[BUGFIX] Hide duplicate button if sys_file_meta is edited
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / EditDocumentController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Controller;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Backend\Form\Exception\AccessDeniedException;
21 use TYPO3\CMS\Backend\Form\Exception\DatabaseRecordException;
22 use TYPO3\CMS\Backend\Form\FormDataCompiler;
23 use TYPO3\CMS\Backend\Form\FormDataGroup\TcaDatabaseRecord;
24 use TYPO3\CMS\Backend\Form\FormResultCompiler;
25 use TYPO3\CMS\Backend\Form\NodeFactory;
26 use TYPO3\CMS\Backend\Form\Utility\FormEngineUtility;
27 use TYPO3\CMS\Backend\Routing\UriBuilder;
28 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
29 use TYPO3\CMS\Backend\Template\ModuleTemplate;
30 use TYPO3\CMS\Backend\Utility\BackendUtility;
31 use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
32 use TYPO3\CMS\Core\Database\ConnectionPool;
33 use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
34 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
35 use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
36 use TYPO3\CMS\Core\DataHandling\DataHandler;
37 use TYPO3\CMS\Core\Http\HtmlResponse;
38 use TYPO3\CMS\Core\Http\RedirectResponse;
39 use TYPO3\CMS\Core\Imaging\Icon;
40 use TYPO3\CMS\Core\Messaging\FlashMessage;
41 use TYPO3\CMS\Core\Messaging\FlashMessageService;
42 use TYPO3\CMS\Core\Page\PageRenderer;
43 use TYPO3\CMS\Core\Type\Bitmask\Permission;
44 use TYPO3\CMS\Core\Utility\GeneralUtility;
45 use TYPO3\CMS\Core\Utility\HttpUtility;
46 use TYPO3\CMS\Core\Utility\MathUtility;
47 use TYPO3\CMS\Core\Utility\PathUtility;
48 use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
49 use TYPO3\CMS\Frontend\Page\CacheHashCalculator;
50 use TYPO3\CMS\Frontend\Page\PageRepository;
51
52 /**
53 * Main backend controller almost always used if some database record is edited in the backend.
54 *
55 * Main job of this controller is to evaluate and sanitize $request parameters,
56 * call the DataHandler if records should be created or updated and
57 * execute FormEngine for record rendering.
58 */
59 class EditDocumentController
60 {
61 use PublicPropertyDeprecationTrait;
62
63 /**
64 * @deprecated since v9. These constants will be set to protected in v10
65 */
66 public const DOCUMENT_CLOSE_MODE_DEFAULT = 0;
67 public const DOCUMENT_CLOSE_MODE_REDIRECT = 1; // works like DOCUMENT_CLOSE_MODE_DEFAULT
68 public const DOCUMENT_CLOSE_MODE_CLEAR_ALL = 3;
69 public const DOCUMENT_CLOSE_MODE_NO_REDIRECT = 4;
70
71 /**
72 * Properties which have been moved to protected status from public
73 *
74 * @var array
75 */
76 protected $deprecatedPublicProperties = [
77 'editconf' => 'Using $editconf of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
78 'defVals' => 'Using $defVals of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
79 'overrideVals' => 'Using $overrideVals of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
80 'columnsOnly' => 'Using $columnsOnly of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
81 'returnUrl' => 'Using $returnUrl of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
82 'closeDoc' => 'Using $closeDoc of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
83 'doSave' => 'Using $doSave of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
84 'returnEditConf' => 'Using $returnEditConf of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
85 'uc' => 'Using $uc of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
86 'retUrl' => 'Using $retUrl of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
87 'R_URL_parts' => 'Using $R_URL_parts of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
88 'R_URL_getvars' => 'Using $R_URL_getvars of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
89 'storeArray' => 'Using $storeArray of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
90 'storeUrl' => 'Using $storeUrl of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
91 'storeUrlMd5' => 'Using $storeUrlMd5 of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
92 'docDat' => 'Using $docDat of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
93 'docHandler' => 'Using $docHandler of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
94 'cmd' => 'Using $cmd of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
95 'mirror' => 'Using $mirror of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
96 'cacheCmd' => 'Using $cacheCmd of class EditDocumentTemplate from the outside is discouraged, the variable will be removed.',
97 'redirect' => 'Using $redirect of class EditDocumentTemplate from the outside is discouraged, the variable will be removed.',
98 'returnNewPageId' => 'Using $returnNewPageId of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
99 'popViewId' => 'Using $popViewId of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
100 'popViewId_addParams' => 'Using $popViewId_addParams of class EditDocumentTemplate from the outside is discouraged, the variable will be removed.',
101 'viewUrl' => 'Using $viewUrl of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
102 'recTitle' => 'Using $recTitle of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
103 'noView' => 'Using $noView of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
104 'MCONF' => 'Using $MCONF of class EditDocumentTemplate from the outside is discouraged, the variable will be removed.',
105 'doc' => 'Using $doc of class EditDocumentTemplate from the outside is discouraged, the variable will be removed.',
106 'perms_clause' => 'Using $perms_clause of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
107 'template' => 'Using $template of class EditDocumentTemplate from the outside is discouraged, the variable will be removed.',
108 'content' => 'Using $content of class EditDocumentTemplate from the outside is discouraged, the variable will be removed.',
109 'R_URI' => 'Using $R_URI of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
110 'pageinfo' => 'Using $pageinfo of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
111 'storeTitle' => 'Using $storeTitle of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
112 'firstEl' => 'Using $firstEl of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
113 'errorC' => 'Using $errorC of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
114 'newC' => 'Using $newC of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
115 'viewId' => 'Using $viewId of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
116 'viewId_addParams' => 'Using $viewId_addParams of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
117 'modTSconfig' => 'Using $modTSconfig of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
118 'dontStoreDocumentRef' => 'Using $dontStoreDocumentRef of class EditDocumentTemplate from the outside is discouraged, as this variable is only used for internal storage.',
119 ];
120
121 /**
122 * An array looking approx like [tablename][list-of-ids]=command, eg. "&edit[pages][123]=edit".
123 *
124 * @see \TYPO3\CMS\Backend\Utility\BackendUtility::editOnClick()
125 * @var array
126 */
127 protected $editconf = [];
128
129 /**
130 * Comma list of field names to edit. If specified, only those fields will be rendered.
131 * Otherwise all (available) fields in the record are shown according to the TCA type.
132 *
133 * @var string|null
134 */
135 protected $columnsOnly;
136
137 /**
138 * Default values for fields
139 *
140 * @var array|null [table][field]
141 */
142 protected $defVals;
143
144 /**
145 * Array of values to force being set as hidden fields in FormEngine
146 *
147 * @var array|null [table][field]
148 */
149 protected $overrideVals;
150
151 /**
152 * If set, this value will be set in $this->retUrl as "returnUrl", if not,
153 * $this->retUrl will link to dummy controller
154 *
155 * @var string|null
156 */
157 protected $returnUrl;
158
159 /**
160 * Prepared return URL. Contains the URL that we should return to from FormEngine if
161 * close button is clicked. Usually passed along as 'returnUrl', but falls back to
162 * "dummy" controller.
163 *
164 * @var string
165 */
166 protected $retUrl;
167
168 /**
169 * Close document command. One of the DOCUMENT_CLOSE_MODE_* constants above
170 *
171 * @var int
172 */
173 protected $closeDoc;
174
175 /**
176 * If true, the processing of incoming data will be performed as if a save-button is pressed.
177 * Used in the forms as a hidden field which can be set through
178 * JavaScript if the form is somehow submitted by JavaScript.
179 *
180 * @var bool
181 */
182 protected $doSave;
183
184 /**
185 * Main DataHandler datamap array
186 *
187 * @var array
188 * @todo: Will be set protected later, still used by ConditionMatcher
189 * @internal Will be removed / protected in v10 without further notice
190 */
191 public $data;
192
193 /**
194 * Main DataHandler cmdmap array
195 *
196 * @var array
197 */
198 protected $cmd;
199
200 /**
201 * DataHandler 'mirror' input
202 *
203 * @var array
204 */
205 protected $mirror;
206
207 /**
208 * @var string
209 * @deprecated since v9, will be removed in v10, unused
210 */
211 protected $cacheCmd;
212
213 /**
214 * @var string
215 * @deprecated since v9, will be removed in v10, unused
216 */
217 protected $redirect;
218
219 /**
220 * Boolean: If set, then the GET var "&id=" will be added to the
221 * retUrl string so that the NEW id of something is returned to the script calling the form.
222 *
223 * @var bool
224 */
225 protected $returnNewPageId = false;
226
227 /**
228 * Updated values for backendUser->uc. Used for new inline records to mark them
229 * as expanded: uc[inlineView][...]
230 *
231 * @var array|null
232 */
233 protected $uc;
234
235 /**
236 * ID for displaying the page in the frontend, "save and view"
237 *
238 * @var int
239 */
240 protected $popViewId;
241
242 /**
243 * @var string
244 * @deprecated since v9, will be removed in v10, unused
245 */
246 protected $popViewId_addParams;
247
248 /**
249 * Alternative URL for viewing the frontend pages.
250 *
251 * @var string
252 */
253 protected $viewUrl;
254
255 /**
256 * Alternative title for the document handler.
257 *
258 * @var string
259 */
260 protected $recTitle;
261
262 /**
263 * If set, then no save & view button is printed
264 *
265 * @var bool
266 */
267 protected $noView;
268
269 /**
270 * @var string
271 */
272 protected $perms_clause;
273
274 /**
275 * If true, $this->editconf array is added a redirect response, used by Wizard/AddController
276 *
277 * @var bool
278 */
279 protected $returnEditConf;
280
281 /**
282 * Workspace used for the editing action.
283 *
284 * @var string|null
285 */
286 protected $workspace;
287
288 /**
289 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
290 * @deprecated since v9, will be removed in v10, unused
291 */
292 protected $doc;
293
294 /**
295 * @deprecated since v9, will be removed in v10, unused
296 */
297 protected $template;
298
299 /**
300 * @deprecated since v9, will be removed in v10, unused
301 */
302 protected $content;
303
304 /**
305 * parse_url() of current requested URI, contains ['path'] and ['query'] parts.
306 *
307 * @var array
308 */
309 protected $R_URL_parts;
310
311 /**
312 * Contains $request query parameters. This array is the foundation for creating
313 * the R_URI internal var which becomes the url to which forms are submitted
314 *
315 * @var array
316 */
317 protected $R_URL_getvars;
318
319 /**
320 * Set to the URL of this script including variables which is needed to re-display the form.
321 *
322 * @var string
323 */
324 protected $R_URI;
325
326 /**
327 * @var array
328 * @deprecated since v9, will be removed in v10, unused
329 */
330 protected $MCONF;
331
332 /**
333 * @var array
334 */
335 protected $pageinfo;
336
337 /**
338 * Is loaded with the "title" of the currently "open document"
339 * used for the open document toolbar
340 *
341 * @var string
342 */
343 protected $storeTitle = '';
344
345 /**
346 * Contains an array with key/value pairs of GET parameters needed to reach the
347 * current document displayed - used in the 'open documents' toolbar.
348 *
349 * @var array
350 */
351 protected $storeArray;
352
353 /**
354 * $this->storeArray imploded to url
355 *
356 * @var string
357 */
358 protected $storeUrl;
359
360 /**
361 * md5 hash of storeURL, used to identify a single open document in backend user uc
362 *
363 * @var string
364 */
365 protected $storeUrlMd5;
366
367 /**
368 * Backend user session data of this module
369 *
370 * @var array
371 */
372 protected $docDat;
373
374 /**
375 * An array of the "open documents" - keys are md5 hashes (see $storeUrlMd5) identifying
376 * the various documents on the GET parameter list needed to open it. The values are
377 * arrays with 0,1,2 keys with information about the document (see compileStoreData()).
378 * The docHandler variable is stored in the $docDat session data, key "0".
379 *
380 * @var array
381 */
382 protected $docHandler;
383
384 /**
385 * Array of the elements to create edit forms for.
386 *
387 * @var array
388 * @todo: Will be set protected later, still used by ConditionMatcher
389 * @internal Will be removed / protected in v10 without further notice
390 */
391 public $elementsData;
392
393 /**
394 * Pointer to the first element in $elementsData
395 *
396 * @var array
397 */
398 protected $firstEl;
399
400 /**
401 * Counter, used to count the number of errors (when users do not have edit permissions)
402 *
403 * @var int
404 */
405 protected $errorC;
406
407 /**
408 * Counter, used to count the number of new record forms displayed
409 *
410 * @var int
411 */
412 protected $newC;
413
414 /**
415 * Is set to the pid value of the last shown record - thus indicating which page to
416 * show when clicking the SAVE/VIEW button
417 *
418 * @var int
419 */
420 protected $viewId;
421
422 /**
423 * Is set to additional parameters (like "&L=xxx") if the record supports it.
424 *
425 * @var string
426 */
427 protected $viewId_addParams;
428
429 /**
430 * Module TSconfig, loaded from main() based on the page id value of viewId
431 *
432 * @var array
433 */
434 protected $modTSconfig;
435
436 /**
437 * @var FormResultCompiler
438 */
439 protected $formResultCompiler;
440
441 /**
442 * Used internally to disable the storage of the document reference (eg. new records)
443 *
444 * @var bool
445 */
446 protected $dontStoreDocumentRef = 0;
447
448 /**
449 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
450 */
451 protected $signalSlotDispatcher;
452
453 /**
454 * Stores information needed to preview the currently saved record
455 *
456 * @var array
457 */
458 protected $previewData = [];
459
460 /**
461 * ModuleTemplate object
462 *
463 * @var ModuleTemplate
464 */
465 protected $moduleTemplate;
466
467 /**
468 * Constructor
469 */
470 public function __construct()
471 {
472 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
473 $this->moduleTemplate->setUiBlock(true);
474 // @todo Used by TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching
475 $GLOBALS['SOBE'] = $this;
476 $this->getLanguageService()->includeLLFile('EXT:lang/Resources/Private/Language/locallang_alt_doc.xlf');
477 }
478
479 /**
480 * Main dispatcher entry method registered as "record_edit" end point
481 *
482 * @param ServerRequestInterface $request the current request
483 * @return ResponseInterface the response with the content
484 */
485 public function mainAction(ServerRequestInterface $request): ResponseInterface
486 {
487 // Unlock all locked records
488 BackendUtility::lockRecords();
489 if ($response = $this->preInit($request)) {
490 return $response;
491 }
492
493 // Process incoming data via DataHandler?
494 $parsedBody = $request->getParsedBody();
495 if ($this->doSave
496 || isset($parsedBody['_savedok'])
497 || isset($parsedBody['_saveandclosedok'])
498 || isset($parsedBody['_savedokview'])
499 || isset($parsedBody['_savedoknew'])
500 || isset($parsedBody['_duplicatedoc'])
501 || isset($parsedBody['_translation_savedok'])
502 || isset($parsedBody['_translation_savedokclear'])
503 ) {
504 if ($response = $this->processData($request)) {
505 return $response;
506 }
507 }
508
509 $this->init($request);
510 $this->main($request);
511
512 return new HtmlResponse($this->moduleTemplate->renderContent());
513 }
514
515 /**
516 * First initialization, always called, even before processData() executes DataHandler processing.
517 *
518 * @param ServerRequestInterface $request
519 * @return ResponseInterface|null Possible redirect response
520 */
521 public function preInit(ServerRequestInterface $request = null): ?ResponseInterface
522 {
523 if ($request === null) {
524 // Missing argument? This method must have been called from outside.
525 // Method will be protected and $request mandatory in v10, giving core freedom to move stuff around
526 // New v10 signature: "protected function preInit(ServerRequestInterface $request): ?ResponseInterface"
527 // @deprecated since TYPO3 v9, method argument $request will be set to mandatory
528 trigger_error('Method preInit() will be set to protected in v10. Do not call from other extension', E_USER_DEPRECATED);
529 $request = $GLOBALS['TYPO3_REQUEST'];
530 }
531
532 if ($response = $this->localizationRedirect($request)) {
533 return $response;
534 }
535
536 $parsedBody = $request->getParsedBody();
537 $queryParams = $request->getQueryParams();
538
539 $this->editconf = $parsedBody['edit'] ?? $queryParams['edit'] ?? [];
540 $this->defVals = $parsedBody['defVals'] ?? $queryParams['defVals'] ?? null;
541 $this->overrideVals = $parsedBody['overrideVals'] ?? $queryParams['overrideVals'] ?? null;
542 $this->columnsOnly = $parsedBody['columnsOnly'] ?? $queryParams['columnsOnly'] ?? null;
543 $this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? null);
544 $this->closeDoc = (int)($parsedBody['closeDoc'] ?? $queryParams['closeDoc'] ?? self::DOCUMENT_CLOSE_MODE_DEFAULT);
545 $this->doSave = (bool)($parsedBody['doSave'] ?? $queryParams['doSave'] ?? false);
546 $this->returnEditConf = (bool)($parsedBody['returnEditConf'] ?? $queryParams['returnEditConf'] ?? false);
547 $this->workspace = $parsedBody['workspace'] ?? $queryParams['workspace'] ?? null;
548 $this->uc = $parsedBody['uc'] ?? $queryParams['uc'] ?? null;
549
550 // Set overrideVals as default values if defVals does not exist.
551 // @todo: Why?
552 if (!is_array($this->defVals) && is_array($this->overrideVals)) {
553 $this->defVals = $this->overrideVals;
554 }
555
556 // Set final return URL
557 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
558 $this->retUrl = $this->returnUrl ?: (string)$uriBuilder->buildUriFromRoute('dummy');
559
560 // Change $this->editconf if versioning applies to any of the records
561 $this->fixWSversioningInEditConf();
562
563 // Prepare R_URL (request url)
564 $this->R_URL_parts = parse_url($request->getAttribute('normalizedParams')->getRequestUri());
565 $this->R_URL_getvars = $queryParams;
566 $this->R_URL_getvars['edit'] = $this->editconf;
567
568 // Prepare 'open documents' url, this is later modified again various times
569 $this->compileStoreData();
570 // Backend user session data of this module
571 $this->docDat = $this->getBackendUser()->getModuleData('FormEngine', 'ses');
572 $this->docHandler = $this->docDat[0];
573
574 // Close document if a request for closing the document has been sent
575 if ((int)$this->closeDoc > self::DOCUMENT_CLOSE_MODE_DEFAULT) {
576 if ($response = $this->closeDocument($this->closeDoc, $request)) {
577 return $response;
578 }
579 }
580
581 // Sets a temporary workspace, this request is based on
582 if ($this->workspace !== null) {
583 $this->getBackendUser()->setTemporaryWorkspace($this->workspace);
584 }
585
586 $this->emitFunctionAfterSignal('preInit', $request);
587
588 return null;
589 }
590
591 /**
592 * Detects, if a save command has been triggered.
593 *
594 * @return bool TRUE, then save the document (data submitted)
595 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10
596 */
597 public function doProcessData()
598 {
599 trigger_error('This method will be removed in TYPO3 v10.', E_USER_DEPRECATED);
600
601 $out = $this->doSave
602 || isset($_POST['_savedok'])
603 || isset($_POST['_saveandclosedok'])
604 || isset($_POST['_savedokview'])
605 || isset($_POST['_savedoknew'])
606 || isset($_POST['_duplicatedoc'])
607 || isset($_POST['_translation_savedok'])
608 || isset($_POST['_translation_savedokclear']);
609 return $out;
610 }
611
612 /**
613 * Do processing of data, submitting it to DataHandler. May return a RedirectResponse
614 *
615 * @param $request ServerRequestInterface
616 * @return ResponseInterface|null
617 */
618 public function processData(ServerRequestInterface $request = null): ?ResponseInterface
619 {
620 // @deprecated Variable can be removed in v10
621 $deprecatedCaller = false;
622 if ($request === null) {
623 // Missing argument? This method must have been called from outside.
624 // Method will be protected and $request mandatory in v10, giving core freedom to move stuff around
625 // New v10 signature: "protected function processData(ServerRequestInterface $request): ?ResponseInterface"
626 // @deprecated since TYPO3 v9, method argument $request will be set to mandatory
627 trigger_error('Method processData() will be set to protected in v10. Do not call from other extension', E_USER_DEPRECATED);
628 $request = $GLOBALS['TYPO3_REQUEST'];
629 $deprecatedCaller = true;
630 }
631
632 $parsedBody = $request->getParsedBody();
633 $queryParams = $request->getQueryParams();
634
635 $beUser = $this->getBackendUser();
636
637 // Processing related GET / POST vars
638 $this->data = $parsedBody['data'] ?? $queryParams['data'] ?? [];
639 $this->cmd = $parsedBody['cmd'] ?? $queryParams['cmd'] ?? [];
640 $this->mirror = $parsedBody['mirror'] ?? $queryParams['mirror'] ?? [];
641 // @deprecated property cacheCmd is unused and can be removed in v10
642 $this->cacheCmd = $parsedBody['cacheCmd'] ?? $queryParams['cacheCmd'] ?? null;
643 // @deprecated property redirect is unused and can be removed in v10
644 $this->redirect = $parsedBody['redirect'] ?? $queryParams['redirect'] ?? null;
645 $this->returnNewPageId = (bool)($parsedBody['returnNewPageId'] ?? $queryParams['returnNewPageId'] ?? false);
646
647 // Only options related to $this->data submission are included here
648 $tce = GeneralUtility::makeInstance(DataHandler::class);
649
650 $tce->setControl($parsedBody['control'] ?? $queryParams['control'] ?? []);
651
652 if (isset($parsedBody['_translation_savedok'])) {
653 $tce->updateModeL10NdiffData = 'FORCE_FFUPD';
654 }
655 if (isset($parsedBody['_translation_savedokclear'])) {
656 $tce->updateModeL10NdiffData = 'FORCE_FFUPD';
657 $tce->updateModeL10NdiffDataClear = true;
658 }
659 // Set default values specific for the user
660 $TCAdefaultOverride = $beUser->getTSConfigProp('TCAdefaults');
661 if (is_array($TCAdefaultOverride)) {
662 $tce->setDefaultsFromUserTS($TCAdefaultOverride);
663 }
664 // Set internal vars
665 if (isset($beUser->uc['neverHideAtCopy']) && $beUser->uc['neverHideAtCopy']) {
666 $tce->neverHideAtCopy = 1;
667 }
668 // Load DataHandler with data
669 $tce->start($this->data, $this->cmd);
670 if (is_array($this->mirror)) {
671 $tce->setMirror($this->mirror);
672 }
673
674 // Perform the saving operation with DataHandler:
675 if ($this->doSave === true) {
676 // @todo: Make DataHandler understand UploadedFileInterface and submit $request->getUploadedFiles() instead of $_FILES here
677 $tce->process_uploads($_FILES);
678 $tce->process_datamap();
679 $tce->process_cmdmap();
680 }
681 // If pages are being edited, we set an instruction about updating the page tree after this operation.
682 if ($tce->pagetreeNeedsRefresh
683 && (isset($this->data['pages']) || $beUser->workspace != 0 && !empty($this->data))
684 ) {
685 BackendUtility::setUpdateSignal('updatePageTree');
686 }
687 // If there was saved any new items, load them:
688 if (!empty($tce->substNEWwithIDs_table)) {
689 // Save the expanded/collapsed states for new inline records, if any
690 FormEngineUtility::updateInlineView($this->uc, $tce);
691 $newEditConf = [];
692 foreach ($this->editconf as $tableName => $tableCmds) {
693 $keys = array_keys($tce->substNEWwithIDs_table, $tableName);
694 if (!empty($keys)) {
695 foreach ($keys as $key) {
696 $editId = $tce->substNEWwithIDs[$key];
697 // Check if the $editId isn't a child record of an IRRE action
698 if (!(is_array($tce->newRelatedIDs[$tableName])
699 && in_array($editId, $tce->newRelatedIDs[$tableName]))
700 ) {
701 // Translate new id to the workspace version
702 if ($versionRec = BackendUtility::getWorkspaceVersionOfRecord(
703 $beUser->workspace,
704 $tableName,
705 $editId,
706 'uid'
707 )) {
708 $editId = $versionRec['uid'];
709 }
710 $newEditConf[$tableName][$editId] = 'edit';
711 }
712 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
713 // Traverse all new records and forge the content of ->editconf so we can continue to edit these records!
714 if ($tableName === 'pages'
715 && $this->retUrl != (string)$uriBuilder->buildUriFromRoute('dummy')
716 && $this->returnNewPageId
717 ) {
718 $this->retUrl .= '&id=' . $tce->substNEWwithIDs[$key];
719 }
720 }
721 } else {
722 $newEditConf[$tableName] = $tableCmds;
723 }
724 }
725 // Reset editconf if newEditConf has values
726 if (!empty($newEditConf)) {
727 $this->editconf = $newEditConf;
728 }
729 // Finally, set the editconf array in the "getvars" so they will be passed along in URLs as needed.
730 $this->R_URL_getvars['edit'] = $this->editconf;
731 // Unset default values since we don't need them anymore.
732 unset($this->R_URL_getvars['defVals']);
733 // Recompile the store* values since editconf changed
734 $this->compileStoreData();
735 }
736 // See if any records was auto-created as new versions?
737 if (!empty($tce->autoVersionIdMap)) {
738 $this->fixWSversioningInEditConf($tce->autoVersionIdMap);
739 }
740 // If a document is saved and a new one is created right after.
741 if (isset($parsedBody['_savedoknew']) && is_array($this->editconf)) {
742 if ($redirect = $this->closeDocument(self::DOCUMENT_CLOSE_MODE_NO_REDIRECT, $request)) {
743 return $redirect;
744 }
745 // Find the current table
746 reset($this->editconf);
747 $nTable = key($this->editconf);
748 // Finding the first id, getting the records pid+uid
749 reset($this->editconf[$nTable]);
750 $nUid = key($this->editconf[$nTable]);
751 $recordFields = 'pid,uid';
752 if (!empty($GLOBALS['TCA'][$nTable]['ctrl']['versioningWS'])) {
753 $recordFields .= ',t3ver_oid';
754 }
755 $nRec = BackendUtility::getRecord($nTable, $nUid, $recordFields);
756 // Determine insertion mode: 'top' is self-explaining,
757 // otherwise new elements are inserted after one using a negative uid
758 $insertRecordOnTop = ($this->getTsConfigOption($nTable, 'saveDocNew') === 'top');
759 // Setting a blank editconf array for a new record:
760 $this->editconf = [];
761 // Determine related page ID for regular live context
762 if ($nRec['pid'] != -1) {
763 if ($insertRecordOnTop) {
764 $relatedPageId = $nRec['pid'];
765 } else {
766 $relatedPageId = -$nRec['uid'];
767 }
768 } else {
769 // Determine related page ID for workspace context
770 if ($insertRecordOnTop) {
771 // Fetch live version of workspace version since the pid value is always -1 in workspaces
772 $liveRecord = BackendUtility::getRecord($nTable, $nRec['t3ver_oid'], $recordFields);
773 $relatedPageId = $liveRecord['pid'];
774 } else {
775 // Use uid of live version of workspace version
776 $relatedPageId = -$nRec['t3ver_oid'];
777 }
778 }
779 $this->editconf[$nTable][$relatedPageId] = 'new';
780 // Finally, set the editconf array in the "getvars" so they will be passed along in URLs as needed.
781 $this->R_URL_getvars['edit'] = $this->editconf;
782 // Recompile the store* values since editconf changed...
783 $this->compileStoreData();
784 }
785 // If a document should be duplicated.
786 if (isset($parsedBody['_duplicatedoc']) && is_array($this->editconf)) {
787 $this->closeDocument(self::DOCUMENT_CLOSE_MODE_NO_REDIRECT, $request);
788 // Find current table
789 reset($this->editconf);
790 $nTable = key($this->editconf);
791 // Find the first id, getting the records pid+uid
792 reset($this->editconf[$nTable]);
793 $nUid = key($this->editconf[$nTable]);
794 if (!MathUtility::canBeInterpretedAsInteger($nUid)) {
795 $nUid = $tce->substNEWwithIDs[$nUid];
796 }
797
798 $recordFields = 'pid,uid';
799 if (!empty($GLOBALS['TCA'][$nTable]['ctrl']['versioningWS'])) {
800 $recordFields .= ',t3ver_oid';
801 }
802 $nRec = BackendUtility::getRecord($nTable, $nUid, $recordFields);
803
804 // Setting a blank editconf array for a new record:
805 $this->editconf = [];
806
807 if ($nRec['pid'] != -1) {
808 $relatedPageId = -$nRec['uid'];
809 } else {
810 $relatedPageId = -$nRec['t3ver_oid'];
811 }
812
813 /** @var $duplicateTce \TYPO3\CMS\Core\DataHandling\DataHandler */
814 $duplicateTce = GeneralUtility::makeInstance(DataHandler::class);
815
816 $duplicateCmd = [
817 $nTable => [
818 $nUid => [
819 'copy' => $relatedPageId
820 ]
821 ]
822 ];
823
824 $duplicateTce->start([], $duplicateCmd);
825 $duplicateTce->process_cmdmap();
826
827 $duplicateMappingArray = $duplicateTce->copyMappingArray;
828 $duplicateUid = $duplicateMappingArray[$nTable][$nUid];
829
830 if ($nTable === 'pages') {
831 BackendUtility::setUpdateSignal('updatePageTree');
832 }
833
834 $this->editconf[$nTable][$duplicateUid] = 'edit';
835 // Finally, set the editconf array in the "getvars" so they will be passed along in URLs as needed.
836 $this->R_URL_getvars['edit'] = $this->editconf;
837 // Recompile the store* values since editconf changed...
838 $this->compileStoreData();
839
840 // Inform the user of the duplication
841 $flashMessage = GeneralUtility::makeInstance(
842 FlashMessage::class,
843 $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.recordDuplicated'),
844 '',
845 FlashMessage::OK
846 );
847 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
848 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
849 $defaultFlashMessageQueue->enqueue($flashMessage);
850 }
851 // If a preview is requested
852 if (isset($parsedBody['_savedokview'])) {
853 // Get the first table and id of the data array from DataHandler
854 $table = reset(array_keys($this->data));
855 $id = reset(array_keys($this->data[$table]));
856 if (!MathUtility::canBeInterpretedAsInteger($id)) {
857 $id = $tce->substNEWwithIDs[$id];
858 }
859 // Store this information for later use
860 $this->previewData['table'] = $table;
861 $this->previewData['id'] = $id;
862 }
863 $tce->printLogErrorMessages();
864
865 if ((int)$this->closeDoc < self::DOCUMENT_CLOSE_MODE_DEFAULT
866 || isset($parsedBody['_saveandclosedok'])
867 || isset($parsedBody['_translation_savedok'])
868 ) {
869 // Redirect if element should be closed after save
870 $possibleRedirect = $this->closeDocument(abs($this->closeDoc), $request);
871 if ($deprecatedCaller && $possibleRedirect) {
872 // @deprecated fall back if method has been called from outside. This if can be removed in v10
873 HttpUtility::redirect($possibleRedirect->getHeaders()['location'][0]);
874 }
875 return $possibleRedirect;
876 }
877 return null;
878 }
879
880 /**
881 * Initialize the view part of the controller logic.
882 *
883 * @param $request ServerRequestInterface
884 */
885 public function init(ServerRequestInterface $request = null): void
886 {
887 if ($request === null) {
888 // Missing argument? This method must have been called from outside.
889 // Method will be protected and $request mandatory in v10, giving core freedom to move stuff around
890 // New v10 signature: "protected function init(ServerRequestInterface $request): void
891 // @deprecated since TYPO3 v9, method argument $request will be set to mandatory
892 trigger_error('Method init() will be set to protected in v10. Do not call from other extension', E_USER_DEPRECATED);
893 $request = $GLOBALS['TYPO3_REQUEST'];
894 }
895
896 $parsedBody = $request->getParsedBody();
897 $queryParams = $request->getQueryParams();
898
899 $beUser = $this->getBackendUser();
900
901 // @deprecated since v9, will be removed in v10, unused, remove call in v10
902 $this->popViewId_addParams = $parsedBody['popViewId_addParams'] ?? $queryParams['popViewId_addParams'] ?? '';
903
904 $this->popViewId = (int)($parsedBody['popViewId'] ?? $queryParams['popViewId'] ?? 0);
905 $this->viewUrl = (string)($parsedBody['viewUrl'] ?? $queryParams['viewUrl'] ?? '');
906 $this->recTitle = (string)($parsedBody['recTitle'] ?? $queryParams['recTitle'] ?? '');
907 $this->noView = (bool)($parsedBody['noView'] ?? $queryParams['noView'] ?? false);
908 $this->perms_clause = $beUser->getPagePermsClause(Permission::PAGE_SHOW);
909 // Set other internal variables:
910 $this->R_URL_getvars['returnUrl'] = $this->retUrl;
911 $this->R_URI = $this->R_URL_parts['path'] . '?' . ltrim(GeneralUtility::implodeArrayForUrl(
912 '',
913 $this->R_URL_getvars
914 ), '&');
915
916 // @deprecated since v9, will be removed in v10, unused
917 $this->MCONF['name'] = 'xMOD_alt_doc.php';
918 // @deprecated since v9, will be removed in v10, unused
919 $this->doc = $GLOBALS['TBE_TEMPLATE'];
920
921 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
922 $pageRenderer->addInlineLanguageLabelFile('EXT:lang/Resources/Private/Language/locallang_alt_doc.xlf');
923
924 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
925 // override the default jumpToUrl
926 $this->moduleTemplate->addJavaScriptCode(
927 'jumpToUrl',
928 '
929 function jumpToUrl(URL,formEl) {
930 if (!TBE_EDITOR.isFormChanged()) {
931 window.location.href = URL;
932 } else if (formEl && formEl.type=="checkbox") {
933 formEl.checked = formEl.checked ? 0 : 1;
934 }
935 }
936 // Info view:
937 function launchView(table,uid) {
938 console.warn(\'Calling launchView() has been deprecated in v9 and will be removed in v10.0\');
939 var thePreviewWindow = window.open(
940 ' . GeneralUtility::quoteJSvalue((string)$uriBuilder->buildUriFromRoute('show_item') . '&table=') . ' + encodeURIComponent(table) + "&uid=" + encodeURIComponent(uid),
941 "ShowItem" + Math.random().toString(16).slice(2),
942 "height=300,width=410,status=0,menubar=0,resizable=0,location=0,directories=0,scrollbars=1,toolbar=0"
943 );
944 if (thePreviewWindow && thePreviewWindow.focus) {
945 thePreviewWindow.focus();
946 }
947 }
948 function deleteRecord(table,id,url) {
949 window.location.href = ' . GeneralUtility::quoteJSvalue((string)$uriBuilder->buildUriFromRoute('tce_db') . '&cmd[') . '+table+"]["+id+"][delete]=1&redirect="+escape(url);
950 }
951 ' . (isset($parsedBody['_savedokview']) && $this->popViewId ? $this->generatePreviewCode() : '')
952 );
953 // Set context sensitive menu
954 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
955
956 $this->emitFunctionAfterSignal('init', $request);
957 }
958
959 /**
960 * @return string
961 */
962 protected function generatePreviewCode(): string
963 {
964 $table = $this->previewData['table'];
965 $recordId = $this->previewData['id'];
966
967 if ($table === 'pages') {
968 $currentPageId = $recordId;
969 } else {
970 $currentPageId = MathUtility::convertToPositiveInteger($this->popViewId);
971 }
972
973 $pageTsConfig = BackendUtility::getPagesTSconfig($currentPageId);
974 $previewConfiguration = $pageTsConfig['TCEMAIN.']['preview.'][$table . '.'] ?? [];
975
976 $recordArray = BackendUtility::getRecord($table, $recordId);
977
978 // find the right preview page id
979 $previewPageId = 0;
980 if (isset($previewConfiguration['previewPageId'])) {
981 $previewPageId = $previewConfiguration['previewPageId'];
982 }
983 // if no preview page was configured
984 if (!$previewPageId) {
985 $rootPageData = null;
986 $rootLine = BackendUtility::BEgetRootLine($currentPageId);
987 $currentPage = reset($rootLine);
988 // Allow all doktypes below 200
989 // This makes custom doktype work as well with opening a frontend page.
990 if ((int)$currentPage['doktype'] <= PageRepository::DOKTYPE_SPACER) {
991 // try the current page
992 $previewPageId = $currentPageId;
993 } else {
994 // or search for the root page
995 foreach ($rootLine as $page) {
996 if ($page['is_siteroot']) {
997 $rootPageData = $page;
998 break;
999 }
1000 }
1001 $previewPageId = isset($rootPageData)
1002 ? (int)$rootPageData['uid']
1003 : $currentPageId;
1004 }
1005 }
1006
1007 $linkParameters = [];
1008
1009 // language handling
1010 $languageField = $GLOBALS['TCA'][$table]['ctrl']['languageField'] ?? '';
1011 if ($languageField && !empty($recordArray[$languageField])) {
1012 $l18nPointer = $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'] ?? '';
1013 if ($l18nPointer && !empty($recordArray[$l18nPointer])
1014 && isset($previewConfiguration['useDefaultLanguageRecord'])
1015 && !$previewConfiguration['useDefaultLanguageRecord']
1016 ) {
1017 // use parent record
1018 $recordId = $recordArray[$l18nPointer];
1019 }
1020 $linkParameters['L'] = $recordArray[$languageField];
1021 }
1022
1023 // map record data to GET parameters
1024 if (isset($previewConfiguration['fieldToParameterMap.'])) {
1025 foreach ($previewConfiguration['fieldToParameterMap.'] as $field => $parameterName) {
1026 $value = $recordArray[$field];
1027 if ($field === 'uid') {
1028 $value = $recordId;
1029 }
1030 $linkParameters[$parameterName] = $value;
1031 }
1032 }
1033
1034 // add/override parameters by configuration
1035 if (isset($previewConfiguration['additionalGetParameters.'])) {
1036 $additionalGetParameters = [];
1037 $this->parseAdditionalGetParameters(
1038 $additionalGetParameters,
1039 $previewConfiguration['additionalGetParameters.']
1040 );
1041 $linkParameters = array_replace($linkParameters, $additionalGetParameters);
1042 }
1043
1044 if (!empty($previewConfiguration['useCacheHash'])) {
1045 $cacheHashCalculator = GeneralUtility::makeInstance(CacheHashCalculator::class);
1046 $fullLinkParameters = GeneralUtility::implodeArrayForUrl('', array_merge($linkParameters, ['id' => $previewPageId]));
1047 $cacheHashParameters = $cacheHashCalculator->getRelevantParameters($fullLinkParameters);
1048 $linkParameters['cHash'] = $cacheHashCalculator->calculateCacheHash($cacheHashParameters);
1049 } else {
1050 $linkParameters['no_cache'] = 1;
1051 }
1052
1053 $this->popViewId = $previewPageId;
1054 $popViewId_addParams = GeneralUtility::implodeArrayForUrl('', $linkParameters, '', false, true);
1055 $anchorSection = $table === 'tt_content' ? '#c' . $recordId : '';
1056
1057 $previewPageRootLine = BackendUtility::BEgetRootLine($this->popViewId);
1058 return '
1059 if (window.opener) {
1060 '
1061 . BackendUtility::viewOnClick(
1062 $this->popViewId,
1063 '',
1064 $previewPageRootLine,
1065 $anchorSection,
1066 $this->viewUrl,
1067 $popViewId_addParams,
1068 false
1069 )
1070 . '
1071 } else {
1072 '
1073 . BackendUtility::viewOnClick(
1074 $this->popViewId,
1075 '',
1076 $previewPageRootLine,
1077 $anchorSection,
1078 $this->viewUrl,
1079 $popViewId_addParams
1080 )
1081 . '
1082 }';
1083 }
1084
1085 /**
1086 * Migrates a set of (possibly nested) GET parameters in TypoScript syntax to a plain array
1087 *
1088 * This basically removes the trailing dots of sub-array keys in TypoScript.
1089 * The result can be used to create a query string with GeneralUtility::implodeArrayForUrl().
1090 *
1091 * @param array $parameters Should be an empty array by default
1092 * @param array $typoScript The TypoScript configuration
1093 */
1094 protected function parseAdditionalGetParameters(array &$parameters, array $typoScript)
1095 {
1096 foreach ($typoScript as $key => $value) {
1097 if (is_array($value)) {
1098 $key = rtrim($key, '.');
1099 $parameters[$key] = [];
1100 $this->parseAdditionalGetParameters($parameters[$key], $value);
1101 } else {
1102 $parameters[$key] = $value;
1103 }
1104 }
1105 }
1106
1107 /**
1108 * Main module operation
1109 *
1110 * @param $request ServerRequestInterface
1111 */
1112 public function main(ServerRequestInterface $request = null): void
1113 {
1114 if ($request === null) {
1115 // Set method signature in v10 to: "protected function main(ServerRequestInterface $request): void"
1116 trigger_error('@deprecated since v9, this method will be set to protected in v10', E_USER_DEPRECATED);
1117 $request = $GLOBALS['TYPO3_REQUEST'];
1118 }
1119
1120 $body = '';
1121 // Begin edit
1122 if (is_array($this->editconf)) {
1123 $this->formResultCompiler = GeneralUtility::makeInstance(FormResultCompiler::class);
1124
1125 // Creating the editing form, wrap it with buttons, document selector etc.
1126 $editForm = $this->makeEditForm();
1127 if ($editForm) {
1128 $this->firstEl = reset($this->elementsData);
1129 // Checking if the currently open document is stored in the list of "open documents" - if not, add it:
1130 if (($this->docDat[1] !== $this->storeUrlMd5 || !isset($this->docHandler[$this->storeUrlMd5]))
1131 && !$this->dontStoreDocumentRef
1132 ) {
1133 $this->docHandler[$this->storeUrlMd5] = [
1134 $this->storeTitle,
1135 $this->storeArray,
1136 $this->storeUrl,
1137 $this->firstEl
1138 ];
1139 $this->getBackendUser()->pushModuleData('FormEngine', [$this->docHandler, $this->storeUrlMd5]);
1140 BackendUtility::setUpdateSignal('OpendocsController::updateNumber', count($this->docHandler));
1141 }
1142 // Module configuration
1143 $this->modTSconfig = $this->viewId
1144 ? BackendUtility::getModTSconfig(
1145 $this->viewId,
1146 'mod.xMOD_alt_doc'
1147 )
1148 : [];
1149 $body = $this->formResultCompiler->addCssFiles();
1150 $body .= $this->compileForm($editForm);
1151 $body .= $this->formResultCompiler->printNeededJSFunctions();
1152 $body .= '</form>';
1153 }
1154 }
1155 // Access check...
1156 // The page will show only if there is a valid page and if this page may be viewed by the user
1157 $this->pageinfo = BackendUtility::readPageAccess($this->viewId, $this->perms_clause);
1158 if ($this->pageinfo) {
1159 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($this->pageinfo);
1160 }
1161 // Setting up the buttons and markers for doc header
1162 $this->getButtons($request);
1163 $this->languageSwitch(
1164 (string)($this->firstEl['table'] ?? ''),
1165 (int)($this->firstEl['uid'] ?? 0),
1166 isset($this->firstEl['pid']) ? (int)$this->firstEl['pid'] : null
1167 );
1168 $this->moduleTemplate->setContent($body);
1169 }
1170
1171 /**
1172 * Creates the editing form with FormEngine, based on the input from GPvars.
1173 *
1174 * @return string HTML form elements wrapped in tables
1175 */
1176 public function makeEditForm(): string
1177 {
1178 // Foreign class call? Method will be protected in v10, giving core freedom to move stuff around
1179 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
1180 if (end($backtrace)['class'] !== __CLASS__) {
1181 // @deprecated since TYPO3 v9, this method will be set to protected in v10
1182 trigger_error('Method makeEditForm() will be set to protected in v10. Do not call from other extension', E_USER_DEPRECATED);
1183 }
1184
1185 // Initialize variables
1186 $this->elementsData = [];
1187 $this->errorC = 0;
1188 $this->newC = 0;
1189 $editForm = '';
1190 $trData = null;
1191 $beUser = $this->getBackendUser();
1192 // Traverse the GPvar edit array tables
1193 foreach ($this->editconf as $table => $conf) {
1194 if (is_array($conf) && $GLOBALS['TCA'][$table] && $beUser->check('tables_modify', $table)) {
1195 // Traverse the keys/comments of each table (keys can be a comma list of uids)
1196 foreach ($conf as $cKey => $command) {
1197 if ($command === 'edit' || $command === 'new') {
1198 // Get the ids:
1199 $ids = GeneralUtility::trimExplode(',', $cKey, true);
1200 // Traverse the ids:
1201 foreach ($ids as $theUid) {
1202 // Don't save this document title in the document selector if the document is new.
1203 if ($command === 'new') {
1204 $this->dontStoreDocumentRef = 1;
1205 }
1206
1207 try {
1208 $formDataGroup = GeneralUtility::makeInstance(TcaDatabaseRecord::class);
1209 $formDataCompiler = GeneralUtility::makeInstance(FormDataCompiler::class, $formDataGroup);
1210 $nodeFactory = GeneralUtility::makeInstance(NodeFactory::class);
1211
1212 // Reset viewId - it should hold data of last entry only
1213 $this->viewId = 0;
1214 $this->viewId_addParams = '';
1215
1216 $formDataCompilerInput = [
1217 'tableName' => $table,
1218 'vanillaUid' => (int)$theUid,
1219 'command' => $command,
1220 'returnUrl' => $this->R_URI,
1221 ];
1222 if (is_array($this->overrideVals) && is_array($this->overrideVals[$table])) {
1223 $formDataCompilerInput['overrideValues'] = $this->overrideVals[$table];
1224 }
1225 if (!empty($this->defVals) && is_array($this->defVals)) {
1226 $formDataCompilerInput['defaultValues'] = $this->defVals;
1227 }
1228
1229 $formData = $formDataCompiler->compile($formDataCompilerInput);
1230
1231 // Set this->viewId if possible
1232 if ($command === 'new'
1233 && $table !== 'pages'
1234 && !empty($formData['parentPageRow']['uid'])
1235 ) {
1236 $this->viewId = $formData['parentPageRow']['uid'];
1237 } else {
1238 if ($table === 'pages') {
1239 $this->viewId = $formData['databaseRow']['uid'];
1240 } elseif (!empty($formData['parentPageRow']['uid'])) {
1241 $this->viewId = $formData['parentPageRow']['uid'];
1242 // Adding "&L=xx" if the record being edited has a languageField with a value larger than zero!
1243 if (!empty($formData['processedTca']['ctrl']['languageField'])
1244 && is_array($formData['databaseRow'][$formData['processedTca']['ctrl']['languageField']])
1245 && $formData['databaseRow'][$formData['processedTca']['ctrl']['languageField']][0] > 0
1246 ) {
1247 $this->viewId_addParams = '&L=' . $formData['databaseRow'][$formData['processedTca']['ctrl']['languageField']][0];
1248 }
1249 }
1250 }
1251
1252 // Determine if delete button can be shown
1253 $deleteAccess = false;
1254 if ($command === 'edit') {
1255 $permission = $formData['userPermissionOnPage'];
1256 if ($formData['tableName'] === 'pages') {
1257 $deleteAccess = $permission & Permission::PAGE_DELETE ? true : false;
1258 } else {
1259 $deleteAccess = $permission & Permission::CONTENT_EDIT ? true : false;
1260 }
1261 }
1262
1263 // Display "is-locked" message
1264 if ($command === 'edit') {
1265 $lockInfo = BackendUtility::isRecordLocked($table, $formData['databaseRow']['uid']);
1266 if ($lockInfo) {
1267 $flashMessage = GeneralUtility::makeInstance(
1268 FlashMessage::class,
1269 $lockInfo['msg'],
1270 '',
1271 FlashMessage::WARNING
1272 );
1273 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
1274 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
1275 $defaultFlashMessageQueue->enqueue($flashMessage);
1276 }
1277 }
1278
1279 // Record title
1280 if (!$this->storeTitle) {
1281 $this->storeTitle = $this->recTitle
1282 ? htmlspecialchars($this->recTitle)
1283 : BackendUtility::getRecordTitle($table, FormEngineUtility::databaseRowCompatibility($formData['databaseRow']), true);
1284 }
1285
1286 $this->elementsData[] = [
1287 'table' => $table,
1288 'uid' => $formData['databaseRow']['uid'],
1289 'pid' => $formData['databaseRow']['pid'],
1290 'cmd' => $command,
1291 'deleteAccess' => $deleteAccess
1292 ];
1293
1294 if ($command !== 'new') {
1295 BackendUtility::lockRecords($table, $formData['databaseRow']['uid'], $table === 'tt_content' ? $formData['databaseRow']['pid'] : 0);
1296 }
1297
1298 // Set list if only specific fields should be rendered. This will trigger
1299 // ListOfFieldsContainer instead of FullRecordContainer in OuterWrapContainer
1300 if ($this->columnsOnly) {
1301 if (is_array($this->columnsOnly)) {
1302 $formData['fieldListToRender'] = $this->columnsOnly[$table];
1303 } else {
1304 $formData['fieldListToRender'] = $this->columnsOnly;
1305 }
1306 }
1307
1308 $formData['renderType'] = 'outerWrapContainer';
1309 $formResult = $nodeFactory->create($formData)->render();
1310
1311 $html = $formResult['html'];
1312
1313 $formResult['html'] = '';
1314 $formResult['doSaveFieldName'] = 'doSave';
1315
1316 // @todo: Put all the stuff into FormEngine as final "compiler" class
1317 // @todo: This is done here for now to not rewrite addCssFiles()
1318 // @todo: and printNeededJSFunctions() now
1319 $this->formResultCompiler->mergeResult($formResult);
1320
1321 // Seems the pid is set as hidden field (again) at end?!
1322 if ($command === 'new') {
1323 // @todo: looks ugly
1324 $html .= LF
1325 . '<input type="hidden"'
1326 . ' name="data[' . htmlspecialchars($table) . '][' . htmlspecialchars($formData['databaseRow']['uid']) . '][pid]"'
1327 . ' value="' . (int)$formData['databaseRow']['pid'] . '" />';
1328 $this->newC++;
1329 }
1330
1331 $editForm .= $html;
1332 } catch (AccessDeniedException $e) {
1333 $this->errorC++;
1334 // Try to fetch error message from "recordInternals" be user object
1335 // @todo: This construct should be logged and localized and de-uglified
1336 $message = $beUser->errorMsg;
1337 if (empty($message)) {
1338 // Create message from exception.
1339 $message = $e->getMessage() . ' ' . $e->getCode();
1340 }
1341 $editForm .= htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.noEditPermission'))
1342 . '<br /><br />' . htmlspecialchars($message) . '<br /><br />';
1343 } catch (DatabaseRecordException $e) {
1344 $editForm = '<div class="alert alert-warning">' . htmlspecialchars($e->getMessage()) . '</div>';
1345 }
1346 } // End of for each uid
1347 }
1348 }
1349 }
1350 }
1351 return $editForm;
1352 }
1353
1354 /**
1355 * Create the panel of buttons for submitting the form or otherwise perform operations.
1356 *
1357 * @param $request ServerRequestInterface
1358 */
1359 protected function getButtons(ServerRequestInterface $request): void
1360 {
1361 $lang = $this->getLanguageService();
1362 /** @var UriBuilder $uriBuilder */
1363 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
1364 // Render SAVE type buttons:
1365 // The action of each button is decided by its name attribute.
1366 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
1367 if (!$this->errorC && !$GLOBALS['TCA'][$this->firstEl['table']]['ctrl']['readOnly']) {
1368 $saveSplitButton = $buttonBar->makeSplitButton();
1369 // SAVE button:
1370 $saveButton = $buttonBar->makeInputButton()
1371 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.saveDoc'))
1372 ->setName('_savedok')
1373 ->setValue('1')
1374 ->setForm('EditDocumentController')
1375 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-document-save', Icon::SIZE_SMALL));
1376 $saveSplitButton->addItem($saveButton, true);
1377
1378 // SAVE / VIEW button:
1379 if ($this->viewId && !$this->noView && !empty($this->firstEl['table']) && $this->getTsConfigOption($this->firstEl['table'], 'saveDocView')) {
1380 $pagesTSconfig = BackendUtility::getPagesTSconfig($this->pageinfo['uid']);
1381 if (isset($pagesTSconfig['TCEMAIN.']['preview.']['disableButtonForDokType'])) {
1382 $excludeDokTypes = GeneralUtility::intExplode(
1383 ',',
1384 $pagesTSconfig['TCEMAIN.']['preview.']['disableButtonForDokType'],
1385 true
1386 );
1387 } else {
1388 // exclude sysfolders, spacers and recycler by default
1389 $excludeDokTypes = [
1390 PageRepository::DOKTYPE_RECYCLER,
1391 PageRepository::DOKTYPE_SYSFOLDER,
1392 PageRepository::DOKTYPE_SPACER
1393 ];
1394 }
1395 if (!in_array((int)$this->pageinfo['doktype'], $excludeDokTypes, true)
1396 || isset($pagesTSconfig['TCEMAIN.']['preview.'][$this->firstEl['table'] . '.']['previewPageId'])
1397 ) {
1398 $saveAndOpenButton = $buttonBar->makeInputButton()
1399 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.saveDocShow'))
1400 ->setName('_savedokview')
1401 ->setValue('1')
1402 ->setForm('EditDocumentController')
1403 ->setOnClick("window.open('', 'newTYPO3frontendWindow');")
1404 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
1405 'actions-document-save-view',
1406 Icon::SIZE_SMALL
1407 ));
1408 $saveSplitButton->addItem($saveAndOpenButton);
1409 }
1410 }
1411 // SAVE / NEW button:
1412 if (count($this->elementsData) === 1 && !empty($this->firstEl['table']) && $this->getTsConfigOption($this->firstEl['table'], 'saveDocNew')) {
1413 $saveAndNewButton = $buttonBar->makeInputButton()
1414 ->setName('_savedoknew')
1415 ->setClasses('t3js-editform-submitButton')
1416 ->setValue('1')
1417 ->setForm('EditDocumentController')
1418 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.saveNewDoc'))
1419 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
1420 'actions-document-save-new',
1421 Icon::SIZE_SMALL
1422 ));
1423 $saveSplitButton->addItem($saveAndNewButton);
1424 }
1425 // SAVE / CLOSE
1426 $saveAndCloseButton = $buttonBar->makeInputButton()
1427 ->setName('_saveandclosedok')
1428 ->setClasses('t3js-editform-submitButton')
1429 ->setValue('1')
1430 ->setForm('EditDocumentController')
1431 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.saveCloseDoc'))
1432 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
1433 'actions-document-save-close',
1434 Icon::SIZE_SMALL
1435 ));
1436 $saveSplitButton->addItem($saveAndCloseButton);
1437 // FINISH TRANSLATION / SAVE / CLOSE
1438 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['explicitConfirmationOfTranslation']) {
1439 $saveTranslationButton = $buttonBar->makeInputButton()
1440 ->setName('_translation_savedok')
1441 ->setValue('1')
1442 ->setForm('EditDocumentController')
1443 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.translationSaveDoc'))
1444 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
1445 'actions-document-save-cleartranslationcache',
1446 Icon::SIZE_SMALL
1447 ));
1448 $saveSplitButton->addItem($saveTranslationButton);
1449 $saveAndClearTranslationButton = $buttonBar->makeInputButton()
1450 ->setName('_translation_savedokclear')
1451 ->setValue('1')
1452 ->setForm('EditDocumentController')
1453 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.translationSaveDocClear'))
1454 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
1455 'actions-document-save-cleartranslationcache',
1456 Icon::SIZE_SMALL
1457 ));
1458 $saveSplitButton->addItem($saveAndClearTranslationButton);
1459 }
1460 $buttonBar->addButton($saveSplitButton, ButtonBar::BUTTON_POSITION_LEFT, 2);
1461 }
1462 // CLOSE button:
1463 $closeButton = $buttonBar->makeLinkButton()
1464 ->setHref('#')
1465 ->setClasses('t3js-editform-close')
1466 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.closeDoc'))
1467 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
1468 'actions-close',
1469 Icon::SIZE_SMALL
1470 ));
1471 $buttonBar->addButton($closeButton);
1472 // DUPLICATE button:
1473 $record = BackendUtility::getRecord($this->firstEl['table'], $this->firstEl['uid']);
1474 $TCActrl = $GLOBALS['TCA'][$this->firstEl['table']]['ctrl'];
1475 $l18nParent = isset($TCActrl['transOrigPointerField'], $record[$TCActrl['transOrigPointerField']])
1476 ? (int)$record[$TCActrl['transOrigPointerField']]
1477 : 0;
1478 $sysLanguageUid = isset($TCActrl['languageField'], $record[$TCActrl['languageField']])
1479 ? (int)$record[$TCActrl['languageField']]
1480 : 0;
1481 $showDuplicateButton = false;
1482 if ($this->firstEl['cmd'] !== 'new' && $this->firstEl['table'] !== 'sys_file_metadata' && MathUtility::canBeInterpretedAsInteger($this->firstEl['uid'])) {
1483 if ($sysLanguageUid === 0) {
1484 // show button, if record is in default language
1485 $showDuplicateButton = true;
1486 } else {
1487 // show button, if record is NOT in default language AND has no parent
1488 $showDuplicateButton = $l18nParent === 0;
1489 }
1490 }
1491 if ($showDuplicateButton) {
1492 $duplicateButton = $buttonBar->makeLinkButton()
1493 ->setHref('#')
1494 ->setClasses('t3js-editform-duplicate')
1495 ->setShowLabelText(true)
1496 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.duplicateDoc'))
1497 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
1498 'actions-document-duplicates-select',
1499 Icon::SIZE_SMALL
1500 ));
1501 $buttonBar->addButton($duplicateButton, ButtonBar::BUTTON_POSITION_LEFT, 3);
1502 }
1503 // DELETE + UNDO buttons:
1504 if (!$this->errorC
1505 && !$GLOBALS['TCA'][$this->firstEl['table']]['ctrl']['readOnly']
1506 && count($this->elementsData) === 1
1507 ) {
1508 if ($this->firstEl['cmd'] !== 'new' && MathUtility::canBeInterpretedAsInteger($this->firstEl['uid'])) {
1509 // Delete:
1510 if ($this->firstEl['deleteAccess']
1511 && !$GLOBALS['TCA'][$this->firstEl['table']]['ctrl']['readOnly']
1512 && !$this->getDisableDelete()
1513 ) {
1514 $returnUrl = $this->retUrl;
1515 if ($this->firstEl['table'] === 'pages') {
1516 parse_str((string)parse_url($returnUrl, PHP_URL_QUERY), $queryParams);
1517 if (isset($queryParams['route'])
1518 && isset($queryParams['id'])
1519 && (string)$this->firstEl['uid'] === (string)$queryParams['id']
1520 ) {
1521 // TODO: Use the page's pid instead of 0, this requires a clean API to manipulate the page
1522 // tree from the outside to be able to mark the pid as active
1523 $returnUrl = (string)$uriBuilder->buildUriFromRoutePath($queryParams['route'], ['id' => 0]);
1524 }
1525 }
1526 $deleteButton = $buttonBar->makeLinkButton()
1527 ->setHref('#')
1528 ->setClasses('t3js-editform-delete-record')
1529 ->setTitle($lang->getLL('deleteItem'))
1530 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
1531 'actions-edit-delete',
1532 Icon::SIZE_SMALL
1533 ))
1534 ->setDataAttributes([
1535 'return-url' => $returnUrl,
1536 'uid' => $this->firstEl['uid'],
1537 'table' => $this->firstEl['table']
1538 ]);
1539 $buttonBar->addButton($deleteButton, ButtonBar::BUTTON_POSITION_LEFT, 4);
1540 }
1541 // Undo:
1542 if (!empty($this->firstEl['table']) && $this->getTsConfigOption($this->firstEl['table'], 'showHistory')) {
1543 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
1544 ->getQueryBuilderForTable('sys_history');
1545 $undoButtonR = $queryBuilder->select('tstamp')
1546 ->from('sys_history')
1547 ->where(
1548 $queryBuilder->expr()->eq(
1549 'tablename',
1550 $queryBuilder->createNamedParameter($this->firstEl['table'], \PDO::PARAM_STR)
1551 ),
1552 $queryBuilder->expr()->eq(
1553 'recuid',
1554 $queryBuilder->createNamedParameter($this->firstEl['uid'], \PDO::PARAM_INT)
1555 )
1556 )
1557 ->orderBy('tstamp', 'DESC')
1558 ->setMaxResults(1)
1559 ->execute()
1560 ->fetch();
1561 if ($undoButtonR !== false) {
1562 $aOnClick = 'window.location.href=' .
1563 GeneralUtility::quoteJSvalue(
1564 (string)$uriBuilder->buildUriFromRoute(
1565 'record_history',
1566 [
1567 'element' => $this->firstEl['table'] . ':' . $this->firstEl['uid'],
1568 'revert' => 'ALL_FIELDS',
1569 'returnUrl' => $this->R_URI,
1570 ]
1571 )
1572 ) . '; return false;';
1573
1574 $undoButton = $buttonBar->makeLinkButton()
1575 ->setHref('#')
1576 ->setOnClick($aOnClick)
1577 ->setTitle(
1578 sprintf(
1579 $lang->getLL('undoLastChange'),
1580 BackendUtility::calcAge(
1581 $GLOBALS['EXEC_TIME'] - $undoButtonR['tstamp'],
1582 $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.minutesHoursDaysYears')
1583 )
1584 )
1585 )
1586 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
1587 'actions-document-history-open',
1588 Icon::SIZE_SMALL
1589 ));
1590 $buttonBar->addButton($undoButton, ButtonBar::BUTTON_POSITION_LEFT, 3);
1591 }
1592 }
1593 if (!empty($this->firstEl['table']) && $this->getTsConfigOption($this->firstEl['table'], 'showHistory')) {
1594 $aOnClick = 'window.location.href=' .
1595 GeneralUtility::quoteJSvalue(
1596 (string)$uriBuilder->buildUriFromRoute(
1597 'record_history',
1598 [
1599 'element' => $this->firstEl['table'] . ':' . $this->firstEl['uid'],
1600 'returnUrl' => $this->R_URI,
1601 ]
1602 )
1603 ) . '; return false;';
1604
1605 $historyButton = $buttonBar->makeLinkButton()
1606 ->setHref('#')
1607 ->setOnClick($aOnClick)
1608 ->setTitle('Open history of this record')
1609 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
1610 'actions-document-history-open',
1611 Icon::SIZE_SMALL
1612 ));
1613 $buttonBar->addButton($historyButton, ButtonBar::BUTTON_POSITION_LEFT, 3);
1614 }
1615 // If only SOME fields are shown in the form, this will link the user to the FULL form:
1616 if ($this->columnsOnly) {
1617 $columnsOnlyButton = $buttonBar->makeLinkButton()
1618 ->setHref($this->R_URI . '&columnsOnly=')
1619 ->setTitle($lang->getLL('editWholeRecord'))
1620 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon(
1621 'actions-open',
1622 Icon::SIZE_SMALL
1623 ));
1624 $buttonBar->addButton($columnsOnlyButton, ButtonBar::BUTTON_POSITION_LEFT, 3);
1625 }
1626 }
1627 }
1628 $cshButton = $buttonBar->makeHelpButton()->setModuleName('xMOD_csh_corebe')->setFieldName('TCEforms');
1629 $buttonBar->addButton($cshButton);
1630
1631 if ($this->returnUrl !== $this->getCloseUrl()) {
1632 $shortCutButton = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar()->makeShortcutButton();
1633 $shortCutButton->setModuleName('xMOD_alt_doc.php')
1634 ->setGetVariables([
1635 'returnUrl',
1636 'edit',
1637 'defVals',
1638 'overrideVals',
1639 'columnsOnly',
1640 'returnNewPageId',
1641 'noView']);
1642 $this->moduleTemplate->getDocHeaderComponent()->getButtonBar()->addButton($shortCutButton);
1643 }
1644
1645 $closeUrl = $this->getCloseUrl();
1646 if ($this->returnUrl !== $closeUrl) {
1647 $scriptName = $request->getAttribute('normalizedParams')->getScriptName();
1648 $aOnClick = 'vHWin=window.open('
1649 . GeneralUtility::quoteJSvalue($scriptName . '?returnUrl=' . $closeUrl) . ','
1650 . GeneralUtility::quoteJSvalue(md5($this->R_URI))
1651 . ',\'width=670,height=500,status=0,menubar=0,scrollbars=1,resizable=1\');vHWin.focus();return false;';
1652 $openInNewWindowButton = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar()
1653 ->makeLinkButton()
1654 ->setHref('#')
1655 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.openInNewWindow'))
1656 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-window-open', Icon::SIZE_SMALL))
1657 ->setOnClick($aOnClick);
1658 $this->moduleTemplate->getDocHeaderComponent()->getButtonBar()->addButton(
1659 $openInNewWindowButton,
1660 ButtonBar::BUTTON_POSITION_RIGHT
1661 );
1662 }
1663 }
1664
1665 /**
1666 * Put together the various elements (buttons, selectors, form) into a table
1667 *
1668 * @param string $editForm HTML form.
1669 * @return string Composite HTML
1670 */
1671 public function compileForm(string $editForm): string
1672 {
1673 // Foreign class call? Method will be protected in v10, giving core freedom to move stuff around
1674 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
1675 if (end($backtrace)['class'] !== __CLASS__) {
1676 // @deprecated since TYPO3 v9, this method will be set to protected in v10
1677 trigger_error('Method compileForm() will be set to protected in v10. Do not call from other extension', E_USER_DEPRECATED);
1678 }
1679
1680 $formContent = '
1681 <form
1682 action="' . htmlspecialchars($this->R_URI) . '"
1683 method="post"
1684 enctype="multipart/form-data"
1685 name="editform"
1686 id="EditDocumentController"
1687 onsubmit="TBE_EDITOR.checkAndDoSubmit(1); return false;"
1688 >
1689 ' . $editForm . '
1690 <input type="hidden" name="returnUrl" value="' . htmlspecialchars($this->retUrl) . '" />
1691 <input type="hidden" name="viewUrl" value="' . htmlspecialchars($this->viewUrl) . '" />
1692 <input type="hidden" name="popViewId" value="' . htmlspecialchars((string)$this->viewId) . '" />
1693 <input type="hidden" name="closeDoc" value="0" />
1694 <input type="hidden" name="doSave" value="0" />
1695 <input type="hidden" name="_serialNumber" value="' . md5(microtime()) . '" />
1696 <input type="hidden" name="_scrollPosition" value="" />';
1697 if ($this->returnNewPageId) {
1698 $formContent .= '<input type="hidden" name="returnNewPageId" value="1" />';
1699 }
1700 if ($this->viewId_addParams) {
1701 $formContent .= '<input type="hidden" name="popViewId_addParams" value="' . htmlspecialchars($this->viewId_addParams) . '" />';
1702 }
1703 return $formContent;
1704 }
1705
1706 /**
1707 * Create shortcut icon
1708 *
1709 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10
1710 */
1711 public function shortCutLink()
1712 {
1713 trigger_error('Method shortCutLink() will be removed in v10', E_USER_DEPRECATED);
1714
1715 if ($this->returnUrl !== $this->getCloseUrl()) {
1716 $shortCutButton = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar()->makeShortcutButton();
1717 $shortCutButton->setModuleName('xMOD_alt_doc.php')
1718 ->setGetVariables([
1719 'returnUrl',
1720 'edit',
1721 'defVals',
1722 'overrideVals',
1723 'columnsOnly',
1724 'returnNewPageId',
1725 'noView']);
1726 $this->moduleTemplate->getDocHeaderComponent()->getButtonBar()->addButton($shortCutButton);
1727 }
1728 }
1729
1730 /**
1731 * Creates open-in-window link
1732 *
1733 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10
1734 */
1735 public function openInNewWindowLink()
1736 {
1737 trigger_error('Method openInNewWindowLink() will be removed in v10', E_USER_DEPRECATED);
1738
1739 $closeUrl = $this->getCloseUrl();
1740 if ($this->returnUrl !== $closeUrl) {
1741 $aOnClick = 'vHWin=window.open(' . GeneralUtility::quoteJSvalue(GeneralUtility::linkThisScript(
1742 ['returnUrl' => $closeUrl]
1743 ))
1744 . ','
1745 . GeneralUtility::quoteJSvalue(md5($this->R_URI))
1746 . ',\'width=670,height=500,status=0,menubar=0,scrollbars=1,resizable=1\');vHWin.focus();return false;';
1747 $openInNewWindowButton = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar()
1748 ->makeLinkButton()
1749 ->setHref('#')
1750 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.openInNewWindow'))
1751 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-window-open', Icon::SIZE_SMALL))
1752 ->setOnClick($aOnClick);
1753 $this->moduleTemplate->getDocHeaderComponent()->getButtonBar()->addButton(
1754 $openInNewWindowButton,
1755 ButtonBar::BUTTON_POSITION_RIGHT
1756 );
1757 }
1758 }
1759
1760 /**
1761 * Returns if delete for the current table is disabled by configuration.
1762 * For sys_file_metadata in default language delete is always disabled.
1763 *
1764 * @return bool
1765 */
1766 protected function getDisableDelete(): bool
1767 {
1768 $disableDelete = false;
1769 if ($this->firstEl['table'] === 'sys_file_metadata') {
1770 $row = BackendUtility::getRecord('sys_file_metadata', $this->firstEl['uid'], 'sys_language_uid');
1771 $languageUid = $row['sys_language_uid'];
1772 if ($languageUid === 0) {
1773 $disableDelete = true;
1774 }
1775 } else {
1776 $disableDelete = (bool)$this->getTsConfigOption($this->firstEl['table'] ?? '', 'disableDelete');
1777 }
1778 return $disableDelete;
1779 }
1780
1781 /**
1782 * Returns the URL (usually for the "returnUrl") which closes the current window.
1783 * Used when editing a record in a popup.
1784 *
1785 * @return string
1786 */
1787 protected function getCloseUrl(): string
1788 {
1789 $closeUrl = GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Public/Html/Close.html');
1790 return PathUtility::getAbsoluteWebPath($closeUrl);
1791 }
1792
1793 /***************************
1794 *
1795 * Localization stuff
1796 *
1797 ***************************/
1798 /**
1799 * Make selector box for creating new translation for a record or switching to edit the record in an existing
1800 * language.
1801 * Displays only languages which are available for the current page.
1802 *
1803 * @param string $table Table name
1804 * @param int $uid Uid for which to create a new language
1805 * @param int $pid|null Pid of the record
1806 */
1807 public function languageSwitch(string $table, int $uid, $pid = null)
1808 {
1809 // Foreign class call? Method will be protected in v10, giving core freedom to move stuff around
1810 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
1811 if (end($backtrace)['class'] !== __CLASS__) {
1812 // @deprecated since TYPO3 v9, this method will be set to protected in v10
1813 trigger_error('Method fixWSversioningInEditConf() will be set to protected in v10. Do not call from other extension', E_USER_DEPRECATED);
1814 }
1815
1816 $languageField = $GLOBALS['TCA'][$table]['ctrl']['languageField'];
1817 $transOrigPointerField = $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'];
1818 /** @var UriBuilder $uriBuilder */
1819 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
1820
1821 // Table editable and activated for languages?
1822 if ($this->getBackendUser()->check('tables_modify', $table)
1823 && $languageField
1824 && $transOrigPointerField
1825 ) {
1826 if ($pid === null) {
1827 $row = BackendUtility::getRecord($table, $uid, 'pid');
1828 $pid = $row['pid'];
1829 }
1830 // Get all available languages for the page
1831 // If editing a page, the translations of the current UID need to be fetched
1832 if ($table === 'pages') {
1833 $row = BackendUtility::getRecord($table, $uid, $GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField']);
1834 // Ensure the check is always done against the default language page
1835 $langRows = $this->getLanguages((int)($row[$GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField']] ?: $uid));
1836 } else {
1837 $langRows = $this->getLanguages((int)$pid);
1838 }
1839 // Page available in other languages than default language?
1840 if (is_array($langRows) && count($langRows) > 1) {
1841 $rowsByLang = [];
1842 $fetchFields = 'uid,' . $languageField . ',' . $transOrigPointerField;
1843 // Get record in current language
1844 $rowCurrent = BackendUtility::getLiveVersionOfRecord($table, $uid, $fetchFields);
1845 if (!is_array($rowCurrent)) {
1846 $rowCurrent = BackendUtility::getRecord($table, $uid, $fetchFields);
1847 }
1848 $currentLanguage = (int)$rowCurrent[$languageField];
1849 // Disabled for records with [all] language!
1850 if ($currentLanguage > -1) {
1851 // Get record in default language if needed
1852 if ($currentLanguage && $rowCurrent[$transOrigPointerField]) {
1853 $rowsByLang[0] = BackendUtility::getLiveVersionOfRecord(
1854 $table,
1855 $rowCurrent[$transOrigPointerField],
1856 $fetchFields
1857 );
1858 if (!is_array($rowsByLang[0])) {
1859 $rowsByLang[0] = BackendUtility::getRecord(
1860 $table,
1861 $rowCurrent[$transOrigPointerField],
1862 $fetchFields
1863 );
1864 }
1865 } else {
1866 $rowsByLang[$rowCurrent[$languageField]] = $rowCurrent;
1867 }
1868 if ($rowCurrent[$transOrigPointerField] || $currentLanguage === 0) {
1869 // Get record in other languages to see what's already available
1870
1871 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
1872 ->getQueryBuilderForTable($table);
1873
1874 $queryBuilder->getRestrictions()
1875 ->removeAll()
1876 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
1877 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
1878
1879 $result = $queryBuilder->select(...GeneralUtility::trimExplode(',', $fetchFields, true))
1880 ->from($table)
1881 ->where(
1882 $queryBuilder->expr()->eq(
1883 'pid',
1884 $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)
1885 ),
1886 $queryBuilder->expr()->gt(
1887 $languageField,
1888 $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
1889 ),
1890 $queryBuilder->expr()->eq(
1891 $transOrigPointerField,
1892 $queryBuilder->createNamedParameter($rowsByLang[0]['uid'], \PDO::PARAM_INT)
1893 )
1894 )
1895 ->execute();
1896
1897 while ($row = $result->fetch()) {
1898 $rowsByLang[$row[$languageField]] = $row;
1899 }
1900 }
1901 $languageMenu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
1902 $languageMenu->setIdentifier('_langSelector');
1903 foreach ($langRows as $lang) {
1904 if ($this->getBackendUser()->checkLanguageAccess($lang['uid'])) {
1905 $newTranslation = isset($rowsByLang[$lang['uid']]) ? '' : ' [' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.new')) . ']';
1906 // Create url for creating a localized record
1907 $addOption = true;
1908 $href = '';
1909 if ($newTranslation) {
1910 $redirectUrl = (string)$uriBuilder->buildUriFromRoute('record_edit', [
1911 'justLocalized' => $table . ':' . $rowsByLang[0]['uid'] . ':' . $lang['uid'],
1912 'returnUrl' => $this->retUrl
1913 ]);
1914
1915 if (array_key_exists(0, $rowsByLang)) {
1916 $href = BackendUtility::getLinkToDataHandlerAction(
1917 '&cmd[' . $table . '][' . $rowsByLang[0]['uid'] . '][localize]=' . $lang['uid'],
1918 $redirectUrl
1919 );
1920 } else {
1921 $addOption = false;
1922 }
1923 } else {
1924 $href = (string)$uriBuilder->buildUriFromRoute('record_edit', [
1925 'edit[' . $table . '][' . $rowsByLang[$lang['uid']]['uid'] . ']' => 'edit',
1926 'returnUrl' => $this->retUrl
1927 ]);
1928 }
1929 if ($addOption) {
1930 $menuItem = $languageMenu->makeMenuItem()
1931 ->setTitle($lang['title'] . $newTranslation)
1932 ->setHref($href);
1933 if ((int)$lang['uid'] === $currentLanguage) {
1934 $menuItem->setActive(true);
1935 }
1936 $languageMenu->addMenuItem($menuItem);
1937 }
1938 }
1939 }
1940 $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($languageMenu);
1941 }
1942 }
1943 }
1944 }
1945
1946 /**
1947 * Redirects to FormEngine with new parameters to edit a just created localized record
1948 *
1949 * @param ServerRequestInterface $request Incoming request object
1950 * @return ResponseInterface|null Possible redirect response
1951 */
1952 public function localizationRedirect(ServerRequestInterface $request = null): ?ResponseInterface
1953 {
1954 $deprecatedCaller = false;
1955 if (!$request instanceof ServerRequestInterface) {
1956 // @deprecated since TYPO3 v9
1957 // Method signature in v10: protected function localizationRedirect(ServerRequestInterface $request): ?ResponseInterface
1958 trigger_error('Method localizationRedirect() will be set to protected in v10. Do not call from other extension', E_USER_DEPRECATED);
1959 $justLocalized = $request;
1960 $request = $GLOBALS['TYPO3_REQUEST'];
1961 $deprecatedCaller = true;
1962 } else {
1963 $justLocalized = $request->getQueryParams()['justLocalized'];
1964 }
1965
1966 if (empty($justLocalized)) {
1967 return null;
1968 }
1969
1970 list($table, $origUid, $language) = explode(':', $justLocalized);
1971
1972 if ($GLOBALS['TCA'][$table]
1973 && $GLOBALS['TCA'][$table]['ctrl']['languageField']
1974 && $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']
1975 ) {
1976 $parsedBody = $request->getParsedBody();
1977 $queryParams = $request->getQueryParams();
1978
1979 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
1980 $queryBuilder->getRestrictions()
1981 ->removeAll()
1982 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
1983 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
1984 $localizedRecord = $queryBuilder->select('uid')
1985 ->from($table)
1986 ->where(
1987 $queryBuilder->expr()->eq(
1988 $GLOBALS['TCA'][$table]['ctrl']['languageField'],
1989 $queryBuilder->createNamedParameter($language, \PDO::PARAM_INT)
1990 ),
1991 $queryBuilder->expr()->eq(
1992 $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'],
1993 $queryBuilder->createNamedParameter($origUid, \PDO::PARAM_INT)
1994 )
1995 )
1996 ->execute()
1997 ->fetch();
1998 $returnUrl = $parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '';
1999 if (is_array($localizedRecord)) {
2000 if ($deprecatedCaller) {
2001 // @deprecated fall back if method has been called from outside. This if can be removed in v10
2002 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
2003 $location = (string)$uriBuilder->buildUriFromRoute('record_edit', [
2004 'edit[' . $table . '][' . $localizedRecord['uid'] . ']' => 'edit',
2005 'returnUrl' => GeneralUtility::sanitizeLocalUrl($returnUrl)
2006 ]);
2007 HttpUtility::redirect($location);
2008 }
2009 // Create redirect response to self to edit just created record
2010 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
2011 return new RedirectResponse(
2012 (string)$uriBuilder->buildUriFromRoute(
2013 'record_edit',
2014 [
2015 'edit[' . $table . '][' . $localizedRecord['uid'] . ']' => 'edit',
2016 'returnUrl' => GeneralUtility::sanitizeLocalUrl($returnUrl)
2017 ]
2018 ),
2019 303
2020 );
2021 }
2022 }
2023 return null;
2024 }
2025
2026 /**
2027 * Returns sys_language records available for record translations on given page.
2028 *
2029 * @param int $id Page id: If zero, the query will select all sys_language records from root level which are NOT
2030 * hidden. If set to another value, the query will select all sys_language records that has a
2031 * translation record on that page (and is not hidden, unless you are admin user)
2032 * @return array Language records including faked record for default language
2033 */
2034 public function getLanguages(int $id): array
2035 {
2036 // Foreign class call? Method will be protected in v10, giving core freedom to move stuff around
2037 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
2038 if (end($backtrace)['class'] !== __CLASS__) {
2039 // @deprecated since TYPO3 v9, this method will be set to protected in v10
2040 trigger_error('Method getLanguages() will be set to protected in v10. Do not call from other extension', E_USER_DEPRECATED);
2041 }
2042
2043 $languageService = $this->getLanguageService();
2044 $modSharedTSconfig = BackendUtility::getModTSconfig($id, 'mod.SHARED');
2045 // Fallback non sprite-configuration
2046 if (preg_match('/\\.gif$/', $modSharedTSconfig['properties']['defaultLanguageFlag'] ?? '')) {
2047 $modSharedTSconfig['properties']['defaultLanguageFlag'] = str_replace(
2048 '.gif',
2049 '',
2050 $modSharedTSconfig['properties']['defaultLanguageFlag']
2051 );
2052 }
2053 $languages = [
2054 0 => [
2055 'uid' => 0,
2056 'pid' => 0,
2057 'hidden' => 0,
2058 'title' => $modSharedTSconfig['properties']['defaultLanguageLabel'] !== ''
2059 ? $modSharedTSconfig['properties']['defaultLanguageLabel'] . ' (' . $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_web_list.xlf:defaultLanguage') . ')'
2060 : $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_web_list.xlf:defaultLanguage'),
2061 'flag' => $modSharedTSconfig['properties']['defaultLanguageFlag']
2062 ]
2063 ];
2064
2065 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
2066 ->getQueryBuilderForTable('sys_language');
2067
2068 $queryBuilder->select('s.uid', 's.pid', 's.hidden', 's.title', 's.flag')
2069 ->from('sys_language', 's')
2070 ->groupBy('s.uid', 's.pid', 's.hidden', 's.title', 's.flag', 's.sorting')
2071 ->orderBy('s.sorting');
2072
2073 if ($id) {
2074 $queryBuilder->getRestrictions()
2075 ->removeAll()
2076 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
2077 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
2078
2079 if (!$this->getBackendUser()->isAdmin()) {
2080 $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(HiddenRestriction::class));
2081 }
2082
2083 // Add join with pages translations to only show active languages
2084 $queryBuilder->from('pages', 'o')
2085 ->where(
2086 $queryBuilder->expr()->eq('o.' . $GLOBALS['TCA']['pages']['ctrl']['languageField'], $queryBuilder->quoteIdentifier('s.uid')),
2087 $queryBuilder->expr()->eq('o.' . $GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'], $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT))
2088 );
2089 }
2090
2091 $result = $queryBuilder->execute();
2092 while ($row = $result->fetch()) {
2093 $languages[$row['uid']] = $row;
2094 }
2095
2096 return $languages;
2097 }
2098
2099 /**
2100 * Fix $this->editconf if versioning applies to any of the records
2101 *
2102 * @param array|bool $mapArray Mapping between old and new ids if auto-versioning has been performed.
2103 */
2104 public function fixWSversioningInEditConf($mapArray = false): void
2105 {
2106 // Foreign class call? Method will be protected in v10, giving core freedom to move stuff around
2107 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
2108 if (end($backtrace)['class'] !== __CLASS__) {
2109 // @deprecated since TYPO3 v9, this method will be set to protected in v10
2110 trigger_error('Method fixWSversioningInEditConf() will be set to protected in v10. Do not call from other extension', E_USER_DEPRECATED);
2111 }
2112
2113 // Traverse the editConf array
2114 if (is_array($this->editconf)) {
2115 // Tables:
2116 foreach ($this->editconf as $table => $conf) {
2117 if (is_array($conf) && $GLOBALS['TCA'][$table]) {
2118 // Traverse the keys/comments of each table (keys can be a comma list of uids)
2119 $newConf = [];
2120 foreach ($conf as $cKey => $cmd) {
2121 if ($cmd === 'edit') {
2122 // Traverse the ids:
2123 $ids = GeneralUtility::trimExplode(',', $cKey, true);
2124 foreach ($ids as $idKey => $theUid) {
2125 if (is_array($mapArray)) {
2126 if ($mapArray[$table][$theUid]) {
2127 $ids[$idKey] = $mapArray[$table][$theUid];
2128 }
2129 } else {
2130 // Default, look for versions in workspace for record:
2131 $calcPRec = $this->getRecordForEdit((string)$table, (int)$theUid);
2132 if (is_array($calcPRec)) {
2133 // Setting UID again if it had changed, eg. due to workspace versioning.
2134 $ids[$idKey] = $calcPRec['uid'];
2135 }
2136 }
2137 }
2138 // Add the possibly manipulated IDs to the new-build newConf array:
2139 $newConf[implode(',', $ids)] = $cmd;
2140 } else {
2141 $newConf[$cKey] = $cmd;
2142 }
2143 }
2144 // Store the new conf array:
2145 $this->editconf[$table] = $newConf;
2146 }
2147 }
2148 }
2149 }
2150
2151 /**
2152 * Get record for editing.
2153 *
2154 * @param string $table Table name
2155 * @param int $theUid Record UID
2156 * @return array|false Returns record to edit, false if none
2157 */
2158 public function getRecordForEdit(string $table, int $theUid)
2159 {
2160 // Foreign class call? Method will be protected in v10, giving core freedom to move stuff around
2161 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
2162 if (end($backtrace)['class'] !== __CLASS__) {
2163 // @deprecated since TYPO3 v9, this method will be set to protected in v10
2164 trigger_error('Method getRecordForEdit() will be set to protected in v10. Do not call from other extension', E_USER_DEPRECATED);
2165 }
2166
2167 // Fetch requested record:
2168 $reqRecord = BackendUtility::getRecord($table, $theUid, 'uid,pid');
2169 if (is_array($reqRecord)) {
2170 // If workspace is OFFLINE:
2171 if ($this->getBackendUser()->workspace != 0) {
2172 // Check for versioning support of the table:
2173 if ($GLOBALS['TCA'][$table] && $GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
2174 // If the record is already a version of "something" pass it by.
2175 if ($reqRecord['pid'] == -1) {
2176 // (If it turns out not to be a version of the current workspace there will be trouble, but
2177 // that is handled inside DataHandler then and in the interface it would clearly be an error of
2178 // links if the user accesses such a scenario)
2179 return $reqRecord;
2180 }
2181 // The input record was online and an offline version must be found or made:
2182 // Look for version of this workspace:
2183 $versionRec = BackendUtility::getWorkspaceVersionOfRecord(
2184 $this->getBackendUser()->workspace,
2185 $table,
2186 $reqRecord['uid'],
2187 'uid,pid,t3ver_oid'
2188 );
2189 return is_array($versionRec) ? $versionRec : $reqRecord;
2190 }
2191 // This means that editing cannot occur on this record because it was not supporting versioning
2192 // which is required inside an offline workspace.
2193 return false;
2194 }
2195 // In ONLINE workspace, just return the originally requested record:
2196 return $reqRecord;
2197 }
2198 // Return FALSE because the table/uid was not found anyway.
2199 return false;
2200 }
2201
2202 /**
2203 * Populates the variables $this->storeArray, $this->storeUrl, $this->storeUrlMd5
2204 *
2205 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10
2206 */
2207 public function compileStoreDat()
2208 {
2209 trigger_error('Method compileStoreDat() will be removed in TYPO3 v10.', E_USER_DEPRECATED);
2210 $this->compileStoreData();
2211 }
2212
2213 /**
2214 * Populates the variables $this->storeArray, $this->storeUrl, $this->storeUrlMd5
2215 * to prepare 'open documents' urls
2216 */
2217 protected function compileStoreData(): void
2218 {
2219 // @todo: Refactor in v10: This GeneralUtility method fiddles with _GP()
2220 $this->storeArray = GeneralUtility::compileSelectedGetVarsFromArray(
2221 'edit,defVals,overrideVals,columnsOnly,noView,workspace',
2222 $this->R_URL_getvars
2223 );
2224 $this->storeUrl = GeneralUtility::implodeArrayForUrl('', $this->storeArray);
2225 $this->storeUrlMd5 = md5($this->storeUrl);
2226 }
2227
2228 /**
2229 * Function used to look for configuration of buttons in the form: Fx. disabling buttons or showing them at various
2230 * positions.
2231 *
2232 * @param string $table The table for which the configuration may be specific
2233 * @param string $key The option for look for. Default is checking if the saveDocNew button should be displayed.
2234 * @return string Return value fetched from USER TSconfig
2235 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10
2236 */
2237 public function getNewIconMode($table, $key = 'saveDocNew')
2238 {
2239 trigger_error('Method getNewIconMode() will be removed in TYPO3 v10.', E_USER_DEPRECATED);
2240 return $this->getTsConfigOption($table, $key);
2241 }
2242
2243 /**
2244 * Get a TSConfig 'option.' array, possibly for a specific table.
2245 *
2246 * @param string $table Table name
2247 * @param string $key Options key
2248 * @return string
2249 */
2250 protected function getTsConfigOption(string $table, string $key): string
2251 {
2252 $TsConfig = $this->getBackendUser()->getTSConfig('options.' . $key);
2253 return trim((string)($TsConfig['properties'][$table] ?? $TsConfig['value']));
2254 }
2255
2256 /**
2257 * Handling the closing of a document
2258 * The argument $mode can be one of this values:
2259 * - 0/1 will redirect to $this->retUrl [self::DOCUMENT_CLOSE_MODE_DEFAULT || self::DOCUMENT_CLOSE_MODE_REDIRECT]
2260 * - 3 will clear the docHandler (thus closing all documents) [self::DOCUMENT_CLOSE_MODE_CLEAR_ALL]
2261 * - 4 will do no redirect [self::DOCUMENT_CLOSE_MODE_NO_REDIRECT]
2262 * - other values will call setDocument with ->retUrl
2263 *
2264 * @param int $mode the close mode: one of self::DOCUMENT_CLOSE_MODE_*
2265 * @param $request ServerRequestInterface Incoming request
2266 * @return ResponseInterface|null Redirect response if needed
2267 */
2268 public function closeDocument($mode = self::DOCUMENT_CLOSE_MODE_DEFAULT, ServerRequestInterface $request = null): ?ResponseInterface
2269 {
2270 // Foreign class call or missing argument? Method will be protected and $request mandatory in v10, giving core freedom to move stuff around
2271 $deprecatedCaller = false;
2272 if ($request === null) {
2273 // Set method signature in v10 to: "protected function closeDocument($mode, ServerRequestInterface $request): ?ResponseInterface"
2274 trigger_error('@deprecated since v9, this method will be set to protected in v10', E_USER_DEPRECATED);
2275 $request = $GLOBALS['TYPO3_REQUEST'];
2276 $deprecatedCaller = true;
2277 }
2278
2279 $mode = (int)$mode;
2280 // If current document is found in docHandler,
2281 // then unset it, possibly unset it ALL and finally, write it to the session data
2282 if (isset($this->docHandler[$this->storeUrlMd5])) {
2283 // add the closing document to the recent documents
2284 $recentDocs = $this->getBackendUser()->getModuleData('opendocs::recent');
2285 if (!is_array($recentDocs)) {
2286 $recentDocs = [];
2287 }
2288 $closedDoc = $this->docHandler[$this->storeUrlMd5];
2289 $recentDocs = array_merge([$this->storeUrlMd5 => $closedDoc], $recentDocs);
2290 if (count($recentDocs) > 8) {
2291 $recentDocs = array_slice($recentDocs, 0, 8);
2292 }
2293 // remove it from the list of the open documents
2294 unset($this->docHandler[$this->storeUrlMd5]);
2295 if ($mode === self::DOCUMENT_CLOSE_MODE_CLEAR_ALL) {
2296 $recentDocs = array_merge($this->docHandler, $recentDocs);
2297 $this->docHandler = [];
2298 }
2299 $this->getBackendUser()->pushModuleData('opendocs::recent', $recentDocs);
2300 $this->getBackendUser()->pushModuleData('FormEngine', [$this->docHandler, $this->docDat[1]]);
2301 BackendUtility::setUpdateSignal('OpendocsController::updateNumber', count($this->docHandler));
2302 }
2303 if ($mode === self::DOCUMENT_CLOSE_MODE_NO_REDIRECT) {
2304 return null;
2305 }
2306 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
2307 // If ->returnEditConf is set, then add the current content of editconf to the ->retUrl variable: used by
2308 // other scripts, like wizard_add, to know which records was created or so...
2309 if ($this->returnEditConf && $this->retUrl != (string)$uriBuilder->buildUriFromRoute('dummy')) {
2310 $this->retUrl .= '&returnEditConf=' . rawurlencode(json_encode($this->editconf));
2311 }
2312 // If mode is NOT set (means 0) OR set to 1, then make a header location redirect to $this->retUrl
2313 if ($mode === self::DOCUMENT_CLOSE_MODE_DEFAULT || $mode === self::DOCUMENT_CLOSE_MODE_REDIRECT) {
2314 if ($deprecatedCaller) {
2315 // @deprecated fall back if method has been called from outside. This if can be removed in v10
2316 HttpUtility::redirect($this->retUrl);
2317 }
2318 return new RedirectResponse($this->retUrl, 303);
2319 }
2320 if ($this->retUrl === '') {
2321 return null;
2322 }
2323 $retUrl = $this->returnUrl;
2324 if (is_array($this->docHandler) && !empty($this->docHandler)) {
2325 if (!empty($setupArr[2])) {
2326 $sParts = parse_url($request->getAttribute('normalizedParams')->getRequestUri());
2327 $retUrl = $sParts['path'] . '?' . $setupArr[2] . '&returnUrl=' . rawurlencode($retUrl);
2328 }
2329 }
2330 if ($deprecatedCaller) {
2331 // @deprecated fall back if method has been called from outside. This if can be removed in v10
2332 HttpUtility::redirect($retUrl);
2333 }
2334 return new RedirectResponse($retUrl, 303);
2335 }
2336
2337 /**
2338 * Redirects to the document pointed to by $currentDocFromHandlerMD5 OR $retUrl,
2339 * depending on some internal calculations.
2340 *
2341 * @param string $currentDocFromHandlerMD5 Pointer to the document in the docHandler array
2342 * @param string $retUrl Alternative/Default retUrl
2343 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10
2344 */
2345 public function setDocument($currentDocFromHandlerMD5 = '', $retUrl = '')
2346 {
2347 trigger_error('This method will be removed in TYPO3 v10.', E_USER_DEPRECATED);
2348 if ($retUrl === '') {
2349 return;
2350 }
2351 if (is_array($this->docHandler) && !empty($this->docHandler)) {
2352 if (isset($this->docHandler[$currentDocFromHandlerMD5])) {
2353 $setupArr = $this->docHandler[$currentDocFromHandlerMD5];
2354 } else {
2355 $setupArr = reset($this->docHandler);
2356 }
2357 if ($setupArr[2]) {
2358 $sParts = parse_url(GeneralUtility::getIndpEnv('REQUEST_URI'));
2359 $retUrl = $sParts['path'] . '?' . $setupArr[2] . '&returnUrl=' . rawurlencode($retUrl);
2360 }
2361 }
2362 HttpUtility::redirect($retUrl);
2363 }
2364
2365 /**
2366 * Emits a signal after a function was executed
2367 *
2368 * @param string $signalName
2369 * @param ServerRequestInterface $request
2370 */
2371 protected function emitFunctionAfterSignal($signalName, ServerRequestInterface $request): void
2372 {
2373 $this->getSignalSlotDispatcher()->dispatch(__CLASS__, $signalName . 'After', [$this, 'request' => $request]);
2374 }
2375
2376 /**
2377 * Get the SignalSlot dispatcher
2378 *
2379 * @return \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
2380 */
2381 protected function getSignalSlotDispatcher()
2382 {
2383 if (!isset($this->signalSlotDispatcher)) {
2384 $this->signalSlotDispatcher = GeneralUtility::makeInstance(Dispatcher::class);
2385 }
2386 return $this->signalSlotDispatcher;
2387 }
2388
2389 /**
2390 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
2391 */
2392 protected function getBackendUser()
2393 {
2394 return $GLOBALS['BE_USER'];
2395 }
2396
2397 /**
2398 * Returns LanguageService
2399 *
2400 * @return \TYPO3\CMS\Core\Localization\LanguageService
2401 */
2402 protected function getLanguageService()
2403 {
2404 return $GLOBALS['LANG'];
2405 }
2406 }