source: trunk/comments.php @ 1900

Last change on this file since 1900 was 1900, checked in by rub, 17 years ago

Apply property svn:eol-style Value: LF

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
RevLine 
[166]1<?php
[354]2// +-----------------------------------------------------------------------+
[593]3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
[1716]5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
[354]6// +-----------------------------------------------------------------------+
[593]7// | branch        : BSF (Best So Far)
[1598]8// | file          : $Id: comments.php 1900 2007-03-12 22:33:53Z rub $
[354]9// | last update   : $Date: 2007-03-12 22:33:53 +0000 (Mon, 12 Mar 2007) $
10// | last modifier : $Author: rub $
11// | revision      : $Revision: 1900 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
[166]27
[579]28// +-----------------------------------------------------------------------+
29// |                           initialization                              |
30// +-----------------------------------------------------------------------+
[1598]31define('PHPWG_ROOT_PATH','./');
32include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
[345]33
[1072]34// +-----------------------------------------------------------------------+
35// | Check Access and exit when user status is not ok                      |
36// +-----------------------------------------------------------------------+
37check_status(ACCESS_GUEST);
38
[796]39$sort_order = array(
40  'descending' => 'DESC',
41  'ascending' => 'ASC'
42  );
43
44// sort_by : database fields proposed for sorting comments list
45$sort_by = array(
46  'date' => 'comment date',
[889]47  'image_id' => 'picture'
[796]48  );
49
50// items_number : list of number of items to display per page
51$items_number = array(5,10,20,50,'all');
52
53// since when display comments ?
54//
55$since_options = array(
56  1 => array('label' => l10n('today'),
57             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 1 DAY)'),
58  2 => array('label' => sprintf(l10n('last %d days'), 7),
59             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 7 DAY)'),
60  3 => array('label' => sprintf(l10n('last %d days'), 30),
61             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 30 DAY)'),
62  4 => array('label' => l10n('the beginning'),
63             'clause' => '1=1') // stupid but generic
64  );
65
[1716]66$page['since'] = isset($_GET['since']) ? $_GET['since'] : 4;
[796]67
68// on which field sorting
69//
70$page['sort_by'] = 'date';
71// if the form was submitted, it overloads default behaviour
72if (isset($_GET['sort_by']))
[393]73{
[796]74  $page['sort_by'] = $_GET['sort_by'];
[393]75}
[796]76
77// order to sort
78//
79$page['sort_order'] = $sort_order['descending'];
80// if the form was submitted, it overloads default behaviour
81if (isset($_GET['sort_order']))
[393]82{
[796]83  $page['sort_order'] = $sort_order[$_GET['sort_order']];
[393]84}
[796]85
86// number of items to display
87//
[1814]88$page['items_number'] = 10;
[796]89if (isset($_GET['items_number']))
90{
91  $page['items_number'] = $_GET['items_number'];
92}
93
[1716]94$page['where_clauses'] = array();
95
[796]96// which category to filter on ?
97if (isset($_GET['cat']) and 0 != $_GET['cat'])
98{
[1716]99  $page['where_clauses'][] =
[796]100    'category_id IN ('.implode(',', get_subcat_ids(array($_GET['cat']))).')';
101}
102
103// search a particular author
104if (isset($_GET['author']) and !empty($_GET['author']))
105{
[1716]106  $page['where_clauses'][] = 'com.author = \''.$_GET['author'].'\'';
[796]107}
108
109// search a substring among comments content
110if (isset($_GET['keyword']) and !empty($_GET['keyword']))
111{
[1716]112  $page['where_clauses'][] =
[796]113    '('.
114    implode(' AND ',
115            array_map(
116              create_function(
117                '$s',
118                'return "content LIKE \'%$s%\'";'
119                ),
120              preg_split('/[\s,;]+/', $keyword)
121              )
122      ).
123    ')';
124}
125
[1716]126$page['where_clauses'][] = $since_options[$page['since']]['clause'];
127
[1598]128// which status to filter on ?
[1716]129if ( !is_admin() )
[1598]130{
[1716]131  $page['where_clauses'][] = 'validated="true"';
[1598]132}
133
[1716]134$page['where_clauses'][] = get_sql_condition_FandF
135  (
136    array
137      (
138        'forbidden_categories' => 'category_id',
139        'visible_categories' => 'category_id',
140        'visible_images' => 'ic.image_id'
141      ),
142    '', true
143  );
[1598]144
[579]145// +-----------------------------------------------------------------------+
146// |                         comments management                           |
147// +-----------------------------------------------------------------------+
[1617]148if (isset($_GET['delete']) and is_numeric($_GET['delete'])
149      and !is_adviser() )
150{// comments deletion
151  check_status(ACCESS_ADMINISTRATOR);
152  $query = '
[579]153DELETE FROM '.COMMENTS_TABLE.'
[1598]154  WHERE id='.$_GET['delete'].'
[579]155;';
[1617]156  pwg_query($query);
157}
[1598]158
[1617]159if (isset($_GET['validate']) and is_numeric($_GET['validate'])
160      and !is_adviser() )
161{  // comments validation
162  check_status(ACCESS_ADMINISTRATOR);
163  $query = '
[579]164UPDATE '.COMMENTS_TABLE.'
165  SET validated = \'true\'
[1617]166  , validation_date = NOW()
[1598]167  WHERE id='.$_GET['validate'].'
[579]168;';
[1617]169  pwg_query($query);
[579]170}
[1617]171
[579]172// +-----------------------------------------------------------------------+
173// |                       page header and options                         |
174// +-----------------------------------------------------------------------+
[355]175
[850]176$title= l10n('title_comments');
177$page['body_id'] = 'theCommentsPage';
178include(PHPWG_ROOT_PATH.'include/page_header.php');
179
[579]180$template->set_filenames(array('comments'=>'comments.tpl'));
181$template->assign_vars(
182  array(
183    'L_COMMENT_TITLE' => $title,
[796]184
185    'F_ACTION'=>PHPWG_ROOT_PATH.'comments.php',
[1716]186    'F_KEYWORD'=>@htmlentities(stripslashes($_GET['keyword'])),
187    'F_AUTHOR'=>@htmlentities(stripslashes($_GET['author'])),
[1090]188
[1082]189    'U_HOME' => make_index_url(),
[579]190    )
191  );
[355]192
[796]193// +-----------------------------------------------------------------------+
194// |                          form construction                            |
195// +-----------------------------------------------------------------------+
196
197// Search in a particular category
198$blockname = 'category';
199
200$template->assign_block_vars(
201  $blockname,
202  array('SELECTED' => '',
203        'VALUE'=> 0,
204        'OPTION' => '------------'
205    ));
206
207$query = '
[1861]208SELECT id, name, uppercats, global_rank
[1677]209  FROM '.CATEGORIES_TABLE.'
210'.get_sql_condition_FandF
211  (
212    array
213      (
214        'forbidden_categories' => 'id',
215        'visible_categories' => 'id'
216      ),
217    'WHERE'
218  ).'
[796]219;';
220display_select_cat_wrapper($query, array(@$_GET['cat']), $blockname, true);
221
222// Filter on recent comments...
223$blockname = 'since_option';
224
225foreach ($since_options as $id => $option)
226{
227  $selected = ($id == $page['since']) ? 'selected="selected"' : '';
[1090]228
[420]229  $template->assign_block_vars(
[796]230    $blockname,
231    array('SELECTED' => $selected,
232          'VALUE'=> $id,
233          'CONTENT' => $option['label']
234      ));
[355]235}
[796]236
237// Sort by
238$blockname = 'sort_by_option';
239
240foreach ($sort_by as $key => $value)
241{
242  $selected = ($key == $page['sort_by']) ? 'selected="selected"' : '';
243
244  $template->assign_block_vars(
245    $blockname,
246    array('SELECTED' => $selected,
247          'VALUE'=> $key,
248          'CONTENT' => l10n($value)
249      ));
250}
251
252// Sorting order
253$blockname = 'sort_order_option';
254
255foreach (array_keys($sort_order) as $option)
256{
257  $selected = ($option == $page['sort_order']) ? 'selected="selected"' : '';
258
259  $template->assign_block_vars(
260    $blockname,
261    array('SELECTED' => $selected,
262          'VALUE'=> $option,
263          'CONTENT' => l10n($option)
264      ));
265}
266
267// Number of items
268$blockname = 'items_number_option';
269
270foreach ($items_number as $option)
271{
272  $selected = ($option == $page['items_number']) ? 'selected="selected"' : '';
273
274  $template->assign_block_vars(
275    $blockname,
276    array('SELECTED' => $selected,
277          'VALUE'=> $option,
278          'CONTENT' => is_numeric($option) ? $option : l10n($option)
279      ));
280}
281
[579]282// +-----------------------------------------------------------------------+
[796]283// |                            navigation bar                             |
284// +-----------------------------------------------------------------------+
285
286if (isset($_GET['start']) and is_numeric($_GET['start']))
287{
288  $start = $_GET['start'];
289}
290else
291{
292  $start = 0;
293}
294
295$query = '
296SELECT COUNT(DISTINCT(id))
297  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
298    INNER JOIN '.COMMENTS_TABLE.' AS com
299    ON ic.image_id = com.image_id
[1716]300  WHERE '.implode('
301    AND ', $page['where_clauses']).'
[796]302;';
303list($counter) = mysql_fetch_row(pwg_query($query));
304
[1598]305$url = PHPWG_ROOT_PATH
306    .'comments.php'
307    .get_query_string_diff(array('start','delete','validate'));
[796]308
309$navbar = create_navigation_bar($url,
310                                $counter,
311                                $start,
312                                $page['items_number'],
313                                '');
314
315$template->assign_vars(array('NAVBAR' => $navbar));
316
317// +-----------------------------------------------------------------------+
[579]318// |                        last comments display                          |
319// +-----------------------------------------------------------------------+
[355]320
[796]321$comments = array();
322$element_ids = array();
323$category_ids = array();
324
[579]325$query = '
[796]326SELECT com.id AS comment_id
327     , com.image_id
328     , ic.category_id
329     , com.author
330     , com.date
331     , com.content
332     , com.id AS comment_id
[1598]333     , com.validated
[796]334  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
335    INNER JOIN '.COMMENTS_TABLE.' AS com
336    ON ic.image_id = com.image_id
[1716]337  WHERE '.implode('
338    AND ', $page['where_clauses']).'
[796]339  GROUP BY comment_id
340  ORDER BY '.$page['sort_by'].' '.$page['sort_order'];
341if ('all' != $page['items_number'])
342{
343  $query.= '
344  LIMIT '.$start.','.$page['items_number'];
345}
346$query.= '
[579]347;';
[587]348$result = pwg_query($query);
[1598]349while ($row = mysql_fetch_assoc($result))
[393]350{
[796]351  array_push($comments, $row);
352  array_push($element_ids, $row['image_id']);
353  array_push($category_ids, $row['category_id']);
[393]354}
[796]355
356if (count($comments) > 0)
[579]357{
[796]358  // retrieving element informations
359  $elements = array();
[579]360  $query = '
[796]361SELECT id, name, file, path, tn_ext
[579]362  FROM '.IMAGES_TABLE.'
[796]363  WHERE id IN ('.implode(',', $element_ids).')
[579]364;';
[796]365  $result = pwg_query($query);
[1598]366  while ($row = mysql_fetch_assoc($result))
[579]367  {
[796]368    $elements[$row['id']] = $row;
[579]369  }
[721]370
[796]371  // retrieving category informations
[579]372  $query = '
[1866]373SELECT id, name, permalink, uppercats
[796]374  FROM '.CATEGORIES_TABLE.'
375  WHERE id IN ('.implode(',', $category_ids).')
376;';
[1866]377  $categories = hash_from_query($query, 'id');
[796]378
379  foreach ($comments as $comment)
[579]380  {
[796]381    if (!empty($elements[$comment['image_id']]['name']))
[166]382    {
[1598]383      $name=$elements[$comment['image_id']]['name'];
[166]384    }
[796]385    else
386    {
[1598]387      $name=get_name_from_file($elements[$comment['image_id']]['file']);
[796]388    }
[1090]389
[796]390    // source of the thumbnail picture
[1598]391    $thumbnail_src = get_thumbnail_url( $elements[$comment['image_id']] );
[1090]392
[796]393    // link to the full size picture
[1090]394    $url = make_picture_url(
395            array(
[1861]396              'category' => $categories[ $comment['category_id'] ],
[1090]397              'image_id' => $comment['image_id'],
398              'image_file' => $elements[$comment['image_id']]['file'],
399            )
400          );
401
[796]402    $author = $comment['author'];
403    if (empty($comment['author']))
[393]404    {
[796]405      $author = l10n('guest');
[166]406    }
[1090]407
[796]408    $template->assign_block_vars(
[848]409      'comment',
[796]410      array(
[1004]411        'U_PICTURE' => $url,
[848]412        'TN_SRC' => $thumbnail_src,
[1598]413        'ALT' => $name,
[848]414        'AUTHOR' => $author,
415        'DATE'=>format_date($comment['date'],'mysql_datetime',true),
[1598]416        'CONTENT'=>trigger_event('render_comment_content',$comment['content']),
[796]417        ));
[1598]418
419    if ( is_admin() )
420    {
421      $url = get_root_url().'comments.php'.get_query_string_diff(array('delete','validate'));
422      $template->assign_block_vars(
423        'comment.action_delete',
424        array(
425          'U_DELETE' => add_url_params($url,
426                          array('delete'=>$comment['comment_id'])
427                         ),
428          ));
429      if ($comment['validated'] != 'true')
430      {
431        $template->assign_block_vars(
432          'comment.action_validate',
433          array(
434            'U_VALIDATE' => add_url_params($url,
435                            array('validate'=>$comment['comment_id'])
436                           ),
437            ));
438      }
439    }
[166]440  }
[579]441}
442// +-----------------------------------------------------------------------+
443// |                           html code display                           |
444// +-----------------------------------------------------------------------+
[1598]445$template->assign_block_vars('title',array());
446$template->parse('comments');
447include(PHPWG_ROOT_PATH.'include/page_tail.php');
[362]448?>
Note: See TracBrowser for help on using the repository browser.