]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/actions/newevent.php
OStatus and XMPP plugins now inform Nodeinfo plugins about their activity
[quix0rs-gnu-social.git] / plugins / Event / actions / newevent.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Add a new event
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Event
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * Add a new event
35  *
36  * @category  Event
37  * @package   StatusNet
38  * @author    Evan Prodromou <evan@status.net>
39  * @copyright 2011 StatusNet, Inc.
40  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
41  * @link      http://status.net/
42  */
43 class NeweventAction extends FormAction
44 {
45     protected $form = 'Event';
46
47     /**
48      * Returns the title of the action
49      *
50      * @return string Action title
51      */
52     function title()
53     {
54         // TRANS: Title for new event form.
55         return _m('TITLE','New event');
56     }
57
58     protected function doPost()
59     {
60         // HUMAN TEXT DATA
61         $title = $this->trimmed('title');
62         if (empty($title)) {
63             // TRANS: Client exception thrown when trying to post an event without providing a title.
64             throw new ClientException(_m('Event must have a title.'));
65         }
66         $description = $this->trimmed('description');
67
68
69         // TIME PARSING
70         $tz          = $this->trimmed('tz');
71
72         $startDate = $this->trimmed('startdate');
73         if (empty($startDate)) {
74             // TRANS: Client exception thrown when trying to post an event without providing a start date.
75             throw new ClientException(_m('Start date required.'));
76         }
77         $startTime = $this->trimmed('event-starttime');
78         if (empty($startTime)) {
79             // TRANS: Client exception thrown when trying to post an event without providing a start time.
80             throw new ClientException(_m('Event must have a start time.'));
81         }
82         $start_str = sprintf('%s %s %s', $startDate, $startTime, $tz);
83         $start = strtotime($start_str);
84         if ($start === false) {
85             // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
86             // TRANS: %s is the data that could not be processed.
87             throw new ClientException(sprintf(_m('Could not parse date %s.'), _ve($start_str)));
88         }
89
90         $endDate = $this->trimmed('enddate');
91         if (empty($endDate)) {
92             // TRANS: Client exception thrown when trying to post an event without providing an end date.
93             throw new ClientException(_m('End date required.'));
94         }
95         $endTime = $this->trimmed('event-endtime');
96         if (empty($endTime)) {
97             // TRANS: Client exception thrown when trying to post an event without providing an end time.
98             throw new ClientException(_m('Event must have an end time.'));
99         }
100         $end_str   = sprintf('%s %s %s', $endDate, $endTime, $tz);
101         $end = strtotime($end_str);
102         if ($end === false) {
103             // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
104             // TRANS: %s is the data that could not be processed.
105             throw new ClientException(sprintf(_m('Could not parse date %s.'), _ve($end_str)));
106         }
107
108         $url         = $this->trimmed('url');
109         if (!empty($url) && !common_valid_http_url($url)) {
110             // TRANS: Client exception thrown when trying to post an event with an invalid (non-empty) URL.
111             throw new ClientException(_m('An event URL must be a valid HTTP/HTTPS link.'));
112         }
113
114
115         // LOCATION DATA
116         $location    = $this->trimmed('location');
117
118
119
120         $options = [ 'source' => 'web' ];
121
122         $act = new Activity();
123         $act->verb = ActivityVerb::POST;
124         $act->time = time();
125         $act->actor = $this->scoped->asActivityObject();
126
127         $act->context = new ActivityContext();
128         // FIXME: Add location here? Let's make it possible to include current location with less code...
129
130
131         $actobj = new ActivityObject();
132         $actobj->id = UUID::gen();
133         $actobj->type = Happening::OBJECT_TYPE;
134         $actobj->title = $title;
135         $actobj->summary = $description;
136         $actobj->extra[] = array('dtstart',
137                                 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
138                                 common_date_iso8601($start_str));
139         $actobj->extra[] = array('dtend',
140                                 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
141                                 common_date_iso8601($end_str));
142         $actobj->extra[] = array('location',
143                                 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
144                                 $location);
145         $actobj->extra[] = array('url',
146                                 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
147                                 $url);
148
149         /* We don't use these ourselves, but we add them to be nice RSS/XML citizens */
150         $actobj->extra[] = array('startdate',
151                                 array('xmlns' => 'http://purl.org/rss/1.0/modules/event/'),
152                                 common_date_iso8601($start_str));
153         $actobj->extra[] = array('enddate',
154                                 array('xmlns' => 'http://purl.org/rss/1.0/modules/event/'),
155                                 common_date_iso8601($end_str));
156         $actobj->extra[] = array('location',
157                                 array('xmlns' => 'http://purl.org/rss/1.0/modules/event/'),
158                                 $location);
159
160         $act->objects = array($actobj);
161
162         $stored = Notice::saveActivity($act, $this->scoped, $options);
163
164         return _m('Saved the event.');
165     }
166 }