]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/timelist.php
Work improving the interface of the Event micro-app
[quix0rs-gnu-social.git] / plugins / Event / timelist.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @category  Event
20  * @package   StatusNet
21  * @author    Zach Copley <zach@status.net>
22  * @copyright 2011 StatusNet, Inc.
23  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
24  * @link      http://status.net/
25  */
26
27 if (!defined('STATUSNET')) {
28     exit(1);
29 }
30
31 /**
32  * Callback handler to populate end time dropdown
33  */
34 class TimelistAction extends Action {
35
36     private $start;
37     private $duration;
38
39     /**
40      * Get ready
41      *
42      * @param array $args misc. arguments
43      *
44      * @return boolean true
45      */
46     function prepare($args) {
47         parent::prepare($args);
48         $this->start = $this->arg('start');
49         $this->duration = $this->boolean('duration', false);
50         return true;
51     }
52
53     /**
54      * Handle input and ouput something
55      *
56      * @param array $args $_REQUEST arguments
57      *
58      * @return void
59      */
60     function handle($args)
61     {
62         parent::handle($args);
63
64         if (!common_logged_in()) {
65             // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
66             $this->clientError(_('Not logged in.'));
67             return;
68         }
69
70         if (!empty($this->start)) {
71             $times = EventTimeList::getTimes($this->start, $this->duration);
72         } else {
73             $this->clientError(_m('Unexpected form submission.'));
74             return;
75         }
76
77         if ($this->boolean('ajax')) {
78             header('Content-Type: application/json; charset=utf-8');
79             print json_encode($times);
80         } else {
81             $this->clientError(_m('This action is AJAX only.'));
82         }
83     }
84
85     /**
86      * Override the regular error handler to show something more
87      * ajaxy
88      *
89      * @param string $msg   error message
90      * @param int    $code  error code
91      */
92     function clientError($msg, $code = 400) {
93         if ($this->boolean('ajax')) {
94             header('Content-Type: application/json; charset=utf-8');
95             print json_encode(
96                 array(
97                     'success' => false,
98                     'code'    => $code,
99                     'message' => $msg
100                 )
101             );
102         } else {
103             parent::clientError($msg, $code);
104         }
105     }
106 }