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