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

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

feature 2541 multisize

  • core implementation + usage on most public/admin pages
  • still to do: sync process, upload, gui/persistence for size parameters, migration script, center of interest ...
  • Property svn:eol-style set to LF
File size: 11.6 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.= '
55    '.get_sql_condition_FandF(
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
71$result = pwg_query($query);
72$categories = array();
73$category_ids = array();
74$image_ids = array();
75$user_representative_updates_for = array();
76
77while ($row = pwg_db_fetch_assoc($result))
78{
79  $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last'];
80
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']))
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'])
90  {
91    // searching a random representant among elements in sub-categories
92    $image_id = get_random_image_in_category($row);
93  }
94  else
95  { // searching a random representant among representant of sub-categories
96    if ($row['count_categories']>0 and $row['count_images']>0)
97    {
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    ).'
112    ORDER BY '.DB_RANDOM_FUNCTION.'()
113    LIMIT 1
114  ;';
115      $subresult = pwg_query($query);
116      if (pwg_db_num_rows($subresult) > 0)
117      {
118        list($image_id) = pwg_db_fetch_row($subresult);
119      }
120    }
121  }
122
123  if (isset($image_id))
124  {
125    if ($conf['representative_cache_on_subcats'] and $row['user_representative_picture_id'] != $image_id)
126    {
127      $user_representative_updates_for[ $user['id'].'#'.$row['id'] ] = $image_id;
128    }
129   
130    $row['representative_picture_id'] = $image_id;
131    array_push($image_ids, $image_id);
132    array_push($categories, $row);
133    array_push($category_ids, $row['id']);
134  }
135  unset($image_id);
136}
137
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
150  WHERE category_id IN ('.implode(',', $category_ids).')
151'.get_sql_condition_FandF
152  (
153    array
154      (
155        'visible_categories' => 'category_id',
156        'visible_images' => 'id'
157      ),
158    'AND'
159  ).'
160  GROUP BY category_id
161;';
162    $result = pwg_query($query);
163    while ($row = pwg_db_fetch_assoc($result))
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
173if ($page['section']=='recent_cats')
174{
175  usort($categories, 'global_rank_compare');
176}
177if (count($categories) > 0)
178{
179  $infos_of_image = array();
180  $new_image_ids = array();
181
182  $query = '
183SELECT *
184  FROM '.IMAGES_TABLE.'
185  WHERE id IN ('.implode(',', $image_ids).')
186;';
187  $result = pwg_query($query);
188  while ($row = pwg_db_fetch_assoc($result))
189  {
190    if ($row['level'] <= $user['level'])
191    {
192      $row['tn_src'] = DerivativeImage::thumb_url($row);
193      $infos_of_image[$row['id']] = $row;
194    }
195    else
196    {
197      // problem: we must not display the thumbnail of a photo which has a
198      // higher privacy level than user privacy level
199      //
200      // * what is the represented category?
201      // * find a random photo matching user permissions
202      // * register it at user_representative_picture_id
203      // * set it as the representative_picture_id for the category
204
205      foreach ($categories as &$category)
206      {
207        if ($row['id'] == $category['representative_picture_id'])
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) and !in_array($image_id, $image_ids))
213          {
214            array_push($new_image_ids, $image_id);
215          }
216
217          if ($conf['representative_cache_on_level'])
218          {
219            $user_representative_updates_for[ $user['id'].'#'.$category['id'] ] = $image_id;
220          }
221         
222          $category['representative_picture_id'] = $image_id;
223        }
224      }
225      unset($category);
226    }
227  }
228
229  if (count($new_image_ids) > 0)
230  {
231    $query = '
232SELECT *
233  FROM '.IMAGES_TABLE.'
234  WHERE id IN ('.implode(',', $new_image_ids).')
235;';
236    $result = pwg_query($query);
237    while ($row = pwg_db_fetch_assoc($result))
238    {
239      $row['tn_src'] =  DerivativeImage::thumb_url($row);
240      $infos_of_image[$row['id']] = $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    if (0 == $category['count_images'])
290    {
291      continue;
292    }
293   
294    $category['name'] = trigger_event(
295        'render_category_name',
296        $category['name'],
297        'subcatify_category_name'
298        );
299
300    if ($page['section']=='recent_cats')
301    {
302      $name = get_cat_display_name_cache($category['uppercats'], null, false);
303    }
304    else
305    {
306      $name = $category['name'];
307    }
308
309    $representative_infos = $infos_of_image[ $category['representative_picture_id'] ];
310
311    $tpl_var =
312        array(
313          'ID'    => $category['id'],
314          'TN_SRC'   => $representative_infos['tn_src'],
315          'TN_ALT'   => strip_tags($category['name']),
316
317          'URL'   => make_index_url(
318            array(
319              'category' => $category
320              )
321            ),
322          'CAPTION_NB_IMAGES' => get_display_images_count
323                                  (
324                                    $category['nb_images'],
325                                    $category['count_images'],
326                                    $category['count_categories'],
327                                    true,
328                                    '<br>'
329                                  ),
330          'DESCRIPTION' =>
331            trigger_event('render_category_literal_description',
332              trigger_event('render_category_description',
333                @$category['comment'],
334                'subcatify_category_description')),
335          'NAME'  => $name,
336         
337          // Extra fields for usage in extra themes
338          'FILE_PATH' => $representative_infos['path'],
339          'FILE_POSTED' => $representative_infos['date_available'],
340          'FILE_CREATED' => $representative_infos['date_creation'],
341          'FILE_DESC' => $representative_infos['comment'],
342          'FILE_AUTHOR' => $representative_infos['author'],
343          'FILE_HIT' => $representative_infos['hit'],
344          'FILE_SIZE' => $representative_infos['filesize'],
345          'FILE_WIDTH' => $representative_infos['width'],
346          'FILE_HEIGHT' => $representative_infos['height'],
347          'FILE_METADATE' => $representative_infos['date_metadata_update'],
348          'FILE_HAS_HD' => $representative_infos['has_high'],
349          'FILE_HD_WIDTH' => $representative_infos['high_width'],
350          'FILE_HD_HEIGHT' => $representative_infos['high_height'],
351          'FILE_HD_FILESIZE' => $representative_infos['high_filesize'],
352          'FILE_RATING_SCORE' => $representative_infos['rating_score'],
353        );
354    if ($conf['index_new_icon'])
355    {
356      $tpl_var['icon_ts'] = get_icon($category['max_date_last'], $category['is_child_date_last']);
357    }
358
359    if ($conf['display_fromto'])
360    {
361      if (isset($dates_of_category[ $category['id'] ]))
362      {
363        $from = $dates_of_category[ $category['id'] ]['from'];
364        $to   = $dates_of_category[ $category['id'] ]['to'];
365
366        if (!empty($from))
367        {
368          $info = '';
369
370          if ($from == $to)
371          {
372            $info = format_date($from);
373          }
374          else
375          {
376            $info = sprintf(
377              l10n('from %s to %s'),
378              format_date($from),
379              format_date($to)
380              );
381          }
382          $tpl_var['INFO_DATES'] = $info;
383        }
384      }
385    }//fromto
386
387    $tpl_thumbnails_var[] = $tpl_var;
388  }
389
390  $tpl_thumbnails_var = trigger_event('loc_end_index_category_thumbnails', $tpl_thumbnails_var, $categories);
391  $template->assign( 'category_thumbnails', $tpl_thumbnails_var);
392
393  $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails');
394}
395pwg_debug('end include/category_cats.inc.php');
396?>
Note: See TracBrowser for help on using the repository browser.