]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Directory/actions/groupdirectory.php
DirectoryPlugin - Group directory mostly working. HTML markup for results needs fixing.
[quix0rs-gnu-social.git] / plugins / Directory / actions / groupdirectory.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Output a group 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  * Group directory
39  *
40  * @category Directory
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 GroupdirectoryAction 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('Group Directory, page %d'), $this->page));
95             }
96             return _m('Group directory');
97         } else if ($this->page == 1) {
98             return sprintf(
99                 _m('Group directory - %s'),
100                 strtoupper($this->filter)
101             );
102         } else {
103             return sprintf(
104                 _m('Group 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 groups 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' => 'group_directory'));
203
204         $alphaNav = new AlphaNav($this, false, false, array('0-9', 'All'));
205         $alphaNav->show();
206
207         $group   = null;
208         $group   = $this->getGroups();
209         $cnt     = 0;
210
211         if (!empty($group)) {
212             $groupList = new SortableGroupList(
213                 $group,
214                 common_current_user(),
215                 $this
216             );
217
218             $cnt = $groupList->show();
219             $group->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 > GROUPS_PER_PAGE,
236             $this->page,
237             'groupdirectory',
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('groupdirectory')
254             )
255         );
256
257         $this->elementStart('fieldset');
258
259         $this->element('legend', null, _m('Search groups'));
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 groups filtered by the current filter, sort key,
274      * sort order, and page
275      */
276     function getGroups()
277     {
278         $group = new User_group();
279
280         $offset = ($this->page-1) * GROUPS_PER_PAGE;
281         $limit  = GROUPS_PER_PAGE + 1;
282
283         if (isset($this->q)) {
284              
285              $order = 'user_group.created ASC';
286
287              if ($this->sort == 'nickname') {
288                  if ($this->reverse) {
289                      $order = 'user_group.nickname DESC';
290                  } else {
291                      $order = 'user_group.nickname ASC';
292                  }
293              } else {
294                  if ($this->reverse) {
295                      $order = 'user_group.created DESC';
296                  }
297              }
298
299              $sql = <<< GROUP_QUERY_END
300 SELECT user_group.*
301 FROM user_group
302 JOIN local_group ON user_group.id = local_group.group_id
303 ORDER BY %s
304 LIMIT %d, %d
305 GROUP_QUERY_END;
306
307         $cnt = 0;
308         $group->query(sprintf($sql, $order, $limit, $offset));
309         $group->find();
310
311         } else {
312             // User is browsing via AlphaNav
313             $sort   = $this->getSortKey();
314
315             $sql = <<< GROUP_QUERY_END
316 SELECT user_group.*
317 FROM user_group
318 JOIN local_group ON user_group.id = local_group.group_id
319 GROUP_QUERY_END;
320
321             switch($this->filter)
322             {
323             case 'all':
324                 // NOOP
325                 break;
326             case '0-9':
327                 $sql .=
328                     '  AND LEFT(user_group.nickname, 1) BETWEEN \'0\' AND \'9\'';
329                 break;
330             default:
331                 $sql .= sprintf(
332                     ' AND LEFT(LOWER(user_group.nickname), 1) = \'%s\'',
333                     $this->filter
334                 );
335             }
336
337             $sql .= sprintf(
338                 ' ORDER BY user_group.%s %s, user_group.nickname ASC LIMIT %d, %d',
339                 $sort,
340                 $this->reverse ? 'DESC' : 'ASC',
341                 $offset,
342                 $limit
343             );
344
345             $group->query($sql);
346         }
347
348         return $group;
349     }
350
351     /**
352      * Filter the sort parameter
353      *
354      * @return string   a column name for sorting
355      */
356     function getSortKey()
357     {
358         switch ($this->sort) {
359         case 'nickname':
360             return $this->sort;
361             break;
362         case 'created':
363             return $this->sort;
364             break;
365         default:
366             return 'nickname';
367         }
368     }
369
370     /**
371      * Show a nice message when there's no search results
372      */
373     function showEmptyListMessage()
374     {
375         if (!empty($this->filter) && ($this->filter != 'all')) {
376             $this->element(
377                 'p',
378                 'error',
379                 sprintf(
380                     _m('No groups starting with %s'),
381                     $this->filter
382                 )
383             );
384         } else {
385             $this->element('p', 'error', _m('No results.'));
386             $message = _m(<<<E_O_T
387 * Make sure all words are spelled correctly.
388 * Try different keywords.
389 * Try more general keywords.
390 * Try fewer keywords.
391 E_O_T
392 );
393             $this->elementStart('div', 'help instructions');
394             $this->raw(common_markup_to_html($message));
395             $this->elementEnd('div');
396         }
397     }
398
399     // XXX This needs some adjustment
400
401 /*
402     function showSections()
403     {
404         $gbp = new GroupsByPostsSection($this);
405         $gbp->show();
406         $gbm = new GroupsByMembersSection($this);
407         $gbm->show();
408     }
409 */
410
411 }