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'],
97 $etpl = get_markup_template('event_end.tpl');
98 $a->page['end'] .= replace_macros($etpl, [
99 '$baseurl' => System::baseUrl(),
105 $ignored = (x($_REQUEST, 'ignored') ? intval($_REQUEST['ignored']) : 0);
108 if ($a->argc == 4 && $a->argv[2] == 'export') {
110 $format = $a->argv[3];
113 // Setup permissions structures
114 $remote_contact = false;
117 $owner_uid = $a->data['user']['uid'];
118 $nick = $a->data['user']['nickname'];
120 if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
121 foreach ($_SESSION['remote'] as $v) {
122 if ($v['uid'] == $a->profile['profile_uid']) {
123 $contact_id = $v['cid'];
131 $groups = Group::getIdsByContactId($contact_id);
132 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
134 intval($a->profile['profile_uid'])
136 if (DBA::isResult($r)) {
137 $remote_contact = true;
141 $is_owner = local_user() == $a->profile['profile_uid'];
143 if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
144 notice(L10n::t('Access to this profile has been restricted.') . EOL);
148 // get the permissions
149 $sql_perms = item_permissions_sql($owner_uid, $remote_contact, $groups);
150 // we only want to have the events of the profile owner
151 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
153 // get the tab navigation bar
154 $tabs = Profile::getTabs($a, false, $a->data['user']['nickname']);
156 // The view mode part is similiar to /mod/events.php
157 if ($mode == 'view') {
158 $thisyear = DateTimeFormat::localNow('Y');
159 $thismonth = DateTimeFormat::localNow('m');
161 $y = intval($thisyear);
165 $m = intval($thismonth);
168 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
169 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
181 if ($nextmonth > 12) {
194 $dim = Temporal::getDaysInMonth($y, $m);
195 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
196 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
199 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
200 if (x($_GET, 'start')) {
201 $start = $_GET['start'];
204 if (x($_GET, 'end')) {
205 $finish = $_GET['end'];
209 $start = DateTimeFormat::utc($start);
210 $finish = DateTimeFormat::utc($finish);
212 $adjust_start = DateTimeFormat::local($start);
213 $adjust_finish = DateTimeFormat::local($finish);
215 // put the event parametes in an array so we can better transmit them
217 'event_id' => intval(defaults($_GET, 'id', 0)),
220 'adjust_start' => $adjust_start,
221 'adjust_finish' => $adjust_finish,
222 'ignore' => $ignored,
225 // get events by id or by date
226 if ($event_params['event_id']) {
227 $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
229 $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
234 if (DBA::isResult($r)) {
235 $r = Event::sortByDate($r);
236 foreach ($r as $rr) {
237 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
238 if (!x($links, $j)) {
239 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
244 // transform the event in a usable array
245 $events = Event::prepareListForTemplate($r);
247 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
248 echo json_encode($events);
252 // links: array('href', 'text', 'extra css classes', 'title')
253 if (x($_GET, 'id')) {
254 $tpl = get_markup_template("event.tpl");
256 // if (Config::get('experimentals','new_calendar')==1){
257 $tpl = get_markup_template("events_js.tpl");
259 // $tpl = get_markup_template("events.tpl");
263 // Get rid of dashes in key names, Smarty3 can't handle them
264 foreach ($events as $key => $event) {
266 foreach ($event['item'] as $k => $v) {
267 $k = str_replace('-', '_', $k);
268 $event_item[$k] = $v;
270 $events[$key]['item'] = $event_item;
273 $o = replace_macros($tpl, [
274 '$baseurl' => System::baseUrl(),
276 '$title' => L10n::t('Events'),
277 '$view' => L10n::t('View'),
278 '$previous' => [System::baseUrl() . "/events/$prevyear/$prevmonth", L10n::t('Previous'), '', ''],
279 '$next' => [System::baseUrl() . "/events/$nextyear/$nextmonth", L10n::t('Next'), '', ''],
280 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
281 '$events' => $events,
282 "today" => L10n::t("today"),
283 "month" => L10n::t("month"),
284 "week" => L10n::t("week"),
285 "day" => L10n::t("day"),
286 "list" => L10n::t("list"),
289 if (x($_GET, 'id')) {
297 if ($mode == 'export') {
298 if (!intval($owner_uid)) {
299 notice(L10n::t('User not found'));
304 // Respect the export feature setting for all other /cal pages if it's not the own profile
305 if ((local_user() !== intval($owner_uid)) && !Feature::isEnabled($owner_uid, "export_calendar")) {
306 notice(L10n::t('Permission denied.') . EOL);
307 goaway('cal/' . $nick);
310 // Get the export data by uid
311 $evexport = Event::exportListByUserId($owner_uid, $format);
313 if (!$evexport["success"]) {
314 if ($evexport["content"]) {
315 notice(L10n::t('This calendar format is not supported'));
317 notice(L10n::t('No exportable data found'));
320 // If it the own calendar return to the events page
321 // otherwise to the profile calendar page
322 if (local_user() === intval($owner_uid)) {
323 $return_path = "events";
325 $return_path = "cal/" . $nick;
328 goaway($return_path);
331 // If nothing went wrong we can echo the export content
332 if ($evexport["success"]) {
333 header('Content-type: text/calendar');
334 header('content-disposition: attachment; filename="' . L10n::t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
335 echo $evexport["content"];