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