]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigrouplistall.php
New individual actions for dealing with groups via API
[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/twitterapi.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 $format   = null;
49     var $page     = null;
50     var $count    = null;
51     var $max_id   = null;
52     var $since_id = null;
53     var $since    = null;
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->page     = (int)$this->arg('page', 1);
70         $this->count    = (int)$this->arg('count', 20);
71         $this->max_id   = (int)$this->arg('max_id', 0);
72         $this->since_id = (int)$this->arg('since_id', 0);
73         $this->since    = $this->arg('since');
74
75         $this->user   = $this->getTargetUser($id);
76         $this->format = $this->arg('format');
77         $this->groups = $this->getGroups();
78
79         return true;
80     }
81
82     /**
83      * Handle the request
84      *
85      * Show the user's groups
86      *
87      * @param array $args $_REQUEST data (unused)
88      *
89      * @return void
90      */
91
92     function handle($args)
93     {
94         parent::handle($args);
95
96         $sitename   = common_config('site', 'name');
97         $title      = sprintf(_("%s groups"), $sitename);
98         $taguribase = common_config('integration', 'taguri');
99         $id         = "tag:$taguribase:Groups";
100         $link       = common_local_url('groups');
101         $subtitle   = sprintf(_("groups on %s"), $sitename);
102
103         switch($this->format) {
104         case 'xml':
105             $this->show_xml_groups($this->groups);
106             break;
107         case 'rss':
108             $this->show_rss_groups($this->groups, $title, $link, $subtitle);
109             break;
110         case 'atom':
111             $selfuri = common_root_url() .
112                 'api/statusnet/groups/list_all.atom';
113             $this->show_atom_groups(
114                 $this->groups,
115                 $title,
116                 $id,
117                 $link,
118                 $subtitle,
119                 $selfuri
120             );
121             break;
122         case 'json':
123             $this->show_json_groups($this->groups);
124             break;
125         default:
126             $this->clientError(
127                 _('API method not found!'),
128                 404,
129                 $this->format
130             );
131             break;
132         }
133
134     }
135
136     /**
137      * Get groups
138      *
139      * @return array groups
140      */
141
142     function getGroups()
143     {
144         $groups = array();
145
146         // XXX: Use the $page, $count, $max_id, $since_id, and $since parameters
147
148         $group = new User_group();
149         $group->orderBy('created DESC');
150         $group->find();
151
152         while ($group->fetch()) {
153             $groups[] = clone($group);
154         }
155
156         return $groups;
157     }
158
159     /**
160      * Is this action read only?
161      *
162      * @param array $args other arguments
163      *
164      * @return boolean true
165      */
166
167     function isReadOnly($args)
168     {
169         return true;
170     }
171
172     /**
173      * When was this feed last modified?
174      *
175      * @return string datestamp of the site's latest group
176      */
177
178     function lastModified()
179     {
180         if (!empty($this->groups) && (count($this->groups) > 0)) {
181             return strtotime($this->groups[0]->created);
182         }
183
184         return null;
185     }
186
187     /**
188      * An entity tag for this list of groups
189      *
190      * Returns an Etag based on the action name, language, and
191      * timestamps of the first and last group the user has joined
192      *
193      * @return string etag
194      */
195
196     function etag()
197     {
198         if (!empty($this->groups) && (count($this->groups) > 0)) {
199
200             $last = count($this->groups) - 1;
201
202             return '"' . implode(
203                 ':',
204                 array($this->arg('action'),
205                       common_language(),
206                       strtotime($this->groups[0]->created),
207                       strtotime($this->groups[$last]->created))
208             )
209             . '"';
210         }
211
212         return null;
213     }
214
215 }