Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like contentPublish 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 contentPublish, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class contentPublish extends AdministrationPage |
||
| 14 | { |
||
| 15 | public $_errors = array(); |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The Pages page has /action/id/flag/ context. |
||
| 19 | * eg. /edit/1/saved/ |
||
| 20 | * |
||
| 21 | * @param array $context |
||
| 22 | * @param array $parts |
||
| 23 | * @return array |
||
| 24 | */ |
||
| 25 | public function parseContext(array &$context, array $parts) |
||
| 41 | |||
| 42 | public function sort(&$sort, &$order, $params) |
||
| 95 | |||
| 96 | View Code Duplication | public function build(array $context = array()) |
|
| 109 | |||
| 110 | public function action() |
||
| 114 | |||
| 115 | public function __switchboard($type = 'view') |
||
| 139 | |||
| 140 | public function view() |
||
| 144 | |||
| 145 | public function __viewIndex() |
||
| 146 | { |
||
| 147 | if (!$section_id = SectionManager::fetchIDFromHandle($this->_context['section_handle'])) { |
||
| 148 | Administration::instance()->throwCustomError( |
||
| 149 | __('The Section, %s, could not be found.', |
||
| 150 | array('<code>' . $this->_context['section_handle'] . '</code>')), |
||
| 151 | __('Unknown Section'), |
||
| 152 | Page::HTTP_STATUS_NOT_FOUND |
||
| 153 | ); |
||
| 154 | } elseif (!is_writable(CONFIG)) { |
||
| 155 | $this->pageAlert(__('The Symphony configuration file, %s, is not writable. The sort order cannot be modified.', |
||
| 156 | array('<code>/manifest/config.php</code>')), Alert::NOTICE); |
||
| 157 | } |
||
| 158 | |||
| 159 | $section = SectionManager::fetch($section_id); |
||
| 160 | |||
| 161 | $this->setPageType('table'); |
||
| 162 | $this->setTitle(__('%1$s – %2$s', array($section->get('name'), __('Symphony')))); |
||
| 163 | |||
| 164 | $filters = array(); |
||
| 165 | $filter_querystring = $prepopulate_querystring = $where = $joins = null; |
||
| 166 | $current_page = (isset($_REQUEST['pg']) && is_numeric($_REQUEST['pg']) ? max(1, |
||
| 167 | intval($_REQUEST['pg'])) : 1); |
||
| 168 | |||
| 169 | if (isset($_REQUEST['filter'])) { |
||
| 170 | // legacy implementation, convert single filter to an array |
||
| 171 | // split string in the form ?filter=handle:value |
||
| 172 | if (!is_array($_REQUEST['filter'])) { |
||
| 173 | list($field_handle, $filter_value) = explode(':', $_REQUEST['filter'], 2); |
||
| 174 | $filters[$field_handle] = rawurldecode($filter_value); |
||
| 175 | } else { |
||
| 176 | $filters = $_REQUEST['filter']; |
||
| 177 | } |
||
| 178 | |||
| 179 | foreach ($filters as $handle => $value) { |
||
| 180 | // Handle multiple values through filtering. RE: #2290 |
||
| 181 | if ((is_array($value) && empty($value)) || trim($value) === '') { |
||
| 182 | continue; |
||
| 183 | } |
||
| 184 | |||
| 185 | View Code Duplication | if (!is_array($value)) { |
|
| 186 | $filter_type = Datasource::determineFilterType($value); |
||
| 187 | $value = preg_split('/' . ($filter_type === Datasource::FILTER_AND ? '\+' : '(?<!\\\\),') . '\s*/', |
||
| 188 | $value, -1, PREG_SPLIT_NO_EMPTY); |
||
| 189 | $value = array_map('trim', $value); |
||
| 190 | $value = array_map(array('Datasource', 'removeEscapedCommas'), $value); |
||
| 191 | } |
||
| 192 | |||
| 193 | // Handle date meta data #2003 |
||
| 194 | $handle = Symphony::Database()->cleanValue($handle); |
||
| 195 | if (in_array($handle, array('system:creation-date', 'system:modification-date'))) { |
||
| 196 | $date_joins = ''; |
||
| 197 | $date_where = ''; |
||
| 198 | $date = new FieldDate(); |
||
| 199 | $date->buildDSRetrievalSQL($value, $date_joins, $date_where, |
||
| 200 | ($filter_type === Datasource::FILTER_AND ? true : false)); |
||
|
|
|||
| 201 | |||
| 202 | // Replace the date field where with the `creation_date` or `modification_date`. |
||
| 203 | $date_where = preg_replace('/`t\d+`.date/', |
||
| 204 | ($field_id !== 'system:modification-date') ? '`e`.creation_date_gmt' : '`e`.modification_date_gmt', |
||
| 205 | $date_where); |
||
| 206 | $where .= $date_where; |
||
| 207 | } else { |
||
| 208 | // Handle normal fields |
||
| 209 | $field_id = FieldManager::fetchFieldIDFromElementName( |
||
| 210 | $handle, |
||
| 211 | $section->get('id') |
||
| 212 | ); |
||
| 213 | |||
| 214 | $field = FieldManager::fetch($field_id); |
||
| 215 | if ($field instanceof Field) { |
||
| 216 | $field->buildDSRetrievalSQL($value, $joins, $where, |
||
| 217 | ($filter_type === Datasource::FILTER_AND ? true : false)); |
||
| 218 | |||
| 219 | $value = implode(',', $value); |
||
| 220 | $encoded_value = rawurlencode($value); |
||
| 221 | $filter_querystring .= sprintf("filter[%s]=%s&", $handle, $encoded_value); |
||
| 222 | |||
| 223 | // Some fields require that prepopulation be done via ID. RE: #2331 |
||
| 224 | if (!is_numeric($value) && method_exists($field, 'fetchIDfromValue')) { |
||
| 225 | $encoded_value = $field->fetchIDfromValue($value); |
||
| 226 | } |
||
| 227 | $prepopulate_querystring .= sprintf("prepopulate[%d]=%s&", $field_id, $encoded_value); |
||
| 228 | } else { |
||
| 229 | unset($filters[$handle]); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | $filter_querystring = preg_replace("/&$/", '', $filter_querystring); |
||
| 235 | $prepopulate_querystring = preg_replace("/&$/", '', $prepopulate_querystring); |
||
| 236 | } |
||
| 237 | |||
| 238 | Sortable::initialize($this, $entries, $sort, $order, array( |
||
| 239 | 'current-section' => $section, |
||
| 240 | 'filters' => ($filter_querystring ? "&" . $filter_querystring : ''), |
||
| 241 | 'unsort' => isset($_REQUEST['unsort']) |
||
| 242 | )); |
||
| 243 | |||
| 244 | $this->Form->setAttribute('action', |
||
| 245 | Administration::instance()->getCurrentPageURL() . '?pg=' . $current_page . ($filter_querystring ? "&" . $filter_querystring : '')); |
||
| 246 | |||
| 247 | // Build filtering interface |
||
| 248 | $this->createFilteringInterface(); |
||
| 249 | |||
| 250 | $subheading_buttons = array( |
||
| 251 | Widget::Anchor(__('Create New'), |
||
| 252 | Administration::instance()->getCurrentPageURL() . 'new/' . ($prepopulate_querystring ? '?' . $prepopulate_querystring : ''), |
||
| 253 | __('Create a new entry'), 'create button', null, array('accesskey' => 'c')) |
||
| 254 | ); |
||
| 255 | |||
| 256 | // Only show the Edit Section button if the Author is a developer. #938 ^BA |
||
| 257 | if (Symphony::Author()->isDeveloper()) { |
||
| 258 | array_unshift($subheading_buttons, |
||
| 259 | Widget::Anchor(__('Edit Section'), SYMPHONY_URL . '/blueprints/sections/edit/' . $section_id . '/', |
||
| 260 | __('Edit Section Configuration'), 'button')); |
||
| 261 | } |
||
| 262 | |||
| 263 | $this->appendSubheading($section->get('name'), $subheading_buttons); |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Allows adjustments to be made to the SQL where and joins statements |
||
| 267 | * before they are used to fetch the entries for the page |
||
| 268 | * |
||
| 269 | * @delegate AdjustPublishFiltering |
||
| 270 | * @since Symphony 2.3.3 |
||
| 271 | * @param string $context |
||
| 272 | * '/publish/' |
||
| 273 | * @param integer $section_id |
||
| 274 | * An array of the current columns, passed by reference |
||
| 275 | * @param string $where |
||
| 276 | * The current where statement, or null if not set |
||
| 277 | * @param string $joins |
||
| 278 | */ |
||
| 279 | Symphony::ExtensionManager()->notifyMembers('AdjustPublishFiltering', '/publish/', |
||
| 280 | array('section-id' => $section_id, 'where' => &$where, 'joins' => &$joins)); |
||
| 281 | |||
| 282 | // Check that the filtered query fails that the filter is dropped and an |
||
| 283 | // error is logged. #841 ^BA |
||
| 284 | try { |
||
| 285 | $entries = EntryManager::fetchByPage($current_page, $section_id, |
||
| 286 | Symphony::Configuration()->get('pagination_maximum_rows', 'symphony'), $where, $joins, true); |
||
| 287 | } catch (DatabaseException $ex) { |
||
| 288 | $this->pageAlert(__('An error occurred while retrieving filtered entries. Showing all entries instead.'), |
||
| 289 | Alert::ERROR); |
||
| 290 | $filter_querystring = null; |
||
| 291 | Symphony::Log()->warning(sprintf( |
||
| 292 | '%s - %s%s%s', |
||
| 293 | $section->get('name') . ' Publish Index', |
||
| 294 | $ex->getMessage(), |
||
| 295 | ($ex->getFile() ? " in file " . $ex->getFile() : null), |
||
| 296 | ($ex->getLine() ? " on line " . $ex->getLine() : null) |
||
| 297 | )); |
||
| 298 | $entries = EntryManager::fetchByPage($current_page, $section_id, |
||
| 299 | Symphony::Configuration()->get('pagination_maximum_rows', 'symphony')); |
||
| 300 | } |
||
| 301 | |||
| 302 | // Flag filtering |
||
| 303 | if (isset($_REQUEST['filter'])) { |
||
| 304 | $filter_stats = new XMLElement('p', '<span>– ' . __('%d of %d entries (filtered)', |
||
| 305 | array($entries['total-entries'], EntryManager::fetchCount($section_id))) . '</span>', |
||
| 306 | array('class' => 'inactive')); |
||
| 307 | } else { |
||
| 308 | $filter_stats = new XMLElement('p', |
||
| 309 | '<span>– ' . __('%d entries', array($entries['total-entries'])) . '</span>', |
||
| 310 | array('class' => 'inactive')); |
||
| 311 | } |
||
| 312 | $this->Breadcrumbs->appendChild($filter_stats); |
||
| 313 | |||
| 314 | // Build table |
||
| 315 | $visible_columns = $section->fetchVisibleColumns(); |
||
| 316 | $columns = array(); |
||
| 317 | |||
| 318 | if (is_array($visible_columns) && !empty($visible_columns)) { |
||
| 319 | foreach ($visible_columns as $column) { |
||
| 320 | $columns[] = array( |
||
| 321 | 'label' => $column->get('label'), |
||
| 322 | 'sortable' => $column->isSortable(), |
||
| 323 | 'handle' => $column->get('id'), |
||
| 324 | 'attrs' => array( |
||
| 325 | 'id' => 'field-' . $column->get('id'), |
||
| 326 | 'class' => 'field-' . $column->get('type') |
||
| 327 | ) |
||
| 328 | ); |
||
| 329 | } |
||
| 330 | } else { |
||
| 331 | $columns[] = array( |
||
| 332 | 'label' => __('ID'), |
||
| 333 | 'sortable' => true, |
||
| 334 | 'handle' => 'id' |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | |||
| 338 | $aTableHead = Sortable::buildTableHeaders($columns, $sort, $order, |
||
| 339 | ($filter_querystring) ? "&" . $filter_querystring : ''); |
||
| 340 | |||
| 341 | $child_sections = array(); |
||
| 342 | $associated_sections = $section->fetchChildAssociations(true); |
||
| 343 | |||
| 344 | if (is_array($associated_sections) && !empty($associated_sections)) { |
||
| 345 | foreach ($associated_sections as $key => $as) { |
||
| 346 | $child_sections[$key] = SectionManager::fetch($as['child_section_id']); |
||
| 347 | $aTableHead[] = array($child_sections[$key]->get('name'), 'col'); |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Allows the creation of custom table columns for each entry. Called |
||
| 353 | * after all the Section Visible columns have been added as well |
||
| 354 | * as the Section Associations |
||
| 355 | * |
||
| 356 | * @delegate AddCustomPublishColumn |
||
| 357 | * @since Symphony 2.2 |
||
| 358 | * @param string $context |
||
| 359 | * '/publish/' |
||
| 360 | * @param array $tableHead |
||
| 361 | * An array of the current columns, passed by reference |
||
| 362 | * @param integer $section_id |
||
| 363 | * The current Section ID |
||
| 364 | */ |
||
| 365 | Symphony::ExtensionManager()->notifyMembers('AddCustomPublishColumn', '/publish/', |
||
| 366 | array('tableHead' => &$aTableHead, 'section_id' => $section->get('id'))); |
||
| 367 | |||
| 368 | // Table Body |
||
| 369 | $aTableBody = array(); |
||
| 370 | |||
| 371 | if (!is_array($entries['records']) || empty($entries['records'])) { |
||
| 372 | $aTableBody = array( |
||
| 373 | Widget::TableRow(array(Widget::TableData(__('None found.'), 'inactive', null, count($aTableHead))), |
||
| 374 | 'odd') |
||
| 375 | ); |
||
| 376 | } else { |
||
| 377 | $field_pool = array(); |
||
| 378 | |||
| 379 | if (is_array($visible_columns) && !empty($visible_columns)) { |
||
| 380 | foreach ($visible_columns as $column) { |
||
| 381 | $field_pool[$column->get('id')] = $column; |
||
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | $link_column = array_reverse($visible_columns); |
||
| 386 | $link_column = end($link_column); |
||
| 387 | reset($visible_columns); |
||
| 388 | |||
| 389 | foreach ($entries['records'] as $entry) { |
||
| 390 | $tableData = array(); |
||
| 391 | |||
| 392 | // Setup each cell |
||
| 393 | if (!is_array($visible_columns) || empty($visible_columns)) { |
||
| 394 | $tableData[] = Widget::TableData(Widget::Anchor($entry->get('id'), |
||
| 395 | Administration::instance()->getCurrentPageURL() . 'edit/' . $entry->get('id') . '/')); |
||
| 396 | } else { |
||
| 397 | $link = Widget::Anchor( |
||
| 398 | '', |
||
| 399 | Administration::instance()->getCurrentPageURL() . 'edit/' . $entry->get('id') . '/' . ($filter_querystring ? '?' . $prepopulate_querystring : ''), |
||
| 400 | $entry->get('id'), |
||
| 401 | 'content' |
||
| 402 | ); |
||
| 403 | |||
| 404 | foreach ($visible_columns as $position => $column) { |
||
| 405 | $data = $entry->getData($column->get('id')); |
||
| 406 | $field = $field_pool[$column->get('id')]; |
||
| 407 | |||
| 408 | $value = $field->prepareTableValue($data, ($column === $link_column) ? $link : null, |
||
| 409 | $entry->get('id')); |
||
| 410 | |||
| 411 | if (!is_object($value) && (strlen(trim($value)) === 0 || $value === __('None'))) { |
||
| 412 | $value = ($position === 0 ? $link->generate() : __('None')); |
||
| 413 | } |
||
| 414 | |||
| 415 | if ($value === __('None')) { |
||
| 416 | $tableData[] = Widget::TableData($value, |
||
| 417 | 'inactive field-' . $column->get('type') . ' field-' . $column->get('id')); |
||
| 418 | } else { |
||
| 419 | $tableData[] = Widget::TableData($value, |
||
| 420 | 'field-' . $column->get('type') . ' field-' . $column->get('id')); |
||
| 421 | } |
||
| 422 | |||
| 423 | unset($field); |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | if (is_array($child_sections) && !empty($child_sections)) { |
||
| 428 | foreach ($child_sections as $key => $as) { |
||
| 429 | $field = FieldManager::fetch((int)$associated_sections[$key]['child_section_field_id']); |
||
| 430 | $parent_section_field_id = (int)$associated_sections[$key]['parent_section_field_id']; |
||
| 431 | |||
| 432 | if (!is_null($parent_section_field_id)) { |
||
| 433 | $search_value = $field->fetchAssociatedEntrySearchValue( |
||
| 434 | $entry->getData($parent_section_field_id), |
||
| 435 | $parent_section_field_id, |
||
| 436 | $entry->get('id') |
||
| 437 | ); |
||
| 438 | } else { |
||
| 439 | $search_value = $entry->get('id'); |
||
| 440 | } |
||
| 441 | |||
| 442 | if (!is_array($search_value)) { |
||
| 443 | $associated_entry_count = $field->fetchAssociatedEntryCount($search_value); |
||
| 444 | |||
| 445 | $tableData[] = Widget::TableData( |
||
| 446 | Widget::Anchor( |
||
| 447 | sprintf('%d →', max(0, intval($associated_entry_count))), |
||
| 448 | sprintf( |
||
| 449 | '%s/publish/%s/?filter[%s]=%s', |
||
| 450 | SYMPHONY_URL, |
||
| 451 | $as->get('handle'), |
||
| 452 | $field->get('element_name'), |
||
| 453 | rawurlencode($search_value) |
||
| 454 | ), |
||
| 455 | $entry->get('id'), |
||
| 456 | 'content' |
||
| 457 | ) |
||
| 458 | ); |
||
| 459 | } |
||
| 460 | } |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Allows Extensions to inject custom table data for each Entry |
||
| 465 | * into the Publish Index |
||
| 466 | * |
||
| 467 | * @delegate AddCustomPublishColumnData |
||
| 468 | * @since Symphony 2.2 |
||
| 469 | * @param string $context |
||
| 470 | * '/publish/' |
||
| 471 | * @param array $tableData |
||
| 472 | * An array of `Widget::TableData`, passed by reference |
||
| 473 | * @param integer $section_id |
||
| 474 | * The current Section ID |
||
| 475 | * @param Entry $entry_id |
||
| 476 | * The entry object, please note that this is by error and this will |
||
| 477 | * be removed in Symphony 2.4. The entry object is available in |
||
| 478 | * the 'entry' key as of Symphony 2.3.1. |
||
| 479 | * @param Entry $entry |
||
| 480 | * The entry object for this row |
||
| 481 | */ |
||
| 482 | Symphony::ExtensionManager()->notifyMembers('AddCustomPublishColumnData', '/publish/', array( |
||
| 483 | 'tableData' => &$tableData, |
||
| 484 | 'section_id' => $section->get('id'), |
||
| 485 | 'entry_id' => $entry, |
||
| 486 | 'entry' => $entry |
||
| 487 | )); |
||
| 488 | |||
| 489 | $tableData[count($tableData) - 1]->appendChild(Widget::Label(__('Select Entry %d', |
||
| 490 | array($entry->get('id'))), null, 'accessible', null, array( |
||
| 491 | 'for' => 'entry-' . $entry->get('id') |
||
| 492 | ))); |
||
| 493 | $tableData[count($tableData) - 1]->appendChild(Widget::Input('items[' . $entry->get('id') . ']', |
||
| 494 | null, 'checkbox', array( |
||
| 495 | 'id' => 'entry-' . $entry->get('id') |
||
| 496 | ))); |
||
| 497 | |||
| 498 | // Add a row to the body array, assigning each cell to the row |
||
| 499 | $aTableBody[] = Widget::TableRow($tableData, null, 'id-' . $entry->get('id')); |
||
| 500 | } |
||
| 501 | } |
||
| 502 | |||
| 503 | $table = Widget::Table( |
||
| 504 | Widget::TableHead($aTableHead), |
||
| 505 | null, |
||
| 506 | Widget::TableBody($aTableBody), |
||
| 507 | 'selectable', |
||
| 508 | null, |
||
| 509 | array( |
||
| 510 | 'role' => 'directory', |
||
| 511 | 'aria-labelledby' => 'symphony-subheading', |
||
| 512 | 'data-interactive' => 'data-interactive' |
||
| 513 | ) |
||
| 514 | ); |
||
| 515 | |||
| 516 | $this->Form->appendChild($table); |
||
| 517 | |||
| 518 | $tableActions = new XMLElement('div'); |
||
| 519 | $tableActions->setAttribute('class', 'actions'); |
||
| 520 | |||
| 521 | $options = array( |
||
| 522 | array(null, false, __('With Selected...')), |
||
| 523 | array( |
||
| 524 | 'delete', |
||
| 525 | false, |
||
| 526 | __('Delete'), |
||
| 527 | 'confirm', |
||
| 528 | null, |
||
| 529 | array( |
||
| 530 | 'data-message' => __('Are you sure you want to delete the selected entries?') |
||
| 531 | ) |
||
| 532 | ) |
||
| 533 | ); |
||
| 534 | |||
| 535 | $toggable_fields = $section->fetchToggleableFields(); |
||
| 536 | |||
| 537 | if (is_array($toggable_fields) && !empty($toggable_fields)) { |
||
| 538 | $index = 2; |
||
| 539 | |||
| 540 | foreach ($toggable_fields as $field) { |
||
| 541 | $toggle_states = $field->getToggleStates(); |
||
| 542 | |||
| 543 | if (is_array($toggle_states)) { |
||
| 544 | $options[$index] = array( |
||
| 545 | 'label' => __('Set %s', array($field->get('label'))), |
||
| 546 | 'options' => array() |
||
| 547 | ); |
||
| 548 | |||
| 549 | foreach ($toggle_states as $value => $state) { |
||
| 550 | $options[$index]['options'][] = array( |
||
| 551 | 'toggle-' . $field->get('id') . '-' . $value, |
||
| 552 | false, |
||
| 553 | $state |
||
| 554 | ); |
||
| 555 | } |
||
| 556 | } |
||
| 557 | |||
| 558 | $index++; |
||
| 559 | } |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Allows an extension to modify the existing options for this page's |
||
| 564 | * With Selected menu. If the `$options` parameter is an empty array, |
||
| 565 | * the 'With Selected' menu will not be rendered. |
||
| 566 | * |
||
| 567 | * @delegate AddCustomActions |
||
| 568 | * @since Symphony 2.3.2 |
||
| 569 | * @param string $context |
||
| 570 | * '/publish/' |
||
| 571 | * @param array $options |
||
| 572 | * An array of arrays, where each child array represents an option |
||
| 573 | * in the With Selected menu. Options should follow the same format |
||
| 574 | * expected by `Widget::__SelectBuildOption`. Passed by reference. |
||
| 575 | */ |
||
| 576 | Symphony::ExtensionManager()->notifyMembers('AddCustomActions', '/publish/', array( |
||
| 577 | 'options' => &$options |
||
| 578 | )); |
||
| 579 | |||
| 580 | if (!empty($options)) { |
||
| 581 | $tableActions->appendChild(Widget::Apply($options)); |
||
| 582 | $this->Form->appendChild($tableActions); |
||
| 583 | } |
||
| 584 | |||
| 585 | if ($entries['total-pages'] > 1) { |
||
| 586 | $ul = new XMLElement('ul'); |
||
| 587 | $ul->setAttribute('class', 'page'); |
||
| 588 | |||
| 589 | // First |
||
| 590 | $li = new XMLElement('li'); |
||
| 591 | |||
| 592 | if ($current_page > 1) { |
||
| 593 | $li->appendChild(Widget::Anchor(__('First'), |
||
| 594 | Administration::instance()->getCurrentPageURL() . '?pg=1' . ($filter_querystring ? "&" . $filter_querystring : ''))); |
||
| 595 | } else { |
||
| 596 | $li->setValue(__('First')); |
||
| 597 | } |
||
| 598 | |||
| 599 | $ul->appendChild($li); |
||
| 600 | |||
| 601 | // Previous |
||
| 602 | $li = new XMLElement('li'); |
||
| 603 | |||
| 604 | View Code Duplication | if ($current_page > 1) { |
|
| 605 | $li->appendChild(Widget::Anchor(__('← Previous'), |
||
| 606 | Administration::instance()->getCurrentPageURL() . '?pg=' . ($current_page - 1) . ($filter_querystring ? "&" . $filter_querystring : ''))); |
||
| 607 | } else { |
||
| 608 | $li->setValue(__('← Previous')); |
||
| 609 | } |
||
| 610 | |||
| 611 | $ul->appendChild($li); |
||
| 612 | |||
| 613 | // Summary |
||
| 614 | $li = new XMLElement('li'); |
||
| 615 | |||
| 616 | $li->setAttribute('title', __('Viewing %1$s - %2$s of %3$s entries', array( |
||
| 617 | $entries['start'], |
||
| 618 | ($current_page !== $entries['total-pages']) ? $current_page * Symphony::Configuration()->get('pagination_maximum_rows', |
||
| 619 | 'symphony') : $entries['total-entries'], |
||
| 620 | $entries['total-entries'] |
||
| 621 | ))); |
||
| 622 | |||
| 623 | $pgform = Widget::Form(Administration::instance()->getCurrentPageURL(), 'get', 'paginationform'); |
||
| 624 | |||
| 625 | $pgmax = max($current_page, $entries['total-pages']); |
||
| 626 | $pgform->appendChild(Widget::Input('pg', null, 'text', array( |
||
| 627 | 'data-active' => __('Go to page …'), |
||
| 628 | 'data-inactive' => __('Page %1$s of %2$s', array((string)$current_page, $pgmax)), |
||
| 629 | 'data-max' => $pgmax |
||
| 630 | ))); |
||
| 631 | |||
| 632 | $li->appendChild($pgform); |
||
| 633 | $ul->appendChild($li); |
||
| 634 | |||
| 635 | // Next |
||
| 636 | $li = new XMLElement('li'); |
||
| 637 | |||
| 638 | View Code Duplication | if ($current_page < $entries['total-pages']) { |
|
| 639 | $li->appendChild(Widget::Anchor(__('Next →'), |
||
| 640 | Administration::instance()->getCurrentPageURL() . '?pg=' . ($current_page + 1) . ($filter_querystring ? "&" . $filter_querystring : ''))); |
||
| 641 | } else { |
||
| 642 | $li->setValue(__('Next →')); |
||
| 643 | } |
||
| 644 | |||
| 645 | $ul->appendChild($li); |
||
| 646 | |||
| 647 | // Last |
||
| 648 | $li = new XMLElement('li'); |
||
| 649 | |||
| 650 | View Code Duplication | if ($current_page < $entries['total-pages']) { |
|
| 651 | $li->appendChild(Widget::Anchor(__('Last'), |
||
| 652 | Administration::instance()->getCurrentPageURL() . '?pg=' . $entries['total-pages'] . ($filter_querystring ? "&" . $filter_querystring : ''))); |
||
| 653 | } else { |
||
| 654 | $li->setValue(__('Last')); |
||
| 655 | } |
||
| 656 | |||
| 657 | $ul->appendChild($li); |
||
| 658 | |||
| 659 | $this->Contents->appendChild($ul); |
||
| 660 | } |
||
| 661 | } |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Append filtering interface |
||
| 665 | */ |
||
| 666 | public function createFilteringInterface() |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Create filtering drawer |
||
| 686 | * |
||
| 687 | * @param Section $section |
||
| 688 | * @return XMLElement |
||
| 689 | */ |
||
| 690 | public function createFilteringDrawer(Section $section) |
||
| 697 | |||
| 698 | public function createFilteringDuplicator($section) |
||
| 715 | |||
| 716 | private function createFieldFilters(&$wrapper, $section) |
||
| 750 | |||
| 751 | private function createFilterComparisons($data) |
||
| 769 | |||
| 770 | private function getFilterQuery($data) |
||
| 784 | |||
| 785 | private function createFilter(&$wrapper, $data) |
||
| 827 | |||
| 828 | private function createFilterSuggestions(&$wrapper, $data) |
||
| 843 | |||
| 844 | private function createFilterHelp(&$wrapper, $operator) |
||
| 857 | |||
| 858 | private function createSystemDateFilters(&$wrapper) |
||
| 899 | |||
| 900 | public function __actionIndex() |
||
| 1012 | |||
| 1013 | public function __viewNew() |
||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * If this entry is being prepopulated, this function will return the prepopulated |
||
| 1139 | * fields and values as a query string. |
||
| 1140 | * |
||
| 1141 | * @since Symphony 2.5.2 |
||
| 1142 | * @return string |
||
| 1143 | */ |
||
| 1144 | public function getPrepopulateString() |
||
| 1163 | |||
| 1164 | /** |
||
| 1165 | * If the entry is being prepopulated, we may want to filter other views by this entry's |
||
| 1166 | * value. This function will create that filter query string. |
||
| 1167 | * |
||
| 1168 | * @since Symphony 2.5.2 |
||
| 1169 | * @return string |
||
| 1170 | */ |
||
| 1171 | public function getFilterString() |
||
| 1200 | |||
| 1201 | /** |
||
| 1202 | * Given a Field and Entry object, this function will wrap |
||
| 1203 | * the Field's displayPublishPanel result with a div that |
||
| 1204 | * contains some contextual information such as the Field ID, |
||
| 1205 | * the Field handle and whether it is required or not. |
||
| 1206 | * |
||
| 1207 | * @param Field $field |
||
| 1208 | * @param Entry $entry |
||
| 1209 | * @return XMLElement |
||
| 1210 | */ |
||
| 1211 | private function __wrapFieldWithDiv(Field $field, Entry $entry) |
||
| 1251 | |||
| 1252 | /** |
||
| 1253 | * Check whether the given `$field` will be hidden because it's been |
||
| 1254 | * prepopulated. |
||
| 1255 | * |
||
| 1256 | * @param Field $field |
||
| 1257 | * @return boolean |
||
| 1258 | */ |
||
| 1259 | public function isFieldHidden(Field $field) |
||
| 1273 | |||
| 1274 | /** |
||
| 1275 | * Prepare a Drawer to visualize section associations |
||
| 1276 | * |
||
| 1277 | * @param Section $section The current Section object |
||
| 1278 | * @throws InvalidArgumentException |
||
| 1279 | * @throws Exception |
||
| 1280 | */ |
||
| 1281 | private function prepareAssociationsDrawer($section) |
||
| 1490 | |||
| 1491 | public function __actionNew() |
||
| 1590 | |||
| 1591 | public function __viewEdit() |
||
| 1808 | |||
| 1809 | public function __actionEdit() |
||
| 1915 | } |
||
| 1916 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: