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