]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigrouplist.php
Merge remote branch 'gitorious/0.9.x' 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  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
30  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
31  * @link      http://status.net/
32  */
33
34 if (!defined('STATUSNET')) {
35     exit(1);
36 }
37
38 require_once INSTALLDIR . '/lib/apibareauth.php';
39
40 /**
41  * Returns whether a user is a member of a specified group.
42  *
43  * @category API
44  * @package  StatusNet
45  * @author   Craig Andrews <candrews@integralblue.com>
46  * @author   Evan Prodromou <evan@status.net>
47  * @author   Jeffery To <jeffery.to@gmail.com>
48  * @author   Zach Copley <zach@status.net>
49  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50  * @link     http://status.net/
51  */
52
53 class ApiGroupListAction extends ApiBareAuthAction
54 {
55     var $groups   = null;
56
57     /**
58      * Take arguments for running
59      *
60      * @param array $args $_REQUEST args
61      *
62      * @return boolean success flag
63      *
64      */
65
66     function prepare($args)
67     {
68         parent::prepare($args);
69
70         $this->user   = $this->getTargetUser(null);
71
72         if (empty($this->user)) {
73             $this->clientError(_('No such user.'), 404, $this->format);
74             return false;
75         }
76
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         // TRANS: %s is a user name
98         $title      = sprintf(_("%s's groups"), $this->user->nickname);
99         $taguribase = TagURI::base();
100         $id         = "tag:$taguribase:Groups";
101         $link       = common_local_url(
102             'usergroups',
103             array('nickname' => $this->user->nickname)
104         );
105
106         $subtitle   = sprintf(
107             // TRANS: Meant to convey the user %2$s is a member of each of the groups listed on site %1$s
108             _("%1\$s groups %2\$s is a member of."),
109             $sitename,
110             $this->user->nickname
111         );
112
113         switch($this->format) {
114         case 'xml':
115             $this->showXmlGroups($this->groups);
116             break;
117         case 'rss':
118             $this->showRssGroups($this->groups, $title, $link, $subtitle);
119             break;
120         case 'atom':
121             $selfuri = common_root_url() . 'api/statusnet/groups/list/' .
122                 $this->user->id . '.atom';
123             $this->showAtomGroups(
124                 $this->groups,
125                 $title,
126                 $id,
127                 $link,
128                 $subtitle,
129                 $selfuri
130             );
131             break;
132         case 'json':
133             $this->showJsonGroups($this->groups);
134             break;
135         default:
136             $this->clientError(
137                 _('API method not found.'),
138                 404,
139                 $this->format
140             );
141             break;
142         }
143
144     }
145
146     /**
147      * Get groups
148      *
149      * @return array groups
150      */
151
152     function getGroups()
153     {
154         $groups = array();
155
156         $group = $this->user->getGroups(
157             ($this->page - 1) * $this->count,
158             $this->count,
159             $this->since_id,
160             $this->max_id
161         );
162
163         while ($group->fetch()) {
164             $groups[] = clone($group);
165         }
166
167         return $groups;
168     }
169
170     /**
171      * Is this action read only?
172      *
173      * @param array $args other arguments
174      *
175      * @return boolean true
176      */
177
178     function isReadOnly($args)
179     {
180         return true;
181     }
182
183     /**
184      * When was this feed last modified?
185      *
186      * @return string datestamp of the latest group the user has joined
187      */
188
189     function lastModified()
190     {
191         if (!empty($this->groups) && (count($this->groups) > 0)) {
192             return strtotime($this->groups[0]->created);
193         }
194
195         return null;
196     }
197
198     /**
199      * An entity tag for this list of groups
200      *
201      * Returns an Etag based on the action name, language, user ID and
202      * timestamps of the first and last group the user has joined
203      *
204      * @return string etag
205      */
206
207     function etag()
208     {
209         if (!empty($this->groups) && (count($this->groups) > 0)) {
210
211             $last = count($this->groups) - 1;
212
213             return '"' . implode(
214                 ':',
215                 array($this->arg('action'),
216                       common_user_cache_hash($this->auth_user),
217                       common_language(),
218                       $this->user->id,
219                       strtotime($this->groups[0]->created),
220                       strtotime($this->groups[$last]->created))
221             )
222             . '"';
223         }
224
225         return null;
226     }
227
228 }