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

Last change on this file since 14290 was 13865, checked in by rvelices, 13 years ago
  • comment edit form looks ok now with the new comment layout
  • removed unused css / simplify it
  • simplified jquery drop boxes ...
  • Property svn:eol-style set to LF
File size: 7.6 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    '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']) && in_array(strtoupper($_GET['comments_order']), array('ASC', 'DESC')))
130    {
131      pwg_set_session_var('comments_order', $_GET['comments_order']);
132    }
133    $comments_order = pwg_get_session_var('comments_order', $conf['comments_order']);
134
135    $template->assign(array(
136      'COMMENTS_ORDER_URL' => add_url_params( duplicate_picture_url(), array('comments_order'=> ($comments_order == 'ASC' ? 'DESC' : 'ASC') ) ),
137      'COMMENTS_ORDER_TITLE' => $comments_order == 'ASC' ? l10n('Show latest comments first') : l10n('Show oldest comments first'),
138      ));
139       
140    $query = '
141SELECT
142    com.id,
143    author,
144    author_id,
145    '.$conf['user_fields']['username'].' AS username,
146    date,
147    image_id,
148    content,
149    validated
150  FROM '.COMMENTS_TABLE.' AS com
151  LEFT JOIN '.USERS_TABLE.' AS u
152    ON u.'.$conf['user_fields']['id'].' = author_id
153  WHERE image_id = '.$page['image_id'].'
154    '.$validated_clause.'
155  ORDER BY date '.$comments_order.'
156  LIMIT '.$conf['nb_comment_page'].' OFFSET '.$page['start'].'
157;';
158    $result = pwg_query( $query );
159
160    while ($row = pwg_db_fetch_assoc($result))
161    {
162      if (!empty($row['author']))
163      {
164        $author = $row['author'];
165        if ($author == 'guest')
166        {
167          $author = l10n('guest');
168        }
169      }
170      else
171      {
172        $author = stripslashes($row['username']);
173      }
174
175      $tpl_comment =
176        array(
177          'ID' => $row['id'],
178          'AUTHOR' => trigger_event('render_comment_author', $author),
179          'DATE' => format_date($row['date'], true),
180          'CONTENT' => trigger_event('render_comment_content',$row['content']),
181        );
182
183      if (can_manage_comment('delete', $row['author_id']))
184      {
185        $tpl_comment['U_DELETE'] = add_url_params(
186          $url_self,
187          array(
188            'action'=>'delete_comment',
189            'comment_to_delete'=>$row['id'],
190            'pwg_token' => get_pwg_token(),
191            )
192          );
193      }
194      if (can_manage_comment('edit', $row['author_id']))
195      {
196        $tpl_comment['U_EDIT'] = add_url_params(
197          $url_self,
198          array(
199            'action'=>'edit_comment',
200            'comment_to_edit'=>$row['id'],
201            )
202          );
203          if (isset($edit_comment) and ($row['id'] == $edit_comment))
204          {
205            $tpl_comment['IN_EDIT'] = true;
206            $key = get_ephemeral_key(2, $page['image_id']);
207            $tpl_comment['KEY'] = $key;
208            $tpl_comment['CONTENT'] = $row['content'];
209            $tpl_comment['PWG_TOKEN'] = get_pwg_token();
210          }
211      }
212      if (is_admin())
213      {
214        if ($row['validated'] != 'true')
215        {
216          $tpl_comment['U_VALIDATE'] = add_url_params(
217                  $url_self,
218                  array(
219                    'action' => 'validate_comment',
220                    'comment_to_validate' => $row['id'],
221                    'pwg_token' => get_pwg_token(),
222                    )
223                  );
224        }
225      }
226      $template->append('comments', $tpl_comment);
227    }
228  }
229
230  $show_add_comment_form = true;
231  if (isset($edit_comment))
232  {
233    $show_add_comment_form = false;
234  }
235  if (is_a_guest() and !$conf['comments_forall'])
236  {
237    $show_add_comment_form = false;
238  }
239
240  if ($show_add_comment_form)
241  {
242    $key = get_ephemeral_key(3, $page['image_id']);
243    $content = '';
244    if ('reject'===@$comment_action)
245    {
246      $content = htmlspecialchars( stripslashes($comm['content']) );
247    }
248    $template->assign('comment_add',
249        array(
250          'F_ACTION' => $url_self,
251          'KEY' => $key,
252          'CONTENT' => $content,
253          'SHOW_AUTHOR' => !is_classic_user()
254        ));
255  }
256}
257
258?>
Note: See TracBrowser for help on using the repository browser.