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 EE_Base_Class 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 EE_Base_Class, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | abstract class EE_Base_Class |
||
| 27 | { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * This is an array of the original properties and values provided during construction |
||
| 31 | * of this model object. (keys are model field names, values are their values). |
||
| 32 | * This list is important to remember so that when we are merging data from the db, we know |
||
| 33 | * which values to override and which to not override. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $_props_n_values_provided_in_constructor; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Timezone |
||
| 41 | * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
||
| 42 | * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
||
| 43 | * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
||
| 44 | * access to it. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $_timezone; |
||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | /** |
||
| 53 | * date format |
||
| 54 | * pattern or format for displaying dates |
||
| 55 | * |
||
| 56 | * @var string $_dt_frmt |
||
| 57 | */ |
||
| 58 | protected $_dt_frmt; |
||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * time format |
||
| 64 | * pattern or format for displaying time |
||
| 65 | * |
||
| 66 | * @var string $_tm_frmt |
||
| 67 | */ |
||
| 68 | protected $_tm_frmt; |
||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * This property is for holding a cached array of object properties indexed by property name as the key. |
||
| 74 | * The purpose of this is for setting a cache on properties that may have calculated values after a |
||
| 75 | * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
||
| 76 | * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $_cached_properties = array(); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * An array containing keys of the related model, and values are either an array of related mode objects or a |
||
| 84 | * single |
||
| 85 | * related model object. see the model's _model_relations. The keys should match those specified. And if the |
||
| 86 | * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
||
| 87 | * all others have an array) |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $_model_relations = array(); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
||
| 95 | * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
||
| 96 | * |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | protected $_fields = array(); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var boolean indicating whether or not this model object is intended to ever be saved |
||
| 103 | * For example, we might create model objects intended to only be used for the duration |
||
| 104 | * of this request and to be thrown away, and if they were accidentally saved |
||
| 105 | * it would be a bug. |
||
| 106 | */ |
||
| 107 | protected $_allow_persist = true; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var boolean indicating whether or not this model object's properties have changed since construction |
||
| 111 | */ |
||
| 112 | protected $_has_changes = false; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var EEM_Base |
||
| 116 | */ |
||
| 117 | protected $_model; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var MoneyFactory |
||
| 121 | */ |
||
| 122 | protected $money_factory; |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
||
| 127 | * play nice |
||
| 128 | * |
||
| 129 | * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
||
| 130 | * layer of the model's _fields array, (eg, EVT_ID, |
||
| 131 | * TXN_amount, QST_name, etc) and values are their values |
||
| 132 | * @param boolean $bydb a flag for setting if the class is instantiated by the |
||
| 133 | * corresponding db model or not. |
||
| 134 | * @param string $timezone indicate what timezone you want any datetime fields to |
||
| 135 | * be in when instantiating a EE_Base_Class object. |
||
| 136 | * @param array $date_formats An array of date formats to set on construct where first |
||
| 137 | * value is the date_format and second value is the time |
||
| 138 | * format. |
||
| 139 | * @throws InvalidArgumentException |
||
| 140 | * @throws InvalidInterfaceException |
||
| 141 | * @throws InvalidDataTypeException |
||
| 142 | * @throws EE_Error |
||
| 143 | */ |
||
| 144 | protected function __construct( |
||
| 207 | |||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * Gets whether or not this model object is allowed to persist/be saved to the database. |
||
| 212 | * |
||
| 213 | * @return boolean |
||
| 214 | */ |
||
| 215 | public function allow_persist() |
||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * Sets whether or not this model object should be allowed to be saved to the DB. |
||
| 224 | * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
||
| 225 | * you got new information that somehow made you change your mind. |
||
| 226 | * |
||
| 227 | * @param boolean $allow_persist |
||
| 228 | * @return boolean |
||
| 229 | */ |
||
| 230 | public function set_allow_persist($allow_persist) |
||
| 234 | |||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * Gets the field's original value when this object was constructed during this request. |
||
| 239 | * This can be helpful when determining if a model object has changed or not |
||
| 240 | * |
||
| 241 | * @param string $field_name |
||
| 242 | * @return mixed|null |
||
| 243 | * @throws EE_Error |
||
| 244 | */ |
||
| 245 | public function get_original($field_name) |
||
| 255 | |||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * @param EE_Base_Class $obj |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function get_class($obj) |
||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * Overrides parent because parent expects old models. |
||
| 270 | * This also doesn't do any validation, and won't work for serialized arrays |
||
| 271 | * |
||
| 272 | * @param string $field_name |
||
| 273 | * @param mixed $field_value |
||
| 274 | * @param bool $use_default |
||
| 275 | * @throws InvalidArgumentException |
||
| 276 | * @throws InvalidInterfaceException |
||
| 277 | * @throws InvalidDataTypeException |
||
| 278 | * @throws EE_Error |
||
| 279 | */ |
||
| 280 | public function set($field_name, $field_value, $use_default = false) |
||
| 356 | |||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * This sets the field value on the db column if it exists for the given $column_name or |
||
| 361 | * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
||
| 362 | * |
||
| 363 | * @see EE_message::get_column_value for related documentation on the necessity of this method. |
||
| 364 | * @param string $field_name Must be the exact column name. |
||
| 365 | * @param mixed $field_value The value to set. |
||
| 366 | * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
||
| 367 | * @throws EE_Error |
||
| 368 | */ |
||
| 369 | public function set_field_or_extra_meta($field_name, $field_value) |
||
| 380 | |||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * This retrieves the value of the db column set on this class or if that's not present |
||
| 385 | * it will attempt to retrieve from extra_meta if found. |
||
| 386 | * Example Usage: |
||
| 387 | * Via EE_Message child class: |
||
| 388 | * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
||
| 389 | * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
||
| 390 | * also have additional main fields specific to the messenger. The system accommodates those extra |
||
| 391 | * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
||
| 392 | * value for those extra fields dynamically via the EE_message object. |
||
| 393 | * |
||
| 394 | * @param string $field_name expecting the fully qualified field name. |
||
| 395 | * @return mixed|null value for the field if found. null if not found. |
||
| 396 | * @throws EE_Error |
||
| 397 | */ |
||
| 398 | public function get_field_or_extra_meta($field_name) |
||
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
||
| 412 | * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
||
| 413 | * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
||
| 414 | * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
||
| 415 | * |
||
| 416 | * @access public |
||
| 417 | * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
||
| 418 | * @return void |
||
| 419 | * @throws InvalidArgumentException |
||
| 420 | * @throws InvalidInterfaceException |
||
| 421 | * @throws InvalidDataTypeException |
||
| 422 | * @throws EE_Error |
||
| 423 | */ |
||
| 424 | public function set_timezone($timezone = '') |
||
| 440 | |||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * This just returns whatever is set for the current timezone. |
||
| 445 | * |
||
| 446 | * @access public |
||
| 447 | * @return string timezone string |
||
| 448 | */ |
||
| 449 | public function get_timezone() |
||
| 453 | |||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * This sets the internal date format to what is sent in to be used as the new default for the class |
||
| 458 | * internally instead of wp set date format options |
||
| 459 | * |
||
| 460 | * @since 4.6 |
||
| 461 | * @param string $format should be a format recognizable by PHP date() functions. |
||
| 462 | */ |
||
| 463 | public function set_date_format($format) |
||
| 469 | |||
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * This sets the internal time format string to what is sent in to be used as the new default for the |
||
| 474 | * class internally instead of wp set time format options. |
||
| 475 | * |
||
| 476 | * @since 4.6 |
||
| 477 | * @param string $format should be a format recognizable by PHP date() functions. |
||
| 478 | */ |
||
| 479 | public function set_time_format($format) |
||
| 485 | |||
| 486 | |||
| 487 | |||
| 488 | /** |
||
| 489 | * This returns the current internal set format for the date and time formats. |
||
| 490 | * |
||
| 491 | * @param bool $full if true (default), then return the full format. Otherwise will return an array |
||
| 492 | * where the first value is the date format and the second value is the time format. |
||
| 493 | * @return mixed string|array |
||
| 494 | */ |
||
| 495 | public function get_format($full = true) |
||
| 499 | |||
| 500 | |||
| 501 | |||
| 502 | /** |
||
| 503 | * cache |
||
| 504 | * stores the passed model object on the current model object. |
||
| 505 | * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
||
| 506 | * |
||
| 507 | * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
||
| 508 | * 'Registration' associated with this model object |
||
| 509 | * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
||
| 510 | * that could be a payment or a registration) |
||
| 511 | * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
||
| 512 | * items which will be stored in an array on this object |
||
| 513 | * @throws EE_Error |
||
| 514 | * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
||
| 515 | * related thing, no array) |
||
| 516 | */ |
||
| 517 | public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
||
| 562 | |||
| 563 | |||
| 564 | |||
| 565 | /** |
||
| 566 | * For adding an item to the cached_properties property. |
||
| 567 | * |
||
| 568 | * @access protected |
||
| 569 | * @param string $fieldname the property item the corresponding value is for. |
||
| 570 | * @param mixed $value The value we are caching. |
||
| 571 | * @param string|null $cache_type |
||
| 572 | * @return void |
||
| 573 | * @throws EE_Error |
||
| 574 | */ |
||
| 575 | protected function _set_cached_property($fieldname, $value, $cache_type = null) |
||
| 582 | |||
| 583 | |||
| 584 | |||
| 585 | /** |
||
| 586 | * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
||
| 587 | * This also SETS the cache if we return the actual property! |
||
| 588 | * |
||
| 589 | * @param string $fieldname the name of the property we're trying to retrieve |
||
| 590 | * @param bool $pretty |
||
| 591 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
| 592 | * (in cases where the same property may be used for different outputs |
||
| 593 | * - i.e. datetime, money etc.) |
||
| 594 | * It can also accept certain pre-defined "schema" strings |
||
| 595 | * to define how to output the property. |
||
| 596 | * see the field's prepare_for_pretty_echoing for what strings can be used |
||
| 597 | * @return mixed whatever the value for the property is we're retrieving |
||
| 598 | * @throws EE_Error |
||
| 599 | */ |
||
| 600 | protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
||
| 614 | |||
| 615 | |||
| 616 | /** |
||
| 617 | * If the cache didn't fetch the needed item, this fetches it. |
||
| 618 | * @param string $fieldname |
||
| 619 | * @param bool $pretty |
||
| 620 | * @param string $extra_cache_ref |
||
| 621 | * @return mixed |
||
| 622 | * @throws EE_Error |
||
| 623 | */ |
||
| 624 | protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
||
| 639 | |||
| 640 | |||
| 641 | /** |
||
| 642 | * set timezone, formats, and output for EE_Datetime_Field objects |
||
| 643 | * |
||
| 644 | * @param \EE_Datetime_Field $datetime_field |
||
| 645 | * @param bool $pretty |
||
| 646 | * @param null $date_or_time |
||
| 647 | * @return void |
||
| 648 | * @throws InvalidArgumentException |
||
| 649 | * @throws InvalidInterfaceException |
||
| 650 | * @throws InvalidDataTypeException |
||
| 651 | * @throws EE_Error |
||
| 652 | */ |
||
| 653 | protected function _prepare_datetime_field( |
||
| 673 | |||
| 674 | |||
| 675 | |||
| 676 | /** |
||
| 677 | * This just takes care of clearing out the cached_properties |
||
| 678 | * |
||
| 679 | * @return void |
||
| 680 | */ |
||
| 681 | protected function _clear_cached_properties() |
||
| 685 | |||
| 686 | |||
| 687 | |||
| 688 | /** |
||
| 689 | * This just clears out ONE property if it exists in the cache |
||
| 690 | * |
||
| 691 | * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
||
| 692 | * @return void |
||
| 693 | */ |
||
| 694 | protected function _clear_cached_property($property_name) |
||
| 700 | |||
| 701 | |||
| 702 | |||
| 703 | /** |
||
| 704 | * Ensures that this related thing is a model object. |
||
| 705 | * |
||
| 706 | * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
||
| 707 | * @param string $model_name name of the related thing, eg 'Attendee', |
||
| 708 | * @return EE_Base_Class |
||
| 709 | * @throws EE_Error |
||
| 710 | */ |
||
| 711 | protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
||
| 719 | |||
| 720 | |||
| 721 | |||
| 722 | /** |
||
| 723 | * Forgets the cached model of the given relation Name. So the next time we request it, |
||
| 724 | * we will fetch it again from the database. (Handy if you know it's changed somehow). |
||
| 725 | * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
||
| 726 | * then only remove that one object from our cached array. Otherwise, clear the entire list |
||
| 727 | * |
||
| 728 | * @param string $relationName one of the keys in the _model_relations array on the model. |
||
| 729 | * Eg 'Registration' |
||
| 730 | * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
||
| 731 | * if you intend to use $clear_all = TRUE, or the relation only |
||
| 732 | * has 1 object anyways (ie, it's a BelongsToRelation) |
||
| 733 | * @param bool $clear_all This flags clearing the entire cache relation property if |
||
| 734 | * this is HasMany or HABTM. |
||
| 735 | * @throws EE_Error |
||
| 736 | * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
||
| 737 | * relation from all |
||
| 738 | */ |
||
| 739 | public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
||
| 815 | |||
| 816 | |||
| 817 | |||
| 818 | /** |
||
| 819 | * update_cache_after_object_save |
||
| 820 | * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
||
| 821 | * obtained after being saved to the db |
||
| 822 | * |
||
| 823 | * @param string $relationName - the type of object that is cached |
||
| 824 | * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
||
| 825 | * @param string $current_cache_id - the ID that was used when originally caching the object |
||
| 826 | * @return boolean TRUE on success, FALSE on fail |
||
| 827 | * @throws EE_Error |
||
| 828 | */ |
||
| 829 | public function update_cache_after_object_save( |
||
| 858 | |||
| 859 | |||
| 860 | |||
| 861 | /** |
||
| 862 | * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
||
| 863 | * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
||
| 864 | * |
||
| 865 | * @param string $relationName |
||
| 866 | * @return EE_Base_Class |
||
| 867 | */ |
||
| 868 | public function get_one_from_cache($relationName) |
||
| 878 | |||
| 879 | |||
| 880 | /** |
||
| 881 | * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
||
| 882 | * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
||
| 883 | * |
||
| 884 | * @param string $relationName |
||
| 885 | * @throws \ReflectionException |
||
| 886 | * @throws InvalidArgumentException |
||
| 887 | * @throws InvalidInterfaceException |
||
| 888 | * @throws InvalidDataTypeException |
||
| 889 | * @throws EE_Error |
||
| 890 | * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
||
| 891 | */ |
||
| 892 | public function get_all_from_cache($relationName) |
||
| 924 | |||
| 925 | |||
| 926 | |||
| 927 | /** |
||
| 928 | * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
||
| 929 | * matching the given query conditions. |
||
| 930 | * |
||
| 931 | * @param null $field_to_order_by What field is being used as the reference point. |
||
| 932 | * @param int $limit How many objects to return. |
||
| 933 | * @param array $query_params Any additional conditions on the query. |
||
| 934 | * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
||
| 935 | * you can indicate just the columns you want returned |
||
| 936 | * @return array|EE_Base_Class[] |
||
| 937 | * @throws EE_Error |
||
| 938 | */ |
||
| 939 | View Code Duplication | public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 951 | |||
| 952 | |||
| 953 | |||
| 954 | /** |
||
| 955 | * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
||
| 956 | * matching the given query conditions. |
||
| 957 | * |
||
| 958 | * @param null $field_to_order_by What field is being used as the reference point. |
||
| 959 | * @param int $limit How many objects to return. |
||
| 960 | * @param array $query_params Any additional conditions on the query. |
||
| 961 | * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
||
| 962 | * you can indicate just the columns you want returned |
||
| 963 | * @return array|EE_Base_Class[] |
||
| 964 | * @throws EE_Error |
||
| 965 | */ |
||
| 966 | View Code Duplication | public function previous_x( |
|
| 982 | |||
| 983 | |||
| 984 | |||
| 985 | /** |
||
| 986 | * Returns the next EE_Base_Class object in sequence from this object as found in the database |
||
| 987 | * matching the given query conditions. |
||
| 988 | * |
||
| 989 | * @param null $field_to_order_by What field is being used as the reference point. |
||
| 990 | * @param array $query_params Any additional conditions on the query. |
||
| 991 | * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
||
| 992 | * you can indicate just the columns you want returned |
||
| 993 | * @return array|EE_Base_Class |
||
| 994 | * @throws EE_Error |
||
| 995 | */ |
||
| 996 | View Code Duplication | public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1008 | |||
| 1009 | |||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
||
| 1013 | * matching the given query conditions. |
||
| 1014 | * |
||
| 1015 | * @param null $field_to_order_by What field is being used as the reference point. |
||
| 1016 | * @param array $query_params Any additional conditions on the query. |
||
| 1017 | * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
||
| 1018 | * you can indicate just the column you want returned |
||
| 1019 | * @return array|EE_Base_Class |
||
| 1020 | * @throws EE_Error |
||
| 1021 | */ |
||
| 1022 | View Code Duplication | public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1034 | |||
| 1035 | |||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Overrides parent because parent expects old models. |
||
| 1039 | * This also doesn't do any validation, and won't work for serialized arrays |
||
| 1040 | * |
||
| 1041 | * @param string $field_name |
||
| 1042 | * @param mixed $field_value_from_db |
||
| 1043 | * @throws EE_Error |
||
| 1044 | */ |
||
| 1045 | public function set_from_db($field_name, $field_value_from_db) |
||
| 1067 | |||
| 1068 | |||
| 1069 | |||
| 1070 | /** |
||
| 1071 | * verifies that the specified field is of the correct type |
||
| 1072 | * |
||
| 1073 | * @param string $field_name |
||
| 1074 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
| 1075 | * (in cases where the same property may be used for different outputs |
||
| 1076 | * - i.e. datetime, money etc.) |
||
| 1077 | * @return mixed |
||
| 1078 | * @throws EE_Error |
||
| 1079 | */ |
||
| 1080 | public function get($field_name, $extra_cache_ref = null) |
||
| 1084 | |||
| 1085 | |||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * This method simply returns the RAW unprocessed value for the given property in this class |
||
| 1089 | * |
||
| 1090 | * @param string $field_name A valid fieldname |
||
| 1091 | * @return mixed Whatever the raw value stored on the property is. |
||
| 1092 | * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
||
| 1093 | */ |
||
| 1094 | public function get_raw($field_name) |
||
| 1109 | |||
| 1110 | |||
| 1111 | |||
| 1112 | /** |
||
| 1113 | * This is used to return the internal DateTime object used for a field that is a |
||
| 1114 | * EE_Datetime_Field. |
||
| 1115 | * |
||
| 1116 | * @param string $field_name The field name retrieving the DateTime object. |
||
| 1117 | * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
||
| 1118 | * @throws EE_Error |
||
| 1119 | * an error is set and false returned. If the field IS an |
||
| 1120 | * EE_Datetime_Field and but the field value is null, then |
||
| 1121 | * just null is returned (because that indicates that likely |
||
| 1122 | * this field is nullable). |
||
| 1123 | */ |
||
| 1124 | public function get_DateTime_object($field_name) |
||
| 1144 | |||
| 1145 | |||
| 1146 | /** |
||
| 1147 | * Gets a Money object for the specified field. Please note that this should only be |
||
| 1148 | * used for fields corresponding to EE_Money_Fields, and it will always return a money object, |
||
| 1149 | * or else it will throw an exception. |
||
| 1150 | * |
||
| 1151 | * @param $field_name |
||
| 1152 | * @return Money |
||
| 1153 | * @throws InvalidEntityException |
||
| 1154 | * @throws EE_Error |
||
| 1155 | * @throws DomainException |
||
| 1156 | */ |
||
| 1157 | public function getMoneyObject($field_name) |
||
| 1182 | |||
| 1183 | |||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * To be used in template to immediately echo out the value, and format it for output. |
||
| 1187 | * Eg, should call stripslashes and whatnot before echoing |
||
| 1188 | * |
||
| 1189 | * @param string $field_name the name of the field as it appears in the DB |
||
| 1190 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
| 1191 | * (in cases where the same property may be used for different outputs |
||
| 1192 | * - i.e. datetime, money etc.) |
||
| 1193 | * @return void |
||
| 1194 | * @throws EE_Error |
||
| 1195 | */ |
||
| 1196 | public function e($field_name, $extra_cache_ref = null) |
||
| 1200 | |||
| 1201 | |||
| 1202 | |||
| 1203 | /** |
||
| 1204 | * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
||
| 1205 | * can be easily used as the value of form input. |
||
| 1206 | * |
||
| 1207 | * @param string $field_name |
||
| 1208 | * @return void |
||
| 1209 | * @throws EE_Error |
||
| 1210 | */ |
||
| 1211 | public function f($field_name) |
||
| 1215 | |||
| 1216 | /** |
||
| 1217 | * Same as `f()` but just returns the value instead of echoing it |
||
| 1218 | * @param string $field_name |
||
| 1219 | * @return string |
||
| 1220 | * @throws EE_Error |
||
| 1221 | */ |
||
| 1222 | public function get_f($field_name) |
||
| 1226 | |||
| 1227 | |||
| 1228 | |||
| 1229 | /** |
||
| 1230 | * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
||
| 1231 | * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
||
| 1232 | * to see what options are available. |
||
| 1233 | * @param string $field_name |
||
| 1234 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
| 1235 | * (in cases where the same property may be used for different outputs |
||
| 1236 | * - i.e. datetime, money etc.) |
||
| 1237 | * @return mixed |
||
| 1238 | * @throws EE_Error |
||
| 1239 | */ |
||
| 1240 | public function get_pretty($field_name, $extra_cache_ref = null) |
||
| 1244 | |||
| 1245 | |||
| 1246 | |||
| 1247 | /** |
||
| 1248 | * This simply returns the datetime for the given field name |
||
| 1249 | * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
||
| 1250 | * (and the equivalent e_date, e_time, e_datetime). |
||
| 1251 | * |
||
| 1252 | * @access protected |
||
| 1253 | * @param string $field_name Field on the instantiated EE_Base_Class child object |
||
| 1254 | * @param string $dt_frmt valid datetime format used for date |
||
| 1255 | * (if '' then we just use the default on the field, |
||
| 1256 | * if NULL we use the last-used format) |
||
| 1257 | * @param string $tm_frmt Same as above except this is for time format |
||
| 1258 | * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
||
| 1259 | * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
||
| 1260 | * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
||
| 1261 | * if field is not a valid dtt field, or void if echoing |
||
| 1262 | * @throws EE_Error |
||
| 1263 | */ |
||
| 1264 | protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
||
| 1277 | |||
| 1278 | |||
| 1279 | |||
| 1280 | /** |
||
| 1281 | * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
||
| 1282 | * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
||
| 1283 | * other echoes the pretty value for dtt) |
||
| 1284 | * |
||
| 1285 | * @param string $field_name name of model object datetime field holding the value |
||
| 1286 | * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
||
| 1287 | * @return string datetime value formatted |
||
| 1288 | * @throws EE_Error |
||
| 1289 | */ |
||
| 1290 | public function get_date($field_name, $format = '') |
||
| 1294 | |||
| 1295 | |||
| 1296 | |||
| 1297 | /** |
||
| 1298 | * @param $field_name |
||
| 1299 | * @param string $format |
||
| 1300 | * @throws EE_Error |
||
| 1301 | */ |
||
| 1302 | public function e_date($field_name, $format = '') |
||
| 1306 | |||
| 1307 | |||
| 1308 | |||
| 1309 | /** |
||
| 1310 | * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
||
| 1311 | * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
||
| 1312 | * other echoes the pretty value for dtt) |
||
| 1313 | * |
||
| 1314 | * @param string $field_name name of model object datetime field holding the value |
||
| 1315 | * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
||
| 1316 | * @return string datetime value formatted |
||
| 1317 | * @throws EE_Error |
||
| 1318 | */ |
||
| 1319 | public function get_time($field_name, $format = '') |
||
| 1323 | |||
| 1324 | |||
| 1325 | |||
| 1326 | /** |
||
| 1327 | * @param $field_name |
||
| 1328 | * @param string $format |
||
| 1329 | * @throws EE_Error |
||
| 1330 | */ |
||
| 1331 | public function e_time($field_name, $format = '') |
||
| 1335 | |||
| 1336 | |||
| 1337 | |||
| 1338 | /** |
||
| 1339 | * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
||
| 1340 | * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
||
| 1341 | * other echoes the pretty value for dtt) |
||
| 1342 | * |
||
| 1343 | * @param string $field_name name of model object datetime field holding the value |
||
| 1344 | * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
||
| 1345 | * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
||
| 1346 | * @return string datetime value formatted |
||
| 1347 | * @throws EE_Error |
||
| 1348 | */ |
||
| 1349 | public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
||
| 1353 | |||
| 1354 | |||
| 1355 | |||
| 1356 | /** |
||
| 1357 | * @param string $field_name |
||
| 1358 | * @param string $dt_frmt |
||
| 1359 | * @param string $tm_frmt |
||
| 1360 | * @throws EE_Error |
||
| 1361 | */ |
||
| 1362 | public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
||
| 1366 | |||
| 1367 | |||
| 1368 | /** |
||
| 1369 | * Get the i8ln value for a date using the WordPress @see date_i18n function. |
||
| 1370 | * |
||
| 1371 | * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
||
| 1372 | * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
||
| 1373 | * on the object will be used. |
||
| 1374 | * @return string Date and time string in set locale or false if no field exists for the given |
||
| 1375 | * @throws InvalidArgumentException |
||
| 1376 | * @throws InvalidInterfaceException |
||
| 1377 | * @throws InvalidDataTypeException |
||
| 1378 | * @throws EE_Error |
||
| 1379 | * field name. |
||
| 1380 | */ |
||
| 1381 | public function get_i18n_datetime($field_name, $format = '') |
||
| 1389 | |||
| 1390 | |||
| 1391 | |||
| 1392 | /** |
||
| 1393 | * This method validates whether the given field name is a valid field on the model object as well as it is of a |
||
| 1394 | * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
||
| 1395 | * thrown. |
||
| 1396 | * |
||
| 1397 | * @param string $field_name The field name being checked |
||
| 1398 | * @throws EE_Error |
||
| 1399 | * @return EE_Datetime_Field |
||
| 1400 | */ |
||
| 1401 | protected function _get_dtt_field_settings($field_name) |
||
| 1412 | |||
| 1413 | |||
| 1414 | |||
| 1415 | |||
| 1416 | /** |
||
| 1417 | * NOTE ABOUT BELOW: |
||
| 1418 | * These convenience date and time setters are for setting date and time independently. In other words you might |
||
| 1419 | * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
||
| 1420 | * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
||
| 1421 | * method and make sure you send the entire datetime value for setting. |
||
| 1422 | */ |
||
| 1423 | /** |
||
| 1424 | * sets the time on a datetime property |
||
| 1425 | * |
||
| 1426 | * @access protected |
||
| 1427 | * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
||
| 1428 | * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
||
| 1429 | * @throws EE_Error |
||
| 1430 | */ |
||
| 1431 | protected function _set_time_for($time, $fieldname) |
||
| 1435 | |||
| 1436 | |||
| 1437 | |||
| 1438 | /** |
||
| 1439 | * sets the date on a datetime property |
||
| 1440 | * |
||
| 1441 | * @access protected |
||
| 1442 | * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
||
| 1443 | * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
||
| 1444 | * @throws EE_Error |
||
| 1445 | */ |
||
| 1446 | protected function _set_date_for($date, $fieldname) |
||
| 1450 | |||
| 1451 | |||
| 1452 | /** |
||
| 1453 | * This takes care of setting a date or time independently on a given model object property. This method also |
||
| 1454 | * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
||
| 1455 | * |
||
| 1456 | * @access protected |
||
| 1457 | * @param string $what "T" for time, 'B' for both, 'D' for Date. |
||
| 1458 | * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
||
| 1459 | * @param string $fieldname the name of the field the date OR time is being set on (must match a |
||
| 1460 | * EE_Datetime_Field property) |
||
| 1461 | * @throws InvalidArgumentException |
||
| 1462 | * @throws InvalidInterfaceException |
||
| 1463 | * @throws InvalidDataTypeException |
||
| 1464 | * @throws EE_Error |
||
| 1465 | */ |
||
| 1466 | protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
||
| 1491 | |||
| 1492 | |||
| 1493 | /** |
||
| 1494 | * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
||
| 1495 | * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
||
| 1496 | * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
||
| 1497 | * that could lead to some unexpected results! |
||
| 1498 | * |
||
| 1499 | * @access public |
||
| 1500 | * @param string $field_name This is the name of the field on the object that contains the date/time |
||
| 1501 | * value being returned. |
||
| 1502 | * @param string $callback must match a valid method in this class (defaults to get_datetime) |
||
| 1503 | * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
||
| 1504 | * @param string $prepend You can include something to prepend on the timestamp |
||
| 1505 | * @param string $append You can include something to append on the timestamp |
||
| 1506 | * @throws InvalidArgumentException |
||
| 1507 | * @throws InvalidInterfaceException |
||
| 1508 | * @throws InvalidDataTypeException |
||
| 1509 | * @throws EE_Error |
||
| 1510 | * @return string timestamp |
||
| 1511 | */ |
||
| 1512 | public function display_in_my_timezone( |
||
| 1543 | |||
| 1544 | |||
| 1545 | |||
| 1546 | /** |
||
| 1547 | * Deletes this model object. |
||
| 1548 | * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
||
| 1549 | * override |
||
| 1550 | * `EE_Base_Class::_delete` NOT this class. |
||
| 1551 | * |
||
| 1552 | * @return boolean | int |
||
| 1553 | * @throws EE_Error |
||
| 1554 | */ |
||
| 1555 | public function delete() |
||
| 1579 | |||
| 1580 | |||
| 1581 | |||
| 1582 | /** |
||
| 1583 | * Calls the specific delete method for the instantiated class. |
||
| 1584 | * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
||
| 1585 | * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
||
| 1586 | * `EE_Base_Class::delete` |
||
| 1587 | * |
||
| 1588 | * @return bool|int |
||
| 1589 | * @throws EE_Error |
||
| 1590 | */ |
||
| 1591 | protected function _delete() |
||
| 1595 | |||
| 1596 | |||
| 1597 | |||
| 1598 | /** |
||
| 1599 | * Deletes this model object permanently from db (but keep in mind related models my block the delete and return an |
||
| 1600 | * error) |
||
| 1601 | * |
||
| 1602 | * @return bool | int |
||
| 1603 | * @throws EE_Error |
||
| 1604 | */ |
||
| 1605 | public function delete_permanently() |
||
| 1625 | |||
| 1626 | |||
| 1627 | |||
| 1628 | /** |
||
| 1629 | * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
||
| 1630 | * related model objects |
||
| 1631 | * |
||
| 1632 | * @throws EE_Error |
||
| 1633 | */ |
||
| 1634 | public function refresh_cache_of_related_objects() |
||
| 1654 | |||
| 1655 | |||
| 1656 | |||
| 1657 | /** |
||
| 1658 | * Saves this object to the database. An array may be supplied to set some values on this |
||
| 1659 | * object just before saving. |
||
| 1660 | * |
||
| 1661 | * @access public |
||
| 1662 | * @param array $set_cols_n_values keys are field names, values are their new values, |
||
| 1663 | * if provided during the save() method (often client code will change the fields' |
||
| 1664 | * values before calling save) |
||
| 1665 | * @throws EE_Error |
||
| 1666 | * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
||
| 1667 | * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
||
| 1668 | */ |
||
| 1669 | public function save($set_cols_n_values = array()) |
||
| 1779 | |||
| 1780 | |||
| 1781 | |||
| 1782 | /** |
||
| 1783 | * Updates the foreign key on related models objects pointing to this to have this model object's ID |
||
| 1784 | * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
||
| 1785 | * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
||
| 1786 | * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
||
| 1787 | * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the |
||
| 1788 | * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
||
| 1789 | * or not they exist in the DB (if they do, their DB records will be automatically updated) |
||
| 1790 | * |
||
| 1791 | * @return void |
||
| 1792 | * @throws EE_Error |
||
| 1793 | */ |
||
| 1794 | protected function _update_cached_related_model_objs_fks() |
||
| 1811 | |||
| 1812 | |||
| 1813 | |||
| 1814 | /** |
||
| 1815 | * Saves this model object and its NEW cached relations to the database. |
||
| 1816 | * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
||
| 1817 | * In order for that to work, we would need to mark model objects as dirty/clean... |
||
| 1818 | * because otherwise, there's a potential for infinite looping of saving |
||
| 1819 | * Saves the cached related model objects, and ensures the relation between them |
||
| 1820 | * and this object and properly setup |
||
| 1821 | * |
||
| 1822 | * @return int ID of new model object on save; 0 on failure+ |
||
| 1823 | * @throws EE_Error |
||
| 1824 | */ |
||
| 1825 | public function save_new_cached_related_model_objs() |
||
| 1861 | |||
| 1862 | |||
| 1863 | |||
| 1864 | /** |
||
| 1865 | * for getting a model while instantiated. |
||
| 1866 | * |
||
| 1867 | * @return EEM_Base | EEM_CPT_Base |
||
| 1868 | */ |
||
| 1869 | public function get_model() |
||
| 1880 | |||
| 1881 | |||
| 1882 | |||
| 1883 | /** |
||
| 1884 | * @param $props_n_values |
||
| 1885 | * @param $classname |
||
| 1886 | * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
||
| 1887 | * @throws EE_Error |
||
| 1888 | */ |
||
| 1889 | protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
||
| 1899 | |||
| 1900 | |||
| 1901 | |||
| 1902 | /** |
||
| 1903 | * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
||
| 1904 | * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
||
| 1905 | * primary key for the model AND it is not null, then we check the db. If there's a an object we return it. If not |
||
| 1906 | * we return false. |
||
| 1907 | * |
||
| 1908 | * @param array $props_n_values incoming array of properties and their values |
||
| 1909 | * @param string $classname the classname of the child class |
||
| 1910 | * @param null $timezone |
||
| 1911 | * @param array $date_formats incoming date_formats in an array where the first value is the |
||
| 1912 | * date_format and the second value is the time format |
||
| 1913 | * @return mixed (EE_Base_Class|bool) |
||
| 1914 | * @throws EE_Error |
||
| 1915 | */ |
||
| 1916 | protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
||
| 1953 | |||
| 1954 | |||
| 1955 | |||
| 1956 | /** |
||
| 1957 | * Gets the EEM_*_Model for this class |
||
| 1958 | * |
||
| 1959 | * @access public now, as this is more convenient |
||
| 1960 | * @param $classname |
||
| 1961 | * @param null $timezone |
||
| 1962 | * @throws EE_Error |
||
| 1963 | * @return EEM_Base |
||
| 1964 | */ |
||
| 1965 | protected static function _get_model($classname, $timezone = null) |
||
| 1982 | |||
| 1983 | |||
| 1984 | /** |
||
| 1985 | * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
||
| 1986 | * |
||
| 1987 | * @param string $model_classname |
||
| 1988 | * @param null $timezone |
||
| 1989 | * @return EEM_Base |
||
| 1990 | * @throws \ReflectionException |
||
| 1991 | * @throws InvalidArgumentException |
||
| 1992 | * @throws InvalidInterfaceException |
||
| 1993 | * @throws InvalidDataTypeException |
||
| 1994 | * @throws EE_Error |
||
| 1995 | */ |
||
| 1996 | protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
||
| 2003 | |||
| 2004 | |||
| 2005 | |||
| 2006 | /** |
||
| 2007 | * If a model name is provided (eg Registration), gets the model classname for that model. |
||
| 2008 | * Also works if a model class's classname is provided (eg EE_Registration). |
||
| 2009 | * |
||
| 2010 | * @param null $model_name |
||
| 2011 | * @return string like EEM_Attendee |
||
| 2012 | */ |
||
| 2013 | private static function _get_model_classname($model_name = null) |
||
| 2022 | |||
| 2023 | |||
| 2024 | |||
| 2025 | /** |
||
| 2026 | * returns the name of the primary key attribute |
||
| 2027 | * |
||
| 2028 | * @param null $classname |
||
| 2029 | * @throws EE_Error |
||
| 2030 | * @return string |
||
| 2031 | */ |
||
| 2032 | protected static function _get_primary_key_name($classname = null) |
||
| 2044 | |||
| 2045 | |||
| 2046 | |||
| 2047 | /** |
||
| 2048 | * Gets the value of the primary key. |
||
| 2049 | * If the object hasn't yet been saved, it should be whatever the model field's default was |
||
| 2050 | * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
||
| 2051 | * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
||
| 2052 | * |
||
| 2053 | * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
||
| 2054 | * @throws EE_Error |
||
| 2055 | */ |
||
| 2056 | public function ID() |
||
| 2066 | |||
| 2067 | |||
| 2068 | |||
| 2069 | /** |
||
| 2070 | * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
||
| 2071 | * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
||
| 2072 | * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
||
| 2073 | * |
||
| 2074 | * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
||
| 2075 | * @param string $relationName eg 'Events','Question',etc. |
||
| 2076 | * an attendee to a group, you also want to specify which role they |
||
| 2077 | * will have in that group. So you would use this parameter to |
||
| 2078 | * specify array('role-column-name'=>'role-id') |
||
| 2079 | * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
||
| 2080 | * allow you to further constrict the relation to being added. |
||
| 2081 | * However, keep in mind that the columns (keys) given must match a |
||
| 2082 | * column on the JOIN table and currently only the HABTM models |
||
| 2083 | * accept these additional conditions. Also remember that if an |
||
| 2084 | * exact match isn't found for these extra cols/val pairs, then a |
||
| 2085 | * NEW row is created in the join table. |
||
| 2086 | * @param null $cache_id |
||
| 2087 | * @throws EE_Error |
||
| 2088 | * @return EE_Base_Class the object the relation was added to |
||
| 2089 | */ |
||
| 2090 | public function _add_relation_to( |
||
| 2134 | |||
| 2135 | |||
| 2136 | |||
| 2137 | /** |
||
| 2138 | * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
||
| 2139 | * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
||
| 2140 | * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
||
| 2141 | * from the cache |
||
| 2142 | * |
||
| 2143 | * @param mixed $otherObjectModelObjectOrID |
||
| 2144 | * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
||
| 2145 | * to the DB yet |
||
| 2146 | * @param string $relationName |
||
| 2147 | * @param array $where_query |
||
| 2148 | * You can optionally include an array of key=>value pairs that allow you to further constrict the |
||
| 2149 | * relation to being added. However, keep in mind that the columns (keys) given must match a column |
||
| 2150 | * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
||
| 2151 | * remember that if an exact match isn't found for these extra cols/val pairs, then a NEW row is |
||
| 2152 | * created in the join table. |
||
| 2153 | * @return EE_Base_Class the relation was removed from |
||
| 2154 | * @throws EE_Error |
||
| 2155 | */ |
||
| 2156 | public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
||
| 2173 | |||
| 2174 | |||
| 2175 | |||
| 2176 | /** |
||
| 2177 | * Removes ALL the related things for the $relationName. |
||
| 2178 | * |
||
| 2179 | * @param string $relationName |
||
| 2180 | * @param array $where_query_params like EEM_Base::get_all's $query_params[0] (where conditions) |
||
| 2181 | * @return EE_Base_Class |
||
| 2182 | * @throws EE_Error |
||
| 2183 | */ |
||
| 2184 | public function _remove_relations($relationName, $where_query_params = array()) |
||
| 2201 | |||
| 2202 | |||
| 2203 | |||
| 2204 | /** |
||
| 2205 | * Gets all the related model objects of the specified type. Eg, if the current class if |
||
| 2206 | * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
||
| 2207 | * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
||
| 2208 | * because we want to get even deleted items etc. |
||
| 2209 | * |
||
| 2210 | * @param string $relationName key in the model's _model_relations array |
||
| 2211 | * @param array $query_params like EEM_Base::get_all |
||
| 2212 | * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary keys |
||
| 2213 | * @throws EE_Error |
||
| 2214 | * or might not be saved yet. Consider using EEM_Base::get_IDs() on these results if |
||
| 2215 | * you want IDs |
||
| 2216 | */ |
||
| 2217 | public function get_many_related($relationName, $query_params = array()) |
||
| 2244 | |||
| 2245 | |||
| 2246 | /** |
||
| 2247 | * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
||
| 2248 | * unless otherwise specified in the $query_params |
||
| 2249 | * |
||
| 2250 | * @param string $relation_name model_name like 'Event', or 'Registration' |
||
| 2251 | * @param array $query_params like EEM_Base::get_all's |
||
| 2252 | * @param string $field_to_count name of field to count by. By default, uses primary key |
||
| 2253 | * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
||
| 2254 | * that by the setting $distinct to TRUE; |
||
| 2255 | * @return int |
||
| 2256 | * @throws EE_Error |
||
| 2257 | */ |
||
| 2258 | public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
||
| 2262 | |||
| 2263 | |||
| 2264 | /** |
||
| 2265 | * Instead of getting the related model objects, simply sums up the values of the specified field. |
||
| 2266 | * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
||
| 2267 | * |
||
| 2268 | * @param string $relation_name model_name like 'Event', or 'Registration' |
||
| 2269 | * @param array $query_params like EEM_Base::get_all's |
||
| 2270 | * @param string $field_to_sum name of field to count by. |
||
| 2271 | * By default, uses primary key (which doesn't make much sense, so you should probably |
||
| 2272 | * change it) |
||
| 2273 | * @return int |
||
| 2274 | * @throws EE_Error |
||
| 2275 | */ |
||
| 2276 | public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
||
| 2280 | |||
| 2281 | |||
| 2282 | |||
| 2283 | /** |
||
| 2284 | * Gets the first (ie, one) related model object of the specified type. |
||
| 2285 | * |
||
| 2286 | * @param string $relationName key in the model's _model_relations array |
||
| 2287 | * @param array $query_params like EEM_Base::get_all |
||
| 2288 | * @return EE_Base_Class (not an array, a single object) |
||
| 2289 | * @throws EE_Error |
||
| 2290 | */ |
||
| 2291 | public function get_first_related($relationName, $query_params = array()) |
||
| 2327 | |||
| 2328 | |||
| 2329 | |||
| 2330 | /** |
||
| 2331 | * Does a delete on all related objects of type $relationName and removes |
||
| 2332 | * the current model object's relation to them. If they can't be deleted (because |
||
| 2333 | * of blocking related model objects) does nothing. If the related model objects are |
||
| 2334 | * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
||
| 2335 | * If this model object doesn't exist yet in the DB, just removes its related things |
||
| 2336 | * |
||
| 2337 | * @param string $relationName |
||
| 2338 | * @param array $query_params like EEM_Base::get_all's |
||
| 2339 | * @return int how many deleted |
||
| 2340 | * @throws EE_Error |
||
| 2341 | */ |
||
| 2342 | View Code Duplication | public function delete_related($relationName, $query_params = array()) |
|
| 2352 | |||
| 2353 | |||
| 2354 | |||
| 2355 | /** |
||
| 2356 | * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
||
| 2357 | * the current model object's relation to them. If they can't be deleted (because |
||
| 2358 | * of blocking related model objects) just does a soft delete on it instead, if possible. |
||
| 2359 | * If the related thing isn't a soft-deletable model object, this function is identical |
||
| 2360 | * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
||
| 2361 | * |
||
| 2362 | * @param string $relationName |
||
| 2363 | * @param array $query_params like EEM_Base::get_all's |
||
| 2364 | * @return int how many deleted (including those soft deleted) |
||
| 2365 | * @throws EE_Error |
||
| 2366 | */ |
||
| 2367 | View Code Duplication | public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2377 | |||
| 2378 | |||
| 2379 | |||
| 2380 | /** |
||
| 2381 | * is_set |
||
| 2382 | * Just a simple utility function children can use for checking if property exists |
||
| 2383 | * |
||
| 2384 | * @access public |
||
| 2385 | * @param string $field_name property to check |
||
| 2386 | * @return bool TRUE if existing,FALSE if not. |
||
| 2387 | */ |
||
| 2388 | public function is_set($field_name) |
||
| 2392 | |||
| 2393 | |||
| 2394 | |||
| 2395 | /** |
||
| 2396 | * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
||
| 2397 | * EE_Error exception if they don't |
||
| 2398 | * |
||
| 2399 | * @param mixed (string|array) $properties properties to check |
||
| 2400 | * @throws EE_Error |
||
| 2401 | * @return bool TRUE if existing, throw EE_Error if not. |
||
| 2402 | */ |
||
| 2403 | protected function _property_exists($properties) |
||
| 2421 | |||
| 2422 | |||
| 2423 | |||
| 2424 | /** |
||
| 2425 | * This simply returns an array of model fields for this object |
||
| 2426 | * |
||
| 2427 | * @return array |
||
| 2428 | * @throws EE_Error |
||
| 2429 | */ |
||
| 2430 | public function model_field_array() |
||
| 2440 | |||
| 2441 | |||
| 2442 | |||
| 2443 | /** |
||
| 2444 | * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
||
| 2445 | * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
||
| 2446 | * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
||
| 2447 | * requiring a plugin to extend the EE_Base_Class (which works fine is there's only 1 plugin, but when will that |
||
| 2448 | * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
||
| 2449 | * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
||
| 2450 | * was called, and an array of the original arguments passed to the function. Whatever their callback function |
||
| 2451 | * returns will be returned by this function. Example: in functions.php (or in a plugin): |
||
| 2452 | * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
||
| 2453 | * my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
||
| 2454 | * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
||
| 2455 | * return $previousReturnValue.$returnString; |
||
| 2456 | * } |
||
| 2457 | * require('EE_Answer.class.php'); |
||
| 2458 | * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
||
| 2459 | * echo $answer->my_callback('monkeys',100); |
||
| 2460 | * //will output "you called my_callback! and passed args:monkeys,100" |
||
| 2461 | * |
||
| 2462 | * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
||
| 2463 | * @param array $args array of original arguments passed to the function |
||
| 2464 | * @throws EE_Error |
||
| 2465 | * @return mixed whatever the plugin which calls add_filter decides |
||
| 2466 | */ |
||
| 2467 | public function __call($methodName, $args) |
||
| 2486 | |||
| 2487 | |||
| 2488 | /** |
||
| 2489 | * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
||
| 2490 | * A $previous_value can be specified in case there are many meta rows with the same key |
||
| 2491 | * |
||
| 2492 | * @param string $meta_key |
||
| 2493 | * @param mixed $meta_value |
||
| 2494 | * @param mixed $previous_value |
||
| 2495 | * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
||
| 2496 | * @throws InvalidArgumentException |
||
| 2497 | * @throws InvalidInterfaceException |
||
| 2498 | * @throws InvalidDataTypeException |
||
| 2499 | * @throws EE_Error |
||
| 2500 | * NOTE: if the values haven't changed, returns 0 |
||
| 2501 | */ |
||
| 2502 | public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
||
| 2523 | |||
| 2524 | |||
| 2525 | /** |
||
| 2526 | * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
||
| 2527 | * no other extra meta for this model object have the same key. Returns TRUE if the |
||
| 2528 | * extra meta row was entered, false if not |
||
| 2529 | * |
||
| 2530 | * @param string $meta_key |
||
| 2531 | * @param mixed $meta_value |
||
| 2532 | * @param boolean $unique |
||
| 2533 | * @return boolean |
||
| 2534 | * @throws InvalidArgumentException |
||
| 2535 | * @throws InvalidInterfaceException |
||
| 2536 | * @throws InvalidDataTypeException |
||
| 2537 | * @throws EE_Error |
||
| 2538 | */ |
||
| 2539 | public function add_extra_meta($meta_key, $meta_value, $unique = false) |
||
| 2566 | |||
| 2567 | |||
| 2568 | /** |
||
| 2569 | * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
||
| 2570 | * is specified, only deletes extra meta records with that value. |
||
| 2571 | * |
||
| 2572 | * @param string $meta_key |
||
| 2573 | * @param mixed $meta_value |
||
| 2574 | * @return int number of extra meta rows deleted |
||
| 2575 | * @throws InvalidArgumentException |
||
| 2576 | * @throws InvalidInterfaceException |
||
| 2577 | * @throws InvalidDataTypeException |
||
| 2578 | * @throws EE_Error |
||
| 2579 | */ |
||
| 2580 | public function delete_extra_meta($meta_key, $meta_value = null) |
||
| 2594 | |||
| 2595 | |||
| 2596 | |||
| 2597 | /** |
||
| 2598 | * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
||
| 2599 | * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
||
| 2600 | * You can specify $default is case you haven't found the extra meta |
||
| 2601 | * |
||
| 2602 | * @param string $meta_key |
||
| 2603 | * @param boolean $single |
||
| 2604 | * @param mixed $default if we don't find anything, what should we return? |
||
| 2605 | * @return mixed single value if $single; array if ! $single |
||
| 2606 | * @throws EE_Error |
||
| 2607 | */ |
||
| 2608 | public function get_extra_meta($meta_key, $single = false, $default = null) |
||
| 2636 | |||
| 2637 | |||
| 2638 | |||
| 2639 | /** |
||
| 2640 | * Returns a simple array of all the extra meta associated with this model object. |
||
| 2641 | * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
||
| 2642 | * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
||
| 2643 | * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
||
| 2644 | * If $one_of_each_key is false, it will return an array with the top-level keys being |
||
| 2645 | * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
||
| 2646 | * finally the extra meta's value as each sub-value. (eg |
||
| 2647 | * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
||
| 2648 | * |
||
| 2649 | * @param boolean $one_of_each_key |
||
| 2650 | * @return array |
||
| 2651 | * @throws EE_Error |
||
| 2652 | */ |
||
| 2653 | public function all_extra_meta_array($one_of_each_key = true) |
||
| 2676 | |||
| 2677 | |||
| 2678 | |||
| 2679 | /** |
||
| 2680 | * Gets a pretty nice displayable nice for this model object. Often overridden |
||
| 2681 | * |
||
| 2682 | * @return string |
||
| 2683 | * @throws EE_Error |
||
| 2684 | */ |
||
| 2685 | public function name() |
||
| 2701 | |||
| 2702 | |||
| 2703 | |||
| 2704 | /** |
||
| 2705 | * in_entity_map |
||
| 2706 | * Checks if this model object has been proven to already be in the entity map |
||
| 2707 | * |
||
| 2708 | * @return boolean |
||
| 2709 | * @throws EE_Error |
||
| 2710 | */ |
||
| 2711 | public function in_entity_map() |
||
| 2720 | |||
| 2721 | |||
| 2722 | |||
| 2723 | /** |
||
| 2724 | * refresh_from_db |
||
| 2725 | * Makes sure the fields and values on this model object are in-sync with what's in the database. |
||
| 2726 | * |
||
| 2727 | * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
||
| 2728 | * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
||
| 2729 | */ |
||
| 2730 | public function refresh_from_db() |
||
| 2752 | |||
| 2753 | |||
| 2754 | /** |
||
| 2755 | * Gets the money field's amount in subunits (and if the currency has no subunits, gets it in the main units) |
||
| 2756 | * |
||
| 2757 | * @param string $money_field_name |
||
| 2758 | * @return int |
||
| 2759 | * @throws InvalidEntityException |
||
| 2760 | * @throws EE_Error |
||
| 2761 | * @throws DomainException |
||
| 2762 | */ |
||
| 2763 | public function moneyInSubunits($money_field_name) |
||
| 2768 | |||
| 2769 | |||
| 2770 | /** |
||
| 2771 | * Sets the money field's amount based on the incoming monetary subunits (eg pennies). If the currency has no |
||
| 2772 | * subunits, the amount is actually assumed to be in the currency's main units |
||
| 2773 | * |
||
| 2774 | * @param string $money_field_name |
||
| 2775 | * @param int $amount_in_subunits |
||
| 2776 | * @throws InvalidArgumentException |
||
| 2777 | * @throws InvalidInterfaceException |
||
| 2778 | * @throws InvalidIdentifierException |
||
| 2779 | * @throws InvalidDataTypeException |
||
| 2780 | * @throws EE_Error |
||
| 2781 | * @throws DomainException |
||
| 2782 | */ |
||
| 2783 | public function setMoneySubunits($money_field_name,$amount_in_subunits) |
||
| 2792 | |||
| 2793 | |||
| 2794 | /** |
||
| 2795 | * @param string $function |
||
| 2796 | * @throws DomainException |
||
| 2797 | * @throws EE_Error |
||
| 2798 | */ |
||
| 2799 | private function verifyUsesMoney($function) |
||
| 2815 | |||
| 2816 | |||
| 2817 | |||
| 2818 | /** |
||
| 2819 | * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
||
| 2820 | * (probably a bad assumption they have made, oh well) |
||
| 2821 | * |
||
| 2822 | * @return string |
||
| 2823 | */ |
||
| 2824 | public function __toString() |
||
| 2833 | |||
| 2834 | |||
| 2835 | |||
| 2836 | /** |
||
| 2837 | * Clear related model objects if they're already in the DB, because otherwise when we |
||
| 2838 | * UN-serialize this model object we'll need to be careful to add them to the entity map. |
||
| 2839 | * This means if we have made changes to those related model objects, and want to unserialize |
||
| 2840 | * the this model object on a subsequent request, changes to those related model objects will be lost. |
||
| 2841 | * Instead, those related model objects should be directly serialized and stored. |
||
| 2842 | * Eg, the following won't work: |
||
| 2843 | * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
||
| 2844 | * $att = $reg->attendee(); |
||
| 2845 | * $att->set( 'ATT_fname', 'Dirk' ); |
||
| 2846 | * update_option( 'my_option', serialize( $reg ) ); |
||
| 2847 | * //END REQUEST |
||
| 2848 | * //START NEXT REQUEST |
||
| 2849 | * $reg = get_option( 'my_option' ); |
||
| 2850 | * $reg->attendee()->save(); |
||
| 2851 | * And would need to be replace with: |
||
| 2852 | * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
||
| 2853 | * $att = $reg->attendee(); |
||
| 2854 | * $att->set( 'ATT_fname', 'Dirk' ); |
||
| 2855 | * update_option( 'my_option', serialize( $reg ) ); |
||
| 2856 | * //END REQUEST |
||
| 2857 | * //START NEXT REQUEST |
||
| 2858 | * $att = get_option( 'my_option' ); |
||
| 2859 | * $att->save(); |
||
| 2860 | * |
||
| 2861 | * @return array |
||
| 2862 | * @throws EE_Error |
||
| 2863 | */ |
||
| 2864 | public function __sleep() |
||
| 2884 | |||
| 2885 | |||
| 2886 | |||
| 2887 | /** |
||
| 2888 | * restore _props_n_values_provided_in_constructor |
||
| 2889 | * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
||
| 2890 | * and therefore should NOT be used to determine if state change has occurred since initial construction. |
||
| 2891 | * At best, you would only be able to detect if state change has occurred during THIS request. |
||
| 2892 | */ |
||
| 2893 | public function __wakeup() |
||
| 2897 | |||
| 2898 | |||
| 2899 | |||
| 2900 | } |
||
| 2901 | |||
| 2903 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.