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

Last change on this file since 12917 was 12894, checked in by mistic100, 13 years ago

feaure:2379 option to display user comments sorted new>old instead of old>new

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