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