Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

584 lines
17KB

  1. <?php
  2. /**
  3. * @file mod/events.php
  4. * @brief The events module
  5. */
  6. use Friendica\App;
  7. use Friendica\Content\Nav;
  8. use Friendica\Content\Widget\CalendarExport;
  9. use Friendica\Core\ACL;
  10. use Friendica\Core\L10n;
  11. use Friendica\Core\Logger;
  12. use Friendica\Core\Renderer;
  13. use Friendica\Core\System;
  14. use Friendica\Core\Worker;
  15. use Friendica\Database\DBA;
  16. use Friendica\Model\Event;
  17. use Friendica\Model\Item;
  18. use Friendica\Model\Profile;
  19. use Friendica\Module\Login;
  20. use Friendica\Util\DateTimeFormat;
  21. use Friendica\Util\Strings;
  22. use Friendica\Util\Temporal;
  23. function events_init(App $a)
  24. {
  25. if (!local_user()) {
  26. return;
  27. }
  28. // If it's a json request abort here because we don't
  29. // need the widget data
  30. if ($a->argc > 1 && $a->argv[1] === 'json') {
  31. return;
  32. }
  33. if (empty($a->page['aside'])) {
  34. $a->page['aside'] = '';
  35. }
  36. $cal_widget = CalendarExport::getHTML();
  37. $a->page['aside'] .= $cal_widget;
  38. return;
  39. }
  40. function events_post(App $a)
  41. {
  42. Logger::log('post: ' . print_r($_REQUEST, true), Logger::DATA);
  43. if (!local_user()) {
  44. return;
  45. }
  46. $event_id = !empty($_POST['event_id']) ? intval($_POST['event_id']) : 0;
  47. $cid = !empty($_POST['cid']) ? intval($_POST['cid']) : 0;
  48. $uid = local_user();
  49. $start_text = Strings::escapeHtml(defaults($_REQUEST, 'start_text', ''));
  50. $finish_text = Strings::escapeHtml(defaults($_REQUEST, 'finish_text', ''));
  51. $adjust = intval(defaults($_POST, 'adjust', 0));
  52. $nofinish = intval(defaults($_POST, 'nofinish', 0));
  53. // The default setting for the `private` field in event_store() is false, so mirror that
  54. $private_event = false;
  55. $start = DBA::NULL_DATETIME;
  56. $finish = DBA::NULL_DATETIME;
  57. if ($start_text) {
  58. $start = $start_text;
  59. }
  60. if ($finish_text) {
  61. $finish = $finish_text;
  62. }
  63. if ($adjust) {
  64. $start = DateTimeFormat::convert($start, 'UTC', date_default_timezone_get());
  65. if (!$nofinish) {
  66. $finish = DateTimeFormat::convert($finish, 'UTC', date_default_timezone_get());
  67. }
  68. } else {
  69. $start = DateTimeFormat::utc($start);
  70. if (!$nofinish) {
  71. $finish = DateTimeFormat::utc($finish);
  72. }
  73. }
  74. // Don't allow the event to finish before it begins.
  75. // It won't hurt anything, but somebody will file a bug report
  76. // and we'll waste a bunch of time responding to it. Time that
  77. // could've been spent doing something else.
  78. $summary = Strings::escapeHtml(trim(defaults($_POST, 'summary', '')));
  79. $desc = Strings::escapeHtml(trim(defaults($_POST, 'desc', '')));
  80. $location = Strings::escapeHtml(trim(defaults($_POST, 'location', '')));
  81. $type = 'event';
  82. $action = ($event_id == '') ? 'new' : "event/" . $event_id;
  83. $onerror_path = "events/" . $action . "?summary=$summary&description=$desc&location=$location&start=$start_text&finish=$finish_text&adjust=$adjust&nofinish=$nofinish";
  84. if (strcmp($finish, $start) < 0 && !$nofinish) {
  85. notice(L10n::t('Event can not end before it has started.') . EOL);
  86. if (intval($_REQUEST['preview'])) {
  87. echo L10n::t('Event can not end before it has started.');
  88. killme();
  89. }
  90. $a->internalRedirect($onerror_path);
  91. }
  92. if (!$summary || ($start === DBA::NULL_DATETIME)) {
  93. notice(L10n::t('Event title and start time are required.') . EOL);
  94. if (intval($_REQUEST['preview'])) {
  95. echo L10n::t('Event title and start time are required.');
  96. killme();
  97. }
  98. $a->internalRedirect($onerror_path);
  99. }
  100. $share = intval(defaults($_POST, 'share', 0));
  101. $c = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
  102. intval(local_user())
  103. );
  104. if (DBA::isResult($c)) {
  105. $self = $c[0]['id'];
  106. } else {
  107. $self = 0;
  108. }
  109. if ($share) {
  110. $str_group_allow = !empty($_POST['group_allow']) ? perms2str($_POST['group_allow']) : '';
  111. $str_contact_allow = !empty($_POST['contact_allow']) ? perms2str($_POST['contact_allow']) : '';
  112. $str_group_deny = !empty($_POST['group_deny']) ? perms2str($_POST['group_deny']) : '';
  113. $str_contact_deny = !empty($_POST['contact_deny']) ? perms2str($_POST['contact_deny']) : '';
  114. // Undo the pseudo-contact of self, since there are real contacts now
  115. if (strpos($str_contact_allow, '<' . $self . '>') !== false) {
  116. $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
  117. }
  118. // Make sure to set the `private` field as true. This is necessary to
  119. // have the posts show up correctly in Diaspora if an event is created
  120. // as visible only to self at first, but then edited to display to others.
  121. if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
  122. $private_event = true;
  123. }
  124. } else {
  125. // Note: do not set `private` field for self-only events. It will
  126. // keep even you from seeing them!
  127. $str_contact_allow = '<' . $self . '>';
  128. $str_group_allow = $str_contact_deny = $str_group_deny = '';
  129. }
  130. $datarray = [];
  131. $datarray['start'] = $start;
  132. $datarray['finish'] = $finish;
  133. $datarray['summary'] = $summary;
  134. $datarray['desc'] = $desc;
  135. $datarray['location'] = $location;
  136. $datarray['type'] = $type;
  137. $datarray['adjust'] = $adjust;
  138. $datarray['nofinish'] = $nofinish;
  139. $datarray['uid'] = $uid;
  140. $datarray['cid'] = $cid;
  141. $datarray['allow_cid'] = $str_contact_allow;
  142. $datarray['allow_gid'] = $str_group_allow;
  143. $datarray['deny_cid'] = $str_contact_deny;
  144. $datarray['deny_gid'] = $str_group_deny;
  145. $datarray['private'] = $private_event;
  146. $datarray['id'] = $event_id;
  147. if (intval($_REQUEST['preview'])) {
  148. $html = Event::getHTML($datarray);
  149. echo $html;
  150. killme();
  151. }
  152. $item_id = Event::store($datarray);
  153. if (!$cid) {
  154. Worker::add(PRIORITY_HIGH, "Notifier", "event", $item_id);
  155. }
  156. $a->internalRedirect('events');
  157. }
  158. function events_content(App $a)
  159. {
  160. if (!local_user()) {
  161. notice(L10n::t('Permission denied.') . EOL);
  162. return Login::form();
  163. }
  164. if ($a->argc == 1) {
  165. $_SESSION['return_path'] = $a->cmd;
  166. }
  167. if (($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
  168. $r = q("UPDATE `event` SET `ignore` = 1 WHERE `id` = %d AND `uid` = %d",
  169. intval($a->argv[2]),
  170. intval(local_user())
  171. );
  172. }
  173. if (($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
  174. $r = q("UPDATE `event` SET `ignore` = 0 WHERE `id` = %d AND `uid` = %d",
  175. intval($a->argv[2]),
  176. intval(local_user())
  177. );
  178. }
  179. if ($a->theme_events_in_profile) {
  180. Nav::setSelected('home');
  181. } else {
  182. Nav::setSelected('events');
  183. }
  184. // get the translation strings for the callendar
  185. $i18n = Event::getStrings();
  186. $htpl = Renderer::getMarkupTemplate('event_head.tpl');
  187. $a->page['htmlhead'] .= Renderer::replaceMacros($htpl, [
  188. '$baseurl' => System::baseUrl(),
  189. '$module_url' => '/events',
  190. '$modparams' => 1,
  191. '$i18n' => $i18n,
  192. ]);
  193. $o = '';
  194. $tabs = '';
  195. // tabs
  196. if ($a->theme_events_in_profile) {
  197. $tabs = Profile::getTabs($a, true);
  198. }
  199. $mode = 'view';
  200. $y = 0;
  201. $m = 0;
  202. $ignored = !empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0;
  203. if ($a->argc > 1) {
  204. if ($a->argc > 2 && $a->argv[1] == 'event') {
  205. $mode = 'edit';
  206. $event_id = intval($a->argv[2]);
  207. }
  208. if ($a->argc > 2 && $a->argv[1] == 'drop') {
  209. $mode = 'drop';
  210. $event_id = intval($a->argv[2]);
  211. }
  212. if ($a->argc > 2 && $a->argv[1] == 'copy') {
  213. $mode = 'copy';
  214. $event_id = intval($a->argv[2]);
  215. }
  216. if ($a->argv[1] === 'new') {
  217. $mode = 'new';
  218. $event_id = 0;
  219. }
  220. if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
  221. $mode = 'view';
  222. $y = intval($a->argv[1]);
  223. $m = intval($a->argv[2]);
  224. }
  225. }
  226. // The view mode part is similiar to /mod/cal.php
  227. if ($mode == 'view') {
  228. $thisyear = DateTimeFormat::localNow('Y');
  229. $thismonth = DateTimeFormat::localNow('m');
  230. if (!$y) {
  231. $y = intval($thisyear);
  232. }
  233. if (!$m) {
  234. $m = intval($thismonth);
  235. }
  236. // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
  237. // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
  238. if ($y < 1901) {
  239. $y = 1900;
  240. }
  241. if ($y > 2099) {
  242. $y = 2100;
  243. }
  244. $nextyear = $y;
  245. $nextmonth = $m + 1;
  246. if ($nextmonth > 12) {
  247. $nextmonth = 1;
  248. $nextyear ++;
  249. }
  250. $prevyear = $y;
  251. if ($m > 1) {
  252. $prevmonth = $m - 1;
  253. } else {
  254. $prevmonth = 12;
  255. $prevyear --;
  256. }
  257. $dim = Temporal::getDaysInMonth($y, $m);
  258. $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
  259. $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
  260. if ($a->argc > 1 && $a->argv[1] === 'json') {
  261. if (!empty($_GET['start'])) {
  262. $start = $_GET['start'];
  263. }
  264. if (!empty($_GET['end'])) {
  265. $finish = $_GET['end'];
  266. }
  267. }
  268. $start = DateTimeFormat::utc($start);
  269. $finish = DateTimeFormat::utc($finish);
  270. $adjust_start = DateTimeFormat::local($start);
  271. $adjust_finish = DateTimeFormat::local($finish);
  272. // put the event parametes in an array so we can better transmit them
  273. $event_params = [
  274. 'event_id' => intval(defaults($_GET, 'id', 0)),
  275. 'start' => $start,
  276. 'finish' => $finish,
  277. 'adjust_start' => $adjust_start,
  278. 'adjust_finish' => $adjust_finish,
  279. 'ignore' => $ignored,
  280. ];
  281. // get events by id or by date
  282. if ($event_params['event_id']) {
  283. $r = Event::getListById(local_user(), $event_params['event_id']);
  284. } else {
  285. $r = Event::getListByDate(local_user(), $event_params);
  286. }
  287. $links = [];
  288. if (DBA::isResult($r)) {
  289. $r = Event::sortByDate($r);
  290. foreach ($r as $rr) {
  291. $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
  292. if (empty($links[$j])) {
  293. $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
  294. }
  295. }
  296. }
  297. $events = [];
  298. // transform the event in a usable array
  299. if (DBA::isResult($r)) {
  300. $r = Event::sortByDate($r);
  301. $events = Event::prepareListForTemplate($r);
  302. }
  303. if ($a->argc > 1 && $a->argv[1] === 'json') {
  304. echo json_encode($events);
  305. killme();
  306. }
  307. if (!empty($_GET['id'])) {
  308. $tpl = Renderer::getMarkupTemplate("event.tpl");
  309. } else {
  310. $tpl = Renderer::getMarkupTemplate("events_js.tpl");
  311. }
  312. // Get rid of dashes in key names, Smarty3 can't handle them
  313. foreach ($events as $key => $event) {
  314. $event_item = [];
  315. foreach ($event['item'] as $k => $v) {
  316. $k = str_replace('-', '_', $k);
  317. $event_item[$k] = $v;
  318. }
  319. $events[$key]['item'] = $event_item;
  320. }
  321. $o = Renderer::replaceMacros($tpl, [
  322. '$baseurl' => System::baseUrl(),
  323. '$tabs' => $tabs,
  324. '$title' => L10n::t('Events'),
  325. '$view' => L10n::t('View'),
  326. '$new_event' => [System::baseUrl() . '/events/new', L10n::t('Create New Event'), '', ''],
  327. '$previous' => [System::baseUrl() . '/events/$prevyear/$prevmonth', L10n::t('Previous'), '', ''],
  328. '$next' => [System::baseUrl() . '/events/$nextyear/$nextmonth', L10n::t('Next'), '', ''],
  329. '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
  330. '$events' => $events,
  331. '$today' => L10n::t('today'),
  332. '$month' => L10n::t('month'),
  333. '$week' => L10n::t('week'),
  334. '$day' => L10n::t('day'),
  335. '$list' => L10n::t('list'),
  336. ]);
  337. if (!empty($_GET['id'])) {
  338. echo $o;
  339. killme();
  340. }
  341. return $o;
  342. }
  343. if (($mode === 'edit' || $mode === 'copy') && $event_id) {
  344. $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  345. intval($event_id),
  346. intval(local_user())
  347. );
  348. if (DBA::isResult($r)) {
  349. $orig_event = $r[0];
  350. }
  351. }
  352. // Passed parameters overrides anything found in the DB
  353. if (in_array($mode, ['edit', 'new', 'copy'])) {
  354. if (empty($orig_event)) {
  355. $orig_event = [];
  356. }
  357. // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
  358. if (!empty($_REQUEST['nofinish'])) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
  359. if (!empty($_REQUEST['adjust'])) {$orig_event['adjust'] = $_REQUEST['adjust'];}
  360. if (!empty($_REQUEST['summary'])) {$orig_event['summary'] = $_REQUEST['summary'];}
  361. if (!empty($_REQUEST['description'])) {$orig_event['description'] = $_REQUEST['description'];}
  362. if (!empty($_REQUEST['location'])) {$orig_event['location'] = $_REQUEST['location'];}
  363. if (!empty($_REQUEST['start'])) {$orig_event['start'] = $_REQUEST['start'];}
  364. if (!empty($_REQUEST['finish'])) {$orig_event['finish'] = $_REQUEST['finish'];}
  365. $n_checked = (!empty($orig_event['nofinish']) ? ' checked="checked" ' : '');
  366. $a_checked = (!empty($orig_event['adjust']) ? ' checked="checked" ' : '');
  367. $t_orig = !empty($orig_event) ? $orig_event['summary'] : '';
  368. $d_orig = !empty($orig_event) ? $orig_event['desc'] : '';
  369. $l_orig = !empty($orig_event) ? $orig_event['location'] : '';
  370. $eid = !empty($orig_event) ? $orig_event['id'] : 0;
  371. $cid = !empty($orig_event) ? $orig_event['cid'] : 0;
  372. $uri = !empty($orig_event) ? $orig_event['uri'] : '';
  373. $sh_disabled = '';
  374. $sh_checked = '';
  375. if (!empty($orig_event)
  376. && ($orig_event['allow_cid'] !== '<' . local_user() . '>'
  377. || $orig_event['allow_gid']
  378. || $orig_event['deny_cid']
  379. || $orig_event['deny_gid']))
  380. {
  381. $sh_checked = ' checked="checked" ';
  382. }
  383. if ($cid || $mode === 'edit') {
  384. $sh_disabled = 'disabled="disabled"';
  385. }
  386. $sdt = !empty($orig_event) ? $orig_event['start'] : 'now';
  387. $fdt = !empty($orig_event) ? $orig_event['finish'] : 'now';
  388. $tz = date_default_timezone_get();
  389. if (!empty($orig_event)) {
  390. $tz = ($orig_event['adjust'] ? date_default_timezone_get() : 'UTC');
  391. }
  392. $syear = DateTimeFormat::convert($sdt, $tz, 'UTC', 'Y');
  393. $smonth = DateTimeFormat::convert($sdt, $tz, 'UTC', 'm');
  394. $sday = DateTimeFormat::convert($sdt, $tz, 'UTC', 'd');
  395. $shour = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'H') : '00';
  396. $sminute = !empty($orig_event) ? DateTimeFormat::convert($sdt, $tz, 'UTC', 'i') : '00';
  397. $fyear = DateTimeFormat::convert($fdt, $tz, 'UTC', 'Y');
  398. $fmonth = DateTimeFormat::convert($fdt, $tz, 'UTC', 'm');
  399. $fday = DateTimeFormat::convert($fdt, $tz, 'UTC', 'd');
  400. $fhour = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'H') : '00';
  401. $fminute = !empty($orig_event) ? DateTimeFormat::convert($fdt, $tz, 'UTC', 'i') : '00';
  402. $perms = ACL::getDefaultUserPermissions($orig_event);
  403. if (!$cid && in_array($mode, ['new', 'copy'])) {
  404. $acl = ACL::getFullSelectorHTML($a->user, false, $orig_event);
  405. } else {
  406. $acl = '';
  407. }
  408. // If we copy an old event, we need to remove the ID and URI
  409. // from the original event.
  410. if ($mode === 'copy') {
  411. $eid = 0;
  412. $uri = '';
  413. }
  414. $tpl = Renderer::getMarkupTemplate('event_form.tpl');
  415. $o .= Renderer::replaceMacros($tpl, [
  416. '$post' => System::baseUrl() . '/events',
  417. '$eid' => $eid,
  418. '$cid' => $cid,
  419. '$uri' => $uri,
  420. '$allow_cid' => json_encode($perms['allow_cid']),
  421. '$allow_gid' => json_encode($perms['allow_gid']),
  422. '$deny_cid' => json_encode($perms['deny_cid']),
  423. '$deny_gid' => json_encode($perms['deny_gid']),
  424. '$title' => L10n::t('Event details'),
  425. '$desc' => L10n::t('Starting date and Title are required.'),
  426. '$s_text' => L10n::t('Event Starts:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
  427. '$s_dsel' => Temporal::getDateTimeField(
  428. new DateTime(),
  429. DateTime::createFromFormat('Y', $syear+5),
  430. DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"),
  431. L10n::t('Event Starts:'),
  432. 'start_text',
  433. true,
  434. true,
  435. '',
  436. '',
  437. true
  438. ),
  439. '$n_text' => L10n::t('Finish date/time is not known or not relevant'),
  440. '$n_checked' => $n_checked,
  441. '$f_text' => L10n::t('Event Finishes:'),
  442. '$f_dsel' => Temporal::getDateTimeField(
  443. new DateTime(),
  444. DateTime::createFromFormat('Y', $fyear+5),
  445. DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"),
  446. L10n::t('Event Finishes:'),
  447. 'finish_text',
  448. true,
  449. true,
  450. 'start_text'
  451. ),
  452. '$a_text' => L10n::t('Adjust for viewer timezone'),
  453. '$a_checked' => $a_checked,
  454. '$d_text' => L10n::t('Description:'),
  455. '$d_orig' => $d_orig,
  456. '$l_text' => L10n::t('Location:'),
  457. '$l_orig' => $l_orig,
  458. '$t_text' => L10n::t('Title:') . ' <span class="required" title="' . L10n::t('Required') . '">*</span>',
  459. '$t_orig' => $t_orig,
  460. '$summary' => ['summary', L10n::t('Title:'), $t_orig, '', '*'],
  461. '$sh_text' => L10n::t('Share this event'),
  462. '$share' => ['share', L10n::t('Share this event'), $sh_checked, '', $sh_disabled],
  463. '$sh_checked' => $sh_checked,
  464. '$nofinish' => ['nofinish', L10n::t('Finish date/time is not known or not relevant'), $n_checked],
  465. '$adjust' => ['adjust', L10n::t('Adjust for viewer timezone'), $a_checked],
  466. '$preview' => L10n::t('Preview'),
  467. '$acl' => $acl,
  468. '$submit' => L10n::t('Submit'),
  469. '$basic' => L10n::t('Basic'),
  470. '$advanced' => L10n::t('Advanced'),
  471. '$permissions' => L10n::t('Permissions'),
  472. ]);
  473. return $o;
  474. }
  475. // Remove an event from the calendar and its related items
  476. if ($mode === 'drop' && $event_id) {
  477. $ev = Event::getListById(local_user(), $event_id);
  478. // Delete only real events (no birthdays)
  479. if (DBA::isResult($ev) && $ev[0]['type'] == 'event') {
  480. Item::deleteForUser(['id' => $ev[0]['itemid']], local_user());
  481. }
  482. if (Item::exists(['id' => $ev[0]['itemid']])) {
  483. notice(L10n::t('Failed to remove event') . EOL);
  484. } else {
  485. info(L10n::t('Event removed') . EOL);
  486. }
  487. $a->internalRedirect('events');
  488. }
  489. }