source: trunk/comments.php @ 3445

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

Feature 1026 : Modify / delete comments for users

+ update config table content
+ minor modification of Sylvia theme
+ need refactoring

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