Completed
Branch FET/10766/extract-activation-d... (f6eee0)
by
unknown
131:06 queued 110:06
created
core/db_models/fields/EE_Slug_Field.php 2 patches
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -3,15 +3,15 @@
 block discarded – undo
3 3
 
4 4
 class EE_Slug_Field extends EE_Text_Field_Base
5 5
 {
6
-    /**
7
-     * ensures string is usable in URLs
8
-     *
9
-     * @param string $value_inputted_for_field_on_model_object
10
-     * @return string
11
-     */
12
-    function prepare_for_set($value_inputted_for_field_on_model_object)
13
-    {
14
-        //reminder: function prepares for use in URLs, not making human-readable.
15
-        return sanitize_title($value_inputted_for_field_on_model_object);
16
-    }
6
+	/**
7
+	 * ensures string is usable in URLs
8
+	 *
9
+	 * @param string $value_inputted_for_field_on_model_object
10
+	 * @return string
11
+	 */
12
+	function prepare_for_set($value_inputted_for_field_on_model_object)
13
+	{
14
+		//reminder: function prepares for use in URLs, not making human-readable.
15
+		return sanitize_title($value_inputted_for_field_on_model_object);
16
+	}
17 17
 }
18 18
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@
 block discarded – undo
1 1
 <?php
2
-require_once(EE_MODELS . 'fields/EE_Text_Field_Base.php');
2
+require_once(EE_MODELS.'fields/EE_Text_Field_Base.php');
3 3
 
4 4
 class EE_Slug_Field extends EE_Text_Field_Base
5 5
 {
Please login to merge, or discard this patch.
core/db_models/fields/EE_Maybe_Serialized_Text_Field.php 2 patches
Indentation   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -8,36 +8,36 @@
 block discarded – undo
8 8
 
9 9
 class EE_Maybe_Serialized_Text_Field extends EE_Serialized_Text_Field
10 10
 {
11
-    /**
12
-     * Value could be an array or a string. If its an array, serialize it. Otherwise, leave it as a string
13
-     *
14
-     * @param array|string $value_of_field_on_model_object
15
-     * @return string (possibly serialized)
16
-     */
17
-    function prepare_for_use_in_db($value_of_field_on_model_object)
18
-    {
19
-        if (is_array($value_of_field_on_model_object)) {
20
-            return parent::prepare_for_use_in_db($value_of_field_on_model_object);
21
-        } else {
22
-            return $value_of_field_on_model_object;
23
-        }
24
-    }
11
+	/**
12
+	 * Value could be an array or a string. If its an array, serialize it. Otherwise, leave it as a string
13
+	 *
14
+	 * @param array|string $value_of_field_on_model_object
15
+	 * @return string (possibly serialized)
16
+	 */
17
+	function prepare_for_use_in_db($value_of_field_on_model_object)
18
+	{
19
+		if (is_array($value_of_field_on_model_object)) {
20
+			return parent::prepare_for_use_in_db($value_of_field_on_model_object);
21
+		} else {
22
+			return $value_of_field_on_model_object;
23
+		}
24
+	}
25 25
 
26
-    /**
27
-     * Formats the array (or string) according to $schema. Right now, just implode with commas
28
-     *
29
-     * @param type $value_on_field_to_be_outputted
30
-     * @param type $schema
31
-     * @return strubg
32
-     */
33
-    function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null)
34
-    {
35
-        $pretty_value = null;
36
-        if (is_array($value_on_field_to_be_outputted)) {
37
-            $pretty_value = parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema);
38
-        } else {
39
-            $pretty_value = $value_on_field_to_be_outputted;
40
-        }
41
-        return $pretty_value;
42
-    }
26
+	/**
27
+	 * Formats the array (or string) according to $schema. Right now, just implode with commas
28
+	 *
29
+	 * @param type $value_on_field_to_be_outputted
30
+	 * @param type $schema
31
+	 * @return strubg
32
+	 */
33
+	function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null)
34
+	{
35
+		$pretty_value = null;
36
+		if (is_array($value_on_field_to_be_outputted)) {
37
+			$pretty_value = parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema);
38
+		} else {
39
+			$pretty_value = $value_on_field_to_be_outputted;
40
+		}
41
+		return $pretty_value;
42
+	}
43 43
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@
 block discarded – undo
4 4
 /**
5 5
  * For a db text column, which can either be an array in PHP code or a string.
6 6
  */
7
-require_once(EE_MODELS . 'fields/EE_Text_Field_Base.php');
7
+require_once(EE_MODELS.'fields/EE_Text_Field_Base.php');
8 8
 
9 9
 class EE_Maybe_Serialized_Text_Field extends EE_Serialized_Text_Field
