469cfff61b76b4b3e7dcc7d2101c8f0057ce0e8e
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / EditFileController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller\File;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Backend\Utility\BackendUtility;
31 use TYPO3\CMS\Backend\Utility\IconUtility;
32 use TYPO3\CMS\Core\Utility\GeneralUtility;
33
34 /**
35 * Script Class for rendering the file editing screen
36 *
37 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
38 */
39 class EditFileController {
40
41 // Module content accumulated.
42 /**
43 * @todo Define visibility
44 */
45 public $content;
46
47 /**
48 * @todo Define visibility
49 */
50 public $title;
51
52 /**
53 * Document template object
54 *
55 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
56 * @todo Define visibility
57 */
58 public $doc;
59
60 // Internal, static: GPvar
61 // Original input target
62 /**
63 * @todo Define visibility
64 */
65 public $origTarget;
66
67 // The original target, but validated.
68 /**
69 * @todo Define visibility
70 */
71 public $target;
72
73 // Return URL of list module.
74 /**
75 * @todo Define visibility
76 */
77 public $returnUrl;
78
79 /**
80 * the file that is being edited on
81 *
82 * @var \TYPO3\CMS\Core\Resource\AbstractFile
83 */
84 protected $fileObject;
85
86 /**
87 * Initialize script class
88 *
89 * @return void
90 * @todo Define visibility
91 */
92 public function init() {
93 // Setting target, which must be a file reference to a file within the mounts.
94 $this->target = ($this->origTarget = ($fileIdentifier = GeneralUtility::_GP('target')));
95 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
96 // create the file object
97 if ($fileIdentifier) {
98 $this->fileObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($fileIdentifier);
99 }
100 // Cleaning and checking target directory
101 if (!$this->fileObject) {
102 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', TRUE);
103 $message = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', TRUE);
104 throw new \RuntimeException($title . ': ' . $message, 1294586841);
105 }
106 // Setting the title and the icon
107 $icon = IconUtility::getSpriteIcon('apps-filetree-root');
108 $this->title = $icon . htmlspecialchars($this->fileObject->getStorage()->getName()) . ': ' . htmlspecialchars($this->fileObject->getIdentifier());
109
110 // Setting template object
111 $this->doc = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
112 $this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/file_edit.html');
113 $this->doc->backPath = $GLOBALS['BACK_PATH'];
114 $this->doc->JScode = $this->doc->wrapScriptTags('
115 function backToList() { //
116 top.goToModule("file_list");
117 }
118 ');
119 $this->doc->form = '<form action="tce_file.php" method="post" name="editform">';
120 }
121
122 /**
123 * Main function, redering the actual content of the editing page
124 *
125 * @return void
126 * @todo Define visibility
127 */
128 public function main() {
129 $docHeaderButtons = $this->getButtons();
130 $this->content = $this->doc->startPage($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.pagetitle'));
131 // Hook before compiling the output
132 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['preOutputProcessingHook'])) {
133 $preOutputProcessingHook = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['preOutputProcessingHook'];
134 if (is_array($preOutputProcessingHook)) {
135 $hookParameters = array(
136 'content' => &$this->content,
137 'target' => &$this->target
138 );
139 foreach ($preOutputProcessingHook as $hookFunction) {
140 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
141 }
142 }
143 }
144 $pageContent = $this->doc->header($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.pagetitle') . ' ' . htmlspecialchars($this->fileObject->getName()));
145 $pageContent .= $this->doc->spacer(2);
146 $code = '';
147 $extList = $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'];
148 if ($extList && GeneralUtility::inList($extList, $this->fileObject->getExtension())) {
149 // Read file content to edit:
150 $fileContent = $this->fileObject->getContents();
151 // Making the formfields
152 $hValue = 'file_edit.php?target=' . rawurlencode($this->origTarget) . '&returnUrl=' . rawurlencode($this->returnUrl);
153 // Edit textarea:
154 $code .= '
155 <div id="c-edit">
156 <textarea rows="30" name="file[editfile][0][data]" wrap="off"' . $this->doc->formWidthText(48, 'width:98%;height:80%', 'off') . ' class="fixed-font enable-tab">' . GeneralUtility::formatForTextarea($fileContent) . '</textarea>
157 <input type="hidden" name="file[editfile][0][target]" value="' . $this->fileObject->getUid() . '" />
158 <input type="hidden" name="redirect" value="' . htmlspecialchars($hValue) . '" />
159 </div>
160 <br />';
161 // Make shortcut:
162 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
163 $this->MCONF['name'] = 'xMOD_file_edit.php';
164 $docHeaderButtons['shortcut'] = $this->doc->makeShortcutIcon('target', '', $this->MCONF['name'], 1);
165 } else {
166 $docHeaderButtons['shortcut'] = '';
167 }
168 } else {
169 $code .= sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.coundNot'), $extList);
170 }
171 // Ending of section and outputting editing form:
172 $pageContent .= $this->doc->sectionEnd();
173 $pageContent .= $code;
174 // Hook after compiling the output
175 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['postOutputProcessingHook'])) {
176 $postOutputProcessingHook = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/file_edit.php']['postOutputProcessingHook'];
177 if (is_array($postOutputProcessingHook)) {
178 $hookParameters = array(
179 'pageContent' => &$pageContent,
180 'target' => &$this->target
181 );
182 foreach ($postOutputProcessingHook as $hookFunction) {
183 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
184 }
185 }
186 }
187 // Add the HTML as a section:
188 $markerArray = array(
189 'CSH' => $docHeaderButtons['csh'],
190 'FUNC_MENU' => BackendUtility::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
191 'BUTTONS' => $docHeaderButtons,
192 'PATH' => $this->title,
193 'CONTENT' => $pageContent
194 );
195 $this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
196 $this->content .= $this->doc->endPage();
197 $this->content = $this->doc->insertStylesAndJS($this->content);
198 }
199
200 /**
201 * Outputting the accumulated content to screen
202 *
203 * @return void
204 * @todo Define visibility
205 */
206 public function printContent() {
207 echo $this->content;
208 }
209
210 /**
211 * Builds the buttons for the docheader and returns them as an array
212 *
213 * @return array
214 * @todo Define visibility
215 */
216 public function getButtons() {
217 $buttons = array();
218 // CSH button
219 $buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_edit', $GLOBALS['BACK_PATH'], '', TRUE);
220 // Save button
221 $theIcon = IconUtility::getSpriteIcon('actions-document-save');
222 $buttons['SAVE'] = '<a href="#" onclick="document.editform.submit();" title="' . $GLOBALS['LANG']->makeEntities($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.submit', TRUE)) . '">' . $theIcon . '</a>';
223 // Save and Close button
224 $theIcon = IconUtility::getSpriteIcon('actions-document-save-close');
225 $buttons['SAVE_CLOSE'] = '<a href="#" onclick="document.editform.redirect.value=\'' . htmlspecialchars($this->returnUrl) . '\'; document.editform.submit();" title="' . $GLOBALS['LANG']->makeEntities($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:file_edit.php.saveAndClose', TRUE)) . '">' . $theIcon . '</a>';
226 // Cancel button
227 $theIcon = IconUtility::getSpriteIcon('actions-document-close');
228 $buttons['CANCEL'] = '<a href="#" onclick="backToList(); return false;" title="' . $GLOBALS['LANG']->makeEntities($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.cancel', TRUE)) . '">' . $theIcon . '</a>';
229 return $buttons;
230 }
231
232 }
233
234 ?>