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

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

Fix bug on my revision 1851.

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