]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigrouplist.php
Remove more redundant $formats
[quix0rs-gnu-social.git] / actions / apigrouplist.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Check to see whether a user a member of a group
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/apibareauth.php';
35
36 /**
37  * Returns whether a user is a member of a specified group.
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 ApiGroupListAction extends ApiBareAuthAction
47 {
48     var $user     = 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->groups = $this->getGroups();
77
78         return true;
79     }
80
81     /**
82      * Handle the request
83      *
84      * Show the user's groups
85      *
86      * @param array $args $_REQUEST data (unused)
87      *
88      * @return void
89      */
90
91     function handle($args)
92     {
93         parent::handle($args);
94
95         if (empty($this->user)) {
96             $this->clientError(_('No such user!'), 404, $this->format);
97             return;
98         }
99
100         $sitename   = common_config('site', 'name');
101         $title      = sprintf(_("%s's groups"), $this->user->nickname);
102         $taguribase = common_config('integration', 'taguri');
103         $id         = "tag:$taguribase:Groups";
104         $link       = common_local_url(
105             'usergroups',
106             array('nickname' => $this->user->nickname)
107         );
108         $subtitle   = sprintf(
109             _("Groups %s is a member of on %s."),
110             $this->user->nickname,
111             $sitename
112         );
113
114         switch($this->format) {
115         case 'xml':
116             $this->show_xml_groups($this->groups);
117             break;
118         case 'rss':
119             $this->show_rss_groups($this->groups, $title, $link, $subtitle);
120             break;
121         case 'atom':
122             $selfuri = common_root_url() . 'api/statusnet/groups/list/' .
123                 $this->user->id . '.atom';
124             $this->show_atom_groups(
125                 $this->groups,
126                 $title,
127                 $id,
128                 $link,
129                 $subtitle,
130                 $selfuri
131             );
132             break;
133         case 'json':
134             $this->show_json_groups($this->groups);
135             break;
136         default:
137             $this->clientError(
138                 _('API method not found!'),
139                 404,
140                 $this->format
141             );
142             break;
143         }
144
145     }
146
147     /**
148      * Get groups
149      *
150      * @return array groups
151      */
152
153     function getGroups()
154     {
155         $groups = array();
156
157         $group = $this->user->getGroups(
158             ($this->page - 1) * $this->count,
159             $this->count,
160             $this->since_id,
161             $this->max_id,
162             $this->since
163         );
164
165         while ($group->fetch()) {
166             $groups[] = clone($group);
167         }
168
169         return $groups;
170     }
171
172     /**
173      * Is this action read only?
174      *
175      * @param array $args other arguments
176      *
177      * @return boolean true
178      */
179
180     function isReadOnly($args)
181     {
182         return true;
183     }
184
185     /**
186      * When was this feed last modified?
187      *
188      * @return string datestamp of the latest group the user has joined
189      */
190
191     function lastModified()
192     {
193         if (!empty($this->groups) && (count($this->groups) > 0)) {
194             return strtotime($this->groups[0]->created);
195         }
196
197         return null;
198     }
199
200     /**
201      * An entity tag for this list of groups
202      *
203      * Returns an Etag based on the action name, language, user ID and
204      * timestamps of the first and last group the user has joined
205      *
206      * @return string etag
207      */
208
209     function etag()
210     {
211         if (!empty($this->groups) && (count($this->groups) > 0)) {
212
213             $last = count($this->groups) - 1;
214
215             return '"' . implode(
216                 ':',
217                 array($this->arg('action'),
218                       common_language(),
219                       $this->user->id,
220                       strtotime($this->groups[0]->created),
221                       strtotime($this->groups[$last]->created))
222             )
223             . '"';
224         }
225
226         return null;
227     }
228
229 }