source: extensions/ExtendedDescription/main.inc.php @ 14518

Last change on this file since 14518 was 12554, checked in by patdenice, 12 years ago

feature:2493
Extended Description write correct url name for tags.

File size: 12.2 KB
Line 
1<?php
2/*
3Plugin Name: Extended Description
4Version: auto
5Description: Add multilinguale descriptions, banner, NMB, category name, etc...
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=175
7Author: P@t & Grum
8*/
9
10if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
11define('EXTENDED_DESC_PATH' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
12load_language('plugin.lang', EXTENDED_DESC_PATH);
13
14global $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]
30function 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
63function get_user_language_tag_url($tag)
64{
65  return get_user_language_desc($tag, get_default_language());
66}
67
68// Traite les autres balises
69function 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|rigtht|;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    // [random album=xx]
128    $patterns[] = '#\[random\s+(?:album|cat)=\s*?(\d+)\s*?\]#ie';
129    $replacements[] = 'extdesc_get_random_photo("$1")';
130
131  // Balises <!--complete-->, <!--more--> et <!--up-down-->
132  switch ($param)
133  {
134    case 'subcatify_category_description' :
135      $patterns[] = '#^(.*?)('. preg_quote($conf['ExtendedDescription']['complete']) . '|' . preg_quote($conf['ExtendedDescription']['more']) . '|' . preg_quote($conf['ExtendedDescription']['up-down']) . ').*$#is';
136      $replacements[] = '$1';
137      $desc = preg_replace($patterns, $replacements, $desc);
138      break;
139
140    case 'main_page_category_description' :
141      $patterns[] = '#^.*' . preg_quote($conf['ExtendedDescription']['complete']) . '|' . preg_quote($conf['ExtendedDescription']['more']) . '#is';
142      $replacements[] = '';
143      $desc = preg_replace($patterns, $replacements, $desc);
144      if (substr_count($desc, $conf['ExtendedDescription']['up-down']))
145      {
146        list($desc, $conf['ExtendedDescription']['bottom_comment']) = explode($conf['ExtendedDescription']['up-down'], $desc);
147        add_event_handler('loc_end_index', 'add_bottom_description');
148      }
149      break;
150
151    default:
152      $desc = preg_replace($patterns, $replacements, $desc);
153  }
154
155  return $desc;
156}
157
158function extended_desc_mail_group_assign_vars($assign_vars)
159{
160  if (isset($assign_vars['CPL_CONTENT']))
161  {
162    $assign_vars['CPL_CONTENT'] = get_extended_desc($assign_vars['CPL_CONTENT']);
163  }
164  return $assign_vars;
165}
166
167// Add bottom description
168function add_bottom_description()
169{
170  global $template, $conf;
171  $template->concat('PLUGIN_INDEX_CONTENT_END', '
172    <div class="additional_info">
173    ' . $conf['ExtendedDescription']['bottom_comment'] . '
174    </div>');
175}
176
177// Remove a category
178function ext_remove_cat($tpl_var, $categories)
179{
180  global $conf;
181
182  $i=0;
183  while($i<count($tpl_var))
184  {
185    if (substr_count($tpl_var[$i]['NAME'], $conf['ExtendedDescription']['not_visible']))
186    {
187      array_splice($tpl_var, $i, 1);
188    }
189    else
190    {
191      $i++;
192    }
193  }
194
195  return $tpl_var;
196}
197
198// Remove a category from menubar
199function ext_remove_menubar_cats($where)
200{
201  global $conf;
202
203  $query = 'SELECT id, uppercats
204    FROM '.CATEGORIES_TABLE.'
205    WHERE name LIKE "%'.$conf['ExtendedDescription']['mb_not_visible'].'%"';
206
207  $result = pwg_query($query);
208  while ($row = mysql_fetch_assoc($result))
209  {
210    $ids[] = $row['id'];
211    $where .= '
212      AND uppercats NOT LIKE "'.$row['uppercats'].',%"';
213  }
214  if (!empty($ids))
215  {
216    $where .= '
217      AND id NOT IN ('.implode(',', $ids).')';
218  }
219  return $where;
220}
221
222// Remove an image
223function ext_remove_image($tpl_var, $pictures)
224{
225  global $conf;
226
227  $i=0;
228  while($i<count($tpl_var))
229  {
230    if (substr_count($pictures[$i]['name'], $conf['ExtendedDescription']['not_visible']))
231    {
232      array_splice($tpl_var, $i, 1);
233      array_splice($pictures, $i, 1);
234    }
235    else
236    {
237      $i++;
238    }
239  }
240
241  return $tpl_var;
242}
243
244// Return html code for  caterogy thumb
245function get_cat_thumb($elem_id)
246{
247  global $template, $user;
248
249  $query = 'SELECT
250cat.id, cat.name, cat.comment, cat.representative_picture_id, cat.permalink, uc.nb_images, uc.count_images, uc.count_categories, img.path, img.tn_ext
251FROM ' . CATEGORIES_TABLE . ' AS cat
252INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' as uc
253  ON cat.id = uc.cat_id AND user_id = '.$user['id'].'
254INNER JOIN ' . IMAGES_TABLE . ' AS img
255  ON img.id = uc.user_representative_picture_id
256WHERE cat.id = ' . $elem_id . ';';
257  $result = pwg_query($query);
258
259  if($result and $category = mysql_fetch_array($result))
260  {
261    $template->set_filename('extended_description_content', dirname(__FILE__) . '/template/cat.tpl');
262
263    $template->assign(
264      array(
265        'ID'    => $category['id'],
266        'TN_SRC'   => get_thumbnail_url($category),
267        'TN_ALT'   => strip_tags($category['name']),
268        'URL'   => make_index_url(array('category' => $category)),
269        'CAPTION_NB_IMAGES' => get_display_images_count
270                                (
271                                  $category['nb_images'],
272                                  $category['count_images'],
273                                  $category['count_categories'],
274                                  true,
275                                  '<br />'
276                                ),
277        'DESCRIPTION' =>
278          trigger_event('render_category_literal_description',
279            trigger_event('render_category_description',
280              @$category['comment'],
281              'subcatify_category_description')),
282        'NAME'  => trigger_event(
283                     'render_category_name',
284                     $category['name'],
285                     'subcatify_category_name'
286                   ),
287      )
288    );
289
290    return $template->parse('extended_description_content', true);
291  }
292  return '';
293}
294
295// Return html code for img thumb
296//function get_img_thumb($elem_id, $cat_id='', $align='', $name='')
297function get_img_thumb($elem_ids, $align='', $name='')
298{
299  global $template;
300
301  $ids=explode(" ",$elem_ids);
302  $assoc = array();
303  foreach($ids as $key=>$val)
304  {
305    list($a,$b)=array_pad(explode(".",$val),2,"");
306    $assoc[0][]=$a;
307    $assoc[1][]=$b;
308  }
309
310  $query = 'SELECT * FROM ' . IMAGES_TABLE . ' WHERE id in (' . implode(",",$assoc[0]). ');';
311  $result = pwg_query($query);
312
313  if($result)
314  {
315    $template->set_filename('extended_description_content', dirname(__FILE__) . '/template/img.tpl');
316
317    $imglist=array();
318    while ($picture = mysql_fetch_array($result))
319    {
320      $imglist[$picture["id"]]=$picture;
321    }
322
323    $img=array();
324    for($i=0;$i<count($assoc[0]);$i++)
325    {
326      if (!empty($assoc[1][$i]))
327      {
328        $url = make_picture_url(array(
329          'image_id' => $imglist[$assoc[0][$i]]['id'],
330          'category' => array(
331            'id' => $assoc[1][$i],
332            'name' => '',
333            'permalink' => '')));
334      }
335      else
336      {
337        $url = make_picture_url(array('image_id' => $imglist[$assoc[0][$i]]['id']));
338      }
339
340      $img[]=array(
341          'ID'          => $imglist[$assoc[0][$i]]['id'],
342          'IMAGE'       => get_thumbnail_url($imglist[$assoc[0][$i]]),
343          'IMAGE_ALT'   => $imglist[$assoc[0][$i]]['file'],
344          'IMG_TITLE'   => ($name=="titleName")?htmlspecialchars($imglist[$assoc[0][$i]]['name'], ENT_QUOTES):get_thumbnail_title($imglist[$assoc[0][$i]]),
345          'U_IMG_LINK'  => $url,
346          'LEGEND'  => ($name=="name")?$imglist[$assoc[0][$i]]['name']:"",
347          'FLOAT' => !empty($align) ? 'float: ' . $align . ';' : '',
348          'COMMENT' => $imglist[$assoc[0][$i]]['file']);
349
350
351    }
352     $template->assign('img', $img);
353    return $template->parse('extended_description_content', true);
354  }
355  return '';
356}
357
358
359function extdesc_get_random_photo($category_id)
360{
361  include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
362 
363  $query = '
364SELECT
365    id,
366    path
367  FROM '.IMAGES_TABLE.'
368    JOIN '.IMAGE_CATEGORY_TABLE.' ON image_id = id
369  WHERE category_id = '.$category_id.'
370  ORDER BY '.DB_RANDOM_FUNCTION.'()
371  LIMIT 1
372;';
373  $result = pwg_query($query);
374  while ($row = pwg_db_fetch_assoc($result))
375  {
376    return '<img src="'.get_element_url($row).'">';
377  }
378
379  return '';
380}
381
382if (script_basename() == 'admin' or script_basename() == 'popuphelp')
383{
384  include(EXTENDED_DESC_PATH . 'admin.inc.php');
385}
386
387add_event_handler ('render_page_banner', 'get_user_language_desc');
388add_event_handler ('render_category_name', 'get_user_language_desc');
389add_event_handler ('render_category_description', 'get_extended_desc', EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
390add_event_handler ('render_tag_name', 'get_user_language_desc');
391add_event_handler ('render_tag_url', 'get_user_language_tag_url', 40);
392add_event_handler ('render_element_description', 'get_extended_desc');
393add_event_handler ('nbm_render_user_customize_mail_content', 'get_extended_desc');
394add_event_handler ('mail_group_assign_vars', 'extended_desc_mail_group_assign_vars');
395add_event_handler ('loc_end_index_category_thumbnails', 'ext_remove_cat', EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
396add_event_handler ('loc_end_index_thumbnails', 'ext_remove_image', EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
397add_event_handler ('get_categories_menu_sql_where', 'ext_remove_menubar_cats');
398?>
Note: See TracBrowser for help on using the repository browser.