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