]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/peoplesearch.php
missing comma in people search
[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 show_top($error=NULL) {
34         }
35         
36         function show_form($error=NULL) {
37                 $q = $this->trimmed('q');
38                 $page = $this->trimmed('page', 1);
39                 
40                 common_show_header(_t('Find people'), NULL, $error, array($this, 'show_top'));
41                 common_element_start('form', array('method' => 'post',
42                                                                                    'id' => 'login',
43                                                                                    'action' => common_local_url('peoplesearch')));
44                 common_element_start('p');
45                 common_element('input', array('name' => 'q',
46                                                                           'id' => 'q',
47                                                                           'type' => 'text',
48                                                                           'class' => 'input_text',
49                                                                           'value' => ($q) ? $q : ''));
50                 common_text(' ');
51                 common_element('input', array('type' => 'submit',
52                                                                           'id' => 'search',
53                                                                           'name' => 'search',
54                                                                           'class' => 'submit',
55                                                                           'value' => _t('Search')));
56                                            
57                 common_element_end('p');
58                 if ($q) {
59                         common_element('hr');
60                         $this->show_results($q, $page);
61                 }
62                 common_element_end('form');
63                 common_show_footer();
64         }
65         
66         function show_results($q, $page) {
67                 
68                 $profile = new Profile();
69
70                 # lcase it for comparison
71                 $q = strtolower($q);
72                 $profile->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' . 
73                                                    'against (\''.addslashes($q).'\')');
74
75                 # Ask for an extra to see if there's more.
76                 
77                 $profile->limit((($page-1)*PROFILES_PER_PAGE), PROFILES_PER_PAGE + 1);
78
79                 $cnt = $profile->find();
80
81                 if ($cnt > 0) {
82                         $terms = preg_split('/[\s,]+/', $q);
83                         common_element_start('ul', array('id' => 'profiles'));
84                         for ($i = 0; $i < min($cnt, PROFILES_PER_PAGE); $i++) {
85                                 if ($profile->fetch()) {
86                                         $this->show_profile($profile, $terms);
87                                 } else {
88                                         // shouldn't happen!
89                                         break;
90                                 }
91                         }
92                         common_element_end('ul');
93                 }
94                 
95                 common_pagination($page > 1, $cnt > PROFILES_PER_PAGE,
96                                                   $page, 'peoplesearch', array('q' => $q));
97         }
98         
99         function show_profile($profile, $terms) {
100                 common_start_element('li', array('class' => 'profile_single',
101                                                                                  'id' => 'profile-' . $profile->id));
102                 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
103                 common_element_start('a', array('href' => $profile->profileurl));
104                 common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
105                                                                         'class' => 'avatar stream',
106                                                                         'width' => AVATAR_STREAM_SIZE,
107                                                                         'height' => AVATAR_STREAM_SIZE,
108                                                                         'alt' =>
109                                                                         ($profile->fullname) ? $profile->fullname :
110                                                                         $profile->nickname));
111                 common_element_end('a');
112                 common_element_start('a', array('href' => $profile->profileurl,
113                                                                                 'class' => 'nickname'));
114                 common_raw($this->highlight($profile->nickname, $terms));
115                 common_element_end('a');
116                 if ($profile->fullname) {
117                         common_element_start('p', 'fullname');
118                         common_raw($this->highlight($profile->fullname, $terms));
119                         common_element_end('p');
120                 }
121                 if ($profile->location) {
122                         common_element_start('p', 'location');
123                         common_raw($this->highlight($profile->location, $terms));
124                         common_element_end('p');
125                 }
126                 if ($profile->location) {
127                         common_element_start('p', 'bio');
128                         common_raw($this->highlight($profile->bio, $terms));
129                         common_element_end('p');
130                 }
131                 common_element_end('li');
132         }
133
134         function highlight($text, $terms) {
135                 $pattern = '/('.implode('|',array_map('htmlspecialchars', $terms)).')/';
136                 $result = preg_replace($pattern, '<strong>\\1</strong>', $text);
137                 return $result;
138         }
139 }