]> git.mxchange.org Git - friendica.git/blob - mod/cal.php
Make frio more consistent by replacing textual links with icons everywhere. (#5415)
[friendica.git] / mod / cal.php
1 <?php
2 /**
3  * @file mod/cal.php
4  * @brief The calendar module
5  *      This calendar is for profile visitors and contains only the events
6  *      of the profile owner
7  */
8
9 use Friendica\App;
10 use Friendica\Content\Feature;
11 use Friendica\Content\Nav;
12 use Friendica\Content\Widget;
13 use Friendica\Core\Config;
14 use Friendica\Core\L10n;
15 use Friendica\Core\System;
16 use Friendica\Database\DBA;
17 use Friendica\Database\DBM;
18 use Friendica\Model\Contact;
19 use Friendica\Model\Event;
20 use Friendica\Model\Group;
21 use Friendica\Model\Profile;
22 use Friendica\Protocol\DFRN;
23 use Friendica\Util\DateTimeFormat;
24 use Friendica\Util\Temporal;
25
26 function cal_init(App $a)
27 {
28         if ($a->argc > 1) {
29                 DFRN::autoRedir($a, $a->argv[1]);
30         }
31
32         if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
33                 return;
34         }
35
36         Nav::setSelected('events');
37
38         if ($a->argc > 1) {
39                 $nick = $a->argv[1];
40                 $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
41                 if (!DBM::is_result($user)) {
42                         return;
43                 }
44
45                 $a->data['user'] = $user;
46                 $a->profile_uid = $user['uid'];
47
48                 // if it's a json request abort here becaus we don't
49                 // need the widget data
50                 if ($a->argv[2] === 'json') {
51                         return;
52                 }
53
54                 $profile = Profile::getByNickname($nick, $a->profile_uid);
55
56                 $account_type = Contact::getAccountType($profile);
57
58                 $tpl = get_markup_template("vcard-widget.tpl");
59
60                 $vcard_widget = replace_macros($tpl, [
61                         '$name' => $profile['name'],
62                         '$photo' => $profile['photo'],
63                         '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
64                         '$account_type' => $account_type,
65                         '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
66                 ]);
67
68                 $cal_widget = Widget\CalendarExport::getHTML();
69
70                 if (!x($a->page, 'aside')) {
71                         $a->page['aside'] = '';
72                 }
73
74                 $a->page['aside'] .= $vcard_widget;
75                 $a->page['aside'] .= $cal_widget;
76         }
77
78         return;
79 }
80
81 function cal_content(App $a)
82 {
83         Nav::setSelected('events');
84
85         // get the translation strings for the callendar
86         $i18n = Event::getStrings();
87
88         $htpl = get_markup_template('event_head.tpl');
89         $a->page['htmlhead'] .= replace_macros($htpl, [
90                 '$baseurl' => System::baseUrl(),
91                 '$module_url' => '/cal/' . $a->data['user']['nickname'],
92                 '$modparams' => 2,
93                 '$i18n' => $i18n,
94         ]);
95
96         $etpl = get_markup_template('event_end.tpl');
97         $a->page['end'] .= replace_macros($etpl, [
98                 '$baseurl' => System::baseUrl(),
99         ]);
100
101         $mode = 'view';
102         $y = 0;
103         $m = 0;
104         $ignored = ((x($_REQUEST, 'ignored')) ? intval($_REQUEST['ignored']) : 0);
105
106         $format = 'ical';
107         if ($a->argc == 4 && $a->argv[2] == 'export') {
108                 $mode = 'export';
109                 $format = $a->argv[3];
110         }
111
112         // Setup permissions structures
113         $remote_contact = false;
114         $contact_id = 0;
115
116         $owner_uid = $a->data['user']['uid'];
117         $nick = $a->data['user']['nickname'];
118
119         if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
120                 foreach ($_SESSION['remote'] as $v) {
121                         if ($v['uid'] == $a->profile['profile_uid']) {
122                                 $contact_id = $v['cid'];
123                                 break;
124                         }
125                 }
126         }
127
128         $groups = [];
129         if ($contact_id) {
130                 $groups = Group::getIdsByContactId($contact_id);
131                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
132                         intval($contact_id),
133                         intval($a->profile['profile_uid'])
134                 );
135                 if (DBM::is_result($r)) {
136                         $remote_contact = true;
137                 }
138         }
139
140         $is_owner = local_user() == $a->profile['profile_uid'];
141
142         if ($a->profile['hidewall'] && (!$is_owner) && (!$remote_contact)) {
143                 notice(L10n::t('Access to this profile has been restricted.') . EOL);
144                 return;
145         }
146
147         // get the permissions
148         $sql_perms = item_permissions_sql($owner_uid, $remote_contact, $groups);
149         // we only want to have the events of the profile owner
150         $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
151
152         // get the tab navigation bar
153         $tabs = Profile::getTabs($a, false, $a->data['user']['nickname']);
154
155         // The view mode part is similiar to /mod/events.php
156         if ($mode == 'view') {
157                 $thisyear = DateTimeFormat::localNow('Y');
158                 $thismonth = DateTimeFormat::localNow('m');
159                 if (!$y) {
160                         $y = intval($thisyear);
161                 }
162
163                 if (!$m) {
164                         $m = intval($thismonth);
165                 }
166
167                 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
168                 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
169
170                 if ($y < 1901) {
171                         $y = 1900;
172                 }
173
174                 if ($y > 2099) {
175                         $y = 2100;
176                 }
177
178                 $nextyear = $y;
179                 $nextmonth = $m + 1;
180                 if ($nextmonth > 12) {
181                         $nextmonth = 1;
182                         $nextyear ++;
183                 }
184
185                 $prevyear = $y;
186                 if ($m > 1) {
187                         $prevmonth = $m - 1;
188                 } else {
189                         $prevmonth = 12;
190                         $prevyear --;
191                 }
192
193                 $dim = Temporal::getDaysInMonth($y, $m);
194                 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
195                 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
196
197
198                 if ($a->argv[2] === 'json') {
199                         if (x($_GET, 'start')) {
200                                 $start = $_GET['start'];
201                         }
202
203                         if (x($_GET, 'end')) {
204                                 $finish = $_GET['end'];
205                         }
206                 }
207
208                 $start = DateTimeFormat::utc($start);
209                 $finish = DateTimeFormat::utc($finish);
210
211                 $adjust_start = DateTimeFormat::local($start);
212                 $adjust_finish = DateTimeFormat::local($finish);
213
214                 // put the event parametes in an array so we can better transmit them
215                 $event_params = [
216                         'event_id'      => intval(defaults($_GET, 'id', 0)),
217                         'start'         => $start,
218                         'finish'        => $finish,
219                         'adjust_start'  => $adjust_start,
220                         'adjust_finish' => $adjust_finish,
221                         'ignore'        => $ignored,
222                 ];
223
224                 // get events by id or by date
225                 if ($event_params['event_id']) {
226                         $r = Event::getListById($owner_uid, $event_params['event-id'], $sql_extra);
227                 } else {
228                         $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
229                 }
230
231                 $links = [];
232
233                 if (DBM::is_result($r)) {
234                         $r = Event::sortByDate($r);
235                         foreach ($r as $rr) {
236                                 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
237                                 if (!x($links, $j)) {
238                                         $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
239                                 }
240                         }
241                 }
242
243                 // transform the event in a usable array
244                 $events = Event::prepareListForTemplate($r);
245
246                 if ($a->argv[2] === 'json') {
247                         echo json_encode($events);
248                         killme();
249                 }
250
251                 // links: array('href', 'text', 'extra css classes', 'title')
252                 if (x($_GET, 'id')) {
253                         $tpl = get_markup_template("event.tpl");
254                 } else {
255 //                      if (Config::get('experimentals','new_calendar')==1){
256                         $tpl = get_markup_template("events_js.tpl");
257 //                      } else {
258 //                              $tpl = get_markup_template("events.tpl");
259 //                      }
260                 }
261
262                 // Get rid of dashes in key names, Smarty3 can't handle them
263                 foreach ($events as $key => $event) {
264                         $event_item = [];
265                         foreach ($event['item'] as $k => $v) {
266                                 $k = str_replace('-', '_', $k);
267                                 $event_item[$k] = $v;
268                         }
269                         $events[$key]['item'] = $event_item;
270                 }
271
272                 $o = replace_macros($tpl, [
273                         '$baseurl' => System::baseUrl(),
274                         '$tabs' => $tabs,
275                         '$title' => L10n::t('Events'),
276                         '$view' => L10n::t('View'),
277                         '$previous' => [System::baseUrl() . "/events/$prevyear/$prevmonth", L10n::t('Previous'), '', ''],
278                         '$next' => [System::baseUrl() . "/events/$nextyear/$nextmonth", L10n::t('Next'), '', ''],
279                         '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
280                         '$events' => $events,
281                         "today" => L10n::t("today"),
282                         "month" => L10n::t("month"),
283                         "week" => L10n::t("week"),
284                         "day" => L10n::t("day"),
285                         "list" => L10n::t("list"),
286                 ]);
287
288                 if (x($_GET, 'id')) {
289                         echo $o;
290                         killme();
291                 }
292
293                 return $o;
294         }
295
296         if ($mode == 'export') {
297                 if (!(intval($owner_uid))) {
298                         notice(L10n::t('User not found'));
299                         return;
300                 }
301
302                 // Test permissions
303                 // Respect the export feature setting for all other /cal pages if it's not the own profile
304                 if (((local_user() !== intval($owner_uid))) && !Feature::isEnabled($owner_uid, "export_calendar")) {
305                         notice(L10n::t('Permission denied.') . EOL);
306                         goaway('cal/' . $nick);
307                 }
308
309                 // Get the export data by uid
310                 $evexport = Event::exportListByUserId($owner_uid, $format);
311
312                 if (!$evexport["success"]) {
313                         if ($evexport["content"]) {
314                                 notice(L10n::t('This calendar format is not supported'));
315                         } else {
316                                 notice(L10n::t('No exportable data found'));
317                         }
318
319                         // If it the own calendar return to the events page
320                         // otherwise to the profile calendar page
321                         if (local_user() === intval($owner_uid)) {
322                                 $return_path = "events";
323                         } else {
324                                 $return_path = "cal/" . $nick;
325                         }
326
327                         goaway($return_path);
328                 }
329
330                 // If nothing went wrong we can echo the export content
331                 if ($evexport["success"]) {
332                         header('Content-type: text/calendar');
333                         header('content-disposition: attachment; filename="' . L10n::t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
334                         echo $evexport["content"];
335                         killme();
336                 }
337
338                 return;
339         }
340 }