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, $user_lang=null) |
---|
31 | { |
---|
32 | if (is_null($user_lang)) |
---|
33 | { |
---|
34 | global $user; |
---|
35 | $user_lang = substr($user['language'], 0, 2); |
---|
36 | } |
---|
37 | |
---|
38 | if (!substr_count(strtolower($desc), '[lang=' . $user_lang . ']')) |
---|
39 | { |
---|
40 | $user_lang = 'default'; |
---|
41 | } |
---|
42 | |
---|
43 | if (substr_count(strtolower($desc), '[lang=' . $user_lang . ']')) |
---|
44 | { |
---|
45 | // la balise avec la langue de l'utilisateur a été trouvée |
---|
46 | $patterns[] = '#(^|\[/lang\])(.*?)(\[lang=(' . $user_lang . '|all)\]|$)#is'; |
---|
47 | $replacements[] = ''; |
---|
48 | $patterns[] = '#\[lang=(' . $user_lang . '|all)\](.*?)\[/lang\]#is'; |
---|
49 | $replacements[] = '\\1'; |
---|
50 | } |
---|
51 | else |
---|
52 | { |
---|
53 | // la balise avec la langue de l'utilisateur n'a pas été trouvée |
---|
54 | // On prend tout ce qui est hors balise |
---|
55 | $patterns[] = '#\[lang=all\](.*?)\[/lang\]#is'; |
---|
56 | $replacements[] = '\\1'; |
---|
57 | $patterns[] = '#\[lang=.*\].*\[/lang\]#is'; |
---|
58 | $replacements[] = ''; |
---|
59 | } |
---|
60 | return preg_replace($patterns, $replacements, $desc); |
---|
61 | } |
---|
62 | |
---|
63 | function get_user_language_tag_url($tag) |
---|
64 | { |
---|
65 | return get_user_language_desc($tag, get_default_language()); |
---|
66 | } |
---|
67 | |
---|
68 | // Traite les autres balises |
---|
69 | function get_extended_desc($desc, $param='') |
---|
70 | { |
---|
71 | global $conf, $page; |
---|
72 | |
---|
73 | if ($param == 'main_page_category_description' and isset($page['category']) and !isset($page['image_id']) and preg_match('#\[redirect (.*?)\]#i', $desc, $m1)) |
---|
74 | { |
---|
75 | if (preg_match('#^(img|cat|search)=(\d*)\.?(\d*|)$#i', $m1[1], $m2)) |
---|
76 | { |
---|
77 | switch ($m2[1]) |
---|
78 | { |
---|
79 | case 'img': |
---|
80 | $url_params = array('image_id' => $m2[2]); |
---|
81 | if (!empty($m2[3])) |
---|
82 | { |
---|
83 | $url_params['category'] = array('id' => $m2[3], 'name' => '', 'permalink' => ''); |
---|
84 | } |
---|
85 | $url = rtrim(make_picture_url($url_params), '-'); |
---|
86 | break; |
---|
87 | |
---|
88 | case 'cat': |
---|
89 | $url_params = array('category' => array('id' => $m2[2], 'name' => '', 'permalink' => '')); |
---|
90 | $url = rtrim(make_index_url($url_params), '-'); |
---|
91 | break; |
---|
92 | |
---|
93 | case 'search': |
---|
94 | $url = make_index_url(array('section' => 'search', 'search' => $m2[2])); |
---|
95 | } |
---|
96 | } |
---|
97 | else |
---|
98 | { |
---|
99 | $url = $m1[1]; |
---|
100 | } |
---|
101 | if (is_admin()) |
---|
102 | { |
---|
103 | global $header_notes; |
---|
104 | $header_notes[] = sprintf(l10n('This category is redirected to %s'), '<a href="'.$url.'">'.$url.'</a>'); |
---|
105 | } |
---|
106 | else |
---|
107 | { |
---|
108 | redirect($url); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | $desc = get_user_language_desc($desc); |
---|
113 | |
---|
114 | // Remove redirect tags for subcatify_category_description |
---|
115 | $patterns[] = '#\[redirect .*?\]#i'; |
---|
116 | $replacements[] = ''; |
---|
117 | |
---|
118 | // Balises [cat=xx] |
---|
119 | $patterns[] = '#\[cat=(\d*)\]#ie'; |
---|
120 | $replacements[] = ($param == 'subcatify_category_description') ? '' : 'get_cat_thumb("$1")'; |
---|
121 | |
---|
122 | // Balises [img=xx.yy,xx.yy,xx.yy;left|right|;name|titleName|] |
---|
123 | //$patterns[] = '#\[img=(\d*)\.?(\d*|);?(left|right|);?(name|titleName|)\]#ie'; |
---|
124 | $patterns[] = '#\[img=([\d\s\.]*);?(left|right|);?(name|titleName|)\]#ie'; |
---|
125 | $replacements[] = ($param == 'subcatify_category_description') ? '' : 'get_img_thumb("$1", "$2", "$3")'; |
---|
126 | |
---|
127 | // Balises [photo=xx.yy;SQ|TH|XXS|XS|S|M|L|XL|XXL;true|false] |
---|
128 | $patterns[] = '#\[photo=([\d\.]*);?(SQ|TH|XXS|XS|S|M|L|XL|XXL|);?(true|false|)\]#ie'; |
---|
129 | $replacements[] = ($param == 'subcatify_category_description') ? '' : 'get_photo_sized("$1", "$2", "$3")'; |
---|
130 | |
---|
131 | // [random album=xx size=SQ|TH|XXS|XS|S|M|L|XL|XXL] |
---|
132 | $patterns[] = '#\[random\s+(?:album|cat)=(\d+)(\s+size=(SQ|TH|XXS|XS|S|M|L|XL|XXL))?\]#ie'; |
---|
133 | $replacements[] = 'extdesc_get_random_photo("$1", "$3")'; |
---|
134 | |
---|
135 | // Balises <!--complete-->, <!--more--> et <!--up-down--> |
---|
136 | switch ($param) |
---|
137 | { |
---|
138 | case 'subcatify_category_description' : |
---|
139 | $patterns[] = '#^(.*?)('. preg_quote($conf['ExtendedDescription']['complete']) . '|' . preg_quote($conf['ExtendedDescription']['more']) . '|' . preg_quote($conf['ExtendedDescription']['up-down']) . ').*$#is'; |
---|
140 | $replacements[] = '$1'; |
---|
141 | $desc = preg_replace($patterns, $replacements, $desc); |
---|
142 | break; |
---|
143 | |
---|
144 | case 'main_page_category_description' : |
---|
145 | $patterns[] = '#^.*' . preg_quote($conf['ExtendedDescription']['complete']) . '|' . preg_quote($conf['ExtendedDescription']['more']) . '#is'; |
---|
146 | $replacements[] = ''; |
---|
147 | $desc = preg_replace($patterns, $replacements, $desc); |
---|
148 | if (substr_count($desc, $conf['ExtendedDescription']['up-down'])) |
---|
149 | { |
---|
150 | list($desc, $conf['ExtendedDescription']['bottom_comment']) = explode($conf['ExtendedDescription']['up-down'], $desc); |
---|
151 | add_event_handler('loc_end_index', 'add_bottom_description'); |
---|
152 | } |
---|
153 | break; |
---|
154 | |
---|
155 | default: |
---|
156 | $desc = preg_replace($patterns, $replacements, $desc); |
---|
157 | } |
---|
158 | |
---|
159 | return $desc; |
---|
160 | } |
---|
161 | |
---|
162 | function extended_desc_mail_group_assign_vars($assign_vars) |
---|
163 | { |
---|
164 | if (isset($assign_vars['CPL_CONTENT'])) |
---|
165 | { |
---|
166 | $assign_vars['CPL_CONTENT'] = get_extended_desc($assign_vars['CPL_CONTENT']); |
---|
167 | } |
---|
168 | return $assign_vars; |
---|
169 | } |
---|
170 | |
---|
171 | // Add bottom description |
---|
172 | function add_bottom_description() |
---|
173 | { |
---|
174 | global $template, $conf; |
---|
175 | $template->concat('PLUGIN_INDEX_CONTENT_END', ' |
---|
176 | <div class="additional_info"> |
---|
177 | ' . $conf['ExtendedDescription']['bottom_comment'] . ' |
---|
178 | </div>'); |
---|
179 | } |
---|
180 | |
---|
181 | // Remove a category |
---|
182 | function ext_remove_cat($tpl_var, $categories) |
---|
183 | { |
---|
184 | global $conf; |
---|
185 | |
---|
186 | $i=0; |
---|
187 | while($i<count($tpl_var)) |
---|
188 | { |
---|
189 | if (substr_count($tpl_var[$i]['NAME'], $conf['ExtendedDescription']['not_visible'])) |
---|
190 | { |
---|
191 | array_splice($tpl_var, $i, 1); |
---|
192 | } |
---|
193 | else |
---|
194 | { |
---|
195 | $i++; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | return $tpl_var; |
---|
200 | } |
---|
201 | |
---|
202 | // Remove a category from menubar |
---|
203 | function ext_remove_menubar_cats($where) |
---|
204 | { |
---|
205 | global $conf; |
---|
206 | |
---|
207 | $query = 'SELECT id, uppercats |
---|
208 | FROM '.CATEGORIES_TABLE.' |
---|
209 | WHERE name LIKE "%'.$conf['ExtendedDescription']['mb_not_visible'].'%"'; |
---|
210 | |
---|
211 | $result = pwg_query($query); |
---|
212 | while ($row = mysql_fetch_assoc($result)) |
---|
213 | { |
---|
214 | $ids[] = $row['id']; |
---|
215 | $where .= ' |
---|
216 | AND uppercats NOT LIKE "'.$row['uppercats'].',%"'; |
---|
217 | } |
---|
218 | if (!empty($ids)) |
---|
219 | { |
---|
220 | $where .= ' |
---|
221 | AND id NOT IN ('.implode(',', $ids).')'; |
---|
222 | } |
---|
223 | return $where; |
---|
224 | } |
---|
225 | |
---|
226 | // Remove an image |
---|
227 | function ext_remove_image($tpl_var, $pictures) |
---|
228 | { |
---|
229 | global $conf; |
---|
230 | |
---|
231 | $i=0; |
---|
232 | while($i<count($tpl_var)) |
---|
233 | { |
---|
234 | if (substr_count($pictures[$i]['name'], $conf['ExtendedDescription']['not_visible'])) |
---|
235 | { |
---|
236 | array_splice($tpl_var, $i, 1); |
---|
237 | array_splice($pictures, $i, 1); |
---|
238 | } |
---|
239 | else |
---|
240 | { |
---|
241 | $i++; |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | return $tpl_var; |
---|
246 | } |
---|
247 | |
---|
248 | // Return html code for caterogy thumb |
---|
249 | function get_cat_thumb($elem_id) |
---|
250 | { |
---|
251 | global $template, $user; |
---|
252 | |
---|
253 | $query = ' |
---|
254 | SELECT |
---|
255 | cat.id, |
---|
256 | cat.name, |
---|
257 | cat.comment, |
---|
258 | cat.representative_picture_id, |
---|
259 | cat.permalink, |
---|
260 | uc.nb_images, |
---|
261 | uc.count_images, |
---|
262 | uc.count_categories, |
---|
263 | img.path |
---|
264 | FROM ' . CATEGORIES_TABLE . ' AS cat |
---|
265 | INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' as uc |
---|
266 | ON cat.id = uc.cat_id AND uc.user_id = '.$user['id'].' |
---|
267 | INNER JOIN ' . IMAGES_TABLE . ' AS img |
---|
268 | ON img.id = uc.user_representative_picture_id |
---|
269 | WHERE cat.id = ' . $elem_id . ';'; |
---|
270 | $result = pwg_query($query); |
---|
271 | |
---|
272 | if($result and $category = mysql_fetch_array($result)) |
---|
273 | { |
---|
274 | $template->set_filename('extended_description_content', dirname(__FILE__) . '/template/cat.tpl'); |
---|
275 | |
---|
276 | $template->assign( |
---|
277 | array( |
---|
278 | 'ID' => $category['id'], |
---|
279 | 'TN_SRC' => DerivativeImage::thumb_url(array( |
---|
280 | 'id' => $category['representative_picture_id'], |
---|
281 | 'path' => $category['path'], |
---|
282 | )), |
---|
283 | 'TN_ALT' => strip_tags($category['name']), |
---|
284 | 'URL' => make_index_url(array('category' => $category)), |
---|
285 | 'CAPTION_NB_IMAGES' => get_display_images_count |
---|
286 | ( |
---|
287 | $category['nb_images'], |
---|
288 | $category['count_images'], |
---|
289 | $category['count_categories'], |
---|
290 | true, |
---|
291 | '<br />' |
---|
292 | ), |
---|
293 | 'DESCRIPTION' => |
---|
294 | trigger_event('render_category_literal_description', |
---|
295 | trigger_event('render_category_description', |
---|
296 | @$category['comment'], |
---|
297 | 'subcatify_category_description')), |
---|
298 | 'NAME' => trigger_event( |
---|
299 | 'render_category_name', |
---|
300 | $category['name'], |
---|
301 | 'subcatify_category_name' |
---|
302 | ), |
---|
303 | ) |
---|
304 | ); |
---|
305 | |
---|
306 | return $template->parse('extended_description_content', true); |
---|
307 | } |
---|
308 | return ''; |
---|
309 | } |
---|
310 | |
---|
311 | // Return html code for img thumb |
---|
312 | //function get_img_thumb($elem_id, $cat_id='', $align='', $name='') |
---|
313 | function get_img_thumb($elem_ids, $align='', $name='') |
---|
314 | { |
---|
315 | global $template; |
---|
316 | |
---|
317 | $ids=explode(" ",$elem_ids); |
---|
318 | $assoc = array(); |
---|
319 | foreach($ids as $key=>$val) |
---|
320 | { |
---|
321 | list($a,$b)=array_pad(explode(".",$val),2,""); |
---|
322 | $assoc[$a] = $b; |
---|
323 | } |
---|
324 | |
---|
325 | $query = 'SELECT * FROM ' . IMAGES_TABLE . ' WHERE id in (' . implode(',', array_keys($assoc)) . ');'; |
---|
326 | $result = pwg_query($query); |
---|
327 | |
---|
328 | if($result) |
---|
329 | { |
---|
330 | $template->set_filename('extended_description_content', dirname(__FILE__) . '/template/img.tpl'); |
---|
331 | |
---|
332 | $imglist=array(); |
---|
333 | while ($picture = mysql_fetch_array($result)) |
---|
334 | { |
---|
335 | $imglist[$picture["id"]]=$picture; |
---|
336 | } |
---|
337 | |
---|
338 | $img=array(); |
---|
339 | foreach ($imglist as $id => $picture) |
---|
340 | { |
---|
341 | if (!empty($assoc[$id])) |
---|
342 | { |
---|
343 | $url = make_picture_url(array( |
---|
344 | 'image_id' => $picture['id'], |
---|
345 | 'category' => array( |
---|
346 | 'id' => $assoc[$id], |
---|
347 | 'name' => '', |
---|
348 | 'permalink' => ''))); |
---|
349 | } |
---|
350 | else |
---|
351 | { |
---|
352 | $url = make_picture_url(array('image_id' => $picture['id'])); |
---|
353 | } |
---|
354 | |
---|
355 | $img[]=array( |
---|
356 | 'ID' => $picture['id'], |
---|
357 | 'IMAGE' => DerivativeImage::thumb_url($picture), |
---|
358 | 'IMAGE_ALT' => $picture['file'], |
---|
359 | 'IMG_TITLE' => ($name=="titleName")?htmlspecialchars($picture['name'], ENT_QUOTES):get_thumbnail_title($picture, $picture['name'], null), |
---|
360 | 'U_IMG_LINK' => $url, |
---|
361 | 'LEGEND' => ($name=="name")?$picture['name']:"", |
---|
362 | 'COMMENT' => $picture['file'], |
---|
363 | ); |
---|
364 | } |
---|
365 | |
---|
366 | $template->assign('img', $img); |
---|
367 | $template->assign('FLOAT', !empty($align) ? 'float: ' . $align . ';' : ''); |
---|
368 | return $template->parse('extended_description_content', true); |
---|
369 | } |
---|
370 | return ''; |
---|
371 | } |
---|
372 | |
---|
373 | // Return html code for a photo |
---|
374 | function get_photo_sized($elem_id, $size, $show_url) |
---|
375 | { |
---|
376 | global $template; |
---|
377 | |
---|
378 | if (empty($size)) $size = 'M'; |
---|
379 | if (empty($show_url)) $show_url = 'true'; |
---|
380 | |
---|
381 | list($image_id, $cat_id) = array_pad(explode(".",$elem_id), 2, ""); |
---|
382 | |
---|
383 | $size_map = array( |
---|
384 | 'SQ' => IMG_SQUARE, |
---|
385 | 'TH' => IMG_THUMB, |
---|
386 | 'XXS' => IMG_XXSMALL, |
---|
387 | 'XS' => IMG_XSMALL, |
---|
388 | 'S' => IMG_SMALL, |
---|
389 | 'M' => IMG_MEDIUM, |
---|
390 | 'L' => IMG_LARGE, |
---|
391 | 'XL' => IMG_XLARGE, |
---|
392 | 'XXL' => IMG_XXLARGE, |
---|
393 | ); |
---|
394 | |
---|
395 | $deriv_type = $size_map[ strtoupper($size) ]; |
---|
396 | |
---|
397 | $query = 'SELECT * FROM ' . IMAGES_TABLE . ' WHERE id = '.$image_id.';'; |
---|
398 | $result = pwg_query($query); |
---|
399 | |
---|
400 | if (pwg_db_num_rows($result)) |
---|
401 | { |
---|
402 | $template->set_filename('extended_description_content', 'picture_content.tpl'); |
---|
403 | |
---|
404 | $picture = pwg_db_fetch_assoc($result); |
---|
405 | |
---|
406 | // url |
---|
407 | if (!empty($cat_id)) |
---|
408 | { |
---|
409 | $url = make_picture_url(array( |
---|
410 | 'image_id' => $picture['id'], |
---|
411 | 'category' => array( |
---|
412 | 'id' => $cat_id, |
---|
413 | 'name' => '', |
---|
414 | 'permalink' => ''))); |
---|
415 | } |
---|
416 | else |
---|
417 | { |
---|
418 | $url = make_picture_url(array('image_id' => $picture['id'])); |
---|
419 | } |
---|
420 | |
---|
421 | // image |
---|
422 | $src_image = new SrcImage($picture); |
---|
423 | $derivatives = DerivativeImage::get_all($src_image); |
---|
424 | $selected_derivative = $derivatives[$deriv_type]; |
---|
425 | |
---|
426 | $template->assign(array( |
---|
427 | 'current' => array( |
---|
428 | 'selected_derivative' => $selected_derivative, |
---|
429 | 'TITLE' => $picture['name'], |
---|
430 | ), |
---|
431 | 'ALT_IMG' => $picture['file'], |
---|
432 | )); |
---|
433 | if (!empty($picture['comment'])) |
---|
434 | { |
---|
435 | $template-assign('COMMENT_IMG', trigger_event('render_element_description', $picture['comment'])); |
---|
436 | } |
---|
437 | |
---|
438 | $content = $template->parse('extended_description_content', true); |
---|
439 | if (get_boolean($show_url)) |
---|
440 | { |
---|
441 | return '<a href="'.$url.'">'.$content.'</a>'; |
---|
442 | } |
---|
443 | else |
---|
444 | { |
---|
445 | return $content; |
---|
446 | } |
---|
447 | } |
---|
448 | |
---|
449 | return ''; |
---|
450 | } |
---|
451 | |
---|
452 | |
---|
453 | |
---|
454 | function extdesc_get_random_photo($category_id, $size="M") |
---|
455 | { |
---|
456 | include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
457 | |
---|
458 | $query = ' |
---|
459 | SELECT id |
---|
460 | FROM '.IMAGES_TABLE.' |
---|
461 | JOIN '.IMAGE_CATEGORY_TABLE.' ON image_id = id |
---|
462 | WHERE category_id = '.$category_id.' |
---|
463 | ORDER BY '.DB_RANDOM_FUNCTION.'() |
---|
464 | LIMIT 1 |
---|
465 | ;'; |
---|
466 | $result = pwg_query($query); |
---|
467 | |
---|
468 | if (pwg_db_num_rows($result)) |
---|
469 | { |
---|
470 | list($img_id) = pwg_db_fetch_row($result); |
---|
471 | return get_photo_sized($img_id, $size, 'false'); |
---|
472 | } |
---|
473 | |
---|
474 | return ''; |
---|
475 | } |
---|
476 | |
---|
477 | if (script_basename() == 'admin' or script_basename() == 'popuphelp') |
---|
478 | { |
---|
479 | include(EXTENDED_DESC_PATH . 'admin.inc.php'); |
---|
480 | } |
---|
481 | |
---|
482 | add_event_handler ('render_page_banner', 'get_user_language_desc'); |
---|
483 | add_event_handler ('render_category_name', 'get_user_language_desc'); |
---|
484 | add_event_handler ('render_category_description', 'get_extended_desc', EVENT_HANDLER_PRIORITY_NEUTRAL, 2); |
---|
485 | add_event_handler ('render_tag_name', 'get_user_language_desc'); |
---|
486 | add_event_handler ('render_tag_url', 'get_user_language_tag_url', 40); |
---|
487 | add_event_handler ('render_element_description', 'get_extended_desc'); |
---|
488 | add_event_handler ('nbm_render_user_customize_mail_content', 'get_extended_desc'); |
---|
489 | add_event_handler ('mail_group_assign_vars', 'extended_desc_mail_group_assign_vars'); |
---|
490 | add_event_handler ('loc_end_index_category_thumbnails', 'ext_remove_cat', EVENT_HANDLER_PRIORITY_NEUTRAL, 2); |
---|
491 | add_event_handler ('loc_end_index_thumbnails', 'ext_remove_image', EVENT_HANDLER_PRIORITY_NEUTRAL, 2); |
---|
492 | add_event_handler ('get_categories_menu_sql_where', 'ext_remove_menubar_cats'); |
---|
493 | ?> |
---|