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

Last change on this file since 18464 was 18464, checked in by mistic100, 12 years ago

feature:2614 restore global_rank on recent_cats

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