source: trunk/admin/cat_modify.php @ 6988

Last change on this file since 6988 was 6988, checked in by rvelices, 14 years ago

feature 1616: rename "category" into "album" (3rd commit; still to come)

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