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\Text\BBCode;
14 use Friendica\Content\Widget;
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\Module\BaseProfile;
24 use Friendica\Util\DateTimeFormat;
25 use Friendica\Util\Temporal;
27 function cal_init(App $a)
29 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
30 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
34 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
37 Nav::setSelected('events');
40 $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
41 if (!DBA::isResult($user)) {
42 throw new \Friendica\Network\HTTPException\NotFoundException();
45 $a->data['user'] = $user;
46 $a->profile_uid = $user['uid'];
48 // if it's a json request abort here becaus we don't
49 // need the widget data
50 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
54 $profile = Profile::getByNickname($nick, $a->profile_uid);
56 $account_type = Contact::getAccountType($profile);
58 $tpl = Renderer::getMarkupTemplate('widget/vcard.tpl');
60 $vcard_widget = Renderer::replaceMacros($tpl, [
61 '$name' => $profile['name'],
62 '$photo' => $profile['photo'],
63 '$addr' => $profile['addr'] ?: '',
64 '$account_type' => $account_type,
65 '$about' => BBCode::convert($profile['about'] ?: ''),
68 $cal_widget = Widget\CalendarExport::getHTML();
70 if (empty(DI::page()['aside'])) {
71 DI::page()['aside'] = '';
74 DI::page()['aside'] .= $vcard_widget;
75 DI::page()['aside'] .= $cal_widget;
80 function cal_content(App $a)
82 Nav::setSelected('events');
84 // get the translation strings for the callendar
85 $i18n = Event::getStrings();
87 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
88 DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
89 '$module_url' => '/cal/' . $a->data['user']['nickname'],
97 $ignored = (!empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0);
100 if ($a->argc == 4 && $a->argv[2] == 'export') {
102 $format = $a->argv[3];
105 // Setup permissions structures
106 $owner_uid = intval($a->data['user']['uid']);
107 $nick = $a->data['user']['nickname'];
109 $contact_id = Session::getRemoteContactID($a->profile['uid']);
111 $remote_contact = $contact_id && DBA::exists('contact', ['id' => $contact_id, 'uid' => $a->profile['uid']]);
113 $is_owner = local_user() == $a->profile['uid'];
115 if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
116 notice(DI::l10n()->t('Access to this profile has been restricted.') . EOL);
120 // get the permissions
121 $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
122 // we only want to have the events of the profile owner
123 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
125 // get the tab navigation bar
126 $tabs = BaseProfile::getTabsHTML($a, 'cal', false, $a->data['user']['nickname']);
128 // The view mode part is similiar to /mod/events.php
129 if ($mode == 'view') {
130 $thisyear = DateTimeFormat::localNow('Y');
131 $thismonth = DateTimeFormat::localNow('m');
133 $y = intval($thisyear);
137 $m = intval($thismonth);
140 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
141 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
153 if ($nextmonth > 12) {
166 $dim = Temporal::getDaysInMonth($y, $m);
167 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
168 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
171 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
172 if (!empty($_GET['start'])) {
173 $start = $_GET['start'];
176 if (!empty($_GET['end'])) {
177 $finish = $_GET['end'];
181 $start = DateTimeFormat::utc($start);
182 $finish = DateTimeFormat::utc($finish);
184 $adjust_start = DateTimeFormat::local($start);
185 $adjust_finish = DateTimeFormat::local($finish);
187 // put the event parametes in an array so we can better transmit them
189 'event_id' => intval($_GET['id'] ?? 0),
192 'adjust_start' => $adjust_start,
193 'adjust_finish' => $adjust_finish,
194 'ignore' => $ignored,
197 // get events by id or by date
198 if ($event_params['event_id']) {
199 $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
201 $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
206 if (DBA::isResult($r)) {
207 $r = Event::sortByDate($r);
208 foreach ($r as $rr) {
209 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
210 if (empty($links[$j])) {
211 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
216 // transform the event in a usable array
217 $events = Event::prepareListForTemplate($r);
219 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
220 echo json_encode($events);
224 // links: array('href', 'text', 'extra css classes', 'title')
225 if (!empty($_GET['id'])) {
226 $tpl = Renderer::getMarkupTemplate("event.tpl");
228 // if (DI::config()->get('experimentals','new_calendar')==1){
229 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
231 // $tpl = Renderer::getMarkupTemplate("events.tpl");
235 // Get rid of dashes in key names, Smarty3 can't handle them
236 foreach ($events as $key => $event) {
238 foreach ($event['item'] as $k => $v) {
239 $k = str_replace('-', '_', $k);
240 $event_item[$k] = $v;
242 $events[$key]['item'] = $event_item;
245 $o = Renderer::replaceMacros($tpl, [
247 '$title' => DI::l10n()->t('Events'),
248 '$view' => DI::l10n()->t('View'),
249 '$previous' => [DI::baseUrl() . "/events/$prevyear/$prevmonth", DI::l10n()->t('Previous'), '', ''],
250 '$next' => [DI::baseUrl() . "/events/$nextyear/$nextmonth", DI::l10n()->t('Next'), '', ''],
251 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
252 '$events' => $events,
253 "today" => DI::l10n()->t("today"),
254 "month" => DI::l10n()->t("month"),
255 "week" => DI::l10n()->t("week"),
256 "day" => DI::l10n()->t("day"),
257 "list" => DI::l10n()->t("list"),
260 if (!empty($_GET['id'])) {
268 if ($mode == 'export') {
270 notice(DI::l10n()->t('User not found'));
275 // Respect the export feature setting for all other /cal pages if it's not the own profile
276 if ((local_user() !== $owner_uid) && !Feature::isEnabled($owner_uid, "export_calendar")) {
277 notice(DI::l10n()->t('Permission denied.') . EOL);
278 DI::baseUrl()->redirect('cal/' . $nick);
281 // Get the export data by uid
282 $evexport = Event::exportListByUserId($owner_uid, $format);
284 if (!$evexport["success"]) {
285 if ($evexport["content"]) {
286 notice(DI::l10n()->t('This calendar format is not supported'));
288 notice(DI::l10n()->t('No exportable data found'));
291 // If it the own calendar return to the events page
292 // otherwise to the profile calendar page
293 if (local_user() === $owner_uid) {
294 $return_path = "events";
296 $return_path = "cal/" . $nick;
299 DI::baseUrl()->redirect($return_path);
302 // If nothing went wrong we can echo the export content
303 if ($evexport["success"]) {
304 header('Content-type: text/calendar');
305 header('content-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
306 echo $evexport["content"];