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