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