Dotclear

source: plugins/efiMetadatas/_widgets.php @ 1918

Revision 1918, 4.2 KB checked in by JcDenis, 14 years ago (diff)

efiMetadatas 0.1:

  • First lab release
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of efiMetadatas, a plugin for Dotclear 2.
4#
5# Copyright (c) 2009 JC Denis and contributors
6# jcdenis@gdwd.com
7#
8# Licensed under the GPL version 2.0 license.
9# A copy of this license is available in LICENSE file or at
10# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11# -- END LICENSE BLOCK ------------------------------------
12
13if (!defined('DC_RC_PATH')){return;}
14
15$core->addBehavior('initWidgets',array('efiMetadatasWidget','adminEFIM'));
16
17class efiMetadatasWidget
18{
19     public static function adminEFIM($w)
20     {
21          global $core;
22
23          $categories_combo = array('-' => '', __('Uncategorized') => 'null');
24          $categories = $core->blog->getCategories();
25          while($categories->fetch())
26          {
27               $cat_title = html::escapeHTML($categories->cat_title);
28               $categories_combo[$cat_title] = $categories->cat_id;
29          }
30
31          $thumbnail_combo = array(
32               '-' => '',
33               __('square') => 'sq',
34               __('thumbnail') => 't',
35               __('small') => 's',
36               __('medium') => 'm'
37          );
38
39          $w->create('efim',
40               __('Entry first image metadatas'),array('efiMetadatasWidget','publicEFIM')
41          );
42          $w->efim->setting('title',
43               __('Title:'),__('Image infos'),'text'
44          );
45          $w->efim->setting('category',
46               __('Category limit:'),'','combo',$categories_combo
47          );
48          $w->efim->setting('thumbsize',
49               __('Thumbnail size:'),'','combo',$thumbnail_combo
50          );
51          $w->efim->setting('showmeta',
52               __('Show empty metadatas'),0,'check'
53          );
54     }
55
56     public static function publicEFIM($w)
57     {
58          global $core, $_ctx; 
59
60          # Not in post context
61          if (!$_ctx->exists('posts') || !$_ctx->posts->post_id) return;
62
63          # Not supported post type
64          if (!in_array($_ctx->posts->post_type,array('post','gal','galitem'))) return '';
65
66          # Category limit
67          if ($w->category == 'null' && $_ctx->posts->cat_id !== null) return;
68          if ($w->category != 'null' && $w->category != '' && $w->category != $_ctx->posts->cat_id) return;
69
70          # Path and url
71          $p_url = $core->blog->settings->public_url;
72          $p_site = preg_replace('#^(.+?//.+?)/(.*)$#','$1',$core->blog->url);
73          $p_root = $core->blog->public_path;
74
75          # Image pattern
76          $pattern = '(?:'.preg_quote($p_site,'/').')?'.preg_quote($p_url,'/');
77          $pattern = sprintf('/<img.+?src="%s(.*?\.(?:jpg|gif|png))"[^>]+/msu',$pattern);
78
79          # Content lookup
80          $subject = $_ctx->posts->post_excerpt_xhtml.$_ctx->posts->post_content_xhtml;
81
82          # No image
83          if (!preg_match_all($pattern,$subject,$m)) return;
84
85          $src = false;
86          $size = $w->thumbsize;
87          $alt = $metas = $thumb = '';
88          $allowed_ext = array('.jpg','.JPG','.png','.PNG','.gif','.GIF');
89
90          # Loop through images
91          foreach ($m[1] as $i => $img)
92          {
93               $src = false;
94               $info = path::info($img);
95               $base = $info['base'];
96               $ext = $info['extension'];
97
98               # Not original
99               if (preg_match('/^\.(.+)_(sq|t|s|m)$/',$base,$mbase))
100               {
101                    $base = $mbase[1];
102               }
103
104               # Full path
105               $f = $p_root.'/'.$info['dirname'].'/'.$base;
106
107               # Find extension
108               foreach($allowed_ext as $end)
109               {
110                    if (file_exists($f.$end))
111                    {
112                         $src = $f.$end;
113                         break;
114                    }
115               }
116
117               # No file
118               if (!$src) continue;
119
120               # Find thumbnail
121               if (!empty($size))
122               {
123                    $t = $p_root.'/'.$info['dirname'].'/.'.$base.'_'.$size.'.jpg';
124                    if (file_exists($t))
125                    {
126                         $thb = $p_url.(dirname($img) != '/' ? dirname($img) : '').'/.'.$base.'_'.$size.'.jpg';
127                    }
128               }
129
130               # Find image description
131               if (preg_match('/alt="([^"]+)"/',$m[0][$i],$malt))
132               {
133                    $alt = $malt[1];
134               }
135               break;
136          }
137
138          # No image
139          if (!$src || !file_exists($src)) return;
140
141          # Image info       
142          $metas_array = imageMeta::readMeta($src);
143
144          # List metas
145          foreach($metas_array as $k => $v)
146          {
147               if (!$w->showmeta && !$v) continue;
148               $metas .= '<li><strong>'.__($k.':').'</strong><br />'.$v.'</li>';
149          }
150
151          # No meta
152          if (empty($metas)) return;
153
154
155          # thumbnail
156          if (!empty($thb))
157          {
158               $thumb = 
159               '<div class="img-box">'.               
160               '<div class="img-thumbnail">'.
161               '<img alt="'.$alt.'" src="'.$thb.'" />'.
162               '</div>'.
163               "</div>\n";
164          }
165
166          return 
167          '<div class="entryFirstImageMetas">'.
168          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
169          $thumb.
170          '<ul>'.$metas.'</ul>'.
171          '</div>';
172     }
173}
174?>
Note: See TracBrowser for help on using the repository browser.

Sites map