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\Renderer;
16 use Friendica\Core\System;
17 use Friendica\Core\Session;
18 use Friendica\Database\DBA;
20 use Friendica\Model\Contact;
21 use Friendica\Model\Event;
22 use Friendica\Model\Item;
23 use Friendica\Model\Profile;
24 use Friendica\Util\DateTimeFormat;
25 use Friendica\Util\Temporal;
27 function cal_init(App $a)
29 if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
30 throw new \Friendica\Network\HTTPException\ForbiddenException(L10n::t('Access denied.'));
34 throw new \Friendica\Network\HTTPException\ForbiddenException(L10n::t('Access denied.'));
37 Nav::setSelected('events');
40 $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
41 if (!DBA::isResult($user)) {
42 throw new \Friendica\Network\HTTPException\NotFoundException();
45 $a->data['user'] = $user;
46 $a->profile_uid = $user['uid'];
48 // if it's a json request abort here becaus we don't
49 // need the widget data
50 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
54 $profile = Profile::getByNickname($nick, $a->profile_uid);
56 $account_type = Contact::getAccountType($profile);
58 $tpl = Renderer::getMarkupTemplate("widget/vcard.tpl");
60 $vcard_widget = Renderer::replaceMacros($tpl, [
61 '$name' => $profile['name'],
62 '$photo' => $profile['photo'],
63 '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
64 '$account_type' => $account_type,
65 '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
68 $cal_widget = Widget\CalendarExport::getHTML();
70 if (empty(DI::page()['aside'])) {
71 DI::page()['aside'] = '';
74 DI::page()['aside'] .= $vcard_widget;
75 DI::page()['aside'] .= $cal_widget;
80 function cal_content(App $a)
82 Nav::setSelected('events');
84 // get the translation strings for the callendar
85 $i18n = Event::getStrings();
87 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
88 DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
89 '$module_url' => '/cal/' . $a->data['user']['nickname'],
97 $ignored = (!empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0);
100 if ($a->argc == 4 && $a->argv[2] == 'export') {
102 $format = $a->argv[3];
105 // Setup permissions structures
106 $remote_contact = false;
109 $owner_uid = intval($a->data['user']['uid']);
110 $nick = $a->data['user']['nickname'];
112 if (!empty(Session::getRemoteContactID($a->profile['profile_uid']))) {
113 $contact_id = Session::getRemoteContactID($a->profile['profile_uid']);
117 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
119 intval($a->profile['profile_uid'])
121 if (DBA::isResult($r)) {
122 $remote_contact = true;
126 $is_owner = local_user() == $a->profile['profile_uid'];
128 if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
129 notice(L10n::t('Access to this profile has been restricted.') . EOL);
133 // get the permissions
134 $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
135 // we only want to have the events of the profile owner
136 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
138 // get the tab navigation bar
139 $tabs = Profile::getTabs($a, 'cal', false, $a->data['user']['nickname']);
141 // The view mode part is similiar to /mod/events.php
142 if ($mode == 'view') {
143 $thisyear = DateTimeFormat::localNow('Y');
144 $thismonth = DateTimeFormat::localNow('m');
146 $y = intval($thisyear);
150 $m = intval($thismonth);
153 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
154 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
166 if ($nextmonth > 12) {
179 $dim = Temporal::getDaysInMonth($y, $m);
180 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
181 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
184 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
185 if (!empty($_GET['start'])) {
186 $start = $_GET['start'];
189 if (!empty($_GET['end'])) {
190 $finish = $_GET['end'];
194 $start = DateTimeFormat::utc($start);
195 $finish = DateTimeFormat::utc($finish);
197 $adjust_start = DateTimeFormat::local($start);
198 $adjust_finish = DateTimeFormat::local($finish);
200 // put the event parametes in an array so we can better transmit them
202 'event_id' => intval($_GET['id'] ?? 0),
205 'adjust_start' => $adjust_start,
206 'adjust_finish' => $adjust_finish,
207 'ignore' => $ignored,
210 // get events by id or by date
211 if ($event_params['event_id']) {
212 $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
214 $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
219 if (DBA::isResult($r)) {
220 $r = Event::sortByDate($r);
221 foreach ($r as $rr) {
222 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
223 if (empty($links[$j])) {
224 $links[$j] = System::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
229 // transform the event in a usable array
230 $events = Event::prepareListForTemplate($r);
232 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
233 echo json_encode($events);
237 // links: array('href', 'text', 'extra css classes', 'title')
238 if (!empty($_GET['id'])) {
239 $tpl = Renderer::getMarkupTemplate("event.tpl");
241 // if (Config::get('experimentals','new_calendar')==1){
242 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
244 // $tpl = Renderer::getMarkupTemplate("events.tpl");
248 // Get rid of dashes in key names, Smarty3 can't handle them
249 foreach ($events as $key => $event) {
251 foreach ($event['item'] as $k => $v) {
252 $k = str_replace('-', '_', $k);
253 $event_item[$k] = $v;
255 $events[$key]['item'] = $event_item;
258 $o = Renderer::replaceMacros($tpl, [
260 '$title' => L10n::t('Events'),
261 '$view' => L10n::t('View'),
262 '$previous' => [System::baseUrl() . "/events/$prevyear/$prevmonth", L10n::t('Previous'), '', ''],
263 '$next' => [System::baseUrl() . "/events/$nextyear/$nextmonth", L10n::t('Next'), '', ''],
264 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
265 '$events' => $events,
266 "today" => L10n::t("today"),
267 "month" => L10n::t("month"),
268 "week" => L10n::t("week"),
269 "day" => L10n::t("day"),
270 "list" => L10n::t("list"),
273 if (!empty($_GET['id'])) {
281 if ($mode == 'export') {
283 notice(L10n::t('User not found'));
288 // Respect the export feature setting for all other /cal pages if it's not the own profile
289 if ((local_user() !== $owner_uid) && !Feature::isEnabled($owner_uid, "export_calendar")) {
290 notice(L10n::t('Permission denied.') . EOL);
291 DI::baseUrl()->redirect('cal/' . $nick);
294 // Get the export data by uid
295 $evexport = Event::exportListByUserId($owner_uid, $format);
297 if (!$evexport["success"]) {
298 if ($evexport["content"]) {
299 notice(L10n::t('This calendar format is not supported'));
301 notice(L10n::t('No exportable data found'));
304 // If it the own calendar return to the events page
305 // otherwise to the profile calendar page
306 if (local_user() === $owner_uid) {
307 $return_path = "events";
309 $return_path = "cal/" . $nick;
312 DI::baseUrl()->redirect($return_path);
315 // If nothing went wrong we can echo the export content
316 if ($evexport["success"]) {
317 header('Content-type: text/calendar');
318 header('content-disposition: attachment; filename="' . L10n::t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
319 echo $evexport["content"];