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