source: trunk/include/category_cats.inc.php @ 13800

Last change on this file since 13800 was 12930, checked in by rvelices, 12 years ago

feature 2548 multisize

  • comments thumbnails + no more hard coded thumbnail sizes in css
  • removed admin thumbnail page + language cleanup
  • Property svn:eol-style set to LF
File size: 10.8 KB
RevLine 
[1597]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[12922]5// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
[2297]6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
[1597]23
24/**
25 * This file is included by the main page to show thumbnails for a category
26 * that have only subcategories or to show recent categories
27 *
28 */
29
[8802]30// $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE
31$query = '
32SELECT
33    c.*,
34    user_representative_picture_id,
35    nb_images,
36    date_last,
37    max_date_last,
38    count_images,
39    count_categories
40  FROM '.CATEGORIES_TABLE.' c
41    INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ucc ON id = cat_id AND user_id = '.$user['id'];
42
43if ('recent_cats' == $page['section'])
[1597]44{
[8802]45  $query.= '
46  WHERE date_last >= '.pwg_db_get_recent_period_expression($user['recent_period']);
[1597]47}
48else
49{
[8802]50  $query.= '
51  WHERE id_uppercat '.(!isset($page['category']) ? 'is NULL' : '= '.$page['category']['id']);
[1597]52}
53
[9116]54$query.= '
55    '.get_sql_condition_FandF(
[8802]56  array(
57    'visible_categories' => 'id',
58    ),
59  'AND'
60  );
61
62if ('recent_cats' != $page['section'])
63{
64  $query.= '
65  ORDER BY rank';
66}
67
68$query.= '
69;';
70
[1597]71$result = pwg_query($query);
72$categories = array();
[1970]73$category_ids = array();
[1597]74$image_ids = array();
[8802]75$user_representative_updates_for = array();
[1597]76
[4325]77while ($row = pwg_db_fetch_assoc($result))
[1597]78{
[1643]79  $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last'];
[1624]80
[8802]81  if (!empty($row['user_representative_picture_id']))
82  {
83    $image_id = $row['user_representative_picture_id'];
84  }
85  else if (!empty($row['representative_picture_id']))
[1597]86  { // if a representative picture is set, it has priority
87    $image_id = $row['representative_picture_id'];
88  }
89  else if ($conf['allow_random_representative'])
[8802]90  {
91    // searching a random representant among elements in sub-categories
92    $image_id = get_random_image_in_category($row);
[1597]93  }
94  else
95  { // searching a random representant among representant of sub-categories
[2424]96    if ($row['count_categories']>0 and $row['count_images']>0)
[1597]97    {
[2424]98      $query = '
99  SELECT representative_picture_id
100    FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
101    ON id = cat_id and user_id = '.$user['id'].'
102    WHERE uppercats LIKE \''.$row['uppercats'].',%\'
103      AND representative_picture_id IS NOT NULL'
104    .get_sql_condition_FandF
105    (
106      array
107        (
108          'visible_categories' => 'id',
109        ),
110      "\n  AND"
111    ).'
[4367]112    ORDER BY '.DB_RANDOM_FUNCTION.'()
[4334]113    LIMIT 1
[2424]114  ;';
115      $subresult = pwg_query($query);
[4325]116      if (pwg_db_num_rows($subresult) > 0)
[2424]117      {
[4325]118        list($image_id) = pwg_db_fetch_row($subresult);
[2424]119      }
[1597]120    }
121  }
122
123  if (isset($image_id))
124  {
[11739]125    if ($conf['representative_cache_on_subcats'] and $row['user_representative_picture_id'] != $image_id)
[8802]126    {
127      $user_representative_updates_for[ $user['id'].'#'.$row['id'] ] = $image_id;
128    }
129   
[1597]130    $row['representative_picture_id'] = $image_id;
131    array_push($image_ids, $image_id);
132    array_push($categories, $row);
[1970]133    array_push($category_ids, $row['id']);
[1597]134  }
135  unset($image_id);
136}
137
[1970]138if ($conf['display_fromto'])
139{
140  $dates_of_category = array();
141  if (count($category_ids) > 0)
142  {
143    $query = '
144SELECT
145    category_id,
146    MIN(date_creation) AS date_creation_min,
147    MAX(date_creation) AS date_creation_max
148  FROM '.IMAGE_CATEGORY_TABLE.'
149    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
[2091]150  WHERE category_id IN ('.implode(',', $category_ids).')
[1971]151'.get_sql_condition_FandF
152  (
153    array
154      (
155        'visible_categories' => 'category_id',
[2091]156        'visible_images' => 'id'
[1971]157      ),
158    'AND'
159  ).'
[1970]160  GROUP BY category_id
161;';
162    $result = pwg_query($query);
[4325]163    while ($row = pwg_db_fetch_assoc($result))
[1970]164    {
165      $dates_of_category[ $row['category_id'] ] = array(
166        'from' => $row['date_creation_min'],
167        'to'   => $row['date_creation_max'],
168        );
169    }
170  }
171}
172
[1719]173if ($page['section']=='recent_cats')
174{
175  usort($categories, 'global_rank_compare');
176}
[1597]177if (count($categories) > 0)
178{
[12546]179  $infos_of_image = array();
[8802]180  $new_image_ids = array();
[1597]181
182  $query = '
[12546]183SELECT *
[1597]184  FROM '.IMAGES_TABLE.'
185  WHERE id IN ('.implode(',', $image_ids).')
186;';
187  $result = pwg_query($query);
[4325]188  while ($row = pwg_db_fetch_assoc($result))
[1597]189  {
[8802]190    if ($row['level'] <= $user['level'])
191    {
[12546]192      $infos_of_image[$row['id']] = $row;
[8802]193    }
194    else
195    {
196      // problem: we must not display the thumbnail of a photo which has a
197      // higher privacy level than user privacy level
198      //
199      // * what is the represented category?
200      // * find a random photo matching user permissions
201      // * register it at user_representative_picture_id
202      // * set it as the representative_picture_id for the category
203
204      foreach ($categories as &$category)
205      {
206        if ($row['id'] == $category['representative_picture_id'])
207        {
[9365]208          // searching a random representant among elements in sub-categories
209          $image_id = get_random_image_in_category($category);
210
211          if (isset($image_id) and !in_array($image_id, $image_ids))
[8802]212          {
[9365]213            array_push($new_image_ids, $image_id);
[8802]214          }
[11739]215
216          if ($conf['representative_cache_on_level'])
217          {
218            $user_representative_updates_for[ $user['id'].'#'.$category['id'] ] = $image_id;
219          }
[9365]220         
221          $category['representative_picture_id'] = $image_id;
[8802]222        }
223      }
224      unset($category);
225    }
[1597]226  }
[8802]227
228  if (count($new_image_ids) > 0)
229  {
230    $query = '
[12546]231SELECT *
[8802]232  FROM '.IMAGES_TABLE.'
233  WHERE id IN ('.implode(',', $new_image_ids).')
234;';
235    $result = pwg_query($query);
236    while ($row = pwg_db_fetch_assoc($result))
237    {
[12546]238      $infos_of_image[$row['id']] = $row;
[8802]239    }
240  }
[12920]241 
242  foreach ($infos_of_image as &$info)
243  {
244    $info['src_image'] = new SrcImage($info);
245  }
246  unset($info);
[1597]247}
248
[8802]249if (count($user_representative_updates_for))
250{
251  $updates = array();
252 
253  foreach ($user_representative_updates_for as $user_cat => $image_id)
254  {
255    list($user_id, $cat_id) = explode('#', $user_cat);
256   
257    array_push(
258      $updates,
259      array(
260        'user_id' => $user_id,
261        'cat_id' => $cat_id,
262        'user_representative_picture_id' => $image_id,
263        )
264      );
265  }
266
267  mass_updates(
268    USER_CACHE_CATEGORIES_TABLE,
269    array(
270      'primary' => array('user_id', 'cat_id'),
271      'update'  => array('user_representative_picture_id')
272      ),
273    $updates
274    );
275}
276
[1597]277if (count($categories) > 0)
278{
[1677]279  // Update filtered data
[1722]280  if (function_exists('update_cats_with_filtered_data'))
281  {
282    update_cats_with_filtered_data($categories);
283  }
[2079]284
[2274]285  $template->set_filename('index_category_thumbnails', 'mainpage_categories.tpl');
286
[1880]287  trigger_action('loc_begin_index_category_thumbnails', $categories);
[2274]288
[3124]289  $tpl_thumbnails_var = array();
290
[2274]291  foreach ($categories as $category)
[1597]292  {
[9365]293    if (0 == $category['count_images'])
294    {
295      continue;
296    }
297   
[2433]298    $category['name'] = trigger_event(
299        'render_category_name',
300        $category['name'],
301        'subcatify_category_name'
302        );
303
[2274]304    if ($page['section']=='recent_cats')
[1597]305    {
[2274]306      $name = get_cat_display_name_cache($category['uppercats'], null, false);
307    }
308    else
309    {
310      $name = $category['name'];
311    }
[1597]312
[12546]313    $representative_infos = $infos_of_image[ $category['representative_picture_id'] ];
314
[2274]315    $tpl_var =
[1597]316        array(
[2231]317          'ID'    => $category['id'],
[12920]318          'representative'   => $representative_infos,
[2515]319          'TN_ALT'   => strip_tags($category['name']),
[1597]320
321          'URL'   => make_index_url(
322            array(
[1861]323              'category' => $category
[1597]324              )
325            ),
[1624]326          'CAPTION_NB_IMAGES' => get_display_images_count
327                                  (
328                                    $category['nb_images'],
329                                    $category['count_images'],
[1851]330                                    $category['count_categories'],
331                                    true,
[3185]332                                    '<br>'
[1624]333                                  ),
[2079]334          'DESCRIPTION' =>
[2091]335            trigger_event('render_category_literal_description',
336              trigger_event('render_category_description',
[2175]337                @$category['comment'],
338                'subcatify_category_description')),
[1597]339          'NAME'  => $name,
[2274]340        );
[11285]341    if ($conf['index_new_icon'])
342    {
[11591]343      $tpl_var['icon_ts'] = get_icon($category['max_date_last'], $category['is_child_date_last']);
[11285]344    }
[1970]345
[2274]346    if ($conf['display_fromto'])
347    {
348      if (isset($dates_of_category[ $category['id'] ]))
[1970]349      {
[2274]350        $from = $dates_of_category[ $category['id'] ]['from'];
351        $to   = $dates_of_category[ $category['id'] ]['to'];
352
353        if (!empty($from))
[1970]354        {
[2274]355          $info = '';
[2091]356
[2274]357          if ($from == $to)
[1970]358          {
[2274]359            $info = format_date($from);
[1970]360          }
[2274]361          else
362          {
363            $info = sprintf(
364              l10n('from %s to %s'),
365              format_date($from),
366              format_date($to)
367              );
368          }
369          $tpl_var['INFO_DATES'] = $info;
[1970]370        }
[2274]371      }
372    }//fromto
[2234]373
[3124]374    $tpl_thumbnails_var[] = $tpl_var;
[1597]375  }
376
[12930]377  $derivative_params = trigger_event('get_index_album_derivative_params', ImageStdParams::get_by_type(IMG_THUMB) );
[3124]378  $tpl_thumbnails_var = trigger_event('loc_end_index_category_thumbnails', $tpl_thumbnails_var, $categories);
[12920]379  $template->assign( array(
380    'category_thumbnails' => $tpl_thumbnails_var,
381    'derivative_params' => $derivative_params,
382    ) );
[3124]383
[2274]384  $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails');
[1597]385}
[3124]386pwg_debug('end include/category_cats.inc.php');
387?>
Note: See TracBrowser for help on using the repository browser.