source: trunk/comments.php @ 2459

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

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 KB
RevLine 
[166]1<?php
[354]2// +-----------------------------------------------------------------------+
[2297]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// +-----------------------------------------------------------------------+
[166]23
[579]24// +-----------------------------------------------------------------------+
25// |                           initialization                              |
26// +-----------------------------------------------------------------------+
[1598]27define('PHPWG_ROOT_PATH','./');
28include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
[345]29
[1072]30// +-----------------------------------------------------------------------+
31// | Check Access and exit when user status is not ok                      |
32// +-----------------------------------------------------------------------+
33check_status(ACCESS_GUEST);
34
[796]35$sort_order = array(
[2223]36  'DESC' => l10n('descending'),
37  'ASC'  => l10n('ascending')
[796]38  );
39
40// sort_by : database fields proposed for sorting comments list
41$sort_by = array(
[2223]42  'date' => l10n('comment date'),
43  'image_id' => l10n('picture')
[796]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
[1716]62$page['since'] = isset($_GET['since']) ? $_GET['since'] : 4;
[796]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']))
[393]69{
[796]70  $page['sort_by'] = $_GET['sort_by'];
[393]71}
[796]72
73// order to sort
74//
[2223]75$page['sort_order'] = 'DESC';
[796]76// if the form was submitted, it overloads default behaviour
77if (isset($_GET['sort_order']))
[393]78{
[2223]79  $page['sort_order'] = $_GET['sort_order'];
[393]80}
[796]81
82// number of items to display
83//
[1814]84$page['items_number'] = 10;
[796]85if (isset($_GET['items_number']))
86{
87  $page['items_number'] = $_GET['items_number'];
88}
89
[1716]90$page['where_clauses'] = array();
91
[796]92// which category to filter on ?
93if (isset($_GET['cat']) and 0 != $_GET['cat'])
94{
[1716]95  $page['where_clauses'][] =
[796]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{
[1716]102  $page['where_clauses'][] = 'com.author = \''.$_GET['author'].'\'';
[796]103}
104
105// search a substring among comments content
106if (isset($_GET['keyword']) and !empty($_GET['keyword']))
107{
[1716]108  $page['where_clauses'][] =
[796]109    '('.
110    implode(' AND ',
111            array_map(
112              create_function(
113                '$s',
114                'return "content LIKE \'%$s%\'";'
115                ),
[2012]116              preg_split('/[\s,;]+/', $_GET['keyword'] )
[796]117              )
118      ).
119    ')';
120}
121
[1716]122$page['where_clauses'][] = $since_options[$page['since']]['clause'];
123
[1598]124// which status to filter on ?
[1716]125if ( !is_admin() )
[1598]126{
[1716]127  $page['where_clauses'][] = 'validated="true"';
[1598]128}
129
[1716]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  );
[1598]140
[579]141// +-----------------------------------------------------------------------+
142// |                         comments management                           |
143// +-----------------------------------------------------------------------+
[1617]144if (isset($_GET['delete']) and is_numeric($_GET['delete'])
145      and !is_adviser() )
146{// comments deletion
147  check_status(ACCESS_ADMINISTRATOR);
148  $query = '
[579]149DELETE FROM '.COMMENTS_TABLE.'
[1598]150  WHERE id='.$_GET['delete'].'
[579]151;';
[1617]152  pwg_query($query);
153}
[1598]154
[1617]155if (isset($_GET['validate']) and is_numeric($_GET['validate'])
156      and !is_adviser() )
157{  // comments validation
158  check_status(ACCESS_ADMINISTRATOR);
159  $query = '
[579]160UPDATE '.COMMENTS_TABLE.'
161  SET validated = \'true\'
[1617]162  , validation_date = NOW()
[1598]163  WHERE id='.$_GET['validate'].'
[579]164;';
[1617]165  pwg_query($query);
[579]166}
[1617]167
[579]168// +-----------------------------------------------------------------------+
169// |                       page header and options                         |
170// +-----------------------------------------------------------------------+
[355]171
[2268]172$title= l10n('User comments');
[850]173$page['body_id'] = 'theCommentsPage';
174
[579]175$template->set_filenames(array('comments'=>'comments.tpl'));
[2223]176$template->assign(
[579]177  array(
[796]178    'F_ACTION'=>PHPWG_ROOT_PATH.'comments.php',
[2134]179    'F_KEYWORD'=>@htmlspecialchars(stripslashes($_GET['keyword'])),
180    'F_AUTHOR'=>@htmlspecialchars(stripslashes($_GET['author'])),
[579]181    )
182  );
[355]183
[796]184// +-----------------------------------------------------------------------+
185// |                          form construction                            |
186// +-----------------------------------------------------------------------+
187
188// Search in a particular category
[2223]189$blockname = 'categories';
[796]190
191$query = '
[1861]192SELECT id, name, uppercats, global_rank
[1677]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  ).'
[796]203;';
204display_select_cat_wrapper($query, array(@$_GET['cat']), $blockname, true);
205
206// Filter on recent comments...
[2223]207$tpl_var=array();
[796]208foreach ($since_options as $id => $option)
209{
[2223]210  $tpl_var[ $id ] = $option['label'];
[355]211}
[2223]212$template->assign( 'since_options', $tpl_var);
213$template->assign( 'since_options_selected', $page['since']);
[796]214
215// Sort by
[2223]216$template->assign( 'sort_by_options', $sort_by);
217$template->assign( 'sort_by_options_selected', $page['sort_by']);
[796]218
219// Sorting order
[2223]220$template->assign( 'sort_order_options', $sort_order);
221$template->assign( 'sort_order_options_selected', $page['sort_order']);
[796]222
223
224// Number of items
225$blockname = 'items_number_option';
[2223]226$tpl_var=array();
[796]227foreach ($items_number as $option)
228{
[2223]229  $tpl_var[ $option ] = is_numeric($option) ? $option : l10n($option);
[796]230}
[2223]231$template->assign( 'item_number_options', $tpl_var);
232$template->assign( 'item_number_options_selected', $page['items_number']);
[796]233
[2223]234
[579]235// +-----------------------------------------------------------------------+
[796]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
[1716]253  WHERE '.implode('
254    AND ', $page['where_clauses']).'
[796]255;';
256list($counter) = mysql_fetch_row(pwg_query($query));
257
[1598]258$url = PHPWG_ROOT_PATH
259    .'comments.php'
260    .get_query_string_diff(array('start','delete','validate'));
[796]261
262$navbar = create_navigation_bar($url,
263                                $counter,
264                                $start,
265                                $page['items_number'],
266                                '');
267
[2223]268$template->assign('NAVBAR', $navbar);
[796]269
270// +-----------------------------------------------------------------------+
[579]271// |                        last comments display                          |
272// +-----------------------------------------------------------------------+
[355]273
[796]274$comments = array();
275$element_ids = array();
276$category_ids = array();
277
[579]278$query = '
[796]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
[1598]286     , com.validated
[796]287  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
288    INNER JOIN '.COMMENTS_TABLE.' AS com
289    ON ic.image_id = com.image_id
[1716]290  WHERE '.implode('
291    AND ', $page['where_clauses']).'
[796]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.= '
[579]300;';
[587]301$result = pwg_query($query);
[1598]302while ($row = mysql_fetch_assoc($result))
[393]303{
[796]304  array_push($comments, $row);
305  array_push($element_ids, $row['image_id']);
306  array_push($category_ids, $row['category_id']);
[393]307}
[796]308
309if (count($comments) > 0)
[579]310{
[796]311  // retrieving element informations
312  $elements = array();
[579]313  $query = '
[796]314SELECT id, name, file, path, tn_ext
[579]315  FROM '.IMAGES_TABLE.'
[796]316  WHERE id IN ('.implode(',', $element_ids).')
[579]317;';
[796]318  $result = pwg_query($query);
[1598]319  while ($row = mysql_fetch_assoc($result))
[579]320  {
[796]321    $elements[$row['id']] = $row;
[579]322  }
[721]323
[796]324  // retrieving category informations
[579]325  $query = '
[1866]326SELECT id, name, permalink, uppercats
[796]327  FROM '.CATEGORIES_TABLE.'
328  WHERE id IN ('.implode(',', $category_ids).')
329;';
[1866]330  $categories = hash_from_query($query, 'id');
[796]331
332  foreach ($comments as $comment)
[579]333  {
[796]334    if (!empty($elements[$comment['image_id']]['name']))
[166]335    {
[1598]336      $name=$elements[$comment['image_id']]['name'];
[166]337    }
[796]338    else
339    {
[1598]340      $name=get_name_from_file($elements[$comment['image_id']]['file']);
[796]341    }
[1090]342
[796]343    // source of the thumbnail picture
[1598]344    $thumbnail_src = get_thumbnail_url( $elements[$comment['image_id']] );
[1090]345
[796]346    // link to the full size picture
[1090]347    $url = make_picture_url(
348            array(
[1861]349              'category' => $categories[ $comment['category_id'] ],
[1090]350              'image_id' => $comment['image_id'],
351              'image_file' => $elements[$comment['image_id']]['file'],
352            )
353          );
354
[796]355    $author = $comment['author'];
356    if (empty($comment['author']))
[393]357    {
[796]358      $author = l10n('guest');
[166]359    }
[1090]360
[2223]361    $tpl_comment =
[796]362      array(
[1004]363        'U_PICTURE' => $url,
[848]364        'TN_SRC' => $thumbnail_src,
[1598]365        'ALT' => $name,
[2030]366        'AUTHOR' => trigger_event('render_comment_author', $author),
[848]367        'DATE'=>format_date($comment['date'],'mysql_datetime',true),
[1598]368        'CONTENT'=>trigger_event('render_comment_content',$comment['content']),
[2223]369        );
[1598]370
371    if ( is_admin() )
372    {
373      $url = get_root_url().'comments.php'.get_query_string_diff(array('delete','validate'));
[2223]374      $tpl_comment['U_DELETE'] = add_url_params($url,
[1598]375                          array('delete'=>$comment['comment_id'])
[2223]376                         );
377
[1598]378      if ($comment['validated'] != 'true')
379      {
[2223]380        $tpl_comment['U_VALIDATE'] = add_url_params($url,
[1598]381                            array('validate'=>$comment['comment_id'])
[2223]382                           );
[1598]383      }
384    }
[2223]385    $template->append('comments', $tpl_comment);
[166]386  }
[579]387}
388// +-----------------------------------------------------------------------+
389// |                           html code display                           |
390// +-----------------------------------------------------------------------+
[2107]391include(PHPWG_ROOT_PATH.'include/page_header.php');
[2223]392$template->pparse('comments');
[1598]393include(PHPWG_ROOT_PATH.'include/page_tail.php');
[2107]394?>
Note: See TracBrowser for help on using the repository browser.