4 * @brief The calendar module
5 * This calendar is for profile visitors and contains only the events
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\Model\Contact;
18 use Friendica\Model\Event;
19 use Friendica\Model\Group;
20 use Friendica\Model\Profile;
21 use Friendica\Protocol\DFRN;
22 use Friendica\Util\DateTimeFormat;
23 use Friendica\Util\Temporal;
25 function cal_init(App $a)
28 DFRN::autoRedir($a, $a->argv[1]);
31 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
32 System::httpExit(403, ['title' => L10n::t('Access denied.')]);
36 System::httpExit(403, ['title' => L10n::t('Access denied.')]);
39 Nav::setSelected('events');
42 $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
43 if (!DBA::isResult($user)) {
44 System::httpExit(404, ['title' => L10n::t('Page not found.')]);
47 $a->data['user'] = $user;
48 $a->profile_uid = $user['uid'];
50 // if it's a json request abort here becaus we don't
51 // need the widget data
52 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
56 $profile = Profile::getByNickname($nick, $a->profile_uid);
58 $account_type = Contact::getAccountType($profile);
60 $tpl = get_markup_template("vcard-widget.tpl");
62 $vcard_widget = replace_macros($tpl, [
63 '$name' => $profile['name'],
64 '$photo' => $profile['photo'],
65 '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
66 '$account_type' => $account_type,
67 '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
70 $cal_widget = Widget\CalendarExport::getHTML();
72 if (!x($a->page, 'aside')) {
73 $a->page['aside'] = '';
76 $a->page['aside'] .= $vcard_widget;
77 $a->page['aside'] .= $cal_widget;
82 function cal_content(App $a)
84 Nav::setSelected('events');
86 // get the translation strings for the callendar
87 $i18n = Event::getStrings();
89 $htpl = get_markup_template('event_head.tpl');
90 $a->page['htmlhead'] .= replace_macros($htpl, [
91 '$baseurl' => System::baseUrl(),
92 '$module_url' => '/cal/' . $a->data['user']['nickname'],
100 $ignored = (x($_REQUEST, 'ignored') ? intval($_REQUEST['ignored']) : 0);
103 if ($a->argc == 4 && $a->argv[2] == 'export') {
105 $format = $a->argv[3];
108 // Setup permissions structures
109 $remote_contact = false;
112 $owner_uid = $a->data['user']['uid'];
113 $nick = $a->data['user']['nickname'];
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'];
126 $groups = Group::getIdsByContactId($contact_id);
127 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
129 intval($a->profile['profile_uid'])
131 if (DBA::isResult($r)) {
132 $remote_contact = true;
136 $is_owner = local_user() == $a->profile['profile_uid'];
138 if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
139 notice(L10n::t('Access to this profile has been restricted.') . EOL);
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;
148 // get the tab navigation bar
149 $tabs = Profile::getTabs($a, false, $a->data['user']['nickname']);
151 // The view mode part is similiar to /mod/events.php
152 if ($mode == 'view') {
153 $thisyear = DateTimeFormat::localNow('Y');
154 $thismonth = DateTimeFormat::localNow('m');
156 $y = intval($thisyear);
160 $m = intval($thismonth);
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.
176 if ($nextmonth > 12) {
189 $dim = Temporal::getDaysInMonth($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);
194 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
195 if (x($_GET, 'start')) {
196 $start = $_GET['start'];
199 if (x($_GET, 'end')) {
200 $finish = $_GET['end'];
204 $start = DateTimeFormat::utc($start);
205 $finish = DateTimeFormat::utc($finish);
207 $adjust_start = DateTimeFormat::local($start);
208 $adjust_finish = DateTimeFormat::local($finish);
210 // put the event parametes in an array so we can better transmit them
212 'event_id' => intval(defaults($_GET, 'id', 0)),
215 'adjust_start' => $adjust_start,
216 'adjust_finish' => $adjust_finish,
217 'ignore' => $ignored,
220 // get events by id or by date
221 if ($event_params['event_id']) {
222 $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
224 $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
229 if (DBA::isResult($r)) {
230 $r = Event::sortByDate($r);
231 foreach ($r as $rr) {
232 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
233 if (!x($links, $j)) {
234 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
239 // transform the event in a usable array
240 $events = Event::prepareListForTemplate($r);
242 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
243 echo json_encode($events);
247 // links: array('href', 'text', 'extra css classes', 'title')
248 if (x($_GET, 'id')) {
249 $tpl = get_markup_template("event.tpl");
251 // if (Config::get('experimentals','new_calendar')==1){
252 $tpl = get_markup_template("events_js.tpl");
254 // $tpl = get_markup_template("events.tpl");
258 // Get rid of dashes in key names, Smarty3 can't handle them
259 foreach ($events as $key => $event) {
261 foreach ($event['item'] as $k => $v) {
262 $k = str_replace('-', '_', $k);
263 $event_item[$k] = $v;
265 $events[$key]['item'] = $event_item;
268 $o = replace_macros($tpl, [
269 '$baseurl' => System::baseUrl(),
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' => Temporal::getCalendarTable($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"),
284 if (x($_GET, 'id')) {
292 if ($mode == 'export') {
293 if (!intval($owner_uid)) {
294 notice(L10n::t('User not found'));
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);
305 // Get the export data by uid
306 $evexport = Event::exportListByUserId($owner_uid, $format);
308 if (!$evexport["success"]) {
309 if ($evexport["content"]) {
310 notice(L10n::t('This calendar format is not supported'));
312 notice(L10n::t('No exportable data found'));
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";
320 $return_path = "cal/" . $nick;
323 goaway($return_path);
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"];