@@ -14,303 +14,303 @@ |
||
14 | 14 | { |
15 | 15 | |
16 | 16 | |
17 | - /** |
|
18 | - * The Foreign key on the model that acts as the PRIMARY KEY used in special autosave handling where we query for |
|
19 | - * autosaves (or the Foreign key on other models in relations pointing to this models primary key which is this |
|
20 | - * value). The _primary_cpt_field is what is equivalent to the post_id field on a cpt join. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - private $_primary_cpt_field; |
|
25 | - |
|
26 | - |
|
27 | - /** |
|
28 | - * This is what field serves as the "parent" column that is linked with whatever the main model's calling this |
|
29 | - * relation has as a primary key. In other words EEM_Event has 'Datetime' => new |
|
30 | - * EE_Has_Many_Revision_Relation('EVT_ID', 'DTT_parent'). That means that in the EEM_Datetime model the |
|
31 | - * 'DTT_Parent' field is related to the 'DTT_ID' primary key field (in the same model) because 'DTT_ID' is the |
|
32 | - * primary key in the other model (EEM_Datetime). |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - private $_parent_pk_relation_field; |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the |
|
41 | - * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but |
|
42 | - * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get |
|
43 | - * related models across the relation, and add-and-remove the relationships. |
|
44 | - * |
|
45 | - * @param string $primary_cpt_field See property description for details |
|
46 | - * @param string $parent_pk_relation_field This is the field that is "connected" to the $primary_cpt_field. |
|
47 | - * See property desc for details. |
|
48 | - * @param boolean $block_deletes For this type of relation, we block by default. If there are |
|
49 | - * related models across this relation, block (prevent and add an |
|
50 | - * error) the deletion of this model |
|
51 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
52 | - * default |
|
53 | - */ |
|
54 | - public function __construct( |
|
55 | - $primary_cpt_field, |
|
56 | - $parent_pk_relation_field, |
|
57 | - $block_deletes = true, |
|
58 | - $blocking_delete_error_message = null |
|
59 | - ) { |
|
60 | - $this->_primary_cpt_field = $primary_cpt_field; |
|
61 | - $this->_parent_pk_relation_field = $parent_pk_relation_field; |
|
62 | - parent::__construct($block_deletes, $blocking_delete_error_message); |
|
63 | - } |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
68 | - * you like. |
|
69 | - * |
|
70 | - * @param EE_Base_Class|int $this_obj_or_id |
|
71 | - * @param EE_Base_Class|int $other_obj_or_id |
|
72 | - * @param array $extra_join_model_fields_n_values |
|
73 | - * @return \EE_Base_Class |
|
74 | - * @throws \EE_Error |
|
75 | - */ |
|
76 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
77 | - { |
|
78 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
79 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
80 | - |
|
81 | - //handle possible revisions |
|
82 | - $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj); |
|
83 | - |
|
84 | - //if is array, then we've already done the add_relation so let's get out |
|
85 | - if (is_array($other_model_obj)) { |
|
86 | - return $other_model_obj[0]; |
|
87 | - } |
|
88 | - //find the field on the other model which is a foreign key to this model |
|
89 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
90 | - //set that field on the other model to this model's ID |
|
91 | - $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
92 | - $other_model_obj->save(); |
|
93 | - return $other_model_obj; |
|
94 | - } |
|
95 | - |
|
96 | - |
|
97 | - /** |
|
98 | - * Sets the other model object's foreign key to its default, instead of pointing to this model object |
|
99 | - * |
|
100 | - * @param EE_Base_Class|int $this_obj_or_id |
|
101 | - * @param EE_Base_Class|int $other_obj_or_id |
|
102 | - * @param array $where_query |
|
103 | - * @return \EE_Base_Class |
|
104 | - * @throws \EE_Error |
|
105 | - */ |
|
106 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
107 | - { |
|
108 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id); |
|
109 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
110 | - //handle possible revisions |
|
111 | - $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj, true); |
|
112 | - |
|
113 | - |
|
114 | - //if is array, then we've already done the add_relation so let's get out |
|
115 | - if (is_array($other_model_obj)) { |
|
116 | - return $other_model_obj[0]; |
|
117 | - } |
|
118 | - |
|
119 | - //find the field on the other model which is a foreign key to this model |
|
120 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
121 | - |
|
122 | - |
|
123 | - //set that field on the other model to this model's ID |
|
124 | - if ($this->_blocking_delete) { |
|
125 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
126 | - $other_model_obj->save(); |
|
127 | - } else { |
|
128 | - $other_model_obj->delete(); |
|
129 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
130 | - return $other_model_obj; |
|
131 | - } |
|
132 | - return $other_model_obj; |
|
133 | - } |
|
134 | - |
|
135 | - |
|
136 | - /** |
|
137 | - * This is identical to EE_Model_Relation->get_all_related() except we're going handle special autosave conditions |
|
138 | - * in here. |
|
139 | - * |
|
140 | - * @param EE_Base_Class|int $model_object_or_id |
|
141 | - * @param array $query_params like EEM_Base::get_all's $query_params |
|
142 | - * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
143 | - * @return EE_Base_Class[] |
|
144 | - * @throws \EE_Error |
|
145 | - */ |
|
146 | - public function get_all_related( |
|
147 | - $model_object_or_id, |
|
148 | - $query_params = array(), |
|
149 | - $values_already_prepared_by_model_object = false |
|
150 | - ) { |
|
151 | - if ($values_already_prepared_by_model_object !== false) { |
|
152 | - EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
153 | - __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
154 | - '4.8.1'); |
|
155 | - } |
|
156 | - |
|
157 | - //if this is an autosave then we're going to get things differently |
|
158 | - if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
159 | - return $this->_do_autosave_get_all($model_object_or_id, $query_params); |
|
160 | - } |
|
161 | - |
|
162 | - return parent::get_all_related($model_object_or_id, $query_params); |
|
163 | - } |
|
164 | - |
|
165 | - |
|
166 | - /** |
|
167 | - * If we're in the midst of an autosave then we're going to do things a bit differently than the usual |
|
168 | - * get_all_related (commenting within). For description of params see the get_all_related() comments |
|
169 | - * |
|
170 | - * @access protected |
|
171 | - * @param $model_object_or_id |
|
172 | - * @param $query_params |
|
173 | - * @param bool $deprecated |
|
174 | - * @return \EE_Base_Class[] |
|
175 | - * @throws \EE_Error |
|
176 | - */ |
|
177 | - protected function _do_autosave_get_all($model_object_or_id, $query_params, $deprecated = false) |
|
178 | - { |
|
179 | - |
|
180 | - //first we check if the post_id for the incoming query is for an autosave. If it isn't that's what we want! |
|
181 | - $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
182 | - |
|
183 | - $autosave = wp_get_post_autosave($model_object_id); |
|
184 | - $id_to_use = $autosave ? $autosave->ID : $model_object_id; |
|
185 | - |
|
186 | - $autosave_relations = parent::get_all_related($id_to_use, $query_params); |
|
187 | - $parent_ids = $parents = array(); |
|
188 | - $return_objs = array(); |
|
189 | - |
|
190 | - //k this is where things differ because NOW what we're going to do is get the PARENTS for the get all related (and we'll also start setting up the return_objs array containing related that DON'T have parent ids, for those that DON'T have parents to merge with our returned objects); |
|
191 | - foreach ($autosave_relations as $a_r) { |
|
192 | - $pid = $a_r->parent(); |
|
193 | - if (! empty($pid)) { |
|
194 | - $parent_ids[] = $pid; |
|
195 | - } else { |
|
196 | - $return_objs[] = $a_r; |
|
197 | - } |
|
198 | - } |
|
199 | - |
|
200 | - //we have to make sure we also include the ORIGINAL values |
|
201 | - $originals = parent::get_all_related($model_object_or_id, $query_params); |
|
202 | - |
|
203 | - //merge $originals with $return_objs |
|
204 | - if ($originals) { |
|
205 | - $return_objs = array_merge($originals, $return_objs); |
|
206 | - } |
|
207 | - |
|
208 | - //now we setup the query to get all the parents |
|
209 | - if (! empty($parent_ids)) { |
|
210 | - $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() . "." . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
211 | - $query_param[0][$query_param_where_this_model_pk] = array('IN', $parent_ids); |
|
212 | - $parents = $this->get_other_model()->get_all($query_params); |
|
213 | - } |
|
214 | - |
|
215 | - //var_dump($parents); |
|
216 | - |
|
217 | - |
|
218 | - //now merge parents with our current $return_objs and send back |
|
219 | - return array_merge($parents, $return_objs); |
|
220 | - } |
|
221 | - |
|
222 | - |
|
223 | - /** |
|
224 | - * Basically this method gets called to verify if the incoming object needs to be manipulated somewhat because it |
|
225 | - * is a revision save. If so, then we change things before sending back. We also do verifications when this IS |
|
226 | - * NOT an revision because we always need to make sure that the autosave/revision has parent recorded (which is |
|
227 | - * sometime delayed if the object is created/saved first by the autosave) |
|
228 | - * |
|
229 | - * @param EE_Base_Class $this_obj |
|
230 | - * @param EE_Base_Class $other_obj |
|
231 | - * @param boolean $remove_relation Indicates whether we're doing a remove_relation or add_relation. |
|
232 | - * @return EE_Base_Class. ($other_obj); |
|
233 | - * @throws \EE_Error |
|
234 | - */ |
|
235 | - protected function _check_for_revision($this_obj, $other_obj, $remove_relation = false) |
|
236 | - { |
|
237 | - $pk_on_related_model = $this->get_other_model()->get_primary_key_field()->get_name(); |
|
238 | - //now we need to determine if we're in a WP revision save cause if we are we need to do some special handling |
|
239 | - if ($this_obj->post_type() === 'revision') { |
|
240 | - //first if $other_obj fk = this_obj pk then we know that this is a pk object, let's make sure there is a matching set for the autosave if there is then we save over it, if there isn't then we need to create a new one. |
|
241 | - $parent_evt_id = $this_obj->parent(); |
|
242 | - /*var_dump($parent_evt_id); |
|
17 | + /** |
|
18 | + * The Foreign key on the model that acts as the PRIMARY KEY used in special autosave handling where we query for |
|
19 | + * autosaves (or the Foreign key on other models in relations pointing to this models primary key which is this |
|
20 | + * value). The _primary_cpt_field is what is equivalent to the post_id field on a cpt join. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + private $_primary_cpt_field; |
|
25 | + |
|
26 | + |
|
27 | + /** |
|
28 | + * This is what field serves as the "parent" column that is linked with whatever the main model's calling this |
|
29 | + * relation has as a primary key. In other words EEM_Event has 'Datetime' => new |
|
30 | + * EE_Has_Many_Revision_Relation('EVT_ID', 'DTT_parent'). That means that in the EEM_Datetime model the |
|
31 | + * 'DTT_Parent' field is related to the 'DTT_ID' primary key field (in the same model) because 'DTT_ID' is the |
|
32 | + * primary key in the other model (EEM_Datetime). |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + private $_parent_pk_relation_field; |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the |
|
41 | + * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but |
|
42 | + * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get |
|
43 | + * related models across the relation, and add-and-remove the relationships. |
|
44 | + * |
|
45 | + * @param string $primary_cpt_field See property description for details |
|
46 | + * @param string $parent_pk_relation_field This is the field that is "connected" to the $primary_cpt_field. |
|
47 | + * See property desc for details. |
|
48 | + * @param boolean $block_deletes For this type of relation, we block by default. If there are |
|
49 | + * related models across this relation, block (prevent and add an |
|
50 | + * error) the deletion of this model |
|
51 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
52 | + * default |
|
53 | + */ |
|
54 | + public function __construct( |
|
55 | + $primary_cpt_field, |
|
56 | + $parent_pk_relation_field, |
|
57 | + $block_deletes = true, |
|
58 | + $blocking_delete_error_message = null |
|
59 | + ) { |
|
60 | + $this->_primary_cpt_field = $primary_cpt_field; |
|
61 | + $this->_parent_pk_relation_field = $parent_pk_relation_field; |
|
62 | + parent::__construct($block_deletes, $blocking_delete_error_message); |
|
63 | + } |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
68 | + * you like. |
|
69 | + * |
|
70 | + * @param EE_Base_Class|int $this_obj_or_id |
|
71 | + * @param EE_Base_Class|int $other_obj_or_id |
|
72 | + * @param array $extra_join_model_fields_n_values |
|
73 | + * @return \EE_Base_Class |
|
74 | + * @throws \EE_Error |
|
75 | + */ |
|
76 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
77 | + { |
|
78 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
79 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
80 | + |
|
81 | + //handle possible revisions |
|
82 | + $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj); |
|
83 | + |
|
84 | + //if is array, then we've already done the add_relation so let's get out |
|
85 | + if (is_array($other_model_obj)) { |
|
86 | + return $other_model_obj[0]; |
|
87 | + } |
|
88 | + //find the field on the other model which is a foreign key to this model |
|
89 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
90 | + //set that field on the other model to this model's ID |
|
91 | + $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
92 | + $other_model_obj->save(); |
|
93 | + return $other_model_obj; |
|
94 | + } |
|
95 | + |
|
96 | + |
|
97 | + /** |
|
98 | + * Sets the other model object's foreign key to its default, instead of pointing to this model object |
|
99 | + * |
|
100 | + * @param EE_Base_Class|int $this_obj_or_id |
|
101 | + * @param EE_Base_Class|int $other_obj_or_id |
|
102 | + * @param array $where_query |
|
103 | + * @return \EE_Base_Class |
|
104 | + * @throws \EE_Error |
|
105 | + */ |
|
106 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
107 | + { |
|
108 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id); |
|
109 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
110 | + //handle possible revisions |
|
111 | + $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj, true); |
|
112 | + |
|
113 | + |
|
114 | + //if is array, then we've already done the add_relation so let's get out |
|
115 | + if (is_array($other_model_obj)) { |
|
116 | + return $other_model_obj[0]; |
|
117 | + } |
|
118 | + |
|
119 | + //find the field on the other model which is a foreign key to this model |
|
120 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
121 | + |
|
122 | + |
|
123 | + //set that field on the other model to this model's ID |
|
124 | + if ($this->_blocking_delete) { |
|
125 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
126 | + $other_model_obj->save(); |
|
127 | + } else { |
|
128 | + $other_model_obj->delete(); |
|
129 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
130 | + return $other_model_obj; |
|
131 | + } |
|
132 | + return $other_model_obj; |
|
133 | + } |
|
134 | + |
|
135 | + |
|
136 | + /** |
|
137 | + * This is identical to EE_Model_Relation->get_all_related() except we're going handle special autosave conditions |
|
138 | + * in here. |
|
139 | + * |
|
140 | + * @param EE_Base_Class|int $model_object_or_id |
|
141 | + * @param array $query_params like EEM_Base::get_all's $query_params |
|
142 | + * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
143 | + * @return EE_Base_Class[] |
|
144 | + * @throws \EE_Error |
|
145 | + */ |
|
146 | + public function get_all_related( |
|
147 | + $model_object_or_id, |
|
148 | + $query_params = array(), |
|
149 | + $values_already_prepared_by_model_object = false |
|
150 | + ) { |
|
151 | + if ($values_already_prepared_by_model_object !== false) { |
|
152 | + EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
153 | + __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
154 | + '4.8.1'); |
|
155 | + } |
|
156 | + |
|
157 | + //if this is an autosave then we're going to get things differently |
|
158 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
159 | + return $this->_do_autosave_get_all($model_object_or_id, $query_params); |
|
160 | + } |
|
161 | + |
|
162 | + return parent::get_all_related($model_object_or_id, $query_params); |
|
163 | + } |
|
164 | + |
|
165 | + |
|
166 | + /** |
|
167 | + * If we're in the midst of an autosave then we're going to do things a bit differently than the usual |
|
168 | + * get_all_related (commenting within). For description of params see the get_all_related() comments |
|
169 | + * |
|
170 | + * @access protected |
|
171 | + * @param $model_object_or_id |
|
172 | + * @param $query_params |
|
173 | + * @param bool $deprecated |
|
174 | + * @return \EE_Base_Class[] |
|
175 | + * @throws \EE_Error |
|
176 | + */ |
|
177 | + protected function _do_autosave_get_all($model_object_or_id, $query_params, $deprecated = false) |
|
178 | + { |
|
179 | + |
|
180 | + //first we check if the post_id for the incoming query is for an autosave. If it isn't that's what we want! |
|
181 | + $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
182 | + |
|
183 | + $autosave = wp_get_post_autosave($model_object_id); |
|
184 | + $id_to_use = $autosave ? $autosave->ID : $model_object_id; |
|
185 | + |
|
186 | + $autosave_relations = parent::get_all_related($id_to_use, $query_params); |
|
187 | + $parent_ids = $parents = array(); |
|
188 | + $return_objs = array(); |
|
189 | + |
|
190 | + //k this is where things differ because NOW what we're going to do is get the PARENTS for the get all related (and we'll also start setting up the return_objs array containing related that DON'T have parent ids, for those that DON'T have parents to merge with our returned objects); |
|
191 | + foreach ($autosave_relations as $a_r) { |
|
192 | + $pid = $a_r->parent(); |
|
193 | + if (! empty($pid)) { |
|
194 | + $parent_ids[] = $pid; |
|
195 | + } else { |
|
196 | + $return_objs[] = $a_r; |
|
197 | + } |
|
198 | + } |
|
199 | + |
|
200 | + //we have to make sure we also include the ORIGINAL values |
|
201 | + $originals = parent::get_all_related($model_object_or_id, $query_params); |
|
202 | + |
|
203 | + //merge $originals with $return_objs |
|
204 | + if ($originals) { |
|
205 | + $return_objs = array_merge($originals, $return_objs); |
|
206 | + } |
|
207 | + |
|
208 | + //now we setup the query to get all the parents |
|
209 | + if (! empty($parent_ids)) { |
|
210 | + $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() . "." . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
211 | + $query_param[0][$query_param_where_this_model_pk] = array('IN', $parent_ids); |
|
212 | + $parents = $this->get_other_model()->get_all($query_params); |
|
213 | + } |
|
214 | + |
|
215 | + //var_dump($parents); |
|
216 | + |
|
217 | + |
|
218 | + //now merge parents with our current $return_objs and send back |
|
219 | + return array_merge($parents, $return_objs); |
|
220 | + } |
|
221 | + |
|
222 | + |
|
223 | + /** |
|
224 | + * Basically this method gets called to verify if the incoming object needs to be manipulated somewhat because it |
|
225 | + * is a revision save. If so, then we change things before sending back. We also do verifications when this IS |
|
226 | + * NOT an revision because we always need to make sure that the autosave/revision has parent recorded (which is |
|
227 | + * sometime delayed if the object is created/saved first by the autosave) |
|
228 | + * |
|
229 | + * @param EE_Base_Class $this_obj |
|
230 | + * @param EE_Base_Class $other_obj |
|
231 | + * @param boolean $remove_relation Indicates whether we're doing a remove_relation or add_relation. |
|
232 | + * @return EE_Base_Class. ($other_obj); |
|
233 | + * @throws \EE_Error |
|
234 | + */ |
|
235 | + protected function _check_for_revision($this_obj, $other_obj, $remove_relation = false) |
|
236 | + { |
|
237 | + $pk_on_related_model = $this->get_other_model()->get_primary_key_field()->get_name(); |
|
238 | + //now we need to determine if we're in a WP revision save cause if we are we need to do some special handling |
|
239 | + if ($this_obj->post_type() === 'revision') { |
|
240 | + //first if $other_obj fk = this_obj pk then we know that this is a pk object, let's make sure there is a matching set for the autosave if there is then we save over it, if there isn't then we need to create a new one. |
|
241 | + $parent_evt_id = $this_obj->parent(); |
|
242 | + /*var_dump($parent_evt_id); |
|
243 | 243 | var_dump($this_obj); |
244 | 244 | var_dump($other_obj);/**/ |
245 | 245 | |
246 | - if (! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
247 | - //let's do query on this objects model to see if the incoming pk value on the obj matches any parents in this objects table. |
|
248 | - $has_parent_obj = $this->get_other_model()->get_one(array( |
|
249 | - array( |
|
250 | - $this->_parent_pk_relation_field => $other_obj->ID(), |
|
251 | - $this->_primary_cpt_field => $this_obj->ID(), |
|
252 | - ), |
|
253 | - )); |
|
254 | - |
|
255 | - if ($has_parent_obj) { |
|
256 | - //this makes sure the update on the current obj happens to the revision's row NOT the parent row. |
|
257 | - |
|
258 | - $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
259 | - $other_obj->set($pk_on_related_model, $has_parent_obj->ID()); |
|
260 | - $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
261 | - |
|
262 | - if (! $remove_relation) { |
|
263 | - $other_obj->save(); |
|
264 | - return array($other_obj); |
|
265 | - } elseif ($remove_relation && ! $this->_blocking_delete) { |
|
266 | - $other_obj->delete(); |
|
267 | - $other_obj->set($this->_parent_pk_relation_field, null, true); |
|
268 | - return array($other_obj); |
|
269 | - } |
|
270 | - |
|
271 | - } else { |
|
272 | - $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
273 | - $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
274 | - $other_obj->set($pk_on_related_model, null, |
|
275 | - true); //ensure we create a new row for the autosave with parent id the same as the incoming ID. |
|
276 | - $other_obj->save(); //make sure we insert. |
|
277 | - return array($other_obj); |
|
278 | - } |
|
279 | - } |
|
280 | - |
|
281 | - //var_dump('what makes it here'); |
|
282 | - //var_dump($other_obj); |
|
283 | - //the next possible condition is that the incoming other_model obj has a NULL pk which means it just gets saved (which in turn creates it) |
|
284 | - |
|
285 | - //the last possible condition on a revision is that the incoming other_model object has a fk that == $this_obj pk which means we just return the $other obj and let it save as normal so we see the return at the bottom of this method. |
|
286 | - |
|
287 | - } else { |
|
288 | - |
|
289 | - //we only need to do the below IF this is not a remove relation |
|
290 | - if (! $remove_relation) { |
|
291 | - //okay this is is a normal update/save/remove so, let's make sure the other object is not a revision of the current object. |
|
292 | - //the other object will likely NOT have the correct fk on it (which is the primary_cpt_field_mame) so we must retrieve from the db to get that first. |
|
293 | - $existing_other_obj = $this->get_other_model()->get_one_by_ID($other_obj->ID()); |
|
294 | - $potential_revision_id = is_object($existing_other_obj) ? $existing_other_obj->get($this->_primary_cpt_field) : null; |
|
295 | - |
|
296 | - if ($parent_this_obj_id = wp_is_post_revision($potential_revision_id)) { |
|
297 | - //yes the OTHER object is linked to the revision of the parent, not the parent itself. That means we need to make the other_object an attachment of this_obj and then duplicate other_obj for the revision. |
|
298 | - $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
299 | - $other_obj->save(); |
|
300 | - |
|
301 | - //now create a new other_obj and fill with details from existing object |
|
302 | - $new_obj = $other_obj; |
|
303 | - $new_obj->set($this->_primary_cpt_field, $potential_revision_id); |
|
304 | - $new_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
305 | - $new_obj->set($pk_on_related_model, null); |
|
306 | - $new_obj->save(); |
|
307 | - return array($new_obj); |
|
308 | - } |
|
309 | - |
|
310 | - } |
|
311 | - } |
|
312 | - |
|
313 | - return $other_obj; |
|
314 | - } |
|
246 | + if (! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
247 | + //let's do query on this objects model to see if the incoming pk value on the obj matches any parents in this objects table. |
|
248 | + $has_parent_obj = $this->get_other_model()->get_one(array( |
|
249 | + array( |
|
250 | + $this->_parent_pk_relation_field => $other_obj->ID(), |
|
251 | + $this->_primary_cpt_field => $this_obj->ID(), |
|
252 | + ), |
|
253 | + )); |
|
254 | + |
|
255 | + if ($has_parent_obj) { |
|
256 | + //this makes sure the update on the current obj happens to the revision's row NOT the parent row. |
|
257 | + |
|
258 | + $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
259 | + $other_obj->set($pk_on_related_model, $has_parent_obj->ID()); |
|
260 | + $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
261 | + |
|
262 | + if (! $remove_relation) { |
|
263 | + $other_obj->save(); |
|
264 | + return array($other_obj); |
|
265 | + } elseif ($remove_relation && ! $this->_blocking_delete) { |
|
266 | + $other_obj->delete(); |
|
267 | + $other_obj->set($this->_parent_pk_relation_field, null, true); |
|
268 | + return array($other_obj); |
|
269 | + } |
|
270 | + |
|
271 | + } else { |
|
272 | + $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
273 | + $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
274 | + $other_obj->set($pk_on_related_model, null, |
|
275 | + true); //ensure we create a new row for the autosave with parent id the same as the incoming ID. |
|
276 | + $other_obj->save(); //make sure we insert. |
|
277 | + return array($other_obj); |
|
278 | + } |
|
279 | + } |
|
280 | + |
|
281 | + //var_dump('what makes it here'); |
|
282 | + //var_dump($other_obj); |
|
283 | + //the next possible condition is that the incoming other_model obj has a NULL pk which means it just gets saved (which in turn creates it) |
|
284 | + |
|
285 | + //the last possible condition on a revision is that the incoming other_model object has a fk that == $this_obj pk which means we just return the $other obj and let it save as normal so we see the return at the bottom of this method. |
|
286 | + |
|
287 | + } else { |
|
288 | + |
|
289 | + //we only need to do the below IF this is not a remove relation |
|
290 | + if (! $remove_relation) { |
|
291 | + //okay this is is a normal update/save/remove so, let's make sure the other object is not a revision of the current object. |
|
292 | + //the other object will likely NOT have the correct fk on it (which is the primary_cpt_field_mame) so we must retrieve from the db to get that first. |
|
293 | + $existing_other_obj = $this->get_other_model()->get_one_by_ID($other_obj->ID()); |
|
294 | + $potential_revision_id = is_object($existing_other_obj) ? $existing_other_obj->get($this->_primary_cpt_field) : null; |
|
295 | + |
|
296 | + if ($parent_this_obj_id = wp_is_post_revision($potential_revision_id)) { |
|
297 | + //yes the OTHER object is linked to the revision of the parent, not the parent itself. That means we need to make the other_object an attachment of this_obj and then duplicate other_obj for the revision. |
|
298 | + $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
299 | + $other_obj->save(); |
|
300 | + |
|
301 | + //now create a new other_obj and fill with details from existing object |
|
302 | + $new_obj = $other_obj; |
|
303 | + $new_obj->set($this->_primary_cpt_field, $potential_revision_id); |
|
304 | + $new_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
305 | + $new_obj->set($pk_on_related_model, null); |
|
306 | + $new_obj->save(); |
|
307 | + return array($new_obj); |
|
308 | + } |
|
309 | + |
|
310 | + } |
|
311 | + } |
|
312 | + |
|
313 | + return $other_obj; |
|
314 | + } |
|
315 | 315 | |
316 | 316 | } |
317 | 317 | \ No newline at end of file |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -require_once(EE_MODELS . 'relations/EE_Has_Many_Relation.php'); |
|
2 | +require_once(EE_MODELS.'relations/EE_Has_Many_Relation.php'); |
|
3 | 3 | |
4 | 4 | |
5 | 5 | /** |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | //k this is where things differ because NOW what we're going to do is get the PARENTS for the get all related (and we'll also start setting up the return_objs array containing related that DON'T have parent ids, for those that DON'T have parents to merge with our returned objects); |
191 | 191 | foreach ($autosave_relations as $a_r) { |
192 | 192 | $pid = $a_r->parent(); |
193 | - if (! empty($pid)) { |
|
193 | + if ( ! empty($pid)) { |
|
194 | 194 | $parent_ids[] = $pid; |
195 | 195 | } else { |
196 | 196 | $return_objs[] = $a_r; |
@@ -206,8 +206,8 @@ discard block |
||
206 | 206 | } |
207 | 207 | |
208 | 208 | //now we setup the query to get all the parents |
209 | - if (! empty($parent_ids)) { |
|
210 | - $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() . "." . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
209 | + if ( ! empty($parent_ids)) { |
|
210 | + $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name().".".$this->get_this_model()->get_primary_key_field()->get_name(); |
|
211 | 211 | $query_param[0][$query_param_where_this_model_pk] = array('IN', $parent_ids); |
212 | 212 | $parents = $this->get_other_model()->get_all($query_params); |
213 | 213 | } |
@@ -243,7 +243,7 @@ discard block |
||
243 | 243 | var_dump($this_obj); |
244 | 244 | var_dump($other_obj);/**/ |
245 | 245 | |
246 | - if (! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
246 | + if ( ! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
247 | 247 | //let's do query on this objects model to see if the incoming pk value on the obj matches any parents in this objects table. |
248 | 248 | $has_parent_obj = $this->get_other_model()->get_one(array( |
249 | 249 | array( |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | $other_obj->set($pk_on_related_model, $has_parent_obj->ID()); |
260 | 260 | $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
261 | 261 | |
262 | - if (! $remove_relation) { |
|
262 | + if ( ! $remove_relation) { |
|
263 | 263 | $other_obj->save(); |
264 | 264 | return array($other_obj); |
265 | 265 | } elseif ($remove_relation && ! $this->_blocking_delete) { |
@@ -287,7 +287,7 @@ discard block |
||
287 | 287 | } else { |
288 | 288 | |
289 | 289 | //we only need to do the below IF this is not a remove relation |
290 | - if (! $remove_relation) { |
|
290 | + if ( ! $remove_relation) { |
|
291 | 291 | //okay this is is a normal update/save/remove so, let's make sure the other object is not a revision of the current object. |
292 | 292 | //the other object will likely NOT have the correct fk on it (which is the primary_cpt_field_mame) so we must retrieve from the db to get that first. |
293 | 293 | $existing_other_obj = $this->get_other_model()->get_one_by_ID($other_obj->ID()); |
@@ -10,264 +10,264 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_HABTM_Any_Relation extends EE_HABTM_Relation |
12 | 12 | { |
13 | - /** |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $_alphabetically_first_model_name; |
|
13 | + /** |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $_alphabetically_first_model_name; |
|
17 | 17 | |
18 | - /** |
|
19 | - * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
20 | - * (and an ee joining-model.) This knows how to join the models, |
|
21 | - * get related models across the relation, and add-and-remove the relationships. |
|
22 | - * |
|
23 | - * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
24 | - * are related models across this relation, block (prevent and add an |
|
25 | - * error) the deletion of this model |
|
26 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
27 | - * default |
|
28 | - */ |
|
29 | - public function __construct($block_deletes = true, $blocking_delete_error_message = '') |
|
30 | - { |
|
31 | - parent::__construct('Extra_Join', $block_deletes, $blocking_delete_error_message); |
|
32 | - } |
|
18 | + /** |
|
19 | + * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
20 | + * (and an ee joining-model.) This knows how to join the models, |
|
21 | + * get related models across the relation, and add-and-remove the relationships. |
|
22 | + * |
|
23 | + * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
24 | + * are related models across this relation, block (prevent and add an |
|
25 | + * error) the deletion of this model |
|
26 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
27 | + * default |
|
28 | + */ |
|
29 | + public function __construct($block_deletes = true, $blocking_delete_error_message = '') |
|
30 | + { |
|
31 | + parent::__construct('Extra_Join', $block_deletes, $blocking_delete_error_message); |
|
32 | + } |
|
33 | 33 | |
34 | 34 | |
35 | - /** |
|
36 | - * @param $this_model_name |
|
37 | - * @param $other_model_name |
|
38 | - * @throws EE_Error |
|
39 | - */ |
|
40 | - public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
41 | - { |
|
42 | - if ($this_model_name < $other_model_name) { |
|
43 | - $this->_alphabetically_first_model_name = $this_model_name; |
|
44 | - } else { |
|
45 | - $this->_alphabetically_first_model_name = $other_model_name; |
|
46 | - } |
|
47 | - parent::_construct_finalize_set_models($this_model_name, $other_model_name); |
|
48 | - } |
|
35 | + /** |
|
36 | + * @param $this_model_name |
|
37 | + * @param $other_model_name |
|
38 | + * @throws EE_Error |
|
39 | + */ |
|
40 | + public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
41 | + { |
|
42 | + if ($this_model_name < $other_model_name) { |
|
43 | + $this->_alphabetically_first_model_name = $this_model_name; |
|
44 | + } else { |
|
45 | + $this->_alphabetically_first_model_name = $other_model_name; |
|
46 | + } |
|
47 | + parent::_construct_finalize_set_models($this_model_name, $other_model_name); |
|
48 | + } |
|
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * @param string $model_name |
|
53 | - * @param string $id_or_name_field should be the string 'ID' or 'name' only |
|
54 | - * @return EE_Model_Field_Base |
|
55 | - * @throws \EE_Error |
|
56 | - */ |
|
57 | - public function get_join_table_fk_field_to($model_name, $id_or_name_field) |
|
58 | - { |
|
59 | - $order = null; |
|
60 | - if ($model_name === $this->_alphabetically_first_model_name) { |
|
61 | - $order = 'first'; |
|
62 | - } else { |
|
63 | - $order = 'second'; |
|
64 | - } |
|
65 | - return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
66 | - } |
|
51 | + /** |
|
52 | + * @param string $model_name |
|
53 | + * @param string $id_or_name_field should be the string 'ID' or 'name' only |
|
54 | + * @return EE_Model_Field_Base |
|
55 | + * @throws \EE_Error |
|
56 | + */ |
|
57 | + public function get_join_table_fk_field_to($model_name, $id_or_name_field) |
|
58 | + { |
|
59 | + $order = null; |
|
60 | + if ($model_name === $this->_alphabetically_first_model_name) { |
|
61 | + $order = 'first'; |
|
62 | + } else { |
|
63 | + $order = 'second'; |
|
64 | + } |
|
65 | + return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
66 | + } |
|
67 | 67 | |
68 | 68 | |
69 | - /** |
|
70 | - * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
71 | - * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
72 | - * |
|
73 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
74 | - * @return string of SQL |
|
75 | - * @throws \EE_Error |
|
76 | - */ |
|
77 | - public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
78 | - { |
|
79 | - //create sql like |
|
80 | - //LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
81 | - //LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
82 | - //remember the model relation chain to the JOIN model, because we'll |
|
83 | - //need it for get_join_statement() |
|
84 | - $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
85 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
86 | - $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
87 | - $this->get_this_model()->get_this_model_name(), |
|
88 | - 'ID'); |
|
89 | - $field_with_model_name = $this->get_join_table_fk_field_to( |
|
90 | - $this->get_this_model()->get_this_model_name(), |
|
91 | - 'name'); |
|
92 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
93 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
94 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
95 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
96 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
97 | - //phew! ok, we have all the info we need, now we can create the SQL join string |
|
98 | - $SQL = $this->_left_join( |
|
99 | - $join_table, |
|
100 | - $join_table_alias, |
|
101 | - $join_table_fk_field_to_this_table->get_table_column(), |
|
102 | - $this_table_alias, |
|
103 | - $this_table_pk_field->get_table_column(), |
|
104 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") . |
|
105 | - $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
69 | + /** |
|
70 | + * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
71 | + * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
72 | + * |
|
73 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
74 | + * @return string of SQL |
|
75 | + * @throws \EE_Error |
|
76 | + */ |
|
77 | + public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
78 | + { |
|
79 | + //create sql like |
|
80 | + //LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
81 | + //LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
82 | + //remember the model relation chain to the JOIN model, because we'll |
|
83 | + //need it for get_join_statement() |
|
84 | + $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
85 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
86 | + $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
87 | + $this->get_this_model()->get_this_model_name(), |
|
88 | + 'ID'); |
|
89 | + $field_with_model_name = $this->get_join_table_fk_field_to( |
|
90 | + $this->get_this_model()->get_this_model_name(), |
|
91 | + 'name'); |
|
92 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
93 | + $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
94 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
95 | + $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
96 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
97 | + //phew! ok, we have all the info we need, now we can create the SQL join string |
|
98 | + $SQL = $this->_left_join( |
|
99 | + $join_table, |
|
100 | + $join_table_alias, |
|
101 | + $join_table_fk_field_to_this_table->get_table_column(), |
|
102 | + $this_table_alias, |
|
103 | + $this_table_pk_field->get_table_column(), |
|
104 | + $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") . |
|
105 | + $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
106 | 106 | |
107 | - return $SQL; |
|
108 | - } |
|
107 | + return $SQL; |
|
108 | + } |
|
109 | 109 | |
110 | 110 | |
111 | - /** |
|
112 | - * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
113 | - * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
114 | - * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
115 | - * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
116 | - * the intermediate join table, and then to the other model's pk's table |
|
117 | - * |
|
118 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
119 | - * @return string of SQL |
|
120 | - * @throws \EE_Error |
|
121 | - */ |
|
122 | - public function get_join_statement($model_relation_chain) |
|
123 | - { |
|
124 | - if ($this->_model_relation_chain_to_join_model === null) { |
|
125 | - throw new EE_Error(sprintf(__('When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
126 | - 'event_espresso'))); |
|
127 | - } |
|
128 | - $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
129 | - $this->get_this_model()->get_this_model_name(), |
|
130 | - 'ID'); |
|
131 | - $join_table_fk_field_to_other_table = $this->get_join_table_fk_field_to( |
|
132 | - $this->get_other_model()->get_this_model_name(), |
|
133 | - 'ID'); |
|
134 | - $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
135 | - $this->get_other_model()->get_this_model_name(), |
|
136 | - 'name'); |
|
111 | + /** |
|
112 | + * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
113 | + * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
114 | + * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
115 | + * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
116 | + * the intermediate join table, and then to the other model's pk's table |
|
117 | + * |
|
118 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
119 | + * @return string of SQL |
|
120 | + * @throws \EE_Error |
|
121 | + */ |
|
122 | + public function get_join_statement($model_relation_chain) |
|
123 | + { |
|
124 | + if ($this->_model_relation_chain_to_join_model === null) { |
|
125 | + throw new EE_Error(sprintf(__('When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
126 | + 'event_espresso'))); |
|
127 | + } |
|
128 | + $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
129 | + $this->get_this_model()->get_this_model_name(), |
|
130 | + 'ID'); |
|
131 | + $join_table_fk_field_to_other_table = $this->get_join_table_fk_field_to( |
|
132 | + $this->get_other_model()->get_this_model_name(), |
|
133 | + 'ID'); |
|
134 | + $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
135 | + $this->get_other_model()->get_this_model_name(), |
|
136 | + 'name'); |
|
137 | 137 | |
138 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
|
139 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
138 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
|
139 | + $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
140 | 140 | |
141 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
142 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
143 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
144 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
141 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
142 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
143 | + $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
144 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
145 | 145 | |
146 | - $SQL = $this->_left_join( |
|
147 | - $other_table, |
|
148 | - $other_table_alias, |
|
149 | - $other_table_pk_field->get_table_column(), |
|
150 | - $join_table_alias, |
|
151 | - $join_table_fk_field_to_other_table->get_table_column(), |
|
152 | - $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
153 | - ) . |
|
154 | - $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
155 | - return $SQL; |
|
156 | - } |
|
146 | + $SQL = $this->_left_join( |
|
147 | + $other_table, |
|
148 | + $other_table_alias, |
|
149 | + $other_table_pk_field->get_table_column(), |
|
150 | + $join_table_alias, |
|
151 | + $join_table_fk_field_to_other_table->get_table_column(), |
|
152 | + $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
153 | + ) . |
|
154 | + $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
155 | + return $SQL; |
|
156 | + } |
|
157 | 157 | |
158 | 158 | |
159 | - /** |
|
160 | - * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
161 | - * |
|
162 | - * @param EE_Base_Class|int $this_obj_or_id |
|
163 | - * @param EE_Base_Class|int $other_obj_or_id |
|
164 | - * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
165 | - * checking existing values and for setting new rows if |
|
166 | - * no exact matches. |
|
167 | - * @return EE_Base_Class |
|
168 | - * @throws \EE_Error |
|
169 | - */ |
|
170 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
171 | - { |
|
172 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
173 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
174 | - //check if such a relationship already exists |
|
175 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
176 | - $this->get_this_model()->get_this_model_name(), |
|
177 | - 'ID'); |
|
178 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
179 | - $this->get_this_model()->get_this_model_name(), |
|
180 | - 'name'); |
|
181 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
182 | - $this->get_other_model()->get_this_model_name(), |
|
183 | - 'ID'); |
|
184 | - $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
185 | - $this->get_other_model()->get_this_model_name(), |
|
186 | - 'name'); |
|
159 | + /** |
|
160 | + * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
161 | + * |
|
162 | + * @param EE_Base_Class|int $this_obj_or_id |
|
163 | + * @param EE_Base_Class|int $other_obj_or_id |
|
164 | + * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
165 | + * checking existing values and for setting new rows if |
|
166 | + * no exact matches. |
|
167 | + * @return EE_Base_Class |
|
168 | + * @throws \EE_Error |
|
169 | + */ |
|
170 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
171 | + { |
|
172 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
173 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
174 | + //check if such a relationship already exists |
|
175 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
176 | + $this->get_this_model()->get_this_model_name(), |
|
177 | + 'ID'); |
|
178 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
179 | + $this->get_this_model()->get_this_model_name(), |
|
180 | + 'name'); |
|
181 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
182 | + $this->get_other_model()->get_this_model_name(), |
|
183 | + 'ID'); |
|
184 | + $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
185 | + $this->get_other_model()->get_this_model_name(), |
|
186 | + 'name'); |
|
187 | 187 | |
188 | - $cols_n_values = array( |
|
189 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
190 | - $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
191 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
192 | - $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
193 | - ); |
|
188 | + $cols_n_values = array( |
|
189 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
190 | + $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
191 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
192 | + $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
193 | + ); |
|
194 | 194 | |
195 | - //if $where_query exists lets add them to the query_params. |
|
196 | - if (! empty($extra_join_model_fields_n_values)) { |
|
197 | - //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
198 | - //make sure we strip THIS models name from the query param |
|
199 | - $parsed_query = array(); |
|
200 | - foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
201 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
202 | - $query_param); |
|
203 | - $parsed_query[$query_param] = $val; |
|
204 | - } |
|
205 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
206 | - } |
|
195 | + //if $where_query exists lets add them to the query_params. |
|
196 | + if (! empty($extra_join_model_fields_n_values)) { |
|
197 | + //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
198 | + //make sure we strip THIS models name from the query param |
|
199 | + $parsed_query = array(); |
|
200 | + foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
201 | + $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
202 | + $query_param); |
|
203 | + $parsed_query[$query_param] = $val; |
|
204 | + } |
|
205 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
206 | + } |
|
207 | 207 | |
208 | - $query_params = array($cols_n_values); |
|
208 | + $query_params = array($cols_n_values); |
|
209 | 209 | |
210 | 210 | |
211 | - $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
212 | - //if there is already an entry in the join table, indicating a relationship, we're done |
|
213 | - //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
214 | - //the other tables, use the joining model directly! |
|
215 | - if (! $existing_entry_in_join_table) { |
|
216 | - $this->get_join_model()->insert($cols_n_values); |
|
217 | - } |
|
218 | - return $other_model_obj; |
|
219 | - } |
|
211 | + $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
212 | + //if there is already an entry in the join table, indicating a relationship, we're done |
|
213 | + //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
214 | + //the other tables, use the joining model directly! |
|
215 | + if (! $existing_entry_in_join_table) { |
|
216 | + $this->get_join_model()->insert($cols_n_values); |
|
217 | + } |
|
218 | + return $other_model_obj; |
|
219 | + } |
|
220 | 220 | |
221 | 221 | |
222 | - /** |
|
223 | - * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
224 | - * |
|
225 | - * @param EE_Base_Class|int $this_obj_or_id |
|
226 | - * @param EE_Base_Class|int $other_obj_or_id |
|
227 | - * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
228 | - * values and for removing existing rows if exact matches exist. |
|
229 | - * @return EE_Base_Class |
|
230 | - * @throws \EE_Error |
|
231 | - */ |
|
232 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
233 | - { |
|
234 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
235 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
236 | - //check if such a relationship already exists |
|
237 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
238 | - $this->get_this_model()->get_this_model_name(), |
|
239 | - 'ID'); |
|
240 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
241 | - $this->get_this_model()->get_this_model_name(), |
|
242 | - 'name'); |
|
243 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
244 | - $this->get_other_model()->get_this_model_name(), |
|
245 | - 'ID'); |
|
246 | - $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
247 | - $this->get_other_model()->get_this_model_name(), |
|
248 | - 'name'); |
|
222 | + /** |
|
223 | + * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
224 | + * |
|
225 | + * @param EE_Base_Class|int $this_obj_or_id |
|
226 | + * @param EE_Base_Class|int $other_obj_or_id |
|
227 | + * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
228 | + * values and for removing existing rows if exact matches exist. |
|
229 | + * @return EE_Base_Class |
|
230 | + * @throws \EE_Error |
|
231 | + */ |
|
232 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
233 | + { |
|
234 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
235 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
236 | + //check if such a relationship already exists |
|
237 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
238 | + $this->get_this_model()->get_this_model_name(), |
|
239 | + 'ID'); |
|
240 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
241 | + $this->get_this_model()->get_this_model_name(), |
|
242 | + 'name'); |
|
243 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
244 | + $this->get_other_model()->get_this_model_name(), |
|
245 | + 'ID'); |
|
246 | + $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
247 | + $this->get_other_model()->get_this_model_name(), |
|
248 | + 'name'); |
|
249 | 249 | |
250 | - $cols_n_values = array( |
|
251 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
252 | - $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
253 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
254 | - $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
255 | - ); |
|
250 | + $cols_n_values = array( |
|
251 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
252 | + $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
253 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
254 | + $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
255 | + ); |
|
256 | 256 | |
257 | - //if $where_query exists lets add them to the query_params. |
|
258 | - if (! empty($where_query)) { |
|
259 | - //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
260 | - //make sure we strip THIS models name from the query param |
|
261 | - $parsed_query = array(); |
|
262 | - foreach ($where_query as $query_param => $val) { |
|
263 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
264 | - $query_param); |
|
265 | - $parsed_query[$query_param] = $val; |
|
266 | - } |
|
267 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
268 | - } |
|
257 | + //if $where_query exists lets add them to the query_params. |
|
258 | + if (! empty($where_query)) { |
|
259 | + //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
260 | + //make sure we strip THIS models name from the query param |
|
261 | + $parsed_query = array(); |
|
262 | + foreach ($where_query as $query_param => $val) { |
|
263 | + $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
264 | + $query_param); |
|
265 | + $parsed_query[$query_param] = $val; |
|
266 | + } |
|
267 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
268 | + } |
|
269 | 269 | |
270 | - $this->get_join_model()->delete(array($cols_n_values)); |
|
271 | - return $other_model_obj; |
|
272 | - } |
|
270 | + $this->get_join_model()->delete(array($cols_n_values)); |
|
271 | + return $other_model_obj; |
|
272 | + } |
|
273 | 273 | } |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | } else { |
63 | 63 | $order = 'second'; |
64 | 64 | } |
65 | - return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
65 | + return $this->get_join_model()->field_settings_for('EXJ_'.$order.'_model_'.$id_or_name_field); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | |
@@ -90,9 +90,9 @@ discard block |
||
90 | 90 | $this->get_this_model()->get_this_model_name(), |
91 | 91 | 'name'); |
92 | 92 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
93 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
93 | + $this->get_this_model()->get_this_model_name()).$this_table_pk_field->get_table_alias(); |
|
94 | 94 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
95 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
95 | + $this->get_join_model()->get_this_model_name()).$join_table_fk_field_to_this_table->get_table_alias(); |
|
96 | 96 | $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
97 | 97 | //phew! ok, we have all the info we need, now we can create the SQL join string |
98 | 98 | $SQL = $this->_left_join( |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | $join_table_fk_field_to_this_table->get_table_column(), |
102 | 102 | $this_table_alias, |
103 | 103 | $this_table_pk_field->get_table_column(), |
104 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") . |
|
104 | + $field_with_model_name->get_qualified_column()."='".$this->get_this_model()->get_this_model_name()."'"). |
|
105 | 105 | $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
106 | 106 | |
107 | 107 | return $SQL; |
@@ -136,11 +136,11 @@ discard block |
||
136 | 136 | 'name'); |
137 | 137 | |
138 | 138 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
139 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
139 | + $this->get_join_model()->get_this_model_name()).$join_table_fk_field_to_this_table->get_table_alias(); |
|
140 | 140 | |
141 | 141 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
142 | 142 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
143 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
143 | + $this->get_other_model()->get_this_model_name()).$other_table_pk_field->get_table_alias(); |
|
144 | 144 | $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
145 | 145 | |
146 | 146 | $SQL = $this->_left_join( |
@@ -149,8 +149,8 @@ discard block |
||
149 | 149 | $other_table_pk_field->get_table_column(), |
150 | 150 | $join_table_alias, |
151 | 151 | $join_table_fk_field_to_other_table->get_table_column(), |
152 | - $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
153 | - ) . |
|
152 | + $field_with_other_model_name->get_qualified_column()."='".$this->get_other_model()->get_this_model_name()."'" |
|
153 | + ). |
|
154 | 154 | $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
155 | 155 | return $SQL; |
156 | 156 | } |
@@ -193,12 +193,12 @@ discard block |
||
193 | 193 | ); |
194 | 194 | |
195 | 195 | //if $where_query exists lets add them to the query_params. |
196 | - if (! empty($extra_join_model_fields_n_values)) { |
|
196 | + if ( ! empty($extra_join_model_fields_n_values)) { |
|
197 | 197 | //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
198 | 198 | //make sure we strip THIS models name from the query param |
199 | 199 | $parsed_query = array(); |
200 | 200 | foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
201 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
201 | + $query_param = str_replace($this->get_join_model()->get_this_model_name().".", "", |
|
202 | 202 | $query_param); |
203 | 203 | $parsed_query[$query_param] = $val; |
204 | 204 | } |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | //if there is already an entry in the join table, indicating a relationship, we're done |
213 | 213 | //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
214 | 214 | //the other tables, use the joining model directly! |
215 | - if (! $existing_entry_in_join_table) { |
|
215 | + if ( ! $existing_entry_in_join_table) { |
|
216 | 216 | $this->get_join_model()->insert($cols_n_values); |
217 | 217 | } |
218 | 218 | return $other_model_obj; |
@@ -255,12 +255,12 @@ discard block |
||
255 | 255 | ); |
256 | 256 | |
257 | 257 | //if $where_query exists lets add them to the query_params. |
258 | - if (! empty($where_query)) { |
|
258 | + if ( ! empty($where_query)) { |
|
259 | 259 | //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
260 | 260 | //make sure we strip THIS models name from the query param |
261 | 261 | $parsed_query = array(); |
262 | 262 | foreach ($where_query as $query_param => $val) { |
263 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
263 | + $query_param = str_replace($this->get_join_model()->get_this_model_name().".", "", |
|
264 | 264 | $query_param); |
265 | 265 | $parsed_query[$query_param] = $val; |
266 | 266 | } |
@@ -18,83 +18,83 @@ |
||
18 | 18 | { |
19 | 19 | |
20 | 20 | |
21 | - /** |
|
22 | - * get_join_statement |
|
23 | - * |
|
24 | - * @param string $model_relation_chain |
|
25 | - * @return string |
|
26 | - * @throws \EE_Error |
|
27 | - */ |
|
28 | - public function get_join_statement($model_relation_chain) |
|
29 | - { |
|
30 | - //create the sql string like |
|
31 | - $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
32 | - //ALSO, need to get the field with the model name |
|
33 | - $field_with_model_name = $this->get_this_model()->get_field_containing_related_model_name(); |
|
21 | + /** |
|
22 | + * get_join_statement |
|
23 | + * |
|
24 | + * @param string $model_relation_chain |
|
25 | + * @return string |
|
26 | + * @throws \EE_Error |
|
27 | + */ |
|
28 | + public function get_join_statement($model_relation_chain) |
|
29 | + { |
|
30 | + //create the sql string like |
|
31 | + $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
32 | + //ALSO, need to get the field with the model name |
|
33 | + $field_with_model_name = $this->get_this_model()->get_field_containing_related_model_name(); |
|
34 | 34 | |
35 | 35 | |
36 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
37 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
38 | - $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
39 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
40 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
41 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
42 | - return $this->_left_join($other_table, |
|
43 | - $other_table_alias, |
|
44 | - $other_table_pk_field->get_table_column(), |
|
45 | - $this_table_alias, |
|
46 | - $this_table_fk_field->get_table_column(), |
|
47 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'") |
|
48 | - . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
49 | - } |
|
36 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
37 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
38 | + $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
39 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
40 | + $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
41 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
42 | + return $this->_left_join($other_table, |
|
43 | + $other_table_alias, |
|
44 | + $other_table_pk_field->get_table_column(), |
|
45 | + $this_table_alias, |
|
46 | + $this_table_fk_field->get_table_column(), |
|
47 | + $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'") |
|
48 | + . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
49 | + } |
|
50 | 50 | |
51 | 51 | |
52 | - /** |
|
53 | - * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
54 | - * you like. |
|
55 | - * |
|
56 | - * @param EE_Base_Class|int $this_obj_or_id |
|
57 | - * @param EE_Base_Class|int $other_obj_or_id |
|
58 | - * @param array $extra_join_model_fields_n_values |
|
59 | - * @return \EE_Base_Class |
|
60 | - * @throws \EE_Error |
|
61 | - */ |
|
62 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
63 | - { |
|
64 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
65 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
66 | - //find the field on THIS model which a foreign key to the other model |
|
67 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
68 | - //set that field on the other model to this model's ID |
|
69 | - $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
70 | - //and make sure this model's field with the foreign model name is set to the correct value |
|
71 | - $this_model_obj->set($this->get_this_model()->get_field_containing_related_model_name()->get_name(), |
|
72 | - $this->get_other_model()->get_this_model_name()); |
|
73 | - $this_model_obj->save(); |
|
74 | - return $other_model_obj; |
|
75 | - } |
|
52 | + /** |
|
53 | + * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
54 | + * you like. |
|
55 | + * |
|
56 | + * @param EE_Base_Class|int $this_obj_or_id |
|
57 | + * @param EE_Base_Class|int $other_obj_or_id |
|
58 | + * @param array $extra_join_model_fields_n_values |
|
59 | + * @return \EE_Base_Class |
|
60 | + * @throws \EE_Error |
|
61 | + */ |
|
62 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
63 | + { |
|
64 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
65 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
66 | + //find the field on THIS model which a foreign key to the other model |
|
67 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
68 | + //set that field on the other model to this model's ID |
|
69 | + $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
70 | + //and make sure this model's field with the foreign model name is set to the correct value |
|
71 | + $this_model_obj->set($this->get_this_model()->get_field_containing_related_model_name()->get_name(), |
|
72 | + $this->get_other_model()->get_this_model_name()); |
|
73 | + $this_model_obj->save(); |
|
74 | + return $other_model_obj; |
|
75 | + } |
|
76 | 76 | |
77 | 77 | |
78 | - /** |
|
79 | - * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
80 | - * |
|
81 | - * @param EE_Base_Class|int $this_obj_or_id |
|
82 | - * @param EE_Base_Class|int $other_obj_or_id |
|
83 | - * @param array $where_query |
|
84 | - * @return \EE_Base_Class |
|
85 | - * @throws \EE_Error |
|
86 | - */ |
|
87 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
88 | - { |
|
89 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
90 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
91 | - //find the field on the other model which is a foreign key to this model |
|
92 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
93 | - //set that field on the other model to this model's ID |
|
94 | - $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
95 | - $this_model_obj->set($this->get_this_model()->get_field_containing_related_model_name()->get_name(), null, |
|
96 | - true); |
|
97 | - $this_model_obj->save(); |
|
98 | - return $other_model_obj; |
|
99 | - } |
|
78 | + /** |
|
79 | + * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
80 | + * |
|
81 | + * @param EE_Base_Class|int $this_obj_or_id |
|
82 | + * @param EE_Base_Class|int $other_obj_or_id |
|
83 | + * @param array $where_query |
|
84 | + * @return \EE_Base_Class |
|
85 | + * @throws \EE_Error |
|
86 | + */ |
|
87 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
88 | + { |
|
89 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
90 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
91 | + //find the field on the other model which is a foreign key to this model |
|
92 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
93 | + //set that field on the other model to this model's ID |
|
94 | + $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
95 | + $this_model_obj->set($this->get_this_model()->get_field_containing_related_model_name()->get_name(), null, |
|
96 | + true); |
|
97 | + $this_model_obj->save(); |
|
98 | + return $other_model_obj; |
|
99 | + } |
|
100 | 100 | } |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -require_once(EE_MODELS . 'relations/EE_Belongs_To_Relation.php'); |
|
2 | +require_once(EE_MODELS.'relations/EE_Belongs_To_Relation.php'); |
|
3 | 3 | |
4 | 4 | |
5 | 5 | /** |
@@ -35,16 +35,16 @@ discard block |
||
35 | 35 | |
36 | 36 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
37 | 37 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
38 | - $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
38 | + $this->get_this_model()->get_this_model_name()).$this_table_fk_field->get_table_alias(); |
|
39 | 39 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
40 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
40 | + $this->get_other_model()->get_this_model_name()).$other_table_pk_field->get_table_alias(); |
|
41 | 41 | $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
42 | 42 | return $this->_left_join($other_table, |
43 | 43 | $other_table_alias, |
44 | 44 | $other_table_pk_field->get_table_column(), |
45 | 45 | $this_table_alias, |
46 | 46 | $this_table_fk_field->get_table_column(), |
47 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'") |
|
47 | + $field_with_model_name->get_qualified_column()."='".$this->get_other_model()->get_this_model_name()."'") |
|
48 | 48 | . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
49 | 49 | } |
50 | 50 |
@@ -12,203 +12,203 @@ |
||
12 | 12 | */ |
13 | 13 | class EE_HABTM_Relation extends EE_Model_Relation_Base |
14 | 14 | { |
15 | - /** |
|
16 | - * Model which defines the relation between two other models. Eg, the EE_Event_Question_Group model, |
|
17 | - * which joins EE_Event and EE_Question_Group |
|
18 | - * |
|
19 | - * @var EEM_Base |
|
20 | - */ |
|
21 | - protected $_joining_model_name; |
|
22 | - |
|
23 | - protected $_model_relation_chain_to_join_model; |
|
24 | - |
|
25 | - |
|
26 | - /** |
|
27 | - * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
28 | - * (and an ee joining-model.) This knows how to join the models, |
|
29 | - * get related models across the relation, and add-and-remove the relationships. |
|
30 | - * |
|
31 | - * @param bool $joining_model_name |
|
32 | - * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
33 | - * are related models across this relation, block (prevent and add an |
|
34 | - * error) the deletion of this model |
|
35 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
36 | - * default |
|
37 | - */ |
|
38 | - public function __construct($joining_model_name, $block_deletes = true, $blocking_delete_error_message = '') |
|
39 | - { |
|
40 | - $this->_joining_model_name = $joining_model_name; |
|
41 | - parent::__construct($block_deletes, $blocking_delete_error_message); |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Gets the joining model's object |
|
46 | - * |
|
47 | - * @return EEM_Base |
|
48 | - */ |
|
49 | - public function get_join_model() |
|
50 | - { |
|
51 | - return $this->_get_model($this->_joining_model_name); |
|
52 | - } |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
57 | - * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
58 | - * |
|
59 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
60 | - * @return string of SQL |
|
61 | - * @throws \EE_Error |
|
62 | - */ |
|
63 | - public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
64 | - { |
|
65 | - //create sql like |
|
66 | - //LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
67 | - //LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
68 | - //remember the model relation chain to the JOIN model, because we'll |
|
69 | - //need it for get_join_statement() |
|
70 | - $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
71 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field();//get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
72 | - $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
73 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
74 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
75 | - |
|
76 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
77 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
78 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
79 | - //phew! ok, we have all the info we need, now we can create the SQL join string |
|
80 | - $SQL = $this->_left_join($join_table, $join_table_alias, $join_table_fk_field_to_this_table->get_table_column(), |
|
81 | - $this_table_alias, |
|
82 | - $this_table_pk_field->get_table_column()) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
83 | - |
|
84 | - return $SQL; |
|
85 | - } |
|
86 | - |
|
87 | - |
|
88 | - /** |
|
89 | - * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
90 | - * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
91 | - * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
92 | - * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
93 | - * the intermediate join table, and then to the other model's pk's table |
|
94 | - * |
|
95 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
96 | - * @return string of SQL |
|
97 | - * @throws \EE_Error |
|
98 | - */ |
|
99 | - public function get_join_statement($model_relation_chain) |
|
100 | - { |
|
101 | - if ($this->_model_relation_chain_to_join_model === null) { |
|
102 | - throw new EE_Error(sprintf(__('When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
103 | - 'event_espresso'))); |
|
104 | - } |
|
105 | - $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
106 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
|
107 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
108 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
109 | - $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
110 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
111 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
112 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
113 | - |
|
114 | - $SQL = $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
|
115 | - $join_table_alias, |
|
116 | - $join_table_fk_field_to_other_table->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
117 | - return $SQL; |
|
118 | - } |
|
119 | - |
|
120 | - |
|
121 | - /** |
|
122 | - * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
123 | - * If the join table has additional columns (eg, the Event_Question_Group table has a is_primary column), then |
|
124 | - * you'll want to directly use the EEM_Event_Question_Group model to add the entry to the table and set those extra |
|
125 | - * columns' values |
|
126 | - * |
|
127 | - * @param EE_Base_Class|int $this_obj_or_id |
|
128 | - * @param EE_Base_Class|int $other_obj_or_id |
|
129 | - * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
130 | - * checking existing values and for setting new rows if |
|
131 | - * no exact matches. |
|
132 | - * @return EE_Base_Class |
|
133 | - * @throws \EE_Error |
|
134 | - */ |
|
135 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
136 | - { |
|
137 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
138 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
139 | - //check if such a relationship already exists |
|
140 | - $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
141 | - $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
142 | - |
|
143 | - $cols_n_values = array( |
|
144 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
145 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
146 | - ); |
|
147 | - |
|
148 | - //if $where_query exists lets add them to the query_params. |
|
149 | - if (! empty($extra_join_model_fields_n_values)) { |
|
150 | - //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
151 | - //make sure we strip THIS models name from the query param |
|
152 | - $parsed_query = array(); |
|
153 | - foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
154 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
155 | - $query_param); |
|
156 | - $parsed_query[$query_param] = $val; |
|
157 | - } |
|
158 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
159 | - } |
|
160 | - |
|
161 | - $query_params = array($cols_n_values); |
|
162 | - |
|
163 | - |
|
164 | - $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
165 | - //if there is already an entry in the join table, indicating a relationship, we're done |
|
166 | - //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
167 | - //the other tables, use the joining model directly! |
|
168 | - if (! $existing_entry_in_join_table) { |
|
169 | - $this->get_join_model()->insert($cols_n_values); |
|
170 | - } |
|
171 | - return $other_model_obj; |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - /** |
|
176 | - * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
177 | - * |
|
178 | - * @param EE_Base_Class|int $this_obj_or_id |
|
179 | - * @param EE_Base_Class|int $other_obj_or_id |
|
180 | - * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
181 | - * values and for removing existing rows if exact matches exist. |
|
182 | - * @return EE_Base_Class |
|
183 | - * @throws \EE_Error |
|
184 | - */ |
|
185 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
186 | - { |
|
187 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
188 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
189 | - //check if such a relationship already exists |
|
190 | - $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
191 | - $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
192 | - |
|
193 | - $cols_n_values = array( |
|
194 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
195 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
196 | - ); |
|
197 | - |
|
198 | - //if $where_query exists lets add them to the query_params. |
|
199 | - if (! empty($where_query)) { |
|
200 | - //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
201 | - //make sure we strip THIS models name from the query param |
|
202 | - $parsed_query = array(); |
|
203 | - foreach ($where_query as $query_param => $val) { |
|
204 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
205 | - $query_param); |
|
206 | - $parsed_query[$query_param] = $val; |
|
207 | - } |
|
208 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
209 | - } |
|
210 | - |
|
211 | - $this->get_join_model()->delete(array($cols_n_values)); |
|
212 | - return $other_model_obj; |
|
213 | - } |
|
15 | + /** |
|
16 | + * Model which defines the relation between two other models. Eg, the EE_Event_Question_Group model, |
|
17 | + * which joins EE_Event and EE_Question_Group |
|
18 | + * |
|
19 | + * @var EEM_Base |
|
20 | + */ |
|
21 | + protected $_joining_model_name; |
|
22 | + |
|
23 | + protected $_model_relation_chain_to_join_model; |
|
24 | + |
|
25 | + |
|
26 | + /** |
|
27 | + * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
28 | + * (and an ee joining-model.) This knows how to join the models, |
|
29 | + * get related models across the relation, and add-and-remove the relationships. |
|
30 | + * |
|
31 | + * @param bool $joining_model_name |
|
32 | + * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
33 | + * are related models across this relation, block (prevent and add an |
|
34 | + * error) the deletion of this model |
|
35 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
36 | + * default |
|
37 | + */ |
|
38 | + public function __construct($joining_model_name, $block_deletes = true, $blocking_delete_error_message = '') |
|
39 | + { |
|
40 | + $this->_joining_model_name = $joining_model_name; |
|
41 | + parent::__construct($block_deletes, $blocking_delete_error_message); |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Gets the joining model's object |
|
46 | + * |
|
47 | + * @return EEM_Base |
|
48 | + */ |
|
49 | + public function get_join_model() |
|
50 | + { |
|
51 | + return $this->_get_model($this->_joining_model_name); |
|
52 | + } |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
57 | + * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
58 | + * |
|
59 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
60 | + * @return string of SQL |
|
61 | + * @throws \EE_Error |
|
62 | + */ |
|
63 | + public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
64 | + { |
|
65 | + //create sql like |
|
66 | + //LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
67 | + //LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
68 | + //remember the model relation chain to the JOIN model, because we'll |
|
69 | + //need it for get_join_statement() |
|
70 | + $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
71 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field();//get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
72 | + $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
73 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
74 | + $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
75 | + |
|
76 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
77 | + $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
78 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
79 | + //phew! ok, we have all the info we need, now we can create the SQL join string |
|
80 | + $SQL = $this->_left_join($join_table, $join_table_alias, $join_table_fk_field_to_this_table->get_table_column(), |
|
81 | + $this_table_alias, |
|
82 | + $this_table_pk_field->get_table_column()) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
83 | + |
|
84 | + return $SQL; |
|
85 | + } |
|
86 | + |
|
87 | + |
|
88 | + /** |
|
89 | + * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
90 | + * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
91 | + * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
92 | + * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
93 | + * the intermediate join table, and then to the other model's pk's table |
|
94 | + * |
|
95 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
96 | + * @return string of SQL |
|
97 | + * @throws \EE_Error |
|
98 | + */ |
|
99 | + public function get_join_statement($model_relation_chain) |
|
100 | + { |
|
101 | + if ($this->_model_relation_chain_to_join_model === null) { |
|
102 | + throw new EE_Error(sprintf(__('When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
103 | + 'event_espresso'))); |
|
104 | + } |
|
105 | + $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
106 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
|
107 | + $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
108 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
109 | + $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
110 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
111 | + $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
112 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
113 | + |
|
114 | + $SQL = $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
|
115 | + $join_table_alias, |
|
116 | + $join_table_fk_field_to_other_table->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
117 | + return $SQL; |
|
118 | + } |
|
119 | + |
|
120 | + |
|
121 | + /** |
|
122 | + * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
123 | + * If the join table has additional columns (eg, the Event_Question_Group table has a is_primary column), then |
|
124 | + * you'll want to directly use the EEM_Event_Question_Group model to add the entry to the table and set those extra |
|
125 | + * columns' values |
|
126 | + * |
|
127 | + * @param EE_Base_Class|int $this_obj_or_id |
|
128 | + * @param EE_Base_Class|int $other_obj_or_id |
|
129 | + * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
130 | + * checking existing values and for setting new rows if |
|
131 | + * no exact matches. |
|
132 | + * @return EE_Base_Class |
|
133 | + * @throws \EE_Error |
|
134 | + */ |
|
135 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
136 | + { |
|
137 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
138 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
139 | + //check if such a relationship already exists |
|
140 | + $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
141 | + $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
142 | + |
|
143 | + $cols_n_values = array( |
|
144 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
145 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
146 | + ); |
|
147 | + |
|
148 | + //if $where_query exists lets add them to the query_params. |
|
149 | + if (! empty($extra_join_model_fields_n_values)) { |
|
150 | + //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
151 | + //make sure we strip THIS models name from the query param |
|
152 | + $parsed_query = array(); |
|
153 | + foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
154 | + $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
155 | + $query_param); |
|
156 | + $parsed_query[$query_param] = $val; |
|
157 | + } |
|
158 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
159 | + } |
|
160 | + |
|
161 | + $query_params = array($cols_n_values); |
|
162 | + |
|
163 | + |
|
164 | + $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
165 | + //if there is already an entry in the join table, indicating a relationship, we're done |
|
166 | + //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
167 | + //the other tables, use the joining model directly! |
|
168 | + if (! $existing_entry_in_join_table) { |
|
169 | + $this->get_join_model()->insert($cols_n_values); |
|
170 | + } |
|
171 | + return $other_model_obj; |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + /** |
|
176 | + * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
177 | + * |
|
178 | + * @param EE_Base_Class|int $this_obj_or_id |
|
179 | + * @param EE_Base_Class|int $other_obj_or_id |
|
180 | + * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
181 | + * values and for removing existing rows if exact matches exist. |
|
182 | + * @return EE_Base_Class |
|
183 | + * @throws \EE_Error |
|
184 | + */ |
|
185 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
186 | + { |
|
187 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
188 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
189 | + //check if such a relationship already exists |
|
190 | + $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
191 | + $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
192 | + |
|
193 | + $cols_n_values = array( |
|
194 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
195 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
196 | + ); |
|
197 | + |
|
198 | + //if $where_query exists lets add them to the query_params. |
|
199 | + if (! empty($where_query)) { |
|
200 | + //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
201 | + //make sure we strip THIS models name from the query param |
|
202 | + $parsed_query = array(); |
|
203 | + foreach ($where_query as $query_param => $val) { |
|
204 | + $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
205 | + $query_param); |
|
206 | + $parsed_query[$query_param] = $val; |
|
207 | + } |
|
208 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
209 | + } |
|
210 | + |
|
211 | + $this->get_join_model()->delete(array($cols_n_values)); |
|
212 | + return $other_model_obj; |
|
213 | + } |
|
214 | 214 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -require_once(EE_MODELS . 'relations/EE_Model_Relation_Base.php'); |
|
3 | +require_once(EE_MODELS.'relations/EE_Model_Relation_Base.php'); |
|
4 | 4 | |
5 | 5 | |
6 | 6 | /** |
@@ -68,18 +68,18 @@ discard block |
||
68 | 68 | //remember the model relation chain to the JOIN model, because we'll |
69 | 69 | //need it for get_join_statement() |
70 | 70 | $this->_model_relation_chain_to_join_model = $model_relation_chain; |
71 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field();//get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
71 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); //get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
72 | 72 | $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
73 | 73 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
74 | - $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); |
|
74 | + $this->get_this_model()->get_this_model_name()).$this_table_pk_field->get_table_alias(); |
|
75 | 75 | |
76 | 76 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
77 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
77 | + $this->get_join_model()->get_this_model_name()).$join_table_fk_field_to_this_table->get_table_alias(); |
|
78 | 78 | $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
79 | 79 | //phew! ok, we have all the info we need, now we can create the SQL join string |
80 | 80 | $SQL = $this->_left_join($join_table, $join_table_alias, $join_table_fk_field_to_this_table->get_table_column(), |
81 | 81 | $this_table_alias, |
82 | - $this_table_pk_field->get_table_column()) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
82 | + $this_table_pk_field->get_table_column()).$this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
83 | 83 | |
84 | 84 | return $SQL; |
85 | 85 | } |
@@ -104,16 +104,16 @@ discard block |
||
104 | 104 | } |
105 | 105 | $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
106 | 106 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, |
107 | - $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
107 | + $this->get_join_model()->get_this_model_name()).$join_table_fk_field_to_this_table->get_table_alias(); |
|
108 | 108 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
109 | 109 | $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
110 | 110 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
111 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
111 | + $this->get_other_model()->get_this_model_name()).$other_table_pk_field->get_table_alias(); |
|
112 | 112 | $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
113 | 113 | |
114 | 114 | $SQL = $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
115 | 115 | $join_table_alias, |
116 | - $join_table_fk_field_to_other_table->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
116 | + $join_table_fk_field_to_other_table->get_table_column()).$this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
117 | 117 | return $SQL; |
118 | 118 | } |
119 | 119 | |
@@ -146,12 +146,12 @@ discard block |
||
146 | 146 | ); |
147 | 147 | |
148 | 148 | //if $where_query exists lets add them to the query_params. |
149 | - if (! empty($extra_join_model_fields_n_values)) { |
|
149 | + if ( ! empty($extra_join_model_fields_n_values)) { |
|
150 | 150 | //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
151 | 151 | //make sure we strip THIS models name from the query param |
152 | 152 | $parsed_query = array(); |
153 | 153 | foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
154 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
154 | + $query_param = str_replace($this->get_join_model()->get_this_model_name().".", "", |
|
155 | 155 | $query_param); |
156 | 156 | $parsed_query[$query_param] = $val; |
157 | 157 | } |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | //if there is already an entry in the join table, indicating a relationship, we're done |
166 | 166 | //again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
167 | 167 | //the other tables, use the joining model directly! |
168 | - if (! $existing_entry_in_join_table) { |
|
168 | + if ( ! $existing_entry_in_join_table) { |
|
169 | 169 | $this->get_join_model()->insert($cols_n_values); |
170 | 170 | } |
171 | 171 | return $other_model_obj; |
@@ -196,12 +196,12 @@ discard block |
||
196 | 196 | ); |
197 | 197 | |
198 | 198 | //if $where_query exists lets add them to the query_params. |
199 | - if (! empty($where_query)) { |
|
199 | + if ( ! empty($where_query)) { |
|
200 | 200 | //make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
201 | 201 | //make sure we strip THIS models name from the query param |
202 | 202 | $parsed_query = array(); |
203 | 203 | foreach ($where_query as $query_param => $val) { |
204 | - $query_param = str_replace($this->get_join_model()->get_this_model_name() . ".", "", |
|
204 | + $query_param = str_replace($this->get_join_model()->get_this_model_name().".", "", |
|
205 | 205 | $query_param); |
206 | 206 | $parsed_query[$query_param] = $val; |
207 | 207 | } |
@@ -13,128 +13,128 @@ |
||
13 | 13 | class EE_Belongs_To_Relation extends EE_Model_Relation_Base |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * Object representing the relationship between two models. Belongs_To means that THIS model has the foreign key |
|
18 | - * to the other model. This knows how to join the models, |
|
19 | - * get related models across the relation, and add-and-remove the relationships. |
|
20 | - * |
|
21 | - * @param boolean $block_deletes For Belongs_To relations, this is set to FALSE by |
|
22 | - * default. if there are related models across this |
|
23 | - * relation, block (prevent and add an error) the |
|
24 | - * deletion of this model |
|
25 | - * @param string $related_model_objects_deletion_error_message a customized error message on blocking deletes |
|
26 | - * instead of the default |
|
27 | - */ |
|
28 | - public function __construct($block_deletes = false, $related_model_objects_deletion_error_message = null) |
|
29 | - { |
|
30 | - parent::__construct($block_deletes, $related_model_objects_deletion_error_message); |
|
31 | - } |
|
16 | + /** |
|
17 | + * Object representing the relationship between two models. Belongs_To means that THIS model has the foreign key |
|
18 | + * to the other model. This knows how to join the models, |
|
19 | + * get related models across the relation, and add-and-remove the relationships. |
|
20 | + * |
|
21 | + * @param boolean $block_deletes For Belongs_To relations, this is set to FALSE by |
|
22 | + * default. if there are related models across this |
|
23 | + * relation, block (prevent and add an error) the |
|
24 | + * deletion of this model |
|
25 | + * @param string $related_model_objects_deletion_error_message a customized error message on blocking deletes |
|
26 | + * instead of the default |
|
27 | + */ |
|
28 | + public function __construct($block_deletes = false, $related_model_objects_deletion_error_message = null) |
|
29 | + { |
|
30 | + parent::__construct($block_deletes, $related_model_objects_deletion_error_message); |
|
31 | + } |
|
32 | 32 | |
33 | 33 | |
34 | - /** |
|
35 | - * get_join_statement |
|
36 | - * |
|
37 | - * @param string $model_relation_chain |
|
38 | - * @return string |
|
39 | - * @throws \EE_Error |
|
40 | - */ |
|
41 | - public function get_join_statement($model_relation_chain) |
|
42 | - { |
|
43 | - //create the sql string like |
|
44 | - $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
45 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
46 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
47 | - $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
48 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
49 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
50 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
51 | - return $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
|
52 | - $this_table_alias, |
|
53 | - $this_table_fk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
54 | - } |
|
34 | + /** |
|
35 | + * get_join_statement |
|
36 | + * |
|
37 | + * @param string $model_relation_chain |
|
38 | + * @return string |
|
39 | + * @throws \EE_Error |
|
40 | + */ |
|
41 | + public function get_join_statement($model_relation_chain) |
|
42 | + { |
|
43 | + //create the sql string like |
|
44 | + $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
45 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
46 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
47 | + $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
48 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
49 | + $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
50 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
51 | + return $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
|
52 | + $this_table_alias, |
|
53 | + $this_table_fk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
54 | + } |
|
55 | 55 | |
56 | 56 | |
57 | - /** |
|
58 | - * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
59 | - * you like. |
|
60 | - * |
|
61 | - * @param EE_Base_Class|int $this_obj_or_id |
|
62 | - * @param EE_Base_Class|int $other_obj_or_id |
|
63 | - * @param array $extra_join_model_fields_n_values |
|
64 | - * @return \EE_Base_Class |
|
65 | - * @throws \EE_Error |
|
66 | - */ |
|
67 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
68 | - { |
|
69 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
70 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
71 | - //find the field on the other model which is a foreign key to this model |
|
72 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
73 | - if ($this_model_obj->get($fk_on_this_model->get_name()) != $other_model_obj->ID()) { |
|
74 | - //set that field on the other model to this model's ID |
|
75 | - $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
76 | - $this_model_obj->save(); |
|
77 | - } |
|
78 | - return $other_model_obj; |
|
79 | - } |
|
57 | + /** |
|
58 | + * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
59 | + * you like. |
|
60 | + * |
|
61 | + * @param EE_Base_Class|int $this_obj_or_id |
|
62 | + * @param EE_Base_Class|int $other_obj_or_id |
|
63 | + * @param array $extra_join_model_fields_n_values |
|
64 | + * @return \EE_Base_Class |
|
65 | + * @throws \EE_Error |
|
66 | + */ |
|
67 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
68 | + { |
|
69 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
70 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
71 | + //find the field on the other model which is a foreign key to this model |
|
72 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
73 | + if ($this_model_obj->get($fk_on_this_model->get_name()) != $other_model_obj->ID()) { |
|
74 | + //set that field on the other model to this model's ID |
|
75 | + $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
76 | + $this_model_obj->save(); |
|
77 | + } |
|
78 | + return $other_model_obj; |
|
79 | + } |
|
80 | 80 | |
81 | 81 | |
82 | - /** |
|
83 | - * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
84 | - * |
|
85 | - * @param EE_Base_Class|int $this_obj_or_id |
|
86 | - * @param EE_Base_Class|int $other_obj_or_id |
|
87 | - * @param array $where_query |
|
88 | - * @return \EE_Base_Class |
|
89 | - * @throws \EE_Error |
|
90 | - */ |
|
91 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
92 | - { |
|
93 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
94 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
95 | - //find the field on the other model which is a foreign key to this model |
|
96 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
97 | - //set that field on the other model to this model's ID |
|
98 | - $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
99 | - $this_model_obj->save(); |
|
100 | - return $other_model_obj; |
|
101 | - } |
|
82 | + /** |
|
83 | + * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
84 | + * |
|
85 | + * @param EE_Base_Class|int $this_obj_or_id |
|
86 | + * @param EE_Base_Class|int $other_obj_or_id |
|
87 | + * @param array $where_query |
|
88 | + * @return \EE_Base_Class |
|
89 | + * @throws \EE_Error |
|
90 | + */ |
|
91 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
92 | + { |
|
93 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
94 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
95 | + //find the field on the other model which is a foreign key to this model |
|
96 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
97 | + //set that field on the other model to this model's ID |
|
98 | + $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
99 | + $this_model_obj->save(); |
|
100 | + return $other_model_obj; |
|
101 | + } |
|
102 | 102 | |
103 | 103 | |
104 | - /** |
|
105 | - * Overrides parent so that we don't NEED to save the $model_object before getting the related objects. |
|
106 | - * |
|
107 | - * @param EE_Base_Class $model_obj_or_id |
|
108 | - * @param array $query_params like EEM_Base::get_all's $query_params |
|
109 | - * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
110 | - * @return EE_Base_Class[] |
|
111 | - * @throws \EE_Error |
|
112 | - */ |
|
113 | - public function get_all_related( |
|
114 | - $model_obj_or_id, |
|
115 | - $query_params = array(), |
|
116 | - $values_already_prepared_by_model_object = false |
|
117 | - ) { |
|
118 | - if ($values_already_prepared_by_model_object !== false) { |
|
119 | - EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
120 | - __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
121 | - '4.8.1'); |
|
122 | - } |
|
123 | - //get column on this model object which is a foreign key to the other model |
|
124 | - $fk_field_obj = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
125 | - //get its value |
|
126 | - if ($model_obj_or_id instanceof EE_Base_Class) { |
|
127 | - $model_obj = $model_obj_or_id; |
|
128 | - } else { |
|
129 | - $model_obj = $this->get_this_model()->ensure_is_obj($model_obj_or_id); |
|
130 | - } |
|
131 | - $ID_value_on_other_model = $model_obj->get($fk_field_obj->get_name()); |
|
132 | - //get all where their PK matches that value |
|
133 | - $query_params[0][$this->get_other_model()->get_primary_key_field()->get_name()] = $ID_value_on_other_model; |
|
134 | - $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
104 | + /** |
|
105 | + * Overrides parent so that we don't NEED to save the $model_object before getting the related objects. |
|
106 | + * |
|
107 | + * @param EE_Base_Class $model_obj_or_id |
|
108 | + * @param array $query_params like EEM_Base::get_all's $query_params |
|
109 | + * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
110 | + * @return EE_Base_Class[] |
|
111 | + * @throws \EE_Error |
|
112 | + */ |
|
113 | + public function get_all_related( |
|
114 | + $model_obj_or_id, |
|
115 | + $query_params = array(), |
|
116 | + $values_already_prepared_by_model_object = false |
|
117 | + ) { |
|
118 | + if ($values_already_prepared_by_model_object !== false) { |
|
119 | + EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
120 | + __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
121 | + '4.8.1'); |
|
122 | + } |
|
123 | + //get column on this model object which is a foreign key to the other model |
|
124 | + $fk_field_obj = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
125 | + //get its value |
|
126 | + if ($model_obj_or_id instanceof EE_Base_Class) { |
|
127 | + $model_obj = $model_obj_or_id; |
|
128 | + } else { |
|
129 | + $model_obj = $this->get_this_model()->ensure_is_obj($model_obj_or_id); |
|
130 | + } |
|
131 | + $ID_value_on_other_model = $model_obj->get($fk_field_obj->get_name()); |
|
132 | + //get all where their PK matches that value |
|
133 | + $query_params[0][$this->get_other_model()->get_primary_key_field()->get_name()] = $ID_value_on_other_model; |
|
134 | + $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
135 | 135 | // echo '$query_params'; |
136 | 136 | // var_dump($query_params); |
137 | - return $this->get_other_model()->get_all($query_params); |
|
138 | - } |
|
137 | + return $this->get_other_model()->get_all($query_params); |
|
138 | + } |
|
139 | 139 | |
140 | 140 | } |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -require_once(EE_MODELS . 'relations/EE_Model_Relation_Base.php'); |
|
2 | +require_once(EE_MODELS.'relations/EE_Model_Relation_Base.php'); |
|
3 | 3 | |
4 | 4 | /** |
5 | 5 | * Class EE_Belongs_To_Relation |
@@ -44,13 +44,13 @@ discard block |
||
44 | 44 | $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
45 | 45 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
46 | 46 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
47 | - $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); |
|
47 | + $this->get_this_model()->get_this_model_name()).$this_table_fk_field->get_table_alias(); |
|
48 | 48 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
49 | - $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); |
|
49 | + $this->get_other_model()->get_this_model_name()).$other_table_pk_field->get_table_alias(); |
|
50 | 50 | $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
51 | 51 | return $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), |
52 | 52 | $this_table_alias, |
53 | - $this_table_fk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
53 | + $this_table_fk_field->get_table_column()).$this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | $this_table_join_column, |
149 | 149 | $extra_join_sql = '' |
150 | 150 | ) { |
151 | - return " LEFT JOIN " . $other_table . " AS " . $other_table_alias . " ON " . $other_table_alias . "." . $other_table_column . "=" . $this_table_alias . "." . $this_table_join_column . ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
151 | + return " LEFT JOIN ".$other_table." AS ".$other_table_alias." ON ".$other_table_alias.".".$other_table_column."=".$this_table_alias.".".$this_table_join_column.($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
152 | 152 | } |
153 | 153 | |
154 | 154 | |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | */ |
194 | 194 | protected function _disable_default_where_conditions_on_query_param($query_params) |
195 | 195 | { |
196 | - if (! isset($query_params['default_where_conditions'])) { |
|
196 | + if ( ! isset($query_params['default_where_conditions'])) { |
|
197 | 197 | $query_params['default_where_conditions'] = 'none'; |
198 | 198 | } |
199 | 199 | return $query_params; |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object, |
222 | 222 | $model_object_or_id); |
223 | 223 | /* @var $model_object_or_id EE_Base_Class */ |
224 | - if (! $delete_is_blocked) { |
|
224 | + if ( ! $delete_is_blocked) { |
|
225 | 225 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
226 | 226 | $related_model_object->delete(); |
227 | 227 | $deleted_count++; |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
256 | 256 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
257 | 257 | $deleted_count++; |
258 | - if (! $delete_is_blocked) { |
|
258 | + if ( ! $delete_is_blocked) { |
|
259 | 259 | $related_model_object->delete_permanently(); |
260 | 260 | } else { |
261 | 261 | //delete is blocked |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | } |
265 | 265 | } else { |
266 | 266 | //its not a soft-deletable thing anyways. do the normal logic. |
267 | - if (! $delete_is_blocked) { |
|
267 | + if ( ! $delete_is_blocked) { |
|
268 | 268 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
269 | 269 | $related_model_object->delete(); |
270 | 270 | $deleted_count++; |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | if ($model_object_or_id instanceof EE_Base_Class) { |
289 | 289 | $model_object_id = $model_object_or_id->ID(); |
290 | 290 | } |
291 | - if (! $model_object_id) { |
|
291 | + if ( ! $model_object_id) { |
|
292 | 292 | throw new EE_Error(sprintf(__("Sorry, we cant get the related %s model objects to %s model object before it has an ID. You can solve that by just saving it before trying to get its related model objects", |
293 | 293 | "event_espresso"), $this->get_other_model()->get_this_model_name(), |
294 | 294 | $this->get_this_model()->get_this_model_name())); |
@@ -17,482 +17,482 @@ |
||
17 | 17 | */ |
18 | 18 | abstract class EE_Model_Relation_Base implements HasSchemaInterface |
19 | 19 | { |
20 | - /** |
|
21 | - * The model name of which this relation is a component (ie, the model that called new EE_Model_Relation_Base) |
|
22 | - * |
|
23 | - * @var string eg Event, Question_Group, Registration |
|
24 | - */ |
|
25 | - private $_this_model_name; |
|
26 | - /** |
|
27 | - * The model name pointed to by this relation (ie, the model we want to establish a relationship to) |
|
28 | - * |
|
29 | - * @var string eg Event, Question_Group, Registration |
|
30 | - */ |
|
31 | - private $_other_model_name; |
|
32 | - |
|
33 | - /** |
|
34 | - * this is typically used when calling the relation models to make sure they inherit any set timezone from the |
|
35 | - * initiating model. |
|
36 | - * |
|
37 | - * @var string |
|
38 | - */ |
|
39 | - protected $_timezone; |
|
40 | - |
|
41 | - /** |
|
42 | - * If you try to delete "this_model", and there are related "other_models", |
|
43 | - * and this isn't null, then abandon the deletion and add this warning. |
|
44 | - * This effectively makes it impossible to delete "this_model" while there are |
|
45 | - * related "other_models" along this relation. |
|
46 | - * |
|
47 | - * @var string (internationalized) |
|
48 | - */ |
|
49 | - protected $_blocking_delete_error_message; |
|
50 | - |
|
51 | - protected $_blocking_delete = false; |
|
52 | - |
|
53 | - /** |
|
54 | - * Object representing the relationship between two models. This knows how to join the models, |
|
55 | - * get related models across the relation, and add-and-remove the relationships. |
|
56 | - * |
|
57 | - * @param boolean $block_deletes if there are related models across this relation, block (prevent |
|
58 | - * and add an error) the deletion of this model |
|
59 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
60 | - * default |
|
61 | - */ |
|
62 | - public function __construct($block_deletes, $blocking_delete_error_message) |
|
63 | - { |
|
64 | - $this->_blocking_delete = $block_deletes; |
|
65 | - $this->_blocking_delete_error_message = $blocking_delete_error_message; |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * @param $this_model_name |
|
71 | - * @param $other_model_name |
|
72 | - * @throws EE_Error |
|
73 | - */ |
|
74 | - public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
75 | - { |
|
76 | - $this->_this_model_name = $this_model_name; |
|
77 | - $this->_other_model_name = $other_model_name; |
|
78 | - if (is_string($this->_blocking_delete)) { |
|
79 | - throw new EE_Error(sprintf(__("When instantiating the relation of type %s from %s to %s, the \$block_deletes argument should be a boolean, not a string (%s)", |
|
80 | - "event_espresso"), |
|
81 | - get_class($this), $this_model_name, $other_model_name, $this->_blocking_delete)); |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - |
|
86 | - /** |
|
87 | - * Gets the model where this relation is defined. |
|
88 | - * |
|
89 | - * @return EEM_Base |
|
90 | - */ |
|
91 | - public function get_this_model() |
|
92 | - { |
|
93 | - return $this->_get_model($this->_this_model_name); |
|
94 | - } |
|
95 | - |
|
96 | - |
|
97 | - /** |
|
98 | - * Gets the model which this relation establishes the relation TO (ie, |
|
99 | - * this relation object was defined on get_this_model(), get_other_model() is the other one) |
|
100 | - * |
|
101 | - * @return EEM_Base |
|
102 | - */ |
|
103 | - public function get_other_model() |
|
104 | - { |
|
105 | - return $this->_get_model($this->_other_model_name); |
|
106 | - } |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * Internally used by get_this_model() and get_other_model() |
|
111 | - * |
|
112 | - * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
|
113 | - * @return EEM_Base |
|
114 | - */ |
|
115 | - protected function _get_model($model_name) |
|
116 | - { |
|
117 | - $modelInstance = EE_Registry::instance()->load_model($model_name); |
|
118 | - $modelInstance->set_timezone($this->_timezone); |
|
119 | - return $modelInstance; |
|
120 | - } |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * entirely possible that relations may be called from a model and we need to make sure those relations have their |
|
125 | - * timezone set correctly. |
|
126 | - * |
|
127 | - * @param string $timezone timezone to set. |
|
128 | - */ |
|
129 | - public function set_timezone($timezone) |
|
130 | - { |
|
131 | - if ($timezone !== null) { |
|
132 | - $this->_timezone = $timezone; |
|
133 | - } |
|
134 | - } |
|
135 | - |
|
136 | - |
|
137 | - /** |
|
138 | - * @param $other_table |
|
139 | - * @param $other_table_alias |
|
140 | - * @param $other_table_column |
|
141 | - * @param $this_table_alias |
|
142 | - * @param $this_table_join_column |
|
143 | - * @param string $extra_join_sql |
|
144 | - * @return string |
|
145 | - */ |
|
146 | - protected function _left_join( |
|
147 | - $other_table, |
|
148 | - $other_table_alias, |
|
149 | - $other_table_column, |
|
150 | - $this_table_alias, |
|
151 | - $this_table_join_column, |
|
152 | - $extra_join_sql = '' |
|
153 | - ) { |
|
154 | - return " LEFT JOIN " . $other_table . " AS " . $other_table_alias . " ON " . $other_table_alias . "." . $other_table_column . "=" . $this_table_alias . "." . $this_table_join_column . ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
155 | - } |
|
156 | - |
|
157 | - |
|
158 | - /** |
|
159 | - * Gets all the model objects of type of other model related to $model_object, |
|
160 | - * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation. |
|
161 | - * For both of those child classes, $model_object must be saved so that it has an ID before querying, |
|
162 | - * otherwise an error will be thrown. Note: by default we disable default_where_conditions |
|
163 | - * EE_Belongs_To_Relation doesn't need to be saved before querying. |
|
164 | - * |
|
165 | - * @param EE_Base_Class|int $model_object_or_id or the primary key of this model |
|
166 | - * @param array $query_params like EEM_Base::get_all's $query_params |
|
167 | - * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
168 | - * @return EE_Base_Class[] |
|
169 | - * @throws \EE_Error |
|
170 | - */ |
|
171 | - public function get_all_related( |
|
172 | - $model_object_or_id, |
|
173 | - $query_params = array(), |
|
174 | - $values_already_prepared_by_model_object = false |
|
175 | - ) { |
|
176 | - if ($values_already_prepared_by_model_object !== false) { |
|
177 | - EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
178 | - __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
179 | - '4.8.1'); |
|
180 | - } |
|
181 | - $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
182 | - $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() |
|
183 | - . "." |
|
184 | - . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
185 | - $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
186 | - $query_params[0][$query_param_where_this_model_pk] = $model_object_id; |
|
187 | - return $this->get_other_model()->get_all($query_params); |
|
188 | - } |
|
189 | - |
|
190 | - |
|
191 | - /** |
|
192 | - * Alters the $query_params to disable default where conditions, unless otherwise specified |
|
193 | - * |
|
194 | - * @param string $query_params |
|
195 | - * @return array |
|
196 | - */ |
|
197 | - protected function _disable_default_where_conditions_on_query_param($query_params) |
|
198 | - { |
|
199 | - if (! isset($query_params['default_where_conditions'])) { |
|
200 | - $query_params['default_where_conditions'] = 'none'; |
|
201 | - } |
|
202 | - return $query_params; |
|
203 | - } |
|
204 | - |
|
205 | - |
|
206 | - /** |
|
207 | - * Deletes the related model objects which meet the query parameters. If no |
|
208 | - * parameters are specified, then all related model objects will be deleted. |
|
209 | - * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
210 | - * model objects will only be soft-deleted. |
|
211 | - * |
|
212 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
213 | - * @param array $query_params |
|
214 | - * @return int of how many related models got deleted |
|
215 | - * @throws \EE_Error |
|
216 | - */ |
|
217 | - public function delete_all_related($model_object_or_id, $query_params = array()) |
|
218 | - { |
|
219 | - //for each thing we would delete, |
|
220 | - $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
221 | - //determine if it's blocked by anything else before it can be deleted |
|
222 | - $deleted_count = 0; |
|
223 | - foreach ($related_model_objects as $related_model_object) { |
|
224 | - $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object, |
|
225 | - $model_object_or_id); |
|
226 | - /* @var $model_object_or_id EE_Base_Class */ |
|
227 | - if (! $delete_is_blocked) { |
|
228 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
229 | - $related_model_object->delete(); |
|
230 | - $deleted_count++; |
|
231 | - } |
|
232 | - } |
|
233 | - return $deleted_count; |
|
234 | - } |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * Deletes the related model objects which meet the query parameters. If no |
|
239 | - * parameters are specified, then all related model objects will be deleted. |
|
240 | - * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
241 | - * model objects will only be soft-deleted. |
|
242 | - * |
|
243 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
244 | - * @param array $query_params |
|
245 | - * @return int of how many related models got deleted |
|
246 | - * @throws \EE_Error |
|
247 | - */ |
|
248 | - public function delete_related_permanently($model_object_or_id, $query_params = array()) |
|
249 | - { |
|
250 | - //for each thing we would delete, |
|
251 | - $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
252 | - //determine if it's blocked by anything else before it can be deleted |
|
253 | - $deleted_count = 0; |
|
254 | - foreach ($related_model_objects as $related_model_object) { |
|
255 | - $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object, |
|
256 | - $model_object_or_id); |
|
257 | - /* @var $model_object_or_id EE_Base_Class */ |
|
258 | - if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
|
259 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
260 | - $deleted_count++; |
|
261 | - if (! $delete_is_blocked) { |
|
262 | - $related_model_object->delete_permanently(); |
|
263 | - } else { |
|
264 | - //delete is blocked |
|
265 | - //brent and darren, in this case, wanted to just soft delete it then |
|
266 | - $related_model_object->delete(); |
|
267 | - } |
|
268 | - } else { |
|
269 | - //its not a soft-deletable thing anyways. do the normal logic. |
|
270 | - if (! $delete_is_blocked) { |
|
271 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
272 | - $related_model_object->delete(); |
|
273 | - $deleted_count++; |
|
274 | - } |
|
275 | - } |
|
276 | - } |
|
277 | - return $deleted_count; |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - /** |
|
282 | - * this just returns a model_object_id for incoming item that could be an object or id. |
|
283 | - * |
|
284 | - * @param EE_Base_Class|int $model_object_or_id model object or the primary key of this model |
|
285 | - * @throws EE_Error |
|
286 | - * @return int |
|
287 | - */ |
|
288 | - protected function _get_model_object_id($model_object_or_id) |
|
289 | - { |
|
290 | - $model_object_id = $model_object_or_id; |
|
291 | - if ($model_object_or_id instanceof EE_Base_Class) { |
|
292 | - $model_object_id = $model_object_or_id->ID(); |
|
293 | - } |
|
294 | - if (! $model_object_id) { |
|
295 | - throw new EE_Error(sprintf(__("Sorry, we cant get the related %s model objects to %s model object before it has an ID. You can solve that by just saving it before trying to get its related model objects", |
|
296 | - "event_espresso"), $this->get_other_model()->get_this_model_name(), |
|
297 | - $this->get_this_model()->get_this_model_name())); |
|
298 | - } |
|
299 | - return $model_object_id; |
|
300 | - } |
|
301 | - |
|
302 | - |
|
303 | - /** |
|
304 | - * Gets the SQL string for performing the join between this model and the other model. |
|
305 | - * |
|
306 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
307 | - * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
308 | - * other_model_primary_table.fk" etc |
|
309 | - */ |
|
310 | - abstract public function get_join_statement($model_relation_chain); |
|
311 | - |
|
312 | - |
|
313 | - /** |
|
314 | - * Adds a relationships between the two model objects provided. Each type of relationship handles this differently |
|
315 | - * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this |
|
316 | - * relationship only allows this model to be related to a single other model of this type) |
|
317 | - * |
|
318 | - * @param $this_obj_or_id |
|
319 | - * @param $other_obj_or_id |
|
320 | - * @param array $extra_join_model_fields_n_values |
|
321 | - * @return \EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for |
|
322 | - * $other_obj_or_id) |
|
323 | - */ |
|
324 | - abstract public function add_relation_to( |
|
325 | - $this_obj_or_id, |
|
326 | - $other_obj_or_id, |
|
327 | - $extra_join_model_fields_n_values = array() |
|
328 | - ); |
|
329 | - |
|
330 | - |
|
331 | - /** |
|
332 | - * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two |
|
333 | - * model objects |
|
334 | - * |
|
335 | - * @param $this_obj_or_id |
|
336 | - * @param $other_obj_or_id |
|
337 | - * @param array $where_query |
|
338 | - * @return bool |
|
339 | - */ |
|
340 | - abstract public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()); |
|
341 | - |
|
342 | - |
|
343 | - /** |
|
344 | - * Removes ALL relation instances for this relation obj |
|
345 | - * |
|
346 | - * @param EE_Base_Class|int $this_obj_or_id |
|
347 | - * @param array $where_query_param like EEM_Base::get_all's $query_params[0] (where conditions) |
|
348 | - * @return EE_Base_Class[] |
|
349 | - * @throws \EE_Error |
|
350 | - */ |
|
351 | - public function remove_relations($this_obj_or_id, $where_query_param = array()) |
|
352 | - { |
|
353 | - $related_things = $this->get_all_related($this_obj_or_id, array($where_query_param)); |
|
354 | - $objs_removed = array(); |
|
355 | - foreach ($related_things as $related_thing) { |
|
356 | - $objs_removed[] = $this->remove_relation_to($this_obj_or_id, $related_thing); |
|
357 | - } |
|
358 | - return $objs_removed; |
|
359 | - } |
|
360 | - |
|
361 | - |
|
362 | - /** |
|
363 | - * If you aren't allowed to delete this model when there are related models across this |
|
364 | - * relation object, return true. Otherwise, if you can delete this model even though |
|
365 | - * related objects exist, returns false. |
|
366 | - * |
|
367 | - * @return boolean |
|
368 | - */ |
|
369 | - public function block_delete_if_related_models_exist() |
|
370 | - { |
|
371 | - return $this->_blocking_delete; |
|
372 | - } |
|
373 | - |
|
374 | - |
|
375 | - /** |
|
376 | - * Gets the error message to show |
|
377 | - * |
|
378 | - * @return string |
|
379 | - */ |
|
380 | - public function get_deletion_error_message() |
|
381 | - { |
|
382 | - if ($this->_blocking_delete_error_message) { |
|
383 | - return $this->_blocking_delete_error_message; |
|
384 | - } else { |
|
20 | + /** |
|
21 | + * The model name of which this relation is a component (ie, the model that called new EE_Model_Relation_Base) |
|
22 | + * |
|
23 | + * @var string eg Event, Question_Group, Registration |
|
24 | + */ |
|
25 | + private $_this_model_name; |
|
26 | + /** |
|
27 | + * The model name pointed to by this relation (ie, the model we want to establish a relationship to) |
|
28 | + * |
|
29 | + * @var string eg Event, Question_Group, Registration |
|
30 | + */ |
|
31 | + private $_other_model_name; |
|
32 | + |
|
33 | + /** |
|
34 | + * this is typically used when calling the relation models to make sure they inherit any set timezone from the |
|
35 | + * initiating model. |
|
36 | + * |
|
37 | + * @var string |
|
38 | + */ |
|
39 | + protected $_timezone; |
|
40 | + |
|
41 | + /** |
|
42 | + * If you try to delete "this_model", and there are related "other_models", |
|
43 | + * and this isn't null, then abandon the deletion and add this warning. |
|
44 | + * This effectively makes it impossible to delete "this_model" while there are |
|
45 | + * related "other_models" along this relation. |
|
46 | + * |
|
47 | + * @var string (internationalized) |
|
48 | + */ |
|
49 | + protected $_blocking_delete_error_message; |
|
50 | + |
|
51 | + protected $_blocking_delete = false; |
|
52 | + |
|
53 | + /** |
|
54 | + * Object representing the relationship between two models. This knows how to join the models, |
|
55 | + * get related models across the relation, and add-and-remove the relationships. |
|
56 | + * |
|
57 | + * @param boolean $block_deletes if there are related models across this relation, block (prevent |
|
58 | + * and add an error) the deletion of this model |
|
59 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
60 | + * default |
|
61 | + */ |
|
62 | + public function __construct($block_deletes, $blocking_delete_error_message) |
|
63 | + { |
|
64 | + $this->_blocking_delete = $block_deletes; |
|
65 | + $this->_blocking_delete_error_message = $blocking_delete_error_message; |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * @param $this_model_name |
|
71 | + * @param $other_model_name |
|
72 | + * @throws EE_Error |
|
73 | + */ |
|
74 | + public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
75 | + { |
|
76 | + $this->_this_model_name = $this_model_name; |
|
77 | + $this->_other_model_name = $other_model_name; |
|
78 | + if (is_string($this->_blocking_delete)) { |
|
79 | + throw new EE_Error(sprintf(__("When instantiating the relation of type %s from %s to %s, the \$block_deletes argument should be a boolean, not a string (%s)", |
|
80 | + "event_espresso"), |
|
81 | + get_class($this), $this_model_name, $other_model_name, $this->_blocking_delete)); |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + |
|
86 | + /** |
|
87 | + * Gets the model where this relation is defined. |
|
88 | + * |
|
89 | + * @return EEM_Base |
|
90 | + */ |
|
91 | + public function get_this_model() |
|
92 | + { |
|
93 | + return $this->_get_model($this->_this_model_name); |
|
94 | + } |
|
95 | + |
|
96 | + |
|
97 | + /** |
|
98 | + * Gets the model which this relation establishes the relation TO (ie, |
|
99 | + * this relation object was defined on get_this_model(), get_other_model() is the other one) |
|
100 | + * |
|
101 | + * @return EEM_Base |
|
102 | + */ |
|
103 | + public function get_other_model() |
|
104 | + { |
|
105 | + return $this->_get_model($this->_other_model_name); |
|
106 | + } |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * Internally used by get_this_model() and get_other_model() |
|
111 | + * |
|
112 | + * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
|
113 | + * @return EEM_Base |
|
114 | + */ |
|
115 | + protected function _get_model($model_name) |
|
116 | + { |
|
117 | + $modelInstance = EE_Registry::instance()->load_model($model_name); |
|
118 | + $modelInstance->set_timezone($this->_timezone); |
|
119 | + return $modelInstance; |
|
120 | + } |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * entirely possible that relations may be called from a model and we need to make sure those relations have their |
|
125 | + * timezone set correctly. |
|
126 | + * |
|
127 | + * @param string $timezone timezone to set. |
|
128 | + */ |
|
129 | + public function set_timezone($timezone) |
|
130 | + { |
|
131 | + if ($timezone !== null) { |
|
132 | + $this->_timezone = $timezone; |
|
133 | + } |
|
134 | + } |
|
135 | + |
|
136 | + |
|
137 | + /** |
|
138 | + * @param $other_table |
|
139 | + * @param $other_table_alias |
|
140 | + * @param $other_table_column |
|
141 | + * @param $this_table_alias |
|
142 | + * @param $this_table_join_column |
|
143 | + * @param string $extra_join_sql |
|
144 | + * @return string |
|
145 | + */ |
|
146 | + protected function _left_join( |
|
147 | + $other_table, |
|
148 | + $other_table_alias, |
|
149 | + $other_table_column, |
|
150 | + $this_table_alias, |
|
151 | + $this_table_join_column, |
|
152 | + $extra_join_sql = '' |
|
153 | + ) { |
|
154 | + return " LEFT JOIN " . $other_table . " AS " . $other_table_alias . " ON " . $other_table_alias . "." . $other_table_column . "=" . $this_table_alias . "." . $this_table_join_column . ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
155 | + } |
|
156 | + |
|
157 | + |
|
158 | + /** |
|
159 | + * Gets all the model objects of type of other model related to $model_object, |
|
160 | + * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation. |
|
161 | + * For both of those child classes, $model_object must be saved so that it has an ID before querying, |
|
162 | + * otherwise an error will be thrown. Note: by default we disable default_where_conditions |
|
163 | + * EE_Belongs_To_Relation doesn't need to be saved before querying. |
|
164 | + * |
|
165 | + * @param EE_Base_Class|int $model_object_or_id or the primary key of this model |
|
166 | + * @param array $query_params like EEM_Base::get_all's $query_params |
|
167 | + * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
168 | + * @return EE_Base_Class[] |
|
169 | + * @throws \EE_Error |
|
170 | + */ |
|
171 | + public function get_all_related( |
|
172 | + $model_object_or_id, |
|
173 | + $query_params = array(), |
|
174 | + $values_already_prepared_by_model_object = false |
|
175 | + ) { |
|
176 | + if ($values_already_prepared_by_model_object !== false) { |
|
177 | + EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related', |
|
178 | + __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
179 | + '4.8.1'); |
|
180 | + } |
|
181 | + $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
182 | + $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() |
|
183 | + . "." |
|
184 | + . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
185 | + $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
186 | + $query_params[0][$query_param_where_this_model_pk] = $model_object_id; |
|
187 | + return $this->get_other_model()->get_all($query_params); |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + /** |
|
192 | + * Alters the $query_params to disable default where conditions, unless otherwise specified |
|
193 | + * |
|
194 | + * @param string $query_params |
|
195 | + * @return array |
|
196 | + */ |
|
197 | + protected function _disable_default_where_conditions_on_query_param($query_params) |
|
198 | + { |
|
199 | + if (! isset($query_params['default_where_conditions'])) { |
|
200 | + $query_params['default_where_conditions'] = 'none'; |
|
201 | + } |
|
202 | + return $query_params; |
|
203 | + } |
|
204 | + |
|
205 | + |
|
206 | + /** |
|
207 | + * Deletes the related model objects which meet the query parameters. If no |
|
208 | + * parameters are specified, then all related model objects will be deleted. |
|
209 | + * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
210 | + * model objects will only be soft-deleted. |
|
211 | + * |
|
212 | + * @param EE_Base_Class|int|string $model_object_or_id |
|
213 | + * @param array $query_params |
|
214 | + * @return int of how many related models got deleted |
|
215 | + * @throws \EE_Error |
|
216 | + */ |
|
217 | + public function delete_all_related($model_object_or_id, $query_params = array()) |
|
218 | + { |
|
219 | + //for each thing we would delete, |
|
220 | + $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
221 | + //determine if it's blocked by anything else before it can be deleted |
|
222 | + $deleted_count = 0; |
|
223 | + foreach ($related_model_objects as $related_model_object) { |
|
224 | + $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object, |
|
225 | + $model_object_or_id); |
|
226 | + /* @var $model_object_or_id EE_Base_Class */ |
|
227 | + if (! $delete_is_blocked) { |
|
228 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
229 | + $related_model_object->delete(); |
|
230 | + $deleted_count++; |
|
231 | + } |
|
232 | + } |
|
233 | + return $deleted_count; |
|
234 | + } |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * Deletes the related model objects which meet the query parameters. If no |
|
239 | + * parameters are specified, then all related model objects will be deleted. |
|
240 | + * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
241 | + * model objects will only be soft-deleted. |
|
242 | + * |
|
243 | + * @param EE_Base_Class|int|string $model_object_or_id |
|
244 | + * @param array $query_params |
|
245 | + * @return int of how many related models got deleted |
|
246 | + * @throws \EE_Error |
|
247 | + */ |
|
248 | + public function delete_related_permanently($model_object_or_id, $query_params = array()) |
|
249 | + { |
|
250 | + //for each thing we would delete, |
|
251 | + $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
252 | + //determine if it's blocked by anything else before it can be deleted |
|
253 | + $deleted_count = 0; |
|
254 | + foreach ($related_model_objects as $related_model_object) { |
|
255 | + $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object, |
|
256 | + $model_object_or_id); |
|
257 | + /* @var $model_object_or_id EE_Base_Class */ |
|
258 | + if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
|
259 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
260 | + $deleted_count++; |
|
261 | + if (! $delete_is_blocked) { |
|
262 | + $related_model_object->delete_permanently(); |
|
263 | + } else { |
|
264 | + //delete is blocked |
|
265 | + //brent and darren, in this case, wanted to just soft delete it then |
|
266 | + $related_model_object->delete(); |
|
267 | + } |
|
268 | + } else { |
|
269 | + //its not a soft-deletable thing anyways. do the normal logic. |
|
270 | + if (! $delete_is_blocked) { |
|
271 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
272 | + $related_model_object->delete(); |
|
273 | + $deleted_count++; |
|
274 | + } |
|
275 | + } |
|
276 | + } |
|
277 | + return $deleted_count; |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + /** |
|
282 | + * this just returns a model_object_id for incoming item that could be an object or id. |
|
283 | + * |
|
284 | + * @param EE_Base_Class|int $model_object_or_id model object or the primary key of this model |
|
285 | + * @throws EE_Error |
|
286 | + * @return int |
|
287 | + */ |
|
288 | + protected function _get_model_object_id($model_object_or_id) |
|
289 | + { |
|
290 | + $model_object_id = $model_object_or_id; |
|
291 | + if ($model_object_or_id instanceof EE_Base_Class) { |
|
292 | + $model_object_id = $model_object_or_id->ID(); |
|
293 | + } |
|
294 | + if (! $model_object_id) { |
|
295 | + throw new EE_Error(sprintf(__("Sorry, we cant get the related %s model objects to %s model object before it has an ID. You can solve that by just saving it before trying to get its related model objects", |
|
296 | + "event_espresso"), $this->get_other_model()->get_this_model_name(), |
|
297 | + $this->get_this_model()->get_this_model_name())); |
|
298 | + } |
|
299 | + return $model_object_id; |
|
300 | + } |
|
301 | + |
|
302 | + |
|
303 | + /** |
|
304 | + * Gets the SQL string for performing the join between this model and the other model. |
|
305 | + * |
|
306 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
307 | + * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
308 | + * other_model_primary_table.fk" etc |
|
309 | + */ |
|
310 | + abstract public function get_join_statement($model_relation_chain); |
|
311 | + |
|
312 | + |
|
313 | + /** |
|
314 | + * Adds a relationships between the two model objects provided. Each type of relationship handles this differently |
|
315 | + * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this |
|
316 | + * relationship only allows this model to be related to a single other model of this type) |
|
317 | + * |
|
318 | + * @param $this_obj_or_id |
|
319 | + * @param $other_obj_or_id |
|
320 | + * @param array $extra_join_model_fields_n_values |
|
321 | + * @return \EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for |
|
322 | + * $other_obj_or_id) |
|
323 | + */ |
|
324 | + abstract public function add_relation_to( |
|
325 | + $this_obj_or_id, |
|
326 | + $other_obj_or_id, |
|
327 | + $extra_join_model_fields_n_values = array() |
|
328 | + ); |
|
329 | + |
|
330 | + |
|
331 | + /** |
|
332 | + * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two |
|
333 | + * model objects |
|
334 | + * |
|
335 | + * @param $this_obj_or_id |
|
336 | + * @param $other_obj_or_id |
|
337 | + * @param array $where_query |
|
338 | + * @return bool |
|
339 | + */ |
|
340 | + abstract public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()); |
|
341 | + |
|
342 | + |
|
343 | + /** |
|
344 | + * Removes ALL relation instances for this relation obj |
|
345 | + * |
|
346 | + * @param EE_Base_Class|int $this_obj_or_id |
|
347 | + * @param array $where_query_param like EEM_Base::get_all's $query_params[0] (where conditions) |
|
348 | + * @return EE_Base_Class[] |
|
349 | + * @throws \EE_Error |
|
350 | + */ |
|
351 | + public function remove_relations($this_obj_or_id, $where_query_param = array()) |
|
352 | + { |
|
353 | + $related_things = $this->get_all_related($this_obj_or_id, array($where_query_param)); |
|
354 | + $objs_removed = array(); |
|
355 | + foreach ($related_things as $related_thing) { |
|
356 | + $objs_removed[] = $this->remove_relation_to($this_obj_or_id, $related_thing); |
|
357 | + } |
|
358 | + return $objs_removed; |
|
359 | + } |
|
360 | + |
|
361 | + |
|
362 | + /** |
|
363 | + * If you aren't allowed to delete this model when there are related models across this |
|
364 | + * relation object, return true. Otherwise, if you can delete this model even though |
|
365 | + * related objects exist, returns false. |
|
366 | + * |
|
367 | + * @return boolean |
|
368 | + */ |
|
369 | + public function block_delete_if_related_models_exist() |
|
370 | + { |
|
371 | + return $this->_blocking_delete; |
|
372 | + } |
|
373 | + |
|
374 | + |
|
375 | + /** |
|
376 | + * Gets the error message to show |
|
377 | + * |
|
378 | + * @return string |
|
379 | + */ |
|
380 | + public function get_deletion_error_message() |
|
381 | + { |
|
382 | + if ($this->_blocking_delete_error_message) { |
|
383 | + return $this->_blocking_delete_error_message; |
|
384 | + } else { |
|
385 | 385 | // return sprintf(__('Cannot delete %1$s when there are related %2$s', "event_espresso"),$this->get_this_model()->item_name(2),$this->get_other_model()->item_name(2)); |
386 | - return sprintf( |
|
387 | - __('This %1$s is currently linked to one or more %2$s records. If this %1$s is incorrect, then please remove it from all %3$s before attempting to delete it.', |
|
388 | - "event_espresso"), |
|
389 | - $this->get_this_model()->item_name(1), |
|
390 | - $this->get_other_model()->item_name(1), |
|
391 | - $this->get_other_model()->item_name(2) |
|
392 | - ); |
|
393 | - } |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Returns whatever is set as the nicename for the object. |
|
398 | - * |
|
399 | - * @return string |
|
400 | - */ |
|
401 | - public function getSchemaDescription() |
|
402 | - { |
|
403 | - $description = $this instanceof EE_Belongs_To_Relation |
|
404 | - ? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso') |
|
405 | - : esc_html__('The related %1$s entities to the %2$s.', 'event_espresso'); |
|
406 | - return sprintf( |
|
407 | - $description, |
|
408 | - $this->get_other_model()->get_this_model_name(), |
|
409 | - $this->get_this_model()->get_this_model_name() |
|
410 | - ); |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * Returns whatever is set as the $_schema_type property for the object. |
|
415 | - * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
416 | - * |
|
417 | - * @return string|array |
|
418 | - */ |
|
419 | - public function getSchemaType() |
|
420 | - { |
|
421 | - return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array'; |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
426 | - * this method and return the properties for the schema. |
|
427 | - * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
428 | - * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
429 | - * |
|
430 | - * @return array |
|
431 | - */ |
|
432 | - public function getSchemaProperties() |
|
433 | - { |
|
434 | - return array(); |
|
435 | - } |
|
436 | - |
|
437 | - /** |
|
438 | - * If a child class has enum values, they should override this method and provide a simple array |
|
439 | - * of the enum values. |
|
440 | - * The reason this is not a property on the class is because there may be filterable enum values that |
|
441 | - * are set on the instantiated object that could be filtered after construct. |
|
442 | - * |
|
443 | - * @return array |
|
444 | - */ |
|
445 | - public function getSchemaEnum() |
|
446 | - { |
|
447 | - return array(); |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * This returns the value of the $_schema_format property on the object. |
|
452 | - * |
|
453 | - * @return string |
|
454 | - */ |
|
455 | - public function getSchemaFormat() |
|
456 | - { |
|
457 | - return array(); |
|
458 | - } |
|
459 | - |
|
460 | - /** |
|
461 | - * This returns the value of the $_schema_readonly property on the object. |
|
462 | - * |
|
463 | - * @return bool |
|
464 | - */ |
|
465 | - public function getSchemaReadonly() |
|
466 | - { |
|
467 | - return true; |
|
468 | - } |
|
469 | - |
|
470 | - /** |
|
471 | - * This returns elements used to represent this field in the json schema. |
|
472 | - * |
|
473 | - * @link http://json-schema.org/ |
|
474 | - * @return array |
|
475 | - */ |
|
476 | - public function getSchema() |
|
477 | - { |
|
478 | - $schema = array( |
|
479 | - 'description' => $this->getSchemaDescription(), |
|
480 | - 'type' => $this->getSchemaType(), |
|
481 | - 'relation' => true, |
|
482 | - 'relation_type' => get_class($this), |
|
483 | - 'readonly' => $this->getSchemaReadonly() |
|
484 | - ); |
|
485 | - |
|
486 | - if ($this instanceof EE_HABTM_Relation) { |
|
487 | - $schema['joining_model_name'] = $this->get_join_model()->get_this_model_name(); |
|
488 | - } |
|
489 | - |
|
490 | - if ($this->getSchemaType() === 'array') { |
|
491 | - $schema['items'] = array( |
|
492 | - 'type' => 'object' |
|
493 | - ); |
|
494 | - } |
|
495 | - |
|
496 | - return $schema; |
|
497 | - } |
|
386 | + return sprintf( |
|
387 | + __('This %1$s is currently linked to one or more %2$s records. If this %1$s is incorrect, then please remove it from all %3$s before attempting to delete it.', |
|
388 | + "event_espresso"), |
|
389 | + $this->get_this_model()->item_name(1), |
|
390 | + $this->get_other_model()->item_name(1), |
|
391 | + $this->get_other_model()->item_name(2) |
|
392 | + ); |
|
393 | + } |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Returns whatever is set as the nicename for the object. |
|
398 | + * |
|
399 | + * @return string |
|
400 | + */ |
|
401 | + public function getSchemaDescription() |
|
402 | + { |
|
403 | + $description = $this instanceof EE_Belongs_To_Relation |
|
404 | + ? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso') |
|
405 | + : esc_html__('The related %1$s entities to the %2$s.', 'event_espresso'); |
|
406 | + return sprintf( |
|
407 | + $description, |
|
408 | + $this->get_other_model()->get_this_model_name(), |
|
409 | + $this->get_this_model()->get_this_model_name() |
|
410 | + ); |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * Returns whatever is set as the $_schema_type property for the object. |
|
415 | + * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
416 | + * |
|
417 | + * @return string|array |
|
418 | + */ |
|
419 | + public function getSchemaType() |
|
420 | + { |
|
421 | + return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array'; |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
426 | + * this method and return the properties for the schema. |
|
427 | + * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
428 | + * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
429 | + * |
|
430 | + * @return array |
|
431 | + */ |
|
432 | + public function getSchemaProperties() |
|
433 | + { |
|
434 | + return array(); |
|
435 | + } |
|
436 | + |
|
437 | + /** |
|
438 | + * If a child class has enum values, they should override this method and provide a simple array |
|
439 | + * of the enum values. |
|
440 | + * The reason this is not a property on the class is because there may be filterable enum values that |
|
441 | + * are set on the instantiated object that could be filtered after construct. |
|
442 | + * |
|
443 | + * @return array |
|
444 | + */ |
|
445 | + public function getSchemaEnum() |
|
446 | + { |
|
447 | + return array(); |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * This returns the value of the $_schema_format property on the object. |
|
452 | + * |
|
453 | + * @return string |
|
454 | + */ |
|
455 | + public function getSchemaFormat() |
|
456 | + { |
|
457 | + return array(); |
|
458 | + } |
|
459 | + |
|
460 | + /** |
|
461 | + * This returns the value of the $_schema_readonly property on the object. |
|
462 | + * |
|
463 | + * @return bool |
|
464 | + */ |
|
465 | + public function getSchemaReadonly() |
|
466 | + { |
|
467 | + return true; |
|
468 | + } |
|
469 | + |
|
470 | + /** |
|
471 | + * This returns elements used to represent this field in the json schema. |
|
472 | + * |
|
473 | + * @link http://json-schema.org/ |
|
474 | + * @return array |
|
475 | + */ |
|
476 | + public function getSchema() |
|
477 | + { |
|
478 | + $schema = array( |
|
479 | + 'description' => $this->getSchemaDescription(), |
|
480 | + 'type' => $this->getSchemaType(), |
|
481 | + 'relation' => true, |
|
482 | + 'relation_type' => get_class($this), |
|
483 | + 'readonly' => $this->getSchemaReadonly() |
|
484 | + ); |
|
485 | + |
|
486 | + if ($this instanceof EE_HABTM_Relation) { |
|
487 | + $schema['joining_model_name'] = $this->get_join_model()->get_this_model_name(); |
|
488 | + } |
|
489 | + |
|
490 | + if ($this->getSchemaType() === 'array') { |
|
491 | + $schema['items'] = array( |
|
492 | + 'type' => 'object' |
|
493 | + ); |
|
494 | + } |
|
495 | + |
|
496 | + return $schema; |
|
497 | + } |
|
498 | 498 | } |
@@ -127,9 +127,9 @@ discard block |
||
127 | 127 | protected $_table_analysis; |
128 | 128 | |
129 | 129 | /** |
130 | - * @var EE_Data_Migration_Manager $_instance |
|
130 | + * @var EE_Data_Migration_Manager $_instance |
|
131 | 131 | * @access private |
132 | - */ |
|
132 | + */ |
|
133 | 133 | private static $_instance = NULL; |
134 | 134 | |
135 | 135 | |
@@ -720,18 +720,18 @@ discard block |
||
720 | 720 | |
721 | 721 | |
722 | 722 | |
723 | - /** |
|
724 | - * Gets all the data migration scripts available in the core folder and folders |
|
725 | - * in addons. Has the side effect of adding them for autoloading |
|
726 | - * |
|
727 | - * @return array keys are expected classnames, values are their filepaths |
|
728 | - * @throws \EE_Error |
|
729 | - */ |
|
723 | + /** |
|
724 | + * Gets all the data migration scripts available in the core folder and folders |
|
725 | + * in addons. Has the side effect of adding them for autoloading |
|
726 | + * |
|
727 | + * @return array keys are expected classnames, values are their filepaths |
|
728 | + * @throws \EE_Error |
|
729 | + */ |
|
730 | 730 | public function get_all_data_migration_scripts_available(){ |
731 | 731 | if( ! $this->_data_migration_class_to_filepath_map){ |
732 | 732 | $this->_data_migration_class_to_filepath_map = array(); |
733 | 733 | foreach($this->get_data_migration_script_folders() as $folder_path){ |
734 | - $folder_path = EEH_File::end_with_directory_separator($folder_path); |
|
734 | + $folder_path = EEH_File::end_with_directory_separator($folder_path); |
|
735 | 735 | $files = glob( $folder_path. '*.dms.php' ); |
736 | 736 | |
737 | 737 | if ( empty( $files ) ) { |
@@ -748,15 +748,15 @@ discard block |
||
748 | 748 | if( $slug !== 'Core' ){ |
749 | 749 | if( ! EE_Registry::instance()->get_addon_by_name( $slug ) ) { |
750 | 750 | EE_Error::doing_it_wrong( |
751 | - __FUNCTION__, |
|
752 | - sprintf( |
|
753 | - __( 'The data migration script "%s" migrates the "%s" data, but there is no EE addon with that name. There is only: %s. ', 'event_espresso' ), |
|
754 | - $classname, |
|
755 | - $slug, |
|
756 | - implode(', ', array_keys( EE_Registry::instance()->get_addons_by_name() ) ) |
|
757 | - ), |
|
758 | - '4.3.0.alpha.019' |
|
759 | - ); |
|
751 | + __FUNCTION__, |
|
752 | + sprintf( |
|
753 | + __( 'The data migration script "%s" migrates the "%s" data, but there is no EE addon with that name. There is only: %s. ', 'event_espresso' ), |
|
754 | + $classname, |
|
755 | + $slug, |
|
756 | + implode(', ', array_keys( EE_Registry::instance()->get_addons_by_name() ) ) |
|
757 | + ), |
|
758 | + '4.3.0.alpha.019' |
|
759 | + ); |
|
760 | 760 | } |
761 | 761 | } |
762 | 762 | $this->_data_migration_class_to_filepath_map[$classname] = $file; |
@@ -939,12 +939,12 @@ discard block |
||
939 | 939 | } |
940 | 940 | |
941 | 941 | /** |
942 | - * Resets the borked data migration scripts so they're no longer borked |
|
943 | - * so we can again attempt to migrate |
|
944 | - * |
|
945 | - * @return bool |
|
946 | - * @throws \EE_Error |
|
947 | - */ |
|
942 | + * Resets the borked data migration scripts so they're no longer borked |
|
943 | + * so we can again attempt to migrate |
|
944 | + * |
|
945 | + * @return bool |
|
946 | + * @throws \EE_Error |
|
947 | + */ |
|
948 | 948 | public function reattempt(){ |
949 | 949 | //find if the last-ran script was borked |
950 | 950 | //set it as being non-borked (we shouldn't ever get DMSs that we don't recognize) |
@@ -208,7 +208,7 @@ discard block |
||
208 | 208 | //it's 4.2-style.eg Core.4.1.0 |
209 | 209 | $plugin_slug = $parts[0];//eg Core |
210 | 210 | $version_string = $parts[1].".".$parts[2].".".$parts[3]; //eg 4.1.0 |
211 | - }else{ |
|
211 | + } else{ |
|
212 | 212 | //it's 4.1-style: eg 4.1.0 |
213 | 213 | $plugin_slug = 'Core'; |
214 | 214 | $version_string = $plugin_slug_and_version_string;//eg 4.1.0 |
@@ -231,12 +231,12 @@ discard block |
||
231 | 231 | if($class instanceof EE_Data_Migration_Script_Base){ |
232 | 232 | $class->instantiate_from_array_of_properties($data_migration_data); |
233 | 233 | return $class; |
234 | - }else{ |
|
234 | + } else{ |
|
235 | 235 | //huh, so its an object but not a data migration script?? that shouldn't happen |
236 | 236 | //just leave it as an array (which will probably just get ignored) |
237 | 237 | throw new EE_Error(sprintf(__("Trying to retrieve DMS class from wp option. No DMS by the name '%s' exists", 'event_espresso'),$data_migration_data['class'])); |
238 | 238 | } |
239 | - }else{ |
|
239 | + } else{ |
|
240 | 240 | //so the data doesn't specify a class. So it must either be a legacy array of info or some array (which we'll probably just ignore), or a class that no longer exists |
241 | 241 | throw new EE_Error(sprintf(__("The wp option with key '%s' does not represent a DMS", 'event_espresso'),$dms_option_name)); |
242 | 242 | } |
@@ -266,7 +266,7 @@ discard block |
||
266 | 266 | //sometimes we also like to know which was the last incomplete script (or if there are any at all) |
267 | 267 | $this->_last_ran_incomplete_script = $class; |
268 | 268 | } |
269 | - }catch(EE_Error $e){ |
|
269 | + } catch(EE_Error $e){ |
|
270 | 270 | //ok so its not a DMS. We'll just keep it, although other code will need to expect non-DMSs |
271 | 271 | $data_migrations_ran[$plugin_slug][$version_string] = maybe_unserialize($data_migration_option['option_value']); |
272 | 272 | } |
@@ -367,7 +367,7 @@ discard block |
||
367 | 367 | //so we want the data migrations to happen. SO, we're going to say the DB is at that state |
368 | 368 | // echo "4.1.0 is greater than $previous_version_installed! update the option"; |
369 | 369 | $db_state = array('Core'=>$previous_version_installed); |
370 | - }else{ |
|
370 | + } else{ |
|
371 | 371 | // echo "4.1.0 is SMALLER than $previous_version_installed"; |
372 | 372 | $db_state = array('Core'=>EVENT_ESPRESSO_VERSION); |
373 | 373 | } |
@@ -435,10 +435,10 @@ discard block |
||
435 | 435 | $migrates_to_version = $script->migrates_to_version(); |
436 | 436 | $next_database_state_to_consider[ $migrates_to_version[ 'slug' ] ] = $migrates_to_version[ 'version' ]; |
437 | 437 | unset($script_class_and_filepaths_available[$classname]); |
438 | - }else{ |
|
438 | + } else{ |
|
439 | 439 | //it must have a status that indicates it has finished, so we don't want to try and run it again |
440 | 440 | } |
441 | - }else{ |
|
441 | + } else{ |
|
442 | 442 | //it exists but it's not a proper data migration script |
443 | 443 | //maybe the script got renamed? or was simply removed from EE? |
444 | 444 | //either way, its certainly not runnable! |
@@ -478,7 +478,7 @@ discard block |
||
478 | 478 | } |
479 | 479 | if($include_completed_scripts){ |
480 | 480 | return $this->_last_ran_script; |
481 | - }else{ |
|
481 | + } else{ |
|
482 | 482 | return $this->_last_ran_incomplete_script; |
483 | 483 | } |
484 | 484 | } |
@@ -539,7 +539,7 @@ discard block |
||
539 | 539 | $this->_data_migrations_ran[$plugin_slug][$version] = $currently_executing_script; |
540 | 540 | } |
541 | 541 | $current_script_name = get_class($currently_executing_script); |
542 | - }catch(Exception $e){ |
|
542 | + } catch(Exception $e){ |
|
543 | 543 | //an exception occurred while trying to get migration scripts |
544 | 544 | |
545 | 545 | $message = sprintf( __("Error Message: %sStack Trace:%s", "event_espresso"), $e->getMessage() . '<br>', $e->getTraceAsString() ); |
@@ -604,14 +604,14 @@ discard block |
||
604 | 604 | ); |
605 | 605 | break; |
606 | 606 | } |
607 | - }catch(Exception $e){ |
|
607 | + } catch(Exception $e){ |
|
608 | 608 | //ok so some exception was thrown which killed the data migration script |
609 | 609 | //double-check we have a real script |
610 | 610 | if($currently_executing_script instanceof EE_Data_Migration_Script_Base){ |
611 | 611 | $script_name = $currently_executing_script->pretty_name(); |
612 | 612 | $currently_executing_script->set_broken(); |
613 | 613 | $currently_executing_script->add_error($e->getMessage()); |
614 | - }else{ |
|
614 | + } else{ |
|
615 | 615 | $script_name = __("Error getting Migration Script", "event_espresso"); |
616 | 616 | } |
617 | 617 | $response_array = array( |
@@ -654,7 +654,7 @@ discard block |
||
654 | 654 | ob_start(); |
655 | 655 | try{ |
656 | 656 | $response = $this->migration_step(); |
657 | - }catch(Exception $e){ |
|
657 | + } catch(Exception $e){ |
|
658 | 658 | $response = array( |
659 | 659 | 'records_to_migrate'=>0, |
660 | 660 | 'records_migrated'=>0, |
@@ -703,7 +703,7 @@ discard block |
||
703 | 703 | $current_database_state = get_option(self::current_database_state); |
704 | 704 | if( ! isset( $current_database_state[ $slug ] ) ) { |
705 | 705 | return true; |
706 | - }else{ |
|
706 | + } else{ |
|
707 | 707 | //just compare the first 3 parts of version string, eg "4.7.1", not "4.7.1.dev.032" because DBs shouldn't change on nano version changes |
708 | 708 | $version_parts_current_db_state = array_slice( explode('.', $current_database_state[ $slug ] ), 0, 3); |
709 | 709 | $version_parts_of_provided_db_state = array_slice( explode( '.', $version ), 0, 3 ); |
@@ -803,7 +803,7 @@ discard block |
||
803 | 803 | //ok then just add this error to its list of errors |
804 | 804 | $last_ran_migration_script_properties['_errors'][] = $error_message; |
805 | 805 | $last_ran_migration_script_properties['_status'] = self::status_fatal_error; |
806 | - }else{ |
|
806 | + } else{ |
|
807 | 807 | //so we don't even know which script was last running |
808 | 808 | //use the data migration error stub, which is designed specifically for this type of thing |
809 | 809 | $general_migration_error = new EE_DMS_Unknown_1_0_0(); |
@@ -839,7 +839,7 @@ discard block |
||
839 | 839 | if( $old_option_value != $script_array_for_saving){ |
840 | 840 | $successful_updates = update_option($option_name,$script_array_for_saving); |
841 | 841 | } |
842 | - }else{//we don't know what this array-thing is. So just save it as-is |
|
842 | + } else{//we don't know what this array-thing is. So just save it as-is |
|
843 | 843 | // $array_of_migrations[$version_string] = $array_or_migration_obj; |
844 | 844 | if($old_option_value != $array_or_migration_obj){ |
845 | 845 | $successful_updates = update_option($option_name,$array_or_migration_obj); |
@@ -906,7 +906,7 @@ discard block |
||
906 | 906 | } |
907 | 907 | //if it wasn't for core, we must keep searching for one that is! |
908 | 908 | continue; |
909 | - }else{ |
|
909 | + } else{ |
|
910 | 910 | $champion_migrates_to= $this->script_migrates_to_version($most_up_to_date_dms_classname); |
911 | 911 | $contender_migrates_to = $this->script_migrates_to_version($classname); |
912 | 912 | if($contender_migrates_to[ 'slug' ] == $plugin_slug && version_compare($champion_migrates_to[ 'version' ], $contender_migrates_to[ 'version' ], '<')){ |
@@ -933,7 +933,7 @@ discard block |
||
933 | 933 | $migrations_ran = $this->get_data_migrations_ran(); |
934 | 934 | if( isset( $migrations_ran[ $plugin_slug ] ) && isset( $migrations_ran[ $plugin_slug ][ $version ] ) ){ |
935 | 935 | return $migrations_ran[ $plugin_slug ][ $version ]; |
936 | - }else{ |
|
936 | + } else{ |
|
937 | 937 | return NULL; |
938 | 938 | } |
939 | 939 | } |
@@ -955,9 +955,9 @@ discard block |
||
955 | 955 | if( $last_ran_script instanceof EE_DMS_Unknown_1_0_0 ){ |
956 | 956 | //if it was an error DMS, just mark it as complete (if another error occurs it will overwrite it) |
957 | 957 | $last_ran_script->set_completed(); |
958 | - }elseif( $last_ran_script instanceof EE_Data_Migration_Script_Base ) { |
|
958 | + } elseif( $last_ran_script instanceof EE_Data_Migration_Script_Base ) { |
|
959 | 959 | $last_ran_script->reattempt(); |
960 | - }else{ |
|
960 | + } else{ |
|
961 | 961 | throw new EE_Error( sprintf( __( 'Unable to reattempt the last ran migration script because it was not a valid migration script. || It was %s', 'event_espresso' ), print_r( $last_ran_script, true ) ) ); |
962 | 962 | } |
963 | 963 | return $this->_save_migrations_ran(); |
@@ -995,7 +995,7 @@ discard block |
||
995 | 995 | if( ! $most_up_to_date_dms ) { |
996 | 996 | //if there is NO DMS for this plugin, obviously there's no schema to verify anyways |
997 | 997 | $verify_db = false; |
998 | - }else{ |
|
998 | + } else{ |
|
999 | 999 | $most_up_to_date_dms_migrates_to = $this->script_migrates_to_version( $most_up_to_date_dms ); |
1000 | 1000 | $verify_db = $this->database_needs_updating_to( $most_up_to_date_dms_migrates_to ); |
1001 | 1001 | } |
@@ -1004,7 +1004,7 @@ discard block |
||
1004 | 1004 | false, |
1005 | 1005 | $verify_db |
1006 | 1006 | ); |
1007 | - }else{ |
|
1007 | + } else{ |
|
1008 | 1008 | //just loop through the addons to make sure their database is setup |
1009 | 1009 | foreach( EE_Registry::instance()->addons as $addon ) { |
1010 | 1010 | if( $addon->name() == $plugin_slug ) { |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | * of this EE installation. Keys should be the name of the version the script upgraded to |
83 | 83 | * @var EE_Data_Migration_Script_Base[] |
84 | 84 | */ |
85 | - private $_data_migrations_ran =null; |
|
85 | + private $_data_migrations_ran = null; |
|
86 | 86 | /** |
87 | 87 | * The last ran script. It's nice to store this somewhere accessible, as its easiest |
88 | 88 | * to know which was the last run by which is the newest wp option; but in most of the code |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | */ |
143 | 143 | public static function instance() { |
144 | 144 | // check if class object is instantiated |
145 | - if ( ! self::$_instance instanceof EE_Data_Migration_Manager ) { |
|
145 | + if ( ! self::$_instance instanceof EE_Data_Migration_Manager) { |
|
146 | 146 | self::$_instance = new self(); |
147 | 147 | } |
148 | 148 | return self::$_instance; |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | * all new usages of the singleton should be made with Classname::instance()) and returns it |
153 | 153 | * @return EE_Data_Migration_Manager |
154 | 154 | */ |
155 | - public static function reset(){ |
|
155 | + public static function reset() { |
|
156 | 156 | self::$_instance = NULL; |
157 | 157 | return self::instance(); |
158 | 158 | } |
@@ -162,7 +162,7 @@ discard block |
||
162 | 162 | /** |
163 | 163 | * constructor |
164 | 164 | */ |
165 | - private function __construct(){ |
|
165 | + private function __construct() { |
|
166 | 166 | $this->stati_that_indicate_to_continue_migrations = array( |
167 | 167 | self::status_continue, |
168 | 168 | self::status_completed |
@@ -181,13 +181,13 @@ discard block |
||
181 | 181 | ); |
182 | 182 | //make sure we've included the base migration script, because we may need the EE_DMS_Unknown_1_0_0 class |
183 | 183 | //to be defined, because right now it doesn't get autoloaded on its own |
184 | - EE_Registry::instance()->load_core( 'Data_Migration_Class_Base', array(), TRUE ); |
|
185 | - EE_Registry::instance()->load_core( 'Data_Migration_Script_Base', array(), TRUE ); |
|
186 | - EE_Registry::instance()->load_core( 'DMS_Unknown_1_0_0', array(), TRUE ); |
|
187 | - EE_Registry::instance()->load_core( 'Data_Migration_Script_Stage', array(), TRUE ); |
|
188 | - EE_Registry::instance()->load_core( 'Data_Migration_Script_Stage_Table', array(), TRUE ); |
|
189 | - $this->_table_manager = EE_Registry::instance()->create( 'TableManager', array(), true ); |
|
190 | - $this->_table_analysis = EE_Registry::instance()->create( 'TableAnalysis', array(), true ); |
|
184 | + EE_Registry::instance()->load_core('Data_Migration_Class_Base', array(), TRUE); |
|
185 | + EE_Registry::instance()->load_core('Data_Migration_Script_Base', array(), TRUE); |
|
186 | + EE_Registry::instance()->load_core('DMS_Unknown_1_0_0', array(), TRUE); |
|
187 | + EE_Registry::instance()->load_core('Data_Migration_Script_Stage', array(), TRUE); |
|
188 | + EE_Registry::instance()->load_core('Data_Migration_Script_Stage_Table', array(), TRUE); |
|
189 | + $this->_table_manager = EE_Registry::instance()->create('TableManager', array(), true); |
|
190 | + $this->_table_analysis = EE_Registry::instance()->create('TableAnalysis', array(), true); |
|
191 | 191 | } |
192 | 192 | |
193 | 193 | |
@@ -200,21 +200,21 @@ discard block |
||
200 | 200 | * @param string $option_name (see EE_Data_Migration_Manage::_save_migrations_ran() where the option name is set) |
201 | 201 | * @return array where the first item is the plugin slug (eg 'Core','Calendar',etc) and the 2nd is the version of that plugin (eg '4.1.0') |
202 | 202 | */ |
203 | - private function _get_plugin_slug_and_version_string_from_dms_option_name($option_name){ |
|
203 | + private function _get_plugin_slug_and_version_string_from_dms_option_name($option_name) { |
|
204 | 204 | $plugin_slug_and_version_string = str_replace(EE_Data_Migration_Manager::data_migration_script_option_prefix, "", $option_name); |
205 | 205 | //check if $plugin_slug_and_version_string is like '4.1.0' (4.1-style) or 'Core.4.1.0' (4.2-style) |
206 | - $parts = explode(".",$plugin_slug_and_version_string); |
|
206 | + $parts = explode(".", $plugin_slug_and_version_string); |
|
207 | 207 | |
208 | - if(count($parts) == 4){ |
|
208 | + if (count($parts) == 4) { |
|
209 | 209 | //it's 4.2-style.eg Core.4.1.0 |
210 | - $plugin_slug = $parts[0];//eg Core |
|
210 | + $plugin_slug = $parts[0]; //eg Core |
|
211 | 211 | $version_string = $parts[1].".".$parts[2].".".$parts[3]; //eg 4.1.0 |
212 | - }else{ |
|
212 | + } else { |
|
213 | 213 | //it's 4.1-style: eg 4.1.0 |
214 | 214 | $plugin_slug = 'Core'; |
215 | - $version_string = $plugin_slug_and_version_string;//eg 4.1.0 |
|
215 | + $version_string = $plugin_slug_and_version_string; //eg 4.1.0 |
|
216 | 216 | } |
217 | - return array($plugin_slug,$version_string); |
|
217 | + return array($plugin_slug, $version_string); |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | /** |
@@ -225,21 +225,21 @@ discard block |
||
225 | 225 | * @return EE_Data_Migration_Script_Base |
226 | 226 | * @throws EE_Error |
227 | 227 | */ |
228 | - private function _get_dms_class_from_wp_option($dms_option_name,$dms_option_value){ |
|
228 | + private function _get_dms_class_from_wp_option($dms_option_name, $dms_option_value) { |
|
229 | 229 | $data_migration_data = maybe_unserialize($dms_option_value); |
230 | - if(isset($data_migration_data['class']) && class_exists($data_migration_data['class'])){ |
|
230 | + if (isset($data_migration_data['class']) && class_exists($data_migration_data['class'])) { |
|
231 | 231 | $class = new $data_migration_data['class']; |
232 | - if($class instanceof EE_Data_Migration_Script_Base){ |
|
232 | + if ($class instanceof EE_Data_Migration_Script_Base) { |
|
233 | 233 | $class->instantiate_from_array_of_properties($data_migration_data); |
234 | 234 | return $class; |
235 | - }else{ |
|
235 | + } else { |
|
236 | 236 | //huh, so its an object but not a data migration script?? that shouldn't happen |
237 | 237 | //just leave it as an array (which will probably just get ignored) |
238 | - throw new EE_Error(sprintf(__("Trying to retrieve DMS class from wp option. No DMS by the name '%s' exists", 'event_espresso'),$data_migration_data['class'])); |
|
238 | + throw new EE_Error(sprintf(__("Trying to retrieve DMS class from wp option. No DMS by the name '%s' exists", 'event_espresso'), $data_migration_data['class'])); |
|
239 | 239 | } |
240 | - }else{ |
|
240 | + } else { |
|
241 | 241 | //so the data doesn't specify a class. So it must either be a legacy array of info or some array (which we'll probably just ignore), or a class that no longer exists |
242 | - throw new EE_Error(sprintf(__("The wp option with key '%s' does not represent a DMS", 'event_espresso'),$dms_option_name)); |
|
242 | + throw new EE_Error(sprintf(__("The wp option with key '%s' does not represent a DMS", 'event_espresso'), $dms_option_name)); |
|
243 | 243 | } |
244 | 244 | } |
245 | 245 | /** |
@@ -247,34 +247,34 @@ discard block |
||
247 | 247 | * the last ran which hasn't finished yet |
248 | 248 | * @return array where each element should be an array of EE_Data_Migration_Script_Base (but also has a few legacy arrays in there - which should probably be ignored) |
249 | 249 | */ |
250 | - public function get_data_migrations_ran(){ |
|
251 | - if( ! $this->_data_migrations_ran ){ |
|
250 | + public function get_data_migrations_ran() { |
|
251 | + if ( ! $this->_data_migrations_ran) { |
|
252 | 252 | //setup autoloaders for each of the scripts in there |
253 | 253 | $this->get_all_data_migration_scripts_available(); |
254 | - $data_migrations_options = $this->get_all_migration_script_options();//get_option(EE_Data_Migration_Manager::data_migrations_option_name,get_option('espresso_data_migrations',array())); |
|
254 | + $data_migrations_options = $this->get_all_migration_script_options(); //get_option(EE_Data_Migration_Manager::data_migrations_option_name,get_option('espresso_data_migrations',array())); |
|
255 | 255 | |
256 | 256 | $data_migrations_ran = array(); |
257 | 257 | //convert into data migration script classes where possible |
258 | - foreach($data_migrations_options as $data_migration_option){ |
|
259 | - list($plugin_slug,$version_string) = $this->_get_plugin_slug_and_version_string_from_dms_option_name($data_migration_option['option_name']); |
|
258 | + foreach ($data_migrations_options as $data_migration_option) { |
|
259 | + list($plugin_slug, $version_string) = $this->_get_plugin_slug_and_version_string_from_dms_option_name($data_migration_option['option_name']); |
|
260 | 260 | |
261 | - try{ |
|
262 | - $class = $this->_get_dms_class_from_wp_option($data_migration_option['option_name'],$data_migration_option['option_value']); |
|
261 | + try { |
|
262 | + $class = $this->_get_dms_class_from_wp_option($data_migration_option['option_name'], $data_migration_option['option_value']); |
|
263 | 263 | $data_migrations_ran[$plugin_slug][$version_string] = $class; |
264 | 264 | //ok so far THIS is the 'last-ran-script'... unless we find another on next iteration |
265 | 265 | $this->_last_ran_script = $class; |
266 | - if( ! $class->is_completed()){ |
|
266 | + if ( ! $class->is_completed()) { |
|
267 | 267 | //sometimes we also like to know which was the last incomplete script (or if there are any at all) |
268 | 268 | $this->_last_ran_incomplete_script = $class; |
269 | 269 | } |
270 | - }catch(EE_Error $e){ |
|
270 | + } catch (EE_Error $e) { |
|
271 | 271 | //ok so its not a DMS. We'll just keep it, although other code will need to expect non-DMSs |
272 | 272 | $data_migrations_ran[$plugin_slug][$version_string] = maybe_unserialize($data_migration_option['option_value']); |
273 | 273 | } |
274 | 274 | } |
275 | 275 | //so here the array of $data_migrations_ran is actually a mix of classes and a few legacy arrays |
276 | 276 | $this->_data_migrations_ran = $data_migrations_ran; |
277 | - if ( ! $this->_data_migrations_ran || ! is_array($this->_data_migrations_ran) ){ |
|
277 | + if ( ! $this->_data_migrations_ran || ! is_array($this->_data_migrations_ran)) { |
|
278 | 278 | $this->_data_migrations_ran = array(); |
279 | 279 | } |
280 | 280 | } |
@@ -291,7 +291,7 @@ discard block |
||
291 | 291 | * @param $new_table |
292 | 292 | * @return mixed string or int |
293 | 293 | */ |
294 | - public function get_mapping_new_pk( $script_name, $old_table, $old_pk, $new_table){ |
|
294 | + public function get_mapping_new_pk($script_name, $old_table, $old_pk, $new_table) { |
|
295 | 295 | $script = EE_Registry::instance()->load_dms($script_name); |
296 | 296 | $mapping = $script->get_mapping_new_pk($old_table, $old_pk, $new_table); |
297 | 297 | return $mapping; |
@@ -302,9 +302,9 @@ discard block |
||
302 | 302 | * option returned in this array is the most-recently ran DMS option |
303 | 303 | * @return array |
304 | 304 | */ |
305 | - public function get_all_migration_script_options(){ |
|
305 | + public function get_all_migration_script_options() { |
|
306 | 306 | global $wpdb; |
307 | - return $wpdb->get_results("SELECT * FROM {$wpdb->options} WHERE option_name like '".EE_Data_Migration_Manager::data_migration_script_option_prefix."%' ORDER BY option_id ASC",ARRAY_A); |
|
307 | + return $wpdb->get_results("SELECT * FROM {$wpdb->options} WHERE option_name like '".EE_Data_Migration_Manager::data_migration_script_option_prefix."%' ORDER BY option_id ASC", ARRAY_A); |
|
308 | 308 | } |
309 | 309 | |
310 | 310 | /** |
@@ -312,8 +312,8 @@ discard block |
||
312 | 312 | * @return array where each value is the full folder path of a folder containing data migration scripts, WITH slashes at the end of the |
313 | 313 | * folder name. |
314 | 314 | */ |
315 | - public function get_data_migration_script_folders(){ |
|
316 | - return apply_filters( 'FHEE__EE_Data_Migration_Manager__get_data_migration_script_folders',array(EE_CORE.'data_migration_scripts') ); |
|
315 | + public function get_data_migration_script_folders() { |
|
316 | + return apply_filters('FHEE__EE_Data_Migration_Manager__get_data_migration_script_folders', array(EE_CORE.'data_migration_scripts')); |
|
317 | 317 | } |
318 | 318 | |
319 | 319 | /** |
@@ -325,11 +325,11 @@ discard block |
||
325 | 325 | * } |
326 | 326 | * @throws EE_Error |
327 | 327 | */ |
328 | - public function script_migrates_to_version($migration_script_name){ |
|
328 | + public function script_migrates_to_version($migration_script_name) { |
|
329 | 329 | $dms_info = $this->parse_dms_classname($migration_script_name); |
330 | 330 | return array( |
331 | - 'slug'=> $dms_info[ 'slug' ], |
|
332 | - 'version'=> $dms_info[ 'major_version' ] . "." . $dms_info[ 'minor_version' ] . "." . $dms_info[ 'micro_version' ] ); |
|
331 | + 'slug'=> $dms_info['slug'], |
|
332 | + 'version'=> $dms_info['major_version'].".".$dms_info['minor_version'].".".$dms_info['micro_version'] ); |
|
333 | 333 | } |
334 | 334 | |
335 | 335 | /** |
@@ -338,13 +338,13 @@ discard block |
||
338 | 338 | * @return array with keys 'slug','major_version','minor_version', and 'micro_version' (the last 3 are ints) |
339 | 339 | * @throws EE_Error |
340 | 340 | */ |
341 | - public function parse_dms_classname($classname){ |
|
341 | + public function parse_dms_classname($classname) { |
|
342 | 342 | $matches = array(); |
343 | - preg_match('~EE_DMS_(.*)_([0-9]*)_([0-9]*)_([0-9]*)~',$classname,$matches); |
|
344 | - if( ! $matches || ! (isset($matches[1]) && isset($matches[2]) && isset($matches[3]))){ |
|
345 | - throw new EE_Error(sprintf(__("%s is not a valid Data Migration Script. The classname should be like EE_DMS_w_x_y_z, where w is either 'Core' or the slug of an addon and x, y and z are numbers, ", "event_espresso"),$classname)); |
|
343 | + preg_match('~EE_DMS_(.*)_([0-9]*)_([0-9]*)_([0-9]*)~', $classname, $matches); |
|
344 | + if ( ! $matches || ! (isset($matches[1]) && isset($matches[2]) && isset($matches[3]))) { |
|
345 | + throw new EE_Error(sprintf(__("%s is not a valid Data Migration Script. The classname should be like EE_DMS_w_x_y_z, where w is either 'Core' or the slug of an addon and x, y and z are numbers, ", "event_espresso"), $classname)); |
|
346 | 346 | } |
347 | - return array('slug'=>$matches[1],'major_version'=>intval($matches[2]),'minor_version'=>intval($matches[3]),'micro_version'=>intval($matches[4])); |
|
347 | + return array('slug'=>$matches[1], 'major_version'=>intval($matches[2]), 'minor_version'=>intval($matches[3]), 'micro_version'=>intval($matches[4])); |
|
348 | 348 | } |
349 | 349 | /** |
350 | 350 | * Ensures that the option indicating the current DB version is set. This should only be |
@@ -353,33 +353,33 @@ discard block |
||
353 | 353 | * to 4.1.x. |
354 | 354 | * @return string of current db state |
355 | 355 | */ |
356 | - public function ensure_current_database_state_is_set(){ |
|
357 | - $espresso_db_core_updates = get_option( 'espresso_db_update', array() ); |
|
356 | + public function ensure_current_database_state_is_set() { |
|
357 | + $espresso_db_core_updates = get_option('espresso_db_update', array()); |
|
358 | 358 | $db_state = get_option(EE_Data_Migration_Manager::current_database_state); |
359 | - if( ! $db_state ){ |
|
359 | + if ( ! $db_state) { |
|
360 | 360 | //mark the DB as being in the state as the last version in there. |
361 | 361 | //this is done to trigger maintenance mode and do data migration scripts |
362 | 362 | //if the admin installed this version of EE over 3.1.x or 4.0.x |
363 | 363 | //otherwise, the normal maintenance mode code is fine |
364 | 364 | $previous_versions_installed = array_keys($espresso_db_core_updates); |
365 | 365 | $previous_version_installed = end($previous_versions_installed); |
366 | - if(version_compare('4.1.0', $previous_version_installed)){ |
|
366 | + if (version_compare('4.1.0', $previous_version_installed)) { |
|
367 | 367 | //last installed version was less than 4.1 |
368 | 368 | //so we want the data migrations to happen. SO, we're going to say the DB is at that state |
369 | 369 | // echo "4.1.0 is greater than $previous_version_installed! update the option"; |
370 | 370 | $db_state = array('Core'=>$previous_version_installed); |
371 | - }else{ |
|
371 | + } else { |
|
372 | 372 | // echo "4.1.0 is SMALLER than $previous_version_installed"; |
373 | 373 | $db_state = array('Core'=>EVENT_ESPRESSO_VERSION); |
374 | 374 | } |
375 | - update_option(EE_Data_Migration_Manager::current_database_state,$db_state); |
|
375 | + update_option(EE_Data_Migration_Manager::current_database_state, $db_state); |
|
376 | 376 | } |
377 | 377 | //in 4.1, $db_state would have only been a simple string like '4.1.0', |
378 | 378 | //but in 4.2+ it should be an array with at least key 'Core' and the value of that plugin's |
379 | 379 | //db, and possibly other keys for other addons like 'Calendar','Permissions',etc |
380 | - if( ! is_array($db_state)){ |
|
380 | + if ( ! is_array($db_state)) { |
|
381 | 381 | $db_state = array('Core'=>$db_state); |
382 | - update_option(EE_Data_Migration_Manager::current_database_state,$db_state); |
|
382 | + update_option(EE_Data_Migration_Manager::current_database_state, $db_state); |
|
383 | 383 | } |
384 | 384 | return $db_state; |
385 | 385 | } |
@@ -390,7 +390,7 @@ discard block |
||
390 | 390 | * or they don't apply), returns an empty array |
391 | 391 | * @return EE_Data_Migration_Script_Base[] |
392 | 392 | */ |
393 | - public function check_for_applicable_data_migration_scripts(){ |
|
393 | + public function check_for_applicable_data_migration_scripts() { |
|
394 | 394 | //get the option describing what options have already run |
395 | 395 | $scripts_ran = $this->get_data_migrations_ran(); |
396 | 396 | //$scripts_ran = array('4.1.0.core'=>array('monkey'=>null)); |
@@ -403,62 +403,62 @@ discard block |
||
403 | 403 | $iteration = 0; |
404 | 404 | $next_database_state_to_consider = $current_database_state; |
405 | 405 | $theoretical_database_state = NULL; |
406 | - do{ |
|
406 | + do { |
|
407 | 407 | //the next state after the currently-considered one will start off looking the same as the current, but we may make additions... |
408 | 408 | $theoretical_database_state = $next_database_state_to_consider; |
409 | 409 | //the next db state to consider is "what would the DB be like had we run all the scripts we found that applied last time?) |
410 | - foreach($script_class_and_filepaths_available as $classname => $filepath){ |
|
410 | + foreach ($script_class_and_filepaths_available as $classname => $filepath) { |
|
411 | 411 | |
412 | 412 | $migrates_to_version = $this->script_migrates_to_version($classname); |
413 | - $script_converts_plugin_slug = $migrates_to_version[ 'slug' ]; |
|
414 | - $script_converts_to_version = $migrates_to_version[ 'version' ]; |
|
413 | + $script_converts_plugin_slug = $migrates_to_version['slug']; |
|
414 | + $script_converts_to_version = $migrates_to_version['version']; |
|
415 | 415 | //check if this version script is DONE or not; or if it's never been ran |
416 | - if( ! $scripts_ran || |
|
416 | + if ( ! $scripts_ran || |
|
417 | 417 | ! isset($scripts_ran[$script_converts_plugin_slug]) || |
418 | - ! isset($scripts_ran[$script_converts_plugin_slug][$script_converts_to_version])){ |
|
418 | + ! isset($scripts_ran[$script_converts_plugin_slug][$script_converts_to_version])) { |
|
419 | 419 | //we haven't ran this conversion script before |
420 | 420 | //now check if it applies... note that we've added an autoloader for it on get_all_data_migration_scripts_available |
421 | - $script = new $classname( $this->_get_table_manager(), $this->_get_table_analysis() ); |
|
421 | + $script = new $classname($this->_get_table_manager(), $this->_get_table_analysis()); |
|
422 | 422 | /* @var $script EE_Data_Migration_Script_Base */ |
423 | 423 | $can_migrate = $script->can_migrate_from_version($theoretical_database_state); |
424 | - if($can_migrate){ |
|
424 | + if ($can_migrate) { |
|
425 | 425 | $script_classes_that_should_run_per_iteration[$iteration][$script->priority()][] = $script; |
426 | 426 | $migrates_to_version = $script->migrates_to_version(); |
427 | - $next_database_state_to_consider[ $migrates_to_version[ 'slug' ] ] = $migrates_to_version[ 'version' ]; |
|
427 | + $next_database_state_to_consider[$migrates_to_version['slug']] = $migrates_to_version['version']; |
|
428 | 428 | unset($script_class_and_filepaths_available[$classname]); |
429 | 429 | } |
430 | - } elseif($scripts_ran[$script_converts_plugin_slug][$script_converts_to_version] instanceof EE_Data_Migration_Script_Base){ |
|
430 | + } elseif ($scripts_ran[$script_converts_plugin_slug][$script_converts_to_version] instanceof EE_Data_Migration_Script_Base) { |
|
431 | 431 | //this script has been ran, or at least started |
432 | 432 | $script = $scripts_ran[$script_converts_plugin_slug][$script_converts_to_version]; |
433 | - if( $script->get_status() != self::status_completed){ |
|
433 | + if ($script->get_status() != self::status_completed) { |
|
434 | 434 | //this script is already underway... keep going with it |
435 | 435 | $script_classes_that_should_run_per_iteration[$iteration][$script->priority()][] = $script; |
436 | 436 | $migrates_to_version = $script->migrates_to_version(); |
437 | - $next_database_state_to_consider[ $migrates_to_version[ 'slug' ] ] = $migrates_to_version[ 'version' ]; |
|
437 | + $next_database_state_to_consider[$migrates_to_version['slug']] = $migrates_to_version['version']; |
|
438 | 438 | unset($script_class_and_filepaths_available[$classname]); |
439 | - }else{ |
|
439 | + } else { |
|
440 | 440 | //it must have a status that indicates it has finished, so we don't want to try and run it again |
441 | 441 | } |
442 | - }else{ |
|
442 | + } else { |
|
443 | 443 | //it exists but it's not a proper data migration script |
444 | 444 | //maybe the script got renamed? or was simply removed from EE? |
445 | 445 | //either way, its certainly not runnable! |
446 | 446 | } |
447 | 447 | } |
448 | 448 | $iteration++; |
449 | - }while( $next_database_state_to_consider != $theoretical_database_state && $iteration<6); |
|
449 | + }while ($next_database_state_to_consider != $theoretical_database_state && $iteration < 6); |
|
450 | 450 | //ok we have all the scripts that should run, now let's make them into flat array |
451 | 451 | $scripts_that_should_run = array(); |
452 | - foreach($script_classes_that_should_run_per_iteration as $scripts_at_priority){ |
|
452 | + foreach ($script_classes_that_should_run_per_iteration as $scripts_at_priority) { |
|
453 | 453 | ksort($scripts_at_priority); |
454 | - foreach($scripts_at_priority as $scripts){ |
|
455 | - foreach($scripts as $script){ |
|
454 | + foreach ($scripts_at_priority as $scripts) { |
|
455 | + foreach ($scripts as $script) { |
|
456 | 456 | $scripts_that_should_run[get_class($script)] = $script; |
457 | 457 | } |
458 | 458 | } |
459 | 459 | } |
460 | 460 | |
461 | - do_action( 'AHEE__EE_Data_Migration_Manager__check_for_applicable_data_migration_scripts__scripts_that_should_run', $scripts_that_should_run ); |
|
461 | + do_action('AHEE__EE_Data_Migration_Manager__check_for_applicable_data_migration_scripts__scripts_that_should_run', $scripts_that_should_run); |
|
462 | 462 | return $scripts_that_should_run; |
463 | 463 | } |
464 | 464 | |
@@ -472,14 +472,14 @@ discard block |
||
472 | 472 | * @param bool $include_completed_scripts |
473 | 473 | * @return EE_Data_Migration_Script_Base |
474 | 474 | */ |
475 | - public function get_last_ran_script($include_completed_scripts = false){ |
|
475 | + public function get_last_ran_script($include_completed_scripts = false) { |
|
476 | 476 | //make sure we've setup the class properties _last_ran_script and _last_ran_incomplete_script |
477 | - if( ! $this->_data_migrations_ran){ |
|
477 | + if ( ! $this->_data_migrations_ran) { |
|
478 | 478 | $this->get_data_migrations_ran(); |
479 | 479 | } |
480 | - if($include_completed_scripts){ |
|
480 | + if ($include_completed_scripts) { |
|
481 | 481 | return $this->_last_ran_script; |
482 | - }else{ |
|
482 | + } else { |
|
483 | 483 | return $this->_last_ran_incomplete_script; |
484 | 484 | } |
485 | 485 | } |
@@ -502,19 +502,19 @@ discard block |
||
502 | 502 | * @type string $message string describing what was done during this step |
503 | 503 | * } |
504 | 504 | */ |
505 | - public function migration_step( $step_size = 0 ){ |
|
505 | + public function migration_step($step_size = 0) { |
|
506 | 506 | |
507 | 507 | //bandaid fix for issue https://events.codebasehq.com/projects/event-espresso/tickets/7535 |
508 | - if ( class_exists( 'EE_CPT_Strategy' ) ) { |
|
509 | - remove_action( 'pre_get_posts', array( EE_CPT_Strategy::instance(), 'pre_get_posts' ), 5 ); |
|
508 | + if (class_exists('EE_CPT_Strategy')) { |
|
509 | + remove_action('pre_get_posts', array(EE_CPT_Strategy::instance(), 'pre_get_posts'), 5); |
|
510 | 510 | } |
511 | 511 | |
512 | - try{ |
|
512 | + try { |
|
513 | 513 | $currently_executing_script = $this->get_last_ran_script(); |
514 | - if( ! $currently_executing_script){ |
|
514 | + if ( ! $currently_executing_script) { |
|
515 | 515 | //Find the next script that needs to execute |
516 | 516 | $scripts = $this->check_for_applicable_data_migration_scripts(); |
517 | - if( ! $scripts ){ |
|
517 | + if ( ! $scripts) { |
|
518 | 518 | //huh, no more scripts to run... apparently we're done! |
519 | 519 | //but dont forget to make sure initial data is there |
520 | 520 | //we should be good to allow them to exit maintenance mode now |
@@ -535,18 +535,18 @@ discard block |
||
535 | 535 | //and add to the array/wp option showing the scripts ran |
536 | 536 | // $this->_data_migrations_ran[$this->script_migrates_to_version(get_class($currently_executing_script))] = $currently_executing_script; |
537 | 537 | $migrates_to = $this->script_migrates_to_version(get_class($currently_executing_script)); |
538 | - $plugin_slug = $migrates_to[ 'slug' ]; |
|
539 | - $version = $migrates_to[ 'version' ]; |
|
538 | + $plugin_slug = $migrates_to['slug']; |
|
539 | + $version = $migrates_to['version']; |
|
540 | 540 | $this->_data_migrations_ran[$plugin_slug][$version] = $currently_executing_script; |
541 | 541 | } |
542 | 542 | $current_script_name = get_class($currently_executing_script); |
543 | - }catch(Exception $e){ |
|
543 | + } catch (Exception $e) { |
|
544 | 544 | //an exception occurred while trying to get migration scripts |
545 | 545 | |
546 | - $message = sprintf( __("Error Message: %sStack Trace:%s", "event_espresso"), $e->getMessage() . '<br>', $e->getTraceAsString() ); |
|
546 | + $message = sprintf(__("Error Message: %sStack Trace:%s", "event_espresso"), $e->getMessage().'<br>', $e->getTraceAsString()); |
|
547 | 547 | //record it on the array of data migration scripts ran. This will be overwritten next time we try and try to run data migrations |
548 | 548 | //but that's ok-- it's just an FYI to support that we couldn't even run any data migrations |
549 | - $this->add_error_to_migrations_ran(sprintf(__("Could not run data migrations because: %s", "event_espresso"),$message)); |
|
549 | + $this->add_error_to_migrations_ran(sprintf(__("Could not run data migrations because: %s", "event_espresso"), $message)); |
|
550 | 550 | return array( |
551 | 551 | 'records_to_migrate'=>1, |
552 | 552 | 'records_migrated'=>0, |
@@ -556,16 +556,16 @@ discard block |
||
556 | 556 | ); |
557 | 557 | } |
558 | 558 | //ok so we definitely have a data migration script |
559 | - try{ |
|
559 | + try { |
|
560 | 560 | //how big of a bite do we want to take? Allow users to easily override via their wp-config |
561 | - if( ! absint( $step_size ) > 0 ){ |
|
562 | - $step_size = defined( 'EE_MIGRATION_STEP_SIZE' ) && absint( EE_MIGRATION_STEP_SIZE ) ? EE_MIGRATION_STEP_SIZE : EE_Data_Migration_Manager::step_size; |
|
561 | + if ( ! absint($step_size) > 0) { |
|
562 | + $step_size = defined('EE_MIGRATION_STEP_SIZE') && absint(EE_MIGRATION_STEP_SIZE) ? EE_MIGRATION_STEP_SIZE : EE_Data_Migration_Manager::step_size; |
|
563 | 563 | } |
564 | 564 | //do what we came to do! |
565 | 565 | $currently_executing_script->migration_step($step_size); |
566 | 566 | //can we wrap it up and verify default data? |
567 | 567 | $init_dbs = false; |
568 | - switch($currently_executing_script->get_status()){ |
|
568 | + switch ($currently_executing_script->get_status()) { |
|
569 | 569 | case EE_Data_Migration_Manager::status_continue: |
570 | 570 | $response_array = array( |
571 | 571 | 'records_to_migrate'=>$currently_executing_script->count_records_to_migrate(), |
@@ -577,16 +577,16 @@ discard block |
||
577 | 577 | case EE_Data_Migration_Manager::status_completed: |
578 | 578 | //ok so THAT script has completed |
579 | 579 | $this->update_current_database_state_to($this->script_migrates_to_version($current_script_name)); |
580 | - $response_array = array( |
|
580 | + $response_array = array( |
|
581 | 581 | 'records_to_migrate'=>$currently_executing_script->count_records_to_migrate(), |
582 | 582 | 'records_migrated'=>$currently_executing_script->count_records_migrated(), |
583 | 583 | 'status'=> EE_Data_Migration_Manager::status_completed, |
584 | 584 | 'message'=>$currently_executing_script->get_feedback_message(), |
585 | - 'script'=> sprintf(__("%s Completed",'event_espresso'),$currently_executing_script->pretty_name()) |
|
585 | + 'script'=> sprintf(__("%s Completed", 'event_espresso'), $currently_executing_script->pretty_name()) |
|
586 | 586 | ); |
587 | 587 | //check if there are any more after this one. |
588 | 588 | $scripts_remaining = $this->check_for_applicable_data_migration_scripts(); |
589 | - if( ! $scripts_remaining ){ |
|
589 | + if ( ! $scripts_remaining) { |
|
590 | 590 | //we should be good to allow them to exit maintenance mode now |
591 | 591 | EE_Maintenance_Mode::instance()->set_maintenance_level(intval(EE_Maintenance_Mode::level_0_not_in_maintenance)); |
592 | 592 | ////huh, no more scripts to run... apparently we're done! |
@@ -600,39 +600,39 @@ discard block |
||
600 | 600 | 'records_to_migrate'=>$currently_executing_script->count_records_to_migrate(), |
601 | 601 | 'records_migrated'=>$currently_executing_script->count_records_migrated(), |
602 | 602 | 'status'=> $currently_executing_script->get_status(), |
603 | - 'message'=> sprintf(__("Minor errors occurred during %s: %s", "event_espresso"), $currently_executing_script->pretty_name(), implode(", ",$currently_executing_script->get_errors())), |
|
603 | + 'message'=> sprintf(__("Minor errors occurred during %s: %s", "event_espresso"), $currently_executing_script->pretty_name(), implode(", ", $currently_executing_script->get_errors())), |
|
604 | 604 | 'script'=>$currently_executing_script->pretty_name() |
605 | 605 | ); |
606 | 606 | break; |
607 | 607 | } |
608 | - }catch(Exception $e){ |
|
608 | + } catch (Exception $e) { |
|
609 | 609 | //ok so some exception was thrown which killed the data migration script |
610 | 610 | //double-check we have a real script |
611 | - if($currently_executing_script instanceof EE_Data_Migration_Script_Base){ |
|
611 | + if ($currently_executing_script instanceof EE_Data_Migration_Script_Base) { |
|
612 | 612 | $script_name = $currently_executing_script->pretty_name(); |
613 | 613 | $currently_executing_script->set_broken(); |
614 | 614 | $currently_executing_script->add_error($e->getMessage()); |
615 | - }else{ |
|
615 | + } else { |
|
616 | 616 | $script_name = __("Error getting Migration Script", "event_espresso"); |
617 | 617 | } |
618 | 618 | $response_array = array( |
619 | 619 | 'records_to_migrate'=>1, |
620 | 620 | 'records_migrated'=>0, |
621 | 621 | 'status'=>self::status_fatal_error, |
622 | - 'message'=> sprintf(__("A fatal error occurred during the migration: %s", "event_espresso"),$e->getMessage()), |
|
622 | + 'message'=> sprintf(__("A fatal error occurred during the migration: %s", "event_espresso"), $e->getMessage()), |
|
623 | 623 | 'script'=>$script_name |
624 | 624 | ); |
625 | 625 | } |
626 | 626 | $successful_save = $this->_save_migrations_ran(); |
627 | - if($successful_save !== TRUE){ |
|
627 | + if ($successful_save !== TRUE) { |
|
628 | 628 | //ok so the current wp option didn't save. that's tricky, because we'd like to update it |
629 | 629 | //and mark it as having a fatal error, but remember- WE CAN'T SAVE THIS WP OPTION! |
630 | 630 | //however, if we throw an exception, and return that, then the next request |
631 | 631 | //won't have as much info in it, and it may be able to save |
632 | - throw new EE_Error(sprintf(__("The error '%s' occurred updating the status of the migration. This is a FATAL ERROR, but the error is preventing the system from remembering that. Please contact event espresso support.", "event_espresso"),$successful_save)); |
|
632 | + throw new EE_Error(sprintf(__("The error '%s' occurred updating the status of the migration. This is a FATAL ERROR, but the error is preventing the system from remembering that. Please contact event espresso support.", "event_espresso"), $successful_save)); |
|
633 | 633 | } |
634 | 634 | //if we're all done, initialize EE plugins' default data etc. |
635 | - if( $init_dbs ) { |
|
635 | + if ($init_dbs) { |
|
636 | 636 | $this->initialize_db_for_enqueued_ee_plugins(); |
637 | 637 | } |
638 | 638 | return $response_array; |
@@ -650,23 +650,23 @@ discard block |
||
650 | 650 | * 'message'=>a string, containing any message you want to show to the user. We may decide to split this up into errors, notifications, and successes |
651 | 651 | * 'script'=>a pretty name of the script currently running |
652 | 652 | */ |
653 | - public function response_to_migration_ajax_request(){ |
|
653 | + public function response_to_migration_ajax_request() { |
|
654 | 654 | // //start output buffer just to make sure we don't mess up the json |
655 | 655 | ob_start(); |
656 | - try{ |
|
656 | + try { |
|
657 | 657 | $response = $this->migration_step(); |
658 | - }catch(Exception $e){ |
|
658 | + } catch (Exception $e) { |
|
659 | 659 | $response = array( |
660 | 660 | 'records_to_migrate'=>0, |
661 | 661 | 'records_migrated'=>0, |
662 | 662 | 'status'=> EE_Data_Migration_Manager::status_fatal_error, |
663 | - 'message'=> sprintf(__("Unknown fatal error occurred: %s", "event_espresso"),$e->getMessage()), |
|
663 | + 'message'=> sprintf(__("Unknown fatal error occurred: %s", "event_espresso"), $e->getMessage()), |
|
664 | 664 | 'script'=>'Unknown'); |
665 | 665 | $this->add_error_to_migrations_ran($e->getMessage()."; Stack trace:".$e->getTraceAsString()); |
666 | 666 | } |
667 | 667 | $warnings_etc = @ob_get_contents(); |
668 | 668 | ob_end_clean(); |
669 | - $response['message'] .=$warnings_etc; |
|
669 | + $response['message'] .= $warnings_etc; |
|
670 | 670 | return $response; |
671 | 671 | } |
672 | 672 | |
@@ -679,14 +679,14 @@ discard block |
||
679 | 679 | * } |
680 | 680 | * @return void |
681 | 681 | */ |
682 | - public function update_current_database_state_to($slug_and_version = null){ |
|
683 | - if( ! $slug_and_version ){ |
|
682 | + public function update_current_database_state_to($slug_and_version = null) { |
|
683 | + if ( ! $slug_and_version) { |
|
684 | 684 | //no version was provided, assume it should be at the current code version |
685 | 685 | $slug_and_version = array('slug' => 'Core', 'version' => espresso_version()); |
686 | 686 | } |
687 | 687 | $current_database_state = get_option(self::current_database_state); |
688 | - $current_database_state[ $slug_and_version[ 'slug' ] ]=$slug_and_version[ 'version' ]; |
|
689 | - update_option(self::current_database_state,$current_database_state); |
|
688 | + $current_database_state[$slug_and_version['slug']] = $slug_and_version['version']; |
|
689 | + update_option(self::current_database_state, $current_database_state); |
|
690 | 690 | } |
691 | 691 | |
692 | 692 | /** |
@@ -697,20 +697,20 @@ discard block |
||
697 | 697 | * } |
698 | 698 | * @return boolean |
699 | 699 | */ |
700 | - public function database_needs_updating_to( $slug_and_version ) { |
|
700 | + public function database_needs_updating_to($slug_and_version) { |
|
701 | 701 | |
702 | - $slug = $slug_and_version[ 'slug' ]; |
|
703 | - $version = $slug_and_version[ 'version' ]; |
|
702 | + $slug = $slug_and_version['slug']; |
|
703 | + $version = $slug_and_version['version']; |
|
704 | 704 | $current_database_state = get_option(self::current_database_state); |
705 | - if( ! isset( $current_database_state[ $slug ] ) ) { |
|
705 | + if ( ! isset($current_database_state[$slug])) { |
|
706 | 706 | return true; |
707 | - }else{ |
|
707 | + } else { |
|
708 | 708 | //just compare the first 3 parts of version string, eg "4.7.1", not "4.7.1.dev.032" because DBs shouldn't change on nano version changes |
709 | - $version_parts_current_db_state = array_slice( explode('.', $current_database_state[ $slug ] ), 0, 3); |
|
710 | - $version_parts_of_provided_db_state = array_slice( explode( '.', $version ), 0, 3 ); |
|
709 | + $version_parts_current_db_state = array_slice(explode('.', $current_database_state[$slug]), 0, 3); |
|
710 | + $version_parts_of_provided_db_state = array_slice(explode('.', $version), 0, 3); |
|
711 | 711 | $needs_updating = false; |
712 | - foreach($version_parts_current_db_state as $offset => $version_part_in_current_db_state ) { |
|
713 | - if( $version_part_in_current_db_state < $version_parts_of_provided_db_state[ $offset ] ) { |
|
712 | + foreach ($version_parts_current_db_state as $offset => $version_part_in_current_db_state) { |
|
713 | + if ($version_part_in_current_db_state < $version_parts_of_provided_db_state[$offset]) { |
|
714 | 714 | $needs_updating = true; |
715 | 715 | break; |
716 | 716 | } |
@@ -728,33 +728,33 @@ discard block |
||
728 | 728 | * @return array keys are expected classnames, values are their filepaths |
729 | 729 | * @throws \EE_Error |
730 | 730 | */ |
731 | - public function get_all_data_migration_scripts_available(){ |
|
732 | - if( ! $this->_data_migration_class_to_filepath_map){ |
|
731 | + public function get_all_data_migration_scripts_available() { |
|
732 | + if ( ! $this->_data_migration_class_to_filepath_map) { |
|
733 | 733 | $this->_data_migration_class_to_filepath_map = array(); |
734 | - foreach($this->get_data_migration_script_folders() as $folder_path){ |
|
734 | + foreach ($this->get_data_migration_script_folders() as $folder_path) { |
|
735 | 735 | $folder_path = EEH_File::end_with_directory_separator($folder_path); |
736 | - $files = glob( $folder_path. '*.dms.php' ); |
|
736 | + $files = glob($folder_path.'*.dms.php'); |
|
737 | 737 | |
738 | - if ( empty( $files ) ) { |
|
738 | + if (empty($files)) { |
|
739 | 739 | continue; |
740 | 740 | } |
741 | 741 | |
742 | - foreach($files as $file){ |
|
743 | - $pos_of_last_slash = strrpos($file,DS); |
|
742 | + foreach ($files as $file) { |
|
743 | + $pos_of_last_slash = strrpos($file, DS); |
|
744 | 744 | $classname = str_replace('.dms.php', '', substr($file, $pos_of_last_slash + 1)); |
745 | - $migrates_to = $this->script_migrates_to_version( $classname ); |
|
746 | - $slug = $migrates_to[ 'slug' ]; |
|
745 | + $migrates_to = $this->script_migrates_to_version($classname); |
|
746 | + $slug = $migrates_to['slug']; |
|
747 | 747 | //check that the slug as contained in the DMS is associated with |
748 | 748 | //the slug of an addon or core |
749 | - if( $slug !== 'Core' ){ |
|
750 | - if( ! EE_Registry::instance()->get_addon_by_name( $slug ) ) { |
|
749 | + if ($slug !== 'Core') { |
|
750 | + if ( ! EE_Registry::instance()->get_addon_by_name($slug)) { |
|
751 | 751 | EE_Error::doing_it_wrong( |
752 | 752 | __FUNCTION__, |
753 | 753 | sprintf( |
754 | - __( 'The data migration script "%s" migrates the "%s" data, but there is no EE addon with that name. There is only: %s. ', 'event_espresso' ), |
|
754 | + __('The data migration script "%s" migrates the "%s" data, but there is no EE addon with that name. There is only: %s. ', 'event_espresso'), |
|
755 | 755 | $classname, |
756 | 756 | $slug, |
757 | - implode(', ', array_keys( EE_Registry::instance()->get_addons_by_name() ) ) |
|
757 | + implode(', ', array_keys(EE_Registry::instance()->get_addons_by_name())) |
|
758 | 758 | ), |
759 | 759 | '4.3.0.alpha.019' |
760 | 760 | ); |
@@ -777,7 +777,7 @@ discard block |
||
777 | 777 | * from each addon, and check if they need updating, |
778 | 778 | * @return boolean |
779 | 779 | */ |
780 | - public function addons_need_updating(){ |
|
780 | + public function addons_need_updating() { |
|
781 | 781 | return false; |
782 | 782 | } |
783 | 783 | /** |
@@ -786,25 +786,25 @@ discard block |
||
786 | 786 | * @param string $error_message |
787 | 787 | * @throws EE_Error |
788 | 788 | */ |
789 | - public function add_error_to_migrations_ran($error_message){ |
|
789 | + public function add_error_to_migrations_ran($error_message) { |
|
790 | 790 | //get last-ran migration script |
791 | 791 | global $wpdb; |
792 | - $last_migration_script_option = $wpdb->get_row("SELECT * FROM $wpdb->options WHERE option_name like '".EE_Data_Migration_Manager::data_migration_script_option_prefix."%' ORDER BY option_id DESC LIMIT 1",ARRAY_A); |
|
792 | + $last_migration_script_option = $wpdb->get_row("SELECT * FROM $wpdb->options WHERE option_name like '".EE_Data_Migration_Manager::data_migration_script_option_prefix."%' ORDER BY option_id DESC LIMIT 1", ARRAY_A); |
|
793 | 793 | |
794 | 794 | $last_ran_migration_script_properties = isset($last_migration_script_option['option_value']) ? maybe_unserialize($last_migration_script_option['option_value']) : null; |
795 | 795 | //now, tread lightly because we're here because a FATAL non-catchable error |
796 | 796 | //was thrown last time when we were trying to run a data migration script |
797 | 797 | //so the fatal error could have happened while getting the migration script |
798 | 798 | //or doing running it... |
799 | - $versions_migrated_to = isset($last_migration_script_option['option_name']) ? str_replace(EE_Data_Migration_Manager::data_migration_script_option_prefix,"",$last_migration_script_option['option_name']) : null; |
|
799 | + $versions_migrated_to = isset($last_migration_script_option['option_name']) ? str_replace(EE_Data_Migration_Manager::data_migration_script_option_prefix, "", $last_migration_script_option['option_name']) : null; |
|
800 | 800 | |
801 | 801 | //check if it THINKS its a data migration script and especially if it's one that HASN'T finished yet |
802 | 802 | //because if it has finished, then it obviously couldn't be the cause of this error, right? (because its all done) |
803 | - if(isset($last_ran_migration_script_properties['class']) && isset($last_ran_migration_script_properties['_status']) && $last_ran_migration_script_properties['_status'] != self::status_completed){ |
|
803 | + if (isset($last_ran_migration_script_properties['class']) && isset($last_ran_migration_script_properties['_status']) && $last_ran_migration_script_properties['_status'] != self::status_completed) { |
|
804 | 804 | //ok then just add this error to its list of errors |
805 | 805 | $last_ran_migration_script_properties['_errors'][] = $error_message; |
806 | 806 | $last_ran_migration_script_properties['_status'] = self::status_fatal_error; |
807 | - }else{ |
|
807 | + } else { |
|
808 | 808 | //so we don't even know which script was last running |
809 | 809 | //use the data migration error stub, which is designed specifically for this type of thing |
810 | 810 | $general_migration_error = new EE_DMS_Unknown_1_0_0(); |
@@ -814,39 +814,39 @@ discard block |
||
814 | 814 | $versions_migrated_to = 'Unknown.1.0.0'; |
815 | 815 | //now just to make sure appears as last (in case the were previously a fatal error like this) |
816 | 816 | //delete the old one |
817 | - delete_option( self::data_migration_script_option_prefix . $versions_migrated_to ); |
|
817 | + delete_option(self::data_migration_script_option_prefix.$versions_migrated_to); |
|
818 | 818 | } |
819 | - update_option(self::data_migration_script_option_prefix.$versions_migrated_to,$last_ran_migration_script_properties); |
|
819 | + update_option(self::data_migration_script_option_prefix.$versions_migrated_to, $last_ran_migration_script_properties); |
|
820 | 820 | |
821 | 821 | } |
822 | 822 | /** |
823 | 823 | * saves what data migrations have ran to the database |
824 | 824 | * @return mixed TRUE if successfully saved migrations ran, string if an error occurred |
825 | 825 | */ |
826 | - protected function _save_migrations_ran(){ |
|
827 | - if($this->_data_migrations_ran == null){ |
|
826 | + protected function _save_migrations_ran() { |
|
827 | + if ($this->_data_migrations_ran == null) { |
|
828 | 828 | $this->get_data_migrations_ran(); |
829 | 829 | } |
830 | 830 | //now, we don't want to save actual classes to the DB because that's messy |
831 | 831 | $successful_updates = true; |
832 | - foreach($this->_data_migrations_ran as $plugin_slug => $migrations_ran_for_plugin){ |
|
833 | - foreach($migrations_ran_for_plugin as $version_string => $array_or_migration_obj){ |
|
832 | + foreach ($this->_data_migrations_ran as $plugin_slug => $migrations_ran_for_plugin) { |
|
833 | + foreach ($migrations_ran_for_plugin as $version_string => $array_or_migration_obj) { |
|
834 | 834 | // echo "saving migration script to $version_string<br>"; |
835 | 835 | $plugin_slug_for_use_in_option_name = $plugin_slug."."; |
836 | 836 | $option_name = self::data_migration_script_option_prefix.$plugin_slug_for_use_in_option_name.$version_string; |
837 | 837 | $old_option_value = get_option($option_name); |
838 | - if($array_or_migration_obj instanceof EE_Data_Migration_Script_Base){ |
|
838 | + if ($array_or_migration_obj instanceof EE_Data_Migration_Script_Base) { |
|
839 | 839 | $script_array_for_saving = $array_or_migration_obj->properties_as_array(); |
840 | - if( $old_option_value != $script_array_for_saving){ |
|
841 | - $successful_updates = update_option($option_name,$script_array_for_saving); |
|
840 | + if ($old_option_value != $script_array_for_saving) { |
|
841 | + $successful_updates = update_option($option_name, $script_array_for_saving); |
|
842 | 842 | } |
843 | - }else{//we don't know what this array-thing is. So just save it as-is |
|
843 | + } else {//we don't know what this array-thing is. So just save it as-is |
|
844 | 844 | // $array_of_migrations[$version_string] = $array_or_migration_obj; |
845 | - if($old_option_value != $array_or_migration_obj){ |
|
846 | - $successful_updates = update_option($option_name,$array_or_migration_obj); |
|
845 | + if ($old_option_value != $array_or_migration_obj) { |
|
846 | + $successful_updates = update_option($option_name, $array_or_migration_obj); |
|
847 | 847 | } |
848 | 848 | } |
849 | - if( ! $successful_updates ){ |
|
849 | + if ( ! $successful_updates) { |
|
850 | 850 | global $wpdb; |
851 | 851 | return $wpdb->last_error; |
852 | 852 | } |
@@ -870,17 +870,17 @@ discard block |
||
870 | 870 | * @return EE_Data_Migration_Script_Base |
871 | 871 | * @throws EE_Error |
872 | 872 | */ |
873 | - function _instantiate_script_from_properties_array($properties_array){ |
|
874 | - if( ! isset($properties_array['class'])){ |
|
875 | - throw new EE_Error(sprintf(__("Properties array has no 'class' properties. Here's what it has: %s", "event_espresso"),implode(",",$properties_array))); |
|
873 | + function _instantiate_script_from_properties_array($properties_array) { |
|
874 | + if ( ! isset($properties_array['class'])) { |
|
875 | + throw new EE_Error(sprintf(__("Properties array has no 'class' properties. Here's what it has: %s", "event_espresso"), implode(",", $properties_array))); |
|
876 | 876 | } |
877 | 877 | $class_name = $properties_array['class']; |
878 | - if( ! class_exists($class_name)){ |
|
879 | - throw new EE_Error(sprintf(__("There is no migration script named %s", "event_espresso"),$class_name)); |
|
878 | + if ( ! class_exists($class_name)) { |
|
879 | + throw new EE_Error(sprintf(__("There is no migration script named %s", "event_espresso"), $class_name)); |
|
880 | 880 | } |
881 | 881 | $class = new $class_name; |
882 | - if( ! $class instanceof EE_Data_Migration_Script_Base){ |
|
883 | - throw new EE_Error(sprintf(__("Class '%s' is supposed to be a migration script. Its not, its a '%s'", "event_espresso"),$class_name,get_class($class))); |
|
882 | + if ( ! $class instanceof EE_Data_Migration_Script_Base) { |
|
883 | + throw new EE_Error(sprintf(__("Class '%s' is supposed to be a migration script. Its not, its a '%s'", "event_espresso"), $class_name, get_class($class))); |
|
884 | 884 | } |
885 | 885 | $class->instantiate_from_array_of_properties($properties_array); |
886 | 886 | return $class; |
@@ -892,25 +892,25 @@ discard block |
||
892 | 892 | * @param string $plugin_slug the slug for the ee plugin we are searching for. Default is 'Core' |
893 | 893 | * @return string |
894 | 894 | */ |
895 | - public function get_most_up_to_date_dms($plugin_slug = 'Core'){ |
|
895 | + public function get_most_up_to_date_dms($plugin_slug = 'Core') { |
|
896 | 896 | $class_to_filepath_map = $this->get_all_data_migration_scripts_available(); |
897 | 897 | $most_up_to_date_dms_classname = NULL; |
898 | - foreach($class_to_filepath_map as $classname => $filepath){ |
|
899 | - if($most_up_to_date_dms_classname === NULL){ |
|
898 | + foreach ($class_to_filepath_map as $classname => $filepath) { |
|
899 | + if ($most_up_to_date_dms_classname === NULL) { |
|
900 | 900 | $migrates_to = $this->script_migrates_to_version($classname); |
901 | - $this_plugin_slug = $migrates_to[ 'slug' ]; |
|
901 | + $this_plugin_slug = $migrates_to['slug']; |
|
902 | 902 | // $version_string = $migrates_to[ 'version' ]; |
903 | 903 | // $details = $this->parse_dms_classname($classname); |
904 | - if($this_plugin_slug == $plugin_slug){ |
|
904 | + if ($this_plugin_slug == $plugin_slug) { |
|
905 | 905 | //if it's for core, it wins |
906 | 906 | $most_up_to_date_dms_classname = $classname; |
907 | 907 | } |
908 | 908 | //if it wasn't for core, we must keep searching for one that is! |
909 | 909 | continue; |
910 | - }else{ |
|
911 | - $champion_migrates_to= $this->script_migrates_to_version($most_up_to_date_dms_classname); |
|
910 | + } else { |
|
911 | + $champion_migrates_to = $this->script_migrates_to_version($most_up_to_date_dms_classname); |
|
912 | 912 | $contender_migrates_to = $this->script_migrates_to_version($classname); |
913 | - if($contender_migrates_to[ 'slug' ] == $plugin_slug && version_compare($champion_migrates_to[ 'version' ], $contender_migrates_to[ 'version' ], '<')){ |
|
913 | + if ($contender_migrates_to['slug'] == $plugin_slug && version_compare($champion_migrates_to['version'], $contender_migrates_to['version'], '<')) { |
|
914 | 914 | //so the contenders version is higher and its for Core |
915 | 915 | $most_up_to_date_dms_classname = $classname; |
916 | 916 | } |
@@ -930,11 +930,11 @@ discard block |
||
930 | 930 | * @param string $plugin_slug like 'Core', 'Mailchimp', 'Calendar', etc |
931 | 931 | * @return EE_Data_Migration_Script_Base |
932 | 932 | */ |
933 | - public function get_migration_ran( $version, $plugin_slug = 'Core' ) { |
|
933 | + public function get_migration_ran($version, $plugin_slug = 'Core') { |
|
934 | 934 | $migrations_ran = $this->get_data_migrations_ran(); |
935 | - if( isset( $migrations_ran[ $plugin_slug ] ) && isset( $migrations_ran[ $plugin_slug ][ $version ] ) ){ |
|
936 | - return $migrations_ran[ $plugin_slug ][ $version ]; |
|
937 | - }else{ |
|
935 | + if (isset($migrations_ran[$plugin_slug]) && isset($migrations_ran[$plugin_slug][$version])) { |
|
936 | + return $migrations_ran[$plugin_slug][$version]; |
|
937 | + } else { |
|
938 | 938 | return NULL; |
939 | 939 | } |
940 | 940 | } |
@@ -946,20 +946,20 @@ discard block |
||
946 | 946 | * @return bool |
947 | 947 | * @throws \EE_Error |
948 | 948 | */ |
949 | - public function reattempt(){ |
|
949 | + public function reattempt() { |
|
950 | 950 | //find if the last-ran script was borked |
951 | 951 | //set it as being non-borked (we shouldn't ever get DMSs that we don't recognize) |
952 | 952 | //add an 'error' saying that we attempted to reset |
953 | 953 | //does it have a stage that was borked too? if so make it no longer borked |
954 | 954 | //add an 'error' saying we attempted to reset |
955 | 955 | $last_ran_script = $this->get_last_ran_script(); |
956 | - if( $last_ran_script instanceof EE_DMS_Unknown_1_0_0 ){ |
|
956 | + if ($last_ran_script instanceof EE_DMS_Unknown_1_0_0) { |
|
957 | 957 | //if it was an error DMS, just mark it as complete (if another error occurs it will overwrite it) |
958 | 958 | $last_ran_script->set_completed(); |
959 | - }elseif( $last_ran_script instanceof EE_Data_Migration_Script_Base ) { |
|
959 | + }elseif ($last_ran_script instanceof EE_Data_Migration_Script_Base) { |
|
960 | 960 | $last_ran_script->reattempt(); |
961 | - }else{ |
|
962 | - throw new EE_Error( sprintf( __( 'Unable to reattempt the last ran migration script because it was not a valid migration script. || It was %s', 'event_espresso' ), print_r( $last_ran_script, true ) ) ); |
|
961 | + } else { |
|
962 | + throw new EE_Error(sprintf(__('Unable to reattempt the last ran migration script because it was not a valid migration script. || It was %s', 'event_espresso'), print_r($last_ran_script, true))); |
|
963 | 963 | } |
964 | 964 | return $this->_save_migrations_ran(); |
965 | 965 | } |
@@ -969,19 +969,19 @@ discard block |
||
969 | 969 | * @param string $plugin_slug like 'Core', 'Mailchimp', 'Calendar', etc |
970 | 970 | * @return boolean |
971 | 971 | */ |
972 | - public function migration_has_ran( $version, $plugin_slug = 'Core' ) { |
|
973 | - return $this->get_migration_ran( $version, $plugin_slug ) !== NULL; |
|
972 | + public function migration_has_ran($version, $plugin_slug = 'Core') { |
|
973 | + return $this->get_migration_ran($version, $plugin_slug) !== NULL; |
|
974 | 974 | } |
975 | 975 | /** |
976 | 976 | * Enqueues this ee plugin to have its data initialized |
977 | 977 | * @param string $plugin_slug either 'Core' or EE_Addon::name()'s return value |
978 | 978 | */ |
979 | - public function enqueue_db_initialization_for( $plugin_slug ) { |
|
979 | + public function enqueue_db_initialization_for($plugin_slug) { |
|
980 | 980 | $queue = $this->get_db_initialization_queue(); |
981 | - if( ! in_array( $plugin_slug, $queue ) ) { |
|
981 | + if ( ! in_array($plugin_slug, $queue)) { |
|
982 | 982 | $queue[] = $plugin_slug; |
983 | 983 | } |
984 | - update_option( self::db_init_queue_option_name, $queue ); |
|
984 | + update_option(self::db_init_queue_option_name, $queue); |
|
985 | 985 | } |
986 | 986 | /** |
987 | 987 | * Calls EE_Addon::initialize_db_if_no_migrations_required() on each addon |
@@ -991,26 +991,26 @@ discard block |
||
991 | 991 | public function initialize_db_for_enqueued_ee_plugins() { |
992 | 992 | // EEH_Debug_Tools::instance()->start_timer( 'initialize_db_for_enqueued_ee_plugins' ); |
993 | 993 | $queue = $this->get_db_initialization_queue(); |
994 | - foreach( $queue as $plugin_slug ) { |
|
995 | - $most_up_to_date_dms = $this->get_most_up_to_date_dms( $plugin_slug ); |
|
996 | - if( ! $most_up_to_date_dms ) { |
|
994 | + foreach ($queue as $plugin_slug) { |
|
995 | + $most_up_to_date_dms = $this->get_most_up_to_date_dms($plugin_slug); |
|
996 | + if ( ! $most_up_to_date_dms) { |
|
997 | 997 | //if there is NO DMS for this plugin, obviously there's no schema to verify anyways |
998 | 998 | $verify_db = false; |
999 | - }else{ |
|
1000 | - $most_up_to_date_dms_migrates_to = $this->script_migrates_to_version( $most_up_to_date_dms ); |
|
1001 | - $verify_db = $this->database_needs_updating_to( $most_up_to_date_dms_migrates_to ); |
|
999 | + } else { |
|
1000 | + $most_up_to_date_dms_migrates_to = $this->script_migrates_to_version($most_up_to_date_dms); |
|
1001 | + $verify_db = $this->database_needs_updating_to($most_up_to_date_dms_migrates_to); |
|
1002 | 1002 | } |
1003 | - if( $plugin_slug == 'Core' ){ |
|
1003 | + if ($plugin_slug == 'Core') { |
|
1004 | 1004 | EE_System::instance()->initialize_db_if_no_migrations_required( |
1005 | 1005 | false, |
1006 | 1006 | $verify_db |
1007 | 1007 | ); |
1008 | - }else{ |
|
1008 | + } else { |
|
1009 | 1009 | //just loop through the addons to make sure their database is setup |
1010 | - foreach( EE_Registry::instance()->addons as $addon ) { |
|
1011 | - if( $addon->name() == $plugin_slug ) { |
|
1010 | + foreach (EE_Registry::instance()->addons as $addon) { |
|
1011 | + if ($addon->name() == $plugin_slug) { |
|
1012 | 1012 | |
1013 | - $addon->initialize_db_if_no_migrations_required( $verify_db ); |
|
1013 | + $addon->initialize_db_if_no_migrations_required($verify_db); |
|
1014 | 1014 | break; |
1015 | 1015 | } |
1016 | 1016 | } |
@@ -1020,7 +1020,7 @@ discard block |
||
1020 | 1020 | // EEH_Debug_Tools::instance()->show_times(); |
1021 | 1021 | //because we just initialized the DBs for the enqueued ee plugins |
1022 | 1022 | //we don't need to keep remembering which ones needed to be initialized |
1023 | - delete_option( self::db_init_queue_option_name ); |
|
1023 | + delete_option(self::db_init_queue_option_name); |
|
1024 | 1024 | } |
1025 | 1025 | |
1026 | 1026 | /** |
@@ -1029,8 +1029,8 @@ discard block |
||
1029 | 1029 | * 'Core', or the return value of EE_Addon::name() for an addon |
1030 | 1030 | * @return array |
1031 | 1031 | */ |
1032 | - public function get_db_initialization_queue(){ |
|
1033 | - return get_option ( self::db_init_queue_option_name, array() ); |
|
1032 | + public function get_db_initialization_queue() { |
|
1033 | + return get_option(self::db_init_queue_option_name, array()); |
|
1034 | 1034 | } |
1035 | 1035 | |
1036 | 1036 | /** |
@@ -1039,13 +1039,13 @@ discard block |
||
1039 | 1039 | * @throws \EE_Error |
1040 | 1040 | */ |
1041 | 1041 | protected function _get_table_analysis() { |
1042 | - if( $this->_table_analysis instanceof TableAnalysis ) { |
|
1042 | + if ($this->_table_analysis instanceof TableAnalysis) { |
|
1043 | 1043 | return $this->_table_analysis; |
1044 | 1044 | } else { |
1045 | 1045 | throw new \EE_Error( |
1046 | 1046 | sprintf( |
1047 | - __( 'Table analysis class on class %1$s is not set properly.', 'event_espresso'), |
|
1048 | - get_class( $this ) |
|
1047 | + __('Table analysis class on class %1$s is not set properly.', 'event_espresso'), |
|
1048 | + get_class($this) |
|
1049 | 1049 | ) |
1050 | 1050 | ); |
1051 | 1051 | } |
@@ -1057,13 +1057,13 @@ discard block |
||
1057 | 1057 | * @throws \EE_Error |
1058 | 1058 | */ |
1059 | 1059 | protected function _get_table_manager() { |
1060 | - if( $this->_table_manager instanceof TableManager ) { |
|
1060 | + if ($this->_table_manager instanceof TableManager) { |
|
1061 | 1061 | return $this->_table_manager; |
1062 | 1062 | } else { |
1063 | 1063 | throw new \EE_Error( |
1064 | 1064 | sprintf( |
1065 | - __( 'Table manager class on class %1$s is not set properly.', 'event_espresso'), |
|
1066 | - get_class( $this ) |
|
1065 | + __('Table manager class on class %1$s is not set properly.', 'event_espresso'), |
|
1066 | + get_class($this) |
|
1067 | 1067 | ) |
1068 | 1068 | ); |
1069 | 1069 | } |
@@ -56,10 +56,10 @@ discard block |
||
56 | 56 | |
57 | 57 | |
58 | 58 | |
59 | - /** |
|
60 | - * @return string |
|
61 | - * @throws \EE_Error |
|
62 | - */ |
|
59 | + /** |
|
60 | + * @return string |
|
61 | + * @throws \EE_Error |
|
62 | + */ |
|
63 | 63 | public function generate_reg_form() { |
64 | 64 | // create empty form so that things don't break |
65 | 65 | $this->reg_form = new EE_Form_Section_Proper(); |
@@ -68,26 +68,26 @@ discard block |
||
68 | 68 | |
69 | 69 | |
70 | 70 | |
71 | - /** |
|
72 | - * @return boolean |
|
73 | - * @throws \RuntimeException |
|
74 | - * @throws \EE_Error |
|
75 | - */ |
|
71 | + /** |
|
72 | + * @return boolean |
|
73 | + * @throws \RuntimeException |
|
74 | + * @throws \EE_Error |
|
75 | + */ |
|
76 | 76 | public function process_reg_step() { |
77 | - // ensure all data gets refreshed from the db |
|
77 | + // ensure all data gets refreshed from the db |
|
78 | 78 | $this->checkout->refresh_all_entities( true ); |
79 | 79 | // ensures that all details and statuses for transaction, registration, and payments are updated |
80 | 80 | $txn_update_params = $this->_finalize_transaction(); |
81 | - // maybe send messages |
|
82 | - $this->_set_notification_triggers(); |
|
83 | - // send messages |
|
84 | - /** @type EE_Registration_Processor $registration_processor */ |
|
85 | - $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
86 | - $registration_processor->trigger_registration_update_notifications( |
|
87 | - $this->checkout->transaction->primary_registration(), |
|
88 | - $txn_update_params |
|
89 | - ); |
|
90 | - // set a hook point |
|
81 | + // maybe send messages |
|
82 | + $this->_set_notification_triggers(); |
|
83 | + // send messages |
|
84 | + /** @type EE_Registration_Processor $registration_processor */ |
|
85 | + $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
86 | + $registration_processor->trigger_registration_update_notifications( |
|
87 | + $this->checkout->transaction->primary_registration(), |
|
88 | + $txn_update_params |
|
89 | + ); |
|
90 | + // set a hook point |
|
91 | 91 | do_action( |
92 | 92 | 'AHEE__EE_SPCO_Reg_Step_Finalize_Registration__process_reg_step__completed', |
93 | 93 | $this->checkout, |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | if ( |
105 | 105 | ! ( |
106 | 106 | $this->checkout->payment_method instanceof EE_Payment_Method |
107 | - && $this->checkout->payment_method->is_off_site() |
|
107 | + && $this->checkout->payment_method->is_off_site() |
|
108 | 108 | ) |
109 | 109 | ) { |
110 | 110 | // mark this reg step as completed |
@@ -116,14 +116,14 @@ discard block |
||
116 | 116 | |
117 | 117 | |
118 | 118 | |
119 | - /** |
|
120 | - * _finalize_transaction |
|
121 | - * ensures that all details and statuses for transaction, registration, and payments are updated |
|
122 | - * |
|
123 | - * @return array |
|
124 | - * @throws \RuntimeException |
|
125 | - * @throws \EE_Error |
|
126 | - */ |
|
119 | + /** |
|
120 | + * _finalize_transaction |
|
121 | + * ensures that all details and statuses for transaction, registration, and payments are updated |
|
122 | + * |
|
123 | + * @return array |
|
124 | + * @throws \RuntimeException |
|
125 | + * @throws \EE_Error |
|
126 | + */ |
|
127 | 127 | protected function _finalize_transaction() { |
128 | 128 | /** @type EE_Transaction_Processor $transaction_processor */ |
129 | 129 | $transaction_processor = EE_Registry::instance()->load_class( 'Transaction_Processor' ); |
@@ -139,17 +139,17 @@ discard block |
||
139 | 139 | } |
140 | 140 | // maybe update status, but don't save transaction just yet |
141 | 141 | $this->checkout->transaction->update_status_based_on_total_paid( false ); |
142 | - // this will result in the base session properties getting saved to the TXN_Session_data field |
|
142 | + // this will result in the base session properties getting saved to the TXN_Session_data field |
|
143 | 143 | $this->checkout->transaction->set_txn_session_data( |
144 | 144 | EE_Registry::instance()->SSN->get_session_data( null, true ) |
145 | 145 | ); |
146 | - // update the TXN if payment conditions have changed, but do NOT trigger notifications, |
|
147 | - // because we will do that in process_reg_step() after setting some more triggers |
|
146 | + // update the TXN if payment conditions have changed, but do NOT trigger notifications, |
|
147 | + // because we will do that in process_reg_step() after setting some more triggers |
|
148 | 148 | return $transaction_processor->update_transaction_and_registrations_after_checkout_or_payment( |
149 | 149 | $this->checkout->transaction, |
150 | 150 | $this->checkout->payment, |
151 | 151 | $this->checkout->reg_cache_where_params, |
152 | - false |
|
152 | + false |
|
153 | 153 | ); |
154 | 154 | } |
155 | 155 | |
@@ -167,43 +167,43 @@ discard block |
||
167 | 167 | protected function _set_notification_triggers() { |
168 | 168 | |
169 | 169 | if ( $this->checkout->payment_method instanceof EE_Payment_Method ) { |
170 | - // let's start with the assumption that we need to trigger notifications |
|
171 | - // then toggle this to false for conditions where we know we don't need to |
|
172 | - $deliver_notifications = true; |
|
173 | - if ( |
|
174 | - // if SPCO revisit |
|
175 | - filter_var($this->checkout->revisit, FILTER_VALIDATE_BOOLEAN) |
|
176 | - // and TXN or REG statuses have NOT changed due to a payment |
|
177 | - && ! ( |
|
178 | - $this->checkout->transaction->txn_status_updated() |
|
179 | - || $this->checkout->any_reg_status_updated() |
|
180 | - ) |
|
181 | - ) { |
|
182 | - $deliver_notifications = false; |
|
183 | - } |
|
184 | - if ($this->checkout->payment_method->is_off_site()) { |
|
185 | - /** @var EE_Gateway $gateway */ |
|
186 | - $gateway = $this->checkout->payment_method->type_obj()->get_gateway(); |
|
187 | - // and the gateway uses a separate request to process the IPN |
|
188 | - if ( |
|
189 | - $gateway instanceof EE_Offsite_Gateway |
|
190 | - && $gateway->handle_IPN_in_this_request(\EE_Registry::instance()->REQ->params(), true) |
|
191 | - ) { |
|
192 | - // IPN request will handle triggering notifications |
|
193 | - $deliver_notifications = false; |
|
194 | - // no really... don't send any notices in this request |
|
195 | - remove_all_filters('FHEE__EED_Messages___maybe_registration__deliver_notifications'); |
|
196 | - add_filter( |
|
197 | - 'FHEE__EED_Messages___maybe_registration__deliver_notifications', |
|
198 | - '__return_false', |
|
199 | - 15 |
|
200 | - ); |
|
201 | - } |
|
202 | - } |
|
203 | - if ($deliver_notifications) { |
|
204 | - // send out notifications |
|
205 | - add_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications', '__return_true', 10); |
|
206 | - } |
|
170 | + // let's start with the assumption that we need to trigger notifications |
|
171 | + // then toggle this to false for conditions where we know we don't need to |
|
172 | + $deliver_notifications = true; |
|
173 | + if ( |
|
174 | + // if SPCO revisit |
|
175 | + filter_var($this->checkout->revisit, FILTER_VALIDATE_BOOLEAN) |
|
176 | + // and TXN or REG statuses have NOT changed due to a payment |
|
177 | + && ! ( |
|
178 | + $this->checkout->transaction->txn_status_updated() |
|
179 | + || $this->checkout->any_reg_status_updated() |
|
180 | + ) |
|
181 | + ) { |
|
182 | + $deliver_notifications = false; |
|
183 | + } |
|
184 | + if ($this->checkout->payment_method->is_off_site()) { |
|
185 | + /** @var EE_Gateway $gateway */ |
|
186 | + $gateway = $this->checkout->payment_method->type_obj()->get_gateway(); |
|
187 | + // and the gateway uses a separate request to process the IPN |
|
188 | + if ( |
|
189 | + $gateway instanceof EE_Offsite_Gateway |
|
190 | + && $gateway->handle_IPN_in_this_request(\EE_Registry::instance()->REQ->params(), true) |
|
191 | + ) { |
|
192 | + // IPN request will handle triggering notifications |
|
193 | + $deliver_notifications = false; |
|
194 | + // no really... don't send any notices in this request |
|
195 | + remove_all_filters('FHEE__EED_Messages___maybe_registration__deliver_notifications'); |
|
196 | + add_filter( |
|
197 | + 'FHEE__EED_Messages___maybe_registration__deliver_notifications', |
|
198 | + '__return_false', |
|
199 | + 15 |
|
200 | + ); |
|
201 | + } |
|
202 | + } |
|
203 | + if ($deliver_notifications) { |
|
204 | + // send out notifications |
|
205 | + add_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications', '__return_true', 10); |
|
206 | + } |
|
207 | 207 | } |
208 | 208 | } |
209 | 209 |
@@ -1,5 +1,5 @@ discard block |
||
1 | -<?php if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
|
2 | - exit( 'No direct script access allowed' ); |
|
1 | +<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
2 | + exit('No direct script access allowed'); |
|
3 | 3 | } |
4 | 4 | |
5 | 5 | |
@@ -21,9 +21,9 @@ discard block |
||
21 | 21 | * @access public |
22 | 22 | * @param EE_Checkout $checkout |
23 | 23 | */ |
24 | - public function __construct( EE_Checkout $checkout ) { |
|
24 | + public function __construct(EE_Checkout $checkout) { |
|
25 | 25 | $this->_slug = 'finalize_registration'; |
26 | - $this->_name = __( 'Finalize Registration', 'event_espresso' ); |
|
26 | + $this->_name = __('Finalize Registration', 'event_espresso'); |
|
27 | 27 | $this->_submit_button_text = $this->_name; |
28 | 28 | $this->_template = ''; |
29 | 29 | $this->checkout = $checkout; |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | */ |
47 | 47 | public function initialize_reg_step() { |
48 | 48 | // there's actually no reg form to process if this is the final step |
49 | - if ( $this->is_current_step() ) { |
|
49 | + if ($this->is_current_step()) { |
|
50 | 50 | $this->checkout->step = $_REQUEST['step'] = $this->slug(); |
51 | 51 | $this->checkout->action = $_REQUEST['action'] = 'process_reg_step'; |
52 | 52 | $this->checkout->generate_reg_form = false; |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | */ |
76 | 76 | public function process_reg_step() { |
77 | 77 | // ensure all data gets refreshed from the db |
78 | - $this->checkout->refresh_all_entities( true ); |
|
78 | + $this->checkout->refresh_all_entities(true); |
|
79 | 79 | // ensures that all details and statuses for transaction, registration, and payments are updated |
80 | 80 | $txn_update_params = $this->_finalize_transaction(); |
81 | 81 | // maybe send messages |
@@ -94,13 +94,13 @@ discard block |
||
94 | 94 | $txn_update_params |
95 | 95 | ); |
96 | 96 | // check if transaction has a primary registrant and that it has a related Attendee object |
97 | - if ( ! $this->_validate_primary_registrant() ) { |
|
97 | + if ( ! $this->_validate_primary_registrant()) { |
|
98 | 98 | return false; |
99 | 99 | } |
100 | 100 | // you don't have to go home but you can't stay here ! |
101 | 101 | $this->checkout->redirect = true; |
102 | 102 | $this->checkout->continue_reg = true; |
103 | - $this->checkout->json_response->set_redirect_url( $this->checkout->redirect_url ); |
|
103 | + $this->checkout->json_response->set_redirect_url($this->checkout->redirect_url); |
|
104 | 104 | if ( |
105 | 105 | ! ( |
106 | 106 | $this->checkout->payment_method instanceof EE_Payment_Method |
@@ -126,22 +126,22 @@ discard block |
||
126 | 126 | */ |
127 | 127 | protected function _finalize_transaction() { |
128 | 128 | /** @type EE_Transaction_Processor $transaction_processor */ |
129 | - $transaction_processor = EE_Registry::instance()->load_class( 'Transaction_Processor' ); |
|
129 | + $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor'); |
|
130 | 130 | //set revisit flag in txn processor |
131 | - $transaction_processor->set_revisit( $this->checkout->revisit ); |
|
131 | + $transaction_processor->set_revisit($this->checkout->revisit); |
|
132 | 132 | // at this point we'll consider a TXN to not have been abandoned |
133 | 133 | $this->checkout->transaction->toggle_abandoned_transaction_status(); |
134 | - if ( $this->checkout->cart instanceof EE_Cart ) { |
|
134 | + if ($this->checkout->cart instanceof EE_Cart) { |
|
135 | 135 | // save TXN data to the cart |
136 | 136 | $this->checkout->cart->get_grand_total()->save_this_and_descendants_to_txn( |
137 | 137 | $this->checkout->transaction->ID() |
138 | 138 | ); |
139 | 139 | } |
140 | 140 | // maybe update status, but don't save transaction just yet |
141 | - $this->checkout->transaction->update_status_based_on_total_paid( false ); |
|
141 | + $this->checkout->transaction->update_status_based_on_total_paid(false); |
|
142 | 142 | // this will result in the base session properties getting saved to the TXN_Session_data field |
143 | 143 | $this->checkout->transaction->set_txn_session_data( |
144 | - EE_Registry::instance()->SSN->get_session_data( null, true ) |
|
144 | + EE_Registry::instance()->SSN->get_session_data(null, true) |
|
145 | 145 | ); |
146 | 146 | // update the TXN if payment conditions have changed, but do NOT trigger notifications, |
147 | 147 | // because we will do that in process_reg_step() after setting some more triggers |
@@ -166,7 +166,7 @@ discard block |
||
166 | 166 | */ |
167 | 167 | protected function _set_notification_triggers() { |
168 | 168 | |
169 | - if ( $this->checkout->payment_method instanceof EE_Payment_Method ) { |
|
169 | + if ($this->checkout->payment_method instanceof EE_Payment_Method) { |
|
170 | 170 | // let's start with the assumption that we need to trigger notifications |
171 | 171 | // then toggle this to false for conditions where we know we don't need to |
172 | 172 | $deliver_notifications = true; |
@@ -216,9 +216,9 @@ discard block |
||
216 | 216 | * @throws \EE_Error |
217 | 217 | */ |
218 | 218 | protected function _validate_primary_registrant() { |
219 | - if ( ! $this->checkout->transaction_has_primary_registrant() ) { |
|
219 | + if ( ! $this->checkout->transaction_has_primary_registrant()) { |
|
220 | 220 | EE_Error::add_error( |
221 | - __( 'A valid Primary Registration for this Transaction could not be found.', 'event_espresso' ), |
|
221 | + __('A valid Primary Registration for this Transaction could not be found.', 'event_espresso'), |
|
222 | 222 | __FILE__, |
223 | 223 | __FUNCTION__, |
224 | 224 | __LINE__ |
@@ -229,7 +229,7 @@ discard block |
||
229 | 229 | } |
230 | 230 | // setup URL for redirect |
231 | 231 | $this->checkout->redirect_url = add_query_arg( |
232 | - array( 'e_reg_url_link' => $this->checkout->transaction->primary_registration()->reg_url_link() ), |
|
232 | + array('e_reg_url_link' => $this->checkout->transaction->primary_registration()->reg_url_link()), |
|
233 | 233 | $this->checkout->thank_you_page_url |
234 | 234 | ); |
235 | 235 | return true; |
@@ -242,7 +242,7 @@ discard block |
||
242 | 242 | */ |
243 | 243 | public function update_reg_step() { |
244 | 244 | EE_Error::doing_it_wrong( |
245 | - __CLASS__ . '::' . __FILE__, |
|
245 | + __CLASS__.'::'.__FILE__, |
|
246 | 246 | __( |
247 | 247 | 'Can not call update_reg_step() on the Finalize Registration reg step.', |
248 | 248 | 'event_espresso' |
@@ -5,63 +5,63 @@ |
||
5 | 5 | |
6 | 6 | interface HasSchemaInterface |
7 | 7 | { |
8 | - /** |
|
9 | - * Returns whatever is set as the nicename for the object. |
|
10 | - * |
|
11 | - * @return string |
|
12 | - */ |
|
13 | - public function getSchemaDescription(); |
|
8 | + /** |
|
9 | + * Returns whatever is set as the nicename for the object. |
|
10 | + * |
|
11 | + * @return string |
|
12 | + */ |
|
13 | + public function getSchemaDescription(); |
|
14 | 14 | |
15 | 15 | |
16 | - /** |
|
17 | - * Returns whatever is set as the $_schema_type property for the object. |
|
18 | - * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
19 | - * |
|
20 | - * @return string|array |
|
21 | - */ |
|
22 | - public function getSchemaType(); |
|
16 | + /** |
|
17 | + * Returns whatever is set as the $_schema_type property for the object. |
|
18 | + * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
19 | + * |
|
20 | + * @return string|array |
|
21 | + */ |
|
22 | + public function getSchemaType(); |
|
23 | 23 | |
24 | 24 | |
25 | - /** |
|
26 | - * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
27 | - * this method and return the properties for the schema. |
|
28 | - * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
29 | - * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
30 | - * |
|
31 | - * @return array |
|
32 | - */ |
|
33 | - public function getSchemaProperties(); |
|
25 | + /** |
|
26 | + * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
27 | + * this method and return the properties for the schema. |
|
28 | + * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
29 | + * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
30 | + * |
|
31 | + * @return array |
|
32 | + */ |
|
33 | + public function getSchemaProperties(); |
|
34 | 34 | |
35 | - /** |
|
36 | - * If a child class has enum values, they should override this method and provide a simple array |
|
37 | - * of the enum values. |
|
38 | - * The reason this is not a property on the class is because there may be filterable enum values that |
|
39 | - * are set on the instantiated object that could be filtered after construct. |
|
40 | - * |
|
41 | - * @return array |
|
42 | - */ |
|
43 | - public function getSchemaEnum(); |
|
35 | + /** |
|
36 | + * If a child class has enum values, they should override this method and provide a simple array |
|
37 | + * of the enum values. |
|
38 | + * The reason this is not a property on the class is because there may be filterable enum values that |
|
39 | + * are set on the instantiated object that could be filtered after construct. |
|
40 | + * |
|
41 | + * @return array |
|
42 | + */ |
|
43 | + public function getSchemaEnum(); |
|
44 | 44 | |
45 | - /** |
|
46 | - * This returns the value of the $_schema_format property on the object. |
|
47 | - * |
|
48 | - * @return string |
|
49 | - */ |
|
50 | - public function getSchemaFormat(); |
|
45 | + /** |
|
46 | + * This returns the value of the $_schema_format property on the object. |
|
47 | + * |
|
48 | + * @return string |
|
49 | + */ |
|
50 | + public function getSchemaFormat(); |
|
51 | 51 | |
52 | - /** |
|
53 | - * This returns the value of the $_schema_readonly property on the object. |
|
54 | - * |
|
55 | - * @return bool |
|
56 | - */ |
|
57 | - public function getSchemaReadonly(); |
|
52 | + /** |
|
53 | + * This returns the value of the $_schema_readonly property on the object. |
|
54 | + * |
|
55 | + * @return bool |
|
56 | + */ |
|
57 | + public function getSchemaReadonly(); |
|
58 | 58 | |
59 | 59 | |
60 | - /** |
|
61 | - * This returns elements used to represent this field in the json schema. |
|
62 | - * |
|
63 | - * @link http://json-schema.org/ |
|
64 | - * @return array |
|
65 | - */ |
|
66 | - public function getSchema(); |
|
60 | + /** |
|
61 | + * This returns elements used to represent this field in the json schema. |
|
62 | + * |
|
63 | + * @link http://json-schema.org/ |
|
64 | + * @return array |
|
65 | + */ |
|
66 | + public function getSchema(); |
|
67 | 67 | } |
68 | 68 | \ No newline at end of file |