@@ -13,1246 +13,1246 @@ discard block |
||
13 | 13 | abstract class EE_Base_Class |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * @var boolean indicating whether or not this model object is intended to ever be saved |
|
18 | - * For example, we might create model objects intended to only be used for the duration |
|
19 | - * of this request and to be thrown away, and if they were accidentally saved |
|
20 | - * it would be a bug. |
|
21 | - */ |
|
22 | - protected $_allow_persist = true; |
|
23 | - |
|
24 | - /** |
|
25 | - * This property is for holding a cached array of object properties indexed by property name as the key. |
|
26 | - * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
27 | - * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
28 | - * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
29 | - * |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $_cached_properties = []; |
|
33 | - |
|
34 | - /** |
|
35 | - * This is a cache of results from custom selections done on a query that constructs this entity. The only purpose |
|
36 | - * for these values is for retrieval of the results, they are not further queryable and they are not persisted to |
|
37 | - * the db. They also do not automatically update if there are any changes to the data that produced their results. |
|
38 | - * The format is a simple array of field_alias => field_value. So for instance if a custom select was something |
|
39 | - * like, "Select COUNT(Registration.REG_ID) as Registration_Count ...", then the resulting value will be in this |
|
40 | - * array as: |
|
41 | - * array( |
|
42 | - * 'Registration_Count' => 24 |
|
43 | - * ); |
|
44 | - * Note: if the custom select configuration for the query included a data type, the value will be in the data type |
|
45 | - * provided for the query (@see EventEspresso\core\domain\values\model\CustomSelects::__construct phpdocs for more |
|
46 | - * info) |
|
47 | - * |
|
48 | - * @var array |
|
49 | - */ |
|
50 | - protected $custom_selection_results = []; |
|
51 | - |
|
52 | - /** |
|
53 | - * date format |
|
54 | - * pattern or format for displaying dates |
|
55 | - * |
|
56 | - * @var string $_dt_frmt |
|
57 | - */ |
|
58 | - protected $_dt_frmt; |
|
59 | - |
|
60 | - /** |
|
61 | - * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
62 | - * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
63 | - * |
|
64 | - * @var array |
|
65 | - */ |
|
66 | - protected $_fields = []; |
|
67 | - |
|
68 | - /** |
|
69 | - * @var boolean indicating whether or not this model object's properties have changed since construction |
|
70 | - */ |
|
71 | - protected $_has_changes = false; |
|
72 | - |
|
73 | - /** |
|
74 | - * @var EEM_Base |
|
75 | - */ |
|
76 | - protected $_model; |
|
77 | - |
|
78 | - |
|
79 | - /** |
|
80 | - * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
81 | - * single |
|
82 | - * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
83 | - * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
84 | - * all others have an array) |
|
85 | - * |
|
86 | - * @var array |
|
87 | - */ |
|
88 | - protected $_model_relations = []; |
|
89 | - |
|
90 | - /** |
|
91 | - * This is an array of the original properties and values provided during construction |
|
92 | - * of this model object. (keys are model field names, values are their values). |
|
93 | - * This list is important to remember so that when we are merging data from the db, we know |
|
94 | - * which values to override and which to not override. |
|
95 | - * |
|
96 | - * @var array |
|
97 | - */ |
|
98 | - protected $_props_n_values_provided_in_constructor; |
|
99 | - |
|
100 | - /** |
|
101 | - * Timezone |
|
102 | - * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
103 | - * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
104 | - * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
105 | - * access to it. |
|
106 | - * |
|
107 | - * @var string |
|
108 | - */ |
|
109 | - protected $_timezone = ''; |
|
110 | - |
|
111 | - /** |
|
112 | - * time format |
|
113 | - * pattern or format for displaying time |
|
114 | - * |
|
115 | - * @var string $_tm_frmt |
|
116 | - */ |
|
117 | - protected $_tm_frmt; |
|
118 | - |
|
119 | - |
|
120 | - /** |
|
121 | - * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
122 | - * play nice |
|
123 | - * |
|
124 | - * @param array $field_values where each key is a field (ie, array key in the 2nd |
|
125 | - * layer of the model's _fields array, (eg, EVT_ID, |
|
126 | - * TXN_amount, QST_name, etc) and values are their values |
|
127 | - * @param bool $set_from_db a flag for setting if the class is instantiated by the |
|
128 | - * corresponding db model or not. |
|
129 | - * @param string $timezone indicate what timezone you want any datetime fields to |
|
130 | - * be in when instantiating a EE_Base_Class object. |
|
131 | - * @param array $date_formats An array of date formats to set on construct where first |
|
132 | - * value is the date_format and second value is the time |
|
133 | - * format. |
|
134 | - * @throws InvalidArgumentException |
|
135 | - * @throws InvalidInterfaceException |
|
136 | - * @throws InvalidDataTypeException |
|
137 | - * @throws EE_Error |
|
138 | - * @throws ReflectionException |
|
139 | - */ |
|
140 | - protected function __construct( |
|
141 | - array $field_values = [], |
|
142 | - bool $set_from_db = false, |
|
143 | - string $timezone = '', |
|
144 | - array $date_formats = [] |
|
145 | - ) { |
|
146 | - // ensure $fieldValues is an array |
|
147 | - $field_values = is_array($field_values) ? $field_values : [$field_values]; |
|
148 | - $className = get_class($this); |
|
149 | - do_action("AHEE__{$className}__construct", $this, $field_values); |
|
150 | - // remember what values were passed to this constructor |
|
151 | - $this->_props_n_values_provided_in_constructor = $field_values; |
|
152 | - $this->setDateAndTimeFormats($date_formats); |
|
153 | - $this->_model = $this->get_model($timezone); |
|
154 | - $model_fields = $this->_model->field_settings(); |
|
155 | - $this->validateFieldValues($model_fields, $field_values); |
|
156 | - $this->setFieldValues($model_fields, $field_values, $set_from_db); |
|
157 | - // remember in entity mapper |
|
158 | - if (! $set_from_db && $this->_model->has_primary_key_field() && $this->ID()) { |
|
159 | - $this->_model->add_to_entity_map($this); |
|
160 | - } |
|
161 | - // setup all the relations |
|
162 | - foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
163 | - $this->_model_relations[ $relation_name ] = $relation_obj instanceof EE_Belongs_To_Relation |
|
164 | - ? null |
|
165 | - : []; |
|
166 | - } |
|
167 | - /** |
|
168 | - * Action done at the end of each model object construction |
|
169 | - * |
|
170 | - * @param EE_Base_Class $this the model object just created |
|
171 | - */ |
|
172 | - do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
173 | - } |
|
174 | - |
|
175 | - |
|
176 | - /** |
|
177 | - * for getting a model while instantiated. |
|
178 | - * |
|
179 | - * @param string $timezone |
|
180 | - * @return EEM_Base | EEM_CPT_Base |
|
181 | - * @throws EE_Error |
|
182 | - * @throws ReflectionException |
|
183 | - */ |
|
184 | - public function get_model(string $timezone = '') |
|
185 | - { |
|
186 | - if (! $this->_model) { |
|
187 | - $timezone = $timezone ?? $this->_timezone; |
|
188 | - $modelName = self::_get_model_classname(get_class($this)); |
|
189 | - $this->_model = self::_get_model_instance_with_name($modelName, $timezone); |
|
190 | - $this->set_timezone($timezone); |
|
191 | - } |
|
192 | - return $this->_model; |
|
193 | - } |
|
194 | - |
|
195 | - |
|
196 | - /** |
|
197 | - * Overrides parent because parent expects old models. |
|
198 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
199 | - * |
|
200 | - * @param string $field_name |
|
201 | - * @param mixed $field_value_from_db |
|
202 | - * @throws InvalidArgumentException |
|
203 | - * @throws InvalidInterfaceException |
|
204 | - * @throws InvalidDataTypeException |
|
205 | - * @throws EE_Error |
|
206 | - */ |
|
207 | - public function set_from_db(string $field_name, $field_value_from_db) |
|
208 | - { |
|
209 | - $field_obj = $this->_model->field_settings_for($field_name); |
|
210 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
211 | - // you would think the DB has no NULLs for non-null label fields right? wrong! |
|
212 | - // eg, a CPT model object could have an entry in the posts table, but no |
|
213 | - // entry in the meta table. Meaning that all its columns in the meta table |
|
214 | - // are null! yikes! so when we find one like that, use defaults for its meta columns |
|
215 | - |
|
216 | - if ($field_value_from_db === null) { |
|
217 | - if ($field_obj->is_nullable()) { |
|
218 | - // if the field allows nulls, then let it be null |
|
219 | - $field_value = null; |
|
220 | - } else { |
|
221 | - $field_value = $field_obj->get_default_value(); |
|
222 | - } |
|
223 | - } else { |
|
224 | - $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
225 | - } |
|
226 | - $this->_fields[ $field_name ] = $field_value; |
|
227 | - $this->_clear_cached_property($field_name); |
|
228 | - } |
|
229 | - } |
|
230 | - |
|
231 | - |
|
232 | - /** |
|
233 | - * Overrides parent because parent expects old models. |
|
234 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
235 | - * |
|
236 | - * @param string $field_name |
|
237 | - * @param mixed $field_value |
|
238 | - * @param bool $use_default |
|
239 | - * @throws InvalidArgumentException |
|
240 | - * @throws InvalidInterfaceException |
|
241 | - * @throws InvalidDataTypeException |
|
242 | - * @throws EE_Error |
|
243 | - * @throws ReflectionException |
|
244 | - * @throws ReflectionException |
|
245 | - * @throws ReflectionException |
|
246 | - */ |
|
247 | - public function set(string $field_name, $field_value, bool $use_default = false) |
|
248 | - { |
|
249 | - // if not using default and nothing has changed, and object has already been setup (has ID), |
|
250 | - // then don't do anything |
|
251 | - if ( |
|
252 | - ! $use_default |
|
253 | - && $this->_fields[ $field_name ] === $field_value |
|
254 | - && $this->ID() |
|
255 | - ) { |
|
256 | - return; |
|
257 | - } |
|
258 | - $this->_has_changes = true; |
|
259 | - $field_obj = $this->_model->field_settings_for($field_name); |
|
260 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
261 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
262 | - $field_obj->set_timezone($this->_timezone); |
|
263 | - $field_obj->set_date_format($this->_dt_frmt); |
|
264 | - $field_obj->set_time_format($this->_tm_frmt); |
|
265 | - } |
|
266 | - $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
267 | - // should the value be null? |
|
268 | - if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
269 | - $this->_fields[ $field_name ] = $field_obj->get_default_value(); |
|
270 | - /** |
|
271 | - * To save having to refactor all the models, if a default value is used for a |
|
272 | - * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
273 | - * object. Then let's do a set again to ensure that it becomes a DateTime |
|
274 | - * object. |
|
275 | - * |
|
276 | - * @since 4.6.10+ |
|
277 | - */ |
|
278 | - if ( |
|
279 | - $field_obj instanceof EE_Datetime_Field |
|
280 | - && $this->_fields[ $field_name ] !== null |
|
281 | - && ! $this->_fields[ $field_name ] instanceof DateTime |
|
282 | - ) { |
|
283 | - empty($this->_fields[ $field_name ]) |
|
284 | - ? $this->set($field_name, time()) |
|
285 | - : $this->set($field_name, $this->_fields[ $field_name ]); |
|
286 | - } |
|
287 | - } else { |
|
288 | - $this->_fields[ $field_name ] = $holder_of_value; |
|
289 | - } |
|
290 | - // if we're not in the constructor... |
|
291 | - // now check if what we set was a primary key |
|
292 | - if ( |
|
293 | - // note: props_n_values_provided_in_constructor is only set at the END of the constructor |
|
294 | - $this->_props_n_values_provided_in_constructor |
|
295 | - && $field_value |
|
296 | - && $field_name === $this->_model->primary_key_name() |
|
297 | - ) { |
|
298 | - // if so, we want all this object's fields to be filled either with |
|
299 | - // what we've explicitly set on this model |
|
300 | - // or what we have in the db |
|
301 | - // echo "setting primary key!"; |
|
302 | - $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
303 | - $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
304 | - foreach ($fields_on_model as $field_obj) { |
|
305 | - if ( |
|
306 | - ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
307 | - && $field_obj->get_name() !== $field_name |
|
308 | - ) { |
|
309 | - $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
310 | - } |
|
311 | - } |
|
312 | - // oh this model object has an ID? well make sure its in the entity mapper |
|
313 | - $this->_model->add_to_entity_map($this); |
|
314 | - } |
|
315 | - // let's unset any cache for this field_name from the $_cached_properties property. |
|
316 | - $this->_clear_cached_property($field_name); |
|
317 | - } else { |
|
318 | - throw new EE_Error( |
|
319 | - sprintf( |
|
320 | - esc_html__( |
|
321 | - 'A valid EE_Model_Field_Base could not be found for the given field name: %s', |
|
322 | - 'event_espresso' |
|
323 | - ), |
|
324 | - $field_name |
|
325 | - ) |
|
326 | - ); |
|
327 | - } |
|
328 | - } |
|
329 | - |
|
330 | - |
|
331 | - /** |
|
332 | - * Gets the value of the primary key. |
|
333 | - * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
334 | - * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
335 | - * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
336 | - * |
|
337 | - * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
338 | - * @throws InvalidArgumentException |
|
339 | - * @throws InvalidInterfaceException |
|
340 | - * @throws InvalidDataTypeException |
|
341 | - * @throws EE_Error |
|
342 | - */ |
|
343 | - public function ID() |
|
344 | - { |
|
345 | - // now that we know the name of the variable, use a variable variable to get its value and return its |
|
346 | - if ($this->_model->has_primary_key_field()) { |
|
347 | - return $this->_fields[ $this->_model->primary_key_name() ]; |
|
348 | - } |
|
349 | - return $this->_model->get_index_primary_key_string($this->_fields); |
|
350 | - } |
|
351 | - |
|
352 | - |
|
353 | - /** |
|
354 | - * If a model name is provided (eg Registration), gets the model classname for that model. |
|
355 | - * Also works if a model class's classname is provided (eg EE_Registration). |
|
356 | - * |
|
357 | - * @param string $model_name |
|
358 | - * @return string like EEM_Attendee |
|
359 | - */ |
|
360 | - private static function _get_model_classname(string $model_name = ''): string |
|
361 | - { |
|
362 | - return strpos($model_name, 'EE_') === 0 |
|
363 | - ? str_replace('EE_', 'EEM_', $model_name) |
|
364 | - : 'EEM_' . $model_name; |
|
365 | - } |
|
366 | - |
|
367 | - |
|
368 | - /** |
|
369 | - * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
370 | - * |
|
371 | - * @param string $model_classname |
|
372 | - * @param string $timezone |
|
373 | - * @return EEM_Base |
|
374 | - * @throws ReflectionException |
|
375 | - * @throws InvalidArgumentException |
|
376 | - * @throws InvalidInterfaceException |
|
377 | - * @throws InvalidDataTypeException |
|
378 | - * @throws EE_Error |
|
379 | - */ |
|
380 | - protected static function _get_model_instance_with_name(string $model_classname, string $timezone = ''): EEM_Base |
|
381 | - { |
|
382 | - $model_classname = str_replace('EEM_', '', $model_classname); |
|
383 | - $model = EE_Registry::instance()->load_model($model_classname); |
|
384 | - $model->set_timezone($timezone); |
|
385 | - return $model; |
|
386 | - } |
|
387 | - |
|
388 | - |
|
389 | - /** |
|
390 | - * This just clears out ONE property if it exists in the cache |
|
391 | - * |
|
392 | - * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
393 | - * @return void |
|
394 | - */ |
|
395 | - protected function _clear_cached_property(string $property_name) |
|
396 | - { |
|
397 | - unset($this->_cached_properties[ $property_name ]); |
|
398 | - } |
|
399 | - |
|
400 | - |
|
401 | - /** |
|
402 | - * Gets the EEM_*_Model for this class |
|
403 | - * |
|
404 | - * @param string $classname |
|
405 | - * @param string $timezone |
|
406 | - * @return EEM_Base |
|
407 | - * @throws InvalidArgumentException |
|
408 | - * @throws InvalidInterfaceException |
|
409 | - * @throws InvalidDataTypeException |
|
410 | - * @throws EE_Error |
|
411 | - * @throws ReflectionException |
|
412 | - */ |
|
413 | - protected static function _get_model(string $classname, string $timezone = ''): EEM_Base |
|
414 | - { |
|
415 | - // find model for this class |
|
416 | - if (! $classname) { |
|
417 | - throw new EE_Error( |
|
418 | - sprintf( |
|
419 | - esc_html__( |
|
420 | - 'What were you thinking calling _get_model(%s)?? You need to specify the class name', |
|
421 | - 'event_espresso' |
|
422 | - ), |
|
423 | - $classname |
|
424 | - ) |
|
425 | - ); |
|
426 | - } |
|
427 | - $modelName = self::_get_model_classname($classname); |
|
428 | - return self::_get_model_instance_with_name($modelName, $timezone); |
|
429 | - } |
|
430 | - |
|
431 | - |
|
432 | - /** |
|
433 | - * verifies that the specified field is of the correct type |
|
434 | - * |
|
435 | - * @param string $field_name |
|
436 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
437 | - * (in cases where the same property may be used for different outputs |
|
438 | - * - i.e. datetime, money etc.) |
|
439 | - * @return mixed |
|
440 | - * @throws InvalidArgumentException |
|
441 | - * @throws InvalidInterfaceException |
|
442 | - * @throws InvalidDataTypeException |
|
443 | - * @throws EE_Error |
|
444 | - */ |
|
445 | - public function get(string $field_name, string $extra_cache_ref = '') |
|
446 | - { |
|
447 | - return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
448 | - } |
|
449 | - |
|
450 | - |
|
451 | - /** |
|
452 | - * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
453 | - * This also SETS the cache if we return the actual property! |
|
454 | - * |
|
455 | - * @param string $field_name the name of the property we're trying to retrieve |
|
456 | - * @param bool $pretty |
|
457 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
458 | - * (in cases where the same property may be used for different outputs |
|
459 | - * - i.e. datetime, money etc.) |
|
460 | - * It can also accept certain pre-defined "schema" strings |
|
461 | - * to define how to output the property. |
|
462 | - * see the field's prepare_for_pretty_echoing for what strings can be used |
|
463 | - * @return mixed whatever the value for the property is we're retrieving |
|
464 | - * @throws InvalidArgumentException |
|
465 | - * @throws InvalidInterfaceException |
|
466 | - * @throws InvalidDataTypeException |
|
467 | - * @throws EE_Error |
|
468 | - */ |
|
469 | - protected function _get_cached_property(string $field_name, bool $pretty = false, string $extra_cache_ref = '') |
|
470 | - { |
|
471 | - // verify the field exists |
|
472 | - $this->_model->field_settings_for($field_name); |
|
473 | - $cache_type = $pretty ? 'pretty' : 'standard'; |
|
474 | - $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
475 | - if (isset($this->_cached_properties[ $field_name ][ $cache_type ])) { |
|
476 | - return $this->_cached_properties[ $field_name ][ $cache_type ]; |
|
477 | - } |
|
478 | - $value = $this->_get_fresh_property($field_name, $pretty, $extra_cache_ref); |
|
479 | - $this->_set_cached_property($field_name, $value, $cache_type); |
|
480 | - return $value; |
|
481 | - } |
|
482 | - |
|
483 | - |
|
484 | - /** |
|
485 | - * If the cache didn't fetch the needed item, this fetches it. |
|
486 | - * |
|
487 | - * @param string $field_name |
|
488 | - * @param bool $pretty |
|
489 | - * @param string $extra_cache_ref |
|
490 | - * @return mixed |
|
491 | - * @throws InvalidArgumentException |
|
492 | - * @throws InvalidInterfaceException |
|
493 | - * @throws InvalidDataTypeException |
|
494 | - * @throws EE_Error |
|
495 | - */ |
|
496 | - protected function _get_fresh_property(string $field_name, bool $pretty = false, string $extra_cache_ref = '') |
|
497 | - { |
|
498 | - $field_obj = $this->_model->field_settings_for($field_name); |
|
499 | - // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
500 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
501 | - $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
502 | - } |
|
503 | - if (! isset($this->_fields[ $field_name ])) { |
|
504 | - $this->_fields[ $field_name ] = null; |
|
505 | - } |
|
506 | - return $pretty |
|
507 | - ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $field_name ], $extra_cache_ref) |
|
508 | - : $field_obj->prepare_for_get($this->_fields[ $field_name ]); |
|
509 | - } |
|
510 | - |
|
511 | - |
|
512 | - /** |
|
513 | - * For adding an item to the cached_properties property. |
|
514 | - * |
|
515 | - * @param string $field_name the property item the corresponding value is for. |
|
516 | - * @param mixed $value The value we are caching. |
|
517 | - * @param string $cache_type |
|
518 | - * @return void |
|
519 | - * @throws InvalidArgumentException |
|
520 | - * @throws InvalidInterfaceException |
|
521 | - * @throws InvalidDataTypeException |
|
522 | - * @throws EE_Error |
|
523 | - */ |
|
524 | - protected function _set_cached_property(string $field_name, $value, string $cache_type = '') |
|
525 | - { |
|
526 | - // first make sure this property exists |
|
527 | - $this->_model->field_settings_for($field_name); |
|
528 | - $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
529 | - $this->_cached_properties[ $field_name ][ $cache_type ] = $value; |
|
530 | - } |
|
531 | - |
|
532 | - |
|
533 | - /** |
|
534 | - * set timezone, formats, and output for EE_Datetime_Field objects |
|
535 | - * |
|
536 | - * @param EE_Datetime_Field $datetime_field |
|
537 | - * @param bool $pretty |
|
538 | - * @param null $date_or_time |
|
539 | - * @return void |
|
540 | - * @throws InvalidArgumentException |
|
541 | - * @throws InvalidInterfaceException |
|
542 | - * @throws InvalidDataTypeException |
|
543 | - */ |
|
544 | - protected function _prepare_datetime_field( |
|
545 | - EE_Datetime_Field $datetime_field, |
|
546 | - bool $pretty = false, |
|
547 | - $date_or_time = null |
|
548 | - ) { |
|
549 | - $datetime_field->set_timezone($this->_timezone); |
|
550 | - $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
551 | - $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
552 | - // set the output returned |
|
553 | - switch ($date_or_time) { |
|
554 | - case 'D': |
|
555 | - $datetime_field->set_date_time_output('date'); |
|
556 | - break; |
|
557 | - case 'T': |
|
558 | - $datetime_field->set_date_time_output('time'); |
|
559 | - break; |
|
560 | - default: |
|
561 | - $datetime_field->set_date_time_output(); |
|
562 | - } |
|
563 | - } |
|
564 | - |
|
565 | - |
|
566 | - /** |
|
567 | - * @param $props_n_values |
|
568 | - * @param $classname |
|
569 | - * @return EE_Base_Class|null |
|
570 | - * @throws ReflectionException |
|
571 | - * @throws InvalidArgumentException |
|
572 | - * @throws InvalidInterfaceException |
|
573 | - * @throws InvalidDataTypeException |
|
574 | - * @throws EE_Error |
|
575 | - * @throws ReflectionException |
|
576 | - */ |
|
577 | - protected static function _get_object_from_entity_mapper($props_n_values, $classname): ?EE_Base_Class |
|
578 | - { |
|
579 | - // TODO: will not work for Term_Relationships because they have no PK! |
|
580 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
581 | - if ( |
|
582 | - array_key_exists($primary_id_ref, $props_n_values) |
|
583 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
584 | - ) { |
|
585 | - $id = $props_n_values[ $primary_id_ref ]; |
|
586 | - return self::_get_model($classname)->get_from_entity_map($id); |
|
587 | - } |
|
588 | - return null; |
|
589 | - } |
|
590 | - |
|
591 | - |
|
592 | - /** |
|
593 | - * returns the name of the primary key attribute |
|
594 | - * |
|
595 | - * @param null $classname |
|
596 | - * @return string |
|
597 | - * @throws InvalidArgumentException |
|
598 | - * @throws InvalidInterfaceException |
|
599 | - * @throws InvalidDataTypeException |
|
600 | - * @throws EE_Error |
|
601 | - * @throws ReflectionException |
|
602 | - * @throws ReflectionException |
|
603 | - * @throws ReflectionException |
|
604 | - */ |
|
605 | - protected static function _get_primary_key_name($classname = null): string |
|
606 | - { |
|
607 | - if (! $classname) { |
|
608 | - throw new EE_Error( |
|
609 | - sprintf( |
|
610 | - esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
|
611 | - $classname |
|
612 | - ) |
|
613 | - ); |
|
614 | - } |
|
615 | - return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
616 | - } |
|
617 | - |
|
618 | - |
|
619 | - /** |
|
620 | - * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
621 | - * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
622 | - * 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 |
|
623 | - * we return false. |
|
624 | - * |
|
625 | - * @param array $props_n_values incoming array of properties and their values |
|
626 | - * @param string $classname the classname of the child class |
|
627 | - * @param string $timezone |
|
628 | - * @param array $date_formats incoming date_formats in an array where the first value is the |
|
629 | - * date_format and the second value is the time format |
|
630 | - * @return EE_Base_Class|EE_Soft_Delete_Base_Class|null |
|
631 | - * @throws InvalidArgumentException |
|
632 | - * @throws InvalidInterfaceException |
|
633 | - * @throws InvalidDataTypeException |
|
634 | - * @throws EE_Error |
|
635 | - * @throws ReflectionException |
|
636 | - * @throws ReflectionException |
|
637 | - * @throws ReflectionException |
|
638 | - */ |
|
639 | - protected static function _check_for_object( |
|
640 | - array $props_n_values, |
|
641 | - string $classname, |
|
642 | - string $timezone = '', |
|
643 | - array $date_formats = [] |
|
644 | - ) { |
|
645 | - $existing = null; |
|
646 | - $model = self::_get_model($classname, $timezone); |
|
647 | - if ($model->has_primary_key_field()) { |
|
648 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
649 | - if ( |
|
650 | - array_key_exists($primary_id_ref, $props_n_values) |
|
651 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
652 | - ) { |
|
653 | - $existing = $model->get_one_by_ID($props_n_values[ $primary_id_ref ]); |
|
654 | - } |
|
655 | - } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
656 | - // no primary key on this model, but there's still a matching item in the DB |
|
657 | - $existing = $model->get_one_by_ID( |
|
658 | - $model->get_index_primary_key_string($props_n_values) |
|
659 | - ); |
|
660 | - } |
|
661 | - if ($existing) { |
|
662 | - $has_date_formats = ! empty($date_formats) && is_array($date_formats) |
|
663 | - && isset($date_formats[0], $date_formats[1]); |
|
664 | - $date_format = $has_date_formats ? $date_formats[0] : (string) get_option('date_format', 'Y-m-d'); |
|
665 | - $time_format = $has_date_formats ? $date_formats[1] : (string) get_option('time_format', 'g:i a'); |
|
666 | - // set date formats before setting values |
|
667 | - $existing->set_date_format($date_format); |
|
668 | - $existing->set_time_format($time_format); |
|
669 | - foreach ($props_n_values as $property => $field_value) { |
|
670 | - $existing->set($property, $field_value); |
|
671 | - } |
|
672 | - return $existing; |
|
673 | - } |
|
674 | - return null; |
|
675 | - } |
|
676 | - |
|
677 | - |
|
678 | - /** |
|
679 | - * This sets the internal date format to what is sent in to be used as the new default for the class |
|
680 | - * internally instead of wp set date format options |
|
681 | - * |
|
682 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
683 | - * @since 4.6 |
|
684 | - */ |
|
685 | - public function set_date_format($format) |
|
686 | - { |
|
687 | - if ($format !== $this->_dt_frmt) { |
|
688 | - $this->_dt_frmt = $format; |
|
689 | - // clear cached_properties because they won't be relevant now. |
|
690 | - $this->_clear_cached_properties(); |
|
691 | - } |
|
692 | - } |
|
693 | - |
|
694 | - |
|
695 | - /** |
|
696 | - * This sets the internal time format string to what is sent in to be used as the new default for the |
|
697 | - * class internally instead of wp set time format options. |
|
698 | - * |
|
699 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
700 | - * @since 4.6 |
|
701 | - */ |
|
702 | - public function set_time_format($format) |
|
703 | - { |
|
704 | - if ($format !== $this->_tm_frmt) { |
|
705 | - $this->_tm_frmt = $format; |
|
706 | - // clear cached_properties because they won't be relevant now. |
|
707 | - $this->_clear_cached_properties(); |
|
708 | - } |
|
709 | - } |
|
710 | - |
|
711 | - |
|
712 | - /** |
|
713 | - * This just takes care of clearing out the cached_properties |
|
714 | - * |
|
715 | - * @return void |
|
716 | - */ |
|
717 | - protected function _clear_cached_properties() |
|
718 | - { |
|
719 | - $this->_cached_properties = []; |
|
720 | - } |
|
721 | - |
|
722 | - |
|
723 | - /** |
|
724 | - * Sets whether or not this model object should be allowed to be saved to the DB. |
|
725 | - * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
726 | - * you got new information that somehow made you change your mind. |
|
727 | - * |
|
728 | - * @param boolean $allow_persist |
|
729 | - * @return boolean |
|
730 | - */ |
|
731 | - public function set_allow_persist($allow_persist) |
|
732 | - { |
|
733 | - return $this->_allow_persist = $allow_persist; |
|
734 | - } |
|
735 | - |
|
736 | - |
|
737 | - /** |
|
738 | - * Gets the field's original value when this object was constructed during this request. |
|
739 | - * This can be helpful when determining if a model object has changed or not |
|
740 | - * |
|
741 | - * @param string $field_name |
|
742 | - * @return mixed|null |
|
743 | - * @throws InvalidArgumentException |
|
744 | - * @throws InvalidInterfaceException |
|
745 | - * @throws InvalidDataTypeException |
|
746 | - * @throws EE_Error |
|
747 | - */ |
|
748 | - public function get_original(string $field_name) |
|
749 | - { |
|
750 | - if ( |
|
751 | - isset($this->_props_n_values_provided_in_constructor[ $field_name ]) |
|
752 | - && $field_settings = $this->_model->field_settings_for($field_name) |
|
753 | - ) { |
|
754 | - return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]); |
|
755 | - } |
|
756 | - return null; |
|
757 | - } |
|
758 | - |
|
759 | - |
|
760 | - /** |
|
761 | - * @param EE_Base_Class $obj |
|
762 | - * @return string |
|
763 | - */ |
|
764 | - public function get_class($obj) |
|
765 | - { |
|
766 | - return get_class($obj); |
|
767 | - } |
|
768 | - |
|
769 | - |
|
770 | - /** |
|
771 | - * Set custom select values for model. |
|
772 | - * |
|
773 | - * @param array $custom_select_values |
|
774 | - */ |
|
775 | - public function setCustomSelectsValues(array $custom_select_values) |
|
776 | - { |
|
777 | - $this->custom_selection_results = $custom_select_values; |
|
778 | - } |
|
779 | - |
|
780 | - |
|
781 | - /** |
|
782 | - * Returns the custom select value for the provided alias if its set. |
|
783 | - * If not set, returns null. |
|
784 | - * |
|
785 | - * @param string $alias |
|
786 | - * @return string|int|float|null |
|
787 | - */ |
|
788 | - public function getCustomSelect($alias) |
|
789 | - { |
|
790 | - return isset($this->custom_selection_results[ $alias ]) |
|
791 | - ? $this->custom_selection_results[ $alias ] |
|
792 | - : null; |
|
793 | - } |
|
794 | - |
|
795 | - |
|
796 | - /** |
|
797 | - * This sets the field value on the db column if it exists for the given $column_name or |
|
798 | - * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
799 | - * |
|
800 | - * @param string $field_name Must be the exact column name. |
|
801 | - * @param mixed $field_value The value to set. |
|
802 | - * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
803 | - * @throws InvalidArgumentException |
|
804 | - * @throws InvalidInterfaceException |
|
805 | - * @throws InvalidDataTypeException |
|
806 | - * @throws EE_Error |
|
807 | - * @throws ReflectionException |
|
808 | - * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
809 | - */ |
|
810 | - public function set_field_or_extra_meta(string $field_name, $field_value) |
|
811 | - { |
|
812 | - if ($this->_model->has_field($field_name)) { |
|
813 | - $this->set($field_name, $field_value); |
|
814 | - return true; |
|
815 | - } |
|
816 | - // ensure this object is saved first so that extra meta can be properly related. |
|
817 | - $this->save(); |
|
818 | - return $this->update_extra_meta($field_name, $field_value); |
|
819 | - } |
|
820 | - |
|
821 | - |
|
822 | - /** |
|
823 | - * Saves this object to the database. An array may be supplied to set some values on this |
|
824 | - * object just before saving. |
|
825 | - * |
|
826 | - * @param array $set_cols_n_values keys are field names, values are their new values, |
|
827 | - * if provided during the save() method |
|
828 | - * (often client code will change the fields' values before calling save) |
|
829 | - * @return boolean|int 1 on a successful update or the ID of the new entry on insert; |
|
830 | - * 0 on failure or if the model object isn't allowed to persist |
|
831 | - * (as determined by EE_Base_Class::allow_persist()) |
|
832 | - * @throws InvalidInterfaceException |
|
833 | - * @throws InvalidDataTypeException |
|
834 | - * @throws EE_Error |
|
835 | - * @throws InvalidArgumentException |
|
836 | - * @throws ReflectionException |
|
837 | - * @throws ReflectionException |
|
838 | - * @throws ReflectionException |
|
839 | - */ |
|
840 | - public function save(array $set_cols_n_values = []) |
|
841 | - { |
|
842 | - /** |
|
843 | - * Filters the fields we're about to save on the model object |
|
844 | - * |
|
845 | - * @param array $set_cols_n_values |
|
846 | - * @param EE_Base_Class $model_object |
|
847 | - */ |
|
848 | - $set_cols_n_values = (array) apply_filters( |
|
849 | - 'FHEE__EE_Base_Class__save__set_cols_n_values', |
|
850 | - $set_cols_n_values, |
|
851 | - $this |
|
852 | - ); |
|
853 | - // set attributes as provided in $set_cols_n_values |
|
854 | - foreach ($set_cols_n_values as $column => $value) { |
|
855 | - $this->set($column, $value); |
|
856 | - } |
|
857 | - // no changes ? then don't do anything |
|
858 | - if (! $this->_has_changes && $this->ID() && $this->_model->get_primary_key_field()->is_auto_increment()) { |
|
859 | - return 0; |
|
860 | - } |
|
861 | - /** |
|
862 | - * Saving a model object. |
|
863 | - * Before we perform a save, this action is fired. |
|
864 | - * |
|
865 | - * @param EE_Base_Class $model_object the model object about to be saved. |
|
866 | - */ |
|
867 | - do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
868 | - if (! $this->allow_persist()) { |
|
869 | - return 0; |
|
870 | - } |
|
871 | - // now get current attribute values |
|
872 | - $save_cols_n_values = $this->_fields; |
|
873 | - // if the object already has an ID, update it. Otherwise, insert it |
|
874 | - // also: change the assumption about values passed to the model NOT being prepare dby the model object. |
|
875 | - // They have been |
|
876 | - $old_assumption_concerning_value_preparation = $this->_model |
|
877 | - ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
878 | - $this->_model->assume_values_already_prepared_by_model_object(true); |
|
879 | - // does this model have an autoincrement PK? |
|
880 | - if ($this->_model->has_primary_key_field()) { |
|
881 | - if ($this->_model->get_primary_key_field()->is_auto_increment()) { |
|
882 | - // ok check if it's set, if so: update; if not, insert |
|
883 | - if (! empty($save_cols_n_values[ $this->_model->primary_key_name() ])) { |
|
884 | - $results = $this->_model->update_by_ID($save_cols_n_values, $this->ID()); |
|
885 | - } else { |
|
886 | - unset($save_cols_n_values[ $this->_model->primary_key_name() ]); |
|
887 | - $results = $this->_model->insert($save_cols_n_values); |
|
888 | - if ($results) { |
|
889 | - // if successful, set the primary key |
|
890 | - // but don't use the normal SET method, because it will check if |
|
891 | - // an item with the same ID exists in the mapper & db, then |
|
892 | - // will find it in the db (because we just added it) and THAT object |
|
893 | - // will get added to the mapper before we can add this one! |
|
894 | - // but if we just avoid using the SET method, all that headache can be avoided |
|
895 | - $pk_field_name = $this->_model->primary_key_name(); |
|
896 | - $this->_fields[ $pk_field_name ] = $results; |
|
897 | - $this->_clear_cached_property($pk_field_name); |
|
898 | - $this->_model->add_to_entity_map($this); |
|
899 | - $this->_update_cached_related_model_objs_fks(); |
|
900 | - } |
|
901 | - } |
|
902 | - } else { |
|
903 | - // PK is NOT auto-increment |
|
904 | - // so check if one like it already exists in the db |
|
905 | - if ($this->_model->exists_by_ID($this->ID())) { |
|
906 | - if (WP_DEBUG && ! $this->in_entity_map()) { |
|
907 | - throw new EE_Error( |
|
908 | - sprintf( |
|
909 | - esc_html__( |
|
910 | - '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', |
|
911 | - 'event_espresso' |
|
912 | - ), |
|
913 | - get_class($this), |
|
914 | - get_class($this->_model) . '::instance()->add_to_entity_map()', |
|
915 | - get_class($this->_model) . '::instance()->get_one_by_ID()', |
|
916 | - '<br />' |
|
917 | - ) |
|
918 | - ); |
|
919 | - } |
|
920 | - $results = $this->_model->update_by_ID($save_cols_n_values, $this->ID()); |
|
921 | - } else { |
|
922 | - $results = $this->_model->insert($save_cols_n_values); |
|
923 | - $this->_update_cached_related_model_objs_fks(); |
|
924 | - } |
|
925 | - } |
|
926 | - } else { |
|
927 | - // there is NO primary key |
|
928 | - $already_in_db = false; |
|
929 | - foreach ($this->_model->unique_indexes() as $index) { |
|
930 | - $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
931 | - if ($this->_model->exists([$uniqueness_where_params])) { |
|
932 | - $already_in_db = true; |
|
933 | - } |
|
934 | - } |
|
935 | - if ($already_in_db) { |
|
936 | - $combined_pk_fields_n_values = array_intersect_key( |
|
937 | - $save_cols_n_values, |
|
938 | - $this->_model->get_combined_primary_key_fields() |
|
939 | - ); |
|
940 | - $results = $this->_model->update( |
|
941 | - $save_cols_n_values, |
|
942 | - $combined_pk_fields_n_values |
|
943 | - ); |
|
944 | - } else { |
|
945 | - $results = $this->_model->insert($save_cols_n_values); |
|
946 | - } |
|
947 | - } |
|
948 | - // restore the old assumption about values being prepared by the model object |
|
949 | - $this->_model->assume_values_already_prepared_by_model_object( |
|
950 | - $old_assumption_concerning_value_preparation |
|
951 | - ); |
|
952 | - /** |
|
953 | - * After saving the model object this action is called |
|
954 | - * |
|
955 | - * @param EE_Base_Class $model_object which was just saved |
|
956 | - * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
957 | - * the new ID (or 0 if an error occurred and it wasn't updated) |
|
958 | - */ |
|
959 | - do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
960 | - $this->_has_changes = false; |
|
961 | - return $results; |
|
962 | - } |
|
963 | - |
|
964 | - |
|
965 | - /** |
|
966 | - * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
967 | - * A $previous_value can be specified in case there are many meta rows with the same key |
|
968 | - * |
|
969 | - * @param string $meta_key |
|
970 | - * @param mixed $meta_value |
|
971 | - * @param mixed $previous_value |
|
972 | - * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
973 | - * NOTE: if the values haven't changed, returns 0 |
|
974 | - * @throws InvalidArgumentException |
|
975 | - * @throws InvalidInterfaceException |
|
976 | - * @throws InvalidDataTypeException |
|
977 | - * @throws EE_Error |
|
978 | - * @throws ReflectionException |
|
979 | - */ |
|
980 | - public function update_extra_meta(string $meta_key, $meta_value, $previous_value = null) |
|
981 | - { |
|
982 | - $query_params = [ |
|
983 | - [ |
|
984 | - 'EXM_key' => $meta_key, |
|
985 | - 'OBJ_ID' => $this->ID(), |
|
986 | - 'EXM_type' => $this->_model->get_this_model_name(), |
|
987 | - ], |
|
988 | - ]; |
|
989 | - if ($previous_value !== null) { |
|
990 | - $query_params[0]['EXM_value'] = $meta_value; |
|
991 | - } |
|
992 | - $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
993 | - if (! $existing_rows_like_that) { |
|
994 | - return $this->add_extra_meta($meta_key, $meta_value); |
|
995 | - } |
|
996 | - foreach ($existing_rows_like_that as $existing_row) { |
|
997 | - $existing_row->save(['EXM_value' => $meta_value]); |
|
998 | - } |
|
999 | - return count($existing_rows_like_that); |
|
1000 | - } |
|
1001 | - |
|
1002 | - |
|
1003 | - /** |
|
1004 | - * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
1005 | - * |
|
1006 | - * @return boolean |
|
1007 | - */ |
|
1008 | - public function allow_persist() |
|
1009 | - { |
|
1010 | - return $this->_allow_persist; |
|
1011 | - } |
|
1012 | - |
|
1013 | - |
|
1014 | - /** |
|
1015 | - * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
1016 | - * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
1017 | - * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
1018 | - * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
1019 | - * 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 |
|
1020 | - * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
1021 | - * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
1022 | - * |
|
1023 | - * @return void |
|
1024 | - * @throws ReflectionException |
|
1025 | - * @throws InvalidArgumentException |
|
1026 | - * @throws InvalidInterfaceException |
|
1027 | - * @throws InvalidDataTypeException |
|
1028 | - * @throws EE_Error |
|
1029 | - */ |
|
1030 | - protected function _update_cached_related_model_objs_fks() |
|
1031 | - { |
|
1032 | - foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
1033 | - if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
1034 | - foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
1035 | - $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
1036 | - $this->_model->get_this_model_name() |
|
1037 | - ); |
|
1038 | - $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
1039 | - if ($related_model_obj_in_cache->ID()) { |
|
1040 | - $related_model_obj_in_cache->save(); |
|
1041 | - } |
|
1042 | - } |
|
1043 | - } |
|
1044 | - } |
|
1045 | - } |
|
1046 | - |
|
1047 | - |
|
1048 | - /** |
|
1049 | - * in_entity_map |
|
1050 | - * Checks if this model object has been proven to already be in the entity map |
|
1051 | - * |
|
1052 | - * @return boolean |
|
1053 | - * @throws InvalidArgumentException |
|
1054 | - * @throws InvalidInterfaceException |
|
1055 | - * @throws InvalidDataTypeException |
|
1056 | - * @throws EE_Error |
|
1057 | - */ |
|
1058 | - public function in_entity_map() |
|
1059 | - { |
|
1060 | - // well, if we looked, did we find it in the entity map? |
|
1061 | - return $this->ID() && $this->_model->get_from_entity_map($this->ID()) === $this; |
|
1062 | - } |
|
1063 | - |
|
1064 | - |
|
1065 | - /** |
|
1066 | - * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
1067 | - * no other extra meta for this model object have the same key. Returns TRUE if the |
|
1068 | - * extra meta row was entered, false if not |
|
1069 | - * |
|
1070 | - * @param string $meta_key |
|
1071 | - * @param mixed $meta_value |
|
1072 | - * @param boolean $unique |
|
1073 | - * @return boolean |
|
1074 | - * @throws InvalidArgumentException |
|
1075 | - * @throws InvalidInterfaceException |
|
1076 | - * @throws InvalidDataTypeException |
|
1077 | - * @throws EE_Error |
|
1078 | - * @throws ReflectionException |
|
1079 | - * @throws ReflectionException |
|
1080 | - */ |
|
1081 | - public function add_extra_meta(string $meta_key, $meta_value, bool $unique = false): bool |
|
1082 | - { |
|
1083 | - if ($unique) { |
|
1084 | - $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
1085 | - [ |
|
1086 | - [ |
|
1087 | - 'EXM_key' => $meta_key, |
|
1088 | - 'OBJ_ID' => $this->ID(), |
|
1089 | - 'EXM_type' => $this->_model->get_this_model_name(), |
|
1090 | - ], |
|
1091 | - ] |
|
1092 | - ); |
|
1093 | - if ($existing_extra_meta) { |
|
1094 | - return false; |
|
1095 | - } |
|
1096 | - } |
|
1097 | - $new_extra_meta = EE_Extra_Meta::new_instance( |
|
1098 | - [ |
|
1099 | - 'EXM_key' => $meta_key, |
|
1100 | - 'EXM_value' => $meta_value, |
|
1101 | - 'OBJ_ID' => $this->ID(), |
|
1102 | - 'EXM_type' => $this->_model->get_this_model_name(), |
|
1103 | - ] |
|
1104 | - ); |
|
1105 | - $new_extra_meta->save(); |
|
1106 | - return true; |
|
1107 | - } |
|
1108 | - |
|
1109 | - |
|
1110 | - /** |
|
1111 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
1112 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
1113 | - * |
|
1114 | - * @param string $relationName |
|
1115 | - * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
1116 | - * @throws InvalidArgumentException |
|
1117 | - * @throws InvalidInterfaceException |
|
1118 | - * @throws InvalidDataTypeException |
|
1119 | - * @throws EE_Error |
|
1120 | - * @throws ReflectionException |
|
1121 | - */ |
|
1122 | - public function get_all_from_cache($relationName) |
|
1123 | - { |
|
1124 | - $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : []; |
|
1125 | - // if the result is not an array, but exists, make it an array |
|
1126 | - $objects = is_array($objects) ? $objects : [$objects]; |
|
1127 | - // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
1128 | - // basically, if this model object was stored in the session, and these cached model objects |
|
1129 | - // already have IDs, let's make sure they're in their model's entity mapper |
|
1130 | - // otherwise we will have duplicates next time we call |
|
1131 | - // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
1132 | - $related_model = EE_Registry::instance()->load_model($relationName); |
|
1133 | - foreach ($objects as $model_object) { |
|
1134 | - if ($related_model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
1135 | - // ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
1136 | - if ($model_object->ID()) { |
|
1137 | - $related_model->add_to_entity_map($model_object); |
|
1138 | - } |
|
1139 | - } else { |
|
1140 | - throw new EE_Error( |
|
1141 | - sprintf( |
|
1142 | - esc_html__( |
|
1143 | - 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
1144 | - 'event_espresso' |
|
1145 | - ), |
|
1146 | - $relationName, |
|
1147 | - gettype($model_object) |
|
1148 | - ) |
|
1149 | - ); |
|
1150 | - } |
|
1151 | - } |
|
1152 | - $this->updateTimezoneOnRelated($objects); |
|
1153 | - return $objects; |
|
1154 | - } |
|
1155 | - |
|
1156 | - |
|
1157 | - /** |
|
1158 | - * This retrieves the value of the db column set on this class or if that's not present |
|
1159 | - * it will attempt to retrieve from extra_meta if found. |
|
1160 | - * Example Usage: |
|
1161 | - * Via EE_Message child class: |
|
1162 | - * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
1163 | - * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
1164 | - * also have additional main fields specific to the messenger. The system accommodates those extra |
|
1165 | - * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
1166 | - * value for those extra fields dynamically via the EE_message object. |
|
1167 | - * |
|
1168 | - * @param string $field_name expecting the fully qualified field name. |
|
1169 | - * @return mixed|null value for the field if found. null if not found. |
|
1170 | - * @throws ReflectionException |
|
1171 | - * @throws InvalidArgumentException |
|
1172 | - * @throws InvalidInterfaceException |
|
1173 | - * @throws InvalidDataTypeException |
|
1174 | - * @throws EE_Error |
|
1175 | - */ |
|
1176 | - public function get_field_or_extra_meta(string $field_name) |
|
1177 | - { |
|
1178 | - // if this isn't a column in the main table, then see if it is in the extra meta. |
|
1179 | - return $this->_model->has_field($field_name) |
|
1180 | - ? $this->get($field_name) |
|
1181 | - : $this->get_extra_meta($field_name, true); |
|
1182 | - } |
|
1183 | - |
|
1184 | - |
|
1185 | - /** |
|
1186 | - * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
1187 | - * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
1188 | - * You can specify $default is case you haven't found the extra meta |
|
1189 | - * |
|
1190 | - * @param string $meta_key |
|
1191 | - * @param boolean $single |
|
1192 | - * @param mixed $default if we don't find anything, what should we return? |
|
1193 | - * @return mixed single value if $single; array if ! $single |
|
1194 | - * @throws ReflectionException |
|
1195 | - * @throws InvalidArgumentException |
|
1196 | - * @throws InvalidInterfaceException |
|
1197 | - * @throws InvalidDataTypeException |
|
1198 | - * @throws EE_Error |
|
1199 | - */ |
|
1200 | - public function get_extra_meta(string $meta_key, bool $single = false, $default = null) |
|
1201 | - { |
|
1202 | - if ($single) { |
|
1203 | - $result = $this->get_first_related( |
|
1204 | - 'Extra_Meta', |
|
1205 | - [['EXM_key' => $meta_key]] |
|
1206 | - ); |
|
1207 | - if ($result instanceof EE_Extra_Meta) { |
|
1208 | - return $result->value(); |
|
1209 | - } |
|
1210 | - } else { |
|
1211 | - $results = $this->get_many_related( |
|
1212 | - 'Extra_Meta', |
|
1213 | - [['EXM_key' => $meta_key]] |
|
1214 | - ); |
|
1215 | - if ($results) { |
|
1216 | - $values = []; |
|
1217 | - foreach ($results as $result) { |
|
1218 | - if ($result instanceof EE_Extra_Meta) { |
|
1219 | - $values[ $result->ID() ] = $result->value(); |
|
1220 | - } |
|
1221 | - } |
|
1222 | - return $values; |
|
1223 | - } |
|
1224 | - } |
|
1225 | - // if nothing discovered yet return default. |
|
1226 | - return apply_filters( |
|
1227 | - 'FHEE__EE_Base_Class__get_extra_meta__default_value', |
|
1228 | - $default, |
|
1229 | - $meta_key, |
|
1230 | - $single, |
|
1231 | - $this |
|
1232 | - ); |
|
1233 | - } |
|
1234 | - |
|
1235 | - |
|
1236 | - /** |
|
1237 | - * Gets the first (ie, one) related model object of the specified type. |
|
1238 | - * |
|
1239 | - * @param string $relationName key in the model's _model_relations array |
|
1240 | - * @param array $query_params |
|
1241 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1242 | - * @return EE_Base_Class (not an array, a single object) |
|
1243 | - * @throws ReflectionException |
|
1244 | - * @throws InvalidArgumentException |
|
1245 | - * @throws InvalidInterfaceException |
|
1246 | - * @throws InvalidDataTypeException |
|
1247 | - * @throws EE_Error |
|
1248 | - */ |
|
1249 | - public function get_first_related($relationName, $query_params = []) |
|
1250 | - { |
|
1251 | - $model_relation = $this->_model->related_settings_for($relationName); |
|
1252 | - if (! $this->ID()) { |
|
1253 | - // this doesn't exist in the DB, |
|
1254 | - // but maybe the relation type is "belongs to" and the related object might |
|
1255 | - if ($model_relation instanceof EE_Belongs_To_Relation) { |
|
16 | + /** |
|
17 | + * @var boolean indicating whether or not this model object is intended to ever be saved |
|
18 | + * For example, we might create model objects intended to only be used for the duration |
|
19 | + * of this request and to be thrown away, and if they were accidentally saved |
|
20 | + * it would be a bug. |
|
21 | + */ |
|
22 | + protected $_allow_persist = true; |
|
23 | + |
|
24 | + /** |
|
25 | + * This property is for holding a cached array of object properties indexed by property name as the key. |
|
26 | + * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
27 | + * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
28 | + * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
29 | + * |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $_cached_properties = []; |
|
33 | + |
|
34 | + /** |
|
35 | + * This is a cache of results from custom selections done on a query that constructs this entity. The only purpose |
|
36 | + * for these values is for retrieval of the results, they are not further queryable and they are not persisted to |
|
37 | + * the db. They also do not automatically update if there are any changes to the data that produced their results. |
|
38 | + * The format is a simple array of field_alias => field_value. So for instance if a custom select was something |
|
39 | + * like, "Select COUNT(Registration.REG_ID) as Registration_Count ...", then the resulting value will be in this |
|
40 | + * array as: |
|
41 | + * array( |
|
42 | + * 'Registration_Count' => 24 |
|
43 | + * ); |
|
44 | + * Note: if the custom select configuration for the query included a data type, the value will be in the data type |
|
45 | + * provided for the query (@see EventEspresso\core\domain\values\model\CustomSelects::__construct phpdocs for more |
|
46 | + * info) |
|
47 | + * |
|
48 | + * @var array |
|
49 | + */ |
|
50 | + protected $custom_selection_results = []; |
|
51 | + |
|
52 | + /** |
|
53 | + * date format |
|
54 | + * pattern or format for displaying dates |
|
55 | + * |
|
56 | + * @var string $_dt_frmt |
|
57 | + */ |
|
58 | + protected $_dt_frmt; |
|
59 | + |
|
60 | + /** |
|
61 | + * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
62 | + * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
63 | + * |
|
64 | + * @var array |
|
65 | + */ |
|
66 | + protected $_fields = []; |
|
67 | + |
|
68 | + /** |
|
69 | + * @var boolean indicating whether or not this model object's properties have changed since construction |
|
70 | + */ |
|
71 | + protected $_has_changes = false; |
|
72 | + |
|
73 | + /** |
|
74 | + * @var EEM_Base |
|
75 | + */ |
|
76 | + protected $_model; |
|
77 | + |
|
78 | + |
|
79 | + /** |
|
80 | + * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
81 | + * single |
|
82 | + * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
83 | + * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
84 | + * all others have an array) |
|
85 | + * |
|
86 | + * @var array |
|
87 | + */ |
|
88 | + protected $_model_relations = []; |
|
89 | + |
|
90 | + /** |
|
91 | + * This is an array of the original properties and values provided during construction |
|
92 | + * of this model object. (keys are model field names, values are their values). |
|
93 | + * This list is important to remember so that when we are merging data from the db, we know |
|
94 | + * which values to override and which to not override. |
|
95 | + * |
|
96 | + * @var array |
|
97 | + */ |
|
98 | + protected $_props_n_values_provided_in_constructor; |
|
99 | + |
|
100 | + /** |
|
101 | + * Timezone |
|
102 | + * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
103 | + * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
104 | + * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
105 | + * access to it. |
|
106 | + * |
|
107 | + * @var string |
|
108 | + */ |
|
109 | + protected $_timezone = ''; |
|
110 | + |
|
111 | + /** |
|
112 | + * time format |
|
113 | + * pattern or format for displaying time |
|
114 | + * |
|
115 | + * @var string $_tm_frmt |
|
116 | + */ |
|
117 | + protected $_tm_frmt; |
|
118 | + |
|
119 | + |
|
120 | + /** |
|
121 | + * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
122 | + * play nice |
|
123 | + * |
|
124 | + * @param array $field_values where each key is a field (ie, array key in the 2nd |
|
125 | + * layer of the model's _fields array, (eg, EVT_ID, |
|
126 | + * TXN_amount, QST_name, etc) and values are their values |
|
127 | + * @param bool $set_from_db a flag for setting if the class is instantiated by the |
|
128 | + * corresponding db model or not. |
|
129 | + * @param string $timezone indicate what timezone you want any datetime fields to |
|
130 | + * be in when instantiating a EE_Base_Class object. |
|
131 | + * @param array $date_formats An array of date formats to set on construct where first |
|
132 | + * value is the date_format and second value is the time |
|
133 | + * format. |
|
134 | + * @throws InvalidArgumentException |
|
135 | + * @throws InvalidInterfaceException |
|
136 | + * @throws InvalidDataTypeException |
|
137 | + * @throws EE_Error |
|
138 | + * @throws ReflectionException |
|
139 | + */ |
|
140 | + protected function __construct( |
|
141 | + array $field_values = [], |
|
142 | + bool $set_from_db = false, |
|
143 | + string $timezone = '', |
|
144 | + array $date_formats = [] |
|
145 | + ) { |
|
146 | + // ensure $fieldValues is an array |
|
147 | + $field_values = is_array($field_values) ? $field_values : [$field_values]; |
|
148 | + $className = get_class($this); |
|
149 | + do_action("AHEE__{$className}__construct", $this, $field_values); |
|
150 | + // remember what values were passed to this constructor |
|
151 | + $this->_props_n_values_provided_in_constructor = $field_values; |
|
152 | + $this->setDateAndTimeFormats($date_formats); |
|
153 | + $this->_model = $this->get_model($timezone); |
|
154 | + $model_fields = $this->_model->field_settings(); |
|
155 | + $this->validateFieldValues($model_fields, $field_values); |
|
156 | + $this->setFieldValues($model_fields, $field_values, $set_from_db); |
|
157 | + // remember in entity mapper |
|
158 | + if (! $set_from_db && $this->_model->has_primary_key_field() && $this->ID()) { |
|
159 | + $this->_model->add_to_entity_map($this); |
|
160 | + } |
|
161 | + // setup all the relations |
|
162 | + foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
163 | + $this->_model_relations[ $relation_name ] = $relation_obj instanceof EE_Belongs_To_Relation |
|
164 | + ? null |
|
165 | + : []; |
|
166 | + } |
|
167 | + /** |
|
168 | + * Action done at the end of each model object construction |
|
169 | + * |
|
170 | + * @param EE_Base_Class $this the model object just created |
|
171 | + */ |
|
172 | + do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
173 | + } |
|
174 | + |
|
175 | + |
|
176 | + /** |
|
177 | + * for getting a model while instantiated. |
|
178 | + * |
|
179 | + * @param string $timezone |
|
180 | + * @return EEM_Base | EEM_CPT_Base |
|
181 | + * @throws EE_Error |
|
182 | + * @throws ReflectionException |
|
183 | + */ |
|
184 | + public function get_model(string $timezone = '') |
|
185 | + { |
|
186 | + if (! $this->_model) { |
|
187 | + $timezone = $timezone ?? $this->_timezone; |
|
188 | + $modelName = self::_get_model_classname(get_class($this)); |
|
189 | + $this->_model = self::_get_model_instance_with_name($modelName, $timezone); |
|
190 | + $this->set_timezone($timezone); |
|
191 | + } |
|
192 | + return $this->_model; |
|
193 | + } |
|
194 | + |
|
195 | + |
|
196 | + /** |
|
197 | + * Overrides parent because parent expects old models. |
|
198 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
199 | + * |
|
200 | + * @param string $field_name |
|
201 | + * @param mixed $field_value_from_db |
|
202 | + * @throws InvalidArgumentException |
|
203 | + * @throws InvalidInterfaceException |
|
204 | + * @throws InvalidDataTypeException |
|
205 | + * @throws EE_Error |
|
206 | + */ |
|
207 | + public function set_from_db(string $field_name, $field_value_from_db) |
|
208 | + { |
|
209 | + $field_obj = $this->_model->field_settings_for($field_name); |
|
210 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
211 | + // you would think the DB has no NULLs for non-null label fields right? wrong! |
|
212 | + // eg, a CPT model object could have an entry in the posts table, but no |
|
213 | + // entry in the meta table. Meaning that all its columns in the meta table |
|
214 | + // are null! yikes! so when we find one like that, use defaults for its meta columns |
|
215 | + |
|
216 | + if ($field_value_from_db === null) { |
|
217 | + if ($field_obj->is_nullable()) { |
|
218 | + // if the field allows nulls, then let it be null |
|
219 | + $field_value = null; |
|
220 | + } else { |
|
221 | + $field_value = $field_obj->get_default_value(); |
|
222 | + } |
|
223 | + } else { |
|
224 | + $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
225 | + } |
|
226 | + $this->_fields[ $field_name ] = $field_value; |
|
227 | + $this->_clear_cached_property($field_name); |
|
228 | + } |
|
229 | + } |
|
230 | + |
|
231 | + |
|
232 | + /** |
|
233 | + * Overrides parent because parent expects old models. |
|
234 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
235 | + * |
|
236 | + * @param string $field_name |
|
237 | + * @param mixed $field_value |
|
238 | + * @param bool $use_default |
|
239 | + * @throws InvalidArgumentException |
|
240 | + * @throws InvalidInterfaceException |
|
241 | + * @throws InvalidDataTypeException |
|
242 | + * @throws EE_Error |
|
243 | + * @throws ReflectionException |
|
244 | + * @throws ReflectionException |
|
245 | + * @throws ReflectionException |
|
246 | + */ |
|
247 | + public function set(string $field_name, $field_value, bool $use_default = false) |
|
248 | + { |
|
249 | + // if not using default and nothing has changed, and object has already been setup (has ID), |
|
250 | + // then don't do anything |
|
251 | + if ( |
|
252 | + ! $use_default |
|
253 | + && $this->_fields[ $field_name ] === $field_value |
|
254 | + && $this->ID() |
|
255 | + ) { |
|
256 | + return; |
|
257 | + } |
|
258 | + $this->_has_changes = true; |
|
259 | + $field_obj = $this->_model->field_settings_for($field_name); |
|
260 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
261 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
262 | + $field_obj->set_timezone($this->_timezone); |
|
263 | + $field_obj->set_date_format($this->_dt_frmt); |
|
264 | + $field_obj->set_time_format($this->_tm_frmt); |
|
265 | + } |
|
266 | + $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
267 | + // should the value be null? |
|
268 | + if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
269 | + $this->_fields[ $field_name ] = $field_obj->get_default_value(); |
|
270 | + /** |
|
271 | + * To save having to refactor all the models, if a default value is used for a |
|
272 | + * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
273 | + * object. Then let's do a set again to ensure that it becomes a DateTime |
|
274 | + * object. |
|
275 | + * |
|
276 | + * @since 4.6.10+ |
|
277 | + */ |
|
278 | + if ( |
|
279 | + $field_obj instanceof EE_Datetime_Field |
|
280 | + && $this->_fields[ $field_name ] !== null |
|
281 | + && ! $this->_fields[ $field_name ] instanceof DateTime |
|
282 | + ) { |
|
283 | + empty($this->_fields[ $field_name ]) |
|
284 | + ? $this->set($field_name, time()) |
|
285 | + : $this->set($field_name, $this->_fields[ $field_name ]); |
|
286 | + } |
|
287 | + } else { |
|
288 | + $this->_fields[ $field_name ] = $holder_of_value; |
|
289 | + } |
|
290 | + // if we're not in the constructor... |
|
291 | + // now check if what we set was a primary key |
|
292 | + if ( |
|
293 | + // note: props_n_values_provided_in_constructor is only set at the END of the constructor |
|
294 | + $this->_props_n_values_provided_in_constructor |
|
295 | + && $field_value |
|
296 | + && $field_name === $this->_model->primary_key_name() |
|
297 | + ) { |
|
298 | + // if so, we want all this object's fields to be filled either with |
|
299 | + // what we've explicitly set on this model |
|
300 | + // or what we have in the db |
|
301 | + // echo "setting primary key!"; |
|
302 | + $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
303 | + $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
304 | + foreach ($fields_on_model as $field_obj) { |
|
305 | + if ( |
|
306 | + ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
307 | + && $field_obj->get_name() !== $field_name |
|
308 | + ) { |
|
309 | + $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
310 | + } |
|
311 | + } |
|
312 | + // oh this model object has an ID? well make sure its in the entity mapper |
|
313 | + $this->_model->add_to_entity_map($this); |
|
314 | + } |
|
315 | + // let's unset any cache for this field_name from the $_cached_properties property. |
|
316 | + $this->_clear_cached_property($field_name); |
|
317 | + } else { |
|
318 | + throw new EE_Error( |
|
319 | + sprintf( |
|
320 | + esc_html__( |
|
321 | + 'A valid EE_Model_Field_Base could not be found for the given field name: %s', |
|
322 | + 'event_espresso' |
|
323 | + ), |
|
324 | + $field_name |
|
325 | + ) |
|
326 | + ); |
|
327 | + } |
|
328 | + } |
|
329 | + |
|
330 | + |
|
331 | + /** |
|
332 | + * Gets the value of the primary key. |
|
333 | + * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
334 | + * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
335 | + * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
336 | + * |
|
337 | + * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
338 | + * @throws InvalidArgumentException |
|
339 | + * @throws InvalidInterfaceException |
|
340 | + * @throws InvalidDataTypeException |
|
341 | + * @throws EE_Error |
|
342 | + */ |
|
343 | + public function ID() |
|
344 | + { |
|
345 | + // now that we know the name of the variable, use a variable variable to get its value and return its |
|
346 | + if ($this->_model->has_primary_key_field()) { |
|
347 | + return $this->_fields[ $this->_model->primary_key_name() ]; |
|
348 | + } |
|
349 | + return $this->_model->get_index_primary_key_string($this->_fields); |
|
350 | + } |
|
351 | + |
|
352 | + |
|
353 | + /** |
|
354 | + * If a model name is provided (eg Registration), gets the model classname for that model. |
|
355 | + * Also works if a model class's classname is provided (eg EE_Registration). |
|
356 | + * |
|
357 | + * @param string $model_name |
|
358 | + * @return string like EEM_Attendee |
|
359 | + */ |
|
360 | + private static function _get_model_classname(string $model_name = ''): string |
|
361 | + { |
|
362 | + return strpos($model_name, 'EE_') === 0 |
|
363 | + ? str_replace('EE_', 'EEM_', $model_name) |
|
364 | + : 'EEM_' . $model_name; |
|
365 | + } |
|
366 | + |
|
367 | + |
|
368 | + /** |
|
369 | + * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
370 | + * |
|
371 | + * @param string $model_classname |
|
372 | + * @param string $timezone |
|
373 | + * @return EEM_Base |
|
374 | + * @throws ReflectionException |
|
375 | + * @throws InvalidArgumentException |
|
376 | + * @throws InvalidInterfaceException |
|
377 | + * @throws InvalidDataTypeException |
|
378 | + * @throws EE_Error |
|
379 | + */ |
|
380 | + protected static function _get_model_instance_with_name(string $model_classname, string $timezone = ''): EEM_Base |
|
381 | + { |
|
382 | + $model_classname = str_replace('EEM_', '', $model_classname); |
|
383 | + $model = EE_Registry::instance()->load_model($model_classname); |
|
384 | + $model->set_timezone($timezone); |
|
385 | + return $model; |
|
386 | + } |
|
387 | + |
|
388 | + |
|
389 | + /** |
|
390 | + * This just clears out ONE property if it exists in the cache |
|
391 | + * |
|
392 | + * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
393 | + * @return void |
|
394 | + */ |
|
395 | + protected function _clear_cached_property(string $property_name) |
|
396 | + { |
|
397 | + unset($this->_cached_properties[ $property_name ]); |
|
398 | + } |
|
399 | + |
|
400 | + |
|
401 | + /** |
|
402 | + * Gets the EEM_*_Model for this class |
|
403 | + * |
|
404 | + * @param string $classname |
|
405 | + * @param string $timezone |
|
406 | + * @return EEM_Base |
|
407 | + * @throws InvalidArgumentException |
|
408 | + * @throws InvalidInterfaceException |
|
409 | + * @throws InvalidDataTypeException |
|
410 | + * @throws EE_Error |
|
411 | + * @throws ReflectionException |
|
412 | + */ |
|
413 | + protected static function _get_model(string $classname, string $timezone = ''): EEM_Base |
|
414 | + { |
|
415 | + // find model for this class |
|
416 | + if (! $classname) { |
|
417 | + throw new EE_Error( |
|
418 | + sprintf( |
|
419 | + esc_html__( |
|
420 | + 'What were you thinking calling _get_model(%s)?? You need to specify the class name', |
|
421 | + 'event_espresso' |
|
422 | + ), |
|
423 | + $classname |
|
424 | + ) |
|
425 | + ); |
|
426 | + } |
|
427 | + $modelName = self::_get_model_classname($classname); |
|
428 | + return self::_get_model_instance_with_name($modelName, $timezone); |
|
429 | + } |
|
430 | + |
|
431 | + |
|
432 | + /** |
|
433 | + * verifies that the specified field is of the correct type |
|
434 | + * |
|
435 | + * @param string $field_name |
|
436 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
437 | + * (in cases where the same property may be used for different outputs |
|
438 | + * - i.e. datetime, money etc.) |
|
439 | + * @return mixed |
|
440 | + * @throws InvalidArgumentException |
|
441 | + * @throws InvalidInterfaceException |
|
442 | + * @throws InvalidDataTypeException |
|
443 | + * @throws EE_Error |
|
444 | + */ |
|
445 | + public function get(string $field_name, string $extra_cache_ref = '') |
|
446 | + { |
|
447 | + return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
448 | + } |
|
449 | + |
|
450 | + |
|
451 | + /** |
|
452 | + * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
453 | + * This also SETS the cache if we return the actual property! |
|
454 | + * |
|
455 | + * @param string $field_name the name of the property we're trying to retrieve |
|
456 | + * @param bool $pretty |
|
457 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
458 | + * (in cases where the same property may be used for different outputs |
|
459 | + * - i.e. datetime, money etc.) |
|
460 | + * It can also accept certain pre-defined "schema" strings |
|
461 | + * to define how to output the property. |
|
462 | + * see the field's prepare_for_pretty_echoing for what strings can be used |
|
463 | + * @return mixed whatever the value for the property is we're retrieving |
|
464 | + * @throws InvalidArgumentException |
|
465 | + * @throws InvalidInterfaceException |
|
466 | + * @throws InvalidDataTypeException |
|
467 | + * @throws EE_Error |
|
468 | + */ |
|
469 | + protected function _get_cached_property(string $field_name, bool $pretty = false, string $extra_cache_ref = '') |
|
470 | + { |
|
471 | + // verify the field exists |
|
472 | + $this->_model->field_settings_for($field_name); |
|
473 | + $cache_type = $pretty ? 'pretty' : 'standard'; |
|
474 | + $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
475 | + if (isset($this->_cached_properties[ $field_name ][ $cache_type ])) { |
|
476 | + return $this->_cached_properties[ $field_name ][ $cache_type ]; |
|
477 | + } |
|
478 | + $value = $this->_get_fresh_property($field_name, $pretty, $extra_cache_ref); |
|
479 | + $this->_set_cached_property($field_name, $value, $cache_type); |
|
480 | + return $value; |
|
481 | + } |
|
482 | + |
|
483 | + |
|
484 | + /** |
|
485 | + * If the cache didn't fetch the needed item, this fetches it. |
|
486 | + * |
|
487 | + * @param string $field_name |
|
488 | + * @param bool $pretty |
|
489 | + * @param string $extra_cache_ref |
|
490 | + * @return mixed |
|
491 | + * @throws InvalidArgumentException |
|
492 | + * @throws InvalidInterfaceException |
|
493 | + * @throws InvalidDataTypeException |
|
494 | + * @throws EE_Error |
|
495 | + */ |
|
496 | + protected function _get_fresh_property(string $field_name, bool $pretty = false, string $extra_cache_ref = '') |
|
497 | + { |
|
498 | + $field_obj = $this->_model->field_settings_for($field_name); |
|
499 | + // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
500 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
501 | + $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
502 | + } |
|
503 | + if (! isset($this->_fields[ $field_name ])) { |
|
504 | + $this->_fields[ $field_name ] = null; |
|
505 | + } |
|
506 | + return $pretty |
|
507 | + ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $field_name ], $extra_cache_ref) |
|
508 | + : $field_obj->prepare_for_get($this->_fields[ $field_name ]); |
|
509 | + } |
|
510 | + |
|
511 | + |
|
512 | + /** |
|
513 | + * For adding an item to the cached_properties property. |
|
514 | + * |
|
515 | + * @param string $field_name the property item the corresponding value is for. |
|
516 | + * @param mixed $value The value we are caching. |
|
517 | + * @param string $cache_type |
|
518 | + * @return void |
|
519 | + * @throws InvalidArgumentException |
|
520 | + * @throws InvalidInterfaceException |
|
521 | + * @throws InvalidDataTypeException |
|
522 | + * @throws EE_Error |
|
523 | + */ |
|
524 | + protected function _set_cached_property(string $field_name, $value, string $cache_type = '') |
|
525 | + { |
|
526 | + // first make sure this property exists |
|
527 | + $this->_model->field_settings_for($field_name); |
|
528 | + $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
529 | + $this->_cached_properties[ $field_name ][ $cache_type ] = $value; |
|
530 | + } |
|
531 | + |
|
532 | + |
|
533 | + /** |
|
534 | + * set timezone, formats, and output for EE_Datetime_Field objects |
|
535 | + * |
|
536 | + * @param EE_Datetime_Field $datetime_field |
|
537 | + * @param bool $pretty |
|
538 | + * @param null $date_or_time |
|
539 | + * @return void |
|
540 | + * @throws InvalidArgumentException |
|
541 | + * @throws InvalidInterfaceException |
|
542 | + * @throws InvalidDataTypeException |
|
543 | + */ |
|
544 | + protected function _prepare_datetime_field( |
|
545 | + EE_Datetime_Field $datetime_field, |
|
546 | + bool $pretty = false, |
|
547 | + $date_or_time = null |
|
548 | + ) { |
|
549 | + $datetime_field->set_timezone($this->_timezone); |
|
550 | + $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
551 | + $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
552 | + // set the output returned |
|
553 | + switch ($date_or_time) { |
|
554 | + case 'D': |
|
555 | + $datetime_field->set_date_time_output('date'); |
|
556 | + break; |
|
557 | + case 'T': |
|
558 | + $datetime_field->set_date_time_output('time'); |
|
559 | + break; |
|
560 | + default: |
|
561 | + $datetime_field->set_date_time_output(); |
|
562 | + } |
|
563 | + } |
|
564 | + |
|
565 | + |
|
566 | + /** |
|
567 | + * @param $props_n_values |
|
568 | + * @param $classname |
|
569 | + * @return EE_Base_Class|null |
|
570 | + * @throws ReflectionException |
|
571 | + * @throws InvalidArgumentException |
|
572 | + * @throws InvalidInterfaceException |
|
573 | + * @throws InvalidDataTypeException |
|
574 | + * @throws EE_Error |
|
575 | + * @throws ReflectionException |
|
576 | + */ |
|
577 | + protected static function _get_object_from_entity_mapper($props_n_values, $classname): ?EE_Base_Class |
|
578 | + { |
|
579 | + // TODO: will not work for Term_Relationships because they have no PK! |
|
580 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
581 | + if ( |
|
582 | + array_key_exists($primary_id_ref, $props_n_values) |
|
583 | + && ! empty($props_n_values[ $primary_id_ref ]) |
|
584 | + ) { |
|
585 | + $id = $props_n_values[ $primary_id_ref ]; |
|
586 | + return self::_get_model($classname)->get_from_entity_map($id); |
|
587 | + } |
|
588 | + return null; |
|
589 | + } |
|
590 | + |
|
591 | + |
|
592 | + /** |
|
593 | + * returns the name of the primary key attribute |
|
594 | + * |
|
595 | + * @param null $classname |
|
596 | + * @return string |
|
597 | + * @throws InvalidArgumentException |
|
598 | + * @throws InvalidInterfaceException |
|
599 | + * @throws InvalidDataTypeException |
|
600 | + * @throws EE_Error |
|
601 | + * @throws ReflectionException |
|
602 | + * @throws ReflectionException |
|
603 | + * @throws ReflectionException |
|
604 | + */ |
|
605 | + protected static function _get_primary_key_name($classname = null): string |
|
606 | + { |
|
607 | + if (! $classname) { |
|
608 | + throw new EE_Error( |
|
609 | + sprintf( |
|
610 | + esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
|
611 | + $classname |
|
612 | + ) |
|
613 | + ); |
|
614 | + } |
|
615 | + return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
616 | + } |
|
617 | + |
|
618 | + |
|
619 | + /** |
|
620 | + * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
621 | + * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
622 | + * 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 |
|
623 | + * we return false. |
|
624 | + * |
|
625 | + * @param array $props_n_values incoming array of properties and their values |
|
626 | + * @param string $classname the classname of the child class |
|
627 | + * @param string $timezone |
|
628 | + * @param array $date_formats incoming date_formats in an array where the first value is the |
|
629 | + * date_format and the second value is the time format |
|
630 | + * @return EE_Base_Class|EE_Soft_Delete_Base_Class|null |
|
631 | + * @throws InvalidArgumentException |
|
632 | + * @throws InvalidInterfaceException |
|
633 | + * @throws InvalidDataTypeException |
|
634 | + * @throws EE_Error |
|
635 | + * @throws ReflectionException |
|
636 | + * @throws ReflectionException |
|
637 | + * @throws ReflectionException |
|
638 | + */ |
|
639 | + protected static function _check_for_object( |
|
640 | + array $props_n_values, |
|
641 | + string $classname, |
|
642 | + string $timezone = '', |
|
643 | + array $date_formats = [] |
|
644 | + ) { |
|
645 | + $existing = null; |
|
646 | + $model = self::_get_model($classname, $timezone); |
|
647 | + if ($model->has_primary_key_field()) { |
|
648 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
649 | + if ( |
|
650 | + array_key_exists($primary_id_ref, $props_n_values) |
|
651 | + && ! empty($props_n_values[ $primary_id_ref ]) |
|
652 | + ) { |
|
653 | + $existing = $model->get_one_by_ID($props_n_values[ $primary_id_ref ]); |
|
654 | + } |
|
655 | + } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
656 | + // no primary key on this model, but there's still a matching item in the DB |
|
657 | + $existing = $model->get_one_by_ID( |
|
658 | + $model->get_index_primary_key_string($props_n_values) |
|
659 | + ); |
|
660 | + } |
|
661 | + if ($existing) { |
|
662 | + $has_date_formats = ! empty($date_formats) && is_array($date_formats) |
|
663 | + && isset($date_formats[0], $date_formats[1]); |
|
664 | + $date_format = $has_date_formats ? $date_formats[0] : (string) get_option('date_format', 'Y-m-d'); |
|
665 | + $time_format = $has_date_formats ? $date_formats[1] : (string) get_option('time_format', 'g:i a'); |
|
666 | + // set date formats before setting values |
|
667 | + $existing->set_date_format($date_format); |
|
668 | + $existing->set_time_format($time_format); |
|
669 | + foreach ($props_n_values as $property => $field_value) { |
|
670 | + $existing->set($property, $field_value); |
|
671 | + } |
|
672 | + return $existing; |
|
673 | + } |
|
674 | + return null; |
|
675 | + } |
|
676 | + |
|
677 | + |
|
678 | + /** |
|
679 | + * This sets the internal date format to what is sent in to be used as the new default for the class |
|
680 | + * internally instead of wp set date format options |
|
681 | + * |
|
682 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
683 | + * @since 4.6 |
|
684 | + */ |
|
685 | + public function set_date_format($format) |
|
686 | + { |
|
687 | + if ($format !== $this->_dt_frmt) { |
|
688 | + $this->_dt_frmt = $format; |
|
689 | + // clear cached_properties because they won't be relevant now. |
|
690 | + $this->_clear_cached_properties(); |
|
691 | + } |
|
692 | + } |
|
693 | + |
|
694 | + |
|
695 | + /** |
|
696 | + * This sets the internal time format string to what is sent in to be used as the new default for the |
|
697 | + * class internally instead of wp set time format options. |
|
698 | + * |
|
699 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
700 | + * @since 4.6 |
|
701 | + */ |
|
702 | + public function set_time_format($format) |
|
703 | + { |
|
704 | + if ($format !== $this->_tm_frmt) { |
|
705 | + $this->_tm_frmt = $format; |
|
706 | + // clear cached_properties because they won't be relevant now. |
|
707 | + $this->_clear_cached_properties(); |
|
708 | + } |
|
709 | + } |
|
710 | + |
|
711 | + |
|
712 | + /** |
|
713 | + * This just takes care of clearing out the cached_properties |
|
714 | + * |
|
715 | + * @return void |
|
716 | + */ |
|
717 | + protected function _clear_cached_properties() |
|
718 | + { |
|
719 | + $this->_cached_properties = []; |
|
720 | + } |
|
721 | + |
|
722 | + |
|
723 | + /** |
|
724 | + * Sets whether or not this model object should be allowed to be saved to the DB. |
|
725 | + * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
726 | + * you got new information that somehow made you change your mind. |
|
727 | + * |
|
728 | + * @param boolean $allow_persist |
|
729 | + * @return boolean |
|
730 | + */ |
|
731 | + public function set_allow_persist($allow_persist) |
|
732 | + { |
|
733 | + return $this->_allow_persist = $allow_persist; |
|
734 | + } |
|
735 | + |
|
736 | + |
|
737 | + /** |
|
738 | + * Gets the field's original value when this object was constructed during this request. |
|
739 | + * This can be helpful when determining if a model object has changed or not |
|
740 | + * |
|
741 | + * @param string $field_name |
|
742 | + * @return mixed|null |
|
743 | + * @throws InvalidArgumentException |
|
744 | + * @throws InvalidInterfaceException |
|
745 | + * @throws InvalidDataTypeException |
|
746 | + * @throws EE_Error |
|
747 | + */ |
|
748 | + public function get_original(string $field_name) |
|
749 | + { |
|
750 | + if ( |
|
751 | + isset($this->_props_n_values_provided_in_constructor[ $field_name ]) |
|
752 | + && $field_settings = $this->_model->field_settings_for($field_name) |
|
753 | + ) { |
|
754 | + return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]); |
|
755 | + } |
|
756 | + return null; |
|
757 | + } |
|
758 | + |
|
759 | + |
|
760 | + /** |
|
761 | + * @param EE_Base_Class $obj |
|
762 | + * @return string |
|
763 | + */ |
|
764 | + public function get_class($obj) |
|
765 | + { |
|
766 | + return get_class($obj); |
|
767 | + } |
|
768 | + |
|
769 | + |
|
770 | + /** |
|
771 | + * Set custom select values for model. |
|
772 | + * |
|
773 | + * @param array $custom_select_values |
|
774 | + */ |
|
775 | + public function setCustomSelectsValues(array $custom_select_values) |
|
776 | + { |
|
777 | + $this->custom_selection_results = $custom_select_values; |
|
778 | + } |
|
779 | + |
|
780 | + |
|
781 | + /** |
|
782 | + * Returns the custom select value for the provided alias if its set. |
|
783 | + * If not set, returns null. |
|
784 | + * |
|
785 | + * @param string $alias |
|
786 | + * @return string|int|float|null |
|
787 | + */ |
|
788 | + public function getCustomSelect($alias) |
|
789 | + { |
|
790 | + return isset($this->custom_selection_results[ $alias ]) |
|
791 | + ? $this->custom_selection_results[ $alias ] |
|
792 | + : null; |
|
793 | + } |
|
794 | + |
|
795 | + |
|
796 | + /** |
|
797 | + * This sets the field value on the db column if it exists for the given $column_name or |
|
798 | + * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
799 | + * |
|
800 | + * @param string $field_name Must be the exact column name. |
|
801 | + * @param mixed $field_value The value to set. |
|
802 | + * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
803 | + * @throws InvalidArgumentException |
|
804 | + * @throws InvalidInterfaceException |
|
805 | + * @throws InvalidDataTypeException |
|
806 | + * @throws EE_Error |
|
807 | + * @throws ReflectionException |
|
808 | + * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
809 | + */ |
|
810 | + public function set_field_or_extra_meta(string $field_name, $field_value) |
|
811 | + { |
|
812 | + if ($this->_model->has_field($field_name)) { |
|
813 | + $this->set($field_name, $field_value); |
|
814 | + return true; |
|
815 | + } |
|
816 | + // ensure this object is saved first so that extra meta can be properly related. |
|
817 | + $this->save(); |
|
818 | + return $this->update_extra_meta($field_name, $field_value); |
|
819 | + } |
|
820 | + |
|
821 | + |
|
822 | + /** |
|
823 | + * Saves this object to the database. An array may be supplied to set some values on this |
|
824 | + * object just before saving. |
|
825 | + * |
|
826 | + * @param array $set_cols_n_values keys are field names, values are their new values, |
|
827 | + * if provided during the save() method |
|
828 | + * (often client code will change the fields' values before calling save) |
|
829 | + * @return boolean|int 1 on a successful update or the ID of the new entry on insert; |
|
830 | + * 0 on failure or if the model object isn't allowed to persist |
|
831 | + * (as determined by EE_Base_Class::allow_persist()) |
|
832 | + * @throws InvalidInterfaceException |
|
833 | + * @throws InvalidDataTypeException |
|
834 | + * @throws EE_Error |
|
835 | + * @throws InvalidArgumentException |
|
836 | + * @throws ReflectionException |
|
837 | + * @throws ReflectionException |
|
838 | + * @throws ReflectionException |
|
839 | + */ |
|
840 | + public function save(array $set_cols_n_values = []) |
|
841 | + { |
|
842 | + /** |
|
843 | + * Filters the fields we're about to save on the model object |
|
844 | + * |
|
845 | + * @param array $set_cols_n_values |
|
846 | + * @param EE_Base_Class $model_object |
|
847 | + */ |
|
848 | + $set_cols_n_values = (array) apply_filters( |
|
849 | + 'FHEE__EE_Base_Class__save__set_cols_n_values', |
|
850 | + $set_cols_n_values, |
|
851 | + $this |
|
852 | + ); |
|
853 | + // set attributes as provided in $set_cols_n_values |
|
854 | + foreach ($set_cols_n_values as $column => $value) { |
|
855 | + $this->set($column, $value); |
|
856 | + } |
|
857 | + // no changes ? then don't do anything |
|
858 | + if (! $this->_has_changes && $this->ID() && $this->_model->get_primary_key_field()->is_auto_increment()) { |
|
859 | + return 0; |
|
860 | + } |
|
861 | + /** |
|
862 | + * Saving a model object. |
|
863 | + * Before we perform a save, this action is fired. |
|
864 | + * |
|
865 | + * @param EE_Base_Class $model_object the model object about to be saved. |
|
866 | + */ |
|
867 | + do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
868 | + if (! $this->allow_persist()) { |
|
869 | + return 0; |
|
870 | + } |
|
871 | + // now get current attribute values |
|
872 | + $save_cols_n_values = $this->_fields; |
|
873 | + // if the object already has an ID, update it. Otherwise, insert it |
|
874 | + // also: change the assumption about values passed to the model NOT being prepare dby the model object. |
|
875 | + // They have been |
|
876 | + $old_assumption_concerning_value_preparation = $this->_model |
|
877 | + ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
878 | + $this->_model->assume_values_already_prepared_by_model_object(true); |
|
879 | + // does this model have an autoincrement PK? |
|
880 | + if ($this->_model->has_primary_key_field()) { |
|
881 | + if ($this->_model->get_primary_key_field()->is_auto_increment()) { |
|
882 | + // ok check if it's set, if so: update; if not, insert |
|
883 | + if (! empty($save_cols_n_values[ $this->_model->primary_key_name() ])) { |
|
884 | + $results = $this->_model->update_by_ID($save_cols_n_values, $this->ID()); |
|
885 | + } else { |
|
886 | + unset($save_cols_n_values[ $this->_model->primary_key_name() ]); |
|
887 | + $results = $this->_model->insert($save_cols_n_values); |
|
888 | + if ($results) { |
|
889 | + // if successful, set the primary key |
|
890 | + // but don't use the normal SET method, because it will check if |
|
891 | + // an item with the same ID exists in the mapper & db, then |
|
892 | + // will find it in the db (because we just added it) and THAT object |
|
893 | + // will get added to the mapper before we can add this one! |
|
894 | + // but if we just avoid using the SET method, all that headache can be avoided |
|
895 | + $pk_field_name = $this->_model->primary_key_name(); |
|
896 | + $this->_fields[ $pk_field_name ] = $results; |
|
897 | + $this->_clear_cached_property($pk_field_name); |
|
898 | + $this->_model->add_to_entity_map($this); |
|
899 | + $this->_update_cached_related_model_objs_fks(); |
|
900 | + } |
|
901 | + } |
|
902 | + } else { |
|
903 | + // PK is NOT auto-increment |
|
904 | + // so check if one like it already exists in the db |
|
905 | + if ($this->_model->exists_by_ID($this->ID())) { |
|
906 | + if (WP_DEBUG && ! $this->in_entity_map()) { |
|
907 | + throw new EE_Error( |
|
908 | + sprintf( |
|
909 | + esc_html__( |
|
910 | + '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', |
|
911 | + 'event_espresso' |
|
912 | + ), |
|
913 | + get_class($this), |
|
914 | + get_class($this->_model) . '::instance()->add_to_entity_map()', |
|
915 | + get_class($this->_model) . '::instance()->get_one_by_ID()', |
|
916 | + '<br />' |
|
917 | + ) |
|
918 | + ); |
|
919 | + } |
|
920 | + $results = $this->_model->update_by_ID($save_cols_n_values, $this->ID()); |
|
921 | + } else { |
|
922 | + $results = $this->_model->insert($save_cols_n_values); |
|
923 | + $this->_update_cached_related_model_objs_fks(); |
|
924 | + } |
|
925 | + } |
|
926 | + } else { |
|
927 | + // there is NO primary key |
|
928 | + $already_in_db = false; |
|
929 | + foreach ($this->_model->unique_indexes() as $index) { |
|
930 | + $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
931 | + if ($this->_model->exists([$uniqueness_where_params])) { |
|
932 | + $already_in_db = true; |
|
933 | + } |
|
934 | + } |
|
935 | + if ($already_in_db) { |
|
936 | + $combined_pk_fields_n_values = array_intersect_key( |
|
937 | + $save_cols_n_values, |
|
938 | + $this->_model->get_combined_primary_key_fields() |
|
939 | + ); |
|
940 | + $results = $this->_model->update( |
|
941 | + $save_cols_n_values, |
|
942 | + $combined_pk_fields_n_values |
|
943 | + ); |
|
944 | + } else { |
|
945 | + $results = $this->_model->insert($save_cols_n_values); |
|
946 | + } |
|
947 | + } |
|
948 | + // restore the old assumption about values being prepared by the model object |
|
949 | + $this->_model->assume_values_already_prepared_by_model_object( |
|
950 | + $old_assumption_concerning_value_preparation |
|
951 | + ); |
|
952 | + /** |
|
953 | + * After saving the model object this action is called |
|
954 | + * |
|
955 | + * @param EE_Base_Class $model_object which was just saved |
|
956 | + * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
957 | + * the new ID (or 0 if an error occurred and it wasn't updated) |
|
958 | + */ |
|
959 | + do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
960 | + $this->_has_changes = false; |
|
961 | + return $results; |
|
962 | + } |
|
963 | + |
|
964 | + |
|
965 | + /** |
|
966 | + * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
967 | + * A $previous_value can be specified in case there are many meta rows with the same key |
|
968 | + * |
|
969 | + * @param string $meta_key |
|
970 | + * @param mixed $meta_value |
|
971 | + * @param mixed $previous_value |
|
972 | + * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
973 | + * NOTE: if the values haven't changed, returns 0 |
|
974 | + * @throws InvalidArgumentException |
|
975 | + * @throws InvalidInterfaceException |
|
976 | + * @throws InvalidDataTypeException |
|
977 | + * @throws EE_Error |
|
978 | + * @throws ReflectionException |
|
979 | + */ |
|
980 | + public function update_extra_meta(string $meta_key, $meta_value, $previous_value = null) |
|
981 | + { |
|
982 | + $query_params = [ |
|
983 | + [ |
|
984 | + 'EXM_key' => $meta_key, |
|
985 | + 'OBJ_ID' => $this->ID(), |
|
986 | + 'EXM_type' => $this->_model->get_this_model_name(), |
|
987 | + ], |
|
988 | + ]; |
|
989 | + if ($previous_value !== null) { |
|
990 | + $query_params[0]['EXM_value'] = $meta_value; |
|
991 | + } |
|
992 | + $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
993 | + if (! $existing_rows_like_that) { |
|
994 | + return $this->add_extra_meta($meta_key, $meta_value); |
|
995 | + } |
|
996 | + foreach ($existing_rows_like_that as $existing_row) { |
|
997 | + $existing_row->save(['EXM_value' => $meta_value]); |
|
998 | + } |
|
999 | + return count($existing_rows_like_that); |
|
1000 | + } |
|
1001 | + |
|
1002 | + |
|
1003 | + /** |
|
1004 | + * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
1005 | + * |
|
1006 | + * @return boolean |
|
1007 | + */ |
|
1008 | + public function allow_persist() |
|
1009 | + { |
|
1010 | + return $this->_allow_persist; |
|
1011 | + } |
|
1012 | + |
|
1013 | + |
|
1014 | + /** |
|
1015 | + * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
1016 | + * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
1017 | + * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
1018 | + * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
1019 | + * 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 |
|
1020 | + * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
1021 | + * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
1022 | + * |
|
1023 | + * @return void |
|
1024 | + * @throws ReflectionException |
|
1025 | + * @throws InvalidArgumentException |
|
1026 | + * @throws InvalidInterfaceException |
|
1027 | + * @throws InvalidDataTypeException |
|
1028 | + * @throws EE_Error |
|
1029 | + */ |
|
1030 | + protected function _update_cached_related_model_objs_fks() |
|
1031 | + { |
|
1032 | + foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
1033 | + if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
1034 | + foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
1035 | + $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
1036 | + $this->_model->get_this_model_name() |
|
1037 | + ); |
|
1038 | + $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
1039 | + if ($related_model_obj_in_cache->ID()) { |
|
1040 | + $related_model_obj_in_cache->save(); |
|
1041 | + } |
|
1042 | + } |
|
1043 | + } |
|
1044 | + } |
|
1045 | + } |
|
1046 | + |
|
1047 | + |
|
1048 | + /** |
|
1049 | + * in_entity_map |
|
1050 | + * Checks if this model object has been proven to already be in the entity map |
|
1051 | + * |
|
1052 | + * @return boolean |
|
1053 | + * @throws InvalidArgumentException |
|
1054 | + * @throws InvalidInterfaceException |
|
1055 | + * @throws InvalidDataTypeException |
|
1056 | + * @throws EE_Error |
|
1057 | + */ |
|
1058 | + public function in_entity_map() |
|
1059 | + { |
|
1060 | + // well, if we looked, did we find it in the entity map? |
|
1061 | + return $this->ID() && $this->_model->get_from_entity_map($this->ID()) === $this; |
|
1062 | + } |
|
1063 | + |
|
1064 | + |
|
1065 | + /** |
|
1066 | + * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
1067 | + * no other extra meta for this model object have the same key. Returns TRUE if the |
|
1068 | + * extra meta row was entered, false if not |
|
1069 | + * |
|
1070 | + * @param string $meta_key |
|
1071 | + * @param mixed $meta_value |
|
1072 | + * @param boolean $unique |
|
1073 | + * @return boolean |
|
1074 | + * @throws InvalidArgumentException |
|
1075 | + * @throws InvalidInterfaceException |
|
1076 | + * @throws InvalidDataTypeException |
|
1077 | + * @throws EE_Error |
|
1078 | + * @throws ReflectionException |
|
1079 | + * @throws ReflectionException |
|
1080 | + */ |
|
1081 | + public function add_extra_meta(string $meta_key, $meta_value, bool $unique = false): bool |
|
1082 | + { |
|
1083 | + if ($unique) { |
|
1084 | + $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
1085 | + [ |
|
1086 | + [ |
|
1087 | + 'EXM_key' => $meta_key, |
|
1088 | + 'OBJ_ID' => $this->ID(), |
|
1089 | + 'EXM_type' => $this->_model->get_this_model_name(), |
|
1090 | + ], |
|
1091 | + ] |
|
1092 | + ); |
|
1093 | + if ($existing_extra_meta) { |
|
1094 | + return false; |
|
1095 | + } |
|
1096 | + } |
|
1097 | + $new_extra_meta = EE_Extra_Meta::new_instance( |
|
1098 | + [ |
|
1099 | + 'EXM_key' => $meta_key, |
|
1100 | + 'EXM_value' => $meta_value, |
|
1101 | + 'OBJ_ID' => $this->ID(), |
|
1102 | + 'EXM_type' => $this->_model->get_this_model_name(), |
|
1103 | + ] |
|
1104 | + ); |
|
1105 | + $new_extra_meta->save(); |
|
1106 | + return true; |
|
1107 | + } |
|
1108 | + |
|
1109 | + |
|
1110 | + /** |
|
1111 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
1112 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
1113 | + * |
|
1114 | + * @param string $relationName |
|
1115 | + * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
1116 | + * @throws InvalidArgumentException |
|
1117 | + * @throws InvalidInterfaceException |
|
1118 | + * @throws InvalidDataTypeException |
|
1119 | + * @throws EE_Error |
|
1120 | + * @throws ReflectionException |
|
1121 | + */ |
|
1122 | + public function get_all_from_cache($relationName) |
|
1123 | + { |
|
1124 | + $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : []; |
|
1125 | + // if the result is not an array, but exists, make it an array |
|
1126 | + $objects = is_array($objects) ? $objects : [$objects]; |
|
1127 | + // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
1128 | + // basically, if this model object was stored in the session, and these cached model objects |
|
1129 | + // already have IDs, let's make sure they're in their model's entity mapper |
|
1130 | + // otherwise we will have duplicates next time we call |
|
1131 | + // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
1132 | + $related_model = EE_Registry::instance()->load_model($relationName); |
|
1133 | + foreach ($objects as $model_object) { |
|
1134 | + if ($related_model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
1135 | + // ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
1136 | + if ($model_object->ID()) { |
|
1137 | + $related_model->add_to_entity_map($model_object); |
|
1138 | + } |
|
1139 | + } else { |
|
1140 | + throw new EE_Error( |
|
1141 | + sprintf( |
|
1142 | + esc_html__( |
|
1143 | + 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
1144 | + 'event_espresso' |
|
1145 | + ), |
|
1146 | + $relationName, |
|
1147 | + gettype($model_object) |
|
1148 | + ) |
|
1149 | + ); |
|
1150 | + } |
|
1151 | + } |
|
1152 | + $this->updateTimezoneOnRelated($objects); |
|
1153 | + return $objects; |
|
1154 | + } |
|
1155 | + |
|
1156 | + |
|
1157 | + /** |
|
1158 | + * This retrieves the value of the db column set on this class or if that's not present |
|
1159 | + * it will attempt to retrieve from extra_meta if found. |
|
1160 | + * Example Usage: |
|
1161 | + * Via EE_Message child class: |
|
1162 | + * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
1163 | + * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
1164 | + * also have additional main fields specific to the messenger. The system accommodates those extra |
|
1165 | + * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
1166 | + * value for those extra fields dynamically via the EE_message object. |
|
1167 | + * |
|
1168 | + * @param string $field_name expecting the fully qualified field name. |
|
1169 | + * @return mixed|null value for the field if found. null if not found. |
|
1170 | + * @throws ReflectionException |
|
1171 | + * @throws InvalidArgumentException |
|
1172 | + * @throws InvalidInterfaceException |
|
1173 | + * @throws InvalidDataTypeException |
|
1174 | + * @throws EE_Error |
|
1175 | + */ |
|
1176 | + public function get_field_or_extra_meta(string $field_name) |
|
1177 | + { |
|
1178 | + // if this isn't a column in the main table, then see if it is in the extra meta. |
|
1179 | + return $this->_model->has_field($field_name) |
|
1180 | + ? $this->get($field_name) |
|
1181 | + : $this->get_extra_meta($field_name, true); |
|
1182 | + } |
|
1183 | + |
|
1184 | + |
|
1185 | + /** |
|
1186 | + * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
1187 | + * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
1188 | + * You can specify $default is case you haven't found the extra meta |
|
1189 | + * |
|
1190 | + * @param string $meta_key |
|
1191 | + * @param boolean $single |
|
1192 | + * @param mixed $default if we don't find anything, what should we return? |
|
1193 | + * @return mixed single value if $single; array if ! $single |
|
1194 | + * @throws ReflectionException |
|
1195 | + * @throws InvalidArgumentException |
|
1196 | + * @throws InvalidInterfaceException |
|
1197 | + * @throws InvalidDataTypeException |
|
1198 | + * @throws EE_Error |
|
1199 | + */ |
|
1200 | + public function get_extra_meta(string $meta_key, bool $single = false, $default = null) |
|
1201 | + { |
|
1202 | + if ($single) { |
|
1203 | + $result = $this->get_first_related( |
|
1204 | + 'Extra_Meta', |
|
1205 | + [['EXM_key' => $meta_key]] |
|
1206 | + ); |
|
1207 | + if ($result instanceof EE_Extra_Meta) { |
|
1208 | + return $result->value(); |
|
1209 | + } |
|
1210 | + } else { |
|
1211 | + $results = $this->get_many_related( |
|
1212 | + 'Extra_Meta', |
|
1213 | + [['EXM_key' => $meta_key]] |
|
1214 | + ); |
|
1215 | + if ($results) { |
|
1216 | + $values = []; |
|
1217 | + foreach ($results as $result) { |
|
1218 | + if ($result instanceof EE_Extra_Meta) { |
|
1219 | + $values[ $result->ID() ] = $result->value(); |
|
1220 | + } |
|
1221 | + } |
|
1222 | + return $values; |
|
1223 | + } |
|
1224 | + } |
|
1225 | + // if nothing discovered yet return default. |
|
1226 | + return apply_filters( |
|
1227 | + 'FHEE__EE_Base_Class__get_extra_meta__default_value', |
|
1228 | + $default, |
|
1229 | + $meta_key, |
|
1230 | + $single, |
|
1231 | + $this |
|
1232 | + ); |
|
1233 | + } |
|
1234 | + |
|
1235 | + |
|
1236 | + /** |
|
1237 | + * Gets the first (ie, one) related model object of the specified type. |
|
1238 | + * |
|
1239 | + * @param string $relationName key in the model's _model_relations array |
|
1240 | + * @param array $query_params |
|
1241 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1242 | + * @return EE_Base_Class (not an array, a single object) |
|
1243 | + * @throws ReflectionException |
|
1244 | + * @throws InvalidArgumentException |
|
1245 | + * @throws InvalidInterfaceException |
|
1246 | + * @throws InvalidDataTypeException |
|
1247 | + * @throws EE_Error |
|
1248 | + */ |
|
1249 | + public function get_first_related($relationName, $query_params = []) |
|
1250 | + { |
|
1251 | + $model_relation = $this->_model->related_settings_for($relationName); |
|
1252 | + if (! $this->ID()) { |
|
1253 | + // this doesn't exist in the DB, |
|
1254 | + // but maybe the relation type is "belongs to" and the related object might |
|
1255 | + if ($model_relation instanceof EE_Belongs_To_Relation) { |
|
1256 | 1256 | $related_model_object = $this->_model->get_first_related( |
1257 | 1257 | $this, |
1258 | 1258 | $relationName, |
@@ -1260,2165 +1260,2165 @@ discard block |
||
1260 | 1260 | ); |
1261 | 1261 | $this->updateTimezoneOnRelated($related_model_object); |
1262 | 1262 | return $related_model_object; |
1263 | - } |
|
1264 | - // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
|
1265 | - // just get what's cached on this object |
|
1263 | + } |
|
1264 | + // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
|
1265 | + // just get what's cached on this object |
|
1266 | 1266 | $cached_result = $this->get_one_from_cache($relationName); |
1267 | 1267 | if ($cached_result) { |
1268 | 1268 | $this->updateTimezoneOnRelated($cached_result); |
1269 | 1269 | return $cached_result; |
1270 | 1270 | } |
1271 | - } |
|
1272 | - // this exists in the DB, get from the cache OR the DB |
|
1273 | - // if they've provided some query parameters, don't bother trying to cache the result |
|
1274 | - // also make sure we're not caching the result of get_first_related |
|
1275 | - // on a relation which should have an array of objects (because the cache might have an array of objects) |
|
1276 | - if ($query_params || ! $model_relation instanceof EE_Belongs_To_Relation) { |
|
1277 | - $related_model_object =$this->_model->get_first_related( |
|
1271 | + } |
|
1272 | + // this exists in the DB, get from the cache OR the DB |
|
1273 | + // if they've provided some query parameters, don't bother trying to cache the result |
|
1274 | + // also make sure we're not caching the result of get_first_related |
|
1275 | + // on a relation which should have an array of objects (because the cache might have an array of objects) |
|
1276 | + if ($query_params || ! $model_relation instanceof EE_Belongs_To_Relation) { |
|
1277 | + $related_model_object =$this->_model->get_first_related( |
|
1278 | 1278 | $this, |
1279 | 1279 | $relationName, |
1280 | 1280 | $query_params |
1281 | - ); |
|
1281 | + ); |
|
1282 | 1282 | $this->updateTimezoneOnRelated($related_model_object); |
1283 | 1283 | return $related_model_object; |
1284 | - } |
|
1285 | - // check if we've already cached the result of this query |
|
1286 | - $cached_result = $this->get_one_from_cache($relationName); |
|
1287 | - if ($cached_result) { |
|
1284 | + } |
|
1285 | + // check if we've already cached the result of this query |
|
1286 | + $cached_result = $this->get_one_from_cache($relationName); |
|
1287 | + if ($cached_result) { |
|
1288 | 1288 | $this->updateTimezoneOnRelated($cached_result); |
1289 | 1289 | return $cached_result; |
1290 | - } |
|
1291 | - $related_model_object = $this->_model->get_first_related( |
|
1292 | - $this, |
|
1293 | - $relationName, |
|
1294 | - $query_params |
|
1295 | - ); |
|
1296 | - $this->cache($relationName, $related_model_object); |
|
1290 | + } |
|
1291 | + $related_model_object = $this->_model->get_first_related( |
|
1292 | + $this, |
|
1293 | + $relationName, |
|
1294 | + $query_params |
|
1295 | + ); |
|
1296 | + $this->cache($relationName, $related_model_object); |
|
1297 | 1297 | $this->updateTimezoneOnRelated($related_model_object); |
1298 | 1298 | return $related_model_object; |
1299 | - } |
|
1300 | - |
|
1301 | - |
|
1302 | - /** |
|
1303 | - * Gets all the related model objects of the specified type. Eg, if the current class if |
|
1304 | - * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
1305 | - * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
1306 | - * because we want to get even deleted items etc. |
|
1307 | - * |
|
1308 | - * @param string $relationName key in the model's _model_relations array |
|
1309 | - * @param array $query_params |
|
1310 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
1311 | - * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary |
|
1312 | - * keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these |
|
1313 | - * results if you want IDs |
|
1314 | - * @throws ReflectionException |
|
1315 | - * @throws InvalidArgumentException |
|
1316 | - * @throws InvalidInterfaceException |
|
1317 | - * @throws InvalidDataTypeException |
|
1318 | - * @throws EE_Error |
|
1319 | - */ |
|
1320 | - public function get_many_related($relationName, $query_params = []) |
|
1321 | - { |
|
1322 | - if (! $this->ID()) { |
|
1323 | - // this doesn't exist in the DB, so just get the related things from the cache |
|
1324 | - return $this->get_all_from_cache($relationName); |
|
1325 | - } |
|
1326 | - // this exists in the DB, so get the related things from either the cache or the DB |
|
1327 | - // if there are query parameters, forget about caching the related model objects. |
|
1328 | - if ($query_params) { |
|
1329 | - $related_model_objects = $this->_model->get_all_related( |
|
1330 | - $this, |
|
1331 | - $relationName, |
|
1332 | - $query_params |
|
1333 | - ); |
|
1299 | + } |
|
1300 | + |
|
1301 | + |
|
1302 | + /** |
|
1303 | + * Gets all the related model objects of the specified type. Eg, if the current class if |
|
1304 | + * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
1305 | + * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
1306 | + * because we want to get even deleted items etc. |
|
1307 | + * |
|
1308 | + * @param string $relationName key in the model's _model_relations array |
|
1309 | + * @param array $query_params |
|
1310 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
1311 | + * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary |
|
1312 | + * keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these |
|
1313 | + * results if you want IDs |
|
1314 | + * @throws ReflectionException |
|
1315 | + * @throws InvalidArgumentException |
|
1316 | + * @throws InvalidInterfaceException |
|
1317 | + * @throws InvalidDataTypeException |
|
1318 | + * @throws EE_Error |
|
1319 | + */ |
|
1320 | + public function get_many_related($relationName, $query_params = []) |
|
1321 | + { |
|
1322 | + if (! $this->ID()) { |
|
1323 | + // this doesn't exist in the DB, so just get the related things from the cache |
|
1324 | + return $this->get_all_from_cache($relationName); |
|
1325 | + } |
|
1326 | + // this exists in the DB, so get the related things from either the cache or the DB |
|
1327 | + // if there are query parameters, forget about caching the related model objects. |
|
1328 | + if ($query_params) { |
|
1329 | + $related_model_objects = $this->_model->get_all_related( |
|
1330 | + $this, |
|
1331 | + $relationName, |
|
1332 | + $query_params |
|
1333 | + ); |
|
1334 | 1334 | $this->updateTimezoneOnRelated($related_model_objects); |
1335 | 1335 | return $related_model_objects; |
1336 | - } |
|
1337 | - // did we already cache the result of this query? |
|
1338 | - $cached_results = $this->get_all_from_cache($relationName); |
|
1339 | - if ($cached_results) { |
|
1336 | + } |
|
1337 | + // did we already cache the result of this query? |
|
1338 | + $cached_results = $this->get_all_from_cache($relationName); |
|
1339 | + if ($cached_results) { |
|
1340 | 1340 | $this->updateTimezoneOnRelated($cached_results); |
1341 | - return $cached_results; |
|
1342 | - } |
|
1343 | - $related_model_objects = $this->_model->get_all_related( |
|
1344 | - $this, |
|
1345 | - $relationName, |
|
1346 | - $query_params |
|
1347 | - ); |
|
1341 | + return $cached_results; |
|
1342 | + } |
|
1343 | + $related_model_objects = $this->_model->get_all_related( |
|
1344 | + $this, |
|
1345 | + $relationName, |
|
1346 | + $query_params |
|
1347 | + ); |
|
1348 | 1348 | $this->updateTimezoneOnRelated($related_model_objects); |
1349 | - // if no query parameters were passed, then we got all the related model objects |
|
1350 | - // for that relation. We can cache them then. |
|
1351 | - foreach ($related_model_objects as $related_model_object) { |
|
1352 | - $this->cache($relationName, $related_model_object); |
|
1353 | - } |
|
1354 | - return $related_model_objects; |
|
1355 | - } |
|
1356 | - |
|
1357 | - |
|
1358 | - /** |
|
1359 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
1360 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
1361 | - * |
|
1362 | - * @param string $relationName |
|
1363 | - * @return EE_Base_Class |
|
1364 | - */ |
|
1365 | - public function get_one_from_cache($relationName) |
|
1366 | - { |
|
1367 | - $cached_array_or_object = isset($this->_model_relations[ $relationName ]) |
|
1368 | - ? $this->_model_relations[ $relationName ] |
|
1369 | - : null; |
|
1370 | - if (is_array($cached_array_or_object)) { |
|
1371 | - $cached_array_or_object = array_shift($cached_array_or_object); |
|
1372 | - } |
|
1373 | - $this->updateTimezoneOnRelated($cached_array_or_object); |
|
1374 | - return $cached_array_or_object; |
|
1375 | - } |
|
1376 | - |
|
1377 | - |
|
1378 | - /** |
|
1379 | - * cache |
|
1380 | - * stores the passed model object on the current model object. |
|
1381 | - * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
1382 | - * |
|
1383 | - * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
1384 | - * 'Registration' associated with this model object |
|
1385 | - * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
1386 | - * that could be a payment or a registration) |
|
1387 | - * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
1388 | - * items which will be stored in an array on this object |
|
1389 | - * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
1390 | - * related thing, no array) |
|
1391 | - * @throws InvalidArgumentException |
|
1392 | - * @throws InvalidInterfaceException |
|
1393 | - * @throws InvalidDataTypeException |
|
1394 | - * @throws EE_Error |
|
1395 | - */ |
|
1396 | - public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
1397 | - { |
|
1398 | - // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
1399 | - if (! $object_to_cache instanceof EE_Base_Class) { |
|
1400 | - return false; |
|
1401 | - } |
|
1402 | - // also get "how" the object is related, or throw an error |
|
1403 | - if (! $relationship_to_model = $this->_model->related_settings_for($relationName)) { |
|
1404 | - throw new EE_Error( |
|
1405 | - sprintf( |
|
1406 | - esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
1407 | - $relationName, |
|
1408 | - get_class($this) |
|
1409 | - ) |
|
1410 | - ); |
|
1411 | - } |
|
1412 | - // how many things are related ? |
|
1413 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
1414 | - // if it's a "belongs to" relationship, then there's only one related model object |
|
1415 | - // eg, if this is a registration, there's only 1 attendee for it |
|
1416 | - // so for these model objects just set it to be cached |
|
1417 | - $this->_model_relations[ $relationName ] = $object_to_cache; |
|
1418 | - return true; |
|
1419 | - } |
|
1420 | - // otherwise, this is the "many" side of a one to many relationship, |
|
1421 | - // so we'll add the object to the array of related objects for that type. |
|
1422 | - // eg: if this is an event, there are many registrations for that event, |
|
1423 | - // so we cache the registrations in an array |
|
1424 | - if (! is_array($this->_model_relations[ $relationName ])) { |
|
1425 | - // if for some reason, the cached item is a model object, |
|
1426 | - // then stick that in the array, otherwise start with an empty array |
|
1427 | - $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ] instanceof EE_Base_Class |
|
1428 | - ? [$this->_model_relations[ $relationName ]] |
|
1429 | - : []; |
|
1430 | - } |
|
1431 | - // first check for a cache_id which is normally empty |
|
1432 | - if (! empty($cache_id)) { |
|
1433 | - // if the cache_id exists, then it means we are purposely trying to cache this |
|
1434 | - // with a known key that can then be used to retrieve the object later on |
|
1435 | - $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache; |
|
1436 | - return $cache_id; |
|
1437 | - } |
|
1438 | - if ($object_to_cache->ID()) { |
|
1439 | - // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
1440 | - $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache; |
|
1441 | - return $object_to_cache->ID(); |
|
1442 | - } |
|
1443 | - // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
1444 | - $this->_model_relations[ $relationName ][] = $object_to_cache; |
|
1445 | - // move the internal pointer to the end of the array |
|
1446 | - end($this->_model_relations[ $relationName ]); |
|
1447 | - // and grab the key so that we can return it |
|
1448 | - return key($this->_model_relations[ $relationName ]); |
|
1449 | - } |
|
1450 | - |
|
1451 | - |
|
1452 | - /** |
|
1453 | - * This just returns whatever is set for the current timezone. |
|
1454 | - * |
|
1455 | - * @return string timezone string |
|
1456 | - */ |
|
1457 | - public function get_timezone() |
|
1458 | - { |
|
1459 | - if (empty($this->_timezone)) { |
|
1460 | - $this->set_timezone(); |
|
1461 | - } |
|
1462 | - return $this->_timezone; |
|
1463 | - } |
|
1464 | - |
|
1465 | - |
|
1466 | - /** |
|
1467 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
1468 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
1469 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
1470 | - * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
1471 | - * |
|
1472 | - * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
1473 | - * @param bool $only_if_not_set if true and $this->_timezone already has a value, then will not do anything |
|
1474 | - * @return void |
|
1475 | - */ |
|
1476 | - public function set_timezone(string $timezone = '', bool $only_if_not_set = false) |
|
1477 | - { |
|
1478 | - static $set_in_progress = false; |
|
1479 | - // don't update the timezone if it's already set ? |
|
1480 | - if (($only_if_not_set && $this->_timezone !== '') ) { |
|
1481 | - return; |
|
1482 | - } |
|
1483 | - $valid_timezone = ! empty($timezone) && $this->_timezone && $timezone !== $this->_timezone |
|
1484 | - ? EEH_DTT_Helper::get_valid_timezone_string($timezone) |
|
1485 | - : $this->_timezone; |
|
1486 | - // do NOT set the timezone if we are already in the process of setting the timezone |
|
1487 | - // OR the existing timezone is already set and the incoming value is nothing (which gets set to default TZ) |
|
1488 | - // OR the existing timezone is already set and the validated value is the same as the existing timezone |
|
1489 | - if ( |
|
1490 | - $set_in_progress |
|
1491 | - || ( |
|
1492 | - ! empty($this->_timezone) |
|
1493 | - && ( |
|
1494 | - empty($timezone) || $valid_timezone === $this->_timezone |
|
1495 | - ) |
|
1496 | - ) |
|
1497 | - ) { |
|
1498 | - return; |
|
1499 | - } |
|
1500 | - $set_in_progress = true; |
|
1501 | - $this->_timezone = $valid_timezone ? $valid_timezone : EEH_DTT_Helper::get_valid_timezone_string(); |
|
1502 | - $TZ = new DateTimeZone($this->_timezone); |
|
1503 | - // make sure we clear all cached properties because they won't be relevant now |
|
1504 | - $this->_clear_cached_properties(); |
|
1505 | - // make sure we update field settings and the date for all EE_Datetime_Fields |
|
1506 | - $model_fields = $this->_model->field_settings(); |
|
1507 | - foreach ($model_fields as $field_name => $field_obj) { |
|
1508 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
1509 | - $field_obj->set_timezone($this->_timezone); |
|
1510 | - if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) { |
|
1511 | - EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], $TZ); |
|
1512 | - } |
|
1513 | - } |
|
1514 | - } |
|
1515 | - $set_in_progress = false; |
|
1516 | - } |
|
1517 | - |
|
1518 | - |
|
1519 | - /** |
|
1520 | - * @param array|EE_Base_Class $related |
|
1521 | - * @since $VID:$ |
|
1522 | - */ |
|
1523 | - private function updateTimezoneOnRelated($related) |
|
1524 | - { |
|
1525 | - if ($related instanceof EE_Base_Class && $related->get_timezone() !== $this->_timezone) { |
|
1526 | - $related->set_timezone($this->_timezone); |
|
1527 | - return; |
|
1528 | - } |
|
1529 | - if (is_array($related)) { |
|
1530 | - foreach ($related as $object) { |
|
1531 | - $this->updateTimezoneOnRelated($object); |
|
1532 | - } |
|
1533 | - } |
|
1534 | - } |
|
1535 | - |
|
1536 | - |
|
1537 | - /** |
|
1538 | - * This returns the current internal set format for the date and time formats. |
|
1539 | - * |
|
1540 | - * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
1541 | - * where the first value is the date format and the second value is the time format. |
|
1542 | - * @return mixed string|array |
|
1543 | - */ |
|
1544 | - public function get_format($full = true) |
|
1545 | - { |
|
1546 | - return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : [$this->_dt_frmt, $this->_tm_frmt]; |
|
1547 | - } |
|
1548 | - |
|
1549 | - |
|
1550 | - /** |
|
1551 | - * update_cache_after_object_save |
|
1552 | - * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
1553 | - * obtained after being saved to the db |
|
1554 | - * |
|
1555 | - * @param string $relationName - the type of object that is cached |
|
1556 | - * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
1557 | - * @param string $current_cache_id - the ID that was used when originally caching the object |
|
1558 | - * @return boolean TRUE on success, FALSE on fail |
|
1559 | - * @throws InvalidArgumentException |
|
1560 | - * @throws InvalidDataTypeException |
|
1561 | - * @throws InvalidInterfaceException |
|
1562 | - * @throws EE_Error |
|
1563 | - */ |
|
1564 | - public function update_cache_after_object_save( |
|
1565 | - $relationName, |
|
1566 | - EE_Base_Class $newly_saved_object, |
|
1567 | - $current_cache_id = '' |
|
1568 | - ) { |
|
1569 | - // verify that incoming object is of the correct type |
|
1570 | - $obj_class = 'EE_' . $relationName; |
|
1571 | - if (! $newly_saved_object instanceof $obj_class) { |
|
1572 | - return false; |
|
1573 | - } |
|
1349 | + // if no query parameters were passed, then we got all the related model objects |
|
1350 | + // for that relation. We can cache them then. |
|
1351 | + foreach ($related_model_objects as $related_model_object) { |
|
1352 | + $this->cache($relationName, $related_model_object); |
|
1353 | + } |
|
1354 | + return $related_model_objects; |
|
1355 | + } |
|
1356 | + |
|
1357 | + |
|
1358 | + /** |
|
1359 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
1360 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
1361 | + * |
|
1362 | + * @param string $relationName |
|
1363 | + * @return EE_Base_Class |
|
1364 | + */ |
|
1365 | + public function get_one_from_cache($relationName) |
|
1366 | + { |
|
1367 | + $cached_array_or_object = isset($this->_model_relations[ $relationName ]) |
|
1368 | + ? $this->_model_relations[ $relationName ] |
|
1369 | + : null; |
|
1370 | + if (is_array($cached_array_or_object)) { |
|
1371 | + $cached_array_or_object = array_shift($cached_array_or_object); |
|
1372 | + } |
|
1373 | + $this->updateTimezoneOnRelated($cached_array_or_object); |
|
1374 | + return $cached_array_or_object; |
|
1375 | + } |
|
1376 | + |
|
1377 | + |
|
1378 | + /** |
|
1379 | + * cache |
|
1380 | + * stores the passed model object on the current model object. |
|
1381 | + * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
1382 | + * |
|
1383 | + * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
1384 | + * 'Registration' associated with this model object |
|
1385 | + * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
1386 | + * that could be a payment or a registration) |
|
1387 | + * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
1388 | + * items which will be stored in an array on this object |
|
1389 | + * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
1390 | + * related thing, no array) |
|
1391 | + * @throws InvalidArgumentException |
|
1392 | + * @throws InvalidInterfaceException |
|
1393 | + * @throws InvalidDataTypeException |
|
1394 | + * @throws EE_Error |
|
1395 | + */ |
|
1396 | + public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
1397 | + { |
|
1398 | + // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
1399 | + if (! $object_to_cache instanceof EE_Base_Class) { |
|
1400 | + return false; |
|
1401 | + } |
|
1402 | + // also get "how" the object is related, or throw an error |
|
1403 | + if (! $relationship_to_model = $this->_model->related_settings_for($relationName)) { |
|
1404 | + throw new EE_Error( |
|
1405 | + sprintf( |
|
1406 | + esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
1407 | + $relationName, |
|
1408 | + get_class($this) |
|
1409 | + ) |
|
1410 | + ); |
|
1411 | + } |
|
1412 | + // how many things are related ? |
|
1413 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
1414 | + // if it's a "belongs to" relationship, then there's only one related model object |
|
1415 | + // eg, if this is a registration, there's only 1 attendee for it |
|
1416 | + // so for these model objects just set it to be cached |
|
1417 | + $this->_model_relations[ $relationName ] = $object_to_cache; |
|
1418 | + return true; |
|
1419 | + } |
|
1420 | + // otherwise, this is the "many" side of a one to many relationship, |
|
1421 | + // so we'll add the object to the array of related objects for that type. |
|
1422 | + // eg: if this is an event, there are many registrations for that event, |
|
1423 | + // so we cache the registrations in an array |
|
1424 | + if (! is_array($this->_model_relations[ $relationName ])) { |
|
1425 | + // if for some reason, the cached item is a model object, |
|
1426 | + // then stick that in the array, otherwise start with an empty array |
|
1427 | + $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ] instanceof EE_Base_Class |
|
1428 | + ? [$this->_model_relations[ $relationName ]] |
|
1429 | + : []; |
|
1430 | + } |
|
1431 | + // first check for a cache_id which is normally empty |
|
1432 | + if (! empty($cache_id)) { |
|
1433 | + // if the cache_id exists, then it means we are purposely trying to cache this |
|
1434 | + // with a known key that can then be used to retrieve the object later on |
|
1435 | + $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache; |
|
1436 | + return $cache_id; |
|
1437 | + } |
|
1438 | + if ($object_to_cache->ID()) { |
|
1439 | + // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
1440 | + $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache; |
|
1441 | + return $object_to_cache->ID(); |
|
1442 | + } |
|
1443 | + // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
1444 | + $this->_model_relations[ $relationName ][] = $object_to_cache; |
|
1445 | + // move the internal pointer to the end of the array |
|
1446 | + end($this->_model_relations[ $relationName ]); |
|
1447 | + // and grab the key so that we can return it |
|
1448 | + return key($this->_model_relations[ $relationName ]); |
|
1449 | + } |
|
1450 | + |
|
1451 | + |
|
1452 | + /** |
|
1453 | + * This just returns whatever is set for the current timezone. |
|
1454 | + * |
|
1455 | + * @return string timezone string |
|
1456 | + */ |
|
1457 | + public function get_timezone() |
|
1458 | + { |
|
1459 | + if (empty($this->_timezone)) { |
|
1460 | + $this->set_timezone(); |
|
1461 | + } |
|
1462 | + return $this->_timezone; |
|
1463 | + } |
|
1464 | + |
|
1465 | + |
|
1466 | + /** |
|
1467 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
1468 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
1469 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
1470 | + * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
1471 | + * |
|
1472 | + * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
1473 | + * @param bool $only_if_not_set if true and $this->_timezone already has a value, then will not do anything |
|
1474 | + * @return void |
|
1475 | + */ |
|
1476 | + public function set_timezone(string $timezone = '', bool $only_if_not_set = false) |
|
1477 | + { |
|
1478 | + static $set_in_progress = false; |
|
1479 | + // don't update the timezone if it's already set ? |
|
1480 | + if (($only_if_not_set && $this->_timezone !== '') ) { |
|
1481 | + return; |
|
1482 | + } |
|
1483 | + $valid_timezone = ! empty($timezone) && $this->_timezone && $timezone !== $this->_timezone |
|
1484 | + ? EEH_DTT_Helper::get_valid_timezone_string($timezone) |
|
1485 | + : $this->_timezone; |
|
1486 | + // do NOT set the timezone if we are already in the process of setting the timezone |
|
1487 | + // OR the existing timezone is already set and the incoming value is nothing (which gets set to default TZ) |
|
1488 | + // OR the existing timezone is already set and the validated value is the same as the existing timezone |
|
1489 | + if ( |
|
1490 | + $set_in_progress |
|
1491 | + || ( |
|
1492 | + ! empty($this->_timezone) |
|
1493 | + && ( |
|
1494 | + empty($timezone) || $valid_timezone === $this->_timezone |
|
1495 | + ) |
|
1496 | + ) |
|
1497 | + ) { |
|
1498 | + return; |
|
1499 | + } |
|
1500 | + $set_in_progress = true; |
|
1501 | + $this->_timezone = $valid_timezone ? $valid_timezone : EEH_DTT_Helper::get_valid_timezone_string(); |
|
1502 | + $TZ = new DateTimeZone($this->_timezone); |
|
1503 | + // make sure we clear all cached properties because they won't be relevant now |
|
1504 | + $this->_clear_cached_properties(); |
|
1505 | + // make sure we update field settings and the date for all EE_Datetime_Fields |
|
1506 | + $model_fields = $this->_model->field_settings(); |
|
1507 | + foreach ($model_fields as $field_name => $field_obj) { |
|
1508 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
1509 | + $field_obj->set_timezone($this->_timezone); |
|
1510 | + if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) { |
|
1511 | + EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], $TZ); |
|
1512 | + } |
|
1513 | + } |
|
1514 | + } |
|
1515 | + $set_in_progress = false; |
|
1516 | + } |
|
1517 | + |
|
1518 | + |
|
1519 | + /** |
|
1520 | + * @param array|EE_Base_Class $related |
|
1521 | + * @since $VID:$ |
|
1522 | + */ |
|
1523 | + private function updateTimezoneOnRelated($related) |
|
1524 | + { |
|
1525 | + if ($related instanceof EE_Base_Class && $related->get_timezone() !== $this->_timezone) { |
|
1526 | + $related->set_timezone($this->_timezone); |
|
1527 | + return; |
|
1528 | + } |
|
1529 | + if (is_array($related)) { |
|
1530 | + foreach ($related as $object) { |
|
1531 | + $this->updateTimezoneOnRelated($object); |
|
1532 | + } |
|
1533 | + } |
|
1534 | + } |
|
1535 | + |
|
1536 | + |
|
1537 | + /** |
|
1538 | + * This returns the current internal set format for the date and time formats. |
|
1539 | + * |
|
1540 | + * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
1541 | + * where the first value is the date format and the second value is the time format. |
|
1542 | + * @return mixed string|array |
|
1543 | + */ |
|
1544 | + public function get_format($full = true) |
|
1545 | + { |
|
1546 | + return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : [$this->_dt_frmt, $this->_tm_frmt]; |
|
1547 | + } |
|
1548 | + |
|
1549 | + |
|
1550 | + /** |
|
1551 | + * update_cache_after_object_save |
|
1552 | + * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
1553 | + * obtained after being saved to the db |
|
1554 | + * |
|
1555 | + * @param string $relationName - the type of object that is cached |
|
1556 | + * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
1557 | + * @param string $current_cache_id - the ID that was used when originally caching the object |
|
1558 | + * @return boolean TRUE on success, FALSE on fail |
|
1559 | + * @throws InvalidArgumentException |
|
1560 | + * @throws InvalidDataTypeException |
|
1561 | + * @throws InvalidInterfaceException |
|
1562 | + * @throws EE_Error |
|
1563 | + */ |
|
1564 | + public function update_cache_after_object_save( |
|
1565 | + $relationName, |
|
1566 | + EE_Base_Class $newly_saved_object, |
|
1567 | + $current_cache_id = '' |
|
1568 | + ) { |
|
1569 | + // verify that incoming object is of the correct type |
|
1570 | + $obj_class = 'EE_' . $relationName; |
|
1571 | + if (! $newly_saved_object instanceof $obj_class) { |
|
1572 | + return false; |
|
1573 | + } |
|
1574 | 1574 | $this->updateTimezoneOnRelated($newly_saved_object); |
1575 | - /* @type EE_Base_Class $newly_saved_object */ |
|
1576 | - // now get the type of relation |
|
1577 | - $relationship_to_model = $this->_model->related_settings_for($relationName); |
|
1578 | - // if this is a 1:1 relationship |
|
1579 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
1580 | - // then just replace the cached object with the newly saved object |
|
1581 | - $this->_model_relations[ $relationName ] = $newly_saved_object; |
|
1582 | - return true; |
|
1583 | - } |
|
1584 | - // or if it's some kind of sordid feral polyamorous relationship... |
|
1585 | - if ( |
|
1586 | - is_array($this->_model_relations[ $relationName ]) |
|
1587 | - && isset($this->_model_relations[ $relationName ][ $current_cache_id ]) |
|
1588 | - ) { |
|
1589 | - // then remove the current cached item |
|
1590 | - unset($this->_model_relations[ $relationName ][ $current_cache_id ]); |
|
1591 | - // and cache the newly saved object using it's new ID |
|
1592 | - $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object; |
|
1593 | - return true; |
|
1594 | - } |
|
1595 | - return false; |
|
1596 | - } |
|
1597 | - |
|
1598 | - |
|
1599 | - /** |
|
1600 | - * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
1601 | - * matching the given query conditions. |
|
1602 | - * |
|
1603 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
1604 | - * @param int $limit How many objects to return. |
|
1605 | - * @param array $query_params Any additional conditions on the query. |
|
1606 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
1607 | - * you can indicate just the columns you want returned |
|
1608 | - * @return array|EE_Base_Class[] |
|
1609 | - * @throws ReflectionException |
|
1610 | - * @throws InvalidArgumentException |
|
1611 | - * @throws InvalidInterfaceException |
|
1612 | - * @throws InvalidDataTypeException |
|
1613 | - * @throws EE_Error |
|
1614 | - */ |
|
1615 | - public function next_x($field_to_order_by = null, $limit = 1, $query_params = [], $columns_to_select = null) |
|
1616 | - { |
|
1617 | - $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1618 | - ? $this->_model->get_primary_key_field()->get_name() |
|
1619 | - : $field_to_order_by; |
|
1620 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
1621 | - if (empty($field) || empty($current_value)) { |
|
1622 | - return []; |
|
1623 | - } |
|
1624 | - return $this->_model->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
1625 | - } |
|
1626 | - |
|
1627 | - |
|
1628 | - /** |
|
1629 | - * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
1630 | - * matching the given query conditions. |
|
1631 | - * |
|
1632 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
1633 | - * @param int $limit How many objects to return. |
|
1634 | - * @param array $query_params Any additional conditions on the query. |
|
1635 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
1636 | - * you can indicate just the columns you want returned |
|
1637 | - * @return array|EE_Base_Class[] |
|
1638 | - * @throws ReflectionException |
|
1639 | - * @throws InvalidArgumentException |
|
1640 | - * @throws InvalidInterfaceException |
|
1641 | - * @throws InvalidDataTypeException |
|
1642 | - * @throws EE_Error |
|
1643 | - */ |
|
1644 | - public function previous_x( |
|
1645 | - $field_to_order_by = null, |
|
1646 | - $limit = 1, |
|
1647 | - $query_params = [], |
|
1648 | - $columns_to_select = null |
|
1649 | - ) { |
|
1650 | - $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1651 | - ? $this->_model->get_primary_key_field()->get_name() |
|
1652 | - : $field_to_order_by; |
|
1653 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
1654 | - if (empty($field) || empty($current_value)) { |
|
1655 | - return []; |
|
1656 | - } |
|
1657 | - return $this->_model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
1658 | - } |
|
1659 | - |
|
1660 | - |
|
1661 | - /** |
|
1662 | - * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
1663 | - * matching the given query conditions. |
|
1664 | - * |
|
1665 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
1666 | - * @param array $query_params Any additional conditions on the query. |
|
1667 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
1668 | - * you can indicate just the columns you want returned |
|
1669 | - * @return array|EE_Base_Class |
|
1670 | - * @throws ReflectionException |
|
1671 | - * @throws InvalidArgumentException |
|
1672 | - * @throws InvalidInterfaceException |
|
1673 | - * @throws InvalidDataTypeException |
|
1674 | - * @throws EE_Error |
|
1675 | - */ |
|
1676 | - public function next($field_to_order_by = null, $query_params = [], $columns_to_select = null) |
|
1677 | - { |
|
1678 | - $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1679 | - ? $this->_model->get_primary_key_field()->get_name() |
|
1680 | - : $field_to_order_by; |
|
1681 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
1682 | - if (empty($field) || empty($current_value)) { |
|
1683 | - return []; |
|
1684 | - } |
|
1685 | - return $this->_model->next($current_value, $field, $query_params, $columns_to_select); |
|
1686 | - } |
|
1687 | - |
|
1688 | - |
|
1689 | - /** |
|
1690 | - * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
1691 | - * matching the given query conditions. |
|
1692 | - * |
|
1693 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
1694 | - * @param array $query_params Any additional conditions on the query. |
|
1695 | - * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
1696 | - * you can indicate just the column you want returned |
|
1697 | - * @return array|EE_Base_Class |
|
1698 | - * @throws ReflectionException |
|
1699 | - * @throws InvalidArgumentException |
|
1700 | - * @throws InvalidInterfaceException |
|
1701 | - * @throws InvalidDataTypeException |
|
1702 | - * @throws EE_Error |
|
1703 | - */ |
|
1704 | - public function previous($field_to_order_by = null, $query_params = [], $columns_to_select = null) |
|
1705 | - { |
|
1706 | - $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1707 | - ? $this->_model->get_primary_key_field()->get_name() |
|
1708 | - : $field_to_order_by; |
|
1709 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
1710 | - if (empty($field) || empty($current_value)) { |
|
1711 | - return []; |
|
1712 | - } |
|
1713 | - return $this->_model->previous($current_value, $field, $query_params, $columns_to_select); |
|
1714 | - } |
|
1715 | - |
|
1716 | - |
|
1717 | - /** |
|
1718 | - * This is used to return the internal DateTime object used for a field that is a |
|
1719 | - * EE_Datetime_Field. |
|
1720 | - * |
|
1721 | - * @param string $field_name The field name retrieving the DateTime object. |
|
1722 | - * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
1723 | - * @throws EE_Error an error is set and false returned. If the field IS an |
|
1724 | - * EE_Datetime_Field and but the field value is null, then |
|
1725 | - * just null is returned (because that indicates that likely |
|
1726 | - * this field is nullable). |
|
1727 | - * @throws InvalidArgumentException |
|
1728 | - * @throws InvalidDataTypeException |
|
1729 | - * @throws InvalidInterfaceException |
|
1730 | - */ |
|
1731 | - public function get_DateTime_object(string $field_name) |
|
1732 | - { |
|
1733 | - $field_settings = $this->_model->field_settings_for($field_name); |
|
1734 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
1735 | - EE_Error::add_error( |
|
1736 | - sprintf( |
|
1737 | - esc_html__( |
|
1738 | - 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
1739 | - 'event_espresso' |
|
1740 | - ), |
|
1741 | - $field_name |
|
1742 | - ), |
|
1743 | - __FILE__, |
|
1744 | - __FUNCTION__, |
|
1745 | - __LINE__ |
|
1746 | - ); |
|
1747 | - return false; |
|
1748 | - } |
|
1749 | - return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime |
|
1750 | - ? clone $this->_fields[ $field_name ] |
|
1751 | - : null; |
|
1752 | - } |
|
1753 | - |
|
1754 | - |
|
1755 | - |
|
1756 | - |
|
1757 | - /** |
|
1758 | - * NOTE ABOUT BELOW: |
|
1759 | - * These convenience date and time setters are for setting date and time independently. In other words you might |
|
1760 | - * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
1761 | - * you want to set both date and time at the same time, you can just use the models default set($field_name,$value) |
|
1762 | - * method and make sure you send the entire datetime value for setting. |
|
1763 | - */ |
|
1764 | - |
|
1765 | - |
|
1766 | - /** |
|
1767 | - * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
1768 | - * can be easily used as the value of form input. |
|
1769 | - * |
|
1770 | - * @param string string $field_name |
|
1771 | - * @return void |
|
1772 | - * @throws InvalidArgumentException |
|
1773 | - * @throws InvalidInterfaceException |
|
1774 | - * @throws InvalidDataTypeException |
|
1775 | - * @throws EE_Error |
|
1776 | - */ |
|
1777 | - public function f(string $field_name) |
|
1778 | - { |
|
1779 | - $this->e($field_name, 'form_input'); |
|
1780 | - } |
|
1781 | - |
|
1782 | - |
|
1783 | - /** |
|
1784 | - * To be used in template to immediately echo out the value, and format it for output. |
|
1785 | - * Eg, should call stripslashes and whatnot before echoing |
|
1786 | - * |
|
1787 | - * @param string $field_name the name of the field as it appears in the DB |
|
1788 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
1789 | - * (in cases where the same property may be used for different outputs |
|
1790 | - * - i.e. datetime, money etc.) |
|
1791 | - * @return void |
|
1792 | - * @throws InvalidArgumentException |
|
1793 | - * @throws InvalidInterfaceException |
|
1794 | - * @throws InvalidDataTypeException |
|
1795 | - * @throws EE_Error |
|
1796 | - */ |
|
1797 | - public function e(string $field_name, string $extra_cache_ref = '') |
|
1798 | - { |
|
1799 | - echo $this->get_pretty($field_name, $extra_cache_ref); |
|
1800 | - } |
|
1801 | - |
|
1802 | - |
|
1803 | - /** |
|
1804 | - * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
1805 | - * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
1806 | - * to see what options are available. |
|
1807 | - * |
|
1808 | - * @param string $field_name |
|
1809 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
1810 | - * (in cases where the same property may be used for different outputs |
|
1811 | - * - i.e. datetime, money etc.) |
|
1812 | - * @return mixed |
|
1813 | - * @throws InvalidArgumentException |
|
1814 | - * @throws InvalidInterfaceException |
|
1815 | - * @throws InvalidDataTypeException |
|
1816 | - * @throws EE_Error |
|
1817 | - */ |
|
1818 | - public function get_pretty(string $field_name, string $extra_cache_ref = '') |
|
1819 | - { |
|
1820 | - return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
1821 | - } |
|
1822 | - |
|
1823 | - |
|
1824 | - /** |
|
1825 | - * Same as `f()` but just returns the value instead of echoing it |
|
1826 | - * |
|
1827 | - * @param string $field_name |
|
1828 | - * @return string |
|
1829 | - * @throws InvalidArgumentException |
|
1830 | - * @throws InvalidInterfaceException |
|
1831 | - * @throws InvalidDataTypeException |
|
1832 | - * @throws EE_Error |
|
1833 | - */ |
|
1834 | - public function get_f(string $field_name) |
|
1835 | - { |
|
1836 | - return (string) $this->get_pretty($field_name, 'form_input'); |
|
1837 | - } |
|
1838 | - |
|
1839 | - |
|
1840 | - /** |
|
1841 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
1842 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
1843 | - * other echoes the pretty value for dtt) |
|
1844 | - * |
|
1845 | - * @param string $field_name name of model object datetime field holding the value |
|
1846 | - * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
1847 | - * @return string datetime value formatted |
|
1848 | - * @throws InvalidArgumentException |
|
1849 | - * @throws InvalidInterfaceException |
|
1850 | - * @throws InvalidDataTypeException |
|
1851 | - * @throws EE_Error |
|
1852 | - */ |
|
1853 | - public function get_date(string $field_name, string $format = '') |
|
1854 | - { |
|
1855 | - return $this->_get_datetime($field_name, $format, null, 'D'); |
|
1856 | - } |
|
1857 | - |
|
1858 | - |
|
1859 | - /** |
|
1860 | - * This simply returns the datetime for the given field name |
|
1861 | - * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
1862 | - * (and the equivalent e_date, e_time, e_datetime). |
|
1863 | - * |
|
1864 | - * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
1865 | - * @param string $date_format valid datetime format used for date |
|
1866 | - * (if '' then we just use the default on the field, |
|
1867 | - * if NULL we use the last-used format) |
|
1868 | - * @param string $time_format Same as above except this is for time format |
|
1869 | - * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
1870 | - * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
1871 | - * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
1872 | - * if field is not a valid dtt field, or void if echoing |
|
1873 | - * @throws InvalidArgumentException |
|
1874 | - * @throws InvalidInterfaceException |
|
1875 | - * @throws InvalidDataTypeException |
|
1876 | - * @throws EE_Error |
|
1877 | - */ |
|
1878 | - protected function _get_datetime( |
|
1879 | - string $field_name, |
|
1880 | - string $date_format = '', |
|
1881 | - string $time_format = '', |
|
1882 | - string $date_or_time = '', |
|
1883 | - bool $echo = false |
|
1884 | - ) { |
|
1885 | - // clear cached property |
|
1886 | - $this->_clear_cached_property($field_name); |
|
1887 | - // reset format properties because they are used in get() |
|
1888 | - $this->_dt_frmt = $date_format !== '' ? $date_format : $this->_dt_frmt; |
|
1889 | - $this->_tm_frmt = $time_format !== '' ? $time_format : $this->_tm_frmt; |
|
1890 | - if ($echo) { |
|
1891 | - $this->e($field_name, $date_or_time); |
|
1892 | - return ''; |
|
1893 | - } |
|
1894 | - return $this->get($field_name, $date_or_time); |
|
1895 | - } |
|
1896 | - |
|
1897 | - |
|
1898 | - /** |
|
1899 | - * @param string $field_name |
|
1900 | - * @param string $format |
|
1901 | - * @throws InvalidArgumentException |
|
1902 | - * @throws InvalidInterfaceException |
|
1903 | - * @throws InvalidDataTypeException |
|
1904 | - * @throws EE_Error |
|
1905 | - */ |
|
1906 | - public function e_date(string $field_name, $format = '') |
|
1907 | - { |
|
1908 | - $this->_get_datetime($field_name, $format, null, 'D', true); |
|
1909 | - } |
|
1910 | - |
|
1911 | - |
|
1912 | - /** |
|
1913 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
1914 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
1915 | - * other echoes the pretty value for dtt) |
|
1916 | - * |
|
1917 | - * @param string $field_name name of model object datetime field holding the value |
|
1918 | - * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
1919 | - * @return string datetime value formatted |
|
1920 | - * @throws InvalidArgumentException |
|
1921 | - * @throws InvalidInterfaceException |
|
1922 | - * @throws InvalidDataTypeException |
|
1923 | - * @throws EE_Error |
|
1924 | - */ |
|
1925 | - public function get_time(string $field_name, string $format = '') |
|
1926 | - { |
|
1927 | - return $this->_get_datetime($field_name, null, $format, 'T'); |
|
1928 | - } |
|
1929 | - |
|
1930 | - |
|
1931 | - /** |
|
1932 | - * @param string $field_name |
|
1933 | - * @param string $format |
|
1934 | - * @throws InvalidArgumentException |
|
1935 | - * @throws InvalidInterfaceException |
|
1936 | - * @throws InvalidDataTypeException |
|
1937 | - * @throws EE_Error |
|
1938 | - */ |
|
1939 | - public function e_time(string $field_name, string $format = '') |
|
1940 | - { |
|
1941 | - $this->_get_datetime($field_name, null, $format, 'T', true); |
|
1942 | - } |
|
1943 | - |
|
1944 | - |
|
1945 | - /** |
|
1946 | - * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
1947 | - * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
1948 | - * other echoes the pretty value for dtt) |
|
1949 | - * |
|
1950 | - * @param string $field_name name of model object datetime field holding the value |
|
1951 | - * @param string $date_format format for the date returned (if NULL we use default in dt_frmt property) |
|
1952 | - * @param string $time_format format for the time returned (if NULL we use default in tm_frmt property) |
|
1953 | - * @return string datetime value formatted |
|
1954 | - * @throws InvalidArgumentException |
|
1955 | - * @throws InvalidInterfaceException |
|
1956 | - * @throws InvalidDataTypeException |
|
1957 | - * @throws EE_Error |
|
1958 | - */ |
|
1959 | - public function get_datetime(string $field_name, $date_format = '', $time_format = '') |
|
1960 | - { |
|
1961 | - return $this->_get_datetime($field_name, $date_format, $time_format); |
|
1962 | - } |
|
1963 | - |
|
1964 | - |
|
1965 | - /** |
|
1966 | - * @param string $field_name |
|
1967 | - * @param string $date_format |
|
1968 | - * @param string $time_format |
|
1969 | - * @throws InvalidArgumentException |
|
1970 | - * @throws InvalidInterfaceException |
|
1971 | - * @throws InvalidDataTypeException |
|
1972 | - * @throws EE_Error |
|
1973 | - */ |
|
1974 | - public function e_datetime(string $field_name, $date_format = '', $time_format = '') |
|
1975 | - { |
|
1976 | - $this->_get_datetime($field_name, $date_format, $time_format, null, true); |
|
1977 | - } |
|
1978 | - |
|
1979 | - |
|
1980 | - /** |
|
1981 | - * Get the i8ln value for a date using the WordPress |
|
1982 | - * |
|
1983 | - * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
1984 | - * @param string $format PHP valid date/time string format. |
|
1985 | - * If none is provided then the internal set format on the object will be used. |
|
1986 | - * @return string Date and time string in set locale or false if no field exists for the given |
|
1987 | - * @throws InvalidArgumentException |
|
1988 | - * @throws InvalidInterfaceException |
|
1989 | - * @throws InvalidDataTypeException |
|
1990 | - * @throws EE_Error |
|
1991 | - * field name. |
|
1992 | - * @see date_i18n function. |
|
1993 | - */ |
|
1994 | - public function get_i18n_datetime(string $field_name, $format = '') |
|
1995 | - { |
|
1996 | - $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
1997 | - return date_i18n( |
|
1998 | - $format, |
|
1999 | - EEH_DTT_Helper::get_timestamp_with_offset( |
|
2000 | - $this->get_raw($field_name), |
|
2001 | - $this->get_timezone() |
|
2002 | - ) |
|
2003 | - ); |
|
2004 | - } |
|
2005 | - |
|
2006 | - |
|
2007 | - /** |
|
2008 | - * This method simply returns the RAW unprocessed value for the given property in this class |
|
2009 | - * |
|
2010 | - * @param string $field_name A valid field name |
|
2011 | - * @return mixed Whatever the raw value stored on the property is. |
|
2012 | - * @throws InvalidArgumentException |
|
2013 | - * @throws InvalidInterfaceException |
|
2014 | - * @throws InvalidDataTypeException |
|
2015 | - * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
2016 | - */ |
|
2017 | - public function get_raw(string $field_name) |
|
2018 | - { |
|
2019 | - $field_settings = $this->_model->field_settings_for($field_name); |
|
2020 | - return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime |
|
2021 | - ? $this->_fields[ $field_name ]->format('U') |
|
2022 | - : $this->_fields[ $field_name ]; |
|
2023 | - } |
|
2024 | - |
|
2025 | - |
|
2026 | - /** |
|
2027 | - * This will return a timestamp for the website timezone |
|
2028 | - * but ONLY when the current website timezone is different |
|
2029 | - * than the timezone set for the website. |
|
2030 | - * NOTE, this currently only works well with methods that return values. |
|
2031 | - * If you use it with methods that echo values |
|
2032 | - * the $_timestamp property may not get reset to its original value |
|
2033 | - * and that could lead to some unexpected results! |
|
2034 | - * |
|
2035 | - * @param string $field_name This is the name of the field on the object that contains the date/time |
|
2036 | - * value being returned. |
|
2037 | - * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
2038 | - * @param array|string $args This is the arguments that will be passed to the callback. |
|
2039 | - * @param string $prepend You can include something to prepend on the timestamp |
|
2040 | - * @param string $append You can include something to append on the timestamp |
|
2041 | - * @return string timestamp |
|
2042 | - * @throws InvalidArgumentException |
|
2043 | - * @throws InvalidInterfaceException |
|
2044 | - * @throws InvalidDataTypeException |
|
2045 | - * @throws EE_Error |
|
2046 | - */ |
|
2047 | - public function display_in_my_timezone( |
|
2048 | - string $field_name, |
|
2049 | - $callback = 'get_datetime', |
|
2050 | - $args = null, |
|
2051 | - $prepend = '', |
|
2052 | - $append = '' |
|
2053 | - ) { |
|
2054 | - $timezone = EEH_DTT_Helper::get_timezone(); |
|
2055 | - if ($timezone === $this->_timezone) { |
|
2056 | - return ''; |
|
2057 | - } |
|
2058 | - $original_timezone = $this->_timezone; |
|
2059 | - $this->set_timezone($timezone); |
|
2060 | - $fn = (array) $field_name; |
|
2061 | - $args = array_merge($fn, (array) $args); |
|
2062 | - if (! method_exists($this, $callback)) { |
|
2063 | - throw new EE_Error( |
|
2064 | - sprintf( |
|
2065 | - esc_html__( |
|
2066 | - 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
2067 | - 'event_espresso' |
|
2068 | - ), |
|
2069 | - $callback |
|
2070 | - ) |
|
2071 | - ); |
|
2072 | - } |
|
2073 | - $args = (array) $args; |
|
2074 | - $return = $prepend . call_user_func_array([$this, $callback], $args) . $append; |
|
2075 | - $this->set_timezone($original_timezone); |
|
2076 | - return $return; |
|
2077 | - } |
|
2078 | - |
|
2079 | - |
|
2080 | - /** |
|
2081 | - * Deletes this model object. |
|
2082 | - * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
2083 | - * override |
|
2084 | - * `EE_Base_Class::_delete` NOT this class. |
|
2085 | - * |
|
2086 | - * @return boolean | int |
|
2087 | - * @throws ReflectionException |
|
2088 | - * @throws InvalidArgumentException |
|
2089 | - * @throws InvalidInterfaceException |
|
2090 | - * @throws InvalidDataTypeException |
|
2091 | - * @throws EE_Error |
|
2092 | - */ |
|
2093 | - public function delete() |
|
2094 | - { |
|
2095 | - /** |
|
2096 | - * Called just before the `EE_Base_Class::_delete` method call. |
|
2097 | - * Note: |
|
2098 | - * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
2099 | - * should be aware that `_delete` may not always result in a permanent delete. |
|
2100 | - * For example, `EE_Soft_Delete_Base_Class::_delete` |
|
2101 | - * soft deletes (trash) the object and does not permanently delete it. |
|
2102 | - * |
|
2103 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
2104 | - */ |
|
2105 | - do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
2106 | - $result = $this->_delete(); |
|
2107 | - /** |
|
2108 | - * Called just after the `EE_Base_Class::_delete` method call. |
|
2109 | - * Note: |
|
2110 | - * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
2111 | - * should be aware that `_delete` may not always result in a permanent delete. |
|
2112 | - * For example `EE_Soft_Base_Class::_delete` |
|
2113 | - * soft deletes (trash) the object and does not permanently delete it. |
|
2114 | - * |
|
2115 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
2116 | - * @param boolean $result |
|
2117 | - */ |
|
2118 | - do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
2119 | - return $result; |
|
2120 | - } |
|
2121 | - |
|
2122 | - |
|
2123 | - /** |
|
2124 | - * Calls the specific delete method for the instantiated class. |
|
2125 | - * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
2126 | - * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
2127 | - * `EE_Base_Class::delete` |
|
2128 | - * |
|
2129 | - * @return bool|int |
|
2130 | - * @throws ReflectionException |
|
2131 | - * @throws InvalidArgumentException |
|
2132 | - * @throws InvalidInterfaceException |
|
2133 | - * @throws InvalidDataTypeException |
|
2134 | - * @throws EE_Error |
|
2135 | - */ |
|
2136 | - protected function _delete() |
|
2137 | - { |
|
2138 | - return $this->delete_permanently(); |
|
2139 | - } |
|
2140 | - |
|
2141 | - |
|
2142 | - /** |
|
2143 | - * Deletes this model object permanently from db |
|
2144 | - * (but keep in mind related models may block the delete and return an error) |
|
2145 | - * |
|
2146 | - * @return bool | int |
|
2147 | - * @throws ReflectionException |
|
2148 | - * @throws InvalidArgumentException |
|
2149 | - * @throws InvalidInterfaceException |
|
2150 | - * @throws InvalidDataTypeException |
|
2151 | - * @throws EE_Error |
|
2152 | - */ |
|
2153 | - public function delete_permanently() |
|
2154 | - { |
|
2155 | - /** |
|
2156 | - * Called just before HARD deleting a model object |
|
2157 | - * |
|
2158 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
2159 | - */ |
|
2160 | - do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
2161 | - $result = $this->_model->delete_permanently_by_ID($this->ID()); |
|
2162 | - $this->refresh_cache_of_related_objects(); |
|
2163 | - /** |
|
2164 | - * Called just after HARD deleting a model object |
|
2165 | - * |
|
2166 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
2167 | - * @param boolean $result |
|
2168 | - */ |
|
2169 | - do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
2170 | - return $result; |
|
2171 | - } |
|
2172 | - |
|
2173 | - |
|
2174 | - /** |
|
2175 | - * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
2176 | - * related model objects |
|
2177 | - * |
|
2178 | - * @throws ReflectionException |
|
2179 | - * @throws InvalidArgumentException |
|
2180 | - * @throws InvalidInterfaceException |
|
2181 | - * @throws InvalidDataTypeException |
|
2182 | - * @throws EE_Error |
|
2183 | - */ |
|
2184 | - public function refresh_cache_of_related_objects() |
|
2185 | - { |
|
2186 | - foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
2187 | - if (! empty($this->_model_relations[ $relation_name ])) { |
|
2188 | - $related_objects = $this->_model_relations[ $relation_name ]; |
|
2189 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
2190 | - // this relation only stores a single model object, not an array |
|
2191 | - // but let's make it consistent |
|
2192 | - $related_objects = [$related_objects]; |
|
2193 | - } |
|
2194 | - foreach ($related_objects as $related_object) { |
|
2195 | - // only refresh their cache if they're in memory |
|
2196 | - if ($related_object instanceof EE_Base_Class) { |
|
2197 | - $related_object->clear_cache( |
|
2198 | - $this->_model->get_this_model_name(), |
|
2199 | - $this |
|
2200 | - ); |
|
2201 | - } |
|
2202 | - } |
|
2203 | - } |
|
2204 | - } |
|
2205 | - } |
|
2206 | - |
|
2207 | - |
|
2208 | - /** |
|
2209 | - * Forgets the cached model of the given relation Name. So the next time we request it, |
|
2210 | - * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
2211 | - * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
2212 | - * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
2213 | - * |
|
2214 | - * @param string $relationName one of the keys in the _model_relations array on the model. |
|
2215 | - * Eg 'Registration' |
|
2216 | - * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
2217 | - * if you intend to use $clear_all = TRUE, or the relation only |
|
2218 | - * has 1 object anyways (ie, it's a BelongsToRelation) |
|
2219 | - * @param bool $clear_all This flags clearing the entire cache relation property if |
|
2220 | - * this is HasMany or HABTM. |
|
2221 | - * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
2222 | - * relation from all |
|
2223 | - * @throws InvalidArgumentException |
|
2224 | - * @throws InvalidInterfaceException |
|
2225 | - * @throws InvalidDataTypeException |
|
2226 | - * @throws EE_Error |
|
2227 | - * @throws ReflectionException |
|
2228 | - */ |
|
2229 | - public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
2230 | - { |
|
2231 | - $relationship_to_model = $this->_model->related_settings_for($relationName); |
|
2232 | - $index_in_cache = ''; |
|
2233 | - if (! $relationship_to_model) { |
|
2234 | - throw new EE_Error( |
|
2235 | - sprintf( |
|
2236 | - esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'), |
|
2237 | - $relationName, |
|
2238 | - get_class($this) |
|
2239 | - ) |
|
2240 | - ); |
|
2241 | - } |
|
2242 | - if ($clear_all) { |
|
2243 | - $obj_removed = true; |
|
2244 | - $this->_model_relations[ $relationName ] = null; |
|
2245 | - } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
2246 | - $obj_removed = $this->_model_relations[ $relationName ]; |
|
2247 | - $this->_model_relations[ $relationName ] = null; |
|
2248 | - } else { |
|
2249 | - if ( |
|
2250 | - $object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
2251 | - && $object_to_remove_or_index_into_array->ID() |
|
2252 | - ) { |
|
2253 | - $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
2254 | - if ( |
|
2255 | - is_array($this->_model_relations[ $relationName ]) |
|
2256 | - && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ]) |
|
2257 | - ) { |
|
2258 | - $index_found_at = null; |
|
2259 | - // find this object in the array even though it has a different key |
|
2260 | - foreach ($this->_model_relations[ $relationName ] as $index => $obj) { |
|
2261 | - /** @noinspection TypeUnsafeComparisonInspection */ |
|
2262 | - if ( |
|
2263 | - $obj instanceof EE_Base_Class |
|
2264 | - && ( |
|
2265 | - $obj == $object_to_remove_or_index_into_array |
|
2266 | - || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
2267 | - ) |
|
2268 | - ) { |
|
2269 | - $index_found_at = $index; |
|
2270 | - break; |
|
2271 | - } |
|
2272 | - } |
|
2273 | - if ($index_found_at) { |
|
2274 | - $index_in_cache = $index_found_at; |
|
2275 | - } else { |
|
2276 | - // it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
2277 | - // if it wasn't in it to begin with. So we're done |
|
2278 | - return $object_to_remove_or_index_into_array; |
|
2279 | - } |
|
2280 | - } |
|
2281 | - } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
2282 | - // so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
2283 | - foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
2284 | - /** @noinspection TypeUnsafeComparisonInspection */ |
|
2285 | - if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
2286 | - $index_in_cache = $index; |
|
2287 | - } |
|
2288 | - } |
|
2289 | - } else { |
|
2290 | - $index_in_cache = $object_to_remove_or_index_into_array; |
|
2291 | - } |
|
2292 | - // supposedly we've found it. But it could just be that the client code |
|
2293 | - // provided a bad index/object |
|
2294 | - if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) { |
|
2295 | - $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ]; |
|
2296 | - unset($this->_model_relations[ $relationName ][ $index_in_cache ]); |
|
2297 | - } else { |
|
2298 | - // that thing was never cached anyways. |
|
2299 | - $obj_removed = null; |
|
2300 | - } |
|
2301 | - } |
|
2302 | - return $obj_removed; |
|
2303 | - } |
|
2304 | - |
|
2305 | - |
|
2306 | - /** |
|
2307 | - * Saves this model object and its NEW cached relations to the database. |
|
2308 | - * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
2309 | - * In order for that to work, we would need to mark model objects as dirty/clean... |
|
2310 | - * because otherwise, there's a potential for infinite looping of saving |
|
2311 | - * Saves the cached related model objects, and ensures the relation between them |
|
2312 | - * and this object and properly setup |
|
2313 | - * |
|
2314 | - * @return int ID of new model object on save; 0 on failure+ |
|
2315 | - * @throws ReflectionException |
|
2316 | - * @throws InvalidArgumentException |
|
2317 | - * @throws InvalidInterfaceException |
|
2318 | - * @throws InvalidDataTypeException |
|
2319 | - * @throws EE_Error |
|
2320 | - */ |
|
2321 | - public function save_new_cached_related_model_objs() |
|
2322 | - { |
|
2323 | - // make sure this has been saved |
|
2324 | - if (! $this->ID()) { |
|
2325 | - $id = $this->save(); |
|
2326 | - } else { |
|
2327 | - $id = $this->ID(); |
|
2328 | - } |
|
2329 | - // now save all the NEW cached model objects (ie they don't exist in the DB) |
|
2330 | - foreach ($this->_model->relation_settings() as $relationName => $relationObj) { |
|
2331 | - if ($this->_model_relations[ $relationName ]) { |
|
2332 | - // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
2333 | - // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
2334 | - /* @var $related_model_obj EE_Base_Class */ |
|
2335 | - if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
2336 | - // add a relation to that relation type (which saves the appropriate thing in the process) |
|
2337 | - // but ONLY if it DOES NOT exist in the DB |
|
2338 | - $related_model_obj = $this->_model_relations[ $relationName ]; |
|
2339 | - // if( ! $related_model_obj->ID()){ |
|
2340 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
2341 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
2342 | - // } |
|
2343 | - } else { |
|
2344 | - foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
2345 | - // add a relation to that relation type (which saves the appropriate thing in the process) |
|
2346 | - // but ONLY if it DOES NOT exist in the DB |
|
2347 | - // if( ! $related_model_obj->ID()){ |
|
2348 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
2349 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
2350 | - // } |
|
2351 | - } |
|
2352 | - } |
|
2353 | - } |
|
2354 | - } |
|
2355 | - return $id; |
|
2356 | - } |
|
2357 | - |
|
2358 | - |
|
2359 | - /** |
|
2360 | - * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
2361 | - * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
2362 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
2363 | - * |
|
2364 | - * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
2365 | - * @param string $relationName eg 'Events','Question',etc. |
|
2366 | - * an attendee to a group, you also want to specify which role they |
|
2367 | - * will have in that group. So you would use this parameter to |
|
2368 | - * specify array('role-column-name'=>'role-id') |
|
2369 | - * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
2370 | - * allow you to further constrict the relation to being added. |
|
2371 | - * However, keep in mind that the columns (keys) given must match a |
|
2372 | - * column on the JOIN table and currently only the HABTM models |
|
2373 | - * accept these additional conditions. Also remember that if an |
|
2374 | - * exact match isn't found for these extra cols/val pairs, then a |
|
2375 | - * NEW row is created in the join table. |
|
2376 | - * @param null $cache_id |
|
2377 | - * @return EE_Base_Class the object the relation was added to |
|
2378 | - * @throws ReflectionException |
|
2379 | - * @throws InvalidArgumentException |
|
2380 | - * @throws InvalidInterfaceException |
|
2381 | - * @throws InvalidDataTypeException |
|
2382 | - * @throws EE_Error |
|
2383 | - */ |
|
2384 | - public function _add_relation_to( |
|
2385 | - $otherObjectModelObjectOrID, |
|
2386 | - $relationName, |
|
2387 | - $extra_join_model_fields_n_values = [], |
|
2388 | - $cache_id = null |
|
2389 | - ) { |
|
2390 | - // if this thing exists in the DB, save the relation to the DB |
|
2391 | - if ($this->ID()) { |
|
2392 | - $otherObject = $this->_model->add_relationship_to( |
|
2393 | - $this, |
|
2394 | - $otherObjectModelObjectOrID, |
|
2395 | - $relationName, |
|
2396 | - $extra_join_model_fields_n_values |
|
2397 | - ); |
|
2398 | - // clear cache so future get_many_related and get_first_related() return new results. |
|
2399 | - $this->clear_cache($relationName, $otherObject, true); |
|
2400 | - if ($otherObject instanceof EE_Base_Class) { |
|
2401 | - $otherObject->clear_cache($this->_model->get_this_model_name(), $this); |
|
2402 | - } |
|
2403 | - } else { |
|
2404 | - // this thing doesn't exist in the DB, so just cache it |
|
2405 | - if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
2406 | - throw new EE_Error( |
|
2407 | - sprintf( |
|
2408 | - esc_html__( |
|
2409 | - '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', |
|
2410 | - 'event_espresso' |
|
2411 | - ), |
|
2412 | - $otherObjectModelObjectOrID, |
|
2413 | - get_class($this) |
|
2414 | - ) |
|
2415 | - ); |
|
2416 | - } |
|
2417 | - $otherObject = $otherObjectModelObjectOrID; |
|
2418 | - $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
2419 | - } |
|
2420 | - if ($otherObject instanceof EE_Base_Class) { |
|
2421 | - // fix the reciprocal relation too |
|
2422 | - if ($otherObject->ID()) { |
|
2423 | - // its saved so assumed relations exist in the DB, so we can just |
|
2424 | - // clear the cache so future queries use the updated info in the DB |
|
2425 | - $otherObject->clear_cache( |
|
2426 | - $this->_model->get_this_model_name(), |
|
2427 | - null, |
|
2428 | - true |
|
2429 | - ); |
|
2430 | - } else { |
|
2431 | - // it's not saved, so it caches relations like this |
|
2432 | - $otherObject->cache($this->_model->get_this_model_name(), $this); |
|
2433 | - } |
|
2434 | - } |
|
2435 | - return $otherObject; |
|
2436 | - } |
|
2437 | - |
|
2438 | - |
|
2439 | - /** |
|
2440 | - * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
2441 | - * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
2442 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
2443 | - * from the cache |
|
2444 | - * |
|
2445 | - * @param mixed $otherObjectModelObjectOrID |
|
2446 | - * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
2447 | - * to the DB yet |
|
2448 | - * @param string $relationName |
|
2449 | - * @param array $where_query |
|
2450 | - * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
2451 | - * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
2452 | - * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
2453 | - * remember that if an exact match isn't found for these extra cols/val pairs, then no row is |
|
2454 | - * deleted. |
|
2455 | - * @return EE_Base_Class the relation was removed from |
|
2456 | - * @throws ReflectionException |
|
2457 | - * @throws InvalidArgumentException |
|
2458 | - * @throws InvalidInterfaceException |
|
2459 | - * @throws InvalidDataTypeException |
|
2460 | - * @throws EE_Error |
|
2461 | - */ |
|
2462 | - public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = []) |
|
2463 | - { |
|
2464 | - if ($this->ID()) { |
|
2465 | - // if this exists in the DB, save the relation change to the DB too |
|
2466 | - $otherObject = $this->_model->remove_relationship_to( |
|
2467 | - $this, |
|
2468 | - $otherObjectModelObjectOrID, |
|
2469 | - $relationName, |
|
2470 | - $where_query |
|
2471 | - ); |
|
2472 | - $this->clear_cache( |
|
2473 | - $relationName, |
|
2474 | - $otherObject |
|
2475 | - ); |
|
2476 | - } else { |
|
2477 | - // this doesn't exist in the DB, just remove it from the cache |
|
2478 | - $otherObject = $this->clear_cache( |
|
2479 | - $relationName, |
|
2480 | - $otherObjectModelObjectOrID |
|
2481 | - ); |
|
2482 | - } |
|
2483 | - if ($otherObject instanceof EE_Base_Class) { |
|
2484 | - $otherObject->clear_cache( |
|
2485 | - $this->_model->get_this_model_name(), |
|
2486 | - $this |
|
2487 | - ); |
|
2488 | - } |
|
2489 | - return $otherObject; |
|
2490 | - } |
|
2491 | - |
|
2492 | - |
|
2493 | - /** |
|
2494 | - * Removes ALL the related things for the $relationName. |
|
2495 | - * |
|
2496 | - * @param string $relationName |
|
2497 | - * @param array $where_query_params |
|
2498 | - * @return EE_Base_Class |
|
2499 | - * @throws ReflectionException |
|
2500 | - * @throws InvalidArgumentException |
|
2501 | - * @throws InvalidInterfaceException |
|
2502 | - * @throws InvalidDataTypeException |
|
2503 | - * @throws EE_Error |
|
2504 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
2505 | - */ |
|
2506 | - public function _remove_relations($relationName, $where_query_params = []) |
|
2507 | - { |
|
2508 | - if ($this->ID()) { |
|
2509 | - // if this exists in the DB, save the relation change to the DB too |
|
2510 | - $otherObjects = $this->_model->remove_relations( |
|
2511 | - $this, |
|
2512 | - $relationName, |
|
2513 | - $where_query_params |
|
2514 | - ); |
|
2515 | - $this->clear_cache( |
|
2516 | - $relationName, |
|
2517 | - null, |
|
2518 | - true |
|
2519 | - ); |
|
2520 | - } else { |
|
2521 | - // this doesn't exist in the DB, just remove it from the cache |
|
2522 | - $otherObjects = $this->clear_cache( |
|
2523 | - $relationName, |
|
2524 | - null, |
|
2525 | - true |
|
2526 | - ); |
|
2527 | - } |
|
2528 | - if (is_array($otherObjects)) { |
|
2529 | - foreach ($otherObjects as $otherObject) { |
|
2530 | - $otherObject->clear_cache( |
|
2531 | - $this->_model->get_this_model_name(), |
|
2532 | - $this |
|
2533 | - ); |
|
2534 | - } |
|
2535 | - } |
|
2536 | - return $otherObjects; |
|
2537 | - } |
|
2538 | - |
|
2539 | - |
|
2540 | - /** |
|
2541 | - * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
2542 | - * unless otherwise specified in the $query_params |
|
2543 | - * |
|
2544 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
2545 | - * @param array $query_params |
|
2546 | - * @param string $field_to_count name of field to count by. By default, uses primary key |
|
2547 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
2548 | - * that by the setting $distinct to TRUE; |
|
2549 | - * @return int |
|
2550 | - * @throws ReflectionException |
|
2551 | - * @throws InvalidArgumentException |
|
2552 | - * @throws InvalidInterfaceException |
|
2553 | - * @throws InvalidDataTypeException |
|
2554 | - * @throws EE_Error |
|
2555 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2556 | - */ |
|
2557 | - public function count_related($relation_name, $query_params = [], $field_to_count = null, $distinct = false) |
|
2558 | - { |
|
2559 | - return $this->_model->count_related( |
|
2560 | - $this, |
|
2561 | - $relation_name, |
|
2562 | - $query_params, |
|
2563 | - $field_to_count, |
|
2564 | - $distinct |
|
2565 | - ); |
|
2566 | - } |
|
2567 | - |
|
2568 | - |
|
2569 | - /** |
|
2570 | - * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
2571 | - * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
2572 | - * |
|
2573 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
2574 | - * @param array $query_params |
|
2575 | - * @param string $field_to_sum name of field to count by. |
|
2576 | - * By default, uses primary key |
|
2577 | - * (which doesn't make much sense, so you should probably change it) |
|
2578 | - * @return int |
|
2579 | - * @throws ReflectionException |
|
2580 | - * @throws InvalidArgumentException |
|
2581 | - * @throws InvalidInterfaceException |
|
2582 | - * @throws InvalidDataTypeException |
|
2583 | - * @throws EE_Error |
|
2584 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2585 | - */ |
|
2586 | - public function sum_related($relation_name, $query_params = [], $field_to_sum = null) |
|
2587 | - { |
|
2588 | - return $this->_model->sum_related( |
|
2589 | - $this, |
|
2590 | - $relation_name, |
|
2591 | - $query_params, |
|
2592 | - $field_to_sum |
|
2593 | - ); |
|
2594 | - } |
|
2595 | - |
|
2596 | - |
|
2597 | - /** |
|
2598 | - * Does a delete on all related objects of type $relationName and removes |
|
2599 | - * the current model object's relation to them. If they can't be deleted (because |
|
2600 | - * of blocking related model objects) does nothing. If the related model objects are |
|
2601 | - * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
2602 | - * If this model object doesn't exist yet in the DB, just removes its related things |
|
2603 | - * |
|
2604 | - * @param string $relationName |
|
2605 | - * @param array $query_params |
|
2606 | - * @return int how many deleted |
|
2607 | - * @throws ReflectionException |
|
2608 | - * @throws InvalidArgumentException |
|
2609 | - * @throws InvalidInterfaceException |
|
2610 | - * @throws InvalidDataTypeException |
|
2611 | - * @throws EE_Error |
|
2612 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2613 | - */ |
|
2614 | - public function delete_related($relationName, $query_params = []) |
|
2615 | - { |
|
2616 | - if ($this->ID()) { |
|
2617 | - $count = $this->_model->delete_related( |
|
2618 | - $this, |
|
2619 | - $relationName, |
|
2620 | - $query_params |
|
2621 | - ); |
|
2622 | - } else { |
|
2623 | - $count = count($this->get_all_from_cache($relationName)); |
|
2624 | - $this->clear_cache($relationName, null, true); |
|
2625 | - } |
|
2626 | - return $count; |
|
2627 | - } |
|
2628 | - |
|
2629 | - |
|
2630 | - /** |
|
2631 | - * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
2632 | - * the current model object's relation to them. If they can't be deleted (because |
|
2633 | - * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
2634 | - * If the related thing isn't a soft-deletable model object, this function is identical |
|
2635 | - * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
2636 | - * |
|
2637 | - * @param string $relationName |
|
2638 | - * @param array $query_params |
|
2639 | - * @return int how many deleted (including those soft deleted) |
|
2640 | - * @throws ReflectionException |
|
2641 | - * @throws InvalidArgumentException |
|
2642 | - * @throws InvalidInterfaceException |
|
2643 | - * @throws InvalidDataTypeException |
|
2644 | - * @throws EE_Error |
|
2645 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2646 | - */ |
|
2647 | - public function delete_related_permanently($relationName, $query_params = []) |
|
2648 | - { |
|
2649 | - if ($this->ID()) { |
|
2650 | - $count = $this->_model->delete_related_permanently( |
|
2651 | - $this, |
|
2652 | - $relationName, |
|
2653 | - $query_params |
|
2654 | - ); |
|
2655 | - } else { |
|
2656 | - $count = count($this->get_all_from_cache($relationName)); |
|
2657 | - } |
|
2658 | - $this->clear_cache($relationName, null, true); |
|
2659 | - return $count; |
|
2660 | - } |
|
2661 | - |
|
2662 | - |
|
2663 | - /** |
|
2664 | - * is_set |
|
2665 | - * Just a simple utility function children can use for checking if property exists |
|
2666 | - * |
|
2667 | - * @param string $field_name property to check |
|
2668 | - * @return bool TRUE if existing, FALSE if not. |
|
2669 | - */ |
|
2670 | - public function is_set(string $field_name) |
|
2671 | - { |
|
2672 | - return isset($this->_fields[ $field_name ]); |
|
2673 | - } |
|
2674 | - |
|
2675 | - |
|
2676 | - /** |
|
2677 | - * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
2678 | - * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
2679 | - * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. |
|
2680 | - * Instead of requiring a plugin to extend the EE_Base_Class |
|
2681 | - * (which works fine is there's only 1 plugin, but when will that happen?) |
|
2682 | - * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' |
|
2683 | - * (eg, filters_hook_espresso__EE_Answer__my_great_function) |
|
2684 | - * and accepts 2 arguments: the object on which the function was called, |
|
2685 | - * and an array of the original arguments passed to the function. |
|
2686 | - * Whatever their callback function returns will be returned by this function. |
|
2687 | - * Example: in functions.php (or in a plugin): |
|
2688 | - * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); |
|
2689 | - * function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
2690 | - * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
2691 | - * return $previousReturnValue.$returnString; |
|
2692 | - * } |
|
2693 | - * require('EE_Answer.class.php'); |
|
2694 | - * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
2695 | - * echo $answer->my_callback('monkeys',100); |
|
2696 | - * //will output "you called my_callback! and passed args:monkeys,100" |
|
2697 | - * |
|
2698 | - * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
2699 | - * @param array $args array of original arguments passed to the function |
|
2700 | - * @return mixed whatever the plugin which calls add_filter decides |
|
2701 | - * @throws EE_Error |
|
2702 | - */ |
|
2703 | - public function __call($methodName, $args) |
|
2704 | - { |
|
2705 | - $className = get_class($this); |
|
2706 | - $tagName = "FHEE__{$className}__{$methodName}"; |
|
2707 | - if (! has_filter($tagName)) { |
|
2708 | - throw new EE_Error( |
|
2709 | - sprintf( |
|
2710 | - esc_html__( |
|
2711 | - "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;}", |
|
2712 | - 'event_espresso' |
|
2713 | - ), |
|
2714 | - $methodName, |
|
2715 | - $className, |
|
2716 | - $tagName |
|
2717 | - ) |
|
2718 | - ); |
|
2719 | - } |
|
2720 | - return apply_filters($tagName, null, $this, $args); |
|
2721 | - } |
|
2722 | - |
|
2723 | - |
|
2724 | - /** |
|
2725 | - * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
2726 | - * is specified, only deletes extra meta records with that value. |
|
2727 | - * |
|
2728 | - * @param string $meta_key |
|
2729 | - * @param mixed|null $meta_value |
|
2730 | - * @return int number of extra meta rows deleted |
|
2731 | - * @throws InvalidArgumentException |
|
2732 | - * @throws InvalidInterfaceException |
|
2733 | - * @throws InvalidDataTypeException |
|
2734 | - * @throws EE_Error |
|
2735 | - * @throws ReflectionException |
|
2736 | - */ |
|
2737 | - public function delete_extra_meta(string $meta_key, $meta_value = null): int |
|
2738 | - { |
|
2739 | - $query_params = [ |
|
2740 | - [ |
|
2741 | - 'EXM_key' => $meta_key, |
|
2742 | - 'OBJ_ID' => $this->ID(), |
|
2743 | - 'EXM_type' => $this->_model->get_this_model_name(), |
|
2744 | - ], |
|
2745 | - ]; |
|
2746 | - if ($meta_value !== null) { |
|
2747 | - $query_params[0]['EXM_value'] = $meta_value; |
|
2748 | - } |
|
2749 | - return EEM_Extra_Meta::instance()->delete($query_params); |
|
2750 | - } |
|
2751 | - |
|
2752 | - |
|
2753 | - /** |
|
2754 | - * Returns a simple array of all the extra meta associated with this model object. |
|
2755 | - * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
2756 | - * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
2757 | - * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
2758 | - * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
2759 | - * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
2760 | - * finally the extra meta's value as each sub-value. (eg |
|
2761 | - * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
2762 | - * |
|
2763 | - * @param boolean $one_of_each_key |
|
2764 | - * @return array |
|
2765 | - * @throws ReflectionException |
|
2766 | - * @throws InvalidArgumentException |
|
2767 | - * @throws InvalidInterfaceException |
|
2768 | - * @throws InvalidDataTypeException |
|
2769 | - * @throws EE_Error |
|
2770 | - */ |
|
2771 | - public function all_extra_meta_array($one_of_each_key = true) |
|
2772 | - { |
|
2773 | - $return_array = []; |
|
2774 | - if ($one_of_each_key) { |
|
2775 | - $extra_meta_objs = $this->get_many_related( |
|
2776 | - 'Extra_Meta', |
|
2777 | - ['group_by' => 'EXM_key'] |
|
2778 | - ); |
|
2779 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
2780 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
2781 | - $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
2782 | - } |
|
2783 | - } |
|
2784 | - } else { |
|
2785 | - $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
2786 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
2787 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
2788 | - if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
2789 | - $return_array[ $extra_meta_obj->key() ] = []; |
|
2790 | - } |
|
2791 | - $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
2792 | - } |
|
2793 | - } |
|
2794 | - } |
|
2795 | - return $return_array; |
|
2796 | - } |
|
2797 | - |
|
2798 | - |
|
2799 | - /** |
|
2800 | - * refresh_from_db |
|
2801 | - * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
2802 | - * |
|
2803 | - * @throws ReflectionException |
|
2804 | - * @throws InvalidArgumentException |
|
2805 | - * @throws InvalidInterfaceException |
|
2806 | - * @throws InvalidDataTypeException |
|
2807 | - * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
2808 | - * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
2809 | - */ |
|
2810 | - public function refresh_from_db() |
|
2811 | - { |
|
2812 | - if ($this->ID() && $this->in_entity_map()) { |
|
2813 | - $this->_model->refresh_entity_map_from_db($this->ID()); |
|
2814 | - } else { |
|
2815 | - // if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
2816 | - // if it has an ID but it's not in the map, and you're asking me to refresh it |
|
2817 | - // that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
2818 | - // absolutely nothing in it for this ID |
|
2819 | - if (WP_DEBUG) { |
|
2820 | - throw new EE_Error( |
|
2821 | - sprintf( |
|
2822 | - esc_html__( |
|
2823 | - '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.', |
|
2824 | - 'event_espresso' |
|
2825 | - ), |
|
2826 | - $this->ID(), |
|
2827 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
2828 | - get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
2829 | - ) |
|
2830 | - ); |
|
2831 | - } |
|
2832 | - } |
|
2833 | - } |
|
2834 | - |
|
2835 | - |
|
2836 | - /** |
|
2837 | - * Nudges $field_name's value by $quantity, without any conditionals (in comparison to bumpConditionally()). |
|
2838 | - * Does not allow negative values, however. |
|
2839 | - * |
|
2840 | - * @param array $fields_n_quantities keys are the field names, and values are the amount by which to bump them |
|
2841 | - * (positive or negative). One important gotcha: all these values must be |
|
2842 | - * on the same table (eg don't pass in one field for the posts table and |
|
2843 | - * another for the event meta table.) |
|
2844 | - * @return bool |
|
2845 | - * @throws EE_Error |
|
2846 | - * @throws InvalidArgumentException |
|
2847 | - * @throws InvalidDataTypeException |
|
2848 | - * @throws InvalidInterfaceException |
|
2849 | - * @throws ReflectionException |
|
2850 | - * @since 4.9.80.p |
|
2851 | - */ |
|
2852 | - public function adjustNumericFieldsInDb(array $fields_n_quantities) |
|
2853 | - { |
|
2854 | - global $wpdb; |
|
2855 | - if (empty($fields_n_quantities)) { |
|
2856 | - // No fields to update? Well sure, we updated them to that value just fine. |
|
2857 | - return true; |
|
2858 | - } |
|
2859 | - $fields = []; |
|
2860 | - $set_sql_statements = []; |
|
2861 | - foreach ($fields_n_quantities as $field_name => $quantity) { |
|
2862 | - $field = $this->_model->field_settings_for($field_name); |
|
2863 | - $fields[] = $field; |
|
2864 | - $column_name = $field->get_table_column(); |
|
2865 | - |
|
2866 | - $abs_qty = absint($quantity); |
|
2867 | - if ($quantity > 0) { |
|
2868 | - // don't let the value be negative as often these fields are unsigned |
|
2869 | - $set_sql_statements[] = $wpdb->prepare( |
|
2870 | - "`{$column_name}` = `{$column_name}` + %d", |
|
2871 | - $abs_qty |
|
2872 | - ); |
|
2873 | - } else { |
|
2874 | - $set_sql_statements[] = $wpdb->prepare( |
|
2875 | - "`{$column_name}` = CASE |
|
1575 | + /* @type EE_Base_Class $newly_saved_object */ |
|
1576 | + // now get the type of relation |
|
1577 | + $relationship_to_model = $this->_model->related_settings_for($relationName); |
|
1578 | + // if this is a 1:1 relationship |
|
1579 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
1580 | + // then just replace the cached object with the newly saved object |
|
1581 | + $this->_model_relations[ $relationName ] = $newly_saved_object; |
|
1582 | + return true; |
|
1583 | + } |
|
1584 | + // or if it's some kind of sordid feral polyamorous relationship... |
|
1585 | + if ( |
|
1586 | + is_array($this->_model_relations[ $relationName ]) |
|
1587 | + && isset($this->_model_relations[ $relationName ][ $current_cache_id ]) |
|
1588 | + ) { |
|
1589 | + // then remove the current cached item |
|
1590 | + unset($this->_model_relations[ $relationName ][ $current_cache_id ]); |
|
1591 | + // and cache the newly saved object using it's new ID |
|
1592 | + $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object; |
|
1593 | + return true; |
|
1594 | + } |
|
1595 | + return false; |
|
1596 | + } |
|
1597 | + |
|
1598 | + |
|
1599 | + /** |
|
1600 | + * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
1601 | + * matching the given query conditions. |
|
1602 | + * |
|
1603 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
1604 | + * @param int $limit How many objects to return. |
|
1605 | + * @param array $query_params Any additional conditions on the query. |
|
1606 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
1607 | + * you can indicate just the columns you want returned |
|
1608 | + * @return array|EE_Base_Class[] |
|
1609 | + * @throws ReflectionException |
|
1610 | + * @throws InvalidArgumentException |
|
1611 | + * @throws InvalidInterfaceException |
|
1612 | + * @throws InvalidDataTypeException |
|
1613 | + * @throws EE_Error |
|
1614 | + */ |
|
1615 | + public function next_x($field_to_order_by = null, $limit = 1, $query_params = [], $columns_to_select = null) |
|
1616 | + { |
|
1617 | + $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1618 | + ? $this->_model->get_primary_key_field()->get_name() |
|
1619 | + : $field_to_order_by; |
|
1620 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
1621 | + if (empty($field) || empty($current_value)) { |
|
1622 | + return []; |
|
1623 | + } |
|
1624 | + return $this->_model->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
1625 | + } |
|
1626 | + |
|
1627 | + |
|
1628 | + /** |
|
1629 | + * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
1630 | + * matching the given query conditions. |
|
1631 | + * |
|
1632 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
1633 | + * @param int $limit How many objects to return. |
|
1634 | + * @param array $query_params Any additional conditions on the query. |
|
1635 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
1636 | + * you can indicate just the columns you want returned |
|
1637 | + * @return array|EE_Base_Class[] |
|
1638 | + * @throws ReflectionException |
|
1639 | + * @throws InvalidArgumentException |
|
1640 | + * @throws InvalidInterfaceException |
|
1641 | + * @throws InvalidDataTypeException |
|
1642 | + * @throws EE_Error |
|
1643 | + */ |
|
1644 | + public function previous_x( |
|
1645 | + $field_to_order_by = null, |
|
1646 | + $limit = 1, |
|
1647 | + $query_params = [], |
|
1648 | + $columns_to_select = null |
|
1649 | + ) { |
|
1650 | + $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1651 | + ? $this->_model->get_primary_key_field()->get_name() |
|
1652 | + : $field_to_order_by; |
|
1653 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
1654 | + if (empty($field) || empty($current_value)) { |
|
1655 | + return []; |
|
1656 | + } |
|
1657 | + return $this->_model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
1658 | + } |
|
1659 | + |
|
1660 | + |
|
1661 | + /** |
|
1662 | + * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
1663 | + * matching the given query conditions. |
|
1664 | + * |
|
1665 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
1666 | + * @param array $query_params Any additional conditions on the query. |
|
1667 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
1668 | + * you can indicate just the columns you want returned |
|
1669 | + * @return array|EE_Base_Class |
|
1670 | + * @throws ReflectionException |
|
1671 | + * @throws InvalidArgumentException |
|
1672 | + * @throws InvalidInterfaceException |
|
1673 | + * @throws InvalidDataTypeException |
|
1674 | + * @throws EE_Error |
|
1675 | + */ |
|
1676 | + public function next($field_to_order_by = null, $query_params = [], $columns_to_select = null) |
|
1677 | + { |
|
1678 | + $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1679 | + ? $this->_model->get_primary_key_field()->get_name() |
|
1680 | + : $field_to_order_by; |
|
1681 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
1682 | + if (empty($field) || empty($current_value)) { |
|
1683 | + return []; |
|
1684 | + } |
|
1685 | + return $this->_model->next($current_value, $field, $query_params, $columns_to_select); |
|
1686 | + } |
|
1687 | + |
|
1688 | + |
|
1689 | + /** |
|
1690 | + * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
1691 | + * matching the given query conditions. |
|
1692 | + * |
|
1693 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
1694 | + * @param array $query_params Any additional conditions on the query. |
|
1695 | + * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
1696 | + * you can indicate just the column you want returned |
|
1697 | + * @return array|EE_Base_Class |
|
1698 | + * @throws ReflectionException |
|
1699 | + * @throws InvalidArgumentException |
|
1700 | + * @throws InvalidInterfaceException |
|
1701 | + * @throws InvalidDataTypeException |
|
1702 | + * @throws EE_Error |
|
1703 | + */ |
|
1704 | + public function previous($field_to_order_by = null, $query_params = [], $columns_to_select = null) |
|
1705 | + { |
|
1706 | + $field = empty($field_to_order_by) && $this->_model->has_primary_key_field() |
|
1707 | + ? $this->_model->get_primary_key_field()->get_name() |
|
1708 | + : $field_to_order_by; |
|
1709 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
1710 | + if (empty($field) || empty($current_value)) { |
|
1711 | + return []; |
|
1712 | + } |
|
1713 | + return $this->_model->previous($current_value, $field, $query_params, $columns_to_select); |
|
1714 | + } |
|
1715 | + |
|
1716 | + |
|
1717 | + /** |
|
1718 | + * This is used to return the internal DateTime object used for a field that is a |
|
1719 | + * EE_Datetime_Field. |
|
1720 | + * |
|
1721 | + * @param string $field_name The field name retrieving the DateTime object. |
|
1722 | + * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
1723 | + * @throws EE_Error an error is set and false returned. If the field IS an |
|
1724 | + * EE_Datetime_Field and but the field value is null, then |
|
1725 | + * just null is returned (because that indicates that likely |
|
1726 | + * this field is nullable). |
|
1727 | + * @throws InvalidArgumentException |
|
1728 | + * @throws InvalidDataTypeException |
|
1729 | + * @throws InvalidInterfaceException |
|
1730 | + */ |
|
1731 | + public function get_DateTime_object(string $field_name) |
|
1732 | + { |
|
1733 | + $field_settings = $this->_model->field_settings_for($field_name); |
|
1734 | + if (! $field_settings instanceof EE_Datetime_Field) { |
|
1735 | + EE_Error::add_error( |
|
1736 | + sprintf( |
|
1737 | + esc_html__( |
|
1738 | + 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
1739 | + 'event_espresso' |
|
1740 | + ), |
|
1741 | + $field_name |
|
1742 | + ), |
|
1743 | + __FILE__, |
|
1744 | + __FUNCTION__, |
|
1745 | + __LINE__ |
|
1746 | + ); |
|
1747 | + return false; |
|
1748 | + } |
|
1749 | + return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime |
|
1750 | + ? clone $this->_fields[ $field_name ] |
|
1751 | + : null; |
|
1752 | + } |
|
1753 | + |
|
1754 | + |
|
1755 | + |
|
1756 | + |
|
1757 | + /** |
|
1758 | + * NOTE ABOUT BELOW: |
|
1759 | + * These convenience date and time setters are for setting date and time independently. In other words you might |
|
1760 | + * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
1761 | + * you want to set both date and time at the same time, you can just use the models default set($field_name,$value) |
|
1762 | + * method and make sure you send the entire datetime value for setting. |
|
1763 | + */ |
|
1764 | + |
|
1765 | + |
|
1766 | + /** |
|
1767 | + * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
1768 | + * can be easily used as the value of form input. |
|
1769 | + * |
|
1770 | + * @param string string $field_name |
|
1771 | + * @return void |
|
1772 | + * @throws InvalidArgumentException |
|
1773 | + * @throws InvalidInterfaceException |
|
1774 | + * @throws InvalidDataTypeException |
|
1775 | + * @throws EE_Error |
|
1776 | + */ |
|
1777 | + public function f(string $field_name) |
|
1778 | + { |
|
1779 | + $this->e($field_name, 'form_input'); |
|
1780 | + } |
|
1781 | + |
|
1782 | + |
|
1783 | + /** |
|
1784 | + * To be used in template to immediately echo out the value, and format it for output. |
|
1785 | + * Eg, should call stripslashes and whatnot before echoing |
|
1786 | + * |
|
1787 | + * @param string $field_name the name of the field as it appears in the DB |
|
1788 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
1789 | + * (in cases where the same property may be used for different outputs |
|
1790 | + * - i.e. datetime, money etc.) |
|
1791 | + * @return void |
|
1792 | + * @throws InvalidArgumentException |
|
1793 | + * @throws InvalidInterfaceException |
|
1794 | + * @throws InvalidDataTypeException |
|
1795 | + * @throws EE_Error |
|
1796 | + */ |
|
1797 | + public function e(string $field_name, string $extra_cache_ref = '') |
|
1798 | + { |
|
1799 | + echo $this->get_pretty($field_name, $extra_cache_ref); |
|
1800 | + } |
|
1801 | + |
|
1802 | + |
|
1803 | + /** |
|
1804 | + * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
1805 | + * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
1806 | + * to see what options are available. |
|
1807 | + * |
|
1808 | + * @param string $field_name |
|
1809 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
1810 | + * (in cases where the same property may be used for different outputs |
|
1811 | + * - i.e. datetime, money etc.) |
|
1812 | + * @return mixed |
|
1813 | + * @throws InvalidArgumentException |
|
1814 | + * @throws InvalidInterfaceException |
|
1815 | + * @throws InvalidDataTypeException |
|
1816 | + * @throws EE_Error |
|
1817 | + */ |
|
1818 | + public function get_pretty(string $field_name, string $extra_cache_ref = '') |
|
1819 | + { |
|
1820 | + return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
1821 | + } |
|
1822 | + |
|
1823 | + |
|
1824 | + /** |
|
1825 | + * Same as `f()` but just returns the value instead of echoing it |
|
1826 | + * |
|
1827 | + * @param string $field_name |
|
1828 | + * @return string |
|
1829 | + * @throws InvalidArgumentException |
|
1830 | + * @throws InvalidInterfaceException |
|
1831 | + * @throws InvalidDataTypeException |
|
1832 | + * @throws EE_Error |
|
1833 | + */ |
|
1834 | + public function get_f(string $field_name) |
|
1835 | + { |
|
1836 | + return (string) $this->get_pretty($field_name, 'form_input'); |
|
1837 | + } |
|
1838 | + |
|
1839 | + |
|
1840 | + /** |
|
1841 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
1842 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
1843 | + * other echoes the pretty value for dtt) |
|
1844 | + * |
|
1845 | + * @param string $field_name name of model object datetime field holding the value |
|
1846 | + * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
1847 | + * @return string datetime value formatted |
|
1848 | + * @throws InvalidArgumentException |
|
1849 | + * @throws InvalidInterfaceException |
|
1850 | + * @throws InvalidDataTypeException |
|
1851 | + * @throws EE_Error |
|
1852 | + */ |
|
1853 | + public function get_date(string $field_name, string $format = '') |
|
1854 | + { |
|
1855 | + return $this->_get_datetime($field_name, $format, null, 'D'); |
|
1856 | + } |
|
1857 | + |
|
1858 | + |
|
1859 | + /** |
|
1860 | + * This simply returns the datetime for the given field name |
|
1861 | + * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
1862 | + * (and the equivalent e_date, e_time, e_datetime). |
|
1863 | + * |
|
1864 | + * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
1865 | + * @param string $date_format valid datetime format used for date |
|
1866 | + * (if '' then we just use the default on the field, |
|
1867 | + * if NULL we use the last-used format) |
|
1868 | + * @param string $time_format Same as above except this is for time format |
|
1869 | + * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
1870 | + * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
1871 | + * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
1872 | + * if field is not a valid dtt field, or void if echoing |
|
1873 | + * @throws InvalidArgumentException |
|
1874 | + * @throws InvalidInterfaceException |
|
1875 | + * @throws InvalidDataTypeException |
|
1876 | + * @throws EE_Error |
|
1877 | + */ |
|
1878 | + protected function _get_datetime( |
|
1879 | + string $field_name, |
|
1880 | + string $date_format = '', |
|
1881 | + string $time_format = '', |
|
1882 | + string $date_or_time = '', |
|
1883 | + bool $echo = false |
|
1884 | + ) { |
|
1885 | + // clear cached property |
|
1886 | + $this->_clear_cached_property($field_name); |
|
1887 | + // reset format properties because they are used in get() |
|
1888 | + $this->_dt_frmt = $date_format !== '' ? $date_format : $this->_dt_frmt; |
|
1889 | + $this->_tm_frmt = $time_format !== '' ? $time_format : $this->_tm_frmt; |
|
1890 | + if ($echo) { |
|
1891 | + $this->e($field_name, $date_or_time); |
|
1892 | + return ''; |
|
1893 | + } |
|
1894 | + return $this->get($field_name, $date_or_time); |
|
1895 | + } |
|
1896 | + |
|
1897 | + |
|
1898 | + /** |
|
1899 | + * @param string $field_name |
|
1900 | + * @param string $format |
|
1901 | + * @throws InvalidArgumentException |
|
1902 | + * @throws InvalidInterfaceException |
|
1903 | + * @throws InvalidDataTypeException |
|
1904 | + * @throws EE_Error |
|
1905 | + */ |
|
1906 | + public function e_date(string $field_name, $format = '') |
|
1907 | + { |
|
1908 | + $this->_get_datetime($field_name, $format, null, 'D', true); |
|
1909 | + } |
|
1910 | + |
|
1911 | + |
|
1912 | + /** |
|
1913 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
1914 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
1915 | + * other echoes the pretty value for dtt) |
|
1916 | + * |
|
1917 | + * @param string $field_name name of model object datetime field holding the value |
|
1918 | + * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
1919 | + * @return string datetime value formatted |
|
1920 | + * @throws InvalidArgumentException |
|
1921 | + * @throws InvalidInterfaceException |
|
1922 | + * @throws InvalidDataTypeException |
|
1923 | + * @throws EE_Error |
|
1924 | + */ |
|
1925 | + public function get_time(string $field_name, string $format = '') |
|
1926 | + { |
|
1927 | + return $this->_get_datetime($field_name, null, $format, 'T'); |
|
1928 | + } |
|
1929 | + |
|
1930 | + |
|
1931 | + /** |
|
1932 | + * @param string $field_name |
|
1933 | + * @param string $format |
|
1934 | + * @throws InvalidArgumentException |
|
1935 | + * @throws InvalidInterfaceException |
|
1936 | + * @throws InvalidDataTypeException |
|
1937 | + * @throws EE_Error |
|
1938 | + */ |
|
1939 | + public function e_time(string $field_name, string $format = '') |
|
1940 | + { |
|
1941 | + $this->_get_datetime($field_name, null, $format, 'T', true); |
|
1942 | + } |
|
1943 | + |
|
1944 | + |
|
1945 | + /** |
|
1946 | + * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
1947 | + * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
1948 | + * other echoes the pretty value for dtt) |
|
1949 | + * |
|
1950 | + * @param string $field_name name of model object datetime field holding the value |
|
1951 | + * @param string $date_format format for the date returned (if NULL we use default in dt_frmt property) |
|
1952 | + * @param string $time_format format for the time returned (if NULL we use default in tm_frmt property) |
|
1953 | + * @return string datetime value formatted |
|
1954 | + * @throws InvalidArgumentException |
|
1955 | + * @throws InvalidInterfaceException |
|
1956 | + * @throws InvalidDataTypeException |
|
1957 | + * @throws EE_Error |
|
1958 | + */ |
|
1959 | + public function get_datetime(string $field_name, $date_format = '', $time_format = '') |
|
1960 | + { |
|
1961 | + return $this->_get_datetime($field_name, $date_format, $time_format); |
|
1962 | + } |
|
1963 | + |
|
1964 | + |
|
1965 | + /** |
|
1966 | + * @param string $field_name |
|
1967 | + * @param string $date_format |
|
1968 | + * @param string $time_format |
|
1969 | + * @throws InvalidArgumentException |
|
1970 | + * @throws InvalidInterfaceException |
|
1971 | + * @throws InvalidDataTypeException |
|
1972 | + * @throws EE_Error |
|
1973 | + */ |
|
1974 | + public function e_datetime(string $field_name, $date_format = '', $time_format = '') |
|
1975 | + { |
|
1976 | + $this->_get_datetime($field_name, $date_format, $time_format, null, true); |
|
1977 | + } |
|
1978 | + |
|
1979 | + |
|
1980 | + /** |
|
1981 | + * Get the i8ln value for a date using the WordPress |
|
1982 | + * |
|
1983 | + * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
1984 | + * @param string $format PHP valid date/time string format. |
|
1985 | + * If none is provided then the internal set format on the object will be used. |
|
1986 | + * @return string Date and time string in set locale or false if no field exists for the given |
|
1987 | + * @throws InvalidArgumentException |
|
1988 | + * @throws InvalidInterfaceException |
|
1989 | + * @throws InvalidDataTypeException |
|
1990 | + * @throws EE_Error |
|
1991 | + * field name. |
|
1992 | + * @see date_i18n function. |
|
1993 | + */ |
|
1994 | + public function get_i18n_datetime(string $field_name, $format = '') |
|
1995 | + { |
|
1996 | + $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
1997 | + return date_i18n( |
|
1998 | + $format, |
|
1999 | + EEH_DTT_Helper::get_timestamp_with_offset( |
|
2000 | + $this->get_raw($field_name), |
|
2001 | + $this->get_timezone() |
|
2002 | + ) |
|
2003 | + ); |
|
2004 | + } |
|
2005 | + |
|
2006 | + |
|
2007 | + /** |
|
2008 | + * This method simply returns the RAW unprocessed value for the given property in this class |
|
2009 | + * |
|
2010 | + * @param string $field_name A valid field name |
|
2011 | + * @return mixed Whatever the raw value stored on the property is. |
|
2012 | + * @throws InvalidArgumentException |
|
2013 | + * @throws InvalidInterfaceException |
|
2014 | + * @throws InvalidDataTypeException |
|
2015 | + * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
2016 | + */ |
|
2017 | + public function get_raw(string $field_name) |
|
2018 | + { |
|
2019 | + $field_settings = $this->_model->field_settings_for($field_name); |
|
2020 | + return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime |
|
2021 | + ? $this->_fields[ $field_name ]->format('U') |
|
2022 | + : $this->_fields[ $field_name ]; |
|
2023 | + } |
|
2024 | + |
|
2025 | + |
|
2026 | + /** |
|
2027 | + * This will return a timestamp for the website timezone |
|
2028 | + * but ONLY when the current website timezone is different |
|
2029 | + * than the timezone set for the website. |
|
2030 | + * NOTE, this currently only works well with methods that return values. |
|
2031 | + * If you use it with methods that echo values |
|
2032 | + * the $_timestamp property may not get reset to its original value |
|
2033 | + * and that could lead to some unexpected results! |
|
2034 | + * |
|
2035 | + * @param string $field_name This is the name of the field on the object that contains the date/time |
|
2036 | + * value being returned. |
|
2037 | + * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
2038 | + * @param array|string $args This is the arguments that will be passed to the callback. |
|
2039 | + * @param string $prepend You can include something to prepend on the timestamp |
|
2040 | + * @param string $append You can include something to append on the timestamp |
|
2041 | + * @return string timestamp |
|
2042 | + * @throws InvalidArgumentException |
|
2043 | + * @throws InvalidInterfaceException |
|
2044 | + * @throws InvalidDataTypeException |
|
2045 | + * @throws EE_Error |
|
2046 | + */ |
|
2047 | + public function display_in_my_timezone( |
|
2048 | + string $field_name, |
|
2049 | + $callback = 'get_datetime', |
|
2050 | + $args = null, |
|
2051 | + $prepend = '', |
|
2052 | + $append = '' |
|
2053 | + ) { |
|
2054 | + $timezone = EEH_DTT_Helper::get_timezone(); |
|
2055 | + if ($timezone === $this->_timezone) { |
|
2056 | + return ''; |
|
2057 | + } |
|
2058 | + $original_timezone = $this->_timezone; |
|
2059 | + $this->set_timezone($timezone); |
|
2060 | + $fn = (array) $field_name; |
|
2061 | + $args = array_merge($fn, (array) $args); |
|
2062 | + if (! method_exists($this, $callback)) { |
|
2063 | + throw new EE_Error( |
|
2064 | + sprintf( |
|
2065 | + esc_html__( |
|
2066 | + 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
2067 | + 'event_espresso' |
|
2068 | + ), |
|
2069 | + $callback |
|
2070 | + ) |
|
2071 | + ); |
|
2072 | + } |
|
2073 | + $args = (array) $args; |
|
2074 | + $return = $prepend . call_user_func_array([$this, $callback], $args) . $append; |
|
2075 | + $this->set_timezone($original_timezone); |
|
2076 | + return $return; |
|
2077 | + } |
|
2078 | + |
|
2079 | + |
|
2080 | + /** |
|
2081 | + * Deletes this model object. |
|
2082 | + * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
2083 | + * override |
|
2084 | + * `EE_Base_Class::_delete` NOT this class. |
|
2085 | + * |
|
2086 | + * @return boolean | int |
|
2087 | + * @throws ReflectionException |
|
2088 | + * @throws InvalidArgumentException |
|
2089 | + * @throws InvalidInterfaceException |
|
2090 | + * @throws InvalidDataTypeException |
|
2091 | + * @throws EE_Error |
|
2092 | + */ |
|
2093 | + public function delete() |
|
2094 | + { |
|
2095 | + /** |
|
2096 | + * Called just before the `EE_Base_Class::_delete` method call. |
|
2097 | + * Note: |
|
2098 | + * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
2099 | + * should be aware that `_delete` may not always result in a permanent delete. |
|
2100 | + * For example, `EE_Soft_Delete_Base_Class::_delete` |
|
2101 | + * soft deletes (trash) the object and does not permanently delete it. |
|
2102 | + * |
|
2103 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
2104 | + */ |
|
2105 | + do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
2106 | + $result = $this->_delete(); |
|
2107 | + /** |
|
2108 | + * Called just after the `EE_Base_Class::_delete` method call. |
|
2109 | + * Note: |
|
2110 | + * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
2111 | + * should be aware that `_delete` may not always result in a permanent delete. |
|
2112 | + * For example `EE_Soft_Base_Class::_delete` |
|
2113 | + * soft deletes (trash) the object and does not permanently delete it. |
|
2114 | + * |
|
2115 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
2116 | + * @param boolean $result |
|
2117 | + */ |
|
2118 | + do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
2119 | + return $result; |
|
2120 | + } |
|
2121 | + |
|
2122 | + |
|
2123 | + /** |
|
2124 | + * Calls the specific delete method for the instantiated class. |
|
2125 | + * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
2126 | + * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
2127 | + * `EE_Base_Class::delete` |
|
2128 | + * |
|
2129 | + * @return bool|int |
|
2130 | + * @throws ReflectionException |
|
2131 | + * @throws InvalidArgumentException |
|
2132 | + * @throws InvalidInterfaceException |
|
2133 | + * @throws InvalidDataTypeException |
|
2134 | + * @throws EE_Error |
|
2135 | + */ |
|
2136 | + protected function _delete() |
|
2137 | + { |
|
2138 | + return $this->delete_permanently(); |
|
2139 | + } |
|
2140 | + |
|
2141 | + |
|
2142 | + /** |
|
2143 | + * Deletes this model object permanently from db |
|
2144 | + * (but keep in mind related models may block the delete and return an error) |
|
2145 | + * |
|
2146 | + * @return bool | int |
|
2147 | + * @throws ReflectionException |
|
2148 | + * @throws InvalidArgumentException |
|
2149 | + * @throws InvalidInterfaceException |
|
2150 | + * @throws InvalidDataTypeException |
|
2151 | + * @throws EE_Error |
|
2152 | + */ |
|
2153 | + public function delete_permanently() |
|
2154 | + { |
|
2155 | + /** |
|
2156 | + * Called just before HARD deleting a model object |
|
2157 | + * |
|
2158 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
2159 | + */ |
|
2160 | + do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
2161 | + $result = $this->_model->delete_permanently_by_ID($this->ID()); |
|
2162 | + $this->refresh_cache_of_related_objects(); |
|
2163 | + /** |
|
2164 | + * Called just after HARD deleting a model object |
|
2165 | + * |
|
2166 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
2167 | + * @param boolean $result |
|
2168 | + */ |
|
2169 | + do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
2170 | + return $result; |
|
2171 | + } |
|
2172 | + |
|
2173 | + |
|
2174 | + /** |
|
2175 | + * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
2176 | + * related model objects |
|
2177 | + * |
|
2178 | + * @throws ReflectionException |
|
2179 | + * @throws InvalidArgumentException |
|
2180 | + * @throws InvalidInterfaceException |
|
2181 | + * @throws InvalidDataTypeException |
|
2182 | + * @throws EE_Error |
|
2183 | + */ |
|
2184 | + public function refresh_cache_of_related_objects() |
|
2185 | + { |
|
2186 | + foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
2187 | + if (! empty($this->_model_relations[ $relation_name ])) { |
|
2188 | + $related_objects = $this->_model_relations[ $relation_name ]; |
|
2189 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
2190 | + // this relation only stores a single model object, not an array |
|
2191 | + // but let's make it consistent |
|
2192 | + $related_objects = [$related_objects]; |
|
2193 | + } |
|
2194 | + foreach ($related_objects as $related_object) { |
|
2195 | + // only refresh their cache if they're in memory |
|
2196 | + if ($related_object instanceof EE_Base_Class) { |
|
2197 | + $related_object->clear_cache( |
|
2198 | + $this->_model->get_this_model_name(), |
|
2199 | + $this |
|
2200 | + ); |
|
2201 | + } |
|
2202 | + } |
|
2203 | + } |
|
2204 | + } |
|
2205 | + } |
|
2206 | + |
|
2207 | + |
|
2208 | + /** |
|
2209 | + * Forgets the cached model of the given relation Name. So the next time we request it, |
|
2210 | + * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
2211 | + * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
2212 | + * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
2213 | + * |
|
2214 | + * @param string $relationName one of the keys in the _model_relations array on the model. |
|
2215 | + * Eg 'Registration' |
|
2216 | + * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
2217 | + * if you intend to use $clear_all = TRUE, or the relation only |
|
2218 | + * has 1 object anyways (ie, it's a BelongsToRelation) |
|
2219 | + * @param bool $clear_all This flags clearing the entire cache relation property if |
|
2220 | + * this is HasMany or HABTM. |
|
2221 | + * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
2222 | + * relation from all |
|
2223 | + * @throws InvalidArgumentException |
|
2224 | + * @throws InvalidInterfaceException |
|
2225 | + * @throws InvalidDataTypeException |
|
2226 | + * @throws EE_Error |
|
2227 | + * @throws ReflectionException |
|
2228 | + */ |
|
2229 | + public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
2230 | + { |
|
2231 | + $relationship_to_model = $this->_model->related_settings_for($relationName); |
|
2232 | + $index_in_cache = ''; |
|
2233 | + if (! $relationship_to_model) { |
|
2234 | + throw new EE_Error( |
|
2235 | + sprintf( |
|
2236 | + esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'), |
|
2237 | + $relationName, |
|
2238 | + get_class($this) |
|
2239 | + ) |
|
2240 | + ); |
|
2241 | + } |
|
2242 | + if ($clear_all) { |
|
2243 | + $obj_removed = true; |
|
2244 | + $this->_model_relations[ $relationName ] = null; |
|
2245 | + } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
2246 | + $obj_removed = $this->_model_relations[ $relationName ]; |
|
2247 | + $this->_model_relations[ $relationName ] = null; |
|
2248 | + } else { |
|
2249 | + if ( |
|
2250 | + $object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
2251 | + && $object_to_remove_or_index_into_array->ID() |
|
2252 | + ) { |
|
2253 | + $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
2254 | + if ( |
|
2255 | + is_array($this->_model_relations[ $relationName ]) |
|
2256 | + && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ]) |
|
2257 | + ) { |
|
2258 | + $index_found_at = null; |
|
2259 | + // find this object in the array even though it has a different key |
|
2260 | + foreach ($this->_model_relations[ $relationName ] as $index => $obj) { |
|
2261 | + /** @noinspection TypeUnsafeComparisonInspection */ |
|
2262 | + if ( |
|
2263 | + $obj instanceof EE_Base_Class |
|
2264 | + && ( |
|
2265 | + $obj == $object_to_remove_or_index_into_array |
|
2266 | + || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
2267 | + ) |
|
2268 | + ) { |
|
2269 | + $index_found_at = $index; |
|
2270 | + break; |
|
2271 | + } |
|
2272 | + } |
|
2273 | + if ($index_found_at) { |
|
2274 | + $index_in_cache = $index_found_at; |
|
2275 | + } else { |
|
2276 | + // it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
2277 | + // if it wasn't in it to begin with. So we're done |
|
2278 | + return $object_to_remove_or_index_into_array; |
|
2279 | + } |
|
2280 | + } |
|
2281 | + } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
2282 | + // so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
2283 | + foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
2284 | + /** @noinspection TypeUnsafeComparisonInspection */ |
|
2285 | + if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
2286 | + $index_in_cache = $index; |
|
2287 | + } |
|
2288 | + } |
|
2289 | + } else { |
|
2290 | + $index_in_cache = $object_to_remove_or_index_into_array; |
|
2291 | + } |
|
2292 | + // supposedly we've found it. But it could just be that the client code |
|
2293 | + // provided a bad index/object |
|
2294 | + if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) { |
|
2295 | + $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ]; |
|
2296 | + unset($this->_model_relations[ $relationName ][ $index_in_cache ]); |
|
2297 | + } else { |
|
2298 | + // that thing was never cached anyways. |
|
2299 | + $obj_removed = null; |
|
2300 | + } |
|
2301 | + } |
|
2302 | + return $obj_removed; |
|
2303 | + } |
|
2304 | + |
|
2305 | + |
|
2306 | + /** |
|
2307 | + * Saves this model object and its NEW cached relations to the database. |
|
2308 | + * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
2309 | + * In order for that to work, we would need to mark model objects as dirty/clean... |
|
2310 | + * because otherwise, there's a potential for infinite looping of saving |
|
2311 | + * Saves the cached related model objects, and ensures the relation between them |
|
2312 | + * and this object and properly setup |
|
2313 | + * |
|
2314 | + * @return int ID of new model object on save; 0 on failure+ |
|
2315 | + * @throws ReflectionException |
|
2316 | + * @throws InvalidArgumentException |
|
2317 | + * @throws InvalidInterfaceException |
|
2318 | + * @throws InvalidDataTypeException |
|
2319 | + * @throws EE_Error |
|
2320 | + */ |
|
2321 | + public function save_new_cached_related_model_objs() |
|
2322 | + { |
|
2323 | + // make sure this has been saved |
|
2324 | + if (! $this->ID()) { |
|
2325 | + $id = $this->save(); |
|
2326 | + } else { |
|
2327 | + $id = $this->ID(); |
|
2328 | + } |
|
2329 | + // now save all the NEW cached model objects (ie they don't exist in the DB) |
|
2330 | + foreach ($this->_model->relation_settings() as $relationName => $relationObj) { |
|
2331 | + if ($this->_model_relations[ $relationName ]) { |
|
2332 | + // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
2333 | + // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
2334 | + /* @var $related_model_obj EE_Base_Class */ |
|
2335 | + if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
2336 | + // add a relation to that relation type (which saves the appropriate thing in the process) |
|
2337 | + // but ONLY if it DOES NOT exist in the DB |
|
2338 | + $related_model_obj = $this->_model_relations[ $relationName ]; |
|
2339 | + // if( ! $related_model_obj->ID()){ |
|
2340 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
2341 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
2342 | + // } |
|
2343 | + } else { |
|
2344 | + foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
2345 | + // add a relation to that relation type (which saves the appropriate thing in the process) |
|
2346 | + // but ONLY if it DOES NOT exist in the DB |
|
2347 | + // if( ! $related_model_obj->ID()){ |
|
2348 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
2349 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
2350 | + // } |
|
2351 | + } |
|
2352 | + } |
|
2353 | + } |
|
2354 | + } |
|
2355 | + return $id; |
|
2356 | + } |
|
2357 | + |
|
2358 | + |
|
2359 | + /** |
|
2360 | + * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
2361 | + * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
2362 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
2363 | + * |
|
2364 | + * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
2365 | + * @param string $relationName eg 'Events','Question',etc. |
|
2366 | + * an attendee to a group, you also want to specify which role they |
|
2367 | + * will have in that group. So you would use this parameter to |
|
2368 | + * specify array('role-column-name'=>'role-id') |
|
2369 | + * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
2370 | + * allow you to further constrict the relation to being added. |
|
2371 | + * However, keep in mind that the columns (keys) given must match a |
|
2372 | + * column on the JOIN table and currently only the HABTM models |
|
2373 | + * accept these additional conditions. Also remember that if an |
|
2374 | + * exact match isn't found for these extra cols/val pairs, then a |
|
2375 | + * NEW row is created in the join table. |
|
2376 | + * @param null $cache_id |
|
2377 | + * @return EE_Base_Class the object the relation was added to |
|
2378 | + * @throws ReflectionException |
|
2379 | + * @throws InvalidArgumentException |
|
2380 | + * @throws InvalidInterfaceException |
|
2381 | + * @throws InvalidDataTypeException |
|
2382 | + * @throws EE_Error |
|
2383 | + */ |
|
2384 | + public function _add_relation_to( |
|
2385 | + $otherObjectModelObjectOrID, |
|
2386 | + $relationName, |
|
2387 | + $extra_join_model_fields_n_values = [], |
|
2388 | + $cache_id = null |
|
2389 | + ) { |
|
2390 | + // if this thing exists in the DB, save the relation to the DB |
|
2391 | + if ($this->ID()) { |
|
2392 | + $otherObject = $this->_model->add_relationship_to( |
|
2393 | + $this, |
|
2394 | + $otherObjectModelObjectOrID, |
|
2395 | + $relationName, |
|
2396 | + $extra_join_model_fields_n_values |
|
2397 | + ); |
|
2398 | + // clear cache so future get_many_related and get_first_related() return new results. |
|
2399 | + $this->clear_cache($relationName, $otherObject, true); |
|
2400 | + if ($otherObject instanceof EE_Base_Class) { |
|
2401 | + $otherObject->clear_cache($this->_model->get_this_model_name(), $this); |
|
2402 | + } |
|
2403 | + } else { |
|
2404 | + // this thing doesn't exist in the DB, so just cache it |
|
2405 | + if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
2406 | + throw new EE_Error( |
|
2407 | + sprintf( |
|
2408 | + esc_html__( |
|
2409 | + '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', |
|
2410 | + 'event_espresso' |
|
2411 | + ), |
|
2412 | + $otherObjectModelObjectOrID, |
|
2413 | + get_class($this) |
|
2414 | + ) |
|
2415 | + ); |
|
2416 | + } |
|
2417 | + $otherObject = $otherObjectModelObjectOrID; |
|
2418 | + $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
2419 | + } |
|
2420 | + if ($otherObject instanceof EE_Base_Class) { |
|
2421 | + // fix the reciprocal relation too |
|
2422 | + if ($otherObject->ID()) { |
|
2423 | + // its saved so assumed relations exist in the DB, so we can just |
|
2424 | + // clear the cache so future queries use the updated info in the DB |
|
2425 | + $otherObject->clear_cache( |
|
2426 | + $this->_model->get_this_model_name(), |
|
2427 | + null, |
|
2428 | + true |
|
2429 | + ); |
|
2430 | + } else { |
|
2431 | + // it's not saved, so it caches relations like this |
|
2432 | + $otherObject->cache($this->_model->get_this_model_name(), $this); |
|
2433 | + } |
|
2434 | + } |
|
2435 | + return $otherObject; |
|
2436 | + } |
|
2437 | + |
|
2438 | + |
|
2439 | + /** |
|
2440 | + * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
2441 | + * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
2442 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
2443 | + * from the cache |
|
2444 | + * |
|
2445 | + * @param mixed $otherObjectModelObjectOrID |
|
2446 | + * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
2447 | + * to the DB yet |
|
2448 | + * @param string $relationName |
|
2449 | + * @param array $where_query |
|
2450 | + * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
2451 | + * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
2452 | + * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
2453 | + * remember that if an exact match isn't found for these extra cols/val pairs, then no row is |
|
2454 | + * deleted. |
|
2455 | + * @return EE_Base_Class the relation was removed from |
|
2456 | + * @throws ReflectionException |
|
2457 | + * @throws InvalidArgumentException |
|
2458 | + * @throws InvalidInterfaceException |
|
2459 | + * @throws InvalidDataTypeException |
|
2460 | + * @throws EE_Error |
|
2461 | + */ |
|
2462 | + public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = []) |
|
2463 | + { |
|
2464 | + if ($this->ID()) { |
|
2465 | + // if this exists in the DB, save the relation change to the DB too |
|
2466 | + $otherObject = $this->_model->remove_relationship_to( |
|
2467 | + $this, |
|
2468 | + $otherObjectModelObjectOrID, |
|
2469 | + $relationName, |
|
2470 | + $where_query |
|
2471 | + ); |
|
2472 | + $this->clear_cache( |
|
2473 | + $relationName, |
|
2474 | + $otherObject |
|
2475 | + ); |
|
2476 | + } else { |
|
2477 | + // this doesn't exist in the DB, just remove it from the cache |
|
2478 | + $otherObject = $this->clear_cache( |
|
2479 | + $relationName, |
|
2480 | + $otherObjectModelObjectOrID |
|
2481 | + ); |
|
2482 | + } |
|
2483 | + if ($otherObject instanceof EE_Base_Class) { |
|
2484 | + $otherObject->clear_cache( |
|
2485 | + $this->_model->get_this_model_name(), |
|
2486 | + $this |
|
2487 | + ); |
|
2488 | + } |
|
2489 | + return $otherObject; |
|
2490 | + } |
|
2491 | + |
|
2492 | + |
|
2493 | + /** |
|
2494 | + * Removes ALL the related things for the $relationName. |
|
2495 | + * |
|
2496 | + * @param string $relationName |
|
2497 | + * @param array $where_query_params |
|
2498 | + * @return EE_Base_Class |
|
2499 | + * @throws ReflectionException |
|
2500 | + * @throws InvalidArgumentException |
|
2501 | + * @throws InvalidInterfaceException |
|
2502 | + * @throws InvalidDataTypeException |
|
2503 | + * @throws EE_Error |
|
2504 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
2505 | + */ |
|
2506 | + public function _remove_relations($relationName, $where_query_params = []) |
|
2507 | + { |
|
2508 | + if ($this->ID()) { |
|
2509 | + // if this exists in the DB, save the relation change to the DB too |
|
2510 | + $otherObjects = $this->_model->remove_relations( |
|
2511 | + $this, |
|
2512 | + $relationName, |
|
2513 | + $where_query_params |
|
2514 | + ); |
|
2515 | + $this->clear_cache( |
|
2516 | + $relationName, |
|
2517 | + null, |
|
2518 | + true |
|
2519 | + ); |
|
2520 | + } else { |
|
2521 | + // this doesn't exist in the DB, just remove it from the cache |
|
2522 | + $otherObjects = $this->clear_cache( |
|
2523 | + $relationName, |
|
2524 | + null, |
|
2525 | + true |
|
2526 | + ); |
|
2527 | + } |
|
2528 | + if (is_array($otherObjects)) { |
|
2529 | + foreach ($otherObjects as $otherObject) { |
|
2530 | + $otherObject->clear_cache( |
|
2531 | + $this->_model->get_this_model_name(), |
|
2532 | + $this |
|
2533 | + ); |
|
2534 | + } |
|
2535 | + } |
|
2536 | + return $otherObjects; |
|
2537 | + } |
|
2538 | + |
|
2539 | + |
|
2540 | + /** |
|
2541 | + * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
2542 | + * unless otherwise specified in the $query_params |
|
2543 | + * |
|
2544 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
2545 | + * @param array $query_params |
|
2546 | + * @param string $field_to_count name of field to count by. By default, uses primary key |
|
2547 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
2548 | + * that by the setting $distinct to TRUE; |
|
2549 | + * @return int |
|
2550 | + * @throws ReflectionException |
|
2551 | + * @throws InvalidArgumentException |
|
2552 | + * @throws InvalidInterfaceException |
|
2553 | + * @throws InvalidDataTypeException |
|
2554 | + * @throws EE_Error |
|
2555 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2556 | + */ |
|
2557 | + public function count_related($relation_name, $query_params = [], $field_to_count = null, $distinct = false) |
|
2558 | + { |
|
2559 | + return $this->_model->count_related( |
|
2560 | + $this, |
|
2561 | + $relation_name, |
|
2562 | + $query_params, |
|
2563 | + $field_to_count, |
|
2564 | + $distinct |
|
2565 | + ); |
|
2566 | + } |
|
2567 | + |
|
2568 | + |
|
2569 | + /** |
|
2570 | + * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
2571 | + * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
2572 | + * |
|
2573 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
2574 | + * @param array $query_params |
|
2575 | + * @param string $field_to_sum name of field to count by. |
|
2576 | + * By default, uses primary key |
|
2577 | + * (which doesn't make much sense, so you should probably change it) |
|
2578 | + * @return int |
|
2579 | + * @throws ReflectionException |
|
2580 | + * @throws InvalidArgumentException |
|
2581 | + * @throws InvalidInterfaceException |
|
2582 | + * @throws InvalidDataTypeException |
|
2583 | + * @throws EE_Error |
|
2584 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2585 | + */ |
|
2586 | + public function sum_related($relation_name, $query_params = [], $field_to_sum = null) |
|
2587 | + { |
|
2588 | + return $this->_model->sum_related( |
|
2589 | + $this, |
|
2590 | + $relation_name, |
|
2591 | + $query_params, |
|
2592 | + $field_to_sum |
|
2593 | + ); |
|
2594 | + } |
|
2595 | + |
|
2596 | + |
|
2597 | + /** |
|
2598 | + * Does a delete on all related objects of type $relationName and removes |
|
2599 | + * the current model object's relation to them. If they can't be deleted (because |
|
2600 | + * of blocking related model objects) does nothing. If the related model objects are |
|
2601 | + * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
2602 | + * If this model object doesn't exist yet in the DB, just removes its related things |
|
2603 | + * |
|
2604 | + * @param string $relationName |
|
2605 | + * @param array $query_params |
|
2606 | + * @return int how many deleted |
|
2607 | + * @throws ReflectionException |
|
2608 | + * @throws InvalidArgumentException |
|
2609 | + * @throws InvalidInterfaceException |
|
2610 | + * @throws InvalidDataTypeException |
|
2611 | + * @throws EE_Error |
|
2612 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2613 | + */ |
|
2614 | + public function delete_related($relationName, $query_params = []) |
|
2615 | + { |
|
2616 | + if ($this->ID()) { |
|
2617 | + $count = $this->_model->delete_related( |
|
2618 | + $this, |
|
2619 | + $relationName, |
|
2620 | + $query_params |
|
2621 | + ); |
|
2622 | + } else { |
|
2623 | + $count = count($this->get_all_from_cache($relationName)); |
|
2624 | + $this->clear_cache($relationName, null, true); |
|
2625 | + } |
|
2626 | + return $count; |
|
2627 | + } |
|
2628 | + |
|
2629 | + |
|
2630 | + /** |
|
2631 | + * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
2632 | + * the current model object's relation to them. If they can't be deleted (because |
|
2633 | + * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
2634 | + * If the related thing isn't a soft-deletable model object, this function is identical |
|
2635 | + * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
2636 | + * |
|
2637 | + * @param string $relationName |
|
2638 | + * @param array $query_params |
|
2639 | + * @return int how many deleted (including those soft deleted) |
|
2640 | + * @throws ReflectionException |
|
2641 | + * @throws InvalidArgumentException |
|
2642 | + * @throws InvalidInterfaceException |
|
2643 | + * @throws InvalidDataTypeException |
|
2644 | + * @throws EE_Error |
|
2645 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2646 | + */ |
|
2647 | + public function delete_related_permanently($relationName, $query_params = []) |
|
2648 | + { |
|
2649 | + if ($this->ID()) { |
|
2650 | + $count = $this->_model->delete_related_permanently( |
|
2651 | + $this, |
|
2652 | + $relationName, |
|
2653 | + $query_params |
|
2654 | + ); |
|
2655 | + } else { |
|
2656 | + $count = count($this->get_all_from_cache($relationName)); |
|
2657 | + } |
|
2658 | + $this->clear_cache($relationName, null, true); |
|
2659 | + return $count; |
|
2660 | + } |
|
2661 | + |
|
2662 | + |
|
2663 | + /** |
|
2664 | + * is_set |
|
2665 | + * Just a simple utility function children can use for checking if property exists |
|
2666 | + * |
|
2667 | + * @param string $field_name property to check |
|
2668 | + * @return bool TRUE if existing, FALSE if not. |
|
2669 | + */ |
|
2670 | + public function is_set(string $field_name) |
|
2671 | + { |
|
2672 | + return isset($this->_fields[ $field_name ]); |
|
2673 | + } |
|
2674 | + |
|
2675 | + |
|
2676 | + /** |
|
2677 | + * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
2678 | + * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
2679 | + * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. |
|
2680 | + * Instead of requiring a plugin to extend the EE_Base_Class |
|
2681 | + * (which works fine is there's only 1 plugin, but when will that happen?) |
|
2682 | + * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' |
|
2683 | + * (eg, filters_hook_espresso__EE_Answer__my_great_function) |
|
2684 | + * and accepts 2 arguments: the object on which the function was called, |
|
2685 | + * and an array of the original arguments passed to the function. |
|
2686 | + * Whatever their callback function returns will be returned by this function. |
|
2687 | + * Example: in functions.php (or in a plugin): |
|
2688 | + * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); |
|
2689 | + * function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
2690 | + * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
2691 | + * return $previousReturnValue.$returnString; |
|
2692 | + * } |
|
2693 | + * require('EE_Answer.class.php'); |
|
2694 | + * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
2695 | + * echo $answer->my_callback('monkeys',100); |
|
2696 | + * //will output "you called my_callback! and passed args:monkeys,100" |
|
2697 | + * |
|
2698 | + * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
2699 | + * @param array $args array of original arguments passed to the function |
|
2700 | + * @return mixed whatever the plugin which calls add_filter decides |
|
2701 | + * @throws EE_Error |
|
2702 | + */ |
|
2703 | + public function __call($methodName, $args) |
|
2704 | + { |
|
2705 | + $className = get_class($this); |
|
2706 | + $tagName = "FHEE__{$className}__{$methodName}"; |
|
2707 | + if (! has_filter($tagName)) { |
|
2708 | + throw new EE_Error( |
|
2709 | + sprintf( |
|
2710 | + esc_html__( |
|
2711 | + "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;}", |
|
2712 | + 'event_espresso' |
|
2713 | + ), |
|
2714 | + $methodName, |
|
2715 | + $className, |
|
2716 | + $tagName |
|
2717 | + ) |
|
2718 | + ); |
|
2719 | + } |
|
2720 | + return apply_filters($tagName, null, $this, $args); |
|
2721 | + } |
|
2722 | + |
|
2723 | + |
|
2724 | + /** |
|
2725 | + * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
2726 | + * is specified, only deletes extra meta records with that value. |
|
2727 | + * |
|
2728 | + * @param string $meta_key |
|
2729 | + * @param mixed|null $meta_value |
|
2730 | + * @return int number of extra meta rows deleted |
|
2731 | + * @throws InvalidArgumentException |
|
2732 | + * @throws InvalidInterfaceException |
|
2733 | + * @throws InvalidDataTypeException |
|
2734 | + * @throws EE_Error |
|
2735 | + * @throws ReflectionException |
|
2736 | + */ |
|
2737 | + public function delete_extra_meta(string $meta_key, $meta_value = null): int |
|
2738 | + { |
|
2739 | + $query_params = [ |
|
2740 | + [ |
|
2741 | + 'EXM_key' => $meta_key, |
|
2742 | + 'OBJ_ID' => $this->ID(), |
|
2743 | + 'EXM_type' => $this->_model->get_this_model_name(), |
|
2744 | + ], |
|
2745 | + ]; |
|
2746 | + if ($meta_value !== null) { |
|
2747 | + $query_params[0]['EXM_value'] = $meta_value; |
|
2748 | + } |
|
2749 | + return EEM_Extra_Meta::instance()->delete($query_params); |
|
2750 | + } |
|
2751 | + |
|
2752 | + |
|
2753 | + /** |
|
2754 | + * Returns a simple array of all the extra meta associated with this model object. |
|
2755 | + * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
2756 | + * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
2757 | + * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
2758 | + * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
2759 | + * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
2760 | + * finally the extra meta's value as each sub-value. (eg |
|
2761 | + * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
2762 | + * |
|
2763 | + * @param boolean $one_of_each_key |
|
2764 | + * @return array |
|
2765 | + * @throws ReflectionException |
|
2766 | + * @throws InvalidArgumentException |
|
2767 | + * @throws InvalidInterfaceException |
|
2768 | + * @throws InvalidDataTypeException |
|
2769 | + * @throws EE_Error |
|
2770 | + */ |
|
2771 | + public function all_extra_meta_array($one_of_each_key = true) |
|
2772 | + { |
|
2773 | + $return_array = []; |
|
2774 | + if ($one_of_each_key) { |
|
2775 | + $extra_meta_objs = $this->get_many_related( |
|
2776 | + 'Extra_Meta', |
|
2777 | + ['group_by' => 'EXM_key'] |
|
2778 | + ); |
|
2779 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
2780 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
2781 | + $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
2782 | + } |
|
2783 | + } |
|
2784 | + } else { |
|
2785 | + $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
2786 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
2787 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
2788 | + if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
2789 | + $return_array[ $extra_meta_obj->key() ] = []; |
|
2790 | + } |
|
2791 | + $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
2792 | + } |
|
2793 | + } |
|
2794 | + } |
|
2795 | + return $return_array; |
|
2796 | + } |
|
2797 | + |
|
2798 | + |
|
2799 | + /** |
|
2800 | + * refresh_from_db |
|
2801 | + * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
2802 | + * |
|
2803 | + * @throws ReflectionException |
|
2804 | + * @throws InvalidArgumentException |
|
2805 | + * @throws InvalidInterfaceException |
|
2806 | + * @throws InvalidDataTypeException |
|
2807 | + * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
2808 | + * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
2809 | + */ |
|
2810 | + public function refresh_from_db() |
|
2811 | + { |
|
2812 | + if ($this->ID() && $this->in_entity_map()) { |
|
2813 | + $this->_model->refresh_entity_map_from_db($this->ID()); |
|
2814 | + } else { |
|
2815 | + // if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
2816 | + // if it has an ID but it's not in the map, and you're asking me to refresh it |
|
2817 | + // that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
2818 | + // absolutely nothing in it for this ID |
|
2819 | + if (WP_DEBUG) { |
|
2820 | + throw new EE_Error( |
|
2821 | + sprintf( |
|
2822 | + esc_html__( |
|
2823 | + '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.', |
|
2824 | + 'event_espresso' |
|
2825 | + ), |
|
2826 | + $this->ID(), |
|
2827 | + get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
2828 | + get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
2829 | + ) |
|
2830 | + ); |
|
2831 | + } |
|
2832 | + } |
|
2833 | + } |
|
2834 | + |
|
2835 | + |
|
2836 | + /** |
|
2837 | + * Nudges $field_name's value by $quantity, without any conditionals (in comparison to bumpConditionally()). |
|
2838 | + * Does not allow negative values, however. |
|
2839 | + * |
|
2840 | + * @param array $fields_n_quantities keys are the field names, and values are the amount by which to bump them |
|
2841 | + * (positive or negative). One important gotcha: all these values must be |
|
2842 | + * on the same table (eg don't pass in one field for the posts table and |
|
2843 | + * another for the event meta table.) |
|
2844 | + * @return bool |
|
2845 | + * @throws EE_Error |
|
2846 | + * @throws InvalidArgumentException |
|
2847 | + * @throws InvalidDataTypeException |
|
2848 | + * @throws InvalidInterfaceException |
|
2849 | + * @throws ReflectionException |
|
2850 | + * @since 4.9.80.p |
|
2851 | + */ |
|
2852 | + public function adjustNumericFieldsInDb(array $fields_n_quantities) |
|
2853 | + { |
|
2854 | + global $wpdb; |
|
2855 | + if (empty($fields_n_quantities)) { |
|
2856 | + // No fields to update? Well sure, we updated them to that value just fine. |
|
2857 | + return true; |
|
2858 | + } |
|
2859 | + $fields = []; |
|
2860 | + $set_sql_statements = []; |
|
2861 | + foreach ($fields_n_quantities as $field_name => $quantity) { |
|
2862 | + $field = $this->_model->field_settings_for($field_name); |
|
2863 | + $fields[] = $field; |
|
2864 | + $column_name = $field->get_table_column(); |
|
2865 | + |
|
2866 | + $abs_qty = absint($quantity); |
|
2867 | + if ($quantity > 0) { |
|
2868 | + // don't let the value be negative as often these fields are unsigned |
|
2869 | + $set_sql_statements[] = $wpdb->prepare( |
|
2870 | + "`{$column_name}` = `{$column_name}` + %d", |
|
2871 | + $abs_qty |
|
2872 | + ); |
|
2873 | + } else { |
|
2874 | + $set_sql_statements[] = $wpdb->prepare( |
|
2875 | + "`{$column_name}` = CASE |
|
2876 | 2876 | WHEN (`{$column_name}` >= %d) |
2877 | 2877 | THEN `{$column_name}` - %d |
2878 | 2878 | ELSE 0 |
2879 | 2879 | END", |
2880 | - $abs_qty, |
|
2881 | - $abs_qty |
|
2882 | - ); |
|
2883 | - } |
|
2884 | - } |
|
2885 | - return $this->updateFieldsInDB( |
|
2886 | - $fields, |
|
2887 | - implode(', ', $set_sql_statements) |
|
2888 | - ); |
|
2889 | - } |
|
2890 | - |
|
2891 | - |
|
2892 | - /** |
|
2893 | - * Change $fields' values to $new_value_sql (which is a string of raw SQL) |
|
2894 | - * |
|
2895 | - * @param EE_Model_Field_Base[] $fields |
|
2896 | - * @param string $new_value_sql |
|
2897 | - * example: 'column_name=123', |
|
2898 | - * or 'column_name=column_name+1', |
|
2899 | - * or 'column_name= CASE |
|
2900 | - * WHEN (`column_name` + `other_column` + 5) <= `yet_another_column` |
|
2901 | - * THEN `column_name` + 5 |
|
2902 | - * ELSE `column_name` |
|
2903 | - * END' |
|
2904 | - * Also updates $field on this model object with the latest value from the database. |
|
2905 | - * @return bool |
|
2906 | - * @throws EE_Error |
|
2907 | - * @throws InvalidArgumentException |
|
2908 | - * @throws InvalidDataTypeException |
|
2909 | - * @throws InvalidInterfaceException |
|
2910 | - * @throws ReflectionException |
|
2911 | - * @since 4.9.80.p |
|
2912 | - */ |
|
2913 | - protected function updateFieldsInDB($fields, $new_value_sql) |
|
2914 | - { |
|
2915 | - // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB |
|
2916 | - // if it wasn't even there to start off. |
|
2917 | - if (! $this->ID()) { |
|
2918 | - $this->save(); |
|
2919 | - } |
|
2920 | - global $wpdb; |
|
2921 | - if (empty($fields)) { |
|
2922 | - throw new InvalidArgumentException( |
|
2923 | - esc_html__( |
|
2924 | - 'EE_Base_Class::updateFieldsInDB was passed an empty array of fields.', |
|
2925 | - 'event_espresso' |
|
2926 | - ) |
|
2927 | - ); |
|
2928 | - } |
|
2929 | - $first_field = reset($fields); |
|
2930 | - $table_alias = $first_field->get_table_alias(); |
|
2931 | - foreach ($fields as $field) { |
|
2932 | - if ($table_alias !== $field->get_table_alias()) { |
|
2933 | - throw new InvalidArgumentException( |
|
2934 | - sprintf( |
|
2935 | - esc_html__( |
|
2936 | - // @codingStandardsIgnoreStart |
|
2937 | - '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.', |
|
2938 | - // @codingStandardsIgnoreEnd |
|
2939 | - 'event_espresso' |
|
2940 | - ), |
|
2941 | - $table_alias, |
|
2942 | - $field->get_table_alias() |
|
2943 | - ) |
|
2944 | - ); |
|
2945 | - } |
|
2946 | - } |
|
2947 | - // Ok the fields are now known to all be for the same table. Proceed with creating the SQL to update it. |
|
2948 | - $table_obj = $this->_model->get_table_obj_by_alias($table_alias); |
|
2949 | - $table_pk_value = $this->ID(); |
|
2950 | - $table_name = $table_obj->get_table_name(); |
|
2951 | - if ($table_obj instanceof EE_Secondary_Table) { |
|
2952 | - $table_pk_field_name = $table_obj->get_fk_on_table(); |
|
2953 | - } else { |
|
2954 | - $table_pk_field_name = $table_obj->get_pk_column(); |
|
2955 | - } |
|
2956 | - |
|
2957 | - $query = |
|
2958 | - "UPDATE `{$table_name}` |
|
2880 | + $abs_qty, |
|
2881 | + $abs_qty |
|
2882 | + ); |
|
2883 | + } |
|
2884 | + } |
|
2885 | + return $this->updateFieldsInDB( |
|
2886 | + $fields, |
|
2887 | + implode(', ', $set_sql_statements) |
|
2888 | + ); |
|
2889 | + } |
|
2890 | + |
|
2891 | + |
|
2892 | + /** |
|
2893 | + * Change $fields' values to $new_value_sql (which is a string of raw SQL) |
|
2894 | + * |
|
2895 | + * @param EE_Model_Field_Base[] $fields |
|
2896 | + * @param string $new_value_sql |
|
2897 | + * example: 'column_name=123', |
|
2898 | + * or 'column_name=column_name+1', |
|
2899 | + * or 'column_name= CASE |
|
2900 | + * WHEN (`column_name` + `other_column` + 5) <= `yet_another_column` |
|
2901 | + * THEN `column_name` + 5 |
|
2902 | + * ELSE `column_name` |
|
2903 | + * END' |
|
2904 | + * Also updates $field on this model object with the latest value from the database. |
|
2905 | + * @return bool |
|
2906 | + * @throws EE_Error |
|
2907 | + * @throws InvalidArgumentException |
|
2908 | + * @throws InvalidDataTypeException |
|
2909 | + * @throws InvalidInterfaceException |
|
2910 | + * @throws ReflectionException |
|
2911 | + * @since 4.9.80.p |
|
2912 | + */ |
|
2913 | + protected function updateFieldsInDB($fields, $new_value_sql) |
|
2914 | + { |
|
2915 | + // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB |
|
2916 | + // if it wasn't even there to start off. |
|
2917 | + if (! $this->ID()) { |
|
2918 | + $this->save(); |
|
2919 | + } |
|
2920 | + global $wpdb; |
|
2921 | + if (empty($fields)) { |
|
2922 | + throw new InvalidArgumentException( |
|
2923 | + esc_html__( |
|
2924 | + 'EE_Base_Class::updateFieldsInDB was passed an empty array of fields.', |
|
2925 | + 'event_espresso' |
|
2926 | + ) |
|
2927 | + ); |
|
2928 | + } |
|
2929 | + $first_field = reset($fields); |
|
2930 | + $table_alias = $first_field->get_table_alias(); |
|
2931 | + foreach ($fields as $field) { |
|
2932 | + if ($table_alias !== $field->get_table_alias()) { |
|
2933 | + throw new InvalidArgumentException( |
|
2934 | + sprintf( |
|
2935 | + esc_html__( |
|
2936 | + // @codingStandardsIgnoreStart |
|
2937 | + '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.', |
|
2938 | + // @codingStandardsIgnoreEnd |
|
2939 | + 'event_espresso' |
|
2940 | + ), |
|
2941 | + $table_alias, |
|
2942 | + $field->get_table_alias() |
|
2943 | + ) |
|
2944 | + ); |
|
2945 | + } |
|
2946 | + } |
|
2947 | + // Ok the fields are now known to all be for the same table. Proceed with creating the SQL to update it. |
|
2948 | + $table_obj = $this->_model->get_table_obj_by_alias($table_alias); |
|
2949 | + $table_pk_value = $this->ID(); |
|
2950 | + $table_name = $table_obj->get_table_name(); |
|
2951 | + if ($table_obj instanceof EE_Secondary_Table) { |
|
2952 | + $table_pk_field_name = $table_obj->get_fk_on_table(); |
|
2953 | + } else { |
|
2954 | + $table_pk_field_name = $table_obj->get_pk_column(); |
|
2955 | + } |
|
2956 | + |
|
2957 | + $query = |
|
2958 | + "UPDATE `{$table_name}` |
|
2959 | 2959 | SET " |
2960 | - . $new_value_sql |
|
2961 | - . $wpdb->prepare( |
|
2962 | - " |
|
2960 | + . $new_value_sql |
|
2961 | + . $wpdb->prepare( |
|
2962 | + " |
|
2963 | 2963 | WHERE `{$table_pk_field_name}` = %d;", |
2964 | - $table_pk_value |
|
2965 | - ); |
|
2966 | - $result = $wpdb->query($query); |
|
2967 | - foreach ($fields as $field) { |
|
2968 | - // If it was successful, we'd like to know the new value. |
|
2969 | - // If it failed, we'd also like to know the new value. |
|
2970 | - $new_value = $this->_model->get_var( |
|
2971 | - $this->_model->alter_query_params_to_restrict_by_ID( |
|
2972 | - $this->_model->get_index_primary_key_string( |
|
2973 | - $this->model_field_array() |
|
2974 | - ), |
|
2975 | - [ |
|
2976 | - 'default_where_conditions' => 'minimum', |
|
2977 | - ] |
|
2978 | - ), |
|
2979 | - $field->get_name() |
|
2980 | - ); |
|
2981 | - $this->set_from_db( |
|
2982 | - $field->get_name(), |
|
2983 | - $new_value |
|
2984 | - ); |
|
2985 | - } |
|
2986 | - return (bool) $result; |
|
2987 | - } |
|
2988 | - |
|
2989 | - |
|
2990 | - /** |
|
2991 | - * This simply returns an array of model fields for this object |
|
2992 | - * |
|
2993 | - * @return array |
|
2994 | - * @throws InvalidArgumentException |
|
2995 | - * @throws InvalidInterfaceException |
|
2996 | - * @throws InvalidDataTypeException |
|
2997 | - * @throws EE_Error |
|
2998 | - */ |
|
2999 | - public function model_field_array(): array |
|
3000 | - { |
|
3001 | - $fields = $this->_model->field_settings(); |
|
3002 | - $properties = []; |
|
3003 | - // remove prepended underscore |
|
3004 | - foreach ($fields as $field_name => $settings) { |
|
3005 | - $properties[ $field_name ] = $this->get($field_name); |
|
3006 | - } |
|
3007 | - return $properties; |
|
3008 | - } |
|
3009 | - |
|
3010 | - |
|
3011 | - /** |
|
3012 | - * Increases the value of the field $field_name_to_bump by $quantity, but only if the values of |
|
3013 | - * $field_name_to_bump plus $field_name_affecting_total and $quantity won't exceed $limit_field_name's value. |
|
3014 | - * For example, this is useful when bumping the value of TKT_reserved, TKT_sold, DTT_reserved or DTT_sold. |
|
3015 | - * Returns true if the value was successfully bumped, and updates the value on this model object. |
|
3016 | - * Otherwise returns false. |
|
3017 | - * |
|
3018 | - * @param string $field_name_to_bump |
|
3019 | - * @param string $field_name_affecting_total |
|
3020 | - * @param string $limit_field_name |
|
3021 | - * @param int $quantity |
|
3022 | - * @return bool |
|
3023 | - * @throws EE_Error |
|
3024 | - * @throws InvalidArgumentException |
|
3025 | - * @throws InvalidDataTypeException |
|
3026 | - * @throws InvalidInterfaceException |
|
3027 | - * @throws ReflectionException |
|
3028 | - * @since 4.9.80.p |
|
3029 | - */ |
|
3030 | - public function incrementFieldConditionallyInDb( |
|
3031 | - $field_name_to_bump, |
|
3032 | - $field_name_affecting_total, |
|
3033 | - $limit_field_name, |
|
3034 | - $quantity |
|
3035 | - ) { |
|
3036 | - global $wpdb; |
|
3037 | - $field = $this->_model->field_settings_for($field_name_to_bump); |
|
3038 | - $column_name = $field->get_table_column(); |
|
3039 | - |
|
3040 | - $field_affecting_total = $this->_model->field_settings_for($field_name_affecting_total); |
|
3041 | - $column_affecting_total = $field_affecting_total->get_table_column(); |
|
3042 | - |
|
3043 | - $limiting_field = $this->_model->field_settings_for($limit_field_name); |
|
3044 | - $limiting_column = $limiting_field->get_table_column(); |
|
3045 | - return $this->updateFieldsInDB( |
|
3046 | - [$field], |
|
3047 | - $wpdb->prepare( |
|
3048 | - "`{$column_name}` = |
|
2964 | + $table_pk_value |
|
2965 | + ); |
|
2966 | + $result = $wpdb->query($query); |
|
2967 | + foreach ($fields as $field) { |
|
2968 | + // If it was successful, we'd like to know the new value. |
|
2969 | + // If it failed, we'd also like to know the new value. |
|
2970 | + $new_value = $this->_model->get_var( |
|
2971 | + $this->_model->alter_query_params_to_restrict_by_ID( |
|
2972 | + $this->_model->get_index_primary_key_string( |
|
2973 | + $this->model_field_array() |
|
2974 | + ), |
|
2975 | + [ |
|
2976 | + 'default_where_conditions' => 'minimum', |
|
2977 | + ] |
|
2978 | + ), |
|
2979 | + $field->get_name() |
|
2980 | + ); |
|
2981 | + $this->set_from_db( |
|
2982 | + $field->get_name(), |
|
2983 | + $new_value |
|
2984 | + ); |
|
2985 | + } |
|
2986 | + return (bool) $result; |
|
2987 | + } |
|
2988 | + |
|
2989 | + |
|
2990 | + /** |
|
2991 | + * This simply returns an array of model fields for this object |
|
2992 | + * |
|
2993 | + * @return array |
|
2994 | + * @throws InvalidArgumentException |
|
2995 | + * @throws InvalidInterfaceException |
|
2996 | + * @throws InvalidDataTypeException |
|
2997 | + * @throws EE_Error |
|
2998 | + */ |
|
2999 | + public function model_field_array(): array |
|
3000 | + { |
|
3001 | + $fields = $this->_model->field_settings(); |
|
3002 | + $properties = []; |
|
3003 | + // remove prepended underscore |
|
3004 | + foreach ($fields as $field_name => $settings) { |
|
3005 | + $properties[ $field_name ] = $this->get($field_name); |
|
3006 | + } |
|
3007 | + return $properties; |
|
3008 | + } |
|
3009 | + |
|
3010 | + |
|
3011 | + /** |
|
3012 | + * Increases the value of the field $field_name_to_bump by $quantity, but only if the values of |
|
3013 | + * $field_name_to_bump plus $field_name_affecting_total and $quantity won't exceed $limit_field_name's value. |
|
3014 | + * For example, this is useful when bumping the value of TKT_reserved, TKT_sold, DTT_reserved or DTT_sold. |
|
3015 | + * Returns true if the value was successfully bumped, and updates the value on this model object. |
|
3016 | + * Otherwise returns false. |
|
3017 | + * |
|
3018 | + * @param string $field_name_to_bump |
|
3019 | + * @param string $field_name_affecting_total |
|
3020 | + * @param string $limit_field_name |
|
3021 | + * @param int $quantity |
|
3022 | + * @return bool |
|
3023 | + * @throws EE_Error |
|
3024 | + * @throws InvalidArgumentException |
|
3025 | + * @throws InvalidDataTypeException |
|
3026 | + * @throws InvalidInterfaceException |
|
3027 | + * @throws ReflectionException |
|
3028 | + * @since 4.9.80.p |
|
3029 | + */ |
|
3030 | + public function incrementFieldConditionallyInDb( |
|
3031 | + $field_name_to_bump, |
|
3032 | + $field_name_affecting_total, |
|
3033 | + $limit_field_name, |
|
3034 | + $quantity |
|
3035 | + ) { |
|
3036 | + global $wpdb; |
|
3037 | + $field = $this->_model->field_settings_for($field_name_to_bump); |
|
3038 | + $column_name = $field->get_table_column(); |
|
3039 | + |
|
3040 | + $field_affecting_total = $this->_model->field_settings_for($field_name_affecting_total); |
|
3041 | + $column_affecting_total = $field_affecting_total->get_table_column(); |
|
3042 | + |
|
3043 | + $limiting_field = $this->_model->field_settings_for($limit_field_name); |
|
3044 | + $limiting_column = $limiting_field->get_table_column(); |
|
3045 | + return $this->updateFieldsInDB( |
|
3046 | + [$field], |
|
3047 | + $wpdb->prepare( |
|
3048 | + "`{$column_name}` = |
|
3049 | 3049 | CASE |
3050 | 3050 | WHEN ((`{$column_name}` + `{$column_affecting_total}` + %d) <= `{$limiting_column}`) OR `{$limiting_column}` = %d |
3051 | 3051 | THEN `{$column_name}` + %d |
3052 | 3052 | ELSE `{$column_name}` |
3053 | 3053 | END", |
3054 | - $quantity, |
|
3055 | - EE_INF_IN_DB, |
|
3056 | - $quantity |
|
3057 | - ) |
|
3058 | - ); |
|
3059 | - } |
|
3060 | - |
|
3061 | - |
|
3062 | - /** |
|
3063 | - * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
3064 | - * (probably a bad assumption they have made, oh well) |
|
3065 | - * |
|
3066 | - * @return string |
|
3067 | - */ |
|
3068 | - public function __toString() |
|
3069 | - { |
|
3070 | - try { |
|
3071 | - return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
3072 | - } catch (Exception $e) { |
|
3073 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
3074 | - return ''; |
|
3075 | - } |
|
3076 | - } |
|
3077 | - |
|
3078 | - |
|
3079 | - /** |
|
3080 | - * Gets a pretty nice displayable nice for this model object. Often overridden |
|
3081 | - * |
|
3082 | - * @return string |
|
3083 | - * @throws InvalidArgumentException |
|
3084 | - * @throws InvalidInterfaceException |
|
3085 | - * @throws InvalidDataTypeException |
|
3086 | - * @throws EE_Error |
|
3087 | - */ |
|
3088 | - public function name() |
|
3089 | - { |
|
3090 | - // find a field that's not a text field |
|
3091 | - $field_we_can_use = $this->_model->get_a_field_of_type('EE_Text_Field_Base'); |
|
3092 | - if ($field_we_can_use) { |
|
3093 | - return $this->get($field_we_can_use->get_name()); |
|
3094 | - } |
|
3095 | - $first_few_properties = $this->model_field_array(); |
|
3096 | - $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
3097 | - $name_parts = []; |
|
3098 | - foreach ($first_few_properties as $name => $value) { |
|
3099 | - $name_parts[] = "$name:$value"; |
|
3100 | - } |
|
3101 | - return implode(',', $name_parts); |
|
3102 | - } |
|
3103 | - |
|
3104 | - |
|
3105 | - /** |
|
3106 | - * Clear related model objects if they're already in the DB, because otherwise when we |
|
3107 | - * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
3108 | - * This means if we have made changes to those related model objects, and want to unserialize |
|
3109 | - * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
3110 | - * Instead, those related model objects should be directly serialized and stored. |
|
3111 | - * Eg, the following won't work: |
|
3112 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
3113 | - * $att = $reg->attendee(); |
|
3114 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
3115 | - * update_option( 'my_option', serialize( $reg ) ); |
|
3116 | - * //END REQUEST |
|
3117 | - * //START NEXT REQUEST |
|
3118 | - * $reg = get_option( 'my_option' ); |
|
3119 | - * $reg->attendee()->save(); |
|
3120 | - * And would need to be replace with: |
|
3121 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
3122 | - * $att = $reg->attendee(); |
|
3123 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
3124 | - * update_option( 'my_option', serialize( $reg ) ); |
|
3125 | - * //END REQUEST |
|
3126 | - * //START NEXT REQUEST |
|
3127 | - * $att = get_option( 'my_option' ); |
|
3128 | - * $att->save(); |
|
3129 | - * |
|
3130 | - * @return array |
|
3131 | - * @throws ReflectionException |
|
3132 | - * @throws InvalidArgumentException |
|
3133 | - * @throws InvalidInterfaceException |
|
3134 | - * @throws InvalidDataTypeException |
|
3135 | - * @throws EE_Error |
|
3136 | - */ |
|
3137 | - public function __sleep() |
|
3138 | - { |
|
3139 | - foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
3140 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
3141 | - $classname = 'EE_' . $this->_model->get_this_model_name(); |
|
3142 | - if ( |
|
3143 | - $this->get_one_from_cache($relation_name) instanceof $classname |
|
3144 | - && $this->get_one_from_cache($relation_name)->ID() |
|
3145 | - ) { |
|
3146 | - $this->clear_cache( |
|
3147 | - $relation_name, |
|
3148 | - $this->get_one_from_cache($relation_name)->ID() |
|
3149 | - ); |
|
3150 | - } |
|
3151 | - } |
|
3152 | - } |
|
3153 | - $this->_props_n_values_provided_in_constructor = []; |
|
3154 | - $properties_to_serialize = get_object_vars($this); |
|
3155 | - // don't serialize the model. It's big and that risks recursion |
|
3156 | - unset($properties_to_serialize['_model']); |
|
3157 | - return array_keys($properties_to_serialize); |
|
3158 | - } |
|
3159 | - |
|
3160 | - |
|
3161 | - /** |
|
3162 | - * restore _props_n_values_provided_in_constructor |
|
3163 | - * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
3164 | - * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
3165 | - * At best, you would only be able to detect if state change has occurred during THIS request. |
|
3166 | - * |
|
3167 | - * @throws EE_Error |
|
3168 | - * @throws ReflectionException |
|
3169 | - */ |
|
3170 | - public function __wakeup() |
|
3171 | - { |
|
3172 | - $this->_model = $this->get_model(); |
|
3173 | - $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
3174 | - } |
|
3175 | - |
|
3176 | - |
|
3177 | - /** |
|
3178 | - * Usage of this magic method is to ensure any internally cached references to object instances that must remain |
|
3179 | - * distinct with the clone host instance are also cloned. |
|
3180 | - */ |
|
3181 | - public function __clone() |
|
3182 | - { |
|
3183 | - // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
|
3184 | - foreach ($this->_fields as $field => $value) { |
|
3185 | - if ($value instanceof DateTime) { |
|
3186 | - $this->_fields[ $field ] = clone $value; |
|
3187 | - } |
|
3188 | - } |
|
3189 | - } |
|
3190 | - |
|
3191 | - |
|
3192 | - /** |
|
3193 | - * Ensures that this related thing is a model object. |
|
3194 | - * |
|
3195 | - * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
3196 | - * @param string $model_name name of the related thing, eg 'Attendee', |
|
3197 | - * @return EE_Base_Class |
|
3198 | - * @throws ReflectionException |
|
3199 | - * @throws InvalidArgumentException |
|
3200 | - * @throws InvalidInterfaceException |
|
3201 | - * @throws InvalidDataTypeException |
|
3202 | - * @throws EE_Error |
|
3203 | - */ |
|
3204 | - protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
3205 | - { |
|
3206 | - $other_model_instance = self::_get_model_instance_with_name( |
|
3207 | - self::_get_model_classname($model_name), |
|
3208 | - $this->_timezone |
|
3209 | - ); |
|
3210 | - return $other_model_instance->ensure_is_obj($object_or_id); |
|
3211 | - } |
|
3212 | - |
|
3213 | - |
|
3214 | - /** |
|
3215 | - * sets the time on a datetime property |
|
3216 | - * |
|
3217 | - * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
3218 | - * @param string $field_name the name of the field the time is being set on (must match a |
|
3219 | - * EE_Datetime_Field) |
|
3220 | - * @throws InvalidArgumentException |
|
3221 | - * @throws InvalidInterfaceException |
|
3222 | - * @throws InvalidDataTypeException |
|
3223 | - * @throws EE_Error |
|
3224 | - */ |
|
3225 | - protected function _set_time_for($time, string $field_name) |
|
3226 | - { |
|
3227 | - $this->_set_date_time('T', $time, $field_name); |
|
3228 | - } |
|
3229 | - |
|
3230 | - |
|
3231 | - /** |
|
3232 | - * This takes care of setting a date or time independently on a given model object property. This method also |
|
3233 | - * verifies that the given field name matches a model object property and is for a EE_Datetime_Field field |
|
3234 | - * |
|
3235 | - * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
3236 | - * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
3237 | - * @param string $field_name the name of the field the date OR time is being set on (must match a |
|
3238 | - * EE_Datetime_Field property) |
|
3239 | - * @throws InvalidArgumentException |
|
3240 | - * @throws InvalidInterfaceException |
|
3241 | - * @throws InvalidDataTypeException |
|
3242 | - * @throws EE_Error |
|
3243 | - */ |
|
3244 | - protected function _set_date_time($what, $datetime_value, string $field_name) |
|
3245 | - { |
|
3246 | - $field = $this->_get_dtt_field_settings($field_name); |
|
3247 | - $field->set_timezone($this->_timezone); |
|
3248 | - $field->set_date_format($this->_dt_frmt); |
|
3249 | - $field->set_time_format($this->_tm_frmt); |
|
3250 | - $what = in_array($what, ['T', 'D', 'B']) ? $what : 'T'; |
|
3251 | - switch ($what) { |
|
3252 | - case 'T': |
|
3253 | - $this->_fields[ $field_name ] = $field->prepare_for_set_with_new_time( |
|
3254 | - $datetime_value, |
|
3255 | - $this->_fields[ $field_name ] |
|
3256 | - ); |
|
3257 | - $this->_has_changes = true; |
|
3258 | - break; |
|
3259 | - case 'D': |
|
3260 | - $this->_fields[ $field_name ] = $field->prepare_for_set_with_new_date( |
|
3261 | - $datetime_value, |
|
3262 | - $this->_fields[ $field_name ] |
|
3263 | - ); |
|
3264 | - $this->_has_changes = true; |
|
3265 | - break; |
|
3266 | - case 'B': |
|
3267 | - $this->_fields[ $field_name ] = $field->prepare_for_set($datetime_value); |
|
3268 | - $this->_has_changes = true; |
|
3269 | - break; |
|
3270 | - } |
|
3271 | - $this->_clear_cached_property($field_name); |
|
3272 | - } |
|
3273 | - |
|
3274 | - |
|
3275 | - /** |
|
3276 | - * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
3277 | - * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
3278 | - * thrown. |
|
3279 | - * |
|
3280 | - * @param string string $field_name The field name being checked |
|
3281 | - * @return EE_Datetime_Field |
|
3282 | - * @throws InvalidArgumentException |
|
3283 | - * @throws InvalidInterfaceException |
|
3284 | - * @throws InvalidDataTypeException |
|
3285 | - * @throws EE_Error |
|
3286 | - */ |
|
3287 | - protected function _get_dtt_field_settings($field_name) |
|
3288 | - { |
|
3289 | - $field = $this->_model->field_settings_for($field_name); |
|
3290 | - // check if field is dtt |
|
3291 | - if ($field instanceof EE_Datetime_Field) { |
|
3292 | - return $field; |
|
3293 | - } |
|
3294 | - throw new EE_Error( |
|
3295 | - sprintf( |
|
3296 | - esc_html__( |
|
3297 | - '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', |
|
3298 | - 'event_espresso' |
|
3299 | - ), |
|
3300 | - $field_name, |
|
3301 | - self::_get_model_classname(get_class($this)) |
|
3302 | - ) |
|
3303 | - ); |
|
3304 | - } |
|
3305 | - |
|
3306 | - |
|
3307 | - /** |
|
3308 | - * sets the date on a datetime property |
|
3309 | - * |
|
3310 | - * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
3311 | - * @param string $field_name the name of the field the date is being set on (must match a |
|
3312 | - * EE_Datetime_Field) |
|
3313 | - * @throws InvalidArgumentException |
|
3314 | - * @throws InvalidInterfaceException |
|
3315 | - * @throws InvalidDataTypeException |
|
3316 | - * @throws EE_Error |
|
3317 | - */ |
|
3318 | - protected function _set_date_for($date, string $field_name) |
|
3319 | - { |
|
3320 | - $this->_set_date_time('D', $date, $field_name); |
|
3321 | - } |
|
3322 | - |
|
3323 | - |
|
3324 | - /** |
|
3325 | - * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
3326 | - * EE_Error exception if they don't |
|
3327 | - * |
|
3328 | - * @param mixed (string|array) $properties properties to check |
|
3329 | - * @return bool TRUE if existing, throw EE_Error if not. |
|
3330 | - * @throws EE_Error |
|
3331 | - */ |
|
3332 | - protected function _property_exists($properties) |
|
3333 | - { |
|
3334 | - foreach ((array) $properties as $property_name) { |
|
3335 | - // first make sure this property exists |
|
3336 | - if (! $this->_fields[ $property_name ]) { |
|
3337 | - throw new EE_Error( |
|
3338 | - sprintf( |
|
3339 | - esc_html__( |
|
3340 | - 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
3341 | - 'event_espresso' |
|
3342 | - ), |
|
3343 | - $property_name |
|
3344 | - ) |
|
3345 | - ); |
|
3346 | - } |
|
3347 | - } |
|
3348 | - return true; |
|
3349 | - } |
|
3350 | - |
|
3351 | - |
|
3352 | - /** |
|
3353 | - * @param array $date_formats |
|
3354 | - * @since $VID:$ |
|
3355 | - */ |
|
3356 | - private function setDateAndTimeFormats(array $date_formats) |
|
3357 | - { |
|
3358 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
3359 | - [$this->_dt_frmt, $this->_tm_frmt] = $date_formats; |
|
3360 | - } else { |
|
3361 | - // set default formats for date and time |
|
3362 | - $this->_dt_frmt = (string) get_option('date_format', 'Y-m-d'); |
|
3363 | - $this->_tm_frmt = (string) get_option('time_format', 'g:i a'); |
|
3364 | - } |
|
3365 | - } |
|
3366 | - |
|
3367 | - |
|
3368 | - /** |
|
3369 | - * @param array $model_fields |
|
3370 | - * @param array $fieldValues |
|
3371 | - * @throws EE_Error |
|
3372 | - * @since $VID:$ |
|
3373 | - */ |
|
3374 | - private function validateFieldValues(array $model_fields, array $fieldValues) |
|
3375 | - { |
|
3376 | - // verify client code has not passed any invalid field names |
|
3377 | - foreach ($fieldValues as $field_name => $field_value) { |
|
3378 | - if (! isset($model_fields[ $field_name ])) { |
|
3379 | - throw new EE_Error( |
|
3380 | - sprintf( |
|
3381 | - esc_html__( |
|
3382 | - 'Invalid field (%s) passed to constructor of %s. Allowed fields are :%s', |
|
3383 | - 'event_espresso' |
|
3384 | - ), |
|
3385 | - $field_name, |
|
3386 | - get_class($this), |
|
3387 | - implode(', ', array_keys($model_fields)) |
|
3388 | - ) |
|
3389 | - ); |
|
3390 | - } |
|
3391 | - } |
|
3392 | - } |
|
3393 | - |
|
3394 | - |
|
3395 | - /** |
|
3396 | - * @param array $model_fields |
|
3397 | - * @param array $fieldValues |
|
3398 | - * @param bool $set_from_db |
|
3399 | - * @throws EE_Error |
|
3400 | - * @throws ReflectionException |
|
3401 | - * @since $VID:$ |
|
3402 | - */ |
|
3403 | - private function setFieldValues(array $model_fields, array $fieldValues, bool $set_from_db) |
|
3404 | - { |
|
3405 | - foreach ($model_fields as $fieldName => $field) { |
|
3406 | - // if db model is instantiating |
|
3407 | - if ($set_from_db) { |
|
3408 | - // client code has indicated these field values are from the database |
|
3409 | - $this->set_from_db( |
|
3410 | - $fieldName, |
|
3411 | - $fieldValues[ $fieldName ] ?? null |
|
3412 | - ); |
|
3413 | - } else { |
|
3414 | - // we're constructing a brand new instance of the model object. |
|
3415 | - // Generally, this means we'll need to do more field validation |
|
3416 | - $this->set( |
|
3417 | - $fieldName, |
|
3418 | - $fieldValues[ $fieldName ] ?? null, |
|
3419 | - true |
|
3420 | - ); |
|
3421 | - } |
|
3422 | - } |
|
3423 | - } |
|
3054 | + $quantity, |
|
3055 | + EE_INF_IN_DB, |
|
3056 | + $quantity |
|
3057 | + ) |
|
3058 | + ); |
|
3059 | + } |
|
3060 | + |
|
3061 | + |
|
3062 | + /** |
|
3063 | + * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
3064 | + * (probably a bad assumption they have made, oh well) |
|
3065 | + * |
|
3066 | + * @return string |
|
3067 | + */ |
|
3068 | + public function __toString() |
|
3069 | + { |
|
3070 | + try { |
|
3071 | + return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
3072 | + } catch (Exception $e) { |
|
3073 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
3074 | + return ''; |
|
3075 | + } |
|
3076 | + } |
|
3077 | + |
|
3078 | + |
|
3079 | + /** |
|
3080 | + * Gets a pretty nice displayable nice for this model object. Often overridden |
|
3081 | + * |
|
3082 | + * @return string |
|
3083 | + * @throws InvalidArgumentException |
|
3084 | + * @throws InvalidInterfaceException |
|
3085 | + * @throws InvalidDataTypeException |
|
3086 | + * @throws EE_Error |
|
3087 | + */ |
|
3088 | + public function name() |
|
3089 | + { |
|
3090 | + // find a field that's not a text field |
|
3091 | + $field_we_can_use = $this->_model->get_a_field_of_type('EE_Text_Field_Base'); |
|
3092 | + if ($field_we_can_use) { |
|
3093 | + return $this->get($field_we_can_use->get_name()); |
|
3094 | + } |
|
3095 | + $first_few_properties = $this->model_field_array(); |
|
3096 | + $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
3097 | + $name_parts = []; |
|
3098 | + foreach ($first_few_properties as $name => $value) { |
|
3099 | + $name_parts[] = "$name:$value"; |
|
3100 | + } |
|
3101 | + return implode(',', $name_parts); |
|
3102 | + } |
|
3103 | + |
|
3104 | + |
|
3105 | + /** |
|
3106 | + * Clear related model objects if they're already in the DB, because otherwise when we |
|
3107 | + * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
3108 | + * This means if we have made changes to those related model objects, and want to unserialize |
|
3109 | + * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
3110 | + * Instead, those related model objects should be directly serialized and stored. |
|
3111 | + * Eg, the following won't work: |
|
3112 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
3113 | + * $att = $reg->attendee(); |
|
3114 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
3115 | + * update_option( 'my_option', serialize( $reg ) ); |
|
3116 | + * //END REQUEST |
|
3117 | + * //START NEXT REQUEST |
|
3118 | + * $reg = get_option( 'my_option' ); |
|
3119 | + * $reg->attendee()->save(); |
|
3120 | + * And would need to be replace with: |
|
3121 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
3122 | + * $att = $reg->attendee(); |
|
3123 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
3124 | + * update_option( 'my_option', serialize( $reg ) ); |
|
3125 | + * //END REQUEST |
|
3126 | + * //START NEXT REQUEST |
|
3127 | + * $att = get_option( 'my_option' ); |
|
3128 | + * $att->save(); |
|
3129 | + * |
|
3130 | + * @return array |
|
3131 | + * @throws ReflectionException |
|
3132 | + * @throws InvalidArgumentException |
|
3133 | + * @throws InvalidInterfaceException |
|
3134 | + * @throws InvalidDataTypeException |
|
3135 | + * @throws EE_Error |
|
3136 | + */ |
|
3137 | + public function __sleep() |
|
3138 | + { |
|
3139 | + foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
3140 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
3141 | + $classname = 'EE_' . $this->_model->get_this_model_name(); |
|
3142 | + if ( |
|
3143 | + $this->get_one_from_cache($relation_name) instanceof $classname |
|
3144 | + && $this->get_one_from_cache($relation_name)->ID() |
|
3145 | + ) { |
|
3146 | + $this->clear_cache( |
|
3147 | + $relation_name, |
|
3148 | + $this->get_one_from_cache($relation_name)->ID() |
|
3149 | + ); |
|
3150 | + } |
|
3151 | + } |
|
3152 | + } |
|
3153 | + $this->_props_n_values_provided_in_constructor = []; |
|
3154 | + $properties_to_serialize = get_object_vars($this); |
|
3155 | + // don't serialize the model. It's big and that risks recursion |
|
3156 | + unset($properties_to_serialize['_model']); |
|
3157 | + return array_keys($properties_to_serialize); |
|
3158 | + } |
|
3159 | + |
|
3160 | + |
|
3161 | + /** |
|
3162 | + * restore _props_n_values_provided_in_constructor |
|
3163 | + * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
3164 | + * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
3165 | + * At best, you would only be able to detect if state change has occurred during THIS request. |
|
3166 | + * |
|
3167 | + * @throws EE_Error |
|
3168 | + * @throws ReflectionException |
|
3169 | + */ |
|
3170 | + public function __wakeup() |
|
3171 | + { |
|
3172 | + $this->_model = $this->get_model(); |
|
3173 | + $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
3174 | + } |
|
3175 | + |
|
3176 | + |
|
3177 | + /** |
|
3178 | + * Usage of this magic method is to ensure any internally cached references to object instances that must remain |
|
3179 | + * distinct with the clone host instance are also cloned. |
|
3180 | + */ |
|
3181 | + public function __clone() |
|
3182 | + { |
|
3183 | + // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
|
3184 | + foreach ($this->_fields as $field => $value) { |
|
3185 | + if ($value instanceof DateTime) { |
|
3186 | + $this->_fields[ $field ] = clone $value; |
|
3187 | + } |
|
3188 | + } |
|
3189 | + } |
|
3190 | + |
|
3191 | + |
|
3192 | + /** |
|
3193 | + * Ensures that this related thing is a model object. |
|
3194 | + * |
|
3195 | + * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
3196 | + * @param string $model_name name of the related thing, eg 'Attendee', |
|
3197 | + * @return EE_Base_Class |
|
3198 | + * @throws ReflectionException |
|
3199 | + * @throws InvalidArgumentException |
|
3200 | + * @throws InvalidInterfaceException |
|
3201 | + * @throws InvalidDataTypeException |
|
3202 | + * @throws EE_Error |
|
3203 | + */ |
|
3204 | + protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
3205 | + { |
|
3206 | + $other_model_instance = self::_get_model_instance_with_name( |
|
3207 | + self::_get_model_classname($model_name), |
|
3208 | + $this->_timezone |
|
3209 | + ); |
|
3210 | + return $other_model_instance->ensure_is_obj($object_or_id); |
|
3211 | + } |
|
3212 | + |
|
3213 | + |
|
3214 | + /** |
|
3215 | + * sets the time on a datetime property |
|
3216 | + * |
|
3217 | + * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
3218 | + * @param string $field_name the name of the field the time is being set on (must match a |
|
3219 | + * EE_Datetime_Field) |
|
3220 | + * @throws InvalidArgumentException |
|
3221 | + * @throws InvalidInterfaceException |
|
3222 | + * @throws InvalidDataTypeException |
|
3223 | + * @throws EE_Error |
|
3224 | + */ |
|
3225 | + protected function _set_time_for($time, string $field_name) |
|
3226 | + { |
|
3227 | + $this->_set_date_time('T', $time, $field_name); |
|
3228 | + } |
|
3229 | + |
|
3230 | + |
|
3231 | + /** |
|
3232 | + * This takes care of setting a date or time independently on a given model object property. This method also |
|
3233 | + * verifies that the given field name matches a model object property and is for a EE_Datetime_Field field |
|
3234 | + * |
|
3235 | + * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
3236 | + * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
3237 | + * @param string $field_name the name of the field the date OR time is being set on (must match a |
|
3238 | + * EE_Datetime_Field property) |
|
3239 | + * @throws InvalidArgumentException |
|
3240 | + * @throws InvalidInterfaceException |
|
3241 | + * @throws InvalidDataTypeException |
|
3242 | + * @throws EE_Error |
|
3243 | + */ |
|
3244 | + protected function _set_date_time($what, $datetime_value, string $field_name) |
|
3245 | + { |
|
3246 | + $field = $this->_get_dtt_field_settings($field_name); |
|
3247 | + $field->set_timezone($this->_timezone); |
|
3248 | + $field->set_date_format($this->_dt_frmt); |
|
3249 | + $field->set_time_format($this->_tm_frmt); |
|
3250 | + $what = in_array($what, ['T', 'D', 'B']) ? $what : 'T'; |
|
3251 | + switch ($what) { |
|
3252 | + case 'T': |
|
3253 | + $this->_fields[ $field_name ] = $field->prepare_for_set_with_new_time( |
|
3254 | + $datetime_value, |
|
3255 | + $this->_fields[ $field_name ] |
|
3256 | + ); |
|
3257 | + $this->_has_changes = true; |
|
3258 | + break; |
|
3259 | + case 'D': |
|
3260 | + $this->_fields[ $field_name ] = $field->prepare_for_set_with_new_date( |
|
3261 | + $datetime_value, |
|
3262 | + $this->_fields[ $field_name ] |
|
3263 | + ); |
|
3264 | + $this->_has_changes = true; |
|
3265 | + break; |
|
3266 | + case 'B': |
|
3267 | + $this->_fields[ $field_name ] = $field->prepare_for_set($datetime_value); |
|
3268 | + $this->_has_changes = true; |
|
3269 | + break; |
|
3270 | + } |
|
3271 | + $this->_clear_cached_property($field_name); |
|
3272 | + } |
|
3273 | + |
|
3274 | + |
|
3275 | + /** |
|
3276 | + * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
3277 | + * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
3278 | + * thrown. |
|
3279 | + * |
|
3280 | + * @param string string $field_name The field name being checked |
|
3281 | + * @return EE_Datetime_Field |
|
3282 | + * @throws InvalidArgumentException |
|
3283 | + * @throws InvalidInterfaceException |
|
3284 | + * @throws InvalidDataTypeException |
|
3285 | + * @throws EE_Error |
|
3286 | + */ |
|
3287 | + protected function _get_dtt_field_settings($field_name) |
|
3288 | + { |
|
3289 | + $field = $this->_model->field_settings_for($field_name); |
|
3290 | + // check if field is dtt |
|
3291 | + if ($field instanceof EE_Datetime_Field) { |
|
3292 | + return $field; |
|
3293 | + } |
|
3294 | + throw new EE_Error( |
|
3295 | + sprintf( |
|
3296 | + esc_html__( |
|
3297 | + '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', |
|
3298 | + 'event_espresso' |
|
3299 | + ), |
|
3300 | + $field_name, |
|
3301 | + self::_get_model_classname(get_class($this)) |
|
3302 | + ) |
|
3303 | + ); |
|
3304 | + } |
|
3305 | + |
|
3306 | + |
|
3307 | + /** |
|
3308 | + * sets the date on a datetime property |
|
3309 | + * |
|
3310 | + * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
3311 | + * @param string $field_name the name of the field the date is being set on (must match a |
|
3312 | + * EE_Datetime_Field) |
|
3313 | + * @throws InvalidArgumentException |
|
3314 | + * @throws InvalidInterfaceException |
|
3315 | + * @throws InvalidDataTypeException |
|
3316 | + * @throws EE_Error |
|
3317 | + */ |
|
3318 | + protected function _set_date_for($date, string $field_name) |
|
3319 | + { |
|
3320 | + $this->_set_date_time('D', $date, $field_name); |
|
3321 | + } |
|
3322 | + |
|
3323 | + |
|
3324 | + /** |
|
3325 | + * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
3326 | + * EE_Error exception if they don't |
|
3327 | + * |
|
3328 | + * @param mixed (string|array) $properties properties to check |
|
3329 | + * @return bool TRUE if existing, throw EE_Error if not. |
|
3330 | + * @throws EE_Error |
|
3331 | + */ |
|
3332 | + protected function _property_exists($properties) |
|
3333 | + { |
|
3334 | + foreach ((array) $properties as $property_name) { |
|
3335 | + // first make sure this property exists |
|
3336 | + if (! $this->_fields[ $property_name ]) { |
|
3337 | + throw new EE_Error( |
|
3338 | + sprintf( |
|
3339 | + esc_html__( |
|
3340 | + 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
3341 | + 'event_espresso' |
|
3342 | + ), |
|
3343 | + $property_name |
|
3344 | + ) |
|
3345 | + ); |
|
3346 | + } |
|
3347 | + } |
|
3348 | + return true; |
|
3349 | + } |
|
3350 | + |
|
3351 | + |
|
3352 | + /** |
|
3353 | + * @param array $date_formats |
|
3354 | + * @since $VID:$ |
|
3355 | + */ |
|
3356 | + private function setDateAndTimeFormats(array $date_formats) |
|
3357 | + { |
|
3358 | + if (! empty($date_formats) && is_array($date_formats)) { |
|
3359 | + [$this->_dt_frmt, $this->_tm_frmt] = $date_formats; |
|
3360 | + } else { |
|
3361 | + // set default formats for date and time |
|
3362 | + $this->_dt_frmt = (string) get_option('date_format', 'Y-m-d'); |
|
3363 | + $this->_tm_frmt = (string) get_option('time_format', 'g:i a'); |
|
3364 | + } |
|
3365 | + } |
|
3366 | + |
|
3367 | + |
|
3368 | + /** |
|
3369 | + * @param array $model_fields |
|
3370 | + * @param array $fieldValues |
|
3371 | + * @throws EE_Error |
|
3372 | + * @since $VID:$ |
|
3373 | + */ |
|
3374 | + private function validateFieldValues(array $model_fields, array $fieldValues) |
|
3375 | + { |
|
3376 | + // verify client code has not passed any invalid field names |
|
3377 | + foreach ($fieldValues as $field_name => $field_value) { |
|
3378 | + if (! isset($model_fields[ $field_name ])) { |
|
3379 | + throw new EE_Error( |
|
3380 | + sprintf( |
|
3381 | + esc_html__( |
|
3382 | + 'Invalid field (%s) passed to constructor of %s. Allowed fields are :%s', |
|
3383 | + 'event_espresso' |
|
3384 | + ), |
|
3385 | + $field_name, |
|
3386 | + get_class($this), |
|
3387 | + implode(', ', array_keys($model_fields)) |
|
3388 | + ) |
|
3389 | + ); |
|
3390 | + } |
|
3391 | + } |
|
3392 | + } |
|
3393 | + |
|
3394 | + |
|
3395 | + /** |
|
3396 | + * @param array $model_fields |
|
3397 | + * @param array $fieldValues |
|
3398 | + * @param bool $set_from_db |
|
3399 | + * @throws EE_Error |
|
3400 | + * @throws ReflectionException |
|
3401 | + * @since $VID:$ |
|
3402 | + */ |
|
3403 | + private function setFieldValues(array $model_fields, array $fieldValues, bool $set_from_db) |
|
3404 | + { |
|
3405 | + foreach ($model_fields as $fieldName => $field) { |
|
3406 | + // if db model is instantiating |
|
3407 | + if ($set_from_db) { |
|
3408 | + // client code has indicated these field values are from the database |
|
3409 | + $this->set_from_db( |
|
3410 | + $fieldName, |
|
3411 | + $fieldValues[ $fieldName ] ?? null |
|
3412 | + ); |
|
3413 | + } else { |
|
3414 | + // we're constructing a brand new instance of the model object. |
|
3415 | + // Generally, this means we'll need to do more field validation |
|
3416 | + $this->set( |
|
3417 | + $fieldName, |
|
3418 | + $fieldValues[ $fieldName ] ?? null, |
|
3419 | + true |
|
3420 | + ); |
|
3421 | + } |
|
3422 | + } |
|
3423 | + } |
|
3424 | 3424 | } |
@@ -155,12 +155,12 @@ discard block |
||
155 | 155 | $this->validateFieldValues($model_fields, $field_values); |
156 | 156 | $this->setFieldValues($model_fields, $field_values, $set_from_db); |
157 | 157 | // remember in entity mapper |
158 | - if (! $set_from_db && $this->_model->has_primary_key_field() && $this->ID()) { |
|
158 | + if ( ! $set_from_db && $this->_model->has_primary_key_field() && $this->ID()) { |
|
159 | 159 | $this->_model->add_to_entity_map($this); |
160 | 160 | } |
161 | 161 | // setup all the relations |
162 | 162 | foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
163 | - $this->_model_relations[ $relation_name ] = $relation_obj instanceof EE_Belongs_To_Relation |
|
163 | + $this->_model_relations[$relation_name] = $relation_obj instanceof EE_Belongs_To_Relation |
|
164 | 164 | ? null |
165 | 165 | : []; |
166 | 166 | } |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | */ |
184 | 184 | public function get_model(string $timezone = '') |
185 | 185 | { |
186 | - if (! $this->_model) { |
|
186 | + if ( ! $this->_model) { |
|
187 | 187 | $timezone = $timezone ?? $this->_timezone; |
188 | 188 | $modelName = self::_get_model_classname(get_class($this)); |
189 | 189 | $this->_model = self::_get_model_instance_with_name($modelName, $timezone); |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | } else { |
224 | 224 | $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
225 | 225 | } |
226 | - $this->_fields[ $field_name ] = $field_value; |
|
226 | + $this->_fields[$field_name] = $field_value; |
|
227 | 227 | $this->_clear_cached_property($field_name); |
228 | 228 | } |
229 | 229 | } |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | // then don't do anything |
251 | 251 | if ( |
252 | 252 | ! $use_default |
253 | - && $this->_fields[ $field_name ] === $field_value |
|
253 | + && $this->_fields[$field_name] === $field_value |
|
254 | 254 | && $this->ID() |
255 | 255 | ) { |
256 | 256 | return; |
@@ -266,7 +266,7 @@ discard block |
||
266 | 266 | $holder_of_value = $field_obj->prepare_for_set($field_value); |
267 | 267 | // should the value be null? |
268 | 268 | if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
269 | - $this->_fields[ $field_name ] = $field_obj->get_default_value(); |
|
269 | + $this->_fields[$field_name] = $field_obj->get_default_value(); |
|
270 | 270 | /** |
271 | 271 | * To save having to refactor all the models, if a default value is used for a |
272 | 272 | * EE_Datetime_Field, and that value is not null nor is it a DateTime |
@@ -277,15 +277,15 @@ discard block |
||
277 | 277 | */ |
278 | 278 | if ( |
279 | 279 | $field_obj instanceof EE_Datetime_Field |
280 | - && $this->_fields[ $field_name ] !== null |
|
281 | - && ! $this->_fields[ $field_name ] instanceof DateTime |
|
280 | + && $this->_fields[$field_name] !== null |
|
281 | + && ! $this->_fields[$field_name] instanceof DateTime |
|
282 | 282 | ) { |
283 | - empty($this->_fields[ $field_name ]) |
|
283 | + empty($this->_fields[$field_name]) |
|
284 | 284 | ? $this->set($field_name, time()) |
285 | - : $this->set($field_name, $this->_fields[ $field_name ]); |
|
285 | + : $this->set($field_name, $this->_fields[$field_name]); |
|
286 | 286 | } |
287 | 287 | } else { |
288 | - $this->_fields[ $field_name ] = $holder_of_value; |
|
288 | + $this->_fields[$field_name] = $holder_of_value; |
|
289 | 289 | } |
290 | 290 | // if we're not in the constructor... |
291 | 291 | // now check if what we set was a primary key |
@@ -344,7 +344,7 @@ discard block |
||
344 | 344 | { |
345 | 345 | // now that we know the name of the variable, use a variable variable to get its value and return its |
346 | 346 | if ($this->_model->has_primary_key_field()) { |
347 | - return $this->_fields[ $this->_model->primary_key_name() ]; |
|
347 | + return $this->_fields[$this->_model->primary_key_name()]; |
|
348 | 348 | } |
349 | 349 | return $this->_model->get_index_primary_key_string($this->_fields); |
350 | 350 | } |
@@ -361,7 +361,7 @@ discard block |
||
361 | 361 | { |
362 | 362 | return strpos($model_name, 'EE_') === 0 |
363 | 363 | ? str_replace('EE_', 'EEM_', $model_name) |
364 | - : 'EEM_' . $model_name; |
|
364 | + : 'EEM_'.$model_name; |
|
365 | 365 | } |
366 | 366 | |
367 | 367 | |
@@ -394,7 +394,7 @@ discard block |
||
394 | 394 | */ |
395 | 395 | protected function _clear_cached_property(string $property_name) |
396 | 396 | { |
397 | - unset($this->_cached_properties[ $property_name ]); |
|
397 | + unset($this->_cached_properties[$property_name]); |
|
398 | 398 | } |
399 | 399 | |
400 | 400 | |
@@ -413,7 +413,7 @@ discard block |
||
413 | 413 | protected static function _get_model(string $classname, string $timezone = ''): EEM_Base |
414 | 414 | { |
415 | 415 | // find model for this class |
416 | - if (! $classname) { |
|
416 | + if ( ! $classname) { |
|
417 | 417 | throw new EE_Error( |
418 | 418 | sprintf( |
419 | 419 | esc_html__( |
@@ -471,9 +471,9 @@ discard block |
||
471 | 471 | // verify the field exists |
472 | 472 | $this->_model->field_settings_for($field_name); |
473 | 473 | $cache_type = $pretty ? 'pretty' : 'standard'; |
474 | - $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
475 | - if (isset($this->_cached_properties[ $field_name ][ $cache_type ])) { |
|
476 | - return $this->_cached_properties[ $field_name ][ $cache_type ]; |
|
474 | + $cache_type .= ! empty($extra_cache_ref) ? '_'.$extra_cache_ref : ''; |
|
475 | + if (isset($this->_cached_properties[$field_name][$cache_type])) { |
|
476 | + return $this->_cached_properties[$field_name][$cache_type]; |
|
477 | 477 | } |
478 | 478 | $value = $this->_get_fresh_property($field_name, $pretty, $extra_cache_ref); |
479 | 479 | $this->_set_cached_property($field_name, $value, $cache_type); |
@@ -500,12 +500,12 @@ discard block |
||
500 | 500 | if ($field_obj instanceof EE_Datetime_Field) { |
501 | 501 | $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
502 | 502 | } |
503 | - if (! isset($this->_fields[ $field_name ])) { |
|
504 | - $this->_fields[ $field_name ] = null; |
|
503 | + if ( ! isset($this->_fields[$field_name])) { |
|
504 | + $this->_fields[$field_name] = null; |
|
505 | 505 | } |
506 | 506 | return $pretty |
507 | - ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $field_name ], $extra_cache_ref) |
|
508 | - : $field_obj->prepare_for_get($this->_fields[ $field_name ]); |
|
507 | + ? $field_obj->prepare_for_pretty_echoing($this->_fields[$field_name], $extra_cache_ref) |
|
508 | + : $field_obj->prepare_for_get($this->_fields[$field_name]); |
|
509 | 509 | } |
510 | 510 | |
511 | 511 | |
@@ -526,7 +526,7 @@ discard block |
||
526 | 526 | // first make sure this property exists |
527 | 527 | $this->_model->field_settings_for($field_name); |
528 | 528 | $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
529 | - $this->_cached_properties[ $field_name ][ $cache_type ] = $value; |
|
529 | + $this->_cached_properties[$field_name][$cache_type] = $value; |
|
530 | 530 | } |
531 | 531 | |
532 | 532 | |
@@ -580,9 +580,9 @@ discard block |
||
580 | 580 | $primary_id_ref = self::_get_primary_key_name($classname); |
581 | 581 | if ( |
582 | 582 | array_key_exists($primary_id_ref, $props_n_values) |
583 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
583 | + && ! empty($props_n_values[$primary_id_ref]) |
|
584 | 584 | ) { |
585 | - $id = $props_n_values[ $primary_id_ref ]; |
|
585 | + $id = $props_n_values[$primary_id_ref]; |
|
586 | 586 | return self::_get_model($classname)->get_from_entity_map($id); |
587 | 587 | } |
588 | 588 | return null; |
@@ -604,7 +604,7 @@ discard block |
||
604 | 604 | */ |
605 | 605 | protected static function _get_primary_key_name($classname = null): string |
606 | 606 | { |
607 | - if (! $classname) { |
|
607 | + if ( ! $classname) { |
|
608 | 608 | throw new EE_Error( |
609 | 609 | sprintf( |
610 | 610 | esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
@@ -648,9 +648,9 @@ discard block |
||
648 | 648 | $primary_id_ref = self::_get_primary_key_name($classname); |
649 | 649 | if ( |
650 | 650 | array_key_exists($primary_id_ref, $props_n_values) |
651 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
651 | + && ! empty($props_n_values[$primary_id_ref]) |
|
652 | 652 | ) { |
653 | - $existing = $model->get_one_by_ID($props_n_values[ $primary_id_ref ]); |
|
653 | + $existing = $model->get_one_by_ID($props_n_values[$primary_id_ref]); |
|
654 | 654 | } |
655 | 655 | } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
656 | 656 | // no primary key on this model, but there's still a matching item in the DB |
@@ -748,10 +748,10 @@ discard block |
||
748 | 748 | public function get_original(string $field_name) |
749 | 749 | { |
750 | 750 | if ( |
751 | - isset($this->_props_n_values_provided_in_constructor[ $field_name ]) |
|
751 | + isset($this->_props_n_values_provided_in_constructor[$field_name]) |
|
752 | 752 | && $field_settings = $this->_model->field_settings_for($field_name) |
753 | 753 | ) { |
754 | - return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]); |
|
754 | + return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[$field_name]); |
|
755 | 755 | } |
756 | 756 | return null; |
757 | 757 | } |
@@ -787,8 +787,8 @@ discard block |
||
787 | 787 | */ |
788 | 788 | public function getCustomSelect($alias) |
789 | 789 | { |
790 | - return isset($this->custom_selection_results[ $alias ]) |
|
791 | - ? $this->custom_selection_results[ $alias ] |
|
790 | + return isset($this->custom_selection_results[$alias]) |
|
791 | + ? $this->custom_selection_results[$alias] |
|
792 | 792 | : null; |
793 | 793 | } |
794 | 794 | |
@@ -855,7 +855,7 @@ discard block |
||
855 | 855 | $this->set($column, $value); |
856 | 856 | } |
857 | 857 | // no changes ? then don't do anything |
858 | - if (! $this->_has_changes && $this->ID() && $this->_model->get_primary_key_field()->is_auto_increment()) { |
|
858 | + if ( ! $this->_has_changes && $this->ID() && $this->_model->get_primary_key_field()->is_auto_increment()) { |
|
859 | 859 | return 0; |
860 | 860 | } |
861 | 861 | /** |
@@ -865,7 +865,7 @@ discard block |
||
865 | 865 | * @param EE_Base_Class $model_object the model object about to be saved. |
866 | 866 | */ |
867 | 867 | do_action('AHEE__EE_Base_Class__save__begin', $this); |
868 | - if (! $this->allow_persist()) { |
|
868 | + if ( ! $this->allow_persist()) { |
|
869 | 869 | return 0; |
870 | 870 | } |
871 | 871 | // now get current attribute values |
@@ -880,10 +880,10 @@ discard block |
||
880 | 880 | if ($this->_model->has_primary_key_field()) { |
881 | 881 | if ($this->_model->get_primary_key_field()->is_auto_increment()) { |
882 | 882 | // ok check if it's set, if so: update; if not, insert |
883 | - if (! empty($save_cols_n_values[ $this->_model->primary_key_name() ])) { |
|
883 | + if ( ! empty($save_cols_n_values[$this->_model->primary_key_name()])) { |
|
884 | 884 | $results = $this->_model->update_by_ID($save_cols_n_values, $this->ID()); |
885 | 885 | } else { |
886 | - unset($save_cols_n_values[ $this->_model->primary_key_name() ]); |
|
886 | + unset($save_cols_n_values[$this->_model->primary_key_name()]); |
|
887 | 887 | $results = $this->_model->insert($save_cols_n_values); |
888 | 888 | if ($results) { |
889 | 889 | // if successful, set the primary key |
@@ -893,7 +893,7 @@ discard block |
||
893 | 893 | // will get added to the mapper before we can add this one! |
894 | 894 | // but if we just avoid using the SET method, all that headache can be avoided |
895 | 895 | $pk_field_name = $this->_model->primary_key_name(); |
896 | - $this->_fields[ $pk_field_name ] = $results; |
|
896 | + $this->_fields[$pk_field_name] = $results; |
|
897 | 897 | $this->_clear_cached_property($pk_field_name); |
898 | 898 | $this->_model->add_to_entity_map($this); |
899 | 899 | $this->_update_cached_related_model_objs_fks(); |
@@ -911,8 +911,8 @@ discard block |
||
911 | 911 | 'event_espresso' |
912 | 912 | ), |
913 | 913 | get_class($this), |
914 | - get_class($this->_model) . '::instance()->add_to_entity_map()', |
|
915 | - get_class($this->_model) . '::instance()->get_one_by_ID()', |
|
914 | + get_class($this->_model).'::instance()->add_to_entity_map()', |
|
915 | + get_class($this->_model).'::instance()->get_one_by_ID()', |
|
916 | 916 | '<br />' |
917 | 917 | ) |
918 | 918 | ); |
@@ -937,7 +937,7 @@ discard block |
||
937 | 937 | $save_cols_n_values, |
938 | 938 | $this->_model->get_combined_primary_key_fields() |
939 | 939 | ); |
940 | - $results = $this->_model->update( |
|
940 | + $results = $this->_model->update( |
|
941 | 941 | $save_cols_n_values, |
942 | 942 | $combined_pk_fields_n_values |
943 | 943 | ); |
@@ -990,7 +990,7 @@ discard block |
||
990 | 990 | $query_params[0]['EXM_value'] = $meta_value; |
991 | 991 | } |
992 | 992 | $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
993 | - if (! $existing_rows_like_that) { |
|
993 | + if ( ! $existing_rows_like_that) { |
|
994 | 994 | return $this->add_extra_meta($meta_key, $meta_value); |
995 | 995 | } |
996 | 996 | foreach ($existing_rows_like_that as $existing_row) { |
@@ -1121,7 +1121,7 @@ discard block |
||
1121 | 1121 | */ |
1122 | 1122 | public function get_all_from_cache($relationName) |
1123 | 1123 | { |
1124 | - $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : []; |
|
1124 | + $objects = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] : []; |
|
1125 | 1125 | // if the result is not an array, but exists, make it an array |
1126 | 1126 | $objects = is_array($objects) ? $objects : [$objects]; |
1127 | 1127 | // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
@@ -1216,7 +1216,7 @@ discard block |
||
1216 | 1216 | $values = []; |
1217 | 1217 | foreach ($results as $result) { |
1218 | 1218 | if ($result instanceof EE_Extra_Meta) { |
1219 | - $values[ $result->ID() ] = $result->value(); |
|
1219 | + $values[$result->ID()] = $result->value(); |
|
1220 | 1220 | } |
1221 | 1221 | } |
1222 | 1222 | return $values; |
@@ -1249,7 +1249,7 @@ discard block |
||
1249 | 1249 | public function get_first_related($relationName, $query_params = []) |
1250 | 1250 | { |
1251 | 1251 | $model_relation = $this->_model->related_settings_for($relationName); |
1252 | - if (! $this->ID()) { |
|
1252 | + if ( ! $this->ID()) { |
|
1253 | 1253 | // this doesn't exist in the DB, |
1254 | 1254 | // but maybe the relation type is "belongs to" and the related object might |
1255 | 1255 | if ($model_relation instanceof EE_Belongs_To_Relation) { |
@@ -1274,7 +1274,7 @@ discard block |
||
1274 | 1274 | // also make sure we're not caching the result of get_first_related |
1275 | 1275 | // on a relation which should have an array of objects (because the cache might have an array of objects) |
1276 | 1276 | if ($query_params || ! $model_relation instanceof EE_Belongs_To_Relation) { |
1277 | - $related_model_object =$this->_model->get_first_related( |
|
1277 | + $related_model_object = $this->_model->get_first_related( |
|
1278 | 1278 | $this, |
1279 | 1279 | $relationName, |
1280 | 1280 | $query_params |
@@ -1319,7 +1319,7 @@ discard block |
||
1319 | 1319 | */ |
1320 | 1320 | public function get_many_related($relationName, $query_params = []) |
1321 | 1321 | { |
1322 | - if (! $this->ID()) { |
|
1322 | + if ( ! $this->ID()) { |
|
1323 | 1323 | // this doesn't exist in the DB, so just get the related things from the cache |
1324 | 1324 | return $this->get_all_from_cache($relationName); |
1325 | 1325 | } |
@@ -1364,8 +1364,8 @@ discard block |
||
1364 | 1364 | */ |
1365 | 1365 | public function get_one_from_cache($relationName) |
1366 | 1366 | { |
1367 | - $cached_array_or_object = isset($this->_model_relations[ $relationName ]) |
|
1368 | - ? $this->_model_relations[ $relationName ] |
|
1367 | + $cached_array_or_object = isset($this->_model_relations[$relationName]) |
|
1368 | + ? $this->_model_relations[$relationName] |
|
1369 | 1369 | : null; |
1370 | 1370 | if (is_array($cached_array_or_object)) { |
1371 | 1371 | $cached_array_or_object = array_shift($cached_array_or_object); |
@@ -1396,11 +1396,11 @@ discard block |
||
1396 | 1396 | public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
1397 | 1397 | { |
1398 | 1398 | // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
1399 | - if (! $object_to_cache instanceof EE_Base_Class) { |
|
1399 | + if ( ! $object_to_cache instanceof EE_Base_Class) { |
|
1400 | 1400 | return false; |
1401 | 1401 | } |
1402 | 1402 | // also get "how" the object is related, or throw an error |
1403 | - if (! $relationship_to_model = $this->_model->related_settings_for($relationName)) { |
|
1403 | + if ( ! $relationship_to_model = $this->_model->related_settings_for($relationName)) { |
|
1404 | 1404 | throw new EE_Error( |
1405 | 1405 | sprintf( |
1406 | 1406 | esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
@@ -1414,38 +1414,38 @@ discard block |
||
1414 | 1414 | // if it's a "belongs to" relationship, then there's only one related model object |
1415 | 1415 | // eg, if this is a registration, there's only 1 attendee for it |
1416 | 1416 | // so for these model objects just set it to be cached |
1417 | - $this->_model_relations[ $relationName ] = $object_to_cache; |
|
1417 | + $this->_model_relations[$relationName] = $object_to_cache; |
|
1418 | 1418 | return true; |
1419 | 1419 | } |
1420 | 1420 | // otherwise, this is the "many" side of a one to many relationship, |
1421 | 1421 | // so we'll add the object to the array of related objects for that type. |
1422 | 1422 | // eg: if this is an event, there are many registrations for that event, |
1423 | 1423 | // so we cache the registrations in an array |
1424 | - if (! is_array($this->_model_relations[ $relationName ])) { |
|
1424 | + if ( ! is_array($this->_model_relations[$relationName])) { |
|
1425 | 1425 | // if for some reason, the cached item is a model object, |
1426 | 1426 | // then stick that in the array, otherwise start with an empty array |
1427 | - $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ] instanceof EE_Base_Class |
|
1428 | - ? [$this->_model_relations[ $relationName ]] |
|
1427 | + $this->_model_relations[$relationName] = $this->_model_relations[$relationName] instanceof EE_Base_Class |
|
1428 | + ? [$this->_model_relations[$relationName]] |
|
1429 | 1429 | : []; |
1430 | 1430 | } |
1431 | 1431 | // first check for a cache_id which is normally empty |
1432 | - if (! empty($cache_id)) { |
|
1432 | + if ( ! empty($cache_id)) { |
|
1433 | 1433 | // if the cache_id exists, then it means we are purposely trying to cache this |
1434 | 1434 | // with a known key that can then be used to retrieve the object later on |
1435 | - $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache; |
|
1435 | + $this->_model_relations[$relationName][$cache_id] = $object_to_cache; |
|
1436 | 1436 | return $cache_id; |
1437 | 1437 | } |
1438 | 1438 | if ($object_to_cache->ID()) { |
1439 | 1439 | // OR the cached object originally came from the db, so let's just use it's PK for an ID |
1440 | - $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache; |
|
1440 | + $this->_model_relations[$relationName][$object_to_cache->ID()] = $object_to_cache; |
|
1441 | 1441 | return $object_to_cache->ID(); |
1442 | 1442 | } |
1443 | 1443 | // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
1444 | - $this->_model_relations[ $relationName ][] = $object_to_cache; |
|
1444 | + $this->_model_relations[$relationName][] = $object_to_cache; |
|
1445 | 1445 | // move the internal pointer to the end of the array |
1446 | - end($this->_model_relations[ $relationName ]); |
|
1446 | + end($this->_model_relations[$relationName]); |
|
1447 | 1447 | // and grab the key so that we can return it |
1448 | - return key($this->_model_relations[ $relationName ]); |
|
1448 | + return key($this->_model_relations[$relationName]); |
|
1449 | 1449 | } |
1450 | 1450 | |
1451 | 1451 | |
@@ -1477,7 +1477,7 @@ discard block |
||
1477 | 1477 | { |
1478 | 1478 | static $set_in_progress = false; |
1479 | 1479 | // don't update the timezone if it's already set ? |
1480 | - if (($only_if_not_set && $this->_timezone !== '') ) { |
|
1480 | + if (($only_if_not_set && $this->_timezone !== '')) { |
|
1481 | 1481 | return; |
1482 | 1482 | } |
1483 | 1483 | $valid_timezone = ! empty($timezone) && $this->_timezone && $timezone !== $this->_timezone |
@@ -1507,8 +1507,8 @@ discard block |
||
1507 | 1507 | foreach ($model_fields as $field_name => $field_obj) { |
1508 | 1508 | if ($field_obj instanceof EE_Datetime_Field) { |
1509 | 1509 | $field_obj->set_timezone($this->_timezone); |
1510 | - if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) { |
|
1511 | - EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], $TZ); |
|
1510 | + if (isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime) { |
|
1511 | + EEH_DTT_Helper::setTimezone($this->_fields[$field_name], $TZ); |
|
1512 | 1512 | } |
1513 | 1513 | } |
1514 | 1514 | } |
@@ -1543,7 +1543,7 @@ discard block |
||
1543 | 1543 | */ |
1544 | 1544 | public function get_format($full = true) |
1545 | 1545 | { |
1546 | - return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : [$this->_dt_frmt, $this->_tm_frmt]; |
|
1546 | + return $full ? $this->_dt_frmt.' '.$this->_tm_frmt : [$this->_dt_frmt, $this->_tm_frmt]; |
|
1547 | 1547 | } |
1548 | 1548 | |
1549 | 1549 | |
@@ -1567,8 +1567,8 @@ discard block |
||
1567 | 1567 | $current_cache_id = '' |
1568 | 1568 | ) { |
1569 | 1569 | // verify that incoming object is of the correct type |
1570 | - $obj_class = 'EE_' . $relationName; |
|
1571 | - if (! $newly_saved_object instanceof $obj_class) { |
|
1570 | + $obj_class = 'EE_'.$relationName; |
|
1571 | + if ( ! $newly_saved_object instanceof $obj_class) { |
|
1572 | 1572 | return false; |
1573 | 1573 | } |
1574 | 1574 | $this->updateTimezoneOnRelated($newly_saved_object); |
@@ -1578,18 +1578,18 @@ discard block |
||
1578 | 1578 | // if this is a 1:1 relationship |
1579 | 1579 | if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
1580 | 1580 | // then just replace the cached object with the newly saved object |
1581 | - $this->_model_relations[ $relationName ] = $newly_saved_object; |
|
1581 | + $this->_model_relations[$relationName] = $newly_saved_object; |
|
1582 | 1582 | return true; |
1583 | 1583 | } |
1584 | 1584 | // or if it's some kind of sordid feral polyamorous relationship... |
1585 | 1585 | if ( |
1586 | - is_array($this->_model_relations[ $relationName ]) |
|
1587 | - && isset($this->_model_relations[ $relationName ][ $current_cache_id ]) |
|
1586 | + is_array($this->_model_relations[$relationName]) |
|
1587 | + && isset($this->_model_relations[$relationName][$current_cache_id]) |
|
1588 | 1588 | ) { |
1589 | 1589 | // then remove the current cached item |
1590 | - unset($this->_model_relations[ $relationName ][ $current_cache_id ]); |
|
1590 | + unset($this->_model_relations[$relationName][$current_cache_id]); |
|
1591 | 1591 | // and cache the newly saved object using it's new ID |
1592 | - $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object; |
|
1592 | + $this->_model_relations[$relationName][$newly_saved_object->ID()] = $newly_saved_object; |
|
1593 | 1593 | return true; |
1594 | 1594 | } |
1595 | 1595 | return false; |
@@ -1731,7 +1731,7 @@ discard block |
||
1731 | 1731 | public function get_DateTime_object(string $field_name) |
1732 | 1732 | { |
1733 | 1733 | $field_settings = $this->_model->field_settings_for($field_name); |
1734 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
1734 | + if ( ! $field_settings instanceof EE_Datetime_Field) { |
|
1735 | 1735 | EE_Error::add_error( |
1736 | 1736 | sprintf( |
1737 | 1737 | esc_html__( |
@@ -1746,8 +1746,8 @@ discard block |
||
1746 | 1746 | ); |
1747 | 1747 | return false; |
1748 | 1748 | } |
1749 | - return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime |
|
1750 | - ? clone $this->_fields[ $field_name ] |
|
1749 | + return isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime |
|
1750 | + ? clone $this->_fields[$field_name] |
|
1751 | 1751 | : null; |
1752 | 1752 | } |
1753 | 1753 | |
@@ -1993,7 +1993,7 @@ discard block |
||
1993 | 1993 | */ |
1994 | 1994 | public function get_i18n_datetime(string $field_name, $format = '') |
1995 | 1995 | { |
1996 | - $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
1996 | + $format = empty($format) ? $this->_dt_frmt.' '.$this->_tm_frmt : $format; |
|
1997 | 1997 | return date_i18n( |
1998 | 1998 | $format, |
1999 | 1999 | EEH_DTT_Helper::get_timestamp_with_offset( |
@@ -2017,9 +2017,9 @@ discard block |
||
2017 | 2017 | public function get_raw(string $field_name) |
2018 | 2018 | { |
2019 | 2019 | $field_settings = $this->_model->field_settings_for($field_name); |
2020 | - return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime |
|
2021 | - ? $this->_fields[ $field_name ]->format('U') |
|
2022 | - : $this->_fields[ $field_name ]; |
|
2020 | + return $field_settings instanceof EE_Datetime_Field && $this->_fields[$field_name] instanceof DateTime |
|
2021 | + ? $this->_fields[$field_name]->format('U') |
|
2022 | + : $this->_fields[$field_name]; |
|
2023 | 2023 | } |
2024 | 2024 | |
2025 | 2025 | |
@@ -2059,7 +2059,7 @@ discard block |
||
2059 | 2059 | $this->set_timezone($timezone); |
2060 | 2060 | $fn = (array) $field_name; |
2061 | 2061 | $args = array_merge($fn, (array) $args); |
2062 | - if (! method_exists($this, $callback)) { |
|
2062 | + if ( ! method_exists($this, $callback)) { |
|
2063 | 2063 | throw new EE_Error( |
2064 | 2064 | sprintf( |
2065 | 2065 | esc_html__( |
@@ -2071,7 +2071,7 @@ discard block |
||
2071 | 2071 | ); |
2072 | 2072 | } |
2073 | 2073 | $args = (array) $args; |
2074 | - $return = $prepend . call_user_func_array([$this, $callback], $args) . $append; |
|
2074 | + $return = $prepend.call_user_func_array([$this, $callback], $args).$append; |
|
2075 | 2075 | $this->set_timezone($original_timezone); |
2076 | 2076 | return $return; |
2077 | 2077 | } |
@@ -2184,8 +2184,8 @@ discard block |
||
2184 | 2184 | public function refresh_cache_of_related_objects() |
2185 | 2185 | { |
2186 | 2186 | foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
2187 | - if (! empty($this->_model_relations[ $relation_name ])) { |
|
2188 | - $related_objects = $this->_model_relations[ $relation_name ]; |
|
2187 | + if ( ! empty($this->_model_relations[$relation_name])) { |
|
2188 | + $related_objects = $this->_model_relations[$relation_name]; |
|
2189 | 2189 | if ($relation_obj instanceof EE_Belongs_To_Relation) { |
2190 | 2190 | // this relation only stores a single model object, not an array |
2191 | 2191 | // but let's make it consistent |
@@ -2230,7 +2230,7 @@ discard block |
||
2230 | 2230 | { |
2231 | 2231 | $relationship_to_model = $this->_model->related_settings_for($relationName); |
2232 | 2232 | $index_in_cache = ''; |
2233 | - if (! $relationship_to_model) { |
|
2233 | + if ( ! $relationship_to_model) { |
|
2234 | 2234 | throw new EE_Error( |
2235 | 2235 | sprintf( |
2236 | 2236 | esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'), |
@@ -2241,10 +2241,10 @@ discard block |
||
2241 | 2241 | } |
2242 | 2242 | if ($clear_all) { |
2243 | 2243 | $obj_removed = true; |
2244 | - $this->_model_relations[ $relationName ] = null; |
|
2244 | + $this->_model_relations[$relationName] = null; |
|
2245 | 2245 | } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
2246 | - $obj_removed = $this->_model_relations[ $relationName ]; |
|
2247 | - $this->_model_relations[ $relationName ] = null; |
|
2246 | + $obj_removed = $this->_model_relations[$relationName]; |
|
2247 | + $this->_model_relations[$relationName] = null; |
|
2248 | 2248 | } else { |
2249 | 2249 | if ( |
2250 | 2250 | $object_to_remove_or_index_into_array instanceof EE_Base_Class |
@@ -2252,12 +2252,12 @@ discard block |
||
2252 | 2252 | ) { |
2253 | 2253 | $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
2254 | 2254 | if ( |
2255 | - is_array($this->_model_relations[ $relationName ]) |
|
2256 | - && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ]) |
|
2255 | + is_array($this->_model_relations[$relationName]) |
|
2256 | + && ! isset($this->_model_relations[$relationName][$index_in_cache]) |
|
2257 | 2257 | ) { |
2258 | 2258 | $index_found_at = null; |
2259 | 2259 | // find this object in the array even though it has a different key |
2260 | - foreach ($this->_model_relations[ $relationName ] as $index => $obj) { |
|
2260 | + foreach ($this->_model_relations[$relationName] as $index => $obj) { |
|
2261 | 2261 | /** @noinspection TypeUnsafeComparisonInspection */ |
2262 | 2262 | if ( |
2263 | 2263 | $obj instanceof EE_Base_Class |
@@ -2291,9 +2291,9 @@ discard block |
||
2291 | 2291 | } |
2292 | 2292 | // supposedly we've found it. But it could just be that the client code |
2293 | 2293 | // provided a bad index/object |
2294 | - if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) { |
|
2295 | - $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ]; |
|
2296 | - unset($this->_model_relations[ $relationName ][ $index_in_cache ]); |
|
2294 | + if (isset($this->_model_relations[$relationName][$index_in_cache])) { |
|
2295 | + $obj_removed = $this->_model_relations[$relationName][$index_in_cache]; |
|
2296 | + unset($this->_model_relations[$relationName][$index_in_cache]); |
|
2297 | 2297 | } else { |
2298 | 2298 | // that thing was never cached anyways. |
2299 | 2299 | $obj_removed = null; |
@@ -2321,27 +2321,27 @@ discard block |
||
2321 | 2321 | public function save_new_cached_related_model_objs() |
2322 | 2322 | { |
2323 | 2323 | // make sure this has been saved |
2324 | - if (! $this->ID()) { |
|
2324 | + if ( ! $this->ID()) { |
|
2325 | 2325 | $id = $this->save(); |
2326 | 2326 | } else { |
2327 | 2327 | $id = $this->ID(); |
2328 | 2328 | } |
2329 | 2329 | // now save all the NEW cached model objects (ie they don't exist in the DB) |
2330 | 2330 | foreach ($this->_model->relation_settings() as $relationName => $relationObj) { |
2331 | - if ($this->_model_relations[ $relationName ]) { |
|
2331 | + if ($this->_model_relations[$relationName]) { |
|
2332 | 2332 | // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
2333 | 2333 | // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
2334 | 2334 | /* @var $related_model_obj EE_Base_Class */ |
2335 | 2335 | if ($relationObj instanceof EE_Belongs_To_Relation) { |
2336 | 2336 | // add a relation to that relation type (which saves the appropriate thing in the process) |
2337 | 2337 | // but ONLY if it DOES NOT exist in the DB |
2338 | - $related_model_obj = $this->_model_relations[ $relationName ]; |
|
2338 | + $related_model_obj = $this->_model_relations[$relationName]; |
|
2339 | 2339 | // if( ! $related_model_obj->ID()){ |
2340 | 2340 | $this->_add_relation_to($related_model_obj, $relationName); |
2341 | 2341 | $related_model_obj->save_new_cached_related_model_objs(); |
2342 | 2342 | // } |
2343 | 2343 | } else { |
2344 | - foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
2344 | + foreach ($this->_model_relations[$relationName] as $related_model_obj) { |
|
2345 | 2345 | // add a relation to that relation type (which saves the appropriate thing in the process) |
2346 | 2346 | // but ONLY if it DOES NOT exist in the DB |
2347 | 2347 | // if( ! $related_model_obj->ID()){ |
@@ -2402,7 +2402,7 @@ discard block |
||
2402 | 2402 | } |
2403 | 2403 | } else { |
2404 | 2404 | // this thing doesn't exist in the DB, so just cache it |
2405 | - if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
2405 | + if ( ! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
2406 | 2406 | throw new EE_Error( |
2407 | 2407 | sprintf( |
2408 | 2408 | esc_html__( |
@@ -2669,7 +2669,7 @@ discard block |
||
2669 | 2669 | */ |
2670 | 2670 | public function is_set(string $field_name) |
2671 | 2671 | { |
2672 | - return isset($this->_fields[ $field_name ]); |
|
2672 | + return isset($this->_fields[$field_name]); |
|
2673 | 2673 | } |
2674 | 2674 | |
2675 | 2675 | |
@@ -2704,7 +2704,7 @@ discard block |
||
2704 | 2704 | { |
2705 | 2705 | $className = get_class($this); |
2706 | 2706 | $tagName = "FHEE__{$className}__{$methodName}"; |
2707 | - if (! has_filter($tagName)) { |
|
2707 | + if ( ! has_filter($tagName)) { |
|
2708 | 2708 | throw new EE_Error( |
2709 | 2709 | sprintf( |
2710 | 2710 | esc_html__( |
@@ -2778,17 +2778,17 @@ discard block |
||
2778 | 2778 | ); |
2779 | 2779 | foreach ($extra_meta_objs as $extra_meta_obj) { |
2780 | 2780 | if ($extra_meta_obj instanceof EE_Extra_Meta) { |
2781 | - $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
2781 | + $return_array[$extra_meta_obj->key()] = $extra_meta_obj->value(); |
|
2782 | 2782 | } |
2783 | 2783 | } |
2784 | 2784 | } else { |
2785 | 2785 | $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
2786 | 2786 | foreach ($extra_meta_objs as $extra_meta_obj) { |
2787 | 2787 | if ($extra_meta_obj instanceof EE_Extra_Meta) { |
2788 | - if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
2789 | - $return_array[ $extra_meta_obj->key() ] = []; |
|
2788 | + if ( ! isset($return_array[$extra_meta_obj->key()])) { |
|
2789 | + $return_array[$extra_meta_obj->key()] = []; |
|
2790 | 2790 | } |
2791 | - $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
2791 | + $return_array[$extra_meta_obj->key()][$extra_meta_obj->ID()] = $extra_meta_obj->value(); |
|
2792 | 2792 | } |
2793 | 2793 | } |
2794 | 2794 | } |
@@ -2824,8 +2824,8 @@ discard block |
||
2824 | 2824 | 'event_espresso' |
2825 | 2825 | ), |
2826 | 2826 | $this->ID(), |
2827 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
2828 | - get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
2827 | + get_class($this->get_model()).'::instance()->add_to_entity_map()', |
|
2828 | + get_class($this->get_model()).'::instance()->refresh_entity_map()' |
|
2829 | 2829 | ) |
2830 | 2830 | ); |
2831 | 2831 | } |
@@ -2914,7 +2914,7 @@ discard block |
||
2914 | 2914 | { |
2915 | 2915 | // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB |
2916 | 2916 | // if it wasn't even there to start off. |
2917 | - if (! $this->ID()) { |
|
2917 | + if ( ! $this->ID()) { |
|
2918 | 2918 | $this->save(); |
2919 | 2919 | } |
2920 | 2920 | global $wpdb; |
@@ -2954,7 +2954,7 @@ discard block |
||
2954 | 2954 | $table_pk_field_name = $table_obj->get_pk_column(); |
2955 | 2955 | } |
2956 | 2956 | |
2957 | - $query = |
|
2957 | + $query = |
|
2958 | 2958 | "UPDATE `{$table_name}` |
2959 | 2959 | SET " |
2960 | 2960 | . $new_value_sql |
@@ -3002,7 +3002,7 @@ discard block |
||
3002 | 3002 | $properties = []; |
3003 | 3003 | // remove prepended underscore |
3004 | 3004 | foreach ($fields as $field_name => $settings) { |
3005 | - $properties[ $field_name ] = $this->get($field_name); |
|
3005 | + $properties[$field_name] = $this->get($field_name); |
|
3006 | 3006 | } |
3007 | 3007 | return $properties; |
3008 | 3008 | } |
@@ -3138,7 +3138,7 @@ discard block |
||
3138 | 3138 | { |
3139 | 3139 | foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
3140 | 3140 | if ($relation_obj instanceof EE_Belongs_To_Relation) { |
3141 | - $classname = 'EE_' . $this->_model->get_this_model_name(); |
|
3141 | + $classname = 'EE_'.$this->_model->get_this_model_name(); |
|
3142 | 3142 | if ( |
3143 | 3143 | $this->get_one_from_cache($relation_name) instanceof $classname |
3144 | 3144 | && $this->get_one_from_cache($relation_name)->ID() |
@@ -3183,7 +3183,7 @@ discard block |
||
3183 | 3183 | // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
3184 | 3184 | foreach ($this->_fields as $field => $value) { |
3185 | 3185 | if ($value instanceof DateTime) { |
3186 | - $this->_fields[ $field ] = clone $value; |
|
3186 | + $this->_fields[$field] = clone $value; |
|
3187 | 3187 | } |
3188 | 3188 | } |
3189 | 3189 | } |
@@ -3250,21 +3250,21 @@ discard block |
||
3250 | 3250 | $what = in_array($what, ['T', 'D', 'B']) ? $what : 'T'; |
3251 | 3251 | switch ($what) { |
3252 | 3252 | case 'T': |
3253 | - $this->_fields[ $field_name ] = $field->prepare_for_set_with_new_time( |
|
3253 | + $this->_fields[$field_name] = $field->prepare_for_set_with_new_time( |
|
3254 | 3254 | $datetime_value, |
3255 | - $this->_fields[ $field_name ] |
|
3255 | + $this->_fields[$field_name] |
|
3256 | 3256 | ); |
3257 | 3257 | $this->_has_changes = true; |
3258 | 3258 | break; |
3259 | 3259 | case 'D': |
3260 | - $this->_fields[ $field_name ] = $field->prepare_for_set_with_new_date( |
|
3260 | + $this->_fields[$field_name] = $field->prepare_for_set_with_new_date( |
|
3261 | 3261 | $datetime_value, |
3262 | - $this->_fields[ $field_name ] |
|
3262 | + $this->_fields[$field_name] |
|
3263 | 3263 | ); |
3264 | 3264 | $this->_has_changes = true; |
3265 | 3265 | break; |
3266 | 3266 | case 'B': |
3267 | - $this->_fields[ $field_name ] = $field->prepare_for_set($datetime_value); |
|
3267 | + $this->_fields[$field_name] = $field->prepare_for_set($datetime_value); |
|
3268 | 3268 | $this->_has_changes = true; |
3269 | 3269 | break; |
3270 | 3270 | } |
@@ -3333,7 +3333,7 @@ discard block |
||
3333 | 3333 | { |
3334 | 3334 | foreach ((array) $properties as $property_name) { |
3335 | 3335 | // first make sure this property exists |
3336 | - if (! $this->_fields[ $property_name ]) { |
|
3336 | + if ( ! $this->_fields[$property_name]) { |
|
3337 | 3337 | throw new EE_Error( |
3338 | 3338 | sprintf( |
3339 | 3339 | esc_html__( |
@@ -3355,7 +3355,7 @@ discard block |
||
3355 | 3355 | */ |
3356 | 3356 | private function setDateAndTimeFormats(array $date_formats) |
3357 | 3357 | { |
3358 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
3358 | + if ( ! empty($date_formats) && is_array($date_formats)) { |
|
3359 | 3359 | [$this->_dt_frmt, $this->_tm_frmt] = $date_formats; |
3360 | 3360 | } else { |
3361 | 3361 | // set default formats for date and time |
@@ -3375,7 +3375,7 @@ discard block |
||
3375 | 3375 | { |
3376 | 3376 | // verify client code has not passed any invalid field names |
3377 | 3377 | foreach ($fieldValues as $field_name => $field_value) { |
3378 | - if (! isset($model_fields[ $field_name ])) { |
|
3378 | + if ( ! isset($model_fields[$field_name])) { |
|
3379 | 3379 | throw new EE_Error( |
3380 | 3380 | sprintf( |
3381 | 3381 | esc_html__( |
@@ -3408,14 +3408,14 @@ discard block |
||
3408 | 3408 | // client code has indicated these field values are from the database |
3409 | 3409 | $this->set_from_db( |
3410 | 3410 | $fieldName, |
3411 | - $fieldValues[ $fieldName ] ?? null |
|
3411 | + $fieldValues[$fieldName] ?? null |
|
3412 | 3412 | ); |
3413 | 3413 | } else { |
3414 | 3414 | // we're constructing a brand new instance of the model object. |
3415 | 3415 | // Generally, this means we'll need to do more field validation |
3416 | 3416 | $this->set( |
3417 | 3417 | $fieldName, |
3418 | - $fieldValues[ $fieldName ] ?? null, |
|
3418 | + $fieldValues[$fieldName] ?? null, |
|
3419 | 3419 | true |
3420 | 3420 | ); |
3421 | 3421 | } |
@@ -10,204 +10,204 @@ |
||
10 | 10 | class EEM_Country extends EEM_Base |
11 | 11 | { |
12 | 12 | |
13 | - // private instance of the Attendee object |
|
14 | - protected static $_instance; |
|
15 | - |
|
16 | - // array of all countries |
|
17 | - private static $_all_countries = false; |
|
18 | - |
|
19 | - // array of all active countries |
|
20 | - private static $_active_countries = false; |
|
21 | - |
|
22 | - |
|
23 | - /** |
|
24 | - * Resets the country |
|
25 | - * |
|
26 | - * @param string $timezone |
|
27 | - * @return EEM_Country |
|
28 | - * @throws EE_Error |
|
29 | - * @throws ReflectionException |
|
30 | - */ |
|
31 | - public static function reset(string $timezone = ''): EEM_Country |
|
32 | - { |
|
33 | - self::$_active_countries = null; |
|
34 | - self::$_all_countries = null; |
|
35 | - return parent::reset($timezone); |
|
36 | - } |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * EEM_Country constructor. |
|
41 | - * |
|
42 | - * @param string $timezone |
|
43 | - * @throws EE_Error |
|
44 | - */ |
|
45 | - protected function __construct(string $timezone = '') |
|
46 | - { |
|
47 | - $this->singular_item = esc_html__('Country', 'event_espresso'); |
|
48 | - $this->plural_item = esc_html__('Countries', 'event_espresso'); |
|
49 | - |
|
50 | - $this->_tables = [ |
|
51 | - 'Country' => new EE_Primary_Table('esp_country', 'CNT_ISO'), |
|
52 | - ]; |
|
53 | - |
|
54 | - $this->_fields = [ |
|
55 | - 'Country' => [ |
|
56 | - 'CNT_active' => new EE_Boolean_Field( |
|
57 | - 'CNT_active', |
|
58 | - esc_html__( |
|
59 | - 'Country Appears in Dropdown Select Lists', |
|
60 | - 'event_espresso' |
|
61 | - ), |
|
62 | - false, |
|
63 | - true |
|
64 | - ), |
|
65 | - 'CNT_ISO' => new EE_Primary_Key_String_Field( |
|
66 | - 'CNT_ISO', |
|
67 | - esc_html__('Country ISO Code', 'event_espresso') |
|
68 | - ), |
|
69 | - 'CNT_ISO3' => new EE_All_Caps_Text_Field( |
|
70 | - 'CNT_ISO3', |
|
71 | - esc_html__('Country ISO3 Code', 'event_espresso'), |
|
72 | - false, |
|
73 | - '' |
|
74 | - ), |
|
75 | - 'RGN_ID' => new EE_Integer_Field( |
|
76 | - 'RGN_ID', |
|
77 | - esc_html__('Region ID', 'event_espresso'), |
|
78 | - false, |
|
79 | - 0 |
|
80 | - ), |
|
81 | - // should be a foreign key, but no region table exists yet |
|
82 | - 'CNT_name' => new EE_Plain_Text_Field( |
|
83 | - 'CNT_name', |
|
84 | - esc_html__('Country Name', 'event_espresso'), |
|
85 | - false, |
|
86 | - '' |
|
87 | - ), |
|
88 | - 'CNT_cur_code' => new EE_All_Caps_Text_Field( |
|
89 | - 'CNT_cur_code', |
|
90 | - esc_html__('Country Currency Code', 'event_espresso'), |
|
91 | - false |
|
92 | - ), |
|
93 | - 'CNT_cur_single' => new EE_Plain_Text_Field( |
|
94 | - 'CNT_cur_single', |
|
95 | - esc_html__('Currency Name Singular', 'event_espresso'), |
|
96 | - false |
|
97 | - ), |
|
98 | - 'CNT_cur_plural' => new EE_Plain_Text_Field( |
|
99 | - 'CNT_cur_plural', |
|
100 | - esc_html__('Currency Name Plural', 'event_espresso'), |
|
101 | - false |
|
102 | - ), |
|
103 | - 'CNT_cur_sign' => new EE_Plain_Text_Field( |
|
104 | - 'CNT_cur_sign', |
|
105 | - esc_html__('Currency Sign', 'event_espresso'), |
|
106 | - false |
|
107 | - ), |
|
108 | - 'CNT_cur_sign_b4' => new EE_Boolean_Field( |
|
109 | - 'CNT_cur_sign_b4', |
|
110 | - esc_html__('Currency Sign Before Number', 'event_espresso'), |
|
111 | - false, |
|
112 | - true |
|
113 | - ), |
|
114 | - 'CNT_cur_dec_plc' => new EE_Integer_Field( |
|
115 | - 'CNT_cur_dec_plc', |
|
116 | - esc_html__('Currency Decimal Places', 'event_espresso'), |
|
117 | - false, |
|
118 | - 2 |
|
119 | - ), |
|
120 | - 'CNT_cur_dec_mrk' => new EE_Plain_Text_Field( |
|
121 | - 'CNT_cur_dec_mrk', |
|
122 | - esc_html__('Currency Decimal Mark', 'event_espresso'), |
|
123 | - false, |
|
124 | - '.' |
|
125 | - ), |
|
126 | - 'CNT_cur_thsnds' => new EE_Plain_Text_Field( |
|
127 | - 'CNT_cur_thsnds', |
|
128 | - esc_html__('Currency Thousands Separator', 'event_espresso'), |
|
129 | - false, |
|
130 | - ',' |
|
131 | - ), |
|
132 | - 'CNT_tel_code' => new EE_Plain_Text_Field( |
|
133 | - 'CNT_tel_code', |
|
134 | - esc_html__('Country Telephone Code', 'event_espresso'), |
|
135 | - false, |
|
136 | - '' |
|
137 | - ), |
|
138 | - 'CNT_is_EU' => new EE_Boolean_Field( |
|
139 | - 'CNT_is_EU', |
|
140 | - esc_html__('Country is Member of EU', 'event_espresso'), |
|
141 | - false, |
|
142 | - false |
|
143 | - ), |
|
144 | - ], |
|
145 | - ]; |
|
146 | - $this->_model_relations = [ |
|
147 | - 'Attendee' => new EE_Has_Many_Relation(), |
|
148 | - 'State' => new EE_Has_Many_Relation(), |
|
149 | - 'Venue' => new EE_Has_Many_Relation(), |
|
150 | - ]; |
|
151 | - // only anyone to view, but only those with the default role can do anything |
|
152 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
153 | - |
|
154 | - parent::__construct($timezone); |
|
155 | - } |
|
156 | - |
|
157 | - |
|
158 | - /** |
|
159 | - * @return array |
|
160 | - * @throws EE_Error |
|
161 | - * @throws ReflectionException |
|
162 | - */ |
|
163 | - public function get_all_countries() |
|
164 | - { |
|
165 | - if (! self::$_all_countries) { |
|
166 | - self::$_all_countries = $this->get_all(['order_by' => ['CNT_name' => 'ASC'], 'limit' => [0, 99999]]); |
|
167 | - } |
|
168 | - return self::$_all_countries; |
|
169 | - } |
|
170 | - |
|
171 | - |
|
172 | - /** |
|
173 | - * _get_countries |
|
174 | - * Gets and caches the list of active countries. If you know the list of active countries |
|
175 | - * has changed during this request, first use EEM_Country::reset() to flush the cache |
|
176 | - * |
|
177 | - * @return array |
|
178 | - * @throws EE_Error |
|
179 | - * @throws ReflectionException |
|
180 | - */ |
|
181 | - public function get_all_active_countries() |
|
182 | - { |
|
183 | - if (! self::$_active_countries) { |
|
184 | - self::$_active_countries = |
|
185 | - $this->get_all([['CNT_active' => true], 'order_by' => ['CNT_name' => 'ASC'], 'limit' => [0, 99999]]); |
|
186 | - } |
|
187 | - return self::$_active_countries; |
|
188 | - } |
|
189 | - |
|
190 | - |
|
191 | - /** |
|
192 | - * Gets the country's name by its ISO |
|
193 | - * |
|
194 | - * @param string $country_ISO |
|
195 | - * @return string |
|
196 | - * @throws EE_Error |
|
197 | - * @throws ReflectionException |
|
198 | - */ |
|
199 | - public function get_country_name_by_ISO($country_ISO) |
|
200 | - { |
|
201 | - if ( |
|
202 | - isset(self::$_all_countries[ $country_ISO ]) |
|
203 | - && self::$_all_countries[ $country_ISO ] instanceof EE_Country |
|
204 | - ) { |
|
205 | - return self::$_all_countries[ $country_ISO ]->name(); |
|
206 | - } |
|
207 | - $names = $this->get_col([['CNT_ISO' => $country_ISO], 'limit' => 1], 'CNT_name'); |
|
208 | - if (is_array($names) && ! empty($names)) { |
|
209 | - return reset($names); |
|
210 | - } |
|
211 | - return ''; |
|
212 | - } |
|
13 | + // private instance of the Attendee object |
|
14 | + protected static $_instance; |
|
15 | + |
|
16 | + // array of all countries |
|
17 | + private static $_all_countries = false; |
|
18 | + |
|
19 | + // array of all active countries |
|
20 | + private static $_active_countries = false; |
|
21 | + |
|
22 | + |
|
23 | + /** |
|
24 | + * Resets the country |
|
25 | + * |
|
26 | + * @param string $timezone |
|
27 | + * @return EEM_Country |
|
28 | + * @throws EE_Error |
|
29 | + * @throws ReflectionException |
|
30 | + */ |
|
31 | + public static function reset(string $timezone = ''): EEM_Country |
|
32 | + { |
|
33 | + self::$_active_countries = null; |
|
34 | + self::$_all_countries = null; |
|
35 | + return parent::reset($timezone); |
|
36 | + } |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * EEM_Country constructor. |
|
41 | + * |
|
42 | + * @param string $timezone |
|
43 | + * @throws EE_Error |
|
44 | + */ |
|
45 | + protected function __construct(string $timezone = '') |
|
46 | + { |
|
47 | + $this->singular_item = esc_html__('Country', 'event_espresso'); |
|
48 | + $this->plural_item = esc_html__('Countries', 'event_espresso'); |
|
49 | + |
|
50 | + $this->_tables = [ |
|
51 | + 'Country' => new EE_Primary_Table('esp_country', 'CNT_ISO'), |
|
52 | + ]; |
|
53 | + |
|
54 | + $this->_fields = [ |
|
55 | + 'Country' => [ |
|
56 | + 'CNT_active' => new EE_Boolean_Field( |
|
57 | + 'CNT_active', |
|
58 | + esc_html__( |
|
59 | + 'Country Appears in Dropdown Select Lists', |
|
60 | + 'event_espresso' |
|
61 | + ), |
|
62 | + false, |
|
63 | + true |
|
64 | + ), |
|
65 | + 'CNT_ISO' => new EE_Primary_Key_String_Field( |
|
66 | + 'CNT_ISO', |
|
67 | + esc_html__('Country ISO Code', 'event_espresso') |
|
68 | + ), |
|
69 | + 'CNT_ISO3' => new EE_All_Caps_Text_Field( |
|
70 | + 'CNT_ISO3', |
|
71 | + esc_html__('Country ISO3 Code', 'event_espresso'), |
|
72 | + false, |
|
73 | + '' |
|
74 | + ), |
|
75 | + 'RGN_ID' => new EE_Integer_Field( |
|
76 | + 'RGN_ID', |
|
77 | + esc_html__('Region ID', 'event_espresso'), |
|
78 | + false, |
|
79 | + 0 |
|
80 | + ), |
|
81 | + // should be a foreign key, but no region table exists yet |
|
82 | + 'CNT_name' => new EE_Plain_Text_Field( |
|
83 | + 'CNT_name', |
|
84 | + esc_html__('Country Name', 'event_espresso'), |
|
85 | + false, |
|
86 | + '' |
|
87 | + ), |
|
88 | + 'CNT_cur_code' => new EE_All_Caps_Text_Field( |
|
89 | + 'CNT_cur_code', |
|
90 | + esc_html__('Country Currency Code', 'event_espresso'), |
|
91 | + false |
|
92 | + ), |
|
93 | + 'CNT_cur_single' => new EE_Plain_Text_Field( |
|
94 | + 'CNT_cur_single', |
|
95 | + esc_html__('Currency Name Singular', 'event_espresso'), |
|
96 | + false |
|
97 | + ), |
|
98 | + 'CNT_cur_plural' => new EE_Plain_Text_Field( |
|
99 | + 'CNT_cur_plural', |
|
100 | + esc_html__('Currency Name Plural', 'event_espresso'), |
|
101 | + false |
|
102 | + ), |
|
103 | + 'CNT_cur_sign' => new EE_Plain_Text_Field( |
|
104 | + 'CNT_cur_sign', |
|
105 | + esc_html__('Currency Sign', 'event_espresso'), |
|
106 | + false |
|
107 | + ), |
|
108 | + 'CNT_cur_sign_b4' => new EE_Boolean_Field( |
|
109 | + 'CNT_cur_sign_b4', |
|
110 | + esc_html__('Currency Sign Before Number', 'event_espresso'), |
|
111 | + false, |
|
112 | + true |
|
113 | + ), |
|
114 | + 'CNT_cur_dec_plc' => new EE_Integer_Field( |
|
115 | + 'CNT_cur_dec_plc', |
|
116 | + esc_html__('Currency Decimal Places', 'event_espresso'), |
|
117 | + false, |
|
118 | + 2 |
|
119 | + ), |
|
120 | + 'CNT_cur_dec_mrk' => new EE_Plain_Text_Field( |
|
121 | + 'CNT_cur_dec_mrk', |
|
122 | + esc_html__('Currency Decimal Mark', 'event_espresso'), |
|
123 | + false, |
|
124 | + '.' |
|
125 | + ), |
|
126 | + 'CNT_cur_thsnds' => new EE_Plain_Text_Field( |
|
127 | + 'CNT_cur_thsnds', |
|
128 | + esc_html__('Currency Thousands Separator', 'event_espresso'), |
|
129 | + false, |
|
130 | + ',' |
|
131 | + ), |
|
132 | + 'CNT_tel_code' => new EE_Plain_Text_Field( |
|
133 | + 'CNT_tel_code', |
|
134 | + esc_html__('Country Telephone Code', 'event_espresso'), |
|
135 | + false, |
|
136 | + '' |
|
137 | + ), |
|
138 | + 'CNT_is_EU' => new EE_Boolean_Field( |
|
139 | + 'CNT_is_EU', |
|
140 | + esc_html__('Country is Member of EU', 'event_espresso'), |
|
141 | + false, |
|
142 | + false |
|
143 | + ), |
|
144 | + ], |
|
145 | + ]; |
|
146 | + $this->_model_relations = [ |
|
147 | + 'Attendee' => new EE_Has_Many_Relation(), |
|
148 | + 'State' => new EE_Has_Many_Relation(), |
|
149 | + 'Venue' => new EE_Has_Many_Relation(), |
|
150 | + ]; |
|
151 | + // only anyone to view, but only those with the default role can do anything |
|
152 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
153 | + |
|
154 | + parent::__construct($timezone); |
|
155 | + } |
|
156 | + |
|
157 | + |
|
158 | + /** |
|
159 | + * @return array |
|
160 | + * @throws EE_Error |
|
161 | + * @throws ReflectionException |
|
162 | + */ |
|
163 | + public function get_all_countries() |
|
164 | + { |
|
165 | + if (! self::$_all_countries) { |
|
166 | + self::$_all_countries = $this->get_all(['order_by' => ['CNT_name' => 'ASC'], 'limit' => [0, 99999]]); |
|
167 | + } |
|
168 | + return self::$_all_countries; |
|
169 | + } |
|
170 | + |
|
171 | + |
|
172 | + /** |
|
173 | + * _get_countries |
|
174 | + * Gets and caches the list of active countries. If you know the list of active countries |
|
175 | + * has changed during this request, first use EEM_Country::reset() to flush the cache |
|
176 | + * |
|
177 | + * @return array |
|
178 | + * @throws EE_Error |
|
179 | + * @throws ReflectionException |
|
180 | + */ |
|
181 | + public function get_all_active_countries() |
|
182 | + { |
|
183 | + if (! self::$_active_countries) { |
|
184 | + self::$_active_countries = |
|
185 | + $this->get_all([['CNT_active' => true], 'order_by' => ['CNT_name' => 'ASC'], 'limit' => [0, 99999]]); |
|
186 | + } |
|
187 | + return self::$_active_countries; |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + /** |
|
192 | + * Gets the country's name by its ISO |
|
193 | + * |
|
194 | + * @param string $country_ISO |
|
195 | + * @return string |
|
196 | + * @throws EE_Error |
|
197 | + * @throws ReflectionException |
|
198 | + */ |
|
199 | + public function get_country_name_by_ISO($country_ISO) |
|
200 | + { |
|
201 | + if ( |
|
202 | + isset(self::$_all_countries[ $country_ISO ]) |
|
203 | + && self::$_all_countries[ $country_ISO ] instanceof EE_Country |
|
204 | + ) { |
|
205 | + return self::$_all_countries[ $country_ISO ]->name(); |
|
206 | + } |
|
207 | + $names = $this->get_col([['CNT_ISO' => $country_ISO], 'limit' => 1], 'CNT_name'); |
|
208 | + if (is_array($names) && ! empty($names)) { |
|
209 | + return reset($names); |
|
210 | + } |
|
211 | + return ''; |
|
212 | + } |
|
213 | 213 | } |
@@ -37,6569 +37,6569 @@ |
||
37 | 37 | abstract class EEM_Base extends EE_Base implements ResettableInterface |
38 | 38 | { |
39 | 39 | |
40 | - /** |
|
41 | - * constants used to categorize capability restrictions on EEM_Base::_caps_restrictions |
|
42 | - */ |
|
43 | - const caps_read = 'read'; |
|
44 | - |
|
45 | - const caps_read_admin = 'read_admin'; |
|
46 | - |
|
47 | - const caps_edit = 'edit'; |
|
48 | - |
|
49 | - const caps_delete = 'delete'; |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * constant used to show EEM_Base has not yet verified the db on this http request |
|
54 | - */ |
|
55 | - const db_verified_none = 0; |
|
56 | - |
|
57 | - /** |
|
58 | - * constant used to show EEM_Base has verified the EE core db on this http request, |
|
59 | - * but not the addons' dbs |
|
60 | - */ |
|
61 | - const db_verified_core = 1; |
|
62 | - |
|
63 | - /** |
|
64 | - * constant used to show EEM_Base has verified the addons' dbs (and implicitly |
|
65 | - * the EE core db too) |
|
66 | - */ |
|
67 | - const db_verified_addons = 2; |
|
68 | - |
|
69 | - /** |
|
70 | - * @const constant for 'default_where_conditions' to apply default where conditions to ALL queried models |
|
71 | - * (eg, if retrieving registrations ordered by their datetimes, this will only return non-trashed |
|
72 | - * registrations for non-trashed tickets for non-trashed datetimes) |
|
73 | - */ |
|
74 | - const default_where_conditions_all = 'all'; |
|
75 | - |
|
76 | - /** |
|
77 | - * @const constant for 'default_where_conditions' to apply default where conditions to THIS model only, but |
|
78 | - * no other models which are joined to (eg, if retrieving registrations ordered by their datetimes, this will |
|
79 | - * return non-trashed registrations, regardless of the related datetimes and tickets' statuses). |
|
80 | - * It is preferred to use EEM_Base::default_where_conditions_minimum_others because, when joining to |
|
81 | - * models which share tables with other models, this can return data for the wrong model. |
|
82 | - */ |
|
83 | - const default_where_conditions_this_only = 'this_model_only'; |
|
84 | - |
|
85 | - /** |
|
86 | - * @const constant for 'default_where_conditions' to apply default where conditions to other models queried, |
|
87 | - * but not the current model (eg, if retrieving registrations ordered by their datetimes, this will |
|
88 | - * return all registrations related to non-trashed tickets and non-trashed datetimes) |
|
89 | - */ |
|
90 | - const default_where_conditions_others_only = 'other_models_only'; |
|
91 | - |
|
92 | - /** |
|
93 | - * @const constant for 'default_where_conditions' to apply minimum where conditions to all models queried. |
|
94 | - * For most models this the same as EEM_Base::default_where_conditions_none, except for models which share |
|
95 | - * their table with other models, like the Event and Venue models. For example, when querying for events |
|
96 | - * ordered by their venues' name, this will be sure to only return real events with associated real venues |
|
97 | - * (regardless of whether those events and venues are trashed) |
|
98 | - * In contrast, using EEM_Base::default_where_conditions_none would could return WP posts other than EE |
|
99 | - * events. |
|
100 | - */ |
|
101 | - const default_where_conditions_minimum_all = 'minimum'; |
|
102 | - |
|
103 | - /** |
|
104 | - * @const constant for 'default_where_conditions' to apply apply where conditions to other models, and full default |
|
105 | - * where conditions for the queried model (eg, when querying events ordered by venues' names, this will |
|
106 | - * return non-trashed events for any venues, regardless of whether those associated venues are trashed or |
|
107 | - * not) |
|
108 | - */ |
|
109 | - const default_where_conditions_minimum_others = 'full_this_minimum_others'; |
|
110 | - |
|
111 | - /** |
|
112 | - * @const constant for 'default_where_conditions' to NOT apply any where conditions. This should very rarely be |
|
113 | - * used, because when querying from a model which shares its table with another model (eg Events and Venues) |
|
114 | - * it's possible it will return table entries for other models. You should use |
|
115 | - * EEM_Base::default_where_conditions_minimum_all instead. |
|
116 | - */ |
|
117 | - const default_where_conditions_none = 'none'; |
|
118 | - |
|
119 | - /** |
|
120 | - * when $_values_already_prepared_by_model_object equals this, we assume |
|
121 | - * the data is just like form input that needs to have the model fields' |
|
122 | - * prepare_for_set and prepare_for_use_in_db called on it |
|
123 | - */ |
|
124 | - const not_prepared_by_model_object = 0; |
|
125 | - |
|
126 | - /** |
|
127 | - * when $_values_already_prepared_by_model_object equals this, we |
|
128 | - * assume this value is coming from a model object and doesn't need to have |
|
129 | - * prepare_for_set called on it, just prepare_for_use_in_db is used |
|
130 | - */ |
|
131 | - const prepared_by_model_object = 1; |
|
132 | - |
|
133 | - /** |
|
134 | - * when $_values_already_prepared_by_model_object equals this, we assume |
|
135 | - * the values are already to be used in the database (ie no processing is done |
|
136 | - * on them by the model's fields) |
|
137 | - */ |
|
138 | - const prepared_for_use_in_db = 2; |
|
139 | - |
|
140 | - /** |
|
141 | - * Flag to indicate whether the values provided to EEM_Base have already been prepared |
|
142 | - * by the model object or not (ie, the model object has used the field's _prepare_for_set function on the values). |
|
143 | - * They almost always WILL NOT, but it's not necessarily a requirement. |
|
144 | - * For example, if you want to run EEM_Event::instance()->get_all(array(array('EVT_ID'=>$_GET['event_id']))); |
|
145 | - * |
|
146 | - * @var boolean |
|
147 | - */ |
|
148 | - private $_values_already_prepared_by_model_object = 0; |
|
149 | - |
|
150 | - |
|
151 | - /** |
|
152 | - * @var string |
|
153 | - */ |
|
154 | - protected $singular_item = 'Item'; |
|
155 | - |
|
156 | - /** |
|
157 | - * @var string |
|
158 | - */ |
|
159 | - protected $plural_item = 'Items'; |
|
160 | - |
|
161 | - /** |
|
162 | - * array of EE_Table objects for defining which tables comprise this model. |
|
163 | - * |
|
164 | - * @type EE_Table_Base[] $_tables |
|
165 | - */ |
|
166 | - protected $_tables; |
|
167 | - |
|
168 | - /** |
|
169 | - * with two levels: top-level has array keys which are database table aliases (ie, keys in _tables) |
|
170 | - * and the value is an array. Each of those sub-arrays have keys of field names (eg 'ATT_ID', which should also be |
|
171 | - * variable names on the model objects (eg, EE_Attendee), and the keys should be children of EE_Model_Field |
|
172 | - * |
|
173 | - * @var EE_Model_Field_Base[][] $_fields |
|
174 | - */ |
|
175 | - protected $_fields; |
|
176 | - |
|
177 | - /** |
|
178 | - * array of different kinds of relations |
|
179 | - * |
|
180 | - * @var EE_Model_Relation_Base[] $_model_relations |
|
181 | - */ |
|
182 | - protected $_model_relations; |
|
183 | - |
|
184 | - /** |
|
185 | - * @var EE_Index[] $_indexes |
|
186 | - */ |
|
187 | - protected $_indexes = []; |
|
188 | - |
|
189 | - /** |
|
190 | - * Default strategy for getting where conditions on this model. This strategy is used to get default |
|
191 | - * where conditions which are added to get_all, update, and delete queries. They can be overridden |
|
192 | - * by setting the same columns as used in these queries in the query yourself. |
|
193 | - * |
|
194 | - * @var EE_Default_Where_Conditions |
|
195 | - */ |
|
196 | - protected $_default_where_conditions_strategy; |
|
197 | - |
|
198 | - /** |
|
199 | - * Strategy for getting conditions on this model when 'default_where_conditions' equals 'minimum'. |
|
200 | - * This is particularly useful when you want something between 'none' and 'default' |
|
201 | - * |
|
202 | - * @var EE_Default_Where_Conditions |
|
203 | - */ |
|
204 | - protected $_minimum_where_conditions_strategy; |
|
205 | - |
|
206 | - /** |
|
207 | - * String describing how to find the "owner" of this model's objects. |
|
208 | - * When there is a foreign key on this model to the wp_users table, this isn't needed. |
|
209 | - * But when there isn't, this indicates which related model, or transiently-related model, |
|
210 | - * has the foreign key to the wp_users table. |
|
211 | - * Eg, for EEM_Registration this would be 'Event' because registrations are directly |
|
212 | - * related to events, and events have a foreign key to wp_users. |
|
213 | - * On EEM_Transaction, this would be 'Transaction.Event' |
|
214 | - * |
|
215 | - * @var string |
|
216 | - */ |
|
217 | - protected $_model_chain_to_wp_user = ''; |
|
218 | - |
|
219 | - /** |
|
220 | - * String describing how to find the model with a password controlling access to this model. This property has the |
|
221 | - * same format as $_model_chain_to_wp_user. This is primarily used by the query param "exclude_protected". |
|
222 | - * This value is the path of models to follow to arrive at the model with the password field. |
|
223 | - * If it is an empty string, it means this model has the password field. If it is null, it means there is no |
|
224 | - * model with a password that should affect reading this on the front-end. |
|
225 | - * Eg this is an empty string for the Event model because it has a password. |
|
226 | - * This is null for the Registration model, because its event's password has no bearing on whether |
|
227 | - * you can read the registration or not on the front-end (it just depends on your capabilities.) |
|
228 | - * This is 'Datetime.Event' on the Ticket model, because model queries for tickets that set "exclude_protected" |
|
229 | - * should hide tickets for datetimes for events that have a password set. |
|
230 | - * |
|
231 | - * @var string |null |
|
232 | - */ |
|
233 | - protected $model_chain_to_password = null; |
|
234 | - |
|
235 | - /** |
|
236 | - * This is a flag typically set by updates so that we don't load the where strategy on updates because updates |
|
237 | - * don't need it (particularly CPT models) |
|
238 | - * |
|
239 | - * @var bool |
|
240 | - */ |
|
241 | - protected $_ignore_where_strategy = false; |
|
242 | - |
|
243 | - /** |
|
244 | - * String used in caps relating to this model. Eg, if the caps relating to this |
|
245 | - * model are 'ee_edit_events', 'ee_read_events', etc, it would be 'events'. |
|
246 | - * |
|
247 | - * @var string. If null it hasn't been initialized yet. If false then we |
|
248 | - * have indicated capabilities don't apply to this |
|
249 | - */ |
|
250 | - protected $_caps_slug = null; |
|
251 | - |
|
252 | - /** |
|
253 | - * 2d array where top-level keys are one of EEM_Base::valid_cap_contexts(), |
|
254 | - * and next-level keys are capability names, and values are a |
|
255 | - * EE_Default_Where_Condition. If the requester requests to apply caps to the query, |
|
256 | - * they specify which context to use (ie, frontend, backend, edit or delete) |
|
257 | - * and then each capability in the corresponding sub-array that they're missing |
|
258 | - * adds the where conditions onto the query. |
|
259 | - * |
|
260 | - * @var array |
|
261 | - */ |
|
262 | - protected $_cap_restrictions = [ |
|
263 | - self::caps_read => [], |
|
264 | - self::caps_read_admin => [], |
|
265 | - self::caps_edit => [], |
|
266 | - self::caps_delete => [], |
|
267 | - ]; |
|
268 | - |
|
269 | - /** |
|
270 | - * Array defining which cap restriction generators to use to create default |
|
271 | - * cap restrictions to put in EEM_Base::_cap_restrictions. |
|
272 | - * Array-keys are one of EEM_Base::valid_cap_contexts(), and values are a child of |
|
273 | - * EE_Restriction_Generator_Base. If you don't want any cap restrictions generated |
|
274 | - * automatically set this to false (not just null). |
|
275 | - * |
|
276 | - * @var EE_Restriction_Generator_Base[] |
|
277 | - */ |
|
278 | - protected $_cap_restriction_generators = []; |
|
279 | - |
|
280 | - /** |
|
281 | - * Keys are all the cap contexts (ie constants EEM_Base::_caps_*) and values are their 'action' |
|
282 | - * as how they'd be used in capability names. Eg EEM_Base::caps_read ('read_frontend') |
|
283 | - * maps to 'read' because when looking for relevant permissions we're going to use |
|
284 | - * 'read' in teh capabilities names like 'ee_read_events' etc. |
|
285 | - * |
|
286 | - * @var array |
|
287 | - */ |
|
288 | - protected $_cap_contexts_to_cap_action_map = [ |
|
289 | - self::caps_read => 'read', |
|
290 | - self::caps_read_admin => 'read', |
|
291 | - self::caps_edit => 'edit', |
|
292 | - self::caps_delete => 'delete', |
|
293 | - ]; |
|
294 | - |
|
295 | - /** |
|
296 | - * Timezone |
|
297 | - * This gets set via the constructor so that we know what timezone incoming strings|timestamps are in when there |
|
298 | - * are EE_Datetime_Fields in use. This can also be used before a get to set what timezone you want strings coming |
|
299 | - * out of the created objects. NOT all EEM_Base child classes use this property but any that use a |
|
300 | - * EE_Datetime_Field data type will have access to it. |
|
301 | - * |
|
302 | - * @var string |
|
303 | - */ |
|
304 | - protected $_timezone; |
|
305 | - |
|
306 | - |
|
307 | - /** |
|
308 | - * This holds the id of the blog currently making the query. Has no bearing on single site but is used for |
|
309 | - * multisite. |
|
310 | - * |
|
311 | - * @var int |
|
312 | - */ |
|
313 | - protected static $_model_query_blog_id; |
|
314 | - |
|
315 | - /** |
|
316 | - * A copy of _fields, except the array keys are the model names pointed to by |
|
317 | - * the field |
|
318 | - * |
|
319 | - * @var EE_Model_Field_Base[] |
|
320 | - */ |
|
321 | - private $_cache_foreign_key_to_fields = []; |
|
322 | - |
|
323 | - /** |
|
324 | - * Cached list of all the fields on the model, indexed by their name |
|
325 | - * |
|
326 | - * @var EE_Model_Field_Base[] |
|
327 | - */ |
|
328 | - private $_cached_fields = null; |
|
329 | - |
|
330 | - /** |
|
331 | - * Cached list of all the fields on the model, except those that are |
|
332 | - * marked as only pertinent to the database |
|
333 | - * |
|
334 | - * @var EE_Model_Field_Base[] |
|
335 | - */ |
|
336 | - private $_cached_fields_non_db_only = null; |
|
337 | - |
|
338 | - /** |
|
339 | - * A cached reference to the primary key for quick lookup |
|
340 | - * |
|
341 | - * @var EE_Model_Field_Base |
|
342 | - */ |
|
343 | - private $_primary_key_field = null; |
|
344 | - |
|
345 | - /** |
|
346 | - * Flag indicating whether this model has a primary key or not |
|
347 | - * |
|
348 | - * @var boolean |
|
349 | - */ |
|
350 | - protected $_has_primary_key_field = null; |
|
351 | - |
|
352 | - /** |
|
353 | - * array in the format: [ FK alias => full PK ] |
|
354 | - * where keys are local column name aliases for foreign keys |
|
355 | - * and values are the fully qualified column name for the primary key they represent |
|
356 | - * ex: |
|
357 | - * [ 'Event.EVT_wp_user' => 'WP_User.ID' ] |
|
358 | - * |
|
359 | - * @var array $foreign_key_aliases |
|
360 | - */ |
|
361 | - protected $foreign_key_aliases = []; |
|
362 | - |
|
363 | - /** |
|
364 | - * Whether or not this model is based off a table in WP core only (CPTs should set |
|
365 | - * this to FALSE, but if we were to make an EE_WP_Post model, it should set this to true). |
|
366 | - * This should be true for models that deal with data that should exist independent of EE. |
|
367 | - * For example, if the model can read and insert data that isn't used by EE, this should be true. |
|
368 | - * It would be false, however, if you could guarantee the model would only interact with EE data, |
|
369 | - * even if it uses a WP core table (eg event and venue models set this to false for that reason: |
|
370 | - * they can only read and insert events and venues custom post types, not arbitrary post types) |
|
371 | - * |
|
372 | - * @var boolean |
|
373 | - */ |
|
374 | - protected $_wp_core_model = false; |
|
375 | - |
|
376 | - /** |
|
377 | - * @var bool stores whether this model has a password field or not. |
|
378 | - * null until initialized by hasPasswordField() |
|
379 | - */ |
|
380 | - protected $has_password_field; |
|
381 | - |
|
382 | - /** |
|
383 | - * @var EE_Password_Field|null Automatically set when calling getPasswordField() |
|
384 | - */ |
|
385 | - protected $password_field; |
|
386 | - |
|
387 | - /** |
|
388 | - * List of valid operators that can be used for querying. |
|
389 | - * The keys are all operators we'll accept, the values are the real SQL |
|
390 | - * operators used |
|
391 | - * |
|
392 | - * @var array |
|
393 | - */ |
|
394 | - protected $_valid_operators = [ |
|
395 | - '=' => '=', |
|
396 | - '<=' => '<=', |
|
397 | - '<' => '<', |
|
398 | - '>=' => '>=', |
|
399 | - '>' => '>', |
|
400 | - '!=' => '!=', |
|
401 | - 'LIKE' => 'LIKE', |
|
402 | - 'like' => 'LIKE', |
|
403 | - 'NOT_LIKE' => 'NOT LIKE', |
|
404 | - 'not_like' => 'NOT LIKE', |
|
405 | - 'NOT LIKE' => 'NOT LIKE', |
|
406 | - 'not like' => 'NOT LIKE', |
|
407 | - 'IN' => 'IN', |
|
408 | - 'in' => 'IN', |
|
409 | - 'NOT_IN' => 'NOT IN', |
|
410 | - 'not_in' => 'NOT IN', |
|
411 | - 'NOT IN' => 'NOT IN', |
|
412 | - 'not in' => 'NOT IN', |
|
413 | - 'between' => 'BETWEEN', |
|
414 | - 'BETWEEN' => 'BETWEEN', |
|
415 | - 'IS_NOT_NULL' => 'IS NOT NULL', |
|
416 | - 'is_not_null' => 'IS NOT NULL', |
|
417 | - 'IS NOT NULL' => 'IS NOT NULL', |
|
418 | - 'is not null' => 'IS NOT NULL', |
|
419 | - 'IS_NULL' => 'IS NULL', |
|
420 | - 'is_null' => 'IS NULL', |
|
421 | - 'IS NULL' => 'IS NULL', |
|
422 | - 'is null' => 'IS NULL', |
|
423 | - 'REGEXP' => 'REGEXP', |
|
424 | - 'regexp' => 'REGEXP', |
|
425 | - 'NOT_REGEXP' => 'NOT REGEXP', |
|
426 | - 'not_regexp' => 'NOT REGEXP', |
|
427 | - 'NOT REGEXP' => 'NOT REGEXP', |
|
428 | - 'not regexp' => 'NOT REGEXP', |
|
429 | - ]; |
|
430 | - |
|
431 | - /** |
|
432 | - * operators that work like 'IN', accepting a comma-separated list of values inside brackets. Eg '(1,2,3)' |
|
433 | - * |
|
434 | - * @var array |
|
435 | - */ |
|
436 | - protected $_in_style_operators = ['IN', 'NOT IN']; |
|
437 | - |
|
438 | - /** |
|
439 | - * operators that work like 'BETWEEN'. Typically used for datetime calculations, i.e. "BETWEEN '12-1-2011' AND |
|
440 | - * '12-31-2012'" |
|
441 | - * |
|
442 | - * @var array |
|
443 | - */ |
|
444 | - protected $_between_style_operators = ['BETWEEN']; |
|
445 | - |
|
446 | - /** |
|
447 | - * Operators that work like SQL's like: input should be assumed to be a string, already prepared for a LIKE query. |
|
448 | - * |
|
449 | - * @var array |
|
450 | - */ |
|
451 | - protected $_like_style_operators = ['LIKE', 'NOT LIKE']; |
|
452 | - |
|
453 | - /** |
|
454 | - * operators that are used for handling NUll and !NULL queries. Typically used for when checking if a row exists |
|
455 | - * on a join table. |
|
456 | - * |
|
457 | - * @var array |
|
458 | - */ |
|
459 | - protected $_null_style_operators = ['IS NOT NULL', 'IS NULL']; |
|
460 | - |
|
461 | - /** |
|
462 | - * Allowed values for $query_params['order'] for ordering in queries |
|
463 | - * |
|
464 | - * @var array |
|
465 | - */ |
|
466 | - protected $_allowed_order_values = ['asc', 'desc', 'ASC', 'DESC']; |
|
467 | - |
|
468 | - /** |
|
469 | - * When these are keys in a WHERE or HAVING clause, they are handled much differently |
|
470 | - * than regular field names. It is assumed that their values are an array of WHERE conditions |
|
471 | - * |
|
472 | - * @var array |
|
473 | - */ |
|
474 | - private $_logic_query_param_keys = ['not', 'and', 'or', 'NOT', 'AND', 'OR']; |
|
475 | - |
|
476 | - /** |
|
477 | - * Allowed keys in $query_params arrays passed into queries. Note that 0 is meant to always be a |
|
478 | - * 'where', but 'where' clauses are so common that we thought we'd omit it |
|
479 | - * |
|
480 | - * @var array |
|
481 | - */ |
|
482 | - private $_allowed_query_params = [ |
|
483 | - 0, |
|
484 | - 'limit', |
|
485 | - 'order_by', |
|
486 | - 'group_by', |
|
487 | - 'having', |
|
488 | - 'force_join', |
|
489 | - 'order', |
|
490 | - 'on_join_limit', |
|
491 | - 'default_where_conditions', |
|
492 | - 'caps', |
|
493 | - 'extra_selects', |
|
494 | - 'exclude_protected', |
|
495 | - ]; |
|
496 | - |
|
497 | - /** |
|
498 | - * All the data types that can be used in $wpdb->prepare statements. |
|
499 | - * |
|
500 | - * @var array |
|
501 | - */ |
|
502 | - private $_valid_wpdb_data_types = ['%d', '%s', '%f']; |
|
503 | - |
|
504 | - /** |
|
505 | - * @var EE_Registry $EE |
|
506 | - */ |
|
507 | - protected $EE = null; |
|
508 | - |
|
509 | - |
|
510 | - /** |
|
511 | - * Property which, when set, will have this model echo out the next X queries to the page for debugging. |
|
512 | - * |
|
513 | - * @var int |
|
514 | - */ |
|
515 | - protected $_show_next_x_db_queries = 0; |
|
516 | - |
|
517 | - /** |
|
518 | - * When using _get_all_wpdb_results, you can specify a custom selection. If you do so, |
|
519 | - * it gets saved on this property as an instance of CustomSelects so those selections can be used in |
|
520 | - * WHERE, GROUP_BY, etc. |
|
521 | - * |
|
522 | - * @var CustomSelects |
|
523 | - */ |
|
524 | - protected $_custom_selections = []; |
|
525 | - |
|
526 | - /** |
|
527 | - * key => value Entity Map using array( EEM_Base::$_model_query_blog_id => array( ID => model object ) ) |
|
528 | - * caches every model object we've fetched from the DB on this request |
|
529 | - * |
|
530 | - * @var array |
|
531 | - */ |
|
532 | - protected $_entity_map; |
|
533 | - |
|
534 | - /** |
|
535 | - * @var LoaderInterface $loader |
|
536 | - */ |
|
537 | - private static $loader; |
|
538 | - |
|
539 | - /** |
|
540 | - * indicates whether an EEM_Base child has already re-verified the DB |
|
541 | - * is ok (we don't want to do it repetitively). Should be set to one the constants |
|
542 | - * looking like EEM_Base::db_verified_* |
|
543 | - * |
|
544 | - * @var int - 0 = none, 1 = core, 2 = addons |
|
545 | - */ |
|
546 | - protected static $_db_verification_level = EEM_Base::db_verified_none; |
|
547 | - |
|
548 | - |
|
549 | - /** |
|
550 | - * About all child constructors: |
|
551 | - * they should define the _tables, _fields and _model_relations arrays. |
|
552 | - * Should ALWAYS be called after child constructor. |
|
553 | - * In order to make the child constructors to be as simple as possible, this parent constructor |
|
554 | - * finalizes constructing all the object's attributes. |
|
555 | - * Generally, rather than requiring a child to code |
|
556 | - * $this->_tables = array( |
|
557 | - * 'Event_Post_Table' => new EE_Table('Event_Post_Table','wp_posts') |
|
558 | - * ...); |
|
559 | - * (thus repeating itself in the array key and in the constructor of the new EE_Table,) |
|
560 | - * each EE_Table has a function to set the table's alias after the constructor, using |
|
561 | - * the array key ('Event_Post_Table'), instead of repeating it. The model fields and model relations |
|
562 | - * do something similar. |
|
563 | - * |
|
564 | - * @param string $timezone |
|
565 | - * @throws EE_Error |
|
566 | - */ |
|
567 | - protected function __construct($timezone = '') |
|
568 | - { |
|
569 | - // check that the model has not been loaded too soon |
|
570 | - if (! did_action('AHEE__EE_System__load_espresso_addons')) { |
|
571 | - throw new EE_Error( |
|
572 | - sprintf( |
|
573 | - esc_html__( |
|
574 | - 'The %1$s model can not be loaded before the "AHEE__EE_System__load_espresso_addons" hook has been called. This gives other addons a chance to extend this model.', |
|
575 | - 'event_espresso' |
|
576 | - ), |
|
577 | - get_class($this) |
|
578 | - ) |
|
579 | - ); |
|
580 | - } |
|
581 | - /** |
|
582 | - * Set blog id for models to current blog. However we ONLY do this if $_model_query_blog_id is not already set. |
|
583 | - */ |
|
584 | - if (empty(EEM_Base::$_model_query_blog_id)) { |
|
585 | - EEM_Base::set_model_query_blog_id(); |
|
586 | - } |
|
587 | - /** |
|
588 | - * Filters the list of tables on a model. It is best to NOT use this directly and instead |
|
589 | - * just use EE_Register_Model_Extension |
|
590 | - * |
|
591 | - * @var EE_Table_Base[] $_tables |
|
592 | - */ |
|
593 | - $this->_tables = (array) apply_filters('FHEE__' . get_class($this) . '__construct__tables', $this->_tables); |
|
594 | - foreach ($this->_tables as $table_alias => $table_obj) { |
|
595 | - /** @var $table_obj EE_Table_Base */ |
|
596 | - $table_obj->_construct_finalize_with_alias($table_alias); |
|
597 | - if ($table_obj instanceof EE_Secondary_Table) { |
|
598 | - /** @var $table_obj EE_Secondary_Table */ |
|
599 | - $table_obj->_construct_finalize_set_table_to_join_with($this->_get_main_table()); |
|
600 | - } |
|
601 | - } |
|
602 | - /** |
|
603 | - * Filters the list of fields on a model. It is best to NOT use this directly and instead just use |
|
604 | - * EE_Register_Model_Extension |
|
605 | - * |
|
606 | - * @param EE_Model_Field_Base[] $_fields |
|
607 | - */ |
|
608 | - $this->_fields = (array) apply_filters('FHEE__' . get_class($this) . '__construct__fields', $this->_fields); |
|
609 | - $this->_invalidate_field_caches(); |
|
610 | - foreach ($this->_fields as $table_alias => $fields_for_table) { |
|
611 | - if (! array_key_exists($table_alias, $this->_tables)) { |
|
612 | - throw new EE_Error( |
|
613 | - sprintf( |
|
614 | - esc_html__( |
|
615 | - "Table alias %s does not exist in EEM_Base child's _tables array. Only tables defined are %s", |
|
616 | - 'event_espresso' |
|
617 | - ), |
|
618 | - $table_alias, |
|
619 | - implode(",", $this->_fields) |
|
620 | - ) |
|
621 | - ); |
|
622 | - } |
|
623 | - foreach ($fields_for_table as $field_name => $field_obj) { |
|
624 | - /** @var $field_obj EE_Model_Field_Base | EE_Primary_Key_Field_Base */ |
|
625 | - // primary key field base has a slightly different _construct_finalize |
|
626 | - /** @var $field_obj EE_Model_Field_Base */ |
|
627 | - $field_obj->_construct_finalize($table_alias, $field_name, $this->get_this_model_name()); |
|
628 | - } |
|
629 | - } |
|
630 | - // everything is related to Extra_Meta |
|
631 | - if (get_class($this) !== 'EEM_Extra_Meta') { |
|
632 | - // make extra meta related to everything, but don't block deleting things just |
|
633 | - // because they have related extra meta info. For now just orphan those extra meta |
|
634 | - // in the future we should automatically delete them |
|
635 | - $this->_model_relations['Extra_Meta'] = new EE_Has_Many_Any_Relation(false); |
|
636 | - } |
|
637 | - // and change logs |
|
638 | - if (get_class($this) !== 'EEM_Change_Log') { |
|
639 | - $this->_model_relations['Change_Log'] = new EE_Has_Many_Any_Relation(false); |
|
640 | - } |
|
641 | - /** |
|
642 | - * Filters the list of relations on a model. It is best to NOT use this directly and instead just use |
|
643 | - * EE_Register_Model_Extension |
|
644 | - * |
|
645 | - * @param EE_Model_Relation_Base[] $_model_relations |
|
646 | - */ |
|
647 | - $this->_model_relations = (array) apply_filters( |
|
648 | - 'FHEE__' . get_class($this) . '__construct__model_relations', |
|
649 | - $this->_model_relations |
|
650 | - ); |
|
651 | - foreach ($this->_model_relations as $model_name => $relation_obj) { |
|
652 | - /** @var $relation_obj EE_Model_Relation_Base */ |
|
653 | - $relation_obj->_construct_finalize_set_models($this->get_this_model_name(), $model_name); |
|
654 | - } |
|
655 | - foreach ($this->_indexes as $index_name => $index_obj) { |
|
656 | - $index_obj->_construct_finalize($index_name, $this->get_this_model_name()); |
|
657 | - } |
|
658 | - $this->set_timezone($timezone); |
|
659 | - // finalize default where condition strategy, or set default |
|
660 | - if (! $this->_default_where_conditions_strategy) { |
|
661 | - // nothing was set during child constructor, so set default |
|
662 | - $this->_default_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
663 | - } |
|
664 | - $this->_default_where_conditions_strategy->_finalize_construct($this); |
|
665 | - if (! $this->_minimum_where_conditions_strategy) { |
|
666 | - // nothing was set during child constructor, so set default |
|
667 | - $this->_minimum_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
668 | - } |
|
669 | - $this->_minimum_where_conditions_strategy->_finalize_construct($this); |
|
670 | - // if the cap slug hasn't been set, and we haven't set it to false on purpose |
|
671 | - // to indicate to NOT set it, set it to the logical default |
|
672 | - if ($this->_caps_slug === null) { |
|
673 | - $this->_caps_slug = EEH_Inflector::pluralize_and_lower($this->get_this_model_name()); |
|
674 | - } |
|
675 | - // initialize the standard cap restriction generators if none were specified by the child constructor |
|
676 | - if (! empty($this->_cap_restriction_generators)) { |
|
677 | - foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
|
678 | - if (! isset($this->_cap_restriction_generators[ $cap_context ])) { |
|
679 | - $this->_cap_restriction_generators[ $cap_context ] = apply_filters( |
|
680 | - 'FHEE__EEM_Base___construct__standard_cap_restriction_generator', |
|
681 | - new EE_Restriction_Generator_Protected(), |
|
682 | - $cap_context, |
|
683 | - $this |
|
684 | - ); |
|
685 | - } |
|
686 | - } |
|
687 | - // } |
|
688 | - // if ($this->_cap_restriction_generators !== false) { |
|
689 | - // if there are cap restriction generators, use them to make the default cap restrictions |
|
690 | - foreach ($this->_cap_restriction_generators as $context => $generator_object) { |
|
691 | - if (! $generator_object) { |
|
692 | - continue; |
|
693 | - } |
|
694 | - if (! $generator_object instanceof EE_Restriction_Generator_Base) { |
|
695 | - throw new EE_Error( |
|
696 | - sprintf( |
|
697 | - esc_html__( |
|
698 | - 'Index "%1$s" in the model %2$s\'s _cap_restriction_generators is not a child of EE_Restriction_Generator_Base. It should be that or NULL.', |
|
699 | - 'event_espresso' |
|
700 | - ), |
|
701 | - $context, |
|
702 | - $this->get_this_model_name() |
|
703 | - ) |
|
704 | - ); |
|
705 | - } |
|
706 | - $action = $this->cap_action_for_context($context); |
|
707 | - if (! $generator_object->construction_finalized()) { |
|
708 | - $generator_object->_construct_finalize($this, $action); |
|
709 | - } |
|
710 | - } |
|
711 | - } |
|
712 | - do_action('AHEE__' . get_class($this) . '__construct__end'); |
|
713 | - } |
|
714 | - |
|
715 | - |
|
716 | - /** |
|
717 | - * Used to set the $_model_query_blog_id static property. |
|
718 | - * |
|
719 | - * @param int $blog_id If provided then will set the blog_id for the models to this id. If not provided then the |
|
720 | - * value for get_current_blog_id() will be used. |
|
721 | - */ |
|
722 | - public static function set_model_query_blog_id($blog_id = 0) |
|
723 | - { |
|
724 | - EEM_Base::$_model_query_blog_id = $blog_id > 0 ? (int) $blog_id : get_current_blog_id(); |
|
725 | - } |
|
726 | - |
|
727 | - |
|
728 | - /** |
|
729 | - * Returns whatever is set as the internal $model_query_blog_id. |
|
730 | - * |
|
731 | - * @return int |
|
732 | - */ |
|
733 | - public static function get_model_query_blog_id() |
|
734 | - { |
|
735 | - return EEM_Base::$_model_query_blog_id; |
|
736 | - } |
|
737 | - |
|
738 | - |
|
739 | - /** |
|
740 | - * This function is a singleton method used to instantiate the Espresso_model object |
|
741 | - * |
|
742 | - * @param string $timezone string representing the timezone we want to set for returned Date Time Strings |
|
743 | - * (and any incoming timezone data that gets saved). |
|
744 | - * Note this just sends the timezone info to the date time model field objects. |
|
745 | - * Default is NULL |
|
746 | - * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
|
747 | - * @return EEM_Base (as in the concrete child class) |
|
748 | - * @throws EE_Error |
|
749 | - * @throws InvalidArgumentException |
|
750 | - * @throws InvalidDataTypeException |
|
751 | - * @throws InvalidInterfaceException |
|
752 | - */ |
|
753 | - public static function instance($timezone = '') |
|
754 | - { |
|
755 | - // check if instance of Espresso_model already exists |
|
756 | - if (! static::$_instance instanceof static) { |
|
757 | - // instantiate Espresso_model |
|
758 | - static::$_instance = new static( |
|
759 | - $timezone, |
|
760 | - LoaderFactory::getLoader()->load('EventEspresso\core\services\orm\ModelFieldFactory') |
|
761 | - ); |
|
762 | - } |
|
763 | - // Espresso model object |
|
764 | - return static::$_instance; |
|
765 | - } |
|
766 | - |
|
767 | - |
|
768 | - /** |
|
769 | - * resets the model and returns it |
|
770 | - * |
|
771 | - * @param string $timezone |
|
772 | - * @return EEM_Base|null (if the model was already instantiated, returns it, with |
|
773 | - * all its properties reset; if it wasn't instantiated, returns null) |
|
774 | - * @throws EE_Error |
|
775 | - * @throws ReflectionException |
|
776 | - * @throws InvalidArgumentException |
|
777 | - * @throws InvalidDataTypeException |
|
778 | - * @throws InvalidInterfaceException |
|
779 | - */ |
|
780 | - public static function reset($timezone = '') |
|
781 | - { |
|
782 | - if (static::$_instance instanceof EEM_Base) { |
|
783 | - // let's try to NOT swap out the current instance for a new one |
|
784 | - // because if someone has a reference to it, we can't remove their reference |
|
785 | - // so it's best to keep using the same reference, but change the original object |
|
786 | - // reset all its properties to their original values as defined in the class |
|
787 | - $r = new ReflectionClass(get_class(static::$_instance)); |
|
788 | - $static_properties = $r->getStaticProperties(); |
|
789 | - foreach ($r->getDefaultProperties() as $property => $value) { |
|
790 | - // don't set instance to null like it was originally, |
|
791 | - // but it's static anyways, and we're ignoring static properties (for now at least) |
|
792 | - if (! isset($static_properties[ $property ])) { |
|
793 | - static::$_instance->{$property} = $value; |
|
794 | - } |
|
795 | - } |
|
796 | - EEH_DTT_Helper::resetDefaultTimezoneString(); |
|
797 | - // and then directly call its constructor again, like we would if we were creating a new one |
|
798 | - static::$_instance->__construct( |
|
799 | - $timezone, |
|
800 | - LoaderFactory::getLoader()->load('EventEspresso\core\services\orm\ModelFieldFactory') |
|
801 | - ); |
|
802 | - return static::instance(); |
|
803 | - } |
|
804 | - return null; |
|
805 | - } |
|
806 | - |
|
807 | - |
|
808 | - /** |
|
809 | - * @return LoaderInterface |
|
810 | - * @throws InvalidArgumentException |
|
811 | - * @throws InvalidDataTypeException |
|
812 | - * @throws InvalidInterfaceException |
|
813 | - */ |
|
814 | - private static function getLoader() |
|
815 | - { |
|
816 | - if (! EEM_Base::$loader instanceof LoaderInterface) { |
|
817 | - EEM_Base::$loader = LoaderFactory::getLoader(); |
|
818 | - } |
|
819 | - return EEM_Base::$loader; |
|
820 | - } |
|
821 | - |
|
822 | - |
|
823 | - /** |
|
824 | - * retrieve the status details from esp_status table as an array IF this model has the status table as a relation. |
|
825 | - * |
|
826 | - * @param boolean $translated return localized strings or JUST the array. |
|
827 | - * @return array |
|
828 | - * @throws EE_Error |
|
829 | - * @throws InvalidArgumentException |
|
830 | - * @throws InvalidDataTypeException |
|
831 | - * @throws InvalidInterfaceException |
|
832 | - * @throws ReflectionException |
|
833 | - */ |
|
834 | - public function status_array($translated = false) |
|
835 | - { |
|
836 | - if (! array_key_exists('Status', $this->_model_relations)) { |
|
837 | - return []; |
|
838 | - } |
|
839 | - $model_name = $this->get_this_model_name(); |
|
840 | - $status_type = str_replace(' ', '_', strtolower(str_replace('_', ' ', $model_name))); |
|
841 | - $statuses = EEM_Status::instance()->get_all([['STS_type' => $status_type]]); |
|
842 | - $status_array = []; |
|
843 | - foreach ($statuses as $status) { |
|
844 | - $status_array[ $status->ID() ] = $status->get('STS_code'); |
|
845 | - } |
|
846 | - return $translated |
|
847 | - ? EEM_Status::instance()->localized_status($status_array, false, 'sentence') |
|
848 | - : $status_array; |
|
849 | - } |
|
850 | - |
|
851 | - |
|
852 | - /** |
|
853 | - * Gets all the EE_Base_Class objects which match the $query_params, by querying the DB. |
|
854 | - * |
|
855 | - * @param array $query_params see github link below for more info |
|
856 | - * @return EE_Base_Class[] *note that there is NO option to pass the output type. If you want results different |
|
857 | - * from EE_Base_Class[], use get_all_wpdb_results(). Array keys are object |
|
858 | - * IDs (if there is a primary key on the model. if not, numerically indexed) |
|
859 | - * Some full examples: get 10 transactions which have Scottish attendees: |
|
860 | - * EEM_Transaction::instance()->get_all( array( array( |
|
861 | - * 'OR'=>array( |
|
862 | - * 'Registration.Attendee.ATT_fname'=>array('like','Mc%'), |
|
863 | - * 'Registration.Attendee.ATT_fname*other'=>array('like','Mac%') |
|
864 | - * ) |
|
865 | - * ), |
|
866 | - * 'limit'=>10, |
|
867 | - * 'group_by'=>'TXN_ID' |
|
868 | - * )); |
|
869 | - * get all the answers to the question titled "shirt size" for event with id |
|
870 | - * 12, ordered by their answer EEM_Answer::instance()->get_all(array( array( |
|
871 | - * 'Question.QST_display_text'=>'shirt size', |
|
872 | - * 'Registration.Event.EVT_ID'=>12 |
|
873 | - * ), |
|
874 | - * 'order_by'=>array('ANS_value'=>'ASC') |
|
875 | - * )); |
|
876 | - * @throws EE_Error |
|
877 | - * @throws ReflectionException |
|
878 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
879 | - * or if you have the development copy of EE you can view this at the path: |
|
880 | - * /docs/G--Model-System/model-query-params.md |
|
881 | - */ |
|
882 | - public function get_all($query_params = []) |
|
883 | - { |
|
884 | - if ( |
|
885 | - isset($query_params['limit']) |
|
886 | - && ! isset($query_params['group_by']) |
|
887 | - ) { |
|
888 | - $query_params['group_by'] = array_keys($this->get_combined_primary_key_fields()); |
|
889 | - } |
|
890 | - return $this->_create_objects($this->_get_all_wpdb_results($query_params)); |
|
891 | - } |
|
892 | - |
|
893 | - |
|
894 | - /** |
|
895 | - * Modifies the query parameters so we only get back model objects |
|
896 | - * that "belong" to the current user |
|
897 | - * |
|
898 | - * @param array $query_params see github link below for more info |
|
899 | - * @return array |
|
900 | - * @throws ReflectionException |
|
901 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
902 | - */ |
|
903 | - public function alter_query_params_to_only_include_mine($query_params = []) |
|
904 | - { |
|
905 | - $wp_user_field_name = $this->wp_user_field_name(); |
|
906 | - if ($wp_user_field_name) { |
|
907 | - $query_params[0][ $wp_user_field_name ] = get_current_user_id(); |
|
908 | - } |
|
909 | - return $query_params; |
|
910 | - } |
|
911 | - |
|
912 | - |
|
913 | - /** |
|
914 | - * Returns the name of the field's name that points to the WP_User table |
|
915 | - * on this model (or follows the _model_chain_to_wp_user and uses that model's |
|
916 | - * foreign key to the WP_User table) |
|
917 | - * |
|
918 | - * @return string|boolean string on success, boolean false when there is no |
|
919 | - * foreign key to the WP_User table |
|
920 | - * @throws ReflectionException |
|
921 | - */ |
|
922 | - public function wp_user_field_name() |
|
923 | - { |
|
924 | - try { |
|
925 | - if (! empty($this->_model_chain_to_wp_user)) { |
|
926 | - $models_to_follow_to_wp_users = explode('.', $this->_model_chain_to_wp_user); |
|
927 | - $last_model_name = end($models_to_follow_to_wp_users); |
|
928 | - $model_with_fk_to_wp_users = EE_Registry::instance()->load_model($last_model_name); |
|
929 | - $model_chain_to_wp_user = $this->_model_chain_to_wp_user . '.'; |
|
930 | - } else { |
|
931 | - $model_with_fk_to_wp_users = $this; |
|
932 | - $model_chain_to_wp_user = ''; |
|
933 | - } |
|
934 | - $wp_user_field = $model_with_fk_to_wp_users->get_foreign_key_to('WP_User'); |
|
935 | - return $model_chain_to_wp_user . $wp_user_field->get_name(); |
|
936 | - } catch (EE_Error $e) { |
|
937 | - return false; |
|
938 | - } |
|
939 | - } |
|
940 | - |
|
941 | - |
|
942 | - /** |
|
943 | - * Returns the _model_chain_to_wp_user string, which indicates which related model |
|
944 | - * (or transiently-related model) has a foreign key to the wp_users table; |
|
945 | - * useful for finding if model objects of this type are 'owned' by the current user. |
|
946 | - * This is an empty string when the foreign key is on this model and when it isn't, |
|
947 | - * but is only non-empty when this model's ownership is indicated by a RELATED model |
|
948 | - * (or transiently-related model) |
|
949 | - * |
|
950 | - * @return string |
|
951 | - */ |
|
952 | - public function model_chain_to_wp_user() |
|
953 | - { |
|
954 | - return $this->_model_chain_to_wp_user; |
|
955 | - } |
|
956 | - |
|
957 | - |
|
958 | - /** |
|
959 | - * Whether this model is 'owned' by a specific wordpress user (even indirectly, |
|
960 | - * like how registrations don't have a foreign key to wp_users, but the |
|
961 | - * events they are for are), or is unrelated to wp users. |
|
962 | - * generally available |
|
963 | - * |
|
964 | - * @return boolean |
|
965 | - */ |
|
966 | - public function is_owned() |
|
967 | - { |
|
968 | - if ($this->model_chain_to_wp_user()) { |
|
969 | - return true; |
|
970 | - } |
|
971 | - try { |
|
972 | - $this->get_foreign_key_to('WP_User'); |
|
973 | - return true; |
|
974 | - } catch (EE_Error $e) { |
|
975 | - return false; |
|
976 | - } |
|
977 | - } |
|
978 | - |
|
979 | - |
|
980 | - /** |
|
981 | - * Used internally to get WPDB results, because other functions, besides get_all, may want to do some queries, but |
|
982 | - * may want to preserve the WPDB results (eg, update, which first queries to make sure we have all the tables on |
|
983 | - * the model) |
|
984 | - * |
|
985 | - * @param array $query_params |
|
986 | - * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
987 | - * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
988 | - * fields on the model, and the models we joined to in the query. However, you can |
|
989 | - * override this and set the select to "*", or a specific column name, like |
|
990 | - * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
991 | - * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
992 | - * the aliases used to refer to this selection, and values are to be |
|
993 | - * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
994 | - * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
995 | - * @return array | stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
996 | - * @throws EE_Error |
|
997 | - * @throws InvalidArgumentException |
|
998 | - * @throws ReflectionException |
|
999 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1000 | - */ |
|
1001 | - protected function _get_all_wpdb_results($query_params = [], $output = ARRAY_A, $columns_to_select = null) |
|
1002 | - { |
|
1003 | - $this->_custom_selections = $this->getCustomSelection($query_params, $columns_to_select); |
|
1004 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
1005 | - $select_expressions = $columns_to_select === null |
|
1006 | - ? $this->_construct_default_select_sql($model_query_info) |
|
1007 | - : ''; |
|
1008 | - if ($this->_custom_selections instanceof CustomSelects) { |
|
1009 | - $custom_expressions = $this->_custom_selections->columnsToSelectExpression(); |
|
1010 | - $select_expressions .= $select_expressions |
|
1011 | - ? ', ' . $custom_expressions |
|
1012 | - : $custom_expressions; |
|
1013 | - } |
|
1014 | - |
|
1015 | - $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
1016 | - return $this->_do_wpdb_query('get_results', [$SQL, $output]); |
|
1017 | - } |
|
1018 | - |
|
1019 | - |
|
1020 | - /** |
|
1021 | - * Get a CustomSelects object if the $query_params or $columns_to_select allows for it. |
|
1022 | - * Note: $query_params['extra_selects'] will always override any $columns_to_select values. It is the preferred |
|
1023 | - * method of including extra select information. |
|
1024 | - * |
|
1025 | - * @param array $query_params |
|
1026 | - * @param null|array|string $columns_to_select |
|
1027 | - * @return null|CustomSelects |
|
1028 | - * @throws InvalidArgumentException |
|
1029 | - */ |
|
1030 | - protected function getCustomSelection(array $query_params, $columns_to_select = null) |
|
1031 | - { |
|
1032 | - if (! isset($query_params['extra_selects']) && $columns_to_select === null) { |
|
1033 | - return null; |
|
1034 | - } |
|
1035 | - $selects = isset($query_params['extra_selects']) ? $query_params['extra_selects'] : $columns_to_select; |
|
1036 | - $selects = is_string($selects) ? explode(',', $selects) : $selects; |
|
1037 | - return new CustomSelects($selects); |
|
1038 | - } |
|
1039 | - |
|
1040 | - |
|
1041 | - /** |
|
1042 | - * Gets an array of rows from the database just like $wpdb->get_results would, |
|
1043 | - * but you can use the model query params to more easily |
|
1044 | - * take care of joins, field preparation etc. |
|
1045 | - * |
|
1046 | - * @param array $query_params |
|
1047 | - * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
1048 | - * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
1049 | - * fields on the model, and the models we joined to in the query. However, you can |
|
1050 | - * override this and set the select to "*", or a specific column name, like |
|
1051 | - * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
1052 | - * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
1053 | - * the aliases used to refer to this selection, and values are to be |
|
1054 | - * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
1055 | - * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
1056 | - * @return array|stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
1057 | - * @throws EE_Error |
|
1058 | - * @throws ReflectionException |
|
1059 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1060 | - */ |
|
1061 | - public function get_all_wpdb_results($query_params = [], $output = ARRAY_A, $columns_to_select = null) |
|
1062 | - { |
|
1063 | - return $this->_get_all_wpdb_results($query_params, $output, $columns_to_select); |
|
1064 | - } |
|
1065 | - |
|
1066 | - |
|
1067 | - /** |
|
1068 | - * For creating a custom select statement |
|
1069 | - * |
|
1070 | - * @param array|string $columns_to_select either a string to be inserted directly as the select statement, |
|
1071 | - * or an array where keys are aliases, and values are arrays where 0=>the |
|
1072 | - * selection SQL, and 1=>is the datatype |
|
1073 | - * @return string |
|
1074 | - * @throws EE_Error |
|
1075 | - */ |
|
1076 | - private function _construct_select_from_input($columns_to_select) |
|
1077 | - { |
|
1078 | - if (is_array($columns_to_select)) { |
|
1079 | - $select_sql_array = []; |
|
1080 | - foreach ($columns_to_select as $alias => $selection_and_datatype) { |
|
1081 | - if (! is_array($selection_and_datatype) || ! isset($selection_and_datatype[1])) { |
|
1082 | - throw new EE_Error( |
|
1083 | - sprintf( |
|
1084 | - esc_html__( |
|
1085 | - "Custom selection %s (alias %s) needs to be an array like array('COUNT(REG_ID)','%%d')", |
|
1086 | - 'event_espresso' |
|
1087 | - ), |
|
1088 | - $selection_and_datatype, |
|
1089 | - $alias |
|
1090 | - ) |
|
1091 | - ); |
|
1092 | - } |
|
1093 | - if (! in_array($selection_and_datatype[1], $this->_valid_wpdb_data_types, true)) { |
|
1094 | - throw new EE_Error( |
|
1095 | - sprintf( |
|
1096 | - esc_html__( |
|
1097 | - "Datatype %s (for selection '%s' and alias '%s') is not a valid wpdb datatype (eg %%s)", |
|
1098 | - 'event_espresso' |
|
1099 | - ), |
|
1100 | - $selection_and_datatype[1], |
|
1101 | - $selection_and_datatype[0], |
|
1102 | - $alias, |
|
1103 | - implode(', ', $this->_valid_wpdb_data_types) |
|
1104 | - ) |
|
1105 | - ); |
|
1106 | - } |
|
1107 | - $select_sql_array[] = "{$selection_and_datatype[0]} AS $alias"; |
|
1108 | - } |
|
1109 | - $columns_to_select_string = implode(', ', $select_sql_array); |
|
1110 | - } else { |
|
1111 | - $columns_to_select_string = $columns_to_select; |
|
1112 | - } |
|
1113 | - return $columns_to_select_string; |
|
1114 | - } |
|
1115 | - |
|
1116 | - |
|
1117 | - /** |
|
1118 | - * Convenient wrapper for getting the primary key field's name. Eg, on Registration, this would be 'REG_ID' |
|
1119 | - * |
|
1120 | - * @return string |
|
1121 | - * @throws EE_Error |
|
1122 | - */ |
|
1123 | - public function primary_key_name() |
|
1124 | - { |
|
1125 | - return $this->get_primary_key_field()->get_name(); |
|
1126 | - } |
|
1127 | - |
|
1128 | - |
|
1129 | - /** |
|
1130 | - * Gets a single item for this model from the DB, given only its ID (or null if none is found). |
|
1131 | - * If there is no primary key on this model, $id is treated as primary key string |
|
1132 | - * |
|
1133 | - * @param mixed $id int or string, depending on the type of the model's primary key |
|
1134 | - * @return EE_Base_Class |
|
1135 | - * @throws EE_Error |
|
1136 | - * @throws ReflectionException |
|
1137 | - */ |
|
1138 | - public function get_one_by_ID($id) |
|
1139 | - { |
|
1140 | - if ($this->get_from_entity_map($id)) { |
|
1141 | - return $this->get_from_entity_map($id); |
|
1142 | - } |
|
1143 | - $model_object = $this->get_one( |
|
1144 | - $this->alter_query_params_to_restrict_by_ID( |
|
1145 | - $id, |
|
1146 | - ['default_where_conditions' => EEM_Base::default_where_conditions_minimum_all] |
|
1147 | - ) |
|
1148 | - ); |
|
1149 | - $className = $this->_get_class_name(); |
|
1150 | - if ($model_object instanceof $className) { |
|
1151 | - // make sure valid objects get added to the entity map |
|
1152 | - // so that the next call to this method doesn't trigger another trip to the db |
|
1153 | - $this->add_to_entity_map($model_object); |
|
1154 | - } |
|
1155 | - return $model_object; |
|
1156 | - } |
|
1157 | - |
|
1158 | - |
|
1159 | - /** |
|
1160 | - * Alters query parameters to only get items with this ID are returned. |
|
1161 | - * Takes into account that the ID might be a string produced by EEM_Base::get_index_primary_key_string(), |
|
1162 | - * or could just be a simple primary key ID |
|
1163 | - * |
|
1164 | - * @param int $id |
|
1165 | - * @param array $query_params see github link below for more info |
|
1166 | - * @return array of normal query params, |
|
1167 | - * @throws EE_Error |
|
1168 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1169 | - */ |
|
1170 | - public function alter_query_params_to_restrict_by_ID($id, $query_params = []) |
|
1171 | - { |
|
1172 | - if (! isset($query_params[0])) { |
|
1173 | - $query_params[0] = []; |
|
1174 | - } |
|
1175 | - $conditions_from_id = $this->parse_index_primary_key_string($id); |
|
1176 | - if ($conditions_from_id === null) { |
|
1177 | - $query_params[0][ $this->primary_key_name() ] = $id; |
|
1178 | - } else { |
|
1179 | - // no primary key, so the $id must be from the get_index_primary_key_string() |
|
1180 | - $query_params[0] = array_replace_recursive($query_params[0], $this->parse_index_primary_key_string($id)); |
|
1181 | - } |
|
1182 | - return $query_params; |
|
1183 | - } |
|
1184 | - |
|
1185 | - |
|
1186 | - /** |
|
1187 | - * Gets a single item for this model from the DB, given the $query_params. Only returns a single class, not an |
|
1188 | - * array. If no item is found, null is returned. |
|
1189 | - * |
|
1190 | - * @param array $query_params like EEM_Base's $query_params variable. |
|
1191 | - * @return EE_Base_Class|null |
|
1192 | - * @throws EE_Error |
|
1193 | - * @throws ReflectionException |
|
1194 | - */ |
|
1195 | - public function get_one(array $query_params = []) |
|
1196 | - { |
|
1197 | - if (! is_array($query_params)) { |
|
1198 | - EE_Error::doing_it_wrong( |
|
1199 | - 'EEM_Base::get_one', |
|
1200 | - sprintf( |
|
1201 | - esc_html__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
1202 | - gettype($query_params) |
|
1203 | - ), |
|
1204 | - '4.6.0' |
|
1205 | - ); |
|
1206 | - $query_params = []; |
|
1207 | - } |
|
1208 | - $query_params['limit'] = 1; |
|
1209 | - $items = $this->get_all($query_params); |
|
1210 | - if (empty($items)) { |
|
1211 | - return null; |
|
1212 | - } |
|
1213 | - return array_shift($items); |
|
1214 | - } |
|
1215 | - |
|
1216 | - |
|
1217 | - /** |
|
1218 | - * Returns the next x number of items in sequence from the given value as |
|
1219 | - * found in the database matching the given query conditions. |
|
1220 | - * |
|
1221 | - * @param mixed $current_field_value Value used for the reference point. |
|
1222 | - * @param null $field_to_order_by What field is used for the |
|
1223 | - * reference point. |
|
1224 | - * @param int $limit How many to return. |
|
1225 | - * @param array $query_params Extra conditions on the query. |
|
1226 | - * @param null $columns_to_select If left null, then an array of |
|
1227 | - * EE_Base_Class objects is returned, |
|
1228 | - * otherwise you can indicate just the |
|
1229 | - * columns you want returned. |
|
1230 | - * @return EE_Base_Class[]|array |
|
1231 | - * @throws EE_Error |
|
1232 | - * @throws ReflectionException |
|
1233 | - */ |
|
1234 | - public function next_x( |
|
1235 | - $current_field_value, |
|
1236 | - $field_to_order_by = null, |
|
1237 | - $limit = 1, |
|
1238 | - $query_params = [], |
|
1239 | - $columns_to_select = null |
|
1240 | - ) { |
|
1241 | - return $this->_get_consecutive( |
|
1242 | - $current_field_value, |
|
1243 | - '>', |
|
1244 | - $field_to_order_by, |
|
1245 | - $limit, |
|
1246 | - $query_params, |
|
1247 | - $columns_to_select |
|
1248 | - ); |
|
1249 | - } |
|
1250 | - |
|
1251 | - |
|
1252 | - /** |
|
1253 | - * Returns the previous x number of items in sequence from the given value |
|
1254 | - * as found in the database matching the given query conditions. |
|
1255 | - * |
|
1256 | - * @param mixed $current_field_value Value used for the reference point. |
|
1257 | - * @param null $field_to_order_by What field is used for the |
|
1258 | - * reference point. |
|
1259 | - * @param int $limit How many to return. |
|
1260 | - * @param array $query_params Extra conditions on the query. |
|
1261 | - * @param null $columns_to_select If left null, then an array of |
|
1262 | - * EE_Base_Class objects is returned, |
|
1263 | - * otherwise you can indicate just the |
|
1264 | - * columns you want returned. |
|
1265 | - * @return EE_Base_Class[]|array |
|
1266 | - * @throws EE_Error |
|
1267 | - * @throws ReflectionException |
|
1268 | - */ |
|
1269 | - public function previous_x( |
|
1270 | - $current_field_value, |
|
1271 | - $field_to_order_by = null, |
|
1272 | - $limit = 1, |
|
1273 | - $query_params = [], |
|
1274 | - $columns_to_select = null |
|
1275 | - ) { |
|
1276 | - return $this->_get_consecutive( |
|
1277 | - $current_field_value, |
|
1278 | - '<', |
|
1279 | - $field_to_order_by, |
|
1280 | - $limit, |
|
1281 | - $query_params, |
|
1282 | - $columns_to_select |
|
1283 | - ); |
|
1284 | - } |
|
1285 | - |
|
1286 | - |
|
1287 | - /** |
|
1288 | - * Returns the next item in sequence from the given value as found in the |
|
1289 | - * database matching the given query conditions. |
|
1290 | - * |
|
1291 | - * @param mixed $current_field_value Value used for the reference point. |
|
1292 | - * @param null $field_to_order_by What field is used for the |
|
1293 | - * reference point. |
|
1294 | - * @param array $query_params Extra conditions on the query. |
|
1295 | - * @param null $columns_to_select If left null, then an EE_Base_Class |
|
1296 | - * object is returned, otherwise you |
|
1297 | - * can indicate just the columns you |
|
1298 | - * want and a single array indexed by |
|
1299 | - * the columns will be returned. |
|
1300 | - * @return EE_Base_Class|null|array() |
|
1301 | - * @throws EE_Error |
|
1302 | - * @throws ReflectionException |
|
1303 | - */ |
|
1304 | - public function next( |
|
1305 | - $current_field_value, |
|
1306 | - $field_to_order_by = null, |
|
1307 | - $query_params = [], |
|
1308 | - $columns_to_select = null |
|
1309 | - ) { |
|
1310 | - $results = $this->_get_consecutive( |
|
1311 | - $current_field_value, |
|
1312 | - '>', |
|
1313 | - $field_to_order_by, |
|
1314 | - 1, |
|
1315 | - $query_params, |
|
1316 | - $columns_to_select |
|
1317 | - ); |
|
1318 | - return empty($results) ? null : reset($results); |
|
1319 | - } |
|
1320 | - |
|
1321 | - |
|
1322 | - /** |
|
1323 | - * Returns the previous item in sequence from the given value as found in |
|
1324 | - * the database matching the given query conditions. |
|
1325 | - * |
|
1326 | - * @param mixed $current_field_value Value used for the reference point. |
|
1327 | - * @param null $field_to_order_by What field is used for the |
|
1328 | - * reference point. |
|
1329 | - * @param array $query_params Extra conditions on the query. |
|
1330 | - * @param null $columns_to_select If left null, then an EE_Base_Class |
|
1331 | - * object is returned, otherwise you |
|
1332 | - * can indicate just the columns you |
|
1333 | - * want and a single array indexed by |
|
1334 | - * the columns will be returned. |
|
1335 | - * @return EE_Base_Class|null|array() |
|
1336 | - * @throws EE_Error |
|
1337 | - * @throws ReflectionException |
|
1338 | - */ |
|
1339 | - public function previous( |
|
1340 | - $current_field_value, |
|
1341 | - $field_to_order_by = null, |
|
1342 | - $query_params = [], |
|
1343 | - $columns_to_select = null |
|
1344 | - ) { |
|
1345 | - $results = $this->_get_consecutive( |
|
1346 | - $current_field_value, |
|
1347 | - '<', |
|
1348 | - $field_to_order_by, |
|
1349 | - 1, |
|
1350 | - $query_params, |
|
1351 | - $columns_to_select |
|
1352 | - ); |
|
1353 | - return empty($results) ? null : reset($results); |
|
1354 | - } |
|
1355 | - |
|
1356 | - |
|
1357 | - /** |
|
1358 | - * Returns the a consecutive number of items in sequence from the given |
|
1359 | - * value as found in the database matching the given query conditions. |
|
1360 | - * |
|
1361 | - * @param mixed $current_field_value Value used for the reference point. |
|
1362 | - * @param string $operand What operand is used for the sequence. |
|
1363 | - * @param string $field_to_order_by What field is used for the reference point. |
|
1364 | - * @param int $limit How many to return. |
|
1365 | - * @param array $query_params Extra conditions on the query. |
|
1366 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, |
|
1367 | - * otherwise you can indicate just the columns you want returned. |
|
1368 | - * @return EE_Base_Class[]|array |
|
1369 | - * @throws EE_Error |
|
1370 | - * @throws ReflectionException |
|
1371 | - */ |
|
1372 | - protected function _get_consecutive( |
|
1373 | - $current_field_value, |
|
1374 | - $operand = '>', |
|
1375 | - $field_to_order_by = null, |
|
1376 | - $limit = 1, |
|
1377 | - $query_params = [], |
|
1378 | - $columns_to_select = null |
|
1379 | - ) { |
|
1380 | - // if $field_to_order_by is empty then let's assume we're ordering by the primary key. |
|
1381 | - if (empty($field_to_order_by)) { |
|
1382 | - if ($this->has_primary_key_field()) { |
|
1383 | - $field_to_order_by = $this->get_primary_key_field()->get_name(); |
|
1384 | - } else { |
|
1385 | - if (WP_DEBUG) { |
|
1386 | - throw new EE_Error( |
|
1387 | - esc_html__( |
|
1388 | - 'EEM_Base::_get_consecutive() has been called with no $field_to_order_by argument and there is no primary key on the field. Please provide the field you would like to use as the base for retrieving the next item(s).', |
|
1389 | - 'event_espresso' |
|
1390 | - ) |
|
1391 | - ); |
|
1392 | - } |
|
1393 | - EE_Error::add_error(__('There was an error with the query.', 'event_espresso')); |
|
1394 | - return []; |
|
1395 | - } |
|
1396 | - } |
|
1397 | - if (! is_array($query_params)) { |
|
1398 | - EE_Error::doing_it_wrong( |
|
1399 | - 'EEM_Base::_get_consecutive', |
|
1400 | - sprintf( |
|
1401 | - esc_html__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
1402 | - gettype($query_params) |
|
1403 | - ), |
|
1404 | - '4.6.0' |
|
1405 | - ); |
|
1406 | - $query_params = []; |
|
1407 | - } |
|
1408 | - // let's add the where query param for consecutive look up. |
|
1409 | - $query_params[0][ $field_to_order_by ] = [$operand, $current_field_value]; |
|
1410 | - $query_params['limit'] = $limit; |
|
1411 | - // set direction |
|
1412 | - $incoming_orderby = isset($query_params['order_by']) ? (array) $query_params['order_by'] : []; |
|
1413 | - $query_params['order_by'] = $operand === '>' |
|
1414 | - ? [$field_to_order_by => 'ASC'] + $incoming_orderby |
|
1415 | - : [$field_to_order_by => 'DESC'] + $incoming_orderby; |
|
1416 | - // if $columns_to_select is empty then that means we're returning EE_Base_Class objects |
|
1417 | - if (empty($columns_to_select)) { |
|
1418 | - return $this->get_all($query_params); |
|
1419 | - } |
|
1420 | - // getting just the fields |
|
1421 | - return $this->_get_all_wpdb_results($query_params, ARRAY_A, $columns_to_select); |
|
1422 | - } |
|
1423 | - |
|
1424 | - |
|
1425 | - /** |
|
1426 | - * This sets the _timezone property after model object has been instantiated. |
|
1427 | - * |
|
1428 | - * @param string|null $timezone valid PHP DateTimeZone timezone string |
|
1429 | - */ |
|
1430 | - public function set_timezone($timezone = '') |
|
1431 | - { |
|
1432 | - static $set_in_progress = false; |
|
1433 | - // if incoming timezone string is empty, then use the existing |
|
1434 | - $valid_timezone = ! empty($timezone) && $timezone !== $this->_timezone |
|
1435 | - ? EEH_DTT_Helper::get_valid_timezone_string($timezone) |
|
1436 | - : $this->_timezone; |
|
1437 | - // do NOT set the timezone if we are already in the process of setting the timezone |
|
1438 | - // OR the existing timezone is already set and the incoming value is nothing (which gets set to default TZ) |
|
1439 | - // OR the existing timezone is already set and the validated value is the same as the existing timezone |
|
1440 | - if ( |
|
1441 | - $set_in_progress |
|
1442 | - || ( |
|
1443 | - ! empty($this->_timezone) |
|
1444 | - && ( |
|
1445 | - empty($timezone) || $valid_timezone === $this->_timezone |
|
1446 | - ) |
|
1447 | - ) |
|
1448 | - ) { |
|
1449 | - return; |
|
1450 | - } |
|
1451 | - $set_in_progress = true; |
|
1452 | - $this->_timezone = $valid_timezone ?: EEH_DTT_Helper::get_valid_timezone_string(); |
|
1453 | - // note we need to loop through relations and set the timezone on those objects as well. |
|
1454 | - foreach ($this->_model_relations as $relation) { |
|
1455 | - $relation->set_timezone($this->_timezone); |
|
1456 | - } |
|
1457 | - // and finally we do the same for any datetime fields |
|
1458 | - foreach ($this->_fields as $field) { |
|
1459 | - if ($field instanceof EE_Datetime_Field) { |
|
1460 | - $field->set_timezone($this->_timezone); |
|
1461 | - } |
|
1462 | - } |
|
1463 | - $set_in_progress = false; |
|
1464 | - } |
|
1465 | - |
|
1466 | - |
|
1467 | - /** |
|
1468 | - * This just returns whatever is set for the current timezone. |
|
1469 | - * |
|
1470 | - * @access public |
|
1471 | - * @return string |
|
1472 | - */ |
|
1473 | - public function get_timezone() |
|
1474 | - { |
|
1475 | - if (empty($this->_timezone)) { |
|
1476 | - $this->set_timezone(); |
|
1477 | - } |
|
1478 | - return $this->_timezone; |
|
1479 | - } |
|
1480 | - |
|
1481 | - |
|
1482 | - /** |
|
1483 | - * This returns the date formats set for the given field name and also ensures that |
|
1484 | - * $this->_timezone property is set correctly. |
|
1485 | - * |
|
1486 | - * @param string $field_name The name of the field the formats are being retrieved for. |
|
1487 | - * @param bool $pretty Whether to return the pretty formats (true) or not (false). |
|
1488 | - * @return array formats in an array with the date format first, and the time format last. |
|
1489 | - * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
1490 | - * @since 4.6.x |
|
1491 | - */ |
|
1492 | - public function get_formats_for($field_name, $pretty = false) |
|
1493 | - { |
|
1494 | - $field_settings = $this->field_settings_for($field_name); |
|
1495 | - // if not a valid EE_Datetime_Field then throw error |
|
1496 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
1497 | - throw new EE_Error( |
|
1498 | - sprintf( |
|
1499 | - esc_html__( |
|
1500 | - 'The field sent into EEM_Base::get_formats_for (%s) is not registered as a EE_Datetime_Field. Please check the spelling and make sure you are submitting the right field name to retrieve date_formats for.', |
|
1501 | - 'event_espresso' |
|
1502 | - ), |
|
1503 | - $field_name |
|
1504 | - ) |
|
1505 | - ); |
|
1506 | - } |
|
1507 | - // while we are here, let's make sure the timezone internally in EEM_Base matches what is stored on the field. |
|
1508 | - $field_timezone = $field_settings->get_timezone(); |
|
1509 | - if (empty($this->_timezone && $field_timezone)) { |
|
1510 | - $this->set_timezone(); |
|
1511 | - } else { |
|
1512 | - // or vice versa if the field TZ isn't set |
|
1513 | - $model_timezone = $this->get_timezone(); |
|
1514 | - $field_settings->set_timezone($model_timezone); |
|
1515 | - } |
|
1516 | - |
|
1517 | - return [$field_settings->get_date_format($pretty), $field_settings->get_time_format($pretty)]; |
|
1518 | - } |
|
1519 | - |
|
1520 | - /** |
|
1521 | - * This returns the current time in a format setup for a query on this model. |
|
1522 | - * Usage of this method makes it easier to setup queries against EE_Datetime_Field columns because |
|
1523 | - * it will return: |
|
1524 | - * - a formatted string in the timezone and format currently set on the EE_Datetime_Field |
|
1525 | - * for the given field for NOW |
|
1526 | - * - or a unix timestamp (equivalent to time()) |
|
1527 | - * Note: When requesting a formatted string, if the date or time format doesn't include seconds, for example, |
|
1528 | - * the time returned, because it uses that format, will also NOT include seconds. For this reason, if you want |
|
1529 | - * the time returned to be the current time down to the exact second, set $timestamp to true. |
|
1530 | - * |
|
1531 | - * @param string $field_name The field the current time is needed for. |
|
1532 | - * @param bool $timestamp True means to return a unix timestamp. Otherwise a |
|
1533 | - * formatted string matching the set format for the field in the set timezone will |
|
1534 | - * be returned. |
|
1535 | - * @param string $what Whether to return the string in just the time format, the date format, or both. |
|
1536 | - * @return int|string If the given field_name is not of the EE_Datetime_Field type, then an EE_Error |
|
1537 | - * exception is triggered. |
|
1538 | - * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
1539 | - * @throws Exception |
|
1540 | - * @since 4.6.x |
|
1541 | - */ |
|
1542 | - public function current_time_for_query($field_name, $timestamp = false, $what = 'both') |
|
1543 | - { |
|
1544 | - $formats = $this->get_formats_for($field_name); |
|
1545 | - $DateTime = new DateTime("now", new DateTimeZone($this->get_timezone())); |
|
1546 | - if ($timestamp) { |
|
1547 | - return $DateTime->format('U'); |
|
1548 | - } |
|
1549 | - // not returning timestamp, so return formatted string in timezone. |
|
1550 | - switch ($what) { |
|
1551 | - case 'time': |
|
1552 | - return $DateTime->format($formats[1]); |
|
1553 | - case 'date': |
|
1554 | - return $DateTime->format($formats[0]); |
|
1555 | - default: |
|
1556 | - return $DateTime->format(implode(' ', $formats)); |
|
1557 | - } |
|
1558 | - } |
|
1559 | - |
|
1560 | - |
|
1561 | - /** |
|
1562 | - * This receives a time string for a given field and ensures that it is setup to match what the internal settings |
|
1563 | - * for the model are. Returns a DateTime object. |
|
1564 | - * Note: a gotcha for when you send in unix timestamp. Remember a unix timestamp is already timezone agnostic, |
|
1565 | - * (functionally the equivalent of UTC+0). So when you send it in, whatever timezone string you include is |
|
1566 | - * ignored. |
|
1567 | - * |
|
1568 | - * @param string $field_name The field being setup. |
|
1569 | - * @param string $timestring The date time string being used. |
|
1570 | - * @param string $format The format for the time string. |
|
1571 | - * @param string $timezone By default, it is assumed the incoming time string is in timezone for |
|
1572 | - * the blog. If this is not the case, then it can be specified here. |
|
1573 | - * If incoming format is 'U', this is ignored. |
|
1574 | - * @return DbSafeDateTime |
|
1575 | - * @throws EE_Error |
|
1576 | - * @throws Exception |
|
1577 | - */ |
|
1578 | - public function convert_datetime_for_query($field_name, $timestring, $format, $timezone = '') |
|
1579 | - { |
|
1580 | - // just using this to ensure the timezone is set correctly internally |
|
1581 | - $this->get_formats_for($field_name); |
|
1582 | - $timezone = ! empty($timezone) ? $timezone : EEH_DTT_Helper::get_timezone(); |
|
1583 | - $db_safe_datetime = DbSafeDateTime::createFromFormat($format, $timestring, new DateTimeZone($timezone)); |
|
1584 | - EEH_DTT_Helper::setTimezone($db_safe_datetime, new DateTimeZone($this->get_timezone())); |
|
1585 | - return $db_safe_datetime; |
|
1586 | - } |
|
1587 | - |
|
1588 | - |
|
1589 | - /** |
|
1590 | - * Gets all the tables comprising this model. Array keys are the table aliases, and values are EE_Table objects |
|
1591 | - * |
|
1592 | - * @return EE_Table_Base[] |
|
1593 | - */ |
|
1594 | - public function get_tables() |
|
1595 | - { |
|
1596 | - return $this->_tables; |
|
1597 | - } |
|
1598 | - |
|
1599 | - |
|
1600 | - /** |
|
1601 | - * Updates all the database entries (in each table for this model) according to $fields_n_values and optionally |
|
1602 | - * also updates all the model objects, where the criteria expressed in $query_params are met.. |
|
1603 | - * Also note: if this model has multiple tables, this update verifies all the secondary tables have an entry for |
|
1604 | - * each row (in the primary table) we're trying to update; if not, it inserts an entry in the secondary table. Eg: |
|
1605 | - * if our model has 2 tables: wp_posts (primary), and wp_esp_event (secondary). Let's say we are trying to update a |
|
1606 | - * model object with EVT_ID = 1 |
|
1607 | - * (which means where wp_posts has ID = 1, because wp_posts.ID is the primary key's column), which exists, but |
|
1608 | - * there is no entry in wp_esp_event for this entry in wp_posts. So, this update script will insert a row into |
|
1609 | - * wp_esp_event, using any available parameters from $fields_n_values (eg, if "EVT_limit" => 40 is in |
|
1610 | - * $fields_n_values, the new entry in wp_esp_event will set EVT_limit = 40, and use default for other columns which |
|
1611 | - * are not specified) |
|
1612 | - * |
|
1613 | - * @param array $fields_n_values keys are model fields (exactly like keys in EEM_Base::_fields, NOT db |
|
1614 | - * columns!), values are strings, integers, floats, and maybe arrays if |
|
1615 | - * they |
|
1616 | - * are to be serialized. Basically, the values are what you'd expect to be |
|
1617 | - * values on the model, NOT necessarily what's in the DB. For example, if |
|
1618 | - * we wanted to update only the TXN_details on any Transactions where its |
|
1619 | - * ID=34, we'd use this method as follows: |
|
1620 | - * EEM_Transaction::instance()->update( |
|
1621 | - * array('TXN_details'=>array('detail1'=>'monkey','detail2'=>'banana'), |
|
1622 | - * array(array('TXN_ID'=>34))); |
|
1623 | - * @param array $query_params Eg, consider updating Question's QST_admin_label field is of type |
|
1624 | - * Simple_HTML. If you use this function to update that field to $new_value |
|
1625 | - * = (note replace 8's with appropriate opening and closing tags in the |
|
1626 | - * following example)"8script8alert('I hack all');8/script88b8boom |
|
1627 | - * baby8/b8", then if you set $values_already_prepared_by_model_object to |
|
1628 | - * TRUE, it is assumed that you've already called |
|
1629 | - * EE_Simple_HTML_Field->prepare_for_set($new_value), which removes the |
|
1630 | - * malicious javascript. However, if |
|
1631 | - * $values_already_prepared_by_model_object is left as FALSE, then |
|
1632 | - * EE_Simple_HTML_Field->prepare_for_set($new_value) will be called on it, |
|
1633 | - * and every other field, before insertion. We provide this parameter |
|
1634 | - * because model objects perform their prepare_for_set function on all |
|
1635 | - * their values, and so don't need to be called again (and in many cases, |
|
1636 | - * shouldn't be called again. Eg: if we escape HTML characters in the |
|
1637 | - * prepare_for_set method...) |
|
1638 | - * @param boolean $keep_model_objs_in_sync if TRUE, makes sure we ALSO update model objects |
|
1639 | - * in this model's entity map according to $fields_n_values that match |
|
1640 | - * $query_params. This obviously has some overhead, so you can disable it |
|
1641 | - * by setting this to FALSE, but be aware that model objects being used |
|
1642 | - * could get out-of-sync with the database |
|
1643 | - * @return int how many rows got updated or FALSE if something went wrong with the query (wp returns FALSE or num |
|
1644 | - * rows affected which *could* include 0 which DOES NOT mean the query was |
|
1645 | - * bad) |
|
1646 | - * @throws EE_Error |
|
1647 | - * @throws ReflectionException |
|
1648 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1649 | - */ |
|
1650 | - public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true) |
|
1651 | - { |
|
1652 | - if (! is_array($query_params)) { |
|
1653 | - EE_Error::doing_it_wrong( |
|
1654 | - 'EEM_Base::update', |
|
1655 | - sprintf( |
|
1656 | - esc_html__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
1657 | - gettype($query_params) |
|
1658 | - ), |
|
1659 | - '4.6.0' |
|
1660 | - ); |
|
1661 | - $query_params = []; |
|
1662 | - } |
|
1663 | - /** |
|
1664 | - * Action called before a model update call has been made. |
|
1665 | - * |
|
1666 | - * @param EEM_Base $model |
|
1667 | - * @param array $fields_n_values the updated fields and their new values |
|
1668 | - * @param array $query_params |
|
1669 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
1670 | - */ |
|
1671 | - do_action('AHEE__EEM_Base__update__begin', $this, $fields_n_values, $query_params); |
|
1672 | - /** |
|
1673 | - * Filters the fields about to be updated given the query parameters. You can provide the |
|
1674 | - * $query_params to $this->get_all() to find exactly which records will be updated |
|
1675 | - * |
|
1676 | - * @param array $fields_n_values fields and their new values |
|
1677 | - * @param EEM_Base $model the model being queried |
|
1678 | - * @param array $query_params |
|
1679 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
1680 | - */ |
|
1681 | - $fields_n_values = (array) apply_filters( |
|
1682 | - 'FHEE__EEM_Base__update__fields_n_values', |
|
1683 | - $fields_n_values, |
|
1684 | - $this, |
|
1685 | - $query_params |
|
1686 | - ); |
|
1687 | - // need to verify that, for any entry we want to update, there are entries in each secondary table. |
|
1688 | - // to do that, for each table, verify that it's PK isn't null. |
|
1689 | - $tables = $this->get_tables(); |
|
1690 | - // and if the other tables don't have a row for each table-to-be-updated, we'll insert one with whatever values available in the current update query |
|
1691 | - // NOTE: we should make this code more efficient by NOT querying twice |
|
1692 | - // before the real update, but that needs to first go through ALPHA testing |
|
1693 | - // as it's dangerous. says Mike August 8 2014 |
|
1694 | - // we want to make sure the default_where strategy is ignored |
|
1695 | - $this->_ignore_where_strategy = true; |
|
1696 | - $wpdb_select_results = $this->_get_all_wpdb_results($query_params); |
|
1697 | - foreach ($wpdb_select_results as $wpdb_result) { |
|
1698 | - // type cast stdClass as array |
|
1699 | - $wpdb_result = (array) $wpdb_result; |
|
1700 | - // get the model object's PK, as we'll want this if we need to insert a row into secondary tables |
|
1701 | - if ($this->has_primary_key_field()) { |
|
1702 | - $main_table_pk_value = $wpdb_result[ $this->get_primary_key_field()->get_qualified_column() ]; |
|
1703 | - } else { |
|
1704 | - // if there's no primary key, we basically can't support having a 2nd table on the model (we could but it would be lots of work) |
|
1705 | - $main_table_pk_value = null; |
|
1706 | - } |
|
1707 | - // if there are more than 1 tables, we'll want to verify that each table for this model has an entry in the other tables |
|
1708 | - // and if the other tables don't have a row for each table-to-be-updated, we'll insert one with whatever values available in the current update query |
|
1709 | - if (count($tables) > 1) { |
|
1710 | - // foreach matching row in the DB, ensure that each table's PK isn't null. If so, there must not be an entry |
|
1711 | - // in that table, and so we'll want to insert one |
|
1712 | - foreach ($tables as $table_obj) { |
|
1713 | - $this_table_pk_column = $table_obj->get_fully_qualified_pk_column(); |
|
1714 | - // if there is no private key for this table on the results, it means there's no entry |
|
1715 | - // in this table, right? so insert a row in the current table, using any fields available |
|
1716 | - if ( |
|
1717 | - ! (array_key_exists($this_table_pk_column, $wpdb_result) |
|
1718 | - && $wpdb_result[ $this_table_pk_column ]) |
|
1719 | - ) { |
|
1720 | - $success = $this->_insert_into_specific_table( |
|
1721 | - $table_obj, |
|
1722 | - $fields_n_values, |
|
1723 | - $main_table_pk_value |
|
1724 | - ); |
|
1725 | - // if we died here, report the error |
|
1726 | - if (! $success) { |
|
1727 | - return false; |
|
1728 | - } |
|
1729 | - } |
|
1730 | - } |
|
1731 | - } |
|
1732 | - // let's make sure default_where strategy is followed now |
|
1733 | - $this->_ignore_where_strategy = false; |
|
1734 | - } |
|
1735 | - // if we want to keep model objects in sync, AND |
|
1736 | - // if this wasn't called from a model object (to update itself) |
|
1737 | - // then we want to make sure we keep all the existing |
|
1738 | - // model objects in sync with the db |
|
1739 | - if ($keep_model_objs_in_sync && ! $this->_values_already_prepared_by_model_object) { |
|
1740 | - if ($this->has_primary_key_field()) { |
|
1741 | - $model_objs_affected_ids = $this->get_col($query_params); |
|
1742 | - } else { |
|
1743 | - // we need to select a bunch of columns and then combine them into the the "index primary key string"s |
|
1744 | - $models_affected_key_columns = $this->_get_all_wpdb_results($query_params); |
|
1745 | - $model_objs_affected_ids = []; |
|
1746 | - foreach ($models_affected_key_columns as $row) { |
|
1747 | - $combined_index_key = $this->get_index_primary_key_string($row); |
|
1748 | - $model_objs_affected_ids[ $combined_index_key ] = $combined_index_key; |
|
1749 | - } |
|
1750 | - } |
|
1751 | - if (! $model_objs_affected_ids) { |
|
1752 | - // wait wait wait- if nothing was affected let's stop here |
|
1753 | - return 0; |
|
1754 | - } |
|
1755 | - foreach ($model_objs_affected_ids as $id) { |
|
1756 | - $model_obj_in_entity_map = $this->get_from_entity_map($id); |
|
1757 | - if ($model_obj_in_entity_map) { |
|
1758 | - foreach ($fields_n_values as $field => $new_value) { |
|
1759 | - $model_obj_in_entity_map->set($field, $new_value); |
|
1760 | - } |
|
1761 | - } |
|
1762 | - } |
|
1763 | - // if there is a primary key on this model, we can now do a slight optimization |
|
1764 | - if ($this->has_primary_key_field()) { |
|
1765 | - // we already know what we want to update. So let's make the query simpler so it's a little more efficient |
|
1766 | - $query_params = [ |
|
1767 | - [$this->primary_key_name() => ['IN', $model_objs_affected_ids]], |
|
1768 | - 'limit' => count($model_objs_affected_ids), |
|
1769 | - 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
1770 | - ]; |
|
1771 | - } |
|
1772 | - } |
|
1773 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
1774 | - $SQL = "UPDATE " . $model_query_info->get_full_join_sql() |
|
1775 | - . " SET " . $this->_construct_update_sql($fields_n_values) |
|
1776 | - . $model_query_info->get_where_sql(); |
|
1777 | - // note: doesn't use _construct_2nd_half_of_select_query() because doesn't accept LIMIT, ORDER BY, etc. |
|
1778 | - $rows_affected = $this->_do_wpdb_query('query', [$SQL]); |
|
1779 | - /** |
|
1780 | - * Action called after a model update call has been made. |
|
1781 | - * |
|
1782 | - * @param EEM_Base $model |
|
1783 | - * @param array $fields_n_values the updated fields and their new values |
|
1784 | - * @param array $query_params |
|
1785 | - * @param int $rows_affected |
|
1786 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
1787 | - */ |
|
1788 | - do_action('AHEE__EEM_Base__update__end', $this, $fields_n_values, $query_params, $rows_affected); |
|
1789 | - return $rows_affected;// how many supposedly got updated |
|
1790 | - } |
|
1791 | - |
|
1792 | - |
|
1793 | - /** |
|
1794 | - * Analogous to $wpdb->get_col, returns a 1-dimensional array where teh values |
|
1795 | - * are teh values of the field specified (or by default the primary key field) |
|
1796 | - * that matched the query params. Note that you should pass the name of the |
|
1797 | - * model FIELD, not the database table's column name. |
|
1798 | - * |
|
1799 | - * @param array $query_params |
|
1800 | - * @param string $field_to_select |
|
1801 | - * @return array just like $wpdb->get_col() |
|
1802 | - * @throws EE_Error |
|
1803 | - * @throws ReflectionException |
|
1804 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1805 | - */ |
|
1806 | - public function get_col($query_params = [], $field_to_select = null) |
|
1807 | - { |
|
1808 | - if ($field_to_select) { |
|
1809 | - $field = $this->field_settings_for($field_to_select); |
|
1810 | - } elseif ($this->has_primary_key_field()) { |
|
1811 | - $field = $this->get_primary_key_field(); |
|
1812 | - } else { |
|
1813 | - $field_settings = $this->field_settings(); |
|
1814 | - // no primary key, just grab the first column |
|
1815 | - $field = reset($field_settings); |
|
1816 | - } |
|
1817 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
1818 | - $select_expressions = $field->get_qualified_column(); |
|
1819 | - $SQL = |
|
1820 | - "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
1821 | - return $this->_do_wpdb_query('get_col', [$SQL]); |
|
1822 | - } |
|
1823 | - |
|
1824 | - |
|
1825 | - /** |
|
1826 | - * Returns a single column value for a single row from the database |
|
1827 | - * |
|
1828 | - * @param array $query_params |
|
1829 | - * @param string $field_to_select |
|
1830 | - * @return string |
|
1831 | - * @throws EE_Error |
|
1832 | - * @throws ReflectionException |
|
1833 | - * @see EEM_Base::get_col() |
|
1834 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1835 | - */ |
|
1836 | - public function get_var($query_params = [], $field_to_select = null) |
|
1837 | - { |
|
1838 | - $query_params['limit'] = 1; |
|
1839 | - $col = $this->get_col($query_params, $field_to_select); |
|
1840 | - if (! empty($col)) { |
|
1841 | - return reset($col); |
|
1842 | - } |
|
1843 | - return null; |
|
1844 | - } |
|
1845 | - |
|
1846 | - |
|
1847 | - /** |
|
1848 | - * Makes the SQL for after "UPDATE table_X inner join table_Y..." and before "...WHERE". Eg "Question.name='party |
|
1849 | - * time?', Question.desc='what do you think?'..." Values are filtered through wpdb->prepare to avoid against SQL |
|
1850 | - * injection, but currently no further filtering is done |
|
1851 | - * |
|
1852 | - * @param array $fields_n_values array keys are field names on this model, and values are what those fields should |
|
1853 | - * be updated to in the DB |
|
1854 | - * @return string of SQL |
|
1855 | - * @throws EE_Error |
|
1856 | - * @global $wpdb |
|
1857 | - */ |
|
1858 | - public function _construct_update_sql($fields_n_values) |
|
1859 | - { |
|
1860 | - global $wpdb; |
|
1861 | - $cols_n_values = []; |
|
1862 | - foreach ($fields_n_values as $field_name => $value) { |
|
1863 | - $field_obj = $this->field_settings_for($field_name); |
|
1864 | - // if the value is NULL, we want to assign the value to that. |
|
1865 | - // wpdb->prepare doesn't really handle that properly |
|
1866 | - $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
1867 | - $value_sql = $prepared_value === null |
|
1868 | - ? 'NULL' |
|
1869 | - : $wpdb->prepare($field_obj->get_wpdb_data_type(), $prepared_value); |
|
1870 | - $cols_n_values[] = $field_obj->get_qualified_column() . "=" . $value_sql; |
|
1871 | - } |
|
1872 | - return implode(",", $cols_n_values); |
|
1873 | - } |
|
1874 | - |
|
1875 | - |
|
1876 | - /** |
|
1877 | - * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
1878 | - * Performs a HARD delete, meaning the database row should always be removed, |
|
1879 | - * not just have a flag field on it switched |
|
1880 | - * Wrapper for EEM_Base::delete_permanently() |
|
1881 | - * |
|
1882 | - * @param mixed $id |
|
1883 | - * @param boolean $allow_blocking |
|
1884 | - * @return int the number of rows deleted |
|
1885 | - * @throws EE_Error |
|
1886 | - * @throws ReflectionException |
|
1887 | - */ |
|
1888 | - public function delete_permanently_by_ID($id, $allow_blocking = true) |
|
1889 | - { |
|
1890 | - return $this->delete_permanently( |
|
1891 | - [ |
|
1892 | - [$this->get_primary_key_field()->get_name() => $id], |
|
1893 | - 'limit' => 1, |
|
1894 | - ], |
|
1895 | - $allow_blocking |
|
1896 | - ); |
|
1897 | - } |
|
1898 | - |
|
1899 | - |
|
1900 | - /** |
|
1901 | - * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
1902 | - * Wrapper for EEM_Base::delete() |
|
1903 | - * |
|
1904 | - * @param mixed $id |
|
1905 | - * @param boolean $allow_blocking |
|
1906 | - * @return int the number of rows deleted |
|
1907 | - * @throws EE_Error |
|
1908 | - * @throws ReflectionException |
|
1909 | - */ |
|
1910 | - public function delete_by_ID($id, $allow_blocking = true) |
|
1911 | - { |
|
1912 | - return $this->delete( |
|
1913 | - [ |
|
1914 | - [$this->get_primary_key_field()->get_name() => $id], |
|
1915 | - 'limit' => 1, |
|
1916 | - ], |
|
1917 | - $allow_blocking |
|
1918 | - ); |
|
1919 | - } |
|
1920 | - |
|
1921 | - |
|
1922 | - /** |
|
1923 | - * Identical to delete_permanently, but does a "soft" delete if possible, |
|
1924 | - * meaning if the model has a field that indicates its been "trashed" or |
|
1925 | - * "soft deleted", we will just set that instead of actually deleting the rows. |
|
1926 | - * |
|
1927 | - * @param array $query_params |
|
1928 | - * @param boolean $allow_blocking |
|
1929 | - * @return int how many rows got deleted |
|
1930 | - * @throws EE_Error |
|
1931 | - * @throws ReflectionException |
|
1932 | - * @see EEM_Base::delete_permanently |
|
1933 | - */ |
|
1934 | - public function delete($query_params, $allow_blocking = true) |
|
1935 | - { |
|
1936 | - return $this->delete_permanently($query_params, $allow_blocking); |
|
1937 | - } |
|
1938 | - |
|
1939 | - |
|
1940 | - /** |
|
1941 | - * Deletes the model objects that meet the query params. Note: this method is overridden |
|
1942 | - * in EEM_Soft_Delete_Base so that soft-deleted model objects are instead only flagged |
|
1943 | - * as archived, not actually deleted |
|
1944 | - * |
|
1945 | - * @param array $query_params |
|
1946 | - * @param boolean $allow_blocking if TRUE, matched objects will only be deleted if there is no related model info |
|
1947 | - * that blocks it (ie, there' sno other data that depends on this data); if false, |
|
1948 | - * deletes regardless of other objects which may depend on it. Its generally |
|
1949 | - * advisable to always leave this as TRUE, otherwise you could easily corrupt your |
|
1950 | - * DB |
|
1951 | - * @return int how many rows got deleted |
|
1952 | - * @throws EE_Error |
|
1953 | - * @throws ReflectionException |
|
1954 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1955 | - */ |
|
1956 | - public function delete_permanently($query_params, $allow_blocking = true) |
|
1957 | - { |
|
1958 | - /** |
|
1959 | - * Action called just before performing a real deletion query. You can use the |
|
1960 | - * model and its $query_params to find exactly which items will be deleted |
|
1961 | - * |
|
1962 | - * @param EEM_Base $model |
|
1963 | - * @param array $query_params |
|
1964 | - * @param boolean $allow_blocking whether or not to allow related model objects |
|
1965 | - * to block (prevent) this deletion |
|
1966 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
1967 | - */ |
|
1968 | - do_action('AHEE__EEM_Base__delete__begin', $this, $query_params, $allow_blocking); |
|
1969 | - // some MySQL databases may be running safe mode, which may restrict |
|
1970 | - // deletion if there is no KEY column used in the WHERE statement of a deletion. |
|
1971 | - // to get around this, we first do a SELECT, get all the IDs, and then run another query |
|
1972 | - // to delete them |
|
1973 | - $items_for_deletion = $this->_get_all_wpdb_results($query_params); |
|
1974 | - $columns_and_ids_for_deleting = $this->_get_ids_for_delete($items_for_deletion, $allow_blocking); |
|
1975 | - $deletion_where_query_part = $this->_build_query_part_for_deleting_from_columns_and_values( |
|
1976 | - $columns_and_ids_for_deleting |
|
1977 | - ); |
|
1978 | - /** |
|
1979 | - * Allows client code to act on the items being deleted before the query is actually executed. |
|
1980 | - * |
|
1981 | - * @param EEM_Base $this The model instance being acted on. |
|
1982 | - * @param array $query_params The incoming array of query parameters influencing what gets deleted. |
|
1983 | - * @param bool $allow_blocking @see param description in method phpdoc block. |
|
1984 | - * @param array $columns_and_ids_for_deleting An array indicating what entities will get removed as |
|
1985 | - * derived from the incoming query parameters. |
|
1986 | - * @see details on the structure of this array in the phpdocs |
|
1987 | - * for the `_get_ids_for_delete_method` |
|
1988 | - * |
|
1989 | - */ |
|
1990 | - do_action( |
|
1991 | - 'AHEE__EEM_Base__delete__before_query', |
|
1992 | - $this, |
|
1993 | - $query_params, |
|
1994 | - $allow_blocking, |
|
1995 | - $columns_and_ids_for_deleting |
|
1996 | - ); |
|
1997 | - if ($deletion_where_query_part) { |
|
1998 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
1999 | - $table_aliases = implode(', ', array_keys($this->_tables)); |
|
2000 | - $SQL = "DELETE " . $table_aliases |
|
2001 | - . " FROM " . $model_query_info->get_full_join_sql() |
|
2002 | - . " WHERE " . $deletion_where_query_part; |
|
2003 | - $rows_deleted = $this->_do_wpdb_query('query', [$SQL]); |
|
2004 | - } else { |
|
2005 | - $rows_deleted = 0; |
|
2006 | - } |
|
2007 | - |
|
2008 | - // Next, make sure those items are removed from the entity map; if they could be put into it at all; and if |
|
2009 | - // there was no error with the delete query. |
|
2010 | - if ( |
|
2011 | - $this->has_primary_key_field() |
|
2012 | - && $rows_deleted !== false |
|
2013 | - && isset($columns_and_ids_for_deleting[ $this->get_primary_key_field()->get_qualified_column() ]) |
|
2014 | - ) { |
|
2015 | - $ids_for_removal = $columns_and_ids_for_deleting[ $this->get_primary_key_field()->get_qualified_column() ]; |
|
2016 | - foreach ($ids_for_removal as $id) { |
|
2017 | - if (isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ])) { |
|
2018 | - unset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]); |
|
2019 | - } |
|
2020 | - } |
|
2021 | - |
|
2022 | - // delete any extra meta attached to the deleted entities but ONLY if this model is not an instance of |
|
2023 | - // `EEM_Extra_Meta`. In other words we want to prevent recursion on EEM_Extra_Meta::delete_permanently calls |
|
2024 | - // unnecessarily. It's very unlikely that users will have assigned Extra Meta to Extra Meta |
|
2025 | - // (although it is possible). |
|
2026 | - // Note this can be skipped by using the provided filter and returning false. |
|
2027 | - if ( |
|
2028 | - apply_filters( |
|
2029 | - 'FHEE__EEM_Base__delete_permanently__dont_delete_extra_meta_for_extra_meta', |
|
2030 | - ! $this instanceof EEM_Extra_Meta, |
|
2031 | - $this |
|
2032 | - ) |
|
2033 | - ) { |
|
2034 | - EEM_Extra_Meta::instance()->delete_permanently( |
|
2035 | - [ |
|
2036 | - 0 => [ |
|
2037 | - 'EXM_type' => $this->get_this_model_name(), |
|
2038 | - 'OBJ_ID' => [ |
|
2039 | - 'IN', |
|
2040 | - $ids_for_removal, |
|
2041 | - ], |
|
2042 | - ], |
|
2043 | - ] |
|
2044 | - ); |
|
2045 | - } |
|
2046 | - } |
|
2047 | - |
|
2048 | - /** |
|
2049 | - * Action called just after performing a real deletion query. Although at this point the |
|
2050 | - * items should have been deleted |
|
2051 | - * |
|
2052 | - * @param EEM_Base $model |
|
2053 | - * @param array $query_params |
|
2054 | - * @param int $rows_deleted |
|
2055 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
2056 | - */ |
|
2057 | - do_action('AHEE__EEM_Base__delete__end', $this, $query_params, $rows_deleted, $columns_and_ids_for_deleting); |
|
2058 | - return $rows_deleted;// how many supposedly got deleted |
|
2059 | - } |
|
2060 | - |
|
2061 | - |
|
2062 | - /** |
|
2063 | - * Checks all the relations that throw error messages when there are blocking related objects |
|
2064 | - * for related model objects. If there are any related model objects on those relations, |
|
2065 | - * adds an EE_Error, and return true |
|
2066 | - * |
|
2067 | - * @param EE_Base_Class|int $this_model_obj_or_id |
|
2068 | - * @param EE_Base_Class $ignore_this_model_obj a model object like 'EE_Event', or 'EE_Term_Taxonomy', which |
|
2069 | - * should be ignored when determining whether there are related |
|
2070 | - * model objects which block this model object's deletion. Useful |
|
2071 | - * if you know A is related to B and are considering deleting A, |
|
2072 | - * but want to see if A has any other objects blocking its deletion |
|
2073 | - * before removing the relation between A and B |
|
2074 | - * @return boolean |
|
2075 | - * @throws EE_Error |
|
2076 | - * @throws ReflectionException |
|
2077 | - */ |
|
2078 | - public function delete_is_blocked_by_related_models($this_model_obj_or_id, $ignore_this_model_obj = null) |
|
2079 | - { |
|
2080 | - // first, if $ignore_this_model_obj was supplied, get its model |
|
2081 | - if ($ignore_this_model_obj && $ignore_this_model_obj instanceof EE_Base_Class) { |
|
2082 | - $ignored_model = $ignore_this_model_obj->get_model(); |
|
2083 | - } else { |
|
2084 | - $ignored_model = null; |
|
2085 | - } |
|
2086 | - // now check all the relations of $this_model_obj_or_id and see if there |
|
2087 | - // are any related model objects blocking it? |
|
2088 | - $is_blocked = false; |
|
2089 | - foreach ($this->_model_relations as $relation_name => $relation_obj) { |
|
2090 | - if ($relation_obj->block_delete_if_related_models_exist()) { |
|
2091 | - // if $ignore_this_model_obj was supplied, then for the query |
|
2092 | - // on that model needs to be told to ignore $ignore_this_model_obj |
|
2093 | - if ($ignored_model && $relation_name === $ignored_model->get_this_model_name()) { |
|
2094 | - $related_model_objects = $relation_obj->get_all_related( |
|
2095 | - $this_model_obj_or_id, |
|
2096 | - [ |
|
2097 | - [ |
|
2098 | - $ignored_model->get_primary_key_field()->get_name() => [ |
|
2099 | - '!=', |
|
2100 | - $ignore_this_model_obj->ID(), |
|
2101 | - ], |
|
2102 | - ], |
|
2103 | - ] |
|
2104 | - ); |
|
2105 | - } else { |
|
2106 | - $related_model_objects = $relation_obj->get_all_related($this_model_obj_or_id); |
|
2107 | - } |
|
2108 | - if ($related_model_objects) { |
|
2109 | - EE_Error::add_error($relation_obj->get_deletion_error_message(), __FILE__, __FUNCTION__, __LINE__); |
|
2110 | - $is_blocked = true; |
|
2111 | - } |
|
2112 | - } |
|
2113 | - } |
|
2114 | - return $is_blocked; |
|
2115 | - } |
|
2116 | - |
|
2117 | - |
|
2118 | - /** |
|
2119 | - * Builds the columns and values for items to delete from the incoming $row_results_for_deleting array. |
|
2120 | - * |
|
2121 | - * @param array $row_results_for_deleting |
|
2122 | - * @param bool $allow_blocking |
|
2123 | - * @return array The shape of this array depends on whether the model `has_primary_key_field` or not. If the |
|
2124 | - * model DOES have a primary_key_field, then the array will be a simple single |
|
2125 | - * dimension array where the key is the fully qualified primary key column and the |
|
2126 | - * value is an array of ids that will be deleted. Example: array('Event.EVT_ID' => |
|
2127 | - * array( 1,2,3)) If the model DOES NOT have a primary_key_field, then the array will |
|
2128 | - * be a two dimensional array where each element is a group of columns and values that |
|
2129 | - * get deleted. Example: array( |
|
2130 | - * 0 => array( |
|
2131 | - * 'Term_Relationship.object_id' => 1 |
|
2132 | - * 'Term_Relationship.term_taxonomy_id' => 5 |
|
2133 | - * ), |
|
2134 | - * 1 => array( |
|
2135 | - * 'Term_Relationship.object_id' => 1 |
|
2136 | - * 'Term_Relationship.term_taxonomy_id' => 6 |
|
2137 | - * ) |
|
2138 | - * ) |
|
2139 | - * @throws EE_Error |
|
2140 | - * @throws ReflectionException |
|
2141 | - */ |
|
2142 | - protected function _get_ids_for_delete(array $row_results_for_deleting, $allow_blocking = true) |
|
2143 | - { |
|
2144 | - $ids_to_delete_indexed_by_column = []; |
|
2145 | - if ($this->has_primary_key_field()) { |
|
2146 | - $primary_table = $this->_get_main_table(); |
|
2147 | - $ids_to_delete_indexed_by_column = []; |
|
2148 | - foreach ($row_results_for_deleting as $item_to_delete) { |
|
2149 | - // before we mark this item for deletion, |
|
2150 | - // make sure there's no related entities blocking its deletion (if we're checking) |
|
2151 | - if ( |
|
2152 | - $allow_blocking |
|
2153 | - && $this->delete_is_blocked_by_related_models( |
|
2154 | - $item_to_delete[ $primary_table->get_fully_qualified_pk_column() ] |
|
2155 | - ) |
|
2156 | - ) { |
|
2157 | - continue; |
|
2158 | - } |
|
2159 | - // primary table deletes |
|
2160 | - if (isset($item_to_delete[ $primary_table->get_fully_qualified_pk_column() ])) { |
|
2161 | - $ids_to_delete_indexed_by_column[ $primary_table->get_fully_qualified_pk_column() ][] = |
|
2162 | - $item_to_delete[ $primary_table->get_fully_qualified_pk_column() ]; |
|
2163 | - } |
|
2164 | - } |
|
2165 | - } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
|
2166 | - $fields = $this->get_combined_primary_key_fields(); |
|
2167 | - foreach ($row_results_for_deleting as $item_to_delete) { |
|
2168 | - $ids_to_delete_indexed_by_column_for_row = []; |
|
2169 | - foreach ($fields as $cpk_field) { |
|
2170 | - if ($cpk_field instanceof EE_Model_Field_Base) { |
|
2171 | - $ids_to_delete_indexed_by_column_for_row[ $cpk_field->get_qualified_column() ] = |
|
2172 | - $item_to_delete[ $cpk_field->get_qualified_column() ]; |
|
2173 | - } |
|
2174 | - } |
|
2175 | - $ids_to_delete_indexed_by_column[] = $ids_to_delete_indexed_by_column_for_row; |
|
2176 | - } |
|
2177 | - } else { |
|
2178 | - // so there's no primary key and no combined key... |
|
2179 | - // sorry, can't help you |
|
2180 | - throw new EE_Error( |
|
2181 | - sprintf( |
|
2182 | - esc_html__( |
|
2183 | - "Cannot delete objects of type %s because there is no primary key NOR combined key", |
|
2184 | - "event_espresso" |
|
2185 | - ), |
|
2186 | - get_class($this) |
|
2187 | - ) |
|
2188 | - ); |
|
2189 | - } |
|
2190 | - return $ids_to_delete_indexed_by_column; |
|
2191 | - } |
|
2192 | - |
|
2193 | - |
|
2194 | - /** |
|
2195 | - * This receives an array of columns and values set to be deleted (as prepared by _get_ids_for_delete) and prepares |
|
2196 | - * the corresponding query_part for the query performing the delete. |
|
2197 | - * |
|
2198 | - * @param array $ids_to_delete_indexed_by_column @see _get_ids_for_delete for how this array might be shaped. |
|
2199 | - * @return string |
|
2200 | - * @throws EE_Error |
|
2201 | - */ |
|
2202 | - protected function _build_query_part_for_deleting_from_columns_and_values(array $ids_to_delete_indexed_by_column) |
|
2203 | - { |
|
2204 | - $query_part = ''; |
|
2205 | - if (empty($ids_to_delete_indexed_by_column)) { |
|
2206 | - return $query_part; |
|
2207 | - } elseif ($this->has_primary_key_field()) { |
|
2208 | - $query = []; |
|
2209 | - foreach ($ids_to_delete_indexed_by_column as $column => $ids) { |
|
2210 | - $query[] = $column . ' IN' . $this->_construct_in_value($ids, $this->_primary_key_field); |
|
2211 | - } |
|
2212 | - $query_part = ! empty($query) ? implode(' AND ', $query) : $query_part; |
|
2213 | - } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
|
2214 | - $ways_to_identify_a_row = []; |
|
2215 | - foreach ($ids_to_delete_indexed_by_column as $ids_to_delete_indexed_by_column_for_each_row) { |
|
2216 | - $values_for_each_combined_primary_key_for_a_row = []; |
|
2217 | - foreach ($ids_to_delete_indexed_by_column_for_each_row as $column => $id) { |
|
2218 | - $values_for_each_combined_primary_key_for_a_row[] = $column . '=' . $id; |
|
2219 | - } |
|
2220 | - $value_and_value_and_value = implode(' AND ', $values_for_each_combined_primary_key_for_a_row); |
|
2221 | - $ways_to_identify_a_row[] = "({$value_and_value_and_value})"; |
|
2222 | - } |
|
2223 | - $query_part = implode(' OR ', $ways_to_identify_a_row); |
|
2224 | - } |
|
2225 | - return $query_part; |
|
2226 | - } |
|
2227 | - |
|
2228 | - |
|
2229 | - /** |
|
2230 | - * Gets the model field by the fully qualified name |
|
2231 | - * |
|
2232 | - * @param string $qualified_column_name eg 'Event_CPT.post_name' or $field_obj->get_qualified_column() |
|
2233 | - * @return EE_Model_Field_Base |
|
2234 | - * @throws EE_Error |
|
2235 | - */ |
|
2236 | - public function get_field_by_column($qualified_column_name) |
|
2237 | - { |
|
2238 | - foreach ($this->field_settings(true) as $field_obj) { |
|
2239 | - if ($field_obj->get_qualified_column() === $qualified_column_name) { |
|
2240 | - return $field_obj; |
|
2241 | - } |
|
2242 | - } |
|
2243 | - throw new EE_Error( |
|
2244 | - sprintf( |
|
2245 | - esc_html__('Could not find a field on the model "%1$s" for qualified column "%2$s"', 'event_espresso'), |
|
2246 | - $this->get_this_model_name(), |
|
2247 | - $qualified_column_name |
|
2248 | - ) |
|
2249 | - ); |
|
2250 | - } |
|
2251 | - |
|
2252 | - |
|
2253 | - /** |
|
2254 | - * Count all the rows that match criteria the model query params. |
|
2255 | - * If $field_to_count isn't provided, the model's primary key is used. Otherwise, we count by field_to_count's |
|
2256 | - * column |
|
2257 | - * |
|
2258 | - * @param array $query_params |
|
2259 | - * @param string $field_to_count field on model to count by (not column name) |
|
2260 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
2261 | - * that by the setting $distinct to TRUE; |
|
2262 | - * @return int |
|
2263 | - * @throws EE_Error |
|
2264 | - * @throws ReflectionException |
|
2265 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2266 | - */ |
|
2267 | - public function count($query_params = [], $field_to_count = null, $distinct = false) |
|
2268 | - { |
|
2269 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
2270 | - if ($field_to_count) { |
|
2271 | - $field_obj = $this->field_settings_for($field_to_count); |
|
2272 | - $column_to_count = $field_obj->get_qualified_column(); |
|
2273 | - } elseif ($this->has_primary_key_field()) { |
|
2274 | - $pk_field_obj = $this->get_primary_key_field(); |
|
2275 | - $column_to_count = $pk_field_obj->get_qualified_column(); |
|
2276 | - } else { |
|
2277 | - // there's no primary key |
|
2278 | - // if we're counting distinct items, and there's no primary key, |
|
2279 | - // we need to list out the columns for distinction; |
|
2280 | - // otherwise we can just use star |
|
2281 | - if ($distinct) { |
|
2282 | - $columns_to_use = []; |
|
2283 | - foreach ($this->get_combined_primary_key_fields() as $field_obj) { |
|
2284 | - $columns_to_use[] = $field_obj->get_qualified_column(); |
|
2285 | - } |
|
2286 | - $column_to_count = implode(',', $columns_to_use); |
|
2287 | - } else { |
|
2288 | - $column_to_count = '*'; |
|
2289 | - } |
|
2290 | - } |
|
2291 | - $column_to_count = $distinct ? "DISTINCT " . $column_to_count : $column_to_count; |
|
2292 | - $SQL = |
|
2293 | - "SELECT COUNT(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
2294 | - return (int) $this->_do_wpdb_query('get_var', [$SQL]); |
|
2295 | - } |
|
2296 | - |
|
2297 | - |
|
2298 | - /** |
|
2299 | - * Sums up the value of the $field_to_sum (defaults to the primary key, which isn't terribly useful) |
|
2300 | - * |
|
2301 | - * @param array $query_params |
|
2302 | - * @param string $field_to_sum name of field (array key in $_fields array) |
|
2303 | - * @return float |
|
2304 | - * @throws EE_Error |
|
2305 | - * @throws ReflectionException |
|
2306 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2307 | - */ |
|
2308 | - public function sum($query_params, $field_to_sum = null) |
|
2309 | - { |
|
2310 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
2311 | - if ($field_to_sum) { |
|
2312 | - $field_obj = $this->field_settings_for($field_to_sum); |
|
2313 | - } else { |
|
2314 | - $field_obj = $this->get_primary_key_field(); |
|
2315 | - } |
|
2316 | - $column_to_count = $field_obj->get_qualified_column(); |
|
2317 | - $SQL = "SELECT SUM(" . $column_to_count . ")" |
|
2318 | - . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
2319 | - $return_value = $this->_do_wpdb_query('get_var', [$SQL]); |
|
2320 | - // $data_type = $field_obj->get_wpdb_data_type(); |
|
2321 | - // if ($data_type === '%d' || $data_type === '%s') { |
|
2322 | - // return (float) $return_value; |
|
2323 | - // } |
|
2324 | - // must be %f |
|
2325 | - return (float) $return_value; |
|
2326 | - } |
|
2327 | - |
|
2328 | - |
|
2329 | - /** |
|
2330 | - * Just calls the specified method on $wpdb with the given arguments |
|
2331 | - * Consolidates a little extra error handling code |
|
2332 | - * |
|
2333 | - * @param string $wpdb_method |
|
2334 | - * @param array $arguments_to_provide |
|
2335 | - * @return mixed |
|
2336 | - * @throws EE_Error |
|
2337 | - * @throws ReflectionException |
|
2338 | - * @global wpdb $wpdb |
|
2339 | - */ |
|
2340 | - protected function _do_wpdb_query($wpdb_method, $arguments_to_provide) |
|
2341 | - { |
|
2342 | - // if we're in maintenance mode level 2, DON'T run any queries |
|
2343 | - // because level 2 indicates the database needs updating and |
|
2344 | - // is probably out of sync with the code |
|
2345 | - if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
2346 | - throw new EE_Error( |
|
2347 | - esc_html__( |
|
2348 | - 'Event Espresso Level 2 Maintenance mode is active. That means EE can not run ANY database queries until the necessary migration scripts have run which will take EE out of maintenance mode level 2. Please inform support of this error.', |
|
2349 | - 'event_espresso' |
|
2350 | - ) |
|
2351 | - ); |
|
2352 | - } |
|
2353 | - global $wpdb; |
|
2354 | - if (! method_exists($wpdb, $wpdb_method)) { |
|
2355 | - throw new EE_Error( |
|
2356 | - sprintf( |
|
2357 | - esc_html__( |
|
2358 | - 'There is no method named "%s" on Wordpress\' $wpdb object', |
|
2359 | - 'event_espresso' |
|
2360 | - ), |
|
2361 | - $wpdb_method |
|
2362 | - ) |
|
2363 | - ); |
|
2364 | - } |
|
2365 | - $old_show_errors_value = $wpdb->show_errors; |
|
2366 | - if (WP_DEBUG) { |
|
2367 | - $wpdb->show_errors(false); |
|
2368 | - } |
|
2369 | - $result = $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
2370 | - $this->show_db_query_if_previously_requested($wpdb->last_query); |
|
2371 | - if (WP_DEBUG) { |
|
2372 | - $wpdb->show_errors($old_show_errors_value); |
|
2373 | - if (! empty($wpdb->last_error)) { |
|
2374 | - throw new EE_Error(sprintf(__('WPDB Error: "%s"', 'event_espresso'), $wpdb->last_error)); |
|
2375 | - } |
|
2376 | - if ($result === false) { |
|
2377 | - throw new EE_Error( |
|
2378 | - sprintf( |
|
2379 | - esc_html__( |
|
2380 | - 'WPDB Error occurred, but no error message was logged by wpdb! The wpdb method called was "%1$s" and the arguments were "%2$s"', |
|
2381 | - 'event_espresso' |
|
2382 | - ), |
|
2383 | - $wpdb_method, |
|
2384 | - var_export($arguments_to_provide, true) |
|
2385 | - ) |
|
2386 | - ); |
|
2387 | - } |
|
2388 | - } elseif ($result === false) { |
|
2389 | - EE_Error::add_error( |
|
2390 | - sprintf( |
|
2391 | - esc_html__( |
|
2392 | - 'A database error has occurred. Turn on WP_DEBUG for more information.||A database error occurred doing wpdb method "%1$s", with arguments "%2$s". The error was "%3$s"', |
|
2393 | - 'event_espresso' |
|
2394 | - ), |
|
2395 | - $wpdb_method, |
|
2396 | - var_export($arguments_to_provide, true), |
|
2397 | - $wpdb->last_error |
|
2398 | - ), |
|
2399 | - __FILE__, |
|
2400 | - __FUNCTION__, |
|
2401 | - __LINE__ |
|
2402 | - ); |
|
2403 | - } |
|
2404 | - return $result; |
|
2405 | - } |
|
2406 | - |
|
2407 | - |
|
2408 | - /** |
|
2409 | - * Attempts to run the indicated WPDB method with the provided arguments, |
|
2410 | - * and if there's an error tries to verify the DB is correct. Uses |
|
2411 | - * the static property EEM_Base::$_db_verification_level to determine whether |
|
2412 | - * we should try to fix the EE core db, the addons, or just give up |
|
2413 | - * |
|
2414 | - * @param string $wpdb_method |
|
2415 | - * @param array $arguments_to_provide |
|
2416 | - * @return mixed |
|
2417 | - * @throws EE_Error |
|
2418 | - * @throws ReflectionException |
|
2419 | - */ |
|
2420 | - private function _process_wpdb_query($wpdb_method, $arguments_to_provide) |
|
2421 | - { |
|
2422 | - global $wpdb; |
|
2423 | - $wpdb->last_error = null; |
|
2424 | - $result = call_user_func_array([$wpdb, $wpdb_method], $arguments_to_provide); |
|
2425 | - // was there an error running the query? but we don't care on new activations |
|
2426 | - // (we're going to setup the DB anyway on new activations) |
|
2427 | - if ( |
|
2428 | - ($result === false || ! empty($wpdb->last_error)) |
|
2429 | - && EE_System::instance()->detect_req_type() !== EE_System::req_type_new_activation |
|
2430 | - ) { |
|
2431 | - switch (EEM_Base::$_db_verification_level) { |
|
2432 | - case EEM_Base::db_verified_none: |
|
2433 | - // let's double-check core's DB |
|
2434 | - $error_message = $this->_verify_core_db($wpdb_method, $arguments_to_provide); |
|
2435 | - break; |
|
2436 | - case EEM_Base::db_verified_core: |
|
2437 | - // STILL NO LOVE?? verify all the addons too. Maybe they need to be fixed |
|
2438 | - $error_message = $this->_verify_addons_db($wpdb_method, $arguments_to_provide); |
|
2439 | - break; |
|
2440 | - case EEM_Base::db_verified_addons: |
|
2441 | - // ummmm... you in trouble |
|
2442 | - return $result; |
|
2443 | - } |
|
2444 | - if (! empty($error_message)) { |
|
2445 | - EE_Log::instance()->log(__FILE__, __FUNCTION__, $error_message, 'error'); |
|
2446 | - trigger_error($error_message); |
|
2447 | - } |
|
2448 | - return $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
2449 | - } |
|
2450 | - return $result; |
|
2451 | - } |
|
2452 | - |
|
2453 | - |
|
2454 | - /** |
|
2455 | - * Verifies the EE core database is up-to-date and records that we've done it on |
|
2456 | - * EEM_Base::$_db_verification_level |
|
2457 | - * |
|
2458 | - * @param string $wpdb_method |
|
2459 | - * @param array $arguments_to_provide |
|
2460 | - * @return string |
|
2461 | - * @throws EE_Error |
|
2462 | - * @throws ReflectionException |
|
2463 | - */ |
|
2464 | - private function _verify_core_db($wpdb_method, $arguments_to_provide) |
|
2465 | - { |
|
2466 | - global $wpdb; |
|
2467 | - // ok remember that we've already attempted fixing the core db, in case the problem persists |
|
2468 | - EEM_Base::$_db_verification_level = EEM_Base::db_verified_core; |
|
2469 | - $error_message = sprintf( |
|
2470 | - esc_html__( |
|
2471 | - 'WPDB Error "%1$s" while running wpdb method "%2$s" with arguments %3$s. Automatically attempting to fix EE Core DB', |
|
2472 | - 'event_espresso' |
|
2473 | - ), |
|
2474 | - $wpdb->last_error, |
|
2475 | - $wpdb_method, |
|
2476 | - wp_json_encode($arguments_to_provide) |
|
2477 | - ); |
|
2478 | - EE_System::instance()->initialize_db_if_no_migrations_required(); |
|
2479 | - return $error_message; |
|
2480 | - } |
|
2481 | - |
|
2482 | - |
|
2483 | - /** |
|
2484 | - * Verifies the EE addons' database is up-to-date and records that we've done it on |
|
2485 | - * EEM_Base::$_db_verification_level |
|
2486 | - * |
|
2487 | - * @param $wpdb_method |
|
2488 | - * @param $arguments_to_provide |
|
2489 | - * @return string |
|
2490 | - * @throws EE_Error |
|
2491 | - * @throws ReflectionException |
|
2492 | - */ |
|
2493 | - private function _verify_addons_db($wpdb_method, $arguments_to_provide) |
|
2494 | - { |
|
2495 | - global $wpdb; |
|
2496 | - // ok remember that we've already attempted fixing the addons dbs, in case the problem persists |
|
2497 | - EEM_Base::$_db_verification_level = EEM_Base::db_verified_addons; |
|
2498 | - $error_message = sprintf( |
|
2499 | - esc_html__( |
|
2500 | - 'WPDB AGAIN: Error "%1$s" while running the same method and arguments as before. Automatically attempting to fix EE Addons DB', |
|
2501 | - 'event_espresso' |
|
2502 | - ), |
|
2503 | - $wpdb->last_error, |
|
2504 | - $wpdb_method, |
|
2505 | - wp_json_encode($arguments_to_provide) |
|
2506 | - ); |
|
2507 | - EE_System::instance()->initialize_addons(); |
|
2508 | - return $error_message; |
|
2509 | - } |
|
2510 | - |
|
2511 | - |
|
2512 | - /** |
|
2513 | - * In order to avoid repeating this code for the get_all, sum, and count functions, put the code parts |
|
2514 | - * that are identical in here. Returns a string of SQL of everything in a SELECT query except the beginning |
|
2515 | - * SELECT clause, eg " FROM wp_posts AS Event INNER JOIN ... WHERE ... ORDER BY ... LIMIT ... GROUP BY ... HAVING |
|
2516 | - * ..." |
|
2517 | - * |
|
2518 | - * @param EE_Model_Query_Info_Carrier $model_query_info |
|
2519 | - * @return string |
|
2520 | - */ |
|
2521 | - private function _construct_2nd_half_of_select_query(EE_Model_Query_Info_Carrier $model_query_info) |
|
2522 | - { |
|
2523 | - return " FROM " . $model_query_info->get_full_join_sql() . |
|
2524 | - $model_query_info->get_where_sql() . |
|
2525 | - $model_query_info->get_group_by_sql() . |
|
2526 | - $model_query_info->get_having_sql() . |
|
2527 | - $model_query_info->get_order_by_sql() . |
|
2528 | - $model_query_info->get_limit_sql(); |
|
2529 | - } |
|
2530 | - |
|
2531 | - |
|
2532 | - /** |
|
2533 | - * Set to easily debug the next X queries ran from this model. |
|
2534 | - * |
|
2535 | - * @param int $count |
|
2536 | - */ |
|
2537 | - public function show_next_x_db_queries($count = 1) |
|
2538 | - { |
|
2539 | - $this->_show_next_x_db_queries = $count; |
|
2540 | - } |
|
2541 | - |
|
2542 | - |
|
2543 | - /** |
|
2544 | - * @param $sql_query |
|
2545 | - */ |
|
2546 | - public function show_db_query_if_previously_requested($sql_query) |
|
2547 | - { |
|
2548 | - if ($this->_show_next_x_db_queries > 0) { |
|
2549 | - echo $sql_query; |
|
2550 | - $this->_show_next_x_db_queries--; |
|
2551 | - } |
|
2552 | - } |
|
2553 | - |
|
2554 | - |
|
2555 | - /** |
|
2556 | - * Adds a relationship of the correct type between $modelObject and $otherModelObject. |
|
2557 | - * There are the 3 cases: |
|
2558 | - * 'belongsTo' relationship: sets $id_or_obj's foreign_key to be $other_model_id_or_obj's primary_key. If |
|
2559 | - * $otherModelObject has no ID, it is first saved. |
|
2560 | - * 'hasMany' relationship: sets $other_model_id_or_obj's foreign_key to be $id_or_obj's primary_key. If $id_or_obj |
|
2561 | - * has no ID, it is first saved. |
|
2562 | - * 'hasAndBelongsToMany' relationships: checks that there isn't already an entry in the join table, and adds one. |
|
2563 | - * If one of the model Objects has not yet been saved to the database, it is saved before adding the entry in the |
|
2564 | - * join table |
|
2565 | - * |
|
2566 | - * @param EE_Base_Class /int $thisModelObject |
|
2567 | - * @param EE_Base_Class /int $id_or_obj EE_base_Class or ID of other Model Object |
|
2568 | - * @param string $relationName , key in EEM_Base::_relations |
|
2569 | - * an attendee to a group, you also want to specify which role they |
|
2570 | - * will have in that group. So you would use this parameter to |
|
2571 | - * specify array('role-column-name'=>'role-id') |
|
2572 | - * @param array $extra_join_model_fields_n_values This allows you to enter further query params for the relation |
|
2573 | - * to for relation to methods that allow you to further specify |
|
2574 | - * extra columns to join by (such as HABTM). Keep in mind that the |
|
2575 | - * only acceptable query_params is strict "col" => "value" pairs |
|
2576 | - * because these will be inserted in any new rows created as well. |
|
2577 | - * @return EE_Base_Class which was added as a relation. Object referred to by $other_model_id_or_obj |
|
2578 | - * @throws EE_Error |
|
2579 | - */ |
|
2580 | - public function add_relationship_to( |
|
2581 | - $id_or_obj, |
|
2582 | - $other_model_id_or_obj, |
|
2583 | - $relationName, |
|
2584 | - $extra_join_model_fields_n_values = [] |
|
2585 | - ) { |
|
2586 | - $relation_obj = $this->related_settings_for($relationName); |
|
2587 | - return $relation_obj->add_relation_to($id_or_obj, $other_model_id_or_obj, $extra_join_model_fields_n_values); |
|
2588 | - } |
|
2589 | - |
|
2590 | - |
|
2591 | - /** |
|
2592 | - * Removes a relationship of the correct type between $modelObject and $otherModelObject. |
|
2593 | - * There are the 3 cases: |
|
2594 | - * 'belongsTo' relationship: sets $modelObject's foreign_key to null, if that field is nullable.Otherwise throws an |
|
2595 | - * error |
|
2596 | - * 'hasMany' relationship: sets $otherModelObject's foreign_key to null,if that field is nullable.Otherwise throws |
|
2597 | - * an error |
|
2598 | - * 'hasAndBelongsToMany' relationships:removes any existing entry in the join table between the two models. |
|
2599 | - * |
|
2600 | - * @param EE_Base_Class /int $id_or_obj |
|
2601 | - * @param EE_Base_Class /int $other_model_id_or_obj EE_Base_Class or ID of other Model Object |
|
2602 | - * @param string $relationName key in EEM_Base::_relations |
|
2603 | - * @param array $where_query This allows you to enter further query params for the relation to for relation to |
|
2604 | - * methods that allow you to further specify extra columns to join by (such as HABTM). |
|
2605 | - * Keep in mind that the only acceptable query_params is strict "col" => "value" pairs |
|
2606 | - * because these will be inserted in any new rows created as well. |
|
2607 | - * @return boolean of success |
|
2608 | - * @throws EE_Error |
|
2609 | - */ |
|
2610 | - public function remove_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query = []) |
|
2611 | - { |
|
2612 | - $relation_obj = $this->related_settings_for($relationName); |
|
2613 | - return $relation_obj->remove_relation_to($id_or_obj, $other_model_id_or_obj, $where_query); |
|
2614 | - } |
|
2615 | - |
|
2616 | - |
|
2617 | - /** |
|
2618 | - * @param mixed $id_or_obj |
|
2619 | - * @param string $relationName |
|
2620 | - * @param array $where_query_params |
|
2621 | - * @param EE_Base_Class[] objects to which relations were removed |
|
2622 | - * @return EE_Base_Class[] |
|
2623 | - * @throws EE_Error |
|
2624 | - */ |
|
2625 | - public function remove_relations($id_or_obj, $relationName, $where_query_params = []) |
|
2626 | - { |
|
2627 | - $relation_obj = $this->related_settings_for($relationName); |
|
2628 | - return $relation_obj->remove_relations($id_or_obj, $where_query_params); |
|
2629 | - } |
|
2630 | - |
|
2631 | - |
|
2632 | - /** |
|
2633 | - * Gets all the related items of the specified $model_name, using $query_params. |
|
2634 | - * Note: by default, we remove the "default query params" |
|
2635 | - * because we want to get even deleted items etc. |
|
2636 | - * |
|
2637 | - * @param mixed $id_or_obj EE_Base_Class child or its ID |
|
2638 | - * @param string $model_name like 'Event', 'Registration', etc. always singular |
|
2639 | - * @param array $query_params see github link below for more info |
|
2640 | - * @return EE_Base_Class[] |
|
2641 | - * @throws EE_Error |
|
2642 | - * @throws ReflectionException |
|
2643 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2644 | - */ |
|
2645 | - public function get_all_related($id_or_obj, $model_name, $query_params = null) |
|
2646 | - { |
|
2647 | - $model_obj = $this->ensure_is_obj($id_or_obj); |
|
2648 | - $relation_settings = $this->related_settings_for($model_name); |
|
2649 | - return $relation_settings->get_all_related($model_obj, $query_params); |
|
2650 | - } |
|
2651 | - |
|
2652 | - |
|
2653 | - /** |
|
2654 | - * Deletes all the model objects across the relation indicated by $model_name |
|
2655 | - * which are related to $id_or_obj which meet the criteria set in $query_params. |
|
2656 | - * However, if the model objects can't be deleted because of blocking related model objects, then |
|
2657 | - * they aren't deleted. (Unless the thing that would have been deleted can be soft-deleted, that still happens). |
|
2658 | - * |
|
2659 | - * @param EE_Base_Class|int|string $id_or_obj |
|
2660 | - * @param string $model_name |
|
2661 | - * @param array $query_params |
|
2662 | - * @return int how many deleted |
|
2663 | - * @throws EE_Error |
|
2664 | - * @throws ReflectionException |
|
2665 | - */ |
|
2666 | - public function delete_related($id_or_obj, $model_name, $query_params = []) |
|
2667 | - { |
|
2668 | - $model_obj = $this->ensure_is_obj($id_or_obj); |
|
2669 | - $relation_settings = $this->related_settings_for($model_name); |
|
2670 | - return $relation_settings->delete_all_related($model_obj, $query_params); |
|
2671 | - } |
|
2672 | - |
|
2673 | - |
|
2674 | - /** |
|
2675 | - * Hard deletes all the model objects across the relation indicated by $model_name |
|
2676 | - * which are related to $id_or_obj which meet the criteria set in $query_params. If |
|
2677 | - * the model objects can't be hard deleted because of blocking related model objects, |
|
2678 | - * just does a soft-delete on them instead. |
|
2679 | - * |
|
2680 | - * @param EE_Base_Class|int|string $id_or_obj |
|
2681 | - * @param string $model_name |
|
2682 | - * @param array $query_params |
|
2683 | - * @return int how many deleted |
|
2684 | - * @throws EE_Error |
|
2685 | - * @throws ReflectionException |
|
2686 | - */ |
|
2687 | - public function delete_related_permanently($id_or_obj, $model_name, $query_params = []) |
|
2688 | - { |
|
2689 | - $model_obj = $this->ensure_is_obj($id_or_obj); |
|
2690 | - $relation_settings = $this->related_settings_for($model_name); |
|
2691 | - return $relation_settings->delete_related_permanently($model_obj, $query_params); |
|
2692 | - } |
|
2693 | - |
|
2694 | - |
|
2695 | - /** |
|
2696 | - * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
2697 | - * unless otherwise specified in the $query_params |
|
2698 | - * |
|
2699 | - * @param int /EE_Base_Class $id_or_obj |
|
2700 | - * @param string $model_name like 'Event', or 'Registration' |
|
2701 | - * @param array $query_params |
|
2702 | - * @param string $field_to_count name of field to count by. By default, uses primary key |
|
2703 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
2704 | - * that by the setting $distinct to TRUE; |
|
2705 | - * @return int |
|
2706 | - * @throws EE_Error |
|
2707 | - * @throws ReflectionException |
|
2708 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2709 | - */ |
|
2710 | - public function count_related( |
|
2711 | - $id_or_obj, |
|
2712 | - $model_name, |
|
2713 | - $query_params = [], |
|
2714 | - $field_to_count = null, |
|
2715 | - $distinct = false |
|
2716 | - ) { |
|
2717 | - $related_model = $this->get_related_model_obj($model_name); |
|
2718 | - // we're just going to use the query params on the related model's normal get_all query, |
|
2719 | - // except add a condition to say to match the current mod |
|
2720 | - if (! isset($query_params['default_where_conditions'])) { |
|
2721 | - $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
2722 | - } |
|
2723 | - $this_model_name = $this->get_this_model_name(); |
|
2724 | - $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
2725 | - $query_params[0][ $this_model_name . "." . $this_pk_field_name ] = $id_or_obj; |
|
2726 | - return $related_model->count($query_params, $field_to_count, $distinct); |
|
2727 | - } |
|
2728 | - |
|
2729 | - |
|
2730 | - /** |
|
2731 | - * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
2732 | - * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
2733 | - * |
|
2734 | - * @param int /EE_Base_Class $id_or_obj |
|
2735 | - * @param string $model_name like 'Event', or 'Registration' |
|
2736 | - * @param array $query_params |
|
2737 | - * @param string $field_to_sum name of field to count by. By default, uses primary key |
|
2738 | - * @return float |
|
2739 | - * @throws EE_Error |
|
2740 | - * @throws ReflectionException |
|
2741 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2742 | - */ |
|
2743 | - public function sum_related($id_or_obj, $model_name, $query_params, $field_to_sum = null) |
|
2744 | - { |
|
2745 | - $related_model = $this->get_related_model_obj($model_name); |
|
2746 | - if (! is_array($query_params)) { |
|
2747 | - EE_Error::doing_it_wrong( |
|
2748 | - 'EEM_Base::sum_related', |
|
2749 | - sprintf( |
|
2750 | - esc_html__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
2751 | - gettype($query_params) |
|
2752 | - ), |
|
2753 | - '4.6.0' |
|
2754 | - ); |
|
2755 | - $query_params = []; |
|
2756 | - } |
|
2757 | - // we're just going to use the query params on the related model's normal get_all query, |
|
2758 | - // except add a condition to say to match the current mod |
|
2759 | - if (! isset($query_params['default_where_conditions'])) { |
|
2760 | - $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
2761 | - } |
|
2762 | - $this_model_name = $this->get_this_model_name(); |
|
2763 | - $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
2764 | - $query_params[0][ $this_model_name . "." . $this_pk_field_name ] = $id_or_obj; |
|
2765 | - return $related_model->sum($query_params, $field_to_sum); |
|
2766 | - } |
|
2767 | - |
|
2768 | - |
|
2769 | - /** |
|
2770 | - * Uses $this->_relatedModels info to find the first related model object of relation $relationName to the given |
|
2771 | - * $modelObject |
|
2772 | - * |
|
2773 | - * @param int | EE_Base_Class $id_or_obj EE_Base_Class child or its ID |
|
2774 | - * @param string $other_model_name , key in $this->_relatedModels, eg 'Registration', or 'Events' |
|
2775 | - * @param array $query_params see github link below for more info |
|
2776 | - * @return EE_Base_Class |
|
2777 | - * @throws EE_Error |
|
2778 | - * @throws ReflectionException |
|
2779 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2780 | - */ |
|
2781 | - public function get_first_related(EE_Base_Class $id_or_obj, $other_model_name, $query_params) |
|
2782 | - { |
|
2783 | - $query_params['limit'] = 1; |
|
2784 | - $results = $this->get_all_related($id_or_obj, $other_model_name, $query_params); |
|
2785 | - if ($results) { |
|
2786 | - return array_shift($results); |
|
2787 | - } |
|
2788 | - return null; |
|
2789 | - } |
|
2790 | - |
|
2791 | - |
|
2792 | - /** |
|
2793 | - * Gets the model's name as it's expected in queries. For example, if this is EEM_Event model, that would be Event |
|
2794 | - * |
|
2795 | - * @return string |
|
2796 | - */ |
|
2797 | - public function get_this_model_name() |
|
2798 | - { |
|
2799 | - return str_replace("EEM_", "", get_class($this)); |
|
2800 | - } |
|
2801 | - |
|
2802 | - |
|
2803 | - /** |
|
2804 | - * Gets the model field on this model which is of type EE_Any_Foreign_Model_Name_Field |
|
2805 | - * |
|
2806 | - * @return EE_Any_Foreign_Model_Name_Field |
|
2807 | - * @throws EE_Error |
|
2808 | - */ |
|
2809 | - public function get_field_containing_related_model_name() |
|
2810 | - { |
|
2811 | - foreach ($this->field_settings(true) as $field) { |
|
2812 | - if ($field instanceof EE_Any_Foreign_Model_Name_Field) { |
|
2813 | - $field_with_model_name = $field; |
|
2814 | - } |
|
2815 | - } |
|
2816 | - if (! isset($field_with_model_name) || ! $field_with_model_name) { |
|
2817 | - throw new EE_Error( |
|
2818 | - sprintf( |
|
2819 | - esc_html__("There is no EE_Any_Foreign_Model_Name field on model %s", "event_espresso"), |
|
2820 | - $this->get_this_model_name() |
|
2821 | - ) |
|
2822 | - ); |
|
2823 | - } |
|
2824 | - return $field_with_model_name; |
|
2825 | - } |
|
2826 | - |
|
2827 | - |
|
2828 | - /** |
|
2829 | - * Inserts a new entry into the database, for each table. |
|
2830 | - * Note: does not add the item to the entity map because that is done by EE_Base_Class::save() right after this. |
|
2831 | - * If client code uses EEM_Base::insert() directly, then although the item isn't in the entity map, |
|
2832 | - * we also know there is no model object with the newly inserted item's ID at the moment (because |
|
2833 | - * if there were, then they would already be in the DB and this would fail); and in the future if someone |
|
2834 | - * creates a model object with this ID (or grabs it from the DB) then it will be added to the |
|
2835 | - * entity map at that time anyways. SO, no need for EEM_Base::insert ot add to the entity map |
|
2836 | - * |
|
2837 | - * @param array $field_n_values keys are field names, values are their values (in the client code's domain if |
|
2838 | - * $values_already_prepared_by_model_object is false, in the model object's domain if |
|
2839 | - * $values_already_prepared_by_model_object is true. See comment about this at the top |
|
2840 | - * of EEM_Base) |
|
2841 | - * @return int|string new primary key on main table that got inserted |
|
2842 | - * @throws EE_Error |
|
2843 | - * @throws ReflectionException |
|
2844 | - */ |
|
2845 | - public function insert($field_n_values) |
|
2846 | - { |
|
2847 | - /** |
|
2848 | - * Filters the fields and their values before inserting an item using the models |
|
2849 | - * |
|
2850 | - * @param array $fields_n_values keys are the fields and values are their new values |
|
2851 | - * @param EEM_Base $model the model used |
|
2852 | - */ |
|
2853 | - $field_n_values = (array) apply_filters('FHEE__EEM_Base__insert__fields_n_values', $field_n_values, $this); |
|
2854 | - if ($this->_satisfies_unique_indexes($field_n_values)) { |
|
2855 | - $main_table = $this->_get_main_table(); |
|
2856 | - $new_id = $this->_insert_into_specific_table($main_table, $field_n_values, false); |
|
2857 | - if ($new_id !== false) { |
|
2858 | - foreach ($this->_get_other_tables() as $other_table) { |
|
2859 | - $this->_insert_into_specific_table($other_table, $field_n_values, $new_id); |
|
2860 | - } |
|
2861 | - } |
|
2862 | - /** |
|
2863 | - * Done just after attempting to insert a new model object |
|
2864 | - * |
|
2865 | - * @param EEM_Base $model used |
|
2866 | - * @param array $fields_n_values fields and their values |
|
2867 | - * @param int|string the ID of the newly-inserted model object |
|
2868 | - */ |
|
2869 | - do_action('AHEE__EEM_Base__insert__end', $this, $field_n_values, $new_id); |
|
2870 | - return $new_id; |
|
2871 | - } |
|
2872 | - return false; |
|
2873 | - } |
|
2874 | - |
|
2875 | - |
|
2876 | - /** |
|
2877 | - * Checks that the result would satisfy the unique indexes on this model |
|
2878 | - * |
|
2879 | - * @param array $field_n_values |
|
2880 | - * @param string $action |
|
2881 | - * @return boolean |
|
2882 | - * @throws EE_Error |
|
2883 | - * @throws ReflectionException |
|
2884 | - */ |
|
2885 | - protected function _satisfies_unique_indexes($field_n_values, $action = 'insert') |
|
2886 | - { |
|
2887 | - foreach ($this->unique_indexes() as $index_name => $index) { |
|
2888 | - $uniqueness_where_params = array_intersect_key($field_n_values, $index->fields()); |
|
2889 | - if ($this->exists([$uniqueness_where_params])) { |
|
2890 | - EE_Error::add_error( |
|
2891 | - sprintf( |
|
2892 | - esc_html__( |
|
2893 | - "Could not %s %s. %s uniqueness index failed. Fields %s must form a unique set, but an entry already exists with values %s.", |
|
2894 | - "event_espresso" |
|
2895 | - ), |
|
2896 | - $action, |
|
2897 | - $this->_get_class_name(), |
|
2898 | - $index_name, |
|
2899 | - implode(",", $index->field_names()), |
|
2900 | - http_build_query($uniqueness_where_params) |
|
2901 | - ), |
|
2902 | - __FILE__, |
|
2903 | - __FUNCTION__, |
|
2904 | - __LINE__ |
|
2905 | - ); |
|
2906 | - return false; |
|
2907 | - } |
|
2908 | - } |
|
2909 | - return true; |
|
2910 | - } |
|
2911 | - |
|
2912 | - |
|
2913 | - /** |
|
2914 | - * Checks the database for an item that conflicts (ie, if this item were |
|
2915 | - * saved to the DB would break some uniqueness requirement, like a primary key |
|
2916 | - * or an index primary key set) with the item specified. $id_obj_or_fields_array |
|
2917 | - * can be either an EE_Base_Class or an array of fields n values |
|
2918 | - * |
|
2919 | - * @param EE_Base_Class|array $obj_or_fields_array |
|
2920 | - * @param boolean $include_primary_key whether to use the model object's primary key |
|
2921 | - * when looking for conflicts |
|
2922 | - * (ie, if false, we ignore the model object's primary key |
|
2923 | - * when finding "conflicts". If true, it's also considered). |
|
2924 | - * Only works for INT primary key, |
|
2925 | - * STRING primary keys cannot be ignored |
|
2926 | - * @return EE_Base_Class|array |
|
2927 | - * @throws EE_Error |
|
2928 | - * @throws ReflectionException |
|
2929 | - */ |
|
2930 | - public function get_one_conflicting($obj_or_fields_array, $include_primary_key = true) |
|
2931 | - { |
|
2932 | - if ($obj_or_fields_array instanceof EE_Base_Class) { |
|
2933 | - $fields_n_values = $obj_or_fields_array->model_field_array(); |
|
2934 | - } elseif (is_array($obj_or_fields_array)) { |
|
2935 | - $fields_n_values = $obj_or_fields_array; |
|
2936 | - } else { |
|
2937 | - throw new EE_Error( |
|
2938 | - sprintf( |
|
2939 | - esc_html__( |
|
2940 | - "%s get_all_conflicting should be called with a model object or an array of field names and values, you provided %d", |
|
2941 | - "event_espresso" |
|
2942 | - ), |
|
2943 | - get_class($this), |
|
2944 | - $obj_or_fields_array |
|
2945 | - ) |
|
2946 | - ); |
|
2947 | - } |
|
2948 | - $query_params = []; |
|
2949 | - if ( |
|
2950 | - $this->has_primary_key_field() |
|
2951 | - && ($include_primary_key |
|
2952 | - || $this->get_primary_key_field() |
|
2953 | - instanceof |
|
2954 | - EE_Primary_Key_String_Field) |
|
2955 | - && isset($fields_n_values[ $this->primary_key_name() ]) |
|
2956 | - ) { |
|
2957 | - $query_params[0]['OR'][ $this->primary_key_name() ] = $fields_n_values[ $this->primary_key_name() ]; |
|
2958 | - } |
|
2959 | - foreach ($this->unique_indexes() as $unique_index_name => $unique_index) { |
|
2960 | - $uniqueness_where_params = |
|
2961 | - array_intersect_key($fields_n_values, $unique_index->fields()); |
|
2962 | - $query_params[0]['OR'][ 'AND*' . $unique_index_name ] = $uniqueness_where_params; |
|
2963 | - } |
|
2964 | - // if there is nothing to base this search on, then we shouldn't find anything |
|
2965 | - if (empty($query_params)) { |
|
2966 | - return []; |
|
2967 | - } |
|
2968 | - return $this->get_one($query_params); |
|
2969 | - } |
|
2970 | - |
|
2971 | - |
|
2972 | - /** |
|
2973 | - * Like count, but is optimized and returns a boolean instead of an int |
|
2974 | - * |
|
2975 | - * @param array $query_params |
|
2976 | - * @return boolean |
|
2977 | - * @throws EE_Error |
|
2978 | - * @throws ReflectionException |
|
2979 | - */ |
|
2980 | - public function exists($query_params) |
|
2981 | - { |
|
2982 | - $query_params['limit'] = 1; |
|
2983 | - return $this->count($query_params) > 0; |
|
2984 | - } |
|
2985 | - |
|
2986 | - |
|
2987 | - /** |
|
2988 | - * Wrapper for exists, except ignores default query parameters so we're only considering ID |
|
2989 | - * |
|
2990 | - * @param int|string $id |
|
2991 | - * @return boolean |
|
2992 | - * @throws EE_Error |
|
2993 | - * @throws ReflectionException |
|
2994 | - */ |
|
2995 | - public function exists_by_ID($id) |
|
2996 | - { |
|
2997 | - return $this->exists( |
|
2998 | - [ |
|
2999 | - 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
3000 | - [ |
|
3001 | - $this->primary_key_name() => $id, |
|
3002 | - ], |
|
3003 | - ] |
|
3004 | - ); |
|
3005 | - } |
|
3006 | - |
|
3007 | - |
|
3008 | - /** |
|
3009 | - * Inserts a new row in $table, using the $cols_n_values which apply to that table. |
|
3010 | - * If a $new_id is supplied and if $table is an EE_Other_Table, we assume |
|
3011 | - * we need to add a foreign key column to point to $new_id (which should be the primary key's value |
|
3012 | - * on the main table) |
|
3013 | - * This is protected rather than private because private is not accessible to any child methods and there MAY be |
|
3014 | - * cases where we want to call it directly rather than via insert(). |
|
3015 | - * |
|
3016 | - * @access protected |
|
3017 | - * @param EE_Table_Base $table |
|
3018 | - * @param array $fields_n_values each key should be in field's keys, and value should be an int, string or |
|
3019 | - * float |
|
3020 | - * @param int $new_id for now we assume only int keys |
|
3021 | - * @return int ID of new row inserted, or FALSE on failure |
|
3022 | - * @throws EE_Error |
|
3023 | - * @throws ReflectionException |
|
3024 | - * @global WPDB $wpdb only used to get the $wpdb->insert_id after performing an insert |
|
3025 | - */ |
|
3026 | - protected function _insert_into_specific_table(EE_Table_Base $table, $fields_n_values, $new_id = 0) |
|
3027 | - { |
|
3028 | - global $wpdb; |
|
3029 | - $insertion_col_n_values = []; |
|
3030 | - $format_for_insertion = []; |
|
3031 | - $fields_on_table = $this->_get_fields_for_table($table->get_table_alias()); |
|
3032 | - foreach ($fields_on_table as $field_obj) { |
|
3033 | - // check if its an auto-incrementing column, in which case we should just leave it to do its autoincrement thing |
|
3034 | - if ($field_obj->is_auto_increment()) { |
|
3035 | - continue; |
|
3036 | - } |
|
3037 | - $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
3038 | - // if the value we want to assign it to is NULL, just don't mention it for the insertion |
|
3039 | - if ($prepared_value !== null) { |
|
3040 | - $insertion_col_n_values[ $field_obj->get_table_column() ] = $prepared_value; |
|
3041 | - $format_for_insertion[] = $field_obj->get_wpdb_data_type(); |
|
3042 | - } |
|
3043 | - } |
|
3044 | - if ($table instanceof EE_Secondary_Table && $new_id) { |
|
3045 | - // its not the main table, so we should have already saved the main table's PK which we just inserted |
|
3046 | - // so add the fk to the main table as a column |
|
3047 | - $insertion_col_n_values[ $table->get_fk_on_table() ] = $new_id; |
|
3048 | - $format_for_insertion[] = |
|
3049 | - '%d';// yes right now we're only allowing these foreign keys to be INTs |
|
3050 | - } |
|
3051 | - // insert the new entry |
|
3052 | - $result = $this->_do_wpdb_query( |
|
3053 | - 'insert', |
|
3054 | - [$table->get_table_name(), $insertion_col_n_values, $format_for_insertion] |
|
3055 | - ); |
|
3056 | - if ($result === false) { |
|
3057 | - return false; |
|
3058 | - } |
|
3059 | - // ok, now what do we return for the ID of the newly-inserted thing? |
|
3060 | - if ($this->has_primary_key_field()) { |
|
3061 | - if ($this->get_primary_key_field()->is_auto_increment()) { |
|
3062 | - return $wpdb->insert_id; |
|
3063 | - } |
|
3064 | - // it's not an auto-increment primary key, so |
|
3065 | - // it must have been supplied |
|
3066 | - return $fields_n_values[ $this->get_primary_key_field()->get_name() ]; |
|
3067 | - } |
|
3068 | - // we can't return a primary key because there is none. instead return |
|
3069 | - // a unique string indicating this model |
|
3070 | - return $this->get_index_primary_key_string($fields_n_values); |
|
3071 | - } |
|
3072 | - |
|
3073 | - |
|
3074 | - /** |
|
3075 | - * Prepare the $field_obj 's value in $fields_n_values for use in the database. |
|
3076 | - * If the field doesn't allow NULL, try to use its default. (If it doesn't allow NULL, |
|
3077 | - * and there is no default, we pass it along. WPDB will take care of it) |
|
3078 | - * |
|
3079 | - * @param EE_Model_Field_Base $field_obj |
|
3080 | - * @param array $fields_n_values |
|
3081 | - * @return mixed string|int|float depending on what the table column will be expecting |
|
3082 | - * @throws EE_Error |
|
3083 | - */ |
|
3084 | - protected function _prepare_value_or_use_default($field_obj, $fields_n_values) |
|
3085 | - { |
|
3086 | - // if this field doesn't allow nullable, don't allow it |
|
3087 | - if ( |
|
3088 | - ! $field_obj->is_nullable() |
|
3089 | - && ( |
|
3090 | - ! isset($fields_n_values[ $field_obj->get_name() ]) |
|
3091 | - || $fields_n_values[ $field_obj->get_name() ] === null |
|
3092 | - ) |
|
3093 | - ) { |
|
3094 | - $fields_n_values[ $field_obj->get_name() ] = $field_obj->get_default_value(); |
|
3095 | - } |
|
3096 | - $unprepared_value = isset($fields_n_values[ $field_obj->get_name() ]) |
|
3097 | - ? $fields_n_values[ $field_obj->get_name() ] |
|
3098 | - : null; |
|
3099 | - return $this->_prepare_value_for_use_in_db($unprepared_value, $field_obj); |
|
3100 | - } |
|
3101 | - |
|
3102 | - |
|
3103 | - /** |
|
3104 | - * Consolidates code for preparing a value supplied to the model for use int eh db. Calls the field's |
|
3105 | - * prepare_for_use_in_db method on the value, and depending on $value_already_prepare_by_model_obj, may also call |
|
3106 | - * the field's prepare_for_set() method. |
|
3107 | - * |
|
3108 | - * @param mixed $value value in the client code domain if $value_already_prepared_by_model_object is |
|
3109 | - * false, otherwise a value in the model object's domain (see lengthy comment at |
|
3110 | - * top of file) |
|
3111 | - * @param EE_Model_Field_Base $field field which will be doing the preparing of the value. If null, we assume |
|
3112 | - * $value is a custom selection |
|
3113 | - * @return mixed a value ready for use in the database for insertions, updating, or in a where clause |
|
3114 | - */ |
|
3115 | - private function _prepare_value_for_use_in_db($value, $field) |
|
3116 | - { |
|
3117 | - if ($field && $field instanceof EE_Model_Field_Base) { |
|
3118 | - // phpcs:disable PSR2.ControlStructures.SwitchDeclaration.TerminatingComment |
|
3119 | - switch ($this->_values_already_prepared_by_model_object) { |
|
3120 | - /** @noinspection PhpMissingBreakStatementInspection */ |
|
3121 | - case self::not_prepared_by_model_object: |
|
3122 | - $value = $field->prepare_for_set($value); |
|
3123 | - // purposefully left out "return" |
|
3124 | - case self::prepared_by_model_object: |
|
3125 | - /** @noinspection SuspiciousAssignmentsInspection */ |
|
3126 | - $value = $field->prepare_for_use_in_db($value); |
|
3127 | - case self::prepared_for_use_in_db: |
|
3128 | - // leave the value alone |
|
3129 | - } |
|
3130 | - return $value; |
|
3131 | - // phpcs:enable |
|
3132 | - } |
|
3133 | - return $value; |
|
3134 | - } |
|
3135 | - |
|
3136 | - |
|
3137 | - /** |
|
3138 | - * Returns the main table on this model |
|
3139 | - * |
|
3140 | - * @return EE_Primary_Table |
|
3141 | - * @throws EE_Error |
|
3142 | - */ |
|
3143 | - protected function _get_main_table() |
|
3144 | - { |
|
3145 | - foreach ($this->_tables as $table) { |
|
3146 | - if ($table instanceof EE_Primary_Table) { |
|
3147 | - return $table; |
|
3148 | - } |
|
3149 | - } |
|
3150 | - throw new EE_Error( |
|
3151 | - sprintf( |
|
3152 | - esc_html__( |
|
3153 | - 'There are no main tables on %s. They should be added to _tables array in the constructor', |
|
3154 | - 'event_espresso' |
|
3155 | - ), |
|
3156 | - get_class($this) |
|
3157 | - ) |
|
3158 | - ); |
|
3159 | - } |
|
3160 | - |
|
3161 | - |
|
3162 | - /** |
|
3163 | - * table |
|
3164 | - * returns EE_Primary_Table table name |
|
3165 | - * |
|
3166 | - * @return string |
|
3167 | - * @throws EE_Error |
|
3168 | - */ |
|
3169 | - public function table() |
|
3170 | - { |
|
3171 | - return $this->_get_main_table()->get_table_name(); |
|
3172 | - } |
|
3173 | - |
|
3174 | - |
|
3175 | - /** |
|
3176 | - * table |
|
3177 | - * returns first EE_Secondary_Table table name |
|
3178 | - * |
|
3179 | - * @return string |
|
3180 | - */ |
|
3181 | - public function second_table() |
|
3182 | - { |
|
3183 | - // grab second table from tables array |
|
3184 | - $second_table = end($this->_tables); |
|
3185 | - return $second_table instanceof EE_Secondary_Table ? $second_table->get_table_name() : null; |
|
3186 | - } |
|
3187 | - |
|
3188 | - |
|
3189 | - /** |
|
3190 | - * get_table_obj_by_alias |
|
3191 | - * returns table name given it's alias |
|
3192 | - * |
|
3193 | - * @param string $table_alias |
|
3194 | - * @return EE_Primary_Table | EE_Secondary_Table |
|
3195 | - */ |
|
3196 | - public function get_table_obj_by_alias($table_alias = '') |
|
3197 | - { |
|
3198 | - return isset($this->_tables[ $table_alias ]) ? $this->_tables[ $table_alias ] : null; |
|
3199 | - } |
|
3200 | - |
|
3201 | - |
|
3202 | - /** |
|
3203 | - * Gets all the tables of type EE_Other_Table from EEM_CPT_Basel_Model::_tables |
|
3204 | - * |
|
3205 | - * @return EE_Secondary_Table[] |
|
3206 | - */ |
|
3207 | - protected function _get_other_tables() |
|
3208 | - { |
|
3209 | - $other_tables = []; |
|
3210 | - foreach ($this->_tables as $table_alias => $table) { |
|
3211 | - if ($table instanceof EE_Secondary_Table) { |
|
3212 | - $other_tables[ $table_alias ] = $table; |
|
3213 | - } |
|
3214 | - } |
|
3215 | - return $other_tables; |
|
3216 | - } |
|
3217 | - |
|
3218 | - |
|
3219 | - /** |
|
3220 | - * Finds all the fields that correspond to the given table |
|
3221 | - * |
|
3222 | - * @param string $table_alias , array key in EEM_Base::_tables |
|
3223 | - * @return EE_Model_Field_Base[] |
|
3224 | - */ |
|
3225 | - public function _get_fields_for_table($table_alias) |
|
3226 | - { |
|
3227 | - return $this->_fields[ $table_alias ]; |
|
3228 | - } |
|
3229 | - |
|
3230 | - |
|
3231 | - /** |
|
3232 | - * Recurses through all the where parameters, and finds all the related models we'll need |
|
3233 | - * to complete this query. Eg, given where parameters like array('EVT_ID'=>3) from within Event model, we won't |
|
3234 | - * need any related models. But if the array were array('Registrations.REG_ID'=>3), we'd need the related |
|
3235 | - * Registration model. If it were array('Registrations.Transactions.Payments.PAY_ID'=>3), then we'd need the |
|
3236 | - * related Registration, Transaction, and Payment models. |
|
3237 | - * |
|
3238 | - * @param array $query_params see github link below for more info |
|
3239 | - * @return EE_Model_Query_Info_Carrier |
|
3240 | - * @throws EE_Error |
|
3241 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
3242 | - */ |
|
3243 | - public function _extract_related_models_from_query($query_params) |
|
3244 | - { |
|
3245 | - $query_info_carrier = new EE_Model_Query_Info_Carrier(); |
|
3246 | - if (array_key_exists(0, $query_params)) { |
|
3247 | - $this->_extract_related_models_from_sub_params_array_keys($query_params[0], $query_info_carrier, 0); |
|
3248 | - } |
|
3249 | - if (array_key_exists('group_by', $query_params)) { |
|
3250 | - if (is_array($query_params['group_by'])) { |
|
3251 | - $this->_extract_related_models_from_sub_params_array_values( |
|
3252 | - $query_params['group_by'], |
|
3253 | - $query_info_carrier, |
|
3254 | - 'group_by' |
|
3255 | - ); |
|
3256 | - } elseif (! empty($query_params['group_by'])) { |
|
3257 | - $this->_extract_related_model_info_from_query_param( |
|
3258 | - $query_params['group_by'], |
|
3259 | - $query_info_carrier, |
|
3260 | - 'group_by' |
|
3261 | - ); |
|
3262 | - } |
|
3263 | - } |
|
3264 | - if (array_key_exists('having', $query_params)) { |
|
3265 | - $this->_extract_related_models_from_sub_params_array_keys( |
|
3266 | - $query_params[0], |
|
3267 | - $query_info_carrier, |
|
3268 | - 'having' |
|
3269 | - ); |
|
3270 | - } |
|
3271 | - if (array_key_exists('order_by', $query_params)) { |
|
3272 | - if (is_array($query_params['order_by'])) { |
|
3273 | - $this->_extract_related_models_from_sub_params_array_keys( |
|
3274 | - $query_params['order_by'], |
|
3275 | - $query_info_carrier, |
|
3276 | - 'order_by' |
|
3277 | - ); |
|
3278 | - } elseif (! empty($query_params['order_by'])) { |
|
3279 | - $this->_extract_related_model_info_from_query_param( |
|
3280 | - $query_params['order_by'], |
|
3281 | - $query_info_carrier, |
|
3282 | - 'order_by' |
|
3283 | - ); |
|
3284 | - } |
|
3285 | - } |
|
3286 | - if (array_key_exists('force_join', $query_params)) { |
|
3287 | - $this->_extract_related_models_from_sub_params_array_values( |
|
3288 | - $query_params['force_join'], |
|
3289 | - $query_info_carrier, |
|
3290 | - 'force_join' |
|
3291 | - ); |
|
3292 | - } |
|
3293 | - $this->extractRelatedModelsFromCustomSelects($query_info_carrier); |
|
3294 | - return $query_info_carrier; |
|
3295 | - } |
|
3296 | - |
|
3297 | - |
|
3298 | - /** |
|
3299 | - * For extracting related models from WHERE (0), HAVING (having), ORDER BY (order_by) or forced joins (force_join) |
|
3300 | - * |
|
3301 | - * @param array $sub_query_params |
|
3302 | - * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
3303 | - * @param string $query_param_type one of $this->_allowed_query_params |
|
3304 | - * @return EE_Model_Query_Info_Carrier |
|
3305 | - * @throws EE_Error |
|
3306 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#-0-where-conditions |
|
3307 | - */ |
|
3308 | - private function _extract_related_models_from_sub_params_array_keys( |
|
3309 | - $sub_query_params, |
|
3310 | - EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
3311 | - $query_param_type |
|
3312 | - ) { |
|
3313 | - if (! empty($sub_query_params)) { |
|
3314 | - $sub_query_params = (array) $sub_query_params; |
|
3315 | - foreach ($sub_query_params as $param => $possibly_array_of_params) { |
|
3316 | - // $param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
3317 | - $this->_extract_related_model_info_from_query_param( |
|
3318 | - $param, |
|
3319 | - $model_query_info_carrier, |
|
3320 | - $query_param_type |
|
3321 | - ); |
|
3322 | - // if $possibly_array_of_params is an array, try recursing into it, searching for keys which |
|
3323 | - // indicate needed joins. Eg, array('NOT'=>array('Registration.TXN_ID'=>23)). In this case, we tried |
|
3324 | - // extracting models out of the 'NOT', which obviously wasn't successful, and then we recurse into the value |
|
3325 | - // of array('Registration.TXN_ID'=>23) |
|
3326 | - $query_param_sans_stars = |
|
3327 | - $this->_remove_stars_and_anything_after_from_condition_query_param_key($param); |
|
3328 | - if (in_array($query_param_sans_stars, $this->_logic_query_param_keys, true)) { |
|
3329 | - if (! is_array($possibly_array_of_params)) { |
|
3330 | - throw new EE_Error( |
|
3331 | - sprintf( |
|
3332 | - esc_html__( |
|
3333 | - "You used a special where query param %s, but the value isn't an array of where query params, it's just %s'. It should be an array, eg array('EVT_ID'=>23,'OR'=>array('Venue.VNU_ID'=>32,'Venue.VNU_name'=>'monkey_land'))", |
|
3334 | - "event_espresso" |
|
3335 | - ), |
|
3336 | - $param, |
|
3337 | - $possibly_array_of_params |
|
3338 | - ) |
|
3339 | - ); |
|
3340 | - } |
|
3341 | - $this->_extract_related_models_from_sub_params_array_keys( |
|
3342 | - $possibly_array_of_params, |
|
3343 | - $model_query_info_carrier, |
|
3344 | - $query_param_type |
|
3345 | - ); |
|
3346 | - } elseif ( |
|
3347 | - $query_param_type === 0 // ie WHERE |
|
3348 | - && is_array($possibly_array_of_params) |
|
3349 | - && isset($possibly_array_of_params[2]) |
|
3350 | - && $possibly_array_of_params[2] == true |
|
3351 | - ) { |
|
3352 | - // then $possible_array_of_params looks something like array('<','DTT_sold',true) |
|
3353 | - // indicating that $possible_array_of_params[1] is actually a field name, |
|
3354 | - // from which we should extract query parameters! |
|
3355 | - if (! isset($possibly_array_of_params[0], $possibly_array_of_params[1])) { |
|
3356 | - throw new EE_Error( |
|
3357 | - sprintf( |
|
3358 | - esc_html__( |
|
3359 | - "Improperly formed query parameter %s. It should be numerically indexed like array('<','DTT_sold',true); but you provided %s", |
|
3360 | - "event_espresso" |
|
3361 | - ), |
|
3362 | - $query_param_type, |
|
3363 | - implode(",", $possibly_array_of_params) |
|
3364 | - ) |
|
3365 | - ); |
|
3366 | - } |
|
3367 | - $this->_extract_related_model_info_from_query_param( |
|
3368 | - $possibly_array_of_params[1], |
|
3369 | - $model_query_info_carrier, |
|
3370 | - $query_param_type |
|
3371 | - ); |
|
3372 | - } |
|
3373 | - } |
|
3374 | - } |
|
3375 | - } |
|
3376 | - |
|
3377 | - |
|
3378 | - /** |
|
3379 | - * For extracting related models from forced_joins, where the array values contain the info about what |
|
3380 | - * models to join with. Eg an array like array('Attendee','Price.Price_Type'); |
|
3381 | - * |
|
3382 | - * @param array $sub_query_params |
|
3383 | - * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
3384 | - * @param string $query_param_type one of $this->_allowed_query_params |
|
3385 | - * @return EE_Model_Query_Info_Carrier |
|
3386 | - * @throws EE_Error |
|
3387 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
3388 | - */ |
|
3389 | - private function _extract_related_models_from_sub_params_array_values( |
|
3390 | - $sub_query_params, |
|
3391 | - EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
3392 | - $query_param_type |
|
3393 | - ) { |
|
3394 | - if (! empty($sub_query_params)) { |
|
3395 | - if (! is_array($sub_query_params)) { |
|
3396 | - throw new EE_Error( |
|
3397 | - sprintf( |
|
3398 | - esc_html__("Query parameter %s should be an array, but it isn't.", "event_espresso"), |
|
3399 | - $sub_query_params |
|
3400 | - ) |
|
3401 | - ); |
|
3402 | - } |
|
3403 | - foreach ($sub_query_params as $param) { |
|
3404 | - // $param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
3405 | - $this->_extract_related_model_info_from_query_param( |
|
3406 | - $param, |
|
3407 | - $model_query_info_carrier, |
|
3408 | - $query_param_type |
|
3409 | - ); |
|
3410 | - } |
|
3411 | - } |
|
3412 | - } |
|
3413 | - |
|
3414 | - |
|
3415 | - /** |
|
3416 | - * Extract all the query parts from model query params |
|
3417 | - * and put into a EEM_Related_Model_Info_Carrier for easy extraction into a query. We create this object |
|
3418 | - * instead of directly constructing the SQL because often we need to extract info from the $query_params |
|
3419 | - * but use them in a different order. Eg, we need to know what models we are querying |
|
3420 | - * before we know what joins to perform. However, we need to know what data types correspond to which fields on |
|
3421 | - * other models before we can finalize the where clause SQL. |
|
3422 | - * |
|
3423 | - * @param array $query_params see github link below for more info |
|
3424 | - * @return EE_Model_Query_Info_Carrier |
|
3425 | - * @throws EE_Error |
|
3426 | - * @throws ModelConfigurationException |
|
3427 | - * @throws ReflectionException |
|
3428 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
3429 | - */ |
|
3430 | - public function _create_model_query_info_carrier($query_params) |
|
3431 | - { |
|
3432 | - if (! is_array($query_params)) { |
|
3433 | - EE_Error::doing_it_wrong( |
|
3434 | - 'EEM_Base::_create_model_query_info_carrier', |
|
3435 | - sprintf( |
|
3436 | - esc_html__( |
|
3437 | - '$query_params should be an array, you passed a variable of type %s', |
|
3438 | - 'event_espresso' |
|
3439 | - ), |
|
3440 | - gettype($query_params) |
|
3441 | - ), |
|
3442 | - '4.6.0' |
|
3443 | - ); |
|
3444 | - $query_params = []; |
|
3445 | - } |
|
3446 | - $query_params[0] = isset($query_params[0]) ? $query_params[0] : []; |
|
3447 | - // first check if we should alter the query to account for caps or not |
|
3448 | - // because the caps might require us to do extra joins |
|
3449 | - if (isset($query_params['caps']) && $query_params['caps'] !== 'none') { |
|
3450 | - $query_params[0] = array_replace_recursive( |
|
3451 | - $query_params[0], |
|
3452 | - $this->caps_where_conditions( |
|
3453 | - $query_params['caps'] |
|
3454 | - ) |
|
3455 | - ); |
|
3456 | - } |
|
3457 | - |
|
3458 | - // check if we should alter the query to remove data related to protected |
|
3459 | - // custom post types |
|
3460 | - if (isset($query_params['exclude_protected']) && $query_params['exclude_protected'] === true) { |
|
3461 | - $where_param_key_for_password = $this->modelChainAndPassword(); |
|
3462 | - // only include if related to a cpt where no password has been set |
|
3463 | - $query_params[0]['OR*nopassword'] = [ |
|
3464 | - $where_param_key_for_password => '', |
|
3465 | - $where_param_key_for_password . '*' => ['IS_NULL'], |
|
3466 | - ]; |
|
3467 | - } |
|
3468 | - $query_object = $this->_extract_related_models_from_query($query_params); |
|
3469 | - // verify where_query_params has NO numeric indexes.... that's simply not how you use it! |
|
3470 | - foreach ($query_params[0] as $key => $value) { |
|
3471 | - if (is_int($key)) { |
|
3472 | - throw new EE_Error( |
|
3473 | - sprintf( |
|
3474 | - esc_html__( |
|
3475 | - "WHERE query params must NOT be numerically-indexed. You provided the array key '%s' for value '%s' while querying model %s. All the query params provided were '%s' Please read documentation on EEM_Base::get_all.", |
|
3476 | - "event_espresso" |
|
3477 | - ), |
|
3478 | - $key, |
|
3479 | - var_export($value, true), |
|
3480 | - var_export($query_params, true), |
|
3481 | - get_class($this) |
|
3482 | - ) |
|
3483 | - ); |
|
3484 | - } |
|
3485 | - } |
|
3486 | - if ( |
|
3487 | - array_key_exists('default_where_conditions', $query_params) |
|
3488 | - && ! empty($query_params['default_where_conditions']) |
|
3489 | - ) { |
|
3490 | - $use_default_where_conditions = $query_params['default_where_conditions']; |
|
3491 | - } else { |
|
3492 | - $use_default_where_conditions = EEM_Base::default_where_conditions_all; |
|
3493 | - } |
|
3494 | - $query_params[0] = array_merge( |
|
3495 | - $this->_get_default_where_conditions_for_models_in_query( |
|
3496 | - $query_object, |
|
3497 | - $use_default_where_conditions, |
|
3498 | - $query_params[0] |
|
3499 | - ), |
|
3500 | - $query_params[0] |
|
3501 | - ); |
|
3502 | - $query_object->set_where_sql($this->_construct_where_clause($query_params[0])); |
|
3503 | - // if this is a "on_join_limit" then we are limiting on on a specific table in a multi_table join. |
|
3504 | - // So we need to setup a subquery and use that for the main join. |
|
3505 | - // Note for now this only works on the primary table for the model. |
|
3506 | - // So for instance, you could set the limit array like this: |
|
3507 | - // array( 'on_join_limit' => array('Primary_Table_Alias', array(1,10) ) ) |
|
3508 | - if (array_key_exists('on_join_limit', $query_params) && ! empty($query_params['on_join_limit'])) { |
|
3509 | - $query_object->set_main_model_join_sql( |
|
3510 | - $this->_construct_limit_join_select( |
|
3511 | - $query_params['on_join_limit'][0], |
|
3512 | - $query_params['on_join_limit'][1] |
|
3513 | - ) |
|
3514 | - ); |
|
3515 | - } |
|
3516 | - // set limit |
|
3517 | - if (array_key_exists('limit', $query_params)) { |
|
3518 | - if (is_array($query_params['limit'])) { |
|
3519 | - if (! isset($query_params['limit'][0], $query_params['limit'][1])) { |
|
3520 | - $e = sprintf( |
|
3521 | - esc_html__( |
|
3522 | - "Invalid DB query. You passed '%s' for the LIMIT, but only the following are valid: an integer, string representing an integer, a string like 'int,int', or an array like array(int,int)", |
|
3523 | - "event_espresso" |
|
3524 | - ), |
|
3525 | - http_build_query($query_params['limit']) |
|
3526 | - ); |
|
3527 | - throw new EE_Error($e . "|" . $e); |
|
3528 | - } |
|
3529 | - // they passed us an array for the limit. Assume it's like array(50,25), meaning offset by 50, and get 25 |
|
3530 | - $query_object->set_limit_sql(" LIMIT " . $query_params['limit'][0] . "," . $query_params['limit'][1]); |
|
3531 | - } elseif (! empty($query_params['limit'])) { |
|
3532 | - $query_object->set_limit_sql(" LIMIT " . $query_params['limit']); |
|
3533 | - } |
|
3534 | - } |
|
3535 | - // set order by |
|
3536 | - if (array_key_exists('order_by', $query_params)) { |
|
3537 | - if (is_array($query_params['order_by'])) { |
|
3538 | - // if they're using 'order_by' as an array, they can't use 'order' (because 'order_by' must |
|
3539 | - // specify whether to ascend or descend on each field. Eg 'order_by'=>array('EVT_ID'=>'ASC'). So |
|
3540 | - // including 'order' wouldn't make any sense if 'order_by' has already specified which way to order! |
|
3541 | - if (array_key_exists('order', $query_params)) { |
|
3542 | - throw new EE_Error( |
|
3543 | - sprintf( |
|
3544 | - esc_html__( |
|
3545 | - "In querying %s, we are using query parameter 'order_by' as an array (keys:%s,values:%s), and so we can't use query parameter 'order' (value %s). You should just use the 'order_by' parameter ", |
|
3546 | - "event_espresso" |
|
3547 | - ), |
|
3548 | - get_class($this), |
|
3549 | - implode(", ", array_keys($query_params['order_by'])), |
|
3550 | - implode(", ", $query_params['order_by']), |
|
3551 | - $query_params['order'] |
|
3552 | - ) |
|
3553 | - ); |
|
3554 | - } |
|
3555 | - $this->_extract_related_models_from_sub_params_array_keys( |
|
3556 | - $query_params['order_by'], |
|
3557 | - $query_object, |
|
3558 | - 'order_by' |
|
3559 | - ); |
|
3560 | - // assume it's an array of fields to order by |
|
3561 | - $order_array = []; |
|
3562 | - foreach ($query_params['order_by'] as $field_name_to_order_by => $order) { |
|
3563 | - $order = $this->_extract_order($order); |
|
3564 | - $order_array[] = $this->_deduce_column_name_from_query_param($field_name_to_order_by) . SP . $order; |
|
3565 | - } |
|
3566 | - $query_object->set_order_by_sql(" ORDER BY " . implode(",", $order_array)); |
|
3567 | - } elseif (! empty($query_params['order_by'])) { |
|
3568 | - $this->_extract_related_model_info_from_query_param( |
|
3569 | - $query_params['order_by'], |
|
3570 | - $query_object, |
|
3571 | - 'order', |
|
3572 | - $query_params['order_by'] |
|
3573 | - ); |
|
3574 | - $order = isset($query_params['order']) |
|
3575 | - ? $this->_extract_order($query_params['order']) |
|
3576 | - : 'DESC'; |
|
3577 | - $query_object->set_order_by_sql( |
|
3578 | - " ORDER BY " . $this->_deduce_column_name_from_query_param($query_params['order_by']) . SP . $order |
|
3579 | - ); |
|
3580 | - } |
|
3581 | - } |
|
3582 | - // if 'order_by' wasn't set, maybe they are just using 'order' on its own? |
|
3583 | - if ( |
|
3584 | - ! array_key_exists('order_by', $query_params) |
|
3585 | - && array_key_exists('order', $query_params) |
|
3586 | - && ! empty($query_params['order']) |
|
3587 | - ) { |
|
3588 | - $pk_field = $this->get_primary_key_field(); |
|
3589 | - $order = $this->_extract_order($query_params['order']); |
|
3590 | - $query_object->set_order_by_sql(" ORDER BY " . $pk_field->get_qualified_column() . SP . $order); |
|
3591 | - } |
|
3592 | - // set group by |
|
3593 | - if (array_key_exists('group_by', $query_params)) { |
|
3594 | - if (is_array($query_params['group_by'])) { |
|
3595 | - // it's an array, so assume we'll be grouping by a bunch of stuff |
|
3596 | - $group_by_array = []; |
|
3597 | - foreach ($query_params['group_by'] as $field_name_to_group_by) { |
|
3598 | - $group_by_array[] = $this->_deduce_column_name_from_query_param($field_name_to_group_by); |
|
3599 | - } |
|
3600 | - $query_object->set_group_by_sql(" GROUP BY " . implode(", ", $group_by_array)); |
|
3601 | - } elseif (! empty($query_params['group_by'])) { |
|
3602 | - $query_object->set_group_by_sql( |
|
3603 | - " GROUP BY " . $this->_deduce_column_name_from_query_param($query_params['group_by']) |
|
3604 | - ); |
|
3605 | - } |
|
3606 | - } |
|
3607 | - // set having |
|
3608 | - if (array_key_exists('having', $query_params) && $query_params['having']) { |
|
3609 | - $query_object->set_having_sql($this->_construct_having_clause($query_params['having'])); |
|
3610 | - } |
|
3611 | - // now, just verify they didn't pass anything wack |
|
3612 | - foreach ($query_params as $query_key => $query_value) { |
|
3613 | - if (! in_array($query_key, $this->_allowed_query_params, true)) { |
|
3614 | - throw new EE_Error( |
|
3615 | - sprintf( |
|
3616 | - esc_html__( |
|
3617 | - "You passed %s as a query parameter to %s, which is illegal! The allowed query parameters are %s", |
|
3618 | - 'event_espresso' |
|
3619 | - ), |
|
3620 | - $query_key, |
|
3621 | - get_class($this), |
|
3622 | - // print_r( $this->_allowed_query_params, TRUE ) |
|
3623 | - implode(',', $this->_allowed_query_params) |
|
3624 | - ) |
|
3625 | - ); |
|
3626 | - } |
|
3627 | - } |
|
3628 | - $main_model_join_sql = $query_object->get_main_model_join_sql(); |
|
3629 | - if (empty($main_model_join_sql)) { |
|
3630 | - $query_object->set_main_model_join_sql($this->_construct_internal_join()); |
|
3631 | - } |
|
3632 | - return $query_object; |
|
3633 | - } |
|
3634 | - |
|
3635 | - |
|
3636 | - /** |
|
3637 | - * Gets the where conditions that should be imposed on the query based on the |
|
3638 | - * context (eg reading frontend, backend, edit or delete). |
|
3639 | - * |
|
3640 | - * @param string $context one of EEM_Base::valid_cap_contexts() |
|
3641 | - * @return array |
|
3642 | - * @throws EE_Error |
|
3643 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
3644 | - */ |
|
3645 | - public function caps_where_conditions($context = self::caps_read) |
|
3646 | - { |
|
3647 | - EEM_Base::verify_is_valid_cap_context($context); |
|
3648 | - $cap_where_conditions = []; |
|
3649 | - $cap_restrictions = $this->caps_missing($context); |
|
3650 | - foreach ($cap_restrictions as $restriction_if_no_cap) { |
|
3651 | - $cap_where_conditions = array_replace_recursive( |
|
3652 | - $cap_where_conditions, |
|
3653 | - $restriction_if_no_cap->get_default_where_conditions() |
|
3654 | - ); |
|
3655 | - } |
|
3656 | - return apply_filters( |
|
3657 | - 'FHEE__EEM_Base__caps_where_conditions__return', |
|
3658 | - $cap_where_conditions, |
|
3659 | - $this, |
|
3660 | - $context, |
|
3661 | - $cap_restrictions |
|
3662 | - ); |
|
3663 | - } |
|
3664 | - |
|
3665 | - |
|
3666 | - /** |
|
3667 | - * Verifies that $should_be_order_string is in $this->_allowed_order_values, |
|
3668 | - * otherwise throws an exception |
|
3669 | - * |
|
3670 | - * @param string $should_be_order_string |
|
3671 | - * @return string either ASC, asc, DESC or desc |
|
3672 | - * @throws EE_Error |
|
3673 | - */ |
|
3674 | - private function _extract_order($should_be_order_string) |
|
3675 | - { |
|
3676 | - if (in_array($should_be_order_string, $this->_allowed_order_values)) { |
|
3677 | - return $should_be_order_string; |
|
3678 | - } |
|
3679 | - throw new EE_Error( |
|
3680 | - sprintf( |
|
3681 | - esc_html__( |
|
3682 | - "While performing a query on '%s', tried to use '%s' as an order parameter. ", |
|
3683 | - "event_espresso" |
|
3684 | - ), |
|
3685 | - get_class($this), |
|
3686 | - $should_be_order_string |
|
3687 | - ) |
|
3688 | - ); |
|
3689 | - } |
|
3690 | - |
|
3691 | - |
|
3692 | - /** |
|
3693 | - * Looks at all the models which are included in this query, and asks each |
|
3694 | - * for their universal_where_params, and returns them in the same format as $query_params[0] (where), |
|
3695 | - * so they can be merged |
|
3696 | - * |
|
3697 | - * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
3698 | - * @param string $use_default_where_conditions can be 'none','other_models_only', or 'all'. |
|
3699 | - * 'none' means NO default where conditions will |
|
3700 | - * be used AT ALL during this query. |
|
3701 | - * 'other_models_only' means default where |
|
3702 | - * conditions from other models will be used, but |
|
3703 | - * not for this primary model. 'all', the default, |
|
3704 | - * means default where conditions will apply as |
|
3705 | - * normal |
|
3706 | - * @param array $where_query_params |
|
3707 | - * @return array |
|
3708 | - * @throws EE_Error |
|
3709 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
3710 | - */ |
|
3711 | - private function _get_default_where_conditions_for_models_in_query( |
|
3712 | - EE_Model_Query_Info_Carrier $query_info_carrier, |
|
3713 | - $use_default_where_conditions = EEM_Base::default_where_conditions_all, |
|
3714 | - $where_query_params = [] |
|
3715 | - ) { |
|
3716 | - $allowed_used_default_where_conditions_values = EEM_Base::valid_default_where_conditions(); |
|
3717 | - if (! in_array($use_default_where_conditions, $allowed_used_default_where_conditions_values)) { |
|
3718 | - throw new EE_Error( |
|
3719 | - sprintf( |
|
3720 | - esc_html__( |
|
3721 | - "You passed an invalid value to the query parameter 'default_where_conditions' of '%s'. Allowed values are %s", |
|
3722 | - "event_espresso" |
|
3723 | - ), |
|
3724 | - $use_default_where_conditions, |
|
3725 | - implode(", ", $allowed_used_default_where_conditions_values) |
|
3726 | - ) |
|
3727 | - ); |
|
3728 | - } |
|
3729 | - $universal_query_params = []; |
|
3730 | - if ($this->_should_use_default_where_conditions($use_default_where_conditions)) { |
|
3731 | - $universal_query_params = $this->_get_default_where_conditions(); |
|
3732 | - } elseif ($this->_should_use_minimum_where_conditions($use_default_where_conditions)) { |
|
3733 | - $universal_query_params = $this->_get_minimum_where_conditions(); |
|
3734 | - } |
|
3735 | - foreach ($query_info_carrier->get_model_names_included() as $model_relation_path => $model_name) { |
|
3736 | - $related_model = $this->get_related_model_obj($model_name); |
|
3737 | - if ($this->_should_use_default_where_conditions($use_default_where_conditions, false)) { |
|
3738 | - $related_model_universal_where_params = |
|
3739 | - $related_model->_get_default_where_conditions($model_relation_path); |
|
3740 | - } elseif ($this->_should_use_minimum_where_conditions($use_default_where_conditions, false)) { |
|
3741 | - $related_model_universal_where_params = |
|
3742 | - $related_model->_get_minimum_where_conditions($model_relation_path); |
|
3743 | - } else { |
|
3744 | - // we don't want to add full or even minimum default where conditions from this model, so just continue |
|
3745 | - continue; |
|
3746 | - } |
|
3747 | - $overrides = $this->_override_defaults_or_make_null_friendly( |
|
3748 | - $related_model_universal_where_params, |
|
3749 | - $where_query_params, |
|
3750 | - $related_model, |
|
3751 | - $model_relation_path |
|
3752 | - ); |
|
3753 | - $universal_query_params = EEH_Array::merge_arrays_and_overwrite_keys( |
|
3754 | - $universal_query_params, |
|
3755 | - $overrides |
|
3756 | - ); |
|
3757 | - } |
|
3758 | - return $universal_query_params; |
|
3759 | - } |
|
3760 | - |
|
3761 | - |
|
3762 | - /** |
|
3763 | - * Determines whether or not we should use default where conditions for the model in question |
|
3764 | - * (this model, or other related models). |
|
3765 | - * Basically, we should use default where conditions on this model if they have requested to use them on all models, |
|
3766 | - * this model only, or to use minimum where conditions on all other models and normal where conditions on this one. |
|
3767 | - * We should use default where conditions on related models when they requested to use default where conditions |
|
3768 | - * on all models, or specifically just on other related models |
|
3769 | - * |
|
3770 | - * @param $default_where_conditions_value |
|
3771 | - * @param bool $for_this_model false means this is for OTHER related models |
|
3772 | - * @return bool |
|
3773 | - */ |
|
3774 | - private function _should_use_default_where_conditions($default_where_conditions_value, $for_this_model = true) |
|
3775 | - { |
|
3776 | - return ( |
|
3777 | - $for_this_model |
|
3778 | - && in_array( |
|
3779 | - $default_where_conditions_value, |
|
3780 | - [ |
|
3781 | - EEM_Base::default_where_conditions_all, |
|
3782 | - EEM_Base::default_where_conditions_this_only, |
|
3783 | - EEM_Base::default_where_conditions_minimum_others, |
|
3784 | - ], |
|
3785 | - true |
|
3786 | - ) |
|
3787 | - ) |
|
3788 | - || ( |
|
3789 | - ! $for_this_model |
|
3790 | - && in_array( |
|
3791 | - $default_where_conditions_value, |
|
3792 | - [ |
|
3793 | - EEM_Base::default_where_conditions_all, |
|
3794 | - EEM_Base::default_where_conditions_others_only, |
|
3795 | - ], |
|
3796 | - true |
|
3797 | - ) |
|
3798 | - ); |
|
3799 | - } |
|
3800 | - |
|
3801 | - |
|
3802 | - /** |
|
3803 | - * Determines whether or not we should use default minimum conditions for the model in question |
|
3804 | - * (this model, or other related models). |
|
3805 | - * Basically, we should use minimum where conditions on this model only if they requested all models to use minimum |
|
3806 | - * where conditions. |
|
3807 | - * We should use minimum where conditions on related models if they requested to use minimum where conditions |
|
3808 | - * on this model or others |
|
3809 | - * |
|
3810 | - * @param $default_where_conditions_value |
|
3811 | - * @param bool $for_this_model false means this is for OTHER related models |
|
3812 | - * @return bool |
|
3813 | - */ |
|
3814 | - private function _should_use_minimum_where_conditions($default_where_conditions_value, $for_this_model = true) |
|
3815 | - { |
|
3816 | - return ( |
|
3817 | - $for_this_model |
|
3818 | - && $default_where_conditions_value === EEM_Base::default_where_conditions_minimum_all |
|
3819 | - ) |
|
3820 | - || ( |
|
3821 | - ! $for_this_model |
|
3822 | - && in_array( |
|
3823 | - $default_where_conditions_value, |
|
3824 | - [ |
|
3825 | - EEM_Base::default_where_conditions_minimum_others, |
|
3826 | - EEM_Base::default_where_conditions_minimum_all, |
|
3827 | - ], |
|
3828 | - true |
|
3829 | - ) |
|
3830 | - ); |
|
3831 | - } |
|
3832 | - |
|
3833 | - |
|
3834 | - /** |
|
3835 | - * Checks if any of the defaults have been overridden. If there are any that AREN'T overridden, |
|
3836 | - * then we also add a special where condition which allows for that model's primary key |
|
3837 | - * to be null (which is important for JOINs. Eg, if you want to see all Events ordered by Venue's name, |
|
3838 | - * then Event's with NO Venue won't appear unless you allow VNU_ID to be NULL) |
|
3839 | - * |
|
3840 | - * @param array $default_where_conditions |
|
3841 | - * @param array $provided_where_conditions |
|
3842 | - * @param EEM_Base $model |
|
3843 | - * @param string $model_relation_path like 'Transaction.Payment.' |
|
3844 | - * @return array |
|
3845 | - * @throws EE_Error |
|
3846 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
3847 | - */ |
|
3848 | - private function _override_defaults_or_make_null_friendly( |
|
3849 | - $default_where_conditions, |
|
3850 | - $provided_where_conditions, |
|
3851 | - $model, |
|
3852 | - $model_relation_path |
|
3853 | - ) { |
|
3854 | - $null_friendly_where_conditions = []; |
|
3855 | - $none_overridden = true; |
|
3856 | - $or_condition_key_for_defaults = 'OR*' . get_class($model); |
|
3857 | - foreach ($default_where_conditions as $key => $val) { |
|
3858 | - if (isset($provided_where_conditions[ $key ])) { |
|
3859 | - $none_overridden = false; |
|
3860 | - } else { |
|
3861 | - $null_friendly_where_conditions[ $or_condition_key_for_defaults ]['AND'][ $key ] = $val; |
|
3862 | - } |
|
3863 | - } |
|
3864 | - if ($none_overridden && $default_where_conditions) { |
|
3865 | - if ($model->has_primary_key_field()) { |
|
3866 | - $null_friendly_where_conditions[ $or_condition_key_for_defaults ][ $model_relation_path |
|
3867 | - . "." |
|
3868 | - . $model->primary_key_name() ] = |
|
3869 | - ['IS NULL']; |
|
3870 | - }/*else{ |
|
40 | + /** |
|
41 | + * constants used to categorize capability restrictions on EEM_Base::_caps_restrictions |
|
42 | + */ |
|
43 | + const caps_read = 'read'; |
|
44 | + |
|
45 | + const caps_read_admin = 'read_admin'; |
|
46 | + |
|
47 | + const caps_edit = 'edit'; |
|
48 | + |
|
49 | + const caps_delete = 'delete'; |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * constant used to show EEM_Base has not yet verified the db on this http request |
|
54 | + */ |
|
55 | + const db_verified_none = 0; |
|
56 | + |
|
57 | + /** |
|
58 | + * constant used to show EEM_Base has verified the EE core db on this http request, |
|
59 | + * but not the addons' dbs |
|
60 | + */ |
|
61 | + const db_verified_core = 1; |
|
62 | + |
|
63 | + /** |
|
64 | + * constant used to show EEM_Base has verified the addons' dbs (and implicitly |
|
65 | + * the EE core db too) |
|
66 | + */ |
|
67 | + const db_verified_addons = 2; |
|
68 | + |
|
69 | + /** |
|
70 | + * @const constant for 'default_where_conditions' to apply default where conditions to ALL queried models |
|
71 | + * (eg, if retrieving registrations ordered by their datetimes, this will only return non-trashed |
|
72 | + * registrations for non-trashed tickets for non-trashed datetimes) |
|
73 | + */ |
|
74 | + const default_where_conditions_all = 'all'; |
|
75 | + |
|
76 | + /** |
|
77 | + * @const constant for 'default_where_conditions' to apply default where conditions to THIS model only, but |
|
78 | + * no other models which are joined to (eg, if retrieving registrations ordered by their datetimes, this will |
|
79 | + * return non-trashed registrations, regardless of the related datetimes and tickets' statuses). |
|
80 | + * It is preferred to use EEM_Base::default_where_conditions_minimum_others because, when joining to |
|
81 | + * models which share tables with other models, this can return data for the wrong model. |
|
82 | + */ |
|
83 | + const default_where_conditions_this_only = 'this_model_only'; |
|
84 | + |
|
85 | + /** |
|
86 | + * @const constant for 'default_where_conditions' to apply default where conditions to other models queried, |
|
87 | + * but not the current model (eg, if retrieving registrations ordered by their datetimes, this will |
|
88 | + * return all registrations related to non-trashed tickets and non-trashed datetimes) |
|
89 | + */ |
|
90 | + const default_where_conditions_others_only = 'other_models_only'; |
|
91 | + |
|
92 | + /** |
|
93 | + * @const constant for 'default_where_conditions' to apply minimum where conditions to all models queried. |
|
94 | + * For most models this the same as EEM_Base::default_where_conditions_none, except for models which share |
|
95 | + * their table with other models, like the Event and Venue models. For example, when querying for events |
|
96 | + * ordered by their venues' name, this will be sure to only return real events with associated real venues |
|
97 | + * (regardless of whether those events and venues are trashed) |
|
98 | + * In contrast, using EEM_Base::default_where_conditions_none would could return WP posts other than EE |
|
99 | + * events. |
|
100 | + */ |
|
101 | + const default_where_conditions_minimum_all = 'minimum'; |
|
102 | + |
|
103 | + /** |
|
104 | + * @const constant for 'default_where_conditions' to apply apply where conditions to other models, and full default |
|
105 | + * where conditions for the queried model (eg, when querying events ordered by venues' names, this will |
|
106 | + * return non-trashed events for any venues, regardless of whether those associated venues are trashed or |
|
107 | + * not) |
|
108 | + */ |
|
109 | + const default_where_conditions_minimum_others = 'full_this_minimum_others'; |
|
110 | + |
|
111 | + /** |
|
112 | + * @const constant for 'default_where_conditions' to NOT apply any where conditions. This should very rarely be |
|
113 | + * used, because when querying from a model which shares its table with another model (eg Events and Venues) |
|
114 | + * it's possible it will return table entries for other models. You should use |
|
115 | + * EEM_Base::default_where_conditions_minimum_all instead. |
|
116 | + */ |
|
117 | + const default_where_conditions_none = 'none'; |
|
118 | + |
|
119 | + /** |
|
120 | + * when $_values_already_prepared_by_model_object equals this, we assume |
|
121 | + * the data is just like form input that needs to have the model fields' |
|
122 | + * prepare_for_set and prepare_for_use_in_db called on it |
|
123 | + */ |
|
124 | + const not_prepared_by_model_object = 0; |
|
125 | + |
|
126 | + /** |
|
127 | + * when $_values_already_prepared_by_model_object equals this, we |
|
128 | + * assume this value is coming from a model object and doesn't need to have |
|
129 | + * prepare_for_set called on it, just prepare_for_use_in_db is used |
|
130 | + */ |
|
131 | + const prepared_by_model_object = 1; |
|
132 | + |
|
133 | + /** |
|
134 | + * when $_values_already_prepared_by_model_object equals this, we assume |
|
135 | + * the values are already to be used in the database (ie no processing is done |
|
136 | + * on them by the model's fields) |
|
137 | + */ |
|
138 | + const prepared_for_use_in_db = 2; |
|
139 | + |
|
140 | + /** |
|
141 | + * Flag to indicate whether the values provided to EEM_Base have already been prepared |
|
142 | + * by the model object or not (ie, the model object has used the field's _prepare_for_set function on the values). |
|
143 | + * They almost always WILL NOT, but it's not necessarily a requirement. |
|
144 | + * For example, if you want to run EEM_Event::instance()->get_all(array(array('EVT_ID'=>$_GET['event_id']))); |
|
145 | + * |
|
146 | + * @var boolean |
|
147 | + */ |
|
148 | + private $_values_already_prepared_by_model_object = 0; |
|
149 | + |
|
150 | + |
|
151 | + /** |
|
152 | + * @var string |
|
153 | + */ |
|
154 | + protected $singular_item = 'Item'; |
|
155 | + |
|
156 | + /** |
|
157 | + * @var string |
|
158 | + */ |
|
159 | + protected $plural_item = 'Items'; |
|
160 | + |
|
161 | + /** |
|
162 | + * array of EE_Table objects for defining which tables comprise this model. |
|
163 | + * |
|
164 | + * @type EE_Table_Base[] $_tables |
|
165 | + */ |
|
166 | + protected $_tables; |
|
167 | + |
|
168 | + /** |
|
169 | + * with two levels: top-level has array keys which are database table aliases (ie, keys in _tables) |
|
170 | + * and the value is an array. Each of those sub-arrays have keys of field names (eg 'ATT_ID', which should also be |
|
171 | + * variable names on the model objects (eg, EE_Attendee), and the keys should be children of EE_Model_Field |
|
172 | + * |
|
173 | + * @var EE_Model_Field_Base[][] $_fields |
|
174 | + */ |
|
175 | + protected $_fields; |
|
176 | + |
|
177 | + /** |
|
178 | + * array of different kinds of relations |
|
179 | + * |
|
180 | + * @var EE_Model_Relation_Base[] $_model_relations |
|
181 | + */ |
|
182 | + protected $_model_relations; |
|
183 | + |
|
184 | + /** |
|
185 | + * @var EE_Index[] $_indexes |
|
186 | + */ |
|
187 | + protected $_indexes = []; |
|
188 | + |
|
189 | + /** |
|
190 | + * Default strategy for getting where conditions on this model. This strategy is used to get default |
|
191 | + * where conditions which are added to get_all, update, and delete queries. They can be overridden |
|
192 | + * by setting the same columns as used in these queries in the query yourself. |
|
193 | + * |
|
194 | + * @var EE_Default_Where_Conditions |
|
195 | + */ |
|
196 | + protected $_default_where_conditions_strategy; |
|
197 | + |
|
198 | + /** |
|
199 | + * Strategy for getting conditions on this model when 'default_where_conditions' equals 'minimum'. |
|
200 | + * This is particularly useful when you want something between 'none' and 'default' |
|
201 | + * |
|
202 | + * @var EE_Default_Where_Conditions |
|
203 | + */ |
|
204 | + protected $_minimum_where_conditions_strategy; |
|
205 | + |
|
206 | + /** |
|
207 | + * String describing how to find the "owner" of this model's objects. |
|
208 | + * When there is a foreign key on this model to the wp_users table, this isn't needed. |
|
209 | + * But when there isn't, this indicates which related model, or transiently-related model, |
|
210 | + * has the foreign key to the wp_users table. |
|
211 | + * Eg, for EEM_Registration this would be 'Event' because registrations are directly |
|
212 | + * related to events, and events have a foreign key to wp_users. |
|
213 | + * On EEM_Transaction, this would be 'Transaction.Event' |
|
214 | + * |
|
215 | + * @var string |
|
216 | + */ |
|
217 | + protected $_model_chain_to_wp_user = ''; |
|
218 | + |
|
219 | + /** |
|
220 | + * String describing how to find the model with a password controlling access to this model. This property has the |
|
221 | + * same format as $_model_chain_to_wp_user. This is primarily used by the query param "exclude_protected". |
|
222 | + * This value is the path of models to follow to arrive at the model with the password field. |
|
223 | + * If it is an empty string, it means this model has the password field. If it is null, it means there is no |
|
224 | + * model with a password that should affect reading this on the front-end. |
|
225 | + * Eg this is an empty string for the Event model because it has a password. |
|
226 | + * This is null for the Registration model, because its event's password has no bearing on whether |
|
227 | + * you can read the registration or not on the front-end (it just depends on your capabilities.) |
|
228 | + * This is 'Datetime.Event' on the Ticket model, because model queries for tickets that set "exclude_protected" |
|
229 | + * should hide tickets for datetimes for events that have a password set. |
|
230 | + * |
|
231 | + * @var string |null |
|
232 | + */ |
|
233 | + protected $model_chain_to_password = null; |
|
234 | + |
|
235 | + /** |
|
236 | + * This is a flag typically set by updates so that we don't load the where strategy on updates because updates |
|
237 | + * don't need it (particularly CPT models) |
|
238 | + * |
|
239 | + * @var bool |
|
240 | + */ |
|
241 | + protected $_ignore_where_strategy = false; |
|
242 | + |
|
243 | + /** |
|
244 | + * String used in caps relating to this model. Eg, if the caps relating to this |
|
245 | + * model are 'ee_edit_events', 'ee_read_events', etc, it would be 'events'. |
|
246 | + * |
|
247 | + * @var string. If null it hasn't been initialized yet. If false then we |
|
248 | + * have indicated capabilities don't apply to this |
|
249 | + */ |
|
250 | + protected $_caps_slug = null; |
|
251 | + |
|
252 | + /** |
|
253 | + * 2d array where top-level keys are one of EEM_Base::valid_cap_contexts(), |
|
254 | + * and next-level keys are capability names, and values are a |
|
255 | + * EE_Default_Where_Condition. If the requester requests to apply caps to the query, |
|
256 | + * they specify which context to use (ie, frontend, backend, edit or delete) |
|
257 | + * and then each capability in the corresponding sub-array that they're missing |
|
258 | + * adds the where conditions onto the query. |
|
259 | + * |
|
260 | + * @var array |
|
261 | + */ |
|
262 | + protected $_cap_restrictions = [ |
|
263 | + self::caps_read => [], |
|
264 | + self::caps_read_admin => [], |
|
265 | + self::caps_edit => [], |
|
266 | + self::caps_delete => [], |
|
267 | + ]; |
|
268 | + |
|
269 | + /** |
|
270 | + * Array defining which cap restriction generators to use to create default |
|
271 | + * cap restrictions to put in EEM_Base::_cap_restrictions. |
|
272 | + * Array-keys are one of EEM_Base::valid_cap_contexts(), and values are a child of |
|
273 | + * EE_Restriction_Generator_Base. If you don't want any cap restrictions generated |
|
274 | + * automatically set this to false (not just null). |
|
275 | + * |
|
276 | + * @var EE_Restriction_Generator_Base[] |
|
277 | + */ |
|
278 | + protected $_cap_restriction_generators = []; |
|
279 | + |
|
280 | + /** |
|
281 | + * Keys are all the cap contexts (ie constants EEM_Base::_caps_*) and values are their 'action' |
|
282 | + * as how they'd be used in capability names. Eg EEM_Base::caps_read ('read_frontend') |
|
283 | + * maps to 'read' because when looking for relevant permissions we're going to use |
|
284 | + * 'read' in teh capabilities names like 'ee_read_events' etc. |
|
285 | + * |
|
286 | + * @var array |
|
287 | + */ |
|
288 | + protected $_cap_contexts_to_cap_action_map = [ |
|
289 | + self::caps_read => 'read', |
|
290 | + self::caps_read_admin => 'read', |
|
291 | + self::caps_edit => 'edit', |
|
292 | + self::caps_delete => 'delete', |
|
293 | + ]; |
|
294 | + |
|
295 | + /** |
|
296 | + * Timezone |
|
297 | + * This gets set via the constructor so that we know what timezone incoming strings|timestamps are in when there |
|
298 | + * are EE_Datetime_Fields in use. This can also be used before a get to set what timezone you want strings coming |
|
299 | + * out of the created objects. NOT all EEM_Base child classes use this property but any that use a |
|
300 | + * EE_Datetime_Field data type will have access to it. |
|
301 | + * |
|
302 | + * @var string |
|
303 | + */ |
|
304 | + protected $_timezone; |
|
305 | + |
|
306 | + |
|
307 | + /** |
|
308 | + * This holds the id of the blog currently making the query. Has no bearing on single site but is used for |
|
309 | + * multisite. |
|
310 | + * |
|
311 | + * @var int |
|
312 | + */ |
|
313 | + protected static $_model_query_blog_id; |
|
314 | + |
|
315 | + /** |
|
316 | + * A copy of _fields, except the array keys are the model names pointed to by |
|
317 | + * the field |
|
318 | + * |
|
319 | + * @var EE_Model_Field_Base[] |
|
320 | + */ |
|
321 | + private $_cache_foreign_key_to_fields = []; |
|
322 | + |
|
323 | + /** |
|
324 | + * Cached list of all the fields on the model, indexed by their name |
|
325 | + * |
|
326 | + * @var EE_Model_Field_Base[] |
|
327 | + */ |
|
328 | + private $_cached_fields = null; |
|
329 | + |
|
330 | + /** |
|
331 | + * Cached list of all the fields on the model, except those that are |
|
332 | + * marked as only pertinent to the database |
|
333 | + * |
|
334 | + * @var EE_Model_Field_Base[] |
|
335 | + */ |
|
336 | + private $_cached_fields_non_db_only = null; |
|
337 | + |
|
338 | + /** |
|
339 | + * A cached reference to the primary key for quick lookup |
|
340 | + * |
|
341 | + * @var EE_Model_Field_Base |
|
342 | + */ |
|
343 | + private $_primary_key_field = null; |
|
344 | + |
|
345 | + /** |
|
346 | + * Flag indicating whether this model has a primary key or not |
|
347 | + * |
|
348 | + * @var boolean |
|
349 | + */ |
|
350 | + protected $_has_primary_key_field = null; |
|
351 | + |
|
352 | + /** |
|
353 | + * array in the format: [ FK alias => full PK ] |
|
354 | + * where keys are local column name aliases for foreign keys |
|
355 | + * and values are the fully qualified column name for the primary key they represent |
|
356 | + * ex: |
|
357 | + * [ 'Event.EVT_wp_user' => 'WP_User.ID' ] |
|
358 | + * |
|
359 | + * @var array $foreign_key_aliases |
|
360 | + */ |
|
361 | + protected $foreign_key_aliases = []; |
|
362 | + |
|
363 | + /** |
|
364 | + * Whether or not this model is based off a table in WP core only (CPTs should set |
|
365 | + * this to FALSE, but if we were to make an EE_WP_Post model, it should set this to true). |
|
366 | + * This should be true for models that deal with data that should exist independent of EE. |
|
367 | + * For example, if the model can read and insert data that isn't used by EE, this should be true. |
|
368 | + * It would be false, however, if you could guarantee the model would only interact with EE data, |
|
369 | + * even if it uses a WP core table (eg event and venue models set this to false for that reason: |
|
370 | + * they can only read and insert events and venues custom post types, not arbitrary post types) |
|
371 | + * |
|
372 | + * @var boolean |
|
373 | + */ |
|
374 | + protected $_wp_core_model = false; |
|
375 | + |
|
376 | + /** |
|
377 | + * @var bool stores whether this model has a password field or not. |
|
378 | + * null until initialized by hasPasswordField() |
|
379 | + */ |
|
380 | + protected $has_password_field; |
|
381 | + |
|
382 | + /** |
|
383 | + * @var EE_Password_Field|null Automatically set when calling getPasswordField() |
|
384 | + */ |
|
385 | + protected $password_field; |
|
386 | + |
|
387 | + /** |
|
388 | + * List of valid operators that can be used for querying. |
|
389 | + * The keys are all operators we'll accept, the values are the real SQL |
|
390 | + * operators used |
|
391 | + * |
|
392 | + * @var array |
|
393 | + */ |
|
394 | + protected $_valid_operators = [ |
|
395 | + '=' => '=', |
|
396 | + '<=' => '<=', |
|
397 | + '<' => '<', |
|
398 | + '>=' => '>=', |
|
399 | + '>' => '>', |
|
400 | + '!=' => '!=', |
|
401 | + 'LIKE' => 'LIKE', |
|
402 | + 'like' => 'LIKE', |
|
403 | + 'NOT_LIKE' => 'NOT LIKE', |
|
404 | + 'not_like' => 'NOT LIKE', |
|
405 | + 'NOT LIKE' => 'NOT LIKE', |
|
406 | + 'not like' => 'NOT LIKE', |
|
407 | + 'IN' => 'IN', |
|
408 | + 'in' => 'IN', |
|
409 | + 'NOT_IN' => 'NOT IN', |
|
410 | + 'not_in' => 'NOT IN', |
|
411 | + 'NOT IN' => 'NOT IN', |
|
412 | + 'not in' => 'NOT IN', |
|
413 | + 'between' => 'BETWEEN', |
|
414 | + 'BETWEEN' => 'BETWEEN', |
|
415 | + 'IS_NOT_NULL' => 'IS NOT NULL', |
|
416 | + 'is_not_null' => 'IS NOT NULL', |
|
417 | + 'IS NOT NULL' => 'IS NOT NULL', |
|
418 | + 'is not null' => 'IS NOT NULL', |
|
419 | + 'IS_NULL' => 'IS NULL', |
|
420 | + 'is_null' => 'IS NULL', |
|
421 | + 'IS NULL' => 'IS NULL', |
|
422 | + 'is null' => 'IS NULL', |
|
423 | + 'REGEXP' => 'REGEXP', |
|
424 | + 'regexp' => 'REGEXP', |
|
425 | + 'NOT_REGEXP' => 'NOT REGEXP', |
|
426 | + 'not_regexp' => 'NOT REGEXP', |
|
427 | + 'NOT REGEXP' => 'NOT REGEXP', |
|
428 | + 'not regexp' => 'NOT REGEXP', |
|
429 | + ]; |
|
430 | + |
|
431 | + /** |
|
432 | + * operators that work like 'IN', accepting a comma-separated list of values inside brackets. Eg '(1,2,3)' |
|
433 | + * |
|
434 | + * @var array |
|
435 | + */ |
|
436 | + protected $_in_style_operators = ['IN', 'NOT IN']; |
|
437 | + |
|
438 | + /** |
|
439 | + * operators that work like 'BETWEEN'. Typically used for datetime calculations, i.e. "BETWEEN '12-1-2011' AND |
|
440 | + * '12-31-2012'" |
|
441 | + * |
|
442 | + * @var array |
|
443 | + */ |
|
444 | + protected $_between_style_operators = ['BETWEEN']; |
|
445 | + |
|
446 | + /** |
|
447 | + * Operators that work like SQL's like: input should be assumed to be a string, already prepared for a LIKE query. |
|
448 | + * |
|
449 | + * @var array |
|
450 | + */ |
|
451 | + protected $_like_style_operators = ['LIKE', 'NOT LIKE']; |
|
452 | + |
|
453 | + /** |
|
454 | + * operators that are used for handling NUll and !NULL queries. Typically used for when checking if a row exists |
|
455 | + * on a join table. |
|
456 | + * |
|
457 | + * @var array |
|
458 | + */ |
|
459 | + protected $_null_style_operators = ['IS NOT NULL', 'IS NULL']; |
|
460 | + |
|
461 | + /** |
|
462 | + * Allowed values for $query_params['order'] for ordering in queries |
|
463 | + * |
|
464 | + * @var array |
|
465 | + */ |
|
466 | + protected $_allowed_order_values = ['asc', 'desc', 'ASC', 'DESC']; |
|
467 | + |
|
468 | + /** |
|
469 | + * When these are keys in a WHERE or HAVING clause, they are handled much differently |
|
470 | + * than regular field names. It is assumed that their values are an array of WHERE conditions |
|
471 | + * |
|
472 | + * @var array |
|
473 | + */ |
|
474 | + private $_logic_query_param_keys = ['not', 'and', 'or', 'NOT', 'AND', 'OR']; |
|
475 | + |
|
476 | + /** |
|
477 | + * Allowed keys in $query_params arrays passed into queries. Note that 0 is meant to always be a |
|
478 | + * 'where', but 'where' clauses are so common that we thought we'd omit it |
|
479 | + * |
|
480 | + * @var array |
|
481 | + */ |
|
482 | + private $_allowed_query_params = [ |
|
483 | + 0, |
|
484 | + 'limit', |
|
485 | + 'order_by', |
|
486 | + 'group_by', |
|
487 | + 'having', |
|
488 | + 'force_join', |
|
489 | + 'order', |
|
490 | + 'on_join_limit', |
|
491 | + 'default_where_conditions', |
|
492 | + 'caps', |
|
493 | + 'extra_selects', |
|
494 | + 'exclude_protected', |
|
495 | + ]; |
|
496 | + |
|
497 | + /** |
|
498 | + * All the data types that can be used in $wpdb->prepare statements. |
|
499 | + * |
|
500 | + * @var array |
|
501 | + */ |
|
502 | + private $_valid_wpdb_data_types = ['%d', '%s', '%f']; |
|
503 | + |
|
504 | + /** |
|
505 | + * @var EE_Registry $EE |
|
506 | + */ |
|
507 | + protected $EE = null; |
|
508 | + |
|
509 | + |
|
510 | + /** |
|
511 | + * Property which, when set, will have this model echo out the next X queries to the page for debugging. |
|
512 | + * |
|
513 | + * @var int |
|
514 | + */ |
|
515 | + protected $_show_next_x_db_queries = 0; |
|
516 | + |
|
517 | + /** |
|
518 | + * When using _get_all_wpdb_results, you can specify a custom selection. If you do so, |
|
519 | + * it gets saved on this property as an instance of CustomSelects so those selections can be used in |
|
520 | + * WHERE, GROUP_BY, etc. |
|
521 | + * |
|
522 | + * @var CustomSelects |
|
523 | + */ |
|
524 | + protected $_custom_selections = []; |
|
525 | + |
|
526 | + /** |
|
527 | + * key => value Entity Map using array( EEM_Base::$_model_query_blog_id => array( ID => model object ) ) |
|
528 | + * caches every model object we've fetched from the DB on this request |
|
529 | + * |
|
530 | + * @var array |
|
531 | + */ |
|
532 | + protected $_entity_map; |
|
533 | + |
|
534 | + /** |
|
535 | + * @var LoaderInterface $loader |
|
536 | + */ |
|
537 | + private static $loader; |
|
538 | + |
|
539 | + /** |
|
540 | + * indicates whether an EEM_Base child has already re-verified the DB |
|
541 | + * is ok (we don't want to do it repetitively). Should be set to one the constants |
|
542 | + * looking like EEM_Base::db_verified_* |
|
543 | + * |
|
544 | + * @var int - 0 = none, 1 = core, 2 = addons |
|
545 | + */ |
|
546 | + protected static $_db_verification_level = EEM_Base::db_verified_none; |
|
547 | + |
|
548 | + |
|
549 | + /** |
|
550 | + * About all child constructors: |
|
551 | + * they should define the _tables, _fields and _model_relations arrays. |
|
552 | + * Should ALWAYS be called after child constructor. |
|
553 | + * In order to make the child constructors to be as simple as possible, this parent constructor |
|
554 | + * finalizes constructing all the object's attributes. |
|
555 | + * Generally, rather than requiring a child to code |
|
556 | + * $this->_tables = array( |
|
557 | + * 'Event_Post_Table' => new EE_Table('Event_Post_Table','wp_posts') |
|
558 | + * ...); |
|
559 | + * (thus repeating itself in the array key and in the constructor of the new EE_Table,) |
|
560 | + * each EE_Table has a function to set the table's alias after the constructor, using |
|
561 | + * the array key ('Event_Post_Table'), instead of repeating it. The model fields and model relations |
|
562 | + * do something similar. |
|
563 | + * |
|
564 | + * @param string $timezone |
|
565 | + * @throws EE_Error |
|
566 | + */ |
|
567 | + protected function __construct($timezone = '') |
|
568 | + { |
|
569 | + // check that the model has not been loaded too soon |
|
570 | + if (! did_action('AHEE__EE_System__load_espresso_addons')) { |
|
571 | + throw new EE_Error( |
|
572 | + sprintf( |
|
573 | + esc_html__( |
|
574 | + 'The %1$s model can not be loaded before the "AHEE__EE_System__load_espresso_addons" hook has been called. This gives other addons a chance to extend this model.', |
|
575 | + 'event_espresso' |
|
576 | + ), |
|
577 | + get_class($this) |
|
578 | + ) |
|
579 | + ); |
|
580 | + } |
|
581 | + /** |
|
582 | + * Set blog id for models to current blog. However we ONLY do this if $_model_query_blog_id is not already set. |
|
583 | + */ |
|
584 | + if (empty(EEM_Base::$_model_query_blog_id)) { |
|
585 | + EEM_Base::set_model_query_blog_id(); |
|
586 | + } |
|
587 | + /** |
|
588 | + * Filters the list of tables on a model. It is best to NOT use this directly and instead |
|
589 | + * just use EE_Register_Model_Extension |
|
590 | + * |
|
591 | + * @var EE_Table_Base[] $_tables |
|
592 | + */ |
|
593 | + $this->_tables = (array) apply_filters('FHEE__' . get_class($this) . '__construct__tables', $this->_tables); |
|
594 | + foreach ($this->_tables as $table_alias => $table_obj) { |
|
595 | + /** @var $table_obj EE_Table_Base */ |
|
596 | + $table_obj->_construct_finalize_with_alias($table_alias); |
|
597 | + if ($table_obj instanceof EE_Secondary_Table) { |
|
598 | + /** @var $table_obj EE_Secondary_Table */ |
|
599 | + $table_obj->_construct_finalize_set_table_to_join_with($this->_get_main_table()); |
|
600 | + } |
|
601 | + } |
|
602 | + /** |
|
603 | + * Filters the list of fields on a model. It is best to NOT use this directly and instead just use |
|
604 | + * EE_Register_Model_Extension |
|
605 | + * |
|
606 | + * @param EE_Model_Field_Base[] $_fields |
|
607 | + */ |
|
608 | + $this->_fields = (array) apply_filters('FHEE__' . get_class($this) . '__construct__fields', $this->_fields); |
|
609 | + $this->_invalidate_field_caches(); |
|
610 | + foreach ($this->_fields as $table_alias => $fields_for_table) { |
|
611 | + if (! array_key_exists($table_alias, $this->_tables)) { |
|
612 | + throw new EE_Error( |
|
613 | + sprintf( |
|
614 | + esc_html__( |
|
615 | + "Table alias %s does not exist in EEM_Base child's _tables array. Only tables defined are %s", |
|
616 | + 'event_espresso' |
|
617 | + ), |
|
618 | + $table_alias, |
|
619 | + implode(",", $this->_fields) |
|
620 | + ) |
|
621 | + ); |
|
622 | + } |
|
623 | + foreach ($fields_for_table as $field_name => $field_obj) { |
|
624 | + /** @var $field_obj EE_Model_Field_Base | EE_Primary_Key_Field_Base */ |
|
625 | + // primary key field base has a slightly different _construct_finalize |
|
626 | + /** @var $field_obj EE_Model_Field_Base */ |
|
627 | + $field_obj->_construct_finalize($table_alias, $field_name, $this->get_this_model_name()); |
|
628 | + } |
|
629 | + } |
|
630 | + // everything is related to Extra_Meta |
|
631 | + if (get_class($this) !== 'EEM_Extra_Meta') { |
|
632 | + // make extra meta related to everything, but don't block deleting things just |
|
633 | + // because they have related extra meta info. For now just orphan those extra meta |
|
634 | + // in the future we should automatically delete them |
|
635 | + $this->_model_relations['Extra_Meta'] = new EE_Has_Many_Any_Relation(false); |
|
636 | + } |
|
637 | + // and change logs |
|
638 | + if (get_class($this) !== 'EEM_Change_Log') { |
|
639 | + $this->_model_relations['Change_Log'] = new EE_Has_Many_Any_Relation(false); |
|
640 | + } |
|
641 | + /** |
|
642 | + * Filters the list of relations on a model. It is best to NOT use this directly and instead just use |
|
643 | + * EE_Register_Model_Extension |
|
644 | + * |
|
645 | + * @param EE_Model_Relation_Base[] $_model_relations |
|
646 | + */ |
|
647 | + $this->_model_relations = (array) apply_filters( |
|
648 | + 'FHEE__' . get_class($this) . '__construct__model_relations', |
|
649 | + $this->_model_relations |
|
650 | + ); |
|
651 | + foreach ($this->_model_relations as $model_name => $relation_obj) { |
|
652 | + /** @var $relation_obj EE_Model_Relation_Base */ |
|
653 | + $relation_obj->_construct_finalize_set_models($this->get_this_model_name(), $model_name); |
|
654 | + } |
|
655 | + foreach ($this->_indexes as $index_name => $index_obj) { |
|
656 | + $index_obj->_construct_finalize($index_name, $this->get_this_model_name()); |
|
657 | + } |
|
658 | + $this->set_timezone($timezone); |
|
659 | + // finalize default where condition strategy, or set default |
|
660 | + if (! $this->_default_where_conditions_strategy) { |
|
661 | + // nothing was set during child constructor, so set default |
|
662 | + $this->_default_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
663 | + } |
|
664 | + $this->_default_where_conditions_strategy->_finalize_construct($this); |
|
665 | + if (! $this->_minimum_where_conditions_strategy) { |
|
666 | + // nothing was set during child constructor, so set default |
|
667 | + $this->_minimum_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
668 | + } |
|
669 | + $this->_minimum_where_conditions_strategy->_finalize_construct($this); |
|
670 | + // if the cap slug hasn't been set, and we haven't set it to false on purpose |
|
671 | + // to indicate to NOT set it, set it to the logical default |
|
672 | + if ($this->_caps_slug === null) { |
|
673 | + $this->_caps_slug = EEH_Inflector::pluralize_and_lower($this->get_this_model_name()); |
|
674 | + } |
|
675 | + // initialize the standard cap restriction generators if none were specified by the child constructor |
|
676 | + if (! empty($this->_cap_restriction_generators)) { |
|
677 | + foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
|
678 | + if (! isset($this->_cap_restriction_generators[ $cap_context ])) { |
|
679 | + $this->_cap_restriction_generators[ $cap_context ] = apply_filters( |
|
680 | + 'FHEE__EEM_Base___construct__standard_cap_restriction_generator', |
|
681 | + new EE_Restriction_Generator_Protected(), |
|
682 | + $cap_context, |
|
683 | + $this |
|
684 | + ); |
|
685 | + } |
|
686 | + } |
|
687 | + // } |
|
688 | + // if ($this->_cap_restriction_generators !== false) { |
|
689 | + // if there are cap restriction generators, use them to make the default cap restrictions |
|
690 | + foreach ($this->_cap_restriction_generators as $context => $generator_object) { |
|
691 | + if (! $generator_object) { |
|
692 | + continue; |
|
693 | + } |
|
694 | + if (! $generator_object instanceof EE_Restriction_Generator_Base) { |
|
695 | + throw new EE_Error( |
|
696 | + sprintf( |
|
697 | + esc_html__( |
|
698 | + 'Index "%1$s" in the model %2$s\'s _cap_restriction_generators is not a child of EE_Restriction_Generator_Base. It should be that or NULL.', |
|
699 | + 'event_espresso' |
|
700 | + ), |
|
701 | + $context, |
|
702 | + $this->get_this_model_name() |
|
703 | + ) |
|
704 | + ); |
|
705 | + } |
|
706 | + $action = $this->cap_action_for_context($context); |
|
707 | + if (! $generator_object->construction_finalized()) { |
|
708 | + $generator_object->_construct_finalize($this, $action); |
|
709 | + } |
|
710 | + } |
|
711 | + } |
|
712 | + do_action('AHEE__' . get_class($this) . '__construct__end'); |
|
713 | + } |
|
714 | + |
|
715 | + |
|
716 | + /** |
|
717 | + * Used to set the $_model_query_blog_id static property. |
|
718 | + * |
|
719 | + * @param int $blog_id If provided then will set the blog_id for the models to this id. If not provided then the |
|
720 | + * value for get_current_blog_id() will be used. |
|
721 | + */ |
|
722 | + public static function set_model_query_blog_id($blog_id = 0) |
|
723 | + { |
|
724 | + EEM_Base::$_model_query_blog_id = $blog_id > 0 ? (int) $blog_id : get_current_blog_id(); |
|
725 | + } |
|
726 | + |
|
727 | + |
|
728 | + /** |
|
729 | + * Returns whatever is set as the internal $model_query_blog_id. |
|
730 | + * |
|
731 | + * @return int |
|
732 | + */ |
|
733 | + public static function get_model_query_blog_id() |
|
734 | + { |
|
735 | + return EEM_Base::$_model_query_blog_id; |
|
736 | + } |
|
737 | + |
|
738 | + |
|
739 | + /** |
|
740 | + * This function is a singleton method used to instantiate the Espresso_model object |
|
741 | + * |
|
742 | + * @param string $timezone string representing the timezone we want to set for returned Date Time Strings |
|
743 | + * (and any incoming timezone data that gets saved). |
|
744 | + * Note this just sends the timezone info to the date time model field objects. |
|
745 | + * Default is NULL |
|
746 | + * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
|
747 | + * @return EEM_Base (as in the concrete child class) |
|
748 | + * @throws EE_Error |
|
749 | + * @throws InvalidArgumentException |
|
750 | + * @throws InvalidDataTypeException |
|
751 | + * @throws InvalidInterfaceException |
|
752 | + */ |
|
753 | + public static function instance($timezone = '') |
|
754 | + { |
|
755 | + // check if instance of Espresso_model already exists |
|
756 | + if (! static::$_instance instanceof static) { |
|
757 | + // instantiate Espresso_model |
|
758 | + static::$_instance = new static( |
|
759 | + $timezone, |
|
760 | + LoaderFactory::getLoader()->load('EventEspresso\core\services\orm\ModelFieldFactory') |
|
761 | + ); |
|
762 | + } |
|
763 | + // Espresso model object |
|
764 | + return static::$_instance; |
|
765 | + } |
|
766 | + |
|
767 | + |
|
768 | + /** |
|
769 | + * resets the model and returns it |
|
770 | + * |
|
771 | + * @param string $timezone |
|
772 | + * @return EEM_Base|null (if the model was already instantiated, returns it, with |
|
773 | + * all its properties reset; if it wasn't instantiated, returns null) |
|
774 | + * @throws EE_Error |
|
775 | + * @throws ReflectionException |
|
776 | + * @throws InvalidArgumentException |
|
777 | + * @throws InvalidDataTypeException |
|
778 | + * @throws InvalidInterfaceException |
|
779 | + */ |
|
780 | + public static function reset($timezone = '') |
|
781 | + { |
|
782 | + if (static::$_instance instanceof EEM_Base) { |
|
783 | + // let's try to NOT swap out the current instance for a new one |
|
784 | + // because if someone has a reference to it, we can't remove their reference |
|
785 | + // so it's best to keep using the same reference, but change the original object |
|
786 | + // reset all its properties to their original values as defined in the class |
|
787 | + $r = new ReflectionClass(get_class(static::$_instance)); |
|
788 | + $static_properties = $r->getStaticProperties(); |
|
789 | + foreach ($r->getDefaultProperties() as $property => $value) { |
|
790 | + // don't set instance to null like it was originally, |
|
791 | + // but it's static anyways, and we're ignoring static properties (for now at least) |
|
792 | + if (! isset($static_properties[ $property ])) { |
|
793 | + static::$_instance->{$property} = $value; |
|
794 | + } |
|
795 | + } |
|
796 | + EEH_DTT_Helper::resetDefaultTimezoneString(); |
|
797 | + // and then directly call its constructor again, like we would if we were creating a new one |
|
798 | + static::$_instance->__construct( |
|
799 | + $timezone, |
|
800 | + LoaderFactory::getLoader()->load('EventEspresso\core\services\orm\ModelFieldFactory') |
|
801 | + ); |
|
802 | + return static::instance(); |
|
803 | + } |
|
804 | + return null; |
|
805 | + } |
|
806 | + |
|
807 | + |
|
808 | + /** |
|
809 | + * @return LoaderInterface |
|
810 | + * @throws InvalidArgumentException |
|
811 | + * @throws InvalidDataTypeException |
|
812 | + * @throws InvalidInterfaceException |
|
813 | + */ |
|
814 | + private static function getLoader() |
|
815 | + { |
|
816 | + if (! EEM_Base::$loader instanceof LoaderInterface) { |
|
817 | + EEM_Base::$loader = LoaderFactory::getLoader(); |
|
818 | + } |
|
819 | + return EEM_Base::$loader; |
|
820 | + } |
|
821 | + |
|
822 | + |
|
823 | + /** |
|
824 | + * retrieve the status details from esp_status table as an array IF this model has the status table as a relation. |
|
825 | + * |
|
826 | + * @param boolean $translated return localized strings or JUST the array. |
|
827 | + * @return array |
|
828 | + * @throws EE_Error |
|
829 | + * @throws InvalidArgumentException |
|
830 | + * @throws InvalidDataTypeException |
|
831 | + * @throws InvalidInterfaceException |
|
832 | + * @throws ReflectionException |
|
833 | + */ |
|
834 | + public function status_array($translated = false) |
|
835 | + { |
|
836 | + if (! array_key_exists('Status', $this->_model_relations)) { |
|
837 | + return []; |
|
838 | + } |
|
839 | + $model_name = $this->get_this_model_name(); |
|
840 | + $status_type = str_replace(' ', '_', strtolower(str_replace('_', ' ', $model_name))); |
|
841 | + $statuses = EEM_Status::instance()->get_all([['STS_type' => $status_type]]); |
|
842 | + $status_array = []; |
|
843 | + foreach ($statuses as $status) { |
|
844 | + $status_array[ $status->ID() ] = $status->get('STS_code'); |
|
845 | + } |
|
846 | + return $translated |
|
847 | + ? EEM_Status::instance()->localized_status($status_array, false, 'sentence') |
|
848 | + : $status_array; |
|
849 | + } |
|
850 | + |
|
851 | + |
|
852 | + /** |
|
853 | + * Gets all the EE_Base_Class objects which match the $query_params, by querying the DB. |
|
854 | + * |
|
855 | + * @param array $query_params see github link below for more info |
|
856 | + * @return EE_Base_Class[] *note that there is NO option to pass the output type. If you want results different |
|
857 | + * from EE_Base_Class[], use get_all_wpdb_results(). Array keys are object |
|
858 | + * IDs (if there is a primary key on the model. if not, numerically indexed) |
|
859 | + * Some full examples: get 10 transactions which have Scottish attendees: |
|
860 | + * EEM_Transaction::instance()->get_all( array( array( |
|
861 | + * 'OR'=>array( |
|
862 | + * 'Registration.Attendee.ATT_fname'=>array('like','Mc%'), |
|
863 | + * 'Registration.Attendee.ATT_fname*other'=>array('like','Mac%') |
|
864 | + * ) |
|
865 | + * ), |
|
866 | + * 'limit'=>10, |
|
867 | + * 'group_by'=>'TXN_ID' |
|
868 | + * )); |
|
869 | + * get all the answers to the question titled "shirt size" for event with id |
|
870 | + * 12, ordered by their answer EEM_Answer::instance()->get_all(array( array( |
|
871 | + * 'Question.QST_display_text'=>'shirt size', |
|
872 | + * 'Registration.Event.EVT_ID'=>12 |
|
873 | + * ), |
|
874 | + * 'order_by'=>array('ANS_value'=>'ASC') |
|
875 | + * )); |
|
876 | + * @throws EE_Error |
|
877 | + * @throws ReflectionException |
|
878 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
879 | + * or if you have the development copy of EE you can view this at the path: |
|
880 | + * /docs/G--Model-System/model-query-params.md |
|
881 | + */ |
|
882 | + public function get_all($query_params = []) |
|
883 | + { |
|
884 | + if ( |
|
885 | + isset($query_params['limit']) |
|
886 | + && ! isset($query_params['group_by']) |
|
887 | + ) { |
|
888 | + $query_params['group_by'] = array_keys($this->get_combined_primary_key_fields()); |
|
889 | + } |
|
890 | + return $this->_create_objects($this->_get_all_wpdb_results($query_params)); |
|
891 | + } |
|
892 | + |
|
893 | + |
|
894 | + /** |
|
895 | + * Modifies the query parameters so we only get back model objects |
|
896 | + * that "belong" to the current user |
|
897 | + * |
|
898 | + * @param array $query_params see github link below for more info |
|
899 | + * @return array |
|
900 | + * @throws ReflectionException |
|
901 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
902 | + */ |
|
903 | + public function alter_query_params_to_only_include_mine($query_params = []) |
|
904 | + { |
|
905 | + $wp_user_field_name = $this->wp_user_field_name(); |
|
906 | + if ($wp_user_field_name) { |
|
907 | + $query_params[0][ $wp_user_field_name ] = get_current_user_id(); |
|
908 | + } |
|
909 | + return $query_params; |
|
910 | + } |
|
911 | + |
|
912 | + |
|
913 | + /** |
|
914 | + * Returns the name of the field's name that points to the WP_User table |
|
915 | + * on this model (or follows the _model_chain_to_wp_user and uses that model's |
|
916 | + * foreign key to the WP_User table) |
|
917 | + * |
|
918 | + * @return string|boolean string on success, boolean false when there is no |
|
919 | + * foreign key to the WP_User table |
|
920 | + * @throws ReflectionException |
|
921 | + */ |
|
922 | + public function wp_user_field_name() |
|
923 | + { |
|
924 | + try { |
|
925 | + if (! empty($this->_model_chain_to_wp_user)) { |
|
926 | + $models_to_follow_to_wp_users = explode('.', $this->_model_chain_to_wp_user); |
|
927 | + $last_model_name = end($models_to_follow_to_wp_users); |
|
928 | + $model_with_fk_to_wp_users = EE_Registry::instance()->load_model($last_model_name); |
|
929 | + $model_chain_to_wp_user = $this->_model_chain_to_wp_user . '.'; |
|
930 | + } else { |
|
931 | + $model_with_fk_to_wp_users = $this; |
|
932 | + $model_chain_to_wp_user = ''; |
|
933 | + } |
|
934 | + $wp_user_field = $model_with_fk_to_wp_users->get_foreign_key_to('WP_User'); |
|
935 | + return $model_chain_to_wp_user . $wp_user_field->get_name(); |
|
936 | + } catch (EE_Error $e) { |
|
937 | + return false; |
|
938 | + } |
|
939 | + } |
|
940 | + |
|
941 | + |
|
942 | + /** |
|
943 | + * Returns the _model_chain_to_wp_user string, which indicates which related model |
|
944 | + * (or transiently-related model) has a foreign key to the wp_users table; |
|
945 | + * useful for finding if model objects of this type are 'owned' by the current user. |
|
946 | + * This is an empty string when the foreign key is on this model and when it isn't, |
|
947 | + * but is only non-empty when this model's ownership is indicated by a RELATED model |
|
948 | + * (or transiently-related model) |
|
949 | + * |
|
950 | + * @return string |
|
951 | + */ |
|
952 | + public function model_chain_to_wp_user() |
|
953 | + { |
|
954 | + return $this->_model_chain_to_wp_user; |
|
955 | + } |
|
956 | + |
|
957 | + |
|
958 | + /** |
|
959 | + * Whether this model is 'owned' by a specific wordpress user (even indirectly, |
|
960 | + * like how registrations don't have a foreign key to wp_users, but the |
|
961 | + * events they are for are), or is unrelated to wp users. |
|
962 | + * generally available |
|
963 | + * |
|
964 | + * @return boolean |
|
965 | + */ |
|
966 | + public function is_owned() |
|
967 | + { |
|
968 | + if ($this->model_chain_to_wp_user()) { |
|
969 | + return true; |
|
970 | + } |
|
971 | + try { |
|
972 | + $this->get_foreign_key_to('WP_User'); |
|
973 | + return true; |
|
974 | + } catch (EE_Error $e) { |
|
975 | + return false; |
|
976 | + } |
|
977 | + } |
|
978 | + |
|
979 | + |
|
980 | + /** |
|
981 | + * Used internally to get WPDB results, because other functions, besides get_all, may want to do some queries, but |
|
982 | + * may want to preserve the WPDB results (eg, update, which first queries to make sure we have all the tables on |
|
983 | + * the model) |
|
984 | + * |
|
985 | + * @param array $query_params |
|
986 | + * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
987 | + * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
988 | + * fields on the model, and the models we joined to in the query. However, you can |
|
989 | + * override this and set the select to "*", or a specific column name, like |
|
990 | + * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
991 | + * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
992 | + * the aliases used to refer to this selection, and values are to be |
|
993 | + * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
994 | + * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
995 | + * @return array | stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
996 | + * @throws EE_Error |
|
997 | + * @throws InvalidArgumentException |
|
998 | + * @throws ReflectionException |
|
999 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1000 | + */ |
|
1001 | + protected function _get_all_wpdb_results($query_params = [], $output = ARRAY_A, $columns_to_select = null) |
|
1002 | + { |
|
1003 | + $this->_custom_selections = $this->getCustomSelection($query_params, $columns_to_select); |
|
1004 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
1005 | + $select_expressions = $columns_to_select === null |
|
1006 | + ? $this->_construct_default_select_sql($model_query_info) |
|
1007 | + : ''; |
|
1008 | + if ($this->_custom_selections instanceof CustomSelects) { |
|
1009 | + $custom_expressions = $this->_custom_selections->columnsToSelectExpression(); |
|
1010 | + $select_expressions .= $select_expressions |
|
1011 | + ? ', ' . $custom_expressions |
|
1012 | + : $custom_expressions; |
|
1013 | + } |
|
1014 | + |
|
1015 | + $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
1016 | + return $this->_do_wpdb_query('get_results', [$SQL, $output]); |
|
1017 | + } |
|
1018 | + |
|
1019 | + |
|
1020 | + /** |
|
1021 | + * Get a CustomSelects object if the $query_params or $columns_to_select allows for it. |
|
1022 | + * Note: $query_params['extra_selects'] will always override any $columns_to_select values. It is the preferred |
|
1023 | + * method of including extra select information. |
|
1024 | + * |
|
1025 | + * @param array $query_params |
|
1026 | + * @param null|array|string $columns_to_select |
|
1027 | + * @return null|CustomSelects |
|
1028 | + * @throws InvalidArgumentException |
|
1029 | + */ |
|
1030 | + protected function getCustomSelection(array $query_params, $columns_to_select = null) |
|
1031 | + { |
|
1032 | + if (! isset($query_params['extra_selects']) && $columns_to_select === null) { |
|
1033 | + return null; |
|
1034 | + } |
|
1035 | + $selects = isset($query_params['extra_selects']) ? $query_params['extra_selects'] : $columns_to_select; |
|
1036 | + $selects = is_string($selects) ? explode(',', $selects) : $selects; |
|
1037 | + return new CustomSelects($selects); |
|
1038 | + } |
|
1039 | + |
|
1040 | + |
|
1041 | + /** |
|
1042 | + * Gets an array of rows from the database just like $wpdb->get_results would, |
|
1043 | + * but you can use the model query params to more easily |
|
1044 | + * take care of joins, field preparation etc. |
|
1045 | + * |
|
1046 | + * @param array $query_params |
|
1047 | + * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
1048 | + * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
1049 | + * fields on the model, and the models we joined to in the query. However, you can |
|
1050 | + * override this and set the select to "*", or a specific column name, like |
|
1051 | + * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
1052 | + * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
1053 | + * the aliases used to refer to this selection, and values are to be |
|
1054 | + * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
1055 | + * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
1056 | + * @return array|stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
1057 | + * @throws EE_Error |
|
1058 | + * @throws ReflectionException |
|
1059 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1060 | + */ |
|
1061 | + public function get_all_wpdb_results($query_params = [], $output = ARRAY_A, $columns_to_select = null) |
|
1062 | + { |
|
1063 | + return $this->_get_all_wpdb_results($query_params, $output, $columns_to_select); |
|
1064 | + } |
|
1065 | + |
|
1066 | + |
|
1067 | + /** |
|
1068 | + * For creating a custom select statement |
|
1069 | + * |
|
1070 | + * @param array|string $columns_to_select either a string to be inserted directly as the select statement, |
|
1071 | + * or an array where keys are aliases, and values are arrays where 0=>the |
|
1072 | + * selection SQL, and 1=>is the datatype |
|
1073 | + * @return string |
|
1074 | + * @throws EE_Error |
|
1075 | + */ |
|
1076 | + private function _construct_select_from_input($columns_to_select) |
|
1077 | + { |
|
1078 | + if (is_array($columns_to_select)) { |
|
1079 | + $select_sql_array = []; |
|
1080 | + foreach ($columns_to_select as $alias => $selection_and_datatype) { |
|
1081 | + if (! is_array($selection_and_datatype) || ! isset($selection_and_datatype[1])) { |
|
1082 | + throw new EE_Error( |
|
1083 | + sprintf( |
|
1084 | + esc_html__( |
|
1085 | + "Custom selection %s (alias %s) needs to be an array like array('COUNT(REG_ID)','%%d')", |
|
1086 | + 'event_espresso' |
|
1087 | + ), |
|
1088 | + $selection_and_datatype, |
|
1089 | + $alias |
|
1090 | + ) |
|
1091 | + ); |
|
1092 | + } |
|
1093 | + if (! in_array($selection_and_datatype[1], $this->_valid_wpdb_data_types, true)) { |
|
1094 | + throw new EE_Error( |
|
1095 | + sprintf( |
|
1096 | + esc_html__( |
|
1097 | + "Datatype %s (for selection '%s' and alias '%s') is not a valid wpdb datatype (eg %%s)", |
|
1098 | + 'event_espresso' |
|
1099 | + ), |
|
1100 | + $selection_and_datatype[1], |
|
1101 | + $selection_and_datatype[0], |
|
1102 | + $alias, |
|
1103 | + implode(', ', $this->_valid_wpdb_data_types) |
|
1104 | + ) |
|
1105 | + ); |
|
1106 | + } |
|
1107 | + $select_sql_array[] = "{$selection_and_datatype[0]} AS $alias"; |
|
1108 | + } |
|
1109 | + $columns_to_select_string = implode(', ', $select_sql_array); |
|
1110 | + } else { |
|
1111 | + $columns_to_select_string = $columns_to_select; |
|
1112 | + } |
|
1113 | + return $columns_to_select_string; |
|
1114 | + } |
|
1115 | + |
|
1116 | + |
|
1117 | + /** |
|
1118 | + * Convenient wrapper for getting the primary key field's name. Eg, on Registration, this would be 'REG_ID' |
|
1119 | + * |
|
1120 | + * @return string |
|
1121 | + * @throws EE_Error |
|
1122 | + */ |
|
1123 | + public function primary_key_name() |
|
1124 | + { |
|
1125 | + return $this->get_primary_key_field()->get_name(); |
|
1126 | + } |
|
1127 | + |
|
1128 | + |
|
1129 | + /** |
|
1130 | + * Gets a single item for this model from the DB, given only its ID (or null if none is found). |
|
1131 | + * If there is no primary key on this model, $id is treated as primary key string |
|
1132 | + * |
|
1133 | + * @param mixed $id int or string, depending on the type of the model's primary key |
|
1134 | + * @return EE_Base_Class |
|
1135 | + * @throws EE_Error |
|
1136 | + * @throws ReflectionException |
|
1137 | + */ |
|
1138 | + public function get_one_by_ID($id) |
|
1139 | + { |
|
1140 | + if ($this->get_from_entity_map($id)) { |
|
1141 | + return $this->get_from_entity_map($id); |
|
1142 | + } |
|
1143 | + $model_object = $this->get_one( |
|
1144 | + $this->alter_query_params_to_restrict_by_ID( |
|
1145 | + $id, |
|
1146 | + ['default_where_conditions' => EEM_Base::default_where_conditions_minimum_all] |
|
1147 | + ) |
|
1148 | + ); |
|
1149 | + $className = $this->_get_class_name(); |
|
1150 | + if ($model_object instanceof $className) { |
|
1151 | + // make sure valid objects get added to the entity map |
|
1152 | + // so that the next call to this method doesn't trigger another trip to the db |
|
1153 | + $this->add_to_entity_map($model_object); |
|
1154 | + } |
|
1155 | + return $model_object; |
|
1156 | + } |
|
1157 | + |
|
1158 | + |
|
1159 | + /** |
|
1160 | + * Alters query parameters to only get items with this ID are returned. |
|
1161 | + * Takes into account that the ID might be a string produced by EEM_Base::get_index_primary_key_string(), |
|
1162 | + * or could just be a simple primary key ID |
|
1163 | + * |
|
1164 | + * @param int $id |
|
1165 | + * @param array $query_params see github link below for more info |
|
1166 | + * @return array of normal query params, |
|
1167 | + * @throws EE_Error |
|
1168 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1169 | + */ |
|
1170 | + public function alter_query_params_to_restrict_by_ID($id, $query_params = []) |
|
1171 | + { |
|
1172 | + if (! isset($query_params[0])) { |
|
1173 | + $query_params[0] = []; |
|
1174 | + } |
|
1175 | + $conditions_from_id = $this->parse_index_primary_key_string($id); |
|
1176 | + if ($conditions_from_id === null) { |
|
1177 | + $query_params[0][ $this->primary_key_name() ] = $id; |
|
1178 | + } else { |
|
1179 | + // no primary key, so the $id must be from the get_index_primary_key_string() |
|
1180 | + $query_params[0] = array_replace_recursive($query_params[0], $this->parse_index_primary_key_string($id)); |
|
1181 | + } |
|
1182 | + return $query_params; |
|
1183 | + } |
|
1184 | + |
|
1185 | + |
|
1186 | + /** |
|
1187 | + * Gets a single item for this model from the DB, given the $query_params. Only returns a single class, not an |
|
1188 | + * array. If no item is found, null is returned. |
|
1189 | + * |
|
1190 | + * @param array $query_params like EEM_Base's $query_params variable. |
|
1191 | + * @return EE_Base_Class|null |
|
1192 | + * @throws EE_Error |
|
1193 | + * @throws ReflectionException |
|
1194 | + */ |
|
1195 | + public function get_one(array $query_params = []) |
|
1196 | + { |
|
1197 | + if (! is_array($query_params)) { |
|
1198 | + EE_Error::doing_it_wrong( |
|
1199 | + 'EEM_Base::get_one', |
|
1200 | + sprintf( |
|
1201 | + esc_html__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
1202 | + gettype($query_params) |
|
1203 | + ), |
|
1204 | + '4.6.0' |
|
1205 | + ); |
|
1206 | + $query_params = []; |
|
1207 | + } |
|
1208 | + $query_params['limit'] = 1; |
|
1209 | + $items = $this->get_all($query_params); |
|
1210 | + if (empty($items)) { |
|
1211 | + return null; |
|
1212 | + } |
|
1213 | + return array_shift($items); |
|
1214 | + } |
|
1215 | + |
|
1216 | + |
|
1217 | + /** |
|
1218 | + * Returns the next x number of items in sequence from the given value as |
|
1219 | + * found in the database matching the given query conditions. |
|
1220 | + * |
|
1221 | + * @param mixed $current_field_value Value used for the reference point. |
|
1222 | + * @param null $field_to_order_by What field is used for the |
|
1223 | + * reference point. |
|
1224 | + * @param int $limit How many to return. |
|
1225 | + * @param array $query_params Extra conditions on the query. |
|
1226 | + * @param null $columns_to_select If left null, then an array of |
|
1227 | + * EE_Base_Class objects is returned, |
|
1228 | + * otherwise you can indicate just the |
|
1229 | + * columns you want returned. |
|
1230 | + * @return EE_Base_Class[]|array |
|
1231 | + * @throws EE_Error |
|
1232 | + * @throws ReflectionException |
|
1233 | + */ |
|
1234 | + public function next_x( |
|
1235 | + $current_field_value, |
|
1236 | + $field_to_order_by = null, |
|
1237 | + $limit = 1, |
|
1238 | + $query_params = [], |
|
1239 | + $columns_to_select = null |
|
1240 | + ) { |
|
1241 | + return $this->_get_consecutive( |
|
1242 | + $current_field_value, |
|
1243 | + '>', |
|
1244 | + $field_to_order_by, |
|
1245 | + $limit, |
|
1246 | + $query_params, |
|
1247 | + $columns_to_select |
|
1248 | + ); |
|
1249 | + } |
|
1250 | + |
|
1251 | + |
|
1252 | + /** |
|
1253 | + * Returns the previous x number of items in sequence from the given value |
|
1254 | + * as found in the database matching the given query conditions. |
|
1255 | + * |
|
1256 | + * @param mixed $current_field_value Value used for the reference point. |
|
1257 | + * @param null $field_to_order_by What field is used for the |
|
1258 | + * reference point. |
|
1259 | + * @param int $limit How many to return. |
|
1260 | + * @param array $query_params Extra conditions on the query. |
|
1261 | + * @param null $columns_to_select If left null, then an array of |
|
1262 | + * EE_Base_Class objects is returned, |
|
1263 | + * otherwise you can indicate just the |
|
1264 | + * columns you want returned. |
|
1265 | + * @return EE_Base_Class[]|array |
|
1266 | + * @throws EE_Error |
|
1267 | + * @throws ReflectionException |
|
1268 | + */ |
|
1269 | + public function previous_x( |
|
1270 | + $current_field_value, |
|
1271 | + $field_to_order_by = null, |
|
1272 | + $limit = 1, |
|
1273 | + $query_params = [], |
|
1274 | + $columns_to_select = null |
|
1275 | + ) { |
|
1276 | + return $this->_get_consecutive( |
|
1277 | + $current_field_value, |
|
1278 | + '<', |
|
1279 | + $field_to_order_by, |
|
1280 | + $limit, |
|
1281 | + $query_params, |
|
1282 | + $columns_to_select |
|
1283 | + ); |
|
1284 | + } |
|
1285 | + |
|
1286 | + |
|
1287 | + /** |
|
1288 | + * Returns the next item in sequence from the given value as found in the |
|
1289 | + * database matching the given query conditions. |
|
1290 | + * |
|
1291 | + * @param mixed $current_field_value Value used for the reference point. |
|
1292 | + * @param null $field_to_order_by What field is used for the |
|
1293 | + * reference point. |
|
1294 | + * @param array $query_params Extra conditions on the query. |
|
1295 | + * @param null $columns_to_select If left null, then an EE_Base_Class |
|
1296 | + * object is returned, otherwise you |
|
1297 | + * can indicate just the columns you |
|
1298 | + * want and a single array indexed by |
|
1299 | + * the columns will be returned. |
|
1300 | + * @return EE_Base_Class|null|array() |
|
1301 | + * @throws EE_Error |
|
1302 | + * @throws ReflectionException |
|
1303 | + */ |
|
1304 | + public function next( |
|
1305 | + $current_field_value, |
|
1306 | + $field_to_order_by = null, |
|
1307 | + $query_params = [], |
|
1308 | + $columns_to_select = null |
|
1309 | + ) { |
|
1310 | + $results = $this->_get_consecutive( |
|
1311 | + $current_field_value, |
|
1312 | + '>', |
|
1313 | + $field_to_order_by, |
|
1314 | + 1, |
|
1315 | + $query_params, |
|
1316 | + $columns_to_select |
|
1317 | + ); |
|
1318 | + return empty($results) ? null : reset($results); |
|
1319 | + } |
|
1320 | + |
|
1321 | + |
|
1322 | + /** |
|
1323 | + * Returns the previous item in sequence from the given value as found in |
|
1324 | + * the database matching the given query conditions. |
|
1325 | + * |
|
1326 | + * @param mixed $current_field_value Value used for the reference point. |
|
1327 | + * @param null $field_to_order_by What field is used for the |
|
1328 | + * reference point. |
|
1329 | + * @param array $query_params Extra conditions on the query. |
|
1330 | + * @param null $columns_to_select If left null, then an EE_Base_Class |
|
1331 | + * object is returned, otherwise you |
|
1332 | + * can indicate just the columns you |
|
1333 | + * want and a single array indexed by |
|
1334 | + * the columns will be returned. |
|
1335 | + * @return EE_Base_Class|null|array() |
|
1336 | + * @throws EE_Error |
|
1337 | + * @throws ReflectionException |
|
1338 | + */ |
|
1339 | + public function previous( |
|
1340 | + $current_field_value, |
|
1341 | + $field_to_order_by = null, |
|
1342 | + $query_params = [], |
|
1343 | + $columns_to_select = null |
|
1344 | + ) { |
|
1345 | + $results = $this->_get_consecutive( |
|
1346 | + $current_field_value, |
|
1347 | + '<', |
|
1348 | + $field_to_order_by, |
|
1349 | + 1, |
|
1350 | + $query_params, |
|
1351 | + $columns_to_select |
|
1352 | + ); |
|
1353 | + return empty($results) ? null : reset($results); |
|
1354 | + } |
|
1355 | + |
|
1356 | + |
|
1357 | + /** |
|
1358 | + * Returns the a consecutive number of items in sequence from the given |
|
1359 | + * value as found in the database matching the given query conditions. |
|
1360 | + * |
|
1361 | + * @param mixed $current_field_value Value used for the reference point. |
|
1362 | + * @param string $operand What operand is used for the sequence. |
|
1363 | + * @param string $field_to_order_by What field is used for the reference point. |
|
1364 | + * @param int $limit How many to return. |
|
1365 | + * @param array $query_params Extra conditions on the query. |
|
1366 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, |
|
1367 | + * otherwise you can indicate just the columns you want returned. |
|
1368 | + * @return EE_Base_Class[]|array |
|
1369 | + * @throws EE_Error |
|
1370 | + * @throws ReflectionException |
|
1371 | + */ |
|
1372 | + protected function _get_consecutive( |
|
1373 | + $current_field_value, |
|
1374 | + $operand = '>', |
|
1375 | + $field_to_order_by = null, |
|
1376 | + $limit = 1, |
|
1377 | + $query_params = [], |
|
1378 | + $columns_to_select = null |
|
1379 | + ) { |
|
1380 | + // if $field_to_order_by is empty then let's assume we're ordering by the primary key. |
|
1381 | + if (empty($field_to_order_by)) { |
|
1382 | + if ($this->has_primary_key_field()) { |
|
1383 | + $field_to_order_by = $this->get_primary_key_field()->get_name(); |
|
1384 | + } else { |
|
1385 | + if (WP_DEBUG) { |
|
1386 | + throw new EE_Error( |
|
1387 | + esc_html__( |
|
1388 | + 'EEM_Base::_get_consecutive() has been called with no $field_to_order_by argument and there is no primary key on the field. Please provide the field you would like to use as the base for retrieving the next item(s).', |
|
1389 | + 'event_espresso' |
|
1390 | + ) |
|
1391 | + ); |
|
1392 | + } |
|
1393 | + EE_Error::add_error(__('There was an error with the query.', 'event_espresso')); |
|
1394 | + return []; |
|
1395 | + } |
|
1396 | + } |
|
1397 | + if (! is_array($query_params)) { |
|
1398 | + EE_Error::doing_it_wrong( |
|
1399 | + 'EEM_Base::_get_consecutive', |
|
1400 | + sprintf( |
|
1401 | + esc_html__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
1402 | + gettype($query_params) |
|
1403 | + ), |
|
1404 | + '4.6.0' |
|
1405 | + ); |
|
1406 | + $query_params = []; |
|
1407 | + } |
|
1408 | + // let's add the where query param for consecutive look up. |
|
1409 | + $query_params[0][ $field_to_order_by ] = [$operand, $current_field_value]; |
|
1410 | + $query_params['limit'] = $limit; |
|
1411 | + // set direction |
|
1412 | + $incoming_orderby = isset($query_params['order_by']) ? (array) $query_params['order_by'] : []; |
|
1413 | + $query_params['order_by'] = $operand === '>' |
|
1414 | + ? [$field_to_order_by => 'ASC'] + $incoming_orderby |
|
1415 | + : [$field_to_order_by => 'DESC'] + $incoming_orderby; |
|
1416 | + // if $columns_to_select is empty then that means we're returning EE_Base_Class objects |
|
1417 | + if (empty($columns_to_select)) { |
|
1418 | + return $this->get_all($query_params); |
|
1419 | + } |
|
1420 | + // getting just the fields |
|
1421 | + return $this->_get_all_wpdb_results($query_params, ARRAY_A, $columns_to_select); |
|
1422 | + } |
|
1423 | + |
|
1424 | + |
|
1425 | + /** |
|
1426 | + * This sets the _timezone property after model object has been instantiated. |
|
1427 | + * |
|
1428 | + * @param string|null $timezone valid PHP DateTimeZone timezone string |
|
1429 | + */ |
|
1430 | + public function set_timezone($timezone = '') |
|
1431 | + { |
|
1432 | + static $set_in_progress = false; |
|
1433 | + // if incoming timezone string is empty, then use the existing |
|
1434 | + $valid_timezone = ! empty($timezone) && $timezone !== $this->_timezone |
|
1435 | + ? EEH_DTT_Helper::get_valid_timezone_string($timezone) |
|
1436 | + : $this->_timezone; |
|
1437 | + // do NOT set the timezone if we are already in the process of setting the timezone |
|
1438 | + // OR the existing timezone is already set and the incoming value is nothing (which gets set to default TZ) |
|
1439 | + // OR the existing timezone is already set and the validated value is the same as the existing timezone |
|
1440 | + if ( |
|
1441 | + $set_in_progress |
|
1442 | + || ( |
|
1443 | + ! empty($this->_timezone) |
|
1444 | + && ( |
|
1445 | + empty($timezone) || $valid_timezone === $this->_timezone |
|
1446 | + ) |
|
1447 | + ) |
|
1448 | + ) { |
|
1449 | + return; |
|
1450 | + } |
|
1451 | + $set_in_progress = true; |
|
1452 | + $this->_timezone = $valid_timezone ?: EEH_DTT_Helper::get_valid_timezone_string(); |
|
1453 | + // note we need to loop through relations and set the timezone on those objects as well. |
|
1454 | + foreach ($this->_model_relations as $relation) { |
|
1455 | + $relation->set_timezone($this->_timezone); |
|
1456 | + } |
|
1457 | + // and finally we do the same for any datetime fields |
|
1458 | + foreach ($this->_fields as $field) { |
|
1459 | + if ($field instanceof EE_Datetime_Field) { |
|
1460 | + $field->set_timezone($this->_timezone); |
|
1461 | + } |
|
1462 | + } |
|
1463 | + $set_in_progress = false; |
|
1464 | + } |
|
1465 | + |
|
1466 | + |
|
1467 | + /** |
|
1468 | + * This just returns whatever is set for the current timezone. |
|
1469 | + * |
|
1470 | + * @access public |
|
1471 | + * @return string |
|
1472 | + */ |
|
1473 | + public function get_timezone() |
|
1474 | + { |
|
1475 | + if (empty($this->_timezone)) { |
|
1476 | + $this->set_timezone(); |
|
1477 | + } |
|
1478 | + return $this->_timezone; |
|
1479 | + } |
|
1480 | + |
|
1481 | + |
|
1482 | + /** |
|
1483 | + * This returns the date formats set for the given field name and also ensures that |
|
1484 | + * $this->_timezone property is set correctly. |
|
1485 | + * |
|
1486 | + * @param string $field_name The name of the field the formats are being retrieved for. |
|
1487 | + * @param bool $pretty Whether to return the pretty formats (true) or not (false). |
|
1488 | + * @return array formats in an array with the date format first, and the time format last. |
|
1489 | + * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
1490 | + * @since 4.6.x |
|
1491 | + */ |
|
1492 | + public function get_formats_for($field_name, $pretty = false) |
|
1493 | + { |
|
1494 | + $field_settings = $this->field_settings_for($field_name); |
|
1495 | + // if not a valid EE_Datetime_Field then throw error |
|
1496 | + if (! $field_settings instanceof EE_Datetime_Field) { |
|
1497 | + throw new EE_Error( |
|
1498 | + sprintf( |
|
1499 | + esc_html__( |
|
1500 | + 'The field sent into EEM_Base::get_formats_for (%s) is not registered as a EE_Datetime_Field. Please check the spelling and make sure you are submitting the right field name to retrieve date_formats for.', |
|
1501 | + 'event_espresso' |
|
1502 | + ), |
|
1503 | + $field_name |
|
1504 | + ) |
|
1505 | + ); |
|
1506 | + } |
|
1507 | + // while we are here, let's make sure the timezone internally in EEM_Base matches what is stored on the field. |
|
1508 | + $field_timezone = $field_settings->get_timezone(); |
|
1509 | + if (empty($this->_timezone && $field_timezone)) { |
|
1510 | + $this->set_timezone(); |
|
1511 | + } else { |
|
1512 | + // or vice versa if the field TZ isn't set |
|
1513 | + $model_timezone = $this->get_timezone(); |
|
1514 | + $field_settings->set_timezone($model_timezone); |
|
1515 | + } |
|
1516 | + |
|
1517 | + return [$field_settings->get_date_format($pretty), $field_settings->get_time_format($pretty)]; |
|
1518 | + } |
|
1519 | + |
|
1520 | + /** |
|
1521 | + * This returns the current time in a format setup for a query on this model. |
|
1522 | + * Usage of this method makes it easier to setup queries against EE_Datetime_Field columns because |
|
1523 | + * it will return: |
|
1524 | + * - a formatted string in the timezone and format currently set on the EE_Datetime_Field |
|
1525 | + * for the given field for NOW |
|
1526 | + * - or a unix timestamp (equivalent to time()) |
|
1527 | + * Note: When requesting a formatted string, if the date or time format doesn't include seconds, for example, |
|
1528 | + * the time returned, because it uses that format, will also NOT include seconds. For this reason, if you want |
|
1529 | + * the time returned to be the current time down to the exact second, set $timestamp to true. |
|
1530 | + * |
|
1531 | + * @param string $field_name The field the current time is needed for. |
|
1532 | + * @param bool $timestamp True means to return a unix timestamp. Otherwise a |
|
1533 | + * formatted string matching the set format for the field in the set timezone will |
|
1534 | + * be returned. |
|
1535 | + * @param string $what Whether to return the string in just the time format, the date format, or both. |
|
1536 | + * @return int|string If the given field_name is not of the EE_Datetime_Field type, then an EE_Error |
|
1537 | + * exception is triggered. |
|
1538 | + * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
1539 | + * @throws Exception |
|
1540 | + * @since 4.6.x |
|
1541 | + */ |
|
1542 | + public function current_time_for_query($field_name, $timestamp = false, $what = 'both') |
|
1543 | + { |
|
1544 | + $formats = $this->get_formats_for($field_name); |
|
1545 | + $DateTime = new DateTime("now", new DateTimeZone($this->get_timezone())); |
|
1546 | + if ($timestamp) { |
|
1547 | + return $DateTime->format('U'); |
|
1548 | + } |
|
1549 | + // not returning timestamp, so return formatted string in timezone. |
|
1550 | + switch ($what) { |
|
1551 | + case 'time': |
|
1552 | + return $DateTime->format($formats[1]); |
|
1553 | + case 'date': |
|
1554 | + return $DateTime->format($formats[0]); |
|
1555 | + default: |
|
1556 | + return $DateTime->format(implode(' ', $formats)); |
|
1557 | + } |
|
1558 | + } |
|
1559 | + |
|
1560 | + |
|
1561 | + /** |
|
1562 | + * This receives a time string for a given field and ensures that it is setup to match what the internal settings |
|
1563 | + * for the model are. Returns a DateTime object. |
|
1564 | + * Note: a gotcha for when you send in unix timestamp. Remember a unix timestamp is already timezone agnostic, |
|
1565 | + * (functionally the equivalent of UTC+0). So when you send it in, whatever timezone string you include is |
|
1566 | + * ignored. |
|
1567 | + * |
|
1568 | + * @param string $field_name The field being setup. |
|
1569 | + * @param string $timestring The date time string being used. |
|
1570 | + * @param string $format The format for the time string. |
|
1571 | + * @param string $timezone By default, it is assumed the incoming time string is in timezone for |
|
1572 | + * the blog. If this is not the case, then it can be specified here. |
|
1573 | + * If incoming format is 'U', this is ignored. |
|
1574 | + * @return DbSafeDateTime |
|
1575 | + * @throws EE_Error |
|
1576 | + * @throws Exception |
|
1577 | + */ |
|
1578 | + public function convert_datetime_for_query($field_name, $timestring, $format, $timezone = '') |
|
1579 | + { |
|
1580 | + // just using this to ensure the timezone is set correctly internally |
|
1581 | + $this->get_formats_for($field_name); |
|
1582 | + $timezone = ! empty($timezone) ? $timezone : EEH_DTT_Helper::get_timezone(); |
|
1583 | + $db_safe_datetime = DbSafeDateTime::createFromFormat($format, $timestring, new DateTimeZone($timezone)); |
|
1584 | + EEH_DTT_Helper::setTimezone($db_safe_datetime, new DateTimeZone($this->get_timezone())); |
|
1585 | + return $db_safe_datetime; |
|
1586 | + } |
|
1587 | + |
|
1588 | + |
|
1589 | + /** |
|
1590 | + * Gets all the tables comprising this model. Array keys are the table aliases, and values are EE_Table objects |
|
1591 | + * |
|
1592 | + * @return EE_Table_Base[] |
|
1593 | + */ |
|
1594 | + public function get_tables() |
|
1595 | + { |
|
1596 | + return $this->_tables; |
|
1597 | + } |
|
1598 | + |
|
1599 | + |
|
1600 | + /** |
|
1601 | + * Updates all the database entries (in each table for this model) according to $fields_n_values and optionally |
|
1602 | + * also updates all the model objects, where the criteria expressed in $query_params are met.. |
|
1603 | + * Also note: if this model has multiple tables, this update verifies all the secondary tables have an entry for |
|
1604 | + * each row (in the primary table) we're trying to update; if not, it inserts an entry in the secondary table. Eg: |
|
1605 | + * if our model has 2 tables: wp_posts (primary), and wp_esp_event (secondary). Let's say we are trying to update a |
|
1606 | + * model object with EVT_ID = 1 |
|
1607 | + * (which means where wp_posts has ID = 1, because wp_posts.ID is the primary key's column), which exists, but |
|
1608 | + * there is no entry in wp_esp_event for this entry in wp_posts. So, this update script will insert a row into |
|
1609 | + * wp_esp_event, using any available parameters from $fields_n_values (eg, if "EVT_limit" => 40 is in |
|
1610 | + * $fields_n_values, the new entry in wp_esp_event will set EVT_limit = 40, and use default for other columns which |
|
1611 | + * are not specified) |
|
1612 | + * |
|
1613 | + * @param array $fields_n_values keys are model fields (exactly like keys in EEM_Base::_fields, NOT db |
|
1614 | + * columns!), values are strings, integers, floats, and maybe arrays if |
|
1615 | + * they |
|
1616 | + * are to be serialized. Basically, the values are what you'd expect to be |
|
1617 | + * values on the model, NOT necessarily what's in the DB. For example, if |
|
1618 | + * we wanted to update only the TXN_details on any Transactions where its |
|
1619 | + * ID=34, we'd use this method as follows: |
|
1620 | + * EEM_Transaction::instance()->update( |
|
1621 | + * array('TXN_details'=>array('detail1'=>'monkey','detail2'=>'banana'), |
|
1622 | + * array(array('TXN_ID'=>34))); |
|
1623 | + * @param array $query_params Eg, consider updating Question's QST_admin_label field is of type |
|
1624 | + * Simple_HTML. If you use this function to update that field to $new_value |
|
1625 | + * = (note replace 8's with appropriate opening and closing tags in the |
|
1626 | + * following example)"8script8alert('I hack all');8/script88b8boom |
|
1627 | + * baby8/b8", then if you set $values_already_prepared_by_model_object to |
|
1628 | + * TRUE, it is assumed that you've already called |
|
1629 | + * EE_Simple_HTML_Field->prepare_for_set($new_value), which removes the |
|
1630 | + * malicious javascript. However, if |
|
1631 | + * $values_already_prepared_by_model_object is left as FALSE, then |
|
1632 | + * EE_Simple_HTML_Field->prepare_for_set($new_value) will be called on it, |
|
1633 | + * and every other field, before insertion. We provide this parameter |
|
1634 | + * because model objects perform their prepare_for_set function on all |
|
1635 | + * their values, and so don't need to be called again (and in many cases, |
|
1636 | + * shouldn't be called again. Eg: if we escape HTML characters in the |
|
1637 | + * prepare_for_set method...) |
|
1638 | + * @param boolean $keep_model_objs_in_sync if TRUE, makes sure we ALSO update model objects |
|
1639 | + * in this model's entity map according to $fields_n_values that match |
|
1640 | + * $query_params. This obviously has some overhead, so you can disable it |
|
1641 | + * by setting this to FALSE, but be aware that model objects being used |
|
1642 | + * could get out-of-sync with the database |
|
1643 | + * @return int how many rows got updated or FALSE if something went wrong with the query (wp returns FALSE or num |
|
1644 | + * rows affected which *could* include 0 which DOES NOT mean the query was |
|
1645 | + * bad) |
|
1646 | + * @throws EE_Error |
|
1647 | + * @throws ReflectionException |
|
1648 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1649 | + */ |
|
1650 | + public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true) |
|
1651 | + { |
|
1652 | + if (! is_array($query_params)) { |
|
1653 | + EE_Error::doing_it_wrong( |
|
1654 | + 'EEM_Base::update', |
|
1655 | + sprintf( |
|
1656 | + esc_html__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
1657 | + gettype($query_params) |
|
1658 | + ), |
|
1659 | + '4.6.0' |
|
1660 | + ); |
|
1661 | + $query_params = []; |
|
1662 | + } |
|
1663 | + /** |
|
1664 | + * Action called before a model update call has been made. |
|
1665 | + * |
|
1666 | + * @param EEM_Base $model |
|
1667 | + * @param array $fields_n_values the updated fields and their new values |
|
1668 | + * @param array $query_params |
|
1669 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
1670 | + */ |
|
1671 | + do_action('AHEE__EEM_Base__update__begin', $this, $fields_n_values, $query_params); |
|
1672 | + /** |
|
1673 | + * Filters the fields about to be updated given the query parameters. You can provide the |
|
1674 | + * $query_params to $this->get_all() to find exactly which records will be updated |
|
1675 | + * |
|
1676 | + * @param array $fields_n_values fields and their new values |
|
1677 | + * @param EEM_Base $model the model being queried |
|
1678 | + * @param array $query_params |
|
1679 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
1680 | + */ |
|
1681 | + $fields_n_values = (array) apply_filters( |
|
1682 | + 'FHEE__EEM_Base__update__fields_n_values', |
|
1683 | + $fields_n_values, |
|
1684 | + $this, |
|
1685 | + $query_params |
|
1686 | + ); |
|
1687 | + // need to verify that, for any entry we want to update, there are entries in each secondary table. |
|
1688 | + // to do that, for each table, verify that it's PK isn't null. |
|
1689 | + $tables = $this->get_tables(); |
|
1690 | + // and if the other tables don't have a row for each table-to-be-updated, we'll insert one with whatever values available in the current update query |
|
1691 | + // NOTE: we should make this code more efficient by NOT querying twice |
|
1692 | + // before the real update, but that needs to first go through ALPHA testing |
|
1693 | + // as it's dangerous. says Mike August 8 2014 |
|
1694 | + // we want to make sure the default_where strategy is ignored |
|
1695 | + $this->_ignore_where_strategy = true; |
|
1696 | + $wpdb_select_results = $this->_get_all_wpdb_results($query_params); |
|
1697 | + foreach ($wpdb_select_results as $wpdb_result) { |
|
1698 | + // type cast stdClass as array |
|
1699 | + $wpdb_result = (array) $wpdb_result; |
|
1700 | + // get the model object's PK, as we'll want this if we need to insert a row into secondary tables |
|
1701 | + if ($this->has_primary_key_field()) { |
|
1702 | + $main_table_pk_value = $wpdb_result[ $this->get_primary_key_field()->get_qualified_column() ]; |
|
1703 | + } else { |
|
1704 | + // if there's no primary key, we basically can't support having a 2nd table on the model (we could but it would be lots of work) |
|
1705 | + $main_table_pk_value = null; |
|
1706 | + } |
|
1707 | + // if there are more than 1 tables, we'll want to verify that each table for this model has an entry in the other tables |
|
1708 | + // and if the other tables don't have a row for each table-to-be-updated, we'll insert one with whatever values available in the current update query |
|
1709 | + if (count($tables) > 1) { |
|
1710 | + // foreach matching row in the DB, ensure that each table's PK isn't null. If so, there must not be an entry |
|
1711 | + // in that table, and so we'll want to insert one |
|
1712 | + foreach ($tables as $table_obj) { |
|
1713 | + $this_table_pk_column = $table_obj->get_fully_qualified_pk_column(); |
|
1714 | + // if there is no private key for this table on the results, it means there's no entry |
|
1715 | + // in this table, right? so insert a row in the current table, using any fields available |
|
1716 | + if ( |
|
1717 | + ! (array_key_exists($this_table_pk_column, $wpdb_result) |
|
1718 | + && $wpdb_result[ $this_table_pk_column ]) |
|
1719 | + ) { |
|
1720 | + $success = $this->_insert_into_specific_table( |
|
1721 | + $table_obj, |
|
1722 | + $fields_n_values, |
|
1723 | + $main_table_pk_value |
|
1724 | + ); |
|
1725 | + // if we died here, report the error |
|
1726 | + if (! $success) { |
|
1727 | + return false; |
|
1728 | + } |
|
1729 | + } |
|
1730 | + } |
|
1731 | + } |
|
1732 | + // let's make sure default_where strategy is followed now |
|
1733 | + $this->_ignore_where_strategy = false; |
|
1734 | + } |
|
1735 | + // if we want to keep model objects in sync, AND |
|
1736 | + // if this wasn't called from a model object (to update itself) |
|
1737 | + // then we want to make sure we keep all the existing |
|
1738 | + // model objects in sync with the db |
|
1739 | + if ($keep_model_objs_in_sync && ! $this->_values_already_prepared_by_model_object) { |
|
1740 | + if ($this->has_primary_key_field()) { |
|
1741 | + $model_objs_affected_ids = $this->get_col($query_params); |
|
1742 | + } else { |
|
1743 | + // we need to select a bunch of columns and then combine them into the the "index primary key string"s |
|
1744 | + $models_affected_key_columns = $this->_get_all_wpdb_results($query_params); |
|
1745 | + $model_objs_affected_ids = []; |
|
1746 | + foreach ($models_affected_key_columns as $row) { |
|
1747 | + $combined_index_key = $this->get_index_primary_key_string($row); |
|
1748 | + $model_objs_affected_ids[ $combined_index_key ] = $combined_index_key; |
|
1749 | + } |
|
1750 | + } |
|
1751 | + if (! $model_objs_affected_ids) { |
|
1752 | + // wait wait wait- if nothing was affected let's stop here |
|
1753 | + return 0; |
|
1754 | + } |
|
1755 | + foreach ($model_objs_affected_ids as $id) { |
|
1756 | + $model_obj_in_entity_map = $this->get_from_entity_map($id); |
|
1757 | + if ($model_obj_in_entity_map) { |
|
1758 | + foreach ($fields_n_values as $field => $new_value) { |
|
1759 | + $model_obj_in_entity_map->set($field, $new_value); |
|
1760 | + } |
|
1761 | + } |
|
1762 | + } |
|
1763 | + // if there is a primary key on this model, we can now do a slight optimization |
|
1764 | + if ($this->has_primary_key_field()) { |
|
1765 | + // we already know what we want to update. So let's make the query simpler so it's a little more efficient |
|
1766 | + $query_params = [ |
|
1767 | + [$this->primary_key_name() => ['IN', $model_objs_affected_ids]], |
|
1768 | + 'limit' => count($model_objs_affected_ids), |
|
1769 | + 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
1770 | + ]; |
|
1771 | + } |
|
1772 | + } |
|
1773 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
1774 | + $SQL = "UPDATE " . $model_query_info->get_full_join_sql() |
|
1775 | + . " SET " . $this->_construct_update_sql($fields_n_values) |
|
1776 | + . $model_query_info->get_where_sql(); |
|
1777 | + // note: doesn't use _construct_2nd_half_of_select_query() because doesn't accept LIMIT, ORDER BY, etc. |
|
1778 | + $rows_affected = $this->_do_wpdb_query('query', [$SQL]); |
|
1779 | + /** |
|
1780 | + * Action called after a model update call has been made. |
|
1781 | + * |
|
1782 | + * @param EEM_Base $model |
|
1783 | + * @param array $fields_n_values the updated fields and their new values |
|
1784 | + * @param array $query_params |
|
1785 | + * @param int $rows_affected |
|
1786 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
1787 | + */ |
|
1788 | + do_action('AHEE__EEM_Base__update__end', $this, $fields_n_values, $query_params, $rows_affected); |
|
1789 | + return $rows_affected;// how many supposedly got updated |
|
1790 | + } |
|
1791 | + |
|
1792 | + |
|
1793 | + /** |
|
1794 | + * Analogous to $wpdb->get_col, returns a 1-dimensional array where teh values |
|
1795 | + * are teh values of the field specified (or by default the primary key field) |
|
1796 | + * that matched the query params. Note that you should pass the name of the |
|
1797 | + * model FIELD, not the database table's column name. |
|
1798 | + * |
|
1799 | + * @param array $query_params |
|
1800 | + * @param string $field_to_select |
|
1801 | + * @return array just like $wpdb->get_col() |
|
1802 | + * @throws EE_Error |
|
1803 | + * @throws ReflectionException |
|
1804 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1805 | + */ |
|
1806 | + public function get_col($query_params = [], $field_to_select = null) |
|
1807 | + { |
|
1808 | + if ($field_to_select) { |
|
1809 | + $field = $this->field_settings_for($field_to_select); |
|
1810 | + } elseif ($this->has_primary_key_field()) { |
|
1811 | + $field = $this->get_primary_key_field(); |
|
1812 | + } else { |
|
1813 | + $field_settings = $this->field_settings(); |
|
1814 | + // no primary key, just grab the first column |
|
1815 | + $field = reset($field_settings); |
|
1816 | + } |
|
1817 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
1818 | + $select_expressions = $field->get_qualified_column(); |
|
1819 | + $SQL = |
|
1820 | + "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
1821 | + return $this->_do_wpdb_query('get_col', [$SQL]); |
|
1822 | + } |
|
1823 | + |
|
1824 | + |
|
1825 | + /** |
|
1826 | + * Returns a single column value for a single row from the database |
|
1827 | + * |
|
1828 | + * @param array $query_params |
|
1829 | + * @param string $field_to_select |
|
1830 | + * @return string |
|
1831 | + * @throws EE_Error |
|
1832 | + * @throws ReflectionException |
|
1833 | + * @see EEM_Base::get_col() |
|
1834 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1835 | + */ |
|
1836 | + public function get_var($query_params = [], $field_to_select = null) |
|
1837 | + { |
|
1838 | + $query_params['limit'] = 1; |
|
1839 | + $col = $this->get_col($query_params, $field_to_select); |
|
1840 | + if (! empty($col)) { |
|
1841 | + return reset($col); |
|
1842 | + } |
|
1843 | + return null; |
|
1844 | + } |
|
1845 | + |
|
1846 | + |
|
1847 | + /** |
|
1848 | + * Makes the SQL for after "UPDATE table_X inner join table_Y..." and before "...WHERE". Eg "Question.name='party |
|
1849 | + * time?', Question.desc='what do you think?'..." Values are filtered through wpdb->prepare to avoid against SQL |
|
1850 | + * injection, but currently no further filtering is done |
|
1851 | + * |
|
1852 | + * @param array $fields_n_values array keys are field names on this model, and values are what those fields should |
|
1853 | + * be updated to in the DB |
|
1854 | + * @return string of SQL |
|
1855 | + * @throws EE_Error |
|
1856 | + * @global $wpdb |
|
1857 | + */ |
|
1858 | + public function _construct_update_sql($fields_n_values) |
|
1859 | + { |
|
1860 | + global $wpdb; |
|
1861 | + $cols_n_values = []; |
|
1862 | + foreach ($fields_n_values as $field_name => $value) { |
|
1863 | + $field_obj = $this->field_settings_for($field_name); |
|
1864 | + // if the value is NULL, we want to assign the value to that. |
|
1865 | + // wpdb->prepare doesn't really handle that properly |
|
1866 | + $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
1867 | + $value_sql = $prepared_value === null |
|
1868 | + ? 'NULL' |
|
1869 | + : $wpdb->prepare($field_obj->get_wpdb_data_type(), $prepared_value); |
|
1870 | + $cols_n_values[] = $field_obj->get_qualified_column() . "=" . $value_sql; |
|
1871 | + } |
|
1872 | + return implode(",", $cols_n_values); |
|
1873 | + } |
|
1874 | + |
|
1875 | + |
|
1876 | + /** |
|
1877 | + * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
1878 | + * Performs a HARD delete, meaning the database row should always be removed, |
|
1879 | + * not just have a flag field on it switched |
|
1880 | + * Wrapper for EEM_Base::delete_permanently() |
|
1881 | + * |
|
1882 | + * @param mixed $id |
|
1883 | + * @param boolean $allow_blocking |
|
1884 | + * @return int the number of rows deleted |
|
1885 | + * @throws EE_Error |
|
1886 | + * @throws ReflectionException |
|
1887 | + */ |
|
1888 | + public function delete_permanently_by_ID($id, $allow_blocking = true) |
|
1889 | + { |
|
1890 | + return $this->delete_permanently( |
|
1891 | + [ |
|
1892 | + [$this->get_primary_key_field()->get_name() => $id], |
|
1893 | + 'limit' => 1, |
|
1894 | + ], |
|
1895 | + $allow_blocking |
|
1896 | + ); |
|
1897 | + } |
|
1898 | + |
|
1899 | + |
|
1900 | + /** |
|
1901 | + * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
1902 | + * Wrapper for EEM_Base::delete() |
|
1903 | + * |
|
1904 | + * @param mixed $id |
|
1905 | + * @param boolean $allow_blocking |
|
1906 | + * @return int the number of rows deleted |
|
1907 | + * @throws EE_Error |
|
1908 | + * @throws ReflectionException |
|
1909 | + */ |
|
1910 | + public function delete_by_ID($id, $allow_blocking = true) |
|
1911 | + { |
|
1912 | + return $this->delete( |
|
1913 | + [ |
|
1914 | + [$this->get_primary_key_field()->get_name() => $id], |
|
1915 | + 'limit' => 1, |
|
1916 | + ], |
|
1917 | + $allow_blocking |
|
1918 | + ); |
|
1919 | + } |
|
1920 | + |
|
1921 | + |
|
1922 | + /** |
|
1923 | + * Identical to delete_permanently, but does a "soft" delete if possible, |
|
1924 | + * meaning if the model has a field that indicates its been "trashed" or |
|
1925 | + * "soft deleted", we will just set that instead of actually deleting the rows. |
|
1926 | + * |
|
1927 | + * @param array $query_params |
|
1928 | + * @param boolean $allow_blocking |
|
1929 | + * @return int how many rows got deleted |
|
1930 | + * @throws EE_Error |
|
1931 | + * @throws ReflectionException |
|
1932 | + * @see EEM_Base::delete_permanently |
|
1933 | + */ |
|
1934 | + public function delete($query_params, $allow_blocking = true) |
|
1935 | + { |
|
1936 | + return $this->delete_permanently($query_params, $allow_blocking); |
|
1937 | + } |
|
1938 | + |
|
1939 | + |
|
1940 | + /** |
|
1941 | + * Deletes the model objects that meet the query params. Note: this method is overridden |
|
1942 | + * in EEM_Soft_Delete_Base so that soft-deleted model objects are instead only flagged |
|
1943 | + * as archived, not actually deleted |
|
1944 | + * |
|
1945 | + * @param array $query_params |
|
1946 | + * @param boolean $allow_blocking if TRUE, matched objects will only be deleted if there is no related model info |
|
1947 | + * that blocks it (ie, there' sno other data that depends on this data); if false, |
|
1948 | + * deletes regardless of other objects which may depend on it. Its generally |
|
1949 | + * advisable to always leave this as TRUE, otherwise you could easily corrupt your |
|
1950 | + * DB |
|
1951 | + * @return int how many rows got deleted |
|
1952 | + * @throws EE_Error |
|
1953 | + * @throws ReflectionException |
|
1954 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
1955 | + */ |
|
1956 | + public function delete_permanently($query_params, $allow_blocking = true) |
|
1957 | + { |
|
1958 | + /** |
|
1959 | + * Action called just before performing a real deletion query. You can use the |
|
1960 | + * model and its $query_params to find exactly which items will be deleted |
|
1961 | + * |
|
1962 | + * @param EEM_Base $model |
|
1963 | + * @param array $query_params |
|
1964 | + * @param boolean $allow_blocking whether or not to allow related model objects |
|
1965 | + * to block (prevent) this deletion |
|
1966 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
1967 | + */ |
|
1968 | + do_action('AHEE__EEM_Base__delete__begin', $this, $query_params, $allow_blocking); |
|
1969 | + // some MySQL databases may be running safe mode, which may restrict |
|
1970 | + // deletion if there is no KEY column used in the WHERE statement of a deletion. |
|
1971 | + // to get around this, we first do a SELECT, get all the IDs, and then run another query |
|
1972 | + // to delete them |
|
1973 | + $items_for_deletion = $this->_get_all_wpdb_results($query_params); |
|
1974 | + $columns_and_ids_for_deleting = $this->_get_ids_for_delete($items_for_deletion, $allow_blocking); |
|
1975 | + $deletion_where_query_part = $this->_build_query_part_for_deleting_from_columns_and_values( |
|
1976 | + $columns_and_ids_for_deleting |
|
1977 | + ); |
|
1978 | + /** |
|
1979 | + * Allows client code to act on the items being deleted before the query is actually executed. |
|
1980 | + * |
|
1981 | + * @param EEM_Base $this The model instance being acted on. |
|
1982 | + * @param array $query_params The incoming array of query parameters influencing what gets deleted. |
|
1983 | + * @param bool $allow_blocking @see param description in method phpdoc block. |
|
1984 | + * @param array $columns_and_ids_for_deleting An array indicating what entities will get removed as |
|
1985 | + * derived from the incoming query parameters. |
|
1986 | + * @see details on the structure of this array in the phpdocs |
|
1987 | + * for the `_get_ids_for_delete_method` |
|
1988 | + * |
|
1989 | + */ |
|
1990 | + do_action( |
|
1991 | + 'AHEE__EEM_Base__delete__before_query', |
|
1992 | + $this, |
|
1993 | + $query_params, |
|
1994 | + $allow_blocking, |
|
1995 | + $columns_and_ids_for_deleting |
|
1996 | + ); |
|
1997 | + if ($deletion_where_query_part) { |
|
1998 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
1999 | + $table_aliases = implode(', ', array_keys($this->_tables)); |
|
2000 | + $SQL = "DELETE " . $table_aliases |
|
2001 | + . " FROM " . $model_query_info->get_full_join_sql() |
|
2002 | + . " WHERE " . $deletion_where_query_part; |
|
2003 | + $rows_deleted = $this->_do_wpdb_query('query', [$SQL]); |
|
2004 | + } else { |
|
2005 | + $rows_deleted = 0; |
|
2006 | + } |
|
2007 | + |
|
2008 | + // Next, make sure those items are removed from the entity map; if they could be put into it at all; and if |
|
2009 | + // there was no error with the delete query. |
|
2010 | + if ( |
|
2011 | + $this->has_primary_key_field() |
|
2012 | + && $rows_deleted !== false |
|
2013 | + && isset($columns_and_ids_for_deleting[ $this->get_primary_key_field()->get_qualified_column() ]) |
|
2014 | + ) { |
|
2015 | + $ids_for_removal = $columns_and_ids_for_deleting[ $this->get_primary_key_field()->get_qualified_column() ]; |
|
2016 | + foreach ($ids_for_removal as $id) { |
|
2017 | + if (isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ])) { |
|
2018 | + unset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]); |
|
2019 | + } |
|
2020 | + } |
|
2021 | + |
|
2022 | + // delete any extra meta attached to the deleted entities but ONLY if this model is not an instance of |
|
2023 | + // `EEM_Extra_Meta`. In other words we want to prevent recursion on EEM_Extra_Meta::delete_permanently calls |
|
2024 | + // unnecessarily. It's very unlikely that users will have assigned Extra Meta to Extra Meta |
|
2025 | + // (although it is possible). |
|
2026 | + // Note this can be skipped by using the provided filter and returning false. |
|
2027 | + if ( |
|
2028 | + apply_filters( |
|
2029 | + 'FHEE__EEM_Base__delete_permanently__dont_delete_extra_meta_for_extra_meta', |
|
2030 | + ! $this instanceof EEM_Extra_Meta, |
|
2031 | + $this |
|
2032 | + ) |
|
2033 | + ) { |
|
2034 | + EEM_Extra_Meta::instance()->delete_permanently( |
|
2035 | + [ |
|
2036 | + 0 => [ |
|
2037 | + 'EXM_type' => $this->get_this_model_name(), |
|
2038 | + 'OBJ_ID' => [ |
|
2039 | + 'IN', |
|
2040 | + $ids_for_removal, |
|
2041 | + ], |
|
2042 | + ], |
|
2043 | + ] |
|
2044 | + ); |
|
2045 | + } |
|
2046 | + } |
|
2047 | + |
|
2048 | + /** |
|
2049 | + * Action called just after performing a real deletion query. Although at this point the |
|
2050 | + * items should have been deleted |
|
2051 | + * |
|
2052 | + * @param EEM_Base $model |
|
2053 | + * @param array $query_params |
|
2054 | + * @param int $rows_deleted |
|
2055 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
2056 | + */ |
|
2057 | + do_action('AHEE__EEM_Base__delete__end', $this, $query_params, $rows_deleted, $columns_and_ids_for_deleting); |
|
2058 | + return $rows_deleted;// how many supposedly got deleted |
|
2059 | + } |
|
2060 | + |
|
2061 | + |
|
2062 | + /** |
|
2063 | + * Checks all the relations that throw error messages when there are blocking related objects |
|
2064 | + * for related model objects. If there are any related model objects on those relations, |
|
2065 | + * adds an EE_Error, and return true |
|
2066 | + * |
|
2067 | + * @param EE_Base_Class|int $this_model_obj_or_id |
|
2068 | + * @param EE_Base_Class $ignore_this_model_obj a model object like 'EE_Event', or 'EE_Term_Taxonomy', which |
|
2069 | + * should be ignored when determining whether there are related |
|
2070 | + * model objects which block this model object's deletion. Useful |
|
2071 | + * if you know A is related to B and are considering deleting A, |
|
2072 | + * but want to see if A has any other objects blocking its deletion |
|
2073 | + * before removing the relation between A and B |
|
2074 | + * @return boolean |
|
2075 | + * @throws EE_Error |
|
2076 | + * @throws ReflectionException |
|
2077 | + */ |
|
2078 | + public function delete_is_blocked_by_related_models($this_model_obj_or_id, $ignore_this_model_obj = null) |
|
2079 | + { |
|
2080 | + // first, if $ignore_this_model_obj was supplied, get its model |
|
2081 | + if ($ignore_this_model_obj && $ignore_this_model_obj instanceof EE_Base_Class) { |
|
2082 | + $ignored_model = $ignore_this_model_obj->get_model(); |
|
2083 | + } else { |
|
2084 | + $ignored_model = null; |
|
2085 | + } |
|
2086 | + // now check all the relations of $this_model_obj_or_id and see if there |
|
2087 | + // are any related model objects blocking it? |
|
2088 | + $is_blocked = false; |
|
2089 | + foreach ($this->_model_relations as $relation_name => $relation_obj) { |
|
2090 | + if ($relation_obj->block_delete_if_related_models_exist()) { |
|
2091 | + // if $ignore_this_model_obj was supplied, then for the query |
|
2092 | + // on that model needs to be told to ignore $ignore_this_model_obj |
|
2093 | + if ($ignored_model && $relation_name === $ignored_model->get_this_model_name()) { |
|
2094 | + $related_model_objects = $relation_obj->get_all_related( |
|
2095 | + $this_model_obj_or_id, |
|
2096 | + [ |
|
2097 | + [ |
|
2098 | + $ignored_model->get_primary_key_field()->get_name() => [ |
|
2099 | + '!=', |
|
2100 | + $ignore_this_model_obj->ID(), |
|
2101 | + ], |
|
2102 | + ], |
|
2103 | + ] |
|
2104 | + ); |
|
2105 | + } else { |
|
2106 | + $related_model_objects = $relation_obj->get_all_related($this_model_obj_or_id); |
|
2107 | + } |
|
2108 | + if ($related_model_objects) { |
|
2109 | + EE_Error::add_error($relation_obj->get_deletion_error_message(), __FILE__, __FUNCTION__, __LINE__); |
|
2110 | + $is_blocked = true; |
|
2111 | + } |
|
2112 | + } |
|
2113 | + } |
|
2114 | + return $is_blocked; |
|
2115 | + } |
|
2116 | + |
|
2117 | + |
|
2118 | + /** |
|
2119 | + * Builds the columns and values for items to delete from the incoming $row_results_for_deleting array. |
|
2120 | + * |
|
2121 | + * @param array $row_results_for_deleting |
|
2122 | + * @param bool $allow_blocking |
|
2123 | + * @return array The shape of this array depends on whether the model `has_primary_key_field` or not. If the |
|
2124 | + * model DOES have a primary_key_field, then the array will be a simple single |
|
2125 | + * dimension array where the key is the fully qualified primary key column and the |
|
2126 | + * value is an array of ids that will be deleted. Example: array('Event.EVT_ID' => |
|
2127 | + * array( 1,2,3)) If the model DOES NOT have a primary_key_field, then the array will |
|
2128 | + * be a two dimensional array where each element is a group of columns and values that |
|
2129 | + * get deleted. Example: array( |
|
2130 | + * 0 => array( |
|
2131 | + * 'Term_Relationship.object_id' => 1 |
|
2132 | + * 'Term_Relationship.term_taxonomy_id' => 5 |
|
2133 | + * ), |
|
2134 | + * 1 => array( |
|
2135 | + * 'Term_Relationship.object_id' => 1 |
|
2136 | + * 'Term_Relationship.term_taxonomy_id' => 6 |
|
2137 | + * ) |
|
2138 | + * ) |
|
2139 | + * @throws EE_Error |
|
2140 | + * @throws ReflectionException |
|
2141 | + */ |
|
2142 | + protected function _get_ids_for_delete(array $row_results_for_deleting, $allow_blocking = true) |
|
2143 | + { |
|
2144 | + $ids_to_delete_indexed_by_column = []; |
|
2145 | + if ($this->has_primary_key_field()) { |
|
2146 | + $primary_table = $this->_get_main_table(); |
|
2147 | + $ids_to_delete_indexed_by_column = []; |
|
2148 | + foreach ($row_results_for_deleting as $item_to_delete) { |
|
2149 | + // before we mark this item for deletion, |
|
2150 | + // make sure there's no related entities blocking its deletion (if we're checking) |
|
2151 | + if ( |
|
2152 | + $allow_blocking |
|
2153 | + && $this->delete_is_blocked_by_related_models( |
|
2154 | + $item_to_delete[ $primary_table->get_fully_qualified_pk_column() ] |
|
2155 | + ) |
|
2156 | + ) { |
|
2157 | + continue; |
|
2158 | + } |
|
2159 | + // primary table deletes |
|
2160 | + if (isset($item_to_delete[ $primary_table->get_fully_qualified_pk_column() ])) { |
|
2161 | + $ids_to_delete_indexed_by_column[ $primary_table->get_fully_qualified_pk_column() ][] = |
|
2162 | + $item_to_delete[ $primary_table->get_fully_qualified_pk_column() ]; |
|
2163 | + } |
|
2164 | + } |
|
2165 | + } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
|
2166 | + $fields = $this->get_combined_primary_key_fields(); |
|
2167 | + foreach ($row_results_for_deleting as $item_to_delete) { |
|
2168 | + $ids_to_delete_indexed_by_column_for_row = []; |
|
2169 | + foreach ($fields as $cpk_field) { |
|
2170 | + if ($cpk_field instanceof EE_Model_Field_Base) { |
|
2171 | + $ids_to_delete_indexed_by_column_for_row[ $cpk_field->get_qualified_column() ] = |
|
2172 | + $item_to_delete[ $cpk_field->get_qualified_column() ]; |
|
2173 | + } |
|
2174 | + } |
|
2175 | + $ids_to_delete_indexed_by_column[] = $ids_to_delete_indexed_by_column_for_row; |
|
2176 | + } |
|
2177 | + } else { |
|
2178 | + // so there's no primary key and no combined key... |
|
2179 | + // sorry, can't help you |
|
2180 | + throw new EE_Error( |
|
2181 | + sprintf( |
|
2182 | + esc_html__( |
|
2183 | + "Cannot delete objects of type %s because there is no primary key NOR combined key", |
|
2184 | + "event_espresso" |
|
2185 | + ), |
|
2186 | + get_class($this) |
|
2187 | + ) |
|
2188 | + ); |
|
2189 | + } |
|
2190 | + return $ids_to_delete_indexed_by_column; |
|
2191 | + } |
|
2192 | + |
|
2193 | + |
|
2194 | + /** |
|
2195 | + * This receives an array of columns and values set to be deleted (as prepared by _get_ids_for_delete) and prepares |
|
2196 | + * the corresponding query_part for the query performing the delete. |
|
2197 | + * |
|
2198 | + * @param array $ids_to_delete_indexed_by_column @see _get_ids_for_delete for how this array might be shaped. |
|
2199 | + * @return string |
|
2200 | + * @throws EE_Error |
|
2201 | + */ |
|
2202 | + protected function _build_query_part_for_deleting_from_columns_and_values(array $ids_to_delete_indexed_by_column) |
|
2203 | + { |
|
2204 | + $query_part = ''; |
|
2205 | + if (empty($ids_to_delete_indexed_by_column)) { |
|
2206 | + return $query_part; |
|
2207 | + } elseif ($this->has_primary_key_field()) { |
|
2208 | + $query = []; |
|
2209 | + foreach ($ids_to_delete_indexed_by_column as $column => $ids) { |
|
2210 | + $query[] = $column . ' IN' . $this->_construct_in_value($ids, $this->_primary_key_field); |
|
2211 | + } |
|
2212 | + $query_part = ! empty($query) ? implode(' AND ', $query) : $query_part; |
|
2213 | + } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
|
2214 | + $ways_to_identify_a_row = []; |
|
2215 | + foreach ($ids_to_delete_indexed_by_column as $ids_to_delete_indexed_by_column_for_each_row) { |
|
2216 | + $values_for_each_combined_primary_key_for_a_row = []; |
|
2217 | + foreach ($ids_to_delete_indexed_by_column_for_each_row as $column => $id) { |
|
2218 | + $values_for_each_combined_primary_key_for_a_row[] = $column . '=' . $id; |
|
2219 | + } |
|
2220 | + $value_and_value_and_value = implode(' AND ', $values_for_each_combined_primary_key_for_a_row); |
|
2221 | + $ways_to_identify_a_row[] = "({$value_and_value_and_value})"; |
|
2222 | + } |
|
2223 | + $query_part = implode(' OR ', $ways_to_identify_a_row); |
|
2224 | + } |
|
2225 | + return $query_part; |
|
2226 | + } |
|
2227 | + |
|
2228 | + |
|
2229 | + /** |
|
2230 | + * Gets the model field by the fully qualified name |
|
2231 | + * |
|
2232 | + * @param string $qualified_column_name eg 'Event_CPT.post_name' or $field_obj->get_qualified_column() |
|
2233 | + * @return EE_Model_Field_Base |
|
2234 | + * @throws EE_Error |
|
2235 | + */ |
|
2236 | + public function get_field_by_column($qualified_column_name) |
|
2237 | + { |
|
2238 | + foreach ($this->field_settings(true) as $field_obj) { |
|
2239 | + if ($field_obj->get_qualified_column() === $qualified_column_name) { |
|
2240 | + return $field_obj; |
|
2241 | + } |
|
2242 | + } |
|
2243 | + throw new EE_Error( |
|
2244 | + sprintf( |
|
2245 | + esc_html__('Could not find a field on the model "%1$s" for qualified column "%2$s"', 'event_espresso'), |
|
2246 | + $this->get_this_model_name(), |
|
2247 | + $qualified_column_name |
|
2248 | + ) |
|
2249 | + ); |
|
2250 | + } |
|
2251 | + |
|
2252 | + |
|
2253 | + /** |
|
2254 | + * Count all the rows that match criteria the model query params. |
|
2255 | + * If $field_to_count isn't provided, the model's primary key is used. Otherwise, we count by field_to_count's |
|
2256 | + * column |
|
2257 | + * |
|
2258 | + * @param array $query_params |
|
2259 | + * @param string $field_to_count field on model to count by (not column name) |
|
2260 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
2261 | + * that by the setting $distinct to TRUE; |
|
2262 | + * @return int |
|
2263 | + * @throws EE_Error |
|
2264 | + * @throws ReflectionException |
|
2265 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2266 | + */ |
|
2267 | + public function count($query_params = [], $field_to_count = null, $distinct = false) |
|
2268 | + { |
|
2269 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
2270 | + if ($field_to_count) { |
|
2271 | + $field_obj = $this->field_settings_for($field_to_count); |
|
2272 | + $column_to_count = $field_obj->get_qualified_column(); |
|
2273 | + } elseif ($this->has_primary_key_field()) { |
|
2274 | + $pk_field_obj = $this->get_primary_key_field(); |
|
2275 | + $column_to_count = $pk_field_obj->get_qualified_column(); |
|
2276 | + } else { |
|
2277 | + // there's no primary key |
|
2278 | + // if we're counting distinct items, and there's no primary key, |
|
2279 | + // we need to list out the columns for distinction; |
|
2280 | + // otherwise we can just use star |
|
2281 | + if ($distinct) { |
|
2282 | + $columns_to_use = []; |
|
2283 | + foreach ($this->get_combined_primary_key_fields() as $field_obj) { |
|
2284 | + $columns_to_use[] = $field_obj->get_qualified_column(); |
|
2285 | + } |
|
2286 | + $column_to_count = implode(',', $columns_to_use); |
|
2287 | + } else { |
|
2288 | + $column_to_count = '*'; |
|
2289 | + } |
|
2290 | + } |
|
2291 | + $column_to_count = $distinct ? "DISTINCT " . $column_to_count : $column_to_count; |
|
2292 | + $SQL = |
|
2293 | + "SELECT COUNT(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
2294 | + return (int) $this->_do_wpdb_query('get_var', [$SQL]); |
|
2295 | + } |
|
2296 | + |
|
2297 | + |
|
2298 | + /** |
|
2299 | + * Sums up the value of the $field_to_sum (defaults to the primary key, which isn't terribly useful) |
|
2300 | + * |
|
2301 | + * @param array $query_params |
|
2302 | + * @param string $field_to_sum name of field (array key in $_fields array) |
|
2303 | + * @return float |
|
2304 | + * @throws EE_Error |
|
2305 | + * @throws ReflectionException |
|
2306 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2307 | + */ |
|
2308 | + public function sum($query_params, $field_to_sum = null) |
|
2309 | + { |
|
2310 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
2311 | + if ($field_to_sum) { |
|
2312 | + $field_obj = $this->field_settings_for($field_to_sum); |
|
2313 | + } else { |
|
2314 | + $field_obj = $this->get_primary_key_field(); |
|
2315 | + } |
|
2316 | + $column_to_count = $field_obj->get_qualified_column(); |
|
2317 | + $SQL = "SELECT SUM(" . $column_to_count . ")" |
|
2318 | + . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
2319 | + $return_value = $this->_do_wpdb_query('get_var', [$SQL]); |
|
2320 | + // $data_type = $field_obj->get_wpdb_data_type(); |
|
2321 | + // if ($data_type === '%d' || $data_type === '%s') { |
|
2322 | + // return (float) $return_value; |
|
2323 | + // } |
|
2324 | + // must be %f |
|
2325 | + return (float) $return_value; |
|
2326 | + } |
|
2327 | + |
|
2328 | + |
|
2329 | + /** |
|
2330 | + * Just calls the specified method on $wpdb with the given arguments |
|
2331 | + * Consolidates a little extra error handling code |
|
2332 | + * |
|
2333 | + * @param string $wpdb_method |
|
2334 | + * @param array $arguments_to_provide |
|
2335 | + * @return mixed |
|
2336 | + * @throws EE_Error |
|
2337 | + * @throws ReflectionException |
|
2338 | + * @global wpdb $wpdb |
|
2339 | + */ |
|
2340 | + protected function _do_wpdb_query($wpdb_method, $arguments_to_provide) |
|
2341 | + { |
|
2342 | + // if we're in maintenance mode level 2, DON'T run any queries |
|
2343 | + // because level 2 indicates the database needs updating and |
|
2344 | + // is probably out of sync with the code |
|
2345 | + if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
2346 | + throw new EE_Error( |
|
2347 | + esc_html__( |
|
2348 | + 'Event Espresso Level 2 Maintenance mode is active. That means EE can not run ANY database queries until the necessary migration scripts have run which will take EE out of maintenance mode level 2. Please inform support of this error.', |
|
2349 | + 'event_espresso' |
|
2350 | + ) |
|
2351 | + ); |
|
2352 | + } |
|
2353 | + global $wpdb; |
|
2354 | + if (! method_exists($wpdb, $wpdb_method)) { |
|
2355 | + throw new EE_Error( |
|
2356 | + sprintf( |
|
2357 | + esc_html__( |
|
2358 | + 'There is no method named "%s" on Wordpress\' $wpdb object', |
|
2359 | + 'event_espresso' |
|
2360 | + ), |
|
2361 | + $wpdb_method |
|
2362 | + ) |
|
2363 | + ); |
|
2364 | + } |
|
2365 | + $old_show_errors_value = $wpdb->show_errors; |
|
2366 | + if (WP_DEBUG) { |
|
2367 | + $wpdb->show_errors(false); |
|
2368 | + } |
|
2369 | + $result = $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
2370 | + $this->show_db_query_if_previously_requested($wpdb->last_query); |
|
2371 | + if (WP_DEBUG) { |
|
2372 | + $wpdb->show_errors($old_show_errors_value); |
|
2373 | + if (! empty($wpdb->last_error)) { |
|
2374 | + throw new EE_Error(sprintf(__('WPDB Error: "%s"', 'event_espresso'), $wpdb->last_error)); |
|
2375 | + } |
|
2376 | + if ($result === false) { |
|
2377 | + throw new EE_Error( |
|
2378 | + sprintf( |
|
2379 | + esc_html__( |
|
2380 | + 'WPDB Error occurred, but no error message was logged by wpdb! The wpdb method called was "%1$s" and the arguments were "%2$s"', |
|
2381 | + 'event_espresso' |
|
2382 | + ), |
|
2383 | + $wpdb_method, |
|
2384 | + var_export($arguments_to_provide, true) |
|
2385 | + ) |
|
2386 | + ); |
|
2387 | + } |
|
2388 | + } elseif ($result === false) { |
|
2389 | + EE_Error::add_error( |
|
2390 | + sprintf( |
|
2391 | + esc_html__( |
|
2392 | + 'A database error has occurred. Turn on WP_DEBUG for more information.||A database error occurred doing wpdb method "%1$s", with arguments "%2$s". The error was "%3$s"', |
|
2393 | + 'event_espresso' |
|
2394 | + ), |
|
2395 | + $wpdb_method, |
|
2396 | + var_export($arguments_to_provide, true), |
|
2397 | + $wpdb->last_error |
|
2398 | + ), |
|
2399 | + __FILE__, |
|
2400 | + __FUNCTION__, |
|
2401 | + __LINE__ |
|
2402 | + ); |
|
2403 | + } |
|
2404 | + return $result; |
|
2405 | + } |
|
2406 | + |
|
2407 | + |
|
2408 | + /** |
|
2409 | + * Attempts to run the indicated WPDB method with the provided arguments, |
|
2410 | + * and if there's an error tries to verify the DB is correct. Uses |
|
2411 | + * the static property EEM_Base::$_db_verification_level to determine whether |
|
2412 | + * we should try to fix the EE core db, the addons, or just give up |
|
2413 | + * |
|
2414 | + * @param string $wpdb_method |
|
2415 | + * @param array $arguments_to_provide |
|
2416 | + * @return mixed |
|
2417 | + * @throws EE_Error |
|
2418 | + * @throws ReflectionException |
|
2419 | + */ |
|
2420 | + private function _process_wpdb_query($wpdb_method, $arguments_to_provide) |
|
2421 | + { |
|
2422 | + global $wpdb; |
|
2423 | + $wpdb->last_error = null; |
|
2424 | + $result = call_user_func_array([$wpdb, $wpdb_method], $arguments_to_provide); |
|
2425 | + // was there an error running the query? but we don't care on new activations |
|
2426 | + // (we're going to setup the DB anyway on new activations) |
|
2427 | + if ( |
|
2428 | + ($result === false || ! empty($wpdb->last_error)) |
|
2429 | + && EE_System::instance()->detect_req_type() !== EE_System::req_type_new_activation |
|
2430 | + ) { |
|
2431 | + switch (EEM_Base::$_db_verification_level) { |
|
2432 | + case EEM_Base::db_verified_none: |
|
2433 | + // let's double-check core's DB |
|
2434 | + $error_message = $this->_verify_core_db($wpdb_method, $arguments_to_provide); |
|
2435 | + break; |
|
2436 | + case EEM_Base::db_verified_core: |
|
2437 | + // STILL NO LOVE?? verify all the addons too. Maybe they need to be fixed |
|
2438 | + $error_message = $this->_verify_addons_db($wpdb_method, $arguments_to_provide); |
|
2439 | + break; |
|
2440 | + case EEM_Base::db_verified_addons: |
|
2441 | + // ummmm... you in trouble |
|
2442 | + return $result; |
|
2443 | + } |
|
2444 | + if (! empty($error_message)) { |
|
2445 | + EE_Log::instance()->log(__FILE__, __FUNCTION__, $error_message, 'error'); |
|
2446 | + trigger_error($error_message); |
|
2447 | + } |
|
2448 | + return $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
2449 | + } |
|
2450 | + return $result; |
|
2451 | + } |
|
2452 | + |
|
2453 | + |
|
2454 | + /** |
|
2455 | + * Verifies the EE core database is up-to-date and records that we've done it on |
|
2456 | + * EEM_Base::$_db_verification_level |
|
2457 | + * |
|
2458 | + * @param string $wpdb_method |
|
2459 | + * @param array $arguments_to_provide |
|
2460 | + * @return string |
|
2461 | + * @throws EE_Error |
|
2462 | + * @throws ReflectionException |
|
2463 | + */ |
|
2464 | + private function _verify_core_db($wpdb_method, $arguments_to_provide) |
|
2465 | + { |
|
2466 | + global $wpdb; |
|
2467 | + // ok remember that we've already attempted fixing the core db, in case the problem persists |
|
2468 | + EEM_Base::$_db_verification_level = EEM_Base::db_verified_core; |
|
2469 | + $error_message = sprintf( |
|
2470 | + esc_html__( |
|
2471 | + 'WPDB Error "%1$s" while running wpdb method "%2$s" with arguments %3$s. Automatically attempting to fix EE Core DB', |
|
2472 | + 'event_espresso' |
|
2473 | + ), |
|
2474 | + $wpdb->last_error, |
|
2475 | + $wpdb_method, |
|
2476 | + wp_json_encode($arguments_to_provide) |
|
2477 | + ); |
|
2478 | + EE_System::instance()->initialize_db_if_no_migrations_required(); |
|
2479 | + return $error_message; |
|
2480 | + } |
|
2481 | + |
|
2482 | + |
|
2483 | + /** |
|
2484 | + * Verifies the EE addons' database is up-to-date and records that we've done it on |
|
2485 | + * EEM_Base::$_db_verification_level |
|
2486 | + * |
|
2487 | + * @param $wpdb_method |
|
2488 | + * @param $arguments_to_provide |
|
2489 | + * @return string |
|
2490 | + * @throws EE_Error |
|
2491 | + * @throws ReflectionException |
|
2492 | + */ |
|
2493 | + private function _verify_addons_db($wpdb_method, $arguments_to_provide) |
|
2494 | + { |
|
2495 | + global $wpdb; |
|
2496 | + // ok remember that we've already attempted fixing the addons dbs, in case the problem persists |
|
2497 | + EEM_Base::$_db_verification_level = EEM_Base::db_verified_addons; |
|
2498 | + $error_message = sprintf( |
|
2499 | + esc_html__( |
|
2500 | + 'WPDB AGAIN: Error "%1$s" while running the same method and arguments as before. Automatically attempting to fix EE Addons DB', |
|
2501 | + 'event_espresso' |
|
2502 | + ), |
|
2503 | + $wpdb->last_error, |
|
2504 | + $wpdb_method, |
|
2505 | + wp_json_encode($arguments_to_provide) |
|
2506 | + ); |
|
2507 | + EE_System::instance()->initialize_addons(); |
|
2508 | + return $error_message; |
|
2509 | + } |
|
2510 | + |
|
2511 | + |
|
2512 | + /** |
|
2513 | + * In order to avoid repeating this code for the get_all, sum, and count functions, put the code parts |
|
2514 | + * that are identical in here. Returns a string of SQL of everything in a SELECT query except the beginning |
|
2515 | + * SELECT clause, eg " FROM wp_posts AS Event INNER JOIN ... WHERE ... ORDER BY ... LIMIT ... GROUP BY ... HAVING |
|
2516 | + * ..." |
|
2517 | + * |
|
2518 | + * @param EE_Model_Query_Info_Carrier $model_query_info |
|
2519 | + * @return string |
|
2520 | + */ |
|
2521 | + private function _construct_2nd_half_of_select_query(EE_Model_Query_Info_Carrier $model_query_info) |
|
2522 | + { |
|
2523 | + return " FROM " . $model_query_info->get_full_join_sql() . |
|
2524 | + $model_query_info->get_where_sql() . |
|
2525 | + $model_query_info->get_group_by_sql() . |
|
2526 | + $model_query_info->get_having_sql() . |
|
2527 | + $model_query_info->get_order_by_sql() . |
|
2528 | + $model_query_info->get_limit_sql(); |
|
2529 | + } |
|
2530 | + |
|
2531 | + |
|
2532 | + /** |
|
2533 | + * Set to easily debug the next X queries ran from this model. |
|
2534 | + * |
|
2535 | + * @param int $count |
|
2536 | + */ |
|
2537 | + public function show_next_x_db_queries($count = 1) |
|
2538 | + { |
|
2539 | + $this->_show_next_x_db_queries = $count; |
|
2540 | + } |
|
2541 | + |
|
2542 | + |
|
2543 | + /** |
|
2544 | + * @param $sql_query |
|
2545 | + */ |
|
2546 | + public function show_db_query_if_previously_requested($sql_query) |
|
2547 | + { |
|
2548 | + if ($this->_show_next_x_db_queries > 0) { |
|
2549 | + echo $sql_query; |
|
2550 | + $this->_show_next_x_db_queries--; |
|
2551 | + } |
|
2552 | + } |
|
2553 | + |
|
2554 | + |
|
2555 | + /** |
|
2556 | + * Adds a relationship of the correct type between $modelObject and $otherModelObject. |
|
2557 | + * There are the 3 cases: |
|
2558 | + * 'belongsTo' relationship: sets $id_or_obj's foreign_key to be $other_model_id_or_obj's primary_key. If |
|
2559 | + * $otherModelObject has no ID, it is first saved. |
|
2560 | + * 'hasMany' relationship: sets $other_model_id_or_obj's foreign_key to be $id_or_obj's primary_key. If $id_or_obj |
|
2561 | + * has no ID, it is first saved. |
|
2562 | + * 'hasAndBelongsToMany' relationships: checks that there isn't already an entry in the join table, and adds one. |
|
2563 | + * If one of the model Objects has not yet been saved to the database, it is saved before adding the entry in the |
|
2564 | + * join table |
|
2565 | + * |
|
2566 | + * @param EE_Base_Class /int $thisModelObject |
|
2567 | + * @param EE_Base_Class /int $id_or_obj EE_base_Class or ID of other Model Object |
|
2568 | + * @param string $relationName , key in EEM_Base::_relations |
|
2569 | + * an attendee to a group, you also want to specify which role they |
|
2570 | + * will have in that group. So you would use this parameter to |
|
2571 | + * specify array('role-column-name'=>'role-id') |
|
2572 | + * @param array $extra_join_model_fields_n_values This allows you to enter further query params for the relation |
|
2573 | + * to for relation to methods that allow you to further specify |
|
2574 | + * extra columns to join by (such as HABTM). Keep in mind that the |
|
2575 | + * only acceptable query_params is strict "col" => "value" pairs |
|
2576 | + * because these will be inserted in any new rows created as well. |
|
2577 | + * @return EE_Base_Class which was added as a relation. Object referred to by $other_model_id_or_obj |
|
2578 | + * @throws EE_Error |
|
2579 | + */ |
|
2580 | + public function add_relationship_to( |
|
2581 | + $id_or_obj, |
|
2582 | + $other_model_id_or_obj, |
|
2583 | + $relationName, |
|
2584 | + $extra_join_model_fields_n_values = [] |
|
2585 | + ) { |
|
2586 | + $relation_obj = $this->related_settings_for($relationName); |
|
2587 | + return $relation_obj->add_relation_to($id_or_obj, $other_model_id_or_obj, $extra_join_model_fields_n_values); |
|
2588 | + } |
|
2589 | + |
|
2590 | + |
|
2591 | + /** |
|
2592 | + * Removes a relationship of the correct type between $modelObject and $otherModelObject. |
|
2593 | + * There are the 3 cases: |
|
2594 | + * 'belongsTo' relationship: sets $modelObject's foreign_key to null, if that field is nullable.Otherwise throws an |
|
2595 | + * error |
|
2596 | + * 'hasMany' relationship: sets $otherModelObject's foreign_key to null,if that field is nullable.Otherwise throws |
|
2597 | + * an error |
|
2598 | + * 'hasAndBelongsToMany' relationships:removes any existing entry in the join table between the two models. |
|
2599 | + * |
|
2600 | + * @param EE_Base_Class /int $id_or_obj |
|
2601 | + * @param EE_Base_Class /int $other_model_id_or_obj EE_Base_Class or ID of other Model Object |
|
2602 | + * @param string $relationName key in EEM_Base::_relations |
|
2603 | + * @param array $where_query This allows you to enter further query params for the relation to for relation to |
|
2604 | + * methods that allow you to further specify extra columns to join by (such as HABTM). |
|
2605 | + * Keep in mind that the only acceptable query_params is strict "col" => "value" pairs |
|
2606 | + * because these will be inserted in any new rows created as well. |
|
2607 | + * @return boolean of success |
|
2608 | + * @throws EE_Error |
|
2609 | + */ |
|
2610 | + public function remove_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query = []) |
|
2611 | + { |
|
2612 | + $relation_obj = $this->related_settings_for($relationName); |
|
2613 | + return $relation_obj->remove_relation_to($id_or_obj, $other_model_id_or_obj, $where_query); |
|
2614 | + } |
|
2615 | + |
|
2616 | + |
|
2617 | + /** |
|
2618 | + * @param mixed $id_or_obj |
|
2619 | + * @param string $relationName |
|
2620 | + * @param array $where_query_params |
|
2621 | + * @param EE_Base_Class[] objects to which relations were removed |
|
2622 | + * @return EE_Base_Class[] |
|
2623 | + * @throws EE_Error |
|
2624 | + */ |
|
2625 | + public function remove_relations($id_or_obj, $relationName, $where_query_params = []) |
|
2626 | + { |
|
2627 | + $relation_obj = $this->related_settings_for($relationName); |
|
2628 | + return $relation_obj->remove_relations($id_or_obj, $where_query_params); |
|
2629 | + } |
|
2630 | + |
|
2631 | + |
|
2632 | + /** |
|
2633 | + * Gets all the related items of the specified $model_name, using $query_params. |
|
2634 | + * Note: by default, we remove the "default query params" |
|
2635 | + * because we want to get even deleted items etc. |
|
2636 | + * |
|
2637 | + * @param mixed $id_or_obj EE_Base_Class child or its ID |
|
2638 | + * @param string $model_name like 'Event', 'Registration', etc. always singular |
|
2639 | + * @param array $query_params see github link below for more info |
|
2640 | + * @return EE_Base_Class[] |
|
2641 | + * @throws EE_Error |
|
2642 | + * @throws ReflectionException |
|
2643 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2644 | + */ |
|
2645 | + public function get_all_related($id_or_obj, $model_name, $query_params = null) |
|
2646 | + { |
|
2647 | + $model_obj = $this->ensure_is_obj($id_or_obj); |
|
2648 | + $relation_settings = $this->related_settings_for($model_name); |
|
2649 | + return $relation_settings->get_all_related($model_obj, $query_params); |
|
2650 | + } |
|
2651 | + |
|
2652 | + |
|
2653 | + /** |
|
2654 | + * Deletes all the model objects across the relation indicated by $model_name |
|
2655 | + * which are related to $id_or_obj which meet the criteria set in $query_params. |
|
2656 | + * However, if the model objects can't be deleted because of blocking related model objects, then |
|
2657 | + * they aren't deleted. (Unless the thing that would have been deleted can be soft-deleted, that still happens). |
|
2658 | + * |
|
2659 | + * @param EE_Base_Class|int|string $id_or_obj |
|
2660 | + * @param string $model_name |
|
2661 | + * @param array $query_params |
|
2662 | + * @return int how many deleted |
|
2663 | + * @throws EE_Error |
|
2664 | + * @throws ReflectionException |
|
2665 | + */ |
|
2666 | + public function delete_related($id_or_obj, $model_name, $query_params = []) |
|
2667 | + { |
|
2668 | + $model_obj = $this->ensure_is_obj($id_or_obj); |
|
2669 | + $relation_settings = $this->related_settings_for($model_name); |
|
2670 | + return $relation_settings->delete_all_related($model_obj, $query_params); |
|
2671 | + } |
|
2672 | + |
|
2673 | + |
|
2674 | + /** |
|
2675 | + * Hard deletes all the model objects across the relation indicated by $model_name |
|
2676 | + * which are related to $id_or_obj which meet the criteria set in $query_params. If |
|
2677 | + * the model objects can't be hard deleted because of blocking related model objects, |
|
2678 | + * just does a soft-delete on them instead. |
|
2679 | + * |
|
2680 | + * @param EE_Base_Class|int|string $id_or_obj |
|
2681 | + * @param string $model_name |
|
2682 | + * @param array $query_params |
|
2683 | + * @return int how many deleted |
|
2684 | + * @throws EE_Error |
|
2685 | + * @throws ReflectionException |
|
2686 | + */ |
|
2687 | + public function delete_related_permanently($id_or_obj, $model_name, $query_params = []) |
|
2688 | + { |
|
2689 | + $model_obj = $this->ensure_is_obj($id_or_obj); |
|
2690 | + $relation_settings = $this->related_settings_for($model_name); |
|
2691 | + return $relation_settings->delete_related_permanently($model_obj, $query_params); |
|
2692 | + } |
|
2693 | + |
|
2694 | + |
|
2695 | + /** |
|
2696 | + * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
2697 | + * unless otherwise specified in the $query_params |
|
2698 | + * |
|
2699 | + * @param int /EE_Base_Class $id_or_obj |
|
2700 | + * @param string $model_name like 'Event', or 'Registration' |
|
2701 | + * @param array $query_params |
|
2702 | + * @param string $field_to_count name of field to count by. By default, uses primary key |
|
2703 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
2704 | + * that by the setting $distinct to TRUE; |
|
2705 | + * @return int |
|
2706 | + * @throws EE_Error |
|
2707 | + * @throws ReflectionException |
|
2708 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2709 | + */ |
|
2710 | + public function count_related( |
|
2711 | + $id_or_obj, |
|
2712 | + $model_name, |
|
2713 | + $query_params = [], |
|
2714 | + $field_to_count = null, |
|
2715 | + $distinct = false |
|
2716 | + ) { |
|
2717 | + $related_model = $this->get_related_model_obj($model_name); |
|
2718 | + // we're just going to use the query params on the related model's normal get_all query, |
|
2719 | + // except add a condition to say to match the current mod |
|
2720 | + if (! isset($query_params['default_where_conditions'])) { |
|
2721 | + $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
2722 | + } |
|
2723 | + $this_model_name = $this->get_this_model_name(); |
|
2724 | + $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
2725 | + $query_params[0][ $this_model_name . "." . $this_pk_field_name ] = $id_or_obj; |
|
2726 | + return $related_model->count($query_params, $field_to_count, $distinct); |
|
2727 | + } |
|
2728 | + |
|
2729 | + |
|
2730 | + /** |
|
2731 | + * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
2732 | + * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
2733 | + * |
|
2734 | + * @param int /EE_Base_Class $id_or_obj |
|
2735 | + * @param string $model_name like 'Event', or 'Registration' |
|
2736 | + * @param array $query_params |
|
2737 | + * @param string $field_to_sum name of field to count by. By default, uses primary key |
|
2738 | + * @return float |
|
2739 | + * @throws EE_Error |
|
2740 | + * @throws ReflectionException |
|
2741 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2742 | + */ |
|
2743 | + public function sum_related($id_or_obj, $model_name, $query_params, $field_to_sum = null) |
|
2744 | + { |
|
2745 | + $related_model = $this->get_related_model_obj($model_name); |
|
2746 | + if (! is_array($query_params)) { |
|
2747 | + EE_Error::doing_it_wrong( |
|
2748 | + 'EEM_Base::sum_related', |
|
2749 | + sprintf( |
|
2750 | + esc_html__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
2751 | + gettype($query_params) |
|
2752 | + ), |
|
2753 | + '4.6.0' |
|
2754 | + ); |
|
2755 | + $query_params = []; |
|
2756 | + } |
|
2757 | + // we're just going to use the query params on the related model's normal get_all query, |
|
2758 | + // except add a condition to say to match the current mod |
|
2759 | + if (! isset($query_params['default_where_conditions'])) { |
|
2760 | + $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
2761 | + } |
|
2762 | + $this_model_name = $this->get_this_model_name(); |
|
2763 | + $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
2764 | + $query_params[0][ $this_model_name . "." . $this_pk_field_name ] = $id_or_obj; |
|
2765 | + return $related_model->sum($query_params, $field_to_sum); |
|
2766 | + } |
|
2767 | + |
|
2768 | + |
|
2769 | + /** |
|
2770 | + * Uses $this->_relatedModels info to find the first related model object of relation $relationName to the given |
|
2771 | + * $modelObject |
|
2772 | + * |
|
2773 | + * @param int | EE_Base_Class $id_or_obj EE_Base_Class child or its ID |
|
2774 | + * @param string $other_model_name , key in $this->_relatedModels, eg 'Registration', or 'Events' |
|
2775 | + * @param array $query_params see github link below for more info |
|
2776 | + * @return EE_Base_Class |
|
2777 | + * @throws EE_Error |
|
2778 | + * @throws ReflectionException |
|
2779 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
2780 | + */ |
|
2781 | + public function get_first_related(EE_Base_Class $id_or_obj, $other_model_name, $query_params) |
|
2782 | + { |
|
2783 | + $query_params['limit'] = 1; |
|
2784 | + $results = $this->get_all_related($id_or_obj, $other_model_name, $query_params); |
|
2785 | + if ($results) { |
|
2786 | + return array_shift($results); |
|
2787 | + } |
|
2788 | + return null; |
|
2789 | + } |
|
2790 | + |
|
2791 | + |
|
2792 | + /** |
|
2793 | + * Gets the model's name as it's expected in queries. For example, if this is EEM_Event model, that would be Event |
|
2794 | + * |
|
2795 | + * @return string |
|
2796 | + */ |
|
2797 | + public function get_this_model_name() |
|
2798 | + { |
|
2799 | + return str_replace("EEM_", "", get_class($this)); |
|
2800 | + } |
|
2801 | + |
|
2802 | + |
|
2803 | + /** |
|
2804 | + * Gets the model field on this model which is of type EE_Any_Foreign_Model_Name_Field |
|
2805 | + * |
|
2806 | + * @return EE_Any_Foreign_Model_Name_Field |
|
2807 | + * @throws EE_Error |
|
2808 | + */ |
|
2809 | + public function get_field_containing_related_model_name() |
|
2810 | + { |
|
2811 | + foreach ($this->field_settings(true) as $field) { |
|
2812 | + if ($field instanceof EE_Any_Foreign_Model_Name_Field) { |
|
2813 | + $field_with_model_name = $field; |
|
2814 | + } |
|
2815 | + } |
|
2816 | + if (! isset($field_with_model_name) || ! $field_with_model_name) { |
|
2817 | + throw new EE_Error( |
|
2818 | + sprintf( |
|
2819 | + esc_html__("There is no EE_Any_Foreign_Model_Name field on model %s", "event_espresso"), |
|
2820 | + $this->get_this_model_name() |
|
2821 | + ) |
|
2822 | + ); |
|
2823 | + } |
|
2824 | + return $field_with_model_name; |
|
2825 | + } |
|
2826 | + |
|
2827 | + |
|
2828 | + /** |
|
2829 | + * Inserts a new entry into the database, for each table. |
|
2830 | + * Note: does not add the item to the entity map because that is done by EE_Base_Class::save() right after this. |
|
2831 | + * If client code uses EEM_Base::insert() directly, then although the item isn't in the entity map, |
|
2832 | + * we also know there is no model object with the newly inserted item's ID at the moment (because |
|
2833 | + * if there were, then they would already be in the DB and this would fail); and in the future if someone |
|
2834 | + * creates a model object with this ID (or grabs it from the DB) then it will be added to the |
|
2835 | + * entity map at that time anyways. SO, no need for EEM_Base::insert ot add to the entity map |
|
2836 | + * |
|
2837 | + * @param array $field_n_values keys are field names, values are their values (in the client code's domain if |
|
2838 | + * $values_already_prepared_by_model_object is false, in the model object's domain if |
|
2839 | + * $values_already_prepared_by_model_object is true. See comment about this at the top |
|
2840 | + * of EEM_Base) |
|
2841 | + * @return int|string new primary key on main table that got inserted |
|
2842 | + * @throws EE_Error |
|
2843 | + * @throws ReflectionException |
|
2844 | + */ |
|
2845 | + public function insert($field_n_values) |
|
2846 | + { |
|
2847 | + /** |
|
2848 | + * Filters the fields and their values before inserting an item using the models |
|
2849 | + * |
|
2850 | + * @param array $fields_n_values keys are the fields and values are their new values |
|
2851 | + * @param EEM_Base $model the model used |
|
2852 | + */ |
|
2853 | + $field_n_values = (array) apply_filters('FHEE__EEM_Base__insert__fields_n_values', $field_n_values, $this); |
|
2854 | + if ($this->_satisfies_unique_indexes($field_n_values)) { |
|
2855 | + $main_table = $this->_get_main_table(); |
|
2856 | + $new_id = $this->_insert_into_specific_table($main_table, $field_n_values, false); |
|
2857 | + if ($new_id !== false) { |
|
2858 | + foreach ($this->_get_other_tables() as $other_table) { |
|
2859 | + $this->_insert_into_specific_table($other_table, $field_n_values, $new_id); |
|
2860 | + } |
|
2861 | + } |
|
2862 | + /** |
|
2863 | + * Done just after attempting to insert a new model object |
|
2864 | + * |
|
2865 | + * @param EEM_Base $model used |
|
2866 | + * @param array $fields_n_values fields and their values |
|
2867 | + * @param int|string the ID of the newly-inserted model object |
|
2868 | + */ |
|
2869 | + do_action('AHEE__EEM_Base__insert__end', $this, $field_n_values, $new_id); |
|
2870 | + return $new_id; |
|
2871 | + } |
|
2872 | + return false; |
|
2873 | + } |
|
2874 | + |
|
2875 | + |
|
2876 | + /** |
|
2877 | + * Checks that the result would satisfy the unique indexes on this model |
|
2878 | + * |
|
2879 | + * @param array $field_n_values |
|
2880 | + * @param string $action |
|
2881 | + * @return boolean |
|
2882 | + * @throws EE_Error |
|
2883 | + * @throws ReflectionException |
|
2884 | + */ |
|
2885 | + protected function _satisfies_unique_indexes($field_n_values, $action = 'insert') |
|
2886 | + { |
|
2887 | + foreach ($this->unique_indexes() as $index_name => $index) { |
|
2888 | + $uniqueness_where_params = array_intersect_key($field_n_values, $index->fields()); |
|
2889 | + if ($this->exists([$uniqueness_where_params])) { |
|
2890 | + EE_Error::add_error( |
|
2891 | + sprintf( |
|
2892 | + esc_html__( |
|
2893 | + "Could not %s %s. %s uniqueness index failed. Fields %s must form a unique set, but an entry already exists with values %s.", |
|
2894 | + "event_espresso" |
|
2895 | + ), |
|
2896 | + $action, |
|
2897 | + $this->_get_class_name(), |
|
2898 | + $index_name, |
|
2899 | + implode(",", $index->field_names()), |
|
2900 | + http_build_query($uniqueness_where_params) |
|
2901 | + ), |
|
2902 | + __FILE__, |
|
2903 | + __FUNCTION__, |
|
2904 | + __LINE__ |
|
2905 | + ); |
|
2906 | + return false; |
|
2907 | + } |
|
2908 | + } |
|
2909 | + return true; |
|
2910 | + } |
|
2911 | + |
|
2912 | + |
|
2913 | + /** |
|
2914 | + * Checks the database for an item that conflicts (ie, if this item were |
|
2915 | + * saved to the DB would break some uniqueness requirement, like a primary key |
|
2916 | + * or an index primary key set) with the item specified. $id_obj_or_fields_array |
|
2917 | + * can be either an EE_Base_Class or an array of fields n values |
|
2918 | + * |
|
2919 | + * @param EE_Base_Class|array $obj_or_fields_array |
|
2920 | + * @param boolean $include_primary_key whether to use the model object's primary key |
|
2921 | + * when looking for conflicts |
|
2922 | + * (ie, if false, we ignore the model object's primary key |
|
2923 | + * when finding "conflicts". If true, it's also considered). |
|
2924 | + * Only works for INT primary key, |
|
2925 | + * STRING primary keys cannot be ignored |
|
2926 | + * @return EE_Base_Class|array |
|
2927 | + * @throws EE_Error |
|
2928 | + * @throws ReflectionException |
|
2929 | + */ |
|
2930 | + public function get_one_conflicting($obj_or_fields_array, $include_primary_key = true) |
|
2931 | + { |
|
2932 | + if ($obj_or_fields_array instanceof EE_Base_Class) { |
|
2933 | + $fields_n_values = $obj_or_fields_array->model_field_array(); |
|
2934 | + } elseif (is_array($obj_or_fields_array)) { |
|
2935 | + $fields_n_values = $obj_or_fields_array; |
|
2936 | + } else { |
|
2937 | + throw new EE_Error( |
|
2938 | + sprintf( |
|
2939 | + esc_html__( |
|
2940 | + "%s get_all_conflicting should be called with a model object or an array of field names and values, you provided %d", |
|
2941 | + "event_espresso" |
|
2942 | + ), |
|
2943 | + get_class($this), |
|
2944 | + $obj_or_fields_array |
|
2945 | + ) |
|
2946 | + ); |
|
2947 | + } |
|
2948 | + $query_params = []; |
|
2949 | + if ( |
|
2950 | + $this->has_primary_key_field() |
|
2951 | + && ($include_primary_key |
|
2952 | + || $this->get_primary_key_field() |
|
2953 | + instanceof |
|
2954 | + EE_Primary_Key_String_Field) |
|
2955 | + && isset($fields_n_values[ $this->primary_key_name() ]) |
|
2956 | + ) { |
|
2957 | + $query_params[0]['OR'][ $this->primary_key_name() ] = $fields_n_values[ $this->primary_key_name() ]; |
|
2958 | + } |
|
2959 | + foreach ($this->unique_indexes() as $unique_index_name => $unique_index) { |
|
2960 | + $uniqueness_where_params = |
|
2961 | + array_intersect_key($fields_n_values, $unique_index->fields()); |
|
2962 | + $query_params[0]['OR'][ 'AND*' . $unique_index_name ] = $uniqueness_where_params; |
|
2963 | + } |
|
2964 | + // if there is nothing to base this search on, then we shouldn't find anything |
|
2965 | + if (empty($query_params)) { |
|
2966 | + return []; |
|
2967 | + } |
|
2968 | + return $this->get_one($query_params); |
|
2969 | + } |
|
2970 | + |
|
2971 | + |
|
2972 | + /** |
|
2973 | + * Like count, but is optimized and returns a boolean instead of an int |
|
2974 | + * |
|
2975 | + * @param array $query_params |
|
2976 | + * @return boolean |
|
2977 | + * @throws EE_Error |
|
2978 | + * @throws ReflectionException |
|
2979 | + */ |
|
2980 | + public function exists($query_params) |
|
2981 | + { |
|
2982 | + $query_params['limit'] = 1; |
|
2983 | + return $this->count($query_params) > 0; |
|
2984 | + } |
|
2985 | + |
|
2986 | + |
|
2987 | + /** |
|
2988 | + * Wrapper for exists, except ignores default query parameters so we're only considering ID |
|
2989 | + * |
|
2990 | + * @param int|string $id |
|
2991 | + * @return boolean |
|
2992 | + * @throws EE_Error |
|
2993 | + * @throws ReflectionException |
|
2994 | + */ |
|
2995 | + public function exists_by_ID($id) |
|
2996 | + { |
|
2997 | + return $this->exists( |
|
2998 | + [ |
|
2999 | + 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
3000 | + [ |
|
3001 | + $this->primary_key_name() => $id, |
|
3002 | + ], |
|
3003 | + ] |
|
3004 | + ); |
|
3005 | + } |
|
3006 | + |
|
3007 | + |
|
3008 | + /** |
|
3009 | + * Inserts a new row in $table, using the $cols_n_values which apply to that table. |
|
3010 | + * If a $new_id is supplied and if $table is an EE_Other_Table, we assume |
|
3011 | + * we need to add a foreign key column to point to $new_id (which should be the primary key's value |
|
3012 | + * on the main table) |
|
3013 | + * This is protected rather than private because private is not accessible to any child methods and there MAY be |
|
3014 | + * cases where we want to call it directly rather than via insert(). |
|
3015 | + * |
|
3016 | + * @access protected |
|
3017 | + * @param EE_Table_Base $table |
|
3018 | + * @param array $fields_n_values each key should be in field's keys, and value should be an int, string or |
|
3019 | + * float |
|
3020 | + * @param int $new_id for now we assume only int keys |
|
3021 | + * @return int ID of new row inserted, or FALSE on failure |
|
3022 | + * @throws EE_Error |
|
3023 | + * @throws ReflectionException |
|
3024 | + * @global WPDB $wpdb only used to get the $wpdb->insert_id after performing an insert |
|
3025 | + */ |
|
3026 | + protected function _insert_into_specific_table(EE_Table_Base $table, $fields_n_values, $new_id = 0) |
|
3027 | + { |
|
3028 | + global $wpdb; |
|
3029 | + $insertion_col_n_values = []; |
|
3030 | + $format_for_insertion = []; |
|
3031 | + $fields_on_table = $this->_get_fields_for_table($table->get_table_alias()); |
|
3032 | + foreach ($fields_on_table as $field_obj) { |
|
3033 | + // check if its an auto-incrementing column, in which case we should just leave it to do its autoincrement thing |
|
3034 | + if ($field_obj->is_auto_increment()) { |
|
3035 | + continue; |
|
3036 | + } |
|
3037 | + $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
3038 | + // if the value we want to assign it to is NULL, just don't mention it for the insertion |
|
3039 | + if ($prepared_value !== null) { |
|
3040 | + $insertion_col_n_values[ $field_obj->get_table_column() ] = $prepared_value; |
|
3041 | + $format_for_insertion[] = $field_obj->get_wpdb_data_type(); |
|
3042 | + } |
|
3043 | + } |
|
3044 | + if ($table instanceof EE_Secondary_Table && $new_id) { |
|
3045 | + // its not the main table, so we should have already saved the main table's PK which we just inserted |
|
3046 | + // so add the fk to the main table as a column |
|
3047 | + $insertion_col_n_values[ $table->get_fk_on_table() ] = $new_id; |
|
3048 | + $format_for_insertion[] = |
|
3049 | + '%d';// yes right now we're only allowing these foreign keys to be INTs |
|
3050 | + } |
|
3051 | + // insert the new entry |
|
3052 | + $result = $this->_do_wpdb_query( |
|
3053 | + 'insert', |
|
3054 | + [$table->get_table_name(), $insertion_col_n_values, $format_for_insertion] |
|
3055 | + ); |
|
3056 | + if ($result === false) { |
|
3057 | + return false; |
|
3058 | + } |
|
3059 | + // ok, now what do we return for the ID of the newly-inserted thing? |
|
3060 | + if ($this->has_primary_key_field()) { |
|
3061 | + if ($this->get_primary_key_field()->is_auto_increment()) { |
|
3062 | + return $wpdb->insert_id; |
|
3063 | + } |
|
3064 | + // it's not an auto-increment primary key, so |
|
3065 | + // it must have been supplied |
|
3066 | + return $fields_n_values[ $this->get_primary_key_field()->get_name() ]; |
|
3067 | + } |
|
3068 | + // we can't return a primary key because there is none. instead return |
|
3069 | + // a unique string indicating this model |
|
3070 | + return $this->get_index_primary_key_string($fields_n_values); |
|
3071 | + } |
|
3072 | + |
|
3073 | + |
|
3074 | + /** |
|
3075 | + * Prepare the $field_obj 's value in $fields_n_values for use in the database. |
|
3076 | + * If the field doesn't allow NULL, try to use its default. (If it doesn't allow NULL, |
|
3077 | + * and there is no default, we pass it along. WPDB will take care of it) |
|
3078 | + * |
|
3079 | + * @param EE_Model_Field_Base $field_obj |
|
3080 | + * @param array $fields_n_values |
|
3081 | + * @return mixed string|int|float depending on what the table column will be expecting |
|
3082 | + * @throws EE_Error |
|
3083 | + */ |
|
3084 | + protected function _prepare_value_or_use_default($field_obj, $fields_n_values) |
|
3085 | + { |
|
3086 | + // if this field doesn't allow nullable, don't allow it |
|
3087 | + if ( |
|
3088 | + ! $field_obj->is_nullable() |
|
3089 | + && ( |
|
3090 | + ! isset($fields_n_values[ $field_obj->get_name() ]) |
|
3091 | + || $fields_n_values[ $field_obj->get_name() ] === null |
|
3092 | + ) |
|
3093 | + ) { |
|
3094 | + $fields_n_values[ $field_obj->get_name() ] = $field_obj->get_default_value(); |
|
3095 | + } |
|
3096 | + $unprepared_value = isset($fields_n_values[ $field_obj->get_name() ]) |
|
3097 | + ? $fields_n_values[ $field_obj->get_name() ] |
|
3098 | + : null; |
|
3099 | + return $this->_prepare_value_for_use_in_db($unprepared_value, $field_obj); |
|
3100 | + } |
|
3101 | + |
|
3102 | + |
|
3103 | + /** |
|
3104 | + * Consolidates code for preparing a value supplied to the model for use int eh db. Calls the field's |
|
3105 | + * prepare_for_use_in_db method on the value, and depending on $value_already_prepare_by_model_obj, may also call |
|
3106 | + * the field's prepare_for_set() method. |
|
3107 | + * |
|
3108 | + * @param mixed $value value in the client code domain if $value_already_prepared_by_model_object is |
|
3109 | + * false, otherwise a value in the model object's domain (see lengthy comment at |
|
3110 | + * top of file) |
|
3111 | + * @param EE_Model_Field_Base $field field which will be doing the preparing of the value. If null, we assume |
|
3112 | + * $value is a custom selection |
|
3113 | + * @return mixed a value ready for use in the database for insertions, updating, or in a where clause |
|
3114 | + */ |
|
3115 | + private function _prepare_value_for_use_in_db($value, $field) |
|
3116 | + { |
|
3117 | + if ($field && $field instanceof EE_Model_Field_Base) { |
|
3118 | + // phpcs:disable PSR2.ControlStructures.SwitchDeclaration.TerminatingComment |
|
3119 | + switch ($this->_values_already_prepared_by_model_object) { |
|
3120 | + /** @noinspection PhpMissingBreakStatementInspection */ |
|
3121 | + case self::not_prepared_by_model_object: |
|
3122 | + $value = $field->prepare_for_set($value); |
|
3123 | + // purposefully left out "return" |
|
3124 | + case self::prepared_by_model_object: |
|
3125 | + /** @noinspection SuspiciousAssignmentsInspection */ |
|
3126 | + $value = $field->prepare_for_use_in_db($value); |
|
3127 | + case self::prepared_for_use_in_db: |
|
3128 | + // leave the value alone |
|
3129 | + } |
|
3130 | + return $value; |
|
3131 | + // phpcs:enable |
|
3132 | + } |
|
3133 | + return $value; |
|
3134 | + } |
|
3135 | + |
|
3136 | + |
|
3137 | + /** |
|
3138 | + * Returns the main table on this model |
|
3139 | + * |
|
3140 | + * @return EE_Primary_Table |
|
3141 | + * @throws EE_Error |
|
3142 | + */ |
|
3143 | + protected function _get_main_table() |
|
3144 | + { |
|
3145 | + foreach ($this->_tables as $table) { |
|
3146 | + if ($table instanceof EE_Primary_Table) { |
|
3147 | + return $table; |
|
3148 | + } |
|
3149 | + } |
|
3150 | + throw new EE_Error( |
|
3151 | + sprintf( |
|
3152 | + esc_html__( |
|
3153 | + 'There are no main tables on %s. They should be added to _tables array in the constructor', |
|
3154 | + 'event_espresso' |
|
3155 | + ), |
|
3156 | + get_class($this) |
|
3157 | + ) |
|
3158 | + ); |
|
3159 | + } |
|
3160 | + |
|
3161 | + |
|
3162 | + /** |
|
3163 | + * table |
|
3164 | + * returns EE_Primary_Table table name |
|
3165 | + * |
|
3166 | + * @return string |
|
3167 | + * @throws EE_Error |
|
3168 | + */ |
|
3169 | + public function table() |
|
3170 | + { |
|
3171 | + return $this->_get_main_table()->get_table_name(); |
|
3172 | + } |
|
3173 | + |
|
3174 | + |
|
3175 | + /** |
|
3176 | + * table |
|
3177 | + * returns first EE_Secondary_Table table name |
|
3178 | + * |
|
3179 | + * @return string |
|
3180 | + */ |
|
3181 | + public function second_table() |
|
3182 | + { |
|
3183 | + // grab second table from tables array |
|
3184 | + $second_table = end($this->_tables); |
|
3185 | + return $second_table instanceof EE_Secondary_Table ? $second_table->get_table_name() : null; |
|
3186 | + } |
|
3187 | + |
|
3188 | + |
|
3189 | + /** |
|
3190 | + * get_table_obj_by_alias |
|
3191 | + * returns table name given it's alias |
|
3192 | + * |
|
3193 | + * @param string $table_alias |
|
3194 | + * @return EE_Primary_Table | EE_Secondary_Table |
|
3195 | + */ |
|
3196 | + public function get_table_obj_by_alias($table_alias = '') |
|
3197 | + { |
|
3198 | + return isset($this->_tables[ $table_alias ]) ? $this->_tables[ $table_alias ] : null; |
|
3199 | + } |
|
3200 | + |
|
3201 | + |
|
3202 | + /** |
|
3203 | + * Gets all the tables of type EE_Other_Table from EEM_CPT_Basel_Model::_tables |
|
3204 | + * |
|
3205 | + * @return EE_Secondary_Table[] |
|
3206 | + */ |
|
3207 | + protected function _get_other_tables() |
|
3208 | + { |
|
3209 | + $other_tables = []; |
|
3210 | + foreach ($this->_tables as $table_alias => $table) { |
|
3211 | + if ($table instanceof EE_Secondary_Table) { |
|
3212 | + $other_tables[ $table_alias ] = $table; |
|
3213 | + } |
|
3214 | + } |
|
3215 | + return $other_tables; |
|
3216 | + } |
|
3217 | + |
|
3218 | + |
|
3219 | + /** |
|
3220 | + * Finds all the fields that correspond to the given table |
|
3221 | + * |
|
3222 | + * @param string $table_alias , array key in EEM_Base::_tables |
|
3223 | + * @return EE_Model_Field_Base[] |
|
3224 | + */ |
|
3225 | + public function _get_fields_for_table($table_alias) |
|
3226 | + { |
|
3227 | + return $this->_fields[ $table_alias ]; |
|
3228 | + } |
|
3229 | + |
|
3230 | + |
|
3231 | + /** |
|
3232 | + * Recurses through all the where parameters, and finds all the related models we'll need |
|
3233 | + * to complete this query. Eg, given where parameters like array('EVT_ID'=>3) from within Event model, we won't |
|
3234 | + * need any related models. But if the array were array('Registrations.REG_ID'=>3), we'd need the related |
|
3235 | + * Registration model. If it were array('Registrations.Transactions.Payments.PAY_ID'=>3), then we'd need the |
|
3236 | + * related Registration, Transaction, and Payment models. |
|
3237 | + * |
|
3238 | + * @param array $query_params see github link below for more info |
|
3239 | + * @return EE_Model_Query_Info_Carrier |
|
3240 | + * @throws EE_Error |
|
3241 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
3242 | + */ |
|
3243 | + public function _extract_related_models_from_query($query_params) |
|
3244 | + { |
|
3245 | + $query_info_carrier = new EE_Model_Query_Info_Carrier(); |
|
3246 | + if (array_key_exists(0, $query_params)) { |
|
3247 | + $this->_extract_related_models_from_sub_params_array_keys($query_params[0], $query_info_carrier, 0); |
|
3248 | + } |
|
3249 | + if (array_key_exists('group_by', $query_params)) { |
|
3250 | + if (is_array($query_params['group_by'])) { |
|
3251 | + $this->_extract_related_models_from_sub_params_array_values( |
|
3252 | + $query_params['group_by'], |
|
3253 | + $query_info_carrier, |
|
3254 | + 'group_by' |
|
3255 | + ); |
|
3256 | + } elseif (! empty($query_params['group_by'])) { |
|
3257 | + $this->_extract_related_model_info_from_query_param( |
|
3258 | + $query_params['group_by'], |
|
3259 | + $query_info_carrier, |
|
3260 | + 'group_by' |
|
3261 | + ); |
|
3262 | + } |
|
3263 | + } |
|
3264 | + if (array_key_exists('having', $query_params)) { |
|
3265 | + $this->_extract_related_models_from_sub_params_array_keys( |
|
3266 | + $query_params[0], |
|
3267 | + $query_info_carrier, |
|
3268 | + 'having' |
|
3269 | + ); |
|
3270 | + } |
|
3271 | + if (array_key_exists('order_by', $query_params)) { |
|
3272 | + if (is_array($query_params['order_by'])) { |
|
3273 | + $this->_extract_related_models_from_sub_params_array_keys( |
|
3274 | + $query_params['order_by'], |
|
3275 | + $query_info_carrier, |
|
3276 | + 'order_by' |
|
3277 | + ); |
|
3278 | + } elseif (! empty($query_params['order_by'])) { |
|
3279 | + $this->_extract_related_model_info_from_query_param( |
|
3280 | + $query_params['order_by'], |
|
3281 | + $query_info_carrier, |
|
3282 | + 'order_by' |
|
3283 | + ); |
|
3284 | + } |
|
3285 | + } |
|
3286 | + if (array_key_exists('force_join', $query_params)) { |
|
3287 | + $this->_extract_related_models_from_sub_params_array_values( |
|
3288 | + $query_params['force_join'], |
|
3289 | + $query_info_carrier, |
|
3290 | + 'force_join' |
|
3291 | + ); |
|
3292 | + } |
|
3293 | + $this->extractRelatedModelsFromCustomSelects($query_info_carrier); |
|
3294 | + return $query_info_carrier; |
|
3295 | + } |
|
3296 | + |
|
3297 | + |
|
3298 | + /** |
|
3299 | + * For extracting related models from WHERE (0), HAVING (having), ORDER BY (order_by) or forced joins (force_join) |
|
3300 | + * |
|
3301 | + * @param array $sub_query_params |
|
3302 | + * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
3303 | + * @param string $query_param_type one of $this->_allowed_query_params |
|
3304 | + * @return EE_Model_Query_Info_Carrier |
|
3305 | + * @throws EE_Error |
|
3306 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#-0-where-conditions |
|
3307 | + */ |
|
3308 | + private function _extract_related_models_from_sub_params_array_keys( |
|
3309 | + $sub_query_params, |
|
3310 | + EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
3311 | + $query_param_type |
|
3312 | + ) { |
|
3313 | + if (! empty($sub_query_params)) { |
|
3314 | + $sub_query_params = (array) $sub_query_params; |
|
3315 | + foreach ($sub_query_params as $param => $possibly_array_of_params) { |
|
3316 | + // $param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
3317 | + $this->_extract_related_model_info_from_query_param( |
|
3318 | + $param, |
|
3319 | + $model_query_info_carrier, |
|
3320 | + $query_param_type |
|
3321 | + ); |
|
3322 | + // if $possibly_array_of_params is an array, try recursing into it, searching for keys which |
|
3323 | + // indicate needed joins. Eg, array('NOT'=>array('Registration.TXN_ID'=>23)). In this case, we tried |
|
3324 | + // extracting models out of the 'NOT', which obviously wasn't successful, and then we recurse into the value |
|
3325 | + // of array('Registration.TXN_ID'=>23) |
|
3326 | + $query_param_sans_stars = |
|
3327 | + $this->_remove_stars_and_anything_after_from_condition_query_param_key($param); |
|
3328 | + if (in_array($query_param_sans_stars, $this->_logic_query_param_keys, true)) { |
|
3329 | + if (! is_array($possibly_array_of_params)) { |
|
3330 | + throw new EE_Error( |
|
3331 | + sprintf( |
|
3332 | + esc_html__( |
|
3333 | + "You used a special where query param %s, but the value isn't an array of where query params, it's just %s'. It should be an array, eg array('EVT_ID'=>23,'OR'=>array('Venue.VNU_ID'=>32,'Venue.VNU_name'=>'monkey_land'))", |
|
3334 | + "event_espresso" |
|
3335 | + ), |
|
3336 | + $param, |
|
3337 | + $possibly_array_of_params |
|
3338 | + ) |
|
3339 | + ); |
|
3340 | + } |
|
3341 | + $this->_extract_related_models_from_sub_params_array_keys( |
|
3342 | + $possibly_array_of_params, |
|
3343 | + $model_query_info_carrier, |
|
3344 | + $query_param_type |
|
3345 | + ); |
|
3346 | + } elseif ( |
|
3347 | + $query_param_type === 0 // ie WHERE |
|
3348 | + && is_array($possibly_array_of_params) |
|
3349 | + && isset($possibly_array_of_params[2]) |
|
3350 | + && $possibly_array_of_params[2] == true |
|
3351 | + ) { |
|
3352 | + // then $possible_array_of_params looks something like array('<','DTT_sold',true) |
|
3353 | + // indicating that $possible_array_of_params[1] is actually a field name, |
|
3354 | + // from which we should extract query parameters! |
|
3355 | + if (! isset($possibly_array_of_params[0], $possibly_array_of_params[1])) { |
|
3356 | + throw new EE_Error( |
|
3357 | + sprintf( |
|
3358 | + esc_html__( |
|
3359 | + "Improperly formed query parameter %s. It should be numerically indexed like array('<','DTT_sold',true); but you provided %s", |
|
3360 | + "event_espresso" |
|
3361 | + ), |
|
3362 | + $query_param_type, |
|
3363 | + implode(",", $possibly_array_of_params) |
|
3364 | + ) |
|
3365 | + ); |
|
3366 | + } |
|
3367 | + $this->_extract_related_model_info_from_query_param( |
|
3368 | + $possibly_array_of_params[1], |
|
3369 | + $model_query_info_carrier, |
|
3370 | + $query_param_type |
|
3371 | + ); |
|
3372 | + } |
|
3373 | + } |
|
3374 | + } |
|
3375 | + } |
|
3376 | + |
|
3377 | + |
|
3378 | + /** |
|
3379 | + * For extracting related models from forced_joins, where the array values contain the info about what |
|
3380 | + * models to join with. Eg an array like array('Attendee','Price.Price_Type'); |
|
3381 | + * |
|
3382 | + * @param array $sub_query_params |
|
3383 | + * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
3384 | + * @param string $query_param_type one of $this->_allowed_query_params |
|
3385 | + * @return EE_Model_Query_Info_Carrier |
|
3386 | + * @throws EE_Error |
|
3387 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
3388 | + */ |
|
3389 | + private function _extract_related_models_from_sub_params_array_values( |
|
3390 | + $sub_query_params, |
|
3391 | + EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
3392 | + $query_param_type |
|
3393 | + ) { |
|
3394 | + if (! empty($sub_query_params)) { |
|
3395 | + if (! is_array($sub_query_params)) { |
|
3396 | + throw new EE_Error( |
|
3397 | + sprintf( |
|
3398 | + esc_html__("Query parameter %s should be an array, but it isn't.", "event_espresso"), |
|
3399 | + $sub_query_params |
|
3400 | + ) |
|
3401 | + ); |
|
3402 | + } |
|
3403 | + foreach ($sub_query_params as $param) { |
|
3404 | + // $param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
3405 | + $this->_extract_related_model_info_from_query_param( |
|
3406 | + $param, |
|
3407 | + $model_query_info_carrier, |
|
3408 | + $query_param_type |
|
3409 | + ); |
|
3410 | + } |
|
3411 | + } |
|
3412 | + } |
|
3413 | + |
|
3414 | + |
|
3415 | + /** |
|
3416 | + * Extract all the query parts from model query params |
|
3417 | + * and put into a EEM_Related_Model_Info_Carrier for easy extraction into a query. We create this object |
|
3418 | + * instead of directly constructing the SQL because often we need to extract info from the $query_params |
|
3419 | + * but use them in a different order. Eg, we need to know what models we are querying |
|
3420 | + * before we know what joins to perform. However, we need to know what data types correspond to which fields on |
|
3421 | + * other models before we can finalize the where clause SQL. |
|
3422 | + * |
|
3423 | + * @param array $query_params see github link below for more info |
|
3424 | + * @return EE_Model_Query_Info_Carrier |
|
3425 | + * @throws EE_Error |
|
3426 | + * @throws ModelConfigurationException |
|
3427 | + * @throws ReflectionException |
|
3428 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
3429 | + */ |
|
3430 | + public function _create_model_query_info_carrier($query_params) |
|
3431 | + { |
|
3432 | + if (! is_array($query_params)) { |
|
3433 | + EE_Error::doing_it_wrong( |
|
3434 | + 'EEM_Base::_create_model_query_info_carrier', |
|
3435 | + sprintf( |
|
3436 | + esc_html__( |
|
3437 | + '$query_params should be an array, you passed a variable of type %s', |
|
3438 | + 'event_espresso' |
|
3439 | + ), |
|
3440 | + gettype($query_params) |
|
3441 | + ), |
|
3442 | + '4.6.0' |
|
3443 | + ); |
|
3444 | + $query_params = []; |
|
3445 | + } |
|
3446 | + $query_params[0] = isset($query_params[0]) ? $query_params[0] : []; |
|
3447 | + // first check if we should alter the query to account for caps or not |
|
3448 | + // because the caps might require us to do extra joins |
|
3449 | + if (isset($query_params['caps']) && $query_params['caps'] !== 'none') { |
|
3450 | + $query_params[0] = array_replace_recursive( |
|
3451 | + $query_params[0], |
|
3452 | + $this->caps_where_conditions( |
|
3453 | + $query_params['caps'] |
|
3454 | + ) |
|
3455 | + ); |
|
3456 | + } |
|
3457 | + |
|
3458 | + // check if we should alter the query to remove data related to protected |
|
3459 | + // custom post types |
|
3460 | + if (isset($query_params['exclude_protected']) && $query_params['exclude_protected'] === true) { |
|
3461 | + $where_param_key_for_password = $this->modelChainAndPassword(); |
|
3462 | + // only include if related to a cpt where no password has been set |
|
3463 | + $query_params[0]['OR*nopassword'] = [ |
|
3464 | + $where_param_key_for_password => '', |
|
3465 | + $where_param_key_for_password . '*' => ['IS_NULL'], |
|
3466 | + ]; |
|
3467 | + } |
|
3468 | + $query_object = $this->_extract_related_models_from_query($query_params); |
|
3469 | + // verify where_query_params has NO numeric indexes.... that's simply not how you use it! |
|
3470 | + foreach ($query_params[0] as $key => $value) { |
|
3471 | + if (is_int($key)) { |
|
3472 | + throw new EE_Error( |
|
3473 | + sprintf( |
|
3474 | + esc_html__( |
|
3475 | + "WHERE query params must NOT be numerically-indexed. You provided the array key '%s' for value '%s' while querying model %s. All the query params provided were '%s' Please read documentation on EEM_Base::get_all.", |
|
3476 | + "event_espresso" |
|
3477 | + ), |
|
3478 | + $key, |
|
3479 | + var_export($value, true), |
|
3480 | + var_export($query_params, true), |
|
3481 | + get_class($this) |
|
3482 | + ) |
|
3483 | + ); |
|
3484 | + } |
|
3485 | + } |
|
3486 | + if ( |
|
3487 | + array_key_exists('default_where_conditions', $query_params) |
|
3488 | + && ! empty($query_params['default_where_conditions']) |
|
3489 | + ) { |
|
3490 | + $use_default_where_conditions = $query_params['default_where_conditions']; |
|
3491 | + } else { |
|
3492 | + $use_default_where_conditions = EEM_Base::default_where_conditions_all; |
|
3493 | + } |
|
3494 | + $query_params[0] = array_merge( |
|
3495 | + $this->_get_default_where_conditions_for_models_in_query( |
|
3496 | + $query_object, |
|
3497 | + $use_default_where_conditions, |
|
3498 | + $query_params[0] |
|
3499 | + ), |
|
3500 | + $query_params[0] |
|
3501 | + ); |
|
3502 | + $query_object->set_where_sql($this->_construct_where_clause($query_params[0])); |
|
3503 | + // if this is a "on_join_limit" then we are limiting on on a specific table in a multi_table join. |
|
3504 | + // So we need to setup a subquery and use that for the main join. |
|
3505 | + // Note for now this only works on the primary table for the model. |
|
3506 | + // So for instance, you could set the limit array like this: |
|
3507 | + // array( 'on_join_limit' => array('Primary_Table_Alias', array(1,10) ) ) |
|
3508 | + if (array_key_exists('on_join_limit', $query_params) && ! empty($query_params['on_join_limit'])) { |
|
3509 | + $query_object->set_main_model_join_sql( |
|
3510 | + $this->_construct_limit_join_select( |
|
3511 | + $query_params['on_join_limit'][0], |
|
3512 | + $query_params['on_join_limit'][1] |
|
3513 | + ) |
|
3514 | + ); |
|
3515 | + } |
|
3516 | + // set limit |
|
3517 | + if (array_key_exists('limit', $query_params)) { |
|
3518 | + if (is_array($query_params['limit'])) { |
|
3519 | + if (! isset($query_params['limit'][0], $query_params['limit'][1])) { |
|
3520 | + $e = sprintf( |
|
3521 | + esc_html__( |
|
3522 | + "Invalid DB query. You passed '%s' for the LIMIT, but only the following are valid: an integer, string representing an integer, a string like 'int,int', or an array like array(int,int)", |
|
3523 | + "event_espresso" |
|
3524 | + ), |
|
3525 | + http_build_query($query_params['limit']) |
|
3526 | + ); |
|
3527 | + throw new EE_Error($e . "|" . $e); |
|
3528 | + } |
|
3529 | + // they passed us an array for the limit. Assume it's like array(50,25), meaning offset by 50, and get 25 |
|
3530 | + $query_object->set_limit_sql(" LIMIT " . $query_params['limit'][0] . "," . $query_params['limit'][1]); |
|
3531 | + } elseif (! empty($query_params['limit'])) { |
|
3532 | + $query_object->set_limit_sql(" LIMIT " . $query_params['limit']); |
|
3533 | + } |
|
3534 | + } |
|
3535 | + // set order by |
|
3536 | + if (array_key_exists('order_by', $query_params)) { |
|
3537 | + if (is_array($query_params['order_by'])) { |
|
3538 | + // if they're using 'order_by' as an array, they can't use 'order' (because 'order_by' must |
|
3539 | + // specify whether to ascend or descend on each field. Eg 'order_by'=>array('EVT_ID'=>'ASC'). So |
|
3540 | + // including 'order' wouldn't make any sense if 'order_by' has already specified which way to order! |
|
3541 | + if (array_key_exists('order', $query_params)) { |
|
3542 | + throw new EE_Error( |
|
3543 | + sprintf( |
|
3544 | + esc_html__( |
|
3545 | + "In querying %s, we are using query parameter 'order_by' as an array (keys:%s,values:%s), and so we can't use query parameter 'order' (value %s). You should just use the 'order_by' parameter ", |
|
3546 | + "event_espresso" |
|
3547 | + ), |
|
3548 | + get_class($this), |
|
3549 | + implode(", ", array_keys($query_params['order_by'])), |
|
3550 | + implode(", ", $query_params['order_by']), |
|
3551 | + $query_params['order'] |
|
3552 | + ) |
|
3553 | + ); |
|
3554 | + } |
|
3555 | + $this->_extract_related_models_from_sub_params_array_keys( |
|
3556 | + $query_params['order_by'], |
|
3557 | + $query_object, |
|
3558 | + 'order_by' |
|
3559 | + ); |
|
3560 | + // assume it's an array of fields to order by |
|
3561 | + $order_array = []; |
|
3562 | + foreach ($query_params['order_by'] as $field_name_to_order_by => $order) { |
|
3563 | + $order = $this->_extract_order($order); |
|
3564 | + $order_array[] = $this->_deduce_column_name_from_query_param($field_name_to_order_by) . SP . $order; |
|
3565 | + } |
|
3566 | + $query_object->set_order_by_sql(" ORDER BY " . implode(",", $order_array)); |
|
3567 | + } elseif (! empty($query_params['order_by'])) { |
|
3568 | + $this->_extract_related_model_info_from_query_param( |
|
3569 | + $query_params['order_by'], |
|
3570 | + $query_object, |
|
3571 | + 'order', |
|
3572 | + $query_params['order_by'] |
|
3573 | + ); |
|
3574 | + $order = isset($query_params['order']) |
|
3575 | + ? $this->_extract_order($query_params['order']) |
|
3576 | + : 'DESC'; |
|
3577 | + $query_object->set_order_by_sql( |
|
3578 | + " ORDER BY " . $this->_deduce_column_name_from_query_param($query_params['order_by']) . SP . $order |
|
3579 | + ); |
|
3580 | + } |
|
3581 | + } |
|
3582 | + // if 'order_by' wasn't set, maybe they are just using 'order' on its own? |
|
3583 | + if ( |
|
3584 | + ! array_key_exists('order_by', $query_params) |
|
3585 | + && array_key_exists('order', $query_params) |
|
3586 | + && ! empty($query_params['order']) |
|
3587 | + ) { |
|
3588 | + $pk_field = $this->get_primary_key_field(); |
|
3589 | + $order = $this->_extract_order($query_params['order']); |
|
3590 | + $query_object->set_order_by_sql(" ORDER BY " . $pk_field->get_qualified_column() . SP . $order); |
|
3591 | + } |
|
3592 | + // set group by |
|
3593 | + if (array_key_exists('group_by', $query_params)) { |
|
3594 | + if (is_array($query_params['group_by'])) { |
|
3595 | + // it's an array, so assume we'll be grouping by a bunch of stuff |
|
3596 | + $group_by_array = []; |
|
3597 | + foreach ($query_params['group_by'] as $field_name_to_group_by) { |
|
3598 | + $group_by_array[] = $this->_deduce_column_name_from_query_param($field_name_to_group_by); |
|
3599 | + } |
|
3600 | + $query_object->set_group_by_sql(" GROUP BY " . implode(", ", $group_by_array)); |
|
3601 | + } elseif (! empty($query_params['group_by'])) { |
|
3602 | + $query_object->set_group_by_sql( |
|
3603 | + " GROUP BY " . $this->_deduce_column_name_from_query_param($query_params['group_by']) |
|
3604 | + ); |
|
3605 | + } |
|
3606 | + } |
|
3607 | + // set having |
|
3608 | + if (array_key_exists('having', $query_params) && $query_params['having']) { |
|
3609 | + $query_object->set_having_sql($this->_construct_having_clause($query_params['having'])); |
|
3610 | + } |
|
3611 | + // now, just verify they didn't pass anything wack |
|
3612 | + foreach ($query_params as $query_key => $query_value) { |
|
3613 | + if (! in_array($query_key, $this->_allowed_query_params, true)) { |
|
3614 | + throw new EE_Error( |
|
3615 | + sprintf( |
|
3616 | + esc_html__( |
|
3617 | + "You passed %s as a query parameter to %s, which is illegal! The allowed query parameters are %s", |
|
3618 | + 'event_espresso' |
|
3619 | + ), |
|
3620 | + $query_key, |
|
3621 | + get_class($this), |
|
3622 | + // print_r( $this->_allowed_query_params, TRUE ) |
|
3623 | + implode(',', $this->_allowed_query_params) |
|
3624 | + ) |
|
3625 | + ); |
|
3626 | + } |
|
3627 | + } |
|
3628 | + $main_model_join_sql = $query_object->get_main_model_join_sql(); |
|
3629 | + if (empty($main_model_join_sql)) { |
|
3630 | + $query_object->set_main_model_join_sql($this->_construct_internal_join()); |
|
3631 | + } |
|
3632 | + return $query_object; |
|
3633 | + } |
|
3634 | + |
|
3635 | + |
|
3636 | + /** |
|
3637 | + * Gets the where conditions that should be imposed on the query based on the |
|
3638 | + * context (eg reading frontend, backend, edit or delete). |
|
3639 | + * |
|
3640 | + * @param string $context one of EEM_Base::valid_cap_contexts() |
|
3641 | + * @return array |
|
3642 | + * @throws EE_Error |
|
3643 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
3644 | + */ |
|
3645 | + public function caps_where_conditions($context = self::caps_read) |
|
3646 | + { |
|
3647 | + EEM_Base::verify_is_valid_cap_context($context); |
|
3648 | + $cap_where_conditions = []; |
|
3649 | + $cap_restrictions = $this->caps_missing($context); |
|
3650 | + foreach ($cap_restrictions as $restriction_if_no_cap) { |
|
3651 | + $cap_where_conditions = array_replace_recursive( |
|
3652 | + $cap_where_conditions, |
|
3653 | + $restriction_if_no_cap->get_default_where_conditions() |
|
3654 | + ); |
|
3655 | + } |
|
3656 | + return apply_filters( |
|
3657 | + 'FHEE__EEM_Base__caps_where_conditions__return', |
|
3658 | + $cap_where_conditions, |
|
3659 | + $this, |
|
3660 | + $context, |
|
3661 | + $cap_restrictions |
|
3662 | + ); |
|
3663 | + } |
|
3664 | + |
|
3665 | + |
|
3666 | + /** |
|
3667 | + * Verifies that $should_be_order_string is in $this->_allowed_order_values, |
|
3668 | + * otherwise throws an exception |
|
3669 | + * |
|
3670 | + * @param string $should_be_order_string |
|
3671 | + * @return string either ASC, asc, DESC or desc |
|
3672 | + * @throws EE_Error |
|
3673 | + */ |
|
3674 | + private function _extract_order($should_be_order_string) |
|
3675 | + { |
|
3676 | + if (in_array($should_be_order_string, $this->_allowed_order_values)) { |
|
3677 | + return $should_be_order_string; |
|
3678 | + } |
|
3679 | + throw new EE_Error( |
|
3680 | + sprintf( |
|
3681 | + esc_html__( |
|
3682 | + "While performing a query on '%s', tried to use '%s' as an order parameter. ", |
|
3683 | + "event_espresso" |
|
3684 | + ), |
|
3685 | + get_class($this), |
|
3686 | + $should_be_order_string |
|
3687 | + ) |
|
3688 | + ); |
|
3689 | + } |
|
3690 | + |
|
3691 | + |
|
3692 | + /** |
|
3693 | + * Looks at all the models which are included in this query, and asks each |
|
3694 | + * for their universal_where_params, and returns them in the same format as $query_params[0] (where), |
|
3695 | + * so they can be merged |
|
3696 | + * |
|
3697 | + * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
3698 | + * @param string $use_default_where_conditions can be 'none','other_models_only', or 'all'. |
|
3699 | + * 'none' means NO default where conditions will |
|
3700 | + * be used AT ALL during this query. |
|
3701 | + * 'other_models_only' means default where |
|
3702 | + * conditions from other models will be used, but |
|
3703 | + * not for this primary model. 'all', the default, |
|
3704 | + * means default where conditions will apply as |
|
3705 | + * normal |
|
3706 | + * @param array $where_query_params |
|
3707 | + * @return array |
|
3708 | + * @throws EE_Error |
|
3709 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
3710 | + */ |
|
3711 | + private function _get_default_where_conditions_for_models_in_query( |
|
3712 | + EE_Model_Query_Info_Carrier $query_info_carrier, |
|
3713 | + $use_default_where_conditions = EEM_Base::default_where_conditions_all, |
|
3714 | + $where_query_params = [] |
|
3715 | + ) { |
|
3716 | + $allowed_used_default_where_conditions_values = EEM_Base::valid_default_where_conditions(); |
|
3717 | + if (! in_array($use_default_where_conditions, $allowed_used_default_where_conditions_values)) { |
|
3718 | + throw new EE_Error( |
|
3719 | + sprintf( |
|
3720 | + esc_html__( |
|
3721 | + "You passed an invalid value to the query parameter 'default_where_conditions' of '%s'. Allowed values are %s", |
|
3722 | + "event_espresso" |
|
3723 | + ), |
|
3724 | + $use_default_where_conditions, |
|
3725 | + implode(", ", $allowed_used_default_where_conditions_values) |
|
3726 | + ) |
|
3727 | + ); |
|
3728 | + } |
|
3729 | + $universal_query_params = []; |
|
3730 | + if ($this->_should_use_default_where_conditions($use_default_where_conditions)) { |
|
3731 | + $universal_query_params = $this->_get_default_where_conditions(); |
|
3732 | + } elseif ($this->_should_use_minimum_where_conditions($use_default_where_conditions)) { |
|
3733 | + $universal_query_params = $this->_get_minimum_where_conditions(); |
|
3734 | + } |
|
3735 | + foreach ($query_info_carrier->get_model_names_included() as $model_relation_path => $model_name) { |
|
3736 | + $related_model = $this->get_related_model_obj($model_name); |
|
3737 | + if ($this->_should_use_default_where_conditions($use_default_where_conditions, false)) { |
|
3738 | + $related_model_universal_where_params = |
|
3739 | + $related_model->_get_default_where_conditions($model_relation_path); |
|
3740 | + } elseif ($this->_should_use_minimum_where_conditions($use_default_where_conditions, false)) { |
|
3741 | + $related_model_universal_where_params = |
|
3742 | + $related_model->_get_minimum_where_conditions($model_relation_path); |
|
3743 | + } else { |
|
3744 | + // we don't want to add full or even minimum default where conditions from this model, so just continue |
|
3745 | + continue; |
|
3746 | + } |
|
3747 | + $overrides = $this->_override_defaults_or_make_null_friendly( |
|
3748 | + $related_model_universal_where_params, |
|
3749 | + $where_query_params, |
|
3750 | + $related_model, |
|
3751 | + $model_relation_path |
|
3752 | + ); |
|
3753 | + $universal_query_params = EEH_Array::merge_arrays_and_overwrite_keys( |
|
3754 | + $universal_query_params, |
|
3755 | + $overrides |
|
3756 | + ); |
|
3757 | + } |
|
3758 | + return $universal_query_params; |
|
3759 | + } |
|
3760 | + |
|
3761 | + |
|
3762 | + /** |
|
3763 | + * Determines whether or not we should use default where conditions for the model in question |
|
3764 | + * (this model, or other related models). |
|
3765 | + * Basically, we should use default where conditions on this model if they have requested to use them on all models, |
|
3766 | + * this model only, or to use minimum where conditions on all other models and normal where conditions on this one. |
|
3767 | + * We should use default where conditions on related models when they requested to use default where conditions |
|
3768 | + * on all models, or specifically just on other related models |
|
3769 | + * |
|
3770 | + * @param $default_where_conditions_value |
|
3771 | + * @param bool $for_this_model false means this is for OTHER related models |
|
3772 | + * @return bool |
|
3773 | + */ |
|
3774 | + private function _should_use_default_where_conditions($default_where_conditions_value, $for_this_model = true) |
|
3775 | + { |
|
3776 | + return ( |
|
3777 | + $for_this_model |
|
3778 | + && in_array( |
|
3779 | + $default_where_conditions_value, |
|
3780 | + [ |
|
3781 | + EEM_Base::default_where_conditions_all, |
|
3782 | + EEM_Base::default_where_conditions_this_only, |
|
3783 | + EEM_Base::default_where_conditions_minimum_others, |
|
3784 | + ], |
|
3785 | + true |
|
3786 | + ) |
|
3787 | + ) |
|
3788 | + || ( |
|
3789 | + ! $for_this_model |
|
3790 | + && in_array( |
|
3791 | + $default_where_conditions_value, |
|
3792 | + [ |
|
3793 | + EEM_Base::default_where_conditions_all, |
|
3794 | + EEM_Base::default_where_conditions_others_only, |
|
3795 | + ], |
|
3796 | + true |
|
3797 | + ) |
|
3798 | + ); |
|
3799 | + } |
|
3800 | + |
|
3801 | + |
|
3802 | + /** |
|
3803 | + * Determines whether or not we should use default minimum conditions for the model in question |
|
3804 | + * (this model, or other related models). |
|
3805 | + * Basically, we should use minimum where conditions on this model only if they requested all models to use minimum |
|
3806 | + * where conditions. |
|
3807 | + * We should use minimum where conditions on related models if they requested to use minimum where conditions |
|
3808 | + * on this model or others |
|
3809 | + * |
|
3810 | + * @param $default_where_conditions_value |
|
3811 | + * @param bool $for_this_model false means this is for OTHER related models |
|
3812 | + * @return bool |
|
3813 | + */ |
|
3814 | + private function _should_use_minimum_where_conditions($default_where_conditions_value, $for_this_model = true) |
|
3815 | + { |
|
3816 | + return ( |
|
3817 | + $for_this_model |
|
3818 | + && $default_where_conditions_value === EEM_Base::default_where_conditions_minimum_all |
|
3819 | + ) |
|
3820 | + || ( |
|
3821 | + ! $for_this_model |
|
3822 | + && in_array( |
|
3823 | + $default_where_conditions_value, |
|
3824 | + [ |
|
3825 | + EEM_Base::default_where_conditions_minimum_others, |
|
3826 | + EEM_Base::default_where_conditions_minimum_all, |
|
3827 | + ], |
|
3828 | + true |
|
3829 | + ) |
|
3830 | + ); |
|
3831 | + } |
|
3832 | + |
|
3833 | + |
|
3834 | + /** |
|
3835 | + * Checks if any of the defaults have been overridden. If there are any that AREN'T overridden, |
|
3836 | + * then we also add a special where condition which allows for that model's primary key |
|
3837 | + * to be null (which is important for JOINs. Eg, if you want to see all Events ordered by Venue's name, |
|
3838 | + * then Event's with NO Venue won't appear unless you allow VNU_ID to be NULL) |
|
3839 | + * |
|
3840 | + * @param array $default_where_conditions |
|
3841 | + * @param array $provided_where_conditions |
|
3842 | + * @param EEM_Base $model |
|
3843 | + * @param string $model_relation_path like 'Transaction.Payment.' |
|
3844 | + * @return array |
|
3845 | + * @throws EE_Error |
|
3846 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
3847 | + */ |
|
3848 | + private function _override_defaults_or_make_null_friendly( |
|
3849 | + $default_where_conditions, |
|
3850 | + $provided_where_conditions, |
|
3851 | + $model, |
|
3852 | + $model_relation_path |
|
3853 | + ) { |
|
3854 | + $null_friendly_where_conditions = []; |
|
3855 | + $none_overridden = true; |
|
3856 | + $or_condition_key_for_defaults = 'OR*' . get_class($model); |
|
3857 | + foreach ($default_where_conditions as $key => $val) { |
|
3858 | + if (isset($provided_where_conditions[ $key ])) { |
|
3859 | + $none_overridden = false; |
|
3860 | + } else { |
|
3861 | + $null_friendly_where_conditions[ $or_condition_key_for_defaults ]['AND'][ $key ] = $val; |
|
3862 | + } |
|
3863 | + } |
|
3864 | + if ($none_overridden && $default_where_conditions) { |
|
3865 | + if ($model->has_primary_key_field()) { |
|
3866 | + $null_friendly_where_conditions[ $or_condition_key_for_defaults ][ $model_relation_path |
|
3867 | + . "." |
|
3868 | + . $model->primary_key_name() ] = |
|
3869 | + ['IS NULL']; |
|
3870 | + }/*else{ |
|
3871 | 3871 | //@todo NO PK, use other defaults |
3872 | 3872 | }*/ |
3873 | - } |
|
3874 | - return $null_friendly_where_conditions; |
|
3875 | - } |
|
3876 | - |
|
3877 | - |
|
3878 | - /** |
|
3879 | - * Uses the _default_where_conditions_strategy set during __construct() to get |
|
3880 | - * default where conditions on all get_all, update, and delete queries done by this model. |
|
3881 | - * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
3882 | - * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
3883 | - * |
|
3884 | - * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
3885 | - * @return array |
|
3886 | - * @throws EE_Error |
|
3887 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
3888 | - */ |
|
3889 | - private function _get_default_where_conditions($model_relation_path = '') |
|
3890 | - { |
|
3891 | - if ($this->_ignore_where_strategy) { |
|
3892 | - return []; |
|
3893 | - } |
|
3894 | - return $this->_default_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
3895 | - } |
|
3896 | - |
|
3897 | - |
|
3898 | - /** |
|
3899 | - * Uses the _minimum_where_conditions_strategy set during __construct() to get |
|
3900 | - * minimum where conditions on all get_all, update, and delete queries done by this model. |
|
3901 | - * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
3902 | - * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
3903 | - * Similar to _get_default_where_conditions |
|
3904 | - * |
|
3905 | - * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
3906 | - * @return array |
|
3907 | - * @throws EE_Error |
|
3908 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
3909 | - */ |
|
3910 | - protected function _get_minimum_where_conditions($model_relation_path = '') |
|
3911 | - { |
|
3912 | - if ($this->_ignore_where_strategy) { |
|
3913 | - return []; |
|
3914 | - } |
|
3915 | - return $this->_minimum_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
3916 | - } |
|
3917 | - |
|
3918 | - |
|
3919 | - /** |
|
3920 | - * Creates the string of SQL for the select part of a select query, everything behind SELECT and before FROM. |
|
3921 | - * Eg, "Event.post_id, Event.post_name,Event_Detail.EVT_ID..." |
|
3922 | - * |
|
3923 | - * @param EE_Model_Query_Info_Carrier $model_query_info |
|
3924 | - * @return string |
|
3925 | - * @throws EE_Error |
|
3926 | - */ |
|
3927 | - private function _construct_default_select_sql(EE_Model_Query_Info_Carrier $model_query_info) |
|
3928 | - { |
|
3929 | - $selects = $this->_get_columns_to_select_for_this_model(); |
|
3930 | - foreach ( |
|
3931 | - $model_query_info->get_model_names_included() as $model_relation_chain => $name_of_other_model_included |
|
3932 | - ) { |
|
3933 | - $other_model_included = $this->get_related_model_obj($name_of_other_model_included); |
|
3934 | - $other_model_selects = $other_model_included->_get_columns_to_select_for_this_model($model_relation_chain); |
|
3935 | - foreach ($other_model_selects as $value) { |
|
3936 | - $selects[] = $value; |
|
3937 | - } |
|
3938 | - } |
|
3939 | - return implode(", ", $selects); |
|
3940 | - } |
|
3941 | - |
|
3942 | - |
|
3943 | - /** |
|
3944 | - * Gets an array of columns to select for this model, which are necessary for it to create its objects. |
|
3945 | - * So that's going to be the columns for all the fields on the model |
|
3946 | - * |
|
3947 | - * @param string $model_relation_chain like 'Question.Question_Group.Event' |
|
3948 | - * @return array numerically indexed, values are columns to select and rename, eg "Event.ID AS 'Event.ID'" |
|
3949 | - */ |
|
3950 | - public function _get_columns_to_select_for_this_model($model_relation_chain = '') |
|
3951 | - { |
|
3952 | - $fields = $this->field_settings(); |
|
3953 | - $selects = []; |
|
3954 | - $table_alias_with_model_relation_chain_prefix = |
|
3955 | - EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
3956 | - $model_relation_chain, |
|
3957 | - $this->get_this_model_name() |
|
3958 | - ); |
|
3959 | - foreach ($fields as $field_obj) { |
|
3960 | - $selects[] = $table_alias_with_model_relation_chain_prefix |
|
3961 | - . $field_obj->get_table_alias() |
|
3962 | - . "." |
|
3963 | - . $field_obj->get_table_column() |
|
3964 | - . " AS '" |
|
3965 | - . $table_alias_with_model_relation_chain_prefix |
|
3966 | - . $field_obj->get_table_alias() |
|
3967 | - . "." |
|
3968 | - . $field_obj->get_table_column() |
|
3969 | - . "'"; |
|
3970 | - } |
|
3971 | - // make sure we are also getting the PKs of each table |
|
3972 | - $tables = $this->get_tables(); |
|
3973 | - if (count($tables) > 1) { |
|
3974 | - foreach ($tables as $table_obj) { |
|
3975 | - $qualified_pk_column = $table_alias_with_model_relation_chain_prefix |
|
3976 | - . $table_obj->get_fully_qualified_pk_column(); |
|
3977 | - if (! in_array($qualified_pk_column, $selects)) { |
|
3978 | - $selects[] = "$qualified_pk_column AS '$qualified_pk_column'"; |
|
3979 | - } |
|
3980 | - } |
|
3981 | - } |
|
3982 | - return $selects; |
|
3983 | - } |
|
3984 | - |
|
3985 | - |
|
3986 | - /** |
|
3987 | - * Given a $query_param like 'Registration.Transaction.TXN_ID', pops off 'Registration.', |
|
3988 | - * gets the join statement for it; gets the data types for it; and passes the remaining 'Transaction.TXN_ID' |
|
3989 | - * onto its related Transaction object to do the same. Returns an EE_Join_And_Data_Types object which contains the |
|
3990 | - * SQL for joining, and the data types |
|
3991 | - * |
|
3992 | - * @param string $query_param like Registration.Transaction.TXN_ID |
|
3993 | - * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
3994 | - * @param string $query_param_type like Registration.Transaction.TXN_ID |
|
3995 | - * or 'PAY_ID'. Otherwise, we don't expect there to be a |
|
3996 | - * column name. We only want model names, eg 'Event.Venue' |
|
3997 | - * or 'Registration's |
|
3998 | - * @param string|null $original_query_param what it originally was (eg |
|
3999 | - * Registration.Transaction.TXN_ID). If null, we assume it |
|
4000 | - * matches $query_param |
|
4001 | - * @return void only modifies the EEM_Related_Model_Info_Carrier passed into it |
|
4002 | - * @throws EE_Error |
|
4003 | - */ |
|
4004 | - private function _extract_related_model_info_from_query_param( |
|
4005 | - $query_param, |
|
4006 | - EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
4007 | - $query_param_type, |
|
4008 | - $original_query_param = null |
|
4009 | - ) { |
|
4010 | - if ($original_query_param === null) { |
|
4011 | - $original_query_param = $query_param; |
|
4012 | - } |
|
4013 | - $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param); |
|
4014 | - // whether or not to allow logic_query_params like 'NOT','OR', or 'AND' |
|
4015 | - $allow_logic_query_params = in_array($query_param_type, ['where', 'having', 0, 'custom_selects'], true); |
|
4016 | - $allow_fields = in_array( |
|
4017 | - $query_param_type, |
|
4018 | - ['where', 'having', 'order_by', 'group_by', 'order', 'custom_selects', 0], |
|
4019 | - true |
|
4020 | - ); |
|
4021 | - // check to see if we have a field on this model |
|
4022 | - $this_model_fields = $this->field_settings(true); |
|
4023 | - if (array_key_exists($query_param, $this_model_fields)) { |
|
4024 | - if ($allow_fields) { |
|
4025 | - return; |
|
4026 | - } |
|
4027 | - throw new EE_Error( |
|
4028 | - sprintf( |
|
4029 | - esc_html__( |
|
4030 | - "Using a field name (%s) on model %s is not allowed on this query param type '%s'. Original query param was %s", |
|
4031 | - "event_espresso" |
|
4032 | - ), |
|
4033 | - $query_param, |
|
4034 | - get_class($this), |
|
4035 | - $query_param_type, |
|
4036 | - $original_query_param |
|
4037 | - ) |
|
4038 | - ); |
|
4039 | - } |
|
4040 | - // check if this is a special logic query param |
|
4041 | - if (in_array($query_param, $this->_logic_query_param_keys, true)) { |
|
4042 | - if ($allow_logic_query_params) { |
|
4043 | - return; |
|
4044 | - } |
|
4045 | - throw new EE_Error( |
|
4046 | - sprintf( |
|
4047 | - esc_html__( |
|
4048 | - 'Logic query params ("%1$s") are being used incorrectly with the following query param ("%2$s") on model %3$s. %4$sAdditional Info:%4$s%5$s', |
|
4049 | - 'event_espresso' |
|
4050 | - ), |
|
4051 | - implode('", "', $this->_logic_query_param_keys), |
|
4052 | - $query_param, |
|
4053 | - get_class($this), |
|
4054 | - '<br />', |
|
4055 | - "\t" |
|
4056 | - . ' $passed_in_query_info = <pre>' |
|
4057 | - . print_r($passed_in_query_info, true) |
|
4058 | - . '</pre>' |
|
4059 | - . "\n\t" |
|
4060 | - . ' $query_param_type = ' |
|
4061 | - . $query_param_type |
|
4062 | - . "\n\t" |
|
4063 | - . ' $original_query_param = ' |
|
4064 | - . $original_query_param |
|
4065 | - ) |
|
4066 | - ); |
|
4067 | - } |
|
4068 | - // check if it's a custom selection |
|
4069 | - if ( |
|
4070 | - $this->_custom_selections instanceof CustomSelects |
|
4071 | - && in_array($query_param, $this->_custom_selections->columnAliases(), true) |
|
4072 | - ) { |
|
4073 | - return; |
|
4074 | - } |
|
4075 | - // check if has a model name at the beginning |
|
4076 | - // and |
|
4077 | - // check if it's a field on a related model |
|
4078 | - if ( |
|
4079 | - $this->extractJoinModelFromQueryParams( |
|
4080 | - $passed_in_query_info, |
|
4081 | - $query_param, |
|
4082 | - $original_query_param, |
|
4083 | - $query_param_type |
|
4084 | - ) |
|
4085 | - ) { |
|
4086 | - return; |
|
4087 | - } |
|
4088 | - |
|
4089 | - // ok so $query_param didn't start with a model name |
|
4090 | - // and we previously confirmed it wasn't a logic query param or field on the current model |
|
4091 | - // it's wack, that's what it is |
|
4092 | - throw new EE_Error( |
|
4093 | - sprintf( |
|
4094 | - esc_html__( |
|
4095 | - "There is no model named '%s' related to %s. Query param type is %s and original query param is %s", |
|
4096 | - "event_espresso" |
|
4097 | - ), |
|
4098 | - $query_param, |
|
4099 | - get_class($this), |
|
4100 | - $query_param_type, |
|
4101 | - $original_query_param |
|
4102 | - ) |
|
4103 | - ); |
|
4104 | - } |
|
4105 | - |
|
4106 | - |
|
4107 | - /** |
|
4108 | - * Extracts any possible join model information from the provided possible_join_string. |
|
4109 | - * This method will read the provided $possible_join_string value and determine if there are any possible model |
|
4110 | - * join |
|
4111 | - * parts that should be added to the query. |
|
4112 | - * |
|
4113 | - * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
4114 | - * @param string $possible_join_string Such as Registration.REG_ID, or Registration |
|
4115 | - * @param null|string $original_query_param |
|
4116 | - * @param string $query_parameter_type The type for the source of the $possible_join_string |
|
4117 | - * ('where', 'order_by', 'group_by', 'custom_selects' |
|
4118 | - * etc.) |
|
4119 | - * @return bool returns true if a join was added and false if not. |
|
4120 | - * @throws EE_Error |
|
4121 | - */ |
|
4122 | - private function extractJoinModelFromQueryParams( |
|
4123 | - EE_Model_Query_Info_Carrier $query_info_carrier, |
|
4124 | - $possible_join_string, |
|
4125 | - $original_query_param, |
|
4126 | - $query_parameter_type |
|
4127 | - ) { |
|
4128 | - foreach ($this->_model_relations as $valid_related_model_name => $relation_obj) { |
|
4129 | - if (strpos($possible_join_string, $valid_related_model_name . ".") === 0) { |
|
4130 | - $this->_add_join_to_model($valid_related_model_name, $query_info_carrier, $original_query_param); |
|
4131 | - $possible_join_string = substr($possible_join_string, strlen($valid_related_model_name . ".")); |
|
4132 | - if ($possible_join_string === '') { |
|
4133 | - // nothing left to $query_param |
|
4134 | - // we should actually end in a field name, not a model like this! |
|
4135 | - throw new EE_Error( |
|
4136 | - sprintf( |
|
4137 | - esc_html__( |
|
4138 | - "Query param '%s' (of type %s on model %s) shouldn't end on a period (.) ", |
|
4139 | - "event_espresso" |
|
4140 | - ), |
|
4141 | - $possible_join_string, |
|
4142 | - $query_parameter_type, |
|
4143 | - get_class($this), |
|
4144 | - $valid_related_model_name |
|
4145 | - ) |
|
4146 | - ); |
|
4147 | - } |
|
4148 | - $related_model_obj = $this->get_related_model_obj($valid_related_model_name); |
|
4149 | - $related_model_obj->_extract_related_model_info_from_query_param( |
|
4150 | - $possible_join_string, |
|
4151 | - $query_info_carrier, |
|
4152 | - $query_parameter_type, |
|
4153 | - $original_query_param |
|
4154 | - ); |
|
4155 | - return true; |
|
4156 | - } |
|
4157 | - if ($possible_join_string === $valid_related_model_name) { |
|
4158 | - $this->_add_join_to_model( |
|
4159 | - $valid_related_model_name, |
|
4160 | - $query_info_carrier, |
|
4161 | - $original_query_param |
|
4162 | - ); |
|
4163 | - return true; |
|
4164 | - } |
|
4165 | - } |
|
4166 | - return false; |
|
4167 | - } |
|
4168 | - |
|
4169 | - |
|
4170 | - /** |
|
4171 | - * Extracts related models from Custom Selects and sets up any joins for those related models. |
|
4172 | - * |
|
4173 | - * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
4174 | - * @throws EE_Error |
|
4175 | - */ |
|
4176 | - private function extractRelatedModelsFromCustomSelects(EE_Model_Query_Info_Carrier $query_info_carrier) |
|
4177 | - { |
|
4178 | - if ( |
|
4179 | - $this->_custom_selections instanceof CustomSelects |
|
4180 | - && ($this->_custom_selections->type() === CustomSelects::TYPE_STRUCTURED |
|
4181 | - || $this->_custom_selections->type() == CustomSelects::TYPE_COMPLEX |
|
4182 | - ) |
|
4183 | - ) { |
|
4184 | - $original_selects = $this->_custom_selections->originalSelects(); |
|
4185 | - foreach ($original_selects as $select_configuration) { |
|
4186 | - $this->extractJoinModelFromQueryParams( |
|
4187 | - $query_info_carrier, |
|
4188 | - $select_configuration[0], |
|
4189 | - $select_configuration[0], |
|
4190 | - 'custom_selects' |
|
4191 | - ); |
|
4192 | - } |
|
4193 | - } |
|
4194 | - } |
|
4195 | - |
|
4196 | - |
|
4197 | - /** |
|
4198 | - * Privately used by _extract_related_model_info_from_query_param to add a join to $model_name |
|
4199 | - * and store it on $passed_in_query_info |
|
4200 | - * |
|
4201 | - * @param string $model_name |
|
4202 | - * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
4203 | - * @param string $original_query_param used to extract the relation chain between the queried |
|
4204 | - * model and $model_name. Eg, if we are querying Event, |
|
4205 | - * and are adding a join to 'Payment' with the original |
|
4206 | - * query param key |
|
4207 | - * 'Registration.Transaction.Payment.PAY_amount', we want |
|
4208 | - * to extract 'Registration.Transaction.Payment', in case |
|
4209 | - * Payment wants to add default query params so that it |
|
4210 | - * will know what models to prepend onto its default query |
|
4211 | - * params or in case it wants to rename tables (in case |
|
4212 | - * there are multiple joins to the same table) |
|
4213 | - * @return void |
|
4214 | - * @throws EE_Error |
|
4215 | - */ |
|
4216 | - private function _add_join_to_model( |
|
4217 | - $model_name, |
|
4218 | - EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
4219 | - $original_query_param |
|
4220 | - ) { |
|
4221 | - $relation_obj = $this->related_settings_for($model_name); |
|
4222 | - $model_relation_chain = EE_Model_Parser::extract_model_relation_chain($model_name, $original_query_param); |
|
4223 | - // check if the relation is HABTM, because then we're essentially doing two joins |
|
4224 | - // If so, join first to the JOIN table, and add its data types, and then continue as normal |
|
4225 | - if ($relation_obj instanceof EE_HABTM_Relation) { |
|
4226 | - $join_model_obj = $relation_obj->get_join_model(); |
|
4227 | - // replace the model specified with the join model for this relation chain, whi |
|
4228 | - $relation_chain_to_join_model = |
|
4229 | - EE_Model_Parser::replace_model_name_with_join_model_name_in_model_relation_chain( |
|
4230 | - $model_name, |
|
4231 | - $join_model_obj->get_this_model_name(), |
|
4232 | - $model_relation_chain |
|
4233 | - ); |
|
4234 | - $passed_in_query_info->merge( |
|
4235 | - new EE_Model_Query_Info_Carrier( |
|
4236 | - [$relation_chain_to_join_model => $join_model_obj->get_this_model_name()], |
|
4237 | - $relation_obj->get_join_to_intermediate_model_statement($relation_chain_to_join_model) |
|
4238 | - ) |
|
4239 | - ); |
|
4240 | - } |
|
4241 | - // now just join to the other table pointed to by the relation object, and add its data types |
|
4242 | - $passed_in_query_info->merge( |
|
4243 | - new EE_Model_Query_Info_Carrier( |
|
4244 | - [$model_relation_chain => $model_name], |
|
4245 | - $relation_obj->get_join_statement($model_relation_chain) |
|
4246 | - ) |
|
4247 | - ); |
|
4248 | - } |
|
4249 | - |
|
4250 | - |
|
4251 | - /** |
|
4252 | - * Constructs SQL for where clause, like "WHERE Event.ID = 23 AND Transaction.amount > 100" etc. |
|
4253 | - * |
|
4254 | - * @param array $where_params |
|
4255 | - * @return string of SQL |
|
4256 | - * @throws EE_Error |
|
4257 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
4258 | - */ |
|
4259 | - private function _construct_where_clause($where_params) |
|
4260 | - { |
|
4261 | - $SQL = $this->_construct_condition_clause_recursive($where_params, ' AND '); |
|
4262 | - if ($SQL) { |
|
4263 | - return " WHERE " . $SQL; |
|
4264 | - } |
|
4265 | - return ''; |
|
4266 | - } |
|
4267 | - |
|
4268 | - |
|
4269 | - /** |
|
4270 | - * Just like the _construct_where_clause, except prepends 'HAVING' instead of 'WHERE', |
|
4271 | - * and should be passed HAVING parameters, not WHERE parameters |
|
4272 | - * |
|
4273 | - * @param array $having_params |
|
4274 | - * @return string |
|
4275 | - * @throws EE_Error |
|
4276 | - */ |
|
4277 | - private function _construct_having_clause($having_params) |
|
4278 | - { |
|
4279 | - $SQL = $this->_construct_condition_clause_recursive($having_params, ' AND '); |
|
4280 | - if ($SQL) { |
|
4281 | - return " HAVING " . $SQL; |
|
4282 | - } |
|
4283 | - return ''; |
|
4284 | - } |
|
4285 | - |
|
4286 | - |
|
4287 | - /** |
|
4288 | - * Used for creating nested WHERE conditions. Eg "WHERE ! (Event.ID = 3 OR ( Event_Meta.meta_key = 'bob' AND |
|
4289 | - * Event_Meta.meta_value = 'foo'))" |
|
4290 | - * |
|
4291 | - * @param array $where_params |
|
4292 | - * @param string $glue joins each sub-clause together. Should really only be " AND " or " OR "... |
|
4293 | - * @return string of SQL |
|
4294 | - * @throws EE_Error |
|
4295 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
4296 | - */ |
|
4297 | - private function _construct_condition_clause_recursive($where_params, $glue = ' AND') |
|
4298 | - { |
|
4299 | - $where_clauses = []; |
|
4300 | - foreach ($where_params as $query_param => $op_and_value_or_sub_condition) { |
|
4301 | - $query_param = |
|
4302 | - $this->_remove_stars_and_anything_after_from_condition_query_param_key( |
|
4303 | - $query_param |
|
4304 | - );// str_replace("*",'',$query_param); |
|
4305 | - if (in_array($query_param, $this->_logic_query_param_keys)) { |
|
4306 | - switch ($query_param) { |
|
4307 | - case 'not': |
|
4308 | - case 'NOT': |
|
4309 | - $where_clauses[] = "! (" |
|
4310 | - . $this->_construct_condition_clause_recursive( |
|
4311 | - $op_and_value_or_sub_condition, |
|
4312 | - $glue |
|
4313 | - ) |
|
4314 | - . ")"; |
|
4315 | - break; |
|
4316 | - case 'and': |
|
4317 | - case 'AND': |
|
4318 | - $where_clauses[] = " (" |
|
4319 | - . $this->_construct_condition_clause_recursive( |
|
4320 | - $op_and_value_or_sub_condition, |
|
4321 | - ' AND ' |
|
4322 | - ) |
|
4323 | - . ")"; |
|
4324 | - break; |
|
4325 | - case 'or': |
|
4326 | - case 'OR': |
|
4327 | - $where_clauses[] = " (" |
|
4328 | - . $this->_construct_condition_clause_recursive( |
|
4329 | - $op_and_value_or_sub_condition, |
|
4330 | - ' OR ' |
|
4331 | - ) |
|
4332 | - . ")"; |
|
4333 | - break; |
|
4334 | - } |
|
4335 | - } else { |
|
4336 | - $field_obj = $this->_deduce_field_from_query_param($query_param); |
|
4337 | - // if it's not a normal field, maybe it's a custom selection? |
|
4338 | - if (! $field_obj) { |
|
4339 | - if ($this->_custom_selections instanceof CustomSelects) { |
|
4340 | - $field_obj = $this->_custom_selections->getDataTypeForAlias($query_param); |
|
4341 | - } else { |
|
4342 | - throw new EE_Error( |
|
4343 | - sprintf( |
|
4344 | - esc_html__( |
|
4345 | - "%s is neither a valid model field name, nor a custom selection", |
|
4346 | - "event_espresso" |
|
4347 | - ), |
|
4348 | - $query_param |
|
4349 | - ) |
|
4350 | - ); |
|
4351 | - } |
|
4352 | - } |
|
4353 | - $op_and_value_sql = $this->_construct_op_and_value($op_and_value_or_sub_condition, $field_obj); |
|
4354 | - $where_clauses[] = $this->_deduce_column_name_from_query_param($query_param) . SP . $op_and_value_sql; |
|
4355 | - } |
|
4356 | - } |
|
4357 | - return $where_clauses ? implode($glue, $where_clauses) : ''; |
|
4358 | - } |
|
4359 | - |
|
4360 | - |
|
4361 | - /** |
|
4362 | - * Takes the input parameter and extract the table name (alias) and column name |
|
4363 | - * |
|
4364 | - * @param string $query_param like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
4365 | - * @return string table alias and column name for SQL, eg "Transaction.TXN_ID" |
|
4366 | - * @throws EE_Error |
|
4367 | - */ |
|
4368 | - private function _deduce_column_name_from_query_param($query_param) |
|
4369 | - { |
|
4370 | - $field = $this->_deduce_field_from_query_param($query_param); |
|
4371 | - if ($field) { |
|
4372 | - $table_alias_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_from_query_param( |
|
4373 | - $field->get_model_name(), |
|
4374 | - $query_param |
|
4375 | - ); |
|
4376 | - return $table_alias_prefix . $field->get_qualified_column(); |
|
4377 | - } |
|
4378 | - if ( |
|
4379 | - $this->_custom_selections instanceof CustomSelects |
|
4380 | - && in_array($query_param, $this->_custom_selections->columnAliases(), true) |
|
4381 | - ) { |
|
4382 | - // maybe it's custom selection item? |
|
4383 | - // if so, just use it as the "column name" |
|
4384 | - return $query_param; |
|
4385 | - } |
|
4386 | - $custom_select_aliases = $this->_custom_selections instanceof CustomSelects |
|
4387 | - ? implode(',', $this->_custom_selections->columnAliases()) |
|
4388 | - : ''; |
|
4389 | - throw new EE_Error( |
|
4390 | - sprintf( |
|
4391 | - esc_html__( |
|
4392 | - "%s is not a valid field on this model, nor a custom selection (%s)", |
|
4393 | - "event_espresso" |
|
4394 | - ), |
|
4395 | - $query_param, |
|
4396 | - $custom_select_aliases |
|
4397 | - ) |
|
4398 | - ); |
|
4399 | - } |
|
4400 | - |
|
4401 | - |
|
4402 | - /** |
|
4403 | - * Removes the * and anything after it from the condition query param key. It is useful to add the * to condition |
|
4404 | - * query param keys (eg, 'OR*', 'EVT_ID') in order for the array keys to still be unique, so that they don't get |
|
4405 | - * overwritten Takes a string like 'Event.EVT_ID*', 'TXN_total**', 'OR*1st', and 'DTT_reg_start*foobar' to |
|
4406 | - * 'Event.EVT_ID', 'TXN_total', 'OR', and 'DTT_reg_start', respectively. |
|
4407 | - * |
|
4408 | - * @param string $condition_query_param_key |
|
4409 | - * @return string |
|
4410 | - */ |
|
4411 | - private function _remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
4412 | - { |
|
4413 | - $pos_of_star = strpos($condition_query_param_key, '*'); |
|
4414 | - if ($pos_of_star === false) { |
|
4415 | - return $condition_query_param_key; |
|
4416 | - } |
|
4417 | - return substr($condition_query_param_key, 0, $pos_of_star); |
|
4418 | - } |
|
4419 | - |
|
4420 | - |
|
4421 | - /** |
|
4422 | - * creates the SQL for the operator and the value in a WHERE clause, eg "< 23" or "LIKE '%monkey%'" |
|
4423 | - * |
|
4424 | - * @param mixed array | string $op_and_value |
|
4425 | - * @param EE_Model_Field_Base|string $field_obj . If string, should be one of EEM_Base::_valid_wpdb_data_types |
|
4426 | - * @return string |
|
4427 | - * @throws EE_Error |
|
4428 | - */ |
|
4429 | - private function _construct_op_and_value($op_and_value, $field_obj) |
|
4430 | - { |
|
4431 | - if (is_array($op_and_value)) { |
|
4432 | - $operator = isset($op_and_value[0]) ? $this->_prepare_operator_for_sql($op_and_value[0]) : null; |
|
4433 | - if (! $operator) { |
|
4434 | - $php_array_like_string = []; |
|
4435 | - foreach ($op_and_value as $key => $value) { |
|
4436 | - $php_array_like_string[] = "$key=>$value"; |
|
4437 | - } |
|
4438 | - throw new EE_Error( |
|
4439 | - sprintf( |
|
4440 | - esc_html__( |
|
4441 | - "You setup a query parameter like you were going to specify an operator, but didn't. You provided '(%s)', but the operator should be at array key index 0 (eg array('>',32))", |
|
4442 | - "event_espresso" |
|
4443 | - ), |
|
4444 | - implode(",", $php_array_like_string) |
|
4445 | - ) |
|
4446 | - ); |
|
4447 | - } |
|
4448 | - $value = isset($op_and_value[1]) ? $op_and_value[1] : null; |
|
4449 | - } else { |
|
4450 | - $operator = '='; |
|
4451 | - $value = $op_and_value; |
|
4452 | - } |
|
4453 | - // check to see if the value is actually another field |
|
4454 | - if (is_array($op_and_value) && isset($op_and_value[2]) && $op_and_value[2] == true) { |
|
4455 | - return $operator . SP . $this->_deduce_column_name_from_query_param($value); |
|
4456 | - } |
|
4457 | - if (in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
4458 | - // in this case, the value should be an array, or at least a comma-separated list |
|
4459 | - // it will need to handle a little differently |
|
4460 | - $cleaned_value = $this->_construct_in_value($value, $field_obj); |
|
4461 | - // note: $cleaned_value has already been run through $wpdb->prepare() |
|
4462 | - return $operator . SP . $cleaned_value; |
|
4463 | - } |
|
4464 | - if (in_array($operator, $this->valid_between_style_operators()) && is_array($value)) { |
|
4465 | - // the value should be an array with count of two. |
|
4466 | - if (count($value) !== 2) { |
|
4467 | - throw new EE_Error( |
|
4468 | - sprintf( |
|
4469 | - esc_html__( |
|
4470 | - "The '%s' operator must be used with an array of values and there must be exactly TWO values in that array.", |
|
4471 | - 'event_espresso' |
|
4472 | - ), |
|
4473 | - "BETWEEN" |
|
4474 | - ) |
|
4475 | - ); |
|
4476 | - } |
|
4477 | - $cleaned_value = $this->_construct_between_value($value, $field_obj); |
|
4478 | - return $operator . SP . $cleaned_value; |
|
4479 | - } |
|
4480 | - if (in_array($operator, $this->valid_null_style_operators())) { |
|
4481 | - if ($value !== null) { |
|
4482 | - throw new EE_Error( |
|
4483 | - sprintf( |
|
4484 | - esc_html__( |
|
4485 | - "You attempted to give a value (%s) while using a NULL-style operator (%s). That isn't valid", |
|
4486 | - "event_espresso" |
|
4487 | - ), |
|
4488 | - $value, |
|
4489 | - $operator |
|
4490 | - ) |
|
4491 | - ); |
|
4492 | - } |
|
4493 | - return $operator; |
|
4494 | - } |
|
4495 | - if (in_array($operator, $this->valid_like_style_operators()) && ! is_array($value)) { |
|
4496 | - // if the operator is 'LIKE', we want to allow percent signs (%) and not |
|
4497 | - // remove other junk. So just treat it as a string. |
|
4498 | - return $operator . SP . $this->_wpdb_prepare_using_field($value, '%s'); |
|
4499 | - } |
|
4500 | - if (! in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
4501 | - return $operator . SP . $this->_wpdb_prepare_using_field($value, $field_obj); |
|
4502 | - } |
|
4503 | - if (in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
4504 | - throw new EE_Error( |
|
4505 | - sprintf( |
|
4506 | - esc_html__( |
|
4507 | - "Operator '%s' must be used with an array of values, eg 'Registration.REG_ID' => array('%s',array(1,2,3))", |
|
4508 | - 'event_espresso' |
|
4509 | - ), |
|
4510 | - $operator, |
|
4511 | - $operator |
|
4512 | - ) |
|
4513 | - ); |
|
4514 | - } |
|
4515 | - if (! in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
4516 | - throw new EE_Error( |
|
4517 | - sprintf( |
|
4518 | - esc_html__( |
|
4519 | - "Operator '%s' must be used with a single value, not an array. Eg 'Registration.REG_ID => array('%s',23))", |
|
4520 | - 'event_espresso' |
|
4521 | - ), |
|
4522 | - $operator, |
|
4523 | - $operator |
|
4524 | - ) |
|
4525 | - ); |
|
4526 | - } |
|
4527 | - throw new EE_Error( |
|
4528 | - sprintf( |
|
4529 | - esc_html__( |
|
4530 | - "It appears you've provided some totally invalid query parameters. Operator and value were:'%s', which isn't right at all", |
|
4531 | - "event_espresso" |
|
4532 | - ), |
|
4533 | - http_build_query($op_and_value) |
|
4534 | - ) |
|
4535 | - ); |
|
4536 | - } |
|
4537 | - |
|
4538 | - |
|
4539 | - /** |
|
4540 | - * Creates the operands to be used in a BETWEEN query, eg "'2014-12-31 20:23:33' AND '2015-01-23 12:32:54'" |
|
4541 | - * |
|
4542 | - * @param array $values |
|
4543 | - * @param EE_Model_Field_Base|string $field_obj if string, it should be the datatype to be used when querying, eg |
|
4544 | - * '%s' |
|
4545 | - * @return string |
|
4546 | - * @throws EE_Error |
|
4547 | - */ |
|
4548 | - public function _construct_between_value($values, $field_obj) |
|
4549 | - { |
|
4550 | - $cleaned_values = []; |
|
4551 | - foreach ($values as $value) { |
|
4552 | - $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
4553 | - } |
|
4554 | - return $cleaned_values[0] . " AND " . $cleaned_values[1]; |
|
4555 | - } |
|
4556 | - |
|
4557 | - |
|
4558 | - /** |
|
4559 | - * Takes an array or a comma-separated list of $values and cleans them |
|
4560 | - * according to $data_type using $wpdb->prepare, and then makes the list a |
|
4561 | - * string surrounded by ( and ). Eg, _construct_in_value(array(1,2,3),'%d') would |
|
4562 | - * return '(1,2,3)'; _construct_in_value("1,2,hack",'%d') would return '(1,2,1)' (assuming |
|
4563 | - * I'm right that a string, when interpreted as a digit, becomes a 1. It might become a 0) |
|
4564 | - * |
|
4565 | - * @param mixed $values array or comma-separated string |
|
4566 | - * @param EE_Model_Field_Base|string $field_obj if string, it should be a wpdb data type like '%s', or '%d' |
|
4567 | - * @return string of SQL to follow an 'IN' or 'NOT IN' operator |
|
4568 | - * @throws EE_Error |
|
4569 | - */ |
|
4570 | - public function _construct_in_value($values, $field_obj) |
|
4571 | - { |
|
4572 | - $prepped = []; |
|
4573 | - // check if the value is a CSV list |
|
4574 | - if (is_string($values)) { |
|
4575 | - // in which case, turn it into an array |
|
4576 | - $values = explode(',', $values); |
|
4577 | - } |
|
4578 | - // make sure we only have one of each value in the list |
|
4579 | - $values = array_unique($values); |
|
4580 | - foreach ($values as $value) { |
|
4581 | - $prepped[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
4582 | - } |
|
4583 | - // we would just LOVE to leave $cleaned_values as an empty array, and return the value as "()", |
|
4584 | - // but unfortunately that's invalid SQL. So instead we return a string which we KNOW will evaluate to be the empty set |
|
4585 | - // which is effectively equivalent to returning "()". We don't return "(0)" because that only works for auto-incrementing columns |
|
4586 | - if (empty($prepped)) { |
|
4587 | - $all_fields = $this->field_settings(); |
|
4588 | - $first_field = reset($all_fields); |
|
4589 | - $main_table = $this->_get_main_table(); |
|
4590 | - $prepped[] = "SELECT {$first_field->get_table_column()} FROM {$main_table->get_table_name()} WHERE FALSE"; |
|
4591 | - } |
|
4592 | - return '(' . implode(',', $prepped) . ')'; |
|
4593 | - } |
|
4594 | - |
|
4595 | - |
|
4596 | - /** |
|
4597 | - * @param mixed $value |
|
4598 | - * @param EE_Model_Field_Base|string $field_obj if string it should be a wpdb data type like '%d' |
|
4599 | - * @return string |
|
4600 | - * @throws EE_Error |
|
4601 | - */ |
|
4602 | - private function _wpdb_prepare_using_field($value, $field_obj) |
|
4603 | - { |
|
4604 | - global $wpdb; |
|
4605 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
4606 | - return $wpdb->prepare( |
|
4607 | - $field_obj->get_wpdb_data_type(), |
|
4608 | - $this->_prepare_value_for_use_in_db($value, $field_obj) |
|
4609 | - ); |
|
4610 | - } //$field_obj should really just be a data type |
|
4611 | - if (! in_array($field_obj, $this->_valid_wpdb_data_types)) { |
|
4612 | - throw new EE_Error( |
|
4613 | - sprintf( |
|
4614 | - esc_html__("%s is not a valid wpdb datatype. Valid ones are %s", "event_espresso"), |
|
4615 | - $field_obj, |
|
4616 | - implode(",", $this->_valid_wpdb_data_types) |
|
4617 | - ) |
|
4618 | - ); |
|
4619 | - } |
|
4620 | - return $wpdb->prepare($field_obj, $value); |
|
4621 | - } |
|
4622 | - |
|
4623 | - |
|
4624 | - /** |
|
4625 | - * Takes the input parameter and finds the model field that it indicates. |
|
4626 | - * |
|
4627 | - * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
4628 | - * @return EE_Model_Field_Base |
|
4629 | - * @throws EE_Error |
|
4630 | - */ |
|
4631 | - protected function _deduce_field_from_query_param($query_param_name) |
|
4632 | - { |
|
4633 | - // ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
4634 | - // which will help us find the database table and column |
|
4635 | - $query_param_parts = explode(".", $query_param_name); |
|
4636 | - if (empty($query_param_parts)) { |
|
4637 | - throw new EE_Error( |
|
4638 | - sprintf( |
|
4639 | - esc_html__( |
|
4640 | - "_extract_column_name is empty when trying to extract column and table name from %s", |
|
4641 | - 'event_espresso' |
|
4642 | - ), |
|
4643 | - $query_param_name |
|
4644 | - ) |
|
4645 | - ); |
|
4646 | - } |
|
4647 | - $number_of_parts = count($query_param_parts); |
|
4648 | - $last_query_param_part = $query_param_parts[ count($query_param_parts) - 1 ]; |
|
4649 | - $field_name = $last_query_param_part; |
|
4650 | - $model_obj = $number_of_parts === 1 |
|
4651 | - ? $this |
|
4652 | - // $number_of_parts >= 2, the last part is the column name, and there are only 2parts. therefore... |
|
4653 | - : $this->get_related_model_obj($query_param_parts[ $number_of_parts - 2 ]); |
|
4654 | - try { |
|
4655 | - return $model_obj->field_settings_for($field_name); |
|
4656 | - } catch (EE_Error $e) { |
|
4657 | - return null; |
|
4658 | - } |
|
4659 | - } |
|
4660 | - |
|
4661 | - |
|
4662 | - /** |
|
4663 | - * Given a field's name (ie, a key in $this->field_settings()), uses the EE_Model_Field object to get the table's |
|
4664 | - * alias and column which corresponds to it |
|
4665 | - * |
|
4666 | - * @param string $field_name |
|
4667 | - * @return string |
|
4668 | - * @throws EE_Error |
|
4669 | - */ |
|
4670 | - public function _get_qualified_column_for_field($field_name) |
|
4671 | - { |
|
4672 | - $all_fields = $this->field_settings(); |
|
4673 | - $field = isset($all_fields[ $field_name ]) ? $all_fields[ $field_name ] : false; |
|
4674 | - if ($field) { |
|
4675 | - return $field->get_qualified_column(); |
|
4676 | - } |
|
4677 | - throw new EE_Error( |
|
4678 | - sprintf( |
|
4679 | - esc_html__( |
|
4680 | - "There is no field titled %s on model %s. Either the query trying to use it is bad, or you need to add it to the list of fields on the model.", |
|
4681 | - 'event_espresso' |
|
4682 | - ), |
|
4683 | - $field_name, |
|
4684 | - get_class($this) |
|
4685 | - ) |
|
4686 | - ); |
|
4687 | - } |
|
4688 | - |
|
4689 | - |
|
4690 | - /** |
|
4691 | - * similar to \EEM_Base::_get_qualified_column_for_field() but returns an array with data for ALL fields. |
|
4692 | - * Example usage: |
|
4693 | - * EEM_Ticket::instance()->get_all_wpdb_results( |
|
4694 | - * array(), |
|
4695 | - * ARRAY_A, |
|
4696 | - * EEM_Ticket::instance()->get_qualified_columns_for_all_fields() |
|
4697 | - * ); |
|
4698 | - * is equivalent to |
|
4699 | - * EEM_Ticket::instance()->get_all_wpdb_results( array(), ARRAY_A, '*' ); |
|
4700 | - * and |
|
4701 | - * EEM_Event::instance()->get_all_wpdb_results( |
|
4702 | - * array( |
|
4703 | - * array( |
|
4704 | - * 'Datetime.Ticket.TKT_ID' => array( '<', 100 ), |
|
4705 | - * ), |
|
4706 | - * ARRAY_A, |
|
4707 | - * implode( |
|
4708 | - * ', ', |
|
4709 | - * array_merge( |
|
4710 | - * EEM_Event::instance()->get_qualified_columns_for_all_fields( '', false ), |
|
4711 | - * EEM_Ticket::instance()->get_qualified_columns_for_all_fields( 'Datetime', false ) |
|
4712 | - * ) |
|
4713 | - * ) |
|
4714 | - * ) |
|
4715 | - * ); |
|
4716 | - * selects rows from the database, selecting all the event and ticket columns, where the ticket ID is below 100 |
|
4717 | - * |
|
4718 | - * @param string $model_relation_chain the chain of models used to join between the model you want to query |
|
4719 | - * and the one whose fields you are selecting for example: when querying |
|
4720 | - * tickets model and selecting fields from the tickets model you would |
|
4721 | - * leave this parameter empty, because no models are needed to join |
|
4722 | - * between the queried model and the selected one. Likewise when |
|
4723 | - * querying the datetime model and selecting fields from the tickets |
|
4724 | - * model, it would also be left empty, because there is a direct |
|
4725 | - * relation from datetimes to tickets, so no model is needed to join |
|
4726 | - * them together. However, when querying from the event model and |
|
4727 | - * selecting fields from the ticket model, you should provide the string |
|
4728 | - * 'Datetime', indicating that the event model must first join to the |
|
4729 | - * datetime model in order to find its relation to ticket model. |
|
4730 | - * Also, when querying from the venue model and selecting fields from |
|
4731 | - * the ticket model, you should provide the string 'Event.Datetime', |
|
4732 | - * indicating you need to join the venue model to the event model, |
|
4733 | - * to the datetime model, in order to find its relation to the ticket |
|
4734 | - * model. This string is used to deduce the prefix that gets added onto |
|
4735 | - * the models' tables qualified columns |
|
4736 | - * @param bool $return_string if true, will return a string with qualified column names separated |
|
4737 | - * by ', ' if false, will simply return a numerically indexed array of |
|
4738 | - * qualified column names |
|
4739 | - * @return array|string |
|
4740 | - */ |
|
4741 | - public function get_qualified_columns_for_all_fields($model_relation_chain = '', $return_string = true) |
|
4742 | - { |
|
4743 | - $table_prefix = str_replace('.', '__', $model_relation_chain) . (empty($model_relation_chain) ? '' : '__'); |
|
4744 | - $qualified_columns = []; |
|
4745 | - foreach ($this->field_settings() as $field) { |
|
4746 | - $qualified_columns[] = $table_prefix . $field->get_qualified_column(); |
|
4747 | - } |
|
4748 | - return $return_string ? implode(', ', $qualified_columns) : $qualified_columns; |
|
4749 | - } |
|
4750 | - |
|
4751 | - |
|
4752 | - /** |
|
4753 | - * constructs the select use on special limit joins |
|
4754 | - * NOTE: for now this has only been tested and will work when the table alias is for the PRIMARY table. Although |
|
4755 | - * its setup so the select query will be setup on and just doing the special select join off of the primary table |
|
4756 | - * (as that is typically where the limits would be set). |
|
4757 | - * |
|
4758 | - * @param string $table_alias The table the select is being built for |
|
4759 | - * @param mixed|string $limit The limit for this select |
|
4760 | - * @return string The final select join element for the query. |
|
4761 | - * @throws EE_Error |
|
4762 | - */ |
|
4763 | - public function _construct_limit_join_select($table_alias, $limit) |
|
4764 | - { |
|
4765 | - $SQL = ''; |
|
4766 | - foreach ($this->_tables as $table_obj) { |
|
4767 | - if ($table_obj instanceof EE_Primary_Table) { |
|
4768 | - $SQL .= $table_alias === $table_obj->get_table_alias() |
|
4769 | - ? $table_obj->get_select_join_limit($limit) |
|
4770 | - : SP . $table_obj->get_table_name() . " AS " . $table_obj->get_table_alias() . SP; |
|
4771 | - } elseif ($table_obj instanceof EE_Secondary_Table) { |
|
4772 | - $SQL .= $table_alias === $table_obj->get_table_alias() |
|
4773 | - ? $table_obj->get_select_join_limit_join($limit) |
|
4774 | - : SP . $table_obj->get_join_sql($table_alias) . SP; |
|
4775 | - } |
|
4776 | - } |
|
4777 | - return $SQL; |
|
4778 | - } |
|
4779 | - |
|
4780 | - |
|
4781 | - /** |
|
4782 | - * Constructs the internal join if there are multiple tables, or simply the table's name and alias |
|
4783 | - * Eg "wp_post AS Event" or "wp_post AS Event INNER JOIN wp_postmeta Event_Meta ON Event.ID = Event_Meta.post_id" |
|
4784 | - * |
|
4785 | - * @return string SQL |
|
4786 | - * @throws EE_Error |
|
4787 | - */ |
|
4788 | - public function _construct_internal_join() |
|
4789 | - { |
|
4790 | - $SQL = $this->_get_main_table()->get_table_sql(); |
|
4791 | - $SQL .= $this->_construct_internal_join_to_table_with_alias($this->_get_main_table()->get_table_alias()); |
|
4792 | - return $SQL; |
|
4793 | - } |
|
4794 | - |
|
4795 | - |
|
4796 | - /** |
|
4797 | - * Constructs the SQL for joining all the tables on this model. |
|
4798 | - * Normally $alias should be the primary table's alias, but in cases where |
|
4799 | - * we have already joined to a secondary table (eg, the secondary table has a foreign key and is joined before the |
|
4800 | - * primary table) then we should provide that secondary table's alias. Eg, with $alias being the primary table's |
|
4801 | - * alias, this will construct SQL like: |
|
4802 | - * " INNER JOIN wp_esp_secondary_table AS Secondary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
4803 | - * With $alias being a secondary table's alias, this will construct SQL like: |
|
4804 | - * " INNER JOIN wp_esp_primary_table AS Primary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
4805 | - * |
|
4806 | - * @param string $alias_prefixed table alias to join to (this table should already be in the FROM SQL clause) |
|
4807 | - * @return string |
|
4808 | - * @throws EE_Error |
|
4809 | - */ |
|
4810 | - public function _construct_internal_join_to_table_with_alias($alias_prefixed) |
|
4811 | - { |
|
4812 | - $SQL = ''; |
|
4813 | - $alias_sans_prefix = EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($alias_prefixed); |
|
4814 | - foreach ($this->_tables as $table_obj) { |
|
4815 | - if ($table_obj instanceof EE_Secondary_Table) {// table is secondary table |
|
4816 | - if ($alias_sans_prefix === $table_obj->get_table_alias()) { |
|
4817 | - // so we're joining to this table, meaning the table is already in |
|
4818 | - // the FROM statement, BUT the primary table isn't. So we want |
|
4819 | - // to add the inverse join sql |
|
4820 | - $SQL .= $table_obj->get_inverse_join_sql($alias_prefixed); |
|
4821 | - } else { |
|
4822 | - // just add a regular JOIN to this table from the primary table |
|
4823 | - $SQL .= $table_obj->get_join_sql($alias_prefixed); |
|
4824 | - } |
|
4825 | - }//if it's a primary table, dont add any SQL. it should already be in the FROM statement |
|
4826 | - } |
|
4827 | - return $SQL; |
|
4828 | - } |
|
4829 | - |
|
4830 | - |
|
4831 | - /** |
|
4832 | - * Gets an array for storing all the data types on the next-to-be-executed-query. |
|
4833 | - * This should be a growing array of keys being table-columns (eg 'EVT_ID' and 'Event.EVT_ID'), and values being |
|
4834 | - * their data type (eg, '%s', '%d', etc) |
|
4835 | - * |
|
4836 | - * @return array |
|
4837 | - */ |
|
4838 | - public function _get_data_types() |
|
4839 | - { |
|
4840 | - $data_types = []; |
|
4841 | - foreach ($this->field_settings() as $field_obj) { |
|
4842 | - // $data_types[$field_obj->get_table_column()] = $field_obj->get_wpdb_data_type(); |
|
4843 | - /** @var $field_obj EE_Model_Field_Base */ |
|
4844 | - $data_types[ $field_obj->get_qualified_column() ] = $field_obj->get_wpdb_data_type(); |
|
4845 | - } |
|
4846 | - return $data_types; |
|
4847 | - } |
|
4848 | - |
|
4849 | - |
|
4850 | - /** |
|
4851 | - * Gets the model object given the relation's name / model's name (eg, 'Event', 'Registration',etc. Always singular) |
|
4852 | - * |
|
4853 | - * @param string $model_name |
|
4854 | - * @return EEM_Base |
|
4855 | - * @throws EE_Error |
|
4856 | - */ |
|
4857 | - public function get_related_model_obj($model_name) |
|
4858 | - { |
|
4859 | - $model_classname = "EEM_" . $model_name; |
|
4860 | - if (! class_exists($model_classname)) { |
|
4861 | - throw new EE_Error( |
|
4862 | - sprintf( |
|
4863 | - esc_html__( |
|
4864 | - "You specified a related model named %s in your query. No such model exists, if it did, it would have the classname %s", |
|
4865 | - 'event_espresso' |
|
4866 | - ), |
|
4867 | - $model_name, |
|
4868 | - $model_classname |
|
4869 | - ) |
|
4870 | - ); |
|
4871 | - } |
|
4872 | - return call_user_func($model_classname . "::instance"); |
|
4873 | - } |
|
4874 | - |
|
4875 | - |
|
4876 | - /** |
|
4877 | - * Returns the array of EE_ModelRelations for this model. |
|
4878 | - * |
|
4879 | - * @return EE_Model_Relation_Base[] |
|
4880 | - */ |
|
4881 | - public function relation_settings() |
|
4882 | - { |
|
4883 | - return $this->_model_relations; |
|
4884 | - } |
|
4885 | - |
|
4886 | - |
|
4887 | - /** |
|
4888 | - * Gets all related models that this model BELONGS TO. Handy to know sometimes |
|
4889 | - * because without THOSE models, this model probably doesn't have much purpose. |
|
4890 | - * (Eg, without an event, datetimes have little purpose.) |
|
4891 | - * |
|
4892 | - * @return EE_Belongs_To_Relation[] |
|
4893 | - */ |
|
4894 | - public function belongs_to_relations() |
|
4895 | - { |
|
4896 | - $belongs_to_relations = []; |
|
4897 | - foreach ($this->relation_settings() as $model_name => $relation_obj) { |
|
4898 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
4899 | - $belongs_to_relations[ $model_name ] = $relation_obj; |
|
4900 | - } |
|
4901 | - } |
|
4902 | - return $belongs_to_relations; |
|
4903 | - } |
|
4904 | - |
|
4905 | - |
|
4906 | - /** |
|
4907 | - * Returns the specified EE_Model_Relation, or throws an exception |
|
4908 | - * |
|
4909 | - * @param string $relation_name name of relation, key in $this->_relatedModels |
|
4910 | - * @return EE_Model_Relation_Base |
|
4911 | - * @throws EE_Error |
|
4912 | - */ |
|
4913 | - public function related_settings_for($relation_name) |
|
4914 | - { |
|
4915 | - $relatedModels = $this->relation_settings(); |
|
4916 | - if (! array_key_exists($relation_name, $relatedModels)) { |
|
4917 | - throw new EE_Error( |
|
4918 | - sprintf( |
|
4919 | - esc_html__( |
|
4920 | - 'Cannot get %s related to %s. There is no model relation of that type. There is, however, %s...', |
|
4921 | - 'event_espresso' |
|
4922 | - ), |
|
4923 | - $relation_name, |
|
4924 | - $this->_get_class_name(), |
|
4925 | - implode(', ', array_keys($relatedModels)) |
|
4926 | - ) |
|
4927 | - ); |
|
4928 | - } |
|
4929 | - return $relatedModels[ $relation_name ]; |
|
4930 | - } |
|
4931 | - |
|
4932 | - |
|
4933 | - /** |
|
4934 | - * A convenience method for getting a specific field's settings, instead of getting all field settings for all |
|
4935 | - * fields |
|
4936 | - * |
|
4937 | - * @param string $fieldName |
|
4938 | - * @param boolean $include_db_only_fields |
|
4939 | - * @return EE_Model_Field_Base |
|
4940 | - * @throws EE_Error |
|
4941 | - */ |
|
4942 | - public function field_settings_for($fieldName, $include_db_only_fields = true) |
|
4943 | - { |
|
4944 | - $fieldSettings = $this->field_settings($include_db_only_fields); |
|
4945 | - if (! array_key_exists($fieldName, $fieldSettings)) { |
|
4946 | - throw new EE_Error( |
|
4947 | - sprintf( |
|
4948 | - esc_html__("There is no field/column '%s' on '%s'", 'event_espresso'), |
|
4949 | - $fieldName, |
|
4950 | - get_class($this) |
|
4951 | - ) |
|
4952 | - ); |
|
4953 | - } |
|
4954 | - return $fieldSettings[ $fieldName ]; |
|
4955 | - } |
|
4956 | - |
|
4957 | - |
|
4958 | - /** |
|
4959 | - * Checks if this field exists on this model |
|
4960 | - * |
|
4961 | - * @param string $fieldName a key in the model's _field_settings array |
|
4962 | - * @return boolean |
|
4963 | - */ |
|
4964 | - public function has_field($fieldName) |
|
4965 | - { |
|
4966 | - $fieldSettings = $this->field_settings(true); |
|
4967 | - if (isset($fieldSettings[ $fieldName ])) { |
|
4968 | - return true; |
|
4969 | - } |
|
4970 | - return false; |
|
4971 | - } |
|
4972 | - |
|
4973 | - |
|
4974 | - /** |
|
4975 | - * Returns whether or not this model has a relation to the specified model |
|
4976 | - * |
|
4977 | - * @param string $relation_name possibly one of the keys in the relation_settings array |
|
4978 | - * @return boolean |
|
4979 | - */ |
|
4980 | - public function has_relation($relation_name) |
|
4981 | - { |
|
4982 | - $relations = $this->relation_settings(); |
|
4983 | - if (isset($relations[ $relation_name ])) { |
|
4984 | - return true; |
|
4985 | - } |
|
4986 | - return false; |
|
4987 | - } |
|
4988 | - |
|
4989 | - |
|
4990 | - /** |
|
4991 | - * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
4992 | - * Eg, on EE_Answer that would be ANS_ID field object |
|
4993 | - * |
|
4994 | - * @param $field_obj |
|
4995 | - * @return boolean |
|
4996 | - */ |
|
4997 | - public function is_primary_key_field($field_obj) |
|
4998 | - { |
|
4999 | - return $field_obj instanceof EE_Primary_Key_Field_Base; |
|
5000 | - } |
|
5001 | - |
|
5002 | - |
|
5003 | - /** |
|
5004 | - * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
5005 | - * Eg, on EE_Answer that would be ANS_ID field object |
|
5006 | - * |
|
5007 | - * @return EE_Model_Field_Base |
|
5008 | - * @throws EE_Error |
|
5009 | - */ |
|
5010 | - public function get_primary_key_field() |
|
5011 | - { |
|
5012 | - if ($this->_primary_key_field === null) { |
|
5013 | - foreach ($this->field_settings(true) as $field_obj) { |
|
5014 | - if ($this->is_primary_key_field($field_obj)) { |
|
5015 | - $this->_primary_key_field = $field_obj; |
|
5016 | - break; |
|
5017 | - } |
|
5018 | - } |
|
5019 | - if (! $this->_primary_key_field instanceof EE_Primary_Key_Field_Base) { |
|
5020 | - throw new EE_Error( |
|
5021 | - sprintf( |
|
5022 | - esc_html__("There is no Primary Key defined on model %s", 'event_espresso'), |
|
5023 | - get_class($this) |
|
5024 | - ) |
|
5025 | - ); |
|
5026 | - } |
|
5027 | - } |
|
5028 | - return $this->_primary_key_field; |
|
5029 | - } |
|
5030 | - |
|
5031 | - |
|
5032 | - /** |
|
5033 | - * Returns whether or not not there is a primary key on this model. |
|
5034 | - * Internally does some caching. |
|
5035 | - * |
|
5036 | - * @return boolean |
|
5037 | - */ |
|
5038 | - public function has_primary_key_field() |
|
5039 | - { |
|
5040 | - if ($this->_has_primary_key_field === null) { |
|
5041 | - try { |
|
5042 | - $this->get_primary_key_field(); |
|
5043 | - $this->_has_primary_key_field = true; |
|
5044 | - } catch (EE_Error $e) { |
|
5045 | - $this->_has_primary_key_field = false; |
|
5046 | - } |
|
5047 | - } |
|
5048 | - return $this->_has_primary_key_field; |
|
5049 | - } |
|
5050 | - |
|
5051 | - |
|
5052 | - /** |
|
5053 | - * Finds the first field of type $field_class_name. |
|
5054 | - * |
|
5055 | - * @param string $field_class_name class name of field that you want to find. Eg, EE_Datetime_Field, |
|
5056 | - * EE_Foreign_Key_Field, etc |
|
5057 | - * @return EE_Model_Field_Base or null if none is found |
|
5058 | - */ |
|
5059 | - public function get_a_field_of_type($field_class_name) |
|
5060 | - { |
|
5061 | - foreach ($this->field_settings() as $field) { |
|
5062 | - if ($field instanceof $field_class_name) { |
|
5063 | - return $field; |
|
5064 | - } |
|
5065 | - } |
|
5066 | - return null; |
|
5067 | - } |
|
5068 | - |
|
5069 | - |
|
5070 | - /** |
|
5071 | - * Gets a foreign key field pointing to model. |
|
5072 | - * |
|
5073 | - * @param string $model_name eg Event, Registration, not EEM_Event |
|
5074 | - * @return EE_Foreign_Key_Field_Base |
|
5075 | - * @throws EE_Error |
|
5076 | - */ |
|
5077 | - public function get_foreign_key_to($model_name) |
|
5078 | - { |
|
5079 | - if (! isset($this->_cache_foreign_key_to_fields[ $model_name ])) { |
|
5080 | - foreach ($this->field_settings() as $field) { |
|
5081 | - if ( |
|
5082 | - $field instanceof EE_Foreign_Key_Field_Base |
|
5083 | - && in_array($model_name, $field->get_model_names_pointed_to()) |
|
5084 | - ) { |
|
5085 | - $this->_cache_foreign_key_to_fields[ $model_name ] = $field; |
|
5086 | - break; |
|
5087 | - } |
|
5088 | - } |
|
5089 | - if (! isset($this->_cache_foreign_key_to_fields[ $model_name ])) { |
|
5090 | - throw new EE_Error( |
|
5091 | - sprintf( |
|
5092 | - esc_html__( |
|
5093 | - "There is no foreign key field pointing to model %s on model %s", |
|
5094 | - 'event_espresso' |
|
5095 | - ), |
|
5096 | - $model_name, |
|
5097 | - get_class($this) |
|
5098 | - ) |
|
5099 | - ); |
|
5100 | - } |
|
5101 | - } |
|
5102 | - return $this->_cache_foreign_key_to_fields[ $model_name ]; |
|
5103 | - } |
|
5104 | - |
|
5105 | - |
|
5106 | - /** |
|
5107 | - * Gets the table name (including $wpdb->prefix) for the table alias |
|
5108 | - * |
|
5109 | - * @param string $table_alias eg Event, Event_Meta, Registration, Transaction, but maybe |
|
5110 | - * a table alias with a model chain prefix, like 'Venue__Event_Venue___Event_Meta'. |
|
5111 | - * Either one works |
|
5112 | - * @return string |
|
5113 | - */ |
|
5114 | - public function get_table_for_alias($table_alias) |
|
5115 | - { |
|
5116 | - $table_alias_sans_model_relation_chain_prefix = |
|
5117 | - EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($table_alias); |
|
5118 | - return $this->_tables[ $table_alias_sans_model_relation_chain_prefix ]->get_table_name(); |
|
5119 | - } |
|
5120 | - |
|
5121 | - |
|
5122 | - /** |
|
5123 | - * Returns a flat array of all field son this model, instead of organizing them |
|
5124 | - * by table_alias as they are in the constructor. |
|
5125 | - * |
|
5126 | - * @param bool $include_db_only_fields flag indicating whether or not to include the db-only fields |
|
5127 | - * @return EE_Model_Field_Base[] where the keys are the field's name |
|
5128 | - */ |
|
5129 | - public function field_settings($include_db_only_fields = false) |
|
5130 | - { |
|
5131 | - if ($include_db_only_fields) { |
|
5132 | - if ($this->_cached_fields === null) { |
|
5133 | - $this->_cached_fields = []; |
|
5134 | - foreach ($this->_fields as $fields_corresponding_to_table) { |
|
5135 | - foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
5136 | - $this->_cached_fields[ $field_name ] = $field_obj; |
|
5137 | - } |
|
5138 | - } |
|
5139 | - } |
|
5140 | - return $this->_cached_fields; |
|
5141 | - } |
|
5142 | - if ($this->_cached_fields_non_db_only === null) { |
|
5143 | - $this->_cached_fields_non_db_only = []; |
|
5144 | - foreach ($this->_fields as $fields_corresponding_to_table) { |
|
5145 | - foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
5146 | - /** @var $field_obj EE_Model_Field_Base */ |
|
5147 | - if (! $field_obj->is_db_only_field()) { |
|
5148 | - $this->_cached_fields_non_db_only[ $field_name ] = $field_obj; |
|
5149 | - } |
|
5150 | - } |
|
5151 | - } |
|
5152 | - } |
|
5153 | - return $this->_cached_fields_non_db_only; |
|
5154 | - } |
|
5155 | - |
|
5156 | - |
|
5157 | - /** |
|
5158 | - * cycle though array of attendees and create objects out of each item |
|
5159 | - * |
|
5160 | - * @access private |
|
5161 | - * @param array $rows of results of $wpdb->get_results($query,ARRAY_A) |
|
5162 | - * @return EE_Base_Class[] array keys are primary keys (if there is a primary key on the model. if not, |
|
5163 | - * numerically indexed) |
|
5164 | - * @throws EE_Error |
|
5165 | - * @throws ReflectionException |
|
5166 | - */ |
|
5167 | - protected function _create_objects($rows = []) |
|
5168 | - { |
|
5169 | - $array_of_objects = []; |
|
5170 | - if (empty($rows)) { |
|
5171 | - return []; |
|
5172 | - } |
|
5173 | - $count_if_model_has_no_primary_key = 0; |
|
5174 | - $has_primary_key = $this->has_primary_key_field(); |
|
5175 | - $primary_key_field = $has_primary_key ? $this->get_primary_key_field() : null; |
|
5176 | - foreach ((array) $rows as $row) { |
|
5177 | - if (empty($row)) { |
|
5178 | - // wp did its weird thing where it returns an array like array(0=>null), which is totally not helpful... |
|
5179 | - return []; |
|
5180 | - } |
|
5181 | - // check if we've already set this object in the results array, |
|
5182 | - // in which case there's no need to process it further (again) |
|
5183 | - if ($has_primary_key) { |
|
5184 | - $table_pk_value = $this->_get_column_value_with_table_alias_or_not( |
|
5185 | - $row, |
|
5186 | - $primary_key_field->get_qualified_column(), |
|
5187 | - $primary_key_field->get_table_column() |
|
5188 | - ); |
|
5189 | - if ($table_pk_value && isset($array_of_objects[ $table_pk_value ])) { |
|
5190 | - continue; |
|
5191 | - } |
|
5192 | - } |
|
5193 | - $classInstance = $this->instantiate_class_from_array_or_object($row); |
|
5194 | - if (! $classInstance) { |
|
5195 | - throw new EE_Error( |
|
5196 | - sprintf( |
|
5197 | - esc_html__('Could not create instance of class %s from row %s', 'event_espresso'), |
|
5198 | - $this->get_this_model_name(), |
|
5199 | - http_build_query($row) |
|
5200 | - ) |
|
5201 | - ); |
|
5202 | - } |
|
5203 | - // set the timezone on the instantiated objects |
|
5204 | - $classInstance->set_timezone($this->get_timezone(), true); |
|
5205 | - // make sure if there is any timezone setting present that we set the timezone for the object |
|
5206 | - $key = $has_primary_key ? $classInstance->ID() : $count_if_model_has_no_primary_key++; |
|
5207 | - $array_of_objects[ $key ] = $classInstance; |
|
5208 | - // also, for all the relations of type BelongsTo, see if we can cache |
|
5209 | - // those related models |
|
5210 | - // (we could do this for other relations too, but if there are conditions |
|
5211 | - // that filtered out some fo the results, then we'd be caching an incomplete set |
|
5212 | - // so it requires a little more thought than just caching them immediately...) |
|
5213 | - foreach ($this->_model_relations as $modelName => $relation_obj) { |
|
5214 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
5215 | - // check if this model's INFO is present. If so, cache it on the model |
|
5216 | - $other_model = $relation_obj->get_other_model(); |
|
5217 | - $other_model_obj_maybe = $other_model->instantiate_class_from_array_or_object($row); |
|
5218 | - // if we managed to make a model object from the results, cache it on the main model object |
|
5219 | - if ($other_model_obj_maybe) { |
|
5220 | - // set timezone on these other model objects if they are present |
|
5221 | - $other_model_obj_maybe->set_timezone($this->get_timezone(), true); |
|
5222 | - $classInstance->cache($modelName, $other_model_obj_maybe); |
|
5223 | - } |
|
5224 | - } |
|
5225 | - } |
|
5226 | - // also, if this was a custom select query, let's see if there are any results for the custom select fields |
|
5227 | - // and add them to the object as well. We'll convert according to the set data_type if there's any set for |
|
5228 | - // the field in the CustomSelects object |
|
5229 | - if ($this->_custom_selections instanceof CustomSelects) { |
|
5230 | - $classInstance->setCustomSelectsValues( |
|
5231 | - $this->getValuesForCustomSelectAliasesFromResults($row) |
|
5232 | - ); |
|
5233 | - } |
|
5234 | - } |
|
5235 | - return $array_of_objects; |
|
5236 | - } |
|
5237 | - |
|
5238 | - |
|
5239 | - /** |
|
5240 | - * This will parse a given row of results from the db and see if any keys in the results match an alias within the |
|
5241 | - * current CustomSelects object. This will be used to build an array of values indexed by those keys. |
|
5242 | - * |
|
5243 | - * @param array $db_results_row |
|
5244 | - * @return array |
|
5245 | - */ |
|
5246 | - protected function getValuesForCustomSelectAliasesFromResults(array $db_results_row) |
|
5247 | - { |
|
5248 | - $results = []; |
|
5249 | - if ($this->_custom_selections instanceof CustomSelects) { |
|
5250 | - foreach ($this->_custom_selections->columnAliases() as $alias) { |
|
5251 | - if (isset($db_results_row[ $alias ])) { |
|
5252 | - $results[ $alias ] = $this->convertValueToDataType( |
|
5253 | - $db_results_row[ $alias ], |
|
5254 | - $this->_custom_selections->getDataTypeForAlias($alias) |
|
5255 | - ); |
|
5256 | - } |
|
5257 | - } |
|
5258 | - } |
|
5259 | - return $results; |
|
5260 | - } |
|
5261 | - |
|
5262 | - |
|
5263 | - /** |
|
5264 | - * This will set the value for the given alias |
|
5265 | - * |
|
5266 | - * @param string $value |
|
5267 | - * @param string $datatype (one of %d, %s, %f) |
|
5268 | - * @return int|string|float (int for %d, string for %s, float for %f) |
|
5269 | - */ |
|
5270 | - protected function convertValueToDataType($value, $datatype) |
|
5271 | - { |
|
5272 | - switch ($datatype) { |
|
5273 | - case '%f': |
|
5274 | - return (float) $value; |
|
5275 | - case '%d': |
|
5276 | - return (int) $value; |
|
5277 | - default: |
|
5278 | - return (string) $value; |
|
5279 | - } |
|
5280 | - } |
|
5281 | - |
|
5282 | - |
|
5283 | - /** |
|
5284 | - * The purpose of this method is to allow us to create a model object that is not in the db that holds default |
|
5285 | - * values. A typical example of where this is used is when creating a new item and the initial load of a form. We |
|
5286 | - * dont' necessarily want to test for if the object is present but just assume it is BUT load the defaults from the |
|
5287 | - * object (as set in the model_field!). |
|
5288 | - * |
|
5289 | - * @return EE_Base_Class single EE_Base_Class object with default values for the properties. |
|
5290 | - * @throws EE_Error |
|
5291 | - * @throws ReflectionException |
|
5292 | - */ |
|
5293 | - public function create_default_object() |
|
5294 | - { |
|
5295 | - $this_model_fields_and_values = []; |
|
5296 | - // setup the row using default values; |
|
5297 | - foreach ($this->field_settings() as $field_name => $field_obj) { |
|
5298 | - $this_model_fields_and_values[ $field_name ] = $field_obj->get_default_value(); |
|
5299 | - } |
|
5300 | - $className = $this->_get_class_name(); |
|
5301 | - return EE_Registry::instance()->load_class( |
|
5302 | - $className, |
|
5303 | - [$this_model_fields_and_values], |
|
5304 | - false, |
|
5305 | - false |
|
5306 | - ); |
|
5307 | - } |
|
5308 | - |
|
5309 | - |
|
5310 | - /** |
|
5311 | - * @param mixed $cols_n_values either an array of where each key is the name of a field, and the value is its value |
|
5312 | - * or an stdClass where each property is the name of a column, |
|
5313 | - * @return EE_Base_Class |
|
5314 | - * @throws EE_Error |
|
5315 | - * @throws ReflectionException |
|
5316 | - */ |
|
5317 | - public function instantiate_class_from_array_or_object($cols_n_values) |
|
5318 | - { |
|
5319 | - if (! is_array($cols_n_values) && is_object($cols_n_values)) { |
|
5320 | - $cols_n_values = get_object_vars($cols_n_values); |
|
5321 | - } |
|
5322 | - $primary_key = null; |
|
5323 | - // make sure the array only has keys that are fields/columns on this model |
|
5324 | - $this_model_fields_n_values = $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
5325 | - if ($this->has_primary_key_field() && isset($this_model_fields_n_values[ $this->primary_key_name() ])) { |
|
5326 | - $primary_key = $this_model_fields_n_values[ $this->primary_key_name() ]; |
|
5327 | - } |
|
5328 | - $className = $this->_get_class_name(); |
|
5329 | - // check we actually found results that we can use to build our model object |
|
5330 | - // if not, return null |
|
5331 | - if ($this->has_primary_key_field()) { |
|
5332 | - if (empty($this_model_fields_n_values[ $this->primary_key_name() ])) { |
|
5333 | - return null; |
|
5334 | - } |
|
5335 | - } elseif ($this->unique_indexes()) { |
|
5336 | - $first_column = reset($this_model_fields_n_values); |
|
5337 | - if (empty($first_column)) { |
|
5338 | - return null; |
|
5339 | - } |
|
5340 | - } |
|
5341 | - // if there is no primary key or the object doesn't already exist in the entity map, then create a new instance |
|
5342 | - if ($primary_key) { |
|
5343 | - $classInstance = $this->get_from_entity_map($primary_key); |
|
5344 | - if (! $classInstance) { |
|
5345 | - $classInstance = EE_Registry::instance()->load_class( |
|
5346 | - $className, |
|
5347 | - [$this_model_fields_n_values, $this->get_timezone()], |
|
5348 | - true, |
|
5349 | - false |
|
5350 | - ); |
|
5351 | - // add this new object to the entity map |
|
5352 | - $classInstance = $this->add_to_entity_map($classInstance); |
|
5353 | - } |
|
5354 | - } else { |
|
5355 | - $classInstance = EE_Registry::instance()->load_class( |
|
5356 | - $className, |
|
5357 | - [$this_model_fields_n_values, $this->get_timezone()], |
|
5358 | - true, |
|
5359 | - false |
|
5360 | - ); |
|
5361 | - } |
|
5362 | - return $classInstance; |
|
5363 | - } |
|
5364 | - |
|
5365 | - |
|
5366 | - /** |
|
5367 | - * Gets the model object from the entity map if it exists |
|
5368 | - * |
|
5369 | - * @param int|string $id the ID of the model object |
|
5370 | - * @return EE_Base_Class |
|
5371 | - */ |
|
5372 | - public function get_from_entity_map($id) |
|
5373 | - { |
|
5374 | - return isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]) |
|
5375 | - ? $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ] |
|
5376 | - : null; |
|
5377 | - } |
|
5378 | - |
|
5379 | - |
|
5380 | - /** |
|
5381 | - * add_to_entity_map |
|
5382 | - * Adds the object to the model's entity mappings |
|
5383 | - * Effectively tells the models "Hey, this model object is the most up-to-date representation of the data, |
|
5384 | - * and for the remainder of the request, it's even more up-to-date than what's in the database. |
|
5385 | - * So, if the database doesn't agree with what's in the entity mapper, ignore the database" |
|
5386 | - * If the database gets updated directly and you want the entity mapper to reflect that change, |
|
5387 | - * then this method should be called immediately after the update query |
|
5388 | - * Note: The map is indexed by whatever the current blog id is set (via EEM_Base::$_model_query_blog_id). This is |
|
5389 | - * so on multisite, the entity map is specific to the query being done for a specific site. |
|
5390 | - * |
|
5391 | - * @param EE_Base_Class $object |
|
5392 | - * @return EE_Base_Class |
|
5393 | - * @throws EE_Error |
|
5394 | - */ |
|
5395 | - public function add_to_entity_map(EE_Base_Class $object) |
|
5396 | - { |
|
5397 | - $className = $this->_get_class_name(); |
|
5398 | - if (! $object instanceof $className) { |
|
5399 | - throw new EE_Error( |
|
5400 | - sprintf( |
|
5401 | - esc_html__("You tried adding a %s to a mapping of %ss", "event_espresso"), |
|
5402 | - is_object($object) ? get_class($object) : $object, |
|
5403 | - $className |
|
5404 | - ) |
|
5405 | - ); |
|
5406 | - } |
|
5407 | - if (! $object->ID()) { |
|
5408 | - throw new DomainException( |
|
5409 | - sprintf( |
|
5410 | - esc_html__( |
|
5411 | - "You tried storing a model object with NO ID in the %s entity mapper.", |
|
5412 | - "event_espresso" |
|
5413 | - ), |
|
5414 | - get_class($this) |
|
5415 | - ) |
|
5416 | - ); |
|
5417 | - } |
|
5418 | - // double check it's not already there |
|
5419 | - $classInstance = $this->get_from_entity_map($object->ID()); |
|
5420 | - if ($classInstance) { |
|
5421 | - return $classInstance; |
|
5422 | - } |
|
5423 | - $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $object->ID() ] = $object; |
|
5424 | - return $object; |
|
5425 | - } |
|
5426 | - |
|
5427 | - |
|
5428 | - /** |
|
5429 | - * if a valid identifier is provided, then that entity is unset from the entity map, |
|
5430 | - * if no identifier is provided, then the entire entity map is emptied |
|
5431 | - * |
|
5432 | - * @param int|string $id the ID of the model object |
|
5433 | - * @return boolean |
|
5434 | - */ |
|
5435 | - public function clear_entity_map($id = null) |
|
5436 | - { |
|
5437 | - if (empty($id)) { |
|
5438 | - $this->_entity_map[ EEM_Base::$_model_query_blog_id ] = []; |
|
5439 | - return true; |
|
5440 | - } |
|
5441 | - if (isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ])) { |
|
5442 | - unset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]); |
|
5443 | - return true; |
|
5444 | - } |
|
5445 | - return false; |
|
5446 | - } |
|
5447 | - |
|
5448 | - |
|
5449 | - /** |
|
5450 | - * Public wrapper for _deduce_fields_n_values_from_cols_n_values. |
|
5451 | - * Given an array where keys are column (or column alias) names and values, |
|
5452 | - * returns an array of their corresponding field names and database values |
|
5453 | - * |
|
5454 | - * @param array $cols_n_values |
|
5455 | - * @return array |
|
5456 | - * @throws EE_Error |
|
5457 | - * @throws ReflectionException |
|
5458 | - */ |
|
5459 | - public function deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
5460 | - { |
|
5461 | - return $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
5462 | - } |
|
5463 | - |
|
5464 | - |
|
5465 | - /** |
|
5466 | - * _deduce_fields_n_values_from_cols_n_values |
|
5467 | - * Given an array where keys are column (or column alias) names and values, |
|
5468 | - * returns an array of their corresponding field names and database values |
|
5469 | - * |
|
5470 | - * @param array $cols_n_values |
|
5471 | - * @return array |
|
5472 | - * @throws EE_Error |
|
5473 | - * @throws ReflectionException |
|
5474 | - */ |
|
5475 | - protected function _deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
5476 | - { |
|
5477 | - $this_model_fields_n_values = []; |
|
5478 | - foreach ($this->get_tables() as $table_alias => $table_obj) { |
|
5479 | - $table_pk_value = $this->_get_column_value_with_table_alias_or_not( |
|
5480 | - $cols_n_values, |
|
5481 | - $table_obj->get_fully_qualified_pk_column(), |
|
5482 | - $table_obj->get_pk_column() |
|
5483 | - ); |
|
5484 | - // there is a primary key on this table and its not set. Use defaults for all its columns |
|
5485 | - if ($table_pk_value === null && $table_obj->get_pk_column()) { |
|
5486 | - foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
5487 | - if (! $field_obj->is_db_only_field()) { |
|
5488 | - // prepare field as if its coming from db |
|
5489 | - $prepared_value = |
|
5490 | - $field_obj->prepare_for_set($field_obj->get_default_value()); |
|
5491 | - $this_model_fields_n_values[ $field_name ] = $field_obj->prepare_for_use_in_db($prepared_value); |
|
5492 | - } |
|
5493 | - } |
|
5494 | - } else { |
|
5495 | - // the table's rows existed. Use their values |
|
5496 | - foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
5497 | - if (! $field_obj->is_db_only_field()) { |
|
5498 | - $this_model_fields_n_values[ $field_name ] = $this->_get_column_value_with_table_alias_or_not( |
|
5499 | - $cols_n_values, |
|
5500 | - $field_obj->get_qualified_column(), |
|
5501 | - $field_obj->get_table_column() |
|
5502 | - ); |
|
5503 | - } |
|
5504 | - } |
|
5505 | - } |
|
5506 | - } |
|
5507 | - return $this_model_fields_n_values; |
|
5508 | - } |
|
5509 | - |
|
5510 | - |
|
5511 | - /** |
|
5512 | - * @param array $cols_n_values |
|
5513 | - * @param string $qualified_column |
|
5514 | - * @param string $regular_column |
|
5515 | - * @return null |
|
5516 | - * @throws EE_Error |
|
5517 | - * @throws ReflectionException |
|
5518 | - */ |
|
5519 | - protected function _get_column_value_with_table_alias_or_not($cols_n_values, $qualified_column, $regular_column) |
|
5520 | - { |
|
5521 | - $value = null; |
|
5522 | - // ask the field what it think it's table_name.column_name should be, and call it the "qualified column" |
|
5523 | - // does the field on the model relate to this column retrieved from the db? |
|
5524 | - // or is it a db-only field? (not relating to the model) |
|
5525 | - if (isset($cols_n_values[ $qualified_column ])) { |
|
5526 | - $value = $cols_n_values[ $qualified_column ]; |
|
5527 | - } elseif (isset($cols_n_values[ $regular_column ])) { |
|
5528 | - $value = $cols_n_values[ $regular_column ]; |
|
5529 | - } elseif (! empty($this->foreign_key_aliases)) { |
|
5530 | - // no PK? ok check if there is a foreign key alias set for this table |
|
5531 | - // then check if that alias exists in the incoming data |
|
5532 | - // AND that the actual PK the $FK_alias represents matches the $qualified_column (full PK) |
|
5533 | - foreach ($this->foreign_key_aliases as $FK_alias => $PK_column) { |
|
5534 | - if ($PK_column === $qualified_column && isset($cols_n_values[ $FK_alias ])) { |
|
5535 | - $value = $cols_n_values[ $FK_alias ]; |
|
5536 | - [$pk_class] = explode('.', $PK_column); |
|
5537 | - $pk_model_name = "EEM_{$pk_class}"; |
|
5538 | - /** @var EEM_Base $pk_model */ |
|
5539 | - $pk_model = EE_Registry::instance()->load_model($pk_model_name); |
|
5540 | - if ($pk_model instanceof EEM_Base) { |
|
5541 | - // make sure object is pulled from db and added to entity map |
|
5542 | - $pk_model->get_one_by_ID($value); |
|
5543 | - } |
|
5544 | - break; |
|
5545 | - } |
|
5546 | - } |
|
5547 | - } |
|
5548 | - return $value; |
|
5549 | - } |
|
5550 | - |
|
5551 | - |
|
5552 | - /** |
|
5553 | - * refresh_entity_map_from_db |
|
5554 | - * Makes sure the model object in the entity map at $id assumes the values |
|
5555 | - * of the database (opposite of EE_base_Class::save()) |
|
5556 | - * |
|
5557 | - * @param int|string $id |
|
5558 | - * @return EE_Base_Class |
|
5559 | - * @throws EE_Error |
|
5560 | - * @throws ReflectionException |
|
5561 | - */ |
|
5562 | - public function refresh_entity_map_from_db($id) |
|
5563 | - { |
|
5564 | - $obj_in_map = $this->get_from_entity_map($id); |
|
5565 | - if ($obj_in_map) { |
|
5566 | - $wpdb_results = $this->_get_all_wpdb_results( |
|
5567 | - [[$this->get_primary_key_field()->get_name() => $id], 'limit' => 1] |
|
5568 | - ); |
|
5569 | - if ($wpdb_results && is_array($wpdb_results)) { |
|
5570 | - $one_row = reset($wpdb_results); |
|
5571 | - foreach ($this->_deduce_fields_n_values_from_cols_n_values($one_row) as $field_name => $db_value) { |
|
5572 | - $obj_in_map->set_from_db($field_name, $db_value); |
|
5573 | - } |
|
5574 | - // clear the cache of related model objects |
|
5575 | - foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
5576 | - $obj_in_map->clear_cache($relation_name, null, true); |
|
5577 | - } |
|
5578 | - } |
|
5579 | - $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ] = $obj_in_map; |
|
5580 | - return $obj_in_map; |
|
5581 | - } |
|
5582 | - return $this->get_one_by_ID($id); |
|
5583 | - } |
|
5584 | - |
|
5585 | - |
|
5586 | - /** |
|
5587 | - * refresh_entity_map_with |
|
5588 | - * Leaves the entry in the entity map alone, but updates it to match the provided |
|
5589 | - * $replacing_model_obj (which we assume to be its equivalent but somehow NOT in the entity map). |
|
5590 | - * This is useful if you have a model object you want to make authoritative over what's in the entity map currently. |
|
5591 | - * Note: The old $replacing_model_obj should now be destroyed as it's now un-authoritative |
|
5592 | - * |
|
5593 | - * @param int|string $id |
|
5594 | - * @param EE_Base_Class $replacing_model_obj |
|
5595 | - * @return EE_Base_Class |
|
5596 | - * @throws EE_Error |
|
5597 | - * @throws ReflectionException |
|
5598 | - */ |
|
5599 | - public function refresh_entity_map_with($id, $replacing_model_obj) |
|
5600 | - { |
|
5601 | - $obj_in_map = $this->get_from_entity_map($id); |
|
5602 | - if ($obj_in_map) { |
|
5603 | - if ($replacing_model_obj instanceof EE_Base_Class) { |
|
5604 | - foreach ($replacing_model_obj->model_field_array() as $field_name => $value) { |
|
5605 | - $obj_in_map->set($field_name, $value); |
|
5606 | - } |
|
5607 | - // make the model object in the entity map's cache match the $replacing_model_obj |
|
5608 | - foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
5609 | - $obj_in_map->clear_cache($relation_name, null, true); |
|
5610 | - foreach ($replacing_model_obj->get_all_from_cache($relation_name) as $cache_id => $cached_obj) { |
|
5611 | - $obj_in_map->cache($relation_name, $cached_obj, $cache_id); |
|
5612 | - } |
|
5613 | - } |
|
5614 | - } |
|
5615 | - return $obj_in_map; |
|
5616 | - } |
|
5617 | - $this->add_to_entity_map($replacing_model_obj); |
|
5618 | - return $replacing_model_obj; |
|
5619 | - } |
|
5620 | - |
|
5621 | - |
|
5622 | - /** |
|
5623 | - * Gets the EE class that corresponds to this model. Eg, for EEM_Answer that |
|
5624 | - * would be EE_Answer.To import that class, you'd just add ".class.php" to the name, like so |
|
5625 | - * require_once($this->_getClassName().".class.php"); |
|
5626 | - * |
|
5627 | - * @return string |
|
5628 | - */ |
|
5629 | - private function _get_class_name() |
|
5630 | - { |
|
5631 | - return "EE_" . $this->get_this_model_name(); |
|
5632 | - } |
|
5633 | - |
|
5634 | - |
|
5635 | - /** |
|
5636 | - * Get the name of the items this model represents, for the quantity specified. Eg, |
|
5637 | - * if $quantity==1, on EEM_Event, it would 'Event' (internationalized), otherwise |
|
5638 | - * it would be 'Events'. |
|
5639 | - * |
|
5640 | - * @param int $quantity |
|
5641 | - * @return string |
|
5642 | - */ |
|
5643 | - public function item_name($quantity = 1) |
|
5644 | - { |
|
5645 | - return (int) $quantity === 1 ? $this->singular_item : $this->plural_item; |
|
5646 | - } |
|
5647 | - |
|
5648 | - |
|
5649 | - /** |
|
5650 | - * Very handy general function to allow for plugins to extend any child of EE_TempBase. |
|
5651 | - * If a method is called on a child of EE_TempBase that doesn't exist, this function is called |
|
5652 | - * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
|
5653 | - * requiring a plugin to extend the EE_TempBase (which works fine is there's only 1 plugin, but when will that |
|
5654 | - * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
|
5655 | - * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
|
5656 | - * was called, and an array of the original arguments passed to the function. Whatever their callback function |
|
5657 | - * returns will be returned by this function. Example: in functions.php (or in a plugin): |
|
5658 | - * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
|
5659 | - * my_callback($previousReturnValue,EE_TempBase $object,$argsArray){ |
|
5660 | - * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
5661 | - * return $previousReturnValue.$returnString; |
|
5662 | - * } |
|
5663 | - * require('EEM_Answer.model.php'); |
|
5664 | - * $answer=EEM_Answer::instance(); |
|
5665 | - * echo $answer->my_callback('monkeys',100); |
|
5666 | - * //will output "you called my_callback! and passed args:monkeys,100" |
|
5667 | - * |
|
5668 | - * @param string $methodName name of method which was called on a child of EE_TempBase, but which |
|
5669 | - * @param array $args array of original arguments passed to the function |
|
5670 | - * @return mixed whatever the plugin which calls add_filter decides |
|
5671 | - * @throws EE_Error |
|
5672 | - */ |
|
5673 | - public function __call($methodName, $args) |
|
5674 | - { |
|
5675 | - $className = get_class($this); |
|
5676 | - $tagName = "FHEE__{$className}__{$methodName}"; |
|
5677 | - if (! has_filter($tagName)) { |
|
5678 | - throw new EE_Error( |
|
5679 | - sprintf( |
|
5680 | - esc_html__( |
|
5681 | - 'Method %1$s on model %2$s does not exist! You can create one with the following code in functions.php or in a plugin: %4$s function my_callback(%4$s \$previousReturnValue, EEM_Base \$object\ $argsArray=NULL ){%4$s /*function body*/%4$s return \$whatever;%4$s }%4$s add_filter( \'%3$s\', \'my_callback\', 10, 3 );', |
|
5682 | - 'event_espresso' |
|
5683 | - ), |
|
5684 | - $methodName, |
|
5685 | - $className, |
|
5686 | - $tagName, |
|
5687 | - '<br />' |
|
5688 | - ) |
|
5689 | - ); |
|
5690 | - } |
|
5691 | - return apply_filters($tagName, null, $this, $args); |
|
5692 | - } |
|
5693 | - |
|
5694 | - |
|
5695 | - /** |
|
5696 | - * Ensures $base_class_obj_or_id is of the EE_Base_Class child that corresponds ot this model. |
|
5697 | - * If not, assumes its an ID, and uses $this->get_one_by_ID() to get the EE_Base_Class. |
|
5698 | - * |
|
5699 | - * @param EE_Base_Class|string|int $base_class_obj_or_id either: |
|
5700 | - * the EE_Base_Class object that corresponds to this Model, |
|
5701 | - * the object's class name |
|
5702 | - * or object's ID |
|
5703 | - * @param boolean $ensure_is_in_db if set, we will also verify this model object |
|
5704 | - * exists in the database. If it does not, we add it |
|
5705 | - * @return EE_Base_Class |
|
5706 | - * @throws EE_Error |
|
5707 | - * @throws ReflectionException |
|
5708 | - */ |
|
5709 | - public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false) |
|
5710 | - { |
|
5711 | - $className = $this->_get_class_name(); |
|
5712 | - if ($base_class_obj_or_id instanceof $className) { |
|
5713 | - $model_object = $base_class_obj_or_id; |
|
5714 | - } else { |
|
5715 | - $primary_key_field = $this->get_primary_key_field(); |
|
5716 | - if ( |
|
5717 | - $primary_key_field instanceof EE_Primary_Key_Int_Field |
|
5718 | - && ( |
|
5719 | - is_int($base_class_obj_or_id) |
|
5720 | - || is_string($base_class_obj_or_id) |
|
5721 | - ) |
|
5722 | - ) { |
|
5723 | - // assume it's an ID. |
|
5724 | - // either a proper integer or a string representing an integer (eg "101" instead of 101) |
|
5725 | - $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
5726 | - } elseif ( |
|
5727 | - $primary_key_field instanceof EE_Primary_Key_String_Field |
|
5728 | - && is_string($base_class_obj_or_id) |
|
5729 | - ) { |
|
5730 | - // assume its a string representation of the object |
|
5731 | - $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
5732 | - } else { |
|
5733 | - throw new EE_Error( |
|
5734 | - sprintf( |
|
5735 | - esc_html__( |
|
5736 | - "'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
5737 | - 'event_espresso' |
|
5738 | - ), |
|
5739 | - $base_class_obj_or_id, |
|
5740 | - $this->_get_class_name(), |
|
5741 | - print_r($base_class_obj_or_id, true) |
|
5742 | - ) |
|
5743 | - ); |
|
5744 | - } |
|
5745 | - } |
|
5746 | - if ($ensure_is_in_db && $model_object->ID() !== null) { |
|
5747 | - $model_object->save(); |
|
5748 | - } |
|
5749 | - return $model_object; |
|
5750 | - } |
|
5751 | - |
|
5752 | - |
|
5753 | - /** |
|
5754 | - * Similar to ensure_is_obj(), this method makes sure $base_class_obj_or_id |
|
5755 | - * is a value of the this model's primary key. If it's an EE_Base_Class child, |
|
5756 | - * returns it ID. |
|
5757 | - * |
|
5758 | - * @param EE_Base_Class|int|string $base_class_obj_or_id |
|
5759 | - * @return int|string depending on the type of this model object's ID |
|
5760 | - * @throws EE_Error |
|
5761 | - */ |
|
5762 | - public function ensure_is_ID($base_class_obj_or_id) |
|
5763 | - { |
|
5764 | - $className = $this->_get_class_name(); |
|
5765 | - if ($base_class_obj_or_id instanceof $className) { |
|
5766 | - $id = $base_class_obj_or_id->ID(); |
|
5767 | - } elseif (is_int($base_class_obj_or_id)) { |
|
5768 | - // assume it's an ID |
|
5769 | - $id = $base_class_obj_or_id; |
|
5770 | - } elseif (is_string($base_class_obj_or_id)) { |
|
5771 | - // assume its a string representation of the object |
|
5772 | - $id = $base_class_obj_or_id; |
|
5773 | - } else { |
|
5774 | - throw new EE_Error( |
|
5775 | - sprintf( |
|
5776 | - esc_html__( |
|
5777 | - "'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
5778 | - 'event_espresso' |
|
5779 | - ), |
|
5780 | - $base_class_obj_or_id, |
|
5781 | - $this->_get_class_name(), |
|
5782 | - print_r($base_class_obj_or_id, true) |
|
5783 | - ) |
|
5784 | - ); |
|
5785 | - } |
|
5786 | - return $id; |
|
5787 | - } |
|
5788 | - |
|
5789 | - |
|
5790 | - /** |
|
5791 | - * Sets whether the values passed to the model (eg, values in WHERE, values in INSERT, UPDATE, etc) |
|
5792 | - * have already been ran through the appropriate model field's prepare_for_use_in_db method. IE, they have |
|
5793 | - * been sanitized and converted into the appropriate domain. |
|
5794 | - * Usually the only place you'll want to change the default (which is to assume values have NOT been sanitized by |
|
5795 | - * the model object/model field) is when making a method call from WITHIN a model object, which has direct access |
|
5796 | - * to its sanitized values. Note: after changing this setting, you should set it back to its previous value (using |
|
5797 | - * get_assumption_concerning_values_already_prepared_by_model_object()) eg. |
|
5798 | - * $EVT = EEM_Event::instance(); $old_setting = |
|
5799 | - * $EVT->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
5800 | - * $EVT->assume_values_already_prepared_by_model_object(true); |
|
5801 | - * $EVT->update(array('foo'=>'bar'),array(array('foo'=>'monkey'))); |
|
5802 | - * $EVT->assume_values_already_prepared_by_model_object($old_setting); |
|
5803 | - * |
|
5804 | - * @param int $values_already_prepared like one of the constants on EEM_Base |
|
5805 | - * @return void |
|
5806 | - */ |
|
5807 | - public function assume_values_already_prepared_by_model_object( |
|
5808 | - $values_already_prepared = self::not_prepared_by_model_object |
|
5809 | - ) { |
|
5810 | - $this->_values_already_prepared_by_model_object = $values_already_prepared; |
|
5811 | - } |
|
5812 | - |
|
5813 | - |
|
5814 | - /** |
|
5815 | - * Read comments for assume_values_already_prepared_by_model_object() |
|
5816 | - * |
|
5817 | - * @return int |
|
5818 | - */ |
|
5819 | - public function get_assumption_concerning_values_already_prepared_by_model_object() |
|
5820 | - { |
|
5821 | - return $this->_values_already_prepared_by_model_object; |
|
5822 | - } |
|
5823 | - |
|
5824 | - |
|
5825 | - /** |
|
5826 | - * Gets all the indexes on this model |
|
5827 | - * |
|
5828 | - * @return EE_Index[] |
|
5829 | - */ |
|
5830 | - public function indexes() |
|
5831 | - { |
|
5832 | - return $this->_indexes; |
|
5833 | - } |
|
5834 | - |
|
5835 | - |
|
5836 | - /** |
|
5837 | - * Gets all the Unique Indexes on this model |
|
5838 | - * |
|
5839 | - * @return EE_Unique_Index[] |
|
5840 | - */ |
|
5841 | - public function unique_indexes() |
|
5842 | - { |
|
5843 | - $unique_indexes = []; |
|
5844 | - foreach ($this->_indexes as $name => $index) { |
|
5845 | - if ($index instanceof EE_Unique_Index) { |
|
5846 | - $unique_indexes [ $name ] = $index; |
|
5847 | - } |
|
5848 | - } |
|
5849 | - return $unique_indexes; |
|
5850 | - } |
|
5851 | - |
|
5852 | - |
|
5853 | - /** |
|
5854 | - * Gets all the fields which, when combined, make the primary key. |
|
5855 | - * This is usually just an array with 1 element (the primary key), but in cases |
|
5856 | - * where there is no primary key, it's a combination of fields as defined |
|
5857 | - * on a primary index |
|
5858 | - * |
|
5859 | - * @return EE_Model_Field_Base[] indexed by the field's name |
|
5860 | - * @throws EE_Error |
|
5861 | - */ |
|
5862 | - public function get_combined_primary_key_fields() |
|
5863 | - { |
|
5864 | - foreach ($this->indexes() as $index) { |
|
5865 | - if ($index instanceof EE_Primary_Key_Index) { |
|
5866 | - return $index->fields(); |
|
5867 | - } |
|
5868 | - } |
|
5869 | - return [$this->primary_key_name() => $this->get_primary_key_field()]; |
|
5870 | - } |
|
5871 | - |
|
5872 | - |
|
5873 | - /** |
|
5874 | - * Used to build a primary key string (when the model has no primary key), |
|
5875 | - * which can be used a unique string to identify this model object. |
|
5876 | - * |
|
5877 | - * @param array $fields_n_values keys are field names, values are their values. |
|
5878 | - * Note: if you have results from `EEM_Base::get_all_wpdb_results()`, you need to |
|
5879 | - * run it through `EEM_Base::deduce_fields_n_values_from_cols_n_values()` |
|
5880 | - * before passing it to this function (that will convert it from columns-n-values |
|
5881 | - * to field-names-n-values). |
|
5882 | - * @return string |
|
5883 | - * @throws EE_Error |
|
5884 | - */ |
|
5885 | - public function get_index_primary_key_string($fields_n_values) |
|
5886 | - { |
|
5887 | - $cols_n_values_for_primary_key_index = array_intersect_key( |
|
5888 | - $fields_n_values, |
|
5889 | - $this->get_combined_primary_key_fields() |
|
5890 | - ); |
|
5891 | - return http_build_query($cols_n_values_for_primary_key_index); |
|
5892 | - } |
|
5893 | - |
|
5894 | - |
|
5895 | - /** |
|
5896 | - * Gets the field values from the primary key string |
|
5897 | - * |
|
5898 | - * @param string $index_primary_key_string |
|
5899 | - * @return null|array |
|
5900 | - * @throws EE_Error |
|
5901 | - * @see EEM_Base::get_combined_primary_key_fields() and EEM_Base::get_index_primary_key_string() |
|
5902 | - */ |
|
5903 | - public function parse_index_primary_key_string($index_primary_key_string) |
|
5904 | - { |
|
5905 | - $key_fields = $this->get_combined_primary_key_fields(); |
|
5906 | - // check all of them are in the $id |
|
5907 | - $key_values_in_combined_pk = []; |
|
5908 | - parse_str($index_primary_key_string, $key_values_in_combined_pk); |
|
5909 | - foreach ($key_fields as $key_field_name => $field_obj) { |
|
5910 | - if (! isset($key_values_in_combined_pk[ $key_field_name ])) { |
|
5911 | - return null; |
|
5912 | - } |
|
5913 | - } |
|
5914 | - return $key_values_in_combined_pk; |
|
5915 | - } |
|
5916 | - |
|
5917 | - |
|
5918 | - /** |
|
5919 | - * verifies that an array of key-value pairs for model fields has a key |
|
5920 | - * for each field comprising the primary key index |
|
5921 | - * |
|
5922 | - * @param array $key_values |
|
5923 | - * @return boolean |
|
5924 | - * @throws EE_Error |
|
5925 | - */ |
|
5926 | - public function has_all_combined_primary_key_fields($key_values) |
|
5927 | - { |
|
5928 | - $keys_it_should_have = array_keys($this->get_combined_primary_key_fields()); |
|
5929 | - foreach ($keys_it_should_have as $key) { |
|
5930 | - if (! isset($key_values[ $key ])) { |
|
5931 | - return false; |
|
5932 | - } |
|
5933 | - } |
|
5934 | - return true; |
|
5935 | - } |
|
5936 | - |
|
5937 | - |
|
5938 | - /** |
|
5939 | - * Finds all model objects in the DB that appear to be a copy of $model_object_or_attributes_array. |
|
5940 | - * We consider something to be a copy if all the attributes match (except the ID, of course). |
|
5941 | - * |
|
5942 | - * @param array|EE_Base_Class $model_object_or_attributes_array If its an array, it's field-value pairs |
|
5943 | - * @param array $query_params see github link below for more info |
|
5944 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
5945 | - * @throws EE_Error |
|
5946 | - * @throws ReflectionException |
|
5947 | - * @return EE_Base_Class[] Array keys are object IDs (if there is a primary key on the model. if not, numerically |
|
5948 | - * indexed) |
|
5949 | - */ |
|
5950 | - public function get_all_copies($model_object_or_attributes_array, $query_params = []) |
|
5951 | - { |
|
5952 | - if ($model_object_or_attributes_array instanceof EE_Base_Class) { |
|
5953 | - $attributes_array = $model_object_or_attributes_array->model_field_array(); |
|
5954 | - } elseif (is_array($model_object_or_attributes_array)) { |
|
5955 | - $attributes_array = $model_object_or_attributes_array; |
|
5956 | - } else { |
|
5957 | - throw new EE_Error( |
|
5958 | - sprintf( |
|
5959 | - esc_html__( |
|
5960 | - "get_all_copies should be provided with either a model object or an array of field-value-pairs, but was given %s", |
|
5961 | - "event_espresso" |
|
5962 | - ), |
|
5963 | - $model_object_or_attributes_array |
|
5964 | - ) |
|
5965 | - ); |
|
5966 | - } |
|
5967 | - // even copies obviously won't have the same ID, so remove the primary key |
|
5968 | - // from the WHERE conditions for finding copies (if there is a primary key, of course) |
|
5969 | - if ($this->has_primary_key_field() && isset($attributes_array[ $this->primary_key_name() ])) { |
|
5970 | - unset($attributes_array[ $this->primary_key_name() ]); |
|
5971 | - } |
|
5972 | - if (isset($query_params[0])) { |
|
5973 | - $query_params[0] = array_merge($attributes_array, $query_params); |
|
5974 | - } else { |
|
5975 | - $query_params[0] = $attributes_array; |
|
5976 | - } |
|
5977 | - return $this->get_all($query_params); |
|
5978 | - } |
|
5979 | - |
|
5980 | - |
|
5981 | - /** |
|
5982 | - * Gets the first copy we find. See get_all_copies for more details |
|
5983 | - * |
|
5984 | - * @param mixed EE_Base_Class | array $model_object_or_attributes_array |
|
5985 | - * @param array $query_params |
|
5986 | - * @return EE_Base_Class |
|
5987 | - * @throws EE_Error |
|
5988 | - * @throws ReflectionException |
|
5989 | - */ |
|
5990 | - public function get_one_copy($model_object_or_attributes_array, $query_params = []) |
|
5991 | - { |
|
5992 | - if (! is_array($query_params)) { |
|
5993 | - EE_Error::doing_it_wrong( |
|
5994 | - 'EEM_Base::get_one_copy', |
|
5995 | - sprintf( |
|
5996 | - esc_html__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
5997 | - gettype($query_params) |
|
5998 | - ), |
|
5999 | - '4.6.0' |
|
6000 | - ); |
|
6001 | - $query_params = []; |
|
6002 | - } |
|
6003 | - $query_params['limit'] = 1; |
|
6004 | - $copies = $this->get_all_copies($model_object_or_attributes_array, $query_params); |
|
6005 | - if (is_array($copies)) { |
|
6006 | - return array_shift($copies); |
|
6007 | - } |
|
6008 | - return null; |
|
6009 | - } |
|
6010 | - |
|
6011 | - |
|
6012 | - /** |
|
6013 | - * Updates the item with the specified id. Ignores default query parameters because |
|
6014 | - * we have specified the ID, and its assumed we KNOW what we're doing |
|
6015 | - * |
|
6016 | - * @param array $fields_n_values keys are field names, values are their new values |
|
6017 | - * @param int|string $id the value of the primary key to update |
|
6018 | - * @return int number of rows updated |
|
6019 | - * @throws EE_Error |
|
6020 | - * @throws ReflectionException |
|
6021 | - */ |
|
6022 | - public function update_by_ID($fields_n_values, $id) |
|
6023 | - { |
|
6024 | - $query_params = [ |
|
6025 | - 0 => [$this->get_primary_key_field()->get_name() => $id], |
|
6026 | - 'default_where_conditions' => EEM_Base::default_where_conditions_others_only, |
|
6027 | - ]; |
|
6028 | - return $this->update($fields_n_values, $query_params); |
|
6029 | - } |
|
6030 | - |
|
6031 | - |
|
6032 | - /** |
|
6033 | - * Changes an operator which was supplied to the models into one usable in SQL |
|
6034 | - * |
|
6035 | - * @param string $operator_supplied |
|
6036 | - * @return string an operator which can be used in SQL |
|
6037 | - * @throws EE_Error |
|
6038 | - */ |
|
6039 | - private function _prepare_operator_for_sql($operator_supplied) |
|
6040 | - { |
|
6041 | - $sql_operator = |
|
6042 | - isset($this->_valid_operators[ $operator_supplied ]) ? $this->_valid_operators[ $operator_supplied ] |
|
6043 | - : null; |
|
6044 | - if ($sql_operator) { |
|
6045 | - return $sql_operator; |
|
6046 | - } |
|
6047 | - throw new EE_Error( |
|
6048 | - sprintf( |
|
6049 | - esc_html__( |
|
6050 | - "The operator '%s' is not in the list of valid operators: %s", |
|
6051 | - "event_espresso" |
|
6052 | - ), |
|
6053 | - $operator_supplied, |
|
6054 | - implode(",", array_keys($this->_valid_operators)) |
|
6055 | - ) |
|
6056 | - ); |
|
6057 | - } |
|
6058 | - |
|
6059 | - |
|
6060 | - /** |
|
6061 | - * Gets the valid operators |
|
6062 | - * |
|
6063 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
6064 | - */ |
|
6065 | - public function valid_operators() |
|
6066 | - { |
|
6067 | - return $this->_valid_operators; |
|
6068 | - } |
|
6069 | - |
|
6070 | - |
|
6071 | - /** |
|
6072 | - * Gets the between-style operators (take 2 arguments). |
|
6073 | - * |
|
6074 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
6075 | - */ |
|
6076 | - public function valid_between_style_operators() |
|
6077 | - { |
|
6078 | - return array_intersect( |
|
6079 | - $this->valid_operators(), |
|
6080 | - $this->_between_style_operators |
|
6081 | - ); |
|
6082 | - } |
|
6083 | - |
|
6084 | - |
|
6085 | - /** |
|
6086 | - * Gets the "like"-style operators (take a single argument, but it may contain wildcards) |
|
6087 | - * |
|
6088 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
6089 | - */ |
|
6090 | - public function valid_like_style_operators() |
|
6091 | - { |
|
6092 | - return array_intersect( |
|
6093 | - $this->valid_operators(), |
|
6094 | - $this->_like_style_operators |
|
6095 | - ); |
|
6096 | - } |
|
6097 | - |
|
6098 | - |
|
6099 | - /** |
|
6100 | - * Gets the "in"-style operators |
|
6101 | - * |
|
6102 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
6103 | - */ |
|
6104 | - public function valid_in_style_operators() |
|
6105 | - { |
|
6106 | - return array_intersect( |
|
6107 | - $this->valid_operators(), |
|
6108 | - $this->_in_style_operators |
|
6109 | - ); |
|
6110 | - } |
|
6111 | - |
|
6112 | - |
|
6113 | - /** |
|
6114 | - * Gets the "null"-style operators (accept no arguments) |
|
6115 | - * |
|
6116 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
6117 | - */ |
|
6118 | - public function valid_null_style_operators() |
|
6119 | - { |
|
6120 | - return array_intersect( |
|
6121 | - $this->valid_operators(), |
|
6122 | - $this->_null_style_operators |
|
6123 | - ); |
|
6124 | - } |
|
6125 | - |
|
6126 | - |
|
6127 | - /** |
|
6128 | - * Gets an array where keys are the primary keys and values are their 'names' |
|
6129 | - * (as determined by the model object's name() function, which is often overridden) |
|
6130 | - * |
|
6131 | - * @param array $query_params like get_all's |
|
6132 | - * @return string[] |
|
6133 | - * @throws EE_Error |
|
6134 | - * @throws ReflectionException |
|
6135 | - */ |
|
6136 | - public function get_all_names($query_params = []) |
|
6137 | - { |
|
6138 | - $objs = $this->get_all($query_params); |
|
6139 | - $names = []; |
|
6140 | - foreach ($objs as $obj) { |
|
6141 | - $names[ $obj->ID() ] = $obj->name(); |
|
6142 | - } |
|
6143 | - return $names; |
|
6144 | - } |
|
6145 | - |
|
6146 | - |
|
6147 | - /** |
|
6148 | - * Gets an array of primary keys from the model objects. If you acquired the model objects |
|
6149 | - * using EEM_Base::get_all() you don't need to call this (and probably shouldn't because |
|
6150 | - * this is duplicated effort and reduces efficiency) you would be better to use |
|
6151 | - * array_keys() on $model_objects. |
|
6152 | - * |
|
6153 | - * @param EE_Base_Class[] $model_objects |
|
6154 | - * @param boolean $filter_out_empty_ids if a model object has an ID of '' or 0, don't bother including it |
|
6155 | - * in the returned array |
|
6156 | - * @return array |
|
6157 | - * @throws EE_Error |
|
6158 | - */ |
|
6159 | - public function get_IDs($model_objects, $filter_out_empty_ids = false) |
|
6160 | - { |
|
6161 | - if (! $this->has_primary_key_field()) { |
|
6162 | - if (WP_DEBUG) { |
|
6163 | - EE_Error::add_error( |
|
6164 | - esc_html__('Trying to get IDs from a model than has no primary key', 'event_espresso'), |
|
6165 | - __FILE__, |
|
6166 | - __FUNCTION__, |
|
6167 | - __LINE__ |
|
6168 | - ); |
|
6169 | - } |
|
6170 | - } |
|
6171 | - $IDs = []; |
|
6172 | - foreach ($model_objects as $model_object) { |
|
6173 | - $id = $model_object->ID(); |
|
6174 | - if (! $id) { |
|
6175 | - if ($filter_out_empty_ids) { |
|
6176 | - continue; |
|
6177 | - } |
|
6178 | - if (WP_DEBUG) { |
|
6179 | - EE_Error::add_error( |
|
6180 | - esc_html__( |
|
6181 | - 'Called %1$s on a model object that has no ID and so probably hasn\'t been saved to the database', |
|
6182 | - 'event_espresso' |
|
6183 | - ), |
|
6184 | - __FILE__, |
|
6185 | - __FUNCTION__, |
|
6186 | - __LINE__ |
|
6187 | - ); |
|
6188 | - } |
|
6189 | - } |
|
6190 | - $IDs[] = $id; |
|
6191 | - } |
|
6192 | - return $IDs; |
|
6193 | - } |
|
6194 | - |
|
6195 | - |
|
6196 | - /** |
|
6197 | - * Returns the string used in capabilities relating to this model. If there |
|
6198 | - * are no capabilities that relate to this model returns false |
|
6199 | - * |
|
6200 | - * @return string|false |
|
6201 | - */ |
|
6202 | - public function cap_slug() |
|
6203 | - { |
|
6204 | - return apply_filters('FHEE__EEM_Base__cap_slug', $this->_caps_slug, $this); |
|
6205 | - } |
|
6206 | - |
|
6207 | - |
|
6208 | - /** |
|
6209 | - * Returns the capability-restrictions array (@param string $context |
|
6210 | - * |
|
6211 | - * @return EE_Default_Where_Conditions[] indexed by associated capability |
|
6212 | - * @throws EE_Error |
|
6213 | - * @see EEM_Base::_cap_restrictions). |
|
6214 | - * If $context is provided (which should be set to one of EEM_Base::valid_cap_contexts()) |
|
6215 | - * only returns the cap restrictions array in that context (ie, the array |
|
6216 | - * at that key) |
|
6217 | - * |
|
6218 | - */ |
|
6219 | - public function cap_restrictions($context = EEM_Base::caps_read) |
|
6220 | - { |
|
6221 | - EEM_Base::verify_is_valid_cap_context($context); |
|
6222 | - // check if we ought to run the restriction generator first |
|
6223 | - if ( |
|
6224 | - isset($this->_cap_restriction_generators[ $context ]) |
|
6225 | - && $this->_cap_restriction_generators[ $context ] instanceof EE_Restriction_Generator_Base |
|
6226 | - && ! $this->_cap_restriction_generators[ $context ]->has_generated_cap_restrictions() |
|
6227 | - ) { |
|
6228 | - $this->_cap_restrictions[ $context ] = array_merge( |
|
6229 | - $this->_cap_restrictions[ $context ], |
|
6230 | - $this->_cap_restriction_generators[ $context ]->generate_restrictions() |
|
6231 | - ); |
|
6232 | - } |
|
6233 | - // and make sure we've finalized the construction of each restriction |
|
6234 | - foreach ($this->_cap_restrictions[ $context ] as $where_conditions_obj) { |
|
6235 | - if ($where_conditions_obj instanceof EE_Default_Where_Conditions) { |
|
6236 | - $where_conditions_obj->_finalize_construct($this); |
|
6237 | - } |
|
6238 | - } |
|
6239 | - return $this->_cap_restrictions[ $context ]; |
|
6240 | - } |
|
6241 | - |
|
6242 | - |
|
6243 | - /** |
|
6244 | - * Indicating whether or not this model thinks its a wp core model |
|
6245 | - * |
|
6246 | - * @return boolean |
|
6247 | - */ |
|
6248 | - public function is_wp_core_model() |
|
6249 | - { |
|
6250 | - return $this->_wp_core_model; |
|
6251 | - } |
|
6252 | - |
|
6253 | - |
|
6254 | - /** |
|
6255 | - * Gets all the caps that are missing which impose a restriction on |
|
6256 | - * queries made in this context |
|
6257 | - * |
|
6258 | - * @param string $context one of EEM_Base::caps_ constants |
|
6259 | - * @return EE_Default_Where_Conditions[] indexed by capability name |
|
6260 | - * @throws EE_Error |
|
6261 | - */ |
|
6262 | - public function caps_missing($context = EEM_Base::caps_read) |
|
6263 | - { |
|
6264 | - $missing_caps = []; |
|
6265 | - $cap_restrictions = $this->cap_restrictions($context); |
|
6266 | - foreach ($cap_restrictions as $cap => $restriction_if_no_cap) { |
|
6267 | - if ( |
|
6268 | - ! EE_Capabilities::instance() |
|
6269 | - ->current_user_can($cap, $this->get_this_model_name() . '_model_applying_caps') |
|
6270 | - ) { |
|
6271 | - $missing_caps[ $cap ] = $restriction_if_no_cap; |
|
6272 | - } |
|
6273 | - } |
|
6274 | - return $missing_caps; |
|
6275 | - } |
|
6276 | - |
|
6277 | - |
|
6278 | - /** |
|
6279 | - * Gets the mapping from capability contexts to action strings used in capability names |
|
6280 | - * |
|
6281 | - * @return array keys are one of EEM_Base::valid_cap_contexts(), and values are usually |
|
6282 | - * one of 'read', 'edit', or 'delete' |
|
6283 | - */ |
|
6284 | - public function cap_contexts_to_cap_action_map() |
|
6285 | - { |
|
6286 | - return apply_filters( |
|
6287 | - 'FHEE__EEM_Base__cap_contexts_to_cap_action_map', |
|
6288 | - $this->_cap_contexts_to_cap_action_map, |
|
6289 | - $this |
|
6290 | - ); |
|
6291 | - } |
|
6292 | - |
|
6293 | - |
|
6294 | - /** |
|
6295 | - * Gets the action string for the specified capability context |
|
6296 | - * |
|
6297 | - * @param string $context |
|
6298 | - * @return string one of EEM_Base::cap_contexts_to_cap_action_map() values |
|
6299 | - * @throws EE_Error |
|
6300 | - */ |
|
6301 | - public function cap_action_for_context($context) |
|
6302 | - { |
|
6303 | - $mapping = $this->cap_contexts_to_cap_action_map(); |
|
6304 | - if (isset($mapping[ $context ])) { |
|
6305 | - return $mapping[ $context ]; |
|
6306 | - } |
|
6307 | - if ($action = apply_filters('FHEE__EEM_Base__cap_action_for_context', null, $this, $mapping, $context)) { |
|
6308 | - return $action; |
|
6309 | - } |
|
6310 | - throw new EE_Error( |
|
6311 | - sprintf( |
|
6312 | - esc_html__( |
|
6313 | - 'Cannot find capability restrictions for context "%1$s", allowed values are:%2$s', |
|
6314 | - 'event_espresso' |
|
6315 | - ), |
|
6316 | - $context, |
|
6317 | - implode(',', array_keys($this->cap_contexts_to_cap_action_map())) |
|
6318 | - ) |
|
6319 | - ); |
|
6320 | - } |
|
6321 | - |
|
6322 | - |
|
6323 | - /** |
|
6324 | - * Returns all the capability contexts which are valid when querying models |
|
6325 | - * |
|
6326 | - * @return array |
|
6327 | - */ |
|
6328 | - public static function valid_cap_contexts() |
|
6329 | - { |
|
6330 | - return apply_filters( |
|
6331 | - 'FHEE__EEM_Base__valid_cap_contexts', |
|
6332 | - [ |
|
6333 | - self::caps_read, |
|
6334 | - self::caps_read_admin, |
|
6335 | - self::caps_edit, |
|
6336 | - self::caps_delete, |
|
6337 | - ] |
|
6338 | - ); |
|
6339 | - } |
|
6340 | - |
|
6341 | - |
|
6342 | - /** |
|
6343 | - * Returns all valid options for 'default_where_conditions' |
|
6344 | - * |
|
6345 | - * @return array |
|
6346 | - */ |
|
6347 | - public static function valid_default_where_conditions() |
|
6348 | - { |
|
6349 | - return [ |
|
6350 | - EEM_Base::default_where_conditions_all, |
|
6351 | - EEM_Base::default_where_conditions_this_only, |
|
6352 | - EEM_Base::default_where_conditions_others_only, |
|
6353 | - EEM_Base::default_where_conditions_minimum_all, |
|
6354 | - EEM_Base::default_where_conditions_minimum_others, |
|
6355 | - EEM_Base::default_where_conditions_none, |
|
6356 | - ]; |
|
6357 | - } |
|
6358 | - |
|
6359 | - // public static function default_where_conditions_full |
|
6360 | - |
|
6361 | - |
|
6362 | - /** |
|
6363 | - * Verifies $context is one of EEM_Base::valid_cap_contexts(), if not it throws an exception |
|
6364 | - * |
|
6365 | - * @param string $context |
|
6366 | - * @return bool |
|
6367 | - * @throws EE_Error |
|
6368 | - */ |
|
6369 | - public static function verify_is_valid_cap_context($context) |
|
6370 | - { |
|
6371 | - $valid_cap_contexts = EEM_Base::valid_cap_contexts(); |
|
6372 | - if (in_array($context, $valid_cap_contexts)) { |
|
6373 | - return true; |
|
6374 | - } |
|
6375 | - throw new EE_Error( |
|
6376 | - sprintf( |
|
6377 | - esc_html__( |
|
6378 | - 'Context "%1$s" passed into model "%2$s" is not a valid context. They are: %3$s', |
|
6379 | - 'event_espresso' |
|
6380 | - ), |
|
6381 | - $context, |
|
6382 | - 'EEM_Base', |
|
6383 | - implode(',', $valid_cap_contexts) |
|
6384 | - ) |
|
6385 | - ); |
|
6386 | - } |
|
6387 | - |
|
6388 | - |
|
6389 | - /** |
|
6390 | - * Clears all the models field caches. This is only useful when a sub-class |
|
6391 | - * might have added a field or something and these caches might be invalidated |
|
6392 | - */ |
|
6393 | - protected function _invalidate_field_caches() |
|
6394 | - { |
|
6395 | - $this->_cache_foreign_key_to_fields = []; |
|
6396 | - $this->_cached_fields = null; |
|
6397 | - $this->_cached_fields_non_db_only = null; |
|
6398 | - } |
|
6399 | - |
|
6400 | - |
|
6401 | - /** |
|
6402 | - * Gets the list of all the where query param keys that relate to logic instead of field names |
|
6403 | - * (eg "and", "or", "not"). |
|
6404 | - * |
|
6405 | - * @return array |
|
6406 | - */ |
|
6407 | - public function logic_query_param_keys() |
|
6408 | - { |
|
6409 | - return $this->_logic_query_param_keys; |
|
6410 | - } |
|
6411 | - |
|
6412 | - |
|
6413 | - /** |
|
6414 | - * Determines whether or not the where query param array key is for a logic query param. |
|
6415 | - * Eg 'OR', 'not*', and 'and*because-i-say-so' should all return true, whereas |
|
6416 | - * 'ATT_fname', 'EVT_name*not-you-or-me', and 'ORG_name' should return false |
|
6417 | - * |
|
6418 | - * @param $query_param_key |
|
6419 | - * @return bool |
|
6420 | - */ |
|
6421 | - public function is_logic_query_param_key($query_param_key) |
|
6422 | - { |
|
6423 | - foreach ($this->logic_query_param_keys() as $logic_query_param_key) { |
|
6424 | - if ( |
|
6425 | - $query_param_key === $logic_query_param_key |
|
6426 | - || strpos($query_param_key, $logic_query_param_key . '*') === 0 |
|
6427 | - ) { |
|
6428 | - return true; |
|
6429 | - } |
|
6430 | - } |
|
6431 | - return false; |
|
6432 | - } |
|
6433 | - |
|
6434 | - |
|
6435 | - /** |
|
6436 | - * Returns true if this model has a password field on it (regardless of whether that password field has any content) |
|
6437 | - * |
|
6438 | - * @return boolean |
|
6439 | - * @since 4.9.74.p |
|
6440 | - */ |
|
6441 | - public function hasPassword() |
|
6442 | - { |
|
6443 | - // if we don't yet know if there's a password field, find out and remember it for next time. |
|
6444 | - if ($this->has_password_field === null) { |
|
6445 | - $password_field = $this->getPasswordField(); |
|
6446 | - $this->has_password_field = $password_field instanceof EE_Password_Field; |
|
6447 | - } |
|
6448 | - return $this->has_password_field; |
|
6449 | - } |
|
6450 | - |
|
6451 | - |
|
6452 | - /** |
|
6453 | - * Returns the password field on this model, if there is one |
|
6454 | - * |
|
6455 | - * @return EE_Password_Field|null |
|
6456 | - * @since 4.9.74.p |
|
6457 | - */ |
|
6458 | - public function getPasswordField() |
|
6459 | - { |
|
6460 | - // if we definitely already know there is a password field or not (because has_password_field is true or false) |
|
6461 | - // there's no need to search for it. If we don't know yet, then find out |
|
6462 | - if ($this->has_password_field === null && $this->password_field === null) { |
|
6463 | - $this->password_field = $this->get_a_field_of_type('EE_Password_Field'); |
|
6464 | - } |
|
6465 | - // don't bother setting has_password_field because that's hasPassword()'s job. |
|
6466 | - return $this->password_field; |
|
6467 | - } |
|
6468 | - |
|
6469 | - |
|
6470 | - /** |
|
6471 | - * Returns the list of field (as EE_Model_Field_Bases) that are protected by the password |
|
6472 | - * |
|
6473 | - * @return EE_Model_Field_Base[] |
|
6474 | - * @throws EE_Error |
|
6475 | - * @since 4.9.74.p |
|
6476 | - */ |
|
6477 | - public function getPasswordProtectedFields() |
|
6478 | - { |
|
6479 | - $password_field = $this->getPasswordField(); |
|
6480 | - $fields = []; |
|
6481 | - if ($password_field instanceof EE_Password_Field) { |
|
6482 | - $field_names = $password_field->protectedFields(); |
|
6483 | - foreach ($field_names as $field_name) { |
|
6484 | - $fields[ $field_name ] = $this->field_settings_for($field_name); |
|
6485 | - } |
|
6486 | - } |
|
6487 | - return $fields; |
|
6488 | - } |
|
6489 | - |
|
6490 | - |
|
6491 | - /** |
|
6492 | - * Checks if the current user can perform the requested action on this model |
|
6493 | - * |
|
6494 | - * @param string $cap_to_check one of the array keys from _cap_contexts_to_cap_action_map |
|
6495 | - * @param EE_Base_Class|array $model_obj_or_fields_n_values |
|
6496 | - * @return bool |
|
6497 | - * @throws EE_Error |
|
6498 | - * @throws InvalidArgumentException |
|
6499 | - * @throws InvalidDataTypeException |
|
6500 | - * @throws InvalidInterfaceException |
|
6501 | - * @throws ReflectionException |
|
6502 | - * @throws UnexpectedEntityException |
|
6503 | - * @since 4.9.74.p |
|
6504 | - */ |
|
6505 | - public function currentUserCan($cap_to_check, $model_obj_or_fields_n_values) |
|
6506 | - { |
|
6507 | - if ($model_obj_or_fields_n_values instanceof EE_Base_Class) { |
|
6508 | - $model_obj_or_fields_n_values = $model_obj_or_fields_n_values->model_field_array(); |
|
6509 | - } |
|
6510 | - if (! is_array($model_obj_or_fields_n_values)) { |
|
6511 | - throw new UnexpectedEntityException( |
|
6512 | - $model_obj_or_fields_n_values, |
|
6513 | - 'EE_Base_Class', |
|
6514 | - sprintf( |
|
6515 | - esc_html__( |
|
6516 | - '%1$s must be passed an `EE_Base_Class or an array of fields names with their values. You passed in something different.', |
|
6517 | - 'event_espresso' |
|
6518 | - ), |
|
6519 | - __FUNCTION__ |
|
6520 | - ) |
|
6521 | - ); |
|
6522 | - } |
|
6523 | - return $this->exists( |
|
6524 | - $this->alter_query_params_to_restrict_by_ID( |
|
6525 | - $this->get_index_primary_key_string($model_obj_or_fields_n_values), |
|
6526 | - [ |
|
6527 | - 'default_where_conditions' => 'none', |
|
6528 | - 'caps' => $cap_to_check, |
|
6529 | - ] |
|
6530 | - ) |
|
6531 | - ); |
|
6532 | - } |
|
6533 | - |
|
6534 | - |
|
6535 | - /** |
|
6536 | - * Returns the query param where conditions key to the password affecting this model. |
|
6537 | - * Eg on EEM_Event this would just be "password", on EEM_Datetime this would be "Event.password", etc. |
|
6538 | - * |
|
6539 | - * @return null|string |
|
6540 | - * @throws EE_Error |
|
6541 | - * @throws InvalidArgumentException |
|
6542 | - * @throws InvalidDataTypeException |
|
6543 | - * @throws InvalidInterfaceException |
|
6544 | - * @throws ModelConfigurationException |
|
6545 | - * @throws ReflectionException |
|
6546 | - * @since 4.9.74.p |
|
6547 | - */ |
|
6548 | - public function modelChainAndPassword() |
|
6549 | - { |
|
6550 | - if ($this->model_chain_to_password === null) { |
|
6551 | - throw new ModelConfigurationException( |
|
6552 | - $this, |
|
6553 | - esc_html_x( |
|
6554 | - // @codingStandardsIgnoreStart |
|
6555 | - 'Cannot exclude protected data because the model has not specified which model has the password.', |
|
6556 | - // @codingStandardsIgnoreEnd |
|
6557 | - '1: model name', |
|
6558 | - 'event_espresso' |
|
6559 | - ) |
|
6560 | - ); |
|
6561 | - } |
|
6562 | - if ($this->model_chain_to_password === '') { |
|
6563 | - $model_with_password = $this; |
|
6564 | - } else { |
|
6565 | - if ($pos_of_period = strrpos($this->model_chain_to_password, '.')) { |
|
6566 | - $last_model_in_chain = substr($this->model_chain_to_password, $pos_of_period + 1); |
|
6567 | - } else { |
|
6568 | - $last_model_in_chain = $this->model_chain_to_password; |
|
6569 | - } |
|
6570 | - $model_with_password = EE_Registry::instance()->load_model($last_model_in_chain); |
|
6571 | - } |
|
6572 | - |
|
6573 | - $password_field = $model_with_password->getPasswordField(); |
|
6574 | - if ($password_field instanceof EE_Password_Field) { |
|
6575 | - $password_field_name = $password_field->get_name(); |
|
6576 | - } else { |
|
6577 | - throw new ModelConfigurationException( |
|
6578 | - $this, |
|
6579 | - sprintf( |
|
6580 | - esc_html_x( |
|
6581 | - 'This model claims related model "%1$s" should have a password field on it, but none was found. The model relation chain is "%2$s"', |
|
6582 | - '1: model name, 2: special string', |
|
6583 | - 'event_espresso' |
|
6584 | - ), |
|
6585 | - $model_with_password->get_this_model_name(), |
|
6586 | - $this->model_chain_to_password |
|
6587 | - ) |
|
6588 | - ); |
|
6589 | - } |
|
6590 | - return ($this->model_chain_to_password ? $this->model_chain_to_password . '.' : '') . $password_field_name; |
|
6591 | - } |
|
6592 | - |
|
6593 | - |
|
6594 | - /** |
|
6595 | - * Returns true if there is a password on a related model which restricts access to some of this model's rows, |
|
6596 | - * or if this model itself has a password affecting access to some of its other fields. |
|
6597 | - * |
|
6598 | - * @return boolean |
|
6599 | - * @since 4.9.74.p |
|
6600 | - */ |
|
6601 | - public function restrictedByRelatedModelPassword() |
|
6602 | - { |
|
6603 | - return $this->model_chain_to_password !== null; |
|
6604 | - } |
|
3873 | + } |
|
3874 | + return $null_friendly_where_conditions; |
|
3875 | + } |
|
3876 | + |
|
3877 | + |
|
3878 | + /** |
|
3879 | + * Uses the _default_where_conditions_strategy set during __construct() to get |
|
3880 | + * default where conditions on all get_all, update, and delete queries done by this model. |
|
3881 | + * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
3882 | + * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
3883 | + * |
|
3884 | + * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
3885 | + * @return array |
|
3886 | + * @throws EE_Error |
|
3887 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
3888 | + */ |
|
3889 | + private function _get_default_where_conditions($model_relation_path = '') |
|
3890 | + { |
|
3891 | + if ($this->_ignore_where_strategy) { |
|
3892 | + return []; |
|
3893 | + } |
|
3894 | + return $this->_default_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
3895 | + } |
|
3896 | + |
|
3897 | + |
|
3898 | + /** |
|
3899 | + * Uses the _minimum_where_conditions_strategy set during __construct() to get |
|
3900 | + * minimum where conditions on all get_all, update, and delete queries done by this model. |
|
3901 | + * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
3902 | + * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
3903 | + * Similar to _get_default_where_conditions |
|
3904 | + * |
|
3905 | + * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
3906 | + * @return array |
|
3907 | + * @throws EE_Error |
|
3908 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
3909 | + */ |
|
3910 | + protected function _get_minimum_where_conditions($model_relation_path = '') |
|
3911 | + { |
|
3912 | + if ($this->_ignore_where_strategy) { |
|
3913 | + return []; |
|
3914 | + } |
|
3915 | + return $this->_minimum_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
3916 | + } |
|
3917 | + |
|
3918 | + |
|
3919 | + /** |
|
3920 | + * Creates the string of SQL for the select part of a select query, everything behind SELECT and before FROM. |
|
3921 | + * Eg, "Event.post_id, Event.post_name,Event_Detail.EVT_ID..." |
|
3922 | + * |
|
3923 | + * @param EE_Model_Query_Info_Carrier $model_query_info |
|
3924 | + * @return string |
|
3925 | + * @throws EE_Error |
|
3926 | + */ |
|
3927 | + private function _construct_default_select_sql(EE_Model_Query_Info_Carrier $model_query_info) |
|
3928 | + { |
|
3929 | + $selects = $this->_get_columns_to_select_for_this_model(); |
|
3930 | + foreach ( |
|
3931 | + $model_query_info->get_model_names_included() as $model_relation_chain => $name_of_other_model_included |
|
3932 | + ) { |
|
3933 | + $other_model_included = $this->get_related_model_obj($name_of_other_model_included); |
|
3934 | + $other_model_selects = $other_model_included->_get_columns_to_select_for_this_model($model_relation_chain); |
|
3935 | + foreach ($other_model_selects as $value) { |
|
3936 | + $selects[] = $value; |
|
3937 | + } |
|
3938 | + } |
|
3939 | + return implode(", ", $selects); |
|
3940 | + } |
|
3941 | + |
|
3942 | + |
|
3943 | + /** |
|
3944 | + * Gets an array of columns to select for this model, which are necessary for it to create its objects. |
|
3945 | + * So that's going to be the columns for all the fields on the model |
|
3946 | + * |
|
3947 | + * @param string $model_relation_chain like 'Question.Question_Group.Event' |
|
3948 | + * @return array numerically indexed, values are columns to select and rename, eg "Event.ID AS 'Event.ID'" |
|
3949 | + */ |
|
3950 | + public function _get_columns_to_select_for_this_model($model_relation_chain = '') |
|
3951 | + { |
|
3952 | + $fields = $this->field_settings(); |
|
3953 | + $selects = []; |
|
3954 | + $table_alias_with_model_relation_chain_prefix = |
|
3955 | + EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
3956 | + $model_relation_chain, |
|
3957 | + $this->get_this_model_name() |
|
3958 | + ); |
|
3959 | + foreach ($fields as $field_obj) { |
|
3960 | + $selects[] = $table_alias_with_model_relation_chain_prefix |
|
3961 | + . $field_obj->get_table_alias() |
|
3962 | + . "." |
|
3963 | + . $field_obj->get_table_column() |
|
3964 | + . " AS '" |
|
3965 | + . $table_alias_with_model_relation_chain_prefix |
|
3966 | + . $field_obj->get_table_alias() |
|
3967 | + . "." |
|
3968 | + . $field_obj->get_table_column() |
|
3969 | + . "'"; |
|
3970 | + } |
|
3971 | + // make sure we are also getting the PKs of each table |
|
3972 | + $tables = $this->get_tables(); |
|
3973 | + if (count($tables) > 1) { |
|
3974 | + foreach ($tables as $table_obj) { |
|
3975 | + $qualified_pk_column = $table_alias_with_model_relation_chain_prefix |
|
3976 | + . $table_obj->get_fully_qualified_pk_column(); |
|
3977 | + if (! in_array($qualified_pk_column, $selects)) { |
|
3978 | + $selects[] = "$qualified_pk_column AS '$qualified_pk_column'"; |
|
3979 | + } |
|
3980 | + } |
|
3981 | + } |
|
3982 | + return $selects; |
|
3983 | + } |
|
3984 | + |
|
3985 | + |
|
3986 | + /** |
|
3987 | + * Given a $query_param like 'Registration.Transaction.TXN_ID', pops off 'Registration.', |
|
3988 | + * gets the join statement for it; gets the data types for it; and passes the remaining 'Transaction.TXN_ID' |
|
3989 | + * onto its related Transaction object to do the same. Returns an EE_Join_And_Data_Types object which contains the |
|
3990 | + * SQL for joining, and the data types |
|
3991 | + * |
|
3992 | + * @param string $query_param like Registration.Transaction.TXN_ID |
|
3993 | + * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
3994 | + * @param string $query_param_type like Registration.Transaction.TXN_ID |
|
3995 | + * or 'PAY_ID'. Otherwise, we don't expect there to be a |
|
3996 | + * column name. We only want model names, eg 'Event.Venue' |
|
3997 | + * or 'Registration's |
|
3998 | + * @param string|null $original_query_param what it originally was (eg |
|
3999 | + * Registration.Transaction.TXN_ID). If null, we assume it |
|
4000 | + * matches $query_param |
|
4001 | + * @return void only modifies the EEM_Related_Model_Info_Carrier passed into it |
|
4002 | + * @throws EE_Error |
|
4003 | + */ |
|
4004 | + private function _extract_related_model_info_from_query_param( |
|
4005 | + $query_param, |
|
4006 | + EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
4007 | + $query_param_type, |
|
4008 | + $original_query_param = null |
|
4009 | + ) { |
|
4010 | + if ($original_query_param === null) { |
|
4011 | + $original_query_param = $query_param; |
|
4012 | + } |
|
4013 | + $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param); |
|
4014 | + // whether or not to allow logic_query_params like 'NOT','OR', or 'AND' |
|
4015 | + $allow_logic_query_params = in_array($query_param_type, ['where', 'having', 0, 'custom_selects'], true); |
|
4016 | + $allow_fields = in_array( |
|
4017 | + $query_param_type, |
|
4018 | + ['where', 'having', 'order_by', 'group_by', 'order', 'custom_selects', 0], |
|
4019 | + true |
|
4020 | + ); |
|
4021 | + // check to see if we have a field on this model |
|
4022 | + $this_model_fields = $this->field_settings(true); |
|
4023 | + if (array_key_exists($query_param, $this_model_fields)) { |
|
4024 | + if ($allow_fields) { |
|
4025 | + return; |
|
4026 | + } |
|
4027 | + throw new EE_Error( |
|
4028 | + sprintf( |
|
4029 | + esc_html__( |
|
4030 | + "Using a field name (%s) on model %s is not allowed on this query param type '%s'. Original query param was %s", |
|
4031 | + "event_espresso" |
|
4032 | + ), |
|
4033 | + $query_param, |
|
4034 | + get_class($this), |
|
4035 | + $query_param_type, |
|
4036 | + $original_query_param |
|
4037 | + ) |
|
4038 | + ); |
|
4039 | + } |
|
4040 | + // check if this is a special logic query param |
|
4041 | + if (in_array($query_param, $this->_logic_query_param_keys, true)) { |
|
4042 | + if ($allow_logic_query_params) { |
|
4043 | + return; |
|
4044 | + } |
|
4045 | + throw new EE_Error( |
|
4046 | + sprintf( |
|
4047 | + esc_html__( |
|
4048 | + 'Logic query params ("%1$s") are being used incorrectly with the following query param ("%2$s") on model %3$s. %4$sAdditional Info:%4$s%5$s', |
|
4049 | + 'event_espresso' |
|
4050 | + ), |
|
4051 | + implode('", "', $this->_logic_query_param_keys), |
|
4052 | + $query_param, |
|
4053 | + get_class($this), |
|
4054 | + '<br />', |
|
4055 | + "\t" |
|
4056 | + . ' $passed_in_query_info = <pre>' |
|
4057 | + . print_r($passed_in_query_info, true) |
|
4058 | + . '</pre>' |
|
4059 | + . "\n\t" |
|
4060 | + . ' $query_param_type = ' |
|
4061 | + . $query_param_type |
|
4062 | + . "\n\t" |
|
4063 | + . ' $original_query_param = ' |
|
4064 | + . $original_query_param |
|
4065 | + ) |
|
4066 | + ); |
|
4067 | + } |
|
4068 | + // check if it's a custom selection |
|
4069 | + if ( |
|
4070 | + $this->_custom_selections instanceof CustomSelects |
|
4071 | + && in_array($query_param, $this->_custom_selections->columnAliases(), true) |
|
4072 | + ) { |
|
4073 | + return; |
|
4074 | + } |
|
4075 | + // check if has a model name at the beginning |
|
4076 | + // and |
|
4077 | + // check if it's a field on a related model |
|
4078 | + if ( |
|
4079 | + $this->extractJoinModelFromQueryParams( |
|
4080 | + $passed_in_query_info, |
|
4081 | + $query_param, |
|
4082 | + $original_query_param, |
|
4083 | + $query_param_type |
|
4084 | + ) |
|
4085 | + ) { |
|
4086 | + return; |
|
4087 | + } |
|
4088 | + |
|
4089 | + // ok so $query_param didn't start with a model name |
|
4090 | + // and we previously confirmed it wasn't a logic query param or field on the current model |
|
4091 | + // it's wack, that's what it is |
|
4092 | + throw new EE_Error( |
|
4093 | + sprintf( |
|
4094 | + esc_html__( |
|
4095 | + "There is no model named '%s' related to %s. Query param type is %s and original query param is %s", |
|
4096 | + "event_espresso" |
|
4097 | + ), |
|
4098 | + $query_param, |
|
4099 | + get_class($this), |
|
4100 | + $query_param_type, |
|
4101 | + $original_query_param |
|
4102 | + ) |
|
4103 | + ); |
|
4104 | + } |
|
4105 | + |
|
4106 | + |
|
4107 | + /** |
|
4108 | + * Extracts any possible join model information from the provided possible_join_string. |
|
4109 | + * This method will read the provided $possible_join_string value and determine if there are any possible model |
|
4110 | + * join |
|
4111 | + * parts that should be added to the query. |
|
4112 | + * |
|
4113 | + * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
4114 | + * @param string $possible_join_string Such as Registration.REG_ID, or Registration |
|
4115 | + * @param null|string $original_query_param |
|
4116 | + * @param string $query_parameter_type The type for the source of the $possible_join_string |
|
4117 | + * ('where', 'order_by', 'group_by', 'custom_selects' |
|
4118 | + * etc.) |
|
4119 | + * @return bool returns true if a join was added and false if not. |
|
4120 | + * @throws EE_Error |
|
4121 | + */ |
|
4122 | + private function extractJoinModelFromQueryParams( |
|
4123 | + EE_Model_Query_Info_Carrier $query_info_carrier, |
|
4124 | + $possible_join_string, |
|
4125 | + $original_query_param, |
|
4126 | + $query_parameter_type |
|
4127 | + ) { |
|
4128 | + foreach ($this->_model_relations as $valid_related_model_name => $relation_obj) { |
|
4129 | + if (strpos($possible_join_string, $valid_related_model_name . ".") === 0) { |
|
4130 | + $this->_add_join_to_model($valid_related_model_name, $query_info_carrier, $original_query_param); |
|
4131 | + $possible_join_string = substr($possible_join_string, strlen($valid_related_model_name . ".")); |
|
4132 | + if ($possible_join_string === '') { |
|
4133 | + // nothing left to $query_param |
|
4134 | + // we should actually end in a field name, not a model like this! |
|
4135 | + throw new EE_Error( |
|
4136 | + sprintf( |
|
4137 | + esc_html__( |
|
4138 | + "Query param '%s' (of type %s on model %s) shouldn't end on a period (.) ", |
|
4139 | + "event_espresso" |
|
4140 | + ), |
|
4141 | + $possible_join_string, |
|
4142 | + $query_parameter_type, |
|
4143 | + get_class($this), |
|
4144 | + $valid_related_model_name |
|
4145 | + ) |
|
4146 | + ); |
|
4147 | + } |
|
4148 | + $related_model_obj = $this->get_related_model_obj($valid_related_model_name); |
|
4149 | + $related_model_obj->_extract_related_model_info_from_query_param( |
|
4150 | + $possible_join_string, |
|
4151 | + $query_info_carrier, |
|
4152 | + $query_parameter_type, |
|
4153 | + $original_query_param |
|
4154 | + ); |
|
4155 | + return true; |
|
4156 | + } |
|
4157 | + if ($possible_join_string === $valid_related_model_name) { |
|
4158 | + $this->_add_join_to_model( |
|
4159 | + $valid_related_model_name, |
|
4160 | + $query_info_carrier, |
|
4161 | + $original_query_param |
|
4162 | + ); |
|
4163 | + return true; |
|
4164 | + } |
|
4165 | + } |
|
4166 | + return false; |
|
4167 | + } |
|
4168 | + |
|
4169 | + |
|
4170 | + /** |
|
4171 | + * Extracts related models from Custom Selects and sets up any joins for those related models. |
|
4172 | + * |
|
4173 | + * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
4174 | + * @throws EE_Error |
|
4175 | + */ |
|
4176 | + private function extractRelatedModelsFromCustomSelects(EE_Model_Query_Info_Carrier $query_info_carrier) |
|
4177 | + { |
|
4178 | + if ( |
|
4179 | + $this->_custom_selections instanceof CustomSelects |
|
4180 | + && ($this->_custom_selections->type() === CustomSelects::TYPE_STRUCTURED |
|
4181 | + || $this->_custom_selections->type() == CustomSelects::TYPE_COMPLEX |
|
4182 | + ) |
|
4183 | + ) { |
|
4184 | + $original_selects = $this->_custom_selections->originalSelects(); |
|
4185 | + foreach ($original_selects as $select_configuration) { |
|
4186 | + $this->extractJoinModelFromQueryParams( |
|
4187 | + $query_info_carrier, |
|
4188 | + $select_configuration[0], |
|
4189 | + $select_configuration[0], |
|
4190 | + 'custom_selects' |
|
4191 | + ); |
|
4192 | + } |
|
4193 | + } |
|
4194 | + } |
|
4195 | + |
|
4196 | + |
|
4197 | + /** |
|
4198 | + * Privately used by _extract_related_model_info_from_query_param to add a join to $model_name |
|
4199 | + * and store it on $passed_in_query_info |
|
4200 | + * |
|
4201 | + * @param string $model_name |
|
4202 | + * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
4203 | + * @param string $original_query_param used to extract the relation chain between the queried |
|
4204 | + * model and $model_name. Eg, if we are querying Event, |
|
4205 | + * and are adding a join to 'Payment' with the original |
|
4206 | + * query param key |
|
4207 | + * 'Registration.Transaction.Payment.PAY_amount', we want |
|
4208 | + * to extract 'Registration.Transaction.Payment', in case |
|
4209 | + * Payment wants to add default query params so that it |
|
4210 | + * will know what models to prepend onto its default query |
|
4211 | + * params or in case it wants to rename tables (in case |
|
4212 | + * there are multiple joins to the same table) |
|
4213 | + * @return void |
|
4214 | + * @throws EE_Error |
|
4215 | + */ |
|
4216 | + private function _add_join_to_model( |
|
4217 | + $model_name, |
|
4218 | + EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
4219 | + $original_query_param |
|
4220 | + ) { |
|
4221 | + $relation_obj = $this->related_settings_for($model_name); |
|
4222 | + $model_relation_chain = EE_Model_Parser::extract_model_relation_chain($model_name, $original_query_param); |
|
4223 | + // check if the relation is HABTM, because then we're essentially doing two joins |
|
4224 | + // If so, join first to the JOIN table, and add its data types, and then continue as normal |
|
4225 | + if ($relation_obj instanceof EE_HABTM_Relation) { |
|
4226 | + $join_model_obj = $relation_obj->get_join_model(); |
|
4227 | + // replace the model specified with the join model for this relation chain, whi |
|
4228 | + $relation_chain_to_join_model = |
|
4229 | + EE_Model_Parser::replace_model_name_with_join_model_name_in_model_relation_chain( |
|
4230 | + $model_name, |
|
4231 | + $join_model_obj->get_this_model_name(), |
|
4232 | + $model_relation_chain |
|
4233 | + ); |
|
4234 | + $passed_in_query_info->merge( |
|
4235 | + new EE_Model_Query_Info_Carrier( |
|
4236 | + [$relation_chain_to_join_model => $join_model_obj->get_this_model_name()], |
|
4237 | + $relation_obj->get_join_to_intermediate_model_statement($relation_chain_to_join_model) |
|
4238 | + ) |
|
4239 | + ); |
|
4240 | + } |
|
4241 | + // now just join to the other table pointed to by the relation object, and add its data types |
|
4242 | + $passed_in_query_info->merge( |
|
4243 | + new EE_Model_Query_Info_Carrier( |
|
4244 | + [$model_relation_chain => $model_name], |
|
4245 | + $relation_obj->get_join_statement($model_relation_chain) |
|
4246 | + ) |
|
4247 | + ); |
|
4248 | + } |
|
4249 | + |
|
4250 | + |
|
4251 | + /** |
|
4252 | + * Constructs SQL for where clause, like "WHERE Event.ID = 23 AND Transaction.amount > 100" etc. |
|
4253 | + * |
|
4254 | + * @param array $where_params |
|
4255 | + * @return string of SQL |
|
4256 | + * @throws EE_Error |
|
4257 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
4258 | + */ |
|
4259 | + private function _construct_where_clause($where_params) |
|
4260 | + { |
|
4261 | + $SQL = $this->_construct_condition_clause_recursive($where_params, ' AND '); |
|
4262 | + if ($SQL) { |
|
4263 | + return " WHERE " . $SQL; |
|
4264 | + } |
|
4265 | + return ''; |
|
4266 | + } |
|
4267 | + |
|
4268 | + |
|
4269 | + /** |
|
4270 | + * Just like the _construct_where_clause, except prepends 'HAVING' instead of 'WHERE', |
|
4271 | + * and should be passed HAVING parameters, not WHERE parameters |
|
4272 | + * |
|
4273 | + * @param array $having_params |
|
4274 | + * @return string |
|
4275 | + * @throws EE_Error |
|
4276 | + */ |
|
4277 | + private function _construct_having_clause($having_params) |
|
4278 | + { |
|
4279 | + $SQL = $this->_construct_condition_clause_recursive($having_params, ' AND '); |
|
4280 | + if ($SQL) { |
|
4281 | + return " HAVING " . $SQL; |
|
4282 | + } |
|
4283 | + return ''; |
|
4284 | + } |
|
4285 | + |
|
4286 | + |
|
4287 | + /** |
|
4288 | + * Used for creating nested WHERE conditions. Eg "WHERE ! (Event.ID = 3 OR ( Event_Meta.meta_key = 'bob' AND |
|
4289 | + * Event_Meta.meta_value = 'foo'))" |
|
4290 | + * |
|
4291 | + * @param array $where_params |
|
4292 | + * @param string $glue joins each sub-clause together. Should really only be " AND " or " OR "... |
|
4293 | + * @return string of SQL |
|
4294 | + * @throws EE_Error |
|
4295 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
4296 | + */ |
|
4297 | + private function _construct_condition_clause_recursive($where_params, $glue = ' AND') |
|
4298 | + { |
|
4299 | + $where_clauses = []; |
|
4300 | + foreach ($where_params as $query_param => $op_and_value_or_sub_condition) { |
|
4301 | + $query_param = |
|
4302 | + $this->_remove_stars_and_anything_after_from_condition_query_param_key( |
|
4303 | + $query_param |
|
4304 | + );// str_replace("*",'',$query_param); |
|
4305 | + if (in_array($query_param, $this->_logic_query_param_keys)) { |
|
4306 | + switch ($query_param) { |
|
4307 | + case 'not': |
|
4308 | + case 'NOT': |
|
4309 | + $where_clauses[] = "! (" |
|
4310 | + . $this->_construct_condition_clause_recursive( |
|
4311 | + $op_and_value_or_sub_condition, |
|
4312 | + $glue |
|
4313 | + ) |
|
4314 | + . ")"; |
|
4315 | + break; |
|
4316 | + case 'and': |
|
4317 | + case 'AND': |
|
4318 | + $where_clauses[] = " (" |
|
4319 | + . $this->_construct_condition_clause_recursive( |
|
4320 | + $op_and_value_or_sub_condition, |
|
4321 | + ' AND ' |
|
4322 | + ) |
|
4323 | + . ")"; |
|
4324 | + break; |
|
4325 | + case 'or': |
|
4326 | + case 'OR': |
|
4327 | + $where_clauses[] = " (" |
|
4328 | + . $this->_construct_condition_clause_recursive( |
|
4329 | + $op_and_value_or_sub_condition, |
|
4330 | + ' OR ' |
|
4331 | + ) |
|
4332 | + . ")"; |
|
4333 | + break; |
|
4334 | + } |
|
4335 | + } else { |
|
4336 | + $field_obj = $this->_deduce_field_from_query_param($query_param); |
|
4337 | + // if it's not a normal field, maybe it's a custom selection? |
|
4338 | + if (! $field_obj) { |
|
4339 | + if ($this->_custom_selections instanceof CustomSelects) { |
|
4340 | + $field_obj = $this->_custom_selections->getDataTypeForAlias($query_param); |
|
4341 | + } else { |
|
4342 | + throw new EE_Error( |
|
4343 | + sprintf( |
|
4344 | + esc_html__( |
|
4345 | + "%s is neither a valid model field name, nor a custom selection", |
|
4346 | + "event_espresso" |
|
4347 | + ), |
|
4348 | + $query_param |
|
4349 | + ) |
|
4350 | + ); |
|
4351 | + } |
|
4352 | + } |
|
4353 | + $op_and_value_sql = $this->_construct_op_and_value($op_and_value_or_sub_condition, $field_obj); |
|
4354 | + $where_clauses[] = $this->_deduce_column_name_from_query_param($query_param) . SP . $op_and_value_sql; |
|
4355 | + } |
|
4356 | + } |
|
4357 | + return $where_clauses ? implode($glue, $where_clauses) : ''; |
|
4358 | + } |
|
4359 | + |
|
4360 | + |
|
4361 | + /** |
|
4362 | + * Takes the input parameter and extract the table name (alias) and column name |
|
4363 | + * |
|
4364 | + * @param string $query_param like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
4365 | + * @return string table alias and column name for SQL, eg "Transaction.TXN_ID" |
|
4366 | + * @throws EE_Error |
|
4367 | + */ |
|
4368 | + private function _deduce_column_name_from_query_param($query_param) |
|
4369 | + { |
|
4370 | + $field = $this->_deduce_field_from_query_param($query_param); |
|
4371 | + if ($field) { |
|
4372 | + $table_alias_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_from_query_param( |
|
4373 | + $field->get_model_name(), |
|
4374 | + $query_param |
|
4375 | + ); |
|
4376 | + return $table_alias_prefix . $field->get_qualified_column(); |
|
4377 | + } |
|
4378 | + if ( |
|
4379 | + $this->_custom_selections instanceof CustomSelects |
|
4380 | + && in_array($query_param, $this->_custom_selections->columnAliases(), true) |
|
4381 | + ) { |
|
4382 | + // maybe it's custom selection item? |
|
4383 | + // if so, just use it as the "column name" |
|
4384 | + return $query_param; |
|
4385 | + } |
|
4386 | + $custom_select_aliases = $this->_custom_selections instanceof CustomSelects |
|
4387 | + ? implode(',', $this->_custom_selections->columnAliases()) |
|
4388 | + : ''; |
|
4389 | + throw new EE_Error( |
|
4390 | + sprintf( |
|
4391 | + esc_html__( |
|
4392 | + "%s is not a valid field on this model, nor a custom selection (%s)", |
|
4393 | + "event_espresso" |
|
4394 | + ), |
|
4395 | + $query_param, |
|
4396 | + $custom_select_aliases |
|
4397 | + ) |
|
4398 | + ); |
|
4399 | + } |
|
4400 | + |
|
4401 | + |
|
4402 | + /** |
|
4403 | + * Removes the * and anything after it from the condition query param key. It is useful to add the * to condition |
|
4404 | + * query param keys (eg, 'OR*', 'EVT_ID') in order for the array keys to still be unique, so that they don't get |
|
4405 | + * overwritten Takes a string like 'Event.EVT_ID*', 'TXN_total**', 'OR*1st', and 'DTT_reg_start*foobar' to |
|
4406 | + * 'Event.EVT_ID', 'TXN_total', 'OR', and 'DTT_reg_start', respectively. |
|
4407 | + * |
|
4408 | + * @param string $condition_query_param_key |
|
4409 | + * @return string |
|
4410 | + */ |
|
4411 | + private function _remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
4412 | + { |
|
4413 | + $pos_of_star = strpos($condition_query_param_key, '*'); |
|
4414 | + if ($pos_of_star === false) { |
|
4415 | + return $condition_query_param_key; |
|
4416 | + } |
|
4417 | + return substr($condition_query_param_key, 0, $pos_of_star); |
|
4418 | + } |
|
4419 | + |
|
4420 | + |
|
4421 | + /** |
|
4422 | + * creates the SQL for the operator and the value in a WHERE clause, eg "< 23" or "LIKE '%monkey%'" |
|
4423 | + * |
|
4424 | + * @param mixed array | string $op_and_value |
|
4425 | + * @param EE_Model_Field_Base|string $field_obj . If string, should be one of EEM_Base::_valid_wpdb_data_types |
|
4426 | + * @return string |
|
4427 | + * @throws EE_Error |
|
4428 | + */ |
|
4429 | + private function _construct_op_and_value($op_and_value, $field_obj) |
|
4430 | + { |
|
4431 | + if (is_array($op_and_value)) { |
|
4432 | + $operator = isset($op_and_value[0]) ? $this->_prepare_operator_for_sql($op_and_value[0]) : null; |
|
4433 | + if (! $operator) { |
|
4434 | + $php_array_like_string = []; |
|
4435 | + foreach ($op_and_value as $key => $value) { |
|
4436 | + $php_array_like_string[] = "$key=>$value"; |
|
4437 | + } |
|
4438 | + throw new EE_Error( |
|
4439 | + sprintf( |
|
4440 | + esc_html__( |
|
4441 | + "You setup a query parameter like you were going to specify an operator, but didn't. You provided '(%s)', but the operator should be at array key index 0 (eg array('>',32))", |
|
4442 | + "event_espresso" |
|
4443 | + ), |
|
4444 | + implode(",", $php_array_like_string) |
|
4445 | + ) |
|
4446 | + ); |
|
4447 | + } |
|
4448 | + $value = isset($op_and_value[1]) ? $op_and_value[1] : null; |
|
4449 | + } else { |
|
4450 | + $operator = '='; |
|
4451 | + $value = $op_and_value; |
|
4452 | + } |
|
4453 | + // check to see if the value is actually another field |
|
4454 | + if (is_array($op_and_value) && isset($op_and_value[2]) && $op_and_value[2] == true) { |
|
4455 | + return $operator . SP . $this->_deduce_column_name_from_query_param($value); |
|
4456 | + } |
|
4457 | + if (in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
4458 | + // in this case, the value should be an array, or at least a comma-separated list |
|
4459 | + // it will need to handle a little differently |
|
4460 | + $cleaned_value = $this->_construct_in_value($value, $field_obj); |
|
4461 | + // note: $cleaned_value has already been run through $wpdb->prepare() |
|
4462 | + return $operator . SP . $cleaned_value; |
|
4463 | + } |
|
4464 | + if (in_array($operator, $this->valid_between_style_operators()) && is_array($value)) { |
|
4465 | + // the value should be an array with count of two. |
|
4466 | + if (count($value) !== 2) { |
|
4467 | + throw new EE_Error( |
|
4468 | + sprintf( |
|
4469 | + esc_html__( |
|
4470 | + "The '%s' operator must be used with an array of values and there must be exactly TWO values in that array.", |
|
4471 | + 'event_espresso' |
|
4472 | + ), |
|
4473 | + "BETWEEN" |
|
4474 | + ) |
|
4475 | + ); |
|
4476 | + } |
|
4477 | + $cleaned_value = $this->_construct_between_value($value, $field_obj); |
|
4478 | + return $operator . SP . $cleaned_value; |
|
4479 | + } |
|
4480 | + if (in_array($operator, $this->valid_null_style_operators())) { |
|
4481 | + if ($value !== null) { |
|
4482 | + throw new EE_Error( |
|
4483 | + sprintf( |
|
4484 | + esc_html__( |
|
4485 | + "You attempted to give a value (%s) while using a NULL-style operator (%s). That isn't valid", |
|
4486 | + "event_espresso" |
|
4487 | + ), |
|
4488 | + $value, |
|
4489 | + $operator |
|
4490 | + ) |
|
4491 | + ); |
|
4492 | + } |
|
4493 | + return $operator; |
|
4494 | + } |
|
4495 | + if (in_array($operator, $this->valid_like_style_operators()) && ! is_array($value)) { |
|
4496 | + // if the operator is 'LIKE', we want to allow percent signs (%) and not |
|
4497 | + // remove other junk. So just treat it as a string. |
|
4498 | + return $operator . SP . $this->_wpdb_prepare_using_field($value, '%s'); |
|
4499 | + } |
|
4500 | + if (! in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
4501 | + return $operator . SP . $this->_wpdb_prepare_using_field($value, $field_obj); |
|
4502 | + } |
|
4503 | + if (in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
4504 | + throw new EE_Error( |
|
4505 | + sprintf( |
|
4506 | + esc_html__( |
|
4507 | + "Operator '%s' must be used with an array of values, eg 'Registration.REG_ID' => array('%s',array(1,2,3))", |
|
4508 | + 'event_espresso' |
|
4509 | + ), |
|
4510 | + $operator, |
|
4511 | + $operator |
|
4512 | + ) |
|
4513 | + ); |
|
4514 | + } |
|
4515 | + if (! in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
4516 | + throw new EE_Error( |
|
4517 | + sprintf( |
|
4518 | + esc_html__( |
|
4519 | + "Operator '%s' must be used with a single value, not an array. Eg 'Registration.REG_ID => array('%s',23))", |
|
4520 | + 'event_espresso' |
|
4521 | + ), |
|
4522 | + $operator, |
|
4523 | + $operator |
|
4524 | + ) |
|
4525 | + ); |
|
4526 | + } |
|
4527 | + throw new EE_Error( |
|
4528 | + sprintf( |
|
4529 | + esc_html__( |
|
4530 | + "It appears you've provided some totally invalid query parameters. Operator and value were:'%s', which isn't right at all", |
|
4531 | + "event_espresso" |
|
4532 | + ), |
|
4533 | + http_build_query($op_and_value) |
|
4534 | + ) |
|
4535 | + ); |
|
4536 | + } |
|
4537 | + |
|
4538 | + |
|
4539 | + /** |
|
4540 | + * Creates the operands to be used in a BETWEEN query, eg "'2014-12-31 20:23:33' AND '2015-01-23 12:32:54'" |
|
4541 | + * |
|
4542 | + * @param array $values |
|
4543 | + * @param EE_Model_Field_Base|string $field_obj if string, it should be the datatype to be used when querying, eg |
|
4544 | + * '%s' |
|
4545 | + * @return string |
|
4546 | + * @throws EE_Error |
|
4547 | + */ |
|
4548 | + public function _construct_between_value($values, $field_obj) |
|
4549 | + { |
|
4550 | + $cleaned_values = []; |
|
4551 | + foreach ($values as $value) { |
|
4552 | + $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
4553 | + } |
|
4554 | + return $cleaned_values[0] . " AND " . $cleaned_values[1]; |
|
4555 | + } |
|
4556 | + |
|
4557 | + |
|
4558 | + /** |
|
4559 | + * Takes an array or a comma-separated list of $values and cleans them |
|
4560 | + * according to $data_type using $wpdb->prepare, and then makes the list a |
|
4561 | + * string surrounded by ( and ). Eg, _construct_in_value(array(1,2,3),'%d') would |
|
4562 | + * return '(1,2,3)'; _construct_in_value("1,2,hack",'%d') would return '(1,2,1)' (assuming |
|
4563 | + * I'm right that a string, when interpreted as a digit, becomes a 1. It might become a 0) |
|
4564 | + * |
|
4565 | + * @param mixed $values array or comma-separated string |
|
4566 | + * @param EE_Model_Field_Base|string $field_obj if string, it should be a wpdb data type like '%s', or '%d' |
|
4567 | + * @return string of SQL to follow an 'IN' or 'NOT IN' operator |
|
4568 | + * @throws EE_Error |
|
4569 | + */ |
|
4570 | + public function _construct_in_value($values, $field_obj) |
|
4571 | + { |
|
4572 | + $prepped = []; |
|
4573 | + // check if the value is a CSV list |
|
4574 | + if (is_string($values)) { |
|
4575 | + // in which case, turn it into an array |
|
4576 | + $values = explode(',', $values); |
|
4577 | + } |
|
4578 | + // make sure we only have one of each value in the list |
|
4579 | + $values = array_unique($values); |
|
4580 | + foreach ($values as $value) { |
|
4581 | + $prepped[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
4582 | + } |
|
4583 | + // we would just LOVE to leave $cleaned_values as an empty array, and return the value as "()", |
|
4584 | + // but unfortunately that's invalid SQL. So instead we return a string which we KNOW will evaluate to be the empty set |
|
4585 | + // which is effectively equivalent to returning "()". We don't return "(0)" because that only works for auto-incrementing columns |
|
4586 | + if (empty($prepped)) { |
|
4587 | + $all_fields = $this->field_settings(); |
|
4588 | + $first_field = reset($all_fields); |
|
4589 | + $main_table = $this->_get_main_table(); |
|
4590 | + $prepped[] = "SELECT {$first_field->get_table_column()} FROM {$main_table->get_table_name()} WHERE FALSE"; |
|
4591 | + } |
|
4592 | + return '(' . implode(',', $prepped) . ')'; |
|
4593 | + } |
|
4594 | + |
|
4595 | + |
|
4596 | + /** |
|
4597 | + * @param mixed $value |
|
4598 | + * @param EE_Model_Field_Base|string $field_obj if string it should be a wpdb data type like '%d' |
|
4599 | + * @return string |
|
4600 | + * @throws EE_Error |
|
4601 | + */ |
|
4602 | + private function _wpdb_prepare_using_field($value, $field_obj) |
|
4603 | + { |
|
4604 | + global $wpdb; |
|
4605 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
4606 | + return $wpdb->prepare( |
|
4607 | + $field_obj->get_wpdb_data_type(), |
|
4608 | + $this->_prepare_value_for_use_in_db($value, $field_obj) |
|
4609 | + ); |
|
4610 | + } //$field_obj should really just be a data type |
|
4611 | + if (! in_array($field_obj, $this->_valid_wpdb_data_types)) { |
|
4612 | + throw new EE_Error( |
|
4613 | + sprintf( |
|
4614 | + esc_html__("%s is not a valid wpdb datatype. Valid ones are %s", "event_espresso"), |
|
4615 | + $field_obj, |
|
4616 | + implode(",", $this->_valid_wpdb_data_types) |
|
4617 | + ) |
|
4618 | + ); |
|
4619 | + } |
|
4620 | + return $wpdb->prepare($field_obj, $value); |
|
4621 | + } |
|
4622 | + |
|
4623 | + |
|
4624 | + /** |
|
4625 | + * Takes the input parameter and finds the model field that it indicates. |
|
4626 | + * |
|
4627 | + * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
4628 | + * @return EE_Model_Field_Base |
|
4629 | + * @throws EE_Error |
|
4630 | + */ |
|
4631 | + protected function _deduce_field_from_query_param($query_param_name) |
|
4632 | + { |
|
4633 | + // ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
4634 | + // which will help us find the database table and column |
|
4635 | + $query_param_parts = explode(".", $query_param_name); |
|
4636 | + if (empty($query_param_parts)) { |
|
4637 | + throw new EE_Error( |
|
4638 | + sprintf( |
|
4639 | + esc_html__( |
|
4640 | + "_extract_column_name is empty when trying to extract column and table name from %s", |
|
4641 | + 'event_espresso' |
|
4642 | + ), |
|
4643 | + $query_param_name |
|
4644 | + ) |
|
4645 | + ); |
|
4646 | + } |
|
4647 | + $number_of_parts = count($query_param_parts); |
|
4648 | + $last_query_param_part = $query_param_parts[ count($query_param_parts) - 1 ]; |
|
4649 | + $field_name = $last_query_param_part; |
|
4650 | + $model_obj = $number_of_parts === 1 |
|
4651 | + ? $this |
|
4652 | + // $number_of_parts >= 2, the last part is the column name, and there are only 2parts. therefore... |
|
4653 | + : $this->get_related_model_obj($query_param_parts[ $number_of_parts - 2 ]); |
|
4654 | + try { |
|
4655 | + return $model_obj->field_settings_for($field_name); |
|
4656 | + } catch (EE_Error $e) { |
|
4657 | + return null; |
|
4658 | + } |
|
4659 | + } |
|
4660 | + |
|
4661 | + |
|
4662 | + /** |
|
4663 | + * Given a field's name (ie, a key in $this->field_settings()), uses the EE_Model_Field object to get the table's |
|
4664 | + * alias and column which corresponds to it |
|
4665 | + * |
|
4666 | + * @param string $field_name |
|
4667 | + * @return string |
|
4668 | + * @throws EE_Error |
|
4669 | + */ |
|
4670 | + public function _get_qualified_column_for_field($field_name) |
|
4671 | + { |
|
4672 | + $all_fields = $this->field_settings(); |
|
4673 | + $field = isset($all_fields[ $field_name ]) ? $all_fields[ $field_name ] : false; |
|
4674 | + if ($field) { |
|
4675 | + return $field->get_qualified_column(); |
|
4676 | + } |
|
4677 | + throw new EE_Error( |
|
4678 | + sprintf( |
|
4679 | + esc_html__( |
|
4680 | + "There is no field titled %s on model %s. Either the query trying to use it is bad, or you need to add it to the list of fields on the model.", |
|
4681 | + 'event_espresso' |
|
4682 | + ), |
|
4683 | + $field_name, |
|
4684 | + get_class($this) |
|
4685 | + ) |
|
4686 | + ); |
|
4687 | + } |
|
4688 | + |
|
4689 | + |
|
4690 | + /** |
|
4691 | + * similar to \EEM_Base::_get_qualified_column_for_field() but returns an array with data for ALL fields. |
|
4692 | + * Example usage: |
|
4693 | + * EEM_Ticket::instance()->get_all_wpdb_results( |
|
4694 | + * array(), |
|
4695 | + * ARRAY_A, |
|
4696 | + * EEM_Ticket::instance()->get_qualified_columns_for_all_fields() |
|
4697 | + * ); |
|
4698 | + * is equivalent to |
|
4699 | + * EEM_Ticket::instance()->get_all_wpdb_results( array(), ARRAY_A, '*' ); |
|
4700 | + * and |
|
4701 | + * EEM_Event::instance()->get_all_wpdb_results( |
|
4702 | + * array( |
|
4703 | + * array( |
|
4704 | + * 'Datetime.Ticket.TKT_ID' => array( '<', 100 ), |
|
4705 | + * ), |
|
4706 | + * ARRAY_A, |
|
4707 | + * implode( |
|
4708 | + * ', ', |
|
4709 | + * array_merge( |
|
4710 | + * EEM_Event::instance()->get_qualified_columns_for_all_fields( '', false ), |
|
4711 | + * EEM_Ticket::instance()->get_qualified_columns_for_all_fields( 'Datetime', false ) |
|
4712 | + * ) |
|
4713 | + * ) |
|
4714 | + * ) |
|
4715 | + * ); |
|
4716 | + * selects rows from the database, selecting all the event and ticket columns, where the ticket ID is below 100 |
|
4717 | + * |
|
4718 | + * @param string $model_relation_chain the chain of models used to join between the model you want to query |
|
4719 | + * and the one whose fields you are selecting for example: when querying |
|
4720 | + * tickets model and selecting fields from the tickets model you would |
|
4721 | + * leave this parameter empty, because no models are needed to join |
|
4722 | + * between the queried model and the selected one. Likewise when |
|
4723 | + * querying the datetime model and selecting fields from the tickets |
|
4724 | + * model, it would also be left empty, because there is a direct |
|
4725 | + * relation from datetimes to tickets, so no model is needed to join |
|
4726 | + * them together. However, when querying from the event model and |
|
4727 | + * selecting fields from the ticket model, you should provide the string |
|
4728 | + * 'Datetime', indicating that the event model must first join to the |
|
4729 | + * datetime model in order to find its relation to ticket model. |
|
4730 | + * Also, when querying from the venue model and selecting fields from |
|
4731 | + * the ticket model, you should provide the string 'Event.Datetime', |
|
4732 | + * indicating you need to join the venue model to the event model, |
|
4733 | + * to the datetime model, in order to find its relation to the ticket |
|
4734 | + * model. This string is used to deduce the prefix that gets added onto |
|
4735 | + * the models' tables qualified columns |
|
4736 | + * @param bool $return_string if true, will return a string with qualified column names separated |
|
4737 | + * by ', ' if false, will simply return a numerically indexed array of |
|
4738 | + * qualified column names |
|
4739 | + * @return array|string |
|
4740 | + */ |
|
4741 | + public function get_qualified_columns_for_all_fields($model_relation_chain = '', $return_string = true) |
|
4742 | + { |
|
4743 | + $table_prefix = str_replace('.', '__', $model_relation_chain) . (empty($model_relation_chain) ? '' : '__'); |
|
4744 | + $qualified_columns = []; |
|
4745 | + foreach ($this->field_settings() as $field) { |
|
4746 | + $qualified_columns[] = $table_prefix . $field->get_qualified_column(); |
|
4747 | + } |
|
4748 | + return $return_string ? implode(', ', $qualified_columns) : $qualified_columns; |
|
4749 | + } |
|
4750 | + |
|
4751 | + |
|
4752 | + /** |
|
4753 | + * constructs the select use on special limit joins |
|
4754 | + * NOTE: for now this has only been tested and will work when the table alias is for the PRIMARY table. Although |
|
4755 | + * its setup so the select query will be setup on and just doing the special select join off of the primary table |
|
4756 | + * (as that is typically where the limits would be set). |
|
4757 | + * |
|
4758 | + * @param string $table_alias The table the select is being built for |
|
4759 | + * @param mixed|string $limit The limit for this select |
|
4760 | + * @return string The final select join element for the query. |
|
4761 | + * @throws EE_Error |
|
4762 | + */ |
|
4763 | + public function _construct_limit_join_select($table_alias, $limit) |
|
4764 | + { |
|
4765 | + $SQL = ''; |
|
4766 | + foreach ($this->_tables as $table_obj) { |
|
4767 | + if ($table_obj instanceof EE_Primary_Table) { |
|
4768 | + $SQL .= $table_alias === $table_obj->get_table_alias() |
|
4769 | + ? $table_obj->get_select_join_limit($limit) |
|
4770 | + : SP . $table_obj->get_table_name() . " AS " . $table_obj->get_table_alias() . SP; |
|
4771 | + } elseif ($table_obj instanceof EE_Secondary_Table) { |
|
4772 | + $SQL .= $table_alias === $table_obj->get_table_alias() |
|
4773 | + ? $table_obj->get_select_join_limit_join($limit) |
|
4774 | + : SP . $table_obj->get_join_sql($table_alias) . SP; |
|
4775 | + } |
|
4776 | + } |
|
4777 | + return $SQL; |
|
4778 | + } |
|
4779 | + |
|
4780 | + |
|
4781 | + /** |
|
4782 | + * Constructs the internal join if there are multiple tables, or simply the table's name and alias |
|
4783 | + * Eg "wp_post AS Event" or "wp_post AS Event INNER JOIN wp_postmeta Event_Meta ON Event.ID = Event_Meta.post_id" |
|
4784 | + * |
|
4785 | + * @return string SQL |
|
4786 | + * @throws EE_Error |
|
4787 | + */ |
|
4788 | + public function _construct_internal_join() |
|
4789 | + { |
|
4790 | + $SQL = $this->_get_main_table()->get_table_sql(); |
|
4791 | + $SQL .= $this->_construct_internal_join_to_table_with_alias($this->_get_main_table()->get_table_alias()); |
|
4792 | + return $SQL; |
|
4793 | + } |
|
4794 | + |
|
4795 | + |
|
4796 | + /** |
|
4797 | + * Constructs the SQL for joining all the tables on this model. |
|
4798 | + * Normally $alias should be the primary table's alias, but in cases where |
|
4799 | + * we have already joined to a secondary table (eg, the secondary table has a foreign key and is joined before the |
|
4800 | + * primary table) then we should provide that secondary table's alias. Eg, with $alias being the primary table's |
|
4801 | + * alias, this will construct SQL like: |
|
4802 | + * " INNER JOIN wp_esp_secondary_table AS Secondary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
4803 | + * With $alias being a secondary table's alias, this will construct SQL like: |
|
4804 | + * " INNER JOIN wp_esp_primary_table AS Primary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
4805 | + * |
|
4806 | + * @param string $alias_prefixed table alias to join to (this table should already be in the FROM SQL clause) |
|
4807 | + * @return string |
|
4808 | + * @throws EE_Error |
|
4809 | + */ |
|
4810 | + public function _construct_internal_join_to_table_with_alias($alias_prefixed) |
|
4811 | + { |
|
4812 | + $SQL = ''; |
|
4813 | + $alias_sans_prefix = EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($alias_prefixed); |
|
4814 | + foreach ($this->_tables as $table_obj) { |
|
4815 | + if ($table_obj instanceof EE_Secondary_Table) {// table is secondary table |
|
4816 | + if ($alias_sans_prefix === $table_obj->get_table_alias()) { |
|
4817 | + // so we're joining to this table, meaning the table is already in |
|
4818 | + // the FROM statement, BUT the primary table isn't. So we want |
|
4819 | + // to add the inverse join sql |
|
4820 | + $SQL .= $table_obj->get_inverse_join_sql($alias_prefixed); |
|
4821 | + } else { |
|
4822 | + // just add a regular JOIN to this table from the primary table |
|
4823 | + $SQL .= $table_obj->get_join_sql($alias_prefixed); |
|
4824 | + } |
|
4825 | + }//if it's a primary table, dont add any SQL. it should already be in the FROM statement |
|
4826 | + } |
|
4827 | + return $SQL; |
|
4828 | + } |
|
4829 | + |
|
4830 | + |
|
4831 | + /** |
|
4832 | + * Gets an array for storing all the data types on the next-to-be-executed-query. |
|
4833 | + * This should be a growing array of keys being table-columns (eg 'EVT_ID' and 'Event.EVT_ID'), and values being |
|
4834 | + * their data type (eg, '%s', '%d', etc) |
|
4835 | + * |
|
4836 | + * @return array |
|
4837 | + */ |
|
4838 | + public function _get_data_types() |
|
4839 | + { |
|
4840 | + $data_types = []; |
|
4841 | + foreach ($this->field_settings() as $field_obj) { |
|
4842 | + // $data_types[$field_obj->get_table_column()] = $field_obj->get_wpdb_data_type(); |
|
4843 | + /** @var $field_obj EE_Model_Field_Base */ |
|
4844 | + $data_types[ $field_obj->get_qualified_column() ] = $field_obj->get_wpdb_data_type(); |
|
4845 | + } |
|
4846 | + return $data_types; |
|
4847 | + } |
|
4848 | + |
|
4849 | + |
|
4850 | + /** |
|
4851 | + * Gets the model object given the relation's name / model's name (eg, 'Event', 'Registration',etc. Always singular) |
|
4852 | + * |
|
4853 | + * @param string $model_name |
|
4854 | + * @return EEM_Base |
|
4855 | + * @throws EE_Error |
|
4856 | + */ |
|
4857 | + public function get_related_model_obj($model_name) |
|
4858 | + { |
|
4859 | + $model_classname = "EEM_" . $model_name; |
|
4860 | + if (! class_exists($model_classname)) { |
|
4861 | + throw new EE_Error( |
|
4862 | + sprintf( |
|
4863 | + esc_html__( |
|
4864 | + "You specified a related model named %s in your query. No such model exists, if it did, it would have the classname %s", |
|
4865 | + 'event_espresso' |
|
4866 | + ), |
|
4867 | + $model_name, |
|
4868 | + $model_classname |
|
4869 | + ) |
|
4870 | + ); |
|
4871 | + } |
|
4872 | + return call_user_func($model_classname . "::instance"); |
|
4873 | + } |
|
4874 | + |
|
4875 | + |
|
4876 | + /** |
|
4877 | + * Returns the array of EE_ModelRelations for this model. |
|
4878 | + * |
|
4879 | + * @return EE_Model_Relation_Base[] |
|
4880 | + */ |
|
4881 | + public function relation_settings() |
|
4882 | + { |
|
4883 | + return $this->_model_relations; |
|
4884 | + } |
|
4885 | + |
|
4886 | + |
|
4887 | + /** |
|
4888 | + * Gets all related models that this model BELONGS TO. Handy to know sometimes |
|
4889 | + * because without THOSE models, this model probably doesn't have much purpose. |
|
4890 | + * (Eg, without an event, datetimes have little purpose.) |
|
4891 | + * |
|
4892 | + * @return EE_Belongs_To_Relation[] |
|
4893 | + */ |
|
4894 | + public function belongs_to_relations() |
|
4895 | + { |
|
4896 | + $belongs_to_relations = []; |
|
4897 | + foreach ($this->relation_settings() as $model_name => $relation_obj) { |
|
4898 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
4899 | + $belongs_to_relations[ $model_name ] = $relation_obj; |
|
4900 | + } |
|
4901 | + } |
|
4902 | + return $belongs_to_relations; |
|
4903 | + } |
|
4904 | + |
|
4905 | + |
|
4906 | + /** |
|
4907 | + * Returns the specified EE_Model_Relation, or throws an exception |
|
4908 | + * |
|
4909 | + * @param string $relation_name name of relation, key in $this->_relatedModels |
|
4910 | + * @return EE_Model_Relation_Base |
|
4911 | + * @throws EE_Error |
|
4912 | + */ |
|
4913 | + public function related_settings_for($relation_name) |
|
4914 | + { |
|
4915 | + $relatedModels = $this->relation_settings(); |
|
4916 | + if (! array_key_exists($relation_name, $relatedModels)) { |
|
4917 | + throw new EE_Error( |
|
4918 | + sprintf( |
|
4919 | + esc_html__( |
|
4920 | + 'Cannot get %s related to %s. There is no model relation of that type. There is, however, %s...', |
|
4921 | + 'event_espresso' |
|
4922 | + ), |
|
4923 | + $relation_name, |
|
4924 | + $this->_get_class_name(), |
|
4925 | + implode(', ', array_keys($relatedModels)) |
|
4926 | + ) |
|
4927 | + ); |
|
4928 | + } |
|
4929 | + return $relatedModels[ $relation_name ]; |
|
4930 | + } |
|
4931 | + |
|
4932 | + |
|
4933 | + /** |
|
4934 | + * A convenience method for getting a specific field's settings, instead of getting all field settings for all |
|
4935 | + * fields |
|
4936 | + * |
|
4937 | + * @param string $fieldName |
|
4938 | + * @param boolean $include_db_only_fields |
|
4939 | + * @return EE_Model_Field_Base |
|
4940 | + * @throws EE_Error |
|
4941 | + */ |
|
4942 | + public function field_settings_for($fieldName, $include_db_only_fields = true) |
|
4943 | + { |
|
4944 | + $fieldSettings = $this->field_settings($include_db_only_fields); |
|
4945 | + if (! array_key_exists($fieldName, $fieldSettings)) { |
|
4946 | + throw new EE_Error( |
|
4947 | + sprintf( |
|
4948 | + esc_html__("There is no field/column '%s' on '%s'", 'event_espresso'), |
|
4949 | + $fieldName, |
|
4950 | + get_class($this) |
|
4951 | + ) |
|
4952 | + ); |
|
4953 | + } |
|
4954 | + return $fieldSettings[ $fieldName ]; |
|
4955 | + } |
|
4956 | + |
|
4957 | + |
|
4958 | + /** |
|
4959 | + * Checks if this field exists on this model |
|
4960 | + * |
|
4961 | + * @param string $fieldName a key in the model's _field_settings array |
|
4962 | + * @return boolean |
|
4963 | + */ |
|
4964 | + public function has_field($fieldName) |
|
4965 | + { |
|
4966 | + $fieldSettings = $this->field_settings(true); |
|
4967 | + if (isset($fieldSettings[ $fieldName ])) { |
|
4968 | + return true; |
|
4969 | + } |
|
4970 | + return false; |
|
4971 | + } |
|
4972 | + |
|
4973 | + |
|
4974 | + /** |
|
4975 | + * Returns whether or not this model has a relation to the specified model |
|
4976 | + * |
|
4977 | + * @param string $relation_name possibly one of the keys in the relation_settings array |
|
4978 | + * @return boolean |
|
4979 | + */ |
|
4980 | + public function has_relation($relation_name) |
|
4981 | + { |
|
4982 | + $relations = $this->relation_settings(); |
|
4983 | + if (isset($relations[ $relation_name ])) { |
|
4984 | + return true; |
|
4985 | + } |
|
4986 | + return false; |
|
4987 | + } |
|
4988 | + |
|
4989 | + |
|
4990 | + /** |
|
4991 | + * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
4992 | + * Eg, on EE_Answer that would be ANS_ID field object |
|
4993 | + * |
|
4994 | + * @param $field_obj |
|
4995 | + * @return boolean |
|
4996 | + */ |
|
4997 | + public function is_primary_key_field($field_obj) |
|
4998 | + { |
|
4999 | + return $field_obj instanceof EE_Primary_Key_Field_Base; |
|
5000 | + } |
|
5001 | + |
|
5002 | + |
|
5003 | + /** |
|
5004 | + * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
5005 | + * Eg, on EE_Answer that would be ANS_ID field object |
|
5006 | + * |
|
5007 | + * @return EE_Model_Field_Base |
|
5008 | + * @throws EE_Error |
|
5009 | + */ |
|
5010 | + public function get_primary_key_field() |
|
5011 | + { |
|
5012 | + if ($this->_primary_key_field === null) { |
|
5013 | + foreach ($this->field_settings(true) as $field_obj) { |
|
5014 | + if ($this->is_primary_key_field($field_obj)) { |
|
5015 | + $this->_primary_key_field = $field_obj; |
|
5016 | + break; |
|
5017 | + } |
|
5018 | + } |
|
5019 | + if (! $this->_primary_key_field instanceof EE_Primary_Key_Field_Base) { |
|
5020 | + throw new EE_Error( |
|
5021 | + sprintf( |
|
5022 | + esc_html__("There is no Primary Key defined on model %s", 'event_espresso'), |
|
5023 | + get_class($this) |
|
5024 | + ) |
|
5025 | + ); |
|
5026 | + } |
|
5027 | + } |
|
5028 | + return $this->_primary_key_field; |
|
5029 | + } |
|
5030 | + |
|
5031 | + |
|
5032 | + /** |
|
5033 | + * Returns whether or not not there is a primary key on this model. |
|
5034 | + * Internally does some caching. |
|
5035 | + * |
|
5036 | + * @return boolean |
|
5037 | + */ |
|
5038 | + public function has_primary_key_field() |
|
5039 | + { |
|
5040 | + if ($this->_has_primary_key_field === null) { |
|
5041 | + try { |
|
5042 | + $this->get_primary_key_field(); |
|
5043 | + $this->_has_primary_key_field = true; |
|
5044 | + } catch (EE_Error $e) { |
|
5045 | + $this->_has_primary_key_field = false; |
|
5046 | + } |
|
5047 | + } |
|
5048 | + return $this->_has_primary_key_field; |
|
5049 | + } |
|
5050 | + |
|
5051 | + |
|
5052 | + /** |
|
5053 | + * Finds the first field of type $field_class_name. |
|
5054 | + * |
|
5055 | + * @param string $field_class_name class name of field that you want to find. Eg, EE_Datetime_Field, |
|
5056 | + * EE_Foreign_Key_Field, etc |
|
5057 | + * @return EE_Model_Field_Base or null if none is found |
|
5058 | + */ |
|
5059 | + public function get_a_field_of_type($field_class_name) |
|
5060 | + { |
|
5061 | + foreach ($this->field_settings() as $field) { |
|
5062 | + if ($field instanceof $field_class_name) { |
|
5063 | + return $field; |
|
5064 | + } |
|
5065 | + } |
|
5066 | + return null; |
|
5067 | + } |
|
5068 | + |
|
5069 | + |
|
5070 | + /** |
|
5071 | + * Gets a foreign key field pointing to model. |
|
5072 | + * |
|
5073 | + * @param string $model_name eg Event, Registration, not EEM_Event |
|
5074 | + * @return EE_Foreign_Key_Field_Base |
|
5075 | + * @throws EE_Error |
|
5076 | + */ |
|
5077 | + public function get_foreign_key_to($model_name) |
|
5078 | + { |
|
5079 | + if (! isset($this->_cache_foreign_key_to_fields[ $model_name ])) { |
|
5080 | + foreach ($this->field_settings() as $field) { |
|
5081 | + if ( |
|
5082 | + $field instanceof EE_Foreign_Key_Field_Base |
|
5083 | + && in_array($model_name, $field->get_model_names_pointed_to()) |
|
5084 | + ) { |
|
5085 | + $this->_cache_foreign_key_to_fields[ $model_name ] = $field; |
|
5086 | + break; |
|
5087 | + } |
|
5088 | + } |
|
5089 | + if (! isset($this->_cache_foreign_key_to_fields[ $model_name ])) { |
|
5090 | + throw new EE_Error( |
|
5091 | + sprintf( |
|
5092 | + esc_html__( |
|
5093 | + "There is no foreign key field pointing to model %s on model %s", |
|
5094 | + 'event_espresso' |
|
5095 | + ), |
|
5096 | + $model_name, |
|
5097 | + get_class($this) |
|
5098 | + ) |
|
5099 | + ); |
|
5100 | + } |
|
5101 | + } |
|
5102 | + return $this->_cache_foreign_key_to_fields[ $model_name ]; |
|
5103 | + } |
|
5104 | + |
|
5105 | + |
|
5106 | + /** |
|
5107 | + * Gets the table name (including $wpdb->prefix) for the table alias |
|
5108 | + * |
|
5109 | + * @param string $table_alias eg Event, Event_Meta, Registration, Transaction, but maybe |
|
5110 | + * a table alias with a model chain prefix, like 'Venue__Event_Venue___Event_Meta'. |
|
5111 | + * Either one works |
|
5112 | + * @return string |
|
5113 | + */ |
|
5114 | + public function get_table_for_alias($table_alias) |
|
5115 | + { |
|
5116 | + $table_alias_sans_model_relation_chain_prefix = |
|
5117 | + EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($table_alias); |
|
5118 | + return $this->_tables[ $table_alias_sans_model_relation_chain_prefix ]->get_table_name(); |
|
5119 | + } |
|
5120 | + |
|
5121 | + |
|
5122 | + /** |
|
5123 | + * Returns a flat array of all field son this model, instead of organizing them |
|
5124 | + * by table_alias as they are in the constructor. |
|
5125 | + * |
|
5126 | + * @param bool $include_db_only_fields flag indicating whether or not to include the db-only fields |
|
5127 | + * @return EE_Model_Field_Base[] where the keys are the field's name |
|
5128 | + */ |
|
5129 | + public function field_settings($include_db_only_fields = false) |
|
5130 | + { |
|
5131 | + if ($include_db_only_fields) { |
|
5132 | + if ($this->_cached_fields === null) { |
|
5133 | + $this->_cached_fields = []; |
|
5134 | + foreach ($this->_fields as $fields_corresponding_to_table) { |
|
5135 | + foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
5136 | + $this->_cached_fields[ $field_name ] = $field_obj; |
|
5137 | + } |
|
5138 | + } |
|
5139 | + } |
|
5140 | + return $this->_cached_fields; |
|
5141 | + } |
|
5142 | + if ($this->_cached_fields_non_db_only === null) { |
|
5143 | + $this->_cached_fields_non_db_only = []; |
|
5144 | + foreach ($this->_fields as $fields_corresponding_to_table) { |
|
5145 | + foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
5146 | + /** @var $field_obj EE_Model_Field_Base */ |
|
5147 | + if (! $field_obj->is_db_only_field()) { |
|
5148 | + $this->_cached_fields_non_db_only[ $field_name ] = $field_obj; |
|
5149 | + } |
|
5150 | + } |
|
5151 | + } |
|
5152 | + } |
|
5153 | + return $this->_cached_fields_non_db_only; |
|
5154 | + } |
|
5155 | + |
|
5156 | + |
|
5157 | + /** |
|
5158 | + * cycle though array of attendees and create objects out of each item |
|
5159 | + * |
|
5160 | + * @access private |
|
5161 | + * @param array $rows of results of $wpdb->get_results($query,ARRAY_A) |
|
5162 | + * @return EE_Base_Class[] array keys are primary keys (if there is a primary key on the model. if not, |
|
5163 | + * numerically indexed) |
|
5164 | + * @throws EE_Error |
|
5165 | + * @throws ReflectionException |
|
5166 | + */ |
|
5167 | + protected function _create_objects($rows = []) |
|
5168 | + { |
|
5169 | + $array_of_objects = []; |
|
5170 | + if (empty($rows)) { |
|
5171 | + return []; |
|
5172 | + } |
|
5173 | + $count_if_model_has_no_primary_key = 0; |
|
5174 | + $has_primary_key = $this->has_primary_key_field(); |
|
5175 | + $primary_key_field = $has_primary_key ? $this->get_primary_key_field() : null; |
|
5176 | + foreach ((array) $rows as $row) { |
|
5177 | + if (empty($row)) { |
|
5178 | + // wp did its weird thing where it returns an array like array(0=>null), which is totally not helpful... |
|
5179 | + return []; |
|
5180 | + } |
|
5181 | + // check if we've already set this object in the results array, |
|
5182 | + // in which case there's no need to process it further (again) |
|
5183 | + if ($has_primary_key) { |
|
5184 | + $table_pk_value = $this->_get_column_value_with_table_alias_or_not( |
|
5185 | + $row, |
|
5186 | + $primary_key_field->get_qualified_column(), |
|
5187 | + $primary_key_field->get_table_column() |
|
5188 | + ); |
|
5189 | + if ($table_pk_value && isset($array_of_objects[ $table_pk_value ])) { |
|
5190 | + continue; |
|
5191 | + } |
|
5192 | + } |
|
5193 | + $classInstance = $this->instantiate_class_from_array_or_object($row); |
|
5194 | + if (! $classInstance) { |
|
5195 | + throw new EE_Error( |
|
5196 | + sprintf( |
|
5197 | + esc_html__('Could not create instance of class %s from row %s', 'event_espresso'), |
|
5198 | + $this->get_this_model_name(), |
|
5199 | + http_build_query($row) |
|
5200 | + ) |
|
5201 | + ); |
|
5202 | + } |
|
5203 | + // set the timezone on the instantiated objects |
|
5204 | + $classInstance->set_timezone($this->get_timezone(), true); |
|
5205 | + // make sure if there is any timezone setting present that we set the timezone for the object |
|
5206 | + $key = $has_primary_key ? $classInstance->ID() : $count_if_model_has_no_primary_key++; |
|
5207 | + $array_of_objects[ $key ] = $classInstance; |
|
5208 | + // also, for all the relations of type BelongsTo, see if we can cache |
|
5209 | + // those related models |
|
5210 | + // (we could do this for other relations too, but if there are conditions |
|
5211 | + // that filtered out some fo the results, then we'd be caching an incomplete set |
|
5212 | + // so it requires a little more thought than just caching them immediately...) |
|
5213 | + foreach ($this->_model_relations as $modelName => $relation_obj) { |
|
5214 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
5215 | + // check if this model's INFO is present. If so, cache it on the model |
|
5216 | + $other_model = $relation_obj->get_other_model(); |
|
5217 | + $other_model_obj_maybe = $other_model->instantiate_class_from_array_or_object($row); |
|
5218 | + // if we managed to make a model object from the results, cache it on the main model object |
|
5219 | + if ($other_model_obj_maybe) { |
|
5220 | + // set timezone on these other model objects if they are present |
|
5221 | + $other_model_obj_maybe->set_timezone($this->get_timezone(), true); |
|
5222 | + $classInstance->cache($modelName, $other_model_obj_maybe); |
|
5223 | + } |
|
5224 | + } |
|
5225 | + } |
|
5226 | + // also, if this was a custom select query, let's see if there are any results for the custom select fields |
|
5227 | + // and add them to the object as well. We'll convert according to the set data_type if there's any set for |
|
5228 | + // the field in the CustomSelects object |
|
5229 | + if ($this->_custom_selections instanceof CustomSelects) { |
|
5230 | + $classInstance->setCustomSelectsValues( |
|
5231 | + $this->getValuesForCustomSelectAliasesFromResults($row) |
|
5232 | + ); |
|
5233 | + } |
|
5234 | + } |
|
5235 | + return $array_of_objects; |
|
5236 | + } |
|
5237 | + |
|
5238 | + |
|
5239 | + /** |
|
5240 | + * This will parse a given row of results from the db and see if any keys in the results match an alias within the |
|
5241 | + * current CustomSelects object. This will be used to build an array of values indexed by those keys. |
|
5242 | + * |
|
5243 | + * @param array $db_results_row |
|
5244 | + * @return array |
|
5245 | + */ |
|
5246 | + protected function getValuesForCustomSelectAliasesFromResults(array $db_results_row) |
|
5247 | + { |
|
5248 | + $results = []; |
|
5249 | + if ($this->_custom_selections instanceof CustomSelects) { |
|
5250 | + foreach ($this->_custom_selections->columnAliases() as $alias) { |
|
5251 | + if (isset($db_results_row[ $alias ])) { |
|
5252 | + $results[ $alias ] = $this->convertValueToDataType( |
|
5253 | + $db_results_row[ $alias ], |
|
5254 | + $this->_custom_selections->getDataTypeForAlias($alias) |
|
5255 | + ); |
|
5256 | + } |
|
5257 | + } |
|
5258 | + } |
|
5259 | + return $results; |
|
5260 | + } |
|
5261 | + |
|
5262 | + |
|
5263 | + /** |
|
5264 | + * This will set the value for the given alias |
|
5265 | + * |
|
5266 | + * @param string $value |
|
5267 | + * @param string $datatype (one of %d, %s, %f) |
|
5268 | + * @return int|string|float (int for %d, string for %s, float for %f) |
|
5269 | + */ |
|
5270 | + protected function convertValueToDataType($value, $datatype) |
|
5271 | + { |
|
5272 | + switch ($datatype) { |
|
5273 | + case '%f': |
|
5274 | + return (float) $value; |
|
5275 | + case '%d': |
|
5276 | + return (int) $value; |
|
5277 | + default: |
|
5278 | + return (string) $value; |
|
5279 | + } |
|
5280 | + } |
|
5281 | + |
|
5282 | + |
|
5283 | + /** |
|
5284 | + * The purpose of this method is to allow us to create a model object that is not in the db that holds default |
|
5285 | + * values. A typical example of where this is used is when creating a new item and the initial load of a form. We |
|
5286 | + * dont' necessarily want to test for if the object is present but just assume it is BUT load the defaults from the |
|
5287 | + * object (as set in the model_field!). |
|
5288 | + * |
|
5289 | + * @return EE_Base_Class single EE_Base_Class object with default values for the properties. |
|
5290 | + * @throws EE_Error |
|
5291 | + * @throws ReflectionException |
|
5292 | + */ |
|
5293 | + public function create_default_object() |
|
5294 | + { |
|
5295 | + $this_model_fields_and_values = []; |
|
5296 | + // setup the row using default values; |
|
5297 | + foreach ($this->field_settings() as $field_name => $field_obj) { |
|
5298 | + $this_model_fields_and_values[ $field_name ] = $field_obj->get_default_value(); |
|
5299 | + } |
|
5300 | + $className = $this->_get_class_name(); |
|
5301 | + return EE_Registry::instance()->load_class( |
|
5302 | + $className, |
|
5303 | + [$this_model_fields_and_values], |
|
5304 | + false, |
|
5305 | + false |
|
5306 | + ); |
|
5307 | + } |
|
5308 | + |
|
5309 | + |
|
5310 | + /** |
|
5311 | + * @param mixed $cols_n_values either an array of where each key is the name of a field, and the value is its value |
|
5312 | + * or an stdClass where each property is the name of a column, |
|
5313 | + * @return EE_Base_Class |
|
5314 | + * @throws EE_Error |
|
5315 | + * @throws ReflectionException |
|
5316 | + */ |
|
5317 | + public function instantiate_class_from_array_or_object($cols_n_values) |
|
5318 | + { |
|
5319 | + if (! is_array($cols_n_values) && is_object($cols_n_values)) { |
|
5320 | + $cols_n_values = get_object_vars($cols_n_values); |
|
5321 | + } |
|
5322 | + $primary_key = null; |
|
5323 | + // make sure the array only has keys that are fields/columns on this model |
|
5324 | + $this_model_fields_n_values = $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
5325 | + if ($this->has_primary_key_field() && isset($this_model_fields_n_values[ $this->primary_key_name() ])) { |
|
5326 | + $primary_key = $this_model_fields_n_values[ $this->primary_key_name() ]; |
|
5327 | + } |
|
5328 | + $className = $this->_get_class_name(); |
|
5329 | + // check we actually found results that we can use to build our model object |
|
5330 | + // if not, return null |
|
5331 | + if ($this->has_primary_key_field()) { |
|
5332 | + if (empty($this_model_fields_n_values[ $this->primary_key_name() ])) { |
|
5333 | + return null; |
|
5334 | + } |
|
5335 | + } elseif ($this->unique_indexes()) { |
|
5336 | + $first_column = reset($this_model_fields_n_values); |
|
5337 | + if (empty($first_column)) { |
|
5338 | + return null; |
|
5339 | + } |
|
5340 | + } |
|
5341 | + // if there is no primary key or the object doesn't already exist in the entity map, then create a new instance |
|
5342 | + if ($primary_key) { |
|
5343 | + $classInstance = $this->get_from_entity_map($primary_key); |
|
5344 | + if (! $classInstance) { |
|
5345 | + $classInstance = EE_Registry::instance()->load_class( |
|
5346 | + $className, |
|
5347 | + [$this_model_fields_n_values, $this->get_timezone()], |
|
5348 | + true, |
|
5349 | + false |
|
5350 | + ); |
|
5351 | + // add this new object to the entity map |
|
5352 | + $classInstance = $this->add_to_entity_map($classInstance); |
|
5353 | + } |
|
5354 | + } else { |
|
5355 | + $classInstance = EE_Registry::instance()->load_class( |
|
5356 | + $className, |
|
5357 | + [$this_model_fields_n_values, $this->get_timezone()], |
|
5358 | + true, |
|
5359 | + false |
|
5360 | + ); |
|
5361 | + } |
|
5362 | + return $classInstance; |
|
5363 | + } |
|
5364 | + |
|
5365 | + |
|
5366 | + /** |
|
5367 | + * Gets the model object from the entity map if it exists |
|
5368 | + * |
|
5369 | + * @param int|string $id the ID of the model object |
|
5370 | + * @return EE_Base_Class |
|
5371 | + */ |
|
5372 | + public function get_from_entity_map($id) |
|
5373 | + { |
|
5374 | + return isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]) |
|
5375 | + ? $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ] |
|
5376 | + : null; |
|
5377 | + } |
|
5378 | + |
|
5379 | + |
|
5380 | + /** |
|
5381 | + * add_to_entity_map |
|
5382 | + * Adds the object to the model's entity mappings |
|
5383 | + * Effectively tells the models "Hey, this model object is the most up-to-date representation of the data, |
|
5384 | + * and for the remainder of the request, it's even more up-to-date than what's in the database. |
|
5385 | + * So, if the database doesn't agree with what's in the entity mapper, ignore the database" |
|
5386 | + * If the database gets updated directly and you want the entity mapper to reflect that change, |
|
5387 | + * then this method should be called immediately after the update query |
|
5388 | + * Note: The map is indexed by whatever the current blog id is set (via EEM_Base::$_model_query_blog_id). This is |
|
5389 | + * so on multisite, the entity map is specific to the query being done for a specific site. |
|
5390 | + * |
|
5391 | + * @param EE_Base_Class $object |
|
5392 | + * @return EE_Base_Class |
|
5393 | + * @throws EE_Error |
|
5394 | + */ |
|
5395 | + public function add_to_entity_map(EE_Base_Class $object) |
|
5396 | + { |
|
5397 | + $className = $this->_get_class_name(); |
|
5398 | + if (! $object instanceof $className) { |
|
5399 | + throw new EE_Error( |
|
5400 | + sprintf( |
|
5401 | + esc_html__("You tried adding a %s to a mapping of %ss", "event_espresso"), |
|
5402 | + is_object($object) ? get_class($object) : $object, |
|
5403 | + $className |
|
5404 | + ) |
|
5405 | + ); |
|
5406 | + } |
|
5407 | + if (! $object->ID()) { |
|
5408 | + throw new DomainException( |
|
5409 | + sprintf( |
|
5410 | + esc_html__( |
|
5411 | + "You tried storing a model object with NO ID in the %s entity mapper.", |
|
5412 | + "event_espresso" |
|
5413 | + ), |
|
5414 | + get_class($this) |
|
5415 | + ) |
|
5416 | + ); |
|
5417 | + } |
|
5418 | + // double check it's not already there |
|
5419 | + $classInstance = $this->get_from_entity_map($object->ID()); |
|
5420 | + if ($classInstance) { |
|
5421 | + return $classInstance; |
|
5422 | + } |
|
5423 | + $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $object->ID() ] = $object; |
|
5424 | + return $object; |
|
5425 | + } |
|
5426 | + |
|
5427 | + |
|
5428 | + /** |
|
5429 | + * if a valid identifier is provided, then that entity is unset from the entity map, |
|
5430 | + * if no identifier is provided, then the entire entity map is emptied |
|
5431 | + * |
|
5432 | + * @param int|string $id the ID of the model object |
|
5433 | + * @return boolean |
|
5434 | + */ |
|
5435 | + public function clear_entity_map($id = null) |
|
5436 | + { |
|
5437 | + if (empty($id)) { |
|
5438 | + $this->_entity_map[ EEM_Base::$_model_query_blog_id ] = []; |
|
5439 | + return true; |
|
5440 | + } |
|
5441 | + if (isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ])) { |
|
5442 | + unset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]); |
|
5443 | + return true; |
|
5444 | + } |
|
5445 | + return false; |
|
5446 | + } |
|
5447 | + |
|
5448 | + |
|
5449 | + /** |
|
5450 | + * Public wrapper for _deduce_fields_n_values_from_cols_n_values. |
|
5451 | + * Given an array where keys are column (or column alias) names and values, |
|
5452 | + * returns an array of their corresponding field names and database values |
|
5453 | + * |
|
5454 | + * @param array $cols_n_values |
|
5455 | + * @return array |
|
5456 | + * @throws EE_Error |
|
5457 | + * @throws ReflectionException |
|
5458 | + */ |
|
5459 | + public function deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
5460 | + { |
|
5461 | + return $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
5462 | + } |
|
5463 | + |
|
5464 | + |
|
5465 | + /** |
|
5466 | + * _deduce_fields_n_values_from_cols_n_values |
|
5467 | + * Given an array where keys are column (or column alias) names and values, |
|
5468 | + * returns an array of their corresponding field names and database values |
|
5469 | + * |
|
5470 | + * @param array $cols_n_values |
|
5471 | + * @return array |
|
5472 | + * @throws EE_Error |
|
5473 | + * @throws ReflectionException |
|
5474 | + */ |
|
5475 | + protected function _deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
5476 | + { |
|
5477 | + $this_model_fields_n_values = []; |
|
5478 | + foreach ($this->get_tables() as $table_alias => $table_obj) { |
|
5479 | + $table_pk_value = $this->_get_column_value_with_table_alias_or_not( |
|
5480 | + $cols_n_values, |
|
5481 | + $table_obj->get_fully_qualified_pk_column(), |
|
5482 | + $table_obj->get_pk_column() |
|
5483 | + ); |
|
5484 | + // there is a primary key on this table and its not set. Use defaults for all its columns |
|
5485 | + if ($table_pk_value === null && $table_obj->get_pk_column()) { |
|
5486 | + foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
5487 | + if (! $field_obj->is_db_only_field()) { |
|
5488 | + // prepare field as if its coming from db |
|
5489 | + $prepared_value = |
|
5490 | + $field_obj->prepare_for_set($field_obj->get_default_value()); |
|
5491 | + $this_model_fields_n_values[ $field_name ] = $field_obj->prepare_for_use_in_db($prepared_value); |
|
5492 | + } |
|
5493 | + } |
|
5494 | + } else { |
|
5495 | + // the table's rows existed. Use their values |
|
5496 | + foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
5497 | + if (! $field_obj->is_db_only_field()) { |
|
5498 | + $this_model_fields_n_values[ $field_name ] = $this->_get_column_value_with_table_alias_or_not( |
|
5499 | + $cols_n_values, |
|
5500 | + $field_obj->get_qualified_column(), |
|
5501 | + $field_obj->get_table_column() |
|
5502 | + ); |
|
5503 | + } |
|
5504 | + } |
|
5505 | + } |
|
5506 | + } |
|
5507 | + return $this_model_fields_n_values; |
|
5508 | + } |
|
5509 | + |
|
5510 | + |
|
5511 | + /** |
|
5512 | + * @param array $cols_n_values |
|
5513 | + * @param string $qualified_column |
|
5514 | + * @param string $regular_column |
|
5515 | + * @return null |
|
5516 | + * @throws EE_Error |
|
5517 | + * @throws ReflectionException |
|
5518 | + */ |
|
5519 | + protected function _get_column_value_with_table_alias_or_not($cols_n_values, $qualified_column, $regular_column) |
|
5520 | + { |
|
5521 | + $value = null; |
|
5522 | + // ask the field what it think it's table_name.column_name should be, and call it the "qualified column" |
|
5523 | + // does the field on the model relate to this column retrieved from the db? |
|
5524 | + // or is it a db-only field? (not relating to the model) |
|
5525 | + if (isset($cols_n_values[ $qualified_column ])) { |
|
5526 | + $value = $cols_n_values[ $qualified_column ]; |
|
5527 | + } elseif (isset($cols_n_values[ $regular_column ])) { |
|
5528 | + $value = $cols_n_values[ $regular_column ]; |
|
5529 | + } elseif (! empty($this->foreign_key_aliases)) { |
|
5530 | + // no PK? ok check if there is a foreign key alias set for this table |
|
5531 | + // then check if that alias exists in the incoming data |
|
5532 | + // AND that the actual PK the $FK_alias represents matches the $qualified_column (full PK) |
|
5533 | + foreach ($this->foreign_key_aliases as $FK_alias => $PK_column) { |
|
5534 | + if ($PK_column === $qualified_column && isset($cols_n_values[ $FK_alias ])) { |
|
5535 | + $value = $cols_n_values[ $FK_alias ]; |
|
5536 | + [$pk_class] = explode('.', $PK_column); |
|
5537 | + $pk_model_name = "EEM_{$pk_class}"; |
|
5538 | + /** @var EEM_Base $pk_model */ |
|
5539 | + $pk_model = EE_Registry::instance()->load_model($pk_model_name); |
|
5540 | + if ($pk_model instanceof EEM_Base) { |
|
5541 | + // make sure object is pulled from db and added to entity map |
|
5542 | + $pk_model->get_one_by_ID($value); |
|
5543 | + } |
|
5544 | + break; |
|
5545 | + } |
|
5546 | + } |
|
5547 | + } |
|
5548 | + return $value; |
|
5549 | + } |
|
5550 | + |
|
5551 | + |
|
5552 | + /** |
|
5553 | + * refresh_entity_map_from_db |
|
5554 | + * Makes sure the model object in the entity map at $id assumes the values |
|
5555 | + * of the database (opposite of EE_base_Class::save()) |
|
5556 | + * |
|
5557 | + * @param int|string $id |
|
5558 | + * @return EE_Base_Class |
|
5559 | + * @throws EE_Error |
|
5560 | + * @throws ReflectionException |
|
5561 | + */ |
|
5562 | + public function refresh_entity_map_from_db($id) |
|
5563 | + { |
|
5564 | + $obj_in_map = $this->get_from_entity_map($id); |
|
5565 | + if ($obj_in_map) { |
|
5566 | + $wpdb_results = $this->_get_all_wpdb_results( |
|
5567 | + [[$this->get_primary_key_field()->get_name() => $id], 'limit' => 1] |
|
5568 | + ); |
|
5569 | + if ($wpdb_results && is_array($wpdb_results)) { |
|
5570 | + $one_row = reset($wpdb_results); |
|
5571 | + foreach ($this->_deduce_fields_n_values_from_cols_n_values($one_row) as $field_name => $db_value) { |
|
5572 | + $obj_in_map->set_from_db($field_name, $db_value); |
|
5573 | + } |
|
5574 | + // clear the cache of related model objects |
|
5575 | + foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
5576 | + $obj_in_map->clear_cache($relation_name, null, true); |
|
5577 | + } |
|
5578 | + } |
|
5579 | + $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ] = $obj_in_map; |
|
5580 | + return $obj_in_map; |
|
5581 | + } |
|
5582 | + return $this->get_one_by_ID($id); |
|
5583 | + } |
|
5584 | + |
|
5585 | + |
|
5586 | + /** |
|
5587 | + * refresh_entity_map_with |
|
5588 | + * Leaves the entry in the entity map alone, but updates it to match the provided |
|
5589 | + * $replacing_model_obj (which we assume to be its equivalent but somehow NOT in the entity map). |
|
5590 | + * This is useful if you have a model object you want to make authoritative over what's in the entity map currently. |
|
5591 | + * Note: The old $replacing_model_obj should now be destroyed as it's now un-authoritative |
|
5592 | + * |
|
5593 | + * @param int|string $id |
|
5594 | + * @param EE_Base_Class $replacing_model_obj |
|
5595 | + * @return EE_Base_Class |
|
5596 | + * @throws EE_Error |
|
5597 | + * @throws ReflectionException |
|
5598 | + */ |
|
5599 | + public function refresh_entity_map_with($id, $replacing_model_obj) |
|
5600 | + { |
|
5601 | + $obj_in_map = $this->get_from_entity_map($id); |
|
5602 | + if ($obj_in_map) { |
|
5603 | + if ($replacing_model_obj instanceof EE_Base_Class) { |
|
5604 | + foreach ($replacing_model_obj->model_field_array() as $field_name => $value) { |
|
5605 | + $obj_in_map->set($field_name, $value); |
|
5606 | + } |
|
5607 | + // make the model object in the entity map's cache match the $replacing_model_obj |
|
5608 | + foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
5609 | + $obj_in_map->clear_cache($relation_name, null, true); |
|
5610 | + foreach ($replacing_model_obj->get_all_from_cache($relation_name) as $cache_id => $cached_obj) { |
|
5611 | + $obj_in_map->cache($relation_name, $cached_obj, $cache_id); |
|
5612 | + } |
|
5613 | + } |
|
5614 | + } |
|
5615 | + return $obj_in_map; |
|
5616 | + } |
|
5617 | + $this->add_to_entity_map($replacing_model_obj); |
|
5618 | + return $replacing_model_obj; |
|
5619 | + } |
|
5620 | + |
|
5621 | + |
|
5622 | + /** |
|
5623 | + * Gets the EE class that corresponds to this model. Eg, for EEM_Answer that |
|
5624 | + * would be EE_Answer.To import that class, you'd just add ".class.php" to the name, like so |
|
5625 | + * require_once($this->_getClassName().".class.php"); |
|
5626 | + * |
|
5627 | + * @return string |
|
5628 | + */ |
|
5629 | + private function _get_class_name() |
|
5630 | + { |
|
5631 | + return "EE_" . $this->get_this_model_name(); |
|
5632 | + } |
|
5633 | + |
|
5634 | + |
|
5635 | + /** |
|
5636 | + * Get the name of the items this model represents, for the quantity specified. Eg, |
|
5637 | + * if $quantity==1, on EEM_Event, it would 'Event' (internationalized), otherwise |
|
5638 | + * it would be 'Events'. |
|
5639 | + * |
|
5640 | + * @param int $quantity |
|
5641 | + * @return string |
|
5642 | + */ |
|
5643 | + public function item_name($quantity = 1) |
|
5644 | + { |
|
5645 | + return (int) $quantity === 1 ? $this->singular_item : $this->plural_item; |
|
5646 | + } |
|
5647 | + |
|
5648 | + |
|
5649 | + /** |
|
5650 | + * Very handy general function to allow for plugins to extend any child of EE_TempBase. |
|
5651 | + * If a method is called on a child of EE_TempBase that doesn't exist, this function is called |
|
5652 | + * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
|
5653 | + * requiring a plugin to extend the EE_TempBase (which works fine is there's only 1 plugin, but when will that |
|
5654 | + * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
|
5655 | + * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
|
5656 | + * was called, and an array of the original arguments passed to the function. Whatever their callback function |
|
5657 | + * returns will be returned by this function. Example: in functions.php (or in a plugin): |
|
5658 | + * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
|
5659 | + * my_callback($previousReturnValue,EE_TempBase $object,$argsArray){ |
|
5660 | + * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
5661 | + * return $previousReturnValue.$returnString; |
|
5662 | + * } |
|
5663 | + * require('EEM_Answer.model.php'); |
|
5664 | + * $answer=EEM_Answer::instance(); |
|
5665 | + * echo $answer->my_callback('monkeys',100); |
|
5666 | + * //will output "you called my_callback! and passed args:monkeys,100" |
|
5667 | + * |
|
5668 | + * @param string $methodName name of method which was called on a child of EE_TempBase, but which |
|
5669 | + * @param array $args array of original arguments passed to the function |
|
5670 | + * @return mixed whatever the plugin which calls add_filter decides |
|
5671 | + * @throws EE_Error |
|
5672 | + */ |
|
5673 | + public function __call($methodName, $args) |
|
5674 | + { |
|
5675 | + $className = get_class($this); |
|
5676 | + $tagName = "FHEE__{$className}__{$methodName}"; |
|
5677 | + if (! has_filter($tagName)) { |
|
5678 | + throw new EE_Error( |
|
5679 | + sprintf( |
|
5680 | + esc_html__( |
|
5681 | + 'Method %1$s on model %2$s does not exist! You can create one with the following code in functions.php or in a plugin: %4$s function my_callback(%4$s \$previousReturnValue, EEM_Base \$object\ $argsArray=NULL ){%4$s /*function body*/%4$s return \$whatever;%4$s }%4$s add_filter( \'%3$s\', \'my_callback\', 10, 3 );', |
|
5682 | + 'event_espresso' |
|
5683 | + ), |
|
5684 | + $methodName, |
|
5685 | + $className, |
|
5686 | + $tagName, |
|
5687 | + '<br />' |
|
5688 | + ) |
|
5689 | + ); |
|
5690 | + } |
|
5691 | + return apply_filters($tagName, null, $this, $args); |
|
5692 | + } |
|
5693 | + |
|
5694 | + |
|
5695 | + /** |
|
5696 | + * Ensures $base_class_obj_or_id is of the EE_Base_Class child that corresponds ot this model. |
|
5697 | + * If not, assumes its an ID, and uses $this->get_one_by_ID() to get the EE_Base_Class. |
|
5698 | + * |
|
5699 | + * @param EE_Base_Class|string|int $base_class_obj_or_id either: |
|
5700 | + * the EE_Base_Class object that corresponds to this Model, |
|
5701 | + * the object's class name |
|
5702 | + * or object's ID |
|
5703 | + * @param boolean $ensure_is_in_db if set, we will also verify this model object |
|
5704 | + * exists in the database. If it does not, we add it |
|
5705 | + * @return EE_Base_Class |
|
5706 | + * @throws EE_Error |
|
5707 | + * @throws ReflectionException |
|
5708 | + */ |
|
5709 | + public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false) |
|
5710 | + { |
|
5711 | + $className = $this->_get_class_name(); |
|
5712 | + if ($base_class_obj_or_id instanceof $className) { |
|
5713 | + $model_object = $base_class_obj_or_id; |
|
5714 | + } else { |
|
5715 | + $primary_key_field = $this->get_primary_key_field(); |
|
5716 | + if ( |
|
5717 | + $primary_key_field instanceof EE_Primary_Key_Int_Field |
|
5718 | + && ( |
|
5719 | + is_int($base_class_obj_or_id) |
|
5720 | + || is_string($base_class_obj_or_id) |
|
5721 | + ) |
|
5722 | + ) { |
|
5723 | + // assume it's an ID. |
|
5724 | + // either a proper integer or a string representing an integer (eg "101" instead of 101) |
|
5725 | + $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
5726 | + } elseif ( |
|
5727 | + $primary_key_field instanceof EE_Primary_Key_String_Field |
|
5728 | + && is_string($base_class_obj_or_id) |
|
5729 | + ) { |
|
5730 | + // assume its a string representation of the object |
|
5731 | + $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
5732 | + } else { |
|
5733 | + throw new EE_Error( |
|
5734 | + sprintf( |
|
5735 | + esc_html__( |
|
5736 | + "'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
5737 | + 'event_espresso' |
|
5738 | + ), |
|
5739 | + $base_class_obj_or_id, |
|
5740 | + $this->_get_class_name(), |
|
5741 | + print_r($base_class_obj_or_id, true) |
|
5742 | + ) |
|
5743 | + ); |
|
5744 | + } |
|
5745 | + } |
|
5746 | + if ($ensure_is_in_db && $model_object->ID() !== null) { |
|
5747 | + $model_object->save(); |
|
5748 | + } |
|
5749 | + return $model_object; |
|
5750 | + } |
|
5751 | + |
|
5752 | + |
|
5753 | + /** |
|
5754 | + * Similar to ensure_is_obj(), this method makes sure $base_class_obj_or_id |
|
5755 | + * is a value of the this model's primary key. If it's an EE_Base_Class child, |
|
5756 | + * returns it ID. |
|
5757 | + * |
|
5758 | + * @param EE_Base_Class|int|string $base_class_obj_or_id |
|
5759 | + * @return int|string depending on the type of this model object's ID |
|
5760 | + * @throws EE_Error |
|
5761 | + */ |
|
5762 | + public function ensure_is_ID($base_class_obj_or_id) |
|
5763 | + { |
|
5764 | + $className = $this->_get_class_name(); |
|
5765 | + if ($base_class_obj_or_id instanceof $className) { |
|
5766 | + $id = $base_class_obj_or_id->ID(); |
|
5767 | + } elseif (is_int($base_class_obj_or_id)) { |
|
5768 | + // assume it's an ID |
|
5769 | + $id = $base_class_obj_or_id; |
|
5770 | + } elseif (is_string($base_class_obj_or_id)) { |
|
5771 | + // assume its a string representation of the object |
|
5772 | + $id = $base_class_obj_or_id; |
|
5773 | + } else { |
|
5774 | + throw new EE_Error( |
|
5775 | + sprintf( |
|
5776 | + esc_html__( |
|
5777 | + "'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
5778 | + 'event_espresso' |
|
5779 | + ), |
|
5780 | + $base_class_obj_or_id, |
|
5781 | + $this->_get_class_name(), |
|
5782 | + print_r($base_class_obj_or_id, true) |
|
5783 | + ) |
|
5784 | + ); |
|
5785 | + } |
|
5786 | + return $id; |
|
5787 | + } |
|
5788 | + |
|
5789 | + |
|
5790 | + /** |
|
5791 | + * Sets whether the values passed to the model (eg, values in WHERE, values in INSERT, UPDATE, etc) |
|
5792 | + * have already been ran through the appropriate model field's prepare_for_use_in_db method. IE, they have |
|
5793 | + * been sanitized and converted into the appropriate domain. |
|
5794 | + * Usually the only place you'll want to change the default (which is to assume values have NOT been sanitized by |
|
5795 | + * the model object/model field) is when making a method call from WITHIN a model object, which has direct access |
|
5796 | + * to its sanitized values. Note: after changing this setting, you should set it back to its previous value (using |
|
5797 | + * get_assumption_concerning_values_already_prepared_by_model_object()) eg. |
|
5798 | + * $EVT = EEM_Event::instance(); $old_setting = |
|
5799 | + * $EVT->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
5800 | + * $EVT->assume_values_already_prepared_by_model_object(true); |
|
5801 | + * $EVT->update(array('foo'=>'bar'),array(array('foo'=>'monkey'))); |
|
5802 | + * $EVT->assume_values_already_prepared_by_model_object($old_setting); |
|
5803 | + * |
|
5804 | + * @param int $values_already_prepared like one of the constants on EEM_Base |
|
5805 | + * @return void |
|
5806 | + */ |
|
5807 | + public function assume_values_already_prepared_by_model_object( |
|
5808 | + $values_already_prepared = self::not_prepared_by_model_object |
|
5809 | + ) { |
|
5810 | + $this->_values_already_prepared_by_model_object = $values_already_prepared; |
|
5811 | + } |
|
5812 | + |
|
5813 | + |
|
5814 | + /** |
|
5815 | + * Read comments for assume_values_already_prepared_by_model_object() |
|
5816 | + * |
|
5817 | + * @return int |
|
5818 | + */ |
|
5819 | + public function get_assumption_concerning_values_already_prepared_by_model_object() |
|
5820 | + { |
|
5821 | + return $this->_values_already_prepared_by_model_object; |
|
5822 | + } |
|
5823 | + |
|
5824 | + |
|
5825 | + /** |
|
5826 | + * Gets all the indexes on this model |
|
5827 | + * |
|
5828 | + * @return EE_Index[] |
|
5829 | + */ |
|
5830 | + public function indexes() |
|
5831 | + { |
|
5832 | + return $this->_indexes; |
|
5833 | + } |
|
5834 | + |
|
5835 | + |
|
5836 | + /** |
|
5837 | + * Gets all the Unique Indexes on this model |
|
5838 | + * |
|
5839 | + * @return EE_Unique_Index[] |
|
5840 | + */ |
|
5841 | + public function unique_indexes() |
|
5842 | + { |
|
5843 | + $unique_indexes = []; |
|
5844 | + foreach ($this->_indexes as $name => $index) { |
|
5845 | + if ($index instanceof EE_Unique_Index) { |
|
5846 | + $unique_indexes [ $name ] = $index; |
|
5847 | + } |
|
5848 | + } |
|
5849 | + return $unique_indexes; |
|
5850 | + } |
|
5851 | + |
|
5852 | + |
|
5853 | + /** |
|
5854 | + * Gets all the fields which, when combined, make the primary key. |
|
5855 | + * This is usually just an array with 1 element (the primary key), but in cases |
|
5856 | + * where there is no primary key, it's a combination of fields as defined |
|
5857 | + * on a primary index |
|
5858 | + * |
|
5859 | + * @return EE_Model_Field_Base[] indexed by the field's name |
|
5860 | + * @throws EE_Error |
|
5861 | + */ |
|
5862 | + public function get_combined_primary_key_fields() |
|
5863 | + { |
|
5864 | + foreach ($this->indexes() as $index) { |
|
5865 | + if ($index instanceof EE_Primary_Key_Index) { |
|
5866 | + return $index->fields(); |
|
5867 | + } |
|
5868 | + } |
|
5869 | + return [$this->primary_key_name() => $this->get_primary_key_field()]; |
|
5870 | + } |
|
5871 | + |
|
5872 | + |
|
5873 | + /** |
|
5874 | + * Used to build a primary key string (when the model has no primary key), |
|
5875 | + * which can be used a unique string to identify this model object. |
|
5876 | + * |
|
5877 | + * @param array $fields_n_values keys are field names, values are their values. |
|
5878 | + * Note: if you have results from `EEM_Base::get_all_wpdb_results()`, you need to |
|
5879 | + * run it through `EEM_Base::deduce_fields_n_values_from_cols_n_values()` |
|
5880 | + * before passing it to this function (that will convert it from columns-n-values |
|
5881 | + * to field-names-n-values). |
|
5882 | + * @return string |
|
5883 | + * @throws EE_Error |
|
5884 | + */ |
|
5885 | + public function get_index_primary_key_string($fields_n_values) |
|
5886 | + { |
|
5887 | + $cols_n_values_for_primary_key_index = array_intersect_key( |
|
5888 | + $fields_n_values, |
|
5889 | + $this->get_combined_primary_key_fields() |
|
5890 | + ); |
|
5891 | + return http_build_query($cols_n_values_for_primary_key_index); |
|
5892 | + } |
|
5893 | + |
|
5894 | + |
|
5895 | + /** |
|
5896 | + * Gets the field values from the primary key string |
|
5897 | + * |
|
5898 | + * @param string $index_primary_key_string |
|
5899 | + * @return null|array |
|
5900 | + * @throws EE_Error |
|
5901 | + * @see EEM_Base::get_combined_primary_key_fields() and EEM_Base::get_index_primary_key_string() |
|
5902 | + */ |
|
5903 | + public function parse_index_primary_key_string($index_primary_key_string) |
|
5904 | + { |
|
5905 | + $key_fields = $this->get_combined_primary_key_fields(); |
|
5906 | + // check all of them are in the $id |
|
5907 | + $key_values_in_combined_pk = []; |
|
5908 | + parse_str($index_primary_key_string, $key_values_in_combined_pk); |
|
5909 | + foreach ($key_fields as $key_field_name => $field_obj) { |
|
5910 | + if (! isset($key_values_in_combined_pk[ $key_field_name ])) { |
|
5911 | + return null; |
|
5912 | + } |
|
5913 | + } |
|
5914 | + return $key_values_in_combined_pk; |
|
5915 | + } |
|
5916 | + |
|
5917 | + |
|
5918 | + /** |
|
5919 | + * verifies that an array of key-value pairs for model fields has a key |
|
5920 | + * for each field comprising the primary key index |
|
5921 | + * |
|
5922 | + * @param array $key_values |
|
5923 | + * @return boolean |
|
5924 | + * @throws EE_Error |
|
5925 | + */ |
|
5926 | + public function has_all_combined_primary_key_fields($key_values) |
|
5927 | + { |
|
5928 | + $keys_it_should_have = array_keys($this->get_combined_primary_key_fields()); |
|
5929 | + foreach ($keys_it_should_have as $key) { |
|
5930 | + if (! isset($key_values[ $key ])) { |
|
5931 | + return false; |
|
5932 | + } |
|
5933 | + } |
|
5934 | + return true; |
|
5935 | + } |
|
5936 | + |
|
5937 | + |
|
5938 | + /** |
|
5939 | + * Finds all model objects in the DB that appear to be a copy of $model_object_or_attributes_array. |
|
5940 | + * We consider something to be a copy if all the attributes match (except the ID, of course). |
|
5941 | + * |
|
5942 | + * @param array|EE_Base_Class $model_object_or_attributes_array If its an array, it's field-value pairs |
|
5943 | + * @param array $query_params see github link below for more info |
|
5944 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
5945 | + * @throws EE_Error |
|
5946 | + * @throws ReflectionException |
|
5947 | + * @return EE_Base_Class[] Array keys are object IDs (if there is a primary key on the model. if not, numerically |
|
5948 | + * indexed) |
|
5949 | + */ |
|
5950 | + public function get_all_copies($model_object_or_attributes_array, $query_params = []) |
|
5951 | + { |
|
5952 | + if ($model_object_or_attributes_array instanceof EE_Base_Class) { |
|
5953 | + $attributes_array = $model_object_or_attributes_array->model_field_array(); |
|
5954 | + } elseif (is_array($model_object_or_attributes_array)) { |
|
5955 | + $attributes_array = $model_object_or_attributes_array; |
|
5956 | + } else { |
|
5957 | + throw new EE_Error( |
|
5958 | + sprintf( |
|
5959 | + esc_html__( |
|
5960 | + "get_all_copies should be provided with either a model object or an array of field-value-pairs, but was given %s", |
|
5961 | + "event_espresso" |
|
5962 | + ), |
|
5963 | + $model_object_or_attributes_array |
|
5964 | + ) |
|
5965 | + ); |
|
5966 | + } |
|
5967 | + // even copies obviously won't have the same ID, so remove the primary key |
|
5968 | + // from the WHERE conditions for finding copies (if there is a primary key, of course) |
|
5969 | + if ($this->has_primary_key_field() && isset($attributes_array[ $this->primary_key_name() ])) { |
|
5970 | + unset($attributes_array[ $this->primary_key_name() ]); |
|
5971 | + } |
|
5972 | + if (isset($query_params[0])) { |
|
5973 | + $query_params[0] = array_merge($attributes_array, $query_params); |
|
5974 | + } else { |
|
5975 | + $query_params[0] = $attributes_array; |
|
5976 | + } |
|
5977 | + return $this->get_all($query_params); |
|
5978 | + } |
|
5979 | + |
|
5980 | + |
|
5981 | + /** |
|
5982 | + * Gets the first copy we find. See get_all_copies for more details |
|
5983 | + * |
|
5984 | + * @param mixed EE_Base_Class | array $model_object_or_attributes_array |
|
5985 | + * @param array $query_params |
|
5986 | + * @return EE_Base_Class |
|
5987 | + * @throws EE_Error |
|
5988 | + * @throws ReflectionException |
|
5989 | + */ |
|
5990 | + public function get_one_copy($model_object_or_attributes_array, $query_params = []) |
|
5991 | + { |
|
5992 | + if (! is_array($query_params)) { |
|
5993 | + EE_Error::doing_it_wrong( |
|
5994 | + 'EEM_Base::get_one_copy', |
|
5995 | + sprintf( |
|
5996 | + esc_html__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
5997 | + gettype($query_params) |
|
5998 | + ), |
|
5999 | + '4.6.0' |
|
6000 | + ); |
|
6001 | + $query_params = []; |
|
6002 | + } |
|
6003 | + $query_params['limit'] = 1; |
|
6004 | + $copies = $this->get_all_copies($model_object_or_attributes_array, $query_params); |
|
6005 | + if (is_array($copies)) { |
|
6006 | + return array_shift($copies); |
|
6007 | + } |
|
6008 | + return null; |
|
6009 | + } |
|
6010 | + |
|
6011 | + |
|
6012 | + /** |
|
6013 | + * Updates the item with the specified id. Ignores default query parameters because |
|
6014 | + * we have specified the ID, and its assumed we KNOW what we're doing |
|
6015 | + * |
|
6016 | + * @param array $fields_n_values keys are field names, values are their new values |
|
6017 | + * @param int|string $id the value of the primary key to update |
|
6018 | + * @return int number of rows updated |
|
6019 | + * @throws EE_Error |
|
6020 | + * @throws ReflectionException |
|
6021 | + */ |
|
6022 | + public function update_by_ID($fields_n_values, $id) |
|
6023 | + { |
|
6024 | + $query_params = [ |
|
6025 | + 0 => [$this->get_primary_key_field()->get_name() => $id], |
|
6026 | + 'default_where_conditions' => EEM_Base::default_where_conditions_others_only, |
|
6027 | + ]; |
|
6028 | + return $this->update($fields_n_values, $query_params); |
|
6029 | + } |
|
6030 | + |
|
6031 | + |
|
6032 | + /** |
|
6033 | + * Changes an operator which was supplied to the models into one usable in SQL |
|
6034 | + * |
|
6035 | + * @param string $operator_supplied |
|
6036 | + * @return string an operator which can be used in SQL |
|
6037 | + * @throws EE_Error |
|
6038 | + */ |
|
6039 | + private function _prepare_operator_for_sql($operator_supplied) |
|
6040 | + { |
|
6041 | + $sql_operator = |
|
6042 | + isset($this->_valid_operators[ $operator_supplied ]) ? $this->_valid_operators[ $operator_supplied ] |
|
6043 | + : null; |
|
6044 | + if ($sql_operator) { |
|
6045 | + return $sql_operator; |
|
6046 | + } |
|
6047 | + throw new EE_Error( |
|
6048 | + sprintf( |
|
6049 | + esc_html__( |
|
6050 | + "The operator '%s' is not in the list of valid operators: %s", |
|
6051 | + "event_espresso" |
|
6052 | + ), |
|
6053 | + $operator_supplied, |
|
6054 | + implode(",", array_keys($this->_valid_operators)) |
|
6055 | + ) |
|
6056 | + ); |
|
6057 | + } |
|
6058 | + |
|
6059 | + |
|
6060 | + /** |
|
6061 | + * Gets the valid operators |
|
6062 | + * |
|
6063 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
6064 | + */ |
|
6065 | + public function valid_operators() |
|
6066 | + { |
|
6067 | + return $this->_valid_operators; |
|
6068 | + } |
|
6069 | + |
|
6070 | + |
|
6071 | + /** |
|
6072 | + * Gets the between-style operators (take 2 arguments). |
|
6073 | + * |
|
6074 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
6075 | + */ |
|
6076 | + public function valid_between_style_operators() |
|
6077 | + { |
|
6078 | + return array_intersect( |
|
6079 | + $this->valid_operators(), |
|
6080 | + $this->_between_style_operators |
|
6081 | + ); |
|
6082 | + } |
|
6083 | + |
|
6084 | + |
|
6085 | + /** |
|
6086 | + * Gets the "like"-style operators (take a single argument, but it may contain wildcards) |
|
6087 | + * |
|
6088 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
6089 | + */ |
|
6090 | + public function valid_like_style_operators() |
|
6091 | + { |
|
6092 | + return array_intersect( |
|
6093 | + $this->valid_operators(), |
|
6094 | + $this->_like_style_operators |
|
6095 | + ); |
|
6096 | + } |
|
6097 | + |
|
6098 | + |
|
6099 | + /** |
|
6100 | + * Gets the "in"-style operators |
|
6101 | + * |
|
6102 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
6103 | + */ |
|
6104 | + public function valid_in_style_operators() |
|
6105 | + { |
|
6106 | + return array_intersect( |
|
6107 | + $this->valid_operators(), |
|
6108 | + $this->_in_style_operators |
|
6109 | + ); |
|
6110 | + } |
|
6111 | + |
|
6112 | + |
|
6113 | + /** |
|
6114 | + * Gets the "null"-style operators (accept no arguments) |
|
6115 | + * |
|
6116 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
6117 | + */ |
|
6118 | + public function valid_null_style_operators() |
|
6119 | + { |
|
6120 | + return array_intersect( |
|
6121 | + $this->valid_operators(), |
|
6122 | + $this->_null_style_operators |
|
6123 | + ); |
|
6124 | + } |
|
6125 | + |
|
6126 | + |
|
6127 | + /** |
|
6128 | + * Gets an array where keys are the primary keys and values are their 'names' |
|
6129 | + * (as determined by the model object's name() function, which is often overridden) |
|
6130 | + * |
|
6131 | + * @param array $query_params like get_all's |
|
6132 | + * @return string[] |
|
6133 | + * @throws EE_Error |
|
6134 | + * @throws ReflectionException |
|
6135 | + */ |
|
6136 | + public function get_all_names($query_params = []) |
|
6137 | + { |
|
6138 | + $objs = $this->get_all($query_params); |
|
6139 | + $names = []; |
|
6140 | + foreach ($objs as $obj) { |
|
6141 | + $names[ $obj->ID() ] = $obj->name(); |
|
6142 | + } |
|
6143 | + return $names; |
|
6144 | + } |
|
6145 | + |
|
6146 | + |
|
6147 | + /** |
|
6148 | + * Gets an array of primary keys from the model objects. If you acquired the model objects |
|
6149 | + * using EEM_Base::get_all() you don't need to call this (and probably shouldn't because |
|
6150 | + * this is duplicated effort and reduces efficiency) you would be better to use |
|
6151 | + * array_keys() on $model_objects. |
|
6152 | + * |
|
6153 | + * @param EE_Base_Class[] $model_objects |
|
6154 | + * @param boolean $filter_out_empty_ids if a model object has an ID of '' or 0, don't bother including it |
|
6155 | + * in the returned array |
|
6156 | + * @return array |
|
6157 | + * @throws EE_Error |
|
6158 | + */ |
|
6159 | + public function get_IDs($model_objects, $filter_out_empty_ids = false) |
|
6160 | + { |
|
6161 | + if (! $this->has_primary_key_field()) { |
|
6162 | + if (WP_DEBUG) { |
|
6163 | + EE_Error::add_error( |
|
6164 | + esc_html__('Trying to get IDs from a model than has no primary key', 'event_espresso'), |
|
6165 | + __FILE__, |
|
6166 | + __FUNCTION__, |
|
6167 | + __LINE__ |
|
6168 | + ); |
|
6169 | + } |
|
6170 | + } |
|
6171 | + $IDs = []; |
|
6172 | + foreach ($model_objects as $model_object) { |
|
6173 | + $id = $model_object->ID(); |
|
6174 | + if (! $id) { |
|
6175 | + if ($filter_out_empty_ids) { |
|
6176 | + continue; |
|
6177 | + } |
|
6178 | + if (WP_DEBUG) { |
|
6179 | + EE_Error::add_error( |
|
6180 | + esc_html__( |
|
6181 | + 'Called %1$s on a model object that has no ID and so probably hasn\'t been saved to the database', |
|
6182 | + 'event_espresso' |
|
6183 | + ), |
|
6184 | + __FILE__, |
|
6185 | + __FUNCTION__, |
|
6186 | + __LINE__ |
|
6187 | + ); |
|
6188 | + } |
|
6189 | + } |
|
6190 | + $IDs[] = $id; |
|
6191 | + } |
|
6192 | + return $IDs; |
|
6193 | + } |
|
6194 | + |
|
6195 | + |
|
6196 | + /** |
|
6197 | + * Returns the string used in capabilities relating to this model. If there |
|
6198 | + * are no capabilities that relate to this model returns false |
|
6199 | + * |
|
6200 | + * @return string|false |
|
6201 | + */ |
|
6202 | + public function cap_slug() |
|
6203 | + { |
|
6204 | + return apply_filters('FHEE__EEM_Base__cap_slug', $this->_caps_slug, $this); |
|
6205 | + } |
|
6206 | + |
|
6207 | + |
|
6208 | + /** |
|
6209 | + * Returns the capability-restrictions array (@param string $context |
|
6210 | + * |
|
6211 | + * @return EE_Default_Where_Conditions[] indexed by associated capability |
|
6212 | + * @throws EE_Error |
|
6213 | + * @see EEM_Base::_cap_restrictions). |
|
6214 | + * If $context is provided (which should be set to one of EEM_Base::valid_cap_contexts()) |
|
6215 | + * only returns the cap restrictions array in that context (ie, the array |
|
6216 | + * at that key) |
|
6217 | + * |
|
6218 | + */ |
|
6219 | + public function cap_restrictions($context = EEM_Base::caps_read) |
|
6220 | + { |
|
6221 | + EEM_Base::verify_is_valid_cap_context($context); |
|
6222 | + // check if we ought to run the restriction generator first |
|
6223 | + if ( |
|
6224 | + isset($this->_cap_restriction_generators[ $context ]) |
|
6225 | + && $this->_cap_restriction_generators[ $context ] instanceof EE_Restriction_Generator_Base |
|
6226 | + && ! $this->_cap_restriction_generators[ $context ]->has_generated_cap_restrictions() |
|
6227 | + ) { |
|
6228 | + $this->_cap_restrictions[ $context ] = array_merge( |
|
6229 | + $this->_cap_restrictions[ $context ], |
|
6230 | + $this->_cap_restriction_generators[ $context ]->generate_restrictions() |
|
6231 | + ); |
|
6232 | + } |
|
6233 | + // and make sure we've finalized the construction of each restriction |
|
6234 | + foreach ($this->_cap_restrictions[ $context ] as $where_conditions_obj) { |
|
6235 | + if ($where_conditions_obj instanceof EE_Default_Where_Conditions) { |
|
6236 | + $where_conditions_obj->_finalize_construct($this); |
|
6237 | + } |
|
6238 | + } |
|
6239 | + return $this->_cap_restrictions[ $context ]; |
|
6240 | + } |
|
6241 | + |
|
6242 | + |
|
6243 | + /** |
|
6244 | + * Indicating whether or not this model thinks its a wp core model |
|
6245 | + * |
|
6246 | + * @return boolean |
|
6247 | + */ |
|
6248 | + public function is_wp_core_model() |
|
6249 | + { |
|
6250 | + return $this->_wp_core_model; |
|
6251 | + } |
|
6252 | + |
|
6253 | + |
|
6254 | + /** |
|
6255 | + * Gets all the caps that are missing which impose a restriction on |
|
6256 | + * queries made in this context |
|
6257 | + * |
|
6258 | + * @param string $context one of EEM_Base::caps_ constants |
|
6259 | + * @return EE_Default_Where_Conditions[] indexed by capability name |
|
6260 | + * @throws EE_Error |
|
6261 | + */ |
|
6262 | + public function caps_missing($context = EEM_Base::caps_read) |
|
6263 | + { |
|
6264 | + $missing_caps = []; |
|
6265 | + $cap_restrictions = $this->cap_restrictions($context); |
|
6266 | + foreach ($cap_restrictions as $cap => $restriction_if_no_cap) { |
|
6267 | + if ( |
|
6268 | + ! EE_Capabilities::instance() |
|
6269 | + ->current_user_can($cap, $this->get_this_model_name() . '_model_applying_caps') |
|
6270 | + ) { |
|
6271 | + $missing_caps[ $cap ] = $restriction_if_no_cap; |
|
6272 | + } |
|
6273 | + } |
|
6274 | + return $missing_caps; |
|
6275 | + } |
|
6276 | + |
|
6277 | + |
|
6278 | + /** |
|
6279 | + * Gets the mapping from capability contexts to action strings used in capability names |
|
6280 | + * |
|
6281 | + * @return array keys are one of EEM_Base::valid_cap_contexts(), and values are usually |
|
6282 | + * one of 'read', 'edit', or 'delete' |
|
6283 | + */ |
|
6284 | + public function cap_contexts_to_cap_action_map() |
|
6285 | + { |
|
6286 | + return apply_filters( |
|
6287 | + 'FHEE__EEM_Base__cap_contexts_to_cap_action_map', |
|
6288 | + $this->_cap_contexts_to_cap_action_map, |
|
6289 | + $this |
|
6290 | + ); |
|
6291 | + } |
|
6292 | + |
|
6293 | + |
|
6294 | + /** |
|
6295 | + * Gets the action string for the specified capability context |
|
6296 | + * |
|
6297 | + * @param string $context |
|
6298 | + * @return string one of EEM_Base::cap_contexts_to_cap_action_map() values |
|
6299 | + * @throws EE_Error |
|
6300 | + */ |
|
6301 | + public function cap_action_for_context($context) |
|
6302 | + { |
|
6303 | + $mapping = $this->cap_contexts_to_cap_action_map(); |
|
6304 | + if (isset($mapping[ $context ])) { |
|
6305 | + return $mapping[ $context ]; |
|
6306 | + } |
|
6307 | + if ($action = apply_filters('FHEE__EEM_Base__cap_action_for_context', null, $this, $mapping, $context)) { |
|
6308 | + return $action; |
|
6309 | + } |
|
6310 | + throw new EE_Error( |
|
6311 | + sprintf( |
|
6312 | + esc_html__( |
|
6313 | + 'Cannot find capability restrictions for context "%1$s", allowed values are:%2$s', |
|
6314 | + 'event_espresso' |
|
6315 | + ), |
|
6316 | + $context, |
|
6317 | + implode(',', array_keys($this->cap_contexts_to_cap_action_map())) |
|
6318 | + ) |
|
6319 | + ); |
|
6320 | + } |
|
6321 | + |
|
6322 | + |
|
6323 | + /** |
|
6324 | + * Returns all the capability contexts which are valid when querying models |
|
6325 | + * |
|
6326 | + * @return array |
|
6327 | + */ |
|
6328 | + public static function valid_cap_contexts() |
|
6329 | + { |
|
6330 | + return apply_filters( |
|
6331 | + 'FHEE__EEM_Base__valid_cap_contexts', |
|
6332 | + [ |
|
6333 | + self::caps_read, |
|
6334 | + self::caps_read_admin, |
|
6335 | + self::caps_edit, |
|
6336 | + self::caps_delete, |
|
6337 | + ] |
|
6338 | + ); |
|
6339 | + } |
|
6340 | + |
|
6341 | + |
|
6342 | + /** |
|
6343 | + * Returns all valid options for 'default_where_conditions' |
|
6344 | + * |
|
6345 | + * @return array |
|
6346 | + */ |
|
6347 | + public static function valid_default_where_conditions() |
|
6348 | + { |
|
6349 | + return [ |
|
6350 | + EEM_Base::default_where_conditions_all, |
|
6351 | + EEM_Base::default_where_conditions_this_only, |
|
6352 | + EEM_Base::default_where_conditions_others_only, |
|
6353 | + EEM_Base::default_where_conditions_minimum_all, |
|
6354 | + EEM_Base::default_where_conditions_minimum_others, |
|
6355 | + EEM_Base::default_where_conditions_none, |
|
6356 | + ]; |
|
6357 | + } |
|
6358 | + |
|
6359 | + // public static function default_where_conditions_full |
|
6360 | + |
|
6361 | + |
|
6362 | + /** |
|
6363 | + * Verifies $context is one of EEM_Base::valid_cap_contexts(), if not it throws an exception |
|
6364 | + * |
|
6365 | + * @param string $context |
|
6366 | + * @return bool |
|
6367 | + * @throws EE_Error |
|
6368 | + */ |
|
6369 | + public static function verify_is_valid_cap_context($context) |
|
6370 | + { |
|
6371 | + $valid_cap_contexts = EEM_Base::valid_cap_contexts(); |
|
6372 | + if (in_array($context, $valid_cap_contexts)) { |
|
6373 | + return true; |
|
6374 | + } |
|
6375 | + throw new EE_Error( |
|
6376 | + sprintf( |
|
6377 | + esc_html__( |
|
6378 | + 'Context "%1$s" passed into model "%2$s" is not a valid context. They are: %3$s', |
|
6379 | + 'event_espresso' |
|
6380 | + ), |
|
6381 | + $context, |
|
6382 | + 'EEM_Base', |
|
6383 | + implode(',', $valid_cap_contexts) |
|
6384 | + ) |
|
6385 | + ); |
|
6386 | + } |
|
6387 | + |
|
6388 | + |
|
6389 | + /** |
|
6390 | + * Clears all the models field caches. This is only useful when a sub-class |
|
6391 | + * might have added a field or something and these caches might be invalidated |
|
6392 | + */ |
|
6393 | + protected function _invalidate_field_caches() |
|
6394 | + { |
|
6395 | + $this->_cache_foreign_key_to_fields = []; |
|
6396 | + $this->_cached_fields = null; |
|
6397 | + $this->_cached_fields_non_db_only = null; |
|
6398 | + } |
|
6399 | + |
|
6400 | + |
|
6401 | + /** |
|
6402 | + * Gets the list of all the where query param keys that relate to logic instead of field names |
|
6403 | + * (eg "and", "or", "not"). |
|
6404 | + * |
|
6405 | + * @return array |
|
6406 | + */ |
|
6407 | + public function logic_query_param_keys() |
|
6408 | + { |
|
6409 | + return $this->_logic_query_param_keys; |
|
6410 | + } |
|
6411 | + |
|
6412 | + |
|
6413 | + /** |
|
6414 | + * Determines whether or not the where query param array key is for a logic query param. |
|
6415 | + * Eg 'OR', 'not*', and 'and*because-i-say-so' should all return true, whereas |
|
6416 | + * 'ATT_fname', 'EVT_name*not-you-or-me', and 'ORG_name' should return false |
|
6417 | + * |
|
6418 | + * @param $query_param_key |
|
6419 | + * @return bool |
|
6420 | + */ |
|
6421 | + public function is_logic_query_param_key($query_param_key) |
|
6422 | + { |
|
6423 | + foreach ($this->logic_query_param_keys() as $logic_query_param_key) { |
|
6424 | + if ( |
|
6425 | + $query_param_key === $logic_query_param_key |
|
6426 | + || strpos($query_param_key, $logic_query_param_key . '*') === 0 |
|
6427 | + ) { |
|
6428 | + return true; |
|
6429 | + } |
|
6430 | + } |
|
6431 | + return false; |
|
6432 | + } |
|
6433 | + |
|
6434 | + |
|
6435 | + /** |
|
6436 | + * Returns true if this model has a password field on it (regardless of whether that password field has any content) |
|
6437 | + * |
|
6438 | + * @return boolean |
|
6439 | + * @since 4.9.74.p |
|
6440 | + */ |
|
6441 | + public function hasPassword() |
|
6442 | + { |
|
6443 | + // if we don't yet know if there's a password field, find out and remember it for next time. |
|
6444 | + if ($this->has_password_field === null) { |
|
6445 | + $password_field = $this->getPasswordField(); |
|
6446 | + $this->has_password_field = $password_field instanceof EE_Password_Field; |
|
6447 | + } |
|
6448 | + return $this->has_password_field; |
|
6449 | + } |
|
6450 | + |
|
6451 | + |
|
6452 | + /** |
|
6453 | + * Returns the password field on this model, if there is one |
|
6454 | + * |
|
6455 | + * @return EE_Password_Field|null |
|
6456 | + * @since 4.9.74.p |
|
6457 | + */ |
|
6458 | + public function getPasswordField() |
|
6459 | + { |
|
6460 | + // if we definitely already know there is a password field or not (because has_password_field is true or false) |
|
6461 | + // there's no need to search for it. If we don't know yet, then find out |
|
6462 | + if ($this->has_password_field === null && $this->password_field === null) { |
|
6463 | + $this->password_field = $this->get_a_field_of_type('EE_Password_Field'); |
|
6464 | + } |
|
6465 | + // don't bother setting has_password_field because that's hasPassword()'s job. |
|
6466 | + return $this->password_field; |
|
6467 | + } |
|
6468 | + |
|
6469 | + |
|
6470 | + /** |
|
6471 | + * Returns the list of field (as EE_Model_Field_Bases) that are protected by the password |
|
6472 | + * |
|
6473 | + * @return EE_Model_Field_Base[] |
|
6474 | + * @throws EE_Error |
|
6475 | + * @since 4.9.74.p |
|
6476 | + */ |
|
6477 | + public function getPasswordProtectedFields() |
|
6478 | + { |
|
6479 | + $password_field = $this->getPasswordField(); |
|
6480 | + $fields = []; |
|
6481 | + if ($password_field instanceof EE_Password_Field) { |
|
6482 | + $field_names = $password_field->protectedFields(); |
|
6483 | + foreach ($field_names as $field_name) { |
|
6484 | + $fields[ $field_name ] = $this->field_settings_for($field_name); |
|
6485 | + } |
|
6486 | + } |
|
6487 | + return $fields; |
|
6488 | + } |
|
6489 | + |
|
6490 | + |
|
6491 | + /** |
|
6492 | + * Checks if the current user can perform the requested action on this model |
|
6493 | + * |
|
6494 | + * @param string $cap_to_check one of the array keys from _cap_contexts_to_cap_action_map |
|
6495 | + * @param EE_Base_Class|array $model_obj_or_fields_n_values |
|
6496 | + * @return bool |
|
6497 | + * @throws EE_Error |
|
6498 | + * @throws InvalidArgumentException |
|
6499 | + * @throws InvalidDataTypeException |
|
6500 | + * @throws InvalidInterfaceException |
|
6501 | + * @throws ReflectionException |
|
6502 | + * @throws UnexpectedEntityException |
|
6503 | + * @since 4.9.74.p |
|
6504 | + */ |
|
6505 | + public function currentUserCan($cap_to_check, $model_obj_or_fields_n_values) |
|
6506 | + { |
|
6507 | + if ($model_obj_or_fields_n_values instanceof EE_Base_Class) { |
|
6508 | + $model_obj_or_fields_n_values = $model_obj_or_fields_n_values->model_field_array(); |
|
6509 | + } |
|
6510 | + if (! is_array($model_obj_or_fields_n_values)) { |
|
6511 | + throw new UnexpectedEntityException( |
|
6512 | + $model_obj_or_fields_n_values, |
|
6513 | + 'EE_Base_Class', |
|
6514 | + sprintf( |
|
6515 | + esc_html__( |
|
6516 | + '%1$s must be passed an `EE_Base_Class or an array of fields names with their values. You passed in something different.', |
|
6517 | + 'event_espresso' |
|
6518 | + ), |
|
6519 | + __FUNCTION__ |
|
6520 | + ) |
|
6521 | + ); |
|
6522 | + } |
|
6523 | + return $this->exists( |
|
6524 | + $this->alter_query_params_to_restrict_by_ID( |
|
6525 | + $this->get_index_primary_key_string($model_obj_or_fields_n_values), |
|
6526 | + [ |
|
6527 | + 'default_where_conditions' => 'none', |
|
6528 | + 'caps' => $cap_to_check, |
|
6529 | + ] |
|
6530 | + ) |
|
6531 | + ); |
|
6532 | + } |
|
6533 | + |
|
6534 | + |
|
6535 | + /** |
|
6536 | + * Returns the query param where conditions key to the password affecting this model. |
|
6537 | + * Eg on EEM_Event this would just be "password", on EEM_Datetime this would be "Event.password", etc. |
|
6538 | + * |
|
6539 | + * @return null|string |
|
6540 | + * @throws EE_Error |
|
6541 | + * @throws InvalidArgumentException |
|
6542 | + * @throws InvalidDataTypeException |
|
6543 | + * @throws InvalidInterfaceException |
|
6544 | + * @throws ModelConfigurationException |
|
6545 | + * @throws ReflectionException |
|
6546 | + * @since 4.9.74.p |
|
6547 | + */ |
|
6548 | + public function modelChainAndPassword() |
|
6549 | + { |
|
6550 | + if ($this->model_chain_to_password === null) { |
|
6551 | + throw new ModelConfigurationException( |
|
6552 | + $this, |
|
6553 | + esc_html_x( |
|
6554 | + // @codingStandardsIgnoreStart |
|
6555 | + 'Cannot exclude protected data because the model has not specified which model has the password.', |
|
6556 | + // @codingStandardsIgnoreEnd |
|
6557 | + '1: model name', |
|
6558 | + 'event_espresso' |
|
6559 | + ) |
|
6560 | + ); |
|
6561 | + } |
|
6562 | + if ($this->model_chain_to_password === '') { |
|
6563 | + $model_with_password = $this; |
|
6564 | + } else { |
|
6565 | + if ($pos_of_period = strrpos($this->model_chain_to_password, '.')) { |
|
6566 | + $last_model_in_chain = substr($this->model_chain_to_password, $pos_of_period + 1); |
|
6567 | + } else { |
|
6568 | + $last_model_in_chain = $this->model_chain_to_password; |
|
6569 | + } |
|
6570 | + $model_with_password = EE_Registry::instance()->load_model($last_model_in_chain); |
|
6571 | + } |
|
6572 | + |
|
6573 | + $password_field = $model_with_password->getPasswordField(); |
|
6574 | + if ($password_field instanceof EE_Password_Field) { |
|
6575 | + $password_field_name = $password_field->get_name(); |
|
6576 | + } else { |
|
6577 | + throw new ModelConfigurationException( |
|
6578 | + $this, |
|
6579 | + sprintf( |
|
6580 | + esc_html_x( |
|
6581 | + 'This model claims related model "%1$s" should have a password field on it, but none was found. The model relation chain is "%2$s"', |
|
6582 | + '1: model name, 2: special string', |
|
6583 | + 'event_espresso' |
|
6584 | + ), |
|
6585 | + $model_with_password->get_this_model_name(), |
|
6586 | + $this->model_chain_to_password |
|
6587 | + ) |
|
6588 | + ); |
|
6589 | + } |
|
6590 | + return ($this->model_chain_to_password ? $this->model_chain_to_password . '.' : '') . $password_field_name; |
|
6591 | + } |
|
6592 | + |
|
6593 | + |
|
6594 | + /** |
|
6595 | + * Returns true if there is a password on a related model which restricts access to some of this model's rows, |
|
6596 | + * or if this model itself has a password affecting access to some of its other fields. |
|
6597 | + * |
|
6598 | + * @return boolean |
|
6599 | + * @since 4.9.74.p |
|
6600 | + */ |
|
6601 | + public function restrictedByRelatedModelPassword() |
|
6602 | + { |
|
6603 | + return $this->model_chain_to_password !== null; |
|
6604 | + } |
|
6605 | 6605 | } |
@@ -567,7 +567,7 @@ discard block |
||
567 | 567 | protected function __construct($timezone = '') |
568 | 568 | { |
569 | 569 | // check that the model has not been loaded too soon |
570 | - if (! did_action('AHEE__EE_System__load_espresso_addons')) { |
|
570 | + if ( ! did_action('AHEE__EE_System__load_espresso_addons')) { |
|
571 | 571 | throw new EE_Error( |
572 | 572 | sprintf( |
573 | 573 | esc_html__( |
@@ -590,7 +590,7 @@ discard block |
||
590 | 590 | * |
591 | 591 | * @var EE_Table_Base[] $_tables |
592 | 592 | */ |
593 | - $this->_tables = (array) apply_filters('FHEE__' . get_class($this) . '__construct__tables', $this->_tables); |
|
593 | + $this->_tables = (array) apply_filters('FHEE__'.get_class($this).'__construct__tables', $this->_tables); |
|
594 | 594 | foreach ($this->_tables as $table_alias => $table_obj) { |
595 | 595 | /** @var $table_obj EE_Table_Base */ |
596 | 596 | $table_obj->_construct_finalize_with_alias($table_alias); |
@@ -605,10 +605,10 @@ discard block |
||
605 | 605 | * |
606 | 606 | * @param EE_Model_Field_Base[] $_fields |
607 | 607 | */ |
608 | - $this->_fields = (array) apply_filters('FHEE__' . get_class($this) . '__construct__fields', $this->_fields); |
|
608 | + $this->_fields = (array) apply_filters('FHEE__'.get_class($this).'__construct__fields', $this->_fields); |
|
609 | 609 | $this->_invalidate_field_caches(); |
610 | 610 | foreach ($this->_fields as $table_alias => $fields_for_table) { |
611 | - if (! array_key_exists($table_alias, $this->_tables)) { |
|
611 | + if ( ! array_key_exists($table_alias, $this->_tables)) { |
|
612 | 612 | throw new EE_Error( |
613 | 613 | sprintf( |
614 | 614 | esc_html__( |
@@ -645,7 +645,7 @@ discard block |
||
645 | 645 | * @param EE_Model_Relation_Base[] $_model_relations |
646 | 646 | */ |
647 | 647 | $this->_model_relations = (array) apply_filters( |
648 | - 'FHEE__' . get_class($this) . '__construct__model_relations', |
|
648 | + 'FHEE__'.get_class($this).'__construct__model_relations', |
|
649 | 649 | $this->_model_relations |
650 | 650 | ); |
651 | 651 | foreach ($this->_model_relations as $model_name => $relation_obj) { |
@@ -657,12 +657,12 @@ discard block |
||
657 | 657 | } |
658 | 658 | $this->set_timezone($timezone); |
659 | 659 | // finalize default where condition strategy, or set default |
660 | - if (! $this->_default_where_conditions_strategy) { |
|
660 | + if ( ! $this->_default_where_conditions_strategy) { |
|
661 | 661 | // nothing was set during child constructor, so set default |
662 | 662 | $this->_default_where_conditions_strategy = new EE_Default_Where_Conditions(); |
663 | 663 | } |
664 | 664 | $this->_default_where_conditions_strategy->_finalize_construct($this); |
665 | - if (! $this->_minimum_where_conditions_strategy) { |
|
665 | + if ( ! $this->_minimum_where_conditions_strategy) { |
|
666 | 666 | // nothing was set during child constructor, so set default |
667 | 667 | $this->_minimum_where_conditions_strategy = new EE_Default_Where_Conditions(); |
668 | 668 | } |
@@ -673,10 +673,10 @@ discard block |
||
673 | 673 | $this->_caps_slug = EEH_Inflector::pluralize_and_lower($this->get_this_model_name()); |
674 | 674 | } |
675 | 675 | // initialize the standard cap restriction generators if none were specified by the child constructor |
676 | - if (! empty($this->_cap_restriction_generators)) { |
|
676 | + if ( ! empty($this->_cap_restriction_generators)) { |
|
677 | 677 | foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
678 | - if (! isset($this->_cap_restriction_generators[ $cap_context ])) { |
|
679 | - $this->_cap_restriction_generators[ $cap_context ] = apply_filters( |
|
678 | + if ( ! isset($this->_cap_restriction_generators[$cap_context])) { |
|
679 | + $this->_cap_restriction_generators[$cap_context] = apply_filters( |
|
680 | 680 | 'FHEE__EEM_Base___construct__standard_cap_restriction_generator', |
681 | 681 | new EE_Restriction_Generator_Protected(), |
682 | 682 | $cap_context, |
@@ -688,10 +688,10 @@ discard block |
||
688 | 688 | // if ($this->_cap_restriction_generators !== false) { |
689 | 689 | // if there are cap restriction generators, use them to make the default cap restrictions |
690 | 690 | foreach ($this->_cap_restriction_generators as $context => $generator_object) { |
691 | - if (! $generator_object) { |
|
691 | + if ( ! $generator_object) { |
|
692 | 692 | continue; |
693 | 693 | } |
694 | - if (! $generator_object instanceof EE_Restriction_Generator_Base) { |
|
694 | + if ( ! $generator_object instanceof EE_Restriction_Generator_Base) { |
|
695 | 695 | throw new EE_Error( |
696 | 696 | sprintf( |
697 | 697 | esc_html__( |
@@ -704,12 +704,12 @@ discard block |
||
704 | 704 | ); |
705 | 705 | } |
706 | 706 | $action = $this->cap_action_for_context($context); |
707 | - if (! $generator_object->construction_finalized()) { |
|
707 | + if ( ! $generator_object->construction_finalized()) { |
|
708 | 708 | $generator_object->_construct_finalize($this, $action); |
709 | 709 | } |
710 | 710 | } |
711 | 711 | } |
712 | - do_action('AHEE__' . get_class($this) . '__construct__end'); |
|
712 | + do_action('AHEE__'.get_class($this).'__construct__end'); |
|
713 | 713 | } |
714 | 714 | |
715 | 715 | |
@@ -753,7 +753,7 @@ discard block |
||
753 | 753 | public static function instance($timezone = '') |
754 | 754 | { |
755 | 755 | // check if instance of Espresso_model already exists |
756 | - if (! static::$_instance instanceof static) { |
|
756 | + if ( ! static::$_instance instanceof static) { |
|
757 | 757 | // instantiate Espresso_model |
758 | 758 | static::$_instance = new static( |
759 | 759 | $timezone, |
@@ -789,7 +789,7 @@ discard block |
||
789 | 789 | foreach ($r->getDefaultProperties() as $property => $value) { |
790 | 790 | // don't set instance to null like it was originally, |
791 | 791 | // but it's static anyways, and we're ignoring static properties (for now at least) |
792 | - if (! isset($static_properties[ $property ])) { |
|
792 | + if ( ! isset($static_properties[$property])) { |
|
793 | 793 | static::$_instance->{$property} = $value; |
794 | 794 | } |
795 | 795 | } |
@@ -813,7 +813,7 @@ discard block |
||
813 | 813 | */ |
814 | 814 | private static function getLoader() |
815 | 815 | { |
816 | - if (! EEM_Base::$loader instanceof LoaderInterface) { |
|
816 | + if ( ! EEM_Base::$loader instanceof LoaderInterface) { |
|
817 | 817 | EEM_Base::$loader = LoaderFactory::getLoader(); |
818 | 818 | } |
819 | 819 | return EEM_Base::$loader; |
@@ -833,15 +833,15 @@ discard block |
||
833 | 833 | */ |
834 | 834 | public function status_array($translated = false) |
835 | 835 | { |
836 | - if (! array_key_exists('Status', $this->_model_relations)) { |
|
836 | + if ( ! array_key_exists('Status', $this->_model_relations)) { |
|
837 | 837 | return []; |
838 | 838 | } |
839 | 839 | $model_name = $this->get_this_model_name(); |
840 | 840 | $status_type = str_replace(' ', '_', strtolower(str_replace('_', ' ', $model_name))); |
841 | - $statuses = EEM_Status::instance()->get_all([['STS_type' => $status_type]]); |
|
841 | + $statuses = EEM_Status::instance()->get_all([['STS_type' => $status_type]]); |
|
842 | 842 | $status_array = []; |
843 | 843 | foreach ($statuses as $status) { |
844 | - $status_array[ $status->ID() ] = $status->get('STS_code'); |
|
844 | + $status_array[$status->ID()] = $status->get('STS_code'); |
|
845 | 845 | } |
846 | 846 | return $translated |
847 | 847 | ? EEM_Status::instance()->localized_status($status_array, false, 'sentence') |
@@ -904,7 +904,7 @@ discard block |
||
904 | 904 | { |
905 | 905 | $wp_user_field_name = $this->wp_user_field_name(); |
906 | 906 | if ($wp_user_field_name) { |
907 | - $query_params[0][ $wp_user_field_name ] = get_current_user_id(); |
|
907 | + $query_params[0][$wp_user_field_name] = get_current_user_id(); |
|
908 | 908 | } |
909 | 909 | return $query_params; |
910 | 910 | } |
@@ -922,17 +922,17 @@ discard block |
||
922 | 922 | public function wp_user_field_name() |
923 | 923 | { |
924 | 924 | try { |
925 | - if (! empty($this->_model_chain_to_wp_user)) { |
|
925 | + if ( ! empty($this->_model_chain_to_wp_user)) { |
|
926 | 926 | $models_to_follow_to_wp_users = explode('.', $this->_model_chain_to_wp_user); |
927 | 927 | $last_model_name = end($models_to_follow_to_wp_users); |
928 | 928 | $model_with_fk_to_wp_users = EE_Registry::instance()->load_model($last_model_name); |
929 | - $model_chain_to_wp_user = $this->_model_chain_to_wp_user . '.'; |
|
929 | + $model_chain_to_wp_user = $this->_model_chain_to_wp_user.'.'; |
|
930 | 930 | } else { |
931 | 931 | $model_with_fk_to_wp_users = $this; |
932 | 932 | $model_chain_to_wp_user = ''; |
933 | 933 | } |
934 | 934 | $wp_user_field = $model_with_fk_to_wp_users->get_foreign_key_to('WP_User'); |
935 | - return $model_chain_to_wp_user . $wp_user_field->get_name(); |
|
935 | + return $model_chain_to_wp_user.$wp_user_field->get_name(); |
|
936 | 936 | } catch (EE_Error $e) { |
937 | 937 | return false; |
938 | 938 | } |
@@ -1008,11 +1008,11 @@ discard block |
||
1008 | 1008 | if ($this->_custom_selections instanceof CustomSelects) { |
1009 | 1009 | $custom_expressions = $this->_custom_selections->columnsToSelectExpression(); |
1010 | 1010 | $select_expressions .= $select_expressions |
1011 | - ? ', ' . $custom_expressions |
|
1011 | + ? ', '.$custom_expressions |
|
1012 | 1012 | : $custom_expressions; |
1013 | 1013 | } |
1014 | 1014 | |
1015 | - $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
1015 | + $SQL = "SELECT $select_expressions ".$this->_construct_2nd_half_of_select_query($model_query_info); |
|
1016 | 1016 | return $this->_do_wpdb_query('get_results', [$SQL, $output]); |
1017 | 1017 | } |
1018 | 1018 | |
@@ -1029,7 +1029,7 @@ discard block |
||
1029 | 1029 | */ |
1030 | 1030 | protected function getCustomSelection(array $query_params, $columns_to_select = null) |
1031 | 1031 | { |
1032 | - if (! isset($query_params['extra_selects']) && $columns_to_select === null) { |
|
1032 | + if ( ! isset($query_params['extra_selects']) && $columns_to_select === null) { |
|
1033 | 1033 | return null; |
1034 | 1034 | } |
1035 | 1035 | $selects = isset($query_params['extra_selects']) ? $query_params['extra_selects'] : $columns_to_select; |
@@ -1078,7 +1078,7 @@ discard block |
||
1078 | 1078 | if (is_array($columns_to_select)) { |
1079 | 1079 | $select_sql_array = []; |
1080 | 1080 | foreach ($columns_to_select as $alias => $selection_and_datatype) { |
1081 | - if (! is_array($selection_and_datatype) || ! isset($selection_and_datatype[1])) { |
|
1081 | + if ( ! is_array($selection_and_datatype) || ! isset($selection_and_datatype[1])) { |
|
1082 | 1082 | throw new EE_Error( |
1083 | 1083 | sprintf( |
1084 | 1084 | esc_html__( |
@@ -1090,7 +1090,7 @@ discard block |
||
1090 | 1090 | ) |
1091 | 1091 | ); |
1092 | 1092 | } |
1093 | - if (! in_array($selection_and_datatype[1], $this->_valid_wpdb_data_types, true)) { |
|
1093 | + if ( ! in_array($selection_and_datatype[1], $this->_valid_wpdb_data_types, true)) { |
|
1094 | 1094 | throw new EE_Error( |
1095 | 1095 | sprintf( |
1096 | 1096 | esc_html__( |
@@ -1169,12 +1169,12 @@ discard block |
||
1169 | 1169 | */ |
1170 | 1170 | public function alter_query_params_to_restrict_by_ID($id, $query_params = []) |
1171 | 1171 | { |
1172 | - if (! isset($query_params[0])) { |
|
1172 | + if ( ! isset($query_params[0])) { |
|
1173 | 1173 | $query_params[0] = []; |
1174 | 1174 | } |
1175 | 1175 | $conditions_from_id = $this->parse_index_primary_key_string($id); |
1176 | 1176 | if ($conditions_from_id === null) { |
1177 | - $query_params[0][ $this->primary_key_name() ] = $id; |
|
1177 | + $query_params[0][$this->primary_key_name()] = $id; |
|
1178 | 1178 | } else { |
1179 | 1179 | // no primary key, so the $id must be from the get_index_primary_key_string() |
1180 | 1180 | $query_params[0] = array_replace_recursive($query_params[0], $this->parse_index_primary_key_string($id)); |
@@ -1194,7 +1194,7 @@ discard block |
||
1194 | 1194 | */ |
1195 | 1195 | public function get_one(array $query_params = []) |
1196 | 1196 | { |
1197 | - if (! is_array($query_params)) { |
|
1197 | + if ( ! is_array($query_params)) { |
|
1198 | 1198 | EE_Error::doing_it_wrong( |
1199 | 1199 | 'EEM_Base::get_one', |
1200 | 1200 | sprintf( |
@@ -1394,7 +1394,7 @@ discard block |
||
1394 | 1394 | return []; |
1395 | 1395 | } |
1396 | 1396 | } |
1397 | - if (! is_array($query_params)) { |
|
1397 | + if ( ! is_array($query_params)) { |
|
1398 | 1398 | EE_Error::doing_it_wrong( |
1399 | 1399 | 'EEM_Base::_get_consecutive', |
1400 | 1400 | sprintf( |
@@ -1406,7 +1406,7 @@ discard block |
||
1406 | 1406 | $query_params = []; |
1407 | 1407 | } |
1408 | 1408 | // let's add the where query param for consecutive look up. |
1409 | - $query_params[0][ $field_to_order_by ] = [$operand, $current_field_value]; |
|
1409 | + $query_params[0][$field_to_order_by] = [$operand, $current_field_value]; |
|
1410 | 1410 | $query_params['limit'] = $limit; |
1411 | 1411 | // set direction |
1412 | 1412 | $incoming_orderby = isset($query_params['order_by']) ? (array) $query_params['order_by'] : []; |
@@ -1493,7 +1493,7 @@ discard block |
||
1493 | 1493 | { |
1494 | 1494 | $field_settings = $this->field_settings_for($field_name); |
1495 | 1495 | // if not a valid EE_Datetime_Field then throw error |
1496 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
1496 | + if ( ! $field_settings instanceof EE_Datetime_Field) { |
|
1497 | 1497 | throw new EE_Error( |
1498 | 1498 | sprintf( |
1499 | 1499 | esc_html__( |
@@ -1649,7 +1649,7 @@ discard block |
||
1649 | 1649 | */ |
1650 | 1650 | public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true) |
1651 | 1651 | { |
1652 | - if (! is_array($query_params)) { |
|
1652 | + if ( ! is_array($query_params)) { |
|
1653 | 1653 | EE_Error::doing_it_wrong( |
1654 | 1654 | 'EEM_Base::update', |
1655 | 1655 | sprintf( |
@@ -1699,7 +1699,7 @@ discard block |
||
1699 | 1699 | $wpdb_result = (array) $wpdb_result; |
1700 | 1700 | // get the model object's PK, as we'll want this if we need to insert a row into secondary tables |
1701 | 1701 | if ($this->has_primary_key_field()) { |
1702 | - $main_table_pk_value = $wpdb_result[ $this->get_primary_key_field()->get_qualified_column() ]; |
|
1702 | + $main_table_pk_value = $wpdb_result[$this->get_primary_key_field()->get_qualified_column()]; |
|
1703 | 1703 | } else { |
1704 | 1704 | // if there's no primary key, we basically can't support having a 2nd table on the model (we could but it would be lots of work) |
1705 | 1705 | $main_table_pk_value = null; |
@@ -1715,7 +1715,7 @@ discard block |
||
1715 | 1715 | // in this table, right? so insert a row in the current table, using any fields available |
1716 | 1716 | if ( |
1717 | 1717 | ! (array_key_exists($this_table_pk_column, $wpdb_result) |
1718 | - && $wpdb_result[ $this_table_pk_column ]) |
|
1718 | + && $wpdb_result[$this_table_pk_column]) |
|
1719 | 1719 | ) { |
1720 | 1720 | $success = $this->_insert_into_specific_table( |
1721 | 1721 | $table_obj, |
@@ -1723,7 +1723,7 @@ discard block |
||
1723 | 1723 | $main_table_pk_value |
1724 | 1724 | ); |
1725 | 1725 | // if we died here, report the error |
1726 | - if (! $success) { |
|
1726 | + if ( ! $success) { |
|
1727 | 1727 | return false; |
1728 | 1728 | } |
1729 | 1729 | } |
@@ -1745,10 +1745,10 @@ discard block |
||
1745 | 1745 | $model_objs_affected_ids = []; |
1746 | 1746 | foreach ($models_affected_key_columns as $row) { |
1747 | 1747 | $combined_index_key = $this->get_index_primary_key_string($row); |
1748 | - $model_objs_affected_ids[ $combined_index_key ] = $combined_index_key; |
|
1748 | + $model_objs_affected_ids[$combined_index_key] = $combined_index_key; |
|
1749 | 1749 | } |
1750 | 1750 | } |
1751 | - if (! $model_objs_affected_ids) { |
|
1751 | + if ( ! $model_objs_affected_ids) { |
|
1752 | 1752 | // wait wait wait- if nothing was affected let's stop here |
1753 | 1753 | return 0; |
1754 | 1754 | } |
@@ -1771,8 +1771,8 @@ discard block |
||
1771 | 1771 | } |
1772 | 1772 | } |
1773 | 1773 | $model_query_info = $this->_create_model_query_info_carrier($query_params); |
1774 | - $SQL = "UPDATE " . $model_query_info->get_full_join_sql() |
|
1775 | - . " SET " . $this->_construct_update_sql($fields_n_values) |
|
1774 | + $SQL = "UPDATE ".$model_query_info->get_full_join_sql() |
|
1775 | + . " SET ".$this->_construct_update_sql($fields_n_values) |
|
1776 | 1776 | . $model_query_info->get_where_sql(); |
1777 | 1777 | // note: doesn't use _construct_2nd_half_of_select_query() because doesn't accept LIMIT, ORDER BY, etc. |
1778 | 1778 | $rows_affected = $this->_do_wpdb_query('query', [$SQL]); |
@@ -1786,7 +1786,7 @@ discard block |
||
1786 | 1786 | * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
1787 | 1787 | */ |
1788 | 1788 | do_action('AHEE__EEM_Base__update__end', $this, $fields_n_values, $query_params, $rows_affected); |
1789 | - return $rows_affected;// how many supposedly got updated |
|
1789 | + return $rows_affected; // how many supposedly got updated |
|
1790 | 1790 | } |
1791 | 1791 | |
1792 | 1792 | |
@@ -1817,7 +1817,7 @@ discard block |
||
1817 | 1817 | $model_query_info = $this->_create_model_query_info_carrier($query_params); |
1818 | 1818 | $select_expressions = $field->get_qualified_column(); |
1819 | 1819 | $SQL = |
1820 | - "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
1820 | + "SELECT $select_expressions ".$this->_construct_2nd_half_of_select_query($model_query_info); |
|
1821 | 1821 | return $this->_do_wpdb_query('get_col', [$SQL]); |
1822 | 1822 | } |
1823 | 1823 | |
@@ -1837,7 +1837,7 @@ discard block |
||
1837 | 1837 | { |
1838 | 1838 | $query_params['limit'] = 1; |
1839 | 1839 | $col = $this->get_col($query_params, $field_to_select); |
1840 | - if (! empty($col)) { |
|
1840 | + if ( ! empty($col)) { |
|
1841 | 1841 | return reset($col); |
1842 | 1842 | } |
1843 | 1843 | return null; |
@@ -1867,7 +1867,7 @@ discard block |
||
1867 | 1867 | $value_sql = $prepared_value === null |
1868 | 1868 | ? 'NULL' |
1869 | 1869 | : $wpdb->prepare($field_obj->get_wpdb_data_type(), $prepared_value); |
1870 | - $cols_n_values[] = $field_obj->get_qualified_column() . "=" . $value_sql; |
|
1870 | + $cols_n_values[] = $field_obj->get_qualified_column()."=".$value_sql; |
|
1871 | 1871 | } |
1872 | 1872 | return implode(",", $cols_n_values); |
1873 | 1873 | } |
@@ -1997,9 +1997,9 @@ discard block |
||
1997 | 1997 | if ($deletion_where_query_part) { |
1998 | 1998 | $model_query_info = $this->_create_model_query_info_carrier($query_params); |
1999 | 1999 | $table_aliases = implode(', ', array_keys($this->_tables)); |
2000 | - $SQL = "DELETE " . $table_aliases |
|
2001 | - . " FROM " . $model_query_info->get_full_join_sql() |
|
2002 | - . " WHERE " . $deletion_where_query_part; |
|
2000 | + $SQL = "DELETE ".$table_aliases |
|
2001 | + . " FROM ".$model_query_info->get_full_join_sql() |
|
2002 | + . " WHERE ".$deletion_where_query_part; |
|
2003 | 2003 | $rows_deleted = $this->_do_wpdb_query('query', [$SQL]); |
2004 | 2004 | } else { |
2005 | 2005 | $rows_deleted = 0; |
@@ -2010,12 +2010,12 @@ discard block |
||
2010 | 2010 | if ( |
2011 | 2011 | $this->has_primary_key_field() |
2012 | 2012 | && $rows_deleted !== false |
2013 | - && isset($columns_and_ids_for_deleting[ $this->get_primary_key_field()->get_qualified_column() ]) |
|
2013 | + && isset($columns_and_ids_for_deleting[$this->get_primary_key_field()->get_qualified_column()]) |
|
2014 | 2014 | ) { |
2015 | - $ids_for_removal = $columns_and_ids_for_deleting[ $this->get_primary_key_field()->get_qualified_column() ]; |
|
2015 | + $ids_for_removal = $columns_and_ids_for_deleting[$this->get_primary_key_field()->get_qualified_column()]; |
|
2016 | 2016 | foreach ($ids_for_removal as $id) { |
2017 | - if (isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ])) { |
|
2018 | - unset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]); |
|
2017 | + if (isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id])) { |
|
2018 | + unset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id]); |
|
2019 | 2019 | } |
2020 | 2020 | } |
2021 | 2021 | |
@@ -2055,7 +2055,7 @@ discard block |
||
2055 | 2055 | * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
2056 | 2056 | */ |
2057 | 2057 | do_action('AHEE__EEM_Base__delete__end', $this, $query_params, $rows_deleted, $columns_and_ids_for_deleting); |
2058 | - return $rows_deleted;// how many supposedly got deleted |
|
2058 | + return $rows_deleted; // how many supposedly got deleted |
|
2059 | 2059 | } |
2060 | 2060 | |
2061 | 2061 | |
@@ -2151,15 +2151,15 @@ discard block |
||
2151 | 2151 | if ( |
2152 | 2152 | $allow_blocking |
2153 | 2153 | && $this->delete_is_blocked_by_related_models( |
2154 | - $item_to_delete[ $primary_table->get_fully_qualified_pk_column() ] |
|
2154 | + $item_to_delete[$primary_table->get_fully_qualified_pk_column()] |
|
2155 | 2155 | ) |
2156 | 2156 | ) { |
2157 | 2157 | continue; |
2158 | 2158 | } |
2159 | 2159 | // primary table deletes |
2160 | - if (isset($item_to_delete[ $primary_table->get_fully_qualified_pk_column() ])) { |
|
2161 | - $ids_to_delete_indexed_by_column[ $primary_table->get_fully_qualified_pk_column() ][] = |
|
2162 | - $item_to_delete[ $primary_table->get_fully_qualified_pk_column() ]; |
|
2160 | + if (isset($item_to_delete[$primary_table->get_fully_qualified_pk_column()])) { |
|
2161 | + $ids_to_delete_indexed_by_column[$primary_table->get_fully_qualified_pk_column()][] = |
|
2162 | + $item_to_delete[$primary_table->get_fully_qualified_pk_column()]; |
|
2163 | 2163 | } |
2164 | 2164 | } |
2165 | 2165 | } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
@@ -2168,8 +2168,8 @@ discard block |
||
2168 | 2168 | $ids_to_delete_indexed_by_column_for_row = []; |
2169 | 2169 | foreach ($fields as $cpk_field) { |
2170 | 2170 | if ($cpk_field instanceof EE_Model_Field_Base) { |
2171 | - $ids_to_delete_indexed_by_column_for_row[ $cpk_field->get_qualified_column() ] = |
|
2172 | - $item_to_delete[ $cpk_field->get_qualified_column() ]; |
|
2171 | + $ids_to_delete_indexed_by_column_for_row[$cpk_field->get_qualified_column()] = |
|
2172 | + $item_to_delete[$cpk_field->get_qualified_column()]; |
|
2173 | 2173 | } |
2174 | 2174 | } |
2175 | 2175 | $ids_to_delete_indexed_by_column[] = $ids_to_delete_indexed_by_column_for_row; |
@@ -2207,7 +2207,7 @@ discard block |
||
2207 | 2207 | } elseif ($this->has_primary_key_field()) { |
2208 | 2208 | $query = []; |
2209 | 2209 | foreach ($ids_to_delete_indexed_by_column as $column => $ids) { |
2210 | - $query[] = $column . ' IN' . $this->_construct_in_value($ids, $this->_primary_key_field); |
|
2210 | + $query[] = $column.' IN'.$this->_construct_in_value($ids, $this->_primary_key_field); |
|
2211 | 2211 | } |
2212 | 2212 | $query_part = ! empty($query) ? implode(' AND ', $query) : $query_part; |
2213 | 2213 | } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
@@ -2215,7 +2215,7 @@ discard block |
||
2215 | 2215 | foreach ($ids_to_delete_indexed_by_column as $ids_to_delete_indexed_by_column_for_each_row) { |
2216 | 2216 | $values_for_each_combined_primary_key_for_a_row = []; |
2217 | 2217 | foreach ($ids_to_delete_indexed_by_column_for_each_row as $column => $id) { |
2218 | - $values_for_each_combined_primary_key_for_a_row[] = $column . '=' . $id; |
|
2218 | + $values_for_each_combined_primary_key_for_a_row[] = $column.'='.$id; |
|
2219 | 2219 | } |
2220 | 2220 | $value_and_value_and_value = implode(' AND ', $values_for_each_combined_primary_key_for_a_row); |
2221 | 2221 | $ways_to_identify_a_row[] = "({$value_and_value_and_value})"; |
@@ -2288,9 +2288,9 @@ discard block |
||
2288 | 2288 | $column_to_count = '*'; |
2289 | 2289 | } |
2290 | 2290 | } |
2291 | - $column_to_count = $distinct ? "DISTINCT " . $column_to_count : $column_to_count; |
|
2291 | + $column_to_count = $distinct ? "DISTINCT ".$column_to_count : $column_to_count; |
|
2292 | 2292 | $SQL = |
2293 | - "SELECT COUNT(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
2293 | + "SELECT COUNT(".$column_to_count.")".$this->_construct_2nd_half_of_select_query($model_query_info); |
|
2294 | 2294 | return (int) $this->_do_wpdb_query('get_var', [$SQL]); |
2295 | 2295 | } |
2296 | 2296 | |
@@ -2314,7 +2314,7 @@ discard block |
||
2314 | 2314 | $field_obj = $this->get_primary_key_field(); |
2315 | 2315 | } |
2316 | 2316 | $column_to_count = $field_obj->get_qualified_column(); |
2317 | - $SQL = "SELECT SUM(" . $column_to_count . ")" |
|
2317 | + $SQL = "SELECT SUM(".$column_to_count.")" |
|
2318 | 2318 | . $this->_construct_2nd_half_of_select_query($model_query_info); |
2319 | 2319 | $return_value = $this->_do_wpdb_query('get_var', [$SQL]); |
2320 | 2320 | // $data_type = $field_obj->get_wpdb_data_type(); |
@@ -2342,7 +2342,7 @@ discard block |
||
2342 | 2342 | // if we're in maintenance mode level 2, DON'T run any queries |
2343 | 2343 | // because level 2 indicates the database needs updating and |
2344 | 2344 | // is probably out of sync with the code |
2345 | - if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
2345 | + if ( ! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
2346 | 2346 | throw new EE_Error( |
2347 | 2347 | esc_html__( |
2348 | 2348 | 'Event Espresso Level 2 Maintenance mode is active. That means EE can not run ANY database queries until the necessary migration scripts have run which will take EE out of maintenance mode level 2. Please inform support of this error.', |
@@ -2351,7 +2351,7 @@ discard block |
||
2351 | 2351 | ); |
2352 | 2352 | } |
2353 | 2353 | global $wpdb; |
2354 | - if (! method_exists($wpdb, $wpdb_method)) { |
|
2354 | + if ( ! method_exists($wpdb, $wpdb_method)) { |
|
2355 | 2355 | throw new EE_Error( |
2356 | 2356 | sprintf( |
2357 | 2357 | esc_html__( |
@@ -2370,7 +2370,7 @@ discard block |
||
2370 | 2370 | $this->show_db_query_if_previously_requested($wpdb->last_query); |
2371 | 2371 | if (WP_DEBUG) { |
2372 | 2372 | $wpdb->show_errors($old_show_errors_value); |
2373 | - if (! empty($wpdb->last_error)) { |
|
2373 | + if ( ! empty($wpdb->last_error)) { |
|
2374 | 2374 | throw new EE_Error(sprintf(__('WPDB Error: "%s"', 'event_espresso'), $wpdb->last_error)); |
2375 | 2375 | } |
2376 | 2376 | if ($result === false) { |
@@ -2441,7 +2441,7 @@ discard block |
||
2441 | 2441 | // ummmm... you in trouble |
2442 | 2442 | return $result; |
2443 | 2443 | } |
2444 | - if (! empty($error_message)) { |
|
2444 | + if ( ! empty($error_message)) { |
|
2445 | 2445 | EE_Log::instance()->log(__FILE__, __FUNCTION__, $error_message, 'error'); |
2446 | 2446 | trigger_error($error_message); |
2447 | 2447 | } |
@@ -2520,11 +2520,11 @@ discard block |
||
2520 | 2520 | */ |
2521 | 2521 | private function _construct_2nd_half_of_select_query(EE_Model_Query_Info_Carrier $model_query_info) |
2522 | 2522 | { |
2523 | - return " FROM " . $model_query_info->get_full_join_sql() . |
|
2524 | - $model_query_info->get_where_sql() . |
|
2525 | - $model_query_info->get_group_by_sql() . |
|
2526 | - $model_query_info->get_having_sql() . |
|
2527 | - $model_query_info->get_order_by_sql() . |
|
2523 | + return " FROM ".$model_query_info->get_full_join_sql(). |
|
2524 | + $model_query_info->get_where_sql(). |
|
2525 | + $model_query_info->get_group_by_sql(). |
|
2526 | + $model_query_info->get_having_sql(). |
|
2527 | + $model_query_info->get_order_by_sql(). |
|
2528 | 2528 | $model_query_info->get_limit_sql(); |
2529 | 2529 | } |
2530 | 2530 | |
@@ -2717,12 +2717,12 @@ discard block |
||
2717 | 2717 | $related_model = $this->get_related_model_obj($model_name); |
2718 | 2718 | // we're just going to use the query params on the related model's normal get_all query, |
2719 | 2719 | // except add a condition to say to match the current mod |
2720 | - if (! isset($query_params['default_where_conditions'])) { |
|
2720 | + if ( ! isset($query_params['default_where_conditions'])) { |
|
2721 | 2721 | $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
2722 | 2722 | } |
2723 | 2723 | $this_model_name = $this->get_this_model_name(); |
2724 | 2724 | $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
2725 | - $query_params[0][ $this_model_name . "." . $this_pk_field_name ] = $id_or_obj; |
|
2725 | + $query_params[0][$this_model_name.".".$this_pk_field_name] = $id_or_obj; |
|
2726 | 2726 | return $related_model->count($query_params, $field_to_count, $distinct); |
2727 | 2727 | } |
2728 | 2728 | |
@@ -2743,7 +2743,7 @@ discard block |
||
2743 | 2743 | public function sum_related($id_or_obj, $model_name, $query_params, $field_to_sum = null) |
2744 | 2744 | { |
2745 | 2745 | $related_model = $this->get_related_model_obj($model_name); |
2746 | - if (! is_array($query_params)) { |
|
2746 | + if ( ! is_array($query_params)) { |
|
2747 | 2747 | EE_Error::doing_it_wrong( |
2748 | 2748 | 'EEM_Base::sum_related', |
2749 | 2749 | sprintf( |
@@ -2756,12 +2756,12 @@ discard block |
||
2756 | 2756 | } |
2757 | 2757 | // we're just going to use the query params on the related model's normal get_all query, |
2758 | 2758 | // except add a condition to say to match the current mod |
2759 | - if (! isset($query_params['default_where_conditions'])) { |
|
2759 | + if ( ! isset($query_params['default_where_conditions'])) { |
|
2760 | 2760 | $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
2761 | 2761 | } |
2762 | 2762 | $this_model_name = $this->get_this_model_name(); |
2763 | 2763 | $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
2764 | - $query_params[0][ $this_model_name . "." . $this_pk_field_name ] = $id_or_obj; |
|
2764 | + $query_params[0][$this_model_name.".".$this_pk_field_name] = $id_or_obj; |
|
2765 | 2765 | return $related_model->sum($query_params, $field_to_sum); |
2766 | 2766 | } |
2767 | 2767 | |
@@ -2813,7 +2813,7 @@ discard block |
||
2813 | 2813 | $field_with_model_name = $field; |
2814 | 2814 | } |
2815 | 2815 | } |
2816 | - if (! isset($field_with_model_name) || ! $field_with_model_name) { |
|
2816 | + if ( ! isset($field_with_model_name) || ! $field_with_model_name) { |
|
2817 | 2817 | throw new EE_Error( |
2818 | 2818 | sprintf( |
2819 | 2819 | esc_html__("There is no EE_Any_Foreign_Model_Name field on model %s", "event_espresso"), |
@@ -2952,14 +2952,14 @@ discard block |
||
2952 | 2952 | || $this->get_primary_key_field() |
2953 | 2953 | instanceof |
2954 | 2954 | EE_Primary_Key_String_Field) |
2955 | - && isset($fields_n_values[ $this->primary_key_name() ]) |
|
2955 | + && isset($fields_n_values[$this->primary_key_name()]) |
|
2956 | 2956 | ) { |
2957 | - $query_params[0]['OR'][ $this->primary_key_name() ] = $fields_n_values[ $this->primary_key_name() ]; |
|
2957 | + $query_params[0]['OR'][$this->primary_key_name()] = $fields_n_values[$this->primary_key_name()]; |
|
2958 | 2958 | } |
2959 | 2959 | foreach ($this->unique_indexes() as $unique_index_name => $unique_index) { |
2960 | 2960 | $uniqueness_where_params = |
2961 | 2961 | array_intersect_key($fields_n_values, $unique_index->fields()); |
2962 | - $query_params[0]['OR'][ 'AND*' . $unique_index_name ] = $uniqueness_where_params; |
|
2962 | + $query_params[0]['OR']['AND*'.$unique_index_name] = $uniqueness_where_params; |
|
2963 | 2963 | } |
2964 | 2964 | // if there is nothing to base this search on, then we shouldn't find anything |
2965 | 2965 | if (empty($query_params)) { |
@@ -3037,16 +3037,16 @@ discard block |
||
3037 | 3037 | $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
3038 | 3038 | // if the value we want to assign it to is NULL, just don't mention it for the insertion |
3039 | 3039 | if ($prepared_value !== null) { |
3040 | - $insertion_col_n_values[ $field_obj->get_table_column() ] = $prepared_value; |
|
3040 | + $insertion_col_n_values[$field_obj->get_table_column()] = $prepared_value; |
|
3041 | 3041 | $format_for_insertion[] = $field_obj->get_wpdb_data_type(); |
3042 | 3042 | } |
3043 | 3043 | } |
3044 | 3044 | if ($table instanceof EE_Secondary_Table && $new_id) { |
3045 | 3045 | // its not the main table, so we should have already saved the main table's PK which we just inserted |
3046 | 3046 | // so add the fk to the main table as a column |
3047 | - $insertion_col_n_values[ $table->get_fk_on_table() ] = $new_id; |
|
3047 | + $insertion_col_n_values[$table->get_fk_on_table()] = $new_id; |
|
3048 | 3048 | $format_for_insertion[] = |
3049 | - '%d';// yes right now we're only allowing these foreign keys to be INTs |
|
3049 | + '%d'; // yes right now we're only allowing these foreign keys to be INTs |
|
3050 | 3050 | } |
3051 | 3051 | // insert the new entry |
3052 | 3052 | $result = $this->_do_wpdb_query( |
@@ -3063,7 +3063,7 @@ discard block |
||
3063 | 3063 | } |
3064 | 3064 | // it's not an auto-increment primary key, so |
3065 | 3065 | // it must have been supplied |
3066 | - return $fields_n_values[ $this->get_primary_key_field()->get_name() ]; |
|
3066 | + return $fields_n_values[$this->get_primary_key_field()->get_name()]; |
|
3067 | 3067 | } |
3068 | 3068 | // we can't return a primary key because there is none. instead return |
3069 | 3069 | // a unique string indicating this model |
@@ -3087,14 +3087,14 @@ discard block |
||
3087 | 3087 | if ( |
3088 | 3088 | ! $field_obj->is_nullable() |
3089 | 3089 | && ( |
3090 | - ! isset($fields_n_values[ $field_obj->get_name() ]) |
|
3091 | - || $fields_n_values[ $field_obj->get_name() ] === null |
|
3090 | + ! isset($fields_n_values[$field_obj->get_name()]) |
|
3091 | + || $fields_n_values[$field_obj->get_name()] === null |
|
3092 | 3092 | ) |
3093 | 3093 | ) { |
3094 | - $fields_n_values[ $field_obj->get_name() ] = $field_obj->get_default_value(); |
|
3094 | + $fields_n_values[$field_obj->get_name()] = $field_obj->get_default_value(); |
|
3095 | 3095 | } |
3096 | - $unprepared_value = isset($fields_n_values[ $field_obj->get_name() ]) |
|
3097 | - ? $fields_n_values[ $field_obj->get_name() ] |
|
3096 | + $unprepared_value = isset($fields_n_values[$field_obj->get_name()]) |
|
3097 | + ? $fields_n_values[$field_obj->get_name()] |
|
3098 | 3098 | : null; |
3099 | 3099 | return $this->_prepare_value_for_use_in_db($unprepared_value, $field_obj); |
3100 | 3100 | } |
@@ -3195,7 +3195,7 @@ discard block |
||
3195 | 3195 | */ |
3196 | 3196 | public function get_table_obj_by_alias($table_alias = '') |
3197 | 3197 | { |
3198 | - return isset($this->_tables[ $table_alias ]) ? $this->_tables[ $table_alias ] : null; |
|
3198 | + return isset($this->_tables[$table_alias]) ? $this->_tables[$table_alias] : null; |
|
3199 | 3199 | } |
3200 | 3200 | |
3201 | 3201 | |
@@ -3209,7 +3209,7 @@ discard block |
||
3209 | 3209 | $other_tables = []; |
3210 | 3210 | foreach ($this->_tables as $table_alias => $table) { |
3211 | 3211 | if ($table instanceof EE_Secondary_Table) { |
3212 | - $other_tables[ $table_alias ] = $table; |
|
3212 | + $other_tables[$table_alias] = $table; |
|
3213 | 3213 | } |
3214 | 3214 | } |
3215 | 3215 | return $other_tables; |
@@ -3224,7 +3224,7 @@ discard block |
||
3224 | 3224 | */ |
3225 | 3225 | public function _get_fields_for_table($table_alias) |
3226 | 3226 | { |
3227 | - return $this->_fields[ $table_alias ]; |
|
3227 | + return $this->_fields[$table_alias]; |
|
3228 | 3228 | } |
3229 | 3229 | |
3230 | 3230 | |
@@ -3253,7 +3253,7 @@ discard block |
||
3253 | 3253 | $query_info_carrier, |
3254 | 3254 | 'group_by' |
3255 | 3255 | ); |
3256 | - } elseif (! empty($query_params['group_by'])) { |
|
3256 | + } elseif ( ! empty($query_params['group_by'])) { |
|
3257 | 3257 | $this->_extract_related_model_info_from_query_param( |
3258 | 3258 | $query_params['group_by'], |
3259 | 3259 | $query_info_carrier, |
@@ -3275,7 +3275,7 @@ discard block |
||
3275 | 3275 | $query_info_carrier, |
3276 | 3276 | 'order_by' |
3277 | 3277 | ); |
3278 | - } elseif (! empty($query_params['order_by'])) { |
|
3278 | + } elseif ( ! empty($query_params['order_by'])) { |
|
3279 | 3279 | $this->_extract_related_model_info_from_query_param( |
3280 | 3280 | $query_params['order_by'], |
3281 | 3281 | $query_info_carrier, |
@@ -3310,7 +3310,7 @@ discard block |
||
3310 | 3310 | EE_Model_Query_Info_Carrier $model_query_info_carrier, |
3311 | 3311 | $query_param_type |
3312 | 3312 | ) { |
3313 | - if (! empty($sub_query_params)) { |
|
3313 | + if ( ! empty($sub_query_params)) { |
|
3314 | 3314 | $sub_query_params = (array) $sub_query_params; |
3315 | 3315 | foreach ($sub_query_params as $param => $possibly_array_of_params) { |
3316 | 3316 | // $param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
@@ -3326,7 +3326,7 @@ discard block |
||
3326 | 3326 | $query_param_sans_stars = |
3327 | 3327 | $this->_remove_stars_and_anything_after_from_condition_query_param_key($param); |
3328 | 3328 | if (in_array($query_param_sans_stars, $this->_logic_query_param_keys, true)) { |
3329 | - if (! is_array($possibly_array_of_params)) { |
|
3329 | + if ( ! is_array($possibly_array_of_params)) { |
|
3330 | 3330 | throw new EE_Error( |
3331 | 3331 | sprintf( |
3332 | 3332 | esc_html__( |
@@ -3352,7 +3352,7 @@ discard block |
||
3352 | 3352 | // then $possible_array_of_params looks something like array('<','DTT_sold',true) |
3353 | 3353 | // indicating that $possible_array_of_params[1] is actually a field name, |
3354 | 3354 | // from which we should extract query parameters! |
3355 | - if (! isset($possibly_array_of_params[0], $possibly_array_of_params[1])) { |
|
3355 | + if ( ! isset($possibly_array_of_params[0], $possibly_array_of_params[1])) { |
|
3356 | 3356 | throw new EE_Error( |
3357 | 3357 | sprintf( |
3358 | 3358 | esc_html__( |
@@ -3391,8 +3391,8 @@ discard block |
||
3391 | 3391 | EE_Model_Query_Info_Carrier $model_query_info_carrier, |
3392 | 3392 | $query_param_type |
3393 | 3393 | ) { |
3394 | - if (! empty($sub_query_params)) { |
|
3395 | - if (! is_array($sub_query_params)) { |
|
3394 | + if ( ! empty($sub_query_params)) { |
|
3395 | + if ( ! is_array($sub_query_params)) { |
|
3396 | 3396 | throw new EE_Error( |
3397 | 3397 | sprintf( |
3398 | 3398 | esc_html__("Query parameter %s should be an array, but it isn't.", "event_espresso"), |
@@ -3429,7 +3429,7 @@ discard block |
||
3429 | 3429 | */ |
3430 | 3430 | public function _create_model_query_info_carrier($query_params) |
3431 | 3431 | { |
3432 | - if (! is_array($query_params)) { |
|
3432 | + if ( ! is_array($query_params)) { |
|
3433 | 3433 | EE_Error::doing_it_wrong( |
3434 | 3434 | 'EEM_Base::_create_model_query_info_carrier', |
3435 | 3435 | sprintf( |
@@ -3462,7 +3462,7 @@ discard block |
||
3462 | 3462 | // only include if related to a cpt where no password has been set |
3463 | 3463 | $query_params[0]['OR*nopassword'] = [ |
3464 | 3464 | $where_param_key_for_password => '', |
3465 | - $where_param_key_for_password . '*' => ['IS_NULL'], |
|
3465 | + $where_param_key_for_password.'*' => ['IS_NULL'], |
|
3466 | 3466 | ]; |
3467 | 3467 | } |
3468 | 3468 | $query_object = $this->_extract_related_models_from_query($query_params); |
@@ -3516,7 +3516,7 @@ discard block |
||
3516 | 3516 | // set limit |
3517 | 3517 | if (array_key_exists('limit', $query_params)) { |
3518 | 3518 | if (is_array($query_params['limit'])) { |
3519 | - if (! isset($query_params['limit'][0], $query_params['limit'][1])) { |
|
3519 | + if ( ! isset($query_params['limit'][0], $query_params['limit'][1])) { |
|
3520 | 3520 | $e = sprintf( |
3521 | 3521 | esc_html__( |
3522 | 3522 | "Invalid DB query. You passed '%s' for the LIMIT, but only the following are valid: an integer, string representing an integer, a string like 'int,int', or an array like array(int,int)", |
@@ -3524,12 +3524,12 @@ discard block |
||
3524 | 3524 | ), |
3525 | 3525 | http_build_query($query_params['limit']) |
3526 | 3526 | ); |
3527 | - throw new EE_Error($e . "|" . $e); |
|
3527 | + throw new EE_Error($e."|".$e); |
|
3528 | 3528 | } |
3529 | 3529 | // they passed us an array for the limit. Assume it's like array(50,25), meaning offset by 50, and get 25 |
3530 | - $query_object->set_limit_sql(" LIMIT " . $query_params['limit'][0] . "," . $query_params['limit'][1]); |
|
3531 | - } elseif (! empty($query_params['limit'])) { |
|
3532 | - $query_object->set_limit_sql(" LIMIT " . $query_params['limit']); |
|
3530 | + $query_object->set_limit_sql(" LIMIT ".$query_params['limit'][0].",".$query_params['limit'][1]); |
|
3531 | + } elseif ( ! empty($query_params['limit'])) { |
|
3532 | + $query_object->set_limit_sql(" LIMIT ".$query_params['limit']); |
|
3533 | 3533 | } |
3534 | 3534 | } |
3535 | 3535 | // set order by |
@@ -3561,10 +3561,10 @@ discard block |
||
3561 | 3561 | $order_array = []; |
3562 | 3562 | foreach ($query_params['order_by'] as $field_name_to_order_by => $order) { |
3563 | 3563 | $order = $this->_extract_order($order); |
3564 | - $order_array[] = $this->_deduce_column_name_from_query_param($field_name_to_order_by) . SP . $order; |
|
3564 | + $order_array[] = $this->_deduce_column_name_from_query_param($field_name_to_order_by).SP.$order; |
|
3565 | 3565 | } |
3566 | - $query_object->set_order_by_sql(" ORDER BY " . implode(",", $order_array)); |
|
3567 | - } elseif (! empty($query_params['order_by'])) { |
|
3566 | + $query_object->set_order_by_sql(" ORDER BY ".implode(",", $order_array)); |
|
3567 | + } elseif ( ! empty($query_params['order_by'])) { |
|
3568 | 3568 | $this->_extract_related_model_info_from_query_param( |
3569 | 3569 | $query_params['order_by'], |
3570 | 3570 | $query_object, |
@@ -3575,7 +3575,7 @@ discard block |
||
3575 | 3575 | ? $this->_extract_order($query_params['order']) |
3576 | 3576 | : 'DESC'; |
3577 | 3577 | $query_object->set_order_by_sql( |
3578 | - " ORDER BY " . $this->_deduce_column_name_from_query_param($query_params['order_by']) . SP . $order |
|
3578 | + " ORDER BY ".$this->_deduce_column_name_from_query_param($query_params['order_by']).SP.$order |
|
3579 | 3579 | ); |
3580 | 3580 | } |
3581 | 3581 | } |
@@ -3587,7 +3587,7 @@ discard block |
||
3587 | 3587 | ) { |
3588 | 3588 | $pk_field = $this->get_primary_key_field(); |
3589 | 3589 | $order = $this->_extract_order($query_params['order']); |
3590 | - $query_object->set_order_by_sql(" ORDER BY " . $pk_field->get_qualified_column() . SP . $order); |
|
3590 | + $query_object->set_order_by_sql(" ORDER BY ".$pk_field->get_qualified_column().SP.$order); |
|
3591 | 3591 | } |
3592 | 3592 | // set group by |
3593 | 3593 | if (array_key_exists('group_by', $query_params)) { |
@@ -3597,10 +3597,10 @@ discard block |
||
3597 | 3597 | foreach ($query_params['group_by'] as $field_name_to_group_by) { |
3598 | 3598 | $group_by_array[] = $this->_deduce_column_name_from_query_param($field_name_to_group_by); |
3599 | 3599 | } |
3600 | - $query_object->set_group_by_sql(" GROUP BY " . implode(", ", $group_by_array)); |
|
3601 | - } elseif (! empty($query_params['group_by'])) { |
|
3600 | + $query_object->set_group_by_sql(" GROUP BY ".implode(", ", $group_by_array)); |
|
3601 | + } elseif ( ! empty($query_params['group_by'])) { |
|
3602 | 3602 | $query_object->set_group_by_sql( |
3603 | - " GROUP BY " . $this->_deduce_column_name_from_query_param($query_params['group_by']) |
|
3603 | + " GROUP BY ".$this->_deduce_column_name_from_query_param($query_params['group_by']) |
|
3604 | 3604 | ); |
3605 | 3605 | } |
3606 | 3606 | } |
@@ -3610,7 +3610,7 @@ discard block |
||
3610 | 3610 | } |
3611 | 3611 | // now, just verify they didn't pass anything wack |
3612 | 3612 | foreach ($query_params as $query_key => $query_value) { |
3613 | - if (! in_array($query_key, $this->_allowed_query_params, true)) { |
|
3613 | + if ( ! in_array($query_key, $this->_allowed_query_params, true)) { |
|
3614 | 3614 | throw new EE_Error( |
3615 | 3615 | sprintf( |
3616 | 3616 | esc_html__( |
@@ -3714,7 +3714,7 @@ discard block |
||
3714 | 3714 | $where_query_params = [] |
3715 | 3715 | ) { |
3716 | 3716 | $allowed_used_default_where_conditions_values = EEM_Base::valid_default_where_conditions(); |
3717 | - if (! in_array($use_default_where_conditions, $allowed_used_default_where_conditions_values)) { |
|
3717 | + if ( ! in_array($use_default_where_conditions, $allowed_used_default_where_conditions_values)) { |
|
3718 | 3718 | throw new EE_Error( |
3719 | 3719 | sprintf( |
3720 | 3720 | esc_html__( |
@@ -3744,7 +3744,7 @@ discard block |
||
3744 | 3744 | // we don't want to add full or even minimum default where conditions from this model, so just continue |
3745 | 3745 | continue; |
3746 | 3746 | } |
3747 | - $overrides = $this->_override_defaults_or_make_null_friendly( |
|
3747 | + $overrides = $this->_override_defaults_or_make_null_friendly( |
|
3748 | 3748 | $related_model_universal_where_params, |
3749 | 3749 | $where_query_params, |
3750 | 3750 | $related_model, |
@@ -3853,19 +3853,19 @@ discard block |
||
3853 | 3853 | ) { |
3854 | 3854 | $null_friendly_where_conditions = []; |
3855 | 3855 | $none_overridden = true; |
3856 | - $or_condition_key_for_defaults = 'OR*' . get_class($model); |
|
3856 | + $or_condition_key_for_defaults = 'OR*'.get_class($model); |
|
3857 | 3857 | foreach ($default_where_conditions as $key => $val) { |
3858 | - if (isset($provided_where_conditions[ $key ])) { |
|
3858 | + if (isset($provided_where_conditions[$key])) { |
|
3859 | 3859 | $none_overridden = false; |
3860 | 3860 | } else { |
3861 | - $null_friendly_where_conditions[ $or_condition_key_for_defaults ]['AND'][ $key ] = $val; |
|
3861 | + $null_friendly_where_conditions[$or_condition_key_for_defaults]['AND'][$key] = $val; |
|
3862 | 3862 | } |
3863 | 3863 | } |
3864 | 3864 | if ($none_overridden && $default_where_conditions) { |
3865 | 3865 | if ($model->has_primary_key_field()) { |
3866 | - $null_friendly_where_conditions[ $or_condition_key_for_defaults ][ $model_relation_path |
|
3866 | + $null_friendly_where_conditions[$or_condition_key_for_defaults][$model_relation_path |
|
3867 | 3867 | . "." |
3868 | - . $model->primary_key_name() ] = |
|
3868 | + . $model->primary_key_name()] = |
|
3869 | 3869 | ['IS NULL']; |
3870 | 3870 | }/*else{ |
3871 | 3871 | //@todo NO PK, use other defaults |
@@ -3974,7 +3974,7 @@ discard block |
||
3974 | 3974 | foreach ($tables as $table_obj) { |
3975 | 3975 | $qualified_pk_column = $table_alias_with_model_relation_chain_prefix |
3976 | 3976 | . $table_obj->get_fully_qualified_pk_column(); |
3977 | - if (! in_array($qualified_pk_column, $selects)) { |
|
3977 | + if ( ! in_array($qualified_pk_column, $selects)) { |
|
3978 | 3978 | $selects[] = "$qualified_pk_column AS '$qualified_pk_column'"; |
3979 | 3979 | } |
3980 | 3980 | } |
@@ -4126,9 +4126,9 @@ discard block |
||
4126 | 4126 | $query_parameter_type |
4127 | 4127 | ) { |
4128 | 4128 | foreach ($this->_model_relations as $valid_related_model_name => $relation_obj) { |
4129 | - if (strpos($possible_join_string, $valid_related_model_name . ".") === 0) { |
|
4129 | + if (strpos($possible_join_string, $valid_related_model_name.".") === 0) { |
|
4130 | 4130 | $this->_add_join_to_model($valid_related_model_name, $query_info_carrier, $original_query_param); |
4131 | - $possible_join_string = substr($possible_join_string, strlen($valid_related_model_name . ".")); |
|
4131 | + $possible_join_string = substr($possible_join_string, strlen($valid_related_model_name.".")); |
|
4132 | 4132 | if ($possible_join_string === '') { |
4133 | 4133 | // nothing left to $query_param |
4134 | 4134 | // we should actually end in a field name, not a model like this! |
@@ -4260,7 +4260,7 @@ discard block |
||
4260 | 4260 | { |
4261 | 4261 | $SQL = $this->_construct_condition_clause_recursive($where_params, ' AND '); |
4262 | 4262 | if ($SQL) { |
4263 | - return " WHERE " . $SQL; |
|
4263 | + return " WHERE ".$SQL; |
|
4264 | 4264 | } |
4265 | 4265 | return ''; |
4266 | 4266 | } |
@@ -4278,7 +4278,7 @@ discard block |
||
4278 | 4278 | { |
4279 | 4279 | $SQL = $this->_construct_condition_clause_recursive($having_params, ' AND '); |
4280 | 4280 | if ($SQL) { |
4281 | - return " HAVING " . $SQL; |
|
4281 | + return " HAVING ".$SQL; |
|
4282 | 4282 | } |
4283 | 4283 | return ''; |
4284 | 4284 | } |
@@ -4301,7 +4301,7 @@ discard block |
||
4301 | 4301 | $query_param = |
4302 | 4302 | $this->_remove_stars_and_anything_after_from_condition_query_param_key( |
4303 | 4303 | $query_param |
4304 | - );// str_replace("*",'',$query_param); |
|
4304 | + ); // str_replace("*",'',$query_param); |
|
4305 | 4305 | if (in_array($query_param, $this->_logic_query_param_keys)) { |
4306 | 4306 | switch ($query_param) { |
4307 | 4307 | case 'not': |
@@ -4335,7 +4335,7 @@ discard block |
||
4335 | 4335 | } else { |
4336 | 4336 | $field_obj = $this->_deduce_field_from_query_param($query_param); |
4337 | 4337 | // if it's not a normal field, maybe it's a custom selection? |
4338 | - if (! $field_obj) { |
|
4338 | + if ( ! $field_obj) { |
|
4339 | 4339 | if ($this->_custom_selections instanceof CustomSelects) { |
4340 | 4340 | $field_obj = $this->_custom_selections->getDataTypeForAlias($query_param); |
4341 | 4341 | } else { |
@@ -4351,7 +4351,7 @@ discard block |
||
4351 | 4351 | } |
4352 | 4352 | } |
4353 | 4353 | $op_and_value_sql = $this->_construct_op_and_value($op_and_value_or_sub_condition, $field_obj); |
4354 | - $where_clauses[] = $this->_deduce_column_name_from_query_param($query_param) . SP . $op_and_value_sql; |
|
4354 | + $where_clauses[] = $this->_deduce_column_name_from_query_param($query_param).SP.$op_and_value_sql; |
|
4355 | 4355 | } |
4356 | 4356 | } |
4357 | 4357 | return $where_clauses ? implode($glue, $where_clauses) : ''; |
@@ -4373,7 +4373,7 @@ discard block |
||
4373 | 4373 | $field->get_model_name(), |
4374 | 4374 | $query_param |
4375 | 4375 | ); |
4376 | - return $table_alias_prefix . $field->get_qualified_column(); |
|
4376 | + return $table_alias_prefix.$field->get_qualified_column(); |
|
4377 | 4377 | } |
4378 | 4378 | if ( |
4379 | 4379 | $this->_custom_selections instanceof CustomSelects |
@@ -4430,7 +4430,7 @@ discard block |
||
4430 | 4430 | { |
4431 | 4431 | if (is_array($op_and_value)) { |
4432 | 4432 | $operator = isset($op_and_value[0]) ? $this->_prepare_operator_for_sql($op_and_value[0]) : null; |
4433 | - if (! $operator) { |
|
4433 | + if ( ! $operator) { |
|
4434 | 4434 | $php_array_like_string = []; |
4435 | 4435 | foreach ($op_and_value as $key => $value) { |
4436 | 4436 | $php_array_like_string[] = "$key=>$value"; |
@@ -4452,14 +4452,14 @@ discard block |
||
4452 | 4452 | } |
4453 | 4453 | // check to see if the value is actually another field |
4454 | 4454 | if (is_array($op_and_value) && isset($op_and_value[2]) && $op_and_value[2] == true) { |
4455 | - return $operator . SP . $this->_deduce_column_name_from_query_param($value); |
|
4455 | + return $operator.SP.$this->_deduce_column_name_from_query_param($value); |
|
4456 | 4456 | } |
4457 | 4457 | if (in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
4458 | 4458 | // in this case, the value should be an array, or at least a comma-separated list |
4459 | 4459 | // it will need to handle a little differently |
4460 | 4460 | $cleaned_value = $this->_construct_in_value($value, $field_obj); |
4461 | 4461 | // note: $cleaned_value has already been run through $wpdb->prepare() |
4462 | - return $operator . SP . $cleaned_value; |
|
4462 | + return $operator.SP.$cleaned_value; |
|
4463 | 4463 | } |
4464 | 4464 | if (in_array($operator, $this->valid_between_style_operators()) && is_array($value)) { |
4465 | 4465 | // the value should be an array with count of two. |
@@ -4475,7 +4475,7 @@ discard block |
||
4475 | 4475 | ); |
4476 | 4476 | } |
4477 | 4477 | $cleaned_value = $this->_construct_between_value($value, $field_obj); |
4478 | - return $operator . SP . $cleaned_value; |
|
4478 | + return $operator.SP.$cleaned_value; |
|
4479 | 4479 | } |
4480 | 4480 | if (in_array($operator, $this->valid_null_style_operators())) { |
4481 | 4481 | if ($value !== null) { |
@@ -4495,10 +4495,10 @@ discard block |
||
4495 | 4495 | if (in_array($operator, $this->valid_like_style_operators()) && ! is_array($value)) { |
4496 | 4496 | // if the operator is 'LIKE', we want to allow percent signs (%) and not |
4497 | 4497 | // remove other junk. So just treat it as a string. |
4498 | - return $operator . SP . $this->_wpdb_prepare_using_field($value, '%s'); |
|
4498 | + return $operator.SP.$this->_wpdb_prepare_using_field($value, '%s'); |
|
4499 | 4499 | } |
4500 | - if (! in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
4501 | - return $operator . SP . $this->_wpdb_prepare_using_field($value, $field_obj); |
|
4500 | + if ( ! in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
4501 | + return $operator.SP.$this->_wpdb_prepare_using_field($value, $field_obj); |
|
4502 | 4502 | } |
4503 | 4503 | if (in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
4504 | 4504 | throw new EE_Error( |
@@ -4512,7 +4512,7 @@ discard block |
||
4512 | 4512 | ) |
4513 | 4513 | ); |
4514 | 4514 | } |
4515 | - if (! in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
4515 | + if ( ! in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
4516 | 4516 | throw new EE_Error( |
4517 | 4517 | sprintf( |
4518 | 4518 | esc_html__( |
@@ -4551,7 +4551,7 @@ discard block |
||
4551 | 4551 | foreach ($values as $value) { |
4552 | 4552 | $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
4553 | 4553 | } |
4554 | - return $cleaned_values[0] . " AND " . $cleaned_values[1]; |
|
4554 | + return $cleaned_values[0]." AND ".$cleaned_values[1]; |
|
4555 | 4555 | } |
4556 | 4556 | |
4557 | 4557 | |
@@ -4585,11 +4585,11 @@ discard block |
||
4585 | 4585 | // which is effectively equivalent to returning "()". We don't return "(0)" because that only works for auto-incrementing columns |
4586 | 4586 | if (empty($prepped)) { |
4587 | 4587 | $all_fields = $this->field_settings(); |
4588 | - $first_field = reset($all_fields); |
|
4588 | + $first_field = reset($all_fields); |
|
4589 | 4589 | $main_table = $this->_get_main_table(); |
4590 | 4590 | $prepped[] = "SELECT {$first_field->get_table_column()} FROM {$main_table->get_table_name()} WHERE FALSE"; |
4591 | 4591 | } |
4592 | - return '(' . implode(',', $prepped) . ')'; |
|
4592 | + return '('.implode(',', $prepped).')'; |
|
4593 | 4593 | } |
4594 | 4594 | |
4595 | 4595 | |
@@ -4608,7 +4608,7 @@ discard block |
||
4608 | 4608 | $this->_prepare_value_for_use_in_db($value, $field_obj) |
4609 | 4609 | ); |
4610 | 4610 | } //$field_obj should really just be a data type |
4611 | - if (! in_array($field_obj, $this->_valid_wpdb_data_types)) { |
|
4611 | + if ( ! in_array($field_obj, $this->_valid_wpdb_data_types)) { |
|
4612 | 4612 | throw new EE_Error( |
4613 | 4613 | sprintf( |
4614 | 4614 | esc_html__("%s is not a valid wpdb datatype. Valid ones are %s", "event_espresso"), |
@@ -4645,12 +4645,12 @@ discard block |
||
4645 | 4645 | ); |
4646 | 4646 | } |
4647 | 4647 | $number_of_parts = count($query_param_parts); |
4648 | - $last_query_param_part = $query_param_parts[ count($query_param_parts) - 1 ]; |
|
4648 | + $last_query_param_part = $query_param_parts[count($query_param_parts) - 1]; |
|
4649 | 4649 | $field_name = $last_query_param_part; |
4650 | 4650 | $model_obj = $number_of_parts === 1 |
4651 | 4651 | ? $this |
4652 | 4652 | // $number_of_parts >= 2, the last part is the column name, and there are only 2parts. therefore... |
4653 | - : $this->get_related_model_obj($query_param_parts[ $number_of_parts - 2 ]); |
|
4653 | + : $this->get_related_model_obj($query_param_parts[$number_of_parts - 2]); |
|
4654 | 4654 | try { |
4655 | 4655 | return $model_obj->field_settings_for($field_name); |
4656 | 4656 | } catch (EE_Error $e) { |
@@ -4670,7 +4670,7 @@ discard block |
||
4670 | 4670 | public function _get_qualified_column_for_field($field_name) |
4671 | 4671 | { |
4672 | 4672 | $all_fields = $this->field_settings(); |
4673 | - $field = isset($all_fields[ $field_name ]) ? $all_fields[ $field_name ] : false; |
|
4673 | + $field = isset($all_fields[$field_name]) ? $all_fields[$field_name] : false; |
|
4674 | 4674 | if ($field) { |
4675 | 4675 | return $field->get_qualified_column(); |
4676 | 4676 | } |
@@ -4740,10 +4740,10 @@ discard block |
||
4740 | 4740 | */ |
4741 | 4741 | public function get_qualified_columns_for_all_fields($model_relation_chain = '', $return_string = true) |
4742 | 4742 | { |
4743 | - $table_prefix = str_replace('.', '__', $model_relation_chain) . (empty($model_relation_chain) ? '' : '__'); |
|
4743 | + $table_prefix = str_replace('.', '__', $model_relation_chain).(empty($model_relation_chain) ? '' : '__'); |
|
4744 | 4744 | $qualified_columns = []; |
4745 | 4745 | foreach ($this->field_settings() as $field) { |
4746 | - $qualified_columns[] = $table_prefix . $field->get_qualified_column(); |
|
4746 | + $qualified_columns[] = $table_prefix.$field->get_qualified_column(); |
|
4747 | 4747 | } |
4748 | 4748 | return $return_string ? implode(', ', $qualified_columns) : $qualified_columns; |
4749 | 4749 | } |
@@ -4767,11 +4767,11 @@ discard block |
||
4767 | 4767 | if ($table_obj instanceof EE_Primary_Table) { |
4768 | 4768 | $SQL .= $table_alias === $table_obj->get_table_alias() |
4769 | 4769 | ? $table_obj->get_select_join_limit($limit) |
4770 | - : SP . $table_obj->get_table_name() . " AS " . $table_obj->get_table_alias() . SP; |
|
4770 | + : SP.$table_obj->get_table_name()." AS ".$table_obj->get_table_alias().SP; |
|
4771 | 4771 | } elseif ($table_obj instanceof EE_Secondary_Table) { |
4772 | 4772 | $SQL .= $table_alias === $table_obj->get_table_alias() |
4773 | 4773 | ? $table_obj->get_select_join_limit_join($limit) |
4774 | - : SP . $table_obj->get_join_sql($table_alias) . SP; |
|
4774 | + : SP.$table_obj->get_join_sql($table_alias).SP; |
|
4775 | 4775 | } |
4776 | 4776 | } |
4777 | 4777 | return $SQL; |
@@ -4841,7 +4841,7 @@ discard block |
||
4841 | 4841 | foreach ($this->field_settings() as $field_obj) { |
4842 | 4842 | // $data_types[$field_obj->get_table_column()] = $field_obj->get_wpdb_data_type(); |
4843 | 4843 | /** @var $field_obj EE_Model_Field_Base */ |
4844 | - $data_types[ $field_obj->get_qualified_column() ] = $field_obj->get_wpdb_data_type(); |
|
4844 | + $data_types[$field_obj->get_qualified_column()] = $field_obj->get_wpdb_data_type(); |
|
4845 | 4845 | } |
4846 | 4846 | return $data_types; |
4847 | 4847 | } |
@@ -4856,8 +4856,8 @@ discard block |
||
4856 | 4856 | */ |
4857 | 4857 | public function get_related_model_obj($model_name) |
4858 | 4858 | { |
4859 | - $model_classname = "EEM_" . $model_name; |
|
4860 | - if (! class_exists($model_classname)) { |
|
4859 | + $model_classname = "EEM_".$model_name; |
|
4860 | + if ( ! class_exists($model_classname)) { |
|
4861 | 4861 | throw new EE_Error( |
4862 | 4862 | sprintf( |
4863 | 4863 | esc_html__( |
@@ -4869,7 +4869,7 @@ discard block |
||
4869 | 4869 | ) |
4870 | 4870 | ); |
4871 | 4871 | } |
4872 | - return call_user_func($model_classname . "::instance"); |
|
4872 | + return call_user_func($model_classname."::instance"); |
|
4873 | 4873 | } |
4874 | 4874 | |
4875 | 4875 | |
@@ -4896,7 +4896,7 @@ discard block |
||
4896 | 4896 | $belongs_to_relations = []; |
4897 | 4897 | foreach ($this->relation_settings() as $model_name => $relation_obj) { |
4898 | 4898 | if ($relation_obj instanceof EE_Belongs_To_Relation) { |
4899 | - $belongs_to_relations[ $model_name ] = $relation_obj; |
|
4899 | + $belongs_to_relations[$model_name] = $relation_obj; |
|
4900 | 4900 | } |
4901 | 4901 | } |
4902 | 4902 | return $belongs_to_relations; |
@@ -4913,7 +4913,7 @@ discard block |
||
4913 | 4913 | public function related_settings_for($relation_name) |
4914 | 4914 | { |
4915 | 4915 | $relatedModels = $this->relation_settings(); |
4916 | - if (! array_key_exists($relation_name, $relatedModels)) { |
|
4916 | + if ( ! array_key_exists($relation_name, $relatedModels)) { |
|
4917 | 4917 | throw new EE_Error( |
4918 | 4918 | sprintf( |
4919 | 4919 | esc_html__( |
@@ -4926,7 +4926,7 @@ discard block |
||
4926 | 4926 | ) |
4927 | 4927 | ); |
4928 | 4928 | } |
4929 | - return $relatedModels[ $relation_name ]; |
|
4929 | + return $relatedModels[$relation_name]; |
|
4930 | 4930 | } |
4931 | 4931 | |
4932 | 4932 | |
@@ -4942,7 +4942,7 @@ discard block |
||
4942 | 4942 | public function field_settings_for($fieldName, $include_db_only_fields = true) |
4943 | 4943 | { |
4944 | 4944 | $fieldSettings = $this->field_settings($include_db_only_fields); |
4945 | - if (! array_key_exists($fieldName, $fieldSettings)) { |
|
4945 | + if ( ! array_key_exists($fieldName, $fieldSettings)) { |
|
4946 | 4946 | throw new EE_Error( |
4947 | 4947 | sprintf( |
4948 | 4948 | esc_html__("There is no field/column '%s' on '%s'", 'event_espresso'), |
@@ -4951,7 +4951,7 @@ discard block |
||
4951 | 4951 | ) |
4952 | 4952 | ); |
4953 | 4953 | } |
4954 | - return $fieldSettings[ $fieldName ]; |
|
4954 | + return $fieldSettings[$fieldName]; |
|
4955 | 4955 | } |
4956 | 4956 | |
4957 | 4957 | |
@@ -4964,7 +4964,7 @@ discard block |
||
4964 | 4964 | public function has_field($fieldName) |
4965 | 4965 | { |
4966 | 4966 | $fieldSettings = $this->field_settings(true); |
4967 | - if (isset($fieldSettings[ $fieldName ])) { |
|
4967 | + if (isset($fieldSettings[$fieldName])) { |
|
4968 | 4968 | return true; |
4969 | 4969 | } |
4970 | 4970 | return false; |
@@ -4980,7 +4980,7 @@ discard block |
||
4980 | 4980 | public function has_relation($relation_name) |
4981 | 4981 | { |
4982 | 4982 | $relations = $this->relation_settings(); |
4983 | - if (isset($relations[ $relation_name ])) { |
|
4983 | + if (isset($relations[$relation_name])) { |
|
4984 | 4984 | return true; |
4985 | 4985 | } |
4986 | 4986 | return false; |
@@ -5016,7 +5016,7 @@ discard block |
||
5016 | 5016 | break; |
5017 | 5017 | } |
5018 | 5018 | } |
5019 | - if (! $this->_primary_key_field instanceof EE_Primary_Key_Field_Base) { |
|
5019 | + if ( ! $this->_primary_key_field instanceof EE_Primary_Key_Field_Base) { |
|
5020 | 5020 | throw new EE_Error( |
5021 | 5021 | sprintf( |
5022 | 5022 | esc_html__("There is no Primary Key defined on model %s", 'event_espresso'), |
@@ -5076,17 +5076,17 @@ discard block |
||
5076 | 5076 | */ |
5077 | 5077 | public function get_foreign_key_to($model_name) |
5078 | 5078 | { |
5079 | - if (! isset($this->_cache_foreign_key_to_fields[ $model_name ])) { |
|
5079 | + if ( ! isset($this->_cache_foreign_key_to_fields[$model_name])) { |
|
5080 | 5080 | foreach ($this->field_settings() as $field) { |
5081 | 5081 | if ( |
5082 | 5082 | $field instanceof EE_Foreign_Key_Field_Base |
5083 | 5083 | && in_array($model_name, $field->get_model_names_pointed_to()) |
5084 | 5084 | ) { |
5085 | - $this->_cache_foreign_key_to_fields[ $model_name ] = $field; |
|
5085 | + $this->_cache_foreign_key_to_fields[$model_name] = $field; |
|
5086 | 5086 | break; |
5087 | 5087 | } |
5088 | 5088 | } |
5089 | - if (! isset($this->_cache_foreign_key_to_fields[ $model_name ])) { |
|
5089 | + if ( ! isset($this->_cache_foreign_key_to_fields[$model_name])) { |
|
5090 | 5090 | throw new EE_Error( |
5091 | 5091 | sprintf( |
5092 | 5092 | esc_html__( |
@@ -5099,7 +5099,7 @@ discard block |
||
5099 | 5099 | ); |
5100 | 5100 | } |
5101 | 5101 | } |
5102 | - return $this->_cache_foreign_key_to_fields[ $model_name ]; |
|
5102 | + return $this->_cache_foreign_key_to_fields[$model_name]; |
|
5103 | 5103 | } |
5104 | 5104 | |
5105 | 5105 | |
@@ -5115,7 +5115,7 @@ discard block |
||
5115 | 5115 | { |
5116 | 5116 | $table_alias_sans_model_relation_chain_prefix = |
5117 | 5117 | EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($table_alias); |
5118 | - return $this->_tables[ $table_alias_sans_model_relation_chain_prefix ]->get_table_name(); |
|
5118 | + return $this->_tables[$table_alias_sans_model_relation_chain_prefix]->get_table_name(); |
|
5119 | 5119 | } |
5120 | 5120 | |
5121 | 5121 | |
@@ -5133,7 +5133,7 @@ discard block |
||
5133 | 5133 | $this->_cached_fields = []; |
5134 | 5134 | foreach ($this->_fields as $fields_corresponding_to_table) { |
5135 | 5135 | foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
5136 | - $this->_cached_fields[ $field_name ] = $field_obj; |
|
5136 | + $this->_cached_fields[$field_name] = $field_obj; |
|
5137 | 5137 | } |
5138 | 5138 | } |
5139 | 5139 | } |
@@ -5144,8 +5144,8 @@ discard block |
||
5144 | 5144 | foreach ($this->_fields as $fields_corresponding_to_table) { |
5145 | 5145 | foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
5146 | 5146 | /** @var $field_obj EE_Model_Field_Base */ |
5147 | - if (! $field_obj->is_db_only_field()) { |
|
5148 | - $this->_cached_fields_non_db_only[ $field_name ] = $field_obj; |
|
5147 | + if ( ! $field_obj->is_db_only_field()) { |
|
5148 | + $this->_cached_fields_non_db_only[$field_name] = $field_obj; |
|
5149 | 5149 | } |
5150 | 5150 | } |
5151 | 5151 | } |
@@ -5186,12 +5186,12 @@ discard block |
||
5186 | 5186 | $primary_key_field->get_qualified_column(), |
5187 | 5187 | $primary_key_field->get_table_column() |
5188 | 5188 | ); |
5189 | - if ($table_pk_value && isset($array_of_objects[ $table_pk_value ])) { |
|
5189 | + if ($table_pk_value && isset($array_of_objects[$table_pk_value])) { |
|
5190 | 5190 | continue; |
5191 | 5191 | } |
5192 | 5192 | } |
5193 | 5193 | $classInstance = $this->instantiate_class_from_array_or_object($row); |
5194 | - if (! $classInstance) { |
|
5194 | + if ( ! $classInstance) { |
|
5195 | 5195 | throw new EE_Error( |
5196 | 5196 | sprintf( |
5197 | 5197 | esc_html__('Could not create instance of class %s from row %s', 'event_espresso'), |
@@ -5204,7 +5204,7 @@ discard block |
||
5204 | 5204 | $classInstance->set_timezone($this->get_timezone(), true); |
5205 | 5205 | // make sure if there is any timezone setting present that we set the timezone for the object |
5206 | 5206 | $key = $has_primary_key ? $classInstance->ID() : $count_if_model_has_no_primary_key++; |
5207 | - $array_of_objects[ $key ] = $classInstance; |
|
5207 | + $array_of_objects[$key] = $classInstance; |
|
5208 | 5208 | // also, for all the relations of type BelongsTo, see if we can cache |
5209 | 5209 | // those related models |
5210 | 5210 | // (we could do this for other relations too, but if there are conditions |
@@ -5248,9 +5248,9 @@ discard block |
||
5248 | 5248 | $results = []; |
5249 | 5249 | if ($this->_custom_selections instanceof CustomSelects) { |
5250 | 5250 | foreach ($this->_custom_selections->columnAliases() as $alias) { |
5251 | - if (isset($db_results_row[ $alias ])) { |
|
5252 | - $results[ $alias ] = $this->convertValueToDataType( |
|
5253 | - $db_results_row[ $alias ], |
|
5251 | + if (isset($db_results_row[$alias])) { |
|
5252 | + $results[$alias] = $this->convertValueToDataType( |
|
5253 | + $db_results_row[$alias], |
|
5254 | 5254 | $this->_custom_selections->getDataTypeForAlias($alias) |
5255 | 5255 | ); |
5256 | 5256 | } |
@@ -5295,7 +5295,7 @@ discard block |
||
5295 | 5295 | $this_model_fields_and_values = []; |
5296 | 5296 | // setup the row using default values; |
5297 | 5297 | foreach ($this->field_settings() as $field_name => $field_obj) { |
5298 | - $this_model_fields_and_values[ $field_name ] = $field_obj->get_default_value(); |
|
5298 | + $this_model_fields_and_values[$field_name] = $field_obj->get_default_value(); |
|
5299 | 5299 | } |
5300 | 5300 | $className = $this->_get_class_name(); |
5301 | 5301 | return EE_Registry::instance()->load_class( |
@@ -5316,20 +5316,20 @@ discard block |
||
5316 | 5316 | */ |
5317 | 5317 | public function instantiate_class_from_array_or_object($cols_n_values) |
5318 | 5318 | { |
5319 | - if (! is_array($cols_n_values) && is_object($cols_n_values)) { |
|
5319 | + if ( ! is_array($cols_n_values) && is_object($cols_n_values)) { |
|
5320 | 5320 | $cols_n_values = get_object_vars($cols_n_values); |
5321 | 5321 | } |
5322 | 5322 | $primary_key = null; |
5323 | 5323 | // make sure the array only has keys that are fields/columns on this model |
5324 | 5324 | $this_model_fields_n_values = $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
5325 | - if ($this->has_primary_key_field() && isset($this_model_fields_n_values[ $this->primary_key_name() ])) { |
|
5326 | - $primary_key = $this_model_fields_n_values[ $this->primary_key_name() ]; |
|
5325 | + if ($this->has_primary_key_field() && isset($this_model_fields_n_values[$this->primary_key_name()])) { |
|
5326 | + $primary_key = $this_model_fields_n_values[$this->primary_key_name()]; |
|
5327 | 5327 | } |
5328 | 5328 | $className = $this->_get_class_name(); |
5329 | 5329 | // check we actually found results that we can use to build our model object |
5330 | 5330 | // if not, return null |
5331 | 5331 | if ($this->has_primary_key_field()) { |
5332 | - if (empty($this_model_fields_n_values[ $this->primary_key_name() ])) { |
|
5332 | + if (empty($this_model_fields_n_values[$this->primary_key_name()])) { |
|
5333 | 5333 | return null; |
5334 | 5334 | } |
5335 | 5335 | } elseif ($this->unique_indexes()) { |
@@ -5341,7 +5341,7 @@ discard block |
||
5341 | 5341 | // if there is no primary key or the object doesn't already exist in the entity map, then create a new instance |
5342 | 5342 | if ($primary_key) { |
5343 | 5343 | $classInstance = $this->get_from_entity_map($primary_key); |
5344 | - if (! $classInstance) { |
|
5344 | + if ( ! $classInstance) { |
|
5345 | 5345 | $classInstance = EE_Registry::instance()->load_class( |
5346 | 5346 | $className, |
5347 | 5347 | [$this_model_fields_n_values, $this->get_timezone()], |
@@ -5371,8 +5371,8 @@ discard block |
||
5371 | 5371 | */ |
5372 | 5372 | public function get_from_entity_map($id) |
5373 | 5373 | { |
5374 | - return isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]) |
|
5375 | - ? $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ] |
|
5374 | + return isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id]) |
|
5375 | + ? $this->_entity_map[EEM_Base::$_model_query_blog_id][$id] |
|
5376 | 5376 | : null; |
5377 | 5377 | } |
5378 | 5378 | |
@@ -5395,7 +5395,7 @@ discard block |
||
5395 | 5395 | public function add_to_entity_map(EE_Base_Class $object) |
5396 | 5396 | { |
5397 | 5397 | $className = $this->_get_class_name(); |
5398 | - if (! $object instanceof $className) { |
|
5398 | + if ( ! $object instanceof $className) { |
|
5399 | 5399 | throw new EE_Error( |
5400 | 5400 | sprintf( |
5401 | 5401 | esc_html__("You tried adding a %s to a mapping of %ss", "event_espresso"), |
@@ -5404,7 +5404,7 @@ discard block |
||
5404 | 5404 | ) |
5405 | 5405 | ); |
5406 | 5406 | } |
5407 | - if (! $object->ID()) { |
|
5407 | + if ( ! $object->ID()) { |
|
5408 | 5408 | throw new DomainException( |
5409 | 5409 | sprintf( |
5410 | 5410 | esc_html__( |
@@ -5420,7 +5420,7 @@ discard block |
||
5420 | 5420 | if ($classInstance) { |
5421 | 5421 | return $classInstance; |
5422 | 5422 | } |
5423 | - $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $object->ID() ] = $object; |
|
5423 | + $this->_entity_map[EEM_Base::$_model_query_blog_id][$object->ID()] = $object; |
|
5424 | 5424 | return $object; |
5425 | 5425 | } |
5426 | 5426 | |
@@ -5435,11 +5435,11 @@ discard block |
||
5435 | 5435 | public function clear_entity_map($id = null) |
5436 | 5436 | { |
5437 | 5437 | if (empty($id)) { |
5438 | - $this->_entity_map[ EEM_Base::$_model_query_blog_id ] = []; |
|
5438 | + $this->_entity_map[EEM_Base::$_model_query_blog_id] = []; |
|
5439 | 5439 | return true; |
5440 | 5440 | } |
5441 | - if (isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ])) { |
|
5442 | - unset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]); |
|
5441 | + if (isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id])) { |
|
5442 | + unset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id]); |
|
5443 | 5443 | return true; |
5444 | 5444 | } |
5445 | 5445 | return false; |
@@ -5484,18 +5484,18 @@ discard block |
||
5484 | 5484 | // there is a primary key on this table and its not set. Use defaults for all its columns |
5485 | 5485 | if ($table_pk_value === null && $table_obj->get_pk_column()) { |
5486 | 5486 | foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
5487 | - if (! $field_obj->is_db_only_field()) { |
|
5487 | + if ( ! $field_obj->is_db_only_field()) { |
|
5488 | 5488 | // prepare field as if its coming from db |
5489 | 5489 | $prepared_value = |
5490 | 5490 | $field_obj->prepare_for_set($field_obj->get_default_value()); |
5491 | - $this_model_fields_n_values[ $field_name ] = $field_obj->prepare_for_use_in_db($prepared_value); |
|
5491 | + $this_model_fields_n_values[$field_name] = $field_obj->prepare_for_use_in_db($prepared_value); |
|
5492 | 5492 | } |
5493 | 5493 | } |
5494 | 5494 | } else { |
5495 | 5495 | // the table's rows existed. Use their values |
5496 | 5496 | foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
5497 | - if (! $field_obj->is_db_only_field()) { |
|
5498 | - $this_model_fields_n_values[ $field_name ] = $this->_get_column_value_with_table_alias_or_not( |
|
5497 | + if ( ! $field_obj->is_db_only_field()) { |
|
5498 | + $this_model_fields_n_values[$field_name] = $this->_get_column_value_with_table_alias_or_not( |
|
5499 | 5499 | $cols_n_values, |
5500 | 5500 | $field_obj->get_qualified_column(), |
5501 | 5501 | $field_obj->get_table_column() |
@@ -5522,17 +5522,17 @@ discard block |
||
5522 | 5522 | // ask the field what it think it's table_name.column_name should be, and call it the "qualified column" |
5523 | 5523 | // does the field on the model relate to this column retrieved from the db? |
5524 | 5524 | // or is it a db-only field? (not relating to the model) |
5525 | - if (isset($cols_n_values[ $qualified_column ])) { |
|
5526 | - $value = $cols_n_values[ $qualified_column ]; |
|
5527 | - } elseif (isset($cols_n_values[ $regular_column ])) { |
|
5528 | - $value = $cols_n_values[ $regular_column ]; |
|
5529 | - } elseif (! empty($this->foreign_key_aliases)) { |
|
5525 | + if (isset($cols_n_values[$qualified_column])) { |
|
5526 | + $value = $cols_n_values[$qualified_column]; |
|
5527 | + } elseif (isset($cols_n_values[$regular_column])) { |
|
5528 | + $value = $cols_n_values[$regular_column]; |
|
5529 | + } elseif ( ! empty($this->foreign_key_aliases)) { |
|
5530 | 5530 | // no PK? ok check if there is a foreign key alias set for this table |
5531 | 5531 | // then check if that alias exists in the incoming data |
5532 | 5532 | // AND that the actual PK the $FK_alias represents matches the $qualified_column (full PK) |
5533 | 5533 | foreach ($this->foreign_key_aliases as $FK_alias => $PK_column) { |
5534 | - if ($PK_column === $qualified_column && isset($cols_n_values[ $FK_alias ])) { |
|
5535 | - $value = $cols_n_values[ $FK_alias ]; |
|
5534 | + if ($PK_column === $qualified_column && isset($cols_n_values[$FK_alias])) { |
|
5535 | + $value = $cols_n_values[$FK_alias]; |
|
5536 | 5536 | [$pk_class] = explode('.', $PK_column); |
5537 | 5537 | $pk_model_name = "EEM_{$pk_class}"; |
5538 | 5538 | /** @var EEM_Base $pk_model */ |
@@ -5576,7 +5576,7 @@ discard block |
||
5576 | 5576 | $obj_in_map->clear_cache($relation_name, null, true); |
5577 | 5577 | } |
5578 | 5578 | } |
5579 | - $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ] = $obj_in_map; |
|
5579 | + $this->_entity_map[EEM_Base::$_model_query_blog_id][$id] = $obj_in_map; |
|
5580 | 5580 | return $obj_in_map; |
5581 | 5581 | } |
5582 | 5582 | return $this->get_one_by_ID($id); |
@@ -5628,7 +5628,7 @@ discard block |
||
5628 | 5628 | */ |
5629 | 5629 | private function _get_class_name() |
5630 | 5630 | { |
5631 | - return "EE_" . $this->get_this_model_name(); |
|
5631 | + return "EE_".$this->get_this_model_name(); |
|
5632 | 5632 | } |
5633 | 5633 | |
5634 | 5634 | |
@@ -5674,7 +5674,7 @@ discard block |
||
5674 | 5674 | { |
5675 | 5675 | $className = get_class($this); |
5676 | 5676 | $tagName = "FHEE__{$className}__{$methodName}"; |
5677 | - if (! has_filter($tagName)) { |
|
5677 | + if ( ! has_filter($tagName)) { |
|
5678 | 5678 | throw new EE_Error( |
5679 | 5679 | sprintf( |
5680 | 5680 | esc_html__( |
@@ -5843,7 +5843,7 @@ discard block |
||
5843 | 5843 | $unique_indexes = []; |
5844 | 5844 | foreach ($this->_indexes as $name => $index) { |
5845 | 5845 | if ($index instanceof EE_Unique_Index) { |
5846 | - $unique_indexes [ $name ] = $index; |
|
5846 | + $unique_indexes [$name] = $index; |
|
5847 | 5847 | } |
5848 | 5848 | } |
5849 | 5849 | return $unique_indexes; |
@@ -5907,7 +5907,7 @@ discard block |
||
5907 | 5907 | $key_values_in_combined_pk = []; |
5908 | 5908 | parse_str($index_primary_key_string, $key_values_in_combined_pk); |
5909 | 5909 | foreach ($key_fields as $key_field_name => $field_obj) { |
5910 | - if (! isset($key_values_in_combined_pk[ $key_field_name ])) { |
|
5910 | + if ( ! isset($key_values_in_combined_pk[$key_field_name])) { |
|
5911 | 5911 | return null; |
5912 | 5912 | } |
5913 | 5913 | } |
@@ -5927,7 +5927,7 @@ discard block |
||
5927 | 5927 | { |
5928 | 5928 | $keys_it_should_have = array_keys($this->get_combined_primary_key_fields()); |
5929 | 5929 | foreach ($keys_it_should_have as $key) { |
5930 | - if (! isset($key_values[ $key ])) { |
|
5930 | + if ( ! isset($key_values[$key])) { |
|
5931 | 5931 | return false; |
5932 | 5932 | } |
5933 | 5933 | } |
@@ -5966,8 +5966,8 @@ discard block |
||
5966 | 5966 | } |
5967 | 5967 | // even copies obviously won't have the same ID, so remove the primary key |
5968 | 5968 | // from the WHERE conditions for finding copies (if there is a primary key, of course) |
5969 | - if ($this->has_primary_key_field() && isset($attributes_array[ $this->primary_key_name() ])) { |
|
5970 | - unset($attributes_array[ $this->primary_key_name() ]); |
|
5969 | + if ($this->has_primary_key_field() && isset($attributes_array[$this->primary_key_name()])) { |
|
5970 | + unset($attributes_array[$this->primary_key_name()]); |
|
5971 | 5971 | } |
5972 | 5972 | if (isset($query_params[0])) { |
5973 | 5973 | $query_params[0] = array_merge($attributes_array, $query_params); |
@@ -5989,7 +5989,7 @@ discard block |
||
5989 | 5989 | */ |
5990 | 5990 | public function get_one_copy($model_object_or_attributes_array, $query_params = []) |
5991 | 5991 | { |
5992 | - if (! is_array($query_params)) { |
|
5992 | + if ( ! is_array($query_params)) { |
|
5993 | 5993 | EE_Error::doing_it_wrong( |
5994 | 5994 | 'EEM_Base::get_one_copy', |
5995 | 5995 | sprintf( |
@@ -6039,7 +6039,7 @@ discard block |
||
6039 | 6039 | private function _prepare_operator_for_sql($operator_supplied) |
6040 | 6040 | { |
6041 | 6041 | $sql_operator = |
6042 | - isset($this->_valid_operators[ $operator_supplied ]) ? $this->_valid_operators[ $operator_supplied ] |
|
6042 | + isset($this->_valid_operators[$operator_supplied]) ? $this->_valid_operators[$operator_supplied] |
|
6043 | 6043 | : null; |
6044 | 6044 | if ($sql_operator) { |
6045 | 6045 | return $sql_operator; |
@@ -6138,7 +6138,7 @@ discard block |
||
6138 | 6138 | $objs = $this->get_all($query_params); |
6139 | 6139 | $names = []; |
6140 | 6140 | foreach ($objs as $obj) { |
6141 | - $names[ $obj->ID() ] = $obj->name(); |
|
6141 | + $names[$obj->ID()] = $obj->name(); |
|
6142 | 6142 | } |
6143 | 6143 | return $names; |
6144 | 6144 | } |
@@ -6158,7 +6158,7 @@ discard block |
||
6158 | 6158 | */ |
6159 | 6159 | public function get_IDs($model_objects, $filter_out_empty_ids = false) |
6160 | 6160 | { |
6161 | - if (! $this->has_primary_key_field()) { |
|
6161 | + if ( ! $this->has_primary_key_field()) { |
|
6162 | 6162 | if (WP_DEBUG) { |
6163 | 6163 | EE_Error::add_error( |
6164 | 6164 | esc_html__('Trying to get IDs from a model than has no primary key', 'event_espresso'), |
@@ -6171,7 +6171,7 @@ discard block |
||
6171 | 6171 | $IDs = []; |
6172 | 6172 | foreach ($model_objects as $model_object) { |
6173 | 6173 | $id = $model_object->ID(); |
6174 | - if (! $id) { |
|
6174 | + if ( ! $id) { |
|
6175 | 6175 | if ($filter_out_empty_ids) { |
6176 | 6176 | continue; |
6177 | 6177 | } |
@@ -6221,22 +6221,22 @@ discard block |
||
6221 | 6221 | EEM_Base::verify_is_valid_cap_context($context); |
6222 | 6222 | // check if we ought to run the restriction generator first |
6223 | 6223 | if ( |
6224 | - isset($this->_cap_restriction_generators[ $context ]) |
|
6225 | - && $this->_cap_restriction_generators[ $context ] instanceof EE_Restriction_Generator_Base |
|
6226 | - && ! $this->_cap_restriction_generators[ $context ]->has_generated_cap_restrictions() |
|
6224 | + isset($this->_cap_restriction_generators[$context]) |
|
6225 | + && $this->_cap_restriction_generators[$context] instanceof EE_Restriction_Generator_Base |
|
6226 | + && ! $this->_cap_restriction_generators[$context]->has_generated_cap_restrictions() |
|
6227 | 6227 | ) { |
6228 | - $this->_cap_restrictions[ $context ] = array_merge( |
|
6229 | - $this->_cap_restrictions[ $context ], |
|
6230 | - $this->_cap_restriction_generators[ $context ]->generate_restrictions() |
|
6228 | + $this->_cap_restrictions[$context] = array_merge( |
|
6229 | + $this->_cap_restrictions[$context], |
|
6230 | + $this->_cap_restriction_generators[$context]->generate_restrictions() |
|
6231 | 6231 | ); |
6232 | 6232 | } |
6233 | 6233 | // and make sure we've finalized the construction of each restriction |
6234 | - foreach ($this->_cap_restrictions[ $context ] as $where_conditions_obj) { |
|
6234 | + foreach ($this->_cap_restrictions[$context] as $where_conditions_obj) { |
|
6235 | 6235 | if ($where_conditions_obj instanceof EE_Default_Where_Conditions) { |
6236 | 6236 | $where_conditions_obj->_finalize_construct($this); |
6237 | 6237 | } |
6238 | 6238 | } |
6239 | - return $this->_cap_restrictions[ $context ]; |
|
6239 | + return $this->_cap_restrictions[$context]; |
|
6240 | 6240 | } |
6241 | 6241 | |
6242 | 6242 | |
@@ -6266,9 +6266,9 @@ discard block |
||
6266 | 6266 | foreach ($cap_restrictions as $cap => $restriction_if_no_cap) { |
6267 | 6267 | if ( |
6268 | 6268 | ! EE_Capabilities::instance() |
6269 | - ->current_user_can($cap, $this->get_this_model_name() . '_model_applying_caps') |
|
6269 | + ->current_user_can($cap, $this->get_this_model_name().'_model_applying_caps') |
|
6270 | 6270 | ) { |
6271 | - $missing_caps[ $cap ] = $restriction_if_no_cap; |
|
6271 | + $missing_caps[$cap] = $restriction_if_no_cap; |
|
6272 | 6272 | } |
6273 | 6273 | } |
6274 | 6274 | return $missing_caps; |
@@ -6301,8 +6301,8 @@ discard block |
||
6301 | 6301 | public function cap_action_for_context($context) |
6302 | 6302 | { |
6303 | 6303 | $mapping = $this->cap_contexts_to_cap_action_map(); |
6304 | - if (isset($mapping[ $context ])) { |
|
6305 | - return $mapping[ $context ]; |
|
6304 | + if (isset($mapping[$context])) { |
|
6305 | + return $mapping[$context]; |
|
6306 | 6306 | } |
6307 | 6307 | if ($action = apply_filters('FHEE__EEM_Base__cap_action_for_context', null, $this, $mapping, $context)) { |
6308 | 6308 | return $action; |
@@ -6423,7 +6423,7 @@ discard block |
||
6423 | 6423 | foreach ($this->logic_query_param_keys() as $logic_query_param_key) { |
6424 | 6424 | if ( |
6425 | 6425 | $query_param_key === $logic_query_param_key |
6426 | - || strpos($query_param_key, $logic_query_param_key . '*') === 0 |
|
6426 | + || strpos($query_param_key, $logic_query_param_key.'*') === 0 |
|
6427 | 6427 | ) { |
6428 | 6428 | return true; |
6429 | 6429 | } |
@@ -6481,7 +6481,7 @@ discard block |
||
6481 | 6481 | if ($password_field instanceof EE_Password_Field) { |
6482 | 6482 | $field_names = $password_field->protectedFields(); |
6483 | 6483 | foreach ($field_names as $field_name) { |
6484 | - $fields[ $field_name ] = $this->field_settings_for($field_name); |
|
6484 | + $fields[$field_name] = $this->field_settings_for($field_name); |
|
6485 | 6485 | } |
6486 | 6486 | } |
6487 | 6487 | return $fields; |
@@ -6507,7 +6507,7 @@ discard block |
||
6507 | 6507 | if ($model_obj_or_fields_n_values instanceof EE_Base_Class) { |
6508 | 6508 | $model_obj_or_fields_n_values = $model_obj_or_fields_n_values->model_field_array(); |
6509 | 6509 | } |
6510 | - if (! is_array($model_obj_or_fields_n_values)) { |
|
6510 | + if ( ! is_array($model_obj_or_fields_n_values)) { |
|
6511 | 6511 | throw new UnexpectedEntityException( |
6512 | 6512 | $model_obj_or_fields_n_values, |
6513 | 6513 | 'EE_Base_Class', |
@@ -6587,7 +6587,7 @@ discard block |
||
6587 | 6587 | ) |
6588 | 6588 | ); |
6589 | 6589 | } |
6590 | - return ($this->model_chain_to_password ? $this->model_chain_to_password . '.' : '') . $password_field_name; |
|
6590 | + return ($this->model_chain_to_password ? $this->model_chain_to_password.'.' : '').$password_field_name; |
|
6591 | 6591 | } |
6592 | 6592 | |
6593 | 6593 |
@@ -17,552 +17,552 @@ |
||
17 | 17 | abstract class EE_Model_Relation_Base implements HasSchemaInterface |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var bool |
|
22 | - */ |
|
23 | - protected $_blocking_delete = false; |
|
24 | - |
|
25 | - /** |
|
26 | - * If you try to delete "this_model", and there are related "other_models", |
|
27 | - * and this isn't null, then abandon the deletion and add this warning. |
|
28 | - * This effectively makes it impossible to delete "this_model" while there are |
|
29 | - * related "other_models" along this relation. |
|
30 | - * |
|
31 | - * @var string (internationalized) |
|
32 | - */ |
|
33 | - protected $_blocking_delete_error_message; |
|
34 | - |
|
35 | - /** |
|
36 | - * The model name pointed to by this relation (ie, the model we want to establish a relationship to) |
|
37 | - * |
|
38 | - * @var string eg Event, Question_Group, Registration |
|
39 | - */ |
|
40 | - private $_other_model_name; |
|
41 | - |
|
42 | - /** |
|
43 | - * The model name of which this relation is a component (ie, the model that called new EE_Model_Relation_Base) |
|
44 | - * |
|
45 | - * @var string eg Event, Question_Group, Registration |
|
46 | - */ |
|
47 | - private $_this_model_name; |
|
48 | - |
|
49 | - /** |
|
50 | - * this is typically used when calling the relation models to make sure they inherit any set timezone from the |
|
51 | - * initiating model. |
|
52 | - * |
|
53 | - * @var string |
|
54 | - */ |
|
55 | - protected $_timezone; |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * Object representing the relationship between two models. This knows how to join the models, |
|
60 | - * get related models across the relation, and add-and-remove the relationships. |
|
61 | - * |
|
62 | - * @param boolean $block_deletes if there are related models across this relation, block (prevent |
|
63 | - * and add an error) the deletion of this model |
|
64 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
65 | - * default |
|
66 | - */ |
|
67 | - public function __construct($block_deletes, $blocking_delete_error_message) |
|
68 | - { |
|
69 | - $this->_blocking_delete = $block_deletes; |
|
70 | - $this->_blocking_delete_error_message = $blocking_delete_error_message; |
|
71 | - } |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * @param $this_model_name |
|
76 | - * @param $other_model_name |
|
77 | - * @throws EE_Error |
|
78 | - */ |
|
79 | - public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
80 | - { |
|
81 | - $this->_this_model_name = $this_model_name; |
|
82 | - $this->_other_model_name = $other_model_name; |
|
83 | - if (is_string($this->_blocking_delete)) { |
|
84 | - throw new EE_Error( |
|
85 | - sprintf( |
|
86 | - esc_html__( |
|
87 | - "When instantiating the relation of type %s from %s to %s, the \$block_deletes argument should be a boolean, not a string (%s)", |
|
88 | - "event_espresso" |
|
89 | - ), |
|
90 | - get_class($this), |
|
91 | - $this_model_name, |
|
92 | - $other_model_name, |
|
93 | - $this->_blocking_delete |
|
94 | - ) |
|
95 | - ); |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * entirely possible that relations may be called from a model and we need to make sure those relations have their |
|
102 | - * timezone set correctly. |
|
103 | - * |
|
104 | - * @param string $timezone timezone to set. |
|
105 | - */ |
|
106 | - public function set_timezone(string $timezone) |
|
107 | - { |
|
108 | - if (! empty($timezone)) { |
|
109 | - $this->_timezone = $timezone; |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - |
|
114 | - /** |
|
115 | - * Deletes the related model objects which meet the query parameters. If no |
|
116 | - * parameters are specified, then all related model objects will be deleted. |
|
117 | - * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
118 | - * model objects will only be soft-deleted. |
|
119 | - * |
|
120 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
121 | - * @param array $query_params |
|
122 | - * @return int of how many related models got deleted |
|
123 | - * @throws EE_Error |
|
124 | - * @throws ReflectionException |
|
125 | - */ |
|
126 | - public function delete_all_related($model_object_or_id, $query_params = []) |
|
127 | - { |
|
128 | - // for each thing we would delete, |
|
129 | - $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
130 | - // determine if it's blocked by anything else before it can be deleted |
|
131 | - $deleted_count = 0; |
|
132 | - foreach ($related_model_objects as $related_model_object) { |
|
133 | - $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
134 | - $related_model_object, |
|
135 | - $model_object_or_id |
|
136 | - ); |
|
137 | - /* @var $model_object_or_id EE_Base_Class */ |
|
138 | - if (! $delete_is_blocked) { |
|
139 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
140 | - $related_model_object->delete(); |
|
141 | - $deleted_count++; |
|
142 | - } |
|
143 | - } |
|
144 | - return $deleted_count; |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * Gets all the model objects of type of other model related to $model_object, |
|
150 | - * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation. |
|
151 | - * For both of those child classes, $model_object must be saved so that it has an ID before querying, |
|
152 | - * otherwise an error will be thrown. Note: by default we disable default_where_conditions |
|
153 | - * EE_Belongs_To_Relation doesn't need to be saved before querying. |
|
154 | - * |
|
155 | - * @param EE_Base_Class|int $model_object_or_id or the primary key of this model |
|
156 | - * @param array $query_params @see |
|
157 | - * https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
158 | - * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
159 | - * @return EE_Base_Class[] |
|
160 | - * @throws EE_Error |
|
161 | - * @throws ReflectionException |
|
162 | - */ |
|
163 | - public function get_all_related( |
|
164 | - $model_object_or_id, |
|
165 | - $query_params = [], |
|
166 | - $values_already_prepared_by_model_object = false |
|
167 | - ) { |
|
168 | - if ($values_already_prepared_by_model_object !== false) { |
|
169 | - EE_Error::doing_it_wrong( |
|
170 | - 'EE_Model_Relation_Base::get_all_related', |
|
171 | - esc_html__('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
172 | - '4.8.1' |
|
173 | - ); |
|
174 | - } |
|
175 | - $query_params = |
|
176 | - $this->_disable_default_where_conditions_on_query_param($query_params); |
|
177 | - $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() |
|
178 | - . |
|
179 | - "." |
|
180 | - . |
|
181 | - $this->get_this_model() |
|
182 | - ->get_primary_key_field() |
|
183 | - ->get_name(); |
|
184 | - $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
185 | - $query_params[0][ $query_param_where_this_model_pk ] = $model_object_id; |
|
186 | - return $this->get_other_model()->get_all($query_params); |
|
187 | - } |
|
188 | - |
|
189 | - |
|
190 | - /** |
|
191 | - * Gets the model which this relation establishes the relation TO (ie, |
|
192 | - * this relation object was defined on get_this_model(), get_other_model() is the other one) |
|
193 | - * |
|
194 | - * @return EEM_Base |
|
195 | - * @throws EE_Error |
|
196 | - * @throws ReflectionException |
|
197 | - */ |
|
198 | - public function get_other_model() |
|
199 | - { |
|
200 | - return $this->_get_model($this->_other_model_name); |
|
201 | - } |
|
202 | - |
|
203 | - |
|
204 | - /** |
|
205 | - * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two |
|
206 | - * model objects |
|
207 | - * |
|
208 | - * @param $this_obj_or_id |
|
209 | - * @param $other_obj_or_id |
|
210 | - * @param array $where_query |
|
211 | - * @return bool |
|
212 | - */ |
|
213 | - abstract public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = []); |
|
214 | - |
|
215 | - |
|
216 | - /** |
|
217 | - * Alters the $query_params to disable default where conditions, unless otherwise specified |
|
218 | - * |
|
219 | - * @param array $query_params |
|
220 | - * @return array |
|
221 | - */ |
|
222 | - protected function _disable_default_where_conditions_on_query_param($query_params) |
|
223 | - { |
|
224 | - if (! isset($query_params['default_where_conditions'])) { |
|
225 | - $query_params['default_where_conditions'] = 'none'; |
|
226 | - } |
|
227 | - return $query_params; |
|
228 | - } |
|
229 | - |
|
230 | - |
|
231 | - /** |
|
232 | - * Gets the model where this relation is defined. |
|
233 | - * |
|
234 | - * @return EEM_Base |
|
235 | - * @throws EE_Error |
|
236 | - * @throws ReflectionException |
|
237 | - */ |
|
238 | - public function get_this_model() |
|
239 | - { |
|
240 | - return $this->_get_model($this->_this_model_name); |
|
241 | - } |
|
242 | - |
|
243 | - |
|
244 | - /** |
|
245 | - * this just returns a model_object_id for incoming item that could be an object or id. |
|
246 | - * |
|
247 | - * @param EE_Base_Class|int $model_object_or_id model object or the primary key of this model |
|
248 | - * @return int |
|
249 | - * @throws EE_Error |
|
250 | - * @throws ReflectionException |
|
251 | - */ |
|
252 | - protected function _get_model_object_id($model_object_or_id) |
|
253 | - { |
|
254 | - $model_object_id = $model_object_or_id; |
|
255 | - if ($model_object_or_id instanceof EE_Base_Class) { |
|
256 | - $model_object_id = $model_object_or_id->ID(); |
|
257 | - } |
|
258 | - if (! $model_object_id) { |
|
259 | - throw new EE_Error( |
|
260 | - sprintf( |
|
261 | - esc_html__( |
|
262 | - "Sorry, we cant get the related %s model objects to %s model object before it has an ID. You can solve that by just saving it before trying to get its related model objects", |
|
263 | - "event_espresso" |
|
264 | - ), |
|
265 | - $this->get_other_model()->get_this_model_name(), |
|
266 | - $this->get_this_model()->get_this_model_name() |
|
267 | - ) |
|
268 | - ); |
|
269 | - } |
|
270 | - return $model_object_id; |
|
271 | - } |
|
272 | - |
|
273 | - |
|
274 | - /** |
|
275 | - * Internally used by get_this_model() and get_other_model() |
|
276 | - * |
|
277 | - * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
|
278 | - * @return EEM_Base |
|
279 | - * @throws EE_Error |
|
280 | - * @throws ReflectionException |
|
281 | - */ |
|
282 | - protected function _get_model($model_name) |
|
283 | - { |
|
284 | - $modelInstance = EE_Registry::instance()->load_model($model_name); |
|
285 | - $modelInstance->set_timezone($this->_timezone); |
|
286 | - return $modelInstance; |
|
287 | - } |
|
288 | - |
|
289 | - |
|
290 | - /** |
|
291 | - * Deletes the related model objects which meet the query parameters. If no |
|
292 | - * parameters are specified, then all related model objects will be deleted. |
|
293 | - * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
294 | - * model objects will only be soft-deleted. |
|
295 | - * |
|
296 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
297 | - * @param array $query_params |
|
298 | - * @return int of how many related models got deleted |
|
299 | - * @throws EE_Error |
|
300 | - * @throws ReflectionException |
|
301 | - */ |
|
302 | - public function delete_related_permanently($model_object_or_id, $query_params = []) |
|
303 | - { |
|
304 | - // for each thing we would delete, |
|
305 | - $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
306 | - // determine if it's blocked by anything else before it can be deleted |
|
307 | - $deleted_count = 0; |
|
308 | - foreach ($related_model_objects as $related_model_object) { |
|
309 | - $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
310 | - $related_model_object, |
|
311 | - $model_object_or_id |
|
312 | - ); |
|
313 | - /* @var $model_object_or_id EE_Base_Class */ |
|
314 | - if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
|
315 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
316 | - $deleted_count++; |
|
317 | - if (! $delete_is_blocked) { |
|
318 | - $related_model_object->delete_permanently(); |
|
319 | - } else { |
|
320 | - // delete is blocked |
|
321 | - // brent and darren, in this case, wanted to just soft delete it then |
|
322 | - $related_model_object->delete(); |
|
323 | - } |
|
324 | - } else { |
|
325 | - // its not a soft-deletable thing anyways. do the normal logic. |
|
326 | - if (! $delete_is_blocked) { |
|
327 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
328 | - $related_model_object->delete(); |
|
329 | - $deleted_count++; |
|
330 | - } |
|
331 | - } |
|
332 | - } |
|
333 | - return $deleted_count; |
|
334 | - } |
|
335 | - |
|
336 | - |
|
337 | - /** |
|
338 | - * Gets the SQL string for performing the join between this model and the other model. |
|
339 | - * |
|
340 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
341 | - * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
342 | - * other_model_primary_table.fk" etc |
|
343 | - */ |
|
344 | - abstract public function get_join_statement($model_relation_chain); |
|
345 | - |
|
346 | - |
|
347 | - /** |
|
348 | - * Adds a relationships between the two model objects provided. Each type of relationship handles this differently |
|
349 | - * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this |
|
350 | - * relationship only allows this model to be related to a single other model of this type) |
|
351 | - * |
|
352 | - * @param $this_obj_or_id |
|
353 | - * @param $other_obj_or_id |
|
354 | - * @param array $extra_join_model_fields_n_values |
|
355 | - * @return EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for |
|
356 | - * $other_obj_or_id) |
|
357 | - */ |
|
358 | - abstract public function add_relation_to( |
|
359 | - $this_obj_or_id, |
|
360 | - $other_obj_or_id, |
|
361 | - $extra_join_model_fields_n_values = [] |
|
362 | - ); |
|
363 | - |
|
364 | - |
|
365 | - /** |
|
366 | - * Removes ALL relation instances for this relation obj |
|
367 | - * |
|
368 | - * @param EE_Base_Class|int $this_obj_or_id |
|
369 | - * @param array $where_query_param @see |
|
370 | - * https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
371 | - * @return EE_Base_Class[] |
|
372 | - * @throws EE_Error |
|
373 | - * @throws ReflectionException |
|
374 | - */ |
|
375 | - public function remove_relations($this_obj_or_id, $where_query_param = []) |
|
376 | - { |
|
377 | - $related_things = $this->get_all_related($this_obj_or_id, [$where_query_param]); |
|
378 | - $objs_removed = []; |
|
379 | - foreach ($related_things as $related_thing) { |
|
380 | - $objs_removed[] = $this->remove_relation_to($this_obj_or_id, $related_thing); |
|
381 | - } |
|
382 | - return $objs_removed; |
|
383 | - } |
|
384 | - |
|
385 | - |
|
386 | - /** |
|
387 | - * If you aren't allowed to delete this model when there are related models across this |
|
388 | - * relation object, return true. Otherwise, if you can delete this model even though |
|
389 | - * related objects exist, returns false. |
|
390 | - * |
|
391 | - * @return boolean |
|
392 | - */ |
|
393 | - public function block_delete_if_related_models_exist() |
|
394 | - { |
|
395 | - return $this->_blocking_delete; |
|
396 | - } |
|
397 | - |
|
398 | - |
|
399 | - /** |
|
400 | - * Gets the error message to show |
|
401 | - * |
|
402 | - * @return string |
|
403 | - * @throws EE_Error |
|
404 | - * @throws ReflectionException |
|
405 | - */ |
|
406 | - public function get_deletion_error_message() |
|
407 | - { |
|
408 | - if ($this->_blocking_delete_error_message) { |
|
409 | - return $this->_blocking_delete_error_message; |
|
410 | - } |
|
411 | - return sprintf( |
|
412 | - esc_html__( |
|
413 | - 'This %1$s is currently linked to one or more %2$s records. If this %1$s is incorrect, then please remove it from all %3$s before attempting to delete it.', |
|
414 | - "event_espresso" |
|
415 | - ), |
|
416 | - $this->get_this_model()->item_name(), |
|
417 | - $this->get_other_model()->item_name(), |
|
418 | - $this->get_other_model()->item_name(2) |
|
419 | - ); |
|
420 | - } |
|
421 | - |
|
422 | - |
|
423 | - /** |
|
424 | - * This returns elements used to represent this field in the json schema. |
|
425 | - * |
|
426 | - * @link http://json-schema.org/ |
|
427 | - * @return array |
|
428 | - * @throws EE_Error |
|
429 | - * @throws ReflectionException |
|
430 | - */ |
|
431 | - public function getSchema() |
|
432 | - { |
|
433 | - $schema = [ |
|
434 | - 'description' => $this->getSchemaDescription(), |
|
435 | - 'type' => $this->getSchemaType(), |
|
436 | - 'relation' => true, |
|
437 | - 'relation_type' => get_class($this), |
|
438 | - 'readonly' => $this->getSchemaReadonly(), |
|
439 | - ]; |
|
440 | - |
|
441 | - if ($this instanceof EE_HABTM_Relation) { |
|
442 | - $schema['joining_model_name'] = $this->get_join_model()->get_this_model_name(); |
|
443 | - } |
|
444 | - |
|
445 | - if ($this->getSchemaType() === 'array') { |
|
446 | - $schema['items'] = [ |
|
447 | - 'type' => 'object', |
|
448 | - ]; |
|
449 | - } |
|
450 | - |
|
451 | - return $schema; |
|
452 | - } |
|
453 | - |
|
454 | - |
|
455 | - /** |
|
456 | - * Returns whatever is set as the nice name for the object. |
|
457 | - * |
|
458 | - * @return string |
|
459 | - * @throws EE_Error |
|
460 | - * @throws ReflectionException |
|
461 | - */ |
|
462 | - public function getSchemaDescription() |
|
463 | - { |
|
464 | - $description = $this instanceof EE_Belongs_To_Relation |
|
465 | - ? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso') |
|
466 | - : esc_html__('The related %1$s entities to the %2$s.', 'event_espresso'); |
|
467 | - return sprintf( |
|
468 | - $description, |
|
469 | - $this->get_other_model()->get_this_model_name(), |
|
470 | - $this->get_this_model()->get_this_model_name() |
|
471 | - ); |
|
472 | - } |
|
473 | - |
|
474 | - |
|
475 | - /** |
|
476 | - * If a child class has enum values, they should override this method and provide a simple array |
|
477 | - * of the enum values. |
|
478 | - * The reason this is not a property on the class is because there may be filterable enum values that |
|
479 | - * are set on the instantiated object that could be filtered after construct. |
|
480 | - * |
|
481 | - * @return array |
|
482 | - */ |
|
483 | - public function getSchemaEnum() |
|
484 | - { |
|
485 | - return []; |
|
486 | - } |
|
487 | - |
|
488 | - |
|
489 | - /** |
|
490 | - * This returns the value of the $_schema_format property on the object. |
|
491 | - * |
|
492 | - * @return array |
|
493 | - */ |
|
494 | - public function getSchemaFormat() |
|
495 | - { |
|
496 | - return []; |
|
497 | - } |
|
498 | - |
|
499 | - |
|
500 | - /** |
|
501 | - * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
502 | - * this method and return the properties for the schema. |
|
503 | - * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
504 | - * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
505 | - * |
|
506 | - * @return array |
|
507 | - */ |
|
508 | - public function getSchemaProperties() |
|
509 | - { |
|
510 | - return []; |
|
511 | - } |
|
512 | - |
|
513 | - |
|
514 | - /** |
|
515 | - * This returns the value of the $_schema_readonly property on the object. |
|
516 | - * |
|
517 | - * @return bool |
|
518 | - */ |
|
519 | - public function getSchemaReadonly() |
|
520 | - { |
|
521 | - return true; |
|
522 | - } |
|
523 | - |
|
524 | - |
|
525 | - /** |
|
526 | - * Returns whatever is set as the $_schema_type property for the object. |
|
527 | - * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
528 | - * |
|
529 | - * @return string|array |
|
530 | - */ |
|
531 | - public function getSchemaType() |
|
532 | - { |
|
533 | - return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array'; |
|
534 | - } |
|
535 | - |
|
536 | - |
|
537 | - /** |
|
538 | - * @param $other_table |
|
539 | - * @param $other_table_alias |
|
540 | - * @param $other_table_column |
|
541 | - * @param $this_table_alias |
|
542 | - * @param $this_table_join_column |
|
543 | - * @param string $extra_join_sql |
|
544 | - * @return string |
|
545 | - */ |
|
546 | - protected function _left_join( |
|
547 | - $other_table, |
|
548 | - $other_table_alias, |
|
549 | - $other_table_column, |
|
550 | - $this_table_alias, |
|
551 | - $this_table_join_column, |
|
552 | - $extra_join_sql = '' |
|
553 | - ) { |
|
554 | - return " LEFT JOIN " . |
|
555 | - $other_table . |
|
556 | - " AS " . |
|
557 | - $other_table_alias . |
|
558 | - " ON " . |
|
559 | - $other_table_alias . |
|
560 | - "." . |
|
561 | - $other_table_column . |
|
562 | - "=" . |
|
563 | - $this_table_alias . |
|
564 | - "." . |
|
565 | - $this_table_join_column . |
|
566 | - ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
567 | - } |
|
20 | + /** |
|
21 | + * @var bool |
|
22 | + */ |
|
23 | + protected $_blocking_delete = false; |
|
24 | + |
|
25 | + /** |
|
26 | + * If you try to delete "this_model", and there are related "other_models", |
|
27 | + * and this isn't null, then abandon the deletion and add this warning. |
|
28 | + * This effectively makes it impossible to delete "this_model" while there are |
|
29 | + * related "other_models" along this relation. |
|
30 | + * |
|
31 | + * @var string (internationalized) |
|
32 | + */ |
|
33 | + protected $_blocking_delete_error_message; |
|
34 | + |
|
35 | + /** |
|
36 | + * The model name pointed to by this relation (ie, the model we want to establish a relationship to) |
|
37 | + * |
|
38 | + * @var string eg Event, Question_Group, Registration |
|
39 | + */ |
|
40 | + private $_other_model_name; |
|
41 | + |
|
42 | + /** |
|
43 | + * The model name of which this relation is a component (ie, the model that called new EE_Model_Relation_Base) |
|
44 | + * |
|
45 | + * @var string eg Event, Question_Group, Registration |
|
46 | + */ |
|
47 | + private $_this_model_name; |
|
48 | + |
|
49 | + /** |
|
50 | + * this is typically used when calling the relation models to make sure they inherit any set timezone from the |
|
51 | + * initiating model. |
|
52 | + * |
|
53 | + * @var string |
|
54 | + */ |
|
55 | + protected $_timezone; |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * Object representing the relationship between two models. This knows how to join the models, |
|
60 | + * get related models across the relation, and add-and-remove the relationships. |
|
61 | + * |
|
62 | + * @param boolean $block_deletes if there are related models across this relation, block (prevent |
|
63 | + * and add an error) the deletion of this model |
|
64 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
65 | + * default |
|
66 | + */ |
|
67 | + public function __construct($block_deletes, $blocking_delete_error_message) |
|
68 | + { |
|
69 | + $this->_blocking_delete = $block_deletes; |
|
70 | + $this->_blocking_delete_error_message = $blocking_delete_error_message; |
|
71 | + } |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * @param $this_model_name |
|
76 | + * @param $other_model_name |
|
77 | + * @throws EE_Error |
|
78 | + */ |
|
79 | + public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
80 | + { |
|
81 | + $this->_this_model_name = $this_model_name; |
|
82 | + $this->_other_model_name = $other_model_name; |
|
83 | + if (is_string($this->_blocking_delete)) { |
|
84 | + throw new EE_Error( |
|
85 | + sprintf( |
|
86 | + esc_html__( |
|
87 | + "When instantiating the relation of type %s from %s to %s, the \$block_deletes argument should be a boolean, not a string (%s)", |
|
88 | + "event_espresso" |
|
89 | + ), |
|
90 | + get_class($this), |
|
91 | + $this_model_name, |
|
92 | + $other_model_name, |
|
93 | + $this->_blocking_delete |
|
94 | + ) |
|
95 | + ); |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * entirely possible that relations may be called from a model and we need to make sure those relations have their |
|
102 | + * timezone set correctly. |
|
103 | + * |
|
104 | + * @param string $timezone timezone to set. |
|
105 | + */ |
|
106 | + public function set_timezone(string $timezone) |
|
107 | + { |
|
108 | + if (! empty($timezone)) { |
|
109 | + $this->_timezone = $timezone; |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + |
|
114 | + /** |
|
115 | + * Deletes the related model objects which meet the query parameters. If no |
|
116 | + * parameters are specified, then all related model objects will be deleted. |
|
117 | + * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
118 | + * model objects will only be soft-deleted. |
|
119 | + * |
|
120 | + * @param EE_Base_Class|int|string $model_object_or_id |
|
121 | + * @param array $query_params |
|
122 | + * @return int of how many related models got deleted |
|
123 | + * @throws EE_Error |
|
124 | + * @throws ReflectionException |
|
125 | + */ |
|
126 | + public function delete_all_related($model_object_or_id, $query_params = []) |
|
127 | + { |
|
128 | + // for each thing we would delete, |
|
129 | + $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
130 | + // determine if it's blocked by anything else before it can be deleted |
|
131 | + $deleted_count = 0; |
|
132 | + foreach ($related_model_objects as $related_model_object) { |
|
133 | + $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
134 | + $related_model_object, |
|
135 | + $model_object_or_id |
|
136 | + ); |
|
137 | + /* @var $model_object_or_id EE_Base_Class */ |
|
138 | + if (! $delete_is_blocked) { |
|
139 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
140 | + $related_model_object->delete(); |
|
141 | + $deleted_count++; |
|
142 | + } |
|
143 | + } |
|
144 | + return $deleted_count; |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * Gets all the model objects of type of other model related to $model_object, |
|
150 | + * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation. |
|
151 | + * For both of those child classes, $model_object must be saved so that it has an ID before querying, |
|
152 | + * otherwise an error will be thrown. Note: by default we disable default_where_conditions |
|
153 | + * EE_Belongs_To_Relation doesn't need to be saved before querying. |
|
154 | + * |
|
155 | + * @param EE_Base_Class|int $model_object_or_id or the primary key of this model |
|
156 | + * @param array $query_params @see |
|
157 | + * https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
158 | + * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
159 | + * @return EE_Base_Class[] |
|
160 | + * @throws EE_Error |
|
161 | + * @throws ReflectionException |
|
162 | + */ |
|
163 | + public function get_all_related( |
|
164 | + $model_object_or_id, |
|
165 | + $query_params = [], |
|
166 | + $values_already_prepared_by_model_object = false |
|
167 | + ) { |
|
168 | + if ($values_already_prepared_by_model_object !== false) { |
|
169 | + EE_Error::doing_it_wrong( |
|
170 | + 'EE_Model_Relation_Base::get_all_related', |
|
171 | + esc_html__('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
172 | + '4.8.1' |
|
173 | + ); |
|
174 | + } |
|
175 | + $query_params = |
|
176 | + $this->_disable_default_where_conditions_on_query_param($query_params); |
|
177 | + $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() |
|
178 | + . |
|
179 | + "." |
|
180 | + . |
|
181 | + $this->get_this_model() |
|
182 | + ->get_primary_key_field() |
|
183 | + ->get_name(); |
|
184 | + $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
185 | + $query_params[0][ $query_param_where_this_model_pk ] = $model_object_id; |
|
186 | + return $this->get_other_model()->get_all($query_params); |
|
187 | + } |
|
188 | + |
|
189 | + |
|
190 | + /** |
|
191 | + * Gets the model which this relation establishes the relation TO (ie, |
|
192 | + * this relation object was defined on get_this_model(), get_other_model() is the other one) |
|
193 | + * |
|
194 | + * @return EEM_Base |
|
195 | + * @throws EE_Error |
|
196 | + * @throws ReflectionException |
|
197 | + */ |
|
198 | + public function get_other_model() |
|
199 | + { |
|
200 | + return $this->_get_model($this->_other_model_name); |
|
201 | + } |
|
202 | + |
|
203 | + |
|
204 | + /** |
|
205 | + * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two |
|
206 | + * model objects |
|
207 | + * |
|
208 | + * @param $this_obj_or_id |
|
209 | + * @param $other_obj_or_id |
|
210 | + * @param array $where_query |
|
211 | + * @return bool |
|
212 | + */ |
|
213 | + abstract public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = []); |
|
214 | + |
|
215 | + |
|
216 | + /** |
|
217 | + * Alters the $query_params to disable default where conditions, unless otherwise specified |
|
218 | + * |
|
219 | + * @param array $query_params |
|
220 | + * @return array |
|
221 | + */ |
|
222 | + protected function _disable_default_where_conditions_on_query_param($query_params) |
|
223 | + { |
|
224 | + if (! isset($query_params['default_where_conditions'])) { |
|
225 | + $query_params['default_where_conditions'] = 'none'; |
|
226 | + } |
|
227 | + return $query_params; |
|
228 | + } |
|
229 | + |
|
230 | + |
|
231 | + /** |
|
232 | + * Gets the model where this relation is defined. |
|
233 | + * |
|
234 | + * @return EEM_Base |
|
235 | + * @throws EE_Error |
|
236 | + * @throws ReflectionException |
|
237 | + */ |
|
238 | + public function get_this_model() |
|
239 | + { |
|
240 | + return $this->_get_model($this->_this_model_name); |
|
241 | + } |
|
242 | + |
|
243 | + |
|
244 | + /** |
|
245 | + * this just returns a model_object_id for incoming item that could be an object or id. |
|
246 | + * |
|
247 | + * @param EE_Base_Class|int $model_object_or_id model object or the primary key of this model |
|
248 | + * @return int |
|
249 | + * @throws EE_Error |
|
250 | + * @throws ReflectionException |
|
251 | + */ |
|
252 | + protected function _get_model_object_id($model_object_or_id) |
|
253 | + { |
|
254 | + $model_object_id = $model_object_or_id; |
|
255 | + if ($model_object_or_id instanceof EE_Base_Class) { |
|
256 | + $model_object_id = $model_object_or_id->ID(); |
|
257 | + } |
|
258 | + if (! $model_object_id) { |
|
259 | + throw new EE_Error( |
|
260 | + sprintf( |
|
261 | + esc_html__( |
|
262 | + "Sorry, we cant get the related %s model objects to %s model object before it has an ID. You can solve that by just saving it before trying to get its related model objects", |
|
263 | + "event_espresso" |
|
264 | + ), |
|
265 | + $this->get_other_model()->get_this_model_name(), |
|
266 | + $this->get_this_model()->get_this_model_name() |
|
267 | + ) |
|
268 | + ); |
|
269 | + } |
|
270 | + return $model_object_id; |
|
271 | + } |
|
272 | + |
|
273 | + |
|
274 | + /** |
|
275 | + * Internally used by get_this_model() and get_other_model() |
|
276 | + * |
|
277 | + * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
|
278 | + * @return EEM_Base |
|
279 | + * @throws EE_Error |
|
280 | + * @throws ReflectionException |
|
281 | + */ |
|
282 | + protected function _get_model($model_name) |
|
283 | + { |
|
284 | + $modelInstance = EE_Registry::instance()->load_model($model_name); |
|
285 | + $modelInstance->set_timezone($this->_timezone); |
|
286 | + return $modelInstance; |
|
287 | + } |
|
288 | + |
|
289 | + |
|
290 | + /** |
|
291 | + * Deletes the related model objects which meet the query parameters. If no |
|
292 | + * parameters are specified, then all related model objects will be deleted. |
|
293 | + * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
294 | + * model objects will only be soft-deleted. |
|
295 | + * |
|
296 | + * @param EE_Base_Class|int|string $model_object_or_id |
|
297 | + * @param array $query_params |
|
298 | + * @return int of how many related models got deleted |
|
299 | + * @throws EE_Error |
|
300 | + * @throws ReflectionException |
|
301 | + */ |
|
302 | + public function delete_related_permanently($model_object_or_id, $query_params = []) |
|
303 | + { |
|
304 | + // for each thing we would delete, |
|
305 | + $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
306 | + // determine if it's blocked by anything else before it can be deleted |
|
307 | + $deleted_count = 0; |
|
308 | + foreach ($related_model_objects as $related_model_object) { |
|
309 | + $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
310 | + $related_model_object, |
|
311 | + $model_object_or_id |
|
312 | + ); |
|
313 | + /* @var $model_object_or_id EE_Base_Class */ |
|
314 | + if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
|
315 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
316 | + $deleted_count++; |
|
317 | + if (! $delete_is_blocked) { |
|
318 | + $related_model_object->delete_permanently(); |
|
319 | + } else { |
|
320 | + // delete is blocked |
|
321 | + // brent and darren, in this case, wanted to just soft delete it then |
|
322 | + $related_model_object->delete(); |
|
323 | + } |
|
324 | + } else { |
|
325 | + // its not a soft-deletable thing anyways. do the normal logic. |
|
326 | + if (! $delete_is_blocked) { |
|
327 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
328 | + $related_model_object->delete(); |
|
329 | + $deleted_count++; |
|
330 | + } |
|
331 | + } |
|
332 | + } |
|
333 | + return $deleted_count; |
|
334 | + } |
|
335 | + |
|
336 | + |
|
337 | + /** |
|
338 | + * Gets the SQL string for performing the join between this model and the other model. |
|
339 | + * |
|
340 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
341 | + * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
342 | + * other_model_primary_table.fk" etc |
|
343 | + */ |
|
344 | + abstract public function get_join_statement($model_relation_chain); |
|
345 | + |
|
346 | + |
|
347 | + /** |
|
348 | + * Adds a relationships between the two model objects provided. Each type of relationship handles this differently |
|
349 | + * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this |
|
350 | + * relationship only allows this model to be related to a single other model of this type) |
|
351 | + * |
|
352 | + * @param $this_obj_or_id |
|
353 | + * @param $other_obj_or_id |
|
354 | + * @param array $extra_join_model_fields_n_values |
|
355 | + * @return EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for |
|
356 | + * $other_obj_or_id) |
|
357 | + */ |
|
358 | + abstract public function add_relation_to( |
|
359 | + $this_obj_or_id, |
|
360 | + $other_obj_or_id, |
|
361 | + $extra_join_model_fields_n_values = [] |
|
362 | + ); |
|
363 | + |
|
364 | + |
|
365 | + /** |
|
366 | + * Removes ALL relation instances for this relation obj |
|
367 | + * |
|
368 | + * @param EE_Base_Class|int $this_obj_or_id |
|
369 | + * @param array $where_query_param @see |
|
370 | + * https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
371 | + * @return EE_Base_Class[] |
|
372 | + * @throws EE_Error |
|
373 | + * @throws ReflectionException |
|
374 | + */ |
|
375 | + public function remove_relations($this_obj_or_id, $where_query_param = []) |
|
376 | + { |
|
377 | + $related_things = $this->get_all_related($this_obj_or_id, [$where_query_param]); |
|
378 | + $objs_removed = []; |
|
379 | + foreach ($related_things as $related_thing) { |
|
380 | + $objs_removed[] = $this->remove_relation_to($this_obj_or_id, $related_thing); |
|
381 | + } |
|
382 | + return $objs_removed; |
|
383 | + } |
|
384 | + |
|
385 | + |
|
386 | + /** |
|
387 | + * If you aren't allowed to delete this model when there are related models across this |
|
388 | + * relation object, return true. Otherwise, if you can delete this model even though |
|
389 | + * related objects exist, returns false. |
|
390 | + * |
|
391 | + * @return boolean |
|
392 | + */ |
|
393 | + public function block_delete_if_related_models_exist() |
|
394 | + { |
|
395 | + return $this->_blocking_delete; |
|
396 | + } |
|
397 | + |
|
398 | + |
|
399 | + /** |
|
400 | + * Gets the error message to show |
|
401 | + * |
|
402 | + * @return string |
|
403 | + * @throws EE_Error |
|
404 | + * @throws ReflectionException |
|
405 | + */ |
|
406 | + public function get_deletion_error_message() |
|
407 | + { |
|
408 | + if ($this->_blocking_delete_error_message) { |
|
409 | + return $this->_blocking_delete_error_message; |
|
410 | + } |
|
411 | + return sprintf( |
|
412 | + esc_html__( |
|
413 | + 'This %1$s is currently linked to one or more %2$s records. If this %1$s is incorrect, then please remove it from all %3$s before attempting to delete it.', |
|
414 | + "event_espresso" |
|
415 | + ), |
|
416 | + $this->get_this_model()->item_name(), |
|
417 | + $this->get_other_model()->item_name(), |
|
418 | + $this->get_other_model()->item_name(2) |
|
419 | + ); |
|
420 | + } |
|
421 | + |
|
422 | + |
|
423 | + /** |
|
424 | + * This returns elements used to represent this field in the json schema. |
|
425 | + * |
|
426 | + * @link http://json-schema.org/ |
|
427 | + * @return array |
|
428 | + * @throws EE_Error |
|
429 | + * @throws ReflectionException |
|
430 | + */ |
|
431 | + public function getSchema() |
|
432 | + { |
|
433 | + $schema = [ |
|
434 | + 'description' => $this->getSchemaDescription(), |
|
435 | + 'type' => $this->getSchemaType(), |
|
436 | + 'relation' => true, |
|
437 | + 'relation_type' => get_class($this), |
|
438 | + 'readonly' => $this->getSchemaReadonly(), |
|
439 | + ]; |
|
440 | + |
|
441 | + if ($this instanceof EE_HABTM_Relation) { |
|
442 | + $schema['joining_model_name'] = $this->get_join_model()->get_this_model_name(); |
|
443 | + } |
|
444 | + |
|
445 | + if ($this->getSchemaType() === 'array') { |
|
446 | + $schema['items'] = [ |
|
447 | + 'type' => 'object', |
|
448 | + ]; |
|
449 | + } |
|
450 | + |
|
451 | + return $schema; |
|
452 | + } |
|
453 | + |
|
454 | + |
|
455 | + /** |
|
456 | + * Returns whatever is set as the nice name for the object. |
|
457 | + * |
|
458 | + * @return string |
|
459 | + * @throws EE_Error |
|
460 | + * @throws ReflectionException |
|
461 | + */ |
|
462 | + public function getSchemaDescription() |
|
463 | + { |
|
464 | + $description = $this instanceof EE_Belongs_To_Relation |
|
465 | + ? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso') |
|
466 | + : esc_html__('The related %1$s entities to the %2$s.', 'event_espresso'); |
|
467 | + return sprintf( |
|
468 | + $description, |
|
469 | + $this->get_other_model()->get_this_model_name(), |
|
470 | + $this->get_this_model()->get_this_model_name() |
|
471 | + ); |
|
472 | + } |
|
473 | + |
|
474 | + |
|
475 | + /** |
|
476 | + * If a child class has enum values, they should override this method and provide a simple array |
|
477 | + * of the enum values. |
|
478 | + * The reason this is not a property on the class is because there may be filterable enum values that |
|
479 | + * are set on the instantiated object that could be filtered after construct. |
|
480 | + * |
|
481 | + * @return array |
|
482 | + */ |
|
483 | + public function getSchemaEnum() |
|
484 | + { |
|
485 | + return []; |
|
486 | + } |
|
487 | + |
|
488 | + |
|
489 | + /** |
|
490 | + * This returns the value of the $_schema_format property on the object. |
|
491 | + * |
|
492 | + * @return array |
|
493 | + */ |
|
494 | + public function getSchemaFormat() |
|
495 | + { |
|
496 | + return []; |
|
497 | + } |
|
498 | + |
|
499 | + |
|
500 | + /** |
|
501 | + * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
502 | + * this method and return the properties for the schema. |
|
503 | + * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
504 | + * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
505 | + * |
|
506 | + * @return array |
|
507 | + */ |
|
508 | + public function getSchemaProperties() |
|
509 | + { |
|
510 | + return []; |
|
511 | + } |
|
512 | + |
|
513 | + |
|
514 | + /** |
|
515 | + * This returns the value of the $_schema_readonly property on the object. |
|
516 | + * |
|
517 | + * @return bool |
|
518 | + */ |
|
519 | + public function getSchemaReadonly() |
|
520 | + { |
|
521 | + return true; |
|
522 | + } |
|
523 | + |
|
524 | + |
|
525 | + /** |
|
526 | + * Returns whatever is set as the $_schema_type property for the object. |
|
527 | + * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
528 | + * |
|
529 | + * @return string|array |
|
530 | + */ |
|
531 | + public function getSchemaType() |
|
532 | + { |
|
533 | + return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array'; |
|
534 | + } |
|
535 | + |
|
536 | + |
|
537 | + /** |
|
538 | + * @param $other_table |
|
539 | + * @param $other_table_alias |
|
540 | + * @param $other_table_column |
|
541 | + * @param $this_table_alias |
|
542 | + * @param $this_table_join_column |
|
543 | + * @param string $extra_join_sql |
|
544 | + * @return string |
|
545 | + */ |
|
546 | + protected function _left_join( |
|
547 | + $other_table, |
|
548 | + $other_table_alias, |
|
549 | + $other_table_column, |
|
550 | + $this_table_alias, |
|
551 | + $this_table_join_column, |
|
552 | + $extra_join_sql = '' |
|
553 | + ) { |
|
554 | + return " LEFT JOIN " . |
|
555 | + $other_table . |
|
556 | + " AS " . |
|
557 | + $other_table_alias . |
|
558 | + " ON " . |
|
559 | + $other_table_alias . |
|
560 | + "." . |
|
561 | + $other_table_column . |
|
562 | + "=" . |
|
563 | + $this_table_alias . |
|
564 | + "." . |
|
565 | + $this_table_join_column . |
|
566 | + ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
567 | + } |
|
568 | 568 | } |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | */ |
106 | 106 | public function set_timezone(string $timezone) |
107 | 107 | { |
108 | - if (! empty($timezone)) { |
|
108 | + if ( ! empty($timezone)) { |
|
109 | 109 | $this->_timezone = $timezone; |
110 | 110 | } |
111 | 111 | } |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | $model_object_or_id |
136 | 136 | ); |
137 | 137 | /* @var $model_object_or_id EE_Base_Class */ |
138 | - if (! $delete_is_blocked) { |
|
138 | + if ( ! $delete_is_blocked) { |
|
139 | 139 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
140 | 140 | $related_model_object->delete(); |
141 | 141 | $deleted_count++; |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | ->get_primary_key_field() |
183 | 183 | ->get_name(); |
184 | 184 | $model_object_id = $this->_get_model_object_id($model_object_or_id); |
185 | - $query_params[0][ $query_param_where_this_model_pk ] = $model_object_id; |
|
185 | + $query_params[0][$query_param_where_this_model_pk] = $model_object_id; |
|
186 | 186 | return $this->get_other_model()->get_all($query_params); |
187 | 187 | } |
188 | 188 | |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | */ |
222 | 222 | protected function _disable_default_where_conditions_on_query_param($query_params) |
223 | 223 | { |
224 | - if (! isset($query_params['default_where_conditions'])) { |
|
224 | + if ( ! isset($query_params['default_where_conditions'])) { |
|
225 | 225 | $query_params['default_where_conditions'] = 'none'; |
226 | 226 | } |
227 | 227 | return $query_params; |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | if ($model_object_or_id instanceof EE_Base_Class) { |
256 | 256 | $model_object_id = $model_object_or_id->ID(); |
257 | 257 | } |
258 | - if (! $model_object_id) { |
|
258 | + if ( ! $model_object_id) { |
|
259 | 259 | throw new EE_Error( |
260 | 260 | sprintf( |
261 | 261 | esc_html__( |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
315 | 315 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
316 | 316 | $deleted_count++; |
317 | - if (! $delete_is_blocked) { |
|
317 | + if ( ! $delete_is_blocked) { |
|
318 | 318 | $related_model_object->delete_permanently(); |
319 | 319 | } else { |
320 | 320 | // delete is blocked |
@@ -323,7 +323,7 @@ discard block |
||
323 | 323 | } |
324 | 324 | } else { |
325 | 325 | // its not a soft-deletable thing anyways. do the normal logic. |
326 | - if (! $delete_is_blocked) { |
|
326 | + if ( ! $delete_is_blocked) { |
|
327 | 327 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
328 | 328 | $related_model_object->delete(); |
329 | 329 | $deleted_count++; |
@@ -551,18 +551,18 @@ discard block |
||
551 | 551 | $this_table_join_column, |
552 | 552 | $extra_join_sql = '' |
553 | 553 | ) { |
554 | - return " LEFT JOIN " . |
|
555 | - $other_table . |
|
556 | - " AS " . |
|
557 | - $other_table_alias . |
|
558 | - " ON " . |
|
559 | - $other_table_alias . |
|
560 | - "." . |
|
561 | - $other_table_column . |
|
562 | - "=" . |
|
563 | - $this_table_alias . |
|
564 | - "." . |
|
565 | - $this_table_join_column . |
|
554 | + return " LEFT JOIN ". |
|
555 | + $other_table. |
|
556 | + " AS ". |
|
557 | + $other_table_alias. |
|
558 | + " ON ". |
|
559 | + $other_table_alias. |
|
560 | + ".". |
|
561 | + $other_table_column. |
|
562 | + "=". |
|
563 | + $this_table_alias. |
|
564 | + ".". |
|
565 | + $this_table_join_column. |
|
566 | 566 | ($extra_join_sql ? " AND $extra_join_sql" : ''); |
567 | 567 | } |
568 | 568 | } |
@@ -10,246 +10,246 @@ |
||
10 | 10 | class EEM_Term_Relationship extends EEM_Base |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * @var EEM_Term_Relationship |
|
15 | - */ |
|
16 | - protected static $_instance; |
|
17 | - |
|
18 | - |
|
19 | - /** |
|
20 | - * EEM_Term_Relationship constructor. |
|
21 | - * |
|
22 | - * @param string $timezone |
|
23 | - * @throws EE_Error |
|
24 | - */ |
|
25 | - protected function __construct(string $timezone = '') |
|
26 | - { |
|
27 | - $this->singular_item = esc_html__('Term Relationship', 'event_espresso'); |
|
28 | - $this->plural_item = esc_html__('Term Relationships', 'event_espresso'); |
|
29 | - $this->_tables = [ |
|
30 | - 'Term_Relationship' => new EE_Primary_Table('term_relationships'), |
|
31 | - ]; |
|
32 | - $models_this_can_attach_to = array_keys(EE_Registry::instance()->cpt_models()); |
|
33 | - $this->_fields = [ |
|
34 | - 'Term_Relationship' => [ |
|
35 | - 'object_id' => new EE_Foreign_Key_Int_Field( |
|
36 | - 'object_id', |
|
37 | - esc_html__('Object(Post) ID', 'event_espresso'), |
|
38 | - false, |
|
39 | - 0, |
|
40 | - $models_this_can_attach_to |
|
41 | - ), |
|
42 | - 'term_taxonomy_id' => new EE_Foreign_Key_Int_Field( |
|
43 | - 'term_taxonomy_id', |
|
44 | - esc_html__( |
|
45 | - 'Term (in context of a taxonomy) ID', |
|
46 | - 'event_espresso' |
|
47 | - ), |
|
48 | - false, |
|
49 | - 0, |
|
50 | - 'Term_Taxonomy' |
|
51 | - ), |
|
52 | - 'term_order' => new EE_Integer_Field( |
|
53 | - 'term_order', |
|
54 | - esc_html__('Term Order', 'event_espresso'), |
|
55 | - false, |
|
56 | - 0 |
|
57 | - ), |
|
58 | - ], |
|
59 | - ]; |
|
60 | - $this->_model_relations = [ |
|
61 | - 'Term_Taxonomy' => new EE_Belongs_To_Relation(), |
|
62 | - ]; |
|
63 | - foreach ($models_this_can_attach_to as $model_name) { |
|
64 | - $this->_model_relations[ $model_name ] = new EE_Belongs_To_Relation(); |
|
65 | - } |
|
66 | - $this->_wp_core_model = true; |
|
67 | - $this->_indexes = [ |
|
68 | - 'PRIMARY' => new EE_Primary_Key_Index(['object_id', 'term_taxonomy_id']), |
|
69 | - ]; |
|
70 | - $path_to_event_model = 'Event'; |
|
71 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] |
|
72 | - = new EE_Restriction_Generator_Event_Related_Public( |
|
73 | - $path_to_event_model |
|
74 | - ); |
|
75 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] |
|
76 | - = new EE_Restriction_Generator_Event_Related_Protected( |
|
77 | - $path_to_event_model |
|
78 | - ); |
|
79 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] |
|
80 | - = new EE_Restriction_Generator_Event_Related_Protected( |
|
81 | - $path_to_event_model |
|
82 | - ); |
|
83 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] |
|
84 | - = new EE_Restriction_Generator_Event_Related_Protected( |
|
85 | - $path_to_event_model, |
|
86 | - EEM_Base::caps_edit |
|
87 | - ); |
|
88 | - $path_to_tax_model = 'Term_Taxonomy.'; |
|
89 | - // add cap restrictions for editing term relations to the "ee_assign_*" |
|
90 | - // and for deleting term relations too |
|
91 | - $cap_contexts_affected = [EEM_Base::caps_edit, EEM_Base::caps_delete]; |
|
92 | - foreach ($cap_contexts_affected as $cap_context_affected) { |
|
93 | - $this->_cap_restrictions[ $cap_context_affected ]['ee_assign_event_category'] |
|
94 | - = new EE_Default_Where_Conditions( |
|
95 | - [ |
|
96 | - $path_to_tax_model . 'taxonomy*ee_assign_event_category' => [ |
|
97 | - '!=', |
|
98 | - 'espresso_event_categories', |
|
99 | - ], |
|
100 | - ] |
|
101 | - ); |
|
102 | - $this->_cap_restrictions[ $cap_context_affected ]['ee_assign_venue_category'] |
|
103 | - = new EE_Default_Where_Conditions( |
|
104 | - [ |
|
105 | - $path_to_tax_model . 'taxonomy*ee_assign_venue_category' => [ |
|
106 | - '!=', |
|
107 | - 'espresso_venue_categories', |
|
108 | - ], |
|
109 | - ] |
|
110 | - ); |
|
111 | - $this->_cap_restrictions[ $cap_context_affected ]['ee_assign_event_type'] |
|
112 | - = new EE_Default_Where_Conditions( |
|
113 | - [ |
|
114 | - $path_to_tax_model . 'taxonomy*ee_assign_event_type' => ['!=', 'espresso_event_type'], |
|
115 | - ] |
|
116 | - ); |
|
117 | - } |
|
118 | - parent::__construct($timezone); |
|
119 | - add_filter( |
|
120 | - 'FHEE__Read__create_model_query_params', |
|
121 | - ['EEM_Term_Relationship', 'rest_api_query_params'], |
|
122 | - 10, |
|
123 | - 3 |
|
124 | - ); |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * Makes sure all term-taxonomy counts are correct |
|
130 | - * |
|
131 | - * @param int|null $term_taxonomy_id the id of the term taxonomy to update. If NULL, updates ALL |
|
132 | - * @return int the number of rows affected |
|
133 | - * @throws EE_Error |
|
134 | - * @global wpdb $wpdb |
|
135 | - */ |
|
136 | - public function update_term_taxonomy_counts(int $term_taxonomy_id = 0): int |
|
137 | - { |
|
138 | - // because this uses a subquery and sometimes assigning to column to be another column's |
|
139 | - // value, we just write the SQL directly. |
|
140 | - global $wpdb; |
|
141 | - |
|
142 | - $query = " |
|
13 | + /** |
|
14 | + * @var EEM_Term_Relationship |
|
15 | + */ |
|
16 | + protected static $_instance; |
|
17 | + |
|
18 | + |
|
19 | + /** |
|
20 | + * EEM_Term_Relationship constructor. |
|
21 | + * |
|
22 | + * @param string $timezone |
|
23 | + * @throws EE_Error |
|
24 | + */ |
|
25 | + protected function __construct(string $timezone = '') |
|
26 | + { |
|
27 | + $this->singular_item = esc_html__('Term Relationship', 'event_espresso'); |
|
28 | + $this->plural_item = esc_html__('Term Relationships', 'event_espresso'); |
|
29 | + $this->_tables = [ |
|
30 | + 'Term_Relationship' => new EE_Primary_Table('term_relationships'), |
|
31 | + ]; |
|
32 | + $models_this_can_attach_to = array_keys(EE_Registry::instance()->cpt_models()); |
|
33 | + $this->_fields = [ |
|
34 | + 'Term_Relationship' => [ |
|
35 | + 'object_id' => new EE_Foreign_Key_Int_Field( |
|
36 | + 'object_id', |
|
37 | + esc_html__('Object(Post) ID', 'event_espresso'), |
|
38 | + false, |
|
39 | + 0, |
|
40 | + $models_this_can_attach_to |
|
41 | + ), |
|
42 | + 'term_taxonomy_id' => new EE_Foreign_Key_Int_Field( |
|
43 | + 'term_taxonomy_id', |
|
44 | + esc_html__( |
|
45 | + 'Term (in context of a taxonomy) ID', |
|
46 | + 'event_espresso' |
|
47 | + ), |
|
48 | + false, |
|
49 | + 0, |
|
50 | + 'Term_Taxonomy' |
|
51 | + ), |
|
52 | + 'term_order' => new EE_Integer_Field( |
|
53 | + 'term_order', |
|
54 | + esc_html__('Term Order', 'event_espresso'), |
|
55 | + false, |
|
56 | + 0 |
|
57 | + ), |
|
58 | + ], |
|
59 | + ]; |
|
60 | + $this->_model_relations = [ |
|
61 | + 'Term_Taxonomy' => new EE_Belongs_To_Relation(), |
|
62 | + ]; |
|
63 | + foreach ($models_this_can_attach_to as $model_name) { |
|
64 | + $this->_model_relations[ $model_name ] = new EE_Belongs_To_Relation(); |
|
65 | + } |
|
66 | + $this->_wp_core_model = true; |
|
67 | + $this->_indexes = [ |
|
68 | + 'PRIMARY' => new EE_Primary_Key_Index(['object_id', 'term_taxonomy_id']), |
|
69 | + ]; |
|
70 | + $path_to_event_model = 'Event'; |
|
71 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] |
|
72 | + = new EE_Restriction_Generator_Event_Related_Public( |
|
73 | + $path_to_event_model |
|
74 | + ); |
|
75 | + $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] |
|
76 | + = new EE_Restriction_Generator_Event_Related_Protected( |
|
77 | + $path_to_event_model |
|
78 | + ); |
|
79 | + $this->_cap_restriction_generators[ EEM_Base::caps_edit ] |
|
80 | + = new EE_Restriction_Generator_Event_Related_Protected( |
|
81 | + $path_to_event_model |
|
82 | + ); |
|
83 | + $this->_cap_restriction_generators[ EEM_Base::caps_delete ] |
|
84 | + = new EE_Restriction_Generator_Event_Related_Protected( |
|
85 | + $path_to_event_model, |
|
86 | + EEM_Base::caps_edit |
|
87 | + ); |
|
88 | + $path_to_tax_model = 'Term_Taxonomy.'; |
|
89 | + // add cap restrictions for editing term relations to the "ee_assign_*" |
|
90 | + // and for deleting term relations too |
|
91 | + $cap_contexts_affected = [EEM_Base::caps_edit, EEM_Base::caps_delete]; |
|
92 | + foreach ($cap_contexts_affected as $cap_context_affected) { |
|
93 | + $this->_cap_restrictions[ $cap_context_affected ]['ee_assign_event_category'] |
|
94 | + = new EE_Default_Where_Conditions( |
|
95 | + [ |
|
96 | + $path_to_tax_model . 'taxonomy*ee_assign_event_category' => [ |
|
97 | + '!=', |
|
98 | + 'espresso_event_categories', |
|
99 | + ], |
|
100 | + ] |
|
101 | + ); |
|
102 | + $this->_cap_restrictions[ $cap_context_affected ]['ee_assign_venue_category'] |
|
103 | + = new EE_Default_Where_Conditions( |
|
104 | + [ |
|
105 | + $path_to_tax_model . 'taxonomy*ee_assign_venue_category' => [ |
|
106 | + '!=', |
|
107 | + 'espresso_venue_categories', |
|
108 | + ], |
|
109 | + ] |
|
110 | + ); |
|
111 | + $this->_cap_restrictions[ $cap_context_affected ]['ee_assign_event_type'] |
|
112 | + = new EE_Default_Where_Conditions( |
|
113 | + [ |
|
114 | + $path_to_tax_model . 'taxonomy*ee_assign_event_type' => ['!=', 'espresso_event_type'], |
|
115 | + ] |
|
116 | + ); |
|
117 | + } |
|
118 | + parent::__construct($timezone); |
|
119 | + add_filter( |
|
120 | + 'FHEE__Read__create_model_query_params', |
|
121 | + ['EEM_Term_Relationship', 'rest_api_query_params'], |
|
122 | + 10, |
|
123 | + 3 |
|
124 | + ); |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * Makes sure all term-taxonomy counts are correct |
|
130 | + * |
|
131 | + * @param int|null $term_taxonomy_id the id of the term taxonomy to update. If NULL, updates ALL |
|
132 | + * @return int the number of rows affected |
|
133 | + * @throws EE_Error |
|
134 | + * @global wpdb $wpdb |
|
135 | + */ |
|
136 | + public function update_term_taxonomy_counts(int $term_taxonomy_id = 0): int |
|
137 | + { |
|
138 | + // because this uses a subquery and sometimes assigning to column to be another column's |
|
139 | + // value, we just write the SQL directly. |
|
140 | + global $wpdb; |
|
141 | + |
|
142 | + $query = " |
|
143 | 143 | UPDATE {$wpdb->term_taxonomy} AS tt |
144 | 144 | SET count = ( |
145 | 145 | select count(*) as proper_count from {$wpdb->term_relationships} AS tr |
146 | 146 | WHERE tt.term_taxonomy_id = tr.term_taxonomy_id |
147 | 147 | )"; |
148 | 148 | |
149 | - if ($term_taxonomy_id) { |
|
150 | - $query .= ' WHERE tt.term_taxonomy_id = %d'; |
|
151 | - $query = $wpdb->prepare( |
|
152 | - $query, |
|
153 | - $term_taxonomy_id |
|
154 | - ); |
|
155 | - } |
|
156 | - return $this->_do_wpdb_query( |
|
157 | - 'query', |
|
158 | - [ |
|
159 | - $query, |
|
160 | - ] |
|
161 | - ); |
|
162 | - } |
|
163 | - |
|
164 | - |
|
165 | - /** |
|
166 | - * Overrides the parent to also make sure term-taxonomy counts are up-to-date after |
|
167 | - * inserting |
|
168 | - * |
|
169 | - * @param array $field_n_values @see EEM_Base::insert |
|
170 | - * @return boolean |
|
171 | - * @throws EE_Error |
|
172 | - * @throws ReflectionException |
|
173 | - */ |
|
174 | - public function insert($field_n_values): bool |
|
175 | - { |
|
176 | - $return = parent::insert($field_n_values); |
|
177 | - if (isset($field_n_values['term_taxonomy_id'])) { |
|
178 | - $this->update_term_taxonomy_counts($field_n_values['term_taxonomy_id']); |
|
179 | - } |
|
180 | - return $return; |
|
181 | - } |
|
182 | - |
|
183 | - |
|
184 | - /** |
|
185 | - * Overrides parent so that after an update, we also check the term_taxonomy_counts are |
|
186 | - * all ok |
|
187 | - * |
|
188 | - * @param array $fields_n_values see EEM_Base::update |
|
189 | - * @param array $query_params @see |
|
190 | - * https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
191 | - * @param boolean $keep_model_objs_in_sync if TRUE, makes sure we ALSO update model objects |
|
192 | - * in this model's entity map according to $fields_n_values that match |
|
193 | - * $query_params. This obviously has some overhead, so you can disable it |
|
194 | - * by setting this to FALSE, but be aware that model objects being used |
|
195 | - * could get out-of-sync with the database |
|
196 | - * @return int |
|
197 | - * @throws EE_Error |
|
198 | - * @throws ReflectionException |
|
199 | - */ |
|
200 | - public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true): int |
|
201 | - { |
|
202 | - $count = parent::update($fields_n_values, $query_params, $keep_model_objs_in_sync); |
|
203 | - if ($count) { |
|
204 | - $this->update_term_taxonomy_counts(); |
|
205 | - } |
|
206 | - return $count; |
|
207 | - } |
|
208 | - |
|
209 | - |
|
210 | - /** |
|
211 | - * Overrides parent so that after running this, we also double-check |
|
212 | - * the term taxonomy counts are up-to-date |
|
213 | - * |
|
214 | - * @param array $query_params @see |
|
215 | - * https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
216 | - * @param boolean $allow_blocking |
|
217 | - * @return int @see EEM_Base::delete |
|
218 | - * @throws EE_Error |
|
219 | - * @throws ReflectionException |
|
220 | - */ |
|
221 | - public function delete($query_params, $allow_blocking = true): int |
|
222 | - { |
|
223 | - $count = parent::delete($query_params, $allow_blocking); |
|
224 | - if ($count) { |
|
225 | - $this->update_term_taxonomy_counts(); |
|
226 | - } |
|
227 | - return $count; |
|
228 | - } |
|
229 | - |
|
230 | - |
|
231 | - /** |
|
232 | - * Makes sure that during REST API queries, we only return term relationships |
|
233 | - * for term taxonomies which should be shown in the rest api |
|
234 | - * |
|
235 | - * @param array $model_query_params |
|
236 | - * @param array $querystring_query_params |
|
237 | - * @param EEM_Base $model |
|
238 | - * @return array |
|
239 | - * @throws EE_Error |
|
240 | - * @throws EE_Error |
|
241 | - */ |
|
242 | - public static function rest_api_query_params( |
|
243 | - array $model_query_params, |
|
244 | - array $querystring_query_params, |
|
245 | - EEM_Base $model |
|
246 | - ): array { |
|
247 | - if ($model === EEM_Term_Relationship::instance()) { |
|
248 | - $taxonomies = get_taxonomies(['show_in_rest' => true]); |
|
249 | - if (! empty($taxonomies)) { |
|
250 | - $model_query_params[0]['Term_Taxonomy.taxonomy'] = ['IN', $taxonomies]; |
|
251 | - } |
|
252 | - } |
|
253 | - return $model_query_params; |
|
254 | - } |
|
149 | + if ($term_taxonomy_id) { |
|
150 | + $query .= ' WHERE tt.term_taxonomy_id = %d'; |
|
151 | + $query = $wpdb->prepare( |
|
152 | + $query, |
|
153 | + $term_taxonomy_id |
|
154 | + ); |
|
155 | + } |
|
156 | + return $this->_do_wpdb_query( |
|
157 | + 'query', |
|
158 | + [ |
|
159 | + $query, |
|
160 | + ] |
|
161 | + ); |
|
162 | + } |
|
163 | + |
|
164 | + |
|
165 | + /** |
|
166 | + * Overrides the parent to also make sure term-taxonomy counts are up-to-date after |
|
167 | + * inserting |
|
168 | + * |
|
169 | + * @param array $field_n_values @see EEM_Base::insert |
|
170 | + * @return boolean |
|
171 | + * @throws EE_Error |
|
172 | + * @throws ReflectionException |
|
173 | + */ |
|
174 | + public function insert($field_n_values): bool |
|
175 | + { |
|
176 | + $return = parent::insert($field_n_values); |
|
177 | + if (isset($field_n_values['term_taxonomy_id'])) { |
|
178 | + $this->update_term_taxonomy_counts($field_n_values['term_taxonomy_id']); |
|
179 | + } |
|
180 | + return $return; |
|
181 | + } |
|
182 | + |
|
183 | + |
|
184 | + /** |
|
185 | + * Overrides parent so that after an update, we also check the term_taxonomy_counts are |
|
186 | + * all ok |
|
187 | + * |
|
188 | + * @param array $fields_n_values see EEM_Base::update |
|
189 | + * @param array $query_params @see |
|
190 | + * https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
191 | + * @param boolean $keep_model_objs_in_sync if TRUE, makes sure we ALSO update model objects |
|
192 | + * in this model's entity map according to $fields_n_values that match |
|
193 | + * $query_params. This obviously has some overhead, so you can disable it |
|
194 | + * by setting this to FALSE, but be aware that model objects being used |
|
195 | + * could get out-of-sync with the database |
|
196 | + * @return int |
|
197 | + * @throws EE_Error |
|
198 | + * @throws ReflectionException |
|
199 | + */ |
|
200 | + public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true): int |
|
201 | + { |
|
202 | + $count = parent::update($fields_n_values, $query_params, $keep_model_objs_in_sync); |
|
203 | + if ($count) { |
|
204 | + $this->update_term_taxonomy_counts(); |
|
205 | + } |
|
206 | + return $count; |
|
207 | + } |
|
208 | + |
|
209 | + |
|
210 | + /** |
|
211 | + * Overrides parent so that after running this, we also double-check |
|
212 | + * the term taxonomy counts are up-to-date |
|
213 | + * |
|
214 | + * @param array $query_params @see |
|
215 | + * https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
216 | + * @param boolean $allow_blocking |
|
217 | + * @return int @see EEM_Base::delete |
|
218 | + * @throws EE_Error |
|
219 | + * @throws ReflectionException |
|
220 | + */ |
|
221 | + public function delete($query_params, $allow_blocking = true): int |
|
222 | + { |
|
223 | + $count = parent::delete($query_params, $allow_blocking); |
|
224 | + if ($count) { |
|
225 | + $this->update_term_taxonomy_counts(); |
|
226 | + } |
|
227 | + return $count; |
|
228 | + } |
|
229 | + |
|
230 | + |
|
231 | + /** |
|
232 | + * Makes sure that during REST API queries, we only return term relationships |
|
233 | + * for term taxonomies which should be shown in the rest api |
|
234 | + * |
|
235 | + * @param array $model_query_params |
|
236 | + * @param array $querystring_query_params |
|
237 | + * @param EEM_Base $model |
|
238 | + * @return array |
|
239 | + * @throws EE_Error |
|
240 | + * @throws EE_Error |
|
241 | + */ |
|
242 | + public static function rest_api_query_params( |
|
243 | + array $model_query_params, |
|
244 | + array $querystring_query_params, |
|
245 | + EEM_Base $model |
|
246 | + ): array { |
|
247 | + if ($model === EEM_Term_Relationship::instance()) { |
|
248 | + $taxonomies = get_taxonomies(['show_in_rest' => true]); |
|
249 | + if (! empty($taxonomies)) { |
|
250 | + $model_query_params[0]['Term_Taxonomy.taxonomy'] = ['IN', $taxonomies]; |
|
251 | + } |
|
252 | + } |
|
253 | + return $model_query_params; |
|
254 | + } |
|
255 | 255 | } |
@@ -17,2908 +17,2908 @@ |
||
17 | 17 | class Events_Admin_Page extends EE_Admin_Page_CPT |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * This will hold the event object for event_details screen. |
|
22 | - * |
|
23 | - * @var EE_Event $_event |
|
24 | - */ |
|
25 | - protected $_event; |
|
26 | - |
|
27 | - |
|
28 | - /** |
|
29 | - * This will hold the category object for category_details screen. |
|
30 | - * |
|
31 | - * @var stdClass $_category |
|
32 | - */ |
|
33 | - protected $_category; |
|
34 | - |
|
35 | - |
|
36 | - /** |
|
37 | - * @var EEM_Event $_event_model |
|
38 | - */ |
|
39 | - protected $_event_model; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var EEM_Datetime $datetime_model |
|
43 | - */ |
|
44 | - protected $datetime_model; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var EEM_Ticket $ticket_model |
|
48 | - */ |
|
49 | - protected $ticket_model; |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * @var EE_Event |
|
54 | - */ |
|
55 | - protected $_cpt_model_obj; |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * @var NodeGroupDao |
|
60 | - */ |
|
61 | - protected $model_obj_node_group_persister; |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * Initialize page props for this admin page group. |
|
66 | - */ |
|
67 | - protected function _init_page_props() |
|
68 | - { |
|
69 | - $this->page_slug = EVENTS_PG_SLUG; |
|
70 | - $this->page_label = EVENTS_LABEL; |
|
71 | - $this->_admin_base_url = EVENTS_ADMIN_URL; |
|
72 | - $this->_admin_base_path = EVENTS_ADMIN; |
|
73 | - $this->_cpt_model_names = [ |
|
74 | - 'create_new' => 'EEM_Event', |
|
75 | - 'edit' => 'EEM_Event', |
|
76 | - ]; |
|
77 | - $this->_cpt_edit_routes = [ |
|
78 | - 'espresso_events' => 'edit', |
|
79 | - ]; |
|
80 | - add_action( |
|
81 | - 'AHEE__EE_Admin_Page_CPT__set_model_object__after_set_object', |
|
82 | - [$this, 'verify_event_edit'], |
|
83 | - 10, |
|
84 | - 2 |
|
85 | - ); |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * Sets the ajax hooks used for this admin page group. |
|
91 | - */ |
|
92 | - protected function _ajax_hooks() |
|
93 | - { |
|
94 | - add_action('wp_ajax_ee_save_timezone_setting', [$this, 'save_timezonestring_setting']); |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * Sets the page properties for this admin page group. |
|
100 | - */ |
|
101 | - protected function _define_page_props() |
|
102 | - { |
|
103 | - $this->_admin_page_title = EVENTS_LABEL; |
|
104 | - $this->_labels = [ |
|
105 | - 'buttons' => [ |
|
106 | - 'add' => esc_html__('Add New Event', 'event_espresso'), |
|
107 | - 'edit' => esc_html__('Edit Event', 'event_espresso'), |
|
108 | - 'delete' => esc_html__('Delete Event', 'event_espresso'), |
|
109 | - 'add_category' => esc_html__('Add New Category', 'event_espresso'), |
|
110 | - 'edit_category' => esc_html__('Edit Category', 'event_espresso'), |
|
111 | - 'delete_category' => esc_html__('Delete Category', 'event_espresso'), |
|
112 | - ], |
|
113 | - 'editor_title' => [ |
|
114 | - 'espresso_events' => esc_html__('Enter event title here', 'event_espresso'), |
|
115 | - ], |
|
116 | - 'publishbox' => [ |
|
117 | - 'create_new' => esc_html__('Save New Event', 'event_espresso'), |
|
118 | - 'edit' => esc_html__('Update Event', 'event_espresso'), |
|
119 | - 'add_category' => esc_html__('Save New Category', 'event_espresso'), |
|
120 | - 'edit_category' => esc_html__('Update Category', 'event_espresso'), |
|
121 | - 'template_settings' => esc_html__('Update Settings', 'event_espresso'), |
|
122 | - ], |
|
123 | - ]; |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * Sets the page routes property for this admin page group. |
|
129 | - */ |
|
130 | - protected function _set_page_routes() |
|
131 | - { |
|
132 | - // load formatter helper |
|
133 | - // load field generator helper |
|
134 | - // is there a evt_id in the request? |
|
135 | - $evt_id = ! empty($this->_req_data['EVT_ID']) && ! is_array($this->_req_data['EVT_ID']) |
|
136 | - ? $this->_req_data['EVT_ID'] |
|
137 | - : 0; |
|
138 | - $evt_id = ! empty($this->_req_data['post']) ? $this->_req_data['post'] : $evt_id; |
|
139 | - $this->_page_routes = [ |
|
140 | - 'default' => [ |
|
141 | - 'func' => '_events_overview_list_table', |
|
142 | - 'capability' => 'ee_read_events', |
|
143 | - ], |
|
144 | - 'create_new' => [ |
|
145 | - 'func' => '_create_new_cpt_item', |
|
146 | - 'capability' => 'ee_edit_events', |
|
147 | - ], |
|
148 | - 'edit' => [ |
|
149 | - 'func' => '_edit_cpt_item', |
|
150 | - 'capability' => 'ee_edit_event', |
|
151 | - 'obj_id' => $evt_id, |
|
152 | - ], |
|
153 | - 'copy_event' => [ |
|
154 | - 'func' => '_copy_events', |
|
155 | - 'capability' => 'ee_edit_event', |
|
156 | - 'obj_id' => $evt_id, |
|
157 | - 'noheader' => true, |
|
158 | - ], |
|
159 | - 'trash_event' => [ |
|
160 | - 'func' => '_trash_or_restore_event', |
|
161 | - 'args' => ['event_status' => 'trash'], |
|
162 | - 'capability' => 'ee_delete_event', |
|
163 | - 'obj_id' => $evt_id, |
|
164 | - 'noheader' => true, |
|
165 | - ], |
|
166 | - 'trash_events' => [ |
|
167 | - 'func' => '_trash_or_restore_events', |
|
168 | - 'args' => ['event_status' => 'trash'], |
|
169 | - 'capability' => 'ee_delete_events', |
|
170 | - 'noheader' => true, |
|
171 | - ], |
|
172 | - 'restore_event' => [ |
|
173 | - 'func' => '_trash_or_restore_event', |
|
174 | - 'args' => ['event_status' => 'draft'], |
|
175 | - 'capability' => 'ee_delete_event', |
|
176 | - 'obj_id' => $evt_id, |
|
177 | - 'noheader' => true, |
|
178 | - ], |
|
179 | - 'restore_events' => [ |
|
180 | - 'func' => '_trash_or_restore_events', |
|
181 | - 'args' => ['event_status' => 'draft'], |
|
182 | - 'capability' => 'ee_delete_events', |
|
183 | - 'noheader' => true, |
|
184 | - ], |
|
185 | - 'delete_event' => [ |
|
186 | - 'func' => '_delete_event', |
|
187 | - 'capability' => 'ee_delete_event', |
|
188 | - 'obj_id' => $evt_id, |
|
189 | - 'noheader' => true, |
|
190 | - ], |
|
191 | - 'delete_events' => [ |
|
192 | - 'func' => '_delete_events', |
|
193 | - 'capability' => 'ee_delete_events', |
|
194 | - 'noheader' => true, |
|
195 | - ], |
|
196 | - 'view_report' => [ |
|
197 | - 'func' => '_view_report', |
|
198 | - 'capability' => 'ee_edit_events', |
|
199 | - ], |
|
200 | - 'default_event_settings' => [ |
|
201 | - 'func' => '_default_event_settings', |
|
202 | - 'capability' => 'manage_options', |
|
203 | - ], |
|
204 | - 'update_default_event_settings' => [ |
|
205 | - 'func' => '_update_default_event_settings', |
|
206 | - 'capability' => 'manage_options', |
|
207 | - 'noheader' => true, |
|
208 | - ], |
|
209 | - 'template_settings' => [ |
|
210 | - 'func' => '_template_settings', |
|
211 | - 'capability' => 'manage_options', |
|
212 | - ], |
|
213 | - // event category tab related |
|
214 | - 'add_category' => [ |
|
215 | - 'func' => '_category_details', |
|
216 | - 'capability' => 'ee_edit_event_category', |
|
217 | - 'args' => ['add'], |
|
218 | - ], |
|
219 | - 'edit_category' => [ |
|
220 | - 'func' => '_category_details', |
|
221 | - 'capability' => 'ee_edit_event_category', |
|
222 | - 'args' => ['edit'], |
|
223 | - ], |
|
224 | - 'delete_categories' => [ |
|
225 | - 'func' => '_delete_categories', |
|
226 | - 'capability' => 'ee_delete_event_category', |
|
227 | - 'noheader' => true, |
|
228 | - ], |
|
229 | - 'delete_category' => [ |
|
230 | - 'func' => '_delete_categories', |
|
231 | - 'capability' => 'ee_delete_event_category', |
|
232 | - 'noheader' => true, |
|
233 | - ], |
|
234 | - 'insert_category' => [ |
|
235 | - 'func' => '_insert_or_update_category', |
|
236 | - 'args' => ['new_category' => true], |
|
237 | - 'capability' => 'ee_edit_event_category', |
|
238 | - 'noheader' => true, |
|
239 | - ], |
|
240 | - 'update_category' => [ |
|
241 | - 'func' => '_insert_or_update_category', |
|
242 | - 'args' => ['new_category' => false], |
|
243 | - 'capability' => 'ee_edit_event_category', |
|
244 | - 'noheader' => true, |
|
245 | - ], |
|
246 | - 'category_list' => [ |
|
247 | - 'func' => '_category_list_table', |
|
248 | - 'capability' => 'ee_manage_event_categories', |
|
249 | - ], |
|
250 | - 'preview_deletion' => [ |
|
251 | - 'func' => 'previewDeletion', |
|
252 | - 'capability' => 'ee_delete_events', |
|
253 | - ], |
|
254 | - 'confirm_deletion' => [ |
|
255 | - 'func' => 'confirmDeletion', |
|
256 | - 'capability' => 'ee_delete_events', |
|
257 | - 'noheader' => true, |
|
258 | - ], |
|
259 | - ]; |
|
260 | - } |
|
261 | - |
|
262 | - |
|
263 | - /** |
|
264 | - * Set the _page_config property for this admin page group. |
|
265 | - */ |
|
266 | - protected function _set_page_config() |
|
267 | - { |
|
268 | - $this->_page_config = [ |
|
269 | - 'default' => [ |
|
270 | - 'nav' => [ |
|
271 | - 'label' => esc_html__('Overview', 'event_espresso'), |
|
272 | - 'order' => 10, |
|
273 | - ], |
|
274 | - 'list_table' => 'Events_Admin_List_Table', |
|
275 | - 'help_tabs' => [ |
|
276 | - 'events_overview_help_tab' => [ |
|
277 | - 'title' => esc_html__('Events Overview', 'event_espresso'), |
|
278 | - 'filename' => 'events_overview', |
|
279 | - ], |
|
280 | - 'events_overview_table_column_headings_help_tab' => [ |
|
281 | - 'title' => esc_html__('Events Overview Table Column Headings', 'event_espresso'), |
|
282 | - 'filename' => 'events_overview_table_column_headings', |
|
283 | - ], |
|
284 | - 'events_overview_filters_help_tab' => [ |
|
285 | - 'title' => esc_html__('Events Overview Filters', 'event_espresso'), |
|
286 | - 'filename' => 'events_overview_filters', |
|
287 | - ], |
|
288 | - 'events_overview_view_help_tab' => [ |
|
289 | - 'title' => esc_html__('Events Overview Views', 'event_espresso'), |
|
290 | - 'filename' => 'events_overview_views', |
|
291 | - ], |
|
292 | - 'events_overview_other_help_tab' => [ |
|
293 | - 'title' => esc_html__('Events Overview Other', 'event_espresso'), |
|
294 | - 'filename' => 'events_overview_other', |
|
295 | - ], |
|
296 | - ], |
|
297 | - // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836 |
|
298 | - // 'help_tour' => [ |
|
299 | - // 'Event_Overview_Help_Tour', |
|
300 | - // // 'New_Features_Test_Help_Tour' for testing multiple help tour |
|
301 | - // ], |
|
302 | - 'qtips' => ['EE_Event_List_Table_Tips'], |
|
303 | - 'require_nonce' => false, |
|
304 | - ], |
|
305 | - 'create_new' => [ |
|
306 | - 'nav' => [ |
|
307 | - 'label' => esc_html__('Add Event', 'event_espresso'), |
|
308 | - 'order' => 5, |
|
309 | - 'persistent' => false, |
|
310 | - ], |
|
311 | - 'metaboxes' => ['_register_event_editor_meta_boxes'], |
|
312 | - 'help_tabs' => [ |
|
313 | - 'event_editor_help_tab' => [ |
|
314 | - 'title' => esc_html__('Event Editor', 'event_espresso'), |
|
315 | - 'filename' => 'event_editor', |
|
316 | - ], |
|
317 | - 'event_editor_title_richtexteditor_help_tab' => [ |
|
318 | - 'title' => esc_html__('Event Title & Rich Text Editor', 'event_espresso'), |
|
319 | - 'filename' => 'event_editor_title_richtexteditor', |
|
320 | - ], |
|
321 | - 'event_editor_venue_details_help_tab' => [ |
|
322 | - 'title' => esc_html__('Event Venue Details', 'event_espresso'), |
|
323 | - 'filename' => 'event_editor_venue_details', |
|
324 | - ], |
|
325 | - 'event_editor_event_datetimes_help_tab' => [ |
|
326 | - 'title' => esc_html__('Event Datetimes', 'event_espresso'), |
|
327 | - 'filename' => 'event_editor_event_datetimes', |
|
328 | - ], |
|
329 | - 'event_editor_event_tickets_help_tab' => [ |
|
330 | - 'title' => esc_html__('Event Tickets', 'event_espresso'), |
|
331 | - 'filename' => 'event_editor_event_tickets', |
|
332 | - ], |
|
333 | - 'event_editor_event_registration_options_help_tab' => [ |
|
334 | - 'title' => esc_html__('Event Registration Options', 'event_espresso'), |
|
335 | - 'filename' => 'event_editor_event_registration_options', |
|
336 | - ], |
|
337 | - 'event_editor_tags_categories_help_tab' => [ |
|
338 | - 'title' => esc_html__('Event Tags & Categories', 'event_espresso'), |
|
339 | - 'filename' => 'event_editor_tags_categories', |
|
340 | - ], |
|
341 | - 'event_editor_questions_registrants_help_tab' => [ |
|
342 | - 'title' => esc_html__('Questions for Registrants', 'event_espresso'), |
|
343 | - 'filename' => 'event_editor_questions_registrants', |
|
344 | - ], |
|
345 | - 'event_editor_save_new_event_help_tab' => [ |
|
346 | - 'title' => esc_html__('Save New Event', 'event_espresso'), |
|
347 | - 'filename' => 'event_editor_save_new_event', |
|
348 | - ], |
|
349 | - 'event_editor_other_help_tab' => [ |
|
350 | - 'title' => esc_html__('Event Other', 'event_espresso'), |
|
351 | - 'filename' => 'event_editor_other', |
|
352 | - ], |
|
353 | - ], |
|
354 | - // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836 |
|
355 | - // 'help_tour' => [ |
|
356 | - // 'Event_Editor_Help_Tour', |
|
357 | - // ], |
|
358 | - 'qtips' => ['EE_Event_Editor_Decaf_Tips'], |
|
359 | - 'require_nonce' => false, |
|
360 | - ], |
|
361 | - 'edit' => [ |
|
362 | - 'nav' => [ |
|
363 | - 'label' => esc_html__('Edit Event', 'event_espresso'), |
|
364 | - 'order' => 5, |
|
365 | - 'persistent' => false, |
|
366 | - 'url' => isset($this->_req_data['post']) |
|
367 | - ? EE_Admin_Page::add_query_args_and_nonce( |
|
368 | - ['post' => $this->_req_data['post'], 'action' => 'edit'], |
|
369 | - $this->_current_page_view_url |
|
370 | - ) |
|
371 | - : $this->_admin_base_url, |
|
372 | - ], |
|
373 | - 'metaboxes' => ['_register_event_editor_meta_boxes'], |
|
374 | - 'help_tabs' => [ |
|
375 | - 'event_editor_help_tab' => [ |
|
376 | - 'title' => esc_html__('Event Editor', 'event_espresso'), |
|
377 | - 'filename' => 'event_editor', |
|
378 | - ], |
|
379 | - 'event_editor_title_richtexteditor_help_tab' => [ |
|
380 | - 'title' => esc_html__('Event Title & Rich Text Editor', 'event_espresso'), |
|
381 | - 'filename' => 'event_editor_title_richtexteditor', |
|
382 | - ], |
|
383 | - 'event_editor_venue_details_help_tab' => [ |
|
384 | - 'title' => esc_html__('Event Venue Details', 'event_espresso'), |
|
385 | - 'filename' => 'event_editor_venue_details', |
|
386 | - ], |
|
387 | - 'event_editor_event_datetimes_help_tab' => [ |
|
388 | - 'title' => esc_html__('Event Datetimes', 'event_espresso'), |
|
389 | - 'filename' => 'event_editor_event_datetimes', |
|
390 | - ], |
|
391 | - 'event_editor_event_tickets_help_tab' => [ |
|
392 | - 'title' => esc_html__('Event Tickets', 'event_espresso'), |
|
393 | - 'filename' => 'event_editor_event_tickets', |
|
394 | - ], |
|
395 | - 'event_editor_event_registration_options_help_tab' => [ |
|
396 | - 'title' => esc_html__('Event Registration Options', 'event_espresso'), |
|
397 | - 'filename' => 'event_editor_event_registration_options', |
|
398 | - ], |
|
399 | - 'event_editor_tags_categories_help_tab' => [ |
|
400 | - 'title' => esc_html__('Event Tags & Categories', 'event_espresso'), |
|
401 | - 'filename' => 'event_editor_tags_categories', |
|
402 | - ], |
|
403 | - 'event_editor_questions_registrants_help_tab' => [ |
|
404 | - 'title' => esc_html__('Questions for Registrants', 'event_espresso'), |
|
405 | - 'filename' => 'event_editor_questions_registrants', |
|
406 | - ], |
|
407 | - 'event_editor_save_new_event_help_tab' => [ |
|
408 | - 'title' => esc_html__('Save New Event', 'event_espresso'), |
|
409 | - 'filename' => 'event_editor_save_new_event', |
|
410 | - ], |
|
411 | - 'event_editor_other_help_tab' => [ |
|
412 | - 'title' => esc_html__('Event Other', 'event_espresso'), |
|
413 | - 'filename' => 'event_editor_other', |
|
414 | - ], |
|
415 | - ], |
|
416 | - 'require_nonce' => false, |
|
417 | - ], |
|
418 | - 'default_event_settings' => [ |
|
419 | - 'nav' => [ |
|
420 | - 'label' => esc_html__('Default Settings', 'event_espresso'), |
|
421 | - 'order' => 40, |
|
422 | - ], |
|
423 | - 'metaboxes' => array_merge($this->_default_espresso_metaboxes, ['_publish_post_box']), |
|
424 | - 'labels' => [ |
|
425 | - 'publishbox' => esc_html__('Update Settings', 'event_espresso'), |
|
426 | - ], |
|
427 | - 'help_tabs' => [ |
|
428 | - 'default_settings_help_tab' => [ |
|
429 | - 'title' => esc_html__('Default Event Settings', 'event_espresso'), |
|
430 | - 'filename' => 'events_default_settings', |
|
431 | - ], |
|
432 | - 'default_settings_status_help_tab' => [ |
|
433 | - 'title' => esc_html__('Default Registration Status', 'event_espresso'), |
|
434 | - 'filename' => 'events_default_settings_status', |
|
435 | - ], |
|
436 | - 'default_maximum_tickets_help_tab' => [ |
|
437 | - 'title' => esc_html__('Default Maximum Tickets Per Order', 'event_espresso'), |
|
438 | - 'filename' => 'events_default_settings_max_tickets', |
|
439 | - ], |
|
440 | - ], |
|
441 | - // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836 |
|
442 | - // 'help_tour' => ['Event_Default_Settings_Help_Tour'], |
|
443 | - 'require_nonce' => false, |
|
444 | - ], |
|
445 | - // template settings |
|
446 | - 'template_settings' => [ |
|
447 | - 'nav' => [ |
|
448 | - 'label' => esc_html__('Templates', 'event_espresso'), |
|
449 | - 'order' => 30, |
|
450 | - ], |
|
451 | - 'metaboxes' => $this->_default_espresso_metaboxes, |
|
452 | - 'help_tabs' => [ |
|
453 | - 'general_settings_templates_help_tab' => [ |
|
454 | - 'title' => esc_html__('Templates', 'event_espresso'), |
|
455 | - 'filename' => 'general_settings_templates', |
|
456 | - ], |
|
457 | - ], |
|
458 | - // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836 |
|
459 | - // 'help_tour' => ['Templates_Help_Tour'], |
|
460 | - 'require_nonce' => false, |
|
461 | - ], |
|
462 | - // event category stuff |
|
463 | - 'add_category' => [ |
|
464 | - 'nav' => [ |
|
465 | - 'label' => esc_html__('Add Category', 'event_espresso'), |
|
466 | - 'order' => 15, |
|
467 | - 'persistent' => false, |
|
468 | - ], |
|
469 | - 'help_tabs' => [ |
|
470 | - 'add_category_help_tab' => [ |
|
471 | - 'title' => esc_html__('Add New Event Category', 'event_espresso'), |
|
472 | - 'filename' => 'events_add_category', |
|
473 | - ], |
|
474 | - ], |
|
475 | - // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836 |
|
476 | - // 'help_tour' => ['Event_Add_Category_Help_Tour'], |
|
477 | - 'metaboxes' => ['_publish_post_box'], |
|
478 | - 'require_nonce' => false, |
|
479 | - ], |
|
480 | - 'edit_category' => [ |
|
481 | - 'nav' => [ |
|
482 | - 'label' => esc_html__('Edit Category', 'event_espresso'), |
|
483 | - 'order' => 15, |
|
484 | - 'persistent' => false, |
|
485 | - 'url' => isset($this->_req_data['EVT_CAT_ID']) |
|
486 | - ? add_query_arg( |
|
487 | - ['EVT_CAT_ID' => $this->_req_data['EVT_CAT_ID']], |
|
488 | - $this->_current_page_view_url |
|
489 | - ) |
|
490 | - : $this->_admin_base_url, |
|
491 | - ], |
|
492 | - 'help_tabs' => [ |
|
493 | - 'edit_category_help_tab' => [ |
|
494 | - 'title' => esc_html__('Edit Event Category', 'event_espresso'), |
|
495 | - 'filename' => 'events_edit_category', |
|
496 | - ], |
|
497 | - ], |
|
498 | - /*'help_tour' => ['Event_Edit_Category_Help_Tour'],*/ |
|
499 | - 'metaboxes' => ['_publish_post_box'], |
|
500 | - 'require_nonce' => false, |
|
501 | - ], |
|
502 | - 'category_list' => [ |
|
503 | - 'nav' => [ |
|
504 | - 'label' => esc_html__('Categories', 'event_espresso'), |
|
505 | - 'order' => 20, |
|
506 | - ], |
|
507 | - 'list_table' => 'Event_Categories_Admin_List_Table', |
|
508 | - 'help_tabs' => [ |
|
509 | - 'events_categories_help_tab' => [ |
|
510 | - 'title' => esc_html__('Event Categories', 'event_espresso'), |
|
511 | - 'filename' => 'events_categories', |
|
512 | - ], |
|
513 | - 'events_categories_table_column_headings_help_tab' => [ |
|
514 | - 'title' => esc_html__('Event Categories Table Column Headings', 'event_espresso'), |
|
515 | - 'filename' => 'events_categories_table_column_headings', |
|
516 | - ], |
|
517 | - 'events_categories_view_help_tab' => [ |
|
518 | - 'title' => esc_html__('Event Categories Views', 'event_espresso'), |
|
519 | - 'filename' => 'events_categories_views', |
|
520 | - ], |
|
521 | - 'events_categories_other_help_tab' => [ |
|
522 | - 'title' => esc_html__('Event Categories Other', 'event_espresso'), |
|
523 | - 'filename' => 'events_categories_other', |
|
524 | - ], |
|
525 | - ], |
|
526 | - // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836 |
|
527 | - // 'help_tour' => [ |
|
528 | - // 'Event_Categories_Help_Tour', |
|
529 | - // ], |
|
530 | - 'metaboxes' => $this->_default_espresso_metaboxes, |
|
531 | - 'require_nonce' => false, |
|
532 | - ], |
|
533 | - 'preview_deletion' => [ |
|
534 | - 'nav' => [ |
|
535 | - 'label' => esc_html__('Preview Deletion', 'event_espresso'), |
|
536 | - 'order' => 15, |
|
537 | - 'persistent' => false, |
|
538 | - 'url' => '', |
|
539 | - ], |
|
540 | - 'require_nonce' => false, |
|
541 | - ], |
|
542 | - ]; |
|
543 | - // only load EE_Event_Editor_Decaf_Tips if domain is not caffeinated |
|
544 | - $domain = $this->loader->getShared('EventEspresso\core\domain\Domain'); |
|
545 | - if (! $domain->isCaffeinated()) { |
|
546 | - $this->_page_config['create_new']['qtips'] = ['EE_Event_Editor_Decaf_Tips']; |
|
547 | - $this->_page_config['edit']['qtips'] = ['EE_Event_Editor_Decaf_Tips']; |
|
548 | - } |
|
549 | - } |
|
550 | - |
|
551 | - |
|
552 | - /** |
|
553 | - * Used to register any global screen options if necessary for every route in this admin page group. |
|
554 | - */ |
|
555 | - protected function _add_screen_options() |
|
556 | - { |
|
557 | - } |
|
558 | - |
|
559 | - |
|
560 | - /** |
|
561 | - * Implementing the screen options for the 'default' route. |
|
562 | - * |
|
563 | - * @throws InvalidArgumentException |
|
564 | - * @throws InvalidDataTypeException |
|
565 | - * @throws InvalidInterfaceException |
|
566 | - */ |
|
567 | - protected function _add_screen_options_default() |
|
568 | - { |
|
569 | - $this->_per_page_screen_option(); |
|
570 | - } |
|
571 | - |
|
572 | - |
|
573 | - /** |
|
574 | - * Implementing screen options for the category list route. |
|
575 | - * |
|
576 | - * @throws InvalidArgumentException |
|
577 | - * @throws InvalidDataTypeException |
|
578 | - * @throws InvalidInterfaceException |
|
579 | - */ |
|
580 | - protected function _add_screen_options_category_list() |
|
581 | - { |
|
582 | - $page_title = $this->_admin_page_title; |
|
583 | - $this->_admin_page_title = esc_html__('Categories', 'event_espresso'); |
|
584 | - $this->_per_page_screen_option(); |
|
585 | - $this->_admin_page_title = $page_title; |
|
586 | - } |
|
587 | - |
|
588 | - |
|
589 | - /** |
|
590 | - * Used to register any global feature pointers for the admin page group. |
|
591 | - */ |
|
592 | - protected function _add_feature_pointers() |
|
593 | - { |
|
594 | - } |
|
595 | - |
|
596 | - |
|
597 | - /** |
|
598 | - * Registers and enqueues any global scripts and styles for the entire admin page group. |
|
599 | - */ |
|
600 | - public function load_scripts_styles() |
|
601 | - { |
|
602 | - wp_register_style( |
|
603 | - 'events-admin-css', |
|
604 | - EVENTS_ASSETS_URL . 'events-admin-page.css', |
|
605 | - [], |
|
606 | - EVENT_ESPRESSO_VERSION |
|
607 | - ); |
|
608 | - wp_register_style( |
|
609 | - 'ee-cat-admin', |
|
610 | - EVENTS_ASSETS_URL . 'ee-cat-admin.css', |
|
611 | - [], |
|
612 | - EVENT_ESPRESSO_VERSION |
|
613 | - ); |
|
614 | - wp_enqueue_style('events-admin-css'); |
|
615 | - wp_enqueue_style('ee-cat-admin'); |
|
616 | - // scripts |
|
617 | - wp_register_script( |
|
618 | - 'event_editor_js', |
|
619 | - EVENTS_ASSETS_URL . 'event_editor.js', |
|
620 | - ['ee_admin_js', 'jquery-ui-slider', 'jquery-ui-timepicker-addon'], |
|
621 | - EVENT_ESPRESSO_VERSION, |
|
622 | - true |
|
623 | - ); |
|
624 | - } |
|
625 | - |
|
626 | - |
|
627 | - /** |
|
628 | - * Enqueuing scripts and styles specific to this view |
|
629 | - */ |
|
630 | - public function load_scripts_styles_create_new() |
|
631 | - { |
|
632 | - $this->load_scripts_styles_edit(); |
|
633 | - } |
|
634 | - |
|
635 | - |
|
636 | - /** |
|
637 | - * Enqueuing scripts and styles specific to this view |
|
638 | - */ |
|
639 | - public function load_scripts_styles_edit() |
|
640 | - { |
|
641 | - // styles |
|
642 | - wp_enqueue_style('espresso-ui-theme'); |
|
643 | - wp_register_style( |
|
644 | - 'event-editor-css', |
|
645 | - EVENTS_ASSETS_URL . 'event-editor.css', |
|
646 | - ['ee-admin-css'], |
|
647 | - EVENT_ESPRESSO_VERSION |
|
648 | - ); |
|
649 | - wp_enqueue_style('event-editor-css'); |
|
650 | - // scripts |
|
651 | - if (! $this->admin_config->useAdvancedEditor()) { |
|
652 | - wp_register_script( |
|
653 | - 'event-datetime-metabox', |
|
654 | - EVENTS_ASSETS_URL . 'event-datetime-metabox.js', |
|
655 | - ['event_editor_js', 'ee-datepicker'], |
|
656 | - EVENT_ESPRESSO_VERSION |
|
657 | - ); |
|
658 | - wp_enqueue_script('event-datetime-metabox'); |
|
659 | - } |
|
660 | - } |
|
661 | - |
|
662 | - |
|
663 | - /** |
|
664 | - * Populating the _views property for the category list table view. |
|
665 | - */ |
|
666 | - protected function _set_list_table_views_category_list() |
|
667 | - { |
|
668 | - $this->_views = [ |
|
669 | - 'all' => [ |
|
670 | - 'slug' => 'all', |
|
671 | - 'label' => esc_html__('All', 'event_espresso'), |
|
672 | - 'count' => 0, |
|
673 | - 'bulk_action' => [ |
|
674 | - 'delete_categories' => esc_html__('Delete Permanently', 'event_espresso'), |
|
675 | - ], |
|
676 | - ], |
|
677 | - ]; |
|
678 | - } |
|
679 | - |
|
680 | - |
|
681 | - /** |
|
682 | - * For adding anything that fires on the admin_init hook for any route within this admin page group. |
|
683 | - */ |
|
684 | - public function admin_init() |
|
685 | - { |
|
686 | - EE_Registry::$i18n_js_strings['image_confirm'] = esc_html__( |
|
687 | - 'Do you really want to delete this image? Please remember to update your event to complete the removal.', |
|
688 | - 'event_espresso' |
|
689 | - ); |
|
690 | - } |
|
691 | - |
|
692 | - |
|
693 | - /** |
|
694 | - * For adding anything that should be triggered on the admin_notices hook for any route within this admin page |
|
695 | - * group. |
|
696 | - */ |
|
697 | - public function admin_notices() |
|
698 | - { |
|
699 | - } |
|
700 | - |
|
701 | - |
|
702 | - /** |
|
703 | - * For adding anything that should be triggered on the `admin_print_footer_scripts` hook for any route within |
|
704 | - * this admin page group. |
|
705 | - */ |
|
706 | - public function admin_footer_scripts() |
|
707 | - { |
|
708 | - } |
|
709 | - |
|
710 | - |
|
711 | - /** |
|
712 | - * Call this function to verify if an event is public and has tickets for sale. If it does, then we need to show a |
|
713 | - * warning (via EE_Error::add_error()); |
|
714 | - * |
|
715 | - * @param EE_Event $event Event object |
|
716 | - * @param string $req_type |
|
717 | - * @return void |
|
718 | - * @throws EE_Error |
|
719 | - */ |
|
720 | - public function verify_event_edit($event = null, $req_type = '') |
|
721 | - { |
|
722 | - // don't need to do this when processing |
|
723 | - if (! empty($req_type)) { |
|
724 | - return; |
|
725 | - } |
|
726 | - // no event? |
|
727 | - if (! $event instanceof EE_Event) { |
|
728 | - $event = $this->_cpt_model_obj; |
|
729 | - } |
|
730 | - // STILL no event? |
|
731 | - if (! $event instanceof EE_Event) { |
|
732 | - return; |
|
733 | - } |
|
734 | - $orig_status = $event->status(); |
|
735 | - // first check if event is active. |
|
736 | - if ( |
|
737 | - $orig_status === EEM_Event::cancelled |
|
738 | - || $orig_status === EEM_Event::postponed |
|
739 | - || $event->is_expired() |
|
740 | - || $event->is_inactive() |
|
741 | - ) { |
|
742 | - return; |
|
743 | - } |
|
744 | - // made it here so it IS active... next check that any of the tickets are sold. |
|
745 | - if ($event->is_sold_out(true)) { |
|
746 | - if ($orig_status !== EEM_Event::sold_out && $event->status() !== $orig_status) { |
|
747 | - EE_Error::add_attention( |
|
748 | - sprintf( |
|
749 | - esc_html__( |
|
750 | - 'Please note that the Event Status has automatically been changed to %s because there are no more spaces available for this event. However, this change is not permanent until you update the event. You can change the status back to something else before updating if you wish.', |
|
751 | - 'event_espresso' |
|
752 | - ), |
|
753 | - EEH_Template::pretty_status(EEM_Event::sold_out, false, 'sentence') |
|
754 | - ) |
|
755 | - ); |
|
756 | - } |
|
757 | - return; |
|
758 | - } |
|
759 | - if ($orig_status === EEM_Event::sold_out) { |
|
760 | - EE_Error::add_attention( |
|
761 | - sprintf( |
|
762 | - esc_html__( |
|
763 | - 'Please note that the Event Status has automatically been changed to %s because more spaces have become available for this event, most likely due to abandoned transactions freeing up reserved tickets. However, this change is not permanent until you update the event. If you wish, you can change the status back to something else before updating.', |
|
764 | - 'event_espresso' |
|
765 | - ), |
|
766 | - EEH_Template::pretty_status($event->status(), false, 'sentence') |
|
767 | - ) |
|
768 | - ); |
|
769 | - } |
|
770 | - // now we need to determine if the event has any tickets on sale. If not then we dont' show the error |
|
771 | - if (! $event->tickets_on_sale()) { |
|
772 | - return; |
|
773 | - } |
|
774 | - // made it here so show warning |
|
775 | - $this->_edit_event_warning(); |
|
776 | - } |
|
777 | - |
|
778 | - |
|
779 | - /** |
|
780 | - * This is the text used for when an event is being edited that is public and has tickets for sale. |
|
781 | - * When needed, hook this into a EE_Error::add_error() notice. |
|
782 | - * |
|
783 | - * @return void |
|
784 | - */ |
|
785 | - protected function _edit_event_warning() |
|
786 | - { |
|
787 | - // we don't want to add warnings during these requests |
|
788 | - if (isset($this->_req_data['action']) && $this->_req_data['action'] === 'editpost') { |
|
789 | - return; |
|
790 | - } |
|
791 | - EE_Error::add_attention( |
|
792 | - sprintf( |
|
793 | - esc_html__( |
|
794 | - 'Your event is open for registration. Making changes may disrupt any transactions in progress. %sLearn more%s', |
|
795 | - 'event_espresso' |
|
796 | - ), |
|
797 | - '<a class="espresso-help-tab-lnk">', |
|
798 | - '</a>' |
|
799 | - ) |
|
800 | - ); |
|
801 | - } |
|
802 | - |
|
803 | - |
|
804 | - /** |
|
805 | - * When a user is creating a new event, notify them if they haven't set their timezone. |
|
806 | - * Otherwise, do the normal logic |
|
807 | - * |
|
808 | - * @return void |
|
809 | - * @throws EE_Error |
|
810 | - * @throws InvalidArgumentException |
|
811 | - * @throws InvalidDataTypeException |
|
812 | - */ |
|
813 | - protected function _create_new_cpt_item() |
|
814 | - { |
|
815 | - $has_timezone_string = get_option('timezone_string'); |
|
816 | - // only nag them about setting their timezone if it's their first event, and they haven't already done it |
|
817 | - if (! $has_timezone_string && ! EEM_Event::instance()->exists([])) { |
|
818 | - EE_Error::add_attention( |
|
819 | - sprintf( |
|
820 | - __( |
|
821 | - 'Your website\'s timezone is currently set to a UTC offset. We recommend updating your timezone to a city or region near you before you create an event. Change your timezone now:%1$s%2$s%3$sChange Timezone%4$s', |
|
822 | - 'event_espresso' |
|
823 | - ), |
|
824 | - '<br>', |
|
825 | - '<select id="timezone_string" name="timezone_string" aria-describedby="timezone-description">' |
|
826 | - . EEH_DTT_Helper::wp_timezone_choice('', EEH_DTT_Helper::get_user_locale()) |
|
827 | - . '</select>', |
|
828 | - '<button class="button button-secondary timezone-submit">', |
|
829 | - '</button><span class="spinner"></span>' |
|
830 | - ), |
|
831 | - __FILE__, |
|
832 | - __FUNCTION__, |
|
833 | - __LINE__ |
|
834 | - ); |
|
835 | - } |
|
836 | - parent::_create_new_cpt_item(); |
|
837 | - } |
|
838 | - |
|
839 | - |
|
840 | - /** |
|
841 | - * Sets the _views property for the default route in this admin page group. |
|
842 | - */ |
|
843 | - protected function _set_list_table_views_default() |
|
844 | - { |
|
845 | - $this->_views = [ |
|
846 | - 'all' => [ |
|
847 | - 'slug' => 'all', |
|
848 | - 'label' => esc_html__('View All Events', 'event_espresso'), |
|
849 | - 'count' => 0, |
|
850 | - 'bulk_action' => [ |
|
851 | - 'trash_events' => esc_html__('Move to Trash', 'event_espresso'), |
|
852 | - ], |
|
853 | - ], |
|
854 | - 'draft' => [ |
|
855 | - 'slug' => 'draft', |
|
856 | - 'label' => esc_html__('Draft', 'event_espresso'), |
|
857 | - 'count' => 0, |
|
858 | - 'bulk_action' => [ |
|
859 | - 'trash_events' => esc_html__('Move to Trash', 'event_espresso'), |
|
860 | - ], |
|
861 | - ], |
|
862 | - ]; |
|
863 | - if (EE_Registry::instance()->CAP->current_user_can('ee_delete_events', 'espresso_events_trash_events')) { |
|
864 | - $this->_views['trash'] = [ |
|
865 | - 'slug' => 'trash', |
|
866 | - 'label' => esc_html__('Trash', 'event_espresso'), |
|
867 | - 'count' => 0, |
|
868 | - 'bulk_action' => [ |
|
869 | - 'restore_events' => esc_html__('Restore From Trash', 'event_espresso'), |
|
870 | - 'delete_events' => esc_html__('Delete Permanently', 'event_espresso'), |
|
871 | - ], |
|
872 | - ]; |
|
873 | - } |
|
874 | - } |
|
875 | - |
|
876 | - |
|
877 | - /** |
|
878 | - * Provides the legend item array for the default list table view. |
|
879 | - * |
|
880 | - * @return array |
|
881 | - */ |
|
882 | - protected function _event_legend_items() |
|
883 | - { |
|
884 | - $items = [ |
|
885 | - 'view_details' => [ |
|
886 | - 'class' => 'dashicons dashicons-search', |
|
887 | - 'desc' => esc_html__('View Event', 'event_espresso'), |
|
888 | - ], |
|
889 | - 'edit_event' => [ |
|
890 | - 'class' => 'ee-icon ee-icon-calendar-edit', |
|
891 | - 'desc' => esc_html__('Edit Event Details', 'event_espresso'), |
|
892 | - ], |
|
893 | - 'view_attendees' => [ |
|
894 | - 'class' => 'dashicons dashicons-groups', |
|
895 | - 'desc' => esc_html__('View Registrations for Event', 'event_espresso'), |
|
896 | - ], |
|
897 | - ]; |
|
898 | - $items = apply_filters('FHEE__Events_Admin_Page___event_legend_items__items', $items); |
|
899 | - $statuses = [ |
|
900 | - 'sold_out_status' => [ |
|
901 | - 'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::sold_out, |
|
902 | - 'desc' => EEH_Template::pretty_status(EE_Datetime::sold_out, false, 'sentence'), |
|
903 | - ], |
|
904 | - 'active_status' => [ |
|
905 | - 'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::active, |
|
906 | - 'desc' => EEH_Template::pretty_status(EE_Datetime::active, false, 'sentence'), |
|
907 | - ], |
|
908 | - 'upcoming_status' => [ |
|
909 | - 'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::upcoming, |
|
910 | - 'desc' => EEH_Template::pretty_status(EE_Datetime::upcoming, false, 'sentence'), |
|
911 | - ], |
|
912 | - 'postponed_status' => [ |
|
913 | - 'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::postponed, |
|
914 | - 'desc' => EEH_Template::pretty_status(EE_Datetime::postponed, false, 'sentence'), |
|
915 | - ], |
|
916 | - 'cancelled_status' => [ |
|
917 | - 'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::cancelled, |
|
918 | - 'desc' => EEH_Template::pretty_status(EE_Datetime::cancelled, false, 'sentence'), |
|
919 | - ], |
|
920 | - 'expired_status' => [ |
|
921 | - 'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::expired, |
|
922 | - 'desc' => EEH_Template::pretty_status(EE_Datetime::expired, false, 'sentence'), |
|
923 | - ], |
|
924 | - 'inactive_status' => [ |
|
925 | - 'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::inactive, |
|
926 | - 'desc' => EEH_Template::pretty_status(EE_Datetime::inactive, false, 'sentence'), |
|
927 | - ], |
|
928 | - ]; |
|
929 | - $statuses = apply_filters('FHEE__Events_Admin_Page__event_legend_items__statuses', $statuses); |
|
930 | - return array_merge($items, $statuses); |
|
931 | - } |
|
932 | - |
|
933 | - |
|
934 | - /** |
|
935 | - * @return EEM_Event |
|
936 | - * @throws EE_Error |
|
937 | - */ |
|
938 | - private function eventModel() |
|
939 | - { |
|
940 | - if (! $this->_event_model instanceof EEM_Event) { |
|
941 | - $this->_event_model = EEM_Event::instance(); |
|
942 | - } |
|
943 | - return $this->_event_model; |
|
944 | - } |
|
945 | - |
|
946 | - |
|
947 | - /** |
|
948 | - * @param string $event_timezone_string |
|
949 | - * @return EEM_Datetime |
|
950 | - * @throws EE_Error |
|
951 | - */ |
|
952 | - private function datetimeModel($event_timezone_string = '') |
|
953 | - { |
|
954 | - if (! $this->datetime_model instanceof EEM_Datetime) { |
|
955 | - $this->datetime_model = EEM_Datetime::instance($event_timezone_string); |
|
956 | - } |
|
957 | - return $this->datetime_model; |
|
958 | - } |
|
959 | - |
|
960 | - |
|
961 | - /** |
|
962 | - * @param string $event_timezone_string |
|
963 | - * @return EEM_Ticket |
|
964 | - * @throws EE_Error |
|
965 | - */ |
|
966 | - private function ticketModel($event_timezone_string = '') |
|
967 | - { |
|
968 | - if (! $this->ticket_model instanceof EEM_Ticket) { |
|
969 | - $this->ticket_model = EEM_Ticket::instance($event_timezone_string); |
|
970 | - } |
|
971 | - return $this->ticket_model; |
|
972 | - } |
|
973 | - |
|
974 | - |
|
975 | - /** |
|
976 | - * Adds extra buttons to the WP CPT permalink field row. |
|
977 | - * Method is called from parent and is hooked into the wp 'get_sample_permalink_html' filter. |
|
978 | - * |
|
979 | - * @param string $return the current html |
|
980 | - * @param int $id the post id for the page |
|
981 | - * @param string $new_title What the title is |
|
982 | - * @param string $new_slug what the slug is |
|
983 | - * @return string The new html string for the permalink area |
|
984 | - */ |
|
985 | - public function extra_permalink_field_buttons($return, $id, $new_title, $new_slug) |
|
986 | - { |
|
987 | - // make sure this is only when editing |
|
988 | - if (! empty($id)) { |
|
989 | - $post = get_post($id); |
|
990 | - $return .= '<a class="button button-small" onclick="prompt(\'Shortcode:\', jQuery(\'#shortcode\').val()); return false;" href="#" tabindex="-1">' |
|
991 | - . esc_html__('Shortcode', 'event_espresso') |
|
992 | - . '</a> '; |
|
993 | - $return .= '<input id="shortcode" type="hidden" value="[ESPRESSO_TICKET_SELECTOR event_id=' |
|
994 | - . $post->ID |
|
995 | - . ']">'; |
|
996 | - } |
|
997 | - return $return; |
|
998 | - } |
|
999 | - |
|
1000 | - |
|
1001 | - /** |
|
1002 | - * _events_overview_list_table |
|
1003 | - * This contains the logic for showing the events_overview list |
|
1004 | - * |
|
1005 | - * @return void |
|
1006 | - * @throws DomainException |
|
1007 | - * @throws EE_Error |
|
1008 | - * @throws InvalidArgumentException |
|
1009 | - * @throws InvalidDataTypeException |
|
1010 | - * @throws InvalidInterfaceException |
|
1011 | - */ |
|
1012 | - protected function _events_overview_list_table() |
|
1013 | - { |
|
1014 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
1015 | - $this->_template_args['after_list_table'] = |
|
1016 | - ! empty($this->_template_args['after_list_table']) |
|
1017 | - ? (array) $this->_template_args['after_list_table'] |
|
1018 | - : []; |
|
1019 | - $this->_template_args['after_list_table']['view_event_list_button'] = EEH_HTML::br() |
|
1020 | - . EEH_Template::get_button_or_link( |
|
1021 | - get_post_type_archive_link('espresso_events'), |
|
1022 | - esc_html__('View Event Archive Page', 'event_espresso'), |
|
1023 | - 'button' |
|
1024 | - ); |
|
1025 | - $this->_template_args['after_list_table']['legend'] = |
|
1026 | - $this->_display_legend($this->_event_legend_items()); |
|
1027 | - $this->_admin_page_title .= ' ' |
|
1028 | - . $this->get_action_link_or_button( |
|
1029 | - 'create_new', |
|
1030 | - 'add', |
|
1031 | - [], |
|
1032 | - 'add-new-h2' |
|
1033 | - ); |
|
1034 | - $this->display_admin_list_table_page_with_no_sidebar(); |
|
1035 | - } |
|
1036 | - |
|
1037 | - |
|
1038 | - /** |
|
1039 | - * this allows for extra misc actions in the default WP publish box |
|
1040 | - * |
|
1041 | - * @return void |
|
1042 | - * @throws DomainException |
|
1043 | - * @throws EE_Error |
|
1044 | - * @throws InvalidArgumentException |
|
1045 | - * @throws InvalidDataTypeException |
|
1046 | - * @throws InvalidInterfaceException |
|
1047 | - * @throws ReflectionException |
|
1048 | - */ |
|
1049 | - public function extra_misc_actions_publish_box() |
|
1050 | - { |
|
1051 | - $this->_generate_publish_box_extra_content(); |
|
1052 | - } |
|
1053 | - |
|
1054 | - |
|
1055 | - /** |
|
1056 | - * This is hooked into the WordPress do_action('save_post') hook and runs after the custom post type has been |
|
1057 | - * saved. |
|
1058 | - * Typically you would use this to save any additional data. |
|
1059 | - * Keep in mind also that "save_post" runs on EVERY post update to the database. |
|
1060 | - * ALSO very important. When a post transitions from scheduled to published, |
|
1061 | - * the save_post action is fired but you will NOT have any _POST data containing any extra info you may have from |
|
1062 | - * other meta saves. So MAKE sure that you handle this accordingly. |
|
1063 | - * |
|
1064 | - * @abstract |
|
1065 | - * @param string $post_id The ID of the cpt that was saved (so you can link relationally) |
|
1066 | - * @param object $post The post object of the cpt that was saved. |
|
1067 | - * @return void |
|
1068 | - * @throws EE_Error |
|
1069 | - * @throws InvalidArgumentException |
|
1070 | - * @throws InvalidDataTypeException |
|
1071 | - * @throws InvalidInterfaceException |
|
1072 | - * @throws ReflectionException |
|
1073 | - */ |
|
1074 | - protected function _insert_update_cpt_item($post_id, $post) |
|
1075 | - { |
|
1076 | - if ($post instanceof WP_Post && $post->post_type !== 'espresso_events') { |
|
1077 | - // get out we're not processing an event save. |
|
1078 | - return; |
|
1079 | - } |
|
1080 | - $event_values = [ |
|
1081 | - 'EVT_member_only' => ! empty($this->_req_data['member_only']) ? 1 : 0, |
|
1082 | - 'EVT_allow_overflow' => ! empty($this->_req_data['EVT_allow_overflow']) ? 1 : 0, |
|
1083 | - 'EVT_timezone_string' => ! empty($this->_req_data['timezone_string']) |
|
1084 | - ? sanitize_text_field($this->_req_data['timezone_string']) |
|
1085 | - : null, |
|
1086 | - ]; |
|
1087 | - /** @var FeatureFlags $flags */ |
|
1088 | - $flags = $this->loader->getShared('EventEspresso\core\domain\services\capabilities\FeatureFlags'); |
|
1089 | - // check if the new EDTR reg options meta box is being used, and if so, don't run updates for legacy version |
|
1090 | - if (! $this->admin_config->useAdvancedEditor() || ! $flags->featureAllowed('use_reg_options_meta_box')) { |
|
1091 | - $event_values['EVT_display_ticket_selector'] = |
|
1092 | - ! empty($this->_req_data['display_ticket_selector']) |
|
1093 | - ? 1 |
|
1094 | - : 0; |
|
1095 | - $event_values['EVT_additional_limit'] = min( |
|
1096 | - apply_filters('FHEE__EE_Events_Admin__insert_update_cpt_item__EVT_additional_limit_max', 255), |
|
1097 | - ! empty($this->_req_data['additional_limit']) |
|
1098 | - ? absint($this->_req_data['additional_limit']) |
|
1099 | - : null |
|
1100 | - ); |
|
1101 | - $event_values['EVT_default_registration_status'] = |
|
1102 | - ! empty($this->_req_data['EVT_default_registration_status']) |
|
1103 | - ? sanitize_text_field($this->_req_data['EVT_default_registration_status']) |
|
1104 | - : EE_Registry::instance()->CFG->registration->default_STS_ID; |
|
1105 | - $event_values['EVT_external_URL'] = ! empty($this->_req_data['externalURL']) |
|
1106 | - ? esc_url_raw($this->_req_data['externalURL']) |
|
1107 | - : null; |
|
1108 | - $event_values['EVT_phone'] = ! empty($this->_req_data['event_phone']) |
|
1109 | - ? sanitize_text_field($this->_req_data['event_phone']) |
|
1110 | - : null; |
|
1111 | - } |
|
1112 | - // update event |
|
1113 | - $success = $this->eventModel()->update_by_ID($event_values, $post_id); |
|
1114 | - // get event_object for other metaboxes... |
|
1115 | - // though it would seem to make sense to just use $this->eventModel()->get_one_by_ID( $post_id ).. |
|
1116 | - // i have to setup where conditions to override the filters in the model |
|
1117 | - // that filter out autodraft and inherit statuses so we GET the inherit id! |
|
1118 | - $get_one_where = [ |
|
1119 | - $this->eventModel()->primary_key_name() => $post_id, |
|
1120 | - 'OR' => [ |
|
1121 | - 'status' => $post->post_status, |
|
1122 | - // if trying to "Publish" a sold out event, it's status will get switched back to "sold_out" in the db, |
|
1123 | - // but the returned object here has a status of "publish", so use the original post status as well |
|
1124 | - 'status*1' => $this->_req_data['original_post_status'], |
|
1125 | - ], |
|
1126 | - ]; |
|
1127 | - $event = $this->eventModel()->get_one([$get_one_where]); |
|
1128 | - // the following are default callbacks for event attachment updates that can be overridden by caffeinated functionality and/or addons. |
|
1129 | - $event_update_callbacks = apply_filters( |
|
1130 | - 'FHEE__Events_Admin_Page___insert_update_cpt_item__event_update_callbacks', |
|
1131 | - [ |
|
1132 | - [$this, '_default_venue_update'], |
|
1133 | - [$this, '_default_tickets_update'], |
|
1134 | - ] |
|
1135 | - ); |
|
1136 | - $att_success = true; |
|
1137 | - foreach ($event_update_callbacks as $e_callback) { |
|
1138 | - $_success = is_callable($e_callback) |
|
1139 | - ? $e_callback($event, $this->_req_data) |
|
1140 | - : false; |
|
1141 | - // if ANY of these updates fail then we want the appropriate global error message |
|
1142 | - $att_success = ! $att_success ? $att_success : $_success; |
|
1143 | - } |
|
1144 | - // any errors? |
|
1145 | - if ($success && false === $att_success) { |
|
1146 | - EE_Error::add_error( |
|
1147 | - esc_html__( |
|
1148 | - 'Event Details saved successfully but something went wrong with saving attachments.', |
|
1149 | - 'event_espresso' |
|
1150 | - ), |
|
1151 | - __FILE__, |
|
1152 | - __FUNCTION__, |
|
1153 | - __LINE__ |
|
1154 | - ); |
|
1155 | - } elseif ($success === false) { |
|
1156 | - EE_Error::add_error( |
|
1157 | - esc_html__('Event Details did not save successfully.', 'event_espresso'), |
|
1158 | - __FILE__, |
|
1159 | - __FUNCTION__, |
|
1160 | - __LINE__ |
|
1161 | - ); |
|
1162 | - } |
|
1163 | - } |
|
1164 | - |
|
1165 | - |
|
1166 | - /** |
|
1167 | - * @param int $post_id |
|
1168 | - * @param int $revision_id |
|
1169 | - * @throws EE_Error |
|
1170 | - * @throws InvalidArgumentException |
|
1171 | - * @throws InvalidDataTypeException |
|
1172 | - * @throws InvalidInterfaceException |
|
1173 | - * @throws ReflectionException |
|
1174 | - * @see parent::restore_item() |
|
1175 | - */ |
|
1176 | - protected function _restore_cpt_item($post_id, $revision_id) |
|
1177 | - { |
|
1178 | - // copy existing event meta to new post |
|
1179 | - $post_evt = $this->eventModel()->get_one_by_ID($post_id); |
|
1180 | - if ($post_evt instanceof EE_Event) { |
|
1181 | - // meta revision restore |
|
1182 | - $post_evt->restore_revision($revision_id); |
|
1183 | - // related objs restore |
|
1184 | - $post_evt->restore_revision($revision_id, ['Venue', 'Datetime', 'Price']); |
|
1185 | - } |
|
1186 | - } |
|
1187 | - |
|
1188 | - |
|
1189 | - /** |
|
1190 | - * Attach the venue to the Event |
|
1191 | - * |
|
1192 | - * @param EE_Event $evtobj Event Object to add the venue to |
|
1193 | - * @param array $data The request data from the form |
|
1194 | - * @return bool Success or fail. |
|
1195 | - * @throws EE_Error |
|
1196 | - * @throws InvalidArgumentException |
|
1197 | - * @throws InvalidDataTypeException |
|
1198 | - * @throws InvalidInterfaceException |
|
1199 | - * @throws ReflectionException |
|
1200 | - */ |
|
1201 | - protected function _default_venue_update(EE_Event $evtobj, $data) |
|
1202 | - { |
|
1203 | - require_once(EE_MODELS . 'EEM_Venue.model.php'); |
|
1204 | - $venue_model = EEM_Venue::instance(); |
|
1205 | - $rows_affected = null; |
|
1206 | - $venue_id = ! empty($data['venue_id']) ? $data['venue_id'] : null; |
|
1207 | - // very important. If we don't have a venue name... |
|
1208 | - // then we'll get out because not necessary to create empty venue |
|
1209 | - if (empty($data['venue_title'])) { |
|
1210 | - return false; |
|
1211 | - } |
|
1212 | - $venue_array = [ |
|
1213 | - 'VNU_wp_user' => $evtobj->get('EVT_wp_user'), |
|
1214 | - 'VNU_name' => ! empty($data['venue_title']) ? $data['venue_title'] : null, |
|
1215 | - 'VNU_desc' => ! empty($data['venue_description']) ? $data['venue_description'] : null, |
|
1216 | - 'VNU_identifier' => ! empty($data['venue_identifier']) ? $data['venue_identifier'] : null, |
|
1217 | - 'VNU_short_desc' => ! empty($data['venue_short_description']) ? $data['venue_short_description'] |
|
1218 | - : null, |
|
1219 | - 'VNU_address' => ! empty($data['address']) ? $data['address'] : null, |
|
1220 | - 'VNU_address2' => ! empty($data['address2']) ? $data['address2'] : null, |
|
1221 | - 'VNU_city' => ! empty($data['city']) ? $data['city'] : null, |
|
1222 | - 'STA_ID' => ! empty($data['state']) ? $data['state'] : null, |
|
1223 | - 'CNT_ISO' => ! empty($data['countries']) ? $data['countries'] : null, |
|
1224 | - 'VNU_zip' => ! empty($data['zip']) ? $data['zip'] : null, |
|
1225 | - 'VNU_phone' => ! empty($data['venue_phone']) ? $data['venue_phone'] : null, |
|
1226 | - 'VNU_capacity' => ! empty($data['venue_capacity']) ? $data['venue_capacity'] : null, |
|
1227 | - 'VNU_url' => ! empty($data['venue_url']) ? $data['venue_url'] : null, |
|
1228 | - 'VNU_virtual_phone' => ! empty($data['virtual_phone']) ? $data['virtual_phone'] : null, |
|
1229 | - 'VNU_virtual_url' => ! empty($data['virtual_url']) ? $data['virtual_url'] : null, |
|
1230 | - 'VNU_enable_for_gmap' => isset($data['enable_for_gmap']) ? 1 : 0, |
|
1231 | - 'status' => 'publish', |
|
1232 | - ]; |
|
1233 | - // if we've got the venue_id then we're just updating the existing venue so let's do that and then get out. |
|
1234 | - if (! empty($venue_id)) { |
|
1235 | - $update_where = [$venue_model->primary_key_name() => $venue_id]; |
|
1236 | - $rows_affected = $venue_model->update($venue_array, [$update_where]); |
|
1237 | - // we've gotta make sure that the venue is always attached to a revision.. |
|
1238 | - // add_relation_to should take care of making sure that the relation is already present. |
|
1239 | - $evtobj->_add_relation_to($venue_id, 'Venue'); |
|
1240 | - return $rows_affected > 0; |
|
1241 | - } |
|
1242 | - // we insert the venue |
|
1243 | - $venue_id = $venue_model->insert($venue_array); |
|
1244 | - $evtobj->_add_relation_to($venue_id, 'Venue'); |
|
1245 | - return ! empty($venue_id); |
|
1246 | - // when we have the ancestor come in it's already been handled by the revision save. |
|
1247 | - } |
|
1248 | - |
|
1249 | - |
|
1250 | - /** |
|
1251 | - * Handles saving everything related to Tickets (datetimes, tickets, prices) |
|
1252 | - * |
|
1253 | - * @param EE_Event $evtobj The Event object we're attaching data to |
|
1254 | - * @param array $data The request data from the form |
|
1255 | - * @return array |
|
1256 | - * @throws EE_Error |
|
1257 | - * @throws InvalidArgumentException |
|
1258 | - * @throws InvalidDataTypeException |
|
1259 | - * @throws InvalidInterfaceException |
|
1260 | - * @throws ReflectionException |
|
1261 | - * @throws Exception |
|
1262 | - */ |
|
1263 | - protected function _default_tickets_update(EE_Event $evtobj, $data) |
|
1264 | - { |
|
1265 | - if ($this->admin_config->useAdvancedEditor()) { |
|
1266 | - return []; |
|
1267 | - } |
|
1268 | - $saved_dtt = null; |
|
1269 | - $saved_tickets = []; |
|
1270 | - $incoming_date_formats = ['Y-m-d', 'h:i a']; |
|
1271 | - $event_timezone_string = $evtobj->get_timezone(); |
|
1272 | - $event_timezone = new DateTimeZone($event_timezone_string); |
|
1273 | - // let's use now in the set timezone. |
|
1274 | - $now = new DateTime('now', $event_timezone); |
|
1275 | - foreach ($data['edit_event_datetimes'] as $row => $dtt) { |
|
1276 | - // trim all values to ensure any excess whitespace is removed. |
|
1277 | - $dtt = array_map('trim', $dtt); |
|
1278 | - $dtt['DTT_EVT_end'] = isset($dtt['DTT_EVT_end']) && ! empty($dtt['DTT_EVT_end']) |
|
20 | + /** |
|
21 | + * This will hold the event object for event_details screen. |
|
22 | + * |
|
23 | + * @var EE_Event $_event |
|
24 | + */ |
|
25 | + protected $_event; |
|
26 | + |
|
27 | + |
|
28 | + /** |
|
29 | + * This will hold the category object for category_details screen. |
|
30 | + * |
|
31 | + * @var stdClass $_category |
|
32 | + */ |
|
33 | + protected $_category; |
|
34 | + |
|
35 | + |
|
36 | + /** |
|
37 | + * @var EEM_Event $_event_model |
|
38 | + */ |
|
39 | + protected $_event_model; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var EEM_Datetime $datetime_model |
|
43 | + */ |
|
44 | + protected $datetime_model; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var EEM_Ticket $ticket_model |
|
48 | + */ |
|
49 | + protected $ticket_model; |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * @var EE_Event |
|
54 | + */ |
|
55 | + protected $_cpt_model_obj; |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * @var NodeGroupDao |
|
60 | + */ |
|
61 | + protected $model_obj_node_group_persister; |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * Initialize page props for this admin page group. |
|
66 | + */ |
|
67 | + protected function _init_page_props() |
|
68 | + { |
|
69 | + $this->page_slug = EVENTS_PG_SLUG; |
|
70 | + $this->page_label = EVENTS_LABEL; |
|
71 | + $this->_admin_base_url = EVENTS_ADMIN_URL; |
|
72 | + $this->_admin_base_path = EVENTS_ADMIN; |
|
73 | + $this->_cpt_model_names = [ |
|
74 | + 'create_new' => 'EEM_Event', |
|
75 | + 'edit' => 'EEM_Event', |
|
76 | + ]; |
|
77 | + $this->_cpt_edit_routes = [ |
|
78 | + 'espresso_events' => 'edit', |
|
79 | + ]; |
|
80 | + add_action( |
|
81 | + 'AHEE__EE_Admin_Page_CPT__set_model_object__after_set_object', |
|
82 | + [$this, 'verify_event_edit'], |
|
83 | + 10, |
|
84 | + 2 |
|
85 | + ); |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * Sets the ajax hooks used for this admin page group. |
|
91 | + */ |
|
92 | + protected function _ajax_hooks() |
|
93 | + { |
|
94 | + add_action('wp_ajax_ee_save_timezone_setting', [$this, 'save_timezonestring_setting']); |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * Sets the page properties for this admin page group. |
|
100 | + */ |
|
101 | + protected function _define_page_props() |
|
102 | + { |
|
103 | + $this->_admin_page_title = EVENTS_LABEL; |
|
104 | + $this->_labels = [ |
|
105 | + 'buttons' => [ |
|
106 | + 'add' => esc_html__('Add New Event', 'event_espresso'), |
|
107 | + 'edit' => esc_html__('Edit Event', 'event_espresso'), |
|
108 | + 'delete' => esc_html__('Delete Event', 'event_espresso'), |
|
109 | + 'add_category' => esc_html__('Add New Category', 'event_espresso'), |
|
110 | + 'edit_category' => esc_html__('Edit Category', 'event_espresso'), |
|
111 | + 'delete_category' => esc_html__('Delete Category', 'event_espresso'), |
|
112 | + ], |
|
113 | + 'editor_title' => [ |
|
114 | + 'espresso_events' => esc_html__('Enter event title here', 'event_espresso'), |
|
115 | + ], |
|
116 | + 'publishbox' => [ |
|
117 | + 'create_new' => esc_html__('Save New Event', 'event_espresso'), |
|
118 | + 'edit' => esc_html__('Update Event', 'event_espresso'), |
|
119 | + 'add_category' => esc_html__('Save New Category', 'event_espresso'), |
|
120 | + 'edit_category' => esc_html__('Update Category', 'event_espresso'), |
|
121 | + 'template_settings' => esc_html__('Update Settings', 'event_espresso'), |
|
122 | + ], |
|
123 | + ]; |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * Sets the page routes property for this admin page group. |
|
129 | + */ |
|
130 | + protected function _set_page_routes() |
|
131 | + { |
|
132 | + // load formatter helper |
|
133 | + // load field generator helper |
|
134 | + // is there a evt_id in the request? |
|
135 | + $evt_id = ! empty($this->_req_data['EVT_ID']) && ! is_array($this->_req_data['EVT_ID']) |
|
136 | + ? $this->_req_data['EVT_ID'] |
|
137 | + : 0; |
|
138 | + $evt_id = ! empty($this->_req_data['post']) ? $this->_req_data['post'] : $evt_id; |
|
139 | + $this->_page_routes = [ |
|
140 | + 'default' => [ |
|
141 | + 'func' => '_events_overview_list_table', |
|
142 | + 'capability' => 'ee_read_events', |
|
143 | + ], |
|
144 | + 'create_new' => [ |
|
145 | + 'func' => '_create_new_cpt_item', |
|
146 | + 'capability' => 'ee_edit_events', |
|
147 | + ], |
|
148 | + 'edit' => [ |
|
149 | + 'func' => '_edit_cpt_item', |
|
150 | + 'capability' => 'ee_edit_event', |
|
151 | + 'obj_id' => $evt_id, |
|
152 | + ], |
|
153 | + 'copy_event' => [ |
|
154 | + 'func' => '_copy_events', |
|
155 | + 'capability' => 'ee_edit_event', |
|
156 | + 'obj_id' => $evt_id, |
|
157 | + 'noheader' => true, |
|
158 | + ], |
|
159 | + 'trash_event' => [ |
|
160 | + 'func' => '_trash_or_restore_event', |
|
161 | + 'args' => ['event_status' => 'trash'], |
|
162 | + 'capability' => 'ee_delete_event', |
|
163 | + 'obj_id' => $evt_id, |
|
164 | + 'noheader' => true, |
|
165 | + ], |
|
166 | + 'trash_events' => [ |
|
167 | + 'func' => '_trash_or_restore_events', |
|
168 | + 'args' => ['event_status' => 'trash'], |
|
169 | + 'capability' => 'ee_delete_events', |
|
170 | + 'noheader' => true, |
|
171 | + ], |
|
172 | + 'restore_event' => [ |
|
173 | + 'func' => '_trash_or_restore_event', |
|
174 | + 'args' => ['event_status' => 'draft'], |
|
175 | + 'capability' => 'ee_delete_event', |
|
176 | + 'obj_id' => $evt_id, |
|
177 | + 'noheader' => true, |
|
178 | + ], |
|
179 | + 'restore_events' => [ |
|
180 | + 'func' => '_trash_or_restore_events', |
|
181 | + 'args' => ['event_status' => 'draft'], |
|
182 | + 'capability' => 'ee_delete_events', |
|
183 | + 'noheader' => true, |
|
184 | + ], |
|
185 | + 'delete_event' => [ |
|
186 | + 'func' => '_delete_event', |
|
187 | + 'capability' => 'ee_delete_event', |
|
188 | + 'obj_id' => $evt_id, |
|
189 | + 'noheader' => true, |
|
190 | + ], |
|
191 | + 'delete_events' => [ |
|
192 | + 'func' => '_delete_events', |
|
193 | + 'capability' => 'ee_delete_events', |
|
194 | + 'noheader' => true, |
|
195 | + ], |
|
196 | + 'view_report' => [ |
|
197 | + 'func' => '_view_report', |
|
198 | + 'capability' => 'ee_edit_events', |
|
199 | + ], |
|
200 | + 'default_event_settings' => [ |
|
201 | + 'func' => '_default_event_settings', |
|
202 | + 'capability' => 'manage_options', |
|
203 | + ], |
|
204 | + 'update_default_event_settings' => [ |
|
205 | + 'func' => '_update_default_event_settings', |
|
206 | + 'capability' => 'manage_options', |
|
207 | + 'noheader' => true, |
|
208 | + ], |
|
209 | + 'template_settings' => [ |
|
210 | + 'func' => '_template_settings', |
|
211 | + 'capability' => 'manage_options', |
|
212 | + ], |
|
213 | + // event category tab related |
|
214 | + 'add_category' => [ |
|
215 | + 'func' => '_category_details', |
|
216 | + 'capability' => 'ee_edit_event_category', |
|
217 | + 'args' => ['add'], |
|
218 | + ], |
|
219 | + 'edit_category' => [ |
|
220 | + 'func' => '_category_details', |
|
221 | + 'capability' => 'ee_edit_event_category', |
|
222 | + 'args' => ['edit'], |
|
223 | + ], |
|
224 | + 'delete_categories' => [ |
|
225 | + 'func' => '_delete_categories', |
|
226 | + 'capability' => 'ee_delete_event_category', |
|
227 | + 'noheader' => true, |
|
228 | + ], |
|
229 | + 'delete_category' => [ |
|
230 | + 'func' => '_delete_categories', |
|
231 | + 'capability' => 'ee_delete_event_category', |
|
232 | + 'noheader' => true, |
|
233 | + ], |
|
234 | + 'insert_category' => [ |
|
235 | + 'func' => '_insert_or_update_category', |
|
236 | + 'args' => ['new_category' => true], |
|
237 | + 'capability' => 'ee_edit_event_category', |
|
238 | + 'noheader' => true, |
|
239 | + ], |
|
240 | + 'update_category' => [ |
|
241 | + 'func' => '_insert_or_update_category', |
|
242 | + 'args' => ['new_category' => false], |
|
243 | + 'capability' => 'ee_edit_event_category', |
|
244 | + 'noheader' => true, |
|
245 | + ], |
|
246 | + 'category_list' => [ |
|
247 | + 'func' => '_category_list_table', |
|
248 | + 'capability' => 'ee_manage_event_categories', |
|
249 | + ], |
|
250 | + 'preview_deletion' => [ |
|
251 | + 'func' => 'previewDeletion', |
|
252 | + 'capability' => 'ee_delete_events', |
|
253 | + ], |
|
254 | + 'confirm_deletion' => [ |
|
255 | + 'func' => 'confirmDeletion', |
|
256 | + 'capability' => 'ee_delete_events', |
|
257 | + 'noheader' => true, |
|
258 | + ], |
|
259 | + ]; |
|
260 | + } |
|
261 | + |
|
262 | + |
|
263 | + /** |
|
264 | + * Set the _page_config property for this admin page group. |
|
265 | + */ |
|
266 | + protected function _set_page_config() |
|
267 | + { |
|
268 | + $this->_page_config = [ |
|
269 | + 'default' => [ |
|
270 | + 'nav' => [ |
|
271 | + 'label' => esc_html__('Overview', 'event_espresso'), |
|
272 | + 'order' => 10, |
|
273 | + ], |
|
274 | + 'list_table' => 'Events_Admin_List_Table', |
|
275 | + 'help_tabs' => [ |
|
276 | + 'events_overview_help_tab' => [ |
|
277 | + 'title' => esc_html__('Events Overview', 'event_espresso'), |
|
278 | + 'filename' => 'events_overview', |
|
279 | + ], |
|
280 | + 'events_overview_table_column_headings_help_tab' => [ |
|
281 | + 'title' => esc_html__('Events Overview Table Column Headings', 'event_espresso'), |
|
282 | + 'filename' => 'events_overview_table_column_headings', |
|
283 | + ], |
|
284 | + 'events_overview_filters_help_tab' => [ |
|
285 | + 'title' => esc_html__('Events Overview Filters', 'event_espresso'), |
|
286 | + 'filename' => 'events_overview_filters', |
|
287 | + ], |
|
288 | + 'events_overview_view_help_tab' => [ |
|
289 | + 'title' => esc_html__('Events Overview Views', 'event_espresso'), |
|
290 | + 'filename' => 'events_overview_views', |
|
291 | + ], |
|
292 | + 'events_overview_other_help_tab' => [ |
|
293 | + 'title' => esc_html__('Events Overview Other', 'event_espresso'), |
|
294 | + 'filename' => 'events_overview_other', |
|
295 | + ], |
|
296 | + ], |
|
297 | + // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836 |
|
298 | + // 'help_tour' => [ |
|
299 | + // 'Event_Overview_Help_Tour', |
|
300 | + // // 'New_Features_Test_Help_Tour' for testing multiple help tour |
|
301 | + // ], |
|
302 | + 'qtips' => ['EE_Event_List_Table_Tips'], |
|
303 | + 'require_nonce' => false, |
|
304 | + ], |
|
305 | + 'create_new' => [ |
|
306 | + 'nav' => [ |
|
307 | + 'label' => esc_html__('Add Event', 'event_espresso'), |
|
308 | + 'order' => 5, |
|
309 | + 'persistent' => false, |
|
310 | + ], |
|
311 | + 'metaboxes' => ['_register_event_editor_meta_boxes'], |
|
312 | + 'help_tabs' => [ |
|
313 | + 'event_editor_help_tab' => [ |
|
314 | + 'title' => esc_html__('Event Editor', 'event_espresso'), |
|
315 | + 'filename' => 'event_editor', |
|
316 | + ], |
|
317 | + 'event_editor_title_richtexteditor_help_tab' => [ |
|
318 | + 'title' => esc_html__('Event Title & Rich Text Editor', 'event_espresso'), |
|
319 | + 'filename' => 'event_editor_title_richtexteditor', |
|
320 | + ], |
|
321 | + 'event_editor_venue_details_help_tab' => [ |
|
322 | + 'title' => esc_html__('Event Venue Details', 'event_espresso'), |
|
323 | + 'filename' => 'event_editor_venue_details', |
|
324 | + ], |
|
325 | + 'event_editor_event_datetimes_help_tab' => [ |
|
326 | + 'title' => esc_html__('Event Datetimes', 'event_espresso'), |
|
327 | + 'filename' => 'event_editor_event_datetimes', |
|
328 | + ], |
|
329 | + 'event_editor_event_tickets_help_tab' => [ |
|
330 | + 'title' => esc_html__('Event Tickets', 'event_espresso'), |
|
331 | + 'filename' => 'event_editor_event_tickets', |
|
332 | + ], |
|
333 | + 'event_editor_event_registration_options_help_tab' => [ |
|
334 | + 'title' => esc_html__('Event Registration Options', 'event_espresso'), |
|
335 | + 'filename' => 'event_editor_event_registration_options', |
|
336 | + ], |
|
337 | + 'event_editor_tags_categories_help_tab' => [ |
|
338 | + 'title' => esc_html__('Event Tags & Categories', 'event_espresso'), |
|
339 | + 'filename' => 'event_editor_tags_categories', |
|
340 | + ], |
|
341 | + 'event_editor_questions_registrants_help_tab' => [ |
|
342 | + 'title' => esc_html__('Questions for Registrants', 'event_espresso'), |
|
343 | + 'filename' => 'event_editor_questions_registrants', |
|
344 | + ], |
|
345 | + 'event_editor_save_new_event_help_tab' => [ |
|
346 | + 'title' => esc_html__('Save New Event', 'event_espresso'), |
|
347 | + 'filename' => 'event_editor_save_new_event', |
|
348 | + ], |
|
349 | + 'event_editor_other_help_tab' => [ |
|
350 | + 'title' => esc_html__('Event Other', 'event_espresso'), |
|
351 | + 'filename' => 'event_editor_other', |
|
352 | + ], |
|
353 | + ], |
|
354 | + // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836 |
|
355 | + // 'help_tour' => [ |
|
356 | + // 'Event_Editor_Help_Tour', |
|
357 | + // ], |
|
358 | + 'qtips' => ['EE_Event_Editor_Decaf_Tips'], |
|
359 | + 'require_nonce' => false, |
|
360 | + ], |
|
361 | + 'edit' => [ |
|
362 | + 'nav' => [ |
|
363 | + 'label' => esc_html__('Edit Event', 'event_espresso'), |
|
364 | + 'order' => 5, |
|
365 | + 'persistent' => false, |
|
366 | + 'url' => isset($this->_req_data['post']) |
|
367 | + ? EE_Admin_Page::add_query_args_and_nonce( |
|
368 | + ['post' => $this->_req_data['post'], 'action' => 'edit'], |
|
369 | + $this->_current_page_view_url |
|
370 | + ) |
|
371 | + : $this->_admin_base_url, |
|
372 | + ], |
|
373 | + 'metaboxes' => ['_register_event_editor_meta_boxes'], |
|
374 | + 'help_tabs' => [ |
|
375 | + 'event_editor_help_tab' => [ |
|
376 | + 'title' => esc_html__('Event Editor', 'event_espresso'), |
|
377 | + 'filename' => 'event_editor', |
|
378 | + ], |
|
379 | + 'event_editor_title_richtexteditor_help_tab' => [ |
|
380 | + 'title' => esc_html__('Event Title & Rich Text Editor', 'event_espresso'), |
|
381 | + 'filename' => 'event_editor_title_richtexteditor', |
|
382 | + ], |
|
383 | + 'event_editor_venue_details_help_tab' => [ |
|
384 | + 'title' => esc_html__('Event Venue Details', 'event_espresso'), |
|
385 | + 'filename' => 'event_editor_venue_details', |
|
386 | + ], |
|
387 | + 'event_editor_event_datetimes_help_tab' => [ |
|
388 | + 'title' => esc_html__('Event Datetimes', 'event_espresso'), |
|
389 | + 'filename' => 'event_editor_event_datetimes', |
|
390 | + ], |
|
391 | + 'event_editor_event_tickets_help_tab' => [ |
|
392 | + 'title' => esc_html__('Event Tickets', 'event_espresso'), |
|
393 | + 'filename' => 'event_editor_event_tickets', |
|
394 | + ], |
|
395 | + 'event_editor_event_registration_options_help_tab' => [ |
|
396 | + 'title' => esc_html__('Event Registration Options', 'event_espresso'), |
|
397 | + 'filename' => 'event_editor_event_registration_options', |
|
398 | + ], |
|
399 | + 'event_editor_tags_categories_help_tab' => [ |
|
400 | + 'title' => esc_html__('Event Tags & Categories', 'event_espresso'), |
|
401 | + 'filename' => 'event_editor_tags_categories', |
|
402 | + ], |
|
403 | + 'event_editor_questions_registrants_help_tab' => [ |
|
404 | + 'title' => esc_html__('Questions for Registrants', 'event_espresso'), |
|
405 | + 'filename' => 'event_editor_questions_registrants', |
|
406 | + ], |
|
407 | + 'event_editor_save_new_event_help_tab' => [ |
|
408 | + 'title' => esc_html__('Save New Event', 'event_espresso'), |
|
409 | + 'filename' => 'event_editor_save_new_event', |
|
410 | + ], |
|
411 | + 'event_editor_other_help_tab' => [ |
|
412 | + 'title' => esc_html__('Event Other', 'event_espresso'), |
|
413 | + 'filename' => 'event_editor_other', |
|
414 | + ], |
|
415 | + ], |
|
416 | + 'require_nonce' => false, |
|
417 | + ], |
|
418 | + 'default_event_settings' => [ |
|
419 | + 'nav' => [ |
|
420 | + 'label' => esc_html__('Default Settings', 'event_espresso'), |
|
421 | + 'order' => 40, |
|
422 | + ], |
|
423 | + 'metaboxes' => array_merge($this->_default_espresso_metaboxes, ['_publish_post_box']), |
|
424 | + 'labels' => [ |
|
425 | + 'publishbox' => esc_html__('Update Settings', 'event_espresso'), |
|
426 | + ], |
|
427 | + 'help_tabs' => [ |
|
428 | + 'default_settings_help_tab' => [ |
|
429 | + 'title' => esc_html__('Default Event Settings', 'event_espresso'), |
|
430 | + 'filename' => 'events_default_settings', |
|
431 | + ], |
|
432 | + 'default_settings_status_help_tab' => [ |
|
433 | + 'title' => esc_html__('Default Registration Status', 'event_espresso'), |
|
434 | + 'filename' => 'events_default_settings_status', |
|
435 | + ], |
|
436 | + 'default_maximum_tickets_help_tab' => [ |
|
437 | + 'title' => esc_html__('Default Maximum Tickets Per Order', 'event_espresso'), |
|
438 | + 'filename' => 'events_default_settings_max_tickets', |
|
439 | + ], |
|
440 | + ], |
|
441 | + // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836 |
|
442 | + // 'help_tour' => ['Event_Default_Settings_Help_Tour'], |
|
443 | + 'require_nonce' => false, |
|
444 | + ], |
|
445 | + // template settings |
|
446 | + 'template_settings' => [ |
|
447 | + 'nav' => [ |
|
448 | + 'label' => esc_html__('Templates', 'event_espresso'), |
|
449 | + 'order' => 30, |
|
450 | + ], |
|
451 | + 'metaboxes' => $this->_default_espresso_metaboxes, |
|
452 | + 'help_tabs' => [ |
|
453 | + 'general_settings_templates_help_tab' => [ |
|
454 | + 'title' => esc_html__('Templates', 'event_espresso'), |
|
455 | + 'filename' => 'general_settings_templates', |
|
456 | + ], |
|
457 | + ], |
|
458 | + // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836 |
|
459 | + // 'help_tour' => ['Templates_Help_Tour'], |
|
460 | + 'require_nonce' => false, |
|
461 | + ], |
|
462 | + // event category stuff |
|
463 | + 'add_category' => [ |
|
464 | + 'nav' => [ |
|
465 | + 'label' => esc_html__('Add Category', 'event_espresso'), |
|
466 | + 'order' => 15, |
|
467 | + 'persistent' => false, |
|
468 | + ], |
|
469 | + 'help_tabs' => [ |
|
470 | + 'add_category_help_tab' => [ |
|
471 | + 'title' => esc_html__('Add New Event Category', 'event_espresso'), |
|
472 | + 'filename' => 'events_add_category', |
|
473 | + ], |
|
474 | + ], |
|
475 | + // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836 |
|
476 | + // 'help_tour' => ['Event_Add_Category_Help_Tour'], |
|
477 | + 'metaboxes' => ['_publish_post_box'], |
|
478 | + 'require_nonce' => false, |
|
479 | + ], |
|
480 | + 'edit_category' => [ |
|
481 | + 'nav' => [ |
|
482 | + 'label' => esc_html__('Edit Category', 'event_espresso'), |
|
483 | + 'order' => 15, |
|
484 | + 'persistent' => false, |
|
485 | + 'url' => isset($this->_req_data['EVT_CAT_ID']) |
|
486 | + ? add_query_arg( |
|
487 | + ['EVT_CAT_ID' => $this->_req_data['EVT_CAT_ID']], |
|
488 | + $this->_current_page_view_url |
|
489 | + ) |
|
490 | + : $this->_admin_base_url, |
|
491 | + ], |
|
492 | + 'help_tabs' => [ |
|
493 | + 'edit_category_help_tab' => [ |
|
494 | + 'title' => esc_html__('Edit Event Category', 'event_espresso'), |
|
495 | + 'filename' => 'events_edit_category', |
|
496 | + ], |
|
497 | + ], |
|
498 | + /*'help_tour' => ['Event_Edit_Category_Help_Tour'],*/ |
|
499 | + 'metaboxes' => ['_publish_post_box'], |
|
500 | + 'require_nonce' => false, |
|
501 | + ], |
|
502 | + 'category_list' => [ |
|
503 | + 'nav' => [ |
|
504 | + 'label' => esc_html__('Categories', 'event_espresso'), |
|
505 | + 'order' => 20, |
|
506 | + ], |
|
507 | + 'list_table' => 'Event_Categories_Admin_List_Table', |
|
508 | + 'help_tabs' => [ |
|
509 | + 'events_categories_help_tab' => [ |
|
510 | + 'title' => esc_html__('Event Categories', 'event_espresso'), |
|
511 | + 'filename' => 'events_categories', |
|
512 | + ], |
|
513 | + 'events_categories_table_column_headings_help_tab' => [ |
|
514 | + 'title' => esc_html__('Event Categories Table Column Headings', 'event_espresso'), |
|
515 | + 'filename' => 'events_categories_table_column_headings', |
|
516 | + ], |
|
517 | + 'events_categories_view_help_tab' => [ |
|
518 | + 'title' => esc_html__('Event Categories Views', 'event_espresso'), |
|
519 | + 'filename' => 'events_categories_views', |
|
520 | + ], |
|
521 | + 'events_categories_other_help_tab' => [ |
|
522 | + 'title' => esc_html__('Event Categories Other', 'event_espresso'), |
|
523 | + 'filename' => 'events_categories_other', |
|
524 | + ], |
|
525 | + ], |
|
526 | + // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836 |
|
527 | + // 'help_tour' => [ |
|
528 | + // 'Event_Categories_Help_Tour', |
|
529 | + // ], |
|
530 | + 'metaboxes' => $this->_default_espresso_metaboxes, |
|
531 | + 'require_nonce' => false, |
|
532 | + ], |
|
533 | + 'preview_deletion' => [ |
|
534 | + 'nav' => [ |
|
535 | + 'label' => esc_html__('Preview Deletion', 'event_espresso'), |
|
536 | + 'order' => 15, |
|
537 | + 'persistent' => false, |
|
538 | + 'url' => '', |
|
539 | + ], |
|
540 | + 'require_nonce' => false, |
|
541 | + ], |
|
542 | + ]; |
|
543 | + // only load EE_Event_Editor_Decaf_Tips if domain is not caffeinated |
|
544 | + $domain = $this->loader->getShared('EventEspresso\core\domain\Domain'); |
|
545 | + if (! $domain->isCaffeinated()) { |
|
546 | + $this->_page_config['create_new']['qtips'] = ['EE_Event_Editor_Decaf_Tips']; |
|
547 | + $this->_page_config['edit']['qtips'] = ['EE_Event_Editor_Decaf_Tips']; |
|
548 | + } |
|
549 | + } |
|
550 | + |
|
551 | + |
|
552 | + /** |
|
553 | + * Used to register any global screen options if necessary for every route in this admin page group. |
|
554 | + */ |
|
555 | + protected function _add_screen_options() |
|
556 | + { |
|
557 | + } |
|
558 | + |
|
559 | + |
|
560 | + /** |
|
561 | + * Implementing the screen options for the 'default' route. |
|
562 | + * |
|
563 | + * @throws InvalidArgumentException |
|
564 | + * @throws InvalidDataTypeException |
|
565 | + * @throws InvalidInterfaceException |
|
566 | + */ |
|
567 | + protected function _add_screen_options_default() |
|
568 | + { |
|
569 | + $this->_per_page_screen_option(); |
|
570 | + } |
|
571 | + |
|
572 | + |
|
573 | + /** |
|
574 | + * Implementing screen options for the category list route. |
|
575 | + * |
|
576 | + * @throws InvalidArgumentException |
|
577 | + * @throws InvalidDataTypeException |
|
578 | + * @throws InvalidInterfaceException |
|
579 | + */ |
|
580 | + protected function _add_screen_options_category_list() |
|
581 | + { |
|
582 | + $page_title = $this->_admin_page_title; |
|
583 | + $this->_admin_page_title = esc_html__('Categories', 'event_espresso'); |
|
584 | + $this->_per_page_screen_option(); |
|
585 | + $this->_admin_page_title = $page_title; |
|
586 | + } |
|
587 | + |
|
588 | + |
|
589 | + /** |
|
590 | + * Used to register any global feature pointers for the admin page group. |
|
591 | + */ |
|
592 | + protected function _add_feature_pointers() |
|
593 | + { |
|
594 | + } |
|
595 | + |
|
596 | + |
|
597 | + /** |
|
598 | + * Registers and enqueues any global scripts and styles for the entire admin page group. |
|
599 | + */ |
|
600 | + public function load_scripts_styles() |
|
601 | + { |
|
602 | + wp_register_style( |
|
603 | + 'events-admin-css', |
|
604 | + EVENTS_ASSETS_URL . 'events-admin-page.css', |
|
605 | + [], |
|
606 | + EVENT_ESPRESSO_VERSION |
|
607 | + ); |
|
608 | + wp_register_style( |
|
609 | + 'ee-cat-admin', |
|
610 | + EVENTS_ASSETS_URL . 'ee-cat-admin.css', |
|
611 | + [], |
|
612 | + EVENT_ESPRESSO_VERSION |
|
613 | + ); |
|
614 | + wp_enqueue_style('events-admin-css'); |
|
615 | + wp_enqueue_style('ee-cat-admin'); |
|
616 | + // scripts |
|
617 | + wp_register_script( |
|
618 | + 'event_editor_js', |
|
619 | + EVENTS_ASSETS_URL . 'event_editor.js', |
|
620 | + ['ee_admin_js', 'jquery-ui-slider', 'jquery-ui-timepicker-addon'], |
|
621 | + EVENT_ESPRESSO_VERSION, |
|
622 | + true |
|
623 | + ); |
|
624 | + } |
|
625 | + |
|
626 | + |
|
627 | + /** |
|
628 | + * Enqueuing scripts and styles specific to this view |
|
629 | + */ |
|
630 | + public function load_scripts_styles_create_new() |
|
631 | + { |
|
632 | + $this->load_scripts_styles_edit(); |
|
633 | + } |
|
634 | + |
|
635 | + |
|
636 | + /** |
|
637 | + * Enqueuing scripts and styles specific to this view |
|
638 | + */ |
|
639 | + public function load_scripts_styles_edit() |
|
640 | + { |
|
641 | + // styles |
|
642 | + wp_enqueue_style('espresso-ui-theme'); |
|
643 | + wp_register_style( |
|
644 | + 'event-editor-css', |
|
645 | + EVENTS_ASSETS_URL . 'event-editor.css', |
|
646 | + ['ee-admin-css'], |
|
647 | + EVENT_ESPRESSO_VERSION |
|
648 | + ); |
|
649 | + wp_enqueue_style('event-editor-css'); |
|
650 | + // scripts |
|
651 | + if (! $this->admin_config->useAdvancedEditor()) { |
|
652 | + wp_register_script( |
|
653 | + 'event-datetime-metabox', |
|
654 | + EVENTS_ASSETS_URL . 'event-datetime-metabox.js', |
|
655 | + ['event_editor_js', 'ee-datepicker'], |
|
656 | + EVENT_ESPRESSO_VERSION |
|
657 | + ); |
|
658 | + wp_enqueue_script('event-datetime-metabox'); |
|
659 | + } |
|
660 | + } |
|
661 | + |
|
662 | + |
|
663 | + /** |
|
664 | + * Populating the _views property for the category list table view. |
|
665 | + */ |
|
666 | + protected function _set_list_table_views_category_list() |
|
667 | + { |
|
668 | + $this->_views = [ |
|
669 | + 'all' => [ |
|
670 | + 'slug' => 'all', |
|
671 | + 'label' => esc_html__('All', 'event_espresso'), |
|
672 | + 'count' => 0, |
|
673 | + 'bulk_action' => [ |
|
674 | + 'delete_categories' => esc_html__('Delete Permanently', 'event_espresso'), |
|
675 | + ], |
|
676 | + ], |
|
677 | + ]; |
|
678 | + } |
|
679 | + |
|
680 | + |
|
681 | + /** |
|
682 | + * For adding anything that fires on the admin_init hook for any route within this admin page group. |
|
683 | + */ |
|
684 | + public function admin_init() |
|
685 | + { |
|
686 | + EE_Registry::$i18n_js_strings['image_confirm'] = esc_html__( |
|
687 | + 'Do you really want to delete this image? Please remember to update your event to complete the removal.', |
|
688 | + 'event_espresso' |
|
689 | + ); |
|
690 | + } |
|
691 | + |
|
692 | + |
|
693 | + /** |
|
694 | + * For adding anything that should be triggered on the admin_notices hook for any route within this admin page |
|
695 | + * group. |
|
696 | + */ |
|
697 | + public function admin_notices() |
|
698 | + { |
|
699 | + } |
|
700 | + |
|
701 | + |
|
702 | + /** |
|
703 | + * For adding anything that should be triggered on the `admin_print_footer_scripts` hook for any route within |
|
704 | + * this admin page group. |
|
705 | + */ |
|
706 | + public function admin_footer_scripts() |
|
707 | + { |
|
708 | + } |
|
709 | + |
|
710 | + |
|
711 | + /** |
|
712 | + * Call this function to verify if an event is public and has tickets for sale. If it does, then we need to show a |
|
713 | + * warning (via EE_Error::add_error()); |
|
714 | + * |
|
715 | + * @param EE_Event $event Event object |
|
716 | + * @param string $req_type |
|
717 | + * @return void |
|
718 | + * @throws EE_Error |
|
719 | + */ |
|
720 | + public function verify_event_edit($event = null, $req_type = '') |
|
721 | + { |
|
722 | + // don't need to do this when processing |
|
723 | + if (! empty($req_type)) { |
|
724 | + return; |
|
725 | + } |
|
726 | + // no event? |
|
727 | + if (! $event instanceof EE_Event) { |
|
728 | + $event = $this->_cpt_model_obj; |
|
729 | + } |
|
730 | + // STILL no event? |
|
731 | + if (! $event instanceof EE_Event) { |
|
732 | + return; |
|
733 | + } |
|
734 | + $orig_status = $event->status(); |
|
735 | + // first check if event is active. |
|
736 | + if ( |
|
737 | + $orig_status === EEM_Event::cancelled |
|
738 | + || $orig_status === EEM_Event::postponed |
|
739 | + || $event->is_expired() |
|
740 | + || $event->is_inactive() |
|
741 | + ) { |
|
742 | + return; |
|
743 | + } |
|
744 | + // made it here so it IS active... next check that any of the tickets are sold. |
|
745 | + if ($event->is_sold_out(true)) { |
|
746 | + if ($orig_status !== EEM_Event::sold_out && $event->status() !== $orig_status) { |
|
747 | + EE_Error::add_attention( |
|
748 | + sprintf( |
|
749 | + esc_html__( |
|
750 | + 'Please note that the Event Status has automatically been changed to %s because there are no more spaces available for this event. However, this change is not permanent until you update the event. You can change the status back to something else before updating if you wish.', |
|
751 | + 'event_espresso' |
|
752 | + ), |
|
753 | + EEH_Template::pretty_status(EEM_Event::sold_out, false, 'sentence') |
|
754 | + ) |
|
755 | + ); |
|
756 | + } |
|
757 | + return; |
|
758 | + } |
|
759 | + if ($orig_status === EEM_Event::sold_out) { |
|
760 | + EE_Error::add_attention( |
|
761 | + sprintf( |
|
762 | + esc_html__( |
|
763 | + 'Please note that the Event Status has automatically been changed to %s because more spaces have become available for this event, most likely due to abandoned transactions freeing up reserved tickets. However, this change is not permanent until you update the event. If you wish, you can change the status back to something else before updating.', |
|
764 | + 'event_espresso' |
|
765 | + ), |
|
766 | + EEH_Template::pretty_status($event->status(), false, 'sentence') |
|
767 | + ) |
|
768 | + ); |
|
769 | + } |
|
770 | + // now we need to determine if the event has any tickets on sale. If not then we dont' show the error |
|
771 | + if (! $event->tickets_on_sale()) { |
|
772 | + return; |
|
773 | + } |
|
774 | + // made it here so show warning |
|
775 | + $this->_edit_event_warning(); |
|
776 | + } |
|
777 | + |
|
778 | + |
|
779 | + /** |
|
780 | + * This is the text used for when an event is being edited that is public and has tickets for sale. |
|
781 | + * When needed, hook this into a EE_Error::add_error() notice. |
|
782 | + * |
|
783 | + * @return void |
|
784 | + */ |
|
785 | + protected function _edit_event_warning() |
|
786 | + { |
|
787 | + // we don't want to add warnings during these requests |
|
788 | + if (isset($this->_req_data['action']) && $this->_req_data['action'] === 'editpost') { |
|
789 | + return; |
|
790 | + } |
|
791 | + EE_Error::add_attention( |
|
792 | + sprintf( |
|
793 | + esc_html__( |
|
794 | + 'Your event is open for registration. Making changes may disrupt any transactions in progress. %sLearn more%s', |
|
795 | + 'event_espresso' |
|
796 | + ), |
|
797 | + '<a class="espresso-help-tab-lnk">', |
|
798 | + '</a>' |
|
799 | + ) |
|
800 | + ); |
|
801 | + } |
|
802 | + |
|
803 | + |
|
804 | + /** |
|
805 | + * When a user is creating a new event, notify them if they haven't set their timezone. |
|
806 | + * Otherwise, do the normal logic |
|
807 | + * |
|
808 | + * @return void |
|
809 | + * @throws EE_Error |
|
810 | + * @throws InvalidArgumentException |
|
811 | + * @throws InvalidDataTypeException |
|
812 | + */ |
|
813 | + protected function _create_new_cpt_item() |
|
814 | + { |
|
815 | + $has_timezone_string = get_option('timezone_string'); |
|
816 | + // only nag them about setting their timezone if it's their first event, and they haven't already done it |
|
817 | + if (! $has_timezone_string && ! EEM_Event::instance()->exists([])) { |
|
818 | + EE_Error::add_attention( |
|
819 | + sprintf( |
|
820 | + __( |
|
821 | + 'Your website\'s timezone is currently set to a UTC offset. We recommend updating your timezone to a city or region near you before you create an event. Change your timezone now:%1$s%2$s%3$sChange Timezone%4$s', |
|
822 | + 'event_espresso' |
|
823 | + ), |
|
824 | + '<br>', |
|
825 | + '<select id="timezone_string" name="timezone_string" aria-describedby="timezone-description">' |
|
826 | + . EEH_DTT_Helper::wp_timezone_choice('', EEH_DTT_Helper::get_user_locale()) |
|
827 | + . '</select>', |
|
828 | + '<button class="button button-secondary timezone-submit">', |
|
829 | + '</button><span class="spinner"></span>' |
|
830 | + ), |
|
831 | + __FILE__, |
|
832 | + __FUNCTION__, |
|
833 | + __LINE__ |
|
834 | + ); |
|
835 | + } |
|
836 | + parent::_create_new_cpt_item(); |
|
837 | + } |
|
838 | + |
|
839 | + |
|
840 | + /** |
|
841 | + * Sets the _views property for the default route in this admin page group. |
|
842 | + */ |
|
843 | + protected function _set_list_table_views_default() |
|
844 | + { |
|
845 | + $this->_views = [ |
|
846 | + 'all' => [ |
|
847 | + 'slug' => 'all', |
|
848 | + 'label' => esc_html__('View All Events', 'event_espresso'), |
|
849 | + 'count' => 0, |
|
850 | + 'bulk_action' => [ |
|
851 | + 'trash_events' => esc_html__('Move to Trash', 'event_espresso'), |
|
852 | + ], |
|
853 | + ], |
|
854 | + 'draft' => [ |
|
855 | + 'slug' => 'draft', |
|
856 | + 'label' => esc_html__('Draft', 'event_espresso'), |
|
857 | + 'count' => 0, |
|
858 | + 'bulk_action' => [ |
|
859 | + 'trash_events' => esc_html__('Move to Trash', 'event_espresso'), |
|
860 | + ], |
|
861 | + ], |
|
862 | + ]; |
|
863 | + if (EE_Registry::instance()->CAP->current_user_can('ee_delete_events', 'espresso_events_trash_events')) { |
|
864 | + $this->_views['trash'] = [ |
|
865 | + 'slug' => 'trash', |
|
866 | + 'label' => esc_html__('Trash', 'event_espresso'), |
|
867 | + 'count' => 0, |
|
868 | + 'bulk_action' => [ |
|
869 | + 'restore_events' => esc_html__('Restore From Trash', 'event_espresso'), |
|
870 | + 'delete_events' => esc_html__('Delete Permanently', 'event_espresso'), |
|
871 | + ], |
|
872 | + ]; |
|
873 | + } |
|
874 | + } |
|
875 | + |
|
876 | + |
|
877 | + /** |
|
878 | + * Provides the legend item array for the default list table view. |
|
879 | + * |
|
880 | + * @return array |
|
881 | + */ |
|
882 | + protected function _event_legend_items() |
|
883 | + { |
|
884 | + $items = [ |
|
885 | + 'view_details' => [ |
|
886 | + 'class' => 'dashicons dashicons-search', |
|
887 | + 'desc' => esc_html__('View Event', 'event_espresso'), |
|
888 | + ], |
|
889 | + 'edit_event' => [ |
|
890 | + 'class' => 'ee-icon ee-icon-calendar-edit', |
|
891 | + 'desc' => esc_html__('Edit Event Details', 'event_espresso'), |
|
892 | + ], |
|
893 | + 'view_attendees' => [ |
|
894 | + 'class' => 'dashicons dashicons-groups', |
|
895 | + 'desc' => esc_html__('View Registrations for Event', 'event_espresso'), |
|
896 | + ], |
|
897 | + ]; |
|
898 | + $items = apply_filters('FHEE__Events_Admin_Page___event_legend_items__items', $items); |
|
899 | + $statuses = [ |
|
900 | + 'sold_out_status' => [ |
|
901 | + 'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::sold_out, |
|
902 | + 'desc' => EEH_Template::pretty_status(EE_Datetime::sold_out, false, 'sentence'), |
|
903 | + ], |
|
904 | + 'active_status' => [ |
|
905 | + 'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::active, |
|
906 | + 'desc' => EEH_Template::pretty_status(EE_Datetime::active, false, 'sentence'), |
|
907 | + ], |
|
908 | + 'upcoming_status' => [ |
|
909 | + 'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::upcoming, |
|
910 | + 'desc' => EEH_Template::pretty_status(EE_Datetime::upcoming, false, 'sentence'), |
|
911 | + ], |
|
912 | + 'postponed_status' => [ |
|
913 | + 'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::postponed, |
|
914 | + 'desc' => EEH_Template::pretty_status(EE_Datetime::postponed, false, 'sentence'), |
|
915 | + ], |
|
916 | + 'cancelled_status' => [ |
|
917 | + 'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::cancelled, |
|
918 | + 'desc' => EEH_Template::pretty_status(EE_Datetime::cancelled, false, 'sentence'), |
|
919 | + ], |
|
920 | + 'expired_status' => [ |
|
921 | + 'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::expired, |
|
922 | + 'desc' => EEH_Template::pretty_status(EE_Datetime::expired, false, 'sentence'), |
|
923 | + ], |
|
924 | + 'inactive_status' => [ |
|
925 | + 'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::inactive, |
|
926 | + 'desc' => EEH_Template::pretty_status(EE_Datetime::inactive, false, 'sentence'), |
|
927 | + ], |
|
928 | + ]; |
|
929 | + $statuses = apply_filters('FHEE__Events_Admin_Page__event_legend_items__statuses', $statuses); |
|
930 | + return array_merge($items, $statuses); |
|
931 | + } |
|
932 | + |
|
933 | + |
|
934 | + /** |
|
935 | + * @return EEM_Event |
|
936 | + * @throws EE_Error |
|
937 | + */ |
|
938 | + private function eventModel() |
|
939 | + { |
|
940 | + if (! $this->_event_model instanceof EEM_Event) { |
|
941 | + $this->_event_model = EEM_Event::instance(); |
|
942 | + } |
|
943 | + return $this->_event_model; |
|
944 | + } |
|
945 | + |
|
946 | + |
|
947 | + /** |
|
948 | + * @param string $event_timezone_string |
|
949 | + * @return EEM_Datetime |
|
950 | + * @throws EE_Error |
|
951 | + */ |
|
952 | + private function datetimeModel($event_timezone_string = '') |
|
953 | + { |
|
954 | + if (! $this->datetime_model instanceof EEM_Datetime) { |
|
955 | + $this->datetime_model = EEM_Datetime::instance($event_timezone_string); |
|
956 | + } |
|
957 | + return $this->datetime_model; |
|
958 | + } |
|
959 | + |
|
960 | + |
|
961 | + /** |
|
962 | + * @param string $event_timezone_string |
|
963 | + * @return EEM_Ticket |
|
964 | + * @throws EE_Error |
|
965 | + */ |
|
966 | + private function ticketModel($event_timezone_string = '') |
|
967 | + { |
|
968 | + if (! $this->ticket_model instanceof EEM_Ticket) { |
|
969 | + $this->ticket_model = EEM_Ticket::instance($event_timezone_string); |
|
970 | + } |
|
971 | + return $this->ticket_model; |
|
972 | + } |
|
973 | + |
|
974 | + |
|
975 | + /** |
|
976 | + * Adds extra buttons to the WP CPT permalink field row. |
|
977 | + * Method is called from parent and is hooked into the wp 'get_sample_permalink_html' filter. |
|
978 | + * |
|
979 | + * @param string $return the current html |
|
980 | + * @param int $id the post id for the page |
|
981 | + * @param string $new_title What the title is |
|
982 | + * @param string $new_slug what the slug is |
|
983 | + * @return string The new html string for the permalink area |
|
984 | + */ |
|
985 | + public function extra_permalink_field_buttons($return, $id, $new_title, $new_slug) |
|
986 | + { |
|
987 | + // make sure this is only when editing |
|
988 | + if (! empty($id)) { |
|
989 | + $post = get_post($id); |
|
990 | + $return .= '<a class="button button-small" onclick="prompt(\'Shortcode:\', jQuery(\'#shortcode\').val()); return false;" href="#" tabindex="-1">' |
|
991 | + . esc_html__('Shortcode', 'event_espresso') |
|
992 | + . '</a> '; |
|
993 | + $return .= '<input id="shortcode" type="hidden" value="[ESPRESSO_TICKET_SELECTOR event_id=' |
|
994 | + . $post->ID |
|
995 | + . ']">'; |
|
996 | + } |
|
997 | + return $return; |
|
998 | + } |
|
999 | + |
|
1000 | + |
|
1001 | + /** |
|
1002 | + * _events_overview_list_table |
|
1003 | + * This contains the logic for showing the events_overview list |
|
1004 | + * |
|
1005 | + * @return void |
|
1006 | + * @throws DomainException |
|
1007 | + * @throws EE_Error |
|
1008 | + * @throws InvalidArgumentException |
|
1009 | + * @throws InvalidDataTypeException |
|
1010 | + * @throws InvalidInterfaceException |
|
1011 | + */ |
|
1012 | + protected function _events_overview_list_table() |
|
1013 | + { |
|
1014 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
1015 | + $this->_template_args['after_list_table'] = |
|
1016 | + ! empty($this->_template_args['after_list_table']) |
|
1017 | + ? (array) $this->_template_args['after_list_table'] |
|
1018 | + : []; |
|
1019 | + $this->_template_args['after_list_table']['view_event_list_button'] = EEH_HTML::br() |
|
1020 | + . EEH_Template::get_button_or_link( |
|
1021 | + get_post_type_archive_link('espresso_events'), |
|
1022 | + esc_html__('View Event Archive Page', 'event_espresso'), |
|
1023 | + 'button' |
|
1024 | + ); |
|
1025 | + $this->_template_args['after_list_table']['legend'] = |
|
1026 | + $this->_display_legend($this->_event_legend_items()); |
|
1027 | + $this->_admin_page_title .= ' ' |
|
1028 | + . $this->get_action_link_or_button( |
|
1029 | + 'create_new', |
|
1030 | + 'add', |
|
1031 | + [], |
|
1032 | + 'add-new-h2' |
|
1033 | + ); |
|
1034 | + $this->display_admin_list_table_page_with_no_sidebar(); |
|
1035 | + } |
|
1036 | + |
|
1037 | + |
|
1038 | + /** |
|
1039 | + * this allows for extra misc actions in the default WP publish box |
|
1040 | + * |
|
1041 | + * @return void |
|
1042 | + * @throws DomainException |
|
1043 | + * @throws EE_Error |
|
1044 | + * @throws InvalidArgumentException |
|
1045 | + * @throws InvalidDataTypeException |
|
1046 | + * @throws InvalidInterfaceException |
|
1047 | + * @throws ReflectionException |
|
1048 | + */ |
|
1049 | + public function extra_misc_actions_publish_box() |
|
1050 | + { |
|
1051 | + $this->_generate_publish_box_extra_content(); |
|
1052 | + } |
|
1053 | + |
|
1054 | + |
|
1055 | + /** |
|
1056 | + * This is hooked into the WordPress do_action('save_post') hook and runs after the custom post type has been |
|
1057 | + * saved. |
|
1058 | + * Typically you would use this to save any additional data. |
|
1059 | + * Keep in mind also that "save_post" runs on EVERY post update to the database. |
|
1060 | + * ALSO very important. When a post transitions from scheduled to published, |
|
1061 | + * the save_post action is fired but you will NOT have any _POST data containing any extra info you may have from |
|
1062 | + * other meta saves. So MAKE sure that you handle this accordingly. |
|
1063 | + * |
|
1064 | + * @abstract |
|
1065 | + * @param string $post_id The ID of the cpt that was saved (so you can link relationally) |
|
1066 | + * @param object $post The post object of the cpt that was saved. |
|
1067 | + * @return void |
|
1068 | + * @throws EE_Error |
|
1069 | + * @throws InvalidArgumentException |
|
1070 | + * @throws InvalidDataTypeException |
|
1071 | + * @throws InvalidInterfaceException |
|
1072 | + * @throws ReflectionException |
|
1073 | + */ |
|
1074 | + protected function _insert_update_cpt_item($post_id, $post) |
|
1075 | + { |
|
1076 | + if ($post instanceof WP_Post && $post->post_type !== 'espresso_events') { |
|
1077 | + // get out we're not processing an event save. |
|
1078 | + return; |
|
1079 | + } |
|
1080 | + $event_values = [ |
|
1081 | + 'EVT_member_only' => ! empty($this->_req_data['member_only']) ? 1 : 0, |
|
1082 | + 'EVT_allow_overflow' => ! empty($this->_req_data['EVT_allow_overflow']) ? 1 : 0, |
|
1083 | + 'EVT_timezone_string' => ! empty($this->_req_data['timezone_string']) |
|
1084 | + ? sanitize_text_field($this->_req_data['timezone_string']) |
|
1085 | + : null, |
|
1086 | + ]; |
|
1087 | + /** @var FeatureFlags $flags */ |
|
1088 | + $flags = $this->loader->getShared('EventEspresso\core\domain\services\capabilities\FeatureFlags'); |
|
1089 | + // check if the new EDTR reg options meta box is being used, and if so, don't run updates for legacy version |
|
1090 | + if (! $this->admin_config->useAdvancedEditor() || ! $flags->featureAllowed('use_reg_options_meta_box')) { |
|
1091 | + $event_values['EVT_display_ticket_selector'] = |
|
1092 | + ! empty($this->_req_data['display_ticket_selector']) |
|
1093 | + ? 1 |
|
1094 | + : 0; |
|
1095 | + $event_values['EVT_additional_limit'] = min( |
|
1096 | + apply_filters('FHEE__EE_Events_Admin__insert_update_cpt_item__EVT_additional_limit_max', 255), |
|
1097 | + ! empty($this->_req_data['additional_limit']) |
|
1098 | + ? absint($this->_req_data['additional_limit']) |
|
1099 | + : null |
|
1100 | + ); |
|
1101 | + $event_values['EVT_default_registration_status'] = |
|
1102 | + ! empty($this->_req_data['EVT_default_registration_status']) |
|
1103 | + ? sanitize_text_field($this->_req_data['EVT_default_registration_status']) |
|
1104 | + : EE_Registry::instance()->CFG->registration->default_STS_ID; |
|
1105 | + $event_values['EVT_external_URL'] = ! empty($this->_req_data['externalURL']) |
|
1106 | + ? esc_url_raw($this->_req_data['externalURL']) |
|
1107 | + : null; |
|
1108 | + $event_values['EVT_phone'] = ! empty($this->_req_data['event_phone']) |
|
1109 | + ? sanitize_text_field($this->_req_data['event_phone']) |
|
1110 | + : null; |
|
1111 | + } |
|
1112 | + // update event |
|
1113 | + $success = $this->eventModel()->update_by_ID($event_values, $post_id); |
|
1114 | + // get event_object for other metaboxes... |
|
1115 | + // though it would seem to make sense to just use $this->eventModel()->get_one_by_ID( $post_id ).. |
|
1116 | + // i have to setup where conditions to override the filters in the model |
|
1117 | + // that filter out autodraft and inherit statuses so we GET the inherit id! |
|
1118 | + $get_one_where = [ |
|
1119 | + $this->eventModel()->primary_key_name() => $post_id, |
|
1120 | + 'OR' => [ |
|
1121 | + 'status' => $post->post_status, |
|
1122 | + // if trying to "Publish" a sold out event, it's status will get switched back to "sold_out" in the db, |
|
1123 | + // but the returned object here has a status of "publish", so use the original post status as well |
|
1124 | + 'status*1' => $this->_req_data['original_post_status'], |
|
1125 | + ], |
|
1126 | + ]; |
|
1127 | + $event = $this->eventModel()->get_one([$get_one_where]); |
|
1128 | + // the following are default callbacks for event attachment updates that can be overridden by caffeinated functionality and/or addons. |
|
1129 | + $event_update_callbacks = apply_filters( |
|
1130 | + 'FHEE__Events_Admin_Page___insert_update_cpt_item__event_update_callbacks', |
|
1131 | + [ |
|
1132 | + [$this, '_default_venue_update'], |
|
1133 | + [$this, '_default_tickets_update'], |
|
1134 | + ] |
|
1135 | + ); |
|
1136 | + $att_success = true; |
|
1137 | + foreach ($event_update_callbacks as $e_callback) { |
|
1138 | + $_success = is_callable($e_callback) |
|
1139 | + ? $e_callback($event, $this->_req_data) |
|
1140 | + : false; |
|
1141 | + // if ANY of these updates fail then we want the appropriate global error message |
|
1142 | + $att_success = ! $att_success ? $att_success : $_success; |
|
1143 | + } |
|
1144 | + // any errors? |
|
1145 | + if ($success && false === $att_success) { |
|
1146 | + EE_Error::add_error( |
|
1147 | + esc_html__( |
|
1148 | + 'Event Details saved successfully but something went wrong with saving attachments.', |
|
1149 | + 'event_espresso' |
|
1150 | + ), |
|
1151 | + __FILE__, |
|
1152 | + __FUNCTION__, |
|
1153 | + __LINE__ |
|
1154 | + ); |
|
1155 | + } elseif ($success === false) { |
|
1156 | + EE_Error::add_error( |
|
1157 | + esc_html__('Event Details did not save successfully.', 'event_espresso'), |
|
1158 | + __FILE__, |
|
1159 | + __FUNCTION__, |
|
1160 | + __LINE__ |
|
1161 | + ); |
|
1162 | + } |
|
1163 | + } |
|
1164 | + |
|
1165 | + |
|
1166 | + /** |
|
1167 | + * @param int $post_id |
|
1168 | + * @param int $revision_id |
|
1169 | + * @throws EE_Error |
|
1170 | + * @throws InvalidArgumentException |
|
1171 | + * @throws InvalidDataTypeException |
|
1172 | + * @throws InvalidInterfaceException |
|
1173 | + * @throws ReflectionException |
|
1174 | + * @see parent::restore_item() |
|
1175 | + */ |
|
1176 | + protected function _restore_cpt_item($post_id, $revision_id) |
|
1177 | + { |
|
1178 | + // copy existing event meta to new post |
|
1179 | + $post_evt = $this->eventModel()->get_one_by_ID($post_id); |
|
1180 | + if ($post_evt instanceof EE_Event) { |
|
1181 | + // meta revision restore |
|
1182 | + $post_evt->restore_revision($revision_id); |
|
1183 | + // related objs restore |
|
1184 | + $post_evt->restore_revision($revision_id, ['Venue', 'Datetime', 'Price']); |
|
1185 | + } |
|
1186 | + } |
|
1187 | + |
|
1188 | + |
|
1189 | + /** |
|
1190 | + * Attach the venue to the Event |
|
1191 | + * |
|
1192 | + * @param EE_Event $evtobj Event Object to add the venue to |
|
1193 | + * @param array $data The request data from the form |
|
1194 | + * @return bool Success or fail. |
|
1195 | + * @throws EE_Error |
|
1196 | + * @throws InvalidArgumentException |
|
1197 | + * @throws InvalidDataTypeException |
|
1198 | + * @throws InvalidInterfaceException |
|
1199 | + * @throws ReflectionException |
|
1200 | + */ |
|
1201 | + protected function _default_venue_update(EE_Event $evtobj, $data) |
|
1202 | + { |
|
1203 | + require_once(EE_MODELS . 'EEM_Venue.model.php'); |
|
1204 | + $venue_model = EEM_Venue::instance(); |
|
1205 | + $rows_affected = null; |
|
1206 | + $venue_id = ! empty($data['venue_id']) ? $data['venue_id'] : null; |
|
1207 | + // very important. If we don't have a venue name... |
|
1208 | + // then we'll get out because not necessary to create empty venue |
|
1209 | + if (empty($data['venue_title'])) { |
|
1210 | + return false; |
|
1211 | + } |
|
1212 | + $venue_array = [ |
|
1213 | + 'VNU_wp_user' => $evtobj->get('EVT_wp_user'), |
|
1214 | + 'VNU_name' => ! empty($data['venue_title']) ? $data['venue_title'] : null, |
|
1215 | + 'VNU_desc' => ! empty($data['venue_description']) ? $data['venue_description'] : null, |
|
1216 | + 'VNU_identifier' => ! empty($data['venue_identifier']) ? $data['venue_identifier'] : null, |
|
1217 | + 'VNU_short_desc' => ! empty($data['venue_short_description']) ? $data['venue_short_description'] |
|
1218 | + : null, |
|
1219 | + 'VNU_address' => ! empty($data['address']) ? $data['address'] : null, |
|
1220 | + 'VNU_address2' => ! empty($data['address2']) ? $data['address2'] : null, |
|
1221 | + 'VNU_city' => ! empty($data['city']) ? $data['city'] : null, |
|
1222 | + 'STA_ID' => ! empty($data['state']) ? $data['state'] : null, |
|
1223 | + 'CNT_ISO' => ! empty($data['countries']) ? $data['countries'] : null, |
|
1224 | + 'VNU_zip' => ! empty($data['zip']) ? $data['zip'] : null, |
|
1225 | + 'VNU_phone' => ! empty($data['venue_phone']) ? $data['venue_phone'] : null, |
|
1226 | + 'VNU_capacity' => ! empty($data['venue_capacity']) ? $data['venue_capacity'] : null, |
|
1227 | + 'VNU_url' => ! empty($data['venue_url']) ? $data['venue_url'] : null, |
|
1228 | + 'VNU_virtual_phone' => ! empty($data['virtual_phone']) ? $data['virtual_phone'] : null, |
|
1229 | + 'VNU_virtual_url' => ! empty($data['virtual_url']) ? $data['virtual_url'] : null, |
|
1230 | + 'VNU_enable_for_gmap' => isset($data['enable_for_gmap']) ? 1 : 0, |
|
1231 | + 'status' => 'publish', |
|
1232 | + ]; |
|
1233 | + // if we've got the venue_id then we're just updating the existing venue so let's do that and then get out. |
|
1234 | + if (! empty($venue_id)) { |
|
1235 | + $update_where = [$venue_model->primary_key_name() => $venue_id]; |
|
1236 | + $rows_affected = $venue_model->update($venue_array, [$update_where]); |
|
1237 | + // we've gotta make sure that the venue is always attached to a revision.. |
|
1238 | + // add_relation_to should take care of making sure that the relation is already present. |
|
1239 | + $evtobj->_add_relation_to($venue_id, 'Venue'); |
|
1240 | + return $rows_affected > 0; |
|
1241 | + } |
|
1242 | + // we insert the venue |
|
1243 | + $venue_id = $venue_model->insert($venue_array); |
|
1244 | + $evtobj->_add_relation_to($venue_id, 'Venue'); |
|
1245 | + return ! empty($venue_id); |
|
1246 | + // when we have the ancestor come in it's already been handled by the revision save. |
|
1247 | + } |
|
1248 | + |
|
1249 | + |
|
1250 | + /** |
|
1251 | + * Handles saving everything related to Tickets (datetimes, tickets, prices) |
|
1252 | + * |
|
1253 | + * @param EE_Event $evtobj The Event object we're attaching data to |
|
1254 | + * @param array $data The request data from the form |
|
1255 | + * @return array |
|
1256 | + * @throws EE_Error |
|
1257 | + * @throws InvalidArgumentException |
|
1258 | + * @throws InvalidDataTypeException |
|
1259 | + * @throws InvalidInterfaceException |
|
1260 | + * @throws ReflectionException |
|
1261 | + * @throws Exception |
|
1262 | + */ |
|
1263 | + protected function _default_tickets_update(EE_Event $evtobj, $data) |
|
1264 | + { |
|
1265 | + if ($this->admin_config->useAdvancedEditor()) { |
|
1266 | + return []; |
|
1267 | + } |
|
1268 | + $saved_dtt = null; |
|
1269 | + $saved_tickets = []; |
|
1270 | + $incoming_date_formats = ['Y-m-d', 'h:i a']; |
|
1271 | + $event_timezone_string = $evtobj->get_timezone(); |
|
1272 | + $event_timezone = new DateTimeZone($event_timezone_string); |
|
1273 | + // let's use now in the set timezone. |
|
1274 | + $now = new DateTime('now', $event_timezone); |
|
1275 | + foreach ($data['edit_event_datetimes'] as $row => $dtt) { |
|
1276 | + // trim all values to ensure any excess whitespace is removed. |
|
1277 | + $dtt = array_map('trim', $dtt); |
|
1278 | + $dtt['DTT_EVT_end'] = isset($dtt['DTT_EVT_end']) && ! empty($dtt['DTT_EVT_end']) |
|
1279 | 1279 | ? $dtt['DTT_EVT_end'] |
1280 | - : $dtt['DTT_EVT_start']; |
|
1281 | - $datetime_values = [ |
|
1282 | - 'DTT_ID' => ! empty($dtt['DTT_ID']) ? $dtt['DTT_ID'] : null, |
|
1283 | - 'DTT_EVT_start' => $dtt['DTT_EVT_start'], |
|
1284 | - 'DTT_EVT_end' => $dtt['DTT_EVT_end'], |
|
1285 | - 'DTT_reg_limit' => empty($dtt['DTT_reg_limit']) ? EE_INF : $dtt['DTT_reg_limit'], |
|
1286 | - 'DTT_order' => $row, |
|
1287 | - ]; |
|
1288 | - // if we have an id then let's get existing object first and then set the new values. |
|
1289 | - // Otherwise we instantiate a new object for save. |
|
1290 | - if (! empty($dtt['DTT_ID'])) { |
|
1291 | - $DTM = $this->datetimeModel($event_timezone_string)->get_one_by_ID($dtt['DTT_ID']); |
|
1292 | - $DTM->set_date_format($incoming_date_formats[0]); |
|
1293 | - $DTM->set_time_format($incoming_date_formats[1]); |
|
1294 | - foreach ($datetime_values as $field => $value) { |
|
1295 | - $DTM->set($field, $value); |
|
1296 | - } |
|
1297 | - // make sure the $dtt_id here is saved in case after the add_relation_to() the autosave replaces it. |
|
1298 | - // We need to do this so we dont' TRASH the parent DTT. |
|
1299 | - $saved_dtts[ $DTM->ID() ] = $DTM; |
|
1300 | - } else { |
|
1301 | - $DTM = EE_Registry::instance()->load_class( |
|
1302 | - 'Datetime', |
|
1303 | - [$datetime_values, $event_timezone_string, $incoming_date_formats], |
|
1304 | - false, |
|
1305 | - false |
|
1306 | - ); |
|
1307 | - foreach ($datetime_values as $field => $value) { |
|
1308 | - $DTM->set($field, $value); |
|
1309 | - } |
|
1310 | - } |
|
1311 | - $DTM->save(); |
|
1312 | - $DTM = $evtobj->_add_relation_to($DTM, 'Datetime'); |
|
1313 | - // before going any further make sure our dates are setup correctly |
|
1280 | + : $dtt['DTT_EVT_start']; |
|
1281 | + $datetime_values = [ |
|
1282 | + 'DTT_ID' => ! empty($dtt['DTT_ID']) ? $dtt['DTT_ID'] : null, |
|
1283 | + 'DTT_EVT_start' => $dtt['DTT_EVT_start'], |
|
1284 | + 'DTT_EVT_end' => $dtt['DTT_EVT_end'], |
|
1285 | + 'DTT_reg_limit' => empty($dtt['DTT_reg_limit']) ? EE_INF : $dtt['DTT_reg_limit'], |
|
1286 | + 'DTT_order' => $row, |
|
1287 | + ]; |
|
1288 | + // if we have an id then let's get existing object first and then set the new values. |
|
1289 | + // Otherwise we instantiate a new object for save. |
|
1290 | + if (! empty($dtt['DTT_ID'])) { |
|
1291 | + $DTM = $this->datetimeModel($event_timezone_string)->get_one_by_ID($dtt['DTT_ID']); |
|
1292 | + $DTM->set_date_format($incoming_date_formats[0]); |
|
1293 | + $DTM->set_time_format($incoming_date_formats[1]); |
|
1294 | + foreach ($datetime_values as $field => $value) { |
|
1295 | + $DTM->set($field, $value); |
|
1296 | + } |
|
1297 | + // make sure the $dtt_id here is saved in case after the add_relation_to() the autosave replaces it. |
|
1298 | + // We need to do this so we dont' TRASH the parent DTT. |
|
1299 | + $saved_dtts[ $DTM->ID() ] = $DTM; |
|
1300 | + } else { |
|
1301 | + $DTM = EE_Registry::instance()->load_class( |
|
1302 | + 'Datetime', |
|
1303 | + [$datetime_values, $event_timezone_string, $incoming_date_formats], |
|
1304 | + false, |
|
1305 | + false |
|
1306 | + ); |
|
1307 | + foreach ($datetime_values as $field => $value) { |
|
1308 | + $DTM->set($field, $value); |
|
1309 | + } |
|
1310 | + } |
|
1311 | + $DTM->save(); |
|
1312 | + $DTM = $evtobj->_add_relation_to($DTM, 'Datetime'); |
|
1313 | + // before going any further make sure our dates are setup correctly |
|
1314 | 1314 | // so that the end date is always equal or greater than the start date. |
1315 | - if ($DTM->get_raw('DTT_EVT_start') > $DTM->get_raw('DTT_EVT_end')) { |
|
1316 | - $DTM->set('DTT_EVT_end', $DTM->get('DTT_EVT_start')); |
|
1317 | - $DTM = EEH_DTT_Helper::date_time_add($DTM, 'DTT_EVT_end', 'days'); |
|
1318 | - $DTM->save(); |
|
1319 | - } |
|
1320 | - // now we got to make sure we add the new DTT_ID to the $saved_dtts array |
|
1321 | - // because it is possible there was a new one created for the autosave. |
|
1322 | - $saved_dtt = $DTM; |
|
1323 | - // if ANY of these updates fail then we want the appropriate global error message. |
|
1324 | - // //todo this is actually sucky we need a better error message but this is what it is for now. |
|
1325 | - } |
|
1326 | - // no dtts get deleted so we don't do any of that logic here. |
|
1327 | - // update tickets next |
|
1328 | - $old_tickets = isset($data['ticket_IDs']) ? explode(',', $data['ticket_IDs']) : []; |
|
1329 | - foreach ($data['edit_tickets'] as $row => $tkt) { |
|
1330 | - $incoming_date_formats = ['Y-m-d', 'h:i a']; |
|
1331 | - $update_prices = false; |
|
1332 | - $ticket_price = isset($data['edit_prices'][ $row ][1]['PRC_amount']) |
|
1333 | - ? $data['edit_prices'][ $row ][1]['PRC_amount'] |
|
1334 | - : 0; |
|
1335 | - // trim inputs to ensure any excess whitespace is removed. |
|
1336 | - $tkt = array_map('trim', $tkt); |
|
1337 | - if (empty($tkt['TKT_start_date'])) { |
|
1338 | - $tkt['TKT_start_date'] = $now->format($incoming_date_formats[0] . ' ' . $incoming_date_formats[1]); |
|
1339 | - } |
|
1340 | - if (empty($tkt['TKT_end_date'])) { |
|
1341 | - // use the start date of the first datetime |
|
1342 | - $dtt = $evtobj->first_datetime(); |
|
1343 | - $tkt['TKT_end_date'] = $dtt->start_date_and_time( |
|
1344 | - $incoming_date_formats[0], |
|
1345 | - $incoming_date_formats[1] |
|
1346 | - ); |
|
1347 | - } |
|
1348 | - $TKT_values = [ |
|
1349 | - 'TKT_ID' => ! empty($tkt['TKT_ID']) ? $tkt['TKT_ID'] : null, |
|
1350 | - 'TTM_ID' => ! empty($tkt['TTM_ID']) ? $tkt['TTM_ID'] : 0, |
|
1351 | - 'TKT_name' => ! empty($tkt['TKT_name']) ? $tkt['TKT_name'] : '', |
|
1352 | - 'TKT_description' => ! empty($tkt['TKT_description']) ? $tkt['TKT_description'] : '', |
|
1353 | - 'TKT_start_date' => $tkt['TKT_start_date'], |
|
1354 | - 'TKT_end_date' => $tkt['TKT_end_date'], |
|
1355 | - 'TKT_qty' => ! isset($tkt['TKT_qty']) || $tkt['TKT_qty'] === '' ? EE_INF : $tkt['TKT_qty'], |
|
1356 | - 'TKT_uses' => ! isset($tkt['TKT_uses']) || $tkt['TKT_uses'] === '' ? EE_INF : $tkt['TKT_uses'], |
|
1357 | - 'TKT_min' => empty($tkt['TKT_min']) ? 0 : $tkt['TKT_min'], |
|
1358 | - 'TKT_max' => empty($tkt['TKT_max']) ? EE_INF : $tkt['TKT_max'], |
|
1359 | - 'TKT_row' => $row, |
|
1360 | - 'TKT_order' => isset($tkt['TKT_order']) ? $tkt['TKT_order'] : $row, |
|
1361 | - 'TKT_price' => $ticket_price, |
|
1362 | - ]; |
|
1363 | - // if this is a default TKT, then we need to set the TKT_ID to 0 and update accordingly, |
|
1364 | - // which means in turn that the prices will become new prices as well. |
|
1365 | - if (isset($tkt['TKT_is_default']) && $tkt['TKT_is_default']) { |
|
1366 | - $TKT_values['TKT_ID'] = 0; |
|
1367 | - $TKT_values['TKT_is_default'] = 0; |
|
1368 | - $TKT_values['TKT_price'] = $ticket_price; |
|
1369 | - $update_prices = true; |
|
1370 | - } |
|
1371 | - // if we have a TKT_ID then we need to get that existing TKT_obj and update it |
|
1372 | - // we actually do our saves a head of doing any add_relations to because its entirely possible |
|
1373 | - // that this ticket didn't removed or added to any datetime in the session but DID have it's items modified. |
|
1374 | - // keep in mind that if the TKT has been sold (and we have changed pricing information), |
|
1375 | - // then we won't be updating the tkt but instead a new tkt will be created and the old one archived. |
|
1376 | - if (! empty($tkt['TKT_ID'])) { |
|
1377 | - $TKT = $this->ticketModel($event_timezone_string)->get_one_by_ID($tkt['TKT_ID']); |
|
1378 | - if ($TKT instanceof EE_Ticket) { |
|
1379 | - $ticket_sold = $TKT->count_related( |
|
1380 | - 'Registration', |
|
1381 | - [ |
|
1382 | - [ |
|
1383 | - 'STS_ID' => [ |
|
1384 | - 'NOT IN', |
|
1385 | - [EEM_Registration::status_id_incomplete], |
|
1386 | - ], |
|
1387 | - ], |
|
1388 | - ] |
|
1389 | - ) > 0; |
|
1390 | - // let's just check the total price for the existing ticket and determine if it matches the new |
|
1391 | - // total price. if they are different then we create a new ticket (if tickets sold) |
|
1392 | - // if they aren't different then we go ahead and modify existing ticket. |
|
1393 | - $create_new_TKT = $ticket_sold |
|
1394 | - && ! $TKT->deleted() |
|
1395 | - && EEH_Money::compare_floats( |
|
1396 | - $ticket_price, |
|
1397 | - $TKT->get('TKT_price'), |
|
1398 | - '!==' |
|
1399 | - ); |
|
1400 | - $TKT->set_date_format($incoming_date_formats[0]); |
|
1401 | - $TKT->set_time_format($incoming_date_formats[1]); |
|
1402 | - // set new values |
|
1403 | - foreach ($TKT_values as $field => $value) { |
|
1404 | - if ($field === 'TKT_qty') { |
|
1405 | - $TKT->set_qty($value); |
|
1406 | - } else { |
|
1407 | - $TKT->set($field, $value); |
|
1408 | - } |
|
1409 | - } |
|
1410 | - // if $create_new_TKT is false then we can safely update the existing ticket. |
|
1411 | - // Otherwise we have to create a new ticket. |
|
1412 | - if ($create_new_TKT) { |
|
1413 | - // archive the old ticket first |
|
1414 | - $TKT->set('TKT_deleted', 1); |
|
1415 | - $TKT->save(); |
|
1416 | - // make sure this ticket is still recorded in our saved_tkts |
|
1417 | - // so we don't run it through the regular trash routine. |
|
1418 | - $saved_tickets[ $TKT->ID() ] = $TKT; |
|
1419 | - // create new ticket that's a copy of the existing except a new id of course |
|
1420 | - // (and not archived) AND has the new TKT_price associated with it. |
|
1421 | - $TKT = clone $TKT; |
|
1422 | - $TKT->set('TKT_ID', 0); |
|
1423 | - $TKT->set('TKT_deleted', 0); |
|
1424 | - $TKT->set('TKT_price', $ticket_price); |
|
1425 | - $TKT->set('TKT_sold', 0); |
|
1426 | - // now we need to make sure that $new prices are created as well and attached to new ticket. |
|
1427 | - $update_prices = true; |
|
1428 | - } |
|
1429 | - // make sure price is set if it hasn't been already |
|
1430 | - $TKT->set('TKT_price', $ticket_price); |
|
1431 | - } |
|
1432 | - } else { |
|
1433 | - // no TKT_id so a new TKT |
|
1434 | - $TKT_values['TKT_price'] = $ticket_price; |
|
1435 | - $TKT = EE_Registry::instance()->load_class('Ticket', [$TKT_values], false, false); |
|
1436 | - if ($TKT instanceof EE_Ticket) { |
|
1437 | - // need to reset values to properly account for the date formats |
|
1438 | - $TKT->set_date_format($incoming_date_formats[0]); |
|
1439 | - $TKT->set_time_format($incoming_date_formats[1]); |
|
1440 | - $TKT->set_timezone($evtobj->get_timezone()); |
|
1441 | - // set new values |
|
1442 | - foreach ($TKT_values as $field => $value) { |
|
1443 | - if ($field === 'TKT_qty') { |
|
1444 | - $TKT->set_qty($value); |
|
1445 | - } else { |
|
1446 | - $TKT->set($field, $value); |
|
1447 | - } |
|
1448 | - } |
|
1449 | - $update_prices = true; |
|
1450 | - } |
|
1451 | - } |
|
1452 | - // cap ticket qty by datetime reg limits |
|
1453 | - $TKT->set_qty(min($TKT->qty(), $TKT->qty('reg_limit'))); |
|
1454 | - // update ticket. |
|
1455 | - $TKT->save(); |
|
1456 | - // before going any further make sure our dates are setup correctly |
|
1457 | - // so that the end date is always equal or greater than the start date. |
|
1458 | - if ($TKT->get_raw('TKT_start_date') > $TKT->get_raw('TKT_end_date')) { |
|
1459 | - $TKT->set('TKT_end_date', $TKT->get('TKT_start_date')); |
|
1460 | - $TKT = EEH_DTT_Helper::date_time_add($TKT, 'TKT_end_date', 'days'); |
|
1461 | - $TKT->save(); |
|
1462 | - } |
|
1463 | - // initially let's add the ticket to the dtt |
|
1464 | - $saved_dtt->_add_relation_to($TKT, 'Ticket'); |
|
1465 | - $saved_tickets[ $TKT->ID() ] = $TKT; |
|
1466 | - // add prices to ticket |
|
1467 | - $this->_add_prices_to_ticket($data['edit_prices'][ $row ], $TKT, $update_prices); |
|
1468 | - } |
|
1469 | - // however now we need to handle permanently deleting tickets via the ui. |
|
1470 | - // Keep in mind that the ui does not allow deleting/archiving tickets that have ticket sold. |
|
1471 | - // However, it does allow for deleting tickets that have no tickets sold, |
|
1472 | - // in which case we want to get rid of permanently because there is no need to save in db. |
|
1473 | - $old_tickets = isset($old_tickets[0]) && $old_tickets[0] === '' ? [] : $old_tickets; |
|
1474 | - $tickets_removed = array_diff($old_tickets, array_keys($saved_tickets)); |
|
1475 | - foreach ($tickets_removed as $id) { |
|
1476 | - $id = absint($id); |
|
1477 | - // get the ticket for this id |
|
1478 | - $tkt_to_remove = $this->ticketModel($event_timezone_string)->get_one_by_ID($id); |
|
1479 | - // need to get all the related datetimes on this ticket and remove from every single one of them |
|
1315 | + if ($DTM->get_raw('DTT_EVT_start') > $DTM->get_raw('DTT_EVT_end')) { |
|
1316 | + $DTM->set('DTT_EVT_end', $DTM->get('DTT_EVT_start')); |
|
1317 | + $DTM = EEH_DTT_Helper::date_time_add($DTM, 'DTT_EVT_end', 'days'); |
|
1318 | + $DTM->save(); |
|
1319 | + } |
|
1320 | + // now we got to make sure we add the new DTT_ID to the $saved_dtts array |
|
1321 | + // because it is possible there was a new one created for the autosave. |
|
1322 | + $saved_dtt = $DTM; |
|
1323 | + // if ANY of these updates fail then we want the appropriate global error message. |
|
1324 | + // //todo this is actually sucky we need a better error message but this is what it is for now. |
|
1325 | + } |
|
1326 | + // no dtts get deleted so we don't do any of that logic here. |
|
1327 | + // update tickets next |
|
1328 | + $old_tickets = isset($data['ticket_IDs']) ? explode(',', $data['ticket_IDs']) : []; |
|
1329 | + foreach ($data['edit_tickets'] as $row => $tkt) { |
|
1330 | + $incoming_date_formats = ['Y-m-d', 'h:i a']; |
|
1331 | + $update_prices = false; |
|
1332 | + $ticket_price = isset($data['edit_prices'][ $row ][1]['PRC_amount']) |
|
1333 | + ? $data['edit_prices'][ $row ][1]['PRC_amount'] |
|
1334 | + : 0; |
|
1335 | + // trim inputs to ensure any excess whitespace is removed. |
|
1336 | + $tkt = array_map('trim', $tkt); |
|
1337 | + if (empty($tkt['TKT_start_date'])) { |
|
1338 | + $tkt['TKT_start_date'] = $now->format($incoming_date_formats[0] . ' ' . $incoming_date_formats[1]); |
|
1339 | + } |
|
1340 | + if (empty($tkt['TKT_end_date'])) { |
|
1341 | + // use the start date of the first datetime |
|
1342 | + $dtt = $evtobj->first_datetime(); |
|
1343 | + $tkt['TKT_end_date'] = $dtt->start_date_and_time( |
|
1344 | + $incoming_date_formats[0], |
|
1345 | + $incoming_date_formats[1] |
|
1346 | + ); |
|
1347 | + } |
|
1348 | + $TKT_values = [ |
|
1349 | + 'TKT_ID' => ! empty($tkt['TKT_ID']) ? $tkt['TKT_ID'] : null, |
|
1350 | + 'TTM_ID' => ! empty($tkt['TTM_ID']) ? $tkt['TTM_ID'] : 0, |
|
1351 | + 'TKT_name' => ! empty($tkt['TKT_name']) ? $tkt['TKT_name'] : '', |
|
1352 | + 'TKT_description' => ! empty($tkt['TKT_description']) ? $tkt['TKT_description'] : '', |
|
1353 | + 'TKT_start_date' => $tkt['TKT_start_date'], |
|
1354 | + 'TKT_end_date' => $tkt['TKT_end_date'], |
|
1355 | + 'TKT_qty' => ! isset($tkt['TKT_qty']) || $tkt['TKT_qty'] === '' ? EE_INF : $tkt['TKT_qty'], |
|
1356 | + 'TKT_uses' => ! isset($tkt['TKT_uses']) || $tkt['TKT_uses'] === '' ? EE_INF : $tkt['TKT_uses'], |
|
1357 | + 'TKT_min' => empty($tkt['TKT_min']) ? 0 : $tkt['TKT_min'], |
|
1358 | + 'TKT_max' => empty($tkt['TKT_max']) ? EE_INF : $tkt['TKT_max'], |
|
1359 | + 'TKT_row' => $row, |
|
1360 | + 'TKT_order' => isset($tkt['TKT_order']) ? $tkt['TKT_order'] : $row, |
|
1361 | + 'TKT_price' => $ticket_price, |
|
1362 | + ]; |
|
1363 | + // if this is a default TKT, then we need to set the TKT_ID to 0 and update accordingly, |
|
1364 | + // which means in turn that the prices will become new prices as well. |
|
1365 | + if (isset($tkt['TKT_is_default']) && $tkt['TKT_is_default']) { |
|
1366 | + $TKT_values['TKT_ID'] = 0; |
|
1367 | + $TKT_values['TKT_is_default'] = 0; |
|
1368 | + $TKT_values['TKT_price'] = $ticket_price; |
|
1369 | + $update_prices = true; |
|
1370 | + } |
|
1371 | + // if we have a TKT_ID then we need to get that existing TKT_obj and update it |
|
1372 | + // we actually do our saves a head of doing any add_relations to because its entirely possible |
|
1373 | + // that this ticket didn't removed or added to any datetime in the session but DID have it's items modified. |
|
1374 | + // keep in mind that if the TKT has been sold (and we have changed pricing information), |
|
1375 | + // then we won't be updating the tkt but instead a new tkt will be created and the old one archived. |
|
1376 | + if (! empty($tkt['TKT_ID'])) { |
|
1377 | + $TKT = $this->ticketModel($event_timezone_string)->get_one_by_ID($tkt['TKT_ID']); |
|
1378 | + if ($TKT instanceof EE_Ticket) { |
|
1379 | + $ticket_sold = $TKT->count_related( |
|
1380 | + 'Registration', |
|
1381 | + [ |
|
1382 | + [ |
|
1383 | + 'STS_ID' => [ |
|
1384 | + 'NOT IN', |
|
1385 | + [EEM_Registration::status_id_incomplete], |
|
1386 | + ], |
|
1387 | + ], |
|
1388 | + ] |
|
1389 | + ) > 0; |
|
1390 | + // let's just check the total price for the existing ticket and determine if it matches the new |
|
1391 | + // total price. if they are different then we create a new ticket (if tickets sold) |
|
1392 | + // if they aren't different then we go ahead and modify existing ticket. |
|
1393 | + $create_new_TKT = $ticket_sold |
|
1394 | + && ! $TKT->deleted() |
|
1395 | + && EEH_Money::compare_floats( |
|
1396 | + $ticket_price, |
|
1397 | + $TKT->get('TKT_price'), |
|
1398 | + '!==' |
|
1399 | + ); |
|
1400 | + $TKT->set_date_format($incoming_date_formats[0]); |
|
1401 | + $TKT->set_time_format($incoming_date_formats[1]); |
|
1402 | + // set new values |
|
1403 | + foreach ($TKT_values as $field => $value) { |
|
1404 | + if ($field === 'TKT_qty') { |
|
1405 | + $TKT->set_qty($value); |
|
1406 | + } else { |
|
1407 | + $TKT->set($field, $value); |
|
1408 | + } |
|
1409 | + } |
|
1410 | + // if $create_new_TKT is false then we can safely update the existing ticket. |
|
1411 | + // Otherwise we have to create a new ticket. |
|
1412 | + if ($create_new_TKT) { |
|
1413 | + // archive the old ticket first |
|
1414 | + $TKT->set('TKT_deleted', 1); |
|
1415 | + $TKT->save(); |
|
1416 | + // make sure this ticket is still recorded in our saved_tkts |
|
1417 | + // so we don't run it through the regular trash routine. |
|
1418 | + $saved_tickets[ $TKT->ID() ] = $TKT; |
|
1419 | + // create new ticket that's a copy of the existing except a new id of course |
|
1420 | + // (and not archived) AND has the new TKT_price associated with it. |
|
1421 | + $TKT = clone $TKT; |
|
1422 | + $TKT->set('TKT_ID', 0); |
|
1423 | + $TKT->set('TKT_deleted', 0); |
|
1424 | + $TKT->set('TKT_price', $ticket_price); |
|
1425 | + $TKT->set('TKT_sold', 0); |
|
1426 | + // now we need to make sure that $new prices are created as well and attached to new ticket. |
|
1427 | + $update_prices = true; |
|
1428 | + } |
|
1429 | + // make sure price is set if it hasn't been already |
|
1430 | + $TKT->set('TKT_price', $ticket_price); |
|
1431 | + } |
|
1432 | + } else { |
|
1433 | + // no TKT_id so a new TKT |
|
1434 | + $TKT_values['TKT_price'] = $ticket_price; |
|
1435 | + $TKT = EE_Registry::instance()->load_class('Ticket', [$TKT_values], false, false); |
|
1436 | + if ($TKT instanceof EE_Ticket) { |
|
1437 | + // need to reset values to properly account for the date formats |
|
1438 | + $TKT->set_date_format($incoming_date_formats[0]); |
|
1439 | + $TKT->set_time_format($incoming_date_formats[1]); |
|
1440 | + $TKT->set_timezone($evtobj->get_timezone()); |
|
1441 | + // set new values |
|
1442 | + foreach ($TKT_values as $field => $value) { |
|
1443 | + if ($field === 'TKT_qty') { |
|
1444 | + $TKT->set_qty($value); |
|
1445 | + } else { |
|
1446 | + $TKT->set($field, $value); |
|
1447 | + } |
|
1448 | + } |
|
1449 | + $update_prices = true; |
|
1450 | + } |
|
1451 | + } |
|
1452 | + // cap ticket qty by datetime reg limits |
|
1453 | + $TKT->set_qty(min($TKT->qty(), $TKT->qty('reg_limit'))); |
|
1454 | + // update ticket. |
|
1455 | + $TKT->save(); |
|
1456 | + // before going any further make sure our dates are setup correctly |
|
1457 | + // so that the end date is always equal or greater than the start date. |
|
1458 | + if ($TKT->get_raw('TKT_start_date') > $TKT->get_raw('TKT_end_date')) { |
|
1459 | + $TKT->set('TKT_end_date', $TKT->get('TKT_start_date')); |
|
1460 | + $TKT = EEH_DTT_Helper::date_time_add($TKT, 'TKT_end_date', 'days'); |
|
1461 | + $TKT->save(); |
|
1462 | + } |
|
1463 | + // initially let's add the ticket to the dtt |
|
1464 | + $saved_dtt->_add_relation_to($TKT, 'Ticket'); |
|
1465 | + $saved_tickets[ $TKT->ID() ] = $TKT; |
|
1466 | + // add prices to ticket |
|
1467 | + $this->_add_prices_to_ticket($data['edit_prices'][ $row ], $TKT, $update_prices); |
|
1468 | + } |
|
1469 | + // however now we need to handle permanently deleting tickets via the ui. |
|
1470 | + // Keep in mind that the ui does not allow deleting/archiving tickets that have ticket sold. |
|
1471 | + // However, it does allow for deleting tickets that have no tickets sold, |
|
1472 | + // in which case we want to get rid of permanently because there is no need to save in db. |
|
1473 | + $old_tickets = isset($old_tickets[0]) && $old_tickets[0] === '' ? [] : $old_tickets; |
|
1474 | + $tickets_removed = array_diff($old_tickets, array_keys($saved_tickets)); |
|
1475 | + foreach ($tickets_removed as $id) { |
|
1476 | + $id = absint($id); |
|
1477 | + // get the ticket for this id |
|
1478 | + $tkt_to_remove = $this->ticketModel($event_timezone_string)->get_one_by_ID($id); |
|
1479 | + // need to get all the related datetimes on this ticket and remove from every single one of them |
|
1480 | 1480 | // (remember this process can ONLY kick off if there are NO tkts_sold) |
1481 | - $dtts = $tkt_to_remove->get_many_related('Datetime'); |
|
1482 | - foreach ($dtts as $dtt) { |
|
1483 | - $tkt_to_remove->_remove_relation_to($dtt, 'Datetime'); |
|
1484 | - } |
|
1485 | - // need to do the same for prices (except these prices can also be deleted because again, |
|
1486 | - // tickets can only be trashed if they don't have any TKTs sold (otherwise they are just archived)) |
|
1487 | - $tkt_to_remove->delete_related_permanently('Price'); |
|
1488 | - // finally let's delete this ticket |
|
1489 | - // (which should not be blocked at this point b/c we've removed all our relationships) |
|
1490 | - $tkt_to_remove->delete_permanently(); |
|
1491 | - } |
|
1492 | - return [$saved_dtt, $saved_tickets]; |
|
1493 | - } |
|
1494 | - |
|
1495 | - |
|
1496 | - /** |
|
1497 | - * This attaches a list of given prices to a ticket. |
|
1498 | - * Note we dont' have to worry about ever removing relationships (or archiving prices) |
|
1499 | - * because if there is a change in price information on a ticket, a new ticket is created anyways |
|
1500 | - * so the archived ticket will retain the old price info and prices are automatically "archived" via the ticket. |
|
1501 | - * |
|
1502 | - * @param array $prices Array of prices from the form. |
|
1503 | - * @param EE_Ticket $ticket EE_Ticket object that prices are being attached to. |
|
1504 | - * @param bool $new_prices Whether attach existing incoming prices or create new ones. |
|
1505 | - * @return void |
|
1506 | - * @throws EE_Error |
|
1507 | - * @throws InvalidArgumentException |
|
1508 | - * @throws InvalidDataTypeException |
|
1509 | - * @throws InvalidInterfaceException |
|
1510 | - * @throws ReflectionException |
|
1511 | - */ |
|
1512 | - private function _add_prices_to_ticket($prices, EE_Ticket $ticket, $new_prices = false) |
|
1513 | - { |
|
1514 | - foreach ($prices as $row => $prc) { |
|
1515 | - $PRC_values = [ |
|
1516 | - 'PRC_ID' => ! empty($prc['PRC_ID']) ? $prc['PRC_ID'] : null, |
|
1517 | - 'PRT_ID' => ! empty($prc['PRT_ID']) ? $prc['PRT_ID'] : null, |
|
1518 | - 'PRC_amount' => ! empty($prc['PRC_amount']) ? $prc['PRC_amount'] : 0, |
|
1519 | - 'PRC_name' => ! empty($prc['PRC_name']) ? $prc['PRC_name'] : '', |
|
1520 | - 'PRC_desc' => ! empty($prc['PRC_desc']) ? $prc['PRC_desc'] : '', |
|
1521 | - 'PRC_is_default' => 0, // make sure prices are NOT set as default from this context |
|
1522 | - 'PRC_order' => $row, |
|
1523 | - ]; |
|
1524 | - if ($new_prices || empty($PRC_values['PRC_ID'])) { |
|
1525 | - $PRC_values['PRC_ID'] = 0; |
|
1526 | - $PRC = EE_Registry::instance()->load_class('Price', [$PRC_values], false, false); |
|
1527 | - } else { |
|
1528 | - $PRC = EEM_Price::instance()->get_one_by_ID($prc['PRC_ID']); |
|
1529 | - // update this price with new values |
|
1530 | - foreach ($PRC_values as $field => $newprc) { |
|
1531 | - $PRC->set($field, $newprc); |
|
1532 | - } |
|
1533 | - $PRC->save(); |
|
1534 | - } |
|
1535 | - $ticket->_add_relation_to($PRC, 'Price'); |
|
1536 | - } |
|
1537 | - } |
|
1538 | - |
|
1539 | - |
|
1540 | - /** |
|
1541 | - * Add in our autosave ajax handlers |
|
1542 | - * |
|
1543 | - */ |
|
1544 | - protected function _ee_autosave_create_new() |
|
1545 | - { |
|
1546 | - } |
|
1547 | - |
|
1548 | - |
|
1549 | - /** |
|
1550 | - * More autosave handlers. |
|
1551 | - */ |
|
1552 | - protected function _ee_autosave_edit() |
|
1553 | - { |
|
1554 | - } |
|
1555 | - |
|
1556 | - |
|
1557 | - /** |
|
1558 | - * _generate_publish_box_extra_content |
|
1559 | - * |
|
1560 | - * @throws DomainException |
|
1561 | - * @throws EE_Error |
|
1562 | - * @throws InvalidArgumentException |
|
1563 | - * @throws InvalidDataTypeException |
|
1564 | - * @throws InvalidInterfaceException |
|
1565 | - * @throws ReflectionException |
|
1566 | - */ |
|
1567 | - private function _generate_publish_box_extra_content() |
|
1568 | - { |
|
1569 | - // load formatter helper |
|
1570 | - // args for getting related registrations |
|
1571 | - $approved_query_args = [ |
|
1572 | - [ |
|
1573 | - 'REG_deleted' => 0, |
|
1574 | - 'STS_ID' => EEM_Registration::status_id_approved, |
|
1575 | - ], |
|
1576 | - ]; |
|
1577 | - $not_approved_query_args = [ |
|
1578 | - [ |
|
1579 | - 'REG_deleted' => 0, |
|
1580 | - 'STS_ID' => EEM_Registration::status_id_not_approved, |
|
1581 | - ], |
|
1582 | - ]; |
|
1583 | - $pending_payment_query_args = [ |
|
1584 | - [ |
|
1585 | - 'REG_deleted' => 0, |
|
1586 | - 'STS_ID' => EEM_Registration::status_id_pending_payment, |
|
1587 | - ], |
|
1588 | - ]; |
|
1589 | - // publish box |
|
1590 | - $publish_box_extra_args = [ |
|
1591 | - 'view_approved_reg_url' => add_query_arg( |
|
1592 | - [ |
|
1593 | - 'action' => 'default', |
|
1594 | - 'event_id' => $this->_cpt_model_obj->ID(), |
|
1595 | - '_reg_status' => EEM_Registration::status_id_approved, |
|
1596 | - ], |
|
1597 | - REG_ADMIN_URL |
|
1598 | - ), |
|
1599 | - 'view_not_approved_reg_url' => add_query_arg( |
|
1600 | - [ |
|
1601 | - 'action' => 'default', |
|
1602 | - 'event_id' => $this->_cpt_model_obj->ID(), |
|
1603 | - '_reg_status' => EEM_Registration::status_id_not_approved, |
|
1604 | - ], |
|
1605 | - REG_ADMIN_URL |
|
1606 | - ), |
|
1607 | - 'view_pending_payment_reg_url' => add_query_arg( |
|
1608 | - [ |
|
1609 | - 'action' => 'default', |
|
1610 | - 'event_id' => $this->_cpt_model_obj->ID(), |
|
1611 | - '_reg_status' => EEM_Registration::status_id_pending_payment, |
|
1612 | - ], |
|
1613 | - REG_ADMIN_URL |
|
1614 | - ), |
|
1615 | - 'approved_regs' => $this->_cpt_model_obj->count_related( |
|
1616 | - 'Registration', |
|
1617 | - $approved_query_args |
|
1618 | - ), |
|
1619 | - 'not_approved_regs' => $this->_cpt_model_obj->count_related( |
|
1620 | - 'Registration', |
|
1621 | - $not_approved_query_args |
|
1622 | - ), |
|
1623 | - 'pending_payment_regs' => $this->_cpt_model_obj->count_related( |
|
1624 | - 'Registration', |
|
1625 | - $pending_payment_query_args |
|
1626 | - ), |
|
1627 | - 'misc_pub_section_class' => apply_filters( |
|
1628 | - 'FHEE_Events_Admin_Page___generate_publish_box_extra_content__misc_pub_section_class', |
|
1629 | - 'misc-pub-section' |
|
1630 | - ), |
|
1631 | - ]; |
|
1632 | - ob_start(); |
|
1633 | - do_action( |
|
1634 | - 'AHEE__Events_Admin_Page___generate_publish_box_extra_content__event_editor_overview_add', |
|
1635 | - $this->_cpt_model_obj |
|
1636 | - ); |
|
1637 | - $publish_box_extra_args['event_editor_overview_add'] = ob_get_clean(); |
|
1638 | - // load template |
|
1639 | - EEH_Template::display_template( |
|
1640 | - EVENTS_TEMPLATE_PATH . 'event_publish_box_extras.template.php', |
|
1641 | - $publish_box_extra_args |
|
1642 | - ); |
|
1643 | - } |
|
1644 | - |
|
1645 | - |
|
1646 | - /** |
|
1647 | - * @return EE_Event |
|
1648 | - */ |
|
1649 | - public function get_event_object() |
|
1650 | - { |
|
1651 | - return $this->_cpt_model_obj; |
|
1652 | - } |
|
1653 | - |
|
1654 | - |
|
1655 | - |
|
1656 | - |
|
1657 | - /** METABOXES * */ |
|
1658 | - /** |
|
1659 | - * _register_event_editor_meta_boxes |
|
1660 | - * add all metaboxes related to the event_editor |
|
1661 | - * |
|
1662 | - * @return void |
|
1663 | - * @throws EE_Error |
|
1664 | - * @throws InvalidArgumentException |
|
1665 | - * @throws InvalidDataTypeException |
|
1666 | - * @throws InvalidInterfaceException |
|
1667 | - * @throws ReflectionException |
|
1668 | - */ |
|
1669 | - protected function _register_event_editor_meta_boxes() |
|
1670 | - { |
|
1671 | - $this->verify_cpt_object(); |
|
1672 | - $use_advanced_editor = $this->admin_config->useAdvancedEditor(); |
|
1673 | - /** @var FeatureFlags $flags */ |
|
1674 | - $flags = $this->loader->getShared('EventEspresso\core\domain\services\capabilities\FeatureFlags'); |
|
1675 | - // check if the new EDTR reg options meta box is being used, and if so, don't load the legacy version |
|
1676 | - if (! $use_advanced_editor || ! $flags->featureAllowed('use_reg_options_meta_box')) { |
|
1677 | - add_meta_box( |
|
1678 | - 'espresso_event_editor_event_options', |
|
1679 | - esc_html__('Event Registration Options', 'event_espresso'), |
|
1680 | - [$this, 'registration_options_meta_box'], |
|
1681 | - $this->page_slug, |
|
1682 | - 'side' |
|
1683 | - ); |
|
1684 | - } |
|
1685 | - if (! $use_advanced_editor) { |
|
1686 | - add_meta_box( |
|
1687 | - 'espresso_event_editor_tickets', |
|
1688 | - esc_html__('Event Datetime & Ticket', 'event_espresso'), |
|
1689 | - [$this, 'ticket_metabox'], |
|
1690 | - $this->page_slug, |
|
1691 | - 'normal', |
|
1692 | - 'high' |
|
1693 | - ); |
|
1694 | - } else { |
|
1695 | - if ($flags->featureAllowed('use_reg_options_meta_box')) { |
|
1696 | - add_action( |
|
1697 | - 'add_meta_boxes_espresso_events', |
|
1698 | - function () { |
|
1699 | - global $current_screen; |
|
1700 | - remove_meta_box('authordiv', $current_screen, 'normal'); |
|
1701 | - }, |
|
1702 | - 99 |
|
1703 | - ); |
|
1704 | - } |
|
1705 | - } |
|
1706 | - // NOTE: if you're looking for other metaboxes in here, |
|
1707 | - // where a metabox has a related management page in the admin |
|
1708 | - // you will find it setup in the related management page's "_Hooks" file. |
|
1709 | - // i.e. messages metabox is found in "espresso_events_Messages_Hooks.class.php". |
|
1710 | - } |
|
1711 | - |
|
1712 | - |
|
1713 | - /** |
|
1714 | - * @throws DomainException |
|
1715 | - * @throws EE_Error |
|
1716 | - * @throws InvalidArgumentException |
|
1717 | - * @throws InvalidDataTypeException |
|
1718 | - * @throws InvalidInterfaceException |
|
1719 | - * @throws ReflectionException |
|
1720 | - */ |
|
1721 | - public function ticket_metabox() |
|
1722 | - { |
|
1723 | - $existing_datetime_ids = $existing_ticket_ids = []; |
|
1724 | - // defaults for template args |
|
1725 | - $template_args = [ |
|
1726 | - 'existing_datetime_ids' => '', |
|
1727 | - 'event_datetime_help_link' => '', |
|
1728 | - 'ticket_options_help_link' => '', |
|
1729 | - 'time' => null, |
|
1730 | - 'ticket_rows' => '', |
|
1731 | - 'existing_ticket_ids' => '', |
|
1732 | - 'total_ticket_rows' => 1, |
|
1733 | - 'ticket_js_structure' => '', |
|
1734 | - 'trash_icon' => 'ee-lock-icon', |
|
1735 | - 'disabled' => '', |
|
1736 | - ]; |
|
1737 | - $event_id = $this->_cpt_model_obj instanceof EE_Event ? $this->_cpt_model_obj->ID() : 0; |
|
1738 | - $event_timezone_string = $this->_cpt_model_obj instanceof EE_Event |
|
1739 | - ? $this->_cpt_model_obj->timezone_string() |
|
1740 | - : ''; |
|
1741 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
1742 | - /** |
|
1743 | - * 1. Start with retrieving Datetimes |
|
1744 | - * 2. Fore each datetime get related tickets |
|
1745 | - * 3. For each ticket get related prices |
|
1746 | - */ |
|
1747 | - $times = $this->datetimeModel($event_timezone_string)->get_all_event_dates($event_id); |
|
1748 | - /** @type EE_Datetime $first_datetime */ |
|
1749 | - $first_datetime = reset($times); |
|
1750 | - // do we get related tickets? |
|
1751 | - if ( |
|
1752 | - $first_datetime instanceof EE_Datetime |
|
1753 | - && $first_datetime->ID() !== 0 |
|
1754 | - ) { |
|
1755 | - $existing_datetime_ids[] = $first_datetime->get('DTT_ID'); |
|
1756 | - $template_args['time'] = $first_datetime; |
|
1757 | - $related_tickets = $first_datetime->tickets( |
|
1758 | - [ |
|
1759 | - ['OR' => ['TKT_deleted' => 1, 'TKT_deleted*' => 0]], |
|
1760 | - 'default_where_conditions' => 'none', |
|
1761 | - ] |
|
1762 | - ); |
|
1763 | - if (! empty($related_tickets)) { |
|
1764 | - $template_args['total_ticket_rows'] = count($related_tickets); |
|
1765 | - $row = 0; |
|
1766 | - foreach ($related_tickets as $ticket) { |
|
1767 | - $existing_ticket_ids[] = $ticket->get('TKT_ID'); |
|
1768 | - $template_args['ticket_rows'] .= $this->_get_ticket_row($ticket, false, $row); |
|
1769 | - $row++; |
|
1770 | - } |
|
1771 | - } else { |
|
1772 | - $template_args['total_ticket_rows'] = 1; |
|
1773 | - /** @type EE_Ticket $ticket */ |
|
1774 | - $ticket = $this->ticketModel($event_timezone_string)->create_default_object(); |
|
1775 | - $template_args['ticket_rows'] .= $this->_get_ticket_row($ticket); |
|
1776 | - } |
|
1777 | - } else { |
|
1778 | - $template_args['time'] = $times[0]; |
|
1779 | - /** @type EE_Ticket $ticket */ |
|
1780 | - $ticket = $this->ticketModel($event_timezone_string)->get_all_default_tickets(); |
|
1781 | - $template_args['ticket_rows'] .= $this->_get_ticket_row($ticket[1]); |
|
1782 | - // NOTE: we're just sending the first default row |
|
1783 | - // (decaf can't manage default tickets so this should be sufficient); |
|
1784 | - } |
|
1785 | - $template_args['event_datetime_help_link'] = $this->_get_help_tab_link( |
|
1786 | - 'event_editor_event_datetimes_help_tab' |
|
1787 | - ); |
|
1788 | - $template_args['ticket_options_help_link'] = $this->_get_help_tab_link('ticket_options_info'); |
|
1789 | - $template_args['existing_datetime_ids'] = implode(',', $existing_datetime_ids); |
|
1790 | - $template_args['existing_ticket_ids'] = implode(',', $existing_ticket_ids); |
|
1791 | - $template_args['ticket_js_structure'] = $this->_get_ticket_row( |
|
1792 | - $this->ticketModel($event_timezone_string)->create_default_object(), |
|
1793 | - true |
|
1794 | - ); |
|
1795 | - $template = apply_filters( |
|
1796 | - 'FHEE__Events_Admin_Page__ticket_metabox__template', |
|
1797 | - EVENTS_TEMPLATE_PATH . 'event_tickets_metabox_main.template.php' |
|
1798 | - ); |
|
1799 | - EEH_Template::display_template($template, $template_args); |
|
1800 | - } |
|
1801 | - |
|
1802 | - |
|
1803 | - /** |
|
1804 | - * Setup an individual ticket form for the decaf event editor page |
|
1805 | - * |
|
1806 | - * @param EE_Ticket $ticket the ticket object |
|
1807 | - * @param boolean $skeleton whether we're generating a skeleton for js manipulation |
|
1808 | - * @param int $row |
|
1809 | - * @return string generated html for the ticket row. |
|
1810 | - * @throws DomainException |
|
1811 | - * @throws EE_Error |
|
1812 | - * @throws InvalidArgumentException |
|
1813 | - * @throws InvalidDataTypeException |
|
1814 | - * @throws InvalidInterfaceException |
|
1815 | - * @throws ReflectionException |
|
1816 | - */ |
|
1817 | - private function _get_ticket_row($ticket, $skeleton = false, $row = 0) |
|
1818 | - { |
|
1819 | - $template_args = [ |
|
1820 | - 'tkt_status_class' => ' tkt-status-' . $ticket->ticket_status(), |
|
1821 | - 'tkt_archive_class' => $ticket->ticket_status() === EE_Ticket::archived && ! $skeleton ? ' tkt-archived' |
|
1822 | - : '', |
|
1823 | - 'ticketrow' => $skeleton ? 'TICKETNUM' : $row, |
|
1824 | - 'TKT_ID' => $ticket->get('TKT_ID'), |
|
1825 | - 'TKT_name' => $ticket->get('TKT_name'), |
|
1826 | - 'TKT_start_date' => $skeleton ? '' : $ticket->get_date('TKT_start_date', 'Y-m-d h:i a'), |
|
1827 | - 'TKT_end_date' => $skeleton ? '' : $ticket->get_date('TKT_end_date', 'Y-m-d h:i a'), |
|
1828 | - 'TKT_is_default' => $ticket->get('TKT_is_default'), |
|
1829 | - 'TKT_qty' => $ticket->get_pretty('TKT_qty', 'input'), |
|
1830 | - 'edit_ticketrow_name' => $skeleton ? 'TICKETNAMEATTR' : 'edit_tickets', |
|
1831 | - 'TKT_sold' => $skeleton ? 0 : $ticket->get('TKT_sold'), |
|
1832 | - 'trash_icon' => ($skeleton || (! empty($ticket) && ! $ticket->get('TKT_deleted'))) |
|
1833 | - && (! empty($ticket) && $ticket->get('TKT_sold') === 0) |
|
1834 | - ? 'trash-icon dashicons dashicons-post-trash clickable' : 'ee-lock-icon', |
|
1835 | - 'disabled' => $skeleton || (! empty($ticket) && ! $ticket->get('TKT_deleted')) ? '' |
|
1836 | - : ' disabled=disabled', |
|
1837 | - ]; |
|
1838 | - $price = $ticket->ID() !== 0 |
|
1839 | - ? $ticket->get_first_related('Price', ['default_where_conditions' => 'none']) |
|
1840 | - : EEM_Price::instance()->create_default_object(); |
|
1841 | - $price_args = [ |
|
1842 | - 'price_currency_symbol' => EE_Registry::instance()->CFG->currency->sign, |
|
1843 | - 'PRC_amount' => $price->get('PRC_amount'), |
|
1844 | - 'PRT_ID' => $price->get('PRT_ID'), |
|
1845 | - 'PRC_ID' => $price->get('PRC_ID'), |
|
1846 | - 'PRC_is_default' => $price->get('PRC_is_default'), |
|
1847 | - ]; |
|
1848 | - // make sure we have default start and end dates if skeleton |
|
1849 | - // handle rows that should NOT be empty |
|
1850 | - if (empty($template_args['TKT_start_date'])) { |
|
1851 | - // if empty then the start date will be now. |
|
1852 | - $template_args['TKT_start_date'] = date('Y-m-d h:i a', current_time('timestamp')); |
|
1853 | - } |
|
1854 | - if (empty($template_args['TKT_end_date'])) { |
|
1855 | - // get the earliest datetime (if present); |
|
1856 | - $earliest_dtt = $this->_cpt_model_obj->ID() > 0 |
|
1857 | - ? $this->_cpt_model_obj->get_first_related( |
|
1858 | - 'Datetime', |
|
1859 | - ['order_by' => ['DTT_EVT_start' => 'ASC']] |
|
1860 | - ) |
|
1861 | - : null; |
|
1862 | - if (! empty($earliest_dtt)) { |
|
1863 | - $template_args['TKT_end_date'] = $earliest_dtt->get_datetime('DTT_EVT_start', 'Y-m-d', 'h:i a'); |
|
1864 | - } else { |
|
1865 | - $template_args['TKT_end_date'] = date( |
|
1866 | - 'Y-m-d h:i a', |
|
1867 | - mktime(0, 0, 0, date('m'), date('d') + 7, date('Y')) |
|
1868 | - ); |
|
1869 | - } |
|
1870 | - } |
|
1871 | - $template_args = array_merge($template_args, $price_args); |
|
1872 | - $template = apply_filters( |
|
1873 | - 'FHEE__Events_Admin_Page__get_ticket_row__template', |
|
1874 | - EVENTS_TEMPLATE_PATH . 'event_tickets_metabox_ticket_row.template.php', |
|
1875 | - $ticket |
|
1876 | - ); |
|
1877 | - return EEH_Template::display_template($template, $template_args, true); |
|
1878 | - } |
|
1879 | - |
|
1880 | - |
|
1881 | - /** |
|
1882 | - * @throws DomainException |
|
1883 | - * @throws EE_Error |
|
1884 | - */ |
|
1885 | - public function registration_options_meta_box() |
|
1886 | - { |
|
1887 | - $yes_no_values = [ |
|
1888 | - ['id' => true, 'text' => esc_html__('Yes', 'event_espresso')], |
|
1889 | - ['id' => false, 'text' => esc_html__('No', 'event_espresso')], |
|
1890 | - ]; |
|
1891 | - $default_reg_status_values = EEM_Registration::reg_status_array( |
|
1892 | - [ |
|
1893 | - EEM_Registration::status_id_cancelled, |
|
1894 | - EEM_Registration::status_id_declined, |
|
1895 | - EEM_Registration::status_id_incomplete, |
|
1896 | - ], |
|
1897 | - true |
|
1898 | - ); |
|
1899 | - // $template_args['is_active_select'] = EEH_Form_Fields::select_input('is_active', $yes_no_values, $this->_cpt_model_obj->is_active()); |
|
1900 | - $template_args['_event'] = $this->_cpt_model_obj; |
|
1901 | - $template_args['active_status'] = $this->_cpt_model_obj->pretty_active_status(false); |
|
1902 | - $template_args['additional_limit'] = $this->_cpt_model_obj->additional_limit(); |
|
1903 | - $template_args['default_registration_status'] = EEH_Form_Fields::select_input( |
|
1904 | - 'default_reg_status', |
|
1905 | - $default_reg_status_values, |
|
1906 | - $this->_cpt_model_obj->default_registration_status() |
|
1907 | - ); |
|
1908 | - $template_args['display_description'] = EEH_Form_Fields::select_input( |
|
1909 | - 'display_desc', |
|
1910 | - $yes_no_values, |
|
1911 | - $this->_cpt_model_obj->display_description() |
|
1912 | - ); |
|
1913 | - $template_args['display_ticket_selector'] = EEH_Form_Fields::select_input( |
|
1914 | - 'display_ticket_selector', |
|
1915 | - $yes_no_values, |
|
1916 | - $this->_cpt_model_obj->display_ticket_selector(), |
|
1917 | - '', |
|
1918 | - '', |
|
1919 | - false |
|
1920 | - ); |
|
1921 | - $template_args['additional_registration_options'] = apply_filters( |
|
1922 | - 'FHEE__Events_Admin_Page__registration_options_meta_box__additional_registration_options', |
|
1923 | - '', |
|
1924 | - $template_args, |
|
1925 | - $yes_no_values, |
|
1926 | - $default_reg_status_values |
|
1927 | - ); |
|
1928 | - EEH_Template::display_template( |
|
1929 | - EVENTS_TEMPLATE_PATH . 'event_registration_options.template.php', |
|
1930 | - $template_args |
|
1931 | - ); |
|
1932 | - } |
|
1933 | - |
|
1934 | - |
|
1935 | - /** |
|
1936 | - * _get_events() |
|
1937 | - * This method simply returns all the events (for the given _view and paging) |
|
1938 | - * |
|
1939 | - * @param int $per_page count of items per page (20 default); |
|
1940 | - * @param int $current_page what is the current page being viewed. |
|
1941 | - * @param bool $count if TRUE then we just return a count of ALL events matching the given _view. |
|
1942 | - * If FALSE then we return an array of event objects |
|
1943 | - * that match the given _view and paging parameters. |
|
1944 | - * @return array|int an array of event objects or count of how many events. |
|
1945 | - * @throws EE_Error |
|
1946 | - * @throws InvalidArgumentException |
|
1947 | - * @throws InvalidDataTypeException |
|
1948 | - * @throws InvalidInterfaceException |
|
1949 | - * @throws ReflectionException |
|
1950 | - * @throws Exception |
|
1951 | - * @throws Exception |
|
1952 | - * @throws Exception |
|
1953 | - */ |
|
1954 | - public function get_events($per_page = 10, $current_page = 1, $count = false) |
|
1955 | - { |
|
1956 | - $EEME = $this->eventModel(); |
|
1957 | - $offset = ($current_page - 1) * $per_page; |
|
1958 | - $limit = $count ? null : $offset . ',' . $per_page; |
|
1959 | - $orderby = isset($this->_req_data['orderby']) ? $this->_req_data['orderby'] : 'EVT_ID'; |
|
1960 | - $order = isset($this->_req_data['order']) ? $this->_req_data['order'] : 'DESC'; |
|
1961 | - if (isset($this->_req_data['month_range'])) { |
|
1962 | - $pieces = explode(' ', $this->_req_data['month_range'], 3); |
|
1963 | - // simulate the FIRST day of the month, that fixes issues for months like February |
|
1964 | - // where PHP doesn't know what to assume for date. |
|
1965 | - // @see https://events.codebasehq.com/projects/event-espresso/tickets/10437 |
|
1966 | - $month_r = ! empty($pieces[0]) ? date('m', EEH_DTT_Helper::first_of_month_timestamp($pieces[0])) : ''; |
|
1967 | - $year_r = ! empty($pieces[1]) ? $pieces[1] : ''; |
|
1968 | - } |
|
1969 | - $where = []; |
|
1970 | - $status = isset($this->_req_data['status']) ? $this->_req_data['status'] : null; |
|
1971 | - // determine what post_status our condition will have for the query. |
|
1972 | - switch ($status) { |
|
1973 | - case 'month': |
|
1974 | - case 'today': |
|
1975 | - case null: |
|
1976 | - case 'all': |
|
1977 | - break; |
|
1978 | - case 'draft': |
|
1979 | - $where['status'] = ['IN', ['draft', 'auto-draft']]; |
|
1980 | - break; |
|
1981 | - default: |
|
1982 | - $where['status'] = $status; |
|
1983 | - } |
|
1984 | - // categories? |
|
1985 | - $category = isset($this->_req_data['EVT_CAT']) && $this->_req_data['EVT_CAT'] > 0 |
|
1986 | - ? $this->_req_data['EVT_CAT'] : null; |
|
1987 | - if (! empty($category)) { |
|
1988 | - $where['Term_Taxonomy.taxonomy'] = EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY; |
|
1989 | - $where['Term_Taxonomy.term_id'] = $category; |
|
1990 | - } |
|
1991 | - // date where conditions |
|
1992 | - $start_formats = EEM_Datetime::instance()->get_formats_for('DTT_EVT_start'); |
|
1993 | - if (isset($this->_req_data['month_range']) && $this->_req_data['month_range'] !== '') { |
|
1994 | - $DateTime = new DateTime( |
|
1995 | - $year_r . '-' . $month_r . '-01 00:00:00', |
|
1996 | - new DateTimeZone('UTC') |
|
1997 | - ); |
|
1998 | - $start = $DateTime->getTimestamp(); |
|
1999 | - // set the datetime to be the end of the month |
|
2000 | - $DateTime->setDate( |
|
2001 | - $year_r, |
|
2002 | - $month_r, |
|
2003 | - $DateTime->format('t') |
|
2004 | - )->setTime(23, 59, 59); |
|
2005 | - $end = $DateTime->getTimestamp(); |
|
2006 | - $where['Datetime.DTT_EVT_start'] = ['BETWEEN', [$start, $end]]; |
|
2007 | - } elseif (isset($this->_req_data['status']) && $this->_req_data['status'] === 'today') { |
|
2008 | - $DateTime = |
|
2009 | - new DateTime('now', new DateTimeZone(EEM_Event::instance()->get_timezone())); |
|
2010 | - $start = $DateTime->setTime(0, 0, 0)->format(implode(' ', $start_formats)); |
|
2011 | - $end = $DateTime->setTime(23, 59, 59)->format(implode(' ', $start_formats)); |
|
2012 | - $where['Datetime.DTT_EVT_start'] = ['BETWEEN', [$start, $end]]; |
|
2013 | - } elseif (isset($this->_req_data['status']) && $this->_req_data['status'] === 'month') { |
|
2014 | - $now = date('Y-m-01'); |
|
2015 | - $DateTime = |
|
2016 | - new DateTime($now, new DateTimeZone(EEM_Event::instance()->get_timezone())); |
|
2017 | - $start = $DateTime->setTime(0, 0, 0)->format(implode(' ', $start_formats)); |
|
2018 | - $end = $DateTime->setDate(date('Y'), date('m'), $DateTime->format('t')) |
|
2019 | - ->setTime(23, 59, 59) |
|
2020 | - ->format(implode(' ', $start_formats)); |
|
2021 | - $where['Datetime.DTT_EVT_start'] = ['BETWEEN', [$start, $end]]; |
|
2022 | - } |
|
2023 | - if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_events', 'get_events')) { |
|
2024 | - $where['EVT_wp_user'] = get_current_user_id(); |
|
2025 | - } elseif ( |
|
2026 | - ! isset($where['status']) |
|
2027 | - && ! EE_Registry::instance()->CAP->current_user_can('ee_read_private_events', 'get_events') |
|
2028 | - ) { |
|
2029 | - $where['OR'] = [ |
|
2030 | - 'status*restrict_private' => ['!=', 'private'], |
|
2031 | - 'AND' => [ |
|
2032 | - 'status*inclusive' => ['=', 'private'], |
|
2033 | - 'EVT_wp_user' => get_current_user_id(), |
|
2034 | - ], |
|
2035 | - ]; |
|
2036 | - } |
|
2037 | - |
|
2038 | - if ( |
|
2039 | - isset($this->_req_data['EVT_wp_user']) |
|
2040 | - && (int) $this->_req_data['EVT_wp_user'] !== (int) get_current_user_id() |
|
2041 | - && EE_Registry::instance()->CAP->current_user_can('ee_read_others_events', 'get_events') |
|
2042 | - ) { |
|
2043 | - $where['EVT_wp_user'] = $this->_req_data['EVT_wp_user']; |
|
2044 | - } |
|
2045 | - // search query handling |
|
2046 | - if (isset($this->_req_data['s'])) { |
|
2047 | - $search_string = '%' . $this->_req_data['s'] . '%'; |
|
2048 | - $where['OR'] = [ |
|
2049 | - 'EVT_name' => ['LIKE', $search_string], |
|
2050 | - 'EVT_desc' => ['LIKE', $search_string], |
|
2051 | - 'EVT_short_desc' => ['LIKE', $search_string], |
|
2052 | - ]; |
|
2053 | - } |
|
2054 | - // filter events by venue. |
|
2055 | - if (isset($this->_req_data['venue']) && ! empty($this->_req_data['venue'])) { |
|
2056 | - $where['Venue.VNU_ID'] = absint($this->_req_data['venue']); |
|
2057 | - } |
|
2058 | - $where = apply_filters('FHEE__Events_Admin_Page__get_events__where', $where, $this->_req_data); |
|
2059 | - $query_params = apply_filters( |
|
2060 | - 'FHEE__Events_Admin_Page__get_events__query_params', |
|
2061 | - [ |
|
2062 | - $where, |
|
2063 | - 'limit' => $limit, |
|
2064 | - 'order_by' => $orderby, |
|
2065 | - 'order' => $order, |
|
2066 | - 'group_by' => 'EVT_ID', |
|
2067 | - ], |
|
2068 | - $this->_req_data |
|
2069 | - ); |
|
2070 | - |
|
2071 | - // let's first check if we have special requests coming in. |
|
2072 | - if (isset($this->_req_data['active_status'])) { |
|
2073 | - switch ($this->_req_data['active_status']) { |
|
2074 | - case 'upcoming': |
|
2075 | - return $EEME->get_upcoming_events($query_params, $count); |
|
2076 | - case 'expired': |
|
2077 | - return $EEME->get_expired_events($query_params, $count); |
|
2078 | - case 'active': |
|
2079 | - return $EEME->get_active_events($query_params, $count); |
|
2080 | - case 'inactive': |
|
2081 | - return $EEME->get_inactive_events($query_params, $count); |
|
2082 | - } |
|
2083 | - } |
|
2084 | - return $count |
|
2085 | - ? $EEME->count([$where], 'EVT_ID', true) |
|
2086 | - : $EEME->get_all($query_params); |
|
2087 | - } |
|
2088 | - |
|
2089 | - |
|
2090 | - /** |
|
2091 | - * handling for WordPress CPT actions (trash, restore, delete) |
|
2092 | - * |
|
2093 | - * @param string $post_id |
|
2094 | - * @throws EE_Error |
|
2095 | - * @throws InvalidArgumentException |
|
2096 | - * @throws InvalidDataTypeException |
|
2097 | - * @throws InvalidInterfaceException |
|
2098 | - * @throws ReflectionException |
|
2099 | - */ |
|
2100 | - public function trash_cpt_item($post_id) |
|
2101 | - { |
|
2102 | - $this->_req_data['EVT_ID'] = $post_id; |
|
2103 | - $this->_trash_or_restore_event('trash', false); |
|
2104 | - } |
|
2105 | - |
|
2106 | - |
|
2107 | - /** |
|
2108 | - * @param string $post_id |
|
2109 | - * @throws EE_Error |
|
2110 | - * @throws InvalidArgumentException |
|
2111 | - * @throws InvalidDataTypeException |
|
2112 | - * @throws InvalidInterfaceException |
|
2113 | - * @throws ReflectionException |
|
2114 | - */ |
|
2115 | - public function restore_cpt_item($post_id) |
|
2116 | - { |
|
2117 | - $this->_req_data['EVT_ID'] = $post_id; |
|
2118 | - $this->_trash_or_restore_event('draft', false); |
|
2119 | - } |
|
2120 | - |
|
2121 | - |
|
2122 | - /** |
|
2123 | - * @param string $post_id |
|
2124 | - * @throws EE_Error |
|
2125 | - * @throws InvalidArgumentException |
|
2126 | - * @throws InvalidDataTypeException |
|
2127 | - * @throws InvalidInterfaceException |
|
2128 | - * @throws ReflectionException |
|
2129 | - */ |
|
2130 | - public function delete_cpt_item($post_id) |
|
2131 | - { |
|
2132 | - throw new EE_Error( |
|
2133 | - esc_html__( |
|
2134 | - 'Please contact Event Espresso support with the details of the steps taken to produce this error.', |
|
2135 | - 'event_espresso' |
|
2136 | - ) |
|
2137 | - ); |
|
2138 | - $this->_req_data['EVT_ID'] = $post_id; |
|
2139 | - $this->_delete_event(); |
|
2140 | - } |
|
2141 | - |
|
2142 | - |
|
2143 | - /** |
|
2144 | - * _trash_or_restore_event |
|
2145 | - * |
|
2146 | - * @param string $event_status |
|
2147 | - * @param bool $redirect_after |
|
2148 | - * @throws EE_Error |
|
2149 | - * @throws InvalidArgumentException |
|
2150 | - * @throws InvalidDataTypeException |
|
2151 | - * @throws InvalidInterfaceException |
|
2152 | - * @throws ReflectionException |
|
2153 | - */ |
|
2154 | - protected function _trash_or_restore_event($event_status = 'trash', $redirect_after = true) |
|
2155 | - { |
|
2156 | - // determine the event id and set to array. |
|
2157 | - $EVT_ID = isset($this->_req_data['EVT_ID']) ? absint($this->_req_data['EVT_ID']) : false; |
|
2158 | - // loop thru events |
|
2159 | - if ($EVT_ID) { |
|
2160 | - // clean status |
|
2161 | - $event_status = sanitize_key($event_status); |
|
2162 | - // grab status |
|
2163 | - if (! empty($event_status)) { |
|
2164 | - $success = $this->_change_event_status($EVT_ID, $event_status); |
|
2165 | - } else { |
|
2166 | - $success = false; |
|
2167 | - $msg = esc_html__( |
|
2168 | - 'An error occurred. The event could not be moved to the trash because a valid event status was not not supplied.', |
|
2169 | - 'event_espresso' |
|
2170 | - ); |
|
2171 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2172 | - } |
|
2173 | - } else { |
|
2174 | - $success = false; |
|
2175 | - $msg = esc_html__( |
|
2176 | - 'An error occurred. The event could not be moved to the trash because a valid event ID was not not supplied.', |
|
2177 | - 'event_espresso' |
|
2178 | - ); |
|
2179 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2180 | - } |
|
2181 | - $action = $event_status === 'trash' ? 'moved to the trash' : 'restored from the trash'; |
|
2182 | - if ($redirect_after) { |
|
2183 | - $this->_redirect_after_action($success, 'Event', $action, ['action' => 'default']); |
|
2184 | - } |
|
2185 | - } |
|
2186 | - |
|
2187 | - |
|
2188 | - /** |
|
2189 | - * _trash_or_restore_events |
|
2190 | - * |
|
2191 | - * @param string $event_status |
|
2192 | - * @return void |
|
2193 | - * @throws EE_Error |
|
2194 | - * @throws InvalidArgumentException |
|
2195 | - * @throws InvalidDataTypeException |
|
2196 | - * @throws InvalidInterfaceException |
|
2197 | - * @throws ReflectionException |
|
2198 | - */ |
|
2199 | - protected function _trash_or_restore_events($event_status = 'trash') |
|
2200 | - { |
|
2201 | - // clean status |
|
2202 | - $event_status = sanitize_key($event_status); |
|
2203 | - // grab status |
|
2204 | - if (! empty($event_status)) { |
|
2205 | - $success = true; |
|
2206 | - // determine the event id and set to array. |
|
2207 | - $EVT_IDs = isset($this->_req_data['EVT_IDs']) ? (array) $this->_req_data['EVT_IDs'] : []; |
|
2208 | - // loop thru events |
|
2209 | - foreach ($EVT_IDs as $EVT_ID) { |
|
2210 | - if ($EVT_ID = absint($EVT_ID)) { |
|
2211 | - $results = $this->_change_event_status($EVT_ID, $event_status); |
|
2212 | - $success = $results !== false ? $success : false; |
|
2213 | - } else { |
|
2214 | - $msg = sprintf( |
|
2215 | - esc_html__( |
|
2216 | - 'An error occurred. Event #%d could not be moved to the trash because a valid event ID was not not supplied.', |
|
2217 | - 'event_espresso' |
|
2218 | - ), |
|
2219 | - $EVT_ID |
|
2220 | - ); |
|
2221 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2222 | - $success = false; |
|
2223 | - } |
|
2224 | - } |
|
2225 | - } else { |
|
2226 | - $success = false; |
|
2227 | - $msg = esc_html__( |
|
2228 | - 'An error occurred. The event could not be moved to the trash because a valid event status was not not supplied.', |
|
2229 | - 'event_espresso' |
|
2230 | - ); |
|
2231 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2232 | - } |
|
2233 | - // in order to force a pluralized result message we need to send back a success status greater than 1 |
|
2234 | - $success = $success ? 2 : false; |
|
2235 | - $action = $event_status === 'trash' ? 'moved to the trash' : 'restored from the trash'; |
|
2236 | - $this->_redirect_after_action($success, 'Events', $action, ['action' => 'default']); |
|
2237 | - } |
|
2238 | - |
|
2239 | - |
|
2240 | - /** |
|
2241 | - * _trash_or_restore_events |
|
2242 | - * |
|
2243 | - * @param int $EVT_ID |
|
2244 | - * @param string $event_status |
|
2245 | - * @return bool |
|
2246 | - * @throws EE_Error |
|
2247 | - * @throws InvalidArgumentException |
|
2248 | - * @throws InvalidDataTypeException |
|
2249 | - * @throws InvalidInterfaceException |
|
2250 | - * @throws ReflectionException |
|
2251 | - */ |
|
2252 | - private function _change_event_status($EVT_ID = 0, $event_status = '') |
|
2253 | - { |
|
2254 | - // grab event id |
|
2255 | - if (! $EVT_ID) { |
|
2256 | - $msg = esc_html__( |
|
2257 | - 'An error occurred. No Event ID or an invalid Event ID was received.', |
|
2258 | - 'event_espresso' |
|
2259 | - ); |
|
2260 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2261 | - return false; |
|
2262 | - } |
|
2263 | - $this->_cpt_model_obj = EEM_Event::instance()->get_one_by_ID($EVT_ID); |
|
2264 | - // clean status |
|
2265 | - $event_status = sanitize_key($event_status); |
|
2266 | - // grab status |
|
2267 | - if (empty($event_status)) { |
|
2268 | - $msg = esc_html__( |
|
2269 | - 'An error occurred. No Event Status or an invalid Event Status was received.', |
|
2270 | - 'event_espresso' |
|
2271 | - ); |
|
2272 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2273 | - return false; |
|
2274 | - } |
|
2275 | - // was event trashed or restored ? |
|
2276 | - switch ($event_status) { |
|
2277 | - case 'draft': |
|
2278 | - $action = 'restored from the trash'; |
|
2279 | - $hook = 'AHEE_event_restored_from_trash'; |
|
2280 | - break; |
|
2281 | - case 'trash': |
|
2282 | - $action = 'moved to the trash'; |
|
2283 | - $hook = 'AHEE_event_moved_to_trash'; |
|
2284 | - break; |
|
2285 | - default: |
|
2286 | - $action = 'updated'; |
|
2287 | - $hook = false; |
|
2288 | - } |
|
2289 | - // use class to change status |
|
2290 | - $this->_cpt_model_obj->set_status($event_status); |
|
2291 | - $success = $this->_cpt_model_obj->save(); |
|
2292 | - if ($success === false) { |
|
2293 | - $msg = sprintf(esc_html__('An error occurred. The event could not be %s.', 'event_espresso'), $action); |
|
2294 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2295 | - return false; |
|
2296 | - } |
|
2297 | - if ($hook) { |
|
2298 | - do_action($hook); |
|
2299 | - } |
|
2300 | - return true; |
|
2301 | - } |
|
2302 | - |
|
2303 | - |
|
2304 | - /** |
|
2305 | - * _delete_event |
|
2306 | - * |
|
2307 | - * @throws InvalidArgumentException |
|
2308 | - * @throws InvalidDataTypeException |
|
2309 | - * @throws InvalidInterfaceException |
|
2310 | - */ |
|
2311 | - protected function _delete_event() |
|
2312 | - { |
|
2313 | - $this->generateDeletionPreview(isset($this->_req_data['EVT_ID']) ? $this->_req_data['EVT_ID'] : []); |
|
2314 | - } |
|
2315 | - |
|
2316 | - |
|
2317 | - /** |
|
2318 | - * Gets the tree traversal batch persister. |
|
2319 | - * |
|
2320 | - * @return NodeGroupDao |
|
2321 | - * @throws InvalidArgumentException |
|
2322 | - * @throws InvalidDataTypeException |
|
2323 | - * @throws InvalidInterfaceException |
|
2324 | - * @since 4.10.12.p |
|
2325 | - */ |
|
2326 | - protected function getModelObjNodeGroupPersister() |
|
2327 | - { |
|
2328 | - if (! $this->model_obj_node_group_persister instanceof NodeGroupDao) { |
|
2329 | - $this->model_obj_node_group_persister = |
|
2330 | - $this->getLoader()->load('\EventEspresso\core\services\orm\tree_traversal\NodeGroupDao'); |
|
2331 | - } |
|
2332 | - return $this->model_obj_node_group_persister; |
|
2333 | - } |
|
2334 | - |
|
2335 | - |
|
2336 | - /** |
|
2337 | - * _delete_events |
|
2338 | - * |
|
2339 | - * @return void |
|
2340 | - * @throws InvalidArgumentException |
|
2341 | - * @throws InvalidDataTypeException |
|
2342 | - * @throws InvalidInterfaceException |
|
2343 | - */ |
|
2344 | - protected function _delete_events() |
|
2345 | - { |
|
2346 | - $this->generateDeletionPreview(isset($this->_req_data['EVT_IDs']) ? (array) $this->_req_data['EVT_IDs'] : []); |
|
2347 | - } |
|
2348 | - |
|
2349 | - |
|
2350 | - protected function generateDeletionPreview($event_ids) |
|
2351 | - { |
|
2352 | - $event_ids = (array) $event_ids; |
|
2353 | - // Set a code we can use to reference this deletion task in the batch jobs and preview page. |
|
2354 | - $deletion_job_code = $this->getModelObjNodeGroupPersister()->generateGroupCode(); |
|
2355 | - $return_url = EE_Admin_Page::add_query_args_and_nonce( |
|
2356 | - [ |
|
2357 | - 'action' => 'preview_deletion', |
|
2358 | - 'deletion_job_code' => $deletion_job_code, |
|
2359 | - ], |
|
2360 | - $this->_admin_base_url |
|
2361 | - ); |
|
2362 | - $event_ids = array_map( |
|
2363 | - 'intval', |
|
2364 | - $event_ids |
|
2365 | - ); |
|
2366 | - |
|
2367 | - EEH_URL::safeRedirectAndExit( |
|
2368 | - EE_Admin_Page::add_query_args_and_nonce( |
|
2369 | - [ |
|
2370 | - 'page' => 'espresso_batch', |
|
2371 | - 'batch' => EED_Batch::batch_job, |
|
2372 | - 'EVT_IDs' => $event_ids, |
|
2373 | - 'deletion_job_code' => $deletion_job_code, |
|
2374 | - 'job_handler' => urlencode('EventEspressoBatchRequest\JobHandlers\PreviewEventDeletion'), |
|
2375 | - 'return_url' => urlencode($return_url), |
|
2376 | - ], |
|
2377 | - admin_url() |
|
2378 | - ) |
|
2379 | - ); |
|
2380 | - } |
|
2381 | - |
|
2382 | - |
|
2383 | - /** |
|
2384 | - * Checks for a POST submission |
|
2385 | - * |
|
2386 | - * @since 4.10.12.p |
|
2387 | - */ |
|
2388 | - protected function confirmDeletion() |
|
2389 | - { |
|
2390 | - $deletion_redirect_logic = |
|
2391 | - $this->getLoader()->getShared('\EventEspresso\core\domain\services\admin\events\data\ConfirmDeletion'); |
|
2392 | - $deletion_redirect_logic->handle($this->get_request_data(), $this->admin_base_url()); |
|
2393 | - } |
|
2394 | - |
|
2395 | - |
|
2396 | - /** |
|
2397 | - * A page for users to preview what exactly will be deleted, and confirm they want to delete it. |
|
2398 | - * |
|
2399 | - * @throws EE_Error |
|
2400 | - * @since 4.10.12.p |
|
2401 | - */ |
|
2402 | - protected function previewDeletion() |
|
2403 | - { |
|
2404 | - $preview_deletion_logic = |
|
2405 | - $this->getLoader()->getShared('\EventEspresso\core\domain\services\admin\events\data\PreviewDeletion'); |
|
2406 | - $this->set_template_args($preview_deletion_logic->handle($this->get_request_data(), $this->admin_base_url())); |
|
2407 | - $this->display_admin_page_with_no_sidebar(); |
|
2408 | - } |
|
2409 | - |
|
2410 | - |
|
2411 | - /** |
|
2412 | - * get total number of events |
|
2413 | - * |
|
2414 | - * @return int |
|
2415 | - * @throws EE_Error |
|
2416 | - * @throws InvalidArgumentException |
|
2417 | - * @throws InvalidDataTypeException |
|
2418 | - * @throws InvalidInterfaceException |
|
2419 | - */ |
|
2420 | - public function total_events() |
|
2421 | - { |
|
2422 | - return EEM_Event::instance()->count(['caps' => 'read_admin'], 'EVT_ID', true); |
|
2423 | - } |
|
2424 | - |
|
2425 | - |
|
2426 | - /** |
|
2427 | - * get total number of draft events |
|
2428 | - * |
|
2429 | - * @return int |
|
2430 | - * @throws EE_Error |
|
2431 | - * @throws InvalidArgumentException |
|
2432 | - * @throws InvalidDataTypeException |
|
2433 | - * @throws InvalidInterfaceException |
|
2434 | - */ |
|
2435 | - public function total_events_draft() |
|
2436 | - { |
|
2437 | - $where = [ |
|
2438 | - 'status' => ['IN', ['draft', 'auto-draft']], |
|
2439 | - ]; |
|
2440 | - return EEM_Event::instance()->count([$where, 'caps' => 'read_admin'], 'EVT_ID', true); |
|
2441 | - } |
|
2442 | - |
|
2443 | - |
|
2444 | - /** |
|
2445 | - * get total number of trashed events |
|
2446 | - * |
|
2447 | - * @return int |
|
2448 | - * @throws EE_Error |
|
2449 | - * @throws InvalidArgumentException |
|
2450 | - * @throws InvalidDataTypeException |
|
2451 | - * @throws InvalidInterfaceException |
|
2452 | - */ |
|
2453 | - public function total_trashed_events() |
|
2454 | - { |
|
2455 | - $where = [ |
|
2456 | - 'status' => 'trash', |
|
2457 | - ]; |
|
2458 | - return EEM_Event::instance()->count([$where, 'caps' => 'read_admin'], 'EVT_ID', true); |
|
2459 | - } |
|
2460 | - |
|
2461 | - |
|
2462 | - /** |
|
2463 | - * _default_event_settings |
|
2464 | - * This generates the Default Settings Tab |
|
2465 | - * |
|
2466 | - * @return void |
|
2467 | - * @throws DomainException |
|
2468 | - * @throws EE_Error |
|
2469 | - * @throws InvalidArgumentException |
|
2470 | - * @throws InvalidDataTypeException |
|
2471 | - * @throws InvalidInterfaceException |
|
2472 | - */ |
|
2473 | - protected function _default_event_settings() |
|
2474 | - { |
|
2475 | - $this->_set_add_edit_form_tags('update_default_event_settings'); |
|
2476 | - $this->_set_publish_post_box_vars(null, false, false, null, false); |
|
2477 | - $this->_template_args['admin_page_content'] = $this->_default_event_settings_form()->get_html(); |
|
2478 | - $this->display_admin_page_with_sidebar(); |
|
2479 | - } |
|
2480 | - |
|
2481 | - |
|
2482 | - /** |
|
2483 | - * Return the form for event settings. |
|
2484 | - * |
|
2485 | - * @return EE_Form_Section_Proper |
|
2486 | - * @throws EE_Error |
|
2487 | - */ |
|
2488 | - protected function _default_event_settings_form() |
|
2489 | - { |
|
2490 | - $registration_config = EE_Registry::instance()->CFG->registration; |
|
2491 | - $registration_stati_for_selection = EEM_Registration::reg_status_array( |
|
2492 | - // exclude |
|
2493 | - [ |
|
2494 | - EEM_Registration::status_id_cancelled, |
|
2495 | - EEM_Registration::status_id_declined, |
|
2496 | - EEM_Registration::status_id_incomplete, |
|
2497 | - EEM_Registration::status_id_wait_list, |
|
2498 | - ], |
|
2499 | - true |
|
2500 | - ); |
|
2501 | - return new EE_Form_Section_Proper( |
|
2502 | - [ |
|
2503 | - 'name' => 'update_default_event_settings', |
|
2504 | - 'html_id' => 'update_default_event_settings', |
|
2505 | - 'html_class' => 'form-table', |
|
2506 | - 'layout_strategy' => new EE_Admin_Two_Column_Layout(), |
|
2507 | - 'subsections' => apply_filters( |
|
2508 | - 'FHEE__Events_Admin_Page___default_event_settings_form__form_subsections', |
|
2509 | - [ |
|
2510 | - 'default_reg_status' => new EE_Select_Input( |
|
2511 | - $registration_stati_for_selection, |
|
2512 | - [ |
|
2513 | - 'default' => isset($registration_config->default_STS_ID) |
|
2514 | - && array_key_exists( |
|
2515 | - $registration_config->default_STS_ID, |
|
2516 | - $registration_stati_for_selection |
|
2517 | - ) |
|
2518 | - ? sanitize_text_field($registration_config->default_STS_ID) |
|
2519 | - : EEM_Registration::status_id_pending_payment, |
|
2520 | - 'html_label_text' => esc_html__('Default Registration Status', 'event_espresso') |
|
2521 | - . EEH_Template::get_help_tab_link( |
|
2522 | - 'default_settings_status_help_tab' |
|
2523 | - ), |
|
2524 | - 'html_help_text' => esc_html__( |
|
2525 | - 'This setting allows you to preselect what the default registration status setting is when creating an event. Note that changing this setting does NOT retroactively apply it to existing events.', |
|
2526 | - 'event_espresso' |
|
2527 | - ), |
|
2528 | - ] |
|
2529 | - ), |
|
2530 | - 'default_max_tickets' => new EE_Integer_Input( |
|
2531 | - [ |
|
2532 | - 'default' => isset($registration_config->default_maximum_number_of_tickets) |
|
2533 | - ? $registration_config->default_maximum_number_of_tickets |
|
2534 | - : EEM_Event::get_default_additional_limit(), |
|
2535 | - 'html_label_text' => esc_html__( |
|
2536 | - 'Default Maximum Tickets Allowed Per Order:', |
|
2537 | - 'event_espresso' |
|
2538 | - ) |
|
2539 | - . EEH_Template::get_help_tab_link( |
|
2540 | - 'default_maximum_tickets_help_tab"' |
|
2541 | - ), |
|
2542 | - 'html_help_text' => esc_html__( |
|
2543 | - 'This setting allows you to indicate what will be the default for the maximum number of tickets per order when creating new events.', |
|
2544 | - 'event_espresso' |
|
2545 | - ), |
|
2546 | - ] |
|
2547 | - ), |
|
2548 | - ] |
|
2549 | - ), |
|
2550 | - ] |
|
2551 | - ); |
|
2552 | - } |
|
2553 | - |
|
2554 | - |
|
2555 | - /** |
|
2556 | - * @return void |
|
2557 | - * @throws EE_Error |
|
2558 | - * @throws InvalidArgumentException |
|
2559 | - * @throws InvalidDataTypeException |
|
2560 | - * @throws InvalidInterfaceException |
|
2561 | - */ |
|
2562 | - protected function _update_default_event_settings() |
|
2563 | - { |
|
2564 | - $form = $this->_default_event_settings_form(); |
|
2565 | - if ($form->was_submitted()) { |
|
2566 | - $form->receive_form_submission(); |
|
2567 | - if ($form->is_valid()) { |
|
2568 | - $registration_config = EE_Registry::instance()->CFG->registration; |
|
2569 | - $valid_data = $form->valid_data(); |
|
2570 | - if (isset($valid_data['default_reg_status'])) { |
|
2571 | - $registration_config->default_STS_ID = $valid_data['default_reg_status']; |
|
2572 | - } |
|
2573 | - if (isset($valid_data['default_max_tickets'])) { |
|
2574 | - $registration_config->default_maximum_number_of_tickets = $valid_data['default_max_tickets']; |
|
2575 | - } |
|
2576 | - do_action( |
|
2577 | - 'AHEE__Events_Admin_Page___update_default_event_settings', |
|
2578 | - $valid_data, |
|
2579 | - EE_Registry::instance()->CFG, |
|
2580 | - $this |
|
2581 | - ); |
|
2582 | - // update because data was valid! |
|
2583 | - EE_Registry::instance()->CFG->update_espresso_config(); |
|
2584 | - EE_Error::overwrite_success(); |
|
2585 | - EE_Error::add_success( |
|
2586 | - __('Default Event Settings were updated', 'event_espresso') |
|
2587 | - ); |
|
2588 | - } |
|
2589 | - } |
|
2590 | - $this->_redirect_after_action(0, '', '', ['action' => 'default_event_settings'], true); |
|
2591 | - } |
|
2592 | - |
|
2593 | - |
|
2594 | - /************* Templates *************/ |
|
2595 | - protected function _template_settings() |
|
2596 | - { |
|
2597 | - $this->_admin_page_title = esc_html__('Template Settings (Preview)', 'event_espresso'); |
|
2598 | - $this->_template_args['preview_img'] = '<img src="' |
|
2599 | - . EVENTS_ASSETS_URL |
|
2600 | - . '/images/' |
|
2601 | - . 'caffeinated_template_features.jpg" alt="' |
|
2602 | - . esc_attr__('Template Settings Preview screenshot', 'event_espresso') |
|
2603 | - . '" />'; |
|
2604 | - $this->_template_args['preview_text'] = '<strong>' |
|
2605 | - . esc_html__( |
|
2606 | - 'Template Settings is a feature that is only available in the premium version of Event Espresso 4 which is available with a support license purchase on EventEspresso.com. Template Settings allow you to configure some of the appearance options for both the Event List and Event Details pages.', |
|
2607 | - 'event_espresso' |
|
2608 | - ) . '</strong>'; |
|
2609 | - $this->display_admin_caf_preview_page('template_settings_tab'); |
|
2610 | - } |
|
2611 | - |
|
2612 | - |
|
2613 | - /** Event Category Stuff **/ |
|
2614 | - /** |
|
2615 | - * set the _category property with the category object for the loaded page. |
|
2616 | - * |
|
2617 | - * @return void |
|
2618 | - */ |
|
2619 | - private function _set_category_object() |
|
2620 | - { |
|
2621 | - if (isset($this->_category->id) && ! empty($this->_category->id)) { |
|
2622 | - return; |
|
2623 | - } //already have the category object so get out. |
|
2624 | - // set default category object |
|
2625 | - $this->_set_empty_category_object(); |
|
2626 | - // only set if we've got an id |
|
2627 | - if (! isset($this->_req_data['EVT_CAT_ID'])) { |
|
2628 | - return; |
|
2629 | - } |
|
2630 | - $category_id = absint($this->_req_data['EVT_CAT_ID']); |
|
2631 | - $term = get_term($category_id, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY); |
|
2632 | - if (! empty($term)) { |
|
2633 | - $this->_category->category_name = $term->name; |
|
2634 | - $this->_category->category_identifier = $term->slug; |
|
2635 | - $this->_category->category_desc = $term->description; |
|
2636 | - $this->_category->id = $term->term_id; |
|
2637 | - $this->_category->parent = $term->parent; |
|
2638 | - } |
|
2639 | - } |
|
2640 | - |
|
2641 | - |
|
2642 | - /** |
|
2643 | - * Clears out category properties. |
|
2644 | - */ |
|
2645 | - private function _set_empty_category_object() |
|
2646 | - { |
|
2647 | - $this->_category = new stdClass(); |
|
2648 | - $this->_category->category_name = $this->_category->category_identifier = $this->_category->category_desc = ''; |
|
2649 | - $this->_category->id = $this->_category->parent = 0; |
|
2650 | - } |
|
2651 | - |
|
2652 | - |
|
2653 | - /** |
|
2654 | - * @throws DomainException |
|
2655 | - * @throws EE_Error |
|
2656 | - * @throws InvalidArgumentException |
|
2657 | - * @throws InvalidDataTypeException |
|
2658 | - * @throws InvalidInterfaceException |
|
2659 | - */ |
|
2660 | - protected function _category_list_table() |
|
2661 | - { |
|
2662 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
2663 | - $this->_search_btn_label = esc_html__('Categories', 'event_espresso'); |
|
2664 | - $this->_admin_page_title .= ' '; |
|
2665 | - $this->_admin_page_title .= $this->get_action_link_or_button( |
|
2666 | - 'add_category', |
|
2667 | - 'add_category', |
|
2668 | - [], |
|
2669 | - 'add-new-h2' |
|
2670 | - ); |
|
2671 | - $this->display_admin_list_table_page_with_sidebar(); |
|
2672 | - } |
|
2673 | - |
|
2674 | - |
|
2675 | - /** |
|
2676 | - * Output category details view. |
|
2677 | - * |
|
2678 | - * @param string $view |
|
2679 | - * @throws DomainException |
|
2680 | - * @throws EE_Error |
|
2681 | - * @throws InvalidArgumentException |
|
2682 | - * @throws InvalidDataTypeException |
|
2683 | - * @throws InvalidInterfaceException |
|
2684 | - */ |
|
2685 | - protected function _category_details($view) |
|
2686 | - { |
|
2687 | - // load formatter helper |
|
2688 | - // load field generator helper |
|
2689 | - $route = $view === 'edit' ? 'update_category' : 'insert_category'; |
|
2690 | - $this->_set_add_edit_form_tags($route); |
|
2691 | - $this->_set_category_object(); |
|
2692 | - $id = ! empty($this->_category->id) ? $this->_category->id : ''; |
|
2693 | - $delete_action = 'delete_category'; |
|
2694 | - // custom redirect |
|
2695 | - $redirect = EE_Admin_Page::add_query_args_and_nonce( |
|
2696 | - ['action' => 'category_list'], |
|
2697 | - $this->_admin_base_url |
|
2698 | - ); |
|
2699 | - $this->_set_publish_post_box_vars('EVT_CAT_ID', $id, $delete_action, $redirect); |
|
2700 | - // take care of contents |
|
2701 | - $this->_template_args['admin_page_content'] = $this->_category_details_content(); |
|
2702 | - $this->display_admin_page_with_sidebar(); |
|
2703 | - } |
|
2704 | - |
|
2705 | - |
|
2706 | - /** |
|
2707 | - * Output category details content. |
|
2708 | - * |
|
2709 | - * @throws DomainException |
|
2710 | - */ |
|
2711 | - protected function _category_details_content() |
|
2712 | - { |
|
2713 | - $editor_args['category_desc'] = [ |
|
2714 | - 'type' => 'wp_editor', |
|
2715 | - 'value' => EEH_Formatter::admin_format_content($this->_category->category_desc), |
|
2716 | - 'class' => 'my_editor_custom', |
|
2717 | - 'wpeditor_args' => ['media_buttons' => false], |
|
2718 | - ]; |
|
2719 | - $_wp_editor = $this->_generate_admin_form_fields($editor_args, 'array'); |
|
2720 | - $all_terms = get_terms( |
|
2721 | - [EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY], |
|
2722 | - ['hide_empty' => 0, 'exclude' => [$this->_category->id]] |
|
2723 | - ); |
|
2724 | - // setup category select for term parents. |
|
2725 | - $category_select_values[] = [ |
|
2726 | - 'text' => esc_html__('No Parent', 'event_espresso'), |
|
2727 | - 'id' => 0, |
|
2728 | - ]; |
|
2729 | - foreach ($all_terms as $term) { |
|
2730 | - $category_select_values[] = [ |
|
2731 | - 'text' => $term->name, |
|
2732 | - 'id' => $term->term_id, |
|
2733 | - ]; |
|
2734 | - } |
|
2735 | - $category_select = EEH_Form_Fields::select_input( |
|
2736 | - 'category_parent', |
|
2737 | - $category_select_values, |
|
2738 | - $this->_category->parent |
|
2739 | - ); |
|
2740 | - $template_args = [ |
|
2741 | - 'category' => $this->_category, |
|
2742 | - 'category_select' => $category_select, |
|
2743 | - 'unique_id_info_help_link' => $this->_get_help_tab_link('unique_id_info'), |
|
2744 | - 'category_desc_editor' => $_wp_editor['category_desc']['field'], |
|
2745 | - 'disable' => '', |
|
2746 | - 'disabled_message' => false, |
|
2747 | - ]; |
|
2748 | - $template = EVENTS_TEMPLATE_PATH . 'event_category_details.template.php'; |
|
2749 | - return EEH_Template::display_template($template, $template_args, true); |
|
2750 | - } |
|
2751 | - |
|
2752 | - |
|
2753 | - /** |
|
2754 | - * Handles deleting categories. |
|
2755 | - */ |
|
2756 | - protected function _delete_categories() |
|
2757 | - { |
|
2758 | - $cat_ids = isset($this->_req_data['EVT_CAT_ID']) ? (array) $this->_req_data['EVT_CAT_ID'] |
|
2759 | - : (array) $this->_req_data['category_id']; |
|
2760 | - foreach ($cat_ids as $cat_id) { |
|
2761 | - $this->_delete_category($cat_id); |
|
2762 | - } |
|
2763 | - // doesn't matter what page we're coming from... we're going to the same place after delete. |
|
2764 | - $query_args = [ |
|
2765 | - 'action' => 'category_list', |
|
2766 | - ]; |
|
2767 | - $this->_redirect_after_action(0, '', '', $query_args); |
|
2768 | - } |
|
2769 | - |
|
2770 | - |
|
2771 | - /** |
|
2772 | - * Handles deleting specific category. |
|
2773 | - * |
|
2774 | - * @param int $cat_id |
|
2775 | - */ |
|
2776 | - protected function _delete_category($cat_id) |
|
2777 | - { |
|
2778 | - $cat_id = absint($cat_id); |
|
2779 | - wp_delete_term($cat_id, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY); |
|
2780 | - } |
|
2781 | - |
|
2782 | - |
|
2783 | - /** |
|
2784 | - * Handles triggering the update or insertion of a new category. |
|
2785 | - * |
|
2786 | - * @param bool $new_category true means we're triggering the insert of a new category. |
|
2787 | - * @throws EE_Error |
|
2788 | - * @throws InvalidArgumentException |
|
2789 | - * @throws InvalidDataTypeException |
|
2790 | - * @throws InvalidInterfaceException |
|
2791 | - */ |
|
2792 | - protected function _insert_or_update_category($new_category) |
|
2793 | - { |
|
2794 | - $cat_id = $new_category ? $this->_insert_category() : $this->_insert_category(true); |
|
2795 | - $success = 0; // we already have a success message so lets not send another. |
|
2796 | - if ($cat_id) { |
|
2797 | - $query_args = [ |
|
2798 | - 'action' => 'edit_category', |
|
2799 | - 'EVT_CAT_ID' => $cat_id, |
|
2800 | - ]; |
|
2801 | - } else { |
|
2802 | - $query_args = ['action' => 'add_category']; |
|
2803 | - } |
|
2804 | - $this->_redirect_after_action($success, '', '', $query_args, true); |
|
2805 | - } |
|
2806 | - |
|
2807 | - |
|
2808 | - /** |
|
2809 | - * Inserts or updates category |
|
2810 | - * |
|
2811 | - * @param bool $update (true indicates we're updating a category). |
|
2812 | - * @return bool|mixed|string |
|
2813 | - */ |
|
2814 | - private function _insert_category($update = false) |
|
2815 | - { |
|
2816 | - $cat_id = $update ? $this->_req_data['EVT_CAT_ID'] : ''; |
|
2817 | - $category_name = isset($this->_req_data['category_name']) ? $this->_req_data['category_name'] : ''; |
|
2818 | - $category_desc = isset($this->_req_data['category_desc']) ? $this->_req_data['category_desc'] : ''; |
|
2819 | - $category_parent = isset($this->_req_data['category_parent']) ? $this->_req_data['category_parent'] : 0; |
|
2820 | - if (empty($category_name)) { |
|
2821 | - $msg = esc_html__('You must add a name for the category.', 'event_espresso'); |
|
2822 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2823 | - return false; |
|
2824 | - } |
|
2825 | - $term_args = [ |
|
2826 | - 'name' => $category_name, |
|
2827 | - 'description' => $category_desc, |
|
2828 | - 'parent' => $category_parent, |
|
2829 | - ]; |
|
2830 | - // was the category_identifier input disabled? |
|
2831 | - if (isset($this->_req_data['category_identifier'])) { |
|
2832 | - $term_args['slug'] = $this->_req_data['category_identifier']; |
|
2833 | - } |
|
2834 | - $insert_ids = $update |
|
2835 | - ? wp_update_term($cat_id, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY, $term_args) |
|
2836 | - : wp_insert_term($category_name, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY, $term_args); |
|
2837 | - if (! is_array($insert_ids)) { |
|
2838 | - $msg = esc_html__( |
|
2839 | - 'An error occurred and the category has not been saved to the database.', |
|
2840 | - 'event_espresso' |
|
2841 | - ); |
|
2842 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2843 | - } else { |
|
2844 | - $cat_id = $insert_ids['term_id']; |
|
2845 | - $msg = sprintf(esc_html__('The category %s was successfully saved', 'event_espresso'), $category_name); |
|
2846 | - EE_Error::add_success($msg); |
|
2847 | - } |
|
2848 | - return $cat_id; |
|
2849 | - } |
|
2850 | - |
|
2851 | - |
|
2852 | - /** |
|
2853 | - * Gets categories or count of categories matching the arguments in the request. |
|
2854 | - * |
|
2855 | - * @param int $per_page |
|
2856 | - * @param int $current_page |
|
2857 | - * @param bool $count |
|
2858 | - * @return EE_Base_Class[]|EE_Term_Taxonomy[]|int |
|
2859 | - * @throws EE_Error |
|
2860 | - * @throws InvalidArgumentException |
|
2861 | - * @throws InvalidDataTypeException |
|
2862 | - * @throws InvalidInterfaceException |
|
2863 | - */ |
|
2864 | - public function get_categories($per_page = 10, $current_page = 1, $count = false) |
|
2865 | - { |
|
2866 | - // testing term stuff |
|
2867 | - $orderby = isset($this->_req_data['orderby']) ? $this->_req_data['orderby'] : 'Term.term_id'; |
|
2868 | - $order = isset($this->_req_data['order']) ? $this->_req_data['order'] : 'DESC'; |
|
2869 | - $limit = ($current_page - 1) * $per_page; |
|
2870 | - $where = ['taxonomy' => EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY]; |
|
2871 | - if (isset($this->_req_data['s'])) { |
|
2872 | - $sstr = '%' . $this->_req_data['s'] . '%'; |
|
2873 | - $where['OR'] = [ |
|
2874 | - 'Term.name' => ['LIKE', $sstr], |
|
2875 | - 'description' => ['LIKE', $sstr], |
|
2876 | - ]; |
|
2877 | - } |
|
2878 | - $query_params = [ |
|
2879 | - $where, |
|
2880 | - 'order_by' => [$orderby => $order], |
|
2881 | - 'limit' => $limit . ',' . $per_page, |
|
2882 | - 'force_join' => ['Term'], |
|
2883 | - ]; |
|
2884 | - return $count |
|
2885 | - ? EEM_Term_Taxonomy::instance()->count($query_params, 'term_id') |
|
2886 | - : EEM_Term_Taxonomy::instance()->get_all($query_params); |
|
2887 | - } |
|
2888 | - |
|
2889 | - /* end category stuff */ |
|
2890 | - /**************/ |
|
2891 | - |
|
2892 | - |
|
2893 | - /** |
|
2894 | - * Callback for the `ee_save_timezone_setting` ajax action. |
|
2895 | - * |
|
2896 | - * @throws EE_Error |
|
2897 | - * @throws InvalidArgumentException |
|
2898 | - * @throws InvalidDataTypeException |
|
2899 | - * @throws InvalidInterfaceException |
|
2900 | - */ |
|
2901 | - public function save_timezonestring_setting() |
|
2902 | - { |
|
2903 | - $timezone_string = isset($this->_req_data['timezone_selected']) |
|
2904 | - ? $this->_req_data['timezone_selected'] |
|
2905 | - : ''; |
|
2906 | - if (empty($timezone_string) || ! EEH_DTT_Helper::validate_timezone($timezone_string, false)) { |
|
2907 | - EE_Error::add_error( |
|
2908 | - esc_html__('An invalid timezone string submitted.', 'event_espresso'), |
|
2909 | - __FILE__, |
|
2910 | - __FUNCTION__, |
|
2911 | - __LINE__ |
|
2912 | - ); |
|
2913 | - $this->_template_args['error'] = true; |
|
2914 | - $this->_return_json(); |
|
2915 | - } |
|
2916 | - |
|
2917 | - update_option('timezone_string', $timezone_string); |
|
2918 | - EE_Error::add_success( |
|
2919 | - esc_html__('Your timezone string was updated.', 'event_espresso') |
|
2920 | - ); |
|
2921 | - $this->_template_args['success'] = true; |
|
2922 | - $this->_return_json(true, ['action' => 'create_new']); |
|
2923 | - } |
|
1481 | + $dtts = $tkt_to_remove->get_many_related('Datetime'); |
|
1482 | + foreach ($dtts as $dtt) { |
|
1483 | + $tkt_to_remove->_remove_relation_to($dtt, 'Datetime'); |
|
1484 | + } |
|
1485 | + // need to do the same for prices (except these prices can also be deleted because again, |
|
1486 | + // tickets can only be trashed if they don't have any TKTs sold (otherwise they are just archived)) |
|
1487 | + $tkt_to_remove->delete_related_permanently('Price'); |
|
1488 | + // finally let's delete this ticket |
|
1489 | + // (which should not be blocked at this point b/c we've removed all our relationships) |
|
1490 | + $tkt_to_remove->delete_permanently(); |
|
1491 | + } |
|
1492 | + return [$saved_dtt, $saved_tickets]; |
|
1493 | + } |
|
1494 | + |
|
1495 | + |
|
1496 | + /** |
|
1497 | + * This attaches a list of given prices to a ticket. |
|
1498 | + * Note we dont' have to worry about ever removing relationships (or archiving prices) |
|
1499 | + * because if there is a change in price information on a ticket, a new ticket is created anyways |
|
1500 | + * so the archived ticket will retain the old price info and prices are automatically "archived" via the ticket. |
|
1501 | + * |
|
1502 | + * @param array $prices Array of prices from the form. |
|
1503 | + * @param EE_Ticket $ticket EE_Ticket object that prices are being attached to. |
|
1504 | + * @param bool $new_prices Whether attach existing incoming prices or create new ones. |
|
1505 | + * @return void |
|
1506 | + * @throws EE_Error |
|
1507 | + * @throws InvalidArgumentException |
|
1508 | + * @throws InvalidDataTypeException |
|
1509 | + * @throws InvalidInterfaceException |
|
1510 | + * @throws ReflectionException |
|
1511 | + */ |
|
1512 | + private function _add_prices_to_ticket($prices, EE_Ticket $ticket, $new_prices = false) |
|
1513 | + { |
|
1514 | + foreach ($prices as $row => $prc) { |
|
1515 | + $PRC_values = [ |
|
1516 | + 'PRC_ID' => ! empty($prc['PRC_ID']) ? $prc['PRC_ID'] : null, |
|
1517 | + 'PRT_ID' => ! empty($prc['PRT_ID']) ? $prc['PRT_ID'] : null, |
|
1518 | + 'PRC_amount' => ! empty($prc['PRC_amount']) ? $prc['PRC_amount'] : 0, |
|
1519 | + 'PRC_name' => ! empty($prc['PRC_name']) ? $prc['PRC_name'] : '', |
|
1520 | + 'PRC_desc' => ! empty($prc['PRC_desc']) ? $prc['PRC_desc'] : '', |
|
1521 | + 'PRC_is_default' => 0, // make sure prices are NOT set as default from this context |
|
1522 | + 'PRC_order' => $row, |
|
1523 | + ]; |
|
1524 | + if ($new_prices || empty($PRC_values['PRC_ID'])) { |
|
1525 | + $PRC_values['PRC_ID'] = 0; |
|
1526 | + $PRC = EE_Registry::instance()->load_class('Price', [$PRC_values], false, false); |
|
1527 | + } else { |
|
1528 | + $PRC = EEM_Price::instance()->get_one_by_ID($prc['PRC_ID']); |
|
1529 | + // update this price with new values |
|
1530 | + foreach ($PRC_values as $field => $newprc) { |
|
1531 | + $PRC->set($field, $newprc); |
|
1532 | + } |
|
1533 | + $PRC->save(); |
|
1534 | + } |
|
1535 | + $ticket->_add_relation_to($PRC, 'Price'); |
|
1536 | + } |
|
1537 | + } |
|
1538 | + |
|
1539 | + |
|
1540 | + /** |
|
1541 | + * Add in our autosave ajax handlers |
|
1542 | + * |
|
1543 | + */ |
|
1544 | + protected function _ee_autosave_create_new() |
|
1545 | + { |
|
1546 | + } |
|
1547 | + |
|
1548 | + |
|
1549 | + /** |
|
1550 | + * More autosave handlers. |
|
1551 | + */ |
|
1552 | + protected function _ee_autosave_edit() |
|
1553 | + { |
|
1554 | + } |
|
1555 | + |
|
1556 | + |
|
1557 | + /** |
|
1558 | + * _generate_publish_box_extra_content |
|
1559 | + * |
|
1560 | + * @throws DomainException |
|
1561 | + * @throws EE_Error |
|
1562 | + * @throws InvalidArgumentException |
|
1563 | + * @throws InvalidDataTypeException |
|
1564 | + * @throws InvalidInterfaceException |
|
1565 | + * @throws ReflectionException |
|
1566 | + */ |
|
1567 | + private function _generate_publish_box_extra_content() |
|
1568 | + { |
|
1569 | + // load formatter helper |
|
1570 | + // args for getting related registrations |
|
1571 | + $approved_query_args = [ |
|
1572 | + [ |
|
1573 | + 'REG_deleted' => 0, |
|
1574 | + 'STS_ID' => EEM_Registration::status_id_approved, |
|
1575 | + ], |
|
1576 | + ]; |
|
1577 | + $not_approved_query_args = [ |
|
1578 | + [ |
|
1579 | + 'REG_deleted' => 0, |
|
1580 | + 'STS_ID' => EEM_Registration::status_id_not_approved, |
|
1581 | + ], |
|
1582 | + ]; |
|
1583 | + $pending_payment_query_args = [ |
|
1584 | + [ |
|
1585 | + 'REG_deleted' => 0, |
|
1586 | + 'STS_ID' => EEM_Registration::status_id_pending_payment, |
|
1587 | + ], |
|
1588 | + ]; |
|
1589 | + // publish box |
|
1590 | + $publish_box_extra_args = [ |
|
1591 | + 'view_approved_reg_url' => add_query_arg( |
|
1592 | + [ |
|
1593 | + 'action' => 'default', |
|
1594 | + 'event_id' => $this->_cpt_model_obj->ID(), |
|
1595 | + '_reg_status' => EEM_Registration::status_id_approved, |
|
1596 | + ], |
|
1597 | + REG_ADMIN_URL |
|
1598 | + ), |
|
1599 | + 'view_not_approved_reg_url' => add_query_arg( |
|
1600 | + [ |
|
1601 | + 'action' => 'default', |
|
1602 | + 'event_id' => $this->_cpt_model_obj->ID(), |
|
1603 | + '_reg_status' => EEM_Registration::status_id_not_approved, |
|
1604 | + ], |
|
1605 | + REG_ADMIN_URL |
|
1606 | + ), |
|
1607 | + 'view_pending_payment_reg_url' => add_query_arg( |
|
1608 | + [ |
|
1609 | + 'action' => 'default', |
|
1610 | + 'event_id' => $this->_cpt_model_obj->ID(), |
|
1611 | + '_reg_status' => EEM_Registration::status_id_pending_payment, |
|
1612 | + ], |
|
1613 | + REG_ADMIN_URL |
|
1614 | + ), |
|
1615 | + 'approved_regs' => $this->_cpt_model_obj->count_related( |
|
1616 | + 'Registration', |
|
1617 | + $approved_query_args |
|
1618 | + ), |
|
1619 | + 'not_approved_regs' => $this->_cpt_model_obj->count_related( |
|
1620 | + 'Registration', |
|
1621 | + $not_approved_query_args |
|
1622 | + ), |
|
1623 | + 'pending_payment_regs' => $this->_cpt_model_obj->count_related( |
|
1624 | + 'Registration', |
|
1625 | + $pending_payment_query_args |
|
1626 | + ), |
|
1627 | + 'misc_pub_section_class' => apply_filters( |
|
1628 | + 'FHEE_Events_Admin_Page___generate_publish_box_extra_content__misc_pub_section_class', |
|
1629 | + 'misc-pub-section' |
|
1630 | + ), |
|
1631 | + ]; |
|
1632 | + ob_start(); |
|
1633 | + do_action( |
|
1634 | + 'AHEE__Events_Admin_Page___generate_publish_box_extra_content__event_editor_overview_add', |
|
1635 | + $this->_cpt_model_obj |
|
1636 | + ); |
|
1637 | + $publish_box_extra_args['event_editor_overview_add'] = ob_get_clean(); |
|
1638 | + // load template |
|
1639 | + EEH_Template::display_template( |
|
1640 | + EVENTS_TEMPLATE_PATH . 'event_publish_box_extras.template.php', |
|
1641 | + $publish_box_extra_args |
|
1642 | + ); |
|
1643 | + } |
|
1644 | + |
|
1645 | + |
|
1646 | + /** |
|
1647 | + * @return EE_Event |
|
1648 | + */ |
|
1649 | + public function get_event_object() |
|
1650 | + { |
|
1651 | + return $this->_cpt_model_obj; |
|
1652 | + } |
|
1653 | + |
|
1654 | + |
|
1655 | + |
|
1656 | + |
|
1657 | + /** METABOXES * */ |
|
1658 | + /** |
|
1659 | + * _register_event_editor_meta_boxes |
|
1660 | + * add all metaboxes related to the event_editor |
|
1661 | + * |
|
1662 | + * @return void |
|
1663 | + * @throws EE_Error |
|
1664 | + * @throws InvalidArgumentException |
|
1665 | + * @throws InvalidDataTypeException |
|
1666 | + * @throws InvalidInterfaceException |
|
1667 | + * @throws ReflectionException |
|
1668 | + */ |
|
1669 | + protected function _register_event_editor_meta_boxes() |
|
1670 | + { |
|
1671 | + $this->verify_cpt_object(); |
|
1672 | + $use_advanced_editor = $this->admin_config->useAdvancedEditor(); |
|
1673 | + /** @var FeatureFlags $flags */ |
|
1674 | + $flags = $this->loader->getShared('EventEspresso\core\domain\services\capabilities\FeatureFlags'); |
|
1675 | + // check if the new EDTR reg options meta box is being used, and if so, don't load the legacy version |
|
1676 | + if (! $use_advanced_editor || ! $flags->featureAllowed('use_reg_options_meta_box')) { |
|
1677 | + add_meta_box( |
|
1678 | + 'espresso_event_editor_event_options', |
|
1679 | + esc_html__('Event Registration Options', 'event_espresso'), |
|
1680 | + [$this, 'registration_options_meta_box'], |
|
1681 | + $this->page_slug, |
|
1682 | + 'side' |
|
1683 | + ); |
|
1684 | + } |
|
1685 | + if (! $use_advanced_editor) { |
|
1686 | + add_meta_box( |
|
1687 | + 'espresso_event_editor_tickets', |
|
1688 | + esc_html__('Event Datetime & Ticket', 'event_espresso'), |
|
1689 | + [$this, 'ticket_metabox'], |
|
1690 | + $this->page_slug, |
|
1691 | + 'normal', |
|
1692 | + 'high' |
|
1693 | + ); |
|
1694 | + } else { |
|
1695 | + if ($flags->featureAllowed('use_reg_options_meta_box')) { |
|
1696 | + add_action( |
|
1697 | + 'add_meta_boxes_espresso_events', |
|
1698 | + function () { |
|
1699 | + global $current_screen; |
|
1700 | + remove_meta_box('authordiv', $current_screen, 'normal'); |
|
1701 | + }, |
|
1702 | + 99 |
|
1703 | + ); |
|
1704 | + } |
|
1705 | + } |
|
1706 | + // NOTE: if you're looking for other metaboxes in here, |
|
1707 | + // where a metabox has a related management page in the admin |
|
1708 | + // you will find it setup in the related management page's "_Hooks" file. |
|
1709 | + // i.e. messages metabox is found in "espresso_events_Messages_Hooks.class.php". |
|
1710 | + } |
|
1711 | + |
|
1712 | + |
|
1713 | + /** |
|
1714 | + * @throws DomainException |
|
1715 | + * @throws EE_Error |
|
1716 | + * @throws InvalidArgumentException |
|
1717 | + * @throws InvalidDataTypeException |
|
1718 | + * @throws InvalidInterfaceException |
|
1719 | + * @throws ReflectionException |
|
1720 | + */ |
|
1721 | + public function ticket_metabox() |
|
1722 | + { |
|
1723 | + $existing_datetime_ids = $existing_ticket_ids = []; |
|
1724 | + // defaults for template args |
|
1725 | + $template_args = [ |
|
1726 | + 'existing_datetime_ids' => '', |
|
1727 | + 'event_datetime_help_link' => '', |
|
1728 | + 'ticket_options_help_link' => '', |
|
1729 | + 'time' => null, |
|
1730 | + 'ticket_rows' => '', |
|
1731 | + 'existing_ticket_ids' => '', |
|
1732 | + 'total_ticket_rows' => 1, |
|
1733 | + 'ticket_js_structure' => '', |
|
1734 | + 'trash_icon' => 'ee-lock-icon', |
|
1735 | + 'disabled' => '', |
|
1736 | + ]; |
|
1737 | + $event_id = $this->_cpt_model_obj instanceof EE_Event ? $this->_cpt_model_obj->ID() : 0; |
|
1738 | + $event_timezone_string = $this->_cpt_model_obj instanceof EE_Event |
|
1739 | + ? $this->_cpt_model_obj->timezone_string() |
|
1740 | + : ''; |
|
1741 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
1742 | + /** |
|
1743 | + * 1. Start with retrieving Datetimes |
|
1744 | + * 2. Fore each datetime get related tickets |
|
1745 | + * 3. For each ticket get related prices |
|
1746 | + */ |
|
1747 | + $times = $this->datetimeModel($event_timezone_string)->get_all_event_dates($event_id); |
|
1748 | + /** @type EE_Datetime $first_datetime */ |
|
1749 | + $first_datetime = reset($times); |
|
1750 | + // do we get related tickets? |
|
1751 | + if ( |
|
1752 | + $first_datetime instanceof EE_Datetime |
|
1753 | + && $first_datetime->ID() !== 0 |
|
1754 | + ) { |
|
1755 | + $existing_datetime_ids[] = $first_datetime->get('DTT_ID'); |
|
1756 | + $template_args['time'] = $first_datetime; |
|
1757 | + $related_tickets = $first_datetime->tickets( |
|
1758 | + [ |
|
1759 | + ['OR' => ['TKT_deleted' => 1, 'TKT_deleted*' => 0]], |
|
1760 | + 'default_where_conditions' => 'none', |
|
1761 | + ] |
|
1762 | + ); |
|
1763 | + if (! empty($related_tickets)) { |
|
1764 | + $template_args['total_ticket_rows'] = count($related_tickets); |
|
1765 | + $row = 0; |
|
1766 | + foreach ($related_tickets as $ticket) { |
|
1767 | + $existing_ticket_ids[] = $ticket->get('TKT_ID'); |
|
1768 | + $template_args['ticket_rows'] .= $this->_get_ticket_row($ticket, false, $row); |
|
1769 | + $row++; |
|
1770 | + } |
|
1771 | + } else { |
|
1772 | + $template_args['total_ticket_rows'] = 1; |
|
1773 | + /** @type EE_Ticket $ticket */ |
|
1774 | + $ticket = $this->ticketModel($event_timezone_string)->create_default_object(); |
|
1775 | + $template_args['ticket_rows'] .= $this->_get_ticket_row($ticket); |
|
1776 | + } |
|
1777 | + } else { |
|
1778 | + $template_args['time'] = $times[0]; |
|
1779 | + /** @type EE_Ticket $ticket */ |
|
1780 | + $ticket = $this->ticketModel($event_timezone_string)->get_all_default_tickets(); |
|
1781 | + $template_args['ticket_rows'] .= $this->_get_ticket_row($ticket[1]); |
|
1782 | + // NOTE: we're just sending the first default row |
|
1783 | + // (decaf can't manage default tickets so this should be sufficient); |
|
1784 | + } |
|
1785 | + $template_args['event_datetime_help_link'] = $this->_get_help_tab_link( |
|
1786 | + 'event_editor_event_datetimes_help_tab' |
|
1787 | + ); |
|
1788 | + $template_args['ticket_options_help_link'] = $this->_get_help_tab_link('ticket_options_info'); |
|
1789 | + $template_args['existing_datetime_ids'] = implode(',', $existing_datetime_ids); |
|
1790 | + $template_args['existing_ticket_ids'] = implode(',', $existing_ticket_ids); |
|
1791 | + $template_args['ticket_js_structure'] = $this->_get_ticket_row( |
|
1792 | + $this->ticketModel($event_timezone_string)->create_default_object(), |
|
1793 | + true |
|
1794 | + ); |
|
1795 | + $template = apply_filters( |
|
1796 | + 'FHEE__Events_Admin_Page__ticket_metabox__template', |
|
1797 | + EVENTS_TEMPLATE_PATH . 'event_tickets_metabox_main.template.php' |
|
1798 | + ); |
|
1799 | + EEH_Template::display_template($template, $template_args); |
|
1800 | + } |
|
1801 | + |
|
1802 | + |
|
1803 | + /** |
|
1804 | + * Setup an individual ticket form for the decaf event editor page |
|
1805 | + * |
|
1806 | + * @param EE_Ticket $ticket the ticket object |
|
1807 | + * @param boolean $skeleton whether we're generating a skeleton for js manipulation |
|
1808 | + * @param int $row |
|
1809 | + * @return string generated html for the ticket row. |
|
1810 | + * @throws DomainException |
|
1811 | + * @throws EE_Error |
|
1812 | + * @throws InvalidArgumentException |
|
1813 | + * @throws InvalidDataTypeException |
|
1814 | + * @throws InvalidInterfaceException |
|
1815 | + * @throws ReflectionException |
|
1816 | + */ |
|
1817 | + private function _get_ticket_row($ticket, $skeleton = false, $row = 0) |
|
1818 | + { |
|
1819 | + $template_args = [ |
|
1820 | + 'tkt_status_class' => ' tkt-status-' . $ticket->ticket_status(), |
|
1821 | + 'tkt_archive_class' => $ticket->ticket_status() === EE_Ticket::archived && ! $skeleton ? ' tkt-archived' |
|
1822 | + : '', |
|
1823 | + 'ticketrow' => $skeleton ? 'TICKETNUM' : $row, |
|
1824 | + 'TKT_ID' => $ticket->get('TKT_ID'), |
|
1825 | + 'TKT_name' => $ticket->get('TKT_name'), |
|
1826 | + 'TKT_start_date' => $skeleton ? '' : $ticket->get_date('TKT_start_date', 'Y-m-d h:i a'), |
|
1827 | + 'TKT_end_date' => $skeleton ? '' : $ticket->get_date('TKT_end_date', 'Y-m-d h:i a'), |
|
1828 | + 'TKT_is_default' => $ticket->get('TKT_is_default'), |
|
1829 | + 'TKT_qty' => $ticket->get_pretty('TKT_qty', 'input'), |
|
1830 | + 'edit_ticketrow_name' => $skeleton ? 'TICKETNAMEATTR' : 'edit_tickets', |
|
1831 | + 'TKT_sold' => $skeleton ? 0 : $ticket->get('TKT_sold'), |
|
1832 | + 'trash_icon' => ($skeleton || (! empty($ticket) && ! $ticket->get('TKT_deleted'))) |
|
1833 | + && (! empty($ticket) && $ticket->get('TKT_sold') === 0) |
|
1834 | + ? 'trash-icon dashicons dashicons-post-trash clickable' : 'ee-lock-icon', |
|
1835 | + 'disabled' => $skeleton || (! empty($ticket) && ! $ticket->get('TKT_deleted')) ? '' |
|
1836 | + : ' disabled=disabled', |
|
1837 | + ]; |
|
1838 | + $price = $ticket->ID() !== 0 |
|
1839 | + ? $ticket->get_first_related('Price', ['default_where_conditions' => 'none']) |
|
1840 | + : EEM_Price::instance()->create_default_object(); |
|
1841 | + $price_args = [ |
|
1842 | + 'price_currency_symbol' => EE_Registry::instance()->CFG->currency->sign, |
|
1843 | + 'PRC_amount' => $price->get('PRC_amount'), |
|
1844 | + 'PRT_ID' => $price->get('PRT_ID'), |
|
1845 | + 'PRC_ID' => $price->get('PRC_ID'), |
|
1846 | + 'PRC_is_default' => $price->get('PRC_is_default'), |
|
1847 | + ]; |
|
1848 | + // make sure we have default start and end dates if skeleton |
|
1849 | + // handle rows that should NOT be empty |
|
1850 | + if (empty($template_args['TKT_start_date'])) { |
|
1851 | + // if empty then the start date will be now. |
|
1852 | + $template_args['TKT_start_date'] = date('Y-m-d h:i a', current_time('timestamp')); |
|
1853 | + } |
|
1854 | + if (empty($template_args['TKT_end_date'])) { |
|
1855 | + // get the earliest datetime (if present); |
|
1856 | + $earliest_dtt = $this->_cpt_model_obj->ID() > 0 |
|
1857 | + ? $this->_cpt_model_obj->get_first_related( |
|
1858 | + 'Datetime', |
|
1859 | + ['order_by' => ['DTT_EVT_start' => 'ASC']] |
|
1860 | + ) |
|
1861 | + : null; |
|
1862 | + if (! empty($earliest_dtt)) { |
|
1863 | + $template_args['TKT_end_date'] = $earliest_dtt->get_datetime('DTT_EVT_start', 'Y-m-d', 'h:i a'); |
|
1864 | + } else { |
|
1865 | + $template_args['TKT_end_date'] = date( |
|
1866 | + 'Y-m-d h:i a', |
|
1867 | + mktime(0, 0, 0, date('m'), date('d') + 7, date('Y')) |
|
1868 | + ); |
|
1869 | + } |
|
1870 | + } |
|
1871 | + $template_args = array_merge($template_args, $price_args); |
|
1872 | + $template = apply_filters( |
|
1873 | + 'FHEE__Events_Admin_Page__get_ticket_row__template', |
|
1874 | + EVENTS_TEMPLATE_PATH . 'event_tickets_metabox_ticket_row.template.php', |
|
1875 | + $ticket |
|
1876 | + ); |
|
1877 | + return EEH_Template::display_template($template, $template_args, true); |
|
1878 | + } |
|
1879 | + |
|
1880 | + |
|
1881 | + /** |
|
1882 | + * @throws DomainException |
|
1883 | + * @throws EE_Error |
|
1884 | + */ |
|
1885 | + public function registration_options_meta_box() |
|
1886 | + { |
|
1887 | + $yes_no_values = [ |
|
1888 | + ['id' => true, 'text' => esc_html__('Yes', 'event_espresso')], |
|
1889 | + ['id' => false, 'text' => esc_html__('No', 'event_espresso')], |
|
1890 | + ]; |
|
1891 | + $default_reg_status_values = EEM_Registration::reg_status_array( |
|
1892 | + [ |
|
1893 | + EEM_Registration::status_id_cancelled, |
|
1894 | + EEM_Registration::status_id_declined, |
|
1895 | + EEM_Registration::status_id_incomplete, |
|
1896 | + ], |
|
1897 | + true |
|
1898 | + ); |
|
1899 | + // $template_args['is_active_select'] = EEH_Form_Fields::select_input('is_active', $yes_no_values, $this->_cpt_model_obj->is_active()); |
|
1900 | + $template_args['_event'] = $this->_cpt_model_obj; |
|
1901 | + $template_args['active_status'] = $this->_cpt_model_obj->pretty_active_status(false); |
|
1902 | + $template_args['additional_limit'] = $this->_cpt_model_obj->additional_limit(); |
|
1903 | + $template_args['default_registration_status'] = EEH_Form_Fields::select_input( |
|
1904 | + 'default_reg_status', |
|
1905 | + $default_reg_status_values, |
|
1906 | + $this->_cpt_model_obj->default_registration_status() |
|
1907 | + ); |
|
1908 | + $template_args['display_description'] = EEH_Form_Fields::select_input( |
|
1909 | + 'display_desc', |
|
1910 | + $yes_no_values, |
|
1911 | + $this->_cpt_model_obj->display_description() |
|
1912 | + ); |
|
1913 | + $template_args['display_ticket_selector'] = EEH_Form_Fields::select_input( |
|
1914 | + 'display_ticket_selector', |
|
1915 | + $yes_no_values, |
|
1916 | + $this->_cpt_model_obj->display_ticket_selector(), |
|
1917 | + '', |
|
1918 | + '', |
|
1919 | + false |
|
1920 | + ); |
|
1921 | + $template_args['additional_registration_options'] = apply_filters( |
|
1922 | + 'FHEE__Events_Admin_Page__registration_options_meta_box__additional_registration_options', |
|
1923 | + '', |
|
1924 | + $template_args, |
|
1925 | + $yes_no_values, |
|
1926 | + $default_reg_status_values |
|
1927 | + ); |
|
1928 | + EEH_Template::display_template( |
|
1929 | + EVENTS_TEMPLATE_PATH . 'event_registration_options.template.php', |
|
1930 | + $template_args |
|
1931 | + ); |
|
1932 | + } |
|
1933 | + |
|
1934 | + |
|
1935 | + /** |
|
1936 | + * _get_events() |
|
1937 | + * This method simply returns all the events (for the given _view and paging) |
|
1938 | + * |
|
1939 | + * @param int $per_page count of items per page (20 default); |
|
1940 | + * @param int $current_page what is the current page being viewed. |
|
1941 | + * @param bool $count if TRUE then we just return a count of ALL events matching the given _view. |
|
1942 | + * If FALSE then we return an array of event objects |
|
1943 | + * that match the given _view and paging parameters. |
|
1944 | + * @return array|int an array of event objects or count of how many events. |
|
1945 | + * @throws EE_Error |
|
1946 | + * @throws InvalidArgumentException |
|
1947 | + * @throws InvalidDataTypeException |
|
1948 | + * @throws InvalidInterfaceException |
|
1949 | + * @throws ReflectionException |
|
1950 | + * @throws Exception |
|
1951 | + * @throws Exception |
|
1952 | + * @throws Exception |
|
1953 | + */ |
|
1954 | + public function get_events($per_page = 10, $current_page = 1, $count = false) |
|
1955 | + { |
|
1956 | + $EEME = $this->eventModel(); |
|
1957 | + $offset = ($current_page - 1) * $per_page; |
|
1958 | + $limit = $count ? null : $offset . ',' . $per_page; |
|
1959 | + $orderby = isset($this->_req_data['orderby']) ? $this->_req_data['orderby'] : 'EVT_ID'; |
|
1960 | + $order = isset($this->_req_data['order']) ? $this->_req_data['order'] : 'DESC'; |
|
1961 | + if (isset($this->_req_data['month_range'])) { |
|
1962 | + $pieces = explode(' ', $this->_req_data['month_range'], 3); |
|
1963 | + // simulate the FIRST day of the month, that fixes issues for months like February |
|
1964 | + // where PHP doesn't know what to assume for date. |
|
1965 | + // @see https://events.codebasehq.com/projects/event-espresso/tickets/10437 |
|
1966 | + $month_r = ! empty($pieces[0]) ? date('m', EEH_DTT_Helper::first_of_month_timestamp($pieces[0])) : ''; |
|
1967 | + $year_r = ! empty($pieces[1]) ? $pieces[1] : ''; |
|
1968 | + } |
|
1969 | + $where = []; |
|
1970 | + $status = isset($this->_req_data['status']) ? $this->_req_data['status'] : null; |
|
1971 | + // determine what post_status our condition will have for the query. |
|
1972 | + switch ($status) { |
|
1973 | + case 'month': |
|
1974 | + case 'today': |
|
1975 | + case null: |
|
1976 | + case 'all': |
|
1977 | + break; |
|
1978 | + case 'draft': |
|
1979 | + $where['status'] = ['IN', ['draft', 'auto-draft']]; |
|
1980 | + break; |
|
1981 | + default: |
|
1982 | + $where['status'] = $status; |
|
1983 | + } |
|
1984 | + // categories? |
|
1985 | + $category = isset($this->_req_data['EVT_CAT']) && $this->_req_data['EVT_CAT'] > 0 |
|
1986 | + ? $this->_req_data['EVT_CAT'] : null; |
|
1987 | + if (! empty($category)) { |
|
1988 | + $where['Term_Taxonomy.taxonomy'] = EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY; |
|
1989 | + $where['Term_Taxonomy.term_id'] = $category; |
|
1990 | + } |
|
1991 | + // date where conditions |
|
1992 | + $start_formats = EEM_Datetime::instance()->get_formats_for('DTT_EVT_start'); |
|
1993 | + if (isset($this->_req_data['month_range']) && $this->_req_data['month_range'] !== '') { |
|
1994 | + $DateTime = new DateTime( |
|
1995 | + $year_r . '-' . $month_r . '-01 00:00:00', |
|
1996 | + new DateTimeZone('UTC') |
|
1997 | + ); |
|
1998 | + $start = $DateTime->getTimestamp(); |
|
1999 | + // set the datetime to be the end of the month |
|
2000 | + $DateTime->setDate( |
|
2001 | + $year_r, |
|
2002 | + $month_r, |
|
2003 | + $DateTime->format('t') |
|
2004 | + )->setTime(23, 59, 59); |
|
2005 | + $end = $DateTime->getTimestamp(); |
|
2006 | + $where['Datetime.DTT_EVT_start'] = ['BETWEEN', [$start, $end]]; |
|
2007 | + } elseif (isset($this->_req_data['status']) && $this->_req_data['status'] === 'today') { |
|
2008 | + $DateTime = |
|
2009 | + new DateTime('now', new DateTimeZone(EEM_Event::instance()->get_timezone())); |
|
2010 | + $start = $DateTime->setTime(0, 0, 0)->format(implode(' ', $start_formats)); |
|
2011 | + $end = $DateTime->setTime(23, 59, 59)->format(implode(' ', $start_formats)); |
|
2012 | + $where['Datetime.DTT_EVT_start'] = ['BETWEEN', [$start, $end]]; |
|
2013 | + } elseif (isset($this->_req_data['status']) && $this->_req_data['status'] === 'month') { |
|
2014 | + $now = date('Y-m-01'); |
|
2015 | + $DateTime = |
|
2016 | + new DateTime($now, new DateTimeZone(EEM_Event::instance()->get_timezone())); |
|
2017 | + $start = $DateTime->setTime(0, 0, 0)->format(implode(' ', $start_formats)); |
|
2018 | + $end = $DateTime->setDate(date('Y'), date('m'), $DateTime->format('t')) |
|
2019 | + ->setTime(23, 59, 59) |
|
2020 | + ->format(implode(' ', $start_formats)); |
|
2021 | + $where['Datetime.DTT_EVT_start'] = ['BETWEEN', [$start, $end]]; |
|
2022 | + } |
|
2023 | + if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_events', 'get_events')) { |
|
2024 | + $where['EVT_wp_user'] = get_current_user_id(); |
|
2025 | + } elseif ( |
|
2026 | + ! isset($where['status']) |
|
2027 | + && ! EE_Registry::instance()->CAP->current_user_can('ee_read_private_events', 'get_events') |
|
2028 | + ) { |
|
2029 | + $where['OR'] = [ |
|
2030 | + 'status*restrict_private' => ['!=', 'private'], |
|
2031 | + 'AND' => [ |
|
2032 | + 'status*inclusive' => ['=', 'private'], |
|
2033 | + 'EVT_wp_user' => get_current_user_id(), |
|
2034 | + ], |
|
2035 | + ]; |
|
2036 | + } |
|
2037 | + |
|
2038 | + if ( |
|
2039 | + isset($this->_req_data['EVT_wp_user']) |
|
2040 | + && (int) $this->_req_data['EVT_wp_user'] !== (int) get_current_user_id() |
|
2041 | + && EE_Registry::instance()->CAP->current_user_can('ee_read_others_events', 'get_events') |
|
2042 | + ) { |
|
2043 | + $where['EVT_wp_user'] = $this->_req_data['EVT_wp_user']; |
|
2044 | + } |
|
2045 | + // search query handling |
|
2046 | + if (isset($this->_req_data['s'])) { |
|
2047 | + $search_string = '%' . $this->_req_data['s'] . '%'; |
|
2048 | + $where['OR'] = [ |
|
2049 | + 'EVT_name' => ['LIKE', $search_string], |
|
2050 | + 'EVT_desc' => ['LIKE', $search_string], |
|
2051 | + 'EVT_short_desc' => ['LIKE', $search_string], |
|
2052 | + ]; |
|
2053 | + } |
|
2054 | + // filter events by venue. |
|
2055 | + if (isset($this->_req_data['venue']) && ! empty($this->_req_data['venue'])) { |
|
2056 | + $where['Venue.VNU_ID'] = absint($this->_req_data['venue']); |
|
2057 | + } |
|
2058 | + $where = apply_filters('FHEE__Events_Admin_Page__get_events__where', $where, $this->_req_data); |
|
2059 | + $query_params = apply_filters( |
|
2060 | + 'FHEE__Events_Admin_Page__get_events__query_params', |
|
2061 | + [ |
|
2062 | + $where, |
|
2063 | + 'limit' => $limit, |
|
2064 | + 'order_by' => $orderby, |
|
2065 | + 'order' => $order, |
|
2066 | + 'group_by' => 'EVT_ID', |
|
2067 | + ], |
|
2068 | + $this->_req_data |
|
2069 | + ); |
|
2070 | + |
|
2071 | + // let's first check if we have special requests coming in. |
|
2072 | + if (isset($this->_req_data['active_status'])) { |
|
2073 | + switch ($this->_req_data['active_status']) { |
|
2074 | + case 'upcoming': |
|
2075 | + return $EEME->get_upcoming_events($query_params, $count); |
|
2076 | + case 'expired': |
|
2077 | + return $EEME->get_expired_events($query_params, $count); |
|
2078 | + case 'active': |
|
2079 | + return $EEME->get_active_events($query_params, $count); |
|
2080 | + case 'inactive': |
|
2081 | + return $EEME->get_inactive_events($query_params, $count); |
|
2082 | + } |
|
2083 | + } |
|
2084 | + return $count |
|
2085 | + ? $EEME->count([$where], 'EVT_ID', true) |
|
2086 | + : $EEME->get_all($query_params); |
|
2087 | + } |
|
2088 | + |
|
2089 | + |
|
2090 | + /** |
|
2091 | + * handling for WordPress CPT actions (trash, restore, delete) |
|
2092 | + * |
|
2093 | + * @param string $post_id |
|
2094 | + * @throws EE_Error |
|
2095 | + * @throws InvalidArgumentException |
|
2096 | + * @throws InvalidDataTypeException |
|
2097 | + * @throws InvalidInterfaceException |
|
2098 | + * @throws ReflectionException |
|
2099 | + */ |
|
2100 | + public function trash_cpt_item($post_id) |
|
2101 | + { |
|
2102 | + $this->_req_data['EVT_ID'] = $post_id; |
|
2103 | + $this->_trash_or_restore_event('trash', false); |
|
2104 | + } |
|
2105 | + |
|
2106 | + |
|
2107 | + /** |
|
2108 | + * @param string $post_id |
|
2109 | + * @throws EE_Error |
|
2110 | + * @throws InvalidArgumentException |
|
2111 | + * @throws InvalidDataTypeException |
|
2112 | + * @throws InvalidInterfaceException |
|
2113 | + * @throws ReflectionException |
|
2114 | + */ |
|
2115 | + public function restore_cpt_item($post_id) |
|
2116 | + { |
|
2117 | + $this->_req_data['EVT_ID'] = $post_id; |
|
2118 | + $this->_trash_or_restore_event('draft', false); |
|
2119 | + } |
|
2120 | + |
|
2121 | + |
|
2122 | + /** |
|
2123 | + * @param string $post_id |
|
2124 | + * @throws EE_Error |
|
2125 | + * @throws InvalidArgumentException |
|
2126 | + * @throws InvalidDataTypeException |
|
2127 | + * @throws InvalidInterfaceException |
|
2128 | + * @throws ReflectionException |
|
2129 | + */ |
|
2130 | + public function delete_cpt_item($post_id) |
|
2131 | + { |
|
2132 | + throw new EE_Error( |
|
2133 | + esc_html__( |
|
2134 | + 'Please contact Event Espresso support with the details of the steps taken to produce this error.', |
|
2135 | + 'event_espresso' |
|
2136 | + ) |
|
2137 | + ); |
|
2138 | + $this->_req_data['EVT_ID'] = $post_id; |
|
2139 | + $this->_delete_event(); |
|
2140 | + } |
|
2141 | + |
|
2142 | + |
|
2143 | + /** |
|
2144 | + * _trash_or_restore_event |
|
2145 | + * |
|
2146 | + * @param string $event_status |
|
2147 | + * @param bool $redirect_after |
|
2148 | + * @throws EE_Error |
|
2149 | + * @throws InvalidArgumentException |
|
2150 | + * @throws InvalidDataTypeException |
|
2151 | + * @throws InvalidInterfaceException |
|
2152 | + * @throws ReflectionException |
|
2153 | + */ |
|
2154 | + protected function _trash_or_restore_event($event_status = 'trash', $redirect_after = true) |
|
2155 | + { |
|
2156 | + // determine the event id and set to array. |
|
2157 | + $EVT_ID = isset($this->_req_data['EVT_ID']) ? absint($this->_req_data['EVT_ID']) : false; |
|
2158 | + // loop thru events |
|
2159 | + if ($EVT_ID) { |
|
2160 | + // clean status |
|
2161 | + $event_status = sanitize_key($event_status); |
|
2162 | + // grab status |
|
2163 | + if (! empty($event_status)) { |
|
2164 | + $success = $this->_change_event_status($EVT_ID, $event_status); |
|
2165 | + } else { |
|
2166 | + $success = false; |
|
2167 | + $msg = esc_html__( |
|
2168 | + 'An error occurred. The event could not be moved to the trash because a valid event status was not not supplied.', |
|
2169 | + 'event_espresso' |
|
2170 | + ); |
|
2171 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2172 | + } |
|
2173 | + } else { |
|
2174 | + $success = false; |
|
2175 | + $msg = esc_html__( |
|
2176 | + 'An error occurred. The event could not be moved to the trash because a valid event ID was not not supplied.', |
|
2177 | + 'event_espresso' |
|
2178 | + ); |
|
2179 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2180 | + } |
|
2181 | + $action = $event_status === 'trash' ? 'moved to the trash' : 'restored from the trash'; |
|
2182 | + if ($redirect_after) { |
|
2183 | + $this->_redirect_after_action($success, 'Event', $action, ['action' => 'default']); |
|
2184 | + } |
|
2185 | + } |
|
2186 | + |
|
2187 | + |
|
2188 | + /** |
|
2189 | + * _trash_or_restore_events |
|
2190 | + * |
|
2191 | + * @param string $event_status |
|
2192 | + * @return void |
|
2193 | + * @throws EE_Error |
|
2194 | + * @throws InvalidArgumentException |
|
2195 | + * @throws InvalidDataTypeException |
|
2196 | + * @throws InvalidInterfaceException |
|
2197 | + * @throws ReflectionException |
|
2198 | + */ |
|
2199 | + protected function _trash_or_restore_events($event_status = 'trash') |
|
2200 | + { |
|
2201 | + // clean status |
|
2202 | + $event_status = sanitize_key($event_status); |
|
2203 | + // grab status |
|
2204 | + if (! empty($event_status)) { |
|
2205 | + $success = true; |
|
2206 | + // determine the event id and set to array. |
|
2207 | + $EVT_IDs = isset($this->_req_data['EVT_IDs']) ? (array) $this->_req_data['EVT_IDs'] : []; |
|
2208 | + // loop thru events |
|
2209 | + foreach ($EVT_IDs as $EVT_ID) { |
|
2210 | + if ($EVT_ID = absint($EVT_ID)) { |
|
2211 | + $results = $this->_change_event_status($EVT_ID, $event_status); |
|
2212 | + $success = $results !== false ? $success : false; |
|
2213 | + } else { |
|
2214 | + $msg = sprintf( |
|
2215 | + esc_html__( |
|
2216 | + 'An error occurred. Event #%d could not be moved to the trash because a valid event ID was not not supplied.', |
|
2217 | + 'event_espresso' |
|
2218 | + ), |
|
2219 | + $EVT_ID |
|
2220 | + ); |
|
2221 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2222 | + $success = false; |
|
2223 | + } |
|
2224 | + } |
|
2225 | + } else { |
|
2226 | + $success = false; |
|
2227 | + $msg = esc_html__( |
|
2228 | + 'An error occurred. The event could not be moved to the trash because a valid event status was not not supplied.', |
|
2229 | + 'event_espresso' |
|
2230 | + ); |
|
2231 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2232 | + } |
|
2233 | + // in order to force a pluralized result message we need to send back a success status greater than 1 |
|
2234 | + $success = $success ? 2 : false; |
|
2235 | + $action = $event_status === 'trash' ? 'moved to the trash' : 'restored from the trash'; |
|
2236 | + $this->_redirect_after_action($success, 'Events', $action, ['action' => 'default']); |
|
2237 | + } |
|
2238 | + |
|
2239 | + |
|
2240 | + /** |
|
2241 | + * _trash_or_restore_events |
|
2242 | + * |
|
2243 | + * @param int $EVT_ID |
|
2244 | + * @param string $event_status |
|
2245 | + * @return bool |
|
2246 | + * @throws EE_Error |
|
2247 | + * @throws InvalidArgumentException |
|
2248 | + * @throws InvalidDataTypeException |
|
2249 | + * @throws InvalidInterfaceException |
|
2250 | + * @throws ReflectionException |
|
2251 | + */ |
|
2252 | + private function _change_event_status($EVT_ID = 0, $event_status = '') |
|
2253 | + { |
|
2254 | + // grab event id |
|
2255 | + if (! $EVT_ID) { |
|
2256 | + $msg = esc_html__( |
|
2257 | + 'An error occurred. No Event ID or an invalid Event ID was received.', |
|
2258 | + 'event_espresso' |
|
2259 | + ); |
|
2260 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2261 | + return false; |
|
2262 | + } |
|
2263 | + $this->_cpt_model_obj = EEM_Event::instance()->get_one_by_ID($EVT_ID); |
|
2264 | + // clean status |
|
2265 | + $event_status = sanitize_key($event_status); |
|
2266 | + // grab status |
|
2267 | + if (empty($event_status)) { |
|
2268 | + $msg = esc_html__( |
|
2269 | + 'An error occurred. No Event Status or an invalid Event Status was received.', |
|
2270 | + 'event_espresso' |
|
2271 | + ); |
|
2272 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2273 | + return false; |
|
2274 | + } |
|
2275 | + // was event trashed or restored ? |
|
2276 | + switch ($event_status) { |
|
2277 | + case 'draft': |
|
2278 | + $action = 'restored from the trash'; |
|
2279 | + $hook = 'AHEE_event_restored_from_trash'; |
|
2280 | + break; |
|
2281 | + case 'trash': |
|
2282 | + $action = 'moved to the trash'; |
|
2283 | + $hook = 'AHEE_event_moved_to_trash'; |
|
2284 | + break; |
|
2285 | + default: |
|
2286 | + $action = 'updated'; |
|
2287 | + $hook = false; |
|
2288 | + } |
|
2289 | + // use class to change status |
|
2290 | + $this->_cpt_model_obj->set_status($event_status); |
|
2291 | + $success = $this->_cpt_model_obj->save(); |
|
2292 | + if ($success === false) { |
|
2293 | + $msg = sprintf(esc_html__('An error occurred. The event could not be %s.', 'event_espresso'), $action); |
|
2294 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2295 | + return false; |
|
2296 | + } |
|
2297 | + if ($hook) { |
|
2298 | + do_action($hook); |
|
2299 | + } |
|
2300 | + return true; |
|
2301 | + } |
|
2302 | + |
|
2303 | + |
|
2304 | + /** |
|
2305 | + * _delete_event |
|
2306 | + * |
|
2307 | + * @throws InvalidArgumentException |
|
2308 | + * @throws InvalidDataTypeException |
|
2309 | + * @throws InvalidInterfaceException |
|
2310 | + */ |
|
2311 | + protected function _delete_event() |
|
2312 | + { |
|
2313 | + $this->generateDeletionPreview(isset($this->_req_data['EVT_ID']) ? $this->_req_data['EVT_ID'] : []); |
|
2314 | + } |
|
2315 | + |
|
2316 | + |
|
2317 | + /** |
|
2318 | + * Gets the tree traversal batch persister. |
|
2319 | + * |
|
2320 | + * @return NodeGroupDao |
|
2321 | + * @throws InvalidArgumentException |
|
2322 | + * @throws InvalidDataTypeException |
|
2323 | + * @throws InvalidInterfaceException |
|
2324 | + * @since 4.10.12.p |
|
2325 | + */ |
|
2326 | + protected function getModelObjNodeGroupPersister() |
|
2327 | + { |
|
2328 | + if (! $this->model_obj_node_group_persister instanceof NodeGroupDao) { |
|
2329 | + $this->model_obj_node_group_persister = |
|
2330 | + $this->getLoader()->load('\EventEspresso\core\services\orm\tree_traversal\NodeGroupDao'); |
|
2331 | + } |
|
2332 | + return $this->model_obj_node_group_persister; |
|
2333 | + } |
|
2334 | + |
|
2335 | + |
|
2336 | + /** |
|
2337 | + * _delete_events |
|
2338 | + * |
|
2339 | + * @return void |
|
2340 | + * @throws InvalidArgumentException |
|
2341 | + * @throws InvalidDataTypeException |
|
2342 | + * @throws InvalidInterfaceException |
|
2343 | + */ |
|
2344 | + protected function _delete_events() |
|
2345 | + { |
|
2346 | + $this->generateDeletionPreview(isset($this->_req_data['EVT_IDs']) ? (array) $this->_req_data['EVT_IDs'] : []); |
|
2347 | + } |
|
2348 | + |
|
2349 | + |
|
2350 | + protected function generateDeletionPreview($event_ids) |
|
2351 | + { |
|
2352 | + $event_ids = (array) $event_ids; |
|
2353 | + // Set a code we can use to reference this deletion task in the batch jobs and preview page. |
|
2354 | + $deletion_job_code = $this->getModelObjNodeGroupPersister()->generateGroupCode(); |
|
2355 | + $return_url = EE_Admin_Page::add_query_args_and_nonce( |
|
2356 | + [ |
|
2357 | + 'action' => 'preview_deletion', |
|
2358 | + 'deletion_job_code' => $deletion_job_code, |
|
2359 | + ], |
|
2360 | + $this->_admin_base_url |
|
2361 | + ); |
|
2362 | + $event_ids = array_map( |
|
2363 | + 'intval', |
|
2364 | + $event_ids |
|
2365 | + ); |
|
2366 | + |
|
2367 | + EEH_URL::safeRedirectAndExit( |
|
2368 | + EE_Admin_Page::add_query_args_and_nonce( |
|
2369 | + [ |
|
2370 | + 'page' => 'espresso_batch', |
|
2371 | + 'batch' => EED_Batch::batch_job, |
|
2372 | + 'EVT_IDs' => $event_ids, |
|
2373 | + 'deletion_job_code' => $deletion_job_code, |
|
2374 | + 'job_handler' => urlencode('EventEspressoBatchRequest\JobHandlers\PreviewEventDeletion'), |
|
2375 | + 'return_url' => urlencode($return_url), |
|
2376 | + ], |
|
2377 | + admin_url() |
|
2378 | + ) |
|
2379 | + ); |
|
2380 | + } |
|
2381 | + |
|
2382 | + |
|
2383 | + /** |
|
2384 | + * Checks for a POST submission |
|
2385 | + * |
|
2386 | + * @since 4.10.12.p |
|
2387 | + */ |
|
2388 | + protected function confirmDeletion() |
|
2389 | + { |
|
2390 | + $deletion_redirect_logic = |
|
2391 | + $this->getLoader()->getShared('\EventEspresso\core\domain\services\admin\events\data\ConfirmDeletion'); |
|
2392 | + $deletion_redirect_logic->handle($this->get_request_data(), $this->admin_base_url()); |
|
2393 | + } |
|
2394 | + |
|
2395 | + |
|
2396 | + /** |
|
2397 | + * A page for users to preview what exactly will be deleted, and confirm they want to delete it. |
|
2398 | + * |
|
2399 | + * @throws EE_Error |
|
2400 | + * @since 4.10.12.p |
|
2401 | + */ |
|
2402 | + protected function previewDeletion() |
|
2403 | + { |
|
2404 | + $preview_deletion_logic = |
|
2405 | + $this->getLoader()->getShared('\EventEspresso\core\domain\services\admin\events\data\PreviewDeletion'); |
|
2406 | + $this->set_template_args($preview_deletion_logic->handle($this->get_request_data(), $this->admin_base_url())); |
|
2407 | + $this->display_admin_page_with_no_sidebar(); |
|
2408 | + } |
|
2409 | + |
|
2410 | + |
|
2411 | + /** |
|
2412 | + * get total number of events |
|
2413 | + * |
|
2414 | + * @return int |
|
2415 | + * @throws EE_Error |
|
2416 | + * @throws InvalidArgumentException |
|
2417 | + * @throws InvalidDataTypeException |
|
2418 | + * @throws InvalidInterfaceException |
|
2419 | + */ |
|
2420 | + public function total_events() |
|
2421 | + { |
|
2422 | + return EEM_Event::instance()->count(['caps' => 'read_admin'], 'EVT_ID', true); |
|
2423 | + } |
|
2424 | + |
|
2425 | + |
|
2426 | + /** |
|
2427 | + * get total number of draft events |
|
2428 | + * |
|
2429 | + * @return int |
|
2430 | + * @throws EE_Error |
|
2431 | + * @throws InvalidArgumentException |
|
2432 | + * @throws InvalidDataTypeException |
|
2433 | + * @throws InvalidInterfaceException |
|
2434 | + */ |
|
2435 | + public function total_events_draft() |
|
2436 | + { |
|
2437 | + $where = [ |
|
2438 | + 'status' => ['IN', ['draft', 'auto-draft']], |
|
2439 | + ]; |
|
2440 | + return EEM_Event::instance()->count([$where, 'caps' => 'read_admin'], 'EVT_ID', true); |
|
2441 | + } |
|
2442 | + |
|
2443 | + |
|
2444 | + /** |
|
2445 | + * get total number of trashed events |
|
2446 | + * |
|
2447 | + * @return int |
|
2448 | + * @throws EE_Error |
|
2449 | + * @throws InvalidArgumentException |
|
2450 | + * @throws InvalidDataTypeException |
|
2451 | + * @throws InvalidInterfaceException |
|
2452 | + */ |
|
2453 | + public function total_trashed_events() |
|
2454 | + { |
|
2455 | + $where = [ |
|
2456 | + 'status' => 'trash', |
|
2457 | + ]; |
|
2458 | + return EEM_Event::instance()->count([$where, 'caps' => 'read_admin'], 'EVT_ID', true); |
|
2459 | + } |
|
2460 | + |
|
2461 | + |
|
2462 | + /** |
|
2463 | + * _default_event_settings |
|
2464 | + * This generates the Default Settings Tab |
|
2465 | + * |
|
2466 | + * @return void |
|
2467 | + * @throws DomainException |
|
2468 | + * @throws EE_Error |
|
2469 | + * @throws InvalidArgumentException |
|
2470 | + * @throws InvalidDataTypeException |
|
2471 | + * @throws InvalidInterfaceException |
|
2472 | + */ |
|
2473 | + protected function _default_event_settings() |
|
2474 | + { |
|
2475 | + $this->_set_add_edit_form_tags('update_default_event_settings'); |
|
2476 | + $this->_set_publish_post_box_vars(null, false, false, null, false); |
|
2477 | + $this->_template_args['admin_page_content'] = $this->_default_event_settings_form()->get_html(); |
|
2478 | + $this->display_admin_page_with_sidebar(); |
|
2479 | + } |
|
2480 | + |
|
2481 | + |
|
2482 | + /** |
|
2483 | + * Return the form for event settings. |
|
2484 | + * |
|
2485 | + * @return EE_Form_Section_Proper |
|
2486 | + * @throws EE_Error |
|
2487 | + */ |
|
2488 | + protected function _default_event_settings_form() |
|
2489 | + { |
|
2490 | + $registration_config = EE_Registry::instance()->CFG->registration; |
|
2491 | + $registration_stati_for_selection = EEM_Registration::reg_status_array( |
|
2492 | + // exclude |
|
2493 | + [ |
|
2494 | + EEM_Registration::status_id_cancelled, |
|
2495 | + EEM_Registration::status_id_declined, |
|
2496 | + EEM_Registration::status_id_incomplete, |
|
2497 | + EEM_Registration::status_id_wait_list, |
|
2498 | + ], |
|
2499 | + true |
|
2500 | + ); |
|
2501 | + return new EE_Form_Section_Proper( |
|
2502 | + [ |
|
2503 | + 'name' => 'update_default_event_settings', |
|
2504 | + 'html_id' => 'update_default_event_settings', |
|
2505 | + 'html_class' => 'form-table', |
|
2506 | + 'layout_strategy' => new EE_Admin_Two_Column_Layout(), |
|
2507 | + 'subsections' => apply_filters( |
|
2508 | + 'FHEE__Events_Admin_Page___default_event_settings_form__form_subsections', |
|
2509 | + [ |
|
2510 | + 'default_reg_status' => new EE_Select_Input( |
|
2511 | + $registration_stati_for_selection, |
|
2512 | + [ |
|
2513 | + 'default' => isset($registration_config->default_STS_ID) |
|
2514 | + && array_key_exists( |
|
2515 | + $registration_config->default_STS_ID, |
|
2516 | + $registration_stati_for_selection |
|
2517 | + ) |
|
2518 | + ? sanitize_text_field($registration_config->default_STS_ID) |
|
2519 | + : EEM_Registration::status_id_pending_payment, |
|
2520 | + 'html_label_text' => esc_html__('Default Registration Status', 'event_espresso') |
|
2521 | + . EEH_Template::get_help_tab_link( |
|
2522 | + 'default_settings_status_help_tab' |
|
2523 | + ), |
|
2524 | + 'html_help_text' => esc_html__( |
|
2525 | + 'This setting allows you to preselect what the default registration status setting is when creating an event. Note that changing this setting does NOT retroactively apply it to existing events.', |
|
2526 | + 'event_espresso' |
|
2527 | + ), |
|
2528 | + ] |
|
2529 | + ), |
|
2530 | + 'default_max_tickets' => new EE_Integer_Input( |
|
2531 | + [ |
|
2532 | + 'default' => isset($registration_config->default_maximum_number_of_tickets) |
|
2533 | + ? $registration_config->default_maximum_number_of_tickets |
|
2534 | + : EEM_Event::get_default_additional_limit(), |
|
2535 | + 'html_label_text' => esc_html__( |
|
2536 | + 'Default Maximum Tickets Allowed Per Order:', |
|
2537 | + 'event_espresso' |
|
2538 | + ) |
|
2539 | + . EEH_Template::get_help_tab_link( |
|
2540 | + 'default_maximum_tickets_help_tab"' |
|
2541 | + ), |
|
2542 | + 'html_help_text' => esc_html__( |
|
2543 | + 'This setting allows you to indicate what will be the default for the maximum number of tickets per order when creating new events.', |
|
2544 | + 'event_espresso' |
|
2545 | + ), |
|
2546 | + ] |
|
2547 | + ), |
|
2548 | + ] |
|
2549 | + ), |
|
2550 | + ] |
|
2551 | + ); |
|
2552 | + } |
|
2553 | + |
|
2554 | + |
|
2555 | + /** |
|
2556 | + * @return void |
|
2557 | + * @throws EE_Error |
|
2558 | + * @throws InvalidArgumentException |
|
2559 | + * @throws InvalidDataTypeException |
|
2560 | + * @throws InvalidInterfaceException |
|
2561 | + */ |
|
2562 | + protected function _update_default_event_settings() |
|
2563 | + { |
|
2564 | + $form = $this->_default_event_settings_form(); |
|
2565 | + if ($form->was_submitted()) { |
|
2566 | + $form->receive_form_submission(); |
|
2567 | + if ($form->is_valid()) { |
|
2568 | + $registration_config = EE_Registry::instance()->CFG->registration; |
|
2569 | + $valid_data = $form->valid_data(); |
|
2570 | + if (isset($valid_data['default_reg_status'])) { |
|
2571 | + $registration_config->default_STS_ID = $valid_data['default_reg_status']; |
|
2572 | + } |
|
2573 | + if (isset($valid_data['default_max_tickets'])) { |
|
2574 | + $registration_config->default_maximum_number_of_tickets = $valid_data['default_max_tickets']; |
|
2575 | + } |
|
2576 | + do_action( |
|
2577 | + 'AHEE__Events_Admin_Page___update_default_event_settings', |
|
2578 | + $valid_data, |
|
2579 | + EE_Registry::instance()->CFG, |
|
2580 | + $this |
|
2581 | + ); |
|
2582 | + // update because data was valid! |
|
2583 | + EE_Registry::instance()->CFG->update_espresso_config(); |
|
2584 | + EE_Error::overwrite_success(); |
|
2585 | + EE_Error::add_success( |
|
2586 | + __('Default Event Settings were updated', 'event_espresso') |
|
2587 | + ); |
|
2588 | + } |
|
2589 | + } |
|
2590 | + $this->_redirect_after_action(0, '', '', ['action' => 'default_event_settings'], true); |
|
2591 | + } |
|
2592 | + |
|
2593 | + |
|
2594 | + /************* Templates *************/ |
|
2595 | + protected function _template_settings() |
|
2596 | + { |
|
2597 | + $this->_admin_page_title = esc_html__('Template Settings (Preview)', 'event_espresso'); |
|
2598 | + $this->_template_args['preview_img'] = '<img src="' |
|
2599 | + . EVENTS_ASSETS_URL |
|
2600 | + . '/images/' |
|
2601 | + . 'caffeinated_template_features.jpg" alt="' |
|
2602 | + . esc_attr__('Template Settings Preview screenshot', 'event_espresso') |
|
2603 | + . '" />'; |
|
2604 | + $this->_template_args['preview_text'] = '<strong>' |
|
2605 | + . esc_html__( |
|
2606 | + 'Template Settings is a feature that is only available in the premium version of Event Espresso 4 which is available with a support license purchase on EventEspresso.com. Template Settings allow you to configure some of the appearance options for both the Event List and Event Details pages.', |
|
2607 | + 'event_espresso' |
|
2608 | + ) . '</strong>'; |
|
2609 | + $this->display_admin_caf_preview_page('template_settings_tab'); |
|
2610 | + } |
|
2611 | + |
|
2612 | + |
|
2613 | + /** Event Category Stuff **/ |
|
2614 | + /** |
|
2615 | + * set the _category property with the category object for the loaded page. |
|
2616 | + * |
|
2617 | + * @return void |
|
2618 | + */ |
|
2619 | + private function _set_category_object() |
|
2620 | + { |
|
2621 | + if (isset($this->_category->id) && ! empty($this->_category->id)) { |
|
2622 | + return; |
|
2623 | + } //already have the category object so get out. |
|
2624 | + // set default category object |
|
2625 | + $this->_set_empty_category_object(); |
|
2626 | + // only set if we've got an id |
|
2627 | + if (! isset($this->_req_data['EVT_CAT_ID'])) { |
|
2628 | + return; |
|
2629 | + } |
|
2630 | + $category_id = absint($this->_req_data['EVT_CAT_ID']); |
|
2631 | + $term = get_term($category_id, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY); |
|
2632 | + if (! empty($term)) { |
|
2633 | + $this->_category->category_name = $term->name; |
|
2634 | + $this->_category->category_identifier = $term->slug; |
|
2635 | + $this->_category->category_desc = $term->description; |
|
2636 | + $this->_category->id = $term->term_id; |
|
2637 | + $this->_category->parent = $term->parent; |
|
2638 | + } |
|
2639 | + } |
|
2640 | + |
|
2641 | + |
|
2642 | + /** |
|
2643 | + * Clears out category properties. |
|
2644 | + */ |
|
2645 | + private function _set_empty_category_object() |
|
2646 | + { |
|
2647 | + $this->_category = new stdClass(); |
|
2648 | + $this->_category->category_name = $this->_category->category_identifier = $this->_category->category_desc = ''; |
|
2649 | + $this->_category->id = $this->_category->parent = 0; |
|
2650 | + } |
|
2651 | + |
|
2652 | + |
|
2653 | + /** |
|
2654 | + * @throws DomainException |
|
2655 | + * @throws EE_Error |
|
2656 | + * @throws InvalidArgumentException |
|
2657 | + * @throws InvalidDataTypeException |
|
2658 | + * @throws InvalidInterfaceException |
|
2659 | + */ |
|
2660 | + protected function _category_list_table() |
|
2661 | + { |
|
2662 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
2663 | + $this->_search_btn_label = esc_html__('Categories', 'event_espresso'); |
|
2664 | + $this->_admin_page_title .= ' '; |
|
2665 | + $this->_admin_page_title .= $this->get_action_link_or_button( |
|
2666 | + 'add_category', |
|
2667 | + 'add_category', |
|
2668 | + [], |
|
2669 | + 'add-new-h2' |
|
2670 | + ); |
|
2671 | + $this->display_admin_list_table_page_with_sidebar(); |
|
2672 | + } |
|
2673 | + |
|
2674 | + |
|
2675 | + /** |
|
2676 | + * Output category details view. |
|
2677 | + * |
|
2678 | + * @param string $view |
|
2679 | + * @throws DomainException |
|
2680 | + * @throws EE_Error |
|
2681 | + * @throws InvalidArgumentException |
|
2682 | + * @throws InvalidDataTypeException |
|
2683 | + * @throws InvalidInterfaceException |
|
2684 | + */ |
|
2685 | + protected function _category_details($view) |
|
2686 | + { |
|
2687 | + // load formatter helper |
|
2688 | + // load field generator helper |
|
2689 | + $route = $view === 'edit' ? 'update_category' : 'insert_category'; |
|
2690 | + $this->_set_add_edit_form_tags($route); |
|
2691 | + $this->_set_category_object(); |
|
2692 | + $id = ! empty($this->_category->id) ? $this->_category->id : ''; |
|
2693 | + $delete_action = 'delete_category'; |
|
2694 | + // custom redirect |
|
2695 | + $redirect = EE_Admin_Page::add_query_args_and_nonce( |
|
2696 | + ['action' => 'category_list'], |
|
2697 | + $this->_admin_base_url |
|
2698 | + ); |
|
2699 | + $this->_set_publish_post_box_vars('EVT_CAT_ID', $id, $delete_action, $redirect); |
|
2700 | + // take care of contents |
|
2701 | + $this->_template_args['admin_page_content'] = $this->_category_details_content(); |
|
2702 | + $this->display_admin_page_with_sidebar(); |
|
2703 | + } |
|
2704 | + |
|
2705 | + |
|
2706 | + /** |
|
2707 | + * Output category details content. |
|
2708 | + * |
|
2709 | + * @throws DomainException |
|
2710 | + */ |
|
2711 | + protected function _category_details_content() |
|
2712 | + { |
|
2713 | + $editor_args['category_desc'] = [ |
|
2714 | + 'type' => 'wp_editor', |
|
2715 | + 'value' => EEH_Formatter::admin_format_content($this->_category->category_desc), |
|
2716 | + 'class' => 'my_editor_custom', |
|
2717 | + 'wpeditor_args' => ['media_buttons' => false], |
|
2718 | + ]; |
|
2719 | + $_wp_editor = $this->_generate_admin_form_fields($editor_args, 'array'); |
|
2720 | + $all_terms = get_terms( |
|
2721 | + [EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY], |
|
2722 | + ['hide_empty' => 0, 'exclude' => [$this->_category->id]] |
|
2723 | + ); |
|
2724 | + // setup category select for term parents. |
|
2725 | + $category_select_values[] = [ |
|
2726 | + 'text' => esc_html__('No Parent', 'event_espresso'), |
|
2727 | + 'id' => 0, |
|
2728 | + ]; |
|
2729 | + foreach ($all_terms as $term) { |
|
2730 | + $category_select_values[] = [ |
|
2731 | + 'text' => $term->name, |
|
2732 | + 'id' => $term->term_id, |
|
2733 | + ]; |
|
2734 | + } |
|
2735 | + $category_select = EEH_Form_Fields::select_input( |
|
2736 | + 'category_parent', |
|
2737 | + $category_select_values, |
|
2738 | + $this->_category->parent |
|
2739 | + ); |
|
2740 | + $template_args = [ |
|
2741 | + 'category' => $this->_category, |
|
2742 | + 'category_select' => $category_select, |
|
2743 | + 'unique_id_info_help_link' => $this->_get_help_tab_link('unique_id_info'), |
|
2744 | + 'category_desc_editor' => $_wp_editor['category_desc']['field'], |
|
2745 | + 'disable' => '', |
|
2746 | + 'disabled_message' => false, |
|
2747 | + ]; |
|
2748 | + $template = EVENTS_TEMPLATE_PATH . 'event_category_details.template.php'; |
|
2749 | + return EEH_Template::display_template($template, $template_args, true); |
|
2750 | + } |
|
2751 | + |
|
2752 | + |
|
2753 | + /** |
|
2754 | + * Handles deleting categories. |
|
2755 | + */ |
|
2756 | + protected function _delete_categories() |
|
2757 | + { |
|
2758 | + $cat_ids = isset($this->_req_data['EVT_CAT_ID']) ? (array) $this->_req_data['EVT_CAT_ID'] |
|
2759 | + : (array) $this->_req_data['category_id']; |
|
2760 | + foreach ($cat_ids as $cat_id) { |
|
2761 | + $this->_delete_category($cat_id); |
|
2762 | + } |
|
2763 | + // doesn't matter what page we're coming from... we're going to the same place after delete. |
|
2764 | + $query_args = [ |
|
2765 | + 'action' => 'category_list', |
|
2766 | + ]; |
|
2767 | + $this->_redirect_after_action(0, '', '', $query_args); |
|
2768 | + } |
|
2769 | + |
|
2770 | + |
|
2771 | + /** |
|
2772 | + * Handles deleting specific category. |
|
2773 | + * |
|
2774 | + * @param int $cat_id |
|
2775 | + */ |
|
2776 | + protected function _delete_category($cat_id) |
|
2777 | + { |
|
2778 | + $cat_id = absint($cat_id); |
|
2779 | + wp_delete_term($cat_id, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY); |
|
2780 | + } |
|
2781 | + |
|
2782 | + |
|
2783 | + /** |
|
2784 | + * Handles triggering the update or insertion of a new category. |
|
2785 | + * |
|
2786 | + * @param bool $new_category true means we're triggering the insert of a new category. |
|
2787 | + * @throws EE_Error |
|
2788 | + * @throws InvalidArgumentException |
|
2789 | + * @throws InvalidDataTypeException |
|
2790 | + * @throws InvalidInterfaceException |
|
2791 | + */ |
|
2792 | + protected function _insert_or_update_category($new_category) |
|
2793 | + { |
|
2794 | + $cat_id = $new_category ? $this->_insert_category() : $this->_insert_category(true); |
|
2795 | + $success = 0; // we already have a success message so lets not send another. |
|
2796 | + if ($cat_id) { |
|
2797 | + $query_args = [ |
|
2798 | + 'action' => 'edit_category', |
|
2799 | + 'EVT_CAT_ID' => $cat_id, |
|
2800 | + ]; |
|
2801 | + } else { |
|
2802 | + $query_args = ['action' => 'add_category']; |
|
2803 | + } |
|
2804 | + $this->_redirect_after_action($success, '', '', $query_args, true); |
|
2805 | + } |
|
2806 | + |
|
2807 | + |
|
2808 | + /** |
|
2809 | + * Inserts or updates category |
|
2810 | + * |
|
2811 | + * @param bool $update (true indicates we're updating a category). |
|
2812 | + * @return bool|mixed|string |
|
2813 | + */ |
|
2814 | + private function _insert_category($update = false) |
|
2815 | + { |
|
2816 | + $cat_id = $update ? $this->_req_data['EVT_CAT_ID'] : ''; |
|
2817 | + $category_name = isset($this->_req_data['category_name']) ? $this->_req_data['category_name'] : ''; |
|
2818 | + $category_desc = isset($this->_req_data['category_desc']) ? $this->_req_data['category_desc'] : ''; |
|
2819 | + $category_parent = isset($this->_req_data['category_parent']) ? $this->_req_data['category_parent'] : 0; |
|
2820 | + if (empty($category_name)) { |
|
2821 | + $msg = esc_html__('You must add a name for the category.', 'event_espresso'); |
|
2822 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2823 | + return false; |
|
2824 | + } |
|
2825 | + $term_args = [ |
|
2826 | + 'name' => $category_name, |
|
2827 | + 'description' => $category_desc, |
|
2828 | + 'parent' => $category_parent, |
|
2829 | + ]; |
|
2830 | + // was the category_identifier input disabled? |
|
2831 | + if (isset($this->_req_data['category_identifier'])) { |
|
2832 | + $term_args['slug'] = $this->_req_data['category_identifier']; |
|
2833 | + } |
|
2834 | + $insert_ids = $update |
|
2835 | + ? wp_update_term($cat_id, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY, $term_args) |
|
2836 | + : wp_insert_term($category_name, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY, $term_args); |
|
2837 | + if (! is_array($insert_ids)) { |
|
2838 | + $msg = esc_html__( |
|
2839 | + 'An error occurred and the category has not been saved to the database.', |
|
2840 | + 'event_espresso' |
|
2841 | + ); |
|
2842 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
2843 | + } else { |
|
2844 | + $cat_id = $insert_ids['term_id']; |
|
2845 | + $msg = sprintf(esc_html__('The category %s was successfully saved', 'event_espresso'), $category_name); |
|
2846 | + EE_Error::add_success($msg); |
|
2847 | + } |
|
2848 | + return $cat_id; |
|
2849 | + } |
|
2850 | + |
|
2851 | + |
|
2852 | + /** |
|
2853 | + * Gets categories or count of categories matching the arguments in the request. |
|
2854 | + * |
|
2855 | + * @param int $per_page |
|
2856 | + * @param int $current_page |
|
2857 | + * @param bool $count |
|
2858 | + * @return EE_Base_Class[]|EE_Term_Taxonomy[]|int |
|
2859 | + * @throws EE_Error |
|
2860 | + * @throws InvalidArgumentException |
|
2861 | + * @throws InvalidDataTypeException |
|
2862 | + * @throws InvalidInterfaceException |
|
2863 | + */ |
|
2864 | + public function get_categories($per_page = 10, $current_page = 1, $count = false) |
|
2865 | + { |
|
2866 | + // testing term stuff |
|
2867 | + $orderby = isset($this->_req_data['orderby']) ? $this->_req_data['orderby'] : 'Term.term_id'; |
|
2868 | + $order = isset($this->_req_data['order']) ? $this->_req_data['order'] : 'DESC'; |
|
2869 | + $limit = ($current_page - 1) * $per_page; |
|
2870 | + $where = ['taxonomy' => EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY]; |
|
2871 | + if (isset($this->_req_data['s'])) { |
|
2872 | + $sstr = '%' . $this->_req_data['s'] . '%'; |
|
2873 | + $where['OR'] = [ |
|
2874 | + 'Term.name' => ['LIKE', $sstr], |
|
2875 | + 'description' => ['LIKE', $sstr], |
|
2876 | + ]; |
|
2877 | + } |
|
2878 | + $query_params = [ |
|
2879 | + $where, |
|
2880 | + 'order_by' => [$orderby => $order], |
|
2881 | + 'limit' => $limit . ',' . $per_page, |
|
2882 | + 'force_join' => ['Term'], |
|
2883 | + ]; |
|
2884 | + return $count |
|
2885 | + ? EEM_Term_Taxonomy::instance()->count($query_params, 'term_id') |
|
2886 | + : EEM_Term_Taxonomy::instance()->get_all($query_params); |
|
2887 | + } |
|
2888 | + |
|
2889 | + /* end category stuff */ |
|
2890 | + /**************/ |
|
2891 | + |
|
2892 | + |
|
2893 | + /** |
|
2894 | + * Callback for the `ee_save_timezone_setting` ajax action. |
|
2895 | + * |
|
2896 | + * @throws EE_Error |
|
2897 | + * @throws InvalidArgumentException |
|
2898 | + * @throws InvalidDataTypeException |
|
2899 | + * @throws InvalidInterfaceException |
|
2900 | + */ |
|
2901 | + public function save_timezonestring_setting() |
|
2902 | + { |
|
2903 | + $timezone_string = isset($this->_req_data['timezone_selected']) |
|
2904 | + ? $this->_req_data['timezone_selected'] |
|
2905 | + : ''; |
|
2906 | + if (empty($timezone_string) || ! EEH_DTT_Helper::validate_timezone($timezone_string, false)) { |
|
2907 | + EE_Error::add_error( |
|
2908 | + esc_html__('An invalid timezone string submitted.', 'event_espresso'), |
|
2909 | + __FILE__, |
|
2910 | + __FUNCTION__, |
|
2911 | + __LINE__ |
|
2912 | + ); |
|
2913 | + $this->_template_args['error'] = true; |
|
2914 | + $this->_return_json(); |
|
2915 | + } |
|
2916 | + |
|
2917 | + update_option('timezone_string', $timezone_string); |
|
2918 | + EE_Error::add_success( |
|
2919 | + esc_html__('Your timezone string was updated.', 'event_espresso') |
|
2920 | + ); |
|
2921 | + $this->_template_args['success'] = true; |
|
2922 | + $this->_return_json(true, ['action' => 'create_new']); |
|
2923 | + } |
|
2924 | 2924 | } |