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

Last change on this file since 28715 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
RevLine 
[1597]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[26461]5// | Copyright(C) 2008-2014 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,
[22879]40    nb_categories,
[8802]41    count_categories
42  FROM '.CATEGORIES_TABLE.' c
[18924]43    INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ucc
44    ON id = cat_id
[18462]45    AND user_id = '.$user['id'];
[18924]46
[18462]47if ('recent_cats' == $page['section'])
48{
49  $query.= '
[21802]50  WHERE '.get_recent_photos_sql('date_last');
[18462]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        );
[18924]63
[8802]64if ('recent_cats' != $page['section'])
65{
66  $query.= '
67  ORDER BY rank';
68}
69
[18892]70$result = pwg_query($query);
[1597]71$categories = array();
[1970]72$category_ids = array();
[1597]73$image_ids = array();
[8802]74$user_representative_updates_for = array();
[1597]75
[18892]76while ($row = pwg_db_fetch_assoc($result))
[1597]77{
[1643]78  $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last'];
[1624]79
[8802]80  if (!empty($row['user_representative_picture_id']))
81  {
82    $image_id = $row['user_representative_picture_id'];
83  }
[21802]84  elseif (!empty($row['representative_picture_id']))
[1597]85  { // if a representative picture is set, it has priority
86    $image_id = $row['representative_picture_id'];
87  }
[21802]88  elseif ($conf['allow_random_representative'])
89  { // searching a random representant among elements in sub-categories
[8802]90    $image_id = get_random_image_in_category($row);
[1597]91  }
[21802]92  elseif ($row['count_categories']>0 and $row['count_images']>0)
[1597]93  { // searching a random representant among representant of sub-categories
[21802]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)
[1597]113    {
[21802]114      list($image_id) = pwg_db_fetch_row($subresult);
[1597]115    }
116  }
117
[21802]118
[1597]119  if (isset($image_id))
120  {
[11739]121    if ($conf['representative_cache_on_subcats'] and $row['user_representative_picture_id'] != $image_id)
[8802]122    {
[22879]123      $user_representative_updates_for[ $row['id'] ] = $image_id;
[8802]124    }
[18924]125
[1597]126    $row['representative_picture_id'] = $image_id;
[21802]127    $image_ids[] = $image_id;
128    $categories[] = $row;
129    $category_ids[] = $row['id'];
[1597]130  }
131  unset($image_id);
132}
133
[1970]134if ($conf['display_fromto'])
135{
136  if (count($category_ids) > 0)
137  {
138    $query = '
139SELECT
140    category_id,
[27336]141    MIN(date_creation) AS `from`,
142    MAX(date_creation) AS `to`
[1970]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;';
[27336]157    $dates_of_category = query2array($query, 'category_id');
[1970]158  }
159}
160
[18892]161if ($page['section']=='recent_cats')
162{
163  usort($categories, 'global_rank_compare');
164}
165
[1597]166if (count($categories) > 0)
167{
[12546]168  $infos_of_image = array();
[8802]169  $new_image_ids = array();
[1597]170
171  $query = '
[12546]172SELECT *
[1597]173  FROM '.IMAGES_TABLE.'
174  WHERE id IN ('.implode(',', $image_ids).')
175;';
176  $result = pwg_query($query);
[4325]177  while ($row = pwg_db_fetch_assoc($result))
[1597]178  {
[8802]179    if ($row['level'] <= $user['level'])
180    {
[12546]181      $infos_of_image[$row['id']] = $row;
[8802]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        {
[9365]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))
[8802]201          {
[25018]202            $new_image_ids[] = $image_id;
[8802]203          }
[11739]204
205          if ($conf['representative_cache_on_level'])
206          {
[22879]207            $user_representative_updates_for[ $category['id'] ] = $image_id;
[11739]208          }
[18924]209
[9365]210          $category['representative_picture_id'] = $image_id;
[8802]211        }
212      }
213      unset($category);
214    }
[1597]215  }
[8802]216
217  if (count($new_image_ids) > 0)
218  {
219    $query = '
[12546]220SELECT *
[8802]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    {
[12546]227      $infos_of_image[$row['id']] = $row;
[8802]228    }
229  }
[18924]230
[12920]231  foreach ($infos_of_image as &$info)
232  {
233    $info['src_image'] = new SrcImage($info);
234  }
235  unset($info);
[1597]236}
237
[8802]238if (count($user_representative_updates_for))
239{
240  $updates = array();
[18924]241
[22879]242  foreach ($user_representative_updates_for as $cat_id => $image_id)
[8802]243  {
[22879]244    $updates[] =
[8802]245      array(
[22879]246        'user_id' => $user['id'],
[8802]247        'cat_id' => $cat_id,
248        'user_representative_picture_id' => $image_id,
[22879]249        );
[8802]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
[1597]262if (count($categories) > 0)
263{
[1677]264  // Update filtered data
[1722]265  if (function_exists('update_cats_with_filtered_data'))
266  {
267    update_cats_with_filtered_data($categories);
268  }
[2079]269
[2274]270  $template->set_filename('index_category_thumbnails', 'mainpage_categories.tpl');
271
[28587]272  trigger_notify('loc_begin_index_category_thumbnails', $categories);
[2274]273
[3124]274  $tpl_thumbnails_var = array();
275
[2274]276  foreach ($categories as $category)
[1597]277  {
[9365]278    if (0 == $category['count_images'])
279    {
280      continue;
281    }
[18924]282
[28587]283    $category['name'] = trigger_change(
[2433]284        'render_category_name',
285        $category['name'],
286        'subcatify_category_name'
287        );
288
[2274]289    if ($page['section']=='recent_cats')
[1597]290    {
[25425]291      $name = get_cat_display_name_cache($category['uppercats'], null);
[2274]292    }
293    else
294    {
295      $name = $category['name'];
296    }
[1597]297
[12546]298    $representative_infos = $infos_of_image[ $category['representative_picture_id'] ];
299
[18924]300    $tpl_var = array_merge( $category, array(
301          'ID'    => $category['id'] /*obsolete*/,
[12920]302          'representative'   => $representative_infos,
[2515]303          'TN_ALT'   => strip_tags($category['name']),
[1597]304
305          'URL'   => make_index_url(
306            array(
[1861]307              'category' => $category
[1597]308              )
309            ),
[1624]310          'CAPTION_NB_IMAGES' => get_display_images_count
311                                  (
312                                    $category['nb_images'],
313                                    $category['count_images'],
[1851]314                                    $category['count_categories'],
315                                    true,
[3185]316                                    '<br>'
[1624]317                                  ),
[2079]318          'DESCRIPTION' =>
[28587]319            trigger_change('render_category_literal_description',
320              trigger_change('render_category_description',
[2175]321                @$category['comment'],
322                'subcatify_category_description')),
[1597]323          'NAME'  => $name,
[18924]324        ) );
[11285]325    if ($conf['index_new_icon'])
326    {
[11591]327      $tpl_var['icon_ts'] = get_icon($category['max_date_last'], $category['is_child_date_last']);
[11285]328    }
[1970]329
[2274]330    if ($conf['display_fromto'])
331    {
332      if (isset($dates_of_category[ $category['id'] ]))
[1970]333      {
[2274]334        $from = $dates_of_category[ $category['id'] ]['from'];
335        $to   = $dates_of_category[ $category['id'] ]['to'];
336
337        if (!empty($from))
[1970]338        {
[2274]339          $info = '';
[2091]340
[19420]341          if (date('Y-m-d', strtotime($from)) == date('Y-m-d', strtotime($to)))
[1970]342          {
[2274]343            $info = format_date($from);
[1970]344          }
[2274]345          else
346          {
[25005]347            $info = l10n(
348              'from %s to %s',
[2274]349              format_date($from),
350              format_date($to)
351              );
352          }
353          $tpl_var['INFO_DATES'] = $info;
[1970]354        }
[2274]355      }
356    }//fromto
[2234]357
[3124]358    $tpl_thumbnails_var[] = $tpl_var;
[1597]359  }
[18924]360
[18892]361  // pagination
362  $page['total_categories'] = count($tpl_thumbnails_var);
[1597]363
[19002]364  $tpl_thumbnails_var_selection = array_slice(
[18924]365    $tpl_thumbnails_var,
[18892]366    $page['startcat'],
367    $conf['nb_categories_page']
368    );
369
[28587]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);
[12920]372  $template->assign( array(
[20177]373    'maxRequests' =>$conf['max_requests'],
[19002]374    'category_thumbnails' => $tpl_thumbnails_var_selection,
[12920]375    'derivative_params' => $derivative_params,
376    ) );
[3124]377
[2274]378  $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails');
[18924]379
[18462]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'] );
[1597]394}
[18462]395
[3124]396pwg_debug('end include/category_cats.inc.php');
397?>
Note: See TracBrowser for help on using the repository browser.