]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Directory/actions/groupdirectory.php
f6b20d0cf5bc28f78dc0eebd554cdb84128faf84
[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('GNUSOCIAL')) { exit(1); }
31
32 /**
33  * Group directory
34  *
35  * @category Directory
36  * @package  StatusNet
37  * @author   Zach Copley <zach@status.net>
38  * @author   Mikael Nordfeldth <mmn@hethane.se>
39  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
40  * @link     http://status.net/
41  */
42 class GroupdirectoryAction extends ManagedAction
43 {
44     /**
45      * The page we're on
46      *
47      * @var integer
48      */
49     public $page;
50
51     /**
52      * What to filter the search results by
53      *
54      * @var string
55      */
56     public $filter;
57
58     /**
59      * Column to sort by
60      *
61      * @var string
62      */
63     public $sort;
64
65     /**
66      * How to order search results, ascending or descending
67      *
68      * @var string
69      */
70     public $reverse;
71
72     /**
73      * Query
74      *
75      * @var string
76      */
77     public $q;
78
79     /**
80      * Title of the page
81      *
82      * @return string Title of the page
83      */
84     function title()
85     {
86         // @fixme: This looks kinda gross
87
88         if ($this->filter == 'all') {
89             if ($this->page != 1) {
90                 // TRANS: Title for group directory page. %d is a page number.
91                 return(sprintf(_m('Group Directory, page %d'), $this->page));
92             }
93             // TRANS: Title for group directory page.
94             return _m('Group directory');
95         } else if ($this->page == 1) {
96             return sprintf(
97                 // TRANS: Title for group directory page when it is filtered.
98                 // TRANS: %s is the filter string.
99                 _m('Group directory - %s'),
100                 strtoupper($this->filter)
101             );
102         } else {
103             return sprintf(
104                 // TRANS: Title for group directory page when it is filtered.
105                 // TRANS: %1$s is the filter string, %2$d is a page number.
106                 _m('Group directory - %1$s, page %2$d'),
107                 strtoupper($this->filter),
108                 $this->page
109             );
110         }
111     }
112
113     /**
114      * Instructions for use
115      *
116      * @return instructions for use
117      */
118     function getInstructions()
119     {
120         // TRANS: Page instructions.
121         return _m("After you join a group you can send messages to all other members\n".
122                  "using the syntax \"!groupname\".\n\n".
123                  "Browse groups, or search for groups by their name, location or topic.\n".
124                  "Separate the terms by spaces; they must be three characters or more.") . "\n";
125     }
126
127     /**
128      * Is this page read-only?
129      *
130      * @return boolean true
131      */
132     function isReadOnly($args)
133     {
134         return true;
135     }
136
137     protected function doPreparation()
138     {
139         $this->page    = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
140         $this->filter  = $this->arg('filter', 'all');
141         $this->reverse = $this->boolean('reverse');
142         $this->q       = $this->trimmed('q');
143         $this->sort    = $this->arg('sort', 'nickname');
144
145         common_set_returnto($this->selfUrl());
146
147         return true;
148     }
149
150     /**
151      * Show the page notice
152      *
153      * Shows instructions for the page
154      *
155      * @return void
156      */
157     function showPageNotice()
158     {
159         $instr  = $this->getInstructions();
160         $output = common_markup_to_html($instr);
161
162         $this->elementStart('div', 'instructions');
163         $this->raw($output);
164         $this->elementEnd('div');
165     }
166
167
168     /**
169      * Content area
170      *
171      * Shows the groups
172      *
173      * @return void
174      */
175     function showContent()
176     {
177         if (common_logged_in()) {
178             $this->elementStart(
179                 'p',
180                 array(
181                     'id' => 'new_group'
182                 )
183             );
184             $this->element(
185                 'a',
186                 array(
187                     'href'  => common_local_url('newgroup'),
188                     'class' => 'more'),
189                     // TRANS: Link to create a new group on the group list page.
190                     _m('Create a new group')
191             );
192             $this->elementEnd('p');
193         }
194
195         $this->showForm();
196
197         $this->elementStart('div', array('id' => 'profile_directory'));
198
199         // @todo FIXME: Does "All" need i18n here?
200         $alphaNav = new AlphaNav($this, false, false, array('0-9', 'All'));
201         $alphaNav->show();
202
203         $group   = null;
204         $group   = $this->getGroups();
205         $cnt     = 0;
206
207         if (!empty($group)) {
208             $groupList = new SortableGroupList(
209                 $group,
210                 common_current_user(),
211                 $this
212             );
213
214             $cnt = $groupList->show();
215             $group->free();
216
217             if (0 == $cnt) {
218                 $this->showEmptyListMessage();
219             }
220         }
221
222         $args = array();
223         if (isset($this->q)) {
224             $args['q'] = $this->q;
225         } else {
226             $args['filter'] = $this->filter;
227         }
228
229         $this->pagination(
230             $this->page > 1,
231             $cnt > PROFILES_PER_PAGE,
232             $this->page,
233             'groupdirectory',
234             $args
235         );
236
237         $this->elementEnd('div');
238     }
239
240     function showForm($error=null)
241     {
242         $this->elementStart(
243             'form',
244             array(
245                 'method' => 'get',
246                 'id'     => 'form_search',
247                 'class'  => 'form_settings',
248                 'action' => common_local_url('groupdirectory')
249             )
250         );
251
252         $this->elementStart('fieldset');
253
254         // TRANS: Fieldset legend.
255         $this->element('legend', null, _m('Search groups'));
256         $this->elementStart('ul', 'form_data');
257         $this->elementStart('li');
258
259         // TRANS: Field label for input of one or more keywords.
260         $this->input('q', _m('Keyword(s)'), $this->q);
261
262         // TRANS: Button text for searching group directory.
263         $this->submit('search', _m('BUTTON','Search'));
264         $this->elementEnd('li');
265         $this->elementEnd('ul');
266         $this->elementEnd('fieldset');
267         $this->elementEnd('form');
268     }
269
270     /*
271      * Get groups filtered by the current filter, sort key,
272      * sort order, and page
273      */
274     function getGroups()
275     {
276         $group = new User_group();
277
278         // Disable this to get global group searches
279         $group->joinAdd(array('id', 'local_group:group_id'));
280
281         $order = false;
282
283         if (!empty($this->q)) {
284             $wheres = array('nickname', 'fullname', 'homepage', 'description', 'location');
285             foreach ($wheres as $where) {
286                 // Double % because of sprintf
287                 $group->whereAdd(sprintf('LOWER(%1$s.%2$s) LIKE LOWER("%%%3$s%%")',
288                                         $group->escapedTableName(), $where,
289                                         $group->escape($this->q)),
290                                     'OR');
291             }
292
293             $order = sprintf('%1$s.%2$s %3$s',
294                             $group->escapedTableName(),
295                             $this->getSortKey('created'),
296                             $this->reverse ? 'DESC' : 'ASC');
297         } else {
298             // User is browsing via AlphaNav
299
300             switch($this->filter) {
301             case 'all':
302                 // NOOP
303                 break;
304             case '0-9':
305                 $group->whereAdd(sprintf('LEFT(%1$s.%2$s, 1) BETWEEN %3$s AND %4$s',
306                                         $group->escapedTableName(),
307                                         'nickname',
308                                         $group->_quote("0"),
309                                         $group->_quote("9")));
310                 break;
311             default:
312                 $group->whereAdd(sprintf('LEFT(LOWER(%1$s.%2$s), 1) = %3$s',
313                                             $group->escapedTableName(),
314                                             'nickname',
315                                             $group->_quote($this->filter)));
316             }
317
318             $order = sprintf('%1$s.%2$s %3$s, %1$s.%4$s ASC',
319                             $group->escapedTableName(),
320                             $this->getSortKey('nickname'),
321                             $this->reverse ? 'DESC' : 'ASC',
322                             'nickname');
323         }
324
325         $offset = ($this->page-1) * PROFILES_PER_PAGE;
326         $limit  = PROFILES_PER_PAGE + 1;
327
328         $group->orderBy($order);
329         $group->limit($offset, $limit);
330
331         $group->find();
332
333         return $group;
334     }
335
336     /**
337      * Filter the sort parameter
338      *
339      * @return string   a column name for sorting
340      */
341     function getSortKey($def='created')
342     {
343         switch ($this->sort) {
344         case 'nickname':
345         case 'created':
346             return $this->sort;
347         default:
348             return $def;
349         }
350     }
351
352     /**
353      * Show a nice message when there's no search results
354      */
355     function showEmptyListMessage()
356     {
357         if (!empty($this->filter) && ($this->filter != 'all')) {
358             $this->element(
359                 'p',
360                 'error',
361                 sprintf(
362                     // TRANS: Empty list message for searching group directory.
363                     // TRANS: %s is the search string.
364                     _m('No groups starting with %s.'),
365                     $this->filter
366                 )
367             );
368         } else {
369             // TRANS: Empty list message for searching group directory.
370             $this->element('p', 'error', _m('No results.'));
371             // TRANS: Help text for searching group directory.
372             $message = _m("* Make sure all words are spelled correctly.\n".
373                           "* Try different keywords.\n".
374                           "* Try more general keywords.\n".
375                           "* Try fewer keywords.");
376             $this->elementStart('div', 'help instructions');
377             $this->raw(common_markup_to_html($message));
378             $this->elementEnd('div');
379         }
380     }
381
382     function showSections()
383     {
384         $gbp = new GroupsByPostsSection($this);
385         $gbp->show();
386         $gbm = new GroupsByMembersSection($this);
387         $gbm->show();
388     }
389 }