]> git.mxchange.org Git - friendica.git/blob - mod/cal.php
New function "Item::storeForUserByUriId"
[friendica.git] / mod / cal.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\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;
34 use Friendica\DI;
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;
43
44 function cal_init(App $a)
45 {
46         if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
47                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
48         }
49
50         if ($a->argc < 2) {
51                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
52         }
53
54         Nav::setSelected('events');
55
56         $nick = $a->argv[1];
57         $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
58         if (!DBA::isResult($user)) {
59                 throw new HTTPException\NotFoundException();
60         }
61
62         $a->data['user'] = $user;
63         $a->profile_uid = $user['uid'];
64
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')) {
68                 return;
69         }
70
71         $a->profile = Profile::getByNickname($nick, $a->profile_uid);
72
73         if (empty($a->profile)) {
74                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
75         }
76
77         $account_type = Contact::getAccountType($a->profile);
78
79         $tpl = Renderer::getMarkupTemplate('widget/vcard.tpl');
80
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']),
87         ]);
88
89         $cal_widget = Widget\CalendarExport::getHTML();
90
91         if (empty(DI::page()['aside'])) {
92                 DI::page()['aside'] = '';
93         }
94
95         DI::page()['aside'] .= $vcard_widget;
96         DI::page()['aside'] .= $cal_widget;
97
98         return;
99 }
100
101 function cal_content(App $a)
102 {
103         Nav::setSelected('events');
104
105         // get the translation strings for the callendar
106         $i18n = Event::getStrings();
107
108         $htpl = Renderer::getMarkupTemplate('event_head.tpl');
109         DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
110                 '$module_url' => '/cal/' . $a->data['user']['nickname'],
111                 '$modparams' => 2,
112                 '$i18n' => $i18n,
113         ]);
114
115         $mode = 'view';
116         $y = 0;
117         $m = 0;
118         $ignored = (!empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0);
119
120         $format = 'ical';
121         if ($a->argc == 4 && $a->argv[2] == 'export') {
122                 $mode = 'export';
123                 $format = $a->argv[3];
124         }
125
126         // Setup permissions structures
127         $owner_uid = intval($a->data['user']['uid']);
128         $nick = $a->data['user']['nickname'];
129
130         $contact_id = Session::getRemoteContactID($a->profile['uid']);
131
132         $remote_contact = $contact_id && DBA::exists('contact', ['id' => $contact_id, 'uid' => $a->profile['uid']]);
133
134         $is_owner = local_user() == $a->profile['uid'];
135
136         if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
137                 notice(DI::l10n()->t('Access to this profile has been restricted.') . EOL);
138                 return;
139         }
140
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;
145
146         // get the tab navigation bar
147         $tabs = BaseProfile::getTabsHTML($a, 'cal', false, $a->data['user']['nickname']);
148
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');
153                 if (!$y) {
154                         $y = intval($thisyear);
155                 }
156
157                 if (!$m) {
158                         $m = intval($thismonth);
159                 }
160
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.
163
164                 if ($y < 1901) {
165                         $y = 1900;
166                 }
167
168                 if ($y > 2099) {
169                         $y = 2100;
170                 }
171
172                 $nextyear = $y;
173                 $nextmonth = $m + 1;
174                 if ($nextmonth > 12) {
175                         $nextmonth = 1;
176                         $nextyear ++;
177                 }
178
179                 $prevyear = $y;
180                 if ($m > 1) {
181                         $prevmonth = $m - 1;
182                 } else {
183                         $prevmonth = 12;
184                         $prevyear --;
185                 }
186
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);
190
191
192                 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
193                         if (!empty($_GET['start'])) {
194                                 $start = $_GET['start'];
195                         }
196
197                         if (!empty($_GET['end'])) {
198                                 $finish = $_GET['end'];
199                         }
200                 }
201
202                 $start = DateTimeFormat::utc($start);
203                 $finish = DateTimeFormat::utc($finish);
204
205                 $adjust_start = DateTimeFormat::local($start);
206                 $adjust_finish = DateTimeFormat::local($finish);
207
208                 // put the event parametes in an array so we can better transmit them
209                 $event_params = [
210                         'event_id'      => intval($_GET['id'] ?? 0),
211                         'start'         => $start,
212                         'finish'        => $finish,
213                         'adjust_start'  => $adjust_start,
214                         'adjust_finish' => $adjust_finish,
215                         'ignore'        => $ignored,
216                 ];
217
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);
221                 } else {
222                         $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
223                 }
224
225                 $links = [];
226
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;
233                                 }
234                         }
235                 }
236
237                 // transform the event in a usable array
238                 $events = Event::prepareListForTemplate($r);
239
240                 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
241                         echo json_encode($events);
242                         exit();
243                 }
244
245                 // links: array('href', 'text', 'extra css classes', 'title')
246                 if (!empty($_GET['id'])) {
247                         $tpl = Renderer::getMarkupTemplate("event.tpl");
248                 } else {
249 //                      if (DI::config()->get('experimentals','new_calendar')==1){
250                         $tpl = Renderer::getMarkupTemplate("events_js.tpl");
251 //                      } else {
252 //                              $tpl = Renderer::getMarkupTemplate("events.tpl");
253 //                      }
254                 }
255
256                 // Get rid of dashes in key names, Smarty3 can't handle them
257                 foreach ($events as $key => $event) {
258                         $event_item = [];
259                         foreach ($event['item'] as $k => $v) {
260                                 $k = str_replace('-', '_', $k);
261                                 $event_item[$k] = $v;
262                         }
263                         $events[$key]['item'] = $event_item;
264                 }
265
266                 $o = Renderer::replaceMacros($tpl, [
267                         '$tabs' => $tabs,
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"),
279                 ]);
280
281                 if (!empty($_GET['id'])) {
282                         echo $o;
283                         exit();
284                 }
285
286                 return $o;
287         }
288
289         if ($mode == 'export') {
290                 if (!$owner_uid) {
291                         notice(DI::l10n()->t('User not found'));
292                         return;
293                 }
294
295                 // Get the export data by uid
296                 $evexport = Event::exportListByUserId($owner_uid, $format);
297
298                 if (!$evexport["success"]) {
299                         if ($evexport["content"]) {
300                                 notice(DI::l10n()->t('This calendar format is not supported'));
301                         } else {
302                                 notice(DI::l10n()->t('No exportable data found'));
303                         }
304
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";
309                         } else {
310                                 $return_path = "cal/" . $nick;
311                         }
312
313                         DI::baseUrl()->redirect($return_path);
314                 }
315
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"];
321                         exit();
322                 }
323
324                 return;
325         }
326 }