source: trunk/comments.php @ 3405

Last change on this file since 3405 was 3405, checked in by nikrou, 15 years ago

remove duplicate retrieved field

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