Total Complexity | 104 |
Total Lines | 666 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like FieldAuthor 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 FieldAuthor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class FieldAuthor extends Field implements ExportableField |
||
16 | { |
||
17 | public function __construct() |
||
18 | { |
||
19 | parent::__construct(); |
||
20 | $this->_name = __('Author'); |
||
|
|||
21 | $this->_required = true; |
||
22 | $this->entryQueryFieldAdapter = new EntryQueryAuthorAdapter($this); |
||
23 | |||
24 | $this->set('author_types', array()); |
||
25 | } |
||
26 | |||
27 | /*------------------------------------------------------------------------- |
||
28 | Definition: |
||
29 | -------------------------------------------------------------------------*/ |
||
30 | |||
31 | public function canToggle() |
||
32 | { |
||
33 | return ($this->get('allow_multiple_selection') === 'yes' ? false : true); |
||
34 | } |
||
35 | |||
36 | public function getToggleStates() |
||
37 | { |
||
38 | $authors = (new AuthorManager)->select()->execute()->rows(); |
||
39 | |||
40 | $states = array(); |
||
41 | foreach ($authors as $a) { |
||
42 | $states[$a->get('id')] = $a->getFullName(); |
||
43 | } |
||
44 | |||
45 | return $states; |
||
46 | } |
||
47 | |||
48 | public function toggleFieldData(array $data, $newState, $entry_id = null) |
||
49 | { |
||
50 | $data['author_id'] = $newState; |
||
51 | return $data; |
||
52 | } |
||
53 | |||
54 | public function canFilter() |
||
55 | { |
||
56 | return true; |
||
57 | } |
||
58 | |||
59 | public function isSortable() |
||
60 | { |
||
61 | return $this->canToggle(); |
||
62 | } |
||
63 | |||
64 | public function allowDatasourceOutputGrouping() |
||
65 | { |
||
66 | // Grouping follows the same rule as toggling. |
||
67 | return $this->canToggle(); |
||
68 | } |
||
69 | |||
70 | public function allowDatasourceParamOutput() |
||
71 | { |
||
72 | return true; |
||
73 | } |
||
74 | |||
75 | public function fetchSuggestionTypes() |
||
76 | { |
||
77 | return array('static'); |
||
78 | } |
||
79 | |||
80 | /*------------------------------------------------------------------------- |
||
81 | Setup: |
||
82 | -------------------------------------------------------------------------*/ |
||
83 | |||
84 | public function createTable() |
||
85 | { |
||
86 | return Symphony::Database() |
||
87 | ->create('tbl_entries_data_' . General::intval($this->get('id'))) |
||
88 | ->ifNotExists() |
||
89 | ->fields([ |
||
90 | 'id' => [ |
||
91 | 'type' => 'int(11)', |
||
92 | 'auto' => true, |
||
93 | ], |
||
94 | 'entry_id' => 'int(11)', |
||
95 | 'author_id' => [ |
||
96 | 'type' => 'int(11)', |
||
97 | 'null' => true, |
||
98 | ] |
||
99 | ]) |
||
100 | ->keys([ |
||
101 | 'id' => 'primary', |
||
102 | 'author' => [ |
||
103 | 'type' => 'unique', |
||
104 | 'cols' => ['entry_id', 'author_id'], |
||
105 | ], |
||
106 | 'author_id' => 'key', |
||
107 | ]) |
||
108 | ->execute() |
||
109 | ->success(); |
||
110 | } |
||
111 | |||
112 | /*------------------------------------------------------------------------- |
||
113 | Utilities: |
||
114 | -------------------------------------------------------------------------*/ |
||
115 | |||
116 | public function set($field, $value) |
||
117 | { |
||
118 | if ($field === 'author_types' && !is_array($value)) { |
||
119 | $value = explode(',', $value); |
||
120 | } |
||
121 | |||
122 | $this->_settings[$field] = $value; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Determines based on the input value whether we want to filter the Author |
||
127 | * field by ID or by the Author's Username |
||
128 | * |
||
129 | * @since Symphony 2.2 |
||
130 | * @deprecated @since Symphony 3.0.0 |
||
131 | * @param string $value |
||
132 | * @return string |
||
133 | * Either `author_id` or `username` |
||
134 | */ |
||
135 | private static function __parseFilter($value) |
||
136 | { |
||
137 | return is_numeric($value) ? 'author_id' : 'username'; |
||
138 | } |
||
139 | |||
140 | /*------------------------------------------------------------------------- |
||
141 | Settings: |
||
142 | -------------------------------------------------------------------------*/ |
||
143 | |||
144 | public function findDefaults(array &$settings) |
||
145 | { |
||
146 | if (!isset($settings['allow_multiple_selection'])) { |
||
147 | $settings['allow_multiple_selection'] = 'no'; |
||
148 | } |
||
149 | |||
150 | if (!isset($settings['author_types'])) { |
||
151 | $settings['author_types'] = array('developer', 'manager', 'author'); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | public function displaySettingsPanel(XMLElement &$wrapper, $errors = null) |
||
156 | { |
||
157 | parent::displaySettingsPanel($wrapper, $errors); |
||
158 | |||
159 | // Author types |
||
160 | $label = Widget::Label(__('Author types')); |
||
161 | $types = $this->get('author_types'); |
||
162 | $options = array( |
||
163 | array('author', empty($types) ? true : in_array('author', $types), __('Author')), |
||
164 | array('manager', empty($types) ? true : in_array('manager', $types), __('Manager')), |
||
165 | array('developer', empty($types) ? true : in_array('developer', $types), __('Developer')) |
||
166 | ); |
||
167 | $label->appendChild( |
||
168 | Widget::Select('fields['.$this->get('sortorder').'][author_types][]', $options, array( |
||
169 | 'multiple' => 'multiple' |
||
170 | )) |
||
171 | ); |
||
172 | |||
173 | if (isset($errors['author_types'])) { |
||
174 | $wrapper->appendChild(Widget::Error($label, $errors['author_types'])); |
||
175 | } else { |
||
176 | $wrapper->appendChild($label); |
||
177 | } |
||
178 | |||
179 | // Options |
||
180 | $div = new XMLElement('div', null, array('class' => 'two columns')); |
||
181 | |||
182 | // Allow multiple selection |
||
183 | $this->createCheckboxSetting($div, 'allow_multiple_selection', __('Allow selection of multiple authors')); |
||
184 | |||
185 | // Default to current logged in user |
||
186 | $this->createCheckboxSetting($div, 'default_to_current_user', __('Select current user by default')); |
||
187 | |||
188 | // Requirements and table display |
||
189 | $wrapper->appendChild($div); |
||
190 | $this->appendStatusFooter($wrapper); |
||
191 | } |
||
192 | |||
193 | public function checkFields(array &$errors, $checkForDuplicates = true) |
||
194 | { |
||
195 | parent::checkFields($errors, $checkForDuplicates); |
||
196 | |||
197 | $types = $this->get('author_types'); |
||
198 | |||
199 | if (empty($types)) { |
||
200 | $errors['author_types'] = __('This is a required field.'); |
||
201 | } |
||
202 | |||
203 | return (is_array($errors) && !empty($errors) ? self::__ERROR__ : self::__OK__); |
||
204 | } |
||
205 | |||
206 | public function commit() |
||
228 | } |
||
229 | |||
230 | /*------------------------------------------------------------------------- |
||
231 | Publish: |
||
232 | -------------------------------------------------------------------------*/ |
||
233 | |||
234 | public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null) |
||
235 | { |
||
236 | $value = isset($data['author_id']) ? $data['author_id'] : null; |
||
237 | |||
238 | if ($this->get('default_to_current_user') === 'yes' && empty($data) && empty($_POST)) { |
||
239 | $value = array(Symphony::Author()->get('id')); |
||
240 | } |
||
241 | |||
242 | if (!is_array($value)) { |
||
243 | $value = array($value); |
||
244 | } |
||
245 | |||
246 | $options = array(); |
||
247 | |||
248 | if ($this->get('required') !== 'yes') { |
||
249 | $options[] = array(null, false, null); |
||
250 | } |
||
251 | |||
252 | $authorQuery = (new AuthorManager) |
||
253 | ->select() |
||
254 | ->sort('id'); |
||
255 | |||
256 | // Custom where to only show Authors based off the Author Types setting |
||
257 | $types = $this->get('author_types'); |
||
258 | |||
259 | if (!empty($types)) { |
||
260 | $authorQuery->where(['user_type' => ['in' => $types]]); |
||
261 | } |
||
262 | |||
263 | $authors = $authorQuery->execute()->rows(); |
||
264 | $found = false; |
||
265 | |||
266 | foreach ($authors as $a) { |
||
267 | if (in_array($a->get('id'), $value)) { |
||
268 | $found = true; |
||
269 | } |
||
270 | |||
271 | $options[] = array($a->get('id'), in_array($a->get('id'), $value), $a->getFullName()); |
||
272 | } |
||
273 | |||
274 | // Ensure the selected Author is included in the options (incase |
||
275 | // the settings change after the original entry was created) |
||
276 | if (!$found && !is_null($value)) { |
||
277 | $authors = AuthorManager::fetchByID($value); |
||
278 | |||
279 | foreach ($authors as $a) { |
||
280 | $options[] = array($a->get('id'), in_array($a->get('id'), $value), $a->getFullName()); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | $fieldname = 'fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix; |
||
285 | |||
286 | if ($this->get('allow_multiple_selection') === 'yes') { |
||
287 | $fieldname .= '[]'; |
||
288 | } |
||
289 | |||
290 | $label = Widget::Label($this->get('label')); |
||
291 | |||
292 | if ($this->get('required') !== 'yes') { |
||
293 | $label->appendChild(new XMLElement('i', __('Optional'))); |
||
294 | } |
||
295 | |||
296 | $label->appendChild(Widget::Select($fieldname, $options, ($this->get('allow_multiple_selection') === 'yes' ? array('multiple' => 'multiple') : null))); |
||
297 | |||
298 | if ($flagWithError != null) { |
||
299 | $wrapper->appendChild(Widget::Error($label, $flagWithError)); |
||
300 | } else { |
||
301 | $wrapper->appendChild($label); |
||
302 | } |
||
303 | } |
||
304 | |||
305 | public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null) |
||
306 | { |
||
307 | $status = self::__OK__; |
||
308 | |||
309 | if (!is_array($data) && !empty($data)) { |
||
310 | return array('author_id' => $data); |
||
311 | } |
||
312 | |||
313 | if (empty($data)) { |
||
314 | return null; |
||
315 | } |
||
316 | |||
317 | $result = array(); |
||
318 | |||
319 | foreach ($data as $id) { |
||
320 | $result['author_id'][] = $id; |
||
321 | } |
||
322 | |||
323 | return $result; |
||
324 | } |
||
325 | |||
326 | /*------------------------------------------------------------------------- |
||
327 | Output: |
||
328 | -------------------------------------------------------------------------*/ |
||
329 | |||
330 | public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = null, $entry_id = null) |
||
331 | { |
||
332 | if (!is_array($data['author_id'])) { |
||
333 | $data['author_id'] = array($data['author_id']); |
||
334 | } |
||
335 | |||
336 | $list = new XMLElement($this->get('element_name')); |
||
337 | $authors = AuthorManager::fetchByID($data['author_id']); |
||
338 | |||
339 | foreach ($authors as $author) { |
||
340 | if (is_null($author)) { |
||
341 | continue; |
||
342 | } |
||
343 | |||
344 | $list->appendChild(new XMLElement( |
||
345 | 'item', |
||
346 | $author->getFullName(), |
||
347 | array( |
||
348 | 'id' => (string)$author->get('id'), |
||
349 | 'handle' => Lang::createHandle($author->getFullName()), |
||
350 | 'username' => General::sanitize($author->get('username')) |
||
351 | ) |
||
352 | )); |
||
353 | } |
||
354 | |||
355 | $wrapper->appendChild($list); |
||
356 | } |
||
357 | |||
358 | public function prepareTextValue($data, $entry_id = null) |
||
359 | { |
||
360 | $value = $this->prepareExportValue($data, ExportableField::LIST_OF + ExportableField::VALUE, $entry_id); |
||
361 | return General::sanitize(implode(', ', $value)); |
||
362 | } |
||
363 | |||
364 | public function getParameterPoolValue(array $data, $entry_id = null) |
||
365 | { |
||
366 | return $this->prepareExportValue($data, ExportableField::LIST_OF + ExportableField::AUTHOR, $entry_id); |
||
367 | } |
||
368 | |||
369 | /*------------------------------------------------------------------------- |
||
370 | Export: |
||
371 | -------------------------------------------------------------------------*/ |
||
372 | |||
373 | /** |
||
374 | * Return a list of supported export modes for use with `prepareExportValue`. |
||
375 | * |
||
376 | * @return array |
||
377 | */ |
||
378 | public function getExportModes() |
||
392 | ); |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Give the field some data and ask it to return a value using one of many |
||
397 | * possible modes. |
||
398 | * |
||
399 | * @param mixed $data |
||
400 | * @param integer $mode |
||
401 | * @param integer $entry_id |
||
402 | * @return array|null |
||
403 | */ |
||
404 | public function prepareExportValue($data, $mode, $entry_id = null) |
||
405 | { |
||
406 | $modes = (object)$this->getExportModes(); |
||
407 | |||
408 | // Make sure we have an array to work with: |
||
409 | if (isset($data['author_id']) && is_array($data['author_id']) === false) { |
||
410 | $data['author_id'] = array( |
||
411 | $data['author_id'] |
||
412 | ); |
||
413 | } |
||
414 | |||
415 | // Return the author IDs: |
||
416 | if ($mode === $modes->listAuthor || $mode === $modes->getPostdata) { |
||
417 | return isset($data['author_id']) |
||
418 | ? $data['author_id'] |
||
419 | : array(); |
||
420 | } |
||
421 | |||
422 | // All other modes require full data: |
||
423 | $authors = isset($data['author_id']) |
||
424 | ? AuthorManager::fetchByID($data['author_id']) |
||
425 | : array(); |
||
426 | $items = array(); |
||
427 | |||
428 | foreach ($authors as $author) { |
||
429 | if (is_null($author)) { |
||
430 | continue; |
||
431 | } |
||
432 | |||
433 | if ($mode === $modes->listAuthorObject) { |
||
434 | $items[] = $author; |
||
435 | } elseif ($mode === $modes->listValue) { |
||
436 | $items[] = $author->getFullName(); |
||
437 | } elseif ($mode === $modes->listAuthorToValue) { |
||
438 | $items[$data['author_id']] = $author->getFullName(); |
||
439 | } |
||
440 | } |
||
441 | |||
442 | return $items; |
||
443 | } |
||
444 | |||
445 | /*------------------------------------------------------------------------- |
||
446 | Filtering: |
||
447 | -------------------------------------------------------------------------*/ |
||
448 | |||
449 | /** |
||
450 | * @deprecated @since Symphony 3.0.0 |
||
451 | * @see Field::buildDSRetrievalSQL() |
||
452 | */ |
||
453 | public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false) |
||
454 | { |
||
455 | if (Symphony::Log()) { |
||
456 | Symphony::Log()->pushDeprecateWarningToLog( |
||
457 | get_called_class() . '::buildDSRetrievalSQL()', |
||
458 | 'EntryQueryFieldAdapter::filter()' |
||
459 | ); |
||
460 | } |
||
461 | $field_id = $this->get('id'); |
||
462 | |||
463 | if (self::isFilterRegex($data[0])) { |
||
464 | $this->_key++; |
||
465 | |||
466 | if (preg_match('/^regexp:/i', $data[0])) { |
||
467 | $pattern = preg_replace('/^regexp:\s*/i', null, $this->cleanValue($data[0])); |
||
468 | $regex = 'REGEXP'; |
||
469 | } else { |
||
470 | $pattern = preg_replace('/^not-?regexp:\s*/i', null, $this->cleanValue($data[0])); |
||
471 | $regex = 'NOT REGEXP'; |
||
472 | } |
||
473 | |||
474 | if (strlen($pattern) == 0) { |
||
475 | return; |
||
476 | } |
||
477 | |||
478 | $joins .= " |
||
479 | LEFT JOIN |
||
480 | `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key} |
||
481 | ON (e.id = t{$field_id}_{$this->_key}.entry_id) |
||
482 | JOIN |
||
483 | `tbl_authors` AS t{$field_id}_{$this->_key}_authors |
||
484 | ON (t{$field_id}_{$this->_key}.author_id = t{$field_id}_{$this->_key}_authors.id) |
||
485 | "; |
||
486 | $where .= " |
||
487 | AND ( |
||
488 | t{$field_id}_{$this->_key}.author_id {$regex} '{$pattern}' |
||
489 | OR t{$field_id}_{$this->_key}_authors.username {$regex} '{$pattern}' |
||
490 | OR CONCAT_WS(' ', |
||
491 | t{$field_id}_{$this->_key}_authors.first_name, |
||
492 | t{$field_id}_{$this->_key}_authors.last_name |
||
493 | ) {$regex} '{$pattern}' |
||
494 | ) |
||
495 | "; |
||
496 | } elseif (self::isFilterSQL($data[0])) { |
||
497 | $this->buildFilterSQL($data[0], array('username', 'first_name', 'last_name'), $joins, $where); |
||
498 | } elseif ($andOperation) { |
||
499 | foreach ($data as $value) { |
||
500 | $this->_key++; |
||
501 | $value = $this->cleanValue($value); |
||
502 | |||
503 | if (self::__parseFilter($value) == "author_id") { |
||
504 | $where .= " |
||
505 | AND t{$field_id}_{$this->_key}.author_id = '{$value}' |
||
506 | "; |
||
507 | $joins .= " |
||
508 | LEFT JOIN |
||
509 | `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key} |
||
510 | ON (e.id = t{$field_id}_{$this->_key}.entry_id) |
||
511 | "; |
||
512 | } else { |
||
513 | $joins .= " |
||
514 | LEFT JOIN |
||
515 | `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key} |
||
516 | ON (e.id = t{$field_id}_{$this->_key}.entry_id) |
||
517 | JOIN |
||
518 | `tbl_authors` AS t{$field_id}_{$this->_key}_authors |
||
519 | ON (t{$field_id}_{$this->_key}.author_id = t{$field_id}_{$this->_key}_authors.id) |
||
520 | "; |
||
521 | $where .= " |
||
522 | AND ( |
||
523 | t{$field_id}_{$this->_key}_authors.username = '{$value}' |
||
524 | OR CONCAT_WS(' ', |
||
525 | t{$field_id}_{$this->_key}_authors.first_name, |
||
526 | t{$field_id}_{$this->_key}_authors.last_name |
||
527 | ) = '{$value}' |
||
528 | ) |
||
529 | "; |
||
530 | } |
||
531 | } |
||
532 | } else { |
||
533 | if (!is_array($data)) { |
||
534 | $data = array($data); |
||
535 | } |
||
536 | |||
537 | foreach ($data as &$value) { |
||
538 | $value = $this->cleanValue($value); |
||
539 | } |
||
540 | |||
541 | $this->_key++; |
||
542 | $data = implode("', '", $data); |
||
543 | $joins .= " |
||
544 | LEFT JOIN |
||
545 | `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key} |
||
546 | ON (e.id = t{$field_id}_{$this->_key}.entry_id) |
||
547 | JOIN |
||
548 | `tbl_authors` AS t{$field_id}_{$this->_key}_authors |
||
549 | ON (t{$field_id}_{$this->_key}.author_id = t{$field_id}_{$this->_key}_authors.id) |
||
550 | "; |
||
551 | $where .= " |
||
552 | AND ( |
||
553 | t{$field_id}_{$this->_key}.author_id IN ('{$data}') |
||
554 | OR |
||
555 | t{$field_id}_{$this->_key}_authors.username IN ('{$data}') |
||
556 | OR CONCAT_WS(' ', |
||
557 | t{$field_id}_{$this->_key}_authors.first_name, |
||
558 | t{$field_id}_{$this->_key}_authors.last_name |
||
559 | ) IN ('{$data}') |
||
560 | ) |
||
561 | "; |
||
562 | } |
||
563 | |||
564 | return true; |
||
565 | } |
||
566 | |||
567 | /*------------------------------------------------------------------------- |
||
568 | Sorting: |
||
569 | -------------------------------------------------------------------------*/ |
||
570 | |||
571 | /** |
||
572 | * @deprecated @since Symphony 3.0.0 |
||
573 | * @see Field::buildSortingSQL() |
||
574 | */ |
||
575 | public function buildSortingSQL(&$joins, &$where, &$sort, $order = 'ASC') |
||
576 | { |
||
577 | if (Symphony::Log()) { |
||
578 | Symphony::Log()->pushDeprecateWarningToLog( |
||
579 | get_called_class() . '::buildSortingSQL()', |
||
580 | 'EntryQueryFieldAdapter::sort()' |
||
581 | ); |
||
582 | } |
||
583 | if ($this->isRandomOrder($order)) { |
||
584 | $sort = 'ORDER BY RAND()'; |
||
585 | } else { |
||
586 | $joins .= " |
||
587 | LEFT OUTER JOIN `tbl_entries_data_".$this->get('id')."` AS `ed` ON (`e`.`id` = `ed`.`entry_id`) |
||
588 | LEFT OUTER JOIN `tbl_authors` AS `a` ON (ed.author_id = a.id) |
||
589 | "; |
||
590 | $sort = sprintf('ORDER BY `a`.`first_name` %1$s, `a`.`last_name` %1$s', $order); |
||
591 | } |
||
592 | } |
||
593 | |||
594 | /** |
||
595 | * @deprecated @since Symphony 3.0.0 |
||
596 | * @see Field::buildSortingSelectSQL() |
||
597 | */ |
||
598 | public function buildSortingSelectSQL($sort, $order = 'ASC') |
||
610 | } |
||
611 | |||
612 | /*------------------------------------------------------------------------- |
||
613 | Events: |
||
614 | -------------------------------------------------------------------------*/ |
||
615 | |||
616 | public function getExampleFormMarkup() |
||
641 | } |
||
642 | |||
643 | /*------------------------------------------------------------------------- |
||
644 | Grouping: |
||
645 | -------------------------------------------------------------------------*/ |
||
646 | |||
647 | public function groupRecords($records) |
||
681 | } |
||
682 | } |
||
683 |