source: trunk/admin/cat_modify.php @ 2299

Last change on this file since 2299 was 2299, checked in by plg, 16 years ago

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 14.4 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
24if (!defined('PHPWG_ROOT_PATH'))
25{
26  die('Hacking attempt!');
27}
28
29include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
30include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
31
32// +-----------------------------------------------------------------------+
33// | Check Access and exit when user status is not ok                      |
34// +-----------------------------------------------------------------------+
35check_status(ACCESS_ADMINISTRATOR);
36
37//---------------------------------------------------------------- verification
38if ( !isset( $_GET['cat_id'] ) || !is_numeric( $_GET['cat_id'] ) )
39{
40  $_GET['cat_id'] = '-1';
41}
42
43//--------------------------------------------------------- form criteria check
44if (isset($_POST['submit']))
45{
46  $data =
47    array(
48      'id' => $_GET['cat_id'],
49      'name' => @$_POST['name'],
50      'commentable' => $_POST['commentable'],
51      'uploadable' =>
52        isset($_POST['uploadable']) ? $_POST['uploadable'] : 'false',
53      'comment' =>
54        $conf['allow_html_descriptions'] ?
55          @$_POST['comment'] : strip_tags(@$_POST['comment'])
56      );
57
58  mass_updates(
59    CATEGORIES_TABLE,
60    array(
61      'primary' => array('id'),
62      'update' => array_diff(array_keys($data), array('id'))
63      ),
64    array($data)
65    );
66
67  set_cat_visible(array($_GET['cat_id']), $_POST['visible']);
68  set_cat_status(array($_GET['cat_id']), $_POST['status']);
69
70  if (isset($_POST['parent']))
71  {
72    move_categories(
73      array($_GET['cat_id']),
74      $_POST['parent']
75      );
76  }
77
78  $image_order = '';
79  if ( !isset($_POST['image_order_default']) )
80  {
81    for ($i=1; $i<=3; $i++)
82    {
83      if ( !empty($_POST['order_field_'.$i]) )
84      {
85        if (! empty($image_order) )
86        {
87          $image_order .= ',';
88        }
89        $image_order .= $_POST['order_field_'.$i];
90        if ($_POST['order_direction_'.$i]=='DESC')
91        {
92          $image_order .= ' DESC';
93        }
94      }
95    }
96  }
97  $image_order = empty($image_order) ? 'null' : "'$image_order'";
98  $query = '
99UPDATE '.CATEGORIES_TABLE.' SET image_order='.$image_order.'
100WHERE ';
101  if (isset($_POST['image_order_subcats']))
102  {
103    $query .= 'uppercats REGEXP \'(^|,)'.$_GET['cat_id'].'(,|$)\'';
104  }
105  else
106  {
107    $query .= 'id='.$_GET['cat_id'].';';
108  }
109  pwg_query($query);
110
111  array_push($page['infos'], l10n('editcat_confirm'));
112}
113else if (isset($_POST['set_random_representant']))
114{
115  set_random_representant(array($_GET['cat_id']));
116}
117else if (isset($_POST['delete_representant']))
118{
119  $query = '
120UPDATE '.CATEGORIES_TABLE.'
121  SET representative_picture_id = NULL
122  WHERE id = '.$_GET['cat_id'].'
123;';
124  pwg_query($query);
125}
126else if (isset($_POST['submitAdd']))
127{
128  $output_create = create_virtual_category(
129    $_POST['virtual_name'],
130    (0 == $_POST['parent'] ? null : $_POST['parent'])
131    );
132
133  if (isset($output_create['error']))
134  {
135    array_push($page['errors'], $output_create['error']);
136  }
137  else
138  {
139    // Virtual category creation succeeded
140    //
141    // Add the information in the information list
142    array_push($page['infos'], $output_create['info']);
143
144    // Link the new category to the current category
145    associate_categories_to_categories(
146      array($_GET['cat_id']),
147      array($output_create['id'])
148      );
149
150    // information
151    array_push(
152      $page['infos'],
153      sprintf(
154        l10n('Category elements associated to the following categories: %s'),
155        '<ul><li>'
156        .get_cat_display_name_from_id($output_create['id'])
157        .'</li></ul>'
158        )
159      );
160  }
161}
162else if (isset($_POST['submitDestinations'])
163         and isset($_POST['destinations'])
164         and count($_POST['destinations']) > 0)
165{
166  associate_categories_to_categories(
167    array($_GET['cat_id']),
168    $_POST['destinations']
169    );
170
171  $category_names = array();
172  foreach ($_POST['destinations'] as $category_id)
173  {
174    array_push(
175      $category_names,
176      get_cat_display_name_from_id($category_id)
177      );
178  }
179
180  array_push(
181    $page['infos'],
182    sprintf(
183      l10n('Category elements associated to the following categories: %s'),
184      '<ul><li>'.implode('</li><li>', $category_names).'</li></ul>'
185      )
186    );
187}
188
189$query = '
190SELECT *
191  FROM '.CATEGORIES_TABLE.'
192  WHERE id = '.$_GET['cat_id'].'
193;';
194$category = mysql_fetch_array( pwg_query( $query ) );
195// nullable fields
196foreach (array('comment','dir','site_id', 'id_uppercat') as $nullable)
197{
198  if (!isset($category[$nullable]))
199  {
200    $category[$nullable] = '';
201  }
202}
203
204$category['is_virtual'] = empty($category['dir']) ? true : false;
205
206
207// Navigation path
208$navigation = get_cat_display_name_cache(
209  $category['uppercats'],
210  get_root_url().'admin.php?page=cat_modify&amp;cat_id='
211  );
212
213$form_action = get_root_url().'admin.php?page=cat_modify&amp;cat_id='.$_GET['cat_id'];
214
215//----------------------------------------------------- template initialization
216$template->set_filename( 'categories', 'admin/cat_modify.tpl');
217
218$base_url = get_root_url().'admin.php?page=';
219$cat_list_url = $base_url.'cat_list';
220
221$self_url = $cat_list_url;
222if (!empty($category['id_uppercat']))
223{
224  $self_url.= '&amp;parent_id='.$category['id_uppercat'];
225}
226
227$template->assign(
228  array(
229    'CATEGORIES_NAV'     => $navigation,
230    'CAT_NAME'           => @htmlspecialchars($category['name']),
231    'CAT_COMMENT'        => @htmlspecialchars($category['comment']),
232
233    'status_values'     => array('public','private'),
234
235    'CAT_STATUS'        => $category['status'],
236    'CAT_VISIBLE'       => $category['visible'],
237    'CAT_COMMENTABLE'   => $category['commentable'],
238    'CAT_UPLOADABLE'    => $category['uploadable'],
239
240    'IMG_ORDER_DEFAULT'  => empty($category['image_order']) ?
241                              'checked="checked"' : '',
242
243    'U_JUMPTO' => make_index_url(
244      array(
245        'category' => $category
246        )
247      ),
248
249    'MAIL_CONTENT' => empty($_POST['mail_content'])
250        ? '' : stripslashes($_POST['mail_content']),
251    'U_CHILDREN' => $cat_list_url.'&amp;parent_id='.$category['id'],
252    'U_HELP' => get_root_url().'popuphelp.php?page=cat_modify',
253
254    'F_ACTION' => $form_action,
255    )
256  );
257
258
259if ('private' == $category['status'])
260{
261  $template->assign( 'U_MANAGE_PERMISSIONS',
262      $base_url.'cat_perm&amp;cat='.$category['id']
263    );
264}
265
266// manage category elements link
267if ($category['nb_images'] > 0)
268{
269  $template->assign( 'U_MANAGE_ELEMENTS',
270        $base_url.'element_set&amp;cat='.$category['id']
271    );
272}
273
274if ($category['is_virtual'])
275{
276  $template->assign(
277    array(
278      'U_DELETE' => $self_url.'&amp;delete='.$category['id'],
279      )
280    );
281}
282else
283{
284  $category['cat_full_dir'] = get_complete_dir($_GET['cat_id']);
285  $template->assign(
286    array(
287      'CAT_FULL_DIR'       => preg_replace('/\/$/',
288                                    '',
289                                    $category['cat_full_dir'] )
290      )
291    );
292  if (!url_is_remote($category['cat_full_dir']) )
293  {
294    $template->assign('SHOW_UPLOADABLE', true);
295  }
296}
297
298// image order management
299
300$sort_fields = array(
301  '' => '',
302  'date_creation' => l10n('Creation date'),
303  'date_available' => l10n('Post date'),
304  'average_rate' => l10n('Average rate'),
305  'hit' => l10n('most_visited_cat'),
306  'file' => l10n('File name'),
307  'id' => 'Id',
308  );
309
310$sort_directions = array(
311  'ASC' => l10n('ascending'),
312  'DESC' => l10n('descending'),
313  );
314
315$template->assign( 'image_order_field_options', $sort_fields);
316$template->assign( 'image_order_direction_options', $sort_directions);
317
318$matches = array();
319if ( !empty( $category['image_order'] ) )
320{
321  preg_match_all('/([a-z_]+) *(?:(asc|desc)(?:ending)?)? *(?:, *|$)/i',
322    $category['image_order'], $matches);
323}
324
325for ($i=0; $i<3; $i++) // 3 fields
326{
327  $tpl_image_order_select = array(
328      'ID' => $i+1,
329      'FIELD' => array(''),
330      'DIRECTION' => array('ASC'),
331    );
332 
333  if ( isset($matches[1][$i]) )
334  {
335    $tpl_image_order_select['FIELD'] = array($matches[1][$i]);
336  }
337 
338  if (isset($matches[2][$i]) and strcasecmp($matches[2][$i],'DESC')==0)
339  {
340    $tpl_image_order_select['DIRECTION'] = array('DESC');
341  }
342  $template->append( 'image_orders', $tpl_image_order_select);
343}
344
345
346// representant management
347if ($category['nb_images'] > 0
348    or !empty($category['representative_picture_id']))
349{
350  $tpl_representant = array();
351
352  // picture to display : the identified representant or the generic random
353  // representant ?
354  if (!empty($category['representative_picture_id']))
355  {
356    $query = '
357SELECT id,tn_ext,path
358  FROM '.IMAGES_TABLE.'
359  WHERE id = '.$category['representative_picture_id'].'
360;';
361    $row = mysql_fetch_array(pwg_query($query));
362    $src = get_thumbnail_url($row);
363    $url = get_root_url().'admin.php?page=picture_modify';
364    $url.= '&amp;image_id='.$category['representative_picture_id'];
365
366    $tpl_representant['picture'] =
367      array(
368        'SRC' => $src,
369        'URL' => $url
370      );
371  }
372
373  // can the admin choose to set a new random representant ?
374  $tpl_representant['ALLOW_SET_RANDOM'] = ($category['nb_images']>0) ? true : false;
375
376  // can the admin delete the current representant ?
377  if (
378    ($category['nb_images'] > 0
379     and $conf['allow_random_representative'])
380    or
381    ($category['nb_images'] == 0
382     and !empty($category['representative_picture_id'])))
383  {
384    $tpl_representant['ALLOW_DELETE'] = true;
385  }
386  $template->assign('representant', $tpl_representant);
387}
388
389if ($category['is_virtual'])
390{
391  // the category can be moved in any category but in itself, in any
392  // sub-category
393  $unmovables = get_subcat_ids(array($category['id']));
394
395  $query = '
396SELECT id,name,uppercats,global_rank
397  FROM '.CATEGORIES_TABLE.'
398  WHERE id NOT IN ('.implode(',', $unmovables).')
399;';
400
401  display_select_cat_wrapper(
402    $query,
403    empty($category['id_uppercat']) ? array() : array($category['id_uppercat']),
404    'move_cat_options'
405    );
406}
407
408
409// create virtual in parent and link
410$query = '
411SELECT id,name,uppercats,global_rank
412  FROM '.CATEGORIES_TABLE.'
413;';
414display_select_cat_wrapper(
415  $query,
416  array(),
417  'create_new_parent_options'
418  );
419
420
421// destination categories
422$query = '
423SELECT id,name,uppercats,global_rank
424  FROM '.CATEGORIES_TABLE.'
425  WHERE id != '.$category['id'].'
426;';
427display_select_cat_wrapper(
428  $query,
429  array(),
430  'category_destination_options'
431  );
432
433// info by email to an access granted group of category informations
434if (isset($_POST['submitEmail']) and !empty($_POST['group']))
435{
436  set_make_full_url();
437
438  /* TODO: if $category['representative_picture_id']
439    is empty find child representative_picture_id */
440  if (!empty($category['representative_picture_id']))
441  {
442    $query = '
443SELECT id, file, path, tn_ext
444  FROM '.IMAGES_TABLE.'
445  WHERE id = '.$category['representative_picture_id'].'
446;';
447
448    $result = pwg_query($query);
449    if (mysql_num_rows($result) > 0)
450    {
451      $element = mysql_fetch_assoc($result);
452
453      $img_url  = '<a href="'.
454                      make_picture_url(array(
455                          'image_id' => $element['id'],
456                          'image_file' => $element['file'],
457                          'category' => $category
458                        ))
459                      .'"><img src="'.get_thumbnail_url($element).'"/></a>';
460    }
461  }
462 
463  if (!isset($img_url))
464  {
465    $img_url = '';
466  }
467
468  // TODO Mettre un array pour traduction subjet
469  pwg_mail_group(
470    $_POST['group'],
471    get_str_email_format(true), /* TODO add a checkbox in order to choose format*/
472    get_l10n_args('[%s] Come to visit the category %s',
473      array($conf['gallery_title'], $category['name'])),
474    'admin',
475    'cat_group_info',
476    array
477    (
478      'IMG_URL' => $img_url,
479      'CAT_NAME' => $category['name'],
480      'LINK' => make_index_url(
481          array(
482            'category' => array(
483              'id' => $category['id'],
484              'name' => $category['name'],
485              'permalink' => $category['permalink']
486              ))),
487      'CPL_CONTENT' => empty($_POST['mail_content'])
488                          ? '' : stripslashes($_POST['mail_content'])
489    ),
490    '' /* TODO Add listbox in order to choose Language selected */);
491
492  unset_make_full_url();
493
494  $query = '
495SELECT
496    name
497  FROM '.GROUPS_TABLE.'
498  WHERE id = '.$_POST['group'].'
499;';
500  list($group_name) = mysql_fetch_row(pwg_query($query));
501 
502  array_push(
503    $page['infos'],
504    sprintf(
505      l10n('An information email was sent to group "%s"'),
506      $group_name
507      )
508    );
509}
510
511if ('private' == $category['status'])
512{
513  $query = '
514SELECT
515    group_id
516  FROM '.GROUP_ACCESS_TABLE.'
517  WHERE cat_id = '.$category['id'].'
518;';
519}
520else
521{
522  $query = '
523SELECT
524    id AS group_id
525  FROM '.GROUPS_TABLE.'
526;';
527}
528$group_ids = array_from_query($query, 'group_id');
529
530if (count($group_ids) > 0)
531{
532  $query = '
533SELECT
534    id,
535    name
536  FROM '.GROUPS_TABLE.'
537  WHERE id IN ('.implode(',', $group_ids).')
538  ORDER BY name ASC
539;';
540  $template->assign('group_mail_options',
541      simple_hash_from_query($query, 'id', 'name')
542    );
543}
544
545//----------------------------------------------------------- sending html code
546$template->assign_var_from_handle('ADMIN_CONTENT', 'categories');
547?>
Note: See TracBrowser for help on using the repository browser.