[TASK] Make file_edit.php dispatched
[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')->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 $pObj
68 * @see \TYPO3\CMS\Backend\Template\DocumentTemplate::startPage
69 */
70 public function preStartPageHook($parameters, $pObj) {
71 // The preg_match call is deprecated and can be removed if the file typo3/file_edit.php is removed.
72 if (GeneralUtility::_GET('M') === 'file_edit' || preg_match('/typo3\\/file_edit\\.php/', $_SERVER['SCRIPT_NAME'])) {
73 $t3editor = $this->getT3editor();
74 if (!$t3editor->isEnabled()) {
75 return;
76 }
77 $pObj->JScode .= $t3editor->getJavascriptCode($pObj);
78 $pObj->loadJavascriptLib(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath('t3editor') . 'res/jslib/fileedit.js');
79 }
80 }
81
82 /**
83 * Hook-function:
84 * called in file_edit module
85 *
86 * @param array $parameters
87 * @param \TYPO3\CMS\Backend\Controller\File\EditFileController $pObj
88 */
89 public function postOutputProcessingHook($parameters, $pObj) {
90 $t3editor = $this->getT3editor();
91 if (!$t3editor->isEnabled() || !$t3editor->getMode()) {
92 return;
93 }
94 $attributes = 'rows="30" ' . 'wrap="off" ' . $pObj->doc->formWidth(48, TRUE, 'width:98%;height:60%');
95 $title = $GLOBALS['LANG']->getLL('file') . ' ' . htmlspecialchars($pObj->target);
96 $outCode = $t3editor->getCodeEditor('file[editfile][0][data]', 'fixed-font enable-tab', '$1', $attributes, $title, array(
97 'target' => (int)$pObj->target
98 ));
99 $parameters['pageContent'] = preg_replace('/\\<textarea .*name="file\\[editfile\\]\\[0\\]\\[data\\]".*\\>([^\\<]*)\\<\\/textarea\\>/mi', $outCode, $parameters['pageContent']);
100 }
101
102 /**
103 * @param array $parameters
104 * @param mixed $pObj
105 *
106 * @return boolean TRUE if successful
107 */
108 public function save($parameters, $pObj) {
109 $savingsuccess = FALSE;
110 if ($parameters['type'] == $this->ajaxSaveType) {
111 $tceFile = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Controller\\File\\FileController');
112 $tceFile->processAjaxRequest(array(), $parameters['ajaxObj']);
113 $result = $parameters['ajaxObj']->getContent('result');
114 $savingsuccess = is_array($result) && $result['editfile'][0];
115 }
116 return $savingsuccess;
117 }
118
119 }