]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Directory/actions/userdirectory.php
Merge branch 'master' of gitorious.org:social/mainline into social-master
[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(array $args=array())
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(array $args=array())
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(array $args=array())
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         if (isset($this->sort)) {
238             $args['sort'] = $this->sort;
239         }        
240         if (!empty($this->reverse)) {
241             $args['reverse'] = $this->reverse;
242         }
243
244         $this->pagination(
245             $this->page > 1,
246             $cnt > PROFILES_PER_PAGE,
247             $this->page,
248             'userdirectory',
249             $args
250         );
251
252         $this->elementEnd('div');
253
254     }
255
256     function showForm($error=null)
257     {
258         $this->elementStart(
259             'form',
260             array(
261                 'method' => 'get',
262                 'id'     => 'form_search',
263                 'class'  => 'form_settings',
264                 'action' => common_local_url('userdirectory')
265             )
266         );
267
268         $this->elementStart('fieldset');
269
270         // TRANS: Fieldset legend.
271         $this->element('legend', null, _m('Search site'));
272         $this->elementStart('ul', 'form_data');
273         $this->elementStart('li');
274
275         // TRANS: Field label for user directory filter.
276         $this->input('q', _m('Keyword(s)'), $this->q);
277
278         // TRANS: Button text.
279         $this->submit('search', _m('BUTTON','Search'));
280         $this->elementEnd('li');
281         $this->elementEnd('ul');
282         $this->elementEnd('fieldset');
283         $this->elementEnd('form');
284     }
285
286     /*
287      * Get users filtered by the current filter, sort key,
288      * sort order, and page
289      */
290     function getUsers()
291     {
292         $profile = new Profile();
293
294         $offset = ($this->page - 1) * PROFILES_PER_PAGE;
295         $limit  = PROFILES_PER_PAGE + 1;
296
297         if (isset($this->q)) {
298              // User is searching via query
299              $search_engine = $profile->getSearchEngine('profile');
300
301              $mode = 'reverse_chron';
302
303              if ($this->sort == 'nickname') {
304                  if ($this->reverse) {
305                      $mode = 'nickname_desc';
306                  } else {
307                      $mode = 'nickname_asc';
308                  }
309              } else {
310                  if ($this->reverse) {
311                      $mode = 'chron';
312                  }
313              }
314
315              $search_engine->set_sort_mode($mode);
316              $search_engine->limit($offset, $limit);
317              $search_engine->query($this->q);
318
319              $profile->find();
320         } else {
321             // User is browsing via AlphaNav
322             $sort   = $this->getSortKey();
323             $sql    = 'SELECT profile.* FROM profile, user WHERE profile.id = user.id';
324
325             switch($this->filter)
326             {
327             case 'all':
328                 // NOOP
329                 break;
330             case '0-9':
331                 $sql .=
332                     '  AND LEFT(profile.nickname, 1) BETWEEN \'0\' AND \'9\'';
333                 break;
334             default:
335                 $sql .= sprintf(
336                     ' AND LEFT(LOWER(profile.nickname), 1) = \'%s\'',
337                     $this->filter
338                 );
339             }
340
341             $sql .= sprintf(
342                 ' ORDER BY profile.%s %s, profile.nickname ASC LIMIT %d, %d',
343                 $sort,
344                 $this->reverse ? 'DESC' : 'ASC',
345                 $offset,
346                 $limit
347             );
348
349             $profile->query($sql);
350         }
351
352         return $profile;
353     }
354
355     /**
356      * Filter the sort parameter
357      *
358      * @return string   a column name for sorting
359      */
360     function getSortKey()
361     {
362         switch ($this->sort) {
363         case 'nickname':
364             return $this->sort;
365             break;
366         case 'created':
367             return $this->sort;
368             break;
369         default:
370             return 'nickname';
371         }
372     }
373
374     /**
375      * Show a nice message when there's no search results
376      */
377     function showEmptyListMessage()
378     {
379         if (!empty($this->filter) && ($this->filter != 'all')) {
380             $this->element(
381                 'p',
382                 'error',
383                 sprintf(
384                     // TRANS: Empty list message for user directory.
385                     _m('No users starting with %s'),
386                     $this->filter
387                 )
388             );
389         } else {
390             // TRANS: Empty list message for user directory.
391             $this->element('p', 'error', _m('No results.'));
392             // TRANS: Standard search suggestions shown when a search does not give any results.
393             $message = _m("* Make sure all words are spelled correctly.
394 * Try different keywords.
395 * Try more general keywords.
396 * Try fewer keywords.");
397             $message .= "\n";
398
399             $this->elementStart('div', 'help instructions');
400             $this->raw(common_markup_to_html($message));
401             $this->elementEnd('div');
402         }
403     }
404 }