source: tags/build-A01/include/functions_category.inc.php @ 29641

Last change on this file since 29641 was 1677, checked in by rub, 17 years ago

Feature Issue ID 0000601: Filter all public pages with only recent elements

It's a finalized version.
Obsolete code of draft are removed.

You can filter categories and images with recent date period on your screen selection.
In the future, filter could be easy done on other type data (plugin?)

You can flat categories and sub-categories with a recent date period of your choice.

Next, perhaps, a panel to choice recent date for the 2 features.

On draft, there have problem with MySql 5, be careful!

Css problem not resolved:

  • Menu "Categories" is bad centered
  • Icon on dark too on the top
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 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-2006 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $Id: functions_category.inc.php 1677 2006-12-21 21:38:20Z rub $
9// | last update   : $Date: 2006-12-21 21:38:20 +0000 (Thu, 21 Dec 2006) $
10// | last modifier : $Author: rub $
11// | revision      : $Revision: 1677 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28/**
29 * Provides functions to handle categories.
30 *
31 *
32 */
33
34/**
35 * Is the category accessible to the connected user ?
36 *
37 * Note : if the user is not authorized to see this category, page creation
38 * ends (exit command in this function)
39 *
40 * @param int category id to verify
41 * @return void
42 */
43function check_restrictions($category_id)
44{
45  global $user;
46
47  // $filter['visible_categories'] and $filter['visible_images']
48  // are not used because it's not necessary (filter <> restriction)
49  if (in_array($category_id, explode(',', $user['forbidden_categories'])))
50  {
51    access_denied();
52  }
53}
54
55function get_categories_menu()
56{
57  global $page, $user, $filter;
58
59  $query = '
60SELECT ';
61  // From CATEGORIES_TABLE
62  $query.= '
63  name, id, nb_images, global_rank,';
64  // From USER_CACHE_CATEGORIES_TABLE
65  $query.= '
66  date_last, max_date_last, count_images, count_categories';
67
68  // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE
69  $query.= '
70FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
71  ON id = cat_id and user_id = '.$user['id'];
72
73  // Always expand when filter is activated
74  if (!$user['expand'] and !$filter['enabled'])
75  {
76    $query.= '
77WHERE
78(id_uppercat is NULL';
79    if (isset($page['category']))
80    {
81      $query.= ' OR id_uppercat IN ('.$page['uppercats'].')';
82    }
83    $query.= ')';
84  }
85  else
86  {
87    $query.= '
88  '.get_sql_condition_FandF
89    (
90      array
91        (
92          'visible_categories' => 'id',
93        ),
94      'WHERE'
95    );
96  }
97
98  $query.= '
99;';
100
101  $result = pwg_query($query);
102  $cats = array();
103  while ($row = mysql_fetch_assoc($result))
104  {
105    array_push($cats, $row);
106  }
107  usort($cats, 'global_rank_compare');
108
109  // Update filtered data
110  update_cats_with_filtered_data($cats);
111
112  return get_html_menu_category($cats);
113}
114
115
116/**
117 * Retrieve informations about a category in the database
118 *
119 * Returns an array with following keys :
120 *
121 *  - comment
122 *  - dir : directory, might be empty for virtual categories
123 *  - name : an array with indexes from 0 (lowest cat name) to n (most
124 *           uppercat name findable)
125 *  - nb_images
126 *  - id_uppercat
127 *  - site_id
128 *  -
129 *
130 * @param int category id
131 * @return array
132 */
133function get_cat_info( $id )
134{
135  $infos = array('nb_images','id_uppercat','comment','site_id'
136                 ,'dir','date_last','uploadable','status','visible'
137                 ,'representative_picture_id','uppercats','commentable'
138                 ,'image_order');
139
140  $query = '
141SELECT '.implode(',', $infos).'
142  FROM '.CATEGORIES_TABLE.'
143  WHERE id = '.$id.'
144;';
145  $row = mysql_fetch_array(pwg_query($query));
146  if (empty($row))
147    return null;
148
149  $cat = array();
150  foreach ($infos as $info)
151  {
152    if (isset($row[$info]))
153    {
154      $cat[$info] = $row[$info];
155    }
156    else
157    {
158      $cat[$info] = '';
159    }
160    // If the field is true or false, the variable is transformed into a
161    // boolean value.
162    if ($cat[$info] == 'true' or $cat[$info] == 'false')
163    {
164      $cat[$info] = get_boolean( $cat[$info] );
165    }
166  }
167  global $conf;
168  if ( !( $conf['allow_html_descriptions'] and
169          preg_match('/<(div|br|img|script).*>/i', $cat['comment']) ) )
170  {
171    $cat['comment'] = nl2br($cat['comment']);
172  }
173
174  $names = array();
175  $query = '
176SELECT name,id
177  FROM '.CATEGORIES_TABLE.'
178  WHERE id IN ('.$cat['uppercats'].')
179;';
180  $result = pwg_query($query);
181  while($row = mysql_fetch_array($result))
182  {
183    $names[$row['id']] = $row['name'];
184  }
185
186  // category names must be in the same order than uppercats list
187  $cat['name'] = array();
188  foreach (explode(',', $cat['uppercats']) as $cat_id)
189  {
190    $cat['name'][$cat_id] = $names[$cat_id];
191  }
192
193  return $cat;
194}
195
196// get_complete_dir returns the concatenation of get_site_url and
197// get_local_dir
198// Example : "pets > rex > 1_year_old" is on the the same site as the
199// PhpWebGallery files and this category has 22 for identifier
200// get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/"
201function get_complete_dir( $category_id )
202{
203  return get_site_url($category_id).get_local_dir($category_id);
204}
205
206// get_local_dir returns an array with complete path without the site url
207// Example : "pets > rex > 1_year_old" is on the the same site as the
208// PhpWebGallery files and this category has 22 for identifier
209// get_local_dir(22) returns "pets/rex/1_year_old/"
210function get_local_dir( $category_id )
211{
212  global $page;
213
214  $uppercats = '';
215  $local_dir = '';
216
217  if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) )
218  {
219    $uppercats = $page['plain_structure'][$category_id]['uppercats'];
220  }
221  else
222  {
223    $query = 'SELECT uppercats';
224    $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id;
225    $query.= ';';
226    $row = mysql_fetch_array( pwg_query( $query ) );
227    $uppercats = $row['uppercats'];
228  }
229
230  $upper_array = explode( ',', $uppercats );
231
232  $database_dirs = array();
233  $query = 'SELECT id,dir';
234  $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')';
235  $query.= ';';
236  $result = pwg_query( $query );
237  while( $row = mysql_fetch_array( $result ) )
238  {
239    $database_dirs[$row['id']] = $row['dir'];
240  }
241  foreach ($upper_array as $id)
242  {
243    $local_dir.= $database_dirs[$id].'/';
244  }
245
246  return $local_dir;
247}
248
249// retrieving the site url : "http://domain.com/gallery/" or
250// simply "./galleries/"
251function get_site_url($category_id)
252{
253  global $page;
254
255  $query = '
256SELECT galleries_url
257  FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c
258  WHERE s.id = c.site_id
259    AND c.id = '.$category_id.'
260;';
261  $row = mysql_fetch_array(pwg_query($query));
262  return $row['galleries_url'];
263}
264
265// returns an array of image orders available for users/visitors
266function get_category_preferred_image_orders()
267{
268  global $conf;
269  return array(
270    array(l10n('default_sort'), '', true),
271    array(l10n('Average rate'), 'average_rate DESC', $conf['rate']),
272    array(l10n('most_visited_cat'), 'hit DESC', true),
273    array(l10n('Creation date'), 'date_creation DESC', true),
274    array(l10n('Post date'), 'date_available DESC', true),
275    array(l10n('File name'), 'file ASC', true)
276  );
277}
278
279function display_select_categories($categories,
280                                   $selecteds,
281                                   $blockname,
282                                   $fullname = true)
283{
284  global $template;
285
286  foreach ($categories as $category)
287  {
288    $selected = '';
289    if (in_array($category['id'], $selecteds))
290    {
291      $selected = ' selected="selected"';
292    }
293
294    if ($fullname)
295    {
296      $option = get_cat_display_name_cache($category['uppercats'],
297                                           null,
298                                           false);
299    }
300    else
301    {
302      $option = str_repeat('&nbsp;',
303                           (3 * substr_count($category['global_rank'], '.')));
304      $option.= '- '.$category['name'];
305    }
306
307    $template->assign_block_vars(
308      $blockname,
309      array('SELECTED'=>$selected,
310            'VALUE'=>$category['id'],
311            'OPTION'=>$option
312        ));
313  }
314}
315
316function display_select_cat_wrapper($query, $selecteds, $blockname,
317                                    $fullname = true)
318{
319  $result = pwg_query($query);
320  $categories = array();
321  if (!empty($result))
322  {
323    while ($row = mysql_fetch_array($result))
324    {
325      array_push($categories, $row);
326    }
327  }
328  usort($categories, 'global_rank_compare');
329  display_select_categories($categories, $selecteds, $blockname, $fullname);
330}
331
332/**
333 * returns all subcategory identifiers of given category ids
334 *
335 * @param array ids
336 * @return array
337 */
338function get_subcat_ids($ids)
339{
340  $query = '
341SELECT DISTINCT(id)
342  FROM '.CATEGORIES_TABLE.'
343  WHERE ';
344  foreach ($ids as $num => $category_id)
345  {
346    if ($num > 0)
347    {
348      $query.= '
349    OR ';
350    }
351    $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'';
352  }
353  $query.= '
354;';
355  $result = pwg_query($query);
356
357  $subcats = array();
358  while ($row = mysql_fetch_array($result))
359  {
360    array_push($subcats, $row['id']);
361  }
362  return $subcats;
363}
364
365function global_rank_compare($a, $b)
366{
367  return strnatcasecmp($a['global_rank'], $b['global_rank']);
368}
369
370function rank_compare($a, $b)
371{
372  if ($a['rank'] == $b['rank'])
373  {
374    return 0;
375  }
376
377  return ($a['rank'] < $b['rank']) ? -1 : 1;
378}
379
380/**
381 * returns display text for information images of category
382 *
383 * @param array categories
384 * @return string
385 */
386function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_categories, $short_message = true)
387{
388  $display_text = '';
389
390  // Count of category is main
391  // if not picture on categorie, test on sub-categories
392  $count = ($cat_nb_images > 0 ? $cat_nb_images : $cat_count_images);
393
394  if ($count > 0)
395  {
396    $display_text.= l10n_dec('image_available', 'images_available', $count);
397
398    if ($cat_nb_images > 0)
399    {
400      if (! $short_message)
401      {
402        $display_text.= ' '.l10n('images_available_cpl');
403      }
404    }
405    else
406    {
407      $display_text.= ' '.l10n_dec('images_available_cat', 'images_available_cats', $cat_count_categories);
408    }
409  }
410
411  return $display_text;
412}
413
414?>
Note: See TracBrowser for help on using the repository browser.