| Total Complexity | 61 |
| Total Lines | 391 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FieldInput 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 FieldInput, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class FieldInput extends Field implements ExportableField, ImportableField |
||
| 11 | { |
||
| 12 | public function __construct() |
||
| 13 | { |
||
| 14 | parent::__construct(); |
||
| 15 | $this->_name = __('Text Input'); |
||
|
|
|||
| 16 | $this->_required = true; |
||
| 17 | |||
| 18 | $this->set('required', 'no'); |
||
| 19 | } |
||
| 20 | |||
| 21 | /*------------------------------------------------------------------------- |
||
| 22 | Definition: |
||
| 23 | -------------------------------------------------------------------------*/ |
||
| 24 | |||
| 25 | public function canFilter() |
||
| 26 | { |
||
| 27 | return true; |
||
| 28 | } |
||
| 29 | |||
| 30 | public function canPrePopulate() |
||
| 31 | { |
||
| 32 | return true; |
||
| 33 | } |
||
| 34 | |||
| 35 | public function isSortable() |
||
| 36 | { |
||
| 37 | return true; |
||
| 38 | } |
||
| 39 | |||
| 40 | public function allowDatasourceOutputGrouping() |
||
| 43 | } |
||
| 44 | |||
| 45 | public function allowDatasourceParamOutput() |
||
| 46 | { |
||
| 47 | return true; |
||
| 48 | } |
||
| 49 | |||
| 50 | /*------------------------------------------------------------------------- |
||
| 51 | Setup: |
||
| 52 | -------------------------------------------------------------------------*/ |
||
| 53 | |||
| 54 | public function createTable() |
||
| 55 | { |
||
| 56 | return Symphony::Database()->query( |
||
| 57 | "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` ( |
||
| 58 | `id` int(11) unsigned NOT null auto_increment, |
||
| 59 | `entry_id` int(11) unsigned NOT null, |
||
| 60 | `handle` varchar(255) default null, |
||
| 61 | `value` varchar(255) default null, |
||
| 62 | PRIMARY KEY (`id`), |
||
| 63 | UNIQUE KEY `entry_id` (`entry_id`), |
||
| 64 | KEY `handle` (`handle`), |
||
| 65 | KEY `value` (`value`) |
||
| 66 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;" |
||
| 67 | ); |
||
| 68 | } |
||
| 69 | |||
| 70 | /*------------------------------------------------------------------------- |
||
| 71 | Utilities: |
||
| 72 | -------------------------------------------------------------------------*/ |
||
| 73 | |||
| 74 | private function __applyValidationRules($data) |
||
| 75 | { |
||
| 76 | $rule = $this->get('validator'); |
||
| 77 | |||
| 78 | return ($rule ? General::validateString($data, $rule) : true); |
||
| 79 | } |
||
| 80 | |||
| 81 | private function __replaceAmpersands($value) |
||
| 84 | } |
||
| 85 | |||
| 86 | /*------------------------------------------------------------------------- |
||
| 87 | Settings: |
||
| 88 | -------------------------------------------------------------------------*/ |
||
| 89 | |||
| 90 | public function setFromPOST(array $settings = array()) |
||
| 91 | { |
||
| 92 | parent::setFromPOST($settings); |
||
| 93 | |||
| 94 | if ($this->get('validator') == '') { |
||
| 95 | $this->remove('validator'); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | public function displaySettingsPanel(XMLElement &$wrapper, $errors = null) |
||
| 100 | { |
||
| 101 | parent::displaySettingsPanel($wrapper, $errors); |
||
| 102 | |||
| 103 | // Validation rule |
||
| 104 | $this->buildValidationSelect($wrapper, $this->get('validator'), 'fields['.$this->get('sortorder').'][validator]', 'input', $errors); |
||
| 105 | |||
| 106 | // Requirements and table display |
||
| 107 | $this->appendStatusFooter($wrapper); |
||
| 108 | } |
||
| 109 | |||
| 110 | public function commit() |
||
| 111 | { |
||
| 112 | if (!parent::commit()) { |
||
| 113 | return false; |
||
| 114 | } |
||
| 115 | |||
| 116 | $id = $this->get('id'); |
||
| 117 | |||
| 118 | if ($id === false) { |
||
| 119 | return false; |
||
| 120 | } |
||
| 121 | |||
| 122 | $fields = array('validator'=>null); |
||
| 123 | |||
| 124 | $fields['validator'] = ($fields['validator'] == 'custom' ? null : $this->get('validator')); |
||
| 125 | |||
| 126 | return FieldManager::saveSettings($id, $fields); |
||
| 127 | } |
||
| 128 | |||
| 129 | /*------------------------------------------------------------------------- |
||
| 130 | Publish: |
||
| 131 | -------------------------------------------------------------------------*/ |
||
| 132 | |||
| 133 | public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null) |
||
| 134 | { |
||
| 135 | $value = General::sanitize(isset($data['value']) ? $data['value'] : null); |
||
| 136 | $label = Widget::Label($this->get('label')); |
||
| 137 | |||
| 138 | if ($this->get('required') !== 'yes') { |
||
| 139 | $label->appendChild(new XMLElement('i', __('Optional'))); |
||
| 140 | } |
||
| 141 | |||
| 142 | $label->appendChild(Widget::Input('fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix, (strlen($value) != 0 ? $value : null))); |
||
| 143 | |||
| 144 | if ($flagWithError != null) { |
||
| 145 | $wrapper->appendChild(Widget::Error($label, $flagWithError)); |
||
| 146 | } else { |
||
| 147 | $wrapper->appendChild($label); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | public function checkPostFieldData($data, &$message, $entry_id = null) |
||
| 152 | { |
||
| 153 | $message = null; |
||
| 154 | |||
| 155 | if (is_array($data) && isset($data['value'])) { |
||
| 156 | $data = $data['value']; |
||
| 157 | } |
||
| 158 | |||
| 159 | if ($this->get('required') === 'yes' && strlen(trim($data)) == 0) { |
||
| 160 | $message = __('‘%s’ is a required field.', array($this->get('label'))); |
||
| 161 | return self::__MISSING_FIELDS__; |
||
| 162 | } |
||
| 163 | |||
| 164 | if (!$this->__applyValidationRules($data)) { |
||
| 165 | $message = __('‘%s’ contains invalid data. Please check the contents.', array($this->get('label'))); |
||
| 166 | return self::__INVALID_FIELDS__; |
||
| 167 | } |
||
| 168 | |||
| 169 | return self::__OK__; |
||
| 170 | } |
||
| 171 | |||
| 172 | public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null) |
||
| 173 | { |
||
| 174 | $status = self::__OK__; |
||
| 175 | |||
| 176 | if (strlen(trim($data)) == 0) { |
||
| 177 | return array(); |
||
| 178 | } |
||
| 179 | |||
| 180 | $result = array( |
||
| 181 | 'value' => $data |
||
| 182 | ); |
||
| 183 | |||
| 184 | $result['handle'] = Lang::createHandle($result['value']); |
||
| 185 | |||
| 186 | return $result; |
||
| 187 | } |
||
| 188 | |||
| 189 | /*------------------------------------------------------------------------- |
||
| 190 | Output: |
||
| 191 | -------------------------------------------------------------------------*/ |
||
| 192 | |||
| 193 | public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = null, $entry_id = null) |
||
| 194 | { |
||
| 195 | $value = $data['value']; |
||
| 196 | |||
| 197 | if ($encode === true) { |
||
| 198 | $value = General::sanitize($value); |
||
| 199 | } else { |
||
| 200 | if (!General::validateXML($data['value'], $errors, false, new XsltProcess)) { |
||
| 201 | $value = html_entity_decode($data['value'], ENT_QUOTES, 'UTF-8'); |
||
| 202 | $value = $this->__replaceAmpersands($value); |
||
| 203 | |||
| 204 | if (!General::validateXML($value, $errors, false, new XsltProcess)) { |
||
| 205 | $value = General::sanitize($data['value']); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | $wrapper->appendChild( |
||
| 211 | new XMLElement($this->get('element_name'), $value, array('handle' => $data['handle'])) |
||
| 212 | ); |
||
| 213 | } |
||
| 214 | |||
| 215 | /*------------------------------------------------------------------------- |
||
| 216 | Import: |
||
| 217 | -------------------------------------------------------------------------*/ |
||
| 218 | |||
| 219 | public function getImportModes() |
||
| 224 | ); |
||
| 225 | } |
||
| 226 | |||
| 227 | public function prepareImportValue($data, $mode, $entry_id = null) |
||
| 228 | { |
||
| 229 | $message = $status = null; |
||
| 230 | $modes = (object)$this->getImportModes(); |
||
| 231 | |||
| 232 | if ($mode === $modes->getValue) { |
||
| 233 | return $data; |
||
| 234 | } elseif ($mode === $modes->getPostdata) { |
||
| 235 | return $this->processRawFieldData($data, $status, $message, true, $entry_id); |
||
| 236 | } |
||
| 237 | |||
| 238 | return null; |
||
| 239 | } |
||
| 240 | |||
| 241 | /*------------------------------------------------------------------------- |
||
| 242 | Export: |
||
| 243 | -------------------------------------------------------------------------*/ |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Return a list of supported export modes for use with `prepareExportValue`. |
||
| 247 | * |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | public function getExportModes() |
||
| 251 | { |
||
| 252 | return array( |
||
| 253 | 'getHandle' => ExportableField::HANDLE, |
||
| 254 | 'getUnformatted' => ExportableField::UNFORMATTED, |
||
| 255 | 'getPostdata' => ExportableField::POSTDATA |
||
| 256 | ); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Give the field some data and ask it to return a value using one of many |
||
| 261 | * possible modes. |
||
| 262 | * |
||
| 263 | * @param mixed $data |
||
| 264 | * @param integer $mode |
||
| 265 | * @param integer $entry_id |
||
| 266 | * @return string|null |
||
| 267 | */ |
||
| 268 | public function prepareExportValue($data, $mode, $entry_id = null) |
||
| 269 | { |
||
| 270 | $modes = (object)$this->getExportModes(); |
||
| 271 | |||
| 272 | // Export handles: |
||
| 273 | if ($mode === $modes->getHandle) { |
||
| 274 | if (isset($data['handle'])) { |
||
| 275 | return $data['handle']; |
||
| 276 | } elseif (isset($data['value'])) { |
||
| 277 | return Lang::createHandle($data['value']); |
||
| 278 | } |
||
| 279 | |||
| 280 | // Export unformatted: |
||
| 281 | } elseif ($mode === $modes->getUnformatted || $mode === $modes->getPostdata) { |
||
| 282 | return isset($data['value']) |
||
| 283 | ? $data['value'] |
||
| 284 | : null; |
||
| 285 | } |
||
| 286 | |||
| 287 | return null; |
||
| 288 | } |
||
| 289 | |||
| 290 | /*------------------------------------------------------------------------- |
||
| 291 | Filtering: |
||
| 292 | -------------------------------------------------------------------------*/ |
||
| 293 | |||
| 294 | public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false) |
||
| 295 | { |
||
| 296 | $field_id = $this->get('id'); |
||
| 297 | |||
| 298 | if (self::isFilterRegex($data[0])) { |
||
| 299 | $this->buildRegexSQL($data[0], array('value', 'handle'), $joins, $where); |
||
| 300 | } elseif (self::isFilterSQL($data[0])) { |
||
| 301 | $this->buildFilterSQL($data[0], array('value', 'handle'), $joins, $where); |
||
| 302 | } elseif ($andOperation) { |
||
| 303 | foreach ($data as $value) { |
||
| 304 | $this->_key++; |
||
| 305 | $value = $this->cleanValue($value); |
||
| 306 | $joins .= " |
||
| 307 | LEFT JOIN |
||
| 308 | `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key} |
||
| 309 | ON (e.id = t{$field_id}_{$this->_key}.entry_id) |
||
| 310 | "; |
||
| 311 | $where .= " |
||
| 312 | AND ( |
||
| 313 | t{$field_id}_{$this->_key}.value = '{$value}' |
||
| 314 | OR t{$field_id}_{$this->_key}.handle = '{$value}' |
||
| 315 | ) |
||
| 316 | "; |
||
| 317 | } |
||
| 318 | } else { |
||
| 319 | if (!is_array($data)) { |
||
| 320 | $data = array($data); |
||
| 321 | } |
||
| 322 | |||
| 323 | foreach ($data as &$value) { |
||
| 324 | $value = $this->cleanValue($value); |
||
| 325 | } |
||
| 326 | |||
| 327 | $this->_key++; |
||
| 328 | $data = implode("', '", $data); |
||
| 329 | $joins .= " |
||
| 330 | LEFT JOIN |
||
| 331 | `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key} |
||
| 332 | ON (e.id = t{$field_id}_{$this->_key}.entry_id) |
||
| 333 | "; |
||
| 334 | $where .= " |
||
| 335 | AND ( |
||
| 336 | t{$field_id}_{$this->_key}.value IN ('{$data}') |
||
| 337 | OR t{$field_id}_{$this->_key}.handle IN ('{$data}') |
||
| 338 | ) |
||
| 339 | "; |
||
| 340 | } |
||
| 341 | |||
| 342 | return true; |
||
| 343 | } |
||
| 344 | |||
| 345 | /*------------------------------------------------------------------------- |
||
| 346 | Sorting: |
||
| 347 | -------------------------------------------------------------------------*/ |
||
| 348 | |||
| 349 | public function buildSortingSQL(&$joins, &$where, &$sort, $order = 'ASC') |
||
| 364 | ); |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | public function buildSortingSelectSQL($sort, $order = 'ASC') |
||
| 371 | } |
||
| 372 | |||
| 373 | /*------------------------------------------------------------------------- |
||
| 374 | Grouping: |
||
| 375 | -------------------------------------------------------------------------*/ |
||
| 376 | |||
| 377 | public function groupRecords($records) |
||
| 401 | } |
||
| 402 | } |
||
| 403 |