source: trunk/comments.php @ 2297

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 13.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      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// | PhpWebGallery - a PHP based picture gallery                           |
25// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
26// | Copyright (C) 2003-2008 PhpWebGallery Team - http://phpwebgallery.net |
27// +-----------------------------------------------------------------------+
28// | file          : $Id: comments.php 2297 2008-04-04 22:57:23Z plg $
29// | last update   : $Date: 2008-04-04 22:57:23 +0000 (Fri, 04 Apr 2008) $
30// | last modifier : $Author: plg $
31// | revision      : $Revision: 2297 $
32// +-----------------------------------------------------------------------+
33// | This program is free software; you can redistribute it and/or modify  |
34// | it under the terms of the GNU General Public License as published by  |
35// | the Free Software Foundation                                          |
36// |                                                                       |
37// | This program is distributed in the hope that it will be useful, but   |
38// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
39// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
40// | General Public License for more details.                              |
41// |                                                                       |
42// | You should have received a copy of the GNU General Public License     |
43// | along with this program; if not, write to the Free Software           |
44// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
45// | USA.                                                                  |
46// +-----------------------------------------------------------------------+
47
48// +-----------------------------------------------------------------------+
49// |                           initialization                              |
50// +-----------------------------------------------------------------------+
51define('PHPWG_ROOT_PATH','./');
52include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
53
54// +-----------------------------------------------------------------------+
55// | Check Access and exit when user status is not ok                      |
56// +-----------------------------------------------------------------------+
57check_status(ACCESS_GUEST);
58
59$sort_order = array(
60  'DESC' => l10n('descending'),
61  'ASC'  => l10n('ascending')
62  );
63
64// sort_by : database fields proposed for sorting comments list
65$sort_by = array(
66  'date' => l10n('comment date'),
67  'image_id' => l10n('picture')
68  );
69
70// items_number : list of number of items to display per page
71$items_number = array(5,10,20,50,'all');
72
73// since when display comments ?
74//
75$since_options = array(
76  1 => array('label' => l10n('today'),
77             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 1 DAY)'),
78  2 => array('label' => sprintf(l10n('last %d days'), 7),
79             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 7 DAY)'),
80  3 => array('label' => sprintf(l10n('last %d days'), 30),
81             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 30 DAY)'),
82  4 => array('label' => l10n('the beginning'),
83             'clause' => '1=1') // stupid but generic
84  );
85
86$page['since'] = isset($_GET['since']) ? $_GET['since'] : 4;
87
88// on which field sorting
89//
90$page['sort_by'] = 'date';
91// if the form was submitted, it overloads default behaviour
92if (isset($_GET['sort_by']))
93{
94  $page['sort_by'] = $_GET['sort_by'];
95}
96
97// order to sort
98//
99$page['sort_order'] = 'DESC';
100// if the form was submitted, it overloads default behaviour
101if (isset($_GET['sort_order']))
102{
103  $page['sort_order'] = $_GET['sort_order'];
104}
105
106// number of items to display
107//
108$page['items_number'] = 10;
109if (isset($_GET['items_number']))
110{
111  $page['items_number'] = $_GET['items_number'];
112}
113
114$page['where_clauses'] = array();
115
116// which category to filter on ?
117if (isset($_GET['cat']) and 0 != $_GET['cat'])
118{
119  $page['where_clauses'][] =
120    'category_id IN ('.implode(',', get_subcat_ids(array($_GET['cat']))).')';
121}
122
123// search a particular author
124if (isset($_GET['author']) and !empty($_GET['author']))
125{
126  $page['where_clauses'][] = 'com.author = \''.$_GET['author'].'\'';
127}
128
129// search a substring among comments content
130if (isset($_GET['keyword']) and !empty($_GET['keyword']))
131{
132  $page['where_clauses'][] =
133    '('.
134    implode(' AND ',
135            array_map(
136              create_function(
137                '$s',
138                'return "content LIKE \'%$s%\'";'
139                ),
140              preg_split('/[\s,;]+/', $_GET['keyword'] )
141              )
142      ).
143    ')';
144}
145
146$page['where_clauses'][] = $since_options[$page['since']]['clause'];
147
148// which status to filter on ?
149if ( !is_admin() )
150{
151  $page['where_clauses'][] = 'validated="true"';
152}
153
154$page['where_clauses'][] = get_sql_condition_FandF
155  (
156    array
157      (
158        'forbidden_categories' => 'category_id',
159        'visible_categories' => 'category_id',
160        'visible_images' => 'ic.image_id'
161      ),
162    '', true
163  );
164
165// +-----------------------------------------------------------------------+
166// |                         comments management                           |
167// +-----------------------------------------------------------------------+
168if (isset($_GET['delete']) and is_numeric($_GET['delete'])
169      and !is_adviser() )
170{// comments deletion
171  check_status(ACCESS_ADMINISTRATOR);
172  $query = '
173DELETE FROM '.COMMENTS_TABLE.'
174  WHERE id='.$_GET['delete'].'
175;';
176  pwg_query($query);
177}
178
179if (isset($_GET['validate']) and is_numeric($_GET['validate'])
180      and !is_adviser() )
181{  // comments validation
182  check_status(ACCESS_ADMINISTRATOR);
183  $query = '
184UPDATE '.COMMENTS_TABLE.'
185  SET validated = \'true\'
186  , validation_date = NOW()
187  WHERE id='.$_GET['validate'].'
188;';
189  pwg_query($query);
190}
191
192// +-----------------------------------------------------------------------+
193// |                       page header and options                         |
194// +-----------------------------------------------------------------------+
195
196$title= l10n('User comments');
197$page['body_id'] = 'theCommentsPage';
198
199$template->set_filenames(array('comments'=>'comments.tpl'));
200$template->assign(
201  array(
202    'F_ACTION'=>PHPWG_ROOT_PATH.'comments.php',
203    'F_KEYWORD'=>@htmlspecialchars(stripslashes($_GET['keyword'])),
204    'F_AUTHOR'=>@htmlspecialchars(stripslashes($_GET['author'])),
205    )
206  );
207
208// +-----------------------------------------------------------------------+
209// |                          form construction                            |
210// +-----------------------------------------------------------------------+
211
212// Search in a particular category
213$blockname = 'categories';
214
215$query = '
216SELECT id, name, uppercats, global_rank
217  FROM '.CATEGORIES_TABLE.'
218'.get_sql_condition_FandF
219  (
220    array
221      (
222        'forbidden_categories' => 'id',
223        'visible_categories' => 'id'
224      ),
225    'WHERE'
226  ).'
227;';
228display_select_cat_wrapper($query, array(@$_GET['cat']), $blockname, true);
229
230// Filter on recent comments...
231$tpl_var=array();
232foreach ($since_options as $id => $option)
233{
234  $tpl_var[ $id ] = $option['label'];
235}
236$template->assign( 'since_options', $tpl_var);
237$template->assign( 'since_options_selected', $page['since']);
238
239// Sort by
240$template->assign( 'sort_by_options', $sort_by);
241$template->assign( 'sort_by_options_selected', $page['sort_by']);
242
243// Sorting order
244$template->assign( 'sort_order_options', $sort_order);
245$template->assign( 'sort_order_options_selected', $page['sort_order']);
246
247
248// Number of items
249$blockname = 'items_number_option';
250$tpl_var=array();
251foreach ($items_number as $option)
252{
253  $tpl_var[ $option ] = is_numeric($option) ? $option : l10n($option);
254}
255$template->assign( 'item_number_options', $tpl_var);
256$template->assign( 'item_number_options_selected', $page['items_number']);
257
258
259// +-----------------------------------------------------------------------+
260// |                            navigation bar                             |
261// +-----------------------------------------------------------------------+
262
263if (isset($_GET['start']) and is_numeric($_GET['start']))
264{
265  $start = $_GET['start'];
266}
267else
268{
269  $start = 0;
270}
271
272$query = '
273SELECT COUNT(DISTINCT(id))
274  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
275    INNER JOIN '.COMMENTS_TABLE.' AS com
276    ON ic.image_id = com.image_id
277  WHERE '.implode('
278    AND ', $page['where_clauses']).'
279;';
280list($counter) = mysql_fetch_row(pwg_query($query));
281
282$url = PHPWG_ROOT_PATH
283    .'comments.php'
284    .get_query_string_diff(array('start','delete','validate'));
285
286$navbar = create_navigation_bar($url,
287                                $counter,
288                                $start,
289                                $page['items_number'],
290                                '');
291
292$template->assign('NAVBAR', $navbar);
293
294// +-----------------------------------------------------------------------+
295// |                        last comments display                          |
296// +-----------------------------------------------------------------------+
297
298$comments = array();
299$element_ids = array();
300$category_ids = array();
301
302$query = '
303SELECT com.id AS comment_id
304     , com.image_id
305     , ic.category_id
306     , com.author
307     , com.date
308     , com.content
309     , com.id AS comment_id
310     , com.validated
311  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
312    INNER JOIN '.COMMENTS_TABLE.' AS com
313    ON ic.image_id = com.image_id
314  WHERE '.implode('
315    AND ', $page['where_clauses']).'
316  GROUP BY comment_id
317  ORDER BY '.$page['sort_by'].' '.$page['sort_order'];
318if ('all' != $page['items_number'])
319{
320  $query.= '
321  LIMIT '.$start.','.$page['items_number'];
322}
323$query.= '
324;';
325$result = pwg_query($query);
326while ($row = mysql_fetch_assoc($result))
327{
328  array_push($comments, $row);
329  array_push($element_ids, $row['image_id']);
330  array_push($category_ids, $row['category_id']);
331}
332
333if (count($comments) > 0)
334{
335  // retrieving element informations
336  $elements = array();
337  $query = '
338SELECT id, name, file, path, tn_ext
339  FROM '.IMAGES_TABLE.'
340  WHERE id IN ('.implode(',', $element_ids).')
341;';
342  $result = pwg_query($query);
343  while ($row = mysql_fetch_assoc($result))
344  {
345    $elements[$row['id']] = $row;
346  }
347
348  // retrieving category informations
349  $query = '
350SELECT id, name, permalink, uppercats
351  FROM '.CATEGORIES_TABLE.'
352  WHERE id IN ('.implode(',', $category_ids).')
353;';
354  $categories = hash_from_query($query, 'id');
355
356  foreach ($comments as $comment)
357  {
358    if (!empty($elements[$comment['image_id']]['name']))
359    {
360      $name=$elements[$comment['image_id']]['name'];
361    }
362    else
363    {
364      $name=get_name_from_file($elements[$comment['image_id']]['file']);
365    }
366
367    // source of the thumbnail picture
368    $thumbnail_src = get_thumbnail_url( $elements[$comment['image_id']] );
369
370    // link to the full size picture
371    $url = make_picture_url(
372            array(
373              'category' => $categories[ $comment['category_id'] ],
374              'image_id' => $comment['image_id'],
375              'image_file' => $elements[$comment['image_id']]['file'],
376            )
377          );
378
379    $author = $comment['author'];
380    if (empty($comment['author']))
381    {
382      $author = l10n('guest');
383    }
384
385    $tpl_comment =
386      array(
387        'U_PICTURE' => $url,
388        'TN_SRC' => $thumbnail_src,
389        'ALT' => $name,
390        'AUTHOR' => trigger_event('render_comment_author', $author),
391        'DATE'=>format_date($comment['date'],'mysql_datetime',true),
392        'CONTENT'=>trigger_event('render_comment_content',$comment['content']),
393        );
394
395    if ( is_admin() )
396    {
397      $url = get_root_url().'comments.php'.get_query_string_diff(array('delete','validate'));
398      $tpl_comment['U_DELETE'] = add_url_params($url,
399                          array('delete'=>$comment['comment_id'])
400                         );
401
402      if ($comment['validated'] != 'true')
403      {
404        $tpl_comment['U_VALIDATE'] = add_url_params($url,
405                            array('validate'=>$comment['comment_id'])
406                           );
407      }
408    }
409    $template->append('comments', $tpl_comment);
410  }
411}
412// +-----------------------------------------------------------------------+
413// |                           html code display                           |
414// +-----------------------------------------------------------------------+
415include(PHPWG_ROOT_PATH.'include/page_header.php');
416$template->pparse('comments');
417include(PHPWG_ROOT_PATH.'include/page_tail.php');
418?>
Note: See TracBrowser for help on using the repository browser.