10 10
 {
Please login to merge, or discard this patch.
core/db_models/fields/EE_WP_Post_Type_Field.php.php 1 patch
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -5,12 +5,12 @@
 block discarded – undo
5 5
  */
6 6
 class EE_WP_Post_Type_Field extends EE_DB_Only_Text_Field
7 7
 {
8
-    /**
9
-     * @param string $post_type the exact string to be used for the post type
10
-     *                          of all these post type model objects/rows
11
-     */
12
-    function __construct($post_type)
13
-    {
14
-        parent::__construct('post_type', __("Post Type", 'event_espresso'), false, $post_type);
15
-    }
8
+	/**
9
+	 * @param string $post_type the exact string to be used for the post type
10
+	 *                          of all these post type model objects/rows
11
+	 */
12
+	function __construct($post_type)
13
+	{
14
+		parent::__construct('post_type', __("Post Type", 'event_espresso'), false, $post_type);
15
+	}
16 16
 }
17 17
\ No newline at end of file
Please login to merge, or discard this patch.
core/db_models/fields/EE_DB_Only_Text_Field.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@
 block discarded – undo
1 1
 <?php
2
-require_once(EE_MODELS . 'fields/EE_DB_Only_Field_Base.php');
2
+require_once(EE_MODELS.'fields/EE_DB_Only_Field_Base.php');
3 3
 
4 4
 class EE_DB_Only_Text_Field extends EE_DB_Only_Field_Base
5 5
 {
Please login to merge, or discard this patch.
core/db_models/fields/EE_Plain_Text_Field.php 2 patches
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -3,14 +3,14 @@
 block discarded – undo
3 3
 
4 4
 class EE_Plain_Text_Field extends EE_Text_Field_Base
5 5
 {
6
-    /**
7
-     * removes all tags when setting
8
-     *
9
-     * @param string $value_inputted_for_field_on_model_object
10
-     * @return string
11
-     */
12
-    function prepare_for_set($value_inputted_for_field_on_model_object)
13
-    {
14
-        return wp_strip_all_tags(parent::prepare_for_set($value_inputted_for_field_on_model_object));
15
-    }
6
+	/**
7
+	 * removes all tags when setting
8
+	 *
9
+	 * @param string $value_inputted_for_field_on_model_object
10
+	 * @return string
11
+	 */
12
+	function prepare_for_set($value_inputted_for_field_on_model_object)
13
+	{
14
+		return wp_strip_all_tags(parent::prepare_for_set($value_inputted_for_field_on_model_object));
15
+	}
16 16
 }
17 17
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@
 block discarded – undo
1 1
 <?php
2
-require_once(EE_MODELS . 'fields/EE_Text_Field_Base.php');
2
+require_once(EE_MODELS.'fields/EE_Text_Field_Base.php');
3 3
 
4 4
 class EE_Plain_Text_Field extends EE_Text_Field_Base
5 5
 {
Please login to merge, or discard this patch.
core/db_models/relations/EE_Model_Relation_Base.php 3 patches
Doc Comments   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -133,9 +133,9 @@  discard block
 block discarded – undo
133 133
 
134 134
     /**
135 135
      * @param        $other_table
136
-     * @param        $other_table_alias
136
+     * @param        string $other_table_alias
137 137
      * @param        $other_table_column
138
-     * @param        $this_table_alias
138
+     * @param        string $this_table_alias
139 139
      * @param        $this_table_join_column
140 140
      * @param string $extra_join_sql
141 141
      * @return string
@@ -189,7 +189,7 @@  discard block
 block discarded – undo
189 189
      * Alters the $query_params to disable default where conditions, unless otherwise specified
190 190
      *
191 191
      * @param string $query_params
192
-     * @return array
192
+     * @return string
193 193
      */
194 194
     protected function _disable_default_where_conditions_on_query_param($query_params)
195 195
     {
@@ -206,7 +206,7 @@  discard block
 block discarded – undo
206 206
      * Note: If the related model is extends EEM_Soft_Delete_Base, then the related
207 207
      * model objects will only be soft-deleted.
208 208
      *
209
-     * @param EE_Base_Class|int|string $model_object_or_id
209
+     * @param EE_Base_Class|null $model_object_or_id
210 210
      * @param array                    $query_params
211 211
      * @return int of how many related models got deleted
212 212
      * @throws \EE_Error
@@ -237,7 +237,7 @@  discard block
 block discarded – undo
237 237
      * Note: If the related model is extends EEM_Soft_Delete_Base, then the related
238 238
      * model objects will only be soft-deleted.
239 239
      *
240
-     * @param EE_Base_Class|int|string $model_object_or_id
240
+     * @param EE_Base_Class|null $model_object_or_id
241 241
      * @param array                    $query_params
242 242
      * @return int of how many related models got deleted
243 243
      * @throws \EE_Error
Please login to merge, or discard this patch.
Indentation   +491 added lines, -491 removed lines patch added patch discarded remove patch
@@ -17,496 +17,496 @@
 block discarded – undo
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
-     * @throws EE_Error
115
-     * @throws InvalidArgumentException
116
-     * @throws ReflectionException
117
-     * @throws RuntimeException
118
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
119
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
120
-     */
121
-    protected function _get_model($model_name)
122
-    {
123
-        if(empty($model_name)) {
124
-            throw new RuntimeException(
125
-                sprintf(
126
-                    esc_html__('Invalid Model name provided: "%1$s".', 'event_espresso'),
127
-                    $model_name
128
-                )
129
-            );
130
-        }
131
-        $modelInstance = EE_Registry::instance()->load_model($model_name);
132
-        $modelInstance->set_timezone($this->_timezone);
133
-        return $modelInstance;
134
-    }
135
-
136
-
137
-    /**
138
-     * entirely possible that relations may be called from a model and we need to make sure those relations have their
139
-     * timezone set correctly.
140
-     *
141
-     * @param string $timezone timezone to set.
142
-     */
143
-    public function set_timezone($timezone)
144
-    {
145
-        if ($timezone !== null) {
146
-            $this->_timezone = $timezone;
147
-        }
148
-    }
149
-
150
-
151
-    /**
152
-     * @param        $other_table
153
-     * @param        $other_table_alias
154
-     * @param        $other_table_column
155
-     * @param        $this_table_alias
156
-     * @param        $this_table_join_column
157
-     * @param string $extra_join_sql
158
-     * @return string
159
-     */
160
-    protected function _left_join(
161
-        $other_table,
162
-        $other_table_alias,
163
-        $other_table_column,
164
-        $this_table_alias,
165
-        $this_table_join_column,
166
-        $extra_join_sql = ''
167
-    ) {
168
-        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" : '');
169
-    }
170
-
171
-
172
-    /**
173
-     * Gets all the model objects of type of other model related to $model_object,
174
-     * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation.
175
-     * For both of those child classes, $model_object must be saved so that it has an ID before querying,
176
-     * otherwise an error will be thrown. Note: by default we disable default_where_conditions
177
-     * EE_Belongs_To_Relation doesn't need to be saved before querying.
178
-     *
179
-     * @param EE_Base_Class|int $model_object_or_id                      or the primary key of this model
180
-     * @param array             $query_params                            like EEM_Base::get_all's $query_params
181
-     * @param boolean           $values_already_prepared_by_model_object @deprecated since 4.8.1
182
-     * @return EE_Base_Class[]
183
-     * @throws \EE_Error
184
-     */
185
-    public function get_all_related(
186
-        $model_object_or_id,
187
-        $query_params = array(),
188
-        $values_already_prepared_by_model_object = false
189
-    ) {
190
-        if ($values_already_prepared_by_model_object !== false) {
191
-            EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related',
192
-                __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'),
193
-                '4.8.1');
194
-        }
195
-        $query_params                                      = $this->_disable_default_where_conditions_on_query_param($query_params);
196
-        $query_param_where_this_model_pk                   = $this->get_this_model()->get_this_model_name()
197
-                                                             . "."
198
-                                                             . $this->get_this_model()->get_primary_key_field()->get_name();
199
-        $model_object_id                                   = $this->_get_model_object_id($model_object_or_id);
200
-        $query_params[0][$query_param_where_this_model_pk] = $model_object_id;
201
-        return $this->get_other_model()->get_all($query_params);
202
-    }
203
-
204
-
205
-    /**
206
-     * Alters the $query_params to disable default where conditions, unless otherwise specified
207
-     *
208
-     * @param string $query_params
209
-     * @return array
210
-     */
211
-    protected function _disable_default_where_conditions_on_query_param($query_params)
212
-    {
213
-        if (! isset($query_params['default_where_conditions'])) {
214
-            $query_params['default_where_conditions'] = 'none';
215
-        }
216
-        return $query_params;
217
-    }
218
-
219
-
220
-    /**
221
-     * Deletes the related model objects which meet the query parameters. If no
222
-     * parameters are specified, then all related model objects will be deleted.
223
-     * Note: If the related model is extends EEM_Soft_Delete_Base, then the related
224
-     * model objects will only be soft-deleted.
225
-     *
226
-     * @param EE_Base_Class|int|string $model_object_or_id
227
-     * @param array                    $query_params
228
-     * @return int of how many related models got deleted
229
-     * @throws \EE_Error
230
-     */
231
-    public function delete_all_related($model_object_or_id, $query_params = array())
232
-    {
233
-        //for each thing we would delete,
234
-        $related_model_objects = $this->get_all_related($model_object_or_id, $query_params);
235
-        //determine if it's blocked by anything else before it can be deleted
236
-        $deleted_count = 0;
237
-        foreach ($related_model_objects as $related_model_object) {
238
-            $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object,
239
-                $model_object_or_id);
240
-            /* @var $model_object_or_id EE_Base_Class */
241
-            if (! $delete_is_blocked) {
242
-                $this->remove_relation_to($model_object_or_id, $related_model_object);
243
-                $related_model_object->delete();
244
-                $deleted_count++;
245
-            }
246
-        }
247
-        return $deleted_count;
248
-    }
249
-
250
-
251
-    /**
252
-     * Deletes the related model objects which meet the query parameters. If no
253
-     * parameters are specified, then all related model objects will be deleted.
254
-     * Note: If the related model is extends EEM_Soft_Delete_Base, then the related
255
-     * model objects will only be soft-deleted.
256
-     *
257
-     * @param EE_Base_Class|int|string $model_object_or_id
258
-     * @param array                    $query_params
259
-     * @return int of how many related models got deleted
260
-     * @throws \EE_Error
261
-     */
262
-    public function delete_related_permanently($model_object_or_id, $query_params = array())
263
-    {
264
-        //for each thing we would delete,
265
-        $related_model_objects = $this->get_all_related($model_object_or_id, $query_params);
266
-        //determine if it's blocked by anything else before it can be deleted
267
-        $deleted_count = 0;
268
-        foreach ($related_model_objects as $related_model_object) {
269
-            $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object,
270
-                $model_object_or_id);
271
-            /* @var $model_object_or_id EE_Base_Class */
272
-            if ($related_model_object instanceof EE_Soft_Delete_Base_Class) {
273
-                $this->remove_relation_to($model_object_or_id, $related_model_object);
274
-                $deleted_count++;
275
-                if (! $delete_is_blocked) {
276
-                    $related_model_object->delete_permanently();
277
-                } else {
278
-                    //delete is blocked
279
-                    //brent and darren, in this case, wanted to just soft delete it then
280
-                    $related_model_object->delete();
281
-                }
282
-            } else {
283
-                //its not a soft-deletable thing anyways. do the normal logic.
284
-                if (! $delete_is_blocked) {
285
-                    $this->remove_relation_to($model_object_or_id, $related_model_object);
286
-                    $related_model_object->delete();
287
-                    $deleted_count++;
288
-                }
289
-            }
290
-        }
291
-        return $deleted_count;
292
-    }
293
-
294
-
295
-    /**
296
-     * this just returns a model_object_id for incoming item that could be an object or id.
297
-     *
298
-     * @param  EE_Base_Class|int $model_object_or_id model object or the primary key of this model
299
-     * @throws EE_Error
300
-     * @return int
301
-     */
302
-    protected function _get_model_object_id($model_object_or_id)
303
-    {
304
-        $model_object_id = $model_object_or_id;
305
-        if ($model_object_or_id instanceof EE_Base_Class) {
306
-            $model_object_id = $model_object_or_id->ID();
307
-        }
308
-        if (! $model_object_id) {
309
-            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",
310
-                "event_espresso"), $this->get_other_model()->get_this_model_name(),
311
-                $this->get_this_model()->get_this_model_name()));
312
-        }
313
-        return $model_object_id;
314
-    }
315
-
316
-
317
-    /**
318
-     * Gets the SQL string for performing the join between this model and the other model.
319
-     *
320
-     * @param string $model_relation_chain like 'Event.Event_Venue.Venue'
321
-     * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk =
322
-     *                other_model_primary_table.fk" etc
323
-     */
324
-    abstract public function get_join_statement($model_relation_chain);
325
-
326
-
327
-    /**
328
-     * Adds a relationships between the two model objects provided. Each type of relationship handles this differently
329
-     * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this
330
-     * relationship only allows this model to be related to a single other model of this type)
331
-     *
332
-     * @param       $this_obj_or_id
333
-     * @param       $other_obj_or_id
334
-     * @param array $extra_join_model_fields_n_values
335
-     * @return \EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for
336
-     *                        $other_obj_or_id)
337
-     */
338
-    abstract public function add_relation_to(
339
-        $this_obj_or_id,
340
-        $other_obj_or_id,
341
-        $extra_join_model_fields_n_values = array()
342
-    );
343
-
344
-
345
-    /**
346
-     * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two
347
-     * model objects
348
-     *
349
-     * @param       $this_obj_or_id
350
-     * @param       $other_obj_or_id
351
-     * @param array $where_query
352
-     * @return bool
353
-     */
354
-    abstract public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array());
355
-
356
-
357
-    /**
358
-     * Removes ALL relation instances for this relation obj
359
-     *
360
-     * @param EE_Base_Class|int $this_obj_or_id
361
-     * @param array             $where_query_param like EEM_Base::get_all's $query_params[0] (where conditions)
362
-     * @return EE_Base_Class[]
363
-     * @throws \EE_Error
364
-     */
365
-    public function remove_relations($this_obj_or_id, $where_query_param = array())
366
-    {
367
-        $related_things = $this->get_all_related($this_obj_or_id, array($where_query_param));
368
-        $objs_removed   = array();
369
-        foreach ($related_things as $related_thing) {
370
-            $objs_removed[] = $this->remove_relation_to($this_obj_or_id, $related_thing);
371
-        }
372
-        return $objs_removed;
373
-    }
374
-
375
-
376
-    /**
377
-     * If you aren't allowed to delete this model when there are related models across this
378
-     * relation object, return true. Otherwise, if you can delete this model even though
379
-     * related objects exist, returns false.
380
-     *
381
-     * @return boolean
382
-     */
383
-    public function block_delete_if_related_models_exist()
384
-    {
385
-        return $this->_blocking_delete;
386
-    }
387
-
388
-
389
-    /**
390
-     * Gets the error message to show
391
-     *
392
-     * @return string
393
-     */
394
-    public function get_deletion_error_message()
395
-    {
396
-        if ($this->_blocking_delete_error_message) {
397
-            return $this->_blocking_delete_error_message;
398
-        } 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
+	 * @throws EE_Error
115
+	 * @throws InvalidArgumentException
116
+	 * @throws ReflectionException
117
+	 * @throws RuntimeException
118
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
119
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
120
+	 */
121
+	protected function _get_model($model_name)
122
+	{
123
+		if(empty($model_name)) {
124
+			throw new RuntimeException(
125
+				sprintf(
126
+					esc_html__('Invalid Model name provided: "%1$s".', 'event_espresso'),
127
+					$model_name
128
+				)
129
+			);
130
+		}
131
+		$modelInstance = EE_Registry::instance()->load_model($model_name);
132
+		$modelInstance->set_timezone($this->_timezone);
133
+		return $modelInstance;
134
+	}
135
+
136
+
137
+	/**
138
+	 * entirely possible that relations may be called from a model and we need to make sure those relations have their
139
+	 * timezone set correctly.
140
+	 *
141
+	 * @param string $timezone timezone to set.
142
+	 */
143
+	public function set_timezone($timezone)
144
+	{
145
+		if ($timezone !== null) {
146
+			$this->_timezone = $timezone;
147
+		}
148
+	}
149
+
150
+
151
+	/**
152
+	 * @param        $other_table
153
+	 * @param        $other_table_alias
154
+	 * @param        $other_table_column
155
+	 * @param        $this_table_alias
156
+	 * @param        $this_table_join_column
157
+	 * @param string $extra_join_sql
158
+	 * @return string
159
+	 */
160
+	protected function _left_join(
161
+		$other_table,
162
+		$other_table_alias,
163
+		$other_table_column,
164
+		$this_table_alias,
165
+		$this_table_join_column,
166
+		$extra_join_sql = ''
167
+	) {
168
+		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" : '');
169
+	}
170
+
171
+
172
+	/**
173
+	 * Gets all the model objects of type of other model related to $model_object,
174
+	 * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation.
175
+	 * For both of those child classes, $model_object must be saved so that it has an ID before querying,
176
+	 * otherwise an error will be thrown. Note: by default we disable default_where_conditions
177
+	 * EE_Belongs_To_Relation doesn't need to be saved before querying.
178
+	 *
179
+	 * @param EE_Base_Class|int $model_object_or_id                      or the primary key of this model
180
+	 * @param array             $query_params                            like EEM_Base::get_all's $query_params
181
+	 * @param boolean           $values_already_prepared_by_model_object @deprecated since 4.8.1
182
+	 * @return EE_Base_Class[]
183
+	 * @throws \EE_Error
184
+	 */
185
+	public function get_all_related(
186
+		$model_object_or_id,
187
+		$query_params = array(),
188
+		$values_already_prepared_by_model_object = false
189
+	) {
190
+		if ($values_already_prepared_by_model_object !== false) {
191
+			EE_Error::doing_it_wrong('EE_Model_Relation_Base::get_all_related',
192
+				__('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'),
193
+				'4.8.1');
194
+		}
195
+		$query_params                                      = $this->_disable_default_where_conditions_on_query_param($query_params);
196
+		$query_param_where_this_model_pk                   = $this->get_this_model()->get_this_model_name()
197
+															 . "."
198
+															 . $this->get_this_model()->get_primary_key_field()->get_name();
199
+		$model_object_id                                   = $this->_get_model_object_id($model_object_or_id);
200
+		$query_params[0][$query_param_where_this_model_pk] = $model_object_id;
201
+		return $this->get_other_model()->get_all($query_params);
202
+	}
203
+
204
+
205
+	/**
206
+	 * Alters the $query_params to disable default where conditions, unless otherwise specified
207
+	 *
208
+	 * @param string $query_params
209
+	 * @return array
210
+	 */
211
+	protected function _disable_default_where_conditions_on_query_param($query_params)
212
+	{
213
+		if (! isset($query_params['default_where_conditions'])) {
214
+			$query_params['default_where_conditions'] = 'none';
215
+		}
216
+		return $query_params;
217
+	}
218
+
219
+
220
+	/**
221
+	 * Deletes the related model objects which meet the query parameters. If no
222
+	 * parameters are specified, then all related model objects will be deleted.
223
+	 * Note: If the related model is extends EEM_Soft_Delete_Base, then the related
224
+	 * model objects will only be soft-deleted.
225
+	 *
226
+	 * @param EE_Base_Class|int|string $model_object_or_id
227
+	 * @param array                    $query_params
228
+	 * @return int of how many related models got deleted
229
+	 * @throws \EE_Error
230
+	 */
231
+	public function delete_all_related($model_object_or_id, $query_params = array())
232
+	{
233
+		//for each thing we would delete,
234
+		$related_model_objects = $this->get_all_related($model_object_or_id, $query_params);
235
+		//determine if it's blocked by anything else before it can be deleted
236
+		$deleted_count = 0;
237
+		foreach ($related_model_objects as $related_model_object) {
238
+			$delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object,
239
+				$model_object_or_id);
240
+			/* @var $model_object_or_id EE_Base_Class */
241
+			if (! $delete_is_blocked) {
242
+				$this->remove_relation_to($model_object_or_id, $related_model_object);
243
+				$related_model_object->delete();
244
+				$deleted_count++;
245
+			}
246
+		}
247
+		return $deleted_count;
248
+	}
249
+
250
+
251
+	/**
252
+	 * Deletes the related model objects which meet the query parameters. If no
253
+	 * parameters are specified, then all related model objects will be deleted.
254
+	 * Note: If the related model is extends EEM_Soft_Delete_Base, then the related
255
+	 * model objects will only be soft-deleted.
256
+	 *
257
+	 * @param EE_Base_Class|int|string $model_object_or_id
258
+	 * @param array                    $query_params
259
+	 * @return int of how many related models got deleted
260
+	 * @throws \EE_Error
261
+	 */
262
+	public function delete_related_permanently($model_object_or_id, $query_params = array())
263
+	{
264
+		//for each thing we would delete,
265
+		$related_model_objects = $this->get_all_related($model_object_or_id, $query_params);
266
+		//determine if it's blocked by anything else before it can be deleted
267
+		$deleted_count = 0;
268
+		foreach ($related_model_objects as $related_model_object) {
269
+			$delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object,
270
+				$model_object_or_id);
271
+			/* @var $model_object_or_id EE_Base_Class */
272
+			if ($related_model_object instanceof EE_Soft_Delete_Base_Class) {
273
+				$this->remove_relation_to($model_object_or_id, $related_model_object);
274
+				$deleted_count++;
275
+				if (! $delete_is_blocked) {
276
+					$related_model_object->delete_permanently();
277
+				} else {
278
+					//delete is blocked
279
+					//brent and darren, in this case, wanted to just soft delete it then
280
+					$related_model_object->delete();
281
+				}
282
+			} else {
283
+				//its not a soft-deletable thing anyways. do the normal logic.
284
+				if (! $delete_is_blocked) {
285
+					$this->remove_relation_to($model_object_or_id, $related_model_object);
286
+					$related_model_object->delete();
287
+					$deleted_count++;
288
+				}
289
+			}
290
+		}
291
+		return $deleted_count;
292
+	}
293
+
294
+
295
+	/**
296
+	 * this just returns a model_object_id for incoming item that could be an object or id.
297
+	 *
298
+	 * @param  EE_Base_Class|int $model_object_or_id model object or the primary key of this model
299
+	 * @throws EE_Error
300
+	 * @return int
301
+	 */
302
+	protected function _get_model_object_id($model_object_or_id)
303
+	{
304
+		$model_object_id = $model_object_or_id;
305
+		if ($model_object_or_id instanceof EE_Base_Class) {
306
+			$model_object_id = $model_object_or_id->ID();
307
+		}
308
+		if (! $model_object_id) {
309
+			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",
310
+				"event_espresso"), $this->get_other_model()->get_this_model_name(),
311
+				$this->get_this_model()->get_this_model_name()));
312
+		}
313
+		return $model_object_id;
314
+	}
315
+
316
+
317
+	/**
318
+	 * Gets the SQL string for performing the join between this model and the other model.
319
+	 *
320
+	 * @param string $model_relation_chain like 'Event.Event_Venue.Venue'
321
+	 * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk =
322
+	 *                other_model_primary_table.fk" etc
323
+	 */
324
+	abstract public function get_join_statement($model_relation_chain);
325
+
326
+
327
+	/**
328
+	 * Adds a relationships between the two model objects provided. Each type of relationship handles this differently
329
+	 * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this
330
+	 * relationship only allows this model to be related to a single other model of this type)
331
+	 *
332
+	 * @param       $this_obj_or_id
333
+	 * @param       $other_obj_or_id
334
+	 * @param array $extra_join_model_fields_n_values
335
+	 * @return \EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for
336
+	 *                        $other_obj_or_id)
337
+	 */
338
+	abstract public function add_relation_to(
339
+		$this_obj_or_id,
340
+		$other_obj_or_id,
341
+		$extra_join_model_fields_n_values = array()
342
+	);
343
+
344
+
345
+	/**
346
+	 * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two
347
+	 * model objects
348
+	 *
349
+	 * @param       $this_obj_or_id
350
+	 * @param       $other_obj_or_id
351
+	 * @param array $where_query
352
+	 * @return bool
353
+	 */
354
+	abstract public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array());
355
+
356
+
357
+	/**
358
+	 * Removes ALL relation instances for this relation obj
359
+	 *
360
+	 * @param EE_Base_Class|int $this_obj_or_id
361
+	 * @param array             $where_query_param like EEM_Base::get_all's $query_params[0] (where conditions)
362
+	 * @return EE_Base_Class[]
363
+	 * @throws \EE_Error
364
+	 */
365
+	public function remove_relations($this_obj_or_id, $where_query_param = array())
366
+	{
367
+		$related_things = $this->get_all_related($this_obj_or_id, array($where_query_param));
368
+		$objs_removed   = array();
369
+		foreach ($related_things as $related_thing) {
370
+			$objs_removed[] = $this->remove_relation_to($this_obj_or_id, $related_thing);
371
+		}
372
+		return $objs_removed;
373
+	}
374
+
375
+
376
+	/**
377
+	 * If you aren't allowed to delete this model when there are related models across this
378
+	 * relation object, return true. Otherwise, if you can delete this model even though
379
+	 * related objects exist, returns false.
380
+	 *
381
+	 * @return boolean
382
+	 */
383
+	public function block_delete_if_related_models_exist()
384
+	{
385
+		return $this->_blocking_delete;
386
+	}
387
+
388
+
389
+	/**
390
+	 * Gets the error message to show
391
+	 *
392
+	 * @return string
393
+	 */
394
+	public function get_deletion_error_message()
395
+	{
396
+		if ($this->_blocking_delete_error_message) {
397
+			return $this->_blocking_delete_error_message;
398
+		} else {
399 399
 //			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));
400
-            return sprintf(
401
-                __('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.',
402
-                    "event_espresso"),
403
-                $this->get_this_model()->item_name(1),
404
-                $this->get_other_model()->item_name(1),
405
-                $this->get_other_model()->item_name(2)
406
-            );
407
-        }
408
-    }
409
-
410
-    /**
411
-     * Returns whatever is set as the nicename for the object.
412
-     *
413
-     * @return string
414
-     */
415
-    public function getSchemaDescription()
416
-    {
417
-        $description = $this instanceof EE_Belongs_To_Relation
418
-            ? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso')
419
-            : esc_html__('The related %1$s entities to the %2$s.', 'event_espresso');
420
-        return sprintf(
421
-            $description,
422
-            $this->get_other_model()->get_this_model_name(),
423
-            $this->get_this_model()->get_this_model_name()
424
-        );
425
-    }
426
-
427
-    /**
428
-     * Returns whatever is set as the $_schema_type property for the object.
429
-     * Note: this will automatically add 'null' to the schema if the object is_nullable()
430
-     *
431
-     * @return string|array
432
-     */
433
-    public function getSchemaType()
434
-    {
435
-        return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array';
436
-    }
437
-
438
-    /**
439
-     * This is usually present when the $_schema_type property is 'object'.  Any child classes will need to override
440
-     * this method and return the properties for the schema.
441
-     * The reason this is not a property on the class is because there may be filters set on the values for the property
442
-     * that won't be exposed on construct.  For example enum type schemas may have the enum values filtered.
443
-     *
444
-     * @return array
445
-     */
446
-    public function getSchemaProperties()
447
-    {
448
-        return array();
449
-    }
450
-
451
-    /**
452
-     * If a child class has enum values, they should override this method and provide a simple array
453
-     * of the enum values.
454
-     * The reason this is not a property on the class is because there may be filterable enum values that
455
-     * are set on the instantiated object that could be filtered after construct.
456
-     *
457
-     * @return array
458
-     */
459
-    public function getSchemaEnum()
460
-    {
461
-        return array();
462
-    }
463
-
464
-    /**
465
-     * This returns the value of the $_schema_format property on the object.
466
-     *
467
-     * @return string
468
-     */
469
-    public function getSchemaFormat()
470
-    {
471
-        return array();
472
-    }
473
-
474
-    /**
475
-     * This returns the value of the $_schema_readonly property on the object.
476
-     *
477
-     * @return bool
478
-     */
479
-    public function getSchemaReadonly()
480
-    {
481
-        return true;
482
-    }
483
-
484
-    /**
485
-     * This returns elements used to represent this field in the json schema.
486
-     *
487
-     * @link http://json-schema.org/
488
-     * @return array
489
-     */
490
-    public function getSchema()
491
-    {
492
-        $schema = array(
493
-            'description' => $this->getSchemaDescription(),
494
-            'type' => $this->getSchemaType(),
495
-            'relation' => true,
496
-            'relation_type' => get_class($this),
497
-            'readonly' => $this->getSchemaReadonly()
498
-        );
499
-
500
-        if ($this instanceof EE_HABTM_Relation) {
501
-            $schema['joining_model_name'] = $this->get_join_model()->get_this_model_name();
502
-        }
503
-
504
-        if ($this->getSchemaType() === 'array') {
505
-            $schema['items'] = array(
506
-                'type' => 'object'
507
-            );
508
-        }
509
-
510
-        return $schema;
511
-    }
400
+			return sprintf(
401
+				__('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.',
402
+					"event_espresso"),
403
+				$this->get_this_model()->item_name(1),
404
+				$this->get_other_model()->item_name(1),
405
+				$this->get_other_model()->item_name(2)
406
+			);
407
+		}
408
+	}
409
+
410
+	/**
411
+	 * Returns whatever is set as the nicename for the object.
412
+	 *
413
+	 * @return string
414
+	 */
415
+	public function getSchemaDescription()
416
+	{
417
+		$description = $this instanceof EE_Belongs_To_Relation
418
+			? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso')
419
+			: esc_html__('The related %1$s entities to the %2$s.', 'event_espresso');
420
+		return sprintf(
421
+			$description,
422
+			$this->get_other_model()->get_this_model_name(),
423
+			$this->get_this_model()->get_this_model_name()
424
+		);
425
+	}
426
+
427
+	/**
428
+	 * Returns whatever is set as the $_schema_type property for the object.
429
+	 * Note: this will automatically add 'null' to the schema if the object is_nullable()
430
+	 *
431
+	 * @return string|array
432
+	 */
433
+	public function getSchemaType()
434
+	{
435
+		return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array';
436
+	}
437
+
438
+	/**
439
+	 * This is usually present when the $_schema_type property is 'object'.  Any child classes will need to override
440
+	 * this method and return the properties for the schema.
441
+	 * The reason this is not a property on the class is because there may be filters set on the values for the property
442
+	 * that won't be exposed on construct.  For example enum type schemas may have the enum values filtered.
443
+	 *
444
+	 * @return array
445
+	 */
446
+	public function getSchemaProperties()
447
+	{
448
+		return array();
449
+	}
450
+
451
+	/**
452
+	 * If a child class has enum values, they should override this method and provide a simple array
453
+	 * of the enum values.
454
+	 * The reason this is not a property on the class is because there may be filterable enum values that
455
+	 * are set on the instantiated object that could be filtered after construct.
456
+	 *
457
+	 * @return array
458
+	 */
459
+	public function getSchemaEnum()
460
+	{
461
+		return array();
462
+	}
463
+
464
+	/**
465
+	 * This returns the value of the $_schema_format property on the object.
466
+	 *
467
+	 * @return string
468
+	 */
469
+	public function getSchemaFormat()
470
+	{
471
+		return array();
472
+	}
473
+
474
+	/**
475
+	 * This returns the value of the $_schema_readonly property on the object.
476
+	 *
477
+	 * @return bool
478
+	 */
479
+	public function getSchemaReadonly()
480
+	{
481
+		return true;
482
+	}
483
+
484
+	/**
485
+	 * This returns elements used to represent this field in the json schema.
486
+	 *
487
+	 * @link http://json-schema.org/
488
+	 * @return array
489
+	 */
490
+	public function getSchema()
491
+	{
492
+		$schema = array(
493
+			'description' => $this->getSchemaDescription(),
494
+			'type' => $this->getSchemaType(),
495
+			'relation' => true,
496
+			'relation_type' => get_class($this),
497
+			'readonly' => $this->getSchemaReadonly()
498
+		);
499
+
500
+		if ($this instanceof EE_HABTM_Relation) {
501
+			$schema['joining_model_name'] = $this->get_join_model()->get_this_model_name();
502
+		}
503
+
504
+		if ($this->getSchemaType() === 'array') {
505
+			$schema['items'] = array(
506
+				'type' => 'object'
507
+			);
508
+		}
509
+
510
+		return $schema;
511
+	}
512 512
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -120,7 +120,7 @@  discard block
 block discarded – undo
