@@ -12,1270 +12,1270 @@ |
||
12 | 12 | class Pricing_Admin_Page extends EE_Admin_Page |
13 | 13 | { |
14 | 14 | |
15 | - protected function _init_page_props() |
|
16 | - { |
|
17 | - $this->page_slug = PRICING_PG_SLUG; |
|
18 | - $this->page_label = PRICING_LABEL; |
|
19 | - $this->_admin_base_url = PRICING_ADMIN_URL; |
|
20 | - $this->_admin_base_path = PRICING_ADMIN; |
|
21 | - } |
|
22 | - |
|
23 | - |
|
24 | - protected function _ajax_hooks() |
|
25 | - { |
|
26 | - add_action('wp_ajax_espresso_update_prices_order', [$this, 'update_price_order']); |
|
27 | - } |
|
28 | - |
|
29 | - |
|
30 | - protected function _define_page_props() |
|
31 | - { |
|
32 | - $this->_admin_page_title = PRICING_LABEL; |
|
33 | - $this->_labels = [ |
|
34 | - 'buttons' => [ |
|
35 | - 'add' => esc_html__('Add New Default Price', 'event_espresso'), |
|
36 | - 'edit' => esc_html__('Edit Default Price', 'event_espresso'), |
|
37 | - 'delete' => esc_html__('Delete Default Price', 'event_espresso'), |
|
38 | - 'add_type' => esc_html__('Add New Default Price Type', 'event_espresso'), |
|
39 | - 'edit_type' => esc_html__('Edit Price Type', 'event_espresso'), |
|
40 | - 'delete_type' => esc_html__('Delete Price Type', 'event_espresso'), |
|
41 | - ], |
|
42 | - ]; |
|
43 | - } |
|
44 | - |
|
45 | - |
|
46 | - /** |
|
47 | - * an array for storing request actions and their corresponding methods |
|
48 | - * |
|
49 | - * @return void |
|
50 | - */ |
|
51 | - protected function _set_page_routes() |
|
52 | - { |
|
53 | - $PRC_ID = $this->request->getRequestParam('PRC_ID', 0, DataType::INTEGER); |
|
54 | - $PRT_ID = $this->request->getRequestParam('PRT_ID', 0, DataType::INTEGER); |
|
55 | - $this->_page_routes = [ |
|
56 | - 'default' => [ |
|
57 | - 'func' => '_price_overview_list_table', |
|
58 | - 'capability' => 'ee_read_default_prices', |
|
59 | - ], |
|
60 | - 'add_new_price' => [ |
|
61 | - 'func' => '_edit_price_details', |
|
62 | - 'args' => ['new_price' => true], |
|
63 | - 'capability' => 'ee_edit_default_prices', |
|
64 | - ], |
|
65 | - 'edit_price' => [ |
|
66 | - 'func' => '_edit_price_details', |
|
67 | - 'args' => ['new_price' => false], |
|
68 | - 'capability' => 'ee_edit_default_price', |
|
69 | - 'obj_id' => $PRC_ID, |
|
70 | - ], |
|
71 | - 'insert_price' => [ |
|
72 | - 'func' => '_insert_or_update_price', |
|
73 | - 'args' => ['new_price' => true], |
|
74 | - 'noheader' => true, |
|
75 | - 'capability' => 'ee_edit_default_prices', |
|
76 | - ], |
|
77 | - 'update_price' => [ |
|
78 | - 'func' => '_insert_or_update_price', |
|
79 | - 'args' => ['new_price' => false], |
|
80 | - 'noheader' => true, |
|
81 | - 'capability' => 'ee_edit_default_price', |
|
82 | - 'obj_id' => $PRC_ID, |
|
83 | - ], |
|
84 | - 'trash_price' => [ |
|
85 | - 'func' => '_trash_or_restore_price', |
|
86 | - 'args' => ['trash' => true], |
|
87 | - 'noheader' => true, |
|
88 | - 'capability' => 'ee_delete_default_price', |
|
89 | - 'obj_id' => $PRC_ID, |
|
90 | - ], |
|
91 | - 'restore_price' => [ |
|
92 | - 'func' => '_trash_or_restore_price', |
|
93 | - 'args' => ['trash' => false], |
|
94 | - 'noheader' => true, |
|
95 | - 'capability' => 'ee_delete_default_price', |
|
96 | - 'obj_id' => $PRC_ID, |
|
97 | - ], |
|
98 | - 'delete_price' => [ |
|
99 | - 'func' => '_delete_price', |
|
100 | - 'noheader' => true, |
|
101 | - 'capability' => 'ee_delete_default_price', |
|
102 | - 'obj_id' => $PRC_ID, |
|
103 | - ], |
|
104 | - 'espresso_update_price_order' => [ |
|
105 | - 'func' => 'update_price_order', |
|
106 | - 'noheader' => true, |
|
107 | - 'capability' => 'ee_edit_default_prices', |
|
108 | - ], |
|
109 | - // price types |
|
110 | - 'price_types' => [ |
|
111 | - 'func' => '_price_types_overview_list_table', |
|
112 | - 'capability' => 'ee_read_default_price_types', |
|
113 | - ], |
|
114 | - 'add_new_price_type' => [ |
|
115 | - 'func' => '_edit_price_type_details', |
|
116 | - 'capability' => 'ee_edit_default_price_types', |
|
117 | - ], |
|
118 | - 'edit_price_type' => [ |
|
119 | - 'func' => '_edit_price_type_details', |
|
120 | - 'capability' => 'ee_edit_default_price_type', |
|
121 | - 'obj_id' => $PRT_ID, |
|
122 | - ], |
|
123 | - 'insert_price_type' => [ |
|
124 | - 'func' => '_insert_or_update_price_type', |
|
125 | - 'args' => ['new_price_type' => true], |
|
126 | - 'noheader' => true, |
|
127 | - 'capability' => 'ee_edit_default_price_types', |
|
128 | - ], |
|
129 | - 'update_price_type' => [ |
|
130 | - 'func' => '_insert_or_update_price_type', |
|
131 | - 'args' => ['new_price_type' => false], |
|
132 | - 'noheader' => true, |
|
133 | - 'capability' => 'ee_edit_default_price_type', |
|
134 | - 'obj_id' => $PRT_ID, |
|
135 | - ], |
|
136 | - 'trash_price_type' => [ |
|
137 | - 'func' => '_trash_or_restore_price_type', |
|
138 | - 'args' => ['trash' => true], |
|
139 | - 'noheader' => true, |
|
140 | - 'capability' => 'ee_delete_default_price_type', |
|
141 | - 'obj_id' => $PRT_ID, |
|
142 | - ], |
|
143 | - 'restore_price_type' => [ |
|
144 | - 'func' => '_trash_or_restore_price_type', |
|
145 | - 'args' => ['trash' => false], |
|
146 | - 'noheader' => true, |
|
147 | - 'capability' => 'ee_delete_default_price_type', |
|
148 | - 'obj_id' => $PRT_ID, |
|
149 | - ], |
|
150 | - 'delete_price_type' => [ |
|
151 | - 'func' => '_delete_price_type', |
|
152 | - 'noheader' => true, |
|
153 | - 'capability' => 'ee_delete_default_price_type', |
|
154 | - 'obj_id' => $PRT_ID, |
|
155 | - ], |
|
156 | - 'tax_settings' => [ |
|
157 | - 'func' => '_tax_settings', |
|
158 | - 'capability' => 'manage_options', |
|
159 | - ], |
|
160 | - 'update_tax_settings' => [ |
|
161 | - 'func' => '_update_tax_settings', |
|
162 | - 'capability' => 'manage_options', |
|
163 | - 'noheader' => true, |
|
164 | - ], |
|
165 | - ]; |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - protected function _set_page_config() |
|
170 | - { |
|
171 | - $PRC_ID = $this->request->getRequestParam('id', 0, DataType::INTEGER); |
|
172 | - $this->_page_config = [ |
|
173 | - 'default' => [ |
|
174 | - 'nav' => [ |
|
175 | - 'label' => esc_html__('Default Pricing', 'event_espresso'), |
|
176 | - 'order' => 10, |
|
177 | - ], |
|
178 | - 'list_table' => 'Prices_List_Table', |
|
179 | - 'metaboxes' => $this->_default_espresso_metaboxes, |
|
180 | - 'help_tabs' => [ |
|
181 | - 'pricing_default_pricing_help_tab' => [ |
|
182 | - 'title' => esc_html__('Default Pricing', 'event_espresso'), |
|
183 | - 'filename' => 'pricing_default_pricing', |
|
184 | - ], |
|
185 | - 'pricing_default_pricing_table_column_headings_help_tab' => [ |
|
186 | - 'title' => esc_html__('Default Pricing Table Column Headings', 'event_espresso'), |
|
187 | - 'filename' => 'pricing_default_pricing_table_column_headings', |
|
188 | - ], |
|
189 | - 'pricing_default_pricing_views_bulk_actions_search_help_tab' => [ |
|
190 | - 'title' => esc_html__('Default Pricing Views & Bulk Actions & Search', 'event_espresso'), |
|
191 | - 'filename' => 'pricing_default_pricing_views_bulk_actions_search', |
|
192 | - ], |
|
193 | - ], |
|
194 | - 'require_nonce' => false, |
|
195 | - ], |
|
196 | - 'add_new_price' => [ |
|
197 | - 'nav' => [ |
|
198 | - 'label' => esc_html__('Add New Default Price', 'event_espresso'), |
|
199 | - 'order' => 20, |
|
200 | - 'persistent' => false, |
|
201 | - ], |
|
202 | - 'help_tabs' => [ |
|
203 | - 'add_new_default_price_help_tab' => [ |
|
204 | - 'title' => esc_html__('Add New Default Price', 'event_espresso'), |
|
205 | - 'filename' => 'pricing_add_new_default_price', |
|
206 | - ], |
|
207 | - ], |
|
208 | - 'metaboxes' => array_merge( |
|
209 | - ['_publish_post_box'], |
|
210 | - $this->_default_espresso_metaboxes |
|
211 | - ), |
|
212 | - 'require_nonce' => false, |
|
213 | - ], |
|
214 | - 'edit_price' => [ |
|
215 | - 'nav' => [ |
|
216 | - 'label' => esc_html__('Edit Default Price', 'event_espresso'), |
|
217 | - 'order' => 20, |
|
218 | - 'url' => $PRC_ID |
|
219 | - ? add_query_arg(['id' => $PRC_ID], $this->_current_page_view_url) |
|
220 | - : $this->_admin_base_url, |
|
221 | - 'persistent' => false, |
|
222 | - ], |
|
223 | - 'metaboxes' => array_merge( |
|
224 | - ['_publish_post_box'], |
|
225 | - $this->_default_espresso_metaboxes |
|
226 | - ), |
|
227 | - 'help_tabs' => [ |
|
228 | - 'edit_default_price_help_tab' => [ |
|
229 | - 'title' => esc_html__('Edit Default Price', 'event_espresso'), |
|
230 | - 'filename' => 'pricing_edit_default_price', |
|
231 | - ], |
|
232 | - ], |
|
233 | - 'require_nonce' => false, |
|
234 | - ], |
|
235 | - 'price_types' => [ |
|
236 | - 'nav' => [ |
|
237 | - 'label' => esc_html__('Price Types', 'event_espresso'), |
|
238 | - 'order' => 30, |
|
239 | - ], |
|
240 | - 'list_table' => 'Price_Types_List_Table', |
|
241 | - 'help_tabs' => [ |
|
242 | - 'pricing_price_types_help_tab' => [ |
|
243 | - 'title' => esc_html__('Price Types', 'event_espresso'), |
|
244 | - 'filename' => 'pricing_price_types', |
|
245 | - ], |
|
246 | - 'pricing_price_types_table_column_headings_help_tab' => [ |
|
247 | - 'title' => esc_html__('Price Types Table Column Headings', 'event_espresso'), |
|
248 | - 'filename' => 'pricing_price_types_table_column_headings', |
|
249 | - ], |
|
250 | - 'pricing_price_types_views_bulk_actions_search_help_tab' => [ |
|
251 | - 'title' => esc_html__('Price Types Views & Bulk Actions & Search', 'event_espresso'), |
|
252 | - 'filename' => 'pricing_price_types_views_bulk_actions_search', |
|
253 | - ], |
|
254 | - ], |
|
255 | - 'metaboxes' => $this->_default_espresso_metaboxes, |
|
256 | - 'require_nonce' => false, |
|
257 | - ], |
|
258 | - 'add_new_price_type' => [ |
|
259 | - 'nav' => [ |
|
260 | - 'label' => esc_html__('Add New Price Type', 'event_espresso'), |
|
261 | - 'order' => 40, |
|
262 | - 'persistent' => false, |
|
263 | - ], |
|
264 | - 'help_tabs' => [ |
|
265 | - 'add_new_price_type_help_tab' => [ |
|
266 | - 'title' => esc_html__('Add New Price Type', 'event_espresso'), |
|
267 | - 'filename' => 'pricing_add_new_price_type', |
|
268 | - ], |
|
269 | - ], |
|
270 | - 'metaboxes' => array_merge( |
|
271 | - ['_publish_post_box'], |
|
272 | - $this->_default_espresso_metaboxes |
|
273 | - ), |
|
274 | - 'require_nonce' => false, |
|
275 | - ], |
|
276 | - 'edit_price_type' => [ |
|
277 | - 'nav' => [ |
|
278 | - 'label' => esc_html__('Edit Price Type', 'event_espresso'), |
|
279 | - 'order' => 40, |
|
280 | - 'persistent' => false, |
|
281 | - ], |
|
282 | - 'help_tabs' => [ |
|
283 | - 'edit_price_type_help_tab' => [ |
|
284 | - 'title' => esc_html__('Edit Price Type', 'event_espresso'), |
|
285 | - 'filename' => 'pricing_edit_price_type', |
|
286 | - ], |
|
287 | - ], |
|
288 | - 'metaboxes' => array_merge( |
|
289 | - ['_publish_post_box'], |
|
290 | - $this->_default_espresso_metaboxes |
|
291 | - ), |
|
292 | - 'require_nonce' => false, |
|
293 | - ], |
|
294 | - 'tax_settings' => [ |
|
295 | - 'nav' => [ |
|
296 | - 'label' => esc_html__('Tax Settings', 'event_espresso'), |
|
297 | - 'order' => 40, |
|
298 | - ], |
|
299 | - 'labels' => [ |
|
300 | - 'publishbox' => esc_html__('Update Tax Settings', 'event_espresso'), |
|
301 | - ], |
|
302 | - 'metaboxes' => array_merge( |
|
303 | - ['_publish_post_box'], |
|
304 | - $this->_default_espresso_metaboxes |
|
305 | - ), |
|
306 | - 'require_nonce' => true, |
|
307 | - ], |
|
308 | - ]; |
|
309 | - } |
|
310 | - |
|
311 | - |
|
312 | - protected function _add_screen_options() |
|
313 | - { |
|
314 | - // todo |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - protected function _add_screen_options_default() |
|
319 | - { |
|
320 | - $this->_per_page_screen_option(); |
|
321 | - } |
|
322 | - |
|
323 | - |
|
324 | - protected function _add_screen_options_price_types() |
|
325 | - { |
|
326 | - $page_title = $this->_admin_page_title; |
|
327 | - $this->_admin_page_title = esc_html__('Price Types', 'event_espresso'); |
|
328 | - $this->_per_page_screen_option(); |
|
329 | - $this->_admin_page_title = $page_title; |
|
330 | - } |
|
331 | - |
|
332 | - |
|
333 | - protected function _add_feature_pointers() |
|
334 | - { |
|
335 | - } |
|
336 | - |
|
337 | - |
|
338 | - public function load_scripts_styles() |
|
339 | - { |
|
340 | - // styles |
|
341 | - wp_enqueue_style('espresso-ui-theme'); |
|
342 | - wp_register_style( |
|
343 | - 'espresso_PRICING', |
|
344 | - PRICING_ASSETS_URL . 'espresso_pricing_admin.css', |
|
345 | - [], |
|
346 | - EVENT_ESPRESSO_VERSION |
|
347 | - ); |
|
348 | - wp_enqueue_style('espresso_PRICING'); |
|
349 | - |
|
350 | - // scripts |
|
351 | - wp_enqueue_script('ee_admin_js'); |
|
352 | - wp_enqueue_script('jquery-ui-position'); |
|
353 | - wp_enqueue_script('jquery-ui-widget'); |
|
354 | - wp_register_script( |
|
355 | - 'espresso_PRICING', |
|
356 | - PRICING_ASSETS_URL . 'espresso_pricing_admin.js', |
|
357 | - ['jquery'], |
|
358 | - EVENT_ESPRESSO_VERSION, |
|
359 | - true |
|
360 | - ); |
|
361 | - wp_enqueue_script('espresso_PRICING'); |
|
362 | - } |
|
363 | - |
|
364 | - |
|
365 | - public function load_scripts_styles_default() |
|
366 | - { |
|
367 | - wp_enqueue_script('espresso_ajax_table_sorting'); |
|
368 | - } |
|
369 | - |
|
370 | - |
|
371 | - public function admin_footer_scripts() |
|
372 | - { |
|
373 | - } |
|
374 | - |
|
375 | - |
|
376 | - public function admin_init() |
|
377 | - { |
|
378 | - } |
|
379 | - |
|
380 | - |
|
381 | - public function admin_notices() |
|
382 | - { |
|
383 | - } |
|
384 | - |
|
385 | - |
|
386 | - protected function _set_list_table_views_default() |
|
387 | - { |
|
388 | - $this->_views = [ |
|
389 | - 'all' => [ |
|
390 | - 'slug' => 'all', |
|
391 | - 'label' => esc_html__('View All Default Pricing', 'event_espresso'), |
|
392 | - 'count' => 0, |
|
393 | - 'bulk_action' => [ |
|
394 | - 'trash_price' => esc_html__('Move to Trash', 'event_espresso'), |
|
395 | - ], |
|
396 | - ], |
|
397 | - ]; |
|
398 | - |
|
399 | - if (EE_Registry::instance()->CAP->current_user_can('ee_delete_default_prices', 'pricing_trash_price')) { |
|
400 | - $this->_views['trashed'] = [ |
|
401 | - 'slug' => 'trashed', |
|
402 | - 'label' => esc_html__('Trash', 'event_espresso'), |
|
403 | - 'count' => 0, |
|
404 | - 'bulk_action' => [ |
|
405 | - 'restore_price' => esc_html__('Restore from Trash', 'event_espresso'), |
|
406 | - 'delete_price' => esc_html__('Delete Permanently', 'event_espresso'), |
|
407 | - ], |
|
408 | - ]; |
|
409 | - } |
|
410 | - } |
|
411 | - |
|
412 | - |
|
413 | - protected function _set_list_table_views_price_types() |
|
414 | - { |
|
415 | - $this->_views = [ |
|
416 | - 'all' => [ |
|
417 | - 'slug' => 'all', |
|
418 | - 'label' => esc_html__('All', 'event_espresso'), |
|
419 | - 'count' => 0, |
|
420 | - 'bulk_action' => [ |
|
421 | - 'trash_price_type' => esc_html__('Move to Trash', 'event_espresso'), |
|
422 | - ], |
|
423 | - ], |
|
424 | - ]; |
|
425 | - |
|
426 | - if ( |
|
427 | - EE_Registry::instance()->CAP->current_user_can( |
|
428 | - 'ee_delete_default_price_types', |
|
429 | - 'pricing_trash_price_type' |
|
430 | - ) |
|
431 | - ) { |
|
432 | - $this->_views['trashed'] = [ |
|
433 | - 'slug' => 'trashed', |
|
434 | - 'label' => esc_html__('Trash', 'event_espresso'), |
|
435 | - 'count' => 0, |
|
436 | - 'bulk_action' => [ |
|
437 | - 'restore_price_type' => esc_html__('Restore from Trash', 'event_espresso'), |
|
438 | - 'delete_price_type' => esc_html__('Delete Permanently', 'event_espresso'), |
|
439 | - ], |
|
440 | - ]; |
|
441 | - } |
|
442 | - } |
|
443 | - |
|
444 | - |
|
445 | - /** |
|
446 | - * generates HTML for main Prices Admin page |
|
447 | - * |
|
448 | - * @return void |
|
449 | - * @throws EE_Error |
|
450 | - */ |
|
451 | - protected function _price_overview_list_table() |
|
452 | - { |
|
453 | - $this->_admin_page_title .= ' ' . $this->get_action_link_or_button( |
|
454 | - 'add_new_price', |
|
455 | - 'add', |
|
456 | - [], |
|
457 | - 'add-new-h2' |
|
458 | - ); |
|
459 | - $this->_admin_page_title .= $this->_learn_more_about_pricing_link(); |
|
460 | - $this->_search_btn_label = esc_html__('Default Prices', 'event_espresso'); |
|
461 | - $this->display_admin_list_table_page_with_sidebar(); |
|
462 | - } |
|
463 | - |
|
464 | - |
|
465 | - /** |
|
466 | - * retrieve data for Prices List table |
|
467 | - * |
|
468 | - * @param int $per_page how many prices displayed per page |
|
469 | - * @param bool $count return the count or objects |
|
470 | - * @param bool $trashed whether the current view is of the trash can - eww yuck! |
|
471 | - * @return EE_Soft_Delete_Base_Class[]|int int = count || array of price objects |
|
472 | - * @throws EE_Error |
|
473 | - * @throws ReflectionException |
|
474 | - */ |
|
475 | - public function get_prices_overview_data(int $per_page = 10, bool $count = false, bool $trashed = false) |
|
476 | - { |
|
477 | - // start with an empty array |
|
478 | - $event_pricing = []; |
|
479 | - |
|
480 | - require_once(PRICING_ADMIN . 'Prices_List_Table.class.php'); |
|
481 | - |
|
482 | - $orderby = $this->request->getRequestParam('orderby', ''); |
|
483 | - $order = $this->request->getRequestParam('order', 'ASC'); |
|
484 | - |
|
485 | - switch ($orderby) { |
|
486 | - case 'name': |
|
487 | - $orderby = ['PRC_name' => $order]; |
|
488 | - break; |
|
489 | - case 'type': |
|
490 | - $orderby = ['Price_Type.PRT_name' => $order]; |
|
491 | - break; |
|
492 | - case 'amount': |
|
493 | - $orderby = ['PRC_amount' => $order]; |
|
494 | - break; |
|
495 | - default: |
|
496 | - $orderby = ['PRC_order' => $order, 'Price_Type.PRT_order' => $order, 'PRC_ID' => $order]; |
|
497 | - } |
|
498 | - |
|
499 | - $current_page = $this->request->getRequestParam('paged', 1, DataType::INTEGER); |
|
500 | - $per_page = $this->request->getRequestParam('perpage', $per_page, DataType::INTEGER); |
|
501 | - |
|
502 | - $where = [ |
|
503 | - 'PRC_is_default' => 1, |
|
504 | - 'PRC_deleted' => $trashed, |
|
505 | - ]; |
|
506 | - |
|
507 | - $offset = ($current_page - 1) * $per_page; |
|
508 | - $limit = [$offset, $per_page]; |
|
509 | - |
|
510 | - $search_term = $this->request->getRequestParam('s'); |
|
511 | - if ($search_term) { |
|
512 | - $search_term = "%{$search_term}%"; |
|
513 | - $where['OR'] = [ |
|
514 | - 'PRC_name' => ['LIKE', $search_term], |
|
515 | - 'PRC_desc' => ['LIKE', $search_term], |
|
516 | - 'PRC_amount' => ['LIKE', $search_term], |
|
517 | - 'Price_Type.PRT_name' => ['LIKE', $search_term], |
|
518 | - ]; |
|
519 | - } |
|
520 | - |
|
521 | - $query_params = [ |
|
522 | - $where, |
|
523 | - 'order_by' => $orderby, |
|
524 | - 'limit' => $limit, |
|
525 | - 'group_by' => 'PRC_ID', |
|
526 | - ]; |
|
527 | - |
|
528 | - if ($count) { |
|
529 | - return $trashed |
|
530 | - ? EEM_Price::instance()->count([$where]) |
|
531 | - : EEM_Price::instance()->count_deleted_and_undeleted([$where]); |
|
532 | - } |
|
533 | - return EEM_Price::instance()->get_all_deleted_and_undeleted($query_params); |
|
534 | - } |
|
535 | - |
|
536 | - |
|
537 | - /** |
|
538 | - * @return void |
|
539 | - * @throws EE_Error |
|
540 | - * @throws ReflectionException |
|
541 | - */ |
|
542 | - protected function _edit_price_details() |
|
543 | - { |
|
544 | - // grab price ID |
|
545 | - $PRC_ID = $this->request->getRequestParam('id', 0, DataType::INTEGER); |
|
546 | - // change page title based on request action |
|
547 | - switch ($this->_req_action) { |
|
548 | - case 'add_new_price': |
|
549 | - $this->_admin_page_title = esc_html__('Add New Price', 'event_espresso'); |
|
550 | - break; |
|
551 | - case 'edit_price': |
|
552 | - $this->_admin_page_title = esc_html__('Edit Price', 'event_espresso'); |
|
553 | - break; |
|
554 | - default: |
|
555 | - $this->_admin_page_title = ucwords(str_replace('_', ' ', $this->_req_action)); |
|
556 | - } |
|
557 | - // add PRC_ID to title if editing |
|
558 | - $this->_admin_page_title = $PRC_ID ? $this->_admin_page_title . ' # ' . $PRC_ID : $this->_admin_page_title; |
|
559 | - |
|
560 | - if ($PRC_ID) { |
|
561 | - $price = EEM_Price::instance()->get_one_by_ID($PRC_ID); |
|
562 | - $additional_hidden_fields = [ |
|
563 | - 'PRC_ID' => ['type' => 'hidden', 'value' => $PRC_ID], |
|
564 | - ]; |
|
565 | - $this->_set_add_edit_form_tags('update_price', $additional_hidden_fields); |
|
566 | - } else { |
|
567 | - $price = EEM_Price::instance()->get_new_price(); |
|
568 | - $this->_set_add_edit_form_tags('insert_price'); |
|
569 | - } |
|
570 | - |
|
571 | - if (! $price instanceof EE_Price) { |
|
572 | - throw new RuntimeException( |
|
573 | - sprintf( |
|
574 | - esc_html__( |
|
575 | - 'A valid Price could not be retrieved from the database with ID: %1$s', |
|
576 | - 'event_espresso' |
|
577 | - ), |
|
578 | - $PRC_ID |
|
579 | - ) |
|
580 | - ); |
|
581 | - } |
|
582 | - |
|
583 | - $this->_template_args['PRC_ID'] = $PRC_ID; |
|
584 | - $this->_template_args['price'] = $price; |
|
585 | - |
|
586 | - $default_base_price = $price->type_obj() && $price->type_obj()->base_type() === 1; |
|
587 | - |
|
588 | - $this->_template_args['default_base_price'] = $default_base_price; |
|
589 | - |
|
590 | - // get price types |
|
591 | - $price_types = EEM_Price_Type::instance()->get_all([['PBT_ID' => ['!=', 1]]]); |
|
592 | - if (empty($price_types)) { |
|
593 | - $msg = esc_html__( |
|
594 | - 'You have no price types defined. Please add a price type before adding a price.', |
|
595 | - 'event_espresso' |
|
596 | - ); |
|
597 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
598 | - $this->display_admin_page_with_sidebar(); |
|
599 | - } |
|
600 | - $attributes = []; |
|
601 | - $price_type_names = []; |
|
602 | - $attributes[] = 'id="PRT_ID"'; |
|
603 | - if ($default_base_price) { |
|
604 | - $attributes[] = 'disabled="disabled"'; |
|
605 | - $price_type_names[] = ['id' => 1, 'text' => esc_html__('Base Price', 'event_espresso')]; |
|
606 | - } |
|
607 | - foreach ($price_types as $type) { |
|
608 | - $price_type_names[] = ['id' => $type->ID(), 'text' => $type->name()]; |
|
609 | - } |
|
610 | - $this->_template_args['attributes'] = implode(' ', $attributes); |
|
611 | - $this->_template_args['price_types'] = $price_type_names; |
|
612 | - |
|
613 | - $this->_template_args['learn_more_about_pricing_link'] = $this->_learn_more_about_pricing_link(); |
|
614 | - $this->_template_args['admin_page_content'] = $this->_edit_price_details_meta_box(); |
|
615 | - |
|
616 | - $this->_set_publish_post_box_vars('id', $PRC_ID); |
|
617 | - // the details template wrapper |
|
618 | - $this->display_admin_page_with_sidebar(); |
|
619 | - } |
|
620 | - |
|
621 | - |
|
622 | - /** |
|
623 | - * |
|
624 | - * @return string |
|
625 | - */ |
|
626 | - public function _edit_price_details_meta_box(): string |
|
627 | - { |
|
628 | - return EEH_Template::display_template( |
|
629 | - PRICING_TEMPLATE_PATH . 'pricing_details_main_meta_box.template.php', |
|
630 | - $this->_template_args, |
|
631 | - true |
|
632 | - ); |
|
633 | - } |
|
634 | - |
|
635 | - |
|
636 | - /** |
|
637 | - * @return array |
|
638 | - * @throws EE_Error |
|
639 | - * @throws ReflectionException |
|
640 | - */ |
|
641 | - protected function set_price_column_values(): array |
|
642 | - { |
|
643 | - $PRC_order = 0; |
|
644 | - $PRT_ID = $this->request->getRequestParam('PRT_ID', 0, DataType::INTEGER); |
|
645 | - if ($PRT_ID) { |
|
646 | - /** @var EE_Price_Type $price_type */ |
|
647 | - $price_type = EEM_Price_Type::instance()->get_one_by_ID($PRT_ID); |
|
648 | - if ($price_type instanceof EE_Price_Type) { |
|
649 | - $PRC_order = $price_type->order(); |
|
650 | - } |
|
651 | - } |
|
652 | - return [ |
|
653 | - 'PRT_ID' => $PRT_ID, |
|
654 | - 'PRC_amount' => $this->request->getRequestParam('PRC_amount', 0, DataType::FLOAT), |
|
655 | - 'PRC_name' => $this->request->getRequestParam('PRC_name'), |
|
656 | - 'PRC_desc' => $this->request->getRequestParam('PRC_desc'), |
|
657 | - 'PRC_is_default' => 1, |
|
658 | - 'PRC_overrides' => null, |
|
659 | - 'PRC_order' => $PRC_order, |
|
660 | - 'PRC_deleted' => 0, |
|
661 | - 'PRC_parent' => 0, |
|
662 | - ]; |
|
663 | - } |
|
664 | - |
|
665 | - |
|
666 | - /** |
|
667 | - * @param bool $insert - whether to insert or update |
|
668 | - * @return void |
|
669 | - * @throws EE_Error |
|
670 | - * @throws ReflectionException |
|
671 | - */ |
|
672 | - protected function _insert_or_update_price(bool $insert = false) |
|
673 | - { |
|
674 | - // why be so pessimistic ??? : ( |
|
675 | - $updated = 0; |
|
676 | - |
|
677 | - $set_column_values = $this->set_price_column_values(); |
|
678 | - // is this a new Price ? |
|
679 | - if ($insert) { |
|
680 | - // run the insert |
|
681 | - $PRC_ID = EEM_Price::instance()->insert($set_column_values); |
|
682 | - if ($PRC_ID) { |
|
683 | - // make sure this new price modifier is attached to the ticket but ONLY if it is not a tax type |
|
684 | - $price = EEM_price::instance()->get_one_by_ID($PRC_ID); |
|
685 | - if ( |
|
686 | - $price instanceof EE_Price |
|
687 | - && $price->type_obj() instanceof EE_Price_type |
|
688 | - && $price->type_obj()->base_type() !== EEM_Price_Type::base_type_tax |
|
689 | - ) { |
|
690 | - $ticket = EEM_Ticket::instance()->get_one_by_ID(1); |
|
691 | - $ticket->_add_relation_to($price, 'Price'); |
|
692 | - $ticket->save(); |
|
693 | - } |
|
694 | - $updated = 1; |
|
695 | - } |
|
696 | - $action_desc = 'created'; |
|
697 | - } else { |
|
698 | - $PRC_ID = $this->request->getRequestParam('PRC_ID', 0, DataType::INTEGER); |
|
699 | - // run the update |
|
700 | - $where_cols_n_values = ['PRC_ID' => $PRC_ID]; |
|
701 | - $updated = EEM_Price::instance()->update($set_column_values, [$where_cols_n_values]); |
|
702 | - |
|
703 | - $price = EEM_Price::instance()->get_one_by_ID($PRC_ID); |
|
704 | - if ($price instanceof EE_Price && $price->type_obj()->base_type() !== EEM_Price_Type::base_type_tax) { |
|
705 | - // if this is $PRC_ID == 1, |
|
706 | - // then we need to update the default ticket attached to this price so the TKT_price value is updated. |
|
707 | - if ($PRC_ID === 1) { |
|
708 | - $ticket = $price->get_first_related('Ticket'); |
|
709 | - if ($ticket) { |
|
710 | - $ticket->set('TKT_price', $price->get('PRC_amount')); |
|
711 | - $ticket->set('TKT_name', $price->get('PRC_name')); |
|
712 | - $ticket->set('TKT_description', $price->get('PRC_desc')); |
|
713 | - $ticket->save(); |
|
714 | - } |
|
715 | - } else { |
|
716 | - // we make sure this price is attached to base ticket. but ONLY if its not a tax ticket type. |
|
717 | - $ticket = EEM_Ticket::instance()->get_one_by_ID(1); |
|
718 | - $ticket->_add_relation_to($PRC_ID, 'Price'); |
|
719 | - $ticket->save(); |
|
720 | - } |
|
721 | - } |
|
722 | - |
|
723 | - $action_desc = 'updated'; |
|
724 | - } |
|
725 | - |
|
726 | - $query_args = ['action' => 'edit_price', 'id' => $PRC_ID]; |
|
727 | - |
|
728 | - $this->_redirect_after_action($updated, 'Prices', $action_desc, $query_args); |
|
729 | - } |
|
730 | - |
|
731 | - |
|
732 | - /** |
|
733 | - * @param bool $trash - whether to move item to trash (TRUE) or restore it (FALSE) |
|
734 | - * @return void |
|
735 | - * @throws EE_Error |
|
736 | - * @throws ReflectionException |
|
737 | - */ |
|
738 | - protected function _trash_or_restore_price(bool $trash = true) |
|
739 | - { |
|
740 | - $entity_model = EEM_Price::instance(); |
|
741 | - $action = $trash ? EE_Admin_List_Table::ACTION_TRASH : EE_Admin_List_Table::ACTION_RESTORE; |
|
742 | - $result = $this->trashRestoreDeleteEntities( |
|
743 | - $entity_model, |
|
744 | - 'id', |
|
745 | - $action, |
|
746 | - 'PRC_deleted', |
|
747 | - [$this, 'adjustTicketRelations'] |
|
748 | - ); |
|
749 | - |
|
750 | - if ($result) { |
|
751 | - $msg = $trash |
|
752 | - ? esc_html( |
|
753 | - _n( |
|
754 | - 'The Price has been trashed', |
|
755 | - 'The Prices have been trashed', |
|
756 | - $result, |
|
757 | - 'event_espresso' |
|
758 | - ) |
|
759 | - ) |
|
760 | - : esc_html( |
|
761 | - _n( |
|
762 | - 'The Price has been restored', |
|
763 | - 'The Prices have been restored', |
|
764 | - $result, |
|
765 | - 'event_espresso' |
|
766 | - ) |
|
767 | - ); |
|
768 | - EE_Error::add_success($msg); |
|
769 | - } |
|
770 | - |
|
771 | - $this->_redirect_after_action( |
|
772 | - $result, |
|
773 | - _n('Price', 'Prices', $result, 'event_espresso'), |
|
774 | - $trash ? 'trashed' : 'restored', |
|
775 | - ['action' => 'default'], |
|
776 | - true |
|
777 | - ); |
|
778 | - } |
|
779 | - |
|
780 | - |
|
781 | - /** |
|
782 | - * @param EEM_Base $entity_model |
|
783 | - * @param int|string $entity_ID |
|
784 | - * @param string $action |
|
785 | - * @param int $result |
|
786 | - * @throws EE_Error |
|
787 | - * @throws ReflectionException |
|
788 | - * @since $VID:$ |
|
789 | - */ |
|
790 | - public function adjustTicketRelations(EEM_Base $entity_model, $entity_ID, string $action, int $result) |
|
791 | - { |
|
792 | - if (! $entity_ID || (float) $result < 1) { |
|
793 | - return; |
|
794 | - } |
|
795 | - |
|
796 | - $entity = $entity_model->get_one_by_ID($entity_ID); |
|
797 | - if (! $entity instanceof EE_Price || $entity->type_obj()->base_type() === EEM_Price_Type::base_type_tax) { |
|
798 | - return; |
|
799 | - } |
|
800 | - |
|
801 | - // get default tickets for updating |
|
802 | - $default_tickets = EEM_Ticket::instance()->get_all_default_tickets(); |
|
803 | - foreach ($default_tickets as $default_ticket) { |
|
804 | - if (! $default_ticket instanceof EE_Ticket) { |
|
805 | - continue; |
|
806 | - } |
|
807 | - switch ($action) { |
|
808 | - case EE_Admin_List_Table::ACTION_DELETE: |
|
809 | - case EE_Admin_List_Table::ACTION_TRASH: |
|
810 | - // if trashing then remove relations to base default ticket. |
|
811 | - $default_ticket->_remove_relation_to($entity_ID, 'Price'); |
|
812 | - break; |
|
813 | - case EE_Admin_List_Table::ACTION_RESTORE: |
|
814 | - // if restoring then add back to base default ticket |
|
815 | - $default_ticket->_add_relation_to($entity_ID, 'Price'); |
|
816 | - break; |
|
817 | - } |
|
818 | - $default_ticket->save(); |
|
819 | - } |
|
820 | - } |
|
821 | - |
|
822 | - |
|
823 | - /** |
|
824 | - * @return void |
|
825 | - * @throws EE_Error |
|
826 | - * @throws ReflectionException |
|
827 | - */ |
|
828 | - protected function _delete_price() |
|
829 | - { |
|
830 | - $entity_model = EEM_Price::instance(); |
|
831 | - $deleted = $this->trashRestoreDeleteEntities($entity_model, 'id'); |
|
832 | - $entity = $entity_model->item_name($deleted); |
|
833 | - $this->_redirect_after_action($deleted, $entity, 'deleted', ['action' => 'default']); |
|
834 | - } |
|
835 | - |
|
836 | - |
|
837 | - /** |
|
838 | - * @throws EE_Error |
|
839 | - * @throws ReflectionException |
|
840 | - */ |
|
841 | - public function update_price_order() |
|
842 | - { |
|
843 | - // grab our row IDs |
|
844 | - $row_ids = $this->request->getRequestParam('row_ids', ''); |
|
845 | - $row_ids = explode(',', rtrim($row_ids, ',')); |
|
846 | - |
|
847 | - $all_updated = true; |
|
848 | - foreach ($row_ids as $i => $row_id) { |
|
849 | - // Update the prices when re-ordering |
|
850 | - $fields_n_values = ['PRC_order' => $i + 1]; |
|
851 | - $query_params = [['PRC_ID' => absint($row_id)]]; |
|
852 | - // any failure will toggle $all_updated to false |
|
853 | - $all_updated = $row_id && EEM_Price::instance()->update($fields_n_values, $query_params) !== false |
|
854 | - ? $all_updated |
|
855 | - : false; |
|
856 | - } |
|
857 | - $success = $all_updated ? esc_html__('Price order was updated successfully.', 'event_espresso') : false; |
|
858 | - $errors = ! $all_updated ? esc_html__('An error occurred. The price order was not updated.', 'event_espresso') |
|
859 | - : false; |
|
860 | - |
|
861 | - echo wp_json_encode(['return_data' => false, 'success' => $success, 'errors' => $errors]); |
|
862 | - die(); |
|
863 | - } |
|
864 | - |
|
865 | - |
|
866 | - /****************************************************************************************************************** |
|
15 | + protected function _init_page_props() |
|
16 | + { |
|
17 | + $this->page_slug = PRICING_PG_SLUG; |
|
18 | + $this->page_label = PRICING_LABEL; |
|
19 | + $this->_admin_base_url = PRICING_ADMIN_URL; |
|
20 | + $this->_admin_base_path = PRICING_ADMIN; |
|
21 | + } |
|
22 | + |
|
23 | + |
|
24 | + protected function _ajax_hooks() |
|
25 | + { |
|
26 | + add_action('wp_ajax_espresso_update_prices_order', [$this, 'update_price_order']); |
|
27 | + } |
|
28 | + |
|
29 | + |
|
30 | + protected function _define_page_props() |
|
31 | + { |
|
32 | + $this->_admin_page_title = PRICING_LABEL; |
|
33 | + $this->_labels = [ |
|
34 | + 'buttons' => [ |
|
35 | + 'add' => esc_html__('Add New Default Price', 'event_espresso'), |
|
36 | + 'edit' => esc_html__('Edit Default Price', 'event_espresso'), |
|
37 | + 'delete' => esc_html__('Delete Default Price', 'event_espresso'), |
|
38 | + 'add_type' => esc_html__('Add New Default Price Type', 'event_espresso'), |
|
39 | + 'edit_type' => esc_html__('Edit Price Type', 'event_espresso'), |
|
40 | + 'delete_type' => esc_html__('Delete Price Type', 'event_espresso'), |
|
41 | + ], |
|
42 | + ]; |
|
43 | + } |
|
44 | + |
|
45 | + |
|
46 | + /** |
|
47 | + * an array for storing request actions and their corresponding methods |
|
48 | + * |
|
49 | + * @return void |
|
50 | + */ |
|
51 | + protected function _set_page_routes() |
|
52 | + { |
|
53 | + $PRC_ID = $this->request->getRequestParam('PRC_ID', 0, DataType::INTEGER); |
|
54 | + $PRT_ID = $this->request->getRequestParam('PRT_ID', 0, DataType::INTEGER); |
|
55 | + $this->_page_routes = [ |
|
56 | + 'default' => [ |
|
57 | + 'func' => '_price_overview_list_table', |
|
58 | + 'capability' => 'ee_read_default_prices', |
|
59 | + ], |
|
60 | + 'add_new_price' => [ |
|
61 | + 'func' => '_edit_price_details', |
|
62 | + 'args' => ['new_price' => true], |
|
63 | + 'capability' => 'ee_edit_default_prices', |
|
64 | + ], |
|
65 | + 'edit_price' => [ |
|
66 | + 'func' => '_edit_price_details', |
|
67 | + 'args' => ['new_price' => false], |
|
68 | + 'capability' => 'ee_edit_default_price', |
|
69 | + 'obj_id' => $PRC_ID, |
|
70 | + ], |
|
71 | + 'insert_price' => [ |
|
72 | + 'func' => '_insert_or_update_price', |
|
73 | + 'args' => ['new_price' => true], |
|
74 | + 'noheader' => true, |
|
75 | + 'capability' => 'ee_edit_default_prices', |
|
76 | + ], |
|
77 | + 'update_price' => [ |
|
78 | + 'func' => '_insert_or_update_price', |
|
79 | + 'args' => ['new_price' => false], |
|
80 | + 'noheader' => true, |
|
81 | + 'capability' => 'ee_edit_default_price', |
|
82 | + 'obj_id' => $PRC_ID, |
|
83 | + ], |
|
84 | + 'trash_price' => [ |
|
85 | + 'func' => '_trash_or_restore_price', |
|
86 | + 'args' => ['trash' => true], |
|
87 | + 'noheader' => true, |
|
88 | + 'capability' => 'ee_delete_default_price', |
|
89 | + 'obj_id' => $PRC_ID, |
|
90 | + ], |
|
91 | + 'restore_price' => [ |
|
92 | + 'func' => '_trash_or_restore_price', |
|
93 | + 'args' => ['trash' => false], |
|
94 | + 'noheader' => true, |
|
95 | + 'capability' => 'ee_delete_default_price', |
|
96 | + 'obj_id' => $PRC_ID, |
|
97 | + ], |
|
98 | + 'delete_price' => [ |
|
99 | + 'func' => '_delete_price', |
|
100 | + 'noheader' => true, |
|
101 | + 'capability' => 'ee_delete_default_price', |
|
102 | + 'obj_id' => $PRC_ID, |
|
103 | + ], |
|
104 | + 'espresso_update_price_order' => [ |
|
105 | + 'func' => 'update_price_order', |
|
106 | + 'noheader' => true, |
|
107 | + 'capability' => 'ee_edit_default_prices', |
|
108 | + ], |
|
109 | + // price types |
|
110 | + 'price_types' => [ |
|
111 | + 'func' => '_price_types_overview_list_table', |
|
112 | + 'capability' => 'ee_read_default_price_types', |
|
113 | + ], |
|
114 | + 'add_new_price_type' => [ |
|
115 | + 'func' => '_edit_price_type_details', |
|
116 | + 'capability' => 'ee_edit_default_price_types', |
|
117 | + ], |
|
118 | + 'edit_price_type' => [ |
|
119 | + 'func' => '_edit_price_type_details', |
|
120 | + 'capability' => 'ee_edit_default_price_type', |
|
121 | + 'obj_id' => $PRT_ID, |
|
122 | + ], |
|
123 | + 'insert_price_type' => [ |
|
124 | + 'func' => '_insert_or_update_price_type', |
|
125 | + 'args' => ['new_price_type' => true], |
|
126 | + 'noheader' => true, |
|
127 | + 'capability' => 'ee_edit_default_price_types', |
|
128 | + ], |
|
129 | + 'update_price_type' => [ |
|
130 | + 'func' => '_insert_or_update_price_type', |
|
131 | + 'args' => ['new_price_type' => false], |
|
132 | + 'noheader' => true, |
|
133 | + 'capability' => 'ee_edit_default_price_type', |
|
134 | + 'obj_id' => $PRT_ID, |
|
135 | + ], |
|
136 | + 'trash_price_type' => [ |
|
137 | + 'func' => '_trash_or_restore_price_type', |
|
138 | + 'args' => ['trash' => true], |
|
139 | + 'noheader' => true, |
|
140 | + 'capability' => 'ee_delete_default_price_type', |
|
141 | + 'obj_id' => $PRT_ID, |
|
142 | + ], |
|
143 | + 'restore_price_type' => [ |
|
144 | + 'func' => '_trash_or_restore_price_type', |
|
145 | + 'args' => ['trash' => false], |
|
146 | + 'noheader' => true, |
|
147 | + 'capability' => 'ee_delete_default_price_type', |
|
148 | + 'obj_id' => $PRT_ID, |
|
149 | + ], |
|
150 | + 'delete_price_type' => [ |
|
151 | + 'func' => '_delete_price_type', |
|
152 | + 'noheader' => true, |
|
153 | + 'capability' => 'ee_delete_default_price_type', |
|
154 | + 'obj_id' => $PRT_ID, |
|
155 | + ], |
|
156 | + 'tax_settings' => [ |
|
157 | + 'func' => '_tax_settings', |
|
158 | + 'capability' => 'manage_options', |
|
159 | + ], |
|
160 | + 'update_tax_settings' => [ |
|
161 | + 'func' => '_update_tax_settings', |
|
162 | + 'capability' => 'manage_options', |
|
163 | + 'noheader' => true, |
|
164 | + ], |
|
165 | + ]; |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + protected function _set_page_config() |
|
170 | + { |
|
171 | + $PRC_ID = $this->request->getRequestParam('id', 0, DataType::INTEGER); |
|
172 | + $this->_page_config = [ |
|
173 | + 'default' => [ |
|
174 | + 'nav' => [ |
|
175 | + 'label' => esc_html__('Default Pricing', 'event_espresso'), |
|
176 | + 'order' => 10, |
|
177 | + ], |
|
178 | + 'list_table' => 'Prices_List_Table', |
|
179 | + 'metaboxes' => $this->_default_espresso_metaboxes, |
|
180 | + 'help_tabs' => [ |
|
181 | + 'pricing_default_pricing_help_tab' => [ |
|
182 | + 'title' => esc_html__('Default Pricing', 'event_espresso'), |
|
183 | + 'filename' => 'pricing_default_pricing', |
|
184 | + ], |
|
185 | + 'pricing_default_pricing_table_column_headings_help_tab' => [ |
|
186 | + 'title' => esc_html__('Default Pricing Table Column Headings', 'event_espresso'), |
|
187 | + 'filename' => 'pricing_default_pricing_table_column_headings', |
|
188 | + ], |
|
189 | + 'pricing_default_pricing_views_bulk_actions_search_help_tab' => [ |
|
190 | + 'title' => esc_html__('Default Pricing Views & Bulk Actions & Search', 'event_espresso'), |
|
191 | + 'filename' => 'pricing_default_pricing_views_bulk_actions_search', |
|
192 | + ], |
|
193 | + ], |
|
194 | + 'require_nonce' => false, |
|
195 | + ], |
|
196 | + 'add_new_price' => [ |
|
197 | + 'nav' => [ |
|
198 | + 'label' => esc_html__('Add New Default Price', 'event_espresso'), |
|
199 | + 'order' => 20, |
|
200 | + 'persistent' => false, |
|
201 | + ], |
|
202 | + 'help_tabs' => [ |
|
203 | + 'add_new_default_price_help_tab' => [ |
|
204 | + 'title' => esc_html__('Add New Default Price', 'event_espresso'), |
|
205 | + 'filename' => 'pricing_add_new_default_price', |
|
206 | + ], |
|
207 | + ], |
|
208 | + 'metaboxes' => array_merge( |
|
209 | + ['_publish_post_box'], |
|
210 | + $this->_default_espresso_metaboxes |
|
211 | + ), |
|
212 | + 'require_nonce' => false, |
|
213 | + ], |
|
214 | + 'edit_price' => [ |
|
215 | + 'nav' => [ |
|
216 | + 'label' => esc_html__('Edit Default Price', 'event_espresso'), |
|
217 | + 'order' => 20, |
|
218 | + 'url' => $PRC_ID |
|
219 | + ? add_query_arg(['id' => $PRC_ID], $this->_current_page_view_url) |
|
220 | + : $this->_admin_base_url, |
|
221 | + 'persistent' => false, |
|
222 | + ], |
|
223 | + 'metaboxes' => array_merge( |
|
224 | + ['_publish_post_box'], |
|
225 | + $this->_default_espresso_metaboxes |
|
226 | + ), |
|
227 | + 'help_tabs' => [ |
|
228 | + 'edit_default_price_help_tab' => [ |
|
229 | + 'title' => esc_html__('Edit Default Price', 'event_espresso'), |
|
230 | + 'filename' => 'pricing_edit_default_price', |
|
231 | + ], |
|
232 | + ], |
|
233 | + 'require_nonce' => false, |
|
234 | + ], |
|
235 | + 'price_types' => [ |
|
236 | + 'nav' => [ |
|
237 | + 'label' => esc_html__('Price Types', 'event_espresso'), |
|
238 | + 'order' => 30, |
|
239 | + ], |
|
240 | + 'list_table' => 'Price_Types_List_Table', |
|
241 | + 'help_tabs' => [ |
|
242 | + 'pricing_price_types_help_tab' => [ |
|
243 | + 'title' => esc_html__('Price Types', 'event_espresso'), |
|
244 | + 'filename' => 'pricing_price_types', |
|
245 | + ], |
|
246 | + 'pricing_price_types_table_column_headings_help_tab' => [ |
|
247 | + 'title' => esc_html__('Price Types Table Column Headings', 'event_espresso'), |
|
248 | + 'filename' => 'pricing_price_types_table_column_headings', |
|
249 | + ], |
|
250 | + 'pricing_price_types_views_bulk_actions_search_help_tab' => [ |
|
251 | + 'title' => esc_html__('Price Types Views & Bulk Actions & Search', 'event_espresso'), |
|
252 | + 'filename' => 'pricing_price_types_views_bulk_actions_search', |
|
253 | + ], |
|
254 | + ], |
|
255 | + 'metaboxes' => $this->_default_espresso_metaboxes, |
|
256 | + 'require_nonce' => false, |
|
257 | + ], |
|
258 | + 'add_new_price_type' => [ |
|
259 | + 'nav' => [ |
|
260 | + 'label' => esc_html__('Add New Price Type', 'event_espresso'), |
|
261 | + 'order' => 40, |
|
262 | + 'persistent' => false, |
|
263 | + ], |
|
264 | + 'help_tabs' => [ |
|
265 | + 'add_new_price_type_help_tab' => [ |
|
266 | + 'title' => esc_html__('Add New Price Type', 'event_espresso'), |
|
267 | + 'filename' => 'pricing_add_new_price_type', |
|
268 | + ], |
|
269 | + ], |
|
270 | + 'metaboxes' => array_merge( |
|
271 | + ['_publish_post_box'], |
|
272 | + $this->_default_espresso_metaboxes |
|
273 | + ), |
|
274 | + 'require_nonce' => false, |
|
275 | + ], |
|
276 | + 'edit_price_type' => [ |
|
277 | + 'nav' => [ |
|
278 | + 'label' => esc_html__('Edit Price Type', 'event_espresso'), |
|
279 | + 'order' => 40, |
|
280 | + 'persistent' => false, |
|
281 | + ], |
|
282 | + 'help_tabs' => [ |
|
283 | + 'edit_price_type_help_tab' => [ |
|
284 | + 'title' => esc_html__('Edit Price Type', 'event_espresso'), |
|
285 | + 'filename' => 'pricing_edit_price_type', |
|
286 | + ], |
|
287 | + ], |
|
288 | + 'metaboxes' => array_merge( |
|
289 | + ['_publish_post_box'], |
|
290 | + $this->_default_espresso_metaboxes |
|
291 | + ), |
|
292 | + 'require_nonce' => false, |
|
293 | + ], |
|
294 | + 'tax_settings' => [ |
|
295 | + 'nav' => [ |
|
296 | + 'label' => esc_html__('Tax Settings', 'event_espresso'), |
|
297 | + 'order' => 40, |
|
298 | + ], |
|
299 | + 'labels' => [ |
|
300 | + 'publishbox' => esc_html__('Update Tax Settings', 'event_espresso'), |
|
301 | + ], |
|
302 | + 'metaboxes' => array_merge( |
|
303 | + ['_publish_post_box'], |
|
304 | + $this->_default_espresso_metaboxes |
|
305 | + ), |
|
306 | + 'require_nonce' => true, |
|
307 | + ], |
|
308 | + ]; |
|
309 | + } |
|
310 | + |
|
311 | + |
|
312 | + protected function _add_screen_options() |
|
313 | + { |
|
314 | + // todo |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + protected function _add_screen_options_default() |
|
319 | + { |
|
320 | + $this->_per_page_screen_option(); |
|
321 | + } |
|
322 | + |
|
323 | + |
|
324 | + protected function _add_screen_options_price_types() |
|
325 | + { |
|
326 | + $page_title = $this->_admin_page_title; |
|
327 | + $this->_admin_page_title = esc_html__('Price Types', 'event_espresso'); |
|
328 | + $this->_per_page_screen_option(); |
|
329 | + $this->_admin_page_title = $page_title; |
|
330 | + } |
|
331 | + |
|
332 | + |
|
333 | + protected function _add_feature_pointers() |
|
334 | + { |
|
335 | + } |
|
336 | + |
|
337 | + |
|
338 | + public function load_scripts_styles() |
|
339 | + { |
|
340 | + // styles |
|
341 | + wp_enqueue_style('espresso-ui-theme'); |
|
342 | + wp_register_style( |
|
343 | + 'espresso_PRICING', |
|
344 | + PRICING_ASSETS_URL . 'espresso_pricing_admin.css', |
|
345 | + [], |
|
346 | + EVENT_ESPRESSO_VERSION |
|
347 | + ); |
|
348 | + wp_enqueue_style('espresso_PRICING'); |
|
349 | + |
|
350 | + // scripts |
|
351 | + wp_enqueue_script('ee_admin_js'); |
|
352 | + wp_enqueue_script('jquery-ui-position'); |
|
353 | + wp_enqueue_script('jquery-ui-widget'); |
|
354 | + wp_register_script( |
|
355 | + 'espresso_PRICING', |
|
356 | + PRICING_ASSETS_URL . 'espresso_pricing_admin.js', |
|
357 | + ['jquery'], |
|
358 | + EVENT_ESPRESSO_VERSION, |
|
359 | + true |
|
360 | + ); |
|
361 | + wp_enqueue_script('espresso_PRICING'); |
|
362 | + } |
|
363 | + |
|
364 | + |
|
365 | + public function load_scripts_styles_default() |
|
366 | + { |
|
367 | + wp_enqueue_script('espresso_ajax_table_sorting'); |
|
368 | + } |
|
369 | + |
|
370 | + |
|
371 | + public function admin_footer_scripts() |
|
372 | + { |
|
373 | + } |
|
374 | + |
|
375 | + |
|
376 | + public function admin_init() |
|
377 | + { |
|
378 | + } |
|
379 | + |
|
380 | + |
|
381 | + public function admin_notices() |
|
382 | + { |
|
383 | + } |
|
384 | + |
|
385 | + |
|
386 | + protected function _set_list_table_views_default() |
|
387 | + { |
|
388 | + $this->_views = [ |
|
389 | + 'all' => [ |
|
390 | + 'slug' => 'all', |
|
391 | + 'label' => esc_html__('View All Default Pricing', 'event_espresso'), |
|
392 | + 'count' => 0, |
|
393 | + 'bulk_action' => [ |
|
394 | + 'trash_price' => esc_html__('Move to Trash', 'event_espresso'), |
|
395 | + ], |
|
396 | + ], |
|
397 | + ]; |
|
398 | + |
|
399 | + if (EE_Registry::instance()->CAP->current_user_can('ee_delete_default_prices', 'pricing_trash_price')) { |
|
400 | + $this->_views['trashed'] = [ |
|
401 | + 'slug' => 'trashed', |
|
402 | + 'label' => esc_html__('Trash', 'event_espresso'), |
|
403 | + 'count' => 0, |
|
404 | + 'bulk_action' => [ |
|
405 | + 'restore_price' => esc_html__('Restore from Trash', 'event_espresso'), |
|
406 | + 'delete_price' => esc_html__('Delete Permanently', 'event_espresso'), |
|
407 | + ], |
|
408 | + ]; |
|
409 | + } |
|
410 | + } |
|
411 | + |
|
412 | + |
|
413 | + protected function _set_list_table_views_price_types() |
|
414 | + { |
|
415 | + $this->_views = [ |
|
416 | + 'all' => [ |
|
417 | + 'slug' => 'all', |
|
418 | + 'label' => esc_html__('All', 'event_espresso'), |
|
419 | + 'count' => 0, |
|
420 | + 'bulk_action' => [ |
|
421 | + 'trash_price_type' => esc_html__('Move to Trash', 'event_espresso'), |
|
422 | + ], |
|
423 | + ], |
|
424 | + ]; |
|
425 | + |
|
426 | + if ( |
|
427 | + EE_Registry::instance()->CAP->current_user_can( |
|
428 | + 'ee_delete_default_price_types', |
|
429 | + 'pricing_trash_price_type' |
|
430 | + ) |
|
431 | + ) { |
|
432 | + $this->_views['trashed'] = [ |
|
433 | + 'slug' => 'trashed', |
|
434 | + 'label' => esc_html__('Trash', 'event_espresso'), |
|
435 | + 'count' => 0, |
|
436 | + 'bulk_action' => [ |
|
437 | + 'restore_price_type' => esc_html__('Restore from Trash', 'event_espresso'), |
|
438 | + 'delete_price_type' => esc_html__('Delete Permanently', 'event_espresso'), |
|
439 | + ], |
|
440 | + ]; |
|
441 | + } |
|
442 | + } |
|
443 | + |
|
444 | + |
|
445 | + /** |
|
446 | + * generates HTML for main Prices Admin page |
|
447 | + * |
|
448 | + * @return void |
|
449 | + * @throws EE_Error |
|
450 | + */ |
|
451 | + protected function _price_overview_list_table() |
|
452 | + { |
|
453 | + $this->_admin_page_title .= ' ' . $this->get_action_link_or_button( |
|
454 | + 'add_new_price', |
|
455 | + 'add', |
|
456 | + [], |
|
457 | + 'add-new-h2' |
|
458 | + ); |
|
459 | + $this->_admin_page_title .= $this->_learn_more_about_pricing_link(); |
|
460 | + $this->_search_btn_label = esc_html__('Default Prices', 'event_espresso'); |
|
461 | + $this->display_admin_list_table_page_with_sidebar(); |
|
462 | + } |
|
463 | + |
|
464 | + |
|
465 | + /** |
|
466 | + * retrieve data for Prices List table |
|
467 | + * |
|
468 | + * @param int $per_page how many prices displayed per page |
|
469 | + * @param bool $count return the count or objects |
|
470 | + * @param bool $trashed whether the current view is of the trash can - eww yuck! |
|
471 | + * @return EE_Soft_Delete_Base_Class[]|int int = count || array of price objects |
|
472 | + * @throws EE_Error |
|
473 | + * @throws ReflectionException |
|
474 | + */ |
|
475 | + public function get_prices_overview_data(int $per_page = 10, bool $count = false, bool $trashed = false) |
|
476 | + { |
|
477 | + // start with an empty array |
|
478 | + $event_pricing = []; |
|
479 | + |
|
480 | + require_once(PRICING_ADMIN . 'Prices_List_Table.class.php'); |
|
481 | + |
|
482 | + $orderby = $this->request->getRequestParam('orderby', ''); |
|
483 | + $order = $this->request->getRequestParam('order', 'ASC'); |
|
484 | + |
|
485 | + switch ($orderby) { |
|
486 | + case 'name': |
|
487 | + $orderby = ['PRC_name' => $order]; |
|
488 | + break; |
|
489 | + case 'type': |
|
490 | + $orderby = ['Price_Type.PRT_name' => $order]; |
|
491 | + break; |
|
492 | + case 'amount': |
|
493 | + $orderby = ['PRC_amount' => $order]; |
|
494 | + break; |
|
495 | + default: |
|
496 | + $orderby = ['PRC_order' => $order, 'Price_Type.PRT_order' => $order, 'PRC_ID' => $order]; |
|
497 | + } |
|
498 | + |
|
499 | + $current_page = $this->request->getRequestParam('paged', 1, DataType::INTEGER); |
|
500 | + $per_page = $this->request->getRequestParam('perpage', $per_page, DataType::INTEGER); |
|
501 | + |
|
502 | + $where = [ |
|
503 | + 'PRC_is_default' => 1, |
|
504 | + 'PRC_deleted' => $trashed, |
|
505 | + ]; |
|
506 | + |
|
507 | + $offset = ($current_page - 1) * $per_page; |
|
508 | + $limit = [$offset, $per_page]; |
|
509 | + |
|
510 | + $search_term = $this->request->getRequestParam('s'); |
|
511 | + if ($search_term) { |
|
512 | + $search_term = "%{$search_term}%"; |
|
513 | + $where['OR'] = [ |
|
514 | + 'PRC_name' => ['LIKE', $search_term], |
|
515 | + 'PRC_desc' => ['LIKE', $search_term], |
|
516 | + 'PRC_amount' => ['LIKE', $search_term], |
|
517 | + 'Price_Type.PRT_name' => ['LIKE', $search_term], |
|
518 | + ]; |
|
519 | + } |
|
520 | + |
|
521 | + $query_params = [ |
|
522 | + $where, |
|
523 | + 'order_by' => $orderby, |
|
524 | + 'limit' => $limit, |
|
525 | + 'group_by' => 'PRC_ID', |
|
526 | + ]; |
|
527 | + |
|
528 | + if ($count) { |
|
529 | + return $trashed |
|
530 | + ? EEM_Price::instance()->count([$where]) |
|
531 | + : EEM_Price::instance()->count_deleted_and_undeleted([$where]); |
|
532 | + } |
|
533 | + return EEM_Price::instance()->get_all_deleted_and_undeleted($query_params); |
|
534 | + } |
|
535 | + |
|
536 | + |
|
537 | + /** |
|
538 | + * @return void |
|
539 | + * @throws EE_Error |
|
540 | + * @throws ReflectionException |
|
541 | + */ |
|
542 | + protected function _edit_price_details() |
|
543 | + { |
|
544 | + // grab price ID |
|
545 | + $PRC_ID = $this->request->getRequestParam('id', 0, DataType::INTEGER); |
|
546 | + // change page title based on request action |
|
547 | + switch ($this->_req_action) { |
|
548 | + case 'add_new_price': |
|
549 | + $this->_admin_page_title = esc_html__('Add New Price', 'event_espresso'); |
|
550 | + break; |
|
551 | + case 'edit_price': |
|
552 | + $this->_admin_page_title = esc_html__('Edit Price', 'event_espresso'); |
|
553 | + break; |
|
554 | + default: |
|
555 | + $this->_admin_page_title = ucwords(str_replace('_', ' ', $this->_req_action)); |
|
556 | + } |
|
557 | + // add PRC_ID to title if editing |
|
558 | + $this->_admin_page_title = $PRC_ID ? $this->_admin_page_title . ' # ' . $PRC_ID : $this->_admin_page_title; |
|
559 | + |
|
560 | + if ($PRC_ID) { |
|
561 | + $price = EEM_Price::instance()->get_one_by_ID($PRC_ID); |
|
562 | + $additional_hidden_fields = [ |
|
563 | + 'PRC_ID' => ['type' => 'hidden', 'value' => $PRC_ID], |
|
564 | + ]; |
|
565 | + $this->_set_add_edit_form_tags('update_price', $additional_hidden_fields); |
|
566 | + } else { |
|
567 | + $price = EEM_Price::instance()->get_new_price(); |
|
568 | + $this->_set_add_edit_form_tags('insert_price'); |
|
569 | + } |
|
570 | + |
|
571 | + if (! $price instanceof EE_Price) { |
|
572 | + throw new RuntimeException( |
|
573 | + sprintf( |
|
574 | + esc_html__( |
|
575 | + 'A valid Price could not be retrieved from the database with ID: %1$s', |
|
576 | + 'event_espresso' |
|
577 | + ), |
|
578 | + $PRC_ID |
|
579 | + ) |
|
580 | + ); |
|
581 | + } |
|
582 | + |
|
583 | + $this->_template_args['PRC_ID'] = $PRC_ID; |
|
584 | + $this->_template_args['price'] = $price; |
|
585 | + |
|
586 | + $default_base_price = $price->type_obj() && $price->type_obj()->base_type() === 1; |
|
587 | + |
|
588 | + $this->_template_args['default_base_price'] = $default_base_price; |
|
589 | + |
|
590 | + // get price types |
|
591 | + $price_types = EEM_Price_Type::instance()->get_all([['PBT_ID' => ['!=', 1]]]); |
|
592 | + if (empty($price_types)) { |
|
593 | + $msg = esc_html__( |
|
594 | + 'You have no price types defined. Please add a price type before adding a price.', |
|
595 | + 'event_espresso' |
|
596 | + ); |
|
597 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
598 | + $this->display_admin_page_with_sidebar(); |
|
599 | + } |
|
600 | + $attributes = []; |
|
601 | + $price_type_names = []; |
|
602 | + $attributes[] = 'id="PRT_ID"'; |
|
603 | + if ($default_base_price) { |
|
604 | + $attributes[] = 'disabled="disabled"'; |
|
605 | + $price_type_names[] = ['id' => 1, 'text' => esc_html__('Base Price', 'event_espresso')]; |
|
606 | + } |
|
607 | + foreach ($price_types as $type) { |
|
608 | + $price_type_names[] = ['id' => $type->ID(), 'text' => $type->name()]; |
|
609 | + } |
|
610 | + $this->_template_args['attributes'] = implode(' ', $attributes); |
|
611 | + $this->_template_args['price_types'] = $price_type_names; |
|
612 | + |
|
613 | + $this->_template_args['learn_more_about_pricing_link'] = $this->_learn_more_about_pricing_link(); |
|
614 | + $this->_template_args['admin_page_content'] = $this->_edit_price_details_meta_box(); |
|
615 | + |
|
616 | + $this->_set_publish_post_box_vars('id', $PRC_ID); |
|
617 | + // the details template wrapper |
|
618 | + $this->display_admin_page_with_sidebar(); |
|
619 | + } |
|
620 | + |
|
621 | + |
|
622 | + /** |
|
623 | + * |
|
624 | + * @return string |
|
625 | + */ |
|
626 | + public function _edit_price_details_meta_box(): string |
|
627 | + { |
|
628 | + return EEH_Template::display_template( |
|
629 | + PRICING_TEMPLATE_PATH . 'pricing_details_main_meta_box.template.php', |
|
630 | + $this->_template_args, |
|
631 | + true |
|
632 | + ); |
|
633 | + } |
|
634 | + |
|
635 | + |
|
636 | + /** |
|
637 | + * @return array |
|
638 | + * @throws EE_Error |
|
639 | + * @throws ReflectionException |
|
640 | + */ |
|
641 | + protected function set_price_column_values(): array |
|
642 | + { |
|
643 | + $PRC_order = 0; |
|
644 | + $PRT_ID = $this->request->getRequestParam('PRT_ID', 0, DataType::INTEGER); |
|
645 | + if ($PRT_ID) { |
|
646 | + /** @var EE_Price_Type $price_type */ |
|
647 | + $price_type = EEM_Price_Type::instance()->get_one_by_ID($PRT_ID); |
|
648 | + if ($price_type instanceof EE_Price_Type) { |
|
649 | + $PRC_order = $price_type->order(); |
|
650 | + } |
|
651 | + } |
|
652 | + return [ |
|
653 | + 'PRT_ID' => $PRT_ID, |
|
654 | + 'PRC_amount' => $this->request->getRequestParam('PRC_amount', 0, DataType::FLOAT), |
|
655 | + 'PRC_name' => $this->request->getRequestParam('PRC_name'), |
|
656 | + 'PRC_desc' => $this->request->getRequestParam('PRC_desc'), |
|
657 | + 'PRC_is_default' => 1, |
|
658 | + 'PRC_overrides' => null, |
|
659 | + 'PRC_order' => $PRC_order, |
|
660 | + 'PRC_deleted' => 0, |
|
661 | + 'PRC_parent' => 0, |
|
662 | + ]; |
|
663 | + } |
|
664 | + |
|
665 | + |
|
666 | + /** |
|
667 | + * @param bool $insert - whether to insert or update |
|
668 | + * @return void |
|
669 | + * @throws EE_Error |
|
670 | + * @throws ReflectionException |
|
671 | + */ |
|
672 | + protected function _insert_or_update_price(bool $insert = false) |
|
673 | + { |
|
674 | + // why be so pessimistic ??? : ( |
|
675 | + $updated = 0; |
|
676 | + |
|
677 | + $set_column_values = $this->set_price_column_values(); |
|
678 | + // is this a new Price ? |
|
679 | + if ($insert) { |
|
680 | + // run the insert |
|
681 | + $PRC_ID = EEM_Price::instance()->insert($set_column_values); |
|
682 | + if ($PRC_ID) { |
|
683 | + // make sure this new price modifier is attached to the ticket but ONLY if it is not a tax type |
|
684 | + $price = EEM_price::instance()->get_one_by_ID($PRC_ID); |
|
685 | + if ( |
|
686 | + $price instanceof EE_Price |
|
687 | + && $price->type_obj() instanceof EE_Price_type |
|
688 | + && $price->type_obj()->base_type() !== EEM_Price_Type::base_type_tax |
|
689 | + ) { |
|
690 | + $ticket = EEM_Ticket::instance()->get_one_by_ID(1); |
|
691 | + $ticket->_add_relation_to($price, 'Price'); |
|
692 | + $ticket->save(); |
|
693 | + } |
|
694 | + $updated = 1; |
|
695 | + } |
|
696 | + $action_desc = 'created'; |
|
697 | + } else { |
|
698 | + $PRC_ID = $this->request->getRequestParam('PRC_ID', 0, DataType::INTEGER); |
|
699 | + // run the update |
|
700 | + $where_cols_n_values = ['PRC_ID' => $PRC_ID]; |
|
701 | + $updated = EEM_Price::instance()->update($set_column_values, [$where_cols_n_values]); |
|
702 | + |
|
703 | + $price = EEM_Price::instance()->get_one_by_ID($PRC_ID); |
|
704 | + if ($price instanceof EE_Price && $price->type_obj()->base_type() !== EEM_Price_Type::base_type_tax) { |
|
705 | + // if this is $PRC_ID == 1, |
|
706 | + // then we need to update the default ticket attached to this price so the TKT_price value is updated. |
|
707 | + if ($PRC_ID === 1) { |
|
708 | + $ticket = $price->get_first_related('Ticket'); |
|
709 | + if ($ticket) { |
|
710 | + $ticket->set('TKT_price', $price->get('PRC_amount')); |
|
711 | + $ticket->set('TKT_name', $price->get('PRC_name')); |
|
712 | + $ticket->set('TKT_description', $price->get('PRC_desc')); |
|
713 | + $ticket->save(); |
|
714 | + } |
|
715 | + } else { |
|
716 | + // we make sure this price is attached to base ticket. but ONLY if its not a tax ticket type. |
|
717 | + $ticket = EEM_Ticket::instance()->get_one_by_ID(1); |
|
718 | + $ticket->_add_relation_to($PRC_ID, 'Price'); |
|
719 | + $ticket->save(); |
|
720 | + } |
|
721 | + } |
|
722 | + |
|
723 | + $action_desc = 'updated'; |
|
724 | + } |
|
725 | + |
|
726 | + $query_args = ['action' => 'edit_price', 'id' => $PRC_ID]; |
|
727 | + |
|
728 | + $this->_redirect_after_action($updated, 'Prices', $action_desc, $query_args); |
|
729 | + } |
|
730 | + |
|
731 | + |
|
732 | + /** |
|
733 | + * @param bool $trash - whether to move item to trash (TRUE) or restore it (FALSE) |
|
734 | + * @return void |
|
735 | + * @throws EE_Error |
|
736 | + * @throws ReflectionException |
|
737 | + */ |
|
738 | + protected function _trash_or_restore_price(bool $trash = true) |
|
739 | + { |
|
740 | + $entity_model = EEM_Price::instance(); |
|
741 | + $action = $trash ? EE_Admin_List_Table::ACTION_TRASH : EE_Admin_List_Table::ACTION_RESTORE; |
|
742 | + $result = $this->trashRestoreDeleteEntities( |
|
743 | + $entity_model, |
|
744 | + 'id', |
|
745 | + $action, |
|
746 | + 'PRC_deleted', |
|
747 | + [$this, 'adjustTicketRelations'] |
|
748 | + ); |
|
749 | + |
|
750 | + if ($result) { |
|
751 | + $msg = $trash |
|
752 | + ? esc_html( |
|
753 | + _n( |
|
754 | + 'The Price has been trashed', |
|
755 | + 'The Prices have been trashed', |
|
756 | + $result, |
|
757 | + 'event_espresso' |
|
758 | + ) |
|
759 | + ) |
|
760 | + : esc_html( |
|
761 | + _n( |
|
762 | + 'The Price has been restored', |
|
763 | + 'The Prices have been restored', |
|
764 | + $result, |
|
765 | + 'event_espresso' |
|
766 | + ) |
|
767 | + ); |
|
768 | + EE_Error::add_success($msg); |
|
769 | + } |
|
770 | + |
|
771 | + $this->_redirect_after_action( |
|
772 | + $result, |
|
773 | + _n('Price', 'Prices', $result, 'event_espresso'), |
|
774 | + $trash ? 'trashed' : 'restored', |
|
775 | + ['action' => 'default'], |
|
776 | + true |
|
777 | + ); |
|
778 | + } |
|
779 | + |
|
780 | + |
|
781 | + /** |
|
782 | + * @param EEM_Base $entity_model |
|
783 | + * @param int|string $entity_ID |
|
784 | + * @param string $action |
|
785 | + * @param int $result |
|
786 | + * @throws EE_Error |
|
787 | + * @throws ReflectionException |
|
788 | + * @since $VID:$ |
|
789 | + */ |
|
790 | + public function adjustTicketRelations(EEM_Base $entity_model, $entity_ID, string $action, int $result) |
|
791 | + { |
|
792 | + if (! $entity_ID || (float) $result < 1) { |
|
793 | + return; |
|
794 | + } |
|
795 | + |
|
796 | + $entity = $entity_model->get_one_by_ID($entity_ID); |
|
797 | + if (! $entity instanceof EE_Price || $entity->type_obj()->base_type() === EEM_Price_Type::base_type_tax) { |
|
798 | + return; |
|
799 | + } |
|
800 | + |
|
801 | + // get default tickets for updating |
|
802 | + $default_tickets = EEM_Ticket::instance()->get_all_default_tickets(); |
|
803 | + foreach ($default_tickets as $default_ticket) { |
|
804 | + if (! $default_ticket instanceof EE_Ticket) { |
|
805 | + continue; |
|
806 | + } |
|
807 | + switch ($action) { |
|
808 | + case EE_Admin_List_Table::ACTION_DELETE: |
|
809 | + case EE_Admin_List_Table::ACTION_TRASH: |
|
810 | + // if trashing then remove relations to base default ticket. |
|
811 | + $default_ticket->_remove_relation_to($entity_ID, 'Price'); |
|
812 | + break; |
|
813 | + case EE_Admin_List_Table::ACTION_RESTORE: |
|
814 | + // if restoring then add back to base default ticket |
|
815 | + $default_ticket->_add_relation_to($entity_ID, 'Price'); |
|
816 | + break; |
|
817 | + } |
|
818 | + $default_ticket->save(); |
|
819 | + } |
|
820 | + } |
|
821 | + |
|
822 | + |
|
823 | + /** |
|
824 | + * @return void |
|
825 | + * @throws EE_Error |
|
826 | + * @throws ReflectionException |
|
827 | + */ |
|
828 | + protected function _delete_price() |
|
829 | + { |
|
830 | + $entity_model = EEM_Price::instance(); |
|
831 | + $deleted = $this->trashRestoreDeleteEntities($entity_model, 'id'); |
|
832 | + $entity = $entity_model->item_name($deleted); |
|
833 | + $this->_redirect_after_action($deleted, $entity, 'deleted', ['action' => 'default']); |
|
834 | + } |
|
835 | + |
|
836 | + |
|
837 | + /** |
|
838 | + * @throws EE_Error |
|
839 | + * @throws ReflectionException |
|
840 | + */ |
|
841 | + public function update_price_order() |
|
842 | + { |
|
843 | + // grab our row IDs |
|
844 | + $row_ids = $this->request->getRequestParam('row_ids', ''); |
|
845 | + $row_ids = explode(',', rtrim($row_ids, ',')); |
|
846 | + |
|
847 | + $all_updated = true; |
|
848 | + foreach ($row_ids as $i => $row_id) { |
|
849 | + // Update the prices when re-ordering |
|
850 | + $fields_n_values = ['PRC_order' => $i + 1]; |
|
851 | + $query_params = [['PRC_ID' => absint($row_id)]]; |
|
852 | + // any failure will toggle $all_updated to false |
|
853 | + $all_updated = $row_id && EEM_Price::instance()->update($fields_n_values, $query_params) !== false |
|
854 | + ? $all_updated |
|
855 | + : false; |
|
856 | + } |
|
857 | + $success = $all_updated ? esc_html__('Price order was updated successfully.', 'event_espresso') : false; |
|
858 | + $errors = ! $all_updated ? esc_html__('An error occurred. The price order was not updated.', 'event_espresso') |
|
859 | + : false; |
|
860 | + |
|
861 | + echo wp_json_encode(['return_data' => false, 'success' => $success, 'errors' => $errors]); |
|
862 | + die(); |
|
863 | + } |
|
864 | + |
|
865 | + |
|
866 | + /****************************************************************************************************************** |
|
867 | 867 | *********************************************** TICKET PRICE TYPES ********************************************* |
868 | 868 | ******************************************************************************************************************/ |
869 | 869 | |
870 | 870 | |
871 | - /** |
|
872 | - * generates HTML for main Prices Admin page |
|
873 | - * |
|
874 | - * @return void |
|
875 | - * @throws EE_Error |
|
876 | - */ |
|
877 | - protected function _price_types_overview_list_table() |
|
878 | - { |
|
879 | - $this->_admin_page_title .= ' ' . $this->get_action_link_or_button( |
|
880 | - 'add_new_price_type', |
|
881 | - 'add_type', |
|
882 | - [], |
|
883 | - 'add-new-h2' |
|
884 | - ); |
|
885 | - $this->_admin_page_title .= $this->_learn_more_about_pricing_link(); |
|
886 | - $this->_search_btn_label = esc_html__('Price Types', 'event_espresso'); |
|
887 | - $this->display_admin_list_table_page_with_sidebar(); |
|
888 | - } |
|
889 | - |
|
890 | - |
|
891 | - /** |
|
892 | - * retrieve data for Price Types List table |
|
893 | - * |
|
894 | - * @param int $per_page how many prices displayed per page |
|
895 | - * @param bool $count return the count or objects |
|
896 | - * @param bool $trashed whether the current view is of the trash can - eww yuck! |
|
897 | - * @return EE_Soft_Delete_Base_Class[]|int int = count || array of price objects |
|
898 | - * @throws EE_Error |
|
899 | - * @throws ReflectionException |
|
900 | - */ |
|
901 | - public function get_price_types_overview_data(int $per_page = 10, bool $count = false, bool $trashed = false) |
|
902 | - { |
|
903 | - // start with an empty array |
|
904 | - require_once(PRICING_ADMIN . 'Price_Types_List_Table.class.php'); |
|
905 | - |
|
906 | - $orderby = $this->request->getRequestParam('orderby', ''); |
|
907 | - $order = $this->request->getRequestParam('order', 'ASC'); |
|
908 | - |
|
909 | - switch ($orderby) { |
|
910 | - case 'name': |
|
911 | - $orderby = ['PRT_name' => $order]; |
|
912 | - break; |
|
913 | - default: |
|
914 | - $orderby = ['PRT_order' => $order]; |
|
915 | - } |
|
916 | - |
|
917 | - $current_page = $this->request->getRequestParam('paged', 1, DataType::INTEGER); |
|
918 | - $per_page = $this->request->getRequestParam('perpage', $per_page, DataType::INTEGER); |
|
919 | - |
|
920 | - $offset = ($current_page - 1) * $per_page; |
|
921 | - $limit = [$offset, $per_page]; |
|
922 | - |
|
923 | - $where = ['PRT_deleted' => $trashed, 'PBT_ID' => ['!=', 1]]; |
|
924 | - |
|
925 | - $search_term = $this->request->getRequestParam('s'); |
|
926 | - if ($search_term) { |
|
927 | - $where['OR'] = [ |
|
928 | - 'PRT_name' => ['LIKE', "%{$search_term}%"], |
|
929 | - ]; |
|
930 | - } |
|
931 | - $query_params = [ |
|
932 | - $where, |
|
933 | - 'order_by' => $orderby, |
|
934 | - 'limit' => $limit, |
|
935 | - ]; |
|
936 | - return $count |
|
937 | - ? EEM_Price_Type::instance()->count_deleted_and_undeleted($query_params) |
|
938 | - : EEM_Price_Type::instance()->get_all_deleted_and_undeleted($query_params); |
|
939 | - } |
|
940 | - |
|
941 | - |
|
942 | - /** |
|
943 | - * _edit_price_type_details |
|
944 | - * |
|
945 | - * @return void |
|
946 | - * @throws EE_Error |
|
947 | - * @throws ReflectionException |
|
948 | - */ |
|
949 | - protected function _edit_price_type_details() |
|
950 | - { |
|
951 | - // grab price type ID |
|
952 | - $PRT_ID = $this->request->getRequestParam('id', 0, DataType::INTEGER); |
|
953 | - // change page title based on request action |
|
954 | - switch ($this->_req_action) { |
|
955 | - case 'add_new_price_type': |
|
956 | - $this->_admin_page_title = esc_html__('Add New Price Type', 'event_espresso'); |
|
957 | - break; |
|
958 | - case 'edit_price_type': |
|
959 | - $this->_admin_page_title = esc_html__('Edit Price Type', 'event_espresso'); |
|
960 | - break; |
|
961 | - default: |
|
962 | - $this->_admin_page_title = ucwords(str_replace('_', ' ', $this->_req_action)); |
|
963 | - } |
|
964 | - // add PRT_ID to title if editing |
|
965 | - $this->_admin_page_title = $PRT_ID ? $this->_admin_page_title . ' # ' . $PRT_ID : $this->_admin_page_title; |
|
966 | - |
|
967 | - if ($PRT_ID) { |
|
968 | - $price_type = EEM_Price_Type::instance()->get_one_by_ID($PRT_ID); |
|
969 | - $additional_hidden_fields = ['PRT_ID' => ['type' => 'hidden', 'value' => $PRT_ID]]; |
|
970 | - $this->_set_add_edit_form_tags('update_price_type', $additional_hidden_fields); |
|
971 | - } else { |
|
972 | - $price_type = EEM_Price_Type::instance()->get_new_price_type(); |
|
973 | - $this->_set_add_edit_form_tags('insert_price_type'); |
|
974 | - } |
|
975 | - |
|
976 | - if (! $price_type instanceof EE_Price_Type) { |
|
977 | - throw new RuntimeException( |
|
978 | - sprintf( |
|
979 | - esc_html__( |
|
980 | - 'A valid Price Type could not be retrieved from the database with ID: %1$s', |
|
981 | - 'event_espresso' |
|
982 | - ), |
|
983 | - $PRT_ID |
|
984 | - ) |
|
985 | - ); |
|
986 | - } |
|
987 | - |
|
988 | - $this->_template_args['PRT_ID'] = $PRT_ID; |
|
989 | - $this->_template_args['price_type'] = $price_type; |
|
990 | - |
|
991 | - $base_types = EEM_Price_Type::instance()->get_base_types(); |
|
992 | - $select_values = []; |
|
993 | - foreach ($base_types as $ref => $text) { |
|
994 | - if ($ref == EEM_Price_Type::base_type_base_price) { |
|
995 | - // do not allow creation of base_type_base_prices because that's a system only base type. |
|
996 | - continue; |
|
997 | - } |
|
998 | - $select_values[] = ['id' => $ref, 'text' => $text]; |
|
999 | - } |
|
1000 | - |
|
1001 | - $this->_template_args['base_type_select'] = EEH_Form_Fields::select_input( |
|
1002 | - 'base_type', |
|
1003 | - $select_values, |
|
1004 | - $price_type->base_type(), |
|
1005 | - 'id="price-type-base-type-slct"' |
|
1006 | - ); |
|
1007 | - |
|
1008 | - $this->_template_args['learn_more_about_pricing_link'] = $this->_learn_more_about_pricing_link(); |
|
1009 | - $this->_template_args['admin_page_content'] = $this->_edit_price_type_details_meta_box(); |
|
1010 | - |
|
1011 | - $redirect_URL = add_query_arg(['action' => 'price_types'], $this->_admin_base_url); |
|
1012 | - $this->_set_publish_post_box_vars('id', $PRT_ID, false, $redirect_URL); |
|
1013 | - // the details template wrapper |
|
1014 | - $this->display_admin_page_with_sidebar(); |
|
1015 | - } |
|
1016 | - |
|
1017 | - |
|
1018 | - /** |
|
1019 | - * _edit_price_type_details_meta_box |
|
1020 | - * |
|
1021 | - * @return string |
|
1022 | - */ |
|
1023 | - public function _edit_price_type_details_meta_box(): string |
|
1024 | - { |
|
1025 | - return EEH_Template::display_template( |
|
1026 | - PRICING_TEMPLATE_PATH . 'pricing_type_details_main_meta_box.template.php', |
|
1027 | - $this->_template_args, |
|
1028 | - true |
|
1029 | - ); |
|
1030 | - } |
|
1031 | - |
|
1032 | - |
|
1033 | - /** |
|
1034 | - * @return array |
|
1035 | - */ |
|
1036 | - protected function set_price_type_column_values(): array |
|
1037 | - { |
|
1038 | - $base_type = $this->request->getRequestParam( |
|
1039 | - 'base_type', |
|
1040 | - EEM_Price_Type::base_type_base_price, |
|
1041 | - DataType::INTEGER |
|
1042 | - ); |
|
1043 | - $is_percent = $this->request->getRequestParam('PRT_is_percent', 0, DataType::INTEGER); |
|
1044 | - $order = $this->request->getRequestParam('PRT_order', 0, DataType::INTEGER); |
|
1045 | - switch ($base_type) { |
|
1046 | - case EEM_Price_Type::base_type_base_price: |
|
1047 | - $is_percent = 0; |
|
1048 | - $order = 0; |
|
1049 | - break; |
|
1050 | - |
|
1051 | - case EEM_Price_Type::base_type_discount: |
|
1052 | - case EEM_Price_Type::base_type_surcharge: |
|
1053 | - break; |
|
1054 | - |
|
1055 | - case EEM_Price_Type::base_type_tax: |
|
1056 | - $is_percent = 1; |
|
1057 | - break; |
|
1058 | - } |
|
1059 | - |
|
1060 | - return [ |
|
1061 | - 'PBT_ID' => $base_type, |
|
1062 | - 'PRT_name' => $this->request->getRequestParam('PRT_name', ''), |
|
1063 | - 'PRT_is_percent' => $is_percent, |
|
1064 | - 'PRT_order' => $order, |
|
1065 | - 'PRT_deleted' => 0, |
|
1066 | - ]; |
|
1067 | - } |
|
1068 | - |
|
1069 | - |
|
1070 | - /** |
|
1071 | - * @param bool $new_price_type - whether to insert or update |
|
1072 | - * @return void |
|
1073 | - * @throws EE_Error |
|
1074 | - * @throws ReflectionException |
|
1075 | - */ |
|
1076 | - protected function _insert_or_update_price_type(bool $new_price_type = false) |
|
1077 | - { |
|
1078 | - // why be so pessimistic ??? : ( |
|
1079 | - $success = 0; |
|
1080 | - |
|
1081 | - $set_column_values = $this->set_price_type_column_values(); |
|
1082 | - // is this a new Price ? |
|
1083 | - if ($new_price_type) { |
|
1084 | - // run the insert |
|
1085 | - if ($PRT_ID = EEM_Price_Type::instance()->insert($set_column_values)) { |
|
1086 | - $success = 1; |
|
1087 | - } |
|
1088 | - $action_desc = 'created'; |
|
1089 | - } else { |
|
1090 | - $PRT_ID = $this->request->getRequestParam('PRT_ID', 0, DataType::INTEGER); |
|
1091 | - // run the update |
|
1092 | - $where_cols_n_values = ['PRT_ID' => $PRT_ID]; |
|
1093 | - if (EEM_Price_Type::instance()->update($set_column_values, [$where_cols_n_values])) { |
|
1094 | - $success = 1; |
|
1095 | - } |
|
1096 | - $action_desc = 'updated'; |
|
1097 | - } |
|
1098 | - |
|
1099 | - $query_args = ['action' => 'edit_price_type', 'id' => $PRT_ID]; |
|
1100 | - $this->_redirect_after_action($success, 'Price Type', $action_desc, $query_args); |
|
1101 | - } |
|
1102 | - |
|
1103 | - |
|
1104 | - /** |
|
1105 | - * @param bool $trash - whether to move item to trash (TRUE) or restore it (FALSE) |
|
1106 | - * @return void |
|
1107 | - * @throws EE_Error |
|
1108 | - * @throws ReflectionException |
|
1109 | - */ |
|
1110 | - protected function _trash_or_restore_price_type(bool $trash = true) |
|
1111 | - { |
|
1112 | - $entity_model = EEM_Price_Type::instance(); |
|
1113 | - $action = $trash ? EE_Admin_List_Table::ACTION_TRASH : EE_Admin_List_Table::ACTION_RESTORE; |
|
1114 | - $success = $this->trashRestoreDeleteEntities($entity_model, 'id', $action, 'PRT_deleted'); |
|
1115 | - if ($success) { |
|
1116 | - $msg = $trash |
|
1117 | - ? esc_html( |
|
1118 | - _n( |
|
1119 | - 'The Price Type has been trashed', |
|
1120 | - 'The Price Types have been trashed', |
|
1121 | - $success, |
|
1122 | - 'event_espresso' |
|
1123 | - ) |
|
1124 | - ) |
|
1125 | - : esc_html( |
|
1126 | - _n( |
|
1127 | - 'The Price Type has been restored', |
|
1128 | - 'The Price Types have been restored', |
|
1129 | - $success, |
|
1130 | - 'event_espresso' |
|
1131 | - ) |
|
1132 | - ); |
|
1133 | - EE_Error::add_success($msg); |
|
1134 | - } |
|
1135 | - $this->_redirect_after_action('', '', '', ['action' => 'price_types'], true); |
|
1136 | - } |
|
1137 | - |
|
1138 | - |
|
1139 | - /** |
|
1140 | - * @return void |
|
1141 | - * @throws EE_Error |
|
1142 | - * @throws ReflectionException |
|
1143 | - */ |
|
1144 | - protected function _delete_price_type() |
|
1145 | - { |
|
1146 | - $entity_model = EEM_Price_Type::instance(); |
|
1147 | - $deleted = $this->trashRestoreDeleteEntities($entity_model, 'id'); |
|
1148 | - $this->_redirect_after_action( |
|
1149 | - $deleted, |
|
1150 | - $entity_model->item_name($deleted), |
|
1151 | - 'deleted', |
|
1152 | - ['action' => 'price_types'] |
|
1153 | - ); |
|
1154 | - } |
|
1155 | - |
|
1156 | - |
|
1157 | - /** |
|
1158 | - * @return string |
|
1159 | - */ |
|
1160 | - protected function _learn_more_about_pricing_link(): string |
|
1161 | - { |
|
1162 | - return ' |
|
871 | + /** |
|
872 | + * generates HTML for main Prices Admin page |
|
873 | + * |
|
874 | + * @return void |
|
875 | + * @throws EE_Error |
|
876 | + */ |
|
877 | + protected function _price_types_overview_list_table() |
|
878 | + { |
|
879 | + $this->_admin_page_title .= ' ' . $this->get_action_link_or_button( |
|
880 | + 'add_new_price_type', |
|
881 | + 'add_type', |
|
882 | + [], |
|
883 | + 'add-new-h2' |
|
884 | + ); |
|
885 | + $this->_admin_page_title .= $this->_learn_more_about_pricing_link(); |
|
886 | + $this->_search_btn_label = esc_html__('Price Types', 'event_espresso'); |
|
887 | + $this->display_admin_list_table_page_with_sidebar(); |
|
888 | + } |
|
889 | + |
|
890 | + |
|
891 | + /** |
|
892 | + * retrieve data for Price Types List table |
|
893 | + * |
|
894 | + * @param int $per_page how many prices displayed per page |
|
895 | + * @param bool $count return the count or objects |
|
896 | + * @param bool $trashed whether the current view is of the trash can - eww yuck! |
|
897 | + * @return EE_Soft_Delete_Base_Class[]|int int = count || array of price objects |
|
898 | + * @throws EE_Error |
|
899 | + * @throws ReflectionException |
|
900 | + */ |
|
901 | + public function get_price_types_overview_data(int $per_page = 10, bool $count = false, bool $trashed = false) |
|
902 | + { |
|
903 | + // start with an empty array |
|
904 | + require_once(PRICING_ADMIN . 'Price_Types_List_Table.class.php'); |
|
905 | + |
|
906 | + $orderby = $this->request->getRequestParam('orderby', ''); |
|
907 | + $order = $this->request->getRequestParam('order', 'ASC'); |
|
908 | + |
|
909 | + switch ($orderby) { |
|
910 | + case 'name': |
|
911 | + $orderby = ['PRT_name' => $order]; |
|
912 | + break; |
|
913 | + default: |
|
914 | + $orderby = ['PRT_order' => $order]; |
|
915 | + } |
|
916 | + |
|
917 | + $current_page = $this->request->getRequestParam('paged', 1, DataType::INTEGER); |
|
918 | + $per_page = $this->request->getRequestParam('perpage', $per_page, DataType::INTEGER); |
|
919 | + |
|
920 | + $offset = ($current_page - 1) * $per_page; |
|
921 | + $limit = [$offset, $per_page]; |
|
922 | + |
|
923 | + $where = ['PRT_deleted' => $trashed, 'PBT_ID' => ['!=', 1]]; |
|
924 | + |
|
925 | + $search_term = $this->request->getRequestParam('s'); |
|
926 | + if ($search_term) { |
|
927 | + $where['OR'] = [ |
|
928 | + 'PRT_name' => ['LIKE', "%{$search_term}%"], |
|
929 | + ]; |
|
930 | + } |
|
931 | + $query_params = [ |
|
932 | + $where, |
|
933 | + 'order_by' => $orderby, |
|
934 | + 'limit' => $limit, |
|
935 | + ]; |
|
936 | + return $count |
|
937 | + ? EEM_Price_Type::instance()->count_deleted_and_undeleted($query_params) |
|
938 | + : EEM_Price_Type::instance()->get_all_deleted_and_undeleted($query_params); |
|
939 | + } |
|
940 | + |
|
941 | + |
|
942 | + /** |
|
943 | + * _edit_price_type_details |
|
944 | + * |
|
945 | + * @return void |
|
946 | + * @throws EE_Error |
|
947 | + * @throws ReflectionException |
|
948 | + */ |
|
949 | + protected function _edit_price_type_details() |
|
950 | + { |
|
951 | + // grab price type ID |
|
952 | + $PRT_ID = $this->request->getRequestParam('id', 0, DataType::INTEGER); |
|
953 | + // change page title based on request action |
|
954 | + switch ($this->_req_action) { |
|
955 | + case 'add_new_price_type': |
|
956 | + $this->_admin_page_title = esc_html__('Add New Price Type', 'event_espresso'); |
|
957 | + break; |
|
958 | + case 'edit_price_type': |
|
959 | + $this->_admin_page_title = esc_html__('Edit Price Type', 'event_espresso'); |
|
960 | + break; |
|
961 | + default: |
|
962 | + $this->_admin_page_title = ucwords(str_replace('_', ' ', $this->_req_action)); |
|
963 | + } |
|
964 | + // add PRT_ID to title if editing |
|
965 | + $this->_admin_page_title = $PRT_ID ? $this->_admin_page_title . ' # ' . $PRT_ID : $this->_admin_page_title; |
|
966 | + |
|
967 | + if ($PRT_ID) { |
|
968 | + $price_type = EEM_Price_Type::instance()->get_one_by_ID($PRT_ID); |
|
969 | + $additional_hidden_fields = ['PRT_ID' => ['type' => 'hidden', 'value' => $PRT_ID]]; |
|
970 | + $this->_set_add_edit_form_tags('update_price_type', $additional_hidden_fields); |
|
971 | + } else { |
|
972 | + $price_type = EEM_Price_Type::instance()->get_new_price_type(); |
|
973 | + $this->_set_add_edit_form_tags('insert_price_type'); |
|
974 | + } |
|
975 | + |
|
976 | + if (! $price_type instanceof EE_Price_Type) { |
|
977 | + throw new RuntimeException( |
|
978 | + sprintf( |
|
979 | + esc_html__( |
|
980 | + 'A valid Price Type could not be retrieved from the database with ID: %1$s', |
|
981 | + 'event_espresso' |
|
982 | + ), |
|
983 | + $PRT_ID |
|
984 | + ) |
|
985 | + ); |
|
986 | + } |
|
987 | + |
|
988 | + $this->_template_args['PRT_ID'] = $PRT_ID; |
|
989 | + $this->_template_args['price_type'] = $price_type; |
|
990 | + |
|
991 | + $base_types = EEM_Price_Type::instance()->get_base_types(); |
|
992 | + $select_values = []; |
|
993 | + foreach ($base_types as $ref => $text) { |
|
994 | + if ($ref == EEM_Price_Type::base_type_base_price) { |
|
995 | + // do not allow creation of base_type_base_prices because that's a system only base type. |
|
996 | + continue; |
|
997 | + } |
|
998 | + $select_values[] = ['id' => $ref, 'text' => $text]; |
|
999 | + } |
|
1000 | + |
|
1001 | + $this->_template_args['base_type_select'] = EEH_Form_Fields::select_input( |
|
1002 | + 'base_type', |
|
1003 | + $select_values, |
|
1004 | + $price_type->base_type(), |
|
1005 | + 'id="price-type-base-type-slct"' |
|
1006 | + ); |
|
1007 | + |
|
1008 | + $this->_template_args['learn_more_about_pricing_link'] = $this->_learn_more_about_pricing_link(); |
|
1009 | + $this->_template_args['admin_page_content'] = $this->_edit_price_type_details_meta_box(); |
|
1010 | + |
|
1011 | + $redirect_URL = add_query_arg(['action' => 'price_types'], $this->_admin_base_url); |
|
1012 | + $this->_set_publish_post_box_vars('id', $PRT_ID, false, $redirect_URL); |
|
1013 | + // the details template wrapper |
|
1014 | + $this->display_admin_page_with_sidebar(); |
|
1015 | + } |
|
1016 | + |
|
1017 | + |
|
1018 | + /** |
|
1019 | + * _edit_price_type_details_meta_box |
|
1020 | + * |
|
1021 | + * @return string |
|
1022 | + */ |
|
1023 | + public function _edit_price_type_details_meta_box(): string |
|
1024 | + { |
|
1025 | + return EEH_Template::display_template( |
|
1026 | + PRICING_TEMPLATE_PATH . 'pricing_type_details_main_meta_box.template.php', |
|
1027 | + $this->_template_args, |
|
1028 | + true |
|
1029 | + ); |
|
1030 | + } |
|
1031 | + |
|
1032 | + |
|
1033 | + /** |
|
1034 | + * @return array |
|
1035 | + */ |
|
1036 | + protected function set_price_type_column_values(): array |
|
1037 | + { |
|
1038 | + $base_type = $this->request->getRequestParam( |
|
1039 | + 'base_type', |
|
1040 | + EEM_Price_Type::base_type_base_price, |
|
1041 | + DataType::INTEGER |
|
1042 | + ); |
|
1043 | + $is_percent = $this->request->getRequestParam('PRT_is_percent', 0, DataType::INTEGER); |
|
1044 | + $order = $this->request->getRequestParam('PRT_order', 0, DataType::INTEGER); |
|
1045 | + switch ($base_type) { |
|
1046 | + case EEM_Price_Type::base_type_base_price: |
|
1047 | + $is_percent = 0; |
|
1048 | + $order = 0; |
|
1049 | + break; |
|
1050 | + |
|
1051 | + case EEM_Price_Type::base_type_discount: |
|
1052 | + case EEM_Price_Type::base_type_surcharge: |
|
1053 | + break; |
|
1054 | + |
|
1055 | + case EEM_Price_Type::base_type_tax: |
|
1056 | + $is_percent = 1; |
|
1057 | + break; |
|
1058 | + } |
|
1059 | + |
|
1060 | + return [ |
|
1061 | + 'PBT_ID' => $base_type, |
|
1062 | + 'PRT_name' => $this->request->getRequestParam('PRT_name', ''), |
|
1063 | + 'PRT_is_percent' => $is_percent, |
|
1064 | + 'PRT_order' => $order, |
|
1065 | + 'PRT_deleted' => 0, |
|
1066 | + ]; |
|
1067 | + } |
|
1068 | + |
|
1069 | + |
|
1070 | + /** |
|
1071 | + * @param bool $new_price_type - whether to insert or update |
|
1072 | + * @return void |
|
1073 | + * @throws EE_Error |
|
1074 | + * @throws ReflectionException |
|
1075 | + */ |
|
1076 | + protected function _insert_or_update_price_type(bool $new_price_type = false) |
|
1077 | + { |
|
1078 | + // why be so pessimistic ??? : ( |
|
1079 | + $success = 0; |
|
1080 | + |
|
1081 | + $set_column_values = $this->set_price_type_column_values(); |
|
1082 | + // is this a new Price ? |
|
1083 | + if ($new_price_type) { |
|
1084 | + // run the insert |
|
1085 | + if ($PRT_ID = EEM_Price_Type::instance()->insert($set_column_values)) { |
|
1086 | + $success = 1; |
|
1087 | + } |
|
1088 | + $action_desc = 'created'; |
|
1089 | + } else { |
|
1090 | + $PRT_ID = $this->request->getRequestParam('PRT_ID', 0, DataType::INTEGER); |
|
1091 | + // run the update |
|
1092 | + $where_cols_n_values = ['PRT_ID' => $PRT_ID]; |
|
1093 | + if (EEM_Price_Type::instance()->update($set_column_values, [$where_cols_n_values])) { |
|
1094 | + $success = 1; |
|
1095 | + } |
|
1096 | + $action_desc = 'updated'; |
|
1097 | + } |
|
1098 | + |
|
1099 | + $query_args = ['action' => 'edit_price_type', 'id' => $PRT_ID]; |
|
1100 | + $this->_redirect_after_action($success, 'Price Type', $action_desc, $query_args); |
|
1101 | + } |
|
1102 | + |
|
1103 | + |
|
1104 | + /** |
|
1105 | + * @param bool $trash - whether to move item to trash (TRUE) or restore it (FALSE) |
|
1106 | + * @return void |
|
1107 | + * @throws EE_Error |
|
1108 | + * @throws ReflectionException |
|
1109 | + */ |
|
1110 | + protected function _trash_or_restore_price_type(bool $trash = true) |
|
1111 | + { |
|
1112 | + $entity_model = EEM_Price_Type::instance(); |
|
1113 | + $action = $trash ? EE_Admin_List_Table::ACTION_TRASH : EE_Admin_List_Table::ACTION_RESTORE; |
|
1114 | + $success = $this->trashRestoreDeleteEntities($entity_model, 'id', $action, 'PRT_deleted'); |
|
1115 | + if ($success) { |
|
1116 | + $msg = $trash |
|
1117 | + ? esc_html( |
|
1118 | + _n( |
|
1119 | + 'The Price Type has been trashed', |
|
1120 | + 'The Price Types have been trashed', |
|
1121 | + $success, |
|
1122 | + 'event_espresso' |
|
1123 | + ) |
|
1124 | + ) |
|
1125 | + : esc_html( |
|
1126 | + _n( |
|
1127 | + 'The Price Type has been restored', |
|
1128 | + 'The Price Types have been restored', |
|
1129 | + $success, |
|
1130 | + 'event_espresso' |
|
1131 | + ) |
|
1132 | + ); |
|
1133 | + EE_Error::add_success($msg); |
|
1134 | + } |
|
1135 | + $this->_redirect_after_action('', '', '', ['action' => 'price_types'], true); |
|
1136 | + } |
|
1137 | + |
|
1138 | + |
|
1139 | + /** |
|
1140 | + * @return void |
|
1141 | + * @throws EE_Error |
|
1142 | + * @throws ReflectionException |
|
1143 | + */ |
|
1144 | + protected function _delete_price_type() |
|
1145 | + { |
|
1146 | + $entity_model = EEM_Price_Type::instance(); |
|
1147 | + $deleted = $this->trashRestoreDeleteEntities($entity_model, 'id'); |
|
1148 | + $this->_redirect_after_action( |
|
1149 | + $deleted, |
|
1150 | + $entity_model->item_name($deleted), |
|
1151 | + 'deleted', |
|
1152 | + ['action' => 'price_types'] |
|
1153 | + ); |
|
1154 | + } |
|
1155 | + |
|
1156 | + |
|
1157 | + /** |
|
1158 | + * @return string |
|
1159 | + */ |
|
1160 | + protected function _learn_more_about_pricing_link(): string |
|
1161 | + { |
|
1162 | + return ' |
|
1163 | 1163 | <a class="hidden" style="margin:0 20px; cursor:pointer; font-size:12px;" > |
1164 | 1164 | ' . esc_html__('learn more about how pricing works', 'event_espresso') . ' |
1165 | 1165 | </a>'; |
1166 | - } |
|
1167 | - |
|
1168 | - |
|
1169 | - /** |
|
1170 | - * @throws EE_Error |
|
1171 | - */ |
|
1172 | - protected function _tax_settings() |
|
1173 | - { |
|
1174 | - $this->_set_add_edit_form_tags('update_tax_settings'); |
|
1175 | - $this->_set_publish_post_box_vars(null, false, false, null, false); |
|
1176 | - $this->_template_args['admin_page_content'] = $this->tax_settings_form()->get_html(); |
|
1177 | - $this->display_admin_page_with_sidebar(); |
|
1178 | - } |
|
1179 | - |
|
1180 | - |
|
1181 | - /** |
|
1182 | - * @return EE_Form_Section_Proper |
|
1183 | - * @throws EE_Error |
|
1184 | - */ |
|
1185 | - protected function tax_settings_form(): EE_Form_Section_Proper |
|
1186 | - { |
|
1187 | - $tax_settings = EE_Config::instance()->tax_settings; |
|
1188 | - return new EE_Form_Section_Proper( |
|
1189 | - [ |
|
1190 | - 'name' => 'tax_settings_form', |
|
1191 | - 'html_id' => 'tax_settings_form', |
|
1192 | - 'html_class' => 'padding', |
|
1193 | - 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
|
1194 | - 'subsections' => apply_filters( |
|
1195 | - 'FHEE__Pricing_Admin_Page__tax_settings_form__form_subsections', |
|
1196 | - [ |
|
1197 | - 'tax_settings' => new EE_Form_Section_Proper( |
|
1198 | - [ |
|
1199 | - 'name' => 'tax_settings_tbl', |
|
1200 | - 'html_id' => 'tax_settings_tbl', |
|
1201 | - 'html_class' => 'form-table', |
|
1202 | - 'layout_strategy' => new EE_Admin_Two_Column_Layout(), |
|
1203 | - 'subsections' => [ |
|
1204 | - 'prices_displayed_including_taxes' => new EE_Yes_No_Input( |
|
1205 | - [ |
|
1206 | - 'html_label_text' => esc_html__( |
|
1207 | - 'Show Prices With Taxes Included?', |
|
1208 | - 'event_espresso' |
|
1209 | - ), |
|
1210 | - 'html_help_text' => esc_html__( |
|
1211 | - 'Indicates whether or not to display prices with the taxes included', |
|
1212 | - 'event_espresso' |
|
1213 | - ), |
|
1214 | - 'default' => $tax_settings->prices_displayed_including_taxes |
|
1215 | - ?? true, |
|
1216 | - 'display_html_label_text' => false, |
|
1217 | - ] |
|
1218 | - ), |
|
1219 | - ], |
|
1220 | - ] |
|
1221 | - ), |
|
1222 | - ] |
|
1223 | - ), |
|
1224 | - ] |
|
1225 | - ); |
|
1226 | - } |
|
1227 | - |
|
1228 | - |
|
1229 | - /** |
|
1230 | - * _update_tax_settings |
|
1231 | - * |
|
1232 | - * @return void |
|
1233 | - * @throws EE_Error |
|
1234 | - * @throws ReflectionException |
|
1235 | - * @since 4.9.13 |
|
1236 | - */ |
|
1237 | - public function _update_tax_settings() |
|
1238 | - { |
|
1239 | - $tax_settings = EE_Config::instance()->tax_settings; |
|
1240 | - if (! $tax_settings instanceof EE_Tax_Config) { |
|
1241 | - $tax_settings = new EE_Tax_Config(); |
|
1242 | - } |
|
1243 | - try { |
|
1244 | - $tax_form = $this->tax_settings_form(); |
|
1245 | - // check for form submission |
|
1246 | - if ($tax_form->was_submitted()) { |
|
1247 | - // capture form data |
|
1248 | - $tax_form->receive_form_submission(); |
|
1249 | - // validate form data |
|
1250 | - if ($tax_form->is_valid()) { |
|
1251 | - // grab validated data from form |
|
1252 | - $valid_data = $tax_form->valid_data(); |
|
1253 | - // set data on config |
|
1254 | - $tax_settings->prices_displayed_including_taxes = |
|
1255 | - $valid_data['tax_settings']['prices_displayed_including_taxes']; |
|
1256 | - } else { |
|
1257 | - if ($tax_form->submission_error_message() !== '') { |
|
1258 | - EE_Error::add_error( |
|
1259 | - $tax_form->submission_error_message(), |
|
1260 | - __FILE__, |
|
1261 | - __FUNCTION__, |
|
1262 | - __LINE__ |
|
1263 | - ); |
|
1264 | - } |
|
1265 | - } |
|
1266 | - } |
|
1267 | - } catch (EE_Error $e) { |
|
1268 | - EE_Error::add_error($e->get_error(), __FILE__, __FUNCTION__, __LINE__); |
|
1269 | - } |
|
1270 | - |
|
1271 | - $what = 'Tax Settings'; |
|
1272 | - $success = $this->_update_espresso_configuration( |
|
1273 | - $what, |
|
1274 | - $tax_settings, |
|
1275 | - __FILE__, |
|
1276 | - __FUNCTION__, |
|
1277 | - __LINE__ |
|
1278 | - ); |
|
1279 | - $this->_redirect_after_action($success, $what, 'updated', ['action' => 'tax_settings']); |
|
1280 | - } |
|
1166 | + } |
|
1167 | + |
|
1168 | + |
|
1169 | + /** |
|
1170 | + * @throws EE_Error |
|
1171 | + */ |
|
1172 | + protected function _tax_settings() |
|
1173 | + { |
|
1174 | + $this->_set_add_edit_form_tags('update_tax_settings'); |
|
1175 | + $this->_set_publish_post_box_vars(null, false, false, null, false); |
|
1176 | + $this->_template_args['admin_page_content'] = $this->tax_settings_form()->get_html(); |
|
1177 | + $this->display_admin_page_with_sidebar(); |
|
1178 | + } |
|
1179 | + |
|
1180 | + |
|
1181 | + /** |
|
1182 | + * @return EE_Form_Section_Proper |
|
1183 | + * @throws EE_Error |
|
1184 | + */ |
|
1185 | + protected function tax_settings_form(): EE_Form_Section_Proper |
|
1186 | + { |
|
1187 | + $tax_settings = EE_Config::instance()->tax_settings; |
|
1188 | + return new EE_Form_Section_Proper( |
|
1189 | + [ |
|
1190 | + 'name' => 'tax_settings_form', |
|
1191 | + 'html_id' => 'tax_settings_form', |
|
1192 | + 'html_class' => 'padding', |
|
1193 | + 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
|
1194 | + 'subsections' => apply_filters( |
|
1195 | + 'FHEE__Pricing_Admin_Page__tax_settings_form__form_subsections', |
|
1196 | + [ |
|
1197 | + 'tax_settings' => new EE_Form_Section_Proper( |
|
1198 | + [ |
|
1199 | + 'name' => 'tax_settings_tbl', |
|
1200 | + 'html_id' => 'tax_settings_tbl', |
|
1201 | + 'html_class' => 'form-table', |
|
1202 | + 'layout_strategy' => new EE_Admin_Two_Column_Layout(), |
|
1203 | + 'subsections' => [ |
|
1204 | + 'prices_displayed_including_taxes' => new EE_Yes_No_Input( |
|
1205 | + [ |
|
1206 | + 'html_label_text' => esc_html__( |
|
1207 | + 'Show Prices With Taxes Included?', |
|
1208 | + 'event_espresso' |
|
1209 | + ), |
|
1210 | + 'html_help_text' => esc_html__( |
|
1211 | + 'Indicates whether or not to display prices with the taxes included', |
|
1212 | + 'event_espresso' |
|
1213 | + ), |
|
1214 | + 'default' => $tax_settings->prices_displayed_including_taxes |
|
1215 | + ?? true, |
|
1216 | + 'display_html_label_text' => false, |
|
1217 | + ] |
|
1218 | + ), |
|
1219 | + ], |
|
1220 | + ] |
|
1221 | + ), |
|
1222 | + ] |
|
1223 | + ), |
|
1224 | + ] |
|
1225 | + ); |
|
1226 | + } |
|
1227 | + |
|
1228 | + |
|
1229 | + /** |
|
1230 | + * _update_tax_settings |
|
1231 | + * |
|
1232 | + * @return void |
|
1233 | + * @throws EE_Error |
|
1234 | + * @throws ReflectionException |
|
1235 | + * @since 4.9.13 |
|
1236 | + */ |
|
1237 | + public function _update_tax_settings() |
|
1238 | + { |
|
1239 | + $tax_settings = EE_Config::instance()->tax_settings; |
|
1240 | + if (! $tax_settings instanceof EE_Tax_Config) { |
|
1241 | + $tax_settings = new EE_Tax_Config(); |
|
1242 | + } |
|
1243 | + try { |
|
1244 | + $tax_form = $this->tax_settings_form(); |
|
1245 | + // check for form submission |
|
1246 | + if ($tax_form->was_submitted()) { |
|
1247 | + // capture form data |
|
1248 | + $tax_form->receive_form_submission(); |
|
1249 | + // validate form data |
|
1250 | + if ($tax_form->is_valid()) { |
|
1251 | + // grab validated data from form |
|
1252 | + $valid_data = $tax_form->valid_data(); |
|
1253 | + // set data on config |
|
1254 | + $tax_settings->prices_displayed_including_taxes = |
|
1255 | + $valid_data['tax_settings']['prices_displayed_including_taxes']; |
|
1256 | + } else { |
|
1257 | + if ($tax_form->submission_error_message() !== '') { |
|
1258 | + EE_Error::add_error( |
|
1259 | + $tax_form->submission_error_message(), |
|
1260 | + __FILE__, |
|
1261 | + __FUNCTION__, |
|
1262 | + __LINE__ |
|
1263 | + ); |
|
1264 | + } |
|
1265 | + } |
|
1266 | + } |
|
1267 | + } catch (EE_Error $e) { |
|
1268 | + EE_Error::add_error($e->get_error(), __FILE__, __FUNCTION__, __LINE__); |
|
1269 | + } |
|
1270 | + |
|
1271 | + $what = 'Tax Settings'; |
|
1272 | + $success = $this->_update_espresso_configuration( |
|
1273 | + $what, |
|
1274 | + $tax_settings, |
|
1275 | + __FILE__, |
|
1276 | + __FUNCTION__, |
|
1277 | + __LINE__ |
|
1278 | + ); |
|
1279 | + $this->_redirect_after_action($success, $what, 'updated', ['action' => 'tax_settings']); |
|
1280 | + } |
|
1281 | 1281 | } |