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

Last change on this file since 2424 was 2424, checked in by rvelices, 16 years ago
  • feature 832: Folders with no images sql optimization
  • recent pics are sorted by date descending, and only then by conforder_by
  • updated prototype.js to version 1.6
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 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    if ($page['section']=='recent_cats')
228    {
229      $name = get_cat_display_name_cache($category['uppercats'], null, false);
230    }
231    else
232    {
233      $name = $category['name'];
234    }
235
236    $icon_ts = get_icon($category['max_date_last'], $category['is_child_date_last']);
237
238    $tpl_var =
239        array(
240          'ID'    => $category['id'],
241          'TN_SRC'   => $thumbnail_src_of[$category['representative_picture_id']],
242          'ALT'   => $category['name'],
243          'ICON'  => $icon_ts,
244
245          'URL'   => make_index_url(
246            array(
247              'category' => $category
248              )
249            ),
250          'CAPTION_NB_IMAGES' => get_display_images_count
251                                  (
252                                    $category['nb_images'],
253                                    $category['count_images'],
254                                    $category['count_categories'],
255                                    true,
256                                    '<br />'
257                                  ),
258          'DESCRIPTION' =>
259            trigger_event('render_category_literal_description',
260              trigger_event('render_category_description',
261                @$category['comment'],
262                'subcatify_category_description')),
263          'NAME'  => $name,
264        );
265
266    if ($conf['display_fromto'])
267    {
268      if (isset($dates_of_category[ $category['id'] ]))
269      {
270        $from = $dates_of_category[ $category['id'] ]['from'];
271        $to   = $dates_of_category[ $category['id'] ]['to'];
272
273        if (!empty($from))
274        {
275          $info = '';
276
277          if ($from == $to)
278          {
279            $info = format_date($from);
280          }
281          else
282          {
283            $info = sprintf(
284              l10n('from %s to %s'),
285              format_date($from),
286              format_date($to)
287              );
288          }
289          $tpl_var['INFO_DATES'] = $info;
290        }
291      }
292    }//fromto
293
294    //plugins need to add/modify sth in this loop ?
295    $tpl_var = trigger_event('loc_index_category_thumbnail',
296                  $tpl_var, $category );
297
298    $template->append( 'category_thumbnails', $tpl_var);
299  }
300
301  trigger_action('loc_end_index_category_thumbnails', $categories);
302  $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails');
303}
304?>
Note: See TracBrowser for help on using the repository browser.