source: trunk/admin/cat_modify.php @ 2408

Last change on this file since 2408 was 2324, checked in by rvelices, 16 years ago
  • move #categories.date_last and nb_images to #user_cache_categories
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 14.6 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$query = 'SELECT DISTINCT category_id
207  FROM '.IMAGE_CATEGORY_TABLE.'
208  WHERE category_id = '.$_GET['cat_id'].'
209  LIMIT 1';
210$result = pwg_query($query);
211$category['has_images'] = mysql_num_rows($result)>0 ? true : false;
212
213// Navigation path
214$navigation = get_cat_display_name_cache(
215  $category['uppercats'],
216  get_root_url().'admin.php?page=cat_modify&amp;cat_id='
217  );
218
219$form_action = get_root_url().'admin.php?page=cat_modify&amp;cat_id='.$_GET['cat_id'];
220
221//----------------------------------------------------- template initialization
222$template->set_filename( 'categories', 'admin/cat_modify.tpl');
223
224$base_url = get_root_url().'admin.php?page=';
225$cat_list_url = $base_url.'cat_list';
226
227$self_url = $cat_list_url;
228if (!empty($category['id_uppercat']))
229{
230  $self_url.= '&amp;parent_id='.$category['id_uppercat'];
231}
232
233$template->assign(
234  array(
235    'CATEGORIES_NAV'     => $navigation,
236    'CAT_NAME'           => @htmlspecialchars($category['name']),
237    'CAT_COMMENT'        => @htmlspecialchars($category['comment']),
238
239    'status_values'     => array('public','private'),
240
241    'CAT_STATUS'        => $category['status'],
242    'CAT_VISIBLE'       => $category['visible'],
243    'CAT_COMMENTABLE'   => $category['commentable'],
244    'CAT_UPLOADABLE'    => $category['uploadable'],
245
246    'IMG_ORDER_DEFAULT'  => empty($category['image_order']) ?
247                              'checked="checked"' : '',
248
249    'U_JUMPTO' => make_index_url(
250      array(
251        'category' => $category
252        )
253      ),
254
255    'MAIL_CONTENT' => empty($_POST['mail_content'])
256        ? '' : stripslashes($_POST['mail_content']),
257    'U_CHILDREN' => $cat_list_url.'&amp;parent_id='.$category['id'],
258    'U_HELP' => get_root_url().'popuphelp.php?page=cat_modify',
259
260    'F_ACTION' => $form_action,
261    )
262  );
263
264
265if ('private' == $category['status'])
266{
267  $template->assign( 'U_MANAGE_PERMISSIONS',
268      $base_url.'cat_perm&amp;cat='.$category['id']
269    );
270}
271
272// manage category elements link
273if ($category['has_images'])
274{
275  $template->assign( 'U_MANAGE_ELEMENTS',
276        $base_url.'element_set&amp;cat='.$category['id']
277    );
278}
279
280if ($category['is_virtual'])
281{
282  $template->assign(
283    array(
284      'U_DELETE' => $self_url.'&amp;delete='.$category['id'],
285      )
286    );
287}
288else
289{
290  $category['cat_full_dir'] = get_complete_dir($_GET['cat_id']);
291  $template->assign(
292    array(
293      'CAT_FULL_DIR'       => preg_replace('/\/$/',
294                                    '',
295                                    $category['cat_full_dir'] )
296      )
297    );
298  if (!url_is_remote($category['cat_full_dir']) )
299  {
300    $template->assign('SHOW_UPLOADABLE', true);
301  }
302}
303
304// image order management
305
306$sort_fields = array(
307  '' => '',
308  'date_creation' => l10n('Creation date'),
309  'date_available' => l10n('Post date'),
310  'average_rate' => l10n('Average rate'),
311  'hit' => l10n('most_visited_cat'),
312  'file' => l10n('File name'),
313  'id' => 'Id',
314  );
315
316$sort_directions = array(
317  'ASC' => l10n('ascending'),
318  'DESC' => l10n('descending'),
319  );
320
321$template->assign( 'image_order_field_options', $sort_fields);
322$template->assign( 'image_order_direction_options', $sort_directions);
323
324$matches = array();
325if ( !empty( $category['image_order'] ) )
326{
327  preg_match_all('/([a-z_]+) *(?:(asc|desc)(?:ending)?)? *(?:, *|$)/i',
328    $category['image_order'], $matches);
329}
330
331for ($i=0; $i<3; $i++) // 3 fields
332{
333  $tpl_image_order_select = array(
334      'ID' => $i+1,
335      'FIELD' => array(''),
336      'DIRECTION' => array('ASC'),
337    );
338 
339  if ( isset($matches[1][$i]) )
340  {
341    $tpl_image_order_select['FIELD'] = array($matches[1][$i]);
342  }
343 
344  if (isset($matches[2][$i]) and strcasecmp($matches[2][$i],'DESC')==0)
345  {
346    $tpl_image_order_select['DIRECTION'] = array('DESC');
347  }
348  $template->append( 'image_orders', $tpl_image_order_select);
349}
350
351
352// representant management
353if ($category['has_images']
354    or !empty($category['representative_picture_id']))
355{
356  $tpl_representant = array();
357
358  // picture to display : the identified representant or the generic random
359  // representant ?
360  if (!empty($category['representative_picture_id']))
361  {
362    $query = '
363SELECT id,tn_ext,path
364  FROM '.IMAGES_TABLE.'
365  WHERE id = '.$category['representative_picture_id'].'
366;';
367    $row = mysql_fetch_array(pwg_query($query));
368    $src = get_thumbnail_url($row);
369    $url = get_root_url().'admin.php?page=picture_modify';
370    $url.= '&amp;image_id='.$category['representative_picture_id'];
371
372    $tpl_representant['picture'] =
373      array(
374        'SRC' => $src,
375        'URL' => $url
376      );
377  }
378
379  // can the admin choose to set a new random representant ?
380  $tpl_representant['ALLOW_SET_RANDOM'] = ($category['has_images']) ? true : false;
381
382  // can the admin delete the current representant ?
383  if (
384    ($category['has_images']
385     and $conf['allow_random_representative'])
386    or
387    (!$category['has_images']
388     and !empty($category['representative_picture_id'])))
389  {
390    $tpl_representant['ALLOW_DELETE'] = true;
391  }
392  $template->assign('representant', $tpl_representant);
393}
394
395if ($category['is_virtual'])
396{
397  // the category can be moved in any category but in itself, in any
398  // sub-category
399  $unmovables = get_subcat_ids(array($category['id']));
400
401  $query = '
402SELECT id,name,uppercats,global_rank
403  FROM '.CATEGORIES_TABLE.'
404  WHERE id NOT IN ('.implode(',', $unmovables).')
405;';
406
407  display_select_cat_wrapper(
408    $query,
409    empty($category['id_uppercat']) ? array() : array($category['id_uppercat']),
410    'move_cat_options'
411    );
412}
413
414
415// create virtual in parent and link
416$query = '
417SELECT id,name,uppercats,global_rank
418  FROM '.CATEGORIES_TABLE.'
419;';
420display_select_cat_wrapper(
421  $query,
422  array(),
423  'create_new_parent_options'
424  );
425
426
427// destination categories
428$query = '
429SELECT id,name,uppercats,global_rank
430  FROM '.CATEGORIES_TABLE.'
431  WHERE id != '.$category['id'].'
432;';
433display_select_cat_wrapper(
434  $query,
435  array(),
436  'category_destination_options'
437  );
438
439// info by email to an access granted group of category informations
440if (isset($_POST['submitEmail']) and !empty($_POST['group']))
441{
442  set_make_full_url();
443
444  /* TODO: if $category['representative_picture_id']
445    is empty find child representative_picture_id */
446  if (!empty($category['representative_picture_id']))
447  {
448    $query = '
449SELECT id, file, path, tn_ext
450  FROM '.IMAGES_TABLE.'
451  WHERE id = '.$category['representative_picture_id'].'
452;';
453
454    $result = pwg_query($query);
455    if (mysql_num_rows($result) > 0)
456    {
457      $element = mysql_fetch_assoc($result);
458
459      $img_url  = '<a href="'.
460                      make_picture_url(array(
461                          'image_id' => $element['id'],
462                          'image_file' => $element['file'],
463                          'category' => $category
464                        ))
465                      .'"><img src="'.get_thumbnail_url($element).'"/></a>';
466    }
467  }
468 
469  if (!isset($img_url))
470  {
471    $img_url = '';
472  }
473
474  // TODO Mettre un array pour traduction subjet
475  pwg_mail_group(
476    $_POST['group'],
477    get_str_email_format(true), /* TODO add a checkbox in order to choose format*/
478    get_l10n_args('[%s] Come to visit the category %s',
479      array($conf['gallery_title'], $category['name'])),
480    'admin',
481    'cat_group_info',
482    array
483    (
484      'IMG_URL' => $img_url,
485      'CAT_NAME' => $category['name'],
486      'LINK' => make_index_url(
487          array(
488            'category' => array(
489              'id' => $category['id'],
490              'name' => $category['name'],
491              'permalink' => $category['permalink']
492              ))),
493      'CPL_CONTENT' => empty($_POST['mail_content'])
494                          ? '' : stripslashes($_POST['mail_content'])
495    ),
496    '' /* TODO Add listbox in order to choose Language selected */);
497
498  unset_make_full_url();
499
500  $query = '
501SELECT
502    name
503  FROM '.GROUPS_TABLE.'
504  WHERE id = '.$_POST['group'].'
505;';
506  list($group_name) = mysql_fetch_row(pwg_query($query));
507 
508  array_push(
509    $page['infos'],
510    sprintf(
511      l10n('An information email was sent to group "%s"'),
512      $group_name
513      )
514    );
515}
516
517if ('private' == $category['status'])
518{
519  $query = '
520SELECT
521    group_id
522  FROM '.GROUP_ACCESS_TABLE.'
523  WHERE cat_id = '.$category['id'].'
524;';
525}
526else
527{
528  $query = '
529SELECT
530    id AS group_id
531  FROM '.GROUPS_TABLE.'
532;';
533}
534$group_ids = array_from_query($query, 'group_id');
535
536if (count($group_ids) > 0)
537{
538  $query = '
539SELECT
540    id,
541    name
542  FROM '.GROUPS_TABLE.'
543  WHERE id IN ('.implode(',', $group_ids).')
544  ORDER BY name ASC
545;';
546  $template->assign('group_mail_options',
547      simple_hash_from_query($query, 'id', 'name')
548    );
549}
550
551//----------------------------------------------------------- sending html code
552$template->assign_var_from_handle('ADMIN_CONTENT', 'categories');
553?>
Note: See TracBrowser for help on using the repository browser.