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

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

big code cleaning

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