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