source: trunk/comments.php @ 5945

Last change on this file since 5945 was 5199, checked in by rvelices, 15 years ago

fix bug (a comment could be displayed several times in the comments liat)

File size: 14.7 KB
RevLine 
[166]1<?php
[354]2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[5196]5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
[2297]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');
[3445]29include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php');
[345]30
[1072]31// +-----------------------------------------------------------------------+
32// | Check Access and exit when user status is not ok                      |
33// +-----------------------------------------------------------------------+
34check_status(ACCESS_GUEST);
35
[796]36$sort_order = array(
[2223]37  'DESC' => l10n('descending'),
38  'ASC'  => l10n('ascending')
[796]39  );
40
41// sort_by : database fields proposed for sorting comments list
42$sort_by = array(
[2223]43  'date' => l10n('comment date'),
44  'image_id' => l10n('picture')
[796]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'),
[4367]54             'clause' => 'date > '.pwg_db_get_recent_period_expression(1)),
[796]55  2 => array('label' => sprintf(l10n('last %d days'), 7),
[4367]56             'clause' => 'date > '.pwg_db_get_recent_period_expression(7)),
[796]57  3 => array('label' => sprintf(l10n('last %d days'), 30),
[4367]58             'clause' => 'date > '.pwg_db_get_recent_period_expression(30)),
[796]59  4 => array('label' => l10n('the beginning'),
60             'clause' => '1=1') // stupid but generic
61  );
62
[4139]63if (!empty($_GET['since']) && is_numeric($_GET['since']))
64{
65  $page['since'] = $_GET['since'];
66}
67else
68{
69  $page['since'] = 4;
70}
[796]71
72// on which field sorting
73//
74$page['sort_by'] = 'date';
75// if the form was submitted, it overloads default behaviour
[2757]76if (isset($_GET['sort_by']) and isset($sort_by[$_GET['sort_by']]) )
[393]77{
[796]78  $page['sort_by'] = $_GET['sort_by'];
[393]79}
[796]80
81// order to sort
82//
[2223]83$page['sort_order'] = 'DESC';
[796]84// if the form was submitted, it overloads default behaviour
[2757]85if (isset($_GET['sort_order']) and isset($sort_order[$_GET['sort_order']]))
[393]86{
[2223]87  $page['sort_order'] = $_GET['sort_order'];
[393]88}
[796]89
90// number of items to display
91//
[1814]92$page['items_number'] = 10;
[796]93if (isset($_GET['items_number']))
94{
95  $page['items_number'] = $_GET['items_number'];
96}
[3600]97if ( !is_numeric($page['items_number']) and $page['items_number']!='all' )
[3520]98{
99  $page['items_number'] = 10;
100}
[796]101
[1716]102$page['where_clauses'] = array();
103
[796]104// which category to filter on ?
105if (isset($_GET['cat']) and 0 != $_GET['cat'])
106{
[1716]107  $page['where_clauses'][] =
[796]108    'category_id IN ('.implode(',', get_subcat_ids(array($_GET['cat']))).')';
109}
110
111// search a particular author
[4139]112if (!empty($_GET['author']))
[796]113{
[3487]114  $page['where_clauses'][] =
115    'u.'.$conf['user_fields']['username'].' = \''.$_GET['author'].'\'
116     OR author = \''.$_GET['author'].'\'';
[796]117}
118
[5195]119// search a specific comment (if you're coming directly from an admin
120// notification email)
121if (!empty($_GET['comment_id']))
122{
123  check_input_parameter('comment_id', $_GET, false, PATTERN_ID);
124
125  // currently, the $_GET['comment_id'] is only used by admins from email
126  // for management purpose (validate/delete)
127  if (!is_admin())
128  {
129    $login_url =
130      get_root_url().'identification.php?redirect='
131      .urlencode(urlencode($_SERVER['REQUEST_URI']))
132      ;
133    redirect($login_url);
134  }
135
136  $page['where_clauses'][] = 'com.id = '.$_GET['comment_id'];
137}
138
[796]139// search a substring among comments content
[4139]140if (!empty($_GET['keyword']))
[796]141{
[1716]142  $page['where_clauses'][] =
[796]143    '('.
144    implode(' AND ',
145            array_map(
146              create_function(
147                '$s',
148                'return "content LIKE \'%$s%\'";'
149                ),
[2012]150              preg_split('/[\s,;]+/', $_GET['keyword'] )
[796]151              )
152      ).
153    ')';
154}
155
[1716]156$page['where_clauses'][] = $since_options[$page['since']]['clause'];
157
[1598]158// which status to filter on ?
[1716]159if ( !is_admin() )
[1598]160{
[4367]161  $page['where_clauses'][] = 'validated=\'true\'';
[1598]162}
163
[1716]164$page['where_clauses'][] = get_sql_condition_FandF
165  (
166    array
167      (
168        'forbidden_categories' => 'category_id',
169        'visible_categories' => 'category_id',
170        'visible_images' => 'ic.image_id'
171      ),
172    '', true
173  );
[1598]174
[579]175// +-----------------------------------------------------------------------+
176// |                         comments management                           |
177// +-----------------------------------------------------------------------+
[1598]178
[5195]179$comment_id = null;
180$action = null;
181
182$actions = array('delete', 'validate', 'edit');
183foreach ($actions as $loop_action)
184{
185  if (isset($_GET[$loop_action]))
186  {
[5199]187    $action = $loop_action;
[5195]188    check_input_parameter($action, $_GET, false, PATTERN_ID);
189    $comment_id = $_GET[$action];
190    break;
191  }
[579]192}
[1617]193
[5195]194if (isset($action))
[3445]195{
[5195]196  check_pwg_token();
197
198  $comment_author_id = get_comment_author_id($comment_id);
[5199]199
[5195]200  if (can_manage_comment($action, $comment_author_id))
[3445]201  {
[5195]202    $perform_redirect = false;
[5199]203
[5195]204    if ('delete' == $action)
205    {
206      delete_user_comment($comment_id);
207      $perform_redirect = true;
208    }
[3445]209
[5195]210    if ('validate' == $action)
211    {
212      validate_user_comment($comment_id);
213      $perform_redirect = true;
214    }
[5199]215
[5195]216    if ('edit' == $action)
217    {
218      if (!empty($_POST['content']))
219      {
220        update_user_comment(
221          array(
222            'comment_id' => $_GET['edit'],
223            'image_id' => $_POST['image_id'],
224            'content' => $_POST['content']
225            ),
226          $_POST['key']
227          );
[5199]228
[5195]229        $edit_comment = null;
230      }
231      else
232      {
233        $edit_comment = $_GET['edit'];
234      }
235    }
[5199]236
[5195]237    if ($perform_redirect)
238    {
239      $redirect_url =
240        PHPWG_ROOT_PATH
241        .'comments.php'
242        .get_query_string_diff(array('delete','validate','pwg_token'));
[5199]243
[5195]244      redirect($redirect_url);
245    }
[3445]246  }
247}
248
[579]249// +-----------------------------------------------------------------------+
250// |                       page header and options                         |
251// +-----------------------------------------------------------------------+
[355]252
[2268]253$title= l10n('User comments');
[850]254$page['body_id'] = 'theCommentsPage';
255
[579]256$template->set_filenames(array('comments'=>'comments.tpl'));
[2223]257$template->assign(
[579]258  array(
[796]259    'F_ACTION'=>PHPWG_ROOT_PATH.'comments.php',
[4182]260    'F_KEYWORD'=> @htmlspecialchars(stripslashes($_GET['keyword'], ENT_QUOTES, 'utf-8')),
261    'F_AUTHOR'=> @htmlspecialchars(stripslashes($_GET['author'], ENT_QUOTES, 'utf-8')),
[579]262    )
263  );
[355]264
[796]265// +-----------------------------------------------------------------------+
266// |                          form construction                            |
267// +-----------------------------------------------------------------------+
268
269// Search in a particular category
[2223]270$blockname = 'categories';
[796]271
272$query = '
[1861]273SELECT id, name, uppercats, global_rank
[1677]274  FROM '.CATEGORIES_TABLE.'
275'.get_sql_condition_FandF
276  (
277    array
278      (
279        'forbidden_categories' => 'id',
280        'visible_categories' => 'id'
281      ),
282    'WHERE'
283  ).'
[796]284;';
285display_select_cat_wrapper($query, array(@$_GET['cat']), $blockname, true);
286
287// Filter on recent comments...
[2223]288$tpl_var=array();
[796]289foreach ($since_options as $id => $option)
290{
[2223]291  $tpl_var[ $id ] = $option['label'];
[355]292}
[2223]293$template->assign( 'since_options', $tpl_var);
294$template->assign( 'since_options_selected', $page['since']);
[796]295
296// Sort by
[2223]297$template->assign( 'sort_by_options', $sort_by);
298$template->assign( 'sort_by_options_selected', $page['sort_by']);
[796]299
300// Sorting order
[2223]301$template->assign( 'sort_order_options', $sort_order);
302$template->assign( 'sort_order_options_selected', $page['sort_order']);
[796]303
304
305// Number of items
306$blockname = 'items_number_option';
[2223]307$tpl_var=array();
[796]308foreach ($items_number as $option)
309{
[2223]310  $tpl_var[ $option ] = is_numeric($option) ? $option : l10n($option);
[796]311}
[2223]312$template->assign( 'item_number_options', $tpl_var);
313$template->assign( 'item_number_options_selected', $page['items_number']);
[796]314
[2223]315
[579]316// +-----------------------------------------------------------------------+
[796]317// |                            navigation bar                             |
318// +-----------------------------------------------------------------------+
319
320if (isset($_GET['start']) and is_numeric($_GET['start']))
321{
322  $start = $_GET['start'];
323}
324else
325{
326  $start = 0;
327}
328
329$query = '
[3450]330SELECT COUNT(DISTINCT(com.id))
[796]331  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
[5199]332    INNER JOIN '.COMMENTS_TABLE.' AS com
[796]333    ON ic.image_id = com.image_id
[4139]334    LEFT JOIN '.USERS_TABLE.' As u
335    ON u.'.$conf['user_fields']['id'].' = com.author_id
[1716]336  WHERE '.implode('
337    AND ', $page['where_clauses']).'
[796]338;';
[4325]339list($counter) = pwg_db_fetch_row(pwg_query($query));
[796]340
[1598]341$url = PHPWG_ROOT_PATH
342    .'comments.php'
[5195]343  .get_query_string_diff(array('start','delete','validate','pwg_token'));
[796]344
345$navbar = create_navigation_bar($url,
346                                $counter,
347                                $start,
348                                $page['items_number'],
349                                '');
350
[3172]351$template->assign('navbar', $navbar);
[796]352
353// +-----------------------------------------------------------------------+
[579]354// |                        last comments display                          |
355// +-----------------------------------------------------------------------+
[355]356
[796]357$comments = array();
358$element_ids = array();
359$category_ids = array();
360
[579]361$query = '
[796]362SELECT com.id AS comment_id
363     , com.image_id
364     , ic.category_id
365     , com.author
[3450]366     , com.author_id
[796]367     , com.date
368     , com.content
[1598]369     , com.validated
[796]370  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
[3487]371    INNER JOIN '.COMMENTS_TABLE.' AS com
[796]372    ON ic.image_id = com.image_id
[4139]373    LEFT JOIN '.USERS_TABLE.' As u
374    ON u.'.$conf['user_fields']['id'].' = com.author_id
[1716]375  WHERE '.implode('
376    AND ', $page['where_clauses']).'
[5199]377  GROUP BY comment_id
[796]378  ORDER BY '.$page['sort_by'].' '.$page['sort_order'];
379if ('all' != $page['items_number'])
380{
381  $query.= '
[4334]382  LIMIT '.$page['items_number'].' OFFSET '.$start;
[796]383}
384$query.= '
[579]385;';
[587]386$result = pwg_query($query);
[4325]387while ($row = pwg_db_fetch_assoc($result))
[393]388{
[796]389  array_push($comments, $row);
390  array_push($element_ids, $row['image_id']);
391  array_push($category_ids, $row['category_id']);
[393]392}
[796]393
394if (count($comments) > 0)
[579]395{
[796]396  // retrieving element informations
397  $elements = array();
[579]398  $query = '
[796]399SELECT id, name, file, path, tn_ext
[579]400  FROM '.IMAGES_TABLE.'
[796]401  WHERE id IN ('.implode(',', $element_ids).')
[579]402;';
[796]403  $result = pwg_query($query);
[4325]404  while ($row = pwg_db_fetch_assoc($result))
[579]405  {
[796]406    $elements[$row['id']] = $row;
[579]407  }
[721]408
[796]409  // retrieving category informations
[579]410  $query = '
[1866]411SELECT id, name, permalink, uppercats
[796]412  FROM '.CATEGORIES_TABLE.'
413  WHERE id IN ('.implode(',', $category_ids).')
414;';
[1866]415  $categories = hash_from_query($query, 'id');
[796]416
417  foreach ($comments as $comment)
[579]418  {
[796]419    if (!empty($elements[$comment['image_id']]['name']))
[166]420    {
[1598]421      $name=$elements[$comment['image_id']]['name'];
[166]422    }
[796]423    else
424    {
[1598]425      $name=get_name_from_file($elements[$comment['image_id']]['file']);
[796]426    }
[1090]427
[796]428    // source of the thumbnail picture
[1598]429    $thumbnail_src = get_thumbnail_url( $elements[$comment['image_id']] );
[1090]430
[796]431    // link to the full size picture
[1090]432    $url = make_picture_url(
[796]433      array(
[5195]434        'category' => $categories[ $comment['category_id'] ],
435        'image_id' => $comment['image_id'],
436        'image_file' => $elements[$comment['image_id']]['file'],
437        )
438      );
[5199]439
[5195]440    $tpl_comment = array(
441      'U_PICTURE' => $url,
442      'TN_SRC' => $thumbnail_src,
443      'ALT' => $name,
444      'AUTHOR' => trigger_event('render_comment_author', $comment['author']),
445      'DATE'=>format_date($comment['date'], true),
446      'CONTENT'=>trigger_event('render_comment_content',$comment['content']),
447      );
[1598]448
[3487]449    if (can_manage_comment('delete', $comment['author_id']))
[1598]450    {
[5195]451      $url =
452        get_root_url()
453        .'comments.php'
454        .get_query_string_diff(array('delete','validate','edit', 'pwg_token'));
[5199]455
[5195]456      $tpl_comment['U_DELETE'] = add_url_params(
457        $url,
458        array(
459          'delete' => $comment['comment_id'],
460          'pwg_token' => get_pwg_token(),
461          )
462        );
[3445]463    }
[5199]464
[3450]465    if (can_manage_comment('edit', $comment['author_id']))
[3445]466    {
[5195]467      $url =
468        get_root_url()
469        .'comments.php'
470        .get_query_string_diff(array('edit', 'delete','validate', 'pwg_token'));
[5199]471
[5195]472      $tpl_comment['U_EDIT'] = add_url_params(
473        $url,
474        array(
475          'edit' => $comment['comment_id'],
476          'pwg_token' => get_pwg_token(),
477          )
478        );
[5199]479
[3487]480      if (isset($edit_comment) and ($comment['comment_id'] == $edit_comment))
[1598]481      {
[3445]482        $tpl_comment['IN_EDIT'] = true;
483        $key = get_comment_post_key($comment['image_id']);
484        $tpl_comment['KEY'] = $key;
485        $tpl_comment['IMAGE_ID'] = $comment['image_id'];
486        $tpl_comment['CONTENT'] = $comment['content'];
[1598]487      }
488    }
[3445]489
[5195]490    if (can_manage_comment('validate', $comment['author_id']))
[3445]491    {
[5195]492      if ('true' != $comment['validated'])
493      {
494        $tpl_comment['U_VALIDATE'] = add_url_params(
495          $url,
496          array(
497            'validate'=> $comment['comment_id'],
498            'pwg_token' => get_pwg_token(),
499            )
500          );
501      }
[3445]502    }
[2223]503    $template->append('comments', $tpl_comment);
[166]504  }
[579]505}
506// +-----------------------------------------------------------------------+
507// |                           html code display                           |
508// +-----------------------------------------------------------------------+
[2107]509include(PHPWG_ROOT_PATH.'include/page_header.php');
[2223]510$template->pparse('comments');
[1598]511include(PHPWG_ROOT_PATH.'include/page_tail.php');
[2107]512?>
Note: See TracBrowser for help on using the repository browser.