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

Last change on this file since 1970 was 1970, checked in by plg, 17 years ago

Feature 660 added: creation date of tthe category images can be displayed
next to category title in subcatify mode. The feature was implemented in a
very simple way that must be improved in display, performance and
recursivity.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 11.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 1970 2007-04-22 13:11:56Z plg $
8// | last update   : $Date: 2007-04-22 13:11:56 +0000 (Sun, 22 Apr 2007) $
9// | last modifier : $Author: plg $
10// | revision      : $Revision: 1970 $
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 '.IMAGES_TABLE.' ON image_id = id
167  WHERE category_id IN ('.implode(',', $category_ids).')
168  GROUP BY category_id
169;';
170    $result = pwg_query($query);
171    while ($row = mysql_fetch_array($result))
172    {
173      $dates_of_category[ $row['category_id'] ] = array(
174        'from' => $row['date_creation_min'],
175        'to'   => $row['date_creation_max'],
176        );
177    }
178    // echo '<pre>'; print_r($dates_of_category); echo '</pre>';
179  }
180}
181
182if ($page['section']=='recent_cats')
183{
184  usort($categories, 'global_rank_compare');
185}
186if (count($categories) > 0)
187{
188  $thumbnail_src_of = array();
189
190  $query = '
191SELECT id, path, tn_ext
192  FROM '.IMAGES_TABLE.'
193  WHERE id IN ('.implode(',', $image_ids).')
194;';
195  $result = pwg_query($query);
196  while ($row = mysql_fetch_assoc($result))
197  {
198    $thumbnail_src_of[$row['id']] = get_thumbnail_url($row);
199  }
200}
201
202if (count($categories) > 0)
203{
204  // Update filtered data
205  if (function_exists('update_cats_with_filtered_data'))
206  {
207    update_cats_with_filtered_data($categories);
208  }
209  trigger_action('loc_begin_index_category_thumbnails', $categories);
210  if ($conf['subcatify'])
211  {
212    $template->set_filename('mainpage_categories', 'mainpage_categories.tpl');
213
214    foreach ($categories as $category)
215    {
216      $comment = strip_tags(@$category['comment'], '<a><br><p><b><i><small><strong><font>');
217      if ($page['section']=='recent_cats')
218      {
219        $name = get_cat_display_name_cache($category['uppercats'], null, false);
220      }
221      else
222      {
223        $name = $category['name'];
224      }
225
226      $icon_ts = get_icon($category['max_date_last'], $category['is_child_date_last']);
227
228      $template->assign_block_vars(
229        'categories.category',
230        array(
231          'SRC'   => $thumbnail_src_of[$category['representative_picture_id']],
232          'ALT'   => $category['name'],
233          'TITLE' => $lang['hint_category'],
234          'ICON'  => $icon_ts,
235
236          'URL'   => make_index_url(
237            array(
238              'category' => $category
239              )
240            ),
241          'CAPTION_NB_IMAGES' => get_display_images_count
242                                  (
243                                    $category['nb_images'],
244                                    $category['count_images'],
245                                    $category['count_categories'],
246                                    true,
247                                    '<br />'
248                                  ),
249          'DESCRIPTION' => @$comment,
250          'NAME'  => $name,
251          )
252        );
253
254      if ($conf['display_fromto'])
255      {
256        if (isset($dates_of_category[ $category['id'] ]))
257        {
258          $from = $dates_of_category[ $category['id'] ]['from'];
259          $to   = $dates_of_category[ $category['id'] ]['to'];
260       
261          if (!empty($from))
262          {
263            $info = '';
264         
265            if ($from == $to)
266            {
267              $info = format_date($from);
268            }
269            else
270            {
271              $info = sprintf(
272                l10n('from %s to %s'),
273                format_date($from),
274                format_date($to)
275                );
276            }
277
278            $template->assign_block_vars(
279              'categories.category.dates',
280              array(
281                'INFO' => $info,
282                )
283              );
284          }
285        }
286      }
287
288      //plugins need to add/modify sth in this loop ?
289      trigger_action('loc_index_category_thumbnail',
290        $category, 'categories.category' );
291    }
292
293    $template->assign_var_from_handle('CATEGORIES', 'mainpage_categories');
294  }
295  else
296  {
297    $template->set_filename( 'thumbnails', 'thumbnails.tpl');
298    // first line
299    $template->assign_block_vars('thumbnails.line', array());
300    // current row displayed
301    $row_number = 0;
302
303    if ($page['section']=='recent_cats')
304    {
305      $old_level_separator = $conf['level_separator'];
306      $conf['level_separator'] = '<br />';
307    }
308
309    foreach ($categories as $category)
310    {
311      $template->assign_block_vars(
312        'thumbnails.line.thumbnail',
313        array(
314          'IMAGE'       => $thumbnail_src_of[ $category['representative_picture_id'] ],
315          'IMAGE_ALT'   => $category['name'],
316          'IMAGE_TITLE' => get_display_images_count
317                                  (
318                                    $category['nb_images'],
319                                    $category['count_images'],
320                                    $category['count_categories'],
321                                    true,
322                                    ' / '
323                                  ),
324
325          'U_IMG_LINK'  => make_index_url(
326            array(
327              'category' => $category
328              )
329            ),
330          'CLASS'       => 'thumbCat',
331          )
332        );
333      if ($page['section']=='recent_cats')
334      {
335        $name = get_cat_display_name_cache($category['uppercats'], null, false);
336      }
337      else
338      {
339        $name = $category['name'];
340        $template->merge_block_vars(
341          'thumbnails.line.thumbnail',
342          array(
343            'IMAGE_TS'    => get_icon($category['max_date_last'], $category['is_child_date_last']),
344           )
345         );
346      }
347      $template->assign_block_vars(
348        'thumbnails.line.thumbnail.category_name',
349        array(
350          'NAME' => $name
351          )
352        );
353
354      //plugins need to add/modify sth in this loop ?
355      trigger_action('loc_index_category_thumbnail',
356        $category, 'thumbnails.line.thumbnail' );
357
358      // create a new line ?
359      if (++$row_number == $user['nb_image_line'])
360      {
361        $template->assign_block_vars('thumbnails.line', array());
362        $row_number = 0;
363      }
364    }
365
366    if ( isset($old_level_separator) )
367    {
368      $conf['level_separator']=$old_level_separator;
369    }
370
371    $template->assign_var_from_handle('CATEGORIES', 'thumbnails');
372    unset( $template->_tpldata['thumbnails.'] );//maybe write a func for that
373  }
374  trigger_action('loc_end_index_category_thumbnails', $categories);
375}
376?>
Note: See TracBrowser for help on using the repository browser.