120 120
      */
121 121
     protected function _get_model($model_name)
122 122
     {
123
-        if(empty($model_name)) {
123
+        if (empty($model_name)) {
124 124
             throw new RuntimeException(
125 125
                 sprintf(
126 126
                     esc_html__('Invalid Model name provided: "%1$s".', 'event_espresso'),
@@ -165,7 +165,7 @@  discard block
 block discarded – undo
165 165
         $this_table_join_column,
166 166
         $extra_join_sql = ''
167 167
     ) {
168
-        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" : '');
168
+        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" : '');
169 169
     }
170 170
 
171 171
 
@@ -210,7 +210,7 @@  discard block
 block discarded – undo
210 210
      */
211 211
     protected function _disable_default_where_conditions_on_query_param($query_params)
212 212
     {
213
-        if (! isset($query_params['default_where_conditions'])) {
213
+        if ( ! isset($query_params['default_where_conditions'])) {
214 214
             $query_params['default_where_conditions'] = 'none';
215 215
         }
216 216
         return $query_params;
@@ -238,7 +238,7 @@  discard block
 block discarded – undo
238 238
             $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models($related_model_object,
239 239
                 $model_object_or_id);
240 240
             /* @var $model_object_or_id EE_Base_Class */
241
-            if (! $delete_is_blocked) {
241
+            if ( ! $delete_is_blocked) {
242 242
                 $this->remove_relation_to($model_object_or_id, $related_model_object);
243 243
                 $related_model_object->delete();
244 244
                 $deleted_count++;
@@ -272,7 +272,7 @@  discard block
 block discarded – undo
272 272
             if ($related_model_object instanceof EE_Soft_Delete_Base_Class) {
273 273
                 $this->remove_relation_to($model_object_or_id, $related_model_object);
274 274
                 $deleted_count++;
275
-                if (! $delete_is_blocked) {
275
+                if ( ! $delete_is_blocked) {
276 276
                     $related_model_object->delete_permanently();
277 277
                 } else {
278 278
                     //delete is blocked
@@ -281,7 +281,7 @@  discard block
 block discarded – undo
281 281
                 }
282 282
             } else {
283 283
                 //its not a soft-deletable thing anyways. do the normal logic.
284
-                if (! $delete_is_blocked) {
284
+                if ( ! $delete_is_blocked) {
285 285
                     $this->remove_relation_to($model_object_or_id, $related_model_object);
286 286
                     $related_model_object->delete();
287 287
                     $deleted_count++;
@@ -305,7 +305,7 @@  discard block
 block discarded – undo
305 305
         if ($model_object_or_id instanceof EE_Base_Class) {
306 306
             $model_object_id = $model_object_or_id->ID();
307 307
         }
308
-        if (! $model_object_id) {
308
+        if ( ! $model_object_id) {
309 309
             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",
310 310
                 "event_espresso"), $this->get_other_model()->get_this_model_name(),
311 311
                 $this->get_this_model()->get_this_model_name()));
Please login to merge, or discard this patch.
core/db_models/relations/EE_Has_Many_Relation.php 2 patches
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-require_once(EE_MODELS . 'relations/EE_Model_Relation_Base.php');
2
+require_once(EE_MODELS.'relations/EE_Model_Relation_Base.php');
3 3
 
4 4
 
5 5
 /**
@@ -46,13 +46,13 @@  discard block
 block discarded – undo
46 46
         $this_table_pk_field  = $this->get_this_model()->get_primary_key_field();
47 47
         $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name());
48 48
         $pk_table_alias       = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain,
49
-                $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias();
49
+                $this->get_this_model()->get_this_model_name()).$this_table_pk_field->get_table_alias();
50 50
         $fk_table_alias       = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain,
51
-                $this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias();
51
+                $this->get_other_model()->get_this_model_name()).$other_table_fk_field->get_table_alias();
52 52
         $fk_table             = $this->get_other_model()->get_table_for_alias($fk_table_alias);
53 53
 
54 54
         return $this->_left_join($fk_table, $fk_table_alias, $other_table_fk_field->get_table_column(), $pk_table_alias,
55
-                $this_table_pk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias);
55
+                $this_table_pk_field->get_table_column()).$this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias);
56 56
     }
57 57
 
58 58
 
Please login to merge, or discard this patch.
Indentation   +81 added lines, -81 removed lines patch added patch discarded remove patch
@@ -13,93 +13,93 @@
 block discarded – undo
13 13
 class EE_Has_Many_Relation extends EE_Model_Relation_Base
14 14
 {
15 15
 
16
-    /**
17
-     * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the
18
-     * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but
19
-     * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get
20
-     * related models across the relation, and add-and-remove the relationships.
21
-     *
22
-     * @param boolean $block_deletes                 For this type of r elation, we block by default. If there are
23
-     *                                               related models across this relation, block (prevent and add an
24
-     *                                               error) the deletion of this model
25
-     * @param string  $blocking_delete_error_message a customized error message on blocking deletes instead of the
26
-     *                                               default
27
-     */
28
-    public function __construct($block_deletes = true, $blocking_delete_error_message = null)
29
-    {
30
-        parent::__construct($block_deletes, $blocking_delete_error_message);
31
-    }
16
+	/**
17
+	 * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the
18
+	 * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but
19
+	 * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get
20
+	 * related models across the relation, and add-and-remove the relationships.
21
+	 *
22
+	 * @param boolean $block_deletes                 For this type of r elation, we block by default. If there are
23
+	 *                                               related models across this relation, block (prevent and add an
24
+	 *                                               error) the deletion of this model
25
+	 * @param string  $blocking_delete_error_message a customized error message on blocking deletes instead of the
26
+	 *                                               default
27
+	 */
28
+	public function __construct($block_deletes = true, $blocking_delete_error_message = null)
29
+	{
30
+		parent::__construct($block_deletes, $blocking_delete_error_message);
31
+	}
32 32
 
33 33
 
34
-    /**
35
-     * Gets the SQL string for performing the join between this model and the other model.
36
-     *
37
-     * @param string $model_relation_chain like 'Event.Event_Venue.Venue'
38
-     * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk =
39
-     *                other_model_primary_table.fk" etc
40
-     * @throws \EE_Error
41
-     */
42
-    public function get_join_statement($model_relation_chain)
43
-    {
44
-        //create the sql string like
45
-        // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions
46
-        $this_table_pk_field  = $this->get_this_model()->get_primary_key_field();
47
-        $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name());
48
-        $pk_table_alias       = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain,
49
-                $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias();
50
-        $fk_table_alias       = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain,
51
-                $this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias();
52
-        $fk_table             = $this->get_other_model()->get_table_for_alias($fk_table_alias);
34
+	/**
35
+	 * Gets the SQL string for performing the join between this model and the other model.
36
+	 *
37
+	 * @param string $model_relation_chain like 'Event.Event_Venue.Venue'
38
+	 * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk =
39
+	 *                other_model_primary_table.fk" etc
40
+	 * @throws \EE_Error
41
+	 */
42
+	public function get_join_statement($model_relation_chain)
43
+	{
44
+		//create the sql string like
45
+		// LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions
46
+		$this_table_pk_field  = $this->get_this_model()->get_primary_key_field();
47
+		$other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name());
48
+		$pk_table_alias       = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain,
49
+				$this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias();
50
+		$fk_table_alias       = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain,
51
+				$this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias();
52
+		$fk_table             = $this->get_other_model()->get_table_for_alias($fk_table_alias);
53 53
 
54
-        return $this->_left_join($fk_table, $fk_table_alias, $other_table_fk_field->get_table_column(), $pk_table_alias,
55
-                $this_table_pk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias);
56
-    }
54
+		return $this->_left_join($fk_table, $fk_table_alias, $other_table_fk_field->get_table_column(), $pk_table_alias,
55
+				$this_table_pk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias);
56
+	}
57 57
 
58 58
 
59
-    /**
60
-     * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if
61
-     * you like.
62
-     *
63
-     * @param EE_Base_Class|int $this_obj_or_id
64
-     * @param EE_Base_Class|int $other_obj_or_id
65
-     * @param array             $extra_join_model_fields_n_values
66
-     * @return \EE_Base_Class
67
-     * @throws \EE_Error
68
-     */
69
-    public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array())
70
-    {
71
-        $this_model_obj  = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true);
72
-        $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true);
59
+	/**
60
+	 * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if
61
+	 * you like.
62
+	 *
63
+	 * @param EE_Base_Class|int $this_obj_or_id
64
+	 * @param EE_Base_Class|int $other_obj_or_id
65
+	 * @param array             $extra_join_model_fields_n_values
66
+	 * @return \EE_Base_Class
67
+	 * @throws \EE_Error
68
+	 */
69
+	public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array())
70
+	{
71
+		$this_model_obj  = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true);
72
+		$other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true);
73 73
 
74
-        //find the field on the other model which is a foreign key to this model
75
-        $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name());
76
-        if ($other_model_obj->get($fk_field_on_other_model->get_name()) != $this_model_obj->ID()) {
77
-            //set that field on the other model to this model's ID
78
-            $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID());
79
-            $other_model_obj->save();
80
-        }
81
-        return $other_model_obj;
82
-    }
74
+		//find the field on the other model which is a foreign key to this model
75
+		$fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name());
76
+		if ($other_model_obj->get($fk_field_on_other_model->get_name()) != $this_model_obj->ID()) {
77
+			//set that field on the other model to this model's ID
78
+			$other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID());
79
+			$other_model_obj->save();
80
+		}
81
+		return $other_model_obj;
82
+	}
83 83
 
