Happening.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * Data class for happenings
  4. *
  5. * PHP version 5
  6. *
  7. * @category Data
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@status.net>
  10. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  11. * @link http://status.net/
  12. *
  13. * StatusNet - the distributed open-source microblogging tool
  14. * Copyright (C) 2011, StatusNet, Inc.
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. */
  29. if (!defined('GNUSOCIAL')) { exit(1); }
  30. /**
  31. * Data class for happenings
  32. *
  33. * There's already an Event class in lib/event.php, so we couldn't
  34. * call this an Event without causing a hole in space-time.
  35. *
  36. * "Happening" seemed good enough.
  37. *
  38. * @category Event
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  42. * @link http://status.net/
  43. *
  44. * @see Managed_DataObject
  45. */
  46. class Happening extends Managed_DataObject
  47. {
  48. const OBJECT_TYPE = 'http://activitystrea.ms/schema/1.0/event';
  49. public $__table = 'happening'; // table name
  50. public $id; // varchar(36) UUID
  51. public $uri; // varchar(191) not 255 because utf8mb4 takes more space
  52. public $profile_id; // int
  53. public $start_time; // datetime
  54. public $end_time; // datetime
  55. public $title; // varchar(191) not 255 because utf8mb4 takes more space
  56. public $location; // varchar(191) not 255 because utf8mb4 takes more space
  57. public $url; // varchar(191) not 255 because utf8mb4 takes more space
  58. public $description; // text
  59. public $created; // datetime
  60. /**
  61. * The One True Thingy that must be defined and declared.
  62. */
  63. public static function schemaDef()
  64. {
  65. return array(
  66. 'description' => 'A real-world happening',
  67. 'fields' => array(
  68. 'id' => array('type' => 'char',
  69. 'length' => 36,
  70. 'not null' => true,
  71. 'description' => 'UUID'),
  72. 'uri' => array('type' => 'varchar',
  73. 'length' => 191,
  74. 'not null' => true),
  75. 'profile_id' => array('type' => 'int', 'not null' => true),
  76. 'start_time' => array('type' => 'datetime', 'not null' => true),
  77. 'end_time' => array('type' => 'datetime', 'not null' => true),
  78. 'title' => array('type' => 'varchar',
  79. 'length' => 191,
  80. 'not null' => true),
  81. 'location' => array('type' => 'varchar',
  82. 'length' => 191),
  83. 'url' => array('type' => 'varchar',
  84. 'length' => 191),
  85. 'description' => array('type' => 'text'),
  86. 'created' => array('type' => 'datetime',
  87. 'not null' => true),
  88. ),
  89. 'primary key' => array('id'),
  90. 'unique keys' => array(
  91. 'happening_uri_key' => array('uri'),
  92. ),
  93. 'foreign keys' => array('happening_profile_id__key' => array('profile', array('profile_id' => 'id')),
  94. 'happening_uri__key' => array('notice', array('uri' => 'uri'))),
  95. 'indexes' => array('happening_created_idx' => array('created'),
  96. 'happening_start_end_idx' => array('start_time', 'end_time')),
  97. );
  98. }
  99. public static function saveActivityObject(Activity $act, Notice $stored)
  100. {
  101. if (count($act->objects) !== 1) {
  102. // TRANS: Exception thrown when there are too many activity objects.
  103. throw new Exception(_m('Too many activity objects.'));
  104. }
  105. $actobj = $act->objects[0];
  106. if (!ActivityUtils::compareTypes($actobj->type, [Happening::OBJECT_TYPE])) {
  107. // TRANS: Exception thrown when event plugin comes across a non-event type object.
  108. throw new Exception(_m('Wrong type for object.'));
  109. }
  110. try {
  111. $other = Happening::getByKeys(['uri' => $actobj->id]);
  112. throw AlreadyFulfilledException('Happening already exists.');
  113. } catch (NoResultException $e) {
  114. // alright, let's save this
  115. }
  116. $dtstart = null;
  117. $dtend = null;
  118. $location = null;
  119. $url = null;
  120. foreach ($actobj->extra as $extra) {
  121. switch ($extra[0]) {
  122. case 'dtstart':
  123. $dtstart = $extra[2];
  124. case 'dtend':
  125. $dtend = $extra[2];
  126. break;
  127. case 'location':
  128. // location is optional
  129. $location = $extra[2];
  130. break;
  131. case 'url':
  132. // url is optional
  133. $url = $extra[2];
  134. }
  135. }
  136. if(empty($dtstart)) {
  137. // TRANS: Exception thrown when has no start date
  138. throw new Exception(_m('No start date for event.'));
  139. }
  140. if(empty($dtend)) {
  141. // TRANS: Exception thrown when has no end date
  142. throw new Exception(_m('No end date for event.'));
  143. }
  144. // convert RFC3339 dates delivered in Activity Stream to MySQL DATETIME date format
  145. $start_time = new DateTime($dtstart);
  146. $start_time->setTimezone(new DateTimeZone('UTC'));
  147. $start_time = $start_time->format('Y-m-d H:i:s');
  148. $end_time = new DateTime($dtend);
  149. $end_time->setTimezone(new DateTimeZone('UTC'));
  150. $end_time = $end_time->format('Y-m-d H:i:s');
  151. $ev = new Happening();
  152. $ev->id = UUID::gen();
  153. $ev->uri = $actobj->id;
  154. $ev->profile_id = $stored->getProfile()->getID();
  155. $ev->start_time = $start_time;
  156. $ev->end_time = $end_time;
  157. $ev->title = $actobj->title;
  158. $ev->location = $location;
  159. $ev->description = $stored->getContent();
  160. $ev->url = $url;
  161. $ev->created = $stored->getCreated();
  162. $ev->insert();
  163. return $ev;
  164. }
  165. public function insert()
  166. {
  167. $result = parent::insert();
  168. if ($result === false) {
  169. common_log_db_error($this, 'INSERT', __FILE__);
  170. throw new ServerException(_('Failed to insert '._ve(get_called_class()).' into database'));
  171. }
  172. return $result;
  173. }
  174. /**
  175. * Returns the profile's canonical url, not necessarily a uri/unique id
  176. *
  177. * @return string $url
  178. */
  179. public function getUrl()
  180. {
  181. if (empty($this->url) ||
  182. !filter_var($this->url, FILTER_VALIDATE_URL)) {
  183. throw new InvalidUrlException($this->url);
  184. }
  185. return $this->url;
  186. }
  187. public function getUri()
  188. {
  189. return $this->uri;
  190. }
  191. public function getStored()
  192. {
  193. return Notice::getByKeys(array('uri'=>$this->getUri()));
  194. }
  195. static function fromStored(Notice $stored)
  196. {
  197. if (!ActivityUtils::compareTypes($stored->getObjectType(), [self::OBJECT_TYPE])) {
  198. throw new ServerException('Notice is not of type '.self::OBJECT_TYPE);
  199. }
  200. return self::getByKeys(array('uri'=>$stored->getUri()));
  201. }
  202. function getRSVPs()
  203. {
  204. return RSVP::forEvent($this);
  205. }
  206. function getRSVP($profile)
  207. {
  208. return RSVP::pkeyGet(array('profile_id' => $profile->getID(),
  209. 'event_uri' => $this->getUri()));
  210. }
  211. static public function getObjectType()
  212. {
  213. return self::OBJECT_TYPE;
  214. }
  215. public function asActivityObject()
  216. {
  217. $actobj = new ActivityObject();
  218. $actobj->id = $this->getUri();
  219. $actobj->type = self::getObjectType();
  220. $actobj->title = $this->title;
  221. $actobj->summary = $this->description;
  222. $actobj->extra[] = array('dtstart',
  223. array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
  224. common_date_iso8601($this->start_time));
  225. $actobj->extra[] = array('dtend',
  226. array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
  227. common_date_iso8601($this->end_time));
  228. $actobj->extra[] = array('location',
  229. array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
  230. $this->location);
  231. try {
  232. $actobj->extra[] = array('url',
  233. array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
  234. $this->getUrl());
  235. } catch (InvalidUrlException $e) {
  236. // oh well, no URL for you!
  237. }
  238. /* We don't use these ourselves, but we add them to be nice RSS/XML citizens */
  239. $actobj->extra[] = array('startdate',
  240. array('xmlns' => 'http://purl.org/rss/1.0/modules/event/'),
  241. common_date_iso8601($this->start_time));
  242. $actobj->extra[] = array('enddate',
  243. array('xmlns' => 'http://purl.org/rss/1.0/modules/event/'),
  244. common_date_iso8601($this->end_time));
  245. $actobj->extra[] = array('location',
  246. array('xmlns' => 'http://purl.org/rss/1.0/modules/event/'),
  247. $this->location);
  248. return $actobj;
  249. }
  250. }