3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/searchaction.php');
23 define(PROFILES_PER_PAGE, 10);
25 class PeoplesearchAction extends SearchAction {
27 function get_instructions() {
28 return _t('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.');
32 function get_title() {
33 return _t('People search');
36 function show_results($q, $page) {
38 $profile = new Profile();
40 # lcase it for comparison
42 $profile->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
43 'against (\''.addslashes($q).'\')');
45 # Ask for an extra to see if there's more.
47 $profile->limit((($page-1)*PROFILES_PER_PAGE), PROFILES_PER_PAGE + 1);
49 $cnt = $profile->find();
52 $terms = preg_split('/[\s,]+/', $q);
53 common_element_start('ul', array('id' => 'profiles'));
54 for ($i = 0; $i < min($cnt, PROFILES_PER_PAGE); $i++) {
55 if ($profile->fetch()) {
56 $this->show_profile($profile, $terms);
62 common_element_end('ul');
64 common_element('p', 'error', _t('No results'));
67 common_pagination($page > 1, $cnt > PROFILES_PER_PAGE,
68 $page, 'peoplesearch', array('q' => $q));
71 function show_profile($profile, $terms) {
72 common_debug(print_r($terms, TRUE), __FILE__);
73 common_element_start('li', array('class' => 'profile_single',
74 'id' => 'profile-' . $profile->id));
75 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
76 common_element_start('a', array('href' => $profile->profileurl));
77 common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
78 'class' => 'avatar stream',
79 'width' => AVATAR_STREAM_SIZE,
80 'height' => AVATAR_STREAM_SIZE,
82 ($profile->fullname) ? $profile->fullname :
84 common_element_end('a');
85 common_element_start('p');
86 common_element_start('a', array('href' => $profile->profileurl,
87 'class' => 'nickname'));
88 common_raw($this->highlight($profile->nickname, $terms));
89 common_element_end('a');
90 if ($profile->fullname) {
92 common_element_start('span', 'fullname');
93 common_raw($this->highlight($profile->fullname, $terms));
94 common_element_end('span');
96 if ($profile->location) {
98 common_element_start('span', 'location');
99 common_raw($this->highlight($profile->location, $terms));
100 common_element_end('span');
102 common_element_end('p');
103 if ($profile->homepage) {
104 common_element_start('p', 'website');
105 common_element('a', array('href' => $profile->homepage),
106 $this->highlight($profile->homepage));
107 common_element_end('p');
110 common_element_start('p', 'bio');
111 common_raw($this->highlight($profile->bio, $terms));
112 common_element_end('p');
114 common_element_end('li');
117 function highlight($text, $terms) {
118 $pattern = '/('.implode('|',array_map('htmlspecialchars', $terms)).')/i';
119 $result = preg_replace($pattern, '<strong>\\1</strong>', htmlspecialchars($text));