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