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

Last change on this file since 12523 was 12400, checked in by patdenice, 13 years ago

[cat] tag is now compatible with 2.3

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