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