source: branches/2.0/comments.php @ 2755

Last change on this file since 2755 was 2755, checked in by rvelices, 16 years ago
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 12.1 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// +-----------------------------------------------------------------------+
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.id AS comment_id
286     , com.validated
287  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
288    INNER JOIN '.COMMENTS_TABLE.' AS com
289    ON ic.image_id = com.image_id
290  WHERE '.implode('
291    AND ', $page['where_clauses']).'
292  GROUP BY comment_id
293  ORDER BY '.$page['sort_by'].' '.$page['sort_order'];
294if ('all' != $page['items_number'])
295{
296  $query.= '
297  LIMIT '.$start.','.$page['items_number'];
298}
299$query.= '
300;';
301$result = pwg_query($query);
302while ($row = mysql_fetch_assoc($result))
303{
304  array_push($comments, $row);
305  array_push($element_ids, $row['image_id']);
306  array_push($category_ids, $row['category_id']);
307}
308
309if (count($comments) > 0)
310{
311  // retrieving element informations
312  $elements = array();
313  $query = '
314SELECT id, name, file, path, tn_ext
315  FROM '.IMAGES_TABLE.'
316  WHERE id IN ('.implode(',', $element_ids).')
317;';
318  $result = pwg_query($query);
319  while ($row = mysql_fetch_assoc($result))
320  {
321    $elements[$row['id']] = $row;
322  }
323
324  // retrieving category informations
325  $query = '
326SELECT id, name, permalink, uppercats
327  FROM '.CATEGORIES_TABLE.'
328  WHERE id IN ('.implode(',', $category_ids).')
329;';
330  $categories = hash_from_query($query, 'id');
331
332  foreach ($comments as $comment)
333  {
334    if (!empty($elements[$comment['image_id']]['name']))
335    {
336      $name=$elements[$comment['image_id']]['name'];
337    }
338    else
339    {
340      $name=get_name_from_file($elements[$comment['image_id']]['file']);
341    }
342
343    // source of the thumbnail picture
344    $thumbnail_src = get_thumbnail_url( $elements[$comment['image_id']] );
345
346    // link to the full size picture
347    $url = make_picture_url(
348            array(
349              'category' => $categories[ $comment['category_id'] ],
350              'image_id' => $comment['image_id'],
351              'image_file' => $elements[$comment['image_id']]['file'],
352            )
353          );
354
355    $author = $comment['author'];
356    if (empty($comment['author']))
357    {
358      $author = l10n('guest');
359    }
360
361    $tpl_comment =
362      array(
363        'U_PICTURE' => $url,
364        'TN_SRC' => $thumbnail_src,
365        'ALT' => $name,
366        'AUTHOR' => trigger_event('render_comment_author', $author),
367        'DATE'=>format_date($comment['date'],'mysql_datetime',true),
368        'CONTENT'=>trigger_event('render_comment_content',$comment['content']),
369        );
370
371    if ( is_admin() )
372    {
373      $url = get_root_url().'comments.php'.get_query_string_diff(array('delete','validate'));
374      $tpl_comment['U_DELETE'] = add_url_params($url,
375                          array('delete'=>$comment['comment_id'])
376                         );
377
378      if ($comment['validated'] != 'true')
379      {
380        $tpl_comment['U_VALIDATE'] = add_url_params($url,
381                            array('validate'=>$comment['comment_id'])
382                           );
383      }
384    }
385    $template->append('comments', $tpl_comment);
386  }
387}
388// +-----------------------------------------------------------------------+
389// |                           html code display                           |
390// +-----------------------------------------------------------------------+
391include(PHPWG_ROOT_PATH.'include/page_header.php');
392$template->pparse('comments');
393include(PHPWG_ROOT_PATH.'include/page_tail.php');
394?>
Note: See TracBrowser for help on using the repository browser.