1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name: Extended Description |
---|
4 | Version: auto |
---|
5 | Description: Add multilinguale descriptions, banner, NMB, category name, etc... |
---|
6 | Plugin URI: http://piwigo.org/ext/extension_view.php?eid=175 |
---|
7 | Author: P@t & Grum |
---|
8 | */ |
---|
9 | |
---|
10 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
11 | define('EXTENDED_DESC_PATH' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/'); |
---|
12 | load_language('plugin.lang', EXTENDED_DESC_PATH); |
---|
13 | |
---|
14 | global $conf; |
---|
15 | |
---|
16 | $extdesc_conf = array( |
---|
17 | 'more' => '<!--more-->', |
---|
18 | 'complete' => '<!--complete-->', |
---|
19 | 'up-down' => '<!--up-down-->', |
---|
20 | 'not_visible' => '<!--hidden-->', |
---|
21 | 'mb_not_visible' => '<!--mb-hidden-->' |
---|
22 | ); |
---|
23 | |
---|
24 | $conf['ExtendedDescription'] = isset($conf['ExtendedDescription']) ? |
---|
25 | array_merge($extdesc_conf, $conf['ExtendedDescription']) : |
---|
26 | $extdesc_conf; |
---|
27 | |
---|
28 | |
---|
29 | // Traite les balises [lang=xx] |
---|
30 | function get_user_language_desc($desc) |
---|
31 | { |
---|
32 | global $user; |
---|
33 | |
---|
34 | $user_lang = substr($user['language'], 0, 2); |
---|
35 | |
---|
36 | if (!substr_count(strtolower($desc), '[lang=' . $user_lang . ']')) |
---|
37 | { |
---|
38 | $user_lang = 'default'; |
---|
39 | } |
---|
40 | |
---|
41 | if (substr_count(strtolower($desc), '[lang=' . $user_lang . ']')) |
---|
42 | { |
---|
43 | // la balise avec la langue de l'utilisateur a été trouvée |
---|
44 | $patterns[] = '#(^|\[/lang\])(.*?)(\[lang=(' . $user_lang . '|all)\]|$)#is'; |
---|
45 | $replacements[] = ''; |
---|
46 | $patterns[] = '#\[lang=(' . $user_lang . '|all)\](.*?)\[/lang\]#is'; |
---|
47 | $replacements[] = '\\1'; |
---|
48 | } |
---|
49 | else |
---|
50 | { |
---|
51 | // la balise avec la langue de l'utilisateur n'a pas été trouvée |
---|
52 | // On prend tout ce qui est hors balise |
---|
53 | $patterns[] = '#\[lang=all\](.*?)\[/lang\]#is'; |
---|
54 | $replacements[] = '\\1'; |
---|
55 | $patterns[] = '#\[lang=.*\].*\[/lang\]#is'; |
---|
56 | $replacements[] = ''; |
---|
57 | } |
---|
58 | return preg_replace($patterns, $replacements, $desc); |
---|
59 | } |
---|
60 | |
---|
61 | // Traite les autres balises |
---|
62 | function get_extended_desc($desc, $param='') |
---|
63 | { |
---|
64 | global $conf, $page; |
---|
65 | |
---|
66 | if ($param == 'main_page_category_description' and isset($page['category']) and !isset($page['image_id']) and preg_match('#\[redirect (.*?)\]#i', $desc, $m1)) |
---|
67 | { |
---|
68 | if (preg_match('#^(img|cat|search)=(\d*)\.?(\d*|)$#i', $m1[1], $m2)) |
---|
69 | { |
---|
70 | switch ($m2[1]) |
---|
71 | { |
---|
72 | case 'img': |
---|
73 | $url_params = array('image_id' => $m2[2]); |
---|
74 | if (!empty($m2[3])) |
---|
75 | { |
---|
76 | $url_params['category'] = array('id' => $m2[3], 'name' => '', 'permalink' => ''); |
---|
77 | } |
---|
78 | $url = rtrim(make_picture_url($url_params), '-'); |
---|
79 | break; |
---|
80 | |
---|
81 | case 'cat': |
---|
82 | $url_params = array('category' => array('id' => $m2[2], 'name' => '', 'permalink' => '')); |
---|
83 | $url = rtrim(make_index_url($url_params), '-'); |
---|
84 | break; |
---|
85 | |
---|
86 | case 'search': |
---|
87 | $url = make_index_url(array('section' => 'search', 'search' => $m2[2])); |
---|
88 | } |
---|
89 | } |
---|
90 | else |
---|
91 | { |
---|
92 | $url = $m1[1]; |
---|
93 | } |
---|
94 | if (is_admin()) |
---|
95 | { |
---|
96 | global $header_notes; |
---|
97 | $header_notes[] = sprintf(l10n('This category is redirected to %s'), '<a href="'.$url.'">'.$url.'</a>'); |
---|
98 | } |
---|
99 | else |
---|
100 | { |
---|
101 | redirect($url); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | $desc = get_user_language_desc($desc); |
---|
106 | |
---|
107 | // Remove redirect tags for subcatify_category_description |
---|
108 | $patterns[] = '#\[redirect .*?\]#i'; |
---|
109 | $replacements[] = ''; |
---|
110 | |
---|
111 | // Balises [cat=xx] |
---|
112 | $patterns[] = '#\[cat=(\d*)\]#ie'; |
---|
113 | $replacements[] = ($param == 'subcatify_category_description') ? '' : 'get_cat_thumb("$1")'; |
---|
114 | |
---|
115 | // Balises [img=xx.yy,xx.yy,xx.yy;left|rigtht|;name|titleName|] |
---|
116 | //$patterns[] = '#\[img=(\d*)\.?(\d*|);?(left|right|);?(name|titleName|)\]#ie'; |
---|
117 | $patterns[] = '#\[img=([\d\s\.]*);?(left|right|);?(name|titleName|)\]#ie'; |
---|
118 | $replacements[] = ($param == 'subcatify_category_description') ? '' : 'get_img_thumb("$1", "$2", "$3")'; |
---|
119 | |
---|
120 | |
---|
121 | // Balises <!--complete-->, <!--more--> et <!--up-down--> |
---|
122 | switch ($param) |
---|
123 | { |
---|
124 | case 'subcatify_category_description' : |
---|
125 | $patterns[] = '#^(.*?)('. preg_quote($conf['ExtendedDescription']['complete']) . '|' . preg_quote($conf['ExtendedDescription']['more']) . '|' . preg_quote($conf['ExtendedDescription']['up-down']) . ').*$#is'; |
---|
126 | $replacements[] = '$1'; |
---|
127 | $desc = preg_replace($patterns, $replacements, $desc); |
---|
128 | break; |
---|
129 | |
---|
130 | case 'main_page_category_description' : |
---|
131 | $patterns[] = '#^.*' . preg_quote($conf['ExtendedDescription']['complete']) . '|' . preg_quote($conf['ExtendedDescription']['more']) . '#is'; |
---|
132 | $replacements[] = ''; |
---|
133 | $desc = preg_replace($patterns, $replacements, $desc); |
---|
134 | if (substr_count($desc, $conf['ExtendedDescription']['up-down'])) |
---|
135 | { |
---|
136 | list($conf['ExtendedDescription']['top_comment'], $desc) = explode($conf['ExtendedDescription']['up-down'], $desc); |
---|
137 | add_event_handler('loc_end_index', 'add_top_description'); |
---|
138 | } |
---|
139 | break; |
---|
140 | |
---|
141 | default: |
---|
142 | $desc = preg_replace($patterns, $replacements, $desc); |
---|
143 | } |
---|
144 | |
---|
145 | return $desc; |
---|
146 | } |
---|
147 | |
---|
148 | function extended_desc_mail_group_assign_vars($assign_vars) |
---|
149 | { |
---|
150 | if (isset($assign_vars['CPL_CONTENT'])) |
---|
151 | { |
---|
152 | $assign_vars['CPL_CONTENT'] = get_extended_desc($assign_vars['CPL_CONTENT']); |
---|
153 | } |
---|
154 | return $assign_vars; |
---|
155 | } |
---|
156 | |
---|
157 | // Add top description |
---|
158 | function add_top_description() |
---|
159 | { |
---|
160 | global $template, $conf; |
---|
161 | $template->concat('PLUGIN_INDEX_CONTENT_BEGIN', ' |
---|
162 | <div class="additional_info"> |
---|
163 | ' . $conf['ExtendedDescription']['top_comment'] . ' |
---|
164 | </div>'); |
---|
165 | } |
---|
166 | |
---|
167 | // Remove a category |
---|
168 | function ext_remove_cat($tpl_var, $categories) |
---|
169 | { |
---|
170 | global $conf; |
---|
171 | |
---|
172 | $i=0; |
---|
173 | while($i<count($tpl_var)) |
---|
174 | { |
---|
175 | if (substr_count($tpl_var[$i]['NAME'], $conf['ExtendedDescription']['not_visible'])) |
---|
176 | { |
---|
177 | array_splice($tpl_var, $i, 1); |
---|
178 | } |
---|
179 | else |
---|
180 | { |
---|
181 | $i++; |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | return $tpl_var; |
---|
186 | } |
---|
187 | |
---|
188 | // Remove a category from menubar |
---|
189 | function ext_remove_menubar_cats($where) |
---|
190 | { |
---|
191 | global $conf; |
---|
192 | |
---|
193 | $query = 'SELECT id, uppercats |
---|
194 | FROM '.CATEGORIES_TABLE.' |
---|
195 | WHERE name LIKE "%'.$conf['ExtendedDescription']['mb_not_visible'].'%"'; |
---|
196 | |
---|
197 | $result = pwg_query($query); |
---|
198 | while ($row = mysql_fetch_assoc($result)) |
---|
199 | { |
---|
200 | $ids[] = $row['id']; |
---|
201 | $where .= ' |
---|
202 | AND uppercats NOT LIKE "'.$row['uppercats'].',%"'; |
---|
203 | } |
---|
204 | if (!empty($ids)) |
---|
205 | { |
---|
206 | $where .= ' |
---|
207 | AND id NOT IN ('.implode(',', $ids).')'; |
---|
208 | } |
---|
209 | return $where; |
---|
210 | } |
---|
211 | |
---|
212 | // Remove an image |
---|
213 | function ext_remove_image($tpl_var, $pictures) |
---|
214 | { |
---|
215 | global $conf; |
---|
216 | |
---|
217 | $i=0; |
---|
218 | while($i<count($tpl_var)) |
---|
219 | { |
---|
220 | if (substr_count($pictures[$i]['name'], $conf['ExtendedDescription']['not_visible'])) |
---|
221 | { |
---|
222 | array_splice($tpl_var, $i, 1); |
---|
223 | array_splice($pictures, $i, 1); |
---|
224 | } |
---|
225 | else |
---|
226 | { |
---|
227 | $i++; |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | return $tpl_var; |
---|
232 | } |
---|
233 | |
---|
234 | // Return html code for caterogy thumb |
---|
235 | function get_cat_thumb($elem_id) |
---|
236 | { |
---|
237 | global $template; |
---|
238 | |
---|
239 | $query = 'SELECT cat.id, cat.name, cat.comment, cat.representative_picture_id, cat.permalink, |
---|
240 | uc.nb_images, uc.count_images, uc.count_categories, |
---|
241 | img.path, img.tn_ext |
---|
242 | FROM ' . CATEGORIES_TABLE . ' AS cat |
---|
243 | INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' as uc |
---|
244 | INNER JOIN ' . IMAGES_TABLE . ' AS img |
---|
245 | ON cat.id = uc.cat_id |
---|
246 | AND img.id = cat.representative_picture_id |
---|
247 | WHERE cat.id = ' . $elem_id . ';'; |
---|
248 | $result = pwg_query($query); |
---|
249 | |
---|
250 | if($result) |
---|
251 | { |
---|
252 | $template->set_filename('extended_description_content', dirname(__FILE__) . '/template/cat.tpl'); |
---|
253 | while($category=mysql_fetch_array($result)) |
---|
254 | { |
---|
255 | $template->assign(array( |
---|
256 | 'ID' => $category['id'], |
---|
257 | 'TN_SRC' => get_thumbnail_url($category), |
---|
258 | 'TN_ALT' => strip_tags($category['name']), |
---|
259 | 'URL' => make_index_url(array('category' => $category)), |
---|
260 | 'CAPTION_NB_IMAGES' => get_display_images_count |
---|
261 | ( |
---|
262 | $category['nb_images'], |
---|
263 | $category['count_images'], |
---|
264 | $category['count_categories'], |
---|
265 | true, |
---|
266 | '<br />' |
---|
267 | ), |
---|
268 | 'DESCRIPTION' => |
---|
269 | trigger_event('render_category_literal_description', |
---|
270 | trigger_event('render_category_description', |
---|
271 | @$category['comment'], |
---|
272 | 'subcatify_category_description')), |
---|
273 | 'NAME' => trigger_event( |
---|
274 | 'render_category_name', |
---|
275 | $category['name'], |
---|
276 | 'subcatify_category_name' |
---|
277 | ), |
---|
278 | )); |
---|
279 | } |
---|
280 | return $template->parse('extended_description_content', true); |
---|
281 | } |
---|
282 | return ''; |
---|
283 | } |
---|
284 | |
---|
285 | // Return html code for img thumb |
---|
286 | //function get_img_thumb($elem_id, $cat_id='', $align='', $name='') |
---|
287 | function get_img_thumb($elem_ids, $align='', $name='') |
---|
288 | { |
---|
289 | global $template; |
---|
290 | |
---|
291 | $ids=explode(" ",$elem_ids); |
---|
292 | $assoc = array(); |
---|
293 | foreach($ids as $key=>$val) |
---|
294 | { |
---|
295 | list($a,$b)=array_pad(explode(".",$val),2,""); |
---|
296 | $assoc[0][]=$a; |
---|
297 | $assoc[1][]=$b; |
---|
298 | } |
---|
299 | |
---|
300 | $query = 'SELECT * FROM ' . IMAGES_TABLE . ' WHERE id in (' . implode(",",$assoc[0]). ');'; |
---|
301 | $result = pwg_query($query); |
---|
302 | |
---|
303 | if($result) |
---|
304 | { |
---|
305 | $template->set_filename('extended_description_content', dirname(__FILE__) . '/template/img.tpl'); |
---|
306 | |
---|
307 | $imglist=array(); |
---|
308 | while ($picture = mysql_fetch_array($result)) |
---|
309 | { |
---|
310 | $imglist[$picture["id"]]=$picture; |
---|
311 | } |
---|
312 | |
---|
313 | $img=array(); |
---|
314 | for($i=0;$i<count($assoc[0]);$i++) |
---|
315 | { |
---|
316 | if (!empty($assoc[1][$i])) |
---|
317 | { |
---|
318 | $url = make_picture_url(array( |
---|
319 | 'image_id' => $imglist[$assoc[0][$i]]['id'], |
---|
320 | 'category' => array( |
---|
321 | 'id' => $assoc[1][$i], |
---|
322 | 'name' => '', |
---|
323 | 'permalink' => ''))); |
---|
324 | } |
---|
325 | else |
---|
326 | { |
---|
327 | $url = make_picture_url(array('image_id' => $imglist[$assoc[0][$i]]['id'])); |
---|
328 | } |
---|
329 | |
---|
330 | $img[]=array( |
---|
331 | 'ID' => $imglist[$assoc[0][$i]]['id'], |
---|
332 | 'IMAGE' => get_thumbnail_url($imglist[$assoc[0][$i]]), |
---|
333 | 'IMAGE_ALT' => $imglist[$assoc[0][$i]]['file'], |
---|
334 | 'IMG_TITLE' => ($name=="titleName")?htmlspecialchars($imglist[$assoc[0][$i]]['name'], ENT_QUOTES):get_thumbnail_title($imglist[$assoc[0][$i]]), |
---|
335 | 'U_IMG_LINK' => $url, |
---|
336 | 'LEGEND' => ($name=="name")?$imglist[$assoc[0][$i]]['name']:"", |
---|
337 | 'FLOAT' => !empty($align) ? 'float: ' . $align . ';' : '', |
---|
338 | 'COMMENT' => $imglist[$assoc[0][$i]]['file']); |
---|
339 | |
---|
340 | |
---|
341 | } |
---|
342 | $template->assign('img', $img); |
---|
343 | return $template->parse('extended_description_content', true); |
---|
344 | } |
---|
345 | return ''; |
---|
346 | } |
---|
347 | |
---|
348 | |
---|
349 | if (script_basename() == 'admin' or script_basename() == 'popuphelp') |
---|
350 | { |
---|
351 | include(EXTENDED_DESC_PATH . 'admin.inc.php'); |
---|
352 | } |
---|
353 | |
---|
354 | add_event_handler ('render_page_banner', 'get_user_language_desc'); |
---|
355 | add_event_handler ('render_category_name', 'get_user_language_desc'); |
---|
356 | add_event_handler ('render_category_description', 'get_extended_desc', EVENT_HANDLER_PRIORITY_NEUTRAL, 2); |
---|
357 | add_event_handler ('render_element_description', 'get_extended_desc'); |
---|
358 | add_event_handler ('nbm_render_user_customize_mail_content', 'get_extended_desc'); |
---|
359 | add_event_handler ('mail_group_assign_vars', 'extended_desc_mail_group_assign_vars'); |
---|
360 | add_event_handler ('loc_end_index_category_thumbnails', 'ext_remove_cat', EVENT_HANDLER_PRIORITY_NEUTRAL, 2); |
---|
361 | add_event_handler ('loc_end_index_thumbnails', 'ext_remove_image', EVENT_HANDLER_PRIORITY_NEUTRAL, 2); |
---|
362 | add_event_handler ('get_categories_menu_sql_where', 'ext_remove_menubar_cats'); |
---|
363 | ?> |
---|