]> git.mxchange.org Git - friendica.git/blob - src/Module/Calendar/Event/Form.php
The last PHPCS error ..
[friendica.git] / src / Module / Calendar / Event / Form.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  */
21
22 namespace Friendica\Module\Calendar\Event;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Content\Widget\CalendarExport;
27 use Friendica\Core\ACL;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Session\Capability\IHandleUserSessions;
31 use Friendica\Model\Event as EventModel;
32 use Friendica\Model\User;
33 use Friendica\Module\Response;
34 use Friendica\Module\Security\Login;
35 use Friendica\Navigation\SystemMessages;
36 use Friendica\Network\HTTPException\BadRequestException;
37 use Friendica\Util\ACLFormatter;
38 use Friendica\Util\DateTimeFormat;
39 use Friendica\Util\Profiler;
40 use Friendica\Util\Temporal;
41 use Psr\Log\LoggerInterface;
42
43 /**
44  * The editor-view of an event
45  */
46 class Form extends BaseModule
47 {
48         const MODE_NEW  = 'new';
49         const MODE_EDIT = 'edit';
50         const MODE_COPY = 'copy';
51
52         const ALLOWED_MODES = [
53                 self::MODE_NEW,
54                 self::MODE_EDIT,
55                 self::MODE_COPY,
56         ];
57
58         /** @var IHandleUserSessions */
59         protected $session;
60         /** @var SystemMessages */
61         protected $sysMessages;
62         /** @var ACLFormatter */
63         protected $aclFormatter;
64         /** @var App\Page */
65         protected $page;
66
67         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $session, SystemMessages $sysMessages, ACLFormatter $aclFormatter, App\Page $page, array $server, array $parameters = [])
68         {
69                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
70
71                 $this->session      = $session;
72                 $this->sysMessages  = $sysMessages;
73                 $this->aclFormatter = $aclFormatter;
74                 $this->page         = $page;
75         }
76
77         protected function content(array $request = []): string
78         {
79                 if (empty($this->parameters['mode']) || !in_array($this->parameters['mode'], self::ALLOWED_MODES)) {
80                         throw new BadRequestException($this->t('Invalid Request'));
81                 }
82
83                 if (!$this->session->getLocalUserId()) {
84                         $this->sysMessages->addNotice($this->t('Permission denied.'));
85                         return Login::form();
86                 }
87
88                 $mode = $this->parameters['mode'];
89
90                 if (($mode === self::MODE_EDIT || $mode === self::MODE_COPY)) {
91                         if (empty($this->parameters['id'])) {
92                                 throw new BadRequestException('Invalid Request');
93                         }
94                         $orig_event = EventModel::getByIdAndUid($this->session->getLocalUserId(), $this->parameters['id']);
95                         if (empty($orig_event)) {
96                                 throw new BadRequestException('Invalid Request');
97                         }
98                 }
99
100                 if ($mode === self::MODE_NEW) {
101                         $this->session->set('return_path', $this->args->getCommand());
102                 }
103
104                 // get the translation strings for the calendar
105                 $i18n = EventModel::getStrings();
106
107                 $this->page->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.min.css');
108                 $this->page->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.print.min.css', 'print');
109                 $this->page->registerFooterScript('view/asset/moment/min/moment-with-locales.min.js');
110                 $this->page->registerFooterScript('view/asset/fullcalendar/dist/fullcalendar.min.js');
111
112                 $htpl = Renderer::getMarkupTemplate('calendar/calendar_head.tpl');
113
114                 $this->page['htmlhead'] .= Renderer::replaceMacros($htpl, [
115                         '$calendar_api' => $this->baseUrl . '/calendar/api/get',
116                         '$event_api'    => $this->baseUrl . '/calendar/event/show',
117                         '$modparams'    => 2,
118                         '$i18n'         => $i18n,
119                 ]);
120
121                 $share_checked  = '';
122                 $share_disabled = '';
123
124                 if (empty($orig_event)) {
125                         $orig_event = User::getById($this->session->getLocalUserId(),
126                                 ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']);
127                 } elseif ($orig_event['allow_cid'] !== '<' . $this->session->getLocalUserId() . '>'
128                                    || $orig_event['allow_gid']
129                                    || $orig_event['deny_cid']
130                                    || $orig_event['deny_gid']) {
131                         $share_checked = ' checked="checked" ';
132                 }
133
134                 // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
135                 if (!empty($request['nofinish'])) {
136                         $orig_event['nofinish'] = $request['nofinish'];
137                 }
138                 if (!empty($request['summary'])) {
139                         $orig_event['summary'] = $request['summary'];
140                 }
141                 if (!empty($request['desc'])) {
142                         $orig_event['desc'] = $request['desc'];
143                 }
144                 if (!empty($request['location'])) {
145                         $orig_event['location'] = $request['location'];
146                 }
147                 if (!empty($request['start'])) {
148                         $orig_event['start'] = $request['start'];
149                 }
150                 if (!empty($request['finish'])) {
151                         $orig_event['finish'] = $request['finish'];
152                 }
153
154                 $n_checked = (!empty($orig_event['nofinish']) ? ' checked="checked" ' : '');
155
156                 $t_orig = $orig_event['summary']  ?? '';
157                 $d_orig = $orig_event['desc']     ?? '';
158                 $l_orig = $orig_event['location'] ?? '';
159                 $eid    = $orig_event['id']       ?? 0;
160                 $cid    = $orig_event['cid']      ?? 0;
161                 $uri    = $orig_event['uri']      ?? '';
162
163                 if ($cid || $mode === 'edit') {
164                         $share_disabled = 'disabled="disabled"';
165                 }
166
167                 $sdt = $orig_event['start']  ?? 'now';
168                 $fdt = $orig_event['finish'] ?? 'now';
169
170                 $syear  = DateTimeFormat::local($sdt, 'Y');
171                 $smonth = DateTimeFormat::local($sdt, 'm');
172                 $sday   = DateTimeFormat::local($sdt, 'd');
173
174                 $shour   = !empty($orig_event) ? DateTimeFormat::local($sdt, 'H') : '00';
175                 $sminute = !empty($orig_event) ? DateTimeFormat::local($sdt, 'i') : '00';
176
177                 $fyear  = DateTimeFormat::local($fdt, 'Y');
178                 $fmonth = DateTimeFormat::local($fdt, 'm');
179                 $fday   = DateTimeFormat::local($fdt, 'd');
180
181                 $fhour   = !empty($orig_event) ? DateTimeFormat::local($fdt, 'H') : '00';
182                 $fminute = !empty($orig_event) ? DateTimeFormat::local($fdt, 'i') : '00';
183
184                 if (!$cid && in_array($mode, [self::MODE_NEW, self::MODE_COPY])) {
185                         $acl = ACL::getFullSelectorHTML($this->page, $this->session->getLocalUserId(), false, ACL::getDefaultUserPermissions($orig_event));
186                 } else {
187                         $acl = '';
188                 }
189
190                 // If we copy an old event, we need to remove the ID and URI
191                 // from the original event.
192                 if ($mode === self::MODE_COPY) {
193                         $eid = 0;
194                         $uri = '';
195                 }
196
197                 $this->page['aside'] .= CalendarExport::getHTML($this->session->getLocalUserId());
198
199                 $tpl = Renderer::getMarkupTemplate('calendar/event_form.tpl');
200
201                 return Renderer::replaceMacros($tpl, [
202                         '$post' => 'calendar/api/create',
203                         '$eid'  => $eid,
204                         '$cid'  => $cid,
205                         '$uri'  => $uri,
206
207                         '$title'  => $this->t('Event details'),
208                         '$desc'   => $this->t('Starting date and Title are required.'),
209                         '$s_text' => $this->t('Event Starts:') . ' <span class="required" title="' . $this->t('Required') . '">*</span>',
210                         '$s_dsel' => Temporal::getDateTimeField(
211                                 new \DateTime(),
212                                 \DateTime::createFromFormat('Y', intval($syear) + 5),
213                                 \DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"),
214                                 $this->t('Event Starts:'),
215                                 'start_text',
216                                 true,
217                                 true,
218                                 '',
219                                 '',
220                                 true
221                         ),
222
223                         '$n_text'    => $this->t('Finish date/time is not known or not relevant'),
224                         '$n_checked' => $n_checked,
225                         '$f_text'    => $this->t('Event Finishes:'),
226                         '$f_dsel'    => Temporal::getDateTimeField(
227                                 new \DateTime(),
228                                 \DateTime::createFromFormat('Y', intval($fyear) + 5),
229                                 \DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"),
230                                 $this->t('Event Finishes:'),
231                                 'finish_text',
232                                 true,
233                                 true,
234                                 'start_text'
235                         ),
236
237                         '$d_text'      => $this->t('Description:'),
238                         '$d_orig'      => $d_orig,
239                         '$l_text'      => $this->t('Location:'),
240                         '$l_orig'      => $l_orig,
241                         '$t_text'      => $this->t('Title:') . ' <span class="required" title="' . $this->t('Required') . '">*</span>',
242                         '$t_orig'      => $t_orig,
243                         '$summary'     => ['summary', $this->t('Title:'), $t_orig, '', '*'],
244                         '$sh_text'     => $this->t('Share this event'),
245                         '$share'       => ['share', $this->t('Share this event'), $share_checked, '', $share_disabled],
246                         '$sh_checked'  => $share_checked,
247                         '$nofinish'    => ['nofinish', $this->t('Finish date/time is not known or not relevant'), $n_checked],
248                         '$preview'     => $this->t('Preview'),
249                         '$acl'         => $acl,
250                         '$submit'      => $this->t('Submit'),
251                         '$basic'       => $this->t('Basic'),
252                         '$advanced'    => $this->t('Advanced'),
253                         '$permissions' => $this->t('Permissions'),
254                 ]);
255         }
256 }