84 84
 
85
-    /**
86
-     * Sets the other model object's foreign key to its default, instead of pointing to this model object.
87
-     * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it
88
-     *
89
-     * @param EE_Base_Class|int $this_obj_or_id
90
-     * @param EE_Base_Class|int $other_obj_or_id
91
-     * @param array             $where_query
92
-     * @return \EE_Base_Class
93
-     * @throws \EE_Error
94
-     */
95
-    public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array())
96
-    {
97
-        $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true);
98
-        //find the field on the other model which is a foreign key to this model
99
-        $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name());
100
-        //set that field on the other model to this model's ID
101
-        $other_model_obj->set($fk_field_on_other_model->get_name(), null, true);
102
-        $other_model_obj->save();
103
-        return $other_model_obj;
104
-    }
85
+	/**
86
+	 * Sets the other model object's foreign key to its default, instead of pointing to this model object.
87
+	 * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it
88
+	 *
89
+	 * @param EE_Base_Class|int $this_obj_or_id
90
+	 * @param EE_Base_Class|int $other_obj_or_id
91
+	 * @param array             $where_query
92
+	 * @return \EE_Base_Class
93
+	 * @throws \EE_Error
94
+	 */
95
+	public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array())
96
+	{
97
+		$other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true);
98
+		//find the field on the other model which is a foreign key to this model
99
+		$fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name());
100
+		//set that field on the other model to this model's ID
101
+		$other_model_obj->set($fk_field_on_other_model->get_name(), null, true);
102
+		$other_model_obj->save();
103
+		return $other_model_obj;
104
+	}
105 105
 }
