5b099b612b99811aa72d1fdd688ba86aa81c3ac5
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Rte / AbstractRte.php
1 <?php
2 namespace TYPO3\CMS\Backend\Rte;
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\Backend\Form\Element\TextElement;
18 use TYPO3\CMS\Backend\Template\DocumentTemplate;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * RTE base class: Delivers browser-detection, TCEforms binding and transformation routines
23 * for the "rte" extension, registering it with the RTE API
24 * See "rte" extension for usage.
25 *
26 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
27 */
28 class AbstractRte {
29
30 /**
31 * Error messages regarding non-availability is collected here.
32 *
33 * @var array
34 */
35 public $errorLog = array();
36
37 /**
38 * Set this to the extension key of the RTE so it can identify itself.
39 *
40 * @var string
41 */
42 public $ID = '';
43
44 /***********************************
45 *
46 * Main API functions;
47 * When you create alternative RTEs, simply override these functions in your parent class.
48 * See the "rte" or "rtehtmlarea" extension as an example!
49 *
50 **********************************/
51 /**
52 * Returns TRUE if the RTE is available. Here you check if the browser requirements are met.
53 * If there are reasons why the RTE cannot be displayed you simply enter them as text in ->errorLog
54 *
55 * @return bool TRUE if this RTE object offers an RTE in the current browser environment
56 */
57 public function isAvailable() {
58 return TRUE;
59 }
60
61 /**
62 * Draws the RTE as a form field or whatever is needed (inserts JavaApplet, creates iframe, renders ....)
63 * Default is to output the transformed content in a plain textarea field. This mode is great for debugging transformations!
64 *
65 * @param object $pObj parent object
66 * @param string $table The table name
67 * @param string $field The field name
68 * @param array $row The current row from which field is being rendered
69 * @param array $PA Array of standard content for rendering form fields from TCEforms. See TCEforms for details on this. Includes for instance the value and the form field name, java script actions and more.
70 * @param array $specConf "special" configuration - what is found at position 4 in the types configuration of a field from record, parsed into an array.
71 * @param array $thisConfig Configuration for RTEs; A mix between TSconfig and otherwise. Contains configuration for display, which buttons are enabled, additional transformation information etc.
72 * @param string $RTEtypeVal Record "type" field value.
73 * @param string $RTErelPath Relative path for images/links in RTE; this is used when the RTE edits content from static files where the path of such media has to be transformed forth and back!
74 * @param int $thePidValue PID value of record (true parent page id)
75 * @return string HTML code for RTE!
76 */
77 public function drawRTE(&$pObj, $table, $field, $row, $PA, $specConf, $thisConfig, $RTEtypeVal, $RTErelPath, $thePidValue) {
78 // Transform value:
79 $value = $this->transformContent('rte', $PA['itemFormElValue'], $table, $field, $row, $specConf, $thisConfig, $RTErelPath, $thePidValue);
80 // Create item:
81 $item = '
82 ' . $this->triggerField($PA['itemFormElName']) . '
83 <textarea name="' . htmlspecialchars($PA['itemFormElName']) . '"' . $pObj->formWidthText('48', 'off') . ' rows="20" wrap="off" style="background-color: #99eebb;">' . GeneralUtility::formatForTextarea($value) . '</textarea>';
84 // Return form item:
85 return $item;
86 }
87
88 /**
89 * Performs transformation of content to/from RTE. The keyword $dirRTE determines the direction.
90 * This function is called in two situations:
91 * a) Right before content from database is sent to the RTE (see ->drawRTE()) it might need transformation
92 * b) When content is sent from the RTE and into the database it might need transformation back again (going on in TCEmain class; You can't affect that.)
93 *
94 * @param string $dirRTE Keyword: "rte" means direction from db to rte, "db" means direction from Rte to DB
95 * @param string $value Value to transform.
96 * @param string $table The table name
97 * @param string $field The field name
98 * @param array $row The current row from which field is being rendered
99 * @param array $specConf "special" configuration - what is found at position 4 in the types configuration of a field from record, parsed into an array.
100 * @param array $thisConfig Configuration for RTEs; A mix between TSconfig and otherwise. Contains configuration for display, which buttons are enabled, additional transformation information etc.
101 * @param string $RTErelPath Relative path for images/links in RTE; this is used when the RTE edits content from static files where the path of such media has to be transformed forth and back!
102 * @param int $pid PID value of record (true parent page id)
103 * @return string Transformed content
104 */
105 public function transformContent($dirRTE, $value, $table, $field, $row, $specConf, $thisConfig, $RTErelPath, $pid) {
106 if ($specConf['rte_transform']) {
107 $p = \TYPO3\CMS\Backend\Utility\BackendUtility::getSpecConfParametersFromArray($specConf['rte_transform']['parameters']);
108 // There must be a mode set for transformation
109 if ($p['mode']) {
110 // Initialize transformation:
111 $parseHTML = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Html\RteHtmlParser::class);
112 $parseHTML->init($table . ':' . $field, $pid);
113 $parseHTML->setRelPath($RTErelPath);
114 // Perform transformation:
115 $value = $parseHTML->RTE_transform($value, $specConf, $dirRTE, $thisConfig);
116 }
117 }
118 return $value;
119 }
120
121 /***********************************
122 *
123 * Helper functions
124 *
125 **********************************/
126 /**
127 * Trigger field - this field tells the TCEmain that processing should be done on this value!
128 *
129 * @param string $fieldName Field name of the RTE field.
130 * @return string <input> field of type "hidden" with a flag telling the TCEmain that this fields content should be transformed back to database state.
131 */
132 public function triggerField($fieldName) {
133 $triggerFieldName = preg_replace('/\\[([^]]+)\\]$/', '[_TRANSFORM_\\1]', $fieldName);
134 return '<input type="hidden" name="' . htmlspecialchars($triggerFieldName) . '" value="RTE" />';
135 }
136
137 }