source: extensions/ExtendedDescription/include/functions.inc.php @ 27027

Last change on this file since 27027 was 27027, checked in by mistic100, 10 years ago

use preg_replace_callback, fix random with no argument

File size: 11.9 KB
Line 
1<?php
2defined('EXTENDED_DESC_PATH') or die('Hacking attempt!');
3
4/**
5 * Return html code for  category thumb
6 */
7function get_cat_thumb($elem_id)
8{
9  global $template, $user;
10
11  $elem_id = intval($elem_id);
12  if ($elem_id<=0)
13  {
14    return '';
15  }
16
17  $query = '
18SELECT
19  cat.id,
20  cat.name,
21  cat.comment,
22  cat.representative_picture_id,
23  cat.permalink,
24  uc.nb_images,
25  uc.count_images,
26  uc.count_categories,
27  img.path
28FROM ' . CATEGORIES_TABLE . ' AS cat
29  INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' as uc
30    ON cat.id = uc.cat_id AND uc.user_id = '.$user['id'].'
31  INNER JOIN ' . IMAGES_TABLE . ' AS img
32    ON img.id = uc.user_representative_picture_id
33WHERE cat.id = ' . $elem_id . ';';
34  $result = pwg_query($query);
35
36  if($result and $category = pwg_db_fetch_assoc($result))
37  {
38    $template->assign(
39      array(
40        'ID'    => $category['id'],
41        'TN_SRC'   => DerivativeImage::thumb_url(array(
42                                  'id' => $category['representative_picture_id'],
43                                  'path' => $category['path'],
44                                )),
45        'TN_ALT'   => strip_tags($category['name']),
46        'URL'   => make_index_url(array('category' => $category)),
47        'CAPTION_NB_IMAGES' => get_display_images_count
48                                (
49                                  $category['nb_images'],
50                                  $category['count_images'],
51                                  $category['count_categories'],
52                                  true,
53                                  '<br />'
54                                ),
55        'DESCRIPTION' =>
56          trigger_event('render_category_literal_description',
57            trigger_event('render_category_description',
58              @$category['comment'],
59              'subcatify_category_description')),
60        'NAME'  => trigger_event(
61                     'render_category_name',
62                     $category['name'],
63                     'subcatify_category_name'
64                   ),
65      )
66    );
67
68    $template->set_filename('extended_description_content', realpath(EXTENDED_DESC_PATH . 'template/cat.tpl'));
69    return $template->parse('extended_description_content', true);
70  }
71  return '';
72}
73
74/**
75 * Return html code for a photo
76 *
77 * @int    id:    picture id
78 * @int    album: album to display picture in    (default: null)
79 * @string size:  picture size                   (default: M)
80 * @string html:  return complete html structure (default: yes)
81 * @string link:  add a link to the picture      (default: yes)
82 */
83function get_photo_sized($param)
84{
85  global $template;
86
87  $default_params = array(
88    'id' =>    array('\d+', null),
89    'album' => array('\d+', null),
90    'size' =>  array('SQ|TH|XXS|XS|S|M|L|XL|XXL', 'M'),
91    'html' =>  array('yes|no', 'yes'),
92    'link' =>  array('yes|no', 'yes'),
93    );
94
95  $params = extdesc_parse_parameters($param, $default_params);
96
97  // check picture id
98  if (empty($params['id'])) return 'missing picture id';
99
100  // parameters
101  $params['link'] = $params['link']=='no' ? false : true;
102  $params['html'] = $params['html']=='no' ? false : true;
103  $deriv_type = extdesc_get_deriv_type($params['size']);
104
105  // get picture
106  $query = 'SELECT * FROM ' . IMAGES_TABLE . ' WHERE id = '.$params['id'].';';
107  $result = pwg_query($query);
108
109  if (pwg_db_num_rows($result))
110  {
111    $picture = pwg_db_fetch_assoc($result);
112
113    // url
114    if ($params['link'])
115    {
116      if (!empty($params['album']))
117      {
118        $query = '
119SELECT id, name, permalink
120  FROM '.CATEGORIES_TABLE.'
121  WHERE id = '.$params['album'].'
122;';
123        $category = pwg_db_fetch_assoc(pwg_query($query));
124
125        $url = make_picture_url(array(
126          'image_id' => $picture['id'],
127          'category' => array(
128            'id' => $category['id'],
129            'name' => $category['name'],
130            'permalink' => $category['permalink'],
131            )));
132      }
133      else
134      {
135        $url = make_picture_url(array('image_id' => $picture['id']));
136      }
137    }
138
139    // image
140    $src_image = new SrcImage($picture);
141    $derivatives = DerivativeImage::get_all($src_image);
142    $selected_derivative = $derivatives[$deriv_type];
143
144    $template->assign(array(
145      'ed_image' => array(
146        'selected_derivative' => $selected_derivative,
147        'ALT_IMG' => $picture['file'],
148      )));
149
150    // output
151    if ($params['html'])
152    {
153      $template->set_filename('extended_description_content', realpath(EXTENDED_DESC_PATH . 'template/picture_content.tpl'));
154      $content = $template->parse('extended_description_content', true);
155
156      if ($params['link']) return '<a href="'.$url.'">'.$content.'</a>';
157      else                 return $content;
158    }
159    else
160    {
161      return $selected_derivative->get_url();
162    }
163  }
164
165  return 'invalid picture id';
166}
167
168/**
169 * Return html code for a random photo
170 *
171 * @int    album: select picture from this album (default: all)
172 * @string size:  picture size                   (default: M)
173 * @string html:  return complete html structure (default: yes)
174 * @string link:  add a link to the picture      (default: no)
175 */
176function extdesc_get_random_photo($param)
177{
178  $default_params = array(
179    'album' => array('\d+', null),
180    'cat' =>   array('\d+', null), // historical
181    'size' =>  array('SQ|TH|XXS|XS|S|M|L|XL|XXL', 'M'),
182    'html' =>  array('yes|no', 'yes'),
183    'link' =>  array('yes|no', 'no'),
184    );
185
186  $params = extdesc_parse_parameters($param, $default_params);
187
188  // check album id
189  if ( empty($params['album']) and !empty($params['cat']) )
190  {
191    $params['album'] = $params['cat'];
192  }
193
194  // get picture id
195  $query = '
196SELECT id, category_id
197  FROM '.IMAGES_TABLE.'
198    JOIN '.IMAGE_CATEGORY_TABLE.' ON image_id = id';
199  if (empty($params['album']))
200  {
201    $query.= '
202  WHERE 1=1 '
203      .get_sql_condition_FandF(array(
204        'forbidden_categories' => 'category_id',
205        'visible_categories' => 'category_id',
206        'visible_images' => 'id'
207        ),
208      'AND'
209      );
210  }
211  else
212  {
213    $query.= '
214  WHERE category_id = '.$params['album'];
215  }
216
217  $query.= '
218  ORDER BY '.DB_RANDOM_FUNCTION.'()
219  LIMIT 1
220;';
221  $result = pwg_query($query);
222
223  if (pwg_db_num_rows($result))
224  {
225    list($params['id'], $params['album']) = pwg_db_fetch_row($result);
226    return get_photo_sized($params);
227  }
228
229  return '';
230}
231
232/**
233 * Return html code for a nivo slider (album or list is mandatory)
234 *
235 * @int    album:     select pictures from this album
236 * @int    nb_images: display only x pictures           (default: 10)
237 * @string random:    random sort order                 (default: no)
238 *
239 * @string list:      pictures id separated by a comma
240 *
241 * @string size:      picture size                      (default: M)
242 * @int    speed:     slideshow duration                (default: 3)
243 * @string title:     display picture name              (default: no)
244 * @string effect:    transition effect                 (default: fade)
245 * @string arrows:    display navigation arrows         (default: yes)
246 * @string control:   display navigation bar            (default: yes)
247 * @string elastic:   adapt slider size to each picture (default: yes)
248 * @int thumbs_size:  size of thumbnails if control=thumb (default: 80)
249 */
250function get_slider($param)
251{
252  global $template, $conf;
253
254  $default_params = array(
255    'album' =>     array('\d+', null),
256    'nb_images' => array('\d+', 10),
257    'random' =>    array('yes|no', 'no'),
258    'list' =>      array('[\d,]+', null),
259    'size' =>      array('SQ|TH|XXS|XS|S|M|L|XL|XXL', 'M'),
260    'speed' =>     array('\d+', 3),
261    'title' =>     array('yes|no', 'no'),
262    'effect' =>    array('[a-zA-Z]+', 'fade'),
263    'arrows' =>    array('yes|no', 'yes'),
264    'control' =>   array('yes|no|thumb', 'yes'),
265    'elastic' =>   array('yes|no', 'yes'),
266    'thumbs_size' => array('\d+', 80),
267    );
268
269  $params = extdesc_parse_parameters($param, $default_params);
270
271  // check size
272  $deriv_type = extdesc_get_deriv_type($params['size']);
273  $enabled = ImageStdParams::get_defined_type_map();
274  if (empty($enabled[ $deriv_type ]))
275  {
276    return '(nivoSlider) size disabled';
277  }
278
279  // parameters
280  if ($params['control'] === 'thumb')
281  {
282    $params['control'] = 'yes';
283    $params['control_thumbs'] = true;
284  }
285  else
286  {
287    $params['control_thumbs'] = false;
288  }
289  $params['arrows'] = $params['arrows']==='yes';
290  $params['control'] = $params['control']==='yes';
291  $params['elastic'] = $params['elastic']==='yes';
292  $params['title'] = $params['title']==='yes';
293  $params['random'] = $params['random']==='yes';
294
295  $tpl_vars = $params;
296
297  // pictures from album...
298  if (!empty($params['album']))
299  {
300    // get image order inside category
301    if ($params['random'])
302    {
303      $order_by = DB_RANDOM_FUNCTION.'()';
304    }
305    else
306    {
307      $query = '
308SELECT image_order
309  FROM '.CATEGORIES_TABLE.'
310  WHERE id = '.$params['album'].'
311;';
312      list($order_by) = pwg_db_fetch_row(pwg_query($query));
313      if (empty($order_by))
314      {
315        $order_by = str_replace('ORDER BY ', null, $conf['order_by_inside_category']);
316      }
317    }
318
319    // get pictures ids
320    $query = '
321SELECT image_id
322  FROM '.IMAGE_CATEGORY_TABLE.' as ic
323    INNER JOIN '.IMAGES_TABLE.' as i
324    ON i.id = ic.image_id
325  WHERE category_id = '.$params['album'].'
326  ORDER BY '.$order_by.'
327  LIMIT '.$params['nb_images'].'
328;';
329    $ids = array_from_query($query, 'image_id');
330    if (empty($ids))
331    {
332      return '(nivoSlider) no photos in album #'.$params['album'];
333    }
334    $ids = implode(',', $ids);
335  }
336  // ...or pictures list
337  else if (empty($params['list']))
338  {
339    return '(nivoSlider) missing album id or photos list';
340  }
341  else
342  {
343    $ids = $params['list'];
344  }
345
346  // get pictures
347  $query = '
348SELECT *
349  FROM '.IMAGES_TABLE.'
350  WHERE id IN ('.$ids.')
351  ORDER BY FIND_IN_SET(id, "'.$ids.'")
352;';
353  $pictures = hash_from_query($query, 'id');
354
355  foreach ($pictures as $row)
356  {
357    // url
358    if (!empty($params['album']))
359    {
360      $url = make_picture_url(array(
361        'image_id' => $row['id'],
362        'category' => array(
363          'id' => $params['album'],
364          'name' => '',
365          'permalink' => '',
366          )));
367    }
368    else
369    {
370      $url = make_picture_url(array('image_id' => $row['id']));
371    }
372
373    $name = render_element_name($row);
374
375    $tpl_vars['elements'][] = array(
376      'ID' => $row['id'],
377      'TN_ALT' => htmlspecialchars(strip_tags($name)),
378      'NAME' => $name,
379      'URL' => $url,
380      'src_image' => new SrcImage($row),
381      );
382  }
383
384  list($tpl_vars['img_size']['w'], $tpl_vars['img_size']['h']) =
385    $enabled[ $deriv_type ]->sizing->ideal_size;
386
387  $tpl_vars['id'] = crc32(uniqid($ids)); // need a unique id if we have multiple sliders
388  $tpl_vars['derivative_params'] = ImageStdParams::get_by_type($deriv_type);
389
390  if ($params['control_thumbs'])
391  {
392    $tpl_vars['derivative_params_thumb'] = ImageStdParams::get_custom(
393      $params['thumbs_size'], $params['thumbs_size'], 1,
394      $params['thumbs_size'], $params['thumbs_size']
395      );
396  }
397
398  $template->assign(array(
399    'EXTENDED_DESC_PATH' => EXTENDED_DESC_PATH,
400    'SLIDER'=> $tpl_vars,
401    ));
402
403  $template->set_filename('extended_description_content', realpath(EXTENDED_DESC_PATH . 'template/slider.tpl'));
404  return $template->parse('extended_description_content', true);
405}
406
407/**
408 * Parse tags parameters
409 */
410function extdesc_parse_parameters($param, $default_params)
411{
412  if (is_array($param))
413  {
414    return $param;
415  }
416
417  $params = array();
418
419  foreach ($default_params as $name => $value)
420  {
421    if (preg_match('#'.$name.'=('.$value[0].')#', $param, $matches))
422    {
423      $params[$name] = $matches[1];
424    }
425    else
426    {
427      $params[$name] = $value[1];
428    }
429  }
430
431  return $params;
432}
433
434/**
435 * Translates shorthand sizes to internal names
436 */
437function extdesc_get_deriv_type($size)
438{
439  $size = strtoupper($size);
440
441  $size_map = array(
442    'SQ' => IMG_SQUARE,
443    'TH' => IMG_THUMB,
444    'XXS' => IMG_XXSMALL,
445    'XS' => IMG_XSMALL,
446    'S' => IMG_SMALL,
447    'M' => IMG_MEDIUM,
448    'L' => IMG_LARGE,
449    'XL' => IMG_XLARGE,
450    'XXL' => IMG_XXLARGE,
451    );
452
453  if (!array_key_exists($size, $size_map))
454  {
455    $size = 'M';
456  }
457
458  return $size_map[$size];
459}
Note: See TracBrowser for help on using the repository browser.