]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Directory/actions/userdirectory.php
Initial go at a site directory plugin
[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     /* Page we're on */
49     protected $page   = null;
50
51     /* What to filter the search results by */
52     protected $filter = null;
53
54     /**
55      * Title of the page
56      *
57      * @return string Title of the page
58      */
59     function title()
60     {
61         // @fixme: This looks kinda gross
62
63         if ($this->filter == 'All') {
64             if ($this->page != 1) {
65                 return(sprintf(_m('All users, page %d'), $this->page));
66             }
67             return _m('All users');
68         }
69
70         if ($this->page == 1) {
71             return sprintf(
72                 _m('Users with nicknames beginning with %s'),
73                 $this->filter
74             );
75         } else {
76             return sprintf(
77                 _m('Users with nicknames starting with %s, page %d'),
78                 $this->filter,
79                 $this->page
80             );
81         }
82     }
83
84     /**
85      * Instructions for use
86      *
87      * @return instructions for use
88      */
89     function getInstructions()
90     {
91         return _('User directory');
92     }
93
94     /**
95      * Is this page read-only?
96      *
97      * @return boolean true
98      */
99     function isReadOnly($args)
100     {
101         return true;
102     }
103
104     /**
105      * Take arguments for running
106      *
107      * @param array $args $_REQUEST args
108      *
109      * @return boolean success flag
110      *
111      * @todo move queries from showContent() to here
112      */
113     function prepare($args)
114     {
115         parent::prepare($args);
116
117         $this->page   = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
118         $this->filter = $this->arg('filter') ? $this->arg('filter') : 'All';
119         common_set_returnto($this->selfUrl());
120
121         return true;
122     }
123
124     /**
125      * Handle request
126      *
127      * Shows the page
128      *
129      * @param array $args $_REQUEST args; handled in prepare()
130      *
131      * @return void
132      */
133     function handle($args)
134     {
135         parent::handle($args);
136         $this->showPage();
137     }
138
139     /**
140      * Show the page notice
141      *
142      * Shows instructions for the page
143      *
144      * @return void
145      */
146     function showPageNotice()
147     {
148         $instr  = $this->getInstructions();
149         $output = common_markup_to_html($instr);
150
151         $this->elementStart('div', 'instructions');
152         $this->raw($output);
153         $this->elementEnd('div');
154     }
155
156     /**
157      * Local navigation
158      *
159      * This page is part of the public group, so show that.
160      *
161      * @return void
162      */
163     function showLocalNav()
164     {
165         $nav = new PublicGroupNav($this);
166         $nav->show();
167     }
168
169     /**
170      * Content area
171      *
172      * Shows the list of popular notices
173      *
174      * @return void
175      */
176     function showContent()
177     {
178         // XXX Need search bar
179
180         $alphaNav = new AlphaNav($this, true, array('All'));
181         $alphaNav->show();
182
183         // XXX Maybe use a more specialized version of ProfileList here
184
185         $profile = $this->getUsers();
186         $cnt     = 0;
187
188         if (!empty($profile)) {
189             $profileList = new SubscriptionList(
190                 $profile,
191                 common_current_user(),
192                 $this
193             );
194
195             $cnt = $profileList->show();
196
197             if (0 == $cnt) {
198                 $this->showEmptyListMessage();
199             }
200         }
201
202         $this->pagination($this->page > 1, $cnt > PROFILES_PER_PAGE,
203                           $this->page, 'userdirectory',
204                           array('filter' => $this->filter));
205
206     }
207
208     /*
209      * Get users filtered by the current filter and page
210      */
211     function getUsers()
212     {
213         $offset = ($this->page-1) * PROFILES_PER_PAGE;
214         $limit =  PROFILES_PER_PAGE + 1;
215
216         $profile = new Profile();
217
218         if ($this->filter != 'All') {
219             $profile->whereAdd(
220                 sprintf('LEFT(UPPER(nickname), 1) = \'%s\'', $this->filter)
221             );
222         }
223         $profile->orderBy('created DESC, nickname');
224         $profile->find();
225
226         return $profile;
227     }
228
229     /**
230      * Show a nice message when there's no search results
231      */
232     function showEmptyListMessage()
233     {
234         $message = sprintf(_m('No users starting with %s'), $this->filter);
235
236         $this->elementStart('div', 'guide');
237         $this->raw(common_markup_to_html($message));
238         $this->elementEnd('div');
239     }
240
241 }