EventPlugin.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Microapp plugin for event invitations and RSVPs
  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. * Event plugin
  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 EventPlugin extends MicroAppPlugin
  46. {
  47. /**
  48. * Set up our tables (event and rsvp)
  49. *
  50. * @see Schema
  51. * @see ColumnDef
  52. *
  53. * @return boolean hook value; true means continue processing, false means stop.
  54. */
  55. function onCheckSchema()
  56. {
  57. $schema = Schema::get();
  58. $schema->ensureTable('happening', Happening::schemaDef());
  59. $schema->ensureTable('rsvp', RSVP::schemaDef());
  60. return true;
  61. }
  62. /**
  63. * Map URLs to actions
  64. *
  65. * @param URLMapper $m path-to-action mapper
  66. *
  67. * @return boolean hook value; true means continue processing, false means stop.
  68. */
  69. public function onRouterInitialized(URLMapper $m)
  70. {
  71. $m->connect('main/event/new',
  72. array('action' => 'newevent'));
  73. $m->connect('main/event/rsvp',
  74. array('action' => 'newrsvp'));
  75. $m->connect('main/event/rsvp/cancel',
  76. array('action' => 'cancelrsvp'));
  77. $m->connect('event/:id',
  78. array('action' => 'showevent'),
  79. array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
  80. $m->connect('rsvp/:id',
  81. array('action' => 'showrsvp'),
  82. array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
  83. $m->connect('main/event/updatetimes',
  84. array('action' => 'timelist'));
  85. return true;
  86. }
  87. function onPluginVersion(&$versions)
  88. {
  89. $versions[] = array('name' => 'Event',
  90. 'version' => GNUSOCIAL_VERSION,
  91. 'author' => 'Evan Prodromou',
  92. 'homepage' => 'http://status.net/wiki/Plugin:Event',
  93. 'description' =>
  94. // TRANS: Plugin description.
  95. _m('Event invitations and RSVPs.'));
  96. return true;
  97. }
  98. function appTitle() {
  99. // TRANS: Title for event application.
  100. return _m('TITLE','Event');
  101. }
  102. function tag() {
  103. return 'event';
  104. }
  105. function types() {
  106. return array(Happening::OBJECT_TYPE,
  107. RSVP::POSITIVE,
  108. RSVP::NEGATIVE,
  109. RSVP::POSSIBLE);
  110. }
  111. /**
  112. * Given a parsed ActivityStreams activity, save it into a notice
  113. * and other data structures.
  114. *
  115. * @param Activity $activity
  116. * @param Profile $actor
  117. * @param array $options=array()
  118. *
  119. * @return Notice the resulting notice
  120. */
  121. function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
  122. {
  123. if (count($activity->objects) != 1) {
  124. // TRANS: Exception thrown when there are too many activity objects.
  125. throw new Exception(_m('Too many activity objects.'));
  126. }
  127. $happeningObj = $activity->objects[0];
  128. if ($happeningObj->type != Happening::OBJECT_TYPE) {
  129. // TRANS: Exception thrown when event plugin comes across a non-event type object.
  130. throw new Exception(_m('Wrong type for object.'));
  131. }
  132. $notice = null;
  133. switch ($activity->verb) {
  134. case ActivityVerb::POST:
  135. // FIXME: get startTime, endTime, location and URL
  136. $notice = Happening::saveNew($actor,
  137. $start_time,
  138. $end_time,
  139. $happeningObj->title,
  140. null,
  141. $happeningObj->summary,
  142. null,
  143. $options);
  144. break;
  145. case RSVP::POSITIVE:
  146. case RSVP::NEGATIVE:
  147. case RSVP::POSSIBLE:
  148. $happening = Happening::getKV('uri', $happeningObj->id);
  149. if (empty($happening)) {
  150. // FIXME: save the event
  151. // TRANS: Exception thrown when trying to RSVP for an unknown event.
  152. throw new Exception(_m('RSVP for unknown event.'));
  153. }
  154. $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
  155. break;
  156. default:
  157. // TRANS: Exception thrown when event plugin comes across a undefined verb.
  158. throw new Exception(_m('Unknown verb for events.'));
  159. }
  160. return $notice;
  161. }
  162. /**
  163. * Turn a Notice into an activity object
  164. *
  165. * @param Notice $notice
  166. *
  167. * @return ActivityObject
  168. */
  169. function activityObjectFromNotice(Notice $notice)
  170. {
  171. $happening = null;
  172. switch ($notice->object_type) {
  173. case Happening::OBJECT_TYPE:
  174. $happening = Happening::fromNotice($notice);
  175. break;
  176. case RSVP::POSITIVE:
  177. case RSVP::NEGATIVE:
  178. case RSVP::POSSIBLE:
  179. $rsvp = RSVP::fromNotice($notice);
  180. $happening = $rsvp->getEvent();
  181. break;
  182. }
  183. if (empty($happening)) {
  184. // TRANS: Exception thrown when event plugin comes across a unknown object type.
  185. throw new Exception(_m('Unknown object type.'));
  186. }
  187. $notice = $happening->getNotice();
  188. if (empty($notice)) {
  189. // TRANS: Exception thrown when referring to a notice that is not an event an in event context.
  190. throw new Exception(_m('Unknown event notice.'));
  191. }
  192. $obj = new ActivityObject();
  193. $obj->id = $happening->uri;
  194. $obj->type = Happening::OBJECT_TYPE;
  195. $obj->title = $happening->title;
  196. $obj->summary = $happening->description;
  197. $obj->link = $notice->getUrl();
  198. // XXX: how to get this stuff into JSON?!
  199. $obj->extra[] = array('dtstart',
  200. array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
  201. common_date_iso8601($happening->start_time));
  202. $obj->extra[] = array('dtend',
  203. array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
  204. common_date_iso8601($happening->end_time));
  205. // FIXME: add location
  206. // FIXME: add URL
  207. // XXX: probably need other stuff here
  208. return $obj;
  209. }
  210. /**
  211. * Change the verb on RSVP notices
  212. *
  213. * @param Notice $notice
  214. *
  215. * @return ActivityObject
  216. */
  217. protected function extendActivity(Notice $stored, Activity $act, Profile $scoped=null) {
  218. switch ($stored->object_type) {
  219. case RSVP::POSITIVE:
  220. case RSVP::NEGATIVE:
  221. case RSVP::POSSIBLE:
  222. $act->verb = $stored->object_type;
  223. break;
  224. }
  225. return true;
  226. }
  227. /**
  228. * Form for our app
  229. *
  230. * @param HTMLOutputter $out
  231. * @return Widget
  232. */
  233. function entryForm($out)
  234. {
  235. return new EventForm($out);
  236. }
  237. /**
  238. * When a notice is deleted, clean up related tables.
  239. *
  240. * @param Notice $notice
  241. */
  242. function deleteRelated(Notice $notice)
  243. {
  244. switch ($notice->object_type) {
  245. case Happening::OBJECT_TYPE:
  246. common_log(LOG_DEBUG, "Deleting event from notice...");
  247. $happening = Happening::fromNotice($notice);
  248. $happening->delete();
  249. break;
  250. case RSVP::POSITIVE:
  251. case RSVP::NEGATIVE:
  252. case RSVP::POSSIBLE:
  253. common_log(LOG_DEBUG, "Deleting rsvp from notice...");
  254. $rsvp = RSVP::fromNotice($notice);
  255. common_log(LOG_DEBUG, "to delete: $rsvp->id");
  256. $rsvp->delete();
  257. break;
  258. default:
  259. common_log(LOG_DEBUG, "Not deleting related, wtf...");
  260. }
  261. }
  262. function onEndShowScripts($action)
  263. {
  264. $action->script($this->path('js/event.js'));
  265. }
  266. function onEndShowStyles($action)
  267. {
  268. $action->cssLink($this->path('css/event.css'));
  269. return true;
  270. }
  271. function onStartAddNoticeReply($nli, $parent, $child)
  272. {
  273. // Filter out any poll responses
  274. if (($parent->object_type == Happening::OBJECT_TYPE) &&
  275. in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
  276. return false;
  277. }
  278. return true;
  279. }
  280. protected function showNoticeItemNotice(NoticeListItem $nli)
  281. {
  282. $nli->showAuthor();
  283. $nli->showContent();
  284. }
  285. protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
  286. {
  287. switch ($stored->object_type) {
  288. case Happening::OBJECT_TYPE:
  289. $this->showEvent($stored, $out, $scoped);
  290. break;
  291. case RSVP::POSITIVE:
  292. case RSVP::NEGATIVE:
  293. case RSVP::POSSIBLE:
  294. $this->showRSVP($stored, $out, $scoped);
  295. break;
  296. }
  297. }
  298. protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
  299. {
  300. $profile = $stored->getProfile();
  301. $event = Happening::fromNotice($stored);
  302. if (!$event instanceof Happening) {
  303. // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
  304. $out->element('p', null, _m('Deleted.'));
  305. return;
  306. }
  307. $out->elementStart('div', 'h-event');
  308. $out->elementStart('h3', 'p-summary p-name');
  309. try {
  310. $out->element('a', array('href' => $event->getUrl()), $event->title);
  311. } catch (InvalidUrlException $e) {
  312. $out->text($event->title);
  313. }
  314. $out->elementEnd('h3');
  315. $now = new DateTime();
  316. $startDate = new DateTime($event->start_time);
  317. $endDate = new DateTime($event->end_time);
  318. $userTz = new DateTimeZone(common_timezone());
  319. // Localize the time for the observer
  320. $now->setTimeZone($userTz);
  321. $startDate->setTimezone($userTz);
  322. $endDate->setTimezone($userTz);
  323. $thisYear = $now->format('Y');
  324. $startYear = $startDate->format('Y');
  325. $endYear = $endDate->format('Y');
  326. $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
  327. if ($startYear != $thisYear || $endYear != $thisYear) {
  328. $dateFmt .= 'Y,'; // append year if we need to think about years
  329. }
  330. $startDateStr = $startDate->format($dateFmt);
  331. $endDateStr = $endDate->format($dateFmt);
  332. $timeFmt = 'g:ia';
  333. $startTimeStr = $startDate->format($timeFmt);
  334. $endTimeStr = $endDate->format("{$timeFmt} (T)");
  335. $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
  336. // TRANS: Field label for event description.
  337. $out->element('strong', null, _m('Time:'));
  338. $out->element('time', array('class' => 'dt-start',
  339. 'datetime' => common_date_iso8601($event->start_time)),
  340. $startDateStr . ' ' . $startTimeStr);
  341. $out->text(' – ');
  342. $out->element('time', array('class' => 'dt-end',
  343. 'datetime' => common_date_iso8601($event->end_time)),
  344. $startDateStr != $endDateStr
  345. ? "$endDateStr $endTimeStr"
  346. : $endTimeStr);
  347. $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
  348. if (!empty($event->location)) {
  349. $out->elementStart('div', 'event-location');
  350. // TRANS: Field label for event description.
  351. $out->element('strong', null, _m('Location:'));
  352. $out->element('span', 'p-location', $event->location);
  353. $out->elementEnd('div');
  354. }
  355. if (!empty($event->description)) {
  356. $out->elementStart('div', 'event-description');
  357. // TRANS: Field label for event description.
  358. $out->element('strong', null, _m('Description:'));
  359. $out->element('div', 'p-description', $event->description);
  360. $out->elementEnd('div');
  361. }
  362. $rsvps = $event->getRSVPs();
  363. $out->elementStart('div', 'event-rsvps');
  364. // TRANS: Field label for event description.
  365. $out->element('strong', null, _m('Attending:'));
  366. $out->elementStart('ul', 'attending-list');
  367. foreach ($rsvps as $verb => $responses) {
  368. $out->elementStart('li', 'rsvp-list');
  369. switch ($verb) {
  370. case RSVP::POSITIVE:
  371. $out->text(_('Yes:'));
  372. break;
  373. case RSVP::NEGATIVE:
  374. $out->text(_('No:'));
  375. break;
  376. case RSVP::POSSIBLE:
  377. $out->text(_('Maybe:'));
  378. break;
  379. }
  380. $ids = array();
  381. foreach ($responses as $response) {
  382. $ids[] = $response->profile_id;
  383. }
  384. $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
  385. $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
  386. $minilist->show();
  387. $out->elementEnd('li');
  388. }
  389. $out->elementEnd('ul');
  390. $out->elementEnd('div');
  391. if ($scoped instanceof Profile) {
  392. $rsvp = $event->getRSVP($scoped);
  393. if (empty($rsvp)) {
  394. $form = new RSVPForm($event, $out);
  395. } else {
  396. $form = new CancelRSVPForm($rsvp, $out);
  397. }
  398. $form->show();
  399. }
  400. $out->elementEnd('div');
  401. }
  402. protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
  403. {
  404. $rsvp = RSVP::fromNotice($stored);
  405. if (empty($rsvp)) {
  406. // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
  407. $out->element('p', null, _m('Deleted.'));
  408. return;
  409. }
  410. $out->elementStart('div', 'rsvp');
  411. $out->raw($rsvp->asHTML());
  412. $out->elementEnd('div');
  413. }
  414. }