]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigrouplist.php
Merge branch '1.0.x' of gitorious.org:statusnet/mainline into 1.0.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 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     function prepare($args)
64     {
65         parent::prepare($args);
66
67         $this->user   = $this->getTargetUser(null);
68
69         if (empty($this->user)) {
70             // TRANS: Client error displayed when user not found for an action.
71             $this->clientError(_('No such user.'), 404, $this->format);
72             return false;
73         }
74
75         $this->groups = $this->getGroups();
76
77         return true;
78     }
79
80     /**
81      * Handle the request
82      *
83      * Show the user's groups
84      *
85      * @param array $args $_REQUEST data (unused)
86      *
87      * @return void
88      */
89     function handle($args)
90     {
91         parent::handle($args);
92
93         $sitename   = common_config('site', 'name');
94         // TRANS: Used as title in check for group membership. %s is a user name.
95         $title      = sprintf(_("%s's groups"), $this->user->nickname);
96         $taguribase = TagURI::base();
97         $id         = "tag:$taguribase:Groups";
98         $link       = common_local_url(
99             'usergroups',
100             array('nickname' => $this->user->nickname)
101         );
102
103         $subtitle   = sprintf(
104             // TRANS: Used as subtitle in check for group membership. %1$s is the site name, %2$s is a user name.
105             _('%1$s groups %2$s is a member of.'),
106             $sitename,
107             $this->user->nickname
108         );
109
110         switch($this->format) {
111         case 'xml':
112             $this->showXmlGroups($this->groups);
113             break;
114         case 'rss':
115             $this->showRssGroups($this->groups, $title, $link, $subtitle);
116             break;
117         case 'atom':
118             $selfuri = common_root_url() . 'api/statusnet/groups/list/' .
119                 $this->user->id . '.atom';
120             $this->showAtomGroups(
121                 $this->groups,
122                 $title,
123                 $id,
124                 $link,
125                 $subtitle,
126                 $selfuri
127             );
128             break;
129         case 'json':
130             $this->showJsonGroups($this->groups);
131             break;
132         default:
133             $this->clientError(
134                 // TRANS: Client error displayed when coming across a non-supported API method.
135                 _('API method not found.'),
136                 404,
137                 $this->format
138             );
139             break;
140         }
141     }
142
143     /**
144      * Get groups
145      *
146      * @return array groups
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     function isReadOnly($args)
174     {
175         return true;
176     }
177
178     /**
179      * When was this feed last modified?
180      *
181      * @return string datestamp of the latest group the user has joined
182      */
183
184     function lastModified()
185     {
186         if (!empty($this->groups) && (count($this->groups) > 0)) {
187             return strtotime($this->groups[0]->created);
188         }
189
190         return null;
191     }
192
193     /**
194      * An entity tag for this list of groups
195      *
196      * Returns an Etag based on the action name, language, user ID and
197      * timestamps of the first and last group the user has joined
198      *
199      * @return string etag
200      */
201     function etag()
202     {
203         if (!empty($this->groups) && (count($this->groups) > 0)) {
204
205             $last = count($this->groups) - 1;
206
207             return '"' . implode(
208                 ':',
209                 array($this->arg('action'),
210                       common_user_cache_hash($this->auth_user),
211                       common_language(),
212                       $this->user->id,
213                       strtotime($this->groups[0]->created),
214                       strtotime($this->groups[$last]->created))
215             )
216             . '"';
217         }
218
219         return null;
220     }
221 }