Please login to merge, or discard this patch.
core/db_models/relations/EE_Has_Many_Any_Relation.php 2 patches
Indentation   +73 added lines, -73 removed lines patch added patch discarded remove patch
@@ -22,83 +22,83 @@
 block discarded – undo
22 22
 class EE_Has_Many_Any_Relation extends EE_Has_Many_Relation
23 23
 {
24 24
 
25
-    /**
26
-     * Gets the SQL string for performing the join between this model and the other model.
27
-     *
28
-     * @param string $model_relation_chain like 'Event.Event_Venue.Venue'
29
-     * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk =
30
-     *                other_model_primary_table.fk" etc
31
-     * @throws \EE_Error
32
-     */
33
-    public function get_join_statement($model_relation_chain)
34
-    {
35
-        //create the sql string like
36
-        // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions
37
-        $this_table_pk_field   = $this->get_this_model()->get_primary_key_field();
38
-        $other_table_fk_field  = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name());
39
-        $pk_table_alias        = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain,
40
-                $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias();
41
-        $fk_table_alias        = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain,
42
-                $this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias();
43
-        $fk_table              = $this->get_other_model()->get_table_for_alias($fk_table_alias);
44
-        $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name();
25
+	/**
26
+	 * Gets the SQL string for performing the join between this model and the other model.
27
+	 *
28
+	 * @param string $model_relation_chain like 'Event.Event_Venue.Venue'
29
+	 * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk =
30
+	 *                other_model_primary_table.fk" etc
31
+	 * @throws \EE_Error
32
+	 */
33
+	public function get_join_statement($model_relation_chain)
34
+	{
35
+		//create the sql string like
36
+		// LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions
37
+		$this_table_pk_field   = $this->get_this_model()->get_primary_key_field();
38
+		$other_table_fk_field  = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name());
39
+		$pk_table_alias        = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain,
40
+				$this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias();
41
+		$fk_table_alias        = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain,
42
+				$this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias();
43
+		$fk_table              = $this->get_other_model()->get_table_for_alias($fk_table_alias);
44
+		$field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name();
45 45
 
46
-        return $this->_left_join($fk_table,
47
-                $fk_table_alias,
48
-                $other_table_fk_field->get_table_column(),
49
-                $pk_table_alias,
50
-                $this_table_pk_field->get_table_column(),
51
-                $fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'")
52
-               . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias);
53
-    }
46
+		return $this->_left_join($fk_table,
47
+				$fk_table_alias,
48
+				$other_table_fk_field->get_table_column(),
49
+				$pk_table_alias,
50
+				$this_table_pk_field->get_table_column(),
51
+				$fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'")
52
+			   . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias);
53
+	}
54 54
 
