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

Last change on this file since 2327 was 2327, 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: 8.7 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    $query = '
92SELECT image_id
93  FROM '.CATEGORIES_TABLE.' AS c INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic
94    ON ic.category_id = c.id';
95    $query.= '
96  WHERE uppercats LIKE \''.$row['uppercats'].',%\''
97  .get_sql_condition_FandF
98  (
99    array
100      (
101        'forbidden_categories' => 'c.id',
102        'visible_categories' => 'c.id',
103        'visible_images' => 'image_id'
104      ),
105    "\n  AND"
106  ).'
107  ORDER BY RAND()
108  LIMIT 0,1
109;';
110    $subresult = pwg_query($query);
111    if (mysql_num_rows($subresult) > 0)
112    {
113      list($image_id) = mysql_fetch_row($subresult);
114    }
115  }
116  else
117  { // searching a random representant among representant of sub-categories
118    $query = '
119SELECT representative_picture_id
120  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
121  ON id = cat_id and user_id = '.$user['id'].'
122  WHERE uppercats LIKE \''.$row['uppercats'].',%\'
123    AND representative_picture_id IS NOT NULL'
124  .get_sql_condition_FandF
125  (
126    array
127      (
128        'visible_categories' => 'id',
129      ),
130    "\n  AND"
131  ).'
132  ORDER BY RAND()
133  LIMIT 0,1
134;';
135    $subresult = pwg_query($query);
136    if (mysql_num_rows($subresult) > 0)
137    {
138      list($image_id) = mysql_fetch_row($subresult);
139    }
140  }
141
142  if (isset($image_id))
143  {
144    $row['representative_picture_id'] = $image_id;
145    array_push($image_ids, $image_id);
146    array_push($categories, $row);
147    array_push($category_ids, $row['id']);
148  }
149  unset($image_id);
150}
151
152if ($conf['display_fromto'])
153{
154  $dates_of_category = array();
155  if (count($category_ids) > 0)
156  {
157    $query = '
158SELECT
159    category_id,
160    MIN(date_creation) AS date_creation_min,
161    MAX(date_creation) AS date_creation_max
162  FROM '.IMAGE_CATEGORY_TABLE.'
163    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
164  WHERE category_id IN ('.implode(',', $category_ids).')
165'.get_sql_condition_FandF
166  (
167    array
168      (
169        'visible_categories' => 'category_id',
170        'visible_images' => 'id'
171      ),
172    'AND'
173  ).'
174  GROUP BY category_id
175;';
176    $result = pwg_query($query);
177    while ($row = mysql_fetch_array($result))
178    {
179      $dates_of_category[ $row['category_id'] ] = array(
180        'from' => $row['date_creation_min'],
181        'to'   => $row['date_creation_max'],
182        );
183    }
184  }
185}
186
187if ($page['section']=='recent_cats')
188{
189  usort($categories, 'global_rank_compare');
190}
191if (count($categories) > 0)
192{
193  $thumbnail_src_of = array();
194
195  $query = '
196SELECT id, path, tn_ext
197  FROM '.IMAGES_TABLE.'
198  WHERE id IN ('.implode(',', $image_ids).')
199;';
200  $result = pwg_query($query);
201  while ($row = mysql_fetch_assoc($result))
202  {
203    $thumbnail_src_of[$row['id']] = get_thumbnail_url($row);
204  }
205}
206
207if (count($categories) > 0)
208{
209  // Update filtered data
210  if (function_exists('update_cats_with_filtered_data'))
211  {
212    update_cats_with_filtered_data($categories);
213  }
214
215  $template->set_filename('index_category_thumbnails', 'mainpage_categories.tpl');
216
217  trigger_action('loc_begin_index_category_thumbnails', $categories);
218
219  foreach ($categories as $category)
220  {
221    if ($page['section']=='recent_cats')
222    {
223      $name = get_cat_display_name_cache($category['uppercats'], null, false);
224    }
225    else
226    {
227      $name = $category['name'];
228    }
229
230    $icon_ts = get_icon($category['max_date_last'], $category['is_child_date_last']);
231
232    $tpl_var =
233        array(
234          'ID'    => $category['id'],
235          'TN_SRC'   => $thumbnail_src_of[$category['representative_picture_id']],
236          'ALT'   => $category['name'],
237          'ICON'  => $icon_ts,
238
239          'URL'   => make_index_url(
240            array(
241              'category' => $category
242              )
243            ),
244          'CAPTION_NB_IMAGES' => get_display_images_count
245                                  (
246                                    $category['nb_images'],
247                                    $category['count_images'],
248                                    $category['count_categories'],
249                                    true,
250                                    '<br />'
251                                  ),
252          'DESCRIPTION' =>
253            trigger_event('render_category_literal_description',
254              trigger_event('render_category_description',
255                @$category['comment'],
256                'subcatify_category_description')),
257          'NAME'  => $name,
258        );
259
260    if ($conf['display_fromto'])
261    {
262      if (isset($dates_of_category[ $category['id'] ]))
263      {
264        $from = $dates_of_category[ $category['id'] ]['from'];
265        $to   = $dates_of_category[ $category['id'] ]['to'];
266
267        if (!empty($from))
268        {
269          $info = '';
270
271          if ($from == $to)
272          {
273            $info = format_date($from);
274          }
275          else
276          {
277            $info = sprintf(
278              l10n('from %s to %s'),
279              format_date($from),
280              format_date($to)
281              );
282          }
283          $tpl_var['INFO_DATES'] = $info;
284        }
285      }
286    }//fromto
287
288    //plugins need to add/modify sth in this loop ?
289    $tpl_var = trigger_event('loc_index_category_thumbnail',
290                  $tpl_var, $category );
291
292    $template->append( 'category_thumbnails', $tpl_var);
293  }
294
295  trigger_action('loc_end_index_category_thumbnails', $categories);
296  $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails');
297}
298?>
Note: See TracBrowser for help on using the repository browser.