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

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

feature:2538 Make a unified messages management
use only $pageinfos and $pageerrors vars and and necessary template to all main pages

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