@@ -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 | |
@@ -87,19 +87,19 @@ discard block |
||
87 | 87 | $this->get_this_model()->get_this_model_name(), |
88 | 88 | 'ID' |
89 | 89 | ); |
90 | - $field_with_model_name = $this->get_join_table_fk_field_to( |
|
90 | + $field_with_model_name = $this->get_join_table_fk_field_to( |
|
91 | 91 | $this->get_this_model()->get_this_model_name(), |
92 | 92 | 'name' |
93 | 93 | ); |
94 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
94 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
95 | 95 | $model_relation_chain, |
96 | 96 | $this->get_this_model()->get_this_model_name() |
97 | - ) . $this_table_pk_field->get_table_alias(); |
|
98 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
97 | + ).$this_table_pk_field->get_table_alias(); |
|
98 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
99 | 99 | $model_relation_chain, |
100 | 100 | $this->get_join_model()->get_this_model_name() |
101 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
102 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
101 | + ).$join_table_fk_field_to_this_table->get_table_alias(); |
|
102 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
103 | 103 | // phew! ok, we have all the info we need, now we can create the SQL join string |
104 | 104 | $SQL = $this->_left_join( |
105 | 105 | $join_table, |
@@ -107,8 +107,8 @@ discard block |
||
107 | 107 | $join_table_fk_field_to_this_table->get_table_column(), |
108 | 108 | $this_table_alias, |
109 | 109 | $this_table_pk_field->get_table_column(), |
110 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
111 | - ) . |
|
110 | + $field_with_model_name->get_qualified_column()."='".$this->get_this_model()->get_this_model_name()."'" |
|
111 | + ). |
|
112 | 112 | $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
113 | 113 | |
114 | 114 | return $SQL; |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | 'event_espresso' |
135 | 135 | ))); |
136 | 136 | } |
137 | - $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
137 | + $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
138 | 138 | $this->get_this_model()->get_this_model_name(), |
139 | 139 | 'ID' |
140 | 140 | ); |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | $this->get_other_model()->get_this_model_name(), |
143 | 143 | 'ID' |
144 | 144 | ); |
145 | - $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
145 | + $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
146 | 146 | $this->get_other_model()->get_this_model_name(), |
147 | 147 | 'name' |
148 | 148 | ); |
@@ -150,14 +150,14 @@ discard block |
||
150 | 150 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
151 | 151 | $this->_model_relation_chain_to_join_model, |
152 | 152 | $this->get_join_model()->get_this_model_name() |
153 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
153 | + ).$join_table_fk_field_to_this_table->get_table_alias(); |
|
154 | 154 | |
155 | 155 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
156 | 156 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
157 | 157 | $model_relation_chain, |
158 | 158 | $this->get_other_model()->get_this_model_name() |
159 | - ) . $other_table_pk_field->get_table_alias(); |
|
160 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
159 | + ).$other_table_pk_field->get_table_alias(); |
|
160 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
161 | 161 | |
162 | 162 | $SQL = $this->_left_join( |
163 | 163 | $other_table, |
@@ -165,8 +165,8 @@ discard block |
||
165 | 165 | $other_table_pk_field->get_table_column(), |
166 | 166 | $join_table_alias, |
167 | 167 | $join_table_fk_field_to_other_table->get_table_column(), |
168 | - $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
169 | - ) . |
|
168 | + $field_with_other_model_name->get_qualified_column()."='".$this->get_other_model()->get_this_model_name()."'" |
|
169 | + ). |
|
170 | 170 | $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
171 | 171 | return $SQL; |
172 | 172 | } |
@@ -188,15 +188,15 @@ discard block |
||
188 | 188 | $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
189 | 189 | $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
190 | 190 | // check if such a relationship already exists |
191 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
191 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
192 | 192 | $this->get_this_model()->get_this_model_name(), |
193 | 193 | 'ID' |
194 | 194 | ); |
195 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
195 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
196 | 196 | $this->get_this_model()->get_this_model_name(), |
197 | 197 | 'name' |
198 | 198 | ); |
199 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
199 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
200 | 200 | $this->get_other_model()->get_this_model_name(), |
201 | 201 | 'ID' |
202 | 202 | ); |
@@ -213,17 +213,17 @@ discard block |
||
213 | 213 | ); |
214 | 214 | |
215 | 215 | // if $where_query exists lets add them to the query_params. |
216 | - if (! empty($extra_join_model_fields_n_values)) { |
|
216 | + if ( ! empty($extra_join_model_fields_n_values)) { |
|
217 | 217 | // 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) |
218 | 218 | // make sure we strip THIS models name from the query param |
219 | 219 | $parsed_query = array(); |
220 | 220 | foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
221 | - $query_param = str_replace( |
|
222 | - $this->get_join_model()->get_this_model_name() . ".", |
|
221 | + $query_param = str_replace( |
|
222 | + $this->get_join_model()->get_this_model_name().".", |
|
223 | 223 | "", |
224 | 224 | $query_param |
225 | 225 | ); |
226 | - $parsed_query[ $query_param ] = $val; |
|
226 | + $parsed_query[$query_param] = $val; |
|
227 | 227 | } |
228 | 228 | $cols_n_values = array_merge($cols_n_values, $parsed_query); |
229 | 229 | } |
@@ -235,7 +235,7 @@ discard block |
||
235 | 235 | // if there is already an entry in the join table, indicating a relationship, we're done |
236 | 236 | // again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
237 | 237 | // the other tables, use the joining model directly! |
238 | - if (! $existing_entry_in_join_table) { |
|
238 | + if ( ! $existing_entry_in_join_table) { |
|
239 | 239 | $this->get_join_model()->insert($cols_n_values); |
240 | 240 | } |
241 | 241 | return $other_model_obj; |
@@ -257,15 +257,15 @@ discard block |
||
257 | 257 | $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
258 | 258 | $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
259 | 259 | // check if such a relationship already exists |
260 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
260 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
261 | 261 | $this->get_this_model()->get_this_model_name(), |
262 | 262 | 'ID' |
263 | 263 | ); |
264 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
264 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
265 | 265 | $this->get_this_model()->get_this_model_name(), |
266 | 266 | 'name' |
267 | 267 | ); |
268 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
268 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
269 | 269 | $this->get_other_model()->get_this_model_name(), |
270 | 270 | 'ID' |
271 | 271 | ); |
@@ -282,17 +282,17 @@ discard block |
||
282 | 282 | ); |
283 | 283 | |
284 | 284 | // if $where_query exists lets add them to the query_params. |
285 | - if (! empty($where_query)) { |
|
285 | + if ( ! empty($where_query)) { |
|
286 | 286 | // 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) |
287 | 287 | // make sure we strip THIS models name from the query param |
288 | 288 | $parsed_query = array(); |
289 | 289 | foreach ($where_query as $query_param => $val) { |
290 | - $query_param = str_replace( |
|
291 | - $this->get_join_model()->get_this_model_name() . ".", |
|
290 | + $query_param = str_replace( |
|
291 | + $this->get_join_model()->get_this_model_name().".", |
|
292 | 292 | "", |
293 | 293 | $query_param |
294 | 294 | ); |
295 | - $parsed_query[ $query_param ] = $val; |
|
295 | + $parsed_query[$query_param] = $val; |
|
296 | 296 | } |
297 | 297 | $cols_n_values = array_merge($cols_n_values, $parsed_query); |
298 | 298 | } |
@@ -10,294 +10,294 @@ |
||
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 | - ); |
|
90 | - $field_with_model_name = $this->get_join_table_fk_field_to( |
|
91 | - $this->get_this_model()->get_this_model_name(), |
|
92 | - 'name' |
|
93 | - ); |
|
94 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
95 | - $model_relation_chain, |
|
96 | - $this->get_this_model()->get_this_model_name() |
|
97 | - ) . $this_table_pk_field->get_table_alias(); |
|
98 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
99 | - $model_relation_chain, |
|
100 | - $this->get_join_model()->get_this_model_name() |
|
101 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
102 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
103 | - // phew! ok, we have all the info we need, now we can create the SQL join string |
|
104 | - $SQL = $this->_left_join( |
|
105 | - $join_table, |
|
106 | - $join_table_alias, |
|
107 | - $join_table_fk_field_to_this_table->get_table_column(), |
|
108 | - $this_table_alias, |
|
109 | - $this_table_pk_field->get_table_column(), |
|
110 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
111 | - ) . |
|
112 | - $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 | + ); |
|
90 | + $field_with_model_name = $this->get_join_table_fk_field_to( |
|
91 | + $this->get_this_model()->get_this_model_name(), |
|
92 | + 'name' |
|
93 | + ); |
|
94 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
95 | + $model_relation_chain, |
|
96 | + $this->get_this_model()->get_this_model_name() |
|
97 | + ) . $this_table_pk_field->get_table_alias(); |
|
98 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
99 | + $model_relation_chain, |
|
100 | + $this->get_join_model()->get_this_model_name() |
|
101 | + ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
102 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
103 | + // phew! ok, we have all the info we need, now we can create the SQL join string |
|
104 | + $SQL = $this->_left_join( |
|
105 | + $join_table, |
|
106 | + $join_table_alias, |
|
107 | + $join_table_fk_field_to_this_table->get_table_column(), |
|
108 | + $this_table_alias, |
|
109 | + $this_table_pk_field->get_table_column(), |
|
110 | + $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
111 | + ) . |
|
112 | + $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
113 | 113 | |
114 | - return $SQL; |
|
115 | - } |
|
114 | + return $SQL; |
|
115 | + } |
|
116 | 116 | |
117 | 117 | |
118 | - /** |
|
119 | - * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
120 | - * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
121 | - * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
122 | - * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
123 | - * the intermediate join table, and then to the other model's pk's table |
|
124 | - * |
|
125 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
126 | - * @return string of SQL |
|
127 | - * @throws \EE_Error |
|
128 | - */ |
|
129 | - public function get_join_statement($model_relation_chain) |
|
130 | - { |
|
131 | - if ($this->_model_relation_chain_to_join_model === null) { |
|
132 | - throw new EE_Error(sprintf(esc_html__( |
|
133 | - 'When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
134 | - 'event_espresso' |
|
135 | - ))); |
|
136 | - } |
|
137 | - $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
138 | - $this->get_this_model()->get_this_model_name(), |
|
139 | - 'ID' |
|
140 | - ); |
|
141 | - $join_table_fk_field_to_other_table = $this->get_join_table_fk_field_to( |
|
142 | - $this->get_other_model()->get_this_model_name(), |
|
143 | - 'ID' |
|
144 | - ); |
|
145 | - $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
146 | - $this->get_other_model()->get_this_model_name(), |
|
147 | - 'name' |
|
148 | - ); |
|
118 | + /** |
|
119 | + * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
120 | + * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
121 | + * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
122 | + * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
123 | + * the intermediate join table, and then to the other model's pk's table |
|
124 | + * |
|
125 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
126 | + * @return string of SQL |
|
127 | + * @throws \EE_Error |
|
128 | + */ |
|
129 | + public function get_join_statement($model_relation_chain) |
|
130 | + { |
|
131 | + if ($this->_model_relation_chain_to_join_model === null) { |
|
132 | + throw new EE_Error(sprintf(esc_html__( |
|
133 | + 'When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
134 | + 'event_espresso' |
|
135 | + ))); |
|
136 | + } |
|
137 | + $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
138 | + $this->get_this_model()->get_this_model_name(), |
|
139 | + 'ID' |
|
140 | + ); |
|
141 | + $join_table_fk_field_to_other_table = $this->get_join_table_fk_field_to( |
|
142 | + $this->get_other_model()->get_this_model_name(), |
|
143 | + 'ID' |
|
144 | + ); |
|
145 | + $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
146 | + $this->get_other_model()->get_this_model_name(), |
|
147 | + 'name' |
|
148 | + ); |
|
149 | 149 | |
150 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
151 | - $this->_model_relation_chain_to_join_model, |
|
152 | - $this->get_join_model()->get_this_model_name() |
|
153 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
150 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
151 | + $this->_model_relation_chain_to_join_model, |
|
152 | + $this->get_join_model()->get_this_model_name() |
|
153 | + ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
154 | 154 | |
155 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
156 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
157 | - $model_relation_chain, |
|
158 | - $this->get_other_model()->get_this_model_name() |
|
159 | - ) . $other_table_pk_field->get_table_alias(); |
|
160 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
155 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
156 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
157 | + $model_relation_chain, |
|
158 | + $this->get_other_model()->get_this_model_name() |
|
159 | + ) . $other_table_pk_field->get_table_alias(); |
|
160 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
161 | 161 | |
162 | - $SQL = $this->_left_join( |
|
163 | - $other_table, |
|
164 | - $other_table_alias, |
|
165 | - $other_table_pk_field->get_table_column(), |
|
166 | - $join_table_alias, |
|
167 | - $join_table_fk_field_to_other_table->get_table_column(), |
|
168 | - $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
169 | - ) . |
|
170 | - $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
171 | - return $SQL; |
|
172 | - } |
|
162 | + $SQL = $this->_left_join( |
|
163 | + $other_table, |
|
164 | + $other_table_alias, |
|
165 | + $other_table_pk_field->get_table_column(), |
|
166 | + $join_table_alias, |
|
167 | + $join_table_fk_field_to_other_table->get_table_column(), |
|
168 | + $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
169 | + ) . |
|
170 | + $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
171 | + return $SQL; |
|
172 | + } |
|
173 | 173 | |
174 | 174 | |
175 | - /** |
|
176 | - * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
177 | - * |
|
178 | - * @param EE_Base_Class|int $this_obj_or_id |
|
179 | - * @param EE_Base_Class|int $other_obj_or_id |
|
180 | - * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
181 | - * checking existing values and for setting new rows if |
|
182 | - * no exact matches. |
|
183 | - * @return EE_Base_Class |
|
184 | - * @throws \EE_Error |
|
185 | - */ |
|
186 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
187 | - { |
|
188 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
189 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
190 | - // check if such a relationship already exists |
|
191 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
192 | - $this->get_this_model()->get_this_model_name(), |
|
193 | - 'ID' |
|
194 | - ); |
|
195 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
196 | - $this->get_this_model()->get_this_model_name(), |
|
197 | - 'name' |
|
198 | - ); |
|
199 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
200 | - $this->get_other_model()->get_this_model_name(), |
|
201 | - 'ID' |
|
202 | - ); |
|
203 | - $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
204 | - $this->get_other_model()->get_this_model_name(), |
|
205 | - 'name' |
|
206 | - ); |
|
175 | + /** |
|
176 | + * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
177 | + * |
|
178 | + * @param EE_Base_Class|int $this_obj_or_id |
|
179 | + * @param EE_Base_Class|int $other_obj_or_id |
|
180 | + * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
181 | + * checking existing values and for setting new rows if |
|
182 | + * no exact matches. |
|
183 | + * @return EE_Base_Class |
|
184 | + * @throws \EE_Error |
|
185 | + */ |
|
186 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
187 | + { |
|
188 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
189 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
190 | + // check if such a relationship already exists |
|
191 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
192 | + $this->get_this_model()->get_this_model_name(), |
|
193 | + 'ID' |
|
194 | + ); |
|
195 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
196 | + $this->get_this_model()->get_this_model_name(), |
|
197 | + 'name' |
|
198 | + ); |
|
199 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
200 | + $this->get_other_model()->get_this_model_name(), |
|
201 | + 'ID' |
|
202 | + ); |
|
203 | + $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
204 | + $this->get_other_model()->get_this_model_name(), |
|
205 | + 'name' |
|
206 | + ); |
|
207 | 207 | |
208 | - $cols_n_values = array( |
|
209 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
210 | - $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
211 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
212 | - $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
213 | - ); |
|
208 | + $cols_n_values = array( |
|
209 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
210 | + $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
211 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
212 | + $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
213 | + ); |
|
214 | 214 | |
215 | - // if $where_query exists lets add them to the query_params. |
|
216 | - if (! empty($extra_join_model_fields_n_values)) { |
|
217 | - // 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) |
|
218 | - // make sure we strip THIS models name from the query param |
|
219 | - $parsed_query = array(); |
|
220 | - foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
221 | - $query_param = str_replace( |
|
222 | - $this->get_join_model()->get_this_model_name() . ".", |
|
223 | - "", |
|
224 | - $query_param |
|
225 | - ); |
|
226 | - $parsed_query[ $query_param ] = $val; |
|
227 | - } |
|
228 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
229 | - } |
|
215 | + // if $where_query exists lets add them to the query_params. |
|
216 | + if (! empty($extra_join_model_fields_n_values)) { |
|
217 | + // 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) |
|
218 | + // make sure we strip THIS models name from the query param |
|
219 | + $parsed_query = array(); |
|
220 | + foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
221 | + $query_param = str_replace( |
|
222 | + $this->get_join_model()->get_this_model_name() . ".", |
|
223 | + "", |
|
224 | + $query_param |
|
225 | + ); |
|
226 | + $parsed_query[ $query_param ] = $val; |
|
227 | + } |
|
228 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
229 | + } |
|
230 | 230 | |
231 | - $query_params = array($cols_n_values); |
|
231 | + $query_params = array($cols_n_values); |
|
232 | 232 | |
233 | 233 | |
234 | - $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
235 | - // if there is already an entry in the join table, indicating a relationship, we're done |
|
236 | - // again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
237 | - // the other tables, use the joining model directly! |
|
238 | - if (! $existing_entry_in_join_table) { |
|
239 | - $this->get_join_model()->insert($cols_n_values); |
|
240 | - } |
|
241 | - return $other_model_obj; |
|
242 | - } |
|
234 | + $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
235 | + // if there is already an entry in the join table, indicating a relationship, we're done |
|
236 | + // again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
237 | + // the other tables, use the joining model directly! |
|
238 | + if (! $existing_entry_in_join_table) { |
|
239 | + $this->get_join_model()->insert($cols_n_values); |
|
240 | + } |
|
241 | + return $other_model_obj; |
|
242 | + } |
|
243 | 243 | |
244 | 244 | |
245 | - /** |
|
246 | - * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
247 | - * |
|
248 | - * @param EE_Base_Class|int $this_obj_or_id |
|
249 | - * @param EE_Base_Class|int $other_obj_or_id |
|
250 | - * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
251 | - * values and for removing existing rows if exact matches exist. |
|
252 | - * @return EE_Base_Class |
|
253 | - * @throws \EE_Error |
|
254 | - */ |
|
255 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
256 | - { |
|
257 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
258 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
259 | - // check if such a relationship already exists |
|
260 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
261 | - $this->get_this_model()->get_this_model_name(), |
|
262 | - 'ID' |
|
263 | - ); |
|
264 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
265 | - $this->get_this_model()->get_this_model_name(), |
|
266 | - 'name' |
|
267 | - ); |
|
268 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
269 | - $this->get_other_model()->get_this_model_name(), |
|
270 | - 'ID' |
|
271 | - ); |
|
272 | - $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
273 | - $this->get_other_model()->get_this_model_name(), |
|
274 | - 'name' |
|
275 | - ); |
|
245 | + /** |
|
246 | + * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
247 | + * |
|
248 | + * @param EE_Base_Class|int $this_obj_or_id |
|
249 | + * @param EE_Base_Class|int $other_obj_or_id |
|
250 | + * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
251 | + * values and for removing existing rows if exact matches exist. |
|
252 | + * @return EE_Base_Class |
|
253 | + * @throws \EE_Error |
|
254 | + */ |
|
255 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
256 | + { |
|
257 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
258 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
259 | + // check if such a relationship already exists |
|
260 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
261 | + $this->get_this_model()->get_this_model_name(), |
|
262 | + 'ID' |
|
263 | + ); |
|
264 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
265 | + $this->get_this_model()->get_this_model_name(), |
|
266 | + 'name' |
|
267 | + ); |
|
268 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
269 | + $this->get_other_model()->get_this_model_name(), |
|
270 | + 'ID' |
|
271 | + ); |
|
272 | + $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
273 | + $this->get_other_model()->get_this_model_name(), |
|
274 | + 'name' |
|
275 | + ); |
|
276 | 276 | |
277 | - $cols_n_values = array( |
|
278 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
279 | - $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
280 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
281 | - $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
282 | - ); |
|
277 | + $cols_n_values = array( |
|
278 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
279 | + $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
280 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
281 | + $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
282 | + ); |
|
283 | 283 | |
284 | - // if $where_query exists lets add them to the query_params. |
|
285 | - if (! empty($where_query)) { |
|
286 | - // 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) |
|
287 | - // make sure we strip THIS models name from the query param |
|
288 | - $parsed_query = array(); |
|
289 | - foreach ($where_query as $query_param => $val) { |
|
290 | - $query_param = str_replace( |
|
291 | - $this->get_join_model()->get_this_model_name() . ".", |
|
292 | - "", |
|
293 | - $query_param |
|
294 | - ); |
|
295 | - $parsed_query[ $query_param ] = $val; |
|
296 | - } |
|
297 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
298 | - } |
|
284 | + // if $where_query exists lets add them to the query_params. |
|
285 | + if (! empty($where_query)) { |
|
286 | + // 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) |
|
287 | + // make sure we strip THIS models name from the query param |
|
288 | + $parsed_query = array(); |
|
289 | + foreach ($where_query as $query_param => $val) { |
|
290 | + $query_param = str_replace( |
|
291 | + $this->get_join_model()->get_this_model_name() . ".", |
|
292 | + "", |
|
293 | + $query_param |
|
294 | + ); |
|
295 | + $parsed_query[ $query_param ] = $val; |
|
296 | + } |
|
297 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
298 | + } |
|
299 | 299 | |
300 | - $this->get_join_model()->delete(array($cols_n_values)); |
|
301 | - return $other_model_obj; |
|
302 | - } |
|
300 | + $this->get_join_model()->delete(array($cols_n_values)); |
|
301 | + return $other_model_obj; |
|
302 | + } |
|
303 | 303 | } |
@@ -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,9 +206,9 @@ 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(); |
|
211 | - $query_param[0][ $query_param_where_this_model_pk ] = array('IN', $parent_ids); |
|
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 | 212 | $parents = $this->get_other_model()->get_all($query_params); |
213 | 213 | } |
214 | 214 | |
@@ -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 | // 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. |
288 | 288 | } else { |
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()); |
@@ -12,303 +12,303 @@ |
||
12 | 12 | { |
13 | 13 | |
14 | 14 | |
15 | - /** |
|
16 | - * The Foreign key on the model that acts as the PRIMARY KEY used in special autosave handling where we query for |
|
17 | - * autosaves (or the Foreign key on other models in relations pointing to this models primary key which is this |
|
18 | - * value). The _primary_cpt_field is what is equivalent to the post_id field on a cpt join. |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - private $_primary_cpt_field; |
|
23 | - |
|
24 | - |
|
25 | - /** |
|
26 | - * This is what field serves as the "parent" column that is linked with whatever the main model's calling this |
|
27 | - * relation has as a primary key. In other words EEM_Event has 'Datetime' => new |
|
28 | - * EE_Has_Many_Revision_Relation('EVT_ID', 'DTT_parent'). That means that in the EEM_Datetime model the |
|
29 | - * 'DTT_Parent' field is related to the 'DTT_ID' primary key field (in the same model) because 'DTT_ID' is the |
|
30 | - * primary key in the other model (EEM_Datetime). |
|
31 | - * |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - private $_parent_pk_relation_field; |
|
35 | - |
|
36 | - |
|
37 | - /** |
|
38 | - * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the |
|
39 | - * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but |
|
40 | - * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get |
|
41 | - * related models across the relation, and add-and-remove the relationships. |
|
42 | - * |
|
43 | - * @param string $primary_cpt_field See property description for details |
|
44 | - * @param string $parent_pk_relation_field This is the field that is "connected" to the $primary_cpt_field. |
|
45 | - * See property desc for details. |
|
46 | - * @param boolean $block_deletes For this type of relation, we block by default. If there are |
|
47 | - * related models across this relation, block (prevent and add an |
|
48 | - * error) the deletion of this model |
|
49 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
50 | - * default |
|
51 | - */ |
|
52 | - public function __construct( |
|
53 | - $primary_cpt_field, |
|
54 | - $parent_pk_relation_field, |
|
55 | - $block_deletes = true, |
|
56 | - $blocking_delete_error_message = null |
|
57 | - ) { |
|
58 | - $this->_primary_cpt_field = $primary_cpt_field; |
|
59 | - $this->_parent_pk_relation_field = $parent_pk_relation_field; |
|
60 | - parent::__construct($block_deletes, $blocking_delete_error_message); |
|
61 | - } |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
66 | - * you like. |
|
67 | - * |
|
68 | - * @param EE_Base_Class|int $this_obj_or_id |
|
69 | - * @param EE_Base_Class|int $other_obj_or_id |
|
70 | - * @param array $extra_join_model_fields_n_values |
|
71 | - * @return \EE_Base_Class |
|
72 | - * @throws \EE_Error |
|
73 | - */ |
|
74 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
75 | - { |
|
76 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
77 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
78 | - |
|
79 | - // handle possible revisions |
|
80 | - $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj); |
|
81 | - |
|
82 | - // if is array, then we've already done the add_relation so let's get out |
|
83 | - if (is_array($other_model_obj)) { |
|
84 | - return $other_model_obj[0]; |
|
85 | - } |
|
86 | - // find the field on the other model which is a foreign key to this model |
|
87 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
88 | - // set that field on the other model to this model's ID |
|
89 | - $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
90 | - $other_model_obj->save(); |
|
91 | - return $other_model_obj; |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * Sets the other model object's foreign key to its default, instead of pointing to this model object |
|
97 | - * |
|
98 | - * @param EE_Base_Class|int $this_obj_or_id |
|
99 | - * @param EE_Base_Class|int $other_obj_or_id |
|
100 | - * @param array $where_query |
|
101 | - * @return \EE_Base_Class |
|
102 | - * @throws \EE_Error |
|
103 | - */ |
|
104 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
105 | - { |
|
106 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id); |
|
107 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
108 | - // handle possible revisions |
|
109 | - $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj, true); |
|
110 | - |
|
111 | - |
|
112 | - // if is array, then we've already done the add_relation so let's get out |
|
113 | - if (is_array($other_model_obj)) { |
|
114 | - return $other_model_obj[0]; |
|
115 | - } |
|
116 | - |
|
117 | - // find the field on the other model which is a foreign key to this model |
|
118 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
119 | - |
|
120 | - |
|
121 | - // set that field on the other model to this model's ID |
|
122 | - if ($this->_blocking_delete) { |
|
123 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
124 | - $other_model_obj->save(); |
|
125 | - } else { |
|
126 | - $other_model_obj->delete(); |
|
127 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
128 | - return $other_model_obj; |
|
129 | - } |
|
130 | - return $other_model_obj; |
|
131 | - } |
|
132 | - |
|
133 | - |
|
134 | - /** |
|
135 | - * This is identical to EE_Model_Relation->get_all_related() except we're going handle special autosave conditions |
|
136 | - * in here. |
|
137 | - * |
|
138 | - * @param EE_Base_Class|int $model_object_or_id |
|
139 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
140 | - * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
141 | - * @return EE_Base_Class[] |
|
142 | - * @throws \EE_Error |
|
143 | - */ |
|
144 | - public function get_all_related( |
|
145 | - $model_object_or_id, |
|
146 | - $query_params = array(), |
|
147 | - $values_already_prepared_by_model_object = false |
|
148 | - ) { |
|
149 | - if ($values_already_prepared_by_model_object !== false) { |
|
150 | - EE_Error::doing_it_wrong( |
|
151 | - 'EE_Model_Relation_Base::get_all_related', |
|
152 | - esc_html__('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
153 | - '4.8.1' |
|
154 | - ); |
|
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); |
|
15 | + /** |
|
16 | + * The Foreign key on the model that acts as the PRIMARY KEY used in special autosave handling where we query for |
|
17 | + * autosaves (or the Foreign key on other models in relations pointing to this models primary key which is this |
|
18 | + * value). The _primary_cpt_field is what is equivalent to the post_id field on a cpt join. |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + private $_primary_cpt_field; |
|
23 | + |
|
24 | + |
|
25 | + /** |
|
26 | + * This is what field serves as the "parent" column that is linked with whatever the main model's calling this |
|
27 | + * relation has as a primary key. In other words EEM_Event has 'Datetime' => new |
|
28 | + * EE_Has_Many_Revision_Relation('EVT_ID', 'DTT_parent'). That means that in the EEM_Datetime model the |
|
29 | + * 'DTT_Parent' field is related to the 'DTT_ID' primary key field (in the same model) because 'DTT_ID' is the |
|
30 | + * primary key in the other model (EEM_Datetime). |
|
31 | + * |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + private $_parent_pk_relation_field; |
|
35 | + |
|
36 | + |
|
37 | + /** |
|
38 | + * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the |
|
39 | + * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but |
|
40 | + * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get |
|
41 | + * related models across the relation, and add-and-remove the relationships. |
|
42 | + * |
|
43 | + * @param string $primary_cpt_field See property description for details |
|
44 | + * @param string $parent_pk_relation_field This is the field that is "connected" to the $primary_cpt_field. |
|
45 | + * See property desc for details. |
|
46 | + * @param boolean $block_deletes For this type of relation, we block by default. If there are |
|
47 | + * related models across this relation, block (prevent and add an |
|
48 | + * error) the deletion of this model |
|
49 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
50 | + * default |
|
51 | + */ |
|
52 | + public function __construct( |
|
53 | + $primary_cpt_field, |
|
54 | + $parent_pk_relation_field, |
|
55 | + $block_deletes = true, |
|
56 | + $blocking_delete_error_message = null |
|
57 | + ) { |
|
58 | + $this->_primary_cpt_field = $primary_cpt_field; |
|
59 | + $this->_parent_pk_relation_field = $parent_pk_relation_field; |
|
60 | + parent::__construct($block_deletes, $blocking_delete_error_message); |
|
61 | + } |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
66 | + * you like. |
|
67 | + * |
|
68 | + * @param EE_Base_Class|int $this_obj_or_id |
|
69 | + * @param EE_Base_Class|int $other_obj_or_id |
|
70 | + * @param array $extra_join_model_fields_n_values |
|
71 | + * @return \EE_Base_Class |
|
72 | + * @throws \EE_Error |
|
73 | + */ |
|
74 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
75 | + { |
|
76 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
77 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
78 | + |
|
79 | + // handle possible revisions |
|
80 | + $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj); |
|
81 | + |
|
82 | + // if is array, then we've already done the add_relation so let's get out |
|
83 | + if (is_array($other_model_obj)) { |
|
84 | + return $other_model_obj[0]; |
|
85 | + } |
|
86 | + // find the field on the other model which is a foreign key to this model |
|
87 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
88 | + // set that field on the other model to this model's ID |
|
89 | + $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
90 | + $other_model_obj->save(); |
|
91 | + return $other_model_obj; |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * Sets the other model object's foreign key to its default, instead of pointing to this model object |
|
97 | + * |
|
98 | + * @param EE_Base_Class|int $this_obj_or_id |
|
99 | + * @param EE_Base_Class|int $other_obj_or_id |
|
100 | + * @param array $where_query |
|
101 | + * @return \EE_Base_Class |
|
102 | + * @throws \EE_Error |
|
103 | + */ |
|
104 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
105 | + { |
|
106 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id); |
|
107 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
108 | + // handle possible revisions |
|
109 | + $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj, true); |
|
110 | + |
|
111 | + |
|
112 | + // if is array, then we've already done the add_relation so let's get out |
|
113 | + if (is_array($other_model_obj)) { |
|
114 | + return $other_model_obj[0]; |
|
115 | + } |
|
116 | + |
|
117 | + // find the field on the other model which is a foreign key to this model |
|
118 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
119 | + |
|
120 | + |
|
121 | + // set that field on the other model to this model's ID |
|
122 | + if ($this->_blocking_delete) { |
|
123 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
124 | + $other_model_obj->save(); |
|
125 | + } else { |
|
126 | + $other_model_obj->delete(); |
|
127 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
128 | + return $other_model_obj; |
|
129 | + } |
|
130 | + return $other_model_obj; |
|
131 | + } |
|
132 | + |
|
133 | + |
|
134 | + /** |
|
135 | + * This is identical to EE_Model_Relation->get_all_related() except we're going handle special autosave conditions |
|
136 | + * in here. |
|
137 | + * |
|
138 | + * @param EE_Base_Class|int $model_object_or_id |
|
139 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
140 | + * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
141 | + * @return EE_Base_Class[] |
|
142 | + * @throws \EE_Error |
|
143 | + */ |
|
144 | + public function get_all_related( |
|
145 | + $model_object_or_id, |
|
146 | + $query_params = array(), |
|
147 | + $values_already_prepared_by_model_object = false |
|
148 | + ) { |
|
149 | + if ($values_already_prepared_by_model_object !== false) { |
|
150 | + EE_Error::doing_it_wrong( |
|
151 | + 'EE_Model_Relation_Base::get_all_related', |
|
152 | + esc_html__('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
153 | + '4.8.1' |
|
154 | + ); |
|
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 | - } else { |
|
271 | - $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
272 | - $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
273 | - $other_obj->set( |
|
274 | - $pk_on_related_model, |
|
275 | - null, |
|
276 | - true |
|
277 | - ); // ensure we create a new row for the autosave with parent id the same as the incoming ID. |
|
278 | - $other_obj->save(); // make sure we insert. |
|
279 | - return array($other_obj); |
|
280 | - } |
|
281 | - } |
|
282 | - |
|
283 | - // var_dump('what makes it here'); |
|
284 | - // var_dump($other_obj); |
|
285 | - // 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) |
|
286 | - |
|
287 | - // 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. |
|
288 | - } else { |
|
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 | - return $other_obj; |
|
313 | - } |
|
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 | + } else { |
|
271 | + $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
272 | + $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
273 | + $other_obj->set( |
|
274 | + $pk_on_related_model, |
|
275 | + null, |
|
276 | + true |
|
277 | + ); // ensure we create a new row for the autosave with parent id the same as the incoming ID. |
|
278 | + $other_obj->save(); // make sure we insert. |
|
279 | + return array($other_obj); |
|
280 | + } |
|
281 | + } |
|
282 | + |
|
283 | + // var_dump('what makes it here'); |
|
284 | + // var_dump($other_obj); |
|
285 | + // 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) |
|
286 | + |
|
287 | + // 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. |
|
288 | + } else { |
|
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 | + return $other_obj; |
|
313 | + } |
|
314 | 314 | } |
@@ -15,94 +15,94 @@ |
||
15 | 15 | class EE_Has_Many_Any_Relation extends EE_Has_Many_Relation |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * Gets the SQL string for performing the join between this model and the other model. |
|
20 | - * |
|
21 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
22 | - * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
23 | - * other_model_primary_table.fk" etc |
|
24 | - * @throws \EE_Error |
|
25 | - */ |
|
26 | - public function get_join_statement($model_relation_chain) |
|
27 | - { |
|
28 | - // create the sql string like |
|
29 | - // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions |
|
30 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
31 | - $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
32 | - $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
33 | - $model_relation_chain, |
|
34 | - $this->get_this_model()->get_this_model_name() |
|
35 | - ) . $this_table_pk_field->get_table_alias(); |
|
36 | - $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
37 | - $model_relation_chain, |
|
38 | - $this->get_other_model()->get_this_model_name() |
|
39 | - ) . $other_table_fk_field->get_table_alias(); |
|
40 | - $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
41 | - $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
|
18 | + /** |
|
19 | + * Gets the SQL string for performing the join between this model and the other model. |
|
20 | + * |
|
21 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
22 | + * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
23 | + * other_model_primary_table.fk" etc |
|
24 | + * @throws \EE_Error |
|
25 | + */ |
|
26 | + public function get_join_statement($model_relation_chain) |
|
27 | + { |
|
28 | + // create the sql string like |
|
29 | + // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions |
|
30 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
31 | + $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
32 | + $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
33 | + $model_relation_chain, |
|
34 | + $this->get_this_model()->get_this_model_name() |
|
35 | + ) . $this_table_pk_field->get_table_alias(); |
|
36 | + $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
37 | + $model_relation_chain, |
|
38 | + $this->get_other_model()->get_this_model_name() |
|
39 | + ) . $other_table_fk_field->get_table_alias(); |
|
40 | + $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
41 | + $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
|
42 | 42 | |
43 | - return $this->_left_join( |
|
44 | - $fk_table, |
|
45 | - $fk_table_alias, |
|
46 | - $other_table_fk_field->get_table_column(), |
|
47 | - $pk_table_alias, |
|
48 | - $this_table_pk_field->get_table_column(), |
|
49 | - $fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
50 | - ) |
|
51 | - . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
52 | - } |
|
43 | + return $this->_left_join( |
|
44 | + $fk_table, |
|
45 | + $fk_table_alias, |
|
46 | + $other_table_fk_field->get_table_column(), |
|
47 | + $pk_table_alias, |
|
48 | + $this_table_pk_field->get_table_column(), |
|
49 | + $fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
50 | + ) |
|
51 | + . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
52 | + } |
|
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
57 | - * you like. |
|
58 | - * |
|
59 | - * @param EE_Base_Class|int $this_obj_or_id |
|
60 | - * @param EE_Base_Class|int $other_obj_or_id |
|
61 | - * @param array $extra_join_model_fields_n_values |
|
62 | - * @return \EE_Base_Class |
|
63 | - * @throws \EE_Error |
|
64 | - */ |
|
65 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
66 | - { |
|
67 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
68 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
55 | + /** |
|
56 | + * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
57 | + * you like. |
|
58 | + * |
|
59 | + * @param EE_Base_Class|int $this_obj_or_id |
|
60 | + * @param EE_Base_Class|int $other_obj_or_id |
|
61 | + * @param array $extra_join_model_fields_n_values |
|
62 | + * @return \EE_Base_Class |
|
63 | + * @throws \EE_Error |
|
64 | + */ |
|
65 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
66 | + { |
|
67 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
68 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
69 | 69 | |
70 | - // find the field on the other model which is a foreign key to this model |
|
71 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
72 | - // set that field on the other model to this model's ID |
|
73 | - $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
74 | - $other_model_obj->set( |
|
75 | - $this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
76 | - $this->get_this_model()->get_this_model_name() |
|
77 | - ); |
|
78 | - $other_model_obj->save(); |
|
79 | - return $other_model_obj; |
|
80 | - } |
|
70 | + // find the field on the other model which is a foreign key to this model |
|
71 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
72 | + // set that field on the other model to this model's ID |
|
73 | + $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
74 | + $other_model_obj->set( |
|
75 | + $this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
76 | + $this->get_this_model()->get_this_model_name() |
|
77 | + ); |
|
78 | + $other_model_obj->save(); |
|
79 | + return $other_model_obj; |
|
80 | + } |
|
81 | 81 | |
82 | 82 | |
83 | - /** |
|
84 | - * Sets the other model object's foreign key to its default, instead of pointing to this model object. |
|
85 | - * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it |
|
86 | - * |
|
87 | - * @param EE_Base_Class|int $this_obj_or_id |
|
88 | - * @param EE_Base_Class|int $other_obj_or_id |
|
89 | - * @param array $where_query |
|
90 | - * @return \EE_Base_Class |
|
91 | - * @throws \EE_Error |
|
92 | - */ |
|
93 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
94 | - { |
|
95 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
96 | - // find the field on the other model which is a foreign key to this model |
|
97 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
98 | - // set that field on the other model to this model's ID |
|
99 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
100 | - $other_model_obj->set( |
|
101 | - $this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
102 | - null, |
|
103 | - true |
|
104 | - ); |
|
105 | - $other_model_obj->save(); |
|
106 | - return $other_model_obj; |
|
107 | - } |
|
83 | + /** |
|
84 | + * Sets the other model object's foreign key to its default, instead of pointing to this model object. |
|
85 | + * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it |
|
86 | + * |
|
87 | + * @param EE_Base_Class|int $this_obj_or_id |
|
88 | + * @param EE_Base_Class|int $other_obj_or_id |
|
89 | + * @param array $where_query |
|
90 | + * @return \EE_Base_Class |
|
91 | + * @throws \EE_Error |
|
92 | + */ |
|
93 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
94 | + { |
|
95 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
96 | + // find the field on the other model which is a foreign key to this model |
|
97 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
98 | + // set that field on the other model to this model's ID |
|
99 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
100 | + $other_model_obj->set( |
|
101 | + $this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
102 | + null, |
|
103 | + true |
|
104 | + ); |
|
105 | + $other_model_obj->save(); |
|
106 | + return $other_model_obj; |
|
107 | + } |
|
108 | 108 | } |
@@ -32,11 +32,11 @@ discard block |
||
32 | 32 | $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
33 | 33 | $model_relation_chain, |
34 | 34 | $this->get_this_model()->get_this_model_name() |
35 | - ) . $this_table_pk_field->get_table_alias(); |
|
36 | - $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
35 | + ).$this_table_pk_field->get_table_alias(); |
|
36 | + $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
37 | 37 | $model_relation_chain, |
38 | 38 | $this->get_other_model()->get_this_model_name() |
39 | - ) . $other_table_fk_field->get_table_alias(); |
|
39 | + ).$other_table_fk_field->get_table_alias(); |
|
40 | 40 | $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
41 | 41 | $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
42 | 42 | |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | $other_table_fk_field->get_table_column(), |
47 | 47 | $pk_table_alias, |
48 | 48 | $this_table_pk_field->get_table_column(), |
49 | - $fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
49 | + $fk_table_alias.'.'.$field_with_model_name->get_table_column()."='".$this->get_this_model()->get_this_model_name()."'" |
|
50 | 50 | ) |
51 | 51 | . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
52 | 52 | } |
@@ -6,5 +6,5 @@ |
||
6 | 6 | */ |
7 | 7 | class EE_Unique_Index extends EE_Index |
8 | 8 | { |
9 | - // yep, actually the same as index right now |
|
9 | + // yep, actually the same as index right now |
|
10 | 10 | } |
@@ -6,5 +6,5 @@ |
||
6 | 6 | */ |
7 | 7 | class EE_Primary_Key_Index extends EE_Unique_Index |
8 | 8 | { |
9 | - // yep, actually the same as unique index right now |
|
9 | + // yep, actually the same as unique index right now |
|
10 | 10 | } |
@@ -14,141 +14,141 @@ discard block |
||
14 | 14 | /** |
15 | 15 | * @var string SQL for performing joins (Eg, "INNER JOIN blah ON blah=blah INNER JOIN FOO ON foo=foo...") |
16 | 16 | */ |
17 | - private $_join_sql; |
|
17 | + private $_join_sql; |
|
18 | 18 | /** |
19 | 19 | * |
20 | 20 | * @var array stating all the models that have been included thus far,so we don't get duplicates. |
21 | 21 | * Keys are the model relation chains to them from the queried model |
22 | 22 | * (eg, "Registration.Transaction.Payment"), and valuesare model names (eg "Payment") |
23 | 23 | */ |
24 | - private $_models_included; |
|
24 | + private $_models_included; |
|
25 | 25 | |
26 | 26 | /** |
27 | 27 | * After we've acquired all the data types, we can create this sql. |
28 | 28 | * @var string |
29 | 29 | */ |
30 | - private $_where_sql; |
|
30 | + private $_where_sql; |
|
31 | 31 | /** |
32 | 32 | * Full join sql. Eg, in a select query, that's everything after the "FROM", and before the "WHERE", so it includes |
33 | 33 | * the declaration of the main model's tables, and then appends all the joining sql to other models |
34 | 34 | * @var string |
35 | 35 | */ |
36 | - private $_main_join_sql; |
|
36 | + private $_main_join_sql; |
|
37 | 37 | |
38 | 38 | |
39 | - private $_limit_sql; |
|
39 | + private $_limit_sql; |
|
40 | 40 | |
41 | - private $_order_by_sql; |
|
41 | + private $_order_by_sql; |
|
42 | 42 | |
43 | - private $_having_sql; |
|
43 | + private $_having_sql; |
|
44 | 44 | |
45 | - private $_group_by_sql; |
|
45 | + private $_group_by_sql; |
|
46 | 46 | |
47 | - public function set_limit_sql($limit_sql) |
|
48 | - { |
|
49 | - $this->_limit_sql = $limit_sql; |
|
50 | - } |
|
47 | + public function set_limit_sql($limit_sql) |
|
48 | + { |
|
49 | + $this->_limit_sql = $limit_sql; |
|
50 | + } |
|
51 | 51 | |
52 | - public function set_order_by_sql($order_by_sql) |
|
53 | - { |
|
54 | - $this->_order_by_sql = $order_by_sql; |
|
55 | - } |
|
56 | - public function set_group_by_sql($group_by_sql) |
|
57 | - { |
|
58 | - $this->_group_by_sql = $group_by_sql; |
|
59 | - } |
|
60 | - public function set_having_sql($having_sql) |
|
61 | - { |
|
62 | - $this->_having_sql = $having_sql; |
|
63 | - } |
|
64 | - public function get_limit_sql() |
|
65 | - { |
|
66 | - return $this->_limit_sql; |
|
67 | - } |
|
68 | - public function get_order_by_sql() |
|
69 | - { |
|
70 | - return $this->_order_by_sql; |
|
71 | - } |
|
72 | - public function get_group_by_sql() |
|
73 | - { |
|
74 | - return $this->_group_by_sql; |
|
75 | - } |
|
76 | - public function get_having_sql() |
|
77 | - { |
|
78 | - return $this->_having_sql; |
|
79 | - } |
|
52 | + public function set_order_by_sql($order_by_sql) |
|
53 | + { |
|
54 | + $this->_order_by_sql = $order_by_sql; |
|
55 | + } |
|
56 | + public function set_group_by_sql($group_by_sql) |
|
57 | + { |
|
58 | + $this->_group_by_sql = $group_by_sql; |
|
59 | + } |
|
60 | + public function set_having_sql($having_sql) |
|
61 | + { |
|
62 | + $this->_having_sql = $having_sql; |
|
63 | + } |
|
64 | + public function get_limit_sql() |
|
65 | + { |
|
66 | + return $this->_limit_sql; |
|
67 | + } |
|
68 | + public function get_order_by_sql() |
|
69 | + { |
|
70 | + return $this->_order_by_sql; |
|
71 | + } |
|
72 | + public function get_group_by_sql() |
|
73 | + { |
|
74 | + return $this->_group_by_sql; |
|
75 | + } |
|
76 | + public function get_having_sql() |
|
77 | + { |
|
78 | + return $this->_having_sql; |
|
79 | + } |
|
80 | 80 | /** |
81 | 81 | * |
82 | 82 | * @param type $model_included_name |
83 | 83 | * @param type $join_sql |
84 | 84 | * @param type $data_types |
85 | 85 | */ |
86 | - public function __construct($model_included_name = array(), $join_sql = '') |
|
87 | - { |
|
88 | - $this->_models_included = $model_included_name; |
|
89 | - $this->_join_sql = $join_sql; |
|
90 | - } |
|
86 | + public function __construct($model_included_name = array(), $join_sql = '') |
|
87 | + { |
|
88 | + $this->_models_included = $model_included_name; |
|
89 | + $this->_join_sql = $join_sql; |
|
90 | + } |
|
91 | 91 | |
92 | 92 | /** |
93 | 93 | * Merges info from the other EEM_Related_Model_Info_Carrier into this one. |
94 | 94 | * @param EE_Model_Query_Info_Carrier $other_model_query_info_carrier |
95 | 95 | */ |
96 | - public function merge($other_model_query_info_carrier) |
|
97 | - { |
|
98 | - if ($other_model_query_info_carrier && ! $this->_have_already_included_one_of_these_models($other_model_query_info_carrier->get_model_names_included())) { |
|
99 | - $model_included_on_other_join_sql_and_data_types_carrier = $other_model_query_info_carrier->get_model_names_included(); |
|
100 | - $this->_models_included = array_merge($this->_models_included, $model_included_on_other_join_sql_and_data_types_carrier); |
|
101 | - $this->_join_sql .= $other_model_query_info_carrier->_join_sql; |
|
102 | - } |
|
103 | - // otherwise don't merge our data. |
|
104 | - // yes, this means that we must immediately merge any model data into our grand list |
|
105 | - // as soon as we get some from ONE model, or else we could reject a EEM_Related_Model_Info_Carrier |
|
106 | - // which is carrying info from two models WHERE one is already included but the other is NOT |
|
107 | - } |
|
96 | + public function merge($other_model_query_info_carrier) |
|
97 | + { |
|
98 | + if ($other_model_query_info_carrier && ! $this->_have_already_included_one_of_these_models($other_model_query_info_carrier->get_model_names_included())) { |
|
99 | + $model_included_on_other_join_sql_and_data_types_carrier = $other_model_query_info_carrier->get_model_names_included(); |
|
100 | + $this->_models_included = array_merge($this->_models_included, $model_included_on_other_join_sql_and_data_types_carrier); |
|
101 | + $this->_join_sql .= $other_model_query_info_carrier->_join_sql; |
|
102 | + } |
|
103 | + // otherwise don't merge our data. |
|
104 | + // yes, this means that we must immediately merge any model data into our grand list |
|
105 | + // as soon as we get some from ONE model, or else we could reject a EEM_Related_Model_Info_Carrier |
|
106 | + // which is carrying info from two models WHERE one is already included but the other is NOT |
|
107 | + } |
|
108 | 108 | /** |
109 | 109 | * Checks whether or not we have already included all the models mentione din $model_names on the query info varrier |
110 | 110 | * @param array $model_names just like EE_MOdel_QUery_Info_Carrier::_models_included: keys are model chain paths, values are the model names only |
111 | 111 | * @return boolean |
112 | 112 | */ |
113 | - protected function _have_already_included_one_of_these_models($model_names) |
|
114 | - { |
|
115 | - foreach ($this->_models_included as $model_relation_path => $model_included) { |
|
116 | - if (array_key_exists($model_relation_path, $model_names)) { |
|
117 | - return true; |
|
118 | - } |
|
119 | - } |
|
120 | - return false; |
|
121 | - } |
|
113 | + protected function _have_already_included_one_of_these_models($model_names) |
|
114 | + { |
|
115 | + foreach ($this->_models_included as $model_relation_path => $model_included) { |
|
116 | + if (array_key_exists($model_relation_path, $model_names)) { |
|
117 | + return true; |
|
118 | + } |
|
119 | + } |
|
120 | + return false; |
|
121 | + } |
|
122 | 122 | /** |
123 | 123 | * Array keys are model names, values are "model relation paths". See EE_Model_Query_Info_Carrier::_models_included for details |
124 | 124 | * @return array like EE_Model_Query_Info_Carrier::_models_included |
125 | 125 | */ |
126 | - public function get_model_names_included() |
|
127 | - { |
|
128 | - return $this->_models_included; |
|
129 | - } |
|
126 | + public function get_model_names_included() |
|
127 | + { |
|
128 | + return $this->_models_included; |
|
129 | + } |
|
130 | 130 | /** |
131 | 131 | * sets the $where_sql for later use from client code |
132 | 132 | * @param string $where_sql |
133 | 133 | */ |
134 | - public function set_where_sql($where_sql) |
|
135 | - { |
|
136 | - $this->_where_sql = $where_sql; |
|
137 | - } |
|
138 | - public function get_where_sql() |
|
139 | - { |
|
140 | - return $this->_where_sql; |
|
141 | - } |
|
134 | + public function set_where_sql($where_sql) |
|
135 | + { |
|
136 | + $this->_where_sql = $where_sql; |
|
137 | + } |
|
138 | + public function get_where_sql() |
|
139 | + { |
|
140 | + return $this->_where_sql; |
|
141 | + } |
|
142 | 142 | |
143 | 143 | /** |
144 | 144 | * Gets the SQL for joining the main model to other models involves in the query, which was set earlier on |
145 | 145 | * the EE_Model_Query_info_Carrier by calling set_main_model_join_sql() |
146 | 146 | * @return string |
147 | 147 | */ |
148 | - public function get_main_model_join_sql() |
|
149 | - { |
|
150 | - return $this->_main_join_sql; |
|
151 | - } |
|
148 | + public function get_main_model_join_sql() |
|
149 | + { |
|
150 | + return $this->_main_join_sql; |
|
151 | + } |
|
152 | 152 | |
153 | 153 | |
154 | 154 | /** |
@@ -158,12 +158,12 @@ discard block |
||
158 | 158 | * after the FROM and before the WHERE.) |
159 | 159 | * @param string $join_sql |
160 | 160 | */ |
161 | - public function set_main_model_join_sql($join_sql) |
|
162 | - { |
|
163 | - $this->_main_join_sql = $join_sql; |
|
164 | - } |
|
165 | - public function get_full_join_sql() |
|
166 | - { |
|
167 | - return $this->_main_join_sql . $this->_join_sql; |
|
168 | - } |
|
161 | + public function set_main_model_join_sql($join_sql) |
|
162 | + { |
|
163 | + $this->_main_join_sql = $join_sql; |
|
164 | + } |
|
165 | + public function get_full_join_sql() |
|
166 | + { |
|
167 | + return $this->_main_join_sql . $this->_join_sql; |
|
168 | + } |
|
169 | 169 | } |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | public function merge($other_model_query_info_carrier) |
97 | 97 | { |
98 | 98 | if ($other_model_query_info_carrier && ! $this->_have_already_included_one_of_these_models($other_model_query_info_carrier->get_model_names_included())) { |
99 | - $model_included_on_other_join_sql_and_data_types_carrier = $other_model_query_info_carrier->get_model_names_included(); |
|
99 | + $model_included_on_other_join_sql_and_data_types_carrier = $other_model_query_info_carrier->get_model_names_included(); |
|
100 | 100 | $this->_models_included = array_merge($this->_models_included, $model_included_on_other_join_sql_and_data_types_carrier); |
101 | 101 | $this->_join_sql .= $other_model_query_info_carrier->_join_sql; |
102 | 102 | } |
@@ -164,6 +164,6 @@ discard block |
||
164 | 164 | } |
165 | 165 | public function get_full_join_sql() |
166 | 166 | { |
167 | - return $this->_main_join_sql . $this->_join_sql; |
|
167 | + return $this->_main_join_sql.$this->_join_sql; |
|
168 | 168 | } |
169 | 169 | } |
@@ -65,14 +65,14 @@ discard block |
||
65 | 65 | self::$_instance = new self($grand_total, $session); |
66 | 66 | } |
67 | 67 | // or maybe retrieve an existing one ? |
68 | - if (! self::$_instance instanceof EE_Cart) { |
|
68 | + if ( ! self::$_instance instanceof EE_Cart) { |
|
69 | 69 | // try getting the cart out of the session |
70 | 70 | $saved_cart = $session instanceof EE_Session ? $session->cart() : null; |
71 | 71 | self::$_instance = $saved_cart instanceof EE_Cart ? $saved_cart : new self($grand_total, $session); |
72 | 72 | unset($saved_cart); |
73 | 73 | } |
74 | 74 | // verify that cart is ok and grand total line item exists |
75 | - if (! self::$_instance instanceof EE_Cart || ! self::$_instance->_grand_total instanceof EE_Line_Item) { |
|
75 | + if ( ! self::$_instance instanceof EE_Cart || ! self::$_instance->_grand_total instanceof EE_Line_Item) { |
|
76 | 76 | self::$_instance = new self($grand_total, $session); |
77 | 77 | } |
78 | 78 | self::$_instance->get_grand_total(); |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | */ |
125 | 125 | public function session() |
126 | 126 | { |
127 | - if (! $this->_session instanceof EE_Session) { |
|
127 | + if ( ! $this->_session instanceof EE_Session) { |
|
128 | 128 | $this->set_session(); |
129 | 129 | } |
130 | 130 | return $this->_session; |
@@ -392,7 +392,7 @@ discard block |
||
392 | 392 | |
393 | 393 | public function __wakeup() |
394 | 394 | { |
395 | - if (! $this->_grand_total instanceof EE_Line_Item && absint($this->_grand_total) !== 0) { |
|
395 | + if ( ! $this->_grand_total instanceof EE_Line_Item && absint($this->_grand_total) !== 0) { |
|
396 | 396 | // $this->_grand_total is actually just an ID, so use it to get the object from the db |
397 | 397 | $this->_grand_total = EEM_Line_Item::instance()->get_one_by_ID($this->_grand_total); |
398 | 398 | } |
@@ -17,397 +17,397 @@ |
||
17 | 17 | class EE_Cart implements ResettableInterface |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * instance of the EE_Cart object |
|
22 | - * |
|
23 | - * @access private |
|
24 | - * @var EE_Cart $_instance |
|
25 | - */ |
|
26 | - private static $_instance; |
|
27 | - |
|
28 | - /** |
|
29 | - * instance of the EE_Session object |
|
30 | - * |
|
31 | - * @access protected |
|
32 | - * @var EE_Session $_session |
|
33 | - */ |
|
34 | - protected $_session; |
|
35 | - |
|
36 | - /** |
|
37 | - * The total Line item which comprises all the children line-item subtotals, |
|
38 | - * which in turn each have their line items. |
|
39 | - * Typically, the line item structure will look like: |
|
40 | - * grand total |
|
41 | - * -tickets-sub-total |
|
42 | - * --ticket1 |
|
43 | - * --ticket2 |
|
44 | - * --... |
|
45 | - * -taxes-sub-total |
|
46 | - * --tax1 |
|
47 | - * --tax2 |
|
48 | - * |
|
49 | - * @var EE_Line_Item |
|
50 | - */ |
|
51 | - private $_grand_total; |
|
52 | - |
|
53 | - |
|
54 | - /** |
|
55 | - * @singleton method used to instantiate class object |
|
56 | - * @access public |
|
57 | - * @param EE_Line_Item $grand_total |
|
58 | - * @param EE_Session $session |
|
59 | - * @return \EE_Cart |
|
60 | - * @throws \EE_Error |
|
61 | - */ |
|
62 | - public static function instance(EE_Line_Item $grand_total = null, EE_Session $session = null) |
|
63 | - { |
|
64 | - if ($grand_total instanceof EE_Line_Item && $grand_total->is_total()) { |
|
65 | - self::$_instance = new self($grand_total, $session); |
|
66 | - } |
|
67 | - // or maybe retrieve an existing one ? |
|
68 | - if (! self::$_instance instanceof EE_Cart) { |
|
69 | - // try getting the cart out of the session |
|
70 | - $saved_cart = $session instanceof EE_Session ? $session->cart() : null; |
|
71 | - self::$_instance = $saved_cart instanceof EE_Cart ? $saved_cart : new self($grand_total, $session); |
|
72 | - unset($saved_cart); |
|
73 | - } |
|
74 | - // verify that cart is ok and grand total line item exists |
|
75 | - if (! self::$_instance instanceof EE_Cart || ! self::$_instance->_grand_total instanceof EE_Line_Item) { |
|
76 | - self::$_instance = new self($grand_total, $session); |
|
77 | - } |
|
78 | - self::$_instance->get_grand_total(); |
|
79 | - // once everything is all said and done, save the cart to the EE_Session |
|
80 | - add_action('shutdown', array(self::$_instance, 'save_cart'), 90); |
|
81 | - return self::$_instance; |
|
82 | - } |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * private constructor to prevent direct creation |
|
87 | - * |
|
88 | - * @Constructor |
|
89 | - * @access private |
|
90 | - * @param EE_Line_Item $grand_total |
|
91 | - * @param EE_Session $session |
|
92 | - */ |
|
93 | - private function __construct(EE_Line_Item $grand_total = null, EE_Session $session = null) |
|
94 | - { |
|
95 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
96 | - $this->set_session($session); |
|
97 | - if ($grand_total instanceof EE_Line_Item && $grand_total->is_total()) { |
|
98 | - $this->set_grand_total_line_item($grand_total); |
|
99 | - } |
|
100 | - } |
|
101 | - |
|
102 | - |
|
103 | - /** |
|
104 | - * Resets the cart completely (whereas empty_cart |
|
105 | - * |
|
106 | - * @param EE_Line_Item $grand_total |
|
107 | - * @param EE_Session $session |
|
108 | - * @return EE_Cart |
|
109 | - * @throws \EE_Error |
|
110 | - */ |
|
111 | - public static function reset(EE_Line_Item $grand_total = null, EE_Session $session = null) |
|
112 | - { |
|
113 | - remove_action('shutdown', array(self::$_instance, 'save_cart'), 90); |
|
114 | - if ($session instanceof EE_Session) { |
|
115 | - $session->reset_cart(); |
|
116 | - } |
|
117 | - self::$_instance = null; |
|
118 | - return self::instance($grand_total, $session); |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * @return \EE_Session |
|
124 | - */ |
|
125 | - public function session() |
|
126 | - { |
|
127 | - if (! $this->_session instanceof EE_Session) { |
|
128 | - $this->set_session(); |
|
129 | - } |
|
130 | - return $this->_session; |
|
131 | - } |
|
132 | - |
|
133 | - |
|
134 | - /** |
|
135 | - * @param EE_Session $session |
|
136 | - */ |
|
137 | - public function set_session(EE_Session $session = null) |
|
138 | - { |
|
139 | - $this->_session = $session instanceof EE_Session ? $session : EE_Registry::instance()->load_core('Session'); |
|
140 | - } |
|
141 | - |
|
142 | - |
|
143 | - /** |
|
144 | - * Sets the cart to match the line item. Especially handy for loading an old cart where you |
|
145 | - * know the grand total line item on it |
|
146 | - * |
|
147 | - * @param EE_Line_Item $line_item |
|
148 | - */ |
|
149 | - public function set_grand_total_line_item(EE_Line_Item $line_item) |
|
150 | - { |
|
151 | - $this->_grand_total = $line_item; |
|
152 | - } |
|
153 | - |
|
154 | - |
|
155 | - /** |
|
156 | - * get_cart_from_reg_url_link |
|
157 | - * |
|
158 | - * @access public |
|
159 | - * @param EE_Transaction $transaction |
|
160 | - * @param EE_Session $session |
|
161 | - * @return \EE_Cart |
|
162 | - * @throws \EE_Error |
|
163 | - */ |
|
164 | - public static function get_cart_from_txn(EE_Transaction $transaction, EE_Session $session = null) |
|
165 | - { |
|
166 | - $grand_total = $transaction->total_line_item(); |
|
167 | - $grand_total->get_items(); |
|
168 | - $grand_total->tax_descendants(); |
|
169 | - return EE_Cart::instance($grand_total, $session); |
|
170 | - } |
|
171 | - |
|
172 | - |
|
173 | - /** |
|
174 | - * Creates the total line item, and ensures it has its 'tickets' and 'taxes' sub-items |
|
175 | - * |
|
176 | - * @return EE_Line_Item |
|
177 | - * @throws \EE_Error |
|
178 | - */ |
|
179 | - private function _create_grand_total() |
|
180 | - { |
|
181 | - $this->_grand_total = EEH_Line_Item::create_total_line_item(); |
|
182 | - return $this->_grand_total; |
|
183 | - } |
|
184 | - |
|
185 | - |
|
186 | - /** |
|
187 | - * Gets all the line items of object type Ticket |
|
188 | - * |
|
189 | - * @access public |
|
190 | - * @return \EE_Line_Item[] |
|
191 | - */ |
|
192 | - public function get_tickets() |
|
193 | - { |
|
194 | - if ($this->_grand_total === null) { |
|
195 | - return array(); |
|
196 | - } |
|
197 | - return EEH_Line_Item::get_ticket_line_items($this->_grand_total); |
|
198 | - } |
|
199 | - |
|
200 | - |
|
201 | - /** |
|
202 | - * returns the total quantity of tickets in the cart |
|
203 | - * |
|
204 | - * @access public |
|
205 | - * @return int |
|
206 | - * @throws \EE_Error |
|
207 | - */ |
|
208 | - public function all_ticket_quantity_count() |
|
209 | - { |
|
210 | - $tickets = $this->get_tickets(); |
|
211 | - if (empty($tickets)) { |
|
212 | - return 0; |
|
213 | - } |
|
214 | - $count = 0; |
|
215 | - foreach ($tickets as $ticket) { |
|
216 | - $count += $ticket->get('LIN_quantity'); |
|
217 | - } |
|
218 | - return $count; |
|
219 | - } |
|
220 | - |
|
221 | - |
|
222 | - /** |
|
223 | - * Gets all the tax line items |
|
224 | - * |
|
225 | - * @return \EE_Line_Item[] |
|
226 | - * @throws \EE_Error |
|
227 | - */ |
|
228 | - public function get_taxes() |
|
229 | - { |
|
230 | - return EEH_Line_Item::get_taxes_subtotal($this->_grand_total)->children(); |
|
231 | - } |
|
232 | - |
|
233 | - |
|
234 | - /** |
|
235 | - * Gets the total line item (which is a parent of all other line items) on this cart |
|
236 | - * |
|
237 | - * @return EE_Line_Item |
|
238 | - * @throws \EE_Error |
|
239 | - */ |
|
240 | - public function get_grand_total() |
|
241 | - { |
|
242 | - return $this->_grand_total instanceof EE_Line_Item ? $this->_grand_total : $this->_create_grand_total(); |
|
243 | - } |
|
244 | - |
|
245 | - |
|
246 | - /** |
|
247 | - * @process items for adding to cart |
|
248 | - * @access public |
|
249 | - * @param EE_Ticket $ticket |
|
250 | - * @param int $qty |
|
251 | - * @return TRUE on success, FALSE on fail |
|
252 | - * @throws \EE_Error |
|
253 | - */ |
|
254 | - public function add_ticket_to_cart(EE_Ticket $ticket, $qty = 1) |
|
255 | - { |
|
256 | - EEH_Line_Item::add_ticket_purchase($this->get_grand_total(), $ticket, $qty); |
|
257 | - return $this->save_cart() ? true : false; |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - /** |
|
262 | - * get_cart_total_before_tax |
|
263 | - * |
|
264 | - * @access public |
|
265 | - * @return float |
|
266 | - * @throws \EE_Error |
|
267 | - */ |
|
268 | - public function get_cart_total_before_tax() |
|
269 | - { |
|
270 | - return $this->get_grand_total()->recalculate_pre_tax_total(); |
|
271 | - } |
|
272 | - |
|
273 | - |
|
274 | - /** |
|
275 | - * gets the total amount of tax paid for items in this cart |
|
276 | - * |
|
277 | - * @access public |
|
278 | - * @return float |
|
279 | - * @throws \EE_Error |
|
280 | - */ |
|
281 | - public function get_applied_taxes() |
|
282 | - { |
|
283 | - return EEH_Line_Item::ensure_taxes_applied($this->_grand_total); |
|
284 | - } |
|
285 | - |
|
286 | - |
|
287 | - /** |
|
288 | - * Gets the total amount to be paid for the items in the cart, including taxes and other modifiers |
|
289 | - * |
|
290 | - * @access public |
|
291 | - * @return float |
|
292 | - * @throws \EE_Error |
|
293 | - */ |
|
294 | - public function get_cart_grand_total() |
|
295 | - { |
|
296 | - EEH_Line_Item::ensure_taxes_applied($this->_grand_total); |
|
297 | - return $this->get_grand_total()->total(); |
|
298 | - } |
|
299 | - |
|
300 | - |
|
301 | - /** |
|
302 | - * Gets the total amount to be paid for the items in the cart, including taxes and other modifiers |
|
303 | - * |
|
304 | - * @access public |
|
305 | - * @return float |
|
306 | - * @throws \EE_Error |
|
307 | - */ |
|
308 | - public function recalculate_all_cart_totals() |
|
309 | - { |
|
310 | - $pre_tax_total = $this->get_cart_total_before_tax(); |
|
311 | - $taxes_total = EEH_Line_Item::ensure_taxes_applied($this->_grand_total); |
|
312 | - $this->_grand_total->set_total($pre_tax_total + $taxes_total); |
|
313 | - $this->_grand_total->save_this_and_descendants_to_txn(); |
|
314 | - return $this->get_grand_total()->total(); |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - /** |
|
319 | - * deletes an item from the cart |
|
320 | - * |
|
321 | - * @access public |
|
322 | - * @param array|bool|string $line_item_codes |
|
323 | - * @return int on success, FALSE on fail |
|
324 | - * @throws \EE_Error |
|
325 | - */ |
|
326 | - public function delete_items($line_item_codes = false) |
|
327 | - { |
|
328 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
329 | - return EEH_Line_Item::delete_items($this->get_grand_total(), $line_item_codes); |
|
330 | - } |
|
331 | - |
|
332 | - |
|
333 | - /** |
|
334 | - * @remove ALL items from cart and zero ALL totals |
|
335 | - * @access public |
|
336 | - * @return bool |
|
337 | - * @throws \EE_Error |
|
338 | - */ |
|
339 | - public function empty_cart() |
|
340 | - { |
|
341 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
342 | - $this->_grand_total = $this->_create_grand_total(); |
|
343 | - return $this->save_cart(true); |
|
344 | - } |
|
345 | - |
|
346 | - |
|
347 | - /** |
|
348 | - * @remove ALL items from cart and delete total as well |
|
349 | - * @access public |
|
350 | - * @return bool |
|
351 | - * @throws \EE_Error |
|
352 | - */ |
|
353 | - public function delete_cart() |
|
354 | - { |
|
355 | - if ($this->_grand_total instanceof EE_Line_Item) { |
|
356 | - $deleted = EEH_Line_Item::delete_all_child_items($this->_grand_total); |
|
357 | - if ($deleted) { |
|
358 | - $deleted += $this->_grand_total->delete(); |
|
359 | - $this->_grand_total = null; |
|
360 | - return true; |
|
361 | - } |
|
362 | - } |
|
363 | - return false; |
|
364 | - } |
|
365 | - |
|
366 | - |
|
367 | - /** |
|
368 | - * @save cart to session |
|
369 | - * @access public |
|
370 | - * @param bool $apply_taxes |
|
371 | - * @return TRUE on success, FALSE on fail |
|
372 | - * @throws \EE_Error |
|
373 | - */ |
|
374 | - public function save_cart($apply_taxes = true) |
|
375 | - { |
|
376 | - if ($apply_taxes && $this->_grand_total instanceof EE_Line_Item) { |
|
377 | - EEH_Line_Item::ensure_taxes_applied($this->_grand_total); |
|
378 | - // make sure we don't cache the transaction because it can get stale |
|
379 | - if ( |
|
380 | - $this->_grand_total->get_one_from_cache('Transaction') instanceof EE_Transaction |
|
381 | - && $this->_grand_total->get_one_from_cache('Transaction')->ID() |
|
382 | - ) { |
|
383 | - $this->_grand_total->clear_cache('Transaction', null, true); |
|
384 | - } |
|
385 | - } |
|
386 | - if ($this->session() instanceof EE_Session) { |
|
387 | - return $this->session()->set_cart($this); |
|
388 | - } else { |
|
389 | - return false; |
|
390 | - } |
|
391 | - } |
|
392 | - |
|
393 | - |
|
394 | - public function __wakeup() |
|
395 | - { |
|
396 | - if (! $this->_grand_total instanceof EE_Line_Item && absint($this->_grand_total) !== 0) { |
|
397 | - // $this->_grand_total is actually just an ID, so use it to get the object from the db |
|
398 | - $this->_grand_total = EEM_Line_Item::instance()->get_one_by_ID($this->_grand_total); |
|
399 | - } |
|
400 | - } |
|
401 | - |
|
402 | - |
|
403 | - /** |
|
404 | - * @return array |
|
405 | - */ |
|
406 | - public function __sleep() |
|
407 | - { |
|
408 | - if ($this->_grand_total instanceof EE_Line_Item && $this->_grand_total->ID()) { |
|
409 | - $this->_grand_total = $this->_grand_total->ID(); |
|
410 | - } |
|
411 | - return array('_grand_total'); |
|
412 | - } |
|
20 | + /** |
|
21 | + * instance of the EE_Cart object |
|
22 | + * |
|
23 | + * @access private |
|
24 | + * @var EE_Cart $_instance |
|
25 | + */ |
|
26 | + private static $_instance; |
|
27 | + |
|
28 | + /** |
|
29 | + * instance of the EE_Session object |
|
30 | + * |
|
31 | + * @access protected |
|
32 | + * @var EE_Session $_session |
|
33 | + */ |
|
34 | + protected $_session; |
|
35 | + |
|
36 | + /** |
|
37 | + * The total Line item which comprises all the children line-item subtotals, |
|
38 | + * which in turn each have their line items. |
|
39 | + * Typically, the line item structure will look like: |
|
40 | + * grand total |
|
41 | + * -tickets-sub-total |
|
42 | + * --ticket1 |
|
43 | + * --ticket2 |
|
44 | + * --... |
|
45 | + * -taxes-sub-total |
|
46 | + * --tax1 |
|
47 | + * --tax2 |
|
48 | + * |
|
49 | + * @var EE_Line_Item |
|
50 | + */ |
|
51 | + private $_grand_total; |
|
52 | + |
|
53 | + |
|
54 | + /** |
|
55 | + * @singleton method used to instantiate class object |
|
56 | + * @access public |
|
57 | + * @param EE_Line_Item $grand_total |
|
58 | + * @param EE_Session $session |
|
59 | + * @return \EE_Cart |
|
60 | + * @throws \EE_Error |
|
61 | + */ |
|
62 | + public static function instance(EE_Line_Item $grand_total = null, EE_Session $session = null) |
|
63 | + { |
|
64 | + if ($grand_total instanceof EE_Line_Item && $grand_total->is_total()) { |
|
65 | + self::$_instance = new self($grand_total, $session); |
|
66 | + } |
|
67 | + // or maybe retrieve an existing one ? |
|
68 | + if (! self::$_instance instanceof EE_Cart) { |
|
69 | + // try getting the cart out of the session |
|
70 | + $saved_cart = $session instanceof EE_Session ? $session->cart() : null; |
|
71 | + self::$_instance = $saved_cart instanceof EE_Cart ? $saved_cart : new self($grand_total, $session); |
|
72 | + unset($saved_cart); |
|
73 | + } |
|
74 | + // verify that cart is ok and grand total line item exists |
|
75 | + if (! self::$_instance instanceof EE_Cart || ! self::$_instance->_grand_total instanceof EE_Line_Item) { |
|
76 | + self::$_instance = new self($grand_total, $session); |
|
77 | + } |
|
78 | + self::$_instance->get_grand_total(); |
|
79 | + // once everything is all said and done, save the cart to the EE_Session |
|
80 | + add_action('shutdown', array(self::$_instance, 'save_cart'), 90); |
|
81 | + return self::$_instance; |
|
82 | + } |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * private constructor to prevent direct creation |
|
87 | + * |
|
88 | + * @Constructor |
|
89 | + * @access private |
|
90 | + * @param EE_Line_Item $grand_total |
|
91 | + * @param EE_Session $session |
|
92 | + */ |
|
93 | + private function __construct(EE_Line_Item $grand_total = null, EE_Session $session = null) |
|
94 | + { |
|
95 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
96 | + $this->set_session($session); |
|
97 | + if ($grand_total instanceof EE_Line_Item && $grand_total->is_total()) { |
|
98 | + $this->set_grand_total_line_item($grand_total); |
|
99 | + } |
|
100 | + } |
|
101 | + |
|
102 | + |
|
103 | + /** |
|
104 | + * Resets the cart completely (whereas empty_cart |
|
105 | + * |
|
106 | + * @param EE_Line_Item $grand_total |
|
107 | + * @param EE_Session $session |
|
108 | + * @return EE_Cart |
|
109 | + * @throws \EE_Error |
|
110 | + */ |
|
111 | + public static function reset(EE_Line_Item $grand_total = null, EE_Session $session = null) |
|
112 | + { |
|
113 | + remove_action('shutdown', array(self::$_instance, 'save_cart'), 90); |
|
114 | + if ($session instanceof EE_Session) { |
|
115 | + $session->reset_cart(); |
|
116 | + } |
|
117 | + self::$_instance = null; |
|
118 | + return self::instance($grand_total, $session); |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * @return \EE_Session |
|
124 | + */ |
|
125 | + public function session() |
|
126 | + { |
|
127 | + if (! $this->_session instanceof EE_Session) { |
|
128 | + $this->set_session(); |
|
129 | + } |
|
130 | + return $this->_session; |
|
131 | + } |
|
132 | + |
|
133 | + |
|
134 | + /** |
|
135 | + * @param EE_Session $session |
|
136 | + */ |
|
137 | + public function set_session(EE_Session $session = null) |
|
138 | + { |
|
139 | + $this->_session = $session instanceof EE_Session ? $session : EE_Registry::instance()->load_core('Session'); |
|
140 | + } |
|
141 | + |
|
142 | + |
|
143 | + /** |
|
144 | + * Sets the cart to match the line item. Especially handy for loading an old cart where you |
|
145 | + * know the grand total line item on it |
|
146 | + * |
|
147 | + * @param EE_Line_Item $line_item |
|
148 | + */ |
|
149 | + public function set_grand_total_line_item(EE_Line_Item $line_item) |
|
150 | + { |
|
151 | + $this->_grand_total = $line_item; |
|
152 | + } |
|
153 | + |
|
154 | + |
|
155 | + /** |
|
156 | + * get_cart_from_reg_url_link |
|
157 | + * |
|
158 | + * @access public |
|
159 | + * @param EE_Transaction $transaction |
|
160 | + * @param EE_Session $session |
|
161 | + * @return \EE_Cart |
|
162 | + * @throws \EE_Error |
|
163 | + */ |
|
164 | + public static function get_cart_from_txn(EE_Transaction $transaction, EE_Session $session = null) |
|
165 | + { |
|
166 | + $grand_total = $transaction->total_line_item(); |
|
167 | + $grand_total->get_items(); |
|
168 | + $grand_total->tax_descendants(); |
|
169 | + return EE_Cart::instance($grand_total, $session); |
|
170 | + } |
|
171 | + |
|
172 | + |
|
173 | + /** |
|
174 | + * Creates the total line item, and ensures it has its 'tickets' and 'taxes' sub-items |
|
175 | + * |
|
176 | + * @return EE_Line_Item |
|
177 | + * @throws \EE_Error |
|
178 | + */ |
|
179 | + private function _create_grand_total() |
|
180 | + { |
|
181 | + $this->_grand_total = EEH_Line_Item::create_total_line_item(); |
|
182 | + return $this->_grand_total; |
|
183 | + } |
|
184 | + |
|
185 | + |
|
186 | + /** |
|
187 | + * Gets all the line items of object type Ticket |
|
188 | + * |
|
189 | + * @access public |
|
190 | + * @return \EE_Line_Item[] |
|
191 | + */ |
|
192 | + public function get_tickets() |
|
193 | + { |
|
194 | + if ($this->_grand_total === null) { |
|
195 | + return array(); |
|
196 | + } |
|
197 | + return EEH_Line_Item::get_ticket_line_items($this->_grand_total); |
|
198 | + } |
|
199 | + |
|
200 | + |
|
201 | + /** |
|
202 | + * returns the total quantity of tickets in the cart |
|
203 | + * |
|
204 | + * @access public |
|
205 | + * @return int |
|
206 | + * @throws \EE_Error |
|
207 | + */ |
|
208 | + public function all_ticket_quantity_count() |
|
209 | + { |
|
210 | + $tickets = $this->get_tickets(); |
|
211 | + if (empty($tickets)) { |
|
212 | + return 0; |
|
213 | + } |
|
214 | + $count = 0; |
|
215 | + foreach ($tickets as $ticket) { |
|
216 | + $count += $ticket->get('LIN_quantity'); |
|
217 | + } |
|
218 | + return $count; |
|
219 | + } |
|
220 | + |
|
221 | + |
|
222 | + /** |
|
223 | + * Gets all the tax line items |
|
224 | + * |
|
225 | + * @return \EE_Line_Item[] |
|
226 | + * @throws \EE_Error |
|
227 | + */ |
|
228 | + public function get_taxes() |
|
229 | + { |
|
230 | + return EEH_Line_Item::get_taxes_subtotal($this->_grand_total)->children(); |
|
231 | + } |
|
232 | + |
|
233 | + |
|
234 | + /** |
|
235 | + * Gets the total line item (which is a parent of all other line items) on this cart |
|
236 | + * |
|
237 | + * @return EE_Line_Item |
|
238 | + * @throws \EE_Error |
|
239 | + */ |
|
240 | + public function get_grand_total() |
|
241 | + { |
|
242 | + return $this->_grand_total instanceof EE_Line_Item ? $this->_grand_total : $this->_create_grand_total(); |
|
243 | + } |
|
244 | + |
|
245 | + |
|
246 | + /** |
|
247 | + * @process items for adding to cart |
|
248 | + * @access public |
|
249 | + * @param EE_Ticket $ticket |
|
250 | + * @param int $qty |
|
251 | + * @return TRUE on success, FALSE on fail |
|
252 | + * @throws \EE_Error |
|
253 | + */ |
|
254 | + public function add_ticket_to_cart(EE_Ticket $ticket, $qty = 1) |
|
255 | + { |
|
256 | + EEH_Line_Item::add_ticket_purchase($this->get_grand_total(), $ticket, $qty); |
|
257 | + return $this->save_cart() ? true : false; |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + /** |
|
262 | + * get_cart_total_before_tax |
|
263 | + * |
|
264 | + * @access public |
|
265 | + * @return float |
|
266 | + * @throws \EE_Error |
|
267 | + */ |
|
268 | + public function get_cart_total_before_tax() |
|
269 | + { |
|
270 | + return $this->get_grand_total()->recalculate_pre_tax_total(); |
|
271 | + } |
|
272 | + |
|
273 | + |
|
274 | + /** |
|
275 | + * gets the total amount of tax paid for items in this cart |
|
276 | + * |
|
277 | + * @access public |
|
278 | + * @return float |
|
279 | + * @throws \EE_Error |
|
280 | + */ |
|
281 | + public function get_applied_taxes() |
|
282 | + { |
|
283 | + return EEH_Line_Item::ensure_taxes_applied($this->_grand_total); |
|
284 | + } |
|
285 | + |
|
286 | + |
|
287 | + /** |
|
288 | + * Gets the total amount to be paid for the items in the cart, including taxes and other modifiers |
|
289 | + * |
|
290 | + * @access public |
|
291 | + * @return float |
|
292 | + * @throws \EE_Error |
|
293 | + */ |
|
294 | + public function get_cart_grand_total() |
|
295 | + { |
|
296 | + EEH_Line_Item::ensure_taxes_applied($this->_grand_total); |
|
297 | + return $this->get_grand_total()->total(); |
|
298 | + } |
|
299 | + |
|
300 | + |
|
301 | + /** |
|
302 | + * Gets the total amount to be paid for the items in the cart, including taxes and other modifiers |
|
303 | + * |
|
304 | + * @access public |
|
305 | + * @return float |
|
306 | + * @throws \EE_Error |
|
307 | + */ |
|
308 | + public function recalculate_all_cart_totals() |
|
309 | + { |
|
310 | + $pre_tax_total = $this->get_cart_total_before_tax(); |
|
311 | + $taxes_total = EEH_Line_Item::ensure_taxes_applied($this->_grand_total); |
|
312 | + $this->_grand_total->set_total($pre_tax_total + $taxes_total); |
|
313 | + $this->_grand_total->save_this_and_descendants_to_txn(); |
|
314 | + return $this->get_grand_total()->total(); |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + /** |
|
319 | + * deletes an item from the cart |
|
320 | + * |
|
321 | + * @access public |
|
322 | + * @param array|bool|string $line_item_codes |
|
323 | + * @return int on success, FALSE on fail |
|
324 | + * @throws \EE_Error |
|
325 | + */ |
|
326 | + public function delete_items($line_item_codes = false) |
|
327 | + { |
|
328 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
329 | + return EEH_Line_Item::delete_items($this->get_grand_total(), $line_item_codes); |
|
330 | + } |
|
331 | + |
|
332 | + |
|
333 | + /** |
|
334 | + * @remove ALL items from cart and zero ALL totals |
|
335 | + * @access public |
|
336 | + * @return bool |
|
337 | + * @throws \EE_Error |
|
338 | + */ |
|
339 | + public function empty_cart() |
|
340 | + { |
|
341 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
342 | + $this->_grand_total = $this->_create_grand_total(); |
|
343 | + return $this->save_cart(true); |
|
344 | + } |
|
345 | + |
|
346 | + |
|
347 | + /** |
|
348 | + * @remove ALL items from cart and delete total as well |
|
349 | + * @access public |
|
350 | + * @return bool |
|
351 | + * @throws \EE_Error |
|
352 | + */ |
|
353 | + public function delete_cart() |
|
354 | + { |
|
355 | + if ($this->_grand_total instanceof EE_Line_Item) { |
|
356 | + $deleted = EEH_Line_Item::delete_all_child_items($this->_grand_total); |
|
357 | + if ($deleted) { |
|
358 | + $deleted += $this->_grand_total->delete(); |
|
359 | + $this->_grand_total = null; |
|
360 | + return true; |
|
361 | + } |
|
362 | + } |
|
363 | + return false; |
|
364 | + } |
|
365 | + |
|
366 | + |
|
367 | + /** |
|
368 | + * @save cart to session |
|
369 | + * @access public |
|
370 | + * @param bool $apply_taxes |
|
371 | + * @return TRUE on success, FALSE on fail |
|
372 | + * @throws \EE_Error |
|
373 | + */ |
|
374 | + public function save_cart($apply_taxes = true) |
|
375 | + { |
|
376 | + if ($apply_taxes && $this->_grand_total instanceof EE_Line_Item) { |
|
377 | + EEH_Line_Item::ensure_taxes_applied($this->_grand_total); |
|
378 | + // make sure we don't cache the transaction because it can get stale |
|
379 | + if ( |
|
380 | + $this->_grand_total->get_one_from_cache('Transaction') instanceof EE_Transaction |
|
381 | + && $this->_grand_total->get_one_from_cache('Transaction')->ID() |
|
382 | + ) { |
|
383 | + $this->_grand_total->clear_cache('Transaction', null, true); |
|
384 | + } |
|
385 | + } |
|
386 | + if ($this->session() instanceof EE_Session) { |
|
387 | + return $this->session()->set_cart($this); |
|
388 | + } else { |
|
389 | + return false; |
|
390 | + } |
|
391 | + } |
|
392 | + |
|
393 | + |
|
394 | + public function __wakeup() |
|
395 | + { |
|
396 | + if (! $this->_grand_total instanceof EE_Line_Item && absint($this->_grand_total) !== 0) { |
|
397 | + // $this->_grand_total is actually just an ID, so use it to get the object from the db |
|
398 | + $this->_grand_total = EEM_Line_Item::instance()->get_one_by_ID($this->_grand_total); |
|
399 | + } |
|
400 | + } |
|
401 | + |
|
402 | + |
|
403 | + /** |
|
404 | + * @return array |
|
405 | + */ |
|
406 | + public function __sleep() |
|
407 | + { |
|
408 | + if ($this->_grand_total instanceof EE_Line_Item && $this->_grand_total->ID()) { |
|
409 | + $this->_grand_total = $this->_grand_total->ID(); |
|
410 | + } |
|
411 | + return array('_grand_total'); |
|
412 | + } |
|
413 | 413 | } |
@@ -13,27 +13,27 @@ |
||
13 | 13 | { |
14 | 14 | |
15 | 15 | |
16 | - /** |
|
17 | - * @deprecated |
|
18 | - * @param EE_Request $request |
|
19 | - * @param EE_Response $response |
|
20 | - * @return EE_Response |
|
21 | - */ |
|
22 | - public function handle_request(EE_Request $request, EE_Response $response) |
|
23 | - { |
|
24 | - EE_Error::doing_it_wrong( |
|
25 | - __METHOD__, |
|
26 | - sprintf( |
|
27 | - esc_html__( |
|
28 | - 'This class is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace', |
|
29 | - 'event_espresso' |
|
30 | - ), |
|
31 | - 'EventEspresso\core\services\request\middleware\DetectLogin', |
|
32 | - '\core\services\request', |
|
33 | - 'EventEspresso\core\services\request' |
|
34 | - ), |
|
35 | - '4.9.52' |
|
36 | - ); |
|
37 | - return $response; |
|
38 | - } |
|
16 | + /** |
|
17 | + * @deprecated |
|
18 | + * @param EE_Request $request |
|
19 | + * @param EE_Response $response |
|
20 | + * @return EE_Response |
|
21 | + */ |
|
22 | + public function handle_request(EE_Request $request, EE_Response $response) |
|
23 | + { |
|
24 | + EE_Error::doing_it_wrong( |
|
25 | + __METHOD__, |
|
26 | + sprintf( |
|
27 | + esc_html__( |
|
28 | + 'This class is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace', |
|
29 | + 'event_espresso' |
|
30 | + ), |
|
31 | + 'EventEspresso\core\services\request\middleware\DetectLogin', |
|
32 | + '\core\services\request', |
|
33 | + 'EventEspresso\core\services\request' |
|
34 | + ), |
|
35 | + '4.9.52' |
|
36 | + ); |
|
37 | + return $response; |
|
38 | + } |
|
39 | 39 | } |
@@ -17,75 +17,75 @@ |
||
17 | 17 | { |
18 | 18 | |
19 | 19 | |
20 | - /** |
|
21 | - * @deprecated |
|
22 | - * @param EE_Request $request |
|
23 | - * @param EE_Response $response |
|
24 | - * @return EE_Response |
|
25 | - */ |
|
26 | - public function handle_request(EE_Request $request, EE_Response $response) |
|
27 | - { |
|
28 | - EE_Error::doing_it_wrong( |
|
29 | - __METHOD__, |
|
30 | - sprintf( |
|
31 | - esc_html__( |
|
32 | - 'This class is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace', |
|
33 | - 'event_espresso' |
|
34 | - ), |
|
35 | - 'EventEspresso\core\services\request\middleware\RecommendedVersions', |
|
36 | - '\core\services\request', |
|
37 | - 'EventEspresso\core\services\request' |
|
38 | - ), |
|
39 | - '4.9.52' |
|
40 | - ); |
|
41 | - return $response; |
|
42 | - } |
|
20 | + /** |
|
21 | + * @deprecated |
|
22 | + * @param EE_Request $request |
|
23 | + * @param EE_Response $response |
|
24 | + * @return EE_Response |
|
25 | + */ |
|
26 | + public function handle_request(EE_Request $request, EE_Response $response) |
|
27 | + { |
|
28 | + EE_Error::doing_it_wrong( |
|
29 | + __METHOD__, |
|
30 | + sprintf( |
|
31 | + esc_html__( |
|
32 | + 'This class is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace', |
|
33 | + 'event_espresso' |
|
34 | + ), |
|
35 | + 'EventEspresso\core\services\request\middleware\RecommendedVersions', |
|
36 | + '\core\services\request', |
|
37 | + 'EventEspresso\core\services\request' |
|
38 | + ), |
|
39 | + '4.9.52' |
|
40 | + ); |
|
41 | + return $response; |
|
42 | + } |
|
43 | 43 | |
44 | 44 | |
45 | - /** |
|
46 | - * @deprecated |
|
47 | - * @param string $version_to_check |
|
48 | - * @param string $operator |
|
49 | - * @return bool |
|
50 | - */ |
|
51 | - public static function check_wp_version($version_to_check = EE_MIN_WP_VER_REQUIRED, $operator = '>=') |
|
52 | - { |
|
53 | - EE_Error::doing_it_wrong( |
|
54 | - __METHOD__, |
|
55 | - sprintf( |
|
56 | - esc_html__( |
|
57 | - 'This method is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace', |
|
58 | - 'event_espresso' |
|
59 | - ), |
|
60 | - 'EventEspresso\core\services\request\middleware\RecommendedVersions::compareWordPressVersion()', |
|
61 | - '\core\services\request', |
|
62 | - 'EventEspresso\core\services\request' |
|
63 | - ), |
|
64 | - '4.9.52', |
|
65 | - '5.0.0' |
|
66 | - ); |
|
67 | - return RecommendedVersions::compareWordPressVersion($version_to_check, $operator); |
|
68 | - } |
|
45 | + /** |
|
46 | + * @deprecated |
|
47 | + * @param string $version_to_check |
|
48 | + * @param string $operator |
|
49 | + * @return bool |
|
50 | + */ |
|
51 | + public static function check_wp_version($version_to_check = EE_MIN_WP_VER_REQUIRED, $operator = '>=') |
|
52 | + { |
|
53 | + EE_Error::doing_it_wrong( |
|
54 | + __METHOD__, |
|
55 | + sprintf( |
|
56 | + esc_html__( |
|
57 | + 'This method is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace', |
|
58 | + 'event_espresso' |
|
59 | + ), |
|
60 | + 'EventEspresso\core\services\request\middleware\RecommendedVersions::compareWordPressVersion()', |
|
61 | + '\core\services\request', |
|
62 | + 'EventEspresso\core\services\request' |
|
63 | + ), |
|
64 | + '4.9.52', |
|
65 | + '5.0.0' |
|
66 | + ); |
|
67 | + return RecommendedVersions::compareWordPressVersion($version_to_check, $operator); |
|
68 | + } |
|
69 | 69 | |
70 | 70 | |
71 | - /** |
|
72 | - * @deprecated |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - public function minimum_wp_version_error() |
|
76 | - { |
|
77 | - EE_Error::doing_it_wrong( |
|
78 | - __METHOD__, |
|
79 | - sprintf( |
|
80 | - esc_html__( |
|
81 | - 'This method is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace', |
|
82 | - 'event_espresso' |
|
83 | - ), |
|
84 | - 'EventEspresso\core\services\request\middleware\RecommendedVersions::minimumWpVersionError()', |
|
85 | - '\core\services\request', |
|
86 | - 'EventEspresso\core\services\request' |
|
87 | - ), |
|
88 | - '4.9.52' |
|
89 | - ); |
|
90 | - } |
|
71 | + /** |
|
72 | + * @deprecated |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + public function minimum_wp_version_error() |
|
76 | + { |
|
77 | + EE_Error::doing_it_wrong( |
|
78 | + __METHOD__, |
|
79 | + sprintf( |
|
80 | + esc_html__( |
|
81 | + 'This method is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace', |
|
82 | + 'event_espresso' |
|
83 | + ), |
|
84 | + 'EventEspresso\core\services\request\middleware\RecommendedVersions::minimumWpVersionError()', |
|
85 | + '\core\services\request', |
|
86 | + 'EventEspresso\core\services\request' |
|
87 | + ), |
|
88 | + '4.9.52' |
|
89 | + ); |
|
90 | + } |
|
91 | 91 | } |