4 * @brief The calendar module
5 * This calendar is for profile visitors and contains only the events
9 use Friendica\Content\Feature;
10 use Friendica\Core\Config;
11 use Friendica\Core\System;
12 use Friendica\Database\DBM;
13 use Friendica\Model\Contact;
14 use Friendica\Model\Group;
15 use Friendica\Model\Profile;
16 use Friendica\Protocol\DFRN;
18 require_once 'include/event.php';
20 function cal_init(App $a)
23 DFRN::autoRedir($a, $a->argv[1]);
26 if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
30 nav_set_selected('events');
34 $user = dba::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
35 if (!DBM::is_result($user)) {
39 $a->data['user'] = $user;
40 $a->profile_uid = $user['uid'];
42 // if it's a json request abort here becaus we don't
43 // need the widget data
44 if ($a->argv[2] === 'json') {
48 $profile = Profile::getByNickname($nick, $a->profile_uid);
50 $account_type = Contact::getAccountType($profile);
52 $tpl = get_markup_template("vcard-widget.tpl");
54 $vcard_widget = replace_macros($tpl, [
55 '$name' => $profile['name'],
56 '$photo' => $profile['photo'],
57 '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
58 '$account_type' => $account_type,
59 '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
62 $cal_widget = widget_events();
64 if (!x($a->page, 'aside')) {
65 $a->page['aside'] = '';
68 $a->page['aside'] .= $vcard_widget;
69 $a->page['aside'] .= $cal_widget;
75 function cal_content(App $a)
77 nav_set_selected('events');
79 // get the translation strings for the callendar
80 $i18n = get_event_strings();
82 $htpl = get_markup_template('event_head.tpl');
83 $a->page['htmlhead'] .= replace_macros($htpl, [
84 '$baseurl' => System::baseUrl(),
85 '$module_url' => '/cal/' . $a->data['user']['nickname'],
90 $etpl = get_markup_template('event_end.tpl');
91 $a->page['end'] .= replace_macros($etpl, [
92 '$baseurl' => System::baseUrl(),
98 $ignored = ((x($_REQUEST, 'ignored')) ? intval($_REQUEST['ignored']) : 0);
101 if ($a->argc == 4 && $a->argv[2] == 'export') {
103 $format = $a->argv[3];
106 // Setup permissions structures
107 $remote_contact = false;
110 $owner_uid = $a->data['user']['uid'];
111 $nick = $a->data['user']['nickname'];
113 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
114 foreach ($_SESSION['remote'] as $v) {
115 if ($v['uid'] == $a->profile['profile_uid']) {
116 $contact_id = $v['cid'];
124 $groups = Group::getIdsByContactId($contact_id);
125 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
127 intval($a->profile['profile_uid'])
129 if (DBM::is_result($r)) {
130 $remote_contact = true;
134 $is_owner = local_user() == $a->profile['profile_uid'];
136 if ($a->profile['hidewall'] && (!$is_owner) && (!$remote_contact)) {
137 notice(t('Access to this profile has been restricted.') . EOL);
141 // get the permissions
142 $sql_perms = item_permissions_sql($owner_uid, $remote_contact, $groups);
143 // we only want to have the events of the profile owner
144 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
146 // get the tab navigation bar
147 $tabs = Profile::getTabs($a, false, $a->data['user']['nickname']);
149 // The view mode part is similiar to /mod/events.php
150 if ($mode == 'view') {
151 $thisyear = datetime_convert('UTC', date_default_timezone_get(), 'now', 'Y');
152 $thismonth = datetime_convert('UTC', date_default_timezone_get(), 'now', 'm');
154 $y = intval($thisyear);
158 $m = intval($thismonth);
161 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
162 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
174 if ($nextmonth > 12) {
187 $dim = get_dim($y, $m);
188 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
189 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
192 if ($a->argv[2] === 'json') {
193 if (x($_GET, 'start')) {
194 $start = $_GET['start'];
197 if (x($_GET, 'end')) {
198 $finish = $_GET['end'];
202 $start = datetime_convert('UTC', 'UTC', $start);
203 $finish = datetime_convert('UTC', 'UTC', $finish);
205 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
206 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
208 // put the event parametes in an array so we can better transmit them
210 'event_id' => (x($_GET, 'id') ? $_GET["id"] : 0),
213 'adjust_start' => $adjust_start,
214 'adjust_finish' => $adjust_finish,
215 'ignored' => $ignored,
218 // get events by id or by date
219 if (x($_GET, 'id')) {
220 $r = event_by_id($owner_uid, $event_params, $sql_extra);
222 $r = events_by_date($owner_uid, $event_params, $sql_extra);
227 if (DBM::is_result($r)) {
228 $r = sort_by_date($r);
229 foreach ($r as $rr) {
230 $j = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'j') : datetime_convert('UTC', 'UTC', $rr['start'], 'j'));
231 if (!x($links, $j)) {
232 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
237 // transform the event in a usable array
238 $events = process_events($r);
240 if ($a->argv[2] === 'json') {
241 echo json_encode($events);
245 // links: array('href', 'text', 'extra css classes', 'title')
246 if (x($_GET, 'id')) {
247 $tpl = get_markup_template("event.tpl");
249 // if (Config::get('experimentals','new_calendar')==1){
250 $tpl = get_markup_template("events_js.tpl");
252 // $tpl = get_markup_template("events.tpl");
256 // Get rid of dashes in key names, Smarty3 can't handle them
257 foreach ($events as $key => $event) {
259 foreach ($event['item'] as $k => $v) {
260 $k = str_replace('-', '_', $k);
261 $event_item[$k] = $v;
263 $events[$key]['item'] = $event_item;
266 $o = replace_macros($tpl, [
267 '$baseurl' => System::baseUrl(),
269 '$title' => t('Events'),
270 '$view' => t('View'),
271 '$previous' => [System::baseUrl() . "/events/$prevyear/$prevmonth", t('Previous'), '', ''],
272 '$next' => [System::baseUrl() . "/events/$nextyear/$nextmonth", t('Next'), '', ''],
273 '$calendar' => cal($y, $m, $links, ' eventcal'),
274 '$events' => $events,
275 "today" => t("today"),
276 "month" => t("month"),
282 if (x($_GET, 'id')) {
290 if ($mode == 'export') {
291 if (!(intval($owner_uid))) {
292 notice(t('User not found'));
297 // Respect the export feature setting for all other /cal pages if it's not the own profile
298 if (((local_user() !== intval($owner_uid))) && !Feature::isEnabled($owner_uid, "export_calendar")) {
299 notice(t('Permission denied.') . EOL);
300 goaway('cal/' . $nick);
303 // Get the export data by uid
304 $evexport = event_export($owner_uid, $format);
306 if (!$evexport["success"]) {
307 if ($evexport["content"]) {
308 notice(t('This calendar format is not supported'));
310 notice(t('No exportable data found'));
313 // If it the own calendar return to the events page
314 // otherwise to the profile calendar page
315 if (local_user() === intval($owner_uid)) {
316 $return_path = "events";
318 $return_path = "cal/" . $nick;
321 goaway($return_path);
324 // If nothing went wrong we can echo the export content
325 if ($evexport["success"]) {
326 header('Content-type: text/calendar');
327 header('content-disposition: attachment; filename="' . t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
328 echo $evexport["content"];