source: trunk/include/functions_category.inc.php @ 1851

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

o Proposition: improved display of 'x images in y sub-categories' or 'x images in this category' for cases when categories contain both images and sub-categories
o Good idea of this new way for way confguest_access, but I kept last implementation for access methods (Could be useful on future development)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 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 1851 2007-02-22 20:20:30Z rub $
9// | last update   : $Date: 2007-02-22 20:20:30 +0000 (Thu, 22 Feb 2007) $
10// | last modifier : $Author: rub $
11// | revision      : $Revision: 1851 $
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  if (function_exists('update_cats_with_filtered_data'))
111  {
112    update_cats_with_filtered_data($cats);
113  }
114
115  return get_html_menu_category($cats);
116}
117
118
119/**
120 * Retrieve informations about a category in the database
121 *
122 * Returns an array with following keys :
123 *
124 *  - comment
125 *  - dir : directory, might be empty for virtual categories
126 *  - name : an array with indexes from 0 (lowest cat name) to n (most
127 *           uppercat name findable)
128 *  - nb_images
129 *  - id_uppercat
130 *  - site_id
131 *  -
132 *
133 * @param int category id
134 * @return array
135 */
136function get_cat_info( $id )
137{
138  $infos = array('nb_images','id_uppercat','comment','site_id'
139                 ,'dir','date_last','uploadable','status','visible'
140                 ,'representative_picture_id','uppercats','commentable'
141                 ,'image_order');
142
143  $query = '
144SELECT '.implode(',', $infos).'
145  FROM '.CATEGORIES_TABLE.'
146  WHERE id = '.$id.'
147;';
148  $row = mysql_fetch_array(pwg_query($query));
149  if (empty($row))
150    return null;
151
152  $cat = array();
153  foreach ($infos as $info)
154  {
155    if (isset($row[$info]))
156    {
157      $cat[$info] = $row[$info];
158    }
159    else
160    {
161      $cat[$info] = '';
162    }
163    // If the field is true or false, the variable is transformed into a
164    // boolean value.
165    if ($cat[$info] == 'true' or $cat[$info] == 'false')
166    {
167      $cat[$info] = get_boolean( $cat[$info] );
168    }
169  }
170  global $conf;
171  if ( !( $conf['allow_html_descriptions'] and
172          preg_match('/<(div|br|img|script).*>/i', $cat['comment']) ) )
173  {
174    $cat['comment'] = nl2br($cat['comment']);
175  }
176
177  $names = array();
178  $query = '
179SELECT name,id
180  FROM '.CATEGORIES_TABLE.'
181  WHERE id IN ('.$cat['uppercats'].')
182;';
183  $result = pwg_query($query);
184  while($row = mysql_fetch_array($result))
185  {
186    $names[$row['id']] = $row['name'];
187  }
188
189  // category names must be in the same order than uppercats list
190  $cat['name'] = array();
191  foreach (explode(',', $cat['uppercats']) as $cat_id)
192  {
193    $cat['name'][$cat_id] = $names[$cat_id];
194  }
195
196  return $cat;
197}
198
199// get_complete_dir returns the concatenation of get_site_url and
200// get_local_dir
201// Example : "pets > rex > 1_year_old" is on the the same site as the
202// PhpWebGallery files and this category has 22 for identifier
203// get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/"
204function get_complete_dir( $category_id )
205{
206  return get_site_url($category_id).get_local_dir($category_id);
207}
208
209// get_local_dir returns an array with complete path without the site url
210// Example : "pets > rex > 1_year_old" is on the the same site as the
211// PhpWebGallery files and this category has 22 for identifier
212// get_local_dir(22) returns "pets/rex/1_year_old/"
213function get_local_dir( $category_id )
214{
215  global $page;
216
217  $uppercats = '';
218  $local_dir = '';
219
220  if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) )
221  {
222    $uppercats = $page['plain_structure'][$category_id]['uppercats'];
223  }
224  else
225  {
226    $query = 'SELECT uppercats';
227    $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id;
228    $query.= ';';
229    $row = mysql_fetch_array( pwg_query( $query ) );
230    $uppercats = $row['uppercats'];
231  }
232
233  $upper_array = explode( ',', $uppercats );
234
235  $database_dirs = array();
236  $query = 'SELECT id,dir';
237  $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')';
238  $query.= ';';
239  $result = pwg_query( $query );
240  while( $row = mysql_fetch_array( $result ) )
241  {
242    $database_dirs[$row['id']] = $row['dir'];
243  }
244  foreach ($upper_array as $id)
245  {
246    $local_dir.= $database_dirs[$id].'/';
247  }
248
249  return $local_dir;
250}
251
252// retrieving the site url : "http://domain.com/gallery/" or
253// simply "./galleries/"
254function get_site_url($category_id)
255{
256  global $page;
257
258  $query = '
259SELECT galleries_url
260  FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c
261  WHERE s.id = c.site_id
262    AND c.id = '.$category_id.'
263;';
264  $row = mysql_fetch_array(pwg_query($query));
265  return $row['galleries_url'];
266}
267
268// returns an array of image orders available for users/visitors
269function get_category_preferred_image_orders()
270{
271  global $conf;
272  return array(
273    array(l10n('default_sort'), '', true),
274    array(l10n('Average rate'), 'average_rate DESC', $conf['rate']),
275    array(l10n('most_visited_cat'), 'hit DESC', true),
276    array(l10n('Creation date'), 'date_creation DESC', true),
277    array(l10n('Post date'), 'date_available DESC', true),
278    array(l10n('File name'), 'file ASC', true)
279  );
280}
281
282function display_select_categories($categories,
283                                   $selecteds,
284                                   $blockname,
285                                   $fullname = true)
286{
287  global $template;
288
289  foreach ($categories as $category)
290  {
291    $selected = '';
292    if (in_array($category['id'], $selecteds))
293    {
294      $selected = ' selected="selected"';
295    }
296
297    if ($fullname)
298    {
299      $option = get_cat_display_name_cache($category['uppercats'],
300                                           null,
301                                           false);
302    }
303    else
304    {
305      $option = str_repeat('&nbsp;',
306                           (3 * substr_count($category['global_rank'], '.')));
307      $option.= '- '.$category['name'];
308    }
309
310    $template->assign_block_vars(
311      $blockname,
312      array('SELECTED'=>$selected,
313            'VALUE'=>$category['id'],
314            'OPTION'=>$option
315        ));
316  }
317}
318
319function display_select_cat_wrapper($query, $selecteds, $blockname,
320                                    $fullname = true)
321{
322  $result = pwg_query($query);
323  $categories = array();
324  if (!empty($result))
325  {
326    while ($row = mysql_fetch_array($result))
327    {
328      array_push($categories, $row);
329    }
330  }
331  usort($categories, 'global_rank_compare');
332  display_select_categories($categories, $selecteds, $blockname, $fullname);
333}
334
335/**
336 * returns all subcategory identifiers of given category ids
337 *
338 * @param array ids
339 * @return array
340 */
341function get_subcat_ids($ids)
342{
343  $query = '
344SELECT DISTINCT(id)
345  FROM '.CATEGORIES_TABLE.'
346  WHERE ';
347  foreach ($ids as $num => $category_id)
348  {
349    if ($num > 0)
350    {
351      $query.= '
352    OR ';
353    }
354    $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'';
355  }
356  $query.= '
357;';
358  $result = pwg_query($query);
359
360  $subcats = array();
361  while ($row = mysql_fetch_array($result))
362  {
363    array_push($subcats, $row['id']);
364  }
365  return $subcats;
366}
367
368function global_rank_compare($a, $b)
369{
370  return strnatcasecmp($a['global_rank'], $b['global_rank']);
371}
372
373function rank_compare($a, $b)
374{
375  if ($a['rank'] == $b['rank'])
376  {
377    return 0;
378  }
379
380  return ($a['rank'] < $b['rank']) ? -1 : 1;
381}
382
383/**
384 * returns display text for information images of category
385 *
386 * @param array categories
387 * @return string
388 */
389function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_categories, $short_message = true, $Separator = '\n')
390{
391  $display_text = '';
392
393  if ($cat_count_images > 0)
394  {
395    if ($cat_nb_images > 0 and $cat_nb_images < $cat_count_images)
396    {
397      $display_text.= get_display_images_count($cat_nb_images, $cat_nb_images, 0, $short_message, $Separator).$Separator;
398      $cat_count_images-= $cat_nb_images;
399      $cat_nb_images = 0;
400    }
401   
402    //at least one image direct or indirect
403    $display_text.= l10n_dec('image_available', 'images_available', $cat_count_images);
404
405    if ($cat_count_categories == 0 or $cat_nb_images == $cat_count_images)
406    {
407      //no descendant categories or descendants do not contain images
408      if (! $short_message)
409      {
410        $display_text.= ' '.l10n('images_available_cpl');
411      }
412    }
413    else
414    {
415      $display_text.= ' '.l10n_dec('images_available_cat', 'images_available_cats', $cat_count_categories);
416    }
417  }
418
419  return $display_text;
420}
421
422?>
Note: See TracBrowser for help on using the repository browser.