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