]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigrouplistall.php
Merge branch 'testing' of gitorious.org:statusnet/mainline into 0.9.x
[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    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/apiprivateauth.php';
39
40 /**
41  * Returns of the lastest 20 groups for the site
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 ApiGroupListAllAction extends ApiPrivateAuthAction
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         $this->groups = $this->getGroups();
72
73         return true;
74     }
75
76     /**
77      * Handle the request
78      *
79      * Show the user's groups
80      *
81      * @param array $args $_REQUEST data (unused)
82      *
83      * @return void
84      */
85
86     function handle($args)
87     {
88         parent::handle($args);
89
90         $sitename   = common_config('site', 'name');
91         // TRANS: Message is used as a title. %s is a site name.
92         $title      = sprintf(_("%s groups"), $sitename);
93         $taguribase = TagURI::base();
94         $id         = "tag:$taguribase:Groups";
95         $link       = common_local_url('groups');
96         $subtitle   = sprintf(_("groups on %s"), $sitename);
97
98         switch($this->format) {
99         case 'xml':
100             $this->showXmlGroups($this->groups);
101             break;
102         case 'rss':
103             $this->showRssGroups($this->groups, $title, $link, $subtitle);
104             break;
105         case 'atom':
106             $selfuri = common_root_url() .
107                 'api/statusnet/groups/list_all.atom';
108             $this->showAtomGroups(
109                 $this->groups,
110                 $title,
111                 $id,
112                 $link,
113                 $subtitle,
114                 $selfuri
115             );
116             break;
117         case 'json':
118             $this->showJsonGroups($this->groups);
119             break;
120         default:
121             $this->clientError(
122                 _('API method not found.'),
123                 404,
124                 $this->format
125             );
126             break;
127         }
128
129     }
130
131     /**
132      * Get groups
133      *
134      * @return array groups
135      */
136
137     function getGroups()
138     {
139         $qry = 'SELECT user_group.* '.
140           'from user_group join local_group on user_group.id = local_group.group_id '.
141           'order by created desc ';
142         $offset = intval($this->page - 1) * intval($this->count);
143         $limit = intval($this->count);
144         if (common_config('db', 'type') == 'pgsql') {
145             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
146         } else {
147             $qry .= ' LIMIT ' . $offset . ', ' . $limit;
148         }
149         $group = new User_group();
150
151         $group->query($qry);
152
153         $groups = array();
154         while ($group->fetch()) {
155             $groups[] = clone($group);
156         }
157
158         return $groups;
159     }
160
161     /**
162      * Is this action read only?
163      *
164      * @param array $args other arguments
165      *
166      * @return boolean true
167      */
168
169     function isReadOnly($args)
170     {
171         return true;
172     }
173
174     /**
175      * When was this feed last modified?
176      *
177      * @return string datestamp of the site's latest group
178      */
179
180     function lastModified()
181     {
182         if (!empty($this->groups) && (count($this->groups) > 0)) {
183             return strtotime($this->groups[0]->created);
184         }
185
186         return null;
187     }
188
189     /**
190      * An entity tag for this list of groups
191      *
192      * Returns an Etag based on the action name, language, and
193      * timestamps of the first and last group the user has joined
194      *
195      * @return string etag
196      */
197
198     function etag()
199     {
200         if (!empty($this->groups) && (count($this->groups) > 0)) {
201
202             $last = count($this->groups) - 1;
203
204             return '"' . implode(
205                 ':',
206                 array($this->arg('action'),
207                       common_language(),
208                       strtotime($this->groups[0]->created),
209                       strtotime($this->groups[$last]->created))
210             )
211             . '"';
212         }
213
214         return null;
215     }
216
217 }