newevent.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Add a new event
  37. *
  38. * @category Event
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class NeweventAction extends Action
  46. {
  47. protected $user = null;
  48. protected $error = null;
  49. protected $complete = null;
  50. protected $title = null;
  51. protected $location = null;
  52. protected $description = null;
  53. protected $startTime = null;
  54. protected $endTime = null;
  55. /**
  56. * Returns the title of the action
  57. *
  58. * @return string Action title
  59. */
  60. function title()
  61. {
  62. // TRANS: Title for new event form.
  63. return _m('TITLE','New event');
  64. }
  65. /**
  66. * For initializing members of the class.
  67. *
  68. * @param array $argarray misc. arguments
  69. *
  70. * @return boolean true
  71. */
  72. function prepare($argarray)
  73. {
  74. parent::prepare($argarray);
  75. $this->user = common_current_user();
  76. if (empty($this->user)) {
  77. // TRANS: Client exception thrown when trying to post an event while not logged in.
  78. throw new ClientException(_m('Must be logged in to post a event.'),
  79. 403);
  80. }
  81. if ($this->isPost()) {
  82. $this->checkSessionToken();
  83. }
  84. try {
  85. $this->title = $this->trimmed('title');
  86. if (empty($this->title)) {
  87. // TRANS: Client exception thrown when trying to post an event without providing a title.
  88. throw new ClientException(_m('Title required.'));
  89. }
  90. $this->location = $this->trimmed('location');
  91. $this->url = $this->trimmed('url');
  92. $this->description = $this->trimmed('description');
  93. $tz = $this->trimmed('tz');
  94. $startDate = $this->trimmed('startdate');
  95. if (empty($startDate)) {
  96. // TRANS: Client exception thrown when trying to post an event without providing a start date.
  97. throw new ClientException(_m('Start date required.'));
  98. }
  99. $startTime = $this->trimmed('event-starttime');
  100. if (empty($startTime)) {
  101. $startTime = '00:00';
  102. }
  103. $endDate = $this->trimmed('enddate');
  104. if (empty($endDate)) {
  105. // TRANS: Client exception thrown when trying to post an event without providing an end date.
  106. throw new ClientException(_m('End date required.'));
  107. }
  108. $endTime = $this->trimmed('event-endtime');
  109. if (empty($endTime)) {
  110. $endTime = '00:00';
  111. }
  112. $start = $startDate . ' ' . $startTime . ' ' . $tz;
  113. $end = $endDate . ' ' . $endTime . ' ' . $tz;
  114. $this->startTime = strtotime($start);
  115. $this->endTime = strtotime($end);
  116. if ($this->startTime == 0) {
  117. // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
  118. // TRANS: %s is the data that could not be processed.
  119. throw new ClientException(sprintf(_m('Could not parse date "%s".'),
  120. $start));
  121. }
  122. if ($this->endTime == 0) {
  123. // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
  124. // TRANS: %s is the data that could not be processed.
  125. throw new ClientException(sprintf(_m('Could not parse date "%s".'),
  126. $end));
  127. }
  128. } catch (ClientException $ce) {
  129. if ($this->boolean('ajax')) {
  130. $this->outputAjaxError($ce->getMessage());
  131. return false;
  132. } else {
  133. $this->error = $ce->getMessage();
  134. $this->showPage();
  135. return false;
  136. }
  137. }
  138. return true;
  139. }
  140. /**
  141. * Handler method
  142. *
  143. * @param array $argarray is ignored since it's now passed in in prepare()
  144. *
  145. * @return void
  146. */
  147. function handle($argarray=null)
  148. {
  149. parent::handle($argarray);
  150. if ($this->isPost()) {
  151. $this->newEvent();
  152. } else {
  153. $this->showPage();
  154. }
  155. return;
  156. }
  157. /**
  158. * Add a new event
  159. *
  160. * @return void
  161. */
  162. function newEvent()
  163. {
  164. try {
  165. if (empty($this->title)) {
  166. // TRANS: Client exception thrown when trying to post an event without providing a title.
  167. throw new ClientException(_m('Event must have a title.'));
  168. }
  169. if (empty($this->startTime)) {
  170. // TRANS: Client exception thrown when trying to post an event without providing a start time.
  171. throw new ClientException(_m('Event must have a start time.'));
  172. }
  173. if (empty($this->endTime)) {
  174. // TRANS: Client exception thrown when trying to post an event without providing an end time.
  175. throw new ClientException(_m('Event must have an end time.'));
  176. }
  177. if (!empty($this->url) && Validate::uri($this->url) === false) {
  178. // TRANS: Client exception thrown when trying to post an event with an invalid URL.
  179. throw new ClientException(_m('URL must be valid.'));
  180. }
  181. $options = array();
  182. // Does the heavy-lifting for getting "To:" information
  183. ToSelector::fillOptions($this, $options);
  184. $profile = $this->user->getProfile();
  185. $saved = Happening::saveNew($profile,
  186. $this->startTime,
  187. $this->endTime,
  188. $this->title,
  189. $this->location,
  190. $this->description,
  191. $this->url,
  192. $options);
  193. $event = Happening::fromNotice($saved);
  194. RSVP::saveNew($profile, $event, RSVP::POSITIVE);
  195. } catch (ClientException $ce) {
  196. if ($this->boolean('ajax')) {
  197. $this->outputAjaxError($ce->getMessage());
  198. return;
  199. } else {
  200. $this->error = $ce->getMessage();
  201. $this->showPage();
  202. return;
  203. }
  204. }
  205. if ($this->boolean('ajax')) {
  206. $this->startHTML('text/xml;charset=utf-8');
  207. $this->elementStart('head');
  208. // TRANS: Page title after sending a notice.
  209. $this->element('title', null, _m('Event saved'));
  210. $this->elementEnd('head');
  211. $this->elementStart('body');
  212. $this->showNotice($saved);
  213. $this->elementEnd('body');
  214. $this->endHTML();
  215. } else {
  216. common_redirect($saved->getUrl(), 303);
  217. }
  218. }
  219. // @todo factor this out into a base class
  220. function outputAjaxError($msg)
  221. {
  222. $this->startHTML('text/xml;charset=utf-8');
  223. $this->elementStart('head');
  224. // TRANS: Page title after an AJAX error occurs
  225. $this->element('title', null, _('Ajax Error'));
  226. $this->elementEnd('head');
  227. $this->elementStart('body');
  228. $this->element('p', array('id' => 'error'), $msg);
  229. $this->elementEnd('body');
  230. $this->endHTML();
  231. return;
  232. }
  233. /**
  234. * Show the event form
  235. *
  236. * @return void
  237. */
  238. function showContent()
  239. {
  240. if (!empty($this->error)) {
  241. $this->element('p', 'error', $this->error);
  242. }
  243. $form = new EventForm($this);
  244. $form->show();
  245. return;
  246. }
  247. /**
  248. * Return true if read only.
  249. *
  250. * MAY override
  251. *
  252. * @param array $args other arguments
  253. *
  254. * @return boolean is read only action?
  255. */
  256. function isReadOnly($args)
  257. {
  258. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  259. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  260. return true;
  261. } else {
  262. return false;
  263. }
  264. }
  265. /**
  266. * Output a notice
  267. *
  268. * Used to generate the notice code for Ajax results.
  269. *
  270. * @param Notice $notice Notice that was saved
  271. *
  272. * @return void
  273. */
  274. function showNotice(Notice $notice)
  275. {
  276. $nli = new NoticeListItem($notice, $this);
  277. $nli->show();
  278. }
  279. }