1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage Form
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 JFormHelper::loadFieldClass('textarea');
13
14 /**
15 * A textarea field for content creation
16 *
17 * @see JEditor
18 * @since 1.6
19 */
20 class JFormFieldEditor extends JFormFieldTextarea
21 {
22 /**
23 * The form field type.
24 *
25 * @var string
26 * @since 1.6
27 */
28 public $type = 'Editor';
29
30 /**
31 * The JEditor object.
32 *
33 * @var JEditor
34 * @since 1.6
35 */
36 protected $editor;
37
38 /**
39 * The height of the editor.
40 *
41 * @var string
42 * @since 3.2
43 */
44 protected $height;
45
46 /**
47 * The width of the editor.
48 *
49 * @var string
50 * @since 3.2
51 */
52 protected $width;
53
54 /**
55 * The assetField of the editor.
56 *
57 * @var string
58 * @since 3.2
59 */
60 protected $assetField;
61
62 /**
63 * The authorField of the editor.
64 *
65 * @var string
66 * @since 3.2
67 */
68 protected $authorField;
69
70 /**
71 * The asset of the editor.
72 *
73 * @var string
74 * @since 3.2
75 */
76 protected $asset;
77
78 /**
79 * The buttons of the editor.
80 *
81 * @var mixed
82 * @since 3.2
83 */
84 protected $buttons;
85
86 /**
87 * The hide of the editor.
88 *
89 * @var array
90 * @since 3.2
91 */
92 protected $hide;
93
94 /**
95 * The editorType of the editor.
96 *
97 * @var array
98 * @since 3.2
99 */
100 protected $editorType;
101
102 /**
103 * Method to get certain otherwise inaccessible properties from the form field object.
104 *
105 * @param string $name The property name for which to the the value.
106 *
107 * @return mixed The property value or null.
108 *
109 * @since 3.2
110 */
111 public function __get($name)
112 {
113 switch ($name)
114 {
115 case 'height':
116 case 'width':
117 case 'assetField':
118 case 'authorField':
119 case 'asset':
120 case 'buttons':
121 case 'hide':
122 case 'editorType':
123 return $this->$name;
124 }
125
126 return parent::__get($name);
127 }
128
129 /**
130 * Method to set certain otherwise inaccessible properties of the form field object.
131 *
132 * @param string $name The property name for which to the the value.
133 * @param mixed $value The value of the property.
134 *
135 * @return void
136 *
137 * @since 3.2
138 */
139 public function __set($name, $value)
140 {
141 switch ($name)
142 {
143 case 'height':
144 case 'width':
145 case 'assetField':
146 case 'authorField':
147 case 'asset':
148 $this->$name = (string) $value;
149 break;
150
151 case 'buttons':
152 $value = (string) $value;
153
154 if ($value === 'true' || $value === 'yes' || $value === '1')
155 {
156 $this->buttons = true;
157 }
158 elseif ($value === 'false' || $value === 'no' || $value === '0')
159 {
160 $this->buttons = false;
161 }
162 else
163 {
164 $this->buttons = explode(',', $value);
165 }
166 break;
167
168 case 'hide':
169 $value = (string) $value;
170 $this->hide = $value ? explode(',', $value) : array();
171 break;
172
173 case 'editorType':
174 // Can be in the form of: editor="desired|alternative".
175 $this->editorType = explode('|', trim((string) $value));
176 break;
177
178 default:
179 parent::__set($name, $value);
180 }
181 }
182
183 /**
184 * Method to attach a JForm object to the field.
185 *
186 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
187 * @param mixed $value The form field value to validate.
188 * @param string $group The field name group control value. This acts as an array container for the field.
189 * For example if the field has name="foo" and the group value is set to "bar" then the
190 * full field name would end up being "bar[foo]".
191 *
192 * @return boolean True on success.
193 *
194 * @see JFormField::setup()
195 * @since 3.2
196 */
197 public function setup(SimpleXMLElement $element, $value, $group = null)
198 {
199 $result = parent::setup($element, $value, $group);
200
201 if ($result === true)
202 {
203 $this->height = $this->element['height'] ? (string) $this->element['height'] : '500';
204 $this->width = $this->element['width'] ? (string) $this->element['width'] : '100%';
205 $this->assetField = $this->element['asset_field'] ? (string) $this->element['asset_field'] : 'asset_id';
206 $this->authorField = $this->element['created_by_field'] ? (string) $this->element['created_by_field'] : 'created_by';
207 $this->asset = $this->form->getValue($this->assetField) ?: (string) $this->element['asset_id'];
208
209 $buttons = (string) $this->element['buttons'];
210 $hide = (string) $this->element['hide'];
211 $editorType = (string) $this->element['editor'];
212
213 if ($buttons === 'true' || $buttons === 'yes' || $buttons === '1')
214 {
215 $this->buttons = true;
216 }
217 elseif ($buttons === 'false' || $buttons === 'no' || $buttons === '0')
218 {
219 $this->buttons = false;
220 }
221 else
222 {
223 $this->buttons = !empty($hide) ? explode(',', $buttons) : array();
224 }
225
226 $this->hide = !empty($hide) ? explode(',', (string) $this->element['hide']) : array();
227 $this->editorType = !empty($editorType) ? explode('|', trim($editorType)) : array();
228 }
229
230 return $result;
231 }
232
233 /**
234 * Method to get the field input markup for the editor area
235 *
236 * @return string The field input markup.
237 *
238 * @since 1.6
239 */
240 protected function getInput()
241 {
242 // Get an editor object.
243 $editor = $this->getEditor();
244 $readonly = $this->readonly || $this->disabled;
245
246 return $editor->display(
247 $this->name, htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8'), $this->width, $this->height, $this->columns, $this->rows,
248 $this->buttons ? (is_array($this->buttons) ? array_merge($this->buttons, $this->hide) : $this->hide) : false, $this->id, $this->asset,
249 $this->form->getValue($this->authorField), array('syntax' => (string) $this->element['syntax'], 'readonly' => $readonly)
250 );
251 }
252
253 /**
254 * Method to get a JEditor object based on the form field.
255 *
256 * @return JEditor The JEditor object.
257 *
258 * @since 1.6
259 */
260 protected function getEditor()
261 {
262 // Only create the editor if it is not already created.
263 if (empty($this->editor))
264 {
265 $editor = null;
266
267 if ($this->editorType)
268 {
269 // Get the list of editor types.
270 $types = $this->editorType;
271
272 // Get the database object.
273 $db = JFactory::getDbo();
274
275 // Iterate over teh types looking for an existing editor.
276 foreach ($types as $element)
277 {
278 // Build the query.
279 $query = $db->getQuery(true)
280 ->select('element')
281 ->from('#__extensions')
282 ->where('element = ' . $db->quote($element))
283 ->where('folder = ' . $db->quote('editors'))
284 ->where('enabled = 1');
285
286 // Check of the editor exists.
287 $db->setQuery($query, 0, 1);
288 $editor = $db->loadResult();
289
290 // If an editor was found stop looking.
291 if ($editor)
292 {
293 break;
294 }
295 }
296 }
297
298 // Create the JEditor instance based on the given editor.
299 if ($editor === null)
300 {
301 $editor = JFactory::getConfig()->get('editor');
302 }
303
304 $this->editor = JEditor::getInstance($editor);
305 }
306
307 return $this->editor;
308 }
309
310 /**
311 * Method to get the JEditor output for an onSave event.
312 *
313 * @return string The JEditor object output.
314 *
315 * @since 1.6
316 */
317 public function save()
318 {
319 return $this->getEditor()->save($this->id);
320 }
321 }
322