a46a021a54d5d005a4f97677a7e61a59dcc2bf33
[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\Utility\GeneralUtility;
18
19 /**
20 * File edit hook for t3editor
21 *
22 * @author Tobias Liebig <mail_typo3@etobi.de>
23 */
24 class FileEditHook {
25
26 /**
27 * @var \TYPO3\CMS\T3editor\T3editor
28 */
29 protected $t3editor = NULL;
30
31 /**
32 * @var string
33 */
34 protected $ajaxSaveType = 'TypoScriptTemplateInformationModuleFunctionController';
35
36 /**
37 * @return \TYPO3\CMS\T3editor\T3editor
38 */
39 protected function getT3editor() {
40 if ($this->t3editor == NULL) {
41 $this->t3editor = GeneralUtility::makeInstance(\TYPO3\CMS\T3editor\T3editor::class)->setAjaxSaveType($this->ajaxSaveType);
42 }
43 return $this->t3editor;
44 }
45
46 /**
47 * Hook-function: inject t3editor JavaScript code before the page is compiled
48 * called in file_edit module
49 *
50 * @param array $parameters
51 * @param \TYPO3\CMS\Backend\Controller\File\EditFileController $pObj
52 */
53 public function preOutputProcessingHook($parameters, $pObj) {
54 $t3editor = $this->getT3editor();
55 $t3editor->setModeByFile($parameters['target']);
56 if (!$t3editor->isEnabled() || !$t3editor->getMode()) {
57 return;
58 }
59 $parameters['content'] = str_replace('<!--###POSTJSMARKER###-->', '<!--###POSTJSMARKER###-->' . $t3editor->getModeSpecificJavascriptCode(), $parameters['content']);
60 }
61
62 /**
63 * Hook-function: inject t3editor JavaScript code before the page is compiled
64 * called in \TYPO3\CMS\Backend\Template\DocumentTemplate:startPage
65 *
66 * @param array $parameters
67 * @param \TYPO3\CMS\Backend\Template\DocumentTemplate $documentTemplate
68 * @see \TYPO3\CMS\Backend\Template\DocumentTemplate::startPage
69 */
70 public function preStartPageHook($parameters, $documentTemplate) {
71 if (GeneralUtility::_GET('M') === 'file_edit') {
72 $t3editor = $this->getT3editor();
73 if (!$t3editor->isEnabled()) {
74 return;
75 }
76 $documentTemplate->JScode .= $t3editor->getJavascriptCode($documentTemplate);
77 $documentTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/T3editor/FileEdit');
78 }
79 }
80
81 /**
82 * Hook-function:
83 * called in file_edit module
84 *
85 * @param array $parameters
86 * @param \TYPO3\CMS\Backend\Controller\File\EditFileController $pObj
87 */
88 public function postOutputProcessingHook($parameters, $pObj) {
89 $t3editor = $this->getT3editor();
90 if (!$t3editor->isEnabled() || !$t3editor->getMode()) {
91 return;
92 }
93 $attributes = 'rows="30" ' . 'wrap="off" ' . $pObj->doc->formWidth(48, TRUE, 'width:98%;height:60%');
94 $title = $GLOBALS['LANG']->getLL('file') . ' ' . htmlspecialchars($pObj->target);
95 $outCode = $t3editor->getCodeEditor('file[editfile][0][data]', 'text-monospace enable-tab', '$1', $attributes, $title, array(
96 'target' => (int)$pObj->target
97 ));
98 $parameters['pageContent'] = preg_replace('/\\<textarea .*name="file\\[editfile\\]\\[0\\]\\[data\\]".*\\>([^\\<]*)\\<\\/textarea\\>/mi', $outCode, $parameters['pageContent']);
99 }
100
101 /**
102 * @param array $parameters
103 * @param mixed $pObj
104 *
105 * @return bool TRUE if successful
106 */
107 public function save($parameters, $pObj) {
108 $savingsuccess = FALSE;
109 if ($parameters['type'] == $this->ajaxSaveType) {
110 $tceFile = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Controller\File\FileController::class);
111 $tceFile->processAjaxRequest(array(), $parameters['ajaxObj']);
112 $result = $parameters['ajaxObj']->getContent('result');
113 $savingsuccess = is_array($result) && $result['editfile'][0];
114 }
115 return $savingsuccess;
116 }
117
118 }