]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Directory/actions/userdirectory.php
Merge branch 'qna' 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 _m(
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     /**
192      * Content area
193      *
194      * Shows the list of popular notices
195      *
196      * @return void
197      */
198     function showContent()
199     {
200         $this->showForm();
201
202         $this->elementStart('div', array('id' => 'user_directory'));
203
204         $alphaNav = new AlphaNav($this, false, false, array('0-9', 'All'));
205         $alphaNav->show();
206
207         $profile = null;
208         $profile = $this->getUsers();
209         $cnt     = 0;
210
211         if (!empty($profile)) {
212             $profileList = new SortableSubscriptionList(
213                 $profile,
214                 common_current_user(),
215                 $this
216             );
217
218             $cnt = $profileList->show();
219             $profile->free();
220
221             if (0 == $cnt) {
222                 $this->showEmptyListMessage();
223             }
224         }
225
226         $args = array();
227         if (isset($this->q)) {
228             $args['q'] = $this->q;
229         } else {
230             $args['filter'] = $this->filter;
231         }
232
233         $this->pagination(
234             $this->page > 1,
235             $cnt > PROFILES_PER_PAGE,
236             $this->page,
237             'userdirectory',
238             $args
239         );
240
241         $this->elementEnd('div');
242
243     }
244
245     function showForm($error=null)
246     {
247         $this->elementStart(
248             'form',
249             array(
250                 'method' => 'get',
251                 'id'     => 'form_search',
252                 'class'  => 'form_settings',
253                 'action' => common_local_url('userdirectory')
254             )
255         );
256
257         $this->elementStart('fieldset');
258
259         $this->element('legend', null, _m('Search site'));
260         $this->elementStart('ul', 'form_data');
261         $this->elementStart('li');
262
263         $this->input('q', _m('Keyword(s)'), $this->q);
264
265         $this->submit('search', _m('BUTTON','Search'));
266         $this->elementEnd('li');
267         $this->elementEnd('ul');
268         $this->elementEnd('fieldset');
269         $this->elementEnd('form');
270     }
271
272     /*
273      * Get users filtered by the current filter, sort key,
274      * sort order, and page
275      */
276     function getUsers()
277     {
278         $profile = new Profile();
279
280         $offset = ($this->page - 1) * PROFILES_PER_PAGE;
281         $limit  = PROFILES_PER_PAGE + 1;
282
283         if (isset($this->q)) {
284              // User is searching via query
285              $search_engine = $profile->getSearchEngine('profile');
286
287              $mode = 'reverse_chron';
288
289              if ($this->sort == 'nickname') {
290                  if ($this->reverse) {
291                      $mode = 'nickname_desc';
292                  } else {
293                      $mode = 'nickname_asc';
294                  }
295              } else {
296                  if ($this->reverse) {
297                      $mode = 'chron';
298                  }
299              }
300
301              $search_engine->set_sort_mode($mode);
302              $search_engine->limit($offset, $limit);
303              $search_engine->query($this->q);
304
305              $profile->find();
306         } else {
307             // User is browsing via AlphaNav
308             $sort   = $this->getSortKey();
309             $sql    = 'SELECT profile.* FROM profile, user WHERE profile.id = user.id';
310
311             switch($this->filter)
312             {
313             case 'all':
314                 // NOOP
315                 break;
316             case '0-9':
317                 $sql .=
318                     '  AND LEFT(profile.nickname, 1) BETWEEN \'0\' AND \'9\'';
319                 break;
320             default:
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         if (!empty($this->filter) && ($this->filter != 'all')) {
366             $this->element(
367                 'p',
368                 'error',
369                 sprintf(
370                     _m('No users starting with %s'),
371                     $this->filter
372                 )
373             );
374         } else {
375             $this->element('p', 'error', _m('No results.'));
376             $message = _m(<<<E_O_T
377 * Make sure all words are spelled correctly.
378 * Try different keywords.
379 * Try more general keywords.
380 * Try fewer keywords.
381 E_O_T
382 );
383             $this->elementStart('div', 'help instructions');
384             $this->raw(common_markup_to_html($message));
385             $this->elementEnd('div');
386         }
387     }
388
389 }