55 55
 
56
-    /**
57
-     * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if
58
-     * you like.
59
-     *
60
-     * @param EE_Base_Class|int $this_obj_or_id
61
-     * @param EE_Base_Class|int $other_obj_or_id
62
-     * @param array             $extra_join_model_fields_n_values
63
-     * @return \EE_Base_Class
64
-     * @throws \EE_Error
65
-     */
66
-    public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array())
67
-    {
68
-        $this_model_obj  = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true);
69
-        $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true);
56
+	/**
57
+	 * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if
58
+	 * you like.
59
+	 *
60
+	 * @param EE_Base_Class|int $this_obj_or_id
61
+	 * @param EE_Base_Class|int $other_obj_or_id
62
+	 * @param array             $extra_join_model_fields_n_values
63
+	 * @return \EE_Base_Class
64
+	 * @throws \EE_Error
65
+	 */
66
+	public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array())
67
+	{
68
+		$this_model_obj  = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true);
69
+		$other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true);
70 70
 
71
-        //find the field on the other model which is a foreign key to this model
72
-        $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name());
73
-        //set that field on the other model to this model's ID
74
-        $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID());
75
-        $other_model_obj->set($this->get_other_model()->get_field_containing_related_model_name()->get_name(),
76
-            $this->get_this_model()->get_this_model_name());
77
-        $other_model_obj->save();
78
-        return $other_model_obj;
79
-    }
71
+		//find the field on the other model which is a foreign key to this model
72
+		$fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name());
73
+		//set that field on the other model to this model's ID
74
+		$other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID());
75
+		$other_model_obj->set($this->get_other_model()->get_field_containing_related_model_name()->get_name(),
76
+			$this->get_this_model()->get_this_model_name());
77
+		$other_model_obj->save();
78
+		return $other_model_obj;
79
+	}
80 80
 
