]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigrouplist.php
CamelCase all function names in the API code
[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 $groups   = null;
49
50     /**
51      * Take arguments for running
52      *
53      * @param array $args $_REQUEST args
54      *
55      * @return boolean success flag
56      *
57      */
58
59     function prepare($args)
60     {
61         parent::prepare($args);
62         
63         $this->user   = $this->getTargetUser($id);
64         $this->groups = $this->getGroups();
65
66         return true;
67     }
68
69     /**
70      * Handle the request
71      *
72      * Show the user's groups
73      *
74      * @param array $args $_REQUEST data (unused)
75      *
76      * @return void
77      */
78
79     function handle($args)
80     {
81         parent::handle($args);
82
83         if (empty($this->user)) {
84             $this->clientError(_('No such user!'), 404, $this->format);
85             return;
86         }
87
88         $sitename   = common_config('site', 'name');
89         $title      = sprintf(_("%s's groups"), $this->user->nickname);
90         $taguribase = common_config('integration', 'taguri');
91         $id         = "tag:$taguribase:Groups";
92         $link       = common_local_url(
93             'usergroups',
94             array('nickname' => $this->user->nickname)
95         );
96         $subtitle   = sprintf(
97             _("Groups %s is a member of on %s."),
98             $this->user->nickname,
99             $sitename
100         );
101
102         switch($this->format) {
103         case 'xml':
104             $this->showXmlGroups($this->groups);
105             break;
106         case 'rss':
107             $this->showRssGroups($this->groups, $title, $link, $subtitle);
108             break;
109         case 'atom':
110             $selfuri = common_root_url() . 'api/statusnet/groups/list/' .
111                 $this->user->id . '.atom';
112             $this->showAtomGroups(
113                 $this->groups,
114                 $title,
115                 $id,
116                 $link,
117                 $subtitle,
118                 $selfuri
119             );
120             break;
121         case 'json':
122             $this->showJsonGroups($this->groups);
123             break;
124         default:
125             $this->clientError(
126                 _('API method not found!'),
127                 404,
128                 $this->format
129             );
130             break;
131         }
132
133     }
134
135     /**
136      * Get groups
137      *
138      * @return array groups
139      */
140
141     function getGroups()
142     {
143         $groups = array();
144
145         $group = $this->user->getGroups(
146             ($this->page - 1) * $this->count,
147             $this->count,
148             $this->since_id,
149             $this->max_id,
150             $this->since
151         );
152
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 latest group the user has joined
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, user ID 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                       $this->user->id,
208                       strtotime($this->groups[0]->created),
209                       strtotime($this->groups[$last]->created))
210             )
211             . '"';
212         }
213
214         return null;
215     }
216
217 }