]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Directory/actions/userdirectory.php
fixup comments
[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     protected $page   = null;
54
55     /**
56      * what to filter the search results by
57      *
58      * @var string
59      */
60     protected $filter = null;
61
62     /**
63      * Title of the page
64      *
65      * @return string Title of the page
66      */
67     function title()
68     {
69         // @fixme: This looks kinda gross
70
71         if ($this->filter == 'all') {
72             if ($this->page != 1) {
73                 return(sprintf(_m('All users, page %d'), $this->page));
74             }
75             return _m('All users');
76         }
77
78         if ($this->page == 1) {
79             return sprintf(
80                 _m('Users with nicknames beginning with %s'),
81                 $this->filter
82             );
83         } else {
84             return sprintf(
85                 _m('Users with nicknames starting with %s, page %d'),
86                 $this->filter,
87                 $this->page
88             );
89         }
90     }
91
92     /**
93      * Instructions for use
94      *
95      * @return instructions for use
96      */
97     function getInstructions()
98     {
99         return _('User directory');
100     }
101
102     /**
103      * Is this page read-only?
104      *
105      * @return boolean true
106      */
107     function isReadOnly($args)
108     {
109         return true;
110     }
111
112     /**
113      * Take arguments for running
114      *
115      * @param array $args $_REQUEST args
116      *
117      * @return boolean success flag
118      */
119     function prepare($args)
120     {
121         parent::prepare($args);
122
123         $this->page   = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
124         $filter       = $this->arg('filter');
125         $this->filter = isset($filter) ? $filter : 'all';
126         $this->sort   = $this->arg('sort');
127         $this->order  = $this->boolean('asc'); // ascending or decending
128
129         common_set_returnto($this->selfUrl());
130
131         return true;
132     }
133
134     /**
135      * Handle request
136      *
137      * Shows the page
138      *
139      * @param array $args $_REQUEST args; handled in prepare()
140      *
141      * @return void
142      */
143     function handle($args)
144     {
145         parent::handle($args);
146         $this->showPage();
147     }
148
149     /**
150      * Show the page notice
151      *
152      * Shows instructions for the page
153      *
154      * @return void
155      */
156     function showPageNotice()
157     {
158         $instr  = $this->getInstructions();
159         $output = common_markup_to_html($instr);
160
161         $this->elementStart('div', 'instructions');
162         $this->raw($output);
163         $this->elementEnd('div');
164     }
165
166     /**
167      * Local navigation
168      *
169      * This page is part of the public group, so show that.
170      *
171      * @return void
172      */
173     function showLocalNav()
174     {
175         $nav = new PublicGroupNav($this);
176         $nav->show();
177     }
178
179     /**
180      * Content area
181      *
182      * Shows the list of popular notices
183      *
184      * @return void
185      */
186     function showContent()
187     {
188         // XXX Need search bar
189
190         $this->elementStart('div', array('id' => 'user_directory'));
191
192         $alphaNav = new AlphaNav($this, true, array('All'));
193         $alphaNav->show();
194
195         // XXX Maybe use a more specialized version of ProfileList here
196
197         $profile = $this->getUsers();
198         $cnt     = 0;
199
200         if (!empty($profile)) {
201             $profileList = new SortableSubscriptionList(
202                 $profile,
203                 common_current_user(),
204                 $this
205             );
206
207             $cnt = $profileList->show();
208
209             if (0 == $cnt) {
210                 $this->showEmptyListMessage();
211             }
212         }
213
214         $this->pagination(
215             $this->page > 1,
216             $cnt > PROFILES_PER_PAGE,
217             $this->page,
218             'userdirectory',
219             array('filter' => $this->filter)
220         );
221
222         $this->elementEnd('div');
223
224     }
225
226     /*
227      * Get users filtered by the current filter, sort key,
228      * sort order, and page
229      */
230     function getUsers()
231     {
232
233         $profile = new Profile();
234
235         $offset = ($this->page - 1) * PROFILES_PER_PAGE;
236         $limit  = PROFILES_PER_PAGE + 1;
237         $sort   = $this->getSortKey();
238         $sql    = 'SELECT profile.* FROM profile, user WHERE profile.id = user.id';
239
240         if ($this->filter != 'all') {
241             $sql .= sprintf(
242                 ' AND LEFT(LOWER(profile.nickname), 1) = \'%s\'',
243                 $this->filter
244             );
245         }
246
247         $sql .= sprintf(
248             ' ORDER BY profile.%s %s, profile.nickname DESC LIMIT %d, %d',
249             $sort,
250             ($this->order) ? 'ASC' : 'DESC',
251             $offset,
252             $limit
253         );
254
255         $profile->query($sql);
256
257         return $profile;
258     }
259
260     /**
261      * Filter the sort parameter
262      *
263      * @return string   a column name for sorting
264      */
265     function getSortKey()
266     {
267         switch ($this->sort) {
268         case 'nickname':
269             return $this->sort;
270             break;
271         case 'created':
272             return $this->sort;
273             break;
274         default:
275             return 'nickname';
276         }
277     }
278
279     /**
280      * Show a nice message when there's no search results
281      */
282     function showEmptyListMessage()
283     {
284         $message = sprintf(_m('No users starting with **%s**'), $this->filter);
285
286         $this->elementStart('div', 'guide');
287         $this->raw(common_markup_to_html($message));
288         $this->elementEnd('div');
289     }
290
291 }