source: trunk/admin/history.php @ 1891

Last change on this file since 1891 was 1891, checked in by plg, 17 years ago

New: in history search, display tag names instead of tag ids in filtered
lines.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.4 KB
RevLine 
[1727]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)
[1782]8// | file          : $Id: history.php 1891 2007-03-11 15:11:53Z plg $
9// | last update   : $Date: 2007-03-11 15:11:53 +0000 (Sun, 11 Mar 2007) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 1891 $
[1727]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 * Display filtered history lines
30 */
31
32// echo '<pre>$_POST:
33// '; print_r($_POST); echo '</pre>';
34// echo '<pre>$_GET:
35// '; print_r($_GET); echo '</pre>';
36
37// +-----------------------------------------------------------------------+
38// |                              functions                                |
39// +-----------------------------------------------------------------------+
40
41// +-----------------------------------------------------------------------+
42// |                           initialization                              |
43// +-----------------------------------------------------------------------+
44
45if (!defined('PHPWG_ROOT_PATH'))
46{
47  die('Hacking attempt!');
48}
49
50include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
[1881]51include_once(PHPWG_ROOT_PATH.'admin/include/functions_history.inc.php');
[1727]52
53if (isset($_GET['start']) and is_numeric($_GET['start']))
54{
55  $page['start'] = $_GET['start'];
56}
57else
58{
59  $page['start'] = 0;
60}
61
[1844]62$types = array('none', 'picture', 'high', 'other');
63
[1727]64// +-----------------------------------------------------------------------+
65// | Check Access and exit when user status is not ok                      |
66// +-----------------------------------------------------------------------+
67
68check_status(ACCESS_ADMINISTRATOR);
69
70// +-----------------------------------------------------------------------+
71// | Build search criteria and redirect to results                         |
72// +-----------------------------------------------------------------------+
73
74$errors = array();
75$search = array();
76
77if (isset($_POST['submit']))
78{
79  // dates
80  if (!empty($_POST['start_year']))
81  {
82    $search['fields']['date-after'] = sprintf(
83      '%d-%02d-%02d',
84      $_POST['start_year'],
85      $_POST['start_month'],
86      $_POST['start_day']
87      );
88  }
89
90  if (!empty($_POST['end_year']))
91  {
92    $search['fields']['date-before'] = sprintf(
93      '%d-%02d-%02d',
94      $_POST['end_year'],
95      $_POST['end_month'],
96      $_POST['end_day']
97      );
98  }
99
[1844]100  $search['fields']['types'] = $_POST['types'];
[1890]101
102  $search['fields']['user'] = $_POST['user'];
[1817]103 
[1727]104  // echo '<pre>'; print_r($search); echo '</pre>';
105 
106  if (!empty($search))
107  {
108    // register search rules in database, then they will be available on
109    // thumbnails page and picture page.
110    $query ='
111INSERT INTO '.SEARCH_TABLE.'
112  (rules)
113  VALUES
114  (\''.serialize($search).'\')
115;';
116    pwg_query($query);
117
118    $search_id = mysql_insert_id();
119   
120    redirect(
121      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
122      );
123  }
124  else
125  {
126    array_push($errors, $lang['search_one_clause_at_least']);
127  }
128}
129
130// +-----------------------------------------------------------------------+
131// |                             template init                             |
132// +-----------------------------------------------------------------------+
133
[1881]134$template->set_filename('history', 'admin/history.tpl');
[1727]135
[1881]136// TabSheet initialization
137history_tabsheet();
138
[1727]139$base_url = PHPWG_ROOT_PATH.'admin.php?page=history';
140
141$template->assign_vars(
142  array(
143    'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=history',
144
145    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=history'
146    )
147  );
148
149$template->assign_vars(
150  array(
151    'TODAY_DAY'   => date('d', time()),
152    'TODAY_MONTH' => date('m', time()),
153    'TODAY_YEAR'  => date('Y', time()),
154    )
155  );
156
157// +-----------------------------------------------------------------------+
158// |                             history lines                             |
159// +-----------------------------------------------------------------------+
160
161if (isset($_GET['search_id'])
162    and $page['search_id'] = (int)$_GET['search_id'])
163{
164  // what are the lines to display in reality ?
165  $query = '
166SELECT rules
167  FROM '.SEARCH_TABLE.'
168  WHERE id = '.$page['search_id'].'
169;';
170  list($serialized_rules) = mysql_fetch_row(pwg_query($query));
171
172  $page['search'] = unserialize($serialized_rules);
173
[1890]174  if (isset($_GET['user_id']))
175  {
176    if (!is_numeric($_GET['user_id']))
177    {
178      die('user_id GET parameter must be an integer value');
179    }
180
181    $page['search']['fields']['user'] = $_GET['user_id'];
182   
183    $query ='
184INSERT INTO '.SEARCH_TABLE.'
185  (rules)
186  VALUES
187  (\''.serialize($page['search']).'\')
188;';
189    pwg_query($query);
190
191    $search_id = mysql_insert_id();
192   
193    redirect(
194      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
195      );
196  }
197
[1727]198  // echo '<pre>'; print_r($page['search']); echo '</pre>';
199 
200  $clauses = array();
201
202  if (isset($page['search']['fields']['date-after']))
203  {
204    array_push(
205      $clauses,
206      "date >= '".$page['search']['fields']['date-after']."'"
207      );
208  }
209
210  if (isset($page['search']['fields']['date-before']))
211  {
212    array_push(
213      $clauses,
214      "date <= '".$page['search']['fields']['date-before']."'"
215      );
216  }
217
[1844]218  if (isset($page['search']['fields']['types']))
[1817]219  {
[1844]220    $local_clauses = array();
[1817]221   
[1844]222    foreach ($types as $type) {
223      if (in_array($type, $page['search']['fields']['types'])) {
224        $clause = 'image_type ';
225        if ($type == 'none')
226        {
227          $clause.= 'IS NULL';
228        }
229        else
230        {
231          $clause.= "= '".$type."'";
232        }
233       
234        array_push($local_clauses, $clause);
235      }
[1817]236    }
237   
[1844]238    if (count($local_clauses) > 0)
[1817]239    {
[1844]240      array_push(
241        $clauses,
242        implode(' OR ', $local_clauses)
243        );
[1817]244    }
245  }
[1890]246
247  if (isset($page['search']['fields']['user'])
248      and $page['search']['fields']['user'] != -1)
249  {
250    array_push(
251      $clauses,
252      'user_id = '.$page['search']['fields']['user']
253      );
254  }
[1817]255 
[1727]256  $clauses = prepend_append_array_items($clauses, '(', ')');
257
258  $where_separator =
259    implode(
260      "\n    AND ",
261      $clauses
262      );
263 
264  $query = '
[1817]265SELECT
266    date,
267    time,
268    user_id,
269    IP,
270    section,
271    category_id,
272    tag_ids,
273    image_id,
[1844]274    image_type
[1727]275  FROM '.HISTORY_TABLE.'
276  WHERE '.$where_separator.'
277;';
278
[1883]279  // LIMIT '.$page['start'].', '.$conf['nb_logs_page'].'
280
[1727]281  $result = pwg_query($query);
[1883]282
283  $page['nb_lines'] = mysql_num_rows($result);
284 
285  $history_lines = array();
286  $user_ids = array();
287  $category_ids = array();
288  $image_ids = array();
289  $tag_ids = array();
290 
[1821]291  while ($row = mysql_fetch_assoc($result))
[1727]292  {
293    $user_ids[$row['user_id']] = 1;
294
295    if (isset($row['category_id']))
296    {
297      $category_ids[$row['category_id']] = 1;
298    }
299
300    if (isset($row['image_id']))
301    {
302      $image_ids[$row['image_id']] = 1;
303    }
304
[1891]305    if (isset($row['tag_ids']))
306    {
307      foreach (explode(',', $row['tag_ids']) as $tag_id)
308      {
309        array_push($tag_ids, $tag_id);
310      }
311    }
312
[1727]313    array_push(
314      $history_lines,
315      $row
316      );
317  }
318
319  // prepare reference data (users, tags, categories...)
320  if (count($user_ids) > 0)
321  {
322    $query = '
323SELECT '.$conf['user_fields']['id'].' AS id
324     , '.$conf['user_fields']['username'].' AS username
325  FROM '.USERS_TABLE.'
326  WHERE id IN ('.implode(',', array_keys($user_ids)).')
327;';
328    $result = pwg_query($query);
329
330    $username_of = array();
331    while ($row = mysql_fetch_array($result))
332    {
333      $username_of[$row['id']] = $row['username'];
334    }
335  }
336
337  if (count($category_ids) > 0)
338  {
339    $query = '
340SELECT id, uppercats
341  FROM '.CATEGORIES_TABLE.'
342  WHERE id IN ('.implode(',', array_keys($category_ids)).')
343;';
344    $uppercats_of = simple_hash_from_query($query, 'id', 'uppercats');
345
346    $name_of_category = array();
347   
348    foreach ($uppercats_of as $category_id => $uppercats)
349    {
350      $name_of_category[$category_id] = get_cat_display_name_cache(
351        $uppercats
352        );
353    }
354  }
355
356  if (count($image_ids) > 0)
357  {
358    $query = '
[1883]359SELECT
360    id,
361    IF(name IS NULL, file, name) AS label,
362    filesize,
363    high_filesize
[1727]364  FROM '.IMAGES_TABLE.'
365  WHERE id IN ('.implode(',', array_keys($image_ids)).')
366;';
[1883]367    // $label_of_image = simple_hash_from_query($query, 'id', 'label');
368    $label_of_image = array();
369    $filesize_of_image = array();
370    $high_filesize_of_image = array();
371   
372    $result = pwg_query($query);
373    while ($row = mysql_fetch_array($result))
374    {
375      $label_of_image[ $row['id'] ] = $row['label'];
376
377      if (isset($row['filesize']))
378      {
379        $filesize_of_image[ $row['id'] ] = $row['filesize'];
380      }
381
382      if (isset($row['high_filesize']))
383      {
384        $high_filesize_of_image[ $row['id'] ] = $row['high_filesize'];
385      }
386    }
387
388    // echo '<pre>'; print_r($high_filesize_of_image); echo '</pre>';
[1727]389  }
[1891]390
391  if (count($tag_ids) > 0)
392  {
393    $tag_ids = array_unique($tag_ids);
394
395    $query = '
396SELECT
397    id,
398    name
399  FROM '.TAGS_TABLE.'
400  WHERE id IN ('.implode(', ', $tag_ids).')
401;';
402    $name_of_tag = array();
403
404    $result = pwg_query($query);
405    while ($row = mysql_fetch_array($result))
406    {
407      $name_of_tag[ $row['id'] ] = $row['name'];
408    }
409  }
[1727]410 
411  $i = 0;
[1883]412  $first_line = $page['start'] + 1;
413  $last_line = $page['start'] + $conf['nb_logs_page'];
[1727]414
[1883]415  $total_filesize = 0;
416
[1727]417  foreach ($history_lines as $line)
418  {
[1883]419    if (isset($line['image_type']))
420    {
421      if ($line['image_type'] == 'high')
422      {
423        if (isset($high_filesize_of_image[$line['image_id']]))
424        {
425          $total_filesize+= $high_filesize_of_image[$line['image_id']];
426        }
427      }
428      else
429      {
430        if (isset($filesize_of_image[$line['image_id']]))
431        {
432          $total_filesize+= $filesize_of_image[$line['image_id']];
433        }
434      }
435    }
436   
437    $i++;
438   
439    if ($i < $first_line or $i > $last_line)
440    {
441      continue;
442    }
[1890]443
444    $user_string = '';
445    if (isset($username_of[$line['user_id']]))
446    {
447      $user_string.= $username_of[$line['user_id']];
448    }
449    else
450    {
451      $user_string.= $line['user_id'];
452    }
453    $user_string.= '&nbsp;<a href="';
454    $user_string.= PHPWG_ROOT_PATH.'admin.php?page=history';
455    $user_string.= '&amp;search_id='.$page['search_id'];
456    $user_string.= '&amp;user_id='.$line['user_id'];
457    $user_string.= '">+</a>';
[1891]458
459    $tags_string = '';
460    if (isset($line['tag_ids']))
461    {
462      $tags_string = preg_replace(
463        '/(\d+)/e',
464        '$name_of_tag["$1"]',
465        str_replace(
466          ',',
467          ', ',
468          $line['tag_ids']
469          )
470        );
471    }
[1883]472   
[1727]473    $template->assign_block_vars(
474      'detail',
475      array(
476        'DATE'      => $line['date'],
477        'TIME'      => $line['time'],
[1890]478        'USER'      => $user_string,
[1727]479        'IP'        => $line['IP'],
480        'IMAGE'     => isset($line['image_id'])
[1821]481          ? ( isset($label_of_image[$line['image_id']])
[1883]482                ? sprintf(
483                    '(%u) %s',
484                    $line['image_id'],
485                    $label_of_image[$line['image_id']]
486                  )
487                : sprintf(
488                    '(%u) deleted ',
489                    $line['image_id']
490                  )
491            )
492          : '',
[1844]493        'TYPE'      => $line['image_type'],
[1727]494        'SECTION'   => $line['section'],
495        'CATEGORY'  => isset($line['category_id'])
[1821]496          ? ( isset($name_of_category[$line['category_id']])
497                ? $name_of_category[$line['category_id']]
498                : 'deleted '.$line['category_id'] )
[1727]499          : '',
[1891]500        'TAGS'       => $tags_string,
[1883]501        'T_CLASS'   => ($i % 2) ? 'row1' : 'row2',
[1727]502        )
503      );
504  }
[1883]505
506  $template->assign_block_vars(
507    'summary',
508    array(
509      'FILESIZE' => $total_filesize.' KB',
510      )
511    );
[1727]512}
513
514// $groups_string = preg_replace(
515//     '/(\d+)/e',
516//     "\$groups['$1']",
517//     implode(
518//       ', ',
519//       $local_user['groups']
520//       )
521//     );
522
523// +-----------------------------------------------------------------------+
524// |                            navigation bar                             |
525// +-----------------------------------------------------------------------+
526
527if (isset($page['search_id']))
528{
529  $navbar = create_navigation_bar(
530    PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start')),
531    $page['nb_lines'],
532    $page['start'],
533    $conf['nb_logs_page']
534    );
535
536  $template->assign_block_vars(
537    'navigation',
538    array(
539      'NAVBAR' => $navbar
540      )
541    );
542}
543
544// +-----------------------------------------------------------------------+
545// |                             filter form                               |
546// +-----------------------------------------------------------------------+
547
548$form = array();
549
550if (isset($page['search']))
551{
552  if (isset($page['search']['fields']['date-after']))
553  {
554    $tokens = explode('-', $page['search']['fields']['date-after']);
555   
556    $form['start_year']  = (int)$tokens[0];
557    $form['start_month'] = (int)$tokens[1];
558    $form['start_day']   = (int)$tokens[2];
559  }
560
561  if (isset($page['search']['fields']['date-before']))
562  {
563    $tokens = explode('-', $page['search']['fields']['date-before']);
[1817]564
565    $form['end_year']  = (int)$tokens[0];
566    $form['end_month'] = (int)$tokens[1];
567    $form['end_day']   = (int)$tokens[2];
[1727]568  }
[1817]569
[1844]570  $form['types'] = $page['search']['fields']['types'];
[1890]571
572  if (isset($page['search']['fields']['user']))
573  {
574    $form['user'] = $page['search']['fields']['user'];
575  }
576  else
577  {
578    $form['user'] = null;
579  }
[1727]580}
581else
582{
583  // by default, at page load, we want the selected date to be the current
584  // date
585  $form['start_year']  = $form['end_year']  = date('Y');
586  $form['start_month'] = $form['end_month'] = date('n');
587  $form['start_day']   = $form['end_day']   = date('j');
[1844]588  $form['types'] = $types;
[1727]589}
590
591// start date
592get_day_list('start_day', @$form['start_day']);
593get_month_list('start_month', @$form['start_month']);
594// end date
595get_day_list('end_day', @$form['end_day']);
596get_month_list('end_month', @$form['end_month']);
597
598$template->assign_vars(
599  array(
600    'START_YEAR' => @$form['start_year'],
601    'END_YEAR'   => @$form['end_year'],
602    )
603  );
[1817]604
[1844]605foreach ($types as $option)
[1817]606{
[1844]607  $selected = '';
608 
609  if (in_array($option, $form['types']))
[1817]610  {
[1844]611    $selected = 'selected="selected"';
[1817]612  }
[1844]613 
614  $template->assign_block_vars(
615    'types_option',
616    array(
617      'VALUE' => $option,
618      'CONTENT' => l10n($option),
619      'SELECTED' => $selected,
620      )
621    );
[1817]622}
[1890]623
624$template->assign_block_vars(
625  'user_option',
626  array(
627    'VALUE'=> -1,
628    'CONTENT' => '------------',
629    'SELECTED' => ''
630    )
631  );
632
633$query = '
634SELECT
635    '.$conf['user_fields']['id'].' AS id,
636    '.$conf['user_fields']['username'].' AS username
637  FROM '.USERS_TABLE.'
638  ORDER BY username ASC
639;';
640$result = pwg_query($query);
641
642while ($row = mysql_fetch_array($result))
643{
644  $selected = '';
645
[1891]646  if (isset($form['user'])
647      and $row['id'] == $form['user'])
[1890]648  {
649    $selected = 'selected="selected"';
650  }
[1727]651 
[1890]652  $template->assign_block_vars(
653    'user_option',
654    array(
655      'VALUE' => $row['id'],
656      'CONTENT' => $row['username'],
657      'SELECTED' => $selected,
658      )
659    );
660}
661 
[1727]662// +-----------------------------------------------------------------------+
663// |                           html code display                           |
664// +-----------------------------------------------------------------------+
665
666$template->assign_var_from_handle('ADMIN_CONTENT', 'history');
667?>
Note: See TracBrowser for help on using the repository browser.