Added feature #15950: Use t3editor in file_edit to edit files in fileadmin
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / classes / class.tx_t3editor_hooks_fileedit.php
index 54d4c9b..4110342 100644 (file)
@@ -149,6 +149,162 @@ class tx_t3editor_hooks_fileedit {
 }\r
 \r
 \r
+if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/t3editor/classes/class.tx_t3editor_hooks_fileedit.php']) {\r
+       include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/t3editor/classes/class.tx_t3editor_hooks_fileedit.php']);\r
+}\r
+\r
+?>\r
+<?php\r
+/***************************************************************\r
+*  Copyright notice\r
+*\r
+*  (c) 2010 Tobias Liebig <mail_typo3@etobi.de>\r
+*  All rights reserved\r
+*\r
+*  This script is part of the TYPO3 project. The TYPO3 project is\r
+*  free software; you can redistribute it and/or modify\r
+*  it under the terms of the GNU General Public License as published by\r
+*  the Free Software Foundation; either version 2 of the License, or\r
+*  (at your option) any later version.\r
+*\r
+*  The GNU General Public License can be found at\r
+*  http://www.gnu.org/copyleft/gpl.html.\r
+*  A copy is found in the textfile GPL.txt and important notices to the license\r
+*  from the author is found in LICENSE.txt distributed with these scripts.\r
+*\r
+*\r
+*  This script is distributed in the hope that it will be useful,\r
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+*  GNU General Public License for more details.\r
+*\r
+*  This copyright notice MUST APPEAR in all copies of the script!\r
+***************************************************************/\r
+\r
+require_once(t3lib_extMgm::extPath('t3editor', 'classes/class.tx_t3editor.php'));\r
+\r
+class tx_t3editor_hooks_fileedit {\r
+\r
+       /**\r
+        * @var tx_t3editor\r
+        */\r
+       protected $t3editor = NULL;\r
+       \r
+       /**\r
+        * @var string\r
+        */\r
+       protected $ajaxSaveType = 'tx_tstemplateinfo';\r
+\r
+       /**\r
+        * @return tx_t3editor\r
+        */\r
+       protected function getT3editor() {\r
+               if ($this->t3editor == NULL) {\r
+                       $this->t3editor = t3lib_div::makeInstance('tx_t3editor')\r
+                               ->setAjaxSaveType($this->ajaxSaveType);\r
+               }\r
+               return $this->t3editor;\r
+       }\r
+\r
+       /**\r
+        * Hook-function: inject t3editor JavaScript code before the page is compiled\r
+        * called in file_edit.php:SC_file_edit->main\r
+        *\r
+        * @param array $parameters\r
+        * @param SC_file_edit $pObj\r
+        */\r
+       public function preOutputProcessingHook($parameters, $pObj) {\r
+               $t3editor = $this->getT3editor();\r
+               $t3editor->setModeByFile($parameters['target']);\r
+\r
+               if (!$t3editor->isEnabled() || !$t3editor->getMode()) {\r
+                       return;\r
+               }\r
+\r
+               $parameters['content'] = str_replace(\r
+                       '<!--###POSTJSMARKER###-->',\r
+                       '<!--###POSTJSMARKER###-->' . $t3editor->getModeSpecificJavascriptCode(),\r
+                       $parameters['content']\r
+               );\r
+       }\r
+\r
+       /**\r
+        * Hook-function: inject t3editor JavaScript code before the page is compiled\r
+        * called in typo3/template.php:startPage\r
+        *\r
+        * @param array $parameters\r
+        * @param template $pObj\r
+        */\r
+       public function preStartPageHook($parameters, $pObj) {\r
+               if (preg_match('/typo3\/file_edit\.php/', $_SERVER['SCRIPT_NAME'])) {\r
+                       $t3editor = $this->getT3editor();\r
+                       if (!$t3editor->isEnabled()) {\r
+                               return;\r
+                       }\r
+                       $pObj->JScode .= $t3editor->getJavascriptCode($pObj);\r
+               }\r
+       }\r
+\r
+       /**\r
+        * Hook-function:\r
+        * called in file_edit.php:SC_file_edit->main\r
+        *\r
+        * @param array $parameters\r
+        * @param SC_file_edit $pObj\r
+        */\r
+       public function postOutputProcessingHook($parameters, $pObj) {\r
+               $t3editor = $this->getT3editor();\r
+\r
+               if (!$t3editor->isEnabled() || !$t3editor->getMode()) {\r
+                       return;\r
+               }\r
+               \r
+               $attributes = 'rows="30" ' .\r
+                       'wrap="off" ' .\r
+                       $pObj->doc->formWidthText(48, 'width:98%;height:60%', 'off');\r
+\r
+               $title = $GLOBALS['LANG']->getLL('file') . ' ' .\r
+                       htmlspecialchars($pObj->target);\r
+\r
+               $outCode = $t3editor->getCodeEditor(\r
+                       'file[editfile][0][data]',\r
+                       'fixed-font enable-tab',\r
+                       '$1', // will be replaced with the actual code later, see preg_replace below\r
+                       $attributes,\r
+                       $title,\r
+                       array(\r
+                               'target' => intval($pObj->target)\r
+                       )\r
+               );\r
+\r
+               $parameters['pageContent'] = preg_replace(\r
+                       '/\<textarea .*name="file\[editfile\]\[0\]\[data\]".*\>([^\<]*)\<\/textarea\>/mi',\r
+                       $outCode,\r
+                       $parameters['pageContent']\r
+               );\r
+\r
+       }\r
+\r
+       /**\r
+        * @return boolean true if successful\r
+        */\r
+       public function save($parameters, $pObj) {\r
+               $savingsuccess = false;\r
+               if ($parameters['type'] == $this->ajaxSaveType) {\r
+                       require_once('init.php');\r
+                       require_once('classes/class.typo3_tcefile.php');\r
+\r
+                       $tceFile = t3lib_div::makeInstance('TYPO3_tcefile');\r
+                       $tceFile->processAjaxRequest(array(), $parameters['ajaxObj']);\r
+\r
+                       $result = $parameters['ajaxObj']->getContent('result');\r
+                       $savingsuccess = is_array($result) && $result['editfile'][0];\r
+               }\r
+               return $savingsuccess;\r
+       }\r
+}\r
+\r
+\r
 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/t3editor/classes/class.tx_t3editor_hooks_fileedit.php']) {\r
        include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/t3editor/classes/class.tx_t3editor_hooks_fileedit.php']);\r
 }\r