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