EventPlugin.php 14 KB

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