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

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

feature:2614 pagination on albums
return to old fashioned way (one query in category_cats), restoring recent_cats feature and "menubar optimization", rename "starta" into "startcat"

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