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

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

feature 3010 : replace trigger_action/event by trigger_notify/change

  • Property svn:eol-style set to LF
File size: 11.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 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  if (count($category_ids) > 0)
137  {
138    $query = '
139SELECT
140    category_id,
141    MIN(date_creation) AS `from`,
142    MAX(date_creation) AS `to`
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    $dates_of_category = query2array($query, 'category_id');
158  }
159}
160
161if ($page['section']=='recent_cats')
162{
163  usort($categories, 'global_rank_compare');
164}
165
166if (count($categories) > 0)
167{
168  $infos_of_image = array();
169  $new_image_ids = array();
170
171  $query = '
172SELECT *
173  FROM '.IMAGES_TABLE.'
174  WHERE id IN ('.implode(',', $image_ids).')
175;';
176  $result = pwg_query($query);
177  while ($row = pwg_db_fetch_assoc($result))
178  {
179    if ($row['level'] <= $user['level'])
180    {
181      $infos_of_image[$row['id']] = $row;
182    }
183    else
184    {
185      // problem: we must not display the thumbnail of a photo which has a
186      // higher privacy level than user privacy level
187      //
188      // * what is the represented category?
189      // * find a random photo matching user permissions
190      // * register it at user_representative_picture_id
191      // * set it as the representative_picture_id for the category
192
193      foreach ($categories as &$category)
194      {
195        if ($row['id'] == $category['representative_picture_id'])
196        {
197          // searching a random representant among elements in sub-categories
198          $image_id = get_random_image_in_category($category);
199
200          if (isset($image_id) and !in_array($image_id, $image_ids))
201          {
202            $new_image_ids[] = $image_id;
203          }
204
205          if ($conf['representative_cache_on_level'])
206          {
207            $user_representative_updates_for[ $category['id'] ] = $image_id;
208          }
209
210          $category['representative_picture_id'] = $image_id;
211        }
212      }
213      unset($category);
214    }
215  }
216
217  if (count($new_image_ids) > 0)
218  {
219    $query = '
220SELECT *
221  FROM '.IMAGES_TABLE.'
222  WHERE id IN ('.implode(',', $new_image_ids).')
223;';
224    $result = pwg_query($query);
225    while ($row = pwg_db_fetch_assoc($result))
226    {
227      $infos_of_image[$row['id']] = $row;
228    }
229  }
230
231  foreach ($infos_of_image as &$info)
232  {
233    $info['src_image'] = new SrcImage($info);
234  }
235  unset($info);
236}
237
238if (count($user_representative_updates_for))
239{
240  $updates = array();
241
242  foreach ($user_representative_updates_for as $cat_id => $image_id)
243  {
244    $updates[] =
245      array(
246        'user_id' => $user['id'],
247        'cat_id' => $cat_id,
248        'user_representative_picture_id' => $image_id,
249        );
250  }
251
252  mass_updates(
253    USER_CACHE_CATEGORIES_TABLE,
254    array(
255      'primary' => array('user_id', 'cat_id'),
256      'update'  => array('user_representative_picture_id')
257      ),
258    $updates
259    );
260}
261
262if (count($categories) > 0)
263{
264  // Update filtered data
265  if (function_exists('update_cats_with_filtered_data'))
266  {
267    update_cats_with_filtered_data($categories);
268  }
269
270  $template->set_filename('index_category_thumbnails', 'mainpage_categories.tpl');
271
272  trigger_notify('loc_begin_index_category_thumbnails', $categories);
273
274  $tpl_thumbnails_var = array();
275
276  foreach ($categories as $category)
277  {
278    if (0 == $category['count_images'])
279    {
280      continue;
281    }
282
283    $category['name'] = trigger_change(
284        'render_category_name',
285        $category['name'],
286        'subcatify_category_name'
287        );
288
289    if ($page['section']=='recent_cats')
290    {
291      $name = get_cat_display_name_cache($category['uppercats'], null);
292    }
293    else
294    {
295      $name = $category['name'];
296    }
297
298    $representative_infos = $infos_of_image[ $category['representative_picture_id'] ];
299
300    $tpl_var = array_merge( $category, array(
301          'ID'    => $category['id'] /*obsolete*/,
302          'representative'   => $representative_infos,
303          'TN_ALT'   => strip_tags($category['name']),
304
305          'URL'   => make_index_url(
306            array(
307              'category' => $category
308              )
309            ),
310          'CAPTION_NB_IMAGES' => get_display_images_count
311                                  (
312                                    $category['nb_images'],
313                                    $category['count_images'],
314                                    $category['count_categories'],
315                                    true,
316                                    '<br>'
317                                  ),
318          'DESCRIPTION' =>
319            trigger_change('render_category_literal_description',
320              trigger_change('render_category_description',
321                @$category['comment'],
322                'subcatify_category_description')),
323          'NAME'  => $name,
324        ) );
325    if ($conf['index_new_icon'])
326    {
327      $tpl_var['icon_ts'] = get_icon($category['max_date_last'], $category['is_child_date_last']);
328    }
329
330    if ($conf['display_fromto'])
331    {
332      if (isset($dates_of_category[ $category['id'] ]))
333      {
334        $from = $dates_of_category[ $category['id'] ]['from'];
335        $to   = $dates_of_category[ $category['id'] ]['to'];
336
337        if (!empty($from))
338        {
339          $info = '';
340
341          if (date('Y-m-d', strtotime($from)) == date('Y-m-d', strtotime($to)))
342          {
343            $info = format_date($from);
344          }
345          else
346          {
347            $info = l10n(
348              'from %s to %s',
349              format_date($from),
350              format_date($to)
351              );
352          }
353          $tpl_var['INFO_DATES'] = $info;
354        }
355      }
356    }//fromto
357
358    $tpl_thumbnails_var[] = $tpl_var;
359  }
360
361  // pagination
362  $page['total_categories'] = count($tpl_thumbnails_var);
363
364  $tpl_thumbnails_var_selection = array_slice(
365    $tpl_thumbnails_var,
366    $page['startcat'],
367    $conf['nb_categories_page']
368    );
369
370  $derivative_params = trigger_change('get_index_album_derivative_params', ImageStdParams::get_by_type(IMG_THUMB) );
371  $tpl_thumbnails_var_selection = trigger_change('loc_end_index_category_thumbnails', $tpl_thumbnails_var_selection);
372  $template->assign( array(
373    'maxRequests' =>$conf['max_requests'],
374    'category_thumbnails' => $tpl_thumbnails_var_selection,
375    'derivative_params' => $derivative_params,
376    ) );
377
378  $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails');
379
380  // navigation bar
381  $page['cats_navigation_bar'] = array();
382  if ($page['total_categories'] > $conf['nb_categories_page'])
383  {
384    $page['cats_navigation_bar'] = create_navigation_bar(
385      duplicate_index_url(array(), array('startcat')),
386      $page['total_categories'],
387      $page['startcat'],
388      $conf['nb_categories_page'],
389      true, 'startcat'
390      );
391  }
392
393  $template->assign('cats_navbar', $page['cats_navigation_bar'] );
394}
395
396pwg_debug('end include/category_cats.inc.php');
397?>
Note: See TracBrowser for help on using the repository browser.