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

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

update for 2.6 + clean

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