]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/peoplesearch.php
refactor common code between searches and between search and settings
[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 define(PROFILES_PER_PAGE, 10);
23
24 class PeoplesearchAction extends SearchAction {
25         
26         function get_instructions() {
27                 return _t('Search for people on %%site.name%% by their name, location, or interests. ' . 
28                                   'Separate the terms by spaces; they must be 3 characters or more.');
29         }
30
31         function get_title() {
32                 return _t('People search');
33         }
34         
35         function show_results($q, $page) {
36                 
37                 $profile = new Profile();
38
39                 # lcase it for comparison
40                 $q = strtolower($q);
41                 $profile->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' . 
42                                                    'against (\''.addslashes($q).'\')');
43
44                 # Ask for an extra to see if there's more.
45                 
46                 $profile->limit((($page-1)*PROFILES_PER_PAGE), PROFILES_PER_PAGE + 1);
47
48                 $cnt = $profile->find();
49
50                 if ($cnt > 0) {
51                         $terms = preg_split('/[\s,]+/', $q);
52                         common_element_start('ul', array('id' => 'profiles'));
53                         for ($i = 0; $i < min($cnt, PROFILES_PER_PAGE); $i++) {
54                                 if ($profile->fetch()) {
55                                         $this->show_profile($profile, $terms);
56                                 } else {
57                                         // shouldn't happen!
58                                         break;
59                                 }
60                         }
61                         common_element_end('ul');
62                 } else {
63                         common_element('p', 'error', _t('No results'));
64                 }
65                 
66                 common_pagination($page > 1, $cnt > PROFILES_PER_PAGE,
67                                                   $page, 'peoplesearch', array('q' => $q));
68         }
69         
70         function show_profile($profile, $terms) {
71                 common_element_start('li', array('class' => 'profile_single',
72                                                                                  'id' => 'profile-' . $profile->id));
73                 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
74                 common_element_start('a', array('href' => $profile->profileurl));
75                 common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
76                                                                         'class' => 'avatar stream',
77                                                                         'width' => AVATAR_STREAM_SIZE,
78                                                                         'height' => AVATAR_STREAM_SIZE,
79                                                                         'alt' =>
80                                                                         ($profile->fullname) ? $profile->fullname :
81                                                                         $profile->nickname));
82                 common_element_end('a');
83                 common_element_start('p');
84                 common_element_start('a', array('href' => $profile->profileurl,
85                                                                                 'class' => 'nickname'));
86                 common_raw($this->highlight($profile->nickname, $terms));
87                 common_element_end('a');
88                 if ($profile->fullname) {
89                         common_text(' | ');
90                         common_element_start('span', 'fullname');
91                         common_raw($this->highlight($profile->fullname, $terms));
92                         common_element_end('span');
93                 }
94                 if ($profile->location) {
95                         common_text(' | ');
96                         common_element_start('span', 'location');
97                         common_raw($this->highlight($profile->location, $terms));
98                         common_element_end('span');
99                 }
100                 common_element_end('p');
101                 if ($profile->homepage) {
102                         common_element_start('p', 'website');
103                         common_element('a', array('href' => $profile->homepage),
104                                                    $this->highlight($profile->homepage));
105                         common_element_end('p');
106                 }
107                 if ($profile->bio) {
108                         common_element_start('p', 'bio');
109                         common_raw($this->highlight($profile->bio, $terms));
110                         common_element_end('p');
111                 }
112                 common_element_end('li');
113         }
114
115         function highlight($text, $terms) {
116                 $pattern = '/('.implode('|',array_map('htmlspecialchars', $terms)).')/i';
117                 $result = preg_replace($pattern, '<strong>\\1</strong>', htmlspecialchars($text));
118                 return $result;
119         }
120 }