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

Last change on this file since 20384 was 20177, checked in by flop25, 11 years ago

bug:2820 Var renamed

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