@@ -528,7 +528,7 @@ discard block |
||
528 | 528 | * |
529 | 529 | * @param bool $full if true (default), then return the full format. Otherwise will return an array |
530 | 530 | * where the first value is the date format and the second value is the time format. |
531 | - * @return mixed string|array |
|
531 | + * @return string string|array |
|
532 | 532 | */ |
533 | 533 | public function get_format($full = true) |
534 | 534 | { |
@@ -706,7 +706,7 @@ discard block |
||
706 | 706 | * |
707 | 707 | * @param \EE_Datetime_Field $datetime_field |
708 | 708 | * @param bool $pretty |
709 | - * @param null $date_or_time |
|
709 | + * @param string|null $date_or_time |
|
710 | 710 | * @return void |
711 | 711 | * @throws InvalidArgumentException |
712 | 712 | * @throws InvalidInterfaceException |
@@ -1057,7 +1057,7 @@ discard block |
||
1057 | 1057 | * |
1058 | 1058 | * @param null $field_to_order_by What field is being used as the reference point. |
1059 | 1059 | * @param array $query_params Any additional conditions on the query. |
1060 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
1060 | + * @param string $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
1061 | 1061 | * you can indicate just the columns you want returned |
1062 | 1062 | * @return array|EE_Base_Class |
1063 | 1063 | * @throws ReflectionException |
@@ -1085,7 +1085,7 @@ discard block |
||
1085 | 1085 | * |
1086 | 1086 | * @param null $field_to_order_by What field is being used as the reference point. |
1087 | 1087 | * @param array $query_params Any additional conditions on the query. |
1088 | - * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
1088 | + * @param string $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
1089 | 1089 | * you can indicate just the column you want returned |
1090 | 1090 | * @return array|EE_Base_Class |
1091 | 1091 | * @throws ReflectionException |
@@ -1516,7 +1516,7 @@ discard block |
||
1516 | 1516 | * sets the time on a datetime property |
1517 | 1517 | * |
1518 | 1518 | * @access protected |
1519 | - * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
1519 | + * @param string $time a valid time string for php datetime functions (or DateTime object) |
|
1520 | 1520 | * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
1521 | 1521 | * @throws ReflectionException |
1522 | 1522 | * @throws InvalidArgumentException |
@@ -1534,7 +1534,7 @@ discard block |
||
1534 | 1534 | * sets the date on a datetime property |
1535 | 1535 | * |
1536 | 1536 | * @access protected |
1537 | - * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
1537 | + * @param string $date a valid date string for php datetime functions ( or DateTime object) |
|
1538 | 1538 | * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
1539 | 1539 | * @throws ReflectionException |
1540 | 1540 | * @throws InvalidArgumentException |
@@ -1712,7 +1712,7 @@ discard block |
||
1712 | 1712 | * Deletes this model object permanently from db |
1713 | 1713 | * (but keep in mind related models may block the delete and return an error) |
1714 | 1714 | * |
1715 | - * @return bool | int |
|
1715 | + * @return integer | int |
|
1716 | 1716 | * @throws ReflectionException |
1717 | 1717 | * @throws InvalidArgumentException |
1718 | 1718 | * @throws InvalidInterfaceException |
@@ -2054,7 +2054,7 @@ discard block |
||
2054 | 2054 | * |
2055 | 2055 | * @param array $props_n_values incoming array of properties and their values |
2056 | 2056 | * @param string $classname the classname of the child class |
2057 | - * @param null $timezone |
|
2057 | + * @param string|null $timezone |
|
2058 | 2058 | * @param array $date_formats incoming date_formats in an array where the first value is the |
2059 | 2059 | * date_format and the second value is the time format |
2060 | 2060 | * @return mixed (EE_Base_Class|bool) |
@@ -2141,7 +2141,7 @@ discard block |
||
2141 | 2141 | * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
2142 | 2142 | * |
2143 | 2143 | * @param string $model_classname |
2144 | - * @param null $timezone |
|
2144 | + * @param string|null $timezone |
|
2145 | 2145 | * @return EEM_Base |
2146 | 2146 | * @throws ReflectionException |
2147 | 2147 | * @throws InvalidArgumentException |
@@ -2495,7 +2495,7 @@ discard block |
||
2495 | 2495 | * @param string $field_to_sum name of field to count by. |
2496 | 2496 | * By default, uses primary key |
2497 | 2497 | * (which doesn't make much sense, so you should probably change it) |
2498 | - * @return int |
|
2498 | + * @return double |
|
2499 | 2499 | * @throws ReflectionException |
2500 | 2500 | * @throws InvalidArgumentException |
2501 | 2501 | * @throws InvalidInterfaceException |
@@ -13,3343 +13,3343 @@ |
||
13 | 13 | abstract class EE_Base_Class |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * This is an array of the original properties and values provided during construction |
|
18 | - * of this model object. (keys are model field names, values are their values). |
|
19 | - * This list is important to remember so that when we are merging data from the db, we know |
|
20 | - * which values to override and which to not override. |
|
21 | - * |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $_props_n_values_provided_in_constructor; |
|
25 | - |
|
26 | - /** |
|
27 | - * Timezone |
|
28 | - * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
29 | - * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
30 | - * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
31 | - * access to it. |
|
32 | - * |
|
33 | - * @var string |
|
34 | - */ |
|
35 | - protected $_timezone; |
|
36 | - |
|
37 | - /** |
|
38 | - * date format |
|
39 | - * pattern or format for displaying dates |
|
40 | - * |
|
41 | - * @var string $_dt_frmt |
|
42 | - */ |
|
43 | - protected $_dt_frmt; |
|
44 | - |
|
45 | - /** |
|
46 | - * time format |
|
47 | - * pattern or format for displaying time |
|
48 | - * |
|
49 | - * @var string $_tm_frmt |
|
50 | - */ |
|
51 | - protected $_tm_frmt; |
|
52 | - |
|
53 | - /** |
|
54 | - * This property is for holding a cached array of object properties indexed by property name as the key. |
|
55 | - * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
56 | - * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
57 | - * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
58 | - * |
|
59 | - * @var array |
|
60 | - */ |
|
61 | - protected $_cached_properties = array(); |
|
62 | - |
|
63 | - /** |
|
64 | - * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
65 | - * single |
|
66 | - * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
67 | - * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
68 | - * all others have an array) |
|
69 | - * |
|
70 | - * @var array |
|
71 | - */ |
|
72 | - protected $_model_relations = array(); |
|
73 | - |
|
74 | - /** |
|
75 | - * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
76 | - * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
77 | - * |
|
78 | - * @var array |
|
79 | - */ |
|
80 | - protected $_fields = array(); |
|
81 | - |
|
82 | - /** |
|
83 | - * @var boolean indicating whether or not this model object is intended to ever be saved |
|
84 | - * For example, we might create model objects intended to only be used for the duration |
|
85 | - * of this request and to be thrown away, and if they were accidentally saved |
|
86 | - * it would be a bug. |
|
87 | - */ |
|
88 | - protected $_allow_persist = true; |
|
89 | - |
|
90 | - /** |
|
91 | - * @var boolean indicating whether or not this model object's properties have changed since construction |
|
92 | - */ |
|
93 | - protected $_has_changes = false; |
|
94 | - |
|
95 | - /** |
|
96 | - * @var EEM_Base |
|
97 | - */ |
|
98 | - protected $_model; |
|
99 | - |
|
100 | - /** |
|
101 | - * This is a cache of results from custom selections done on a query that constructs this entity. The only purpose |
|
102 | - * for these values is for retrieval of the results, they are not further queryable and they are not persisted to |
|
103 | - * the db. They also do not automatically update if there are any changes to the data that produced their results. |
|
104 | - * The format is a simple array of field_alias => field_value. So for instance if a custom select was something |
|
105 | - * like, "Select COUNT(Registration.REG_ID) as Registration_Count ...", then the resulting value will be in this |
|
106 | - * array as: |
|
107 | - * array( |
|
108 | - * 'Registration_Count' => 24 |
|
109 | - * ); |
|
110 | - * Note: if the custom select configuration for the query included a data type, the value will be in the data type |
|
111 | - * provided for the query (@see EventEspresso\core\domain\values\model\CustomSelects::__construct phpdocs for more |
|
112 | - * info) |
|
113 | - * |
|
114 | - * @var array |
|
115 | - */ |
|
116 | - protected $custom_selection_results = array(); |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
121 | - * play nice |
|
122 | - * |
|
123 | - * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
|
124 | - * layer of the model's _fields array, (eg, EVT_ID, |
|
125 | - * TXN_amount, QST_name, etc) and values are their values |
|
126 | - * @param boolean $bydb a flag for setting if the class is instantiated by the |
|
127 | - * corresponding db model or not. |
|
128 | - * @param string $timezone indicate what timezone you want any datetime fields to |
|
129 | - * be in when instantiating a EE_Base_Class object. |
|
130 | - * @param array $date_formats An array of date formats to set on construct where first |
|
131 | - * value is the date_format and second value is the time |
|
132 | - * format. |
|
133 | - * @throws InvalidArgumentException |
|
134 | - * @throws InvalidInterfaceException |
|
135 | - * @throws InvalidDataTypeException |
|
136 | - * @throws EE_Error |
|
137 | - * @throws ReflectionException |
|
138 | - */ |
|
139 | - protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
|
140 | - { |
|
141 | - $className = get_class($this); |
|
142 | - do_action("AHEE__{$className}__construct", $this, $fieldValues); |
|
143 | - $this->_model = $this->get_model(); |
|
144 | - $model_fields = $this->_model->field_settings(false); |
|
145 | - // ensure $fieldValues is an array |
|
146 | - $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
|
147 | - // verify client code has not passed any invalid field names |
|
148 | - foreach ($fieldValues as $field_name => $field_value) { |
|
149 | - if (! isset($model_fields[ $field_name ])) { |
|
150 | - throw new EE_Error( |
|
151 | - sprintf( |
|
152 | - esc_html__( |
|
153 | - 'Invalid field (%s) passed to constructor of %s. Allowed fields are :%s', |
|
154 | - 'event_espresso' |
|
155 | - ), |
|
156 | - $field_name, |
|
157 | - get_class($this), |
|
158 | - implode(', ', array_keys($model_fields)) |
|
159 | - ) |
|
160 | - ); |
|
161 | - } |
|
162 | - } |
|
163 | - $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
164 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
165 | - list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
|
166 | - } else { |
|
167 | - // set default formats for date and time |
|
168 | - $this->_dt_frmt = (string) get_option('date_format', 'Y-m-d'); |
|
169 | - $this->_tm_frmt = (string) get_option('time_format', 'g:i a'); |
|
170 | - } |
|
171 | - // if db model is instantiating |
|
172 | - if ($bydb) { |
|
173 | - // client code has indicated these field values are from the database |
|
174 | - foreach ($model_fields as $fieldName => $field) { |
|
175 | - $this->set_from_db( |
|
176 | - $fieldName, |
|
177 | - isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null |
|
178 | - ); |
|
179 | - } |
|
180 | - } else { |
|
181 | - // we're constructing a brand |
|
182 | - // new instance of the model object. Generally, this means we'll need to do more field validation |
|
183 | - foreach ($model_fields as $fieldName => $field) { |
|
184 | - $this->set( |
|
185 | - $fieldName, |
|
186 | - isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null, |
|
187 | - true |
|
188 | - ); |
|
189 | - } |
|
190 | - } |
|
191 | - // remember what values were passed to this constructor |
|
192 | - $this->_props_n_values_provided_in_constructor = $fieldValues; |
|
193 | - // remember in entity mapper |
|
194 | - if (! $bydb && $this->_model->has_primary_key_field() && $this->ID()) { |
|
195 | - $this->_model->add_to_entity_map($this); |
|
196 | - } |
|
197 | - // setup all the relations |
|
198 | - foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
199 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
200 | - $this->_model_relations[ $relation_name ] = null; |
|
201 | - } else { |
|
202 | - $this->_model_relations[ $relation_name ] = array(); |
|
203 | - } |
|
204 | - } |
|
205 | - /** |
|
206 | - * Action done at the end of each model object construction |
|
207 | - * |
|
208 | - * @param EE_Base_Class $this the model object just created |
|
209 | - */ |
|
210 | - do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
211 | - } |
|
212 | - |
|
213 | - |
|
214 | - /** |
|
215 | - * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
216 | - * |
|
217 | - * @return boolean |
|
218 | - */ |
|
219 | - public function allow_persist() |
|
220 | - { |
|
221 | - return $this->_allow_persist; |
|
222 | - } |
|
223 | - |
|
224 | - |
|
225 | - /** |
|
226 | - * Sets whether or not this model object should be allowed to be saved to the DB. |
|
227 | - * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
228 | - * you got new information that somehow made you change your mind. |
|
229 | - * |
|
230 | - * @param boolean $allow_persist |
|
231 | - * @return boolean |
|
232 | - */ |
|
233 | - public function set_allow_persist($allow_persist) |
|
234 | - { |
|
235 | - return $this->_allow_persist = $allow_persist; |
|
236 | - } |
|
237 | - |
|
238 | - |
|
239 | - /** |
|
240 | - * Gets the field's original value when this object was constructed during this request. |
|
241 | - * This can be helpful when determining if a model object has changed or not |
|
242 | - * |
|
243 | - * @param string $field_name |
|
244 | - * @return mixed|null |
|
245 | - * @throws ReflectionException |
|
246 | - * @throws InvalidArgumentException |
|
247 | - * @throws InvalidInterfaceException |
|
248 | - * @throws InvalidDataTypeException |
|
249 | - * @throws EE_Error |
|
250 | - */ |
|
251 | - public function get_original($field_name) |
|
252 | - { |
|
253 | - if ( |
|
254 | - isset($this->_props_n_values_provided_in_constructor[ $field_name ]) |
|
255 | - && $field_settings = $this->_model->field_settings_for($field_name) |
|
256 | - ) { |
|
257 | - return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]); |
|
258 | - } |
|
259 | - return null; |
|
260 | - } |
|
261 | - |
|
262 | - |
|
263 | - /** |
|
264 | - * @param EE_Base_Class $obj |
|
265 | - * @return string |
|
266 | - */ |
|
267 | - public function get_class($obj) |
|
268 | - { |
|
269 | - return get_class($obj); |
|
270 | - } |
|
271 | - |
|
272 | - |
|
273 | - /** |
|
274 | - * Overrides parent because parent expects old models. |
|
275 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
276 | - * |
|
277 | - * @param string $field_name |
|
278 | - * @param mixed $field_value |
|
279 | - * @param bool $use_default |
|
280 | - * @throws InvalidArgumentException |
|
281 | - * @throws InvalidInterfaceException |
|
282 | - * @throws InvalidDataTypeException |
|
283 | - * @throws EE_Error |
|
284 | - * @throws ReflectionException |
|
285 | - * @throws ReflectionException |
|
286 | - * @throws ReflectionException |
|
287 | - */ |
|
288 | - public function set($field_name, $field_value, $use_default = false) |
|
289 | - { |
|
290 | - // if not using default and nothing has changed, and object has already been setup (has ID), |
|
291 | - // then don't do anything |
|
292 | - if ( |
|
293 | - ! $use_default |
|
294 | - && $this->_fields[ $field_name ] === $field_value |
|
295 | - && $this->ID() |
|
296 | - ) { |
|
297 | - return; |
|
298 | - } |
|
299 | - $this->_has_changes = true; |
|
300 | - $field_obj = $this->_model->field_settings_for($field_name); |
|
301 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
302 | - // if ( method_exists( $field_obj, 'set_timezone' )) { |
|
303 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
304 | - $field_obj->set_timezone($this->_timezone); |
|
305 | - $field_obj->set_date_format($this->_dt_frmt); |
|
306 | - $field_obj->set_time_format($this->_tm_frmt); |
|
307 | - } |
|
308 | - $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
309 | - // should the value be null? |
|
310 | - if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
311 | - $this->_fields[ $field_name ] = $field_obj->get_default_value(); |
|
312 | - /** |
|
313 | - * To save having to refactor all the models, if a default value is used for a |
|
314 | - * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
315 | - * object. Then let's do a set again to ensure that it becomes a DateTime |
|
316 | - * object. |
|
317 | - * |
|
318 | - * @since 4.6.10+ |
|
319 | - */ |
|
320 | - if ( |
|
321 | - $field_obj instanceof EE_Datetime_Field |
|
322 | - && $this->_fields[ $field_name ] !== null |
|
323 | - && ! $this->_fields[ $field_name ] instanceof DateTime |
|
324 | - ) { |
|
325 | - empty($this->_fields[ $field_name ]) |
|
326 | - ? $this->set($field_name, time()) |
|
327 | - : $this->set($field_name, $this->_fields[ $field_name ]); |
|
328 | - } |
|
329 | - } else { |
|
330 | - $this->_fields[ $field_name ] = $holder_of_value; |
|
331 | - } |
|
332 | - // if we're not in the constructor... |
|
333 | - // now check if what we set was a primary key |
|
334 | - if ( |
|
16 | + /** |
|
17 | + * This is an array of the original properties and values provided during construction |
|
18 | + * of this model object. (keys are model field names, values are their values). |
|
19 | + * This list is important to remember so that when we are merging data from the db, we know |
|
20 | + * which values to override and which to not override. |
|
21 | + * |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $_props_n_values_provided_in_constructor; |
|
25 | + |
|
26 | + /** |
|
27 | + * Timezone |
|
28 | + * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
29 | + * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
30 | + * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
31 | + * access to it. |
|
32 | + * |
|
33 | + * @var string |
|
34 | + */ |
|
35 | + protected $_timezone; |
|
36 | + |
|
37 | + /** |
|
38 | + * date format |
|
39 | + * pattern or format for displaying dates |
|
40 | + * |
|
41 | + * @var string $_dt_frmt |
|
42 | + */ |
|
43 | + protected $_dt_frmt; |
|
44 | + |
|
45 | + /** |
|
46 | + * time format |
|
47 | + * pattern or format for displaying time |
|
48 | + * |
|
49 | + * @var string $_tm_frmt |
|
50 | + */ |
|
51 | + protected $_tm_frmt; |
|
52 | + |
|
53 | + /** |
|
54 | + * This property is for holding a cached array of object properties indexed by property name as the key. |
|
55 | + * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
56 | + * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
57 | + * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
58 | + * |
|
59 | + * @var array |
|
60 | + */ |
|
61 | + protected $_cached_properties = array(); |
|
62 | + |
|
63 | + /** |
|
64 | + * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
65 | + * single |
|
66 | + * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
67 | + * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
68 | + * all others have an array) |
|
69 | + * |
|
70 | + * @var array |
|
71 | + */ |
|
72 | + protected $_model_relations = array(); |
|
73 | + |
|
74 | + /** |
|
75 | + * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
76 | + * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
77 | + * |
|
78 | + * @var array |
|
79 | + */ |
|
80 | + protected $_fields = array(); |
|
81 | + |
|
82 | + /** |
|
83 | + * @var boolean indicating whether or not this model object is intended to ever be saved |
|
84 | + * For example, we might create model objects intended to only be used for the duration |
|
85 | + * of this request and to be thrown away, and if they were accidentally saved |
|
86 | + * it would be a bug. |
|
87 | + */ |
|
88 | + protected $_allow_persist = true; |
|
89 | + |
|
90 | + /** |
|
91 | + * @var boolean indicating whether or not this model object's properties have changed since construction |
|
92 | + */ |
|
93 | + protected $_has_changes = false; |
|
94 | + |
|
95 | + /** |
|
96 | + * @var EEM_Base |
|
97 | + */ |
|
98 | + protected $_model; |
|
99 | + |
|
100 | + /** |
|
101 | + * This is a cache of results from custom selections done on a query that constructs this entity. The only purpose |
|
102 | + * for these values is for retrieval of the results, they are not further queryable and they are not persisted to |
|
103 | + * the db. They also do not automatically update if there are any changes to the data that produced their results. |
|
104 | + * The format is a simple array of field_alias => field_value. So for instance if a custom select was something |
|
105 | + * like, "Select COUNT(Registration.REG_ID) as Registration_Count ...", then the resulting value will be in this |
|
106 | + * array as: |
|
107 | + * array( |
|
108 | + * 'Registration_Count' => 24 |
|
109 | + * ); |
|
110 | + * Note: if the custom select configuration for the query included a data type, the value will be in the data type |
|
111 | + * provided for the query (@see EventEspresso\core\domain\values\model\CustomSelects::__construct phpdocs for more |
|
112 | + * info) |
|
113 | + * |
|
114 | + * @var array |
|
115 | + */ |
|
116 | + protected $custom_selection_results = array(); |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
121 | + * play nice |
|
122 | + * |
|
123 | + * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
|
124 | + * layer of the model's _fields array, (eg, EVT_ID, |
|
125 | + * TXN_amount, QST_name, etc) and values are their values |
|
126 | + * @param boolean $bydb a flag for setting if the class is instantiated by the |
|
127 | + * corresponding db model or not. |
|
128 | + * @param string $timezone indicate what timezone you want any datetime fields to |
|
129 | + * be in when instantiating a EE_Base_Class object. |
|
130 | + * @param array $date_formats An array of date formats to set on construct where first |
|
131 | + * value is the date_format and second value is the time |
|
132 | + * format. |
|
133 | + * @throws InvalidArgumentException |
|
134 | + * @throws InvalidInterfaceException |
|
135 | + * @throws InvalidDataTypeException |
|
136 | + * @throws EE_Error |
|
137 | + * @throws ReflectionException |
|
138 | + */ |
|
139 | + protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
|
140 | + { |
|
141 | + $className = get_class($this); |
|
142 | + do_action("AHEE__{$className}__construct", $this, $fieldValues); |
|
143 | + $this->_model = $this->get_model(); |
|
144 | + $model_fields = $this->_model->field_settings(false); |
|
145 | + // ensure $fieldValues is an array |
|
146 | + $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
|
147 | + // verify client code has not passed any invalid field names |
|
148 | + foreach ($fieldValues as $field_name => $field_value) { |
|
149 | + if (! isset($model_fields[ $field_name ])) { |
|
150 | + throw new EE_Error( |
|
151 | + sprintf( |
|
152 | + esc_html__( |
|
153 | + 'Invalid field (%s) passed to constructor of %s. Allowed fields are :%s', |
|
154 | + 'event_espresso' |
|
155 | + ), |
|
156 | + $field_name, |
|
157 | + get_class($this), |
|
158 | + implode(', ', array_keys($model_fields)) |
|
159 | + ) |
|
160 | + ); |
|
161 | + } |
|
162 | + } |
|
163 | + $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
164 | + if (! empty($date_formats) && is_array($date_formats)) { |
|
165 | + list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
|
166 | + } else { |
|
167 | + // set default formats for date and time |
|
168 | + $this->_dt_frmt = (string) get_option('date_format', 'Y-m-d'); |
|
169 | + $this->_tm_frmt = (string) get_option('time_format', 'g:i a'); |
|
170 | + } |
|
171 | + // if db model is instantiating |
|
172 | + if ($bydb) { |
|
173 | + // client code has indicated these field values are from the database |
|
174 | + foreach ($model_fields as $fieldName => $field) { |
|
175 | + $this->set_from_db( |
|
176 | + $fieldName, |
|
177 | + isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null |
|
178 | + ); |
|
179 | + } |
|
180 | + } else { |
|
181 | + // we're constructing a brand |
|
182 | + // new instance of the model object. Generally, this means we'll need to do more field validation |
|
183 | + foreach ($model_fields as $fieldName => $field) { |
|
184 | + $this->set( |
|
185 | + $fieldName, |
|
186 | + isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null, |
|
187 | + true |
|
188 | + ); |
|
189 | + } |
|
190 | + } |
|
191 | + // remember what values were passed to this constructor |
|
192 | + $this->_props_n_values_provided_in_constructor = $fieldValues; |
|
193 | + // remember in entity mapper |
|
194 | + if (! $bydb && $this->_model->has_primary_key_field() && $this->ID()) { |
|
195 | + $this->_model->add_to_entity_map($this); |
|
196 | + } |
|
197 | + // setup all the relations |
|
198 | + foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
199 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
200 | + $this->_model_relations[ $relation_name ] = null; |
|
201 | + } else { |
|
202 | + $this->_model_relations[ $relation_name ] = array(); |
|
203 | + } |
|
204 | + } |
|
205 | + /** |
|
206 | + * Action done at the end of each model object construction |
|
207 | + * |
|
208 | + * @param EE_Base_Class $this the model object just created |
|
209 | + */ |
|
210 | + do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
211 | + } |
|
212 | + |
|
213 | + |
|
214 | + /** |
|
215 | + * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
216 | + * |
|
217 | + * @return boolean |
|
218 | + */ |
|
219 | + public function allow_persist() |
|
220 | + { |
|
221 | + return $this->_allow_persist; |
|
222 | + } |
|
223 | + |
|
224 | + |
|
225 | + /** |
|
226 | + * Sets whether or not this model object should be allowed to be saved to the DB. |
|
227 | + * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
228 | + * you got new information that somehow made you change your mind. |
|
229 | + * |
|
230 | + * @param boolean $allow_persist |
|
231 | + * @return boolean |
|
232 | + */ |
|
233 | + public function set_allow_persist($allow_persist) |
|
234 | + { |
|
235 | + return $this->_allow_persist = $allow_persist; |
|
236 | + } |
|
237 | + |
|
238 | + |
|
239 | + /** |
|
240 | + * Gets the field's original value when this object was constructed during this request. |
|
241 | + * This can be helpful when determining if a model object has changed or not |
|
242 | + * |
|
243 | + * @param string $field_name |
|
244 | + * @return mixed|null |
|
245 | + * @throws ReflectionException |
|
246 | + * @throws InvalidArgumentException |
|
247 | + * @throws InvalidInterfaceException |
|
248 | + * @throws InvalidDataTypeException |
|
249 | + * @throws EE_Error |
|
250 | + */ |
|
251 | + public function get_original($field_name) |
|
252 | + { |
|
253 | + if ( |
|
254 | + isset($this->_props_n_values_provided_in_constructor[ $field_name ]) |
|
255 | + && $field_settings = $this->_model->field_settings_for($field_name) |
|
256 | + ) { |
|
257 | + return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]); |
|
258 | + } |
|
259 | + return null; |
|
260 | + } |
|
261 | + |
|
262 | + |
|
263 | + /** |
|
264 | + * @param EE_Base_Class $obj |
|
265 | + * @return string |
|
266 | + */ |
|
267 | + public function get_class($obj) |
|
268 | + { |
|
269 | + return get_class($obj); |
|
270 | + } |
|
271 | + |
|
272 | + |
|
273 | + /** |
|
274 | + * Overrides parent because parent expects old models. |
|
275 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
276 | + * |
|
277 | + * @param string $field_name |
|
278 | + * @param mixed $field_value |
|
279 | + * @param bool $use_default |
|
280 | + * @throws InvalidArgumentException |
|
281 | + * @throws InvalidInterfaceException |
|
282 | + * @throws InvalidDataTypeException |
|
283 | + * @throws EE_Error |
|
284 | + * @throws ReflectionException |
|
285 | + * @throws ReflectionException |
|
286 | + * @throws ReflectionException |
|
287 | + */ |
|
288 | + public function set($field_name, $field_value, $use_default = false) |
|
289 | + { |
|
290 | + // if not using default and nothing has changed, and object has already been setup (has ID), |
|
291 | + // then don't do anything |
|
292 | + if ( |
|
293 | + ! $use_default |
|
294 | + && $this->_fields[ $field_name ] === $field_value |
|
295 | + && $this->ID() |
|
296 | + ) { |
|
297 | + return; |
|
298 | + } |
|
299 | + $this->_has_changes = true; |
|
300 | + $field_obj = $this->_model->field_settings_for($field_name); |
|
301 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
302 | + // if ( method_exists( $field_obj, 'set_timezone' )) { |
|
303 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
304 | + $field_obj->set_timezone($this->_timezone); |
|
305 | + $field_obj->set_date_format($this->_dt_frmt); |
|
306 | + $field_obj->set_time_format($this->_tm_frmt); |
|
307 | + } |
|
308 | + $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
309 | + // should the value be null? |
|
310 | + if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
311 | + $this->_fields[ $field_name ] = $field_obj->get_default_value(); |
|
312 | + /** |
|
313 | + * To save having to refactor all the models, if a default value is used for a |
|
314 | + * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
315 | + * object. Then let's do a set again to ensure that it becomes a DateTime |
|
316 | + * object. |
|
317 | + * |
|
318 | + * @since 4.6.10+ |
|
319 | + */ |
|
320 | + if ( |
|
321 | + $field_obj instanceof EE_Datetime_Field |
|
322 | + && $this->_fields[ $field_name ] !== null |
|
323 | + && ! $this->_fields[ $field_name ] instanceof DateTime |
|
324 | + ) { |
|
325 | + empty($this->_fields[ $field_name ]) |
|
326 | + ? $this->set($field_name, time()) |
|
327 | + : $this->set($field_name, $this->_fields[ $field_name ]); |
|
328 | + } |
|
329 | + } else { |
|
330 | + $this->_fields[ $field_name ] = $holder_of_value; |
|
331 | + } |
|
332 | + // if we're not in the constructor... |
|
333 | + // now check if what we set was a primary key |
|
334 | + if ( |
|
335 | 335 | // note: props_n_values_provided_in_constructor is only set at the END of the constructor |
336 | - $this->_props_n_values_provided_in_constructor |
|
337 | - && $field_value |
|
338 | - && $field_name === $this->_model->primary_key_name() |
|
339 | - ) { |
|
340 | - // if so, we want all this object's fields to be filled either with |
|
341 | - // what we've explicitly set on this model |
|
342 | - // or what we have in the db |
|
343 | - // echo "setting primary key!"; |
|
344 | - $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
345 | - $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
346 | - foreach ($fields_on_model as $field_obj) { |
|
347 | - if ( |
|
348 | - ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
349 | - && $field_obj->get_name() !== $field_name |
|
350 | - ) { |
|
351 | - $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
352 | - } |
|
353 | - } |
|
354 | - // oh this model object has an ID? well make sure its in the entity mapper |
|
355 | - $this->_model->add_to_entity_map($this); |
|
356 | - } |
|
357 | - // let's unset any cache for this field_name from the $_cached_properties property. |
|
358 | - $this->_clear_cached_property($field_name); |
|
359 | - } else { |
|
360 | - throw new EE_Error( |
|
361 | - sprintf( |
|
362 | - esc_html__( |
|
363 | - 'A valid EE_Model_Field_Base could not be found for the given field name: %s', |
|
364 | - 'event_espresso' |
|
365 | - ), |
|
366 | - $field_name |
|
367 | - ) |
|
368 | - ); |
|
369 | - } |
|
370 | - } |
|
371 | - |
|
372 | - |
|
373 | - /** |
|
374 | - * Set custom select values for model. |
|
375 | - * |
|
376 | - * @param array $custom_select_values |
|
377 | - */ |
|
378 | - public function setCustomSelectsValues(array $custom_select_values) |
|
379 | - { |
|
380 | - $this->custom_selection_results = $custom_select_values; |
|
381 | - } |
|
382 | - |
|
383 | - |
|
384 | - /** |
|
385 | - * Returns the custom select value for the provided alias if its set. |
|
386 | - * If not set, returns null. |
|
387 | - * |
|
388 | - * @param string $alias |
|
389 | - * @return string|int|float|null |
|
390 | - */ |
|
391 | - public function getCustomSelect($alias) |
|
392 | - { |
|
393 | - return isset($this->custom_selection_results[ $alias ]) |
|
394 | - ? $this->custom_selection_results[ $alias ] |
|
395 | - : null; |
|
396 | - } |
|
397 | - |
|
398 | - |
|
399 | - /** |
|
400 | - * This sets the field value on the db column if it exists for the given $column_name or |
|
401 | - * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
402 | - * |
|
403 | - * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
404 | - * @param string $field_name Must be the exact column name. |
|
405 | - * @param mixed $field_value The value to set. |
|
406 | - * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
407 | - * @throws InvalidArgumentException |
|
408 | - * @throws InvalidInterfaceException |
|
409 | - * @throws InvalidDataTypeException |
|
410 | - * @throws EE_Error |
|
411 | - * @throws ReflectionException |
|
412 | - */ |
|
413 | - public function set_field_or_extra_meta($field_name, $field_value) |
|
414 | - { |
|
415 | - if ($this->_model->has_field($field_name)) { |
|
416 | - $this->set($field_name, $field_value); |
|
417 | - return true; |
|
418 | - } |
|
419 | - // ensure this object is saved first so that extra meta can be properly related. |
|
420 | - $this->save(); |
|
421 | - return $this->update_extra_meta($field_name, $field_value); |
|
422 | - } |
|
423 | - |
|
424 | - |
|
425 | - /** |
|
426 | - * This retrieves the value of the db column set on this class or if that's not present |
|
427 | - * it will attempt to retrieve from extra_meta if found. |
|
428 | - * Example Usage: |
|
429 | - * Via EE_Message child class: |
|
430 | - * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
431 | - * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
432 | - * also have additional main fields specific to the messenger. The system accommodates those extra |
|
433 | - * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
434 | - * value for those extra fields dynamically via the EE_message object. |
|
435 | - * |
|
436 | - * @param string $field_name expecting the fully qualified field name. |
|
437 | - * @return mixed|null value for the field if found. null if not found. |
|
438 | - * @throws ReflectionException |
|
439 | - * @throws InvalidArgumentException |
|
440 | - * @throws InvalidInterfaceException |
|
441 | - * @throws InvalidDataTypeException |
|
442 | - * @throws EE_Error |
|
443 | - */ |
|
444 | - public function get_field_or_extra_meta($field_name) |
|
445 | - { |
|
446 | - if ($this->_model->has_field($field_name)) { |
|
447 | - $column_value = $this->get($field_name); |
|
448 | - } else { |
|
449 | - // This isn't a column in the main table, let's see if it is in the extra meta. |
|
450 | - $column_value = $this->get_extra_meta($field_name, true, null); |
|
451 | - } |
|
452 | - return $column_value; |
|
453 | - } |
|
454 | - |
|
455 | - |
|
456 | - /** |
|
457 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
458 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
459 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
460 | - * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
461 | - * |
|
462 | - * @access public |
|
463 | - * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
464 | - * @return void |
|
465 | - * @throws InvalidArgumentException |
|
466 | - * @throws InvalidInterfaceException |
|
467 | - * @throws InvalidDataTypeException |
|
468 | - * @throws EE_Error |
|
469 | - * @throws ReflectionException |
|
470 | - */ |
|
471 | - public function set_timezone($timezone = '') |
|
472 | - { |
|
473 | - $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
474 | - // make sure we clear all cached properties because they won't be relevant now |
|
475 | - $this->_clear_cached_properties(); |
|
476 | - // make sure we update field settings and the date for all EE_Datetime_Fields |
|
477 | - $model_fields = $this->_model->field_settings(false); |
|
478 | - foreach ($model_fields as $field_name => $field_obj) { |
|
479 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
480 | - $field_obj->set_timezone($this->_timezone); |
|
481 | - if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) { |
|
482 | - EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], new DateTimeZone($this->_timezone)); |
|
483 | - } |
|
484 | - } |
|
485 | - } |
|
486 | - } |
|
487 | - |
|
488 | - |
|
489 | - /** |
|
490 | - * This just returns whatever is set for the current timezone. |
|
491 | - * |
|
492 | - * @access public |
|
493 | - * @return string timezone string |
|
494 | - */ |
|
495 | - public function get_timezone() |
|
496 | - { |
|
497 | - return $this->_timezone; |
|
498 | - } |
|
499 | - |
|
500 | - |
|
501 | - /** |
|
502 | - * This sets the internal date format to what is sent in to be used as the new default for the class |
|
503 | - * internally instead of wp set date format options |
|
504 | - * |
|
505 | - * @since 4.6 |
|
506 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
507 | - */ |
|
508 | - public function set_date_format($format) |
|
509 | - { |
|
510 | - $this->_dt_frmt = $format; |
|
511 | - // clear cached_properties because they won't be relevant now. |
|
512 | - $this->_clear_cached_properties(); |
|
513 | - } |
|
514 | - |
|
515 | - |
|
516 | - /** |
|
517 | - * This sets the internal time format string to what is sent in to be used as the new default for the |
|
518 | - * class internally instead of wp set time format options. |
|
519 | - * |
|
520 | - * @since 4.6 |
|
521 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
522 | - */ |
|
523 | - public function set_time_format($format) |
|
524 | - { |
|
525 | - $this->_tm_frmt = $format; |
|
526 | - // clear cached_properties because they won't be relevant now. |
|
527 | - $this->_clear_cached_properties(); |
|
528 | - } |
|
529 | - |
|
530 | - |
|
531 | - /** |
|
532 | - * This returns the current internal set format for the date and time formats. |
|
533 | - * |
|
534 | - * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
535 | - * where the first value is the date format and the second value is the time format. |
|
536 | - * @return mixed string|array |
|
537 | - */ |
|
538 | - public function get_format($full = true) |
|
539 | - { |
|
540 | - return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
541 | - } |
|
542 | - |
|
543 | - |
|
544 | - /** |
|
545 | - * cache |
|
546 | - * stores the passed model object on the current model object. |
|
547 | - * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
548 | - * |
|
549 | - * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
550 | - * 'Registration' associated with this model object |
|
551 | - * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
552 | - * that could be a payment or a registration) |
|
553 | - * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
554 | - * items which will be stored in an array on this object |
|
555 | - * @throws ReflectionException |
|
556 | - * @throws InvalidArgumentException |
|
557 | - * @throws InvalidInterfaceException |
|
558 | - * @throws InvalidDataTypeException |
|
559 | - * @throws EE_Error |
|
560 | - * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
561 | - * related thing, no array) |
|
562 | - */ |
|
563 | - public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
564 | - { |
|
565 | - // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
566 | - if (! $object_to_cache instanceof EE_Base_Class) { |
|
567 | - return false; |
|
568 | - } |
|
569 | - // also get "how" the object is related, or throw an error |
|
570 | - if (! $relationship_to_model = $this->_model->related_settings_for($relationName)) { |
|
571 | - throw new EE_Error( |
|
572 | - sprintf( |
|
573 | - esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
574 | - $relationName, |
|
575 | - get_class($this) |
|
576 | - ) |
|
577 | - ); |
|
578 | - } |
|
579 | - // how many things are related ? |
|
580 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
581 | - // if it's a "belongs to" relationship, then there's only one related model object |
|
582 | - // eg, if this is a registration, there's only 1 attendee for it |
|
583 | - // so for these model objects just set it to be cached |
|
584 | - $this->_model_relations[ $relationName ] = $object_to_cache; |
|
585 | - $return = true; |
|
586 | - } else { |
|
587 | - // otherwise, this is the "many" side of a one to many relationship, |
|
588 | - // so we'll add the object to the array of related objects for that type. |
|
589 | - // eg: if this is an event, there are many registrations for that event, |
|
590 | - // so we cache the registrations in an array |
|
591 | - if (! is_array($this->_model_relations[ $relationName ])) { |
|
592 | - // if for some reason, the cached item is a model object, |
|
593 | - // then stick that in the array, otherwise start with an empty array |
|
594 | - $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ] |
|
595 | - instanceof |
|
596 | - EE_Base_Class |
|
597 | - ? array($this->_model_relations[ $relationName ]) : array(); |
|
598 | - } |
|
599 | - // first check for a cache_id which is normally empty |
|
600 | - if (! empty($cache_id)) { |
|
601 | - // if the cache_id exists, then it means we are purposely trying to cache this |
|
602 | - // with a known key that can then be used to retrieve the object later on |
|
603 | - $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache; |
|
604 | - $return = $cache_id; |
|
605 | - } elseif ($object_to_cache->ID()) { |
|
606 | - // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
607 | - $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache; |
|
608 | - $return = $object_to_cache->ID(); |
|
609 | - } else { |
|
610 | - // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
611 | - $this->_model_relations[ $relationName ][] = $object_to_cache; |
|
612 | - // move the internal pointer to the end of the array |
|
613 | - end($this->_model_relations[ $relationName ]); |
|
614 | - // and grab the key so that we can return it |
|
615 | - $return = key($this->_model_relations[ $relationName ]); |
|
616 | - } |
|
617 | - } |
|
618 | - return $return; |
|
619 | - } |
|
620 | - |
|
621 | - |
|
622 | - /** |
|
623 | - * For adding an item to the cached_properties property. |
|
624 | - * |
|
625 | - * @access protected |
|
626 | - * @param string $fieldname the property item the corresponding value is for. |
|
627 | - * @param mixed $value The value we are caching. |
|
628 | - * @param string|null $cache_type |
|
629 | - * @return void |
|
630 | - * @throws ReflectionException |
|
631 | - * @throws InvalidArgumentException |
|
632 | - * @throws InvalidInterfaceException |
|
633 | - * @throws InvalidDataTypeException |
|
634 | - * @throws EE_Error |
|
635 | - */ |
|
636 | - protected function _set_cached_property($fieldname, $value, $cache_type = null) |
|
637 | - { |
|
638 | - // first make sure this property exists |
|
639 | - $this->_model->field_settings_for($fieldname); |
|
640 | - $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
641 | - $this->_cached_properties[ $fieldname ][ $cache_type ] = $value; |
|
642 | - } |
|
643 | - |
|
644 | - |
|
645 | - /** |
|
646 | - * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
647 | - * This also SETS the cache if we return the actual property! |
|
648 | - * |
|
649 | - * @param string $fieldname the name of the property we're trying to retrieve |
|
650 | - * @param bool $pretty |
|
651 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
652 | - * (in cases where the same property may be used for different outputs |
|
653 | - * - i.e. datetime, money etc.) |
|
654 | - * It can also accept certain pre-defined "schema" strings |
|
655 | - * to define how to output the property. |
|
656 | - * see the field's prepare_for_pretty_echoing for what strings can be used |
|
657 | - * @return mixed whatever the value for the property is we're retrieving |
|
658 | - * @throws ReflectionException |
|
659 | - * @throws InvalidArgumentException |
|
660 | - * @throws InvalidInterfaceException |
|
661 | - * @throws InvalidDataTypeException |
|
662 | - * @throws EE_Error |
|
663 | - */ |
|
664 | - protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
665 | - { |
|
666 | - // verify the field exists |
|
667 | - $this->_model->field_settings_for($fieldname); |
|
668 | - $cache_type = $pretty ? 'pretty' : 'standard'; |
|
669 | - $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
670 | - if (isset($this->_cached_properties[ $fieldname ][ $cache_type ])) { |
|
671 | - return $this->_cached_properties[ $fieldname ][ $cache_type ]; |
|
672 | - } |
|
673 | - $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
|
674 | - $this->_set_cached_property($fieldname, $value, $cache_type); |
|
675 | - return $value; |
|
676 | - } |
|
677 | - |
|
678 | - |
|
679 | - /** |
|
680 | - * If the cache didn't fetch the needed item, this fetches it. |
|
681 | - * |
|
682 | - * @param string $fieldname |
|
683 | - * @param bool $pretty |
|
684 | - * @param string $extra_cache_ref |
|
685 | - * @return mixed |
|
686 | - * @throws InvalidArgumentException |
|
687 | - * @throws InvalidInterfaceException |
|
688 | - * @throws InvalidDataTypeException |
|
689 | - * @throws EE_Error |
|
690 | - * @throws ReflectionException |
|
691 | - */ |
|
692 | - protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
693 | - { |
|
694 | - $field_obj = $this->_model->field_settings_for($fieldname); |
|
695 | - // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
696 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
697 | - $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
698 | - } |
|
699 | - if (! isset($this->_fields[ $fieldname ])) { |
|
700 | - $this->_fields[ $fieldname ] = null; |
|
701 | - } |
|
702 | - $value = $pretty |
|
703 | - ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $fieldname ], $extra_cache_ref) |
|
704 | - : $field_obj->prepare_for_get($this->_fields[ $fieldname ]); |
|
705 | - return $value; |
|
706 | - } |
|
707 | - |
|
708 | - |
|
709 | - /** |
|
710 | - * set timezone, formats, and output for EE_Datetime_Field objects |
|
711 | - * |
|
712 | - * @param \EE_Datetime_Field $datetime_field |
|
713 | - * @param bool $pretty |
|
714 | - * @param null $date_or_time |
|
715 | - * @return void |
|
716 | - * @throws InvalidArgumentException |
|
717 | - * @throws InvalidInterfaceException |
|
718 | - * @throws InvalidDataTypeException |
|
719 | - * @throws EE_Error |
|
720 | - */ |
|
721 | - protected function _prepare_datetime_field( |
|
722 | - EE_Datetime_Field $datetime_field, |
|
723 | - $pretty = false, |
|
724 | - $date_or_time = null |
|
725 | - ) { |
|
726 | - $datetime_field->set_timezone($this->_timezone); |
|
727 | - $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
728 | - $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
729 | - // set the output returned |
|
730 | - switch ($date_or_time) { |
|
731 | - case 'D': |
|
732 | - $datetime_field->set_date_time_output('date'); |
|
733 | - break; |
|
734 | - case 'T': |
|
735 | - $datetime_field->set_date_time_output('time'); |
|
736 | - break; |
|
737 | - default: |
|
738 | - $datetime_field->set_date_time_output(); |
|
739 | - } |
|
740 | - } |
|
741 | - |
|
742 | - |
|
743 | - /** |
|
744 | - * This just takes care of clearing out the cached_properties |
|
745 | - * |
|
746 | - * @return void |
|
747 | - */ |
|
748 | - protected function _clear_cached_properties() |
|
749 | - { |
|
750 | - $this->_cached_properties = array(); |
|
751 | - } |
|
752 | - |
|
753 | - |
|
754 | - /** |
|
755 | - * This just clears out ONE property if it exists in the cache |
|
756 | - * |
|
757 | - * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
758 | - * @return void |
|
759 | - */ |
|
760 | - protected function _clear_cached_property($property_name) |
|
761 | - { |
|
762 | - if (isset($this->_cached_properties[ $property_name ])) { |
|
763 | - unset($this->_cached_properties[ $property_name ]); |
|
764 | - } |
|
765 | - } |
|
766 | - |
|
767 | - |
|
768 | - /** |
|
769 | - * Ensures that this related thing is a model object. |
|
770 | - * |
|
771 | - * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
772 | - * @param string $model_name name of the related thing, eg 'Attendee', |
|
773 | - * @return EE_Base_Class |
|
774 | - * @throws ReflectionException |
|
775 | - * @throws InvalidArgumentException |
|
776 | - * @throws InvalidInterfaceException |
|
777 | - * @throws InvalidDataTypeException |
|
778 | - * @throws EE_Error |
|
779 | - */ |
|
780 | - protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
781 | - { |
|
782 | - $other_model_instance = self::_get_model_instance_with_name( |
|
783 | - self::_get_model_classname($model_name), |
|
784 | - $this->_timezone |
|
785 | - ); |
|
786 | - return $other_model_instance->ensure_is_obj($object_or_id); |
|
787 | - } |
|
788 | - |
|
789 | - |
|
790 | - /** |
|
791 | - * Forgets the cached model of the given relation Name. So the next time we request it, |
|
792 | - * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
793 | - * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
794 | - * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
795 | - * |
|
796 | - * @param string $relationName one of the keys in the _model_relations array on the model. |
|
797 | - * Eg 'Registration' |
|
798 | - * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
799 | - * if you intend to use $clear_all = TRUE, or the relation only |
|
800 | - * has 1 object anyways (ie, it's a BelongsToRelation) |
|
801 | - * @param bool $clear_all This flags clearing the entire cache relation property if |
|
802 | - * this is HasMany or HABTM. |
|
803 | - * @throws ReflectionException |
|
804 | - * @throws InvalidArgumentException |
|
805 | - * @throws InvalidInterfaceException |
|
806 | - * @throws InvalidDataTypeException |
|
807 | - * @throws EE_Error |
|
808 | - * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
809 | - * relation from all |
|
810 | - */ |
|
811 | - public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
812 | - { |
|
813 | - $relationship_to_model = $this->_model->related_settings_for($relationName); |
|
814 | - $index_in_cache = ''; |
|
815 | - if (! $relationship_to_model) { |
|
816 | - throw new EE_Error( |
|
817 | - sprintf( |
|
818 | - esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'), |
|
819 | - $relationName, |
|
820 | - get_class($this) |
|
821 | - ) |
|
822 | - ); |
|
823 | - } |
|
824 | - if ($clear_all) { |
|
825 | - $obj_removed = true; |
|
826 | - $this->_model_relations[ $relationName ] = null; |
|
827 | - } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
828 | - $obj_removed = $this->_model_relations[ $relationName ]; |
|
829 | - $this->_model_relations[ $relationName ] = null; |
|
830 | - } else { |
|
831 | - if ( |
|
832 | - $object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
833 | - && $object_to_remove_or_index_into_array->ID() |
|
834 | - ) { |
|
835 | - $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
836 | - if ( |
|
837 | - is_array($this->_model_relations[ $relationName ]) |
|
838 | - && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ]) |
|
839 | - ) { |
|
840 | - $index_found_at = null; |
|
841 | - // find this object in the array even though it has a different key |
|
842 | - foreach ($this->_model_relations[ $relationName ] as $index => $obj) { |
|
843 | - /** @noinspection TypeUnsafeComparisonInspection */ |
|
844 | - if ( |
|
845 | - $obj instanceof EE_Base_Class |
|
846 | - && ( |
|
847 | - $obj == $object_to_remove_or_index_into_array |
|
848 | - || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
849 | - ) |
|
850 | - ) { |
|
851 | - $index_found_at = $index; |
|
852 | - break; |
|
853 | - } |
|
854 | - } |
|
855 | - if ($index_found_at) { |
|
856 | - $index_in_cache = $index_found_at; |
|
857 | - } else { |
|
858 | - // it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
859 | - // if it wasn't in it to begin with. So we're done |
|
860 | - return $object_to_remove_or_index_into_array; |
|
861 | - } |
|
862 | - } |
|
863 | - } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
864 | - // so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
865 | - foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
866 | - /** @noinspection TypeUnsafeComparisonInspection */ |
|
867 | - if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
868 | - $index_in_cache = $index; |
|
869 | - } |
|
870 | - } |
|
871 | - } else { |
|
872 | - $index_in_cache = $object_to_remove_or_index_into_array; |
|
873 | - } |
|
874 | - // supposedly we've found it. But it could just be that the client code |
|
875 | - // provided a bad index/object |
|
876 | - if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) { |
|
877 | - $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ]; |
|
878 | - unset($this->_model_relations[ $relationName ][ $index_in_cache ]); |
|
879 | - } else { |
|
880 | - // that thing was never cached anyways. |
|
881 | - $obj_removed = null; |
|
882 | - } |
|
883 | - } |
|
884 | - return $obj_removed; |
|
885 | - } |
|
886 | - |
|
887 | - |
|
888 | - /** |
|
889 | - * update_cache_after_object_save |
|
890 | - * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
891 | - * obtained after being saved to the db |
|
892 | - * |
|
893 | - * @param string $relationName - the type of object that is cached |
|
894 | - * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
895 | - * @param string $current_cache_id - the ID that was used when originally caching the object |
|
896 | - * @return boolean TRUE on success, FALSE on fail |
|
897 | - * @throws ReflectionException |
|
898 | - * @throws InvalidArgumentException |
|
899 | - * @throws InvalidInterfaceException |
|
900 | - * @throws InvalidDataTypeException |
|
901 | - * @throws EE_Error |
|
902 | - */ |
|
903 | - public function update_cache_after_object_save( |
|
904 | - $relationName, |
|
905 | - EE_Base_Class $newly_saved_object, |
|
906 | - $current_cache_id = '' |
|
907 | - ) { |
|
908 | - // verify that incoming object is of the correct type |
|
909 | - $obj_class = 'EE_' . $relationName; |
|
910 | - if ($newly_saved_object instanceof $obj_class) { |
|
911 | - /* @type EE_Base_Class $newly_saved_object */ |
|
912 | - // now get the type of relation |
|
913 | - $relationship_to_model = $this->_model->related_settings_for($relationName); |
|
914 | - // if this is a 1:1 relationship |
|
915 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
916 | - // then just replace the cached object with the newly saved object |
|
917 | - $this->_model_relations[ $relationName ] = $newly_saved_object; |
|
918 | - return true; |
|
919 | - // or if it's some kind of sordid feral polyamorous relationship... |
|
920 | - } |
|
921 | - if ( |
|
922 | - is_array($this->_model_relations[ $relationName ]) |
|
923 | - && isset($this->_model_relations[ $relationName ][ $current_cache_id ]) |
|
924 | - ) { |
|
925 | - // then remove the current cached item |
|
926 | - unset($this->_model_relations[ $relationName ][ $current_cache_id ]); |
|
927 | - // and cache the newly saved object using it's new ID |
|
928 | - $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object; |
|
929 | - return true; |
|
930 | - } |
|
931 | - } |
|
932 | - return false; |
|
933 | - } |
|
934 | - |
|
935 | - |
|
936 | - /** |
|
937 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
938 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
939 | - * |
|
940 | - * @param string $relationName |
|
941 | - * @return EE_Base_Class |
|
942 | - */ |
|
943 | - public function get_one_from_cache($relationName) |
|
944 | - { |
|
945 | - $cached_array_or_object = isset($this->_model_relations[ $relationName ]) |
|
946 | - ? $this->_model_relations[ $relationName ] |
|
947 | - : null; |
|
948 | - if (is_array($cached_array_or_object)) { |
|
949 | - return array_shift($cached_array_or_object); |
|
950 | - } |
|
951 | - return $cached_array_or_object; |
|
952 | - } |
|
953 | - |
|
954 | - |
|
955 | - /** |
|
956 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
957 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
958 | - * |
|
959 | - * @param string $relationName |
|
960 | - * @throws ReflectionException |
|
961 | - * @throws InvalidArgumentException |
|
962 | - * @throws InvalidInterfaceException |
|
963 | - * @throws InvalidDataTypeException |
|
964 | - * @throws EE_Error |
|
965 | - * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
966 | - */ |
|
967 | - public function get_all_from_cache($relationName) |
|
968 | - { |
|
969 | - $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : array(); |
|
970 | - // if the result is not an array, but exists, make it an array |
|
971 | - $objects = is_array($objects) ? $objects : array($objects); |
|
972 | - // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
973 | - // basically, if this model object was stored in the session, and these cached model objects |
|
974 | - // already have IDs, let's make sure they're in their model's entity mapper |
|
975 | - // otherwise we will have duplicates next time we call |
|
976 | - // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
977 | - $related_model = EE_Registry::instance()->load_model($relationName); |
|
978 | - foreach ($objects as $model_object) { |
|
979 | - if ($related_model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
980 | - // ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
981 | - if ($model_object->ID()) { |
|
982 | - $related_model->add_to_entity_map($model_object); |
|
983 | - } |
|
984 | - } else { |
|
985 | - throw new EE_Error( |
|
986 | - sprintf( |
|
987 | - esc_html__( |
|
988 | - 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
989 | - 'event_espresso' |
|
990 | - ), |
|
991 | - $relationName, |
|
992 | - gettype($model_object) |
|
993 | - ) |
|
994 | - ); |
|
995 | - } |
|
996 | - } |
|
997 | - return $objects; |
|
998 | - } |
|
999 | - |
|
1000 | - |
|
1001 | - /** |
|
1002 | - * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
1003 | - * matching the given query conditions. |
|
1004 | - * |
|
1005 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
1006 | - * @param int $limit How many objects to return. |
|
1007 | - * @param array $query_params Any additional conditions on the query. |
|
1008 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
1009 | - * you can indicate just the columns you want returned |
|
1010 | - * @return array|EE_Base_Class[] |
|
1011 | - * @throws ReflectionException |
|
1012 | - * @throws InvalidArgumentException |
|
1013 | - * @throws InvalidInterfaceException |
|
1014 | - * @throws InvalidDataTypeException |
|
1015 | - * @throws EE_Error |
|
1016 | - */ |
|
1017 | - public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
1018 | - { |
|
1019 | - $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1020 | - ? $this->_model->get_primary_key_field()->get_name() |
|
1021 | - : $field_to_order_by; |
|
1022 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
1023 | - if (empty($field) || empty($current_value)) { |
|
1024 | - return array(); |
|
1025 | - } |
|
1026 | - return $this->_model->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
1027 | - } |
|
1028 | - |
|
1029 | - |
|
1030 | - /** |
|
1031 | - * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
1032 | - * matching the given query conditions. |
|
1033 | - * |
|
1034 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
1035 | - * @param int $limit How many objects to return. |
|
1036 | - * @param array $query_params Any additional conditions on the query. |
|
1037 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
1038 | - * you can indicate just the columns you want returned |
|
1039 | - * @return array|EE_Base_Class[] |
|
1040 | - * @throws ReflectionException |
|
1041 | - * @throws InvalidArgumentException |
|
1042 | - * @throws InvalidInterfaceException |
|
1043 | - * @throws InvalidDataTypeException |
|
1044 | - * @throws EE_Error |
|
1045 | - */ |
|
1046 | - public function previous_x( |
|
1047 | - $field_to_order_by = null, |
|
1048 | - $limit = 1, |
|
1049 | - $query_params = array(), |
|
1050 | - $columns_to_select = null |
|
1051 | - ) { |
|
1052 | - $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1053 | - ? $this->_model->get_primary_key_field()->get_name() |
|
1054 | - : $field_to_order_by; |
|
1055 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
1056 | - if (empty($field) || empty($current_value)) { |
|
1057 | - return array(); |
|
1058 | - } |
|
1059 | - return $this->_model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
1060 | - } |
|
1061 | - |
|
1062 | - |
|
1063 | - /** |
|
1064 | - * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
1065 | - * matching the given query conditions. |
|
1066 | - * |
|
1067 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
1068 | - * @param array $query_params Any additional conditions on the query. |
|
1069 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
1070 | - * you can indicate just the columns you want returned |
|
1071 | - * @return array|EE_Base_Class |
|
1072 | - * @throws ReflectionException |
|
1073 | - * @throws InvalidArgumentException |
|
1074 | - * @throws InvalidInterfaceException |
|
1075 | - * @throws InvalidDataTypeException |
|
1076 | - * @throws EE_Error |
|
1077 | - */ |
|
1078 | - public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
1079 | - { |
|
1080 | - $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1081 | - ? $this->_model->get_primary_key_field()->get_name() |
|
1082 | - : $field_to_order_by; |
|
1083 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
1084 | - if (empty($field) || empty($current_value)) { |
|
1085 | - return array(); |
|
1086 | - } |
|
1087 | - return $this->_model->next($current_value, $field, $query_params, $columns_to_select); |
|
1088 | - } |
|
1089 | - |
|
1090 | - |
|
1091 | - /** |
|
1092 | - * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
1093 | - * matching the given query conditions. |
|
1094 | - * |
|
1095 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
1096 | - * @param array $query_params Any additional conditions on the query. |
|
1097 | - * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
1098 | - * you can indicate just the column you want returned |
|
1099 | - * @return array|EE_Base_Class |
|
1100 | - * @throws ReflectionException |
|
1101 | - * @throws InvalidArgumentException |
|
1102 | - * @throws InvalidInterfaceException |
|
1103 | - * @throws InvalidDataTypeException |
|
1104 | - * @throws EE_Error |
|
1105 | - */ |
|
1106 | - public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
1107 | - { |
|
1108 | - $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1109 | - ? $this->_model->get_primary_key_field()->get_name() |
|
1110 | - : $field_to_order_by; |
|
1111 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
1112 | - if (empty($field) || empty($current_value)) { |
|
1113 | - return array(); |
|
1114 | - } |
|
1115 | - return $this->_model->previous($current_value, $field, $query_params, $columns_to_select); |
|
1116 | - } |
|
1117 | - |
|
1118 | - |
|
1119 | - /** |
|
1120 | - * Overrides parent because parent expects old models. |
|
1121 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
1122 | - * |
|
1123 | - * @param string $field_name |
|
1124 | - * @param mixed $field_value_from_db |
|
1125 | - * @throws ReflectionException |
|
1126 | - * @throws InvalidArgumentException |
|
1127 | - * @throws InvalidInterfaceException |
|
1128 | - * @throws InvalidDataTypeException |
|
1129 | - * @throws EE_Error |
|
1130 | - */ |
|
1131 | - public function set_from_db($field_name, $field_value_from_db) |
|
1132 | - { |
|
1133 | - $field_obj = $this->_model->field_settings_for($field_name); |
|
1134 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
1135 | - // you would think the DB has no NULLs for non-null label fields right? wrong! |
|
1136 | - // eg, a CPT model object could have an entry in the posts table, but no |
|
1137 | - // entry in the meta table. Meaning that all its columns in the meta table |
|
1138 | - // are null! yikes! so when we find one like that, use defaults for its meta columns |
|
1139 | - if ($field_value_from_db === null) { |
|
1140 | - if ($field_obj->is_nullable()) { |
|
1141 | - // if the field allows nulls, then let it be null |
|
1142 | - $field_value = null; |
|
1143 | - } else { |
|
1144 | - $field_value = $field_obj->get_default_value(); |
|
1145 | - } |
|
1146 | - } else { |
|
1147 | - $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
1148 | - } |
|
1149 | - $this->_fields[ $field_name ] = $field_value; |
|
1150 | - $this->_clear_cached_property($field_name); |
|
1151 | - } |
|
1152 | - } |
|
1153 | - |
|
1154 | - |
|
1155 | - /** |
|
1156 | - * verifies that the specified field is of the correct type |
|
1157 | - * |
|
1158 | - * @param string $field_name |
|
1159 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
1160 | - * (in cases where the same property may be used for different outputs |
|
1161 | - * - i.e. datetime, money etc.) |
|
1162 | - * @return mixed |
|
1163 | - * @throws ReflectionException |
|
1164 | - * @throws InvalidArgumentException |
|
1165 | - * @throws InvalidInterfaceException |
|
1166 | - * @throws InvalidDataTypeException |
|
1167 | - * @throws EE_Error |
|
1168 | - */ |
|
1169 | - public function get($field_name, $extra_cache_ref = null) |
|
1170 | - { |
|
1171 | - return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
1172 | - } |
|
1173 | - |
|
1174 | - |
|
1175 | - /** |
|
1176 | - * This method simply returns the RAW unprocessed value for the given property in this class |
|
1177 | - * |
|
1178 | - * @param string $field_name A valid fieldname |
|
1179 | - * @return mixed Whatever the raw value stored on the property is. |
|
1180 | - * @throws ReflectionException |
|
1181 | - * @throws InvalidArgumentException |
|
1182 | - * @throws InvalidInterfaceException |
|
1183 | - * @throws InvalidDataTypeException |
|
1184 | - * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
1185 | - */ |
|
1186 | - public function get_raw($field_name) |
|
1187 | - { |
|
1188 | - $field_settings = $this->_model->field_settings_for($field_name); |
|
1189 | - return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime |
|
1190 | - ? $this->_fields[ $field_name ]->format('U') |
|
1191 | - : $this->_fields[ $field_name ]; |
|
1192 | - } |
|
1193 | - |
|
1194 | - |
|
1195 | - /** |
|
1196 | - * This is used to return the internal DateTime object used for a field that is a |
|
1197 | - * EE_Datetime_Field. |
|
1198 | - * |
|
1199 | - * @param string $field_name The field name retrieving the DateTime object. |
|
1200 | - * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
1201 | - * @throws EE_Error an error is set and false returned. If the field IS an |
|
1202 | - * EE_Datetime_Field and but the field value is null, then |
|
1203 | - * just null is returned (because that indicates that likely |
|
1204 | - * this field is nullable). |
|
1205 | - * @throws InvalidArgumentException |
|
1206 | - * @throws InvalidDataTypeException |
|
1207 | - * @throws InvalidInterfaceException |
|
1208 | - * @throws ReflectionException |
|
1209 | - */ |
|
1210 | - public function get_DateTime_object($field_name) |
|
1211 | - { |
|
1212 | - $field_settings = $this->_model->field_settings_for($field_name); |
|
1213 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
1214 | - EE_Error::add_error( |
|
1215 | - sprintf( |
|
1216 | - esc_html__( |
|
1217 | - 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
1218 | - 'event_espresso' |
|
1219 | - ), |
|
1220 | - $field_name |
|
1221 | - ), |
|
1222 | - __FILE__, |
|
1223 | - __FUNCTION__, |
|
1224 | - __LINE__ |
|
1225 | - ); |
|
1226 | - return false; |
|
1227 | - } |
|
1228 | - return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime |
|
1229 | - ? clone $this->_fields[ $field_name ] |
|
1230 | - : null; |
|
1231 | - } |
|
1232 | - |
|
1233 | - |
|
1234 | - /** |
|
1235 | - * To be used in template to immediately echo out the value, and format it for output. |
|
1236 | - * Eg, should call stripslashes and whatnot before echoing |
|
1237 | - * |
|
1238 | - * @param string $field_name the name of the field as it appears in the DB |
|
1239 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
1240 | - * (in cases where the same property may be used for different outputs |
|
1241 | - * - i.e. datetime, money etc.) |
|
1242 | - * @return void |
|
1243 | - * @throws ReflectionException |
|
1244 | - * @throws InvalidArgumentException |
|
1245 | - * @throws InvalidInterfaceException |
|
1246 | - * @throws InvalidDataTypeException |
|
1247 | - * @throws EE_Error |
|
1248 | - */ |
|
1249 | - public function e($field_name, $extra_cache_ref = null) |
|
1250 | - { |
|
1251 | - echo $this->get_pretty($field_name, $extra_cache_ref); |
|
1252 | - } |
|
1253 | - |
|
1254 | - |
|
1255 | - /** |
|
1256 | - * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
1257 | - * can be easily used as the value of form input. |
|
1258 | - * |
|
1259 | - * @param string $field_name |
|
1260 | - * @return void |
|
1261 | - * @throws ReflectionException |
|
1262 | - * @throws InvalidArgumentException |
|
1263 | - * @throws InvalidInterfaceException |
|
1264 | - * @throws InvalidDataTypeException |
|
1265 | - * @throws EE_Error |
|
1266 | - */ |
|
1267 | - public function f($field_name) |
|
1268 | - { |
|
1269 | - $this->e($field_name, 'form_input'); |
|
1270 | - } |
|
1271 | - |
|
1272 | - |
|
1273 | - /** |
|
1274 | - * Same as `f()` but just returns the value instead of echoing it |
|
1275 | - * |
|
1276 | - * @param string $field_name |
|
1277 | - * @return string |
|
1278 | - * @throws ReflectionException |
|
1279 | - * @throws InvalidArgumentException |
|
1280 | - * @throws InvalidInterfaceException |
|
1281 | - * @throws InvalidDataTypeException |
|
1282 | - * @throws EE_Error |
|
1283 | - */ |
|
1284 | - public function get_f($field_name) |
|
1285 | - { |
|
1286 | - return (string) $this->get_pretty($field_name, 'form_input'); |
|
1287 | - } |
|
1288 | - |
|
1289 | - |
|
1290 | - /** |
|
1291 | - * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
1292 | - * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
1293 | - * to see what options are available. |
|
1294 | - * |
|
1295 | - * @param string $field_name |
|
1296 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
1297 | - * (in cases where the same property may be used for different outputs |
|
1298 | - * - i.e. datetime, money etc.) |
|
1299 | - * @return mixed |
|
1300 | - * @throws ReflectionException |
|
1301 | - * @throws InvalidArgumentException |
|
1302 | - * @throws InvalidInterfaceException |
|
1303 | - * @throws InvalidDataTypeException |
|
1304 | - * @throws EE_Error |
|
1305 | - */ |
|
1306 | - public function get_pretty($field_name, $extra_cache_ref = null) |
|
1307 | - { |
|
1308 | - return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
1309 | - } |
|
1310 | - |
|
1311 | - |
|
1312 | - /** |
|
1313 | - * This simply returns the datetime for the given field name |
|
1314 | - * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
1315 | - * (and the equivalent e_date, e_time, e_datetime). |
|
1316 | - * |
|
1317 | - * @access protected |
|
1318 | - * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
1319 | - * @param string $dt_frmt valid datetime format used for date |
|
1320 | - * (if '' then we just use the default on the field, |
|
1321 | - * if NULL we use the last-used format) |
|
1322 | - * @param string $tm_frmt Same as above except this is for time format |
|
1323 | - * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
1324 | - * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
1325 | - * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
1326 | - * if field is not a valid dtt field, or void if echoing |
|
1327 | - * @throws ReflectionException |
|
1328 | - * @throws InvalidArgumentException |
|
1329 | - * @throws InvalidInterfaceException |
|
1330 | - * @throws InvalidDataTypeException |
|
1331 | - * @throws EE_Error |
|
1332 | - */ |
|
1333 | - protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
|
1334 | - { |
|
1335 | - // clear cached property |
|
1336 | - $this->_clear_cached_property($field_name); |
|
1337 | - // reset format properties because they are used in get() |
|
1338 | - $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt; |
|
1339 | - $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt; |
|
1340 | - if ($echo) { |
|
1341 | - $this->e($field_name, $date_or_time); |
|
1342 | - return ''; |
|
1343 | - } |
|
1344 | - return $this->get($field_name, $date_or_time); |
|
1345 | - } |
|
1346 | - |
|
1347 | - |
|
1348 | - /** |
|
1349 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
1350 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
1351 | - * other echoes the pretty value for dtt) |
|
1352 | - * |
|
1353 | - * @param string $field_name name of model object datetime field holding the value |
|
1354 | - * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
1355 | - * @return string datetime value formatted |
|
1356 | - * @throws ReflectionException |
|
1357 | - * @throws InvalidArgumentException |
|
1358 | - * @throws InvalidInterfaceException |
|
1359 | - * @throws InvalidDataTypeException |
|
1360 | - * @throws EE_Error |
|
1361 | - */ |
|
1362 | - public function get_date($field_name, $format = '') |
|
1363 | - { |
|
1364 | - return $this->_get_datetime($field_name, $format, null, 'D'); |
|
1365 | - } |
|
1366 | - |
|
1367 | - |
|
1368 | - /** |
|
1369 | - * @param $field_name |
|
1370 | - * @param string $format |
|
1371 | - * @throws ReflectionException |
|
1372 | - * @throws InvalidArgumentException |
|
1373 | - * @throws InvalidInterfaceException |
|
1374 | - * @throws InvalidDataTypeException |
|
1375 | - * @throws EE_Error |
|
1376 | - */ |
|
1377 | - public function e_date($field_name, $format = '') |
|
1378 | - { |
|
1379 | - $this->_get_datetime($field_name, $format, null, 'D', true); |
|
1380 | - } |
|
1381 | - |
|
1382 | - |
|
1383 | - /** |
|
1384 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
1385 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
1386 | - * other echoes the pretty value for dtt) |
|
1387 | - * |
|
1388 | - * @param string $field_name name of model object datetime field holding the value |
|
1389 | - * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
1390 | - * @return string datetime value formatted |
|
1391 | - * @throws ReflectionException |
|
1392 | - * @throws InvalidArgumentException |
|
1393 | - * @throws InvalidInterfaceException |
|
1394 | - * @throws InvalidDataTypeException |
|
1395 | - * @throws EE_Error |
|
1396 | - */ |
|
1397 | - public function get_time($field_name, $format = '') |
|
1398 | - { |
|
1399 | - return $this->_get_datetime($field_name, null, $format, 'T'); |
|
1400 | - } |
|
1401 | - |
|
1402 | - |
|
1403 | - /** |
|
1404 | - * @param $field_name |
|
1405 | - * @param string $format |
|
1406 | - * @throws ReflectionException |
|
1407 | - * @throws InvalidArgumentException |
|
1408 | - * @throws InvalidInterfaceException |
|
1409 | - * @throws InvalidDataTypeException |
|
1410 | - * @throws EE_Error |
|
1411 | - */ |
|
1412 | - public function e_time($field_name, $format = '') |
|
1413 | - { |
|
1414 | - $this->_get_datetime($field_name, null, $format, 'T', true); |
|
1415 | - } |
|
1416 | - |
|
1417 | - |
|
1418 | - /** |
|
1419 | - * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
1420 | - * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
1421 | - * other echoes the pretty value for dtt) |
|
1422 | - * |
|
1423 | - * @param string $field_name name of model object datetime field holding the value |
|
1424 | - * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
|
1425 | - * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
|
1426 | - * @return string datetime value formatted |
|
1427 | - * @throws ReflectionException |
|
1428 | - * @throws InvalidArgumentException |
|
1429 | - * @throws InvalidInterfaceException |
|
1430 | - * @throws InvalidDataTypeException |
|
1431 | - * @throws EE_Error |
|
1432 | - */ |
|
1433 | - public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
1434 | - { |
|
1435 | - return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt); |
|
1436 | - } |
|
1437 | - |
|
1438 | - |
|
1439 | - /** |
|
1440 | - * @param string $field_name |
|
1441 | - * @param string $dt_frmt |
|
1442 | - * @param string $tm_frmt |
|
1443 | - * @throws ReflectionException |
|
1444 | - * @throws InvalidArgumentException |
|
1445 | - * @throws InvalidInterfaceException |
|
1446 | - * @throws InvalidDataTypeException |
|
1447 | - * @throws EE_Error |
|
1448 | - */ |
|
1449 | - public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
1450 | - { |
|
1451 | - $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true); |
|
1452 | - } |
|
1453 | - |
|
1454 | - |
|
1455 | - /** |
|
1456 | - * Get the i8ln value for a date using the WordPress @see date_i18n function. |
|
1457 | - * |
|
1458 | - * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
1459 | - * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
|
1460 | - * on the object will be used. |
|
1461 | - * @return string Date and time string in set locale or false if no field exists for the given |
|
1462 | - * @throws ReflectionException |
|
1463 | - * @throws InvalidArgumentException |
|
1464 | - * @throws InvalidInterfaceException |
|
1465 | - * @throws InvalidDataTypeException |
|
1466 | - * @throws EE_Error |
|
1467 | - * field name. |
|
1468 | - */ |
|
1469 | - public function get_i18n_datetime($field_name, $format = '') |
|
1470 | - { |
|
1471 | - $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
1472 | - return date_i18n( |
|
1473 | - $format, |
|
1474 | - EEH_DTT_Helper::get_timestamp_with_offset( |
|
1475 | - $this->get_raw($field_name), |
|
1476 | - $this->_timezone |
|
1477 | - ) |
|
1478 | - ); |
|
1479 | - } |
|
1480 | - |
|
1481 | - |
|
1482 | - /** |
|
1483 | - * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
1484 | - * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
1485 | - * thrown. |
|
1486 | - * |
|
1487 | - * @param string $field_name The field name being checked |
|
1488 | - * @throws ReflectionException |
|
1489 | - * @throws InvalidArgumentException |
|
1490 | - * @throws InvalidInterfaceException |
|
1491 | - * @throws InvalidDataTypeException |
|
1492 | - * @throws EE_Error |
|
1493 | - * @return EE_Datetime_Field |
|
1494 | - */ |
|
1495 | - protected function _get_dtt_field_settings($field_name) |
|
1496 | - { |
|
1497 | - $field = $this->_model->field_settings_for($field_name); |
|
1498 | - // check if field is dtt |
|
1499 | - if ($field instanceof EE_Datetime_Field) { |
|
1500 | - return $field; |
|
1501 | - } |
|
1502 | - throw new EE_Error( |
|
1503 | - sprintf( |
|
1504 | - esc_html__( |
|
1505 | - 'The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field. Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor', |
|
1506 | - 'event_espresso' |
|
1507 | - ), |
|
1508 | - $field_name, |
|
1509 | - self::_get_model_classname(get_class($this)) |
|
1510 | - ) |
|
1511 | - ); |
|
1512 | - } |
|
1513 | - |
|
1514 | - |
|
1515 | - |
|
1516 | - |
|
1517 | - /** |
|
1518 | - * NOTE ABOUT BELOW: |
|
1519 | - * These convenience date and time setters are for setting date and time independently. In other words you might |
|
1520 | - * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
1521 | - * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
|
1522 | - * method and make sure you send the entire datetime value for setting. |
|
1523 | - */ |
|
1524 | - /** |
|
1525 | - * sets the time on a datetime property |
|
1526 | - * |
|
1527 | - * @access protected |
|
1528 | - * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
1529 | - * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
|
1530 | - * @throws ReflectionException |
|
1531 | - * @throws InvalidArgumentException |
|
1532 | - * @throws InvalidInterfaceException |
|
1533 | - * @throws InvalidDataTypeException |
|
1534 | - * @throws EE_Error |
|
1535 | - */ |
|
1536 | - protected function _set_time_for($time, $fieldname) |
|
1537 | - { |
|
1538 | - $this->_set_date_time('T', $time, $fieldname); |
|
1539 | - } |
|
1540 | - |
|
1541 | - |
|
1542 | - /** |
|
1543 | - * sets the date on a datetime property |
|
1544 | - * |
|
1545 | - * @access protected |
|
1546 | - * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
1547 | - * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
|
1548 | - * @throws ReflectionException |
|
1549 | - * @throws InvalidArgumentException |
|
1550 | - * @throws InvalidInterfaceException |
|
1551 | - * @throws InvalidDataTypeException |
|
1552 | - * @throws EE_Error |
|
1553 | - */ |
|
1554 | - protected function _set_date_for($date, $fieldname) |
|
1555 | - { |
|
1556 | - $this->_set_date_time('D', $date, $fieldname); |
|
1557 | - } |
|
1558 | - |
|
1559 | - |
|
1560 | - /** |
|
1561 | - * This takes care of setting a date or time independently on a given model object property. This method also |
|
1562 | - * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
|
1563 | - * |
|
1564 | - * @access protected |
|
1565 | - * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
1566 | - * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
1567 | - * @param string $fieldname the name of the field the date OR time is being set on (must match a |
|
1568 | - * EE_Datetime_Field property) |
|
1569 | - * @throws ReflectionException |
|
1570 | - * @throws InvalidArgumentException |
|
1571 | - * @throws InvalidInterfaceException |
|
1572 | - * @throws InvalidDataTypeException |
|
1573 | - * @throws EE_Error |
|
1574 | - */ |
|
1575 | - protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
|
1576 | - { |
|
1577 | - $field = $this->_get_dtt_field_settings($fieldname); |
|
1578 | - $field->set_timezone($this->_timezone); |
|
1579 | - $field->set_date_format($this->_dt_frmt); |
|
1580 | - $field->set_time_format($this->_tm_frmt); |
|
1581 | - switch ($what) { |
|
1582 | - case 'T': |
|
1583 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_time( |
|
1584 | - $datetime_value, |
|
1585 | - $this->_fields[ $fieldname ] |
|
1586 | - ); |
|
1587 | - $this->_has_changes = true; |
|
1588 | - break; |
|
1589 | - case 'D': |
|
1590 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date( |
|
1591 | - $datetime_value, |
|
1592 | - $this->_fields[ $fieldname ] |
|
1593 | - ); |
|
1594 | - $this->_has_changes = true; |
|
1595 | - break; |
|
1596 | - case 'B': |
|
1597 | - $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value); |
|
1598 | - $this->_has_changes = true; |
|
1599 | - break; |
|
1600 | - } |
|
1601 | - $this->_clear_cached_property($fieldname); |
|
1602 | - } |
|
1603 | - |
|
1604 | - |
|
1605 | - /** |
|
1606 | - * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
1607 | - * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
1608 | - * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
1609 | - * that could lead to some unexpected results! |
|
1610 | - * |
|
1611 | - * @access public |
|
1612 | - * @param string $field_name This is the name of the field on the object that contains the date/time |
|
1613 | - * value being returned. |
|
1614 | - * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
1615 | - * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
1616 | - * @param string $prepend You can include something to prepend on the timestamp |
|
1617 | - * @param string $append You can include something to append on the timestamp |
|
1618 | - * @throws ReflectionException |
|
1619 | - * @throws InvalidArgumentException |
|
1620 | - * @throws InvalidInterfaceException |
|
1621 | - * @throws InvalidDataTypeException |
|
1622 | - * @throws EE_Error |
|
1623 | - * @return string timestamp |
|
1624 | - */ |
|
1625 | - public function display_in_my_timezone( |
|
1626 | - $field_name, |
|
1627 | - $callback = 'get_datetime', |
|
1628 | - $args = null, |
|
1629 | - $prepend = '', |
|
1630 | - $append = '' |
|
1631 | - ) { |
|
1632 | - $timezone = EEH_DTT_Helper::get_timezone(); |
|
1633 | - if ($timezone === $this->_timezone) { |
|
1634 | - return ''; |
|
1635 | - } |
|
1636 | - $original_timezone = $this->_timezone; |
|
1637 | - $this->set_timezone($timezone); |
|
1638 | - $fn = (array) $field_name; |
|
1639 | - $args = array_merge($fn, (array) $args); |
|
1640 | - if (! method_exists($this, $callback)) { |
|
1641 | - throw new EE_Error( |
|
1642 | - sprintf( |
|
1643 | - esc_html__( |
|
1644 | - 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
1645 | - 'event_espresso' |
|
1646 | - ), |
|
1647 | - $callback |
|
1648 | - ) |
|
1649 | - ); |
|
1650 | - } |
|
1651 | - $args = (array) $args; |
|
1652 | - $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
1653 | - $this->set_timezone($original_timezone); |
|
1654 | - return $return; |
|
1655 | - } |
|
1656 | - |
|
1657 | - |
|
1658 | - /** |
|
1659 | - * Deletes this model object. |
|
1660 | - * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
1661 | - * override |
|
1662 | - * `EE_Base_Class::_delete` NOT this class. |
|
1663 | - * |
|
1664 | - * @return boolean | int |
|
1665 | - * @throws ReflectionException |
|
1666 | - * @throws InvalidArgumentException |
|
1667 | - * @throws InvalidInterfaceException |
|
1668 | - * @throws InvalidDataTypeException |
|
1669 | - * @throws EE_Error |
|
1670 | - */ |
|
1671 | - public function delete() |
|
1672 | - { |
|
1673 | - /** |
|
1674 | - * Called just before the `EE_Base_Class::_delete` method call. |
|
1675 | - * Note: |
|
1676 | - * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
1677 | - * should be aware that `_delete` may not always result in a permanent delete. |
|
1678 | - * For example, `EE_Soft_Delete_Base_Class::_delete` |
|
1679 | - * soft deletes (trash) the object and does not permanently delete it. |
|
1680 | - * |
|
1681 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
1682 | - */ |
|
1683 | - do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
1684 | - $result = $this->_delete(); |
|
1685 | - /** |
|
1686 | - * Called just after the `EE_Base_Class::_delete` method call. |
|
1687 | - * Note: |
|
1688 | - * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
1689 | - * should be aware that `_delete` may not always result in a permanent delete. |
|
1690 | - * For example `EE_Soft_Base_Class::_delete` |
|
1691 | - * soft deletes (trash) the object and does not permanently delete it. |
|
1692 | - * |
|
1693 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
1694 | - * @param boolean $result |
|
1695 | - */ |
|
1696 | - do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
1697 | - return $result; |
|
1698 | - } |
|
1699 | - |
|
1700 | - |
|
1701 | - /** |
|
1702 | - * Calls the specific delete method for the instantiated class. |
|
1703 | - * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
1704 | - * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
1705 | - * `EE_Base_Class::delete` |
|
1706 | - * |
|
1707 | - * @return bool|int |
|
1708 | - * @throws ReflectionException |
|
1709 | - * @throws InvalidArgumentException |
|
1710 | - * @throws InvalidInterfaceException |
|
1711 | - * @throws InvalidDataTypeException |
|
1712 | - * @throws EE_Error |
|
1713 | - */ |
|
1714 | - protected function _delete() |
|
1715 | - { |
|
1716 | - return $this->delete_permanently(); |
|
1717 | - } |
|
1718 | - |
|
1719 | - |
|
1720 | - /** |
|
1721 | - * Deletes this model object permanently from db |
|
1722 | - * (but keep in mind related models may block the delete and return an error) |
|
1723 | - * |
|
1724 | - * @return bool | int |
|
1725 | - * @throws ReflectionException |
|
1726 | - * @throws InvalidArgumentException |
|
1727 | - * @throws InvalidInterfaceException |
|
1728 | - * @throws InvalidDataTypeException |
|
1729 | - * @throws EE_Error |
|
1730 | - */ |
|
1731 | - public function delete_permanently() |
|
1732 | - { |
|
1733 | - /** |
|
1734 | - * Called just before HARD deleting a model object |
|
1735 | - * |
|
1736 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
1737 | - */ |
|
1738 | - do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
1739 | - $result = $this->_model->delete_permanently_by_ID($this->ID()); |
|
1740 | - $this->refresh_cache_of_related_objects(); |
|
1741 | - /** |
|
1742 | - * Called just after HARD deleting a model object |
|
1743 | - * |
|
1744 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
1745 | - * @param boolean $result |
|
1746 | - */ |
|
1747 | - do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
1748 | - return $result; |
|
1749 | - } |
|
1750 | - |
|
1751 | - |
|
1752 | - /** |
|
1753 | - * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
1754 | - * related model objects |
|
1755 | - * |
|
1756 | - * @throws ReflectionException |
|
1757 | - * @throws InvalidArgumentException |
|
1758 | - * @throws InvalidInterfaceException |
|
1759 | - * @throws InvalidDataTypeException |
|
1760 | - * @throws EE_Error |
|
1761 | - */ |
|
1762 | - public function refresh_cache_of_related_objects() |
|
1763 | - { |
|
1764 | - foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
1765 | - if (! empty($this->_model_relations[ $relation_name ])) { |
|
1766 | - $related_objects = $this->_model_relations[ $relation_name ]; |
|
1767 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
1768 | - // this relation only stores a single model object, not an array |
|
1769 | - // but let's make it consistent |
|
1770 | - $related_objects = array($related_objects); |
|
1771 | - } |
|
1772 | - foreach ($related_objects as $related_object) { |
|
1773 | - // only refresh their cache if they're in memory |
|
1774 | - if ($related_object instanceof EE_Base_Class) { |
|
1775 | - $related_object->clear_cache( |
|
1776 | - $this->_model->get_this_model_name(), |
|
1777 | - $this |
|
1778 | - ); |
|
1779 | - } |
|
1780 | - } |
|
1781 | - } |
|
1782 | - } |
|
1783 | - } |
|
1784 | - |
|
1785 | - |
|
1786 | - /** |
|
1787 | - * Saves this object to the database. An array may be supplied to set some values on this |
|
1788 | - * object just before saving. |
|
1789 | - * |
|
1790 | - * @access public |
|
1791 | - * @param array $set_cols_n_values keys are field names, values are their new values, |
|
1792 | - * if provided during the save() method (often client code will change the fields' |
|
1793 | - * values before calling save) |
|
1794 | - * @throws InvalidArgumentException |
|
1795 | - * @throws InvalidInterfaceException |
|
1796 | - * @throws InvalidDataTypeException |
|
1797 | - * @throws EE_Error |
|
1798 | - * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
1799 | - * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
1800 | - * @throws ReflectionException |
|
1801 | - * @throws ReflectionException |
|
1802 | - * @throws ReflectionException |
|
1803 | - */ |
|
1804 | - public function save($set_cols_n_values = array()) |
|
1805 | - { |
|
1806 | - /** |
|
1807 | - * Filters the fields we're about to save on the model object |
|
1808 | - * |
|
1809 | - * @param array $set_cols_n_values |
|
1810 | - * @param EE_Base_Class $model_object |
|
1811 | - */ |
|
1812 | - $set_cols_n_values = (array) apply_filters( |
|
1813 | - 'FHEE__EE_Base_Class__save__set_cols_n_values', |
|
1814 | - $set_cols_n_values, |
|
1815 | - $this |
|
1816 | - ); |
|
1817 | - // set attributes as provided in $set_cols_n_values |
|
1818 | - foreach ($set_cols_n_values as $column => $value) { |
|
1819 | - $this->set($column, $value); |
|
1820 | - } |
|
1821 | - // no changes ? then don't do anything |
|
1822 | - if (! $this->_has_changes && $this->ID() && $this->_model->get_primary_key_field()->is_auto_increment()) { |
|
1823 | - return 0; |
|
1824 | - } |
|
1825 | - /** |
|
1826 | - * Saving a model object. |
|
1827 | - * Before we perform a save, this action is fired. |
|
1828 | - * |
|
1829 | - * @param EE_Base_Class $model_object the model object about to be saved. |
|
1830 | - */ |
|
1831 | - do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
1832 | - if (! $this->allow_persist()) { |
|
1833 | - return 0; |
|
1834 | - } |
|
1835 | - // now get current attribute values |
|
1836 | - $save_cols_n_values = $this->_fields; |
|
1837 | - // if the object already has an ID, update it. Otherwise, insert it |
|
1838 | - // also: change the assumption about values passed to the model NOT being prepare dby the model object. |
|
1839 | - // They have been |
|
1840 | - $old_assumption_concerning_value_preparation = $this->_model |
|
1841 | - ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
1842 | - $this->_model->assume_values_already_prepared_by_model_object(true); |
|
1843 | - // does this model have an autoincrement PK? |
|
1844 | - if ($this->_model->has_primary_key_field()) { |
|
1845 | - if ($this->_model->get_primary_key_field()->is_auto_increment()) { |
|
1846 | - // ok check if it's set, if so: update; if not, insert |
|
1847 | - if (! empty($save_cols_n_values[ $this->_model->primary_key_name() ])) { |
|
1848 | - $results = $this->_model->update_by_ID($save_cols_n_values, $this->ID()); |
|
1849 | - } else { |
|
1850 | - unset($save_cols_n_values[ $this->_model->primary_key_name() ]); |
|
1851 | - $results = $this->_model->insert($save_cols_n_values); |
|
1852 | - if ($results) { |
|
1853 | - // if successful, set the primary key |
|
1854 | - // but don't use the normal SET method, because it will check if |
|
1855 | - // an item with the same ID exists in the mapper & db, then |
|
1856 | - // will find it in the db (because we just added it) and THAT object |
|
1857 | - // will get added to the mapper before we can add this one! |
|
1858 | - // but if we just avoid using the SET method, all that headache can be avoided |
|
1859 | - $pk_field_name = $this->_model->primary_key_name(); |
|
1860 | - $this->_fields[ $pk_field_name ] = $results; |
|
1861 | - $this->_clear_cached_property($pk_field_name); |
|
1862 | - $this->_model->add_to_entity_map($this); |
|
1863 | - $this->_update_cached_related_model_objs_fks(); |
|
1864 | - } |
|
1865 | - } |
|
1866 | - } else {// PK is NOT auto-increment |
|
1867 | - // so check if one like it already exists in the db |
|
1868 | - if ($this->_model->exists_by_ID($this->ID())) { |
|
1869 | - if (WP_DEBUG && ! $this->in_entity_map()) { |
|
1870 | - throw new EE_Error( |
|
1871 | - sprintf( |
|
1872 | - esc_html__( |
|
1873 | - 'Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s', |
|
1874 | - 'event_espresso' |
|
1875 | - ), |
|
1876 | - get_class($this), |
|
1877 | - get_class($this->_model) . '::instance()->add_to_entity_map()', |
|
1878 | - get_class($this->_model) . '::instance()->get_one_by_ID()', |
|
1879 | - '<br />' |
|
1880 | - ) |
|
1881 | - ); |
|
1882 | - } |
|
1883 | - $results = $this->_model->update_by_ID($save_cols_n_values, $this->ID()); |
|
1884 | - } else { |
|
1885 | - $results = $this->_model->insert($save_cols_n_values); |
|
1886 | - $this->_update_cached_related_model_objs_fks(); |
|
1887 | - } |
|
1888 | - } |
|
1889 | - } else {// there is NO primary key |
|
1890 | - $already_in_db = false; |
|
1891 | - foreach ($this->_model->unique_indexes() as $index) { |
|
1892 | - $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
1893 | - if ($this->_model->exists(array($uniqueness_where_params))) { |
|
1894 | - $already_in_db = true; |
|
1895 | - } |
|
1896 | - } |
|
1897 | - if ($already_in_db) { |
|
1898 | - $combined_pk_fields_n_values = array_intersect_key( |
|
1899 | - $save_cols_n_values, |
|
1900 | - $this->_model->get_combined_primary_key_fields() |
|
1901 | - ); |
|
1902 | - $results = $this->_model->update( |
|
1903 | - $save_cols_n_values, |
|
1904 | - $combined_pk_fields_n_values |
|
1905 | - ); |
|
1906 | - } else { |
|
1907 | - $results = $this->_model->insert($save_cols_n_values); |
|
1908 | - } |
|
1909 | - } |
|
1910 | - // restore the old assumption about values being prepared by the model object |
|
1911 | - $this->_model->assume_values_already_prepared_by_model_object( |
|
1912 | - $old_assumption_concerning_value_preparation |
|
1913 | - ); |
|
1914 | - /** |
|
1915 | - * After saving the model object this action is called |
|
1916 | - * |
|
1917 | - * @param EE_Base_Class $model_object which was just saved |
|
1918 | - * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
1919 | - * the new ID (or 0 if an error occurred and it wasn't updated) |
|
1920 | - */ |
|
1921 | - do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
1922 | - $this->_has_changes = false; |
|
1923 | - return $results; |
|
1924 | - } |
|
1925 | - |
|
1926 | - |
|
1927 | - /** |
|
1928 | - * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
1929 | - * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
1930 | - * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
1931 | - * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
1932 | - * 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 |
|
1933 | - * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
1934 | - * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
1935 | - * |
|
1936 | - * @return void |
|
1937 | - * @throws ReflectionException |
|
1938 | - * @throws InvalidArgumentException |
|
1939 | - * @throws InvalidInterfaceException |
|
1940 | - * @throws InvalidDataTypeException |
|
1941 | - * @throws EE_Error |
|
1942 | - */ |
|
1943 | - protected function _update_cached_related_model_objs_fks() |
|
1944 | - { |
|
1945 | - foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
1946 | - if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
1947 | - foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
1948 | - $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
1949 | - $this->_model->get_this_model_name() |
|
1950 | - ); |
|
1951 | - $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
1952 | - if ($related_model_obj_in_cache->ID()) { |
|
1953 | - $related_model_obj_in_cache->save(); |
|
1954 | - } |
|
1955 | - } |
|
1956 | - } |
|
1957 | - } |
|
1958 | - } |
|
1959 | - |
|
1960 | - |
|
1961 | - /** |
|
1962 | - * Saves this model object and its NEW cached relations to the database. |
|
1963 | - * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
1964 | - * In order for that to work, we would need to mark model objects as dirty/clean... |
|
1965 | - * because otherwise, there's a potential for infinite looping of saving |
|
1966 | - * Saves the cached related model objects, and ensures the relation between them |
|
1967 | - * and this object and properly setup |
|
1968 | - * |
|
1969 | - * @return int ID of new model object on save; 0 on failure+ |
|
1970 | - * @throws ReflectionException |
|
1971 | - * @throws InvalidArgumentException |
|
1972 | - * @throws InvalidInterfaceException |
|
1973 | - * @throws InvalidDataTypeException |
|
1974 | - * @throws EE_Error |
|
1975 | - */ |
|
1976 | - public function save_new_cached_related_model_objs() |
|
1977 | - { |
|
1978 | - // make sure this has been saved |
|
1979 | - if (! $this->ID()) { |
|
1980 | - $id = $this->save(); |
|
1981 | - } else { |
|
1982 | - $id = $this->ID(); |
|
1983 | - } |
|
1984 | - // now save all the NEW cached model objects (ie they don't exist in the DB) |
|
1985 | - foreach ($this->_model->relation_settings() as $relationName => $relationObj) { |
|
1986 | - if ($this->_model_relations[ $relationName ]) { |
|
1987 | - // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
1988 | - // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
1989 | - /* @var $related_model_obj EE_Base_Class */ |
|
1990 | - if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
1991 | - // add a relation to that relation type (which saves the appropriate thing in the process) |
|
1992 | - // but ONLY if it DOES NOT exist in the DB |
|
1993 | - $related_model_obj = $this->_model_relations[ $relationName ]; |
|
1994 | - // if( ! $related_model_obj->ID()){ |
|
1995 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
1996 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
1997 | - // } |
|
1998 | - } else { |
|
1999 | - foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
2000 | - // add a relation to that relation type (which saves the appropriate thing in the process) |
|
2001 | - // but ONLY if it DOES NOT exist in the DB |
|
2002 | - // if( ! $related_model_obj->ID()){ |
|
2003 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
2004 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
2005 | - // } |
|
2006 | - } |
|
2007 | - } |
|
2008 | - } |
|
2009 | - } |
|
2010 | - return $id; |
|
2011 | - } |
|
2012 | - |
|
2013 | - |
|
2014 | - /** |
|
2015 | - * for getting a model while instantiated. |
|
2016 | - * |
|
2017 | - * @return EEM_Base | EEM_CPT_Base |
|
2018 | - * @throws ReflectionException |
|
2019 | - * @throws InvalidArgumentException |
|
2020 | - * @throws InvalidInterfaceException |
|
2021 | - * @throws InvalidDataTypeException |
|
2022 | - * @throws EE_Error |
|
2023 | - */ |
|
2024 | - public function get_model() |
|
2025 | - { |
|
2026 | - if (! $this->_model) { |
|
2027 | - $modelName = self::_get_model_classname(get_class($this)); |
|
2028 | - $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
2029 | - } |
|
2030 | - return $this->_model; |
|
2031 | - } |
|
2032 | - |
|
2033 | - |
|
2034 | - /** |
|
2035 | - * @param $props_n_values |
|
2036 | - * @param $classname |
|
2037 | - * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
2038 | - * @throws ReflectionException |
|
2039 | - * @throws InvalidArgumentException |
|
2040 | - * @throws InvalidInterfaceException |
|
2041 | - * @throws InvalidDataTypeException |
|
2042 | - * @throws EE_Error |
|
2043 | - */ |
|
2044 | - protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
2045 | - { |
|
2046 | - // TODO: will not work for Term_Relationships because they have no PK! |
|
2047 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
2048 | - if ( |
|
2049 | - array_key_exists($primary_id_ref, $props_n_values) |
|
2050 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
2051 | - ) { |
|
2052 | - $id = $props_n_values[ $primary_id_ref ]; |
|
2053 | - return self::_get_model($classname)->get_from_entity_map($id); |
|
2054 | - } |
|
2055 | - return false; |
|
2056 | - } |
|
2057 | - |
|
2058 | - |
|
2059 | - /** |
|
2060 | - * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
2061 | - * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
2062 | - * 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 |
|
2063 | - * we return false. |
|
2064 | - * |
|
2065 | - * @param array $props_n_values incoming array of properties and their values |
|
2066 | - * @param string $classname the classname of the child class |
|
2067 | - * @param null $timezone |
|
2068 | - * @param array $date_formats incoming date_formats in an array where the first value is the |
|
2069 | - * date_format and the second value is the time format |
|
2070 | - * @return mixed (EE_Base_Class|bool) |
|
2071 | - * @throws InvalidArgumentException |
|
2072 | - * @throws InvalidInterfaceException |
|
2073 | - * @throws InvalidDataTypeException |
|
2074 | - * @throws EE_Error |
|
2075 | - * @throws ReflectionException |
|
2076 | - * @throws ReflectionException |
|
2077 | - * @throws ReflectionException |
|
2078 | - */ |
|
2079 | - protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
2080 | - { |
|
2081 | - $existing = null; |
|
2082 | - $model = self::_get_model($classname, $timezone); |
|
2083 | - if ($model->has_primary_key_field()) { |
|
2084 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
2085 | - if ( |
|
2086 | - array_key_exists($primary_id_ref, $props_n_values) |
|
2087 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
2088 | - ) { |
|
2089 | - $existing = $model->get_one_by_ID( |
|
2090 | - $props_n_values[ $primary_id_ref ] |
|
2091 | - ); |
|
2092 | - } |
|
2093 | - } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
2094 | - // no primary key on this model, but there's still a matching item in the DB |
|
2095 | - $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
2096 | - self::_get_model($classname, $timezone) |
|
2097 | - ->get_index_primary_key_string($props_n_values) |
|
2098 | - ); |
|
2099 | - } |
|
2100 | - if ($existing) { |
|
2101 | - // set date formats if present before setting values |
|
2102 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
2103 | - $existing->set_date_format($date_formats[0]); |
|
2104 | - $existing->set_time_format($date_formats[1]); |
|
2105 | - } else { |
|
2106 | - // set default formats for date and time |
|
2107 | - $existing->set_date_format(get_option('date_format')); |
|
2108 | - $existing->set_time_format(get_option('time_format')); |
|
2109 | - } |
|
2110 | - foreach ($props_n_values as $property => $field_value) { |
|
2111 | - $existing->set($property, $field_value); |
|
2112 | - } |
|
2113 | - return $existing; |
|
2114 | - } |
|
2115 | - return false; |
|
2116 | - } |
|
2117 | - |
|
2118 | - |
|
2119 | - /** |
|
2120 | - * Gets the EEM_*_Model for this class |
|
2121 | - * |
|
2122 | - * @access public now, as this is more convenient |
|
2123 | - * @param $classname |
|
2124 | - * @param null $timezone |
|
2125 | - * @throws ReflectionException |
|
2126 | - * @throws InvalidArgumentException |
|
2127 | - * @throws InvalidInterfaceException |
|
2128 | - * @throws InvalidDataTypeException |
|
2129 | - * @throws EE_Error |
|
2130 | - * @return EEM_Base |
|
2131 | - */ |
|
2132 | - protected static function _get_model($classname, $timezone = null) |
|
2133 | - { |
|
2134 | - // find model for this class |
|
2135 | - if (! $classname) { |
|
2136 | - throw new EE_Error( |
|
2137 | - sprintf( |
|
2138 | - esc_html__( |
|
2139 | - 'What were you thinking calling _get_model(%s)?? You need to specify the class name', |
|
2140 | - 'event_espresso' |
|
2141 | - ), |
|
2142 | - $classname |
|
2143 | - ) |
|
2144 | - ); |
|
2145 | - } |
|
2146 | - $modelName = self::_get_model_classname($classname); |
|
2147 | - return self::_get_model_instance_with_name($modelName, $timezone); |
|
2148 | - } |
|
2149 | - |
|
2150 | - |
|
2151 | - /** |
|
2152 | - * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
2153 | - * |
|
2154 | - * @param string $model_classname |
|
2155 | - * @param null $timezone |
|
2156 | - * @return EEM_Base |
|
2157 | - * @throws ReflectionException |
|
2158 | - * @throws InvalidArgumentException |
|
2159 | - * @throws InvalidInterfaceException |
|
2160 | - * @throws InvalidDataTypeException |
|
2161 | - * @throws EE_Error |
|
2162 | - */ |
|
2163 | - protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
2164 | - { |
|
2165 | - $model_classname = str_replace('EEM_', '', $model_classname); |
|
2166 | - $model = EE_Registry::instance()->load_model($model_classname); |
|
2167 | - $model->set_timezone($timezone); |
|
2168 | - return $model; |
|
2169 | - } |
|
2170 | - |
|
2171 | - |
|
2172 | - /** |
|
2173 | - * If a model name is provided (eg Registration), gets the model classname for that model. |
|
2174 | - * Also works if a model class's classname is provided (eg EE_Registration). |
|
2175 | - * |
|
2176 | - * @param null $model_name |
|
2177 | - * @return string like EEM_Attendee |
|
2178 | - */ |
|
2179 | - private static function _get_model_classname($model_name = null) |
|
2180 | - { |
|
2181 | - if (strpos($model_name, 'EE_') === 0) { |
|
2182 | - $model_classname = str_replace('EE_', 'EEM_', $model_name); |
|
2183 | - } else { |
|
2184 | - $model_classname = 'EEM_' . $model_name; |
|
2185 | - } |
|
2186 | - return $model_classname; |
|
2187 | - } |
|
2188 | - |
|
2189 | - |
|
2190 | - /** |
|
2191 | - * returns the name of the primary key attribute |
|
2192 | - * |
|
2193 | - * @param null $classname |
|
2194 | - * @throws ReflectionException |
|
2195 | - * @throws InvalidArgumentException |
|
2196 | - * @throws InvalidInterfaceException |
|
2197 | - * @throws InvalidDataTypeException |
|
2198 | - * @throws EE_Error |
|
2199 | - * @return string |
|
2200 | - */ |
|
2201 | - protected static function _get_primary_key_name($classname = null) |
|
2202 | - { |
|
2203 | - if (! $classname) { |
|
2204 | - throw new EE_Error( |
|
2205 | - sprintf( |
|
2206 | - esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
|
2207 | - $classname |
|
2208 | - ) |
|
2209 | - ); |
|
2210 | - } |
|
2211 | - return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
2212 | - } |
|
2213 | - |
|
2214 | - |
|
2215 | - /** |
|
2216 | - * Gets the value of the primary key. |
|
2217 | - * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
2218 | - * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
2219 | - * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
2220 | - * |
|
2221 | - * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
2222 | - * @throws ReflectionException |
|
2223 | - * @throws InvalidArgumentException |
|
2224 | - * @throws InvalidInterfaceException |
|
2225 | - * @throws InvalidDataTypeException |
|
2226 | - * @throws EE_Error |
|
2227 | - */ |
|
2228 | - public function ID() |
|
2229 | - { |
|
2230 | - // now that we know the name of the variable, use a variable variable to get its value and return its |
|
2231 | - if ($this->_model->has_primary_key_field()) { |
|
2232 | - return $this->_fields[ $this->_model->primary_key_name() ]; |
|
2233 | - } |
|
2234 | - return $this->_model->get_index_primary_key_string($this->_fields); |
|
2235 | - } |
|
2236 | - |
|
2237 | - |
|
2238 | - /** |
|
2239 | - * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
2240 | - * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
2241 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
2242 | - * |
|
2243 | - * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
2244 | - * @param string $relationName eg 'Events','Question',etc. |
|
2245 | - * an attendee to a group, you also want to specify which role they |
|
2246 | - * will have in that group. So you would use this parameter to |
|
2247 | - * specify array('role-column-name'=>'role-id') |
|
2248 | - * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
2249 | - * allow you to further constrict the relation to being added. |
|
2250 | - * However, keep in mind that the columns (keys) given must match a |
|
2251 | - * column on the JOIN table and currently only the HABTM models |
|
2252 | - * accept these additional conditions. Also remember that if an |
|
2253 | - * exact match isn't found for these extra cols/val pairs, then a |
|
2254 | - * NEW row is created in the join table. |
|
2255 | - * @param null $cache_id |
|
2256 | - * @throws ReflectionException |
|
2257 | - * @throws InvalidArgumentException |
|
2258 | - * @throws InvalidInterfaceException |
|
2259 | - * @throws InvalidDataTypeException |
|
2260 | - * @throws EE_Error |
|
2261 | - * @return EE_Base_Class the object the relation was added to |
|
2262 | - */ |
|
2263 | - public function _add_relation_to( |
|
2264 | - $otherObjectModelObjectOrID, |
|
2265 | - $relationName, |
|
2266 | - $extra_join_model_fields_n_values = array(), |
|
2267 | - $cache_id = null |
|
2268 | - ) { |
|
2269 | - // if this thing exists in the DB, save the relation to the DB |
|
2270 | - if ($this->ID()) { |
|
2271 | - $otherObject = $this->_model->add_relationship_to( |
|
2272 | - $this, |
|
2273 | - $otherObjectModelObjectOrID, |
|
2274 | - $relationName, |
|
2275 | - $extra_join_model_fields_n_values |
|
2276 | - ); |
|
2277 | - // clear cache so future get_many_related and get_first_related() return new results. |
|
2278 | - $this->clear_cache($relationName, $otherObject, true); |
|
2279 | - if ($otherObject instanceof EE_Base_Class) { |
|
2280 | - $otherObject->clear_cache($this->_model->get_this_model_name(), $this); |
|
2281 | - } |
|
2282 | - } else { |
|
2283 | - // this thing doesn't exist in the DB, so just cache it |
|
2284 | - if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
2285 | - throw new EE_Error( |
|
2286 | - sprintf( |
|
2287 | - esc_html__( |
|
2288 | - 'Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s', |
|
2289 | - 'event_espresso' |
|
2290 | - ), |
|
2291 | - $otherObjectModelObjectOrID, |
|
2292 | - get_class($this) |
|
2293 | - ) |
|
2294 | - ); |
|
2295 | - } |
|
2296 | - $otherObject = $otherObjectModelObjectOrID; |
|
2297 | - $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
2298 | - } |
|
2299 | - if ($otherObject instanceof EE_Base_Class) { |
|
2300 | - // fix the reciprocal relation too |
|
2301 | - if ($otherObject->ID()) { |
|
2302 | - // its saved so assumed relations exist in the DB, so we can just |
|
2303 | - // clear the cache so future queries use the updated info in the DB |
|
2304 | - $otherObject->clear_cache( |
|
2305 | - $this->_model->get_this_model_name(), |
|
2306 | - null, |
|
2307 | - true |
|
2308 | - ); |
|
2309 | - } else { |
|
2310 | - // it's not saved, so it caches relations like this |
|
2311 | - $otherObject->cache($this->_model->get_this_model_name(), $this); |
|
2312 | - } |
|
2313 | - } |
|
2314 | - return $otherObject; |
|
2315 | - } |
|
2316 | - |
|
2317 | - |
|
2318 | - /** |
|
2319 | - * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
2320 | - * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
2321 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
2322 | - * from the cache |
|
2323 | - * |
|
2324 | - * @param mixed $otherObjectModelObjectOrID |
|
2325 | - * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
2326 | - * to the DB yet |
|
2327 | - * @param string $relationName |
|
2328 | - * @param array $where_query |
|
2329 | - * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
2330 | - * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
2331 | - * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
2332 | - * remember that if an exact match isn't found for these extra cols/val pairs, then no row is |
|
2333 | - * deleted. |
|
2334 | - * @return EE_Base_Class the relation was removed from |
|
2335 | - * @throws ReflectionException |
|
2336 | - * @throws InvalidArgumentException |
|
2337 | - * @throws InvalidInterfaceException |
|
2338 | - * @throws InvalidDataTypeException |
|
2339 | - * @throws EE_Error |
|
2340 | - */ |
|
2341 | - public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
2342 | - { |
|
2343 | - if ($this->ID()) { |
|
2344 | - // if this exists in the DB, save the relation change to the DB too |
|
2345 | - $otherObject = $this->_model->remove_relationship_to( |
|
2346 | - $this, |
|
2347 | - $otherObjectModelObjectOrID, |
|
2348 | - $relationName, |
|
2349 | - $where_query |
|
2350 | - ); |
|
2351 | - $this->clear_cache( |
|
2352 | - $relationName, |
|
2353 | - $otherObject |
|
2354 | - ); |
|
2355 | - } else { |
|
2356 | - // this doesn't exist in the DB, just remove it from the cache |
|
2357 | - $otherObject = $this->clear_cache( |
|
2358 | - $relationName, |
|
2359 | - $otherObjectModelObjectOrID |
|
2360 | - ); |
|
2361 | - } |
|
2362 | - if ($otherObject instanceof EE_Base_Class) { |
|
2363 | - $otherObject->clear_cache( |
|
2364 | - $this->_model->get_this_model_name(), |
|
2365 | - $this |
|
2366 | - ); |
|
2367 | - } |
|
2368 | - return $otherObject; |
|
2369 | - } |
|
2370 | - |
|
2371 | - |
|
2372 | - /** |
|
2373 | - * Removes ALL the related things for the $relationName. |
|
2374 | - * |
|
2375 | - * @param string $relationName |
|
2376 | - * @param array $where_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
2377 | - * @return EE_Base_Class |
|
2378 | - * @throws ReflectionException |
|
2379 | - * @throws InvalidArgumentException |
|
2380 | - * @throws InvalidInterfaceException |
|
2381 | - * @throws InvalidDataTypeException |
|
2382 | - * @throws EE_Error |
|
2383 | - */ |
|
2384 | - public function _remove_relations($relationName, $where_query_params = array()) |
|
2385 | - { |
|
2386 | - if ($this->ID()) { |
|
2387 | - // if this exists in the DB, save the relation change to the DB too |
|
2388 | - $otherObjects = $this->_model->remove_relations( |
|
2389 | - $this, |
|
2390 | - $relationName, |
|
2391 | - $where_query_params |
|
2392 | - ); |
|
2393 | - $this->clear_cache( |
|
2394 | - $relationName, |
|
2395 | - null, |
|
2396 | - true |
|
2397 | - ); |
|
2398 | - } else { |
|
2399 | - // this doesn't exist in the DB, just remove it from the cache |
|
2400 | - $otherObjects = $this->clear_cache( |
|
2401 | - $relationName, |
|
2402 | - null, |
|
2403 | - true |
|
2404 | - ); |
|
2405 | - } |
|
2406 | - if (is_array($otherObjects)) { |
|
2407 | - foreach ($otherObjects as $otherObject) { |
|
2408 | - $otherObject->clear_cache( |
|
2409 | - $this->_model->get_this_model_name(), |
|
2410 | - $this |
|
2411 | - ); |
|
2412 | - } |
|
2413 | - } |
|
2414 | - return $otherObjects; |
|
2415 | - } |
|
2416 | - |
|
2417 | - |
|
2418 | - /** |
|
2419 | - * Gets all the related model objects of the specified type. Eg, if the current class if |
|
2420 | - * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
2421 | - * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
2422 | - * because we want to get even deleted items etc. |
|
2423 | - * |
|
2424 | - * @param string $relationName key in the model's _model_relations array |
|
2425 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
2426 | - * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary |
|
2427 | - * keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these |
|
2428 | - * results if you want IDs |
|
2429 | - * @throws ReflectionException |
|
2430 | - * @throws InvalidArgumentException |
|
2431 | - * @throws InvalidInterfaceException |
|
2432 | - * @throws InvalidDataTypeException |
|
2433 | - * @throws EE_Error |
|
2434 | - */ |
|
2435 | - public function get_many_related($relationName, $query_params = array()) |
|
2436 | - { |
|
2437 | - if ($this->ID()) { |
|
2438 | - // this exists in the DB, so get the related things from either the cache or the DB |
|
2439 | - // if there are query parameters, forget about caching the related model objects. |
|
2440 | - if ($query_params) { |
|
2441 | - $related_model_objects = $this->_model->get_all_related( |
|
2442 | - $this, |
|
2443 | - $relationName, |
|
2444 | - $query_params |
|
2445 | - ); |
|
2446 | - } else { |
|
2447 | - // did we already cache the result of this query? |
|
2448 | - $cached_results = $this->get_all_from_cache($relationName); |
|
2449 | - if (! $cached_results) { |
|
2450 | - $related_model_objects = $this->_model->get_all_related( |
|
2451 | - $this, |
|
2452 | - $relationName, |
|
2453 | - $query_params |
|
2454 | - ); |
|
2455 | - // if no query parameters were passed, then we got all the related model objects |
|
2456 | - // for that relation. We can cache them then. |
|
2457 | - foreach ($related_model_objects as $related_model_object) { |
|
2458 | - $this->cache($relationName, $related_model_object); |
|
2459 | - } |
|
2460 | - } else { |
|
2461 | - $related_model_objects = $cached_results; |
|
2462 | - } |
|
2463 | - } |
|
2464 | - } else { |
|
2465 | - // this doesn't exist in the DB, so just get the related things from the cache |
|
2466 | - $related_model_objects = $this->get_all_from_cache($relationName); |
|
2467 | - } |
|
2468 | - return $related_model_objects; |
|
2469 | - } |
|
2470 | - |
|
2471 | - |
|
2472 | - /** |
|
2473 | - * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
2474 | - * unless otherwise specified in the $query_params |
|
2475 | - * |
|
2476 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
2477 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2478 | - * @param string $field_to_count name of field to count by. By default, uses primary key |
|
2479 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
2480 | - * that by the setting $distinct to TRUE; |
|
2481 | - * @return int |
|
2482 | - * @throws ReflectionException |
|
2483 | - * @throws InvalidArgumentException |
|
2484 | - * @throws InvalidInterfaceException |
|
2485 | - * @throws InvalidDataTypeException |
|
2486 | - * @throws EE_Error |
|
2487 | - */ |
|
2488 | - public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
2489 | - { |
|
2490 | - return $this->_model->count_related( |
|
2491 | - $this, |
|
2492 | - $relation_name, |
|
2493 | - $query_params, |
|
2494 | - $field_to_count, |
|
2495 | - $distinct |
|
2496 | - ); |
|
2497 | - } |
|
2498 | - |
|
2499 | - |
|
2500 | - /** |
|
2501 | - * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
2502 | - * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
2503 | - * |
|
2504 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
2505 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2506 | - * @param string $field_to_sum name of field to count by. |
|
2507 | - * By default, uses primary key |
|
2508 | - * (which doesn't make much sense, so you should probably change it) |
|
2509 | - * @return int |
|
2510 | - * @throws ReflectionException |
|
2511 | - * @throws InvalidArgumentException |
|
2512 | - * @throws InvalidInterfaceException |
|
2513 | - * @throws InvalidDataTypeException |
|
2514 | - * @throws EE_Error |
|
2515 | - */ |
|
2516 | - public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
2517 | - { |
|
2518 | - return $this->_model->sum_related( |
|
2519 | - $this, |
|
2520 | - $relation_name, |
|
2521 | - $query_params, |
|
2522 | - $field_to_sum |
|
2523 | - ); |
|
2524 | - } |
|
2525 | - |
|
2526 | - |
|
2527 | - /** |
|
2528 | - * Gets the first (ie, one) related model object of the specified type. |
|
2529 | - * |
|
2530 | - * @param string $relationName key in the model's _model_relations array |
|
2531 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2532 | - * @return EE_Base_Class (not an array, a single object) |
|
2533 | - * @throws ReflectionException |
|
2534 | - * @throws InvalidArgumentException |
|
2535 | - * @throws InvalidInterfaceException |
|
2536 | - * @throws InvalidDataTypeException |
|
2537 | - * @throws EE_Error |
|
2538 | - */ |
|
2539 | - public function get_first_related($relationName, $query_params = array()) |
|
2540 | - { |
|
2541 | - $model_relation = $this->_model->related_settings_for($relationName); |
|
2542 | - if ($this->ID()) {// this exists in the DB, get from the cache OR the DB |
|
2543 | - // if they've provided some query parameters, don't bother trying to cache the result |
|
2544 | - // also make sure we're not caching the result of get_first_related |
|
2545 | - // on a relation which should have an array of objects (because the cache might have an array of objects) |
|
2546 | - if ( |
|
2547 | - $query_params |
|
2548 | - || ! $model_relation instanceof EE_Belongs_To_Relation |
|
2549 | - ) { |
|
2550 | - $related_model_object = $this->_model->get_first_related( |
|
2551 | - $this, |
|
2552 | - $relationName, |
|
2553 | - $query_params |
|
2554 | - ); |
|
2555 | - } else { |
|
2556 | - // first, check if we've already cached the result of this query |
|
2557 | - $cached_result = $this->get_one_from_cache($relationName); |
|
2558 | - if (! $cached_result) { |
|
2559 | - $related_model_object = $this->_model->get_first_related( |
|
2560 | - $this, |
|
2561 | - $relationName, |
|
2562 | - $query_params |
|
2563 | - ); |
|
2564 | - $this->cache($relationName, $related_model_object); |
|
2565 | - } else { |
|
2566 | - $related_model_object = $cached_result; |
|
2567 | - } |
|
2568 | - } |
|
2569 | - } else { |
|
2570 | - $related_model_object = null; |
|
2571 | - // this doesn't exist in the Db, |
|
2572 | - // but maybe the relation is of type belongs to, and so the related thing might |
|
2573 | - if ($model_relation instanceof EE_Belongs_To_Relation) { |
|
2574 | - $related_model_object = $this->_model->get_first_related( |
|
2575 | - $this, |
|
2576 | - $relationName, |
|
2577 | - $query_params |
|
2578 | - ); |
|
2579 | - } |
|
2580 | - // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
|
2581 | - // just get what's cached on this object |
|
2582 | - if (! $related_model_object) { |
|
2583 | - $related_model_object = $this->get_one_from_cache($relationName); |
|
2584 | - } |
|
2585 | - } |
|
2586 | - return $related_model_object; |
|
2587 | - } |
|
2588 | - |
|
2589 | - |
|
2590 | - /** |
|
2591 | - * Does a delete on all related objects of type $relationName and removes |
|
2592 | - * the current model object's relation to them. If they can't be deleted (because |
|
2593 | - * of blocking related model objects) does nothing. If the related model objects are |
|
2594 | - * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
2595 | - * If this model object doesn't exist yet in the DB, just removes its related things |
|
2596 | - * |
|
2597 | - * @param string $relationName |
|
2598 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2599 | - * @return int how many deleted |
|
2600 | - * @throws ReflectionException |
|
2601 | - * @throws InvalidArgumentException |
|
2602 | - * @throws InvalidInterfaceException |
|
2603 | - * @throws InvalidDataTypeException |
|
2604 | - * @throws EE_Error |
|
2605 | - */ |
|
2606 | - public function delete_related($relationName, $query_params = array()) |
|
2607 | - { |
|
2608 | - if ($this->ID()) { |
|
2609 | - $count = $this->_model->delete_related( |
|
2610 | - $this, |
|
2611 | - $relationName, |
|
2612 | - $query_params |
|
2613 | - ); |
|
2614 | - } else { |
|
2615 | - $count = count($this->get_all_from_cache($relationName)); |
|
2616 | - $this->clear_cache($relationName, null, true); |
|
2617 | - } |
|
2618 | - return $count; |
|
2619 | - } |
|
2620 | - |
|
2621 | - |
|
2622 | - /** |
|
2623 | - * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
2624 | - * the current model object's relation to them. If they can't be deleted (because |
|
2625 | - * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
2626 | - * If the related thing isn't a soft-deletable model object, this function is identical |
|
2627 | - * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
2628 | - * |
|
2629 | - * @param string $relationName |
|
2630 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2631 | - * @return int how many deleted (including those soft deleted) |
|
2632 | - * @throws ReflectionException |
|
2633 | - * @throws InvalidArgumentException |
|
2634 | - * @throws InvalidInterfaceException |
|
2635 | - * @throws InvalidDataTypeException |
|
2636 | - * @throws EE_Error |
|
2637 | - */ |
|
2638 | - public function delete_related_permanently($relationName, $query_params = array()) |
|
2639 | - { |
|
2640 | - if ($this->ID()) { |
|
2641 | - $count = $this->_model->delete_related_permanently( |
|
2642 | - $this, |
|
2643 | - $relationName, |
|
2644 | - $query_params |
|
2645 | - ); |
|
2646 | - } else { |
|
2647 | - $count = count($this->get_all_from_cache($relationName)); |
|
2648 | - } |
|
2649 | - $this->clear_cache($relationName, null, true); |
|
2650 | - return $count; |
|
2651 | - } |
|
2652 | - |
|
2653 | - |
|
2654 | - /** |
|
2655 | - * is_set |
|
2656 | - * Just a simple utility function children can use for checking if property exists |
|
2657 | - * |
|
2658 | - * @access public |
|
2659 | - * @param string $field_name property to check |
|
2660 | - * @return bool TRUE if existing,FALSE if not. |
|
2661 | - */ |
|
2662 | - public function is_set($field_name) |
|
2663 | - { |
|
2664 | - return isset($this->_fields[ $field_name ]); |
|
2665 | - } |
|
2666 | - |
|
2667 | - |
|
2668 | - /** |
|
2669 | - * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
2670 | - * EE_Error exception if they don't |
|
2671 | - * |
|
2672 | - * @param mixed (string|array) $properties properties to check |
|
2673 | - * @throws EE_Error |
|
2674 | - * @return bool TRUE if existing, throw EE_Error if not. |
|
2675 | - */ |
|
2676 | - protected function _property_exists($properties) |
|
2677 | - { |
|
2678 | - foreach ((array) $properties as $property_name) { |
|
2679 | - // first make sure this property exists |
|
2680 | - if (! $this->_fields[ $property_name ]) { |
|
2681 | - throw new EE_Error( |
|
2682 | - sprintf( |
|
2683 | - esc_html__( |
|
2684 | - 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
2685 | - 'event_espresso' |
|
2686 | - ), |
|
2687 | - $property_name |
|
2688 | - ) |
|
2689 | - ); |
|
2690 | - } |
|
2691 | - } |
|
2692 | - return true; |
|
2693 | - } |
|
2694 | - |
|
2695 | - |
|
2696 | - /** |
|
2697 | - * This simply returns an array of model fields for this object |
|
2698 | - * |
|
2699 | - * @return array |
|
2700 | - * @throws ReflectionException |
|
2701 | - * @throws InvalidArgumentException |
|
2702 | - * @throws InvalidInterfaceException |
|
2703 | - * @throws InvalidDataTypeException |
|
2704 | - * @throws EE_Error |
|
2705 | - */ |
|
2706 | - public function model_field_array() |
|
2707 | - { |
|
2708 | - $fields = $this->_model->field_settings(false); |
|
2709 | - $properties = array(); |
|
2710 | - // remove prepended underscore |
|
2711 | - foreach ($fields as $field_name => $settings) { |
|
2712 | - $properties[ $field_name ] = $this->get($field_name); |
|
2713 | - } |
|
2714 | - return $properties; |
|
2715 | - } |
|
2716 | - |
|
2717 | - |
|
2718 | - /** |
|
2719 | - * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
2720 | - * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
2721 | - * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. |
|
2722 | - * Instead of requiring a plugin to extend the EE_Base_Class |
|
2723 | - * (which works fine is there's only 1 plugin, but when will that happen?) |
|
2724 | - * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' |
|
2725 | - * (eg, filters_hook_espresso__EE_Answer__my_great_function) |
|
2726 | - * and accepts 2 arguments: the object on which the function was called, |
|
2727 | - * and an array of the original arguments passed to the function. |
|
2728 | - * Whatever their callback function returns will be returned by this function. |
|
2729 | - * Example: in functions.php (or in a plugin): |
|
2730 | - * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); |
|
2731 | - * function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
2732 | - * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
2733 | - * return $previousReturnValue.$returnString; |
|
2734 | - * } |
|
2735 | - * require('EE_Answer.class.php'); |
|
2736 | - * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
2737 | - * echo $answer->my_callback('monkeys',100); |
|
2738 | - * //will output "you called my_callback! and passed args:monkeys,100" |
|
2739 | - * |
|
2740 | - * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
2741 | - * @param array $args array of original arguments passed to the function |
|
2742 | - * @throws EE_Error |
|
2743 | - * @return mixed whatever the plugin which calls add_filter decides |
|
2744 | - */ |
|
2745 | - public function __call($methodName, $args) |
|
2746 | - { |
|
2747 | - $className = get_class($this); |
|
2748 | - $tagName = "FHEE__{$className}__{$methodName}"; |
|
2749 | - if (! has_filter($tagName)) { |
|
2750 | - throw new EE_Error( |
|
2751 | - sprintf( |
|
2752 | - esc_html__( |
|
2753 | - "Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}", |
|
2754 | - 'event_espresso' |
|
2755 | - ), |
|
2756 | - $methodName, |
|
2757 | - $className, |
|
2758 | - $tagName |
|
2759 | - ) |
|
2760 | - ); |
|
2761 | - } |
|
2762 | - return apply_filters($tagName, null, $this, $args); |
|
2763 | - } |
|
2764 | - |
|
2765 | - |
|
2766 | - /** |
|
2767 | - * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
2768 | - * A $previous_value can be specified in case there are many meta rows with the same key |
|
2769 | - * |
|
2770 | - * @param string $meta_key |
|
2771 | - * @param mixed $meta_value |
|
2772 | - * @param mixed $previous_value |
|
2773 | - * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
2774 | - * NOTE: if the values haven't changed, returns 0 |
|
2775 | - * @throws InvalidArgumentException |
|
2776 | - * @throws InvalidInterfaceException |
|
2777 | - * @throws InvalidDataTypeException |
|
2778 | - * @throws EE_Error |
|
2779 | - * @throws ReflectionException |
|
2780 | - */ |
|
2781 | - public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
2782 | - { |
|
2783 | - $query_params = array( |
|
2784 | - array( |
|
2785 | - 'EXM_key' => $meta_key, |
|
2786 | - 'OBJ_ID' => $this->ID(), |
|
2787 | - 'EXM_type' => $this->_model->get_this_model_name(), |
|
2788 | - ), |
|
2789 | - ); |
|
2790 | - if ($previous_value !== null) { |
|
2791 | - $query_params[0]['EXM_value'] = $meta_value; |
|
2792 | - } |
|
2793 | - $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
2794 | - if (! $existing_rows_like_that) { |
|
2795 | - return $this->add_extra_meta($meta_key, $meta_value); |
|
2796 | - } |
|
2797 | - foreach ($existing_rows_like_that as $existing_row) { |
|
2798 | - $existing_row->save(array('EXM_value' => $meta_value)); |
|
2799 | - } |
|
2800 | - return count($existing_rows_like_that); |
|
2801 | - } |
|
2802 | - |
|
2803 | - |
|
2804 | - /** |
|
2805 | - * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
2806 | - * no other extra meta for this model object have the same key. Returns TRUE if the |
|
2807 | - * extra meta row was entered, false if not |
|
2808 | - * |
|
2809 | - * @param string $meta_key |
|
2810 | - * @param mixed $meta_value |
|
2811 | - * @param boolean $unique |
|
2812 | - * @return boolean |
|
2813 | - * @throws InvalidArgumentException |
|
2814 | - * @throws InvalidInterfaceException |
|
2815 | - * @throws InvalidDataTypeException |
|
2816 | - * @throws EE_Error |
|
2817 | - * @throws ReflectionException |
|
2818 | - * @throws ReflectionException |
|
2819 | - */ |
|
2820 | - public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
2821 | - { |
|
2822 | - if ($unique) { |
|
2823 | - $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
2824 | - array( |
|
2825 | - array( |
|
2826 | - 'EXM_key' => $meta_key, |
|
2827 | - 'OBJ_ID' => $this->ID(), |
|
2828 | - 'EXM_type' => $this->_model->get_this_model_name(), |
|
2829 | - ), |
|
2830 | - ) |
|
2831 | - ); |
|
2832 | - if ($existing_extra_meta) { |
|
2833 | - return false; |
|
2834 | - } |
|
2835 | - } |
|
2836 | - $new_extra_meta = EE_Extra_Meta::new_instance( |
|
2837 | - array( |
|
2838 | - 'EXM_key' => $meta_key, |
|
2839 | - 'EXM_value' => $meta_value, |
|
2840 | - 'OBJ_ID' => $this->ID(), |
|
2841 | - 'EXM_type' => $this->_model->get_this_model_name(), |
|
2842 | - ) |
|
2843 | - ); |
|
2844 | - $new_extra_meta->save(); |
|
2845 | - return true; |
|
2846 | - } |
|
2847 | - |
|
2848 | - |
|
2849 | - /** |
|
2850 | - * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
2851 | - * is specified, only deletes extra meta records with that value. |
|
2852 | - * |
|
2853 | - * @param string $meta_key |
|
2854 | - * @param mixed $meta_value |
|
2855 | - * @return int number of extra meta rows deleted |
|
2856 | - * @throws InvalidArgumentException |
|
2857 | - * @throws InvalidInterfaceException |
|
2858 | - * @throws InvalidDataTypeException |
|
2859 | - * @throws EE_Error |
|
2860 | - * @throws ReflectionException |
|
2861 | - */ |
|
2862 | - public function delete_extra_meta($meta_key, $meta_value = null) |
|
2863 | - { |
|
2864 | - $query_params = array( |
|
2865 | - array( |
|
2866 | - 'EXM_key' => $meta_key, |
|
2867 | - 'OBJ_ID' => $this->ID(), |
|
2868 | - 'EXM_type' => $this->_model->get_this_model_name(), |
|
2869 | - ), |
|
2870 | - ); |
|
2871 | - if ($meta_value !== null) { |
|
2872 | - $query_params[0]['EXM_value'] = $meta_value; |
|
2873 | - } |
|
2874 | - return EEM_Extra_Meta::instance()->delete($query_params); |
|
2875 | - } |
|
2876 | - |
|
2877 | - |
|
2878 | - /** |
|
2879 | - * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
2880 | - * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
2881 | - * You can specify $default is case you haven't found the extra meta |
|
2882 | - * |
|
2883 | - * @param string $meta_key |
|
2884 | - * @param boolean $single |
|
2885 | - * @param mixed $default if we don't find anything, what should we return? |
|
2886 | - * @return mixed single value if $single; array if ! $single |
|
2887 | - * @throws ReflectionException |
|
2888 | - * @throws InvalidArgumentException |
|
2889 | - * @throws InvalidInterfaceException |
|
2890 | - * @throws InvalidDataTypeException |
|
2891 | - * @throws EE_Error |
|
2892 | - */ |
|
2893 | - public function get_extra_meta($meta_key, $single = false, $default = null) |
|
2894 | - { |
|
2895 | - if ($single) { |
|
2896 | - $result = $this->get_first_related( |
|
2897 | - 'Extra_Meta', |
|
2898 | - array(array('EXM_key' => $meta_key)) |
|
2899 | - ); |
|
2900 | - if ($result instanceof EE_Extra_Meta) { |
|
2901 | - return $result->value(); |
|
2902 | - } |
|
2903 | - } else { |
|
2904 | - $results = $this->get_many_related( |
|
2905 | - 'Extra_Meta', |
|
2906 | - array(array('EXM_key' => $meta_key)) |
|
2907 | - ); |
|
2908 | - if ($results) { |
|
2909 | - $values = array(); |
|
2910 | - foreach ($results as $result) { |
|
2911 | - if ($result instanceof EE_Extra_Meta) { |
|
2912 | - $values[ $result->ID() ] = $result->value(); |
|
2913 | - } |
|
2914 | - } |
|
2915 | - return $values; |
|
2916 | - } |
|
2917 | - } |
|
2918 | - // if nothing discovered yet return default. |
|
2919 | - return apply_filters( |
|
2920 | - 'FHEE__EE_Base_Class__get_extra_meta__default_value', |
|
2921 | - $default, |
|
2922 | - $meta_key, |
|
2923 | - $single, |
|
2924 | - $this |
|
2925 | - ); |
|
2926 | - } |
|
2927 | - |
|
2928 | - |
|
2929 | - /** |
|
2930 | - * Returns a simple array of all the extra meta associated with this model object. |
|
2931 | - * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
2932 | - * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
2933 | - * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
2934 | - * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
2935 | - * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
2936 | - * finally the extra meta's value as each sub-value. (eg |
|
2937 | - * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
2938 | - * |
|
2939 | - * @param boolean $one_of_each_key |
|
2940 | - * @return array |
|
2941 | - * @throws ReflectionException |
|
2942 | - * @throws InvalidArgumentException |
|
2943 | - * @throws InvalidInterfaceException |
|
2944 | - * @throws InvalidDataTypeException |
|
2945 | - * @throws EE_Error |
|
2946 | - */ |
|
2947 | - public function all_extra_meta_array($one_of_each_key = true) |
|
2948 | - { |
|
2949 | - $return_array = array(); |
|
2950 | - if ($one_of_each_key) { |
|
2951 | - $extra_meta_objs = $this->get_many_related( |
|
2952 | - 'Extra_Meta', |
|
2953 | - array('group_by' => 'EXM_key') |
|
2954 | - ); |
|
2955 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
2956 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
2957 | - $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
2958 | - } |
|
2959 | - } |
|
2960 | - } else { |
|
2961 | - $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
2962 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
2963 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
2964 | - if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
2965 | - $return_array[ $extra_meta_obj->key() ] = array(); |
|
2966 | - } |
|
2967 | - $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
2968 | - } |
|
2969 | - } |
|
2970 | - } |
|
2971 | - return $return_array; |
|
2972 | - } |
|
2973 | - |
|
2974 | - |
|
2975 | - /** |
|
2976 | - * Gets a pretty nice displayable nice for this model object. Often overridden |
|
2977 | - * |
|
2978 | - * @return string |
|
2979 | - * @throws ReflectionException |
|
2980 | - * @throws InvalidArgumentException |
|
2981 | - * @throws InvalidInterfaceException |
|
2982 | - * @throws InvalidDataTypeException |
|
2983 | - * @throws EE_Error |
|
2984 | - */ |
|
2985 | - public function name() |
|
2986 | - { |
|
2987 | - // find a field that's not a text field |
|
2988 | - $field_we_can_use = $this->_model->get_a_field_of_type('EE_Text_Field_Base'); |
|
2989 | - if ($field_we_can_use) { |
|
2990 | - return $this->get($field_we_can_use->get_name()); |
|
2991 | - } |
|
2992 | - $first_few_properties = $this->model_field_array(); |
|
2993 | - $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
2994 | - $name_parts = array(); |
|
2995 | - foreach ($first_few_properties as $name => $value) { |
|
2996 | - $name_parts[] = "$name:$value"; |
|
2997 | - } |
|
2998 | - return implode(',', $name_parts); |
|
2999 | - } |
|
3000 | - |
|
3001 | - |
|
3002 | - /** |
|
3003 | - * in_entity_map |
|
3004 | - * Checks if this model object has been proven to already be in the entity map |
|
3005 | - * |
|
3006 | - * @return boolean |
|
3007 | - * @throws ReflectionException |
|
3008 | - * @throws InvalidArgumentException |
|
3009 | - * @throws InvalidInterfaceException |
|
3010 | - * @throws InvalidDataTypeException |
|
3011 | - * @throws EE_Error |
|
3012 | - */ |
|
3013 | - public function in_entity_map() |
|
3014 | - { |
|
3015 | - // well, if we looked, did we find it in the entity map? |
|
3016 | - return $this->ID() && $this->_model->get_from_entity_map($this->ID()) === $this; |
|
3017 | - } |
|
3018 | - |
|
3019 | - |
|
3020 | - /** |
|
3021 | - * refresh_from_db |
|
3022 | - * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
3023 | - * |
|
3024 | - * @throws ReflectionException |
|
3025 | - * @throws InvalidArgumentException |
|
3026 | - * @throws InvalidInterfaceException |
|
3027 | - * @throws InvalidDataTypeException |
|
3028 | - * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
3029 | - * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
3030 | - */ |
|
3031 | - public function refresh_from_db() |
|
3032 | - { |
|
3033 | - if ($this->ID() && $this->in_entity_map()) { |
|
3034 | - $this->_model->refresh_entity_map_from_db($this->ID()); |
|
3035 | - } else { |
|
3036 | - // if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
3037 | - // if it has an ID but it's not in the map, and you're asking me to refresh it |
|
3038 | - // that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
3039 | - // absolutely nothing in it for this ID |
|
3040 | - if (WP_DEBUG) { |
|
3041 | - throw new EE_Error( |
|
3042 | - sprintf( |
|
3043 | - esc_html__( |
|
3044 | - 'Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.', |
|
3045 | - 'event_espresso' |
|
3046 | - ), |
|
3047 | - $this->ID(), |
|
3048 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
3049 | - get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
3050 | - ) |
|
3051 | - ); |
|
3052 | - } |
|
3053 | - } |
|
3054 | - } |
|
3055 | - |
|
3056 | - |
|
3057 | - /** |
|
3058 | - * Change $fields' values to $new_value_sql (which is a string of raw SQL) |
|
3059 | - * |
|
3060 | - * @since 4.9.80.p |
|
3061 | - * @param EE_Model_Field_Base[] $fields |
|
3062 | - * @param string $new_value_sql |
|
3063 | - * example: 'column_name=123', |
|
3064 | - * or 'column_name=column_name+1', |
|
3065 | - * or 'column_name= CASE |
|
3066 | - * WHEN (`column_name` + `other_column` + 5) <= `yet_another_column` |
|
3067 | - * THEN `column_name` + 5 |
|
3068 | - * ELSE `column_name` |
|
3069 | - * END' |
|
3070 | - * Also updates $field on this model object with the latest value from the database. |
|
3071 | - * @return bool |
|
3072 | - * @throws EE_Error |
|
3073 | - * @throws InvalidArgumentException |
|
3074 | - * @throws InvalidDataTypeException |
|
3075 | - * @throws InvalidInterfaceException |
|
3076 | - * @throws ReflectionException |
|
3077 | - */ |
|
3078 | - protected function updateFieldsInDB($fields, $new_value_sql) |
|
3079 | - { |
|
3080 | - // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB |
|
3081 | - // if it wasn't even there to start off. |
|
3082 | - if (! $this->ID()) { |
|
3083 | - $this->save(); |
|
3084 | - } |
|
3085 | - global $wpdb; |
|
3086 | - if (empty($fields)) { |
|
3087 | - throw new InvalidArgumentException( |
|
3088 | - esc_html__( |
|
3089 | - 'EE_Base_Class::updateFieldsInDB was passed an empty array of fields.', |
|
3090 | - 'event_espresso' |
|
3091 | - ) |
|
3092 | - ); |
|
3093 | - } |
|
3094 | - $first_field = reset($fields); |
|
3095 | - $table_alias = $first_field->get_table_alias(); |
|
3096 | - foreach ($fields as $field) { |
|
3097 | - if ($table_alias !== $field->get_table_alias()) { |
|
3098 | - throw new InvalidArgumentException( |
|
3099 | - sprintf( |
|
3100 | - esc_html__( |
|
3101 | - // @codingStandardsIgnoreStart |
|
3102 | - 'EE_Base_Class::updateFieldsInDB was passed fields for different tables ("%1$s" and "%2$s"), which is not supported. Instead, please call the method multiple times.', |
|
3103 | - // @codingStandardsIgnoreEnd |
|
3104 | - 'event_espresso' |
|
3105 | - ), |
|
3106 | - $table_alias, |
|
3107 | - $field->get_table_alias() |
|
3108 | - ) |
|
3109 | - ); |
|
3110 | - } |
|
3111 | - } |
|
3112 | - // Ok the fields are now known to all be for the same table. Proceed with creating the SQL to update it. |
|
3113 | - $table_obj = $this->_model->get_table_obj_by_alias($table_alias); |
|
3114 | - $table_pk_value = $this->ID(); |
|
3115 | - $table_name = $table_obj->get_table_name(); |
|
3116 | - if ($table_obj instanceof EE_Secondary_Table) { |
|
3117 | - $table_pk_field_name = $table_obj->get_fk_on_table(); |
|
3118 | - } else { |
|
3119 | - $table_pk_field_name = $table_obj->get_pk_column(); |
|
3120 | - } |
|
3121 | - |
|
3122 | - $query = |
|
3123 | - "UPDATE `{$table_name}` |
|
336 | + $this->_props_n_values_provided_in_constructor |
|
337 | + && $field_value |
|
338 | + && $field_name === $this->_model->primary_key_name() |
|
339 | + ) { |
|
340 | + // if so, we want all this object's fields to be filled either with |
|
341 | + // what we've explicitly set on this model |
|
342 | + // or what we have in the db |
|
343 | + // echo "setting primary key!"; |
|
344 | + $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
345 | + $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
346 | + foreach ($fields_on_model as $field_obj) { |
|
347 | + if ( |
|
348 | + ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
349 | + && $field_obj->get_name() !== $field_name |
|
350 | + ) { |
|
351 | + $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
352 | + } |
|
353 | + } |
|
354 | + // oh this model object has an ID? well make sure its in the entity mapper |
|
355 | + $this->_model->add_to_entity_map($this); |
|
356 | + } |
|
357 | + // let's unset any cache for this field_name from the $_cached_properties property. |
|
358 | + $this->_clear_cached_property($field_name); |
|
359 | + } else { |
|
360 | + throw new EE_Error( |
|
361 | + sprintf( |
|
362 | + esc_html__( |
|
363 | + 'A valid EE_Model_Field_Base could not be found for the given field name: %s', |
|
364 | + 'event_espresso' |
|
365 | + ), |
|
366 | + $field_name |
|
367 | + ) |
|
368 | + ); |
|
369 | + } |
|
370 | + } |
|
371 | + |
|
372 | + |
|
373 | + /** |
|
374 | + * Set custom select values for model. |
|
375 | + * |
|
376 | + * @param array $custom_select_values |
|
377 | + */ |
|
378 | + public function setCustomSelectsValues(array $custom_select_values) |
|
379 | + { |
|
380 | + $this->custom_selection_results = $custom_select_values; |
|
381 | + } |
|
382 | + |
|
383 | + |
|
384 | + /** |
|
385 | + * Returns the custom select value for the provided alias if its set. |
|
386 | + * If not set, returns null. |
|
387 | + * |
|
388 | + * @param string $alias |
|
389 | + * @return string|int|float|null |
|
390 | + */ |
|
391 | + public function getCustomSelect($alias) |
|
392 | + { |
|
393 | + return isset($this->custom_selection_results[ $alias ]) |
|
394 | + ? $this->custom_selection_results[ $alias ] |
|
395 | + : null; |
|
396 | + } |
|
397 | + |
|
398 | + |
|
399 | + /** |
|
400 | + * This sets the field value on the db column if it exists for the given $column_name or |
|
401 | + * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
402 | + * |
|
403 | + * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
404 | + * @param string $field_name Must be the exact column name. |
|
405 | + * @param mixed $field_value The value to set. |
|
406 | + * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
407 | + * @throws InvalidArgumentException |
|
408 | + * @throws InvalidInterfaceException |
|
409 | + * @throws InvalidDataTypeException |
|
410 | + * @throws EE_Error |
|
411 | + * @throws ReflectionException |
|
412 | + */ |
|
413 | + public function set_field_or_extra_meta($field_name, $field_value) |
|
414 | + { |
|
415 | + if ($this->_model->has_field($field_name)) { |
|
416 | + $this->set($field_name, $field_value); |
|
417 | + return true; |
|
418 | + } |
|
419 | + // ensure this object is saved first so that extra meta can be properly related. |
|
420 | + $this->save(); |
|
421 | + return $this->update_extra_meta($field_name, $field_value); |
|
422 | + } |
|
423 | + |
|
424 | + |
|
425 | + /** |
|
426 | + * This retrieves the value of the db column set on this class or if that's not present |
|
427 | + * it will attempt to retrieve from extra_meta if found. |
|
428 | + * Example Usage: |
|
429 | + * Via EE_Message child class: |
|
430 | + * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
431 | + * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
432 | + * also have additional main fields specific to the messenger. The system accommodates those extra |
|
433 | + * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
434 | + * value for those extra fields dynamically via the EE_message object. |
|
435 | + * |
|
436 | + * @param string $field_name expecting the fully qualified field name. |
|
437 | + * @return mixed|null value for the field if found. null if not found. |
|
438 | + * @throws ReflectionException |
|
439 | + * @throws InvalidArgumentException |
|
440 | + * @throws InvalidInterfaceException |
|
441 | + * @throws InvalidDataTypeException |
|
442 | + * @throws EE_Error |
|
443 | + */ |
|
444 | + public function get_field_or_extra_meta($field_name) |
|
445 | + { |
|
446 | + if ($this->_model->has_field($field_name)) { |
|
447 | + $column_value = $this->get($field_name); |
|
448 | + } else { |
|
449 | + // This isn't a column in the main table, let's see if it is in the extra meta. |
|
450 | + $column_value = $this->get_extra_meta($field_name, true, null); |
|
451 | + } |
|
452 | + return $column_value; |
|
453 | + } |
|
454 | + |
|
455 | + |
|
456 | + /** |
|
457 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
458 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
459 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
460 | + * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
461 | + * |
|
462 | + * @access public |
|
463 | + * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
464 | + * @return void |
|
465 | + * @throws InvalidArgumentException |
|
466 | + * @throws InvalidInterfaceException |
|
467 | + * @throws InvalidDataTypeException |
|
468 | + * @throws EE_Error |
|
469 | + * @throws ReflectionException |
|
470 | + */ |
|
471 | + public function set_timezone($timezone = '') |
|
472 | + { |
|
473 | + $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
474 | + // make sure we clear all cached properties because they won't be relevant now |
|
475 | + $this->_clear_cached_properties(); |
|
476 | + // make sure we update field settings and the date for all EE_Datetime_Fields |
|
477 | + $model_fields = $this->_model->field_settings(false); |
|
478 | + foreach ($model_fields as $field_name => $field_obj) { |
|
479 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
480 | + $field_obj->set_timezone($this->_timezone); |
|
481 | + if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) { |
|
482 | + EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], new DateTimeZone($this->_timezone)); |
|
483 | + } |
|
484 | + } |
|
485 | + } |
|
486 | + } |
|
487 | + |
|
488 | + |
|
489 | + /** |
|
490 | + * This just returns whatever is set for the current timezone. |
|
491 | + * |
|
492 | + * @access public |
|
493 | + * @return string timezone string |
|
494 | + */ |
|
495 | + public function get_timezone() |
|
496 | + { |
|
497 | + return $this->_timezone; |
|
498 | + } |
|
499 | + |
|
500 | + |
|
501 | + /** |
|
502 | + * This sets the internal date format to what is sent in to be used as the new default for the class |
|
503 | + * internally instead of wp set date format options |
|
504 | + * |
|
505 | + * @since 4.6 |
|
506 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
507 | + */ |
|
508 | + public function set_date_format($format) |
|
509 | + { |
|
510 | + $this->_dt_frmt = $format; |
|
511 | + // clear cached_properties because they won't be relevant now. |
|
512 | + $this->_clear_cached_properties(); |
|
513 | + } |
|
514 | + |
|
515 | + |
|
516 | + /** |
|
517 | + * This sets the internal time format string to what is sent in to be used as the new default for the |
|
518 | + * class internally instead of wp set time format options. |
|
519 | + * |
|
520 | + * @since 4.6 |
|
521 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
522 | + */ |
|
523 | + public function set_time_format($format) |
|
524 | + { |
|
525 | + $this->_tm_frmt = $format; |
|
526 | + // clear cached_properties because they won't be relevant now. |
|
527 | + $this->_clear_cached_properties(); |
|
528 | + } |
|
529 | + |
|
530 | + |
|
531 | + /** |
|
532 | + * This returns the current internal set format for the date and time formats. |
|
533 | + * |
|
534 | + * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
535 | + * where the first value is the date format and the second value is the time format. |
|
536 | + * @return mixed string|array |
|
537 | + */ |
|
538 | + public function get_format($full = true) |
|
539 | + { |
|
540 | + return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
541 | + } |
|
542 | + |
|
543 | + |
|
544 | + /** |
|
545 | + * cache |
|
546 | + * stores the passed model object on the current model object. |
|
547 | + * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
548 | + * |
|
549 | + * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
550 | + * 'Registration' associated with this model object |
|
551 | + * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
552 | + * that could be a payment or a registration) |
|
553 | + * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
554 | + * items which will be stored in an array on this object |
|
555 | + * @throws ReflectionException |
|
556 | + * @throws InvalidArgumentException |
|
557 | + * @throws InvalidInterfaceException |
|
558 | + * @throws InvalidDataTypeException |
|
559 | + * @throws EE_Error |
|
560 | + * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
561 | + * related thing, no array) |
|
562 | + */ |
|
563 | + public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
564 | + { |
|
565 | + // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
566 | + if (! $object_to_cache instanceof EE_Base_Class) { |
|
567 | + return false; |
|
568 | + } |
|
569 | + // also get "how" the object is related, or throw an error |
|
570 | + if (! $relationship_to_model = $this->_model->related_settings_for($relationName)) { |
|
571 | + throw new EE_Error( |
|
572 | + sprintf( |
|
573 | + esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
574 | + $relationName, |
|
575 | + get_class($this) |
|
576 | + ) |
|
577 | + ); |
|
578 | + } |
|
579 | + // how many things are related ? |
|
580 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
581 | + // if it's a "belongs to" relationship, then there's only one related model object |
|
582 | + // eg, if this is a registration, there's only 1 attendee for it |
|
583 | + // so for these model objects just set it to be cached |
|
584 | + $this->_model_relations[ $relationName ] = $object_to_cache; |
|
585 | + $return = true; |
|
586 | + } else { |
|
587 | + // otherwise, this is the "many" side of a one to many relationship, |
|
588 | + // so we'll add the object to the array of related objects for that type. |
|
589 | + // eg: if this is an event, there are many registrations for that event, |
|
590 | + // so we cache the registrations in an array |
|
591 | + if (! is_array($this->_model_relations[ $relationName ])) { |
|
592 | + // if for some reason, the cached item is a model object, |
|
593 | + // then stick that in the array, otherwise start with an empty array |
|
594 | + $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ] |
|
595 | + instanceof |
|
596 | + EE_Base_Class |
|
597 | + ? array($this->_model_relations[ $relationName ]) : array(); |
|
598 | + } |
|
599 | + // first check for a cache_id which is normally empty |
|
600 | + if (! empty($cache_id)) { |
|
601 | + // if the cache_id exists, then it means we are purposely trying to cache this |
|
602 | + // with a known key that can then be used to retrieve the object later on |
|
603 | + $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache; |
|
604 | + $return = $cache_id; |
|
605 | + } elseif ($object_to_cache->ID()) { |
|
606 | + // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
607 | + $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache; |
|
608 | + $return = $object_to_cache->ID(); |
|
609 | + } else { |
|
610 | + // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
611 | + $this->_model_relations[ $relationName ][] = $object_to_cache; |
|
612 | + // move the internal pointer to the end of the array |
|
613 | + end($this->_model_relations[ $relationName ]); |
|
614 | + // and grab the key so that we can return it |
|
615 | + $return = key($this->_model_relations[ $relationName ]); |
|
616 | + } |
|
617 | + } |
|
618 | + return $return; |
|
619 | + } |
|
620 | + |
|
621 | + |
|
622 | + /** |
|
623 | + * For adding an item to the cached_properties property. |
|
624 | + * |
|
625 | + * @access protected |
|
626 | + * @param string $fieldname the property item the corresponding value is for. |
|
627 | + * @param mixed $value The value we are caching. |
|
628 | + * @param string|null $cache_type |
|
629 | + * @return void |
|
630 | + * @throws ReflectionException |
|
631 | + * @throws InvalidArgumentException |
|
632 | + * @throws InvalidInterfaceException |
|
633 | + * @throws InvalidDataTypeException |
|
634 | + * @throws EE_Error |
|
635 | + */ |
|
636 | + protected function _set_cached_property($fieldname, $value, $cache_type = null) |
|
637 | + { |
|
638 | + // first make sure this property exists |
|
639 | + $this->_model->field_settings_for($fieldname); |
|
640 | + $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
641 | + $this->_cached_properties[ $fieldname ][ $cache_type ] = $value; |
|
642 | + } |
|
643 | + |
|
644 | + |
|
645 | + /** |
|
646 | + * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
647 | + * This also SETS the cache if we return the actual property! |
|
648 | + * |
|
649 | + * @param string $fieldname the name of the property we're trying to retrieve |
|
650 | + * @param bool $pretty |
|
651 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
652 | + * (in cases where the same property may be used for different outputs |
|
653 | + * - i.e. datetime, money etc.) |
|
654 | + * It can also accept certain pre-defined "schema" strings |
|
655 | + * to define how to output the property. |
|
656 | + * see the field's prepare_for_pretty_echoing for what strings can be used |
|
657 | + * @return mixed whatever the value for the property is we're retrieving |
|
658 | + * @throws ReflectionException |
|
659 | + * @throws InvalidArgumentException |
|
660 | + * @throws InvalidInterfaceException |
|
661 | + * @throws InvalidDataTypeException |
|
662 | + * @throws EE_Error |
|
663 | + */ |
|
664 | + protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
665 | + { |
|
666 | + // verify the field exists |
|
667 | + $this->_model->field_settings_for($fieldname); |
|
668 | + $cache_type = $pretty ? 'pretty' : 'standard'; |
|
669 | + $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
670 | + if (isset($this->_cached_properties[ $fieldname ][ $cache_type ])) { |
|
671 | + return $this->_cached_properties[ $fieldname ][ $cache_type ]; |
|
672 | + } |
|
673 | + $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
|
674 | + $this->_set_cached_property($fieldname, $value, $cache_type); |
|
675 | + return $value; |
|
676 | + } |
|
677 | + |
|
678 | + |
|
679 | + /** |
|
680 | + * If the cache didn't fetch the needed item, this fetches it. |
|
681 | + * |
|
682 | + * @param string $fieldname |
|
683 | + * @param bool $pretty |
|
684 | + * @param string $extra_cache_ref |
|
685 | + * @return mixed |
|
686 | + * @throws InvalidArgumentException |
|
687 | + * @throws InvalidInterfaceException |
|
688 | + * @throws InvalidDataTypeException |
|
689 | + * @throws EE_Error |
|
690 | + * @throws ReflectionException |
|
691 | + */ |
|
692 | + protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
693 | + { |
|
694 | + $field_obj = $this->_model->field_settings_for($fieldname); |
|
695 | + // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
696 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
697 | + $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
698 | + } |
|
699 | + if (! isset($this->_fields[ $fieldname ])) { |
|
700 | + $this->_fields[ $fieldname ] = null; |
|
701 | + } |
|
702 | + $value = $pretty |
|
703 | + ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $fieldname ], $extra_cache_ref) |
|
704 | + : $field_obj->prepare_for_get($this->_fields[ $fieldname ]); |
|
705 | + return $value; |
|
706 | + } |
|
707 | + |
|
708 | + |
|
709 | + /** |
|
710 | + * set timezone, formats, and output for EE_Datetime_Field objects |
|
711 | + * |
|
712 | + * @param \EE_Datetime_Field $datetime_field |
|
713 | + * @param bool $pretty |
|
714 | + * @param null $date_or_time |
|
715 | + * @return void |
|
716 | + * @throws InvalidArgumentException |
|
717 | + * @throws InvalidInterfaceException |
|
718 | + * @throws InvalidDataTypeException |
|
719 | + * @throws EE_Error |
|
720 | + */ |
|
721 | + protected function _prepare_datetime_field( |
|
722 | + EE_Datetime_Field $datetime_field, |
|
723 | + $pretty = false, |
|
724 | + $date_or_time = null |
|
725 | + ) { |
|
726 | + $datetime_field->set_timezone($this->_timezone); |
|
727 | + $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
728 | + $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
729 | + // set the output returned |
|
730 | + switch ($date_or_time) { |
|
731 | + case 'D': |
|
732 | + $datetime_field->set_date_time_output('date'); |
|
733 | + break; |
|
734 | + case 'T': |
|
735 | + $datetime_field->set_date_time_output('time'); |
|
736 | + break; |
|
737 | + default: |
|
738 | + $datetime_field->set_date_time_output(); |
|
739 | + } |
|
740 | + } |
|
741 | + |
|
742 | + |
|
743 | + /** |
|
744 | + * This just takes care of clearing out the cached_properties |
|
745 | + * |
|
746 | + * @return void |
|
747 | + */ |
|
748 | + protected function _clear_cached_properties() |
|
749 | + { |
|
750 | + $this->_cached_properties = array(); |
|
751 | + } |
|
752 | + |
|
753 | + |
|
754 | + /** |
|
755 | + * This just clears out ONE property if it exists in the cache |
|
756 | + * |
|
757 | + * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
758 | + * @return void |
|
759 | + */ |
|
760 | + protected function _clear_cached_property($property_name) |
|
761 | + { |
|
762 | + if (isset($this->_cached_properties[ $property_name ])) { |
|
763 | + unset($this->_cached_properties[ $property_name ]); |
|
764 | + } |
|
765 | + } |
|
766 | + |
|
767 | + |
|
768 | + /** |
|
769 | + * Ensures that this related thing is a model object. |
|
770 | + * |
|
771 | + * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
772 | + * @param string $model_name name of the related thing, eg 'Attendee', |
|
773 | + * @return EE_Base_Class |
|
774 | + * @throws ReflectionException |
|
775 | + * @throws InvalidArgumentException |
|
776 | + * @throws InvalidInterfaceException |
|
777 | + * @throws InvalidDataTypeException |
|
778 | + * @throws EE_Error |
|
779 | + */ |
|
780 | + protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
781 | + { |
|
782 | + $other_model_instance = self::_get_model_instance_with_name( |
|
783 | + self::_get_model_classname($model_name), |
|
784 | + $this->_timezone |
|
785 | + ); |
|
786 | + return $other_model_instance->ensure_is_obj($object_or_id); |
|
787 | + } |
|
788 | + |
|
789 | + |
|
790 | + /** |
|
791 | + * Forgets the cached model of the given relation Name. So the next time we request it, |
|
792 | + * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
793 | + * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
794 | + * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
795 | + * |
|
796 | + * @param string $relationName one of the keys in the _model_relations array on the model. |
|
797 | + * Eg 'Registration' |
|
798 | + * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
799 | + * if you intend to use $clear_all = TRUE, or the relation only |
|
800 | + * has 1 object anyways (ie, it's a BelongsToRelation) |
|
801 | + * @param bool $clear_all This flags clearing the entire cache relation property if |
|
802 | + * this is HasMany or HABTM. |
|
803 | + * @throws ReflectionException |
|
804 | + * @throws InvalidArgumentException |
|
805 | + * @throws InvalidInterfaceException |
|
806 | + * @throws InvalidDataTypeException |
|
807 | + * @throws EE_Error |
|
808 | + * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
809 | + * relation from all |
|
810 | + */ |
|
811 | + public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
812 | + { |
|
813 | + $relationship_to_model = $this->_model->related_settings_for($relationName); |
|
814 | + $index_in_cache = ''; |
|
815 | + if (! $relationship_to_model) { |
|
816 | + throw new EE_Error( |
|
817 | + sprintf( |
|
818 | + esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'), |
|
819 | + $relationName, |
|
820 | + get_class($this) |
|
821 | + ) |
|
822 | + ); |
|
823 | + } |
|
824 | + if ($clear_all) { |
|
825 | + $obj_removed = true; |
|
826 | + $this->_model_relations[ $relationName ] = null; |
|
827 | + } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
828 | + $obj_removed = $this->_model_relations[ $relationName ]; |
|
829 | + $this->_model_relations[ $relationName ] = null; |
|
830 | + } else { |
|
831 | + if ( |
|
832 | + $object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
833 | + && $object_to_remove_or_index_into_array->ID() |
|
834 | + ) { |
|
835 | + $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
836 | + if ( |
|
837 | + is_array($this->_model_relations[ $relationName ]) |
|
838 | + && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ]) |
|
839 | + ) { |
|
840 | + $index_found_at = null; |
|
841 | + // find this object in the array even though it has a different key |
|
842 | + foreach ($this->_model_relations[ $relationName ] as $index => $obj) { |
|
843 | + /** @noinspection TypeUnsafeComparisonInspection */ |
|
844 | + if ( |
|
845 | + $obj instanceof EE_Base_Class |
|
846 | + && ( |
|
847 | + $obj == $object_to_remove_or_index_into_array |
|
848 | + || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
849 | + ) |
|
850 | + ) { |
|
851 | + $index_found_at = $index; |
|
852 | + break; |
|
853 | + } |
|
854 | + } |
|
855 | + if ($index_found_at) { |
|
856 | + $index_in_cache = $index_found_at; |
|
857 | + } else { |
|
858 | + // it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
859 | + // if it wasn't in it to begin with. So we're done |
|
860 | + return $object_to_remove_or_index_into_array; |
|
861 | + } |
|
862 | + } |
|
863 | + } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
864 | + // so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
865 | + foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
866 | + /** @noinspection TypeUnsafeComparisonInspection */ |
|
867 | + if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
868 | + $index_in_cache = $index; |
|
869 | + } |
|
870 | + } |
|
871 | + } else { |
|
872 | + $index_in_cache = $object_to_remove_or_index_into_array; |
|
873 | + } |
|
874 | + // supposedly we've found it. But it could just be that the client code |
|
875 | + // provided a bad index/object |
|
876 | + if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) { |
|
877 | + $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ]; |
|
878 | + unset($this->_model_relations[ $relationName ][ $index_in_cache ]); |
|
879 | + } else { |
|
880 | + // that thing was never cached anyways. |
|
881 | + $obj_removed = null; |
|
882 | + } |
|
883 | + } |
|
884 | + return $obj_removed; |
|
885 | + } |
|
886 | + |
|
887 | + |
|
888 | + /** |
|
889 | + * update_cache_after_object_save |
|
890 | + * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
891 | + * obtained after being saved to the db |
|
892 | + * |
|
893 | + * @param string $relationName - the type of object that is cached |
|
894 | + * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
895 | + * @param string $current_cache_id - the ID that was used when originally caching the object |
|
896 | + * @return boolean TRUE on success, FALSE on fail |
|
897 | + * @throws ReflectionException |
|
898 | + * @throws InvalidArgumentException |
|
899 | + * @throws InvalidInterfaceException |
|
900 | + * @throws InvalidDataTypeException |
|
901 | + * @throws EE_Error |
|
902 | + */ |
|
903 | + public function update_cache_after_object_save( |
|
904 | + $relationName, |
|
905 | + EE_Base_Class $newly_saved_object, |
|
906 | + $current_cache_id = '' |
|
907 | + ) { |
|
908 | + // verify that incoming object is of the correct type |
|
909 | + $obj_class = 'EE_' . $relationName; |
|
910 | + if ($newly_saved_object instanceof $obj_class) { |
|
911 | + /* @type EE_Base_Class $newly_saved_object */ |
|
912 | + // now get the type of relation |
|
913 | + $relationship_to_model = $this->_model->related_settings_for($relationName); |
|
914 | + // if this is a 1:1 relationship |
|
915 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
916 | + // then just replace the cached object with the newly saved object |
|
917 | + $this->_model_relations[ $relationName ] = $newly_saved_object; |
|
918 | + return true; |
|
919 | + // or if it's some kind of sordid feral polyamorous relationship... |
|
920 | + } |
|
921 | + if ( |
|
922 | + is_array($this->_model_relations[ $relationName ]) |
|
923 | + && isset($this->_model_relations[ $relationName ][ $current_cache_id ]) |
|
924 | + ) { |
|
925 | + // then remove the current cached item |
|
926 | + unset($this->_model_relations[ $relationName ][ $current_cache_id ]); |
|
927 | + // and cache the newly saved object using it's new ID |
|
928 | + $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object; |
|
929 | + return true; |
|
930 | + } |
|
931 | + } |
|
932 | + return false; |
|
933 | + } |
|
934 | + |
|
935 | + |
|
936 | + /** |
|
937 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
938 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
939 | + * |
|
940 | + * @param string $relationName |
|
941 | + * @return EE_Base_Class |
|
942 | + */ |
|
943 | + public function get_one_from_cache($relationName) |
|
944 | + { |
|
945 | + $cached_array_or_object = isset($this->_model_relations[ $relationName ]) |
|
946 | + ? $this->_model_relations[ $relationName ] |
|
947 | + : null; |
|
948 | + if (is_array($cached_array_or_object)) { |
|
949 | + return array_shift($cached_array_or_object); |
|
950 | + } |
|
951 | + return $cached_array_or_object; |
|
952 | + } |
|
953 | + |
|
954 | + |
|
955 | + /** |
|
956 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
957 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
958 | + * |
|
959 | + * @param string $relationName |
|
960 | + * @throws ReflectionException |
|
961 | + * @throws InvalidArgumentException |
|
962 | + * @throws InvalidInterfaceException |
|
963 | + * @throws InvalidDataTypeException |
|
964 | + * @throws EE_Error |
|
965 | + * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
966 | + */ |
|
967 | + public function get_all_from_cache($relationName) |
|
968 | + { |
|
969 | + $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : array(); |
|
970 | + // if the result is not an array, but exists, make it an array |
|
971 | + $objects = is_array($objects) ? $objects : array($objects); |
|
972 | + // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
973 | + // basically, if this model object was stored in the session, and these cached model objects |
|
974 | + // already have IDs, let's make sure they're in their model's entity mapper |
|
975 | + // otherwise we will have duplicates next time we call |
|
976 | + // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
977 | + $related_model = EE_Registry::instance()->load_model($relationName); |
|
978 | + foreach ($objects as $model_object) { |
|
979 | + if ($related_model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
980 | + // ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
981 | + if ($model_object->ID()) { |
|
982 | + $related_model->add_to_entity_map($model_object); |
|
983 | + } |
|
984 | + } else { |
|
985 | + throw new EE_Error( |
|
986 | + sprintf( |
|
987 | + esc_html__( |
|
988 | + 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
989 | + 'event_espresso' |
|
990 | + ), |
|
991 | + $relationName, |
|
992 | + gettype($model_object) |
|
993 | + ) |
|
994 | + ); |
|
995 | + } |
|
996 | + } |
|
997 | + return $objects; |
|
998 | + } |
|
999 | + |
|
1000 | + |
|
1001 | + /** |
|
1002 | + * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
1003 | + * matching the given query conditions. |
|
1004 | + * |
|
1005 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
1006 | + * @param int $limit How many objects to return. |
|
1007 | + * @param array $query_params Any additional conditions on the query. |
|
1008 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
1009 | + * you can indicate just the columns you want returned |
|
1010 | + * @return array|EE_Base_Class[] |
|
1011 | + * @throws ReflectionException |
|
1012 | + * @throws InvalidArgumentException |
|
1013 | + * @throws InvalidInterfaceException |
|
1014 | + * @throws InvalidDataTypeException |
|
1015 | + * @throws EE_Error |
|
1016 | + */ |
|
1017 | + public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
1018 | + { |
|
1019 | + $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1020 | + ? $this->_model->get_primary_key_field()->get_name() |
|
1021 | + : $field_to_order_by; |
|
1022 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
1023 | + if (empty($field) || empty($current_value)) { |
|
1024 | + return array(); |
|
1025 | + } |
|
1026 | + return $this->_model->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
1027 | + } |
|
1028 | + |
|
1029 | + |
|
1030 | + /** |
|
1031 | + * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
1032 | + * matching the given query conditions. |
|
1033 | + * |
|
1034 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
1035 | + * @param int $limit How many objects to return. |
|
1036 | + * @param array $query_params Any additional conditions on the query. |
|
1037 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
1038 | + * you can indicate just the columns you want returned |
|
1039 | + * @return array|EE_Base_Class[] |
|
1040 | + * @throws ReflectionException |
|
1041 | + * @throws InvalidArgumentException |
|
1042 | + * @throws InvalidInterfaceException |
|
1043 | + * @throws InvalidDataTypeException |
|
1044 | + * @throws EE_Error |
|
1045 | + */ |
|
1046 | + public function previous_x( |
|
1047 | + $field_to_order_by = null, |
|
1048 | + $limit = 1, |
|
1049 | + $query_params = array(), |
|
1050 | + $columns_to_select = null |
|
1051 | + ) { |
|
1052 | + $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1053 | + ? $this->_model->get_primary_key_field()->get_name() |
|
1054 | + : $field_to_order_by; |
|
1055 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
1056 | + if (empty($field) || empty($current_value)) { |
|
1057 | + return array(); |
|
1058 | + } |
|
1059 | + return $this->_model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
1060 | + } |
|
1061 | + |
|
1062 | + |
|
1063 | + /** |
|
1064 | + * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
1065 | + * matching the given query conditions. |
|
1066 | + * |
|
1067 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
1068 | + * @param array $query_params Any additional conditions on the query. |
|
1069 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
1070 | + * you can indicate just the columns you want returned |
|
1071 | + * @return array|EE_Base_Class |
|
1072 | + * @throws ReflectionException |
|
1073 | + * @throws InvalidArgumentException |
|
1074 | + * @throws InvalidInterfaceException |
|
1075 | + * @throws InvalidDataTypeException |
|
1076 | + * @throws EE_Error |
|
1077 | + */ |
|
1078 | + public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
1079 | + { |
|
1080 | + $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1081 | + ? $this->_model->get_primary_key_field()->get_name() |
|
1082 | + : $field_to_order_by; |
|
1083 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
1084 | + if (empty($field) || empty($current_value)) { |
|
1085 | + return array(); |
|
1086 | + } |
|
1087 | + return $this->_model->next($current_value, $field, $query_params, $columns_to_select); |
|
1088 | + } |
|
1089 | + |
|
1090 | + |
|
1091 | + /** |
|
1092 | + * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
1093 | + * matching the given query conditions. |
|
1094 | + * |
|
1095 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
1096 | + * @param array $query_params Any additional conditions on the query. |
|
1097 | + * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
1098 | + * you can indicate just the column you want returned |
|
1099 | + * @return array|EE_Base_Class |
|
1100 | + * @throws ReflectionException |
|
1101 | + * @throws InvalidArgumentException |
|
1102 | + * @throws InvalidInterfaceException |
|
1103 | + * @throws InvalidDataTypeException |
|
1104 | + * @throws EE_Error |
|
1105 | + */ |
|
1106 | + public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
1107 | + { |
|
1108 | + $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1109 | + ? $this->_model->get_primary_key_field()->get_name() |
|
1110 | + : $field_to_order_by; |
|
1111 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
1112 | + if (empty($field) || empty($current_value)) { |
|
1113 | + return array(); |
|
1114 | + } |
|
1115 | + return $this->_model->previous($current_value, $field, $query_params, $columns_to_select); |
|
1116 | + } |
|
1117 | + |
|
1118 | + |
|
1119 | + /** |
|
1120 | + * Overrides parent because parent expects old models. |
|
1121 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
1122 | + * |
|
1123 | + * @param string $field_name |
|
1124 | + * @param mixed $field_value_from_db |
|
1125 | + * @throws ReflectionException |
|
1126 | + * @throws InvalidArgumentException |
|
1127 | + * @throws InvalidInterfaceException |
|
1128 | + * @throws InvalidDataTypeException |
|
1129 | + * @throws EE_Error |
|
1130 | + */ |
|
1131 | + public function set_from_db($field_name, $field_value_from_db) |
|
1132 | + { |
|
1133 | + $field_obj = $this->_model->field_settings_for($field_name); |
|
1134 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
1135 | + // you would think the DB has no NULLs for non-null label fields right? wrong! |
|
1136 | + // eg, a CPT model object could have an entry in the posts table, but no |
|
1137 | + // entry in the meta table. Meaning that all its columns in the meta table |
|
1138 | + // are null! yikes! so when we find one like that, use defaults for its meta columns |
|
1139 | + if ($field_value_from_db === null) { |
|
1140 | + if ($field_obj->is_nullable()) { |
|
1141 | + // if the field allows nulls, then let it be null |
|
1142 | + $field_value = null; |
|
1143 | + } else { |
|
1144 | + $field_value = $field_obj->get_default_value(); |
|
1145 | + } |
|
1146 | + } else { |
|
1147 | + $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
1148 | + } |
|
1149 | + $this->_fields[ $field_name ] = $field_value; |
|
1150 | + $this->_clear_cached_property($field_name); |
|
1151 | + } |
|
1152 | + } |
|
1153 | + |
|
1154 | + |
|
1155 | + /** |
|
1156 | + * verifies that the specified field is of the correct type |
|
1157 | + * |
|
1158 | + * @param string $field_name |
|
1159 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
1160 | + * (in cases where the same property may be used for different outputs |
|
1161 | + * - i.e. datetime, money etc.) |
|
1162 | + * @return mixed |
|
1163 | + * @throws ReflectionException |
|
1164 | + * @throws InvalidArgumentException |
|
1165 | + * @throws InvalidInterfaceException |
|
1166 | + * @throws InvalidDataTypeException |
|
1167 | + * @throws EE_Error |
|
1168 | + */ |
|
1169 | + public function get($field_name, $extra_cache_ref = null) |
|
1170 | + { |
|
1171 | + return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
1172 | + } |
|
1173 | + |
|
1174 | + |
|
1175 | + /** |
|
1176 | + * This method simply returns the RAW unprocessed value for the given property in this class |
|
1177 | + * |
|
1178 | + * @param string $field_name A valid fieldname |
|
1179 | + * @return mixed Whatever the raw value stored on the property is. |
|
1180 | + * @throws ReflectionException |
|
1181 | + * @throws InvalidArgumentException |
|
1182 | + * @throws InvalidInterfaceException |
|
1183 | + * @throws InvalidDataTypeException |
|
1184 | + * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
1185 | + */ |
|
1186 | + public function get_raw($field_name) |
|
1187 | + { |
|
1188 | + $field_settings = $this->_model->field_settings_for($field_name); |
|
1189 | + return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime |
|
1190 | + ? $this->_fields[ $field_name ]->format('U') |
|
1191 | + : $this->_fields[ $field_name ]; |
|
1192 | + } |
|
1193 | + |
|
1194 | + |
|
1195 | + /** |
|
1196 | + * This is used to return the internal DateTime object used for a field that is a |
|
1197 | + * EE_Datetime_Field. |
|
1198 | + * |
|
1199 | + * @param string $field_name The field name retrieving the DateTime object. |
|
1200 | + * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
1201 | + * @throws EE_Error an error is set and false returned. If the field IS an |
|
1202 | + * EE_Datetime_Field and but the field value is null, then |
|
1203 | + * just null is returned (because that indicates that likely |
|
1204 | + * this field is nullable). |
|
1205 | + * @throws InvalidArgumentException |
|
1206 | + * @throws InvalidDataTypeException |
|
1207 | + * @throws InvalidInterfaceException |
|
1208 | + * @throws ReflectionException |
|
1209 | + */ |
|
1210 | + public function get_DateTime_object($field_name) |
|
1211 | + { |
|
1212 | + $field_settings = $this->_model->field_settings_for($field_name); |
|
1213 | + if (! $field_settings instanceof EE_Datetime_Field) { |
|
1214 | + EE_Error::add_error( |
|
1215 | + sprintf( |
|
1216 | + esc_html__( |
|
1217 | + 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
1218 | + 'event_espresso' |
|
1219 | + ), |
|
1220 | + $field_name |
|
1221 | + ), |
|
1222 | + __FILE__, |
|
1223 | + __FUNCTION__, |
|
1224 | + __LINE__ |
|
1225 | + ); |
|
1226 | + return false; |
|
1227 | + } |
|
1228 | + return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime |
|
1229 | + ? clone $this->_fields[ $field_name ] |
|
1230 | + : null; |
|
1231 | + } |
|
1232 | + |
|
1233 | + |
|
1234 | + /** |
|
1235 | + * To be used in template to immediately echo out the value, and format it for output. |
|
1236 | + * Eg, should call stripslashes and whatnot before echoing |
|
1237 | + * |
|
1238 | + * @param string $field_name the name of the field as it appears in the DB |
|
1239 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
1240 | + * (in cases where the same property may be used for different outputs |
|
1241 | + * - i.e. datetime, money etc.) |
|
1242 | + * @return void |
|
1243 | + * @throws ReflectionException |
|
1244 | + * @throws InvalidArgumentException |
|
1245 | + * @throws InvalidInterfaceException |
|
1246 | + * @throws InvalidDataTypeException |
|
1247 | + * @throws EE_Error |
|
1248 | + */ |
|
1249 | + public function e($field_name, $extra_cache_ref = null) |
|
1250 | + { |
|
1251 | + echo $this->get_pretty($field_name, $extra_cache_ref); |
|
1252 | + } |
|
1253 | + |
|
1254 | + |
|
1255 | + /** |
|
1256 | + * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
1257 | + * can be easily used as the value of form input. |
|
1258 | + * |
|
1259 | + * @param string $field_name |
|
1260 | + * @return void |
|
1261 | + * @throws ReflectionException |
|
1262 | + * @throws InvalidArgumentException |
|
1263 | + * @throws InvalidInterfaceException |
|
1264 | + * @throws InvalidDataTypeException |
|
1265 | + * @throws EE_Error |
|
1266 | + */ |
|
1267 | + public function f($field_name) |
|
1268 | + { |
|
1269 | + $this->e($field_name, 'form_input'); |
|
1270 | + } |
|
1271 | + |
|
1272 | + |
|
1273 | + /** |
|
1274 | + * Same as `f()` but just returns the value instead of echoing it |
|
1275 | + * |
|
1276 | + * @param string $field_name |
|
1277 | + * @return string |
|
1278 | + * @throws ReflectionException |
|
1279 | + * @throws InvalidArgumentException |
|
1280 | + * @throws InvalidInterfaceException |
|
1281 | + * @throws InvalidDataTypeException |
|
1282 | + * @throws EE_Error |
|
1283 | + */ |
|
1284 | + public function get_f($field_name) |
|
1285 | + { |
|
1286 | + return (string) $this->get_pretty($field_name, 'form_input'); |
|
1287 | + } |
|
1288 | + |
|
1289 | + |
|
1290 | + /** |
|
1291 | + * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
1292 | + * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
1293 | + * to see what options are available. |
|
1294 | + * |
|
1295 | + * @param string $field_name |
|
1296 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
1297 | + * (in cases where the same property may be used for different outputs |
|
1298 | + * - i.e. datetime, money etc.) |
|
1299 | + * @return mixed |
|
1300 | + * @throws ReflectionException |
|
1301 | + * @throws InvalidArgumentException |
|
1302 | + * @throws InvalidInterfaceException |
|
1303 | + * @throws InvalidDataTypeException |
|
1304 | + * @throws EE_Error |
|
1305 | + */ |
|
1306 | + public function get_pretty($field_name, $extra_cache_ref = null) |
|
1307 | + { |
|
1308 | + return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
1309 | + } |
|
1310 | + |
|
1311 | + |
|
1312 | + /** |
|
1313 | + * This simply returns the datetime for the given field name |
|
1314 | + * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
1315 | + * (and the equivalent e_date, e_time, e_datetime). |
|
1316 | + * |
|
1317 | + * @access protected |
|
1318 | + * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
1319 | + * @param string $dt_frmt valid datetime format used for date |
|
1320 | + * (if '' then we just use the default on the field, |
|
1321 | + * if NULL we use the last-used format) |
|
1322 | + * @param string $tm_frmt Same as above except this is for time format |
|
1323 | + * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
1324 | + * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
1325 | + * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
1326 | + * if field is not a valid dtt field, or void if echoing |
|
1327 | + * @throws ReflectionException |
|
1328 | + * @throws InvalidArgumentException |
|
1329 | + * @throws InvalidInterfaceException |
|
1330 | + * @throws InvalidDataTypeException |
|
1331 | + * @throws EE_Error |
|
1332 | + */ |
|
1333 | + protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
|
1334 | + { |
|
1335 | + // clear cached property |
|
1336 | + $this->_clear_cached_property($field_name); |
|
1337 | + // reset format properties because they are used in get() |
|
1338 | + $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt; |
|
1339 | + $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt; |
|
1340 | + if ($echo) { |
|
1341 | + $this->e($field_name, $date_or_time); |
|
1342 | + return ''; |
|
1343 | + } |
|
1344 | + return $this->get($field_name, $date_or_time); |
|
1345 | + } |
|
1346 | + |
|
1347 | + |
|
1348 | + /** |
|
1349 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
1350 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
1351 | + * other echoes the pretty value for dtt) |
|
1352 | + * |
|
1353 | + * @param string $field_name name of model object datetime field holding the value |
|
1354 | + * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
1355 | + * @return string datetime value formatted |
|
1356 | + * @throws ReflectionException |
|
1357 | + * @throws InvalidArgumentException |
|
1358 | + * @throws InvalidInterfaceException |
|
1359 | + * @throws InvalidDataTypeException |
|
1360 | + * @throws EE_Error |
|
1361 | + */ |
|
1362 | + public function get_date($field_name, $format = '') |
|
1363 | + { |
|
1364 | + return $this->_get_datetime($field_name, $format, null, 'D'); |
|
1365 | + } |
|
1366 | + |
|
1367 | + |
|
1368 | + /** |
|
1369 | + * @param $field_name |
|
1370 | + * @param string $format |
|
1371 | + * @throws ReflectionException |
|
1372 | + * @throws InvalidArgumentException |
|
1373 | + * @throws InvalidInterfaceException |
|
1374 | + * @throws InvalidDataTypeException |
|
1375 | + * @throws EE_Error |
|
1376 | + */ |
|
1377 | + public function e_date($field_name, $format = '') |
|
1378 | + { |
|
1379 | + $this->_get_datetime($field_name, $format, null, 'D', true); |
|
1380 | + } |
|
1381 | + |
|
1382 | + |
|
1383 | + /** |
|
1384 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
1385 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
1386 | + * other echoes the pretty value for dtt) |
|
1387 | + * |
|
1388 | + * @param string $field_name name of model object datetime field holding the value |
|
1389 | + * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
1390 | + * @return string datetime value formatted |
|
1391 | + * @throws ReflectionException |
|
1392 | + * @throws InvalidArgumentException |
|
1393 | + * @throws InvalidInterfaceException |
|
1394 | + * @throws InvalidDataTypeException |
|
1395 | + * @throws EE_Error |
|
1396 | + */ |
|
1397 | + public function get_time($field_name, $format = '') |
|
1398 | + { |
|
1399 | + return $this->_get_datetime($field_name, null, $format, 'T'); |
|
1400 | + } |
|
1401 | + |
|
1402 | + |
|
1403 | + /** |
|
1404 | + * @param $field_name |
|
1405 | + * @param string $format |
|
1406 | + * @throws ReflectionException |
|
1407 | + * @throws InvalidArgumentException |
|
1408 | + * @throws InvalidInterfaceException |
|
1409 | + * @throws InvalidDataTypeException |
|
1410 | + * @throws EE_Error |
|
1411 | + */ |
|
1412 | + public function e_time($field_name, $format = '') |
|
1413 | + { |
|
1414 | + $this->_get_datetime($field_name, null, $format, 'T', true); |
|
1415 | + } |
|
1416 | + |
|
1417 | + |
|
1418 | + /** |
|
1419 | + * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
1420 | + * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
1421 | + * other echoes the pretty value for dtt) |
|
1422 | + * |
|
1423 | + * @param string $field_name name of model object datetime field holding the value |
|
1424 | + * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
|
1425 | + * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
|
1426 | + * @return string datetime value formatted |
|
1427 | + * @throws ReflectionException |
|
1428 | + * @throws InvalidArgumentException |
|
1429 | + * @throws InvalidInterfaceException |
|
1430 | + * @throws InvalidDataTypeException |
|
1431 | + * @throws EE_Error |
|
1432 | + */ |
|
1433 | + public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
1434 | + { |
|
1435 | + return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt); |
|
1436 | + } |
|
1437 | + |
|
1438 | + |
|
1439 | + /** |
|
1440 | + * @param string $field_name |
|
1441 | + * @param string $dt_frmt |
|
1442 | + * @param string $tm_frmt |
|
1443 | + * @throws ReflectionException |
|
1444 | + * @throws InvalidArgumentException |
|
1445 | + * @throws InvalidInterfaceException |
|
1446 | + * @throws InvalidDataTypeException |
|
1447 | + * @throws EE_Error |
|
1448 | + */ |
|
1449 | + public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
1450 | + { |
|
1451 | + $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true); |
|
1452 | + } |
|
1453 | + |
|
1454 | + |
|
1455 | + /** |
|
1456 | + * Get the i8ln value for a date using the WordPress @see date_i18n function. |
|
1457 | + * |
|
1458 | + * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
1459 | + * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
|
1460 | + * on the object will be used. |
|
1461 | + * @return string Date and time string in set locale or false if no field exists for the given |
|
1462 | + * @throws ReflectionException |
|
1463 | + * @throws InvalidArgumentException |
|
1464 | + * @throws InvalidInterfaceException |
|
1465 | + * @throws InvalidDataTypeException |
|
1466 | + * @throws EE_Error |
|
1467 | + * field name. |
|
1468 | + */ |
|
1469 | + public function get_i18n_datetime($field_name, $format = '') |
|
1470 | + { |
|
1471 | + $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
1472 | + return date_i18n( |
|
1473 | + $format, |
|
1474 | + EEH_DTT_Helper::get_timestamp_with_offset( |
|
1475 | + $this->get_raw($field_name), |
|
1476 | + $this->_timezone |
|
1477 | + ) |
|
1478 | + ); |
|
1479 | + } |
|
1480 | + |
|
1481 | + |
|
1482 | + /** |
|
1483 | + * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
1484 | + * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
1485 | + * thrown. |
|
1486 | + * |
|
1487 | + * @param string $field_name The field name being checked |
|
1488 | + * @throws ReflectionException |
|
1489 | + * @throws InvalidArgumentException |
|
1490 | + * @throws InvalidInterfaceException |
|
1491 | + * @throws InvalidDataTypeException |
|
1492 | + * @throws EE_Error |
|
1493 | + * @return EE_Datetime_Field |
|
1494 | + */ |
|
1495 | + protected function _get_dtt_field_settings($field_name) |
|
1496 | + { |
|
1497 | + $field = $this->_model->field_settings_for($field_name); |
|
1498 | + // check if field is dtt |
|
1499 | + if ($field instanceof EE_Datetime_Field) { |
|
1500 | + return $field; |
|
1501 | + } |
|
1502 | + throw new EE_Error( |
|
1503 | + sprintf( |
|
1504 | + esc_html__( |
|
1505 | + 'The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field. Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor', |
|
1506 | + 'event_espresso' |
|
1507 | + ), |
|
1508 | + $field_name, |
|
1509 | + self::_get_model_classname(get_class($this)) |
|
1510 | + ) |
|
1511 | + ); |
|
1512 | + } |
|
1513 | + |
|
1514 | + |
|
1515 | + |
|
1516 | + |
|
1517 | + /** |
|
1518 | + * NOTE ABOUT BELOW: |
|
1519 | + * These convenience date and time setters are for setting date and time independently. In other words you might |
|
1520 | + * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
1521 | + * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
|
1522 | + * method and make sure you send the entire datetime value for setting. |
|
1523 | + */ |
|
1524 | + /** |
|
1525 | + * sets the time on a datetime property |
|
1526 | + * |
|
1527 | + * @access protected |
|
1528 | + * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
1529 | + * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
|
1530 | + * @throws ReflectionException |
|
1531 | + * @throws InvalidArgumentException |
|
1532 | + * @throws InvalidInterfaceException |
|
1533 | + * @throws InvalidDataTypeException |
|
1534 | + * @throws EE_Error |
|
1535 | + */ |
|
1536 | + protected function _set_time_for($time, $fieldname) |
|
1537 | + { |
|
1538 | + $this->_set_date_time('T', $time, $fieldname); |
|
1539 | + } |
|
1540 | + |
|
1541 | + |
|
1542 | + /** |
|
1543 | + * sets the date on a datetime property |
|
1544 | + * |
|
1545 | + * @access protected |
|
1546 | + * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
1547 | + * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
|
1548 | + * @throws ReflectionException |
|
1549 | + * @throws InvalidArgumentException |
|
1550 | + * @throws InvalidInterfaceException |
|
1551 | + * @throws InvalidDataTypeException |
|
1552 | + * @throws EE_Error |
|
1553 | + */ |
|
1554 | + protected function _set_date_for($date, $fieldname) |
|
1555 | + { |
|
1556 | + $this->_set_date_time('D', $date, $fieldname); |
|
1557 | + } |
|
1558 | + |
|
1559 | + |
|
1560 | + /** |
|
1561 | + * This takes care of setting a date or time independently on a given model object property. This method also |
|
1562 | + * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
|
1563 | + * |
|
1564 | + * @access protected |
|
1565 | + * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
1566 | + * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
1567 | + * @param string $fieldname the name of the field the date OR time is being set on (must match a |
|
1568 | + * EE_Datetime_Field property) |
|
1569 | + * @throws ReflectionException |
|
1570 | + * @throws InvalidArgumentException |
|
1571 | + * @throws InvalidInterfaceException |
|
1572 | + * @throws InvalidDataTypeException |
|
1573 | + * @throws EE_Error |
|
1574 | + */ |
|
1575 | + protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
|
1576 | + { |
|
1577 | + $field = $this->_get_dtt_field_settings($fieldname); |
|
1578 | + $field->set_timezone($this->_timezone); |
|
1579 | + $field->set_date_format($this->_dt_frmt); |
|
1580 | + $field->set_time_format($this->_tm_frmt); |
|
1581 | + switch ($what) { |
|
1582 | + case 'T': |
|
1583 | + $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_time( |
|
1584 | + $datetime_value, |
|
1585 | + $this->_fields[ $fieldname ] |
|
1586 | + ); |
|
1587 | + $this->_has_changes = true; |
|
1588 | + break; |
|
1589 | + case 'D': |
|
1590 | + $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date( |
|
1591 | + $datetime_value, |
|
1592 | + $this->_fields[ $fieldname ] |
|
1593 | + ); |
|
1594 | + $this->_has_changes = true; |
|
1595 | + break; |
|
1596 | + case 'B': |
|
1597 | + $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value); |
|
1598 | + $this->_has_changes = true; |
|
1599 | + break; |
|
1600 | + } |
|
1601 | + $this->_clear_cached_property($fieldname); |
|
1602 | + } |
|
1603 | + |
|
1604 | + |
|
1605 | + /** |
|
1606 | + * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
1607 | + * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
1608 | + * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
1609 | + * that could lead to some unexpected results! |
|
1610 | + * |
|
1611 | + * @access public |
|
1612 | + * @param string $field_name This is the name of the field on the object that contains the date/time |
|
1613 | + * value being returned. |
|
1614 | + * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
1615 | + * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
1616 | + * @param string $prepend You can include something to prepend on the timestamp |
|
1617 | + * @param string $append You can include something to append on the timestamp |
|
1618 | + * @throws ReflectionException |
|
1619 | + * @throws InvalidArgumentException |
|
1620 | + * @throws InvalidInterfaceException |
|
1621 | + * @throws InvalidDataTypeException |
|
1622 | + * @throws EE_Error |
|
1623 | + * @return string timestamp |
|
1624 | + */ |
|
1625 | + public function display_in_my_timezone( |
|
1626 | + $field_name, |
|
1627 | + $callback = 'get_datetime', |
|
1628 | + $args = null, |
|
1629 | + $prepend = '', |
|
1630 | + $append = '' |
|
1631 | + ) { |
|
1632 | + $timezone = EEH_DTT_Helper::get_timezone(); |
|
1633 | + if ($timezone === $this->_timezone) { |
|
1634 | + return ''; |
|
1635 | + } |
|
1636 | + $original_timezone = $this->_timezone; |
|
1637 | + $this->set_timezone($timezone); |
|
1638 | + $fn = (array) $field_name; |
|
1639 | + $args = array_merge($fn, (array) $args); |
|
1640 | + if (! method_exists($this, $callback)) { |
|
1641 | + throw new EE_Error( |
|
1642 | + sprintf( |
|
1643 | + esc_html__( |
|
1644 | + 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
1645 | + 'event_espresso' |
|
1646 | + ), |
|
1647 | + $callback |
|
1648 | + ) |
|
1649 | + ); |
|
1650 | + } |
|
1651 | + $args = (array) $args; |
|
1652 | + $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
1653 | + $this->set_timezone($original_timezone); |
|
1654 | + return $return; |
|
1655 | + } |
|
1656 | + |
|
1657 | + |
|
1658 | + /** |
|
1659 | + * Deletes this model object. |
|
1660 | + * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
1661 | + * override |
|
1662 | + * `EE_Base_Class::_delete` NOT this class. |
|
1663 | + * |
|
1664 | + * @return boolean | int |
|
1665 | + * @throws ReflectionException |
|
1666 | + * @throws InvalidArgumentException |
|
1667 | + * @throws InvalidInterfaceException |
|
1668 | + * @throws InvalidDataTypeException |
|
1669 | + * @throws EE_Error |
|
1670 | + */ |
|
1671 | + public function delete() |
|
1672 | + { |
|
1673 | + /** |
|
1674 | + * Called just before the `EE_Base_Class::_delete` method call. |
|
1675 | + * Note: |
|
1676 | + * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
1677 | + * should be aware that `_delete` may not always result in a permanent delete. |
|
1678 | + * For example, `EE_Soft_Delete_Base_Class::_delete` |
|
1679 | + * soft deletes (trash) the object and does not permanently delete it. |
|
1680 | + * |
|
1681 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
1682 | + */ |
|
1683 | + do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
1684 | + $result = $this->_delete(); |
|
1685 | + /** |
|
1686 | + * Called just after the `EE_Base_Class::_delete` method call. |
|
1687 | + * Note: |
|
1688 | + * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
1689 | + * should be aware that `_delete` may not always result in a permanent delete. |
|
1690 | + * For example `EE_Soft_Base_Class::_delete` |
|
1691 | + * soft deletes (trash) the object and does not permanently delete it. |
|
1692 | + * |
|
1693 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
1694 | + * @param boolean $result |
|
1695 | + */ |
|
1696 | + do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
1697 | + return $result; |
|
1698 | + } |
|
1699 | + |
|
1700 | + |
|
1701 | + /** |
|
1702 | + * Calls the specific delete method for the instantiated class. |
|
1703 | + * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
1704 | + * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
1705 | + * `EE_Base_Class::delete` |
|
1706 | + * |
|
1707 | + * @return bool|int |
|
1708 | + * @throws ReflectionException |
|
1709 | + * @throws InvalidArgumentException |
|
1710 | + * @throws InvalidInterfaceException |
|
1711 | + * @throws InvalidDataTypeException |
|
1712 | + * @throws EE_Error |
|
1713 | + */ |
|
1714 | + protected function _delete() |
|
1715 | + { |
|
1716 | + return $this->delete_permanently(); |
|
1717 | + } |
|
1718 | + |
|
1719 | + |
|
1720 | + /** |
|
1721 | + * Deletes this model object permanently from db |
|
1722 | + * (but keep in mind related models may block the delete and return an error) |
|
1723 | + * |
|
1724 | + * @return bool | int |
|
1725 | + * @throws ReflectionException |
|
1726 | + * @throws InvalidArgumentException |
|
1727 | + * @throws InvalidInterfaceException |
|
1728 | + * @throws InvalidDataTypeException |
|
1729 | + * @throws EE_Error |
|
1730 | + */ |
|
1731 | + public function delete_permanently() |
|
1732 | + { |
|
1733 | + /** |
|
1734 | + * Called just before HARD deleting a model object |
|
1735 | + * |
|
1736 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
1737 | + */ |
|
1738 | + do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
1739 | + $result = $this->_model->delete_permanently_by_ID($this->ID()); |
|
1740 | + $this->refresh_cache_of_related_objects(); |
|
1741 | + /** |
|
1742 | + * Called just after HARD deleting a model object |
|
1743 | + * |
|
1744 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
1745 | + * @param boolean $result |
|
1746 | + */ |
|
1747 | + do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
1748 | + return $result; |
|
1749 | + } |
|
1750 | + |
|
1751 | + |
|
1752 | + /** |
|
1753 | + * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
1754 | + * related model objects |
|
1755 | + * |
|
1756 | + * @throws ReflectionException |
|
1757 | + * @throws InvalidArgumentException |
|
1758 | + * @throws InvalidInterfaceException |
|
1759 | + * @throws InvalidDataTypeException |
|
1760 | + * @throws EE_Error |
|
1761 | + */ |
|
1762 | + public function refresh_cache_of_related_objects() |
|
1763 | + { |
|
1764 | + foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
1765 | + if (! empty($this->_model_relations[ $relation_name ])) { |
|
1766 | + $related_objects = $this->_model_relations[ $relation_name ]; |
|
1767 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
1768 | + // this relation only stores a single model object, not an array |
|
1769 | + // but let's make it consistent |
|
1770 | + $related_objects = array($related_objects); |
|
1771 | + } |
|
1772 | + foreach ($related_objects as $related_object) { |
|
1773 | + // only refresh their cache if they're in memory |
|
1774 | + if ($related_object instanceof EE_Base_Class) { |
|
1775 | + $related_object->clear_cache( |
|
1776 | + $this->_model->get_this_model_name(), |
|
1777 | + $this |
|
1778 | + ); |
|
1779 | + } |
|
1780 | + } |
|
1781 | + } |
|
1782 | + } |
|
1783 | + } |
|
1784 | + |
|
1785 | + |
|
1786 | + /** |
|
1787 | + * Saves this object to the database. An array may be supplied to set some values on this |
|
1788 | + * object just before saving. |
|
1789 | + * |
|
1790 | + * @access public |
|
1791 | + * @param array $set_cols_n_values keys are field names, values are their new values, |
|
1792 | + * if provided during the save() method (often client code will change the fields' |
|
1793 | + * values before calling save) |
|
1794 | + * @throws InvalidArgumentException |
|
1795 | + * @throws InvalidInterfaceException |
|
1796 | + * @throws InvalidDataTypeException |
|
1797 | + * @throws EE_Error |
|
1798 | + * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
1799 | + * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
1800 | + * @throws ReflectionException |
|
1801 | + * @throws ReflectionException |
|
1802 | + * @throws ReflectionException |
|
1803 | + */ |
|
1804 | + public function save($set_cols_n_values = array()) |
|
1805 | + { |
|
1806 | + /** |
|
1807 | + * Filters the fields we're about to save on the model object |
|
1808 | + * |
|
1809 | + * @param array $set_cols_n_values |
|
1810 | + * @param EE_Base_Class $model_object |
|
1811 | + */ |
|
1812 | + $set_cols_n_values = (array) apply_filters( |
|
1813 | + 'FHEE__EE_Base_Class__save__set_cols_n_values', |
|
1814 | + $set_cols_n_values, |
|
1815 | + $this |
|
1816 | + ); |
|
1817 | + // set attributes as provided in $set_cols_n_values |
|
1818 | + foreach ($set_cols_n_values as $column => $value) { |
|
1819 | + $this->set($column, $value); |
|
1820 | + } |
|
1821 | + // no changes ? then don't do anything |
|
1822 | + if (! $this->_has_changes && $this->ID() && $this->_model->get_primary_key_field()->is_auto_increment()) { |
|
1823 | + return 0; |
|
1824 | + } |
|
1825 | + /** |
|
1826 | + * Saving a model object. |
|
1827 | + * Before we perform a save, this action is fired. |
|
1828 | + * |
|
1829 | + * @param EE_Base_Class $model_object the model object about to be saved. |
|
1830 | + */ |
|
1831 | + do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
1832 | + if (! $this->allow_persist()) { |
|
1833 | + return 0; |
|
1834 | + } |
|
1835 | + // now get current attribute values |
|
1836 | + $save_cols_n_values = $this->_fields; |
|
1837 | + // if the object already has an ID, update it. Otherwise, insert it |
|
1838 | + // also: change the assumption about values passed to the model NOT being prepare dby the model object. |
|
1839 | + // They have been |
|
1840 | + $old_assumption_concerning_value_preparation = $this->_model |
|
1841 | + ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
1842 | + $this->_model->assume_values_already_prepared_by_model_object(true); |
|
1843 | + // does this model have an autoincrement PK? |
|
1844 | + if ($this->_model->has_primary_key_field()) { |
|
1845 | + if ($this->_model->get_primary_key_field()->is_auto_increment()) { |
|
1846 | + // ok check if it's set, if so: update; if not, insert |
|
1847 | + if (! empty($save_cols_n_values[ $this->_model->primary_key_name() ])) { |
|
1848 | + $results = $this->_model->update_by_ID($save_cols_n_values, $this->ID()); |
|
1849 | + } else { |
|
1850 | + unset($save_cols_n_values[ $this->_model->primary_key_name() ]); |
|
1851 | + $results = $this->_model->insert($save_cols_n_values); |
|
1852 | + if ($results) { |
|
1853 | + // if successful, set the primary key |
|
1854 | + // but don't use the normal SET method, because it will check if |
|
1855 | + // an item with the same ID exists in the mapper & db, then |
|
1856 | + // will find it in the db (because we just added it) and THAT object |
|
1857 | + // will get added to the mapper before we can add this one! |
|
1858 | + // but if we just avoid using the SET method, all that headache can be avoided |
|
1859 | + $pk_field_name = $this->_model->primary_key_name(); |
|
1860 | + $this->_fields[ $pk_field_name ] = $results; |
|
1861 | + $this->_clear_cached_property($pk_field_name); |
|
1862 | + $this->_model->add_to_entity_map($this); |
|
1863 | + $this->_update_cached_related_model_objs_fks(); |
|
1864 | + } |
|
1865 | + } |
|
1866 | + } else {// PK is NOT auto-increment |
|
1867 | + // so check if one like it already exists in the db |
|
1868 | + if ($this->_model->exists_by_ID($this->ID())) { |
|
1869 | + if (WP_DEBUG && ! $this->in_entity_map()) { |
|
1870 | + throw new EE_Error( |
|
1871 | + sprintf( |
|
1872 | + esc_html__( |
|
1873 | + 'Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s', |
|
1874 | + 'event_espresso' |
|
1875 | + ), |
|
1876 | + get_class($this), |
|
1877 | + get_class($this->_model) . '::instance()->add_to_entity_map()', |
|
1878 | + get_class($this->_model) . '::instance()->get_one_by_ID()', |
|
1879 | + '<br />' |
|
1880 | + ) |
|
1881 | + ); |
|
1882 | + } |
|
1883 | + $results = $this->_model->update_by_ID($save_cols_n_values, $this->ID()); |
|
1884 | + } else { |
|
1885 | + $results = $this->_model->insert($save_cols_n_values); |
|
1886 | + $this->_update_cached_related_model_objs_fks(); |
|
1887 | + } |
|
1888 | + } |
|
1889 | + } else {// there is NO primary key |
|
1890 | + $already_in_db = false; |
|
1891 | + foreach ($this->_model->unique_indexes() as $index) { |
|
1892 | + $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
1893 | + if ($this->_model->exists(array($uniqueness_where_params))) { |
|
1894 | + $already_in_db = true; |
|
1895 | + } |
|
1896 | + } |
|
1897 | + if ($already_in_db) { |
|
1898 | + $combined_pk_fields_n_values = array_intersect_key( |
|
1899 | + $save_cols_n_values, |
|
1900 | + $this->_model->get_combined_primary_key_fields() |
|
1901 | + ); |
|
1902 | + $results = $this->_model->update( |
|
1903 | + $save_cols_n_values, |
|
1904 | + $combined_pk_fields_n_values |
|
1905 | + ); |
|
1906 | + } else { |
|
1907 | + $results = $this->_model->insert($save_cols_n_values); |
|
1908 | + } |
|
1909 | + } |
|
1910 | + // restore the old assumption about values being prepared by the model object |
|
1911 | + $this->_model->assume_values_already_prepared_by_model_object( |
|
1912 | + $old_assumption_concerning_value_preparation |
|
1913 | + ); |
|
1914 | + /** |
|
1915 | + * After saving the model object this action is called |
|
1916 | + * |
|
1917 | + * @param EE_Base_Class $model_object which was just saved |
|
1918 | + * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
1919 | + * the new ID (or 0 if an error occurred and it wasn't updated) |
|
1920 | + */ |
|
1921 | + do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
1922 | + $this->_has_changes = false; |
|
1923 | + return $results; |
|
1924 | + } |
|
1925 | + |
|
1926 | + |
|
1927 | + /** |
|
1928 | + * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
1929 | + * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
1930 | + * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
1931 | + * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
1932 | + * 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 |
|
1933 | + * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
1934 | + * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
1935 | + * |
|
1936 | + * @return void |
|
1937 | + * @throws ReflectionException |
|
1938 | + * @throws InvalidArgumentException |
|
1939 | + * @throws InvalidInterfaceException |
|
1940 | + * @throws InvalidDataTypeException |
|
1941 | + * @throws EE_Error |
|
1942 | + */ |
|
1943 | + protected function _update_cached_related_model_objs_fks() |
|
1944 | + { |
|
1945 | + foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
1946 | + if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
1947 | + foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
1948 | + $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
1949 | + $this->_model->get_this_model_name() |
|
1950 | + ); |
|
1951 | + $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
1952 | + if ($related_model_obj_in_cache->ID()) { |
|
1953 | + $related_model_obj_in_cache->save(); |
|
1954 | + } |
|
1955 | + } |
|
1956 | + } |
|
1957 | + } |
|
1958 | + } |
|
1959 | + |
|
1960 | + |
|
1961 | + /** |
|
1962 | + * Saves this model object and its NEW cached relations to the database. |
|
1963 | + * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
1964 | + * In order for that to work, we would need to mark model objects as dirty/clean... |
|
1965 | + * because otherwise, there's a potential for infinite looping of saving |
|
1966 | + * Saves the cached related model objects, and ensures the relation between them |
|
1967 | + * and this object and properly setup |
|
1968 | + * |
|
1969 | + * @return int ID of new model object on save; 0 on failure+ |
|
1970 | + * @throws ReflectionException |
|
1971 | + * @throws InvalidArgumentException |
|
1972 | + * @throws InvalidInterfaceException |
|
1973 | + * @throws InvalidDataTypeException |
|
1974 | + * @throws EE_Error |
|
1975 | + */ |
|
1976 | + public function save_new_cached_related_model_objs() |
|
1977 | + { |
|
1978 | + // make sure this has been saved |
|
1979 | + if (! $this->ID()) { |
|
1980 | + $id = $this->save(); |
|
1981 | + } else { |
|
1982 | + $id = $this->ID(); |
|
1983 | + } |
|
1984 | + // now save all the NEW cached model objects (ie they don't exist in the DB) |
|
1985 | + foreach ($this->_model->relation_settings() as $relationName => $relationObj) { |
|
1986 | + if ($this->_model_relations[ $relationName ]) { |
|
1987 | + // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
1988 | + // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
1989 | + /* @var $related_model_obj EE_Base_Class */ |
|
1990 | + if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
1991 | + // add a relation to that relation type (which saves the appropriate thing in the process) |
|
1992 | + // but ONLY if it DOES NOT exist in the DB |
|
1993 | + $related_model_obj = $this->_model_relations[ $relationName ]; |
|
1994 | + // if( ! $related_model_obj->ID()){ |
|
1995 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
1996 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
1997 | + // } |
|
1998 | + } else { |
|
1999 | + foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
2000 | + // add a relation to that relation type (which saves the appropriate thing in the process) |
|
2001 | + // but ONLY if it DOES NOT exist in the DB |
|
2002 | + // if( ! $related_model_obj->ID()){ |
|
2003 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
2004 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
2005 | + // } |
|
2006 | + } |
|
2007 | + } |
|
2008 | + } |
|
2009 | + } |
|
2010 | + return $id; |
|
2011 | + } |
|
2012 | + |
|
2013 | + |
|
2014 | + /** |
|
2015 | + * for getting a model while instantiated. |
|
2016 | + * |
|
2017 | + * @return EEM_Base | EEM_CPT_Base |
|
2018 | + * @throws ReflectionException |
|
2019 | + * @throws InvalidArgumentException |
|
2020 | + * @throws InvalidInterfaceException |
|
2021 | + * @throws InvalidDataTypeException |
|
2022 | + * @throws EE_Error |
|
2023 | + */ |
|
2024 | + public function get_model() |
|
2025 | + { |
|
2026 | + if (! $this->_model) { |
|
2027 | + $modelName = self::_get_model_classname(get_class($this)); |
|
2028 | + $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
2029 | + } |
|
2030 | + return $this->_model; |
|
2031 | + } |
|
2032 | + |
|
2033 | + |
|
2034 | + /** |
|
2035 | + * @param $props_n_values |
|
2036 | + * @param $classname |
|
2037 | + * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
2038 | + * @throws ReflectionException |
|
2039 | + * @throws InvalidArgumentException |
|
2040 | + * @throws InvalidInterfaceException |
|
2041 | + * @throws InvalidDataTypeException |
|
2042 | + * @throws EE_Error |
|
2043 | + */ |
|
2044 | + protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
2045 | + { |
|
2046 | + // TODO: will not work for Term_Relationships because they have no PK! |
|
2047 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
2048 | + if ( |
|
2049 | + array_key_exists($primary_id_ref, $props_n_values) |
|
2050 | + && ! empty($props_n_values[ $primary_id_ref ]) |
|
2051 | + ) { |
|
2052 | + $id = $props_n_values[ $primary_id_ref ]; |
|
2053 | + return self::_get_model($classname)->get_from_entity_map($id); |
|
2054 | + } |
|
2055 | + return false; |
|
2056 | + } |
|
2057 | + |
|
2058 | + |
|
2059 | + /** |
|
2060 | + * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
2061 | + * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
2062 | + * 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 |
|
2063 | + * we return false. |
|
2064 | + * |
|
2065 | + * @param array $props_n_values incoming array of properties and their values |
|
2066 | + * @param string $classname the classname of the child class |
|
2067 | + * @param null $timezone |
|
2068 | + * @param array $date_formats incoming date_formats in an array where the first value is the |
|
2069 | + * date_format and the second value is the time format |
|
2070 | + * @return mixed (EE_Base_Class|bool) |
|
2071 | + * @throws InvalidArgumentException |
|
2072 | + * @throws InvalidInterfaceException |
|
2073 | + * @throws InvalidDataTypeException |
|
2074 | + * @throws EE_Error |
|
2075 | + * @throws ReflectionException |
|
2076 | + * @throws ReflectionException |
|
2077 | + * @throws ReflectionException |
|
2078 | + */ |
|
2079 | + protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
2080 | + { |
|
2081 | + $existing = null; |
|
2082 | + $model = self::_get_model($classname, $timezone); |
|
2083 | + if ($model->has_primary_key_field()) { |
|
2084 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
2085 | + if ( |
|
2086 | + array_key_exists($primary_id_ref, $props_n_values) |
|
2087 | + && ! empty($props_n_values[ $primary_id_ref ]) |
|
2088 | + ) { |
|
2089 | + $existing = $model->get_one_by_ID( |
|
2090 | + $props_n_values[ $primary_id_ref ] |
|
2091 | + ); |
|
2092 | + } |
|
2093 | + } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
2094 | + // no primary key on this model, but there's still a matching item in the DB |
|
2095 | + $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
2096 | + self::_get_model($classname, $timezone) |
|
2097 | + ->get_index_primary_key_string($props_n_values) |
|
2098 | + ); |
|
2099 | + } |
|
2100 | + if ($existing) { |
|
2101 | + // set date formats if present before setting values |
|
2102 | + if (! empty($date_formats) && is_array($date_formats)) { |
|
2103 | + $existing->set_date_format($date_formats[0]); |
|
2104 | + $existing->set_time_format($date_formats[1]); |
|
2105 | + } else { |
|
2106 | + // set default formats for date and time |
|
2107 | + $existing->set_date_format(get_option('date_format')); |
|
2108 | + $existing->set_time_format(get_option('time_format')); |
|
2109 | + } |
|
2110 | + foreach ($props_n_values as $property => $field_value) { |
|
2111 | + $existing->set($property, $field_value); |
|
2112 | + } |
|
2113 | + return $existing; |
|
2114 | + } |
|
2115 | + return false; |
|
2116 | + } |
|
2117 | + |
|
2118 | + |
|
2119 | + /** |
|
2120 | + * Gets the EEM_*_Model for this class |
|
2121 | + * |
|
2122 | + * @access public now, as this is more convenient |
|
2123 | + * @param $classname |
|
2124 | + * @param null $timezone |
|
2125 | + * @throws ReflectionException |
|
2126 | + * @throws InvalidArgumentException |
|
2127 | + * @throws InvalidInterfaceException |
|
2128 | + * @throws InvalidDataTypeException |
|
2129 | + * @throws EE_Error |
|
2130 | + * @return EEM_Base |
|
2131 | + */ |
|
2132 | + protected static function _get_model($classname, $timezone = null) |
|
2133 | + { |
|
2134 | + // find model for this class |
|
2135 | + if (! $classname) { |
|
2136 | + throw new EE_Error( |
|
2137 | + sprintf( |
|
2138 | + esc_html__( |
|
2139 | + 'What were you thinking calling _get_model(%s)?? You need to specify the class name', |
|
2140 | + 'event_espresso' |
|
2141 | + ), |
|
2142 | + $classname |
|
2143 | + ) |
|
2144 | + ); |
|
2145 | + } |
|
2146 | + $modelName = self::_get_model_classname($classname); |
|
2147 | + return self::_get_model_instance_with_name($modelName, $timezone); |
|
2148 | + } |
|
2149 | + |
|
2150 | + |
|
2151 | + /** |
|
2152 | + * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
2153 | + * |
|
2154 | + * @param string $model_classname |
|
2155 | + * @param null $timezone |
|
2156 | + * @return EEM_Base |
|
2157 | + * @throws ReflectionException |
|
2158 | + * @throws InvalidArgumentException |
|
2159 | + * @throws InvalidInterfaceException |
|
2160 | + * @throws InvalidDataTypeException |
|
2161 | + * @throws EE_Error |
|
2162 | + */ |
|
2163 | + protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
2164 | + { |
|
2165 | + $model_classname = str_replace('EEM_', '', $model_classname); |
|
2166 | + $model = EE_Registry::instance()->load_model($model_classname); |
|
2167 | + $model->set_timezone($timezone); |
|
2168 | + return $model; |
|
2169 | + } |
|
2170 | + |
|
2171 | + |
|
2172 | + /** |
|
2173 | + * If a model name is provided (eg Registration), gets the model classname for that model. |
|
2174 | + * Also works if a model class's classname is provided (eg EE_Registration). |
|
2175 | + * |
|
2176 | + * @param null $model_name |
|
2177 | + * @return string like EEM_Attendee |
|
2178 | + */ |
|
2179 | + private static function _get_model_classname($model_name = null) |
|
2180 | + { |
|
2181 | + if (strpos($model_name, 'EE_') === 0) { |
|
2182 | + $model_classname = str_replace('EE_', 'EEM_', $model_name); |
|
2183 | + } else { |
|
2184 | + $model_classname = 'EEM_' . $model_name; |
|
2185 | + } |
|
2186 | + return $model_classname; |
|
2187 | + } |
|
2188 | + |
|
2189 | + |
|
2190 | + /** |
|
2191 | + * returns the name of the primary key attribute |
|
2192 | + * |
|
2193 | + * @param null $classname |
|
2194 | + * @throws ReflectionException |
|
2195 | + * @throws InvalidArgumentException |
|
2196 | + * @throws InvalidInterfaceException |
|
2197 | + * @throws InvalidDataTypeException |
|
2198 | + * @throws EE_Error |
|
2199 | + * @return string |
|
2200 | + */ |
|
2201 | + protected static function _get_primary_key_name($classname = null) |
|
2202 | + { |
|
2203 | + if (! $classname) { |
|
2204 | + throw new EE_Error( |
|
2205 | + sprintf( |
|
2206 | + esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
|
2207 | + $classname |
|
2208 | + ) |
|
2209 | + ); |
|
2210 | + } |
|
2211 | + return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
2212 | + } |
|
2213 | + |
|
2214 | + |
|
2215 | + /** |
|
2216 | + * Gets the value of the primary key. |
|
2217 | + * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
2218 | + * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
2219 | + * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
2220 | + * |
|
2221 | + * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
2222 | + * @throws ReflectionException |
|
2223 | + * @throws InvalidArgumentException |
|
2224 | + * @throws InvalidInterfaceException |
|
2225 | + * @throws InvalidDataTypeException |
|
2226 | + * @throws EE_Error |
|
2227 | + */ |
|
2228 | + public function ID() |
|
2229 | + { |
|
2230 | + // now that we know the name of the variable, use a variable variable to get its value and return its |
|
2231 | + if ($this->_model->has_primary_key_field()) { |
|
2232 | + return $this->_fields[ $this->_model->primary_key_name() ]; |
|
2233 | + } |
|
2234 | + return $this->_model->get_index_primary_key_string($this->_fields); |
|
2235 | + } |
|
2236 | + |
|
2237 | + |
|
2238 | + /** |
|
2239 | + * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
2240 | + * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
2241 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
2242 | + * |
|
2243 | + * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
2244 | + * @param string $relationName eg 'Events','Question',etc. |
|
2245 | + * an attendee to a group, you also want to specify which role they |
|
2246 | + * will have in that group. So you would use this parameter to |
|
2247 | + * specify array('role-column-name'=>'role-id') |
|
2248 | + * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
2249 | + * allow you to further constrict the relation to being added. |
|
2250 | + * However, keep in mind that the columns (keys) given must match a |
|
2251 | + * column on the JOIN table and currently only the HABTM models |
|
2252 | + * accept these additional conditions. Also remember that if an |
|
2253 | + * exact match isn't found for these extra cols/val pairs, then a |
|
2254 | + * NEW row is created in the join table. |
|
2255 | + * @param null $cache_id |
|
2256 | + * @throws ReflectionException |
|
2257 | + * @throws InvalidArgumentException |
|
2258 | + * @throws InvalidInterfaceException |
|
2259 | + * @throws InvalidDataTypeException |
|
2260 | + * @throws EE_Error |
|
2261 | + * @return EE_Base_Class the object the relation was added to |
|
2262 | + */ |
|
2263 | + public function _add_relation_to( |
|
2264 | + $otherObjectModelObjectOrID, |
|
2265 | + $relationName, |
|
2266 | + $extra_join_model_fields_n_values = array(), |
|
2267 | + $cache_id = null |
|
2268 | + ) { |
|
2269 | + // if this thing exists in the DB, save the relation to the DB |
|
2270 | + if ($this->ID()) { |
|
2271 | + $otherObject = $this->_model->add_relationship_to( |
|
2272 | + $this, |
|
2273 | + $otherObjectModelObjectOrID, |
|
2274 | + $relationName, |
|
2275 | + $extra_join_model_fields_n_values |
|
2276 | + ); |
|
2277 | + // clear cache so future get_many_related and get_first_related() return new results. |
|
2278 | + $this->clear_cache($relationName, $otherObject, true); |
|
2279 | + if ($otherObject instanceof EE_Base_Class) { |
|
2280 | + $otherObject->clear_cache($this->_model->get_this_model_name(), $this); |
|
2281 | + } |
|
2282 | + } else { |
|
2283 | + // this thing doesn't exist in the DB, so just cache it |
|
2284 | + if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
2285 | + throw new EE_Error( |
|
2286 | + sprintf( |
|
2287 | + esc_html__( |
|
2288 | + 'Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s', |
|
2289 | + 'event_espresso' |
|
2290 | + ), |
|
2291 | + $otherObjectModelObjectOrID, |
|
2292 | + get_class($this) |
|
2293 | + ) |
|
2294 | + ); |
|
2295 | + } |
|
2296 | + $otherObject = $otherObjectModelObjectOrID; |
|
2297 | + $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
2298 | + } |
|
2299 | + if ($otherObject instanceof EE_Base_Class) { |
|
2300 | + // fix the reciprocal relation too |
|
2301 | + if ($otherObject->ID()) { |
|
2302 | + // its saved so assumed relations exist in the DB, so we can just |
|
2303 | + // clear the cache so future queries use the updated info in the DB |
|
2304 | + $otherObject->clear_cache( |
|
2305 | + $this->_model->get_this_model_name(), |
|
2306 | + null, |
|
2307 | + true |
|
2308 | + ); |
|
2309 | + } else { |
|
2310 | + // it's not saved, so it caches relations like this |
|
2311 | + $otherObject->cache($this->_model->get_this_model_name(), $this); |
|
2312 | + } |
|
2313 | + } |
|
2314 | + return $otherObject; |
|
2315 | + } |
|
2316 | + |
|
2317 | + |
|
2318 | + /** |
|
2319 | + * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
2320 | + * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
2321 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
2322 | + * from the cache |
|
2323 | + * |
|
2324 | + * @param mixed $otherObjectModelObjectOrID |
|
2325 | + * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
2326 | + * to the DB yet |
|
2327 | + * @param string $relationName |
|
2328 | + * @param array $where_query |
|
2329 | + * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
2330 | + * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
2331 | + * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
2332 | + * remember that if an exact match isn't found for these extra cols/val pairs, then no row is |
|
2333 | + * deleted. |
|
2334 | + * @return EE_Base_Class the relation was removed from |
|
2335 | + * @throws ReflectionException |
|
2336 | + * @throws InvalidArgumentException |
|
2337 | + * @throws InvalidInterfaceException |
|
2338 | + * @throws InvalidDataTypeException |
|
2339 | + * @throws EE_Error |
|
2340 | + */ |
|
2341 | + public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
2342 | + { |
|
2343 | + if ($this->ID()) { |
|
2344 | + // if this exists in the DB, save the relation change to the DB too |
|
2345 | + $otherObject = $this->_model->remove_relationship_to( |
|
2346 | + $this, |
|
2347 | + $otherObjectModelObjectOrID, |
|
2348 | + $relationName, |
|
2349 | + $where_query |
|
2350 | + ); |
|
2351 | + $this->clear_cache( |
|
2352 | + $relationName, |
|
2353 | + $otherObject |
|
2354 | + ); |
|
2355 | + } else { |
|
2356 | + // this doesn't exist in the DB, just remove it from the cache |
|
2357 | + $otherObject = $this->clear_cache( |
|
2358 | + $relationName, |
|
2359 | + $otherObjectModelObjectOrID |
|
2360 | + ); |
|
2361 | + } |
|
2362 | + if ($otherObject instanceof EE_Base_Class) { |
|
2363 | + $otherObject->clear_cache( |
|
2364 | + $this->_model->get_this_model_name(), |
|
2365 | + $this |
|
2366 | + ); |
|
2367 | + } |
|
2368 | + return $otherObject; |
|
2369 | + } |
|
2370 | + |
|
2371 | + |
|
2372 | + /** |
|
2373 | + * Removes ALL the related things for the $relationName. |
|
2374 | + * |
|
2375 | + * @param string $relationName |
|
2376 | + * @param array $where_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
2377 | + * @return EE_Base_Class |
|
2378 | + * @throws ReflectionException |
|
2379 | + * @throws InvalidArgumentException |
|
2380 | + * @throws InvalidInterfaceException |
|
2381 | + * @throws InvalidDataTypeException |
|
2382 | + * @throws EE_Error |
|
2383 | + */ |
|
2384 | + public function _remove_relations($relationName, $where_query_params = array()) |
|
2385 | + { |
|
2386 | + if ($this->ID()) { |
|
2387 | + // if this exists in the DB, save the relation change to the DB too |
|
2388 | + $otherObjects = $this->_model->remove_relations( |
|
2389 | + $this, |
|
2390 | + $relationName, |
|
2391 | + $where_query_params |
|
2392 | + ); |
|
2393 | + $this->clear_cache( |
|
2394 | + $relationName, |
|
2395 | + null, |
|
2396 | + true |
|
2397 | + ); |
|
2398 | + } else { |
|
2399 | + // this doesn't exist in the DB, just remove it from the cache |
|
2400 | + $otherObjects = $this->clear_cache( |
|
2401 | + $relationName, |
|
2402 | + null, |
|
2403 | + true |
|
2404 | + ); |
|
2405 | + } |
|
2406 | + if (is_array($otherObjects)) { |
|
2407 | + foreach ($otherObjects as $otherObject) { |
|
2408 | + $otherObject->clear_cache( |
|
2409 | + $this->_model->get_this_model_name(), |
|
2410 | + $this |
|
2411 | + ); |
|
2412 | + } |
|
2413 | + } |
|
2414 | + return $otherObjects; |
|
2415 | + } |
|
2416 | + |
|
2417 | + |
|
2418 | + /** |
|
2419 | + * Gets all the related model objects of the specified type. Eg, if the current class if |
|
2420 | + * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
2421 | + * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
2422 | + * because we want to get even deleted items etc. |
|
2423 | + * |
|
2424 | + * @param string $relationName key in the model's _model_relations array |
|
2425 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
2426 | + * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary |
|
2427 | + * keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these |
|
2428 | + * results if you want IDs |
|
2429 | + * @throws ReflectionException |
|
2430 | + * @throws InvalidArgumentException |
|
2431 | + * @throws InvalidInterfaceException |
|
2432 | + * @throws InvalidDataTypeException |
|
2433 | + * @throws EE_Error |
|
2434 | + */ |
|
2435 | + public function get_many_related($relationName, $query_params = array()) |
|
2436 | + { |
|
2437 | + if ($this->ID()) { |
|
2438 | + // this exists in the DB, so get the related things from either the cache or the DB |
|
2439 | + // if there are query parameters, forget about caching the related model objects. |
|
2440 | + if ($query_params) { |
|
2441 | + $related_model_objects = $this->_model->get_all_related( |
|
2442 | + $this, |
|
2443 | + $relationName, |
|
2444 | + $query_params |
|
2445 | + ); |
|
2446 | + } else { |
|
2447 | + // did we already cache the result of this query? |
|
2448 | + $cached_results = $this->get_all_from_cache($relationName); |
|
2449 | + if (! $cached_results) { |
|
2450 | + $related_model_objects = $this->_model->get_all_related( |
|
2451 | + $this, |
|
2452 | + $relationName, |
|
2453 | + $query_params |
|
2454 | + ); |
|
2455 | + // if no query parameters were passed, then we got all the related model objects |
|
2456 | + // for that relation. We can cache them then. |
|
2457 | + foreach ($related_model_objects as $related_model_object) { |
|
2458 | + $this->cache($relationName, $related_model_object); |
|
2459 | + } |
|
2460 | + } else { |
|
2461 | + $related_model_objects = $cached_results; |
|
2462 | + } |
|
2463 | + } |
|
2464 | + } else { |
|
2465 | + // this doesn't exist in the DB, so just get the related things from the cache |
|
2466 | + $related_model_objects = $this->get_all_from_cache($relationName); |
|
2467 | + } |
|
2468 | + return $related_model_objects; |
|
2469 | + } |
|
2470 | + |
|
2471 | + |
|
2472 | + /** |
|
2473 | + * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
2474 | + * unless otherwise specified in the $query_params |
|
2475 | + * |
|
2476 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
2477 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2478 | + * @param string $field_to_count name of field to count by. By default, uses primary key |
|
2479 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
2480 | + * that by the setting $distinct to TRUE; |
|
2481 | + * @return int |
|
2482 | + * @throws ReflectionException |
|
2483 | + * @throws InvalidArgumentException |
|
2484 | + * @throws InvalidInterfaceException |
|
2485 | + * @throws InvalidDataTypeException |
|
2486 | + * @throws EE_Error |
|
2487 | + */ |
|
2488 | + public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
2489 | + { |
|
2490 | + return $this->_model->count_related( |
|
2491 | + $this, |
|
2492 | + $relation_name, |
|
2493 | + $query_params, |
|
2494 | + $field_to_count, |
|
2495 | + $distinct |
|
2496 | + ); |
|
2497 | + } |
|
2498 | + |
|
2499 | + |
|
2500 | + /** |
|
2501 | + * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
2502 | + * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
2503 | + * |
|
2504 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
2505 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2506 | + * @param string $field_to_sum name of field to count by. |
|
2507 | + * By default, uses primary key |
|
2508 | + * (which doesn't make much sense, so you should probably change it) |
|
2509 | + * @return int |
|
2510 | + * @throws ReflectionException |
|
2511 | + * @throws InvalidArgumentException |
|
2512 | + * @throws InvalidInterfaceException |
|
2513 | + * @throws InvalidDataTypeException |
|
2514 | + * @throws EE_Error |
|
2515 | + */ |
|
2516 | + public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
2517 | + { |
|
2518 | + return $this->_model->sum_related( |
|
2519 | + $this, |
|
2520 | + $relation_name, |
|
2521 | + $query_params, |
|
2522 | + $field_to_sum |
|
2523 | + ); |
|
2524 | + } |
|
2525 | + |
|
2526 | + |
|
2527 | + /** |
|
2528 | + * Gets the first (ie, one) related model object of the specified type. |
|
2529 | + * |
|
2530 | + * @param string $relationName key in the model's _model_relations array |
|
2531 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2532 | + * @return EE_Base_Class (not an array, a single object) |
|
2533 | + * @throws ReflectionException |
|
2534 | + * @throws InvalidArgumentException |
|
2535 | + * @throws InvalidInterfaceException |
|
2536 | + * @throws InvalidDataTypeException |
|
2537 | + * @throws EE_Error |
|
2538 | + */ |
|
2539 | + public function get_first_related($relationName, $query_params = array()) |
|
2540 | + { |
|
2541 | + $model_relation = $this->_model->related_settings_for($relationName); |
|
2542 | + if ($this->ID()) {// this exists in the DB, get from the cache OR the DB |
|
2543 | + // if they've provided some query parameters, don't bother trying to cache the result |
|
2544 | + // also make sure we're not caching the result of get_first_related |
|
2545 | + // on a relation which should have an array of objects (because the cache might have an array of objects) |
|
2546 | + if ( |
|
2547 | + $query_params |
|
2548 | + || ! $model_relation instanceof EE_Belongs_To_Relation |
|
2549 | + ) { |
|
2550 | + $related_model_object = $this->_model->get_first_related( |
|
2551 | + $this, |
|
2552 | + $relationName, |
|
2553 | + $query_params |
|
2554 | + ); |
|
2555 | + } else { |
|
2556 | + // first, check if we've already cached the result of this query |
|
2557 | + $cached_result = $this->get_one_from_cache($relationName); |
|
2558 | + if (! $cached_result) { |
|
2559 | + $related_model_object = $this->_model->get_first_related( |
|
2560 | + $this, |
|
2561 | + $relationName, |
|
2562 | + $query_params |
|
2563 | + ); |
|
2564 | + $this->cache($relationName, $related_model_object); |
|
2565 | + } else { |
|
2566 | + $related_model_object = $cached_result; |
|
2567 | + } |
|
2568 | + } |
|
2569 | + } else { |
|
2570 | + $related_model_object = null; |
|
2571 | + // this doesn't exist in the Db, |
|
2572 | + // but maybe the relation is of type belongs to, and so the related thing might |
|
2573 | + if ($model_relation instanceof EE_Belongs_To_Relation) { |
|
2574 | + $related_model_object = $this->_model->get_first_related( |
|
2575 | + $this, |
|
2576 | + $relationName, |
|
2577 | + $query_params |
|
2578 | + ); |
|
2579 | + } |
|
2580 | + // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
|
2581 | + // just get what's cached on this object |
|
2582 | + if (! $related_model_object) { |
|
2583 | + $related_model_object = $this->get_one_from_cache($relationName); |
|
2584 | + } |
|
2585 | + } |
|
2586 | + return $related_model_object; |
|
2587 | + } |
|
2588 | + |
|
2589 | + |
|
2590 | + /** |
|
2591 | + * Does a delete on all related objects of type $relationName and removes |
|
2592 | + * the current model object's relation to them. If they can't be deleted (because |
|
2593 | + * of blocking related model objects) does nothing. If the related model objects are |
|
2594 | + * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
2595 | + * If this model object doesn't exist yet in the DB, just removes its related things |
|
2596 | + * |
|
2597 | + * @param string $relationName |
|
2598 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2599 | + * @return int how many deleted |
|
2600 | + * @throws ReflectionException |
|
2601 | + * @throws InvalidArgumentException |
|
2602 | + * @throws InvalidInterfaceException |
|
2603 | + * @throws InvalidDataTypeException |
|
2604 | + * @throws EE_Error |
|
2605 | + */ |
|
2606 | + public function delete_related($relationName, $query_params = array()) |
|
2607 | + { |
|
2608 | + if ($this->ID()) { |
|
2609 | + $count = $this->_model->delete_related( |
|
2610 | + $this, |
|
2611 | + $relationName, |
|
2612 | + $query_params |
|
2613 | + ); |
|
2614 | + } else { |
|
2615 | + $count = count($this->get_all_from_cache($relationName)); |
|
2616 | + $this->clear_cache($relationName, null, true); |
|
2617 | + } |
|
2618 | + return $count; |
|
2619 | + } |
|
2620 | + |
|
2621 | + |
|
2622 | + /** |
|
2623 | + * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
2624 | + * the current model object's relation to them. If they can't be deleted (because |
|
2625 | + * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
2626 | + * If the related thing isn't a soft-deletable model object, this function is identical |
|
2627 | + * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
2628 | + * |
|
2629 | + * @param string $relationName |
|
2630 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2631 | + * @return int how many deleted (including those soft deleted) |
|
2632 | + * @throws ReflectionException |
|
2633 | + * @throws InvalidArgumentException |
|
2634 | + * @throws InvalidInterfaceException |
|
2635 | + * @throws InvalidDataTypeException |
|
2636 | + * @throws EE_Error |
|
2637 | + */ |
|
2638 | + public function delete_related_permanently($relationName, $query_params = array()) |
|
2639 | + { |
|
2640 | + if ($this->ID()) { |
|
2641 | + $count = $this->_model->delete_related_permanently( |
|
2642 | + $this, |
|
2643 | + $relationName, |
|
2644 | + $query_params |
|
2645 | + ); |
|
2646 | + } else { |
|
2647 | + $count = count($this->get_all_from_cache($relationName)); |
|
2648 | + } |
|
2649 | + $this->clear_cache($relationName, null, true); |
|
2650 | + return $count; |
|
2651 | + } |
|
2652 | + |
|
2653 | + |
|
2654 | + /** |
|
2655 | + * is_set |
|
2656 | + * Just a simple utility function children can use for checking if property exists |
|
2657 | + * |
|
2658 | + * @access public |
|
2659 | + * @param string $field_name property to check |
|
2660 | + * @return bool TRUE if existing,FALSE if not. |
|
2661 | + */ |
|
2662 | + public function is_set($field_name) |
|
2663 | + { |
|
2664 | + return isset($this->_fields[ $field_name ]); |
|
2665 | + } |
|
2666 | + |
|
2667 | + |
|
2668 | + /** |
|
2669 | + * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
2670 | + * EE_Error exception if they don't |
|
2671 | + * |
|
2672 | + * @param mixed (string|array) $properties properties to check |
|
2673 | + * @throws EE_Error |
|
2674 | + * @return bool TRUE if existing, throw EE_Error if not. |
|
2675 | + */ |
|
2676 | + protected function _property_exists($properties) |
|
2677 | + { |
|
2678 | + foreach ((array) $properties as $property_name) { |
|
2679 | + // first make sure this property exists |
|
2680 | + if (! $this->_fields[ $property_name ]) { |
|
2681 | + throw new EE_Error( |
|
2682 | + sprintf( |
|
2683 | + esc_html__( |
|
2684 | + 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
2685 | + 'event_espresso' |
|
2686 | + ), |
|
2687 | + $property_name |
|
2688 | + ) |
|
2689 | + ); |
|
2690 | + } |
|
2691 | + } |
|
2692 | + return true; |
|
2693 | + } |
|
2694 | + |
|
2695 | + |
|
2696 | + /** |
|
2697 | + * This simply returns an array of model fields for this object |
|
2698 | + * |
|
2699 | + * @return array |
|
2700 | + * @throws ReflectionException |
|
2701 | + * @throws InvalidArgumentException |
|
2702 | + * @throws InvalidInterfaceException |
|
2703 | + * @throws InvalidDataTypeException |
|
2704 | + * @throws EE_Error |
|
2705 | + */ |
|
2706 | + public function model_field_array() |
|
2707 | + { |
|
2708 | + $fields = $this->_model->field_settings(false); |
|
2709 | + $properties = array(); |
|
2710 | + // remove prepended underscore |
|
2711 | + foreach ($fields as $field_name => $settings) { |
|
2712 | + $properties[ $field_name ] = $this->get($field_name); |
|
2713 | + } |
|
2714 | + return $properties; |
|
2715 | + } |
|
2716 | + |
|
2717 | + |
|
2718 | + /** |
|
2719 | + * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
2720 | + * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
2721 | + * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. |
|
2722 | + * Instead of requiring a plugin to extend the EE_Base_Class |
|
2723 | + * (which works fine is there's only 1 plugin, but when will that happen?) |
|
2724 | + * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' |
|
2725 | + * (eg, filters_hook_espresso__EE_Answer__my_great_function) |
|
2726 | + * and accepts 2 arguments: the object on which the function was called, |
|
2727 | + * and an array of the original arguments passed to the function. |
|
2728 | + * Whatever their callback function returns will be returned by this function. |
|
2729 | + * Example: in functions.php (or in a plugin): |
|
2730 | + * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); |
|
2731 | + * function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
2732 | + * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
2733 | + * return $previousReturnValue.$returnString; |
|
2734 | + * } |
|
2735 | + * require('EE_Answer.class.php'); |
|
2736 | + * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
2737 | + * echo $answer->my_callback('monkeys',100); |
|
2738 | + * //will output "you called my_callback! and passed args:monkeys,100" |
|
2739 | + * |
|
2740 | + * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
2741 | + * @param array $args array of original arguments passed to the function |
|
2742 | + * @throws EE_Error |
|
2743 | + * @return mixed whatever the plugin which calls add_filter decides |
|
2744 | + */ |
|
2745 | + public function __call($methodName, $args) |
|
2746 | + { |
|
2747 | + $className = get_class($this); |
|
2748 | + $tagName = "FHEE__{$className}__{$methodName}"; |
|
2749 | + if (! has_filter($tagName)) { |
|
2750 | + throw new EE_Error( |
|
2751 | + sprintf( |
|
2752 | + esc_html__( |
|
2753 | + "Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}", |
|
2754 | + 'event_espresso' |
|
2755 | + ), |
|
2756 | + $methodName, |
|
2757 | + $className, |
|
2758 | + $tagName |
|
2759 | + ) |
|
2760 | + ); |
|
2761 | + } |
|
2762 | + return apply_filters($tagName, null, $this, $args); |
|
2763 | + } |
|
2764 | + |
|
2765 | + |
|
2766 | + /** |
|
2767 | + * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
2768 | + * A $previous_value can be specified in case there are many meta rows with the same key |
|
2769 | + * |
|
2770 | + * @param string $meta_key |
|
2771 | + * @param mixed $meta_value |
|
2772 | + * @param mixed $previous_value |
|
2773 | + * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
2774 | + * NOTE: if the values haven't changed, returns 0 |
|
2775 | + * @throws InvalidArgumentException |
|
2776 | + * @throws InvalidInterfaceException |
|
2777 | + * @throws InvalidDataTypeException |
|
2778 | + * @throws EE_Error |
|
2779 | + * @throws ReflectionException |
|
2780 | + */ |
|
2781 | + public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
2782 | + { |
|
2783 | + $query_params = array( |
|
2784 | + array( |
|
2785 | + 'EXM_key' => $meta_key, |
|
2786 | + 'OBJ_ID' => $this->ID(), |
|
2787 | + 'EXM_type' => $this->_model->get_this_model_name(), |
|
2788 | + ), |
|
2789 | + ); |
|
2790 | + if ($previous_value !== null) { |
|
2791 | + $query_params[0]['EXM_value'] = $meta_value; |
|
2792 | + } |
|
2793 | + $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
2794 | + if (! $existing_rows_like_that) { |
|
2795 | + return $this->add_extra_meta($meta_key, $meta_value); |
|
2796 | + } |
|
2797 | + foreach ($existing_rows_like_that as $existing_row) { |
|
2798 | + $existing_row->save(array('EXM_value' => $meta_value)); |
|
2799 | + } |
|
2800 | + return count($existing_rows_like_that); |
|
2801 | + } |
|
2802 | + |
|
2803 | + |
|
2804 | + /** |
|
2805 | + * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
2806 | + * no other extra meta for this model object have the same key. Returns TRUE if the |
|
2807 | + * extra meta row was entered, false if not |
|
2808 | + * |
|
2809 | + * @param string $meta_key |
|
2810 | + * @param mixed $meta_value |
|
2811 | + * @param boolean $unique |
|
2812 | + * @return boolean |
|
2813 | + * @throws InvalidArgumentException |
|
2814 | + * @throws InvalidInterfaceException |
|
2815 | + * @throws InvalidDataTypeException |
|
2816 | + * @throws EE_Error |
|
2817 | + * @throws ReflectionException |
|
2818 | + * @throws ReflectionException |
|
2819 | + */ |
|
2820 | + public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
2821 | + { |
|
2822 | + if ($unique) { |
|
2823 | + $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
2824 | + array( |
|
2825 | + array( |
|
2826 | + 'EXM_key' => $meta_key, |
|
2827 | + 'OBJ_ID' => $this->ID(), |
|
2828 | + 'EXM_type' => $this->_model->get_this_model_name(), |
|
2829 | + ), |
|
2830 | + ) |
|
2831 | + ); |
|
2832 | + if ($existing_extra_meta) { |
|
2833 | + return false; |
|
2834 | + } |
|
2835 | + } |
|
2836 | + $new_extra_meta = EE_Extra_Meta::new_instance( |
|
2837 | + array( |
|
2838 | + 'EXM_key' => $meta_key, |
|
2839 | + 'EXM_value' => $meta_value, |
|
2840 | + 'OBJ_ID' => $this->ID(), |
|
2841 | + 'EXM_type' => $this->_model->get_this_model_name(), |
|
2842 | + ) |
|
2843 | + ); |
|
2844 | + $new_extra_meta->save(); |
|
2845 | + return true; |
|
2846 | + } |
|
2847 | + |
|
2848 | + |
|
2849 | + /** |
|
2850 | + * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
2851 | + * is specified, only deletes extra meta records with that value. |
|
2852 | + * |
|
2853 | + * @param string $meta_key |
|
2854 | + * @param mixed $meta_value |
|
2855 | + * @return int number of extra meta rows deleted |
|
2856 | + * @throws InvalidArgumentException |
|
2857 | + * @throws InvalidInterfaceException |
|
2858 | + * @throws InvalidDataTypeException |
|
2859 | + * @throws EE_Error |
|
2860 | + * @throws ReflectionException |
|
2861 | + */ |
|
2862 | + public function delete_extra_meta($meta_key, $meta_value = null) |
|
2863 | + { |
|
2864 | + $query_params = array( |
|
2865 | + array( |
|
2866 | + 'EXM_key' => $meta_key, |
|
2867 | + 'OBJ_ID' => $this->ID(), |
|
2868 | + 'EXM_type' => $this->_model->get_this_model_name(), |
|
2869 | + ), |
|
2870 | + ); |
|
2871 | + if ($meta_value !== null) { |
|
2872 | + $query_params[0]['EXM_value'] = $meta_value; |
|
2873 | + } |
|
2874 | + return EEM_Extra_Meta::instance()->delete($query_params); |
|
2875 | + } |
|
2876 | + |
|
2877 | + |
|
2878 | + /** |
|
2879 | + * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
2880 | + * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
2881 | + * You can specify $default is case you haven't found the extra meta |
|
2882 | + * |
|
2883 | + * @param string $meta_key |
|
2884 | + * @param boolean $single |
|
2885 | + * @param mixed $default if we don't find anything, what should we return? |
|
2886 | + * @return mixed single value if $single; array if ! $single |
|
2887 | + * @throws ReflectionException |
|
2888 | + * @throws InvalidArgumentException |
|
2889 | + * @throws InvalidInterfaceException |
|
2890 | + * @throws InvalidDataTypeException |
|
2891 | + * @throws EE_Error |
|
2892 | + */ |
|
2893 | + public function get_extra_meta($meta_key, $single = false, $default = null) |
|
2894 | + { |
|
2895 | + if ($single) { |
|
2896 | + $result = $this->get_first_related( |
|
2897 | + 'Extra_Meta', |
|
2898 | + array(array('EXM_key' => $meta_key)) |
|
2899 | + ); |
|
2900 | + if ($result instanceof EE_Extra_Meta) { |
|
2901 | + return $result->value(); |
|
2902 | + } |
|
2903 | + } else { |
|
2904 | + $results = $this->get_many_related( |
|
2905 | + 'Extra_Meta', |
|
2906 | + array(array('EXM_key' => $meta_key)) |
|
2907 | + ); |
|
2908 | + if ($results) { |
|
2909 | + $values = array(); |
|
2910 | + foreach ($results as $result) { |
|
2911 | + if ($result instanceof EE_Extra_Meta) { |
|
2912 | + $values[ $result->ID() ] = $result->value(); |
|
2913 | + } |
|
2914 | + } |
|
2915 | + return $values; |
|
2916 | + } |
|
2917 | + } |
|
2918 | + // if nothing discovered yet return default. |
|
2919 | + return apply_filters( |
|
2920 | + 'FHEE__EE_Base_Class__get_extra_meta__default_value', |
|
2921 | + $default, |
|
2922 | + $meta_key, |
|
2923 | + $single, |
|
2924 | + $this |
|
2925 | + ); |
|
2926 | + } |
|
2927 | + |
|
2928 | + |
|
2929 | + /** |
|
2930 | + * Returns a simple array of all the extra meta associated with this model object. |
|
2931 | + * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
2932 | + * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
2933 | + * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
2934 | + * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
2935 | + * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
2936 | + * finally the extra meta's value as each sub-value. (eg |
|
2937 | + * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
2938 | + * |
|
2939 | + * @param boolean $one_of_each_key |
|
2940 | + * @return array |
|
2941 | + * @throws ReflectionException |
|
2942 | + * @throws InvalidArgumentException |
|
2943 | + * @throws InvalidInterfaceException |
|
2944 | + * @throws InvalidDataTypeException |
|
2945 | + * @throws EE_Error |
|
2946 | + */ |
|
2947 | + public function all_extra_meta_array($one_of_each_key = true) |
|
2948 | + { |
|
2949 | + $return_array = array(); |
|
2950 | + if ($one_of_each_key) { |
|
2951 | + $extra_meta_objs = $this->get_many_related( |
|
2952 | + 'Extra_Meta', |
|
2953 | + array('group_by' => 'EXM_key') |
|
2954 | + ); |
|
2955 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
2956 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
2957 | + $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
2958 | + } |
|
2959 | + } |
|
2960 | + } else { |
|
2961 | + $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
2962 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
2963 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
2964 | + if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
2965 | + $return_array[ $extra_meta_obj->key() ] = array(); |
|
2966 | + } |
|
2967 | + $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
2968 | + } |
|
2969 | + } |
|
2970 | + } |
|
2971 | + return $return_array; |
|
2972 | + } |
|
2973 | + |
|
2974 | + |
|
2975 | + /** |
|
2976 | + * Gets a pretty nice displayable nice for this model object. Often overridden |
|
2977 | + * |
|
2978 | + * @return string |
|
2979 | + * @throws ReflectionException |
|
2980 | + * @throws InvalidArgumentException |
|
2981 | + * @throws InvalidInterfaceException |
|
2982 | + * @throws InvalidDataTypeException |
|
2983 | + * @throws EE_Error |
|
2984 | + */ |
|
2985 | + public function name() |
|
2986 | + { |
|
2987 | + // find a field that's not a text field |
|
2988 | + $field_we_can_use = $this->_model->get_a_field_of_type('EE_Text_Field_Base'); |
|
2989 | + if ($field_we_can_use) { |
|
2990 | + return $this->get($field_we_can_use->get_name()); |
|
2991 | + } |
|
2992 | + $first_few_properties = $this->model_field_array(); |
|
2993 | + $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
2994 | + $name_parts = array(); |
|
2995 | + foreach ($first_few_properties as $name => $value) { |
|
2996 | + $name_parts[] = "$name:$value"; |
|
2997 | + } |
|
2998 | + return implode(',', $name_parts); |
|
2999 | + } |
|
3000 | + |
|
3001 | + |
|
3002 | + /** |
|
3003 | + * in_entity_map |
|
3004 | + * Checks if this model object has been proven to already be in the entity map |
|
3005 | + * |
|
3006 | + * @return boolean |
|
3007 | + * @throws ReflectionException |
|
3008 | + * @throws InvalidArgumentException |
|
3009 | + * @throws InvalidInterfaceException |
|
3010 | + * @throws InvalidDataTypeException |
|
3011 | + * @throws EE_Error |
|
3012 | + */ |
|
3013 | + public function in_entity_map() |
|
3014 | + { |
|
3015 | + // well, if we looked, did we find it in the entity map? |
|
3016 | + return $this->ID() && $this->_model->get_from_entity_map($this->ID()) === $this; |
|
3017 | + } |
|
3018 | + |
|
3019 | + |
|
3020 | + /** |
|
3021 | + * refresh_from_db |
|
3022 | + * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
3023 | + * |
|
3024 | + * @throws ReflectionException |
|
3025 | + * @throws InvalidArgumentException |
|
3026 | + * @throws InvalidInterfaceException |
|
3027 | + * @throws InvalidDataTypeException |
|
3028 | + * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
3029 | + * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
3030 | + */ |
|
3031 | + public function refresh_from_db() |
|
3032 | + { |
|
3033 | + if ($this->ID() && $this->in_entity_map()) { |
|
3034 | + $this->_model->refresh_entity_map_from_db($this->ID()); |
|
3035 | + } else { |
|
3036 | + // if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
3037 | + // if it has an ID but it's not in the map, and you're asking me to refresh it |
|
3038 | + // that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
3039 | + // absolutely nothing in it for this ID |
|
3040 | + if (WP_DEBUG) { |
|
3041 | + throw new EE_Error( |
|
3042 | + sprintf( |
|
3043 | + esc_html__( |
|
3044 | + 'Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.', |
|
3045 | + 'event_espresso' |
|
3046 | + ), |
|
3047 | + $this->ID(), |
|
3048 | + get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
3049 | + get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
3050 | + ) |
|
3051 | + ); |
|
3052 | + } |
|
3053 | + } |
|
3054 | + } |
|
3055 | + |
|
3056 | + |
|
3057 | + /** |
|
3058 | + * Change $fields' values to $new_value_sql (which is a string of raw SQL) |
|
3059 | + * |
|
3060 | + * @since 4.9.80.p |
|
3061 | + * @param EE_Model_Field_Base[] $fields |
|
3062 | + * @param string $new_value_sql |
|
3063 | + * example: 'column_name=123', |
|
3064 | + * or 'column_name=column_name+1', |
|
3065 | + * or 'column_name= CASE |
|
3066 | + * WHEN (`column_name` + `other_column` + 5) <= `yet_another_column` |
|
3067 | + * THEN `column_name` + 5 |
|
3068 | + * ELSE `column_name` |
|
3069 | + * END' |
|
3070 | + * Also updates $field on this model object with the latest value from the database. |
|
3071 | + * @return bool |
|
3072 | + * @throws EE_Error |
|
3073 | + * @throws InvalidArgumentException |
|
3074 | + * @throws InvalidDataTypeException |
|
3075 | + * @throws InvalidInterfaceException |
|
3076 | + * @throws ReflectionException |
|
3077 | + */ |
|
3078 | + protected function updateFieldsInDB($fields, $new_value_sql) |
|
3079 | + { |
|
3080 | + // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB |
|
3081 | + // if it wasn't even there to start off. |
|
3082 | + if (! $this->ID()) { |
|
3083 | + $this->save(); |
|
3084 | + } |
|
3085 | + global $wpdb; |
|
3086 | + if (empty($fields)) { |
|
3087 | + throw new InvalidArgumentException( |
|
3088 | + esc_html__( |
|
3089 | + 'EE_Base_Class::updateFieldsInDB was passed an empty array of fields.', |
|
3090 | + 'event_espresso' |
|
3091 | + ) |
|
3092 | + ); |
|
3093 | + } |
|
3094 | + $first_field = reset($fields); |
|
3095 | + $table_alias = $first_field->get_table_alias(); |
|
3096 | + foreach ($fields as $field) { |
|
3097 | + if ($table_alias !== $field->get_table_alias()) { |
|
3098 | + throw new InvalidArgumentException( |
|
3099 | + sprintf( |
|
3100 | + esc_html__( |
|
3101 | + // @codingStandardsIgnoreStart |
|
3102 | + 'EE_Base_Class::updateFieldsInDB was passed fields for different tables ("%1$s" and "%2$s"), which is not supported. Instead, please call the method multiple times.', |
|
3103 | + // @codingStandardsIgnoreEnd |
|
3104 | + 'event_espresso' |
|
3105 | + ), |
|
3106 | + $table_alias, |
|
3107 | + $field->get_table_alias() |
|
3108 | + ) |
|
3109 | + ); |
|
3110 | + } |
|
3111 | + } |
|
3112 | + // Ok the fields are now known to all be for the same table. Proceed with creating the SQL to update it. |
|
3113 | + $table_obj = $this->_model->get_table_obj_by_alias($table_alias); |
|
3114 | + $table_pk_value = $this->ID(); |
|
3115 | + $table_name = $table_obj->get_table_name(); |
|
3116 | + if ($table_obj instanceof EE_Secondary_Table) { |
|
3117 | + $table_pk_field_name = $table_obj->get_fk_on_table(); |
|
3118 | + } else { |
|
3119 | + $table_pk_field_name = $table_obj->get_pk_column(); |
|
3120 | + } |
|
3121 | + |
|
3122 | + $query = |
|
3123 | + "UPDATE `{$table_name}` |
|
3124 | 3124 | SET " |
3125 | - . $new_value_sql |
|
3126 | - . $wpdb->prepare( |
|
3127 | - " |
|
3125 | + . $new_value_sql |
|
3126 | + . $wpdb->prepare( |
|
3127 | + " |
|
3128 | 3128 | WHERE `{$table_pk_field_name}` = %d;", |
3129 | - $table_pk_value |
|
3130 | - ); |
|
3131 | - $result = $wpdb->query($query); |
|
3132 | - foreach ($fields as $field) { |
|
3133 | - // If it was successful, we'd like to know the new value. |
|
3134 | - // If it failed, we'd also like to know the new value. |
|
3135 | - $new_value = $this->_model->get_var( |
|
3136 | - $this->_model->alter_query_params_to_restrict_by_ID( |
|
3137 | - $this->_model->get_index_primary_key_string( |
|
3138 | - $this->model_field_array() |
|
3139 | - ), |
|
3140 | - array( |
|
3141 | - 'default_where_conditions' => 'minimum', |
|
3142 | - ) |
|
3143 | - ), |
|
3144 | - $field->get_name() |
|
3145 | - ); |
|
3146 | - $this->set_from_db( |
|
3147 | - $field->get_name(), |
|
3148 | - $new_value |
|
3149 | - ); |
|
3150 | - } |
|
3151 | - return (bool) $result; |
|
3152 | - } |
|
3153 | - |
|
3154 | - |
|
3155 | - /** |
|
3156 | - * Nudges $field_name's value by $quantity, without any conditionals (in comparison to bumpConditionally()). |
|
3157 | - * Does not allow negative values, however. |
|
3158 | - * |
|
3159 | - * @since 4.9.80.p |
|
3160 | - * @param array $fields_n_quantities keys are the field names, and values are the amount by which to bump them |
|
3161 | - * (positive or negative). One important gotcha: all these values must be |
|
3162 | - * on the same table (eg don't pass in one field for the posts table and |
|
3163 | - * another for the event meta table.) |
|
3164 | - * @return bool |
|
3165 | - * @throws EE_Error |
|
3166 | - * @throws InvalidArgumentException |
|
3167 | - * @throws InvalidDataTypeException |
|
3168 | - * @throws InvalidInterfaceException |
|
3169 | - * @throws ReflectionException |
|
3170 | - */ |
|
3171 | - public function adjustNumericFieldsInDb(array $fields_n_quantities) |
|
3172 | - { |
|
3173 | - global $wpdb; |
|
3174 | - if (empty($fields_n_quantities)) { |
|
3175 | - // No fields to update? Well sure, we updated them to that value just fine. |
|
3176 | - return true; |
|
3177 | - } |
|
3178 | - $fields = []; |
|
3179 | - $set_sql_statements = []; |
|
3180 | - foreach ($fields_n_quantities as $field_name => $quantity) { |
|
3181 | - $field = $this->_model->field_settings_for($field_name, true); |
|
3182 | - $fields[] = $field; |
|
3183 | - $column_name = $field->get_table_column(); |
|
3184 | - |
|
3185 | - $abs_qty = absint($quantity); |
|
3186 | - if ($quantity > 0) { |
|
3187 | - // don't let the value be negative as often these fields are unsigned |
|
3188 | - $set_sql_statements[] = $wpdb->prepare( |
|
3189 | - "`{$column_name}` = `{$column_name}` + %d", |
|
3190 | - $abs_qty |
|
3191 | - ); |
|
3192 | - } else { |
|
3193 | - $set_sql_statements[] = $wpdb->prepare( |
|
3194 | - "`{$column_name}` = CASE |
|
3129 | + $table_pk_value |
|
3130 | + ); |
|
3131 | + $result = $wpdb->query($query); |
|
3132 | + foreach ($fields as $field) { |
|
3133 | + // If it was successful, we'd like to know the new value. |
|
3134 | + // If it failed, we'd also like to know the new value. |
|
3135 | + $new_value = $this->_model->get_var( |
|
3136 | + $this->_model->alter_query_params_to_restrict_by_ID( |
|
3137 | + $this->_model->get_index_primary_key_string( |
|
3138 | + $this->model_field_array() |
|
3139 | + ), |
|
3140 | + array( |
|
3141 | + 'default_where_conditions' => 'minimum', |
|
3142 | + ) |
|
3143 | + ), |
|
3144 | + $field->get_name() |
|
3145 | + ); |
|
3146 | + $this->set_from_db( |
|
3147 | + $field->get_name(), |
|
3148 | + $new_value |
|
3149 | + ); |
|
3150 | + } |
|
3151 | + return (bool) $result; |
|
3152 | + } |
|
3153 | + |
|
3154 | + |
|
3155 | + /** |
|
3156 | + * Nudges $field_name's value by $quantity, without any conditionals (in comparison to bumpConditionally()). |
|
3157 | + * Does not allow negative values, however. |
|
3158 | + * |
|
3159 | + * @since 4.9.80.p |
|
3160 | + * @param array $fields_n_quantities keys are the field names, and values are the amount by which to bump them |
|
3161 | + * (positive or negative). One important gotcha: all these values must be |
|
3162 | + * on the same table (eg don't pass in one field for the posts table and |
|
3163 | + * another for the event meta table.) |
|
3164 | + * @return bool |
|
3165 | + * @throws EE_Error |
|
3166 | + * @throws InvalidArgumentException |
|
3167 | + * @throws InvalidDataTypeException |
|
3168 | + * @throws InvalidInterfaceException |
|
3169 | + * @throws ReflectionException |
|
3170 | + */ |
|
3171 | + public function adjustNumericFieldsInDb(array $fields_n_quantities) |
|
3172 | + { |
|
3173 | + global $wpdb; |
|
3174 | + if (empty($fields_n_quantities)) { |
|
3175 | + // No fields to update? Well sure, we updated them to that value just fine. |
|
3176 | + return true; |
|
3177 | + } |
|
3178 | + $fields = []; |
|
3179 | + $set_sql_statements = []; |
|
3180 | + foreach ($fields_n_quantities as $field_name => $quantity) { |
|
3181 | + $field = $this->_model->field_settings_for($field_name, true); |
|
3182 | + $fields[] = $field; |
|
3183 | + $column_name = $field->get_table_column(); |
|
3184 | + |
|
3185 | + $abs_qty = absint($quantity); |
|
3186 | + if ($quantity > 0) { |
|
3187 | + // don't let the value be negative as often these fields are unsigned |
|
3188 | + $set_sql_statements[] = $wpdb->prepare( |
|
3189 | + "`{$column_name}` = `{$column_name}` + %d", |
|
3190 | + $abs_qty |
|
3191 | + ); |
|
3192 | + } else { |
|
3193 | + $set_sql_statements[] = $wpdb->prepare( |
|
3194 | + "`{$column_name}` = CASE |
|
3195 | 3195 | WHEN (`{$column_name}` >= %d) |
3196 | 3196 | THEN `{$column_name}` - %d |
3197 | 3197 | ELSE 0 |
3198 | 3198 | END", |
3199 | - $abs_qty, |
|
3200 | - $abs_qty |
|
3201 | - ); |
|
3202 | - } |
|
3203 | - } |
|
3204 | - return $this->updateFieldsInDB( |
|
3205 | - $fields, |
|
3206 | - implode(', ', $set_sql_statements) |
|
3207 | - ); |
|
3208 | - } |
|
3209 | - |
|
3210 | - |
|
3211 | - /** |
|
3212 | - * Increases the value of the field $field_name_to_bump by $quantity, but only if the values of |
|
3213 | - * $field_name_to_bump plus $field_name_affecting_total and $quantity won't exceed $limit_field_name's value. |
|
3214 | - * For example, this is useful when bumping the value of TKT_reserved, TKT_sold, DTT_reserved or DTT_sold. |
|
3215 | - * Returns true if the value was successfully bumped, and updates the value on this model object. |
|
3216 | - * Otherwise returns false. |
|
3217 | - * |
|
3218 | - * @since 4.9.80.p |
|
3219 | - * @param string $field_name_to_bump |
|
3220 | - * @param string $field_name_affecting_total |
|
3221 | - * @param string $limit_field_name |
|
3222 | - * @param int $quantity |
|
3223 | - * @return bool |
|
3224 | - * @throws EE_Error |
|
3225 | - * @throws InvalidArgumentException |
|
3226 | - * @throws InvalidDataTypeException |
|
3227 | - * @throws InvalidInterfaceException |
|
3228 | - * @throws ReflectionException |
|
3229 | - */ |
|
3230 | - public function incrementFieldConditionallyInDb($field_name_to_bump, $field_name_affecting_total, $limit_field_name, $quantity) |
|
3231 | - { |
|
3232 | - global $wpdb; |
|
3233 | - $field = $this->_model->field_settings_for($field_name_to_bump, true); |
|
3234 | - $column_name = $field->get_table_column(); |
|
3235 | - |
|
3236 | - $field_affecting_total = $this->_model->field_settings_for($field_name_affecting_total, true); |
|
3237 | - $column_affecting_total = $field_affecting_total->get_table_column(); |
|
3238 | - |
|
3239 | - $limiting_field = $this->_model->field_settings_for($limit_field_name, true); |
|
3240 | - $limiting_column = $limiting_field->get_table_column(); |
|
3241 | - return $this->updateFieldsInDB( |
|
3242 | - [$field], |
|
3243 | - $wpdb->prepare( |
|
3244 | - "`{$column_name}` = |
|
3199 | + $abs_qty, |
|
3200 | + $abs_qty |
|
3201 | + ); |
|
3202 | + } |
|
3203 | + } |
|
3204 | + return $this->updateFieldsInDB( |
|
3205 | + $fields, |
|
3206 | + implode(', ', $set_sql_statements) |
|
3207 | + ); |
|
3208 | + } |
|
3209 | + |
|
3210 | + |
|
3211 | + /** |
|
3212 | + * Increases the value of the field $field_name_to_bump by $quantity, but only if the values of |
|
3213 | + * $field_name_to_bump plus $field_name_affecting_total and $quantity won't exceed $limit_field_name's value. |
|
3214 | + * For example, this is useful when bumping the value of TKT_reserved, TKT_sold, DTT_reserved or DTT_sold. |
|
3215 | + * Returns true if the value was successfully bumped, and updates the value on this model object. |
|
3216 | + * Otherwise returns false. |
|
3217 | + * |
|
3218 | + * @since 4.9.80.p |
|
3219 | + * @param string $field_name_to_bump |
|
3220 | + * @param string $field_name_affecting_total |
|
3221 | + * @param string $limit_field_name |
|
3222 | + * @param int $quantity |
|
3223 | + * @return bool |
|
3224 | + * @throws EE_Error |
|
3225 | + * @throws InvalidArgumentException |
|
3226 | + * @throws InvalidDataTypeException |
|
3227 | + * @throws InvalidInterfaceException |
|
3228 | + * @throws ReflectionException |
|
3229 | + */ |
|
3230 | + public function incrementFieldConditionallyInDb($field_name_to_bump, $field_name_affecting_total, $limit_field_name, $quantity) |
|
3231 | + { |
|
3232 | + global $wpdb; |
|
3233 | + $field = $this->_model->field_settings_for($field_name_to_bump, true); |
|
3234 | + $column_name = $field->get_table_column(); |
|
3235 | + |
|
3236 | + $field_affecting_total = $this->_model->field_settings_for($field_name_affecting_total, true); |
|
3237 | + $column_affecting_total = $field_affecting_total->get_table_column(); |
|
3238 | + |
|
3239 | + $limiting_field = $this->_model->field_settings_for($limit_field_name, true); |
|
3240 | + $limiting_column = $limiting_field->get_table_column(); |
|
3241 | + return $this->updateFieldsInDB( |
|
3242 | + [$field], |
|
3243 | + $wpdb->prepare( |
|
3244 | + "`{$column_name}` = |
|
3245 | 3245 | CASE |
3246 | 3246 | WHEN ((`{$column_name}` + `{$column_affecting_total}` + %d) <= `{$limiting_column}`) OR `{$limiting_column}` = %d |
3247 | 3247 | THEN `{$column_name}` + %d |
3248 | 3248 | ELSE `{$column_name}` |
3249 | 3249 | END", |
3250 | - $quantity, |
|
3251 | - EE_INF_IN_DB, |
|
3252 | - $quantity |
|
3253 | - ) |
|
3254 | - ); |
|
3255 | - } |
|
3256 | - |
|
3257 | - |
|
3258 | - /** |
|
3259 | - * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
3260 | - * (probably a bad assumption they have made, oh well) |
|
3261 | - * |
|
3262 | - * @return string |
|
3263 | - */ |
|
3264 | - public function __toString() |
|
3265 | - { |
|
3266 | - try { |
|
3267 | - return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
3268 | - } catch (Exception $e) { |
|
3269 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
3270 | - return ''; |
|
3271 | - } |
|
3272 | - } |
|
3273 | - |
|
3274 | - |
|
3275 | - /** |
|
3276 | - * Clear related model objects if they're already in the DB, because otherwise when we |
|
3277 | - * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
3278 | - * This means if we have made changes to those related model objects, and want to unserialize |
|
3279 | - * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
3280 | - * Instead, those related model objects should be directly serialized and stored. |
|
3281 | - * Eg, the following won't work: |
|
3282 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
3283 | - * $att = $reg->attendee(); |
|
3284 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
3285 | - * update_option( 'my_option', serialize( $reg ) ); |
|
3286 | - * //END REQUEST |
|
3287 | - * //START NEXT REQUEST |
|
3288 | - * $reg = get_option( 'my_option' ); |
|
3289 | - * $reg->attendee()->save(); |
|
3290 | - * And would need to be replace with: |
|
3291 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
3292 | - * $att = $reg->attendee(); |
|
3293 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
3294 | - * update_option( 'my_option', serialize( $reg ) ); |
|
3295 | - * //END REQUEST |
|
3296 | - * //START NEXT REQUEST |
|
3297 | - * $att = get_option( 'my_option' ); |
|
3298 | - * $att->save(); |
|
3299 | - * |
|
3300 | - * @return array |
|
3301 | - * @throws ReflectionException |
|
3302 | - * @throws InvalidArgumentException |
|
3303 | - * @throws InvalidInterfaceException |
|
3304 | - * @throws InvalidDataTypeException |
|
3305 | - * @throws EE_Error |
|
3306 | - */ |
|
3307 | - public function __sleep() |
|
3308 | - { |
|
3309 | - foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
3310 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
3311 | - $classname = 'EE_' . $this->_model->get_this_model_name(); |
|
3312 | - if ($this->get_one_from_cache($relation_name) instanceof $classname |
|
3313 | - && $this->get_one_from_cache($relation_name)->ID() |
|
3314 | - ) { |
|
3315 | - $this->clear_cache( |
|
3316 | - $relation_name, |
|
3317 | - $this->get_one_from_cache($relation_name)->ID() |
|
3318 | - ); |
|
3319 | - } |
|
3320 | - } |
|
3321 | - } |
|
3322 | - $this->_props_n_values_provided_in_constructor = array(); |
|
3323 | - $properties_to_serialize = get_object_vars($this); |
|
3324 | - // don't serialize the model. It's big and that risks recursion |
|
3325 | - unset($properties_to_serialize['_model']); |
|
3326 | - return array_keys($properties_to_serialize); |
|
3327 | - } |
|
3328 | - |
|
3329 | - |
|
3330 | - /** |
|
3331 | - * restore _props_n_values_provided_in_constructor |
|
3332 | - * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
3333 | - * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
3334 | - * At best, you would only be able to detect if state change has occurred during THIS request. |
|
3335 | - */ |
|
3336 | - public function __wakeup() |
|
3337 | - { |
|
3338 | - $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
3339 | - } |
|
3340 | - |
|
3341 | - |
|
3342 | - /** |
|
3343 | - * Usage of this magic method is to ensure any internally cached references to object instances that must remain |
|
3344 | - * distinct with the clone host instance are also cloned. |
|
3345 | - */ |
|
3346 | - public function __clone() |
|
3347 | - { |
|
3348 | - // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
|
3349 | - foreach ($this->_fields as $field => $value) { |
|
3350 | - if ($value instanceof DateTime) { |
|
3351 | - $this->_fields[ $field ] = clone $value; |
|
3352 | - } |
|
3353 | - } |
|
3354 | - } |
|
3250 | + $quantity, |
|
3251 | + EE_INF_IN_DB, |
|
3252 | + $quantity |
|
3253 | + ) |
|
3254 | + ); |
|
3255 | + } |
|
3256 | + |
|
3257 | + |
|
3258 | + /** |
|
3259 | + * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
3260 | + * (probably a bad assumption they have made, oh well) |
|
3261 | + * |
|
3262 | + * @return string |
|
3263 | + */ |
|
3264 | + public function __toString() |
|
3265 | + { |
|
3266 | + try { |
|
3267 | + return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
3268 | + } catch (Exception $e) { |
|
3269 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
3270 | + return ''; |
|
3271 | + } |
|
3272 | + } |
|
3273 | + |
|
3274 | + |
|
3275 | + /** |
|
3276 | + * Clear related model objects if they're already in the DB, because otherwise when we |
|
3277 | + * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
3278 | + * This means if we have made changes to those related model objects, and want to unserialize |
|
3279 | + * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
3280 | + * Instead, those related model objects should be directly serialized and stored. |
|
3281 | + * Eg, the following won't work: |
|
3282 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
3283 | + * $att = $reg->attendee(); |
|
3284 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
3285 | + * update_option( 'my_option', serialize( $reg ) ); |
|
3286 | + * //END REQUEST |
|
3287 | + * //START NEXT REQUEST |
|
3288 | + * $reg = get_option( 'my_option' ); |
|
3289 | + * $reg->attendee()->save(); |
|
3290 | + * And would need to be replace with: |
|
3291 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
3292 | + * $att = $reg->attendee(); |
|
3293 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
3294 | + * update_option( 'my_option', serialize( $reg ) ); |
|
3295 | + * //END REQUEST |
|
3296 | + * //START NEXT REQUEST |
|
3297 | + * $att = get_option( 'my_option' ); |
|
3298 | + * $att->save(); |
|
3299 | + * |
|
3300 | + * @return array |
|
3301 | + * @throws ReflectionException |
|
3302 | + * @throws InvalidArgumentException |
|
3303 | + * @throws InvalidInterfaceException |
|
3304 | + * @throws InvalidDataTypeException |
|
3305 | + * @throws EE_Error |
|
3306 | + */ |
|
3307 | + public function __sleep() |
|
3308 | + { |
|
3309 | + foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
3310 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
3311 | + $classname = 'EE_' . $this->_model->get_this_model_name(); |
|
3312 | + if ($this->get_one_from_cache($relation_name) instanceof $classname |
|
3313 | + && $this->get_one_from_cache($relation_name)->ID() |
|
3314 | + ) { |
|
3315 | + $this->clear_cache( |
|
3316 | + $relation_name, |
|
3317 | + $this->get_one_from_cache($relation_name)->ID() |
|
3318 | + ); |
|
3319 | + } |
|
3320 | + } |
|
3321 | + } |
|
3322 | + $this->_props_n_values_provided_in_constructor = array(); |
|
3323 | + $properties_to_serialize = get_object_vars($this); |
|
3324 | + // don't serialize the model. It's big and that risks recursion |
|
3325 | + unset($properties_to_serialize['_model']); |
|
3326 | + return array_keys($properties_to_serialize); |
|
3327 | + } |
|
3328 | + |
|
3329 | + |
|
3330 | + /** |
|
3331 | + * restore _props_n_values_provided_in_constructor |
|
3332 | + * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
3333 | + * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
3334 | + * At best, you would only be able to detect if state change has occurred during THIS request. |
|
3335 | + */ |
|
3336 | + public function __wakeup() |
|
3337 | + { |
|
3338 | + $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
3339 | + } |
|
3340 | + |
|
3341 | + |
|
3342 | + /** |
|
3343 | + * Usage of this magic method is to ensure any internally cached references to object instances that must remain |
|
3344 | + * distinct with the clone host instance are also cloned. |
|
3345 | + */ |
|
3346 | + public function __clone() |
|
3347 | + { |
|
3348 | + // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
|
3349 | + foreach ($this->_fields as $field => $value) { |
|
3350 | + if ($value instanceof DateTime) { |
|
3351 | + $this->_fields[ $field ] = clone $value; |
|
3352 | + } |
|
3353 | + } |
|
3354 | + } |
|
3355 | 3355 | } |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
147 | 147 | // verify client code has not passed any invalid field names |
148 | 148 | foreach ($fieldValues as $field_name => $field_value) { |
149 | - if (! isset($model_fields[ $field_name ])) { |
|
149 | + if ( ! isset($model_fields[$field_name])) { |
|
150 | 150 | throw new EE_Error( |
151 | 151 | sprintf( |
152 | 152 | esc_html__( |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | } |
162 | 162 | } |
163 | 163 | $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
164 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
164 | + if ( ! empty($date_formats) && is_array($date_formats)) { |
|
165 | 165 | list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
166 | 166 | } else { |
167 | 167 | // set default formats for date and time |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | foreach ($model_fields as $fieldName => $field) { |
175 | 175 | $this->set_from_db( |
176 | 176 | $fieldName, |
177 | - isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null |
|
177 | + isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null |
|
178 | 178 | ); |
179 | 179 | } |
180 | 180 | } else { |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | foreach ($model_fields as $fieldName => $field) { |
184 | 184 | $this->set( |
185 | 185 | $fieldName, |
186 | - isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null, |
|
186 | + isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null, |
|
187 | 187 | true |
188 | 188 | ); |
189 | 189 | } |
@@ -191,15 +191,15 @@ discard block |
||
191 | 191 | // remember what values were passed to this constructor |
192 | 192 | $this->_props_n_values_provided_in_constructor = $fieldValues; |
193 | 193 | // remember in entity mapper |
194 | - if (! $bydb && $this->_model->has_primary_key_field() && $this->ID()) { |
|
194 | + if ( ! $bydb && $this->_model->has_primary_key_field() && $this->ID()) { |
|
195 | 195 | $this->_model->add_to_entity_map($this); |
196 | 196 | } |
197 | 197 | // setup all the relations |
198 | 198 | foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
199 | 199 | if ($relation_obj instanceof EE_Belongs_To_Relation) { |
200 | - $this->_model_relations[ $relation_name ] = null; |
|
200 | + $this->_model_relations[$relation_name] = null; |
|
201 | 201 | } else { |
202 | - $this->_model_relations[ $relation_name ] = array(); |
|
202 | + $this->_model_relations[$relation_name] = array(); |
|
203 | 203 | } |
204 | 204 | } |
205 | 205 | /** |
@@ -251,10 +251,10 @@ discard block |
||
251 | 251 | public function get_original($field_name) |
252 | 252 | { |
253 | 253 | if ( |
254 | - isset($this->_props_n_values_provided_in_constructor[ $field_name ]) |
|
254 | + isset($this->_props_n_values_provided_in_constructor[$field_name]) |
|
255 | 255 | && $field_settings = $this->_model->field_settings_for($field_name) |
256 | 256 | ) { |
257 | - return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]); |
|
257 | + return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[$field_name]); |
|
258 | 258 | } |
259 | 259 | return null; |
260 | 260 | } |
@@ -291,7 +291,7 @@ discard block |
||
291 | 291 | // then don't do anything |
292 | 292 | if ( |
293 | 293 | ! $use_default |
294 | - && $this->_fields[ $field_name ] === $field_value |
|
294 | + && $this->_fields[$field_name] === $field_value |
|
295 | 295 | && $this->ID() |
296 | 296 | ) { |
297 | 297 | return; |
@@ -308,7 +308,7 @@ discard block |
||
308 | 308 | $holder_of_value = $field_obj->prepare_for_set($field_value); |
309 | 309 | // should the value be null? |
310 | 310 | if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
311 | - $this->_fields[ $field_name ] = $field_obj->get_default_value(); |
|
311 | + $this->_fields[$field_name] = $field_obj->get_default_value(); |
|
312 | 312 | /** |
313 | 313 | * To save having to refactor all the models, if a default value is used for a |
314 | 314 | * EE_Datetime_Field, and that value is not null nor is it a DateTime |
@@ -319,15 +319,15 @@ discard block |
||
319 | 319 | */ |
320 | 320 | if ( |
321 | 321 | $field_obj instanceof EE_Datetime_Field |
322 | - && $this->_fields[ $field_name ] !== null |
|
323 | - && ! $this->_fields[ $field_name ] instanceof DateTime |
|
322 | + && $this->_fields[$field_name] !== null |
|
323 | + && ! $this->_fields[$field_name] instanceof DateTime |
|
324 | 324 | ) { |
325 | - empty($this->_fields[ $field_name ]) |
|
325 | + empty($this->_fields[$field_name]) |
|
326 | 326 | ? $this->set($field_name, time()) |
327 | - : $this->set($field_name, $this->_fields[ $field_name ]); |
|
327 | + : $this->set($field_name, $this->_fields[$field_name]); |
|
328 | 328 | } |
329 | 329 | } else { |
330 | - $this->_fields[ $field_name ] = $holder_of_value; |
|
330 | + $this->_fields[$field_name] = $holder_of_value; |
|
331 | 331 | } |
332 | 332 | // if we're not in the constructor... |
333 | 333 | // now check if what we set was a primary key |
@@ -390,8 +390,8 @@ discard block |
||
390 | 390 | */ |
391 | 391 | public function getCustomSelect($alias) |
392 | 392 | { |
393 | - return isset($this->custom_selection_results[ $alias ]) |
|
394 | - ? $this->custom_selection_results[ $alias ] |
|
393 | + return isset($this->custom_selection_results[$alias]) |
|
394 | + ? $this->custom_selection_results[$alias] |
|
395 | 395 | : null; |
396 | 396 | } |
397 | 397 | |
@@ -478,8 +478,8 @@ discard block |
||
478 | 478 | foreach ($model_fields as $field_name => $field_obj) { |
479 | 479 | if ($field_obj instanceof EE_Datetime_Field) { |
480 | 480 | $field_obj->set_timezone($this->_timezone); |
481 | - if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) { |
|
482 | - EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], new DateTimeZone($this->_timezone)); |
|
481 | + if (isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime) { |
|
482 | + EEH_DTT_Helper::setTimezone($this->_fields[$field_name], new DateTimeZone($this->_timezone)); |
|
483 | 483 | } |
484 | 484 | } |
485 | 485 | } |
@@ -537,7 +537,7 @@ discard block |
||
537 | 537 | */ |
538 | 538 | public function get_format($full = true) |
539 | 539 | { |
540 | - return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
540 | + return $full ? $this->_dt_frmt.' '.$this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
541 | 541 | } |
542 | 542 | |
543 | 543 | |
@@ -563,11 +563,11 @@ discard block |
||
563 | 563 | public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
564 | 564 | { |
565 | 565 | // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
566 | - if (! $object_to_cache instanceof EE_Base_Class) { |
|
566 | + if ( ! $object_to_cache instanceof EE_Base_Class) { |
|
567 | 567 | return false; |
568 | 568 | } |
569 | 569 | // also get "how" the object is related, or throw an error |
570 | - if (! $relationship_to_model = $this->_model->related_settings_for($relationName)) { |
|
570 | + if ( ! $relationship_to_model = $this->_model->related_settings_for($relationName)) { |
|
571 | 571 | throw new EE_Error( |
572 | 572 | sprintf( |
573 | 573 | esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
@@ -581,38 +581,38 @@ discard block |
||
581 | 581 | // if it's a "belongs to" relationship, then there's only one related model object |
582 | 582 | // eg, if this is a registration, there's only 1 attendee for it |
583 | 583 | // so for these model objects just set it to be cached |
584 | - $this->_model_relations[ $relationName ] = $object_to_cache; |
|
584 | + $this->_model_relations[$relationName] = $object_to_cache; |
|
585 | 585 | $return = true; |
586 | 586 | } else { |
587 | 587 | // otherwise, this is the "many" side of a one to many relationship, |
588 | 588 | // so we'll add the object to the array of related objects for that type. |
589 | 589 | // eg: if this is an event, there are many registrations for that event, |
590 | 590 | // so we cache the registrations in an array |
591 | - if (! is_array($this->_model_relations[ $relationName ])) { |
|
591 | + if ( ! is_array($this->_model_relations[$relationName])) { |
|
592 | 592 | // if for some reason, the cached item is a model object, |
593 | 593 | // then stick that in the array, otherwise start with an empty array |
594 | - $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ] |
|
594 | + $this->_model_relations[$relationName] = $this->_model_relations[$relationName] |
|
595 | 595 | instanceof |
596 | 596 | EE_Base_Class |
597 | - ? array($this->_model_relations[ $relationName ]) : array(); |
|
597 | + ? array($this->_model_relations[$relationName]) : array(); |
|
598 | 598 | } |
599 | 599 | // first check for a cache_id which is normally empty |
600 | - if (! empty($cache_id)) { |
|
600 | + if ( ! empty($cache_id)) { |
|
601 | 601 | // if the cache_id exists, then it means we are purposely trying to cache this |
602 | 602 | // with a known key that can then be used to retrieve the object later on |
603 | - $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache; |
|
603 | + $this->_model_relations[$relationName][$cache_id] = $object_to_cache; |
|
604 | 604 | $return = $cache_id; |
605 | 605 | } elseif ($object_to_cache->ID()) { |
606 | 606 | // OR the cached object originally came from the db, so let's just use it's PK for an ID |
607 | - $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache; |
|
607 | + $this->_model_relations[$relationName][$object_to_cache->ID()] = $object_to_cache; |
|
608 | 608 | $return = $object_to_cache->ID(); |
609 | 609 | } else { |
610 | 610 | // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
611 | - $this->_model_relations[ $relationName ][] = $object_to_cache; |
|
611 | + $this->_model_relations[$relationName][] = $object_to_cache; |
|
612 | 612 | // move the internal pointer to the end of the array |
613 | - end($this->_model_relations[ $relationName ]); |
|
613 | + end($this->_model_relations[$relationName]); |
|
614 | 614 | // and grab the key so that we can return it |
615 | - $return = key($this->_model_relations[ $relationName ]); |
|
615 | + $return = key($this->_model_relations[$relationName]); |
|
616 | 616 | } |
617 | 617 | } |
618 | 618 | return $return; |
@@ -638,7 +638,7 @@ discard block |
||
638 | 638 | // first make sure this property exists |
639 | 639 | $this->_model->field_settings_for($fieldname); |
640 | 640 | $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
641 | - $this->_cached_properties[ $fieldname ][ $cache_type ] = $value; |
|
641 | + $this->_cached_properties[$fieldname][$cache_type] = $value; |
|
642 | 642 | } |
643 | 643 | |
644 | 644 | |
@@ -666,9 +666,9 @@ discard block |
||
666 | 666 | // verify the field exists |
667 | 667 | $this->_model->field_settings_for($fieldname); |
668 | 668 | $cache_type = $pretty ? 'pretty' : 'standard'; |
669 | - $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
670 | - if (isset($this->_cached_properties[ $fieldname ][ $cache_type ])) { |
|
671 | - return $this->_cached_properties[ $fieldname ][ $cache_type ]; |
|
669 | + $cache_type .= ! empty($extra_cache_ref) ? '_'.$extra_cache_ref : ''; |
|
670 | + if (isset($this->_cached_properties[$fieldname][$cache_type])) { |
|
671 | + return $this->_cached_properties[$fieldname][$cache_type]; |
|
672 | 672 | } |
673 | 673 | $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
674 | 674 | $this->_set_cached_property($fieldname, $value, $cache_type); |
@@ -696,12 +696,12 @@ discard block |
||
696 | 696 | if ($field_obj instanceof EE_Datetime_Field) { |
697 | 697 | $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
698 | 698 | } |
699 | - if (! isset($this->_fields[ $fieldname ])) { |
|
700 | - $this->_fields[ $fieldname ] = null; |
|
699 | + if ( ! isset($this->_fields[$fieldname])) { |
|
700 | + $this->_fields[$fieldname] = null; |
|
701 | 701 | } |
702 | 702 | $value = $pretty |
703 | - ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $fieldname ], $extra_cache_ref) |
|
704 | - : $field_obj->prepare_for_get($this->_fields[ $fieldname ]); |
|
703 | + ? $field_obj->prepare_for_pretty_echoing($this->_fields[$fieldname], $extra_cache_ref) |
|
704 | + : $field_obj->prepare_for_get($this->_fields[$fieldname]); |
|
705 | 705 | return $value; |
706 | 706 | } |
707 | 707 | |
@@ -759,8 +759,8 @@ discard block |
||
759 | 759 | */ |
760 | 760 | protected function _clear_cached_property($property_name) |
761 | 761 | { |
762 | - if (isset($this->_cached_properties[ $property_name ])) { |
|
763 | - unset($this->_cached_properties[ $property_name ]); |
|
762 | + if (isset($this->_cached_properties[$property_name])) { |
|
763 | + unset($this->_cached_properties[$property_name]); |
|
764 | 764 | } |
765 | 765 | } |
766 | 766 | |
@@ -812,7 +812,7 @@ discard block |
||
812 | 812 | { |
813 | 813 | $relationship_to_model = $this->_model->related_settings_for($relationName); |
814 | 814 | $index_in_cache = ''; |
815 | - if (! $relationship_to_model) { |
|
815 | + if ( ! $relationship_to_model) { |
|
816 | 816 | throw new EE_Error( |
817 | 817 | sprintf( |
818 | 818 | esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'), |
@@ -823,10 +823,10 @@ discard block |
||
823 | 823 | } |
824 | 824 | if ($clear_all) { |
825 | 825 | $obj_removed = true; |
826 | - $this->_model_relations[ $relationName ] = null; |
|
826 | + $this->_model_relations[$relationName] = null; |
|
827 | 827 | } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
828 | - $obj_removed = $this->_model_relations[ $relationName ]; |
|
829 | - $this->_model_relations[ $relationName ] = null; |
|
828 | + $obj_removed = $this->_model_relations[$relationName]; |
|
829 | + $this->_model_relations[$relationName] = null; |
|
830 | 830 | } else { |
831 | 831 | if ( |
832 | 832 | $object_to_remove_or_index_into_array instanceof EE_Base_Class |
@@ -834,12 +834,12 @@ discard block |
||
834 | 834 | ) { |
835 | 835 | $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
836 | 836 | if ( |
837 | - is_array($this->_model_relations[ $relationName ]) |
|
838 | - && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ]) |
|
837 | + is_array($this->_model_relations[$relationName]) |
|
838 | + && ! isset($this->_model_relations[$relationName][$index_in_cache]) |
|
839 | 839 | ) { |
840 | 840 | $index_found_at = null; |
841 | 841 | // find this object in the array even though it has a different key |
842 | - foreach ($this->_model_relations[ $relationName ] as $index => $obj) { |
|
842 | + foreach ($this->_model_relations[$relationName] as $index => $obj) { |
|
843 | 843 | /** @noinspection TypeUnsafeComparisonInspection */ |
844 | 844 | if ( |
845 | 845 | $obj instanceof EE_Base_Class |
@@ -873,9 +873,9 @@ discard block |
||
873 | 873 | } |
874 | 874 | // supposedly we've found it. But it could just be that the client code |
875 | 875 | // provided a bad index/object |
876 | - if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) { |
|
877 | - $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ]; |
|
878 | - unset($this->_model_relations[ $relationName ][ $index_in_cache ]); |
|
876 | + if (isset($this->_model_relations[$relationName][$index_in_cache])) { |
|
877 | + $obj_removed = $this->_model_relations[$relationName][$index_in_cache]; |
|
878 | + unset($this->_model_relations[$relationName][$index_in_cache]); |
|
879 | 879 | } else { |
880 | 880 | // that thing was never cached anyways. |
881 | 881 | $obj_removed = null; |
@@ -906,7 +906,7 @@ discard block |
||
906 | 906 | $current_cache_id = '' |
907 | 907 | ) { |
908 | 908 | // verify that incoming object is of the correct type |
909 | - $obj_class = 'EE_' . $relationName; |
|
909 | + $obj_class = 'EE_'.$relationName; |
|
910 | 910 | if ($newly_saved_object instanceof $obj_class) { |
911 | 911 | /* @type EE_Base_Class $newly_saved_object */ |
912 | 912 | // now get the type of relation |
@@ -914,18 +914,18 @@ discard block |
||
914 | 914 | // if this is a 1:1 relationship |
915 | 915 | if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
916 | 916 | // then just replace the cached object with the newly saved object |
917 | - $this->_model_relations[ $relationName ] = $newly_saved_object; |
|
917 | + $this->_model_relations[$relationName] = $newly_saved_object; |
|
918 | 918 | return true; |
919 | 919 | // or if it's some kind of sordid feral polyamorous relationship... |
920 | 920 | } |
921 | 921 | if ( |
922 | - is_array($this->_model_relations[ $relationName ]) |
|
923 | - && isset($this->_model_relations[ $relationName ][ $current_cache_id ]) |
|
922 | + is_array($this->_model_relations[$relationName]) |
|
923 | + && isset($this->_model_relations[$relationName][$current_cache_id]) |
|
924 | 924 | ) { |
925 | 925 | // then remove the current cached item |
926 | - unset($this->_model_relations[ $relationName ][ $current_cache_id ]); |
|
926 | + unset($this->_model_relations[$relationName][$current_cache_id]); |
|
927 | 927 | // and cache the newly saved object using it's new ID |
928 | - $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object; |
|
928 | + $this->_model_relations[$relationName][$newly_saved_object->ID()] = $newly_saved_object; |
|
929 | 929 | return true; |
930 | 930 | } |
931 | 931 | } |
@@ -942,8 +942,8 @@ discard block |
||
942 | 942 | */ |
943 | 943 | public function get_one_from_cache($relationName) |
944 | 944 | { |
945 | - $cached_array_or_object = isset($this->_model_relations[ $relationName ]) |
|
946 | - ? $this->_model_relations[ $relationName ] |
|
945 | + $cached_array_or_object = isset($this->_model_relations[$relationName]) |
|
946 | + ? $this->_model_relations[$relationName] |
|
947 | 947 | : null; |
948 | 948 | if (is_array($cached_array_or_object)) { |
949 | 949 | return array_shift($cached_array_or_object); |
@@ -966,7 +966,7 @@ discard block |
||
966 | 966 | */ |
967 | 967 | public function get_all_from_cache($relationName) |
968 | 968 | { |
969 | - $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : array(); |
|
969 | + $objects = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] : array(); |
|
970 | 970 | // if the result is not an array, but exists, make it an array |
971 | 971 | $objects = is_array($objects) ? $objects : array($objects); |
972 | 972 | // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
@@ -1146,7 +1146,7 @@ discard block |
||
1146 | 1146 | } else { |
1147 | 1147 | $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
1148 | 1148 | } |
1149 | - $this->_fields[ $field_name ] = $field_value; |
|
1149 | + $this->_fields[$field_name] = $field_value; |
|
1150 | 1150 | $this->_clear_cached_property($field_name); |
1151 | 1151 | } |
1152 | 1152 | } |
@@ -1186,9 +1186,9 @@ discard block |
||
1186 | 1186 | public function get_raw($field_name) |
1187 | 1187 | { |
1188 | 1188 | $field_settings = $this->_model->field_settings_for($field_name); |
1189 | - return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime |
|
1190 | - ? $this->_fields[ $field_name ]->format('U') |
|
1191 | - : $this->_fields[ $field_name ]; |
|
1189 | + return $field_settings instanceof EE_Datetime_Field && $this->_fields[$field_name] instanceof DateTime |
|
1190 | + ? $this->_fields[$field_name]->format('U') |
|
1191 | + : $this->_fields[$field_name]; |
|
1192 | 1192 | } |
1193 | 1193 | |
1194 | 1194 | |
@@ -1210,7 +1210,7 @@ discard block |
||
1210 | 1210 | public function get_DateTime_object($field_name) |
1211 | 1211 | { |
1212 | 1212 | $field_settings = $this->_model->field_settings_for($field_name); |
1213 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
1213 | + if ( ! $field_settings instanceof EE_Datetime_Field) { |
|
1214 | 1214 | EE_Error::add_error( |
1215 | 1215 | sprintf( |
1216 | 1216 | esc_html__( |
@@ -1225,8 +1225,8 @@ discard block |
||
1225 | 1225 | ); |
1226 | 1226 | return false; |
1227 | 1227 | } |
1228 | - return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime |
|
1229 | - ? clone $this->_fields[ $field_name ] |
|
1228 | + return isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime |
|
1229 | + ? clone $this->_fields[$field_name] |
|
1230 | 1230 | : null; |
1231 | 1231 | } |
1232 | 1232 | |
@@ -1468,7 +1468,7 @@ discard block |
||
1468 | 1468 | */ |
1469 | 1469 | public function get_i18n_datetime($field_name, $format = '') |
1470 | 1470 | { |
1471 | - $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
1471 | + $format = empty($format) ? $this->_dt_frmt.' '.$this->_tm_frmt : $format; |
|
1472 | 1472 | return date_i18n( |
1473 | 1473 | $format, |
1474 | 1474 | EEH_DTT_Helper::get_timestamp_with_offset( |
@@ -1580,21 +1580,21 @@ discard block |
||
1580 | 1580 | $field->set_time_format($this->_tm_frmt); |
1581 | 1581 | switch ($what) { |
1582 | 1582 | case 'T': |
1583 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_time( |
|
1583 | + $this->_fields[$fieldname] = $field->prepare_for_set_with_new_time( |
|
1584 | 1584 | $datetime_value, |
1585 | - $this->_fields[ $fieldname ] |
|
1585 | + $this->_fields[$fieldname] |
|
1586 | 1586 | ); |
1587 | 1587 | $this->_has_changes = true; |
1588 | 1588 | break; |
1589 | 1589 | case 'D': |
1590 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date( |
|
1590 | + $this->_fields[$fieldname] = $field->prepare_for_set_with_new_date( |
|
1591 | 1591 | $datetime_value, |
1592 | - $this->_fields[ $fieldname ] |
|
1592 | + $this->_fields[$fieldname] |
|
1593 | 1593 | ); |
1594 | 1594 | $this->_has_changes = true; |
1595 | 1595 | break; |
1596 | 1596 | case 'B': |
1597 | - $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value); |
|
1597 | + $this->_fields[$fieldname] = $field->prepare_for_set($datetime_value); |
|
1598 | 1598 | $this->_has_changes = true; |
1599 | 1599 | break; |
1600 | 1600 | } |
@@ -1637,7 +1637,7 @@ discard block |
||
1637 | 1637 | $this->set_timezone($timezone); |
1638 | 1638 | $fn = (array) $field_name; |
1639 | 1639 | $args = array_merge($fn, (array) $args); |
1640 | - if (! method_exists($this, $callback)) { |
|
1640 | + if ( ! method_exists($this, $callback)) { |
|
1641 | 1641 | throw new EE_Error( |
1642 | 1642 | sprintf( |
1643 | 1643 | esc_html__( |
@@ -1649,7 +1649,7 @@ discard block |
||
1649 | 1649 | ); |
1650 | 1650 | } |
1651 | 1651 | $args = (array) $args; |
1652 | - $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
1652 | + $return = $prepend.call_user_func_array(array($this, $callback), $args).$append; |
|
1653 | 1653 | $this->set_timezone($original_timezone); |
1654 | 1654 | return $return; |
1655 | 1655 | } |
@@ -1762,8 +1762,8 @@ discard block |
||
1762 | 1762 | public function refresh_cache_of_related_objects() |
1763 | 1763 | { |
1764 | 1764 | foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
1765 | - if (! empty($this->_model_relations[ $relation_name ])) { |
|
1766 | - $related_objects = $this->_model_relations[ $relation_name ]; |
|
1765 | + if ( ! empty($this->_model_relations[$relation_name])) { |
|
1766 | + $related_objects = $this->_model_relations[$relation_name]; |
|
1767 | 1767 | if ($relation_obj instanceof EE_Belongs_To_Relation) { |
1768 | 1768 | // this relation only stores a single model object, not an array |
1769 | 1769 | // but let's make it consistent |
@@ -1819,7 +1819,7 @@ discard block |
||
1819 | 1819 | $this->set($column, $value); |
1820 | 1820 | } |
1821 | 1821 | // no changes ? then don't do anything |
1822 | - if (! $this->_has_changes && $this->ID() && $this->_model->get_primary_key_field()->is_auto_increment()) { |
|
1822 | + if ( ! $this->_has_changes && $this->ID() && $this->_model->get_primary_key_field()->is_auto_increment()) { |
|
1823 | 1823 | return 0; |
1824 | 1824 | } |
1825 | 1825 | /** |
@@ -1829,7 +1829,7 @@ discard block |
||
1829 | 1829 | * @param EE_Base_Class $model_object the model object about to be saved. |
1830 | 1830 | */ |
1831 | 1831 | do_action('AHEE__EE_Base_Class__save__begin', $this); |
1832 | - if (! $this->allow_persist()) { |
|
1832 | + if ( ! $this->allow_persist()) { |
|
1833 | 1833 | return 0; |
1834 | 1834 | } |
1835 | 1835 | // now get current attribute values |
@@ -1844,10 +1844,10 @@ discard block |
||
1844 | 1844 | if ($this->_model->has_primary_key_field()) { |
1845 | 1845 | if ($this->_model->get_primary_key_field()->is_auto_increment()) { |
1846 | 1846 | // ok check if it's set, if so: update; if not, insert |
1847 | - if (! empty($save_cols_n_values[ $this->_model->primary_key_name() ])) { |
|
1847 | + if ( ! empty($save_cols_n_values[$this->_model->primary_key_name()])) { |
|
1848 | 1848 | $results = $this->_model->update_by_ID($save_cols_n_values, $this->ID()); |
1849 | 1849 | } else { |
1850 | - unset($save_cols_n_values[ $this->_model->primary_key_name() ]); |
|
1850 | + unset($save_cols_n_values[$this->_model->primary_key_name()]); |
|
1851 | 1851 | $results = $this->_model->insert($save_cols_n_values); |
1852 | 1852 | if ($results) { |
1853 | 1853 | // if successful, set the primary key |
@@ -1857,7 +1857,7 @@ discard block |
||
1857 | 1857 | // will get added to the mapper before we can add this one! |
1858 | 1858 | // but if we just avoid using the SET method, all that headache can be avoided |
1859 | 1859 | $pk_field_name = $this->_model->primary_key_name(); |
1860 | - $this->_fields[ $pk_field_name ] = $results; |
|
1860 | + $this->_fields[$pk_field_name] = $results; |
|
1861 | 1861 | $this->_clear_cached_property($pk_field_name); |
1862 | 1862 | $this->_model->add_to_entity_map($this); |
1863 | 1863 | $this->_update_cached_related_model_objs_fks(); |
@@ -1874,8 +1874,8 @@ discard block |
||
1874 | 1874 | 'event_espresso' |
1875 | 1875 | ), |
1876 | 1876 | get_class($this), |
1877 | - get_class($this->_model) . '::instance()->add_to_entity_map()', |
|
1878 | - get_class($this->_model) . '::instance()->get_one_by_ID()', |
|
1877 | + get_class($this->_model).'::instance()->add_to_entity_map()', |
|
1878 | + get_class($this->_model).'::instance()->get_one_by_ID()', |
|
1879 | 1879 | '<br />' |
1880 | 1880 | ) |
1881 | 1881 | ); |
@@ -1976,27 +1976,27 @@ discard block |
||
1976 | 1976 | public function save_new_cached_related_model_objs() |
1977 | 1977 | { |
1978 | 1978 | // make sure this has been saved |
1979 | - if (! $this->ID()) { |
|
1979 | + if ( ! $this->ID()) { |
|
1980 | 1980 | $id = $this->save(); |
1981 | 1981 | } else { |
1982 | 1982 | $id = $this->ID(); |
1983 | 1983 | } |
1984 | 1984 | // now save all the NEW cached model objects (ie they don't exist in the DB) |
1985 | 1985 | foreach ($this->_model->relation_settings() as $relationName => $relationObj) { |
1986 | - if ($this->_model_relations[ $relationName ]) { |
|
1986 | + if ($this->_model_relations[$relationName]) { |
|
1987 | 1987 | // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
1988 | 1988 | // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
1989 | 1989 | /* @var $related_model_obj EE_Base_Class */ |
1990 | 1990 | if ($relationObj instanceof EE_Belongs_To_Relation) { |
1991 | 1991 | // add a relation to that relation type (which saves the appropriate thing in the process) |
1992 | 1992 | // but ONLY if it DOES NOT exist in the DB |
1993 | - $related_model_obj = $this->_model_relations[ $relationName ]; |
|
1993 | + $related_model_obj = $this->_model_relations[$relationName]; |
|
1994 | 1994 | // if( ! $related_model_obj->ID()){ |
1995 | 1995 | $this->_add_relation_to($related_model_obj, $relationName); |
1996 | 1996 | $related_model_obj->save_new_cached_related_model_objs(); |
1997 | 1997 | // } |
1998 | 1998 | } else { |
1999 | - foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
1999 | + foreach ($this->_model_relations[$relationName] as $related_model_obj) { |
|
2000 | 2000 | // add a relation to that relation type (which saves the appropriate thing in the process) |
2001 | 2001 | // but ONLY if it DOES NOT exist in the DB |
2002 | 2002 | // if( ! $related_model_obj->ID()){ |
@@ -2023,7 +2023,7 @@ discard block |
||
2023 | 2023 | */ |
2024 | 2024 | public function get_model() |
2025 | 2025 | { |
2026 | - if (! $this->_model) { |
|
2026 | + if ( ! $this->_model) { |
|
2027 | 2027 | $modelName = self::_get_model_classname(get_class($this)); |
2028 | 2028 | $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
2029 | 2029 | } |
@@ -2047,9 +2047,9 @@ discard block |
||
2047 | 2047 | $primary_id_ref = self::_get_primary_key_name($classname); |
2048 | 2048 | if ( |
2049 | 2049 | array_key_exists($primary_id_ref, $props_n_values) |
2050 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
2050 | + && ! empty($props_n_values[$primary_id_ref]) |
|
2051 | 2051 | ) { |
2052 | - $id = $props_n_values[ $primary_id_ref ]; |
|
2052 | + $id = $props_n_values[$primary_id_ref]; |
|
2053 | 2053 | return self::_get_model($classname)->get_from_entity_map($id); |
2054 | 2054 | } |
2055 | 2055 | return false; |
@@ -2084,10 +2084,10 @@ discard block |
||
2084 | 2084 | $primary_id_ref = self::_get_primary_key_name($classname); |
2085 | 2085 | if ( |
2086 | 2086 | array_key_exists($primary_id_ref, $props_n_values) |
2087 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
2087 | + && ! empty($props_n_values[$primary_id_ref]) |
|
2088 | 2088 | ) { |
2089 | 2089 | $existing = $model->get_one_by_ID( |
2090 | - $props_n_values[ $primary_id_ref ] |
|
2090 | + $props_n_values[$primary_id_ref] |
|
2091 | 2091 | ); |
2092 | 2092 | } |
2093 | 2093 | } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
@@ -2099,7 +2099,7 @@ discard block |
||
2099 | 2099 | } |
2100 | 2100 | if ($existing) { |
2101 | 2101 | // set date formats if present before setting values |
2102 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
2102 | + if ( ! empty($date_formats) && is_array($date_formats)) { |
|
2103 | 2103 | $existing->set_date_format($date_formats[0]); |
2104 | 2104 | $existing->set_time_format($date_formats[1]); |
2105 | 2105 | } else { |
@@ -2132,7 +2132,7 @@ discard block |
||
2132 | 2132 | protected static function _get_model($classname, $timezone = null) |
2133 | 2133 | { |
2134 | 2134 | // find model for this class |
2135 | - if (! $classname) { |
|
2135 | + if ( ! $classname) { |
|
2136 | 2136 | throw new EE_Error( |
2137 | 2137 | sprintf( |
2138 | 2138 | esc_html__( |
@@ -2181,7 +2181,7 @@ discard block |
||
2181 | 2181 | if (strpos($model_name, 'EE_') === 0) { |
2182 | 2182 | $model_classname = str_replace('EE_', 'EEM_', $model_name); |
2183 | 2183 | } else { |
2184 | - $model_classname = 'EEM_' . $model_name; |
|
2184 | + $model_classname = 'EEM_'.$model_name; |
|
2185 | 2185 | } |
2186 | 2186 | return $model_classname; |
2187 | 2187 | } |
@@ -2200,7 +2200,7 @@ discard block |
||
2200 | 2200 | */ |
2201 | 2201 | protected static function _get_primary_key_name($classname = null) |
2202 | 2202 | { |
2203 | - if (! $classname) { |
|
2203 | + if ( ! $classname) { |
|
2204 | 2204 | throw new EE_Error( |
2205 | 2205 | sprintf( |
2206 | 2206 | esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
@@ -2229,7 +2229,7 @@ discard block |
||
2229 | 2229 | { |
2230 | 2230 | // now that we know the name of the variable, use a variable variable to get its value and return its |
2231 | 2231 | if ($this->_model->has_primary_key_field()) { |
2232 | - return $this->_fields[ $this->_model->primary_key_name() ]; |
|
2232 | + return $this->_fields[$this->_model->primary_key_name()]; |
|
2233 | 2233 | } |
2234 | 2234 | return $this->_model->get_index_primary_key_string($this->_fields); |
2235 | 2235 | } |
@@ -2281,7 +2281,7 @@ discard block |
||
2281 | 2281 | } |
2282 | 2282 | } else { |
2283 | 2283 | // this thing doesn't exist in the DB, so just cache it |
2284 | - if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
2284 | + if ( ! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
2285 | 2285 | throw new EE_Error( |
2286 | 2286 | sprintf( |
2287 | 2287 | esc_html__( |
@@ -2446,7 +2446,7 @@ discard block |
||
2446 | 2446 | } else { |
2447 | 2447 | // did we already cache the result of this query? |
2448 | 2448 | $cached_results = $this->get_all_from_cache($relationName); |
2449 | - if (! $cached_results) { |
|
2449 | + if ( ! $cached_results) { |
|
2450 | 2450 | $related_model_objects = $this->_model->get_all_related( |
2451 | 2451 | $this, |
2452 | 2452 | $relationName, |
@@ -2555,7 +2555,7 @@ discard block |
||
2555 | 2555 | } else { |
2556 | 2556 | // first, check if we've already cached the result of this query |
2557 | 2557 | $cached_result = $this->get_one_from_cache($relationName); |
2558 | - if (! $cached_result) { |
|
2558 | + if ( ! $cached_result) { |
|
2559 | 2559 | $related_model_object = $this->_model->get_first_related( |
2560 | 2560 | $this, |
2561 | 2561 | $relationName, |
@@ -2579,7 +2579,7 @@ discard block |
||
2579 | 2579 | } |
2580 | 2580 | // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
2581 | 2581 | // just get what's cached on this object |
2582 | - if (! $related_model_object) { |
|
2582 | + if ( ! $related_model_object) { |
|
2583 | 2583 | $related_model_object = $this->get_one_from_cache($relationName); |
2584 | 2584 | } |
2585 | 2585 | } |
@@ -2661,7 +2661,7 @@ discard block |
||
2661 | 2661 | */ |
2662 | 2662 | public function is_set($field_name) |
2663 | 2663 | { |
2664 | - return isset($this->_fields[ $field_name ]); |
|
2664 | + return isset($this->_fields[$field_name]); |
|
2665 | 2665 | } |
2666 | 2666 | |
2667 | 2667 | |
@@ -2677,7 +2677,7 @@ discard block |
||
2677 | 2677 | { |
2678 | 2678 | foreach ((array) $properties as $property_name) { |
2679 | 2679 | // first make sure this property exists |
2680 | - if (! $this->_fields[ $property_name ]) { |
|
2680 | + if ( ! $this->_fields[$property_name]) { |
|
2681 | 2681 | throw new EE_Error( |
2682 | 2682 | sprintf( |
2683 | 2683 | esc_html__( |
@@ -2709,7 +2709,7 @@ discard block |
||
2709 | 2709 | $properties = array(); |
2710 | 2710 | // remove prepended underscore |
2711 | 2711 | foreach ($fields as $field_name => $settings) { |
2712 | - $properties[ $field_name ] = $this->get($field_name); |
|
2712 | + $properties[$field_name] = $this->get($field_name); |
|
2713 | 2713 | } |
2714 | 2714 | return $properties; |
2715 | 2715 | } |
@@ -2746,7 +2746,7 @@ discard block |
||
2746 | 2746 | { |
2747 | 2747 | $className = get_class($this); |
2748 | 2748 | $tagName = "FHEE__{$className}__{$methodName}"; |
2749 | - if (! has_filter($tagName)) { |
|
2749 | + if ( ! has_filter($tagName)) { |
|
2750 | 2750 | throw new EE_Error( |
2751 | 2751 | sprintf( |
2752 | 2752 | esc_html__( |
@@ -2791,7 +2791,7 @@ discard block |
||
2791 | 2791 | $query_params[0]['EXM_value'] = $meta_value; |
2792 | 2792 | } |
2793 | 2793 | $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
2794 | - if (! $existing_rows_like_that) { |
|
2794 | + if ( ! $existing_rows_like_that) { |
|
2795 | 2795 | return $this->add_extra_meta($meta_key, $meta_value); |
2796 | 2796 | } |
2797 | 2797 | foreach ($existing_rows_like_that as $existing_row) { |
@@ -2909,7 +2909,7 @@ discard block |
||
2909 | 2909 | $values = array(); |
2910 | 2910 | foreach ($results as $result) { |
2911 | 2911 | if ($result instanceof EE_Extra_Meta) { |
2912 | - $values[ $result->ID() ] = $result->value(); |
|
2912 | + $values[$result->ID()] = $result->value(); |
|
2913 | 2913 | } |
2914 | 2914 | } |
2915 | 2915 | return $values; |
@@ -2954,17 +2954,17 @@ discard block |
||
2954 | 2954 | ); |
2955 | 2955 | foreach ($extra_meta_objs as $extra_meta_obj) { |
2956 | 2956 | if ($extra_meta_obj instanceof EE_Extra_Meta) { |
2957 | - $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
2957 | + $return_array[$extra_meta_obj->key()] = $extra_meta_obj->value(); |
|
2958 | 2958 | } |
2959 | 2959 | } |
2960 | 2960 | } else { |
2961 | 2961 | $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
2962 | 2962 | foreach ($extra_meta_objs as $extra_meta_obj) { |
2963 | 2963 | if ($extra_meta_obj instanceof EE_Extra_Meta) { |
2964 | - if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
2965 | - $return_array[ $extra_meta_obj->key() ] = array(); |
|
2964 | + if ( ! isset($return_array[$extra_meta_obj->key()])) { |
|
2965 | + $return_array[$extra_meta_obj->key()] = array(); |
|
2966 | 2966 | } |
2967 | - $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
2967 | + $return_array[$extra_meta_obj->key()][$extra_meta_obj->ID()] = $extra_meta_obj->value(); |
|
2968 | 2968 | } |
2969 | 2969 | } |
2970 | 2970 | } |
@@ -3045,8 +3045,8 @@ discard block |
||
3045 | 3045 | 'event_espresso' |
3046 | 3046 | ), |
3047 | 3047 | $this->ID(), |
3048 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
3049 | - get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
3048 | + get_class($this->get_model()).'::instance()->add_to_entity_map()', |
|
3049 | + get_class($this->get_model()).'::instance()->refresh_entity_map()' |
|
3050 | 3050 | ) |
3051 | 3051 | ); |
3052 | 3052 | } |
@@ -3079,7 +3079,7 @@ discard block |
||
3079 | 3079 | { |
3080 | 3080 | // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB |
3081 | 3081 | // if it wasn't even there to start off. |
3082 | - if (! $this->ID()) { |
|
3082 | + if ( ! $this->ID()) { |
|
3083 | 3083 | $this->save(); |
3084 | 3084 | } |
3085 | 3085 | global $wpdb; |
@@ -3308,7 +3308,7 @@ discard block |
||
3308 | 3308 | { |
3309 | 3309 | foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
3310 | 3310 | if ($relation_obj instanceof EE_Belongs_To_Relation) { |
3311 | - $classname = 'EE_' . $this->_model->get_this_model_name(); |
|
3311 | + $classname = 'EE_'.$this->_model->get_this_model_name(); |
|
3312 | 3312 | if ($this->get_one_from_cache($relation_name) instanceof $classname |
3313 | 3313 | && $this->get_one_from_cache($relation_name)->ID() |
3314 | 3314 | ) { |
@@ -3348,7 +3348,7 @@ discard block |
||
3348 | 3348 | // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
3349 | 3349 | foreach ($this->_fields as $field => $value) { |
3350 | 3350 | if ($value instanceof DateTime) { |
3351 | - $this->_fields[ $field ] = clone $value; |
|
3351 | + $this->_fields[$field] = clone $value; |
|
3352 | 3352 | } |
3353 | 3353 | } |
3354 | 3354 | } |