]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Directory/actions/userdirectory.php
Make paging work correctly in the user-directory
[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         // @todo fixme: This looks kinda gross
91
92         if ($this->filter == 'all') {
93             if ($this->page != 1) {
94                 // TRANS: Page title for user directory. %d is a page number.
95                 return(sprintf(_m('User Directory, page %d'), $this->page));
96             }
97             // TRANS: Page title for user directory.
98             return _m('User directory');
99         } else if ($this->page == 1) {
100             return sprintf(
101                 // TRANS: Page title for user directory. %s is the applied filter.
102                 _m('User directory - %s'),
103                 strtoupper($this->filter)
104             );
105         } else {
106             return sprintf(
107                 // TRANS: Page title for user directory.
108                 // TRANS: %1$s is the applied filter, %2$d is a page number.
109                 _m('User directory - %1$s, page %2$d'),
110                 strtoupper($this->filter),
111                 $this->page
112             );
113         }
114     }
115
116     /**
117      * Instructions for use
118      *
119      * @return instructions for use
120      */
121     function getInstructions()
122     {
123         // TRANS: %%site.name%% is the name of the StatusNet site.
124         return _m('Search for people on %%site.name%% by their name, '
125             . 'location, or interests. Separate the terms by spaces; '
126             . ' they must be 3 characters or more.'
127         );
128     }
129
130     /**
131      * Is this page read-only?
132      *
133      * @return boolean true
134      */
135     function isReadOnly($args)
136     {
137         return true;
138     }
139
140     /**
141      * Take arguments for running
142      *
143      * @param array $args $_REQUEST args
144      *
145      * @return boolean success flag
146      */
147     function prepare($args)
148     {
149         parent::prepare($args);
150
151         $this->page    = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
152         $this->filter  = $this->arg('filter', 'all');
153         $this->reverse = $this->boolean('reverse');
154         $this->q       = $this->trimmed('q');
155         $this->sort    = $this->arg('sort', 'nickname');
156
157         common_set_returnto($this->selfUrl());
158
159         return true;
160     }
161
162     /**
163      * Handle request
164      *
165      * Shows the page
166      *
167      * @param array $args $_REQUEST args; handled in prepare()
168      *
169      * @return void
170      */
171     function handle($args)
172     {
173         parent::handle($args);
174         $this->showPage();
175     }
176
177     /**
178      * Show the page notice
179      *
180      * Shows instructions for the page
181      *
182      * @return void
183      */
184     function showPageNotice()
185     {
186         $instr  = $this->getInstructions();
187         $output = common_markup_to_html($instr);
188
189         $this->elementStart('div', 'instructions');
190         $this->raw($output);
191         $this->elementEnd('div');
192     }
193
194
195     /**
196      * Content area
197      *
198      * Shows the list of popular notices
199      *
200      * @return void
201      */
202     function showContent()
203     {
204         $this->showForm();
205
206         $this->elementStart('div', array('id' => 'profile_directory'));
207
208         $alphaNav = new AlphaNav($this, false, false, array('0-9', 'All'));
209         $alphaNav->show();
210
211         $profile = null;
212         $profile = $this->getUsers();
213         $cnt     = 0;
214
215         if (!empty($profile)) {
216             $profileList = new SortableSubscriptionList(
217                 $profile,
218                 common_current_user(),
219                 $this
220             );
221
222             $cnt = $profileList->show();
223             $profile->free();
224
225             if (0 == $cnt) {
226                 $this->showEmptyListMessage();
227             }
228         }
229
230         $args = array();
231         if (isset($this->q)) {
232             $args['q'] = $this->q;
233         } elseif (isset($this->filter) && $this->filter != 'all') {
234             $args['filter'] = $this->filter;
235         }
236
237         $this->pagination(
238             $this->page > 1,
239             $cnt > PROFILES_PER_PAGE,
240             $this->page,
241             'userdirectory',
242             $args
243         );
244
245         $this->elementEnd('div');
246
247     }
248
249     function showForm($error=null)
250     {
251         $this->elementStart(
252             'form',
253             array(
254                 'method' => 'get',
255                 'id'     => 'form_search',
256                 'class'  => 'form_settings',
257                 'action' => common_local_url('userdirectory')
258             )
259         );
260
261         $this->elementStart('fieldset');
262
263         // TRANS: Fieldset legend.
264         $this->element('legend', null, _m('Search site'));
265         $this->elementStart('ul', 'form_data');
266         $this->elementStart('li');
267
268         // TRANS: Field label for user directory filter.
269         $this->input('q', _m('Keyword(s)'), $this->q);
270
271         // TRANS: Button text.
272         $this->submit('search', _m('BUTTON','Search'));
273         $this->elementEnd('li');
274         $this->elementEnd('ul');
275         $this->elementEnd('fieldset');
276         $this->elementEnd('form');
277     }
278
279     /*
280      * Get users filtered by the current filter, sort key,
281      * sort order, and page
282      */
283     function getUsers()
284     {
285         $profile = new Profile();
286
287         $offset = ($this->page - 1) * PROFILES_PER_PAGE;
288         $limit  = PROFILES_PER_PAGE + 1;
289
290         if (isset($this->q)) {
291              // User is searching via query
292              $search_engine = $profile->getSearchEngine('profile');
293
294              $mode = 'reverse_chron';
295
296              if ($this->sort == 'nickname') {
297                  if ($this->reverse) {
298                      $mode = 'nickname_desc';
299                  } else {
300                      $mode = 'nickname_asc';
301                  }
302              } else {
303                  if ($this->reverse) {
304                      $mode = 'chron';
305                  }
306              }
307
308              $search_engine->set_sort_mode($mode);
309              $search_engine->limit($offset, $limit);
310              $search_engine->query($this->q);
311
312              $profile->find();
313         } else {
314             // User is browsing via AlphaNav
315             $sort   = $this->getSortKey();
316             $sql    = 'SELECT profile.* FROM profile, user WHERE profile.id = user.id';
317
318             switch($this->filter)
319             {
320             case 'all':
321                 // NOOP
322                 break;
323             case '0-9':
324                 $sql .=
325                     '  AND LEFT(profile.nickname, 1) BETWEEN \'0\' AND \'9\'';
326                 break;
327             default:
328                 $sql .= sprintf(
329                     ' AND LEFT(LOWER(profile.nickname), 1) = \'%s\'',
330                     $this->filter
331                 );
332             }
333
334             $sql .= sprintf(
335                 ' ORDER BY profile.%s %s, profile.nickname ASC LIMIT %d, %d',
336                 $sort,
337                 $this->reverse ? 'DESC' : 'ASC',
338                 $offset,
339                 $limit
340             );
341
342             $profile->query($sql);
343         }
344
345         return $profile;
346     }
347
348     /**
349      * Filter the sort parameter
350      *
351      * @return string   a column name for sorting
352      */
353     function getSortKey()
354     {
355         switch ($this->sort) {
356         case 'nickname':
357             return $this->sort;
358             break;
359         case 'created':
360             return $this->sort;
361             break;
362         default:
363             return 'nickname';
364         }
365     }
366
367     /**
368      * Show a nice message when there's no search results
369      */
370     function showEmptyListMessage()
371     {
372         if (!empty($this->filter) && ($this->filter != 'all')) {
373             $this->element(
374                 'p',
375                 'error',
376                 sprintf(
377                     // TRANS: Empty list message for user directory.
378                     _m('No users starting with %s'),
379                     $this->filter
380                 )
381             );
382         } else {
383             // TRANS: Empty list message for user directory.
384             $this->element('p', 'error', _m('No results.'));
385             // TRANS: Standard search suggestions shown when a search does not give any results.
386             $message = _m("* Make sure all words are spelled correctly.
387 * Try different keywords.
388 * Try more general keywords.
389 * Try fewer keywords.");
390             $message .= "\n";
391
392             $this->elementStart('div', 'help instructions');
393             $this->raw(common_markup_to_html($message));
394             $this->elementEnd('div');
395         }
396     }
397 }