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\Util\DateTimeFormat;
23 use Friendica\Util\Temporal;
25 function cal_init(App $a)
27 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
28 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
32 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
35 Nav::setSelected('events');
38 $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
39 if (!DBA::isResult($user)) {
40 throw new \Friendica\Network\HTTPException\NotFoundException();
43 $a->data['user'] = $user;
44 $a->profile_uid = $user['uid'];
46 // if it's a json request abort here becaus we don't
47 // need the widget data
48 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
52 $profile = Profile::getByNickname($nick, $a->profile_uid);
54 $account_type = Contact::getAccountType($profile);
56 $tpl = Renderer::getMarkupTemplate("widget/vcard.tpl");
58 $vcard_widget = Renderer::replaceMacros($tpl, [
59 '$name' => $profile['name'],
60 '$photo' => $profile['photo'],
61 '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
62 '$account_type' => $account_type,
63 '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
66 $cal_widget = Widget\CalendarExport::getHTML();
68 if (empty(DI::page()['aside'])) {
69 DI::page()['aside'] = '';
72 DI::page()['aside'] .= $vcard_widget;
73 DI::page()['aside'] .= $cal_widget;
78 function cal_content(App $a)
80 Nav::setSelected('events');
82 // get the translation strings for the callendar
83 $i18n = Event::getStrings();
85 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
86 DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
87 '$module_url' => '/cal/' . $a->data['user']['nickname'],
95 $ignored = (!empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0);
98 if ($a->argc == 4 && $a->argv[2] == 'export') {
100 $format = $a->argv[3];
103 // Setup permissions structures
104 $remote_contact = false;
107 $owner_uid = intval($a->data['user']['uid']);
108 $nick = $a->data['user']['nickname'];
110 if (!empty(Session::getRemoteContactID($a->profile['profile_uid']))) {
111 $contact_id = Session::getRemoteContactID($a->profile['profile_uid']);
115 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
117 intval($a->profile['profile_uid'])
119 if (DBA::isResult($r)) {
120 $remote_contact = true;
124 $is_owner = local_user() == $a->profile['profile_uid'];
126 if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
127 notice(DI::l10n()->t('Access to this profile has been restricted.') . EOL);
131 // get the permissions
132 $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
133 // we only want to have the events of the profile owner
134 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
136 // get the tab navigation bar
137 $tabs = Profile::getTabs($a, 'cal', false, $a->data['user']['nickname']);
139 // The view mode part is similiar to /mod/events.php
140 if ($mode == 'view') {
141 $thisyear = DateTimeFormat::localNow('Y');
142 $thismonth = DateTimeFormat::localNow('m');
144 $y = intval($thisyear);
148 $m = intval($thismonth);
151 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
152 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
164 if ($nextmonth > 12) {
177 $dim = Temporal::getDaysInMonth($y, $m);
178 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
179 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
182 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
183 if (!empty($_GET['start'])) {
184 $start = $_GET['start'];
187 if (!empty($_GET['end'])) {
188 $finish = $_GET['end'];
192 $start = DateTimeFormat::utc($start);
193 $finish = DateTimeFormat::utc($finish);
195 $adjust_start = DateTimeFormat::local($start);
196 $adjust_finish = DateTimeFormat::local($finish);
198 // put the event parametes in an array so we can better transmit them
200 'event_id' => intval($_GET['id'] ?? 0),
203 'adjust_start' => $adjust_start,
204 'adjust_finish' => $adjust_finish,
205 'ignore' => $ignored,
208 // get events by id or by date
209 if ($event_params['event_id']) {
210 $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
212 $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
217 if (DBA::isResult($r)) {
218 $r = Event::sortByDate($r);
219 foreach ($r as $rr) {
220 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
221 if (empty($links[$j])) {
222 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
227 // transform the event in a usable array
228 $events = Event::prepareListForTemplate($r);
230 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
231 echo json_encode($events);
235 // links: array('href', 'text', 'extra css classes', 'title')
236 if (!empty($_GET['id'])) {
237 $tpl = Renderer::getMarkupTemplate("event.tpl");
239 // if (DI::config()->get('experimentals','new_calendar')==1){
240 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
242 // $tpl = Renderer::getMarkupTemplate("events.tpl");
246 // Get rid of dashes in key names, Smarty3 can't handle them
247 foreach ($events as $key => $event) {
249 foreach ($event['item'] as $k => $v) {
250 $k = str_replace('-', '_', $k);
251 $event_item[$k] = $v;
253 $events[$key]['item'] = $event_item;
256 $o = Renderer::replaceMacros($tpl, [
258 '$title' => DI::l10n()->t('Events'),
259 '$view' => DI::l10n()->t('View'),
260 '$previous' => [DI::baseUrl() . "/events/$prevyear/$prevmonth", DI::l10n()->t('Previous'), '', ''],
261 '$next' => [DI::baseUrl() . "/events/$nextyear/$nextmonth", DI::l10n()->t('Next'), '', ''],
262 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
263 '$events' => $events,
264 "today" => DI::l10n()->t("today"),
265 "month" => DI::l10n()->t("month"),
266 "week" => DI::l10n()->t("week"),
267 "day" => DI::l10n()->t("day"),
268 "list" => DI::l10n()->t("list"),
271 if (!empty($_GET['id'])) {
279 if ($mode == 'export') {
281 notice(DI::l10n()->t('User not found'));
286 // Respect the export feature setting for all other /cal pages if it's not the own profile
287 if ((local_user() !== $owner_uid) && !Feature::isEnabled($owner_uid, "export_calendar")) {
288 notice(DI::l10n()->t('Permission denied.') . EOL);
289 DI::baseUrl()->redirect('cal/' . $nick);
292 // Get the export data by uid
293 $evexport = Event::exportListByUserId($owner_uid, $format);
295 if (!$evexport["success"]) {
296 if ($evexport["content"]) {
297 notice(DI::l10n()->t('This calendar format is not supported'));
299 notice(DI::l10n()->t('No exportable data found'));
302 // If it the own calendar return to the events page
303 // otherwise to the profile calendar page
304 if (local_user() === $owner_uid) {
305 $return_path = "events";
307 $return_path = "cal/" . $nick;
310 DI::baseUrl()->redirect($return_path);
313 // If nothing went wrong we can echo the export content
314 if ($evexport["success"]) {
315 header('Content-type: text/calendar');
316 header('content-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
317 echo $evexport["content"];