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\Content\Nav;
11 use Friendica\Core\Config;
12 use Friendica\Core\System;
13 use Friendica\Database\DBM;
14 use Friendica\Model\Contact;
15 use Friendica\Model\Group;
16 use Friendica\Model\Profile;
17 use Friendica\Protocol\DFRN;
19 require_once 'include/event.php';
21 function cal_init(App $a)
24 DFRN::autoRedir($a, $a->argv[1]);
27 if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
31 Nav::setSelected('events');
35 $user = dba::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
36 if (!DBM::is_result($user)) {
40 $a->data['user'] = $user;
41 $a->profile_uid = $user['uid'];
43 // if it's a json request abort here becaus we don't
44 // need the widget data
45 if ($a->argv[2] === 'json') {
49 $profile = Profile::getByNickname($nick, $a->profile_uid);
51 $account_type = Contact::getAccountType($profile);
53 $tpl = get_markup_template("vcard-widget.tpl");
55 $vcard_widget = replace_macros($tpl, [
56 '$name' => $profile['name'],
57 '$photo' => $profile['photo'],
58 '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
59 '$account_type' => $account_type,
60 '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
63 $cal_widget = widget_events();
65 if (!x($a->page, 'aside')) {
66 $a->page['aside'] = '';
69 $a->page['aside'] .= $vcard_widget;
70 $a->page['aside'] .= $cal_widget;
76 function cal_content(App $a)
78 Nav::setSelected('events');
80 // get the translation strings for the callendar
81 $i18n = get_event_strings();
83 $htpl = get_markup_template('event_head.tpl');
84 $a->page['htmlhead'] .= replace_macros($htpl, [
85 '$baseurl' => System::baseUrl(),
86 '$module_url' => '/cal/' . $a->data['user']['nickname'],
91 $etpl = get_markup_template('event_end.tpl');
92 $a->page['end'] .= replace_macros($etpl, [
93 '$baseurl' => System::baseUrl(),
99 $ignored = ((x($_REQUEST, 'ignored')) ? intval($_REQUEST['ignored']) : 0);
102 if ($a->argc == 4 && $a->argv[2] == 'export') {
104 $format = $a->argv[3];
107 // Setup permissions structures
108 $remote_contact = false;
111 $owner_uid = $a->data['user']['uid'];
112 $nick = $a->data['user']['nickname'];
114 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
115 foreach ($_SESSION['remote'] as $v) {
116 if ($v['uid'] == $a->profile['profile_uid']) {
117 $contact_id = $v['cid'];
125 $groups = Group::getIdsByContactId($contact_id);
126 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
128 intval($a->profile['profile_uid'])
130 if (DBM::is_result($r)) {
131 $remote_contact = true;
135 $is_owner = local_user() == $a->profile['profile_uid'];
137 if ($a->profile['hidewall'] && (!$is_owner) && (!$remote_contact)) {
138 notice(t('Access to this profile has been restricted.') . EOL);
142 // get the permissions
143 $sql_perms = item_permissions_sql($owner_uid, $remote_contact, $groups);
144 // we only want to have the events of the profile owner
145 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
147 // get the tab navigation bar
148 $tabs = Profile::getTabs($a, false, $a->data['user']['nickname']);
150 // The view mode part is similiar to /mod/events.php
151 if ($mode == 'view') {
152 $thisyear = datetime_convert('UTC', date_default_timezone_get(), 'now', 'Y');
153 $thismonth = datetime_convert('UTC', date_default_timezone_get(), 'now', 'm');
155 $y = intval($thisyear);
159 $m = intval($thismonth);
162 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
163 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
175 if ($nextmonth > 12) {
188 $dim = get_dim($y, $m);
189 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
190 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
193 if ($a->argv[2] === 'json') {
194 if (x($_GET, 'start')) {
195 $start = $_GET['start'];
198 if (x($_GET, 'end')) {
199 $finish = $_GET['end'];
203 $start = datetime_convert('UTC', 'UTC', $start);
204 $finish = datetime_convert('UTC', 'UTC', $finish);
206 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
207 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
209 // put the event parametes in an array so we can better transmit them
211 'event_id' => (x($_GET, 'id') ? $_GET["id"] : 0),
214 'adjust_start' => $adjust_start,
215 'adjust_finish' => $adjust_finish,
216 'ignored' => $ignored,
219 // get events by id or by date
220 if (x($_GET, 'id')) {
221 $r = event_by_id($owner_uid, $event_params, $sql_extra);
223 $r = events_by_date($owner_uid, $event_params, $sql_extra);
228 if (DBM::is_result($r)) {
229 $r = sort_by_date($r);
230 foreach ($r as $rr) {
231 $j = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'j') : datetime_convert('UTC', 'UTC', $rr['start'], 'j'));
232 if (!x($links, $j)) {
233 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
238 // transform the event in a usable array
239 $events = process_events($r);
241 if ($a->argv[2] === 'json') {
242 echo json_encode($events);
246 // links: array('href', 'text', 'extra css classes', 'title')
247 if (x($_GET, 'id')) {
248 $tpl = get_markup_template("event.tpl");
250 // if (Config::get('experimentals','new_calendar')==1){
251 $tpl = get_markup_template("events_js.tpl");
253 // $tpl = get_markup_template("events.tpl");
257 // Get rid of dashes in key names, Smarty3 can't handle them
258 foreach ($events as $key => $event) {
260 foreach ($event['item'] as $k => $v) {
261 $k = str_replace('-', '_', $k);
262 $event_item[$k] = $v;
264 $events[$key]['item'] = $event_item;
267 $o = replace_macros($tpl, [
268 '$baseurl' => System::baseUrl(),
270 '$title' => t('Events'),
271 '$view' => t('View'),
272 '$previous' => [System::baseUrl() . "/events/$prevyear/$prevmonth", t('Previous'), '', ''],
273 '$next' => [System::baseUrl() . "/events/$nextyear/$nextmonth", t('Next'), '', ''],
274 '$calendar' => cal($y, $m, $links, ' eventcal'),
275 '$events' => $events,
276 "today" => t("today"),
277 "month" => t("month"),
283 if (x($_GET, 'id')) {
291 if ($mode == 'export') {
292 if (!(intval($owner_uid))) {
293 notice(t('User not found'));
298 // Respect the export feature setting for all other /cal pages if it's not the own profile
299 if (((local_user() !== intval($owner_uid))) && !Feature::isEnabled($owner_uid, "export_calendar")) {
300 notice(t('Permission denied.') . EOL);
301 goaway('cal/' . $nick);
304 // Get the export data by uid
305 $evexport = event_export($owner_uid, $format);
307 if (!$evexport["success"]) {
308 if ($evexport["content"]) {
309 notice(t('This calendar format is not supported'));
311 notice(t('No exportable data found'));
314 // If it the own calendar return to the events page
315 // otherwise to the profile calendar page
316 if (local_user() === intval($owner_uid)) {
317 $return_path = "events";
319 $return_path = "cal/" . $nick;
322 goaway($return_path);
325 // If nothing went wrong we can echo the export content
326 if ($evexport["success"]) {
327 header('Content-type: text/calendar');
328 header('content-disposition: attachment; filename="' . t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
329 echo $evexport["content"];