81 81
 
82
-    /**
83
-     * Sets the other model object's foreign key to its default, instead of pointing to this model object.
84
-     * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it
85
-     *
86
-     * @param EE_Base_Class|int $this_obj_or_id
87
-     * @param EE_Base_Class|int $other_obj_or_id
88
-     * @param array             $where_query
89
-     * @return \EE_Base_Class
90
-     * @throws \EE_Error
91
-     */
92
-    public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array())
93
-    {
94
-        $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true);
95
-        //find the field on the other model which is a foreign key to this model
96
-        $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name());
97
-        //set that field on the other model to this model's ID
98
-        $other_model_obj->set($fk_field_on_other_model->get_name(), null, true);
99
-        $other_model_obj->set($this->get_other_model()->get_field_containing_related_model_name()->get_name(), null,
100
-            true);
101
-        $other_model_obj->save();
102
-        return $other_model_obj;
103
-    }
82
+	/**
83
+	 * Sets the other model object's foreign key to its default, instead of pointing to this model object.
84
+	 * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it
85
+	 *
86
+	 * @param EE_Base_Class|int $this_obj_or_id
87
+	 * @param EE_Base_Class|int $other_obj_or_id
88
+	 * @param array             $where_query
89
+	 * @return \EE_Base_Class
90
+	 * @throws \EE_Error
91
+	 */
92
+	public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array())
93
+	{
94
+		$other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true);
95
+		//find the field on the other model which is a foreign key to this model
96
+		$fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name());
97
+		//set that field on the other model to this model's ID
98
+		$other_model_obj->set($fk_field_on_other_model->get_name(), null, true);
99
+		$other_model_obj->set($this->get_other_model()->get_field_containing_related_model_name()->get_name(), null,
100
+			true);
101
+		$other_model_obj->save();
102
+		return $other_model_obj;
103
+	}
104 104
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
 /*
5 5
  *
6 6
  */
