3 * @copyright Copyright (C) 2010-2021, the Friendica project
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\Nav;
28 use Friendica\Content\Text\BBCode;
29 use Friendica\Content\Widget;
30 use Friendica\Core\Renderer;
31 use Friendica\Core\Session;
32 use Friendica\Database\DBA;
34 use Friendica\Model\Contact;
35 use Friendica\Model\Event;
36 use Friendica\Model\Item;
37 use Friendica\Model\User;
38 use Friendica\Module\BaseProfile;
39 use Friendica\Network\HTTPException;
40 use Friendica\Util\DateTimeFormat;
41 use Friendica\Util\Temporal;
43 function cal_init(App $a)
45 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
46 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
50 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
53 Nav::setSelected('events');
56 $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
57 if (!DBA::isResult($user)) {
58 throw new HTTPException\NotFoundException();
61 $a->data['user'] = $user;
62 $a->profile_uid = $user['uid'];
64 // if it's a json request abort here becaus we don't
65 // need the widget data
66 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
70 $a->profile = User::getOwnerDataByNick($nick);
71 if (empty($a->profile)) {
72 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
75 $account_type = Contact::getAccountType($a->profile);
77 $tpl = Renderer::getMarkupTemplate('widget/vcard.tpl');
79 $vcard_widget = Renderer::replaceMacros($tpl, [
80 '$name' => $a->profile['name'],
81 '$photo' => $a->profile['photo'],
82 '$addr' => $a->profile['addr'] ?: '',
83 '$account_type' => $account_type,
84 '$about' => BBCode::convert($a->profile['about']),
87 $cal_widget = Widget\CalendarExport::getHTML($user['uid']);
89 if (empty(DI::page()['aside'])) {
90 DI::page()['aside'] = '';
93 DI::page()['aside'] .= $vcard_widget;
94 DI::page()['aside'] .= $cal_widget;
99 function cal_content(App $a)
101 Nav::setSelected('events');
103 // get the translation strings for the callendar
104 $i18n = Event::getStrings();
106 DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.min.css');
107 DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.print.min.css', 'print');
108 DI::page()->registerFooterScript('view/asset/moment/min/moment-with-locales.min.js');
109 DI::page()->registerFooterScript('view/asset/fullcalendar/dist/fullcalendar.min.js');
111 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
112 DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
113 '$module_url' => '/cal/' . $a->data['user']['nickname'],
121 $ignored = (!empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0);
124 if ($a->argc == 4 && $a->argv[2] == 'export') {
126 $format = $a->argv[3];
129 // Setup permissions structures
130 $owner_uid = intval($a->data['user']['uid']);
131 $nick = $a->data['user']['nickname'];
132 if (empty($a->profile)) {
133 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
136 $contact_id = Session::getRemoteContactID($a->profile['uid']);
138 $remote_contact = $contact_id && DBA::exists('contact', ['id' => $contact_id, 'uid' => $a->profile['uid']]);
140 $is_owner = local_user() == $a->profile['uid'];
142 if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
143 notice(DI::l10n()->t('Access to this profile has been restricted.'));
147 // get the permissions
148 $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
149 // we only want to have the events of the profile owner
150 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
152 // get the tab navigation bar
153 $tabs = BaseProfile::getTabsHTML($a, 'cal', false, $a->data['user']['nickname']);
155 // The view mode part is similiar to /mod/events.php
156 if ($mode == 'view') {
157 $thisyear = DateTimeFormat::localNow('Y');
158 $thismonth = DateTimeFormat::localNow('m');
160 $y = intval($thisyear);
164 $m = intval($thismonth);
167 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
168 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
180 if ($nextmonth > 12) {
193 $dim = Temporal::getDaysInMonth($y, $m);
194 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
195 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
198 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
199 if (!empty($_GET['start'])) {
200 $start = $_GET['start'];
203 if (!empty($_GET['end'])) {
204 $finish = $_GET['end'];
208 $start = DateTimeFormat::utc($start);
209 $finish = DateTimeFormat::utc($finish);
211 $adjust_start = DateTimeFormat::local($start);
212 $adjust_finish = DateTimeFormat::local($finish);
214 // put the event parametes in an array so we can better transmit them
216 'event_id' => intval($_GET['id'] ?? 0),
219 'adjust_start' => $adjust_start,
220 'adjust_finish' => $adjust_finish,
221 'ignore' => $ignored,
224 // get events by id or by date
225 if ($event_params['event_id']) {
226 $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
228 $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
233 if (DBA::isResult($r)) {
234 $r = Event::sortByDate($r);
235 foreach ($r as $rr) {
236 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
237 if (empty($links[$j])) {
238 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
243 // transform the event in a usable array
244 $events = Event::prepareListForTemplate($r);
246 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
247 echo json_encode($events);
251 // links: array('href', 'text', 'extra css classes', 'title')
252 if (!empty($_GET['id'])) {
253 $tpl = Renderer::getMarkupTemplate("event.tpl");
255 // if (DI::config()->get('experimentals','new_calendar')==1){
256 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
258 // $tpl = Renderer::getMarkupTemplate("events.tpl");
262 // Get rid of dashes in key names, Smarty3 can't handle them
263 foreach ($events as $key => $event) {
265 foreach ($event['item'] as $k => $v) {
266 $k = str_replace('-', '_', $k);
267 $event_item[$k] = $v;
269 $events[$key]['item'] = $event_item;
272 $o = Renderer::replaceMacros($tpl, [
274 '$title' => DI::l10n()->t('Events'),
275 '$view' => DI::l10n()->t('View'),
276 '$previous' => [DI::baseUrl() . "/events/$prevyear/$prevmonth", DI::l10n()->t('Previous'), '', ''],
277 '$next' => [DI::baseUrl() . "/events/$nextyear/$nextmonth", DI::l10n()->t('Next'), '', ''],
278 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
279 '$events' => $events,
280 "today" => DI::l10n()->t("today"),
281 "month" => DI::l10n()->t("month"),
282 "week" => DI::l10n()->t("week"),
283 "day" => DI::l10n()->t("day"),
284 "list" => DI::l10n()->t("list"),
287 if (!empty($_GET['id'])) {
295 if ($mode == 'export') {
297 notice(DI::l10n()->t('User not found'));
301 // Get the export data by uid
302 $evexport = Event::exportListByUserId($owner_uid, $format);
304 if (!$evexport["success"]) {
305 if ($evexport["content"]) {
306 notice(DI::l10n()->t('This calendar format is not supported'));
308 notice(DI::l10n()->t('No exportable data found'));
311 // If it the own calendar return to the events page
312 // otherwise to the profile calendar page
313 if (local_user() === $owner_uid) {
314 $return_path = "events";
316 $return_path = "cal/" . $nick;
319 DI::baseUrl()->redirect($return_path);
322 // If nothing went wrong we can echo the export content
323 if ($evexport["success"]) {
324 header('Content-type: text/calendar');
325 header('content-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
326 echo $evexport["content"];