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 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
109 DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
110 '$module_url' => '/cal/' . $a->data['user']['nickname'],
118 $ignored = (!empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0);
121 if ($a->argc == 4 && $a->argv[2] == 'export') {
123 $format = $a->argv[3];
126 // Setup permissions structures
127 $owner_uid = intval($a->data['user']['uid']);
128 $nick = $a->data['user']['nickname'];
130 $contact_id = Session::getRemoteContactID($a->profile['uid']);
132 $remote_contact = $contact_id && DBA::exists('contact', ['id' => $contact_id, 'uid' => $a->profile['uid']]);
134 $is_owner = local_user() == $a->profile['uid'];
136 if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
137 notice(DI::l10n()->t('Access to this profile has been restricted.'));
141 // get the permissions
142 $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
143 // we only want to have the events of the profile owner
144 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
146 // get the tab navigation bar
147 $tabs = BaseProfile::getTabsHTML($a, 'cal', false, $a->data['user']['nickname']);
149 // The view mode part is similiar to /mod/events.php
150 if ($mode == 'view') {
151 $thisyear = DateTimeFormat::localNow('Y');
152 $thismonth = DateTimeFormat::localNow('m');
154 $y = intval($thisyear);
158 $m = intval($thismonth);
161 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
162 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
174 if ($nextmonth > 12) {
187 $dim = Temporal::getDaysInMonth($y, $m);
188 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
189 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
192 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
193 if (!empty($_GET['start'])) {
194 $start = $_GET['start'];
197 if (!empty($_GET['end'])) {
198 $finish = $_GET['end'];
202 $start = DateTimeFormat::utc($start);
203 $finish = DateTimeFormat::utc($finish);
205 $adjust_start = DateTimeFormat::local($start);
206 $adjust_finish = DateTimeFormat::local($finish);
208 // put the event parametes in an array so we can better transmit them
210 'event_id' => intval($_GET['id'] ?? 0),
213 'adjust_start' => $adjust_start,
214 'adjust_finish' => $adjust_finish,
215 'ignore' => $ignored,
218 // get events by id or by date
219 if ($event_params['event_id']) {
220 $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
222 $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
227 if (DBA::isResult($r)) {
228 $r = Event::sortByDate($r);
229 foreach ($r as $rr) {
230 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
231 if (empty($links[$j])) {
232 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
237 // transform the event in a usable array
238 $events = Event::prepareListForTemplate($r);
240 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
241 echo json_encode($events);
245 // links: array('href', 'text', 'extra css classes', 'title')
246 if (!empty($_GET['id'])) {
247 $tpl = Renderer::getMarkupTemplate("event.tpl");
249 // if (DI::config()->get('experimentals','new_calendar')==1){
250 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
252 // $tpl = Renderer::getMarkupTemplate("events.tpl");
256 // Get rid of dashes in key names, Smarty3 can't handle them
257 foreach ($events as $key => $event) {
259 foreach ($event['item'] as $k => $v) {
260 $k = str_replace('-', '_', $k);
261 $event_item[$k] = $v;
263 $events[$key]['item'] = $event_item;
266 $o = Renderer::replaceMacros($tpl, [
268 '$title' => DI::l10n()->t('Events'),
269 '$view' => DI::l10n()->t('View'),
270 '$previous' => [DI::baseUrl() . "/events/$prevyear/$prevmonth", DI::l10n()->t('Previous'), '', ''],
271 '$next' => [DI::baseUrl() . "/events/$nextyear/$nextmonth", DI::l10n()->t('Next'), '', ''],
272 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
273 '$events' => $events,
274 "today" => DI::l10n()->t("today"),
275 "month" => DI::l10n()->t("month"),
276 "week" => DI::l10n()->t("week"),
277 "day" => DI::l10n()->t("day"),
278 "list" => DI::l10n()->t("list"),
281 if (!empty($_GET['id'])) {
289 if ($mode == 'export') {
291 notice(DI::l10n()->t('User not found'));
295 // Get the export data by uid
296 $evexport = Event::exportListByUserId($owner_uid, $format);
298 if (!$evexport["success"]) {
299 if ($evexport["content"]) {
300 notice(DI::l10n()->t('This calendar format is not supported'));
302 notice(DI::l10n()->t('No exportable data found'));
305 // If it the own calendar return to the events page
306 // otherwise to the profile calendar page
307 if (local_user() === $owner_uid) {
308 $return_path = "events";
310 $return_path = "cal/" . $nick;
313 DI::baseUrl()->redirect($return_path);
316 // If nothing went wrong we can echo the export content
317 if ($evexport["success"]) {
318 header('Content-type: text/calendar');
319 header('content-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
320 echo $evexport["content"];