7
-require_once(EE_MODELS . 'relations/EE_Model_Relation_Base.php');
7
+require_once(EE_MODELS.'relations/EE_Model_Relation_Base.php');
8 8
 
9 9
 
10 10
 /**
@@ -37,9 +37,9 @@  discard block
 block discarded – undo
37 37
         $this_table_pk_field   = $this->get_this_model()->get_primary_key_field();
38 38
         $other_table_fk_field  = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name());
39 39
         $pk_table_alias        = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain,
40
-                $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias();
40
+                $this->get_this_model()->get_this_model_name()).$this_table_pk_field->get_table_alias();
41 41
         $fk_table_alias        = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain,
42
-                $this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias();
42
+                $this->get_other_model()->get_this_model_name()).$other_table_fk_field->get_table_alias();
43 43
         $fk_table              = $this->get_other_model()->get_table_for_alias($fk_table_alias);
44 44
         $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name();
45 45
 
@@ -48,7 +48,7 @@  discard block
 block discarded – undo
48 48
                 $other_table_fk_field->get_table_column(),
49 49
                 $pk_table_alias,
50 50
                 $this_table_pk_field->get_table_column(),
51
-                $fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'")
51
+                $fk_table_alias.'.'.$field_with_model_name->get_table_column()."='".$this->get_this_model()->get_this_model_name()."'")
52 52
                . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias);
53 53
     }
54 54
 
Please login to merge, or discard this patch.
core/db_models/relations/EE_Has_Many_Revision_Relation.php 2 patches
Indentation   +295 added lines, -295 removed lines patch added patch discarded remove patch
@@ -14,303 +14,303 @@
 block discarded – undo
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
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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());
Please login to merge, or discard this patch.