source: trunk/admin/cat_modify.php @ 12954

Last change on this file since 12954 was 12954, checked in by rvelices, 12 years ago

feature 2548 multisize

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