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

Last change on this file since 2326 was 2326, checked in by rvelices, 16 years ago

just some optimizations (especially for large dbs)

  • replace some REGEXP with LIKE in sql
  • optimized queries for the combination of large data sets with picture_url_style file
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 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 2326 2008-05-03 01:51:50Z rvelices $
8// | last update   : $Date: 2008-05-03 01:51:50 +0000 (Sat, 03 May 2008) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2326 $
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 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      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' => l10n('hint_category'),
205          'ICON'  => $icon_ts,
206
207          'URL'   => make_index_url(
208            array(
209              'category' => $category
210              )
211            ),
212          'CAPTION_NB_IMAGES' => get_display_images_count
213                                  (
214                                    $category['nb_images'],
215                                    $category['count_images'],
216                                    $category['count_categories'],
217                                    true,
218                                    '<br />'
219                                  ),
220          'DESCRIPTION' =>
221            trigger_event('render_category_literal_description',
222              trigger_event('render_category_description',
223                @$category['comment'],
224                'subcatify_category_description')),
225          'NAME'  => $name,
226          )
227        );
228
229      //plugins need to add/modify sth in this loop ?
230      trigger_action('loc_index_category_thumbnail',
231        $category, 'categories.category' );
232    }
233
234    $template->assign_var_from_handle('CATEGORIES', 'mainpage_categories');
235  }
236  else
237  {
238    $template->set_filename( 'thumbnails', 'thumbnails.tpl');
239    // first line
240    $template->assign_block_vars('thumbnails.line', array());
241    // current row displayed
242    $row_number = 0;
243
244    if ($page['section']=='recent_cats')
245    {
246      $old_level_separator = $conf['level_separator'];
247      $conf['level_separator'] = '<br />';
248    }
249
250    foreach ($categories as $category)
251    {
252      $template->assign_block_vars(
253        'thumbnails.line.thumbnail',
254        array(
255          'IMAGE'       => $thumbnail_src_of[ $category['representative_picture_id'] ],
256          'IMAGE_ALT'   => $category['name'],
257          'IMAGE_TITLE' => get_display_images_count
258                                  (
259                                    $category['nb_images'],
260                                    $category['count_images'],
261                                    $category['count_categories'],
262                                    true,
263                                    ' / '
264                                  ),
265
266          'U_IMG_LINK'  => make_index_url(
267            array(
268              'category' => $category
269              )
270            ),
271          'CLASS'       => 'thumbCat',
272          )
273        );
274      if ($page['section']=='recent_cats')
275      {
276        $name = get_cat_display_name_cache($category['uppercats'], null, false);
277      }
278      else
279      {
280        $name = $category['name'];
281        $template->merge_block_vars(
282          'thumbnails.line.thumbnail',
283          array(
284            'IMAGE_TS'    => get_icon($category['max_date_last'], $category['is_child_date_last']),
285           )
286         );
287      }
288      $template->assign_block_vars(
289        'thumbnails.line.thumbnail.category_name',
290        array(
291          'NAME' => $name
292          )
293        );
294
295      //plugins need to add/modify sth in this loop ?
296      trigger_action('loc_index_category_thumbnail',
297        $category, 'thumbnails.line.thumbnail' );
298
299      // create a new line ?
300      if (++$row_number == $user['nb_image_line'])
301      {
302        $template->assign_block_vars('thumbnails.line', array());
303        $row_number = 0;
304      }
305    }
306
307    if ( isset($old_level_separator) )
308    {
309      $conf['level_separator']=$old_level_separator;
310    }
311
312    $template->assign_var_from_handle('CATEGORIES', 'thumbnails');
313    $template->delete_block_vars('thumbnails', true); // category_default reuse them
314  }
315  trigger_action('loc_end_index_category_thumbnails', $categories);
316}
317?>
Note: See TracBrowser for help on using the repository browser.