]> git.mxchange.org Git - friendica.git/blob - mod/cal.php
Unused use removed
[friendica.git] / mod / cal.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  * The calendar module
21  *
22  * This calendar is for profile visitors and contains only the events
23  * of the profile owner
24  */
25
26 use Friendica\App;
27 use Friendica\Content\Nav;
28 use Friendica\Content\Widget;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\System;
31 use Friendica\Database\DBA;
32 use Friendica\DI;
33 use Friendica\Model\Event;
34 use Friendica\Model\Item;
35 use Friendica\Model\User;
36 use Friendica\Module\BaseProfile;
37 use Friendica\Network\HTTPException;
38 use Friendica\Util\DateTimeFormat;
39 use Friendica\Util\Temporal;
40
41 function cal_init(App $a)
42 {
43         if (DI::config()->get('system', 'block_public') && !DI::userSession()->isAuthenticated()) {
44                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
45         }
46
47         if (DI::args()->getArgc() < 2) {
48                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
49         }
50
51         Nav::setSelected('events');
52
53         // if it's a json request abort here becaus we don't
54         // need the widget data
55         if (!empty(DI::args()->getArgv()[2]) && (DI::args()->getArgv()[2] === 'json')) {
56                 return;
57         }
58
59         $owner = User::getOwnerDataByNick(DI::args()->getArgv()[1]);
60         if (empty($owner)) {
61                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
62         }
63
64         if (empty(DI::page()['aside'])) {
65                 DI::page()['aside'] = '';
66         }
67
68         DI::page()['aside'] .= Widget\VCard::getHTML($owner);
69         DI::page()['aside'] .= Widget\CalendarExport::getHTML($owner['uid']);
70
71         return;
72 }
73
74 function cal_content(App $a)
75 {
76         $owner = User::getOwnerDataByNick(DI::args()->getArgv()[1]);
77         if (empty($owner)) {
78                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
79         }
80
81         Nav::setSelected('events');
82
83         // get the translation strings for the callendar
84         $i18n = Event::getStrings();
85
86         DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.min.css');
87         DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.print.min.css', 'print');
88         DI::page()->registerFooterScript('view/asset/moment/min/moment-with-locales.min.js');
89         DI::page()->registerFooterScript('view/asset/fullcalendar/dist/fullcalendar.min.js');
90
91         $htpl = Renderer::getMarkupTemplate('event_head.tpl');
92         DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
93                 '$module_url' => '/cal/' . $owner['nickname'],
94                 '$modparams' => 2,
95                 '$i18n' => $i18n,
96         ]);
97
98         $mode = 'view';
99         $y = 0;
100         $m = 0;
101         $ignored = (!empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0);
102
103         // Setup permissions structures
104         $owner_uid = intval($owner['uid']);
105
106         $contact_id = DI::userSession()->getRemoteContactID($owner['uid']);
107
108         $remote_contact = $contact_id && DBA::exists('contact', ['id' => $contact_id, 'uid' => $owner['uid']]);
109
110         $is_owner = DI::userSession()->getLocalUserId() == $owner['uid'];
111
112         if ($owner['hidewall'] && !$is_owner && !$remote_contact) {
113                 DI::sysmsg()->addNotice(DI::l10n()->t('Access to this profile has been restricted.'));
114                 return;
115         }
116
117         // get the permissions
118         $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
119         // we only want to have the events of the profile owner
120         $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
121
122         // get the tab navigation bar
123         $tabs = BaseProfile::getTabsHTML($a, 'cal', false, $owner['nickname'], $owner['hide-friends']);
124
125         // The view mode part is similiar to /mod/events.php
126         if ($mode == 'view') {
127                 $thisyear = DateTimeFormat::localNow('Y');
128                 $thismonth = DateTimeFormat::localNow('m');
129                 if (!$y) {
130                         $y = intval($thisyear);
131                 }
132
133                 if (!$m) {
134                         $m = intval($thismonth);
135                 }
136
137                 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
138                 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
139
140                 if ($y < 1901) {
141                         $y = 1900;
142                 }
143
144                 if ($y > 2099) {
145                         $y = 2100;
146                 }
147
148                 $nextyear = $y;
149                 $nextmonth = $m + 1;
150                 if ($nextmonth > 12) {
151                         $nextmonth = 1;
152                         $nextyear ++;
153                 }
154
155                 $prevyear = $y;
156                 if ($m > 1) {
157                         $prevmonth = $m - 1;
158                 } else {
159                         $prevmonth = 12;
160                         $prevyear --;
161                 }
162
163                 $dim = Temporal::getDaysInMonth($y, $m);
164                 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
165                 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
166
167
168                 if (!empty(DI::args()->getArgv()[2]) && (DI::args()->getArgv()[2] === 'json')) {
169                         if (!empty($_GET['start'])) {
170                                 $start = $_GET['start'];
171                         }
172
173                         if (!empty($_GET['end'])) {
174                                 $finish = $_GET['end'];
175                         }
176                 }
177
178                 $start = DateTimeFormat::utc($start);
179                 $finish = DateTimeFormat::utc($finish);
180
181                 // put the event parametes in an array so we can better transmit them
182                 $event_params = [
183                         'event_id'      => intval($_GET['id'] ?? 0),
184                         'start'         => $start,
185                         'finish'        => $finish,
186                         'ignore'        => $ignored,
187                 ];
188
189                 // get events by id or by date
190                 if ($event_params['event_id']) {
191                         $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
192                 } else {
193                         $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
194                 }
195
196                 $links = [];
197
198                 if (DBA::isResult($r)) {
199                         $r = Event::sortByDate($r);
200                         foreach ($r as $rr) {
201                                 $j = DateTimeFormat::local($rr['start'], 'j');
202                                 if (empty($links[$j])) {
203                                         $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
204                                 }
205                         }
206                 }
207
208                 // transform the event in a usable array
209                 $events = Event::prepareListForTemplate($r);
210
211                 if (!empty(DI::args()->getArgv()[2]) && (DI::args()->getArgv()[2] === 'json')) {
212                         System::jsonExit($events);
213                 }
214
215                 // links: array('href', 'text', 'extra css classes', 'title')
216                 if (!empty($_GET['id'])) {
217                         $tpl = Renderer::getMarkupTemplate("event.tpl");
218                 } else {
219                         $tpl = Renderer::getMarkupTemplate("events_js.tpl");
220                 }
221
222                 // Get rid of dashes in key names, Smarty3 can't handle them
223                 foreach ($events as $key => $event) {
224                         $event_item = [];
225                         foreach ($event['item'] as $k => $v) {
226                                 $k = str_replace('-', '_', $k);
227                                 $event_item[$k] = $v;
228                         }
229                         $events[$key]['item'] = $event_item;
230                 }
231
232                 $o = Renderer::replaceMacros($tpl, [
233                         '$tabs' => $tabs,
234                         '$title' => DI::l10n()->t('Events'),
235                         '$view' => DI::l10n()->t('View'),
236                         '$previous' => [DI::baseUrl() . "/events/$prevyear/$prevmonth", DI::l10n()->t('Previous'), '', ''],
237                         '$next' => [DI::baseUrl() . "/events/$nextyear/$nextmonth", DI::l10n()->t('Next'), '', ''],
238                         '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
239                         '$events' => $events,
240                         "today" => DI::l10n()->t("today"),
241                         "month" => DI::l10n()->t("month"),
242                         "week" => DI::l10n()->t("week"),
243                         "day" => DI::l10n()->t("day"),
244                         "list" => DI::l10n()->t("list"),
245                 ]);
246
247                 if (!empty($_GET['id'])) {
248                         System::httpExit($o);
249                 }
250
251                 return $o;
252         }
253 }