@@ -133,9 +133,9 @@ discard block |
||
133 | 133 | |
134 | 134 | /** |
135 | 135 | * @param $other_table |
136 | - * @param $other_table_alias |
|
136 | + * @param string $other_table_alias |
|
137 | 137 | * @param $other_table_column |
138 | - * @param $this_table_alias |
|
138 | + * @param string $this_table_alias |
|
139 | 139 | * @param $this_table_join_column |
140 | 140 | * @param string $extra_join_sql |
141 | 141 | * @return string |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | * Alters the $query_params to disable default where conditions, unless otherwise specified |
190 | 190 | * |
191 | 191 | * @param string $query_params |
192 | - * @return array |
|
192 | + * @return string |
|
193 | 193 | */ |
194 | 194 | protected function _disable_default_where_conditions_on_query_param($query_params) |
195 | 195 | { |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
207 | 207 | * model objects will only be soft-deleted. |
208 | 208 | * |
209 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
209 | + * @param EE_Base_Class|null $model_object_or_id |
|
210 | 210 | * @param array $query_params |
211 | 211 | * @return int of how many related models got deleted |
212 | 212 | * @throws \EE_Error |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
238 | 238 | * model objects will only be soft-deleted. |
239 | 239 | * |
240 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
240 | + * @param EE_Base_Class|null $model_object_or_id |
|
241 | 241 | * @param array $query_params |
242 | 242 | * @return int of how many related models got deleted |
243 | 243 | * @throws \EE_Error |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | $this_table_join_column, |
149 | 149 | $extra_join_sql = '' |
150 | 150 | ) { |
151 | - return " LEFT JOIN " . $other_table . " AS " . $other_table_alias . " ON " . $other_table_alias . "." . $other_table_column . "=" . $this_table_alias . "." . $this_table_join_column . ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
151 | + return " LEFT JOIN ".$other_table." AS ".$other_table_alias." ON ".$other_table_alias.".".$other_table_column."=".$this_table_alias.".".$this_table_join_column.($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
152 | 152 | } |
153 | 153 | |
154 | 154 | |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | */ |
194 | 194 | protected function _disable_default_where_conditions_on_query_param($query_params) |
195 | 195 | { |
196 | - if (! isset($query_params['default_where_conditions'])) { |
|
196 | + if ( ! isset($query_params['default_where_conditions'])) { |
|
197 | 197 | $query_params['default_where_conditions'] = 'none'; |
198 | 198 | } |
199 | 199 | return $query_params; |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object, |
222 | 222 | $model_object_or_id); |
223 | 223 | /* @var $model_object_or_id EE_Base_Class */ |
224 | - if (! $delete_is_blocked) { |
|
224 | + if ( ! $delete_is_blocked) { |
|
225 | 225 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
226 | 226 | $related_model_object->delete(); |
227 | 227 | $deleted_count++; |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
256 | 256 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
257 | 257 | $deleted_count++; |
258 | - if (! $delete_is_blocked) { |
|
258 | + if ( ! $delete_is_blocked) { |
|
259 | 259 | $related_model_object->delete_permanently(); |
260 | 260 | } else { |
261 | 261 | //delete is blocked |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | } |
265 | 265 | } else { |
266 | 266 | //its not a soft-deletable thing anyways. do the normal logic. |
267 | - if (! $delete_is_blocked) { |
|
267 | + if ( ! $delete_is_blocked) { |
|
268 | 268 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
269 | 269 | $related_model_object->delete(); |
270 | 270 | $deleted_count++; |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | if ($model_object_or_id instanceof EE_Base_Class) { |
289 | 289 | $model_object_id = $model_object_or_id->ID(); |
290 | 290 | } |
291 | - if (! $model_object_id) { |
|
291 | + if ( ! $model_object_id) { |
|
292 | 292 | throw new EE_Error(sprintf(__("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", |
293 | 293 | "event_espresso"), $this->get_other_model()->get_this_model_name(), |
294 | 294 | $this->get_this_model()->get_this_model_name())); |
@@ -17,482 +17,482 @@ |
||
17 | 17 | */ |
18 | 18 | abstract class EE_Model_Relation_Base implements HasSchemaInterface |
19 | 19 | { |
20 | - /** |
|
21 | - * The model name of which this relation is a component (ie, the model that called new EE_Model_Relation_Base) |
|
22 | - * |
|
23 | - * @var string eg Event, Question_Group, Registration |
|
24 | - */ |
|
25 | - private $_this_model_name; |
|
26 | - /** |
|
27 | - * The model name pointed to by this relation (ie, the model we want to establish a relationship to) |
|
28 | - * |
|
29 | - * @var string eg Event, Question_Group, Registration |
|
30 | - */ |
|
31 | - private $_other_model_name; |
|
32 | - |
|
33 | - /** |
|
34 | - * this is typically used when calling the relation models to make sure they inherit any set timezone from the |
|
35 | - * initiating model. |
|
36 | - * |
|
37 | - * @var string |
|
38 | - */ |
|
39 | - protected $_timezone; |
|
40 | - |
|
41 | - /** |
|
42 | - * If you try to delete "this_model", and there are related "other_models", |
|
43 | - * and this isn't null, then abandon the deletion and add this warning. |
|
44 | - * This effectively makes it impossible to delete "this_model" while there are |
|
45 | - * related "other_models" along this relation. |
|
46 | - * |
|
47 | - * @var string (internationalized) |
|
48 | - */ |
|
49 | - protected $_blocking_delete_error_message; |
|
50 | - |
|
51 | - protected $_blocking_delete = false; |
|
52 | - |
|
53 | - /** |
|
54 | - * Object representing the relationship between two models. This knows how to join the models, |
|
55 | - * get related models across the relation, and add-and-remove the relationships. |
|
56 | - * |
|
57 | - * @param boolean $block_deletes if there are related models across this relation, block (prevent |
|
58 | - * and add an error) the deletion of this model |
|
59 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
60 | - * default |
|
61 | - */ |
|
62 | - public function __construct($block_deletes, $blocking_delete_error_message) |
|
63 | - { |
|
64 | - $this->_blocking_delete = $block_deletes; |
|
65 | - $this->_blocking_delete_error_message = $blocking_delete_error_message; |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * @param $this_model_name |
|
71 | - * @param $other_model_name |
|
72 | - * @throws EE_Error |
|
73 | - */ |
|
74 | - public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
75 | - { |
|
76 | - $this->_this_model_name = $this_model_name; |
|
77 | - $this->_other_model_name = $other_model_name; |
|
78 | - if (is_string($this->_blocking_delete)) { |
|
79 | - throw new EE_Error(sprintf(__("When instantiating the relation of type %s from %s to %s, the \$block_deletes argument should be a boolean, not a string (%s)", |
|
80 | - "event_espresso"), |
|
81 | - get_class($this), $this_model_name, $other_model_name, $this->_blocking_delete)); |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - |
|
86 | - /** |
|
87 | - * Gets the model where this relation is defined. |
|
88 | - * |
|
89 | - * @return EEM_Base |
|
90 | - */ |
|
91 | - public function get_this_model() |
|
92 | - { |
|
93 | - return $this->_get_model($this->_this_model_name); |
|
94 | - } |
|
95 | - |
|
96 | - |
|
97 | - /** |
|
98 | - * Gets the model which this relation establishes the relation TO (ie, |
|
99 | - * this relation object was defined on get_this_model(), get_other_model() is the other one) |
|
100 | - * |
|
101 | - * @return EEM_Base |
|
102 | - */ |
|
103 | - public function get_other_model() |
|
104 | - { |
|
105 | - return $this->_get_model($this->_other_model_name); |
|
106 | - } |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * Internally used by get_this_model() and get_other_model() |
|
111 | - * |
|
112 | - * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
|
113 | - * @return EEM_Base |
|
114 | - */ |
|
115 | - protected function _get_model($model_name) |
|
116 | - { |
|
117 | - $modelInstance = EE_Registry::instance()->load_model($model_name); |
|
118 | - $modelInstance->set_timezone($this->_timezone); |
|
119 | - return $modelInstance; |
|
120 | - } |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * entirely possible that relations may be called from a model and we need to make sure those relations have their |
|
125 | - * timezone set correctly. |
|
126 | - * |
|
127 | - * @param string $timezone timezone to set. |
|
128 | - */ |
|
129 | - public function set_timezone($timezone) |
|
130 | - { |
|
131 | - if ($timezone !== null) { |
|
132 | - $this->_timezone = $timezone; |
|
133 | - } |
|
134 | - } |
|
135 | - |
|
136 | - |
|
137 | - /** |
|
138 | - * @param $other_table |
|
139 | - * @param $other_table_alias |
|
140 | - * @param $other_table_column |
|
141 | - * @param $this_table_alias |
|
142 | - * @param $this_table_join_column |
|
143 | - * @param string $extra_join_sql |
|
144 | - * @return string |
|
145 | - */ |
|
146 | - protected function _left_join( |
|
147 | - $other_table, |
|
148 | - $other_table_alias, |
|
149 | - $other_table_column, |
|
150 | - $this_table_alias, |
|
151 | - $this_table_join_column, |
|
152 | - $extra_join_sql = '' |
|
153 | - ) { |
|
154 | - return " LEFT JOIN " . $other_table . " AS " . $other_table_alias . " ON " . $other_table_alias . "." . $other_table_column . "=" . $this_table_alias . "." . $this_table_join_column . ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
155 | - } |
|
156 | - |
|
157 | - |
|
158 | - /** |
|
159 | - * Gets all the model objects of type of other model related to $model_object, |
|
160 | - * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation. |
|
161 | - * For both of those child classes, $model_object must be saved so that it has an ID before querying, |
|
162 | - * otherwise an error will be thrown. Note: by default we disable default_where_conditions |
|
163 | - * EE_Belongs_To_Relation doesn't need to be saved before querying. |
|
164 | - * |
|
165 | - * @param EE_Base_Class|int $model_object_or_id or the primary key of this model |
|
166 | - * @param array $query_params like EEM_Base::get_all's $query_params |
|
167 | - * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
168 | - * @return EE_Base_Class[] |
|
169 | - * @throws \EE_Error |
|
170 | - */ |
|
171 | - public function get_all_related( |
|
172 | - $model_object_or_id, |
|
173 | - $query_params = array(), |
|
174 | - $values_already_prepared_by_model_object = false |
|
175 | - ) { |
|
176 | - if ($values_already_prepared_by_model_object !== false) { |
|
177 | - EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
178 | - __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
179 | - '4.8.1'); |
|
180 | - } |
|
181 | - $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
182 | - $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() |
|
183 | - . "." |
|
184 | - . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
185 | - $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
186 | - $query_params[0][$query_param_where_this_model_pk] = $model_object_id; |
|
187 | - return $this->get_other_model()->get_all($query_params); |
|
188 | - } |
|
189 | - |
|
190 | - |
|
191 | - /** |
|
192 | - * Alters the $query_params to disable default where conditions, unless otherwise specified |
|
193 | - * |
|
194 | - * @param string $query_params |
|
195 | - * @return array |
|
196 | - */ |
|
197 | - protected function _disable_default_where_conditions_on_query_param($query_params) |
|
198 | - { |
|
199 | - if (! isset($query_params['default_where_conditions'])) { |
|
200 | - $query_params['default_where_conditions'] = 'none'; |
|
201 | - } |
|
202 | - return $query_params; |
|
203 | - } |
|
204 | - |
|
205 | - |
|
206 | - /** |
|
207 | - * Deletes the related model objects which meet the query parameters. If no |
|
208 | - * parameters are specified, then all related model objects will be deleted. |
|
209 | - * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
210 | - * model objects will only be soft-deleted. |
|
211 | - * |
|
212 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
213 | - * @param array $query_params |
|
214 | - * @return int of how many related models got deleted |
|
215 | - * @throws \EE_Error |
|
216 | - */ |
|
217 | - public function delete_all_related($model_object_or_id, $query_params = array()) |
|
218 | - { |
|
219 | - //for each thing we would delete, |
|
220 | - $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
221 | - //determine if it's blocked by anything else before it can be deleted |
|
222 | - $deleted_count = 0; |
|
223 | - foreach ($related_model_objects as $related_model_object) { |
|
224 | - $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object, |
|
225 | - $model_object_or_id); |
|
226 | - /* @var $model_object_or_id EE_Base_Class */ |
|
227 | - if (! $delete_is_blocked) { |
|
228 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
229 | - $related_model_object->delete(); |
|
230 | - $deleted_count++; |
|
231 | - } |
|
232 | - } |
|
233 | - return $deleted_count; |
|
234 | - } |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * Deletes the related model objects which meet the query parameters. If no |
|
239 | - * parameters are specified, then all related model objects will be deleted. |
|
240 | - * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
241 | - * model objects will only be soft-deleted. |
|
242 | - * |
|
243 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
244 | - * @param array $query_params |
|
245 | - * @return int of how many related models got deleted |
|
246 | - * @throws \EE_Error |
|
247 | - */ |
|
248 | - public function delete_related_permanently($model_object_or_id, $query_params = array()) |
|
249 | - { |
|
250 | - //for each thing we would delete, |
|
251 | - $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
252 | - //determine if it's blocked by anything else before it can be deleted |
|
253 | - $deleted_count = 0; |
|
254 | - foreach ($related_model_objects as $related_model_object) { |
|
255 | - $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object, |
|
256 | - $model_object_or_id); |
|
257 | - /* @var $model_object_or_id EE_Base_Class */ |
|
258 | - if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
|
259 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
260 | - $deleted_count++; |
|
261 | - if (! $delete_is_blocked) { |
|
262 | - $related_model_object->delete_permanently(); |
|
263 | - } else { |
|
264 | - //delete is blocked |
|
265 | - //brent and darren, in this case, wanted to just soft delete it then |
|
266 | - $related_model_object->delete(); |
|
267 | - } |
|
268 | - } else { |
|
269 | - //its not a soft-deletable thing anyways. do the normal logic. |
|
270 | - if (! $delete_is_blocked) { |
|
271 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
272 | - $related_model_object->delete(); |
|
273 | - $deleted_count++; |
|
274 | - } |
|
275 | - } |
|
276 | - } |
|
277 | - return $deleted_count; |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - /** |
|
282 | - * this just returns a model_object_id for incoming item that could be an object or id. |
|
283 | - * |
|
284 | - * @param EE_Base_Class|int $model_object_or_id model object or the primary key of this model |
|
285 | - * @throws EE_Error |
|
286 | - * @return int |
|
287 | - */ |
|
288 | - protected function _get_model_object_id($model_object_or_id) |
|
289 | - { |
|
290 | - $model_object_id = $model_object_or_id; |
|
291 | - if ($model_object_or_id instanceof EE_Base_Class) { |
|
292 | - $model_object_id = $model_object_or_id->ID(); |
|
293 | - } |
|
294 | - if (! $model_object_id) { |
|
295 | - throw new EE_Error(sprintf(__("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", |
|
296 | - "event_espresso"), $this->get_other_model()->get_this_model_name(), |
|
297 | - $this->get_this_model()->get_this_model_name())); |
|
298 | - } |
|
299 | - return $model_object_id; |
|
300 | - } |
|
301 | - |
|
302 | - |
|
303 | - /** |
|
304 | - * Gets the SQL string for performing the join between this model and the other model. |
|
305 | - * |
|
306 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
307 | - * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
308 | - * other_model_primary_table.fk" etc |
|
309 | - */ |
|
310 | - abstract public function get_join_statement($model_relation_chain); |
|
311 | - |
|
312 | - |
|
313 | - /** |
|
314 | - * Adds a relationships between the two model objects provided. Each type of relationship handles this differently |
|
315 | - * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this |
|
316 | - * relationship only allows this model to be related to a single other model of this type) |
|
317 | - * |
|
318 | - * @param $this_obj_or_id |
|
319 | - * @param $other_obj_or_id |
|
320 | - * @param array $extra_join_model_fields_n_values |
|
321 | - * @return \EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for |
|
322 | - * $other_obj_or_id) |
|
323 | - */ |
|
324 | - abstract public function add_relation_to( |
|
325 | - $this_obj_or_id, |
|
326 | - $other_obj_or_id, |
|
327 | - $extra_join_model_fields_n_values = array() |
|
328 | - ); |
|
329 | - |
|
330 | - |
|
331 | - /** |
|
332 | - * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two |
|
333 | - * model objects |
|
334 | - * |
|
335 | - * @param $this_obj_or_id |
|
336 | - * @param $other_obj_or_id |
|
337 | - * @param array $where_query |
|
338 | - * @return bool |
|
339 | - */ |
|
340 | - abstract public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()); |
|
341 | - |
|
342 | - |
|
343 | - /** |
|
344 | - * Removes ALL relation instances for this relation obj |
|
345 | - * |
|
346 | - * @param EE_Base_Class|int $this_obj_or_id |
|
347 | - * @param array $where_query_param like EEM_Base::get_all's $query_params[0] (where conditions) |
|
348 | - * @return EE_Base_Class[] |
|
349 | - * @throws \EE_Error |
|
350 | - */ |
|
351 | - public function remove_relations($this_obj_or_id, $where_query_param = array()) |
|
352 | - { |
|
353 | - $related_things = $this->get_all_related($this_obj_or_id, array($where_query_param)); |
|
354 | - $objs_removed = array(); |
|
355 | - foreach ($related_things as $related_thing) { |
|
356 | - $objs_removed[] = $this->remove_relation_to($this_obj_or_id, $related_thing); |
|
357 | - } |
|
358 | - return $objs_removed; |
|
359 | - } |
|
360 | - |
|
361 | - |
|
362 | - /** |
|
363 | - * If you aren't allowed to delete this model when there are related models across this |
|
364 | - * relation object, return true. Otherwise, if you can delete this model even though |
|
365 | - * related objects exist, returns false. |
|
366 | - * |
|
367 | - * @return boolean |
|
368 | - */ |
|
369 | - public function block_delete_if_related_models_exist() |
|
370 | - { |
|
371 | - return $this->_blocking_delete; |
|
372 | - } |
|
373 | - |
|
374 | - |
|
375 | - /** |
|
376 | - * Gets the error message to show |
|
377 | - * |
|
378 | - * @return string |
|
379 | - */ |
|
380 | - public function get_deletion_error_message() |
|
381 | - { |
|
382 | - if ($this->_blocking_delete_error_message) { |
|
383 | - return $this->_blocking_delete_error_message; |
|
384 | - } else { |
|
20 | + /** |
|
21 | + * The model name of which this relation is a component (ie, the model that called new EE_Model_Relation_Base) |
|
22 | + * |
|
23 | + * @var string eg Event, Question_Group, Registration |
|
24 | + */ |
|
25 | + private $_this_model_name; |
|
26 | + /** |
|
27 | + * The model name pointed to by this relation (ie, the model we want to establish a relationship to) |
|
28 | + * |
|
29 | + * @var string eg Event, Question_Group, Registration |
|
30 | + */ |
|
31 | + private $_other_model_name; |
|
32 | + |
|
33 | + /** |
|
34 | + * this is typically used when calling the relation models to make sure they inherit any set timezone from the |
|
35 | + * initiating model. |
|
36 | + * |
|
37 | + * @var string |
|
38 | + */ |
|
39 | + protected $_timezone; |
|
40 | + |
|
41 | + /** |
|
42 | + * If you try to delete "this_model", and there are related "other_models", |
|
43 | + * and this isn't null, then abandon the deletion and add this warning. |
|
44 | + * This effectively makes it impossible to delete "this_model" while there are |
|
45 | + * related "other_models" along this relation. |
|
46 | + * |
|
47 | + * @var string (internationalized) |
|
48 | + */ |
|
49 | + protected $_blocking_delete_error_message; |
|
50 | + |
|
51 | + protected $_blocking_delete = false; |
|
52 | + |
|
53 | + /** |
|
54 | + * Object representing the relationship between two models. This knows how to join the models, |
|
55 | + * get related models across the relation, and add-and-remove the relationships. |
|
56 | + * |
|
57 | + * @param boolean $block_deletes if there are related models across this relation, block (prevent |
|
58 | + * and add an error) the deletion of this model |
|
59 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
60 | + * default |
|
61 | + */ |
|
62 | + public function __construct($block_deletes, $blocking_delete_error_message) |
|
63 | + { |
|
64 | + $this->_blocking_delete = $block_deletes; |
|
65 | + $this->_blocking_delete_error_message = $blocking_delete_error_message; |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * @param $this_model_name |
|
71 | + * @param $other_model_name |
|
72 | + * @throws EE_Error |
|
73 | + */ |
|
74 | + public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
75 | + { |
|
76 | + $this->_this_model_name = $this_model_name; |
|
77 | + $this->_other_model_name = $other_model_name; |
|
78 | + if (is_string($this->_blocking_delete)) { |
|
79 | + throw new EE_Error(sprintf(__("When instantiating the relation of type %s from %s to %s, the \$block_deletes argument should be a boolean, not a string (%s)", |
|
80 | + "event_espresso"), |
|
81 | + get_class($this), $this_model_name, $other_model_name, $this->_blocking_delete)); |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + |
|
86 | + /** |
|
87 | + * Gets the model where this relation is defined. |
|
88 | + * |
|
89 | + * @return EEM_Base |
|
90 | + */ |
|
91 | + public function get_this_model() |
|
92 | + { |
|
93 | + return $this->_get_model($this->_this_model_name); |
|
94 | + } |
|
95 | + |
|
96 | + |
|
97 | + /** |
|
98 | + * Gets the model which this relation establishes the relation TO (ie, |
|
99 | + * this relation object was defined on get_this_model(), get_other_model() is the other one) |
|
100 | + * |
|
101 | + * @return EEM_Base |
|
102 | + */ |
|
103 | + public function get_other_model() |
|
104 | + { |
|
105 | + return $this->_get_model($this->_other_model_name); |
|
106 | + } |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * Internally used by get_this_model() and get_other_model() |
|
111 | + * |
|
112 | + * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
|
113 | + * @return EEM_Base |
|
114 | + */ |
|
115 | + protected function _get_model($model_name) |
|
116 | + { |
|
117 | + $modelInstance = EE_Registry::instance()->load_model($model_name); |
|
118 | + $modelInstance->set_timezone($this->_timezone); |
|
119 | + return $modelInstance; |
|
120 | + } |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * entirely possible that relations may be called from a model and we need to make sure those relations have their |
|
125 | + * timezone set correctly. |
|
126 | + * |
|
127 | + * @param string $timezone timezone to set. |
|
128 | + */ |
|
129 | + public function set_timezone($timezone) |
|
130 | + { |
|
131 | + if ($timezone !== null) { |
|
132 | + $this->_timezone = $timezone; |
|
133 | + } |
|
134 | + } |
|
135 | + |
|
136 | + |
|
137 | + /** |
|
138 | + * @param $other_table |
|
139 | + * @param $other_table_alias |
|
140 | + * @param $other_table_column |
|
141 | + * @param $this_table_alias |
|
142 | + * @param $this_table_join_column |
|
143 | + * @param string $extra_join_sql |
|
144 | + * @return string |
|
145 | + */ |
|
146 | + protected function _left_join( |
|
147 | + $other_table, |
|
148 | + $other_table_alias, |
|
149 | + $other_table_column, |
|
150 | + $this_table_alias, |
|
151 | + $this_table_join_column, |
|
152 | + $extra_join_sql = '' |
|
153 | + ) { |
|
154 | + return " LEFT JOIN " . $other_table . " AS " . $other_table_alias . " ON " . $other_table_alias . "." . $other_table_column . "=" . $this_table_alias . "." . $this_table_join_column . ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
155 | + } |
|
156 | + |
|
157 | + |
|
158 | + /** |
|
159 | + * Gets all the model objects of type of other model related to $model_object, |
|
160 | + * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation. |
|
161 | + * For both of those child classes, $model_object must be saved so that it has an ID before querying, |
|
162 | + * otherwise an error will be thrown. Note: by default we disable default_where_conditions |
|
163 | + * EE_Belongs_To_Relation doesn't need to be saved before querying. |
|
164 | + * |
|
165 | + * @param EE_Base_Class|int $model_object_or_id or the primary key of this model |
|
166 | + * @param array $query_params like EEM_Base::get_all's $query_params |
|
167 | + * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
168 | + * @return EE_Base_Class[] |
|
169 | + * @throws \EE_Error |
|
170 | + */ |
|
171 | + public function get_all_related( |
|
172 | + $model_object_or_id, |
|
173 | + $query_params = array(), |
|
174 | + $values_already_prepared_by_model_object = false |
|
175 | + ) { |
|
176 | + if ($values_already_prepared_by_model_object !== false) { |
|
177 | + EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
178 | + __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
179 | + '4.8.1'); |
|
180 | + } |
|
181 | + $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
182 | + $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() |
|
183 | + . "." |
|
184 | + . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
185 | + $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
186 | + $query_params[0][$query_param_where_this_model_pk] = $model_object_id; |
|
187 | + return $this->get_other_model()->get_all($query_params); |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + /** |
|
192 | + * Alters the $query_params to disable default where conditions, unless otherwise specified |
|
193 | + * |
|
194 | + * @param string $query_params |
|
195 | + * @return array |
|
196 | + */ |
|
197 | + protected function _disable_default_where_conditions_on_query_param($query_params) |
|
198 | + { |
|
199 | + if (! isset($query_params['default_where_conditions'])) { |
|
200 | + $query_params['default_where_conditions'] = 'none'; |
|
201 | + } |
|
202 | + return $query_params; |
|
203 | + } |
|
204 | + |
|
205 | + |
|
206 | + /** |
|
207 | + * Deletes the related model objects which meet the query parameters. If no |
|
208 | + * parameters are specified, then all related model objects will be deleted. |
|
209 | + * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
210 | + * model objects will only be soft-deleted. |
|
211 | + * |
|
212 | + * @param EE_Base_Class|int|string $model_object_or_id |
|
213 | + * @param array $query_params |
|
214 | + * @return int of how many related models got deleted |
|
215 | + * @throws \EE_Error |
|
216 | + */ |
|
217 | + public function delete_all_related($model_object_or_id, $query_params = array()) |
|
218 | + { |
|
219 | + //for each thing we would delete, |
|
220 | + $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
221 | + //determine if it's blocked by anything else before it can be deleted |
|
222 | + $deleted_count = 0; |
|
223 | + foreach ($related_model_objects as $related_model_object) { |
|
224 | + $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object, |
|
225 | + $model_object_or_id); |
|
226 | + /* @var $model_object_or_id EE_Base_Class */ |
|
227 | + if (! $delete_is_blocked) { |
|
228 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
229 | + $related_model_object->delete(); |
|
230 | + $deleted_count++; |
|
231 | + } |
|
232 | + } |
|
233 | + return $deleted_count; |
|
234 | + } |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * Deletes the related model objects which meet the query parameters. If no |
|
239 | + * parameters are specified, then all related model objects will be deleted. |
|
240 | + * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
241 | + * model objects will only be soft-deleted. |
|
242 | + * |
|
243 | + * @param EE_Base_Class|int|string $model_object_or_id |
|
244 | + * @param array $query_params |
|
245 | + * @return int of how many related models got deleted |
|
246 | + * @throws \EE_Error |
|
247 | + */ |
|
248 | + public function delete_related_permanently($model_object_or_id, $query_params = array()) |
|
249 | + { |
|
250 | + //for each thing we would delete, |
|
251 | + $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
252 | + //determine if it's blocked by anything else before it can be deleted |
|
253 | + $deleted_count = 0; |
|
254 | + foreach ($related_model_objects as $related_model_object) { |
|
255 | + $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object, |
|
256 | + $model_object_or_id); |
|
257 | + /* @var $model_object_or_id EE_Base_Class */ |
|
258 | + if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
|
259 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
260 | + $deleted_count++; |
|
261 | + if (! $delete_is_blocked) { |
|
262 | + $related_model_object->delete_permanently(); |
|
263 | + } else { |
|
264 | + //delete is blocked |
|
265 | + //brent and darren, in this case, wanted to just soft delete it then |
|
266 | + $related_model_object->delete(); |
|
267 | + } |
|
268 | + } else { |
|
269 | + //its not a soft-deletable thing anyways. do the normal logic. |
|
270 | + if (! $delete_is_blocked) { |
|
271 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
272 | + $related_model_object->delete(); |
|
273 | + $deleted_count++; |
|
274 | + } |
|
275 | + } |
|
276 | + } |
|
277 | + return $deleted_count; |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + /** |
|
282 | + * this just returns a model_object_id for incoming item that could be an object or id. |
|
283 | + * |
|
284 | + * @param EE_Base_Class|int $model_object_or_id model object or the primary key of this model |
|
285 | + * @throws EE_Error |
|
286 | + * @return int |
|
287 | + */ |
|
288 | + protected function _get_model_object_id($model_object_or_id) |
|
289 | + { |
|
290 | + $model_object_id = $model_object_or_id; |
|
291 | + if ($model_object_or_id instanceof EE_Base_Class) { |
|
292 | + $model_object_id = $model_object_or_id->ID(); |
|
293 | + } |
|
294 | + if (! $model_object_id) { |
|
295 | + throw new EE_Error(sprintf(__("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", |
|
296 | + "event_espresso"), $this->get_other_model()->get_this_model_name(), |
|
297 | + $this->get_this_model()->get_this_model_name())); |
|
298 | + } |
|
299 | + return $model_object_id; |
|
300 | + } |
|
301 | + |
|
302 | + |
|
303 | + /** |
|
304 | + * Gets the SQL string for performing the join between this model and the other model. |
|
305 | + * |
|
306 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
307 | + * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
308 | + * other_model_primary_table.fk" etc |
|
309 | + */ |
|
310 | + abstract public function get_join_statement($model_relation_chain); |
|
311 | + |
|
312 | + |
|
313 | + /** |
|
314 | + * Adds a relationships between the two model objects provided. Each type of relationship handles this differently |
|
315 | + * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this |
|
316 | + * relationship only allows this model to be related to a single other model of this type) |
|
317 | + * |
|
318 | + * @param $this_obj_or_id |
|
319 | + * @param $other_obj_or_id |
|
320 | + * @param array $extra_join_model_fields_n_values |
|
321 | + * @return \EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for |
|
322 | + * $other_obj_or_id) |
|
323 | + */ |
|
324 | + abstract public function add_relation_to( |
|
325 | + $this_obj_or_id, |
|
326 | + $other_obj_or_id, |
|
327 | + $extra_join_model_fields_n_values = array() |
|
328 | + ); |
|
329 | + |
|
330 | + |
|
331 | + /** |
|
332 | + * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two |
|
333 | + * model objects |
|
334 | + * |
|
335 | + * @param $this_obj_or_id |
|
336 | + * @param $other_obj_or_id |
|
337 | + * @param array $where_query |
|
338 | + * @return bool |
|
339 | + */ |
|
340 | + abstract public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()); |
|
341 | + |
|
342 | + |
|
343 | + /** |
|
344 | + * Removes ALL relation instances for this relation obj |
|
345 | + * |
|
346 | + * @param EE_Base_Class|int $this_obj_or_id |
|
347 | + * @param array $where_query_param like EEM_Base::get_all's $query_params[0] (where conditions) |
|
348 | + * @return EE_Base_Class[] |
|
349 | + * @throws \EE_Error |
|
350 | + */ |
|
351 | + public function remove_relations($this_obj_or_id, $where_query_param = array()) |
|
352 | + { |
|
353 | + $related_things = $this->get_all_related($this_obj_or_id, array($where_query_param)); |
|
354 | + $objs_removed = array(); |
|
355 | + foreach ($related_things as $related_thing) { |
|
356 | + $objs_removed[] = $this->remove_relation_to($this_obj_or_id, $related_thing); |
|
357 | + } |
|
358 | + return $objs_removed; |
|
359 | + } |
|
360 | + |
|
361 | + |
|
362 | + /** |
|
363 | + * If you aren't allowed to delete this model when there are related models across this |
|
364 | + * relation object, return true. Otherwise, if you can delete this model even though |
|
365 | + * related objects exist, returns false. |
|
366 | + * |
|
367 | + * @return boolean |
|
368 | + */ |
|
369 | + public function block_delete_if_related_models_exist() |
|
370 | + { |
|
371 | + return $this->_blocking_delete; |
|
372 | + } |
|
373 | + |
|
374 | + |
|
375 | + /** |
|
376 | + * Gets the error message to show |
|
377 | + * |
|
378 | + * @return string |
|
379 | + */ |
|
380 | + public function get_deletion_error_message() |
|
381 | + { |
|
382 | + if ($this->_blocking_delete_error_message) { |
|
383 | + return $this->_blocking_delete_error_message; |
|
384 | + } else { |
|
385 | 385 | // return sprintf(__('Cannot delete %1$s when there are related %2$s', "event_espresso"),$this->get_this_model()->item_name(2),$this->get_other_model()->item_name(2)); |
386 | - return sprintf( |
|
387 | - __('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.', |
|
388 | - "event_espresso"), |
|
389 | - $this->get_this_model()->item_name(1), |
|
390 | - $this->get_other_model()->item_name(1), |
|
391 | - $this->get_other_model()->item_name(2) |
|
392 | - ); |
|
393 | - } |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Returns whatever is set as the nicename for the object. |
|
398 | - * |
|
399 | - * @return string |
|
400 | - */ |
|
401 | - public function getSchemaDescription() |
|
402 | - { |
|
403 | - $description = $this instanceof EE_Belongs_To_Relation |
|
404 | - ? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso') |
|
405 | - : esc_html__('The related %1$s entities to the %2$s.', 'event_espresso'); |
|
406 | - return sprintf( |
|
407 | - $description, |
|
408 | - $this->get_other_model()->get_this_model_name(), |
|
409 | - $this->get_this_model()->get_this_model_name() |
|
410 | - ); |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * Returns whatever is set as the $_schema_type property for the object. |
|
415 | - * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
416 | - * |
|
417 | - * @return string|array |
|
418 | - */ |
|
419 | - public function getSchemaType() |
|
420 | - { |
|
421 | - return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array'; |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
426 | - * this method and return the properties for the schema. |
|
427 | - * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
428 | - * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
429 | - * |
|
430 | - * @return array |
|
431 | - */ |
|
432 | - public function getSchemaProperties() |
|
433 | - { |
|
434 | - return array(); |
|
435 | - } |
|
436 | - |
|
437 | - /** |
|
438 | - * If a child class has enum values, they should override this method and provide a simple array |
|
439 | - * of the enum values. |
|
440 | - * The reason this is not a property on the class is because there may be filterable enum values that |
|
441 | - * are set on the instantiated object that could be filtered after construct. |
|
442 | - * |
|
443 | - * @return array |
|
444 | - */ |
|
445 | - public function getSchemaEnum() |
|
446 | - { |
|
447 | - return array(); |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * This returns the value of the $_schema_format property on the object. |
|
452 | - * |
|
453 | - * @return string |
|
454 | - */ |
|
455 | - public function getSchemaFormat() |
|
456 | - { |
|
457 | - return array(); |
|
458 | - } |
|
459 | - |
|
460 | - /** |
|
461 | - * This returns the value of the $_schema_readonly property on the object. |
|
462 | - * |
|
463 | - * @return bool |
|
464 | - */ |
|
465 | - public function getSchemaReadonly() |
|
466 | - { |
|
467 | - return true; |
|
468 | - } |
|
469 | - |
|
470 | - /** |
|
471 | - * This returns elements used to represent this field in the json schema. |
|
472 | - * |
|
473 | - * @link http://json-schema.org/ |
|
474 | - * @return array |
|
475 | - */ |
|
476 | - public function getSchema() |
|
477 | - { |
|
478 | - $schema = array( |
|
479 | - 'description' => $this->getSchemaDescription(), |
|
480 | - 'type' => $this->getSchemaType(), |
|
481 | - 'relation' => true, |
|
482 | - 'relation_type' => get_class($this), |
|
483 | - 'readonly' => $this->getSchemaReadonly() |
|
484 | - ); |
|
485 | - |
|
486 | - if ($this instanceof EE_HABTM_Relation) { |
|
487 | - $schema['joining_model_name'] = $this->get_join_model()->get_this_model_name(); |
|
488 | - } |
|
489 | - |
|
490 | - if ($this->getSchemaType() === 'array') { |
|
491 | - $schema['items'] = array( |
|
492 | - 'type' => 'object' |
|
493 | - ); |
|
494 | - } |
|
495 | - |
|
496 | - return $schema; |
|
497 | - } |
|
386 | + return sprintf( |
|
387 | + __('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.', |
|
388 | + "event_espresso"), |
|
389 | + $this->get_this_model()->item_name(1), |
|
390 | + $this->get_other_model()->item_name(1), |
|
391 | + $this->get_other_model()->item_name(2) |
|
392 | + ); |
|
393 | + } |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Returns whatever is set as the nicename for the object. |
|
398 | + * |
|
399 | + * @return string |
|
400 | + */ |
|
401 | + public function getSchemaDescription() |
|
402 | + { |
|
403 | + $description = $this instanceof EE_Belongs_To_Relation |
|
404 | + ? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso') |
|
405 | + : esc_html__('The related %1$s entities to the %2$s.', 'event_espresso'); |
|
406 | + return sprintf( |
|
407 | + $description, |
|
408 | + $this->get_other_model()->get_this_model_name(), |
|
409 | + $this->get_this_model()->get_this_model_name() |
|
410 | + ); |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * Returns whatever is set as the $_schema_type property for the object. |
|
415 | + * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
416 | + * |
|
417 | + * @return string|array |
|
418 | + */ |
|
419 | + public function getSchemaType() |
|
420 | + { |
|
421 | + return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array'; |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
426 | + * this method and return the properties for the schema. |
|
427 | + * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
428 | + * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
429 | + * |
|
430 | + * @return array |
|
431 | + */ |
|
432 | + public function getSchemaProperties() |
|
433 | + { |
|
434 | + return array(); |
|
435 | + } |
|
436 | + |
|
437 | + /** |
|
438 | + * If a child class has enum values, they should override this method and provide a simple array |
|
439 | + * of the enum values. |
|
440 | + * The reason this is not a property on the class is because there may be filterable enum values that |
|
441 | + * are set on the instantiated object that could be filtered after construct. |
|
442 | + * |
|
443 | + * @return array |
|
444 | + */ |
|
445 | + public function getSchemaEnum() |
|
446 | + { |
|
447 | + return array(); |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * This returns the value of the $_schema_format property on the object. |
|
452 | + * |
|
453 | + * @return string |
|
454 | + */ |
|
455 | + public function getSchemaFormat() |
|
456 | + { |
|
457 | + return array(); |
|
458 | + } |
|
459 | + |
|
460 | + /** |
|
461 | + * This returns the value of the $_schema_readonly property on the object. |
|
462 | + * |
|
463 | + * @return bool |
|
464 | + */ |
|
465 | + public function getSchemaReadonly() |
|
466 | + { |
|
467 | + return true; |
|
468 | + } |
|
469 | + |
|
470 | + /** |
|
471 | + * This returns elements used to represent this field in the json schema. |
|
472 | + * |
|
473 | + * @link http://json-schema.org/ |
|
474 | + * @return array |
|
475 | + */ |
|
476 | + public function getSchema() |
|
477 | + { |
|
478 | + $schema = array( |
|
479 | + 'description' => $this->getSchemaDescription(), |
|
480 | + 'type' => $this->getSchemaType(), |
|
481 | + 'relation' => true, |
|
482 | + 'relation_type' => get_class($this), |
|
483 | + 'readonly' => $this->getSchemaReadonly() |
|
484 | + ); |
|
485 | + |
|
486 | + if ($this instanceof EE_HABTM_Relation) { |
|
487 | + $schema['joining_model_name'] = $this->get_join_model()->get_this_model_name(); |
|
488 | + } |
|
489 | + |
|
490 | + if ($this->getSchemaType() === 'array') { |
|
491 | + $schema['items'] = array( |
|
492 | + 'type' => 'object' |
|
493 | + ); |
|
494 | + } |
|
495 | + |
|
496 | + return $schema; |
|
497 | + } |
|
498 | 498 | } |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -require_once(EE_MODELS . 'relations/EE_Model_Relation_Base.php'); |
|
2 | +require_once(EE_MODELS.'relations/EE_Model_Relation_Base.php'); |
|
3 | 3 | |
4 | 4 | |
5 | 5 | /** |
@@ -46,13 +46,13 @@ discard block |
||
46 | 46 | $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
47 | 47 | $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
48 | 48 | $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
49 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
49 | + $this->get_this_model()->get_this_model_name()).$this_table_pk_field->get_table_alias(); |
|
50 | 50 | $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
51 | - $this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias(); |
|
51 | + $this->get_other_model()->get_this_model_name()).$other_table_fk_field->get_table_alias(); |
|
52 | 52 | $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
53 | 53 | |
54 | 54 | return $this->_left_join($fk_table, $fk_table_alias, $other_table_fk_field->get_table_column(), $pk_table_alias, |
55 | - $this_table_pk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
55 | + $this_table_pk_field->get_table_column()).$this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 |
@@ -13,93 +13,93 @@ |
||
13 | 13 | class EE_Has_Many_Relation extends EE_Model_Relation_Base |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the |
|
18 | - * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but |
|
19 | - * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get |
|
20 | - * related models across the relation, and add-and-remove the relationships. |
|
21 | - * |
|
22 | - * @param boolean $block_deletes For this type of r elation, we block by default. If there are |
|
23 | - * related models across this relation, block (prevent and add an |
|
24 | - * error) the deletion of this model |
|
25 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
26 | - * default |
|
27 | - */ |
|
28 | - public function __construct($block_deletes = true, $blocking_delete_error_message = null) |
|
29 | - { |
|
30 | - parent::__construct($block_deletes, $blocking_delete_error_message); |
|
31 | - } |
|
16 | + /** |
|
17 | + * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the |
|
18 | + * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but |
|
19 | + * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get |
|
20 | + * related models across the relation, and add-and-remove the relationships. |
|
21 | + * |
|
22 | + * @param boolean $block_deletes For this type of r elation, we block by default. If there are |
|
23 | + * related models across this relation, block (prevent and add an |
|
24 | + * error) the deletion of this model |
|
25 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
26 | + * default |
|
27 | + */ |
|
28 | + public function __construct($block_deletes = true, $blocking_delete_error_message = null) |
|
29 | + { |
|
30 | + parent::__construct($block_deletes, $blocking_delete_error_message); |
|
31 | + } |
|
32 | 32 | |
33 | 33 | |
34 | - /** |
|
35 | - * Gets the SQL string for performing the join between this model and the other model. |
|
36 | - * |
|
37 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
38 | - * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
39 | - * other_model_primary_table.fk" etc |
|
40 | - * @throws \EE_Error |
|
41 | - */ |
|
42 | - public function get_join_statement($model_relation_chain) |
|
43 | - { |
|
44 | - //create the sql string like |
|
45 | - // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions |
|
46 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
47 | - $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
48 | - $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
49 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
50 | - $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
51 | - $this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias(); |
|
52 | - $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
34 | + /** |
|
35 | + * Gets the SQL string for performing the join between this model and the other model. |
|
36 | + * |
|
37 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
38 | + * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
39 | + * other_model_primary_table.fk" etc |
|
40 | + * @throws \EE_Error |
|
41 | + */ |
|
42 | + public function get_join_statement($model_relation_chain) |
|
43 | + { |
|
44 | + //create the sql string like |
|
45 | + // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions |
|
46 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
47 | + $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
48 | + $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
49 | + $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
50 | + $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
51 | + $this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias(); |
|
52 | + $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
53 | 53 | |
54 | - return $this->_left_join($fk_table, $fk_table_alias, $other_table_fk_field->get_table_column(), $pk_table_alias, |
|
55 | - $this_table_pk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
56 | - } |
|
54 | + return $this->_left_join($fk_table, $fk_table_alias, $other_table_fk_field->get_table_column(), $pk_table_alias, |
|
55 | + $this_table_pk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
56 | + } |
|
57 | 57 | |
58 | 58 | |
59 | - /** |
|
60 | - * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
61 | - * you like. |
|
62 | - * |
|
63 | - * @param EE_Base_Class|int $this_obj_or_id |
|
64 | - * @param EE_Base_Class|int $other_obj_or_id |
|
65 | - * @param array $extra_join_model_fields_n_values |
|
66 | - * @return \EE_Base_Class |
|
67 | - * @throws \EE_Error |
|
68 | - */ |
|
69 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
70 | - { |
|
71 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
72 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
59 | + /** |
|
60 | + * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
61 | + * you like. |
|
62 | + * |
|
63 | + * @param EE_Base_Class|int $this_obj_or_id |
|
64 | + * @param EE_Base_Class|int $other_obj_or_id |
|
65 | + * @param array $extra_join_model_fields_n_values |
|
66 | + * @return \EE_Base_Class |
|
67 | + * @throws \EE_Error |
|
68 | + */ |
|
69 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
70 | + { |
|
71 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
72 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
73 | 73 | |
74 | - //find the field on the other model which is a foreign key to this model |
|
75 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
76 | - if ($other_model_obj->get($fk_field_on_other_model->get_name()) != $this_model_obj->ID()) { |
|
77 | - //set that field on the other model to this model's ID |
|
78 | - $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
79 | - $other_model_obj->save(); |
|
80 | - } |
|
81 | - return $other_model_obj; |
|
82 | - } |
|
74 | + //find the field on the other model which is a foreign key to this model |
|
75 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
76 | + if ($other_model_obj->get($fk_field_on_other_model->get_name()) != $this_model_obj->ID()) { |
|
77 | + //set that field on the other model to this model's ID |
|
78 | + $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
79 | + $other_model_obj->save(); |
|
80 | + } |
|
81 | + return $other_model_obj; |
|
82 | + } |
|
83 | 83 | |
84 | 84 | |
85 | - /** |
|
86 | - * Sets the other model object's foreign key to its default, instead of pointing to this model object. |
|
87 | - * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it |
|
88 | - * |
|
89 | - * @param EE_Base_Class|int $this_obj_or_id |
|
90 | - * @param EE_Base_Class|int $other_obj_or_id |
|
91 | - * @param array $where_query |
|
92 | - * @return \EE_Base_Class |
|
93 | - * @throws \EE_Error |
|
94 | - */ |
|
95 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
96 | - { |
|
97 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
98 | - //find the field on the other model which is a foreign key to this model |
|
99 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
100 | - //set that field on the other model to this model's ID |
|
101 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
102 | - $other_model_obj->save(); |
|
103 | - return $other_model_obj; |
|
104 | - } |
|
85 | + /** |
|
86 | + * Sets the other model object's foreign key to its default, instead of pointing to this model object. |
|
87 | + * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it |
|
88 | + * |
|
89 | + * @param EE_Base_Class|int $this_obj_or_id |
|
90 | + * @param EE_Base_Class|int $other_obj_or_id |
|
91 | + * @param array $where_query |
|
92 | + * @return \EE_Base_Class |
|
93 | + * @throws \EE_Error |
|
94 | + */ |
|
95 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
96 | + { |
|
97 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
98 | + //find the field on the other model which is a foreign key to this model |
|
99 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
100 | + //set that field on the other model to this model's ID |
|
101 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
102 | + $other_model_obj->save(); |
|
103 | + return $other_model_obj; |
|
104 | + } |
|
105 | 105 | } |
@@ -22,83 +22,83 @@ |
||
22 | 22 | class EE_Has_Many_Any_Relation extends EE_Has_Many_Relation |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * Gets the SQL string for performing the join between this model and the other model. |
|
27 | - * |
|
28 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
29 | - * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
30 | - * other_model_primary_table.fk" etc |
|
31 | - * @throws \EE_Error |
|
32 | - */ |
|
33 | - public function get_join_statement($model_relation_chain) |
|
34 | - { |
|
35 | - //create the sql string like |
|
36 | - // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions |
|
37 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
38 | - $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
39 | - $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
40 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
41 | - $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
42 | - $this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias(); |
|
43 | - $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
44 | - $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
|
25 | + /** |
|
26 | + * Gets the SQL string for performing the join between this model and the other model. |
|
27 | + * |
|
28 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
29 | + * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
30 | + * other_model_primary_table.fk" etc |
|
31 | + * @throws \EE_Error |
|
32 | + */ |
|
33 | + public function get_join_statement($model_relation_chain) |
|
34 | + { |
|
35 | + //create the sql string like |
|
36 | + // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions |
|
37 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
38 | + $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
39 | + $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
40 | + $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
41 | + $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
42 | + $this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias(); |
|
43 | + $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
44 | + $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
|
45 | 45 | |
46 | - return $this->_left_join($fk_table, |
|
47 | - $fk_table_alias, |
|
48 | - $other_table_fk_field->get_table_column(), |
|
49 | - $pk_table_alias, |
|
50 | - $this_table_pk_field->get_table_column(), |
|
51 | - $fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") |
|
52 | - . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
53 | - } |
|
46 | + return $this->_left_join($fk_table, |
|
47 | + $fk_table_alias, |
|
48 | + $other_table_fk_field->get_table_column(), |
|
49 | + $pk_table_alias, |
|
50 | + $this_table_pk_field->get_table_column(), |
|
51 | + $fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") |
|
52 | + . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
53 | + } |
|
54 | 54 | |
55 | 55 | |
56 | - /** |
|
57 | - * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
58 | - * you like. |
|
59 | - * |
|
60 | - * @param EE_Base_Class|int $this_obj_or_id |
|
61 | - * @param EE_Base_Class|int $other_obj_or_id |
|
62 | - * @param array $extra_join_model_fields_n_values |
|
63 | - * @return \EE_Base_Class |
|
64 | - * @throws \EE_Error |
|
65 | - */ |
|
66 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
67 | - { |
|
68 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
69 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
56 | + /** |
|
57 | + * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
58 | + * you like. |
|
59 | + * |
|
60 | + * @param EE_Base_Class|int $this_obj_or_id |
|
61 | + * @param EE_Base_Class|int $other_obj_or_id |
|
62 | + * @param array $extra_join_model_fields_n_values |
|
63 | + * @return \EE_Base_Class |
|
64 | + * @throws \EE_Error |
|
65 | + */ |
|
66 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
67 | + { |
|
68 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
69 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
70 | 70 | |
71 | - //find the field on the other model which is a foreign key to this model |
|
72 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
73 | - //set that field on the other model to this model's ID |
|
74 | - $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
75 | - $other_model_obj->set($this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
76 | - $this->get_this_model()->get_this_model_name()); |
|
77 | - $other_model_obj->save(); |
|
78 | - return $other_model_obj; |
|
79 | - } |
|
71 | + //find the field on the other model which is a foreign key to this model |
|
72 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
73 | + //set that field on the other model to this model's ID |
|
74 | + $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
75 | + $other_model_obj->set($this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
76 | + $this->get_this_model()->get_this_model_name()); |
|
77 | + $other_model_obj->save(); |
|
78 | + return $other_model_obj; |
|
79 | + } |
|
80 | 80 | |
81 | 81 | |
82 | - /** |
|
83 | - * Sets the other model object's foreign key to its default, instead of pointing to this model object. |
|
84 | - * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it |
|
85 | - * |
|
86 | - * @param EE_Base_Class|int $this_obj_or_id |
|
87 | - * @param EE_Base_Class|int $other_obj_or_id |
|
88 | - * @param array $where_query |
|
89 | - * @return \EE_Base_Class |
|
90 | - * @throws \EE_Error |
|
91 | - */ |
|
92 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
93 | - { |
|
94 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
95 | - //find the field on the other model which is a foreign key to this model |
|
96 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
97 | - //set that field on the other model to this model's ID |
|
98 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
99 | - $other_model_obj->set($this->get_other_model()->get_field_containing_related_model_name()->get_name(), null, |
|
100 | - true); |
|
101 | - $other_model_obj->save(); |
|
102 | - return $other_model_obj; |
|
103 | - } |
|
82 | + /** |
|
83 | + * Sets the other model object's foreign key to its default, instead of pointing to this model object. |
|
84 | + * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it |
|
85 | + * |
|
86 | + * @param EE_Base_Class|int $this_obj_or_id |
|
87 | + * @param EE_Base_Class|int $other_obj_or_id |
|
88 | + * @param array $where_query |
|
89 | + * @return \EE_Base_Class |
|
90 | + * @throws \EE_Error |
|
91 | + */ |
|
92 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
93 | + { |
|
94 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
95 | + //find the field on the other model which is a foreign key to this model |
|
96 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
97 | + //set that field on the other model to this model's ID |
|
98 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
99 | + $other_model_obj->set($this->get_other_model()->get_field_containing_related_model_name()->get_name(), null, |
|
100 | + true); |
|
101 | + $other_model_obj->save(); |
|
102 | + return $other_model_obj; |
|
103 | + } |
|
104 | 104 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | /* |
5 | 5 | * |
6 | 6 | */ |
7 | -require_once(EE_MODELS . 'relations/EE_Model_Relation_Base.php'); |
|
7 | +require_once(EE_MODELS.'relations/EE_Model_Relation_Base.php'); |
|
8 | 8 | |
9 | 9 | |
10 | 10 | /** |
@@ -37,9 +37,9 @@ discard block |
||
37 | 37 | $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
38 | 38 | $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
39 | 39 | $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
40 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
40 | + $this->get_this_model()->get_this_model_name()).$this_table_pk_field->get_table_alias(); |
|
41 | 41 | $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
42 | - $this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias(); |
|
42 | + $this->get_other_model()->get_this_model_name()).$other_table_fk_field->get_table_alias(); |
|
43 | 43 | $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
44 | 44 | $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
45 | 45 | |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | $other_table_fk_field->get_table_column(), |
49 | 49 | $pk_table_alias, |
50 | 50 | $this_table_pk_field->get_table_column(), |
51 | - $fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") |
|
51 | + $fk_table_alias.'.'.$field_with_model_name->get_table_column()."='".$this->get_this_model()->get_this_model_name()."'") |
|
52 | 52 | . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
53 | 53 | } |
54 | 54 |
@@ -14,303 +14,303 @@ |
||
14 | 14 | { |
15 | 15 | |
16 | 16 | |
17 | - /** |
|
18 | - * The Foreign key on the model that acts as the PRIMARY KEY used in special autosave handling where we query for |
|
19 | - * autosaves (or the Foreign key on other models in relations pointing to this models primary key which is this |
|
20 | - * value). The _primary_cpt_field is what is equivalent to the post_id field on a cpt join. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - private $_primary_cpt_field; |
|
25 | - |
|
26 | - |
|
27 | - /** |
|
28 | - * This is what field serves as the "parent" column that is linked with whatever the main model's calling this |
|
29 | - * relation has as a primary key. In other words EEM_Event has 'Datetime' => new |
|
30 | - * EE_Has_Many_Revision_Relation('EVT_ID', 'DTT_parent'). That means that in the EEM_Datetime model the |
|
31 | - * 'DTT_Parent' field is related to the 'DTT_ID' primary key field (in the same model) because 'DTT_ID' is the |
|
32 | - * primary key in the other model (EEM_Datetime). |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - private $_parent_pk_relation_field; |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the |
|
41 | - * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but |
|
42 | - * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get |
|
43 | - * related models across the relation, and add-and-remove the relationships. |
|
44 | - * |
|
45 | - * @param string $primary_cpt_field See property description for details |
|
46 | - * @param string $parent_pk_relation_field This is the field that is "connected" to the $primary_cpt_field. |
|
47 | - * See property desc for details. |
|
48 | - * @param boolean $block_deletes For this type of relation, we block by default. If there are |
|
49 | - * related models across this relation, block (prevent and add an |
|
50 | - * error) the deletion of this model |
|
51 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
52 | - * default |
|
53 | - */ |
|
54 | - public function __construct( |
|
55 | - $primary_cpt_field, |
|
56 | - $parent_pk_relation_field, |
|
57 | - $block_deletes = true, |
|
58 | - $blocking_delete_error_message = null |
|
59 | - ) { |
|
60 | - $this->_primary_cpt_field = $primary_cpt_field; |
|
61 | - $this->_parent_pk_relation_field = $parent_pk_relation_field; |
|
62 | - parent::__construct($block_deletes, $blocking_delete_error_message); |
|
63 | - } |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
68 | - * you like. |
|
69 | - * |
|
70 | - * @param EE_Base_Class|int $this_obj_or_id |
|
71 | - * @param EE_Base_Class|int $other_obj_or_id |
|
72 | - * @param array $extra_join_model_fields_n_values |
|
73 | - * @return \EE_Base_Class |
|
74 | - * @throws \EE_Error |
|
75 | - */ |
|
76 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
77 | - { |
|
78 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
79 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
80 | - |
|
81 | - //handle possible revisions |
|
82 | - $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj); |
|
83 | - |
|
84 | - //if is array, then we've already done the add_relation so let's get out |
|
85 | - if (is_array($other_model_obj)) { |
|
86 | - return $other_model_obj[0]; |
|
87 | - } |
|
88 | - //find the field on the other model which is a foreign key to this model |
|
89 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
90 | - //set that field on the other model to this model's ID |
|
91 | - $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
92 | - $other_model_obj->save(); |
|
93 | - return $other_model_obj; |
|
94 | - } |
|
95 | - |
|
96 | - |
|
97 | - /** |
|
98 | - * Sets the other model object's foreign key to its default, instead of pointing to this model object |
|
99 | - * |
|
100 | - * @param EE_Base_Class|int $this_obj_or_id |
|
101 | - * @param EE_Base_Class|int $other_obj_or_id |
|
102 | - * @param array $where_query |
|
103 | - * @return \EE_Base_Class |
|
104 | - * @throws \EE_Error |
|
105 | - */ |
|
106 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
107 | - { |
|
108 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id); |
|
109 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
110 | - //handle possible revisions |
|
111 | - $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj, true); |
|
112 | - |
|
113 | - |
|
114 | - //if is array, then we've already done the add_relation so let's get out |
|
115 | - if (is_array($other_model_obj)) { |
|
116 | - return $other_model_obj[0]; |
|
117 | - } |
|
118 | - |
|
119 | - //find the field on the other model which is a foreign key to this model |
|
120 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
121 | - |
|
122 | - |
|
123 | - //set that field on the other model to this model's ID |
|
124 | - if ($this->_blocking_delete) { |
|
125 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
126 | - $other_model_obj->save(); |
|
127 | - } else { |
|
128 | - $other_model_obj->delete(); |
|
129 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
130 | - return $other_model_obj; |
|
131 | - } |
|
132 | - return $other_model_obj; |
|
133 | - } |
|
134 | - |
|
135 | - |
|
136 | - /** |
|
137 | - * This is identical to EE_Model_Relation->get_all_related() except we're going handle special autosave conditions |
|
138 | - * in here. |
|
139 | - * |
|
140 | - * @param EE_Base_Class|int $model_object_or_id |
|
141 | - * @param array $query_params like EEM_Base::get_all's $query_params |
|
142 | - * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
143 | - * @return EE_Base_Class[] |
|
144 | - * @throws \EE_Error |
|
145 | - */ |
|
146 | - public function get_all_related( |
|
147 | - $model_object_or_id, |
|
148 | - $query_params = array(), |
|
149 | - $values_already_prepared_by_model_object = false |
|
150 | - ) { |
|
151 | - if ($values_already_prepared_by_model_object !== false) { |
|
152 | - EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
153 | - __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
154 | - '4.8.1'); |
|
155 | - } |
|
156 | - |
|
157 | - //if this is an autosave then we're going to get things differently |
|
158 | - if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
159 | - return $this->_do_autosave_get_all($model_object_or_id, $query_params); |
|
160 | - } |
|
161 | - |
|
162 | - return parent::get_all_related($model_object_or_id, $query_params); |
|
163 | - } |
|
164 | - |
|
165 | - |
|
166 | - /** |
|
167 | - * If we're in the midst of an autosave then we're going to do things a bit differently than the usual |
|
168 | - * get_all_related (commenting within). For description of params see the get_all_related() comments |
|
169 | - * |
|
170 | - * @access protected |
|
171 | - * @param $model_object_or_id |
|
172 | - * @param $query_params |
|
173 | - * @param bool $deprecated |
|
174 | - * @return \EE_Base_Class[] |
|
175 | - * @throws \EE_Error |
|
176 | - */ |
|
177 | - protected function _do_autosave_get_all($model_object_or_id, $query_params, $deprecated = false) |
|
178 | - { |
|
179 | - |
|
180 | - //first we check if the post_id for the incoming query is for an autosave. If it isn't that's what we want! |
|
181 | - $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
182 | - |
|
183 | - $autosave = wp_get_post_autosave($model_object_id); |
|
184 | - $id_to_use = $autosave ? $autosave->ID : $model_object_id; |
|
185 | - |
|
186 | - $autosave_relations = parent::get_all_related($id_to_use, $query_params); |
|
187 | - $parent_ids = $parents = array(); |
|
188 | - $return_objs = array(); |
|
189 | - |
|
190 | - //k this is where things differ because NOW what we're going to do is get the PARENTS for the get all related (and we'll also start setting up the return_objs array containing related that DON'T have parent ids, for those that DON'T have parents to merge with our returned objects); |
|
191 | - foreach ($autosave_relations as $a_r) { |
|
192 | - $pid = $a_r->parent(); |
|
193 | - if (! empty($pid)) { |
|
194 | - $parent_ids[] = $pid; |
|
195 | - } else { |
|
196 | - $return_objs[] = $a_r; |
|
197 | - } |
|
198 | - } |
|
199 | - |
|
200 | - //we have to make sure we also include the ORIGINAL values |
|
201 | - $originals = parent::get_all_related($model_object_or_id, $query_params); |
|
202 | - |
|
203 | - //merge $originals with $return_objs |
|
204 | - if ($originals) { |
|
205 | - $return_objs = array_merge($originals, $return_objs); |
|
206 | - } |
|
207 | - |
|
208 | - //now we setup the query to get all the parents |
|
209 | - if (! empty($parent_ids)) { |
|
210 | - $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() . "." . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
211 | - $query_param[0][$query_param_where_this_model_pk] = array('IN', $parent_ids); |
|
212 | - $parents = $this->get_other_model()->get_all($query_params); |
|
213 | - } |
|
214 | - |
|
215 | - //var_dump($parents); |
|
216 | - |
|
217 | - |
|
218 | - //now merge parents with our current $return_objs and send back |
|
219 | - return array_merge($parents, $return_objs); |
|
220 | - } |
|
221 | - |
|
222 | - |
|
223 | - /** |
|
224 | - * Basically this method gets called to verify if the incoming object needs to be manipulated somewhat because it |
|
225 | - * is a revision save. If so, then we change things before sending back. We also do verifications when this IS |
|
226 | - * NOT an revision because we always need to make sure that the autosave/revision has parent recorded (which is |
|
227 | - * sometime delayed if the object is created/saved first by the autosave) |
|
228 | - * |
|
229 | - * @param EE_Base_Class $this_obj |
|
230 | - * @param EE_Base_Class $other_obj |
|
231 | - * @param boolean $remove_relation Indicates whether we're doing a remove_relation or add_relation. |
|
232 | - * @return EE_Base_Class. ($other_obj); |
|
233 | - * @throws \EE_Error |
|
234 | - */ |
|
235 | - protected function _check_for_revision($this_obj, $other_obj, $remove_relation = false) |
|
236 | - { |
|
237 | - $pk_on_related_model = $this->get_other_model()->get_primary_key_field()->get_name(); |
|
238 | - //now we need to determine if we're in a WP revision save cause if we are we need to do some special handling |
|
239 | - if ($this_obj->post_type() === 'revision') { |
|
240 | - //first if $other_obj fk = this_obj pk then we know that this is a pk object, let's make sure there is a matching set for the autosave if there is then we save over it, if there isn't then we need to create a new one. |
|
241 | - $parent_evt_id = $this_obj->parent(); |
|
242 | - /*var_dump($parent_evt_id); |
|
17 | + /** |
|
18 | + * The Foreign key on the model that acts as the PRIMARY KEY used in special autosave handling where we query for |
|
19 | + * autosaves (or the Foreign key on other models in relations pointing to this models primary key which is this |
|
20 | + * value). The _primary_cpt_field is what is equivalent to the post_id field on a cpt join. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + private $_primary_cpt_field; |
|
25 | + |
|
26 | + |
|
27 | + /** |
|
28 | + * This is what field serves as the "parent" column that is linked with whatever the main model's calling this |
|
29 | + * relation has as a primary key. In other words EEM_Event has 'Datetime' => new |
|
30 | + * EE_Has_Many_Revision_Relation('EVT_ID', 'DTT_parent'). That means that in the EEM_Datetime model the |
|
31 | + * 'DTT_Parent' field is related to the 'DTT_ID' primary key field (in the same model) because 'DTT_ID' is the |
|
32 | + * primary key in the other model (EEM_Datetime). |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + private $_parent_pk_relation_field; |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the |
|
41 | + * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but |
|
42 | + * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get |
|
43 | + * related models across the relation, and add-and-remove the relationships. |
|
44 | + * |
|
45 | + * @param string $primary_cpt_field See property description for details |
|
46 | + * @param string $parent_pk_relation_field This is the field that is "connected" to the $primary_cpt_field. |
|
47 | + * See property desc for details. |
|
48 | + * @param boolean $block_deletes For this type of relation, we block by default. If there are |
|
49 | + * related models across this relation, block (prevent and add an |
|
50 | + * error) the deletion of this model |
|
51 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
52 | + * default |
|
53 | + */ |
|
54 | + public function __construct( |
|
55 | + $primary_cpt_field, |
|
56 | + $parent_pk_relation_field, |
|
57 | + $block_deletes = true, |
|
58 | + $blocking_delete_error_message = null |
|
59 | + ) { |
|
60 | + $this->_primary_cpt_field = $primary_cpt_field; |
|
61 | + $this->_parent_pk_relation_field = $parent_pk_relation_field; |
|
62 | + parent::__construct($block_deletes, $blocking_delete_error_message); |
|
63 | + } |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
68 | + * you like. |
|
69 | + * |
|
70 | + * @param EE_Base_Class|int $this_obj_or_id |
|
71 | + * @param EE_Base_Class|int $other_obj_or_id |
|
72 | + * @param array $extra_join_model_fields_n_values |
|
73 | + * @return \EE_Base_Class |
|
74 | + * @throws \EE_Error |
|
75 | + */ |
|
76 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
77 | + { |
|
78 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
79 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
80 | + |
|
81 | + //handle possible revisions |
|
82 | + $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj); |
|
83 | + |
|
84 | + //if is array, then we've already done the add_relation so let's get out |
|
85 | + if (is_array($other_model_obj)) { |
|
86 | + return $other_model_obj[0]; |
|
87 | + } |
|
88 | + //find the field on the other model which is a foreign key to this model |
|
89 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
90 | + //set that field on the other model to this model's ID |
|
91 | + $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
92 | + $other_model_obj->save(); |
|
93 | + return $other_model_obj; |
|
94 | + } |
|
95 | + |
|
96 | + |
|
97 | + /** |
|
98 | + * Sets the other model object's foreign key to its default, instead of pointing to this model object |
|
99 | + * |
|
100 | + * @param EE_Base_Class|int $this_obj_or_id |
|
101 | + * @param EE_Base_Class|int $other_obj_or_id |
|
102 | + * @param array $where_query |
|
103 | + * @return \EE_Base_Class |
|
104 | + * @throws \EE_Error |
|
105 | + */ |
|
106 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
107 | + { |
|
108 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id); |
|
109 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
110 | + //handle possible revisions |
|
111 | + $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj, true); |
|
112 | + |
|
113 | + |
|
114 | + //if is array, then we've already done the add_relation so let's get out |
|
115 | + if (is_array($other_model_obj)) { |
|
116 | + return $other_model_obj[0]; |
|
117 | + } |
|
118 | + |
|
119 | + //find the field on the other model which is a foreign key to this model |
|
120 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
121 | + |
|
122 | + |
|
123 | + //set that field on the other model to this model's ID |
|
124 | + if ($this->_blocking_delete) { |
|
125 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
126 | + $other_model_obj->save(); |
|
127 | + } else { |
|
128 | + $other_model_obj->delete(); |
|
129 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
130 | + return $other_model_obj; |
|
131 | + } |
|
132 | + return $other_model_obj; |
|
133 | + } |
|
134 | + |
|
135 | + |
|
136 | + /** |
|
137 | + * This is identical to EE_Model_Relation->get_all_related() except we're going handle special autosave conditions |
|
138 | + * in here. |
|
139 | + * |
|
140 | + * @param EE_Base_Class|int $model_object_or_id |
|
141 | + * @param array $query_params like EEM_Base::get_all's $query_params |
|
142 | + * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
143 | + * @return EE_Base_Class[] |
|
144 | + * @throws \EE_Error |
|
145 | + */ |
|
146 | + public function get_all_related( |
|
147 | + $model_object_or_id, |
|
148 | + $query_params = array(), |
|
149 | + $values_already_prepared_by_model_object = false |
|
150 | + ) { |
|
151 | + if ($values_already_prepared_by_model_object !== false) { |
|
152 | + EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
153 | + __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
154 | + '4.8.1'); |
|
155 | + } |
|
156 | + |
|
157 | + //if this is an autosave then we're going to get things differently |
|
158 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
159 | + return $this->_do_autosave_get_all($model_object_or_id, $query_params); |
|
160 | + } |
|
161 | + |
|
162 | + return parent::get_all_related($model_object_or_id, $query_params); |
|
163 | + } |
|
164 | + |
|
165 | + |
|
166 | + /** |
|
167 | + * If we're in the midst of an autosave then we're going to do things a bit differently than the usual |
|
168 | + * get_all_related (commenting within). For description of params see the get_all_related() comments |
|
169 | + * |
|
170 | + * @access protected |
|
171 | + * @param $model_object_or_id |
|
172 | + * @param $query_params |
|
173 | + * @param bool $deprecated |
|
174 | + * @return \EE_Base_Class[] |
|
175 | + * @throws \EE_Error |
|
176 | + */ |
|
177 | + protected function _do_autosave_get_all($model_object_or_id, $query_params, $deprecated = false) |
|
178 | + { |
|
179 | + |
|
180 | + //first we check if the post_id for the incoming query is for an autosave. If it isn't that's what we want! |
|
181 | + $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
182 | + |
|
183 | + $autosave = wp_get_post_autosave($model_object_id); |
|
184 | + $id_to_use = $autosave ? $autosave->ID : $model_object_id; |
|
185 | + |
|
186 | + $autosave_relations = parent::get_all_related($id_to_use, $query_params); |
|
187 | + $parent_ids = $parents = array(); |
|
188 | + $return_objs = array(); |
|
189 | + |
|
190 | + //k this is where things differ because NOW what we're going to do is get the PARENTS for the get all related (and we'll also start setting up the return_objs array containing related that DON'T have parent ids, for those that DON'T have parents to merge with our returned objects); |
|
191 | + foreach ($autosave_relations as $a_r) { |
|
192 | + $pid = $a_r->parent(); |
|
193 | + if (! empty($pid)) { |
|
194 | + $parent_ids[] = $pid; |
|
195 | + } else { |
|
196 | + $return_objs[] = $a_r; |
|
197 | + } |
|
198 | + } |
|
199 | + |
|
200 | + //we have to make sure we also include the ORIGINAL values |
|
201 | + $originals = parent::get_all_related($model_object_or_id, $query_params); |
|
202 | + |
|
203 | + //merge $originals with $return_objs |
|
204 | + if ($originals) { |
|
205 | + $return_objs = array_merge($originals, $return_objs); |
|
206 | + } |
|
207 | + |
|
208 | + //now we setup the query to get all the parents |
|
209 | + if (! empty($parent_ids)) { |
|
210 | + $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() . "." . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
211 | + $query_param[0][$query_param_where_this_model_pk] = array('IN', $parent_ids); |
|
212 | + $parents = $this->get_other_model()->get_all($query_params); |
|
213 | + } |
|
214 | + |
|
215 | + //var_dump($parents); |
|
216 | + |
|
217 | + |
|
218 | + //now merge parents with our current $return_objs and send back |
|
219 | + return array_merge($parents, $return_objs); |
|
220 | + } |
|
221 | + |
|
222 | + |
|
223 | + /** |
|
224 | + * Basically this method gets called to verify if the incoming object needs to be manipulated somewhat because it |
|
225 | + * is a revision save. If so, then we change things before sending back. We also do verifications when this IS |
|
226 | + * NOT an revision because we always need to make sure that the autosave/revision has parent recorded (which is |
|
227 | + * sometime delayed if the object is created/saved first by the autosave) |
|
228 | + * |
|
229 | + * @param EE_Base_Class $this_obj |
|
230 | + * @param EE_Base_Class $other_obj |
|
231 | + * @param boolean $remove_relation Indicates whether we're doing a remove_relation or add_relation. |
|
232 | + * @return EE_Base_Class. ($other_obj); |
|
233 | + * @throws \EE_Error |
|
234 | + */ |
|
235 | + protected function _check_for_revision($this_obj, $other_obj, $remove_relation = false) |
|
236 | + { |
|
237 | + $pk_on_related_model = $this->get_other_model()->get_primary_key_field()->get_name(); |
|
238 | + //now we need to determine if we're in a WP revision save cause if we are we need to do some special handling |
|
239 | + if ($this_obj->post_type() === 'revision') { |
|
240 | + //first if $other_obj fk = this_obj pk then we know that this is a pk object, let's make sure there is a matching set for the autosave if there is then we save over it, if there isn't then we need to create a new one. |
|
241 | + $parent_evt_id = $this_obj->parent(); |
|
242 | + /*var_dump($parent_evt_id); |
|
243 | 243 | var_dump($this_obj); |
244 | 244 | var_dump($other_obj);/**/ |
245 | 245 | |
246 | - if (! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
247 | - //let's do query on this objects model to see if the incoming pk value on the obj matches any parents in this objects table. |
|
248 | - $has_parent_obj = $this->get_other_model()->get_one(array( |
|
249 | - array( |
|
250 | - $this->_parent_pk_relation_field => $other_obj->ID(), |
|
251 | - $this->_primary_cpt_field => $this_obj->ID(), |
|
252 | - ), |
|
253 | - )); |
|
254 | - |
|
255 | - if ($has_parent_obj) { |
|
256 | - //this makes sure the update on the current obj happens to the revision's row NOT the parent row. |
|
257 | - |
|
258 | - $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
259 | - $other_obj->set($pk_on_related_model, $has_parent_obj->ID()); |
|
260 | - $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
261 | - |
|
262 | - if (! $remove_relation) { |
|
263 | - $other_obj->save(); |
|
264 | - return array($other_obj); |
|
265 | - } elseif ($remove_relation && ! $this->_blocking_delete) { |
|
266 | - $other_obj->delete(); |
|
267 | - $other_obj->set($this->_parent_pk_relation_field, null, true); |
|
268 | - return array($other_obj); |
|
269 | - } |
|
270 | - |
|
271 | - } else { |
|
272 | - $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
273 | - $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
274 | - $other_obj->set($pk_on_related_model, null, |
|
275 | - true); //ensure we create a new row for the autosave with parent id the same as the incoming ID. |
|
276 | - $other_obj->save(); //make sure we insert. |
|
277 | - return array($other_obj); |
|
278 | - } |
|
279 | - } |
|
280 | - |
|
281 | - //var_dump('what makes it here'); |
|
282 | - //var_dump($other_obj); |
|
283 | - //the next possible condition is that the incoming other_model obj has a NULL pk which means it just gets saved (which in turn creates it) |
|
284 | - |
|
285 | - //the last possible condition on a revision is that the incoming other_model object has a fk that == $this_obj pk which means we just return the $other obj and let it save as normal so we see the return at the bottom of this method. |
|
286 | - |
|
287 | - } else { |
|
288 | - |
|
289 | - //we only need to do the below IF this is not a remove relation |
|
290 | - if (! $remove_relation) { |
|
291 | - //okay this is is a normal update/save/remove so, let's make sure the other object is not a revision of the current object. |
|
292 | - //the other object will likely NOT have the correct fk on it (which is the primary_cpt_field_mame) so we must retrieve from the db to get that first. |
|
293 | - $existing_other_obj = $this->get_other_model()->get_one_by_ID($other_obj->ID()); |
|
294 | - $potential_revision_id = is_object($existing_other_obj) ? $existing_other_obj->get($this->_primary_cpt_field) : null; |
|
295 | - |
|
296 | - if ($parent_this_obj_id = wp_is_post_revision($potential_revision_id)) { |
|
297 | - //yes the OTHER object is linked to the revision of the parent, not the parent itself. That means we need to make the other_object an attachment of this_obj and then duplicate other_obj for the revision. |
|
298 | - $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
299 | - $other_obj->save(); |
|
300 | - |
|
301 | - //now create a new other_obj and fill with details from existing object |
|
302 | - $new_obj = $other_obj; |
|
303 | - $new_obj->set($this->_primary_cpt_field, $potential_revision_id); |
|
304 | - $new_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
305 | - $new_obj->set($pk_on_related_model, null); |
|
306 | - $new_obj->save(); |
|
307 | - return array($new_obj); |
|
308 | - } |
|
309 | - |
|
310 | - } |
|
311 | - } |
|
312 | - |
|
313 | - return $other_obj; |
|
314 | - } |
|
246 | + if (! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
247 | + //let's do query on this objects model to see if the incoming pk value on the obj matches any parents in this objects table. |
|
248 | + $has_parent_obj = $this->get_other_model()->get_one(array( |
|
249 | + array( |
|
250 | + $this->_parent_pk_relation_field => $other_obj->ID(), |
|
251 | + $this->_primary_cpt_field => $this_obj->ID(), |
|
252 | + ), |
|
253 | + )); |
|
254 | + |
|
255 | + if ($has_parent_obj) { |
|
256 | + //this makes sure the update on the current obj happens to the revision's row NOT the parent row. |
|
257 | + |
|
258 | + $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
259 | + $other_obj->set($pk_on_related_model, $has_parent_obj->ID()); |
|
260 | + $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
261 | + |
|
262 | + if (! $remove_relation) { |
|
263 | + $other_obj->save(); |
|
264 | + return array($other_obj); |
|
265 | + } elseif ($remove_relation && ! $this->_blocking_delete) { |
|
266 | + $other_obj->delete(); |
|
267 | + $other_obj->set($this->_parent_pk_relation_field, null, true); |
|
268 | + return array($other_obj); |
|
269 | + } |
|
270 | + |
|
271 | + } else { |
|
272 | + $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
273 | + $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
274 | + $other_obj->set($pk_on_related_model, null, |
|
275 | + true); //ensure we create a new row for the autosave with parent id the same as the incoming ID. |
|
276 | + $other_obj->save(); //make sure we insert. |
|
277 | + return array($other_obj); |
|
278 | + } |
|
279 | + } |
|
280 | + |
|
281 | + //var_dump('what makes it here'); |
|
282 | + //var_dump($other_obj); |
|
283 | + //the next possible condition is that the incoming other_model obj has a NULL pk which means it just gets saved (which in turn creates it) |
|
284 | + |
|
285 | + //the last possible condition on a revision is that the incoming other_model object has a fk that == $this_obj pk which means we just return the $other obj and let it save as normal so we see the return at the bottom of this method. |
|
286 | + |
|
287 | + } else { |
|
288 | + |
|
289 | + //we only need to do the below IF this is not a remove relation |
|
290 | + if (! $remove_relation) { |
|
291 | + //okay this is is a normal update/save/remove so, let's make sure the other object is not a revision of the current object. |
|
292 | + //the other object will likely NOT have the correct fk on it (which is the primary_cpt_field_mame) so we must retrieve from the db to get that first. |
|
293 | + $existing_other_obj = $this->get_other_model()->get_one_by_ID($other_obj->ID()); |
|
294 | + $potential_revision_id = is_object($existing_other_obj) ? $existing_other_obj->get($this->_primary_cpt_field) : null; |
|
295 | + |
|
296 | + if ($parent_this_obj_id = wp_is_post_revision($potential_revision_id)) { |
|
297 | + //yes the OTHER object is linked to the revision of the parent, not the parent itself. That means we need to make the other_object an attachment of this_obj and then duplicate other_obj for the revision. |
|
298 | + $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
299 | + $other_obj->save(); |
|
300 | + |
|
301 | + //now create a new other_obj and fill with details from existing object |
|
302 | + $new_obj = $other_obj; |
|
303 | + $new_obj->set($this->_primary_cpt_field, $potential_revision_id); |
|
304 | + $new_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
305 | + $new_obj->set($pk_on_related_model, null); |
|
306 | + $new_obj->save(); |
|
307 | + return array($new_obj); |
|
308 | + } |
|
309 | + |
|
310 | + } |
|
311 | + } |
|
312 | + |
|
313 | + return $other_obj; |
|
314 | + } |
|
315 | 315 | |
316 | 316 | } |
317 | 317 | \ No newline at end of file |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -require_once(EE_MODELS . 'relations/EE_Has_Many_Relation.php'); |
|
2 | +require_once(EE_MODELS.'relations/EE_Has_Many_Relation.php'); |
|
3 | 3 | |
4 | 4 | |
5 | 5 | /** |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | //k this is where things differ because NOW what we're going to do is get the PARENTS for the get all related (and we'll also start setting up the return_objs array containing related that DON'T have parent ids, for those that DON'T have parents to merge with our returned objects); |
191 | 191 | foreach ($autosave_relations as $a_r) { |
192 | 192 | $pid = $a_r->parent(); |
193 | - if (! empty($pid)) { |
|
193 | + if ( ! empty($pid)) { |
|
194 | 194 | $parent_ids[] = $pid; |
195 | 195 | } else { |
196 | 196 | $return_objs[] = $a_r; |
@@ -206,8 +206,8 @@ discard block |
||
206 | 206 | } |
207 | 207 | |
208 | 208 | //now we setup the query to get all the parents |
209 | - if (! empty($parent_ids)) { |
|
210 | - $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() . "." . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
209 | + if ( ! empty($parent_ids)) { |
|
210 | + $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name().".".$this->get_this_model()->get_primary_key_field()->get_name(); |
|
211 | 211 | $query_param[0][$query_param_where_this_model_pk] = array('IN', $parent_ids); |
212 | 212 | $parents = $this->get_other_model()->get_all($query_params); |
213 | 213 | } |
@@ -243,7 +243,7 @@ discard block |
||
243 | 243 | var_dump($this_obj); |
244 | 244 | var_dump($other_obj);/**/ |
245 | 245 | |
246 | - if (! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
246 | + if ( ! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
247 | 247 | //let's do query on this objects model to see if the incoming pk value on the obj matches any parents in this objects table. |
248 | 248 | $has_parent_obj = $this->get_other_model()->get_one(array( |
249 | 249 | array( |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | $other_obj->set($pk_on_related_model, $has_parent_obj->ID()); |
260 | 260 | $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
261 | 261 | |
262 | - if (! $remove_relation) { |
|
262 | + if ( ! $remove_relation) { |
|
263 | 263 | $other_obj->save(); |
264 | 264 | return array($other_obj); |
265 | 265 | } elseif ($remove_relation && ! $this->_blocking_delete) { |
@@ -287,7 +287,7 @@ discard block |
||
287 | 287 | } else { |
288 | 288 | |
289 | 289 | //we only need to do the below IF this is not a remove relation |
290 | - if (! $remove_relation) { |
|
290 | + if ( ! $remove_relation) { |
|
291 | 291 | //okay this is is a normal update/save/remove so, let's make sure the other object is not a revision of the current object. |
292 | 292 | //the other object will likely NOT have the correct fk on it (which is the primary_cpt_field_mame) so we must retrieve from the db to get that first. |
293 | 293 | $existing_other_obj = $this->get_other_model()->get_one_by_ID($other_obj->ID()); |
@@ -10,264 +10,264 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_HABTM_Any_Relation extends EE_HABTM_Relation |
12 | 12 | { |
13 | - /** |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $_alphabetically_first_model_name; |
|
13 | + /** |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $_alphabetically_first_model_name; |
|
17 | 17 | |
18 | - /** |
|
19 | - * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
20 | - * (and an ee joining-model.) This knows how to join the models, |
|
21 | - * get related models across the relation, and add-and-remove the relationships. |
|
22 | - * |
|
23 | - * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
24 | - * are related models across this relation, block (prevent and add an |
|
25 | - * error) the deletion of this model |
|
26 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
27 | - * default |
|
28 | - */ |
|
29 | - public function __construct($block_deletes = true, $blocking_delete_error_message = '') |
|
30 | - { |
|
31 | - parent::__construct('Extra_Join', $block_deletes, $blocking_delete_error_message); |
|
32 | - } |
|
18 | + /** |
|
19 | + * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
20 | + * (and an ee joining-model.) This knows how to join the models, |
|
21 | + * get related models across the relation, and add-and-remove the relationships. |
|
22 | + * |
|
23 | + * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
24 | + * are related models across this relation, block (prevent and add an |
|
25 | + * error) the deletion of this model |
|
26 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
27 | + * default |
|
28 | + */ |
|
29 | + public function __construct($block_deletes = true, $blocking_delete_error_message = '') |
|
30 | + { |
|
31 | + parent::__construct('Extra_Join', $block_deletes, $blocking_delete_error_message); |
|
32 | + } |
|
33 | 33 | |
34 | 34 | |
35 | - /** |
|
36 | - * @param $this_model_name |
|
37 | - * @param $other_model_name |
|
38 | - * @throws EE_Error |
|
39 | - */ |
|
40 | - public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
41 | - { |
|
42 | - if ($this_model_name < $other_model_name) { |
|
43 | - $this->_alphabetically_first_model_name = $this_model_name; |
|
44 | - } else { |
|
45 | - $this->_alphabetically_first_model_name = $other_model_name; |
|
46 | - } |
|
47 | - parent::_construct_finalize_set_models($this_model_name, $other_model_name); |
|
48 | - } |
|
35 | + /** |
|
36 | + * @param $this_model_name |
|
37 | + * @param $other_model_name |
|
38 | + * @throws EE_Error |
|
39 | + */ |
|
40 | + public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
41 | + { |
|
42 | + if ($this_model_name < $other_model_name) { |
|
43 | + $this->_alphabetically_first_model_name = $this_model_name; |
|
44 | + } else { |
|
45 | + $this->_alphabetically_first_model_name = $other_model_name; |
|
46 | + } |
|
47 | + parent::_construct_finalize_set_models($this_model_name, $other_model_name); |
|
48 | + } |
|
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * @param string $model_name |
|
53 | - * @param string $id_or_name_field should be the string 'ID' or 'name' only |
|
54 | - * @return EE_Model_Field_Base |
|
55 | - * @throws \EE_Error |
|
56 | - */ |
|
57 | - public function get_join_table_fk_field_to($model_name, $id_or_name_field) |
|
58 | - { |
|
59 | - $order = null; |
|
60 | - if ($model_name === $this->_alphabetically_first_model_name) { |
|
61 | - $order = 'first'; |
|
62 | - } else { |
|
63 | - $order = 'second'; |
|
64 | - } |
|
65 | - return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
66 | - } |
|
51 | + /** |
|
52 | + * @param string $model_name |
|
53 | + * @param string $id_or_name_field should be the string 'ID' or 'name' only |
|
54 | + * @return EE_Model_Field_Base |
|
55 | + * @throws \EE_Error |
|
56 | + */ |
|
57 | + public function get_join_table_fk_field_to($model_name, $id_or_name_field) |
|
58 | + { |
|
59 | + $order = null; |
|
60 | + if ($model_name === $this->_alphabetically_first_model_name) { |
|
61 | + $order = 'first'; |
|
62 | + } else { |
|
63 | + $order = 'second'; |
|
64 | + } |
|
65 | + return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
66 | + } |
|
67 | 67 | |
68 | 68 | |
69 | - /** |
|
70 | - * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
71 | - * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
72 | - * |
|
73 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
74 | - * @return string of SQL |
|
75 | - * @throws \EE_Error |
|
76 | - */ |
|
77 | - public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
78 | - { |
|
79 | - //create sql like |
|
80 | - //LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
81 | - //LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
82 | - //remember the model relation chain to the JOIN model, because we'll |
|
83 | - //need it for get_join_statement() |
|
84 | - $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
85 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
86 | - $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
87 | - $this->get_this_model()->get_this_model_name(), |
|
88 | - 'ID'); |
|
89 | - $field_with_model_name = $this->get_join_table_fk_field_to( |
|
90 | - $this->get_this_model()->get_this_model_name(), |
|
91 | - 'name'); |
|
92 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
93 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
94 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
95 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
96 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
97 | - //phew! ok, we have all the info we need, now we can create the SQL join string |
|
98 | - $SQL = $this->_left_join( |
|
99 | - $join_table, |
|
100 | - $join_table_alias, |
|
101 | - $join_table_fk_field_to_this_table->get_table_column(), |
|
102 | - $this_table_alias, |
|
103 | - $this_table_pk_field->get_table_column(), |
|
104 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") . |
|
105 | - $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
69 | + /** |
|
70 | + * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
71 | + * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
72 | + * |
|
73 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
74 | + * @return string of SQL |
|
75 | + * @throws \EE_Error |
|
76 | + */ |
|
77 | + public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
78 | + { |
|
79 | + //create sql like |
|
80 | + //LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
81 | + //LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
82 | + //remember the model relation chain to the JOIN model, because we'll |
|
83 | + //need it for get_join_statement() |
|
84 | + $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
85 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
86 | + $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
87 | + $this->get_this_model()->get_this_model_name(), |
|
88 | + 'ID'); |
|
89 | + $field_with_model_name = $this->get_join_table_fk_field_to( |
|
90 | + $this->get_this_model()->get_this_model_name(), |
|
91 | + 'name'); |
|
92 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
93 | + $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
94 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
95 | + $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
96 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
97 | + //phew! ok, we have all the info we need, now we can create the SQL join string |
|
98 | + $SQL = $this->_left_join( |
|
99 | + $join_table, |
|
100 | + $join_table_alias, |
|
101 | + $join_table_fk_field_to_this_table->get_table_column(), |
|
102 | + $this_table_alias, |
|
103 | + $this_table_pk_field->get_table_column(), |
|
104 | + $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") . |
|
105 | + $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
106 | 106 | |
107 | - return $SQL; |
|
108 | - } |
|
107 | + return $SQL; |
|
108 | + } |
|
109 | 109 | |
110 | 110 | |
111 | - /** |
|
112 | - * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
113 | - * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
114 | - * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
115 | - * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
116 | - * the intermediate join table, and then to the other model's pk's table |
|
117 | - * |
|
118 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
119 | - * @return string of SQL |
|
120 | - * @throws \EE_Error |
|
121 | - */ |
|
122 | - public function get_join_statement($model_relation_chain) |
|
123 | - { |
|
124 | - if ($this->_model_relation_chain_to_join_model === null) { |
|
125 | - throw new EE_Error(sprintf(__('When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
126 | - 'event_espresso'))); |
|
127 | - } |
|
128 | - $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
129 | - $this->get_this_model()->get_this_model_name(), |
|
130 | - 'ID'); |
|
131 | - $join_table_fk_field_to_other_table = $this->get_join_table_fk_field_to( |
|
132 | - $this->get_other_model()->get_this_model_name(), |
|
133 | - 'ID'); |
|
134 | - $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
135 | - $this->get_other_model()->get_this_model_name(), |
|
136 | - 'name'); |
|
111 | + /** |
|
112 | + * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
113 | + * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
114 | + * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
115 | + * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
116 | + * the intermediate join table, and then to the other model's pk's table |
|
117 | + * |
|
118 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
119 | + * @return string of SQL |
|
120 | + * @throws \EE_Error |
|
121 | + */ |
|
122 | + public function get_join_statement($model_relation_chain) |
|
123 | + { |
|
124 | + if ($this->_model_relation_chain_to_join_model === null) { |
|
125 | + throw new EE_Error(sprintf(__('When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
126 | + 'event_espresso'))); |
|
127 | + } |
|
128 | + $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
129 | + $this->get_this_model()->get_this_model_name(), |
|
130 | + 'ID'); |
|
131 | + $join_table_fk_field_to_other_table = $this->get_join_table_fk_field_to( |
|
132 | + $this->get_other_model()->get_this_model_name(), |
|
133 | + 'ID'); |
|
134 | + $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
135 | + $this->get_other_model()->get_this_model_name(), |
|
136 | + 'name'); |
|
137 | 137 | |
138 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
|
139 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
138 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
|
139 | + $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
140 | 140 | |
141 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
142 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
143 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
144 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
141 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
142 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
143 | + $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
144 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
145 | 145 | |
146 | - $SQL = $this->_left_join( |
|
147 | - $other_table, |
|
148 | - $other_table_alias, |
|
149 | - $other_table_pk_field->get_table_column(), |
|
150 | - $join_table_alias, |
|
151 | - $join_table_fk_field_to_other_table->get_table_column(), |
|
152 | - $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
153 | - ) . |
|
154 | - $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
155 | - return $SQL; |
|
156 | - } |
|
146 | + $SQL = $this->_left_join( |
|
147 | + $other_table, |
|
148 | + $other_table_alias, |
|
149 | + $other_table_pk_field->get_table_column(), |
|
150 | + $join_table_alias, |
|
151 | + $join_table_fk_field_to_other_table->get_table_column(), |
|
152 | + $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
153 | + ) . |
|
154 | + $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
155 | + return $SQL; |
|
156 | + } |
|
157 | 157 | |
158 | 158 | |
159 | - /** |
|
160 | - * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
161 | - * |
|
162 | - * @param EE_Base_Class|int $this_obj_or_id |
|
163 | - * @param EE_Base_Class|int $other_obj_or_id |
|
164 | - * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
165 | - * checking existing values and for setting new rows if |
|
166 | - * no exact matches. |
|
167 | - * @return EE_Base_Class |
|
168 | - * @throws \EE_Error |
|
169 | - */ |
|
170 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
171 | - { |
|
172 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
173 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
174 | - //check if such a relationship already exists |
|
175 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
176 | - $this->get_this_model()->get_this_model_name(), |
|
177 | - 'ID'); |
|
178 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
179 | - $this->get_this_model()->get_this_model_name(), |
|
180 | - 'name'); |
|
181 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
182 | - $this->get_other_model()->get_this_model_name(), |
|
183 | - 'ID'); |
|
184 | - $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
185 | - $this->get_other_model()->get_this_model_name(), |
|
186 | - 'name'); |
|
159 | + /** |
|
160 | + * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
161 | + * |
|
162 | + * @param EE_Base_Class|int $this_obj_or_id |
|
163 | + * @param EE_Base_Class|int $other_obj_or_id |
|
164 | + * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
165 | + * checking existing values and for setting new rows if |
|
166 | + * no exact matches. |
|
167 | + * @return EE_Base_Class |
|
168 | + * @throws \EE_Error |
|
169 | + */ |
|
170 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
171 | + { |
|
172 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
173 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
174 | + //check if such a relationship already exists |
|
175 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
176 | + $this->get_this_model()->get_this_model_name(), |
|
177 | + 'ID'); |
|
178 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
179 | + $this->get_this_model()->get_this_model_name(), |
|
180 | + 'name'); |
|
181 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
182 | + $this->get_other_model()->get_this_model_name(), |
|
183 | + 'ID'); |
|
184 | + $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
185 | + $this->get_other_model()->get_this_model_name(), |
|
186 | + 'name'); |
|
187 | 187 | |
188 | - $cols_n_values = array( |
|
189 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
190 | - $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
191 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
192 | - $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
193 | - ); |
|
188 | + $cols_n_values = array( |
|
189 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
190 | + $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
191 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
192 | + $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
193 | + ); |
|
194 | 194 | |
195 | - //if $where_query exists lets add them to the query_params. |
|
196 | - if (! empty($extra_join_model_fields_n_values)) { |
|
197 | - //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
198 | - //make sure we strip THIS models name from the query param |
|
199 | - $parsed_query = array(); |
|
200 | - foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
201 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
202 | - $query_param); |
|
203 | - $parsed_query[$query_param] = $val; |
|
204 | - } |
|
205 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
206 | - } |
|
195 | + //if $where_query exists lets add them to the query_params. |
|
196 | + if (! empty($extra_join_model_fields_n_values)) { |
|
197 | + //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
198 | + //make sure we strip THIS models name from the query param |
|
199 | + $parsed_query = array(); |
|
200 | + foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
201 | + $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
202 | + $query_param); |
|
203 | + $parsed_query[$query_param] = $val; |
|
204 | + } |
|
205 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
206 | + } |
|
207 | 207 | |
208 | - $query_params = array($cols_n_values); |
|
208 | + $query_params = array($cols_n_values); |
|
209 | 209 | |
210 | 210 | |
211 | - $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
212 | - //if there is already an entry in the join table, indicating a relationship, we're done |
|
213 | - //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
214 | - //the other tables, use the joining model directly! |
|
215 | - if (! $existing_entry_in_join_table) { |
|
216 | - $this->get_join_model()->insert($cols_n_values); |
|
217 | - } |
|
218 | - return $other_model_obj; |
|
219 | - } |
|
211 | + $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
212 | + //if there is already an entry in the join table, indicating a relationship, we're done |
|
213 | + //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
214 | + //the other tables, use the joining model directly! |
|
215 | + if (! $existing_entry_in_join_table) { |
|
216 | + $this->get_join_model()->insert($cols_n_values); |
|
217 | + } |
|
218 | + return $other_model_obj; |
|
219 | + } |
|
220 | 220 | |
221 | 221 | |
222 | - /** |
|
223 | - * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
224 | - * |
|
225 | - * @param EE_Base_Class|int $this_obj_or_id |
|
226 | - * @param EE_Base_Class|int $other_obj_or_id |
|
227 | - * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
228 | - * values and for removing existing rows if exact matches exist. |
|
229 | - * @return EE_Base_Class |
|
230 | - * @throws \EE_Error |
|
231 | - */ |
|
232 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
233 | - { |
|
234 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
235 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
236 | - //check if such a relationship already exists |
|
237 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
238 | - $this->get_this_model()->get_this_model_name(), |
|
239 | - 'ID'); |
|
240 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
241 | - $this->get_this_model()->get_this_model_name(), |
|
242 | - 'name'); |
|
243 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
244 | - $this->get_other_model()->get_this_model_name(), |
|
245 | - 'ID'); |
|
246 | - $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
247 | - $this->get_other_model()->get_this_model_name(), |
|
248 | - 'name'); |
|
222 | + /** |
|
223 | + * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
224 | + * |
|
225 | + * @param EE_Base_Class|int $this_obj_or_id |
|
226 | + * @param EE_Base_Class|int $other_obj_or_id |
|
227 | + * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
228 | + * values and for removing existing rows if exact matches exist. |
|
229 | + * @return EE_Base_Class |
|
230 | + * @throws \EE_Error |
|
231 | + */ |
|
232 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
233 | + { |
|
234 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
235 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
236 | + //check if such a relationship already exists |
|
237 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
238 | + $this->get_this_model()->get_this_model_name(), |
|
239 | + 'ID'); |
|
240 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
241 | + $this->get_this_model()->get_this_model_name(), |
|
242 | + 'name'); |
|
243 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
244 | + $this->get_other_model()->get_this_model_name(), |
|
245 | + 'ID'); |
|
246 | + $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
247 | + $this->get_other_model()->get_this_model_name(), |
|
248 | + 'name'); |
|
249 | 249 | |
250 | - $cols_n_values = array( |
|
251 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
252 | - $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
253 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
254 | - $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
255 | - ); |
|
250 | + $cols_n_values = array( |
|
251 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
252 | + $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
253 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
254 | + $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
255 | + ); |
|
256 | 256 | |
257 | - //if $where_query exists lets add them to the query_params. |
|
258 | - if (! empty($where_query)) { |
|
259 | - //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
260 | - //make sure we strip THIS models name from the query param |
|
261 | - $parsed_query = array(); |
|
262 | - foreach ($where_query as $query_param => $val) { |
|
263 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
264 | - $query_param); |
|
265 | - $parsed_query[$query_param] = $val; |
|
266 | - } |
|
267 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
268 | - } |
|
257 | + //if $where_query exists lets add them to the query_params. |
|
258 | + if (! empty($where_query)) { |
|
259 | + //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
260 | + //make sure we strip THIS models name from the query param |
|
261 | + $parsed_query = array(); |
|
262 | + foreach ($where_query as $query_param => $val) { |
|
263 | + $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
264 | + $query_param); |
|
265 | + $parsed_query[$query_param] = $val; |
|
266 | + } |
|
267 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
268 | + } |
|
269 | 269 | |
270 | - $this->get_join_model()->delete(array($cols_n_values)); |
|
271 | - return $other_model_obj; |
|
272 | - } |
|
270 | + $this->get_join_model()->delete(array($cols_n_values)); |
|
271 | + return $other_model_obj; |
|
272 | + } |
|
273 | 273 | } |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | } else { |
63 | 63 | $order = 'second'; |
64 | 64 | } |
65 | - return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
65 | + return $this->get_join_model()->field_settings_for('EXJ_'.$order.'_model_'.$id_or_name_field); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | |
@@ -90,9 +90,9 @@ discard block |
||
90 | 90 | $this->get_this_model()->get_this_model_name(), |
91 | 91 | 'name'); |
92 | 92 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
93 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
93 | + $this->get_this_model()->get_this_model_name()).$this_table_pk_field->get_table_alias(); |
|
94 | 94 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
95 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
95 | + $this->get_join_model()->get_this_model_name()).$join_table_fk_field_to_this_table->get_table_alias(); |
|
96 | 96 | $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
97 | 97 | //phew! ok, we have all the info we need, now we can create the SQL join string |
98 | 98 | $SQL = $this->_left_join( |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | $join_table_fk_field_to_this_table->get_table_column(), |
102 | 102 | $this_table_alias, |
103 | 103 | $this_table_pk_field->get_table_column(), |
104 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") . |
|
104 | + $field_with_model_name->get_qualified_column()."='".$this->get_this_model()->get_this_model_name()."'"). |
|
105 | 105 | $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
106 | 106 | |
107 | 107 | return $SQL; |
@@ -136,11 +136,11 @@ discard block |
||
136 | 136 | 'name'); |
137 | 137 | |
138 | 138 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
139 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
139 | + $this->get_join_model()->get_this_model_name()).$join_table_fk_field_to_this_table->get_table_alias(); |
|
140 | 140 | |
141 | 141 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
142 | 142 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
143 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
143 | + $this->get_other_model()->get_this_model_name()).$other_table_pk_field->get_table_alias(); |
|
144 | 144 | $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
145 | 145 | |
146 | 146 | $SQL = $this->_left_join( |
@@ -149,8 +149,8 @@ discard block |
||
149 | 149 | $other_table_pk_field->get_table_column(), |
150 | 150 | $join_table_alias, |
151 | 151 | $join_table_fk_field_to_other_table->get_table_column(), |
152 | - $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
153 | - ) . |
|
152 | + $field_with_other_model_name->get_qualified_column()."='".$this->get_other_model()->get_this_model_name()."'" |
|
153 | + ). |
|
154 | 154 | $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
155 | 155 | return $SQL; |
156 | 156 | } |
@@ -193,12 +193,12 @@ discard block |
||
193 | 193 | ); |
194 | 194 | |
195 | 195 | //if $where_query exists lets add them to the query_params. |
196 | - if (! empty($extra_join_model_fields_n_values)) { |
|
196 | + if ( ! empty($extra_join_model_fields_n_values)) { |
|
197 | 197 | //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
198 | 198 | //make sure we strip THIS models name from the query param |
199 | 199 | $parsed_query = array(); |
200 | 200 | foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
201 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
201 | + $query_param = str_replace($this->get_join_model()->get_this_model_name().".", "", |
|
202 | 202 | $query_param); |
203 | 203 | $parsed_query[$query_param] = $val; |
204 | 204 | } |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | //if there is already an entry in the join table, indicating a relationship, we're done |
213 | 213 | //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
214 | 214 | //the other tables, use the joining model directly! |
215 | - if (! $existing_entry_in_join_table) { |
|
215 | + if ( ! $existing_entry_in_join_table) { |
|
216 | 216 | $this->get_join_model()->insert($cols_n_values); |
217 | 217 | } |
218 | 218 | return $other_model_obj; |
@@ -255,12 +255,12 @@ discard block |
||
255 | 255 | ); |
256 | 256 | |
257 | 257 | //if $where_query exists lets add them to the query_params. |
258 | - if (! empty($where_query)) { |
|
258 | + if ( ! empty($where_query)) { |
|
259 | 259 | //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
260 | 260 | //make sure we strip THIS models name from the query param |
261 | 261 | $parsed_query = array(); |
262 | 262 | foreach ($where_query as $query_param => $val) { |
263 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
263 | + $query_param = str_replace($this->get_join_model()->get_this_model_name().".", "", |
|
264 | 264 | $query_param); |
265 | 265 | $parsed_query[$query_param] = $val; |
266 | 266 | } |
@@ -18,83 +18,83 @@ |
||
18 | 18 | { |
19 | 19 | |
20 | 20 | |
21 | - /** |
|
22 | - * get_join_statement |
|
23 | - * |
|
24 | - * @param string $model_relation_chain |
|
25 | - * @return string |
|
26 | - * @throws \EE_Error |
|
27 | - */ |
|
28 | - public function get_join_statement($model_relation_chain) |
|
29 | - { |
|
30 | - //create the sql string like |
|
31 | - $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
32 | - //ALSO, need to get the field with the model name |
|
33 | - $field_with_model_name = $this->get_this_model()->get_field_containing_related_model_name(); |
|
21 | + /** |
|
22 | + * get_join_statement |
|
23 | + * |
|
24 | + * @param string $model_relation_chain |
|
25 | + * @return string |
|
26 | + * @throws \EE_Error |
|
27 | + */ |
|
28 | + public function get_join_statement($model_relation_chain) |
|
29 | + { |
|
30 | + //create the sql string like |
|
31 | + $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
32 | + //ALSO, need to get the field with the model name |
|
33 | + $field_with_model_name = $this->get_this_model()->get_field_containing_related_model_name(); |
|
34 | 34 | |
35 | 35 | |
36 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
37 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
38 | - $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
39 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
40 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
41 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
42 | - return $this->_left_join($other_table, |
|
43 | - $other_table_alias, |
|
44 | - $other_table_pk_field->get_table_column(), |
|
45 | - $this_table_alias, |
|
46 | - $this_table_fk_field->get_table_column(), |
|
47 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'") |
|
48 | - . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
49 | - } |
|
36 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
37 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
38 | + $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
39 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
40 | + $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
41 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
42 | + return $this->_left_join($other_table, |
|
43 | + $other_table_alias, |
|
44 | + $other_table_pk_field->get_table_column(), |
|
45 | + $this_table_alias, |
|
46 | + $this_table_fk_field->get_table_column(), |
|
47 | + $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'") |
|
48 | + . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
49 | + } |
|
50 | 50 | |
51 | 51 | |
52 | - /** |
|
53 | - * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
54 | - * you like. |
|
55 | - * |
|
56 | - * @param EE_Base_Class|int $this_obj_or_id |
|
57 | - * @param EE_Base_Class|int $other_obj_or_id |
|
58 | - * @param array $extra_join_model_fields_n_values |
|
59 | - * @return \EE_Base_Class |
|
60 | - * @throws \EE_Error |
|
61 | - */ |
|
62 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
63 | - { |
|
64 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
65 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
66 | - //find the field on THIS model which a foreign key to the other model |
|
67 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
68 | - //set that field on the other model to this model's ID |
|
69 | - $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
70 | - //and make sure this model's field with the foreign model name is set to the correct value |
|
71 | - $this_model_obj->set($this->get_this_model()->get_field_containing_related_model_name()->get_name(), |
|
72 | - $this->get_other_model()->get_this_model_name()); |
|
73 | - $this_model_obj->save(); |
|
74 | - return $other_model_obj; |
|
75 | - } |
|
52 | + /** |
|
53 | + * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
54 | + * you like. |
|
55 | + * |
|
56 | + * @param EE_Base_Class|int $this_obj_or_id |
|
57 | + * @param EE_Base_Class|int $other_obj_or_id |
|
58 | + * @param array $extra_join_model_fields_n_values |
|
59 | + * @return \EE_Base_Class |
|
60 | + * @throws \EE_Error |
|
61 | + */ |
|
62 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
63 | + { |
|
64 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
65 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
66 | + //find the field on THIS model which a foreign key to the other model |
|
67 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
68 | + //set that field on the other model to this model's ID |
|
69 | + $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
70 | + //and make sure this model's field with the foreign model name is set to the correct value |
|
71 | + $this_model_obj->set($this->get_this_model()->get_field_containing_related_model_name()->get_name(), |
|
72 | + $this->get_other_model()->get_this_model_name()); |
|
73 | + $this_model_obj->save(); |
|
74 | + return $other_model_obj; |
|
75 | + } |
|
76 | 76 | |
77 | 77 | |
78 | - /** |
|
79 | - * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
80 | - * |
|
81 | - * @param EE_Base_Class|int $this_obj_or_id |
|
82 | - * @param EE_Base_Class|int $other_obj_or_id |
|
83 | - * @param array $where_query |
|
84 | - * @return \EE_Base_Class |
|
85 | - * @throws \EE_Error |
|
86 | - */ |
|
87 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
88 | - { |
|
89 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
90 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
91 | - //find the field on the other model which is a foreign key to this model |
|
92 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
93 | - //set that field on the other model to this model's ID |
|
94 | - $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
95 | - $this_model_obj->set($this->get_this_model()->get_field_containing_related_model_name()->get_name(), null, |
|
96 | - true); |
|
97 | - $this_model_obj->save(); |
|
98 | - return $other_model_obj; |
|
99 | - } |
|
78 | + /** |
|
79 | + * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
80 | + * |
|
81 | + * @param EE_Base_Class|int $this_obj_or_id |
|
82 | + * @param EE_Base_Class|int $other_obj_or_id |
|
83 | + * @param array $where_query |
|
84 | + * @return \EE_Base_Class |
|
85 | + * @throws \EE_Error |
|
86 | + */ |
|
87 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
88 | + { |
|
89 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
90 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
91 | + //find the field on the other model which is a foreign key to this model |
|
92 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
93 | + //set that field on the other model to this model's ID |
|
94 | + $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
95 | + $this_model_obj->set($this->get_this_model()->get_field_containing_related_model_name()->get_name(), null, |
|
96 | + true); |
|
97 | + $this_model_obj->save(); |
|
98 | + return $other_model_obj; |
|
99 | + } |
|
100 | 100 | } |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -require_once(EE_MODELS . 'relations/EE_Belongs_To_Relation.php'); |
|
2 | +require_once(EE_MODELS.'relations/EE_Belongs_To_Relation.php'); |
|
3 | 3 | |
4 | 4 | |
5 | 5 | /** |
@@ -35,16 +35,16 @@ discard block |
||
35 | 35 | |
36 | 36 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
37 | 37 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
38 | - $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
38 | + $this->get_this_model()->get_this_model_name()).$this_table_fk_field->get_table_alias(); |
|
39 | 39 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
40 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
40 | + $this->get_other_model()->get_this_model_name()).$other_table_pk_field->get_table_alias(); |
|
41 | 41 | $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
42 | 42 | return $this->_left_join($other_table, |
43 | 43 | $other_table_alias, |
44 | 44 | $other_table_pk_field->get_table_column(), |
45 | 45 | $this_table_alias, |
46 | 46 | $this_table_fk_field->get_table_column(), |
47 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'") |
|
47 | + $field_with_model_name->get_qualified_column()."='".$this->get_other_model()->get_this_model_name()."'") |
|
48 | 48 | . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
49 | 49 | } |
50 | 50 |
@@ -12,203 +12,203 @@ |
||
12 | 12 | */ |
13 | 13 | class EE_HABTM_Relation extends EE_Model_Relation_Base |
14 | 14 | { |
15 | - /** |
|
16 | - * Model which defines the relation between two other models. Eg, the EE_Event_Question_Group model, |
|
17 | - * which joins EE_Event and EE_Question_Group |
|
18 | - * |
|
19 | - * @var EEM_Base |
|
20 | - */ |
|
21 | - protected $_joining_model_name; |
|
22 | - |
|
23 | - protected $_model_relation_chain_to_join_model; |
|
24 | - |
|
25 | - |
|
26 | - /** |
|
27 | - * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
28 | - * (and an ee joining-model.) This knows how to join the models, |
|
29 | - * get related models across the relation, and add-and-remove the relationships. |
|
30 | - * |
|
31 | - * @param bool $joining_model_name |
|
32 | - * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
33 | - * are related models across this relation, block (prevent and add an |
|
34 | - * error) the deletion of this model |
|
35 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
36 | - * default |
|
37 | - */ |
|
38 | - public function __construct($joining_model_name, $block_deletes = true, $blocking_delete_error_message = '') |
|
39 | - { |
|
40 | - $this->_joining_model_name = $joining_model_name; |
|
41 | - parent::__construct($block_deletes, $blocking_delete_error_message); |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Gets the joining model's object |
|
46 | - * |
|
47 | - * @return EEM_Base |
|
48 | - */ |
|
49 | - public function get_join_model() |
|
50 | - { |
|
51 | - return $this->_get_model($this->_joining_model_name); |
|
52 | - } |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
57 | - * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
58 | - * |
|
59 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
60 | - * @return string of SQL |
|
61 | - * @throws \EE_Error |
|
62 | - */ |
|
63 | - public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
64 | - { |
|
65 | - //create sql like |
|
66 | - //LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
67 | - //LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
68 | - //remember the model relation chain to the JOIN model, because we'll |
|
69 | - //need it for get_join_statement() |
|
70 | - $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
71 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field();//get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
72 | - $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
73 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
74 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
75 | - |
|
76 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
77 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
78 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
79 | - //phew! ok, we have all the info we need, now we can create the SQL join string |
|
80 | - $SQL = $this->_left_join($join_table, $join_table_alias, $join_table_fk_field_to_this_table->get_table_column(), |
|
81 | - $this_table_alias, |
|
82 | - $this_table_pk_field->get_table_column()) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
83 | - |
|
84 | - return $SQL; |
|
85 | - } |
|
86 | - |
|
87 | - |
|
88 | - /** |
|
89 | - * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
90 | - * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
91 | - * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
92 | - * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
93 | - * the intermediate join table, and then to the other model's pk's table |
|
94 | - * |
|
95 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
96 | - * @return string of SQL |
|
97 | - * @throws \EE_Error |
|
98 | - */ |
|
99 | - public function get_join_statement($model_relation_chain) |
|
100 | - { |
|
101 | - if ($this->_model_relation_chain_to_join_model === null) { |
|
102 | - throw new EE_Error(sprintf(__('When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
103 | - 'event_espresso'))); |
|
104 | - } |
|
105 | - $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
106 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
|
107 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
108 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
109 | - $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
110 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
111 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
112 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
113 | - |
|
114 | - $SQL = $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
|
115 | - $join_table_alias, |
|
116 | - $join_table_fk_field_to_other_table->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
117 | - return $SQL; |
|
118 | - } |
|
119 | - |
|
120 | - |
|
121 | - /** |
|
122 | - * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
123 | - * If the join table has additional columns (eg, the Event_Question_Group table has a is_primary column), then |
|
124 | - * you'll want to directly use the EEM_Event_Question_Group model to add the entry to the table and set those extra |
|
125 | - * columns' values |
|
126 | - * |
|
127 | - * @param EE_Base_Class|int $this_obj_or_id |
|
128 | - * @param EE_Base_Class|int $other_obj_or_id |
|
129 | - * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
130 | - * checking existing values and for setting new rows if |
|
131 | - * no exact matches. |
|
132 | - * @return EE_Base_Class |
|
133 | - * @throws \EE_Error |
|
134 | - */ |
|
135 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
136 | - { |
|
137 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
138 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
139 | - //check if such a relationship already exists |
|
140 | - $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
141 | - $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
142 | - |
|
143 | - $cols_n_values = array( |
|
144 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
145 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
146 | - ); |
|
147 | - |
|
148 | - //if $where_query exists lets add them to the query_params. |
|
149 | - if (! empty($extra_join_model_fields_n_values)) { |
|
150 | - //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
151 | - //make sure we strip THIS models name from the query param |
|
152 | - $parsed_query = array(); |
|
153 | - foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
154 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
155 | - $query_param); |
|
156 | - $parsed_query[$query_param] = $val; |
|
157 | - } |
|
158 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
159 | - } |
|
160 | - |
|
161 | - $query_params = array($cols_n_values); |
|
162 | - |
|
163 | - |
|
164 | - $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
165 | - //if there is already an entry in the join table, indicating a relationship, we're done |
|
166 | - //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
167 | - //the other tables, use the joining model directly! |
|
168 | - if (! $existing_entry_in_join_table) { |
|
169 | - $this->get_join_model()->insert($cols_n_values); |
|
170 | - } |
|
171 | - return $other_model_obj; |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - /** |
|
176 | - * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
177 | - * |
|
178 | - * @param EE_Base_Class|int $this_obj_or_id |
|
179 | - * @param EE_Base_Class|int $other_obj_or_id |
|
180 | - * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
181 | - * values and for removing existing rows if exact matches exist. |
|
182 | - * @return EE_Base_Class |
|
183 | - * @throws \EE_Error |
|
184 | - */ |
|
185 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
186 | - { |
|
187 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
188 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
189 | - //check if such a relationship already exists |
|
190 | - $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
191 | - $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
192 | - |
|
193 | - $cols_n_values = array( |
|
194 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
195 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
196 | - ); |
|
197 | - |
|
198 | - //if $where_query exists lets add them to the query_params. |
|
199 | - if (! empty($where_query)) { |
|
200 | - //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
201 | - //make sure we strip THIS models name from the query param |
|
202 | - $parsed_query = array(); |
|
203 | - foreach ($where_query as $query_param => $val) { |
|
204 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
205 | - $query_param); |
|
206 | - $parsed_query[$query_param] = $val; |
|
207 | - } |
|
208 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
209 | - } |
|
210 | - |
|
211 | - $this->get_join_model()->delete(array($cols_n_values)); |
|
212 | - return $other_model_obj; |
|
213 | - } |
|
15 | + /** |
|
16 | + * Model which defines the relation between two other models. Eg, the EE_Event_Question_Group model, |
|
17 | + * which joins EE_Event and EE_Question_Group |
|
18 | + * |
|
19 | + * @var EEM_Base |
|
20 | + */ |
|
21 | + protected $_joining_model_name; |
|
22 | + |
|
23 | + protected $_model_relation_chain_to_join_model; |
|
24 | + |
|
25 | + |
|
26 | + /** |
|
27 | + * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
28 | + * (and an ee joining-model.) This knows how to join the models, |
|
29 | + * get related models across the relation, and add-and-remove the relationships. |
|
30 | + * |
|
31 | + * @param bool $joining_model_name |
|
32 | + * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
33 | + * are related models across this relation, block (prevent and add an |
|
34 | + * error) the deletion of this model |
|
35 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
36 | + * default |
|
37 | + */ |
|
38 | + public function __construct($joining_model_name, $block_deletes = true, $blocking_delete_error_message = '') |
|
39 | + { |
|
40 | + $this->_joining_model_name = $joining_model_name; |
|
41 | + parent::__construct($block_deletes, $blocking_delete_error_message); |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Gets the joining model's object |
|
46 | + * |
|
47 | + * @return EEM_Base |
|
48 | + */ |
|
49 | + public function get_join_model() |
|
50 | + { |
|
51 | + return $this->_get_model($this->_joining_model_name); |
|
52 | + } |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
57 | + * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
58 | + * |
|
59 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
60 | + * @return string of SQL |
|
61 | + * @throws \EE_Error |
|
62 | + */ |
|
63 | + public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
64 | + { |
|
65 | + //create sql like |
|
66 | + //LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
67 | + //LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
68 | + //remember the model relation chain to the JOIN model, because we'll |
|
69 | + //need it for get_join_statement() |
|
70 | + $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
71 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field();//get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
72 | + $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
73 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
74 | + $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
75 | + |
|
76 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
77 | + $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
78 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
79 | + //phew! ok, we have all the info we need, now we can create the SQL join string |
|
80 | + $SQL = $this->_left_join($join_table, $join_table_alias, $join_table_fk_field_to_this_table->get_table_column(), |
|
81 | + $this_table_alias, |
|
82 | + $this_table_pk_field->get_table_column()) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
83 | + |
|
84 | + return $SQL; |
|
85 | + } |
|
86 | + |
|
87 | + |
|
88 | + /** |
|
89 | + * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
90 | + * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
91 | + * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
92 | + * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
93 | + * the intermediate join table, and then to the other model's pk's table |
|
94 | + * |
|
95 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
96 | + * @return string of SQL |
|
97 | + * @throws \EE_Error |
|
98 | + */ |
|
99 | + public function get_join_statement($model_relation_chain) |
|
100 | + { |
|
101 | + if ($this->_model_relation_chain_to_join_model === null) { |
|
102 | + throw new EE_Error(sprintf(__('When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
103 | + 'event_espresso'))); |
|
104 | + } |
|
105 | + $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
106 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
|
107 | + $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
108 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
109 | + $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
110 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
111 | + $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
112 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
113 | + |
|
114 | + $SQL = $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
|
115 | + $join_table_alias, |
|
116 | + $join_table_fk_field_to_other_table->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
117 | + return $SQL; |
|
118 | + } |
|
119 | + |
|
120 | + |
|
121 | + /** |
|
122 | + * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
123 | + * If the join table has additional columns (eg, the Event_Question_Group table has a is_primary column), then |
|
124 | + * you'll want to directly use the EEM_Event_Question_Group model to add the entry to the table and set those extra |
|
125 | + * columns' values |
|
126 | + * |
|
127 | + * @param EE_Base_Class|int $this_obj_or_id |
|
128 | + * @param EE_Base_Class|int $other_obj_or_id |
|
129 | + * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
130 | + * checking existing values and for setting new rows if |
|
131 | + * no exact matches. |
|
132 | + * @return EE_Base_Class |
|
133 | + * @throws \EE_Error |
|
134 | + */ |
|
135 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
136 | + { |
|
137 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
138 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
139 | + //check if such a relationship already exists |
|
140 | + $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
141 | + $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
142 | + |
|
143 | + $cols_n_values = array( |
|
144 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
145 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
146 | + ); |
|
147 | + |
|
148 | + //if $where_query exists lets add them to the query_params. |
|
149 | + if (! empty($extra_join_model_fields_n_values)) { |
|
150 | + //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
151 | + //make sure we strip THIS models name from the query param |
|
152 | + $parsed_query = array(); |
|
153 | + foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
154 | + $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
155 | + $query_param); |
|
156 | + $parsed_query[$query_param] = $val; |
|
157 | + } |
|
158 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
159 | + } |
|
160 | + |
|
161 | + $query_params = array($cols_n_values); |
|
162 | + |
|
163 | + |
|
164 | + $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
165 | + //if there is already an entry in the join table, indicating a relationship, we're done |
|
166 | + //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
167 | + //the other tables, use the joining model directly! |
|
168 | + if (! $existing_entry_in_join_table) { |
|
169 | + $this->get_join_model()->insert($cols_n_values); |
|
170 | + } |
|
171 | + return $other_model_obj; |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + /** |
|
176 | + * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
177 | + * |
|
178 | + * @param EE_Base_Class|int $this_obj_or_id |
|
179 | + * @param EE_Base_Class|int $other_obj_or_id |
|
180 | + * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
181 | + * values and for removing existing rows if exact matches exist. |
|
182 | + * @return EE_Base_Class |
|
183 | + * @throws \EE_Error |
|
184 | + */ |
|
185 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
186 | + { |
|
187 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
188 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
189 | + //check if such a relationship already exists |
|
190 | + $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
191 | + $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
192 | + |
|
193 | + $cols_n_values = array( |
|
194 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
195 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
196 | + ); |
|
197 | + |
|
198 | + //if $where_query exists lets add them to the query_params. |
|
199 | + if (! empty($where_query)) { |
|
200 | + //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
201 | + //make sure we strip THIS models name from the query param |
|
202 | + $parsed_query = array(); |
|
203 | + foreach ($where_query as $query_param => $val) { |
|
204 | + $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
205 | + $query_param); |
|
206 | + $parsed_query[$query_param] = $val; |
|
207 | + } |
|
208 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
209 | + } |
|
210 | + |
|
211 | + $this->get_join_model()->delete(array($cols_n_values)); |
|
212 | + return $other_model_obj; |
|
213 | + } |
|
214 | 214 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -require_once(EE_MODELS . 'relations/EE_Model_Relation_Base.php'); |
|
3 | +require_once(EE_MODELS.'relations/EE_Model_Relation_Base.php'); |
|
4 | 4 | |
5 | 5 | |
6 | 6 | /** |
@@ -68,18 +68,18 @@ discard block |
||
68 | 68 | //remember the model relation chain to the JOIN model, because we'll |
69 | 69 | //need it for get_join_statement() |
70 | 70 | $this->_model_relation_chain_to_join_model = $model_relation_chain; |
71 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field();//get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
71 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); //get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
72 | 72 | $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
73 | 73 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
74 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
74 | + $this->get_this_model()->get_this_model_name()).$this_table_pk_field->get_table_alias(); |
|
75 | 75 | |
76 | 76 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
77 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
77 | + $this->get_join_model()->get_this_model_name()).$join_table_fk_field_to_this_table->get_table_alias(); |
|
78 | 78 | $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
79 | 79 | //phew! ok, we have all the info we need, now we can create the SQL join string |
80 | 80 | $SQL = $this->_left_join($join_table, $join_table_alias, $join_table_fk_field_to_this_table->get_table_column(), |
81 | 81 | $this_table_alias, |
82 | - $this_table_pk_field->get_table_column()) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
82 | + $this_table_pk_field->get_table_column()).$this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
83 | 83 | |
84 | 84 | return $SQL; |
85 | 85 | } |
@@ -104,16 +104,16 @@ discard block |
||
104 | 104 | } |
105 | 105 | $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
106 | 106 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
107 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
107 | + $this->get_join_model()->get_this_model_name()).$join_table_fk_field_to_this_table->get_table_alias(); |
|
108 | 108 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
109 | 109 | $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
110 | 110 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
111 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
111 | + $this->get_other_model()->get_this_model_name()).$other_table_pk_field->get_table_alias(); |
|
112 | 112 | $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
113 | 113 | |
114 | 114 | $SQL = $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
115 | 115 | $join_table_alias, |
116 | - $join_table_fk_field_to_other_table->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
116 | + $join_table_fk_field_to_other_table->get_table_column()).$this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
117 | 117 | return $SQL; |
118 | 118 | } |
119 | 119 | |
@@ -146,12 +146,12 @@ discard block |
||
146 | 146 | ); |
147 | 147 | |
148 | 148 | //if $where_query exists lets add them to the query_params. |
149 | - if (! empty($extra_join_model_fields_n_values)) { |
|
149 | + if ( ! empty($extra_join_model_fields_n_values)) { |
|
150 | 150 | //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
151 | 151 | //make sure we strip THIS models name from the query param |
152 | 152 | $parsed_query = array(); |
153 | 153 | foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
154 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
154 | + $query_param = str_replace($this->get_join_model()->get_this_model_name().".", "", |
|
155 | 155 | $query_param); |
156 | 156 | $parsed_query[$query_param] = $val; |
157 | 157 | } |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | //if there is already an entry in the join table, indicating a relationship, we're done |
166 | 166 | //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
167 | 167 | //the other tables, use the joining model directly! |
168 | - if (! $existing_entry_in_join_table) { |
|
168 | + if ( ! $existing_entry_in_join_table) { |
|
169 | 169 | $this->get_join_model()->insert($cols_n_values); |
170 | 170 | } |
171 | 171 | return $other_model_obj; |
@@ -196,12 +196,12 @@ discard block |
||
196 | 196 | ); |
197 | 197 | |
198 | 198 | //if $where_query exists lets add them to the query_params. |
199 | - if (! empty($where_query)) { |
|
199 | + if ( ! empty($where_query)) { |
|
200 | 200 | //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
201 | 201 | //make sure we strip THIS models name from the query param |
202 | 202 | $parsed_query = array(); |
203 | 203 | foreach ($where_query as $query_param => $val) { |
204 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
204 | + $query_param = str_replace($this->get_join_model()->get_this_model_name().".", "", |
|
205 | 205 | $query_param); |
206 | 206 | $parsed_query[$query_param] = $val; |
207 | 207 | } |
@@ -13,128 +13,128 @@ |
||
13 | 13 | class EE_Belongs_To_Relation extends EE_Model_Relation_Base |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * Object representing the relationship between two models. Belongs_To means that THIS model has the foreign key |
|
18 | - * to the other model. This knows how to join the models, |
|
19 | - * get related models across the relation, and add-and-remove the relationships. |
|
20 | - * |
|
21 | - * @param boolean $block_deletes For Belongs_To relations, this is set to FALSE by |
|
22 | - * default. if there are related models across this |
|
23 | - * relation, block (prevent and add an error) the |
|
24 | - * deletion of this model |
|
25 | - * @param string $related_model_objects_deletion_error_message a customized error message on blocking deletes |
|
26 | - * instead of the default |
|
27 | - */ |
|
28 | - public function __construct($block_deletes = false, $related_model_objects_deletion_error_message = null) |
|
29 | - { |
|
30 | - parent::__construct($block_deletes, $related_model_objects_deletion_error_message); |
|
31 | - } |
|
16 | + /** |
|
17 | + * Object representing the relationship between two models. Belongs_To means that THIS model has the foreign key |
|
18 | + * to the other model. This knows how to join the models, |
|
19 | + * get related models across the relation, and add-and-remove the relationships. |
|
20 | + * |
|
21 | + * @param boolean $block_deletes For Belongs_To relations, this is set to FALSE by |
|
22 | + * default. if there are related models across this |
|
23 | + * relation, block (prevent and add an error) the |
|
24 | + * deletion of this model |
|
25 | + * @param string $related_model_objects_deletion_error_message a customized error message on blocking deletes |
|
26 | + * instead of the default |
|
27 | + */ |
|
28 | + public function __construct($block_deletes = false, $related_model_objects_deletion_error_message = null) |
|
29 | + { |
|
30 | + parent::__construct($block_deletes, $related_model_objects_deletion_error_message); |
|
31 | + } |
|
32 | 32 | |
33 | 33 | |
34 | - /** |
|
35 | - * get_join_statement |
|
36 | - * |
|
37 | - * @param string $model_relation_chain |
|
38 | - * @return string |
|
39 | - * @throws \EE_Error |
|
40 | - */ |
|
41 | - public function get_join_statement($model_relation_chain) |
|
42 | - { |
|
43 | - //create the sql string like |
|
44 | - $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
45 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
46 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
47 | - $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
48 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
49 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
50 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
51 | - return $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
|
52 | - $this_table_alias, |
|
53 | - $this_table_fk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
54 | - } |
|
34 | + /** |
|
35 | + * get_join_statement |
|
36 | + * |
|
37 | + * @param string $model_relation_chain |
|
38 | + * @return string |
|
39 | + * @throws \EE_Error |
|
40 | + */ |
|
41 | + public function get_join_statement($model_relation_chain) |
|
42 | + { |
|
43 | + //create the sql string like |
|
44 | + $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
45 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
46 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
47 | + $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
48 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
49 | + $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
50 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
51 | + return $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
|
52 | + $this_table_alias, |
|
53 | + $this_table_fk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
54 | + } |
|
55 | 55 | |
56 | 56 | |
57 | - /** |
|
58 | - * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
59 | - * you like. |
|
60 | - * |
|
61 | - * @param EE_Base_Class|int $this_obj_or_id |
|
62 | - * @param EE_Base_Class|int $other_obj_or_id |
|
63 | - * @param array $extra_join_model_fields_n_values |
|
64 | - * @return \EE_Base_Class |
|
65 | - * @throws \EE_Error |
|
66 | - */ |
|
67 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
68 | - { |
|
69 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
70 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
71 | - //find the field on the other model which is a foreign key to this model |
|
72 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
73 | - if ($this_model_obj->get($fk_on_this_model->get_name()) != $other_model_obj->ID()) { |
|
74 | - //set that field on the other model to this model's ID |
|
75 | - $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
76 | - $this_model_obj->save(); |
|
77 | - } |
|
78 | - return $other_model_obj; |
|
79 | - } |
|
57 | + /** |
|
58 | + * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
59 | + * you like. |
|
60 | + * |
|
61 | + * @param EE_Base_Class|int $this_obj_or_id |
|
62 | + * @param EE_Base_Class|int $other_obj_or_id |
|
63 | + * @param array $extra_join_model_fields_n_values |
|
64 | + * @return \EE_Base_Class |
|
65 | + * @throws \EE_Error |
|
66 | + */ |
|
67 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
68 | + { |
|
69 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
70 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
71 | + //find the field on the other model which is a foreign key to this model |
|
72 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
73 | + if ($this_model_obj->get($fk_on_this_model->get_name()) != $other_model_obj->ID()) { |
|
74 | + //set that field on the other model to this model's ID |
|
75 | + $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
76 | + $this_model_obj->save(); |
|
77 | + } |
|
78 | + return $other_model_obj; |
|
79 | + } |
|
80 | 80 | |
81 | 81 | |
82 | - /** |
|
83 | - * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
84 | - * |
|
85 | - * @param EE_Base_Class|int $this_obj_or_id |
|
86 | - * @param EE_Base_Class|int $other_obj_or_id |
|
87 | - * @param array $where_query |
|
88 | - * @return \EE_Base_Class |
|
89 | - * @throws \EE_Error |
|
90 | - */ |
|
91 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
92 | - { |
|
93 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
94 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
95 | - //find the field on the other model which is a foreign key to this model |
|
96 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
97 | - //set that field on the other model to this model's ID |
|
98 | - $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
99 | - $this_model_obj->save(); |
|
100 | - return $other_model_obj; |
|
101 | - } |
|
82 | + /** |
|
83 | + * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
84 | + * |
|
85 | + * @param EE_Base_Class|int $this_obj_or_id |
|
86 | + * @param EE_Base_Class|int $other_obj_or_id |
|
87 | + * @param array $where_query |
|
88 | + * @return \EE_Base_Class |
|
89 | + * @throws \EE_Error |
|
90 | + */ |
|
91 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
92 | + { |
|
93 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
94 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
95 | + //find the field on the other model which is a foreign key to this model |
|
96 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
97 | + //set that field on the other model to this model's ID |
|
98 | + $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
99 | + $this_model_obj->save(); |
|
100 | + return $other_model_obj; |
|
101 | + } |
|
102 | 102 | |
103 | 103 | |
104 | - /** |
|
105 | - * Overrides parent so that we don't NEED to save the $model_object before getting the related objects. |
|
106 | - * |
|
107 | - * @param EE_Base_Class $model_obj_or_id |
|
108 | - * @param array $query_params like EEM_Base::get_all's $query_params |
|
109 | - * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
110 | - * @return EE_Base_Class[] |
|
111 | - * @throws \EE_Error |
|
112 | - */ |
|
113 | - public function get_all_related( |
|
114 | - $model_obj_or_id, |
|
115 | - $query_params = array(), |
|
116 | - $values_already_prepared_by_model_object = false |
|
117 | - ) { |
|
118 | - if ($values_already_prepared_by_model_object !== false) { |
|
119 | - EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
120 | - __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
121 | - '4.8.1'); |
|
122 | - } |
|
123 | - //get column on this model object which is a foreign key to the other model |
|
124 | - $fk_field_obj = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
125 | - //get its value |
|
126 | - if ($model_obj_or_id instanceof EE_Base_Class) { |
|
127 | - $model_obj = $model_obj_or_id; |
|
128 | - } else { |
|
129 | - $model_obj = $this->get_this_model()->ensure_is_obj($model_obj_or_id); |
|
130 | - } |
|
131 | - $ID_value_on_other_model = $model_obj->get($fk_field_obj->get_name()); |
|
132 | - //get all where their PK matches that value |
|
133 | - $query_params[0][$this->get_other_model()->get_primary_key_field()->get_name()] = $ID_value_on_other_model; |
|
134 | - $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
104 | + /** |
|
105 | + * Overrides parent so that we don't NEED to save the $model_object before getting the related objects. |
|
106 | + * |
|
107 | + * @param EE_Base_Class $model_obj_or_id |
|
108 | + * @param array $query_params like EEM_Base::get_all's $query_params |
|
109 | + * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
110 | + * @return EE_Base_Class[] |
|
111 | + * @throws \EE_Error |
|
112 | + */ |
|
113 | + public function get_all_related( |
|
114 | + $model_obj_or_id, |
|
115 | + $query_params = array(), |
|
116 | + $values_already_prepared_by_model_object = false |
|
117 | + ) { |
|
118 | + if ($values_already_prepared_by_model_object !== false) { |
|
119 | + EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
120 | + __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
121 | + '4.8.1'); |
|
122 | + } |
|
123 | + //get column on this model object which is a foreign key to the other model |
|
124 | + $fk_field_obj = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
125 | + //get its value |
|
126 | + if ($model_obj_or_id instanceof EE_Base_Class) { |
|
127 | + $model_obj = $model_obj_or_id; |
|
128 | + } else { |
|
129 | + $model_obj = $this->get_this_model()->ensure_is_obj($model_obj_or_id); |
|
130 | + } |
|
131 | + $ID_value_on_other_model = $model_obj->get($fk_field_obj->get_name()); |
|
132 | + //get all where their PK matches that value |
|
133 | + $query_params[0][$this->get_other_model()->get_primary_key_field()->get_name()] = $ID_value_on_other_model; |
|
134 | + $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
135 | 135 | // echo '$query_params'; |
136 | 136 | // var_dump($query_params); |
137 | - return $this->get_other_model()->get_all($query_params); |
|
138 | - } |
|
137 | + return $this->get_other_model()->get_all($query_params); |
|
138 | + } |
|
139 | 139 | |
140 | 140 | } |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -require_once(EE_MODELS . 'relations/EE_Model_Relation_Base.php'); |
|
2 | +require_once(EE_MODELS.'relations/EE_Model_Relation_Base.php'); |
|
3 | 3 | |
4 | 4 | /** |
5 | 5 | * Class EE_Belongs_To_Relation |
@@ -44,13 +44,13 @@ discard block |
||
44 | 44 | $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
45 | 45 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
46 | 46 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
47 | - $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
47 | + $this->get_this_model()->get_this_model_name()).$this_table_fk_field->get_table_alias(); |
|
48 | 48 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
49 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
49 | + $this->get_other_model()->get_this_model_name()).$other_table_pk_field->get_table_alias(); |
|
50 | 50 | $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
51 | 51 | return $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
52 | 52 | $this_table_alias, |
53 | - $this_table_fk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
53 | + $this_table_fk_field->get_table_column()).$this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 |
@@ -567,7 +567,7 @@ discard block |
||
567 | 567 | * allowed) |
568 | 568 | * |
569 | 569 | * @param string $datetime_string mysql timestamp in UTC |
570 | - * @return mixed null | DateTime |
|
570 | + * @return null|DbSafeDateTime null | DateTime |
|
571 | 571 | * @throws \EE_Error |
572 | 572 | */ |
573 | 573 | public function prepare_for_set_from_db($datetime_string) |
@@ -724,7 +724,7 @@ discard block |
||
724 | 724 | * |
725 | 725 | * @param \DateTimeZone $DateTimeZone |
726 | 726 | * @param int $time |
727 | - * @return mixed |
|
727 | + * @return integer |
|
728 | 728 | * @throws \DomainException |
729 | 729 | */ |
730 | 730 | public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
@@ -17,747 +17,747 @@ |
||
17 | 17 | class EE_Datetime_Field extends EE_Model_Field_Base |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * The pattern we're looking for is if only the characters 0-9 are found and there are only |
|
22 | - * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
|
23 | - * |
|
24 | - * @type string unix_timestamp_regex |
|
25 | - */ |
|
26 | - const unix_timestamp_regex = '/[0-9]{10,}/'; |
|
27 | - |
|
28 | - /** |
|
29 | - * @type string mysql_timestamp_format |
|
30 | - */ |
|
31 | - const mysql_timestamp_format = 'Y-m-d H:i:s'; |
|
32 | - |
|
33 | - /** |
|
34 | - * @type string mysql_date_format |
|
35 | - */ |
|
36 | - const mysql_date_format = 'Y-m-d'; |
|
37 | - |
|
38 | - /** |
|
39 | - * @type string mysql_time_format |
|
40 | - */ |
|
41 | - const mysql_time_format = 'H:i:s'; |
|
42 | - |
|
43 | - /** |
|
44 | - * Const for using in the default value. If the field's default is set to this, |
|
45 | - * then we will return the time of calling `get_default_value()`, not |
|
46 | - * just the current time at construction |
|
47 | - */ |
|
48 | - const now = 'now'; |
|
49 | - |
|
50 | - /** |
|
51 | - * The following properties hold the default formats for date and time. |
|
52 | - * Defaults are set via the constructor and can be overridden on class instantiation. |
|
53 | - * However they can also be overridden later by the set_format() method |
|
54 | - * (and corresponding set_date_format, set_time_format methods); |
|
55 | - */ |
|
56 | - /** |
|
57 | - * @type string $_date_format |
|
58 | - */ |
|
59 | - protected $_date_format = ''; |
|
60 | - |
|
61 | - /** |
|
62 | - * @type string $_time_format |
|
63 | - */ |
|
64 | - protected $_time_format = ''; |
|
65 | - |
|
66 | - /** |
|
67 | - * @type string $_pretty_date_format |
|
68 | - */ |
|
69 | - protected $_pretty_date_format = ''; |
|
70 | - |
|
71 | - /** |
|
72 | - * @type string $_pretty_time_format |
|
73 | - */ |
|
74 | - protected $_pretty_time_format = ''; |
|
75 | - |
|
76 | - /** |
|
77 | - * @type DateTimeZone $_DateTimeZone |
|
78 | - */ |
|
79 | - protected $_DateTimeZone; |
|
80 | - |
|
81 | - /** |
|
82 | - * @type DateTimeZone $_UTC_DateTimeZone |
|
83 | - */ |
|
84 | - protected $_UTC_DateTimeZone; |
|
85 | - |
|
86 | - /** |
|
87 | - * @type DateTimeZone $_blog_DateTimeZone |
|
88 | - */ |
|
89 | - protected $_blog_DateTimeZone; |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * This property holds how we want the output returned when getting a datetime string. It is set for the |
|
94 | - * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
|
95 | - * and time returned via getters. |
|
96 | - * |
|
97 | - * @var mixed (null|string) |
|
98 | - */ |
|
99 | - protected $_date_time_output; |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * timezone string |
|
104 | - * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
|
105 | - * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
|
106 | - * coming out of the object to be in. Default timezone is the current WP timezone option setting |
|
107 | - * |
|
108 | - * @var string |
|
109 | - */ |
|
110 | - protected $_timezone_string; |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
|
115 | - * offsets for comparison purposes). |
|
116 | - * |
|
117 | - * @var int |
|
118 | - */ |
|
119 | - protected $_blog_offset; |
|
120 | - |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * @param string $table_column |
|
125 | - * @param string $nice_name |
|
126 | - * @param bool $nullable |
|
127 | - * @param string $default_value |
|
128 | - * @param string $timezone_string |
|
129 | - * @param string $date_format |
|
130 | - * @param string $time_format |
|
131 | - * @param string $pretty_date_format |
|
132 | - * @param string $pretty_time_format |
|
133 | - * @throws EE_Error |
|
134 | - * @throws InvalidArgumentException |
|
135 | - */ |
|
136 | - public function __construct( |
|
137 | - $table_column, |
|
138 | - $nice_name, |
|
139 | - $nullable, |
|
140 | - $default_value, |
|
141 | - $timezone_string = '', |
|
142 | - $date_format = '', |
|
143 | - $time_format = '', |
|
144 | - $pretty_date_format = '', |
|
145 | - $pretty_time_format = '' |
|
146 | - ) { |
|
147 | - |
|
148 | - $this->_date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
149 | - $this->_time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
150 | - $this->_pretty_date_format = ! empty($pretty_date_format) ? $pretty_date_format : get_option('date_format'); |
|
151 | - $this->_pretty_time_format = ! empty($pretty_time_format) ? $pretty_time_format : get_option('time_format'); |
|
152 | - |
|
153 | - parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
154 | - $this->set_timezone($timezone_string); |
|
155 | - $this->setSchemaFormat('date-time'); |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - /** |
|
160 | - * @return DateTimeZone |
|
161 | - * @throws \EE_Error |
|
162 | - */ |
|
163 | - public function get_UTC_DateTimeZone() |
|
164 | - { |
|
165 | - return $this->_UTC_DateTimeZone instanceof DateTimeZone |
|
166 | - ? $this->_UTC_DateTimeZone |
|
167 | - : $this->_create_timezone_object_from_timezone_string('UTC'); |
|
168 | - } |
|
169 | - |
|
170 | - |
|
171 | - /** |
|
172 | - * @return DateTimeZone |
|
173 | - * @throws \EE_Error |
|
174 | - */ |
|
175 | - public function get_blog_DateTimeZone() |
|
176 | - { |
|
177 | - return $this->_blog_DateTimeZone instanceof DateTimeZone |
|
178 | - ? $this->_blog_DateTimeZone |
|
179 | - : $this->_create_timezone_object_from_timezone_string(''); |
|
180 | - } |
|
181 | - |
|
182 | - |
|
183 | - /** |
|
184 | - * this prepares any incoming date data and make sure its converted to a utc unix timestamp |
|
185 | - * |
|
186 | - * @param string|int $value_inputted_for_field_on_model_object could be a string formatted date time or int unix |
|
187 | - * timestamp |
|
188 | - * @return DateTime |
|
189 | - */ |
|
190 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
191 | - { |
|
192 | - return $this->_get_date_object($value_inputted_for_field_on_model_object); |
|
193 | - } |
|
194 | - |
|
195 | - |
|
196 | - /** |
|
197 | - * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
|
198 | - * getters need to know whether we're just returning the date or the time or both. By default we return both. |
|
199 | - * |
|
200 | - * @param bool $pretty If we're returning the pretty formats or standard format string. |
|
201 | - * @return string The final assembled format string. |
|
202 | - */ |
|
203 | - protected function _get_date_time_output($pretty = false) |
|
204 | - { |
|
205 | - |
|
206 | - switch ($this->_date_time_output) { |
|
207 | - case 'time' : |
|
208 | - return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
209 | - break; |
|
210 | - |
|
211 | - case 'date' : |
|
212 | - return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
213 | - break; |
|
214 | - |
|
215 | - default : |
|
216 | - return $pretty |
|
217 | - ? $this->_pretty_date_format . ' ' . $this->_pretty_time_format |
|
218 | - : $this->_date_format . ' ' . $this->_time_format; |
|
219 | - } |
|
220 | - } |
|
221 | - |
|
222 | - |
|
223 | - /** |
|
224 | - * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
|
225 | - * returned (using the format properties) |
|
226 | - * |
|
227 | - * @param string $what acceptable values are 'time' or 'date'. |
|
228 | - * Any other value will be set but will always result |
|
229 | - * in both 'date' and 'time' being returned. |
|
230 | - * @return void |
|
231 | - */ |
|
232 | - public function set_date_time_output($what = null) |
|
233 | - { |
|
234 | - $this->_date_time_output = $what; |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - /** |
|
239 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
240 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
241 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
|
242 | - * We also set some other properties in this method. |
|
243 | - * |
|
244 | - * @param string $timezone_string A valid timezone string as described by @link |
|
245 | - * http://www.php.net/manual/en/timezones.php |
|
246 | - * @return void |
|
247 | - * @throws InvalidArgumentException |
|
248 | - * @throws InvalidDataTypeException |
|
249 | - * @throws InvalidInterfaceException |
|
250 | - */ |
|
251 | - public function set_timezone($timezone_string) |
|
252 | - { |
|
253 | - if (empty($timezone_string) && $this->_timezone_string !== null) { |
|
254 | - // leave the timezone AS-IS if we already have one and |
|
255 | - // the function arg didn't provide one |
|
256 | - return; |
|
257 | - } |
|
258 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
259 | - $this->_timezone_string = ! empty($timezone_string) ? $timezone_string : 'UTC'; |
|
260 | - $this->_DateTimeZone = $this->_create_timezone_object_from_timezone_string($this->_timezone_string); |
|
261 | - } |
|
262 | - |
|
263 | - |
|
264 | - /** |
|
265 | - * _create_timezone_object_from_timezone_name |
|
266 | - * |
|
267 | - * @access protected |
|
268 | - * @param string $timezone_string |
|
269 | - * @return \DateTimeZone |
|
270 | - * @throws InvalidArgumentException |
|
271 | - * @throws InvalidDataTypeException |
|
272 | - * @throws InvalidInterfaceException |
|
273 | - */ |
|
274 | - protected function _create_timezone_object_from_timezone_string($timezone_string = '') |
|
275 | - { |
|
276 | - return new DateTimeZone(EEH_DTT_Helper::get_valid_timezone_string($timezone_string)); |
|
277 | - } |
|
278 | - |
|
279 | - |
|
280 | - /** |
|
281 | - * This just returns whatever is set for the current timezone. |
|
282 | - * |
|
283 | - * @access public |
|
284 | - * @return string timezone string |
|
285 | - */ |
|
286 | - public function get_timezone() |
|
287 | - { |
|
288 | - return $this->_timezone_string; |
|
289 | - } |
|
290 | - |
|
291 | - |
|
292 | - /** |
|
293 | - * set the $_date_format property |
|
294 | - * |
|
295 | - * @access public |
|
296 | - * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
|
297 | - * @param bool $pretty Whether to set pretty format or not. |
|
298 | - * @return void |
|
299 | - */ |
|
300 | - public function set_date_format($format, $pretty = false) |
|
301 | - { |
|
302 | - if ($pretty) { |
|
303 | - $this->_pretty_date_format = $format; |
|
304 | - } else { |
|
305 | - $this->_date_format = $format; |
|
306 | - } |
|
307 | - } |
|
308 | - |
|
309 | - |
|
310 | - /** |
|
311 | - * return the $_date_format property value. |
|
312 | - * |
|
313 | - * @param bool $pretty Whether to get pretty format or not. |
|
314 | - * @return string |
|
315 | - */ |
|
316 | - public function get_date_format($pretty = false) |
|
317 | - { |
|
318 | - return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
319 | - } |
|
320 | - |
|
321 | - |
|
322 | - /** |
|
323 | - * set the $_time_format property |
|
324 | - * |
|
325 | - * @access public |
|
326 | - * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
|
327 | - * @param bool $pretty Whether to set pretty format or not. |
|
328 | - * @return void |
|
329 | - */ |
|
330 | - public function set_time_format($format, $pretty = false) |
|
331 | - { |
|
332 | - if ($pretty) { |
|
333 | - $this->_pretty_time_format = $format; |
|
334 | - } else { |
|
335 | - $this->_time_format = $format; |
|
336 | - } |
|
337 | - } |
|
338 | - |
|
339 | - |
|
340 | - /** |
|
341 | - * return the $_time_format property value. |
|
342 | - * |
|
343 | - * @param bool $pretty Whether to get pretty format or not. |
|
344 | - * @return string |
|
345 | - */ |
|
346 | - public function get_time_format($pretty = false) |
|
347 | - { |
|
348 | - return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
349 | - } |
|
350 | - |
|
351 | - |
|
352 | - /** |
|
353 | - * set the $_pretty_date_format property |
|
354 | - * |
|
355 | - * @access public |
|
356 | - * @param string $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
|
357 | - * @return void |
|
358 | - */ |
|
359 | - public function set_pretty_date_format($format) |
|
360 | - { |
|
361 | - $this->_pretty_date_format = $format; |
|
362 | - } |
|
363 | - |
|
364 | - |
|
365 | - /** |
|
366 | - * set the $_pretty_time_format property |
|
367 | - * |
|
368 | - * @access public |
|
369 | - * @param string $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
|
370 | - * @return void |
|
371 | - */ |
|
372 | - public function set_pretty_time_format($format) |
|
373 | - { |
|
374 | - $this->_pretty_time_format = $format; |
|
375 | - } |
|
376 | - |
|
377 | - |
|
378 | - /** |
|
379 | - * Only sets the time portion of the datetime. |
|
380 | - * |
|
381 | - * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
|
382 | - * @param DateTime $current current DateTime object for the datetime field |
|
383 | - * @return DateTime |
|
384 | - */ |
|
385 | - public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current) |
|
386 | - { |
|
387 | - // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
388 | - // Otherwise parse the string. |
|
389 | - if ($time_to_set_string instanceof DateTime) { |
|
390 | - $parsed = array( |
|
391 | - 'hour' => $time_to_set_string->format('H'), |
|
392 | - 'minute' => $time_to_set_string->format('i'), |
|
393 | - 'second' => $time_to_set_string->format('s'), |
|
394 | - ); |
|
395 | - } else { |
|
396 | - //parse incoming string |
|
397 | - $parsed = date_parse_from_format($this->_time_format, $time_to_set_string); |
|
398 | - } |
|
399 | - EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
400 | - return $current->setTime($parsed['hour'], $parsed['minute'], $parsed['second']); |
|
401 | - } |
|
402 | - |
|
403 | - |
|
404 | - /** |
|
405 | - * Only sets the date portion of the datetime. |
|
406 | - * |
|
407 | - * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
|
408 | - * @param DateTime $current current DateTime object for the datetime field |
|
409 | - * @return DateTime |
|
410 | - */ |
|
411 | - public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current) |
|
412 | - { |
|
413 | - // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
414 | - // Otherwise parse the string. |
|
415 | - if ($date_to_set_string instanceof DateTime) { |
|
416 | - $parsed = array( |
|
417 | - 'year' => $date_to_set_string->format('Y'), |
|
418 | - 'month' => $date_to_set_string->format('m'), |
|
419 | - 'day' => $date_to_set_string->format('d'), |
|
420 | - ); |
|
421 | - } else { |
|
422 | - //parse incoming string |
|
423 | - $parsed = date_parse_from_format($this->_date_format, $date_to_set_string); |
|
424 | - } |
|
425 | - EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
426 | - return $current->setDate($parsed['year'], $parsed['month'], $parsed['day']); |
|
427 | - } |
|
428 | - |
|
429 | - |
|
430 | - /** |
|
431 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
|
432 | - * datetime gets to this stage it should ALREADY be in UTC time |
|
433 | - * |
|
434 | - * @param DateTime $DateTime |
|
435 | - * @return string formatted date time for given timezone |
|
436 | - * @throws \EE_Error |
|
437 | - */ |
|
438 | - public function prepare_for_get($DateTime) |
|
439 | - { |
|
440 | - return $this->_prepare_for_display($DateTime); |
|
441 | - } |
|
442 | - |
|
443 | - |
|
444 | - /** |
|
445 | - * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
|
446 | - * from the set wp timezone. If so, then it returns the datetime string formatted via |
|
447 | - * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
|
448 | - * abbreviation to the date_string. |
|
449 | - * |
|
450 | - * @param mixed $DateTime |
|
451 | - * @param null $schema |
|
452 | - * @return string |
|
453 | - * @throws \EE_Error |
|
454 | - */ |
|
455 | - public function prepare_for_pretty_echoing($DateTime, $schema = null) |
|
456 | - { |
|
457 | - return $this->_prepare_for_display($DateTime, $schema ? $schema : true); |
|
458 | - } |
|
459 | - |
|
460 | - |
|
461 | - /** |
|
462 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
463 | - * timezone). |
|
464 | - * |
|
465 | - * @param DateTime $DateTime |
|
466 | - * @param bool|string $schema |
|
467 | - * @return string |
|
468 | - * @throws \EE_Error |
|
469 | - */ |
|
470 | - protected function _prepare_for_display($DateTime, $schema = false) |
|
471 | - { |
|
472 | - if (! $DateTime instanceof DateTime) { |
|
473 | - if ($this->_nullable) { |
|
474 | - return ''; |
|
475 | - } else { |
|
476 | - if (WP_DEBUG) { |
|
477 | - throw new EE_Error( |
|
478 | - sprintf( |
|
479 | - __( |
|
480 | - 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable.', |
|
481 | - 'event_espresso' |
|
482 | - ), |
|
483 | - $this->_nicename |
|
484 | - ) |
|
485 | - ); |
|
486 | - } else { |
|
487 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now); |
|
488 | - EE_Error::add_error( |
|
489 | - sprintf( |
|
490 | - __( |
|
491 | - 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable. When WP_DEBUG is false, the value is set to "now" instead of throwing an exception.', |
|
492 | - 'event_espresso' |
|
493 | - ), |
|
494 | - $this->_nicename |
|
495 | - ) |
|
496 | - ); |
|
497 | - } |
|
498 | - } |
|
499 | - } |
|
500 | - $format_string = $this->_get_date_time_output($schema); |
|
501 | - EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
502 | - if ($schema) { |
|
503 | - if ($this->_display_timezone()) { |
|
504 | - //must be explicit because schema could equal true. |
|
505 | - if ($schema === 'no_html') { |
|
506 | - $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
507 | - } else { |
|
508 | - $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
509 | - } |
|
510 | - } else { |
|
511 | - $timezone_string = ''; |
|
512 | - } |
|
513 | - |
|
514 | - return $DateTime->format($format_string) . $timezone_string; |
|
515 | - } |
|
516 | - return $DateTime->format($format_string); |
|
517 | - } |
|
518 | - |
|
519 | - |
|
520 | - /** |
|
521 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
522 | - * timezone). |
|
523 | - * |
|
524 | - * @param mixed $datetime_value u |
|
525 | - * @return string mysql timestamp in UTC |
|
526 | - * @throws \EE_Error |
|
527 | - */ |
|
528 | - public function prepare_for_use_in_db($datetime_value) |
|
529 | - { |
|
530 | - //we allow an empty value or DateTime object, but nothing else. |
|
531 | - if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
532 | - throw new EE_Error( |
|
533 | - sprintf( |
|
534 | - __( |
|
535 | - 'The incoming value being prepared for setting in the database must either be empty or a php |
|
20 | + /** |
|
21 | + * The pattern we're looking for is if only the characters 0-9 are found and there are only |
|
22 | + * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
|
23 | + * |
|
24 | + * @type string unix_timestamp_regex |
|
25 | + */ |
|
26 | + const unix_timestamp_regex = '/[0-9]{10,}/'; |
|
27 | + |
|
28 | + /** |
|
29 | + * @type string mysql_timestamp_format |
|
30 | + */ |
|
31 | + const mysql_timestamp_format = 'Y-m-d H:i:s'; |
|
32 | + |
|
33 | + /** |
|
34 | + * @type string mysql_date_format |
|
35 | + */ |
|
36 | + const mysql_date_format = 'Y-m-d'; |
|
37 | + |
|
38 | + /** |
|
39 | + * @type string mysql_time_format |
|
40 | + */ |
|
41 | + const mysql_time_format = 'H:i:s'; |
|
42 | + |
|
43 | + /** |
|
44 | + * Const for using in the default value. If the field's default is set to this, |
|
45 | + * then we will return the time of calling `get_default_value()`, not |
|
46 | + * just the current time at construction |
|
47 | + */ |
|
48 | + const now = 'now'; |
|
49 | + |
|
50 | + /** |
|
51 | + * The following properties hold the default formats for date and time. |
|
52 | + * Defaults are set via the constructor and can be overridden on class instantiation. |
|
53 | + * However they can also be overridden later by the set_format() method |
|
54 | + * (and corresponding set_date_format, set_time_format methods); |
|
55 | + */ |
|
56 | + /** |
|
57 | + * @type string $_date_format |
|
58 | + */ |
|
59 | + protected $_date_format = ''; |
|
60 | + |
|
61 | + /** |
|
62 | + * @type string $_time_format |
|
63 | + */ |
|
64 | + protected $_time_format = ''; |
|
65 | + |
|
66 | + /** |
|
67 | + * @type string $_pretty_date_format |
|
68 | + */ |
|
69 | + protected $_pretty_date_format = ''; |
|
70 | + |
|
71 | + /** |
|
72 | + * @type string $_pretty_time_format |
|
73 | + */ |
|
74 | + protected $_pretty_time_format = ''; |
|
75 | + |
|
76 | + /** |
|
77 | + * @type DateTimeZone $_DateTimeZone |
|
78 | + */ |
|
79 | + protected $_DateTimeZone; |
|
80 | + |
|
81 | + /** |
|
82 | + * @type DateTimeZone $_UTC_DateTimeZone |
|
83 | + */ |
|
84 | + protected $_UTC_DateTimeZone; |
|
85 | + |
|
86 | + /** |
|
87 | + * @type DateTimeZone $_blog_DateTimeZone |
|
88 | + */ |
|
89 | + protected $_blog_DateTimeZone; |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * This property holds how we want the output returned when getting a datetime string. It is set for the |
|
94 | + * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
|
95 | + * and time returned via getters. |
|
96 | + * |
|
97 | + * @var mixed (null|string) |
|
98 | + */ |
|
99 | + protected $_date_time_output; |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * timezone string |
|
104 | + * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
|
105 | + * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
|
106 | + * coming out of the object to be in. Default timezone is the current WP timezone option setting |
|
107 | + * |
|
108 | + * @var string |
|
109 | + */ |
|
110 | + protected $_timezone_string; |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
|
115 | + * offsets for comparison purposes). |
|
116 | + * |
|
117 | + * @var int |
|
118 | + */ |
|
119 | + protected $_blog_offset; |
|
120 | + |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * @param string $table_column |
|
125 | + * @param string $nice_name |
|
126 | + * @param bool $nullable |
|
127 | + * @param string $default_value |
|
128 | + * @param string $timezone_string |
|
129 | + * @param string $date_format |
|
130 | + * @param string $time_format |
|
131 | + * @param string $pretty_date_format |
|
132 | + * @param string $pretty_time_format |
|
133 | + * @throws EE_Error |
|
134 | + * @throws InvalidArgumentException |
|
135 | + */ |
|
136 | + public function __construct( |
|
137 | + $table_column, |
|
138 | + $nice_name, |
|
139 | + $nullable, |
|
140 | + $default_value, |
|
141 | + $timezone_string = '', |
|
142 | + $date_format = '', |
|
143 | + $time_format = '', |
|
144 | + $pretty_date_format = '', |
|
145 | + $pretty_time_format = '' |
|
146 | + ) { |
|
147 | + |
|
148 | + $this->_date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
149 | + $this->_time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
150 | + $this->_pretty_date_format = ! empty($pretty_date_format) ? $pretty_date_format : get_option('date_format'); |
|
151 | + $this->_pretty_time_format = ! empty($pretty_time_format) ? $pretty_time_format : get_option('time_format'); |
|
152 | + |
|
153 | + parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
154 | + $this->set_timezone($timezone_string); |
|
155 | + $this->setSchemaFormat('date-time'); |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + /** |
|
160 | + * @return DateTimeZone |
|
161 | + * @throws \EE_Error |
|
162 | + */ |
|
163 | + public function get_UTC_DateTimeZone() |
|
164 | + { |
|
165 | + return $this->_UTC_DateTimeZone instanceof DateTimeZone |
|
166 | + ? $this->_UTC_DateTimeZone |
|
167 | + : $this->_create_timezone_object_from_timezone_string('UTC'); |
|
168 | + } |
|
169 | + |
|
170 | + |
|
171 | + /** |
|
172 | + * @return DateTimeZone |
|
173 | + * @throws \EE_Error |
|
174 | + */ |
|
175 | + public function get_blog_DateTimeZone() |
|
176 | + { |
|
177 | + return $this->_blog_DateTimeZone instanceof DateTimeZone |
|
178 | + ? $this->_blog_DateTimeZone |
|
179 | + : $this->_create_timezone_object_from_timezone_string(''); |
|
180 | + } |
|
181 | + |
|
182 | + |
|
183 | + /** |
|
184 | + * this prepares any incoming date data and make sure its converted to a utc unix timestamp |
|
185 | + * |
|
186 | + * @param string|int $value_inputted_for_field_on_model_object could be a string formatted date time or int unix |
|
187 | + * timestamp |
|
188 | + * @return DateTime |
|
189 | + */ |
|
190 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
191 | + { |
|
192 | + return $this->_get_date_object($value_inputted_for_field_on_model_object); |
|
193 | + } |
|
194 | + |
|
195 | + |
|
196 | + /** |
|
197 | + * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
|
198 | + * getters need to know whether we're just returning the date or the time or both. By default we return both. |
|
199 | + * |
|
200 | + * @param bool $pretty If we're returning the pretty formats or standard format string. |
|
201 | + * @return string The final assembled format string. |
|
202 | + */ |
|
203 | + protected function _get_date_time_output($pretty = false) |
|
204 | + { |
|
205 | + |
|
206 | + switch ($this->_date_time_output) { |
|
207 | + case 'time' : |
|
208 | + return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
209 | + break; |
|
210 | + |
|
211 | + case 'date' : |
|
212 | + return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
213 | + break; |
|
214 | + |
|
215 | + default : |
|
216 | + return $pretty |
|
217 | + ? $this->_pretty_date_format . ' ' . $this->_pretty_time_format |
|
218 | + : $this->_date_format . ' ' . $this->_time_format; |
|
219 | + } |
|
220 | + } |
|
221 | + |
|
222 | + |
|
223 | + /** |
|
224 | + * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
|
225 | + * returned (using the format properties) |
|
226 | + * |
|
227 | + * @param string $what acceptable values are 'time' or 'date'. |
|
228 | + * Any other value will be set but will always result |
|
229 | + * in both 'date' and 'time' being returned. |
|
230 | + * @return void |
|
231 | + */ |
|
232 | + public function set_date_time_output($what = null) |
|
233 | + { |
|
234 | + $this->_date_time_output = $what; |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + /** |
|
239 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
240 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
241 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
|
242 | + * We also set some other properties in this method. |
|
243 | + * |
|
244 | + * @param string $timezone_string A valid timezone string as described by @link |
|
245 | + * http://www.php.net/manual/en/timezones.php |
|
246 | + * @return void |
|
247 | + * @throws InvalidArgumentException |
|
248 | + * @throws InvalidDataTypeException |
|
249 | + * @throws InvalidInterfaceException |
|
250 | + */ |
|
251 | + public function set_timezone($timezone_string) |
|
252 | + { |
|
253 | + if (empty($timezone_string) && $this->_timezone_string !== null) { |
|
254 | + // leave the timezone AS-IS if we already have one and |
|
255 | + // the function arg didn't provide one |
|
256 | + return; |
|
257 | + } |
|
258 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
259 | + $this->_timezone_string = ! empty($timezone_string) ? $timezone_string : 'UTC'; |
|
260 | + $this->_DateTimeZone = $this->_create_timezone_object_from_timezone_string($this->_timezone_string); |
|
261 | + } |
|
262 | + |
|
263 | + |
|
264 | + /** |
|
265 | + * _create_timezone_object_from_timezone_name |
|
266 | + * |
|
267 | + * @access protected |
|
268 | + * @param string $timezone_string |
|
269 | + * @return \DateTimeZone |
|
270 | + * @throws InvalidArgumentException |
|
271 | + * @throws InvalidDataTypeException |
|
272 | + * @throws InvalidInterfaceException |
|
273 | + */ |
|
274 | + protected function _create_timezone_object_from_timezone_string($timezone_string = '') |
|
275 | + { |
|
276 | + return new DateTimeZone(EEH_DTT_Helper::get_valid_timezone_string($timezone_string)); |
|
277 | + } |
|
278 | + |
|
279 | + |
|
280 | + /** |
|
281 | + * This just returns whatever is set for the current timezone. |
|
282 | + * |
|
283 | + * @access public |
|
284 | + * @return string timezone string |
|
285 | + */ |
|
286 | + public function get_timezone() |
|
287 | + { |
|
288 | + return $this->_timezone_string; |
|
289 | + } |
|
290 | + |
|
291 | + |
|
292 | + /** |
|
293 | + * set the $_date_format property |
|
294 | + * |
|
295 | + * @access public |
|
296 | + * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
|
297 | + * @param bool $pretty Whether to set pretty format or not. |
|
298 | + * @return void |
|
299 | + */ |
|
300 | + public function set_date_format($format, $pretty = false) |
|
301 | + { |
|
302 | + if ($pretty) { |
|
303 | + $this->_pretty_date_format = $format; |
|
304 | + } else { |
|
305 | + $this->_date_format = $format; |
|
306 | + } |
|
307 | + } |
|
308 | + |
|
309 | + |
|
310 | + /** |
|
311 | + * return the $_date_format property value. |
|
312 | + * |
|
313 | + * @param bool $pretty Whether to get pretty format or not. |
|
314 | + * @return string |
|
315 | + */ |
|
316 | + public function get_date_format($pretty = false) |
|
317 | + { |
|
318 | + return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
319 | + } |
|
320 | + |
|
321 | + |
|
322 | + /** |
|
323 | + * set the $_time_format property |
|
324 | + * |
|
325 | + * @access public |
|
326 | + * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
|
327 | + * @param bool $pretty Whether to set pretty format or not. |
|
328 | + * @return void |
|
329 | + */ |
|
330 | + public function set_time_format($format, $pretty = false) |
|
331 | + { |
|
332 | + if ($pretty) { |
|
333 | + $this->_pretty_time_format = $format; |
|
334 | + } else { |
|
335 | + $this->_time_format = $format; |
|
336 | + } |
|
337 | + } |
|
338 | + |
|
339 | + |
|
340 | + /** |
|
341 | + * return the $_time_format property value. |
|
342 | + * |
|
343 | + * @param bool $pretty Whether to get pretty format or not. |
|
344 | + * @return string |
|
345 | + */ |
|
346 | + public function get_time_format($pretty = false) |
|
347 | + { |
|
348 | + return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
349 | + } |
|
350 | + |
|
351 | + |
|
352 | + /** |
|
353 | + * set the $_pretty_date_format property |
|
354 | + * |
|
355 | + * @access public |
|
356 | + * @param string $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
|
357 | + * @return void |
|
358 | + */ |
|
359 | + public function set_pretty_date_format($format) |
|
360 | + { |
|
361 | + $this->_pretty_date_format = $format; |
|
362 | + } |
|
363 | + |
|
364 | + |
|
365 | + /** |
|
366 | + * set the $_pretty_time_format property |
|
367 | + * |
|
368 | + * @access public |
|
369 | + * @param string $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
|
370 | + * @return void |
|
371 | + */ |
|
372 | + public function set_pretty_time_format($format) |
|
373 | + { |
|
374 | + $this->_pretty_time_format = $format; |
|
375 | + } |
|
376 | + |
|
377 | + |
|
378 | + /** |
|
379 | + * Only sets the time portion of the datetime. |
|
380 | + * |
|
381 | + * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
|
382 | + * @param DateTime $current current DateTime object for the datetime field |
|
383 | + * @return DateTime |
|
384 | + */ |
|
385 | + public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current) |
|
386 | + { |
|
387 | + // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
388 | + // Otherwise parse the string. |
|
389 | + if ($time_to_set_string instanceof DateTime) { |
|
390 | + $parsed = array( |
|
391 | + 'hour' => $time_to_set_string->format('H'), |
|
392 | + 'minute' => $time_to_set_string->format('i'), |
|
393 | + 'second' => $time_to_set_string->format('s'), |
|
394 | + ); |
|
395 | + } else { |
|
396 | + //parse incoming string |
|
397 | + $parsed = date_parse_from_format($this->_time_format, $time_to_set_string); |
|
398 | + } |
|
399 | + EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
400 | + return $current->setTime($parsed['hour'], $parsed['minute'], $parsed['second']); |
|
401 | + } |
|
402 | + |
|
403 | + |
|
404 | + /** |
|
405 | + * Only sets the date portion of the datetime. |
|
406 | + * |
|
407 | + * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
|
408 | + * @param DateTime $current current DateTime object for the datetime field |
|
409 | + * @return DateTime |
|
410 | + */ |
|
411 | + public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current) |
|
412 | + { |
|
413 | + // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
414 | + // Otherwise parse the string. |
|
415 | + if ($date_to_set_string instanceof DateTime) { |
|
416 | + $parsed = array( |
|
417 | + 'year' => $date_to_set_string->format('Y'), |
|
418 | + 'month' => $date_to_set_string->format('m'), |
|
419 | + 'day' => $date_to_set_string->format('d'), |
|
420 | + ); |
|
421 | + } else { |
|
422 | + //parse incoming string |
|
423 | + $parsed = date_parse_from_format($this->_date_format, $date_to_set_string); |
|
424 | + } |
|
425 | + EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
426 | + return $current->setDate($parsed['year'], $parsed['month'], $parsed['day']); |
|
427 | + } |
|
428 | + |
|
429 | + |
|
430 | + /** |
|
431 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
|
432 | + * datetime gets to this stage it should ALREADY be in UTC time |
|
433 | + * |
|
434 | + * @param DateTime $DateTime |
|
435 | + * @return string formatted date time for given timezone |
|
436 | + * @throws \EE_Error |
|
437 | + */ |
|
438 | + public function prepare_for_get($DateTime) |
|
439 | + { |
|
440 | + return $this->_prepare_for_display($DateTime); |
|
441 | + } |
|
442 | + |
|
443 | + |
|
444 | + /** |
|
445 | + * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
|
446 | + * from the set wp timezone. If so, then it returns the datetime string formatted via |
|
447 | + * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
|
448 | + * abbreviation to the date_string. |
|
449 | + * |
|
450 | + * @param mixed $DateTime |
|
451 | + * @param null $schema |
|
452 | + * @return string |
|
453 | + * @throws \EE_Error |
|
454 | + */ |
|
455 | + public function prepare_for_pretty_echoing($DateTime, $schema = null) |
|
456 | + { |
|
457 | + return $this->_prepare_for_display($DateTime, $schema ? $schema : true); |
|
458 | + } |
|
459 | + |
|
460 | + |
|
461 | + /** |
|
462 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
463 | + * timezone). |
|
464 | + * |
|
465 | + * @param DateTime $DateTime |
|
466 | + * @param bool|string $schema |
|
467 | + * @return string |
|
468 | + * @throws \EE_Error |
|
469 | + */ |
|
470 | + protected function _prepare_for_display($DateTime, $schema = false) |
|
471 | + { |
|
472 | + if (! $DateTime instanceof DateTime) { |
|
473 | + if ($this->_nullable) { |
|
474 | + return ''; |
|
475 | + } else { |
|
476 | + if (WP_DEBUG) { |
|
477 | + throw new EE_Error( |
|
478 | + sprintf( |
|
479 | + __( |
|
480 | + 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable.', |
|
481 | + 'event_espresso' |
|
482 | + ), |
|
483 | + $this->_nicename |
|
484 | + ) |
|
485 | + ); |
|
486 | + } else { |
|
487 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now); |
|
488 | + EE_Error::add_error( |
|
489 | + sprintf( |
|
490 | + __( |
|
491 | + 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable. When WP_DEBUG is false, the value is set to "now" instead of throwing an exception.', |
|
492 | + 'event_espresso' |
|
493 | + ), |
|
494 | + $this->_nicename |
|
495 | + ) |
|
496 | + ); |
|
497 | + } |
|
498 | + } |
|
499 | + } |
|
500 | + $format_string = $this->_get_date_time_output($schema); |
|
501 | + EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
502 | + if ($schema) { |
|
503 | + if ($this->_display_timezone()) { |
|
504 | + //must be explicit because schema could equal true. |
|
505 | + if ($schema === 'no_html') { |
|
506 | + $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
507 | + } else { |
|
508 | + $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
509 | + } |
|
510 | + } else { |
|
511 | + $timezone_string = ''; |
|
512 | + } |
|
513 | + |
|
514 | + return $DateTime->format($format_string) . $timezone_string; |
|
515 | + } |
|
516 | + return $DateTime->format($format_string); |
|
517 | + } |
|
518 | + |
|
519 | + |
|
520 | + /** |
|
521 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
522 | + * timezone). |
|
523 | + * |
|
524 | + * @param mixed $datetime_value u |
|
525 | + * @return string mysql timestamp in UTC |
|
526 | + * @throws \EE_Error |
|
527 | + */ |
|
528 | + public function prepare_for_use_in_db($datetime_value) |
|
529 | + { |
|
530 | + //we allow an empty value or DateTime object, but nothing else. |
|
531 | + if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
532 | + throw new EE_Error( |
|
533 | + sprintf( |
|
534 | + __( |
|
535 | + 'The incoming value being prepared for setting in the database must either be empty or a php |
|
536 | 536 | DateTime object, instead of: %1$s %2$s', |
537 | - 'event_espresso' |
|
538 | - ), |
|
539 | - '<br />', |
|
540 | - print_r($datetime_value, true) |
|
541 | - ) |
|
542 | - ); |
|
543 | - } |
|
544 | - |
|
545 | - if ($datetime_value instanceof DateTime) { |
|
546 | - if (! $datetime_value instanceof DbSafeDateTime) { |
|
547 | - $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
|
548 | - } |
|
549 | - EEH_DTT_Helper::setTimezone($datetime_value, $this->get_UTC_DateTimeZone()); |
|
550 | - return $datetime_value->format( |
|
551 | - EE_Datetime_Field::mysql_timestamp_format |
|
552 | - ); |
|
553 | - } |
|
554 | - |
|
555 | - // if $datetime_value is empty, and ! $this->_nullable, use current_time() but set the GMT flag to true |
|
556 | - return ! $this->_nullable && empty($datetime_value) ? current_time('mysql', true) : null; |
|
557 | - } |
|
558 | - |
|
559 | - |
|
560 | - /** |
|
561 | - * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
|
562 | - * allowed) |
|
563 | - * |
|
564 | - * @param string $datetime_string mysql timestamp in UTC |
|
565 | - * @return mixed null | DateTime |
|
566 | - * @throws \EE_Error |
|
567 | - */ |
|
568 | - public function prepare_for_set_from_db($datetime_string) |
|
569 | - { |
|
570 | - //if $datetime_value is empty, and ! $this->_nullable, just use time() |
|
571 | - if (empty($datetime_string) && $this->_nullable) { |
|
572 | - return null; |
|
573 | - } |
|
574 | - // datetime strings from the db should ALWAYS be in UTC+0, so use UTC_DateTimeZone when creating |
|
575 | - if (empty($datetime_string)) { |
|
576 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
577 | - } else { |
|
578 | - $DateTime = DateTime::createFromFormat( |
|
579 | - EE_Datetime_Field::mysql_timestamp_format, |
|
580 | - $datetime_string, |
|
581 | - $this->get_UTC_DateTimeZone() |
|
582 | - ); |
|
583 | - if ($DateTime instanceof \DateTime) { |
|
584 | - $DateTime = new DbSafeDateTime( |
|
585 | - $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
586 | - $this->get_UTC_DateTimeZone() |
|
587 | - ); |
|
588 | - } |
|
589 | - } |
|
590 | - |
|
591 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
592 | - // if still no datetime object, then let's just use now |
|
593 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
594 | - } |
|
595 | - // THEN apply the field's set DateTimeZone |
|
596 | - EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
597 | - return $DateTime; |
|
598 | - } |
|
599 | - |
|
600 | - |
|
601 | - /** |
|
602 | - * All this method does is determine if we're going to display the timezone string or not on any output. |
|
603 | - * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
|
604 | - * If so, then true. |
|
605 | - * |
|
606 | - * @return bool true for yes false for no |
|
607 | - * @throws \EE_Error |
|
608 | - */ |
|
609 | - protected function _display_timezone() |
|
610 | - { |
|
611 | - |
|
612 | - // first let's do a comparison of timezone strings. |
|
613 | - // If they match then we can get out without any further calculations |
|
614 | - $blog_string = get_option('timezone_string'); |
|
615 | - if ($blog_string === $this->_timezone_string) { |
|
616 | - return false; |
|
617 | - } |
|
618 | - // now we need to calc the offset for the timezone string so we can compare with the blog offset. |
|
619 | - $this_offset = $this->get_timezone_offset($this->_DateTimeZone); |
|
620 | - $blog_offset = $this->get_timezone_offset($this->get_blog_DateTimeZone()); |
|
621 | - // now compare |
|
622 | - return $blog_offset !== $this_offset; |
|
623 | - } |
|
624 | - |
|
625 | - |
|
626 | - /** |
|
627 | - * This method returns a php DateTime object for setting on the EE_Base_Class model. |
|
628 | - * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
|
629 | - * with. |
|
630 | - * |
|
631 | - * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
|
632 | - * in the format that is set on the date_field (or DateTime |
|
633 | - * object)! |
|
634 | - * @return DateTime |
|
635 | - */ |
|
636 | - protected function _get_date_object($date_string) |
|
637 | - { |
|
638 | - //first if this is an empty date_string and nullable is allowed, just return null. |
|
639 | - if ($this->_nullable && empty($date_string)) { |
|
640 | - return null; |
|
641 | - } |
|
642 | - |
|
643 | - // if incoming date |
|
644 | - if ($date_string instanceof DateTime) { |
|
645 | - EEH_DTT_Helper::setTimezone($date_string, $this->_DateTimeZone); |
|
646 | - return $date_string; |
|
647 | - } |
|
648 | - // if empty date_string and made it here. |
|
649 | - // Return a datetime object for now in the given timezone. |
|
650 | - if (empty($date_string)) { |
|
651 | - return new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
652 | - } |
|
653 | - // if $date_string is matches something that looks like a Unix timestamp let's just use it. |
|
654 | - if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $date_string)) { |
|
655 | - try { |
|
656 | - // This is operating under the assumption that the incoming Unix timestamp |
|
657 | - // is an ACTUAL Unix timestamp and not the calculated one output by current_time('timestamp'); |
|
658 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
659 | - $DateTime->setTimestamp($date_string); |
|
660 | - |
|
661 | - return $DateTime; |
|
662 | - } catch (Exception $e) { |
|
663 | - // should be rare, but if things got fooled then let's just continue |
|
664 | - } |
|
665 | - } |
|
666 | - //not a unix timestamp. So we will use the set format on this object and set timezone to |
|
667 | - //create the DateTime object. |
|
668 | - $format = $this->_date_format . ' ' . $this->_time_format; |
|
669 | - try { |
|
670 | - $DateTime = DateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
|
671 | - if ($DateTime instanceof DateTime) { |
|
672 | - $DateTime = new DbSafeDateTime( |
|
673 | - $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
674 | - $this->_DateTimeZone |
|
675 | - ); |
|
676 | - } |
|
677 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
678 | - throw new EE_Error( |
|
679 | - sprintf( |
|
680 | - __('"%1$s" does not represent a valid Date Time in the format "%2$s".', 'event_espresso'), |
|
681 | - $date_string, |
|
682 | - $format |
|
683 | - ) |
|
684 | - ); |
|
685 | - } |
|
686 | - } catch (Exception $e) { |
|
687 | - // if we made it here then likely then something went really wrong. |
|
688 | - // Instead of throwing an exception, let's just return a DateTime object for now, in the set timezone. |
|
689 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
690 | - } |
|
691 | - |
|
692 | - return $DateTime; |
|
693 | - } |
|
694 | - |
|
695 | - |
|
696 | - |
|
697 | - /** |
|
698 | - * get_timezone_transitions |
|
699 | - * |
|
700 | - * @param \DateTimeZone $DateTimeZone |
|
701 | - * @param int $time |
|
702 | - * @param bool $first_only |
|
703 | - * @return mixed |
|
704 | - */ |
|
705 | - public function get_timezone_transitions(DateTimeZone $DateTimeZone, $time = null, $first_only = true) |
|
706 | - { |
|
707 | - return EEH_DTT_Helper::get_timezone_transitions($DateTimeZone, $time, $first_only); |
|
708 | - } |
|
709 | - |
|
710 | - |
|
711 | - |
|
712 | - /** |
|
713 | - * get_timezone_offset |
|
714 | - * |
|
715 | - * @param \DateTimeZone $DateTimeZone |
|
716 | - * @param int $time |
|
717 | - * @return mixed |
|
718 | - * @throws \DomainException |
|
719 | - */ |
|
720 | - public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
|
721 | - { |
|
722 | - return EEH_DTT_Helper::get_timezone_offset($DateTimeZone, $time); |
|
723 | - } |
|
724 | - |
|
725 | - |
|
726 | - /** |
|
727 | - * This will take an incoming timezone string and return the abbreviation for that timezone |
|
728 | - * |
|
729 | - * @param string $timezone_string |
|
730 | - * @return string abbreviation |
|
731 | - * @throws \EE_Error |
|
732 | - */ |
|
733 | - public function get_timezone_abbrev($timezone_string) |
|
734 | - { |
|
735 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
736 | - $dateTime = new DateTime(\EE_Datetime_Field::now, new DateTimeZone($timezone_string)); |
|
737 | - |
|
738 | - return $dateTime->format('T'); |
|
739 | - } |
|
740 | - |
|
741 | - /** |
|
742 | - * Overrides the parent to allow for having a dynamic "now" value |
|
743 | - * |
|
744 | - * @return mixed |
|
745 | - */ |
|
746 | - public function get_default_value() |
|
747 | - { |
|
748 | - if ($this->_default_value === EE_Datetime_Field::now) { |
|
749 | - return time(); |
|
750 | - } else { |
|
751 | - return parent::get_default_value(); |
|
752 | - } |
|
753 | - } |
|
754 | - |
|
755 | - |
|
756 | - public function getSchemaDescription() |
|
757 | - { |
|
758 | - return sprintf( |
|
759 | - esc_html__('%s - the value for this field is in the timezone of the site.', 'event_espresso'), |
|
760 | - $this->get_nicename() |
|
761 | - ); |
|
762 | - } |
|
537 | + 'event_espresso' |
|
538 | + ), |
|
539 | + '<br />', |
|
540 | + print_r($datetime_value, true) |
|
541 | + ) |
|
542 | + ); |
|
543 | + } |
|
544 | + |
|
545 | + if ($datetime_value instanceof DateTime) { |
|
546 | + if (! $datetime_value instanceof DbSafeDateTime) { |
|
547 | + $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
|
548 | + } |
|
549 | + EEH_DTT_Helper::setTimezone($datetime_value, $this->get_UTC_DateTimeZone()); |
|
550 | + return $datetime_value->format( |
|
551 | + EE_Datetime_Field::mysql_timestamp_format |
|
552 | + ); |
|
553 | + } |
|
554 | + |
|
555 | + // if $datetime_value is empty, and ! $this->_nullable, use current_time() but set the GMT flag to true |
|
556 | + return ! $this->_nullable && empty($datetime_value) ? current_time('mysql', true) : null; |
|
557 | + } |
|
558 | + |
|
559 | + |
|
560 | + /** |
|
561 | + * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
|
562 | + * allowed) |
|
563 | + * |
|
564 | + * @param string $datetime_string mysql timestamp in UTC |
|
565 | + * @return mixed null | DateTime |
|
566 | + * @throws \EE_Error |
|
567 | + */ |
|
568 | + public function prepare_for_set_from_db($datetime_string) |
|
569 | + { |
|
570 | + //if $datetime_value is empty, and ! $this->_nullable, just use time() |
|
571 | + if (empty($datetime_string) && $this->_nullable) { |
|
572 | + return null; |
|
573 | + } |
|
574 | + // datetime strings from the db should ALWAYS be in UTC+0, so use UTC_DateTimeZone when creating |
|
575 | + if (empty($datetime_string)) { |
|
576 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
577 | + } else { |
|
578 | + $DateTime = DateTime::createFromFormat( |
|
579 | + EE_Datetime_Field::mysql_timestamp_format, |
|
580 | + $datetime_string, |
|
581 | + $this->get_UTC_DateTimeZone() |
|
582 | + ); |
|
583 | + if ($DateTime instanceof \DateTime) { |
|
584 | + $DateTime = new DbSafeDateTime( |
|
585 | + $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
586 | + $this->get_UTC_DateTimeZone() |
|
587 | + ); |
|
588 | + } |
|
589 | + } |
|
590 | + |
|
591 | + if (! $DateTime instanceof DbSafeDateTime) { |
|
592 | + // if still no datetime object, then let's just use now |
|
593 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
594 | + } |
|
595 | + // THEN apply the field's set DateTimeZone |
|
596 | + EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
597 | + return $DateTime; |
|
598 | + } |
|
599 | + |
|
600 | + |
|
601 | + /** |
|
602 | + * All this method does is determine if we're going to display the timezone string or not on any output. |
|
603 | + * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
|
604 | + * If so, then true. |
|
605 | + * |
|
606 | + * @return bool true for yes false for no |
|
607 | + * @throws \EE_Error |
|
608 | + */ |
|
609 | + protected function _display_timezone() |
|
610 | + { |
|
611 | + |
|
612 | + // first let's do a comparison of timezone strings. |
|
613 | + // If they match then we can get out without any further calculations |
|
614 | + $blog_string = get_option('timezone_string'); |
|
615 | + if ($blog_string === $this->_timezone_string) { |
|
616 | + return false; |
|
617 | + } |
|
618 | + // now we need to calc the offset for the timezone string so we can compare with the blog offset. |
|
619 | + $this_offset = $this->get_timezone_offset($this->_DateTimeZone); |
|
620 | + $blog_offset = $this->get_timezone_offset($this->get_blog_DateTimeZone()); |
|
621 | + // now compare |
|
622 | + return $blog_offset !== $this_offset; |
|
623 | + } |
|
624 | + |
|
625 | + |
|
626 | + /** |
|
627 | + * This method returns a php DateTime object for setting on the EE_Base_Class model. |
|
628 | + * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
|
629 | + * with. |
|
630 | + * |
|
631 | + * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
|
632 | + * in the format that is set on the date_field (or DateTime |
|
633 | + * object)! |
|
634 | + * @return DateTime |
|
635 | + */ |
|
636 | + protected function _get_date_object($date_string) |
|
637 | + { |
|
638 | + //first if this is an empty date_string and nullable is allowed, just return null. |
|
639 | + if ($this->_nullable && empty($date_string)) { |
|
640 | + return null; |
|
641 | + } |
|
642 | + |
|
643 | + // if incoming date |
|
644 | + if ($date_string instanceof DateTime) { |
|
645 | + EEH_DTT_Helper::setTimezone($date_string, $this->_DateTimeZone); |
|
646 | + return $date_string; |
|
647 | + } |
|
648 | + // if empty date_string and made it here. |
|
649 | + // Return a datetime object for now in the given timezone. |
|
650 | + if (empty($date_string)) { |
|
651 | + return new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
652 | + } |
|
653 | + // if $date_string is matches something that looks like a Unix timestamp let's just use it. |
|
654 | + if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $date_string)) { |
|
655 | + try { |
|
656 | + // This is operating under the assumption that the incoming Unix timestamp |
|
657 | + // is an ACTUAL Unix timestamp and not the calculated one output by current_time('timestamp'); |
|
658 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
659 | + $DateTime->setTimestamp($date_string); |
|
660 | + |
|
661 | + return $DateTime; |
|
662 | + } catch (Exception $e) { |
|
663 | + // should be rare, but if things got fooled then let's just continue |
|
664 | + } |
|
665 | + } |
|
666 | + //not a unix timestamp. So we will use the set format on this object and set timezone to |
|
667 | + //create the DateTime object. |
|
668 | + $format = $this->_date_format . ' ' . $this->_time_format; |
|
669 | + try { |
|
670 | + $DateTime = DateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
|
671 | + if ($DateTime instanceof DateTime) { |
|
672 | + $DateTime = new DbSafeDateTime( |
|
673 | + $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
674 | + $this->_DateTimeZone |
|
675 | + ); |
|
676 | + } |
|
677 | + if (! $DateTime instanceof DbSafeDateTime) { |
|
678 | + throw new EE_Error( |
|
679 | + sprintf( |
|
680 | + __('"%1$s" does not represent a valid Date Time in the format "%2$s".', 'event_espresso'), |
|
681 | + $date_string, |
|
682 | + $format |
|
683 | + ) |
|
684 | + ); |
|
685 | + } |
|
686 | + } catch (Exception $e) { |
|
687 | + // if we made it here then likely then something went really wrong. |
|
688 | + // Instead of throwing an exception, let's just return a DateTime object for now, in the set timezone. |
|
689 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
690 | + } |
|
691 | + |
|
692 | + return $DateTime; |
|
693 | + } |
|
694 | + |
|
695 | + |
|
696 | + |
|
697 | + /** |
|
698 | + * get_timezone_transitions |
|
699 | + * |
|
700 | + * @param \DateTimeZone $DateTimeZone |
|
701 | + * @param int $time |
|
702 | + * @param bool $first_only |
|
703 | + * @return mixed |
|
704 | + */ |
|
705 | + public function get_timezone_transitions(DateTimeZone $DateTimeZone, $time = null, $first_only = true) |
|
706 | + { |
|
707 | + return EEH_DTT_Helper::get_timezone_transitions($DateTimeZone, $time, $first_only); |
|
708 | + } |
|
709 | + |
|
710 | + |
|
711 | + |
|
712 | + /** |
|
713 | + * get_timezone_offset |
|
714 | + * |
|
715 | + * @param \DateTimeZone $DateTimeZone |
|
716 | + * @param int $time |
|
717 | + * @return mixed |
|
718 | + * @throws \DomainException |
|
719 | + */ |
|
720 | + public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
|
721 | + { |
|
722 | + return EEH_DTT_Helper::get_timezone_offset($DateTimeZone, $time); |
|
723 | + } |
|
724 | + |
|
725 | + |
|
726 | + /** |
|
727 | + * This will take an incoming timezone string and return the abbreviation for that timezone |
|
728 | + * |
|
729 | + * @param string $timezone_string |
|
730 | + * @return string abbreviation |
|
731 | + * @throws \EE_Error |
|
732 | + */ |
|
733 | + public function get_timezone_abbrev($timezone_string) |
|
734 | + { |
|
735 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
736 | + $dateTime = new DateTime(\EE_Datetime_Field::now, new DateTimeZone($timezone_string)); |
|
737 | + |
|
738 | + return $dateTime->format('T'); |
|
739 | + } |
|
740 | + |
|
741 | + /** |
|
742 | + * Overrides the parent to allow for having a dynamic "now" value |
|
743 | + * |
|
744 | + * @return mixed |
|
745 | + */ |
|
746 | + public function get_default_value() |
|
747 | + { |
|
748 | + if ($this->_default_value === EE_Datetime_Field::now) { |
|
749 | + return time(); |
|
750 | + } else { |
|
751 | + return parent::get_default_value(); |
|
752 | + } |
|
753 | + } |
|
754 | + |
|
755 | + |
|
756 | + public function getSchemaDescription() |
|
757 | + { |
|
758 | + return sprintf( |
|
759 | + esc_html__('%s - the value for this field is in the timezone of the site.', 'event_espresso'), |
|
760 | + $this->get_nicename() |
|
761 | + ); |
|
762 | + } |
|
763 | 763 | } |
@@ -214,8 +214,8 @@ discard block |
||
214 | 214 | |
215 | 215 | default : |
216 | 216 | return $pretty |
217 | - ? $this->_pretty_date_format . ' ' . $this->_pretty_time_format |
|
218 | - : $this->_date_format . ' ' . $this->_time_format; |
|
217 | + ? $this->_pretty_date_format.' '.$this->_pretty_time_format |
|
218 | + : $this->_date_format.' '.$this->_time_format; |
|
219 | 219 | } |
220 | 220 | } |
221 | 221 | |
@@ -469,7 +469,7 @@ discard block |
||
469 | 469 | */ |
470 | 470 | protected function _prepare_for_display($DateTime, $schema = false) |
471 | 471 | { |
472 | - if (! $DateTime instanceof DateTime) { |
|
472 | + if ( ! $DateTime instanceof DateTime) { |
|
473 | 473 | if ($this->_nullable) { |
474 | 474 | return ''; |
475 | 475 | } else { |
@@ -503,15 +503,15 @@ discard block |
||
503 | 503 | if ($this->_display_timezone()) { |
504 | 504 | //must be explicit because schema could equal true. |
505 | 505 | if ($schema === 'no_html') { |
506 | - $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
506 | + $timezone_string = ' ('.$DateTime->format('T').')'; |
|
507 | 507 | } else { |
508 | - $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
508 | + $timezone_string = ' <span class="ee_dtt_timezone_string">('.$DateTime->format('T').')</span>'; |
|
509 | 509 | } |
510 | 510 | } else { |
511 | 511 | $timezone_string = ''; |
512 | 512 | } |
513 | 513 | |
514 | - return $DateTime->format($format_string) . $timezone_string; |
|
514 | + return $DateTime->format($format_string).$timezone_string; |
|
515 | 515 | } |
516 | 516 | return $DateTime->format($format_string); |
517 | 517 | } |
@@ -528,7 +528,7 @@ discard block |
||
528 | 528 | public function prepare_for_use_in_db($datetime_value) |
529 | 529 | { |
530 | 530 | //we allow an empty value or DateTime object, but nothing else. |
531 | - if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
531 | + if ( ! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
532 | 532 | throw new EE_Error( |
533 | 533 | sprintf( |
534 | 534 | __( |
@@ -543,7 +543,7 @@ discard block |
||
543 | 543 | } |
544 | 544 | |
545 | 545 | if ($datetime_value instanceof DateTime) { |
546 | - if (! $datetime_value instanceof DbSafeDateTime) { |
|
546 | + if ( ! $datetime_value instanceof DbSafeDateTime) { |
|
547 | 547 | $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
548 | 548 | } |
549 | 549 | EEH_DTT_Helper::setTimezone($datetime_value, $this->get_UTC_DateTimeZone()); |
@@ -588,7 +588,7 @@ discard block |
||
588 | 588 | } |
589 | 589 | } |
590 | 590 | |
591 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
591 | + if ( ! $DateTime instanceof DbSafeDateTime) { |
|
592 | 592 | // if still no datetime object, then let's just use now |
593 | 593 | $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
594 | 594 | } |
@@ -665,7 +665,7 @@ discard block |
||
665 | 665 | } |
666 | 666 | //not a unix timestamp. So we will use the set format on this object and set timezone to |
667 | 667 | //create the DateTime object. |
668 | - $format = $this->_date_format . ' ' . $this->_time_format; |
|
668 | + $format = $this->_date_format.' '.$this->_time_format; |
|
669 | 669 | try { |
670 | 670 | $DateTime = DateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
671 | 671 | if ($DateTime instanceof DateTime) { |
@@ -674,7 +674,7 @@ discard block |
||
674 | 674 | $this->_DateTimeZone |
675 | 675 | ); |
676 | 676 | } |
677 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
677 | + if ( ! $DateTime instanceof DbSafeDateTime) { |
|
678 | 678 | throw new EE_Error( |
679 | 679 | sprintf( |
680 | 680 | __('"%1$s" does not represent a valid Date Time in the format "%2$s".', 'event_espresso'), |