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

Last change on this file since 2433 was 2433, checked in by patdenice, 16 years ago

Merge from branch-1_7 (r2432).

Add triggers for category name (render_category_name).
Add strip_tags for ALT attribute on category thumbnail.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      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 thumbnails for a category
26 * that have only subcategories or to show recent categories
27 *
28 */
29
30if ($page['section']=='recent_cats')
31{
32  // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE
33  $query = '
34SELECT
35  id, name, permalink, representative_picture_id, comment, nb_images, uppercats,
36  date_last, max_date_last, count_images, count_categories, global_rank
37  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
38  ON id = cat_id and user_id = '.$user['id'].'
39  WHERE date_last >= SUBDATE(
40    CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY
41  )
42'.get_sql_condition_FandF
43  (
44    array
45      (
46        'visible_categories' => 'id',
47      ),
48    'AND'
49  ).'
50;';
51}
52else
53{
54  // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE
55  $query = '
56SELECT
57  id, name, permalink, representative_picture_id, comment, nb_images, uppercats,
58  date_last, max_date_last, count_images, count_categories
59  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
60  ON id = cat_id and user_id = '.$user['id'].'
61  WHERE id_uppercat '.
62  (!isset($page['category']) ? 'is NULL' : '= '.$page['category']['id']).'
63'.get_sql_condition_FandF
64  (
65    array
66      (
67        'visible_categories' => 'id',
68      ),
69    'AND'
70  ).'
71  ORDER BY rank
72;';
73}
74
75$result = pwg_query($query);
76$categories = array();
77$category_ids = array();
78$image_ids = array();
79
80while ($row = mysql_fetch_assoc($result))
81{
82  $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last'];
83
84  if (isset($row['representative_picture_id'])
85      and is_numeric($row['representative_picture_id']))
86  { // if a representative picture is set, it has priority
87    $image_id = $row['representative_picture_id'];
88  }
89  else if ($conf['allow_random_representative'])
90  {// searching a random representant among elements in sub-categories
91    if ($row['count_images']>0)
92    {
93      $query = '
94SELECT image_id
95  FROM '.CATEGORIES_TABLE.' AS c INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic
96    ON ic.category_id = c.id';
97      $query.= '
98  WHERE (c.id='.$row['id'].' OR uppercats LIKE \''.$row['uppercats'].',%\')'
99    .get_sql_condition_FandF
100    (
101      array
102        (
103          'forbidden_categories' => 'c.id',
104          'visible_categories' => 'c.id',
105          'visible_images' => 'image_id'
106        ),
107      "\n  AND"
108    ).'
109  ORDER BY RAND()
110  LIMIT 0,1
111;';
112      $subresult = pwg_query($query);
113      if (mysql_num_rows($subresult) > 0)
114      {
115        list($image_id) = mysql_fetch_row($subresult);
116      }
117    }
118  }
119  else
120  { // searching a random representant among representant of sub-categories
121    if ($row['count_categories']>0 and $row['count_images']>0)
122    {
123      $query = '
124  SELECT representative_picture_id
125    FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
126    ON id = cat_id and user_id = '.$user['id'].'
127    WHERE uppercats LIKE \''.$row['uppercats'].',%\'
128      AND representative_picture_id IS NOT NULL'
129    .get_sql_condition_FandF
130    (
131      array
132        (
133          'visible_categories' => 'id',
134        ),
135      "\n  AND"
136    ).'
137    ORDER BY RAND()
138    LIMIT 0,1
139  ;';
140      $subresult = pwg_query($query);
141      if (mysql_num_rows($subresult) > 0)
142      {
143        list($image_id) = mysql_fetch_row($subresult);
144      }
145    }
146  }
147
148  if (isset($image_id))
149  {
150    $row['representative_picture_id'] = $image_id;
151    array_push($image_ids, $image_id);
152    array_push($categories, $row);
153    array_push($category_ids, $row['id']);
154  }
155  unset($image_id);
156}
157
158if ($conf['display_fromto'])
159{
160  $dates_of_category = array();
161  if (count($category_ids) > 0)
162  {
163    $query = '
164SELECT
165    category_id,
166    MIN(date_creation) AS date_creation_min,
167    MAX(date_creation) AS date_creation_max
168  FROM '.IMAGE_CATEGORY_TABLE.'
169    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
170  WHERE category_id IN ('.implode(',', $category_ids).')
171'.get_sql_condition_FandF
172  (
173    array
174      (
175        'visible_categories' => 'category_id',
176        'visible_images' => 'id'
177      ),
178    'AND'
179  ).'
180  GROUP BY category_id
181;';
182    $result = pwg_query($query);
183    while ($row = mysql_fetch_array($result))
184    {
185      $dates_of_category[ $row['category_id'] ] = array(
186        'from' => $row['date_creation_min'],
187        'to'   => $row['date_creation_max'],
188        );
189    }
190  }
191}
192
193if ($page['section']=='recent_cats')
194{
195  usort($categories, 'global_rank_compare');
196}
197if (count($categories) > 0)
198{
199  $thumbnail_src_of = array();
200
201  $query = '
202SELECT id, path, tn_ext
203  FROM '.IMAGES_TABLE.'
204  WHERE id IN ('.implode(',', $image_ids).')
205;';
206  $result = pwg_query($query);
207  while ($row = mysql_fetch_assoc($result))
208  {
209    $thumbnail_src_of[$row['id']] = get_thumbnail_url($row);
210  }
211}
212
213if (count($categories) > 0)
214{
215  // Update filtered data
216  if (function_exists('update_cats_with_filtered_data'))
217  {
218    update_cats_with_filtered_data($categories);
219  }
220
221  $template->set_filename('index_category_thumbnails', 'mainpage_categories.tpl');
222
223  trigger_action('loc_begin_index_category_thumbnails', $categories);
224
225  foreach ($categories as $category)
226  {
227    $category['name'] = trigger_event(
228        'render_category_name',
229        $category['name'],
230        'subcatify_category_name'
231        );
232
233    if ($page['section']=='recent_cats')
234    {
235      $name = get_cat_display_name_cache($category['uppercats'], null, false);
236    }
237    else
238    {
239      $name = $category['name'];
240    }
241
242    $icon_ts = get_icon($category['max_date_last'], $category['is_child_date_last']);
243
244    $tpl_var =
245        array(
246          'ID'    => $category['id'],
247          'TN_SRC'   => $thumbnail_src_of[$category['representative_picture_id']],
248          'ALT'   => strip_tags($category['name']),
249          'ICON'  => $icon_ts,
250
251          'URL'   => make_index_url(
252            array(
253              'category' => $category
254              )
255            ),
256          'CAPTION_NB_IMAGES' => get_display_images_count
257                                  (
258                                    $category['nb_images'],
259                                    $category['count_images'],
260                                    $category['count_categories'],
261                                    true,
262                                    '<br />'
263                                  ),
264          'DESCRIPTION' =>
265            trigger_event('render_category_literal_description',
266              trigger_event('render_category_description',
267                @$category['comment'],
268                'subcatify_category_description')),
269          'NAME'  => $name,
270        );
271
272    if ($conf['display_fromto'])
273    {
274      if (isset($dates_of_category[ $category['id'] ]))
275      {
276        $from = $dates_of_category[ $category['id'] ]['from'];
277        $to   = $dates_of_category[ $category['id'] ]['to'];
278
279        if (!empty($from))
280        {
281          $info = '';
282
283          if ($from == $to)
284          {
285            $info = format_date($from);
286          }
287          else
288          {
289            $info = sprintf(
290              l10n('from %s to %s'),
291              format_date($from),
292              format_date($to)
293              );
294          }
295          $tpl_var['INFO_DATES'] = $info;
296        }
297      }
298    }//fromto
299
300    //plugins need to add/modify sth in this loop ?
301    $tpl_var = trigger_event('loc_index_category_thumbnail',
302                  $tpl_var, $category );
303
304    $template->append( 'category_thumbnails', $tpl_var);
305  }
306
307  trigger_action('loc_end_index_category_thumbnails', $categories);
308  $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails');
309}
310?>
Note: See TracBrowser for help on using the repository browser.