source: trunk/include/picture_comment.inc.php @ 17351

Last change on this file since 17351 was 17351, checked in by mistic100, 12 years ago

feature 2380: add URL for user comment

  • Property svn:eol-style set to LF
File size: 8.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 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 * This file is included by the picture page to manage user comments
26 *
27 */
28
29// the picture is commentable if it belongs at least to one category which
30// is commentable
31$page['show_comments'] = false;
32foreach ($related_categories as $category)
33{
34  if ($category['commentable'])
35  {
36    $page['show_comments'] = true;
37    break;
38  }
39}
40
41if ( $page['show_comments'] and isset( $_POST['content'] ) )
42{
43  if ( is_a_guest() and !$conf['comments_forall'] )
44  {
45    die ('Session expired');
46  }
47
48  $comm = array(
49    'author' => trim( @$_POST['author'] ),
50    'content' => trim( $_POST['content'] ),
51    'website_url' => trim( $_POST['website_url'] ),
52    'image_id' => $page['image_id'],
53   );
54
55  include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php');
56
57  $comment_action = insert_user_comment($comm, @$_POST['key'], $page['infos']);
58
59  switch ($comment_action)
60  {
61    case 'moderate':
62      array_push($page['infos'], l10n('An administrator must authorize your comment before it is visible.') );
63    case 'validate':
64      array_push($page['infos'], l10n('Your comment has been registered'));
65      break;
66    case 'reject':
67      set_status_header(403);
68      array_push($page['errors'], l10n('Your comment has NOT been registered because it did not pass the validation rules') );
69      break;
70    default:
71      trigger_error('Invalid comment action '.$comment_action, E_USER_WARNING);
72  }
73
74  // allow plugins to notify what's going on
75  trigger_action( 'user_comment_insertion',
76      array_merge($comm, array('action'=>$comment_action) )
77    );
78}
79elseif ( isset($_POST['content']) )
80{
81  set_status_header(403);
82  die('ugly spammer');
83}
84
85if ($page['show_comments'])
86{
87  if ( !is_admin() )
88  {
89    $validated_clause = '  AND validated = \'true\'';
90  }
91  else
92  {
93    $validated_clause = '';
94  }
95
96  // number of comments for this picture
97  $query = '
98SELECT
99    COUNT(*) AS nb_comments
100  FROM '.COMMENTS_TABLE.'
101  WHERE image_id = '.$page['image_id']
102  .$validated_clause.'
103;';
104  $row = pwg_db_fetch_assoc( pwg_query( $query ) );
105
106  // navigation bar creation
107  if (!isset($page['start']))
108  {
109    $page['start'] = 0;
110  }
111
112  $navigation_bar = create_navigation_bar(
113    duplicate_picture_url(array(), array('start')),
114    $row['nb_comments'],
115    $page['start'],
116    $conf['nb_comment_page'],
117    true // We want a clean URL
118    );
119
120  $template->assign(
121    array(
122      'COMMENT_COUNT' => $row['nb_comments'],
123      'navbar' => $navigation_bar,
124      )
125    );
126
127  if ($row['nb_comments'] > 0)
128  {
129    // comments order (get, session, conf)
130    if (!empty($_GET['comments_order']) && in_array(strtoupper($_GET['comments_order']), array('ASC', 'DESC')))
131    {
132      pwg_set_session_var('comments_order', $_GET['comments_order']);
133    }
134    $comments_order = pwg_get_session_var('comments_order', $conf['comments_order']);
135
136    $template->assign(array(
137      'COMMENTS_ORDER_URL' => add_url_params( duplicate_picture_url(), array('comments_order'=> ($comments_order == 'ASC' ? 'DESC' : 'ASC') ) ),
138      'COMMENTS_ORDER_TITLE' => $comments_order == 'ASC' ? l10n('Show latest comments first') : l10n('Show oldest comments first'),
139      ));
140       
141    $query = '
142SELECT
143    com.id,
144    author,
145    author_id,
146    '.$conf['user_fields']['username'].' AS username,
147    date,
148    image_id,
149    website_url,
150    content,
151    validated
152  FROM '.COMMENTS_TABLE.' AS com
153  LEFT JOIN '.USERS_TABLE.' AS u
154    ON u.'.$conf['user_fields']['id'].' = author_id
155  WHERE image_id = '.$page['image_id'].'
156    '.$validated_clause.'
157  ORDER BY date '.$comments_order.'
158  LIMIT '.$conf['nb_comment_page'].' OFFSET '.$page['start'].'
159;';
160    $result = pwg_query( $query );
161
162    while ($row = pwg_db_fetch_assoc($result))
163    {
164      if (!empty($row['author']))
165      {
166        $author = $row['author'];
167        if ($author == 'guest')
168        {
169          $author = l10n('guest');
170        }
171      }
172      else
173      {
174        $author = stripslashes($row['username']);
175      }
176
177      $tpl_comment =
178        array(
179          'ID' => $row['id'],
180          'AUTHOR' => trigger_event('render_comment_author', $author),
181          'DATE' => format_date($row['date'], true),
182          'CONTENT' => trigger_event('render_comment_content',$row['content']),
183          'WEBSITE_URL' => $row['website_url'],
184        );
185
186      if (can_manage_comment('delete', $row['author_id']))
187      {
188        $tpl_comment['U_DELETE'] = add_url_params(
189          $url_self,
190          array(
191            'action'=>'delete_comment',
192            'comment_to_delete'=>$row['id'],
193            'pwg_token' => get_pwg_token(),
194            )
195          );
196      }
197      if (can_manage_comment('edit', $row['author_id']))
198      {
199        $tpl_comment['U_EDIT'] = add_url_params(
200          $url_self,
201          array(
202            'action'=>'edit_comment',
203            'comment_to_edit'=>$row['id'],
204            )
205          );
206          if (isset($edit_comment) and ($row['id'] == $edit_comment))
207          {
208            $tpl_comment['IN_EDIT'] = true;
209            $key = get_ephemeral_key(2, $page['image_id']);
210            $tpl_comment['KEY'] = $key;
211            $tpl_comment['CONTENT'] = $row['content'];
212            $tpl_comment['PWG_TOKEN'] = get_pwg_token();
213            $tpl_comment['U_CANCEL'] = $url_self;
214          }
215      }
216      if (is_admin())
217      {
218        if ($row['validated'] != 'true')
219        {
220          $tpl_comment['U_VALIDATE'] = add_url_params(
221                  $url_self,
222                  array(
223                    'action' => 'validate_comment',
224                    'comment_to_validate' => $row['id'],
225                    'pwg_token' => get_pwg_token(),
226                    )
227                  );
228        }
229      }
230      $template->append('comments', $tpl_comment);
231    }
232  }
233
234  $show_add_comment_form = true;
235  if (isset($edit_comment))
236  {
237    $show_add_comment_form = false;
238  }
239  if (is_a_guest() and !$conf['comments_forall'])
240  {
241    $show_add_comment_form = false;
242  }
243
244  if ($show_add_comment_form)
245  {
246    $key = get_ephemeral_key(3, $page['image_id']);
247    $content = $author = $website_url = '';
248    if ('reject'===@$comment_action)
249    {
250      $content = htmlspecialchars( stripslashes($comm['content']) );
251      $author = htmlspecialchars( stripslashes($comm['author']) );
252      $website_url = htmlspecialchars( stripslashes($comm['website_url']) );
253    }
254    $template->assign('comment_add',
255        array(
256          'F_ACTION' => $url_self,
257          'KEY' => $key,
258          'CONTENT' => $content,
259          'SHOW_AUTHOR' => !is_classic_user(),
260          'AUTHOR' => $author ,
261          'WEBSITE_URL' => $website_url, 
262        ));
263  }
264}
265
266?>
Note: See TracBrowser for help on using the repository browser.