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

Last change on this file since 2325 was 2325, checked in by rub, 16 years ago

Resolved issue 0000823: Enhance upload functionalities

First commit, others will be follow.
Not hesitate to change my translations.

Add upload configuration tabsheet (move and add configuration)
Change and add define for access level
Can show upload link every time
Can restrict access upload.class.php
Can choice category on upload page
Add upload class not use for the moment
Review quickly and temporary style of upload.tpl

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      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 * Provides functions to handle categories.
26 *
27 *
28 */
29
30/**
31 * Is the category accessible to the connected user ?
32 *
33 * Note : if the user is not authorized to see this category, page creation
34 * ends (exit command in this function)
35 *
36 * @param int category id to verify
37 * @return void
38 */
39function check_restrictions($category_id)
40{
41  global $user;
42
43  // $filter['visible_categories'] and $filter['visible_images']
44  // are not used because it's not necessary (filter <> restriction)
45  if (in_array($category_id, explode(',', $user['forbidden_categories'])))
46  {
47    access_denied();
48  }
49}
50
51function get_categories_menu()
52{
53  global $page, $user, $filter;
54
55  $query = '
56SELECT ';
57  // From CATEGORIES_TABLE
58  $query.= '
59  id, name, permalink, nb_images, global_rank,';
60  // From USER_CACHE_CATEGORIES_TABLE
61  $query.= '
62  date_last, max_date_last, count_images, count_categories';
63
64  // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE
65  $query.= '
66FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
67  ON id = cat_id and user_id = '.$user['id'];
68
69  // Always expand when filter is activated
70  if (!$user['expand'] and !$filter['enabled'])
71  {
72    $where = '
73(id_uppercat is NULL';
74    if (isset($page['category']))
75    {
76      $where .= ' OR id_uppercat IN ('.$page['category']['uppercats'].')';
77    }
78    $where .= ')';
79  }
80  else
81  {
82    $where = '
83  '.get_sql_condition_FandF
84    (
85      array
86        (
87          'visible_categories' => 'id',
88        ),
89      null,
90      true
91    );
92  }
93
94  $where = trigger_event('get_categories_menu_sql_where',
95    $where, $user['expand'], $filter['enabled'] );
96
97  $query.= '
98WHERE '.$where.'
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, @$page['category'] );
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  $query = '
139SELECT *
140  FROM '.CATEGORIES_TABLE.'
141  WHERE id = '.$id.'
142;';
143  $cat = mysql_fetch_assoc(pwg_query($query));
144  if (empty($cat))
145    return null;
146
147  foreach ($cat as $k => $v)
148  {
149    // If the field is true or false, the variable is transformed into a
150    // boolean value.
151    if ($cat[$k] == 'true' or $cat[$k] == 'false')
152    {
153      $cat[$k] = get_boolean( $cat[$k] );
154    }
155  }
156
157  $upper_ids = explode(',', $cat['uppercats']);
158  if ( count($upper_ids)==1 )
159  {// no need to make a query for level 1
160    $cat['upper_names'] = array(
161        array(
162          'id' => $cat['id'],
163          'name' => $cat['name'],
164          'permalink' => $cat['permalink'],
165          )
166      );
167  }
168  else
169  {
170    $names = array();
171    $query = '
172  SELECT id, name, permalink
173    FROM '.CATEGORIES_TABLE.'
174    WHERE id IN ('.$cat['uppercats'].')
175  ;';
176    $names = hash_from_query($query, 'id');
177
178    // category names must be in the same order than uppercats list
179    $cat['upper_names'] = array();
180    foreach ($upper_ids as $cat_id)
181    {
182      array_push( $cat['upper_names'], $names[$cat_id]);
183    }
184  }
185  return $cat;
186}
187
188// get_complete_dir returns the concatenation of get_site_url and
189// get_local_dir
190// Example : "pets > rex > 1_year_old" is on the the same site as the
191// PhpWebGallery files and this category has 22 for identifier
192// get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/"
193function get_complete_dir( $category_id )
194{
195  return get_site_url($category_id).get_local_dir($category_id);
196}
197
198// get_local_dir returns an array with complete path without the site url
199// Example : "pets > rex > 1_year_old" is on the the same site as the
200// PhpWebGallery files and this category has 22 for identifier
201// get_local_dir(22) returns "pets/rex/1_year_old/"
202function get_local_dir( $category_id )
203{
204  global $page;
205
206  $uppercats = '';
207  $local_dir = '';
208
209  if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) )
210  {
211    $uppercats = $page['plain_structure'][$category_id]['uppercats'];
212  }
213  else
214  {
215    $query = 'SELECT uppercats';
216    $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id;
217    $query.= ';';
218    $row = mysql_fetch_array( pwg_query( $query ) );
219    $uppercats = $row['uppercats'];
220  }
221
222  $upper_array = explode( ',', $uppercats );
223
224  $database_dirs = array();
225  $query = 'SELECT id,dir';
226  $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')';
227  $query.= ';';
228  $result = pwg_query( $query );
229  while( $row = mysql_fetch_array( $result ) )
230  {
231    $database_dirs[$row['id']] = $row['dir'];
232  }
233  foreach ($upper_array as $id)
234  {
235    $local_dir.= $database_dirs[$id].'/';
236  }
237
238  return $local_dir;
239}
240
241// retrieving the site url : "http://domain.com/gallery/" or
242// simply "./galleries/"
243function get_site_url($category_id)
244{
245  global $page;
246
247  $query = '
248SELECT galleries_url
249  FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c
250  WHERE s.id = c.site_id
251    AND c.id = '.$category_id.'
252;';
253  $row = mysql_fetch_array(pwg_query($query));
254  return $row['galleries_url'];
255}
256
257// returns an array of image orders available for users/visitors
258function get_category_preferred_image_orders()
259{
260  global $conf;
261  return array(
262    array(l10n('default_sort'), '', true),
263    array(l10n('Average rate'), 'average_rate DESC', $conf['rate']),
264    array(l10n('most_visited_cat'), 'hit DESC', true),
265    array(l10n('Creation date'), 'date_creation DESC', true),
266    array(l10n('Post date'), 'date_available DESC', true),
267    array(l10n('File name'), 'file ASC', true)
268  );
269}
270
271function display_select_categories($categories,
272                                   $selecteds,
273                                   $blockname,
274                                   $fullname = true)
275{
276  global $template;
277
278  $tpl_cats = array();
279  foreach ($categories as $category)
280  {
281    if ($fullname)
282    {
283      $option = get_cat_display_name_cache($category['uppercats'],
284                                           null,
285                                           false);
286    }
287    else
288    {
289      $option = str_repeat('&nbsp;',
290                           (3 * substr_count($category['global_rank'], '.')));
291      $option.= '- '.$category['name'];
292    }
293    $tpl_cats[ $category['id'] ] = $option;
294  }
295
296  $template->assign( $blockname, $tpl_cats);
297  $template->assign( $blockname.'_selected', $selecteds);
298}
299
300function display_select_cat_wrapper($query, $selecteds, $blockname,
301                                    $fullname = true)
302{
303  $result = pwg_query($query);
304  $categories = array();
305  if (!empty($result))
306  {
307    while ($row = mysql_fetch_assoc($result))
308    {
309      array_push($categories, $row);
310    }
311  }
312  usort($categories, 'global_rank_compare');
313  display_select_categories($categories, $selecteds, $blockname, $fullname);
314}
315
316/**
317 * returns all subcategory identifiers of given category ids
318 *
319 * @param array ids
320 * @return array
321 */
322function get_subcat_ids($ids)
323{
324  $query = '
325SELECT DISTINCT(id)
326  FROM '.CATEGORIES_TABLE.'
327  WHERE ';
328  foreach ($ids as $num => $category_id)
329  {
330    is_numeric($category_id)
331      or trigger_error(
332        'get_subcat_ids expecting numeric, not '.gettype($category_id),
333        E_USER_WARNING
334      );
335    if ($num > 0)
336    {
337      $query.= '
338    OR ';
339    }
340    $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'';
341  }
342  $query.= '
343;';
344  $result = pwg_query($query);
345
346  $subcats = array();
347  while ($row = mysql_fetch_array($result))
348  {
349    array_push($subcats, $row['id']);
350  }
351  return $subcats;
352}
353
354/** finds a matching category id from a potential list of permalinks
355 * @param array permalinks example: holiday holiday/france holiday/france/paris
356 * @param int idx - output of the index in $permalinks that matches
357 * return category id or null if no match
358 */
359function get_cat_id_from_permalinks( $permalinks, &$idx )
360{
361  $in = '';
362  foreach($permalinks as $permalink)
363  {
364    if ( !empty($in) ) $in.=', ';
365    $in .= '"'.$permalink.'"';
366  }
367  $query ='
368SELECT c.id, op.permalink, 1 AS is_old
369  FROM '.OLD_PERMALINKS_TABLE.' op INNER JOIN '.CATEGORIES_TABLE.' c
370    ON op.cat_id=c.id
371  WHERE op.permalink IN ('.$in.')
372UNION
373SELECT id, permalink, 0 AS is_old
374  FROM '.CATEGORIES_TABLE.'
375  WHERE permalink IN ('.$in.')
376;';
377  $perma_hash = hash_from_query($query, 'permalink');
378
379  if ( empty($perma_hash) )
380    return null;
381  for ($i=count($permalinks)-1; $i>=0; $i--)
382  {
383    if ( isset( $perma_hash[ $permalinks[$i] ] ) )
384    {
385      $idx = $i;
386      $cat_id = $perma_hash[ $permalinks[$i] ]['id'];
387      if ($perma_hash[ $permalinks[$i] ]['is_old'])
388      {
389        $query='
390UPDATE '.OLD_PERMALINKS_TABLE.' SET last_hit=NOW(), hit=hit+1
391  WHERE permalink="'.$permalinks[$i].'" AND cat_id='.$cat_id.'
392  LIMIT 1';
393        pwg_query($query);
394      }
395      return $cat_id;
396    }
397  }
398  return null;
399}
400
401function global_rank_compare($a, $b)
402{
403  return strnatcasecmp($a['global_rank'], $b['global_rank']);
404}
405
406function rank_compare($a, $b)
407{
408  if ($a['rank'] == $b['rank'])
409  {
410    return 0;
411  }
412
413  return ($a['rank'] < $b['rank']) ? -1 : 1;
414}
415
416/**
417 * returns display text for information images of category
418 *
419 * @param array categories
420 * @return string
421 */
422function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_categories, $short_message = true, $Separator = '\n')
423{
424  $display_text = '';
425
426  if ($cat_count_images > 0)
427  {
428    if ($cat_nb_images > 0 and $cat_nb_images < $cat_count_images)
429    {
430      $display_text.= get_display_images_count($cat_nb_images, $cat_nb_images, 0, $short_message, $Separator).$Separator;
431      $cat_count_images-= $cat_nb_images;
432      $cat_nb_images = 0;
433    }
434
435    //at least one image direct or indirect
436    $display_text.= l10n_dec('%d element', '%d elements', $cat_count_images);
437
438    if ($cat_count_categories == 0 or $cat_nb_images == $cat_count_images)
439    {
440      //no descendant categories or descendants do not contain images
441      if (! $short_message)
442      {
443        $display_text.= ' '.l10n('images_available_cpl');
444      }
445    }
446    else
447    {
448      $display_text.= ' '.l10n_dec('images_available_cat', 'images_available_cats', $cat_count_categories);
449    }
450  }
451
452  return $display_text;
453}
454
455/**
456 * returns the link of upload menu
457 *
458 * @param null
459 * @return string or null
460 */
461function get_upload_menu_link()
462{
463  global $conf, $page, $user;
464
465  $show_link = false;
466  $arg_link = null;
467
468  if (is_autorize_status($conf['upload_user_access']))
469  {
470    if (isset($page['category']) and $page['category']['uploadable'] )
471    {
472      // upload a picture in the category
473      $show_link = true;
474      $arg_link = 'cat='.$page['category']['id'];
475    }
476    else
477    if ($conf['upload_link_everytime'])
478    {
479      // upload a picture in the category
480      $query = '
481SELECT
482  1
483FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
484  ON id = cat_id and user_id = '.$user['id'].'
485WHERE
486  uploadable = \'true\'
487  '.get_sql_condition_FandF
488    (
489      array
490        (
491          'visible_categories' => 'id',
492        ),
493      'AND'
494    ).'
495LIMIT 1';
496
497      $show_link = mysql_num_rows(pwg_query($query)) <> 0;
498    }
499  }
500  if ($show_link)
501  {
502    return get_root_url().'upload.php'.(empty($arg_link) ? '' : '?'.$arg_link);
503  }
504  else
505  {
506    return;
507  }
508}
509
510?>
Note: See TracBrowser for help on using the repository browser.