Merge branch 'long-descriptions'
[scuttle] / search.php
1 <?php
2 /***************************************************************************
3 Copyright (c) 2005 - 2006 Marcus Campbell
4 http://scuttle.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 ***************************************************************************/
20
21 require_once 'header.inc.php';
22
23 // POST
24 if (isset($_POST['terms'])) {
25     // Redirect to GET
26     header('Location: '. createURL('search', $_POST['range'] .'/'. filter($_POST['terms'], 'url')));
27
28 // GET
29 } else {
30     $bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
31     $templateservice =& ServiceFactory::getServiceInstance('TemplateService');
32     $userservice =& ServiceFactory::getServiceInstance('UserService');
33
34     $logged_on_userid = $userservice->getCurrentUserId();
35     list($url, $range, $terms, $page) = explode('/', $_SERVER['PATH_INFO']);
36
37     $tplvars = array();
38     $tplVars['loadjs'] = true;
39     
40     // Pagination
41     $perpage = getPerPageCount();
42     if (isset($_GET['page']) && intval($_GET['page']) > 1) {
43         $page = $_GET['page'];
44         $start = ($page - 1) * $perpage;
45     } else {
46         $page = 0;
47         $start = 0;
48     }
49     
50     $s_user = NULL;
51     $s_start = NULL;
52     $s_end = NULL;
53     $s_watchlist = NULL;
54
55     // No search terms
56     if (is_null($terms)) {
57         $tplVars['subtitle'] = T_('Search Bookmarks');
58         $s_start = date('Y-m-d H:i:s', strtotime($dtend .' -'. $defaultRecentDays .' days'));
59         $s_end = date('Y-m-d H:i:s', strtotime('tomorrow'));
60     
61     // Search terms
62     } else {
63         $tplVars['subtitle'] = T_('Search Results');
64         $selected = ' selected="selected"';
65
66          switch ($range) {
67             case 'all':
68                 $tplVars['select_all'] = $selected;
69                 $s_user = NULL;
70                 break;
71             case 'watchlist':
72                 $tplVars['select_watchlist'] = $selected;
73                 $s_user = $logged_on_userid;
74                 $s_watchlist = true;
75                 break;
76             default:
77                 $s_user = $range;
78                 break;
79         }
80
81         if (isset($s_user)) {
82             if (is_numeric($s_user)) {
83                 $s_user = intval($s_user);
84             } else {
85                 if (!($userinfo = $userservice->getUserByUsername($s_user) ) ) {
86                     $tplVars['error'] = sprintf(T_('User with username %s was not found'), $s_user);
87                     $templateservice->loadTemplate('error.404.tpl', $tplVars);
88                     exit();
89                 } else {
90                     $s_user =& $userinfo[$userservice->getFieldName('primary')];
91                 }
92             }
93         }
94     }
95     $bookmarks =& $bookmarkservice->getBookmarks($start, $perpage, $s_user, NULL, $terms, getSortOrder(), $s_watchlist, $s_start, $s_end);
96     
97     $tplVars['page'] = $page;
98     $tplVars['start'] = $start;
99     $tplVars['popCount'] = 25;
100     $tplVars['sidebar_blocks'] = array('recent');
101     $tplVars['range'] = $range;
102     $tplVars['terms'] = $terms;
103     $tplVars['pagetitle'] = T_('Search Bookmarks');
104     $tplVars['bookmarkCount'] = $start + 1;
105     $tplVars['total'] = $bookmarks['total'];
106     $tplVars['bookmarks'] =& $bookmarks['bookmarks'];
107     $tplVars['cat_url'] = createURL('tags', '%2$s');
108     $tplVars['nav_url'] = createURL('search', $range .'/'. $terms .'/%3$s');
109     
110     $templateservice->loadTemplate('bookmarks.tpl', $tplVars);
111 }
112 ?>

Benjamin Mako Hill || Want to submit a patch?