]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Directory/actions/userdirectory.php
* Integrate search input box
[quix0rs-gnu-social.git] / plugins / Directory / actions / userdirectory.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Output a user directory
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Public
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2011 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET'))
31 {
32     exit(1);
33 }
34
35 require_once INSTALLDIR . '/lib/publicgroupnav.php';
36
37 /**
38  * User directory
39  *
40  * @category Personal
41  * @package  StatusNet
42  * @author   Zach Copley <zach@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46 class UserdirectoryAction extends Action
47 {
48     /**
49      * The page we're on
50      *
51      * @var integer
52      */
53     public $page;
54
55     /**
56      * What to filter the search results by
57      *
58      * @var string
59      */
60     public $filter;
61
62     /**
63      * Column to sort by
64      *
65      * @var string
66      */
67     public $sort;
68
69     /**
70      * How to order search results, ascending or descending
71      *
72      * @var string
73      */
74     public $reverse;
75
76     /**
77      * Query
78      *
79      * @var string
80      */
81     public $q;
82
83     /**
84      * Title of the page
85      *
86      * @return string Title of the page
87      */
88     function title()
89     {
90         // @fixme: This looks kinda gross
91
92         if ($this->filter == 'all') {
93             if ($this->page != 1) {
94                 return(sprintf(_m('All users, page %d'), $this->page));
95             }
96             return _m('All users');
97         }
98
99         if ($this->page == 1) {
100             return sprintf(
101                 _m('Users with nicknames beginning with %s'),
102                 $this->filter
103             );
104         } else {
105             return sprintf(
106                 _m('Users with nicknames starting with %s, page %d'),
107                 $this->filter,
108                 $this->page
109             );
110         }
111     }
112
113     /**
114      * Instructions for use
115      *
116      * @return instructions for use
117      */
118     function getInstructions()
119     {
120         return _('User directory');
121     }
122
123     /**
124      * Is this page read-only?
125      *
126      * @return boolean true
127      */
128     function isReadOnly($args)
129     {
130         return true;
131     }
132
133     /**
134      * Take arguments for running
135      *
136      * @param array $args $_REQUEST args
137      *
138      * @return boolean success flag
139      */
140     function prepare($args)
141     {
142         parent::prepare($args);
143
144         $this->page    = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
145         $this->filter  = $this->arg('filter', 'all');
146         $this->reverse = $this->boolean('reverse');
147         $this->q       = $this->trimmed('q');
148         $this->sort    = $this->arg('sort', 'nickname');
149
150         common_set_returnto($this->selfUrl());
151
152         return true;
153     }
154
155     /**
156      * Handle request
157      *
158      * Shows the page
159      *
160      * @param array $args $_REQUEST args; handled in prepare()
161      *
162      * @return void
163      */
164     function handle($args)
165     {
166         parent::handle($args);
167         $this->showPage();
168     }
169
170     /**
171      * Show the page notice
172      *
173      * Shows instructions for the page
174      *
175      * @return void
176      */
177     function showPageNotice()
178     {
179         $instr  = $this->getInstructions();
180         $output = common_markup_to_html($instr);
181
182         $this->elementStart('div', 'instructions');
183         $this->raw($output);
184         $this->elementEnd('div');
185     }
186
187     /**
188      * Local navigation
189      *
190      * This page is part of the public group, so show that.
191      *
192      * @return void
193      */
194     function showLocalNav()
195     {
196         $nav = new PublicGroupNav($this);
197         $nav->show();
198     }
199
200     /**
201      * Content area
202      *
203      * Shows the list of popular notices
204      *
205      * @return void
206      */
207     function showContent()
208     {
209         $this->showForm();
210
211         $this->elementStart('div', array('id' => 'user_directory'));
212
213         $alphaNav = new AlphaNav($this, true, array('All'));
214         $alphaNav->show();
215
216         $profile = null;
217         $profile = $this->getUsers();
218         $cnt     = 0;
219
220         if (!empty($profile)) {
221             $profileList = new SortableSubscriptionList(
222                 $profile,
223                 common_current_user(),
224                 $this
225             );
226
227             $cnt = $profileList->show();
228             $profile->free();
229
230             if (0 == $cnt) {
231                 $this->showEmptyListMessage();
232             }
233         }
234
235         $args = array();
236         if (isset($this->q)) {
237             $args['q'] = $this->q;
238         } else {
239             $args['filter'] = $this->filter;
240         }
241
242         $this->pagination(
243             $this->page > 1,
244             $cnt > PROFILES_PER_PAGE,
245             $this->page,
246             'userdirectory',
247             $args
248         );
249
250         $this->elementEnd('div');
251
252     }
253
254     function showForm($error=null)
255     {
256         $this->elementStart(
257             'form',
258             array(
259                 'method' => 'get',
260                 'id'     => 'form_search',
261                 'class'  => 'form_settings',
262                 'action' => common_local_url('userdirectory')
263             )
264         );
265
266         $this->elementStart('fieldset');
267
268         $this->element('legend', null, _('Search site'));
269         $this->elementStart('ul', 'form_data');
270         $this->elementStart('li');
271
272         $this->input('q', _('Keyword(s)'), $this->q);
273
274         $this->submit('search', _m('BUTTON','Search'));
275         $this->elementEnd('li');
276         $this->elementEnd('ul');
277         $this->elementEnd('fieldset');
278         $this->elementEnd('form');
279     }
280
281     /*
282      * Get users filtered by the current filter, sort key,
283      * sort order, and page
284      */
285     function getUsers()
286     {
287         $profile = new Profile();
288
289         $offset = ($this->page - 1) * PROFILES_PER_PAGE;
290         $limit  = PROFILES_PER_PAGE + 1;
291
292         if (isset($this->q)) {
293              // User is searching via query
294              $search_engine = $profile->getSearchEngine('profile');
295
296              $mode = 'reverse_chron';
297
298              if ($this->sort == 'nickname') {
299                  if ($this->reverse) {
300                      $mode = 'nickname_desc';
301                  } else {
302                      $mode = 'nickname_asc';
303                  }
304              } else {
305                  if ($this->reverse) {
306                      $mode = 'chron';
307                  }
308              }
309
310              $search_engine->set_sort_mode($mode);
311              $search_engine->limit($offset, $limit);
312              $search_engine->query($this->q);
313
314              $profile->find();
315         } else {
316             // User is browsing via AlphaNav
317             $sort   = $this->getSortKey();
318             $sql    = 'SELECT profile.* FROM profile, user WHERE profile.id = user.id';
319
320             if ($this->filter != 'all') {
321                 $sql .= sprintf(
322                     ' AND LEFT(LOWER(profile.nickname), 1) = \'%s\'',
323                     $this->filter
324                 );
325             }
326
327             $sql .= sprintf(
328                 ' ORDER BY profile.%s %s, profile.nickname ASC LIMIT %d, %d',
329                 $sort,
330                 $this->reverse ? 'DESC' : 'ASC',
331                 $offset,
332                 $limit
333             );
334
335             $profile->query($sql);
336         }
337
338         return $profile;
339     }
340
341     /**
342      * Filter the sort parameter
343      *
344      * @return string   a column name for sorting
345      */
346     function getSortKey()
347     {
348         switch ($this->sort) {
349         case 'nickname':
350             return $this->sort;
351             break;
352         case 'created':
353             return $this->sort;
354             break;
355         default:
356             return 'nickname';
357         }
358     }
359
360     /**
361      * Show a nice message when there's no search results
362      */
363     function showEmptyListMessage()
364     {
365         $message = sprintf(_m('No users starting with **%s**'), $this->filter);
366
367         $this->elementStart('div', 'guide');
368         $this->raw(common_markup_to_html($message));
369         $this->elementEnd('div');
370     }
371
372 }