source: trunk/admin/element_set_global.php @ 786

Last change on this file since 786 was 764, checked in by plg, 19 years ago
  • elements batch management : element_set page becomes the frontend to element_set_global and element_set_unit, infos_images (after a long time of use) become deprecated : the more powerful element_set is used instead. Consequently, batch management concerns caddie but also "normal categories".
  • refactoring code in admin.php to include the sub-file (clearer)
  • caddie : function fill_caddie replaces the code in category.php and can be used in admin/element_set.php
  • caddie : caddie table is added in delete_elements function
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2005-04-16 16:56:32 +0000 (Sat, 16 Apr 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 764 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28/**
29 * Management of elements set. Elements can belong to a category or to the
30 * user caddie.
31 *
32 */
33 
34if (!defined('PHPWG_ROOT_PATH'))
35{
36  die('Hacking attempt!');
37}
38include_once(PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php');
39
40// +-----------------------------------------------------------------------+
41// |                               functions                               |
42// +-----------------------------------------------------------------------+
43
44/**
45 * returns the list of uniq keywords among given elements
46 *
47 * @param array element_ids
48 */
49function get_elements_keywords($element_ids)
50{
51  if (0 == count($element_ids))
52  {
53    return array();
54  }
55 
56  $keywords = array();
57 
58  $query = '
59SELECT keywords
60  FROM '.IMAGES_TABLE.'
61  WHERE id IN ('.implode(',', $element_ids).')
62;';
63  $result = pwg_query($query);
64  while ($row = mysql_fetch_array($result))
65  {
66    if (isset($row['keywords']) and !empty($row['keywords']))
67    {
68      $keywords = array_merge($keywords, explode(',', $row['keywords']));
69    }
70  }
71  return array_unique($keywords);
72}
73
74// +-----------------------------------------------------------------------+
75// |                       global mode form submission                     |
76// +-----------------------------------------------------------------------+
77$errors = array();
78
79if (isset($_POST['submit']))
80{
81  $collection = array();
82 
83//   echo '<pre>';
84//   print_r($_POST);
85//   echo '</pre>';
86//   exit();
87
88  switch ($_POST['target'])
89  {
90    case 'all' :
91    {
92      $collection = $page['cat_elements_id'];
93      break;
94    }
95    case 'selection' :
96    {
97      $collection = $_POST['selection'];
98      break;
99    }
100  }
101
102  if ($_POST['associate'] != 0)
103  {
104    $datas = array();
105
106    $query = '
107SELECT image_id
108  FROM '.IMAGE_CATEGORY_TABLE.'
109  WHERE category_id = '.$_POST['associate'].'
110;';
111    $associated = array_from_query($query, 'image_id');
112
113    // TODO : if $associable array is empty, no further actions
114    $associable = array_diff($collection, $associated);
115   
116    foreach ($associable as $item)
117    {
118      array_push($datas,
119                 array('category_id'=>$_POST['associate'],
120                       'image_id'=>$item));
121    }
122 
123    mass_inserts(IMAGE_CATEGORY_TABLE,
124                 array('image_id', 'category_id'),
125                 $datas);
126    update_category(array($_POST['associate']));
127  }
128
129  if ($_POST['dissociate'] != 0)
130  {
131    // physical links must not be broken, so we must first retrieve image_id
132    // which create virtual links with the category to "dissociate from".
133    $query = '
134SELECT id
135  FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.IMAGES_TABLE.' ON image_id = id
136  WHERE category_id = '.$_POST['dissociate'].'
137    AND category_id != storage_category_id
138    AND id IN ('.implode(',', $collection).')
139;';
140    $dissociables = array_from_query($query, 'id');
141
142    $query = '
143DELETE FROM '.IMAGE_CATEGORY_TABLE.'
144  WHERE category_id = '.$_POST['dissociate'].'
145  AND image_id IN ('.implode(',', $dissociables).')
146';
147    pwg_query($query);
148
149    update_category(array($_POST['dissociate']));
150  }
151
152  $datas = array();
153  $dbfields = array('primary' => array('id'), 'update' => array());
154
155  if (!empty($_POST['add_keywords']) or $_POST['remove_keyword'] != '0')
156  {
157    array_push($dbfields['update'], 'keywords');
158  }
159
160  $formfields = array('author', 'name', 'date_creation');
161  foreach ($formfields as $formfield)
162  {
163    if ($_POST[$formfield.'_action'] != 'leave')
164    {
165      array_push($dbfields['update'], $formfield);
166    }
167  }
168 
169  // updating elements is useful only if needed...
170  if (count($dbfields['update']) > 0)
171  {
172    $query = '
173SELECT id, keywords
174  FROM '.IMAGES_TABLE.'
175  WHERE id IN ('.implode(',', $collection).')
176;';
177    $result = pwg_query($query);
178
179    while ($row = mysql_fetch_array($result))
180    {
181      $data = array();
182      $data['id'] = $row['id'];
183     
184      if (!empty($_POST['add_keywords']))
185      {
186        $data['keywords'] =
187          implode(
188            ',',
189            array_unique(
190              array_merge(
191                get_keywords(empty($row['keywords']) ? '' : $row['keywords']),
192                get_keywords($_POST['add_keywords'])
193                )
194              )
195            );
196      }
197
198      if ($_POST['remove_keyword'] != '0')
199      {
200        if (!isset($data['keywords']))
201        {
202          $data['keywords'] = empty($row['keywords']) ? '' : $row['keywords'];
203        }
204       
205        $data['keywords'] =
206          implode(
207            ',',
208            array_unique(
209              array_diff(
210                get_keywords($data['keywords']),
211                array($_POST['remove_keyword'])
212                )
213              )
214            );
215
216        if ($data['keywords'] == '')
217        {
218          unset($data['keywords']);
219        }
220      }
221
222      if ('set' == $_POST['author_action'])
223      {
224        $data['author'] = $_POST['author'];
225
226        if ('' == $data['author'])
227        {
228          unset($data['author']);
229        }
230      }
231
232      if ('set' == $_POST['name_action'])
233      {
234        $data['name'] = $_POST['name'];
235
236        if ('' == $data['name'])
237        {
238          unset($data['name']);
239        }
240      }
241
242      if ('set' == $_POST['date_creation_action'])
243      {
244        $data['date_creation'] =
245          $_POST['date_creation_year']
246          .'-'.$_POST['date_creation_month']
247          .'-'.$_POST['date_creation_day']
248          ;
249      }
250     
251      array_push($datas, $data);
252    }
253    // echo '<pre>'; print_r($datas); echo '</pre>';
254    mass_updates(IMAGES_TABLE, $dbfields, $datas);
255  }
256}
257
258// +-----------------------------------------------------------------------+
259// |                             template init                             |
260// +-----------------------------------------------------------------------+
261$template->set_filenames(
262  array('element_set_global' => 'admin/element_set_global.tpl'));
263
264$base_url = PHPWG_ROOT_PATH.'admin.php';
265
266// $form_action = $base_url.'?page=element_set_global';
267
268$template->assign_vars(
269  array(
270    'CATEGORY_TITLE'=>$page['title'],
271   
272    'L_SUBMIT'=>$lang['submit'],
273
274    'U_COLS'=>$base_url.get_query_string_diff(array('cols')),
275    'U_DISPLAY'=>$base_url.get_query_string_diff(array('display')),
276   
277    'U_UNIT_MODE'
278    =>
279    $base_url
280    .get_query_string_diff(array('mode','display'))
281    .'&amp;mode=unit',
282   
283    'F_ACTION'=>$base_url.get_query_string_diff(array()),
284   )
285 );
286
287// +-----------------------------------------------------------------------+
288// |                            caddie options                             |
289// +-----------------------------------------------------------------------+
290
291if ('caddie' == $_GET['cat'])
292{
293  $template->assign_block_vars('in_caddie', array());
294}
295else
296{
297  $template->assign_block_vars('not_in_caddie', array());
298}
299
300// +-----------------------------------------------------------------------+
301// |                           global mode form                            |
302// +-----------------------------------------------------------------------+
303
304// Virtualy associate a picture to a category
305$blockname = 'associate_option';
306
307$template->assign_block_vars(
308  $blockname,
309  array('SELECTED' => '',
310        'VALUE'=> 0,
311        'OPTION' => '------------'
312    ));
313
314$query = '
315SELECT id,name,uppercats,global_rank
316  FROM '.CATEGORIES_TABLE.'
317;';
318display_select_cat_wrapper($query, array(), $blockname, true);
319
320// Dissociate from a category : categories listed for dissociation can
321// only represent virtual links. Links to physical categories can't be
322// broken
323$blockname = 'dissociate_option';
324
325$template->assign_block_vars(
326  $blockname,
327  array('SELECTED' => '',
328        'VALUE'=> 0,
329        'OPTION' => '------------'
330    ));
331
332if (count($page['cat_elements_id']) > 0)
333{
334  $query = '
335SELECT DISTINCT(category_id) AS id, c.name, uppercats, global_rank
336  FROM '.IMAGE_CATEGORY_TABLE.' AS ic,
337       '.CATEGORIES_TABLE.' AS c,
338       '.IMAGES_TABLE.' AS i
339  WHERE ic.image_id IN ('.implode(',', $page['cat_elements_id']).')
340    AND ic.category_id = c.id
341    AND ic.image_id = i.id
342    AND ic.category_id != i.storage_category_id
343;';
344  display_select_cat_wrapper($query, array(), $blockname, true);
345}
346
347$blockname = 'remove_keyword_option';
348
349$template->assign_block_vars(
350  $blockname,
351  array('VALUE'=> 0,
352        'OPTION' => '------------'
353    ));
354
355$keywords = get_elements_keywords($page['cat_elements_id']);
356
357foreach ($keywords as $keyword)
358{
359  $template->assign_block_vars(
360  $blockname,
361  array('VALUE'=> $keyword,
362        'OPTION' => $keyword
363    ));
364}
365
366// creation date
367$day =
368empty($_POST['date_creation_day']) ? date('j') : $_POST['date_creation_day'];
369get_day_list('date_creation_day', $day);
370
371if (!empty($_POST['date_creation_month']))
372{
373  $month = $_POST['date_creation_month'];
374}
375else
376{
377  $month = date('n');
378}
379get_month_list('date_creation_month', $month);
380
381if (!empty($_POST['date_creation_year']))
382{
383  $year = $_POST['date_creation_year'];
384}
385else
386{
387  $year = date('Y');
388}
389$template->assign_vars(array('DATE_CREATION_YEAR_VALUE'=>$year));
390
391// +-----------------------------------------------------------------------+
392// |                        global mode thumbnails                         |
393// +-----------------------------------------------------------------------+
394
395$page['cols'] = !empty($_GET['cols']) ? intval($_GET['cols']) : 5;
396$page['nb_images'] = !empty($_GET['display']) ? intval($_GET['display']) : 20;
397
398if (count($page['cat_elements_id']) > 0)
399{
400  $nav_bar = create_navigation_bar(
401    $base_url.get_query_string_diff(array('start')),
402    count($page['cat_elements_id']),
403    $page['start'],
404    $page['nb_images'],
405    '');
406  $template->assign_vars(array('NAV_BAR' => $nav_bar));
407
408  $query = '
409SELECT id,path,tn_ext
410  FROM '.IMAGES_TABLE.'
411  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
412  '.$conf['order_by'].'
413  LIMIT '.$page['start'].', '.$page['nb_images'].'
414;';
415  //echo '<pre>'.$query.'</pre>';
416  $result = pwg_query($query);
417
418  // template thumbnail initialization
419  if (mysql_num_rows($result) > 0)
420  {
421    $template->assign_block_vars('thumbnails', array());
422    // first line
423    $template->assign_block_vars('thumbnails.line', array());
424    // current row displayed
425    $row_number = 0;
426  }
427
428  while ($row = mysql_fetch_array($result))
429  {
430    $src = get_thumbnail_src($row['path'], @$row['tn_ext']);
431   
432    $template->assign_block_vars(
433      'thumbnails.line.thumbnail',
434      array(
435        'ID' => $row['id'],
436        'SRC' => $src,
437        'ALT' => 'TODO',
438        'TITLE' => 'TODO'
439        )
440      );
441   
442    // create a new line ?
443    if (++$row_number == $page['cols'])
444    {
445    $template->assign_block_vars('thumbnails.line', array());
446    $row_number = 0;
447    }
448  }
449}
450
451//----------------------------------------------------------- sending html code
452$template->assign_var_from_handle('ADMIN_CONTENT', 'element_set_global');
453?>
Note: See TracBrowser for help on using the repository browser.