source: trunk/admin/batch_manager_global.php @ 8408

Last change on this file since 8408 was 8408, checked in by plg, 10 years ago

Batch Manager:

  • add l0n to the new Batch Manager where is was missing
  • change photo "name" to photo "title"
  • change the way we display "Select all" and "Select the whole set"
File size: 16.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
24/**
25 * Management of elements set. Elements can belong to a category or to the
26 * user caddie.
27 *
28 */
29
30if (!defined('PHPWG_ROOT_PATH'))
31{
32  die('Hacking attempt!');
33}
34
35include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
36
37// +-----------------------------------------------------------------------+
38// | Check Access and exit when user status is not ok                      |
39// +-----------------------------------------------------------------------+
40
41check_status(ACCESS_ADMINISTRATOR);
42
43trigger_action('loc_begin_element_set_global');
44
45// the $_POST['selection'] was already checked in element_set.php
46check_input_parameter('del_tags', $_POST, true, PATTERN_ID);
47check_input_parameter('associate', $_POST, false, PATTERN_ID);
48check_input_parameter('dissociate', $_POST, false, PATTERN_ID);
49
50// +-----------------------------------------------------------------------+
51// |                            current selection                          |
52// +-----------------------------------------------------------------------+
53
54$collection = array();
55if (isset($_POST['setSelected']))
56{
57  $collection = $page['cat_elements_id'];
58}
59else if (isset($_POST['selection']))
60{
61  $collection = $_POST['selection'];
62}
63
64// +-----------------------------------------------------------------------+
65// |                       global mode form submission                     |
66// +-----------------------------------------------------------------------+
67
68if (isset($_POST['submit']))
69{
70  // if the user tries to apply an action, it means that there is at least 1
71  // photo in the selection
72  if (count($collection) == 0)
73  {
74    array_push($page['errors'], l10n('Select at least one picture'));
75  }
76
77  $action = $_POST['selectAction'];
78 
79  if ('remove_from_caddie' == $action)
80  {
81    $query = '
82DELETE
83  FROM '.CADDIE_TABLE.'
84  WHERE element_id IN ('.implode(',', $collection).')
85    AND user_id = '.$user['id'].'
86;';
87    pwg_query($query);
88
89    // if we are here in the code, it means that the user is currently
90    // displaying the caddie content, so we have to remove the current
91    // selection from the current set
92    $page['cat_elements_id'] = array_diff($page['cat_elements_id'], $collection);
93  }
94
95  if ('add_tags' == $action)
96  {
97    $tag_ids = get_fckb_tag_ids($_POST['add_tags']);
98    add_tags($tag_ids, $collection);
99  }
100
101  if ('del_tags' == $action)
102  {
103    if (count($_POST['del_tags']) == 0)
104    {
105      array_push($page['errors'], l10n('Select at least one tag'));
106    }
107   
108    $query = '
109DELETE
110  FROM '.IMAGE_TAG_TABLE.'
111  WHERE image_id IN ('.implode(',', $collection).')
112    AND tag_id IN ('.implode(',', $_POST['del_tags']).')
113;';
114    pwg_query($query);
115  }
116
117  if ('associate' == $action)
118  {
119    associate_images_to_categories(
120      $collection,
121      array($_POST['associate'])
122      );
123
124    $_SESSION['page_infos'] = array(
125      l10n('Information data registered in database')
126      );
127   
128    // let's refresh the page because we the current set might be modified
129    $redirect_url = get_root_url().'admin.php?page='.$_GET['page'];
130    redirect($redirect_url);
131  }
132
133  if ('dissociate' == $action)
134  {
135    // physical links must not be broken, so we must first retrieve image_id
136    // which create virtual links with the category to "dissociate from".
137    $query = '
138SELECT id
139  FROM '.IMAGE_CATEGORY_TABLE.'
140    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
141  WHERE category_id = '.$_POST['dissociate'].'
142    AND id IN ('.implode(',', $collection).')
143    AND (
144      category_id != storage_category_id
145      OR storage_category_id IS NULL
146    )
147;';
148    $dissociables = array_from_query($query, 'id');
149
150    if (!empty($dissociables))
151    {
152      $query = '
153DELETE
154  FROM '.IMAGE_CATEGORY_TABLE.'
155  WHERE category_id = '.$_POST['dissociate'].'
156    AND image_id IN ('.implode(',', $dissociables).')
157';
158      pwg_query($query);
159
160      update_category($_POST['dissociate']);
161     
162      $_SESSION['page_infos'] = array(
163        l10n('Information data registered in database')
164        );
165     
166      // let's refresh the page because we the current set might be modified
167      $redirect_url = get_root_url().'admin.php?page='.$_GET['page'];
168      redirect($redirect_url);
169    }
170  }
171
172  // author
173  if ('author' == $action)
174  {
175    $datas = array();
176    foreach ($collection as $image_id)
177    {
178      array_push(
179        $datas,
180        array(
181          'id' => $image_id,
182          'author' => $_POST['author']
183          )
184        );
185    }
186
187    mass_updates(
188      IMAGES_TABLE,
189      array('primary' => array('id'), 'update' => array('author')),
190      $datas
191      );
192  }
193
194  // title
195  if ('title' == $action)
196  {
197    $datas = array();
198    foreach ($collection as $image_id)
199    {
200      array_push(
201        $datas,
202        array(
203          'id' => $image_id,
204          'name' => $_POST['title']
205          )
206        );
207    }
208
209    mass_updates(
210      IMAGES_TABLE,
211      array('primary' => array('id'), 'update' => array('name')),
212      $datas
213      );
214  }
215 
216  // date_creation
217  if ('date_creation' == $action)
218  {
219    $date_creation = sprintf(
220      '%u-%u-%u',
221      $_POST['date_creation_year'],
222      $_POST['date_creation_month'],
223      $_POST['date_creation_day']
224      );
225
226    $datas = array();
227    foreach ($collection as $image_id)
228    {
229      array_push(
230        $datas,
231        array(
232          'id' => $image_id,
233          'date_creation' => $date_creation
234          )
235        );
236    }
237
238    mass_updates(
239      IMAGES_TABLE,
240      array('primary' => array('id'), 'update' => array('date_creation')),
241      $datas
242      );
243  }
244 
245  // privacy_level
246  if ('level' == $action)
247  {
248    $datas = array();
249    foreach ($collection as $image_id)
250    {
251      array_push(
252        $datas,
253        array(
254          'id' => $image_id,
255          'level' => $_POST['level']
256          )
257        );
258    }
259
260    mass_updates(
261      IMAGES_TABLE,
262      array('primary' => array('id'), 'update' => array('level')),
263      $datas
264      );
265  }
266 
267  // add_to_caddie
268  if ('add_to_caddie' == $action)
269  {
270    fill_caddie($collection);
271  }
272 
273  // delete
274  if ('delete' == $action)
275  {
276    if (isset($_POST['confirm_deletion']) and 1 == $_POST['confirm_deletion'])
277    {
278      $deleted_count = delete_elements($collection, true);
279      if ($deleted_count > 0)
280      {
281        $_SESSION['page_infos'] = array(
282          sprintf(
283            l10n_dec(
284              '%d photo was deleted',
285              '%d photos were deleted',
286              $deleted_count
287              ),
288            $deleted_count
289            )
290          );
291
292        $redirect_url = get_root_url().'admin.php?page='.$_GET['page'];
293        redirect($redirect_url);
294      }
295      else
296      {
297        array_push($page['errors'], l10n('No photo can be deleted'));
298      }
299    }
300    else
301    {
302      array_push($page['errors'], l10n('You need to confirm deletion'));
303    }
304  }
305}
306
307// +-----------------------------------------------------------------------+
308// |                             template init                             |
309// +-----------------------------------------------------------------------+
310$template->set_filenames(array('batch_manager_global' => 'batch_manager_global.tpl'));
311
312$base_url = get_root_url().'admin.php';
313
314$template->assign(
315  array(
316    'filter' => $_SESSION['bulk_manager_filter'],
317   
318    'selection' => $collection,
319   
320    'U_DISPLAY'=>$base_url.get_query_string_diff(array('display')),
321
322    'U_UNIT_MODE'
323    =>
324    $base_url
325    .get_query_string_diff(array('mode','display'))
326    .'&amp;mode=unit',
327
328    'F_ACTION'=>$base_url.get_query_string_diff(array('cat')),
329   )
330 );
331
332// +-----------------------------------------------------------------------+
333// |                            caddie options                             |
334// +-----------------------------------------------------------------------+
335
336$in_caddie = false;
337if (isset($_SESSION['bulk_manager_filter']['prefilter'])
338    and 'caddie' == $_SESSION['bulk_manager_filter']['prefilter'])
339{
340  $in_caddie = true;
341}
342$template->assign('IN_CADDIE', $in_caddie);
343
344// +-----------------------------------------------------------------------+
345// |                            deletion form                              |
346// +-----------------------------------------------------------------------+
347
348// we can only remove photos that have no storage_category_id, in other
349// word, it currently (Butterfly) means that the photo was added with
350// pLoader
351if (count($page['cat_elements_id']) > 0)
352{
353  $query = '
354SELECT
355    COUNT(*)
356  FROM '.IMAGES_TABLE.'
357  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
358    AND storage_category_id IS NULL
359;';
360  list($counter) = pwg_db_fetch_row(pwg_query($query));
361
362  if ($counter > 0)
363  {
364    $template->assign('show_delete_form', true);
365  }
366}
367
368// +-----------------------------------------------------------------------+
369// |                           global mode form                            |
370// +-----------------------------------------------------------------------+
371
372// privacy level
373$template->assign(
374    array(
375      'filter_level_options'=> get_privacy_level_options(),
376      'filter_level_options_selected' => isset($_SESSION['bulk_manager_filter']['level'])
377        ? $_SESSION['bulk_manager_filter']['level']
378        : 0,
379    )
380  );
381
382// Virtualy associate a picture to a category
383$query = '
384SELECT id,name,uppercats,global_rank
385  FROM '.CATEGORIES_TABLE.'
386;';
387display_select_cat_wrapper($query, array(), 'associate_options', true);
388
389// in the filter box, which category to select by default
390$selected_category = array();
391
392if (isset($_SESSION['bulk_manager_filter']['category']))
393{
394  $selected_category = array($_SESSION['bulk_manager_filter']['category']);
395}
396else
397{
398  // we need to know the category in which the last photo was added
399  $selected_category = array();
400
401  $query = '
402SELECT
403    category_id,
404    id_uppercat
405  FROM '.IMAGES_TABLE.' AS i
406    JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON image_id = i.id
407    JOIN '.CATEGORIES_TABLE.' AS c ON category_id = c.id
408  ORDER BY i.id DESC
409  LIMIT 1
410;';
411  $result = pwg_query($query);
412  if (pwg_db_num_rows($result) > 0)
413  {
414    $row = pwg_db_fetch_assoc($result);
415 
416    $selected_category = array($row['category_id']);
417  }
418}
419
420$query = '
421SELECT id,name,uppercats,global_rank
422  FROM '.CATEGORIES_TABLE.'
423;';
424display_select_cat_wrapper($query, $selected_category, 'filter_category_options', true);
425
426// Dissociate from a category : categories listed for dissociation can only
427// represent virtual links. We can't create orphans. Links to physical
428// categories can't be broken.
429if (count($page['cat_elements_id']) > 0)
430{
431  $query = '
432SELECT
433    DISTINCT(category_id) AS id,
434    c.name,
435    c.uppercats,
436    c.global_rank
437  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
438    JOIN '.CATEGORIES_TABLE.' AS c ON c.id = ic.category_id
439    JOIN '.IMAGES_TABLE.' AS i ON i.id = ic.image_id
440  WHERE ic.image_id IN ('.implode(',', $page['cat_elements_id']).')
441    AND (
442      ic.category_id != i.storage_category_id
443      OR i.storage_category_id IS NULL
444    )
445;';
446  display_select_cat_wrapper($query, array(), 'dissociate_options', true);
447}
448
449if (count($page['cat_elements_id']) > 0)
450{
451  // remove tags
452  $tags = get_common_tags($page['cat_elements_id'], -1);
453
454  $template->assign(
455    array(
456      'DEL_TAG_SELECTION' => get_html_tag_selection($tags, 'del_tags'),
457      )
458    );
459}
460
461// creation date
462$day =
463empty($_POST['date_creation_day']) ? date('j') : $_POST['date_creation_day'];
464
465$month =
466empty($_POST['date_creation_month']) ? date('n') : $_POST['date_creation_month'];
467
468$year =
469empty($_POST['date_creation_year']) ? date('Y') : $_POST['date_creation_year'];
470
471$month_list = $lang['month'];
472$month_list[0]='------------';
473ksort($month_list);
474$template->assign( array(
475      'month_list'         => $month_list,
476      'DATE_CREATION_DAY'  => (int)$day,
477      'DATE_CREATION_MONTH'=> (int)$month,
478      'DATE_CREATION_YEAR' => (int)$year,
479    )
480  );
481
482// image level options
483$template->assign(
484    array(
485      'level_options'=> get_privacy_level_options(),
486      'level_options_selected' => 0,
487    )
488  );
489
490// +-----------------------------------------------------------------------+
491// |                        global mode thumbnails                         |
492// +-----------------------------------------------------------------------+
493
494// how many items to display on this page
495if (!empty($_GET['display']))
496{
497  if ('all' == $_GET['display'])
498  {
499    $page['nb_images'] = count($page['cat_elements_id']);
500  }
501  else
502  {
503    $page['nb_images'] = intval($_GET['display']);
504  }
505}
506else
507{
508  $page['nb_images'] = 20;
509}
510
511$nb_thumbs_page = 0;
512
513if (count($page['cat_elements_id']) > 0)
514{
515  $nav_bar = create_navigation_bar(
516    $base_url.get_query_string_diff(array('start')),
517    count($page['cat_elements_id']),
518    $page['start'],
519    $page['nb_images']
520    );
521  $template->assign('navbar', $nav_bar);
522
523  $is_category = false;
524  if (isset($_SESSION['bulk_manager_filter']['category'])
525      and !isset($_SESSION['bulk_manager_filter']['category_recursive']))
526  {
527    $is_category = true;
528  }
529
530  if (isset($_SESSION['bulk_manager_filter']['prefilter'])
531      and 'duplicates' == $_SESSION['bulk_manager_filter']['prefilter'])
532  {
533    $conf['order_by'] = ' ORDER BY file, id';
534  }
535
536
537  $query = '
538SELECT id,path,tn_ext,file,filesize,level,name
539  FROM '.IMAGES_TABLE;
540 
541  if ($is_category)
542  {
543    $category_info = get_cat_info($_SESSION['bulk_manager_filter']['category']);
544   
545    $conf['order_by'] = $conf['order_by_inside_category'];
546    if (!empty($category_info['image_order']))
547    {
548      $conf['order_by'] = ' ORDER BY '.$category_info['image_order'];
549    }
550
551    $query.= '
552    JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
553  }
554
555  $query.= '
556  WHERE id IN ('.implode(',', $page['cat_elements_id']).')';
557
558  if ($is_category)
559  {
560    $query.= '
561    AND category_id = '.$_SESSION['bulk_manager_filter']['category'];
562  }
563
564  $query.= '
565  '.$conf['order_by'].'
566  LIMIT '.$page['nb_images'].' OFFSET '.$page['start'].'
567;';
568  $result = pwg_query($query);
569
570  // template thumbnail initialization
571  while ($row = pwg_db_fetch_assoc($result))
572  {
573    $nb_thumbs_page++;
574    $src = get_thumbnail_url($row);
575
576    $title = $row['name'];
577    if (empty($title))
578    {     
579      $title = get_name_from_file($row['file']);
580    }
581
582    $template->append(
583      'thumbnails',
584      array(
585        'ID' => $row['id'],
586        'TN_SRC' => $src,
587        'FILE' => $row['file'],
588        'TITLE' => $title,
589        'LEVEL' => $row['level']
590        )
591      );
592  }
593}
594
595$template->assign(
596  array(
597    'nb_thumbs_page' => $nb_thumbs_page,
598    'nb_thumbs_set' => count($page['cat_elements_id']),
599    )
600  );
601
602trigger_action('loc_end_element_set_global');
603
604//----------------------------------------------------------- sending html code
605$template->assign_var_from_handle('ADMIN_CONTENT', 'batch_manager_global');
606?>
Note: See TracBrowser for help on using the repository browser.