]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigrouplistall.php
Remove more redundant $formats
[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    Zach Copley <zach@status.net>
25  * @copyright 2009 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     exit(1);
32 }
33
34 require_once INSTALLDIR . '/lib/api.php';
35
36 /**
37  * Returns of the lastest 20 groups for the site
38  *
39  * @category API
40  * @package  StatusNet
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45
46 class ApiGroupListAllAction extends TwitterApiAction
47 {
48     var $page     = null;
49     var $count    = null;
50     var $max_id   = null;
51     var $since_id = null;
52     var $since    = null;
53     var $groups   = null;
54
55     /**
56      * Take arguments for running
57      *
58      * @param array $args $_REQUEST args
59      *
60      * @return boolean success flag
61      *
62      */
63
64     function prepare($args)
65     {
66         parent::prepare($args);
67
68         $this->page     = (int)$this->arg('page', 1);
69         $this->count    = (int)$this->arg('count', 20);
70         $this->max_id   = (int)$this->arg('max_id', 0);
71         $this->since_id = (int)$this->arg('since_id', 0);
72         $this->since    = $this->arg('since');
73
74         $this->user   = $this->getTargetUser($id);
75         $this->groups = $this->getGroups();
76
77         return true;
78     }
79
80     /**
81      * Handle the request
82      *
83      * Show the user's groups
84      *
85      * @param array $args $_REQUEST data (unused)
86      *
87      * @return void
88      */
89
90     function handle($args)
91     {
92         parent::handle($args);
93
94         $sitename   = common_config('site', 'name');
95         $title      = sprintf(_("%s groups"), $sitename);
96         $taguribase = common_config('integration', 'taguri');
97         $id         = "tag:$taguribase:Groups";
98         $link       = common_local_url('groups');
99         $subtitle   = sprintf(_("groups on %s"), $sitename);
100
101         switch($this->format) {
102         case 'xml':
103             $this->show_xml_groups($this->groups);
104             break;
105         case 'rss':
106             $this->show_rss_groups($this->groups, $title, $link, $subtitle);
107             break;
108         case 'atom':
109             $selfuri = common_root_url() .
110                 'api/statusnet/groups/list_all.atom';
111             $this->show_atom_groups(
112                 $this->groups,
113                 $title,
114                 $id,
115                 $link,
116                 $subtitle,
117                 $selfuri
118             );
119             break;
120         case 'json':
121             $this->show_json_groups($this->groups);
122             break;
123         default:
124             $this->clientError(
125                 _('API method not found!'),
126                 404,
127                 $this->format
128             );
129             break;
130         }
131
132     }
133
134     /**
135      * Get groups
136      *
137      * @return array groups
138      */
139
140     function getGroups()
141     {
142         $groups = array();
143
144         // XXX: Use the $page, $count, $max_id, $since_id, and $since parameters
145
146         $group = new User_group();
147         $group->orderBy('created DESC');
148         $group->find();
149
150         while ($group->fetch()) {
151             $groups[] = clone($group);
152         }
153
154         return $groups;
155     }
156
157     /**
158      * Is this action read only?
159      *
160      * @param array $args other arguments
161      *
162      * @return boolean true
163      */
164
165     function isReadOnly($args)
166     {
167         return true;
168     }
169
170     /**
171      * When was this feed last modified?
172      *
173      * @return string datestamp of the site's latest group
174      */
175
176     function lastModified()
177     {
178         if (!empty($this->groups) && (count($this->groups) > 0)) {
179             return strtotime($this->groups[0]->created);
180         }
181
182         return null;
183     }
184
185     /**
186      * An entity tag for this list of groups
187      *
188      * Returns an Etag based on the action name, language, and
189      * timestamps of the first and last group the user has joined
190      *
191      * @return string etag
192      */
193
194     function etag()
195     {
196         if (!empty($this->groups) && (count($this->groups) > 0)) {
197
198             $last = count($this->groups) - 1;
199
200             return '"' . implode(
201                 ':',
202                 array($this->arg('action'),
203                       common_language(),
204                       strtotime($this->groups[0]->created),
205                       strtotime($this->groups[$last]->created))
206             )
207             . '"';
208         }
209
210         return null;
211     }
212
213 }