]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Directory/actions/groupdirectory.php
Group directory - use correct constant
[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: Page notice for groups directory.
119         // TRANS: %%site.name%% is the name of the StatusNet site.
120         // TRANS: %%%%action.newgroup%%%% is a URL. Do not change it.
121         // TRANS: This message contains Markdown links in the form [link text](link).
122         $instructions = <<< END_OF_INSTRUCTIONS
123 %%site.name%% groups let you find and talk with people of similar
124 interests. After you join a group you can send messages to all other
125 members using the syntax "!groupname".
126
127 Browse groups, or search for groups on by their name, location or topic.
128 Separate the terms by spaces; they must be three characters or more.
129
130 Don't see a group you like? [start your own](%%action.newgroup%%)!
131 END_OF_INSTRUCTIONS;
132
133         return _m($instructions);
134     }
135
136     /**
137      * Is this page read-only?
138      *
139      * @return boolean true
140      */
141     function isReadOnly($args)
142     {
143         return true;
144     }
145
146     /**
147      * Take arguments for running
148      *
149      * @param array $args $_REQUEST args
150      *
151      * @return boolean success flag
152      */
153     function prepare($args)
154     {
155         parent::prepare($args);
156
157         $this->page    = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
158         $this->filter  = $this->arg('filter', 'all');
159         $this->reverse = $this->boolean('reverse');
160         $this->q       = $this->trimmed('q');
161         $this->sort    = $this->arg('sort', 'nickname');
162
163         common_set_returnto($this->selfUrl());
164
165         return true;
166     }
167
168     /**
169      * Handle request
170      *
171      * Shows the page
172      *
173      * @param array $args $_REQUEST args; handled in prepare()
174      *
175      * @return void
176      */
177     function handle($args)
178     {
179         parent::handle($args);
180         $this->showPage();
181     }
182
183     /**
184      * Show the page notice
185      *
186      * Shows instructions for the page
187      *
188      * @return void
189      */
190     function showPageNotice()
191     {
192         $instr  = $this->getInstructions();
193         $output = common_markup_to_html($instr);
194
195         $this->elementStart('div', 'instructions');
196         $this->raw($output);
197         $this->elementEnd('div');
198     }
199
200
201     /**
202      * Content area
203      *
204      * Shows the list of popular notices
205      *
206      * @return void
207      */
208     function showContent()
209     {
210         $this->showForm();
211
212         $this->elementStart('div', array('id' => 'profile_directory'));
213
214         $alphaNav = new AlphaNav($this, false, false, array('0-9', 'All'));
215         $alphaNav->show();
216
217         $group   = null;
218         $group   = $this->getGroups();
219         $cnt     = 0;
220
221         if (!empty($group)) {
222             $groupList = new SortableGroupList(
223                 $group,
224                 common_current_user(),
225                 $this
226             );
227
228             $cnt = $groupList->show();
229             $group->free();
230
231             if (0 == $cnt) {
232                 $this->showEmptyListMessage();
233             }
234         }
235
236         $args = array();
237         if (isset($this->q)) {
238             $args['q'] = $this->q;
239         } else {
240             $args['filter'] = $this->filter;
241         }
242
243         $this->pagination(
244             $this->page > 1,
245             $cnt > PROFILES_PER_PAGE,
246             $this->page,
247             'groupdirectory',
248             $args
249         );
250
251         $this->elementEnd('div');
252     }
253
254     function showForm($error=null)
255     {
256         $this->elementStart(
257             'form',
258             array(
259                 'method' => 'get',
260                 'id'     => 'form_search',
261                 'class'  => 'form_settings',
262                 'action' => common_local_url('groupdirectory')
263             )
264         );
265
266         $this->elementStart('fieldset');
267
268         $this->element('legend', null, _m('Search groups'));
269         $this->elementStart('ul', 'form_data');
270         $this->elementStart('li');
271
272         $this->input('q', _m('Keyword(s)'), $this->q);
273
274         $this->submit('search', _m('BUTTON','Search'));
275         $this->elementEnd('li');
276         $this->elementEnd('ul');
277         $this->elementEnd('fieldset');
278         $this->elementEnd('form');
279     }
280
281     /*
282      * Get groups filtered by the current filter, sort key,
283      * sort order, and page
284      */
285     function getGroups()
286     {
287         $group = new User_group();
288
289         $offset = ($this->page-1) * PROFILES_PER_PAGE;
290         $limit  = PROFILES_PER_PAGE + 1;
291
292         if (isset($this->q)) {
293
294              $order = 'user_group.created ASC';
295
296              if ($this->sort == 'nickname') {
297                  if ($this->reverse) {
298                      $order = 'user_group.nickname DESC';
299                  } else {
300                      $order = 'user_group.nickname ASC';
301                  }
302              } else {
303                  if ($this->reverse) {
304                      $order = 'user_group.created DESC';
305                  }
306              }
307
308              $sql = <<< GROUP_QUERY_END
309 SELECT user_group.*
310 FROM user_group
311 JOIN local_group ON user_group.id = local_group.group_id
312 ORDER BY %s
313 LIMIT %d, %d
314 GROUP_QUERY_END;
315
316         $cnt = 0;
317         $group->query(sprintf($sql, $order, $limit, $offset));
318         $group->find();
319
320         } else {
321             // User is browsing via AlphaNav
322             $sort   = $this->getSortKey();
323
324             $sql = <<< GROUP_QUERY_END
325 SELECT user_group.*
326 FROM user_group
327 JOIN local_group ON user_group.id = local_group.group_id
328 GROUP_QUERY_END;
329
330             switch($this->filter)
331             {
332             case 'all':
333                 // NOOP
334                 break;
335             case '0-9':
336                 $sql .=
337                     '  AND LEFT(user_group.nickname, 1) BETWEEN \'0\' AND \'9\'';
338                 break;
339             default:
340                 $sql .= sprintf(
341                     ' AND LEFT(LOWER(user_group.nickname), 1) = \'%s\'',
342                     $this->filter
343                 );
344             }
345
346             $sql .= sprintf(
347                 ' ORDER BY user_group.%s %s, user_group.nickname ASC LIMIT %d, %d',
348                 $sort,
349                 $this->reverse ? 'DESC' : 'ASC',
350                 $offset,
351                 $limit
352             );
353
354             $group->query($sql);
355         }
356
357         return $group;
358     }
359
360     /**
361      * Filter the sort parameter
362      *
363      * @return string   a column name for sorting
364      */
365     function getSortKey()
366     {
367         switch ($this->sort) {
368         case 'nickname':
369             return $this->sort;
370             break;
371         case 'created':
372             return $this->sort;
373             break;
374         default:
375             return 'nickname';
376         }
377     }
378
379     /**
380      * Show a nice message when there's no search results
381      */
382     function showEmptyListMessage()
383     {
384         if (!empty($this->filter) && ($this->filter != 'all')) {
385             $this->element(
386                 'p',
387                 'error',
388                 sprintf(
389                     _m('No groups starting with %s'),
390                     $this->filter
391                 )
392             );
393         } else {
394             $this->element('p', 'error', _m('No results.'));
395             $message = _m(<<<E_O_T
396 * Make sure all words are spelled correctly.
397 * Try different keywords.
398 * Try more general keywords.
399 * Try fewer keywords.
400 E_O_T
401 );
402             $this->elementStart('div', 'help instructions');
403             $this->raw(common_markup_to_html($message));
404             $this->elementEnd('div');
405         }
406     }
407
408     // XXX This needs some adjustment
409
410 /*
411     function showSections()
412     {
413         $gbp = new GroupsByPostsSection($this);
414         $gbp->show();
415         $gbm = new GroupsByMembersSection($this);
416         $gbm->show();
417     }
418 */
419
420 }