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

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

reorganize code, clean EOL, remove [img] (deprecated since 08/2012)

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  $query = '
12SELECT
13  cat.id,
14  cat.name,
15  cat.comment,
16  cat.representative_picture_id,
17  cat.permalink,
18  uc.nb_images,
19  uc.count_images,
20  uc.count_categories,
21  img.path
22FROM ' . CATEGORIES_TABLE . ' AS cat
23  INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' as uc
24    ON cat.id = uc.cat_id AND uc.user_id = '.$user['id'].'
25  INNER JOIN ' . IMAGES_TABLE . ' AS img
26    ON img.id = uc.user_representative_picture_id
27WHERE cat.id = ' . $elem_id . ';';
28  $result = pwg_query($query);
29
30  if($result and $category = pwg_db_fetch_assoc($result))
31  {
32    $template->assign(
33      array(
34        'ID'    => $category['id'],
35        'TN_SRC'   => DerivativeImage::thumb_url(array(
36                                  'id' => $category['representative_picture_id'],
37                                  'path' => $category['path'],
38                                )),
39        'TN_ALT'   => strip_tags($category['name']),
40        'URL'   => make_index_url(array('category' => $category)),
41        'CAPTION_NB_IMAGES' => get_display_images_count
42                                (
43                                  $category['nb_images'],
44                                  $category['count_images'],
45                                  $category['count_categories'],
46                                  true,
47                                  '<br />'
48                                ),
49        'DESCRIPTION' =>
50          trigger_event('render_category_literal_description',
51            trigger_event('render_category_description',
52              @$category['comment'],
53              'subcatify_category_description')),
54        'NAME'  => trigger_event(
55                     'render_category_name',
56                     $category['name'],
57                     'subcatify_category_name'
58                   ),
59      )
60    );
61
62    $template->set_filename('extended_description_content', realpath(EXTENDED_DESC_PATH . 'template/cat.tpl'));
63    return $template->parse('extended_description_content', true);
64  }
65  return '';
66}
67
68/**
69 * Return html code for a photo
70 *
71 * @int    id:    picture id
72 * @int    album: album to display picture in    (default: null)
73 * @string size:  picture size                   (default: M)
74 * @string html:  return complete html structure (default: yes)
75 * @string link:  add a link to the picture      (default: yes)
76 */
77function get_photo_sized($param)
78{
79  global $template;
80
81  $default_params = array(
82    'id' =>    array('\d+', null),
83    'album' => array('\d+', null),
84    'size' =>  array('SQ|TH|XXS|XS|S|M|L|XL|XXL', 'M'),
85    'html' =>  array('yes|no', 'yes'),
86    'link' =>  array('yes|no', 'yes'),
87    );
88
89  $params = extdesc_parse_parameters($param, $default_params);
90
91  // check picture id
92  if (empty($params['id'])) return 'missing picture id';
93
94  // parameters
95  $params['link'] = $params['link']=='no' ? false : true;
96  $params['html'] = $params['html']=='no' ? false : true;
97  $deriv_type = extdesc_get_deriv_type($params['size']);
98
99  // get picture
100  $query = 'SELECT * FROM ' . IMAGES_TABLE . ' WHERE id = '.$params['id'].';';
101  $result = pwg_query($query);
102
103  if (pwg_db_num_rows($result))
104  {
105    $picture = pwg_db_fetch_assoc($result);
106
107    // url
108    if ($params['link'])
109    {
110      if (!empty($params['album']))
111      {
112        $query = '
113SELECT id, name, permalink
114  FROM '.CATEGORIES_TABLE.'
115  WHERE id = '.$params['album'].'
116;';
117        $category = pwg_db_fetch_assoc(pwg_query($query));
118
119        $url = make_picture_url(array(
120          'image_id' => $picture['id'],
121          'category' => array(
122            'id' => $category['id'],
123            'name' => $category['name'],
124            'permalink' => $category['permalink'],
125            )));
126      }
127      else
128      {
129        $url = make_picture_url(array('image_id' => $picture['id']));
130      }
131    }
132
133    // image
134    $src_image = new SrcImage($picture);
135    $derivatives = DerivativeImage::get_all($src_image);
136    $selected_derivative = $derivatives[$deriv_type];
137
138    $template->assign(array(
139      'ed_image' => array(
140        'selected_derivative' => $selected_derivative,
141        'ALT_IMG' => $picture['file'],
142      )));
143
144    // output
145    if ($params['html'])
146    {
147      $template->set_filename('extended_description_content', realpath(EXTENDED_DESC_PATH . 'template/picture_content.tpl'));
148      $content = $template->parse('extended_description_content', true);
149
150      if ($params['link']) return '<a href="'.$url.'">'.$content.'</a>';
151      else                 return $content;
152    }
153    else
154    {
155      return $selected_derivative->get_url();
156    }
157  }
158
159  return 'invalid picture id';
160}
161
162/**
163 * Return html code for a random photo
164 *
165 * @int    album: select picture from this album (default: all)
166 * @string size:  picture size                   (default: M)
167 * @string html:  return complete html structure (default: yes)
168 * @string link:  add a link to the picture      (default: no)
169 */
170function extdesc_get_random_photo($param)
171{
172  $default_params = array(
173    'album' => array('\d+', null),
174    'cat' =>   array('\d+', null), // historical
175    'size' =>  array('SQ|TH|XXS|XS|S|M|L|XL|XXL', 'M'),
176    'html' =>  array('yes|no', 'yes'),
177    'link' =>  array('yes|no', 'no'),
178    );
179
180  $params = extdesc_parse_parameters($param, $default_params);
181
182  // check album id
183  if ( empty($params['album']) and !empty($params['cat']) )
184  {
185    $params['album'] = $params['cat'];
186  }
187
188  // get picture id
189  $query = '
190SELECT id, category_id
191  FROM '.IMAGES_TABLE.'
192    JOIN '.IMAGE_CATEGORY_TABLE.' ON image_id = id';
193  if (empty($params['album']))
194  {
195    $query = '
196  WHERE 1=1 '
197      .get_sql_condition_FandF(array(
198        'forbidden_categories' => 'category_id',
199        'visible_categories' => 'category_id',
200        'visible_images' => 'id'
201        ),
202      'AND'
203      );
204  }
205  else
206  {
207    $query.= '
208  WHERE category_id = '.$params['album'];
209  }
210
211  $query.= '
212  ORDER BY '.DB_RANDOM_FUNCTION.'()
213  LIMIT 1
214;';
215  $result = pwg_query($query);
216
217  if (pwg_db_num_rows($result))
218  {
219    list($params['id'], $params['album']) = pwg_db_fetch_row($result);
220    return get_photo_sized($params);
221  }
222
223  return '';
224}
225
226/**
227 * Return html code for a nivo slider (album or list is mandatory)
228 *
229 * @int    album:     select pictures from this album
230 * @int    nb_images: display only x pictures           (default: 10)
231 * @string random:    random sort order                 (default: no)
232 *
233 * @string list:      pictures id separated by a comma
234 *
235 * @string size:      picture size                      (default: M)
236 * @int    speed:     slideshow duration                (default: 3)
237 * @string title:     display picture name              (default: no)
238 * @string effect:    transition effect                 (default: fade)
239 * @string arrows:    display navigation arrows         (default: yes)
240 * @string control:   display navigation bar            (default: yes)
241 * @string elastic:   adapt slider size to each picture (default: yes)
242 * @int thumbs_size:  size of thumbnails if control=thumb (default: 80)
243 */
244function get_slider($param)
245{
246  global $template, $conf;
247
248  $default_params = array(
249    'album' =>     array('\d+', null),
250    'nb_images' => array('\d+', 10),
251    'random' =>    array('yes|no', 'no'),
252    'list' =>      array('[\d,]+', null),
253    'size' =>      array('SQ|TH|XXS|XS|S|M|L|XL|XXL', 'M'),
254    'speed' =>     array('\d+', 3),
255    'title' =>     array('yes|no', 'no'),
256    'effect' =>    array('[a-zA-Z]+', 'fade'),
257    'arrows' =>    array('yes|no', 'yes'),
258    'control' =>   array('yes|no|thumb', 'yes'),
259    'elastic' =>   array('yes|no', 'yes'),
260    'thumbs_size' => array('\d+', 80),
261    );
262
263  $params = extdesc_parse_parameters($param, $default_params);
264
265  // check size
266  $deriv_type = extdesc_get_deriv_type($params['size']);
267  $enabled = ImageStdParams::get_defined_type_map();
268  if (empty($enabled[ $deriv_type ]))
269  {
270    return '(nivoSlider) size disabled';
271  }
272
273  // parameters
274  if ($params['control'] === 'thumb')
275  {
276    $params['control'] = 'yes';
277    $params['control_thumbs'] = true;
278  }
279  else
280  {
281    $params['control_thumbs'] = false;
282  }
283  $params['arrows'] = $params['arrows']==='yes';
284  $params['control'] = $params['control']==='yes';
285  $params['elastic'] = $params['elastic']==='yes';
286  $params['title'] = $params['title']==='yes';
287  $params['random'] = $params['random']==='yes';
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 * Parse tags parameters
403 */
404function extdesc_parse_parameters($param, $default_params)
405{
406  if (is_array($param))
407  {
408    return $param;
409  }
410
411  $params = array();
412
413  foreach ($default_params as $name => $value)
414  {
415    if (preg_match('#'.$name.'=('.$value[0].')#', $param, $matches))
416    {
417      $params[$name] = $matches[1];
418    }
419    else
420    {
421      $params[$name] = $value[1];
422    }
423  }
424
425  return $params;
426}
427
428/**
429 * Translates shorthand sizes to internal names
430 */
431function extdesc_get_deriv_type($size)
432{
433  $size = strtoupper($size);
434
435  $size_map = array(
436    'SQ' => IMG_SQUARE,
437    'TH' => IMG_THUMB,
438    'XXS' => IMG_XXSMALL,
439    'XS' => IMG_XSMALL,
440    'S' => IMG_SMALL,
441    'M' => IMG_MEDIUM,
442    'L' => IMG_LARGE,
443    'XL' => IMG_XLARGE,
444    'XXL' => IMG_XXLARGE,
445    );
446
447  if (!array_key_exists($size, $size_map))
448  {
449    $size = 'M';
450  }
451
452  return $size_map[$size];
453}
Note: See TracBrowser for help on using the repository browser.