[!!!][FEATURE] Introduce PSR-7-based Routing for Backend AJAX Requests
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / Classes / Hook / FileEditHook.php
1 <?php
2 namespace TYPO3\CMS\T3editor\Hook;
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 TYPO3\CMS\Core\Page\PageRenderer;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * File edit hook for t3editor
22 */
23 class FileEditHook {
24
25 /**
26 * @var \TYPO3\CMS\T3editor\T3editor
27 */
28 protected $t3editor = NULL;
29
30 /**
31 * @var string
32 */
33 protected $ajaxSaveType = 'TypoScriptTemplateInformationModuleFunctionController';
34
35 /**
36 * @return \TYPO3\CMS\T3editor\T3editor
37 */
38 protected function getT3editor() {
39 if ($this->t3editor === NULL) {
40 $this->t3editor = GeneralUtility::makeInstance(\TYPO3\CMS\T3editor\T3editor::class)->setAjaxSaveType($this->ajaxSaveType);
41 }
42 return $this->t3editor;
43 }
44
45 /**
46 * Hook-function: inject t3editor JavaScript code before the page is compiled
47 * called in file_edit module
48 *
49 * @param array $parameters
50 * @param \TYPO3\CMS\Backend\Controller\File\EditFileController $pObj
51 */
52 public function preOutputProcessingHook($parameters, $pObj) {
53 $t3editor = $this->getT3editor();
54 $t3editor->setModeByFile($parameters['target']);
55 if (!$t3editor->getMode()) {
56 return;
57 }
58 }
59
60 /**
61 * Hook-function: inject t3editor JavaScript code before the page is compiled
62 * called in \TYPO3\CMS\Backend\Template\DocumentTemplate:startPage
63 *
64 * @param array $parameters
65 * @param \TYPO3\CMS\Backend\Template\DocumentTemplate $documentTemplate
66 * @see \TYPO3\CMS\Backend\Template\DocumentTemplate::startPage
67 */
68 public function preStartPageHook($parameters, $documentTemplate) {
69 // @todo: this is a workaround. Ideally the document template holds the current request so we can match the route
70 // against the name of the route and not the GET parameter
71 if (GeneralUtility::_GET('route') === '/file/editcontent') {
72 $t3editor = $this->getT3editor();
73 $t3editor->getJavascriptCode($documentTemplate);
74 $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/T3editor/FileEdit');
75 }
76 }
77
78 /**
79 * Hook-function:
80 * called in file_edit module
81 *
82 * @param array $parameters
83 * @param \TYPO3\CMS\Backend\Controller\File\EditFileController $pObj
84 */
85 public function postOutputProcessingHook($parameters, $pObj) {
86 $t3editor = $this->getT3editor();
87 if (!$t3editor->getMode()) {
88 return;
89 }
90 $attributes = 'rows="30" ' . 'wrap="off" ' . $pObj->doc->formWidth(48, TRUE, 'width:98%;height:60%');
91 $title = $GLOBALS['LANG']->getLL('file') . ' ' . htmlspecialchars($pObj->target);
92 $outCode = $t3editor->getCodeEditor('file[editfile][0][data]', 'text-monospace enable-tab', '$1', $attributes, $title, array(
93 'target' => (int)$pObj->target
94 ));
95 $parameters['pageContent'] = preg_replace('/\\<textarea .*name="file\\[editfile\\]\\[0\\]\\[data\\]".*\\>([^\\<]*)\\<\\/textarea\\>/mi', $outCode, $parameters['pageContent']);
96 }
97
98 /**
99 * @param array $parameters
100 * @param mixed $pObj
101 *
102 * @return bool TRUE if successful
103 */
104 public function save($parameters, $pObj) {
105 $savingsuccess = FALSE;
106 if ($parameters['type'] === $this->ajaxSaveType) {
107 /** @var \TYPO3\CMS\Backend\Controller\File\FileController $tceFile */
108 $tceFile = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Controller\File\FileController::class);
109 $response = $tceFile->processAjaxRequest($parameters['request'], $parameters['response']);
110 $result = json_decode((string)$response->getBody(), TRUE)['result'];
111 $savingsuccess = is_array($result) && $result['editfile'][0];
112 }
113 return $savingsuccess;
114 }
115
116 /**
117 * @return PageRenderer
118 */
119 protected function getPageRenderer() {
120 return GeneralUtility::makeInstance(PageRenderer::class);
121 }
122
123 }