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\Config;
15 use Friendica\Core\Renderer;
16 use Friendica\Core\Session;
17 use Friendica\Database\DBA;
19 use Friendica\Model\Contact;
20 use Friendica\Model\Event;
21 use Friendica\Model\Item;
22 use Friendica\Model\Profile;
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'] != "") ? $profile['addr'] : ""),
63 '$account_type' => $account_type,
64 '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
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 $remote_contact = false;
108 $owner_uid = intval($a->data['user']['uid']);
109 $nick = $a->data['user']['nickname'];
111 if (!empty(Session::getRemoteContactID($a->profile['profile_uid']))) {
112 $contact_id = Session::getRemoteContactID($a->profile['profile_uid']);
116 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
118 intval($a->profile['profile_uid'])
120 if (DBA::isResult($r)) {
121 $remote_contact = true;
125 $is_owner = local_user() == $a->profile['profile_uid'];
127 if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
128 notice(DI::l10n()->t('Access to this profile has been restricted.') . EOL);
132 // get the permissions
133 $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
134 // we only want to have the events of the profile owner
135 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
137 // get the tab navigation bar
138 $tabs = Profile::getTabs($a, 'cal', false, $a->data['user']['nickname']);
140 // The view mode part is similiar to /mod/events.php
141 if ($mode == 'view') {
142 $thisyear = DateTimeFormat::localNow('Y');
143 $thismonth = DateTimeFormat::localNow('m');
145 $y = intval($thisyear);
149 $m = intval($thismonth);
152 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
153 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
165 if ($nextmonth > 12) {
178 $dim = Temporal::getDaysInMonth($y, $m);
179 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
180 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
183 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
184 if (!empty($_GET['start'])) {
185 $start = $_GET['start'];
188 if (!empty($_GET['end'])) {
189 $finish = $_GET['end'];
193 $start = DateTimeFormat::utc($start);
194 $finish = DateTimeFormat::utc($finish);
196 $adjust_start = DateTimeFormat::local($start);
197 $adjust_finish = DateTimeFormat::local($finish);
199 // put the event parametes in an array so we can better transmit them
201 'event_id' => intval($_GET['id'] ?? 0),
204 'adjust_start' => $adjust_start,
205 'adjust_finish' => $adjust_finish,
206 'ignore' => $ignored,
209 // get events by id or by date
210 if ($event_params['event_id']) {
211 $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
213 $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
218 if (DBA::isResult($r)) {
219 $r = Event::sortByDate($r);
220 foreach ($r as $rr) {
221 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
222 if (empty($links[$j])) {
223 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
228 // transform the event in a usable array
229 $events = Event::prepareListForTemplate($r);
231 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
232 echo json_encode($events);
236 // links: array('href', 'text', 'extra css classes', 'title')
237 if (!empty($_GET['id'])) {
238 $tpl = Renderer::getMarkupTemplate("event.tpl");
240 // if (DI::config()->get('experimentals','new_calendar')==1){
241 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
243 // $tpl = Renderer::getMarkupTemplate("events.tpl");
247 // Get rid of dashes in key names, Smarty3 can't handle them
248 foreach ($events as $key => $event) {
250 foreach ($event['item'] as $k => $v) {
251 $k = str_replace('-', '_', $k);
252 $event_item[$k] = $v;
254 $events[$key]['item'] = $event_item;
257 $o = Renderer::replaceMacros($tpl, [
259 '$title' => DI::l10n()->t('Events'),
260 '$view' => DI::l10n()->t('View'),
261 '$previous' => [DI::baseUrl() . "/events/$prevyear/$prevmonth", DI::l10n()->t('Previous'), '', ''],
262 '$next' => [DI::baseUrl() . "/events/$nextyear/$nextmonth", DI::l10n()->t('Next'), '', ''],
263 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
264 '$events' => $events,
265 "today" => DI::l10n()->t("today"),
266 "month" => DI::l10n()->t("month"),
267 "week" => DI::l10n()->t("week"),
268 "day" => DI::l10n()->t("day"),
269 "list" => DI::l10n()->t("list"),
272 if (!empty($_GET['id'])) {
280 if ($mode == 'export') {
282 notice(DI::l10n()->t('User not found'));
287 // Respect the export feature setting for all other /cal pages if it's not the own profile
288 if ((local_user() !== $owner_uid) && !Feature::isEnabled($owner_uid, "export_calendar")) {
289 notice(DI::l10n()->t('Permission denied.') . EOL);
290 DI::baseUrl()->redirect('cal/' . $nick);
293 // Get the export data by uid
294 $evexport = Event::exportListByUserId($owner_uid, $format);
296 if (!$evexport["success"]) {
297 if ($evexport["content"]) {
298 notice(DI::l10n()->t('This calendar format is not supported'));
300 notice(DI::l10n()->t('No exportable data found'));
303 // If it the own calendar return to the events page
304 // otherwise to the profile calendar page
305 if (local_user() === $owner_uid) {
306 $return_path = "events";
308 $return_path = "cal/" . $nick;
311 DI::baseUrl()->redirect($return_path);
314 // If nothing went wrong we can echo the export content
315 if ($evexport["success"]) {
316 header('Content-type: text/calendar');
317 header('content-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
318 echo $evexport["content"];