source: branches/branch-1_7/include/category_cats.inc.php @ 2432

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

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: 10.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: category_cats.inc.php 2432 2008-07-12 14:27:22Z patdenice $
8// | last update   : $Date: 2008-07-12 14:27:22 +0000 (Sat, 12 Jul 2008) $
9// | last modifier : $Author: patdenice $
10// | revision      : $Revision: 2432 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27/**
28 * This file is included by the main page to show thumbnails for a category
29 * that have only subcategories or to show recent categories
30 *
31 */
32
33if ($page['section']=='recent_cats')
34{
35  // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE
36  $query = '
37SELECT
38  id, name, permalink, representative_picture_id, comment, nb_images, uppercats,
39  date_last, max_date_last, count_images, count_categories, global_rank
40  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
41  ON id = cat_id and user_id = '.$user['id'].'
42  WHERE date_last >= SUBDATE(
43    CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY
44  )
45'.get_sql_condition_FandF
46  (
47    array
48      (
49        'visible_categories' => 'id',
50      ),
51    'AND'
52  ).'
53;';
54}
55else
56{
57  // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE
58  $query = '
59SELECT
60  id, name, permalink, representative_picture_id, comment, nb_images, uppercats,
61  date_last, max_date_last, count_images, count_categories
62  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
63  ON id = cat_id and user_id = '.$user['id'].'
64  WHERE id_uppercat '.
65  (!isset($page['category']) ? 'is NULL' : '= '.$page['category']['id']).'
66'.get_sql_condition_FandF
67  (
68    array
69      (
70        'visible_categories' => 'id',
71      ),
72    'AND'
73  ).'
74  ORDER BY rank
75;';
76}
77
78$result = pwg_query($query);
79$categories = array();
80$image_ids = array();
81
82while ($row = mysql_fetch_assoc($result))
83{
84  $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last'];
85
86  if (isset($row['representative_picture_id'])
87      and is_numeric($row['representative_picture_id']))
88  { // if a representative picture is set, it has priority
89    $image_id = $row['representative_picture_id'];
90  }
91  else if ($conf['allow_random_representative'])
92  {// searching a random representant among elements in sub-categories
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  else
119  { // searching a random representant among representant of sub-categories
120    $query = '
121SELECT representative_picture_id
122  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
123  ON id = cat_id and user_id = '.$user['id'].'
124  WHERE uppercats LIKE \''.$row['uppercats'].',%\'
125    AND representative_picture_id IS NOT NULL'
126  .get_sql_condition_FandF
127  (
128    array
129      (
130        'visible_categories' => 'id',
131      ),
132    "\n  AND"
133  ).'
134  ORDER BY RAND()
135  LIMIT 0,1
136;';
137    $subresult = pwg_query($query);
138    if (mysql_num_rows($subresult) > 0)
139    {
140      list($image_id) = mysql_fetch_row($subresult);
141    }
142  }
143
144  if (isset($image_id))
145  {
146    $row['representative_picture_id'] = $image_id;
147    array_push($image_ids, $image_id);
148    array_push($categories, $row);
149  }
150  unset($image_id);
151}
152
153if ($page['section']=='recent_cats')
154{
155  usort($categories, 'global_rank_compare');
156}
157if (count($categories) > 0)
158{
159  $thumbnail_src_of = array();
160
161  $query = '
162SELECT id, path, tn_ext
163  FROM '.IMAGES_TABLE.'
164  WHERE id IN ('.implode(',', $image_ids).')
165;';
166  $result = pwg_query($query);
167  while ($row = mysql_fetch_assoc($result))
168  {
169    $thumbnail_src_of[$row['id']] = get_thumbnail_url($row);
170  }
171}
172
173if (count($categories) > 0)
174{
175  // Update filtered data
176  if (function_exists('update_cats_with_filtered_data'))
177  {
178    update_cats_with_filtered_data($categories);
179  }
180
181  trigger_action('loc_begin_index_category_thumbnails', $categories);
182  if ($conf['subcatify'])
183  {
184    $template->set_filename('mainpage_categories', 'mainpage_categories.tpl');
185
186    foreach ($categories as $category)
187    {
188      $category['name'] = trigger_event(
189        'render_category_name',
190        $category['name'],
191        'subcatify_category_name'
192        );
193
194      if ($page['section']=='recent_cats')
195      {
196        $name = get_cat_display_name_cache($category['uppercats'], null, false);
197      }
198      else
199      {
200        $name = $category['name'];
201      }
202
203      $icon_ts = get_icon($category['max_date_last'], $category['is_child_date_last']);
204
205      $template->assign_block_vars(
206        'categories.category',
207        array(
208          'SRC'   => $thumbnail_src_of[$category['representative_picture_id']],
209          'ALT'   => strip_tags($category['name']),
210          'TITLE' => l10n('hint_category'),
211          'ICON'  => $icon_ts,
212
213          'URL'   => make_index_url(
214            array(
215              'category' => $category
216              )
217            ),
218          'CAPTION_NB_IMAGES' => get_display_images_count
219                                  (
220                                    $category['nb_images'],
221                                    $category['count_images'],
222                                    $category['count_categories'],
223                                    true,
224                                    '<br />'
225                                  ),
226          'DESCRIPTION' =>
227            trigger_event('render_category_literal_description',
228              trigger_event('render_category_description',
229                @$category['comment'],
230                'subcatify_category_description')),
231          'NAME'  => $name,
232          )
233        );
234
235      //plugins need to add/modify sth in this loop ?
236      trigger_action('loc_index_category_thumbnail',
237        $category, 'categories.category' );
238    }
239
240    $template->assign_var_from_handle('CATEGORIES', 'mainpage_categories');
241  }
242  else
243  {
244    $template->set_filename( 'thumbnails', 'thumbnails.tpl');
245    // first line
246    $template->assign_block_vars('thumbnails.line', array());
247    // current row displayed
248    $row_number = 0;
249
250    if ($page['section']=='recent_cats')
251    {
252      $old_level_separator = $conf['level_separator'];
253      $conf['level_separator'] = '<br />';
254    }
255
256    foreach ($categories as $category)
257    {
258      $category['name'] = trigger_event(
259        'render_category_name',
260        $category['name'],
261        'thumbnail_category_name'
262        );
263
264      $template->assign_block_vars(
265        'thumbnails.line.thumbnail',
266        array(
267          'IMAGE'       => $thumbnail_src_of[ $category['representative_picture_id'] ],
268          'IMAGE_ALT'   => strip_tags($category['name']),
269          'IMAGE_TITLE' => get_display_images_count
270                                  (
271                                    $category['nb_images'],
272                                    $category['count_images'],
273                                    $category['count_categories'],
274                                    true,
275                                    ' / '
276                                  ),
277
278          'U_IMG_LINK'  => make_index_url(
279            array(
280              'category' => $category
281              )
282            ),
283          'CLASS'       => 'thumbCat',
284          )
285        );
286      if ($page['section']=='recent_cats')
287      {
288        $name = get_cat_display_name_cache($category['uppercats'], null, false);
289      }
290      else
291      {
292        $name = $category['name'];
293        $template->merge_block_vars(
294          'thumbnails.line.thumbnail',
295          array(
296            'IMAGE_TS'    => get_icon($category['max_date_last'], $category['is_child_date_last']),
297           )
298         );
299      }
300      $template->assign_block_vars(
301        'thumbnails.line.thumbnail.category_name',
302        array(
303          'NAME' => $name
304          )
305        );
306
307      //plugins need to add/modify sth in this loop ?
308      trigger_action('loc_index_category_thumbnail',
309        $category, 'thumbnails.line.thumbnail' );
310
311      // create a new line ?
312      if (++$row_number == $user['nb_image_line'])
313      {
314        $template->assign_block_vars('thumbnails.line', array());
315        $row_number = 0;
316      }
317    }
318
319    if ( isset($old_level_separator) )
320    {
321      $conf['level_separator']=$old_level_separator;
322    }
323
324    $template->assign_var_from_handle('CATEGORIES', 'thumbnails');
325    $template->delete_block_vars('thumbnails', true); // category_default reuse them
326  }
327  trigger_action('loc_end_index_category_thumbnails', $categories);
328}
329?>
Note: See TracBrowser for help on using the repository browser.