source: extensions/Comments_on_Albums/include/coa_albums.php @ 18927

Last change on this file since 18927 was 18927, checked in by mistic100, 11 years ago

update with Piwigo 2.5 features (website url, email, mandatory email and username)

File size: 11.7 KB
Line 
1<?php
2/* Code adapted from include/picture_comment.inc.php and picture.php */
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5global $conf, $user;
6
7// +-----------------------------------------------------------------------+
8// |                            category infos                             |
9// +-----------------------------------------------------------------------+
10$category = $page['category'];
11
12$url_self = duplicate_index_url(array(
13  'category' => array(
14    'id' => $category['id'], 
15    'name' => $category['name'], 
16    'permalink' => $category['permalink']
17    ), 
18  array('start')
19  ));
20
21
22// +-----------------------------------------------------------------------+
23// |                                actions                                |
24// +-----------------------------------------------------------------------+
25if (isset($_GET['action'])) 
26{
27  switch ($_GET['action']) 
28  {
29    case 'edit_comment' : 
30    {
31      include_once(COA_PATH.'include/functions_comment.inc.php'); // custom fonctions
32      check_input_parameter('comment_to_edit', $_GET, false, PATTERN_ID);
33      $author_id = get_comment_author_id_albums($_GET['comment_to_edit']);
34
35      if (can_manage_comment('edit', $author_id)) 
36      {
37        if (!empty($_POST['content'])) 
38        {
39          check_pwg_token();
40          $comment_action = update_user_comment_albums(
41            array(
42              'comment_id' => $_GET['comment_to_edit'],
43              'category_id' => $category['id'],
44              'content' => $_POST['content']
45              ),
46            $_POST['key']
47            );
48         
49          $perform_redirect = false;
50          switch ($comment_action)
51          {
52            case 'moderate':
53              $_SESSION['page_infos'][] = l10n('An administrator must authorize your comment before it is visible.');
54            case 'validate':
55              $_SESSION['page_infos'][] = l10n('Your comment has been registered');
56              $perform_redirect = true;
57              break;
58            case 'reject':
59              $_SESSION['page_errors'][] = l10n('Your comment has NOT been registered because it did not pass the validation rules');
60              $perform_redirect = true;
61              break;
62            default:
63              trigger_error('Invalid comment action '.$comment_action, E_USER_WARNING);
64          }
65
66          if ($perform_redirect)
67          {
68            redirect($url_self);
69          }
70          unset($_POST['content']);
71        } 
72        else 
73        {
74          $edit_comment = $_GET['comment_to_edit'];
75        }
76        break;
77      }
78    }
79    case 'delete_comment' : 
80    {
81      check_pwg_token();
82     
83      include_once(COA_PATH.'include/functions_comment.inc.php'); // custom fonctions
84     
85      check_input_parameter('comment_to_delete', $_GET, false, PATTERN_ID);
86     
87      $author_id = get_comment_author_id_albums($_GET['comment_to_delete']);
88
89      if (can_manage_comment('delete', $author_id)) 
90      {
91        delete_user_comment_albums($_GET['comment_to_delete']);
92      }
93
94      redirect($url_self);
95    }
96    case 'validate_comment' : 
97    {
98      check_pwg_token();
99     
100      include_once(COA_PATH.'include/functions_comment.inc.php'); // custom fonctions
101     
102      check_input_parameter('comment_to_validate', $_GET, false, PATTERN_ID);
103     
104      $author_id = get_comment_author_id_albums($_GET['comment_to_validate']);
105
106      if (can_manage_comment('validate', $author_id)) 
107      {
108        validate_user_comment_albums($_GET['comment_to_validate']);
109      }
110
111      redirect($url_self);
112    }
113  }
114}
115
116
117// +-----------------------------------------------------------------------+
118// |                            insert comment                             |
119// +-----------------------------------------------------------------------+
120if ($category['commentable'] and isset($_POST['content'])) 
121{
122  if (is_a_guest() and !$conf['comments_forall']) 
123  {
124    die('Session expired');
125  }
126 
127  $comm = array(
128    'author' => trim( @$_POST['author'] ),
129    'content' => trim( $_POST['content'] ),
130    'website_url' => trim( $_POST['website_url'] ),
131    'email' => trim( @$_POST['email'] ),
132    'category_id' => $category['id'],
133   );
134
135  include_once(COA_PATH.'include/functions_comment.inc.php'); // custom fonctions
136 
137  $comment_action = insert_user_comment_albums($comm, @$_POST['key'], $page['errors']);
138
139  switch ($comment_action) 
140  {
141    case 'moderate':
142      array_push($page['infos'], l10n('An administrator must authorize your comment before it is visible.'));
143    case 'validate':
144      array_push($page['infos'], l10n('Your comment has been registered'));
145      break;
146    case 'reject':
147      set_status_header(403);
148      array_push($page['errors'], l10n('Your comment has NOT been registered because it did not pass the validation rules'));
149      break;
150    default:
151      trigger_error('Invalid comment action '.$comment_action, E_USER_WARNING);
152  }
153   
154  // allow plugins to notify what's going on
155  trigger_action( 'user_comment_insertion',
156      array_merge($comm, array('action'=>$comment_action) )
157    );
158   
159  $template->assign('DISPLAY_COMMENTS_BLOCK', true);
160} 
161else if (isset($_POST['content'])) 
162{
163  set_status_header(403);
164  die('ugly spammer');
165}
166
167
168// +-----------------------------------------------------------------------+
169// |                           display comments                            |
170// +-----------------------------------------------------------------------+
171if ($category['commentable']) 
172{
173  if (!is_admin()) 
174  {
175    $validated_clause = " AND validated = 'true'";
176  } 
177  else 
178  {
179    $validated_clause = null;
180  }
181
182  // number of comments for this category
183  $query = '
184SELECT
185    COUNT(*) AS nb_comments
186  FROM '.COA_TABLE.'
187  WHERE category_id = '.$category['id']
188  .$validated_clause.'
189;';
190  $row = pwg_db_fetch_assoc(pwg_query($query));
191
192  // navigation bar creation, can't use $_GET['start'] because used by thumbnails navigation bar
193  if (isset($_GET['start_comments'])) 
194  {
195    $page['start_comments'] = $_GET['start_comments'];
196  } 
197  else 
198  {
199    $page['start_comments'] = 0;
200  }
201  include_once(COA_PATH.'include/functions.inc.php'); // custom fonctions
202
203  $navigation_bar = create_comment_navigation_bar(
204    duplicate_index_url(array(), array('start')),
205    $row['nb_comments'],
206    $page['start_comments'],
207    $conf['nb_comment_page']
208    );
209
210  $template->assign(
211    array(
212      'COMMENT_COUNT' => $row['nb_comments'],
213      'comment_navbar' => $navigation_bar,
214      )
215    );
216
217  if ($row['nb_comments'] > 0) 
218  {
219    // comments order (get, session, conf)
220    if (!empty($_GET['comments_order']) && in_array(strtoupper($_GET['comments_order']), array('ASC', 'DESC')))
221    {
222      pwg_set_session_var('comments_order', $_GET['comments_order']);
223    }
224    $comments_order = pwg_get_session_var('comments_order', $conf['comments_order']);
225
226    $template->assign(array(
227      'COMMENTS_ORDER_URL' => add_url_params( duplicate_index_url(), array('comments_order'=> ($comments_order == 'ASC' ? 'DESC' : 'ASC') ) ),
228      'COMMENTS_ORDER_TITLE' => $comments_order == 'ASC' ? l10n('Show latest comments first') : l10n('Show oldest comments first'),
229      ));
230     
231    // get comments
232    $query = '
233SELECT
234    com.id,
235    com.author,
236    com.author_id,
237    u.'.$conf['user_fields']['username'].' AS username,
238    u.'.$conf['user_fields']['email'].' AS user_email,
239    com.email,
240    com.date,
241    com.website_url,
242    com.category_id,
243    com.content,
244    com.validated
245  FROM '.COA_TABLE.' AS com
246  LEFT JOIN '.USERS_TABLE.' AS u
247    ON u.'.$conf['user_fields']['id'].' = author_id
248  WHERE category_id = '.$category['id'].'
249    '.$validated_clause.'
250  ORDER BY date '.$comments_order.'
251  LIMIT '.$conf['nb_comment_page'].' OFFSET '.$page['start_comments'].'
252;';
253    $result = pwg_query($query);
254
255    while ($row = pwg_db_fetch_assoc($result)) 
256    {
257      if ($row['author'] == 'guest')
258      {
259        $row['author'] = l10n('guest');
260      }
261     
262      $email = null;
263      if (!empty($row['user_email']))
264      {
265        $email = $row['user_email'];
266      }
267      else if (!empty($row['email']))
268      {
269        $email = $row['email'];
270      }
271     
272      // comment content
273      $tpl_comment = array(
274        'ID' => $row['id'],
275        'AUTHOR' => trigger_event('render_comment_author', $row['author']),
276        'DATE' => format_date($row['date'], true),
277        'WEBSITE_URL' => $row['website_url'],
278        'CONTENT' => trigger_event('render_comment_content', $row['content'], 'album'),
279        );
280     
281      // rights
282      if (can_manage_comment('delete', $row['author_id'])) 
283      {
284        $tpl_comment['U_DELETE'] = add_url_params(
285          $url_self, 
286          array(
287            'action' => 'delete_comment',
288            'comment_to_delete' => $row['id'],
289            'pwg_token' => get_pwg_token(),
290            )
291          );
292      }
293      if (can_manage_comment('edit', $row['author_id'])) 
294      {
295        $tpl_comment['U_EDIT'] = add_url_params(
296          $url_self, 
297          array(
298            'action' => 'edit_comment',
299            'comment_to_edit' => $row['id'],
300            )
301          );
302        if (isset($edit_comment) and ($row['id'] == $edit_comment)) 
303        {
304          $tpl_comment['IN_EDIT'] = true;
305          $key = get_ephemeral_key(2, $category['id']);
306          $tpl_comment['KEY'] = $key;
307          $tpl_comment['CONTENT'] = $row['content'];
308          $tpl_comment['PWG_TOKEN'] = get_pwg_token();
309        }
310      }
311      if (is_admin())
312      {
313        $tpl_comment['EMAIL'] = $email;
314       
315        if ($row['validated'] != 'true') 
316        {
317          $tpl_comment['U_VALIDATE'] = add_url_params(
318            $url_self, 
319            array(
320              'action' => 'validate_comment',
321              'comment_to_validate' => $row['id'],
322              'pwg_token' => get_pwg_token(),
323              )
324            );
325        }
326      }
327     
328      $template->append('comments', $tpl_comment);
329    }
330  }
331
332  // comment form
333  $show_add_comment_form = true;
334  if (isset($edit_comment)) 
335  {
336    $show_add_comment_form = false;
337  }
338  if (is_a_guest() and !$conf['comments_forall']) 
339  {
340    $show_add_comment_form = false;
341  }
342
343  if ($show_add_comment_form) 
344  {
345    $key = get_ephemeral_key(3, $category['id']);
346   
347    $template->assign('comment_add', 
348      array(
349        'F_ACTION' =>         $url_self,
350        'KEY' =>              $key,
351        'CONTENT' =>          stripslashes(@$_POST['content']),
352        'SHOW_AUTHOR' =>      !is_classic_user(),
353        'AUTHOR_MANDATORY' => $conf['comments_author_mandatory'],
354        'AUTHOR' =>           stripslashes(@$_POST['author']),
355        'WEBSITE_URL' =>      stripslashes(@$_POST['website_url']),
356        'SHOW_EMAIL' =>       !is_classic_user() or empty($user['email']),
357        'EMAIL_MANDATORY' =>  $conf['comments_email_mandatory'],
358        'EMAIL' =>            stripslashes(@$_POST['email']), 
359        )
360      );
361  }
362 
363  // template
364  $template->assign(array(
365    'COA_PATH' => COA_PATH, // for css
366    'COA_ABSOLUTE_PATH' => dirname(__FILE__) .'/../', // for template
367    ));
368 
369  $template->set_filename('comments_on_albums', dirname(__FILE__) .'/../template/albums.tpl');
370  if (isset($pwg_loaded_plugins['rv_tscroller']) AND count($page['navigation_bar']) != 0)
371  {
372    $template->assign('COMMENTS_ON_TOP', true);
373    $template->concat('PLUGIN_INDEX_CONTENT_BEGIN', $template->parse('comments_on_albums', true));
374  }
375  else
376  {
377    $template->concat('PLUGIN_INDEX_CONTENT_END', $template->parse('comments_on_albums', true));
378  }
379}
380
381?>
Note: See TracBrowser for help on using the repository browser.