EventPlugin.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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('GNUSOCIAL')) { exit(1); }
  31. /**
  32. * Event plugin
  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 EventPlugin extends ActivityVerbHandlerPlugin
  42. {
  43. const PLUGIN_VERSION = '2.0.0';
  44. /**
  45. * Set up our tables (event and rsvp)
  46. *
  47. * @see Schema
  48. * @see ColumnDef
  49. *
  50. * @return boolean hook value; true means continue processing, false means stop.
  51. */
  52. function onCheckSchema()
  53. {
  54. $schema = Schema::get();
  55. $schema->ensureTable('happening', Happening::schemaDef());
  56. $schema->ensureTable('rsvp', RSVP::schemaDef());
  57. return true;
  58. }
  59. public function onBeforePluginCheckSchema()
  60. {
  61. RSVP::beforeSchemaUpdate();
  62. return true;
  63. }
  64. /**
  65. * Map URLs to actions
  66. *
  67. * @param URLMapper $m path-to-action mapper
  68. *
  69. * @return boolean hook value; true means continue processing, false means stop.
  70. */
  71. public function onRouterInitialized(URLMapper $m)
  72. {
  73. $m->connect('main/event/new',
  74. array('action' => 'newevent'));
  75. $m->connect('main/event/rsvp',
  76. array('action' => 'rsvp'));
  77. $m->connect('main/event/rsvp/:rsvp', // this will probably change to include event notice id
  78. array('action' => 'rsvp'),
  79. array('rsvp' => '[a-z]+'));
  80. $m->connect('event/:id',
  81. array('action' => 'showevent'),
  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('rsvp/:id',
  84. array('action' => 'showrsvp'),
  85. array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
  86. $m->connect('main/event/updatetimes',
  87. array('action' => 'timelist'));
  88. $m->connect(':nickname/events',
  89. array('action' => 'events'),
  90. array('nickname' => Nickname::DISPLAY_FMT));
  91. return true;
  92. }
  93. function onPluginVersion(array &$versions)
  94. {
  95. $versions[] = array('name' => 'Event',
  96. 'version' => self::PLUGIN_VERSION,
  97. 'author' => 'Evan Prodromou',
  98. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Event',
  99. 'description' =>
  100. // TRANS: Plugin description.
  101. _m('Event invitations and RSVPs.'));
  102. return true;
  103. }
  104. function appTitle() {
  105. // TRANS: Title for event application.
  106. return _m('TITLE','Event');
  107. }
  108. function tag() {
  109. return 'event';
  110. }
  111. function types() {
  112. return array(Happening::OBJECT_TYPE);
  113. }
  114. function verbs() {
  115. return array(ActivityVerb::POST,
  116. RSVP::POSITIVE,
  117. RSVP::NEGATIVE,
  118. RSVP::POSSIBLE);
  119. }
  120. function isMyNotice(Notice $notice) {
  121. if (!empty($notice->object_type)) {
  122. return parent::isMyNotice($notice);
  123. }
  124. return $this->isMyVerb($notice->verb);
  125. }
  126. public function newFormAction() {
  127. // such as 'newbookmark' or 'newevent' route
  128. return 'new'.$this->tag();
  129. }
  130. function onStartShowEntryForms(&$tabs)
  131. {
  132. $tabs[$this->tag()] = array('title' => $this->appTitle(),
  133. 'href' => common_local_url($this->newFormAction()),
  134. );
  135. return true;
  136. }
  137. function onStartMakeEntryForm($tag, $out, &$form)
  138. {
  139. if ($tag == $this->tag()) {
  140. $form = $this->entryForm($out);
  141. return false;
  142. }
  143. return true;
  144. }
  145. protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  146. {
  147. return $verb;
  148. }
  149. protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  150. {
  151. return true;
  152. }
  153. protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  154. {
  155. throw new ServerException('Event does not handle doActionPost yet', 501);
  156. }
  157. protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
  158. {
  159. return new RSVPForm(Happening::fromStored($target), $action);
  160. }
  161. protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
  162. {
  163. switch (true) {
  164. case ActivityUtils::compareVerbs($stored->getVerb(), [ActivityVerb::POST]):
  165. return Happening::saveActivityObject($act, $stored);
  166. break;
  167. case ActivityUtils::compareVerbs($stored->getVerb(), [RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
  168. return RSVP::saveActivityObject($act, $stored);
  169. break;
  170. }
  171. return null;
  172. }
  173. function activityObjectFromNotice(Notice $stored)
  174. {
  175. $happening = null;
  176. switch (true) {
  177. case $stored->isVerb([ActivityVerb::POST]) && $stored->isObjectType([Happening::OBJECT_TYPE]):
  178. $obj = Happening::fromStored($stored)->asActivityObject();
  179. break;
  180. // isObjectType here is because we had the verb stored in object_type previously for unknown reasons
  181. case $stored->isObjectType([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
  182. case $stored->isVerb([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
  183. $obj = RSVP::fromStored($stored)->asActivityObject();
  184. break;
  185. default:
  186. // TRANS: Exception thrown when event plugin comes across a unknown object type.
  187. throw new Exception(_m('Unknown object type.'));
  188. }
  189. return $obj;
  190. }
  191. /**
  192. * Form for our app
  193. *
  194. * @param HTMLOutputter $out
  195. * @return Widget
  196. */
  197. function entryForm($out)
  198. {
  199. return new EventForm($out);
  200. }
  201. function deleteRelated(Notice $stored)
  202. {
  203. switch (true) {
  204. case $stored->isObjectType([Happening::OBJECT_TYPE]):
  205. common_log(LOG_DEBUG, "Deleting event from notice...");
  206. try {
  207. $happening = Happening::fromStored($stored);
  208. $happening->delete();
  209. } catch (NoResultException $e) {
  210. // already gone
  211. }
  212. break;
  213. // isObjectType here is because we had the verb stored in object_type previously for unknown reasons
  214. case $stored->isObjectType([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
  215. case $stored->isVerb([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
  216. common_log(LOG_DEBUG, "Deleting rsvp from notice...");
  217. try {
  218. $rsvp = RSVP::fromStored($stored);
  219. $rsvp->delete();
  220. } catch (NoResultException $e) {
  221. // already gone
  222. }
  223. break;
  224. }
  225. }
  226. function onEndShowScripts($action)
  227. {
  228. $action->script($this->path('js/event.js'));
  229. }
  230. function onEndShowStyles($action)
  231. {
  232. $action->cssLink($this->path('css/event.css'));
  233. return true;
  234. }
  235. function onStartAddNoticeReply($nli, $parent, $child)
  236. {
  237. if (($parent->object_type == Happening::OBJECT_TYPE) &&
  238. in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
  239. return false;
  240. }
  241. return true;
  242. }
  243. protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
  244. {
  245. switch (true) {
  246. case ActivityUtils::compareTypes($stored->verb, array(ActivityVerb::POST)) &&
  247. ActivityUtils::compareTypes($stored->object_type, array(Happening::OBJECT_TYPE)):
  248. $this->showEvent($stored, $out, $scoped);
  249. break;
  250. case ActivityUtils::compareVerbs($stored->verb, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE)):
  251. $this->showRSVP($stored, $out, $scoped);
  252. break;
  253. default:
  254. throw new ServerException('This is not an Event notice');
  255. }
  256. return true;
  257. }
  258. protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
  259. {
  260. $profile = $stored->getProfile();
  261. $event = Happening::fromStored($stored);
  262. $out->elementStart('div', 'h-event');
  263. $out->elementStart('h3', 'p-summary p-name');
  264. try {
  265. $out->element('a', array('href' => $event->getUrl()), $event->title);
  266. } catch (InvalidUrlException $e) {
  267. $out->text($event->title);
  268. }
  269. $out->elementEnd('h3');
  270. $now = new DateTime();
  271. $startDate = new DateTime($event->start_time);
  272. $endDate = new DateTime($event->end_time);
  273. $userTz = new DateTimeZone(common_timezone());
  274. // Localize the time for the observer
  275. $now->setTimeZone($userTz);
  276. $startDate->setTimezone($userTz);
  277. $endDate->setTimezone($userTz);
  278. $thisYear = $now->format('Y');
  279. $startYear = $startDate->format('Y');
  280. $endYear = $endDate->format('Y');
  281. $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
  282. if ($startYear != $thisYear || $endYear != $thisYear) {
  283. $dateFmt .= 'Y,'; // append year if we need to think about years
  284. }
  285. $startDateStr = $startDate->format($dateFmt);
  286. $endDateStr = $endDate->format($dateFmt);
  287. $timeFmt = 'g:ia';
  288. $startTimeStr = $startDate->format($timeFmt);
  289. $endTimeStr = $endDate->format("{$timeFmt} (T)");
  290. $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
  291. // TRANS: Field label for event description.
  292. $out->element('strong', null, _m('Time:'));
  293. $out->element('time', array('class' => 'dt-start',
  294. 'datetime' => common_date_iso8601($event->start_time)),
  295. $startDateStr . ' ' . $startTimeStr);
  296. $out->text(' – ');
  297. $out->element('time', array('class' => 'dt-end',
  298. 'datetime' => common_date_iso8601($event->end_time)),
  299. $startDateStr != $endDateStr
  300. ? "$endDateStr $endTimeStr"
  301. : $endTimeStr);
  302. $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
  303. if (!empty($event->location)) {
  304. $out->elementStart('div', 'event-location');
  305. // TRANS: Field label for event description.
  306. $out->element('strong', null, _m('Location:'));
  307. $out->element('span', 'p-location', $event->location);
  308. $out->elementEnd('div');
  309. }
  310. if (!empty($event->description)) {
  311. $out->elementStart('div', 'event-description');
  312. // TRANS: Field label for event description.
  313. $out->element('strong', null, _m('Description:'));
  314. $out->element('div', 'p-description', $event->description);
  315. $out->elementEnd('div');
  316. }
  317. $rsvps = $event->getRSVPs();
  318. $out->elementStart('div', 'event-rsvps');
  319. // TRANS: Field label for event description.
  320. $out->element('strong', null, _m('Attending:'));
  321. $out->elementStart('ul', 'attending-list');
  322. foreach ($rsvps as $verb => $responses) {
  323. $out->elementStart('li', 'rsvp-list');
  324. switch ($verb) {
  325. case RSVP::POSITIVE:
  326. $out->text(_('Yes:'));
  327. break;
  328. case RSVP::NEGATIVE:
  329. $out->text(_('No:'));
  330. break;
  331. case RSVP::POSSIBLE:
  332. $out->text(_('Maybe:'));
  333. break;
  334. }
  335. $ids = array();
  336. foreach ($responses as $response) {
  337. $ids[] = $response->profile_id;
  338. }
  339. $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
  340. $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
  341. $minilist->show();
  342. $out->elementEnd('li');
  343. }
  344. $out->elementEnd('ul');
  345. $out->elementEnd('div');
  346. if ($scoped instanceof Profile) {
  347. $form = new RSVPForm($out, array('event'=>$event, 'scoped'=>$scoped));
  348. $form->show();
  349. }
  350. $out->elementEnd('div');
  351. }
  352. protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
  353. {
  354. try {
  355. $rsvp = RSVP::fromStored($stored)->asHTML();
  356. } catch (NoResultException $e) {
  357. // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
  358. $out->element('p', null, _m('Deleted.'));
  359. return;
  360. }
  361. $out->elementStart('div', 'rsvp');
  362. $out->raw($rsvp);
  363. $out->elementEnd('div');
  364. }
  365. function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
  366. {
  367. $menu->menuItem(common_local_url('events', array('nickname' => $target->getNickname())),
  368. // TRANS: Menu item in sample plugin.
  369. _m('Happenings'),
  370. // TRANS: Menu item title in sample plugin.
  371. _m('A list of your events'), false, 'nav_timeline_events');
  372. return true;
  373. }
  374. }