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 $owner_uid = intval($a->data['user']['uid']);
105 $nick = $a->data['user']['nickname'];
107 $contact_id = Session::getRemoteContactID($a->profile['uid']);
109 $remote_contact = $contact_id && DBA::exists('contact', ['id' => $contact_id, 'uid' => $a->profile['uid']]);
111 $is_owner = local_user() == $a->profile['uid'];
113 if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
114 notice(DI::l10n()->t('Access to this profile has been restricted.') . EOL);
118 // get the permissions
119 $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
120 // we only want to have the events of the profile owner
121 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
123 // get the tab navigation bar
124 $tabs = Profile::getTabs($a, 'cal', false, $a->data['user']['nickname']);
126 // The view mode part is similiar to /mod/events.php
127 if ($mode == 'view') {
128 $thisyear = DateTimeFormat::localNow('Y');
129 $thismonth = DateTimeFormat::localNow('m');
131 $y = intval($thisyear);
135 $m = intval($thismonth);
138 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
139 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
151 if ($nextmonth > 12) {
164 $dim = Temporal::getDaysInMonth($y, $m);
165 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
166 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
169 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
170 if (!empty($_GET['start'])) {
171 $start = $_GET['start'];
174 if (!empty($_GET['end'])) {
175 $finish = $_GET['end'];
179 $start = DateTimeFormat::utc($start);
180 $finish = DateTimeFormat::utc($finish);
182 $adjust_start = DateTimeFormat::local($start);
183 $adjust_finish = DateTimeFormat::local($finish);
185 // put the event parametes in an array so we can better transmit them
187 'event_id' => intval($_GET['id'] ?? 0),
190 'adjust_start' => $adjust_start,
191 'adjust_finish' => $adjust_finish,
192 'ignore' => $ignored,
195 // get events by id or by date
196 if ($event_params['event_id']) {
197 $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
199 $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
204 if (DBA::isResult($r)) {
205 $r = Event::sortByDate($r);
206 foreach ($r as $rr) {
207 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
208 if (empty($links[$j])) {
209 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
214 // transform the event in a usable array
215 $events = Event::prepareListForTemplate($r);
217 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
218 echo json_encode($events);
222 // links: array('href', 'text', 'extra css classes', 'title')
223 if (!empty($_GET['id'])) {
224 $tpl = Renderer::getMarkupTemplate("event.tpl");
226 // if (DI::config()->get('experimentals','new_calendar')==1){
227 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
229 // $tpl = Renderer::getMarkupTemplate("events.tpl");
233 // Get rid of dashes in key names, Smarty3 can't handle them
234 foreach ($events as $key => $event) {
236 foreach ($event['item'] as $k => $v) {
237 $k = str_replace('-', '_', $k);
238 $event_item[$k] = $v;
240 $events[$key]['item'] = $event_item;
243 $o = Renderer::replaceMacros($tpl, [
245 '$title' => DI::l10n()->t('Events'),
246 '$view' => DI::l10n()->t('View'),
247 '$previous' => [DI::baseUrl() . "/events/$prevyear/$prevmonth", DI::l10n()->t('Previous'), '', ''],
248 '$next' => [DI::baseUrl() . "/events/$nextyear/$nextmonth", DI::l10n()->t('Next'), '', ''],
249 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
250 '$events' => $events,
251 "today" => DI::l10n()->t("today"),
252 "month" => DI::l10n()->t("month"),
253 "week" => DI::l10n()->t("week"),
254 "day" => DI::l10n()->t("day"),
255 "list" => DI::l10n()->t("list"),
258 if (!empty($_GET['id'])) {
266 if ($mode == 'export') {
268 notice(DI::l10n()->t('User not found'));
273 // Respect the export feature setting for all other /cal pages if it's not the own profile
274 if ((local_user() !== $owner_uid) && !Feature::isEnabled($owner_uid, "export_calendar")) {
275 notice(DI::l10n()->t('Permission denied.') . EOL);
276 DI::baseUrl()->redirect('cal/' . $nick);
279 // Get the export data by uid
280 $evexport = Event::exportListByUserId($owner_uid, $format);
282 if (!$evexport["success"]) {
283 if ($evexport["content"]) {
284 notice(DI::l10n()->t('This calendar format is not supported'));
286 notice(DI::l10n()->t('No exportable data found'));
289 // If it the own calendar return to the events page
290 // otherwise to the profile calendar page
291 if (local_user() === $owner_uid) {
292 $return_path = "events";
294 $return_path = "cal/" . $nick;
297 DI::baseUrl()->redirect($return_path);
300 // If nothing went wrong we can echo the export content
301 if ($evexport["success"]) {
302 header('Content-type: text/calendar');
303 header('content-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
304 echo $evexport["content"];