]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/peoplesearch.php
045d99e39ac41f9e9adc2f691b1ac0497083c08c
[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 # XXX common parent for people and content search?
25
26 class PeoplesearchAction extends Action {
27         
28         function handle($args) {
29                 parent::handle($args);
30                 $this->show_form();
31         }
32
33         function get_instructions() {
34                 return _t('Search for people on %%site.name%% by their name, location, or interests. ' . 
35                                   'Separate the terms by spaces; they must be 3 characters or more.');
36         }
37         
38         function show_top($error=NULL) {
39                 if ($error) {
40                         common_element('p', 'error', $error);
41                 } else {
42                         $instr = $this->get_instructions();
43                         $output = common_markup_to_html($instr);
44                         common_element_start('div', 'instructions');
45                         common_raw($output);
46                         common_element_end('div');
47                 }
48         }
49         
50         function show_form($error=NULL) {
51                 $q = $this->trimmed('q');
52                 $page = $this->trimmed('page', 1);
53                 
54                 common_show_header(_t('Find people'), NULL, $error, array($this, 'show_top'));
55                 common_element_start('form', array('method' => 'post',
56                                                                                    'id' => 'login',
57                                                                                    'action' => common_local_url('peoplesearch')));
58                 common_element_start('p');
59                 common_element('input', array('name' => 'q',
60                                                                           'id' => 'q',
61                                                                           'type' => 'text',
62                                                                           'class' => 'input_text',
63                                                                           'value' => ($q) ? $q : ''));
64                 common_text(' ');
65                 common_element('input', array('type' => 'submit',
66                                                                           'id' => 'search',
67                                                                           'name' => 'search',
68                                                                           'class' => 'submit',
69                                                                           'value' => _t('Search')));
70                                            
71                 common_element_end('p');
72                 common_element_end('form');
73                 if ($q) {
74                         $this->show_results($q, $page);
75                 }
76                 common_show_footer();
77         }
78         
79         function show_results($q, $page) {
80                 
81                 $profile = new Profile();
82
83                 # lcase it for comparison
84                 $q = strtolower($q);
85                 $profile->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' . 
86                                                    'against (\''.addslashes($q).'\')');
87
88                 # Ask for an extra to see if there's more.
89                 
90                 $profile->limit((($page-1)*PROFILES_PER_PAGE), PROFILES_PER_PAGE + 1);
91
92                 $cnt = $profile->find();
93
94                 if ($cnt > 0) {
95                         $terms = preg_split('/[\s,]+/', $q);
96                         common_element_start('ul', array('id' => 'profiles'));
97                         for ($i = 0; $i < min($cnt, PROFILES_PER_PAGE); $i++) {
98                                 if ($profile->fetch()) {
99                                         $this->show_profile($profile, $terms);
100                                 } else {
101                                         // shouldn't happen!
102                                         break;
103                                 }
104                         }
105                         common_element_end('ul');
106                 } else {
107                         common_element('p', 'error', _t('No results'));
108                 }
109                 
110                 common_pagination($page > 1, $cnt > PROFILES_PER_PAGE,
111                                                   $page, 'peoplesearch', array('q' => $q));
112         }
113         
114         function show_profile($profile, $terms) {
115                 common_element_start('li', array('class' => 'profile_single',
116                                                                                  'id' => 'profile-' . $profile->id));
117                 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
118                 common_element_start('a', array('href' => $profile->profileurl));
119                 common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
120                                                                         'class' => 'avatar stream',
121                                                                         'width' => AVATAR_STREAM_SIZE,
122                                                                         'height' => AVATAR_STREAM_SIZE,
123                                                                         'alt' =>
124                                                                         ($profile->fullname) ? $profile->fullname :
125                                                                         $profile->nickname));
126                 common_element_end('a');
127                 common_element_start('p');
128                 common_element_start('a', array('href' => $profile->profileurl,
129                                                                                 'class' => 'nickname'));
130                 common_raw($this->highlight($profile->nickname, $terms));
131                 common_element_end('a');
132                 if ($profile->fullname) {
133                         common_text(' | ');
134                         common_element_start('span', 'fullname');
135                         common_raw($this->highlight($profile->fullname, $terms));
136                         common_element_end('span');
137                 }
138                 if ($profile->location) {
139                         common_text(' | ');
140                         common_element_start('span', 'location');
141                         common_raw($this->highlight($profile->location, $terms));
142                         common_element_end('span');
143                 }
144                 common_element_end('p');
145                 if ($profile->homepage) {
146                         common_element_start('p', 'website');
147                         common_element('a', array('href' => $profile->homepage),
148                                                    $this->highlight($profile->homepage));
149                         common_element_end('p');
150                 }
151                 if ($profile->bio) {
152                         common_element_start('p', 'bio');
153                         common_raw($this->highlight($profile->bio, $terms));
154                         common_element_end('p');
155                 }
156                 common_element_end('li');
157         }
158
159         function highlight($text, $terms) {
160                 $pattern = '/('.implode('|',array_map('htmlspecialchars', $terms)).')/i';
161                 $result = preg_replace($pattern, '<strong>\\1</strong>', $text);
162                 return $result;
163         }
164 }