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

Last change on this file since 20230 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
RevLine 
[1597]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[19703]5// | Copyright(C) 2008-2013 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[1597]23
24/**
[18165]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
[1597]27 *
28 */
29
[18165]30
[8802]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
[18924]42    INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ucc
43    ON id = cat_id
[18462]44    AND user_id = '.$user['id'];
[18924]45
[18462]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        );
[18924]62
[8802]63if ('recent_cats' != $page['section'])
64{
65  $query.= '
66  ORDER BY rank';
67}
68
69$query.= '
70;';
71
[18892]72$result = pwg_query($query);
[1597]73$categories = array();
[1970]74$category_ids = array();
[1597]75$image_ids = array();
[8802]76$user_representative_updates_for = array();
[1597]77
[18892]78while ($row = pwg_db_fetch_assoc($result))
[1597]79{
[1643]80  $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last'];
[1624]81
[8802]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']))
[1597]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'])
[8802]91  {
92    // searching a random representant among elements in sub-categories
93    $image_id = get_random_image_in_category($row);
[1597]94  }
95  else
96  { // searching a random representant among representant of sub-categories
[2424]97    if ($row['count_categories']>0 and $row['count_images']>0)
[1597]98    {
[2424]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    ).'
[4367]113    ORDER BY '.DB_RANDOM_FUNCTION.'()
[4334]114    LIMIT 1
[2424]115  ;';
116      $subresult = pwg_query($query);
[4325]117      if (pwg_db_num_rows($subresult) > 0)
[2424]118      {
[4325]119        list($image_id) = pwg_db_fetch_row($subresult);
[2424]120      }
[1597]121    }
122  }
123
124  if (isset($image_id))
125  {
[11739]126    if ($conf['representative_cache_on_subcats'] and $row['user_representative_picture_id'] != $image_id)
[8802]127    {
128      $user_representative_updates_for[ $user['id'].'#'.$row['id'] ] = $image_id;
129    }
[18924]130
[1597]131    $row['representative_picture_id'] = $image_id;
132    array_push($image_ids, $image_id);
133    array_push($categories, $row);
[1970]134    array_push($category_ids, $row['id']);
[1597]135  }
136  unset($image_id);
137}
138
[1970]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
[2091]151  WHERE category_id IN ('.implode(',', $category_ids).')
[1971]152'.get_sql_condition_FandF
153  (
154    array
155      (
156        'visible_categories' => 'category_id',
[2091]157        'visible_images' => 'id'
[1971]158      ),
159    'AND'
160  ).'
[1970]161  GROUP BY category_id
162;';
163    $result = pwg_query($query);
[4325]164    while ($row = pwg_db_fetch_assoc($result))
[1970]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
[18892]174if ($page['section']=='recent_cats')
175{
176  usort($categories, 'global_rank_compare');
177}
178
[1597]179if (count($categories) > 0)
180{
[12546]181  $infos_of_image = array();
[8802]182  $new_image_ids = array();
[1597]183
184  $query = '
[12546]185SELECT *
[1597]186  FROM '.IMAGES_TABLE.'
187  WHERE id IN ('.implode(',', $image_ids).')
188;';
189  $result = pwg_query($query);
[4325]190  while ($row = pwg_db_fetch_assoc($result))
[1597]191  {
[8802]192    if ($row['level'] <= $user['level'])
193    {
[12546]194      $infos_of_image[$row['id']] = $row;
[8802]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        {
[9365]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))
[8802]214          {
[9365]215            array_push($new_image_ids, $image_id);
[8802]216          }
[11739]217
218          if ($conf['representative_cache_on_level'])
219          {
220            $user_representative_updates_for[ $user['id'].'#'.$category['id'] ] = $image_id;
221          }
[18924]222
[9365]223          $category['representative_picture_id'] = $image_id;
[8802]224        }
225      }
226      unset($category);
227    }
[1597]228  }
[8802]229
230  if (count($new_image_ids) > 0)
231  {
232    $query = '
[12546]233SELECT *
[8802]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    {
[12546]240      $infos_of_image[$row['id']] = $row;
[8802]241    }
242  }
[18924]243
[12920]244  foreach ($infos_of_image as &$info)
245  {
246    $info['src_image'] = new SrcImage($info);
247  }
248  unset($info);
[1597]249}
250
[8802]251if (count($user_representative_updates_for))
252{
253  $updates = array();
[18924]254
[8802]255  foreach ($user_representative_updates_for as $user_cat => $image_id)
256  {
257    list($user_id, $cat_id) = explode('#', $user_cat);
[18924]258
[8802]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
[1597]279if (count($categories) > 0)
280{
[1677]281  // Update filtered data
[1722]282  if (function_exists('update_cats_with_filtered_data'))
283  {
284    update_cats_with_filtered_data($categories);
285  }
[2079]286
[2274]287  $template->set_filename('index_category_thumbnails', 'mainpage_categories.tpl');
288
[1880]289  trigger_action('loc_begin_index_category_thumbnails', $categories);
[2274]290
[3124]291  $tpl_thumbnails_var = array();
292
[2274]293  foreach ($categories as $category)
[1597]294  {
[9365]295    if (0 == $category['count_images'])
296    {
297      continue;
298    }
[18924]299
[2433]300    $category['name'] = trigger_event(
301        'render_category_name',
302        $category['name'],
303        'subcatify_category_name'
304        );
305
[2274]306    if ($page['section']=='recent_cats')
[1597]307    {
[2274]308      $name = get_cat_display_name_cache($category['uppercats'], null, false);
309    }
310    else
311    {
312      $name = $category['name'];
313    }
[1597]314
[12546]315    $representative_infos = $infos_of_image[ $category['representative_picture_id'] ];
316
[18924]317    $tpl_var = array_merge( $category, array(
318          'ID'    => $category['id'] /*obsolete*/,
[12920]319          'representative'   => $representative_infos,
[2515]320          'TN_ALT'   => strip_tags($category['name']),
[1597]321
322          'URL'   => make_index_url(
323            array(
[1861]324              'category' => $category
[1597]325              )
326            ),
[1624]327          'CAPTION_NB_IMAGES' => get_display_images_count
328                                  (
329                                    $category['nb_images'],
330                                    $category['count_images'],
[1851]331                                    $category['count_categories'],
332                                    true,
[3185]333                                    '<br>'
[1624]334                                  ),
[2079]335          'DESCRIPTION' =>
[2091]336            trigger_event('render_category_literal_description',
337              trigger_event('render_category_description',
[2175]338                @$category['comment'],
339                'subcatify_category_description')),
[1597]340          'NAME'  => $name,
[18924]341        ) );
[11285]342    if ($conf['index_new_icon'])
343    {
[11591]344      $tpl_var['icon_ts'] = get_icon($category['max_date_last'], $category['is_child_date_last']);
[11285]345    }
[1970]346
[2274]347    if ($conf['display_fromto'])
348    {
349      if (isset($dates_of_category[ $category['id'] ]))
[1970]350      {
[2274]351        $from = $dates_of_category[ $category['id'] ]['from'];
352        $to   = $dates_of_category[ $category['id'] ]['to'];
353
354        if (!empty($from))
[1970]355        {
[2274]356          $info = '';
[2091]357
[19420]358          if (date('Y-m-d', strtotime($from)) == date('Y-m-d', strtotime($to)))
[1970]359          {
[2274]360            $info = format_date($from);
[1970]361          }
[2274]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;
[1970]371        }
[2274]372      }
373    }//fromto
[2234]374
[3124]375    $tpl_thumbnails_var[] = $tpl_var;
[1597]376  }
[18924]377
[18892]378  // pagination
379  $page['total_categories'] = count($tpl_thumbnails_var);
[1597]380
[19002]381  $tpl_thumbnails_var_selection = array_slice(
[18924]382    $tpl_thumbnails_var,
[18892]383    $page['startcat'],
384    $conf['nb_categories_page']
385    );
386
[12930]387  $derivative_params = trigger_event('get_index_album_derivative_params', ImageStdParams::get_by_type(IMG_THUMB) );
[19002]388  $tpl_thumbnails_var_selection = trigger_event('loc_end_index_category_thumbnails', $tpl_thumbnails_var_selection);
[12920]389  $template->assign( array(
[20177]390    'maxRequests' =>$conf['max_requests'],
[19002]391    'category_thumbnails' => $tpl_thumbnails_var_selection,
[12920]392    'derivative_params' => $derivative_params,
393    ) );
[3124]394
[2274]395  $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails');
[18924]396
[18462]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'] );
[1597]411}
[18462]412
[3124]413pwg_debug('end include/category_cats.inc.php');
414?>
Note: See TracBrowser for help on using the repository browser.