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\Core\Config;
13 use Friendica\Core\L10n;
14 use Friendica\Core\System;
15 use Friendica\Database\DBM;
16 use Friendica\Model\Contact;
17 use Friendica\Model\Group;
18 use Friendica\Model\Profile;
19 use Friendica\Protocol\DFRN;
20 use Friendica\Util\Temporal;
22 require_once 'include/event.php';
24 function cal_init(App $a)
27 DFRN::autoRedir($a, $a->argv[1]);
30 if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
34 Nav::setSelected('events');
38 $user = dba::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
39 if (!DBM::is_result($user)) {
43 $a->data['user'] = $user;
44 $a->profile_uid = $user['uid'];
46 // if it's a json request abort here becaus we don't
47 // need the widget data
48 if ($a->argv[2] === 'json') {
52 $profile = Profile::getByNickname($nick, $a->profile_uid);
54 $account_type = Contact::getAccountType($profile);
56 $tpl = get_markup_template("vcard-widget.tpl");
58 $vcard_widget = replace_macros($tpl, [
59 '$name' => $profile['name'],
60 '$photo' => $profile['photo'],
61 '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
62 '$account_type' => $account_type,
63 '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
66 $cal_widget = widget_events();
68 if (!x($a->page, 'aside')) {
69 $a->page['aside'] = '';
72 $a->page['aside'] .= $vcard_widget;
73 $a->page['aside'] .= $cal_widget;
79 function cal_content(App $a)
81 Nav::setSelected('events');
83 // get the translation strings for the callendar
84 $i18n = get_event_strings();
86 $htpl = get_markup_template('event_head.tpl');
87 $a->page['htmlhead'] .= replace_macros($htpl, [
88 '$baseurl' => System::baseUrl(),
89 '$module_url' => '/cal/' . $a->data['user']['nickname'],
94 $etpl = get_markup_template('event_end.tpl');
95 $a->page['end'] .= replace_macros($etpl, [
96 '$baseurl' => System::baseUrl(),
102 $ignored = ((x($_REQUEST, 'ignored')) ? intval($_REQUEST['ignored']) : 0);
105 if ($a->argc == 4 && $a->argv[2] == 'export') {
107 $format = $a->argv[3];
110 // Setup permissions structures
111 $remote_contact = false;
114 $owner_uid = $a->data['user']['uid'];
115 $nick = $a->data['user']['nickname'];
117 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
118 foreach ($_SESSION['remote'] as $v) {
119 if ($v['uid'] == $a->profile['profile_uid']) {
120 $contact_id = $v['cid'];
128 $groups = Group::getIdsByContactId($contact_id);
129 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
131 intval($a->profile['profile_uid'])
133 if (DBM::is_result($r)) {
134 $remote_contact = true;
138 $is_owner = local_user() == $a->profile['profile_uid'];
140 if ($a->profile['hidewall'] && (!$is_owner) && (!$remote_contact)) {
141 notice(L10n::t('Access to this profile has been restricted.') . EOL);
145 // get the permissions
146 $sql_perms = item_permissions_sql($owner_uid, $remote_contact, $groups);
147 // we only want to have the events of the profile owner
148 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
150 // get the tab navigation bar
151 $tabs = Profile::getTabs($a, false, $a->data['user']['nickname']);
153 // The view mode part is similiar to /mod/events.php
154 if ($mode == 'view') {
155 $thisyear = Temporal::localNow('Y');
156 $thismonth = Temporal::localNow('m');
158 $y = intval($thisyear);
162 $m = intval($thismonth);
165 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
166 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
178 if ($nextmonth > 12) {
191 $dim = get_dim($y, $m);
192 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
193 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
196 if ($a->argv[2] === 'json') {
197 if (x($_GET, 'start')) {
198 $start = $_GET['start'];
201 if (x($_GET, 'end')) {
202 $finish = $_GET['end'];
206 $start = Temporal::utc($start);
207 $finish = Temporal::utc($finish);
209 $adjust_start = Temporal::local($start);
210 $adjust_finish = Temporal::local($finish);
212 // put the event parametes in an array so we can better transmit them
214 'event_id' => (x($_GET, 'id') ? $_GET["id"] : 0),
217 'adjust_start' => $adjust_start,
218 'adjust_finish' => $adjust_finish,
219 'ignored' => $ignored,
222 // get events by id or by date
223 if (x($_GET, 'id')) {
224 $r = event_by_id($owner_uid, $event_params, $sql_extra);
226 $r = events_by_date($owner_uid, $event_params, $sql_extra);
231 if (DBM::is_result($r)) {
232 $r = sort_by_date($r);
233 foreach ($r as $rr) {
234 $j = $rr['adjust'] ? Temporal::local($rr['start'], 'j') : Temporal::utc($rr['start'], 'j');
235 if (!x($links, $j)) {
236 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
241 // transform the event in a usable array
242 $events = process_events($r);
244 if ($a->argv[2] === 'json') {
245 echo json_encode($events);
249 // links: array('href', 'text', 'extra css classes', 'title')
250 if (x($_GET, 'id')) {
251 $tpl = get_markup_template("event.tpl");
253 // if (Config::get('experimentals','new_calendar')==1){
254 $tpl = get_markup_template("events_js.tpl");
256 // $tpl = get_markup_template("events.tpl");
260 // Get rid of dashes in key names, Smarty3 can't handle them
261 foreach ($events as $key => $event) {
263 foreach ($event['item'] as $k => $v) {
264 $k = str_replace('-', '_', $k);
265 $event_item[$k] = $v;
267 $events[$key]['item'] = $event_item;
270 $o = replace_macros($tpl, [
271 '$baseurl' => System::baseUrl(),
273 '$title' => L10n::t('Events'),
274 '$view' => L10n::t('View'),
275 '$previous' => [System::baseUrl() . "/events/$prevyear/$prevmonth", L10n::t('Previous'), '', ''],
276 '$next' => [System::baseUrl() . "/events/$nextyear/$nextmonth", L10n::t('Next'), '', ''],
277 '$calendar' => cal($y, $m, $links, ' eventcal'),
278 '$events' => $events,
279 "today" => L10n::t("today"),
280 "month" => L10n::t("month"),
281 "week" => L10n::t("week"),
282 "day" => L10n::t("day"),
283 "list" => L10n::t("list"),
286 if (x($_GET, 'id')) {
294 if ($mode == 'export') {
295 if (!(intval($owner_uid))) {
296 notice(L10n::t('User not found'));
301 // Respect the export feature setting for all other /cal pages if it's not the own profile
302 if (((local_user() !== intval($owner_uid))) && !Feature::isEnabled($owner_uid, "export_calendar")) {
303 notice(L10n::t('Permission denied.') . EOL);
304 goaway('cal/' . $nick);
307 // Get the export data by uid
308 $evexport = event_export($owner_uid, $format);
310 if (!$evexport["success"]) {
311 if ($evexport["content"]) {
312 notice(L10n::t('This calendar format is not supported'));
314 notice(L10n::t('No exportable data found'));
317 // If it the own calendar return to the events page
318 // otherwise to the profile calendar page
319 if (local_user() === intval($owner_uid)) {
320 $return_path = "events";
322 $return_path = "cal/" . $nick;
325 goaway($return_path);
328 // If nothing went wrong we can echo the export content
329 if ($evexport["success"]) {
330 header('Content-type: text/calendar');
331 header('content-disposition: attachment; filename="' . L10n::t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
332 echo $evexport["content"];