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

Last change on this file since 4486 was 4367, checked in by nikrou, 15 years ago

Feature 1255: modification in sql queries

  • manage random function
  • manage regex syntax
  • manage quote (single instead of double)
  • manage interval
  • Property svn:eol-style set to LF
File size: 8.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 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  c.*, nb_images, date_last, max_date_last, count_images, count_categories
36  FROM '.CATEGORIES_TABLE.' c INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
37  ON id = cat_id and user_id = '.$user['id'].'
38  WHERE date_last >= '.pwg_db_get_recent_period_expression($user['recent_period']).'
39'.get_sql_condition_FandF
40  (
41    array
42      (
43        'visible_categories' => 'id',
44      ),
45    'AND'
46  ).'
47;';
48}
49else
50{
51  // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE
52  $query = '
53SELECT
54  c.*, nb_images, date_last, max_date_last, count_images, count_categories
55  FROM '.CATEGORIES_TABLE.' c INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
56  ON id = cat_id and user_id = '.$user['id'].'
57  WHERE id_uppercat '.
58  (!isset($page['category']) ? 'is NULL' : '= '.$page['category']['id']).'
59'.get_sql_condition_FandF
60  (
61    array
62      (
63        'visible_categories' => 'id',
64      ),
65    'AND'
66  ).'
67  ORDER BY rank
68;';
69}
70
71$result = pwg_query($query);
72$categories = array();
73$category_ids = array();
74$image_ids = array();
75
76while ($row = pwg_db_fetch_assoc($result))
77{
78  $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last'];
79
80  if (isset($row['representative_picture_id'])
81      and is_numeric($row['representative_picture_id']))
82  { // if a representative picture is set, it has priority
83    $image_id = $row['representative_picture_id'];
84  }
85  else if ($conf['allow_random_representative'])
86  {// searching a random representant among elements in sub-categories
87    if ($row['count_images']>0)
88    {
89      $query = '
90SELECT image_id
91  FROM '.CATEGORIES_TABLE.' AS c INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic
92    ON ic.category_id = c.id';
93      $query.= '
94  WHERE (c.id='.$row['id'].' OR uppercats LIKE \''.$row['uppercats'].',%\')'
95    .get_sql_condition_FandF
96    (
97      array
98        (
99          'forbidden_categories' => 'c.id',
100          'visible_categories' => 'c.id',
101          'visible_images' => 'image_id'
102        ),
103      "\n  AND"
104    ).'
105  ORDER BY '.DB_RANDOM_FUNCTION.'()
106  LIMIT 1
107;';
108      $subresult = pwg_query($query);
109      if (pwg_db_num_rows($subresult) > 0)
110      {
111        list($image_id) = pwg_db_fetch_row($subresult);
112      }
113    }
114  }
115  else
116  { // searching a random representant among representant of sub-categories
117    if ($row['count_categories']>0 and $row['count_images']>0)
118    {
119      $query = '
120  SELECT representative_picture_id
121    FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
122    ON id = cat_id and user_id = '.$user['id'].'
123    WHERE uppercats LIKE \''.$row['uppercats'].',%\'
124      AND representative_picture_id IS NOT NULL'
125    .get_sql_condition_FandF
126    (
127      array
128        (
129          'visible_categories' => 'id',
130        ),
131      "\n  AND"
132    ).'
133    ORDER BY '.DB_RANDOM_FUNCTION.'()
134    LIMIT 1
135  ;';
136      $subresult = pwg_query($query);
137      if (pwg_db_num_rows($subresult) > 0)
138      {
139        list($image_id) = pwg_db_fetch_row($subresult);
140      }
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    array_push($category_ids, $row['id']);
150  }
151  unset($image_id);
152}
153
154if ($conf['display_fromto'])
155{
156  $dates_of_category = array();
157  if (count($category_ids) > 0)
158  {
159    $query = '
160SELECT
161    category_id,
162    MIN(date_creation) AS date_creation_min,
163    MAX(date_creation) AS date_creation_max
164  FROM '.IMAGE_CATEGORY_TABLE.'
165    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
166  WHERE category_id IN ('.implode(',', $category_ids).')
167'.get_sql_condition_FandF
168  (
169    array
170      (
171        'visible_categories' => 'category_id',
172        'visible_images' => 'id'
173      ),
174    'AND'
175  ).'
176  GROUP BY category_id
177;';
178    $result = pwg_query($query);
179    while ($row = pwg_db_fetch_assoc($result))
180    {
181      $dates_of_category[ $row['category_id'] ] = array(
182        'from' => $row['date_creation_min'],
183        'to'   => $row['date_creation_max'],
184        );
185    }
186  }
187}
188
189if ($page['section']=='recent_cats')
190{
191  usort($categories, 'global_rank_compare');
192}
193if (count($categories) > 0)
194{
195  $thumbnail_src_of = array();
196
197  $query = '
198SELECT id, path, tn_ext
199  FROM '.IMAGES_TABLE.'
200  WHERE id IN ('.implode(',', $image_ids).')
201;';
202  $result = pwg_query($query);
203  while ($row = pwg_db_fetch_assoc($result))
204  {
205    $thumbnail_src_of[$row['id']] = get_thumbnail_url($row);
206  }
207}
208
209if (count($categories) > 0)
210{
211  // Update filtered data
212  if (function_exists('update_cats_with_filtered_data'))
213  {
214    update_cats_with_filtered_data($categories);
215  }
216
217  $template->set_filename('index_category_thumbnails', 'mainpage_categories.tpl');
218
219  trigger_action('loc_begin_index_category_thumbnails', $categories);
220
221  $tpl_thumbnails_var = array();
222
223  foreach ($categories as $category)
224  {
225    $category['name'] = trigger_event(
226        'render_category_name',
227        $category['name'],
228        'subcatify_category_name'
229        );
230
231    if ($page['section']=='recent_cats')
232    {
233      $name = get_cat_display_name_cache($category['uppercats'], null, false);
234    }
235    else
236    {
237      $name = $category['name'];
238    }
239
240    $tpl_var =
241        array(
242          'ID'    => $category['id'],
243          'TN_SRC'   => $thumbnail_src_of[$category['representative_picture_id']],
244          'TN_ALT'   => strip_tags($category['name']),
245          'icon_ts'  => get_icon($category['max_date_last'], $category['is_child_date_last']),
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' =>
261            trigger_event('render_category_literal_description',
262              trigger_event('render_category_description',
263                @$category['comment'],
264                'subcatify_category_description')),
265          'NAME'  => $name,
266        );
267
268    if ($conf['display_fromto'])
269    {
270      if (isset($dates_of_category[ $category['id'] ]))
271      {
272        $from = $dates_of_category[ $category['id'] ]['from'];
273        $to   = $dates_of_category[ $category['id'] ]['to'];
274
275        if (!empty($from))
276        {
277          $info = '';
278
279          if ($from == $to)
280          {
281            $info = format_date($from);
282          }
283          else
284          {
285            $info = sprintf(
286              l10n('from %s to %s'),
287              format_date($from),
288              format_date($to)
289              );
290          }
291          $tpl_var['INFO_DATES'] = $info;
292        }
293      }
294    }//fromto
295
296    $tpl_thumbnails_var[] = $tpl_var;
297  }
298
299  $tpl_thumbnails_var = trigger_event('loc_end_index_category_thumbnails', $tpl_thumbnails_var, $categories);
300  $template->assign( 'category_thumbnails', $tpl_thumbnails_var);
301
302  $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails');
303}
304pwg_debug('end include/category_cats.inc.php');
305?>
Note: See TracBrowser for help on using the repository browser.