]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigrouplist.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into 0.9.x
[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    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/apibareauth.php';
38
39 /**
40  * Returns whether a user is a member of a specified group.
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 ApiGroupListAction extends ApiBareAuthAction
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
71         if (empty($this->user)) {
72             $this->clientError(_('No such user.'), 404, $this->format);
73             return false;
74         }
75
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         $sitename   = common_config('site', 'name');
96         $title      = sprintf(_("%s's groups"), $this->user->nickname);
97         $taguribase = TagURI::base();
98         $id         = "tag:$taguribase:Groups";
99         $link       = common_local_url(
100             'usergroups',
101             array('nickname' => $this->user->nickname)
102         );
103         $subtitle   = sprintf(
104             _("Groups %1\$s is a member of on %2\$s."),
105             $this->user->nickname,
106             $sitename
107         );
108
109         switch($this->format) {
110         case 'xml':
111             $this->showXmlGroups($this->groups);
112             break;
113         case 'rss':
114             $this->showRssGroups($this->groups, $title, $link, $subtitle);
115             break;
116         case 'atom':
117             $selfuri = common_root_url() . 'api/statusnet/groups/list/' .
118                 $this->user->id . '.atom';
119             $this->showAtomGroups(
120                 $this->groups,
121                 $title,
122                 $id,
123                 $link,
124                 $subtitle,
125                 $selfuri
126             );
127             break;
128         case 'json':
129             $this->showJsonGroups($this->groups);
130             break;
131         default:
132             $this->clientError(
133                 _('API method not found.'),
134                 404,
135                 $this->format
136             );
137             break;
138         }
139
140     }
141
142     /**
143      * Get groups
144      *
145      * @return array groups
146      */
147
148     function getGroups()
149     {
150         $groups = array();
151
152         $group = $this->user->getGroups(
153             ($this->page - 1) * $this->count,
154             $this->count,
155             $this->since_id,
156             $this->max_id
157         );
158
159         while ($group->fetch()) {
160             $groups[] = clone($group);
161         }
162
163         return $groups;
164     }
165
166     /**
167      * Is this action read only?
168      *
169      * @param array $args other arguments
170      *
171      * @return boolean true
172      */
173
174     function isReadOnly($args)
175     {
176         return true;
177     }
178
179     /**
180      * When was this feed last modified?
181      *
182      * @return string datestamp of the latest group the user has joined
183      */
184
185     function lastModified()
186     {
187         if (!empty($this->groups) && (count($this->groups) > 0)) {
188             return strtotime($this->groups[0]->created);
189         }
190
191         return null;
192     }
193
194     /**
195      * An entity tag for this list of groups
196      *
197      * Returns an Etag based on the action name, language, user ID and
198      * timestamps of the first and last group the user has joined
199      *
200      * @return string etag
201      */
202
203     function etag()
204     {
205         if (!empty($this->groups) && (count($this->groups) > 0)) {
206
207             $last = count($this->groups) - 1;
208
209             return '"' . implode(
210                 ':',
211                 array($this->arg('action'),
212                       common_language(),
213                       $this->user->id,
214                       strtotime($this->groups[0]->created),
215                       strtotime($this->groups[$last]->created))
216             )
217             . '"';
218         }
219
220         return null;
221     }
222
223 }