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

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

add [login-link] and [logged] blocks
allow "true" and "false" for boolean parameters
fix "title" parameter for [slider]

File size: 12.8 KB
Line 
1<?php
2defined('EXTENDED_DESC_PATH') or die('Hacking attempt!');
3
4/**
5 * Return html code for  category thumb
6 */
7function extdesc_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_change('render_category_literal_description',
57            trigger_change('render_category_description',
58              @$category['comment'],
59              'subcatify_category_description')),
60        'NAME'  => trigger_change(
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 * @bool   html:  return complete html structure (default: true)
81 * @bool   link:  add a link to the picture      (default: true)
82 */
83function extdesc_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('boolean', true),
92    'link' =>  array('boolean', true),
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  $deriv_type = extdesc_get_deriv_type($params['size']);
102
103  // get picture
104  $query = 'SELECT * FROM ' . IMAGES_TABLE . ' WHERE id = '.$params['id'].';';
105  $result = pwg_query($query);
106
107  if (pwg_db_num_rows($result))
108  {
109    $picture = pwg_db_fetch_assoc($result);
110
111    // url
112    if ($params['link'])
113    {
114      if (!empty($params['album']))
115      {
116        $query = '
117SELECT id, name, permalink
118  FROM '.CATEGORIES_TABLE.'
119  WHERE id = '.$params['album'].'
120;';
121        $category = pwg_db_fetch_assoc(pwg_query($query));
122
123        $url = make_picture_url(array(
124          'image_id' => $picture['id'],
125          'category' => array(
126            'id' => $category['id'],
127            'name' => $category['name'],
128            'permalink' => $category['permalink'],
129            )));
130      }
131      else
132      {
133        $url = make_picture_url(array('image_id' => $picture['id']));
134      }
135    }
136
137    // image
138    $src_image = new SrcImage($picture);
139    $derivatives = DerivativeImage::get_all($src_image);
140    $selected_derivative = $derivatives[$deriv_type];
141
142    $template->assign(array(
143      'ed_image' => array(
144        'selected_derivative' => $selected_derivative,
145        'ALT_IMG' => $picture['file'],
146      )));
147
148    // output
149    if ($params['html'])
150    {
151      $template->set_filename('extended_description_content', realpath(EXTENDED_DESC_PATH . 'template/picture_content.tpl'));
152      $content = $template->parse('extended_description_content', true);
153
154      if ($params['link']) return '<a href="'.$url.'">'.$content.'</a>';
155      else                 return $content;
156    }
157    else
158    {
159      return $selected_derivative->get_url();
160    }
161  }
162
163  return 'invalid picture id';
164}
165
166/**
167 * Return html code for a random photo
168 *
169 * @int    album: select picture from this album (default: all)
170 * @string size:  picture size                   (default: M)
171 * @bool   html:  return complete html structure (default: true)
172 * @bool   link:  add a link to the picture      (default: false)
173 */
174function extdesc_get_random_photo($param)
175{
176  $default_params = array(
177    'album' => array('\d+', null),
178    'cat' =>   array('\d+', null), // historical
179    'size' =>  array('SQ|TH|XXS|XS|S|M|L|XL|XXL', 'M'),
180    'html' =>  array('boolean', true),
181    'link' =>  array('boolean', false),
182    );
183
184  $params = extdesc_parse_parameters($param, $default_params);
185
186  // check album id
187  if ( empty($params['album']) and !empty($params['cat']) )
188  {
189    $params['album'] = $params['cat'];
190  }
191
192  // get picture id
193  $query = '
194SELECT id, category_id
195  FROM '.IMAGES_TABLE.'
196    JOIN '.IMAGE_CATEGORY_TABLE.' ON image_id = id';
197  if (empty($params['album']))
198  {
199    $query.= '
200  WHERE 1=1 '
201      .get_sql_condition_FandF(array(
202        'forbidden_categories' => 'category_id',
203        'visible_categories' => 'category_id',
204        'visible_images' => 'id'
205        ),
206      'AND'
207      );
208  }
209  else
210  {
211    $query.= '
212  WHERE category_id = '.$params['album'];
213  }
214
215  $query.= '
216  ORDER BY '.DB_RANDOM_FUNCTION.'()
217  LIMIT 1
218;';
219  $result = pwg_query($query);
220
221  if (pwg_db_num_rows($result))
222  {
223    list($params['id'], $params['album']) = pwg_db_fetch_row($result);
224    return extdesc_get_photo_sized($params);
225  }
226
227  return '';
228}
229
230/**
231 * Return html code for a nivo slider (album or list is mandatory)
232 *
233 * @int    album:     select pictures from this album
234 * @int    nb_images: display only x pictures           (default: 10)
235 * @string random:    random sort order                 (default: no)
236 *
237 * @string list:      pictures id separated by a comma
238 *
239 * @string size:      picture size                      (default: M)
240 * @int    speed:     slideshow duration                (default: 3)
241 * @bool   title:     display picture name              (default: false)
242 * @string effect:    transition effect                 (default: fade)
243 * @bool   arrows:    display navigation arrows         (default: true)
244 * @string control:   display navigation bar            (default: true)
245 * @bool   elastic:   adapt slider size to each picture (default: true)
246 * @int thumbs_size:  size of thumbnails if control=thumb (default: 80)
247 */
248function extdesc_get_slider($param)
249{
250  global $template, $conf;
251
252  $default_params = array(
253    'album' =>     array('\d+', null),
254    'nb_images' => array('\d+', 10),
255    'random' =>    array('boolean', false),
256    'list' =>      array('[\d,]+', null),
257    'size' =>      array('SQ|TH|XXS|XS|S|M|L|XL|XXL', 'M'),
258    'speed' =>     array('\d+', 5),
259    'title' =>     array('boolean', false),
260    'effect' =>    array('[a-zA-Z]+', 'fade'),
261    'arrows' =>    array('boolean', true),
262    'control' =>   array('yes|no|true|false|thumb', true),
263    'elastic' =>   array('boolean', true),
264    'thumbs_size' => array('\d+', 80),
265    );
266
267  $params = extdesc_parse_parameters($param, $default_params);
268
269  // check size
270  $deriv_type = extdesc_get_deriv_type($params['size']);
271  $enabled = ImageStdParams::get_defined_type_map();
272  if (empty($enabled[ $deriv_type ]))
273  {
274    return '(nivoSlider) size disabled';
275  }
276
277  // parameters
278  if ($params['control'] === 'thumb')
279  {
280    $params['control'] = true;
281    $params['control_thumbs'] = true;
282  }
283  else
284  {
285    $params['control'] = filter_var($params['control'], FILTER_VALIDATE_BOOLEAN);
286    $params['control_thumbs'] = false;
287  }
288
289  $tpl_vars = $params;
290
291  // pictures from album...
292  if (!empty($params['album']))
293  {
294    // get image order inside category
295    if ($params['random'])
296    {
297      $order_by = DB_RANDOM_FUNCTION.'()';
298    }
299    else
300    {
301      $query = '
302SELECT image_order
303  FROM '.CATEGORIES_TABLE.'
304  WHERE id = '.$params['album'].'
305;';
306      list($order_by) = pwg_db_fetch_row(pwg_query($query));
307      if (empty($order_by))
308      {
309        $order_by = str_replace('ORDER BY ', null, $conf['order_by_inside_category']);
310      }
311    }
312
313    // get pictures ids
314    $query = '
315SELECT image_id
316  FROM '.IMAGE_CATEGORY_TABLE.' as ic
317    INNER JOIN '.IMAGES_TABLE.' as i
318    ON i.id = ic.image_id
319  WHERE category_id = '.$params['album'].'
320  ORDER BY '.$order_by.'
321  LIMIT '.$params['nb_images'].'
322;';
323    $ids = array_from_query($query, 'image_id');
324    if (empty($ids))
325    {
326      return '(nivoSlider) no photos in album #'.$params['album'];
327    }
328    $ids = implode(',', $ids);
329  }
330  // ...or pictures list
331  else if (empty($params['list']))
332  {
333    return '(nivoSlider) missing album id or photos list';
334  }
335  else
336  {
337    $ids = $params['list'];
338  }
339
340  // get pictures
341  $query = '
342SELECT *
343  FROM '.IMAGES_TABLE.'
344  WHERE id IN ('.$ids.')
345  ORDER BY FIND_IN_SET(id, "'.$ids.'")
346;';
347  $pictures = hash_from_query($query, 'id');
348
349  foreach ($pictures as $row)
350  {
351    // url
352    if (!empty($params['album']))
353    {
354      $url = make_picture_url(array(
355        'image_id' => $row['id'],
356        'category' => array(
357          'id' => $params['album'],
358          'name' => '',
359          'permalink' => '',
360          )));
361    }
362    else
363    {
364      $url = make_picture_url(array('image_id' => $row['id']));
365    }
366
367    $name = render_element_name($row);
368
369    $tpl_vars['elements'][] = array(
370      'ID' => $row['id'],
371      'TN_ALT' => htmlspecialchars(strip_tags($name)),
372      'NAME' => $name,
373      'URL' => $url,
374      'src_image' => new SrcImage($row),
375      );
376  }
377
378  list($tpl_vars['img_size']['w'], $tpl_vars['img_size']['h']) =
379    $enabled[ $deriv_type ]->sizing->ideal_size;
380
381  $tpl_vars['id'] = crc32(uniqid($ids)); // need a unique id if we have multiple sliders
382  $tpl_vars['derivative_params'] = ImageStdParams::get_by_type($deriv_type);
383
384  if ($params['control_thumbs'])
385  {
386    $tpl_vars['derivative_params_thumb'] = ImageStdParams::get_custom(
387      $params['thumbs_size'], $params['thumbs_size'], 1,
388      $params['thumbs_size'], $params['thumbs_size']
389      );
390  }
391
392  $template->assign(array(
393    'EXTENDED_DESC_PATH' => EXTENDED_DESC_PATH,
394    'SLIDER'=> $tpl_vars,
395    ));
396
397  $template->set_filename('extended_description_content', realpath(EXTENDED_DESC_PATH . 'template/slider.tpl'));
398  return $template->parse('extended_description_content', true);
399}
400
401/**
402 * Return html code for login link
403 *
404 * @bool   html: return complete html structure (default: true)
405 * @string text: link text, translatable        (default: Login)
406 */
407function extdesc_get_login_link($param)
408{
409  $default_params = array(
410    'html' => array('boolean', true),
411    'text' => array('".*"', ''),
412    );
413
414  $params = extdesc_parse_parameters($param, $default_params);
415 
416  $url =
417      get_root_url().'identification.php?redirect='
418      .urlencode(urlencode($_SERVER['REQUEST_URI']));
419 
420  if ($params['html'])
421  {
422    if (empty($params['text']))
423    {
424      $params['text'] = l10n('Login');
425    }
426    else
427    {
428      $params['text'] = get_user_language_desc(mb_substr($params['text'], 1, -1));
429    }
430 
431    return '<a href="' . $url . '">' . $params['text'] . '</a>';
432  }
433  else
434  {
435    return $url;
436  }
437 
438}
439
440/**
441 * Parse tags parameters
442 */
443function extdesc_parse_parameters($param, $default_params)
444{
445  if (is_array($param))
446  {
447    return $param;
448  }
449
450  $params = array();
451
452  foreach ($default_params as $name => $value)
453  {
454    $is_bool = false;
455    if ($value[0] == 'boolean')
456    {
457      $is_bool = true;
458      $value[0] = 'yes|no|true|false';
459    }
460   
461    if (preg_match('#'.$name.'=('.$value[0].')#', $param, $matches))
462    {
463      $params[$name] = $matches[1];
464      if ($is_bool)
465      {
466        $params[$name] = filter_var($params[$name], FILTER_VALIDATE_BOOLEAN);
467      }
468    }
469    else
470    {
471      $params[$name] = $value[1];
472    }
473  }
474
475  return $params;
476}
477
478/**
479 * Translates shorthand sizes to internal names
480 */
481function extdesc_get_deriv_type($size)
482{
483  $size = strtoupper($size);
484
485  $size_map = array(
486    'SQ' => IMG_SQUARE,
487    'TH' => IMG_THUMB,
488    'XXS' => IMG_XXSMALL,
489    'XS' => IMG_XSMALL,
490    'S' => IMG_SMALL,
491    'M' => IMG_MEDIUM,
492    'L' => IMG_LARGE,
493    'XL' => IMG_XLARGE,
494    'XXL' => IMG_XXLARGE,
495    );
496
497  if (!array_key_exists($size, $size_map))
498  {
499    $size = 'M';
500  }
501
502  return $size_map[$size];
503}
Note: See TracBrowser for help on using the repository browser.