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

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

update for 2.4
delete useless admin page
now compatible with RV Thumb Scroller

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