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

Last change on this file since 8802 was 8802, checked in by plg, 13 years ago

bug 937 fixed: makes sure a user won't see the thumbnail of a photo that has a
higher privacy level than user privacy level.

For an acceptable solution at performance level, I have implemented a cache:
for a given user, each album has a representative_picture_id. This cache also
avoids to perform numerous "order by rand()" SQL queries which is the case
when $confallow_random_representative = true;

  • Property svn:eol-style set to LF
File size: 10.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
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// +-----------------------------------------------------------------------+
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
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'])
44{
45  $query.= '
46  WHERE date_last >= '.pwg_db_get_recent_period_expression($user['recent_period']);
47}
48else
49{
50  $query.= '
51  WHERE id_uppercat '.(!isset($page['category']) ? 'is NULL' : '= '.$page['category']['id']);
52}
53
54$query.= get_sql_condition_FandF(
55  array(
56    'visible_categories' => 'id',
57    ),
58  'AND'
59  );
60
61if ('recent_cats' != $page['section'])
62{
63  $query.= '
64  ORDER BY rank';
65}
66
67$query.= '
68;';
69
70$result = pwg_query($query);
71$categories = array();
72$category_ids = array();
73$image_ids = array();
74$user_representative_updates_for = array();
75
76while ($row = pwg_db_fetch_assoc($result))
77{
78  $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last'];
79
80  if (!empty($row['user_representative_picture_id']))
81  {
82    $image_id = $row['user_representative_picture_id'];
83  }
84  else if (!empty($row['representative_picture_id']))
85  { // if a representative picture is set, it has priority
86    $image_id = $row['representative_picture_id'];
87  }
88  else if ($conf['allow_random_representative'])
89  {
90    // searching a random representant among elements in sub-categories
91    $image_id = get_random_image_in_category($row);
92  }
93  else
94  { // searching a random representant among representant of sub-categories
95    if ($row['count_categories']>0 and $row['count_images']>0)
96    {
97      $query = '
98  SELECT representative_picture_id
99    FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
100    ON id = cat_id and user_id = '.$user['id'].'
101    WHERE uppercats LIKE \''.$row['uppercats'].',%\'
102      AND representative_picture_id IS NOT NULL'
103    .get_sql_condition_FandF
104    (
105      array
106        (
107          'visible_categories' => 'id',
108        ),
109      "\n  AND"
110    ).'
111    ORDER BY '.DB_RANDOM_FUNCTION.'()
112    LIMIT 1
113  ;';
114      $subresult = pwg_query($query);
115      if (pwg_db_num_rows($subresult) > 0)
116      {
117        list($image_id) = pwg_db_fetch_row($subresult);
118      }
119    }
120  }
121
122  if (isset($image_id))
123  {
124    if ($row['user_representative_picture_id'] != $image_id)
125    {
126      $user_representative_updates_for[ $user['id'].'#'.$row['id'] ] = $image_id;
127    }
128   
129    $row['representative_picture_id'] = $image_id;
130    array_push($image_ids, $image_id);
131    array_push($categories, $row);
132    array_push($category_ids, $row['id']);
133  }
134  unset($image_id);
135}
136
137if ($conf['display_fromto'])
138{
139  $dates_of_category = array();
140  if (count($category_ids) > 0)
141  {
142    $query = '
143SELECT
144    category_id,
145    MIN(date_creation) AS date_creation_min,
146    MAX(date_creation) AS date_creation_max
147  FROM '.IMAGE_CATEGORY_TABLE.'
148    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
149  WHERE category_id IN ('.implode(',', $category_ids).')
150'.get_sql_condition_FandF
151  (
152    array
153      (
154        'visible_categories' => 'category_id',
155        'visible_images' => 'id'
156      ),
157    'AND'
158  ).'
159  GROUP BY category_id
160;';
161    $result = pwg_query($query);
162    while ($row = pwg_db_fetch_assoc($result))
163    {
164      $dates_of_category[ $row['category_id'] ] = array(
165        'from' => $row['date_creation_min'],
166        'to'   => $row['date_creation_max'],
167        );
168    }
169  }
170}
171
172if ($page['section']=='recent_cats')
173{
174  usort($categories, 'global_rank_compare');
175}
176if (count($categories) > 0)
177{
178  $thumbnail_src_of = array();
179  $new_image_ids = array();
180
181  $query = '
182SELECT id, path, tn_ext, level
183  FROM '.IMAGES_TABLE.'
184  WHERE id IN ('.implode(',', $image_ids).')
185;';
186  $result = pwg_query($query);
187  while ($row = pwg_db_fetch_assoc($result))
188  {
189    if ($row['level'] <= $user['level'])
190    {
191      $thumbnail_src_of[$row['id']] = get_thumbnail_url($row);
192    }
193    else
194    {
195      // problem: we must not display the thumbnail of a photo which has a
196      // higher privacy level than user privacy level
197      //
198      // * what is the represented category?
199      // * find a random photo matching user permissions
200      // * register it at user_representative_picture_id
201      // * set it as the representative_picture_id for the category
202
203      foreach ($categories as &$category)
204      {
205        if ($row['id'] == $category['representative_picture_id'])
206        {
207          if ($category['count_images']>0)
208          {
209            // searching a random representant among elements in sub-categories
210            $image_id = get_random_image_in_category($category);
211
212            if (isset($image_id))
213            {
214              if (!in_array($image_id, $image_ids))
215              {
216                array_push($new_image_ids, $image_id);
217              }
218             
219              $user_representative_updates_for[ $user['id'].'#'.$category['id'] ] = $image_id;
220
221              $category['representative_picture_id'] = $image_id;
222            }
223          }
224        }
225      }
226      unset($category);
227    }
228  }
229
230  if (count($new_image_ids) > 0)
231  {
232    $query = '
233SELECT id, path, tn_ext
234  FROM '.IMAGES_TABLE.'
235  WHERE id IN ('.implode(',', $new_image_ids).')
236;';
237    $result = pwg_query($query);
238    while ($row = pwg_db_fetch_assoc($result))
239    {
240      $thumbnail_src_of[$row['id']] = get_thumbnail_url($row);
241    }
242  }
243}
244
245if (count($user_representative_updates_for))
246{
247  $updates = array();
248 
249  foreach ($user_representative_updates_for as $user_cat => $image_id)
250  {
251    list($user_id, $cat_id) = explode('#', $user_cat);
252   
253    array_push(
254      $updates,
255      array(
256        'user_id' => $user_id,
257        'cat_id' => $cat_id,
258        'user_representative_picture_id' => $image_id,
259        )
260      );
261  }
262
263  mass_updates(
264    USER_CACHE_CATEGORIES_TABLE,
265    array(
266      'primary' => array('user_id', 'cat_id'),
267      'update'  => array('user_representative_picture_id')
268      ),
269    $updates
270    );
271}
272
273if (count($categories) > 0)
274{
275  // Update filtered data
276  if (function_exists('update_cats_with_filtered_data'))
277  {
278    update_cats_with_filtered_data($categories);
279  }
280
281  $template->set_filename('index_category_thumbnails', 'mainpage_categories.tpl');
282
283  trigger_action('loc_begin_index_category_thumbnails', $categories);
284
285  $tpl_thumbnails_var = array();
286
287  foreach ($categories as $category)
288  {
289    $category['name'] = trigger_event(
290        'render_category_name',
291        $category['name'],
292        'subcatify_category_name'
293        );
294
295    if ($page['section']=='recent_cats')
296    {
297      $name = get_cat_display_name_cache($category['uppercats'], null, false);
298    }
299    else
300    {
301      $name = $category['name'];
302    }
303
304    $tpl_var =
305        array(
306          'ID'    => $category['id'],
307          'TN_SRC'   => $thumbnail_src_of[$category['representative_picture_id']],
308          'TN_ALT'   => strip_tags($category['name']),
309          'icon_ts'  => get_icon($category['max_date_last'], $category['is_child_date_last']),
310
311          'URL'   => make_index_url(
312            array(
313              'category' => $category
314              )
315            ),
316          'CAPTION_NB_IMAGES' => get_display_images_count
317                                  (
318                                    $category['nb_images'],
319                                    $category['count_images'],
320                                    $category['count_categories'],
321                                    true,
322                                    '<br>'
323                                  ),
324          'DESCRIPTION' =>
325            trigger_event('render_category_literal_description',
326              trigger_event('render_category_description',
327                @$category['comment'],
328                'subcatify_category_description')),
329          'NAME'  => $name,
330        );
331
332    if ($conf['display_fromto'])
333    {
334      if (isset($dates_of_category[ $category['id'] ]))
335      {
336        $from = $dates_of_category[ $category['id'] ]['from'];
337        $to   = $dates_of_category[ $category['id'] ]['to'];
338
339        if (!empty($from))
340        {
341          $info = '';
342
343          if ($from == $to)
344          {
345            $info = format_date($from);
346          }
347          else
348          {
349            $info = sprintf(
350              l10n('from %s to %s'),
351              format_date($from),
352              format_date($to)
353              );
354          }
355          $tpl_var['INFO_DATES'] = $info;
356        }
357      }
358    }//fromto
359
360    $tpl_thumbnails_var[] = $tpl_var;
361  }
362
363  $tpl_thumbnails_var = trigger_event('loc_end_index_category_thumbnails', $tpl_thumbnails_var, $categories);
364  $template->assign( 'category_thumbnails', $tpl_thumbnails_var);
365
366  $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails');
367}
368pwg_debug('end include/category_cats.inc.php');
369?>
Note: See TracBrowser for help on using the repository browser.