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 Field 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 Field, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Field |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * The desired result when creating a field in the section editor |
||
| 18 | * |
||
| 19 | * @var integer |
||
| 20 | */ |
||
| 21 | const __OK__ = 100; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * If an error occurring when saving a section because of this field, |
||
| 25 | * this will be returned |
||
| 26 | * |
||
| 27 | * @var integer |
||
| 28 | */ |
||
| 29 | const __ERROR__ = 150; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * When saving a section, if a value that is required is missing, |
||
| 33 | * this will be returned |
||
| 34 | * |
||
| 35 | * @var integer |
||
| 36 | */ |
||
| 37 | const __MISSING_FIELDS__ = 200; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * If a value for a setting is invalid, this will be returned |
||
| 41 | * |
||
| 42 | * @var integer |
||
| 43 | */ |
||
| 44 | const __INVALID_FIELDS__ = 220; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * If there already is an instance of this field in this section and |
||
| 48 | * `mustBeUnique()` returns true, this will be returned |
||
| 49 | * |
||
| 50 | * @var integer |
||
| 51 | * @see mustBeUnique() |
||
| 52 | */ |
||
| 53 | const __DUPLICATE__ = 300; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Fields can returned this is an error occurred when saving the |
||
| 57 | * field's settings that doesn't fit another `Field` constant |
||
| 58 | * |
||
| 59 | * @var integer |
||
| 60 | */ |
||
| 61 | const __ERROR_CUSTOM__ = 400; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * If the field name is not a valid QName, this error will be returned |
||
| 65 | * |
||
| 66 | * @var integer |
||
| 67 | */ |
||
| 68 | const __INVALID_QNAME__ = 500; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Used by the `FieldManager` to return fields that can be toggled |
||
| 72 | * |
||
| 73 | * @var integer |
||
| 74 | */ |
||
| 75 | const __TOGGLEABLE_ONLY__ = 600; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Used by the `FieldManager` to return fields that can't be toggled |
||
| 79 | * |
||
| 80 | * @var integer |
||
| 81 | */ |
||
| 82 | const __UNTOGGLEABLE_ONLY__ = 700; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Used by the `FieldManager` to return fields that can be filtered |
||
| 86 | * |
||
| 87 | * @var integer |
||
| 88 | */ |
||
| 89 | const __FILTERABLE_ONLY__ = 800; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Used by the `FieldManager` to return fields that can't be filtered |
||
| 93 | * |
||
| 94 | * @var integer |
||
| 95 | */ |
||
| 96 | const __UNFILTERABLE_ONLY__ = 900; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Used by the `FieldManager` to just return all fields |
||
| 100 | * |
||
| 101 | * @var integer |
||
| 102 | */ |
||
| 103 | const __FIELD_ALL__ = 1000; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Used to manage the joins when this field used in a datasource |
||
| 107 | * |
||
| 108 | * @var integer |
||
| 109 | */ |
||
| 110 | protected $_key = 0; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * An associative array of the settings for this `Field` instance |
||
| 114 | * |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | protected $_settings = array(); |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Whether this field is required inherently, defaults to false. |
||
| 121 | * |
||
| 122 | * @var boolean |
||
| 123 | */ |
||
| 124 | protected $_required = false; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Whether this field can be viewed on the entries table. Note |
||
| 128 | * that this is not the same variable as the one set when saving |
||
| 129 | * a field in the section editor, rather just the if the field has |
||
| 130 | * the ability to be shown. Defaults to true. |
||
| 131 | * |
||
| 132 | * @var boolean |
||
| 133 | */ |
||
| 134 | protected $_showcolumn = true; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Whether this field has an association that should be shown on |
||
| 138 | * the Publish Index. This does not mean that it will be, but just |
||
| 139 | * that this field has the ability too. Defaults to false. |
||
| 140 | * |
||
| 141 | * @var boolean |
||
| 142 | */ |
||
| 143 | protected $_showassociation = false; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Construct a new instance of this field. |
||
| 147 | */ |
||
| 148 | public function __construct() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Test whether this field can show the table column. |
||
| 156 | * |
||
| 157 | * @return boolean |
||
| 158 | * true if this can, false otherwise. |
||
| 159 | */ |
||
| 160 | public function canShowTableColumn() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Test whether this field can show the association column in |
||
| 167 | * the Publish Index. |
||
| 168 | * |
||
| 169 | * @since Symphony 2.6.0 |
||
| 170 | * @return boolean |
||
| 171 | * true if this can, false otherwise. |
||
| 172 | */ |
||
| 173 | public function canShowAssociationColumn() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Test whether this field can be toggled using the With Selected menu |
||
| 180 | * on the Publish Index. |
||
| 181 | * |
||
| 182 | * @return boolean |
||
| 183 | * true if it can be toggled, false otherwise. |
||
| 184 | */ |
||
| 185 | public function canToggle() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Accessor to the toggle states. This default implementation returns |
||
| 192 | * an empty array. |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | * the array of toggle states. |
||
| 196 | */ |
||
| 197 | public function getToggleStates() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Toggle the field data. This default implementation always returns |
||
| 204 | * the input data. |
||
| 205 | * |
||
| 206 | * @param array $data |
||
| 207 | * the data to toggle. |
||
| 208 | * @param string $newState |
||
| 209 | * the new value to set |
||
| 210 | * @param integer $entry_id (optional) |
||
| 211 | * an optional entry ID for more intelligent processing. defaults to null |
||
| 212 | * @return array |
||
| 213 | * the toggled data. |
||
| 214 | */ |
||
| 215 | public function toggleFieldData(array $data, $newState, $entry_id = null) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Test whether this field can be filtered in the publish index. This default |
||
| 222 | * implementation prohibts filtering. Publish Filtering allows the index view |
||
| 223 | * to filter results. Subclasses should override this if |
||
| 224 | * filtering is supported. |
||
| 225 | * |
||
| 226 | * @return boolean |
||
| 227 | * true if this can be publish-filtered, false otherwise. |
||
| 228 | */ |
||
| 229 | public function canPublishFilter() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Test whether this field can be filtered. This default implementation |
||
| 236 | * prohibits filtering. Filtering allows the XML output results to be limited |
||
| 237 | * according to an input parameter. Subclasses should override this if |
||
| 238 | * filtering is supported. |
||
| 239 | * |
||
| 240 | * @return boolean |
||
| 241 | * true if this can be filtered, false otherwise. |
||
| 242 | */ |
||
| 243 | public function canFilter() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Test whether this field can be prepopulated with data. This default |
||
| 250 | * implementation does not support pre-population and, thus, returns false. |
||
| 251 | * |
||
| 252 | * @return boolean |
||
| 253 | * true if this can be pre-populated, false otherwise. |
||
| 254 | */ |
||
| 255 | public function canPrePopulate() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Test whether this field can be sorted. This default implementation |
||
| 262 | * returns false. |
||
| 263 | * |
||
| 264 | * @return boolean |
||
| 265 | * true if this field is sortable, false otherwise. |
||
| 266 | */ |
||
| 267 | public function isSortable() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Test whether this field must be unique in a section, that is, only one of |
||
| 274 | * this field's type is allowed per section. This default implementation |
||
| 275 | * always returns false. |
||
| 276 | * |
||
| 277 | * @return boolean |
||
| 278 | * true if the content of this field must be unique, false otherwise. |
||
| 279 | */ |
||
| 280 | public function mustBeUnique() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Test whether this field supports data-source output grouping. This |
||
| 287 | * default implementation prohibits grouping. Data-source grouping allows |
||
| 288 | * clients of this field to group the XML output according to this field. |
||
| 289 | * Subclasses should override this if grouping is supported. |
||
| 290 | * |
||
| 291 | * @return boolean |
||
| 292 | * true if this field does support data-source grouping, false otherwise. |
||
| 293 | */ |
||
| 294 | public function allowDatasourceOutputGrouping() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Test whether this field requires grouping. If this function returns true |
||
| 301 | * SQL statements generated in the `EntryManager` will include the `DISTINCT` keyword |
||
| 302 | * to only return a single row for an entry regardless of how many 'matches' it |
||
| 303 | * might have. This default implementation returns false. |
||
| 304 | * |
||
| 305 | * @return boolean |
||
| 306 | * true if this field requires grouping, false otherwise. |
||
| 307 | */ |
||
| 308 | public function requiresSQLGrouping() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Test whether this field supports data-source parameter output. This |
||
| 315 | * default implementation prohibits parameter output. Data-source |
||
| 316 | * parameter output allows this field to be provided as a parameter |
||
| 317 | * to other data-sources or XSLT. Subclasses should override this if |
||
| 318 | * parameter output is supported. |
||
| 319 | * |
||
| 320 | * @return boolean |
||
| 321 | * true if this supports data-source parameter output, false otherwise. |
||
| 322 | */ |
||
| 323 | public function allowDatasourceParamOutput() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Fill the input data array with default values for known keys provided |
||
| 330 | * these settings are not already set. The input array is then used to set |
||
| 331 | * the values of the corresponding settings for this field. This function |
||
| 332 | * is called when a section is saved. |
||
| 333 | * |
||
| 334 | * @param array $settings |
||
| 335 | * the data array to initialize if necessary. |
||
| 336 | */ |
||
| 337 | public function setFromPOST(array $settings = array()) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Add or overwrite the settings of this field by providing an associative array |
||
| 348 | * of the settings. This will do nothing if the input array is empty. If a setting is |
||
| 349 | * omitted from the input array, it will not be unset by this function |
||
| 350 | * |
||
| 351 | * @param array $array |
||
| 352 | * the associative array of settings for this field |
||
| 353 | */ |
||
| 354 | public function setArray(array $array = array()) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Fields have settings that define how that field will act in a section, including |
||
| 367 | * if it's required, any validators, if it can be shown on the entries table etc. This |
||
| 368 | * function will set a setting to a value. This function will set a setting to a value |
||
| 369 | * overwriting any existing value for this setting |
||
| 370 | * |
||
| 371 | * @param string $setting |
||
| 372 | * the setting key. |
||
| 373 | * @param mixed $value |
||
| 374 | * the value of the setting. |
||
| 375 | */ |
||
| 376 | public function set($setting, $value) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Unset the value of a setting by the key |
||
| 383 | * |
||
| 384 | * @param string $setting |
||
| 385 | * the key of the setting to unset. |
||
| 386 | */ |
||
| 387 | public function remove($setting) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Just prior to the field being deleted, this function allows |
||
| 394 | * Fields to cleanup any additional things before it is removed |
||
| 395 | * from the section. This may be useful to remove data from any |
||
| 396 | * custom field tables or the configuration. |
||
| 397 | * |
||
| 398 | * @since Symphony 2.2.1 |
||
| 399 | * @return boolean |
||
| 400 | */ |
||
| 401 | public function tearDown() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Allows a field to set default settings. |
||
| 408 | * |
||
| 409 | * @param array $settings |
||
| 410 | * the array of settings to populate with their defaults. |
||
| 411 | */ |
||
| 412 | public function findDefaults(array &$settings) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Display the default settings panel, calls the `buildSummaryBlock` |
||
| 418 | * function after basic field settings are added to the wrapper. |
||
| 419 | * |
||
| 420 | * @see buildSummaryBlock() |
||
| 421 | * @param XMLElement $wrapper |
||
| 422 | * the input XMLElement to which the display of this will be appended. |
||
| 423 | * @param mixed $errors |
||
| 424 | * the input error collection. this defaults to null. |
||
| 425 | * @throws InvalidArgumentException |
||
| 426 | */ |
||
| 427 | public function displaySettingsPanel(XMLElement &$wrapper, $errors = null) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Accessor to the a setting by name. If no setting is provided all the |
||
| 452 | * settings of this `Field` instance are returned. |
||
| 453 | * |
||
| 454 | * @param string $setting (optional) |
||
| 455 | * the name of the setting to access the value for. This is optional and |
||
| 456 | * defaults to null in which case all settings are returned. |
||
| 457 | * @return null|mixed|array |
||
| 458 | * the value of the setting if there is one, all settings if the input setting |
||
| 459 | * was omitted or null if the setting was supplied but there is no value |
||
| 460 | * for that setting. |
||
| 461 | */ |
||
| 462 | View Code Duplication | public function get($setting = null) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Accessor to the name of this field object. The name may contain characters |
||
| 477 | * that normally would be stripped in the handle while also allowing the field |
||
| 478 | * name to be localized. If a name is not set, it will return the handle of the |
||
| 479 | * the field |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | * The field name |
||
| 483 | */ |
||
| 484 | public function name() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Accessor to the handle of this field object. The Symphony convention is |
||
| 491 | * for field subclass names to be prefixed with field. Handle removes this prefix |
||
| 492 | * so that the class handle can be used as the field type. |
||
| 493 | * |
||
| 494 | * @return string |
||
| 495 | * The field classname minus the field prefix. |
||
| 496 | */ |
||
| 497 | public function handle() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Construct the html block to display a summary of this field, which is the field |
||
| 504 | * Label and it's location within the section. Any error messages generated are |
||
| 505 | * appended to the optional input error array. This function calls |
||
| 506 | * `buildLocationSelect` once it is completed |
||
| 507 | * |
||
| 508 | * @see buildLocationSelect() |
||
| 509 | * @param array $errors (optional) |
||
| 510 | * an array to append html formatted error messages to. this defaults to null. |
||
| 511 | * @throws InvalidArgumentException |
||
| 512 | * @return XMLElement |
||
| 513 | * the root XML element of the html display of this. |
||
| 514 | */ |
||
| 515 | public function buildSummaryBlock($errors = null) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Build the location select widget. This widget allows users to select |
||
| 557 | * whether this field will appear in the main content column or in the sidebar |
||
| 558 | * when creating a new entry. |
||
| 559 | * |
||
| 560 | * @param string|null $selected (optional) |
||
| 561 | * the currently selected location, if there is one. this defaults to null. |
||
| 562 | * @param string $name (optional) |
||
| 563 | * the name of this field. this is optional and defaults to `fields[location]`. |
||
| 564 | * @param string $label_value (optional) |
||
| 565 | * any predefined label for this widget. this is an optional argument that defaults |
||
| 566 | * to null. |
||
| 567 | * @throws InvalidArgumentException |
||
| 568 | * @return XMLElement |
||
| 569 | * An XMLElement representing a `<select>` field containing the options. |
||
| 570 | */ |
||
| 571 | public function buildLocationSelect($selected = null, $name = 'fields[location]', $label_value = null) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Construct the html widget for selecting a text formatter for this field. |
||
| 591 | * |
||
| 592 | * @param string $selected (optional) |
||
| 593 | * the currently selected text formatter name if there is one. this defaults |
||
| 594 | * to null. |
||
| 595 | * @param string $name (optional) |
||
| 596 | * the name of this field in the form. this is optional and defaults to |
||
| 597 | * "fields[format]". |
||
| 598 | * @param string $label_value |
||
| 599 | * the default label for the widget to construct. if null is passed in then |
||
| 600 | * this defaults to the localization of "Formatting". |
||
| 601 | * @throws InvalidArgumentException |
||
| 602 | * @return XMLElement |
||
| 603 | * An XMLElement representing a `<select>` field containing the options. |
||
| 604 | */ |
||
| 605 | public function buildFormatterSelect($selected = null, $name = 'fields[format]', $label_value) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Append a validator selector to a given `XMLElement`. Note that this |
||
| 633 | * function differs from the other two similarly named build functions in |
||
| 634 | * that it takes an `XMLElement` to append the Validator to as a parameter, |
||
| 635 | * and does not return anything. |
||
| 636 | * |
||
| 637 | * @param XMLElement $wrapper |
||
| 638 | * the parent element to append the XMLElement of the Validation select to, |
||
| 639 | * passed by reference. |
||
| 640 | * @param string $selected (optional) |
||
| 641 | * the current validator selection if there is one. defaults to null if there |
||
| 642 | * isn't. |
||
| 643 | * @param string $name (optional) |
||
| 644 | * the form element name of this field. this defaults to "fields[validator]". |
||
| 645 | * @param string $type (optional) |
||
| 646 | * the type of input for the validation to apply to. this defaults to 'input' |
||
| 647 | * but also accepts 'upload'. |
||
| 648 | * @param array $errors (optional) |
||
| 649 | * an associative array of errors |
||
| 650 | * @throws InvalidArgumentException |
||
| 651 | */ |
||
| 652 | public function buildValidationSelect( |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Append the html widget for selecting an association interface and editor |
||
| 688 | * for this field. |
||
| 689 | * |
||
| 690 | * @param XMLElement $wrapper |
||
| 691 | * the parent XML element to append the association interface selection to, |
||
| 692 | * if either interfaces or editors are provided to the system. |
||
| 693 | * @since Symphony 2.5.0 |
||
| 694 | */ |
||
| 695 | public function appendAssociationInterfaceSelect(XMLElement &$wrapper) |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Get association data of the current field from the page context. |
||
| 750 | * |
||
| 751 | * @since Symphony 2.5.0 |
||
| 752 | * @return array |
||
| 753 | */ |
||
| 754 | public function getAssociationContext() |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Set association data for the current field. |
||
| 781 | * |
||
| 782 | * @since Symphony 2.5.0 |
||
| 783 | * @param XMLElement $wrapper |
||
| 784 | */ |
||
| 785 | public function setAssociationContext(XMLElement &$wrapper) |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Append the show association html widget to the input parent XML element. This |
||
| 803 | * widget allows fields that provide linking to hide or show the column in the linked |
||
| 804 | * section, similar to how the Show Column functionality works, but for the linked |
||
| 805 | * section. |
||
| 806 | * |
||
| 807 | * @param XMLElement $wrapper |
||
| 808 | * the parent XML element to append the checkbox to. |
||
| 809 | * @param string $help (optional) |
||
| 810 | * a help message to show below the checkbox. |
||
| 811 | * @throws InvalidArgumentException |
||
| 812 | */ |
||
| 813 | public function appendShowAssociationCheckbox(XMLElement &$wrapper, $help = null) |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Given the setting name and the label, this helper method will add |
||
| 826 | * the required markup for a checkbox to the given `$wrapper`. |
||
| 827 | * |
||
| 828 | * @since Symphony 2.5.2 |
||
| 829 | * @param XMLElement $wrapper |
||
| 830 | * Passed by reference, this will have the resulting markup appended to it |
||
| 831 | * @param string $setting |
||
| 832 | * This will be used with $this->get() to get the existing value |
||
| 833 | * @param string $label_description |
||
| 834 | * This will be localisable and displayed after the checkbox when |
||
| 835 | * generated. |
||
| 836 | * @param string $help (optional) |
||
| 837 | * A help message to show below the checkbox. |
||
| 838 | * @return XMLElement |
||
| 839 | * The Label and Checkbox that was just added to the `$wrapper`. |
||
| 840 | */ |
||
| 841 | public function createCheckboxSetting(XMLElement &$wrapper, $setting, $label_description, $help = null) |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Append the default status footer to the field settings panel. |
||
| 854 | * Displays the required and show column checkboxes. |
||
| 855 | * |
||
| 856 | * @param XMLElement $wrapper |
||
| 857 | * the parent XML element to append the checkbox to. |
||
| 858 | * @throws InvalidArgumentException |
||
| 859 | */ |
||
| 860 | public function appendStatusFooter(XMLElement &$wrapper) |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Append and set a labeled html checkbox to the input XML element if this |
||
| 874 | * field is set as a required field. |
||
| 875 | * |
||
| 876 | * @param XMLElement $wrapper |
||
| 877 | * the parent XML element to append the constructed html checkbox to if |
||
| 878 | * necessary. |
||
| 879 | * @throws InvalidArgumentException |
||
| 880 | */ |
||
| 881 | public function appendRequiredCheckbox(XMLElement &$wrapper) |
||
| 889 | |||
| 890 | /** |
||
| 891 | * Append the show column html widget to the input parent XML element. This |
||
| 892 | * displays a column in the entries table or not. |
||
| 893 | * |
||
| 894 | * @param XMLElement $wrapper |
||
| 895 | * the parent XML element to append the checkbox to. |
||
| 896 | * @throws InvalidArgumentException |
||
| 897 | */ |
||
| 898 | public function appendShowColumnCheckbox(XMLElement &$wrapper) |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Check the field's settings to ensure they are valid on the section |
||
| 909 | * editor |
||
| 910 | * |
||
| 911 | * @param array $errors |
||
| 912 | * the array to populate with the errors found. |
||
| 913 | * @param boolean $checkForDuplicates (optional) |
||
| 914 | * if set to true, duplicate Field name's in the same section will be flagged |
||
| 915 | * as errors. Defaults to true. |
||
| 916 | * @return integer |
||
| 917 | * returns the status of the checking. if errors has been populated with |
||
| 918 | * any errors `self::__ERROR__`, `self::__OK__` otherwise. |
||
| 919 | */ |
||
| 920 | public function checkFields(array &$errors, $checkForDuplicates = true) |
||
| 961 | |||
| 962 | /** |
||
| 963 | * Format this field value for display in the Associations Drawer publish index. |
||
| 964 | * By default, Symphony will use the return value of the `prepareReadableValue` function. |
||
| 965 | * |
||
| 966 | * @since Symphony 2.4 |
||
| 967 | * @since Symphony 2.5.0 The prepopulate parameter was added. |
||
| 968 | * |
||
| 969 | * @param Entry $e |
||
| 970 | * The associated entry |
||
| 971 | * @param array $parent_association |
||
| 972 | * An array containing information about the association |
||
| 973 | * @param string $prepopulate |
||
| 974 | * A string containing prepopulate parameter to append to the association url |
||
| 975 | * |
||
| 976 | * @return XMLElement |
||
| 977 | * The XMLElement must be a li node, since it will be added an ul node. |
||
| 978 | */ |
||
| 979 | public function prepareAssociationsDrawerXMLElement(Entry $e, array $parent_association, $prepopulate = '') |
||
| 997 | |||
| 998 | /** |
||
| 999 | * Format this field value for display as readable text value. By default, it |
||
| 1000 | * will call `Field::prepareTextValue` to get the raw text value of this field. |
||
| 1001 | * |
||
| 1002 | * If $truncate is set to true, Symphony will truncate the value to the |
||
| 1003 | * configuration setting `cell_truncation_length`. |
||
| 1004 | * |
||
| 1005 | * @since Symphony 2.5.0 |
||
| 1006 | * @param array $data |
||
| 1007 | * an associative array of data for this string. At minimum this requires a |
||
| 1008 | * key of 'value'. |
||
| 1009 | * @param integer $entry_id (optional) |
||
| 1010 | * An option entry ID for more intelligent processing. Defaults to null. |
||
| 1011 | * @param bool $truncate |
||
| 1012 | * Defaults to false |
||
| 1013 | * @param string $defaultValue (optional) |
||
| 1014 | * The value to use when no plain text representation of the field's data |
||
| 1015 | * can be made. Defaults to null. |
||
| 1016 | * @return string |
||
| 1017 | * The readable text summary of the values of this field instance. |
||
| 1018 | */ |
||
| 1019 | public function prepareReadableValue($data, $entry_id = null, $truncate = false, $defaultValue = null) |
||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * Format this field value for complete display as text (string). By default, |
||
| 1040 | * it looks for the 'value' key in the $data array and strip tags from it. |
||
| 1041 | * |
||
| 1042 | * @since Symphony 2.5.0 |
||
| 1043 | * @param array $data |
||
| 1044 | * an associative array of data for this string. At minimum this requires a |
||
| 1045 | * key of 'value'. |
||
| 1046 | * @param integer $entry_id (optional) |
||
| 1047 | * An option entry ID for more intelligent processing. defaults to null |
||
| 1048 | * @return string |
||
| 1049 | * the complete text representation of the values of this field instance. |
||
| 1050 | */ |
||
| 1051 | public function prepareTextValue($data, $entry_id = null) |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Format this field value for display in the publish index tables. |
||
| 1058 | * |
||
| 1059 | * Since Symphony 2.5.0, this function will call `Field::prepareReadableValue` |
||
| 1060 | * in order to get the field's human readable value. |
||
| 1061 | * |
||
| 1062 | * @param array $data |
||
| 1063 | * an associative array of data for this string. At minimum this requires a |
||
| 1064 | * key of 'value'. |
||
| 1065 | * @param XMLElement $link (optional) |
||
| 1066 | * an XML link structure to append the content of this to provided it is not |
||
| 1067 | * null. it defaults to null. |
||
| 1068 | * @param integer $entry_id (optional) |
||
| 1069 | * An option entry ID for more intelligent processing. defaults to null |
||
| 1070 | * @return string |
||
| 1071 | * the formatted string summary of the values of this field instance. |
||
| 1072 | */ |
||
| 1073 | public function prepareTableValue($data, XMLElement $link = null, $entry_id = null) |
||
| 1085 | |||
| 1086 | /** |
||
| 1087 | * This is general purpose factory method that makes it easier to create the |
||
| 1088 | * markup needed in order to create an Associations Drawer XMLElement. |
||
| 1089 | * |
||
| 1090 | * @since Symphony 2.5.0 |
||
| 1091 | * |
||
| 1092 | * @param string $value |
||
| 1093 | * The value to display in the link |
||
| 1094 | * @param Entry $e |
||
| 1095 | * The associated entry |
||
| 1096 | * @param array $parent_association |
||
| 1097 | * An array containing information about the association |
||
| 1098 | * @param string $prepopulate |
||
| 1099 | * A string containing prepopulate parameter to append to the association url |
||
| 1100 | * |
||
| 1101 | * @return XMLElement |
||
| 1102 | * The XMLElement must be a li node, since it will be added an ul node. |
||
| 1103 | */ |
||
| 1104 | public static function createAssociationsDrawerXMLElement( |
||
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Display the publish panel for this field. The display panel is the |
||
| 1121 | * interface shown to Authors that allow them to input data into this |
||
| 1122 | * field for an `Entry`. |
||
| 1123 | * |
||
| 1124 | * @param XMLElement $wrapper |
||
| 1125 | * the XML element to append the html defined user interface to this |
||
| 1126 | * field. |
||
| 1127 | * @param array $data (optional) |
||
| 1128 | * any existing data that has been supplied for this field instance. |
||
| 1129 | * this is encoded as an array of columns, each column maps to an |
||
| 1130 | * array of row indexes to the contents of that column. this defaults |
||
| 1131 | * to null. |
||
| 1132 | * @param mixed $flagWithError (optional) |
||
| 1133 | * flag with error defaults to null. |
||
| 1134 | * @param string $fieldnamePrefix (optional) |
||
| 1135 | * the string to be prepended to the display of the name of this field. |
||
| 1136 | * this defaults to null. |
||
| 1137 | * @param string $fieldnamePostfix (optional) |
||
| 1138 | * the string to be appended to the display of the name of this field. |
||
| 1139 | * this defaults to null. |
||
| 1140 | * @param integer $entry_id (optional) |
||
| 1141 | * the entry id of this field. this defaults to null. |
||
| 1142 | */ |
||
| 1143 | public function displayPublishPanel( |
||
| 1152 | |||
| 1153 | /** |
||
| 1154 | * Check the field data that has been posted from a form. This will set the |
||
| 1155 | * input message to the error message or to null if there is none. Any existing |
||
| 1156 | * message value will be overwritten. |
||
| 1157 | * |
||
| 1158 | * @param array $data |
||
| 1159 | * the input data to check. |
||
| 1160 | * @param string $message |
||
| 1161 | * the place to set any generated error message. any previous value for |
||
| 1162 | * this variable will be overwritten. |
||
| 1163 | * @param integer $entry_id (optional) |
||
| 1164 | * the optional id of this field entry instance. this defaults to null. |
||
| 1165 | * @return integer |
||
| 1166 | * `self::__MISSING_FIELDS__` if there are any missing required fields, |
||
| 1167 | * `self::__OK__` otherwise. |
||
| 1168 | */ |
||
| 1169 | public function checkPostFieldData($data, &$message, $entry_id = null) |
||
| 1183 | |||
| 1184 | /** |
||
| 1185 | * Process the raw field data. |
||
| 1186 | * |
||
| 1187 | * @param mixed $data |
||
| 1188 | * post data from the entry form |
||
| 1189 | * @param integer $status |
||
| 1190 | * the status code resultant from processing the data. |
||
| 1191 | * @param string $message |
||
| 1192 | * the place to set any generated error message. any previous value for |
||
| 1193 | * this variable will be overwritten. |
||
| 1194 | * @param boolean $simulate (optional) |
||
| 1195 | * true if this will tell the CF's to simulate data creation, false |
||
| 1196 | * otherwise. this defaults to false. this is important if clients |
||
| 1197 | * will be deleting or adding data outside of the main entry object |
||
| 1198 | * commit function. |
||
| 1199 | * @param mixed $entry_id (optional) |
||
| 1200 | * the current entry. defaults to null. |
||
| 1201 | * @return array |
||
| 1202 | * the processed field data. |
||
| 1203 | */ |
||
| 1204 | public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null) |
||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Returns the types of filter suggestion this field supports. |
||
| 1215 | * The array may contain the following values: |
||
| 1216 | * |
||
| 1217 | * - `entry` for searching entries in the current section |
||
| 1218 | * - `association` for searching entries in associated sections |
||
| 1219 | * - `static` for searching static values |
||
| 1220 | * - `date` for searching in a calendar |
||
| 1221 | * - `parameters` for searching in parameters |
||
| 1222 | * |
||
| 1223 | * If the date type is set, only the calendar will be shown in the suggestion dropdown. |
||
| 1224 | * |
||
| 1225 | * @since Symphony 2.6.0 |
||
| 1226 | * @return array |
||
| 1227 | */ |
||
| 1228 | public function fetchSuggestionTypes() |
||
| 1232 | |||
| 1233 | /** |
||
| 1234 | * Display the default data-source filter panel. |
||
| 1235 | * |
||
| 1236 | * @param XMLElement $wrapper |
||
| 1237 | * the input XMLElement to which the display of this will be appended. |
||
| 1238 | * @param mixed $data (optional) |
||
| 1239 | * the input data. this defaults to null. |
||
| 1240 | * @param null $errors |
||
| 1241 | * the input error collection. this defaults to null. |
||
| 1242 | * @param string $fieldnamePrefix |
||
| 1243 | * the prefix to apply to the display of this. |
||
| 1244 | * @param string $fieldnamePostfix |
||
| 1245 | * the suffix to apply to the display of this. |
||
| 1246 | * @throws InvalidArgumentException |
||
| 1247 | */ |
||
| 1248 | public function displayDatasourceFilterPanel( |
||
| 1271 | |||
| 1272 | /** |
||
| 1273 | * Inserts tags at the bottom of the filter panel |
||
| 1274 | * |
||
| 1275 | * @since Symphony 2.6.0 |
||
| 1276 | * @param XMLElement $wrapper |
||
| 1277 | */ |
||
| 1278 | public function displayFilteringOptions(XMLElement &$wrapper) |
||
| 1304 | |||
| 1305 | /** |
||
| 1306 | * Returns the keywords that this field supports for filtering. Note |
||
| 1307 | * that no filter will do a simple 'straight' match on the value. |
||
| 1308 | * |
||
| 1309 | * @since Symphony 2.6.0 |
||
| 1310 | * @return array |
||
| 1311 | */ |
||
| 1312 | public function fetchFilterableOperators() |
||
| 1337 | |||
| 1338 | /** |
||
| 1339 | * Default accessor for the includable elements of this field. This array |
||
| 1340 | * will populate the `Datasource` included elements. Fields that have |
||
| 1341 | * different modes will override this and add new items to the array. |
||
| 1342 | * The Symphony convention is element_name : mode. Modes allow Fields to |
||
| 1343 | * output different XML in datasources. |
||
| 1344 | * |
||
| 1345 | * @return array |
||
| 1346 | * the array of includable elements from this field. |
||
| 1347 | */ |
||
| 1348 | public function fetchIncludableElements() |
||
| 1352 | |||
| 1353 | /** |
||
| 1354 | * Construct the SQL statement fragments to use to retrieve the data of this |
||
| 1355 | * field when utilized as a data source. |
||
| 1356 | * |
||
| 1357 | * @see toolkit.Datasource#__determineFilterType |
||
| 1358 | * @param array $data |
||
| 1359 | * An array of the data that contains the values for the filter as specified |
||
| 1360 | * in the datasource editor. The value that is entered in the datasource editor |
||
| 1361 | * is made into an array by using + or , to separate the filter. |
||
| 1362 | * @param string $joins |
||
| 1363 | * A string containing any table joins for the current SQL fragment. By default |
||
| 1364 | * Datasources will always join to the `tbl_entries` table, which has an alias of |
||
| 1365 | * `e`. This parameter is passed by reference. |
||
| 1366 | * @param string $where |
||
| 1367 | * A string containing the WHERE conditions for the current SQL fragment. This |
||
| 1368 | * is passed by reference and is expected to be used to add additional conditions |
||
| 1369 | * specific to this field |
||
| 1370 | * @param boolean $andOperation (optional) |
||
| 1371 | * This parameter defines whether the `$data` provided should be treated as |
||
| 1372 | * AND or OR conditions. This parameter will be set to true if $data used a |
||
| 1373 | * + to separate the values, otherwise it will be false. It is false by default. |
||
| 1374 | * @return boolean |
||
| 1375 | * True if the construction of the SQL was successful, false otherwise. |
||
| 1376 | */ |
||
| 1377 | public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false) |
||
| 1423 | |||
| 1424 | /** |
||
| 1425 | * Test whether the input string is a regular expression, by searching |
||
| 1426 | * for the prefix of `regexp:` or `not-regexp:` in the given `$string`. |
||
| 1427 | * |
||
| 1428 | * @param string $string |
||
| 1429 | * The string to test. |
||
| 1430 | * @return boolean |
||
| 1431 | * True if the string is prefixed with `regexp:` or `not-regexp:`, false otherwise. |
||
| 1432 | */ |
||
| 1433 | protected static function isFilterRegex($string) |
||
| 1441 | |||
| 1442 | /** |
||
| 1443 | * Builds a basic REGEXP statement given a `$filter`. This function supports |
||
| 1444 | * `regexp:` or `not-regexp:`. Users should keep in mind this function |
||
| 1445 | * uses MySQL patterns, not the usual PHP patterns, the syntax between these |
||
| 1446 | * flavours differs at times. |
||
| 1447 | * |
||
| 1448 | * @since Symphony 2.3 |
||
| 1449 | * @link http://dev.mysql.com/doc/refman/5.5/en/regexp.html |
||
| 1450 | * @param string $filter |
||
| 1451 | * The full filter, eg. `regexp: ^[a-d]` |
||
| 1452 | * @param array $columns |
||
| 1453 | * The array of columns that need the given `$filter` applied to. The conditions |
||
| 1454 | * will be added using `OR`. |
||
| 1455 | * @param string $joins |
||
| 1456 | * A string containing any table joins for the current SQL fragment. By default |
||
| 1457 | * Datasources will always join to the `tbl_entries` table, which has an alias of |
||
| 1458 | * `e`. This parameter is passed by reference. |
||
| 1459 | * @param string $where |
||
| 1460 | * A string containing the WHERE conditions for the current SQL fragment. This |
||
| 1461 | * is passed by reference and is expected to be used to add additional conditions |
||
| 1462 | * specific to this field |
||
| 1463 | */ |
||
| 1464 | public function buildRegexSQL($filter, array $columns, &$joins, &$where) |
||
| 1499 | |||
| 1500 | /** |
||
| 1501 | * Clean the input value using html entity encode and the database specific |
||
| 1502 | * clean methods. |
||
| 1503 | * |
||
| 1504 | * @param mixed $value |
||
| 1505 | * the value to clean. |
||
| 1506 | * @return string |
||
| 1507 | * the cleaned value. |
||
| 1508 | */ |
||
| 1509 | public function cleanValue($value) |
||
| 1513 | |||
| 1514 | /** |
||
| 1515 | * Build the SQL command to append to the default query to enable |
||
| 1516 | * sorting of this field. By default this will sort the results by |
||
| 1517 | * the entry id in ascending order. |
||
| 1518 | * |
||
| 1519 | * @param string $joins |
||
| 1520 | * the join element of the query to append the custom join sql to. |
||
| 1521 | * @param string $where |
||
| 1522 | * the where condition of the query to append to the existing where clause. |
||
| 1523 | * @param string $sort |
||
| 1524 | * the existing sort component of the sql query to append the custom |
||
| 1525 | * sort sql code to. |
||
| 1526 | * @param string $order (optional) |
||
| 1527 | * an optional sorting direction. this defaults to ascending. if this |
||
| 1528 | * is declared either 'random' or 'rand' then a random sort is applied. |
||
| 1529 | */ |
||
| 1530 | View Code Duplication | public function buildSortingSQL(&$joins, &$where, &$sort, $order = 'ASC') |
|
| 1539 | |||
| 1540 | /** |
||
| 1541 | * Default implementation of record grouping. This default implementation |
||
| 1542 | * will throw an `Exception`. Thus, clients must overload this method |
||
| 1543 | * for grouping to be successful. |
||
| 1544 | * |
||
| 1545 | * @throws Exception |
||
| 1546 | * @param array $records |
||
| 1547 | * the records to group. |
||
| 1548 | */ |
||
| 1549 | public function groupRecords($records) |
||
| 1556 | |||
| 1557 | /** |
||
| 1558 | * Function to format this field if it chosen in a data-source to be |
||
| 1559 | * output as a parameter in the XML. |
||
| 1560 | * |
||
| 1561 | * Since Symphony 2.5.0, it will defaults to `prepareReadableValue` return value. |
||
| 1562 | * |
||
| 1563 | * @param array $data |
||
| 1564 | * The data for this field from it's `tbl_entry_data_{id}` table |
||
| 1565 | * @param integer $entry_id |
||
| 1566 | * The optional id of this field entry instance |
||
| 1567 | * @return string|array |
||
| 1568 | * The formatted value to be used as the parameter. Note that this can be |
||
| 1569 | * an array or a string. When returning multiple values use array, otherwise |
||
| 1570 | * use string. |
||
| 1571 | */ |
||
| 1572 | public function getParameterPoolValue(array $data, $entry_id = null) |
||
| 1576 | |||
| 1577 | /** |
||
| 1578 | * Append the formatted XML output of this field as utilized as a data source. |
||
| 1579 | * |
||
| 1580 | * Since Symphony 2.5.0, it will defaults to `prepareReadableValue` return value. |
||
| 1581 | * |
||
| 1582 | * @param XMLElement $wrapper |
||
| 1583 | * the XML element to append the XML representation of this to. |
||
| 1584 | * @param array $data |
||
| 1585 | * the current set of values for this field. the values are structured as |
||
| 1586 | * for displayPublishPanel. |
||
| 1587 | * @param boolean $encode (optional) |
||
| 1588 | * flag as to whether this should be html encoded prior to output. this |
||
| 1589 | * defaults to false. |
||
| 1590 | * @param string $mode |
||
| 1591 | * A field can provide ways to output this field's data. For instance a mode |
||
| 1592 | * could be 'items' or 'full' and then the function would display the data |
||
| 1593 | * in a different way depending on what was selected in the datasource |
||
| 1594 | * included elements. |
||
| 1595 | * @param integer $entry_id (optional) |
||
| 1596 | * the identifier of this field entry instance. defaults to null. |
||
| 1597 | */ |
||
| 1598 | public function appendFormattedElement( |
||
| 1609 | |||
| 1610 | /** |
||
| 1611 | * The default method for constructing the example form markup containing this |
||
| 1612 | * field when utilized as part of an event. This displays in the event documentation |
||
| 1613 | * and serves as a basic guide for how markup should be constructed on the |
||
| 1614 | * `Frontend` to save this field |
||
| 1615 | * |
||
| 1616 | * @throws InvalidArgumentException |
||
| 1617 | * @return XMLElement |
||
| 1618 | * a label widget containing the formatted field element name of this. |
||
| 1619 | */ |
||
| 1620 | View Code Duplication | public function getExampleFormMarkup() |
|
| 1627 | |||
| 1628 | /** |
||
| 1629 | * Commit the settings of this field from the section editor to |
||
| 1630 | * create an instance of this field in a section. |
||
| 1631 | * |
||
| 1632 | * @return boolean |
||
| 1633 | * true if the commit was successful, false otherwise. |
||
| 1634 | */ |
||
| 1635 | public function commit() |
||
| 1659 | |||
| 1660 | /** |
||
| 1661 | * The default field table construction method. This constructs the bare |
||
| 1662 | * minimum set of columns for a valid field table. Subclasses are expected |
||
| 1663 | * to overload this method to create a table structure that contains |
||
| 1664 | * additional columns to store the specific data created by the field. |
||
| 1665 | * |
||
| 1666 | * @throws DatabaseException |
||
| 1667 | * @return boolean |
||
| 1668 | */ |
||
| 1669 | public function createTable() |
||
| 1682 | |||
| 1683 | /** |
||
| 1684 | * Remove the entry data of this field from the database. |
||
| 1685 | * |
||
| 1686 | * @param integer|array $entry_id |
||
| 1687 | * the ID of the entry, or an array of entry ID's to delete. |
||
| 1688 | * @param array $data (optional) |
||
| 1689 | * The entry data provided for fields to do additional cleanup |
||
| 1690 | * This is an optional argument and defaults to null. |
||
| 1691 | * @throws DatabaseException |
||
| 1692 | * @return boolean |
||
| 1693 | * Returns true after the cleanup has been completed |
||
| 1694 | */ |
||
| 1695 | public function entryDataCleanup($entry_id, $data = null) |
||
| 1701 | |||
| 1702 | /** |
||
| 1703 | * Accessor to the associated entry search value for this field |
||
| 1704 | * instance. This default implementation simply returns `$data` |
||
| 1705 | * |
||
| 1706 | * @param array $data |
||
| 1707 | * the data from which to construct the associated search entry value, this is usually |
||
| 1708 | * Entry with the `$parent_entry_id` value's data. |
||
| 1709 | * @param integer $field_id (optional) |
||
| 1710 | * the ID of the field that is the parent in the relationship |
||
| 1711 | * @param integer $parent_entry_id (optional) |
||
| 1712 | * the ID of the entry from the parent section in the relationship |
||
| 1713 | * @return array|string |
||
| 1714 | * Defaults to returning `$data`, but overriding implementation should return |
||
| 1715 | * a string |
||
| 1716 | */ |
||
| 1717 | public function fetchAssociatedEntrySearchValue($data, $field_id = null, $parent_entry_id = null) |
||
| 1721 | |||
| 1722 | /** |
||
| 1723 | * Fetch the count of the associated entries given a `$value`. |
||
| 1724 | * |
||
| 1725 | * @see toolkit.Field#fetchAssociatedEntrySearchValue() |
||
| 1726 | * @param mixed $value |
||
| 1727 | * the value to find the associated entry count for, this usually comes from |
||
| 1728 | * the `fetchAssociatedEntrySearchValue` function. |
||
| 1729 | * @return void|integer |
||
| 1730 | * this default implementation returns void. overriding implementations should |
||
| 1731 | * return an integer. |
||
| 1732 | */ |
||
| 1733 | public function fetchAssociatedEntryCount($value) |
||
| 1736 | |||
| 1737 | /** |
||
| 1738 | * Find related entries from a linking field's data table. Default implementation uses |
||
| 1739 | * column names `entry_id` and `relation_id` as with the Select Box Link |
||
| 1740 | * |
||
| 1741 | * @since Symphony 2.5.0 |
||
| 1742 | * |
||
| 1743 | * @param integer $entry_id |
||
| 1744 | * @param integer $parent_field_id |
||
| 1745 | * @return array |
||
| 1746 | */ |
||
| 1747 | View Code Duplication | public function findRelatedEntries($entry_id, $parent_field_id) |
|
| 1762 | |||
| 1763 | /** |
||
| 1764 | * Find related entries for the current field. Default implementation uses |
||
| 1765 | * column names `entry_id` and `relation_id` as with the Select Box Link |
||
| 1766 | * |
||
| 1767 | * @since Symphony 2.5.0 |
||
| 1768 | * |
||
| 1769 | * @param integer $field_id |
||
| 1770 | * @param integer $entry_id |
||
| 1771 | * @return array |
||
| 1772 | */ |
||
| 1773 | View Code Duplication | public function findParentRelatedEntries($field_id, $entry_id) |
|
| 1788 | } |
||
| 1789 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: