source: extensions/Comments_on_Albums/trunk/include/coa_comments_page.php @ 26848

Last change on this file since 26848 was 26848, checked in by mistic100, 10 years ago

keep old trigger functions (PHP 5.2 issue)

File size: 10.6 KB
Line 
1<?php
2/* inspired by comments.php */
3defined('COA_ID') or die('Hacking attempt!');
4
5global $template, $page, $conf, $user;
6
7// +-----------------------------------------------------------------------+
8// |                    add a button for switch page                       |
9// +-----------------------------------------------------------------------+
10$template->assign('COA_MODE', (isset($_GET['display_mode']) and $_GET['display_mode'] == 'albums') ? 'albums' : 'photos');
11$template->set_prefilter('comments', 'coa_add_button');
12
13function coa_add_button($content, &$smarty)
14{
15  $search ='<form class="filter" action="{$F_ACTION}" method="get">';
16
17  $add = '
18<fieldset>
19  <legend>{\'Display comments on\'|@translate}</legend>
20  <span style="font-size:1.1em;"><a href="comments.php" {if $COA_MODE=="photos"}style="font-weight:bold;"{/if}>{\'Photos\'|@translate}</a> |
21  <a href="comments.php?display_mode=albums" {if $COA_MODE=="albums"}style="font-weight:bold;"{/if}>{\'Albums\'|@translate}</a></span>
22</fieldset>';
23
24  return str_replace($search, $add.$search, $content);
25}
26
27
28// +-----------------------------------------------------------------------+
29//                        comments on albums page                          |
30// +-----------------------------------------------------------------------+
31if (!isset($_GET['display_mode']) or $_GET['display_mode'] != 'albums')
32{
33  return;
34}
35
36$url_self = PHPWG_ROOT_PATH.'comments.php'
37  .get_query_string_diff(array('edit_albums','delete_albums','validate_albums','pwg_token'));
38
39// reset some template vars
40$template->clear_assign(array('comments', 'navbar', 'sort_by_options'));
41
42// sort_by : database fields proposed for sorting comments list
43global $sort_by;
44$sort_by = array(
45  'date' => l10n('comment date'),
46  'category_id' => l10n('Album')
47  );
48$template->assign('sort_by_options', $sort_by);
49
50// clean where_clauses from unknown column
51foreach ($page['where_clauses'] as &$cond)
52{
53  if (strpos($cond, 'ic.image_id') !== false)
54  {
55    $cond = get_sql_condition_FandF(
56      array(
57        'forbidden_categories' => 'category_id',
58        'visible_categories' => 'category_id'
59        ),
60      '', true
61      );
62  }
63}
64unset($cond);
65
66// +-----------------------------------------------------------------------+
67// |                         comments management                           |
68// +-----------------------------------------------------------------------+
69
70$comment_id = null;
71$action = null;
72
73$actions = array('delete_albums', 'validate_albums', 'edit_albums');
74foreach ($actions as $loop_action)
75{
76  if (isset($_GET[$loop_action]))
77  {
78    $action = $loop_action;
79    check_input_parameter($action, $_GET, false, PATTERN_ID);
80    $comment_id = $_GET[$action];
81    break;
82  }
83}
84
85if (isset($action))
86{
87  include_once(COA_PATH.'include/functions_comment.inc.php');
88
89  $comment_author_id = get_comment_author_id_albums($comment_id);
90
91  if (can_manage_comment(str_replace('_albums', null, $action), $comment_author_id))
92  {
93    $perform_redirect = false;
94
95    if ('delete_albums' == $action)
96    {
97      check_pwg_token();
98      delete_user_comment_albums($comment_id);
99      $perform_redirect = true;
100    }
101    if ('validate_albums' == $action)
102    {
103      check_pwg_token();
104      validate_user_comment_albums($comment_id);
105      $perform_redirect = true;
106    }
107    if ('edit_albums' == $action)
108    {
109      if (!empty($_POST['content']))
110      {
111        check_pwg_token();
112        $comment_action = update_user_comment_albums(
113          array(
114            'comment_id' => $_GET['edit_albums'],
115            'category_id' => $_POST['image_id'],
116            'content' => $_POST['content'],
117            'website_url' => @$_POST['website_url'],
118            ),
119          $_POST['key']
120          );
121
122        switch ($comment_action)
123        {
124          case 'moderate':
125            $_SESSION['page_infos'][] = l10n('An administrator must authorize your comment before it is visible.');
126          case 'validate':
127            $_SESSION['page_infos'][] = l10n('Your comment has been registered');
128            $perform_redirect = true;
129            break;
130          case 'reject':
131            $_SESSION['page_errors'][] = l10n('Your comment has NOT been registered because it did not pass the validation rules');
132            break;
133          default:
134            trigger_error('Invalid comment action '.$comment_action, E_USER_WARNING);
135        }
136      }
137      else
138      {
139        $edit_comment = $_GET['edit_albums'];
140      }
141    }
142    if ($perform_redirect)
143    {
144      redirect($url_self);
145    }
146  }
147}
148
149// +-----------------------------------------------------------------------+
150// |                            navigation bar                             |
151// +-----------------------------------------------------------------------+
152
153if (isset($_GET['start']) and is_numeric($_GET['start']))
154{
155  $start = $_GET['start'];
156}
157else
158{
159  $start = 0;
160}
161
162$query = '
163SELECT COUNT(DISTINCT(com.id))
164  FROM '.CATEGORIES_TABLE.' AS cat
165  INNER JOIN '.COA_TABLE.' AS com
166    ON cat.id = com.category_id
167  LEFT JOIN '.USERS_TABLE.' AS u
168    ON u.'.$conf['user_fields']['id'].' = com.author_id
169  WHERE '.implode('
170    AND ', $page['where_clauses']).'
171;';
172list($counter) = pwg_db_fetch_row(pwg_query($query));
173
174$url = PHPWG_ROOT_PATH.'comments.php'
175  .get_query_string_diff(array('start','delete_albums','validate_albums','edit_albums','pwg_token'));
176
177$navbar = create_navigation_bar(
178  $url,
179  $counter,
180  $start,
181  $page['items_number'],
182  ''
183  );
184
185$template->assign('navbar', $navbar);
186
187// +-----------------------------------------------------------------------+
188// |                        last comments display                          |
189// +-----------------------------------------------------------------------+
190
191$comments = array();
192$element_ids = array();
193$category_ids = array();
194
195$query = '
196SELECT
197    com.id AS comment_id,
198    com.category_id,
199    com.author,
200    com.author_id,
201    u.'.$conf['user_fields']['username'].' AS username,
202    u.'.$conf['user_fields']['email'].' AS user_email,
203    com.email,
204    com.date,
205    com.website_url,
206    com.content,
207    com.validated
208  FROM '.CATEGORIES_TABLE.' AS cat
209    INNER JOIN '.COA_TABLE.' AS com
210      ON cat.id = com.category_id
211    LEFT JOIN '.USERS_TABLE.' As u
212      ON u.'.$conf['user_fields']['id'].' = com.author_id
213  WHERE '.implode('
214    AND ', $page['where_clauses']).'
215  GROUP BY
216    comment_id,
217    com.category_id,
218    com.author,
219    com.author_id,
220    com.date,
221    com.content,
222    com.validated
223  ORDER BY '.$page['sort_by'].' '.$page['sort_order'];
224  if ('all' != $page['items_number'])
225  {
226    $query.= '
227    LIMIT '.$page['items_number'].' OFFSET '.$start;
228  }
229$query.= '
230;';
231$result = pwg_query($query);
232
233while ($row = pwg_db_fetch_assoc($result))
234{
235  $comments[] = $row;
236  $element_ids[] = $row['category_id'];
237}
238
239if (count($comments) > 0)
240{
241  // retrieving category informations
242  $query = '
243SELECT
244    cat.id,
245    cat.name,
246    cat.permalink,
247    cat.uppercats,
248    com.id as comment_id,
249    img.id AS image_id,
250    img.path
251  FROM '.CATEGORIES_TABLE.' AS cat
252    LEFT JOIN '.COA_TABLE.' AS com
253      ON com.category_id = cat.id
254    LEFT JOIN '.USER_CACHE_CATEGORIES_TABLE.' AS ucc
255      ON ucc.cat_id = cat.id AND ucc.user_id = '.$user['id'].'
256    LEFT JOIN '.IMAGES_TABLE.' AS img
257      ON img.id = ucc.user_representative_picture_id
258  '.get_sql_condition_FandF(
259    array(
260      'forbidden_categories' => 'cat.id',
261      'visible_categories' => 'cat.id'
262      ),
263    'WHERE'
264    ).'
265    AND cat.id IN ('.implode(',', $element_ids).')
266;';
267  $categories = hash_from_query($query, 'comment_id');
268
269  foreach ($comments as $comment)
270  {
271    // source of the thumbnail picture
272    $comment['src_image'] = new SrcImage($categories[$comment['comment_id']]);
273
274    // category url
275    $comment['cat_url'] = make_index_url(
276      array(
277        'section' => 'categories',
278        'category' => $categories[$comment['comment_id']],
279        )
280      );
281
282    $email = null;
283    if (!empty($comment['user_email']))
284    {
285      $email = $comment['user_email'];
286    }
287    else if (!empty($comment['email']))
288    {
289      $email = $comment['email'];
290    }
291
292    // comment content
293    $tpl_comment = array(
294      'ID' => $comment['comment_id'],
295      'U_PICTURE' => $comment['cat_url'],
296      'src_image' => $comment['src_image'],
297      'ALT' => trigger_event('render_category_name', $categories[$comment['comment_id']]['name']),
298      'AUTHOR' => trigger_event('render_comment_author', $comment['author']),
299      'WEBSITE_URL' => $comment['website_url'],
300      'DATE' => format_date($comment['date'], true),
301      'CONTENT' => trigger_event('render_comment_content', $comment['content'], 'album'),
302      );
303
304    if (is_admin())
305    {
306      $tpl_comment['EMAIL'] = $email;
307    }
308
309    // rights
310    if (can_manage_comment('delete', $comment['author_id']))
311    {
312      $tpl_comment['U_DELETE'] = add_url_params(
313        $url_self,
314        array(
315          'delete_albums' => $comment['comment_id'],
316          'pwg_token' => get_pwg_token(),
317          )
318        );
319    }
320    if (can_manage_comment('edit', $comment['author_id']))
321    {
322      $tpl_comment['U_EDIT'] = add_url_params(
323        $url_self,
324        array(
325          'edit_albums' => $comment['comment_id'],
326          )
327        );
328
329      if (isset($edit_comment) and ($comment['comment_id'] == $edit_comment))
330      {
331        $tpl_comment['IN_EDIT'] = true;
332        $key = get_ephemeral_key(2, $comment['category_id']);
333        $tpl_comment['KEY'] = $key;
334        $tpl_comment['IMAGE_ID'] = $comment['category_id'];
335        $tpl_comment['CONTENT'] = $comment['content'];
336        $tpl_comment['PWG_TOKEN'] = get_pwg_token();
337        $tpl_comment['U_CANCEL'] = $url_self;
338      }
339    }
340    if (can_manage_comment('validate', $comment['author_id']))
341    {
342      if ('true' != $comment['validated'])
343      {
344        $tpl_comment['U_VALIDATE'] = add_url_params(
345          $url_self,
346          array(
347            'validate_albums'=> $comment['comment_id'],
348            'pwg_token' => get_pwg_token(),
349            )
350          );
351      }
352    }
353
354    $template->append('comments', $tpl_comment);
355  }
356}
357
358// +-----------------------------------------------------------------------+
359// |                            template                                   |
360// +-----------------------------------------------------------------------+
361// add a line to display category name
362$template->set_prefilter('comments', 'coa_change_comments_list');
363
364function coa_change_comments_list($content)
365{
366  $search[0] = '<a href="{$comment.U_PICTURE}">';
367  $replacement[0] = $search[0].'{$comment.ALT}<br>';
368  $search[1] = '<input type="submit"';
369  $replacement[1] = '<input type=hidden name=display_mode value=albums>'.$search[1];
370  return str_replace($search, $replacement, $content);
371}
Note: See TracBrowser for help on using the repository browser.