Total Complexity | 61 |
Total Lines | 391 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like fieldTextarea often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use fieldTextarea, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class fieldTextarea extends Field implements ExportableField, ImportableField |
||
|
|||
11 | { |
||
12 | public function __construct() |
||
13 | { |
||
14 | parent::__construct(); |
||
15 | $this->_name = __('Textarea'); |
||
16 | $this->_required = true; |
||
17 | |||
18 | // Set default |
||
19 | $this->set('show_column', 'no'); |
||
20 | $this->set('required', 'no'); |
||
21 | } |
||
22 | |||
23 | /*------------------------------------------------------------------------- |
||
24 | Definition: |
||
25 | -------------------------------------------------------------------------*/ |
||
26 | |||
27 | public function canFilter() |
||
30 | } |
||
31 | |||
32 | public function canPrePopulate() |
||
35 | } |
||
36 | |||
37 | /*------------------------------------------------------------------------- |
||
38 | Setup: |
||
39 | -------------------------------------------------------------------------*/ |
||
40 | |||
41 | public function createTable() |
||
42 | { |
||
43 | return Symphony::Database()->query( |
||
44 | "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` ( |
||
45 | `id` int(11) unsigned NOT null auto_increment, |
||
46 | `entry_id` int(11) unsigned NOT null, |
||
47 | `value` MEDIUMTEXT, |
||
48 | `value_formatted` MEDIUMTEXT, |
||
49 | PRIMARY KEY (`id`), |
||
50 | UNIQUE KEY `entry_id` (`entry_id`), |
||
51 | FULLTEXT KEY `value` (`value`) |
||
52 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;" |
||
53 | ); |
||
54 | } |
||
55 | |||
56 | /*------------------------------------------------------------------------- |
||
57 | Utilities: |
||
58 | -------------------------------------------------------------------------*/ |
||
59 | |||
60 | protected function __applyFormatting($data, $validate = false, &$errors = null) |
||
61 | { |
||
62 | $result = ''; |
||
63 | |||
64 | if ($this->get('formatter')) { |
||
65 | $formatter = TextformatterManager::create($this->get('formatter')); |
||
66 | $result = $formatter->run($data); |
||
67 | } |
||
68 | |||
69 | if ($validate === true) { |
||
70 | if (!General::validateXML($result, $errors, false, new XsltProcess)) { |
||
71 | $result = html_entity_decode($result, ENT_QUOTES, 'UTF-8'); |
||
72 | $result = $this->__replaceAmpersands($result); |
||
73 | |||
74 | if (!General::validateXML($result, $errors, false, new XsltProcess)) { |
||
75 | return false; |
||
76 | } |
||
77 | } |
||
78 | } |
||
79 | |||
80 | return $result; |
||
81 | } |
||
82 | |||
83 | private function __replaceAmpersands($value) |
||
84 | { |
||
85 | return preg_replace('/&(?!(#[0-9]+|#x[0-9a-f]+|amp|lt|gt);)/i', '&', trim($value)); |
||
86 | } |
||
87 | |||
88 | /*------------------------------------------------------------------------- |
||
89 | Settings: |
||
90 | -------------------------------------------------------------------------*/ |
||
91 | |||
92 | public function findDefaults(array &$settings) |
||
96 | } |
||
97 | } |
||
98 | |||
99 | public function displaySettingsPanel(XMLElement &$wrapper, $errors = null) |
||
116 | } |
||
117 | |||
118 | public function commit() |
||
119 | { |
||
120 | if (!parent::commit()) { |
||
121 | return false; |
||
122 | } |
||
123 | |||
124 | $id = $this->get('id'); |
||
125 | |||
126 | if ($id === false) { |
||
127 | return false; |
||
128 | } |
||
129 | |||
130 | $fields = array(); |
||
131 | |||
132 | if ($this->get('formatter') != 'none') { |
||
133 | $fields['formatter'] = $this->get('formatter'); |
||
134 | } |
||
135 | |||
136 | $fields['size'] = $this->get('size'); |
||
137 | |||
138 | return FieldManager::saveSettings($id, $fields); |
||
139 | } |
||
140 | |||
141 | /*------------------------------------------------------------------------- |
||
142 | Publish: |
||
143 | -------------------------------------------------------------------------*/ |
||
144 | |||
145 | public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null) |
||
146 | { |
||
147 | $label = Widget::Label($this->get('label')); |
||
148 | |||
149 | if ($this->get('required') !== 'yes') { |
||
150 | $label->appendChild(new XMLElement('i', __('Optional'))); |
||
151 | } |
||
152 | |||
153 | $value = isset($data['value']) ? $data['value'] : null; |
||
154 | $textarea = Widget::Textarea('fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix, (int)$this->get('size'), 50, (strlen($value) != 0 ? General::sanitizeDouble($value) : null)); |
||
155 | |||
156 | if ($this->get('formatter') != 'none') { |
||
157 | $textarea->setAttribute('class', $this->get('formatter')); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Allows developers modify the textarea before it is rendered in the publish forms |
||
162 | * |
||
163 | * @delegate ModifyTextareaFieldPublishWidget |
||
164 | * @param string $context |
||
165 | * '/backend/' |
||
166 | * @param Field $field |
||
167 | * @param Widget $label |
||
168 | * @param Widget $textarea |
||
169 | */ |
||
170 | Symphony::ExtensionManager()->notifyMembers('ModifyTextareaFieldPublishWidget', '/backend/', array( |
||
171 | 'field' => &$this, |
||
172 | 'label' => &$label, |
||
173 | 'textarea' => &$textarea |
||
174 | )); |
||
175 | |||
176 | $label->appendChild($textarea); |
||
177 | |||
178 | if ($flagWithError != null) { |
||
179 | $wrapper->appendChild(Widget::Error($label, $flagWithError)); |
||
180 | } else { |
||
181 | $wrapper->appendChild($label); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | public function checkPostFieldData($data, &$message, $entry_id = null) |
||
186 | { |
||
187 | $message = null; |
||
188 | |||
189 | if ($this->get('required') === 'yes' && strlen(trim($data)) == 0) { |
||
190 | $message = __('‘%s’ is a required field.', array($this->get('label'))); |
||
191 | return self::__MISSING_FIELDS__; |
||
192 | } |
||
193 | |||
194 | if ($this->__applyFormatting($data, true, $errors) === false) { |
||
195 | $message = __('‘%s’ contains invalid XML.', array($this->get('label'))) . ' ' . __('The following error was returned:') . ' <code>' . $errors[0]['message'] . '</code>'; |
||
196 | return self::__INVALID_FIELDS__; |
||
197 | } |
||
198 | |||
199 | return self::__OK__; |
||
200 | } |
||
201 | |||
202 | public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null) |
||
203 | { |
||
204 | $status = self::__OK__; |
||
205 | |||
206 | if (strlen(trim($data)) == 0) { |
||
207 | return array(); |
||
208 | } |
||
209 | |||
210 | $result = array( |
||
211 | 'value' => $data |
||
212 | ); |
||
213 | |||
214 | $result['value_formatted'] = $this->__applyFormatting($data, true, $errors); |
||
215 | |||
216 | if ($result['value_formatted'] === false) { |
||
217 | // Run the formatter again, but this time do not validate. We will sanitize the output |
||
218 | $result['value_formatted'] = General::sanitize($this->__applyFormatting($data)); |
||
219 | } |
||
220 | |||
221 | return $result; |
||
222 | } |
||
223 | |||
224 | /*------------------------------------------------------------------------- |
||
225 | Output: |
||
226 | -------------------------------------------------------------------------*/ |
||
227 | |||
228 | public function fetchIncludableElements() |
||
229 | { |
||
230 | if ($this->get('formatter')) { |
||
231 | return array( |
||
232 | $this->get('element_name') . ': formatted', |
||
233 | $this->get('element_name') . ': unformatted' |
||
234 | ); |
||
235 | } |
||
236 | |||
237 | return array( |
||
238 | $this->get('element_name') |
||
239 | ); |
||
240 | } |
||
241 | |||
242 | public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = null, $entry_id = null) |
||
243 | { |
||
244 | $attributes = array(); |
||
245 | |||
246 | if (!is_null($mode)) { |
||
247 | $attributes['mode'] = $mode; |
||
248 | } |
||
249 | |||
250 | if ($mode == 'formatted') { |
||
251 | if ($this->get('formatter') && isset($data['value_formatted'])) { |
||
252 | $value = $data['value_formatted']; |
||
253 | } else { |
||
254 | $value = $this->__replaceAmpersands($data['value']); |
||
255 | } |
||
256 | |||
257 | $wrapper->appendChild( |
||
258 | new XMLElement( |
||
259 | $this->get('element_name'), |
||
260 | ($encode ? General::sanitize($value) : $value), |
||
261 | $attributes |
||
262 | ) |
||
263 | ); |
||
264 | } elseif ($mode == null || $mode == 'unformatted') { |
||
265 | $value = !empty($data['value']) |
||
266 | ? sprintf('<![CDATA[%s]]>', str_replace(']]>', ']]]]><![CDATA[>', $data['value'])) |
||
267 | : $data['value']; |
||
268 | |||
269 | $wrapper->appendChild( |
||
270 | new XMLElement($this->get('element_name'), $value, $attributes) |
||
271 | ); |
||
272 | } |
||
273 | } |
||
274 | |||
275 | /*------------------------------------------------------------------------- |
||
276 | Import: |
||
277 | -------------------------------------------------------------------------*/ |
||
278 | |||
279 | public function getImportModes() |
||
280 | { |
||
281 | return array( |
||
282 | 'getValue' => ImportableField::STRING_VALUE, |
||
283 | 'getPostdata' => ImportableField::ARRAY_VALUE |
||
284 | ); |
||
285 | } |
||
286 | |||
287 | public function prepareImportValue($data, $mode, $entry_id = null) |
||
288 | { |
||
289 | $message = $status = null; |
||
290 | $modes = (object)$this->getImportModes(); |
||
291 | |||
292 | if ($mode === $modes->getValue) { |
||
293 | return $data; |
||
294 | } elseif ($mode === $modes->getPostdata) { |
||
295 | return $this->processRawFieldData($data, $status, $message, true, $entry_id); |
||
296 | } |
||
297 | |||
298 | return null; |
||
299 | } |
||
300 | |||
301 | /*------------------------------------------------------------------------- |
||
302 | Export: |
||
303 | -------------------------------------------------------------------------*/ |
||
304 | |||
305 | /** |
||
306 | * Return a list of supported export modes for use with `prepareExportValue`. |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | public function getExportModes() |
||
317 | ); |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Give the field some data and ask it to return a value using one of many |
||
322 | * possible modes. |
||
323 | * |
||
324 | * @param mixed $data |
||
325 | * @param integer $mode |
||
326 | * @param integer $entry_id |
||
327 | * @return string|null |
||
328 | */ |
||
329 | public function prepareExportValue($data, $mode, $entry_id = null) |
||
330 | { |
||
331 | $modes = (object)$this->getExportModes(); |
||
332 | |||
333 | // Export handles: |
||
334 | if ($mode === $modes->getHandle) { |
||
335 | if (isset($data['handle'])) { |
||
336 | return $data['handle']; |
||
337 | } elseif (isset($data['value'])) { |
||
338 | return Lang::createHandle($data['value']); |
||
339 | } |
||
340 | |||
341 | // Export unformatted: |
||
342 | } elseif ($mode === $modes->getUnformatted || $mode === $modes->getPostdata) { |
||
343 | return isset($data['value']) |
||
344 | ? $data['value'] |
||
345 | : null; |
||
346 | |||
347 | // Export formatted: |
||
348 | } elseif ($mode === $modes->getFormatted) { |
||
349 | if (isset($data['value_formatted'])) { |
||
350 | return $data['value_formatted']; |
||
351 | } elseif (isset($data['value'])) { |
||
352 | return General::sanitize($data['value']); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | return null; |
||
357 | } |
||
358 | |||
359 | /*------------------------------------------------------------------------- |
||
360 | Filtering: |
||
361 | -------------------------------------------------------------------------*/ |
||
362 | |||
363 | public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false) |
||
389 | } |
||
390 | |||
391 | /*------------------------------------------------------------------------- |
||
392 | Events: |
||
393 | -------------------------------------------------------------------------*/ |
||
394 | |||
395 | public function getExampleFormMarkup() |
||
396 | { |
||
397 | $label = Widget::Label($this->get('label')); |
||
398 | $label->appendChild(Widget::Textarea('fields['.$this->get('element_name').']', (int)$this->get('size'), 50)); |
||
399 | |||
403 |
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider
.