@@ -9,291 +9,291 @@ |
||
9 | 9 | */ |
10 | 10 | class EE_HABTM_Relation extends EE_Model_Relation_Base |
11 | 11 | { |
12 | - /** |
|
13 | - * Model which defines the relation between two other models. Eg, the EE_Event_Question_Group model, |
|
14 | - * which joins EE_Event and EE_Question_Group |
|
15 | - */ |
|
16 | - protected ?string $_joining_model_name = ''; |
|
12 | + /** |
|
13 | + * Model which defines the relation between two other models. Eg, the EE_Event_Question_Group model, |
|
14 | + * which joins EE_Event and EE_Question_Group |
|
15 | + */ |
|
16 | + protected ?string $_joining_model_name = ''; |
|
17 | 17 | |
18 | - protected ?string $_model_relation_chain_to_join_model = ''; |
|
18 | + protected ?string $_model_relation_chain_to_join_model = ''; |
|
19 | 19 | |
20 | 20 | |
21 | - /** |
|
22 | - * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
23 | - * (and an ee joining-model.) This knows how to join the models, |
|
24 | - * get related models across the relation, and add-and-remove the relationships. |
|
25 | - * |
|
26 | - * @param string $joining_model_name |
|
27 | - * @param bool $block_deletes for this type of relation, we block by default for now. if there |
|
28 | - * are related models across this relation, block (prevent and add an |
|
29 | - * error) the deletion of this model |
|
30 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
31 | - * default |
|
32 | - */ |
|
33 | - public function __construct( |
|
34 | - $joining_model_name, |
|
35 | - bool $block_deletes = true, |
|
36 | - string $blocking_delete_error_message = '' |
|
37 | - ) { |
|
38 | - $this->_joining_model_name = $joining_model_name; |
|
39 | - parent::__construct($block_deletes, $blocking_delete_error_message); |
|
40 | - } |
|
21 | + /** |
|
22 | + * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
23 | + * (and an ee joining-model.) This knows how to join the models, |
|
24 | + * get related models across the relation, and add-and-remove the relationships. |
|
25 | + * |
|
26 | + * @param string $joining_model_name |
|
27 | + * @param bool $block_deletes for this type of relation, we block by default for now. if there |
|
28 | + * are related models across this relation, block (prevent and add an |
|
29 | + * error) the deletion of this model |
|
30 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
31 | + * default |
|
32 | + */ |
|
33 | + public function __construct( |
|
34 | + $joining_model_name, |
|
35 | + bool $block_deletes = true, |
|
36 | + string $blocking_delete_error_message = '' |
|
37 | + ) { |
|
38 | + $this->_joining_model_name = $joining_model_name; |
|
39 | + parent::__construct($block_deletes, $blocking_delete_error_message); |
|
40 | + } |
|
41 | 41 | |
42 | 42 | |
43 | - /** |
|
44 | - * Gets the joining model's object |
|
45 | - * |
|
46 | - * @return EEM_Base |
|
47 | - * @throws Exception |
|
48 | - */ |
|
49 | - public function get_join_model(): EEM_Base |
|
50 | - { |
|
51 | - return $this->_get_model($this->_joining_model_name); |
|
52 | - } |
|
43 | + /** |
|
44 | + * Gets the joining model's object |
|
45 | + * |
|
46 | + * @return EEM_Base |
|
47 | + * @throws Exception |
|
48 | + */ |
|
49 | + public function get_join_model(): EEM_Base |
|
50 | + { |
|
51 | + return $this->_get_model($this->_joining_model_name); |
|
52 | + } |
|
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
57 | - * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
58 | - * |
|
59 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
60 | - * @return string of SQL |
|
61 | - * @throws EE_Error |
|
62 | - * @throws Exception |
|
63 | - */ |
|
64 | - public function get_join_to_intermediate_model_statement(string $model_relation_chain): string |
|
65 | - { |
|
66 | - // create sql like |
|
67 | - // LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
68 | - // LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
69 | - // remember the model relation chain to the JOIN model, because we'll |
|
70 | - // need it for get_join_statement() |
|
71 | - $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
72 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field( |
|
73 | - );// get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
74 | - $join_table_fk_field_to_this_table = |
|
75 | - $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
76 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
77 | - $model_relation_chain, |
|
78 | - $this->get_this_model()->get_this_model_name() |
|
79 | - ) . $this_table_pk_field->get_table_alias(); |
|
55 | + /** |
|
56 | + * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
57 | + * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
58 | + * |
|
59 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
60 | + * @return string of SQL |
|
61 | + * @throws EE_Error |
|
62 | + * @throws Exception |
|
63 | + */ |
|
64 | + public function get_join_to_intermediate_model_statement(string $model_relation_chain): string |
|
65 | + { |
|
66 | + // create sql like |
|
67 | + // LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
68 | + // LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
69 | + // remember the model relation chain to the JOIN model, because we'll |
|
70 | + // need it for get_join_statement() |
|
71 | + $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
72 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field( |
|
73 | + );// get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
74 | + $join_table_fk_field_to_this_table = |
|
75 | + $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
76 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
77 | + $model_relation_chain, |
|
78 | + $this->get_this_model()->get_this_model_name() |
|
79 | + ) . $this_table_pk_field->get_table_alias(); |
|
80 | 80 | |
81 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
82 | - $model_relation_chain, |
|
83 | - $this->get_join_model()->get_this_model_name() |
|
84 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
85 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
86 | - // phew! ok, we have all the info we need, now we can create the SQL join string |
|
87 | - return $this->_left_join( |
|
88 | - $join_table, |
|
89 | - $join_table_alias, |
|
90 | - $join_table_fk_field_to_this_table->get_table_column(), |
|
91 | - $this_table_alias, |
|
92 | - $this_table_pk_field->get_table_column() |
|
93 | - ) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
94 | - } |
|
81 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
82 | + $model_relation_chain, |
|
83 | + $this->get_join_model()->get_this_model_name() |
|
84 | + ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
85 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
86 | + // phew! ok, we have all the info we need, now we can create the SQL join string |
|
87 | + return $this->_left_join( |
|
88 | + $join_table, |
|
89 | + $join_table_alias, |
|
90 | + $join_table_fk_field_to_this_table->get_table_column(), |
|
91 | + $this_table_alias, |
|
92 | + $this_table_pk_field->get_table_column() |
|
93 | + ) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
94 | + } |
|
95 | 95 | |
96 | 96 | |
97 | - /** |
|
98 | - * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
99 | - * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
100 | - * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
101 | - * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
102 | - * the intermediate join table, and then to the other model's pk's table |
|
103 | - * |
|
104 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
105 | - * @return string of SQL |
|
106 | - * @throws EE_Error |
|
107 | - * @throws Exception |
|
108 | - */ |
|
109 | - public function get_join_statement(string $model_relation_chain): string |
|
110 | - { |
|
111 | - if (empty($this->_model_relation_chain_to_join_model)) { |
|
112 | - throw new EE_Error( |
|
113 | - esc_html__( |
|
114 | - 'When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
115 | - 'event_espresso' |
|
116 | - ) |
|
117 | - ); |
|
118 | - } |
|
119 | - $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to( |
|
120 | - $this->get_this_model()->get_this_model_name() |
|
121 | - ); |
|
122 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
123 | - $this->_model_relation_chain_to_join_model, |
|
124 | - $this->get_join_model()->get_this_model_name() |
|
125 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
126 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
127 | - $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to( |
|
128 | - $this->get_other_model()->get_this_model_name() |
|
129 | - ); |
|
130 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
131 | - $model_relation_chain, |
|
132 | - $this->get_other_model()->get_this_model_name() |
|
133 | - ) . $other_table_pk_field->get_table_alias(); |
|
134 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
97 | + /** |
|
98 | + * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
99 | + * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
100 | + * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
101 | + * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
102 | + * the intermediate join table, and then to the other model's pk's table |
|
103 | + * |
|
104 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
105 | + * @return string of SQL |
|
106 | + * @throws EE_Error |
|
107 | + * @throws Exception |
|
108 | + */ |
|
109 | + public function get_join_statement(string $model_relation_chain): string |
|
110 | + { |
|
111 | + if (empty($this->_model_relation_chain_to_join_model)) { |
|
112 | + throw new EE_Error( |
|
113 | + esc_html__( |
|
114 | + 'When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
115 | + 'event_espresso' |
|
116 | + ) |
|
117 | + ); |
|
118 | + } |
|
119 | + $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to( |
|
120 | + $this->get_this_model()->get_this_model_name() |
|
121 | + ); |
|
122 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
123 | + $this->_model_relation_chain_to_join_model, |
|
124 | + $this->get_join_model()->get_this_model_name() |
|
125 | + ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
126 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
127 | + $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to( |
|
128 | + $this->get_other_model()->get_this_model_name() |
|
129 | + ); |
|
130 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
131 | + $model_relation_chain, |
|
132 | + $this->get_other_model()->get_this_model_name() |
|
133 | + ) . $other_table_pk_field->get_table_alias(); |
|
134 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
135 | 135 | |
136 | - return $this->_left_join( |
|
137 | - $other_table, |
|
138 | - $other_table_alias, |
|
139 | - $other_table_pk_field->get_table_column(), |
|
140 | - $join_table_alias, |
|
141 | - $join_table_fk_field_to_other_table->get_table_column() |
|
142 | - ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
143 | - } |
|
136 | + return $this->_left_join( |
|
137 | + $other_table, |
|
138 | + $other_table_alias, |
|
139 | + $other_table_pk_field->get_table_column(), |
|
140 | + $join_table_alias, |
|
141 | + $join_table_fk_field_to_other_table->get_table_column() |
|
142 | + ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
143 | + } |
|
144 | 144 | |
145 | 145 | |
146 | - /** |
|
147 | - * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
148 | - * If the join table has additional columns (eg, the Event_Question_Group table has a is_primary column), then |
|
149 | - * you'll want to directly use the EEM_Event_Question_Group model to add the entry to the table and set those extra |
|
150 | - * columns' values |
|
151 | - * |
|
152 | - * @param EE_Base_Class|int $this_obj_or_id |
|
153 | - * @param EE_Base_Class|int $other_obj_or_id |
|
154 | - * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
155 | - * checking existing values and for setting new rows if |
|
156 | - * no exact matches. |
|
157 | - * @return EE_Base_Class |
|
158 | - * @throws EE_Error |
|
159 | - * @throws Exception |
|
160 | - */ |
|
161 | - public function add_relation_to( |
|
162 | - $this_obj_or_id, |
|
163 | - $other_obj_or_id, |
|
164 | - array $extra_join_model_fields_n_values = [] |
|
165 | - ): EE_Base_Class { |
|
166 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
167 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
168 | - // check if such a relationship already exists |
|
169 | - $join_model_fk_to_this_model = |
|
170 | - $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
171 | - $join_model_fk_to_other_model = |
|
172 | - $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
146 | + /** |
|
147 | + * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
148 | + * If the join table has additional columns (eg, the Event_Question_Group table has a is_primary column), then |
|
149 | + * you'll want to directly use the EEM_Event_Question_Group model to add the entry to the table and set those extra |
|
150 | + * columns' values |
|
151 | + * |
|
152 | + * @param EE_Base_Class|int $this_obj_or_id |
|
153 | + * @param EE_Base_Class|int $other_obj_or_id |
|
154 | + * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
155 | + * checking existing values and for setting new rows if |
|
156 | + * no exact matches. |
|
157 | + * @return EE_Base_Class |
|
158 | + * @throws EE_Error |
|
159 | + * @throws Exception |
|
160 | + */ |
|
161 | + public function add_relation_to( |
|
162 | + $this_obj_or_id, |
|
163 | + $other_obj_or_id, |
|
164 | + array $extra_join_model_fields_n_values = [] |
|
165 | + ): EE_Base_Class { |
|
166 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
167 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
168 | + // check if such a relationship already exists |
|
169 | + $join_model_fk_to_this_model = |
|
170 | + $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
171 | + $join_model_fk_to_other_model = |
|
172 | + $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
173 | 173 | |
174 | - $foreign_keys = $all_fields = [ |
|
175 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
176 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
177 | - ]; |
|
174 | + $foreign_keys = $all_fields = [ |
|
175 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
176 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
177 | + ]; |
|
178 | 178 | |
179 | - // if $where_query exists lets add them to the query_params. |
|
180 | - if (! empty($extra_join_model_fields_n_values)) { |
|
181 | - // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
182 | - // make sure we strip THIS models name from the query param |
|
183 | - $parsed_query = []; |
|
184 | - foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
185 | - $query_param = str_replace( |
|
186 | - $this->get_join_model()->get_this_model_name() . ".", |
|
187 | - "", |
|
188 | - $query_param |
|
189 | - ); |
|
190 | - $parsed_query[ $query_param ] = $val; |
|
191 | - } |
|
192 | - $all_fields = array_merge($foreign_keys, $parsed_query); |
|
193 | - } |
|
179 | + // if $where_query exists lets add them to the query_params. |
|
180 | + if (! empty($extra_join_model_fields_n_values)) { |
|
181 | + // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
182 | + // make sure we strip THIS models name from the query param |
|
183 | + $parsed_query = []; |
|
184 | + foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
185 | + $query_param = str_replace( |
|
186 | + $this->get_join_model()->get_this_model_name() . ".", |
|
187 | + "", |
|
188 | + $query_param |
|
189 | + ); |
|
190 | + $parsed_query[ $query_param ] = $val; |
|
191 | + } |
|
192 | + $all_fields = array_merge($foreign_keys, $parsed_query); |
|
193 | + } |
|
194 | 194 | |
195 | - $existing_entry_in_join_table = $this->get_join_model()->get_one([$foreign_keys]); |
|
196 | - // If there is already an entry in the join table, indicating a relationship, update it instead of adding a |
|
197 | - // new row. |
|
198 | - // Again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
199 | - // the other tables) use the joining model directly! |
|
200 | - if (! $existing_entry_in_join_table) { |
|
201 | - $this->get_join_model()->insert($all_fields); |
|
202 | - } else { |
|
203 | - $this->get_join_model()->update( |
|
204 | - $all_fields, |
|
205 | - [$foreign_keys] |
|
206 | - ); |
|
207 | - } |
|
208 | - return $other_model_obj; |
|
209 | - } |
|
195 | + $existing_entry_in_join_table = $this->get_join_model()->get_one([$foreign_keys]); |
|
196 | + // If there is already an entry in the join table, indicating a relationship, update it instead of adding a |
|
197 | + // new row. |
|
198 | + // Again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
199 | + // the other tables) use the joining model directly! |
|
200 | + if (! $existing_entry_in_join_table) { |
|
201 | + $this->get_join_model()->insert($all_fields); |
|
202 | + } else { |
|
203 | + $this->get_join_model()->update( |
|
204 | + $all_fields, |
|
205 | + [$foreign_keys] |
|
206 | + ); |
|
207 | + } |
|
208 | + return $other_model_obj; |
|
209 | + } |
|
210 | 210 | |
211 | 211 | |
212 | - /** |
|
213 | - * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
214 | - * |
|
215 | - * @param EE_Base_Class|int $this_obj_or_id |
|
216 | - * @param EE_Base_Class|int $other_obj_or_id |
|
217 | - * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
218 | - * values and for removing existing rows if exact matches exist. |
|
219 | - * @return EE_Base_Class |
|
220 | - * @throws EE_Error |
|
221 | - * @throws Exception |
|
222 | - */ |
|
223 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, array $where_query = []): EE_Base_Class |
|
224 | - { |
|
225 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
226 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
227 | - // check if such a relationship already exists |
|
228 | - $join_model_fk_to_this_model = |
|
229 | - $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
230 | - $join_model_fk_to_other_model = |
|
231 | - $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
212 | + /** |
|
213 | + * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
214 | + * |
|
215 | + * @param EE_Base_Class|int $this_obj_or_id |
|
216 | + * @param EE_Base_Class|int $other_obj_or_id |
|
217 | + * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
218 | + * values and for removing existing rows if exact matches exist. |
|
219 | + * @return EE_Base_Class |
|
220 | + * @throws EE_Error |
|
221 | + * @throws Exception |
|
222 | + */ |
|
223 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, array $where_query = []): EE_Base_Class |
|
224 | + { |
|
225 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
226 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
227 | + // check if such a relationship already exists |
|
228 | + $join_model_fk_to_this_model = |
|
229 | + $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
230 | + $join_model_fk_to_other_model = |
|
231 | + $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
232 | 232 | |
233 | - $cols_n_values = [ |
|
234 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
235 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
236 | - ]; |
|
233 | + $cols_n_values = [ |
|
234 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
235 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
236 | + ]; |
|
237 | 237 | |
238 | - // if $where_query exists lets add them to the query_params. |
|
239 | - if (! empty($where_query)) { |
|
240 | - // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
241 | - // make sure we strip THIS models name from the query param |
|
242 | - $parsed_query = []; |
|
243 | - foreach ($where_query as $query_param => $val) { |
|
244 | - $query_param = str_replace( |
|
245 | - $this->get_join_model()->get_this_model_name() . ".", |
|
246 | - "", |
|
247 | - $query_param |
|
248 | - ); |
|
249 | - $parsed_query[ $query_param ] = $val; |
|
250 | - } |
|
251 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
252 | - } |
|
238 | + // if $where_query exists lets add them to the query_params. |
|
239 | + if (! empty($where_query)) { |
|
240 | + // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
241 | + // make sure we strip THIS models name from the query param |
|
242 | + $parsed_query = []; |
|
243 | + foreach ($where_query as $query_param => $val) { |
|
244 | + $query_param = str_replace( |
|
245 | + $this->get_join_model()->get_this_model_name() . ".", |
|
246 | + "", |
|
247 | + $query_param |
|
248 | + ); |
|
249 | + $parsed_query[ $query_param ] = $val; |
|
250 | + } |
|
251 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
252 | + } |
|
253 | 253 | |
254 | - $this->get_join_model()->delete([$cols_n_values]); |
|
255 | - return $other_model_obj; |
|
256 | - } |
|
254 | + $this->get_join_model()->delete([$cols_n_values]); |
|
255 | + return $other_model_obj; |
|
256 | + } |
|
257 | 257 | |
258 | 258 | |
259 | - /** |
|
260 | - * Gets all the non-key fields (ie, not the primary key and not foreign keys) on the join model. |
|
261 | - * |
|
262 | - * @return EE_Model_Field_Base[] |
|
263 | - * @throws EE_Error |
|
264 | - * @throws Exception |
|
265 | - * @since 4.9.76.p |
|
266 | - */ |
|
267 | - public function getNonKeyFields(): array |
|
268 | - { |
|
269 | - // all fields besides the primary key and two foreign keys should be parameters |
|
270 | - $join_model = $this->get_join_model(); |
|
271 | - $standard_fields = []; |
|
272 | - if ($join_model->has_primary_key_field()) { |
|
273 | - $standard_fields[] = $join_model->primary_key_name(); |
|
274 | - } |
|
275 | - if ($this->get_this_model()->has_primary_key_field()) { |
|
276 | - $standard_fields[] = $this->get_this_model()->primary_key_name(); |
|
277 | - } |
|
278 | - if ($this->get_other_model()->has_primary_key_field()) { |
|
279 | - $standard_fields[] = $this->get_other_model()->primary_key_name(); |
|
280 | - } |
|
281 | - return array_diff_key( |
|
282 | - $join_model->field_settings(), |
|
283 | - array_flip($standard_fields) |
|
284 | - ); |
|
285 | - } |
|
259 | + /** |
|
260 | + * Gets all the non-key fields (ie, not the primary key and not foreign keys) on the join model. |
|
261 | + * |
|
262 | + * @return EE_Model_Field_Base[] |
|
263 | + * @throws EE_Error |
|
264 | + * @throws Exception |
|
265 | + * @since 4.9.76.p |
|
266 | + */ |
|
267 | + public function getNonKeyFields(): array |
|
268 | + { |
|
269 | + // all fields besides the primary key and two foreign keys should be parameters |
|
270 | + $join_model = $this->get_join_model(); |
|
271 | + $standard_fields = []; |
|
272 | + if ($join_model->has_primary_key_field()) { |
|
273 | + $standard_fields[] = $join_model->primary_key_name(); |
|
274 | + } |
|
275 | + if ($this->get_this_model()->has_primary_key_field()) { |
|
276 | + $standard_fields[] = $this->get_this_model()->primary_key_name(); |
|
277 | + } |
|
278 | + if ($this->get_other_model()->has_primary_key_field()) { |
|
279 | + $standard_fields[] = $this->get_other_model()->primary_key_name(); |
|
280 | + } |
|
281 | + return array_diff_key( |
|
282 | + $join_model->field_settings(), |
|
283 | + array_flip($standard_fields) |
|
284 | + ); |
|
285 | + } |
|
286 | 286 | |
287 | 287 | |
288 | - /** |
|
289 | - * Returns true if the join model has non-key fields (ie, fields that aren't the primary key or foreign keys.) |
|
290 | - * |
|
291 | - * @return boolean |
|
292 | - * @throws Exception |
|
293 | - * @since 4.9.76.p |
|
294 | - */ |
|
295 | - public function hasNonKeyFields(): bool |
|
296 | - { |
|
297 | - return count($this->get_join_model()->field_settings()) > 3; |
|
298 | - } |
|
288 | + /** |
|
289 | + * Returns true if the join model has non-key fields (ie, fields that aren't the primary key or foreign keys.) |
|
290 | + * |
|
291 | + * @return boolean |
|
292 | + * @throws Exception |
|
293 | + * @since 4.9.76.p |
|
294 | + */ |
|
295 | + public function hasNonKeyFields(): bool |
|
296 | + { |
|
297 | + return count($this->get_join_model()->field_settings()) > 3; |
|
298 | + } |
|
299 | 299 | } |
@@ -70,19 +70,19 @@ discard block |
||
70 | 70 | // need it for get_join_statement() |
71 | 71 | $this->_model_relation_chain_to_join_model = $model_relation_chain; |
72 | 72 | $this_table_pk_field = $this->get_this_model()->get_primary_key_field( |
73 | - );// get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
73 | + ); // get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
74 | 74 | $join_table_fk_field_to_this_table = |
75 | 75 | $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
76 | 76 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
77 | 77 | $model_relation_chain, |
78 | 78 | $this->get_this_model()->get_this_model_name() |
79 | - ) . $this_table_pk_field->get_table_alias(); |
|
79 | + ).$this_table_pk_field->get_table_alias(); |
|
80 | 80 | |
81 | 81 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
82 | 82 | $model_relation_chain, |
83 | 83 | $this->get_join_model()->get_this_model_name() |
84 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
85 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
84 | + ).$join_table_fk_field_to_this_table->get_table_alias(); |
|
85 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
86 | 86 | // phew! ok, we have all the info we need, now we can create the SQL join string |
87 | 87 | return $this->_left_join( |
88 | 88 | $join_table, |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | $join_table_fk_field_to_this_table->get_table_column(), |
91 | 91 | $this_table_alias, |
92 | 92 | $this_table_pk_field->get_table_column() |
93 | - ) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
93 | + ).$this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
123 | 123 | $this->_model_relation_chain_to_join_model, |
124 | 124 | $this->get_join_model()->get_this_model_name() |
125 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
125 | + ).$join_table_fk_field_to_this_table->get_table_alias(); |
|
126 | 126 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
127 | 127 | $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to( |
128 | 128 | $this->get_other_model()->get_this_model_name() |
@@ -130,8 +130,8 @@ discard block |
||
130 | 130 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
131 | 131 | $model_relation_chain, |
132 | 132 | $this->get_other_model()->get_this_model_name() |
133 | - ) . $other_table_pk_field->get_table_alias(); |
|
134 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
133 | + ).$other_table_pk_field->get_table_alias(); |
|
134 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
135 | 135 | |
136 | 136 | return $this->_left_join( |
137 | 137 | $other_table, |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | $other_table_pk_field->get_table_column(), |
140 | 140 | $join_table_alias, |
141 | 141 | $join_table_fk_field_to_other_table->get_table_column() |
142 | - ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
142 | + ).$this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
143 | 143 | } |
144 | 144 | |
145 | 145 | |
@@ -177,17 +177,17 @@ discard block |
||
177 | 177 | ]; |
178 | 178 | |
179 | 179 | // if $where_query exists lets add them to the query_params. |
180 | - if (! empty($extra_join_model_fields_n_values)) { |
|
180 | + if ( ! empty($extra_join_model_fields_n_values)) { |
|
181 | 181 | // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
182 | 182 | // make sure we strip THIS models name from the query param |
183 | 183 | $parsed_query = []; |
184 | 184 | foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
185 | - $query_param = str_replace( |
|
186 | - $this->get_join_model()->get_this_model_name() . ".", |
|
185 | + $query_param = str_replace( |
|
186 | + $this->get_join_model()->get_this_model_name().".", |
|
187 | 187 | "", |
188 | 188 | $query_param |
189 | 189 | ); |
190 | - $parsed_query[ $query_param ] = $val; |
|
190 | + $parsed_query[$query_param] = $val; |
|
191 | 191 | } |
192 | 192 | $all_fields = array_merge($foreign_keys, $parsed_query); |
193 | 193 | } |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | // new row. |
198 | 198 | // Again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
199 | 199 | // the other tables) use the joining model directly! |
200 | - if (! $existing_entry_in_join_table) { |
|
200 | + if ( ! $existing_entry_in_join_table) { |
|
201 | 201 | $this->get_join_model()->insert($all_fields); |
202 | 202 | } else { |
203 | 203 | $this->get_join_model()->update( |
@@ -236,17 +236,17 @@ discard block |
||
236 | 236 | ]; |
237 | 237 | |
238 | 238 | // if $where_query exists lets add them to the query_params. |
239 | - if (! empty($where_query)) { |
|
239 | + if ( ! empty($where_query)) { |
|
240 | 240 | // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
241 | 241 | // make sure we strip THIS models name from the query param |
242 | 242 | $parsed_query = []; |
243 | 243 | foreach ($where_query as $query_param => $val) { |
244 | - $query_param = str_replace( |
|
245 | - $this->get_join_model()->get_this_model_name() . ".", |
|
244 | + $query_param = str_replace( |
|
245 | + $this->get_join_model()->get_this_model_name().".", |
|
246 | 246 | "", |
247 | 247 | $query_param |
248 | 248 | ); |
249 | - $parsed_query[ $query_param ] = $val; |
|
249 | + $parsed_query[$query_param] = $val; |
|
250 | 250 | } |
251 | 251 | $cols_n_values = array_merge($cols_n_values, $parsed_query); |
252 | 252 | } |
@@ -10,291 +10,291 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_HABTM_Any_Relation extends EE_HABTM_Relation |
12 | 12 | { |
13 | - protected string $_alphabetically_first_model_name = ''; |
|
13 | + protected string $_alphabetically_first_model_name = ''; |
|
14 | 14 | |
15 | 15 | |
16 | - /** |
|
17 | - * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
18 | - * (and an ee joining-model.) This knows how to join the models, |
|
19 | - * get related models across the relation, and add-and-remove the relationships. |
|
20 | - * |
|
21 | - * @param bool $block_deletes for this type of relation, we block by default for now. if there |
|
22 | - * are related models across this relation, block (prevent and add an |
|
23 | - * error) the deletion of this model |
|
24 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
25 | - * default |
|
26 | - */ |
|
27 | - public function __construct(bool $block_deletes = true, string $blocking_delete_error_message = '') |
|
28 | - { |
|
29 | - parent::__construct('Extra_Join', $block_deletes, $blocking_delete_error_message); |
|
30 | - } |
|
16 | + /** |
|
17 | + * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
18 | + * (and an ee joining-model.) This knows how to join the models, |
|
19 | + * get related models across the relation, and add-and-remove the relationships. |
|
20 | + * |
|
21 | + * @param bool $block_deletes for this type of relation, we block by default for now. if there |
|
22 | + * are related models across this relation, block (prevent and add an |
|
23 | + * error) the deletion of this model |
|
24 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
25 | + * default |
|
26 | + */ |
|
27 | + public function __construct(bool $block_deletes = true, string $blocking_delete_error_message = '') |
|
28 | + { |
|
29 | + parent::__construct('Extra_Join', $block_deletes, $blocking_delete_error_message); |
|
30 | + } |
|
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * @param $this_model_name |
|
35 | - * @param $other_model_name |
|
36 | - */ |
|
37 | - public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
38 | - { |
|
39 | - $this->_alphabetically_first_model_name = min($this_model_name, $other_model_name); |
|
40 | - parent::_construct_finalize_set_models($this_model_name, $other_model_name); |
|
41 | - } |
|
33 | + /** |
|
34 | + * @param $this_model_name |
|
35 | + * @param $other_model_name |
|
36 | + */ |
|
37 | + public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
38 | + { |
|
39 | + $this->_alphabetically_first_model_name = min($this_model_name, $other_model_name); |
|
40 | + parent::_construct_finalize_set_models($this_model_name, $other_model_name); |
|
41 | + } |
|
42 | 42 | |
43 | 43 | |
44 | - /** |
|
45 | - * @param string $model_name |
|
46 | - * @param string $id_or_name_field should be the string 'ID' or 'name' only |
|
47 | - * @return EE_Model_Field_Base |
|
48 | - * @throws EE_Error |
|
49 | - * @throws Exception |
|
50 | - */ |
|
51 | - public function get_join_table_fk_field_to(string $model_name, string $id_or_name_field): EE_Model_Field_Base |
|
52 | - { |
|
53 | - $order = $model_name === $this->_alphabetically_first_model_name ? 'first' : 'second'; |
|
54 | - return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
55 | - } |
|
44 | + /** |
|
45 | + * @param string $model_name |
|
46 | + * @param string $id_or_name_field should be the string 'ID' or 'name' only |
|
47 | + * @return EE_Model_Field_Base |
|
48 | + * @throws EE_Error |
|
49 | + * @throws Exception |
|
50 | + */ |
|
51 | + public function get_join_table_fk_field_to(string $model_name, string $id_or_name_field): EE_Model_Field_Base |
|
52 | + { |
|
53 | + $order = $model_name === $this->_alphabetically_first_model_name ? 'first' : 'second'; |
|
54 | + return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
55 | + } |
|
56 | 56 | |
57 | 57 | |
58 | - /** |
|
59 | - * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
60 | - * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
61 | - * |
|
62 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
63 | - * @return string of SQL |
|
64 | - * @throws EE_Error |
|
65 | - * @throws Exception |
|
66 | - */ |
|
67 | - public function get_join_to_intermediate_model_statement(string $model_relation_chain): string |
|
68 | - { |
|
69 | - // create sql like |
|
70 | - // LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
71 | - // LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
72 | - // remember the model relation chain to the JOIN model, because we'll |
|
73 | - // need it for get_join_statement() |
|
74 | - $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
75 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
76 | - $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
77 | - $this->get_this_model()->get_this_model_name(), |
|
78 | - 'ID' |
|
79 | - ); |
|
80 | - $field_with_model_name = $this->get_join_table_fk_field_to( |
|
81 | - $this->get_this_model()->get_this_model_name(), |
|
82 | - 'name' |
|
83 | - ); |
|
84 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
85 | - $model_relation_chain, |
|
86 | - $this->get_this_model()->get_this_model_name() |
|
87 | - ) . $this_table_pk_field->get_table_alias(); |
|
88 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
89 | - $model_relation_chain, |
|
90 | - $this->get_join_model()->get_this_model_name() |
|
91 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
92 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
93 | - // phew! ok, we have all the info we need, now we can create the SQL join string |
|
94 | - return $this->_left_join( |
|
95 | - $join_table, |
|
96 | - $join_table_alias, |
|
97 | - $join_table_fk_field_to_this_table->get_table_column(), |
|
98 | - $this_table_alias, |
|
99 | - $this_table_pk_field->get_table_column(), |
|
100 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name( |
|
101 | - ) . "'" |
|
102 | - ) . |
|
103 | - $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
104 | - } |
|
58 | + /** |
|
59 | + * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
60 | + * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
61 | + * |
|
62 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
63 | + * @return string of SQL |
|
64 | + * @throws EE_Error |
|
65 | + * @throws Exception |
|
66 | + */ |
|
67 | + public function get_join_to_intermediate_model_statement(string $model_relation_chain): string |
|
68 | + { |
|
69 | + // create sql like |
|
70 | + // LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
71 | + // LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
72 | + // remember the model relation chain to the JOIN model, because we'll |
|
73 | + // need it for get_join_statement() |
|
74 | + $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
75 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
76 | + $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
77 | + $this->get_this_model()->get_this_model_name(), |
|
78 | + 'ID' |
|
79 | + ); |
|
80 | + $field_with_model_name = $this->get_join_table_fk_field_to( |
|
81 | + $this->get_this_model()->get_this_model_name(), |
|
82 | + 'name' |
|
83 | + ); |
|
84 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
85 | + $model_relation_chain, |
|
86 | + $this->get_this_model()->get_this_model_name() |
|
87 | + ) . $this_table_pk_field->get_table_alias(); |
|
88 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
89 | + $model_relation_chain, |
|
90 | + $this->get_join_model()->get_this_model_name() |
|
91 | + ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
92 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
93 | + // phew! ok, we have all the info we need, now we can create the SQL join string |
|
94 | + return $this->_left_join( |
|
95 | + $join_table, |
|
96 | + $join_table_alias, |
|
97 | + $join_table_fk_field_to_this_table->get_table_column(), |
|
98 | + $this_table_alias, |
|
99 | + $this_table_pk_field->get_table_column(), |
|
100 | + $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name( |
|
101 | + ) . "'" |
|
102 | + ) . |
|
103 | + $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
104 | + } |
|
105 | 105 | |
106 | 106 | |
107 | - /** |
|
108 | - * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
109 | - * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
110 | - * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
111 | - * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
112 | - * the intermediate join table, and then to the other model's pk's table |
|
113 | - * |
|
114 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
115 | - * @return string of SQL |
|
116 | - * @throws EE_Error |
|
117 | - * @throws Exception |
|
118 | - */ |
|
119 | - public function get_join_statement(string $model_relation_chain): string |
|
120 | - { |
|
121 | - if (empty($this->_model_relation_chain_to_join_model)) { |
|
122 | - throw new EE_Error( |
|
123 | - esc_html__( |
|
124 | - 'When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
125 | - 'event_espresso' |
|
126 | - ) |
|
127 | - ); |
|
128 | - } |
|
129 | - $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
130 | - $this->get_this_model()->get_this_model_name(), |
|
131 | - 'ID' |
|
132 | - ); |
|
133 | - $join_table_fk_field_to_other_table = $this->get_join_table_fk_field_to( |
|
134 | - $this->get_other_model()->get_this_model_name(), |
|
135 | - 'ID' |
|
136 | - ); |
|
137 | - $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
138 | - $this->get_other_model()->get_this_model_name(), |
|
139 | - 'name' |
|
140 | - ); |
|
107 | + /** |
|
108 | + * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
109 | + * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
110 | + * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
111 | + * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
112 | + * the intermediate join table, and then to the other model's pk's table |
|
113 | + * |
|
114 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
115 | + * @return string of SQL |
|
116 | + * @throws EE_Error |
|
117 | + * @throws Exception |
|
118 | + */ |
|
119 | + public function get_join_statement(string $model_relation_chain): string |
|
120 | + { |
|
121 | + if (empty($this->_model_relation_chain_to_join_model)) { |
|
122 | + throw new EE_Error( |
|
123 | + esc_html__( |
|
124 | + 'When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
125 | + 'event_espresso' |
|
126 | + ) |
|
127 | + ); |
|
128 | + } |
|
129 | + $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
130 | + $this->get_this_model()->get_this_model_name(), |
|
131 | + 'ID' |
|
132 | + ); |
|
133 | + $join_table_fk_field_to_other_table = $this->get_join_table_fk_field_to( |
|
134 | + $this->get_other_model()->get_this_model_name(), |
|
135 | + 'ID' |
|
136 | + ); |
|
137 | + $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
138 | + $this->get_other_model()->get_this_model_name(), |
|
139 | + 'name' |
|
140 | + ); |
|
141 | 141 | |
142 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
143 | - $this->_model_relation_chain_to_join_model, |
|
144 | - $this->get_join_model()->get_this_model_name() |
|
145 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
142 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
143 | + $this->_model_relation_chain_to_join_model, |
|
144 | + $this->get_join_model()->get_this_model_name() |
|
145 | + ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
146 | 146 | |
147 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
148 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
149 | - $model_relation_chain, |
|
150 | - $this->get_other_model()->get_this_model_name() |
|
151 | - ) . $other_table_pk_field->get_table_alias(); |
|
152 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
147 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
148 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
149 | + $model_relation_chain, |
|
150 | + $this->get_other_model()->get_this_model_name() |
|
151 | + ) . $other_table_pk_field->get_table_alias(); |
|
152 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
153 | 153 | |
154 | - return $this->_left_join( |
|
155 | - $other_table, |
|
156 | - $other_table_alias, |
|
157 | - $other_table_pk_field->get_table_column(), |
|
158 | - $join_table_alias, |
|
159 | - $join_table_fk_field_to_other_table->get_table_column(), |
|
160 | - $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model() |
|
161 | - ->get_this_model_name() . "'" |
|
162 | - ) . |
|
163 | - $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
164 | - } |
|
154 | + return $this->_left_join( |
|
155 | + $other_table, |
|
156 | + $other_table_alias, |
|
157 | + $other_table_pk_field->get_table_column(), |
|
158 | + $join_table_alias, |
|
159 | + $join_table_fk_field_to_other_table->get_table_column(), |
|
160 | + $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model() |
|
161 | + ->get_this_model_name() . "'" |
|
162 | + ) . |
|
163 | + $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
164 | + } |
|
165 | 165 | |
166 | 166 | |
167 | - /** |
|
168 | - * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
169 | - * |
|
170 | - * @param EE_Base_Class|int $this_obj_or_id |
|
171 | - * @param EE_Base_Class|int $other_obj_or_id |
|
172 | - * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
173 | - * checking existing values and for setting new rows if |
|
174 | - * no exact matches. |
|
175 | - * @return EE_Base_Class |
|
176 | - * @throws EE_Error |
|
177 | - * @throws Exception |
|
178 | - */ |
|
179 | - public function add_relation_to( |
|
180 | - $this_obj_or_id, |
|
181 | - $other_obj_or_id, |
|
182 | - array $extra_join_model_fields_n_values = [] |
|
183 | - ): EE_Base_Class { |
|
184 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
185 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
186 | - // check if such a relationship already exists |
|
187 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
188 | - $this->get_this_model()->get_this_model_name(), |
|
189 | - 'ID' |
|
190 | - ); |
|
191 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
192 | - $this->get_this_model()->get_this_model_name(), |
|
193 | - 'name' |
|
194 | - ); |
|
195 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
196 | - $this->get_other_model()->get_this_model_name(), |
|
197 | - 'ID' |
|
198 | - ); |
|
199 | - $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
200 | - $this->get_other_model()->get_this_model_name(), |
|
201 | - 'name' |
|
202 | - ); |
|
167 | + /** |
|
168 | + * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
169 | + * |
|
170 | + * @param EE_Base_Class|int $this_obj_or_id |
|
171 | + * @param EE_Base_Class|int $other_obj_or_id |
|
172 | + * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
173 | + * checking existing values and for setting new rows if |
|
174 | + * no exact matches. |
|
175 | + * @return EE_Base_Class |
|
176 | + * @throws EE_Error |
|
177 | + * @throws Exception |
|
178 | + */ |
|
179 | + public function add_relation_to( |
|
180 | + $this_obj_or_id, |
|
181 | + $other_obj_or_id, |
|
182 | + array $extra_join_model_fields_n_values = [] |
|
183 | + ): EE_Base_Class { |
|
184 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
185 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
186 | + // check if such a relationship already exists |
|
187 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
188 | + $this->get_this_model()->get_this_model_name(), |
|
189 | + 'ID' |
|
190 | + ); |
|
191 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
192 | + $this->get_this_model()->get_this_model_name(), |
|
193 | + 'name' |
|
194 | + ); |
|
195 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
196 | + $this->get_other_model()->get_this_model_name(), |
|
197 | + 'ID' |
|
198 | + ); |
|
199 | + $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
200 | + $this->get_other_model()->get_this_model_name(), |
|
201 | + 'name' |
|
202 | + ); |
|
203 | 203 | |
204 | - $cols_n_values = [ |
|
205 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
206 | - $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
207 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
208 | - $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
209 | - ]; |
|
204 | + $cols_n_values = [ |
|
205 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
206 | + $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
207 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
208 | + $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
209 | + ]; |
|
210 | 210 | |
211 | - // if $where_query exists lets add them to the query_params. |
|
212 | - if (! empty($extra_join_model_fields_n_values)) { |
|
213 | - // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
214 | - // make sure we strip THIS models name from the query param |
|
215 | - $parsed_query = []; |
|
216 | - foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
217 | - $query_param = str_replace( |
|
218 | - $this->get_join_model()->get_this_model_name() . ".", |
|
219 | - "", |
|
220 | - $query_param |
|
221 | - ); |
|
222 | - $parsed_query[ $query_param ] = $val; |
|
223 | - } |
|
224 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
225 | - } |
|
211 | + // if $where_query exists lets add them to the query_params. |
|
212 | + if (! empty($extra_join_model_fields_n_values)) { |
|
213 | + // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
214 | + // make sure we strip THIS models name from the query param |
|
215 | + $parsed_query = []; |
|
216 | + foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
217 | + $query_param = str_replace( |
|
218 | + $this->get_join_model()->get_this_model_name() . ".", |
|
219 | + "", |
|
220 | + $query_param |
|
221 | + ); |
|
222 | + $parsed_query[ $query_param ] = $val; |
|
223 | + } |
|
224 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
225 | + } |
|
226 | 226 | |
227 | - $query_params = [$cols_n_values]; |
|
227 | + $query_params = [$cols_n_values]; |
|
228 | 228 | |
229 | 229 | |
230 | - $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
231 | - // if there is already an entry in the join table, indicating a relationship, we're done |
|
232 | - // again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
233 | - // the other tables, use the joining model directly! |
|
234 | - if (! $existing_entry_in_join_table) { |
|
235 | - $this->get_join_model()->insert($cols_n_values); |
|
236 | - } |
|
237 | - return $other_model_obj; |
|
238 | - } |
|
230 | + $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
231 | + // if there is already an entry in the join table, indicating a relationship, we're done |
|
232 | + // again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
233 | + // the other tables, use the joining model directly! |
|
234 | + if (! $existing_entry_in_join_table) { |
|
235 | + $this->get_join_model()->insert($cols_n_values); |
|
236 | + } |
|
237 | + return $other_model_obj; |
|
238 | + } |
|
239 | 239 | |
240 | 240 | |
241 | - /** |
|
242 | - * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
243 | - * |
|
244 | - * @param EE_Base_Class|int $this_obj_or_id |
|
245 | - * @param EE_Base_Class|int $other_obj_or_id |
|
246 | - * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
247 | - * values and for removing existing rows if exact matches exist. |
|
248 | - * @return EE_Base_Class |
|
249 | - * @throws EE_Error |
|
250 | - * @throws Exception |
|
251 | - */ |
|
252 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, array $where_query = []): EE_Base_Class |
|
253 | - { |
|
254 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
255 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
256 | - // check if such a relationship already exists |
|
257 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
258 | - $this->get_this_model()->get_this_model_name(), |
|
259 | - 'ID' |
|
260 | - ); |
|
261 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
262 | - $this->get_this_model()->get_this_model_name(), |
|
263 | - 'name' |
|
264 | - ); |
|
265 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
266 | - $this->get_other_model()->get_this_model_name(), |
|
267 | - 'ID' |
|
268 | - ); |
|
269 | - $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
270 | - $this->get_other_model()->get_this_model_name(), |
|
271 | - 'name' |
|
272 | - ); |
|
241 | + /** |
|
242 | + * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
243 | + * |
|
244 | + * @param EE_Base_Class|int $this_obj_or_id |
|
245 | + * @param EE_Base_Class|int $other_obj_or_id |
|
246 | + * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
247 | + * values and for removing existing rows if exact matches exist. |
|
248 | + * @return EE_Base_Class |
|
249 | + * @throws EE_Error |
|
250 | + * @throws Exception |
|
251 | + */ |
|
252 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, array $where_query = []): EE_Base_Class |
|
253 | + { |
|
254 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
255 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
256 | + // check if such a relationship already exists |
|
257 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
258 | + $this->get_this_model()->get_this_model_name(), |
|
259 | + 'ID' |
|
260 | + ); |
|
261 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
262 | + $this->get_this_model()->get_this_model_name(), |
|
263 | + 'name' |
|
264 | + ); |
|
265 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
266 | + $this->get_other_model()->get_this_model_name(), |
|
267 | + 'ID' |
|
268 | + ); |
|
269 | + $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
270 | + $this->get_other_model()->get_this_model_name(), |
|
271 | + 'name' |
|
272 | + ); |
|
273 | 273 | |
274 | - $cols_n_values = [ |
|
275 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
276 | - $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
277 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
278 | - $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
279 | - ]; |
|
274 | + $cols_n_values = [ |
|
275 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
276 | + $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
277 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
278 | + $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
279 | + ]; |
|
280 | 280 | |
281 | - // if $where_query exists lets add them to the query_params. |
|
282 | - if (! empty($where_query)) { |
|
283 | - // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
284 | - // make sure we strip THIS models name from the query param |
|
285 | - $parsed_query = []; |
|
286 | - foreach ($where_query as $query_param => $val) { |
|
287 | - $query_param = str_replace( |
|
288 | - $this->get_join_model()->get_this_model_name() . ".", |
|
289 | - "", |
|
290 | - $query_param |
|
291 | - ); |
|
292 | - $parsed_query[ $query_param ] = $val; |
|
293 | - } |
|
294 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
295 | - } |
|
281 | + // if $where_query exists lets add them to the query_params. |
|
282 | + if (! empty($where_query)) { |
|
283 | + // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
284 | + // make sure we strip THIS models name from the query param |
|
285 | + $parsed_query = []; |
|
286 | + foreach ($where_query as $query_param => $val) { |
|
287 | + $query_param = str_replace( |
|
288 | + $this->get_join_model()->get_this_model_name() . ".", |
|
289 | + "", |
|
290 | + $query_param |
|
291 | + ); |
|
292 | + $parsed_query[ $query_param ] = $val; |
|
293 | + } |
|
294 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
295 | + } |
|
296 | 296 | |
297 | - $this->get_join_model()->delete([$cols_n_values]); |
|
298 | - return $other_model_obj; |
|
299 | - } |
|
297 | + $this->get_join_model()->delete([$cols_n_values]); |
|
298 | + return $other_model_obj; |
|
299 | + } |
|
300 | 300 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | public function get_join_table_fk_field_to(string $model_name, string $id_or_name_field): EE_Model_Field_Base |
52 | 52 | { |
53 | 53 | $order = $model_name === $this->_alphabetically_first_model_name ? 'first' : 'second'; |
54 | - return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
54 | + return $this->get_join_model()->field_settings_for('EXJ_'.$order.'_model_'.$id_or_name_field); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | |
@@ -77,19 +77,19 @@ discard block |
||
77 | 77 | $this->get_this_model()->get_this_model_name(), |
78 | 78 | 'ID' |
79 | 79 | ); |
80 | - $field_with_model_name = $this->get_join_table_fk_field_to( |
|
80 | + $field_with_model_name = $this->get_join_table_fk_field_to( |
|
81 | 81 | $this->get_this_model()->get_this_model_name(), |
82 | 82 | 'name' |
83 | 83 | ); |
84 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
84 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
85 | 85 | $model_relation_chain, |
86 | 86 | $this->get_this_model()->get_this_model_name() |
87 | - ) . $this_table_pk_field->get_table_alias(); |
|
88 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
87 | + ).$this_table_pk_field->get_table_alias(); |
|
88 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
89 | 89 | $model_relation_chain, |
90 | 90 | $this->get_join_model()->get_this_model_name() |
91 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
92 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
91 | + ).$join_table_fk_field_to_this_table->get_table_alias(); |
|
92 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
93 | 93 | // phew! ok, we have all the info we need, now we can create the SQL join string |
94 | 94 | return $this->_left_join( |
95 | 95 | $join_table, |
@@ -97,9 +97,9 @@ discard block |
||
97 | 97 | $join_table_fk_field_to_this_table->get_table_column(), |
98 | 98 | $this_table_alias, |
99 | 99 | $this_table_pk_field->get_table_column(), |
100 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name( |
|
101 | - ) . "'" |
|
102 | - ) . |
|
100 | + $field_with_model_name->get_qualified_column()."='".$this->get_this_model()->get_this_model_name( |
|
101 | + )."'" |
|
102 | + ). |
|
103 | 103 | $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
104 | 104 | } |
105 | 105 | |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | ) |
127 | 127 | ); |
128 | 128 | } |
129 | - $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
129 | + $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
130 | 130 | $this->get_this_model()->get_this_model_name(), |
131 | 131 | 'ID' |
132 | 132 | ); |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | $this->get_other_model()->get_this_model_name(), |
135 | 135 | 'ID' |
136 | 136 | ); |
137 | - $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
137 | + $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
138 | 138 | $this->get_other_model()->get_this_model_name(), |
139 | 139 | 'name' |
140 | 140 | ); |
@@ -142,14 +142,14 @@ discard block |
||
142 | 142 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
143 | 143 | $this->_model_relation_chain_to_join_model, |
144 | 144 | $this->get_join_model()->get_this_model_name() |
145 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
145 | + ).$join_table_fk_field_to_this_table->get_table_alias(); |
|
146 | 146 | |
147 | 147 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
148 | 148 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
149 | 149 | $model_relation_chain, |
150 | 150 | $this->get_other_model()->get_this_model_name() |
151 | - ) . $other_table_pk_field->get_table_alias(); |
|
152 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
151 | + ).$other_table_pk_field->get_table_alias(); |
|
152 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
153 | 153 | |
154 | 154 | return $this->_left_join( |
155 | 155 | $other_table, |
@@ -157,9 +157,9 @@ discard block |
||
157 | 157 | $other_table_pk_field->get_table_column(), |
158 | 158 | $join_table_alias, |
159 | 159 | $join_table_fk_field_to_other_table->get_table_column(), |
160 | - $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model() |
|
161 | - ->get_this_model_name() . "'" |
|
162 | - ) . |
|
160 | + $field_with_other_model_name->get_qualified_column()."='".$this->get_other_model() |
|
161 | + ->get_this_model_name()."'" |
|
162 | + ). |
|
163 | 163 | $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
164 | 164 | } |
165 | 165 | |
@@ -184,15 +184,15 @@ discard block |
||
184 | 184 | $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
185 | 185 | $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
186 | 186 | // check if such a relationship already exists |
187 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
187 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
188 | 188 | $this->get_this_model()->get_this_model_name(), |
189 | 189 | 'ID' |
190 | 190 | ); |
191 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
191 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
192 | 192 | $this->get_this_model()->get_this_model_name(), |
193 | 193 | 'name' |
194 | 194 | ); |
195 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
195 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
196 | 196 | $this->get_other_model()->get_this_model_name(), |
197 | 197 | 'ID' |
198 | 198 | ); |
@@ -209,17 +209,17 @@ discard block |
||
209 | 209 | ]; |
210 | 210 | |
211 | 211 | // if $where_query exists lets add them to the query_params. |
212 | - if (! empty($extra_join_model_fields_n_values)) { |
|
212 | + if ( ! empty($extra_join_model_fields_n_values)) { |
|
213 | 213 | // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
214 | 214 | // make sure we strip THIS models name from the query param |
215 | 215 | $parsed_query = []; |
216 | 216 | foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
217 | - $query_param = str_replace( |
|
218 | - $this->get_join_model()->get_this_model_name() . ".", |
|
217 | + $query_param = str_replace( |
|
218 | + $this->get_join_model()->get_this_model_name().".", |
|
219 | 219 | "", |
220 | 220 | $query_param |
221 | 221 | ); |
222 | - $parsed_query[ $query_param ] = $val; |
|
222 | + $parsed_query[$query_param] = $val; |
|
223 | 223 | } |
224 | 224 | $cols_n_values = array_merge($cols_n_values, $parsed_query); |
225 | 225 | } |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | // if there is already an entry in the join table, indicating a relationship, we're done |
232 | 232 | // again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
233 | 233 | // the other tables, use the joining model directly! |
234 | - if (! $existing_entry_in_join_table) { |
|
234 | + if ( ! $existing_entry_in_join_table) { |
|
235 | 235 | $this->get_join_model()->insert($cols_n_values); |
236 | 236 | } |
237 | 237 | return $other_model_obj; |
@@ -254,15 +254,15 @@ discard block |
||
254 | 254 | $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
255 | 255 | $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
256 | 256 | // check if such a relationship already exists |
257 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
257 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
258 | 258 | $this->get_this_model()->get_this_model_name(), |
259 | 259 | 'ID' |
260 | 260 | ); |
261 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
261 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
262 | 262 | $this->get_this_model()->get_this_model_name(), |
263 | 263 | 'name' |
264 | 264 | ); |
265 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
265 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
266 | 266 | $this->get_other_model()->get_this_model_name(), |
267 | 267 | 'ID' |
268 | 268 | ); |
@@ -279,17 +279,17 @@ discard block |
||
279 | 279 | ]; |
280 | 280 | |
281 | 281 | // if $where_query exists lets add them to the query_params. |
282 | - if (! empty($where_query)) { |
|
282 | + if ( ! empty($where_query)) { |
|
283 | 283 | // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
284 | 284 | // make sure we strip THIS models name from the query param |
285 | 285 | $parsed_query = []; |
286 | 286 | foreach ($where_query as $query_param => $val) { |
287 | - $query_param = str_replace( |
|
288 | - $this->get_join_model()->get_this_model_name() . ".", |
|
287 | + $query_param = str_replace( |
|
288 | + $this->get_join_model()->get_this_model_name().".", |
|
289 | 289 | "", |
290 | 290 | $query_param |
291 | 291 | ); |
292 | - $parsed_query[ $query_param ] = $val; |
|
292 | + $parsed_query[$query_param] = $val; |
|
293 | 293 | } |
294 | 294 | $cols_n_values = array_merge($cols_n_values, $parsed_query); |
295 | 295 | } |
@@ -14,106 +14,106 @@ |
||
14 | 14 | */ |
15 | 15 | class EE_Has_Many_Any_Relation extends EE_Has_Many_Relation |
16 | 16 | { |
17 | - /** |
|
18 | - * Gets the SQL string for performing the join between this model and the other model. |
|
19 | - * |
|
20 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
21 | - * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
22 | - * other_model_primary_table.fk" etc |
|
23 | - * @throws EE_Error |
|
24 | - * @throws Exception |
|
25 | - */ |
|
26 | - public function get_join_statement(string $model_relation_chain): string |
|
27 | - { |
|
28 | - // create the sql string like |
|
29 | - // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions |
|
30 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
31 | - $other_table_fk_field = |
|
32 | - $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
33 | - $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
34 | - $model_relation_chain, |
|
35 | - $this->get_this_model()->get_this_model_name() |
|
36 | - ) . $this_table_pk_field->get_table_alias(); |
|
37 | - $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
38 | - $model_relation_chain, |
|
39 | - $this->get_other_model()->get_this_model_name() |
|
40 | - ) . $other_table_fk_field->get_table_alias(); |
|
41 | - $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
42 | - $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
|
17 | + /** |
|
18 | + * Gets the SQL string for performing the join between this model and the other model. |
|
19 | + * |
|
20 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
21 | + * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
22 | + * other_model_primary_table.fk" etc |
|
23 | + * @throws EE_Error |
|
24 | + * @throws Exception |
|
25 | + */ |
|
26 | + public function get_join_statement(string $model_relation_chain): string |
|
27 | + { |
|
28 | + // create the sql string like |
|
29 | + // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions |
|
30 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
31 | + $other_table_fk_field = |
|
32 | + $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
33 | + $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
34 | + $model_relation_chain, |
|
35 | + $this->get_this_model()->get_this_model_name() |
|
36 | + ) . $this_table_pk_field->get_table_alias(); |
|
37 | + $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
38 | + $model_relation_chain, |
|
39 | + $this->get_other_model()->get_this_model_name() |
|
40 | + ) . $other_table_fk_field->get_table_alias(); |
|
41 | + $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
42 | + $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
|
43 | 43 | |
44 | - return $this->_left_join( |
|
45 | - $fk_table, |
|
46 | - $fk_table_alias, |
|
47 | - $other_table_fk_field->get_table_column(), |
|
48 | - $pk_table_alias, |
|
49 | - $this_table_pk_field->get_table_column(), |
|
50 | - $fk_table_alias . '.' . $field_with_model_name->get_table_column() |
|
51 | - . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
52 | - ) |
|
53 | - . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
54 | - } |
|
44 | + return $this->_left_join( |
|
45 | + $fk_table, |
|
46 | + $fk_table_alias, |
|
47 | + $other_table_fk_field->get_table_column(), |
|
48 | + $pk_table_alias, |
|
49 | + $this_table_pk_field->get_table_column(), |
|
50 | + $fk_table_alias . '.' . $field_with_model_name->get_table_column() |
|
51 | + . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
52 | + ) |
|
53 | + . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
54 | + } |
|
55 | 55 | |
56 | 56 | |
57 | - /** |
|
58 | - * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
59 | - * you like. |
|
60 | - * |
|
61 | - * @param EE_Base_Class|int $this_obj_or_id |
|
62 | - * @param EE_Base_Class|int $other_obj_or_id |
|
63 | - * @param array $extra_join_model_fields_n_values |
|
64 | - * @return EE_Base_Class |
|
65 | - * @throws EE_Error |
|
66 | - * @throws Exception |
|
67 | - */ |
|
68 | - public function add_relation_to( |
|
69 | - $this_obj_or_id, |
|
70 | - $other_obj_or_id, |
|
71 | - array $extra_join_model_fields_n_values = [] |
|
72 | - ): EE_Base_Class { |
|
73 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
74 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
57 | + /** |
|
58 | + * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
59 | + * you like. |
|
60 | + * |
|
61 | + * @param EE_Base_Class|int $this_obj_or_id |
|
62 | + * @param EE_Base_Class|int $other_obj_or_id |
|
63 | + * @param array $extra_join_model_fields_n_values |
|
64 | + * @return EE_Base_Class |
|
65 | + * @throws EE_Error |
|
66 | + * @throws Exception |
|
67 | + */ |
|
68 | + public function add_relation_to( |
|
69 | + $this_obj_or_id, |
|
70 | + $other_obj_or_id, |
|
71 | + array $extra_join_model_fields_n_values = [] |
|
72 | + ): EE_Base_Class { |
|
73 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
74 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
75 | 75 | |
76 | - // find the field on the other model which is a foreign key to this model |
|
77 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to( |
|
78 | - $this->get_this_model()->get_this_model_name() |
|
79 | - ); |
|
80 | - // set that field on the other model to this model's ID |
|
81 | - $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
82 | - $other_model_obj->set( |
|
83 | - $this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
84 | - $this->get_this_model()->get_this_model_name() |
|
85 | - ); |
|
86 | - $other_model_obj->save(); |
|
87 | - return $other_model_obj; |
|
88 | - } |
|
76 | + // find the field on the other model which is a foreign key to this model |
|
77 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to( |
|
78 | + $this->get_this_model()->get_this_model_name() |
|
79 | + ); |
|
80 | + // set that field on the other model to this model's ID |
|
81 | + $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
82 | + $other_model_obj->set( |
|
83 | + $this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
84 | + $this->get_this_model()->get_this_model_name() |
|
85 | + ); |
|
86 | + $other_model_obj->save(); |
|
87 | + return $other_model_obj; |
|
88 | + } |
|
89 | 89 | |
90 | 90 | |
91 | - /** |
|
92 | - * Sets the other model object's foreign key to its default, instead of pointing to this model object. |
|
93 | - * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it |
|
94 | - * |
|
95 | - * @param EE_Base_Class|int $this_obj_or_id |
|
96 | - * @param EE_Base_Class|int $other_obj_or_id |
|
97 | - * @param array $where_query |
|
98 | - * @return EE_Base_Class |
|
99 | - * @throws EE_Error |
|
100 | - * @throws Exception |
|
101 | - */ |
|
102 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, array $where_query = []): EE_Base_Class |
|
103 | - { |
|
104 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
105 | - // find the field on the other model which is a foreign key to this model |
|
106 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to( |
|
107 | - $this->get_this_model()->get_this_model_name() |
|
108 | - ); |
|
109 | - // set that field on the other model to this model's ID |
|
110 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
111 | - $other_model_obj->set( |
|
112 | - $this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
113 | - null, |
|
114 | - true |
|
115 | - ); |
|
116 | - $other_model_obj->save(); |
|
117 | - return $other_model_obj; |
|
118 | - } |
|
91 | + /** |
|
92 | + * Sets the other model object's foreign key to its default, instead of pointing to this model object. |
|
93 | + * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it |
|
94 | + * |
|
95 | + * @param EE_Base_Class|int $this_obj_or_id |
|
96 | + * @param EE_Base_Class|int $other_obj_or_id |
|
97 | + * @param array $where_query |
|
98 | + * @return EE_Base_Class |
|
99 | + * @throws EE_Error |
|
100 | + * @throws Exception |
|
101 | + */ |
|
102 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, array $where_query = []): EE_Base_Class |
|
103 | + { |
|
104 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
105 | + // find the field on the other model which is a foreign key to this model |
|
106 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to( |
|
107 | + $this->get_this_model()->get_this_model_name() |
|
108 | + ); |
|
109 | + // set that field on the other model to this model's ID |
|
110 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
111 | + $other_model_obj->set( |
|
112 | + $this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
113 | + null, |
|
114 | + true |
|
115 | + ); |
|
116 | + $other_model_obj->save(); |
|
117 | + return $other_model_obj; |
|
118 | + } |
|
119 | 119 | } |
@@ -33,11 +33,11 @@ discard block |
||
33 | 33 | $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
34 | 34 | $model_relation_chain, |
35 | 35 | $this->get_this_model()->get_this_model_name() |
36 | - ) . $this_table_pk_field->get_table_alias(); |
|
37 | - $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
36 | + ).$this_table_pk_field->get_table_alias(); |
|
37 | + $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
38 | 38 | $model_relation_chain, |
39 | 39 | $this->get_other_model()->get_this_model_name() |
40 | - ) . $other_table_fk_field->get_table_alias(); |
|
40 | + ).$other_table_fk_field->get_table_alias(); |
|
41 | 41 | $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
42 | 42 | $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
43 | 43 | |
@@ -47,8 +47,8 @@ discard block |
||
47 | 47 | $other_table_fk_field->get_table_column(), |
48 | 48 | $pk_table_alias, |
49 | 49 | $this_table_pk_field->get_table_column(), |
50 | - $fk_table_alias . '.' . $field_with_model_name->get_table_column() |
|
51 | - . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
50 | + $fk_table_alias.'.'.$field_with_model_name->get_table_column() |
|
51 | + . "='".$this->get_this_model()->get_this_model_name()."'" |
|
52 | 52 | ) |
53 | 53 | . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
54 | 54 | } |
@@ -14,106 +14,106 @@ |
||
14 | 14 | */ |
15 | 15 | class EE_Belongs_To_Any_Relation extends EE_Belongs_To_Relation |
16 | 16 | { |
17 | - /** |
|
18 | - * get_join_statement |
|
19 | - * |
|
20 | - * @param string $model_relation_chain |
|
21 | - * @return string |
|
22 | - * @throws EE_Error |
|
23 | - * @throws Exception |
|
24 | - */ |
|
25 | - public function get_join_statement(string $model_relation_chain): string |
|
26 | - { |
|
27 | - // create the sql string like |
|
28 | - $this_table_fk_field = |
|
29 | - $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
30 | - // ALSO, need to get the field with the model name |
|
31 | - $field_with_model_name = $this->get_this_model()->get_field_containing_related_model_name(); |
|
17 | + /** |
|
18 | + * get_join_statement |
|
19 | + * |
|
20 | + * @param string $model_relation_chain |
|
21 | + * @return string |
|
22 | + * @throws EE_Error |
|
23 | + * @throws Exception |
|
24 | + */ |
|
25 | + public function get_join_statement(string $model_relation_chain): string |
|
26 | + { |
|
27 | + // create the sql string like |
|
28 | + $this_table_fk_field = |
|
29 | + $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
30 | + // ALSO, need to get the field with the model name |
|
31 | + $field_with_model_name = $this->get_this_model()->get_field_containing_related_model_name(); |
|
32 | 32 | |
33 | 33 | |
34 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
35 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
36 | - $model_relation_chain, |
|
37 | - $this->get_this_model()->get_this_model_name() |
|
38 | - ) . $this_table_fk_field->get_table_alias(); |
|
39 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
40 | - $model_relation_chain, |
|
41 | - $this->get_other_model()->get_this_model_name() |
|
42 | - ) . $other_table_pk_field->get_table_alias(); |
|
43 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
44 | - return $this->_left_join( |
|
45 | - $other_table, |
|
46 | - $other_table_alias, |
|
47 | - $other_table_pk_field->get_table_column(), |
|
48 | - $this_table_alias, |
|
49 | - $this_table_fk_field->get_table_column(), |
|
50 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name( |
|
51 | - ) . "'" |
|
52 | - ) |
|
53 | - . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
54 | - } |
|
34 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
35 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
36 | + $model_relation_chain, |
|
37 | + $this->get_this_model()->get_this_model_name() |
|
38 | + ) . $this_table_fk_field->get_table_alias(); |
|
39 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
40 | + $model_relation_chain, |
|
41 | + $this->get_other_model()->get_this_model_name() |
|
42 | + ) . $other_table_pk_field->get_table_alias(); |
|
43 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
44 | + return $this->_left_join( |
|
45 | + $other_table, |
|
46 | + $other_table_alias, |
|
47 | + $other_table_pk_field->get_table_column(), |
|
48 | + $this_table_alias, |
|
49 | + $this_table_fk_field->get_table_column(), |
|
50 | + $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name( |
|
51 | + ) . "'" |
|
52 | + ) |
|
53 | + . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
54 | + } |
|
55 | 55 | |
56 | 56 | |
57 | - /** |
|
58 | - * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
59 | - * you like. |
|
60 | - * |
|
61 | - * @param EE_Base_Class|int $this_obj_or_id |
|
62 | - * @param EE_Base_Class|int $other_obj_or_id |
|
63 | - * @param array $extra_join_model_fields_n_values |
|
64 | - * @return EE_Base_Class |
|
65 | - * @throws EE_Error |
|
66 | - * @throws ReflectionException |
|
67 | - * @throws Exception |
|
68 | - */ |
|
69 | - public function add_relation_to( |
|
70 | - $this_obj_or_id, |
|
71 | - $other_obj_or_id, |
|
72 | - array $extra_join_model_fields_n_values = [] |
|
73 | - ): EE_Base_Class { |
|
74 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
75 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
76 | - // find the field on THIS model which a foreign key to the other model |
|
77 | - $fk_on_this_model = |
|
78 | - $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
79 | - // set that field on the other model to this model's ID |
|
80 | - $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
81 | - // and make sure this model's field with the foreign model name is set to the correct value |
|
82 | - $this_model_obj->set( |
|
83 | - $this->get_this_model()->get_field_containing_related_model_name()->get_name(), |
|
84 | - $this->get_other_model()->get_this_model_name() |
|
85 | - ); |
|
86 | - $this_model_obj->save(); |
|
87 | - return $other_model_obj; |
|
88 | - } |
|
57 | + /** |
|
58 | + * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
59 | + * you like. |
|
60 | + * |
|
61 | + * @param EE_Base_Class|int $this_obj_or_id |
|
62 | + * @param EE_Base_Class|int $other_obj_or_id |
|
63 | + * @param array $extra_join_model_fields_n_values |
|
64 | + * @return EE_Base_Class |
|
65 | + * @throws EE_Error |
|
66 | + * @throws ReflectionException |
|
67 | + * @throws Exception |
|
68 | + */ |
|
69 | + public function add_relation_to( |
|
70 | + $this_obj_or_id, |
|
71 | + $other_obj_or_id, |
|
72 | + array $extra_join_model_fields_n_values = [] |
|
73 | + ): EE_Base_Class { |
|
74 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
75 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
76 | + // find the field on THIS model which a foreign key to the other model |
|
77 | + $fk_on_this_model = |
|
78 | + $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
79 | + // set that field on the other model to this model's ID |
|
80 | + $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
81 | + // and make sure this model's field with the foreign model name is set to the correct value |
|
82 | + $this_model_obj->set( |
|
83 | + $this->get_this_model()->get_field_containing_related_model_name()->get_name(), |
|
84 | + $this->get_other_model()->get_this_model_name() |
|
85 | + ); |
|
86 | + $this_model_obj->save(); |
|
87 | + return $other_model_obj; |
|
88 | + } |
|
89 | 89 | |
90 | 90 | |
91 | - /** |
|
92 | - * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
93 | - * |
|
94 | - * @param EE_Base_Class|int $this_obj_or_id |
|
95 | - * @param EE_Base_Class|int $other_obj_or_id |
|
96 | - * @param array $where_query |
|
97 | - * @return EE_Base_Class |
|
98 | - * @throws EE_Error |
|
99 | - * @throws ReflectionException |
|
100 | - * @throws Exception |
|
101 | - */ |
|
102 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, array $where_query = []): EE_Base_Class |
|
103 | - { |
|
104 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
105 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
106 | - // find the field on the other model which is a foreign key to this model |
|
107 | - $fk_on_this_model = |
|
108 | - $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
109 | - // set that field on the other model to this model's ID |
|
110 | - $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
111 | - $this_model_obj->set( |
|
112 | - $this->get_this_model()->get_field_containing_related_model_name()->get_name(), |
|
113 | - null, |
|
114 | - true |
|
115 | - ); |
|
116 | - $this_model_obj->save(); |
|
117 | - return $other_model_obj; |
|
118 | - } |
|
91 | + /** |
|
92 | + * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
93 | + * |
|
94 | + * @param EE_Base_Class|int $this_obj_or_id |
|
95 | + * @param EE_Base_Class|int $other_obj_or_id |
|
96 | + * @param array $where_query |
|
97 | + * @return EE_Base_Class |
|
98 | + * @throws EE_Error |
|
99 | + * @throws ReflectionException |
|
100 | + * @throws Exception |
|
101 | + */ |
|
102 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, array $where_query = []): EE_Base_Class |
|
103 | + { |
|
104 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
105 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
106 | + // find the field on the other model which is a foreign key to this model |
|
107 | + $fk_on_this_model = |
|
108 | + $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
109 | + // set that field on the other model to this model's ID |
|
110 | + $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
111 | + $this_model_obj->set( |
|
112 | + $this->get_this_model()->get_field_containing_related_model_name()->get_name(), |
|
113 | + null, |
|
114 | + true |
|
115 | + ); |
|
116 | + $this_model_obj->save(); |
|
117 | + return $other_model_obj; |
|
118 | + } |
|
119 | 119 | } |
@@ -35,20 +35,20 @@ |
||
35 | 35 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
36 | 36 | $model_relation_chain, |
37 | 37 | $this->get_this_model()->get_this_model_name() |
38 | - ) . $this_table_fk_field->get_table_alias(); |
|
39 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
38 | + ).$this_table_fk_field->get_table_alias(); |
|
39 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
40 | 40 | $model_relation_chain, |
41 | 41 | $this->get_other_model()->get_this_model_name() |
42 | - ) . $other_table_pk_field->get_table_alias(); |
|
43 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
42 | + ).$other_table_pk_field->get_table_alias(); |
|
43 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
44 | 44 | return $this->_left_join( |
45 | 45 | $other_table, |
46 | 46 | $other_table_alias, |
47 | 47 | $other_table_pk_field->get_table_column(), |
48 | 48 | $this_table_alias, |
49 | 49 | $this_table_fk_field->get_table_column(), |
50 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name( |
|
51 | - ) . "'" |
|
50 | + $field_with_model_name->get_qualified_column()."='".$this->get_other_model()->get_this_model_name( |
|
51 | + )."'" |
|
52 | 52 | ) |
53 | 53 | . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
54 | 54 | } |
@@ -16,531 +16,531 @@ |
||
16 | 16 | */ |
17 | 17 | abstract class EE_Model_Relation_Base implements HasSchemaInterface |
18 | 18 | { |
19 | - /** |
|
20 | - * The model name of which this relation is a component (ie, the model that called new EE_Model_Relation_Base) |
|
21 | - * |
|
22 | - * @var string eg Event, Question_Group, Registration |
|
23 | - */ |
|
24 | - private ?string $_this_model_name = ''; |
|
25 | - |
|
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 ?string $_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 ?string $_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 ?string $_blocking_delete_error_message; |
|
50 | - |
|
51 | - protected bool $_blocking_delete = false; |
|
52 | - |
|
53 | - |
|
54 | - /** |
|
55 | - * Object representing the relationship between two models. This knows how to join the models, |
|
56 | - * get related models across the relation, and add-and-remove the relationships. |
|
57 | - * |
|
58 | - * @param bool $block_deletes if there are related models across this relation, block (prevent |
|
59 | - * and add an error) the deletion of this model |
|
60 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
61 | - * default |
|
62 | - */ |
|
63 | - public function __construct(bool $block_deletes = false, string $blocking_delete_error_message = '') |
|
64 | - { |
|
65 | - $this->_blocking_delete = filter_var($block_deletes, FILTER_VALIDATE_BOOLEAN); |
|
66 | - $this->_blocking_delete_error_message = $blocking_delete_error_message; |
|
67 | - } |
|
68 | - |
|
69 | - |
|
70 | - /** |
|
71 | - * @param $this_model_name |
|
72 | - * @param $other_model_name |
|
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 | - } |
|
79 | - |
|
80 | - |
|
81 | - /** |
|
82 | - * Gets the model where this relation is defined. |
|
83 | - * |
|
84 | - * @return EEM_Base |
|
85 | - * @throws Exception |
|
86 | - */ |
|
87 | - public function get_this_model(): EEM_Base |
|
88 | - { |
|
89 | - return $this->_get_model($this->_this_model_name); |
|
90 | - } |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Gets the model which this relation establishes the relation TO (ie, |
|
95 | - * this relation object was defined on get_this_model(), get_other_model() is the other one) |
|
96 | - * |
|
97 | - * @return EEM_Base |
|
98 | - * @throws Exception |
|
99 | - */ |
|
100 | - public function get_other_model(): EEM_Base |
|
101 | - { |
|
102 | - return $this->_get_model($this->_other_model_name); |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * Internally used by get_this_model() and get_other_model() |
|
108 | - * |
|
109 | - * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
|
110 | - * @return EEM_Base |
|
111 | - * @throws Exception |
|
112 | - */ |
|
113 | - protected function _get_model(string $model_name): EEM_Base |
|
114 | - { |
|
115 | - /** @var EEM_Base $modelInstance */ |
|
116 | - $modelInstance = EE_Registry::instance()->load_model($model_name); |
|
117 | - $modelInstance->set_timezone($this->_timezone); |
|
118 | - return $modelInstance; |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * entirely possible that relations may be called from a model and we need to make sure those relations have their |
|
124 | - * timezone set correctly. |
|
125 | - * |
|
126 | - * @param string|null $timezone timezone to set. |
|
127 | - */ |
|
128 | - public function set_timezone(?string $timezone = '') |
|
129 | - { |
|
130 | - if ($timezone) { |
|
131 | - $this->_timezone = $timezone; |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - |
|
136 | - /** |
|
137 | - * @param string $other_table |
|
138 | - * @param string $other_table_alias |
|
139 | - * @param string $other_table_column |
|
140 | - * @param string $this_table_alias |
|
141 | - * @param string $this_table_join_column |
|
142 | - * @param string $extra_join_sql |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - protected function _left_join( |
|
146 | - string $other_table, |
|
147 | - string $other_table_alias, |
|
148 | - string $other_table_column, |
|
149 | - string $this_table_alias, |
|
150 | - string $this_table_join_column, |
|
151 | - string $extra_join_sql = '' |
|
152 | - ): string { |
|
153 | - return " |
|
19 | + /** |
|
20 | + * The model name of which this relation is a component (ie, the model that called new EE_Model_Relation_Base) |
|
21 | + * |
|
22 | + * @var string eg Event, Question_Group, Registration |
|
23 | + */ |
|
24 | + private ?string $_this_model_name = ''; |
|
25 | + |
|
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 ?string $_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 ?string $_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 ?string $_blocking_delete_error_message; |
|
50 | + |
|
51 | + protected bool $_blocking_delete = false; |
|
52 | + |
|
53 | + |
|
54 | + /** |
|
55 | + * Object representing the relationship between two models. This knows how to join the models, |
|
56 | + * get related models across the relation, and add-and-remove the relationships. |
|
57 | + * |
|
58 | + * @param bool $block_deletes if there are related models across this relation, block (prevent |
|
59 | + * and add an error) the deletion of this model |
|
60 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
61 | + * default |
|
62 | + */ |
|
63 | + public function __construct(bool $block_deletes = false, string $blocking_delete_error_message = '') |
|
64 | + { |
|
65 | + $this->_blocking_delete = filter_var($block_deletes, FILTER_VALIDATE_BOOLEAN); |
|
66 | + $this->_blocking_delete_error_message = $blocking_delete_error_message; |
|
67 | + } |
|
68 | + |
|
69 | + |
|
70 | + /** |
|
71 | + * @param $this_model_name |
|
72 | + * @param $other_model_name |
|
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 | + } |
|
79 | + |
|
80 | + |
|
81 | + /** |
|
82 | + * Gets the model where this relation is defined. |
|
83 | + * |
|
84 | + * @return EEM_Base |
|
85 | + * @throws Exception |
|
86 | + */ |
|
87 | + public function get_this_model(): EEM_Base |
|
88 | + { |
|
89 | + return $this->_get_model($this->_this_model_name); |
|
90 | + } |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Gets the model which this relation establishes the relation TO (ie, |
|
95 | + * this relation object was defined on get_this_model(), get_other_model() is the other one) |
|
96 | + * |
|
97 | + * @return EEM_Base |
|
98 | + * @throws Exception |
|
99 | + */ |
|
100 | + public function get_other_model(): EEM_Base |
|
101 | + { |
|
102 | + return $this->_get_model($this->_other_model_name); |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * Internally used by get_this_model() and get_other_model() |
|
108 | + * |
|
109 | + * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
|
110 | + * @return EEM_Base |
|
111 | + * @throws Exception |
|
112 | + */ |
|
113 | + protected function _get_model(string $model_name): EEM_Base |
|
114 | + { |
|
115 | + /** @var EEM_Base $modelInstance */ |
|
116 | + $modelInstance = EE_Registry::instance()->load_model($model_name); |
|
117 | + $modelInstance->set_timezone($this->_timezone); |
|
118 | + return $modelInstance; |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * entirely possible that relations may be called from a model and we need to make sure those relations have their |
|
124 | + * timezone set correctly. |
|
125 | + * |
|
126 | + * @param string|null $timezone timezone to set. |
|
127 | + */ |
|
128 | + public function set_timezone(?string $timezone = '') |
|
129 | + { |
|
130 | + if ($timezone) { |
|
131 | + $this->_timezone = $timezone; |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + |
|
136 | + /** |
|
137 | + * @param string $other_table |
|
138 | + * @param string $other_table_alias |
|
139 | + * @param string $other_table_column |
|
140 | + * @param string $this_table_alias |
|
141 | + * @param string $this_table_join_column |
|
142 | + * @param string $extra_join_sql |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + protected function _left_join( |
|
146 | + string $other_table, |
|
147 | + string $other_table_alias, |
|
148 | + string $other_table_column, |
|
149 | + string $this_table_alias, |
|
150 | + string $this_table_join_column, |
|
151 | + string $extra_join_sql = '' |
|
152 | + ): string { |
|
153 | + return " |
|
154 | 154 | LEFT JOIN " . $other_table . " AS " . $other_table_alias . " |
155 | 155 | ON " . $other_table_alias . "." . $other_table_column |
156 | - . "=" . $this_table_alias . "." . $this_table_join_column |
|
157 | - . ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
158 | - } |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * Gets all the model objects of type of other model related to $model_object, |
|
163 | - * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation. |
|
164 | - * For both of those child classes, $model_object must be saved so that it has an ID before querying, |
|
165 | - * otherwise an error will be thrown. Note: by default we disable default_where_conditions |
|
166 | - * EE_Belongs_To_Relation doesn't need to be saved before querying. |
|
167 | - * |
|
168 | - * @param EE_Base_Class|int|string $model_object_or_id or the primary key of this model |
|
169 | - * @param array|null $query_params |
|
170 | - * @param boolean $values_already_prepared_by_model_object @deprecated 4.8.1 |
|
171 | - * @return EE_Base_Class[] |
|
172 | - * @throws EE_Error |
|
173 | - * @throws ReflectionException |
|
174 | - * @throws Exception |
|
175 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
176 | - */ |
|
177 | - public function get_all_related( |
|
178 | - $model_object_or_id, |
|
179 | - ?array $query_params = [], |
|
180 | - bool $values_already_prepared_by_model_object = false |
|
181 | - ): array { |
|
182 | - if ($values_already_prepared_by_model_object !== false) { |
|
183 | - EE_Error::doing_it_wrong( |
|
184 | - 'EE_Model_Relation_Base::get_all_related', |
|
185 | - esc_html__( |
|
186 | - 'The argument $values_already_prepared_by_model_object is no longer used.', |
|
187 | - 'event_espresso' |
|
188 | - ), |
|
189 | - '4.8.1' |
|
190 | - ); |
|
191 | - } |
|
192 | - $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
193 | - |
|
194 | - $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() |
|
195 | - . "." . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
196 | - |
|
197 | - $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
198 | - |
|
199 | - $query_params[0][ $query_param_where_this_model_pk ] = $model_object_id; |
|
200 | - return $this->get_other_model()->get_all($query_params); |
|
201 | - } |
|
202 | - |
|
203 | - |
|
204 | - /** |
|
205 | - * Alters the $query_params to disable default where conditions, unless otherwise specified |
|
206 | - * |
|
207 | - * @param array $query_params |
|
208 | - * @return array |
|
209 | - */ |
|
210 | - protected function _disable_default_where_conditions_on_query_param(array $query_params): array |
|
211 | - { |
|
212 | - if (! isset($query_params['default_where_conditions'])) { |
|
213 | - $query_params['default_where_conditions'] = 'none'; |
|
214 | - } |
|
215 | - return $query_params; |
|
216 | - } |
|
217 | - |
|
218 | - |
|
219 | - /** |
|
220 | - * Deletes the related model objects which meet the query parameters. If no |
|
221 | - * parameters are specified, then all related model objects will be deleted. |
|
222 | - * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
223 | - * model objects will only be soft-deleted. |
|
224 | - * |
|
225 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
226 | - * @param array $query_params |
|
227 | - * @return int of how many related models got deleted |
|
228 | - * @throws EE_Error |
|
229 | - * @throws ReflectionException |
|
230 | - * @throws Exception |
|
231 | - */ |
|
232 | - public function delete_all_related($model_object_or_id, array $query_params = []): int |
|
233 | - { |
|
234 | - // for each thing we would delete, |
|
235 | - $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
236 | - // determine if it's blocked by anything else before it can be deleted |
|
237 | - $deleted_count = 0; |
|
238 | - foreach ($related_model_objects as $related_model_object) { |
|
239 | - $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
240 | - $related_model_object, |
|
241 | - $model_object_or_id |
|
242 | - ); |
|
243 | - /* @var $model_object_or_id EE_Base_Class */ |
|
244 | - if (! $delete_is_blocked) { |
|
245 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
246 | - $related_model_object->delete(); |
|
247 | - $deleted_count++; |
|
248 | - } |
|
249 | - } |
|
250 | - return $deleted_count; |
|
251 | - } |
|
252 | - |
|
253 | - |
|
254 | - /** |
|
255 | - * Deletes the related model objects which meet the query parameters. If no |
|
256 | - * parameters are specified, then all related model objects will be deleted. |
|
257 | - * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
258 | - * model objects will only be soft-deleted. |
|
259 | - * |
|
260 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
261 | - * @param array $query_params |
|
262 | - * @return int of how many related models got deleted |
|
263 | - * @throws EE_Error |
|
264 | - * @throws ReflectionException |
|
265 | - * @throws Exception |
|
266 | - */ |
|
267 | - public function delete_related_permanently($model_object_or_id, array $query_params = []): int |
|
268 | - { |
|
269 | - // for each thing we would delete, |
|
270 | - $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
271 | - // determine if it's blocked by anything else before it can be deleted |
|
272 | - $deleted_count = 0; |
|
273 | - foreach ($related_model_objects as $related_model_object) { |
|
274 | - $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
275 | - $related_model_object, |
|
276 | - $model_object_or_id |
|
277 | - ); |
|
278 | - /* @var $model_object_or_id EE_Base_Class */ |
|
279 | - if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
|
280 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
281 | - $deleted_count++; |
|
282 | - if (! $delete_is_blocked) { |
|
283 | - $related_model_object->delete_permanently(); |
|
284 | - } else { |
|
285 | - // delete is blocked |
|
286 | - // brent and darren, in this case, wanted to just soft delete it then |
|
287 | - $related_model_object->delete(); |
|
288 | - } |
|
289 | - } else { |
|
290 | - // its not a soft-deletable thing anyways. do the normal logic. |
|
291 | - if (! $delete_is_blocked) { |
|
292 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
293 | - $related_model_object->delete(); |
|
294 | - $deleted_count++; |
|
295 | - } |
|
296 | - } |
|
297 | - } |
|
298 | - return $deleted_count; |
|
299 | - } |
|
300 | - |
|
301 | - |
|
302 | - /** |
|
303 | - * this just returns a model_object_id for incoming item that could be an object or id. |
|
304 | - * |
|
305 | - * @param EE_Base_Class|int|string $model_object_or_id model object or the primary key of this model |
|
306 | - * @return int |
|
307 | - * @throws EE_Error |
|
308 | - * @throws ReflectionException |
|
309 | - * @throws Exception |
|
310 | - */ |
|
311 | - protected function _get_model_object_id($model_object_or_id) |
|
312 | - { |
|
313 | - $model_object_id = $model_object_or_id; |
|
314 | - if ($model_object_or_id instanceof EE_Base_Class) { |
|
315 | - $model_object_id = $model_object_or_id->ID(); |
|
316 | - } |
|
317 | - if (! $model_object_id) { |
|
318 | - throw new EE_Error( |
|
319 | - sprintf( |
|
320 | - esc_html__( |
|
321 | - "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", |
|
322 | - "event_espresso" |
|
323 | - ), |
|
324 | - $this->get_other_model()->get_this_model_name(), |
|
325 | - $this->get_this_model()->get_this_model_name() |
|
326 | - ) |
|
327 | - ); |
|
328 | - } |
|
329 | - return $model_object_id; |
|
330 | - } |
|
331 | - |
|
332 | - |
|
333 | - /** |
|
334 | - * Gets the SQL string for performing the join between this model and the other model. |
|
335 | - * |
|
336 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
337 | - * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
338 | - * other_model_primary_table.fk" etc |
|
339 | - */ |
|
340 | - abstract public function get_join_statement(string $model_relation_chain): string; |
|
341 | - |
|
342 | - |
|
343 | - /** |
|
344 | - * Adds a relationships between the two model objects provided. Each type of relationship handles this differently |
|
345 | - * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this |
|
346 | - * relationship only allows this model to be related to a single other model of this type) |
|
347 | - * |
|
348 | - * @param EE_Base_Class|int|string $this_obj_or_id |
|
349 | - * @param EE_Base_Class|int|string $other_obj_or_id |
|
350 | - * @param array $extra_join_model_fields_n_values |
|
351 | - * @return EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for |
|
352 | - * $other_obj_or_id) |
|
353 | - */ |
|
354 | - abstract public function add_relation_to( |
|
355 | - $this_obj_or_id, |
|
356 | - $other_obj_or_id, |
|
357 | - array $extra_join_model_fields_n_values = [] |
|
358 | - ): EE_Base_Class; |
|
359 | - |
|
360 | - |
|
361 | - /** |
|
362 | - * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two |
|
363 | - * model objects |
|
364 | - * |
|
365 | - * @param EE_Base_Class|int|string $this_obj_or_id |
|
366 | - * @param EE_Base_Class|int|string $other_obj_or_id |
|
367 | - * @param array $where_query |
|
368 | - * @return EE_Base_Class |
|
369 | - */ |
|
370 | - abstract public function remove_relation_to( |
|
371 | - $this_obj_or_id, |
|
372 | - $other_obj_or_id, |
|
373 | - array $where_query = [] |
|
374 | - ): EE_Base_Class; |
|
375 | - |
|
376 | - |
|
377 | - /** |
|
378 | - * Removes ALL relation instances for this relation obj |
|
379 | - * |
|
380 | - * @param EE_Base_Class|int|string $this_obj_or_id |
|
381 | - * @param array $where_query_param @see |
|
382 | - * https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
383 | - * @return EE_Base_Class[] |
|
384 | - * @throws EE_Error |
|
385 | - * @throws ReflectionException |
|
386 | - */ |
|
387 | - public function remove_relations($this_obj_or_id, array $where_query_param = []): array |
|
388 | - { |
|
389 | - $related_things = $this->get_all_related($this_obj_or_id, [$where_query_param]); |
|
390 | - $objs_removed = []; |
|
391 | - foreach ($related_things as $related_thing) { |
|
392 | - $objs_removed[] = $this->remove_relation_to($this_obj_or_id, $related_thing); |
|
393 | - } |
|
394 | - return $objs_removed; |
|
395 | - } |
|
396 | - |
|
397 | - |
|
398 | - /** |
|
399 | - * If you aren't allowed to delete this model when there are related models across this |
|
400 | - * relation object, return true. Otherwise, if you can delete this model even though |
|
401 | - * related objects exist, returns false. |
|
402 | - * |
|
403 | - * @return boolean |
|
404 | - */ |
|
405 | - public function block_delete_if_related_models_exist(): bool |
|
406 | - { |
|
407 | - return $this->_blocking_delete; |
|
408 | - } |
|
409 | - |
|
410 | - |
|
411 | - /** |
|
412 | - * Gets the error message to show |
|
413 | - * |
|
414 | - * @return string |
|
415 | - * @throws Exception |
|
416 | - */ |
|
417 | - public function get_deletion_error_message(): string |
|
418 | - { |
|
419 | - if ($this->_blocking_delete_error_message) { |
|
420 | - return $this->_blocking_delete_error_message; |
|
421 | - } else { |
|
156 | + . "=" . $this_table_alias . "." . $this_table_join_column |
|
157 | + . ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
158 | + } |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * Gets all the model objects of type of other model related to $model_object, |
|
163 | + * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation. |
|
164 | + * For both of those child classes, $model_object must be saved so that it has an ID before querying, |
|
165 | + * otherwise an error will be thrown. Note: by default we disable default_where_conditions |
|
166 | + * EE_Belongs_To_Relation doesn't need to be saved before querying. |
|
167 | + * |
|
168 | + * @param EE_Base_Class|int|string $model_object_or_id or the primary key of this model |
|
169 | + * @param array|null $query_params |
|
170 | + * @param boolean $values_already_prepared_by_model_object @deprecated 4.8.1 |
|
171 | + * @return EE_Base_Class[] |
|
172 | + * @throws EE_Error |
|
173 | + * @throws ReflectionException |
|
174 | + * @throws Exception |
|
175 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
176 | + */ |
|
177 | + public function get_all_related( |
|
178 | + $model_object_or_id, |
|
179 | + ?array $query_params = [], |
|
180 | + bool $values_already_prepared_by_model_object = false |
|
181 | + ): array { |
|
182 | + if ($values_already_prepared_by_model_object !== false) { |
|
183 | + EE_Error::doing_it_wrong( |
|
184 | + 'EE_Model_Relation_Base::get_all_related', |
|
185 | + esc_html__( |
|
186 | + 'The argument $values_already_prepared_by_model_object is no longer used.', |
|
187 | + 'event_espresso' |
|
188 | + ), |
|
189 | + '4.8.1' |
|
190 | + ); |
|
191 | + } |
|
192 | + $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
193 | + |
|
194 | + $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() |
|
195 | + . "." . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
196 | + |
|
197 | + $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
198 | + |
|
199 | + $query_params[0][ $query_param_where_this_model_pk ] = $model_object_id; |
|
200 | + return $this->get_other_model()->get_all($query_params); |
|
201 | + } |
|
202 | + |
|
203 | + |
|
204 | + /** |
|
205 | + * Alters the $query_params to disable default where conditions, unless otherwise specified |
|
206 | + * |
|
207 | + * @param array $query_params |
|
208 | + * @return array |
|
209 | + */ |
|
210 | + protected function _disable_default_where_conditions_on_query_param(array $query_params): array |
|
211 | + { |
|
212 | + if (! isset($query_params['default_where_conditions'])) { |
|
213 | + $query_params['default_where_conditions'] = 'none'; |
|
214 | + } |
|
215 | + return $query_params; |
|
216 | + } |
|
217 | + |
|
218 | + |
|
219 | + /** |
|
220 | + * Deletes the related model objects which meet the query parameters. If no |
|
221 | + * parameters are specified, then all related model objects will be deleted. |
|
222 | + * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
223 | + * model objects will only be soft-deleted. |
|
224 | + * |
|
225 | + * @param EE_Base_Class|int|string $model_object_or_id |
|
226 | + * @param array $query_params |
|
227 | + * @return int of how many related models got deleted |
|
228 | + * @throws EE_Error |
|
229 | + * @throws ReflectionException |
|
230 | + * @throws Exception |
|
231 | + */ |
|
232 | + public function delete_all_related($model_object_or_id, array $query_params = []): int |
|
233 | + { |
|
234 | + // for each thing we would delete, |
|
235 | + $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
236 | + // determine if it's blocked by anything else before it can be deleted |
|
237 | + $deleted_count = 0; |
|
238 | + foreach ($related_model_objects as $related_model_object) { |
|
239 | + $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
240 | + $related_model_object, |
|
241 | + $model_object_or_id |
|
242 | + ); |
|
243 | + /* @var $model_object_or_id EE_Base_Class */ |
|
244 | + if (! $delete_is_blocked) { |
|
245 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
246 | + $related_model_object->delete(); |
|
247 | + $deleted_count++; |
|
248 | + } |
|
249 | + } |
|
250 | + return $deleted_count; |
|
251 | + } |
|
252 | + |
|
253 | + |
|
254 | + /** |
|
255 | + * Deletes the related model objects which meet the query parameters. If no |
|
256 | + * parameters are specified, then all related model objects will be deleted. |
|
257 | + * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
258 | + * model objects will only be soft-deleted. |
|
259 | + * |
|
260 | + * @param EE_Base_Class|int|string $model_object_or_id |
|
261 | + * @param array $query_params |
|
262 | + * @return int of how many related models got deleted |
|
263 | + * @throws EE_Error |
|
264 | + * @throws ReflectionException |
|
265 | + * @throws Exception |
|
266 | + */ |
|
267 | + public function delete_related_permanently($model_object_or_id, array $query_params = []): int |
|
268 | + { |
|
269 | + // for each thing we would delete, |
|
270 | + $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
271 | + // determine if it's blocked by anything else before it can be deleted |
|
272 | + $deleted_count = 0; |
|
273 | + foreach ($related_model_objects as $related_model_object) { |
|
274 | + $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
275 | + $related_model_object, |
|
276 | + $model_object_or_id |
|
277 | + ); |
|
278 | + /* @var $model_object_or_id EE_Base_Class */ |
|
279 | + if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
|
280 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
281 | + $deleted_count++; |
|
282 | + if (! $delete_is_blocked) { |
|
283 | + $related_model_object->delete_permanently(); |
|
284 | + } else { |
|
285 | + // delete is blocked |
|
286 | + // brent and darren, in this case, wanted to just soft delete it then |
|
287 | + $related_model_object->delete(); |
|
288 | + } |
|
289 | + } else { |
|
290 | + // its not a soft-deletable thing anyways. do the normal logic. |
|
291 | + if (! $delete_is_blocked) { |
|
292 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
293 | + $related_model_object->delete(); |
|
294 | + $deleted_count++; |
|
295 | + } |
|
296 | + } |
|
297 | + } |
|
298 | + return $deleted_count; |
|
299 | + } |
|
300 | + |
|
301 | + |
|
302 | + /** |
|
303 | + * this just returns a model_object_id for incoming item that could be an object or id. |
|
304 | + * |
|
305 | + * @param EE_Base_Class|int|string $model_object_or_id model object or the primary key of this model |
|
306 | + * @return int |
|
307 | + * @throws EE_Error |
|
308 | + * @throws ReflectionException |
|
309 | + * @throws Exception |
|
310 | + */ |
|
311 | + protected function _get_model_object_id($model_object_or_id) |
|
312 | + { |
|
313 | + $model_object_id = $model_object_or_id; |
|
314 | + if ($model_object_or_id instanceof EE_Base_Class) { |
|
315 | + $model_object_id = $model_object_or_id->ID(); |
|
316 | + } |
|
317 | + if (! $model_object_id) { |
|
318 | + throw new EE_Error( |
|
319 | + sprintf( |
|
320 | + esc_html__( |
|
321 | + "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", |
|
322 | + "event_espresso" |
|
323 | + ), |
|
324 | + $this->get_other_model()->get_this_model_name(), |
|
325 | + $this->get_this_model()->get_this_model_name() |
|
326 | + ) |
|
327 | + ); |
|
328 | + } |
|
329 | + return $model_object_id; |
|
330 | + } |
|
331 | + |
|
332 | + |
|
333 | + /** |
|
334 | + * Gets the SQL string for performing the join between this model and the other model. |
|
335 | + * |
|
336 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
337 | + * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
338 | + * other_model_primary_table.fk" etc |
|
339 | + */ |
|
340 | + abstract public function get_join_statement(string $model_relation_chain): string; |
|
341 | + |
|
342 | + |
|
343 | + /** |
|
344 | + * Adds a relationships between the two model objects provided. Each type of relationship handles this differently |
|
345 | + * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this |
|
346 | + * relationship only allows this model to be related to a single other model of this type) |
|
347 | + * |
|
348 | + * @param EE_Base_Class|int|string $this_obj_or_id |
|
349 | + * @param EE_Base_Class|int|string $other_obj_or_id |
|
350 | + * @param array $extra_join_model_fields_n_values |
|
351 | + * @return EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for |
|
352 | + * $other_obj_or_id) |
|
353 | + */ |
|
354 | + abstract public function add_relation_to( |
|
355 | + $this_obj_or_id, |
|
356 | + $other_obj_or_id, |
|
357 | + array $extra_join_model_fields_n_values = [] |
|
358 | + ): EE_Base_Class; |
|
359 | + |
|
360 | + |
|
361 | + /** |
|
362 | + * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two |
|
363 | + * model objects |
|
364 | + * |
|
365 | + * @param EE_Base_Class|int|string $this_obj_or_id |
|
366 | + * @param EE_Base_Class|int|string $other_obj_or_id |
|
367 | + * @param array $where_query |
|
368 | + * @return EE_Base_Class |
|
369 | + */ |
|
370 | + abstract public function remove_relation_to( |
|
371 | + $this_obj_or_id, |
|
372 | + $other_obj_or_id, |
|
373 | + array $where_query = [] |
|
374 | + ): EE_Base_Class; |
|
375 | + |
|
376 | + |
|
377 | + /** |
|
378 | + * Removes ALL relation instances for this relation obj |
|
379 | + * |
|
380 | + * @param EE_Base_Class|int|string $this_obj_or_id |
|
381 | + * @param array $where_query_param @see |
|
382 | + * https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
383 | + * @return EE_Base_Class[] |
|
384 | + * @throws EE_Error |
|
385 | + * @throws ReflectionException |
|
386 | + */ |
|
387 | + public function remove_relations($this_obj_or_id, array $where_query_param = []): array |
|
388 | + { |
|
389 | + $related_things = $this->get_all_related($this_obj_or_id, [$where_query_param]); |
|
390 | + $objs_removed = []; |
|
391 | + foreach ($related_things as $related_thing) { |
|
392 | + $objs_removed[] = $this->remove_relation_to($this_obj_or_id, $related_thing); |
|
393 | + } |
|
394 | + return $objs_removed; |
|
395 | + } |
|
396 | + |
|
397 | + |
|
398 | + /** |
|
399 | + * If you aren't allowed to delete this model when there are related models across this |
|
400 | + * relation object, return true. Otherwise, if you can delete this model even though |
|
401 | + * related objects exist, returns false. |
|
402 | + * |
|
403 | + * @return boolean |
|
404 | + */ |
|
405 | + public function block_delete_if_related_models_exist(): bool |
|
406 | + { |
|
407 | + return $this->_blocking_delete; |
|
408 | + } |
|
409 | + |
|
410 | + |
|
411 | + /** |
|
412 | + * Gets the error message to show |
|
413 | + * |
|
414 | + * @return string |
|
415 | + * @throws Exception |
|
416 | + */ |
|
417 | + public function get_deletion_error_message(): string |
|
418 | + { |
|
419 | + if ($this->_blocking_delete_error_message) { |
|
420 | + return $this->_blocking_delete_error_message; |
|
421 | + } else { |
|
422 | 422 | // return sprintf(esc_html__('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)); |
423 | - return sprintf( |
|
424 | - esc_html__( |
|
425 | - '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.', |
|
426 | - "event_espresso" |
|
427 | - ), |
|
428 | - $this->get_this_model()->item_name(), |
|
429 | - $this->get_other_model()->item_name(), |
|
430 | - $this->get_other_model()->item_name(2) |
|
431 | - ); |
|
432 | - } |
|
433 | - } |
|
434 | - |
|
435 | - |
|
436 | - /** |
|
437 | - * Returns whatever is set as the nicename for the object. |
|
438 | - * |
|
439 | - * @return string |
|
440 | - * @throws Exception |
|
441 | - */ |
|
442 | - public function getSchemaDescription(): string |
|
443 | - { |
|
444 | - $description = $this instanceof EE_Belongs_To_Relation |
|
445 | - ? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso') |
|
446 | - : esc_html__('The related %1$s entities to the %2$s.', 'event_espresso'); |
|
447 | - return sprintf( |
|
448 | - $description, |
|
449 | - $this->get_other_model()->get_this_model_name(), |
|
450 | - $this->get_this_model()->get_this_model_name() |
|
451 | - ); |
|
452 | - } |
|
453 | - |
|
454 | - |
|
455 | - /** |
|
456 | - * Returns whatever is set as the $_schema_type property for the object. |
|
457 | - * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
458 | - * |
|
459 | - * @return string |
|
460 | - */ |
|
461 | - public function getSchemaType(): string |
|
462 | - { |
|
463 | - return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array'; |
|
464 | - } |
|
465 | - |
|
466 | - |
|
467 | - /** |
|
468 | - * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
469 | - * this method and return the properties for the schema. |
|
470 | - * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
471 | - * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
472 | - * |
|
473 | - * @return array |
|
474 | - */ |
|
475 | - public function getSchemaProperties(): array |
|
476 | - { |
|
477 | - return []; |
|
478 | - } |
|
479 | - |
|
480 | - |
|
481 | - /** |
|
482 | - * If a child class has enum values, they should override this method and provide a simple array |
|
483 | - * of the enum values. |
|
484 | - * The reason this is not a property on the class is because there may be filterable enum values that |
|
485 | - * are set on the instantiated object that could be filtered after construct. |
|
486 | - * |
|
487 | - * @return array |
|
488 | - */ |
|
489 | - public function getSchemaEnum(): array |
|
490 | - { |
|
491 | - return []; |
|
492 | - } |
|
493 | - |
|
494 | - |
|
495 | - /** |
|
496 | - * This returns the value of the $_schema_format property on the object. |
|
497 | - * |
|
498 | - * @return array |
|
499 | - */ |
|
500 | - public function getSchemaFormat(): array |
|
501 | - { |
|
502 | - return []; |
|
503 | - } |
|
504 | - |
|
505 | - |
|
506 | - /** |
|
507 | - * This returns the value of the $_schema_readonly property on the object. |
|
508 | - * |
|
509 | - * @return bool |
|
510 | - */ |
|
511 | - public function getSchemaReadonly(): bool |
|
512 | - { |
|
513 | - return true; |
|
514 | - } |
|
515 | - |
|
516 | - |
|
517 | - /** |
|
518 | - * This returns elements used to represent this field in the json schema. |
|
519 | - * |
|
520 | - * @link http://json-schema.org/ |
|
521 | - * @return array |
|
522 | - * @throws Exception |
|
523 | - */ |
|
524 | - public function getSchema(): array |
|
525 | - { |
|
526 | - $schema = [ |
|
527 | - 'description' => $this->getSchemaDescription(), |
|
528 | - 'type' => $this->getSchemaType(), |
|
529 | - 'relation' => true, |
|
530 | - 'relation_type' => get_class($this), |
|
531 | - 'readonly' => $this->getSchemaReadonly(), |
|
532 | - ]; |
|
533 | - |
|
534 | - if ($this instanceof EE_HABTM_Relation) { |
|
535 | - $schema['joining_model_name'] = $this->get_join_model()->get_this_model_name(); |
|
536 | - } |
|
537 | - |
|
538 | - if ($this->getSchemaType() === 'array') { |
|
539 | - $schema['items'] = [ |
|
540 | - 'type' => 'object', |
|
541 | - ]; |
|
542 | - } |
|
543 | - |
|
544 | - return $schema; |
|
545 | - } |
|
423 | + return sprintf( |
|
424 | + esc_html__( |
|
425 | + '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.', |
|
426 | + "event_espresso" |
|
427 | + ), |
|
428 | + $this->get_this_model()->item_name(), |
|
429 | + $this->get_other_model()->item_name(), |
|
430 | + $this->get_other_model()->item_name(2) |
|
431 | + ); |
|
432 | + } |
|
433 | + } |
|
434 | + |
|
435 | + |
|
436 | + /** |
|
437 | + * Returns whatever is set as the nicename for the object. |
|
438 | + * |
|
439 | + * @return string |
|
440 | + * @throws Exception |
|
441 | + */ |
|
442 | + public function getSchemaDescription(): string |
|
443 | + { |
|
444 | + $description = $this instanceof EE_Belongs_To_Relation |
|
445 | + ? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso') |
|
446 | + : esc_html__('The related %1$s entities to the %2$s.', 'event_espresso'); |
|
447 | + return sprintf( |
|
448 | + $description, |
|
449 | + $this->get_other_model()->get_this_model_name(), |
|
450 | + $this->get_this_model()->get_this_model_name() |
|
451 | + ); |
|
452 | + } |
|
453 | + |
|
454 | + |
|
455 | + /** |
|
456 | + * Returns whatever is set as the $_schema_type property for the object. |
|
457 | + * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
458 | + * |
|
459 | + * @return string |
|
460 | + */ |
|
461 | + public function getSchemaType(): string |
|
462 | + { |
|
463 | + return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array'; |
|
464 | + } |
|
465 | + |
|
466 | + |
|
467 | + /** |
|
468 | + * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
469 | + * this method and return the properties for the schema. |
|
470 | + * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
471 | + * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
472 | + * |
|
473 | + * @return array |
|
474 | + */ |
|
475 | + public function getSchemaProperties(): array |
|
476 | + { |
|
477 | + return []; |
|
478 | + } |
|
479 | + |
|
480 | + |
|
481 | + /** |
|
482 | + * If a child class has enum values, they should override this method and provide a simple array |
|
483 | + * of the enum values. |
|
484 | + * The reason this is not a property on the class is because there may be filterable enum values that |
|
485 | + * are set on the instantiated object that could be filtered after construct. |
|
486 | + * |
|
487 | + * @return array |
|
488 | + */ |
|
489 | + public function getSchemaEnum(): array |
|
490 | + { |
|
491 | + return []; |
|
492 | + } |
|
493 | + |
|
494 | + |
|
495 | + /** |
|
496 | + * This returns the value of the $_schema_format property on the object. |
|
497 | + * |
|
498 | + * @return array |
|
499 | + */ |
|
500 | + public function getSchemaFormat(): array |
|
501 | + { |
|
502 | + return []; |
|
503 | + } |
|
504 | + |
|
505 | + |
|
506 | + /** |
|
507 | + * This returns the value of the $_schema_readonly property on the object. |
|
508 | + * |
|
509 | + * @return bool |
|
510 | + */ |
|
511 | + public function getSchemaReadonly(): bool |
|
512 | + { |
|
513 | + return true; |
|
514 | + } |
|
515 | + |
|
516 | + |
|
517 | + /** |
|
518 | + * This returns elements used to represent this field in the json schema. |
|
519 | + * |
|
520 | + * @link http://json-schema.org/ |
|
521 | + * @return array |
|
522 | + * @throws Exception |
|
523 | + */ |
|
524 | + public function getSchema(): array |
|
525 | + { |
|
526 | + $schema = [ |
|
527 | + 'description' => $this->getSchemaDescription(), |
|
528 | + 'type' => $this->getSchemaType(), |
|
529 | + 'relation' => true, |
|
530 | + 'relation_type' => get_class($this), |
|
531 | + 'readonly' => $this->getSchemaReadonly(), |
|
532 | + ]; |
|
533 | + |
|
534 | + if ($this instanceof EE_HABTM_Relation) { |
|
535 | + $schema['joining_model_name'] = $this->get_join_model()->get_this_model_name(); |
|
536 | + } |
|
537 | + |
|
538 | + if ($this->getSchemaType() === 'array') { |
|
539 | + $schema['items'] = [ |
|
540 | + 'type' => 'object', |
|
541 | + ]; |
|
542 | + } |
|
543 | + |
|
544 | + return $schema; |
|
545 | + } |
|
546 | 546 | } |
@@ -12,311 +12,311 @@ |
||
12 | 12 | */ |
13 | 13 | class EEM_Status extends EEM_Base |
14 | 14 | { |
15 | - protected static ?EEM_Status $_instance = null; |
|
15 | + protected static ?EEM_Status $_instance = null; |
|
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * @param string|null $timezone |
|
20 | - * @throws EE_Error |
|
21 | - */ |
|
22 | - protected function __construct(?string $timezone = '') |
|
23 | - { |
|
24 | - $this->singular_item = esc_html__('Status', 'event_espresso'); |
|
25 | - $this->plural_item = esc_html__('Stati', 'event_espresso'); |
|
26 | - $this->_tables = [ |
|
27 | - 'StatusTable' => new EE_Primary_Table('esp_status', 'STS_ID'), |
|
28 | - ]; |
|
29 | - $this->_fields = [ |
|
30 | - 'StatusTable' => [ |
|
31 | - 'STS_ID' => new EE_Primary_Key_String_Field('STS_ID', esc_html__('Status ID', 'event_espresso')), |
|
32 | - 'STS_code' => new EE_Plain_Text_Field( |
|
33 | - 'STS_code', |
|
34 | - esc_html__('Status Code', 'event_espresso'), |
|
35 | - false, |
|
36 | - '' |
|
37 | - ), |
|
38 | - 'STS_type' => new EE_Enum_Text_Field( |
|
39 | - 'STS_type', |
|
40 | - esc_html__("Type", "event_espresso"), |
|
41 | - false, |
|
42 | - 'event', |
|
43 | - [ |
|
44 | - 'event' => esc_html__("Event", "event_espresso"),// deprecated |
|
45 | - 'registration' => esc_html__("Registration", "event_espresso"), |
|
46 | - 'transaction' => esc_html__("Transaction", "event_espresso"), |
|
47 | - 'payment' => esc_html__("Payment", "event_espresso"), |
|
48 | - 'email' => esc_html__("Email", "event_espresso"), |
|
49 | - 'message' => esc_html__("Message", "event_espresso"), |
|
50 | - ] |
|
51 | - ), |
|
52 | - 'STS_can_edit' => new EE_Boolean_Field( |
|
53 | - 'STS_can_edit', |
|
54 | - esc_html__('Editable?', 'event_espresso'), |
|
55 | - false, |
|
56 | - false |
|
57 | - ), |
|
58 | - 'STS_desc' => new EE_Simple_HTML_Field( |
|
59 | - 'STS_desc', |
|
60 | - esc_html__("Description", "event_espresso"), |
|
61 | - false, |
|
62 | - '' |
|
63 | - ), |
|
64 | - 'STS_open' => new EE_Boolean_Field('STS_open', esc_html__("Open?", "event_espresso"), false, false), |
|
65 | - ], |
|
66 | - ]; |
|
67 | - $this->_model_relations = [ |
|
68 | - 'Registration' => new EE_Has_Many_Relation(), |
|
69 | - 'Transaction' => new EE_Has_Many_Relation(), |
|
70 | - 'Payment' => new EE_Has_Many_Relation(), |
|
71 | - ]; |
|
72 | - // this model is generally available for reading |
|
73 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
18 | + /** |
|
19 | + * @param string|null $timezone |
|
20 | + * @throws EE_Error |
|
21 | + */ |
|
22 | + protected function __construct(?string $timezone = '') |
|
23 | + { |
|
24 | + $this->singular_item = esc_html__('Status', 'event_espresso'); |
|
25 | + $this->plural_item = esc_html__('Stati', 'event_espresso'); |
|
26 | + $this->_tables = [ |
|
27 | + 'StatusTable' => new EE_Primary_Table('esp_status', 'STS_ID'), |
|
28 | + ]; |
|
29 | + $this->_fields = [ |
|
30 | + 'StatusTable' => [ |
|
31 | + 'STS_ID' => new EE_Primary_Key_String_Field('STS_ID', esc_html__('Status ID', 'event_espresso')), |
|
32 | + 'STS_code' => new EE_Plain_Text_Field( |
|
33 | + 'STS_code', |
|
34 | + esc_html__('Status Code', 'event_espresso'), |
|
35 | + false, |
|
36 | + '' |
|
37 | + ), |
|
38 | + 'STS_type' => new EE_Enum_Text_Field( |
|
39 | + 'STS_type', |
|
40 | + esc_html__("Type", "event_espresso"), |
|
41 | + false, |
|
42 | + 'event', |
|
43 | + [ |
|
44 | + 'event' => esc_html__("Event", "event_espresso"),// deprecated |
|
45 | + 'registration' => esc_html__("Registration", "event_espresso"), |
|
46 | + 'transaction' => esc_html__("Transaction", "event_espresso"), |
|
47 | + 'payment' => esc_html__("Payment", "event_espresso"), |
|
48 | + 'email' => esc_html__("Email", "event_espresso"), |
|
49 | + 'message' => esc_html__("Message", "event_espresso"), |
|
50 | + ] |
|
51 | + ), |
|
52 | + 'STS_can_edit' => new EE_Boolean_Field( |
|
53 | + 'STS_can_edit', |
|
54 | + esc_html__('Editable?', 'event_espresso'), |
|
55 | + false, |
|
56 | + false |
|
57 | + ), |
|
58 | + 'STS_desc' => new EE_Simple_HTML_Field( |
|
59 | + 'STS_desc', |
|
60 | + esc_html__("Description", "event_espresso"), |
|
61 | + false, |
|
62 | + '' |
|
63 | + ), |
|
64 | + 'STS_open' => new EE_Boolean_Field('STS_open', esc_html__("Open?", "event_espresso"), false, false), |
|
65 | + ], |
|
66 | + ]; |
|
67 | + $this->_model_relations = [ |
|
68 | + 'Registration' => new EE_Has_Many_Relation(), |
|
69 | + 'Transaction' => new EE_Has_Many_Relation(), |
|
70 | + 'Payment' => new EE_Has_Many_Relation(), |
|
71 | + ]; |
|
72 | + // this model is generally available for reading |
|
73 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
74 | 74 | |
75 | - parent::__construct($timezone); |
|
76 | - } |
|
75 | + parent::__construct($timezone); |
|
76 | + } |
|
77 | 77 | |
78 | 78 | |
79 | - /** |
|
80 | - * This method provides the localized singular or plural string for a given status id |
|
81 | - * |
|
82 | - * @param array $statuses This should be an array of statuses in the format array( $status_id, $status_code ). |
|
83 | - * That way if there isn't a translation in the index we'll return the default code. |
|
84 | - * @param boolean $plural Whether to return plural string or not. Note, nearly all of the plural strings are the |
|
85 | - * same as the singular (in English), however, this may NOT be the case with other |
|
86 | - * languages |
|
87 | - * @param string $schema This can be either 'upper', 'lower', or 'sentence'. Basically indicates how we want |
|
88 | - * the status string returned ( UPPER, lower, Sentence) |
|
89 | - * @return array an array of translated strings for the incoming status id. |
|
90 | - * @throws EE_Error |
|
91 | - */ |
|
92 | - public function localized_status($statuses, $plural = false, $schema = 'upper') |
|
93 | - { |
|
94 | - // note these are all in lower case because ucwords() on upper case will NOT convert. |
|
95 | - $translation_array = [ |
|
96 | - RegStatus::PENDING_PAYMENT => [ |
|
97 | - esc_html__('pending payment', 'event_espresso'), // singular |
|
98 | - esc_html__('pending payments', 'event_espresso'), // plural |
|
99 | - ], |
|
100 | - RegStatus::APPROVED => [ |
|
101 | - esc_html__('approved', 'event_espresso'), // singular |
|
102 | - esc_html__('approved', 'event_espresso'), // plural |
|
103 | - ], |
|
104 | - RegStatus::AWAITING_REVIEW => [ |
|
105 | - esc_html__('not approved / awaiting review', 'event_espresso'), |
|
106 | - esc_html__('not approved / awaiting review', 'event_espresso'), |
|
107 | - ], |
|
108 | - RegStatus::CANCELLED => [ |
|
109 | - esc_html__('cancelled', 'event_espresso'), |
|
110 | - esc_html__('cancelled', 'event_espresso'), |
|
111 | - ], |
|
112 | - RegStatus::INCOMPLETE => [ |
|
113 | - esc_html__('incomplete', 'event_espresso'), |
|
114 | - esc_html__('incomplete', 'event_espresso'), |
|
115 | - ], |
|
116 | - RegStatus::DECLINED => [ |
|
117 | - esc_html__('declined', 'event_espresso'), |
|
118 | - esc_html__('declined', 'event_espresso'), |
|
119 | - ], |
|
120 | - RegStatus::WAIT_LIST => [ |
|
121 | - esc_html__('wait list', 'event_espresso'), |
|
122 | - esc_html__('wait list', 'event_espresso'), |
|
123 | - ], |
|
124 | - EEM_Transaction::overpaid_status_code => [ |
|
125 | - esc_html__('overpaid', 'event_espresso'), |
|
126 | - esc_html__('overpaid', 'event_espresso'), |
|
127 | - ], |
|
128 | - EEM_Transaction::complete_status_code => [ |
|
129 | - esc_html__('complete', 'event_espresso'), |
|
130 | - esc_html__('complete', 'event_espresso'), |
|
131 | - ], |
|
132 | - EEM_Transaction::incomplete_status_code => [ |
|
133 | - esc_html__('incomplete', 'event_espresso'), |
|
134 | - esc_html__('incomplete', 'event_espresso'), |
|
135 | - ], |
|
136 | - EEM_Transaction::failed_status_code => [ |
|
137 | - esc_html__('failed', 'event_espresso'), |
|
138 | - esc_html__('failed', 'event_espresso'), |
|
139 | - ], |
|
140 | - EEM_Transaction::abandoned_status_code => [ |
|
141 | - esc_html__('abandoned', 'event_espresso'), |
|
142 | - esc_html__('abandoned', 'event_espresso'), |
|
143 | - ], |
|
144 | - EEM_Payment::status_id_approved => [ |
|
145 | - esc_html__('accepted', 'event_espresso'), |
|
146 | - esc_html__('accepted', 'event_espresso'), |
|
147 | - ], |
|
148 | - EEM_Payment::status_id_pending => [ |
|
149 | - esc_html__('pending', 'event_espresso'), |
|
150 | - esc_html__('pending', 'event_espresso'), |
|
151 | - ], |
|
152 | - EEM_Payment::status_id_cancelled => [ |
|
153 | - esc_html__('cancelled', 'event_espresso'), |
|
154 | - esc_html__('cancelled', 'event_espresso'), |
|
155 | - ], |
|
156 | - EEM_Payment::status_id_declined => [ |
|
157 | - esc_html__('declined', 'event_espresso'), |
|
158 | - esc_html__('declined', 'event_espresso'), |
|
159 | - ], |
|
160 | - EEM_Payment::status_id_failed => [ |
|
161 | - esc_html__('failed', 'event_espresso'), |
|
162 | - esc_html__('failed', 'event_espresso'), |
|
163 | - ], |
|
164 | - // following statuses are NOT part of the EEM_Status but to keep things centralized we include in here. |
|
165 | - EEM_Event::sold_out => [ |
|
166 | - esc_html__('sold out', 'event_espresso'), |
|
167 | - esc_html__('sold out', 'event_espresso'), |
|
168 | - ], |
|
169 | - EEM_Event::postponed => [ |
|
170 | - esc_html__('postponed', 'event_espresso'), |
|
171 | - esc_html__('Postponed', 'event_espresso'), |
|
172 | - ], |
|
173 | - EEM_Event::cancelled => [ |
|
174 | - esc_html__('cancelled', 'event_espresso'), |
|
175 | - esc_html__('cancelled', 'event_espresso'), |
|
176 | - ], |
|
177 | - EE_Ticket::archived => [ |
|
178 | - esc_html__('archived', 'event_espresso'), |
|
179 | - esc_html__('archived', 'event_espresso'), |
|
180 | - ], |
|
181 | - EE_Ticket::expired => [ |
|
182 | - esc_html__('expired', 'event_espresso'), |
|
183 | - esc_html__('expired', 'event_espresso'), |
|
184 | - ], |
|
185 | - EE_Ticket::sold_out => [ |
|
186 | - esc_html__('sold out', 'event_espresso'), |
|
187 | - esc_html__('sold out', 'event_espresso'), |
|
188 | - ], |
|
189 | - EE_Ticket::pending => [ |
|
190 | - esc_html__('upcoming', 'event_espresso'), |
|
191 | - esc_html__('upcoming', 'event_espresso'), |
|
192 | - ], |
|
193 | - EE_Ticket::onsale => [ |
|
194 | - esc_html__('on sale', 'event_espresso'), |
|
195 | - esc_html__('on sale', 'event_espresso'), |
|
196 | - ], |
|
197 | - EE_Datetime::cancelled => [ |
|
198 | - esc_html__('cancelled', 'event_espresso'), |
|
199 | - esc_html__('cancelled', 'event_espresso'), |
|
200 | - ], |
|
201 | - EE_Datetime::sold_out => [ |
|
202 | - esc_html__('sold out', 'event_espresso'), |
|
203 | - esc_html__('sold out', 'event_espresso'), |
|
204 | - ], |
|
205 | - EE_Datetime::expired => [ |
|
206 | - esc_html__('expired', 'event_espresso'), |
|
207 | - esc_html__('expired', 'event_espresso'), |
|
208 | - ], |
|
209 | - EE_Datetime::inactive => [ |
|
210 | - esc_html__('inactive', 'event_espresso'), |
|
211 | - esc_html__('inactive', 'event_espresso'), |
|
212 | - ], |
|
213 | - EE_Datetime::upcoming => [ |
|
214 | - esc_html__('upcoming', 'event_espresso'), |
|
215 | - esc_html__('upcoming', 'event_espresso'), |
|
216 | - ], |
|
217 | - EE_Datetime::active => [ |
|
218 | - esc_html__('active', 'event_espresso'), |
|
219 | - esc_html__('active', 'event_espresso'), |
|
220 | - ], |
|
221 | - EE_Datetime::postponed => [ |
|
222 | - esc_html__('postponed', 'event_espresso'), |
|
223 | - esc_html__('postponed', 'event_espresso'), |
|
224 | - ], |
|
225 | - // messages related |
|
226 | - EEM_Message::status_sent => [ |
|
227 | - esc_html__('sent', 'event_espresso'), |
|
228 | - esc_html__('sent', 'event_espresso'), |
|
229 | - ], |
|
230 | - EEM_Message::status_idle => [ |
|
231 | - esc_html__('queued for sending', 'event_espresso'), |
|
232 | - esc_html__('queued for sending', 'event_espresso'), |
|
233 | - ], |
|
234 | - EEM_Message::status_failed => [ |
|
235 | - esc_html__('failed', 'event_espresso'), |
|
236 | - esc_html__('failed', 'event_espresso'), |
|
237 | - ], |
|
238 | - EEM_Message::status_debug_only => [ |
|
239 | - esc_html__('debug only', 'event_espresso'), |
|
240 | - esc_html__('debug only', 'event_espresso'), |
|
241 | - ], |
|
242 | - EEM_Message::status_messenger_executing => [ |
|
243 | - esc_html__('messenger is executing', 'event_espresso'), |
|
244 | - esc_html__('messenger is executing', 'event_espresso'), |
|
245 | - ], |
|
246 | - EEM_Message::status_resend => [ |
|
247 | - esc_html__('queued for resending', 'event_espresso'), |
|
248 | - esc_html__('queued for resending', 'event_espresso'), |
|
249 | - ], |
|
250 | - EEM_Message::status_incomplete => [ |
|
251 | - esc_html__('queued for generating', 'event_espresso'), |
|
252 | - esc_html__('queued for generating', 'event_espresso'), |
|
253 | - ], |
|
254 | - EEM_Message::status_retry => [ |
|
255 | - esc_html__('failed sending, can be retried', 'event_espresso'), |
|
256 | - esc_html__('failed sending, can be retried', 'event_espresso'), |
|
257 | - ], |
|
258 | - EEM_CPT_Base::post_status_publish => [ |
|
259 | - esc_html__('published', 'event_espresso'), |
|
260 | - esc_html__('published', 'event_espresso'), |
|
261 | - ], |
|
262 | - EEM_CPT_Base::post_status_future => [ |
|
263 | - esc_html__('scheduled', 'event_espresso'), |
|
264 | - esc_html__('scheduled', 'event_espresso'), |
|
265 | - ], |
|
266 | - EEM_CPT_Base::post_status_draft => [ |
|
267 | - esc_html__('draft', 'event_espresso'), |
|
268 | - esc_html__('draft', 'event_espresso'), |
|
269 | - ], |
|
270 | - EEM_CPT_Base::post_status_pending => [ |
|
271 | - esc_html__('pending', 'event_espresso'), |
|
272 | - esc_html__('pending', 'event_espresso'), |
|
273 | - ], |
|
274 | - EEM_CPT_Base::post_status_private => [ |
|
275 | - esc_html__('private', 'event_espresso'), |
|
276 | - esc_html__('private', 'event_espresso'), |
|
277 | - ], |
|
278 | - EEM_CPT_Base::post_status_trashed => [ |
|
279 | - esc_html__('trashed', 'event_espresso'), |
|
280 | - esc_html__('trashed', 'event_espresso'), |
|
281 | - ], |
|
282 | - ]; |
|
79 | + /** |
|
80 | + * This method provides the localized singular or plural string for a given status id |
|
81 | + * |
|
82 | + * @param array $statuses This should be an array of statuses in the format array( $status_id, $status_code ). |
|
83 | + * That way if there isn't a translation in the index we'll return the default code. |
|
84 | + * @param boolean $plural Whether to return plural string or not. Note, nearly all of the plural strings are the |
|
85 | + * same as the singular (in English), however, this may NOT be the case with other |
|
86 | + * languages |
|
87 | + * @param string $schema This can be either 'upper', 'lower', or 'sentence'. Basically indicates how we want |
|
88 | + * the status string returned ( UPPER, lower, Sentence) |
|
89 | + * @return array an array of translated strings for the incoming status id. |
|
90 | + * @throws EE_Error |
|
91 | + */ |
|
92 | + public function localized_status($statuses, $plural = false, $schema = 'upper') |
|
93 | + { |
|
94 | + // note these are all in lower case because ucwords() on upper case will NOT convert. |
|
95 | + $translation_array = [ |
|
96 | + RegStatus::PENDING_PAYMENT => [ |
|
97 | + esc_html__('pending payment', 'event_espresso'), // singular |
|
98 | + esc_html__('pending payments', 'event_espresso'), // plural |
|
99 | + ], |
|
100 | + RegStatus::APPROVED => [ |
|
101 | + esc_html__('approved', 'event_espresso'), // singular |
|
102 | + esc_html__('approved', 'event_espresso'), // plural |
|
103 | + ], |
|
104 | + RegStatus::AWAITING_REVIEW => [ |
|
105 | + esc_html__('not approved / awaiting review', 'event_espresso'), |
|
106 | + esc_html__('not approved / awaiting review', 'event_espresso'), |
|
107 | + ], |
|
108 | + RegStatus::CANCELLED => [ |
|
109 | + esc_html__('cancelled', 'event_espresso'), |
|
110 | + esc_html__('cancelled', 'event_espresso'), |
|
111 | + ], |
|
112 | + RegStatus::INCOMPLETE => [ |
|
113 | + esc_html__('incomplete', 'event_espresso'), |
|
114 | + esc_html__('incomplete', 'event_espresso'), |
|
115 | + ], |
|
116 | + RegStatus::DECLINED => [ |
|
117 | + esc_html__('declined', 'event_espresso'), |
|
118 | + esc_html__('declined', 'event_espresso'), |
|
119 | + ], |
|
120 | + RegStatus::WAIT_LIST => [ |
|
121 | + esc_html__('wait list', 'event_espresso'), |
|
122 | + esc_html__('wait list', 'event_espresso'), |
|
123 | + ], |
|
124 | + EEM_Transaction::overpaid_status_code => [ |
|
125 | + esc_html__('overpaid', 'event_espresso'), |
|
126 | + esc_html__('overpaid', 'event_espresso'), |
|
127 | + ], |
|
128 | + EEM_Transaction::complete_status_code => [ |
|
129 | + esc_html__('complete', 'event_espresso'), |
|
130 | + esc_html__('complete', 'event_espresso'), |
|
131 | + ], |
|
132 | + EEM_Transaction::incomplete_status_code => [ |
|
133 | + esc_html__('incomplete', 'event_espresso'), |
|
134 | + esc_html__('incomplete', 'event_espresso'), |
|
135 | + ], |
|
136 | + EEM_Transaction::failed_status_code => [ |
|
137 | + esc_html__('failed', 'event_espresso'), |
|
138 | + esc_html__('failed', 'event_espresso'), |
|
139 | + ], |
|
140 | + EEM_Transaction::abandoned_status_code => [ |
|
141 | + esc_html__('abandoned', 'event_espresso'), |
|
142 | + esc_html__('abandoned', 'event_espresso'), |
|
143 | + ], |
|
144 | + EEM_Payment::status_id_approved => [ |
|
145 | + esc_html__('accepted', 'event_espresso'), |
|
146 | + esc_html__('accepted', 'event_espresso'), |
|
147 | + ], |
|
148 | + EEM_Payment::status_id_pending => [ |
|
149 | + esc_html__('pending', 'event_espresso'), |
|
150 | + esc_html__('pending', 'event_espresso'), |
|
151 | + ], |
|
152 | + EEM_Payment::status_id_cancelled => [ |
|
153 | + esc_html__('cancelled', 'event_espresso'), |
|
154 | + esc_html__('cancelled', 'event_espresso'), |
|
155 | + ], |
|
156 | + EEM_Payment::status_id_declined => [ |
|
157 | + esc_html__('declined', 'event_espresso'), |
|
158 | + esc_html__('declined', 'event_espresso'), |
|
159 | + ], |
|
160 | + EEM_Payment::status_id_failed => [ |
|
161 | + esc_html__('failed', 'event_espresso'), |
|
162 | + esc_html__('failed', 'event_espresso'), |
|
163 | + ], |
|
164 | + // following statuses are NOT part of the EEM_Status but to keep things centralized we include in here. |
|
165 | + EEM_Event::sold_out => [ |
|
166 | + esc_html__('sold out', 'event_espresso'), |
|
167 | + esc_html__('sold out', 'event_espresso'), |
|
168 | + ], |
|
169 | + EEM_Event::postponed => [ |
|
170 | + esc_html__('postponed', 'event_espresso'), |
|
171 | + esc_html__('Postponed', 'event_espresso'), |
|
172 | + ], |
|
173 | + EEM_Event::cancelled => [ |
|
174 | + esc_html__('cancelled', 'event_espresso'), |
|
175 | + esc_html__('cancelled', 'event_espresso'), |
|
176 | + ], |
|
177 | + EE_Ticket::archived => [ |
|
178 | + esc_html__('archived', 'event_espresso'), |
|
179 | + esc_html__('archived', 'event_espresso'), |
|
180 | + ], |
|
181 | + EE_Ticket::expired => [ |
|
182 | + esc_html__('expired', 'event_espresso'), |
|
183 | + esc_html__('expired', 'event_espresso'), |
|
184 | + ], |
|
185 | + EE_Ticket::sold_out => [ |
|
186 | + esc_html__('sold out', 'event_espresso'), |
|
187 | + esc_html__('sold out', 'event_espresso'), |
|
188 | + ], |
|
189 | + EE_Ticket::pending => [ |
|
190 | + esc_html__('upcoming', 'event_espresso'), |
|
191 | + esc_html__('upcoming', 'event_espresso'), |
|
192 | + ], |
|
193 | + EE_Ticket::onsale => [ |
|
194 | + esc_html__('on sale', 'event_espresso'), |
|
195 | + esc_html__('on sale', 'event_espresso'), |
|
196 | + ], |
|
197 | + EE_Datetime::cancelled => [ |
|
198 | + esc_html__('cancelled', 'event_espresso'), |
|
199 | + esc_html__('cancelled', 'event_espresso'), |
|
200 | + ], |
|
201 | + EE_Datetime::sold_out => [ |
|
202 | + esc_html__('sold out', 'event_espresso'), |
|
203 | + esc_html__('sold out', 'event_espresso'), |
|
204 | + ], |
|
205 | + EE_Datetime::expired => [ |
|
206 | + esc_html__('expired', 'event_espresso'), |
|
207 | + esc_html__('expired', 'event_espresso'), |
|
208 | + ], |
|
209 | + EE_Datetime::inactive => [ |
|
210 | + esc_html__('inactive', 'event_espresso'), |
|
211 | + esc_html__('inactive', 'event_espresso'), |
|
212 | + ], |
|
213 | + EE_Datetime::upcoming => [ |
|
214 | + esc_html__('upcoming', 'event_espresso'), |
|
215 | + esc_html__('upcoming', 'event_espresso'), |
|
216 | + ], |
|
217 | + EE_Datetime::active => [ |
|
218 | + esc_html__('active', 'event_espresso'), |
|
219 | + esc_html__('active', 'event_espresso'), |
|
220 | + ], |
|
221 | + EE_Datetime::postponed => [ |
|
222 | + esc_html__('postponed', 'event_espresso'), |
|
223 | + esc_html__('postponed', 'event_espresso'), |
|
224 | + ], |
|
225 | + // messages related |
|
226 | + EEM_Message::status_sent => [ |
|
227 | + esc_html__('sent', 'event_espresso'), |
|
228 | + esc_html__('sent', 'event_espresso'), |
|
229 | + ], |
|
230 | + EEM_Message::status_idle => [ |
|
231 | + esc_html__('queued for sending', 'event_espresso'), |
|
232 | + esc_html__('queued for sending', 'event_espresso'), |
|
233 | + ], |
|
234 | + EEM_Message::status_failed => [ |
|
235 | + esc_html__('failed', 'event_espresso'), |
|
236 | + esc_html__('failed', 'event_espresso'), |
|
237 | + ], |
|
238 | + EEM_Message::status_debug_only => [ |
|
239 | + esc_html__('debug only', 'event_espresso'), |
|
240 | + esc_html__('debug only', 'event_espresso'), |
|
241 | + ], |
|
242 | + EEM_Message::status_messenger_executing => [ |
|
243 | + esc_html__('messenger is executing', 'event_espresso'), |
|
244 | + esc_html__('messenger is executing', 'event_espresso'), |
|
245 | + ], |
|
246 | + EEM_Message::status_resend => [ |
|
247 | + esc_html__('queued for resending', 'event_espresso'), |
|
248 | + esc_html__('queued for resending', 'event_espresso'), |
|
249 | + ], |
|
250 | + EEM_Message::status_incomplete => [ |
|
251 | + esc_html__('queued for generating', 'event_espresso'), |
|
252 | + esc_html__('queued for generating', 'event_espresso'), |
|
253 | + ], |
|
254 | + EEM_Message::status_retry => [ |
|
255 | + esc_html__('failed sending, can be retried', 'event_espresso'), |
|
256 | + esc_html__('failed sending, can be retried', 'event_espresso'), |
|
257 | + ], |
|
258 | + EEM_CPT_Base::post_status_publish => [ |
|
259 | + esc_html__('published', 'event_espresso'), |
|
260 | + esc_html__('published', 'event_espresso'), |
|
261 | + ], |
|
262 | + EEM_CPT_Base::post_status_future => [ |
|
263 | + esc_html__('scheduled', 'event_espresso'), |
|
264 | + esc_html__('scheduled', 'event_espresso'), |
|
265 | + ], |
|
266 | + EEM_CPT_Base::post_status_draft => [ |
|
267 | + esc_html__('draft', 'event_espresso'), |
|
268 | + esc_html__('draft', 'event_espresso'), |
|
269 | + ], |
|
270 | + EEM_CPT_Base::post_status_pending => [ |
|
271 | + esc_html__('pending', 'event_espresso'), |
|
272 | + esc_html__('pending', 'event_espresso'), |
|
273 | + ], |
|
274 | + EEM_CPT_Base::post_status_private => [ |
|
275 | + esc_html__('private', 'event_espresso'), |
|
276 | + esc_html__('private', 'event_espresso'), |
|
277 | + ], |
|
278 | + EEM_CPT_Base::post_status_trashed => [ |
|
279 | + esc_html__('trashed', 'event_espresso'), |
|
280 | + esc_html__('trashed', 'event_espresso'), |
|
281 | + ], |
|
282 | + ]; |
|
283 | 283 | |
284 | - $translation_array = apply_filters('FHEE__EEM_Status__localized_status__translation_array', $translation_array); |
|
284 | + $translation_array = apply_filters('FHEE__EEM_Status__localized_status__translation_array', $translation_array); |
|
285 | 285 | |
286 | - if (! is_array($statuses)) { |
|
287 | - throw new EE_Error( |
|
288 | - esc_html__( |
|
289 | - 'The incoming statuses argument must be an array with keys as the $status_id and values as the $status_code', |
|
290 | - 'event_espresso' |
|
291 | - ) |
|
292 | - ); |
|
293 | - } |
|
286 | + if (! is_array($statuses)) { |
|
287 | + throw new EE_Error( |
|
288 | + esc_html__( |
|
289 | + 'The incoming statuses argument must be an array with keys as the $status_id and values as the $status_code', |
|
290 | + 'event_espresso' |
|
291 | + ) |
|
292 | + ); |
|
293 | + } |
|
294 | 294 | |
295 | - $translation = []; |
|
295 | + $translation = []; |
|
296 | 296 | |
297 | - foreach ($statuses as $id => $code) { |
|
298 | - if (isset($translation_array[ $id ])) { |
|
299 | - $translation[ $id ] = $plural ? $translation_array[ $id ][1] : $translation_array[ $id ][0]; |
|
300 | - } else { |
|
301 | - $translation[ $id ] = $code; |
|
302 | - } |
|
297 | + foreach ($statuses as $id => $code) { |
|
298 | + if (isset($translation_array[ $id ])) { |
|
299 | + $translation[ $id ] = $plural ? $translation_array[ $id ][1] : $translation_array[ $id ][0]; |
|
300 | + } else { |
|
301 | + $translation[ $id ] = $code; |
|
302 | + } |
|
303 | 303 | |
304 | - // schema |
|
305 | - switch ($schema) { |
|
306 | - case 'lower': |
|
307 | - $translation[ $id ] = strtolower( |
|
308 | - $translation[ $id ] |
|
309 | - ); // even though these start in lower case, this will catch any statuses added via filter. |
|
310 | - break; |
|
311 | - case 'sentence': |
|
312 | - $translation[ $id ] = ucwords($translation[ $id ]); |
|
313 | - break; |
|
314 | - case 'upper': |
|
315 | - $translation[ $id ] = strtoupper($translation[ $id ]); |
|
316 | - break; |
|
317 | - } |
|
318 | - } |
|
304 | + // schema |
|
305 | + switch ($schema) { |
|
306 | + case 'lower': |
|
307 | + $translation[ $id ] = strtolower( |
|
308 | + $translation[ $id ] |
|
309 | + ); // even though these start in lower case, this will catch any statuses added via filter. |
|
310 | + break; |
|
311 | + case 'sentence': |
|
312 | + $translation[ $id ] = ucwords($translation[ $id ]); |
|
313 | + break; |
|
314 | + case 'upper': |
|
315 | + $translation[ $id ] = strtoupper($translation[ $id ]); |
|
316 | + break; |
|
317 | + } |
|
318 | + } |
|
319 | 319 | |
320 | - return $translation; |
|
321 | - } |
|
320 | + return $translation; |
|
321 | + } |
|
322 | 322 | } |
@@ -9,204 +9,204 @@ |
||
9 | 9 | */ |
10 | 10 | class EEM_Price_Type extends EEM_Soft_Delete_Base |
11 | 11 | { |
12 | - /** |
|
13 | - * constants for price base types. In the DB, we decided to store the price base type |
|
14 | - * as an integer. So, to avoid just having magic numbers everwhere (eg, querying for |
|
15 | - * all price types with PBT_ID = 2), we define these constants, to make code more understandable. |
|
16 | - * So, as an example, to query for all price types that are a tax, we'd do |
|
17 | - * EEM_PRice_Type::instance()->get_all(array(array('PBT_ID'=>EEM_Price_Type::base_type_tax))) |
|
18 | - * instead of |
|
19 | - * EEM_Price_Type::instance()->get_all(array(array('PBT_ID'=>2))) |
|
20 | - * Although the 2nd is shorter, it's much less obvious what it's doing. Also, should these magic IDs ever |
|
21 | - * change, we can continue to use the constant, by simply change its value. |
|
22 | - */ |
|
23 | - const base_type_base_price = 1; |
|
24 | - |
|
25 | - const base_type_discount = 2; |
|
26 | - |
|
27 | - const base_type_surcharge = 3; |
|
28 | - |
|
29 | - const base_type_tax = 4; |
|
30 | - |
|
31 | - |
|
32 | - protected static ?EEM_Price_Type $_instance = null; |
|
33 | - |
|
34 | - /** |
|
35 | - * keys are one of the base_type_* constants above, and values are the text-representation of the base type. |
|
36 | - * |
|
37 | - * @var string[] |
|
38 | - */ |
|
39 | - public array $base_types; |
|
40 | - |
|
41 | - /** |
|
42 | - * An array of the price type objects |
|
43 | - */ |
|
44 | - public ?array $type = null; |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * return an array of Base types. Keys are INTs which are used in the database, |
|
49 | - * values are text-representations of the base type. |
|
50 | - * |
|
51 | - * @return array |
|
52 | - */ |
|
53 | - public function get_base_types(): array |
|
54 | - { |
|
55 | - return $this->base_types; |
|
56 | - } |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * Gets the name of the base |
|
61 | - * |
|
62 | - * @param int $base_type_int |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - public function get_base_type_name(int $base_type_int): string |
|
66 | - { |
|
67 | - return $this->base_types[ $base_type_int ]; |
|
68 | - } |
|
69 | - |
|
70 | - |
|
71 | - /** |
|
72 | - * private constructor to prevent direct creation |
|
73 | - * |
|
74 | - * @param string|null $timezone |
|
75 | - * @return void |
|
76 | - * @throws EE_Error |
|
77 | - */ |
|
78 | - protected function __construct(?string $timezone = '') |
|
79 | - { |
|
80 | - $this->base_types = [ |
|
81 | - EEM_Price_Type::base_type_base_price => esc_html__('Price', 'event_espresso'), |
|
82 | - EEM_Price_Type::base_type_discount => esc_html__('Discount', 'event_espresso'), |
|
83 | - EEM_Price_Type::base_type_surcharge => esc_html__('Surcharge', 'event_espresso'), |
|
84 | - EEM_Price_Type::base_type_tax => esc_html__('Tax', 'event_espresso'), |
|
85 | - ]; |
|
86 | - $this->singular_item = esc_html__('Price Type', 'event_espresso'); |
|
87 | - $this->plural_item = esc_html__('Price Types', 'event_espresso'); |
|
88 | - |
|
89 | - $this->_tables = [ |
|
90 | - 'Price_Type' => new EE_Primary_Table('esp_price_type', 'PRT_ID'), |
|
91 | - ]; |
|
92 | - $this->_fields = [ |
|
93 | - 'Price_Type' => [ |
|
94 | - 'PRT_ID' => new EE_Primary_Key_Int_Field( |
|
95 | - 'PRT_ID', |
|
96 | - esc_html__('Price Type ID', 'event_espresso') |
|
97 | - ), |
|
98 | - 'PRT_name' => new EE_Plain_Text_Field( |
|
99 | - 'PRT_name', |
|
100 | - esc_html__('Price Type Name', 'event_espresso'), |
|
101 | - false, |
|
102 | - '' |
|
103 | - ), |
|
104 | - 'PBT_ID' => new EE_Enum_Integer_Field( |
|
105 | - 'PBT_ID', |
|
106 | - esc_html__( |
|
107 | - 'Price Base type ID, 1 = Price , 2 = Discount , 3 = Surcharge , 4 = Tax', |
|
108 | - 'event_espresso' |
|
109 | - ), |
|
110 | - false, |
|
111 | - EEM_Price_Type::base_type_base_price, |
|
112 | - $this->base_types |
|
113 | - ), |
|
114 | - 'PRT_is_percent' => new EE_Boolean_Field( |
|
115 | - 'PRT_is_percent', |
|
116 | - esc_html__( |
|
117 | - 'Flag indicating price is a percentage', |
|
118 | - 'event_espresso' |
|
119 | - ), |
|
120 | - false, |
|
121 | - false |
|
122 | - ), |
|
123 | - 'PRT_order' => new EE_Integer_Field( |
|
124 | - 'PRT_order', |
|
125 | - esc_html__( |
|
126 | - 'Order in which price should be applied. ', |
|
127 | - 'event_espresso' |
|
128 | - ), |
|
129 | - false, |
|
130 | - 0 |
|
131 | - ), |
|
132 | - 'PRT_deleted' => new EE_Trashed_Flag_Field( |
|
133 | - 'PRT_deleted', |
|
134 | - esc_html__( |
|
135 | - 'Flag indicating price type has been trashed', |
|
136 | - 'event_espresso' |
|
137 | - ), |
|
138 | - false, |
|
139 | - false |
|
140 | - ), |
|
141 | - 'PRT_wp_user' => new EE_WP_User_Field( |
|
142 | - 'PRT_wp_user', |
|
143 | - esc_html__('Price Type Creator ID', 'event_espresso'), |
|
144 | - false |
|
145 | - ), |
|
146 | - ], |
|
147 | - ]; |
|
148 | - $this->_model_relations = [ |
|
149 | - 'Price' => new EE_Has_Many_Relation(), |
|
150 | - 'WP_User' => new EE_Belongs_To_Relation(), |
|
151 | - ]; |
|
152 | - // this model is generally available for reading |
|
153 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
154 | - // all price types are "default" in terms of capability names |
|
155 | - $this->_caps_slug = 'default_price_types'; |
|
156 | - parent::__construct($timezone); |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * instantiate a new price type object with blank/empty properties |
|
162 | - * |
|
163 | - * @return EE_Price_Type |
|
164 | - * @throws EE_Error |
|
165 | - * @throws ReflectionException |
|
166 | - */ |
|
167 | - public function get_new_price_type(): EE_Price_Type |
|
168 | - { |
|
169 | - return EE_Price_Type::new_instance(); |
|
170 | - } |
|
171 | - |
|
172 | - |
|
173 | - /** |
|
174 | - * @param array $query_params |
|
175 | - * @param bool $allow_blocking if TRUE, matched objects will only be deleted if there is no related model info |
|
176 | - * that blocks it (ie, there' sno other data that depends on this data); |
|
177 | - * if false, deletes regardless of other objects which may depend on it. |
|
178 | - * Its generally advisable to always leave this as TRUE, |
|
179 | - * otherwise you could easily corrupt your DB |
|
180 | - * @return bool |
|
181 | - * @throws EE_Error |
|
182 | - * @throws ReflectionException |
|
183 | - */ |
|
184 | - public function delete_permanently($query_params = [], $allow_blocking = true) |
|
185 | - { |
|
186 | - $would_be_deleted_price_types = $this->get_all_deleted_and_undeleted($query_params); |
|
187 | - $would_be_deleted_price_type_ids = array_keys($would_be_deleted_price_types); |
|
188 | - |
|
189 | - $ID = $query_params[0][ $this->get_primary_key_field()->get_name() ]; |
|
190 | - |
|
191 | - // check if any prices use this price type |
|
192 | - $prc_query_params = [['PRT_ID' => ['IN', $would_be_deleted_price_type_ids]]]; |
|
193 | - if ($prices = $this->get_all_related($ID, 'Price', $prc_query_params)) { |
|
194 | - $prices_names_and_ids = []; |
|
195 | - foreach ($prices as $price) { |
|
196 | - /* @var $price EE_Price */ |
|
197 | - $prices_names_and_ids[] = $price->name() . "(" . $price->ID() . ")"; |
|
198 | - } |
|
199 | - $msg = sprintf( |
|
200 | - esc_html__( |
|
201 | - 'The Price Type(s) could not be deleted because there are existing Prices that currently use this Price Type. If you still wish to delete this Price Type, then either delete those Prices or change them to use other Price Types.The prices are: %s', |
|
202 | - 'event_espresso' |
|
203 | - ), |
|
204 | - implode(",", $prices_names_and_ids) |
|
205 | - ); |
|
206 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
207 | - return false; |
|
208 | - } |
|
209 | - |
|
210 | - return parent::delete_permanently($query_params); |
|
211 | - } |
|
12 | + /** |
|
13 | + * constants for price base types. In the DB, we decided to store the price base type |
|
14 | + * as an integer. So, to avoid just having magic numbers everwhere (eg, querying for |
|
15 | + * all price types with PBT_ID = 2), we define these constants, to make code more understandable. |
|
16 | + * So, as an example, to query for all price types that are a tax, we'd do |
|
17 | + * EEM_PRice_Type::instance()->get_all(array(array('PBT_ID'=>EEM_Price_Type::base_type_tax))) |
|
18 | + * instead of |
|
19 | + * EEM_Price_Type::instance()->get_all(array(array('PBT_ID'=>2))) |
|
20 | + * Although the 2nd is shorter, it's much less obvious what it's doing. Also, should these magic IDs ever |
|
21 | + * change, we can continue to use the constant, by simply change its value. |
|
22 | + */ |
|
23 | + const base_type_base_price = 1; |
|
24 | + |
|
25 | + const base_type_discount = 2; |
|
26 | + |
|
27 | + const base_type_surcharge = 3; |
|
28 | + |
|
29 | + const base_type_tax = 4; |
|
30 | + |
|
31 | + |
|
32 | + protected static ?EEM_Price_Type $_instance = null; |
|
33 | + |
|
34 | + /** |
|
35 | + * keys are one of the base_type_* constants above, and values are the text-representation of the base type. |
|
36 | + * |
|
37 | + * @var string[] |
|
38 | + */ |
|
39 | + public array $base_types; |
|
40 | + |
|
41 | + /** |
|
42 | + * An array of the price type objects |
|
43 | + */ |
|
44 | + public ?array $type = null; |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * return an array of Base types. Keys are INTs which are used in the database, |
|
49 | + * values are text-representations of the base type. |
|
50 | + * |
|
51 | + * @return array |
|
52 | + */ |
|
53 | + public function get_base_types(): array |
|
54 | + { |
|
55 | + return $this->base_types; |
|
56 | + } |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * Gets the name of the base |
|
61 | + * |
|
62 | + * @param int $base_type_int |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + public function get_base_type_name(int $base_type_int): string |
|
66 | + { |
|
67 | + return $this->base_types[ $base_type_int ]; |
|
68 | + } |
|
69 | + |
|
70 | + |
|
71 | + /** |
|
72 | + * private constructor to prevent direct creation |
|
73 | + * |
|
74 | + * @param string|null $timezone |
|
75 | + * @return void |
|
76 | + * @throws EE_Error |
|
77 | + */ |
|
78 | + protected function __construct(?string $timezone = '') |
|
79 | + { |
|
80 | + $this->base_types = [ |
|
81 | + EEM_Price_Type::base_type_base_price => esc_html__('Price', 'event_espresso'), |
|
82 | + EEM_Price_Type::base_type_discount => esc_html__('Discount', 'event_espresso'), |
|
83 | + EEM_Price_Type::base_type_surcharge => esc_html__('Surcharge', 'event_espresso'), |
|
84 | + EEM_Price_Type::base_type_tax => esc_html__('Tax', 'event_espresso'), |
|
85 | + ]; |
|
86 | + $this->singular_item = esc_html__('Price Type', 'event_espresso'); |
|
87 | + $this->plural_item = esc_html__('Price Types', 'event_espresso'); |
|
88 | + |
|
89 | + $this->_tables = [ |
|
90 | + 'Price_Type' => new EE_Primary_Table('esp_price_type', 'PRT_ID'), |
|
91 | + ]; |
|
92 | + $this->_fields = [ |
|
93 | + 'Price_Type' => [ |
|
94 | + 'PRT_ID' => new EE_Primary_Key_Int_Field( |
|
95 | + 'PRT_ID', |
|
96 | + esc_html__('Price Type ID', 'event_espresso') |
|
97 | + ), |
|
98 | + 'PRT_name' => new EE_Plain_Text_Field( |
|
99 | + 'PRT_name', |
|
100 | + esc_html__('Price Type Name', 'event_espresso'), |
|
101 | + false, |
|
102 | + '' |
|
103 | + ), |
|
104 | + 'PBT_ID' => new EE_Enum_Integer_Field( |
|
105 | + 'PBT_ID', |
|
106 | + esc_html__( |
|
107 | + 'Price Base type ID, 1 = Price , 2 = Discount , 3 = Surcharge , 4 = Tax', |
|
108 | + 'event_espresso' |
|
109 | + ), |
|
110 | + false, |
|
111 | + EEM_Price_Type::base_type_base_price, |
|
112 | + $this->base_types |
|
113 | + ), |
|
114 | + 'PRT_is_percent' => new EE_Boolean_Field( |
|
115 | + 'PRT_is_percent', |
|
116 | + esc_html__( |
|
117 | + 'Flag indicating price is a percentage', |
|
118 | + 'event_espresso' |
|
119 | + ), |
|
120 | + false, |
|
121 | + false |
|
122 | + ), |
|
123 | + 'PRT_order' => new EE_Integer_Field( |
|
124 | + 'PRT_order', |
|
125 | + esc_html__( |
|
126 | + 'Order in which price should be applied. ', |
|
127 | + 'event_espresso' |
|
128 | + ), |
|
129 | + false, |
|
130 | + 0 |
|
131 | + ), |
|
132 | + 'PRT_deleted' => new EE_Trashed_Flag_Field( |
|
133 | + 'PRT_deleted', |
|
134 | + esc_html__( |
|
135 | + 'Flag indicating price type has been trashed', |
|
136 | + 'event_espresso' |
|
137 | + ), |
|
138 | + false, |
|
139 | + false |
|
140 | + ), |
|
141 | + 'PRT_wp_user' => new EE_WP_User_Field( |
|
142 | + 'PRT_wp_user', |
|
143 | + esc_html__('Price Type Creator ID', 'event_espresso'), |
|
144 | + false |
|
145 | + ), |
|
146 | + ], |
|
147 | + ]; |
|
148 | + $this->_model_relations = [ |
|
149 | + 'Price' => new EE_Has_Many_Relation(), |
|
150 | + 'WP_User' => new EE_Belongs_To_Relation(), |
|
151 | + ]; |
|
152 | + // this model is generally available for reading |
|
153 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
154 | + // all price types are "default" in terms of capability names |
|
155 | + $this->_caps_slug = 'default_price_types'; |
|
156 | + parent::__construct($timezone); |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * instantiate a new price type object with blank/empty properties |
|
162 | + * |
|
163 | + * @return EE_Price_Type |
|
164 | + * @throws EE_Error |
|
165 | + * @throws ReflectionException |
|
166 | + */ |
|
167 | + public function get_new_price_type(): EE_Price_Type |
|
168 | + { |
|
169 | + return EE_Price_Type::new_instance(); |
|
170 | + } |
|
171 | + |
|
172 | + |
|
173 | + /** |
|
174 | + * @param array $query_params |
|
175 | + * @param bool $allow_blocking if TRUE, matched objects will only be deleted if there is no related model info |
|
176 | + * that blocks it (ie, there' sno other data that depends on this data); |
|
177 | + * if false, deletes regardless of other objects which may depend on it. |
|
178 | + * Its generally advisable to always leave this as TRUE, |
|
179 | + * otherwise you could easily corrupt your DB |
|
180 | + * @return bool |
|
181 | + * @throws EE_Error |
|
182 | + * @throws ReflectionException |
|
183 | + */ |
|
184 | + public function delete_permanently($query_params = [], $allow_blocking = true) |
|
185 | + { |
|
186 | + $would_be_deleted_price_types = $this->get_all_deleted_and_undeleted($query_params); |
|
187 | + $would_be_deleted_price_type_ids = array_keys($would_be_deleted_price_types); |
|
188 | + |
|
189 | + $ID = $query_params[0][ $this->get_primary_key_field()->get_name() ]; |
|
190 | + |
|
191 | + // check if any prices use this price type |
|
192 | + $prc_query_params = [['PRT_ID' => ['IN', $would_be_deleted_price_type_ids]]]; |
|
193 | + if ($prices = $this->get_all_related($ID, 'Price', $prc_query_params)) { |
|
194 | + $prices_names_and_ids = []; |
|
195 | + foreach ($prices as $price) { |
|
196 | + /* @var $price EE_Price */ |
|
197 | + $prices_names_and_ids[] = $price->name() . "(" . $price->ID() . ")"; |
|
198 | + } |
|
199 | + $msg = sprintf( |
|
200 | + esc_html__( |
|
201 | + 'The Price Type(s) could not be deleted because there are existing Prices that currently use this Price Type. If you still wish to delete this Price Type, then either delete those Prices or change them to use other Price Types.The prices are: %s', |
|
202 | + 'event_espresso' |
|
203 | + ), |
|
204 | + implode(",", $prices_names_and_ids) |
|
205 | + ); |
|
206 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
207 | + return false; |
|
208 | + } |
|
209 | + |
|
210 | + return parent::delete_permanently($query_params); |
|
211 | + } |
|
212 | 212 | } |
@@ -11,272 +11,272 @@ |
||
11 | 11 | */ |
12 | 12 | class EEM_Term extends EEM_Base |
13 | 13 | { |
14 | - protected static ?EEM_Term $_instance = null; |
|
14 | + protected static ?EEM_Term $_instance = null; |
|
15 | 15 | |
16 | 16 | |
17 | - /** |
|
18 | - * @param string|null $timezone |
|
19 | - * @throws EE_Error |
|
20 | - */ |
|
21 | - protected function __construct(?string $timezone = '') |
|
22 | - { |
|
23 | - $this->singular_item = esc_html__('Term', 'event_espresso'); |
|
24 | - $this->plural_item = esc_html__('Terms', 'event_espresso'); |
|
25 | - $this->_tables = [ |
|
26 | - 'Term' => new EE_Primary_Table('terms', 'term_id'), |
|
27 | - ]; |
|
28 | - $this->_fields = [ |
|
29 | - 'Term' => [ |
|
30 | - 'term_id' => new EE_Primary_Key_Int_Field('term_id', esc_html__('Term ID', 'event_espresso')), |
|
31 | - 'name' => new EE_Plain_Text_Field('name', esc_html__('Term Name', 'event_espresso'), false, ''), |
|
32 | - 'slug' => new EE_Slug_Field('slug', esc_html__('Term Slug', 'event_espresso'), false), |
|
33 | - 'term_group' => new EE_Integer_Field( |
|
34 | - 'term_group', |
|
35 | - esc_html__("Term Group", "event_espresso"), |
|
36 | - false, |
|
37 | - 0 |
|
38 | - ), |
|
39 | - ], |
|
40 | - ]; |
|
41 | - $this->_model_relations = [ |
|
42 | - 'Term_Taxonomy' => new EE_Has_Many_Relation(), |
|
43 | - ]; |
|
44 | - $this->_wp_core_model = true; |
|
45 | - $path_to_tax_model = 'Term_Taxonomy'; |
|
46 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
47 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = |
|
48 | - new EE_Restriction_Generator_Taxonomy_Protected( |
|
49 | - $path_to_tax_model |
|
50 | - ); |
|
51 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = false; |
|
52 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = false; |
|
53 | - $path_to_tax_model = $path_to_tax_model . '.'; |
|
54 | - // add cap restrictions for editing relating to the "ee_edit_*" |
|
55 | - $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_event_category'] = new EE_Default_Where_Conditions( |
|
56 | - [ |
|
57 | - $path_to_tax_model . 'taxonomy*ee_edit_event_category' => ['!=', 'espresso_event_categories'], |
|
58 | - ] |
|
59 | - ); |
|
60 | - $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_venue_category'] = new EE_Default_Where_Conditions( |
|
61 | - [ |
|
62 | - $path_to_tax_model . 'taxonomy*ee_edit_venue_category' => ['!=', 'espresso_venue_categories'], |
|
63 | - ] |
|
64 | - ); |
|
65 | - $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_event_type'] = new EE_Default_Where_Conditions( |
|
66 | - [ |
|
67 | - $path_to_tax_model . 'taxonomy*ee_edit_event_type' => ['!=', 'espresso_event_type'], |
|
68 | - ] |
|
69 | - ); |
|
70 | - // add cap restrictions for deleting relating to the "ee_deleting_*" |
|
71 | - $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_event_category'] = new EE_Default_Where_Conditions( |
|
72 | - [ |
|
73 | - $path_to_tax_model . 'taxonomy*ee_delete_event_category' => ['!=', 'espresso_event_categories'], |
|
74 | - ] |
|
75 | - ); |
|
76 | - $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_venue_category'] = new EE_Default_Where_Conditions( |
|
77 | - [ |
|
78 | - $path_to_tax_model . 'taxonomy*ee_delete_venue_category' => ['!=', 'espresso_venue_categories'], |
|
79 | - ] |
|
80 | - ); |
|
81 | - $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_event_type'] = new EE_Default_Where_Conditions( |
|
82 | - [ |
|
83 | - $path_to_tax_model . 'taxonomy*ee_delete_event_type' => ['!=', 'espresso_event_type'], |
|
84 | - ] |
|
85 | - ); |
|
86 | - parent::__construct($timezone); |
|
87 | - add_filter('FHEE__Read__create_model_query_params', ['EEM_Term', 'rest_api_query_params'], 10, 3); |
|
88 | - } |
|
17 | + /** |
|
18 | + * @param string|null $timezone |
|
19 | + * @throws EE_Error |
|
20 | + */ |
|
21 | + protected function __construct(?string $timezone = '') |
|
22 | + { |
|
23 | + $this->singular_item = esc_html__('Term', 'event_espresso'); |
|
24 | + $this->plural_item = esc_html__('Terms', 'event_espresso'); |
|
25 | + $this->_tables = [ |
|
26 | + 'Term' => new EE_Primary_Table('terms', 'term_id'), |
|
27 | + ]; |
|
28 | + $this->_fields = [ |
|
29 | + 'Term' => [ |
|
30 | + 'term_id' => new EE_Primary_Key_Int_Field('term_id', esc_html__('Term ID', 'event_espresso')), |
|
31 | + 'name' => new EE_Plain_Text_Field('name', esc_html__('Term Name', 'event_espresso'), false, ''), |
|
32 | + 'slug' => new EE_Slug_Field('slug', esc_html__('Term Slug', 'event_espresso'), false), |
|
33 | + 'term_group' => new EE_Integer_Field( |
|
34 | + 'term_group', |
|
35 | + esc_html__("Term Group", "event_espresso"), |
|
36 | + false, |
|
37 | + 0 |
|
38 | + ), |
|
39 | + ], |
|
40 | + ]; |
|
41 | + $this->_model_relations = [ |
|
42 | + 'Term_Taxonomy' => new EE_Has_Many_Relation(), |
|
43 | + ]; |
|
44 | + $this->_wp_core_model = true; |
|
45 | + $path_to_tax_model = 'Term_Taxonomy'; |
|
46 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
47 | + $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = |
|
48 | + new EE_Restriction_Generator_Taxonomy_Protected( |
|
49 | + $path_to_tax_model |
|
50 | + ); |
|
51 | + $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = false; |
|
52 | + $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = false; |
|
53 | + $path_to_tax_model = $path_to_tax_model . '.'; |
|
54 | + // add cap restrictions for editing relating to the "ee_edit_*" |
|
55 | + $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_event_category'] = new EE_Default_Where_Conditions( |
|
56 | + [ |
|
57 | + $path_to_tax_model . 'taxonomy*ee_edit_event_category' => ['!=', 'espresso_event_categories'], |
|
58 | + ] |
|
59 | + ); |
|
60 | + $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_venue_category'] = new EE_Default_Where_Conditions( |
|
61 | + [ |
|
62 | + $path_to_tax_model . 'taxonomy*ee_edit_venue_category' => ['!=', 'espresso_venue_categories'], |
|
63 | + ] |
|
64 | + ); |
|
65 | + $this->_cap_restrictions[ EEM_Base::caps_edit ]['ee_edit_event_type'] = new EE_Default_Where_Conditions( |
|
66 | + [ |
|
67 | + $path_to_tax_model . 'taxonomy*ee_edit_event_type' => ['!=', 'espresso_event_type'], |
|
68 | + ] |
|
69 | + ); |
|
70 | + // add cap restrictions for deleting relating to the "ee_deleting_*" |
|
71 | + $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_event_category'] = new EE_Default_Where_Conditions( |
|
72 | + [ |
|
73 | + $path_to_tax_model . 'taxonomy*ee_delete_event_category' => ['!=', 'espresso_event_categories'], |
|
74 | + ] |
|
75 | + ); |
|
76 | + $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_venue_category'] = new EE_Default_Where_Conditions( |
|
77 | + [ |
|
78 | + $path_to_tax_model . 'taxonomy*ee_delete_venue_category' => ['!=', 'espresso_venue_categories'], |
|
79 | + ] |
|
80 | + ); |
|
81 | + $this->_cap_restrictions[ EEM_Base::caps_delete ]['ee_delete_event_type'] = new EE_Default_Where_Conditions( |
|
82 | + [ |
|
83 | + $path_to_tax_model . 'taxonomy*ee_delete_event_type' => ['!=', 'espresso_event_type'], |
|
84 | + ] |
|
85 | + ); |
|
86 | + parent::__construct($timezone); |
|
87 | + add_filter('FHEE__Read__create_model_query_params', ['EEM_Term', 'rest_api_query_params'], 10, 3); |
|
88 | + } |
|
89 | 89 | |
90 | 90 | |
91 | - /** |
|
92 | - * retrieves a list of all EE event categories |
|
93 | - * |
|
94 | - * @access public |
|
95 | - * @param bool $show_uncategorized |
|
96 | - * @return EE_Base_Class[] |
|
97 | - * @throws EE_Error |
|
98 | - * @throws ReflectionException |
|
99 | - */ |
|
100 | - public function get_all_ee_categories($show_uncategorized = false) |
|
101 | - { |
|
102 | - $where_params = [ |
|
103 | - 'Term_Taxonomy.taxonomy' => 'espresso_event_categories', |
|
104 | - 'NOT' => ['name' => esc_html__('Uncategorized', 'event_espresso')], |
|
105 | - ]; |
|
106 | - if ($show_uncategorized) { |
|
107 | - unset($where_params['NOT']); |
|
108 | - } |
|
109 | - return EEM_Term::instance()->get_all( |
|
110 | - [ |
|
111 | - $where_params, |
|
112 | - 'order_by' => ['name' => 'ASC'], |
|
113 | - ] |
|
114 | - ); |
|
115 | - } |
|
91 | + /** |
|
92 | + * retrieves a list of all EE event categories |
|
93 | + * |
|
94 | + * @access public |
|
95 | + * @param bool $show_uncategorized |
|
96 | + * @return EE_Base_Class[] |
|
97 | + * @throws EE_Error |
|
98 | + * @throws ReflectionException |
|
99 | + */ |
|
100 | + public function get_all_ee_categories($show_uncategorized = false) |
|
101 | + { |
|
102 | + $where_params = [ |
|
103 | + 'Term_Taxonomy.taxonomy' => 'espresso_event_categories', |
|
104 | + 'NOT' => ['name' => esc_html__('Uncategorized', 'event_espresso')], |
|
105 | + ]; |
|
106 | + if ($show_uncategorized) { |
|
107 | + unset($where_params['NOT']); |
|
108 | + } |
|
109 | + return EEM_Term::instance()->get_all( |
|
110 | + [ |
|
111 | + $where_params, |
|
112 | + 'order_by' => ['name' => 'ASC'], |
|
113 | + ] |
|
114 | + ); |
|
115 | + } |
|
116 | 116 | |
117 | 117 | |
118 | - /** |
|
119 | - * retrieves a list of all post_tags associated with an EE CPT |
|
120 | - * |
|
121 | - * @access public |
|
122 | - * @param string $post_type |
|
123 | - * @return array |
|
124 | - * @throws EE_Error |
|
125 | - * @throws ReflectionException |
|
126 | - */ |
|
127 | - public function get_all_CPT_post_tags($post_type = '') |
|
128 | - { |
|
129 | - switch ($post_type) { |
|
130 | - case EspressoPostType::EVENTS: |
|
131 | - return $this->get_all_event_post_tags(); |
|
118 | + /** |
|
119 | + * retrieves a list of all post_tags associated with an EE CPT |
|
120 | + * |
|
121 | + * @access public |
|
122 | + * @param string $post_type |
|
123 | + * @return array |
|
124 | + * @throws EE_Error |
|
125 | + * @throws ReflectionException |
|
126 | + */ |
|
127 | + public function get_all_CPT_post_tags($post_type = '') |
|
128 | + { |
|
129 | + switch ($post_type) { |
|
130 | + case EspressoPostType::EVENTS: |
|
131 | + return $this->get_all_event_post_tags(); |
|
132 | 132 | |
133 | - case EspressoPostType::VENUES: |
|
134 | - return $this->get_all_venue_post_tags(); |
|
133 | + case EspressoPostType::VENUES: |
|
134 | + return $this->get_all_venue_post_tags(); |
|
135 | 135 | |
136 | - default: |
|
137 | - $event_tags = $this->get_all_event_post_tags(); |
|
138 | - $venue_tags = $this->get_all_venue_post_tags(); |
|
139 | - return array_merge($event_tags, $venue_tags); |
|
140 | - } |
|
141 | - } |
|
136 | + default: |
|
137 | + $event_tags = $this->get_all_event_post_tags(); |
|
138 | + $venue_tags = $this->get_all_venue_post_tags(); |
|
139 | + return array_merge($event_tags, $venue_tags); |
|
140 | + } |
|
141 | + } |
|
142 | 142 | |
143 | 143 | |
144 | - /** |
|
145 | - * returns an EE_Term object for the given tag |
|
146 | - * if it has been utilized by any EE_Events or EE_Venues |
|
147 | - * |
|
148 | - * @param string $tag |
|
149 | - * @return EE_Term|null |
|
150 | - * @throws EE_Error |
|
151 | - * @throws ReflectionException |
|
152 | - */ |
|
153 | - public function get_post_tag_for_event_or_venue($tag) |
|
154 | - { |
|
155 | - $post_tag_results = $this->get_all_wpdb_results( |
|
156 | - [ |
|
157 | - [ |
|
158 | - 'slug' => $tag, |
|
159 | - 'Term_Taxonomy.taxonomy' => 'post_tag', |
|
160 | - 'OR' => [ |
|
161 | - 'Term_Taxonomy.Venue.post_type' => EspressoPostType::VENUES, |
|
162 | - 'Term_Taxonomy.Event.post_type' => EspressoPostType::EVENTS, |
|
163 | - ], |
|
164 | - ], |
|
165 | - 'default_where_conditions' => 'none', |
|
166 | - 'extra_selects' => [ |
|
167 | - 'event_post_type' => ['Term_Taxonomy___Event_CPT.post_type', '%s'], |
|
168 | - 'venue_post_type' => ['Term_Taxonomy___Venue_CPT.post_type', '%s'], |
|
169 | - ], |
|
170 | - 'group_by' => [ |
|
171 | - 'event_post_type', |
|
172 | - 'venue_post_type', |
|
173 | - ], |
|
174 | - 'limit' => 2, |
|
175 | - ] |
|
176 | - ); |
|
177 | - if (! $post_tag_results) { |
|
178 | - return null; |
|
179 | - } |
|
144 | + /** |
|
145 | + * returns an EE_Term object for the given tag |
|
146 | + * if it has been utilized by any EE_Events or EE_Venues |
|
147 | + * |
|
148 | + * @param string $tag |
|
149 | + * @return EE_Term|null |
|
150 | + * @throws EE_Error |
|
151 | + * @throws ReflectionException |
|
152 | + */ |
|
153 | + public function get_post_tag_for_event_or_venue($tag) |
|
154 | + { |
|
155 | + $post_tag_results = $this->get_all_wpdb_results( |
|
156 | + [ |
|
157 | + [ |
|
158 | + 'slug' => $tag, |
|
159 | + 'Term_Taxonomy.taxonomy' => 'post_tag', |
|
160 | + 'OR' => [ |
|
161 | + 'Term_Taxonomy.Venue.post_type' => EspressoPostType::VENUES, |
|
162 | + 'Term_Taxonomy.Event.post_type' => EspressoPostType::EVENTS, |
|
163 | + ], |
|
164 | + ], |
|
165 | + 'default_where_conditions' => 'none', |
|
166 | + 'extra_selects' => [ |
|
167 | + 'event_post_type' => ['Term_Taxonomy___Event_CPT.post_type', '%s'], |
|
168 | + 'venue_post_type' => ['Term_Taxonomy___Venue_CPT.post_type', '%s'], |
|
169 | + ], |
|
170 | + 'group_by' => [ |
|
171 | + 'event_post_type', |
|
172 | + 'venue_post_type', |
|
173 | + ], |
|
174 | + 'limit' => 2, |
|
175 | + ] |
|
176 | + ); |
|
177 | + if (! $post_tag_results) { |
|
178 | + return null; |
|
179 | + } |
|
180 | 180 | |
181 | - $post_types = []; |
|
182 | - foreach ((array) $post_tag_results as $row) { |
|
183 | - if ($row['event_post_type'] === EspressoPostType::EVENTS) { |
|
184 | - $post_types[] = EEM_Event::instance()->post_type(); |
|
185 | - } elseif ($row['venue_post_type'] === EspressoPostType::VENUES) { |
|
186 | - $post_types[] = EEM_Venue::instance()->post_type(); |
|
187 | - } |
|
188 | - } |
|
189 | - $post_tag_row = reset($post_tag_results); |
|
190 | - $post_tag = $this->instantiate_class_from_array_or_object($post_tag_row); |
|
191 | - if (! $post_tag instanceof EE_Term) { |
|
192 | - return null; |
|
193 | - } |
|
181 | + $post_types = []; |
|
182 | + foreach ((array) $post_tag_results as $row) { |
|
183 | + if ($row['event_post_type'] === EspressoPostType::EVENTS) { |
|
184 | + $post_types[] = EEM_Event::instance()->post_type(); |
|
185 | + } elseif ($row['venue_post_type'] === EspressoPostType::VENUES) { |
|
186 | + $post_types[] = EEM_Venue::instance()->post_type(); |
|
187 | + } |
|
188 | + } |
|
189 | + $post_tag_row = reset($post_tag_results); |
|
190 | + $post_tag = $this->instantiate_class_from_array_or_object($post_tag_row); |
|
191 | + if (! $post_tag instanceof EE_Term) { |
|
192 | + return null; |
|
193 | + } |
|
194 | 194 | |
195 | - if ($post_tag->post_type === null) { |
|
196 | - $post_tag->post_type = []; |
|
197 | - } |
|
198 | - $post_tag->post_type = array_merge($post_tag->post_type, array_unique($post_types)); |
|
199 | - return $post_tag; |
|
200 | - } |
|
195 | + if ($post_tag->post_type === null) { |
|
196 | + $post_tag->post_type = []; |
|
197 | + } |
|
198 | + $post_tag->post_type = array_merge($post_tag->post_type, array_unique($post_types)); |
|
199 | + return $post_tag; |
|
200 | + } |
|
201 | 201 | |
202 | 202 | |
203 | - /** |
|
204 | - * get_all_event_post_tags |
|
205 | - * |
|
206 | - * @return EE_Base_Class[] |
|
207 | - * @throws EE_Error |
|
208 | - * @throws ReflectionException |
|
209 | - */ |
|
210 | - public function get_all_event_post_tags() |
|
211 | - { |
|
212 | - $post_tags = EEM_Term::instance()->get_all( |
|
213 | - [ |
|
214 | - [ |
|
215 | - 'Term_Taxonomy.taxonomy' => 'post_tag', |
|
216 | - 'Term_Taxonomy.Event.post_type' => EspressoPostType::EVENTS, |
|
217 | - ], |
|
218 | - 'order_by' => ['name' => 'ASC'], |
|
219 | - 'force_join' => ['Term_Taxonomy.Event'], |
|
220 | - ] |
|
221 | - ); |
|
222 | - foreach ($post_tags as $key => $post_tag) { |
|
223 | - if (! isset($post_tags[ $key ]->post_type)) { |
|
224 | - $post_tags[ $key ]->post_type = []; |
|
225 | - } |
|
226 | - $post_tags[ $key ]->post_type[] = EspressoPostType::EVENTS; |
|
227 | - } |
|
228 | - return $post_tags; |
|
229 | - } |
|
203 | + /** |
|
204 | + * get_all_event_post_tags |
|
205 | + * |
|
206 | + * @return EE_Base_Class[] |
|
207 | + * @throws EE_Error |
|
208 | + * @throws ReflectionException |
|
209 | + */ |
|
210 | + public function get_all_event_post_tags() |
|
211 | + { |
|
212 | + $post_tags = EEM_Term::instance()->get_all( |
|
213 | + [ |
|
214 | + [ |
|
215 | + 'Term_Taxonomy.taxonomy' => 'post_tag', |
|
216 | + 'Term_Taxonomy.Event.post_type' => EspressoPostType::EVENTS, |
|
217 | + ], |
|
218 | + 'order_by' => ['name' => 'ASC'], |
|
219 | + 'force_join' => ['Term_Taxonomy.Event'], |
|
220 | + ] |
|
221 | + ); |
|
222 | + foreach ($post_tags as $key => $post_tag) { |
|
223 | + if (! isset($post_tags[ $key ]->post_type)) { |
|
224 | + $post_tags[ $key ]->post_type = []; |
|
225 | + } |
|
226 | + $post_tags[ $key ]->post_type[] = EspressoPostType::EVENTS; |
|
227 | + } |
|
228 | + return $post_tags; |
|
229 | + } |
|
230 | 230 | |
231 | 231 | |
232 | - /** |
|
233 | - * get_all_venue_post_tags |
|
234 | - * |
|
235 | - * @return EE_Base_Class[] |
|
236 | - * @throws EE_Error |
|
237 | - * @throws ReflectionException |
|
238 | - */ |
|
239 | - public function get_all_venue_post_tags() |
|
240 | - { |
|
241 | - $post_tags = EEM_Term::instance()->get_all( |
|
242 | - [ |
|
243 | - [ |
|
244 | - 'Term_Taxonomy.taxonomy' => 'post_tag', |
|
245 | - 'Term_Taxonomy.Venue.post_type' => EspressoPostType::VENUES, |
|
246 | - ], |
|
247 | - 'order_by' => ['name' => 'ASC'], |
|
248 | - 'force_join' => ['Term_Taxonomy'], |
|
249 | - ] |
|
250 | - ); |
|
251 | - foreach ($post_tags as $key => $post_tag) { |
|
252 | - if (! isset($post_tags[ $key ]->post_type)) { |
|
253 | - $post_tags[ $key ]->post_type = []; |
|
254 | - } |
|
255 | - $post_tags[ $key ]->post_type[] = EspressoPostType::VENUES; |
|
256 | - } |
|
257 | - return $post_tags; |
|
258 | - } |
|
232 | + /** |
|
233 | + * get_all_venue_post_tags |
|
234 | + * |
|
235 | + * @return EE_Base_Class[] |
|
236 | + * @throws EE_Error |
|
237 | + * @throws ReflectionException |
|
238 | + */ |
|
239 | + public function get_all_venue_post_tags() |
|
240 | + { |
|
241 | + $post_tags = EEM_Term::instance()->get_all( |
|
242 | + [ |
|
243 | + [ |
|
244 | + 'Term_Taxonomy.taxonomy' => 'post_tag', |
|
245 | + 'Term_Taxonomy.Venue.post_type' => EspressoPostType::VENUES, |
|
246 | + ], |
|
247 | + 'order_by' => ['name' => 'ASC'], |
|
248 | + 'force_join' => ['Term_Taxonomy'], |
|
249 | + ] |
|
250 | + ); |
|
251 | + foreach ($post_tags as $key => $post_tag) { |
|
252 | + if (! isset($post_tags[ $key ]->post_type)) { |
|
253 | + $post_tags[ $key ]->post_type = []; |
|
254 | + } |
|
255 | + $post_tags[ $key ]->post_type[] = EspressoPostType::VENUES; |
|
256 | + } |
|
257 | + return $post_tags; |
|
258 | + } |
|
259 | 259 | |
260 | 260 | |
261 | - /** |
|
262 | - * Makes sure that during REST API queries, we only return terms |
|
263 | - * for term taxonomies which should be shown in the rest api |
|
264 | - * |
|
265 | - * @param array $model_query_params |
|
266 | - * @param array $querystring_query_params |
|
267 | - * @param EEM_Base $model |
|
268 | - * @return array |
|
269 | - * @throws EE_Error |
|
270 | - * @throws ReflectionException |
|
271 | - */ |
|
272 | - public static function rest_api_query_params($model_query_params, $querystring_query_params, $model) |
|
273 | - { |
|
274 | - if ($model === EEM_Term::instance()) { |
|
275 | - $taxonomies = get_taxonomies(['show_in_rest' => true]); |
|
276 | - if (! empty($taxonomies)) { |
|
277 | - $model_query_params[0]['Term_Taxonomy.taxonomy'] = ['IN', $taxonomies]; |
|
278 | - } |
|
279 | - } |
|
280 | - return $model_query_params; |
|
281 | - } |
|
261 | + /** |
|
262 | + * Makes sure that during REST API queries, we only return terms |
|
263 | + * for term taxonomies which should be shown in the rest api |
|
264 | + * |
|
265 | + * @param array $model_query_params |
|
266 | + * @param array $querystring_query_params |
|
267 | + * @param EEM_Base $model |
|
268 | + * @return array |
|
269 | + * @throws EE_Error |
|
270 | + * @throws ReflectionException |
|
271 | + */ |
|
272 | + public static function rest_api_query_params($model_query_params, $querystring_query_params, $model) |
|
273 | + { |
|
274 | + if ($model === EEM_Term::instance()) { |
|
275 | + $taxonomies = get_taxonomies(['show_in_rest' => true]); |
|
276 | + if (! empty($taxonomies)) { |
|
277 | + $model_query_params[0]['Term_Taxonomy.taxonomy'] = ['IN', $taxonomies]; |
|
278 | + } |
|
279 | + } |
|
280 | + return $model_query_params; |
|
281 | + } |
|
282 | 282 | } |
@@ -15,813 +15,813 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class EEM_Registration extends EEM_Soft_Delete_Base |
17 | 17 | { |
18 | - /** |
|
19 | - * The value of REG_count for a primary registrant |
|
20 | - */ |
|
21 | - const PRIMARY_REGISTRANT_COUNT = 1; |
|
22 | - |
|
23 | - /** |
|
24 | - * @depecated 5.0.20.p use RegStatus::INCOMPLETE instead |
|
25 | - */ |
|
26 | - const status_id_incomplete = 'RIC'; |
|
27 | - |
|
28 | - /** |
|
29 | - * @depecated 5.0.20.p use RegStatus::AWAITING_REVIEW instead |
|
30 | - */ |
|
31 | - const status_id_not_approved = 'RNA'; |
|
32 | - |
|
33 | - /** |
|
34 | - * @depecated 5.0.20.p use RegStatus::PENDING_PAYMENT instead |
|
35 | - */ |
|
36 | - const status_id_pending_payment = 'RPP'; |
|
37 | - |
|
38 | - /** |
|
39 | - * @depecated 5.0.20.p use RegStatus::WAIT_LIST instead |
|
40 | - */ |
|
41 | - const status_id_wait_list = 'RWL'; |
|
42 | - |
|
43 | - /** |
|
44 | - * @depecated 5.0.20.p use RegStatus::APPROVED instead |
|
45 | - */ |
|
46 | - const status_id_approved = 'RAP'; |
|
47 | - |
|
48 | - /** |
|
49 | - * @depecated 5.0.20.p use RegStatus::CANCELLED instead |
|
50 | - */ |
|
51 | - const status_id_cancelled = 'RCN'; |
|
52 | - |
|
53 | - /** |
|
54 | - * @depecated 5.0.20.p use RegStatus::DECLINED instead |
|
55 | - */ |
|
56 | - const status_id_declined = 'RDC'; |
|
57 | - |
|
58 | - |
|
59 | - protected static ?EEM_Registration $_instance = null; |
|
60 | - |
|
61 | - protected ?TableAnalysis $_table_analysis = null; |
|
62 | - |
|
63 | - /** |
|
64 | - * Keys are the status IDs for registrations (eg, RAP, RCN, etc), and the values |
|
65 | - * are status codes (eg, approved, cancelled, etc) |
|
66 | - * |
|
67 | - * @var array|null |
|
68 | - */ |
|
69 | - private static array $_reg_status = []; |
|
70 | - |
|
71 | - |
|
72 | - /** |
|
73 | - * private constructor to prevent direct creation |
|
74 | - * |
|
75 | - * @param string|null $timezone string representing the timezone we want to set for returned Date Time Strings (and |
|
76 | - * any incoming timezone data that gets saved). Note this just sends the timezone info |
|
77 | - * to the date time model field objects. Default is NULL (and will be assumed using |
|
78 | - * the set timezone in the 'timezone_string' wp option) |
|
79 | - * @throws EE_Error |
|
80 | - * @throws ReflectionException |
|
81 | - */ |
|
82 | - protected function __construct(?string $timezone = '') |
|
83 | - { |
|
84 | - $this->_table_analysis = EE_Registry::instance()->create('TableAnalysis', [], true); |
|
85 | - $this->singular_item = esc_html__('Registration', 'event_espresso'); |
|
86 | - $this->plural_item = esc_html__('Registrations', 'event_espresso'); |
|
87 | - $this->_tables = [ |
|
88 | - 'Registration' => new EE_Primary_Table('esp_registration', 'REG_ID'), |
|
89 | - ]; |
|
90 | - $this->_fields = [ |
|
91 | - 'Registration' => [ |
|
92 | - 'REG_ID' => new EE_Primary_Key_Int_Field( |
|
93 | - 'REG_ID', |
|
94 | - esc_html__('Registration ID', 'event_espresso') |
|
95 | - ), |
|
96 | - 'EVT_ID' => new EE_Foreign_Key_Int_Field( |
|
97 | - 'EVT_ID', |
|
98 | - esc_html__('Event ID', 'event_espresso'), |
|
99 | - false, |
|
100 | - 0, |
|
101 | - 'Event' |
|
102 | - ), |
|
103 | - 'ATT_ID' => new EE_Foreign_Key_Int_Field( |
|
104 | - 'ATT_ID', |
|
105 | - esc_html__('Attendee ID', 'event_espresso'), |
|
106 | - false, |
|
107 | - 0, |
|
108 | - 'Attendee' |
|
109 | - ), |
|
110 | - 'TXN_ID' => new EE_Foreign_Key_Int_Field( |
|
111 | - 'TXN_ID', |
|
112 | - esc_html__('Transaction ID', 'event_espresso'), |
|
113 | - false, |
|
114 | - 0, |
|
115 | - 'Transaction' |
|
116 | - ), |
|
117 | - 'TKT_ID' => new EE_Foreign_Key_Int_Field( |
|
118 | - 'TKT_ID', |
|
119 | - esc_html__('Ticket ID', 'event_espresso'), |
|
120 | - false, |
|
121 | - 0, |
|
122 | - 'Ticket' |
|
123 | - ), |
|
124 | - 'STS_ID' => new EE_Foreign_Key_String_Field( |
|
125 | - 'STS_ID', |
|
126 | - esc_html__('Status ID', 'event_espresso'), |
|
127 | - false, |
|
128 | - RegStatus::INCOMPLETE, |
|
129 | - 'Status' |
|
130 | - ), |
|
131 | - 'REG_date' => new EE_Datetime_Field( |
|
132 | - 'REG_date', |
|
133 | - esc_html__('Time registration occurred', 'event_espresso'), |
|
134 | - false, |
|
135 | - EE_Datetime_Field::now, |
|
136 | - $timezone |
|
137 | - ), |
|
138 | - 'REG_final_price' => new EE_Money_Field( |
|
139 | - 'REG_final_price', |
|
140 | - esc_html__('Registration\'s share of the transaction total', 'event_espresso'), |
|
141 | - false, |
|
142 | - 0 |
|
143 | - ), |
|
144 | - 'REG_paid' => new EE_Money_Field( |
|
145 | - 'REG_paid', |
|
146 | - esc_html__('Amount paid to date towards registration', 'event_espresso'), |
|
147 | - false, |
|
148 | - 0 |
|
149 | - ), |
|
150 | - 'REG_session' => new EE_Plain_Text_Field( |
|
151 | - 'REG_session', |
|
152 | - esc_html__('Session ID of registration', 'event_espresso'), |
|
153 | - false, |
|
154 | - '' |
|
155 | - ), |
|
156 | - 'REG_code' => new EE_Plain_Text_Field( |
|
157 | - 'REG_code', |
|
158 | - esc_html__('Unique Code for this registration', 'event_espresso'), |
|
159 | - false, |
|
160 | - '' |
|
161 | - ), |
|
162 | - 'REG_url_link' => new EE_Plain_Text_Field( |
|
163 | - 'REG_url_link', |
|
164 | - esc_html__('String to be used in URL for identifying registration', 'event_espresso'), |
|
165 | - false, |
|
166 | - '' |
|
167 | - ), |
|
168 | - 'REG_count' => new EE_Integer_Field( |
|
169 | - 'REG_count', |
|
170 | - esc_html__('Count of this registration in the group registration', 'event_espresso'), |
|
171 | - true, |
|
172 | - 1 |
|
173 | - ), |
|
174 | - 'REG_group_size' => new EE_Integer_Field( |
|
175 | - 'REG_group_size', |
|
176 | - esc_html__('Number of registrations on this group', 'event_espresso'), |
|
177 | - false, |
|
178 | - 1 |
|
179 | - ), |
|
180 | - 'REG_att_is_going' => new EE_Boolean_Field( |
|
181 | - 'REG_att_is_going', |
|
182 | - esc_html__('Flag indicating the registrant plans on attending', 'event_espresso'), |
|
183 | - false, |
|
184 | - false |
|
185 | - ), |
|
186 | - 'REG_deleted' => new EE_Trashed_Flag_Field( |
|
187 | - 'REG_deleted', |
|
188 | - esc_html__('Flag indicating if registration has been archived or not.', 'event_espresso'), |
|
189 | - false, |
|
190 | - false |
|
191 | - ), |
|
192 | - ], |
|
193 | - ]; |
|
194 | - $this->_model_relations = [ |
|
195 | - 'Event' => new EE_Belongs_To_Relation(), |
|
196 | - 'Attendee' => new EE_Belongs_To_Relation(), |
|
197 | - 'Transaction' => new EE_Belongs_To_Relation(), |
|
198 | - 'Ticket' => new EE_Belongs_To_Relation(), |
|
199 | - 'Status' => new EE_Belongs_To_Relation(), |
|
200 | - 'Answer' => new EE_Has_Many_Relation(), |
|
201 | - 'Checkin' => new EE_Has_Many_Relation(), |
|
202 | - 'Registration_Payment' => new EE_Has_Many_Relation(), |
|
203 | - 'Payment' => new EE_HABTM_Relation('Registration_Payment'), |
|
204 | - 'Message' => new EE_Has_Many_Any_Relation(false), |
|
205 | - // allow deletes even if there are messages in the queue related |
|
206 | - ]; |
|
207 | - $this->_model_chain_to_wp_user = 'Event'; |
|
208 | - parent::__construct($timezone); |
|
209 | - } |
|
210 | - |
|
211 | - |
|
212 | - /** |
|
213 | - * a list of ALL valid registration statuses currently in use within the system |
|
214 | - * generated by combining the filterable active and inactive reg status arrays |
|
215 | - * |
|
216 | - * @return array |
|
217 | - */ |
|
218 | - public static function reg_statuses(): array |
|
219 | - { |
|
220 | - return array_unique( |
|
221 | - array_merge( |
|
222 | - EEM_Registration::active_reg_statuses(), |
|
223 | - EEM_Registration::inactive_reg_statuses() |
|
224 | - ) |
|
225 | - ); |
|
226 | - } |
|
227 | - |
|
228 | - |
|
229 | - /** |
|
230 | - * reg_statuses_that_allow_payment |
|
231 | - * a filterable list of registration statuses that allow a registrant to make a payment |
|
232 | - * |
|
233 | - * @access public |
|
234 | - * @return array |
|
235 | - */ |
|
236 | - public static function reg_statuses_that_allow_payment(): array |
|
237 | - { |
|
238 | - return (array) apply_filters( |
|
239 | - 'FHEE__EEM_Registration__reg_statuses_that_allow_payment', |
|
240 | - [ |
|
241 | - RegStatus::APPROVED, |
|
242 | - RegStatus::PENDING_PAYMENT, |
|
243 | - ] |
|
244 | - ); |
|
245 | - } |
|
246 | - |
|
247 | - |
|
248 | - /** |
|
249 | - * active_reg_statuses |
|
250 | - * a filterable list of registration statuses that are considered active |
|
251 | - * |
|
252 | - * @access public |
|
253 | - * @return array |
|
254 | - */ |
|
255 | - public static function active_reg_statuses(): array |
|
256 | - { |
|
257 | - return (array) apply_filters( |
|
258 | - 'FHEE__EEM_Registration__active_reg_statuses', |
|
259 | - [ |
|
260 | - RegStatus::APPROVED, |
|
261 | - RegStatus::PENDING_PAYMENT, |
|
262 | - RegStatus::WAIT_LIST, |
|
263 | - RegStatus::AWAITING_REVIEW, |
|
264 | - ] |
|
265 | - ); |
|
266 | - } |
|
267 | - |
|
268 | - |
|
269 | - /** |
|
270 | - * inactive_reg_statuses |
|
271 | - * a filterable list of registration statuses that are not considered active |
|
272 | - * |
|
273 | - * @access public |
|
274 | - * @return array |
|
275 | - */ |
|
276 | - public static function inactive_reg_statuses(): array |
|
277 | - { |
|
278 | - return (array) apply_filters( |
|
279 | - 'FHEE__EEM_Registration__inactive_reg_statuses', |
|
280 | - [ |
|
281 | - RegStatus::INCOMPLETE, |
|
282 | - RegStatus::CANCELLED, |
|
283 | - RegStatus::DECLINED, |
|
284 | - ] |
|
285 | - ); |
|
286 | - } |
|
287 | - |
|
288 | - |
|
289 | - /** |
|
290 | - * closed_reg_statuses |
|
291 | - * a filterable list of registration statuses that are considered "closed" |
|
292 | - * meaning they should not be considered in any calculations involving monies owing |
|
293 | - * |
|
294 | - * @access public |
|
295 | - * @return array |
|
296 | - */ |
|
297 | - public static function closed_reg_statuses(): array |
|
298 | - { |
|
299 | - return (array) apply_filters( |
|
300 | - 'FHEE__EEM_Registration__closed_reg_statuses', |
|
301 | - [ |
|
302 | - RegStatus::CANCELLED, |
|
303 | - RegStatus::DECLINED, |
|
304 | - RegStatus::WAIT_LIST, |
|
305 | - ] |
|
306 | - ); |
|
307 | - } |
|
308 | - |
|
309 | - |
|
310 | - /** |
|
311 | - * get list of registration statuses |
|
312 | - * |
|
313 | - * @param array $exclude The status ids to exclude from the returned results |
|
314 | - * @param bool $translated If true will return the values as singular localized strings |
|
315 | - * @return array |
|
316 | - * @throws EE_Error |
|
317 | - * @throws ReflectionException |
|
318 | - */ |
|
319 | - public static function reg_status_array(array $exclude = [], bool $translated = false): array |
|
320 | - { |
|
321 | - if (empty(self::$_reg_status)) { |
|
322 | - EEM_Registration::instance()->_get_registration_status_array($exclude); |
|
323 | - } elseif ($exclude) { |
|
324 | - foreach ($exclude as $excluded_status) { |
|
325 | - unset(self::$_reg_status[ $excluded_status ]); |
|
326 | - } |
|
327 | - } |
|
328 | - return $translated |
|
329 | - ? EEM_Status::instance()->localized_status(self::$_reg_status, false, 'sentence') |
|
330 | - : self::$_reg_status; |
|
331 | - } |
|
332 | - |
|
333 | - |
|
334 | - /** |
|
335 | - * get list of registration statuses |
|
336 | - * |
|
337 | - * @param array $exclude |
|
338 | - * @param bool $recurse |
|
339 | - * @return void |
|
340 | - * @throws EE_Error |
|
341 | - * @throws ReflectionException |
|
342 | - */ |
|
343 | - private function _get_registration_status_array(array $exclude = [], bool $recurse = true) |
|
344 | - { |
|
345 | - // in the very rare circumstance that we are deleting a model's table's data |
|
346 | - // and the table hasn't actually been created, this could have an error |
|
347 | - /** @type WPDB $wpdb */ |
|
348 | - global $wpdb; |
|
349 | - if ($this->_get_table_analysis()->tableExists($wpdb->prefix . 'esp_status')) { |
|
350 | - $results = $wpdb->get_results( |
|
351 | - "SELECT STS_ID, STS_code FROM {$wpdb->prefix}esp_status WHERE STS_type = 'registration'" |
|
352 | - ); |
|
353 | - self::$_reg_status = []; |
|
354 | - foreach ($results as $status) { |
|
355 | - if (! in_array($status->STS_ID, $exclude, true)) { |
|
356 | - self::$_reg_status[ $status->STS_ID ] = $status->STS_code; |
|
357 | - } |
|
358 | - } |
|
359 | - } |
|
360 | - // in case reg status codes have been deleted from db |
|
361 | - if ($recurse && empty(self::$_reg_status)) { |
|
362 | - EEH_Activation::insert_default_status_codes(); |
|
363 | - $this->_get_registration_status_array($exclude, false); |
|
364 | - } |
|
365 | - } |
|
366 | - |
|
367 | - |
|
368 | - /** |
|
369 | - * Gets the injected table analyzer, or throws an exception |
|
370 | - * |
|
371 | - * @return TableAnalysis |
|
372 | - * @throws EE_Error |
|
373 | - */ |
|
374 | - protected function _get_table_analysis(): TableAnalysis |
|
375 | - { |
|
376 | - if ($this->_table_analysis instanceof TableAnalysis) { |
|
377 | - return $this->_table_analysis; |
|
378 | - } |
|
379 | - throw new EE_Error( |
|
380 | - sprintf( |
|
381 | - esc_html__('Table analysis class on class %1$s is not set properly.', 'event_espresso'), |
|
382 | - get_class($this) |
|
383 | - ) |
|
384 | - ); |
|
385 | - } |
|
386 | - |
|
387 | - |
|
388 | - /** |
|
389 | - * This returns a wpdb->results array of all registration date month and years matching the incoming query params |
|
390 | - * and grouped by month and year. |
|
391 | - * |
|
392 | - * @param array $where_params |
|
393 | - * @return bool|stdClass[] |
|
394 | - * @throws EE_Error |
|
395 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
396 | - */ |
|
397 | - public function get_reg_months_and_years(array $where_params) |
|
398 | - { |
|
399 | - $query_params[0] = $where_params; |
|
400 | - $query_params['group_by'] = ['reg_year', 'reg_month']; |
|
401 | - $query_params['order_by'] = ['REG_date' => 'DESC']; |
|
402 | - $columns_to_select = [ |
|
403 | - 'reg_year' => ['YEAR(REG_date)', '%s'], |
|
404 | - 'reg_month' => ['MONTHNAME(REG_date)', '%s'], |
|
405 | - ]; |
|
406 | - return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select); |
|
407 | - } |
|
408 | - |
|
409 | - |
|
410 | - /** |
|
411 | - * retrieve ALL registrations for a particular Attendee from db |
|
412 | - * |
|
413 | - * @param int $ATT_ID |
|
414 | - * @return EE_Registration[]|bool|null |
|
415 | - * @throws EE_Error |
|
416 | - * @throws ReflectionException |
|
417 | - */ |
|
418 | - public function get_all_registrations_for_attendee(int $ATT_ID = 0): ?array |
|
419 | - { |
|
420 | - if (! $ATT_ID) { |
|
421 | - return null; |
|
422 | - } |
|
423 | - return $this->get_all([['ATT_ID' => $ATT_ID]]); |
|
424 | - } |
|
425 | - |
|
426 | - |
|
427 | - /** |
|
428 | - * Gets a registration given their REG_url_link. Yes, this should usually |
|
429 | - * be passed via a GET parameter. |
|
430 | - * |
|
431 | - * @param string $REG_url_link |
|
432 | - * @return EE_Registration|null |
|
433 | - * @throws EE_Error |
|
434 | - */ |
|
435 | - public function get_registration_for_reg_url_link(string $REG_url_link): ?EE_Registration |
|
436 | - { |
|
437 | - if (! $REG_url_link) { |
|
438 | - return null; |
|
439 | - } |
|
440 | - return $this->get_one([['REG_url_link' => $REG_url_link]]); |
|
441 | - } |
|
442 | - |
|
443 | - |
|
444 | - /** |
|
445 | - * retrieve registration for a specific transaction attendee from db |
|
446 | - * |
|
447 | - * @access public |
|
448 | - * @param int $TXN_ID |
|
449 | - * @param int $ATT_ID |
|
450 | - * @param int $att_nmbr in case the ATT_ID is the same for multiple registrations (same details used) then the |
|
451 | - * attendee number is required |
|
452 | - * @return EE_Registration|null array on success, FALSE on fail |
|
453 | - * @throws EE_Error |
|
454 | - */ |
|
455 | - public function get_registration_for_transaction_attendee(int $TXN_ID = 0, int $ATT_ID = 0, int $att_nmbr = 0): |
|
456 | - ?EE_Registration |
|
457 | - { |
|
458 | - return $this->get_one( |
|
459 | - [ |
|
460 | - [ |
|
461 | - 'TXN_ID' => $TXN_ID, |
|
462 | - 'ATT_ID' => $ATT_ID, |
|
463 | - ], |
|
464 | - 'limit' => [min($att_nmbr - 1, 0), 1], |
|
465 | - ] |
|
466 | - ); |
|
467 | - } |
|
468 | - |
|
469 | - |
|
470 | - /** |
|
471 | - * get the number of registrations per day for the Registration Admin page Reports Tab. |
|
472 | - * (doesn't utilize models because it's a fairly specialized query) |
|
473 | - * |
|
474 | - * @access public |
|
475 | - * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
|
476 | - * @return EE_Registration[]|bool|null with properties regDate and total |
|
477 | - * @throws EE_Error |
|
478 | - */ |
|
479 | - public function get_registrations_per_day_report(string $period = '-1 month') |
|
480 | - { |
|
481 | - $sql_date = $this->convert_datetime_for_query( |
|
482 | - 'REG_date', |
|
483 | - date('Y-m-d H:i:s', strtotime($period)), |
|
484 | - 'Y-m-d H:i:s', |
|
485 | - 'UTC' |
|
486 | - ); |
|
487 | - $where = [ |
|
488 | - 'REG_date' => ['>=', $sql_date], |
|
489 | - 'STS_ID' => ['!=', RegStatus::INCOMPLETE], |
|
490 | - ]; |
|
491 | - if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_day_report')) { |
|
492 | - $where['Event.EVT_wp_user'] = get_current_user_id(); |
|
493 | - } |
|
494 | - $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'REG_date'); |
|
495 | - return $this->_get_all_wpdb_results( |
|
496 | - [ |
|
497 | - $where, |
|
498 | - 'group_by' => 'regDate', |
|
499 | - 'order_by' => ['REG_date' => 'ASC'], |
|
500 | - ], |
|
501 | - OBJECT, |
|
502 | - [ |
|
503 | - 'regDate' => ['DATE(' . $query_interval . ')', '%s'], |
|
504 | - 'total' => ['count(REG_ID)', '%d'], |
|
505 | - ] |
|
506 | - ); |
|
507 | - } |
|
508 | - |
|
509 | - |
|
510 | - /** |
|
511 | - * Get the number of registrations per day including the count of registrations for each Registration Status. |
|
512 | - * Note: RegStatus::INCOMPLETE registrations are excluded from the results. |
|
513 | - * |
|
514 | - * @param string $period |
|
515 | - * @return stdClass[] with properties Registration_REG_date and a column for each registration status as the STS_ID |
|
516 | - * @throws EE_Error |
|
517 | - * (i.e. RAP) |
|
518 | - * @throws EE_Error|ReflectionException |
|
519 | - */ |
|
520 | - public function get_registrations_per_day_and_per_status_report(string $period = '-1 month'): array |
|
521 | - { |
|
522 | - global $wpdb; |
|
523 | - $registration_table = $wpdb->prefix . 'esp_registration'; |
|
524 | - $event_table = $wpdb->posts; |
|
525 | - $sql_date = date('Y-m-d H:i:s', strtotime($period)); |
|
526 | - // prepare the query interval for displaying offset |
|
527 | - $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'dates.REG_date'); |
|
528 | - // inner date query |
|
529 | - $inner_date_query = "SELECT DISTINCT REG_date from $registration_table "; |
|
530 | - $inner_where = ' WHERE'; |
|
531 | - // exclude events not authored by user if permissions in effect |
|
532 | - if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
533 | - $inner_date_query .= "LEFT JOIN $event_table ON ID = EVT_ID"; |
|
534 | - $inner_where .= ' post_author = ' . get_current_user_id() . ' AND'; |
|
535 | - } |
|
536 | - $inner_where .= " REG_date >= '$sql_date'"; |
|
537 | - $inner_date_query .= $inner_where; |
|
538 | - // start main query |
|
539 | - $select = "SELECT DATE($query_interval) as Registration_REG_date, "; |
|
540 | - $join = ''; |
|
541 | - $join_parts = []; |
|
542 | - $select_parts = []; |
|
543 | - // loop through registration stati to do parts for each status. |
|
544 | - foreach (EEM_Registration::reg_status_array() as $STS_ID => $STS_code) { |
|
545 | - if ($STS_ID === RegStatus::INCOMPLETE) { |
|
546 | - continue; |
|
547 | - } |
|
548 | - $select_parts[] = "COUNT($STS_code.REG_ID) as $STS_ID"; |
|
549 | - $join_parts[] = |
|
550 | - "$registration_table AS $STS_code ON $STS_code.REG_date = dates.REG_date AND $STS_code.STS_ID = '$STS_ID'"; |
|
551 | - } |
|
552 | - // setup the selects |
|
553 | - $select .= implode(', ', $select_parts); |
|
554 | - $select .= " FROM ($inner_date_query) AS dates LEFT JOIN "; |
|
555 | - // setup the joins |
|
556 | - $join .= implode(' LEFT JOIN ', $join_parts); |
|
557 | - // now let's put it all together |
|
558 | - $query = $select . $join . ' GROUP BY Registration_REG_date'; |
|
559 | - // and execute it |
|
560 | - return $wpdb->get_results($query, ARRAY_A); |
|
561 | - } |
|
562 | - |
|
563 | - |
|
564 | - /** |
|
565 | - * get the number of registrations per event for the Registration Admin page Reports Tab |
|
566 | - * |
|
567 | - * @access public |
|
568 | - * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
|
569 | - * @return stdClass[] each with properties event_name, reg_limit, and total |
|
570 | - * @throws EE_Error |
|
571 | - */ |
|
572 | - public function get_registrations_per_event_report(string $period = '-1 month'): array |
|
573 | - { |
|
574 | - $date_sql = $this->convert_datetime_for_query( |
|
575 | - 'REG_date', |
|
576 | - date('Y-m-d H:i:s', strtotime($period)), |
|
577 | - 'Y-m-d H:i:s', |
|
578 | - 'UTC' |
|
579 | - ); |
|
580 | - $where = [ |
|
581 | - 'REG_date' => ['>=', $date_sql], |
|
582 | - 'STS_ID' => ['!=', RegStatus::INCOMPLETE], |
|
583 | - ]; |
|
584 | - if ( |
|
585 | - ! EE_Registry::instance()->CAP->current_user_can( |
|
586 | - 'ee_read_others_registrations', |
|
587 | - 'reg_per_event_report' |
|
588 | - ) |
|
589 | - ) { |
|
590 | - $where['Event.EVT_wp_user'] = get_current_user_id(); |
|
591 | - } |
|
592 | - return $this->_get_all_wpdb_results( |
|
593 | - [ |
|
594 | - $where, |
|
595 | - 'group_by' => 'Event.EVT_name', |
|
596 | - 'order_by' => 'Event.EVT_name', |
|
597 | - 'limit' => [0, 24], |
|
598 | - ], |
|
599 | - OBJECT, |
|
600 | - [ |
|
601 | - 'event_name' => ['Event_CPT.post_title', '%s'], |
|
602 | - 'total' => ['COUNT(REG_ID)', '%s'], |
|
603 | - ] |
|
604 | - ); |
|
605 | - } |
|
606 | - |
|
607 | - |
|
608 | - /** |
|
609 | - * Get the number of registrations per event grouped by registration status. |
|
610 | - * Note: RegStatus::INCOMPLETE registrations are excluded from the results. |
|
611 | - * |
|
612 | - * @param string $period |
|
613 | - * @return stdClass[] with properties `Registration_Event` and a column for each registration status as the STS_ID |
|
614 | - * (i.e. RAP) |
|
615 | - * @throws EE_Error |
|
616 | - * @throws ReflectionException |
|
617 | - */ |
|
618 | - public function get_registrations_per_event_and_per_status_report(string $period = '-1 month'): array |
|
619 | - { |
|
620 | - global $wpdb; |
|
621 | - $registration_table = $wpdb->prefix . 'esp_registration'; |
|
622 | - $event_table = $wpdb->posts; |
|
623 | - $sql_date = date('Y-m-d H:i:s', strtotime($period)); |
|
624 | - // inner date query |
|
625 | - $inner_date_query = "SELECT DISTINCT EVT_ID, REG_date from $registration_table "; |
|
626 | - $inner_where = ' WHERE'; |
|
627 | - // exclude events not authored by user if permissions in effect |
|
628 | - if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
629 | - $inner_date_query .= "LEFT JOIN $event_table ON ID = EVT_ID"; |
|
630 | - $inner_where .= ' post_author = ' . get_current_user_id() . ' AND'; |
|
631 | - } |
|
632 | - $inner_where .= " REG_date >= '$sql_date'"; |
|
633 | - $inner_date_query .= $inner_where; |
|
634 | - // build main query |
|
635 | - $select = 'SELECT Event.post_title as Registration_Event, '; |
|
636 | - $join = ''; |
|
637 | - $join_parts = []; |
|
638 | - $select_parts = []; |
|
639 | - // loop through registration stati to do parts for each status. |
|
640 | - foreach (EEM_Registration::reg_status_array() as $STS_ID => $STS_code) { |
|
641 | - if ($STS_ID === RegStatus::INCOMPLETE) { |
|
642 | - continue; |
|
643 | - } |
|
644 | - $select_parts[] = "COUNT($STS_code.REG_ID) as $STS_ID"; |
|
645 | - $join_parts[] = |
|
646 | - "$registration_table AS $STS_code ON $STS_code.EVT_ID = dates.EVT_ID AND $STS_code.STS_ID = '$STS_ID' AND $STS_code.REG_date = dates.REG_date"; |
|
647 | - } |
|
648 | - // setup the selects |
|
649 | - $select .= implode(', ', $select_parts); |
|
650 | - $select .= " FROM ($inner_date_query) AS dates LEFT JOIN $event_table as Event ON Event.ID = dates.EVT_ID LEFT JOIN "; |
|
651 | - // setup remaining joins |
|
652 | - $join .= implode(' LEFT JOIN ', $join_parts); |
|
653 | - // now put it all together |
|
654 | - $query = $select . $join . ' GROUP BY Registration_Event'; |
|
655 | - // and execute |
|
656 | - return $wpdb->get_results($query, ARRAY_A); |
|
657 | - } |
|
658 | - |
|
659 | - |
|
660 | - /** |
|
661 | - * Returns the EE_Registration of the primary attendee on the transaction id provided |
|
662 | - * |
|
663 | - * @param int $TXN_ID |
|
664 | - * @return EE_Registration|null |
|
665 | - * @throws EE_Error |
|
666 | - */ |
|
667 | - public function get_primary_registration_for_transaction_ID(int $TXN_ID = 0): ?EE_Registration |
|
668 | - { |
|
669 | - if (! $TXN_ID) { |
|
670 | - return null; |
|
671 | - } |
|
672 | - return $this->get_one( |
|
673 | - [ |
|
674 | - [ |
|
675 | - 'TXN_ID' => $TXN_ID, |
|
676 | - 'REG_count' => EEM_Registration::PRIMARY_REGISTRANT_COUNT, |
|
677 | - ], |
|
678 | - ] |
|
679 | - ); |
|
680 | - } |
|
681 | - |
|
682 | - |
|
683 | - /** |
|
684 | - * get_event_registration_count |
|
685 | - * |
|
686 | - * @access public |
|
687 | - * @param int $EVT_ID |
|
688 | - * @param boolean $for_incomplete_payments |
|
689 | - * @return int |
|
690 | - * @throws EE_Error |
|
691 | - */ |
|
692 | - public function get_event_registration_count(int $EVT_ID, bool $for_incomplete_payments = false): int |
|
693 | - { |
|
694 | - // we only count approved registrations towards registration limits |
|
695 | - $query_params = [['EVT_ID' => $EVT_ID, 'STS_ID' => RegStatus::APPROVED]]; |
|
696 | - if ($for_incomplete_payments) { |
|
697 | - $query_params[0]['Transaction.STS_ID'] = ['!=', EEM_Transaction::complete_status_code]; |
|
698 | - } |
|
699 | - return $this->count($query_params); |
|
700 | - } |
|
701 | - |
|
702 | - |
|
703 | - /** |
|
704 | - * Deletes all registrations with no transactions. Note that this needs to be very efficient |
|
705 | - * and so it uses wpdb directly. Also, we can't put a limit on this because MySQL doesn't allow a limit on a delete |
|
706 | - * when joining tables like this. |
|
707 | - * |
|
708 | - * @return int|bool number deleted |
|
709 | - * @throws EE_Error |
|
710 | - * @throws ReflectionException |
|
711 | - * @global WPDB $wpdb |
|
712 | - */ |
|
713 | - public function delete_registrations_with_no_transaction() |
|
714 | - { |
|
715 | - /** @type WPDB $wpdb */ |
|
716 | - global $wpdb; |
|
717 | - return $wpdb->query( |
|
718 | - 'DELETE r FROM ' |
|
719 | - . $this->table() |
|
720 | - . ' r LEFT JOIN ' |
|
721 | - . EEM_Transaction::instance()->table() |
|
722 | - . ' t ON r.TXN_ID = t.TXN_ID WHERE t.TXN_ID IS NULL' |
|
723 | - ); |
|
724 | - } |
|
725 | - |
|
726 | - |
|
727 | - /** |
|
728 | - * Count registrations checked into (or out of) a datetime |
|
729 | - * |
|
730 | - * @param int $DTT_ID datetime ID |
|
731 | - * @param bool $checked_in whether to count registrations checked IN or OUT |
|
732 | - * @return int |
|
733 | - * @throws EE_Error |
|
734 | - * @throws ReflectionException |
|
735 | - * @throws ReflectionException |
|
736 | - */ |
|
737 | - public function count_registrations_checked_into_datetime(int $DTT_ID, bool $checked_in = true): int |
|
738 | - { |
|
739 | - global $wpdb; |
|
740 | - // subquery to get latest checkin |
|
741 | - $query = $wpdb->prepare( |
|
742 | - 'SELECT ' |
|
743 | - . 'COUNT( DISTINCT checkins.REG_ID ) ' |
|
744 | - . 'FROM ' . EEM_Checkin::instance()->table() . ' AS checkins INNER JOIN' |
|
745 | - . '( SELECT ' |
|
746 | - . 'max( CHK_timestamp ) AS latest_checkin, ' |
|
747 | - . 'REG_ID AS REG_ID ' |
|
748 | - . 'FROM ' . EEM_Checkin::instance()->table() . ' ' |
|
749 | - . 'WHERE DTT_ID=%d ' |
|
750 | - . 'GROUP BY REG_ID' |
|
751 | - . ') AS most_recent_checkin_per_reg ' |
|
752 | - . 'ON checkins.REG_ID=most_recent_checkin_per_reg.REG_ID ' |
|
753 | - . 'AND checkins.CHK_timestamp = most_recent_checkin_per_reg.latest_checkin ' |
|
754 | - . 'WHERE ' |
|
755 | - . 'checkins.CHK_in=%d', |
|
756 | - $DTT_ID, |
|
757 | - $checked_in |
|
758 | - ); |
|
759 | - return (int) $wpdb->get_var($query); |
|
760 | - } |
|
761 | - |
|
762 | - |
|
763 | - /** |
|
764 | - * Count registrations checked into (or out of) an event. |
|
765 | - * |
|
766 | - * @param int $EVT_ID event ID |
|
767 | - * @param bool $checked_in whether to count registrations checked IN or OUT |
|
768 | - * @return int |
|
769 | - * @throws EE_Error |
|
770 | - * @throws ReflectionException |
|
771 | - * @throws ReflectionException |
|
772 | - * @throws ReflectionException |
|
773 | - */ |
|
774 | - public function count_registrations_checked_into_event(int $EVT_ID, bool $checked_in = true): int |
|
775 | - { |
|
776 | - global $wpdb; |
|
777 | - // subquery to get latest checkin |
|
778 | - $query = $wpdb->prepare( |
|
779 | - 'SELECT ' |
|
780 | - . 'COUNT( DISTINCT checkins.REG_ID ) ' |
|
781 | - . 'FROM ' . EEM_Checkin::instance()->table() . ' AS checkins INNER JOIN' |
|
782 | - . '( SELECT ' |
|
783 | - . 'max( CHK_timestamp ) AS latest_checkin, ' |
|
784 | - . 'REG_ID AS REG_ID ' |
|
785 | - . 'FROM ' . EEM_Checkin::instance()->table() . ' AS c ' |
|
786 | - . 'INNER JOIN ' . EEM_Datetime::instance()->table() . ' AS d ' |
|
787 | - . 'ON c.DTT_ID=d.DTT_ID ' |
|
788 | - . 'WHERE d.EVT_ID=%d ' |
|
789 | - . 'GROUP BY REG_ID' |
|
790 | - . ') AS most_recent_checkin_per_reg ' |
|
791 | - . 'ON checkins.REG_ID=most_recent_checkin_per_reg.REG_ID ' |
|
792 | - . 'AND checkins.CHK_timestamp = most_recent_checkin_per_reg.latest_checkin ' |
|
793 | - . 'WHERE ' |
|
794 | - . 'checkins.CHK_in=%d', |
|
795 | - $EVT_ID, |
|
796 | - $checked_in |
|
797 | - ); |
|
798 | - return (int) $wpdb->get_var($query); |
|
799 | - } |
|
800 | - |
|
801 | - |
|
802 | - /** |
|
803 | - * The purpose of this method is to retrieve an array of |
|
804 | - * EE_Registration objects that represent the latest registration |
|
805 | - * for each ATT_ID given in the function argument. |
|
806 | - * |
|
807 | - * @param array $attendee_ids |
|
808 | - * @return EE_Registration[]|bool|null |
|
809 | - * @throws EE_Error |
|
810 | - * @throws ReflectionException |
|
811 | - */ |
|
812 | - public function get_latest_registration_for_each_of_given_contacts(array $attendee_ids = []) |
|
813 | - { |
|
814 | - // first do a native wp_query to get the latest REG_ID's matching these attendees. |
|
815 | - global $wpdb; |
|
816 | - $registration_table = $wpdb->prefix . 'esp_registration'; |
|
817 | - $attendee_table = $wpdb->posts; |
|
818 | - $attendee_ids = is_array($attendee_ids) |
|
819 | - ? array_map('absint', $attendee_ids) |
|
820 | - : [(int) $attendee_ids]; |
|
821 | - $ATT_IDs = implode(',', $attendee_ids); |
|
822 | - // first we do a query to get the registration ids |
|
823 | - // (because a group by before order by causes the order by to be ignored.) |
|
824 | - $registration_id_query = " |
|
18 | + /** |
|
19 | + * The value of REG_count for a primary registrant |
|
20 | + */ |
|
21 | + const PRIMARY_REGISTRANT_COUNT = 1; |
|
22 | + |
|
23 | + /** |
|
24 | + * @depecated 5.0.20.p use RegStatus::INCOMPLETE instead |
|
25 | + */ |
|
26 | + const status_id_incomplete = 'RIC'; |
|
27 | + |
|
28 | + /** |
|
29 | + * @depecated 5.0.20.p use RegStatus::AWAITING_REVIEW instead |
|
30 | + */ |
|
31 | + const status_id_not_approved = 'RNA'; |
|
32 | + |
|
33 | + /** |
|
34 | + * @depecated 5.0.20.p use RegStatus::PENDING_PAYMENT instead |
|
35 | + */ |
|
36 | + const status_id_pending_payment = 'RPP'; |
|
37 | + |
|
38 | + /** |
|
39 | + * @depecated 5.0.20.p use RegStatus::WAIT_LIST instead |
|
40 | + */ |
|
41 | + const status_id_wait_list = 'RWL'; |
|
42 | + |
|
43 | + /** |
|
44 | + * @depecated 5.0.20.p use RegStatus::APPROVED instead |
|
45 | + */ |
|
46 | + const status_id_approved = 'RAP'; |
|
47 | + |
|
48 | + /** |
|
49 | + * @depecated 5.0.20.p use RegStatus::CANCELLED instead |
|
50 | + */ |
|
51 | + const status_id_cancelled = 'RCN'; |
|
52 | + |
|
53 | + /** |
|
54 | + * @depecated 5.0.20.p use RegStatus::DECLINED instead |
|
55 | + */ |
|
56 | + const status_id_declined = 'RDC'; |
|
57 | + |
|
58 | + |
|
59 | + protected static ?EEM_Registration $_instance = null; |
|
60 | + |
|
61 | + protected ?TableAnalysis $_table_analysis = null; |
|
62 | + |
|
63 | + /** |
|
64 | + * Keys are the status IDs for registrations (eg, RAP, RCN, etc), and the values |
|
65 | + * are status codes (eg, approved, cancelled, etc) |
|
66 | + * |
|
67 | + * @var array|null |
|
68 | + */ |
|
69 | + private static array $_reg_status = []; |
|
70 | + |
|
71 | + |
|
72 | + /** |
|
73 | + * private constructor to prevent direct creation |
|
74 | + * |
|
75 | + * @param string|null $timezone string representing the timezone we want to set for returned Date Time Strings (and |
|
76 | + * any incoming timezone data that gets saved). Note this just sends the timezone info |
|
77 | + * to the date time model field objects. Default is NULL (and will be assumed using |
|
78 | + * the set timezone in the 'timezone_string' wp option) |
|
79 | + * @throws EE_Error |
|
80 | + * @throws ReflectionException |
|
81 | + */ |
|
82 | + protected function __construct(?string $timezone = '') |
|
83 | + { |
|
84 | + $this->_table_analysis = EE_Registry::instance()->create('TableAnalysis', [], true); |
|
85 | + $this->singular_item = esc_html__('Registration', 'event_espresso'); |
|
86 | + $this->plural_item = esc_html__('Registrations', 'event_espresso'); |
|
87 | + $this->_tables = [ |
|
88 | + 'Registration' => new EE_Primary_Table('esp_registration', 'REG_ID'), |
|
89 | + ]; |
|
90 | + $this->_fields = [ |
|
91 | + 'Registration' => [ |
|
92 | + 'REG_ID' => new EE_Primary_Key_Int_Field( |
|
93 | + 'REG_ID', |
|
94 | + esc_html__('Registration ID', 'event_espresso') |
|
95 | + ), |
|
96 | + 'EVT_ID' => new EE_Foreign_Key_Int_Field( |
|
97 | + 'EVT_ID', |
|
98 | + esc_html__('Event ID', 'event_espresso'), |
|
99 | + false, |
|
100 | + 0, |
|
101 | + 'Event' |
|
102 | + ), |
|
103 | + 'ATT_ID' => new EE_Foreign_Key_Int_Field( |
|
104 | + 'ATT_ID', |
|
105 | + esc_html__('Attendee ID', 'event_espresso'), |
|
106 | + false, |
|
107 | + 0, |
|
108 | + 'Attendee' |
|
109 | + ), |
|
110 | + 'TXN_ID' => new EE_Foreign_Key_Int_Field( |
|
111 | + 'TXN_ID', |
|
112 | + esc_html__('Transaction ID', 'event_espresso'), |
|
113 | + false, |
|
114 | + 0, |
|
115 | + 'Transaction' |
|
116 | + ), |
|
117 | + 'TKT_ID' => new EE_Foreign_Key_Int_Field( |
|
118 | + 'TKT_ID', |
|
119 | + esc_html__('Ticket ID', 'event_espresso'), |
|
120 | + false, |
|
121 | + 0, |
|
122 | + 'Ticket' |
|
123 | + ), |
|
124 | + 'STS_ID' => new EE_Foreign_Key_String_Field( |
|
125 | + 'STS_ID', |
|
126 | + esc_html__('Status ID', 'event_espresso'), |
|
127 | + false, |
|
128 | + RegStatus::INCOMPLETE, |
|
129 | + 'Status' |
|
130 | + ), |
|
131 | + 'REG_date' => new EE_Datetime_Field( |
|
132 | + 'REG_date', |
|
133 | + esc_html__('Time registration occurred', 'event_espresso'), |
|
134 | + false, |
|
135 | + EE_Datetime_Field::now, |
|
136 | + $timezone |
|
137 | + ), |
|
138 | + 'REG_final_price' => new EE_Money_Field( |
|
139 | + 'REG_final_price', |
|
140 | + esc_html__('Registration\'s share of the transaction total', 'event_espresso'), |
|
141 | + false, |
|
142 | + 0 |
|
143 | + ), |
|
144 | + 'REG_paid' => new EE_Money_Field( |
|
145 | + 'REG_paid', |
|
146 | + esc_html__('Amount paid to date towards registration', 'event_espresso'), |
|
147 | + false, |
|
148 | + 0 |
|
149 | + ), |
|
150 | + 'REG_session' => new EE_Plain_Text_Field( |
|
151 | + 'REG_session', |
|
152 | + esc_html__('Session ID of registration', 'event_espresso'), |
|
153 | + false, |
|
154 | + '' |
|
155 | + ), |
|
156 | + 'REG_code' => new EE_Plain_Text_Field( |
|
157 | + 'REG_code', |
|
158 | + esc_html__('Unique Code for this registration', 'event_espresso'), |
|
159 | + false, |
|
160 | + '' |
|
161 | + ), |
|
162 | + 'REG_url_link' => new EE_Plain_Text_Field( |
|
163 | + 'REG_url_link', |
|
164 | + esc_html__('String to be used in URL for identifying registration', 'event_espresso'), |
|
165 | + false, |
|
166 | + '' |
|
167 | + ), |
|
168 | + 'REG_count' => new EE_Integer_Field( |
|
169 | + 'REG_count', |
|
170 | + esc_html__('Count of this registration in the group registration', 'event_espresso'), |
|
171 | + true, |
|
172 | + 1 |
|
173 | + ), |
|
174 | + 'REG_group_size' => new EE_Integer_Field( |
|
175 | + 'REG_group_size', |
|
176 | + esc_html__('Number of registrations on this group', 'event_espresso'), |
|
177 | + false, |
|
178 | + 1 |
|
179 | + ), |
|
180 | + 'REG_att_is_going' => new EE_Boolean_Field( |
|
181 | + 'REG_att_is_going', |
|
182 | + esc_html__('Flag indicating the registrant plans on attending', 'event_espresso'), |
|
183 | + false, |
|
184 | + false |
|
185 | + ), |
|
186 | + 'REG_deleted' => new EE_Trashed_Flag_Field( |
|
187 | + 'REG_deleted', |
|
188 | + esc_html__('Flag indicating if registration has been archived or not.', 'event_espresso'), |
|
189 | + false, |
|
190 | + false |
|
191 | + ), |
|
192 | + ], |
|
193 | + ]; |
|
194 | + $this->_model_relations = [ |
|
195 | + 'Event' => new EE_Belongs_To_Relation(), |
|
196 | + 'Attendee' => new EE_Belongs_To_Relation(), |
|
197 | + 'Transaction' => new EE_Belongs_To_Relation(), |
|
198 | + 'Ticket' => new EE_Belongs_To_Relation(), |
|
199 | + 'Status' => new EE_Belongs_To_Relation(), |
|
200 | + 'Answer' => new EE_Has_Many_Relation(), |
|
201 | + 'Checkin' => new EE_Has_Many_Relation(), |
|
202 | + 'Registration_Payment' => new EE_Has_Many_Relation(), |
|
203 | + 'Payment' => new EE_HABTM_Relation('Registration_Payment'), |
|
204 | + 'Message' => new EE_Has_Many_Any_Relation(false), |
|
205 | + // allow deletes even if there are messages in the queue related |
|
206 | + ]; |
|
207 | + $this->_model_chain_to_wp_user = 'Event'; |
|
208 | + parent::__construct($timezone); |
|
209 | + } |
|
210 | + |
|
211 | + |
|
212 | + /** |
|
213 | + * a list of ALL valid registration statuses currently in use within the system |
|
214 | + * generated by combining the filterable active and inactive reg status arrays |
|
215 | + * |
|
216 | + * @return array |
|
217 | + */ |
|
218 | + public static function reg_statuses(): array |
|
219 | + { |
|
220 | + return array_unique( |
|
221 | + array_merge( |
|
222 | + EEM_Registration::active_reg_statuses(), |
|
223 | + EEM_Registration::inactive_reg_statuses() |
|
224 | + ) |
|
225 | + ); |
|
226 | + } |
|
227 | + |
|
228 | + |
|
229 | + /** |
|
230 | + * reg_statuses_that_allow_payment |
|
231 | + * a filterable list of registration statuses that allow a registrant to make a payment |
|
232 | + * |
|
233 | + * @access public |
|
234 | + * @return array |
|
235 | + */ |
|
236 | + public static function reg_statuses_that_allow_payment(): array |
|
237 | + { |
|
238 | + return (array) apply_filters( |
|
239 | + 'FHEE__EEM_Registration__reg_statuses_that_allow_payment', |
|
240 | + [ |
|
241 | + RegStatus::APPROVED, |
|
242 | + RegStatus::PENDING_PAYMENT, |
|
243 | + ] |
|
244 | + ); |
|
245 | + } |
|
246 | + |
|
247 | + |
|
248 | + /** |
|
249 | + * active_reg_statuses |
|
250 | + * a filterable list of registration statuses that are considered active |
|
251 | + * |
|
252 | + * @access public |
|
253 | + * @return array |
|
254 | + */ |
|
255 | + public static function active_reg_statuses(): array |
|
256 | + { |
|
257 | + return (array) apply_filters( |
|
258 | + 'FHEE__EEM_Registration__active_reg_statuses', |
|
259 | + [ |
|
260 | + RegStatus::APPROVED, |
|
261 | + RegStatus::PENDING_PAYMENT, |
|
262 | + RegStatus::WAIT_LIST, |
|
263 | + RegStatus::AWAITING_REVIEW, |
|
264 | + ] |
|
265 | + ); |
|
266 | + } |
|
267 | + |
|
268 | + |
|
269 | + /** |
|
270 | + * inactive_reg_statuses |
|
271 | + * a filterable list of registration statuses that are not considered active |
|
272 | + * |
|
273 | + * @access public |
|
274 | + * @return array |
|
275 | + */ |
|
276 | + public static function inactive_reg_statuses(): array |
|
277 | + { |
|
278 | + return (array) apply_filters( |
|
279 | + 'FHEE__EEM_Registration__inactive_reg_statuses', |
|
280 | + [ |
|
281 | + RegStatus::INCOMPLETE, |
|
282 | + RegStatus::CANCELLED, |
|
283 | + RegStatus::DECLINED, |
|
284 | + ] |
|
285 | + ); |
|
286 | + } |
|
287 | + |
|
288 | + |
|
289 | + /** |
|
290 | + * closed_reg_statuses |
|
291 | + * a filterable list of registration statuses that are considered "closed" |
|
292 | + * meaning they should not be considered in any calculations involving monies owing |
|
293 | + * |
|
294 | + * @access public |
|
295 | + * @return array |
|
296 | + */ |
|
297 | + public static function closed_reg_statuses(): array |
|
298 | + { |
|
299 | + return (array) apply_filters( |
|
300 | + 'FHEE__EEM_Registration__closed_reg_statuses', |
|
301 | + [ |
|
302 | + RegStatus::CANCELLED, |
|
303 | + RegStatus::DECLINED, |
|
304 | + RegStatus::WAIT_LIST, |
|
305 | + ] |
|
306 | + ); |
|
307 | + } |
|
308 | + |
|
309 | + |
|
310 | + /** |
|
311 | + * get list of registration statuses |
|
312 | + * |
|
313 | + * @param array $exclude The status ids to exclude from the returned results |
|
314 | + * @param bool $translated If true will return the values as singular localized strings |
|
315 | + * @return array |
|
316 | + * @throws EE_Error |
|
317 | + * @throws ReflectionException |
|
318 | + */ |
|
319 | + public static function reg_status_array(array $exclude = [], bool $translated = false): array |
|
320 | + { |
|
321 | + if (empty(self::$_reg_status)) { |
|
322 | + EEM_Registration::instance()->_get_registration_status_array($exclude); |
|
323 | + } elseif ($exclude) { |
|
324 | + foreach ($exclude as $excluded_status) { |
|
325 | + unset(self::$_reg_status[ $excluded_status ]); |
|
326 | + } |
|
327 | + } |
|
328 | + return $translated |
|
329 | + ? EEM_Status::instance()->localized_status(self::$_reg_status, false, 'sentence') |
|
330 | + : self::$_reg_status; |
|
331 | + } |
|
332 | + |
|
333 | + |
|
334 | + /** |
|
335 | + * get list of registration statuses |
|
336 | + * |
|
337 | + * @param array $exclude |
|
338 | + * @param bool $recurse |
|
339 | + * @return void |
|
340 | + * @throws EE_Error |
|
341 | + * @throws ReflectionException |
|
342 | + */ |
|
343 | + private function _get_registration_status_array(array $exclude = [], bool $recurse = true) |
|
344 | + { |
|
345 | + // in the very rare circumstance that we are deleting a model's table's data |
|
346 | + // and the table hasn't actually been created, this could have an error |
|
347 | + /** @type WPDB $wpdb */ |
|
348 | + global $wpdb; |
|
349 | + if ($this->_get_table_analysis()->tableExists($wpdb->prefix . 'esp_status')) { |
|
350 | + $results = $wpdb->get_results( |
|
351 | + "SELECT STS_ID, STS_code FROM {$wpdb->prefix}esp_status WHERE STS_type = 'registration'" |
|
352 | + ); |
|
353 | + self::$_reg_status = []; |
|
354 | + foreach ($results as $status) { |
|
355 | + if (! in_array($status->STS_ID, $exclude, true)) { |
|
356 | + self::$_reg_status[ $status->STS_ID ] = $status->STS_code; |
|
357 | + } |
|
358 | + } |
|
359 | + } |
|
360 | + // in case reg status codes have been deleted from db |
|
361 | + if ($recurse && empty(self::$_reg_status)) { |
|
362 | + EEH_Activation::insert_default_status_codes(); |
|
363 | + $this->_get_registration_status_array($exclude, false); |
|
364 | + } |
|
365 | + } |
|
366 | + |
|
367 | + |
|
368 | + /** |
|
369 | + * Gets the injected table analyzer, or throws an exception |
|
370 | + * |
|
371 | + * @return TableAnalysis |
|
372 | + * @throws EE_Error |
|
373 | + */ |
|
374 | + protected function _get_table_analysis(): TableAnalysis |
|
375 | + { |
|
376 | + if ($this->_table_analysis instanceof TableAnalysis) { |
|
377 | + return $this->_table_analysis; |
|
378 | + } |
|
379 | + throw new EE_Error( |
|
380 | + sprintf( |
|
381 | + esc_html__('Table analysis class on class %1$s is not set properly.', 'event_espresso'), |
|
382 | + get_class($this) |
|
383 | + ) |
|
384 | + ); |
|
385 | + } |
|
386 | + |
|
387 | + |
|
388 | + /** |
|
389 | + * This returns a wpdb->results array of all registration date month and years matching the incoming query params |
|
390 | + * and grouped by month and year. |
|
391 | + * |
|
392 | + * @param array $where_params |
|
393 | + * @return bool|stdClass[] |
|
394 | + * @throws EE_Error |
|
395 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
396 | + */ |
|
397 | + public function get_reg_months_and_years(array $where_params) |
|
398 | + { |
|
399 | + $query_params[0] = $where_params; |
|
400 | + $query_params['group_by'] = ['reg_year', 'reg_month']; |
|
401 | + $query_params['order_by'] = ['REG_date' => 'DESC']; |
|
402 | + $columns_to_select = [ |
|
403 | + 'reg_year' => ['YEAR(REG_date)', '%s'], |
|
404 | + 'reg_month' => ['MONTHNAME(REG_date)', '%s'], |
|
405 | + ]; |
|
406 | + return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select); |
|
407 | + } |
|
408 | + |
|
409 | + |
|
410 | + /** |
|
411 | + * retrieve ALL registrations for a particular Attendee from db |
|
412 | + * |
|
413 | + * @param int $ATT_ID |
|
414 | + * @return EE_Registration[]|bool|null |
|
415 | + * @throws EE_Error |
|
416 | + * @throws ReflectionException |
|
417 | + */ |
|
418 | + public function get_all_registrations_for_attendee(int $ATT_ID = 0): ?array |
|
419 | + { |
|
420 | + if (! $ATT_ID) { |
|
421 | + return null; |
|
422 | + } |
|
423 | + return $this->get_all([['ATT_ID' => $ATT_ID]]); |
|
424 | + } |
|
425 | + |
|
426 | + |
|
427 | + /** |
|
428 | + * Gets a registration given their REG_url_link. Yes, this should usually |
|
429 | + * be passed via a GET parameter. |
|
430 | + * |
|
431 | + * @param string $REG_url_link |
|
432 | + * @return EE_Registration|null |
|
433 | + * @throws EE_Error |
|
434 | + */ |
|
435 | + public function get_registration_for_reg_url_link(string $REG_url_link): ?EE_Registration |
|
436 | + { |
|
437 | + if (! $REG_url_link) { |
|
438 | + return null; |
|
439 | + } |
|
440 | + return $this->get_one([['REG_url_link' => $REG_url_link]]); |
|
441 | + } |
|
442 | + |
|
443 | + |
|
444 | + /** |
|
445 | + * retrieve registration for a specific transaction attendee from db |
|
446 | + * |
|
447 | + * @access public |
|
448 | + * @param int $TXN_ID |
|
449 | + * @param int $ATT_ID |
|
450 | + * @param int $att_nmbr in case the ATT_ID is the same for multiple registrations (same details used) then the |
|
451 | + * attendee number is required |
|
452 | + * @return EE_Registration|null array on success, FALSE on fail |
|
453 | + * @throws EE_Error |
|
454 | + */ |
|
455 | + public function get_registration_for_transaction_attendee(int $TXN_ID = 0, int $ATT_ID = 0, int $att_nmbr = 0): |
|
456 | + ?EE_Registration |
|
457 | + { |
|
458 | + return $this->get_one( |
|
459 | + [ |
|
460 | + [ |
|
461 | + 'TXN_ID' => $TXN_ID, |
|
462 | + 'ATT_ID' => $ATT_ID, |
|
463 | + ], |
|
464 | + 'limit' => [min($att_nmbr - 1, 0), 1], |
|
465 | + ] |
|
466 | + ); |
|
467 | + } |
|
468 | + |
|
469 | + |
|
470 | + /** |
|
471 | + * get the number of registrations per day for the Registration Admin page Reports Tab. |
|
472 | + * (doesn't utilize models because it's a fairly specialized query) |
|
473 | + * |
|
474 | + * @access public |
|
475 | + * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
|
476 | + * @return EE_Registration[]|bool|null with properties regDate and total |
|
477 | + * @throws EE_Error |
|
478 | + */ |
|
479 | + public function get_registrations_per_day_report(string $period = '-1 month') |
|
480 | + { |
|
481 | + $sql_date = $this->convert_datetime_for_query( |
|
482 | + 'REG_date', |
|
483 | + date('Y-m-d H:i:s', strtotime($period)), |
|
484 | + 'Y-m-d H:i:s', |
|
485 | + 'UTC' |
|
486 | + ); |
|
487 | + $where = [ |
|
488 | + 'REG_date' => ['>=', $sql_date], |
|
489 | + 'STS_ID' => ['!=', RegStatus::INCOMPLETE], |
|
490 | + ]; |
|
491 | + if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_day_report')) { |
|
492 | + $where['Event.EVT_wp_user'] = get_current_user_id(); |
|
493 | + } |
|
494 | + $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'REG_date'); |
|
495 | + return $this->_get_all_wpdb_results( |
|
496 | + [ |
|
497 | + $where, |
|
498 | + 'group_by' => 'regDate', |
|
499 | + 'order_by' => ['REG_date' => 'ASC'], |
|
500 | + ], |
|
501 | + OBJECT, |
|
502 | + [ |
|
503 | + 'regDate' => ['DATE(' . $query_interval . ')', '%s'], |
|
504 | + 'total' => ['count(REG_ID)', '%d'], |
|
505 | + ] |
|
506 | + ); |
|
507 | + } |
|
508 | + |
|
509 | + |
|
510 | + /** |
|
511 | + * Get the number of registrations per day including the count of registrations for each Registration Status. |
|
512 | + * Note: RegStatus::INCOMPLETE registrations are excluded from the results. |
|
513 | + * |
|
514 | + * @param string $period |
|
515 | + * @return stdClass[] with properties Registration_REG_date and a column for each registration status as the STS_ID |
|
516 | + * @throws EE_Error |
|
517 | + * (i.e. RAP) |
|
518 | + * @throws EE_Error|ReflectionException |
|
519 | + */ |
|
520 | + public function get_registrations_per_day_and_per_status_report(string $period = '-1 month'): array |
|
521 | + { |
|
522 | + global $wpdb; |
|
523 | + $registration_table = $wpdb->prefix . 'esp_registration'; |
|
524 | + $event_table = $wpdb->posts; |
|
525 | + $sql_date = date('Y-m-d H:i:s', strtotime($period)); |
|
526 | + // prepare the query interval for displaying offset |
|
527 | + $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'dates.REG_date'); |
|
528 | + // inner date query |
|
529 | + $inner_date_query = "SELECT DISTINCT REG_date from $registration_table "; |
|
530 | + $inner_where = ' WHERE'; |
|
531 | + // exclude events not authored by user if permissions in effect |
|
532 | + if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
533 | + $inner_date_query .= "LEFT JOIN $event_table ON ID = EVT_ID"; |
|
534 | + $inner_where .= ' post_author = ' . get_current_user_id() . ' AND'; |
|
535 | + } |
|
536 | + $inner_where .= " REG_date >= '$sql_date'"; |
|
537 | + $inner_date_query .= $inner_where; |
|
538 | + // start main query |
|
539 | + $select = "SELECT DATE($query_interval) as Registration_REG_date, "; |
|
540 | + $join = ''; |
|
541 | + $join_parts = []; |
|
542 | + $select_parts = []; |
|
543 | + // loop through registration stati to do parts for each status. |
|
544 | + foreach (EEM_Registration::reg_status_array() as $STS_ID => $STS_code) { |
|
545 | + if ($STS_ID === RegStatus::INCOMPLETE) { |
|
546 | + continue; |
|
547 | + } |
|
548 | + $select_parts[] = "COUNT($STS_code.REG_ID) as $STS_ID"; |
|
549 | + $join_parts[] = |
|
550 | + "$registration_table AS $STS_code ON $STS_code.REG_date = dates.REG_date AND $STS_code.STS_ID = '$STS_ID'"; |
|
551 | + } |
|
552 | + // setup the selects |
|
553 | + $select .= implode(', ', $select_parts); |
|
554 | + $select .= " FROM ($inner_date_query) AS dates LEFT JOIN "; |
|
555 | + // setup the joins |
|
556 | + $join .= implode(' LEFT JOIN ', $join_parts); |
|
557 | + // now let's put it all together |
|
558 | + $query = $select . $join . ' GROUP BY Registration_REG_date'; |
|
559 | + // and execute it |
|
560 | + return $wpdb->get_results($query, ARRAY_A); |
|
561 | + } |
|
562 | + |
|
563 | + |
|
564 | + /** |
|
565 | + * get the number of registrations per event for the Registration Admin page Reports Tab |
|
566 | + * |
|
567 | + * @access public |
|
568 | + * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
|
569 | + * @return stdClass[] each with properties event_name, reg_limit, and total |
|
570 | + * @throws EE_Error |
|
571 | + */ |
|
572 | + public function get_registrations_per_event_report(string $period = '-1 month'): array |
|
573 | + { |
|
574 | + $date_sql = $this->convert_datetime_for_query( |
|
575 | + 'REG_date', |
|
576 | + date('Y-m-d H:i:s', strtotime($period)), |
|
577 | + 'Y-m-d H:i:s', |
|
578 | + 'UTC' |
|
579 | + ); |
|
580 | + $where = [ |
|
581 | + 'REG_date' => ['>=', $date_sql], |
|
582 | + 'STS_ID' => ['!=', RegStatus::INCOMPLETE], |
|
583 | + ]; |
|
584 | + if ( |
|
585 | + ! EE_Registry::instance()->CAP->current_user_can( |
|
586 | + 'ee_read_others_registrations', |
|
587 | + 'reg_per_event_report' |
|
588 | + ) |
|
589 | + ) { |
|
590 | + $where['Event.EVT_wp_user'] = get_current_user_id(); |
|
591 | + } |
|
592 | + return $this->_get_all_wpdb_results( |
|
593 | + [ |
|
594 | + $where, |
|
595 | + 'group_by' => 'Event.EVT_name', |
|
596 | + 'order_by' => 'Event.EVT_name', |
|
597 | + 'limit' => [0, 24], |
|
598 | + ], |
|
599 | + OBJECT, |
|
600 | + [ |
|
601 | + 'event_name' => ['Event_CPT.post_title', '%s'], |
|
602 | + 'total' => ['COUNT(REG_ID)', '%s'], |
|
603 | + ] |
|
604 | + ); |
|
605 | + } |
|
606 | + |
|
607 | + |
|
608 | + /** |
|
609 | + * Get the number of registrations per event grouped by registration status. |
|
610 | + * Note: RegStatus::INCOMPLETE registrations are excluded from the results. |
|
611 | + * |
|
612 | + * @param string $period |
|
613 | + * @return stdClass[] with properties `Registration_Event` and a column for each registration status as the STS_ID |
|
614 | + * (i.e. RAP) |
|
615 | + * @throws EE_Error |
|
616 | + * @throws ReflectionException |
|
617 | + */ |
|
618 | + public function get_registrations_per_event_and_per_status_report(string $period = '-1 month'): array |
|
619 | + { |
|
620 | + global $wpdb; |
|
621 | + $registration_table = $wpdb->prefix . 'esp_registration'; |
|
622 | + $event_table = $wpdb->posts; |
|
623 | + $sql_date = date('Y-m-d H:i:s', strtotime($period)); |
|
624 | + // inner date query |
|
625 | + $inner_date_query = "SELECT DISTINCT EVT_ID, REG_date from $registration_table "; |
|
626 | + $inner_where = ' WHERE'; |
|
627 | + // exclude events not authored by user if permissions in effect |
|
628 | + if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
629 | + $inner_date_query .= "LEFT JOIN $event_table ON ID = EVT_ID"; |
|
630 | + $inner_where .= ' post_author = ' . get_current_user_id() . ' AND'; |
|
631 | + } |
|
632 | + $inner_where .= " REG_date >= '$sql_date'"; |
|
633 | + $inner_date_query .= $inner_where; |
|
634 | + // build main query |
|
635 | + $select = 'SELECT Event.post_title as Registration_Event, '; |
|
636 | + $join = ''; |
|
637 | + $join_parts = []; |
|
638 | + $select_parts = []; |
|
639 | + // loop through registration stati to do parts for each status. |
|
640 | + foreach (EEM_Registration::reg_status_array() as $STS_ID => $STS_code) { |
|
641 | + if ($STS_ID === RegStatus::INCOMPLETE) { |
|
642 | + continue; |
|
643 | + } |
|
644 | + $select_parts[] = "COUNT($STS_code.REG_ID) as $STS_ID"; |
|
645 | + $join_parts[] = |
|
646 | + "$registration_table AS $STS_code ON $STS_code.EVT_ID = dates.EVT_ID AND $STS_code.STS_ID = '$STS_ID' AND $STS_code.REG_date = dates.REG_date"; |
|
647 | + } |
|
648 | + // setup the selects |
|
649 | + $select .= implode(', ', $select_parts); |
|
650 | + $select .= " FROM ($inner_date_query) AS dates LEFT JOIN $event_table as Event ON Event.ID = dates.EVT_ID LEFT JOIN "; |
|
651 | + // setup remaining joins |
|
652 | + $join .= implode(' LEFT JOIN ', $join_parts); |
|
653 | + // now put it all together |
|
654 | + $query = $select . $join . ' GROUP BY Registration_Event'; |
|
655 | + // and execute |
|
656 | + return $wpdb->get_results($query, ARRAY_A); |
|
657 | + } |
|
658 | + |
|
659 | + |
|
660 | + /** |
|
661 | + * Returns the EE_Registration of the primary attendee on the transaction id provided |
|
662 | + * |
|
663 | + * @param int $TXN_ID |
|
664 | + * @return EE_Registration|null |
|
665 | + * @throws EE_Error |
|
666 | + */ |
|
667 | + public function get_primary_registration_for_transaction_ID(int $TXN_ID = 0): ?EE_Registration |
|
668 | + { |
|
669 | + if (! $TXN_ID) { |
|
670 | + return null; |
|
671 | + } |
|
672 | + return $this->get_one( |
|
673 | + [ |
|
674 | + [ |
|
675 | + 'TXN_ID' => $TXN_ID, |
|
676 | + 'REG_count' => EEM_Registration::PRIMARY_REGISTRANT_COUNT, |
|
677 | + ], |
|
678 | + ] |
|
679 | + ); |
|
680 | + } |
|
681 | + |
|
682 | + |
|
683 | + /** |
|
684 | + * get_event_registration_count |
|
685 | + * |
|
686 | + * @access public |
|
687 | + * @param int $EVT_ID |
|
688 | + * @param boolean $for_incomplete_payments |
|
689 | + * @return int |
|
690 | + * @throws EE_Error |
|
691 | + */ |
|
692 | + public function get_event_registration_count(int $EVT_ID, bool $for_incomplete_payments = false): int |
|
693 | + { |
|
694 | + // we only count approved registrations towards registration limits |
|
695 | + $query_params = [['EVT_ID' => $EVT_ID, 'STS_ID' => RegStatus::APPROVED]]; |
|
696 | + if ($for_incomplete_payments) { |
|
697 | + $query_params[0]['Transaction.STS_ID'] = ['!=', EEM_Transaction::complete_status_code]; |
|
698 | + } |
|
699 | + return $this->count($query_params); |
|
700 | + } |
|
701 | + |
|
702 | + |
|
703 | + /** |
|
704 | + * Deletes all registrations with no transactions. Note that this needs to be very efficient |
|
705 | + * and so it uses wpdb directly. Also, we can't put a limit on this because MySQL doesn't allow a limit on a delete |
|
706 | + * when joining tables like this. |
|
707 | + * |
|
708 | + * @return int|bool number deleted |
|
709 | + * @throws EE_Error |
|
710 | + * @throws ReflectionException |
|
711 | + * @global WPDB $wpdb |
|
712 | + */ |
|
713 | + public function delete_registrations_with_no_transaction() |
|
714 | + { |
|
715 | + /** @type WPDB $wpdb */ |
|
716 | + global $wpdb; |
|
717 | + return $wpdb->query( |
|
718 | + 'DELETE r FROM ' |
|
719 | + . $this->table() |
|
720 | + . ' r LEFT JOIN ' |
|
721 | + . EEM_Transaction::instance()->table() |
|
722 | + . ' t ON r.TXN_ID = t.TXN_ID WHERE t.TXN_ID IS NULL' |
|
723 | + ); |
|
724 | + } |
|
725 | + |
|
726 | + |
|
727 | + /** |
|
728 | + * Count registrations checked into (or out of) a datetime |
|
729 | + * |
|
730 | + * @param int $DTT_ID datetime ID |
|
731 | + * @param bool $checked_in whether to count registrations checked IN or OUT |
|
732 | + * @return int |
|
733 | + * @throws EE_Error |
|
734 | + * @throws ReflectionException |
|
735 | + * @throws ReflectionException |
|
736 | + */ |
|
737 | + public function count_registrations_checked_into_datetime(int $DTT_ID, bool $checked_in = true): int |
|
738 | + { |
|
739 | + global $wpdb; |
|
740 | + // subquery to get latest checkin |
|
741 | + $query = $wpdb->prepare( |
|
742 | + 'SELECT ' |
|
743 | + . 'COUNT( DISTINCT checkins.REG_ID ) ' |
|
744 | + . 'FROM ' . EEM_Checkin::instance()->table() . ' AS checkins INNER JOIN' |
|
745 | + . '( SELECT ' |
|
746 | + . 'max( CHK_timestamp ) AS latest_checkin, ' |
|
747 | + . 'REG_ID AS REG_ID ' |
|
748 | + . 'FROM ' . EEM_Checkin::instance()->table() . ' ' |
|
749 | + . 'WHERE DTT_ID=%d ' |
|
750 | + . 'GROUP BY REG_ID' |
|
751 | + . ') AS most_recent_checkin_per_reg ' |
|
752 | + . 'ON checkins.REG_ID=most_recent_checkin_per_reg.REG_ID ' |
|
753 | + . 'AND checkins.CHK_timestamp = most_recent_checkin_per_reg.latest_checkin ' |
|
754 | + . 'WHERE ' |
|
755 | + . 'checkins.CHK_in=%d', |
|
756 | + $DTT_ID, |
|
757 | + $checked_in |
|
758 | + ); |
|
759 | + return (int) $wpdb->get_var($query); |
|
760 | + } |
|
761 | + |
|
762 | + |
|
763 | + /** |
|
764 | + * Count registrations checked into (or out of) an event. |
|
765 | + * |
|
766 | + * @param int $EVT_ID event ID |
|
767 | + * @param bool $checked_in whether to count registrations checked IN or OUT |
|
768 | + * @return int |
|
769 | + * @throws EE_Error |
|
770 | + * @throws ReflectionException |
|
771 | + * @throws ReflectionException |
|
772 | + * @throws ReflectionException |
|
773 | + */ |
|
774 | + public function count_registrations_checked_into_event(int $EVT_ID, bool $checked_in = true): int |
|
775 | + { |
|
776 | + global $wpdb; |
|
777 | + // subquery to get latest checkin |
|
778 | + $query = $wpdb->prepare( |
|
779 | + 'SELECT ' |
|
780 | + . 'COUNT( DISTINCT checkins.REG_ID ) ' |
|
781 | + . 'FROM ' . EEM_Checkin::instance()->table() . ' AS checkins INNER JOIN' |
|
782 | + . '( SELECT ' |
|
783 | + . 'max( CHK_timestamp ) AS latest_checkin, ' |
|
784 | + . 'REG_ID AS REG_ID ' |
|
785 | + . 'FROM ' . EEM_Checkin::instance()->table() . ' AS c ' |
|
786 | + . 'INNER JOIN ' . EEM_Datetime::instance()->table() . ' AS d ' |
|
787 | + . 'ON c.DTT_ID=d.DTT_ID ' |
|
788 | + . 'WHERE d.EVT_ID=%d ' |
|
789 | + . 'GROUP BY REG_ID' |
|
790 | + . ') AS most_recent_checkin_per_reg ' |
|
791 | + . 'ON checkins.REG_ID=most_recent_checkin_per_reg.REG_ID ' |
|
792 | + . 'AND checkins.CHK_timestamp = most_recent_checkin_per_reg.latest_checkin ' |
|
793 | + . 'WHERE ' |
|
794 | + . 'checkins.CHK_in=%d', |
|
795 | + $EVT_ID, |
|
796 | + $checked_in |
|
797 | + ); |
|
798 | + return (int) $wpdb->get_var($query); |
|
799 | + } |
|
800 | + |
|
801 | + |
|
802 | + /** |
|
803 | + * The purpose of this method is to retrieve an array of |
|
804 | + * EE_Registration objects that represent the latest registration |
|
805 | + * for each ATT_ID given in the function argument. |
|
806 | + * |
|
807 | + * @param array $attendee_ids |
|
808 | + * @return EE_Registration[]|bool|null |
|
809 | + * @throws EE_Error |
|
810 | + * @throws ReflectionException |
|
811 | + */ |
|
812 | + public function get_latest_registration_for_each_of_given_contacts(array $attendee_ids = []) |
|
813 | + { |
|
814 | + // first do a native wp_query to get the latest REG_ID's matching these attendees. |
|
815 | + global $wpdb; |
|
816 | + $registration_table = $wpdb->prefix . 'esp_registration'; |
|
817 | + $attendee_table = $wpdb->posts; |
|
818 | + $attendee_ids = is_array($attendee_ids) |
|
819 | + ? array_map('absint', $attendee_ids) |
|
820 | + : [(int) $attendee_ids]; |
|
821 | + $ATT_IDs = implode(',', $attendee_ids); |
|
822 | + // first we do a query to get the registration ids |
|
823 | + // (because a group by before order by causes the order by to be ignored.) |
|
824 | + $registration_id_query = " |
|
825 | 825 | SELECT registrations.registration_ids as registration_id |
826 | 826 | FROM ( |
827 | 827 | SELECT |
@@ -835,61 +835,61 @@ discard block |
||
835 | 835 | ) AS registrations |
836 | 836 | GROUP BY registrations.attendee_ids |
837 | 837 | "; |
838 | - $registration_ids = $wpdb->get_results($registration_id_query, ARRAY_A); |
|
839 | - if (empty($registration_ids)) { |
|
840 | - return []; |
|
841 | - } |
|
842 | - $ids_for_model_query = []; |
|
843 | - // let's flatten the ids so they can be used in the model query. |
|
844 | - foreach ($registration_ids as $registration_id) { |
|
845 | - if (isset($registration_id['registration_id'])) { |
|
846 | - $ids_for_model_query[] = $registration_id['registration_id']; |
|
847 | - } |
|
848 | - } |
|
849 | - // construct query |
|
850 | - $_where = [ |
|
851 | - 'REG_ID' => ['IN', $ids_for_model_query], |
|
852 | - ]; |
|
853 | - return $this->get_all([$_where]); |
|
854 | - } |
|
855 | - |
|
856 | - |
|
857 | - /** |
|
858 | - * returns a count of registrations for the supplied event having the status as specified |
|
859 | - * |
|
860 | - * @param int $EVT_ID |
|
861 | - * @param array|string $statuses |
|
862 | - * @return int |
|
863 | - * @throws InvalidArgumentException |
|
864 | - * @throws InvalidStatusException |
|
865 | - * @throws EE_Error |
|
866 | - */ |
|
867 | - public function event_reg_count_for_statuses(int $EVT_ID, $statuses = []): int |
|
868 | - { |
|
869 | - $EVT_ID = absint($EVT_ID); |
|
870 | - if (! $EVT_ID) { |
|
871 | - throw new InvalidArgumentException( |
|
872 | - esc_html__('An invalid Event ID was supplied.', 'event_espresso') |
|
873 | - ); |
|
874 | - } |
|
875 | - $statuses = is_array($statuses) ? $statuses : [$statuses]; |
|
876 | - $statuses = ! empty($statuses) ? $statuses : [RegStatus::APPROVED]; |
|
877 | - |
|
878 | - $valid_reg_statuses = EEM_Registration::reg_statuses(); |
|
879 | - foreach ($statuses as $status) { |
|
880 | - if (! in_array($status, $valid_reg_statuses, true)) { |
|
881 | - throw new InvalidStatusException($status, esc_html__('Registration', 'event_espresso')); |
|
882 | - } |
|
883 | - } |
|
884 | - return $this->count( |
|
885 | - [ |
|
886 | - [ |
|
887 | - 'EVT_ID' => $EVT_ID, |
|
888 | - 'STS_ID' => ['IN', $statuses], |
|
889 | - ], |
|
890 | - ], |
|
891 | - 'REG_ID', |
|
892 | - true |
|
893 | - ); |
|
894 | - } |
|
838 | + $registration_ids = $wpdb->get_results($registration_id_query, ARRAY_A); |
|
839 | + if (empty($registration_ids)) { |
|
840 | + return []; |
|
841 | + } |
|
842 | + $ids_for_model_query = []; |
|
843 | + // let's flatten the ids so they can be used in the model query. |
|
844 | + foreach ($registration_ids as $registration_id) { |
|
845 | + if (isset($registration_id['registration_id'])) { |
|
846 | + $ids_for_model_query[] = $registration_id['registration_id']; |
|
847 | + } |
|
848 | + } |
|
849 | + // construct query |
|
850 | + $_where = [ |
|
851 | + 'REG_ID' => ['IN', $ids_for_model_query], |
|
852 | + ]; |
|
853 | + return $this->get_all([$_where]); |
|
854 | + } |
|
855 | + |
|
856 | + |
|
857 | + /** |
|
858 | + * returns a count of registrations for the supplied event having the status as specified |
|
859 | + * |
|
860 | + * @param int $EVT_ID |
|
861 | + * @param array|string $statuses |
|
862 | + * @return int |
|
863 | + * @throws InvalidArgumentException |
|
864 | + * @throws InvalidStatusException |
|
865 | + * @throws EE_Error |
|
866 | + */ |
|
867 | + public function event_reg_count_for_statuses(int $EVT_ID, $statuses = []): int |
|
868 | + { |
|
869 | + $EVT_ID = absint($EVT_ID); |
|
870 | + if (! $EVT_ID) { |
|
871 | + throw new InvalidArgumentException( |
|
872 | + esc_html__('An invalid Event ID was supplied.', 'event_espresso') |
|
873 | + ); |
|
874 | + } |
|
875 | + $statuses = is_array($statuses) ? $statuses : [$statuses]; |
|
876 | + $statuses = ! empty($statuses) ? $statuses : [RegStatus::APPROVED]; |
|
877 | + |
|
878 | + $valid_reg_statuses = EEM_Registration::reg_statuses(); |
|
879 | + foreach ($statuses as $status) { |
|
880 | + if (! in_array($status, $valid_reg_statuses, true)) { |
|
881 | + throw new InvalidStatusException($status, esc_html__('Registration', 'event_espresso')); |
|
882 | + } |
|
883 | + } |
|
884 | + return $this->count( |
|
885 | + [ |
|
886 | + [ |
|
887 | + 'EVT_ID' => $EVT_ID, |
|
888 | + 'STS_ID' => ['IN', $statuses], |
|
889 | + ], |
|
890 | + ], |
|
891 | + 'REG_ID', |
|
892 | + true |
|
893 | + ); |
|
894 | + } |
|
895 | 895 | } |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | const status_id_declined = 'RDC'; |
57 | 57 | |
58 | 58 | |
59 | - protected static ?EEM_Registration $_instance = null; |
|
59 | + protected static ?EEM_Registration $_instance = null; |
|
60 | 60 | |
61 | 61 | protected ?TableAnalysis $_table_analysis = null; |
62 | 62 | |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | ), |
192 | 192 | ], |
193 | 193 | ]; |
194 | - $this->_model_relations = [ |
|
194 | + $this->_model_relations = [ |
|
195 | 195 | 'Event' => new EE_Belongs_To_Relation(), |
196 | 196 | 'Attendee' => new EE_Belongs_To_Relation(), |
197 | 197 | 'Transaction' => new EE_Belongs_To_Relation(), |
@@ -322,7 +322,7 @@ discard block |
||
322 | 322 | EEM_Registration::instance()->_get_registration_status_array($exclude); |
323 | 323 | } elseif ($exclude) { |
324 | 324 | foreach ($exclude as $excluded_status) { |
325 | - unset(self::$_reg_status[ $excluded_status ]); |
|
325 | + unset(self::$_reg_status[$excluded_status]); |
|
326 | 326 | } |
327 | 327 | } |
328 | 328 | return $translated |
@@ -346,14 +346,14 @@ discard block |
||
346 | 346 | // and the table hasn't actually been created, this could have an error |
347 | 347 | /** @type WPDB $wpdb */ |
348 | 348 | global $wpdb; |
349 | - if ($this->_get_table_analysis()->tableExists($wpdb->prefix . 'esp_status')) { |
|
349 | + if ($this->_get_table_analysis()->tableExists($wpdb->prefix.'esp_status')) { |
|
350 | 350 | $results = $wpdb->get_results( |
351 | 351 | "SELECT STS_ID, STS_code FROM {$wpdb->prefix}esp_status WHERE STS_type = 'registration'" |
352 | 352 | ); |
353 | 353 | self::$_reg_status = []; |
354 | 354 | foreach ($results as $status) { |
355 | - if (! in_array($status->STS_ID, $exclude, true)) { |
|
356 | - self::$_reg_status[ $status->STS_ID ] = $status->STS_code; |
|
355 | + if ( ! in_array($status->STS_ID, $exclude, true)) { |
|
356 | + self::$_reg_status[$status->STS_ID] = $status->STS_code; |
|
357 | 357 | } |
358 | 358 | } |
359 | 359 | } |
@@ -417,7 +417,7 @@ discard block |
||
417 | 417 | */ |
418 | 418 | public function get_all_registrations_for_attendee(int $ATT_ID = 0): ?array |
419 | 419 | { |
420 | - if (! $ATT_ID) { |
|
420 | + if ( ! $ATT_ID) { |
|
421 | 421 | return null; |
422 | 422 | } |
423 | 423 | return $this->get_all([['ATT_ID' => $ATT_ID]]); |
@@ -434,7 +434,7 @@ discard block |
||
434 | 434 | */ |
435 | 435 | public function get_registration_for_reg_url_link(string $REG_url_link): ?EE_Registration |
436 | 436 | { |
437 | - if (! $REG_url_link) { |
|
437 | + if ( ! $REG_url_link) { |
|
438 | 438 | return null; |
439 | 439 | } |
440 | 440 | return $this->get_one([['REG_url_link' => $REG_url_link]]); |
@@ -484,11 +484,11 @@ discard block |
||
484 | 484 | 'Y-m-d H:i:s', |
485 | 485 | 'UTC' |
486 | 486 | ); |
487 | - $where = [ |
|
487 | + $where = [ |
|
488 | 488 | 'REG_date' => ['>=', $sql_date], |
489 | 489 | 'STS_ID' => ['!=', RegStatus::INCOMPLETE], |
490 | 490 | ]; |
491 | - if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_day_report')) { |
|
491 | + if ( ! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_day_report')) { |
|
492 | 492 | $where['Event.EVT_wp_user'] = get_current_user_id(); |
493 | 493 | } |
494 | 494 | $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'REG_date'); |
@@ -500,7 +500,7 @@ discard block |
||
500 | 500 | ], |
501 | 501 | OBJECT, |
502 | 502 | [ |
503 | - 'regDate' => ['DATE(' . $query_interval . ')', '%s'], |
|
503 | + 'regDate' => ['DATE('.$query_interval.')', '%s'], |
|
504 | 504 | 'total' => ['count(REG_ID)', '%d'], |
505 | 505 | ] |
506 | 506 | ); |
@@ -520,7 +520,7 @@ discard block |
||
520 | 520 | public function get_registrations_per_day_and_per_status_report(string $period = '-1 month'): array |
521 | 521 | { |
522 | 522 | global $wpdb; |
523 | - $registration_table = $wpdb->prefix . 'esp_registration'; |
|
523 | + $registration_table = $wpdb->prefix.'esp_registration'; |
|
524 | 524 | $event_table = $wpdb->posts; |
525 | 525 | $sql_date = date('Y-m-d H:i:s', strtotime($period)); |
526 | 526 | // prepare the query interval for displaying offset |
@@ -529,9 +529,9 @@ discard block |
||
529 | 529 | $inner_date_query = "SELECT DISTINCT REG_date from $registration_table "; |
530 | 530 | $inner_where = ' WHERE'; |
531 | 531 | // exclude events not authored by user if permissions in effect |
532 | - if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
532 | + if ( ! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
533 | 533 | $inner_date_query .= "LEFT JOIN $event_table ON ID = EVT_ID"; |
534 | - $inner_where .= ' post_author = ' . get_current_user_id() . ' AND'; |
|
534 | + $inner_where .= ' post_author = '.get_current_user_id().' AND'; |
|
535 | 535 | } |
536 | 536 | $inner_where .= " REG_date >= '$sql_date'"; |
537 | 537 | $inner_date_query .= $inner_where; |
@@ -555,7 +555,7 @@ discard block |
||
555 | 555 | // setup the joins |
556 | 556 | $join .= implode(' LEFT JOIN ', $join_parts); |
557 | 557 | // now let's put it all together |
558 | - $query = $select . $join . ' GROUP BY Registration_REG_date'; |
|
558 | + $query = $select.$join.' GROUP BY Registration_REG_date'; |
|
559 | 559 | // and execute it |
560 | 560 | return $wpdb->get_results($query, ARRAY_A); |
561 | 561 | } |
@@ -577,7 +577,7 @@ discard block |
||
577 | 577 | 'Y-m-d H:i:s', |
578 | 578 | 'UTC' |
579 | 579 | ); |
580 | - $where = [ |
|
580 | + $where = [ |
|
581 | 581 | 'REG_date' => ['>=', $date_sql], |
582 | 582 | 'STS_ID' => ['!=', RegStatus::INCOMPLETE], |
583 | 583 | ]; |
@@ -618,16 +618,16 @@ discard block |
||
618 | 618 | public function get_registrations_per_event_and_per_status_report(string $period = '-1 month'): array |
619 | 619 | { |
620 | 620 | global $wpdb; |
621 | - $registration_table = $wpdb->prefix . 'esp_registration'; |
|
621 | + $registration_table = $wpdb->prefix.'esp_registration'; |
|
622 | 622 | $event_table = $wpdb->posts; |
623 | 623 | $sql_date = date('Y-m-d H:i:s', strtotime($period)); |
624 | 624 | // inner date query |
625 | 625 | $inner_date_query = "SELECT DISTINCT EVT_ID, REG_date from $registration_table "; |
626 | 626 | $inner_where = ' WHERE'; |
627 | 627 | // exclude events not authored by user if permissions in effect |
628 | - if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
628 | + if ( ! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) { |
|
629 | 629 | $inner_date_query .= "LEFT JOIN $event_table ON ID = EVT_ID"; |
630 | - $inner_where .= ' post_author = ' . get_current_user_id() . ' AND'; |
|
630 | + $inner_where .= ' post_author = '.get_current_user_id().' AND'; |
|
631 | 631 | } |
632 | 632 | $inner_where .= " REG_date >= '$sql_date'"; |
633 | 633 | $inner_date_query .= $inner_where; |
@@ -651,7 +651,7 @@ discard block |
||
651 | 651 | // setup remaining joins |
652 | 652 | $join .= implode(' LEFT JOIN ', $join_parts); |
653 | 653 | // now put it all together |
654 | - $query = $select . $join . ' GROUP BY Registration_Event'; |
|
654 | + $query = $select.$join.' GROUP BY Registration_Event'; |
|
655 | 655 | // and execute |
656 | 656 | return $wpdb->get_results($query, ARRAY_A); |
657 | 657 | } |
@@ -666,7 +666,7 @@ discard block |
||
666 | 666 | */ |
667 | 667 | public function get_primary_registration_for_transaction_ID(int $TXN_ID = 0): ?EE_Registration |
668 | 668 | { |
669 | - if (! $TXN_ID) { |
|
669 | + if ( ! $TXN_ID) { |
|
670 | 670 | return null; |
671 | 671 | } |
672 | 672 | return $this->get_one( |
@@ -741,11 +741,11 @@ discard block |
||
741 | 741 | $query = $wpdb->prepare( |
742 | 742 | 'SELECT ' |
743 | 743 | . 'COUNT( DISTINCT checkins.REG_ID ) ' |
744 | - . 'FROM ' . EEM_Checkin::instance()->table() . ' AS checkins INNER JOIN' |
|
744 | + . 'FROM '.EEM_Checkin::instance()->table().' AS checkins INNER JOIN' |
|
745 | 745 | . '( SELECT ' |
746 | 746 | . 'max( CHK_timestamp ) AS latest_checkin, ' |
747 | 747 | . 'REG_ID AS REG_ID ' |
748 | - . 'FROM ' . EEM_Checkin::instance()->table() . ' ' |
|
748 | + . 'FROM '.EEM_Checkin::instance()->table().' ' |
|
749 | 749 | . 'WHERE DTT_ID=%d ' |
750 | 750 | . 'GROUP BY REG_ID' |
751 | 751 | . ') AS most_recent_checkin_per_reg ' |
@@ -778,12 +778,12 @@ discard block |
||
778 | 778 | $query = $wpdb->prepare( |
779 | 779 | 'SELECT ' |
780 | 780 | . 'COUNT( DISTINCT checkins.REG_ID ) ' |
781 | - . 'FROM ' . EEM_Checkin::instance()->table() . ' AS checkins INNER JOIN' |
|
781 | + . 'FROM '.EEM_Checkin::instance()->table().' AS checkins INNER JOIN' |
|
782 | 782 | . '( SELECT ' |
783 | 783 | . 'max( CHK_timestamp ) AS latest_checkin, ' |
784 | 784 | . 'REG_ID AS REG_ID ' |
785 | - . 'FROM ' . EEM_Checkin::instance()->table() . ' AS c ' |
|
786 | - . 'INNER JOIN ' . EEM_Datetime::instance()->table() . ' AS d ' |
|
785 | + . 'FROM '.EEM_Checkin::instance()->table().' AS c ' |
|
786 | + . 'INNER JOIN '.EEM_Datetime::instance()->table().' AS d ' |
|
787 | 787 | . 'ON c.DTT_ID=d.DTT_ID ' |
788 | 788 | . 'WHERE d.EVT_ID=%d ' |
789 | 789 | . 'GROUP BY REG_ID' |
@@ -813,7 +813,7 @@ discard block |
||
813 | 813 | { |
814 | 814 | // first do a native wp_query to get the latest REG_ID's matching these attendees. |
815 | 815 | global $wpdb; |
816 | - $registration_table = $wpdb->prefix . 'esp_registration'; |
|
816 | + $registration_table = $wpdb->prefix.'esp_registration'; |
|
817 | 817 | $attendee_table = $wpdb->posts; |
818 | 818 | $attendee_ids = is_array($attendee_ids) |
819 | 819 | ? array_map('absint', $attendee_ids) |
@@ -835,7 +835,7 @@ discard block |
||
835 | 835 | ) AS registrations |
836 | 836 | GROUP BY registrations.attendee_ids |
837 | 837 | "; |
838 | - $registration_ids = $wpdb->get_results($registration_id_query, ARRAY_A); |
|
838 | + $registration_ids = $wpdb->get_results($registration_id_query, ARRAY_A); |
|
839 | 839 | if (empty($registration_ids)) { |
840 | 840 | return []; |
841 | 841 | } |
@@ -867,7 +867,7 @@ discard block |
||
867 | 867 | public function event_reg_count_for_statuses(int $EVT_ID, $statuses = []): int |
868 | 868 | { |
869 | 869 | $EVT_ID = absint($EVT_ID); |
870 | - if (! $EVT_ID) { |
|
870 | + if ( ! $EVT_ID) { |
|
871 | 871 | throw new InvalidArgumentException( |
872 | 872 | esc_html__('An invalid Event ID was supplied.', 'event_espresso') |
873 | 873 | ); |
@@ -877,7 +877,7 @@ discard block |
||
877 | 877 | |
878 | 878 | $valid_reg_statuses = EEM_Registration::reg_statuses(); |
879 | 879 | foreach ($statuses as $status) { |
880 | - if (! in_array($status, $valid_reg_statuses, true)) { |
|
880 | + if ( ! in_array($status, $valid_reg_statuses, true)) { |
|
881 | 881 | throw new InvalidStatusException($status, esc_html__('Registration', 'event_espresso')); |
882 | 882 | } |
883 | 883 | } |