newevent.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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('GNUSOCIAL')) { exit(1); }
  31. /**
  32. * Add a new event
  33. *
  34. * @category Event
  35. * @package StatusNet
  36. * @author Evan Prodromou <evan@status.net>
  37. * @copyright 2011 StatusNet, Inc.
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  39. * @link http://status.net/
  40. */
  41. class NeweventAction extends FormAction
  42. {
  43. protected $form = 'Event';
  44. /**
  45. * Returns the title of the action
  46. *
  47. * @return string Action title
  48. */
  49. function title()
  50. {
  51. // TRANS: Title for new event form.
  52. return _m('TITLE','New event');
  53. }
  54. protected function doPost()
  55. {
  56. // HUMAN TEXT DATA
  57. $title = $this->trimmed('title');
  58. if (empty($title)) {
  59. // TRANS: Client exception thrown when trying to post an event without providing a title.
  60. throw new ClientException(_m('Event must have a title.'));
  61. }
  62. $description = $this->trimmed('description');
  63. // TIME PARSING
  64. $tz = $this->trimmed('tz');
  65. $startDate = $this->trimmed('startdate');
  66. if (empty($startDate)) {
  67. // TRANS: Client exception thrown when trying to post an event without providing a start date.
  68. throw new ClientException(_m('Start date required.'));
  69. }
  70. $startTime = $this->trimmed('event-starttime');
  71. if (empty($startTime)) {
  72. // TRANS: Client exception thrown when trying to post an event without providing a start time.
  73. throw new ClientException(_m('Event must have a start time.'));
  74. }
  75. $start_str = sprintf('%s %s %s', $startDate, $startTime, $tz);
  76. $start = strtotime($start_str);
  77. if ($start === false) {
  78. // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
  79. // TRANS: %s is the data that could not be processed.
  80. throw new ClientException(sprintf(_m('Could not parse date %s.'), _ve($start_str)));
  81. }
  82. $endDate = $this->trimmed('enddate');
  83. if (empty($endDate)) {
  84. // TRANS: Client exception thrown when trying to post an event without providing an end date.
  85. throw new ClientException(_m('End date required.'));
  86. }
  87. $endTime = $this->trimmed('event-endtime');
  88. if (empty($endTime)) {
  89. // TRANS: Client exception thrown when trying to post an event without providing an end time.
  90. throw new ClientException(_m('Event must have an end time.'));
  91. }
  92. $end_str = sprintf('%s %s %s', $endDate, $endTime, $tz);
  93. $end = strtotime($end_str);
  94. if ($end === false) {
  95. // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
  96. // TRANS: %s is the data that could not be processed.
  97. throw new ClientException(sprintf(_m('Could not parse date %s.'), _ve($end_str)));
  98. }
  99. $url = $this->trimmed('url');
  100. if (!empty($url) && !common_valid_http_url($url)) {
  101. // TRANS: Client exception thrown when trying to post an event with an invalid (non-empty) URL.
  102. throw new ClientException(_m('An event URL must be a valid HTTP/HTTPS link.'));
  103. }
  104. // LOCATION DATA
  105. $location = $this->trimmed('location');
  106. $options = [ 'source' => 'web' ];
  107. $act = new Activity();
  108. $act->verb = ActivityVerb::POST;
  109. $act->time = time();
  110. $act->actor = $this->scoped->asActivityObject();
  111. $act->context = new ActivityContext();
  112. // FIXME: Add location here? Let's make it possible to include current location with less code...
  113. $actobj = new ActivityObject();
  114. $actobj->id = UUID::gen();
  115. $actobj->type = Happening::OBJECT_TYPE;
  116. $actobj->title = $title;
  117. $actobj->summary = $description;
  118. $actobj->extra[] = array('dtstart',
  119. array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
  120. common_date_iso8601($start_str));
  121. $actobj->extra[] = array('dtend',
  122. array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
  123. common_date_iso8601($end_str));
  124. $actobj->extra[] = array('location',
  125. array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
  126. $location);
  127. $actobj->extra[] = array('url',
  128. array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
  129. $url);
  130. /* We don't use these ourselves, but we add them to be nice RSS/XML citizens */
  131. $actobj->extra[] = array('startdate',
  132. array('xmlns' => 'http://purl.org/rss/1.0/plugins/event/'),
  133. common_date_iso8601($start_str));
  134. $actobj->extra[] = array('enddate',
  135. array('xmlns' => 'http://purl.org/rss/1.0/plugins/event/'),
  136. common_date_iso8601($end_str));
  137. $actobj->extra[] = array('location',
  138. array('xmlns' => 'http://purl.org/rss/1.0/plugins/event/'),
  139. $location);
  140. $act->objects = array($actobj);
  141. $stored = Notice::saveActivity($act, $this->scoped, $options);
  142. return _m('Saved the event.');
  143. }
  144. }