| Total Complexity | 147 |
| Total Lines | 663 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SectionDatasource 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 SectionDatasource, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class SectionDatasource extends Datasource |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * An array of Field objects that this Datasource has created to display |
||
| 21 | * the results. |
||
| 22 | */ |
||
| 23 | private static $_fieldPool = array(); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * An array of the Symphony meta data parameters. |
||
| 27 | */ |
||
| 28 | private static $_system_parameters = array( |
||
| 29 | 'system:id', |
||
| 30 | 'system:author', |
||
| 31 | 'system:creation-date', |
||
| 32 | 'system:modification-date', |
||
| 33 | 'system:date' // deprecated |
||
| 34 | ); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Set's the Section ID that this Datasource will use as it's source |
||
| 38 | * |
||
| 39 | * @param integer $source |
||
| 40 | */ |
||
| 41 | public function setSource($source) |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Return's the Section ID that this datasource is using as it's source |
||
| 48 | * |
||
| 49 | * @return integer |
||
| 50 | */ |
||
| 51 | public function getSource() |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * If this Datasource requires System Parameters to be output, this function |
||
| 58 | * will return true, otherwise false. |
||
| 59 | * |
||
| 60 | * @return boolean |
||
| 61 | */ |
||
| 62 | public function canProcessSystemParameters() |
||
| 63 | { |
||
| 64 | if (!is_array($this->dsParamPARAMOUTPUT)) { |
||
| 65 | return false; |
||
| 66 | } |
||
| 67 | |||
| 68 | foreach (self::$_system_parameters as $system_parameter) { |
||
| 69 | if (in_array($system_parameter, $this->dsParamPARAMOUTPUT) === true) { |
||
| 70 | return true; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | return false; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Given a name for the group, and an associative array that |
||
| 79 | * contains three keys, `attr`, `records` and `groups`. Grouping |
||
| 80 | * of Entries is done by the grouping Field at a PHP level, not |
||
| 81 | * through the Database. |
||
| 82 | * |
||
| 83 | * @param string $element |
||
| 84 | * The name for the XML node for this group |
||
| 85 | * @param array $group |
||
| 86 | * An associative array of the group data, includes `attr`, `records` |
||
| 87 | * and `groups` keys. |
||
| 88 | * @throws Exception |
||
| 89 | * @return XMLElement |
||
| 90 | */ |
||
| 91 | public function processRecordGroup($element, array $group) |
||
| 92 | { |
||
| 93 | $xGroup = new XMLElement($element, null, $group['attr']); |
||
| 94 | |||
| 95 | if (is_array($group['records']) && !empty($group['records'])) { |
||
| 96 | if (isset($group['records'][0])) { |
||
| 97 | $data = $group['records'][0]->getData(); |
||
| 98 | $pool = FieldManager::fetch(array_keys($data)); |
||
| 99 | self::$_fieldPool += $pool; |
||
| 100 | } |
||
| 101 | |||
| 102 | foreach ($group['records'] as $entry) { |
||
| 103 | $xEntry = $this->processEntry($entry); |
||
| 104 | |||
| 105 | if ($xEntry instanceof XMLElement) { |
||
| 106 | $xGroup->appendChild($xEntry); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | if (is_array($group['groups']) && !empty($group['groups'])) { |
||
| 112 | foreach ($group['groups'] as $element => $group) { |
||
| 113 | foreach ($group as $g) { |
||
| 114 | $xGroup->appendChild( |
||
| 115 | $this->processRecordGroup($element, $g) |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | if (!$this->_param_output_only) { |
||
| 122 | return $xGroup; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Given an Entry object, this function will generate an XML representation |
||
| 128 | * of the Entry to be returned. It will also add any parameters selected |
||
| 129 | * by this datasource to the parameter pool. |
||
| 130 | * |
||
| 131 | * @param Entry $entry |
||
| 132 | * @throws Exception |
||
| 133 | * @return XMLElement|boolean |
||
| 134 | * Returns boolean when only parameters are to be returned. |
||
| 135 | */ |
||
| 136 | public function processEntry(Entry $entry) |
||
| 137 | { |
||
| 138 | $data = $entry->getData(); |
||
| 139 | |||
| 140 | $xEntry = new XMLElement('entry'); |
||
| 141 | $xEntry->setAttribute('id', $entry->get('id')); |
||
| 142 | |||
| 143 | if (!empty($this->_associated_sections)) { |
||
| 144 | $this->setAssociatedEntryCounts($xEntry, $entry); |
||
| 145 | } |
||
| 146 | |||
| 147 | if ($this->_can_process_system_parameters) { |
||
| 148 | $this->processSystemParameters($entry); |
||
| 149 | } |
||
| 150 | |||
| 151 | foreach ($data as $field_id => $values) { |
||
| 152 | if (!isset(self::$_fieldPool[$field_id]) || !is_object(self::$_fieldPool[$field_id])) { |
||
| 153 | self::$_fieldPool[$field_id] = FieldManager::fetch($field_id); |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->processOutputParameters($entry, $field_id, $values); |
||
| 157 | |||
| 158 | if (!$this->_param_output_only) { |
||
| 159 | foreach ($this->dsParamINCLUDEDELEMENTS as $handle) { |
||
| 160 | list($handle, $mode) = preg_split('/\s*:\s*/', $handle, 2); |
||
| 161 | |||
| 162 | if (self::$_fieldPool[$field_id]->get('element_name') == $handle) { |
||
| 163 | self::$_fieldPool[$field_id]->appendFormattedElement($xEntry, $values, ($this->dsParamHTMLENCODE === 'yes' ? true : false), $mode, $entry->get('id')); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | if ($this->_param_output_only) { |
||
| 170 | return true; |
||
| 171 | } |
||
| 172 | |||
| 173 | // This is deprecated and will be removed in Symphony 3.0.0 |
||
| 174 | if (in_array('system:date', $this->dsParamINCLUDEDELEMENTS)) { |
||
| 175 | if (Symphony::Log()) { |
||
| 176 | Symphony::Log()->pushDeprecateWarningToLog('system:date', 'system:creation-date` or `system:modification-date', array( |
||
| 177 | 'message-format' => __('The `%s` data source field is deprecated.') |
||
| 178 | )); |
||
| 179 | } |
||
| 180 | $xDate = new XMLElement('system-date'); |
||
| 181 | $xDate->appendChild( |
||
| 182 | General::createXMLDateObject( |
||
| 183 | DateTimeObj::get('U', $entry->get('creation_date')), |
||
| 184 | 'created' |
||
| 185 | ) |
||
| 186 | ); |
||
| 187 | $xDate->appendChild( |
||
| 188 | General::createXMLDateObject( |
||
| 189 | DateTimeObj::get('U', $entry->get('modification_date')), |
||
| 190 | 'modified' |
||
| 191 | ) |
||
| 192 | ); |
||
| 193 | $xEntry->appendChild($xDate); |
||
| 194 | } |
||
| 195 | |||
| 196 | return $xEntry; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * An entry may be associated to other entries from various fields through |
||
| 201 | * the section associations. This function will set the number of related |
||
| 202 | * entries as attributes to the main `<entry>` element grouped by the |
||
| 203 | * related entry's section. |
||
| 204 | * |
||
| 205 | * @param XMLElement $xEntry |
||
| 206 | * The <entry> XMLElement that the associated section counts will |
||
| 207 | * be set on |
||
| 208 | * @param Entry $entry |
||
| 209 | * The current entry object |
||
| 210 | * @throws Exception |
||
| 211 | */ |
||
| 212 | public function setAssociatedEntryCounts(XMLElement &$xEntry, Entry $entry) |
||
| 213 | { |
||
| 214 | $associated_entry_counts = $entry->fetchAllAssociatedEntryCounts($this->_associated_sections); |
||
| 215 | |||
| 216 | if (!empty($associated_entry_counts)) { |
||
| 217 | foreach ($associated_entry_counts as $section_id => $fields) { |
||
| 218 | foreach ($this->_associated_sections as $section) { |
||
| 219 | if ($section['id'] != $section_id) { |
||
| 220 | continue; |
||
| 221 | } |
||
| 222 | |||
| 223 | // For each related field show the count (#2083) |
||
| 224 | foreach ($fields as $field_id => $count) { |
||
| 225 | $field_handle = FieldManager::fetchHandleFromID($field_id); |
||
| 226 | $section_handle = $section['handle']; |
||
| 227 | // Make sure attribute does not begin with a digit |
||
| 228 | if (preg_match('/^[0-9]/', $section_handle)) { |
||
| 229 | $section_handle = 'x-' . $section_handle; |
||
| 230 | } |
||
| 231 | if ($field_handle) { |
||
| 232 | $xEntry->setAttribute($section_handle . '-' . $field_handle, (string)$count); |
||
| 233 | } |
||
| 234 | |||
| 235 | // Backwards compatibility (without field handle) |
||
| 236 | $xEntry->setAttribute($section_handle, (string)$count); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Given an Entry object, this function will iterate over the `dsParamPARAMOUTPUT` |
||
| 245 | * setting to see any of the Symphony system parameters need to be set. |
||
| 246 | * The current system parameters supported are `system:id`, `system:author`, |
||
| 247 | * `system:creation-date` and `system:modification-date`. |
||
| 248 | * If these parameters are found, the result is added |
||
| 249 | * to the `$param_pool` array using the key, `ds-datasource-handle.parameter-name` |
||
| 250 | * For the moment, this function also supports the pre Symphony 2.3 syntax, |
||
| 251 | * `ds-datasource-handle` which did not support multiple parameters. |
||
| 252 | * |
||
| 253 | * @param Entry $entry |
||
| 254 | * The Entry object that contains the values that may need to be added |
||
| 255 | * into the parameter pool. |
||
| 256 | */ |
||
| 257 | public function processSystemParameters(Entry $entry) |
||
| 258 | { |
||
| 259 | if (!isset($this->dsParamPARAMOUTPUT)) { |
||
| 260 | return; |
||
| 261 | } |
||
| 262 | |||
| 263 | // Support the legacy parameter `ds-datasource-handle` |
||
| 264 | $key = 'ds-' . $this->dsParamROOTELEMENT; |
||
| 265 | $singleParam = count($this->dsParamPARAMOUTPUT) == 1; |
||
| 266 | |||
| 267 | foreach ($this->dsParamPARAMOUTPUT as $param) { |
||
| 268 | // The new style of paramater is `ds-datasource-handle.field-handle` |
||
| 269 | $param_key = $key . '.' . str_replace(':', '-', $param); |
||
| 270 | |||
| 271 | if ($param === 'system:id') { |
||
| 272 | $this->_param_pool[$param_key][] = $entry->get('id'); |
||
| 273 | |||
| 274 | if ($singleParam) { |
||
| 275 | $this->_param_pool[$key][] = $entry->get('id'); |
||
| 276 | } |
||
| 277 | } elseif ($param === 'system:author') { |
||
| 278 | $this->_param_pool[$param_key][] = $entry->get('author_id'); |
||
| 279 | |||
| 280 | if ($singleParam) { |
||
| 281 | $this->_param_pool[$key][] = $entry->get('author_id'); |
||
| 282 | } |
||
| 283 | } elseif ($param === 'system:creation-date' || $param === 'system:date') { |
||
| 284 | if ($param === 'system:date' && Symphony::Log()) { |
||
| 285 | Symphony::Log()->pushDeprecateWarningToLog('system:date', 'system:creation-date', array( |
||
| 286 | 'message-format' => __('The `%s` data source output parameter is deprecated.') |
||
| 287 | )); |
||
| 288 | } |
||
| 289 | $this->_param_pool[$param_key][] = $entry->get('creation_date'); |
||
| 290 | |||
| 291 | if ($singleParam) { |
||
| 292 | $this->_param_pool[$key][] = $entry->get('creation_date'); |
||
| 293 | } |
||
| 294 | } elseif ($param === 'system:modification-date') { |
||
| 295 | $this->_param_pool[$param_key][] = $entry->get('modification_date'); |
||
| 296 | |||
| 297 | if ($singleParam) { |
||
| 298 | $this->_param_pool[$key][] = $entry->get('modification_date'); |
||
| 299 | } |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Given an Entry object, a `$field_id` and an array of `$data`, this |
||
| 306 | * function iterates over the `dsParamPARAMOUTPUT` and will call the |
||
| 307 | * field's (identified by `$field_id`) `getParameterPoolValue` function |
||
| 308 | * to add parameters to the `$this->_param_pool`. |
||
| 309 | * |
||
| 310 | * @param Entry $entry |
||
| 311 | * @param integer $field_id |
||
| 312 | * @param array $data |
||
| 313 | */ |
||
| 314 | public function processOutputParameters(Entry $entry, $field_id, array $data) |
||
| 353 | } |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * This function iterates over `dsParamFILTERS` and builds the relevant |
||
| 360 | * `$where` and `$joins` parameters with SQL. This SQL is generated from |
||
| 361 | * `Field->buildDSRetrievalSQL`. A third parameter, `$group` is populated |
||
| 362 | * with boolean from `Field->requiresSQLGrouping()` |
||
| 363 | * |
||
| 364 | * @param string $where |
||
| 365 | * @param string $joins |
||
| 366 | * @param boolean $group |
||
| 367 | * @throws Exception |
||
| 368 | */ |
||
| 369 | public function processFilters(&$where, &$joins, &$group) |
||
| 370 | { |
||
| 371 | if (!is_array($this->dsParamFILTERS) || empty($this->dsParamFILTERS)) { |
||
| 372 | return; |
||
| 373 | } |
||
| 374 | |||
| 375 | $pool = FieldManager::fetch(array_filter(array_keys($this->dsParamFILTERS), 'is_int')); |
||
| 376 | self::$_fieldPool += $pool; |
||
| 377 | |||
| 378 | if (!is_string($where)) { |
||
| 379 | $where = ''; |
||
| 380 | } |
||
| 381 | |||
| 382 | foreach ($this->dsParamFILTERS as $field_id => $filter) { |
||
| 383 | if ((is_array($filter) && empty($filter)) || trim($filter) == '') { |
||
| 384 | continue; |
||
| 385 | } |
||
| 386 | |||
| 387 | if (!is_array($filter)) { |
||
| 388 | $filter_type = Datasource::determineFilterType($filter); |
||
| 389 | $value = Datasource::splitFilter($filter_type, $filter); |
||
| 390 | } else { |
||
| 391 | $filter_type = Datasource::FILTER_OR; |
||
| 392 | $value = $filter; |
||
| 393 | } |
||
| 394 | |||
| 395 | if (!in_array($field_id, self::$_system_parameters) && $field_id != 'id' && !(self::$_fieldPool[$field_id] instanceof Field)) { |
||
| 396 | throw new Exception( |
||
| 397 | __( |
||
| 398 | 'Error creating field object with id %1$d, for filtering in data source %2$s. Check this field exists.', |
||
| 399 | array($field_id, '<code>' . $this->dsParamROOTELEMENT . '</code>') |
||
| 400 | ) |
||
| 401 | ); |
||
| 402 | } |
||
| 403 | |||
| 404 | // Support system:id as well as the old 'id'. #1691 |
||
| 405 | if ($field_id === 'system:id' || $field_id === 'id') { |
||
| 406 | if ($filter_type == Datasource::FILTER_AND) { |
||
| 407 | $value = array_map(function ($val) { |
||
| 408 | return explode(',', $val); |
||
| 409 | }, $value); |
||
| 410 | } else { |
||
| 411 | $value = array($value); |
||
| 412 | } |
||
| 413 | |||
| 414 | foreach ($value as $v) { |
||
| 415 | $c = 'IN'; |
||
| 416 | if (stripos($v[0], 'not:') === 0) { |
||
| 417 | $v[0] = preg_replace('/^not:\s*/', null, $v[0]); |
||
| 418 | $c = 'NOT IN'; |
||
| 419 | } |
||
| 420 | |||
| 421 | // Cast all ID's to integers. (RE: #2191) |
||
| 422 | $v = array_map(function ($val) { |
||
| 423 | $val = General::intval($val); |
||
| 424 | |||
| 425 | // General::intval can return -1, so reset that to 0 |
||
| 426 | // so there are no side effects for the following |
||
| 427 | // array_sum and array_filter calls. RE: #2475 |
||
| 428 | if ($val === -1) { |
||
| 429 | $val = 0; |
||
| 430 | } |
||
| 431 | |||
| 432 | return $val; |
||
| 433 | }, $v); |
||
| 434 | $count = array_sum($v); |
||
| 435 | $v = array_filter($v); |
||
| 436 | |||
| 437 | // If the ID was cast to 0, then we need to filter on 'id' = 0, |
||
| 438 | // which will of course return no results, but without it the |
||
| 439 | // Datasource will return ALL results, which is not the |
||
| 440 | // desired behaviour. RE: #1619 |
||
| 441 | if ($count === 0) { |
||
| 442 | $v[] = 0; |
||
| 443 | } |
||
| 444 | |||
| 445 | // If there are no ID's, no need to filter. RE: #1567 |
||
| 446 | if (!empty($v)) { |
||
| 447 | $where .= " AND `e`.id " . $c . " (".implode(", ", $v).") "; |
||
| 448 | } |
||
| 449 | } |
||
| 450 | } elseif ($field_id === 'system:creation-date' || $field_id === 'system:modification-date' || $field_id === 'system:date') { |
||
| 451 | if ($field_id === 'system:date' && Symphony::Log()) { |
||
| 452 | Symphony::Log()->pushDeprecateWarningToLog('system:date', 'system:creation-date` or `system:modification-date', array( |
||
| 453 | 'message-format' => __('The `%s` data source filter is deprecated.') |
||
| 454 | )); |
||
| 455 | } |
||
| 456 | $date_joins = ''; |
||
| 457 | $date_where = ''; |
||
| 458 | $date = new FieldDate(); |
||
| 459 | $date->buildDSRetrievalSQL($value, $date_joins, $date_where, ($filter_type == Datasource::FILTER_AND ? true : false)); |
||
| 460 | |||
| 461 | // Replace the date field where with the `creation_date` or `modification_date`. |
||
| 462 | $date_where = preg_replace('/`t\d+`.date/', ($field_id !== 'system:modification-date') ? '`e`.creation_date_gmt' : '`e`.modification_date_gmt', $date_where); |
||
| 463 | $where .= $date_where; |
||
| 464 | } else { |
||
| 465 | if (!self::$_fieldPool[$field_id]->buildDSRetrievalSQL($value, $joins, $where, ($filter_type == Datasource::FILTER_AND ? true : false))) { |
||
| 466 | $this->_force_empty_result = true; |
||
| 467 | return; |
||
| 468 | } |
||
| 469 | |||
| 470 | if (!$group) { |
||
| 471 | $group = self::$_fieldPool[$field_id]->requiresSQLGrouping(); |
||
| 472 | } |
||
| 473 | } |
||
| 474 | } |
||
| 475 | } |
||
| 476 | |||
| 477 | public function execute(array &$param_pool = null) |
||
| 680 | } |
||
| 681 | } |
||
| 682 |