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

Last change on this file since 21802 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
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 '.get_recent_photos_sql('date_last');
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$result = pwg_query($query);
70$categories = array();
71$category_ids = array();
72$image_ids = array();
73$user_representative_updates_for = array();
74
75while ($row = pwg_db_fetch_assoc($result))
76{
77  $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last'];
78
79  if (!empty($row['user_representative_picture_id']))
80  {
81    $image_id = $row['user_representative_picture_id'];
82  }
83  elseif (!empty($row['representative_picture_id']))
84  { // if a representative picture is set, it has priority
85    $image_id = $row['representative_picture_id'];
86  }
87  elseif ($conf['allow_random_representative'])
88  { // searching a random representant among elements in sub-categories
89    $image_id = get_random_image_in_category($row);
90  }
91  elseif ($row['count_categories']>0 and $row['count_images']>0)
92  { // searching a random representant among representant of sub-categories
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)
112    {
113      list($image_id) = pwg_db_fetch_row($subresult);
114    }
115  }
116
117
118  if (isset($image_id))
119  {
120    if ($conf['representative_cache_on_subcats'] and $row['user_representative_picture_id'] != $image_id)
121    {
122      $user_representative_updates_for[ $user['id'].'#'.$row['id'] ] = $image_id;
123    }
124
125    $row['representative_picture_id'] = $image_id;
126    $image_ids[] = $image_id;
127    $categories[] = $row;
128    $category_ids[] = $row['id'];
129  }
130  unset($image_id);
131}
132
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
145  WHERE category_id IN ('.implode(',', $category_ids).')
146'.get_sql_condition_FandF
147  (
148    array
149      (
150        'visible_categories' => 'category_id',
151        'visible_images' => 'id'
152      ),
153    'AND'
154  ).'
155  GROUP BY category_id
156;';
157    $result = pwg_query($query);
158    while ($row = pwg_db_fetch_assoc($result))
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
168if ($page['section']=='recent_cats')
169{
170  usort($categories, 'global_rank_compare');
171}
172
173if (count($categories) > 0)
174{
175  $infos_of_image = array();
176  $new_image_ids = array();
177
178  $query = '
179SELECT *
180  FROM '.IMAGES_TABLE.'
181  WHERE id IN ('.implode(',', $image_ids).')
182;';
183  $result = pwg_query($query);
184  while ($row = pwg_db_fetch_assoc($result))
185  {
186    if ($row['level'] <= $user['level'])
187    {
188      $infos_of_image[$row['id']] = $row;
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        {
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))
208          {
209            array_push($new_image_ids, $image_id);
210          }
211
212          if ($conf['representative_cache_on_level'])
213          {
214            $user_representative_updates_for[ $user['id'].'#'.$category['id'] ] = $image_id;
215          }
216
217          $category['representative_picture_id'] = $image_id;
218        }
219      }
220      unset($category);
221    }
222  }
223
224  if (count($new_image_ids) > 0)
225  {
226    $query = '
227SELECT *
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    {
234      $infos_of_image[$row['id']] = $row;
235    }
236  }
237
238  foreach ($infos_of_image as &$info)
239  {
240    $info['src_image'] = new SrcImage($info);
241  }
242  unset($info);
243}
244
245if (count($user_representative_updates_for))
246{
247  $updates = array();
248
249  foreach ($user_representative_updates_for as $user_cat => $image_id)
250  {
251    list($user_id, $cat_id) = explode('#', $user_cat);
252
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
273if (count($categories) > 0)
274{
275  // Update filtered data
276  if (function_exists('update_cats_with_filtered_data'))
277  {
278    update_cats_with_filtered_data($categories);
279  }
280
281  $template->set_filename('index_category_thumbnails', 'mainpage_categories.tpl');
282
283  trigger_action('loc_begin_index_category_thumbnails', $categories);
284
285  $tpl_thumbnails_var = array();
286
287  foreach ($categories as $category)
288  {
289    if (0 == $category['count_images'])
290    {
291      continue;
292    }
293
294    $category['name'] = trigger_event(
295        'render_category_name',
296        $category['name'],
297        'subcatify_category_name'
298        );
299
300    if ($page['section']=='recent_cats')
301    {
302      $name = get_cat_display_name_cache($category['uppercats'], null, false);
303    }
304    else
305    {
306      $name = $category['name'];
307    }
308
309    $representative_infos = $infos_of_image[ $category['representative_picture_id'] ];
310
311    $tpl_var = array_merge( $category, array(
312          'ID'    => $category['id'] /*obsolete*/,
313          'representative'   => $representative_infos,
314          'TN_ALT'   => strip_tags($category['name']),
315
316          'URL'   => make_index_url(
317            array(
318              'category' => $category
319              )
320            ),
321          'CAPTION_NB_IMAGES' => get_display_images_count
322                                  (
323                                    $category['nb_images'],
324                                    $category['count_images'],
325                                    $category['count_categories'],
326                                    true,
327                                    '<br>'
328                                  ),
329          'DESCRIPTION' =>
330            trigger_event('render_category_literal_description',
331              trigger_event('render_category_description',
332                @$category['comment'],
333                'subcatify_category_description')),
334          'NAME'  => $name,
335        ) );
336    if ($conf['index_new_icon'])
337    {
338      $tpl_var['icon_ts'] = get_icon($category['max_date_last'], $category['is_child_date_last']);
339    }
340
341    if ($conf['display_fromto'])
342    {
343      if (isset($dates_of_category[ $category['id'] ]))
344      {
345        $from = $dates_of_category[ $category['id'] ]['from'];
346        $to   = $dates_of_category[ $category['id'] ]['to'];
347
348        if (!empty($from))
349        {
350          $info = '';
351
352          if (date('Y-m-d', strtotime($from)) == date('Y-m-d', strtotime($to)))
353          {
354            $info = format_date($from);
355          }
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;
365        }
366      }
367    }//fromto
368
369    $tpl_thumbnails_var[] = $tpl_var;
370  }
371
372  // pagination
373  $page['total_categories'] = count($tpl_thumbnails_var);
374
375  $tpl_thumbnails_var_selection = array_slice(
376    $tpl_thumbnails_var,
377    $page['startcat'],
378    $conf['nb_categories_page']
379    );
380
381  $derivative_params = trigger_event('get_index_album_derivative_params', ImageStdParams::get_by_type(IMG_THUMB) );
382  $tpl_thumbnails_var_selection = trigger_event('loc_end_index_category_thumbnails', $tpl_thumbnails_var_selection);
383  $template->assign( array(
384    'maxRequests' =>$conf['max_requests'],
385    'category_thumbnails' => $tpl_thumbnails_var_selection,
386    'derivative_params' => $derivative_params,
387    ) );
388
389  $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails');
390
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'] );
405}
406
407pwg_debug('end include/category_cats.inc.php');
408?>
Note: See TracBrowser for help on using the repository browser.