source: tags/release-1_5_0RC2/admin/element_set_global.php @ 25219

Last change on this file since 25219 was 875, checked in by plg, 19 years ago
  • bug 162 fixed: division by zero when trying to view "all" items in admin/element_set_(global|unit)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.7 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-09-24 19:05:55 +0000 (Sat, 24 Sep 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 875 $
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
78if (isset($_POST['submit']))
79{
80  $collection = array();
81 
82//   echo '<pre>';
83//   print_r($_POST);
84//   echo '</pre>';
85//   exit();
86
87  switch ($_POST['target'])
88  {
89    case 'all' :
90    {
91      $collection = $page['cat_elements_id'];
92      break;
93    }
94    case 'selection' :
95    {
96      if (!isset($_POST['selection']) or count($_POST['selection']) == 0)
97      {
98        array_push($page['errors'], l10n('Select at least one picture'));
99      }
100      else
101      {
102        $collection = $_POST['selection'];
103      }
104      break;
105    }
106  }
107
108  if ($_POST['associate'] != 0 and count($collection) > 0)
109  {
110    $datas = array();
111
112    $query = '
113SELECT image_id
114  FROM '.IMAGE_CATEGORY_TABLE.'
115  WHERE category_id = '.$_POST['associate'].'
116;';
117    $associated = array_from_query($query, 'image_id');
118
119    // TODO : if $associable array is empty, no further actions
120    $associable = array_diff($collection, $associated);
121   
122    foreach ($associable as $item)
123    {
124      array_push($datas,
125                 array('category_id'=>$_POST['associate'],
126                       'image_id'=>$item));
127    }
128 
129    mass_inserts(IMAGE_CATEGORY_TABLE,
130                 array('image_id', 'category_id'),
131                 $datas);
132    update_category(array($_POST['associate']));
133  }
134
135  if ($_POST['dissociate'] != 0 and count($collection) > 0)
136  {
137    // physical links must not be broken, so we must first retrieve image_id
138    // which create virtual links with the category to "dissociate from".
139    $query = '
140SELECT id
141  FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.IMAGES_TABLE.' ON image_id = id
142  WHERE category_id = '.$_POST['dissociate'].'
143    AND category_id != storage_category_id
144    AND id IN ('.implode(',', $collection).')
145;';
146    $dissociables = array_from_query($query, 'id');
147
148    $query = '
149DELETE FROM '.IMAGE_CATEGORY_TABLE.'
150  WHERE category_id = '.$_POST['dissociate'].'
151  AND image_id IN ('.implode(',', $dissociables).')
152';
153    pwg_query($query);
154
155    update_category(array($_POST['dissociate']));
156  }
157
158  $datas = array();
159  $dbfields = array('primary' => array('id'), 'update' => array());
160
161  if (!empty($_POST['add_keywords']) or $_POST['remove_keyword'] != '0')
162  {
163    array_push($dbfields['update'], 'keywords');
164  }
165
166  $formfields = array('author', 'name', 'date_creation');
167  foreach ($formfields as $formfield)
168  {
169    if ($_POST[$formfield.'_action'] != 'leave')
170    {
171      array_push($dbfields['update'], $formfield);
172    }
173  }
174 
175  // updating elements is useful only if needed...
176  if (count($dbfields['update']) > 0 and count($collection) > 0)
177  {
178    $query = '
179SELECT id, keywords
180  FROM '.IMAGES_TABLE.'
181  WHERE id IN ('.implode(',', $collection).')
182;';
183    $result = pwg_query($query);
184
185    while ($row = mysql_fetch_array($result))
186    {
187      $data = array();
188      $data['id'] = $row['id'];
189     
190      if (!empty($_POST['add_keywords']))
191      {
192        $data['keywords'] =
193          implode(
194            ',',
195            array_unique(
196              array_merge(
197                get_keywords(empty($row['keywords']) ? '' : $row['keywords']),
198                get_keywords($_POST['add_keywords'])
199                )
200              )
201            );
202      }
203
204      if ($_POST['remove_keyword'] != '0')
205      {
206        if (!isset($data['keywords']))
207        {
208          $data['keywords'] = empty($row['keywords']) ? '' : $row['keywords'];
209        }
210       
211        $data['keywords'] =
212          implode(
213            ',',
214            array_unique(
215              array_diff(
216                get_keywords($data['keywords']),
217                array($_POST['remove_keyword'])
218                )
219              )
220            );
221
222        if ($data['keywords'] == '')
223        {
224          unset($data['keywords']);
225        }
226      }
227
228      if ('set' == $_POST['author_action'])
229      {
230        $data['author'] = $_POST['author'];
231
232        if ('' == $data['author'])
233        {
234          unset($data['author']);
235        }
236      }
237
238      if ('set' == $_POST['name_action'])
239      {
240        $data['name'] = $_POST['name'];
241
242        if ('' == $data['name'])
243        {
244          unset($data['name']);
245        }
246      }
247
248      if ('set' == $_POST['date_creation_action'])
249      {
250        $data['date_creation'] =
251          $_POST['date_creation_year']
252          .'-'.$_POST['date_creation_month']
253          .'-'.$_POST['date_creation_day']
254          ;
255      }
256     
257      array_push($datas, $data);
258    }
259    // echo '<pre>'; print_r($datas); echo '</pre>';
260    mass_updates(IMAGES_TABLE, $dbfields, $datas);
261  }
262}
263
264// +-----------------------------------------------------------------------+
265// |                             template init                             |
266// +-----------------------------------------------------------------------+
267$template->set_filenames(
268  array('element_set_global' => 'admin/element_set_global.tpl'));
269
270$base_url = PHPWG_ROOT_PATH.'admin.php';
271
272// $form_action = $base_url.'?page=element_set_global';
273
274$template->assign_vars(
275  array(
276    'CATEGORIES_NAV'=>$page['title'],
277   
278    'L_SUBMIT'=>$lang['submit'],
279
280    'U_COLS'=>$base_url.get_query_string_diff(array('cols')),
281    'U_DISPLAY'=>$base_url.get_query_string_diff(array('display')),
282   
283    'U_UNIT_MODE'
284    =>
285    $base_url
286    .get_query_string_diff(array('mode','display'))
287    .'&amp;mode=unit',
288   
289    'F_ACTION'=>$base_url.get_query_string_diff(array()),
290   )
291 );
292
293// +-----------------------------------------------------------------------+
294// |                            caddie options                             |
295// +-----------------------------------------------------------------------+
296
297if ('caddie' == $_GET['cat'])
298{
299  $template->assign_block_vars('in_caddie', array());
300}
301else
302{
303  $template->assign_block_vars('not_in_caddie', array());
304}
305
306// +-----------------------------------------------------------------------+
307// |                           global mode form                            |
308// +-----------------------------------------------------------------------+
309
310// Virtualy associate a picture to a category
311$blockname = 'associate_option';
312
313$template->assign_block_vars(
314  $blockname,
315  array('SELECTED' => '',
316        'VALUE'=> 0,
317        'OPTION' => '------------'
318    ));
319
320$query = '
321SELECT id,name,uppercats,global_rank
322  FROM '.CATEGORIES_TABLE.'
323;';
324display_select_cat_wrapper($query, array(), $blockname, true);
325
326// Dissociate from a category : categories listed for dissociation can
327// only represent virtual links. Links to physical categories can't be
328// broken
329$blockname = 'dissociate_option';
330
331$template->assign_block_vars(
332  $blockname,
333  array('SELECTED' => '',
334        'VALUE'=> 0,
335        'OPTION' => '------------'
336    ));
337
338if (count($page['cat_elements_id']) > 0)
339{
340  $query = '
341SELECT DISTINCT(category_id) AS id, c.name, uppercats, global_rank
342  FROM '.IMAGE_CATEGORY_TABLE.' AS ic,
343       '.CATEGORIES_TABLE.' AS c,
344       '.IMAGES_TABLE.' AS i
345  WHERE ic.image_id IN ('.implode(',', $page['cat_elements_id']).')
346    AND ic.category_id = c.id
347    AND ic.image_id = i.id
348    AND ic.category_id != i.storage_category_id
349;';
350  display_select_cat_wrapper($query, array(), $blockname, true);
351}
352
353$blockname = 'remove_keyword_option';
354
355$template->assign_block_vars(
356  $blockname,
357  array('VALUE'=> 0,
358        'OPTION' => '------------'
359    ));
360
361$keywords = get_elements_keywords($page['cat_elements_id']);
362
363foreach ($keywords as $keyword)
364{
365  $template->assign_block_vars(
366  $blockname,
367  array('VALUE'=> $keyword,
368        'OPTION' => $keyword
369    ));
370}
371
372// creation date
373$day =
374empty($_POST['date_creation_day']) ? date('j') : $_POST['date_creation_day'];
375get_day_list('date_creation_day', $day);
376
377if (!empty($_POST['date_creation_month']))
378{
379  $month = $_POST['date_creation_month'];
380}
381else
382{
383  $month = date('n');
384}
385get_month_list('date_creation_month', $month);
386
387if (!empty($_POST['date_creation_year']))
388{
389  $year = $_POST['date_creation_year'];
390}
391else
392{
393  $year = date('Y');
394}
395$template->assign_vars(array('DATE_CREATION_YEAR_VALUE'=>$year));
396
397// +-----------------------------------------------------------------------+
398// |                        global mode thumbnails                         |
399// +-----------------------------------------------------------------------+
400
401$page['cols'] = !empty($_GET['cols']) ? intval($_GET['cols']) : 5;
402
403// how many items to display on this page
404if (!empty($_GET['display']))
405{
406  if ('all' == $_GET['display'])
407  {
408    $page['nb_images'] = count($page['cat_elements_id']);
409  }
410  else
411  {
412    $page['nb_images'] = intval($_GET['display']);
413  }
414}
415else
416{
417  $page['nb_images'] = 20;
418}
419
420if (count($page['cat_elements_id']) > 0)
421{
422  $nav_bar = create_navigation_bar(
423    $base_url.get_query_string_diff(array('start')),
424    count($page['cat_elements_id']),
425    $page['start'],
426    $page['nb_images'],
427    '');
428  $template->assign_vars(array('NAV_BAR' => $nav_bar));
429
430  $query = '
431SELECT id,path,tn_ext
432  FROM '.IMAGES_TABLE.'
433  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
434  '.$conf['order_by'].'
435  LIMIT '.$page['start'].', '.$page['nb_images'].'
436;';
437  //echo '<pre>'.$query.'</pre>';
438  $result = pwg_query($query);
439
440  // template thumbnail initialization
441  if (mysql_num_rows($result) > 0)
442  {
443    $template->assign_block_vars('thumbnails', array());
444    // first line
445    $template->assign_block_vars('thumbnails.line', array());
446    // current row displayed
447    $row_number = 0;
448  }
449
450  while ($row = mysql_fetch_array($result))
451  {
452    $src = get_thumbnail_src($row['path'], @$row['tn_ext']);
453   
454    $template->assign_block_vars(
455      'thumbnails.line.thumbnail',
456      array(
457        'ID' => $row['id'],
458        'SRC' => $src,
459        'ALT' => 'TODO',
460        'TITLE' => 'TODO'
461        )
462      );
463   
464    // create a new line ?
465    if (++$row_number == $page['cols'])
466    {
467    $template->assign_block_vars('thumbnails.line', array());
468    $row_number = 0;
469    }
470  }
471}
472
473//----------------------------------------------------------- sending html code
474$template->assign_var_from_handle('ADMIN_CONTENT', 'element_set_global');
475?>
Note: See TracBrowser for help on using the repository browser.