6 * This calendar is for profile visitors and contains only the events
11 use Friendica\Content\Feature;
12 use Friendica\Content\Nav;
13 use Friendica\Content\Widget;
14 use Friendica\Core\Renderer;
15 use Friendica\Core\Session;
16 use Friendica\Database\DBA;
18 use Friendica\Model\Contact;
19 use Friendica\Model\Event;
20 use Friendica\Model\Item;
21 use Friendica\Model\Profile;
22 use Friendica\Module\BaseProfile;
23 use Friendica\Util\DateTimeFormat;
24 use Friendica\Util\Temporal;
26 function cal_init(App $a)
28 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
29 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
33 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
36 Nav::setSelected('events');
39 $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
40 if (!DBA::isResult($user)) {
41 throw new \Friendica\Network\HTTPException\NotFoundException();
44 $a->data['user'] = $user;
45 $a->profile_uid = $user['uid'];
47 // if it's a json request abort here becaus we don't
48 // need the widget data
49 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
53 $profile = Profile::getByNickname($nick, $a->profile_uid);
55 $account_type = Contact::getAccountType($profile);
57 $tpl = Renderer::getMarkupTemplate("widget/vcard.tpl");
59 $vcard_widget = Renderer::replaceMacros($tpl, [
60 '$name' => $profile['name'],
61 '$photo' => $profile['photo'],
62 '$addr' => $profile['addr'] ?: '',
63 '$account_type' => $account_type,
64 '$about' => $profile['about'] ?: '',
67 $cal_widget = Widget\CalendarExport::getHTML();
69 if (empty(DI::page()['aside'])) {
70 DI::page()['aside'] = '';
73 DI::page()['aside'] .= $vcard_widget;
74 DI::page()['aside'] .= $cal_widget;
79 function cal_content(App $a)
81 Nav::setSelected('events');
83 // get the translation strings for the callendar
84 $i18n = Event::getStrings();
86 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
87 DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
88 '$module_url' => '/cal/' . $a->data['user']['nickname'],
96 $ignored = (!empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0);
99 if ($a->argc == 4 && $a->argv[2] == 'export') {
101 $format = $a->argv[3];
104 // Setup permissions structures
105 $owner_uid = intval($a->data['user']['uid']);
106 $nick = $a->data['user']['nickname'];
108 $contact_id = Session::getRemoteContactID($a->profile['uid']);
110 $remote_contact = $contact_id && DBA::exists('contact', ['id' => $contact_id, 'uid' => $a->profile['uid']]);
112 $is_owner = local_user() == $a->profile['uid'];
114 if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
115 notice(DI::l10n()->t('Access to this profile has been restricted.') . EOL);
119 // get the permissions
120 $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
121 // we only want to have the events of the profile owner
122 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
124 // get the tab navigation bar
125 $tabs = BaseProfile::getTabsHTML($a, 'cal', false, $a->data['user']['nickname']);
127 // The view mode part is similiar to /mod/events.php
128 if ($mode == 'view') {
129 $thisyear = DateTimeFormat::localNow('Y');
130 $thismonth = DateTimeFormat::localNow('m');
132 $y = intval($thisyear);
136 $m = intval($thismonth);
139 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
140 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
152 if ($nextmonth > 12) {
165 $dim = Temporal::getDaysInMonth($y, $m);
166 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
167 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
170 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
171 if (!empty($_GET['start'])) {
172 $start = $_GET['start'];
175 if (!empty($_GET['end'])) {
176 $finish = $_GET['end'];
180 $start = DateTimeFormat::utc($start);
181 $finish = DateTimeFormat::utc($finish);
183 $adjust_start = DateTimeFormat::local($start);
184 $adjust_finish = DateTimeFormat::local($finish);
186 // put the event parametes in an array so we can better transmit them
188 'event_id' => intval($_GET['id'] ?? 0),
191 'adjust_start' => $adjust_start,
192 'adjust_finish' => $adjust_finish,
193 'ignore' => $ignored,
196 // get events by id or by date
197 if ($event_params['event_id']) {
198 $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
200 $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
205 if (DBA::isResult($r)) {
206 $r = Event::sortByDate($r);
207 foreach ($r as $rr) {
208 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
209 if (empty($links[$j])) {
210 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
215 // transform the event in a usable array
216 $events = Event::prepareListForTemplate($r);
218 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
219 echo json_encode($events);
223 // links: array('href', 'text', 'extra css classes', 'title')
224 if (!empty($_GET['id'])) {
225 $tpl = Renderer::getMarkupTemplate("event.tpl");
227 // if (DI::config()->get('experimentals','new_calendar')==1){
228 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
230 // $tpl = Renderer::getMarkupTemplate("events.tpl");
234 // Get rid of dashes in key names, Smarty3 can't handle them
235 foreach ($events as $key => $event) {
237 foreach ($event['item'] as $k => $v) {
238 $k = str_replace('-', '_', $k);
239 $event_item[$k] = $v;
241 $events[$key]['item'] = $event_item;
244 $o = Renderer::replaceMacros($tpl, [
246 '$title' => DI::l10n()->t('Events'),
247 '$view' => DI::l10n()->t('View'),
248 '$previous' => [DI::baseUrl() . "/events/$prevyear/$prevmonth", DI::l10n()->t('Previous'), '', ''],
249 '$next' => [DI::baseUrl() . "/events/$nextyear/$nextmonth", DI::l10n()->t('Next'), '', ''],
250 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
251 '$events' => $events,
252 "today" => DI::l10n()->t("today"),
253 "month" => DI::l10n()->t("month"),
254 "week" => DI::l10n()->t("week"),
255 "day" => DI::l10n()->t("day"),
256 "list" => DI::l10n()->t("list"),
259 if (!empty($_GET['id'])) {
267 if ($mode == 'export') {
269 notice(DI::l10n()->t('User not found'));
274 // Respect the export feature setting for all other /cal pages if it's not the own profile
275 if ((local_user() !== $owner_uid) && !Feature::isEnabled($owner_uid, "export_calendar")) {
276 notice(DI::l10n()->t('Permission denied.') . EOL);
277 DI::baseUrl()->redirect('cal/' . $nick);
280 // Get the export data by uid
281 $evexport = Event::exportListByUserId($owner_uid, $format);
283 if (!$evexport["success"]) {
284 if ($evexport["content"]) {
285 notice(DI::l10n()->t('This calendar format is not supported'));
287 notice(DI::l10n()->t('No exportable data found'));
290 // If it the own calendar return to the events page
291 // otherwise to the profile calendar page
292 if (local_user() === $owner_uid) {
293 $return_path = "events";
295 $return_path = "cal/" . $nick;
298 DI::baseUrl()->redirect($return_path);
301 // If nothing went wrong we can echo the export content
302 if ($evexport["success"]) {
303 header('Content-type: text/calendar');
304 header('content-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
305 echo $evexport["content"];