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

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

Apply property svn:eol-style Value: LF

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 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 1900 2007-03-12 22:33:53Z rub $
8// | last update   : $Date: 2007-03-12 22:33:53 +0000 (Mon, 12 Mar 2007) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 1900 $
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  id, name, permalink, 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['category']['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, @$page['category'] );
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  $query = '
138SELECT *
139  FROM '.CATEGORIES_TABLE.'
140  WHERE id = '.$id.'
141;';
142  $cat = mysql_fetch_assoc(pwg_query($query));
143  if (empty($cat))
144    return null;
145
146  foreach ($cat as $k => $v)
147  {
148    // If the field is true or false, the variable is transformed into a
149    // boolean value.
150    if ($cat[$k] == 'true' or $cat[$k] == 'false')
151    {
152      $cat[$k] = get_boolean( $cat[$k] );
153    }
154  }
155  global $conf;
156  if ( !( $conf['allow_html_descriptions'] and
157          preg_match('/<(div|br|img|script).*>/i', $cat['comment']) ) )
158  {
159    $cat['comment'] = nl2br(@$cat['comment']);
160  }
161
162  $upper_ids = explode(',', $cat['uppercats']);
163  if ( count($upper_ids)==1 )
164  {// no need to make a query for level 1
165    $cat['upper_names'] = array(
166        array(
167          'id' => $cat['id'],
168          'name' => $cat['name'],
169          'permalink' => $cat['permalink'],
170          )
171      );
172  }
173  else
174  {
175    $names = array();
176    $query = '
177  SELECT id, name, permalink
178    FROM '.CATEGORIES_TABLE.'
179    WHERE id IN ('.$cat['uppercats'].')
180  ;';
181    $names = hash_from_query($query, 'id');
182
183    // category names must be in the same order than uppercats list
184    $cat['upper_names'] = array();
185    foreach ($upper_ids as $cat_id)
186    {
187      array_push( $cat['upper_names'], $names[$cat_id]);
188    }
189  }
190  return $cat;
191}
192
193// get_complete_dir returns the concatenation of get_site_url and
194// get_local_dir
195// Example : "pets > rex > 1_year_old" is on the the same site as the
196// PhpWebGallery files and this category has 22 for identifier
197// get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/"
198function get_complete_dir( $category_id )
199{
200  return get_site_url($category_id).get_local_dir($category_id);
201}
202
203// get_local_dir returns an array with complete path without the site url
204// Example : "pets > rex > 1_year_old" is on the the same site as the
205// PhpWebGallery files and this category has 22 for identifier
206// get_local_dir(22) returns "pets/rex/1_year_old/"
207function get_local_dir( $category_id )
208{
209  global $page;
210
211  $uppercats = '';
212  $local_dir = '';
213
214  if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) )
215  {
216    $uppercats = $page['plain_structure'][$category_id]['uppercats'];
217  }
218  else
219  {
220    $query = 'SELECT uppercats';
221    $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id;
222    $query.= ';';
223    $row = mysql_fetch_array( pwg_query( $query ) );
224    $uppercats = $row['uppercats'];
225  }
226
227  $upper_array = explode( ',', $uppercats );
228
229  $database_dirs = array();
230  $query = 'SELECT id,dir';
231  $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')';
232  $query.= ';';
233  $result = pwg_query( $query );
234  while( $row = mysql_fetch_array( $result ) )
235  {
236    $database_dirs[$row['id']] = $row['dir'];
237  }
238  foreach ($upper_array as $id)
239  {
240    $local_dir.= $database_dirs[$id].'/';
241  }
242
243  return $local_dir;
244}
245
246// retrieving the site url : "http://domain.com/gallery/" or
247// simply "./galleries/"
248function get_site_url($category_id)
249{
250  global $page;
251
252  $query = '
253SELECT galleries_url
254  FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c
255  WHERE s.id = c.site_id
256    AND c.id = '.$category_id.'
257;';
258  $row = mysql_fetch_array(pwg_query($query));
259  return $row['galleries_url'];
260}
261
262// returns an array of image orders available for users/visitors
263function get_category_preferred_image_orders()
264{
265  global $conf;
266  return array(
267    array(l10n('default_sort'), '', true),
268    array(l10n('Average rate'), 'average_rate DESC', $conf['rate']),
269    array(l10n('most_visited_cat'), 'hit DESC', true),
270    array(l10n('Creation date'), 'date_creation DESC', true),
271    array(l10n('Post date'), 'date_available DESC', true),
272    array(l10n('File name'), 'file ASC', true)
273  );
274}
275
276function display_select_categories($categories,
277                                   $selecteds,
278                                   $blockname,
279                                   $fullname = true)
280{
281  global $template;
282
283  foreach ($categories as $category)
284  {
285    $selected = '';
286    if (in_array($category['id'], $selecteds))
287    {
288      $selected = ' selected="selected"';
289    }
290
291    if ($fullname)
292    {
293      $option = get_cat_display_name_cache($category['uppercats'],
294                                           null,
295                                           false);
296    }
297    else
298    {
299      $option = str_repeat('&nbsp;',
300                           (3 * substr_count($category['global_rank'], '.')));
301      $option.= '- '.$category['name'];
302    }
303
304    $template->assign_block_vars(
305      $blockname,
306      array('SELECTED'=>$selected,
307            'VALUE'=>$category['id'],
308            'OPTION'=>$option
309        ));
310  }
311}
312
313function display_select_cat_wrapper($query, $selecteds, $blockname,
314                                    $fullname = true)
315{
316  $result = pwg_query($query);
317  $categories = array();
318  if (!empty($result))
319  {
320    while ($row = mysql_fetch_assoc($result))
321    {
322      array_push($categories, $row);
323    }
324  }
325  usort($categories, 'global_rank_compare');
326  display_select_categories($categories, $selecteds, $blockname, $fullname);
327}
328
329/**
330 * returns all subcategory identifiers of given category ids
331 *
332 * @param array ids
333 * @return array
334 */
335function get_subcat_ids($ids)
336{
337  $query = '
338SELECT DISTINCT(id)
339  FROM '.CATEGORIES_TABLE.'
340  WHERE ';
341  foreach ($ids as $num => $category_id)
342  {
343    is_numeric($category_id)
344      or trigger_error(
345        'get_subcat_ids expecting numeric, not '.gettype($category_id),
346        E_USER_WARNING
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
367/** returns a category id that corresponds to the given permalink (or null)
368 * @param string permalink
369 */
370function get_cat_id_from_permalink( $permalink )
371{
372  $query ='
373SELECT id FROM '.CATEGORIES_TABLE.'
374  WHERE permalink="'.$permalink.'"';
375  $ids = array_from_query($query, 'id');
376  if (!empty($ids))
377  {
378    return $ids[0];
379  }
380  return null;
381}
382
383/** returns a category id that has used before this permalink (or null)
384 * @param string permalink
385 * @param boolean is_hit if true update the usage counters on the old permalinks
386 */
387function get_cat_id_from_old_permalink($permalink, $is_hit)
388{
389  $query='
390SELECT c.id
391  FROM '.OLD_PERMALINKS_TABLE.' op INNER JOIN '.CATEGORIES_TABLE.' c
392    ON op.cat_id=c.id
393  WHERE op.permalink="'.$permalink.'"
394  LIMIT 1';
395  $result = pwg_query($query);
396  $cat_id = null;
397  if ( mysql_num_rows($result) )
398    list( $cat_id ) = mysql_fetch_array($result);
399
400  if ( isset($cat_id) and $is_hit )
401  {
402    $query='
403UPDATE '.OLD_PERMALINKS_TABLE.' SET last_hit=NOW(), hit=hit+1
404  WHERE permalink="'.$permalink.'" AND cat_id='.$cat_id.'
405  LIMIT 1';
406    pwg_query($query);
407  }
408  return $cat_id;
409}
410
411function global_rank_compare($a, $b)
412{
413  return strnatcasecmp($a['global_rank'], $b['global_rank']);
414}
415
416function rank_compare($a, $b)
417{
418  if ($a['rank'] == $b['rank'])
419  {
420    return 0;
421  }
422
423  return ($a['rank'] < $b['rank']) ? -1 : 1;
424}
425
426/**
427 * returns display text for information images of category
428 *
429 * @param array categories
430 * @return string
431 */
432function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_categories, $short_message = true, $Separator = '\n')
433{
434  $display_text = '';
435
436  if ($cat_count_images > 0)
437  {
438    if ($cat_nb_images > 0 and $cat_nb_images < $cat_count_images)
439    {
440      $display_text.= get_display_images_count($cat_nb_images, $cat_nb_images, 0, $short_message, $Separator).$Separator;
441      $cat_count_images-= $cat_nb_images;
442      $cat_nb_images = 0;
443    }
444   
445    //at least one image direct or indirect
446    $display_text.= l10n_dec('image_available', 'images_available', $cat_count_images);
447
448    if ($cat_count_categories == 0 or $cat_nb_images == $cat_count_images)
449    {
450      //no descendant categories or descendants do not contain images
451      if (! $short_message)
452      {
453        $display_text.= ' '.l10n('images_available_cpl');
454      }
455    }
456    else
457    {
458      $display_text.= ' '.l10n_dec('images_available_cat', 'images_available_cats', $cat_count_categories);
459    }
460  }
461
462  return $display_text;
463}
464
465?>
Note: See TracBrowser for help on using the repository browser.