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

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

feature 2614: pagination on albums

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