]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/groupsearch.php
Merge branch 'master' of evan@dev.controlyourself.ca:/var/www/trunk
[quix0rs-gnu-social.git] / actions / groupsearch.php
1 <?php
2
3
4 //        define('GROUPS_PER_PAGE', 20);
5
6
7 /**
8  * Group search action class.
9  *
10  * PHP version 5
11  *
12  * @category Action
13  * @package  Laconica
14  * @author   Evan Prodromou <evan@controlyourself.ca>
15  * @author   Robin Millette <millette@controlyourself.ca>
16  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
17  * @link     http://laconi.ca/
18  *
19  * Laconica - a distributed open-source microblogging tool
20  * Copyright (C) 2008, Controlez-Vous, Inc.
21  *
22  * This program is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU Affero General Public License as published by
24  * the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU Affero General Public License for more details.
31  *
32  * You should have received a copy of the GNU Affero General Public License
33  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34  */
35
36 if (!defined('LACONICA')) {
37     exit(1);
38 }
39
40 //require_once INSTALLDIR.'/lib/searchaction.php';
41 //require_once INSTALLDIR.'/lib/profilelist.php';
42
43 /**
44  * Group search action class.
45  *
46  * @category Action
47  * @package  Laconica
48  * @author   Evan Prodromou <evan@controlyourself.ca>
49  * @author   Robin Millette <millette@controlyourself.ca>
50  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
51  * @link     http://laconi.ca/
52  */
53 class GroupsearchAction extends SearchAction
54 {
55     function getInstructions()
56     {
57         return _('Search for groups on %%site.name%% by their name, location, or description. ' .
58                   'Separate the terms by spaces; they must be 3 characters or more.');
59     }
60
61     function title()
62     {
63         return _('Group search');
64     }
65
66     function showResults($q, $page)
67     {
68         $user_group = new User_group;
69         $user_group->limit((($page-1)*GROUPS_PER_PAGE), GROUPS_PER_PAGE + 1);
70         $wheres = array('nickname', 'fullname', 'homepage', 'description', 'location');
71         foreach ($wheres as $where) {
72             $where_q = "$where like '%" . trim($user_group->escape($q), '\'') . '%\'';
73             $user_group->whereAdd($where_q, 'OR');
74         }
75         $cnt = $user_group->find();
76         if ($cnt > 0) {
77             $terms = preg_split('/[\s,]+/', $q);
78             $results = new GroupSearchResults($user_group, $terms, $this);
79             $results->show();
80         } else {
81             $this->element('p', 'error', _('No results'));
82         }
83         $user_group->free();
84         $this->pagination($page > 1, $cnt > GROUPS_PER_PAGE,
85                           $page, 'groupsearch', array('q' => $q));
86     }
87 }
88
89 class GroupSearchResults extends GroupList
90 {
91     var $terms = null;
92     var $pattern = null;
93     
94     function __construct($user_group, $terms, $action)
95     {
96         parent::__construct($user_group, $terms, $action);
97         $this->terms = array_map('preg_quote', 
98                                  array_map('htmlspecialchars', $terms));
99         $this->pattern = '/('.implode('|',$terms).')/i';
100     }
101     
102     function highlight($text)
103     {
104         return preg_replace($this->pattern, '<strong>\\1</strong>', htmlspecialchars($text));
105     }
106 }
107