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

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

Add filter conditions on new sql query added for feature 660.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 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 1971 2007-04-22 16:19:16Z rub $
8// | last update   : $Date: 2007-04-22 16:19:16 +0000 (Sun, 22 Apr 2007) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 1971 $
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,
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$category_ids = array();
81$image_ids = array();
82
83while ($row = mysql_fetch_assoc($result))
84{
85  $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last'];
86
87  if (isset($row['representative_picture_id'])
88      and is_numeric($row['representative_picture_id']))
89  { // if a representative picture is set, it has priority
90    $image_id = $row['representative_picture_id'];
91  }
92  else if ($conf['allow_random_representative'])
93  {// searching a random representant among elements in sub-categories
94    $query = '
95SELECT image_id
96  FROM '.CATEGORIES_TABLE.' AS c INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic
97    ON ic.category_id = c.id';
98    $query.= '
99  WHERE uppercats REGEXP \'(^|,)'.$row['id'].'(,|$)\'
100'.get_sql_condition_FandF
101  (
102    array
103      (
104        'forbidden_categories' => 'c.id',
105        'visible_categories' => 'c.id',
106        'visible_images' => 'image_id'
107      ),
108    'AND'
109  ).'
110  ORDER BY RAND()
111  LIMIT 0,1
112;';
113    $subresult = pwg_query($query);
114    if (mysql_num_rows($subresult) > 0)
115    {
116      list($image_id) = mysql_fetch_row($subresult);
117    }
118  }
119  else
120  { // searching a random representant among representant of sub-categories
121    $query = '
122SELECT representative_picture_id
123  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
124  ON id = cat_id and user_id = '.$user['id'].'
125  WHERE uppercats REGEXP \'(^|,)'.$row['id'].'(,|$)\'
126    AND representative_picture_id IS NOT NULL
127'.get_sql_condition_FandF
128  (
129    array
130      (
131        'visible_categories' => 'id',
132      ),
133    'AND'
134  ).'
135  ORDER BY RAND()
136  LIMIT 0,1
137;';
138    $subresult = pwg_query($query);
139    if (mysql_num_rows($subresult) > 0)
140    {
141      list($image_id) = mysql_fetch_row($subresult);
142    }
143  }
144
145  if (isset($image_id))
146  {
147    $row['representative_picture_id'] = $image_id;
148    array_push($image_ids, $image_id);
149    array_push($categories, $row);
150    array_push($category_ids, $row['id']);
151  }
152  unset($image_id);
153}
154
155if ($conf['display_fromto'])
156{
157  $dates_of_category = array();
158  if (count($category_ids) > 0)
159  {
160    $query = '
161SELECT
162    category_id,
163    MIN(date_creation) AS date_creation_min,
164    MAX(date_creation) AS date_creation_max
165  FROM '.IMAGE_CATEGORY_TABLE.'
166    INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
167      ON category_id = cat_id and user_id = '.$user['id'].'
168    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
169  WHERE category_id IN ('.implode(',', $category_ids).')
170'.get_sql_condition_FandF
171  (
172    array
173      (
174        'visible_categories' => 'category_id',
175        'visible_images' => 'image_id'
176      ),
177    'AND'
178  ).'
179  GROUP BY category_id
180;';
181    $result = pwg_query($query);
182    while ($row = mysql_fetch_array($result))
183    {
184      $dates_of_category[ $row['category_id'] ] = array(
185        'from' => $row['date_creation_min'],
186        'to'   => $row['date_creation_max'],
187        );
188    }
189    // echo '<pre>'; print_r($dates_of_category); echo '</pre>';
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  trigger_action('loc_begin_index_category_thumbnails', $categories);
221  if ($conf['subcatify'])
222  {
223    $template->set_filename('mainpage_categories', 'mainpage_categories.tpl');
224
225    foreach ($categories as $category)
226    {
227      $comment = strip_tags(@$category['comment'], '<a><br><p><b><i><small><strong><font>');
228      if ($page['section']=='recent_cats')
229      {
230        $name = get_cat_display_name_cache($category['uppercats'], null, false);
231      }
232      else
233      {
234        $name = $category['name'];
235      }
236
237      $icon_ts = get_icon($category['max_date_last'], $category['is_child_date_last']);
238
239      $template->assign_block_vars(
240        'categories.category',
241        array(
242          'SRC'   => $thumbnail_src_of[$category['representative_picture_id']],
243          'ALT'   => $category['name'],
244          'TITLE' => $lang['hint_category'],
245          'ICON'  => $icon_ts,
246
247          'URL'   => make_index_url(
248            array(
249              'category' => $category
250              )
251            ),
252          'CAPTION_NB_IMAGES' => get_display_images_count
253                                  (
254                                    $category['nb_images'],
255                                    $category['count_images'],
256                                    $category['count_categories'],
257                                    true,
258                                    '<br />'
259                                  ),
260          'DESCRIPTION' => @$comment,
261          'NAME'  => $name,
262          )
263        );
264
265      if ($conf['display_fromto'])
266      {
267        if (isset($dates_of_category[ $category['id'] ]))
268        {
269          $from = $dates_of_category[ $category['id'] ]['from'];
270          $to   = $dates_of_category[ $category['id'] ]['to'];
271       
272          if (!empty($from))
273          {
274            $info = '';
275         
276            if ($from == $to)
277            {
278              $info = format_date($from);
279            }
280            else
281            {
282              $info = sprintf(
283                l10n('from %s to %s'),
284                format_date($from),
285                format_date($to)
286                );
287            }
288
289            $template->assign_block_vars(
290              'categories.category.dates',
291              array(
292                'INFO' => $info,
293                )
294              );
295          }
296        }
297      }
298
299      //plugins need to add/modify sth in this loop ?
300      trigger_action('loc_index_category_thumbnail',
301        $category, 'categories.category' );
302    }
303
304    $template->assign_var_from_handle('CATEGORIES', 'mainpage_categories');
305  }
306  else
307  {
308    $template->set_filename( 'thumbnails', 'thumbnails.tpl');
309    // first line
310    $template->assign_block_vars('thumbnails.line', array());
311    // current row displayed
312    $row_number = 0;
313
314    if ($page['section']=='recent_cats')
315    {
316      $old_level_separator = $conf['level_separator'];
317      $conf['level_separator'] = '<br />';
318    }
319
320    foreach ($categories as $category)
321    {
322      $template->assign_block_vars(
323        'thumbnails.line.thumbnail',
324        array(
325          'IMAGE'       => $thumbnail_src_of[ $category['representative_picture_id'] ],
326          'IMAGE_ALT'   => $category['name'],
327          'IMAGE_TITLE' => get_display_images_count
328                                  (
329                                    $category['nb_images'],
330                                    $category['count_images'],
331                                    $category['count_categories'],
332                                    true,
333                                    ' / '
334                                  ),
335
336          'U_IMG_LINK'  => make_index_url(
337            array(
338              'category' => $category
339              )
340            ),
341          'CLASS'       => 'thumbCat',
342          )
343        );
344      if ($page['section']=='recent_cats')
345      {
346        $name = get_cat_display_name_cache($category['uppercats'], null, false);
347      }
348      else
349      {
350        $name = $category['name'];
351        $template->merge_block_vars(
352          'thumbnails.line.thumbnail',
353          array(
354            'IMAGE_TS'    => get_icon($category['max_date_last'], $category['is_child_date_last']),
355           )
356         );
357      }
358      $template->assign_block_vars(
359        'thumbnails.line.thumbnail.category_name',
360        array(
361          'NAME' => $name
362          )
363        );
364
365      //plugins need to add/modify sth in this loop ?
366      trigger_action('loc_index_category_thumbnail',
367        $category, 'thumbnails.line.thumbnail' );
368
369      // create a new line ?
370      if (++$row_number == $user['nb_image_line'])
371      {
372        $template->assign_block_vars('thumbnails.line', array());
373        $row_number = 0;
374      }
375    }
376
377    if ( isset($old_level_separator) )
378    {
379      $conf['level_separator']=$old_level_separator;
380    }
381
382    $template->assign_var_from_handle('CATEGORIES', 'thumbnails');
383    unset( $template->_tpldata['thumbnails.'] );//maybe write a func for that
384  }
385  trigger_action('loc_end_index_category_thumbnails', $categories);
386}
387?>
Note: See TracBrowser for help on using the repository browser.