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

Last change on this file since 25425 was 25425, checked in by mistic100, 10 years ago

delete replace_space function, modify get_cat_display_name_* functions

  • Property svn:eol-style set to LF
File size: 11.3 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    nb_categories,
41    count_categories
42  FROM '.CATEGORIES_TABLE.' c
43    INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ucc
44    ON id = cat_id
45    AND user_id = '.$user['id'];
46
47if ('recent_cats' == $page['section'])
48{
49  $query.= '
50  WHERE '.get_recent_photos_sql('date_last');
51}
52else
53{
54  $query.= '
55  WHERE id_uppercat '.(!isset($page['category']) ? 'is NULL' : '= '.$page['category']['id']);
56}
57
58$query.= '
59      '.get_sql_condition_FandF(
60        array('visible_categories' => 'id'),
61        'AND'
62        );
63
64if ('recent_cats' != $page['section'])
65{
66  $query.= '
67  ORDER BY rank';
68}
69
70$result = pwg_query($query);
71$categories = array();
72$category_ids = array();
73$image_ids = array();
74$user_representative_updates_for = array();
75
76while ($row = pwg_db_fetch_assoc($result))
77{
78  $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last'];
79
80  if (!empty($row['user_representative_picture_id']))
81  {
82    $image_id = $row['user_representative_picture_id'];
83  }
84  elseif (!empty($row['representative_picture_id']))
85  { // if a representative picture is set, it has priority
86    $image_id = $row['representative_picture_id'];
87  }
88  elseif ($conf['allow_random_representative'])
89  { // searching a random representant among elements in sub-categories
90    $image_id = get_random_image_in_category($row);
91  }
92  elseif ($row['count_categories']>0 and $row['count_images']>0)
93  { // searching a random representant among representant of sub-categories
94    $query = '
95SELECT representative_picture_id
96  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
97  ON id = cat_id and user_id = '.$user['id'].'
98  WHERE uppercats LIKE \''.$row['uppercats'].',%\'
99    AND representative_picture_id IS NOT NULL'
100  .get_sql_condition_FandF
101  (
102    array
103      (
104        'visible_categories' => 'id',
105      ),
106    "\n  AND"
107  ).'
108  ORDER BY '.DB_RANDOM_FUNCTION.'()
109  LIMIT 1
110;';
111    $subresult = pwg_query($query);
112    if (pwg_db_num_rows($subresult) > 0)
113    {
114      list($image_id) = pwg_db_fetch_row($subresult);
115    }
116  }
117
118
119  if (isset($image_id))
120  {
121    if ($conf['representative_cache_on_subcats'] and $row['user_representative_picture_id'] != $image_id)
122    {
123      $user_representative_updates_for[ $row['id'] ] = $image_id;
124    }
125
126    $row['representative_picture_id'] = $image_id;
127    $image_ids[] = $image_id;
128    $categories[] = $row;
129    $category_ids[] = $row['id'];
130  }
131  unset($image_id);
132}
133
134if ($conf['display_fromto'])
135{
136  $dates_of_category = array();
137  if (count($category_ids) > 0)
138  {
139    $query = '
140SELECT
141    category_id,
142    MIN(date_creation) AS date_creation_min,
143    MAX(date_creation) AS date_creation_max
144  FROM '.IMAGE_CATEGORY_TABLE.'
145    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
146  WHERE category_id IN ('.implode(',', $category_ids).')
147'.get_sql_condition_FandF
148  (
149    array
150      (
151        'visible_categories' => 'category_id',
152        'visible_images' => 'id'
153      ),
154    'AND'
155  ).'
156  GROUP BY category_id
157;';
158    $result = pwg_query($query);
159    while ($row = pwg_db_fetch_assoc($result))
160    {
161      $dates_of_category[ $row['category_id'] ] = array(
162        'from' => $row['date_creation_min'],
163        'to'   => $row['date_creation_max'],
164        );
165    }
166  }
167}
168
169if ($page['section']=='recent_cats')
170{
171  usort($categories, 'global_rank_compare');
172}
173
174if (count($categories) > 0)
175{
176  $infos_of_image = array();
177  $new_image_ids = array();
178
179  $query = '
180SELECT *
181  FROM '.IMAGES_TABLE.'
182  WHERE id IN ('.implode(',', $image_ids).')
183;';
184  $result = pwg_query($query);
185  while ($row = pwg_db_fetch_assoc($result))
186  {
187    if ($row['level'] <= $user['level'])
188    {
189      $infos_of_image[$row['id']] = $row;
190    }
191    else
192    {
193      // problem: we must not display the thumbnail of a photo which has a
194      // higher privacy level than user privacy level
195      //
196      // * what is the represented category?
197      // * find a random photo matching user permissions
198      // * register it at user_representative_picture_id
199      // * set it as the representative_picture_id for the category
200
201      foreach ($categories as &$category)
202      {
203        if ($row['id'] == $category['representative_picture_id'])
204        {
205          // searching a random representant among elements in sub-categories
206          $image_id = get_random_image_in_category($category);
207
208          if (isset($image_id) and !in_array($image_id, $image_ids))
209          {
210            $new_image_ids[] = $image_id;
211          }
212
213          if ($conf['representative_cache_on_level'])
214          {
215            $user_representative_updates_for[ $category['id'] ] = $image_id;
216          }
217
218          $category['representative_picture_id'] = $image_id;
219        }
220      }
221      unset($category);
222    }
223  }
224
225  if (count($new_image_ids) > 0)
226  {
227    $query = '
228SELECT *
229  FROM '.IMAGES_TABLE.'
230  WHERE id IN ('.implode(',', $new_image_ids).')
231;';
232    $result = pwg_query($query);
233    while ($row = pwg_db_fetch_assoc($result))
234    {
235      $infos_of_image[$row['id']] = $row;
236    }
237  }
238
239  foreach ($infos_of_image as &$info)
240  {
241    $info['src_image'] = new SrcImage($info);
242  }
243  unset($info);
244}
245
246if (count($user_representative_updates_for))
247{
248  $updates = array();
249
250  foreach ($user_representative_updates_for as $cat_id => $image_id)
251  {
252    $updates[] =
253      array(
254        'user_id' => $user['id'],
255        'cat_id' => $cat_id,
256        'user_representative_picture_id' => $image_id,
257        );
258  }
259
260  mass_updates(
261    USER_CACHE_CATEGORIES_TABLE,
262    array(
263      'primary' => array('user_id', 'cat_id'),
264      'update'  => array('user_representative_picture_id')
265      ),
266    $updates
267    );
268}
269
270if (count($categories) > 0)
271{
272  // Update filtered data
273  if (function_exists('update_cats_with_filtered_data'))
274  {
275    update_cats_with_filtered_data($categories);
276  }
277
278  $template->set_filename('index_category_thumbnails', 'mainpage_categories.tpl');
279
280  trigger_action('loc_begin_index_category_thumbnails', $categories);
281
282  $tpl_thumbnails_var = array();
283
284  foreach ($categories as $category)
285  {
286    if (0 == $category['count_images'])
287    {
288      continue;
289    }
290
291    $category['name'] = trigger_event(
292        'render_category_name',
293        $category['name'],
294        'subcatify_category_name'
295        );
296
297    if ($page['section']=='recent_cats')
298    {
299      $name = get_cat_display_name_cache($category['uppercats'], null);
300    }
301    else
302    {
303      $name = $category['name'];
304    }
305
306    $representative_infos = $infos_of_image[ $category['representative_picture_id'] ];
307
308    $tpl_var = array_merge( $category, array(
309          'ID'    => $category['id'] /*obsolete*/,
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 (date('Y-m-d', strtotime($from)) == date('Y-m-d', strtotime($to)))
350          {
351            $info = format_date($from);
352          }
353          else
354          {
355            $info = l10n(
356              '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  // pagination
370  $page['total_categories'] = count($tpl_thumbnails_var);
371
372  $tpl_thumbnails_var_selection = array_slice(
373    $tpl_thumbnails_var,
374    $page['startcat'],
375    $conf['nb_categories_page']
376    );
377
378  $derivative_params = trigger_event('get_index_album_derivative_params', ImageStdParams::get_by_type(IMG_THUMB) );
379  $tpl_thumbnails_var_selection = trigger_event('loc_end_index_category_thumbnails', $tpl_thumbnails_var_selection);
380  $template->assign( array(
381    'maxRequests' =>$conf['max_requests'],
382    'category_thumbnails' => $tpl_thumbnails_var_selection,
383    'derivative_params' => $derivative_params,
384    ) );
385
386  $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails');
387
388  // navigation bar
389  $page['cats_navigation_bar'] = array();
390  if ($page['total_categories'] > $conf['nb_categories_page'])
391  {
392    $page['cats_navigation_bar'] = create_navigation_bar(
393      duplicate_index_url(array(), array('startcat')),
394      $page['total_categories'],
395      $page['startcat'],
396      $conf['nb_categories_page'],
397      true, 'startcat'
398      );
399  }
400
401  $template->assign('cats_navbar', $page['cats_navigation_bar'] );
402}
403
404pwg_debug('end include/category_cats.inc.php');
405?>
Note: See TracBrowser for help on using the repository browser.