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

Last change on this file since 21850 was 21802, checked in by rvelices, 11 years ago

feature 2557 recent photos/albums should never be empty

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