3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 * This calendar is for profile visitors and contains only the events
23 * of the profile owner
27 use Friendica\Content\Feature;
28 use Friendica\Content\Nav;
29 use Friendica\Content\Text\BBCode;
30 use Friendica\Content\Widget;
31 use Friendica\Core\Renderer;
32 use Friendica\Core\Session;
33 use Friendica\Database\DBA;
35 use Friendica\Model\Contact;
36 use Friendica\Model\Event;
37 use Friendica\Model\Item;
38 use Friendica\Model\Profile;
39 use Friendica\Module\BaseProfile;
40 use Friendica\Network\HTTPException;
41 use Friendica\Util\DateTimeFormat;
42 use Friendica\Util\Temporal;
44 function cal_init(App $a)
46 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
47 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
51 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
54 Nav::setSelected('events');
57 $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
58 if (!DBA::isResult($user)) {
59 throw new HTTPException\NotFoundException();
62 $a->data['user'] = $user;
63 $a->profile_uid = $user['uid'];
65 // if it's a json request abort here becaus we don't
66 // need the widget data
67 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
71 $a->profile = Profile::getByNickname($nick, $a->profile_uid);
73 if (empty($a->profile)) {
74 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
77 $account_type = Contact::getAccountType($a->profile);
79 $tpl = Renderer::getMarkupTemplate('widget/vcard.tpl');
81 $vcard_widget = Renderer::replaceMacros($tpl, [
82 '$name' => $a->profile['name'],
83 '$photo' => $a->profile['photo'],
84 '$addr' => $a->profile['addr'] ?: '',
85 '$account_type' => $account_type,
86 '$about' => BBCode::convert($a->profile['about']),
89 $cal_widget = Widget\CalendarExport::getHTML();
91 if (empty(DI::page()['aside'])) {
92 DI::page()['aside'] = '';
95 DI::page()['aside'] .= $vcard_widget;
96 DI::page()['aside'] .= $cal_widget;
101 function cal_content(App $a)
103 Nav::setSelected('events');
105 // get the translation strings for the callendar
106 $i18n = Event::getStrings();
108 DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.min.css');
109 DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.print.min.css', 'print');
110 DI::page()->registerFooterScript('view/asset/moment/min/moment-with-locales.min.js');
111 DI::page()->registerFooterScript('view/asset/fullcalendar/dist/fullcalendar.min.js');
113 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
114 DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
115 '$module_url' => '/cal/' . $a->data['user']['nickname'],
123 $ignored = (!empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0);
126 if ($a->argc == 4 && $a->argv[2] == 'export') {
128 $format = $a->argv[3];
131 // Setup permissions structures
132 $owner_uid = intval($a->data['user']['uid']);
133 $nick = $a->data['user']['nickname'];
135 $contact_id = Session::getRemoteContactID($a->profile['uid']);
137 $remote_contact = $contact_id && DBA::exists('contact', ['id' => $contact_id, 'uid' => $a->profile['uid']]);
139 $is_owner = local_user() == $a->profile['uid'];
141 if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
142 notice(DI::l10n()->t('Access to this profile has been restricted.'));
146 // get the permissions
147 $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
148 // we only want to have the events of the profile owner
149 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
151 // get the tab navigation bar
152 $tabs = BaseProfile::getTabsHTML($a, 'cal', false, $a->data['user']['nickname']);
154 // The view mode part is similiar to /mod/events.php
155 if ($mode == 'view') {
156 $thisyear = DateTimeFormat::localNow('Y');
157 $thismonth = DateTimeFormat::localNow('m');
159 $y = intval($thisyear);
163 $m = intval($thismonth);
166 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
167 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
179 if ($nextmonth > 12) {
192 $dim = Temporal::getDaysInMonth($y, $m);
193 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
194 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
197 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
198 if (!empty($_GET['start'])) {
199 $start = $_GET['start'];
202 if (!empty($_GET['end'])) {
203 $finish = $_GET['end'];
207 $start = DateTimeFormat::utc($start);
208 $finish = DateTimeFormat::utc($finish);
210 $adjust_start = DateTimeFormat::local($start);
211 $adjust_finish = DateTimeFormat::local($finish);
213 // put the event parametes in an array so we can better transmit them
215 'event_id' => intval($_GET['id'] ?? 0),
218 'adjust_start' => $adjust_start,
219 'adjust_finish' => $adjust_finish,
220 'ignore' => $ignored,
223 // get events by id or by date
224 if ($event_params['event_id']) {
225 $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
227 $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
232 if (DBA::isResult($r)) {
233 $r = Event::sortByDate($r);
234 foreach ($r as $rr) {
235 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
236 if (empty($links[$j])) {
237 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
242 // transform the event in a usable array
243 $events = Event::prepareListForTemplate($r);
245 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
246 echo json_encode($events);
250 // links: array('href', 'text', 'extra css classes', 'title')
251 if (!empty($_GET['id'])) {
252 $tpl = Renderer::getMarkupTemplate("event.tpl");
254 // if (DI::config()->get('experimentals','new_calendar')==1){
255 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
257 // $tpl = Renderer::getMarkupTemplate("events.tpl");
261 // Get rid of dashes in key names, Smarty3 can't handle them
262 foreach ($events as $key => $event) {
264 foreach ($event['item'] as $k => $v) {
265 $k = str_replace('-', '_', $k);
266 $event_item[$k] = $v;
268 $events[$key]['item'] = $event_item;
271 $o = Renderer::replaceMacros($tpl, [
273 '$title' => DI::l10n()->t('Events'),
274 '$view' => DI::l10n()->t('View'),
275 '$previous' => [DI::baseUrl() . "/events/$prevyear/$prevmonth", DI::l10n()->t('Previous'), '', ''],
276 '$next' => [DI::baseUrl() . "/events/$nextyear/$nextmonth", DI::l10n()->t('Next'), '', ''],
277 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
278 '$events' => $events,
279 "today" => DI::l10n()->t("today"),
280 "month" => DI::l10n()->t("month"),
281 "week" => DI::l10n()->t("week"),
282 "day" => DI::l10n()->t("day"),
283 "list" => DI::l10n()->t("list"),
286 if (!empty($_GET['id'])) {
294 if ($mode == 'export') {
296 notice(DI::l10n()->t('User not found'));
300 // Get the export data by uid
301 $evexport = Event::exportListByUserId($owner_uid, $format);
303 if (!$evexport["success"]) {
304 if ($evexport["content"]) {
305 notice(DI::l10n()->t('This calendar format is not supported'));
307 notice(DI::l10n()->t('No exportable data found'));
310 // If it the own calendar return to the events page
311 // otherwise to the profile calendar page
312 if (local_user() === $owner_uid) {
313 $return_path = "events";
315 $return_path = "cal/" . $nick;
318 DI::baseUrl()->redirect($return_path);
321 // If nothing went wrong we can echo the export content
322 if ($evexport["success"]) {
323 header('Content-type: text/calendar');
324 header('content-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
325 echo $evexport["content"];