]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigrouplist.php
* L10n updates: consistent puctuation
[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($id);
70         $this->groups = $this->getGroups();
71
72         return true;
73     }
74
75     /**
76      * Handle the request
77      *
78      * Show the user's groups
79      *
80      * @param array $args $_REQUEST data (unused)
81      *
82      * @return void
83      */
84
85     function handle($args)
86     {
87         parent::handle($args);
88
89         if (empty($this->user)) {
90             $this->clientError(_('No such user.'), 404, $this->format);
91             return;
92         }
93
94         $sitename   = common_config('site', 'name');
95         $title      = sprintf(_("%s's groups"), $this->user->nickname);
96         $taguribase = common_config('integration', 'taguri');
97         $id         = "tag:$taguribase:Groups";
98         $link       = common_local_url(
99             'usergroups',
100             array('nickname' => $this->user->nickname)
101         );
102         $subtitle   = sprintf(
103             _("Groups %1$s is a member of on %2$s."),
104             $this->user->nickname,
105             $sitename
106         );
107
108         switch($this->format) {
109         case 'xml':
110             $this->showXmlGroups($this->groups);
111             break;
112         case 'rss':
113             $this->showRssGroups($this->groups, $title, $link, $subtitle);
114             break;
115         case 'atom':
116             $selfuri = common_root_url() . 'api/statusnet/groups/list/' .
117                 $this->user->id . '.atom';
118             $this->showAtomGroups(
119                 $this->groups,
120                 $title,
121                 $id,
122                 $link,
123                 $subtitle,
124                 $selfuri
125             );
126             break;
127         case 'json':
128             $this->showJsonGroups($this->groups);
129             break;
130         default:
131             $this->clientError(
132                 _('API method not found!'),
133                 404,
134                 $this->format
135             );
136             break;
137         }
138
139     }
140
141     /**
142      * Get groups
143      *
144      * @return array groups
145      */
146
147     function getGroups()
148     {
149         $groups = array();
150
151         $group = $this->user->getGroups(
152             ($this->page - 1) * $this->count,
153             $this->count,
154             $this->since_id,
155             $this->max_id,
156             $this->since
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 }