]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigrouplistall.php
Merge branch 'testing' into 0.9.x
[quix0rs-gnu-social.git] / actions / apigrouplistall.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show the newest groups
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  API
23  * @package   StatusNet
24  * @author    Craig Andrews <candrews@integralblue.com>
25  * @author    Evan Prodromou <evan@status.net>
26  * @author    Jeffery To <jeffery.to@gmail.com>
27  * @author    Zach Copley <zach@status.net>
28  * @copyright 2009 StatusNet, Inc.
29  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30  * @link      http://status.net/
31  */
32
33 if (!defined('STATUSNET')) {
34     exit(1);
35 }
36
37 require_once INSTALLDIR . '/lib/apiprivateauth.php';
38
39 /**
40  * Returns of the lastest 20 groups for the site
41  *
42  * @category API
43  * @package  StatusNet
44  * @author   Craig Andrews <candrews@integralblue.com>
45  * @author   Evan Prodromou <evan@status.net>
46  * @author   Jeffery To <jeffery.to@gmail.com>
47  * @author   Zach Copley <zach@status.net>
48  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49  * @link     http://status.net/
50  */
51
52 class ApiGroupListAllAction extends ApiPrivateAuthAction
53 {
54     var $groups   = null;
55
56     /**
57      * Take arguments for running
58      *
59      * @param array $args $_REQUEST args
60      *
61      * @return boolean success flag
62      *
63      */
64
65     function prepare($args)
66     {
67         parent::prepare($args);
68
69         $this->user   = $this->getTargetUser(null);
70         $this->groups = $this->getGroups();
71
72         return true;
73     }
74
75     /**
76      * Handle the request
77      *
78      * Show the user's groups
79      *
80      * @param array $args $_REQUEST data (unused)
81      *
82      * @return void
83      */
84
85     function handle($args)
86     {
87         parent::handle($args);
88
89         $sitename   = common_config('site', 'name');
90         // TRANS: Message is used as a title. %s is a site name.
91         $title      = sprintf(_("%s groups"), $sitename);
92         $taguribase = TagURI::base();
93         $id         = "tag:$taguribase:Groups";
94         $link       = common_local_url('groups');
95         $subtitle   = sprintf(_("groups on %s"), $sitename);
96
97         switch($this->format) {
98         case 'xml':
99             $this->showXmlGroups($this->groups);
100             break;
101         case 'rss':
102             $this->showRssGroups($this->groups, $title, $link, $subtitle);
103             break;
104         case 'atom':
105             $selfuri = common_root_url() .
106                 'api/statusnet/groups/list_all.atom';
107             $this->showAtomGroups(
108                 $this->groups,
109                 $title,
110                 $id,
111                 $link,
112                 $subtitle,
113                 $selfuri
114             );
115             break;
116         case 'json':
117             $this->showJsonGroups($this->groups);
118             break;
119         default:
120             $this->clientError(
121                 _('API method not found.'),
122                 404,
123                 $this->format
124             );
125             break;
126         }
127
128     }
129
130     /**
131      * Get groups
132      *
133      * @return array groups
134      */
135
136     function getGroups()
137     {
138         $qry = 'SELECT user_group.* '.
139           'from user_group join local_group on user_group.id = local_group.group_id '.
140           'order by created desc ';
141         $offset = intval($this->page - 1) * intval($this->count);
142         $limit = intval($this->count);
143         if (common_config('db', 'type') == 'pgsql') {
144             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
145         } else {
146             $qry .= ' LIMIT ' . $offset . ', ' . $limit;
147         }
148         $group = new User_group();
149
150         $group->query($qry);
151
152         $groups = array();
153         while ($group->fetch()) {
154             $groups[] = clone($group);
155         }
156
157         return $groups;
158     }
159
160     /**
161      * Is this action read only?
162      *
163      * @param array $args other arguments
164      *
165      * @return boolean true
166      */
167
168     function isReadOnly($args)
169     {
170         return true;
171     }
172
173     /**
174      * When was this feed last modified?
175      *
176      * @return string datestamp of the site's latest group
177      */
178
179     function lastModified()
180     {
181         if (!empty($this->groups) && (count($this->groups) > 0)) {
182             return strtotime($this->groups[0]->created);
183         }
184
185         return null;
186     }
187
188     /**
189      * An entity tag for this list of groups
190      *
191      * Returns an Etag based on the action name, language, and
192      * timestamps of the first and last group the user has joined
193      *
194      * @return string etag
195      */
196
197     function etag()
198     {
199         if (!empty($this->groups) && (count($this->groups) > 0)) {
200
201             $last = count($this->groups) - 1;
202
203             return '"' . implode(
204                 ':',
205                 array($this->arg('action'),
206                       common_language(),
207                       strtotime($this->groups[0]->created),
208                       strtotime($this->groups[$last]->created))
209             )
210             . '"';
211         }
212
213         return null;
214     }
215
216 }