]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/peoplesearch.php
Don't accept non-objects before testing with "instanceof".
[quix0rs-gnu-social.git] / actions / peoplesearch.php
1 <?php
2 /**
3  * People search action class.
4  *
5  * PHP version 5
6  *
7  * @category Action
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @author   Robin Millette <millette@status.net>
11  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12  * @link     http://status.net/
13  *
14  * StatusNet - the distributed open-source microblogging tool
15  * Copyright (C) 2008, 2009, StatusNet, Inc.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU Affero General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU Affero General Public License for more details.
26  *
27  * You should have received a copy of the GNU Affero General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/searchaction.php';
36 require_once INSTALLDIR.'/lib/profilelist.php';
37
38 /**
39  * People search action class.
40  *
41  * @category Action
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @author   Robin Millette <millette@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
46  * @link     http://status.net/
47  */
48 class PeoplesearchAction extends SearchAction
49 {
50     function getInstructions()
51     {
52         // TRANS: Instructions for the "People search" page.
53         // TRANS: %%site.name%% is the name of the StatusNet site.
54         return _('Search for people on %%site.name%% by their name, location, or interests. ' .
55                   'Separate the terms by spaces; they must be 3 characters or more.');
56     }
57
58     function title()
59     {
60         // TRANS: Title of a page where users can search for other users.
61         return _('People search');
62     }
63
64     function showResults($q, $page)
65     {
66         $profile = new Profile();
67         $search_engine = $profile->getSearchEngine('profile');
68         $search_engine->set_sort_mode('chron');
69         // Ask for an extra to see if there's more.
70         $search_engine->limit((($page-1)*PROFILES_PER_PAGE), PROFILES_PER_PAGE + 1);
71         if (false === $search_engine->query($q)) {
72             $cnt = 0;
73         }
74         else {
75             $cnt = $profile->find();
76         }
77         if ($cnt > 0) {
78             $terms = preg_split('/[\s,]+/', $q);
79             $results = new PeopleSearchResults($profile, $terms, $this);
80             $results->show();
81             $profile->free();
82             $this->pagination($page > 1, $cnt > PROFILES_PER_PAGE,
83                           $page, 'peoplesearch', array('q' => $q));
84
85         } else {
86             // TRANS: Message on the "People search" page where a query has no results.
87             $this->element('p', 'error', _('No results.'));
88             $this->searchSuggestions($q);
89             $profile->free();
90         }
91     }
92
93     function showScripts()
94     {
95         parent::showScripts();
96         $this->autofocus('q');
97     }
98 }
99
100 /**
101  * People search results class
102  *
103  * Derivative of ProfileList with specialization for highlighting search terms.
104  *
105  * @category Widget
106  * @package  StatusNet
107  * @author   Evan Prodromou <evan@status.net>
108  * @author   Robin Millette <millette@status.net>
109  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
110  * @link     http://status.net/
111  *
112  * @see PeoplesearchAction
113  */
114
115 class PeopleSearchResults extends ProfileList
116 {
117     var $terms = null;
118     var $pattern = null;
119
120     function __construct($profile, $terms, $action)
121     {
122         parent::__construct($profile, $action);
123
124         $this->terms = array_map('preg_quote',
125                                  array_map('htmlspecialchars', $terms));
126
127         $this->pattern = '/('.implode('|',$terms).')/i';
128     }
129
130     function newProfileItem($profile)
131     {
132         return new PeopleSearchResultItem($profile, $this->action);
133     }
134 }
135
136 class PeopleSearchResultItem extends ProfileListItem
137 {
138     function highlight($text)
139     {
140         return preg_replace($this->pattern, '<strong>\\1</strong>', htmlspecialchars($text));
141     }
142 }