]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/peoplesearch.php
try to make replies point to the clicked-on notice
[quix0rs-gnu-social.git] / actions / peoplesearch.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 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 Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/searchaction.php');
23 define('PROFILES_PER_PAGE', 10);
24
25 class PeoplesearchAction extends SearchAction {
26
27         function get_instructions() {
28                 return _('Search for people on %%site.name%% by their name, location, or interests. ' .
29                                   'Separate the terms by spaces; they must be 3 characters or more.');
30         }
31
32         function get_title() {
33                 return _('People search');
34         }
35
36         function show_results($q, $page) {
37
38                 $profile = new Profile();
39
40                 # lcase it for comparison
41                 $q = strtolower($q);
42
43                 if(common_config('db','type')=='mysql') {
44                         $profile->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
45                                                    'against (\''.addslashes($q).'\')');
46                 } else {
47                         $profile->whereAdd('textsearch @@ plainto_tsquery(\''.addslashes($q).'\')');
48                 }
49
50                 # Ask for an extra to see if there's more.
51
52                 $profile->limit((($page-1)*PROFILES_PER_PAGE), PROFILES_PER_PAGE + 1);
53
54                 $cnt = $profile->find();
55
56                 if ($cnt > 0) {
57                         $terms = preg_split('/[\s,]+/', $q);
58                         common_element_start('ul', array('id' => 'profiles'));
59                         for ($i = 0; $i < min($cnt, PROFILES_PER_PAGE); $i++) {
60                                 if ($profile->fetch()) {
61                                         $this->show_profile($profile, $terms);
62                                 } else {
63                                         // shouldn't happen!
64                                         break;
65                                 }
66                         }
67                         common_element_end('ul');
68                 } else {
69                         common_element('p', 'error', _('No results'));
70                 }
71
72                 common_pagination($page > 1, $cnt > PROFILES_PER_PAGE,
73                                                   $page, 'peoplesearch', array('q' => $q));
74         }
75
76         function show_profile($profile, $terms) {
77                 common_element_start('li', array('class' => 'profile_single',
78                                                                                  'id' => 'profile-' . $profile->id));
79                 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
80                 common_element_start('a', array('href' => $profile->profileurl));
81                 common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
82                                                                         'class' => 'avatar stream',
83                                                                         'width' => AVATAR_STREAM_SIZE,
84                                                                         'height' => AVATAR_STREAM_SIZE,
85                                                                         'alt' =>
86                                                                         ($profile->fullname) ? $profile->fullname :
87                                                                         $profile->nickname));
88                 common_element_end('a');
89                 common_element_start('p');
90                 common_element_start('a', array('href' => $profile->profileurl,
91                                                                                 'class' => 'nickname'));
92                 common_raw($this->highlight($profile->nickname, $terms));
93                 common_element_end('a');
94                 if ($profile->fullname) {
95                         common_text(' | ');
96                         common_element_start('span', 'fullname');
97                         common_raw($this->highlight($profile->fullname, $terms));
98                         common_element_end('span');
99                 }
100                 if ($profile->location) {
101                         common_text(' | ');
102                         common_element_start('span', 'location');
103                         common_raw($this->highlight($profile->location, $terms));
104                         common_element_end('span');
105                 }
106                 common_element_end('p');
107                 if ($profile->homepage) {
108                         common_element_start('p', 'website');
109                         common_element_start('a', array('href' => $profile->homepage));
110                         common_raw($this->highlight($profile->homepage, $terms));
111                         common_element_end('a');
112                         common_element_end('p');
113                 }
114                 if ($profile->bio) {
115                         common_element_start('p', 'bio');
116                         common_raw($this->highlight($profile->bio, $terms));
117                         common_element_end('p');
118                 }
119                 common_element_end('li');
120         }
121
122         function highlight($text, $terms) {
123                 $terms = array_map('preg_quote', array_map('htmlspecialchars', $terms));
124                 $pattern = '/('.implode('|',$terms).')/i';
125                 $result = preg_replace($pattern, '<strong>\\1</strong>', htmlspecialchars($text));
126                 return $result;
127         }
128 }