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

Last change on this file since 1851 was 1851, checked in by rub, 17 years ago

o Proposition: improved display of 'x images in y sub-categories' or 'x images in this category' for cases when categories contain both images and sub-categories
o Good idea of this new way for way confguest_access, but I kept last implementation for access methods (Could be useful on future development)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 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 1851 2007-02-22 20:20:30Z rub $
8// | last update   : $Date: 2007-02-22 20:20:30 +0000 (Thu, 22 Feb 2007) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 1851 $
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, 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, representative_picture_id, comment, nb_images,
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']).'
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 uppercats REGEXP \'(^|,)'.$row['id'].'(,|$)\'
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    '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 REGEXP \'(^|,)'.$row['id'].'(,|$)\'
125    AND representative_picture_id IS NOT NULL
126'.get_sql_condition_FandF
127  (
128    array
129      (
130        'visible_categories' => 'id',
131      ),
132    '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  if ($conf['subcatify'])
182  {
183    $template->set_filename('mainpage_categories', 'mainpage_categories.tpl');
184
185    foreach ($categories as $category)
186    {
187      $comment = strip_tags(@$category['comment'], '<a><br><p><b><i><small><strong><font>');
188      if ($page['section']=='recent_cats')
189      {
190        $name = get_cat_display_name_cache($category['uppercats'], null, false);
191      }
192      else
193      {
194        $name = $category['name'];
195      }
196
197      $icon_ts = get_icon($category['max_date_last'], $category['is_child_date_last']);
198
199      $template->assign_block_vars(
200        'categories.category',
201        array(
202          'SRC'   => $thumbnail_src_of[$category['representative_picture_id']],
203          'ALT'   => $category['name'],
204          'TITLE' => $lang['hint_category'],
205          'ICON'  => $icon_ts,
206
207          'URL'   => make_index_url(
208            array(
209              'category' => $category['id'],
210              'cat_name' => $category['name'],
211              )
212            ),
213          'CAPTION_NB_IMAGES' => get_display_images_count
214                                  (
215                                    $category['nb_images'],
216                                    $category['count_images'],
217                                    $category['count_categories'],
218                                    true,
219                                    '<BR>'
220                                  ),
221          'DESCRIPTION' => @$comment,
222          'NAME'  => $name,
223          )
224        );
225    }
226
227    $template->assign_var_from_handle('CATEGORIES', 'mainpage_categories');
228  }
229  else
230  {
231    $template->set_filename( 'thumbnails', 'thumbnails.tpl');
232    // first line
233    $template->assign_block_vars('thumbnails.line', array());
234    // current row displayed
235    $row_number = 0;
236
237    if ($page['section']=='recent_cats')
238    {
239      $old_level_separator = $conf['level_separator'];
240      $conf['level_separator'] = '<br />';
241    }
242
243    foreach ($categories as $category)
244    {
245      $template->assign_block_vars(
246        'thumbnails.line.thumbnail',
247        array(
248          'IMAGE'       => $thumbnail_src_of[ $category['representative_picture_id'] ],
249          'IMAGE_ALT'   => $category['name'],
250          'IMAGE_TITLE' => get_display_images_count
251                                  (
252                                    $category['nb_images'],
253                                    $category['count_images'],
254                                    $category['count_categories'],
255                                    true,
256                                    '<BR>'
257                                  ),
258
259          'U_IMG_LINK'  => make_index_url(
260            array(
261              'category' => $category['id'],
262              'cat_name' => $category['name'],
263              )
264            ),
265          'CLASS'       => 'thumbCat',
266          )
267        );
268      if ($page['section']=='recent_cats')
269      {
270        $name = get_cat_display_name_cache($category['uppercats'], null, false);
271      }
272      else
273      {
274        $name = $category['name'];
275        $template->merge_block_vars(
276          'thumbnails.line.thumbnail',
277          array(
278            'IMAGE_TS'    => get_icon($category['max_date_last'], $category['is_child_date_last']),
279           )
280         );
281      }
282      $template->assign_block_vars(
283        'thumbnails.line.thumbnail.category_name',
284        array(
285          'NAME' => $name
286          )
287        );
288
289      // create a new line ?
290      if (++$row_number == $user['nb_image_line'])
291      {
292        $template->assign_block_vars('thumbnails.line', array());
293        $row_number = 0;
294      }
295    }
296
297    if ( isset($old_level_separator) )
298    {
299      $conf['level_separator']=$old_level_separator;
300    }
301
302    $template->assign_var_from_handle('CATEGORIES', 'thumbnails');
303    unset( $template->_tpldata['thumbnails.'] );//maybe write a func for that
304  }
305}
306?>
Note: See TracBrowser for help on using the repository browser.