source: extensions/Comments_on_Albums/include/coa_comments_page.php @ 15640

Last change on this file since 15640 was 15640, checked in by mistic100, 12 years ago
  • sql bug comments page
  • add trigger_action for other plugins
File size: 9.7 KB
Line 
1<?php 
2/* inspired by comments.php */
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4load_language('plugin.lang', COA_PATH);
5
6// +-----------------------------------------------------------------------+
7// |                             main page                                 |
8// +-----------------------------------------------------------------------+
9
10if ( !isset($_GET['display_mode']) or $_GET['display_mode'] != 'albums' ) 
11{ 
12  // adds a button for switch page
13  $template->set_prefilter('comments', 'coa_add_button');
14 
15  function coa_add_button($content, &$smarty) 
16  {   
17    $search ='<form class="filter" action="{$F_ACTION}" method="get">';
18    $replacement = '
19<form class="filter">
20<fieldset>
21  <legend>{\'Display comments on\'|@translate}</legend>
22  <span style="font-size:1.1em;"><a href="#" style="font-weight:bold;">{\'Photos\'|@translate}</a> | <a href="comments.php?display_mode=albums">{\'Albums\'|@translate}</a></span>
23</fieldset>
24</form>';
25
26    return str_replace($search, $replacement.$search, $content);
27  }
28 
29 
30// +-----------------------------------------------------------------------+
31//                        comments on albums page                          |
32// +-----------------------------------------------------------------------+
33
34} 
35else if ($_GET['display_mode'] == 'albums') 
36{
37  include_once(COA_PATH.'include/functions_comment.inc.php'); // custom functions
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
43  $sort_by = array(
44    'date' => l10n('comment date'),
45    'category_id' => l10n('album')
46    );
47  $template->assign('sort_by_options', $sort_by);
48
49  // +-----------------------------------------------------------------------+
50  // |                         comments management                           |
51  // +-----------------------------------------------------------------------+
52  $comment_id = null;
53  $action = null;
54
55  $actions = array('delete_albums', 'validate_albums', 'edit_albums'); // different indexes to not interfer with the main process
56  foreach ($actions as $loop_action) 
57  {
58    if (isset($_GET[$loop_action])) 
59    {
60      $action = $loop_action;
61      check_input_parameter($action, $_GET, false, PATTERN_ID);
62      $comment_id = $_GET[$action];
63      break;
64    }
65  }
66
67  if (isset($action)) 
68  {
69    $comment_author_id = get_comment_author_id_albums($comment_id);
70   
71    if (can_manage_comment(str_replace('_albums', null, $action), $comment_author_id)) 
72    {
73      $perform_redirect = false;
74
75      if ('delete_albums' == $action) 
76      {
77        check_pwg_token();
78        delete_user_comment_albums($comment_id);
79        $perform_redirect = true;
80      }
81      if ('validate_albums' == $action) 
82      {
83        check_pwg_token();
84        validate_user_comment_albums($comment_id);
85        $perform_redirect = true;
86      }
87      if ('edit_albums' == $action) 
88      {
89        if (!empty($_POST['content'])) 
90        {
91          check_pwg_token();
92          update_user_comment_albums(
93            array(
94              'comment_id' => $_GET['edit_albums'],
95              'category_id' => $_POST['image_id'],
96              'content' => $_POST['content']
97              ), 
98            $_POST['key']
99            );
100         
101          $perform_redirect = true;
102        } 
103        else 
104        {
105          $edit_comment = $_GET['edit_albums'];
106        }
107      }
108      if ($perform_redirect) 
109      {
110        $redirect_url = 
111          PHPWG_ROOT_PATH
112          .'comments.php'
113          .get_query_string_diff(array('delete_albums','validate_albums','edit_albums','pwg_token'));
114       
115        redirect($redirect_url);
116      }
117    }
118  }
119
120  // +-----------------------------------------------------------------------+
121  // |                            navigation bar                             |
122  // +-----------------------------------------------------------------------+
123  if (isset($_GET['start']) and is_numeric($_GET['start'])) 
124  {
125    $start = $_GET['start'];
126  } 
127  else 
128  {
129    $start = 0;
130  }
131
132  $query = '
133SELECT COUNT(DISTINCT(com.id))
134  FROM '.COA_TABLE.' AS com
135  LEFT JOIN '.USERS_TABLE.' As u
136    ON u.'.$conf['user_fields']['id'].' = com.author_id
137  WHERE '.implode('
138    AND ', $page['where_clauses']).'
139;';
140  list($counter) = pwg_db_fetch_row(pwg_query($query));
141
142  $url = PHPWG_ROOT_PATH
143      .'comments.php'
144    .get_query_string_diff(array('start','delete_albums','validate_albums','edit_albums','pwg_token'));
145   
146  $navbar = create_navigation_bar(
147    $url, 
148    $counter, 
149    $start, 
150    $page['items_number'], 
151    ''
152    );
153 
154  $template->assign('navbar', $navbar);
155
156  // +-----------------------------------------------------------------------+
157  // |                        last comments display                          |
158  // +-----------------------------------------------------------------------+
159  $comments = array();
160  $element_ids = array();
161  $category_ids = array();
162
163  $query = '
164SELECT
165    com.id AS comment_id,
166    com.category_id,
167    com.author,
168    com.author_id,
169    '.$conf['user_fields']['username'].' AS username,
170    com.date,
171    com.content,
172    com.validated
173  FROM '.COA_TABLE.' AS com
174    LEFT JOIN '.USERS_TABLE.' As u
175      ON u.'.$conf['user_fields']['id'].' = com.author_id
176  WHERE '.implode('
177    AND ', $page['where_clauses']).'
178  GROUP BY
179    comment_id,
180    com.category_id,
181    com.author,
182    com.author_id,
183    com.date,
184    com.content,
185    com.validated
186  ORDER BY '.$page['sort_by'].' '.$page['sort_order'];
187if ('all' != $page['items_number']) 
188{
189  $query.= '
190  LIMIT '.$page['items_number'].' OFFSET '.$start;
191}
192$query.= '
193;';
194  $result = pwg_query($query);
195
196  while ($row = pwg_db_fetch_assoc($result)) 
197  {
198    array_push($comments, $row);
199    array_push($element_ids, $row['category_id']);
200  }
201
202  if (count($comments) > 0) 
203  {
204    // retrieving category informations
205    $query = '
206SELECT
207    cat.id,
208    cat.name,
209    cat.permalink,
210    cat.uppercats,
211    com.id as comment_id,
212    img.id AS image_id,
213    img.path
214  FROM '.CATEGORIES_TABLE.' AS cat
215    LEFT JOIN '.COA_TABLE.' AS com
216      ON com.category_id = cat.id
217    LEFT JOIN '.USER_CACHE_CATEGORIES_TABLE.' AS ucc
218      ON ucc.cat_id = cat.id AND ucc.user_id = '.$user['id'].'
219    LEFT JOIN '.IMAGES_TABLE.' AS img
220      ON img.id = ucc.user_representative_picture_id
221  '.get_sql_condition_FandF(
222    array(
223      'forbidden_categories' => 'cat.id',
224      'visible_categories' => 'cat.id'
225      ), 
226    'WHERE'
227    ).'
228    AND cat.id IN ('.implode(',', $element_ids).')
229;';
230    $categories = hash_from_query($query, 'comment_id');
231
232    foreach ($comments as $comment) 
233    {
234      // source of the thumbnail picture
235      $comment['src_image'] = new SrcImage($categories[$comment['comment_id']]);
236   
237      // category url
238      $comment['cat_url'] = duplicate_index_url(
239        array(
240          'category' => array(
241            'id' => $categories[$comment['comment_id']]['id'], 
242            'name' => $categories[$comment['comment_id']]['name'], 
243            'permalink' => $categories[$comment['comment_id']]['permalink'],
244            ),
245          array('start')
246          )
247        );
248     
249      // comment content
250      $tpl_comment = array(
251        'ID' => $comment['comment_id'],
252        'U_PICTURE' => $comment['cat_url'],
253        'ALT' => trigger_event('render_category_name', $categories[$comment['comment_id']]['name']),
254        'src_image' => $comment['src_image'],
255        'AUTHOR' => trigger_event('render_comment_author', $comment['author']),
256        'DATE' => format_date($comment['date'], true),
257        'CONTENT' => trigger_event('render_comment_content', $comment['content'], 'album'),
258        );
259
260      // rights
261      if (can_manage_comment('delete', $comment['author_id'])) 
262      {
263        $tpl_comment['U_DELETE'] = add_url_params(
264          $url, 
265          array(
266            'delete_albums' => $comment['comment_id'],
267            'pwg_token' => get_pwg_token(),
268            )
269          );
270      }
271      if (can_manage_comment('edit', $comment['author_id'])) 
272      {
273        $tpl_comment['U_EDIT'] = add_url_params(
274          $url, 
275          array(
276            'edit_albums' => $comment['comment_id'],
277            )
278          );
279
280        if (isset($edit_comment) and ($comment['comment_id'] == $edit_comment)) 
281        {
282          $tpl_comment['IN_EDIT'] = true;
283          $key = get_ephemeral_key(2, $comment['category_id']);
284          $tpl_comment['KEY'] = $key;
285          $tpl_comment['IMAGE_ID'] = $comment['category_id'];
286          $tpl_comment['CONTENT'] = $comment['content'];
287          $tpl_comment['PWG_TOKEN'] = get_pwg_token();
288        }
289      }
290      if (can_manage_comment('validate', $comment['author_id'])) 
291      {
292        if ('true' != $comment['validated']) 
293        {
294          $tpl_comment['U_VALIDATE'] = add_url_params(
295            $url, 
296            array(
297              'validate_albums'=> $comment['comment_id'],
298              'pwg_token' => get_pwg_token(),
299              )
300            );
301        }
302      }
303     
304      $template->append('comments', $tpl_comment);
305    }
306  }
307
308  // +-----------------------------------------------------------------------+
309  // |                            template                                   |
310  // +-----------------------------------------------------------------------+
311  $template->set_filenames(array('comments'=> dirname(__FILE__).'/../template/comments_page.tpl'));
312 
313  // add a line to display category name
314  $template->set_prefilter('comments', 'coa_change_comments_list');
315 
316  function coa_change_comments_list($content, &$smarty) {
317    $search = '<img src="{$pwg->derivative_url($derivative_params, $comment.src_image)}" alt="{$comment.ALT}">';
318    $replacement = $search.'<br/>{$comment.ALT}';
319    return str_replace($search, $replacement, $content);
320  }
321}
322
323?>
Note: See TracBrowser for help on using the repository browser.