]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigrouplistall.php
Moved group create API into its own action
[quix0rs-gnu-social.git] / actions / apigrouplistall.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show the newest groups
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    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/lib/api.php';
35
36 /**
37  * Returns of the lastest 20 groups for the site
38  *
39  * @category API
40  * @package  StatusNet
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45
46 class ApiGroupListAllAction extends ApiAction
47 {
48     var $groups   = null;
49
50     /**
51      * Take arguments for running
52      *
53      * @param array $args $_REQUEST args
54      *
55      * @return boolean success flag
56      *
57      */
58
59     function prepare($args)
60     {
61         parent::prepare($args);
62
63         $this->user   = $this->getTargetUser($id);
64         $this->groups = $this->getGroups();
65
66         return true;
67     }
68
69     /**
70      * Handle the request
71      *
72      * Show the user's groups
73      *
74      * @param array $args $_REQUEST data (unused)
75      *
76      * @return void
77      */
78
79     function handle($args)
80     {
81         parent::handle($args);
82
83         $sitename   = common_config('site', 'name');
84         $title      = sprintf(_("%s groups"), $sitename);
85         $taguribase = common_config('integration', 'taguri');
86         $id         = "tag:$taguribase:Groups";
87         $link       = common_local_url('groups');
88         $subtitle   = sprintf(_("groups on %s"), $sitename);
89
90         switch($this->format) {
91         case 'xml':
92             $this->showXmlGroups($this->groups);
93             break;
94         case 'rss':
95             $this->showRssGroups($this->groups, $title, $link, $subtitle);
96             break;
97         case 'atom':
98             $selfuri = common_root_url() .
99                 'api/statusnet/groups/list_all.atom';
100             $this->showAtomGroups(
101                 $this->groups,
102                 $title,
103                 $id,
104                 $link,
105                 $subtitle,
106                 $selfuri
107             );
108             break;
109         case 'json':
110             $this->showJsonGroups($this->groups);
111             break;
112         default:
113             $this->clientError(
114                 _('API method not found!'),
115                 404,
116                 $this->format
117             );
118             break;
119         }
120
121     }
122
123     /**
124      * Get groups
125      *
126      * @return array groups
127      */
128
129     function getGroups()
130     {
131         $groups = array();
132
133         // XXX: Use the $page, $count, $max_id, $since_id, and $since parameters
134
135         $group = new User_group();
136         $group->orderBy('created DESC');
137         $group->find();
138
139         while ($group->fetch()) {
140             $groups[] = clone($group);
141         }
142
143         return $groups;
144     }
145
146     /**
147      * Is this action read only?
148      *
149      * @param array $args other arguments
150      *
151      * @return boolean true
152      */
153
154     function isReadOnly($args)
155     {
156         return true;
157     }
158
159     /**
160      * When was this feed last modified?
161      *
162      * @return string datestamp of the site's latest group
163      */
164
165     function lastModified()
166     {
167         if (!empty($this->groups) && (count($this->groups) > 0)) {
168             return strtotime($this->groups[0]->created);
169         }
170
171         return null;
172     }
173
174     /**
175      * An entity tag for this list of groups
176      *
177      * Returns an Etag based on the action name, language, and
178      * timestamps of the first and last group the user has joined
179      *
180      * @return string etag
181      */
182
183     function etag()
184     {
185         if (!empty($this->groups) && (count($this->groups) > 0)) {
186
187             $last = count($this->groups) - 1;
188
189             return '"' . implode(
190                 ':',
191                 array($this->arg('action'),
192                       common_language(),
193                       strtotime($this->groups[0]->created),
194                       strtotime($this->groups[$last]->created))
195             )
196             . '"';
197         }
198
199         return null;
200     }
201
202 }