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