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;
19 use Friendica\Model\Contact;
20 use Friendica\Model\Event;
21 use Friendica\Model\Group;
22 use Friendica\Model\Item;
23 use Friendica\Model\Profile;
24 use Friendica\Protocol\DFRN;
25 use Friendica\Util\DateTimeFormat;
26 use Friendica\Util\Temporal;
28 function cal_init(App $a)
30 if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
31 throw new \Friendica\Network\HTTPException\ForbiddenException(L10n::t('Access denied.'));
35 throw new \Friendica\Network\HTTPException\ForbiddenException(L10n::t('Access denied.'));
38 Nav::setSelected('events');
41 $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
42 if (!DBA::isResult($user)) {
43 throw new \Friendica\Network\HTTPException\NotFoundException();
46 $a->data['user'] = $user;
47 $a->profile_uid = $user['uid'];
49 // if it's a json request abort here becaus we don't
50 // need the widget data
51 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
55 $profile = Profile::getByNickname($nick, $a->profile_uid);
57 $account_type = Contact::getAccountType($profile);
59 $tpl = Renderer::getMarkupTemplate("widget/vcard.tpl");
61 $vcard_widget = Renderer::replaceMacros($tpl, [
62 '$name' => $profile['name'],
63 '$photo' => $profile['photo'],
64 '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
65 '$account_type' => $account_type,
66 '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
69 $cal_widget = Widget\CalendarExport::getHTML();
71 if (empty($a->page['aside'])) {
72 $a->page['aside'] = '';
75 $a->page['aside'] .= $vcard_widget;
76 $a->page['aside'] .= $cal_widget;
81 function cal_content(App $a)
83 Nav::setSelected('events');
85 // get the translation strings for the callendar
86 $i18n = Event::getStrings();
88 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
89 $a->page['htmlhead'] .= Renderer::replaceMacros($htpl, [
90 '$module_url' => '/cal/' . $a->data['user']['nickname'],
98 $ignored = (!empty($_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 = intval($a->data['user']['uid']);
111 $nick = $a->data['user']['nickname'];
113 if (!empty(Session::getRemoteContactID($a->profile['profile_uid']))) {
114 $contact_id = Session::getRemoteContactID($a->profile['profile_uid']);
118 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
120 intval($a->profile['profile_uid'])
122 if (DBA::isResult($r)) {
123 $remote_contact = true;
127 $is_owner = local_user() == $a->profile['profile_uid'];
129 if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
130 notice(L10n::t('Access to this profile has been restricted.') . EOL);
134 // get the permissions
135 $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
136 // we only want to have the events of the profile owner
137 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
139 // get the tab navigation bar
140 $tabs = Profile::getTabs($a, 'cal', false, $a->data['user']['nickname']);
142 // The view mode part is similiar to /mod/events.php
143 if ($mode == 'view') {
144 $thisyear = DateTimeFormat::localNow('Y');
145 $thismonth = DateTimeFormat::localNow('m');
147 $y = intval($thisyear);
151 $m = intval($thismonth);
154 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
155 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
167 if ($nextmonth > 12) {
180 $dim = Temporal::getDaysInMonth($y, $m);
181 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
182 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
185 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
186 if (!empty($_GET['start'])) {
187 $start = $_GET['start'];
190 if (!empty($_GET['end'])) {
191 $finish = $_GET['end'];
195 $start = DateTimeFormat::utc($start);
196 $finish = DateTimeFormat::utc($finish);
198 $adjust_start = DateTimeFormat::local($start);
199 $adjust_finish = DateTimeFormat::local($finish);
201 // put the event parametes in an array so we can better transmit them
203 'event_id' => intval($_GET['id'] ?? 0),
206 'adjust_start' => $adjust_start,
207 'adjust_finish' => $adjust_finish,
208 'ignore' => $ignored,
211 // get events by id or by date
212 if ($event_params['event_id']) {
213 $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
215 $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
220 if (DBA::isResult($r)) {
221 $r = Event::sortByDate($r);
222 foreach ($r as $rr) {
223 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
224 if (empty($links[$j])) {
225 $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
230 // transform the event in a usable array
231 $events = Event::prepareListForTemplate($r);
233 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
234 echo json_encode($events);
238 // links: array('href', 'text', 'extra css classes', 'title')
239 if (!empty($_GET['id'])) {
240 $tpl = Renderer::getMarkupTemplate("event.tpl");
242 // if (Config::get('experimentals','new_calendar')==1){
243 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
245 // $tpl = Renderer::getMarkupTemplate("events.tpl");
249 // Get rid of dashes in key names, Smarty3 can't handle them
250 foreach ($events as $key => $event) {
252 foreach ($event['item'] as $k => $v) {
253 $k = str_replace('-', '_', $k);
254 $event_item[$k] = $v;
256 $events[$key]['item'] = $event_item;
259 $o = Renderer::replaceMacros($tpl, [
261 '$title' => L10n::t('Events'),
262 '$view' => L10n::t('View'),
263 '$previous' => [System::baseUrl() . "/events/$prevyear/$prevmonth", L10n::t('Previous'), '', ''],
264 '$next' => [System::baseUrl() . "/events/$nextyear/$nextmonth", L10n::t('Next'), '', ''],
265 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
266 '$events' => $events,
267 "today" => L10n::t("today"),
268 "month" => L10n::t("month"),
269 "week" => L10n::t("week"),
270 "day" => L10n::t("day"),
271 "list" => L10n::t("list"),
274 if (!empty($_GET['id'])) {
282 if ($mode == 'export') {
284 notice(L10n::t('User not found'));
289 // Respect the export feature setting for all other /cal pages if it's not the own profile
290 if ((local_user() !== $owner_uid) && !Feature::isEnabled($owner_uid, "export_calendar")) {
291 notice(L10n::t('Permission denied.') . EOL);
292 $a->internalRedirect('cal/' . $nick);
295 // Get the export data by uid
296 $evexport = Event::exportListByUserId($owner_uid, $format);
298 if (!$evexport["success"]) {
299 if ($evexport["content"]) {
300 notice(L10n::t('This calendar format is not supported'));
302 notice(L10n::t('No exportable data found'));
305 // If it the own calendar return to the events page
306 // otherwise to the profile calendar page
307 if (local_user() === $owner_uid) {
308 $return_path = "events";
310 $return_path = "cal/" . $nick;
313 $a->internalRedirect($return_path);
316 // If nothing went wrong we can echo the export content
317 if ($evexport["success"]) {
318 header('Content-type: text/calendar');
319 header('content-disposition: attachment; filename="' . L10n::t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
320 echo $evexport["content"];