@@ -16,1259 +16,1259 @@ |
||
| 16 | 16 | { |
| 17 | 17 | |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * This is used to hold the reports template data which is setup early in the request. |
|
| 21 | - * |
|
| 22 | - * @type array |
|
| 23 | - */ |
|
| 24 | - protected $_reports_template_data = array(); |
|
| 19 | + /** |
|
| 20 | + * This is used to hold the reports template data which is setup early in the request. |
|
| 21 | + * |
|
| 22 | + * @type array |
|
| 23 | + */ |
|
| 24 | + protected $_reports_template_data = array(); |
|
| 25 | 25 | |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Extend_Registrations_Admin_Page constructor. |
|
| 29 | - * |
|
| 30 | - * @param bool $routing |
|
| 31 | - */ |
|
| 32 | - public function __construct($routing = true) |
|
| 33 | - { |
|
| 34 | - parent::__construct($routing); |
|
| 35 | - if (! defined('REG_CAF_TEMPLATE_PATH')) { |
|
| 36 | - define('REG_CAF_TEMPLATE_PATH', EE_CORE_CAF_ADMIN_EXTEND . 'registrations/templates/'); |
|
| 37 | - define('REG_CAF_ASSETS', EE_CORE_CAF_ADMIN_EXTEND . 'registrations/assets/'); |
|
| 38 | - define('REG_CAF_ASSETS_URL', EE_CORE_CAF_ADMIN_EXTEND_URL . 'registrations/assets/'); |
|
| 39 | - } |
|
| 40 | - } |
|
| 27 | + /** |
|
| 28 | + * Extend_Registrations_Admin_Page constructor. |
|
| 29 | + * |
|
| 30 | + * @param bool $routing |
|
| 31 | + */ |
|
| 32 | + public function __construct($routing = true) |
|
| 33 | + { |
|
| 34 | + parent::__construct($routing); |
|
| 35 | + if (! defined('REG_CAF_TEMPLATE_PATH')) { |
|
| 36 | + define('REG_CAF_TEMPLATE_PATH', EE_CORE_CAF_ADMIN_EXTEND . 'registrations/templates/'); |
|
| 37 | + define('REG_CAF_ASSETS', EE_CORE_CAF_ADMIN_EXTEND . 'registrations/assets/'); |
|
| 38 | + define('REG_CAF_ASSETS_URL', EE_CORE_CAF_ADMIN_EXTEND_URL . 'registrations/assets/'); |
|
| 39 | + } |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Extending page configuration. |
|
| 45 | - */ |
|
| 46 | - protected function _extend_page_config() |
|
| 47 | - { |
|
| 48 | - $this->_admin_base_path = EE_CORE_CAF_ADMIN_EXTEND . 'registrations'; |
|
| 49 | - $reg_id = ! empty($this->_req_data['_REG_ID']) && ! is_array($this->_req_data['_REG_ID']) |
|
| 50 | - ? $this->_req_data['_REG_ID'] |
|
| 51 | - : 0; |
|
| 52 | - $new_page_routes = array( |
|
| 53 | - 'reports' => array( |
|
| 54 | - 'func' => '_registration_reports', |
|
| 55 | - 'capability' => 'ee_read_registrations', |
|
| 56 | - ), |
|
| 57 | - 'registration_checkins' => array( |
|
| 58 | - 'func' => '_registration_checkin_list_table', |
|
| 59 | - 'capability' => 'ee_read_checkins', |
|
| 60 | - ), |
|
| 61 | - 'newsletter_selected_send' => array( |
|
| 62 | - 'func' => '_newsletter_selected_send', |
|
| 63 | - 'noheader' => true, |
|
| 64 | - 'capability' => 'ee_send_message', |
|
| 65 | - ), |
|
| 66 | - 'delete_checkin_rows' => array( |
|
| 67 | - 'func' => '_delete_checkin_rows', |
|
| 68 | - 'noheader' => true, |
|
| 69 | - 'capability' => 'ee_delete_checkins', |
|
| 70 | - ), |
|
| 71 | - 'delete_checkin_row' => array( |
|
| 72 | - 'func' => '_delete_checkin_row', |
|
| 73 | - 'noheader' => true, |
|
| 74 | - 'capability' => 'ee_delete_checkin', |
|
| 75 | - 'obj_id' => $reg_id, |
|
| 76 | - ), |
|
| 77 | - 'toggle_checkin_status' => array( |
|
| 78 | - 'func' => '_toggle_checkin_status', |
|
| 79 | - 'noheader' => true, |
|
| 80 | - 'capability' => 'ee_edit_checkin', |
|
| 81 | - 'obj_id' => $reg_id, |
|
| 82 | - ), |
|
| 83 | - 'toggle_checkin_status_bulk' => array( |
|
| 84 | - 'func' => '_toggle_checkin_status', |
|
| 85 | - 'noheader' => true, |
|
| 86 | - 'capability' => 'ee_edit_checkins', |
|
| 87 | - ), |
|
| 88 | - 'event_registrations' => array( |
|
| 89 | - 'func' => '_event_registrations_list_table', |
|
| 90 | - 'capability' => 'ee_read_checkins', |
|
| 91 | - ), |
|
| 92 | - 'registrations_checkin_report' => array( |
|
| 93 | - 'func' => '_registrations_checkin_report', |
|
| 94 | - 'noheader' => true, |
|
| 95 | - 'capability' => 'ee_read_registrations', |
|
| 96 | - ), |
|
| 97 | - ); |
|
| 98 | - $this->_page_routes = array_merge($this->_page_routes, $new_page_routes); |
|
| 99 | - $new_page_config = array( |
|
| 100 | - 'reports' => array( |
|
| 101 | - 'nav' => array( |
|
| 102 | - 'label' => esc_html__('Reports', 'event_espresso'), |
|
| 103 | - 'order' => 30, |
|
| 104 | - ), |
|
| 105 | - 'help_tabs' => array( |
|
| 106 | - 'registrations_reports_help_tab' => array( |
|
| 107 | - 'title' => esc_html__('Registration Reports', 'event_espresso'), |
|
| 108 | - 'filename' => 'registrations_reports', |
|
| 109 | - ), |
|
| 110 | - ), |
|
| 111 | - /*'help_tour' => array( 'Registration_Reports_Help_Tour' ),*/ |
|
| 112 | - 'require_nonce' => false, |
|
| 113 | - ), |
|
| 114 | - 'event_registrations' => array( |
|
| 115 | - 'nav' => array( |
|
| 116 | - 'label' => esc_html__('Event Check-In', 'event_espresso'), |
|
| 117 | - 'order' => 10, |
|
| 118 | - 'persistent' => true, |
|
| 119 | - ), |
|
| 120 | - 'help_tabs' => array( |
|
| 121 | - 'registrations_event_checkin_help_tab' => array( |
|
| 122 | - 'title' => esc_html__('Registrations Event Check-In', 'event_espresso'), |
|
| 123 | - 'filename' => 'registrations_event_checkin', |
|
| 124 | - ), |
|
| 125 | - 'registrations_event_checkin_table_column_headings_help_tab' => array( |
|
| 126 | - 'title' => esc_html__('Event Check-In Table Column Headings', 'event_espresso'), |
|
| 127 | - 'filename' => 'registrations_event_checkin_table_column_headings', |
|
| 128 | - ), |
|
| 129 | - 'registrations_event_checkin_filters_help_tab' => array( |
|
| 130 | - 'title' => esc_html__('Event Check-In Filters', 'event_espresso'), |
|
| 131 | - 'filename' => 'registrations_event_checkin_filters', |
|
| 132 | - ), |
|
| 133 | - 'registrations_event_checkin_views_help_tab' => array( |
|
| 134 | - 'title' => esc_html__('Event Check-In Views', 'event_espresso'), |
|
| 135 | - 'filename' => 'registrations_event_checkin_views', |
|
| 136 | - ), |
|
| 137 | - 'registrations_event_checkin_other_help_tab' => array( |
|
| 138 | - 'title' => esc_html__('Event Check-In Other', 'event_espresso'), |
|
| 139 | - 'filename' => 'registrations_event_checkin_other', |
|
| 140 | - ), |
|
| 141 | - ), |
|
| 142 | - 'help_tour' => array('Event_Checkin_Help_Tour'), |
|
| 143 | - 'qtips' => array('Registration_List_Table_Tips'), |
|
| 144 | - 'list_table' => 'EE_Event_Registrations_List_Table', |
|
| 145 | - 'metaboxes' => array(), |
|
| 146 | - 'require_nonce' => false, |
|
| 147 | - ), |
|
| 148 | - 'registration_checkins' => array( |
|
| 149 | - 'nav' => array( |
|
| 150 | - 'label' => esc_html__('Check-In Records', 'event_espresso'), |
|
| 151 | - 'order' => 15, |
|
| 152 | - 'persistent' => false, |
|
| 153 | - 'url' => '', |
|
| 154 | - ), |
|
| 155 | - 'list_table' => 'EE_Registration_CheckIn_List_Table', |
|
| 156 | - // 'help_tour' => array( 'Checkin_Toggle_View_Help_Tour' ), |
|
| 157 | - 'metaboxes' => array(), |
|
| 158 | - 'require_nonce' => false, |
|
| 159 | - ), |
|
| 160 | - ); |
|
| 161 | - $this->_page_config = array_merge($this->_page_config, $new_page_config); |
|
| 162 | - $this->_page_config['contact_list']['list_table'] = 'Extend_EE_Attendee_Contact_List_Table'; |
|
| 163 | - $this->_page_config['default']['list_table'] = 'Extend_EE_Registrations_List_Table'; |
|
| 164 | - } |
|
| 43 | + /** |
|
| 44 | + * Extending page configuration. |
|
| 45 | + */ |
|
| 46 | + protected function _extend_page_config() |
|
| 47 | + { |
|
| 48 | + $this->_admin_base_path = EE_CORE_CAF_ADMIN_EXTEND . 'registrations'; |
|
| 49 | + $reg_id = ! empty($this->_req_data['_REG_ID']) && ! is_array($this->_req_data['_REG_ID']) |
|
| 50 | + ? $this->_req_data['_REG_ID'] |
|
| 51 | + : 0; |
|
| 52 | + $new_page_routes = array( |
|
| 53 | + 'reports' => array( |
|
| 54 | + 'func' => '_registration_reports', |
|
| 55 | + 'capability' => 'ee_read_registrations', |
|
| 56 | + ), |
|
| 57 | + 'registration_checkins' => array( |
|
| 58 | + 'func' => '_registration_checkin_list_table', |
|
| 59 | + 'capability' => 'ee_read_checkins', |
|
| 60 | + ), |
|
| 61 | + 'newsletter_selected_send' => array( |
|
| 62 | + 'func' => '_newsletter_selected_send', |
|
| 63 | + 'noheader' => true, |
|
| 64 | + 'capability' => 'ee_send_message', |
|
| 65 | + ), |
|
| 66 | + 'delete_checkin_rows' => array( |
|
| 67 | + 'func' => '_delete_checkin_rows', |
|
| 68 | + 'noheader' => true, |
|
| 69 | + 'capability' => 'ee_delete_checkins', |
|
| 70 | + ), |
|
| 71 | + 'delete_checkin_row' => array( |
|
| 72 | + 'func' => '_delete_checkin_row', |
|
| 73 | + 'noheader' => true, |
|
| 74 | + 'capability' => 'ee_delete_checkin', |
|
| 75 | + 'obj_id' => $reg_id, |
|
| 76 | + ), |
|
| 77 | + 'toggle_checkin_status' => array( |
|
| 78 | + 'func' => '_toggle_checkin_status', |
|
| 79 | + 'noheader' => true, |
|
| 80 | + 'capability' => 'ee_edit_checkin', |
|
| 81 | + 'obj_id' => $reg_id, |
|
| 82 | + ), |
|
| 83 | + 'toggle_checkin_status_bulk' => array( |
|
| 84 | + 'func' => '_toggle_checkin_status', |
|
| 85 | + 'noheader' => true, |
|
| 86 | + 'capability' => 'ee_edit_checkins', |
|
| 87 | + ), |
|
| 88 | + 'event_registrations' => array( |
|
| 89 | + 'func' => '_event_registrations_list_table', |
|
| 90 | + 'capability' => 'ee_read_checkins', |
|
| 91 | + ), |
|
| 92 | + 'registrations_checkin_report' => array( |
|
| 93 | + 'func' => '_registrations_checkin_report', |
|
| 94 | + 'noheader' => true, |
|
| 95 | + 'capability' => 'ee_read_registrations', |
|
| 96 | + ), |
|
| 97 | + ); |
|
| 98 | + $this->_page_routes = array_merge($this->_page_routes, $new_page_routes); |
|
| 99 | + $new_page_config = array( |
|
| 100 | + 'reports' => array( |
|
| 101 | + 'nav' => array( |
|
| 102 | + 'label' => esc_html__('Reports', 'event_espresso'), |
|
| 103 | + 'order' => 30, |
|
| 104 | + ), |
|
| 105 | + 'help_tabs' => array( |
|
| 106 | + 'registrations_reports_help_tab' => array( |
|
| 107 | + 'title' => esc_html__('Registration Reports', 'event_espresso'), |
|
| 108 | + 'filename' => 'registrations_reports', |
|
| 109 | + ), |
|
| 110 | + ), |
|
| 111 | + /*'help_tour' => array( 'Registration_Reports_Help_Tour' ),*/ |
|
| 112 | + 'require_nonce' => false, |
|
| 113 | + ), |
|
| 114 | + 'event_registrations' => array( |
|
| 115 | + 'nav' => array( |
|
| 116 | + 'label' => esc_html__('Event Check-In', 'event_espresso'), |
|
| 117 | + 'order' => 10, |
|
| 118 | + 'persistent' => true, |
|
| 119 | + ), |
|
| 120 | + 'help_tabs' => array( |
|
| 121 | + 'registrations_event_checkin_help_tab' => array( |
|
| 122 | + 'title' => esc_html__('Registrations Event Check-In', 'event_espresso'), |
|
| 123 | + 'filename' => 'registrations_event_checkin', |
|
| 124 | + ), |
|
| 125 | + 'registrations_event_checkin_table_column_headings_help_tab' => array( |
|
| 126 | + 'title' => esc_html__('Event Check-In Table Column Headings', 'event_espresso'), |
|
| 127 | + 'filename' => 'registrations_event_checkin_table_column_headings', |
|
| 128 | + ), |
|
| 129 | + 'registrations_event_checkin_filters_help_tab' => array( |
|
| 130 | + 'title' => esc_html__('Event Check-In Filters', 'event_espresso'), |
|
| 131 | + 'filename' => 'registrations_event_checkin_filters', |
|
| 132 | + ), |
|
| 133 | + 'registrations_event_checkin_views_help_tab' => array( |
|
| 134 | + 'title' => esc_html__('Event Check-In Views', 'event_espresso'), |
|
| 135 | + 'filename' => 'registrations_event_checkin_views', |
|
| 136 | + ), |
|
| 137 | + 'registrations_event_checkin_other_help_tab' => array( |
|
| 138 | + 'title' => esc_html__('Event Check-In Other', 'event_espresso'), |
|
| 139 | + 'filename' => 'registrations_event_checkin_other', |
|
| 140 | + ), |
|
| 141 | + ), |
|
| 142 | + 'help_tour' => array('Event_Checkin_Help_Tour'), |
|
| 143 | + 'qtips' => array('Registration_List_Table_Tips'), |
|
| 144 | + 'list_table' => 'EE_Event_Registrations_List_Table', |
|
| 145 | + 'metaboxes' => array(), |
|
| 146 | + 'require_nonce' => false, |
|
| 147 | + ), |
|
| 148 | + 'registration_checkins' => array( |
|
| 149 | + 'nav' => array( |
|
| 150 | + 'label' => esc_html__('Check-In Records', 'event_espresso'), |
|
| 151 | + 'order' => 15, |
|
| 152 | + 'persistent' => false, |
|
| 153 | + 'url' => '', |
|
| 154 | + ), |
|
| 155 | + 'list_table' => 'EE_Registration_CheckIn_List_Table', |
|
| 156 | + // 'help_tour' => array( 'Checkin_Toggle_View_Help_Tour' ), |
|
| 157 | + 'metaboxes' => array(), |
|
| 158 | + 'require_nonce' => false, |
|
| 159 | + ), |
|
| 160 | + ); |
|
| 161 | + $this->_page_config = array_merge($this->_page_config, $new_page_config); |
|
| 162 | + $this->_page_config['contact_list']['list_table'] = 'Extend_EE_Attendee_Contact_List_Table'; |
|
| 163 | + $this->_page_config['default']['list_table'] = 'Extend_EE_Registrations_List_Table'; |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Ajax hooks for all routes in this page. |
|
| 169 | - */ |
|
| 170 | - protected function _ajax_hooks() |
|
| 171 | - { |
|
| 172 | - parent::_ajax_hooks(); |
|
| 173 | - add_action('wp_ajax_get_newsletter_form_content', array($this, 'get_newsletter_form_content')); |
|
| 174 | - } |
|
| 167 | + /** |
|
| 168 | + * Ajax hooks for all routes in this page. |
|
| 169 | + */ |
|
| 170 | + protected function _ajax_hooks() |
|
| 171 | + { |
|
| 172 | + parent::_ajax_hooks(); |
|
| 173 | + add_action('wp_ajax_get_newsletter_form_content', array($this, 'get_newsletter_form_content')); |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * Global scripts for all routes in this page. |
|
| 179 | - */ |
|
| 180 | - public function load_scripts_styles() |
|
| 181 | - { |
|
| 182 | - parent::load_scripts_styles(); |
|
| 183 | - // if newsletter message type is active then let's add filter and load js for it. |
|
| 184 | - if (EEH_MSG_Template::is_mt_active('newsletter')) { |
|
| 185 | - // enqueue newsletter js |
|
| 186 | - wp_enqueue_script( |
|
| 187 | - 'ee-newsletter-trigger', |
|
| 188 | - REG_CAF_ASSETS_URL . 'ee-newsletter-trigger.js', |
|
| 189 | - array('ee-dialog'), |
|
| 190 | - EVENT_ESPRESSO_VERSION, |
|
| 191 | - true |
|
| 192 | - ); |
|
| 193 | - wp_enqueue_style( |
|
| 194 | - 'ee-newsletter-trigger-css', |
|
| 195 | - REG_CAF_ASSETS_URL . 'ee-newsletter-trigger.css', |
|
| 196 | - array(), |
|
| 197 | - EVENT_ESPRESSO_VERSION |
|
| 198 | - ); |
|
| 199 | - // hook in buttons for newsletter message type trigger. |
|
| 200 | - add_action( |
|
| 201 | - 'AHEE__EE_Admin_List_Table__extra_tablenav__after_bottom_buttons', |
|
| 202 | - array($this, 'add_newsletter_action_buttons'), |
|
| 203 | - 10 |
|
| 204 | - ); |
|
| 205 | - } |
|
| 206 | - } |
|
| 177 | + /** |
|
| 178 | + * Global scripts for all routes in this page. |
|
| 179 | + */ |
|
| 180 | + public function load_scripts_styles() |
|
| 181 | + { |
|
| 182 | + parent::load_scripts_styles(); |
|
| 183 | + // if newsletter message type is active then let's add filter and load js for it. |
|
| 184 | + if (EEH_MSG_Template::is_mt_active('newsletter')) { |
|
| 185 | + // enqueue newsletter js |
|
| 186 | + wp_enqueue_script( |
|
| 187 | + 'ee-newsletter-trigger', |
|
| 188 | + REG_CAF_ASSETS_URL . 'ee-newsletter-trigger.js', |
|
| 189 | + array('ee-dialog'), |
|
| 190 | + EVENT_ESPRESSO_VERSION, |
|
| 191 | + true |
|
| 192 | + ); |
|
| 193 | + wp_enqueue_style( |
|
| 194 | + 'ee-newsletter-trigger-css', |
|
| 195 | + REG_CAF_ASSETS_URL . 'ee-newsletter-trigger.css', |
|
| 196 | + array(), |
|
| 197 | + EVENT_ESPRESSO_VERSION |
|
| 198 | + ); |
|
| 199 | + // hook in buttons for newsletter message type trigger. |
|
| 200 | + add_action( |
|
| 201 | + 'AHEE__EE_Admin_List_Table__extra_tablenav__after_bottom_buttons', |
|
| 202 | + array($this, 'add_newsletter_action_buttons'), |
|
| 203 | + 10 |
|
| 204 | + ); |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | 208 | |
| 209 | - /** |
|
| 210 | - * Scripts and styles for just the reports route. |
|
| 211 | - */ |
|
| 212 | - public function load_scripts_styles_reports() |
|
| 213 | - { |
|
| 214 | - wp_register_script( |
|
| 215 | - 'ee-reg-reports-js', |
|
| 216 | - REG_CAF_ASSETS_URL . 'ee-registration-admin-reports.js', |
|
| 217 | - array('google-charts'), |
|
| 218 | - EVENT_ESPRESSO_VERSION, |
|
| 219 | - true |
|
| 220 | - ); |
|
| 221 | - wp_enqueue_script('ee-reg-reports-js'); |
|
| 222 | - $this->_registration_reports_js_setup(); |
|
| 223 | - } |
|
| 209 | + /** |
|
| 210 | + * Scripts and styles for just the reports route. |
|
| 211 | + */ |
|
| 212 | + public function load_scripts_styles_reports() |
|
| 213 | + { |
|
| 214 | + wp_register_script( |
|
| 215 | + 'ee-reg-reports-js', |
|
| 216 | + REG_CAF_ASSETS_URL . 'ee-registration-admin-reports.js', |
|
| 217 | + array('google-charts'), |
|
| 218 | + EVENT_ESPRESSO_VERSION, |
|
| 219 | + true |
|
| 220 | + ); |
|
| 221 | + wp_enqueue_script('ee-reg-reports-js'); |
|
| 222 | + $this->_registration_reports_js_setup(); |
|
| 223 | + } |
|
| 224 | 224 | |
| 225 | 225 | |
| 226 | - /** |
|
| 227 | - * Register screen options for event_registrations route. |
|
| 228 | - */ |
|
| 229 | - protected function _add_screen_options_event_registrations() |
|
| 230 | - { |
|
| 231 | - $this->_per_page_screen_option(); |
|
| 232 | - } |
|
| 226 | + /** |
|
| 227 | + * Register screen options for event_registrations route. |
|
| 228 | + */ |
|
| 229 | + protected function _add_screen_options_event_registrations() |
|
| 230 | + { |
|
| 231 | + $this->_per_page_screen_option(); |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | 234 | |
| 235 | - /** |
|
| 236 | - * Register screen options for registration_checkins route |
|
| 237 | - */ |
|
| 238 | - protected function _add_screen_options_registration_checkins() |
|
| 239 | - { |
|
| 240 | - $page_title = $this->_admin_page_title; |
|
| 241 | - $this->_admin_page_title = esc_html__('Check-In Records', 'event_espresso'); |
|
| 242 | - $this->_per_page_screen_option(); |
|
| 243 | - $this->_admin_page_title = $page_title; |
|
| 244 | - } |
|
| 235 | + /** |
|
| 236 | + * Register screen options for registration_checkins route |
|
| 237 | + */ |
|
| 238 | + protected function _add_screen_options_registration_checkins() |
|
| 239 | + { |
|
| 240 | + $page_title = $this->_admin_page_title; |
|
| 241 | + $this->_admin_page_title = esc_html__('Check-In Records', 'event_espresso'); |
|
| 242 | + $this->_per_page_screen_option(); |
|
| 243 | + $this->_admin_page_title = $page_title; |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | 246 | |
| 247 | - /** |
|
| 248 | - * Set views property for event_registrations route. |
|
| 249 | - */ |
|
| 250 | - protected function _set_list_table_views_event_registrations() |
|
| 251 | - { |
|
| 252 | - $this->_views = array( |
|
| 253 | - 'all' => array( |
|
| 254 | - 'slug' => 'all', |
|
| 255 | - 'label' => esc_html__('All', 'event_espresso'), |
|
| 256 | - 'count' => 0, |
|
| 257 | - 'bulk_action' => ! isset($this->_req_data['event_id']) |
|
| 258 | - ? array() |
|
| 259 | - : array( |
|
| 260 | - 'toggle_checkin_status_bulk' => esc_html__('Toggle Check-In', 'event_espresso'), |
|
| 261 | - ), |
|
| 262 | - ), |
|
| 263 | - ); |
|
| 264 | - } |
|
| 247 | + /** |
|
| 248 | + * Set views property for event_registrations route. |
|
| 249 | + */ |
|
| 250 | + protected function _set_list_table_views_event_registrations() |
|
| 251 | + { |
|
| 252 | + $this->_views = array( |
|
| 253 | + 'all' => array( |
|
| 254 | + 'slug' => 'all', |
|
| 255 | + 'label' => esc_html__('All', 'event_espresso'), |
|
| 256 | + 'count' => 0, |
|
| 257 | + 'bulk_action' => ! isset($this->_req_data['event_id']) |
|
| 258 | + ? array() |
|
| 259 | + : array( |
|
| 260 | + 'toggle_checkin_status_bulk' => esc_html__('Toggle Check-In', 'event_espresso'), |
|
| 261 | + ), |
|
| 262 | + ), |
|
| 263 | + ); |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | 266 | |
| 267 | - /** |
|
| 268 | - * Set views property for registration_checkins route. |
|
| 269 | - */ |
|
| 270 | - protected function _set_list_table_views_registration_checkins() |
|
| 271 | - { |
|
| 272 | - $this->_views = array( |
|
| 273 | - 'all' => array( |
|
| 274 | - 'slug' => 'all', |
|
| 275 | - 'label' => esc_html__('All', 'event_espresso'), |
|
| 276 | - 'count' => 0, |
|
| 277 | - 'bulk_action' => array('delete_checkin_rows' => esc_html__('Delete Check-In Rows', 'event_espresso')), |
|
| 278 | - ), |
|
| 279 | - ); |
|
| 280 | - } |
|
| 267 | + /** |
|
| 268 | + * Set views property for registration_checkins route. |
|
| 269 | + */ |
|
| 270 | + protected function _set_list_table_views_registration_checkins() |
|
| 271 | + { |
|
| 272 | + $this->_views = array( |
|
| 273 | + 'all' => array( |
|
| 274 | + 'slug' => 'all', |
|
| 275 | + 'label' => esc_html__('All', 'event_espresso'), |
|
| 276 | + 'count' => 0, |
|
| 277 | + 'bulk_action' => array('delete_checkin_rows' => esc_html__('Delete Check-In Rows', 'event_espresso')), |
|
| 278 | + ), |
|
| 279 | + ); |
|
| 280 | + } |
|
| 281 | 281 | |
| 282 | 282 | |
| 283 | - /** |
|
| 284 | - * callback for ajax action. |
|
| 285 | - * |
|
| 286 | - * @since 4.3.0 |
|
| 287 | - * @return void (JSON) |
|
| 288 | - * @throws EE_Error |
|
| 289 | - * @throws InvalidArgumentException |
|
| 290 | - * @throws InvalidDataTypeException |
|
| 291 | - * @throws InvalidInterfaceException |
|
| 292 | - */ |
|
| 293 | - public function get_newsletter_form_content() |
|
| 294 | - { |
|
| 295 | - // do a nonce check cause we're not coming in from an normal route here. |
|
| 296 | - $nonce = isset($this->_req_data['get_newsletter_form_content_nonce']) ? sanitize_text_field( |
|
| 297 | - $this->_req_data['get_newsletter_form_content_nonce'] |
|
| 298 | - ) : ''; |
|
| 299 | - $nonce_ref = 'get_newsletter_form_content_nonce'; |
|
| 300 | - $this->_verify_nonce($nonce, $nonce_ref); |
|
| 301 | - // let's get the mtp for the incoming MTP_ ID |
|
| 302 | - if (! isset($this->_req_data['GRP_ID'])) { |
|
| 303 | - EE_Error::add_error( |
|
| 304 | - esc_html__( |
|
| 305 | - 'There must be something broken with the js or html structure because the required data for getting a message template group is not present (need an GRP_ID).', |
|
| 306 | - 'event_espresso' |
|
| 307 | - ), |
|
| 308 | - __FILE__, |
|
| 309 | - __FUNCTION__, |
|
| 310 | - __LINE__ |
|
| 311 | - ); |
|
| 312 | - $this->_template_args['success'] = false; |
|
| 313 | - $this->_template_args['error'] = true; |
|
| 314 | - $this->_return_json(); |
|
| 315 | - } |
|
| 316 | - $MTPG = EEM_Message_Template_Group::instance()->get_one_by_ID($this->_req_data['GRP_ID']); |
|
| 317 | - if (! $MTPG instanceof EE_Message_Template_Group) { |
|
| 318 | - EE_Error::add_error( |
|
| 319 | - sprintf( |
|
| 320 | - esc_html__( |
|
| 321 | - 'The GRP_ID given (%d) does not appear to have a corresponding row in the database.', |
|
| 322 | - 'event_espresso' |
|
| 323 | - ), |
|
| 324 | - $this->_req_data['GRP_ID'] |
|
| 325 | - ), |
|
| 326 | - __FILE__, |
|
| 327 | - __FUNCTION__, |
|
| 328 | - __LINE__ |
|
| 329 | - ); |
|
| 330 | - $this->_template_args['success'] = false; |
|
| 331 | - $this->_template_args['error'] = true; |
|
| 332 | - $this->_return_json(); |
|
| 333 | - } |
|
| 334 | - $MTPs = $MTPG->context_templates(); |
|
| 335 | - $MTPs = $MTPs['attendee']; |
|
| 336 | - $template_fields = array(); |
|
| 337 | - /** @var EE_Message_Template $MTP */ |
|
| 338 | - foreach ($MTPs as $MTP) { |
|
| 339 | - $field = $MTP->get('MTP_template_field'); |
|
| 340 | - if ($field === 'content') { |
|
| 341 | - $content = $MTP->get('MTP_content'); |
|
| 342 | - if (! empty($content['newsletter_content'])) { |
|
| 343 | - $template_fields['newsletter_content'] = $content['newsletter_content']; |
|
| 344 | - } |
|
| 345 | - continue; |
|
| 346 | - } |
|
| 347 | - $template_fields[ $MTP->get('MTP_template_field') ] = $MTP->get('MTP_content'); |
|
| 348 | - } |
|
| 349 | - $this->_template_args['success'] = true; |
|
| 350 | - $this->_template_args['error'] = false; |
|
| 351 | - $this->_template_args['data'] = array( |
|
| 352 | - 'batch_message_from' => isset($template_fields['from']) |
|
| 353 | - ? $template_fields['from'] |
|
| 354 | - : '', |
|
| 355 | - 'batch_message_subject' => isset($template_fields['subject']) |
|
| 356 | - ? $template_fields['subject'] |
|
| 357 | - : '', |
|
| 358 | - 'batch_message_content' => isset($template_fields['newsletter_content']) |
|
| 359 | - ? $template_fields['newsletter_content'] |
|
| 360 | - : '', |
|
| 361 | - ); |
|
| 362 | - $this->_return_json(); |
|
| 363 | - } |
|
| 283 | + /** |
|
| 284 | + * callback for ajax action. |
|
| 285 | + * |
|
| 286 | + * @since 4.3.0 |
|
| 287 | + * @return void (JSON) |
|
| 288 | + * @throws EE_Error |
|
| 289 | + * @throws InvalidArgumentException |
|
| 290 | + * @throws InvalidDataTypeException |
|
| 291 | + * @throws InvalidInterfaceException |
|
| 292 | + */ |
|
| 293 | + public function get_newsletter_form_content() |
|
| 294 | + { |
|
| 295 | + // do a nonce check cause we're not coming in from an normal route here. |
|
| 296 | + $nonce = isset($this->_req_data['get_newsletter_form_content_nonce']) ? sanitize_text_field( |
|
| 297 | + $this->_req_data['get_newsletter_form_content_nonce'] |
|
| 298 | + ) : ''; |
|
| 299 | + $nonce_ref = 'get_newsletter_form_content_nonce'; |
|
| 300 | + $this->_verify_nonce($nonce, $nonce_ref); |
|
| 301 | + // let's get the mtp for the incoming MTP_ ID |
|
| 302 | + if (! isset($this->_req_data['GRP_ID'])) { |
|
| 303 | + EE_Error::add_error( |
|
| 304 | + esc_html__( |
|
| 305 | + 'There must be something broken with the js or html structure because the required data for getting a message template group is not present (need an GRP_ID).', |
|
| 306 | + 'event_espresso' |
|
| 307 | + ), |
|
| 308 | + __FILE__, |
|
| 309 | + __FUNCTION__, |
|
| 310 | + __LINE__ |
|
| 311 | + ); |
|
| 312 | + $this->_template_args['success'] = false; |
|
| 313 | + $this->_template_args['error'] = true; |
|
| 314 | + $this->_return_json(); |
|
| 315 | + } |
|
| 316 | + $MTPG = EEM_Message_Template_Group::instance()->get_one_by_ID($this->_req_data['GRP_ID']); |
|
| 317 | + if (! $MTPG instanceof EE_Message_Template_Group) { |
|
| 318 | + EE_Error::add_error( |
|
| 319 | + sprintf( |
|
| 320 | + esc_html__( |
|
| 321 | + 'The GRP_ID given (%d) does not appear to have a corresponding row in the database.', |
|
| 322 | + 'event_espresso' |
|
| 323 | + ), |
|
| 324 | + $this->_req_data['GRP_ID'] |
|
| 325 | + ), |
|
| 326 | + __FILE__, |
|
| 327 | + __FUNCTION__, |
|
| 328 | + __LINE__ |
|
| 329 | + ); |
|
| 330 | + $this->_template_args['success'] = false; |
|
| 331 | + $this->_template_args['error'] = true; |
|
| 332 | + $this->_return_json(); |
|
| 333 | + } |
|
| 334 | + $MTPs = $MTPG->context_templates(); |
|
| 335 | + $MTPs = $MTPs['attendee']; |
|
| 336 | + $template_fields = array(); |
|
| 337 | + /** @var EE_Message_Template $MTP */ |
|
| 338 | + foreach ($MTPs as $MTP) { |
|
| 339 | + $field = $MTP->get('MTP_template_field'); |
|
| 340 | + if ($field === 'content') { |
|
| 341 | + $content = $MTP->get('MTP_content'); |
|
| 342 | + if (! empty($content['newsletter_content'])) { |
|
| 343 | + $template_fields['newsletter_content'] = $content['newsletter_content']; |
|
| 344 | + } |
|
| 345 | + continue; |
|
| 346 | + } |
|
| 347 | + $template_fields[ $MTP->get('MTP_template_field') ] = $MTP->get('MTP_content'); |
|
| 348 | + } |
|
| 349 | + $this->_template_args['success'] = true; |
|
| 350 | + $this->_template_args['error'] = false; |
|
| 351 | + $this->_template_args['data'] = array( |
|
| 352 | + 'batch_message_from' => isset($template_fields['from']) |
|
| 353 | + ? $template_fields['from'] |
|
| 354 | + : '', |
|
| 355 | + 'batch_message_subject' => isset($template_fields['subject']) |
|
| 356 | + ? $template_fields['subject'] |
|
| 357 | + : '', |
|
| 358 | + 'batch_message_content' => isset($template_fields['newsletter_content']) |
|
| 359 | + ? $template_fields['newsletter_content'] |
|
| 360 | + : '', |
|
| 361 | + ); |
|
| 362 | + $this->_return_json(); |
|
| 363 | + } |
|
| 364 | 364 | |
| 365 | 365 | |
| 366 | - /** |
|
| 367 | - * callback for AHEE__EE_Admin_List_Table__extra_tablenav__after_bottom_buttons action |
|
| 368 | - * |
|
| 369 | - * @since 4.3.0 |
|
| 370 | - * @param EE_Admin_List_Table $list_table |
|
| 371 | - * @return void |
|
| 372 | - * @throws InvalidArgumentException |
|
| 373 | - * @throws InvalidDataTypeException |
|
| 374 | - * @throws InvalidInterfaceException |
|
| 375 | - */ |
|
| 376 | - public function add_newsletter_action_buttons(EE_Admin_List_Table $list_table) |
|
| 377 | - { |
|
| 378 | - if (! EE_Registry::instance()->CAP->current_user_can( |
|
| 379 | - 'ee_send_message', |
|
| 380 | - 'espresso_registrations_newsletter_selected_send' |
|
| 381 | - ) |
|
| 382 | - ) { |
|
| 383 | - return; |
|
| 384 | - } |
|
| 385 | - $routes_to_add_to = array( |
|
| 386 | - 'contact_list', |
|
| 387 | - 'event_registrations', |
|
| 388 | - 'default', |
|
| 389 | - ); |
|
| 390 | - if ($this->_current_page === 'espresso_registrations' && in_array($this->_req_action, $routes_to_add_to)) { |
|
| 391 | - if (($this->_req_action === 'event_registrations' && empty($this->_req_data['event_id'])) |
|
| 392 | - || (isset($this->_req_data['status']) && $this->_req_data['status'] === 'trash') |
|
| 393 | - ) { |
|
| 394 | - echo ''; |
|
| 395 | - } else { |
|
| 396 | - $button_text = sprintf( |
|
| 397 | - esc_html__('Send Batch Message (%s selected)', 'event_espresso'), |
|
| 398 | - '<span class="send-selected-newsletter-count">0</span>' |
|
| 399 | - ); |
|
| 400 | - echo '<button id="selected-batch-send-trigger" class="button secondary-button">' |
|
| 401 | - . '<span class="dashicons dashicons-email "></span>' |
|
| 402 | - . $button_text |
|
| 403 | - . '</button>'; |
|
| 404 | - add_action('admin_footer', array($this, 'newsletter_send_form_skeleton')); |
|
| 405 | - } |
|
| 406 | - } |
|
| 407 | - } |
|
| 366 | + /** |
|
| 367 | + * callback for AHEE__EE_Admin_List_Table__extra_tablenav__after_bottom_buttons action |
|
| 368 | + * |
|
| 369 | + * @since 4.3.0 |
|
| 370 | + * @param EE_Admin_List_Table $list_table |
|
| 371 | + * @return void |
|
| 372 | + * @throws InvalidArgumentException |
|
| 373 | + * @throws InvalidDataTypeException |
|
| 374 | + * @throws InvalidInterfaceException |
|
| 375 | + */ |
|
| 376 | + public function add_newsletter_action_buttons(EE_Admin_List_Table $list_table) |
|
| 377 | + { |
|
| 378 | + if (! EE_Registry::instance()->CAP->current_user_can( |
|
| 379 | + 'ee_send_message', |
|
| 380 | + 'espresso_registrations_newsletter_selected_send' |
|
| 381 | + ) |
|
| 382 | + ) { |
|
| 383 | + return; |
|
| 384 | + } |
|
| 385 | + $routes_to_add_to = array( |
|
| 386 | + 'contact_list', |
|
| 387 | + 'event_registrations', |
|
| 388 | + 'default', |
|
| 389 | + ); |
|
| 390 | + if ($this->_current_page === 'espresso_registrations' && in_array($this->_req_action, $routes_to_add_to)) { |
|
| 391 | + if (($this->_req_action === 'event_registrations' && empty($this->_req_data['event_id'])) |
|
| 392 | + || (isset($this->_req_data['status']) && $this->_req_data['status'] === 'trash') |
|
| 393 | + ) { |
|
| 394 | + echo ''; |
|
| 395 | + } else { |
|
| 396 | + $button_text = sprintf( |
|
| 397 | + esc_html__('Send Batch Message (%s selected)', 'event_espresso'), |
|
| 398 | + '<span class="send-selected-newsletter-count">0</span>' |
|
| 399 | + ); |
|
| 400 | + echo '<button id="selected-batch-send-trigger" class="button secondary-button">' |
|
| 401 | + . '<span class="dashicons dashicons-email "></span>' |
|
| 402 | + . $button_text |
|
| 403 | + . '</button>'; |
|
| 404 | + add_action('admin_footer', array($this, 'newsletter_send_form_skeleton')); |
|
| 405 | + } |
|
| 406 | + } |
|
| 407 | + } |
|
| 408 | 408 | |
| 409 | 409 | |
| 410 | - /** |
|
| 411 | - * @throws DomainException |
|
| 412 | - * @throws EE_Error |
|
| 413 | - * @throws InvalidArgumentException |
|
| 414 | - * @throws InvalidDataTypeException |
|
| 415 | - * @throws InvalidInterfaceException |
|
| 416 | - */ |
|
| 417 | - public function newsletter_send_form_skeleton() |
|
| 418 | - { |
|
| 419 | - $list_table = $this->_list_table_object; |
|
| 420 | - $codes = array(); |
|
| 421 | - // need to templates for the newsletter message type for the template selector. |
|
| 422 | - $values[] = array('text' => esc_html__('Select Template to Use', 'event_espresso'), 'id' => 0); |
|
| 423 | - $mtps = EEM_Message_Template_Group::instance()->get_all( |
|
| 424 | - array(array('MTP_message_type' => 'newsletter', 'MTP_messenger' => 'email')) |
|
| 425 | - ); |
|
| 426 | - foreach ($mtps as $mtp) { |
|
| 427 | - $name = $mtp->name(); |
|
| 428 | - $values[] = array( |
|
| 429 | - 'text' => empty($name) ? esc_html__('Global', 'event_espresso') : $name, |
|
| 430 | - 'id' => $mtp->ID(), |
|
| 431 | - ); |
|
| 432 | - } |
|
| 433 | - // need to get a list of shortcodes that are available for the newsletter message type. |
|
| 434 | - $shortcodes = EEH_MSG_Template::get_shortcodes( |
|
| 435 | - 'newsletter', |
|
| 436 | - 'email', |
|
| 437 | - array(), |
|
| 438 | - 'attendee', |
|
| 439 | - false |
|
| 440 | - ); |
|
| 441 | - foreach ($shortcodes as $field => $shortcode_array) { |
|
| 442 | - $available_shortcodes = array(); |
|
| 443 | - foreach ($shortcode_array as $shortcode => $shortcode_details) { |
|
| 444 | - $field_id = $field === '[NEWSLETTER_CONTENT]' |
|
| 445 | - ? 'content' |
|
| 446 | - : $field; |
|
| 447 | - $field_id = 'batch-message-' . strtolower($field_id); |
|
| 448 | - $available_shortcodes[] = '<span class="js-shortcode-selection" data-value="' |
|
| 449 | - . $shortcode |
|
| 450 | - . '" data-linked-input-id="' . $field_id . '">' |
|
| 451 | - . $shortcode |
|
| 452 | - . '</span>'; |
|
| 453 | - } |
|
| 454 | - $codes[ $field ] = implode(', ', $available_shortcodes); |
|
| 455 | - } |
|
| 456 | - $shortcodes = $codes; |
|
| 457 | - $form_template = REG_CAF_TEMPLATE_PATH . 'newsletter-send-form.template.php'; |
|
| 458 | - $form_template_args = array( |
|
| 459 | - 'form_action' => admin_url('admin.php?page=espresso_registrations'), |
|
| 460 | - 'form_route' => 'newsletter_selected_send', |
|
| 461 | - 'form_nonce_name' => 'newsletter_selected_send_nonce', |
|
| 462 | - 'form_nonce' => wp_create_nonce('newsletter_selected_send_nonce'), |
|
| 463 | - 'redirect_back_to' => $this->_req_action, |
|
| 464 | - 'ajax_nonce' => wp_create_nonce('get_newsletter_form_content_nonce'), |
|
| 465 | - 'template_selector' => EEH_Form_Fields::select_input('newsletter_mtp_selected', $values), |
|
| 466 | - 'shortcodes' => $shortcodes, |
|
| 467 | - 'id_type' => $list_table instanceof EE_Attendee_Contact_List_Table ? 'contact' : 'registration', |
|
| 468 | - ); |
|
| 469 | - EEH_Template::display_template($form_template, $form_template_args); |
|
| 470 | - } |
|
| 410 | + /** |
|
| 411 | + * @throws DomainException |
|
| 412 | + * @throws EE_Error |
|
| 413 | + * @throws InvalidArgumentException |
|
| 414 | + * @throws InvalidDataTypeException |
|
| 415 | + * @throws InvalidInterfaceException |
|
| 416 | + */ |
|
| 417 | + public function newsletter_send_form_skeleton() |
|
| 418 | + { |
|
| 419 | + $list_table = $this->_list_table_object; |
|
| 420 | + $codes = array(); |
|
| 421 | + // need to templates for the newsletter message type for the template selector. |
|
| 422 | + $values[] = array('text' => esc_html__('Select Template to Use', 'event_espresso'), 'id' => 0); |
|
| 423 | + $mtps = EEM_Message_Template_Group::instance()->get_all( |
|
| 424 | + array(array('MTP_message_type' => 'newsletter', 'MTP_messenger' => 'email')) |
|
| 425 | + ); |
|
| 426 | + foreach ($mtps as $mtp) { |
|
| 427 | + $name = $mtp->name(); |
|
| 428 | + $values[] = array( |
|
| 429 | + 'text' => empty($name) ? esc_html__('Global', 'event_espresso') : $name, |
|
| 430 | + 'id' => $mtp->ID(), |
|
| 431 | + ); |
|
| 432 | + } |
|
| 433 | + // need to get a list of shortcodes that are available for the newsletter message type. |
|
| 434 | + $shortcodes = EEH_MSG_Template::get_shortcodes( |
|
| 435 | + 'newsletter', |
|
| 436 | + 'email', |
|
| 437 | + array(), |
|
| 438 | + 'attendee', |
|
| 439 | + false |
|
| 440 | + ); |
|
| 441 | + foreach ($shortcodes as $field => $shortcode_array) { |
|
| 442 | + $available_shortcodes = array(); |
|
| 443 | + foreach ($shortcode_array as $shortcode => $shortcode_details) { |
|
| 444 | + $field_id = $field === '[NEWSLETTER_CONTENT]' |
|
| 445 | + ? 'content' |
|
| 446 | + : $field; |
|
| 447 | + $field_id = 'batch-message-' . strtolower($field_id); |
|
| 448 | + $available_shortcodes[] = '<span class="js-shortcode-selection" data-value="' |
|
| 449 | + . $shortcode |
|
| 450 | + . '" data-linked-input-id="' . $field_id . '">' |
|
| 451 | + . $shortcode |
|
| 452 | + . '</span>'; |
|
| 453 | + } |
|
| 454 | + $codes[ $field ] = implode(', ', $available_shortcodes); |
|
| 455 | + } |
|
| 456 | + $shortcodes = $codes; |
|
| 457 | + $form_template = REG_CAF_TEMPLATE_PATH . 'newsletter-send-form.template.php'; |
|
| 458 | + $form_template_args = array( |
|
| 459 | + 'form_action' => admin_url('admin.php?page=espresso_registrations'), |
|
| 460 | + 'form_route' => 'newsletter_selected_send', |
|
| 461 | + 'form_nonce_name' => 'newsletter_selected_send_nonce', |
|
| 462 | + 'form_nonce' => wp_create_nonce('newsletter_selected_send_nonce'), |
|
| 463 | + 'redirect_back_to' => $this->_req_action, |
|
| 464 | + 'ajax_nonce' => wp_create_nonce('get_newsletter_form_content_nonce'), |
|
| 465 | + 'template_selector' => EEH_Form_Fields::select_input('newsletter_mtp_selected', $values), |
|
| 466 | + 'shortcodes' => $shortcodes, |
|
| 467 | + 'id_type' => $list_table instanceof EE_Attendee_Contact_List_Table ? 'contact' : 'registration', |
|
| 468 | + ); |
|
| 469 | + EEH_Template::display_template($form_template, $form_template_args); |
|
| 470 | + } |
|
| 471 | 471 | |
| 472 | 472 | |
| 473 | - /** |
|
| 474 | - * Handles sending selected registrations/contacts a newsletter. |
|
| 475 | - * |
|
| 476 | - * @since 4.3.0 |
|
| 477 | - * @return void |
|
| 478 | - * @throws EE_Error |
|
| 479 | - * @throws InvalidArgumentException |
|
| 480 | - * @throws InvalidDataTypeException |
|
| 481 | - * @throws InvalidInterfaceException |
|
| 482 | - */ |
|
| 483 | - protected function _newsletter_selected_send() |
|
| 484 | - { |
|
| 485 | - $success = true; |
|
| 486 | - // first we need to make sure we have a GRP_ID so we know what template we're sending and updating! |
|
| 487 | - if (empty($this->_req_data['newsletter_mtp_selected'])) { |
|
| 488 | - EE_Error::add_error( |
|
| 489 | - esc_html__( |
|
| 490 | - 'In order to send a message, a Message Template GRP_ID is needed. It was not provided so messages were not sent.', |
|
| 491 | - 'event_espresso' |
|
| 492 | - ), |
|
| 493 | - __FILE__, |
|
| 494 | - __FUNCTION__, |
|
| 495 | - __LINE__ |
|
| 496 | - ); |
|
| 497 | - $success = false; |
|
| 498 | - } |
|
| 499 | - if ($success) { |
|
| 500 | - // update Message template in case there are any changes |
|
| 501 | - $Message_Template_Group = EEM_Message_Template_Group::instance()->get_one_by_ID( |
|
| 502 | - $this->_req_data['newsletter_mtp_selected'] |
|
| 503 | - ); |
|
| 504 | - $Message_Templates = $Message_Template_Group instanceof EE_Message_Template_Group |
|
| 505 | - ? $Message_Template_Group->context_templates() |
|
| 506 | - : array(); |
|
| 507 | - if (empty($Message_Templates)) { |
|
| 508 | - EE_Error::add_error( |
|
| 509 | - esc_html__( |
|
| 510 | - 'Unable to retrieve message template fields from the db. Messages not sent.', |
|
| 511 | - 'event_espresso' |
|
| 512 | - ), |
|
| 513 | - __FILE__, |
|
| 514 | - __FUNCTION__, |
|
| 515 | - __LINE__ |
|
| 516 | - ); |
|
| 517 | - } |
|
| 518 | - // let's just update the specific fields |
|
| 519 | - foreach ($Message_Templates['attendee'] as $Message_Template) { |
|
| 520 | - if ($Message_Template instanceof EE_Message_Template) { |
|
| 521 | - $field = $Message_Template->get('MTP_template_field'); |
|
| 522 | - $content = $Message_Template->get('MTP_content'); |
|
| 523 | - $new_content = $content; |
|
| 524 | - switch ($field) { |
|
| 525 | - case 'from': |
|
| 526 | - $new_content = ! empty($this->_req_data['batch_message']['from']) |
|
| 527 | - ? $this->_req_data['batch_message']['from'] |
|
| 528 | - : $content; |
|
| 529 | - break; |
|
| 530 | - case 'subject': |
|
| 531 | - $new_content = ! empty($this->_req_data['batch_message']['subject']) |
|
| 532 | - ? $this->_req_data['batch_message']['subject'] |
|
| 533 | - : $content; |
|
| 534 | - break; |
|
| 535 | - case 'content': |
|
| 536 | - $new_content = $content; |
|
| 537 | - $new_content['newsletter_content'] = ! empty($this->_req_data['batch_message']['content']) |
|
| 538 | - ? $this->_req_data['batch_message']['content'] |
|
| 539 | - : $content['newsletter_content']; |
|
| 540 | - break; |
|
| 541 | - default: |
|
| 542 | - // continue the foreach loop, we don't want to set $new_content nor save. |
|
| 543 | - continue 2; |
|
| 544 | - } |
|
| 545 | - $Message_Template->set('MTP_content', $new_content); |
|
| 546 | - $Message_Template->save(); |
|
| 547 | - } |
|
| 548 | - } |
|
| 549 | - // great fields are updated! now let's make sure we just have contact objects (EE_Attendee). |
|
| 550 | - $id_type = ! empty($this->_req_data['batch_message']['id_type']) |
|
| 551 | - ? $this->_req_data['batch_message']['id_type'] |
|
| 552 | - : 'registration'; |
|
| 553 | - // id_type will affect how we assemble the ids. |
|
| 554 | - $ids = ! empty($this->_req_data['batch_message']['ids']) |
|
| 555 | - ? json_decode(stripslashes($this->_req_data['batch_message']['ids'])) |
|
| 556 | - : array(); |
|
| 557 | - $registrations_used_for_contact_data = array(); |
|
| 558 | - // using switch because eventually we'll have other contexts that will be used for generating messages. |
|
| 559 | - switch ($id_type) { |
|
| 560 | - case 'registration': |
|
| 561 | - $registrations_used_for_contact_data = EEM_Registration::instance()->get_all( |
|
| 562 | - array( |
|
| 563 | - array( |
|
| 564 | - 'REG_ID' => array('IN', $ids), |
|
| 565 | - ), |
|
| 566 | - ) |
|
| 567 | - ); |
|
| 568 | - break; |
|
| 569 | - case 'contact': |
|
| 570 | - $registrations_used_for_contact_data = EEM_Registration::instance() |
|
| 571 | - ->get_latest_registration_for_each_of_given_contacts( |
|
| 572 | - $ids |
|
| 573 | - ); |
|
| 574 | - break; |
|
| 575 | - } |
|
| 576 | - do_action_ref_array( |
|
| 577 | - 'AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send__with_registrations', |
|
| 578 | - array( |
|
| 579 | - $registrations_used_for_contact_data, |
|
| 580 | - $Message_Template_Group->ID(), |
|
| 581 | - ) |
|
| 582 | - ); |
|
| 583 | - // kept for backward compat, internally we no longer use this action. |
|
| 584 | - // @deprecated 4.8.36.rc.002 |
|
| 585 | - $contacts = $id_type === 'registration' |
|
| 586 | - ? EEM_Attendee::instance()->get_array_of_contacts_from_reg_ids($ids) |
|
| 587 | - : EEM_Attendee::instance()->get_all(array(array('ATT_ID' => array('in', $ids)))); |
|
| 588 | - do_action_ref_array( |
|
| 589 | - 'AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send', |
|
| 590 | - array( |
|
| 591 | - $contacts, |
|
| 592 | - $Message_Template_Group->ID(), |
|
| 593 | - ) |
|
| 594 | - ); |
|
| 595 | - } |
|
| 596 | - $query_args = array( |
|
| 597 | - 'action' => ! empty($this->_req_data['redirect_back_to']) |
|
| 598 | - ? $this->_req_data['redirect_back_to'] |
|
| 599 | - : 'default', |
|
| 600 | - ); |
|
| 601 | - $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 602 | - } |
|
| 473 | + /** |
|
| 474 | + * Handles sending selected registrations/contacts a newsletter. |
|
| 475 | + * |
|
| 476 | + * @since 4.3.0 |
|
| 477 | + * @return void |
|
| 478 | + * @throws EE_Error |
|
| 479 | + * @throws InvalidArgumentException |
|
| 480 | + * @throws InvalidDataTypeException |
|
| 481 | + * @throws InvalidInterfaceException |
|
| 482 | + */ |
|
| 483 | + protected function _newsletter_selected_send() |
|
| 484 | + { |
|
| 485 | + $success = true; |
|
| 486 | + // first we need to make sure we have a GRP_ID so we know what template we're sending and updating! |
|
| 487 | + if (empty($this->_req_data['newsletter_mtp_selected'])) { |
|
| 488 | + EE_Error::add_error( |
|
| 489 | + esc_html__( |
|
| 490 | + 'In order to send a message, a Message Template GRP_ID is needed. It was not provided so messages were not sent.', |
|
| 491 | + 'event_espresso' |
|
| 492 | + ), |
|
| 493 | + __FILE__, |
|
| 494 | + __FUNCTION__, |
|
| 495 | + __LINE__ |
|
| 496 | + ); |
|
| 497 | + $success = false; |
|
| 498 | + } |
|
| 499 | + if ($success) { |
|
| 500 | + // update Message template in case there are any changes |
|
| 501 | + $Message_Template_Group = EEM_Message_Template_Group::instance()->get_one_by_ID( |
|
| 502 | + $this->_req_data['newsletter_mtp_selected'] |
|
| 503 | + ); |
|
| 504 | + $Message_Templates = $Message_Template_Group instanceof EE_Message_Template_Group |
|
| 505 | + ? $Message_Template_Group->context_templates() |
|
| 506 | + : array(); |
|
| 507 | + if (empty($Message_Templates)) { |
|
| 508 | + EE_Error::add_error( |
|
| 509 | + esc_html__( |
|
| 510 | + 'Unable to retrieve message template fields from the db. Messages not sent.', |
|
| 511 | + 'event_espresso' |
|
| 512 | + ), |
|
| 513 | + __FILE__, |
|
| 514 | + __FUNCTION__, |
|
| 515 | + __LINE__ |
|
| 516 | + ); |
|
| 517 | + } |
|
| 518 | + // let's just update the specific fields |
|
| 519 | + foreach ($Message_Templates['attendee'] as $Message_Template) { |
|
| 520 | + if ($Message_Template instanceof EE_Message_Template) { |
|
| 521 | + $field = $Message_Template->get('MTP_template_field'); |
|
| 522 | + $content = $Message_Template->get('MTP_content'); |
|
| 523 | + $new_content = $content; |
|
| 524 | + switch ($field) { |
|
| 525 | + case 'from': |
|
| 526 | + $new_content = ! empty($this->_req_data['batch_message']['from']) |
|
| 527 | + ? $this->_req_data['batch_message']['from'] |
|
| 528 | + : $content; |
|
| 529 | + break; |
|
| 530 | + case 'subject': |
|
| 531 | + $new_content = ! empty($this->_req_data['batch_message']['subject']) |
|
| 532 | + ? $this->_req_data['batch_message']['subject'] |
|
| 533 | + : $content; |
|
| 534 | + break; |
|
| 535 | + case 'content': |
|
| 536 | + $new_content = $content; |
|
| 537 | + $new_content['newsletter_content'] = ! empty($this->_req_data['batch_message']['content']) |
|
| 538 | + ? $this->_req_data['batch_message']['content'] |
|
| 539 | + : $content['newsletter_content']; |
|
| 540 | + break; |
|
| 541 | + default: |
|
| 542 | + // continue the foreach loop, we don't want to set $new_content nor save. |
|
| 543 | + continue 2; |
|
| 544 | + } |
|
| 545 | + $Message_Template->set('MTP_content', $new_content); |
|
| 546 | + $Message_Template->save(); |
|
| 547 | + } |
|
| 548 | + } |
|
| 549 | + // great fields are updated! now let's make sure we just have contact objects (EE_Attendee). |
|
| 550 | + $id_type = ! empty($this->_req_data['batch_message']['id_type']) |
|
| 551 | + ? $this->_req_data['batch_message']['id_type'] |
|
| 552 | + : 'registration'; |
|
| 553 | + // id_type will affect how we assemble the ids. |
|
| 554 | + $ids = ! empty($this->_req_data['batch_message']['ids']) |
|
| 555 | + ? json_decode(stripslashes($this->_req_data['batch_message']['ids'])) |
|
| 556 | + : array(); |
|
| 557 | + $registrations_used_for_contact_data = array(); |
|
| 558 | + // using switch because eventually we'll have other contexts that will be used for generating messages. |
|
| 559 | + switch ($id_type) { |
|
| 560 | + case 'registration': |
|
| 561 | + $registrations_used_for_contact_data = EEM_Registration::instance()->get_all( |
|
| 562 | + array( |
|
| 563 | + array( |
|
| 564 | + 'REG_ID' => array('IN', $ids), |
|
| 565 | + ), |
|
| 566 | + ) |
|
| 567 | + ); |
|
| 568 | + break; |
|
| 569 | + case 'contact': |
|
| 570 | + $registrations_used_for_contact_data = EEM_Registration::instance() |
|
| 571 | + ->get_latest_registration_for_each_of_given_contacts( |
|
| 572 | + $ids |
|
| 573 | + ); |
|
| 574 | + break; |
|
| 575 | + } |
|
| 576 | + do_action_ref_array( |
|
| 577 | + 'AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send__with_registrations', |
|
| 578 | + array( |
|
| 579 | + $registrations_used_for_contact_data, |
|
| 580 | + $Message_Template_Group->ID(), |
|
| 581 | + ) |
|
| 582 | + ); |
|
| 583 | + // kept for backward compat, internally we no longer use this action. |
|
| 584 | + // @deprecated 4.8.36.rc.002 |
|
| 585 | + $contacts = $id_type === 'registration' |
|
| 586 | + ? EEM_Attendee::instance()->get_array_of_contacts_from_reg_ids($ids) |
|
| 587 | + : EEM_Attendee::instance()->get_all(array(array('ATT_ID' => array('in', $ids)))); |
|
| 588 | + do_action_ref_array( |
|
| 589 | + 'AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send', |
|
| 590 | + array( |
|
| 591 | + $contacts, |
|
| 592 | + $Message_Template_Group->ID(), |
|
| 593 | + ) |
|
| 594 | + ); |
|
| 595 | + } |
|
| 596 | + $query_args = array( |
|
| 597 | + 'action' => ! empty($this->_req_data['redirect_back_to']) |
|
| 598 | + ? $this->_req_data['redirect_back_to'] |
|
| 599 | + : 'default', |
|
| 600 | + ); |
|
| 601 | + $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 602 | + } |
|
| 603 | 603 | |
| 604 | 604 | |
| 605 | - /** |
|
| 606 | - * This is called when javascript is being enqueued to setup the various data needed for the reports js. |
|
| 607 | - * Also $this->{$_reports_template_data} property is set for later usage by the _registration_reports method. |
|
| 608 | - */ |
|
| 609 | - protected function _registration_reports_js_setup() |
|
| 610 | - { |
|
| 611 | - $this->_reports_template_data['admin_reports'][] = $this->_registrations_per_day_report(); |
|
| 612 | - $this->_reports_template_data['admin_reports'][] = $this->_registrations_per_event_report(); |
|
| 613 | - } |
|
| 605 | + /** |
|
| 606 | + * This is called when javascript is being enqueued to setup the various data needed for the reports js. |
|
| 607 | + * Also $this->{$_reports_template_data} property is set for later usage by the _registration_reports method. |
|
| 608 | + */ |
|
| 609 | + protected function _registration_reports_js_setup() |
|
| 610 | + { |
|
| 611 | + $this->_reports_template_data['admin_reports'][] = $this->_registrations_per_day_report(); |
|
| 612 | + $this->_reports_template_data['admin_reports'][] = $this->_registrations_per_event_report(); |
|
| 613 | + } |
|
| 614 | 614 | |
| 615 | 615 | |
| 616 | - /** |
|
| 617 | - * generates Business Reports regarding Registrations |
|
| 618 | - * |
|
| 619 | - * @access protected |
|
| 620 | - * @return void |
|
| 621 | - * @throws DomainException |
|
| 622 | - */ |
|
| 623 | - protected function _registration_reports() |
|
| 624 | - { |
|
| 625 | - $template_path = EE_ADMIN_TEMPLATE . 'admin_reports.template.php'; |
|
| 626 | - $this->_template_args['admin_page_content'] = EEH_Template::display_template( |
|
| 627 | - $template_path, |
|
| 628 | - $this->_reports_template_data, |
|
| 629 | - true |
|
| 630 | - ); |
|
| 631 | - // the final template wrapper |
|
| 632 | - $this->display_admin_page_with_no_sidebar(); |
|
| 633 | - } |
|
| 616 | + /** |
|
| 617 | + * generates Business Reports regarding Registrations |
|
| 618 | + * |
|
| 619 | + * @access protected |
|
| 620 | + * @return void |
|
| 621 | + * @throws DomainException |
|
| 622 | + */ |
|
| 623 | + protected function _registration_reports() |
|
| 624 | + { |
|
| 625 | + $template_path = EE_ADMIN_TEMPLATE . 'admin_reports.template.php'; |
|
| 626 | + $this->_template_args['admin_page_content'] = EEH_Template::display_template( |
|
| 627 | + $template_path, |
|
| 628 | + $this->_reports_template_data, |
|
| 629 | + true |
|
| 630 | + ); |
|
| 631 | + // the final template wrapper |
|
| 632 | + $this->display_admin_page_with_no_sidebar(); |
|
| 633 | + } |
|
| 634 | 634 | |
| 635 | 635 | |
| 636 | - /** |
|
| 637 | - * Generates Business Report showing total registrations per day. |
|
| 638 | - * |
|
| 639 | - * @param string $period The period (acceptable by PHP Datetime constructor) for which the report is generated. |
|
| 640 | - * @return string |
|
| 641 | - * @throws EE_Error |
|
| 642 | - * @throws InvalidArgumentException |
|
| 643 | - * @throws InvalidDataTypeException |
|
| 644 | - * @throws InvalidInterfaceException |
|
| 645 | - */ |
|
| 646 | - private function _registrations_per_day_report($period = '-1 month') |
|
| 647 | - { |
|
| 648 | - $report_ID = 'reg-admin-registrations-per-day-report-dv'; |
|
| 649 | - $results = EEM_Registration::instance()->get_registrations_per_day_and_per_status_report($period); |
|
| 650 | - $results = (array) $results; |
|
| 651 | - $regs = array(); |
|
| 652 | - $subtitle = ''; |
|
| 653 | - if ($results) { |
|
| 654 | - $column_titles = array(); |
|
| 655 | - $tracker = 0; |
|
| 656 | - foreach ($results as $result) { |
|
| 657 | - $report_column_values = array(); |
|
| 658 | - foreach ($result as $property_name => $property_value) { |
|
| 659 | - $property_value = $property_name === 'Registration_REG_date' ? $property_value |
|
| 660 | - : (int) $property_value; |
|
| 661 | - $report_column_values[] = $property_value; |
|
| 662 | - if ($tracker === 0) { |
|
| 663 | - if ($property_name === 'Registration_REG_date') { |
|
| 664 | - $column_titles[] = esc_html__( |
|
| 665 | - 'Date (only days with registrations are shown)', |
|
| 666 | - 'event_espresso' |
|
| 667 | - ); |
|
| 668 | - } else { |
|
| 669 | - $column_titles[] = EEH_Template::pretty_status($property_name, false, 'sentence'); |
|
| 670 | - } |
|
| 671 | - } |
|
| 672 | - } |
|
| 673 | - $tracker++; |
|
| 674 | - $regs[] = $report_column_values; |
|
| 675 | - } |
|
| 676 | - // make sure the column_titles is pushed to the beginning of the array |
|
| 677 | - array_unshift($regs, $column_titles); |
|
| 678 | - // setup the date range. |
|
| 679 | - $DateTimeZone = new DateTimeZone(EEH_DTT_Helper::get_timezone()); |
|
| 680 | - $beginning_date = new DateTime("now " . $period, $DateTimeZone); |
|
| 681 | - $ending_date = new DateTime("now", $DateTimeZone); |
|
| 682 | - $subtitle = sprintf( |
|
| 683 | - _x('For the period: %1$s to %2$s', 'Used to give date range', 'event_espresso'), |
|
| 684 | - $beginning_date->format('Y-m-d'), |
|
| 685 | - $ending_date->format('Y-m-d') |
|
| 686 | - ); |
|
| 687 | - } |
|
| 688 | - $report_title = esc_html__('Total Registrations per Day', 'event_espresso'); |
|
| 689 | - $report_params = array( |
|
| 690 | - 'title' => $report_title, |
|
| 691 | - 'subtitle' => $subtitle, |
|
| 692 | - 'id' => $report_ID, |
|
| 693 | - 'regs' => $regs, |
|
| 694 | - 'noResults' => empty($regs), |
|
| 695 | - 'noRegsMsg' => sprintf( |
|
| 696 | - esc_html__( |
|
| 697 | - '%sThere are currently no registration records in the last month for this report.%s', |
|
| 698 | - 'event_espresso' |
|
| 699 | - ), |
|
| 700 | - '<h2>' . $report_title . '</h2><p>', |
|
| 701 | - '</p>' |
|
| 702 | - ), |
|
| 703 | - ); |
|
| 704 | - wp_localize_script('ee-reg-reports-js', 'regPerDay', $report_params); |
|
| 705 | - return $report_ID; |
|
| 706 | - } |
|
| 636 | + /** |
|
| 637 | + * Generates Business Report showing total registrations per day. |
|
| 638 | + * |
|
| 639 | + * @param string $period The period (acceptable by PHP Datetime constructor) for which the report is generated. |
|
| 640 | + * @return string |
|
| 641 | + * @throws EE_Error |
|
| 642 | + * @throws InvalidArgumentException |
|
| 643 | + * @throws InvalidDataTypeException |
|
| 644 | + * @throws InvalidInterfaceException |
|
| 645 | + */ |
|
| 646 | + private function _registrations_per_day_report($period = '-1 month') |
|
| 647 | + { |
|
| 648 | + $report_ID = 'reg-admin-registrations-per-day-report-dv'; |
|
| 649 | + $results = EEM_Registration::instance()->get_registrations_per_day_and_per_status_report($period); |
|
| 650 | + $results = (array) $results; |
|
| 651 | + $regs = array(); |
|
| 652 | + $subtitle = ''; |
|
| 653 | + if ($results) { |
|
| 654 | + $column_titles = array(); |
|
| 655 | + $tracker = 0; |
|
| 656 | + foreach ($results as $result) { |
|
| 657 | + $report_column_values = array(); |
|
| 658 | + foreach ($result as $property_name => $property_value) { |
|
| 659 | + $property_value = $property_name === 'Registration_REG_date' ? $property_value |
|
| 660 | + : (int) $property_value; |
|
| 661 | + $report_column_values[] = $property_value; |
|
| 662 | + if ($tracker === 0) { |
|
| 663 | + if ($property_name === 'Registration_REG_date') { |
|
| 664 | + $column_titles[] = esc_html__( |
|
| 665 | + 'Date (only days with registrations are shown)', |
|
| 666 | + 'event_espresso' |
|
| 667 | + ); |
|
| 668 | + } else { |
|
| 669 | + $column_titles[] = EEH_Template::pretty_status($property_name, false, 'sentence'); |
|
| 670 | + } |
|
| 671 | + } |
|
| 672 | + } |
|
| 673 | + $tracker++; |
|
| 674 | + $regs[] = $report_column_values; |
|
| 675 | + } |
|
| 676 | + // make sure the column_titles is pushed to the beginning of the array |
|
| 677 | + array_unshift($regs, $column_titles); |
|
| 678 | + // setup the date range. |
|
| 679 | + $DateTimeZone = new DateTimeZone(EEH_DTT_Helper::get_timezone()); |
|
| 680 | + $beginning_date = new DateTime("now " . $period, $DateTimeZone); |
|
| 681 | + $ending_date = new DateTime("now", $DateTimeZone); |
|
| 682 | + $subtitle = sprintf( |
|
| 683 | + _x('For the period: %1$s to %2$s', 'Used to give date range', 'event_espresso'), |
|
| 684 | + $beginning_date->format('Y-m-d'), |
|
| 685 | + $ending_date->format('Y-m-d') |
|
| 686 | + ); |
|
| 687 | + } |
|
| 688 | + $report_title = esc_html__('Total Registrations per Day', 'event_espresso'); |
|
| 689 | + $report_params = array( |
|
| 690 | + 'title' => $report_title, |
|
| 691 | + 'subtitle' => $subtitle, |
|
| 692 | + 'id' => $report_ID, |
|
| 693 | + 'regs' => $regs, |
|
| 694 | + 'noResults' => empty($regs), |
|
| 695 | + 'noRegsMsg' => sprintf( |
|
| 696 | + esc_html__( |
|
| 697 | + '%sThere are currently no registration records in the last month for this report.%s', |
|
| 698 | + 'event_espresso' |
|
| 699 | + ), |
|
| 700 | + '<h2>' . $report_title . '</h2><p>', |
|
| 701 | + '</p>' |
|
| 702 | + ), |
|
| 703 | + ); |
|
| 704 | + wp_localize_script('ee-reg-reports-js', 'regPerDay', $report_params); |
|
| 705 | + return $report_ID; |
|
| 706 | + } |
|
| 707 | 707 | |
| 708 | 708 | |
| 709 | - /** |
|
| 710 | - * Generates Business Report showing total registrations per event. |
|
| 711 | - * |
|
| 712 | - * @param string $period The period (acceptable by PHP Datetime constructor) for which the report is generated. |
|
| 713 | - * @return string |
|
| 714 | - * @throws EE_Error |
|
| 715 | - * @throws InvalidArgumentException |
|
| 716 | - * @throws InvalidDataTypeException |
|
| 717 | - * @throws InvalidInterfaceException |
|
| 718 | - */ |
|
| 719 | - private function _registrations_per_event_report($period = '-1 month') |
|
| 720 | - { |
|
| 721 | - $report_ID = 'reg-admin-registrations-per-event-report-dv'; |
|
| 722 | - $results = EEM_Registration::instance()->get_registrations_per_event_and_per_status_report($period); |
|
| 723 | - $results = (array) $results; |
|
| 724 | - $regs = array(); |
|
| 725 | - $subtitle = ''; |
|
| 726 | - if ($results) { |
|
| 727 | - $column_titles = array(); |
|
| 728 | - $tracker = 0; |
|
| 729 | - foreach ($results as $result) { |
|
| 730 | - $report_column_values = array(); |
|
| 731 | - foreach ($result as $property_name => $property_value) { |
|
| 732 | - $property_value = $property_name === 'Registration_Event' ? wp_trim_words( |
|
| 733 | - $property_value, |
|
| 734 | - 4, |
|
| 735 | - '...' |
|
| 736 | - ) : (int) $property_value; |
|
| 737 | - $report_column_values[] = $property_value; |
|
| 738 | - if ($tracker === 0) { |
|
| 739 | - if ($property_name === 'Registration_Event') { |
|
| 740 | - $column_titles[] = esc_html__('Event', 'event_espresso'); |
|
| 741 | - } else { |
|
| 742 | - $column_titles[] = EEH_Template::pretty_status($property_name, false, 'sentence'); |
|
| 743 | - } |
|
| 744 | - } |
|
| 745 | - } |
|
| 746 | - $tracker++; |
|
| 747 | - $regs[] = $report_column_values; |
|
| 748 | - } |
|
| 749 | - // make sure the column_titles is pushed to the beginning of the array |
|
| 750 | - array_unshift($regs, $column_titles); |
|
| 751 | - // setup the date range. |
|
| 752 | - $DateTimeZone = new DateTimeZone(EEH_DTT_Helper::get_timezone()); |
|
| 753 | - $beginning_date = new DateTime("now " . $period, $DateTimeZone); |
|
| 754 | - $ending_date = new DateTime("now", $DateTimeZone); |
|
| 755 | - $subtitle = sprintf( |
|
| 756 | - _x('For the period: %1$s to %2$s', 'Used to give date range', 'event_espresso'), |
|
| 757 | - $beginning_date->format('Y-m-d'), |
|
| 758 | - $ending_date->format('Y-m-d') |
|
| 759 | - ); |
|
| 760 | - } |
|
| 761 | - $report_title = esc_html__('Total Registrations per Event', 'event_espresso'); |
|
| 762 | - $report_params = array( |
|
| 763 | - 'title' => $report_title, |
|
| 764 | - 'subtitle' => $subtitle, |
|
| 765 | - 'id' => $report_ID, |
|
| 766 | - 'regs' => $regs, |
|
| 767 | - 'noResults' => empty($regs), |
|
| 768 | - 'noRegsMsg' => sprintf( |
|
| 769 | - esc_html__( |
|
| 770 | - '%sThere are currently no registration records in the last month for this report.%s', |
|
| 771 | - 'event_espresso' |
|
| 772 | - ), |
|
| 773 | - '<h2>' . $report_title . '</h2><p>', |
|
| 774 | - '</p>' |
|
| 775 | - ), |
|
| 776 | - ); |
|
| 777 | - wp_localize_script('ee-reg-reports-js', 'regPerEvent', $report_params); |
|
| 778 | - return $report_ID; |
|
| 779 | - } |
|
| 709 | + /** |
|
| 710 | + * Generates Business Report showing total registrations per event. |
|
| 711 | + * |
|
| 712 | + * @param string $period The period (acceptable by PHP Datetime constructor) for which the report is generated. |
|
| 713 | + * @return string |
|
| 714 | + * @throws EE_Error |
|
| 715 | + * @throws InvalidArgumentException |
|
| 716 | + * @throws InvalidDataTypeException |
|
| 717 | + * @throws InvalidInterfaceException |
|
| 718 | + */ |
|
| 719 | + private function _registrations_per_event_report($period = '-1 month') |
|
| 720 | + { |
|
| 721 | + $report_ID = 'reg-admin-registrations-per-event-report-dv'; |
|
| 722 | + $results = EEM_Registration::instance()->get_registrations_per_event_and_per_status_report($period); |
|
| 723 | + $results = (array) $results; |
|
| 724 | + $regs = array(); |
|
| 725 | + $subtitle = ''; |
|
| 726 | + if ($results) { |
|
| 727 | + $column_titles = array(); |
|
| 728 | + $tracker = 0; |
|
| 729 | + foreach ($results as $result) { |
|
| 730 | + $report_column_values = array(); |
|
| 731 | + foreach ($result as $property_name => $property_value) { |
|
| 732 | + $property_value = $property_name === 'Registration_Event' ? wp_trim_words( |
|
| 733 | + $property_value, |
|
| 734 | + 4, |
|
| 735 | + '...' |
|
| 736 | + ) : (int) $property_value; |
|
| 737 | + $report_column_values[] = $property_value; |
|
| 738 | + if ($tracker === 0) { |
|
| 739 | + if ($property_name === 'Registration_Event') { |
|
| 740 | + $column_titles[] = esc_html__('Event', 'event_espresso'); |
|
| 741 | + } else { |
|
| 742 | + $column_titles[] = EEH_Template::pretty_status($property_name, false, 'sentence'); |
|
| 743 | + } |
|
| 744 | + } |
|
| 745 | + } |
|
| 746 | + $tracker++; |
|
| 747 | + $regs[] = $report_column_values; |
|
| 748 | + } |
|
| 749 | + // make sure the column_titles is pushed to the beginning of the array |
|
| 750 | + array_unshift($regs, $column_titles); |
|
| 751 | + // setup the date range. |
|
| 752 | + $DateTimeZone = new DateTimeZone(EEH_DTT_Helper::get_timezone()); |
|
| 753 | + $beginning_date = new DateTime("now " . $period, $DateTimeZone); |
|
| 754 | + $ending_date = new DateTime("now", $DateTimeZone); |
|
| 755 | + $subtitle = sprintf( |
|
| 756 | + _x('For the period: %1$s to %2$s', 'Used to give date range', 'event_espresso'), |
|
| 757 | + $beginning_date->format('Y-m-d'), |
|
| 758 | + $ending_date->format('Y-m-d') |
|
| 759 | + ); |
|
| 760 | + } |
|
| 761 | + $report_title = esc_html__('Total Registrations per Event', 'event_espresso'); |
|
| 762 | + $report_params = array( |
|
| 763 | + 'title' => $report_title, |
|
| 764 | + 'subtitle' => $subtitle, |
|
| 765 | + 'id' => $report_ID, |
|
| 766 | + 'regs' => $regs, |
|
| 767 | + 'noResults' => empty($regs), |
|
| 768 | + 'noRegsMsg' => sprintf( |
|
| 769 | + esc_html__( |
|
| 770 | + '%sThere are currently no registration records in the last month for this report.%s', |
|
| 771 | + 'event_espresso' |
|
| 772 | + ), |
|
| 773 | + '<h2>' . $report_title . '</h2><p>', |
|
| 774 | + '</p>' |
|
| 775 | + ), |
|
| 776 | + ); |
|
| 777 | + wp_localize_script('ee-reg-reports-js', 'regPerEvent', $report_params); |
|
| 778 | + return $report_ID; |
|
| 779 | + } |
|
| 780 | 780 | |
| 781 | 781 | |
| 782 | - /** |
|
| 783 | - * generates HTML for the Registration Check-in list table (showing all Check-ins for a specific registration) |
|
| 784 | - * |
|
| 785 | - * @access protected |
|
| 786 | - * @return void |
|
| 787 | - * @throws EE_Error |
|
| 788 | - * @throws InvalidArgumentException |
|
| 789 | - * @throws InvalidDataTypeException |
|
| 790 | - * @throws InvalidInterfaceException |
|
| 791 | - * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 792 | - */ |
|
| 793 | - protected function _registration_checkin_list_table() |
|
| 794 | - { |
|
| 795 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 796 | - $reg_id = isset($this->_req_data['_REG_ID']) ? $this->_req_data['_REG_ID'] : null; |
|
| 797 | - /** @var EE_Registration $registration */ |
|
| 798 | - $registration = EEM_Registration::instance()->get_one_by_ID($reg_id); |
|
| 799 | - $attendee = $registration->attendee(); |
|
| 800 | - $this->_admin_page_title .= $this->get_action_link_or_button( |
|
| 801 | - 'new_registration', |
|
| 802 | - 'add-registrant', |
|
| 803 | - array('event_id' => $registration->event_ID()), |
|
| 804 | - 'add-new-h2' |
|
| 805 | - ); |
|
| 806 | - $checked_in = new CheckinStatusDashicon(EE_Checkin::status_checked_in); |
|
| 807 | - $checked_out = new CheckinStatusDashicon(EE_Checkin::status_checked_out); |
|
| 808 | - $legend_items = array( |
|
| 809 | - 'checkin' => array( |
|
| 810 | - 'class' => $checked_in->cssClasses(), |
|
| 811 | - 'desc' => $checked_in->legendLabel(), |
|
| 812 | - ), |
|
| 813 | - 'checkout' => array( |
|
| 814 | - 'class' => $checked_out->cssClasses(), |
|
| 815 | - 'desc' => $checked_out->legendLabel(), |
|
| 816 | - ), |
|
| 817 | - ); |
|
| 818 | - $this->_template_args['after_list_table'] = $this->_display_legend($legend_items); |
|
| 819 | - $dtt_id = isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : null; |
|
| 820 | - /** @var EE_Datetime $datetime */ |
|
| 821 | - $datetime = EEM_Datetime::instance()->get_one_by_ID($dtt_id); |
|
| 822 | - $datetime_label = ''; |
|
| 823 | - if ($datetime instanceof EE_Datetime) { |
|
| 824 | - $datetime_label = $datetime->get_dtt_display_name(true); |
|
| 825 | - $datetime_label .= ! empty($datetime_label) |
|
| 826 | - ? ' (' . $datetime->get_dtt_display_name() . ')' |
|
| 827 | - : $datetime->get_dtt_display_name(); |
|
| 828 | - } |
|
| 829 | - $datetime_link = ! empty($dtt_id) && $registration instanceof EE_Registration |
|
| 830 | - ? EE_Admin_Page::add_query_args_and_nonce( |
|
| 831 | - array( |
|
| 832 | - 'action' => 'event_registrations', |
|
| 833 | - 'event_id' => $registration->event_ID(), |
|
| 834 | - 'DTT_ID' => $dtt_id, |
|
| 835 | - ), |
|
| 836 | - $this->_admin_base_url |
|
| 837 | - ) |
|
| 838 | - : ''; |
|
| 839 | - $datetime_link = ! empty($datetime_link) |
|
| 840 | - ? '<a href="' . $datetime_link . '">' |
|
| 841 | - . '<span id="checkin-dtt">' |
|
| 842 | - . $datetime_label |
|
| 843 | - . '</span></a>' |
|
| 844 | - : $datetime_label; |
|
| 845 | - $attendee_name = $attendee instanceof EE_Attendee |
|
| 846 | - ? $attendee->full_name() |
|
| 847 | - : ''; |
|
| 848 | - $attendee_link = $attendee instanceof EE_Attendee |
|
| 849 | - ? $attendee->get_admin_details_link() |
|
| 850 | - : ''; |
|
| 851 | - $attendee_link = ! empty($attendee_link) |
|
| 852 | - ? '<a href="' . $attendee->get_admin_details_link() . '"' |
|
| 853 | - . ' title="' . esc_html__('Click for attendee details', 'event_espresso') . '">' |
|
| 854 | - . '<span id="checkin-attendee-name">' |
|
| 855 | - . $attendee_name |
|
| 856 | - . '</span></a>' |
|
| 857 | - : ''; |
|
| 858 | - $event_link = $registration->event() instanceof EE_Event |
|
| 859 | - ? $registration->event()->get_admin_details_link() |
|
| 860 | - : ''; |
|
| 861 | - $event_link = ! empty($event_link) |
|
| 862 | - ? '<a href="' . $event_link . '"' |
|
| 863 | - . ' title="' . esc_html__('Click here to edit event.', 'event_espresso') . '">' |
|
| 864 | - . '<span id="checkin-event-name">' |
|
| 865 | - . $registration->event_name() |
|
| 866 | - . '</span>' |
|
| 867 | - . '</a>' |
|
| 868 | - : ''; |
|
| 869 | - $this->_template_args['before_list_table'] = ! empty($reg_id) && ! empty($dtt_id) |
|
| 870 | - ? '<h2>' . sprintf( |
|
| 871 | - esc_html__('Displaying check in records for %1$s for %2$s at the event, %3$s', 'event_espresso'), |
|
| 872 | - $attendee_link, |
|
| 873 | - $datetime_link, |
|
| 874 | - $event_link |
|
| 875 | - ) . '</h2>' |
|
| 876 | - : ''; |
|
| 877 | - $this->_template_args['list_table_hidden_fields'] = ! empty($reg_id) |
|
| 878 | - ? '<input type="hidden" name="_REG_ID" value="' . $reg_id . '">' : ''; |
|
| 879 | - $this->_template_args['list_table_hidden_fields'] .= ! empty($dtt_id) |
|
| 880 | - ? '<input type="hidden" name="DTT_ID" value="' . $dtt_id . '">' : ''; |
|
| 881 | - $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 882 | - } |
|
| 782 | + /** |
|
| 783 | + * generates HTML for the Registration Check-in list table (showing all Check-ins for a specific registration) |
|
| 784 | + * |
|
| 785 | + * @access protected |
|
| 786 | + * @return void |
|
| 787 | + * @throws EE_Error |
|
| 788 | + * @throws InvalidArgumentException |
|
| 789 | + * @throws InvalidDataTypeException |
|
| 790 | + * @throws InvalidInterfaceException |
|
| 791 | + * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 792 | + */ |
|
| 793 | + protected function _registration_checkin_list_table() |
|
| 794 | + { |
|
| 795 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 796 | + $reg_id = isset($this->_req_data['_REG_ID']) ? $this->_req_data['_REG_ID'] : null; |
|
| 797 | + /** @var EE_Registration $registration */ |
|
| 798 | + $registration = EEM_Registration::instance()->get_one_by_ID($reg_id); |
|
| 799 | + $attendee = $registration->attendee(); |
|
| 800 | + $this->_admin_page_title .= $this->get_action_link_or_button( |
|
| 801 | + 'new_registration', |
|
| 802 | + 'add-registrant', |
|
| 803 | + array('event_id' => $registration->event_ID()), |
|
| 804 | + 'add-new-h2' |
|
| 805 | + ); |
|
| 806 | + $checked_in = new CheckinStatusDashicon(EE_Checkin::status_checked_in); |
|
| 807 | + $checked_out = new CheckinStatusDashicon(EE_Checkin::status_checked_out); |
|
| 808 | + $legend_items = array( |
|
| 809 | + 'checkin' => array( |
|
| 810 | + 'class' => $checked_in->cssClasses(), |
|
| 811 | + 'desc' => $checked_in->legendLabel(), |
|
| 812 | + ), |
|
| 813 | + 'checkout' => array( |
|
| 814 | + 'class' => $checked_out->cssClasses(), |
|
| 815 | + 'desc' => $checked_out->legendLabel(), |
|
| 816 | + ), |
|
| 817 | + ); |
|
| 818 | + $this->_template_args['after_list_table'] = $this->_display_legend($legend_items); |
|
| 819 | + $dtt_id = isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : null; |
|
| 820 | + /** @var EE_Datetime $datetime */ |
|
| 821 | + $datetime = EEM_Datetime::instance()->get_one_by_ID($dtt_id); |
|
| 822 | + $datetime_label = ''; |
|
| 823 | + if ($datetime instanceof EE_Datetime) { |
|
| 824 | + $datetime_label = $datetime->get_dtt_display_name(true); |
|
| 825 | + $datetime_label .= ! empty($datetime_label) |
|
| 826 | + ? ' (' . $datetime->get_dtt_display_name() . ')' |
|
| 827 | + : $datetime->get_dtt_display_name(); |
|
| 828 | + } |
|
| 829 | + $datetime_link = ! empty($dtt_id) && $registration instanceof EE_Registration |
|
| 830 | + ? EE_Admin_Page::add_query_args_and_nonce( |
|
| 831 | + array( |
|
| 832 | + 'action' => 'event_registrations', |
|
| 833 | + 'event_id' => $registration->event_ID(), |
|
| 834 | + 'DTT_ID' => $dtt_id, |
|
| 835 | + ), |
|
| 836 | + $this->_admin_base_url |
|
| 837 | + ) |
|
| 838 | + : ''; |
|
| 839 | + $datetime_link = ! empty($datetime_link) |
|
| 840 | + ? '<a href="' . $datetime_link . '">' |
|
| 841 | + . '<span id="checkin-dtt">' |
|
| 842 | + . $datetime_label |
|
| 843 | + . '</span></a>' |
|
| 844 | + : $datetime_label; |
|
| 845 | + $attendee_name = $attendee instanceof EE_Attendee |
|
| 846 | + ? $attendee->full_name() |
|
| 847 | + : ''; |
|
| 848 | + $attendee_link = $attendee instanceof EE_Attendee |
|
| 849 | + ? $attendee->get_admin_details_link() |
|
| 850 | + : ''; |
|
| 851 | + $attendee_link = ! empty($attendee_link) |
|
| 852 | + ? '<a href="' . $attendee->get_admin_details_link() . '"' |
|
| 853 | + . ' title="' . esc_html__('Click for attendee details', 'event_espresso') . '">' |
|
| 854 | + . '<span id="checkin-attendee-name">' |
|
| 855 | + . $attendee_name |
|
| 856 | + . '</span></a>' |
|
| 857 | + : ''; |
|
| 858 | + $event_link = $registration->event() instanceof EE_Event |
|
| 859 | + ? $registration->event()->get_admin_details_link() |
|
| 860 | + : ''; |
|
| 861 | + $event_link = ! empty($event_link) |
|
| 862 | + ? '<a href="' . $event_link . '"' |
|
| 863 | + . ' title="' . esc_html__('Click here to edit event.', 'event_espresso') . '">' |
|
| 864 | + . '<span id="checkin-event-name">' |
|
| 865 | + . $registration->event_name() |
|
| 866 | + . '</span>' |
|
| 867 | + . '</a>' |
|
| 868 | + : ''; |
|
| 869 | + $this->_template_args['before_list_table'] = ! empty($reg_id) && ! empty($dtt_id) |
|
| 870 | + ? '<h2>' . sprintf( |
|
| 871 | + esc_html__('Displaying check in records for %1$s for %2$s at the event, %3$s', 'event_espresso'), |
|
| 872 | + $attendee_link, |
|
| 873 | + $datetime_link, |
|
| 874 | + $event_link |
|
| 875 | + ) . '</h2>' |
|
| 876 | + : ''; |
|
| 877 | + $this->_template_args['list_table_hidden_fields'] = ! empty($reg_id) |
|
| 878 | + ? '<input type="hidden" name="_REG_ID" value="' . $reg_id . '">' : ''; |
|
| 879 | + $this->_template_args['list_table_hidden_fields'] .= ! empty($dtt_id) |
|
| 880 | + ? '<input type="hidden" name="DTT_ID" value="' . $dtt_id . '">' : ''; |
|
| 881 | + $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 882 | + } |
|
| 883 | 883 | |
| 884 | 884 | |
| 885 | - /** |
|
| 886 | - * toggle the Check-in status for the given registration (coming from ajax) |
|
| 887 | - * |
|
| 888 | - * @return void (JSON) |
|
| 889 | - * @throws EE_Error |
|
| 890 | - * @throws InvalidArgumentException |
|
| 891 | - * @throws InvalidDataTypeException |
|
| 892 | - * @throws InvalidInterfaceException |
|
| 893 | - */ |
|
| 894 | - public function toggle_checkin_status() |
|
| 895 | - { |
|
| 896 | - // first make sure we have the necessary data |
|
| 897 | - if (! isset($this->_req_data['_regid'])) { |
|
| 898 | - EE_Error::add_error( |
|
| 899 | - esc_html__( |
|
| 900 | - 'There must be something broken with the html structure because the required data for toggling the Check-in status is not being sent via ajax', |
|
| 901 | - 'event_espresso' |
|
| 902 | - ), |
|
| 903 | - __FILE__, |
|
| 904 | - __FUNCTION__, |
|
| 905 | - __LINE__ |
|
| 906 | - ); |
|
| 907 | - $this->_template_args['success'] = false; |
|
| 908 | - $this->_template_args['error'] = true; |
|
| 909 | - $this->_return_json(); |
|
| 910 | - }; |
|
| 911 | - // do a nonce check cause we're not coming in from an normal route here. |
|
| 912 | - $nonce = isset($this->_req_data['checkinnonce']) ? sanitize_text_field($this->_req_data['checkinnonce']) |
|
| 913 | - : ''; |
|
| 914 | - $nonce_ref = 'checkin_nonce'; |
|
| 915 | - $this->_verify_nonce($nonce, $nonce_ref); |
|
| 916 | - // beautiful! Made it this far so let's get the status. |
|
| 917 | - $new_status = new CheckinStatusDashicon($this->_toggle_checkin_status()); |
|
| 918 | - // setup new class to return via ajax |
|
| 919 | - $this->_template_args['admin_page_content'] = 'clickable trigger-checkin ' . $new_status->cssClasses(); |
|
| 920 | - $this->_template_args['success'] = true; |
|
| 921 | - $this->_return_json(); |
|
| 922 | - } |
|
| 885 | + /** |
|
| 886 | + * toggle the Check-in status for the given registration (coming from ajax) |
|
| 887 | + * |
|
| 888 | + * @return void (JSON) |
|
| 889 | + * @throws EE_Error |
|
| 890 | + * @throws InvalidArgumentException |
|
| 891 | + * @throws InvalidDataTypeException |
|
| 892 | + * @throws InvalidInterfaceException |
|
| 893 | + */ |
|
| 894 | + public function toggle_checkin_status() |
|
| 895 | + { |
|
| 896 | + // first make sure we have the necessary data |
|
| 897 | + if (! isset($this->_req_data['_regid'])) { |
|
| 898 | + EE_Error::add_error( |
|
| 899 | + esc_html__( |
|
| 900 | + 'There must be something broken with the html structure because the required data for toggling the Check-in status is not being sent via ajax', |
|
| 901 | + 'event_espresso' |
|
| 902 | + ), |
|
| 903 | + __FILE__, |
|
| 904 | + __FUNCTION__, |
|
| 905 | + __LINE__ |
|
| 906 | + ); |
|
| 907 | + $this->_template_args['success'] = false; |
|
| 908 | + $this->_template_args['error'] = true; |
|
| 909 | + $this->_return_json(); |
|
| 910 | + }; |
|
| 911 | + // do a nonce check cause we're not coming in from an normal route here. |
|
| 912 | + $nonce = isset($this->_req_data['checkinnonce']) ? sanitize_text_field($this->_req_data['checkinnonce']) |
|
| 913 | + : ''; |
|
| 914 | + $nonce_ref = 'checkin_nonce'; |
|
| 915 | + $this->_verify_nonce($nonce, $nonce_ref); |
|
| 916 | + // beautiful! Made it this far so let's get the status. |
|
| 917 | + $new_status = new CheckinStatusDashicon($this->_toggle_checkin_status()); |
|
| 918 | + // setup new class to return via ajax |
|
| 919 | + $this->_template_args['admin_page_content'] = 'clickable trigger-checkin ' . $new_status->cssClasses(); |
|
| 920 | + $this->_template_args['success'] = true; |
|
| 921 | + $this->_return_json(); |
|
| 922 | + } |
|
| 923 | 923 | |
| 924 | 924 | |
| 925 | - /** |
|
| 926 | - * handles toggling the checkin status for the registration, |
|
| 927 | - * |
|
| 928 | - * @access protected |
|
| 929 | - * @return int|void |
|
| 930 | - * @throws EE_Error |
|
| 931 | - * @throws InvalidArgumentException |
|
| 932 | - * @throws InvalidDataTypeException |
|
| 933 | - * @throws InvalidInterfaceException |
|
| 934 | - */ |
|
| 935 | - protected function _toggle_checkin_status() |
|
| 936 | - { |
|
| 937 | - // first let's get the query args out of the way for the redirect |
|
| 938 | - $query_args = array( |
|
| 939 | - 'action' => 'event_registrations', |
|
| 940 | - 'event_id' => isset($this->_req_data['event_id']) ? $this->_req_data['event_id'] : null, |
|
| 941 | - 'DTT_ID' => isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : null, |
|
| 942 | - ); |
|
| 943 | - $new_status = false; |
|
| 944 | - // bulk action check in toggle |
|
| 945 | - if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 946 | - // cycle thru checkboxes |
|
| 947 | - while (list($REG_ID, $value) = each($this->_req_data['checkbox'])) { |
|
| 948 | - $DTT_ID = isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : null; |
|
| 949 | - $new_status = $this->_toggle_checkin($REG_ID, $DTT_ID); |
|
| 950 | - } |
|
| 951 | - } elseif (isset($this->_req_data['_regid'])) { |
|
| 952 | - // coming from ajax request |
|
| 953 | - $DTT_ID = isset($this->_req_data['dttid']) ? $this->_req_data['dttid'] : null; |
|
| 954 | - $query_args['DTT_ID'] = $DTT_ID; |
|
| 955 | - $new_status = $this->_toggle_checkin($this->_req_data['_regid'], $DTT_ID); |
|
| 956 | - } else { |
|
| 957 | - EE_Error::add_error( |
|
| 958 | - esc_html__('Missing some required data to toggle the Check-in', 'event_espresso'), |
|
| 959 | - __FILE__, |
|
| 960 | - __FUNCTION__, |
|
| 961 | - __LINE__ |
|
| 962 | - ); |
|
| 963 | - } |
|
| 964 | - if (defined('DOING_AJAX')) { |
|
| 965 | - return $new_status; |
|
| 966 | - } |
|
| 967 | - $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 968 | - } |
|
| 925 | + /** |
|
| 926 | + * handles toggling the checkin status for the registration, |
|
| 927 | + * |
|
| 928 | + * @access protected |
|
| 929 | + * @return int|void |
|
| 930 | + * @throws EE_Error |
|
| 931 | + * @throws InvalidArgumentException |
|
| 932 | + * @throws InvalidDataTypeException |
|
| 933 | + * @throws InvalidInterfaceException |
|
| 934 | + */ |
|
| 935 | + protected function _toggle_checkin_status() |
|
| 936 | + { |
|
| 937 | + // first let's get the query args out of the way for the redirect |
|
| 938 | + $query_args = array( |
|
| 939 | + 'action' => 'event_registrations', |
|
| 940 | + 'event_id' => isset($this->_req_data['event_id']) ? $this->_req_data['event_id'] : null, |
|
| 941 | + 'DTT_ID' => isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : null, |
|
| 942 | + ); |
|
| 943 | + $new_status = false; |
|
| 944 | + // bulk action check in toggle |
|
| 945 | + if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 946 | + // cycle thru checkboxes |
|
| 947 | + while (list($REG_ID, $value) = each($this->_req_data['checkbox'])) { |
|
| 948 | + $DTT_ID = isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : null; |
|
| 949 | + $new_status = $this->_toggle_checkin($REG_ID, $DTT_ID); |
|
| 950 | + } |
|
| 951 | + } elseif (isset($this->_req_data['_regid'])) { |
|
| 952 | + // coming from ajax request |
|
| 953 | + $DTT_ID = isset($this->_req_data['dttid']) ? $this->_req_data['dttid'] : null; |
|
| 954 | + $query_args['DTT_ID'] = $DTT_ID; |
|
| 955 | + $new_status = $this->_toggle_checkin($this->_req_data['_regid'], $DTT_ID); |
|
| 956 | + } else { |
|
| 957 | + EE_Error::add_error( |
|
| 958 | + esc_html__('Missing some required data to toggle the Check-in', 'event_espresso'), |
|
| 959 | + __FILE__, |
|
| 960 | + __FUNCTION__, |
|
| 961 | + __LINE__ |
|
| 962 | + ); |
|
| 963 | + } |
|
| 964 | + if (defined('DOING_AJAX')) { |
|
| 965 | + return $new_status; |
|
| 966 | + } |
|
| 967 | + $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 968 | + } |
|
| 969 | 969 | |
| 970 | 970 | |
| 971 | - /** |
|
| 972 | - * This is toggles a single Check-in for the given registration and datetime. |
|
| 973 | - * |
|
| 974 | - * @param int $REG_ID The registration we're toggling |
|
| 975 | - * @param int $DTT_ID The datetime we're toggling |
|
| 976 | - * @return int The new status toggled to. |
|
| 977 | - * @throws EE_Error |
|
| 978 | - * @throws InvalidArgumentException |
|
| 979 | - * @throws InvalidDataTypeException |
|
| 980 | - * @throws InvalidInterfaceException |
|
| 981 | - */ |
|
| 982 | - private function _toggle_checkin($REG_ID, $DTT_ID) |
|
| 983 | - { |
|
| 984 | - /** @var EE_Registration $REG */ |
|
| 985 | - $REG = EEM_Registration::instance()->get_one_by_ID($REG_ID); |
|
| 986 | - $new_status = $REG->toggle_checkin_status($DTT_ID); |
|
| 987 | - if ($new_status !== false) { |
|
| 988 | - EE_Error::add_success($REG->get_checkin_msg($DTT_ID)); |
|
| 989 | - } else { |
|
| 990 | - EE_Error::add_error($REG->get_checkin_msg($DTT_ID, true), __FILE__, __FUNCTION__, __LINE__); |
|
| 991 | - $new_status = false; |
|
| 992 | - } |
|
| 993 | - return $new_status; |
|
| 994 | - } |
|
| 971 | + /** |
|
| 972 | + * This is toggles a single Check-in for the given registration and datetime. |
|
| 973 | + * |
|
| 974 | + * @param int $REG_ID The registration we're toggling |
|
| 975 | + * @param int $DTT_ID The datetime we're toggling |
|
| 976 | + * @return int The new status toggled to. |
|
| 977 | + * @throws EE_Error |
|
| 978 | + * @throws InvalidArgumentException |
|
| 979 | + * @throws InvalidDataTypeException |
|
| 980 | + * @throws InvalidInterfaceException |
|
| 981 | + */ |
|
| 982 | + private function _toggle_checkin($REG_ID, $DTT_ID) |
|
| 983 | + { |
|
| 984 | + /** @var EE_Registration $REG */ |
|
| 985 | + $REG = EEM_Registration::instance()->get_one_by_ID($REG_ID); |
|
| 986 | + $new_status = $REG->toggle_checkin_status($DTT_ID); |
|
| 987 | + if ($new_status !== false) { |
|
| 988 | + EE_Error::add_success($REG->get_checkin_msg($DTT_ID)); |
|
| 989 | + } else { |
|
| 990 | + EE_Error::add_error($REG->get_checkin_msg($DTT_ID, true), __FILE__, __FUNCTION__, __LINE__); |
|
| 991 | + $new_status = false; |
|
| 992 | + } |
|
| 993 | + return $new_status; |
|
| 994 | + } |
|
| 995 | 995 | |
| 996 | 996 | |
| 997 | - /** |
|
| 998 | - * Takes care of deleting multiple EE_Checkin table rows |
|
| 999 | - * |
|
| 1000 | - * @access protected |
|
| 1001 | - * @return void |
|
| 1002 | - * @throws EE_Error |
|
| 1003 | - * @throws InvalidArgumentException |
|
| 1004 | - * @throws InvalidDataTypeException |
|
| 1005 | - * @throws InvalidInterfaceException |
|
| 1006 | - */ |
|
| 1007 | - protected function _delete_checkin_rows() |
|
| 1008 | - { |
|
| 1009 | - $query_args = array( |
|
| 1010 | - 'action' => 'registration_checkins', |
|
| 1011 | - 'DTT_ID' => isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : 0, |
|
| 1012 | - '_REG_ID' => isset($this->_req_data['_REG_ID']) ? $this->_req_data['_REG_ID'] : 0, |
|
| 1013 | - ); |
|
| 1014 | - $errors = 0; |
|
| 1015 | - if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 1016 | - while (list($CHK_ID, $value) = each($this->_req_data['checkbox'])) { |
|
| 1017 | - if (! EEM_Checkin::instance()->delete_by_ID($CHK_ID)) { |
|
| 1018 | - $errors++; |
|
| 1019 | - } |
|
| 1020 | - } |
|
| 1021 | - } else { |
|
| 1022 | - EE_Error::add_error( |
|
| 1023 | - esc_html__( |
|
| 1024 | - 'So, something went wrong with the bulk delete because there was no data received for instructions on WHAT to delete!', |
|
| 1025 | - 'event_espresso' |
|
| 1026 | - ), |
|
| 1027 | - __FILE__, |
|
| 1028 | - __FUNCTION__, |
|
| 1029 | - __LINE__ |
|
| 1030 | - ); |
|
| 1031 | - $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 1032 | - } |
|
| 1033 | - if ($errors > 0) { |
|
| 1034 | - EE_Error::add_error( |
|
| 1035 | - sprintf(__('There were %d records that did not delete successfully', 'event_espresso'), $errors), |
|
| 1036 | - __FILE__, |
|
| 1037 | - __FUNCTION__, |
|
| 1038 | - __LINE__ |
|
| 1039 | - ); |
|
| 1040 | - } else { |
|
| 1041 | - EE_Error::add_success(__('Records were successfully deleted', 'event_espresso')); |
|
| 1042 | - } |
|
| 1043 | - $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 1044 | - } |
|
| 997 | + /** |
|
| 998 | + * Takes care of deleting multiple EE_Checkin table rows |
|
| 999 | + * |
|
| 1000 | + * @access protected |
|
| 1001 | + * @return void |
|
| 1002 | + * @throws EE_Error |
|
| 1003 | + * @throws InvalidArgumentException |
|
| 1004 | + * @throws InvalidDataTypeException |
|
| 1005 | + * @throws InvalidInterfaceException |
|
| 1006 | + */ |
|
| 1007 | + protected function _delete_checkin_rows() |
|
| 1008 | + { |
|
| 1009 | + $query_args = array( |
|
| 1010 | + 'action' => 'registration_checkins', |
|
| 1011 | + 'DTT_ID' => isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : 0, |
|
| 1012 | + '_REG_ID' => isset($this->_req_data['_REG_ID']) ? $this->_req_data['_REG_ID'] : 0, |
|
| 1013 | + ); |
|
| 1014 | + $errors = 0; |
|
| 1015 | + if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 1016 | + while (list($CHK_ID, $value) = each($this->_req_data['checkbox'])) { |
|
| 1017 | + if (! EEM_Checkin::instance()->delete_by_ID($CHK_ID)) { |
|
| 1018 | + $errors++; |
|
| 1019 | + } |
|
| 1020 | + } |
|
| 1021 | + } else { |
|
| 1022 | + EE_Error::add_error( |
|
| 1023 | + esc_html__( |
|
| 1024 | + 'So, something went wrong with the bulk delete because there was no data received for instructions on WHAT to delete!', |
|
| 1025 | + 'event_espresso' |
|
| 1026 | + ), |
|
| 1027 | + __FILE__, |
|
| 1028 | + __FUNCTION__, |
|
| 1029 | + __LINE__ |
|
| 1030 | + ); |
|
| 1031 | + $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 1032 | + } |
|
| 1033 | + if ($errors > 0) { |
|
| 1034 | + EE_Error::add_error( |
|
| 1035 | + sprintf(__('There were %d records that did not delete successfully', 'event_espresso'), $errors), |
|
| 1036 | + __FILE__, |
|
| 1037 | + __FUNCTION__, |
|
| 1038 | + __LINE__ |
|
| 1039 | + ); |
|
| 1040 | + } else { |
|
| 1041 | + EE_Error::add_success(__('Records were successfully deleted', 'event_espresso')); |
|
| 1042 | + } |
|
| 1043 | + $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 1044 | + } |
|
| 1045 | 1045 | |
| 1046 | 1046 | |
| 1047 | - /** |
|
| 1048 | - * Deletes a single EE_Checkin row |
|
| 1049 | - * |
|
| 1050 | - * @return void |
|
| 1051 | - * @throws EE_Error |
|
| 1052 | - * @throws InvalidArgumentException |
|
| 1053 | - * @throws InvalidDataTypeException |
|
| 1054 | - * @throws InvalidInterfaceException |
|
| 1055 | - */ |
|
| 1056 | - protected function _delete_checkin_row() |
|
| 1057 | - { |
|
| 1058 | - $query_args = array( |
|
| 1059 | - 'action' => 'registration_checkins', |
|
| 1060 | - 'DTT_ID' => isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : 0, |
|
| 1061 | - '_REG_ID' => isset($this->_req_data['_REG_ID']) ? $this->_req_data['_REG_ID'] : 0, |
|
| 1062 | - ); |
|
| 1063 | - if (! empty($this->_req_data['CHK_ID'])) { |
|
| 1064 | - if (! EEM_Checkin::instance()->delete_by_ID($this->_req_data['CHK_ID'])) { |
|
| 1065 | - EE_Error::add_error( |
|
| 1066 | - esc_html__('Something went wrong and this check-in record was not deleted', 'event_espresso'), |
|
| 1067 | - __FILE__, |
|
| 1068 | - __FUNCTION__, |
|
| 1069 | - __LINE__ |
|
| 1070 | - ); |
|
| 1071 | - } else { |
|
| 1072 | - EE_Error::add_success(__('Check-In record successfully deleted', 'event_espresso')); |
|
| 1073 | - } |
|
| 1074 | - } else { |
|
| 1075 | - EE_Error::add_error( |
|
| 1076 | - esc_html__( |
|
| 1077 | - 'In order to delete a Check-in record, there must be a Check-In ID available. There is not. It is not your fault, there is just a gremlin living in the code', |
|
| 1078 | - 'event_espresso' |
|
| 1079 | - ), |
|
| 1080 | - __FILE__, |
|
| 1081 | - __FUNCTION__, |
|
| 1082 | - __LINE__ |
|
| 1083 | - ); |
|
| 1084 | - } |
|
| 1085 | - $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 1086 | - } |
|
| 1047 | + /** |
|
| 1048 | + * Deletes a single EE_Checkin row |
|
| 1049 | + * |
|
| 1050 | + * @return void |
|
| 1051 | + * @throws EE_Error |
|
| 1052 | + * @throws InvalidArgumentException |
|
| 1053 | + * @throws InvalidDataTypeException |
|
| 1054 | + * @throws InvalidInterfaceException |
|
| 1055 | + */ |
|
| 1056 | + protected function _delete_checkin_row() |
|
| 1057 | + { |
|
| 1058 | + $query_args = array( |
|
| 1059 | + 'action' => 'registration_checkins', |
|
| 1060 | + 'DTT_ID' => isset($this->_req_data['DTT_ID']) ? $this->_req_data['DTT_ID'] : 0, |
|
| 1061 | + '_REG_ID' => isset($this->_req_data['_REG_ID']) ? $this->_req_data['_REG_ID'] : 0, |
|
| 1062 | + ); |
|
| 1063 | + if (! empty($this->_req_data['CHK_ID'])) { |
|
| 1064 | + if (! EEM_Checkin::instance()->delete_by_ID($this->_req_data['CHK_ID'])) { |
|
| 1065 | + EE_Error::add_error( |
|
| 1066 | + esc_html__('Something went wrong and this check-in record was not deleted', 'event_espresso'), |
|
| 1067 | + __FILE__, |
|
| 1068 | + __FUNCTION__, |
|
| 1069 | + __LINE__ |
|
| 1070 | + ); |
|
| 1071 | + } else { |
|
| 1072 | + EE_Error::add_success(__('Check-In record successfully deleted', 'event_espresso')); |
|
| 1073 | + } |
|
| 1074 | + } else { |
|
| 1075 | + EE_Error::add_error( |
|
| 1076 | + esc_html__( |
|
| 1077 | + 'In order to delete a Check-in record, there must be a Check-In ID available. There is not. It is not your fault, there is just a gremlin living in the code', |
|
| 1078 | + 'event_espresso' |
|
| 1079 | + ), |
|
| 1080 | + __FILE__, |
|
| 1081 | + __FUNCTION__, |
|
| 1082 | + __LINE__ |
|
| 1083 | + ); |
|
| 1084 | + } |
|
| 1085 | + $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 1086 | + } |
|
| 1087 | 1087 | |
| 1088 | 1088 | |
| 1089 | - /** |
|
| 1090 | - * generates HTML for the Event Registrations List Table |
|
| 1091 | - * |
|
| 1092 | - * @access protected |
|
| 1093 | - * @return void |
|
| 1094 | - * @throws EE_Error |
|
| 1095 | - * @throws InvalidArgumentException |
|
| 1096 | - * @throws InvalidDataTypeException |
|
| 1097 | - * @throws InvalidInterfaceException |
|
| 1098 | - */ |
|
| 1099 | - protected function _event_registrations_list_table() |
|
| 1100 | - { |
|
| 1101 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 1102 | - $this->_admin_page_title .= isset($this->_req_data['event_id']) |
|
| 1103 | - ? $this->get_action_link_or_button( |
|
| 1104 | - 'new_registration', |
|
| 1105 | - 'add-registrant', |
|
| 1106 | - array('event_id' => $this->_req_data['event_id']), |
|
| 1107 | - 'add-new-h2', |
|
| 1108 | - '', |
|
| 1109 | - false |
|
| 1110 | - ) |
|
| 1111 | - : ''; |
|
| 1112 | - $checked_in = new CheckinStatusDashicon(EE_Checkin::status_checked_in); |
|
| 1113 | - $checked_out = new CheckinStatusDashicon(EE_Checkin::status_checked_out); |
|
| 1114 | - $checked_never = new CheckinStatusDashicon(EE_Checkin::status_checked_never); |
|
| 1115 | - $legend_items = array( |
|
| 1116 | - 'star-icon' => array( |
|
| 1117 | - 'class' => 'dashicons dashicons-star-filled lt-blue-icon ee-icon-size-8', |
|
| 1118 | - 'desc' => esc_html__('This Registrant is the Primary Registrant', 'event_espresso'), |
|
| 1119 | - ), |
|
| 1120 | - 'checkin' => array( |
|
| 1121 | - 'class' => $checked_in->cssClasses(), |
|
| 1122 | - 'desc' => $checked_in->legendLabel(), |
|
| 1123 | - ), |
|
| 1124 | - 'checkout' => array( |
|
| 1125 | - 'class' => $checked_out->cssClasses(), |
|
| 1126 | - 'desc' => $checked_out->legendLabel(), |
|
| 1127 | - ), |
|
| 1128 | - 'nocheckinrecord' => array( |
|
| 1129 | - 'class' => $checked_never->cssClasses(), |
|
| 1130 | - 'desc' => $checked_never->legendLabel(), |
|
| 1131 | - ), |
|
| 1132 | - 'approved_status' => array( |
|
| 1133 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_approved, |
|
| 1134 | - 'desc' => EEH_Template::pretty_status(EEM_Registration::status_id_approved, false, 'sentence'), |
|
| 1135 | - ), |
|
| 1136 | - 'cancelled_status' => array( |
|
| 1137 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_cancelled, |
|
| 1138 | - 'desc' => EEH_Template::pretty_status(EEM_Registration::status_id_cancelled, false, 'sentence'), |
|
| 1139 | - ), |
|
| 1140 | - 'declined_status' => array( |
|
| 1141 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_declined, |
|
| 1142 | - 'desc' => EEH_Template::pretty_status(EEM_Registration::status_id_declined, false, 'sentence'), |
|
| 1143 | - ), |
|
| 1144 | - 'not_approved' => array( |
|
| 1145 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_not_approved, |
|
| 1146 | - 'desc' => EEH_Template::pretty_status(EEM_Registration::status_id_not_approved, false, 'sentence'), |
|
| 1147 | - ), |
|
| 1148 | - 'pending_status' => array( |
|
| 1149 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_pending_payment, |
|
| 1150 | - 'desc' => EEH_Template::pretty_status(EEM_Registration::status_id_pending_payment, false, 'sentence'), |
|
| 1151 | - ), |
|
| 1152 | - 'wait_list' => array( |
|
| 1153 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_wait_list, |
|
| 1154 | - 'desc' => EEH_Template::pretty_status(EEM_Registration::status_id_wait_list, false, 'sentence'), |
|
| 1155 | - ), |
|
| 1156 | - ); |
|
| 1157 | - $this->_template_args['after_list_table'] = $this->_display_legend($legend_items); |
|
| 1158 | - $event_id = isset($this->_req_data['event_id']) ? $this->_req_data['event_id'] : null; |
|
| 1159 | - $this->_template_args['before_list_table'] = ! empty($event_id) |
|
| 1160 | - ? '<h2>' . sprintf( |
|
| 1161 | - esc_html__('Viewing Registrations for Event: %s', 'event_espresso'), |
|
| 1162 | - EEM_Event::instance()->get_one_by_ID($event_id)->get('EVT_name') |
|
| 1163 | - ) . '</h2>' |
|
| 1164 | - : ''; |
|
| 1165 | - // need to get the number of datetimes on the event and set default datetime_id if there is only one datetime on |
|
| 1166 | - // the event. |
|
| 1167 | - /** @var EE_Event $event */ |
|
| 1168 | - $event = EEM_Event::instance()->get_one_by_ID($event_id); |
|
| 1169 | - $DTT_ID = ! empty($this->_req_data['DTT_ID']) ? absint($this->_req_data['DTT_ID']) : 0; |
|
| 1170 | - $datetime = null; |
|
| 1171 | - if ($event instanceof EE_Event) { |
|
| 1172 | - $datetimes_on_event = $event->datetimes(); |
|
| 1173 | - if (count($datetimes_on_event) === 1) { |
|
| 1174 | - $datetime = reset($datetimes_on_event); |
|
| 1175 | - } |
|
| 1176 | - } |
|
| 1177 | - $datetime = $datetime instanceof EE_Datetime ? $datetime : EEM_Datetime::instance()->get_one_by_ID($DTT_ID); |
|
| 1178 | - if ($datetime instanceof EE_Datetime && $this->_template_args['before_list_table'] !== '') { |
|
| 1179 | - $this->_template_args['before_list_table'] = substr($this->_template_args['before_list_table'], 0, -5); |
|
| 1180 | - $this->_template_args['before_list_table'] .= ' <span class="drk-grey-text">'; |
|
| 1181 | - $this->_template_args['before_list_table'] .= '<span class="dashicons dashicons-calendar"></span>'; |
|
| 1182 | - $this->_template_args['before_list_table'] .= $datetime->name(); |
|
| 1183 | - $this->_template_args['before_list_table'] .= ' ( ' . $datetime->date_and_time_range() . ' )'; |
|
| 1184 | - $this->_template_args['before_list_table'] .= '</span></h2>'; |
|
| 1185 | - } |
|
| 1186 | - // if no datetime, then we're on the initial view, so let's give some helpful instructions on what the status |
|
| 1187 | - // column represents |
|
| 1188 | - if (! $datetime instanceof EE_Datetime) { |
|
| 1189 | - $this->_template_args['before_list_table'] .= '<br><p class="description">' |
|
| 1190 | - . esc_html__( |
|
| 1191 | - 'In this view, the check-in status represents the latest check-in record for the registration in that row.', |
|
| 1192 | - 'event_espresso' |
|
| 1193 | - ) |
|
| 1194 | - . '</p>'; |
|
| 1195 | - } |
|
| 1196 | - $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 1197 | - } |
|
| 1089 | + /** |
|
| 1090 | + * generates HTML for the Event Registrations List Table |
|
| 1091 | + * |
|
| 1092 | + * @access protected |
|
| 1093 | + * @return void |
|
| 1094 | + * @throws EE_Error |
|
| 1095 | + * @throws InvalidArgumentException |
|
| 1096 | + * @throws InvalidDataTypeException |
|
| 1097 | + * @throws InvalidInterfaceException |
|
| 1098 | + */ |
|
| 1099 | + protected function _event_registrations_list_table() |
|
| 1100 | + { |
|
| 1101 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 1102 | + $this->_admin_page_title .= isset($this->_req_data['event_id']) |
|
| 1103 | + ? $this->get_action_link_or_button( |
|
| 1104 | + 'new_registration', |
|
| 1105 | + 'add-registrant', |
|
| 1106 | + array('event_id' => $this->_req_data['event_id']), |
|
| 1107 | + 'add-new-h2', |
|
| 1108 | + '', |
|
| 1109 | + false |
|
| 1110 | + ) |
|
| 1111 | + : ''; |
|
| 1112 | + $checked_in = new CheckinStatusDashicon(EE_Checkin::status_checked_in); |
|
| 1113 | + $checked_out = new CheckinStatusDashicon(EE_Checkin::status_checked_out); |
|
| 1114 | + $checked_never = new CheckinStatusDashicon(EE_Checkin::status_checked_never); |
|
| 1115 | + $legend_items = array( |
|
| 1116 | + 'star-icon' => array( |
|
| 1117 | + 'class' => 'dashicons dashicons-star-filled lt-blue-icon ee-icon-size-8', |
|
| 1118 | + 'desc' => esc_html__('This Registrant is the Primary Registrant', 'event_espresso'), |
|
| 1119 | + ), |
|
| 1120 | + 'checkin' => array( |
|
| 1121 | + 'class' => $checked_in->cssClasses(), |
|
| 1122 | + 'desc' => $checked_in->legendLabel(), |
|
| 1123 | + ), |
|
| 1124 | + 'checkout' => array( |
|
| 1125 | + 'class' => $checked_out->cssClasses(), |
|
| 1126 | + 'desc' => $checked_out->legendLabel(), |
|
| 1127 | + ), |
|
| 1128 | + 'nocheckinrecord' => array( |
|
| 1129 | + 'class' => $checked_never->cssClasses(), |
|
| 1130 | + 'desc' => $checked_never->legendLabel(), |
|
| 1131 | + ), |
|
| 1132 | + 'approved_status' => array( |
|
| 1133 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_approved, |
|
| 1134 | + 'desc' => EEH_Template::pretty_status(EEM_Registration::status_id_approved, false, 'sentence'), |
|
| 1135 | + ), |
|
| 1136 | + 'cancelled_status' => array( |
|
| 1137 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_cancelled, |
|
| 1138 | + 'desc' => EEH_Template::pretty_status(EEM_Registration::status_id_cancelled, false, 'sentence'), |
|
| 1139 | + ), |
|
| 1140 | + 'declined_status' => array( |
|
| 1141 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_declined, |
|
| 1142 | + 'desc' => EEH_Template::pretty_status(EEM_Registration::status_id_declined, false, 'sentence'), |
|
| 1143 | + ), |
|
| 1144 | + 'not_approved' => array( |
|
| 1145 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_not_approved, |
|
| 1146 | + 'desc' => EEH_Template::pretty_status(EEM_Registration::status_id_not_approved, false, 'sentence'), |
|
| 1147 | + ), |
|
| 1148 | + 'pending_status' => array( |
|
| 1149 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_pending_payment, |
|
| 1150 | + 'desc' => EEH_Template::pretty_status(EEM_Registration::status_id_pending_payment, false, 'sentence'), |
|
| 1151 | + ), |
|
| 1152 | + 'wait_list' => array( |
|
| 1153 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_wait_list, |
|
| 1154 | + 'desc' => EEH_Template::pretty_status(EEM_Registration::status_id_wait_list, false, 'sentence'), |
|
| 1155 | + ), |
|
| 1156 | + ); |
|
| 1157 | + $this->_template_args['after_list_table'] = $this->_display_legend($legend_items); |
|
| 1158 | + $event_id = isset($this->_req_data['event_id']) ? $this->_req_data['event_id'] : null; |
|
| 1159 | + $this->_template_args['before_list_table'] = ! empty($event_id) |
|
| 1160 | + ? '<h2>' . sprintf( |
|
| 1161 | + esc_html__('Viewing Registrations for Event: %s', 'event_espresso'), |
|
| 1162 | + EEM_Event::instance()->get_one_by_ID($event_id)->get('EVT_name') |
|
| 1163 | + ) . '</h2>' |
|
| 1164 | + : ''; |
|
| 1165 | + // need to get the number of datetimes on the event and set default datetime_id if there is only one datetime on |
|
| 1166 | + // the event. |
|
| 1167 | + /** @var EE_Event $event */ |
|
| 1168 | + $event = EEM_Event::instance()->get_one_by_ID($event_id); |
|
| 1169 | + $DTT_ID = ! empty($this->_req_data['DTT_ID']) ? absint($this->_req_data['DTT_ID']) : 0; |
|
| 1170 | + $datetime = null; |
|
| 1171 | + if ($event instanceof EE_Event) { |
|
| 1172 | + $datetimes_on_event = $event->datetimes(); |
|
| 1173 | + if (count($datetimes_on_event) === 1) { |
|
| 1174 | + $datetime = reset($datetimes_on_event); |
|
| 1175 | + } |
|
| 1176 | + } |
|
| 1177 | + $datetime = $datetime instanceof EE_Datetime ? $datetime : EEM_Datetime::instance()->get_one_by_ID($DTT_ID); |
|
| 1178 | + if ($datetime instanceof EE_Datetime && $this->_template_args['before_list_table'] !== '') { |
|
| 1179 | + $this->_template_args['before_list_table'] = substr($this->_template_args['before_list_table'], 0, -5); |
|
| 1180 | + $this->_template_args['before_list_table'] .= ' <span class="drk-grey-text">'; |
|
| 1181 | + $this->_template_args['before_list_table'] .= '<span class="dashicons dashicons-calendar"></span>'; |
|
| 1182 | + $this->_template_args['before_list_table'] .= $datetime->name(); |
|
| 1183 | + $this->_template_args['before_list_table'] .= ' ( ' . $datetime->date_and_time_range() . ' )'; |
|
| 1184 | + $this->_template_args['before_list_table'] .= '</span></h2>'; |
|
| 1185 | + } |
|
| 1186 | + // if no datetime, then we're on the initial view, so let's give some helpful instructions on what the status |
|
| 1187 | + // column represents |
|
| 1188 | + if (! $datetime instanceof EE_Datetime) { |
|
| 1189 | + $this->_template_args['before_list_table'] .= '<br><p class="description">' |
|
| 1190 | + . esc_html__( |
|
| 1191 | + 'In this view, the check-in status represents the latest check-in record for the registration in that row.', |
|
| 1192 | + 'event_espresso' |
|
| 1193 | + ) |
|
| 1194 | + . '</p>'; |
|
| 1195 | + } |
|
| 1196 | + $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 1197 | + } |
|
| 1198 | 1198 | |
| 1199 | - /** |
|
| 1200 | - * Download the registrations check-in report (same as the normal registration report, but with different where |
|
| 1201 | - * conditions) |
|
| 1202 | - * |
|
| 1203 | - * @return void ends the request by a redirect or download |
|
| 1204 | - */ |
|
| 1205 | - public function _registrations_checkin_report() |
|
| 1206 | - { |
|
| 1207 | - $this->_registrations_report_base('_get_checkin_query_params_from_request'); |
|
| 1208 | - } |
|
| 1199 | + /** |
|
| 1200 | + * Download the registrations check-in report (same as the normal registration report, but with different where |
|
| 1201 | + * conditions) |
|
| 1202 | + * |
|
| 1203 | + * @return void ends the request by a redirect or download |
|
| 1204 | + */ |
|
| 1205 | + public function _registrations_checkin_report() |
|
| 1206 | + { |
|
| 1207 | + $this->_registrations_report_base('_get_checkin_query_params_from_request'); |
|
| 1208 | + } |
|
| 1209 | 1209 | |
| 1210 | - /** |
|
| 1211 | - * Gets the query params from the request, plus adds a where condition for the registration status, |
|
| 1212 | - * because on the checkin page we only ever want to see approved and pending-approval registrations |
|
| 1213 | - * |
|
| 1214 | - * @param array $request |
|
| 1215 | - * @param int $per_page |
|
| 1216 | - * @param bool $count |
|
| 1217 | - * @return array |
|
| 1218 | - * @throws EE_Error |
|
| 1219 | - */ |
|
| 1220 | - protected function _get_checkin_query_params_from_request( |
|
| 1221 | - $request, |
|
| 1222 | - $per_page = 10, |
|
| 1223 | - $count = false |
|
| 1224 | - ) { |
|
| 1225 | - $query_params = $this->_get_registration_query_parameters($request, $per_page, $count); |
|
| 1226 | - // unlike the regular registrations list table, |
|
| 1227 | - $status_ids_array = apply_filters( |
|
| 1228 | - 'FHEE__Extend_Registrations_Admin_Page__get_event_attendees__status_ids_array', |
|
| 1229 | - array(EEM_Registration::status_id_pending_payment, EEM_Registration::status_id_approved) |
|
| 1230 | - ); |
|
| 1231 | - $query_params[0]['STS_ID'] = array('IN', $status_ids_array); |
|
| 1232 | - return $query_params; |
|
| 1233 | - } |
|
| 1210 | + /** |
|
| 1211 | + * Gets the query params from the request, plus adds a where condition for the registration status, |
|
| 1212 | + * because on the checkin page we only ever want to see approved and pending-approval registrations |
|
| 1213 | + * |
|
| 1214 | + * @param array $request |
|
| 1215 | + * @param int $per_page |
|
| 1216 | + * @param bool $count |
|
| 1217 | + * @return array |
|
| 1218 | + * @throws EE_Error |
|
| 1219 | + */ |
|
| 1220 | + protected function _get_checkin_query_params_from_request( |
|
| 1221 | + $request, |
|
| 1222 | + $per_page = 10, |
|
| 1223 | + $count = false |
|
| 1224 | + ) { |
|
| 1225 | + $query_params = $this->_get_registration_query_parameters($request, $per_page, $count); |
|
| 1226 | + // unlike the regular registrations list table, |
|
| 1227 | + $status_ids_array = apply_filters( |
|
| 1228 | + 'FHEE__Extend_Registrations_Admin_Page__get_event_attendees__status_ids_array', |
|
| 1229 | + array(EEM_Registration::status_id_pending_payment, EEM_Registration::status_id_approved) |
|
| 1230 | + ); |
|
| 1231 | + $query_params[0]['STS_ID'] = array('IN', $status_ids_array); |
|
| 1232 | + return $query_params; |
|
| 1233 | + } |
|
| 1234 | 1234 | |
| 1235 | 1235 | |
| 1236 | - /** |
|
| 1237 | - * Gets registrations for an event |
|
| 1238 | - * |
|
| 1239 | - * @param int $per_page |
|
| 1240 | - * @param bool $count whether to return count or data. |
|
| 1241 | - * @param bool $trash |
|
| 1242 | - * @param string $orderby |
|
| 1243 | - * @return EE_Registration[]|int |
|
| 1244 | - * @throws EE_Error |
|
| 1245 | - * @throws InvalidArgumentException |
|
| 1246 | - * @throws InvalidDataTypeException |
|
| 1247 | - * @throws InvalidInterfaceException |
|
| 1248 | - */ |
|
| 1249 | - public function get_event_attendees($per_page = 10, $count = false, $trash = false, $orderby = 'ATT_fname') |
|
| 1250 | - { |
|
| 1251 | - // normalize some request params that get setup by the parent `get_registrations` method. |
|
| 1252 | - $request = $this->_req_data; |
|
| 1253 | - $request['orderby'] = ! empty($this->_req_data['orderby']) ? $this->_req_data['orderby'] : $orderby; |
|
| 1254 | - $request['order'] = ! empty($this->_req_data['order']) ? $this->_req_data['order'] : 'ASC'; |
|
| 1255 | - if ($trash) { |
|
| 1256 | - $request['status'] = 'trash'; |
|
| 1257 | - } |
|
| 1258 | - $query_params = $this->_get_checkin_query_params_from_request($request, $per_page, $count); |
|
| 1259 | - /** |
|
| 1260 | - * Override the default groupby added by EEM_Base so that sorts with multiple order bys work as expected |
|
| 1261 | - * |
|
| 1262 | - * @link https://events.codebasehq.com/projects/event-espresso/tickets/10093 |
|
| 1263 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1264 | - * or if you have the development copy of EE you can view this at the path: |
|
| 1265 | - * /docs/G--Model-System/model-query-params.md |
|
| 1266 | - */ |
|
| 1267 | - $query_params['group_by'] = ''; |
|
| 1236 | + /** |
|
| 1237 | + * Gets registrations for an event |
|
| 1238 | + * |
|
| 1239 | + * @param int $per_page |
|
| 1240 | + * @param bool $count whether to return count or data. |
|
| 1241 | + * @param bool $trash |
|
| 1242 | + * @param string $orderby |
|
| 1243 | + * @return EE_Registration[]|int |
|
| 1244 | + * @throws EE_Error |
|
| 1245 | + * @throws InvalidArgumentException |
|
| 1246 | + * @throws InvalidDataTypeException |
|
| 1247 | + * @throws InvalidInterfaceException |
|
| 1248 | + */ |
|
| 1249 | + public function get_event_attendees($per_page = 10, $count = false, $trash = false, $orderby = 'ATT_fname') |
|
| 1250 | + { |
|
| 1251 | + // normalize some request params that get setup by the parent `get_registrations` method. |
|
| 1252 | + $request = $this->_req_data; |
|
| 1253 | + $request['orderby'] = ! empty($this->_req_data['orderby']) ? $this->_req_data['orderby'] : $orderby; |
|
| 1254 | + $request['order'] = ! empty($this->_req_data['order']) ? $this->_req_data['order'] : 'ASC'; |
|
| 1255 | + if ($trash) { |
|
| 1256 | + $request['status'] = 'trash'; |
|
| 1257 | + } |
|
| 1258 | + $query_params = $this->_get_checkin_query_params_from_request($request, $per_page, $count); |
|
| 1259 | + /** |
|
| 1260 | + * Override the default groupby added by EEM_Base so that sorts with multiple order bys work as expected |
|
| 1261 | + * |
|
| 1262 | + * @link https://events.codebasehq.com/projects/event-espresso/tickets/10093 |
|
| 1263 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1264 | + * or if you have the development copy of EE you can view this at the path: |
|
| 1265 | + * /docs/G--Model-System/model-query-params.md |
|
| 1266 | + */ |
|
| 1267 | + $query_params['group_by'] = ''; |
|
| 1268 | 1268 | |
| 1269 | - return $count |
|
| 1270 | - ? EEM_Registration::instance()->count($query_params) |
|
| 1271 | - /** @type EE_Registration[] */ |
|
| 1272 | - : EEM_Registration::instance()->get_all($query_params); |
|
| 1273 | - } |
|
| 1269 | + return $count |
|
| 1270 | + ? EEM_Registration::instance()->count($query_params) |
|
| 1271 | + /** @type EE_Registration[] */ |
|
| 1272 | + : EEM_Registration::instance()->get_all($query_params); |
|
| 1273 | + } |
|
| 1274 | 1274 | } |
@@ -19,201 +19,201 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class PayPalSettingsForm extends EE_Payment_Method_Form |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @var string of HTML being the help tab link |
|
| 24 | - */ |
|
| 25 | - protected $helpTabLink; |
|
| 22 | + /** |
|
| 23 | + * @var string of HTML being the help tab link |
|
| 24 | + */ |
|
| 25 | + protected $helpTabLink; |
|
| 26 | 26 | |
| 27 | - public function __construct(array $options_array = array(), $help_tab_link = '') |
|
| 28 | - { |
|
| 29 | - $this->helpTabLink = $help_tab_link; |
|
| 30 | - $options_array = array_replace_recursive( |
|
| 31 | - array( |
|
| 32 | - 'extra_meta_inputs' => array( |
|
| 33 | - 'api_username' => new EE_Text_Input( |
|
| 34 | - array( |
|
| 35 | - 'html_label_text' => sprintf( |
|
| 36 | - // translators: %s link to help doc |
|
| 37 | - esc_html__('API Username %s', 'event_espresso'), |
|
| 38 | - $help_tab_link |
|
| 39 | - ), |
|
| 40 | - 'required' => true, |
|
| 41 | - ) |
|
| 42 | - ), |
|
| 43 | - 'api_password' => new EE_Text_Input( |
|
| 44 | - array( |
|
| 45 | - 'html_label_text' => sprintf( |
|
| 46 | - // translators: %s link to help doc |
|
| 47 | - esc_html__('API Password %s', 'event_espresso'), |
|
| 48 | - $help_tab_link |
|
| 49 | - ), |
|
| 50 | - 'required' => true, |
|
| 51 | - ) |
|
| 52 | - ), |
|
| 53 | - 'api_signature' => new EE_Text_Input( |
|
| 54 | - array( |
|
| 55 | - 'html_label_text' => sprintf( |
|
| 56 | - // translators: %s link to help doc |
|
| 57 | - esc_html__('API Signature %s', 'event_espresso'), |
|
| 58 | - $help_tab_link |
|
| 59 | - ), |
|
| 60 | - 'required' => true, |
|
| 61 | - ) |
|
| 62 | - ), |
|
| 63 | - ) |
|
| 64 | - ), |
|
| 65 | - $options_array |
|
| 66 | - ); |
|
| 67 | - parent::__construct($options_array); |
|
| 68 | - } |
|
| 27 | + public function __construct(array $options_array = array(), $help_tab_link = '') |
|
| 28 | + { |
|
| 29 | + $this->helpTabLink = $help_tab_link; |
|
| 30 | + $options_array = array_replace_recursive( |
|
| 31 | + array( |
|
| 32 | + 'extra_meta_inputs' => array( |
|
| 33 | + 'api_username' => new EE_Text_Input( |
|
| 34 | + array( |
|
| 35 | + 'html_label_text' => sprintf( |
|
| 36 | + // translators: %s link to help doc |
|
| 37 | + esc_html__('API Username %s', 'event_espresso'), |
|
| 38 | + $help_tab_link |
|
| 39 | + ), |
|
| 40 | + 'required' => true, |
|
| 41 | + ) |
|
| 42 | + ), |
|
| 43 | + 'api_password' => new EE_Text_Input( |
|
| 44 | + array( |
|
| 45 | + 'html_label_text' => sprintf( |
|
| 46 | + // translators: %s link to help doc |
|
| 47 | + esc_html__('API Password %s', 'event_espresso'), |
|
| 48 | + $help_tab_link |
|
| 49 | + ), |
|
| 50 | + 'required' => true, |
|
| 51 | + ) |
|
| 52 | + ), |
|
| 53 | + 'api_signature' => new EE_Text_Input( |
|
| 54 | + array( |
|
| 55 | + 'html_label_text' => sprintf( |
|
| 56 | + // translators: %s link to help doc |
|
| 57 | + esc_html__('API Signature %s', 'event_espresso'), |
|
| 58 | + $help_tab_link |
|
| 59 | + ), |
|
| 60 | + 'required' => true, |
|
| 61 | + ) |
|
| 62 | + ), |
|
| 63 | + ) |
|
| 64 | + ), |
|
| 65 | + $options_array |
|
| 66 | + ); |
|
| 67 | + parent::__construct($options_array); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Tests the the PayPal API credentials work ok |
|
| 72 | - * @return string of an error using the credentials, otherwise, if the credentials work, returns a blank string |
|
| 73 | - * @throws EE_Error |
|
| 74 | - */ |
|
| 75 | - protected function checkForCredentialsErrors() |
|
| 76 | - { |
|
| 77 | - $request_params = array( |
|
| 78 | - 'METHOD' => 'GetBalance', |
|
| 79 | - 'VERSION' => '204.0', |
|
| 80 | - 'USER' => urlencode($this->get_input_value('api_username')), |
|
| 81 | - 'PWD' => urlencode($this->get_input_value('api_password')), |
|
| 82 | - 'SIGNATURE' => urlencode($this->get_input_value('api_signature')), |
|
| 83 | - ); |
|
| 84 | - $gateway_url = $this->get_input_value('PMD_debug_mode') |
|
| 85 | - ? 'https://api-3t.sandbox.paypal.com/nvp' |
|
| 86 | - : 'https://api-3t.paypal.com/nvp'; |
|
| 87 | - // Request Customer Details. |
|
| 88 | - $response = wp_remote_post( |
|
| 89 | - $gateway_url, |
|
| 90 | - array( |
|
| 91 | - 'method' => 'POST', |
|
| 92 | - 'timeout' => 45, |
|
| 93 | - 'httpversion' => '1.1', |
|
| 94 | - 'cookies' => array(), |
|
| 95 | - 'headers' => array(), |
|
| 96 | - 'body' => http_build_query($request_params, '', '&'), |
|
| 97 | - ) |
|
| 98 | - ); |
|
| 99 | - if (is_wp_error($response) || empty($response['body'])) { |
|
| 100 | - // If we got here then there was an error in this request. |
|
| 101 | - // maybe is turned off. We don't know the credentials are invalid |
|
| 102 | - EE_Error::add_error( |
|
| 103 | - sprintf( |
|
| 104 | - // translators: %1$s Error message received from PayPal |
|
| 105 | - esc_html__( |
|
| 106 | - // @codingStandardsIgnoreStart |
|
| 107 | - 'Your PayPal credentials could not be verified. The following error occurred while communicating with PayPal: %1$s', |
|
| 108 | - // @codingStandardsIgnoreEnd |
|
| 109 | - 'event_espresso' |
|
| 110 | - ), |
|
| 111 | - $response->get_error_message() |
|
| 112 | - ), |
|
| 113 | - __FILE__, |
|
| 114 | - __FUNCTION__, |
|
| 115 | - __LINE__ |
|
| 116 | - ); |
|
| 117 | - } |
|
| 118 | - $response_args = array(); |
|
| 119 | - parse_str(urldecode($response['body']), $response_args); |
|
| 70 | + /** |
|
| 71 | + * Tests the the PayPal API credentials work ok |
|
| 72 | + * @return string of an error using the credentials, otherwise, if the credentials work, returns a blank string |
|
| 73 | + * @throws EE_Error |
|
| 74 | + */ |
|
| 75 | + protected function checkForCredentialsErrors() |
|
| 76 | + { |
|
| 77 | + $request_params = array( |
|
| 78 | + 'METHOD' => 'GetBalance', |
|
| 79 | + 'VERSION' => '204.0', |
|
| 80 | + 'USER' => urlencode($this->get_input_value('api_username')), |
|
| 81 | + 'PWD' => urlencode($this->get_input_value('api_password')), |
|
| 82 | + 'SIGNATURE' => urlencode($this->get_input_value('api_signature')), |
|
| 83 | + ); |
|
| 84 | + $gateway_url = $this->get_input_value('PMD_debug_mode') |
|
| 85 | + ? 'https://api-3t.sandbox.paypal.com/nvp' |
|
| 86 | + : 'https://api-3t.paypal.com/nvp'; |
|
| 87 | + // Request Customer Details. |
|
| 88 | + $response = wp_remote_post( |
|
| 89 | + $gateway_url, |
|
| 90 | + array( |
|
| 91 | + 'method' => 'POST', |
|
| 92 | + 'timeout' => 45, |
|
| 93 | + 'httpversion' => '1.1', |
|
| 94 | + 'cookies' => array(), |
|
| 95 | + 'headers' => array(), |
|
| 96 | + 'body' => http_build_query($request_params, '', '&'), |
|
| 97 | + ) |
|
| 98 | + ); |
|
| 99 | + if (is_wp_error($response) || empty($response['body'])) { |
|
| 100 | + // If we got here then there was an error in this request. |
|
| 101 | + // maybe is turned off. We don't know the credentials are invalid |
|
| 102 | + EE_Error::add_error( |
|
| 103 | + sprintf( |
|
| 104 | + // translators: %1$s Error message received from PayPal |
|
| 105 | + esc_html__( |
|
| 106 | + // @codingStandardsIgnoreStart |
|
| 107 | + 'Your PayPal credentials could not be verified. The following error occurred while communicating with PayPal: %1$s', |
|
| 108 | + // @codingStandardsIgnoreEnd |
|
| 109 | + 'event_espresso' |
|
| 110 | + ), |
|
| 111 | + $response->get_error_message() |
|
| 112 | + ), |
|
| 113 | + __FILE__, |
|
| 114 | + __FUNCTION__, |
|
| 115 | + __LINE__ |
|
| 116 | + ); |
|
| 117 | + } |
|
| 118 | + $response_args = array(); |
|
| 119 | + parse_str(urldecode($response['body']), $response_args); |
|
| 120 | 120 | |
| 121 | - if (empty($response_args['ACK'])) { |
|
| 122 | - EE_Error::add_error( |
|
| 123 | - esc_html__( |
|
| 124 | - 'Your PayPal credentials could not be verified. Part of their response was missing.', |
|
| 125 | - 'event_espresso' |
|
| 126 | - ), |
|
| 127 | - __FILE__, |
|
| 128 | - __FUNCTION__, |
|
| 129 | - __LINE__ |
|
| 130 | - ); |
|
| 131 | - } |
|
| 132 | - if (in_array( |
|
| 133 | - $response_args['ACK'], |
|
| 134 | - array( |
|
| 135 | - 'Success', |
|
| 136 | - 'SuccessWithWarning' |
|
| 137 | - ), |
|
| 138 | - true |
|
| 139 | - ) |
|
| 140 | - ) { |
|
| 141 | - return ''; |
|
| 142 | - } else { |
|
| 143 | - return sprintf( |
|
| 144 | - // translators: %1$s: PayPal response message, %2$s: PayPal response code |
|
| 145 | - esc_html__( |
|
| 146 | - // @codingStandardsIgnoreStart |
|
| 147 | - 'Your PayPal API credentials appear to be invalid. PayPal said "%1$s (%2$s)". Please see tips below.', |
|
| 148 | - // @codingStandardsIgnoreEnd |
|
| 149 | - 'event_espresso' |
|
| 150 | - ), |
|
| 151 | - isset($response_args['L_LONGMESSAGE0']) |
|
| 152 | - ? $response_args['L_LONGMESSAGE0'] |
|
| 153 | - : esc_html__('No error message received from PayPal', 'event_espresso'), |
|
| 154 | - isset($response_args['L_ERRORCODE0']) ? $response_args['L_ERRORCODE0'] : 0 |
|
| 155 | - ); |
|
| 156 | - } |
|
| 157 | - } |
|
| 121 | + if (empty($response_args['ACK'])) { |
|
| 122 | + EE_Error::add_error( |
|
| 123 | + esc_html__( |
|
| 124 | + 'Your PayPal credentials could not be verified. Part of their response was missing.', |
|
| 125 | + 'event_espresso' |
|
| 126 | + ), |
|
| 127 | + __FILE__, |
|
| 128 | + __FUNCTION__, |
|
| 129 | + __LINE__ |
|
| 130 | + ); |
|
| 131 | + } |
|
| 132 | + if (in_array( |
|
| 133 | + $response_args['ACK'], |
|
| 134 | + array( |
|
| 135 | + 'Success', |
|
| 136 | + 'SuccessWithWarning' |
|
| 137 | + ), |
|
| 138 | + true |
|
| 139 | + ) |
|
| 140 | + ) { |
|
| 141 | + return ''; |
|
| 142 | + } else { |
|
| 143 | + return sprintf( |
|
| 144 | + // translators: %1$s: PayPal response message, %2$s: PayPal response code |
|
| 145 | + esc_html__( |
|
| 146 | + // @codingStandardsIgnoreStart |
|
| 147 | + 'Your PayPal API credentials appear to be invalid. PayPal said "%1$s (%2$s)". Please see tips below.', |
|
| 148 | + // @codingStandardsIgnoreEnd |
|
| 149 | + 'event_espresso' |
|
| 150 | + ), |
|
| 151 | + isset($response_args['L_LONGMESSAGE0']) |
|
| 152 | + ? $response_args['L_LONGMESSAGE0'] |
|
| 153 | + : esc_html__('No error message received from PayPal', 'event_espresso'), |
|
| 154 | + isset($response_args['L_ERRORCODE0']) ? $response_args['L_ERRORCODE0'] : 0 |
|
| 155 | + ); |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * Gets the HTML to show the link to the help tab |
|
| 161 | - * @return string |
|
| 162 | - */ |
|
| 163 | - protected function helpTabLink() |
|
| 164 | - { |
|
| 165 | - return $this->helpTabLink; |
|
| 166 | - } |
|
| 159 | + /** |
|
| 160 | + * Gets the HTML to show the link to the help tab |
|
| 161 | + * @return string |
|
| 162 | + */ |
|
| 163 | + protected function helpTabLink() |
|
| 164 | + { |
|
| 165 | + return $this->helpTabLink; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * Does the normal validation, but also verifies the PayPal API credentials work. |
|
| 170 | - * If they don't, sets a validation error on the entire form, and adds validation errors (which are really more |
|
| 171 | - * tips) on each of the inputs that could be the cause of the problem. |
|
| 172 | - * @throws EE_Error |
|
| 173 | - */ |
|
| 174 | - public function _validate() |
|
| 175 | - { |
|
| 176 | - parent::_validate(); |
|
| 177 | - $credentials_message = $this->checkForCredentialsErrors(); |
|
| 178 | - if ($credentials_message !== '') { |
|
| 179 | - $this->add_validation_error($credentials_message); |
|
| 180 | - $this->get_input('PMD_debug_mode')->add_validation_error( |
|
| 181 | - esc_html__( |
|
| 182 | - // @codingStandardsIgnoreStart |
|
| 183 | - 'If you are using PayPal Sandbox (test) credentials, Debug mode should be set to "Yes". Otherwise, if you are using live PayPal credentials, set this to "No".', |
|
| 184 | - // @codingStandardsIgnoreEnd |
|
| 185 | - 'event_espresso' |
|
| 186 | - ) |
|
| 187 | - ); |
|
| 188 | - $this->get_input('api_username')->add_validation_error( |
|
| 189 | - sprintf( |
|
| 190 | - // translators: $1$s HTML for a link to the help tab |
|
| 191 | - esc_html__( |
|
| 192 | - 'Are you sure this is your API username, not your login username? %1$s', |
|
| 193 | - 'event_espresso' |
|
| 194 | - ), |
|
| 195 | - $this->helpTabLink() |
|
| 196 | - ) |
|
| 197 | - ); |
|
| 198 | - $this->get_input('api_password')->add_validation_error( |
|
| 199 | - sprintf( |
|
| 200 | - // translators: $1$s HTML for a link to the help tab |
|
| 201 | - esc_html__( |
|
| 202 | - 'Are you sure this is your API password, not your login password? %1$s', |
|
| 203 | - 'event_espresso' |
|
| 204 | - ), |
|
| 205 | - $this->helpTabLink() |
|
| 206 | - ) |
|
| 207 | - ); |
|
| 208 | - $this->get_input('api_signature')->add_validation_error( |
|
| 209 | - sprintf( |
|
| 210 | - // translators: $1$s HTML for a link to the help tab |
|
| 211 | - esc_html__('Please verify your API signature is correct. %1$s', 'event_espresso'), |
|
| 212 | - $this->helpTabLink() |
|
| 213 | - ) |
|
| 214 | - ); |
|
| 215 | - } |
|
| 216 | - } |
|
| 168 | + /** |
|
| 169 | + * Does the normal validation, but also verifies the PayPal API credentials work. |
|
| 170 | + * If they don't, sets a validation error on the entire form, and adds validation errors (which are really more |
|
| 171 | + * tips) on each of the inputs that could be the cause of the problem. |
|
| 172 | + * @throws EE_Error |
|
| 173 | + */ |
|
| 174 | + public function _validate() |
|
| 175 | + { |
|
| 176 | + parent::_validate(); |
|
| 177 | + $credentials_message = $this->checkForCredentialsErrors(); |
|
| 178 | + if ($credentials_message !== '') { |
|
| 179 | + $this->add_validation_error($credentials_message); |
|
| 180 | + $this->get_input('PMD_debug_mode')->add_validation_error( |
|
| 181 | + esc_html__( |
|
| 182 | + // @codingStandardsIgnoreStart |
|
| 183 | + 'If you are using PayPal Sandbox (test) credentials, Debug mode should be set to "Yes". Otherwise, if you are using live PayPal credentials, set this to "No".', |
|
| 184 | + // @codingStandardsIgnoreEnd |
|
| 185 | + 'event_espresso' |
|
| 186 | + ) |
|
| 187 | + ); |
|
| 188 | + $this->get_input('api_username')->add_validation_error( |
|
| 189 | + sprintf( |
|
| 190 | + // translators: $1$s HTML for a link to the help tab |
|
| 191 | + esc_html__( |
|
| 192 | + 'Are you sure this is your API username, not your login username? %1$s', |
|
| 193 | + 'event_espresso' |
|
| 194 | + ), |
|
| 195 | + $this->helpTabLink() |
|
| 196 | + ) |
|
| 197 | + ); |
|
| 198 | + $this->get_input('api_password')->add_validation_error( |
|
| 199 | + sprintf( |
|
| 200 | + // translators: $1$s HTML for a link to the help tab |
|
| 201 | + esc_html__( |
|
| 202 | + 'Are you sure this is your API password, not your login password? %1$s', |
|
| 203 | + 'event_espresso' |
|
| 204 | + ), |
|
| 205 | + $this->helpTabLink() |
|
| 206 | + ) |
|
| 207 | + ); |
|
| 208 | + $this->get_input('api_signature')->add_validation_error( |
|
| 209 | + sprintf( |
|
| 210 | + // translators: $1$s HTML for a link to the help tab |
|
| 211 | + esc_html__('Please verify your API signature is correct. %1$s', 'event_espresso'), |
|
| 212 | + $this->helpTabLink() |
|
| 213 | + ) |
|
| 214 | + ); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | 217 | } |
| 218 | 218 | // End of file PayPalSettingsForm.php |
| 219 | 219 | // Location: ${NAMESPACE}/PayPalSettingsForm.php |
@@ -17,30 +17,30 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class PayPalProSettingsForm extends PayPalSettingsForm |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * SettingsForm constructor. |
|
| 22 | - * |
|
| 23 | - * @param array $options_array |
|
| 24 | - * @param string $help_tab_link |
|
| 25 | - */ |
|
| 26 | - public function __construct(array $options_array = array(), $help_tab_link = '') |
|
| 27 | - { |
|
| 28 | - $options_array = array_replace_recursive( |
|
| 29 | - array( |
|
| 30 | - 'extra_meta_inputs' => array( |
|
| 31 | - 'credit_card_types' => new EE_Checkbox_Multi_Input( |
|
| 32 | - EE_PMT_Paypal_Pro::card_types_supported(), |
|
| 33 | - array( |
|
| 34 | - 'html_label_text' => __('Card Types Supported', 'event_espresso'), |
|
| 35 | - 'required' => true |
|
| 36 | - ) |
|
| 37 | - ), |
|
| 38 | - ) |
|
| 39 | - ), |
|
| 40 | - $options_array |
|
| 41 | - ); |
|
| 42 | - parent::__construct($options_array, $help_tab_link); |
|
| 43 | - } |
|
| 20 | + /** |
|
| 21 | + * SettingsForm constructor. |
|
| 22 | + * |
|
| 23 | + * @param array $options_array |
|
| 24 | + * @param string $help_tab_link |
|
| 25 | + */ |
|
| 26 | + public function __construct(array $options_array = array(), $help_tab_link = '') |
|
| 27 | + { |
|
| 28 | + $options_array = array_replace_recursive( |
|
| 29 | + array( |
|
| 30 | + 'extra_meta_inputs' => array( |
|
| 31 | + 'credit_card_types' => new EE_Checkbox_Multi_Input( |
|
| 32 | + EE_PMT_Paypal_Pro::card_types_supported(), |
|
| 33 | + array( |
|
| 34 | + 'html_label_text' => __('Card Types Supported', 'event_espresso'), |
|
| 35 | + 'required' => true |
|
| 36 | + ) |
|
| 37 | + ), |
|
| 38 | + ) |
|
| 39 | + ), |
|
| 40 | + $options_array |
|
| 41 | + ); |
|
| 42 | + parent::__construct($options_array, $help_tab_link); |
|
| 43 | + } |
|
| 44 | 44 | } |
| 45 | 45 | // End of file SettingsForm.php |
| 46 | 46 | // Location: EventEspresso/caffeinated/payment_methods/PayPal_Pro/forms/PayPalProSettingsForm.php |
@@ -19,53 +19,53 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class SettingsForm extends PayPalSettingsForm |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * SettingsForm constructor. |
|
| 24 | - * |
|
| 25 | - * @param array $options_array |
|
| 26 | - * @param string $help_tab_link |
|
| 27 | - * @throws InvalidDataTypeException |
|
| 28 | - * @throws InvalidInterfaceException |
|
| 29 | - * @throws InvalidArgumentException |
|
| 30 | - */ |
|
| 31 | - public function __construct(array $options_array = array(), $help_tab_link = '') |
|
| 32 | - { |
|
| 33 | - $options_array = array_replace_recursive( |
|
| 34 | - array( |
|
| 35 | - 'extra_meta_inputs' => array( |
|
| 36 | - 'request_shipping_addr' => new EE_Yes_No_Input( |
|
| 37 | - array( |
|
| 38 | - 'html_label_text' => sprintf( |
|
| 39 | - esc_html__('Request Shipping Address %s', 'event_espresso'), |
|
| 40 | - $help_tab_link |
|
| 41 | - ), |
|
| 42 | - 'html_help_text' => esc_html__( |
|
| 43 | - // @codingStandardsIgnoreStart |
|
| 44 | - 'If set to "Yes", then a shipping address will be requested on the PayPal checkout page.', |
|
| 45 | - // @codingStandardsIgnoreEnd |
|
| 46 | - 'event_espresso' |
|
| 47 | - ), |
|
| 48 | - 'required' => true, |
|
| 49 | - 'default' => false, |
|
| 50 | - ) |
|
| 51 | - ), |
|
| 52 | - 'image_url' => new EE_Admin_File_Uploader_Input( |
|
| 53 | - array( |
|
| 54 | - 'html_label_text' => sprintf( |
|
| 55 | - esc_html__('Image URL %s', 'event_espresso'), |
|
| 56 | - $help_tab_link |
|
| 57 | - ), |
|
| 58 | - 'html_help_text' => esc_html__( |
|
| 59 | - 'Used for your business/personal logo on the PayPal page', |
|
| 60 | - 'event_espresso' |
|
| 61 | - ), |
|
| 62 | - 'required' => false, |
|
| 63 | - ) |
|
| 64 | - ), |
|
| 65 | - ) |
|
| 66 | - ), |
|
| 67 | - $options_array |
|
| 68 | - ); |
|
| 69 | - parent::__construct($options_array, $help_tab_link); |
|
| 70 | - } |
|
| 22 | + /** |
|
| 23 | + * SettingsForm constructor. |
|
| 24 | + * |
|
| 25 | + * @param array $options_array |
|
| 26 | + * @param string $help_tab_link |
|
| 27 | + * @throws InvalidDataTypeException |
|
| 28 | + * @throws InvalidInterfaceException |
|
| 29 | + * @throws InvalidArgumentException |
|
| 30 | + */ |
|
| 31 | + public function __construct(array $options_array = array(), $help_tab_link = '') |
|
| 32 | + { |
|
| 33 | + $options_array = array_replace_recursive( |
|
| 34 | + array( |
|
| 35 | + 'extra_meta_inputs' => array( |
|
| 36 | + 'request_shipping_addr' => new EE_Yes_No_Input( |
|
| 37 | + array( |
|
| 38 | + 'html_label_text' => sprintf( |
|
| 39 | + esc_html__('Request Shipping Address %s', 'event_espresso'), |
|
| 40 | + $help_tab_link |
|
| 41 | + ), |
|
| 42 | + 'html_help_text' => esc_html__( |
|
| 43 | + // @codingStandardsIgnoreStart |
|
| 44 | + 'If set to "Yes", then a shipping address will be requested on the PayPal checkout page.', |
|
| 45 | + // @codingStandardsIgnoreEnd |
|
| 46 | + 'event_espresso' |
|
| 47 | + ), |
|
| 48 | + 'required' => true, |
|
| 49 | + 'default' => false, |
|
| 50 | + ) |
|
| 51 | + ), |
|
| 52 | + 'image_url' => new EE_Admin_File_Uploader_Input( |
|
| 53 | + array( |
|
| 54 | + 'html_label_text' => sprintf( |
|
| 55 | + esc_html__('Image URL %s', 'event_espresso'), |
|
| 56 | + $help_tab_link |
|
| 57 | + ), |
|
| 58 | + 'html_help_text' => esc_html__( |
|
| 59 | + 'Used for your business/personal logo on the PayPal page', |
|
| 60 | + 'event_espresso' |
|
| 61 | + ), |
|
| 62 | + 'required' => false, |
|
| 63 | + ) |
|
| 64 | + ), |
|
| 65 | + ) |
|
| 66 | + ), |
|
| 67 | + $options_array |
|
| 68 | + ); |
|
| 69 | + parent::__construct($options_array, $help_tab_link); |
|
| 70 | + } |
|
| 71 | 71 | } |
@@ -20,1689 +20,1689 @@ |
||
| 20 | 20 | class EEH_Line_Item |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - // other functions: cancel ticket purchase |
|
| 24 | - // delete ticket purchase |
|
| 25 | - // add promotion |
|
| 26 | - |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Adds a simple item (unrelated to any other model object) to the provided PARENT line item. |
|
| 30 | - * Does NOT automatically re-calculate the line item totals or update the related transaction. |
|
| 31 | - * You should call recalculate_total_including_taxes() on the grant total line item after this |
|
| 32 | - * to update the subtotals, and EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 33 | - * to keep the registration final prices in-sync with the transaction's total. |
|
| 34 | - * |
|
| 35 | - * @param EE_Line_Item $parent_line_item |
|
| 36 | - * @param string $name |
|
| 37 | - * @param float $unit_price |
|
| 38 | - * @param string $description |
|
| 39 | - * @param int $quantity |
|
| 40 | - * @param boolean $taxable |
|
| 41 | - * @param boolean $code if set to a value, ensures there is only one line item with that code |
|
| 42 | - * @return boolean success |
|
| 43 | - * @throws \EE_Error |
|
| 44 | - */ |
|
| 45 | - public static function add_unrelated_item(EE_Line_Item $parent_line_item, $name, $unit_price, $description = '', $quantity = 1, $taxable = false, $code = null) |
|
| 46 | - { |
|
| 47 | - $items_subtotal = self::get_pre_tax_subtotal($parent_line_item); |
|
| 48 | - $line_item = EE_Line_Item::new_instance(array( |
|
| 49 | - 'LIN_name' => $name, |
|
| 50 | - 'LIN_desc' => $description, |
|
| 51 | - 'LIN_unit_price' => $unit_price, |
|
| 52 | - 'LIN_quantity' => $quantity, |
|
| 53 | - 'LIN_percent' => null, |
|
| 54 | - 'LIN_is_taxable' => $taxable, |
|
| 55 | - 'LIN_order' => $items_subtotal instanceof EE_Line_Item ? count($items_subtotal->children()) : 0, |
|
| 56 | - 'LIN_total' => (float) $unit_price * (int) $quantity, |
|
| 57 | - 'LIN_type' => EEM_Line_Item::type_line_item, |
|
| 58 | - 'LIN_code' => $code, |
|
| 59 | - )); |
|
| 60 | - $line_item = apply_filters( |
|
| 61 | - 'FHEE__EEH_Line_Item__add_unrelated_item__line_item', |
|
| 62 | - $line_item, |
|
| 63 | - $parent_line_item |
|
| 64 | - ); |
|
| 65 | - return self::add_item($parent_line_item, $line_item); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Adds a simple item ( unrelated to any other model object) to the total line item, |
|
| 71 | - * in the correct spot in the line item tree. Automatically |
|
| 72 | - * re-calculates the line item totals and updates the related transaction. But |
|
| 73 | - * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
|
| 74 | - * should probably change because of this). |
|
| 75 | - * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 76 | - * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 77 | - * |
|
| 78 | - * @param EE_Line_Item $parent_line_item |
|
| 79 | - * @param string $name |
|
| 80 | - * @param float $percentage_amount |
|
| 81 | - * @param string $description |
|
| 82 | - * @param boolean $taxable |
|
| 83 | - * @return boolean success |
|
| 84 | - * @throws \EE_Error |
|
| 85 | - */ |
|
| 86 | - public static function add_percentage_based_item(EE_Line_Item $parent_line_item, $name, $percentage_amount, $description = '', $taxable = false) |
|
| 87 | - { |
|
| 88 | - $line_item = EE_Line_Item::new_instance(array( |
|
| 89 | - 'LIN_name' => $name, |
|
| 90 | - 'LIN_desc' => $description, |
|
| 91 | - 'LIN_unit_price' => 0, |
|
| 92 | - 'LIN_percent' => $percentage_amount, |
|
| 93 | - 'LIN_quantity' => 1, |
|
| 94 | - 'LIN_is_taxable' => $taxable, |
|
| 95 | - 'LIN_total' => (float) ($percentage_amount * ($parent_line_item->total() / 100)), |
|
| 96 | - 'LIN_type' => EEM_Line_Item::type_line_item, |
|
| 97 | - 'LIN_parent' => $parent_line_item->ID() |
|
| 98 | - )); |
|
| 99 | - $line_item = apply_filters( |
|
| 100 | - 'FHEE__EEH_Line_Item__add_percentage_based_item__line_item', |
|
| 101 | - $line_item |
|
| 102 | - ); |
|
| 103 | - return $parent_line_item->add_child_line_item($line_item, false); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Returns the new line item created by adding a purchase of the ticket |
|
| 109 | - * ensures that ticket line item is saved, and that cart total has been recalculated. |
|
| 110 | - * If this ticket has already been purchased, just increments its count. |
|
| 111 | - * Automatically re-calculates the line item totals and updates the related transaction. But |
|
| 112 | - * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
|
| 113 | - * should probably change because of this). |
|
| 114 | - * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 115 | - * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 116 | - * |
|
| 117 | - * @param EE_Line_Item $total_line_item grand total line item of type EEM_Line_Item::type_total |
|
| 118 | - * @param EE_Ticket $ticket |
|
| 119 | - * @param int $qty |
|
| 120 | - * @return \EE_Line_Item |
|
| 121 | - * @throws \EE_Error |
|
| 122 | - */ |
|
| 123 | - public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1) |
|
| 124 | - { |
|
| 125 | - if (!$total_line_item instanceof EE_Line_Item || !$total_line_item->is_total()) { |
|
| 126 | - throw new EE_Error(sprintf(__('A valid line item total is required in order to add tickets. A line item of type "%s" was passed.', 'event_espresso'), $ticket->ID(), $total_line_item->ID())); |
|
| 127 | - } |
|
| 128 | - // either increment the qty for an existing ticket |
|
| 129 | - $line_item = self::increment_ticket_qty_if_already_in_cart($total_line_item, $ticket, $qty); |
|
| 130 | - // or add a new one |
|
| 131 | - if (!$line_item instanceof EE_Line_Item) { |
|
| 132 | - $line_item = self::create_ticket_line_item($total_line_item, $ticket, $qty); |
|
| 133 | - } |
|
| 134 | - $total_line_item->recalculate_total_including_taxes(); |
|
| 135 | - return $line_item; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Returns the new line item created by adding a purchase of the ticket |
|
| 141 | - * @param \EE_Line_Item $total_line_item |
|
| 142 | - * @param EE_Ticket $ticket |
|
| 143 | - * @param int $qty |
|
| 144 | - * @return \EE_Line_Item |
|
| 145 | - * @throws \EE_Error |
|
| 146 | - */ |
|
| 147 | - public static function increment_ticket_qty_if_already_in_cart(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1) |
|
| 148 | - { |
|
| 149 | - $line_item = null; |
|
| 150 | - if ($total_line_item instanceof EE_Line_Item && $total_line_item->is_total()) { |
|
| 151 | - $ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item); |
|
| 152 | - foreach ((array) $ticket_line_items as $ticket_line_item) { |
|
| 153 | - if ($ticket_line_item instanceof EE_Line_Item |
|
| 154 | - && (int) $ticket_line_item->OBJ_ID() === (int) $ticket->ID() |
|
| 155 | - ) { |
|
| 156 | - $line_item = $ticket_line_item; |
|
| 157 | - break; |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - if ($line_item instanceof EE_Line_Item) { |
|
| 162 | - EEH_Line_Item::increment_quantity($line_item, $qty); |
|
| 163 | - return $line_item; |
|
| 164 | - } |
|
| 165 | - return null; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Increments the line item and all its children's quantity by $qty (but percent line items are unaffected). |
|
| 171 | - * Does NOT save or recalculate other line items totals |
|
| 172 | - * |
|
| 173 | - * @param EE_Line_Item $line_item |
|
| 174 | - * @param int $qty |
|
| 175 | - * @return void |
|
| 176 | - * @throws \EE_Error |
|
| 177 | - */ |
|
| 178 | - public static function increment_quantity(EE_Line_Item $line_item, $qty = 1) |
|
| 179 | - { |
|
| 180 | - if (!$line_item->is_percent()) { |
|
| 181 | - $qty += $line_item->quantity(); |
|
| 182 | - $line_item->set_quantity($qty); |
|
| 183 | - $line_item->set_total($line_item->unit_price() * $qty); |
|
| 184 | - $line_item->save(); |
|
| 185 | - } |
|
| 186 | - foreach ($line_item->children() as $child) { |
|
| 187 | - if ($child->is_sub_line_item()) { |
|
| 188 | - EEH_Line_Item::update_quantity($child, $qty); |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Decrements the line item and all its children's quantity by $qty (but percent line items are unaffected). |
|
| 196 | - * Does NOT save or recalculate other line items totals |
|
| 197 | - * |
|
| 198 | - * @param EE_Line_Item $line_item |
|
| 199 | - * @param int $qty |
|
| 200 | - * @return void |
|
| 201 | - * @throws \EE_Error |
|
| 202 | - */ |
|
| 203 | - public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1) |
|
| 204 | - { |
|
| 205 | - if (!$line_item->is_percent()) { |
|
| 206 | - $qty = $line_item->quantity() - $qty; |
|
| 207 | - $qty = max($qty, 0); |
|
| 208 | - $line_item->set_quantity($qty); |
|
| 209 | - $line_item->set_total($line_item->unit_price() * $qty); |
|
| 210 | - $line_item->save(); |
|
| 211 | - } |
|
| 212 | - foreach ($line_item->children() as $child) { |
|
| 213 | - if ($child->is_sub_line_item()) { |
|
| 214 | - EEH_Line_Item::update_quantity($child, $qty); |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Updates the line item and its children's quantities to the specified number. |
|
| 222 | - * Does NOT save them or recalculate totals. |
|
| 223 | - * |
|
| 224 | - * @param EE_Line_Item $line_item |
|
| 225 | - * @param int $new_quantity |
|
| 226 | - * @throws \EE_Error |
|
| 227 | - */ |
|
| 228 | - public static function update_quantity(EE_Line_Item $line_item, $new_quantity) |
|
| 229 | - { |
|
| 230 | - if (!$line_item->is_percent()) { |
|
| 231 | - $line_item->set_quantity($new_quantity); |
|
| 232 | - $line_item->set_total($line_item->unit_price() * $new_quantity); |
|
| 233 | - $line_item->save(); |
|
| 234 | - } |
|
| 235 | - foreach ($line_item->children() as $child) { |
|
| 236 | - if ($child->is_sub_line_item()) { |
|
| 237 | - EEH_Line_Item::update_quantity($child, $new_quantity); |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Returns the new line item created by adding a purchase of the ticket |
|
| 245 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 246 | - * @param EE_Ticket $ticket |
|
| 247 | - * @param int $qty |
|
| 248 | - * @return \EE_Line_Item |
|
| 249 | - * @throws \EE_Error |
|
| 250 | - */ |
|
| 251 | - public static function create_ticket_line_item(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1) |
|
| 252 | - { |
|
| 253 | - $datetimes = $ticket->datetimes(); |
|
| 254 | - $first_datetime = reset($datetimes); |
|
| 255 | - if ($first_datetime instanceof EE_Datetime && $first_datetime->event() instanceof EE_Event) { |
|
| 256 | - $first_datetime_name = $first_datetime->event()->name(); |
|
| 257 | - } else { |
|
| 258 | - $first_datetime_name = __('Event', 'event_espresso'); |
|
| 259 | - } |
|
| 260 | - $event = sprintf(_x('(For %1$s)', '(For Event Name)', 'event_espresso'), $first_datetime_name); |
|
| 261 | - // get event subtotal line |
|
| 262 | - $events_sub_total = self::get_event_line_item_for_ticket($total_line_item, $ticket); |
|
| 263 | - // add $ticket to cart |
|
| 264 | - $line_item = EE_Line_Item::new_instance(array( |
|
| 265 | - 'LIN_name' => $ticket->name(), |
|
| 266 | - 'LIN_desc' => $ticket->description() !== '' ? $ticket->description() . ' ' . $event : $event, |
|
| 267 | - 'LIN_unit_price' => $ticket->price(), |
|
| 268 | - 'LIN_quantity' => $qty, |
|
| 269 | - 'LIN_is_taxable' => $ticket->taxable(), |
|
| 270 | - 'LIN_order' => count($events_sub_total->children()), |
|
| 271 | - 'LIN_total' => $ticket->price() * $qty, |
|
| 272 | - 'LIN_type' => EEM_Line_Item::type_line_item, |
|
| 273 | - 'OBJ_ID' => $ticket->ID(), |
|
| 274 | - 'OBJ_type' => 'Ticket' |
|
| 275 | - )); |
|
| 276 | - $line_item = apply_filters( |
|
| 277 | - 'FHEE__EEH_Line_Item__create_ticket_line_item__line_item', |
|
| 278 | - $line_item |
|
| 279 | - ); |
|
| 280 | - $events_sub_total->add_child_line_item($line_item); |
|
| 281 | - // now add the sub-line items |
|
| 282 | - $running_total_for_ticket = 0; |
|
| 283 | - foreach ($ticket->prices(array('order_by' => array('PRC_order' => 'ASC'))) as $price) { |
|
| 284 | - $sign = $price->is_discount() ? -1 : 1; |
|
| 285 | - $price_total = $price->is_percent() |
|
| 286 | - ? $running_total_for_ticket * $price->amount() / 100 |
|
| 287 | - : $price->amount() * $qty; |
|
| 288 | - $sub_line_item = EE_Line_Item::new_instance(array( |
|
| 289 | - 'LIN_name' => $price->name(), |
|
| 290 | - 'LIN_desc' => $price->desc(), |
|
| 291 | - 'LIN_quantity' => $price->is_percent() ? null : $qty, |
|
| 292 | - 'LIN_is_taxable' => false, |
|
| 293 | - 'LIN_order' => $price->order(), |
|
| 294 | - 'LIN_total' => $sign * $price_total, |
|
| 295 | - 'LIN_type' => EEM_Line_Item::type_sub_line_item, |
|
| 296 | - 'OBJ_ID' => $price->ID(), |
|
| 297 | - 'OBJ_type' => 'Price' |
|
| 298 | - )); |
|
| 299 | - $sub_line_item = apply_filters( |
|
| 300 | - 'FHEE__EEH_Line_Item__create_ticket_line_item__sub_line_item', |
|
| 301 | - $sub_line_item |
|
| 302 | - ); |
|
| 303 | - if ($price->is_percent()) { |
|
| 304 | - $sub_line_item->set_percent($sign * $price->amount()); |
|
| 305 | - } else { |
|
| 306 | - $sub_line_item->set_unit_price($sign * $price->amount()); |
|
| 307 | - } |
|
| 308 | - $running_total_for_ticket += $price_total; |
|
| 309 | - $line_item->add_child_line_item($sub_line_item); |
|
| 310 | - } |
|
| 311 | - return $line_item; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Adds the specified item under the pre-tax-sub-total line item. Automatically |
|
| 317 | - * re-calculates the line item totals and updates the related transaction. But |
|
| 318 | - * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
|
| 319 | - * should probably change because of this). |
|
| 320 | - * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 321 | - * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 322 | - * |
|
| 323 | - * @param EE_Line_Item $total_line_item |
|
| 324 | - * @param EE_Line_Item $item to be added |
|
| 325 | - * @return boolean |
|
| 326 | - * @throws \EE_Error |
|
| 327 | - */ |
|
| 328 | - public static function add_item(EE_Line_Item $total_line_item, EE_Line_Item $item) |
|
| 329 | - { |
|
| 330 | - $pre_tax_subtotal = self::get_pre_tax_subtotal($total_line_item); |
|
| 331 | - if ($pre_tax_subtotal instanceof EE_Line_Item) { |
|
| 332 | - $success = $pre_tax_subtotal->add_child_line_item($item); |
|
| 333 | - } else { |
|
| 334 | - return false; |
|
| 335 | - } |
|
| 336 | - $total_line_item->recalculate_total_including_taxes(); |
|
| 337 | - return $success; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * cancels an existing ticket line item, |
|
| 343 | - * by decrementing it's quantity by 1 and adding a new "type_cancellation" sub-line-item. |
|
| 344 | - * ALL totals and subtotals will NEED TO BE UPDATED after performing this action |
|
| 345 | - * |
|
| 346 | - * @param EE_Line_Item $ticket_line_item |
|
| 347 | - * @param int $qty |
|
| 348 | - * @return bool success |
|
| 349 | - * @throws \EE_Error |
|
| 350 | - */ |
|
| 351 | - public static function cancel_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1) |
|
| 352 | - { |
|
| 353 | - // validate incoming line_item |
|
| 354 | - if ($ticket_line_item->OBJ_type() !== 'Ticket') { |
|
| 355 | - throw new EE_Error( |
|
| 356 | - sprintf( |
|
| 357 | - __('The supplied line item must have an Object Type of "Ticket", not %1$s.', 'event_espresso'), |
|
| 358 | - $ticket_line_item->type() |
|
| 359 | - ) |
|
| 360 | - ); |
|
| 361 | - } |
|
| 362 | - if ($ticket_line_item->quantity() < $qty) { |
|
| 363 | - throw new EE_Error( |
|
| 364 | - sprintf( |
|
| 365 | - __('Can not cancel %1$d ticket(s) because the supplied line item has a quantity of %2$d.', 'event_espresso'), |
|
| 366 | - $qty, |
|
| 367 | - $ticket_line_item->quantity() |
|
| 368 | - ) |
|
| 369 | - ); |
|
| 370 | - } |
|
| 371 | - // decrement ticket quantity; don't rely on auto-fixing when recalculating totals to do this |
|
| 372 | - $ticket_line_item->set_quantity($ticket_line_item->quantity() - $qty); |
|
| 373 | - foreach ($ticket_line_item->children() as $child_line_item) { |
|
| 374 | - if ($child_line_item->is_sub_line_item() |
|
| 375 | - && !$child_line_item->is_percent() |
|
| 376 | - && !$child_line_item->is_cancellation() |
|
| 377 | - ) { |
|
| 378 | - $child_line_item->set_quantity($child_line_item->quantity() - $qty); |
|
| 379 | - } |
|
| 380 | - } |
|
| 381 | - // get cancellation sub line item |
|
| 382 | - $cancellation_line_item = EEH_Line_Item::get_descendants_of_type( |
|
| 383 | - $ticket_line_item, |
|
| 384 | - EEM_Line_Item::type_cancellation |
|
| 385 | - ); |
|
| 386 | - $cancellation_line_item = reset($cancellation_line_item); |
|
| 387 | - // verify that this ticket was indeed previously cancelled |
|
| 388 | - if ($cancellation_line_item instanceof EE_Line_Item) { |
|
| 389 | - // increment cancelled quantity |
|
| 390 | - $cancellation_line_item->set_quantity($cancellation_line_item->quantity() + $qty); |
|
| 391 | - } else { |
|
| 392 | - // create cancellation sub line item |
|
| 393 | - $cancellation_line_item = EE_Line_Item::new_instance(array( |
|
| 394 | - 'LIN_name' => __('Cancellation', 'event_espresso'), |
|
| 395 | - 'LIN_desc' => sprintf( |
|
| 396 | - _x('Cancelled %1$s : %2$s', 'Cancelled Ticket Name : 2015-01-01 11:11', 'event_espresso'), |
|
| 397 | - $ticket_line_item->name(), |
|
| 398 | - current_time(get_option('date_format') . ' ' . get_option('time_format')) |
|
| 399 | - ), |
|
| 400 | - 'LIN_unit_price' => 0, // $ticket_line_item->unit_price() |
|
| 401 | - 'LIN_quantity' => $qty, |
|
| 402 | - 'LIN_is_taxable' => $ticket_line_item->is_taxable(), |
|
| 403 | - 'LIN_order' => count($ticket_line_item->children()), |
|
| 404 | - 'LIN_total' => 0, // $ticket_line_item->unit_price() |
|
| 405 | - 'LIN_type' => EEM_Line_Item::type_cancellation, |
|
| 406 | - )); |
|
| 407 | - $ticket_line_item->add_child_line_item($cancellation_line_item); |
|
| 408 | - } |
|
| 409 | - if ($ticket_line_item->save_this_and_descendants() > 0) { |
|
| 410 | - // decrement parent line item quantity |
|
| 411 | - $event_line_item = $ticket_line_item->parent(); |
|
| 412 | - if ($event_line_item instanceof EE_Line_Item && $event_line_item->OBJ_type() === 'Event') { |
|
| 413 | - $event_line_item->set_quantity($event_line_item->quantity() - $qty); |
|
| 414 | - $event_line_item->save(); |
|
| 415 | - } |
|
| 416 | - EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item); |
|
| 417 | - return true; |
|
| 418 | - } |
|
| 419 | - return false; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * reinstates (un-cancels?) a previously canceled ticket line item, |
|
| 425 | - * by incrementing it's quantity by 1, and decrementing it's "type_cancellation" sub-line-item. |
|
| 426 | - * ALL totals and subtotals will NEED TO BE UPDATED after performing this action |
|
| 427 | - * |
|
| 428 | - * @param EE_Line_Item $ticket_line_item |
|
| 429 | - * @param int $qty |
|
| 430 | - * @return bool success |
|
| 431 | - * @throws \EE_Error |
|
| 432 | - */ |
|
| 433 | - public static function reinstate_canceled_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1) |
|
| 434 | - { |
|
| 435 | - // validate incoming line_item |
|
| 436 | - if ($ticket_line_item->OBJ_type() !== 'Ticket') { |
|
| 437 | - throw new EE_Error( |
|
| 438 | - sprintf( |
|
| 439 | - __('The supplied line item must have an Object Type of "Ticket", not %1$s.', 'event_espresso'), |
|
| 440 | - $ticket_line_item->type() |
|
| 441 | - ) |
|
| 442 | - ); |
|
| 443 | - } |
|
| 444 | - // get cancellation sub line item |
|
| 445 | - $cancellation_line_item = EEH_Line_Item::get_descendants_of_type( |
|
| 446 | - $ticket_line_item, |
|
| 447 | - EEM_Line_Item::type_cancellation |
|
| 448 | - ); |
|
| 449 | - $cancellation_line_item = reset($cancellation_line_item); |
|
| 450 | - // verify that this ticket was indeed previously cancelled |
|
| 451 | - if (!$cancellation_line_item instanceof EE_Line_Item) { |
|
| 452 | - return false; |
|
| 453 | - } |
|
| 454 | - if ($cancellation_line_item->quantity() > $qty) { |
|
| 455 | - // decrement cancelled quantity |
|
| 456 | - $cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty); |
|
| 457 | - } elseif ($cancellation_line_item->quantity() == $qty) { |
|
| 458 | - // decrement cancelled quantity in case anyone still has the object kicking around |
|
| 459 | - $cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty); |
|
| 460 | - // delete because quantity will end up as 0 |
|
| 461 | - $cancellation_line_item->delete(); |
|
| 462 | - // and attempt to destroy the object, |
|
| 463 | - // even though PHP won't actually destroy it until it needs the memory |
|
| 464 | - unset($cancellation_line_item); |
|
| 465 | - } else { |
|
| 466 | - // what ?!?! negative quantity ?!?! |
|
| 467 | - throw new EE_Error( |
|
| 468 | - sprintf( |
|
| 469 | - __( |
|
| 470 | - 'Can not reinstate %1$d cancelled ticket(s) because the cancelled ticket quantity is only %2$d.', |
|
| 471 | - 'event_espresso' |
|
| 472 | - ), |
|
| 473 | - $qty, |
|
| 474 | - $cancellation_line_item->quantity() |
|
| 475 | - ) |
|
| 476 | - ); |
|
| 477 | - } |
|
| 478 | - // increment ticket quantity |
|
| 479 | - $ticket_line_item->set_quantity($ticket_line_item->quantity() + $qty); |
|
| 480 | - if ($ticket_line_item->save_this_and_descendants() > 0) { |
|
| 481 | - // increment parent line item quantity |
|
| 482 | - $event_line_item = $ticket_line_item->parent(); |
|
| 483 | - if ($event_line_item instanceof EE_Line_Item && $event_line_item->OBJ_type() === 'Event') { |
|
| 484 | - $event_line_item->set_quantity($event_line_item->quantity() + $qty); |
|
| 485 | - } |
|
| 486 | - EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item); |
|
| 487 | - return true; |
|
| 488 | - } |
|
| 489 | - return false; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - |
|
| 493 | - /** |
|
| 494 | - * calls EEH_Line_Item::find_transaction_grand_total_for_line_item() |
|
| 495 | - * then EE_Line_Item::recalculate_total_including_taxes() on the result |
|
| 496 | - * |
|
| 497 | - * @param EE_Line_Item $line_item |
|
| 498 | - * @return \EE_Line_Item |
|
| 499 | - */ |
|
| 500 | - public static function get_grand_total_and_recalculate_everything(EE_Line_Item $line_item) |
|
| 501 | - { |
|
| 502 | - $grand_total_line_item = EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item); |
|
| 503 | - return $grand_total_line_item->recalculate_total_including_taxes(); |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * Gets the line item which contains the subtotal of all the items |
|
| 509 | - * |
|
| 510 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 511 | - * @return \EE_Line_Item |
|
| 512 | - * @throws \EE_Error |
|
| 513 | - */ |
|
| 514 | - public static function get_pre_tax_subtotal(EE_Line_Item $total_line_item) |
|
| 515 | - { |
|
| 516 | - $pre_tax_subtotal = $total_line_item->get_child_line_item('pre-tax-subtotal'); |
|
| 517 | - return $pre_tax_subtotal instanceof EE_Line_Item |
|
| 518 | - ? $pre_tax_subtotal |
|
| 519 | - : self::create_pre_tax_subtotal($total_line_item); |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Gets the line item for the taxes subtotal |
|
| 525 | - * |
|
| 526 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 527 | - * @return \EE_Line_Item |
|
| 528 | - * @throws \EE_Error |
|
| 529 | - */ |
|
| 530 | - public static function get_taxes_subtotal(EE_Line_Item $total_line_item) |
|
| 531 | - { |
|
| 532 | - $taxes = $total_line_item->get_child_line_item('taxes'); |
|
| 533 | - return $taxes ? $taxes : self::create_taxes_subtotal($total_line_item); |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - |
|
| 537 | - /** |
|
| 538 | - * sets the TXN ID on an EE_Line_Item if passed a valid EE_Transaction object |
|
| 539 | - * |
|
| 540 | - * @param EE_Line_Item $line_item |
|
| 541 | - * @param EE_Transaction $transaction |
|
| 542 | - * @return void |
|
| 543 | - * @throws \EE_Error |
|
| 544 | - */ |
|
| 545 | - public static function set_TXN_ID(EE_Line_Item $line_item, $transaction = null) |
|
| 546 | - { |
|
| 547 | - if ($transaction) { |
|
| 548 | - /** @type EEM_Transaction $EEM_Transaction */ |
|
| 549 | - $EEM_Transaction = EE_Registry::instance()->load_model('Transaction'); |
|
| 550 | - $TXN_ID = $EEM_Transaction->ensure_is_ID($transaction); |
|
| 551 | - $line_item->set_TXN_ID($TXN_ID); |
|
| 552 | - } |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * Creates a new default total line item for the transaction, |
|
| 558 | - * and its tickets subtotal and taxes subtotal line items (and adds the |
|
| 559 | - * existing taxes as children of the taxes subtotal line item) |
|
| 560 | - * |
|
| 561 | - * @param EE_Transaction $transaction |
|
| 562 | - * @return \EE_Line_Item of type total |
|
| 563 | - * @throws \EE_Error |
|
| 564 | - */ |
|
| 565 | - public static function create_total_line_item($transaction = null) |
|
| 566 | - { |
|
| 567 | - $total_line_item = EE_Line_Item::new_instance(array( |
|
| 568 | - 'LIN_code' => 'total', |
|
| 569 | - 'LIN_name' => __('Grand Total', 'event_espresso'), |
|
| 570 | - 'LIN_type' => EEM_Line_Item::type_total, |
|
| 571 | - 'OBJ_type' => 'Transaction' |
|
| 572 | - )); |
|
| 573 | - $total_line_item = apply_filters( |
|
| 574 | - 'FHEE__EEH_Line_Item__create_total_line_item__total_line_item', |
|
| 575 | - $total_line_item |
|
| 576 | - ); |
|
| 577 | - self::set_TXN_ID($total_line_item, $transaction); |
|
| 578 | - self::create_pre_tax_subtotal($total_line_item, $transaction); |
|
| 579 | - self::create_taxes_subtotal($total_line_item, $transaction); |
|
| 580 | - return $total_line_item; |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - |
|
| 584 | - /** |
|
| 585 | - * Creates a default items subtotal line item |
|
| 586 | - * |
|
| 587 | - * @param EE_Line_Item $total_line_item |
|
| 588 | - * @param EE_Transaction $transaction |
|
| 589 | - * @return EE_Line_Item |
|
| 590 | - * @throws \EE_Error |
|
| 591 | - */ |
|
| 592 | - protected static function create_pre_tax_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 593 | - { |
|
| 594 | - $pre_tax_line_item = EE_Line_Item::new_instance(array( |
|
| 595 | - 'LIN_code' => 'pre-tax-subtotal', |
|
| 596 | - 'LIN_name' => __('Pre-Tax Subtotal', 'event_espresso'), |
|
| 597 | - 'LIN_type' => EEM_Line_Item::type_sub_total |
|
| 598 | - )); |
|
| 599 | - $pre_tax_line_item = apply_filters( |
|
| 600 | - 'FHEE__EEH_Line_Item__create_pre_tax_subtotal__pre_tax_line_item', |
|
| 601 | - $pre_tax_line_item |
|
| 602 | - ); |
|
| 603 | - self::set_TXN_ID($pre_tax_line_item, $transaction); |
|
| 604 | - $total_line_item->add_child_line_item($pre_tax_line_item); |
|
| 605 | - self::create_event_subtotal($pre_tax_line_item, $transaction); |
|
| 606 | - return $pre_tax_line_item; |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * Creates a line item for the taxes subtotal and finds all the tax prices |
|
| 612 | - * and applies taxes to it |
|
| 613 | - * |
|
| 614 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 615 | - * @param EE_Transaction $transaction |
|
| 616 | - * @return EE_Line_Item |
|
| 617 | - * @throws \EE_Error |
|
| 618 | - */ |
|
| 619 | - protected static function create_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 620 | - { |
|
| 621 | - $tax_line_item = EE_Line_Item::new_instance(array( |
|
| 622 | - 'LIN_code' => 'taxes', |
|
| 623 | - 'LIN_name' => __('Taxes', 'event_espresso'), |
|
| 624 | - 'LIN_type' => EEM_Line_Item::type_tax_sub_total, |
|
| 625 | - 'LIN_order' => 1000,// this should always come last |
|
| 626 | - )); |
|
| 627 | - $tax_line_item = apply_filters( |
|
| 628 | - 'FHEE__EEH_Line_Item__create_taxes_subtotal__tax_line_item', |
|
| 629 | - $tax_line_item |
|
| 630 | - ); |
|
| 631 | - self::set_TXN_ID($tax_line_item, $transaction); |
|
| 632 | - $total_line_item->add_child_line_item($tax_line_item); |
|
| 633 | - // and lastly, add the actual taxes |
|
| 634 | - self::apply_taxes($total_line_item); |
|
| 635 | - return $tax_line_item; |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * Creates a default items subtotal line item |
|
| 641 | - * |
|
| 642 | - * @param EE_Line_Item $pre_tax_line_item |
|
| 643 | - * @param EE_Transaction $transaction |
|
| 644 | - * @param EE_Event $event |
|
| 645 | - * @return EE_Line_Item |
|
| 646 | - * @throws \EE_Error |
|
| 647 | - */ |
|
| 648 | - public static function create_event_subtotal(EE_Line_Item $pre_tax_line_item, $transaction = null, $event = null) |
|
| 649 | - { |
|
| 650 | - $event_line_item = EE_Line_Item::new_instance(array( |
|
| 651 | - 'LIN_code' => self::get_event_code($event), |
|
| 652 | - 'LIN_name' => self::get_event_name($event), |
|
| 653 | - 'LIN_desc' => self::get_event_desc($event), |
|
| 654 | - 'LIN_type' => EEM_Line_Item::type_sub_total, |
|
| 655 | - 'OBJ_type' => 'Event', |
|
| 656 | - 'OBJ_ID' => $event instanceof EE_Event ? $event->ID() : 0 |
|
| 657 | - )); |
|
| 658 | - $event_line_item = apply_filters( |
|
| 659 | - 'FHEE__EEH_Line_Item__create_event_subtotal__event_line_item', |
|
| 660 | - $event_line_item |
|
| 661 | - ); |
|
| 662 | - self::set_TXN_ID($event_line_item, $transaction); |
|
| 663 | - $pre_tax_line_item->add_child_line_item($event_line_item); |
|
| 664 | - return $event_line_item; |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - |
|
| 668 | - /** |
|
| 669 | - * Gets what the event ticket's code SHOULD be |
|
| 670 | - * |
|
| 671 | - * @param EE_Event $event |
|
| 672 | - * @return string |
|
| 673 | - * @throws \EE_Error |
|
| 674 | - */ |
|
| 675 | - public static function get_event_code($event) |
|
| 676 | - { |
|
| 677 | - return 'event-' . ($event instanceof EE_Event ? $event->ID() : '0'); |
|
| 678 | - } |
|
| 679 | - |
|
| 680 | - /** |
|
| 681 | - * Gets the event name |
|
| 682 | - * @param EE_Event $event |
|
| 683 | - * @return string |
|
| 684 | - */ |
|
| 685 | - public static function get_event_name($event) |
|
| 686 | - { |
|
| 687 | - return $event instanceof EE_Event ? mb_substr($event->name(), 0, 245) : __('Event', 'event_espresso'); |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * Gets the event excerpt |
|
| 692 | - * @param EE_Event $event |
|
| 693 | - * @return string |
|
| 694 | - */ |
|
| 695 | - public static function get_event_desc($event) |
|
| 696 | - { |
|
| 697 | - return $event instanceof EE_Event ? $event->short_description() : ''; |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - /** |
|
| 701 | - * Given the grand total line item and a ticket, finds the event sub-total |
|
| 702 | - * line item the ticket's purchase should be added onto |
|
| 703 | - * |
|
| 704 | - * @access public |
|
| 705 | - * @param EE_Line_Item $grand_total the grand total line item |
|
| 706 | - * @param EE_Ticket $ticket |
|
| 707 | - * @throws \EE_Error |
|
| 708 | - * @return EE_Line_Item |
|
| 709 | - */ |
|
| 710 | - public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket) |
|
| 711 | - { |
|
| 712 | - $first_datetime = $ticket->first_datetime(); |
|
| 713 | - if (!$first_datetime instanceof EE_Datetime) { |
|
| 714 | - throw new EE_Error( |
|
| 715 | - sprintf(__('The supplied ticket (ID %d) has no datetimes', 'event_espresso'), $ticket->ID()) |
|
| 716 | - ); |
|
| 717 | - } |
|
| 718 | - $event = $first_datetime->event(); |
|
| 719 | - if (!$event instanceof EE_Event) { |
|
| 720 | - throw new EE_Error( |
|
| 721 | - sprintf( |
|
| 722 | - __('The supplied ticket (ID %d) has no event data associated with it.', 'event_espresso'), |
|
| 723 | - $ticket->ID() |
|
| 724 | - ) |
|
| 725 | - ); |
|
| 726 | - } |
|
| 727 | - $events_sub_total = EEH_Line_Item::get_event_line_item($grand_total, $event); |
|
| 728 | - if (!$events_sub_total instanceof EE_Line_Item) { |
|
| 729 | - throw new EE_Error( |
|
| 730 | - sprintf( |
|
| 731 | - __('There is no events sub-total for ticket %s on total line item %d', 'event_espresso'), |
|
| 732 | - $ticket->ID(), |
|
| 733 | - $grand_total->ID() |
|
| 734 | - ) |
|
| 735 | - ); |
|
| 736 | - } |
|
| 737 | - return $events_sub_total; |
|
| 738 | - } |
|
| 739 | - |
|
| 740 | - |
|
| 741 | - /** |
|
| 742 | - * Gets the event line item |
|
| 743 | - * |
|
| 744 | - * @param EE_Line_Item $grand_total |
|
| 745 | - * @param EE_Event $event |
|
| 746 | - * @return EE_Line_Item for the event subtotal which is a child of $grand_total |
|
| 747 | - * @throws \EE_Error |
|
| 748 | - */ |
|
| 749 | - public static function get_event_line_item(EE_Line_Item $grand_total, $event) |
|
| 750 | - { |
|
| 751 | - /** @type EE_Event $event */ |
|
| 752 | - $event = EEM_Event::instance()->ensure_is_obj($event, true); |
|
| 753 | - $event_line_item = null; |
|
| 754 | - $found = false; |
|
| 755 | - foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) { |
|
| 756 | - // default event subtotal, we should only ever find this the first time this method is called |
|
| 757 | - if (!$event_line_item->OBJ_ID()) { |
|
| 758 | - // let's use this! but first... set the event details |
|
| 759 | - EEH_Line_Item::set_event_subtotal_details($event_line_item, $event); |
|
| 760 | - $found = true; |
|
| 761 | - break; |
|
| 762 | - } elseif ($event_line_item->OBJ_ID() === $event->ID()) { |
|
| 763 | - // found existing line item for this event in the cart, so break out of loop and use this one |
|
| 764 | - $found = true; |
|
| 765 | - break; |
|
| 766 | - } |
|
| 767 | - } |
|
| 768 | - if (!$found) { |
|
| 769 | - // there is no event sub-total yet, so add it |
|
| 770 | - $pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total); |
|
| 771 | - // create a new "event" subtotal below that |
|
| 772 | - $event_line_item = EEH_Line_Item::create_event_subtotal($pre_tax_subtotal, null, $event); |
|
| 773 | - // and set the event details |
|
| 774 | - EEH_Line_Item::set_event_subtotal_details($event_line_item, $event); |
|
| 775 | - } |
|
| 776 | - return $event_line_item; |
|
| 777 | - } |
|
| 778 | - |
|
| 779 | - |
|
| 780 | - /** |
|
| 781 | - * Creates a default items subtotal line item |
|
| 782 | - * |
|
| 783 | - * @param EE_Line_Item $event_line_item |
|
| 784 | - * @param EE_Event $event |
|
| 785 | - * @param EE_Transaction $transaction |
|
| 786 | - * @return EE_Line_Item |
|
| 787 | - * @throws \EE_Error |
|
| 788 | - */ |
|
| 789 | - public static function set_event_subtotal_details( |
|
| 790 | - EE_Line_Item $event_line_item, |
|
| 791 | - EE_Event $event, |
|
| 792 | - $transaction = null |
|
| 793 | - ) { |
|
| 794 | - if ($event instanceof EE_Event) { |
|
| 795 | - $event_line_item->set_code(self::get_event_code($event)); |
|
| 796 | - $event_line_item->set_name(self::get_event_name($event)); |
|
| 797 | - $event_line_item->set_desc(self::get_event_desc($event)); |
|
| 798 | - $event_line_item->set_OBJ_ID($event->ID()); |
|
| 799 | - } |
|
| 800 | - self::set_TXN_ID($event_line_item, $transaction); |
|
| 801 | - } |
|
| 802 | - |
|
| 803 | - |
|
| 804 | - /** |
|
| 805 | - * Finds what taxes should apply, adds them as tax line items under the taxes sub-total, |
|
| 806 | - * and recalculates the taxes sub-total and the grand total. Resets the taxes, so |
|
| 807 | - * any old taxes are removed |
|
| 808 | - * |
|
| 809 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 810 | - * @throws \EE_Error |
|
| 811 | - */ |
|
| 812 | - public static function apply_taxes(EE_Line_Item $total_line_item) |
|
| 813 | - { |
|
| 814 | - /** @type EEM_Price $EEM_Price */ |
|
| 815 | - $EEM_Price = EE_Registry::instance()->load_model('Price'); |
|
| 816 | - // get array of taxes via Price Model |
|
| 817 | - $ordered_taxes = $EEM_Price->get_all_prices_that_are_taxes(); |
|
| 818 | - ksort($ordered_taxes); |
|
| 819 | - $taxes_line_item = self::get_taxes_subtotal($total_line_item); |
|
| 820 | - // just to be safe, remove its old tax line items |
|
| 821 | - $taxes_line_item->delete_children_line_items(); |
|
| 822 | - // loop thru taxes |
|
| 823 | - foreach ($ordered_taxes as $order => $taxes) { |
|
| 824 | - foreach ($taxes as $tax) { |
|
| 825 | - if ($tax instanceof EE_Price) { |
|
| 826 | - $tax_line_item = EE_Line_Item::new_instance( |
|
| 827 | - array( |
|
| 828 | - 'LIN_name' => $tax->name(), |
|
| 829 | - 'LIN_desc' => $tax->desc(), |
|
| 830 | - 'LIN_percent' => $tax->amount(), |
|
| 831 | - 'LIN_is_taxable' => false, |
|
| 832 | - 'LIN_order' => $order, |
|
| 833 | - 'LIN_total' => 0, |
|
| 834 | - 'LIN_type' => EEM_Line_Item::type_tax, |
|
| 835 | - 'OBJ_type' => 'Price', |
|
| 836 | - 'OBJ_ID' => $tax->ID() |
|
| 837 | - ) |
|
| 838 | - ); |
|
| 839 | - $tax_line_item = apply_filters( |
|
| 840 | - 'FHEE__EEH_Line_Item__apply_taxes__tax_line_item', |
|
| 841 | - $tax_line_item |
|
| 842 | - ); |
|
| 843 | - $taxes_line_item->add_child_line_item($tax_line_item); |
|
| 844 | - } |
|
| 845 | - } |
|
| 846 | - } |
|
| 847 | - $total_line_item->recalculate_total_including_taxes(); |
|
| 848 | - } |
|
| 849 | - |
|
| 850 | - |
|
| 851 | - /** |
|
| 852 | - * Ensures that taxes have been applied to the order, if not applies them. |
|
| 853 | - * Returns the total amount of tax |
|
| 854 | - * |
|
| 855 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 856 | - * @return float |
|
| 857 | - * @throws \EE_Error |
|
| 858 | - */ |
|
| 859 | - public static function ensure_taxes_applied($total_line_item) |
|
| 860 | - { |
|
| 861 | - $taxes_subtotal = self::get_taxes_subtotal($total_line_item); |
|
| 862 | - if (!$taxes_subtotal->children()) { |
|
| 863 | - self::apply_taxes($total_line_item); |
|
| 864 | - } |
|
| 865 | - return $taxes_subtotal->total(); |
|
| 866 | - } |
|
| 867 | - |
|
| 868 | - |
|
| 869 | - /** |
|
| 870 | - * Deletes ALL children of the passed line item |
|
| 871 | - * |
|
| 872 | - * @param EE_Line_Item $parent_line_item |
|
| 873 | - * @return bool |
|
| 874 | - * @throws \EE_Error |
|
| 875 | - */ |
|
| 876 | - public static function delete_all_child_items(EE_Line_Item $parent_line_item) |
|
| 877 | - { |
|
| 878 | - $deleted = 0; |
|
| 879 | - foreach ($parent_line_item->children() as $child_line_item) { |
|
| 880 | - if ($child_line_item instanceof EE_Line_Item) { |
|
| 881 | - $deleted += EEH_Line_Item::delete_all_child_items($child_line_item); |
|
| 882 | - if ($child_line_item->ID()) { |
|
| 883 | - $child_line_item->delete(); |
|
| 884 | - unset($child_line_item); |
|
| 885 | - } else { |
|
| 886 | - $parent_line_item->delete_child_line_item($child_line_item->code()); |
|
| 887 | - } |
|
| 888 | - $deleted++; |
|
| 889 | - } |
|
| 890 | - } |
|
| 891 | - return $deleted; |
|
| 892 | - } |
|
| 893 | - |
|
| 894 | - |
|
| 895 | - /** |
|
| 896 | - * Deletes the line items as indicated by the line item code(s) provided, |
|
| 897 | - * regardless of where they're found in the line item tree. Automatically |
|
| 898 | - * re-calculates the line item totals and updates the related transaction. But |
|
| 899 | - * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
|
| 900 | - * should probably change because of this). |
|
| 901 | - * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 902 | - * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 903 | - * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 904 | - * @param array|bool|string $line_item_codes |
|
| 905 | - * @return int number of items successfully removed |
|
| 906 | - */ |
|
| 907 | - public static function delete_items(EE_Line_Item $total_line_item, $line_item_codes = false) |
|
| 908 | - { |
|
| 909 | - |
|
| 910 | - if ($total_line_item->type() !== EEM_Line_Item::type_total) { |
|
| 911 | - EE_Error::doing_it_wrong( |
|
| 912 | - 'EEH_Line_Item::delete_items', |
|
| 913 | - __( |
|
| 914 | - 'This static method should only be called with a TOTAL line item, otherwise we won\'t recalculate the totals correctly', |
|
| 915 | - 'event_espresso' |
|
| 916 | - ), |
|
| 917 | - '4.6.18' |
|
| 918 | - ); |
|
| 919 | - } |
|
| 920 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 921 | - |
|
| 922 | - // check if only a single line_item_id was passed |
|
| 923 | - if (!empty($line_item_codes) && !is_array($line_item_codes)) { |
|
| 924 | - // place single line_item_id in an array to appear as multiple line_item_ids |
|
| 925 | - $line_item_codes = array($line_item_codes); |
|
| 926 | - } |
|
| 927 | - $removals = 0; |
|
| 928 | - // cycle thru line_item_ids |
|
| 929 | - foreach ($line_item_codes as $line_item_id) { |
|
| 930 | - $removals += $total_line_item->delete_child_line_item($line_item_id); |
|
| 931 | - } |
|
| 932 | - |
|
| 933 | - if ($removals > 0) { |
|
| 934 | - $total_line_item->recalculate_taxes_and_tax_total(); |
|
| 935 | - return $removals; |
|
| 936 | - } else { |
|
| 937 | - return false; |
|
| 938 | - } |
|
| 939 | - } |
|
| 940 | - |
|
| 941 | - |
|
| 942 | - /** |
|
| 943 | - * Overwrites the previous tax by clearing out the old taxes, and creates a new |
|
| 944 | - * tax and updates the total line item accordingly |
|
| 945 | - * |
|
| 946 | - * @param EE_Line_Item $total_line_item |
|
| 947 | - * @param float $amount |
|
| 948 | - * @param string $name |
|
| 949 | - * @param string $description |
|
| 950 | - * @param string $code |
|
| 951 | - * @param boolean $add_to_existing_line_item |
|
| 952 | - * if true, and a duplicate line item with the same code is found, |
|
| 953 | - * $amount will be added onto it; otherwise will simply set the taxes to match $amount |
|
| 954 | - * @return EE_Line_Item the new tax line item created |
|
| 955 | - * @throws \EE_Error |
|
| 956 | - */ |
|
| 957 | - public static function set_total_tax_to( |
|
| 958 | - EE_Line_Item $total_line_item, |
|
| 959 | - $amount, |
|
| 960 | - $name = null, |
|
| 961 | - $description = null, |
|
| 962 | - $code = null, |
|
| 963 | - $add_to_existing_line_item = false |
|
| 964 | - ) { |
|
| 965 | - $tax_subtotal = self::get_taxes_subtotal($total_line_item); |
|
| 966 | - $taxable_total = $total_line_item->taxable_total(); |
|
| 967 | - |
|
| 968 | - if ($add_to_existing_line_item) { |
|
| 969 | - $new_tax = $tax_subtotal->get_child_line_item($code); |
|
| 970 | - EEM_Line_Item::instance()->delete( |
|
| 971 | - array(array('LIN_code' => array('!=', $code), 'LIN_parent' => $tax_subtotal->ID())) |
|
| 972 | - ); |
|
| 973 | - } else { |
|
| 974 | - $new_tax = null; |
|
| 975 | - $tax_subtotal->delete_children_line_items(); |
|
| 976 | - } |
|
| 977 | - if ($new_tax) { |
|
| 978 | - $new_tax->set_total($new_tax->total() + $amount); |
|
| 979 | - $new_tax->set_percent($taxable_total ? $new_tax->total() / $taxable_total * 100 : 0); |
|
| 980 | - } else { |
|
| 981 | - // no existing tax item. Create it |
|
| 982 | - $new_tax = EE_Line_Item::new_instance(array( |
|
| 983 | - 'TXN_ID' => $total_line_item->TXN_ID(), |
|
| 984 | - 'LIN_name' => $name ? $name : __('Tax', 'event_espresso'), |
|
| 985 | - 'LIN_desc' => $description ? $description : '', |
|
| 986 | - 'LIN_percent' => $taxable_total ? ($amount / $taxable_total * 100) : 0, |
|
| 987 | - 'LIN_total' => $amount, |
|
| 988 | - 'LIN_parent' => $tax_subtotal->ID(), |
|
| 989 | - 'LIN_type' => EEM_Line_Item::type_tax, |
|
| 990 | - 'LIN_code' => $code |
|
| 991 | - )); |
|
| 992 | - } |
|
| 993 | - |
|
| 994 | - $new_tax = apply_filters( |
|
| 995 | - 'FHEE__EEH_Line_Item__set_total_tax_to__new_tax_subtotal', |
|
| 996 | - $new_tax, |
|
| 997 | - $total_line_item |
|
| 998 | - ); |
|
| 999 | - $new_tax->save(); |
|
| 1000 | - $tax_subtotal->set_total($new_tax->total()); |
|
| 1001 | - $tax_subtotal->save(); |
|
| 1002 | - $total_line_item->recalculate_total_including_taxes(); |
|
| 1003 | - return $new_tax; |
|
| 1004 | - } |
|
| 1005 | - |
|
| 1006 | - |
|
| 1007 | - /** |
|
| 1008 | - * Makes all the line items which are children of $line_item taxable (or not). |
|
| 1009 | - * Does NOT save the line items |
|
| 1010 | - * @param EE_Line_Item $line_item |
|
| 1011 | - * @param string $code_substring_for_whitelist if this string is part of the line item's code |
|
| 1012 | - * it will be whitelisted (ie, except from becoming taxable) |
|
| 1013 | - * @param boolean $taxable |
|
| 1014 | - */ |
|
| 1015 | - public static function set_line_items_taxable( |
|
| 1016 | - EE_Line_Item $line_item, |
|
| 1017 | - $taxable = true, |
|
| 1018 | - $code_substring_for_whitelist = null |
|
| 1019 | - ) { |
|
| 1020 | - $whitelisted = false; |
|
| 1021 | - if ($code_substring_for_whitelist !== null) { |
|
| 1022 | - $whitelisted = strpos($line_item->code(), $code_substring_for_whitelist) !== false ? true : false; |
|
| 1023 | - } |
|
| 1024 | - if (!$whitelisted && $line_item->is_line_item()) { |
|
| 1025 | - $line_item->set_is_taxable($taxable); |
|
| 1026 | - } |
|
| 1027 | - foreach ($line_item->children() as $child_line_item) { |
|
| 1028 | - EEH_Line_Item::set_line_items_taxable($child_line_item, $taxable, $code_substring_for_whitelist); |
|
| 1029 | - } |
|
| 1030 | - } |
|
| 1031 | - |
|
| 1032 | - |
|
| 1033 | - /** |
|
| 1034 | - * Gets all descendants that are event subtotals |
|
| 1035 | - * |
|
| 1036 | - * @uses EEH_Line_Item::get_subtotals_of_object_type() |
|
| 1037 | - * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1038 | - * @return EE_Line_Item[] |
|
| 1039 | - */ |
|
| 1040 | - public static function get_event_subtotals(EE_Line_Item $parent_line_item) |
|
| 1041 | - { |
|
| 1042 | - return self::get_subtotals_of_object_type($parent_line_item, 'Event'); |
|
| 1043 | - } |
|
| 1044 | - |
|
| 1045 | - |
|
| 1046 | - /** |
|
| 1047 | - * Gets all descendants subtotals that match the supplied object type |
|
| 1048 | - * |
|
| 1049 | - * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1050 | - * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1051 | - * @param string $obj_type |
|
| 1052 | - * @return EE_Line_Item[] |
|
| 1053 | - */ |
|
| 1054 | - public static function get_subtotals_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '') |
|
| 1055 | - { |
|
| 1056 | - return self::_get_descendants_by_type_and_object_type( |
|
| 1057 | - $parent_line_item, |
|
| 1058 | - EEM_Line_Item::type_sub_total, |
|
| 1059 | - $obj_type |
|
| 1060 | - ); |
|
| 1061 | - } |
|
| 1062 | - |
|
| 1063 | - |
|
| 1064 | - /** |
|
| 1065 | - * Gets all descendants that are tickets |
|
| 1066 | - * |
|
| 1067 | - * @uses EEH_Line_Item::get_line_items_of_object_type() |
|
| 1068 | - * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1069 | - * @return EE_Line_Item[] |
|
| 1070 | - */ |
|
| 1071 | - public static function get_ticket_line_items(EE_Line_Item $parent_line_item) |
|
| 1072 | - { |
|
| 1073 | - return self::get_line_items_of_object_type($parent_line_item, 'Ticket'); |
|
| 1074 | - } |
|
| 1075 | - |
|
| 1076 | - |
|
| 1077 | - /** |
|
| 1078 | - * Gets all descendants subtotals that match the supplied object type |
|
| 1079 | - * |
|
| 1080 | - * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1081 | - * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1082 | - * @param string $obj_type |
|
| 1083 | - * @return EE_Line_Item[] |
|
| 1084 | - */ |
|
| 1085 | - public static function get_line_items_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '') |
|
| 1086 | - { |
|
| 1087 | - return self::_get_descendants_by_type_and_object_type($parent_line_item, EEM_Line_Item::type_line_item, $obj_type); |
|
| 1088 | - } |
|
| 1089 | - |
|
| 1090 | - |
|
| 1091 | - /** |
|
| 1092 | - * Gets all the descendants (ie, children or children of children etc) that are of the type 'tax' |
|
| 1093 | - * @uses EEH_Line_Item::get_descendants_of_type() |
|
| 1094 | - * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1095 | - * @return EE_Line_Item[] |
|
| 1096 | - */ |
|
| 1097 | - public static function get_tax_descendants(EE_Line_Item $parent_line_item) |
|
| 1098 | - { |
|
| 1099 | - return EEH_Line_Item::get_descendants_of_type($parent_line_item, EEM_Line_Item::type_tax); |
|
| 1100 | - } |
|
| 1101 | - |
|
| 1102 | - |
|
| 1103 | - /** |
|
| 1104 | - * Gets all the real items purchased which are children of this item |
|
| 1105 | - * @uses EEH_Line_Item::get_descendants_of_type() |
|
| 1106 | - * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1107 | - * @return EE_Line_Item[] |
|
| 1108 | - */ |
|
| 1109 | - public static function get_line_item_descendants(EE_Line_Item $parent_line_item) |
|
| 1110 | - { |
|
| 1111 | - return EEH_Line_Item::get_descendants_of_type($parent_line_item, EEM_Line_Item::type_line_item); |
|
| 1112 | - } |
|
| 1113 | - |
|
| 1114 | - |
|
| 1115 | - /** |
|
| 1116 | - * Gets all descendants of supplied line item that match the supplied line item type |
|
| 1117 | - * |
|
| 1118 | - * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1119 | - * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1120 | - * @param string $line_item_type one of the EEM_Line_Item constants |
|
| 1121 | - * @return EE_Line_Item[] |
|
| 1122 | - */ |
|
| 1123 | - public static function get_descendants_of_type(EE_Line_Item $parent_line_item, $line_item_type) |
|
| 1124 | - { |
|
| 1125 | - return self::_get_descendants_by_type_and_object_type($parent_line_item, $line_item_type, null); |
|
| 1126 | - } |
|
| 1127 | - |
|
| 1128 | - |
|
| 1129 | - /** |
|
| 1130 | - * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type as well |
|
| 1131 | - * |
|
| 1132 | - * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1133 | - * @param string $line_item_type one of the EEM_Line_Item constants |
|
| 1134 | - * @param string | NULL $obj_type object model class name (minus prefix) or NULL to ignore object type when searching |
|
| 1135 | - * @return EE_Line_Item[] |
|
| 1136 | - */ |
|
| 1137 | - protected static function _get_descendants_by_type_and_object_type( |
|
| 1138 | - EE_Line_Item $parent_line_item, |
|
| 1139 | - $line_item_type, |
|
| 1140 | - $obj_type = null |
|
| 1141 | - ) { |
|
| 1142 | - $objects = array(); |
|
| 1143 | - foreach ($parent_line_item->children() as $child_line_item) { |
|
| 1144 | - if ($child_line_item instanceof EE_Line_Item) { |
|
| 1145 | - if ($child_line_item->type() === $line_item_type |
|
| 1146 | - && ( |
|
| 1147 | - $child_line_item->OBJ_type() === $obj_type || $obj_type === null |
|
| 1148 | - ) |
|
| 1149 | - ) { |
|
| 1150 | - $objects[] = $child_line_item; |
|
| 1151 | - } else { |
|
| 1152 | - // go-through-all-its children looking for more matches |
|
| 1153 | - $objects = array_merge( |
|
| 1154 | - $objects, |
|
| 1155 | - self::_get_descendants_by_type_and_object_type( |
|
| 1156 | - $child_line_item, |
|
| 1157 | - $line_item_type, |
|
| 1158 | - $obj_type |
|
| 1159 | - ) |
|
| 1160 | - ); |
|
| 1161 | - } |
|
| 1162 | - } |
|
| 1163 | - } |
|
| 1164 | - return $objects; |
|
| 1165 | - } |
|
| 1166 | - |
|
| 1167 | - |
|
| 1168 | - /** |
|
| 1169 | - * Gets all descendants subtotals that match the supplied object type |
|
| 1170 | - * |
|
| 1171 | - * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1172 | - * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1173 | - * @param string $OBJ_type object type (like Event) |
|
| 1174 | - * @param array $OBJ_IDs array of OBJ_IDs |
|
| 1175 | - * @return EE_Line_Item[] |
|
| 1176 | - */ |
|
| 1177 | - public static function get_line_items_by_object_type_and_IDs( |
|
| 1178 | - EE_Line_Item $parent_line_item, |
|
| 1179 | - $OBJ_type = '', |
|
| 1180 | - $OBJ_IDs = array() |
|
| 1181 | - ) { |
|
| 1182 | - return self::_get_descendants_by_object_type_and_object_ID($parent_line_item, $OBJ_type, $OBJ_IDs); |
|
| 1183 | - } |
|
| 1184 | - |
|
| 1185 | - |
|
| 1186 | - /** |
|
| 1187 | - * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type as well |
|
| 1188 | - * |
|
| 1189 | - * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1190 | - * @param string $OBJ_type object type (like Event) |
|
| 1191 | - * @param array $OBJ_IDs array of OBJ_IDs |
|
| 1192 | - * @return EE_Line_Item[] |
|
| 1193 | - */ |
|
| 1194 | - protected static function _get_descendants_by_object_type_and_object_ID( |
|
| 1195 | - EE_Line_Item $parent_line_item, |
|
| 1196 | - $OBJ_type, |
|
| 1197 | - $OBJ_IDs |
|
| 1198 | - ) { |
|
| 1199 | - $objects = array(); |
|
| 1200 | - foreach ($parent_line_item->children() as $child_line_item) { |
|
| 1201 | - if ($child_line_item instanceof EE_Line_Item) { |
|
| 1202 | - if ($child_line_item->OBJ_type() === $OBJ_type |
|
| 1203 | - && is_array($OBJ_IDs) |
|
| 1204 | - && in_array($child_line_item->OBJ_ID(), $OBJ_IDs) |
|
| 1205 | - ) { |
|
| 1206 | - $objects[] = $child_line_item; |
|
| 1207 | - } else { |
|
| 1208 | - // go-through-all-its children looking for more matches |
|
| 1209 | - $objects = array_merge( |
|
| 1210 | - $objects, |
|
| 1211 | - self::_get_descendants_by_object_type_and_object_ID( |
|
| 1212 | - $child_line_item, |
|
| 1213 | - $OBJ_type, |
|
| 1214 | - $OBJ_IDs |
|
| 1215 | - ) |
|
| 1216 | - ); |
|
| 1217 | - } |
|
| 1218 | - } |
|
| 1219 | - } |
|
| 1220 | - return $objects; |
|
| 1221 | - } |
|
| 1222 | - |
|
| 1223 | - |
|
| 1224 | - /** |
|
| 1225 | - * Uses a breadth-first-search in order to find the nearest descendant of |
|
| 1226 | - * the specified type and returns it, else NULL |
|
| 1227 | - * |
|
| 1228 | - * @uses EEH_Line_Item::_get_nearest_descendant() |
|
| 1229 | - * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1230 | - * @param string $type like one of the EEM_Line_Item::type_* |
|
| 1231 | - * @return EE_Line_Item |
|
| 1232 | - */ |
|
| 1233 | - public static function get_nearest_descendant_of_type(EE_Line_Item $parent_line_item, $type) |
|
| 1234 | - { |
|
| 1235 | - return self::_get_nearest_descendant($parent_line_item, 'LIN_type', $type); |
|
| 1236 | - } |
|
| 1237 | - |
|
| 1238 | - |
|
| 1239 | - /** |
|
| 1240 | - * Uses a breadth-first-search in order to find the nearest descendant |
|
| 1241 | - * having the specified LIN_code and returns it, else NULL |
|
| 1242 | - * |
|
| 1243 | - * @uses EEH_Line_Item::_get_nearest_descendant() |
|
| 1244 | - * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1245 | - * @param string $code any value used for LIN_code |
|
| 1246 | - * @return EE_Line_Item |
|
| 1247 | - */ |
|
| 1248 | - public static function get_nearest_descendant_having_code(EE_Line_Item $parent_line_item, $code) |
|
| 1249 | - { |
|
| 1250 | - return self::_get_nearest_descendant($parent_line_item, 'LIN_code', $code); |
|
| 1251 | - } |
|
| 1252 | - |
|
| 1253 | - |
|
| 1254 | - /** |
|
| 1255 | - * Uses a breadth-first-search in order to find the nearest descendant |
|
| 1256 | - * having the specified LIN_code and returns it, else NULL |
|
| 1257 | - * |
|
| 1258 | - * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1259 | - * @param string $search_field name of EE_Line_Item property |
|
| 1260 | - * @param string $value any value stored in $search_field |
|
| 1261 | - * @return EE_Line_Item |
|
| 1262 | - */ |
|
| 1263 | - protected static function _get_nearest_descendant(EE_Line_Item $parent_line_item, $search_field, $value) |
|
| 1264 | - { |
|
| 1265 | - foreach ($parent_line_item->children() as $child) { |
|
| 1266 | - if ($child->get($search_field) == $value) { |
|
| 1267 | - return $child; |
|
| 1268 | - } |
|
| 1269 | - } |
|
| 1270 | - foreach ($parent_line_item->children() as $child) { |
|
| 1271 | - $descendant_found = self::_get_nearest_descendant($child, $search_field, $value); |
|
| 1272 | - if ($descendant_found) { |
|
| 1273 | - return $descendant_found; |
|
| 1274 | - } |
|
| 1275 | - } |
|
| 1276 | - return null; |
|
| 1277 | - } |
|
| 1278 | - |
|
| 1279 | - |
|
| 1280 | - /** |
|
| 1281 | - * if passed line item has a TXN ID, uses that to jump directly to the grand total line item for the transaction, |
|
| 1282 | - * else recursively walks up the line item tree until a parent of type total is found, |
|
| 1283 | - * |
|
| 1284 | - * @param EE_Line_Item $line_item |
|
| 1285 | - * @return \EE_Line_Item |
|
| 1286 | - * @throws \EE_Error |
|
| 1287 | - */ |
|
| 1288 | - public static function find_transaction_grand_total_for_line_item(EE_Line_Item $line_item) |
|
| 1289 | - { |
|
| 1290 | - if ($line_item->TXN_ID()) { |
|
| 1291 | - $total_line_item = $line_item->transaction()->total_line_item(false); |
|
| 1292 | - if ($total_line_item instanceof EE_Line_Item) { |
|
| 1293 | - return $total_line_item; |
|
| 1294 | - } |
|
| 1295 | - } else { |
|
| 1296 | - $line_item_parent = $line_item->parent(); |
|
| 1297 | - if ($line_item_parent instanceof EE_Line_Item) { |
|
| 1298 | - if ($line_item_parent->is_total()) { |
|
| 1299 | - return $line_item_parent; |
|
| 1300 | - } |
|
| 1301 | - return EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item_parent); |
|
| 1302 | - } |
|
| 1303 | - } |
|
| 1304 | - throw new EE_Error( |
|
| 1305 | - sprintf( |
|
| 1306 | - __('A valid grand total for line item %1$d was not found.', 'event_espresso'), |
|
| 1307 | - $line_item->ID() |
|
| 1308 | - ) |
|
| 1309 | - ); |
|
| 1310 | - } |
|
| 1311 | - |
|
| 1312 | - |
|
| 1313 | - /** |
|
| 1314 | - * Prints out a representation of the line item tree |
|
| 1315 | - * |
|
| 1316 | - * @param EE_Line_Item $line_item |
|
| 1317 | - * @param int $indentation |
|
| 1318 | - * @return void |
|
| 1319 | - * @throws \EE_Error |
|
| 1320 | - */ |
|
| 1321 | - public static function visualize(EE_Line_Item $line_item, $indentation = 0) |
|
| 1322 | - { |
|
| 1323 | - echo defined('EE_TESTS_DIR') ? "\n" : '<br />'; |
|
| 1324 | - if (!$indentation) { |
|
| 1325 | - echo defined('EE_TESTS_DIR') ? "\n" : '<br />'; |
|
| 1326 | - } |
|
| 1327 | - for ($i = 0; $i < $indentation; $i++) { |
|
| 1328 | - echo ". "; |
|
| 1329 | - } |
|
| 1330 | - $breakdown = ''; |
|
| 1331 | - if ($line_item->is_line_item()) { |
|
| 1332 | - if ($line_item->is_percent()) { |
|
| 1333 | - $breakdown = "{$line_item->percent()}%"; |
|
| 1334 | - } else { |
|
| 1335 | - $breakdown = '$' . "{$line_item->unit_price()} x {$line_item->quantity()}"; |
|
| 1336 | - } |
|
| 1337 | - } |
|
| 1338 | - echo $line_item->name() . " [ ID:{$line_item->ID()} | qty:{$line_item->quantity()} ] {$line_item->type()} : " . '$' . "{$line_item->total()}"; |
|
| 1339 | - if ($breakdown) { |
|
| 1340 | - echo " ( {$breakdown} )"; |
|
| 1341 | - } |
|
| 1342 | - if ($line_item->is_taxable()) { |
|
| 1343 | - echo " * taxable"; |
|
| 1344 | - } |
|
| 1345 | - if ($line_item->children()) { |
|
| 1346 | - foreach ($line_item->children() as $child) { |
|
| 1347 | - self::visualize($child, $indentation + 1); |
|
| 1348 | - } |
|
| 1349 | - } |
|
| 1350 | - } |
|
| 1351 | - |
|
| 1352 | - |
|
| 1353 | - /** |
|
| 1354 | - * Calculates the registration's final price, taking into account that they |
|
| 1355 | - * need to not only help pay for their OWN ticket, but also any transaction-wide surcharges and taxes, |
|
| 1356 | - * and receive a portion of any transaction-wide discounts. |
|
| 1357 | - * eg1, if I buy a $1 ticket and brent buys a $9 ticket, and we receive a $5 discount |
|
| 1358 | - * then I'll get 1/10 of that $5 discount, which is $0.50, and brent will get |
|
| 1359 | - * 9/10ths of that $5 discount, which is $4.50. So my final price should be $0.50 |
|
| 1360 | - * and brent's final price should be $5.50. |
|
| 1361 | - * |
|
| 1362 | - * In order to do this, we basically need to traverse the line item tree calculating |
|
| 1363 | - * the running totals (just as if we were recalculating the total), but when we identify |
|
| 1364 | - * regular line items, we need to keep track of their share of the grand total. |
|
| 1365 | - * Also, we need to keep track of the TAXABLE total for each ticket purchase, so |
|
| 1366 | - * we can know how to apply taxes to it. (Note: "taxable total" does not equal the "pretax total" |
|
| 1367 | - * when there are non-taxable items; otherwise they would be the same) |
|
| 1368 | - * |
|
| 1369 | - * @param EE_Line_Item $line_item |
|
| 1370 | - * @param array $billable_ticket_quantities array of EE_Ticket IDs and their corresponding quantity that |
|
| 1371 | - * can be included in price calculations at this moment |
|
| 1372 | - * @return array keys are line items for tickets IDs and values are their share of the running total, |
|
| 1373 | - * plus the key 'total', and 'taxable' which also has keys of all the ticket IDs. Eg |
|
| 1374 | - * array( |
|
| 1375 | - * 12 => 4.3 |
|
| 1376 | - * 23 => 8.0 |
|
| 1377 | - * 'total' => 16.6, |
|
| 1378 | - * 'taxable' => array( |
|
| 1379 | - * 12 => 10, |
|
| 1380 | - * 23 => 4 |
|
| 1381 | - * ). |
|
| 1382 | - * So to find which registrations have which final price, we need to find which line item |
|
| 1383 | - * is theirs, which can be done with |
|
| 1384 | - * `EEM_Line_Item::instance()->get_line_item_for_registration( $registration );` |
|
| 1385 | - */ |
|
| 1386 | - public static function calculate_reg_final_prices_per_line_item(EE_Line_Item $line_item, $billable_ticket_quantities = array()) |
|
| 1387 | - { |
|
| 1388 | - // init running grand total if not already |
|
| 1389 | - if (!isset($running_totals['total'])) { |
|
| 1390 | - $running_totals['total'] = 0; |
|
| 1391 | - } |
|
| 1392 | - if (!isset($running_totals['taxable'])) { |
|
| 1393 | - $running_totals['taxable'] = array('total' => 0); |
|
| 1394 | - } |
|
| 1395 | - foreach ($line_item->children() as $child_line_item) { |
|
| 1396 | - switch ($child_line_item->type()) { |
|
| 1397 | - case EEM_Line_Item::type_sub_total: |
|
| 1398 | - $running_totals_from_subtotal = EEH_Line_Item::calculate_reg_final_prices_per_line_item($child_line_item, $billable_ticket_quantities); |
|
| 1399 | - // combine arrays but preserve numeric keys |
|
| 1400 | - $running_totals = array_replace_recursive($running_totals_from_subtotal, $running_totals); |
|
| 1401 | - $running_totals['total'] += $running_totals_from_subtotal['total']; |
|
| 1402 | - $running_totals['taxable']['total'] += $running_totals_from_subtotal['taxable']['total']; |
|
| 1403 | - break; |
|
| 1404 | - |
|
| 1405 | - case EEM_Line_Item::type_tax_sub_total: |
|
| 1406 | - // find how much the taxes percentage is |
|
| 1407 | - if ($child_line_item->percent() !== 0) { |
|
| 1408 | - $tax_percent_decimal = $child_line_item->percent() / 100; |
|
| 1409 | - } else { |
|
| 1410 | - $tax_percent_decimal = EE_Taxes::get_total_taxes_percentage() / 100; |
|
| 1411 | - } |
|
| 1412 | - // and apply to all the taxable totals, and add to the pretax totals |
|
| 1413 | - foreach ($running_totals as $line_item_id => $this_running_total) { |
|
| 1414 | - // "total" and "taxable" array key is an exception |
|
| 1415 | - if ($line_item_id === 'taxable') { |
|
| 1416 | - continue; |
|
| 1417 | - } |
|
| 1418 | - $taxable_total = $running_totals['taxable'][ $line_item_id ]; |
|
| 1419 | - $running_totals[ $line_item_id ] += ($taxable_total * $tax_percent_decimal); |
|
| 1420 | - } |
|
| 1421 | - break; |
|
| 1422 | - |
|
| 1423 | - case EEM_Line_Item::type_line_item: |
|
| 1424 | - // ticket line items or ???? |
|
| 1425 | - if ($child_line_item->OBJ_type() === 'Ticket') { |
|
| 1426 | - // kk it's a ticket |
|
| 1427 | - if (isset($running_totals[ $child_line_item->ID() ])) { |
|
| 1428 | - // huh? that shouldn't happen. |
|
| 1429 | - $running_totals['total'] += $child_line_item->total(); |
|
| 1430 | - } else { |
|
| 1431 | - // its not in our running totals yet. great. |
|
| 1432 | - if ($child_line_item->is_taxable()) { |
|
| 1433 | - $taxable_amount = $child_line_item->unit_price(); |
|
| 1434 | - } else { |
|
| 1435 | - $taxable_amount = 0; |
|
| 1436 | - } |
|
| 1437 | - // are we only calculating totals for some tickets? |
|
| 1438 | - if (isset($billable_ticket_quantities[ $child_line_item->OBJ_ID() ])) { |
|
| 1439 | - $quantity = $billable_ticket_quantities[ $child_line_item->OBJ_ID() ]; |
|
| 1440 | - $running_totals[ $child_line_item->ID() ] = $quantity |
|
| 1441 | - ? $child_line_item->unit_price() |
|
| 1442 | - : 0; |
|
| 1443 | - $running_totals['taxable'][ $child_line_item->ID() ] = $quantity |
|
| 1444 | - ? $taxable_amount |
|
| 1445 | - : 0; |
|
| 1446 | - } else { |
|
| 1447 | - $quantity = $child_line_item->quantity(); |
|
| 1448 | - $running_totals[ $child_line_item->ID() ] = $child_line_item->unit_price(); |
|
| 1449 | - $running_totals['taxable'][ $child_line_item->ID() ] = $taxable_amount; |
|
| 1450 | - } |
|
| 1451 | - $running_totals['taxable']['total'] += $taxable_amount * $quantity; |
|
| 1452 | - $running_totals['total'] += $child_line_item->unit_price() * $quantity; |
|
| 1453 | - } |
|
| 1454 | - } else { |
|
| 1455 | - // it's some other type of item added to the cart |
|
| 1456 | - // it should affect the running totals |
|
| 1457 | - // basically we want to convert it into a PERCENT modifier. Because |
|
| 1458 | - // more clearly affect all registration's final price equally |
|
| 1459 | - $line_items_percent_of_running_total = $running_totals['total'] > 0 |
|
| 1460 | - ? ($child_line_item->total() / $running_totals['total']) + 1 |
|
| 1461 | - : 1; |
|
| 1462 | - foreach ($running_totals as $line_item_id => $this_running_total) { |
|
| 1463 | - // the "taxable" array key is an exception |
|
| 1464 | - if ($line_item_id === 'taxable') { |
|
| 1465 | - continue; |
|
| 1466 | - } |
|
| 1467 | - // update the running totals |
|
| 1468 | - // yes this actually even works for the running grand total! |
|
| 1469 | - $running_totals[ $line_item_id ] = |
|
| 1470 | - $line_items_percent_of_running_total * $this_running_total; |
|
| 1471 | - |
|
| 1472 | - if ($child_line_item->is_taxable()) { |
|
| 1473 | - $running_totals['taxable'][ $line_item_id ] = |
|
| 1474 | - $line_items_percent_of_running_total * $running_totals['taxable'][ $line_item_id ]; |
|
| 1475 | - } |
|
| 1476 | - } |
|
| 1477 | - } |
|
| 1478 | - break; |
|
| 1479 | - } |
|
| 1480 | - } |
|
| 1481 | - return $running_totals; |
|
| 1482 | - } |
|
| 1483 | - |
|
| 1484 | - |
|
| 1485 | - /** |
|
| 1486 | - * @param \EE_Line_Item $total_line_item |
|
| 1487 | - * @param \EE_Line_Item $ticket_line_item |
|
| 1488 | - * @return float | null |
|
| 1489 | - * @throws \OutOfRangeException |
|
| 1490 | - */ |
|
| 1491 | - public static function calculate_final_price_for_ticket_line_item(\EE_Line_Item $total_line_item, \EE_Line_Item $ticket_line_item) |
|
| 1492 | - { |
|
| 1493 | - static $final_prices_per_ticket_line_item = array(); |
|
| 1494 | - if (empty($final_prices_per_ticket_line_item)) { |
|
| 1495 | - $final_prices_per_ticket_line_item = \EEH_Line_Item::calculate_reg_final_prices_per_line_item( |
|
| 1496 | - $total_line_item |
|
| 1497 | - ); |
|
| 1498 | - } |
|
| 1499 | - // ok now find this new registration's final price |
|
| 1500 | - if (isset($final_prices_per_ticket_line_item[ $ticket_line_item->ID() ])) { |
|
| 1501 | - return $final_prices_per_ticket_line_item[ $ticket_line_item->ID() ]; |
|
| 1502 | - } |
|
| 1503 | - $message = sprintf( |
|
| 1504 | - __( |
|
| 1505 | - 'The final price for the ticket line item (ID:%1$d) could not be calculated.', |
|
| 1506 | - 'event_espresso' |
|
| 1507 | - ), |
|
| 1508 | - $ticket_line_item->ID() |
|
| 1509 | - ); |
|
| 1510 | - if (WP_DEBUG) { |
|
| 1511 | - $message .= '<br>' . print_r($final_prices_per_ticket_line_item, true); |
|
| 1512 | - throw new \OutOfRangeException($message); |
|
| 1513 | - } else { |
|
| 1514 | - EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message); |
|
| 1515 | - } |
|
| 1516 | - return null; |
|
| 1517 | - } |
|
| 1518 | - |
|
| 1519 | - |
|
| 1520 | - /** |
|
| 1521 | - * Creates a duplicate of the line item tree, except only includes billable items |
|
| 1522 | - * and the portion of line items attributed to billable things |
|
| 1523 | - * |
|
| 1524 | - * @param EE_Line_Item $line_item |
|
| 1525 | - * @param EE_Registration[] $registrations |
|
| 1526 | - * @return \EE_Line_Item |
|
| 1527 | - * @throws \EE_Error |
|
| 1528 | - */ |
|
| 1529 | - public static function billable_line_item_tree(EE_Line_Item $line_item, $registrations) |
|
| 1530 | - { |
|
| 1531 | - $copy_li = EEH_Line_Item::billable_line_item($line_item, $registrations); |
|
| 1532 | - foreach ($line_item->children() as $child_li) { |
|
| 1533 | - $copy_li->add_child_line_item(EEH_Line_Item::billable_line_item_tree($child_li, $registrations)); |
|
| 1534 | - } |
|
| 1535 | - // if this is the grand total line item, make sure the totals all add up |
|
| 1536 | - // (we could have duplicated this logic AS we copied the line items, but |
|
| 1537 | - // it seems DRYer this way) |
|
| 1538 | - if ($copy_li->type() === EEM_Line_Item::type_total) { |
|
| 1539 | - $copy_li->recalculate_total_including_taxes(); |
|
| 1540 | - } |
|
| 1541 | - return $copy_li; |
|
| 1542 | - } |
|
| 1543 | - |
|
| 1544 | - |
|
| 1545 | - /** |
|
| 1546 | - * Creates a new, unsaved line item from $line_item that factors in the |
|
| 1547 | - * number of billable registrations on $registrations. |
|
| 1548 | - * |
|
| 1549 | - * @param EE_Line_Item $line_item |
|
| 1550 | - * @return EE_Line_Item |
|
| 1551 | - * @throws \EE_Error |
|
| 1552 | - * @param EE_Registration[] $registrations |
|
| 1553 | - */ |
|
| 1554 | - public static function billable_line_item(EE_Line_Item $line_item, $registrations) |
|
| 1555 | - { |
|
| 1556 | - $new_li_fields = $line_item->model_field_array(); |
|
| 1557 | - if ($line_item->type() === EEM_Line_Item::type_line_item && |
|
| 1558 | - $line_item->OBJ_type() === 'Ticket' |
|
| 1559 | - ) { |
|
| 1560 | - $count = 0; |
|
| 1561 | - foreach ($registrations as $registration) { |
|
| 1562 | - if ($line_item->OBJ_ID() === $registration->ticket_ID() && |
|
| 1563 | - in_array($registration->status_ID(), EEM_Registration::reg_statuses_that_allow_payment()) |
|
| 1564 | - ) { |
|
| 1565 | - $count++; |
|
| 1566 | - } |
|
| 1567 | - } |
|
| 1568 | - $new_li_fields['LIN_quantity'] = $count; |
|
| 1569 | - } |
|
| 1570 | - // don't set the total. We'll leave that up to the code that calculates it |
|
| 1571 | - unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent'], $new_li_fields['LIN_total']); |
|
| 1572 | - return EE_Line_Item::new_instance($new_li_fields); |
|
| 1573 | - } |
|
| 1574 | - |
|
| 1575 | - |
|
| 1576 | - /** |
|
| 1577 | - * Returns a modified line item tree where all the subtotals which have a total of 0 |
|
| 1578 | - * are removed, and line items with a quantity of 0 |
|
| 1579 | - * |
|
| 1580 | - * @param EE_Line_Item $line_item |null |
|
| 1581 | - * @return \EE_Line_Item|null |
|
| 1582 | - * @throws \EE_Error |
|
| 1583 | - */ |
|
| 1584 | - public static function non_empty_line_items(EE_Line_Item $line_item) |
|
| 1585 | - { |
|
| 1586 | - $copied_li = EEH_Line_Item::non_empty_line_item($line_item); |
|
| 1587 | - if ($copied_li === null) { |
|
| 1588 | - return null; |
|
| 1589 | - } |
|
| 1590 | - // if this is an event subtotal, we want to only include it if it |
|
| 1591 | - // has a non-zero total and at least one ticket line item child |
|
| 1592 | - $ticket_children = 0; |
|
| 1593 | - foreach ($line_item->children() as $child_li) { |
|
| 1594 | - $child_li_copy = EEH_Line_Item::non_empty_line_items($child_li); |
|
| 1595 | - if ($child_li_copy !== null) { |
|
| 1596 | - $copied_li->add_child_line_item($child_li_copy); |
|
| 1597 | - if ($child_li_copy->type() === EEM_Line_Item::type_line_item && |
|
| 1598 | - $child_li_copy->OBJ_type() === 'Ticket' |
|
| 1599 | - ) { |
|
| 1600 | - $ticket_children++; |
|
| 1601 | - } |
|
| 1602 | - } |
|
| 1603 | - } |
|
| 1604 | - // if this is an event subtotal with NO ticket children |
|
| 1605 | - // we basically want to ignore it |
|
| 1606 | - if ($ticket_children === 0 |
|
| 1607 | - && $line_item->type() === EEM_Line_Item::type_sub_total |
|
| 1608 | - && $line_item->OBJ_type() === 'Event' |
|
| 1609 | - && $line_item->total() === 0 |
|
| 1610 | - ) { |
|
| 1611 | - return null; |
|
| 1612 | - } |
|
| 1613 | - return $copied_li; |
|
| 1614 | - } |
|
| 1615 | - |
|
| 1616 | - |
|
| 1617 | - /** |
|
| 1618 | - * Creates a new, unsaved line item, but if it's a ticket line item |
|
| 1619 | - * with a total of 0, or a subtotal of 0, returns null instead |
|
| 1620 | - * |
|
| 1621 | - * @param EE_Line_Item $line_item |
|
| 1622 | - * @return EE_Line_Item |
|
| 1623 | - * @throws \EE_Error |
|
| 1624 | - */ |
|
| 1625 | - public static function non_empty_line_item(EE_Line_Item $line_item) |
|
| 1626 | - { |
|
| 1627 | - if ($line_item->type() === EEM_Line_Item::type_line_item && |
|
| 1628 | - $line_item->OBJ_type() === 'Ticket' && |
|
| 1629 | - $line_item->quantity() === 0 |
|
| 1630 | - ) { |
|
| 1631 | - return null; |
|
| 1632 | - } |
|
| 1633 | - $new_li_fields = $line_item->model_field_array(); |
|
| 1634 | - // don't set the total. We'll leave that up to the code that calculates it |
|
| 1635 | - unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent']); |
|
| 1636 | - return EE_Line_Item::new_instance($new_li_fields); |
|
| 1637 | - } |
|
| 1638 | - |
|
| 1639 | - |
|
| 1640 | - |
|
| 1641 | - /**************************************** @DEPRECATED METHODS *************************************** */ |
|
| 1642 | - /** |
|
| 1643 | - * @deprecated |
|
| 1644 | - * @param EE_Line_Item $total_line_item |
|
| 1645 | - * @return \EE_Line_Item |
|
| 1646 | - * @throws \EE_Error |
|
| 1647 | - */ |
|
| 1648 | - public static function get_items_subtotal(EE_Line_Item $total_line_item) |
|
| 1649 | - { |
|
| 1650 | - EE_Error::doing_it_wrong('EEH_Line_Item::get_items_subtotal()', __('Method replaced with EEH_Line_Item::get_pre_tax_subtotal()', 'event_espresso'), '4.6.0'); |
|
| 1651 | - return self::get_pre_tax_subtotal($total_line_item); |
|
| 1652 | - } |
|
| 1653 | - |
|
| 1654 | - |
|
| 1655 | - /** |
|
| 1656 | - * @deprecated |
|
| 1657 | - * @param EE_Transaction $transaction |
|
| 1658 | - * @return \EE_Line_Item |
|
| 1659 | - * @throws \EE_Error |
|
| 1660 | - */ |
|
| 1661 | - public static function create_default_total_line_item($transaction = null) |
|
| 1662 | - { |
|
| 1663 | - EE_Error::doing_it_wrong('EEH_Line_Item::create_default_total_line_item()', __('Method replaced with EEH_Line_Item::create_total_line_item()', 'event_espresso'), '4.6.0'); |
|
| 1664 | - return self::create_total_line_item($transaction); |
|
| 1665 | - } |
|
| 1666 | - |
|
| 1667 | - |
|
| 1668 | - /** |
|
| 1669 | - * @deprecated |
|
| 1670 | - * @param EE_Line_Item $total_line_item |
|
| 1671 | - * @param EE_Transaction $transaction |
|
| 1672 | - * @return \EE_Line_Item |
|
| 1673 | - * @throws \EE_Error |
|
| 1674 | - */ |
|
| 1675 | - public static function create_default_tickets_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 1676 | - { |
|
| 1677 | - EE_Error::doing_it_wrong('EEH_Line_Item::create_default_tickets_subtotal()', __('Method replaced with EEH_Line_Item::create_pre_tax_subtotal()', 'event_espresso'), '4.6.0'); |
|
| 1678 | - return self::create_pre_tax_subtotal($total_line_item, $transaction); |
|
| 1679 | - } |
|
| 1680 | - |
|
| 1681 | - |
|
| 1682 | - /** |
|
| 1683 | - * @deprecated |
|
| 1684 | - * @param EE_Line_Item $total_line_item |
|
| 1685 | - * @param EE_Transaction $transaction |
|
| 1686 | - * @return \EE_Line_Item |
|
| 1687 | - * @throws \EE_Error |
|
| 1688 | - */ |
|
| 1689 | - public static function create_default_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 1690 | - { |
|
| 1691 | - EE_Error::doing_it_wrong('EEH_Line_Item::create_default_taxes_subtotal()', __('Method replaced with EEH_Line_Item::create_taxes_subtotal()', 'event_espresso'), '4.6.0'); |
|
| 1692 | - return self::create_taxes_subtotal($total_line_item, $transaction); |
|
| 1693 | - } |
|
| 1694 | - |
|
| 1695 | - |
|
| 1696 | - /** |
|
| 1697 | - * @deprecated |
|
| 1698 | - * @param EE_Line_Item $total_line_item |
|
| 1699 | - * @param EE_Transaction $transaction |
|
| 1700 | - * @return \EE_Line_Item |
|
| 1701 | - * @throws \EE_Error |
|
| 1702 | - */ |
|
| 1703 | - public static function create_default_event_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 1704 | - { |
|
| 1705 | - EE_Error::doing_it_wrong('EEH_Line_Item::create_default_event_subtotal()', __('Method replaced with EEH_Line_Item::create_event_subtotal()', 'event_espresso'), '4.6.0'); |
|
| 1706 | - return self::create_event_subtotal($total_line_item, $transaction); |
|
| 1707 | - } |
|
| 23 | + // other functions: cancel ticket purchase |
|
| 24 | + // delete ticket purchase |
|
| 25 | + // add promotion |
|
| 26 | + |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Adds a simple item (unrelated to any other model object) to the provided PARENT line item. |
|
| 30 | + * Does NOT automatically re-calculate the line item totals or update the related transaction. |
|
| 31 | + * You should call recalculate_total_including_taxes() on the grant total line item after this |
|
| 32 | + * to update the subtotals, and EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 33 | + * to keep the registration final prices in-sync with the transaction's total. |
|
| 34 | + * |
|
| 35 | + * @param EE_Line_Item $parent_line_item |
|
| 36 | + * @param string $name |
|
| 37 | + * @param float $unit_price |
|
| 38 | + * @param string $description |
|
| 39 | + * @param int $quantity |
|
| 40 | + * @param boolean $taxable |
|
| 41 | + * @param boolean $code if set to a value, ensures there is only one line item with that code |
|
| 42 | + * @return boolean success |
|
| 43 | + * @throws \EE_Error |
|
| 44 | + */ |
|
| 45 | + public static function add_unrelated_item(EE_Line_Item $parent_line_item, $name, $unit_price, $description = '', $quantity = 1, $taxable = false, $code = null) |
|
| 46 | + { |
|
| 47 | + $items_subtotal = self::get_pre_tax_subtotal($parent_line_item); |
|
| 48 | + $line_item = EE_Line_Item::new_instance(array( |
|
| 49 | + 'LIN_name' => $name, |
|
| 50 | + 'LIN_desc' => $description, |
|
| 51 | + 'LIN_unit_price' => $unit_price, |
|
| 52 | + 'LIN_quantity' => $quantity, |
|
| 53 | + 'LIN_percent' => null, |
|
| 54 | + 'LIN_is_taxable' => $taxable, |
|
| 55 | + 'LIN_order' => $items_subtotal instanceof EE_Line_Item ? count($items_subtotal->children()) : 0, |
|
| 56 | + 'LIN_total' => (float) $unit_price * (int) $quantity, |
|
| 57 | + 'LIN_type' => EEM_Line_Item::type_line_item, |
|
| 58 | + 'LIN_code' => $code, |
|
| 59 | + )); |
|
| 60 | + $line_item = apply_filters( |
|
| 61 | + 'FHEE__EEH_Line_Item__add_unrelated_item__line_item', |
|
| 62 | + $line_item, |
|
| 63 | + $parent_line_item |
|
| 64 | + ); |
|
| 65 | + return self::add_item($parent_line_item, $line_item); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Adds a simple item ( unrelated to any other model object) to the total line item, |
|
| 71 | + * in the correct spot in the line item tree. Automatically |
|
| 72 | + * re-calculates the line item totals and updates the related transaction. But |
|
| 73 | + * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
|
| 74 | + * should probably change because of this). |
|
| 75 | + * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 76 | + * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 77 | + * |
|
| 78 | + * @param EE_Line_Item $parent_line_item |
|
| 79 | + * @param string $name |
|
| 80 | + * @param float $percentage_amount |
|
| 81 | + * @param string $description |
|
| 82 | + * @param boolean $taxable |
|
| 83 | + * @return boolean success |
|
| 84 | + * @throws \EE_Error |
|
| 85 | + */ |
|
| 86 | + public static function add_percentage_based_item(EE_Line_Item $parent_line_item, $name, $percentage_amount, $description = '', $taxable = false) |
|
| 87 | + { |
|
| 88 | + $line_item = EE_Line_Item::new_instance(array( |
|
| 89 | + 'LIN_name' => $name, |
|
| 90 | + 'LIN_desc' => $description, |
|
| 91 | + 'LIN_unit_price' => 0, |
|
| 92 | + 'LIN_percent' => $percentage_amount, |
|
| 93 | + 'LIN_quantity' => 1, |
|
| 94 | + 'LIN_is_taxable' => $taxable, |
|
| 95 | + 'LIN_total' => (float) ($percentage_amount * ($parent_line_item->total() / 100)), |
|
| 96 | + 'LIN_type' => EEM_Line_Item::type_line_item, |
|
| 97 | + 'LIN_parent' => $parent_line_item->ID() |
|
| 98 | + )); |
|
| 99 | + $line_item = apply_filters( |
|
| 100 | + 'FHEE__EEH_Line_Item__add_percentage_based_item__line_item', |
|
| 101 | + $line_item |
|
| 102 | + ); |
|
| 103 | + return $parent_line_item->add_child_line_item($line_item, false); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Returns the new line item created by adding a purchase of the ticket |
|
| 109 | + * ensures that ticket line item is saved, and that cart total has been recalculated. |
|
| 110 | + * If this ticket has already been purchased, just increments its count. |
|
| 111 | + * Automatically re-calculates the line item totals and updates the related transaction. But |
|
| 112 | + * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
|
| 113 | + * should probably change because of this). |
|
| 114 | + * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 115 | + * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 116 | + * |
|
| 117 | + * @param EE_Line_Item $total_line_item grand total line item of type EEM_Line_Item::type_total |
|
| 118 | + * @param EE_Ticket $ticket |
|
| 119 | + * @param int $qty |
|
| 120 | + * @return \EE_Line_Item |
|
| 121 | + * @throws \EE_Error |
|
| 122 | + */ |
|
| 123 | + public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1) |
|
| 124 | + { |
|
| 125 | + if (!$total_line_item instanceof EE_Line_Item || !$total_line_item->is_total()) { |
|
| 126 | + throw new EE_Error(sprintf(__('A valid line item total is required in order to add tickets. A line item of type "%s" was passed.', 'event_espresso'), $ticket->ID(), $total_line_item->ID())); |
|
| 127 | + } |
|
| 128 | + // either increment the qty for an existing ticket |
|
| 129 | + $line_item = self::increment_ticket_qty_if_already_in_cart($total_line_item, $ticket, $qty); |
|
| 130 | + // or add a new one |
|
| 131 | + if (!$line_item instanceof EE_Line_Item) { |
|
| 132 | + $line_item = self::create_ticket_line_item($total_line_item, $ticket, $qty); |
|
| 133 | + } |
|
| 134 | + $total_line_item->recalculate_total_including_taxes(); |
|
| 135 | + return $line_item; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Returns the new line item created by adding a purchase of the ticket |
|
| 141 | + * @param \EE_Line_Item $total_line_item |
|
| 142 | + * @param EE_Ticket $ticket |
|
| 143 | + * @param int $qty |
|
| 144 | + * @return \EE_Line_Item |
|
| 145 | + * @throws \EE_Error |
|
| 146 | + */ |
|
| 147 | + public static function increment_ticket_qty_if_already_in_cart(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1) |
|
| 148 | + { |
|
| 149 | + $line_item = null; |
|
| 150 | + if ($total_line_item instanceof EE_Line_Item && $total_line_item->is_total()) { |
|
| 151 | + $ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item); |
|
| 152 | + foreach ((array) $ticket_line_items as $ticket_line_item) { |
|
| 153 | + if ($ticket_line_item instanceof EE_Line_Item |
|
| 154 | + && (int) $ticket_line_item->OBJ_ID() === (int) $ticket->ID() |
|
| 155 | + ) { |
|
| 156 | + $line_item = $ticket_line_item; |
|
| 157 | + break; |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + if ($line_item instanceof EE_Line_Item) { |
|
| 162 | + EEH_Line_Item::increment_quantity($line_item, $qty); |
|
| 163 | + return $line_item; |
|
| 164 | + } |
|
| 165 | + return null; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Increments the line item and all its children's quantity by $qty (but percent line items are unaffected). |
|
| 171 | + * Does NOT save or recalculate other line items totals |
|
| 172 | + * |
|
| 173 | + * @param EE_Line_Item $line_item |
|
| 174 | + * @param int $qty |
|
| 175 | + * @return void |
|
| 176 | + * @throws \EE_Error |
|
| 177 | + */ |
|
| 178 | + public static function increment_quantity(EE_Line_Item $line_item, $qty = 1) |
|
| 179 | + { |
|
| 180 | + if (!$line_item->is_percent()) { |
|
| 181 | + $qty += $line_item->quantity(); |
|
| 182 | + $line_item->set_quantity($qty); |
|
| 183 | + $line_item->set_total($line_item->unit_price() * $qty); |
|
| 184 | + $line_item->save(); |
|
| 185 | + } |
|
| 186 | + foreach ($line_item->children() as $child) { |
|
| 187 | + if ($child->is_sub_line_item()) { |
|
| 188 | + EEH_Line_Item::update_quantity($child, $qty); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Decrements the line item and all its children's quantity by $qty (but percent line items are unaffected). |
|
| 196 | + * Does NOT save or recalculate other line items totals |
|
| 197 | + * |
|
| 198 | + * @param EE_Line_Item $line_item |
|
| 199 | + * @param int $qty |
|
| 200 | + * @return void |
|
| 201 | + * @throws \EE_Error |
|
| 202 | + */ |
|
| 203 | + public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1) |
|
| 204 | + { |
|
| 205 | + if (!$line_item->is_percent()) { |
|
| 206 | + $qty = $line_item->quantity() - $qty; |
|
| 207 | + $qty = max($qty, 0); |
|
| 208 | + $line_item->set_quantity($qty); |
|
| 209 | + $line_item->set_total($line_item->unit_price() * $qty); |
|
| 210 | + $line_item->save(); |
|
| 211 | + } |
|
| 212 | + foreach ($line_item->children() as $child) { |
|
| 213 | + if ($child->is_sub_line_item()) { |
|
| 214 | + EEH_Line_Item::update_quantity($child, $qty); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Updates the line item and its children's quantities to the specified number. |
|
| 222 | + * Does NOT save them or recalculate totals. |
|
| 223 | + * |
|
| 224 | + * @param EE_Line_Item $line_item |
|
| 225 | + * @param int $new_quantity |
|
| 226 | + * @throws \EE_Error |
|
| 227 | + */ |
|
| 228 | + public static function update_quantity(EE_Line_Item $line_item, $new_quantity) |
|
| 229 | + { |
|
| 230 | + if (!$line_item->is_percent()) { |
|
| 231 | + $line_item->set_quantity($new_quantity); |
|
| 232 | + $line_item->set_total($line_item->unit_price() * $new_quantity); |
|
| 233 | + $line_item->save(); |
|
| 234 | + } |
|
| 235 | + foreach ($line_item->children() as $child) { |
|
| 236 | + if ($child->is_sub_line_item()) { |
|
| 237 | + EEH_Line_Item::update_quantity($child, $new_quantity); |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Returns the new line item created by adding a purchase of the ticket |
|
| 245 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 246 | + * @param EE_Ticket $ticket |
|
| 247 | + * @param int $qty |
|
| 248 | + * @return \EE_Line_Item |
|
| 249 | + * @throws \EE_Error |
|
| 250 | + */ |
|
| 251 | + public static function create_ticket_line_item(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1) |
|
| 252 | + { |
|
| 253 | + $datetimes = $ticket->datetimes(); |
|
| 254 | + $first_datetime = reset($datetimes); |
|
| 255 | + if ($first_datetime instanceof EE_Datetime && $first_datetime->event() instanceof EE_Event) { |
|
| 256 | + $first_datetime_name = $first_datetime->event()->name(); |
|
| 257 | + } else { |
|
| 258 | + $first_datetime_name = __('Event', 'event_espresso'); |
|
| 259 | + } |
|
| 260 | + $event = sprintf(_x('(For %1$s)', '(For Event Name)', 'event_espresso'), $first_datetime_name); |
|
| 261 | + // get event subtotal line |
|
| 262 | + $events_sub_total = self::get_event_line_item_for_ticket($total_line_item, $ticket); |
|
| 263 | + // add $ticket to cart |
|
| 264 | + $line_item = EE_Line_Item::new_instance(array( |
|
| 265 | + 'LIN_name' => $ticket->name(), |
|
| 266 | + 'LIN_desc' => $ticket->description() !== '' ? $ticket->description() . ' ' . $event : $event, |
|
| 267 | + 'LIN_unit_price' => $ticket->price(), |
|
| 268 | + 'LIN_quantity' => $qty, |
|
| 269 | + 'LIN_is_taxable' => $ticket->taxable(), |
|
| 270 | + 'LIN_order' => count($events_sub_total->children()), |
|
| 271 | + 'LIN_total' => $ticket->price() * $qty, |
|
| 272 | + 'LIN_type' => EEM_Line_Item::type_line_item, |
|
| 273 | + 'OBJ_ID' => $ticket->ID(), |
|
| 274 | + 'OBJ_type' => 'Ticket' |
|
| 275 | + )); |
|
| 276 | + $line_item = apply_filters( |
|
| 277 | + 'FHEE__EEH_Line_Item__create_ticket_line_item__line_item', |
|
| 278 | + $line_item |
|
| 279 | + ); |
|
| 280 | + $events_sub_total->add_child_line_item($line_item); |
|
| 281 | + // now add the sub-line items |
|
| 282 | + $running_total_for_ticket = 0; |
|
| 283 | + foreach ($ticket->prices(array('order_by' => array('PRC_order' => 'ASC'))) as $price) { |
|
| 284 | + $sign = $price->is_discount() ? -1 : 1; |
|
| 285 | + $price_total = $price->is_percent() |
|
| 286 | + ? $running_total_for_ticket * $price->amount() / 100 |
|
| 287 | + : $price->amount() * $qty; |
|
| 288 | + $sub_line_item = EE_Line_Item::new_instance(array( |
|
| 289 | + 'LIN_name' => $price->name(), |
|
| 290 | + 'LIN_desc' => $price->desc(), |
|
| 291 | + 'LIN_quantity' => $price->is_percent() ? null : $qty, |
|
| 292 | + 'LIN_is_taxable' => false, |
|
| 293 | + 'LIN_order' => $price->order(), |
|
| 294 | + 'LIN_total' => $sign * $price_total, |
|
| 295 | + 'LIN_type' => EEM_Line_Item::type_sub_line_item, |
|
| 296 | + 'OBJ_ID' => $price->ID(), |
|
| 297 | + 'OBJ_type' => 'Price' |
|
| 298 | + )); |
|
| 299 | + $sub_line_item = apply_filters( |
|
| 300 | + 'FHEE__EEH_Line_Item__create_ticket_line_item__sub_line_item', |
|
| 301 | + $sub_line_item |
|
| 302 | + ); |
|
| 303 | + if ($price->is_percent()) { |
|
| 304 | + $sub_line_item->set_percent($sign * $price->amount()); |
|
| 305 | + } else { |
|
| 306 | + $sub_line_item->set_unit_price($sign * $price->amount()); |
|
| 307 | + } |
|
| 308 | + $running_total_for_ticket += $price_total; |
|
| 309 | + $line_item->add_child_line_item($sub_line_item); |
|
| 310 | + } |
|
| 311 | + return $line_item; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Adds the specified item under the pre-tax-sub-total line item. Automatically |
|
| 317 | + * re-calculates the line item totals and updates the related transaction. But |
|
| 318 | + * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
|
| 319 | + * should probably change because of this). |
|
| 320 | + * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 321 | + * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 322 | + * |
|
| 323 | + * @param EE_Line_Item $total_line_item |
|
| 324 | + * @param EE_Line_Item $item to be added |
|
| 325 | + * @return boolean |
|
| 326 | + * @throws \EE_Error |
|
| 327 | + */ |
|
| 328 | + public static function add_item(EE_Line_Item $total_line_item, EE_Line_Item $item) |
|
| 329 | + { |
|
| 330 | + $pre_tax_subtotal = self::get_pre_tax_subtotal($total_line_item); |
|
| 331 | + if ($pre_tax_subtotal instanceof EE_Line_Item) { |
|
| 332 | + $success = $pre_tax_subtotal->add_child_line_item($item); |
|
| 333 | + } else { |
|
| 334 | + return false; |
|
| 335 | + } |
|
| 336 | + $total_line_item->recalculate_total_including_taxes(); |
|
| 337 | + return $success; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * cancels an existing ticket line item, |
|
| 343 | + * by decrementing it's quantity by 1 and adding a new "type_cancellation" sub-line-item. |
|
| 344 | + * ALL totals and subtotals will NEED TO BE UPDATED after performing this action |
|
| 345 | + * |
|
| 346 | + * @param EE_Line_Item $ticket_line_item |
|
| 347 | + * @param int $qty |
|
| 348 | + * @return bool success |
|
| 349 | + * @throws \EE_Error |
|
| 350 | + */ |
|
| 351 | + public static function cancel_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1) |
|
| 352 | + { |
|
| 353 | + // validate incoming line_item |
|
| 354 | + if ($ticket_line_item->OBJ_type() !== 'Ticket') { |
|
| 355 | + throw new EE_Error( |
|
| 356 | + sprintf( |
|
| 357 | + __('The supplied line item must have an Object Type of "Ticket", not %1$s.', 'event_espresso'), |
|
| 358 | + $ticket_line_item->type() |
|
| 359 | + ) |
|
| 360 | + ); |
|
| 361 | + } |
|
| 362 | + if ($ticket_line_item->quantity() < $qty) { |
|
| 363 | + throw new EE_Error( |
|
| 364 | + sprintf( |
|
| 365 | + __('Can not cancel %1$d ticket(s) because the supplied line item has a quantity of %2$d.', 'event_espresso'), |
|
| 366 | + $qty, |
|
| 367 | + $ticket_line_item->quantity() |
|
| 368 | + ) |
|
| 369 | + ); |
|
| 370 | + } |
|
| 371 | + // decrement ticket quantity; don't rely on auto-fixing when recalculating totals to do this |
|
| 372 | + $ticket_line_item->set_quantity($ticket_line_item->quantity() - $qty); |
|
| 373 | + foreach ($ticket_line_item->children() as $child_line_item) { |
|
| 374 | + if ($child_line_item->is_sub_line_item() |
|
| 375 | + && !$child_line_item->is_percent() |
|
| 376 | + && !$child_line_item->is_cancellation() |
|
| 377 | + ) { |
|
| 378 | + $child_line_item->set_quantity($child_line_item->quantity() - $qty); |
|
| 379 | + } |
|
| 380 | + } |
|
| 381 | + // get cancellation sub line item |
|
| 382 | + $cancellation_line_item = EEH_Line_Item::get_descendants_of_type( |
|
| 383 | + $ticket_line_item, |
|
| 384 | + EEM_Line_Item::type_cancellation |
|
| 385 | + ); |
|
| 386 | + $cancellation_line_item = reset($cancellation_line_item); |
|
| 387 | + // verify that this ticket was indeed previously cancelled |
|
| 388 | + if ($cancellation_line_item instanceof EE_Line_Item) { |
|
| 389 | + // increment cancelled quantity |
|
| 390 | + $cancellation_line_item->set_quantity($cancellation_line_item->quantity() + $qty); |
|
| 391 | + } else { |
|
| 392 | + // create cancellation sub line item |
|
| 393 | + $cancellation_line_item = EE_Line_Item::new_instance(array( |
|
| 394 | + 'LIN_name' => __('Cancellation', 'event_espresso'), |
|
| 395 | + 'LIN_desc' => sprintf( |
|
| 396 | + _x('Cancelled %1$s : %2$s', 'Cancelled Ticket Name : 2015-01-01 11:11', 'event_espresso'), |
|
| 397 | + $ticket_line_item->name(), |
|
| 398 | + current_time(get_option('date_format') . ' ' . get_option('time_format')) |
|
| 399 | + ), |
|
| 400 | + 'LIN_unit_price' => 0, // $ticket_line_item->unit_price() |
|
| 401 | + 'LIN_quantity' => $qty, |
|
| 402 | + 'LIN_is_taxable' => $ticket_line_item->is_taxable(), |
|
| 403 | + 'LIN_order' => count($ticket_line_item->children()), |
|
| 404 | + 'LIN_total' => 0, // $ticket_line_item->unit_price() |
|
| 405 | + 'LIN_type' => EEM_Line_Item::type_cancellation, |
|
| 406 | + )); |
|
| 407 | + $ticket_line_item->add_child_line_item($cancellation_line_item); |
|
| 408 | + } |
|
| 409 | + if ($ticket_line_item->save_this_and_descendants() > 0) { |
|
| 410 | + // decrement parent line item quantity |
|
| 411 | + $event_line_item = $ticket_line_item->parent(); |
|
| 412 | + if ($event_line_item instanceof EE_Line_Item && $event_line_item->OBJ_type() === 'Event') { |
|
| 413 | + $event_line_item->set_quantity($event_line_item->quantity() - $qty); |
|
| 414 | + $event_line_item->save(); |
|
| 415 | + } |
|
| 416 | + EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item); |
|
| 417 | + return true; |
|
| 418 | + } |
|
| 419 | + return false; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * reinstates (un-cancels?) a previously canceled ticket line item, |
|
| 425 | + * by incrementing it's quantity by 1, and decrementing it's "type_cancellation" sub-line-item. |
|
| 426 | + * ALL totals and subtotals will NEED TO BE UPDATED after performing this action |
|
| 427 | + * |
|
| 428 | + * @param EE_Line_Item $ticket_line_item |
|
| 429 | + * @param int $qty |
|
| 430 | + * @return bool success |
|
| 431 | + * @throws \EE_Error |
|
| 432 | + */ |
|
| 433 | + public static function reinstate_canceled_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1) |
|
| 434 | + { |
|
| 435 | + // validate incoming line_item |
|
| 436 | + if ($ticket_line_item->OBJ_type() !== 'Ticket') { |
|
| 437 | + throw new EE_Error( |
|
| 438 | + sprintf( |
|
| 439 | + __('The supplied line item must have an Object Type of "Ticket", not %1$s.', 'event_espresso'), |
|
| 440 | + $ticket_line_item->type() |
|
| 441 | + ) |
|
| 442 | + ); |
|
| 443 | + } |
|
| 444 | + // get cancellation sub line item |
|
| 445 | + $cancellation_line_item = EEH_Line_Item::get_descendants_of_type( |
|
| 446 | + $ticket_line_item, |
|
| 447 | + EEM_Line_Item::type_cancellation |
|
| 448 | + ); |
|
| 449 | + $cancellation_line_item = reset($cancellation_line_item); |
|
| 450 | + // verify that this ticket was indeed previously cancelled |
|
| 451 | + if (!$cancellation_line_item instanceof EE_Line_Item) { |
|
| 452 | + return false; |
|
| 453 | + } |
|
| 454 | + if ($cancellation_line_item->quantity() > $qty) { |
|
| 455 | + // decrement cancelled quantity |
|
| 456 | + $cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty); |
|
| 457 | + } elseif ($cancellation_line_item->quantity() == $qty) { |
|
| 458 | + // decrement cancelled quantity in case anyone still has the object kicking around |
|
| 459 | + $cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty); |
|
| 460 | + // delete because quantity will end up as 0 |
|
| 461 | + $cancellation_line_item->delete(); |
|
| 462 | + // and attempt to destroy the object, |
|
| 463 | + // even though PHP won't actually destroy it until it needs the memory |
|
| 464 | + unset($cancellation_line_item); |
|
| 465 | + } else { |
|
| 466 | + // what ?!?! negative quantity ?!?! |
|
| 467 | + throw new EE_Error( |
|
| 468 | + sprintf( |
|
| 469 | + __( |
|
| 470 | + 'Can not reinstate %1$d cancelled ticket(s) because the cancelled ticket quantity is only %2$d.', |
|
| 471 | + 'event_espresso' |
|
| 472 | + ), |
|
| 473 | + $qty, |
|
| 474 | + $cancellation_line_item->quantity() |
|
| 475 | + ) |
|
| 476 | + ); |
|
| 477 | + } |
|
| 478 | + // increment ticket quantity |
|
| 479 | + $ticket_line_item->set_quantity($ticket_line_item->quantity() + $qty); |
|
| 480 | + if ($ticket_line_item->save_this_and_descendants() > 0) { |
|
| 481 | + // increment parent line item quantity |
|
| 482 | + $event_line_item = $ticket_line_item->parent(); |
|
| 483 | + if ($event_line_item instanceof EE_Line_Item && $event_line_item->OBJ_type() === 'Event') { |
|
| 484 | + $event_line_item->set_quantity($event_line_item->quantity() + $qty); |
|
| 485 | + } |
|
| 486 | + EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item); |
|
| 487 | + return true; |
|
| 488 | + } |
|
| 489 | + return false; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + |
|
| 493 | + /** |
|
| 494 | + * calls EEH_Line_Item::find_transaction_grand_total_for_line_item() |
|
| 495 | + * then EE_Line_Item::recalculate_total_including_taxes() on the result |
|
| 496 | + * |
|
| 497 | + * @param EE_Line_Item $line_item |
|
| 498 | + * @return \EE_Line_Item |
|
| 499 | + */ |
|
| 500 | + public static function get_grand_total_and_recalculate_everything(EE_Line_Item $line_item) |
|
| 501 | + { |
|
| 502 | + $grand_total_line_item = EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item); |
|
| 503 | + return $grand_total_line_item->recalculate_total_including_taxes(); |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * Gets the line item which contains the subtotal of all the items |
|
| 509 | + * |
|
| 510 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 511 | + * @return \EE_Line_Item |
|
| 512 | + * @throws \EE_Error |
|
| 513 | + */ |
|
| 514 | + public static function get_pre_tax_subtotal(EE_Line_Item $total_line_item) |
|
| 515 | + { |
|
| 516 | + $pre_tax_subtotal = $total_line_item->get_child_line_item('pre-tax-subtotal'); |
|
| 517 | + return $pre_tax_subtotal instanceof EE_Line_Item |
|
| 518 | + ? $pre_tax_subtotal |
|
| 519 | + : self::create_pre_tax_subtotal($total_line_item); |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Gets the line item for the taxes subtotal |
|
| 525 | + * |
|
| 526 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 527 | + * @return \EE_Line_Item |
|
| 528 | + * @throws \EE_Error |
|
| 529 | + */ |
|
| 530 | + public static function get_taxes_subtotal(EE_Line_Item $total_line_item) |
|
| 531 | + { |
|
| 532 | + $taxes = $total_line_item->get_child_line_item('taxes'); |
|
| 533 | + return $taxes ? $taxes : self::create_taxes_subtotal($total_line_item); |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + |
|
| 537 | + /** |
|
| 538 | + * sets the TXN ID on an EE_Line_Item if passed a valid EE_Transaction object |
|
| 539 | + * |
|
| 540 | + * @param EE_Line_Item $line_item |
|
| 541 | + * @param EE_Transaction $transaction |
|
| 542 | + * @return void |
|
| 543 | + * @throws \EE_Error |
|
| 544 | + */ |
|
| 545 | + public static function set_TXN_ID(EE_Line_Item $line_item, $transaction = null) |
|
| 546 | + { |
|
| 547 | + if ($transaction) { |
|
| 548 | + /** @type EEM_Transaction $EEM_Transaction */ |
|
| 549 | + $EEM_Transaction = EE_Registry::instance()->load_model('Transaction'); |
|
| 550 | + $TXN_ID = $EEM_Transaction->ensure_is_ID($transaction); |
|
| 551 | + $line_item->set_TXN_ID($TXN_ID); |
|
| 552 | + } |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * Creates a new default total line item for the transaction, |
|
| 558 | + * and its tickets subtotal and taxes subtotal line items (and adds the |
|
| 559 | + * existing taxes as children of the taxes subtotal line item) |
|
| 560 | + * |
|
| 561 | + * @param EE_Transaction $transaction |
|
| 562 | + * @return \EE_Line_Item of type total |
|
| 563 | + * @throws \EE_Error |
|
| 564 | + */ |
|
| 565 | + public static function create_total_line_item($transaction = null) |
|
| 566 | + { |
|
| 567 | + $total_line_item = EE_Line_Item::new_instance(array( |
|
| 568 | + 'LIN_code' => 'total', |
|
| 569 | + 'LIN_name' => __('Grand Total', 'event_espresso'), |
|
| 570 | + 'LIN_type' => EEM_Line_Item::type_total, |
|
| 571 | + 'OBJ_type' => 'Transaction' |
|
| 572 | + )); |
|
| 573 | + $total_line_item = apply_filters( |
|
| 574 | + 'FHEE__EEH_Line_Item__create_total_line_item__total_line_item', |
|
| 575 | + $total_line_item |
|
| 576 | + ); |
|
| 577 | + self::set_TXN_ID($total_line_item, $transaction); |
|
| 578 | + self::create_pre_tax_subtotal($total_line_item, $transaction); |
|
| 579 | + self::create_taxes_subtotal($total_line_item, $transaction); |
|
| 580 | + return $total_line_item; |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + |
|
| 584 | + /** |
|
| 585 | + * Creates a default items subtotal line item |
|
| 586 | + * |
|
| 587 | + * @param EE_Line_Item $total_line_item |
|
| 588 | + * @param EE_Transaction $transaction |
|
| 589 | + * @return EE_Line_Item |
|
| 590 | + * @throws \EE_Error |
|
| 591 | + */ |
|
| 592 | + protected static function create_pre_tax_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 593 | + { |
|
| 594 | + $pre_tax_line_item = EE_Line_Item::new_instance(array( |
|
| 595 | + 'LIN_code' => 'pre-tax-subtotal', |
|
| 596 | + 'LIN_name' => __('Pre-Tax Subtotal', 'event_espresso'), |
|
| 597 | + 'LIN_type' => EEM_Line_Item::type_sub_total |
|
| 598 | + )); |
|
| 599 | + $pre_tax_line_item = apply_filters( |
|
| 600 | + 'FHEE__EEH_Line_Item__create_pre_tax_subtotal__pre_tax_line_item', |
|
| 601 | + $pre_tax_line_item |
|
| 602 | + ); |
|
| 603 | + self::set_TXN_ID($pre_tax_line_item, $transaction); |
|
| 604 | + $total_line_item->add_child_line_item($pre_tax_line_item); |
|
| 605 | + self::create_event_subtotal($pre_tax_line_item, $transaction); |
|
| 606 | + return $pre_tax_line_item; |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * Creates a line item for the taxes subtotal and finds all the tax prices |
|
| 612 | + * and applies taxes to it |
|
| 613 | + * |
|
| 614 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 615 | + * @param EE_Transaction $transaction |
|
| 616 | + * @return EE_Line_Item |
|
| 617 | + * @throws \EE_Error |
|
| 618 | + */ |
|
| 619 | + protected static function create_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 620 | + { |
|
| 621 | + $tax_line_item = EE_Line_Item::new_instance(array( |
|
| 622 | + 'LIN_code' => 'taxes', |
|
| 623 | + 'LIN_name' => __('Taxes', 'event_espresso'), |
|
| 624 | + 'LIN_type' => EEM_Line_Item::type_tax_sub_total, |
|
| 625 | + 'LIN_order' => 1000,// this should always come last |
|
| 626 | + )); |
|
| 627 | + $tax_line_item = apply_filters( |
|
| 628 | + 'FHEE__EEH_Line_Item__create_taxes_subtotal__tax_line_item', |
|
| 629 | + $tax_line_item |
|
| 630 | + ); |
|
| 631 | + self::set_TXN_ID($tax_line_item, $transaction); |
|
| 632 | + $total_line_item->add_child_line_item($tax_line_item); |
|
| 633 | + // and lastly, add the actual taxes |
|
| 634 | + self::apply_taxes($total_line_item); |
|
| 635 | + return $tax_line_item; |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * Creates a default items subtotal line item |
|
| 641 | + * |
|
| 642 | + * @param EE_Line_Item $pre_tax_line_item |
|
| 643 | + * @param EE_Transaction $transaction |
|
| 644 | + * @param EE_Event $event |
|
| 645 | + * @return EE_Line_Item |
|
| 646 | + * @throws \EE_Error |
|
| 647 | + */ |
|
| 648 | + public static function create_event_subtotal(EE_Line_Item $pre_tax_line_item, $transaction = null, $event = null) |
|
| 649 | + { |
|
| 650 | + $event_line_item = EE_Line_Item::new_instance(array( |
|
| 651 | + 'LIN_code' => self::get_event_code($event), |
|
| 652 | + 'LIN_name' => self::get_event_name($event), |
|
| 653 | + 'LIN_desc' => self::get_event_desc($event), |
|
| 654 | + 'LIN_type' => EEM_Line_Item::type_sub_total, |
|
| 655 | + 'OBJ_type' => 'Event', |
|
| 656 | + 'OBJ_ID' => $event instanceof EE_Event ? $event->ID() : 0 |
|
| 657 | + )); |
|
| 658 | + $event_line_item = apply_filters( |
|
| 659 | + 'FHEE__EEH_Line_Item__create_event_subtotal__event_line_item', |
|
| 660 | + $event_line_item |
|
| 661 | + ); |
|
| 662 | + self::set_TXN_ID($event_line_item, $transaction); |
|
| 663 | + $pre_tax_line_item->add_child_line_item($event_line_item); |
|
| 664 | + return $event_line_item; |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + |
|
| 668 | + /** |
|
| 669 | + * Gets what the event ticket's code SHOULD be |
|
| 670 | + * |
|
| 671 | + * @param EE_Event $event |
|
| 672 | + * @return string |
|
| 673 | + * @throws \EE_Error |
|
| 674 | + */ |
|
| 675 | + public static function get_event_code($event) |
|
| 676 | + { |
|
| 677 | + return 'event-' . ($event instanceof EE_Event ? $event->ID() : '0'); |
|
| 678 | + } |
|
| 679 | + |
|
| 680 | + /** |
|
| 681 | + * Gets the event name |
|
| 682 | + * @param EE_Event $event |
|
| 683 | + * @return string |
|
| 684 | + */ |
|
| 685 | + public static function get_event_name($event) |
|
| 686 | + { |
|
| 687 | + return $event instanceof EE_Event ? mb_substr($event->name(), 0, 245) : __('Event', 'event_espresso'); |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * Gets the event excerpt |
|
| 692 | + * @param EE_Event $event |
|
| 693 | + * @return string |
|
| 694 | + */ |
|
| 695 | + public static function get_event_desc($event) |
|
| 696 | + { |
|
| 697 | + return $event instanceof EE_Event ? $event->short_description() : ''; |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + /** |
|
| 701 | + * Given the grand total line item and a ticket, finds the event sub-total |
|
| 702 | + * line item the ticket's purchase should be added onto |
|
| 703 | + * |
|
| 704 | + * @access public |
|
| 705 | + * @param EE_Line_Item $grand_total the grand total line item |
|
| 706 | + * @param EE_Ticket $ticket |
|
| 707 | + * @throws \EE_Error |
|
| 708 | + * @return EE_Line_Item |
|
| 709 | + */ |
|
| 710 | + public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket) |
|
| 711 | + { |
|
| 712 | + $first_datetime = $ticket->first_datetime(); |
|
| 713 | + if (!$first_datetime instanceof EE_Datetime) { |
|
| 714 | + throw new EE_Error( |
|
| 715 | + sprintf(__('The supplied ticket (ID %d) has no datetimes', 'event_espresso'), $ticket->ID()) |
|
| 716 | + ); |
|
| 717 | + } |
|
| 718 | + $event = $first_datetime->event(); |
|
| 719 | + if (!$event instanceof EE_Event) { |
|
| 720 | + throw new EE_Error( |
|
| 721 | + sprintf( |
|
| 722 | + __('The supplied ticket (ID %d) has no event data associated with it.', 'event_espresso'), |
|
| 723 | + $ticket->ID() |
|
| 724 | + ) |
|
| 725 | + ); |
|
| 726 | + } |
|
| 727 | + $events_sub_total = EEH_Line_Item::get_event_line_item($grand_total, $event); |
|
| 728 | + if (!$events_sub_total instanceof EE_Line_Item) { |
|
| 729 | + throw new EE_Error( |
|
| 730 | + sprintf( |
|
| 731 | + __('There is no events sub-total for ticket %s on total line item %d', 'event_espresso'), |
|
| 732 | + $ticket->ID(), |
|
| 733 | + $grand_total->ID() |
|
| 734 | + ) |
|
| 735 | + ); |
|
| 736 | + } |
|
| 737 | + return $events_sub_total; |
|
| 738 | + } |
|
| 739 | + |
|
| 740 | + |
|
| 741 | + /** |
|
| 742 | + * Gets the event line item |
|
| 743 | + * |
|
| 744 | + * @param EE_Line_Item $grand_total |
|
| 745 | + * @param EE_Event $event |
|
| 746 | + * @return EE_Line_Item for the event subtotal which is a child of $grand_total |
|
| 747 | + * @throws \EE_Error |
|
| 748 | + */ |
|
| 749 | + public static function get_event_line_item(EE_Line_Item $grand_total, $event) |
|
| 750 | + { |
|
| 751 | + /** @type EE_Event $event */ |
|
| 752 | + $event = EEM_Event::instance()->ensure_is_obj($event, true); |
|
| 753 | + $event_line_item = null; |
|
| 754 | + $found = false; |
|
| 755 | + foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) { |
|
| 756 | + // default event subtotal, we should only ever find this the first time this method is called |
|
| 757 | + if (!$event_line_item->OBJ_ID()) { |
|
| 758 | + // let's use this! but first... set the event details |
|
| 759 | + EEH_Line_Item::set_event_subtotal_details($event_line_item, $event); |
|
| 760 | + $found = true; |
|
| 761 | + break; |
|
| 762 | + } elseif ($event_line_item->OBJ_ID() === $event->ID()) { |
|
| 763 | + // found existing line item for this event in the cart, so break out of loop and use this one |
|
| 764 | + $found = true; |
|
| 765 | + break; |
|
| 766 | + } |
|
| 767 | + } |
|
| 768 | + if (!$found) { |
|
| 769 | + // there is no event sub-total yet, so add it |
|
| 770 | + $pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total); |
|
| 771 | + // create a new "event" subtotal below that |
|
| 772 | + $event_line_item = EEH_Line_Item::create_event_subtotal($pre_tax_subtotal, null, $event); |
|
| 773 | + // and set the event details |
|
| 774 | + EEH_Line_Item::set_event_subtotal_details($event_line_item, $event); |
|
| 775 | + } |
|
| 776 | + return $event_line_item; |
|
| 777 | + } |
|
| 778 | + |
|
| 779 | + |
|
| 780 | + /** |
|
| 781 | + * Creates a default items subtotal line item |
|
| 782 | + * |
|
| 783 | + * @param EE_Line_Item $event_line_item |
|
| 784 | + * @param EE_Event $event |
|
| 785 | + * @param EE_Transaction $transaction |
|
| 786 | + * @return EE_Line_Item |
|
| 787 | + * @throws \EE_Error |
|
| 788 | + */ |
|
| 789 | + public static function set_event_subtotal_details( |
|
| 790 | + EE_Line_Item $event_line_item, |
|
| 791 | + EE_Event $event, |
|
| 792 | + $transaction = null |
|
| 793 | + ) { |
|
| 794 | + if ($event instanceof EE_Event) { |
|
| 795 | + $event_line_item->set_code(self::get_event_code($event)); |
|
| 796 | + $event_line_item->set_name(self::get_event_name($event)); |
|
| 797 | + $event_line_item->set_desc(self::get_event_desc($event)); |
|
| 798 | + $event_line_item->set_OBJ_ID($event->ID()); |
|
| 799 | + } |
|
| 800 | + self::set_TXN_ID($event_line_item, $transaction); |
|
| 801 | + } |
|
| 802 | + |
|
| 803 | + |
|
| 804 | + /** |
|
| 805 | + * Finds what taxes should apply, adds them as tax line items under the taxes sub-total, |
|
| 806 | + * and recalculates the taxes sub-total and the grand total. Resets the taxes, so |
|
| 807 | + * any old taxes are removed |
|
| 808 | + * |
|
| 809 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 810 | + * @throws \EE_Error |
|
| 811 | + */ |
|
| 812 | + public static function apply_taxes(EE_Line_Item $total_line_item) |
|
| 813 | + { |
|
| 814 | + /** @type EEM_Price $EEM_Price */ |
|
| 815 | + $EEM_Price = EE_Registry::instance()->load_model('Price'); |
|
| 816 | + // get array of taxes via Price Model |
|
| 817 | + $ordered_taxes = $EEM_Price->get_all_prices_that_are_taxes(); |
|
| 818 | + ksort($ordered_taxes); |
|
| 819 | + $taxes_line_item = self::get_taxes_subtotal($total_line_item); |
|
| 820 | + // just to be safe, remove its old tax line items |
|
| 821 | + $taxes_line_item->delete_children_line_items(); |
|
| 822 | + // loop thru taxes |
|
| 823 | + foreach ($ordered_taxes as $order => $taxes) { |
|
| 824 | + foreach ($taxes as $tax) { |
|
| 825 | + if ($tax instanceof EE_Price) { |
|
| 826 | + $tax_line_item = EE_Line_Item::new_instance( |
|
| 827 | + array( |
|
| 828 | + 'LIN_name' => $tax->name(), |
|
| 829 | + 'LIN_desc' => $tax->desc(), |
|
| 830 | + 'LIN_percent' => $tax->amount(), |
|
| 831 | + 'LIN_is_taxable' => false, |
|
| 832 | + 'LIN_order' => $order, |
|
| 833 | + 'LIN_total' => 0, |
|
| 834 | + 'LIN_type' => EEM_Line_Item::type_tax, |
|
| 835 | + 'OBJ_type' => 'Price', |
|
| 836 | + 'OBJ_ID' => $tax->ID() |
|
| 837 | + ) |
|
| 838 | + ); |
|
| 839 | + $tax_line_item = apply_filters( |
|
| 840 | + 'FHEE__EEH_Line_Item__apply_taxes__tax_line_item', |
|
| 841 | + $tax_line_item |
|
| 842 | + ); |
|
| 843 | + $taxes_line_item->add_child_line_item($tax_line_item); |
|
| 844 | + } |
|
| 845 | + } |
|
| 846 | + } |
|
| 847 | + $total_line_item->recalculate_total_including_taxes(); |
|
| 848 | + } |
|
| 849 | + |
|
| 850 | + |
|
| 851 | + /** |
|
| 852 | + * Ensures that taxes have been applied to the order, if not applies them. |
|
| 853 | + * Returns the total amount of tax |
|
| 854 | + * |
|
| 855 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 856 | + * @return float |
|
| 857 | + * @throws \EE_Error |
|
| 858 | + */ |
|
| 859 | + public static function ensure_taxes_applied($total_line_item) |
|
| 860 | + { |
|
| 861 | + $taxes_subtotal = self::get_taxes_subtotal($total_line_item); |
|
| 862 | + if (!$taxes_subtotal->children()) { |
|
| 863 | + self::apply_taxes($total_line_item); |
|
| 864 | + } |
|
| 865 | + return $taxes_subtotal->total(); |
|
| 866 | + } |
|
| 867 | + |
|
| 868 | + |
|
| 869 | + /** |
|
| 870 | + * Deletes ALL children of the passed line item |
|
| 871 | + * |
|
| 872 | + * @param EE_Line_Item $parent_line_item |
|
| 873 | + * @return bool |
|
| 874 | + * @throws \EE_Error |
|
| 875 | + */ |
|
| 876 | + public static function delete_all_child_items(EE_Line_Item $parent_line_item) |
|
| 877 | + { |
|
| 878 | + $deleted = 0; |
|
| 879 | + foreach ($parent_line_item->children() as $child_line_item) { |
|
| 880 | + if ($child_line_item instanceof EE_Line_Item) { |
|
| 881 | + $deleted += EEH_Line_Item::delete_all_child_items($child_line_item); |
|
| 882 | + if ($child_line_item->ID()) { |
|
| 883 | + $child_line_item->delete(); |
|
| 884 | + unset($child_line_item); |
|
| 885 | + } else { |
|
| 886 | + $parent_line_item->delete_child_line_item($child_line_item->code()); |
|
| 887 | + } |
|
| 888 | + $deleted++; |
|
| 889 | + } |
|
| 890 | + } |
|
| 891 | + return $deleted; |
|
| 892 | + } |
|
| 893 | + |
|
| 894 | + |
|
| 895 | + /** |
|
| 896 | + * Deletes the line items as indicated by the line item code(s) provided, |
|
| 897 | + * regardless of where they're found in the line item tree. Automatically |
|
| 898 | + * re-calculates the line item totals and updates the related transaction. But |
|
| 899 | + * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
|
| 900 | + * should probably change because of this). |
|
| 901 | + * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
|
| 902 | + * after using this, to keep the registration final prices in-sync with the transaction's total. |
|
| 903 | + * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
|
| 904 | + * @param array|bool|string $line_item_codes |
|
| 905 | + * @return int number of items successfully removed |
|
| 906 | + */ |
|
| 907 | + public static function delete_items(EE_Line_Item $total_line_item, $line_item_codes = false) |
|
| 908 | + { |
|
| 909 | + |
|
| 910 | + if ($total_line_item->type() !== EEM_Line_Item::type_total) { |
|
| 911 | + EE_Error::doing_it_wrong( |
|
| 912 | + 'EEH_Line_Item::delete_items', |
|
| 913 | + __( |
|
| 914 | + 'This static method should only be called with a TOTAL line item, otherwise we won\'t recalculate the totals correctly', |
|
| 915 | + 'event_espresso' |
|
| 916 | + ), |
|
| 917 | + '4.6.18' |
|
| 918 | + ); |
|
| 919 | + } |
|
| 920 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 921 | + |
|
| 922 | + // check if only a single line_item_id was passed |
|
| 923 | + if (!empty($line_item_codes) && !is_array($line_item_codes)) { |
|
| 924 | + // place single line_item_id in an array to appear as multiple line_item_ids |
|
| 925 | + $line_item_codes = array($line_item_codes); |
|
| 926 | + } |
|
| 927 | + $removals = 0; |
|
| 928 | + // cycle thru line_item_ids |
|
| 929 | + foreach ($line_item_codes as $line_item_id) { |
|
| 930 | + $removals += $total_line_item->delete_child_line_item($line_item_id); |
|
| 931 | + } |
|
| 932 | + |
|
| 933 | + if ($removals > 0) { |
|
| 934 | + $total_line_item->recalculate_taxes_and_tax_total(); |
|
| 935 | + return $removals; |
|
| 936 | + } else { |
|
| 937 | + return false; |
|
| 938 | + } |
|
| 939 | + } |
|
| 940 | + |
|
| 941 | + |
|
| 942 | + /** |
|
| 943 | + * Overwrites the previous tax by clearing out the old taxes, and creates a new |
|
| 944 | + * tax and updates the total line item accordingly |
|
| 945 | + * |
|
| 946 | + * @param EE_Line_Item $total_line_item |
|
| 947 | + * @param float $amount |
|
| 948 | + * @param string $name |
|
| 949 | + * @param string $description |
|
| 950 | + * @param string $code |
|
| 951 | + * @param boolean $add_to_existing_line_item |
|
| 952 | + * if true, and a duplicate line item with the same code is found, |
|
| 953 | + * $amount will be added onto it; otherwise will simply set the taxes to match $amount |
|
| 954 | + * @return EE_Line_Item the new tax line item created |
|
| 955 | + * @throws \EE_Error |
|
| 956 | + */ |
|
| 957 | + public static function set_total_tax_to( |
|
| 958 | + EE_Line_Item $total_line_item, |
|
| 959 | + $amount, |
|
| 960 | + $name = null, |
|
| 961 | + $description = null, |
|
| 962 | + $code = null, |
|
| 963 | + $add_to_existing_line_item = false |
|
| 964 | + ) { |
|
| 965 | + $tax_subtotal = self::get_taxes_subtotal($total_line_item); |
|
| 966 | + $taxable_total = $total_line_item->taxable_total(); |
|
| 967 | + |
|
| 968 | + if ($add_to_existing_line_item) { |
|
| 969 | + $new_tax = $tax_subtotal->get_child_line_item($code); |
|
| 970 | + EEM_Line_Item::instance()->delete( |
|
| 971 | + array(array('LIN_code' => array('!=', $code), 'LIN_parent' => $tax_subtotal->ID())) |
|
| 972 | + ); |
|
| 973 | + } else { |
|
| 974 | + $new_tax = null; |
|
| 975 | + $tax_subtotal->delete_children_line_items(); |
|
| 976 | + } |
|
| 977 | + if ($new_tax) { |
|
| 978 | + $new_tax->set_total($new_tax->total() + $amount); |
|
| 979 | + $new_tax->set_percent($taxable_total ? $new_tax->total() / $taxable_total * 100 : 0); |
|
| 980 | + } else { |
|
| 981 | + // no existing tax item. Create it |
|
| 982 | + $new_tax = EE_Line_Item::new_instance(array( |
|
| 983 | + 'TXN_ID' => $total_line_item->TXN_ID(), |
|
| 984 | + 'LIN_name' => $name ? $name : __('Tax', 'event_espresso'), |
|
| 985 | + 'LIN_desc' => $description ? $description : '', |
|
| 986 | + 'LIN_percent' => $taxable_total ? ($amount / $taxable_total * 100) : 0, |
|
| 987 | + 'LIN_total' => $amount, |
|
| 988 | + 'LIN_parent' => $tax_subtotal->ID(), |
|
| 989 | + 'LIN_type' => EEM_Line_Item::type_tax, |
|
| 990 | + 'LIN_code' => $code |
|
| 991 | + )); |
|
| 992 | + } |
|
| 993 | + |
|
| 994 | + $new_tax = apply_filters( |
|
| 995 | + 'FHEE__EEH_Line_Item__set_total_tax_to__new_tax_subtotal', |
|
| 996 | + $new_tax, |
|
| 997 | + $total_line_item |
|
| 998 | + ); |
|
| 999 | + $new_tax->save(); |
|
| 1000 | + $tax_subtotal->set_total($new_tax->total()); |
|
| 1001 | + $tax_subtotal->save(); |
|
| 1002 | + $total_line_item->recalculate_total_including_taxes(); |
|
| 1003 | + return $new_tax; |
|
| 1004 | + } |
|
| 1005 | + |
|
| 1006 | + |
|
| 1007 | + /** |
|
| 1008 | + * Makes all the line items which are children of $line_item taxable (or not). |
|
| 1009 | + * Does NOT save the line items |
|
| 1010 | + * @param EE_Line_Item $line_item |
|
| 1011 | + * @param string $code_substring_for_whitelist if this string is part of the line item's code |
|
| 1012 | + * it will be whitelisted (ie, except from becoming taxable) |
|
| 1013 | + * @param boolean $taxable |
|
| 1014 | + */ |
|
| 1015 | + public static function set_line_items_taxable( |
|
| 1016 | + EE_Line_Item $line_item, |
|
| 1017 | + $taxable = true, |
|
| 1018 | + $code_substring_for_whitelist = null |
|
| 1019 | + ) { |
|
| 1020 | + $whitelisted = false; |
|
| 1021 | + if ($code_substring_for_whitelist !== null) { |
|
| 1022 | + $whitelisted = strpos($line_item->code(), $code_substring_for_whitelist) !== false ? true : false; |
|
| 1023 | + } |
|
| 1024 | + if (!$whitelisted && $line_item->is_line_item()) { |
|
| 1025 | + $line_item->set_is_taxable($taxable); |
|
| 1026 | + } |
|
| 1027 | + foreach ($line_item->children() as $child_line_item) { |
|
| 1028 | + EEH_Line_Item::set_line_items_taxable($child_line_item, $taxable, $code_substring_for_whitelist); |
|
| 1029 | + } |
|
| 1030 | + } |
|
| 1031 | + |
|
| 1032 | + |
|
| 1033 | + /** |
|
| 1034 | + * Gets all descendants that are event subtotals |
|
| 1035 | + * |
|
| 1036 | + * @uses EEH_Line_Item::get_subtotals_of_object_type() |
|
| 1037 | + * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1038 | + * @return EE_Line_Item[] |
|
| 1039 | + */ |
|
| 1040 | + public static function get_event_subtotals(EE_Line_Item $parent_line_item) |
|
| 1041 | + { |
|
| 1042 | + return self::get_subtotals_of_object_type($parent_line_item, 'Event'); |
|
| 1043 | + } |
|
| 1044 | + |
|
| 1045 | + |
|
| 1046 | + /** |
|
| 1047 | + * Gets all descendants subtotals that match the supplied object type |
|
| 1048 | + * |
|
| 1049 | + * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1050 | + * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1051 | + * @param string $obj_type |
|
| 1052 | + * @return EE_Line_Item[] |
|
| 1053 | + */ |
|
| 1054 | + public static function get_subtotals_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '') |
|
| 1055 | + { |
|
| 1056 | + return self::_get_descendants_by_type_and_object_type( |
|
| 1057 | + $parent_line_item, |
|
| 1058 | + EEM_Line_Item::type_sub_total, |
|
| 1059 | + $obj_type |
|
| 1060 | + ); |
|
| 1061 | + } |
|
| 1062 | + |
|
| 1063 | + |
|
| 1064 | + /** |
|
| 1065 | + * Gets all descendants that are tickets |
|
| 1066 | + * |
|
| 1067 | + * @uses EEH_Line_Item::get_line_items_of_object_type() |
|
| 1068 | + * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1069 | + * @return EE_Line_Item[] |
|
| 1070 | + */ |
|
| 1071 | + public static function get_ticket_line_items(EE_Line_Item $parent_line_item) |
|
| 1072 | + { |
|
| 1073 | + return self::get_line_items_of_object_type($parent_line_item, 'Ticket'); |
|
| 1074 | + } |
|
| 1075 | + |
|
| 1076 | + |
|
| 1077 | + /** |
|
| 1078 | + * Gets all descendants subtotals that match the supplied object type |
|
| 1079 | + * |
|
| 1080 | + * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1081 | + * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1082 | + * @param string $obj_type |
|
| 1083 | + * @return EE_Line_Item[] |
|
| 1084 | + */ |
|
| 1085 | + public static function get_line_items_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '') |
|
| 1086 | + { |
|
| 1087 | + return self::_get_descendants_by_type_and_object_type($parent_line_item, EEM_Line_Item::type_line_item, $obj_type); |
|
| 1088 | + } |
|
| 1089 | + |
|
| 1090 | + |
|
| 1091 | + /** |
|
| 1092 | + * Gets all the descendants (ie, children or children of children etc) that are of the type 'tax' |
|
| 1093 | + * @uses EEH_Line_Item::get_descendants_of_type() |
|
| 1094 | + * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1095 | + * @return EE_Line_Item[] |
|
| 1096 | + */ |
|
| 1097 | + public static function get_tax_descendants(EE_Line_Item $parent_line_item) |
|
| 1098 | + { |
|
| 1099 | + return EEH_Line_Item::get_descendants_of_type($parent_line_item, EEM_Line_Item::type_tax); |
|
| 1100 | + } |
|
| 1101 | + |
|
| 1102 | + |
|
| 1103 | + /** |
|
| 1104 | + * Gets all the real items purchased which are children of this item |
|
| 1105 | + * @uses EEH_Line_Item::get_descendants_of_type() |
|
| 1106 | + * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1107 | + * @return EE_Line_Item[] |
|
| 1108 | + */ |
|
| 1109 | + public static function get_line_item_descendants(EE_Line_Item $parent_line_item) |
|
| 1110 | + { |
|
| 1111 | + return EEH_Line_Item::get_descendants_of_type($parent_line_item, EEM_Line_Item::type_line_item); |
|
| 1112 | + } |
|
| 1113 | + |
|
| 1114 | + |
|
| 1115 | + /** |
|
| 1116 | + * Gets all descendants of supplied line item that match the supplied line item type |
|
| 1117 | + * |
|
| 1118 | + * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1119 | + * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1120 | + * @param string $line_item_type one of the EEM_Line_Item constants |
|
| 1121 | + * @return EE_Line_Item[] |
|
| 1122 | + */ |
|
| 1123 | + public static function get_descendants_of_type(EE_Line_Item $parent_line_item, $line_item_type) |
|
| 1124 | + { |
|
| 1125 | + return self::_get_descendants_by_type_and_object_type($parent_line_item, $line_item_type, null); |
|
| 1126 | + } |
|
| 1127 | + |
|
| 1128 | + |
|
| 1129 | + /** |
|
| 1130 | + * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type as well |
|
| 1131 | + * |
|
| 1132 | + * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1133 | + * @param string $line_item_type one of the EEM_Line_Item constants |
|
| 1134 | + * @param string | NULL $obj_type object model class name (minus prefix) or NULL to ignore object type when searching |
|
| 1135 | + * @return EE_Line_Item[] |
|
| 1136 | + */ |
|
| 1137 | + protected static function _get_descendants_by_type_and_object_type( |
|
| 1138 | + EE_Line_Item $parent_line_item, |
|
| 1139 | + $line_item_type, |
|
| 1140 | + $obj_type = null |
|
| 1141 | + ) { |
|
| 1142 | + $objects = array(); |
|
| 1143 | + foreach ($parent_line_item->children() as $child_line_item) { |
|
| 1144 | + if ($child_line_item instanceof EE_Line_Item) { |
|
| 1145 | + if ($child_line_item->type() === $line_item_type |
|
| 1146 | + && ( |
|
| 1147 | + $child_line_item->OBJ_type() === $obj_type || $obj_type === null |
|
| 1148 | + ) |
|
| 1149 | + ) { |
|
| 1150 | + $objects[] = $child_line_item; |
|
| 1151 | + } else { |
|
| 1152 | + // go-through-all-its children looking for more matches |
|
| 1153 | + $objects = array_merge( |
|
| 1154 | + $objects, |
|
| 1155 | + self::_get_descendants_by_type_and_object_type( |
|
| 1156 | + $child_line_item, |
|
| 1157 | + $line_item_type, |
|
| 1158 | + $obj_type |
|
| 1159 | + ) |
|
| 1160 | + ); |
|
| 1161 | + } |
|
| 1162 | + } |
|
| 1163 | + } |
|
| 1164 | + return $objects; |
|
| 1165 | + } |
|
| 1166 | + |
|
| 1167 | + |
|
| 1168 | + /** |
|
| 1169 | + * Gets all descendants subtotals that match the supplied object type |
|
| 1170 | + * |
|
| 1171 | + * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
|
| 1172 | + * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1173 | + * @param string $OBJ_type object type (like Event) |
|
| 1174 | + * @param array $OBJ_IDs array of OBJ_IDs |
|
| 1175 | + * @return EE_Line_Item[] |
|
| 1176 | + */ |
|
| 1177 | + public static function get_line_items_by_object_type_and_IDs( |
|
| 1178 | + EE_Line_Item $parent_line_item, |
|
| 1179 | + $OBJ_type = '', |
|
| 1180 | + $OBJ_IDs = array() |
|
| 1181 | + ) { |
|
| 1182 | + return self::_get_descendants_by_object_type_and_object_ID($parent_line_item, $OBJ_type, $OBJ_IDs); |
|
| 1183 | + } |
|
| 1184 | + |
|
| 1185 | + |
|
| 1186 | + /** |
|
| 1187 | + * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type as well |
|
| 1188 | + * |
|
| 1189 | + * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1190 | + * @param string $OBJ_type object type (like Event) |
|
| 1191 | + * @param array $OBJ_IDs array of OBJ_IDs |
|
| 1192 | + * @return EE_Line_Item[] |
|
| 1193 | + */ |
|
| 1194 | + protected static function _get_descendants_by_object_type_and_object_ID( |
|
| 1195 | + EE_Line_Item $parent_line_item, |
|
| 1196 | + $OBJ_type, |
|
| 1197 | + $OBJ_IDs |
|
| 1198 | + ) { |
|
| 1199 | + $objects = array(); |
|
| 1200 | + foreach ($parent_line_item->children() as $child_line_item) { |
|
| 1201 | + if ($child_line_item instanceof EE_Line_Item) { |
|
| 1202 | + if ($child_line_item->OBJ_type() === $OBJ_type |
|
| 1203 | + && is_array($OBJ_IDs) |
|
| 1204 | + && in_array($child_line_item->OBJ_ID(), $OBJ_IDs) |
|
| 1205 | + ) { |
|
| 1206 | + $objects[] = $child_line_item; |
|
| 1207 | + } else { |
|
| 1208 | + // go-through-all-its children looking for more matches |
|
| 1209 | + $objects = array_merge( |
|
| 1210 | + $objects, |
|
| 1211 | + self::_get_descendants_by_object_type_and_object_ID( |
|
| 1212 | + $child_line_item, |
|
| 1213 | + $OBJ_type, |
|
| 1214 | + $OBJ_IDs |
|
| 1215 | + ) |
|
| 1216 | + ); |
|
| 1217 | + } |
|
| 1218 | + } |
|
| 1219 | + } |
|
| 1220 | + return $objects; |
|
| 1221 | + } |
|
| 1222 | + |
|
| 1223 | + |
|
| 1224 | + /** |
|
| 1225 | + * Uses a breadth-first-search in order to find the nearest descendant of |
|
| 1226 | + * the specified type and returns it, else NULL |
|
| 1227 | + * |
|
| 1228 | + * @uses EEH_Line_Item::_get_nearest_descendant() |
|
| 1229 | + * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1230 | + * @param string $type like one of the EEM_Line_Item::type_* |
|
| 1231 | + * @return EE_Line_Item |
|
| 1232 | + */ |
|
| 1233 | + public static function get_nearest_descendant_of_type(EE_Line_Item $parent_line_item, $type) |
|
| 1234 | + { |
|
| 1235 | + return self::_get_nearest_descendant($parent_line_item, 'LIN_type', $type); |
|
| 1236 | + } |
|
| 1237 | + |
|
| 1238 | + |
|
| 1239 | + /** |
|
| 1240 | + * Uses a breadth-first-search in order to find the nearest descendant |
|
| 1241 | + * having the specified LIN_code and returns it, else NULL |
|
| 1242 | + * |
|
| 1243 | + * @uses EEH_Line_Item::_get_nearest_descendant() |
|
| 1244 | + * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1245 | + * @param string $code any value used for LIN_code |
|
| 1246 | + * @return EE_Line_Item |
|
| 1247 | + */ |
|
| 1248 | + public static function get_nearest_descendant_having_code(EE_Line_Item $parent_line_item, $code) |
|
| 1249 | + { |
|
| 1250 | + return self::_get_nearest_descendant($parent_line_item, 'LIN_code', $code); |
|
| 1251 | + } |
|
| 1252 | + |
|
| 1253 | + |
|
| 1254 | + /** |
|
| 1255 | + * Uses a breadth-first-search in order to find the nearest descendant |
|
| 1256 | + * having the specified LIN_code and returns it, else NULL |
|
| 1257 | + * |
|
| 1258 | + * @param \EE_Line_Item $parent_line_item - the line item to find descendants of |
|
| 1259 | + * @param string $search_field name of EE_Line_Item property |
|
| 1260 | + * @param string $value any value stored in $search_field |
|
| 1261 | + * @return EE_Line_Item |
|
| 1262 | + */ |
|
| 1263 | + protected static function _get_nearest_descendant(EE_Line_Item $parent_line_item, $search_field, $value) |
|
| 1264 | + { |
|
| 1265 | + foreach ($parent_line_item->children() as $child) { |
|
| 1266 | + if ($child->get($search_field) == $value) { |
|
| 1267 | + return $child; |
|
| 1268 | + } |
|
| 1269 | + } |
|
| 1270 | + foreach ($parent_line_item->children() as $child) { |
|
| 1271 | + $descendant_found = self::_get_nearest_descendant($child, $search_field, $value); |
|
| 1272 | + if ($descendant_found) { |
|
| 1273 | + return $descendant_found; |
|
| 1274 | + } |
|
| 1275 | + } |
|
| 1276 | + return null; |
|
| 1277 | + } |
|
| 1278 | + |
|
| 1279 | + |
|
| 1280 | + /** |
|
| 1281 | + * if passed line item has a TXN ID, uses that to jump directly to the grand total line item for the transaction, |
|
| 1282 | + * else recursively walks up the line item tree until a parent of type total is found, |
|
| 1283 | + * |
|
| 1284 | + * @param EE_Line_Item $line_item |
|
| 1285 | + * @return \EE_Line_Item |
|
| 1286 | + * @throws \EE_Error |
|
| 1287 | + */ |
|
| 1288 | + public static function find_transaction_grand_total_for_line_item(EE_Line_Item $line_item) |
|
| 1289 | + { |
|
| 1290 | + if ($line_item->TXN_ID()) { |
|
| 1291 | + $total_line_item = $line_item->transaction()->total_line_item(false); |
|
| 1292 | + if ($total_line_item instanceof EE_Line_Item) { |
|
| 1293 | + return $total_line_item; |
|
| 1294 | + } |
|
| 1295 | + } else { |
|
| 1296 | + $line_item_parent = $line_item->parent(); |
|
| 1297 | + if ($line_item_parent instanceof EE_Line_Item) { |
|
| 1298 | + if ($line_item_parent->is_total()) { |
|
| 1299 | + return $line_item_parent; |
|
| 1300 | + } |
|
| 1301 | + return EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item_parent); |
|
| 1302 | + } |
|
| 1303 | + } |
|
| 1304 | + throw new EE_Error( |
|
| 1305 | + sprintf( |
|
| 1306 | + __('A valid grand total for line item %1$d was not found.', 'event_espresso'), |
|
| 1307 | + $line_item->ID() |
|
| 1308 | + ) |
|
| 1309 | + ); |
|
| 1310 | + } |
|
| 1311 | + |
|
| 1312 | + |
|
| 1313 | + /** |
|
| 1314 | + * Prints out a representation of the line item tree |
|
| 1315 | + * |
|
| 1316 | + * @param EE_Line_Item $line_item |
|
| 1317 | + * @param int $indentation |
|
| 1318 | + * @return void |
|
| 1319 | + * @throws \EE_Error |
|
| 1320 | + */ |
|
| 1321 | + public static function visualize(EE_Line_Item $line_item, $indentation = 0) |
|
| 1322 | + { |
|
| 1323 | + echo defined('EE_TESTS_DIR') ? "\n" : '<br />'; |
|
| 1324 | + if (!$indentation) { |
|
| 1325 | + echo defined('EE_TESTS_DIR') ? "\n" : '<br />'; |
|
| 1326 | + } |
|
| 1327 | + for ($i = 0; $i < $indentation; $i++) { |
|
| 1328 | + echo ". "; |
|
| 1329 | + } |
|
| 1330 | + $breakdown = ''; |
|
| 1331 | + if ($line_item->is_line_item()) { |
|
| 1332 | + if ($line_item->is_percent()) { |
|
| 1333 | + $breakdown = "{$line_item->percent()}%"; |
|
| 1334 | + } else { |
|
| 1335 | + $breakdown = '$' . "{$line_item->unit_price()} x {$line_item->quantity()}"; |
|
| 1336 | + } |
|
| 1337 | + } |
|
| 1338 | + echo $line_item->name() . " [ ID:{$line_item->ID()} | qty:{$line_item->quantity()} ] {$line_item->type()} : " . '$' . "{$line_item->total()}"; |
|
| 1339 | + if ($breakdown) { |
|
| 1340 | + echo " ( {$breakdown} )"; |
|
| 1341 | + } |
|
| 1342 | + if ($line_item->is_taxable()) { |
|
| 1343 | + echo " * taxable"; |
|
| 1344 | + } |
|
| 1345 | + if ($line_item->children()) { |
|
| 1346 | + foreach ($line_item->children() as $child) { |
|
| 1347 | + self::visualize($child, $indentation + 1); |
|
| 1348 | + } |
|
| 1349 | + } |
|
| 1350 | + } |
|
| 1351 | + |
|
| 1352 | + |
|
| 1353 | + /** |
|
| 1354 | + * Calculates the registration's final price, taking into account that they |
|
| 1355 | + * need to not only help pay for their OWN ticket, but also any transaction-wide surcharges and taxes, |
|
| 1356 | + * and receive a portion of any transaction-wide discounts. |
|
| 1357 | + * eg1, if I buy a $1 ticket and brent buys a $9 ticket, and we receive a $5 discount |
|
| 1358 | + * then I'll get 1/10 of that $5 discount, which is $0.50, and brent will get |
|
| 1359 | + * 9/10ths of that $5 discount, which is $4.50. So my final price should be $0.50 |
|
| 1360 | + * and brent's final price should be $5.50. |
|
| 1361 | + * |
|
| 1362 | + * In order to do this, we basically need to traverse the line item tree calculating |
|
| 1363 | + * the running totals (just as if we were recalculating the total), but when we identify |
|
| 1364 | + * regular line items, we need to keep track of their share of the grand total. |
|
| 1365 | + * Also, we need to keep track of the TAXABLE total for each ticket purchase, so |
|
| 1366 | + * we can know how to apply taxes to it. (Note: "taxable total" does not equal the "pretax total" |
|
| 1367 | + * when there are non-taxable items; otherwise they would be the same) |
|
| 1368 | + * |
|
| 1369 | + * @param EE_Line_Item $line_item |
|
| 1370 | + * @param array $billable_ticket_quantities array of EE_Ticket IDs and their corresponding quantity that |
|
| 1371 | + * can be included in price calculations at this moment |
|
| 1372 | + * @return array keys are line items for tickets IDs and values are their share of the running total, |
|
| 1373 | + * plus the key 'total', and 'taxable' which also has keys of all the ticket IDs. Eg |
|
| 1374 | + * array( |
|
| 1375 | + * 12 => 4.3 |
|
| 1376 | + * 23 => 8.0 |
|
| 1377 | + * 'total' => 16.6, |
|
| 1378 | + * 'taxable' => array( |
|
| 1379 | + * 12 => 10, |
|
| 1380 | + * 23 => 4 |
|
| 1381 | + * ). |
|
| 1382 | + * So to find which registrations have which final price, we need to find which line item |
|
| 1383 | + * is theirs, which can be done with |
|
| 1384 | + * `EEM_Line_Item::instance()->get_line_item_for_registration( $registration );` |
|
| 1385 | + */ |
|
| 1386 | + public static function calculate_reg_final_prices_per_line_item(EE_Line_Item $line_item, $billable_ticket_quantities = array()) |
|
| 1387 | + { |
|
| 1388 | + // init running grand total if not already |
|
| 1389 | + if (!isset($running_totals['total'])) { |
|
| 1390 | + $running_totals['total'] = 0; |
|
| 1391 | + } |
|
| 1392 | + if (!isset($running_totals['taxable'])) { |
|
| 1393 | + $running_totals['taxable'] = array('total' => 0); |
|
| 1394 | + } |
|
| 1395 | + foreach ($line_item->children() as $child_line_item) { |
|
| 1396 | + switch ($child_line_item->type()) { |
|
| 1397 | + case EEM_Line_Item::type_sub_total: |
|
| 1398 | + $running_totals_from_subtotal = EEH_Line_Item::calculate_reg_final_prices_per_line_item($child_line_item, $billable_ticket_quantities); |
|
| 1399 | + // combine arrays but preserve numeric keys |
|
| 1400 | + $running_totals = array_replace_recursive($running_totals_from_subtotal, $running_totals); |
|
| 1401 | + $running_totals['total'] += $running_totals_from_subtotal['total']; |
|
| 1402 | + $running_totals['taxable']['total'] += $running_totals_from_subtotal['taxable']['total']; |
|
| 1403 | + break; |
|
| 1404 | + |
|
| 1405 | + case EEM_Line_Item::type_tax_sub_total: |
|
| 1406 | + // find how much the taxes percentage is |
|
| 1407 | + if ($child_line_item->percent() !== 0) { |
|
| 1408 | + $tax_percent_decimal = $child_line_item->percent() / 100; |
|
| 1409 | + } else { |
|
| 1410 | + $tax_percent_decimal = EE_Taxes::get_total_taxes_percentage() / 100; |
|
| 1411 | + } |
|
| 1412 | + // and apply to all the taxable totals, and add to the pretax totals |
|
| 1413 | + foreach ($running_totals as $line_item_id => $this_running_total) { |
|
| 1414 | + // "total" and "taxable" array key is an exception |
|
| 1415 | + if ($line_item_id === 'taxable') { |
|
| 1416 | + continue; |
|
| 1417 | + } |
|
| 1418 | + $taxable_total = $running_totals['taxable'][ $line_item_id ]; |
|
| 1419 | + $running_totals[ $line_item_id ] += ($taxable_total * $tax_percent_decimal); |
|
| 1420 | + } |
|
| 1421 | + break; |
|
| 1422 | + |
|
| 1423 | + case EEM_Line_Item::type_line_item: |
|
| 1424 | + // ticket line items or ???? |
|
| 1425 | + if ($child_line_item->OBJ_type() === 'Ticket') { |
|
| 1426 | + // kk it's a ticket |
|
| 1427 | + if (isset($running_totals[ $child_line_item->ID() ])) { |
|
| 1428 | + // huh? that shouldn't happen. |
|
| 1429 | + $running_totals['total'] += $child_line_item->total(); |
|
| 1430 | + } else { |
|
| 1431 | + // its not in our running totals yet. great. |
|
| 1432 | + if ($child_line_item->is_taxable()) { |
|
| 1433 | + $taxable_amount = $child_line_item->unit_price(); |
|
| 1434 | + } else { |
|
| 1435 | + $taxable_amount = 0; |
|
| 1436 | + } |
|
| 1437 | + // are we only calculating totals for some tickets? |
|
| 1438 | + if (isset($billable_ticket_quantities[ $child_line_item->OBJ_ID() ])) { |
|
| 1439 | + $quantity = $billable_ticket_quantities[ $child_line_item->OBJ_ID() ]; |
|
| 1440 | + $running_totals[ $child_line_item->ID() ] = $quantity |
|
| 1441 | + ? $child_line_item->unit_price() |
|
| 1442 | + : 0; |
|
| 1443 | + $running_totals['taxable'][ $child_line_item->ID() ] = $quantity |
|
| 1444 | + ? $taxable_amount |
|
| 1445 | + : 0; |
|
| 1446 | + } else { |
|
| 1447 | + $quantity = $child_line_item->quantity(); |
|
| 1448 | + $running_totals[ $child_line_item->ID() ] = $child_line_item->unit_price(); |
|
| 1449 | + $running_totals['taxable'][ $child_line_item->ID() ] = $taxable_amount; |
|
| 1450 | + } |
|
| 1451 | + $running_totals['taxable']['total'] += $taxable_amount * $quantity; |
|
| 1452 | + $running_totals['total'] += $child_line_item->unit_price() * $quantity; |
|
| 1453 | + } |
|
| 1454 | + } else { |
|
| 1455 | + // it's some other type of item added to the cart |
|
| 1456 | + // it should affect the running totals |
|
| 1457 | + // basically we want to convert it into a PERCENT modifier. Because |
|
| 1458 | + // more clearly affect all registration's final price equally |
|
| 1459 | + $line_items_percent_of_running_total = $running_totals['total'] > 0 |
|
| 1460 | + ? ($child_line_item->total() / $running_totals['total']) + 1 |
|
| 1461 | + : 1; |
|
| 1462 | + foreach ($running_totals as $line_item_id => $this_running_total) { |
|
| 1463 | + // the "taxable" array key is an exception |
|
| 1464 | + if ($line_item_id === 'taxable') { |
|
| 1465 | + continue; |
|
| 1466 | + } |
|
| 1467 | + // update the running totals |
|
| 1468 | + // yes this actually even works for the running grand total! |
|
| 1469 | + $running_totals[ $line_item_id ] = |
|
| 1470 | + $line_items_percent_of_running_total * $this_running_total; |
|
| 1471 | + |
|
| 1472 | + if ($child_line_item->is_taxable()) { |
|
| 1473 | + $running_totals['taxable'][ $line_item_id ] = |
|
| 1474 | + $line_items_percent_of_running_total * $running_totals['taxable'][ $line_item_id ]; |
|
| 1475 | + } |
|
| 1476 | + } |
|
| 1477 | + } |
|
| 1478 | + break; |
|
| 1479 | + } |
|
| 1480 | + } |
|
| 1481 | + return $running_totals; |
|
| 1482 | + } |
|
| 1483 | + |
|
| 1484 | + |
|
| 1485 | + /** |
|
| 1486 | + * @param \EE_Line_Item $total_line_item |
|
| 1487 | + * @param \EE_Line_Item $ticket_line_item |
|
| 1488 | + * @return float | null |
|
| 1489 | + * @throws \OutOfRangeException |
|
| 1490 | + */ |
|
| 1491 | + public static function calculate_final_price_for_ticket_line_item(\EE_Line_Item $total_line_item, \EE_Line_Item $ticket_line_item) |
|
| 1492 | + { |
|
| 1493 | + static $final_prices_per_ticket_line_item = array(); |
|
| 1494 | + if (empty($final_prices_per_ticket_line_item)) { |
|
| 1495 | + $final_prices_per_ticket_line_item = \EEH_Line_Item::calculate_reg_final_prices_per_line_item( |
|
| 1496 | + $total_line_item |
|
| 1497 | + ); |
|
| 1498 | + } |
|
| 1499 | + // ok now find this new registration's final price |
|
| 1500 | + if (isset($final_prices_per_ticket_line_item[ $ticket_line_item->ID() ])) { |
|
| 1501 | + return $final_prices_per_ticket_line_item[ $ticket_line_item->ID() ]; |
|
| 1502 | + } |
|
| 1503 | + $message = sprintf( |
|
| 1504 | + __( |
|
| 1505 | + 'The final price for the ticket line item (ID:%1$d) could not be calculated.', |
|
| 1506 | + 'event_espresso' |
|
| 1507 | + ), |
|
| 1508 | + $ticket_line_item->ID() |
|
| 1509 | + ); |
|
| 1510 | + if (WP_DEBUG) { |
|
| 1511 | + $message .= '<br>' . print_r($final_prices_per_ticket_line_item, true); |
|
| 1512 | + throw new \OutOfRangeException($message); |
|
| 1513 | + } else { |
|
| 1514 | + EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message); |
|
| 1515 | + } |
|
| 1516 | + return null; |
|
| 1517 | + } |
|
| 1518 | + |
|
| 1519 | + |
|
| 1520 | + /** |
|
| 1521 | + * Creates a duplicate of the line item tree, except only includes billable items |
|
| 1522 | + * and the portion of line items attributed to billable things |
|
| 1523 | + * |
|
| 1524 | + * @param EE_Line_Item $line_item |
|
| 1525 | + * @param EE_Registration[] $registrations |
|
| 1526 | + * @return \EE_Line_Item |
|
| 1527 | + * @throws \EE_Error |
|
| 1528 | + */ |
|
| 1529 | + public static function billable_line_item_tree(EE_Line_Item $line_item, $registrations) |
|
| 1530 | + { |
|
| 1531 | + $copy_li = EEH_Line_Item::billable_line_item($line_item, $registrations); |
|
| 1532 | + foreach ($line_item->children() as $child_li) { |
|
| 1533 | + $copy_li->add_child_line_item(EEH_Line_Item::billable_line_item_tree($child_li, $registrations)); |
|
| 1534 | + } |
|
| 1535 | + // if this is the grand total line item, make sure the totals all add up |
|
| 1536 | + // (we could have duplicated this logic AS we copied the line items, but |
|
| 1537 | + // it seems DRYer this way) |
|
| 1538 | + if ($copy_li->type() === EEM_Line_Item::type_total) { |
|
| 1539 | + $copy_li->recalculate_total_including_taxes(); |
|
| 1540 | + } |
|
| 1541 | + return $copy_li; |
|
| 1542 | + } |
|
| 1543 | + |
|
| 1544 | + |
|
| 1545 | + /** |
|
| 1546 | + * Creates a new, unsaved line item from $line_item that factors in the |
|
| 1547 | + * number of billable registrations on $registrations. |
|
| 1548 | + * |
|
| 1549 | + * @param EE_Line_Item $line_item |
|
| 1550 | + * @return EE_Line_Item |
|
| 1551 | + * @throws \EE_Error |
|
| 1552 | + * @param EE_Registration[] $registrations |
|
| 1553 | + */ |
|
| 1554 | + public static function billable_line_item(EE_Line_Item $line_item, $registrations) |
|
| 1555 | + { |
|
| 1556 | + $new_li_fields = $line_item->model_field_array(); |
|
| 1557 | + if ($line_item->type() === EEM_Line_Item::type_line_item && |
|
| 1558 | + $line_item->OBJ_type() === 'Ticket' |
|
| 1559 | + ) { |
|
| 1560 | + $count = 0; |
|
| 1561 | + foreach ($registrations as $registration) { |
|
| 1562 | + if ($line_item->OBJ_ID() === $registration->ticket_ID() && |
|
| 1563 | + in_array($registration->status_ID(), EEM_Registration::reg_statuses_that_allow_payment()) |
|
| 1564 | + ) { |
|
| 1565 | + $count++; |
|
| 1566 | + } |
|
| 1567 | + } |
|
| 1568 | + $new_li_fields['LIN_quantity'] = $count; |
|
| 1569 | + } |
|
| 1570 | + // don't set the total. We'll leave that up to the code that calculates it |
|
| 1571 | + unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent'], $new_li_fields['LIN_total']); |
|
| 1572 | + return EE_Line_Item::new_instance($new_li_fields); |
|
| 1573 | + } |
|
| 1574 | + |
|
| 1575 | + |
|
| 1576 | + /** |
|
| 1577 | + * Returns a modified line item tree where all the subtotals which have a total of 0 |
|
| 1578 | + * are removed, and line items with a quantity of 0 |
|
| 1579 | + * |
|
| 1580 | + * @param EE_Line_Item $line_item |null |
|
| 1581 | + * @return \EE_Line_Item|null |
|
| 1582 | + * @throws \EE_Error |
|
| 1583 | + */ |
|
| 1584 | + public static function non_empty_line_items(EE_Line_Item $line_item) |
|
| 1585 | + { |
|
| 1586 | + $copied_li = EEH_Line_Item::non_empty_line_item($line_item); |
|
| 1587 | + if ($copied_li === null) { |
|
| 1588 | + return null; |
|
| 1589 | + } |
|
| 1590 | + // if this is an event subtotal, we want to only include it if it |
|
| 1591 | + // has a non-zero total and at least one ticket line item child |
|
| 1592 | + $ticket_children = 0; |
|
| 1593 | + foreach ($line_item->children() as $child_li) { |
|
| 1594 | + $child_li_copy = EEH_Line_Item::non_empty_line_items($child_li); |
|
| 1595 | + if ($child_li_copy !== null) { |
|
| 1596 | + $copied_li->add_child_line_item($child_li_copy); |
|
| 1597 | + if ($child_li_copy->type() === EEM_Line_Item::type_line_item && |
|
| 1598 | + $child_li_copy->OBJ_type() === 'Ticket' |
|
| 1599 | + ) { |
|
| 1600 | + $ticket_children++; |
|
| 1601 | + } |
|
| 1602 | + } |
|
| 1603 | + } |
|
| 1604 | + // if this is an event subtotal with NO ticket children |
|
| 1605 | + // we basically want to ignore it |
|
| 1606 | + if ($ticket_children === 0 |
|
| 1607 | + && $line_item->type() === EEM_Line_Item::type_sub_total |
|
| 1608 | + && $line_item->OBJ_type() === 'Event' |
|
| 1609 | + && $line_item->total() === 0 |
|
| 1610 | + ) { |
|
| 1611 | + return null; |
|
| 1612 | + } |
|
| 1613 | + return $copied_li; |
|
| 1614 | + } |
|
| 1615 | + |
|
| 1616 | + |
|
| 1617 | + /** |
|
| 1618 | + * Creates a new, unsaved line item, but if it's a ticket line item |
|
| 1619 | + * with a total of 0, or a subtotal of 0, returns null instead |
|
| 1620 | + * |
|
| 1621 | + * @param EE_Line_Item $line_item |
|
| 1622 | + * @return EE_Line_Item |
|
| 1623 | + * @throws \EE_Error |
|
| 1624 | + */ |
|
| 1625 | + public static function non_empty_line_item(EE_Line_Item $line_item) |
|
| 1626 | + { |
|
| 1627 | + if ($line_item->type() === EEM_Line_Item::type_line_item && |
|
| 1628 | + $line_item->OBJ_type() === 'Ticket' && |
|
| 1629 | + $line_item->quantity() === 0 |
|
| 1630 | + ) { |
|
| 1631 | + return null; |
|
| 1632 | + } |
|
| 1633 | + $new_li_fields = $line_item->model_field_array(); |
|
| 1634 | + // don't set the total. We'll leave that up to the code that calculates it |
|
| 1635 | + unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent']); |
|
| 1636 | + return EE_Line_Item::new_instance($new_li_fields); |
|
| 1637 | + } |
|
| 1638 | + |
|
| 1639 | + |
|
| 1640 | + |
|
| 1641 | + /**************************************** @DEPRECATED METHODS *************************************** */ |
|
| 1642 | + /** |
|
| 1643 | + * @deprecated |
|
| 1644 | + * @param EE_Line_Item $total_line_item |
|
| 1645 | + * @return \EE_Line_Item |
|
| 1646 | + * @throws \EE_Error |
|
| 1647 | + */ |
|
| 1648 | + public static function get_items_subtotal(EE_Line_Item $total_line_item) |
|
| 1649 | + { |
|
| 1650 | + EE_Error::doing_it_wrong('EEH_Line_Item::get_items_subtotal()', __('Method replaced with EEH_Line_Item::get_pre_tax_subtotal()', 'event_espresso'), '4.6.0'); |
|
| 1651 | + return self::get_pre_tax_subtotal($total_line_item); |
|
| 1652 | + } |
|
| 1653 | + |
|
| 1654 | + |
|
| 1655 | + /** |
|
| 1656 | + * @deprecated |
|
| 1657 | + * @param EE_Transaction $transaction |
|
| 1658 | + * @return \EE_Line_Item |
|
| 1659 | + * @throws \EE_Error |
|
| 1660 | + */ |
|
| 1661 | + public static function create_default_total_line_item($transaction = null) |
|
| 1662 | + { |
|
| 1663 | + EE_Error::doing_it_wrong('EEH_Line_Item::create_default_total_line_item()', __('Method replaced with EEH_Line_Item::create_total_line_item()', 'event_espresso'), '4.6.0'); |
|
| 1664 | + return self::create_total_line_item($transaction); |
|
| 1665 | + } |
|
| 1666 | + |
|
| 1667 | + |
|
| 1668 | + /** |
|
| 1669 | + * @deprecated |
|
| 1670 | + * @param EE_Line_Item $total_line_item |
|
| 1671 | + * @param EE_Transaction $transaction |
|
| 1672 | + * @return \EE_Line_Item |
|
| 1673 | + * @throws \EE_Error |
|
| 1674 | + */ |
|
| 1675 | + public static function create_default_tickets_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 1676 | + { |
|
| 1677 | + EE_Error::doing_it_wrong('EEH_Line_Item::create_default_tickets_subtotal()', __('Method replaced with EEH_Line_Item::create_pre_tax_subtotal()', 'event_espresso'), '4.6.0'); |
|
| 1678 | + return self::create_pre_tax_subtotal($total_line_item, $transaction); |
|
| 1679 | + } |
|
| 1680 | + |
|
| 1681 | + |
|
| 1682 | + /** |
|
| 1683 | + * @deprecated |
|
| 1684 | + * @param EE_Line_Item $total_line_item |
|
| 1685 | + * @param EE_Transaction $transaction |
|
| 1686 | + * @return \EE_Line_Item |
|
| 1687 | + * @throws \EE_Error |
|
| 1688 | + */ |
|
| 1689 | + public static function create_default_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 1690 | + { |
|
| 1691 | + EE_Error::doing_it_wrong('EEH_Line_Item::create_default_taxes_subtotal()', __('Method replaced with EEH_Line_Item::create_taxes_subtotal()', 'event_espresso'), '4.6.0'); |
|
| 1692 | + return self::create_taxes_subtotal($total_line_item, $transaction); |
|
| 1693 | + } |
|
| 1694 | + |
|
| 1695 | + |
|
| 1696 | + /** |
|
| 1697 | + * @deprecated |
|
| 1698 | + * @param EE_Line_Item $total_line_item |
|
| 1699 | + * @param EE_Transaction $transaction |
|
| 1700 | + * @return \EE_Line_Item |
|
| 1701 | + * @throws \EE_Error |
|
| 1702 | + */ |
|
| 1703 | + public static function create_default_event_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 1704 | + { |
|
| 1705 | + EE_Error::doing_it_wrong('EEH_Line_Item::create_default_event_subtotal()', __('Method replaced with EEH_Line_Item::create_event_subtotal()', 'event_espresso'), '4.6.0'); |
|
| 1706 | + return self::create_event_subtotal($total_line_item, $transaction); |
|
| 1707 | + } |
|
| 1708 | 1708 | } |
@@ -14,178 +14,178 @@ |
||
| 14 | 14 | class EEH_Sideloader extends EEH_Base |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - private $_upload_to; |
|
| 18 | - private $_upload_from; |
|
| 19 | - private $_permissions; |
|
| 20 | - private $_new_file_name; |
|
| 21 | - |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * constructor allows the user to set the properties on the sideloader on construct. However, there are also setters for doing so. |
|
| 25 | - * |
|
| 26 | - * @access public |
|
| 27 | - * @param array $init array fo initializing the sideloader if keys match the properties. |
|
| 28 | - */ |
|
| 29 | - public function __construct($init = array()) |
|
| 30 | - { |
|
| 31 | - $this->_init($init); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * sets the properties for class either to defaults or using incoming initialization array |
|
| 37 | - * |
|
| 38 | - * @access private |
|
| 39 | - * @param array $init array on init (keys match properties others ignored) |
|
| 40 | - * @return void |
|
| 41 | - */ |
|
| 42 | - private function _init($init) |
|
| 43 | - { |
|
| 44 | - $defaults = array( |
|
| 45 | - '_upload_to' => $this->_get_wp_uploads_dir(), |
|
| 46 | - '_upload_from' => '', |
|
| 47 | - '_permissions' => 0644, |
|
| 48 | - '_new_file_name' => 'EE_Sideloader_' . uniqid() . '.default' |
|
| 49 | - ); |
|
| 50 | - |
|
| 51 | - $props = array_merge($defaults, $init); |
|
| 52 | - |
|
| 53 | - foreach ($props as $key => $val) { |
|
| 54 | - if (EEH_Class_Tools::has_property($this, $key)) { |
|
| 55 | - $this->{$key} = $val; |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - // make sure we include the required wp file for needed functions |
|
| 60 | - require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - // utilities |
|
| 65 | - private function _get_wp_uploads_dir() |
|
| 66 | - { |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - // setters |
|
| 70 | - public function set_upload_to($upload_to_folder) |
|
| 71 | - { |
|
| 72 | - $this->_upload_to = $upload_to_folder; |
|
| 73 | - } |
|
| 74 | - public function set_upload_from($upload_from_folder) |
|
| 75 | - { |
|
| 76 | - $this->_upload_from_folder = $upload_from_folder; |
|
| 77 | - } |
|
| 78 | - public function set_permissions($permissions) |
|
| 79 | - { |
|
| 80 | - $this->_permissions = $permissions; |
|
| 81 | - } |
|
| 82 | - public function set_new_file_name($new_file_name) |
|
| 83 | - { |
|
| 84 | - $this->_new_file_name = $new_file_name; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - // getters |
|
| 88 | - public function get_upload_to() |
|
| 89 | - { |
|
| 90 | - return $this->_upload_to; |
|
| 91 | - } |
|
| 92 | - public function get_upload_from() |
|
| 93 | - { |
|
| 94 | - return $this->_upload_from; |
|
| 95 | - } |
|
| 96 | - public function get_permissions() |
|
| 97 | - { |
|
| 98 | - return $this->_permissions; |
|
| 99 | - } |
|
| 100 | - public function get_new_file_name() |
|
| 101 | - { |
|
| 102 | - return $this->_new_file_name; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - |
|
| 106 | - // upload methods |
|
| 107 | - public function sideload() |
|
| 108 | - { |
|
| 109 | - // setup temp dir |
|
| 110 | - $temp_file = wp_tempnam($this->_upload_from); |
|
| 111 | - |
|
| 112 | - if (!$temp_file) { |
|
| 113 | - EE_Error::add_error( |
|
| 114 | - esc_html__('Something went wrong with the upload. Unable to create a tmp file for the uploaded file on the server', 'event_espresso'), |
|
| 115 | - __FILE__, |
|
| 116 | - __FUNCTION__, |
|
| 117 | - __LINE__ |
|
| 118 | - ); |
|
| 119 | - return false; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - do_action('AHEE__EEH_Sideloader__sideload__before', $this, $temp_file); |
|
| 123 | - |
|
| 124 | - $wp_remote_args = apply_filters('FHEE__EEH_Sideloader__sideload__wp_remote_args', array( 'timeout' => 500, 'stream' => true, 'filename' => $temp_file ), $this, $temp_file); |
|
| 125 | - |
|
| 126 | - $response = wp_safe_remote_get($this->_upload_from, $wp_remote_args); |
|
| 127 | - |
|
| 128 | - if (is_wp_error($response) || 200 != wp_remote_retrieve_response_code($response)) { |
|
| 129 | - unlink($temp_file); |
|
| 130 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 131 | - EE_Error::add_error( |
|
| 132 | - sprintf( |
|
| 133 | - esc_html__('Unable to upload the file. Either the path given to upload from is incorrect, or something else happened. Here is the path given: %s', 'event_espresso'), |
|
| 134 | - $this->_upload_from |
|
| 135 | - ), |
|
| 136 | - __FILE__, |
|
| 137 | - __FUNCTION__, |
|
| 138 | - __LINE__ |
|
| 139 | - ); |
|
| 140 | - } |
|
| 141 | - return false; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - // possible md5 check |
|
| 145 | - $content_md5 = wp_remote_retrieve_header($response, 'content-md5'); |
|
| 146 | - if ($content_md5) { |
|
| 147 | - $md5_check = verify_file_md5($temp_file, $content_md5); |
|
| 148 | - if (is_wp_error($md5_check)) { |
|
| 149 | - unlink($temp_file); |
|
| 150 | - EE_Error::add_error( |
|
| 151 | - $md5_check->get_error_message(), |
|
| 152 | - __FILE__, |
|
| 153 | - __FUNCTION__, |
|
| 154 | - __LINE__ |
|
| 155 | - ); |
|
| 156 | - return false; |
|
| 157 | - } |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - $file = $temp_file; |
|
| 161 | - |
|
| 162 | - // now we have the file, let's get it in the right directory with the right name. |
|
| 163 | - $path = apply_filters('FHEE__EEH_Sideloader__sideload__new_path', $this->_upload_to . $this->_new_file_name, $this); |
|
| 164 | - |
|
| 165 | - // move file in |
|
| 166 | - if (false === @ rename($file, $path)) { |
|
| 167 | - unlink($temp_file); |
|
| 168 | - EE_Error::add_error( |
|
| 169 | - sprintf( |
|
| 170 | - esc_html__('Unable to move the file to new location (possible permissions errors). This is the path the class attempted to move the file to: %s', 'event_espresso'), |
|
| 171 | - $path |
|
| 172 | - ), |
|
| 173 | - __FILE__, |
|
| 174 | - __FUNCTION__, |
|
| 175 | - __LINE__ |
|
| 176 | - ); |
|
| 177 | - return false; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - // set permissions |
|
| 181 | - $permissions = apply_filters('FHEE__EEH_Sideloader__sideload__permissions_applied', $this->_permissions, $this); |
|
| 182 | - chmod($path, $permissions); |
|
| 183 | - |
|
| 184 | - // that's it. let's allow for actions after file uploaded. |
|
| 185 | - do_action('AHEE__EE_Sideloader__sideload_after', $this, $path); |
|
| 186 | - |
|
| 187 | - // unlink tempfile |
|
| 188 | - @unlink($temp_file); |
|
| 189 | - return true; |
|
| 190 | - } |
|
| 17 | + private $_upload_to; |
|
| 18 | + private $_upload_from; |
|
| 19 | + private $_permissions; |
|
| 20 | + private $_new_file_name; |
|
| 21 | + |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * constructor allows the user to set the properties on the sideloader on construct. However, there are also setters for doing so. |
|
| 25 | + * |
|
| 26 | + * @access public |
|
| 27 | + * @param array $init array fo initializing the sideloader if keys match the properties. |
|
| 28 | + */ |
|
| 29 | + public function __construct($init = array()) |
|
| 30 | + { |
|
| 31 | + $this->_init($init); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * sets the properties for class either to defaults or using incoming initialization array |
|
| 37 | + * |
|
| 38 | + * @access private |
|
| 39 | + * @param array $init array on init (keys match properties others ignored) |
|
| 40 | + * @return void |
|
| 41 | + */ |
|
| 42 | + private function _init($init) |
|
| 43 | + { |
|
| 44 | + $defaults = array( |
|
| 45 | + '_upload_to' => $this->_get_wp_uploads_dir(), |
|
| 46 | + '_upload_from' => '', |
|
| 47 | + '_permissions' => 0644, |
|
| 48 | + '_new_file_name' => 'EE_Sideloader_' . uniqid() . '.default' |
|
| 49 | + ); |
|
| 50 | + |
|
| 51 | + $props = array_merge($defaults, $init); |
|
| 52 | + |
|
| 53 | + foreach ($props as $key => $val) { |
|
| 54 | + if (EEH_Class_Tools::has_property($this, $key)) { |
|
| 55 | + $this->{$key} = $val; |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + // make sure we include the required wp file for needed functions |
|
| 60 | + require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + // utilities |
|
| 65 | + private function _get_wp_uploads_dir() |
|
| 66 | + { |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + // setters |
|
| 70 | + public function set_upload_to($upload_to_folder) |
|
| 71 | + { |
|
| 72 | + $this->_upload_to = $upload_to_folder; |
|
| 73 | + } |
|
| 74 | + public function set_upload_from($upload_from_folder) |
|
| 75 | + { |
|
| 76 | + $this->_upload_from_folder = $upload_from_folder; |
|
| 77 | + } |
|
| 78 | + public function set_permissions($permissions) |
|
| 79 | + { |
|
| 80 | + $this->_permissions = $permissions; |
|
| 81 | + } |
|
| 82 | + public function set_new_file_name($new_file_name) |
|
| 83 | + { |
|
| 84 | + $this->_new_file_name = $new_file_name; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + // getters |
|
| 88 | + public function get_upload_to() |
|
| 89 | + { |
|
| 90 | + return $this->_upload_to; |
|
| 91 | + } |
|
| 92 | + public function get_upload_from() |
|
| 93 | + { |
|
| 94 | + return $this->_upload_from; |
|
| 95 | + } |
|
| 96 | + public function get_permissions() |
|
| 97 | + { |
|
| 98 | + return $this->_permissions; |
|
| 99 | + } |
|
| 100 | + public function get_new_file_name() |
|
| 101 | + { |
|
| 102 | + return $this->_new_file_name; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + |
|
| 106 | + // upload methods |
|
| 107 | + public function sideload() |
|
| 108 | + { |
|
| 109 | + // setup temp dir |
|
| 110 | + $temp_file = wp_tempnam($this->_upload_from); |
|
| 111 | + |
|
| 112 | + if (!$temp_file) { |
|
| 113 | + EE_Error::add_error( |
|
| 114 | + esc_html__('Something went wrong with the upload. Unable to create a tmp file for the uploaded file on the server', 'event_espresso'), |
|
| 115 | + __FILE__, |
|
| 116 | + __FUNCTION__, |
|
| 117 | + __LINE__ |
|
| 118 | + ); |
|
| 119 | + return false; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + do_action('AHEE__EEH_Sideloader__sideload__before', $this, $temp_file); |
|
| 123 | + |
|
| 124 | + $wp_remote_args = apply_filters('FHEE__EEH_Sideloader__sideload__wp_remote_args', array( 'timeout' => 500, 'stream' => true, 'filename' => $temp_file ), $this, $temp_file); |
|
| 125 | + |
|
| 126 | + $response = wp_safe_remote_get($this->_upload_from, $wp_remote_args); |
|
| 127 | + |
|
| 128 | + if (is_wp_error($response) || 200 != wp_remote_retrieve_response_code($response)) { |
|
| 129 | + unlink($temp_file); |
|
| 130 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 131 | + EE_Error::add_error( |
|
| 132 | + sprintf( |
|
| 133 | + esc_html__('Unable to upload the file. Either the path given to upload from is incorrect, or something else happened. Here is the path given: %s', 'event_espresso'), |
|
| 134 | + $this->_upload_from |
|
| 135 | + ), |
|
| 136 | + __FILE__, |
|
| 137 | + __FUNCTION__, |
|
| 138 | + __LINE__ |
|
| 139 | + ); |
|
| 140 | + } |
|
| 141 | + return false; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + // possible md5 check |
|
| 145 | + $content_md5 = wp_remote_retrieve_header($response, 'content-md5'); |
|
| 146 | + if ($content_md5) { |
|
| 147 | + $md5_check = verify_file_md5($temp_file, $content_md5); |
|
| 148 | + if (is_wp_error($md5_check)) { |
|
| 149 | + unlink($temp_file); |
|
| 150 | + EE_Error::add_error( |
|
| 151 | + $md5_check->get_error_message(), |
|
| 152 | + __FILE__, |
|
| 153 | + __FUNCTION__, |
|
| 154 | + __LINE__ |
|
| 155 | + ); |
|
| 156 | + return false; |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + $file = $temp_file; |
|
| 161 | + |
|
| 162 | + // now we have the file, let's get it in the right directory with the right name. |
|
| 163 | + $path = apply_filters('FHEE__EEH_Sideloader__sideload__new_path', $this->_upload_to . $this->_new_file_name, $this); |
|
| 164 | + |
|
| 165 | + // move file in |
|
| 166 | + if (false === @ rename($file, $path)) { |
|
| 167 | + unlink($temp_file); |
|
| 168 | + EE_Error::add_error( |
|
| 169 | + sprintf( |
|
| 170 | + esc_html__('Unable to move the file to new location (possible permissions errors). This is the path the class attempted to move the file to: %s', 'event_espresso'), |
|
| 171 | + $path |
|
| 172 | + ), |
|
| 173 | + __FILE__, |
|
| 174 | + __FUNCTION__, |
|
| 175 | + __LINE__ |
|
| 176 | + ); |
|
| 177 | + return false; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + // set permissions |
|
| 181 | + $permissions = apply_filters('FHEE__EEH_Sideloader__sideload__permissions_applied', $this->_permissions, $this); |
|
| 182 | + chmod($path, $permissions); |
|
| 183 | + |
|
| 184 | + // that's it. let's allow for actions after file uploaded. |
|
| 185 | + do_action('AHEE__EE_Sideloader__sideload_after', $this, $path); |
|
| 186 | + |
|
| 187 | + // unlink tempfile |
|
| 188 | + @unlink($temp_file); |
|
| 189 | + return true; |
|
| 190 | + } |
|
| 191 | 191 | } //end EEH_Template class |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | '_upload_to' => $this->_get_wp_uploads_dir(), |
| 46 | 46 | '_upload_from' => '', |
| 47 | 47 | '_permissions' => 0644, |
| 48 | - '_new_file_name' => 'EE_Sideloader_' . uniqid() . '.default' |
|
| 48 | + '_new_file_name' => 'EE_Sideloader_'.uniqid().'.default' |
|
| 49 | 49 | ); |
| 50 | 50 | |
| 51 | 51 | $props = array_merge($defaults, $init); |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | // make sure we include the required wp file for needed functions |
| 60 | - require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
| 60 | + require_once(ABSPATH.'wp-admin/includes/file.php'); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | // setup temp dir |
| 110 | 110 | $temp_file = wp_tempnam($this->_upload_from); |
| 111 | 111 | |
| 112 | - if (!$temp_file) { |
|
| 112 | + if ( ! $temp_file) { |
|
| 113 | 113 | EE_Error::add_error( |
| 114 | 114 | esc_html__('Something went wrong with the upload. Unable to create a tmp file for the uploaded file on the server', 'event_espresso'), |
| 115 | 115 | __FILE__, |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | |
| 122 | 122 | do_action('AHEE__EEH_Sideloader__sideload__before', $this, $temp_file); |
| 123 | 123 | |
| 124 | - $wp_remote_args = apply_filters('FHEE__EEH_Sideloader__sideload__wp_remote_args', array( 'timeout' => 500, 'stream' => true, 'filename' => $temp_file ), $this, $temp_file); |
|
| 124 | + $wp_remote_args = apply_filters('FHEE__EEH_Sideloader__sideload__wp_remote_args', array('timeout' => 500, 'stream' => true, 'filename' => $temp_file), $this, $temp_file); |
|
| 125 | 125 | |
| 126 | 126 | $response = wp_safe_remote_get($this->_upload_from, $wp_remote_args); |
| 127 | 127 | |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | $file = $temp_file; |
| 161 | 161 | |
| 162 | 162 | // now we have the file, let's get it in the right directory with the right name. |
| 163 | - $path = apply_filters('FHEE__EEH_Sideloader__sideload__new_path', $this->_upload_to . $this->_new_file_name, $this); |
|
| 163 | + $path = apply_filters('FHEE__EEH_Sideloader__sideload__new_path', $this->_upload_to.$this->_new_file_name, $this); |
|
| 164 | 164 | |
| 165 | 165 | // move file in |
| 166 | 166 | if (false === @ rename($file, $path)) { |
@@ -14,2503 +14,2503 @@ |
||
| 14 | 14 | class Transactions_Admin_Page extends EE_Admin_Page |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var EE_Transaction |
|
| 19 | - */ |
|
| 20 | - private $_transaction; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var EE_Session |
|
| 24 | - */ |
|
| 25 | - private $_session; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var array $_txn_status |
|
| 29 | - */ |
|
| 30 | - private static $_txn_status; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var array $_pay_status |
|
| 34 | - */ |
|
| 35 | - private static $_pay_status; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var array $_existing_reg_payment_REG_IDs |
|
| 39 | - */ |
|
| 40 | - protected $_existing_reg_payment_REG_IDs = null; |
|
| 41 | - |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @Constructor |
|
| 45 | - * @access public |
|
| 46 | - * @param bool $routing |
|
| 47 | - * @throws EE_Error |
|
| 48 | - * @throws InvalidArgumentException |
|
| 49 | - * @throws ReflectionException |
|
| 50 | - * @throws InvalidDataTypeException |
|
| 51 | - * @throws InvalidInterfaceException |
|
| 52 | - */ |
|
| 53 | - public function __construct($routing = true) |
|
| 54 | - { |
|
| 55 | - parent::__construct($routing); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * _init_page_props |
|
| 61 | - * |
|
| 62 | - * @return void |
|
| 63 | - */ |
|
| 64 | - protected function _init_page_props() |
|
| 65 | - { |
|
| 66 | - $this->page_slug = TXN_PG_SLUG; |
|
| 67 | - $this->page_label = esc_html__('Transactions', 'event_espresso'); |
|
| 68 | - $this->_admin_base_url = TXN_ADMIN_URL; |
|
| 69 | - $this->_admin_base_path = TXN_ADMIN; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * _ajax_hooks |
|
| 75 | - * |
|
| 76 | - * @return void |
|
| 77 | - */ |
|
| 78 | - protected function _ajax_hooks() |
|
| 79 | - { |
|
| 80 | - add_action('wp_ajax_espresso_apply_payment', array($this, 'apply_payments_or_refunds')); |
|
| 81 | - add_action('wp_ajax_espresso_apply_refund', array($this, 'apply_payments_or_refunds')); |
|
| 82 | - add_action('wp_ajax_espresso_delete_payment', array($this, 'delete_payment')); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * _define_page_props |
|
| 88 | - * |
|
| 89 | - * @return void |
|
| 90 | - */ |
|
| 91 | - protected function _define_page_props() |
|
| 92 | - { |
|
| 93 | - $this->_admin_page_title = $this->page_label; |
|
| 94 | - $this->_labels = array( |
|
| 95 | - 'buttons' => array( |
|
| 96 | - 'add' => esc_html__('Add New Transaction', 'event_espresso'), |
|
| 97 | - 'edit' => esc_html__('Edit Transaction', 'event_espresso'), |
|
| 98 | - 'delete' => esc_html__('Delete Transaction', 'event_espresso'), |
|
| 99 | - ), |
|
| 100 | - ); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * grab url requests and route them |
|
| 106 | - * |
|
| 107 | - * @access private |
|
| 108 | - * @return void |
|
| 109 | - * @throws EE_Error |
|
| 110 | - * @throws InvalidArgumentException |
|
| 111 | - * @throws InvalidDataTypeException |
|
| 112 | - * @throws InvalidInterfaceException |
|
| 113 | - */ |
|
| 114 | - public function _set_page_routes() |
|
| 115 | - { |
|
| 116 | - |
|
| 117 | - $this->_set_transaction_status_array(); |
|
| 118 | - |
|
| 119 | - $txn_id = ! empty($this->_req_data['TXN_ID']) |
|
| 120 | - && ! is_array($this->_req_data['TXN_ID']) |
|
| 121 | - ? $this->_req_data['TXN_ID'] |
|
| 122 | - : 0; |
|
| 123 | - |
|
| 124 | - $this->_page_routes = array( |
|
| 125 | - |
|
| 126 | - 'default' => array( |
|
| 127 | - 'func' => '_transactions_overview_list_table', |
|
| 128 | - 'capability' => 'ee_read_transactions', |
|
| 129 | - ), |
|
| 130 | - |
|
| 131 | - 'view_transaction' => array( |
|
| 132 | - 'func' => '_transaction_details', |
|
| 133 | - 'capability' => 'ee_read_transaction', |
|
| 134 | - 'obj_id' => $txn_id, |
|
| 135 | - ), |
|
| 136 | - |
|
| 137 | - 'send_payment_reminder' => array( |
|
| 138 | - 'func' => '_send_payment_reminder', |
|
| 139 | - 'noheader' => true, |
|
| 140 | - 'capability' => 'ee_send_message', |
|
| 141 | - ), |
|
| 142 | - |
|
| 143 | - 'espresso_apply_payment' => array( |
|
| 144 | - 'func' => 'apply_payments_or_refunds', |
|
| 145 | - 'noheader' => true, |
|
| 146 | - 'capability' => 'ee_edit_payments', |
|
| 147 | - ), |
|
| 148 | - |
|
| 149 | - 'espresso_apply_refund' => array( |
|
| 150 | - 'func' => 'apply_payments_or_refunds', |
|
| 151 | - 'noheader' => true, |
|
| 152 | - 'capability' => 'ee_edit_payments', |
|
| 153 | - ), |
|
| 154 | - |
|
| 155 | - 'espresso_delete_payment' => array( |
|
| 156 | - 'func' => 'delete_payment', |
|
| 157 | - 'noheader' => true, |
|
| 158 | - 'capability' => 'ee_delete_payments', |
|
| 159 | - ), |
|
| 160 | - |
|
| 161 | - ); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - |
|
| 165 | - protected function _set_page_config() |
|
| 166 | - { |
|
| 167 | - $this->_page_config = array( |
|
| 168 | - 'default' => array( |
|
| 169 | - 'nav' => array( |
|
| 170 | - 'label' => esc_html__('Overview', 'event_espresso'), |
|
| 171 | - 'order' => 10, |
|
| 172 | - ), |
|
| 173 | - 'list_table' => 'EE_Admin_Transactions_List_Table', |
|
| 174 | - 'help_tabs' => array( |
|
| 175 | - 'transactions_overview_help_tab' => array( |
|
| 176 | - 'title' => esc_html__('Transactions Overview', 'event_espresso'), |
|
| 177 | - 'filename' => 'transactions_overview', |
|
| 178 | - ), |
|
| 179 | - 'transactions_overview_table_column_headings_help_tab' => array( |
|
| 180 | - 'title' => esc_html__('Transactions Table Column Headings', 'event_espresso'), |
|
| 181 | - 'filename' => 'transactions_overview_table_column_headings', |
|
| 182 | - ), |
|
| 183 | - 'transactions_overview_views_filters_help_tab' => array( |
|
| 184 | - 'title' => esc_html__('Transaction Views & Filters & Search', 'event_espresso'), |
|
| 185 | - 'filename' => 'transactions_overview_views_filters_search', |
|
| 186 | - ), |
|
| 187 | - ), |
|
| 188 | - 'help_tour' => array('Transactions_Overview_Help_Tour'), |
|
| 189 | - /** |
|
| 190 | - * commented out because currently we are not displaying tips for transaction list table status but this |
|
| 191 | - * may change in a later iteration so want to keep the code for then. |
|
| 192 | - */ |
|
| 193 | - // 'qtips' => array( 'Transactions_List_Table_Tips' ), |
|
| 194 | - 'require_nonce' => false, |
|
| 195 | - ), |
|
| 196 | - 'view_transaction' => array( |
|
| 197 | - 'nav' => array( |
|
| 198 | - 'label' => esc_html__('View Transaction', 'event_espresso'), |
|
| 199 | - 'order' => 5, |
|
| 200 | - 'url' => isset($this->_req_data['TXN_ID']) |
|
| 201 | - ? add_query_arg(array('TXN_ID' => $this->_req_data['TXN_ID']), $this->_current_page_view_url) |
|
| 202 | - : $this->_admin_base_url, |
|
| 203 | - 'persistent' => false, |
|
| 204 | - ), |
|
| 205 | - 'help_tabs' => array( |
|
| 206 | - 'transactions_view_transaction_help_tab' => array( |
|
| 207 | - 'title' => esc_html__('View Transaction', 'event_espresso'), |
|
| 208 | - 'filename' => 'transactions_view_transaction', |
|
| 209 | - ), |
|
| 210 | - 'transactions_view_transaction_transaction_details_table_help_tab' => array( |
|
| 211 | - 'title' => esc_html__('Transaction Details Table', 'event_espresso'), |
|
| 212 | - 'filename' => 'transactions_view_transaction_transaction_details_table', |
|
| 213 | - ), |
|
| 214 | - 'transactions_view_transaction_attendees_registered_help_tab' => array( |
|
| 215 | - 'title' => esc_html__('Attendees Registered', 'event_espresso'), |
|
| 216 | - 'filename' => 'transactions_view_transaction_attendees_registered', |
|
| 217 | - ), |
|
| 218 | - 'transactions_view_transaction_views_primary_registrant_billing_information_help_tab' => array( |
|
| 219 | - 'title' => esc_html__('Primary Registrant & Billing Information', 'event_espresso'), |
|
| 220 | - 'filename' => 'transactions_view_transaction_primary_registrant_billing_information', |
|
| 221 | - ), |
|
| 222 | - ), |
|
| 223 | - 'qtips' => array('Transaction_Details_Tips'), |
|
| 224 | - 'help_tour' => array('Transaction_Details_Help_Tour'), |
|
| 225 | - 'metaboxes' => array('_transaction_details_metaboxes'), |
|
| 226 | - |
|
| 227 | - 'require_nonce' => false, |
|
| 228 | - ), |
|
| 229 | - ); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * The below methods aren't used by this class currently |
|
| 235 | - */ |
|
| 236 | - protected function _add_screen_options() |
|
| 237 | - { |
|
| 238 | - // noop |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - protected function _add_feature_pointers() |
|
| 242 | - { |
|
| 243 | - // noop |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - public function admin_init() |
|
| 247 | - { |
|
| 248 | - // IF a registration was JUST added via the admin... |
|
| 249 | - if (isset( |
|
| 250 | - $this->_req_data['redirect_from'], |
|
| 251 | - $this->_req_data['EVT_ID'], |
|
| 252 | - $this->_req_data['event_name'] |
|
| 253 | - )) { |
|
| 254 | - // then set a cookie so that we can block any attempts to use |
|
| 255 | - // the back button as a way to enter another registration. |
|
| 256 | - setcookie( |
|
| 257 | - 'ee_registration_added', |
|
| 258 | - $this->_req_data['EVT_ID'], |
|
| 259 | - time() + WEEK_IN_SECONDS, |
|
| 260 | - '/' |
|
| 261 | - ); |
|
| 262 | - // and update the global |
|
| 263 | - $_COOKIE['ee_registration_added'] = $this->_req_data['EVT_ID']; |
|
| 264 | - } |
|
| 265 | - EE_Registry::$i18n_js_strings['invalid_server_response'] = esc_html__( |
|
| 266 | - 'An error occurred! Your request may have been processed, but a valid response from the server was not received. Please refresh the page and try again.', |
|
| 267 | - 'event_espresso' |
|
| 268 | - ); |
|
| 269 | - EE_Registry::$i18n_js_strings['error_occurred'] = esc_html__( |
|
| 270 | - 'An error occurred! Please refresh the page and try again.', |
|
| 271 | - 'event_espresso' |
|
| 272 | - ); |
|
| 273 | - EE_Registry::$i18n_js_strings['txn_status_array'] = self::$_txn_status; |
|
| 274 | - EE_Registry::$i18n_js_strings['pay_status_array'] = self::$_pay_status; |
|
| 275 | - EE_Registry::$i18n_js_strings['payments_total'] = esc_html__('Payments Total', 'event_espresso'); |
|
| 276 | - EE_Registry::$i18n_js_strings['transaction_overpaid'] = esc_html__( |
|
| 277 | - 'This transaction has been overpaid ! Payments Total', |
|
| 278 | - 'event_espresso' |
|
| 279 | - ); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - public function admin_notices() |
|
| 283 | - { |
|
| 284 | - // noop |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - public function admin_footer_scripts() |
|
| 288 | - { |
|
| 289 | - // noop |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * _set_transaction_status_array |
|
| 295 | - * sets list of transaction statuses |
|
| 296 | - * |
|
| 297 | - * @access private |
|
| 298 | - * @return void |
|
| 299 | - * @throws EE_Error |
|
| 300 | - * @throws InvalidArgumentException |
|
| 301 | - * @throws InvalidDataTypeException |
|
| 302 | - * @throws InvalidInterfaceException |
|
| 303 | - */ |
|
| 304 | - private function _set_transaction_status_array() |
|
| 305 | - { |
|
| 306 | - self::$_txn_status = EEM_Transaction::instance()->status_array(true); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * get_transaction_status_array |
|
| 312 | - * return the transaction status array for wp_list_table |
|
| 313 | - * |
|
| 314 | - * @access public |
|
| 315 | - * @return array |
|
| 316 | - */ |
|
| 317 | - public function get_transaction_status_array() |
|
| 318 | - { |
|
| 319 | - return self::$_txn_status; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * get list of payment statuses |
|
| 325 | - * |
|
| 326 | - * @access private |
|
| 327 | - * @return void |
|
| 328 | - * @throws EE_Error |
|
| 329 | - * @throws InvalidArgumentException |
|
| 330 | - * @throws InvalidDataTypeException |
|
| 331 | - * @throws InvalidInterfaceException |
|
| 332 | - */ |
|
| 333 | - private function _get_payment_status_array() |
|
| 334 | - { |
|
| 335 | - self::$_pay_status = EEM_Payment::instance()->status_array(true); |
|
| 336 | - $this->_template_args['payment_status'] = self::$_pay_status; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * _add_screen_options_default |
|
| 342 | - * |
|
| 343 | - * @access protected |
|
| 344 | - * @return void |
|
| 345 | - * @throws InvalidArgumentException |
|
| 346 | - * @throws InvalidDataTypeException |
|
| 347 | - * @throws InvalidInterfaceException |
|
| 348 | - */ |
|
| 349 | - protected function _add_screen_options_default() |
|
| 350 | - { |
|
| 351 | - $this->_per_page_screen_option(); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * load_scripts_styles |
|
| 357 | - * |
|
| 358 | - * @access public |
|
| 359 | - * @return void |
|
| 360 | - */ |
|
| 361 | - public function load_scripts_styles() |
|
| 362 | - { |
|
| 363 | - // enqueue style |
|
| 364 | - wp_register_style( |
|
| 365 | - 'espresso_txn', |
|
| 366 | - TXN_ASSETS_URL . 'espresso_transactions_admin.css', |
|
| 367 | - array(), |
|
| 368 | - EVENT_ESPRESSO_VERSION |
|
| 369 | - ); |
|
| 370 | - wp_enqueue_style('espresso_txn'); |
|
| 371 | - // scripts |
|
| 372 | - wp_register_script( |
|
| 373 | - 'espresso_txn', |
|
| 374 | - TXN_ASSETS_URL . 'espresso_transactions_admin.js', |
|
| 375 | - array( |
|
| 376 | - 'ee_admin_js', |
|
| 377 | - 'ee-datepicker', |
|
| 378 | - 'jquery-ui-datepicker', |
|
| 379 | - 'jquery-ui-draggable', |
|
| 380 | - 'ee-dialog', |
|
| 381 | - 'ee-accounting', |
|
| 382 | - 'ee-serialize-full-array', |
|
| 383 | - ), |
|
| 384 | - EVENT_ESPRESSO_VERSION, |
|
| 385 | - true |
|
| 386 | - ); |
|
| 387 | - wp_enqueue_script('espresso_txn'); |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * load_scripts_styles_view_transaction |
|
| 393 | - * |
|
| 394 | - * @access public |
|
| 395 | - * @return void |
|
| 396 | - */ |
|
| 397 | - public function load_scripts_styles_view_transaction() |
|
| 398 | - { |
|
| 399 | - // styles |
|
| 400 | - wp_enqueue_style('espresso-ui-theme'); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * load_scripts_styles_default |
|
| 406 | - * |
|
| 407 | - * @access public |
|
| 408 | - * @return void |
|
| 409 | - */ |
|
| 410 | - public function load_scripts_styles_default() |
|
| 411 | - { |
|
| 412 | - // styles |
|
| 413 | - wp_enqueue_style('espresso-ui-theme'); |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * _set_list_table_views_default |
|
| 419 | - * |
|
| 420 | - * @access protected |
|
| 421 | - * @return void |
|
| 422 | - */ |
|
| 423 | - protected function _set_list_table_views_default() |
|
| 424 | - { |
|
| 425 | - $this->_views = array( |
|
| 426 | - 'all' => array( |
|
| 427 | - 'slug' => 'all', |
|
| 428 | - 'label' => esc_html__('View All Transactions', 'event_espresso'), |
|
| 429 | - 'count' => 0, |
|
| 430 | - ), |
|
| 431 | - 'abandoned' => array( |
|
| 432 | - 'slug' => 'abandoned', |
|
| 433 | - 'label' => esc_html__('Abandoned Transactions', 'event_espresso'), |
|
| 434 | - 'count' => 0, |
|
| 435 | - ), |
|
| 436 | - 'incomplete' => array( |
|
| 437 | - 'slug' => 'incomplete', |
|
| 438 | - 'label' => esc_html__('Incomplete Transactions', 'event_espresso'), |
|
| 439 | - 'count' => 0, |
|
| 440 | - ) |
|
| 441 | - ); |
|
| 442 | - if (/** |
|
| 443 | - * Filters whether a link to the "Failed Transactions" list table |
|
| 444 | - * appears on the Transactions Admin Page list table. |
|
| 445 | - * List display can be turned back on via the following: |
|
| 446 | - * add_filter( |
|
| 447 | - * 'FHEE__Transactions_Admin_Page___set_list_table_views_default__display_failed_txns_list', |
|
| 448 | - * '__return_true' |
|
| 449 | - * ); |
|
| 450 | - * |
|
| 451 | - * @since 4.9.70.p |
|
| 452 | - * @param boolean $display_failed_txns_list |
|
| 453 | - * @param Transactions_Admin_Page $this |
|
| 454 | - */ |
|
| 455 | - apply_filters( |
|
| 456 | - 'FHEE__Transactions_Admin_Page___set_list_table_views_default__display_failed_txns_list', |
|
| 457 | - false, |
|
| 458 | - $this |
|
| 459 | - ) |
|
| 460 | - ) { |
|
| 461 | - $this->_views['failed'] = array( |
|
| 462 | - 'slug' => 'failed', |
|
| 463 | - 'label' => esc_html__('Failed Transactions', 'event_espresso'), |
|
| 464 | - 'count' => 0, |
|
| 465 | - ); |
|
| 466 | - } |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - |
|
| 470 | - /** |
|
| 471 | - * _set_transaction_object |
|
| 472 | - * This sets the _transaction property for the transaction details screen |
|
| 473 | - * |
|
| 474 | - * @access private |
|
| 475 | - * @return void |
|
| 476 | - * @throws EE_Error |
|
| 477 | - * @throws InvalidArgumentException |
|
| 478 | - * @throws RuntimeException |
|
| 479 | - * @throws InvalidDataTypeException |
|
| 480 | - * @throws InvalidInterfaceException |
|
| 481 | - * @throws ReflectionException |
|
| 482 | - */ |
|
| 483 | - private function _set_transaction_object() |
|
| 484 | - { |
|
| 485 | - if ($this->_transaction instanceof EE_Transaction) { |
|
| 486 | - return; |
|
| 487 | - } //get out we've already set the object |
|
| 488 | - |
|
| 489 | - $TXN_ID = ! empty($this->_req_data['TXN_ID']) |
|
| 490 | - ? absint($this->_req_data['TXN_ID']) |
|
| 491 | - : false; |
|
| 492 | - |
|
| 493 | - // get transaction object |
|
| 494 | - $this->_transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID); |
|
| 495 | - $this->_session = $this->_transaction instanceof EE_Transaction |
|
| 496 | - ? $this->_transaction->get('TXN_session_data') |
|
| 497 | - : null; |
|
| 498 | - if ($this->_transaction instanceof EE_Transaction) { |
|
| 499 | - $this->_transaction->verify_abandoned_transaction_status(); |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - if (! $this->_transaction instanceof EE_Transaction) { |
|
| 503 | - $error_msg = sprintf( |
|
| 504 | - esc_html__( |
|
| 505 | - 'An error occurred and the details for the transaction with the ID # %d could not be retrieved.', |
|
| 506 | - 'event_espresso' |
|
| 507 | - ), |
|
| 508 | - $TXN_ID |
|
| 509 | - ); |
|
| 510 | - EE_Error::add_error($error_msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 511 | - } |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - |
|
| 515 | - /** |
|
| 516 | - * _transaction_legend_items |
|
| 517 | - * |
|
| 518 | - * @access protected |
|
| 519 | - * @return array |
|
| 520 | - * @throws EE_Error |
|
| 521 | - * @throws InvalidArgumentException |
|
| 522 | - * @throws ReflectionException |
|
| 523 | - * @throws InvalidDataTypeException |
|
| 524 | - * @throws InvalidInterfaceException |
|
| 525 | - */ |
|
| 526 | - protected function _transaction_legend_items() |
|
| 527 | - { |
|
| 528 | - EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 529 | - $items = array(); |
|
| 530 | - |
|
| 531 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
| 532 | - 'ee_read_global_messages', |
|
| 533 | - 'view_filtered_messages' |
|
| 534 | - )) { |
|
| 535 | - $related_for_icon = EEH_MSG_Template::get_message_action_icon('see_notifications_for'); |
|
| 536 | - if (is_array($related_for_icon) |
|
| 537 | - && isset($related_for_icon['css_class'], $related_for_icon['label']) |
|
| 538 | - ) { |
|
| 539 | - $items['view_related_messages'] = array( |
|
| 540 | - 'class' => $related_for_icon['css_class'], |
|
| 541 | - 'desc' => $related_for_icon['label'], |
|
| 542 | - ); |
|
| 543 | - } |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - $items = apply_filters( |
|
| 547 | - 'FHEE__Transactions_Admin_Page___transaction_legend_items__items', |
|
| 548 | - array_merge( |
|
| 549 | - $items, |
|
| 550 | - array( |
|
| 551 | - 'view_details' => array( |
|
| 552 | - 'class' => 'dashicons dashicons-cart', |
|
| 553 | - 'desc' => esc_html__('View Transaction Details', 'event_espresso'), |
|
| 554 | - ), |
|
| 555 | - 'view_invoice' => array( |
|
| 556 | - 'class' => 'dashicons dashicons-media-spreadsheet', |
|
| 557 | - 'desc' => esc_html__('View Transaction Invoice', 'event_espresso'), |
|
| 558 | - ), |
|
| 559 | - 'view_receipt' => array( |
|
| 560 | - 'class' => 'dashicons dashicons-media-default', |
|
| 561 | - 'desc' => esc_html__('View Transaction Receipt', 'event_espresso'), |
|
| 562 | - ), |
|
| 563 | - 'view_registration' => array( |
|
| 564 | - 'class' => 'dashicons dashicons-clipboard', |
|
| 565 | - 'desc' => esc_html__('View Registration Details', 'event_espresso'), |
|
| 566 | - ), |
|
| 567 | - 'payment_overview_link' => array( |
|
| 568 | - 'class' => 'dashicons dashicons-money', |
|
| 569 | - 'desc' => esc_html__('Make Payment on Frontend', 'event_espresso'), |
|
| 570 | - ), |
|
| 571 | - ) |
|
| 572 | - ) |
|
| 573 | - ); |
|
| 574 | - |
|
| 575 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
| 576 | - 'ee_send_message', |
|
| 577 | - 'espresso_transactions_send_payment_reminder' |
|
| 578 | - )) { |
|
| 579 | - if (EEH_MSG_Template::is_mt_active('payment_reminder')) { |
|
| 580 | - $items['send_payment_reminder'] = array( |
|
| 581 | - 'class' => 'dashicons dashicons-email-alt', |
|
| 582 | - 'desc' => esc_html__('Send Payment Reminder', 'event_espresso'), |
|
| 583 | - ); |
|
| 584 | - } else { |
|
| 585 | - $items['blank*'] = array( |
|
| 586 | - 'class' => '', |
|
| 587 | - 'desc' => '', |
|
| 588 | - ); |
|
| 589 | - } |
|
| 590 | - } else { |
|
| 591 | - $items['blank*'] = array( |
|
| 592 | - 'class' => '', |
|
| 593 | - 'desc' => '', |
|
| 594 | - ); |
|
| 595 | - } |
|
| 596 | - $more_items = apply_filters( |
|
| 597 | - 'FHEE__Transactions_Admin_Page___transaction_legend_items__more_items', |
|
| 598 | - array( |
|
| 599 | - 'overpaid' => array( |
|
| 600 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::overpaid_status_code, |
|
| 601 | - 'desc' => EEH_Template::pretty_status( |
|
| 602 | - EEM_Transaction::overpaid_status_code, |
|
| 603 | - false, |
|
| 604 | - 'sentence' |
|
| 605 | - ), |
|
| 606 | - ), |
|
| 607 | - 'complete' => array( |
|
| 608 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::complete_status_code, |
|
| 609 | - 'desc' => EEH_Template::pretty_status( |
|
| 610 | - EEM_Transaction::complete_status_code, |
|
| 611 | - false, |
|
| 612 | - 'sentence' |
|
| 613 | - ), |
|
| 614 | - ), |
|
| 615 | - 'incomplete' => array( |
|
| 616 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::incomplete_status_code, |
|
| 617 | - 'desc' => EEH_Template::pretty_status( |
|
| 618 | - EEM_Transaction::incomplete_status_code, |
|
| 619 | - false, |
|
| 620 | - 'sentence' |
|
| 621 | - ), |
|
| 622 | - ), |
|
| 623 | - 'abandoned' => array( |
|
| 624 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::abandoned_status_code, |
|
| 625 | - 'desc' => EEH_Template::pretty_status( |
|
| 626 | - EEM_Transaction::abandoned_status_code, |
|
| 627 | - false, |
|
| 628 | - 'sentence' |
|
| 629 | - ), |
|
| 630 | - ), |
|
| 631 | - 'failed' => array( |
|
| 632 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::failed_status_code, |
|
| 633 | - 'desc' => EEH_Template::pretty_status( |
|
| 634 | - EEM_Transaction::failed_status_code, |
|
| 635 | - false, |
|
| 636 | - 'sentence' |
|
| 637 | - ), |
|
| 638 | - ), |
|
| 639 | - ) |
|
| 640 | - ); |
|
| 641 | - |
|
| 642 | - return array_merge($items, $more_items); |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - |
|
| 646 | - /** |
|
| 647 | - * _transactions_overview_list_table |
|
| 648 | - * |
|
| 649 | - * @access protected |
|
| 650 | - * @return void |
|
| 651 | - * @throws DomainException |
|
| 652 | - * @throws EE_Error |
|
| 653 | - * @throws InvalidArgumentException |
|
| 654 | - * @throws InvalidDataTypeException |
|
| 655 | - * @throws InvalidInterfaceException |
|
| 656 | - * @throws ReflectionException |
|
| 657 | - */ |
|
| 658 | - protected function _transactions_overview_list_table() |
|
| 659 | - { |
|
| 660 | - $this->_admin_page_title = esc_html__('Transactions', 'event_espresso'); |
|
| 661 | - $event = isset($this->_req_data['EVT_ID']) |
|
| 662 | - ? EEM_Event::instance()->get_one_by_ID($this->_req_data['EVT_ID']) |
|
| 663 | - : null; |
|
| 664 | - $this->_template_args['admin_page_header'] = $event instanceof EE_Event |
|
| 665 | - ? sprintf( |
|
| 666 | - esc_html__( |
|
| 667 | - '%sViewing Transactions for the Event: %s%s', |
|
| 668 | - 'event_espresso' |
|
| 669 | - ), |
|
| 670 | - '<h3>', |
|
| 671 | - '<a href="' |
|
| 672 | - . EE_Admin_Page::add_query_args_and_nonce( |
|
| 673 | - array('action' => 'edit', 'post' => $event->ID()), |
|
| 674 | - EVENTS_ADMIN_URL |
|
| 675 | - ) |
|
| 676 | - . '" title="' |
|
| 677 | - . esc_attr__( |
|
| 678 | - 'Click to Edit event', |
|
| 679 | - 'event_espresso' |
|
| 680 | - ) |
|
| 681 | - . '">' . $event->get('EVT_name') . '</a>', |
|
| 682 | - '</h3>' |
|
| 683 | - ) |
|
| 684 | - : ''; |
|
| 685 | - $this->_template_args['after_list_table'] = $this->_display_legend($this->_transaction_legend_items()); |
|
| 686 | - $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * _transaction_details |
|
| 692 | - * generates HTML for the View Transaction Details Admin page |
|
| 693 | - * |
|
| 694 | - * @access protected |
|
| 695 | - * @return void |
|
| 696 | - * @throws DomainException |
|
| 697 | - * @throws EE_Error |
|
| 698 | - * @throws InvalidArgumentException |
|
| 699 | - * @throws InvalidDataTypeException |
|
| 700 | - * @throws InvalidInterfaceException |
|
| 701 | - * @throws RuntimeException |
|
| 702 | - * @throws ReflectionException |
|
| 703 | - */ |
|
| 704 | - protected function _transaction_details() |
|
| 705 | - { |
|
| 706 | - do_action('AHEE__Transactions_Admin_Page__transaction_details__start', $this->_transaction); |
|
| 707 | - |
|
| 708 | - $this->_set_transaction_status_array(); |
|
| 709 | - |
|
| 710 | - $this->_template_args = array(); |
|
| 711 | - $this->_template_args['transactions_page'] = $this->_wp_page_slug; |
|
| 712 | - |
|
| 713 | - $this->_set_transaction_object(); |
|
| 714 | - |
|
| 715 | - if (! $this->_transaction instanceof EE_Transaction) { |
|
| 716 | - return; |
|
| 717 | - } |
|
| 718 | - $primary_registration = $this->_transaction->primary_registration(); |
|
| 719 | - $attendee = $primary_registration instanceof EE_Registration |
|
| 720 | - ? $primary_registration->attendee() |
|
| 721 | - : null; |
|
| 722 | - |
|
| 723 | - $this->_template_args['txn_nmbr']['value'] = $this->_transaction->ID(); |
|
| 724 | - $this->_template_args['txn_nmbr']['label'] = esc_html__('Transaction Number', 'event_espresso'); |
|
| 725 | - |
|
| 726 | - $this->_template_args['txn_datetime']['value'] = $this->_transaction->get_i18n_datetime('TXN_timestamp'); |
|
| 727 | - $this->_template_args['txn_datetime']['label'] = esc_html__('Date', 'event_espresso'); |
|
| 728 | - |
|
| 729 | - $this->_template_args['txn_status']['value'] = self::$_txn_status[ $this->_transaction->get('STS_ID') ]; |
|
| 730 | - $this->_template_args['txn_status']['label'] = esc_html__('Transaction Status', 'event_espresso'); |
|
| 731 | - $this->_template_args['txn_status']['class'] = 'status-' . $this->_transaction->get('STS_ID'); |
|
| 732 | - |
|
| 733 | - $this->_template_args['grand_total'] = $this->_transaction->get('TXN_total'); |
|
| 734 | - $this->_template_args['total_paid'] = $this->_transaction->get('TXN_paid'); |
|
| 735 | - |
|
| 736 | - $amount_due = $this->_transaction->get('TXN_total') - $this->_transaction->get('TXN_paid'); |
|
| 737 | - $this->_template_args['amount_due'] = EEH_Template::format_currency( |
|
| 738 | - $amount_due, |
|
| 739 | - true |
|
| 740 | - ); |
|
| 741 | - if (EE_Registry::instance()->CFG->currency->sign_b4) { |
|
| 742 | - $this->_template_args['amount_due'] = EE_Registry::instance()->CFG->currency->sign |
|
| 743 | - . $this->_template_args['amount_due']; |
|
| 744 | - } else { |
|
| 745 | - $this->_template_args['amount_due'] .= EE_Registry::instance()->CFG->currency->sign; |
|
| 746 | - } |
|
| 747 | - $this->_template_args['amount_due_class'] = ''; |
|
| 748 | - |
|
| 749 | - if ($this->_transaction->get('TXN_paid') == $this->_transaction->get('TXN_total')) { |
|
| 750 | - // paid in full |
|
| 751 | - $this->_template_args['amount_due'] = false; |
|
| 752 | - } elseif ($this->_transaction->get('TXN_paid') > $this->_transaction->get('TXN_total')) { |
|
| 753 | - // overpaid |
|
| 754 | - $this->_template_args['amount_due_class'] = 'txn-overview-no-payment-spn'; |
|
| 755 | - } elseif ($this->_transaction->get('TXN_total') > 0 |
|
| 756 | - && $this->_transaction->get('TXN_paid') > 0 |
|
| 757 | - ) { |
|
| 758 | - // monies owing |
|
| 759 | - $this->_template_args['amount_due_class'] = 'txn-overview-part-payment-spn'; |
|
| 760 | - } elseif ($this->_transaction->get('TXN_total') > 0 |
|
| 761 | - && $this->_transaction->get('TXN_paid') == 0 |
|
| 762 | - ) { |
|
| 763 | - // no payments made yet |
|
| 764 | - $this->_template_args['amount_due_class'] = 'txn-overview-no-payment-spn'; |
|
| 765 | - } elseif ($this->_transaction->get('TXN_total') == 0) { |
|
| 766 | - // free event |
|
| 767 | - $this->_template_args['amount_due'] = false; |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - $payment_method = $this->_transaction->payment_method(); |
|
| 771 | - |
|
| 772 | - $this->_template_args['method_of_payment_name'] = $payment_method instanceof EE_Payment_Method |
|
| 773 | - ? $payment_method->admin_name() |
|
| 774 | - : esc_html__('Unknown', 'event_espresso'); |
|
| 775 | - |
|
| 776 | - $this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign; |
|
| 777 | - // link back to overview |
|
| 778 | - $this->_template_args['txn_overview_url'] = ! empty($_SERVER['HTTP_REFERER']) |
|
| 779 | - ? $_SERVER['HTTP_REFERER'] |
|
| 780 | - : TXN_ADMIN_URL; |
|
| 781 | - |
|
| 782 | - |
|
| 783 | - // next link |
|
| 784 | - $next_txn = $this->_transaction->next( |
|
| 785 | - null, |
|
| 786 | - array(array('STS_ID' => array('!=', EEM_Transaction::failed_status_code))), |
|
| 787 | - 'TXN_ID' |
|
| 788 | - ); |
|
| 789 | - $this->_template_args['next_transaction'] = $next_txn |
|
| 790 | - ? $this->_next_link( |
|
| 791 | - EE_Admin_Page::add_query_args_and_nonce( |
|
| 792 | - array('action' => 'view_transaction', 'TXN_ID' => $next_txn['TXN_ID']), |
|
| 793 | - TXN_ADMIN_URL |
|
| 794 | - ), |
|
| 795 | - 'dashicons dashicons-arrow-right ee-icon-size-22' |
|
| 796 | - ) |
|
| 797 | - : ''; |
|
| 798 | - // previous link |
|
| 799 | - $previous_txn = $this->_transaction->previous( |
|
| 800 | - null, |
|
| 801 | - array(array('STS_ID' => array('!=', EEM_Transaction::failed_status_code))), |
|
| 802 | - 'TXN_ID' |
|
| 803 | - ); |
|
| 804 | - $this->_template_args['previous_transaction'] = $previous_txn |
|
| 805 | - ? $this->_previous_link( |
|
| 806 | - EE_Admin_Page::add_query_args_and_nonce( |
|
| 807 | - array('action' => 'view_transaction', 'TXN_ID' => $previous_txn['TXN_ID']), |
|
| 808 | - TXN_ADMIN_URL |
|
| 809 | - ), |
|
| 810 | - 'dashicons dashicons-arrow-left ee-icon-size-22' |
|
| 811 | - ) |
|
| 812 | - : ''; |
|
| 813 | - |
|
| 814 | - // were we just redirected here after adding a new registration ??? |
|
| 815 | - if (isset( |
|
| 816 | - $this->_req_data['redirect_from'], |
|
| 817 | - $this->_req_data['EVT_ID'], |
|
| 818 | - $this->_req_data['event_name'] |
|
| 819 | - )) { |
|
| 820 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
| 821 | - 'ee_edit_registrations', |
|
| 822 | - 'espresso_registrations_new_registration', |
|
| 823 | - $this->_req_data['EVT_ID'] |
|
| 824 | - )) { |
|
| 825 | - $this->_admin_page_title .= '<a id="add-new-registration" class="add-new-h2 button-primary" href="'; |
|
| 826 | - $this->_admin_page_title .= EE_Admin_Page::add_query_args_and_nonce( |
|
| 827 | - array( |
|
| 828 | - 'page' => 'espresso_registrations', |
|
| 829 | - 'action' => 'new_registration', |
|
| 830 | - 'return' => 'default', |
|
| 831 | - 'TXN_ID' => $this->_transaction->ID(), |
|
| 832 | - 'event_id' => $this->_req_data['EVT_ID'], |
|
| 833 | - ), |
|
| 834 | - REG_ADMIN_URL |
|
| 835 | - ); |
|
| 836 | - $this->_admin_page_title .= '">'; |
|
| 837 | - |
|
| 838 | - $this->_admin_page_title .= sprintf( |
|
| 839 | - esc_html__('Add Another New Registration to Event: "%1$s" ?', 'event_espresso'), |
|
| 840 | - htmlentities(urldecode($this->_req_data['event_name']), ENT_QUOTES, 'UTF-8') |
|
| 841 | - ); |
|
| 842 | - $this->_admin_page_title .= '</a>'; |
|
| 843 | - } |
|
| 844 | - EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
| 845 | - } |
|
| 846 | - // grab messages at the last second |
|
| 847 | - $this->_template_args['notices'] = EE_Error::get_notices(); |
|
| 848 | - // path to template |
|
| 849 | - $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_header.template.php'; |
|
| 850 | - $this->_template_args['admin_page_header'] = EEH_Template::display_template( |
|
| 851 | - $template_path, |
|
| 852 | - $this->_template_args, |
|
| 853 | - true |
|
| 854 | - ); |
|
| 855 | - |
|
| 856 | - // the details template wrapper |
|
| 857 | - $this->display_admin_page_with_sidebar(); |
|
| 858 | - } |
|
| 859 | - |
|
| 860 | - |
|
| 861 | - /** |
|
| 862 | - * _transaction_details_metaboxes |
|
| 863 | - * |
|
| 864 | - * @access protected |
|
| 865 | - * @return void |
|
| 866 | - * @throws EE_Error |
|
| 867 | - * @throws InvalidArgumentException |
|
| 868 | - * @throws InvalidDataTypeException |
|
| 869 | - * @throws InvalidInterfaceException |
|
| 870 | - * @throws RuntimeException |
|
| 871 | - * @throws ReflectionException |
|
| 872 | - */ |
|
| 873 | - protected function _transaction_details_metaboxes() |
|
| 874 | - { |
|
| 875 | - |
|
| 876 | - $this->_set_transaction_object(); |
|
| 877 | - |
|
| 878 | - if (! $this->_transaction instanceof EE_Transaction) { |
|
| 879 | - return; |
|
| 880 | - } |
|
| 881 | - add_meta_box( |
|
| 882 | - 'edit-txn-details-mbox', |
|
| 883 | - esc_html__('Transaction Details', 'event_espresso'), |
|
| 884 | - array($this, 'txn_details_meta_box'), |
|
| 885 | - $this->_wp_page_slug, |
|
| 886 | - 'normal', |
|
| 887 | - 'high' |
|
| 888 | - ); |
|
| 889 | - add_meta_box( |
|
| 890 | - 'edit-txn-attendees-mbox', |
|
| 891 | - esc_html__('Attendees Registered in this Transaction', 'event_espresso'), |
|
| 892 | - array($this, 'txn_attendees_meta_box'), |
|
| 893 | - $this->_wp_page_slug, |
|
| 894 | - 'normal', |
|
| 895 | - 'high', |
|
| 896 | - array('TXN_ID' => $this->_transaction->ID()) |
|
| 897 | - ); |
|
| 898 | - add_meta_box( |
|
| 899 | - 'edit-txn-registrant-mbox', |
|
| 900 | - esc_html__('Primary Contact', 'event_espresso'), |
|
| 901 | - array($this, 'txn_registrant_side_meta_box'), |
|
| 902 | - $this->_wp_page_slug, |
|
| 903 | - 'side', |
|
| 904 | - 'high' |
|
| 905 | - ); |
|
| 906 | - add_meta_box( |
|
| 907 | - 'edit-txn-billing-info-mbox', |
|
| 908 | - esc_html__('Billing Information', 'event_espresso'), |
|
| 909 | - array($this, 'txn_billing_info_side_meta_box'), |
|
| 910 | - $this->_wp_page_slug, |
|
| 911 | - 'side', |
|
| 912 | - 'high' |
|
| 913 | - ); |
|
| 914 | - } |
|
| 915 | - |
|
| 916 | - |
|
| 917 | - /** |
|
| 918 | - * Callback for transaction actions metabox. |
|
| 919 | - * |
|
| 920 | - * @param EE_Transaction|null $transaction |
|
| 921 | - * @throws DomainException |
|
| 922 | - * @throws EE_Error |
|
| 923 | - * @throws InvalidArgumentException |
|
| 924 | - * @throws InvalidDataTypeException |
|
| 925 | - * @throws InvalidInterfaceException |
|
| 926 | - * @throws ReflectionException |
|
| 927 | - * @throws RuntimeException |
|
| 928 | - */ |
|
| 929 | - public function getActionButtons(EE_Transaction $transaction = null) |
|
| 930 | - { |
|
| 931 | - $content = ''; |
|
| 932 | - $actions = array(); |
|
| 933 | - if (! $transaction instanceof EE_Transaction) { |
|
| 934 | - return $content; |
|
| 935 | - } |
|
| 936 | - /** @var EE_Registration $primary_registration */ |
|
| 937 | - $primary_registration = $transaction->primary_registration(); |
|
| 938 | - $attendee = $primary_registration instanceof EE_Registration |
|
| 939 | - ? $primary_registration->attendee() |
|
| 940 | - : null; |
|
| 941 | - |
|
| 942 | - if ($attendee instanceof EE_Attendee |
|
| 943 | - && EE_Registry::instance()->CAP->current_user_can( |
|
| 944 | - 'ee_send_message', |
|
| 945 | - 'espresso_transactions_send_payment_reminder' |
|
| 946 | - ) |
|
| 947 | - ) { |
|
| 948 | - $actions['payment_reminder'] = |
|
| 949 | - EEH_MSG_Template::is_mt_active('payment_reminder') |
|
| 950 | - && $this->_transaction->get('STS_ID') !== EEM_Transaction::complete_status_code |
|
| 951 | - && $this->_transaction->get('STS_ID') !== EEM_Transaction::overpaid_status_code |
|
| 952 | - ? EEH_Template::get_button_or_link( |
|
| 953 | - EE_Admin_Page::add_query_args_and_nonce( |
|
| 954 | - array( |
|
| 955 | - 'action' => 'send_payment_reminder', |
|
| 956 | - 'TXN_ID' => $this->_transaction->ID(), |
|
| 957 | - 'redirect_to' => 'view_transaction', |
|
| 958 | - ), |
|
| 959 | - TXN_ADMIN_URL |
|
| 960 | - ), |
|
| 961 | - esc_html__(' Send Payment Reminder', 'event_espresso'), |
|
| 962 | - 'button secondary-button', |
|
| 963 | - 'dashicons dashicons-email-alt' |
|
| 964 | - ) |
|
| 965 | - : ''; |
|
| 966 | - } |
|
| 967 | - |
|
| 968 | - if ($primary_registration instanceof EE_Registration |
|
| 969 | - && EEH_MSG_Template::is_mt_active('receipt') |
|
| 970 | - ) { |
|
| 971 | - $actions['receipt'] = EEH_Template::get_button_or_link( |
|
| 972 | - $primary_registration->receipt_url(), |
|
| 973 | - esc_html__('View Receipt', 'event_espresso'), |
|
| 974 | - 'button secondary-button', |
|
| 975 | - 'dashicons dashicons-media-default' |
|
| 976 | - ); |
|
| 977 | - } |
|
| 978 | - |
|
| 979 | - if ($primary_registration instanceof EE_Registration |
|
| 980 | - && EEH_MSG_Template::is_mt_active('invoice') |
|
| 981 | - ) { |
|
| 982 | - $actions['invoice'] = EEH_Template::get_button_or_link( |
|
| 983 | - $primary_registration->invoice_url(), |
|
| 984 | - esc_html__('View Invoice', 'event_espresso'), |
|
| 985 | - 'button secondary-button', |
|
| 986 | - 'dashicons dashicons-media-spreadsheet' |
|
| 987 | - ); |
|
| 988 | - } |
|
| 989 | - $actions = array_filter( |
|
| 990 | - apply_filters('FHEE__Transactions_Admin_Page__getActionButtons__actions', $actions, $transaction) |
|
| 991 | - ); |
|
| 992 | - if ($actions) { |
|
| 993 | - $content = '<ul>'; |
|
| 994 | - $content .= '<li>' . implode('</li><li>', $actions) . '</li>'; |
|
| 995 | - $content .= '</uL>'; |
|
| 996 | - } |
|
| 997 | - return $content; |
|
| 998 | - } |
|
| 999 | - |
|
| 1000 | - |
|
| 1001 | - /** |
|
| 1002 | - * txn_details_meta_box |
|
| 1003 | - * generates HTML for the Transaction main meta box |
|
| 1004 | - * |
|
| 1005 | - * @return void |
|
| 1006 | - * @throws DomainException |
|
| 1007 | - * @throws EE_Error |
|
| 1008 | - * @throws InvalidArgumentException |
|
| 1009 | - * @throws InvalidDataTypeException |
|
| 1010 | - * @throws InvalidInterfaceException |
|
| 1011 | - * @throws RuntimeException |
|
| 1012 | - * @throws ReflectionException |
|
| 1013 | - */ |
|
| 1014 | - public function txn_details_meta_box() |
|
| 1015 | - { |
|
| 1016 | - $this->_set_transaction_object(); |
|
| 1017 | - $this->_template_args['TXN_ID'] = $this->_transaction->ID(); |
|
| 1018 | - $this->_template_args['attendee'] = $this->_transaction->primary_registration() instanceof EE_Registration |
|
| 1019 | - ? $this->_transaction->primary_registration()->attendee() |
|
| 1020 | - : null; |
|
| 1021 | - $this->_template_args['can_edit_payments'] = EE_Registry::instance()->CAP->current_user_can( |
|
| 1022 | - 'ee_edit_payments', |
|
| 1023 | - 'apply_payment_or_refund_from_registration_details' |
|
| 1024 | - ); |
|
| 1025 | - $this->_template_args['can_delete_payments'] = EE_Registry::instance()->CAP->current_user_can( |
|
| 1026 | - 'ee_delete_payments', |
|
| 1027 | - 'delete_payment_from_registration_details' |
|
| 1028 | - ); |
|
| 1029 | - |
|
| 1030 | - // get line table |
|
| 1031 | - EEH_Autoloader::register_line_item_display_autoloaders(); |
|
| 1032 | - $Line_Item_Display = new EE_Line_Item_Display( |
|
| 1033 | - 'admin_table', |
|
| 1034 | - 'EE_Admin_Table_Line_Item_Display_Strategy' |
|
| 1035 | - ); |
|
| 1036 | - $this->_template_args['line_item_table'] = $Line_Item_Display->display_line_item( |
|
| 1037 | - $this->_transaction->total_line_item() |
|
| 1038 | - ); |
|
| 1039 | - $this->_template_args['REG_code'] = $this->_transaction->get_first_related('Registration') |
|
| 1040 | - ->get('REG_code'); |
|
| 1041 | - |
|
| 1042 | - // process taxes |
|
| 1043 | - $taxes = $this->_transaction->get_many_related( |
|
| 1044 | - 'Line_Item', |
|
| 1045 | - array(array('LIN_type' => EEM_Line_Item::type_tax)) |
|
| 1046 | - ); |
|
| 1047 | - $this->_template_args['taxes'] = ! empty($taxes) ? $taxes : false; |
|
| 1048 | - |
|
| 1049 | - $this->_template_args['grand_total'] = EEH_Template::format_currency( |
|
| 1050 | - $this->_transaction->get('TXN_total'), |
|
| 1051 | - false, |
|
| 1052 | - false |
|
| 1053 | - ); |
|
| 1054 | - $this->_template_args['grand_raw_total'] = $this->_transaction->get('TXN_total'); |
|
| 1055 | - $this->_template_args['TXN_status'] = $this->_transaction->get('STS_ID'); |
|
| 1056 | - |
|
| 1057 | - // process payment details |
|
| 1058 | - $payments = $this->_transaction->get_many_related('Payment'); |
|
| 1059 | - if (! empty($payments)) { |
|
| 1060 | - $this->_template_args['payments'] = $payments; |
|
| 1061 | - $this->_template_args['existing_reg_payments'] = $this->_get_registration_payment_IDs($payments); |
|
| 1062 | - } else { |
|
| 1063 | - $this->_template_args['payments'] = false; |
|
| 1064 | - $this->_template_args['existing_reg_payments'] = array(); |
|
| 1065 | - } |
|
| 1066 | - |
|
| 1067 | - $this->_template_args['edit_payment_url'] = add_query_arg(array('action' => 'edit_payment'), TXN_ADMIN_URL); |
|
| 1068 | - $this->_template_args['delete_payment_url'] = add_query_arg( |
|
| 1069 | - array('action' => 'espresso_delete_payment'), |
|
| 1070 | - TXN_ADMIN_URL |
|
| 1071 | - ); |
|
| 1072 | - |
|
| 1073 | - if (isset($txn_details['invoice_number'])) { |
|
| 1074 | - $this->_template_args['txn_details']['invoice_number']['value'] = $this->_template_args['REG_code']; |
|
| 1075 | - $this->_template_args['txn_details']['invoice_number']['label'] = esc_html__( |
|
| 1076 | - 'Invoice Number', |
|
| 1077 | - 'event_espresso' |
|
| 1078 | - ); |
|
| 1079 | - } |
|
| 1080 | - |
|
| 1081 | - $this->_template_args['txn_details']['registration_session']['value'] = $this->_transaction |
|
| 1082 | - ->get_first_related('Registration') |
|
| 1083 | - ->get('REG_session'); |
|
| 1084 | - $this->_template_args['txn_details']['registration_session']['label'] = esc_html__( |
|
| 1085 | - 'Registration Session', |
|
| 1086 | - 'event_espresso' |
|
| 1087 | - ); |
|
| 1088 | - |
|
| 1089 | - $this->_template_args['txn_details']['ip_address']['value'] = isset($this->_session['ip_address']) |
|
| 1090 | - ? $this->_session['ip_address'] |
|
| 1091 | - : ''; |
|
| 1092 | - $this->_template_args['txn_details']['ip_address']['label'] = esc_html__( |
|
| 1093 | - 'Transaction placed from IP', |
|
| 1094 | - 'event_espresso' |
|
| 1095 | - ); |
|
| 1096 | - |
|
| 1097 | - $this->_template_args['txn_details']['user_agent']['value'] = isset($this->_session['user_agent']) |
|
| 1098 | - ? $this->_session['user_agent'] |
|
| 1099 | - : ''; |
|
| 1100 | - $this->_template_args['txn_details']['user_agent']['label'] = esc_html__( |
|
| 1101 | - 'Registrant User Agent', |
|
| 1102 | - 'event_espresso' |
|
| 1103 | - ); |
|
| 1104 | - |
|
| 1105 | - $reg_steps = '<ul>'; |
|
| 1106 | - foreach ($this->_transaction->reg_steps() as $reg_step => $reg_step_status) { |
|
| 1107 | - if ($reg_step_status === true) { |
|
| 1108 | - $reg_steps .= '<li style="color:#70cc50">' |
|
| 1109 | - . sprintf( |
|
| 1110 | - esc_html__('%1$s : Completed', 'event_espresso'), |
|
| 1111 | - ucwords(str_replace('_', ' ', $reg_step)) |
|
| 1112 | - ) |
|
| 1113 | - . '</li>'; |
|
| 1114 | - } elseif (is_numeric($reg_step_status) && $reg_step_status !== false) { |
|
| 1115 | - $reg_steps .= '<li style="color:#2EA2CC">' |
|
| 1116 | - . sprintf( |
|
| 1117 | - esc_html__('%1$s : Initiated %2$s', 'event_espresso'), |
|
| 1118 | - ucwords(str_replace('_', ' ', $reg_step)), |
|
| 1119 | - date( |
|
| 1120 | - get_option('date_format') . ' ' . get_option('time_format'), |
|
| 1121 | - ($reg_step_status + (get_option('gmt_offset') * HOUR_IN_SECONDS)) |
|
| 1122 | - ) |
|
| 1123 | - ) |
|
| 1124 | - . '</li>'; |
|
| 1125 | - } else { |
|
| 1126 | - $reg_steps .= '<li style="color:#E76700">' |
|
| 1127 | - . sprintf( |
|
| 1128 | - esc_html__('%1$s : Never Initiated', 'event_espresso'), |
|
| 1129 | - ucwords(str_replace('_', ' ', $reg_step)) |
|
| 1130 | - ) |
|
| 1131 | - . '</li>'; |
|
| 1132 | - } |
|
| 1133 | - } |
|
| 1134 | - $reg_steps .= '</ul>'; |
|
| 1135 | - $this->_template_args['txn_details']['reg_steps']['value'] = $reg_steps; |
|
| 1136 | - $this->_template_args['txn_details']['reg_steps']['label'] = esc_html__( |
|
| 1137 | - 'Registration Step Progress', |
|
| 1138 | - 'event_espresso' |
|
| 1139 | - ); |
|
| 1140 | - |
|
| 1141 | - |
|
| 1142 | - $this->_get_registrations_to_apply_payment_to(); |
|
| 1143 | - $this->_get_payment_methods($payments); |
|
| 1144 | - $this->_get_payment_status_array(); |
|
| 1145 | - $this->_get_reg_status_selection(); // sets up the template args for the reg status array for the transaction. |
|
| 1146 | - |
|
| 1147 | - $this->_template_args['transaction_form_url'] = add_query_arg( |
|
| 1148 | - array( |
|
| 1149 | - 'action' => 'edit_transaction', |
|
| 1150 | - 'process' => 'transaction', |
|
| 1151 | - ), |
|
| 1152 | - TXN_ADMIN_URL |
|
| 1153 | - ); |
|
| 1154 | - $this->_template_args['apply_payment_form_url'] = add_query_arg( |
|
| 1155 | - array( |
|
| 1156 | - 'page' => 'espresso_transactions', |
|
| 1157 | - 'action' => 'espresso_apply_payment', |
|
| 1158 | - ), |
|
| 1159 | - WP_AJAX_URL |
|
| 1160 | - ); |
|
| 1161 | - $this->_template_args['delete_payment_form_url'] = add_query_arg( |
|
| 1162 | - array( |
|
| 1163 | - 'page' => 'espresso_transactions', |
|
| 1164 | - 'action' => 'espresso_delete_payment', |
|
| 1165 | - ), |
|
| 1166 | - WP_AJAX_URL |
|
| 1167 | - ); |
|
| 1168 | - |
|
| 1169 | - $this->_template_args['action_buttons'] = $this->getActionButtons($this->_transaction); |
|
| 1170 | - |
|
| 1171 | - // 'espresso_delete_payment_nonce' |
|
| 1172 | - |
|
| 1173 | - $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_txn_details.template.php'; |
|
| 1174 | - echo EEH_Template::display_template($template_path, $this->_template_args, true); |
|
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - |
|
| 1178 | - /** |
|
| 1179 | - * _get_registration_payment_IDs |
|
| 1180 | - * generates an array of Payment IDs and their corresponding Registration IDs |
|
| 1181 | - * |
|
| 1182 | - * @access protected |
|
| 1183 | - * @param EE_Payment[] $payments |
|
| 1184 | - * @return array |
|
| 1185 | - * @throws EE_Error |
|
| 1186 | - * @throws InvalidArgumentException |
|
| 1187 | - * @throws InvalidDataTypeException |
|
| 1188 | - * @throws InvalidInterfaceException |
|
| 1189 | - * @throws ReflectionException |
|
| 1190 | - */ |
|
| 1191 | - protected function _get_registration_payment_IDs($payments = array()) |
|
| 1192 | - { |
|
| 1193 | - $existing_reg_payments = array(); |
|
| 1194 | - // get all reg payments for these payments |
|
| 1195 | - $reg_payments = EEM_Registration_Payment::instance()->get_all( |
|
| 1196 | - array( |
|
| 1197 | - array( |
|
| 1198 | - 'PAY_ID' => array( |
|
| 1199 | - 'IN', |
|
| 1200 | - array_keys($payments), |
|
| 1201 | - ), |
|
| 1202 | - ), |
|
| 1203 | - ) |
|
| 1204 | - ); |
|
| 1205 | - if (! empty($reg_payments)) { |
|
| 1206 | - foreach ($payments as $payment) { |
|
| 1207 | - if (! $payment instanceof EE_Payment) { |
|
| 1208 | - continue; |
|
| 1209 | - } elseif (! isset($existing_reg_payments[ $payment->ID() ])) { |
|
| 1210 | - $existing_reg_payments[ $payment->ID() ] = array(); |
|
| 1211 | - } |
|
| 1212 | - foreach ($reg_payments as $reg_payment) { |
|
| 1213 | - if ($reg_payment instanceof EE_Registration_Payment |
|
| 1214 | - && $reg_payment->payment_ID() === $payment->ID() |
|
| 1215 | - ) { |
|
| 1216 | - $existing_reg_payments[ $payment->ID() ][] = $reg_payment->registration_ID(); |
|
| 1217 | - } |
|
| 1218 | - } |
|
| 1219 | - } |
|
| 1220 | - } |
|
| 1221 | - |
|
| 1222 | - return $existing_reg_payments; |
|
| 1223 | - } |
|
| 1224 | - |
|
| 1225 | - |
|
| 1226 | - /** |
|
| 1227 | - * _get_registrations_to_apply_payment_to |
|
| 1228 | - * generates HTML for displaying a series of checkboxes in the admin payment modal window |
|
| 1229 | - * which allows the admin to only apply the payment to the specific registrations |
|
| 1230 | - * |
|
| 1231 | - * @access protected |
|
| 1232 | - * @return void |
|
| 1233 | - * @throws \EE_Error |
|
| 1234 | - */ |
|
| 1235 | - protected function _get_registrations_to_apply_payment_to() |
|
| 1236 | - { |
|
| 1237 | - // we want any registration with an active status (ie: not deleted or cancelled) |
|
| 1238 | - $query_params = array( |
|
| 1239 | - array( |
|
| 1240 | - 'STS_ID' => array( |
|
| 1241 | - 'IN', |
|
| 1242 | - array( |
|
| 1243 | - EEM_Registration::status_id_approved, |
|
| 1244 | - EEM_Registration::status_id_pending_payment, |
|
| 1245 | - EEM_Registration::status_id_not_approved, |
|
| 1246 | - ), |
|
| 1247 | - ), |
|
| 1248 | - ), |
|
| 1249 | - ); |
|
| 1250 | - $registrations_to_apply_payment_to = EEH_HTML::br() |
|
| 1251 | - . EEH_HTML::div( |
|
| 1252 | - '', |
|
| 1253 | - 'txn-admin-apply-payment-to-registrations-dv', |
|
| 1254 | - '', |
|
| 1255 | - 'clear: both; margin: 1.5em 0 0; display: none;' |
|
| 1256 | - ); |
|
| 1257 | - $registrations_to_apply_payment_to .= EEH_HTML::br() . EEH_HTML::div('', '', 'admin-primary-mbox-tbl-wrap'); |
|
| 1258 | - $registrations_to_apply_payment_to .= EEH_HTML::table('', '', 'admin-primary-mbox-tbl'); |
|
| 1259 | - $registrations_to_apply_payment_to .= EEH_HTML::thead( |
|
| 1260 | - EEH_HTML::tr( |
|
| 1261 | - EEH_HTML::th(esc_html__('ID', 'event_espresso')) . |
|
| 1262 | - EEH_HTML::th(esc_html__('Registrant', 'event_espresso')) . |
|
| 1263 | - EEH_HTML::th(esc_html__('Ticket', 'event_espresso')) . |
|
| 1264 | - EEH_HTML::th(esc_html__('Event', 'event_espresso')) . |
|
| 1265 | - EEH_HTML::th(esc_html__('Paid', 'event_espresso'), '', 'txn-admin-payment-paid-td jst-cntr') . |
|
| 1266 | - EEH_HTML::th(esc_html__('Owing', 'event_espresso'), '', 'txn-admin-payment-owing-td jst-cntr') . |
|
| 1267 | - EEH_HTML::th(esc_html__('Apply', 'event_espresso'), '', 'jst-cntr') |
|
| 1268 | - ) |
|
| 1269 | - ); |
|
| 1270 | - $registrations_to_apply_payment_to .= EEH_HTML::tbody(); |
|
| 1271 | - // get registrations for TXN |
|
| 1272 | - $registrations = $this->_transaction->registrations($query_params); |
|
| 1273 | - $existing_reg_payments = $this->_template_args['existing_reg_payments']; |
|
| 1274 | - foreach ($registrations as $registration) { |
|
| 1275 | - if ($registration instanceof EE_Registration) { |
|
| 1276 | - $attendee_name = $registration->attendee() instanceof EE_Attendee |
|
| 1277 | - ? $registration->attendee()->full_name() |
|
| 1278 | - : esc_html__('Unknown Attendee', 'event_espresso'); |
|
| 1279 | - $owing = $registration->final_price() - $registration->paid(); |
|
| 1280 | - $taxable = $registration->ticket()->taxable() |
|
| 1281 | - ? ' <span class="smaller-text lt-grey-text"> ' . esc_html__('+ tax', 'event_espresso') . '</span>' |
|
| 1282 | - : ''; |
|
| 1283 | - $checked = empty($existing_reg_payments) || in_array($registration->ID(), $existing_reg_payments) |
|
| 1284 | - ? ' checked="checked"' |
|
| 1285 | - : ''; |
|
| 1286 | - $disabled = $registration->final_price() > 0 ? '' : ' disabled'; |
|
| 1287 | - $registrations_to_apply_payment_to .= EEH_HTML::tr( |
|
| 1288 | - EEH_HTML::td($registration->ID()) . |
|
| 1289 | - EEH_HTML::td($attendee_name) . |
|
| 1290 | - EEH_HTML::td( |
|
| 1291 | - $registration->ticket()->name() . ' : ' . $registration->ticket()->pretty_price() . $taxable |
|
| 1292 | - ) . |
|
| 1293 | - EEH_HTML::td($registration->event_name()) . |
|
| 1294 | - EEH_HTML::td($registration->pretty_paid(), '', 'txn-admin-payment-paid-td jst-cntr') . |
|
| 1295 | - EEH_HTML::td(EEH_Template::format_currency($owing), '', 'txn-admin-payment-owing-td jst-cntr') . |
|
| 1296 | - EEH_HTML::td( |
|
| 1297 | - '<input type="checkbox" value="' . $registration->ID() |
|
| 1298 | - . '" name="txn_admin_payment[registrations]"' |
|
| 1299 | - . $checked . $disabled . '>', |
|
| 1300 | - '', |
|
| 1301 | - 'jst-cntr' |
|
| 1302 | - ), |
|
| 1303 | - 'apply-payment-registration-row-' . $registration->ID() |
|
| 1304 | - ); |
|
| 1305 | - } |
|
| 1306 | - } |
|
| 1307 | - $registrations_to_apply_payment_to .= EEH_HTML::tbodyx(); |
|
| 1308 | - $registrations_to_apply_payment_to .= EEH_HTML::tablex(); |
|
| 1309 | - $registrations_to_apply_payment_to .= EEH_HTML::divx(); |
|
| 1310 | - $registrations_to_apply_payment_to .= EEH_HTML::p( |
|
| 1311 | - esc_html__( |
|
| 1312 | - 'The payment will only be applied to the registrations that have a check mark in their corresponding check box. Checkboxes for free registrations have been disabled.', |
|
| 1313 | - 'event_espresso' |
|
| 1314 | - ), |
|
| 1315 | - '', |
|
| 1316 | - 'clear description' |
|
| 1317 | - ); |
|
| 1318 | - $registrations_to_apply_payment_to .= EEH_HTML::divx(); |
|
| 1319 | - $this->_template_args['registrations_to_apply_payment_to'] = $registrations_to_apply_payment_to; |
|
| 1320 | - } |
|
| 1321 | - |
|
| 1322 | - |
|
| 1323 | - /** |
|
| 1324 | - * _get_reg_status_selection |
|
| 1325 | - * |
|
| 1326 | - * @todo this will need to be adjusted either once MER comes along OR we move default reg status to tickets |
|
| 1327 | - * instead of events. |
|
| 1328 | - * @access protected |
|
| 1329 | - * @return void |
|
| 1330 | - * @throws EE_Error |
|
| 1331 | - */ |
|
| 1332 | - protected function _get_reg_status_selection() |
|
| 1333 | - { |
|
| 1334 | - // first get all possible statuses |
|
| 1335 | - $statuses = EEM_Registration::reg_status_array(array(), true); |
|
| 1336 | - // let's add a "don't change" option. |
|
| 1337 | - $status_array['NAN'] = esc_html__('Leave the Same', 'event_espresso'); |
|
| 1338 | - $status_array = array_merge($status_array, $statuses); |
|
| 1339 | - $this->_template_args['status_change_select'] = EEH_Form_Fields::select_input( |
|
| 1340 | - 'txn_reg_status_change[reg_status]', |
|
| 1341 | - $status_array, |
|
| 1342 | - 'NAN', |
|
| 1343 | - 'id="txn-admin-payment-reg-status-inp"', |
|
| 1344 | - 'txn-reg-status-change-reg-status' |
|
| 1345 | - ); |
|
| 1346 | - $this->_template_args['delete_status_change_select'] = EEH_Form_Fields::select_input( |
|
| 1347 | - 'delete_txn_reg_status_change[reg_status]', |
|
| 1348 | - $status_array, |
|
| 1349 | - 'NAN', |
|
| 1350 | - 'delete-txn-admin-payment-reg-status-inp', |
|
| 1351 | - 'delete-txn-reg-status-change-reg-status' |
|
| 1352 | - ); |
|
| 1353 | - } |
|
| 1354 | - |
|
| 1355 | - |
|
| 1356 | - /** |
|
| 1357 | - * _get_payment_methods |
|
| 1358 | - * Gets all the payment methods available generally, or the ones that are already |
|
| 1359 | - * selected on these payments (in case their payment methods are no longer active). |
|
| 1360 | - * Has the side-effect of updating the template args' payment_methods item |
|
| 1361 | - * |
|
| 1362 | - * @access private |
|
| 1363 | - * @param EE_Payment[] to show on this page |
|
| 1364 | - * @return void |
|
| 1365 | - * @throws EE_Error |
|
| 1366 | - * @throws InvalidArgumentException |
|
| 1367 | - * @throws InvalidDataTypeException |
|
| 1368 | - * @throws InvalidInterfaceException |
|
| 1369 | - * @throws ReflectionException |
|
| 1370 | - */ |
|
| 1371 | - private function _get_payment_methods($payments = array()) |
|
| 1372 | - { |
|
| 1373 | - $payment_methods_of_payments = array(); |
|
| 1374 | - foreach ($payments as $payment) { |
|
| 1375 | - if ($payment instanceof EE_Payment) { |
|
| 1376 | - $payment_methods_of_payments[] = $payment->get('PMD_ID'); |
|
| 1377 | - } |
|
| 1378 | - } |
|
| 1379 | - if ($payment_methods_of_payments) { |
|
| 1380 | - $query_args = array( |
|
| 1381 | - array( |
|
| 1382 | - 'OR*payment_method_for_payment' => array( |
|
| 1383 | - 'PMD_ID' => array('IN', $payment_methods_of_payments), |
|
| 1384 | - 'PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%'), |
|
| 1385 | - ), |
|
| 1386 | - ), |
|
| 1387 | - ); |
|
| 1388 | - } else { |
|
| 1389 | - $query_args = array(array('PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%'))); |
|
| 1390 | - } |
|
| 1391 | - $this->_template_args['payment_methods'] = EEM_Payment_Method::instance()->get_all($query_args); |
|
| 1392 | - } |
|
| 1393 | - |
|
| 1394 | - |
|
| 1395 | - /** |
|
| 1396 | - * txn_attendees_meta_box |
|
| 1397 | - * generates HTML for the Attendees Transaction main meta box |
|
| 1398 | - * |
|
| 1399 | - * @access public |
|
| 1400 | - * @param WP_Post $post |
|
| 1401 | - * @param array $metabox |
|
| 1402 | - * @return void |
|
| 1403 | - * @throws DomainException |
|
| 1404 | - * @throws EE_Error |
|
| 1405 | - */ |
|
| 1406 | - public function txn_attendees_meta_box($post, $metabox = array('args' => array())) |
|
| 1407 | - { |
|
| 1408 | - |
|
| 1409 | - /** @noinspection NonSecureExtractUsageInspection */ |
|
| 1410 | - extract($metabox['args']); |
|
| 1411 | - $this->_template_args['post'] = $post; |
|
| 1412 | - $this->_template_args['event_attendees'] = array(); |
|
| 1413 | - // process items in cart |
|
| 1414 | - $line_items = $this->_transaction->get_many_related( |
|
| 1415 | - 'Line_Item', |
|
| 1416 | - array(array('LIN_type' => 'line-item')) |
|
| 1417 | - ); |
|
| 1418 | - if (! empty($line_items)) { |
|
| 1419 | - foreach ($line_items as $item) { |
|
| 1420 | - if ($item instanceof EE_Line_Item) { |
|
| 1421 | - switch ($item->OBJ_type()) { |
|
| 1422 | - case 'Event': |
|
| 1423 | - break; |
|
| 1424 | - case 'Ticket': |
|
| 1425 | - $ticket = $item->ticket(); |
|
| 1426 | - // right now we're only handling tickets here. |
|
| 1427 | - // Cause its expected that only tickets will have attendees right? |
|
| 1428 | - if (! $ticket instanceof EE_Ticket) { |
|
| 1429 | - break; |
|
| 1430 | - } |
|
| 1431 | - try { |
|
| 1432 | - $event_name = $ticket->get_event_name(); |
|
| 1433 | - } catch (Exception $e) { |
|
| 1434 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 1435 | - $event_name = esc_html__('Unknown Event', 'event_espresso'); |
|
| 1436 | - } |
|
| 1437 | - $event_name .= ' - ' . $item->get('LIN_name'); |
|
| 1438 | - $ticket_price = EEH_Template::format_currency($item->get('LIN_unit_price')); |
|
| 1439 | - // now get all of the registrations for this transaction that use this ticket |
|
| 1440 | - $registrations = $ticket->get_many_related( |
|
| 1441 | - 'Registration', |
|
| 1442 | - array(array('TXN_ID' => $this->_transaction->ID())) |
|
| 1443 | - ); |
|
| 1444 | - foreach ($registrations as $registration) { |
|
| 1445 | - if (! $registration instanceof EE_Registration) { |
|
| 1446 | - break; |
|
| 1447 | - } |
|
| 1448 | - $this->_template_args['event_attendees'][ $registration->ID() ]['STS_ID'] |
|
| 1449 | - = $registration->status_ID(); |
|
| 1450 | - $this->_template_args['event_attendees'][ $registration->ID() ]['att_num'] |
|
| 1451 | - = $registration->count(); |
|
| 1452 | - $this->_template_args['event_attendees'][ $registration->ID() ]['event_ticket_name'] |
|
| 1453 | - = $event_name; |
|
| 1454 | - $this->_template_args['event_attendees'][ $registration->ID() ]['ticket_price'] |
|
| 1455 | - = $ticket_price; |
|
| 1456 | - // attendee info |
|
| 1457 | - $attendee = $registration->get_first_related('Attendee'); |
|
| 1458 | - if ($attendee instanceof EE_Attendee) { |
|
| 1459 | - $this->_template_args['event_attendees'][ $registration->ID() ]['att_id'] |
|
| 1460 | - = $attendee->ID(); |
|
| 1461 | - $this->_template_args['event_attendees'][ $registration->ID() ]['attendee'] |
|
| 1462 | - = $attendee->full_name(); |
|
| 1463 | - $this->_template_args['event_attendees'][ $registration->ID() ]['email'] |
|
| 1464 | - = '<a href="mailto:' . $attendee->email() . '?subject=' . $event_name |
|
| 1465 | - . esc_html__( |
|
| 1466 | - ' Event', |
|
| 1467 | - 'event_espresso' |
|
| 1468 | - ) |
|
| 1469 | - . '">' . $attendee->email() . '</a>'; |
|
| 1470 | - $this->_template_args['event_attendees'][ $registration->ID() ]['address'] |
|
| 1471 | - = EEH_Address::format($attendee, 'inline', false, false); |
|
| 1472 | - } else { |
|
| 1473 | - $this->_template_args['event_attendees'][ $registration->ID() ]['att_id'] = ''; |
|
| 1474 | - $this->_template_args['event_attendees'][ $registration->ID() ]['attendee'] = ''; |
|
| 1475 | - $this->_template_args['event_attendees'][ $registration->ID() ]['email'] = ''; |
|
| 1476 | - $this->_template_args['event_attendees'][ $registration->ID() ]['address'] = ''; |
|
| 1477 | - } |
|
| 1478 | - } |
|
| 1479 | - break; |
|
| 1480 | - } |
|
| 1481 | - } |
|
| 1482 | - } |
|
| 1483 | - |
|
| 1484 | - $this->_template_args['transaction_form_url'] = add_query_arg( |
|
| 1485 | - array( |
|
| 1486 | - 'action' => 'edit_transaction', |
|
| 1487 | - 'process' => 'attendees', |
|
| 1488 | - ), |
|
| 1489 | - TXN_ADMIN_URL |
|
| 1490 | - ); |
|
| 1491 | - echo EEH_Template::display_template( |
|
| 1492 | - TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_attendees.template.php', |
|
| 1493 | - $this->_template_args, |
|
| 1494 | - true |
|
| 1495 | - ); |
|
| 1496 | - } else { |
|
| 1497 | - echo sprintf( |
|
| 1498 | - esc_html__( |
|
| 1499 | - '%1$sFor some reason, there are no attendees registered for this transaction. Likely the registration was abandoned in process.%2$s', |
|
| 1500 | - 'event_espresso' |
|
| 1501 | - ), |
|
| 1502 | - '<p class="important-notice">', |
|
| 1503 | - '</p>' |
|
| 1504 | - ); |
|
| 1505 | - } |
|
| 1506 | - } |
|
| 1507 | - |
|
| 1508 | - |
|
| 1509 | - /** |
|
| 1510 | - * txn_registrant_side_meta_box |
|
| 1511 | - * generates HTML for the Edit Transaction side meta box |
|
| 1512 | - * |
|
| 1513 | - * @access public |
|
| 1514 | - * @return void |
|
| 1515 | - * @throws DomainException |
|
| 1516 | - * @throws EE_Error |
|
| 1517 | - * @throws InvalidArgumentException |
|
| 1518 | - * @throws InvalidDataTypeException |
|
| 1519 | - * @throws InvalidInterfaceException |
|
| 1520 | - * @throws ReflectionException |
|
| 1521 | - */ |
|
| 1522 | - public function txn_registrant_side_meta_box() |
|
| 1523 | - { |
|
| 1524 | - $primary_att = $this->_transaction->primary_registration() instanceof EE_Registration |
|
| 1525 | - ? $this->_transaction->primary_registration()->get_first_related('Attendee') |
|
| 1526 | - : null; |
|
| 1527 | - if (! $primary_att instanceof EE_Attendee) { |
|
| 1528 | - $this->_template_args['no_attendee_message'] = esc_html__( |
|
| 1529 | - 'There is no attached contact for this transaction. The transaction either failed due to an error or was abandoned.', |
|
| 1530 | - 'event_espresso' |
|
| 1531 | - ); |
|
| 1532 | - $primary_att = EEM_Attendee::instance()->create_default_object(); |
|
| 1533 | - } |
|
| 1534 | - $this->_template_args['ATT_ID'] = $primary_att->ID(); |
|
| 1535 | - $this->_template_args['prime_reg_fname'] = $primary_att->fname(); |
|
| 1536 | - $this->_template_args['prime_reg_lname'] = $primary_att->lname(); |
|
| 1537 | - $this->_template_args['prime_reg_email'] = $primary_att->email(); |
|
| 1538 | - $this->_template_args['prime_reg_phone'] = $primary_att->phone(); |
|
| 1539 | - $this->_template_args['edit_attendee_url'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 1540 | - array( |
|
| 1541 | - 'action' => 'edit_attendee', |
|
| 1542 | - 'post' => $primary_att->ID(), |
|
| 1543 | - ), |
|
| 1544 | - REG_ADMIN_URL |
|
| 1545 | - ); |
|
| 1546 | - // get formatted address for registrant |
|
| 1547 | - $this->_template_args['formatted_address'] = EEH_Address::format($primary_att); |
|
| 1548 | - echo EEH_Template::display_template( |
|
| 1549 | - TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_registrant.template.php', |
|
| 1550 | - $this->_template_args, |
|
| 1551 | - true |
|
| 1552 | - ); |
|
| 1553 | - } |
|
| 1554 | - |
|
| 1555 | - |
|
| 1556 | - /** |
|
| 1557 | - * txn_billing_info_side_meta_box |
|
| 1558 | - * generates HTML for the Edit Transaction side meta box |
|
| 1559 | - * |
|
| 1560 | - * @access public |
|
| 1561 | - * @return void |
|
| 1562 | - * @throws DomainException |
|
| 1563 | - * @throws EE_Error |
|
| 1564 | - */ |
|
| 1565 | - public function txn_billing_info_side_meta_box() |
|
| 1566 | - { |
|
| 1567 | - |
|
| 1568 | - $this->_template_args['billing_form'] = $this->_transaction->billing_info(); |
|
| 1569 | - $this->_template_args['billing_form_url'] = add_query_arg( |
|
| 1570 | - array('action' => 'edit_transaction', 'process' => 'billing'), |
|
| 1571 | - TXN_ADMIN_URL |
|
| 1572 | - ); |
|
| 1573 | - |
|
| 1574 | - $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_billing_info.template.php'; |
|
| 1575 | - echo EEH_Template::display_template($template_path, $this->_template_args, true);/**/ |
|
| 1576 | - } |
|
| 1577 | - |
|
| 1578 | - |
|
| 1579 | - /** |
|
| 1580 | - * apply_payments_or_refunds |
|
| 1581 | - * registers a payment or refund made towards a transaction |
|
| 1582 | - * |
|
| 1583 | - * @access public |
|
| 1584 | - * @return void |
|
| 1585 | - * @throws EE_Error |
|
| 1586 | - * @throws InvalidArgumentException |
|
| 1587 | - * @throws ReflectionException |
|
| 1588 | - * @throws RuntimeException |
|
| 1589 | - * @throws InvalidDataTypeException |
|
| 1590 | - * @throws InvalidInterfaceException |
|
| 1591 | - */ |
|
| 1592 | - public function apply_payments_or_refunds() |
|
| 1593 | - { |
|
| 1594 | - $json_response_data = array('return_data' => false); |
|
| 1595 | - $valid_data = $this->_validate_payment_request_data(); |
|
| 1596 | - $has_access = EE_Registry::instance()->CAP->current_user_can( |
|
| 1597 | - 'ee_edit_payments', |
|
| 1598 | - 'apply_payment_or_refund_from_registration_details' |
|
| 1599 | - ); |
|
| 1600 | - if (! empty($valid_data) && $has_access) { |
|
| 1601 | - $PAY_ID = $valid_data['PAY_ID']; |
|
| 1602 | - // save the new payment |
|
| 1603 | - $payment = $this->_create_payment_from_request_data($valid_data); |
|
| 1604 | - // get the TXN for this payment |
|
| 1605 | - $transaction = $payment->transaction(); |
|
| 1606 | - // verify transaction |
|
| 1607 | - if ($transaction instanceof EE_Transaction) { |
|
| 1608 | - // calculate_total_payments_and_update_status |
|
| 1609 | - $this->_process_transaction_payments($transaction); |
|
| 1610 | - $REG_IDs = $this->_get_REG_IDs_to_apply_payment_to($payment); |
|
| 1611 | - $this->_remove_existing_registration_payments($payment, $PAY_ID); |
|
| 1612 | - // apply payment to registrations (if applicable) |
|
| 1613 | - if (! empty($REG_IDs)) { |
|
| 1614 | - $this->_update_registration_payments($transaction, $payment, $REG_IDs); |
|
| 1615 | - $this->_maybe_send_notifications(); |
|
| 1616 | - // now process status changes for the same registrations |
|
| 1617 | - $this->_process_registration_status_change($transaction, $REG_IDs); |
|
| 1618 | - } |
|
| 1619 | - $this->_maybe_send_notifications($payment); |
|
| 1620 | - // prepare to render page |
|
| 1621 | - $json_response_data['return_data'] = $this->_build_payment_json_response($payment, $REG_IDs); |
|
| 1622 | - do_action( |
|
| 1623 | - 'AHEE__Transactions_Admin_Page__apply_payments_or_refund__after_recording', |
|
| 1624 | - $transaction, |
|
| 1625 | - $payment |
|
| 1626 | - ); |
|
| 1627 | - } else { |
|
| 1628 | - EE_Error::add_error( |
|
| 1629 | - esc_html__( |
|
| 1630 | - 'A valid Transaction for this payment could not be retrieved.', |
|
| 1631 | - 'event_espresso' |
|
| 1632 | - ), |
|
| 1633 | - __FILE__, |
|
| 1634 | - __FUNCTION__, |
|
| 1635 | - __LINE__ |
|
| 1636 | - ); |
|
| 1637 | - } |
|
| 1638 | - } else { |
|
| 1639 | - if ($has_access) { |
|
| 1640 | - EE_Error::add_error( |
|
| 1641 | - esc_html__( |
|
| 1642 | - 'The payment form data could not be processed. Please try again.', |
|
| 1643 | - 'event_espresso' |
|
| 1644 | - ), |
|
| 1645 | - __FILE__, |
|
| 1646 | - __FUNCTION__, |
|
| 1647 | - __LINE__ |
|
| 1648 | - ); |
|
| 1649 | - } else { |
|
| 1650 | - EE_Error::add_error( |
|
| 1651 | - esc_html__( |
|
| 1652 | - 'You do not have access to apply payments or refunds to a registration.', |
|
| 1653 | - 'event_espresso' |
|
| 1654 | - ), |
|
| 1655 | - __FILE__, |
|
| 1656 | - __FUNCTION__, |
|
| 1657 | - __LINE__ |
|
| 1658 | - ); |
|
| 1659 | - } |
|
| 1660 | - } |
|
| 1661 | - $notices = EE_Error::get_notices( |
|
| 1662 | - false, |
|
| 1663 | - false, |
|
| 1664 | - false |
|
| 1665 | - ); |
|
| 1666 | - $this->_template_args = array( |
|
| 1667 | - 'data' => $json_response_data, |
|
| 1668 | - 'error' => $notices['errors'], |
|
| 1669 | - 'success' => $notices['success'], |
|
| 1670 | - ); |
|
| 1671 | - $this->_return_json(); |
|
| 1672 | - } |
|
| 1673 | - |
|
| 1674 | - |
|
| 1675 | - /** |
|
| 1676 | - * _validate_payment_request_data |
|
| 1677 | - * |
|
| 1678 | - * @return array |
|
| 1679 | - * @throws EE_Error |
|
| 1680 | - */ |
|
| 1681 | - protected function _validate_payment_request_data() |
|
| 1682 | - { |
|
| 1683 | - if (! isset($this->_req_data['txn_admin_payment'])) { |
|
| 1684 | - return false; |
|
| 1685 | - } |
|
| 1686 | - $payment_form = $this->_generate_payment_form_section(); |
|
| 1687 | - try { |
|
| 1688 | - if ($payment_form->was_submitted()) { |
|
| 1689 | - $payment_form->receive_form_submission(); |
|
| 1690 | - if (! $payment_form->is_valid()) { |
|
| 1691 | - $submission_error_messages = array(); |
|
| 1692 | - foreach ($payment_form->get_validation_errors_accumulated() as $validation_error) { |
|
| 1693 | - if ($validation_error instanceof EE_Validation_Error) { |
|
| 1694 | - $submission_error_messages[] = sprintf( |
|
| 1695 | - _x('%s : %s', 'Form Section Name : Form Validation Error', 'event_espresso'), |
|
| 1696 | - $validation_error->get_form_section()->html_label_text(), |
|
| 1697 | - $validation_error->getMessage() |
|
| 1698 | - ); |
|
| 1699 | - } |
|
| 1700 | - } |
|
| 1701 | - EE_Error::add_error( |
|
| 1702 | - implode('<br />', $submission_error_messages), |
|
| 1703 | - __FILE__, |
|
| 1704 | - __FUNCTION__, |
|
| 1705 | - __LINE__ |
|
| 1706 | - ); |
|
| 1707 | - |
|
| 1708 | - return array(); |
|
| 1709 | - } |
|
| 1710 | - } |
|
| 1711 | - } catch (EE_Error $e) { |
|
| 1712 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 1713 | - |
|
| 1714 | - return array(); |
|
| 1715 | - } |
|
| 1716 | - |
|
| 1717 | - return $payment_form->valid_data(); |
|
| 1718 | - } |
|
| 1719 | - |
|
| 1720 | - |
|
| 1721 | - /** |
|
| 1722 | - * _generate_payment_form_section |
|
| 1723 | - * |
|
| 1724 | - * @return EE_Form_Section_Proper |
|
| 1725 | - * @throws EE_Error |
|
| 1726 | - */ |
|
| 1727 | - protected function _generate_payment_form_section() |
|
| 1728 | - { |
|
| 1729 | - return new EE_Form_Section_Proper( |
|
| 1730 | - array( |
|
| 1731 | - 'name' => 'txn_admin_payment', |
|
| 1732 | - 'subsections' => array( |
|
| 1733 | - 'PAY_ID' => new EE_Text_Input( |
|
| 1734 | - array( |
|
| 1735 | - 'default' => 0, |
|
| 1736 | - 'required' => false, |
|
| 1737 | - 'html_label_text' => esc_html__('Payment ID', 'event_espresso'), |
|
| 1738 | - 'validation_strategies' => array(new EE_Int_Normalization()), |
|
| 1739 | - ) |
|
| 1740 | - ), |
|
| 1741 | - 'TXN_ID' => new EE_Text_Input( |
|
| 1742 | - array( |
|
| 1743 | - 'default' => 0, |
|
| 1744 | - 'required' => true, |
|
| 1745 | - 'html_label_text' => esc_html__('Transaction ID', 'event_espresso'), |
|
| 1746 | - 'validation_strategies' => array(new EE_Int_Normalization()), |
|
| 1747 | - ) |
|
| 1748 | - ), |
|
| 1749 | - 'type' => new EE_Text_Input( |
|
| 1750 | - array( |
|
| 1751 | - 'default' => 1, |
|
| 1752 | - 'required' => true, |
|
| 1753 | - 'html_label_text' => esc_html__('Payment or Refund', 'event_espresso'), |
|
| 1754 | - 'validation_strategies' => array(new EE_Int_Normalization()), |
|
| 1755 | - ) |
|
| 1756 | - ), |
|
| 1757 | - 'amount' => new EE_Text_Input( |
|
| 1758 | - array( |
|
| 1759 | - 'default' => 0, |
|
| 1760 | - 'required' => true, |
|
| 1761 | - 'html_label_text' => esc_html__('Payment amount', 'event_espresso'), |
|
| 1762 | - 'validation_strategies' => array(new EE_Float_Normalization()), |
|
| 1763 | - ) |
|
| 1764 | - ), |
|
| 1765 | - 'status' => new EE_Text_Input( |
|
| 1766 | - array( |
|
| 1767 | - 'default' => EEM_Payment::status_id_approved, |
|
| 1768 | - 'required' => true, |
|
| 1769 | - 'html_label_text' => esc_html__('Payment status', 'event_espresso'), |
|
| 1770 | - ) |
|
| 1771 | - ), |
|
| 1772 | - 'PMD_ID' => new EE_Text_Input( |
|
| 1773 | - array( |
|
| 1774 | - 'default' => 2, |
|
| 1775 | - 'required' => true, |
|
| 1776 | - 'html_label_text' => esc_html__('Payment Method', 'event_espresso'), |
|
| 1777 | - 'validation_strategies' => array(new EE_Int_Normalization()), |
|
| 1778 | - ) |
|
| 1779 | - ), |
|
| 1780 | - 'date' => new EE_Text_Input( |
|
| 1781 | - array( |
|
| 1782 | - 'default' => time(), |
|
| 1783 | - 'required' => true, |
|
| 1784 | - 'html_label_text' => esc_html__('Payment date', 'event_espresso'), |
|
| 1785 | - ) |
|
| 1786 | - ), |
|
| 1787 | - 'txn_id_chq_nmbr' => new EE_Text_Input( |
|
| 1788 | - array( |
|
| 1789 | - 'default' => '', |
|
| 1790 | - 'required' => false, |
|
| 1791 | - 'html_label_text' => esc_html__('Transaction or Cheque Number', 'event_espresso'), |
|
| 1792 | - 'validation_strategies' => array( |
|
| 1793 | - new EE_Max_Length_Validation_Strategy( |
|
| 1794 | - esc_html__('Input too long', 'event_espresso'), |
|
| 1795 | - 100 |
|
| 1796 | - ), |
|
| 1797 | - ), |
|
| 1798 | - ) |
|
| 1799 | - ), |
|
| 1800 | - 'po_number' => new EE_Text_Input( |
|
| 1801 | - array( |
|
| 1802 | - 'default' => '', |
|
| 1803 | - 'required' => false, |
|
| 1804 | - 'html_label_text' => esc_html__('Purchase Order Number', 'event_espresso'), |
|
| 1805 | - 'validation_strategies' => array( |
|
| 1806 | - new EE_Max_Length_Validation_Strategy( |
|
| 1807 | - esc_html__('Input too long', 'event_espresso'), |
|
| 1808 | - 100 |
|
| 1809 | - ), |
|
| 1810 | - ), |
|
| 1811 | - ) |
|
| 1812 | - ), |
|
| 1813 | - 'accounting' => new EE_Text_Input( |
|
| 1814 | - array( |
|
| 1815 | - 'default' => '', |
|
| 1816 | - 'required' => false, |
|
| 1817 | - 'html_label_text' => esc_html__('Extra Field for Accounting', 'event_espresso'), |
|
| 1818 | - 'validation_strategies' => array( |
|
| 1819 | - new EE_Max_Length_Validation_Strategy( |
|
| 1820 | - esc_html__('Input too long', 'event_espresso'), |
|
| 1821 | - 100 |
|
| 1822 | - ), |
|
| 1823 | - ), |
|
| 1824 | - ) |
|
| 1825 | - ), |
|
| 1826 | - ), |
|
| 1827 | - ) |
|
| 1828 | - ); |
|
| 1829 | - } |
|
| 1830 | - |
|
| 1831 | - |
|
| 1832 | - /** |
|
| 1833 | - * _create_payment_from_request_data |
|
| 1834 | - * |
|
| 1835 | - * @param array $valid_data |
|
| 1836 | - * @return EE_Payment |
|
| 1837 | - * @throws EE_Error |
|
| 1838 | - */ |
|
| 1839 | - protected function _create_payment_from_request_data($valid_data) |
|
| 1840 | - { |
|
| 1841 | - $PAY_ID = $valid_data['PAY_ID']; |
|
| 1842 | - // get payment amount |
|
| 1843 | - $amount = $valid_data['amount'] ? abs($valid_data['amount']) : 0; |
|
| 1844 | - // payments have a type value of 1 and refunds have a type value of -1 |
|
| 1845 | - // so multiplying amount by type will give a positive value for payments, and negative values for refunds |
|
| 1846 | - $amount = $valid_data['type'] < 0 ? $amount * -1 : $amount; |
|
| 1847 | - // for some reason the date string coming in has extra spaces between the date and time. This fixes that. |
|
| 1848 | - $date = $valid_data['date'] |
|
| 1849 | - ? preg_replace('/\s+/', ' ', $valid_data['date']) |
|
| 1850 | - : date('Y-m-d g:i a', current_time('timestamp')); |
|
| 1851 | - $payment = EE_Payment::new_instance( |
|
| 1852 | - array( |
|
| 1853 | - 'TXN_ID' => $valid_data['TXN_ID'], |
|
| 1854 | - 'STS_ID' => $valid_data['status'], |
|
| 1855 | - 'PAY_timestamp' => $date, |
|
| 1856 | - 'PAY_source' => EEM_Payment_Method::scope_admin, |
|
| 1857 | - 'PMD_ID' => $valid_data['PMD_ID'], |
|
| 1858 | - 'PAY_amount' => $amount, |
|
| 1859 | - 'PAY_txn_id_chq_nmbr' => $valid_data['txn_id_chq_nmbr'], |
|
| 1860 | - 'PAY_po_number' => $valid_data['po_number'], |
|
| 1861 | - 'PAY_extra_accntng' => $valid_data['accounting'], |
|
| 1862 | - 'PAY_details' => $valid_data, |
|
| 1863 | - 'PAY_ID' => $PAY_ID, |
|
| 1864 | - ), |
|
| 1865 | - '', |
|
| 1866 | - array('Y-m-d', 'g:i a') |
|
| 1867 | - ); |
|
| 1868 | - |
|
| 1869 | - if (! $payment->save()) { |
|
| 1870 | - EE_Error::add_error( |
|
| 1871 | - sprintf( |
|
| 1872 | - esc_html__('Payment %1$d has not been successfully saved to the database.', 'event_espresso'), |
|
| 1873 | - $payment->ID() |
|
| 1874 | - ), |
|
| 1875 | - __FILE__, |
|
| 1876 | - __FUNCTION__, |
|
| 1877 | - __LINE__ |
|
| 1878 | - ); |
|
| 1879 | - } |
|
| 1880 | - |
|
| 1881 | - return $payment; |
|
| 1882 | - } |
|
| 1883 | - |
|
| 1884 | - |
|
| 1885 | - /** |
|
| 1886 | - * _process_transaction_payments |
|
| 1887 | - * |
|
| 1888 | - * @param \EE_Transaction $transaction |
|
| 1889 | - * @return void |
|
| 1890 | - * @throws EE_Error |
|
| 1891 | - * @throws InvalidArgumentException |
|
| 1892 | - * @throws ReflectionException |
|
| 1893 | - * @throws InvalidDataTypeException |
|
| 1894 | - * @throws InvalidInterfaceException |
|
| 1895 | - */ |
|
| 1896 | - protected function _process_transaction_payments(EE_Transaction $transaction) |
|
| 1897 | - { |
|
| 1898 | - /** @type EE_Transaction_Payments $transaction_payments */ |
|
| 1899 | - $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments'); |
|
| 1900 | - // update the transaction with this payment |
|
| 1901 | - if ($transaction_payments->calculate_total_payments_and_update_status($transaction)) { |
|
| 1902 | - EE_Error::add_success( |
|
| 1903 | - esc_html__( |
|
| 1904 | - 'The payment has been processed successfully.', |
|
| 1905 | - 'event_espresso' |
|
| 1906 | - ), |
|
| 1907 | - __FILE__, |
|
| 1908 | - __FUNCTION__, |
|
| 1909 | - __LINE__ |
|
| 1910 | - ); |
|
| 1911 | - } else { |
|
| 1912 | - EE_Error::add_error( |
|
| 1913 | - esc_html__( |
|
| 1914 | - 'The payment was processed successfully but the amount paid for the transaction was not updated.', |
|
| 1915 | - 'event_espresso' |
|
| 1916 | - ), |
|
| 1917 | - __FILE__, |
|
| 1918 | - __FUNCTION__, |
|
| 1919 | - __LINE__ |
|
| 1920 | - ); |
|
| 1921 | - } |
|
| 1922 | - } |
|
| 1923 | - |
|
| 1924 | - |
|
| 1925 | - /** |
|
| 1926 | - * _get_REG_IDs_to_apply_payment_to |
|
| 1927 | - * returns a list of registration IDs that the payment will apply to |
|
| 1928 | - * |
|
| 1929 | - * @param \EE_Payment $payment |
|
| 1930 | - * @return array |
|
| 1931 | - * @throws EE_Error |
|
| 1932 | - */ |
|
| 1933 | - protected function _get_REG_IDs_to_apply_payment_to(EE_Payment $payment) |
|
| 1934 | - { |
|
| 1935 | - $REG_IDs = array(); |
|
| 1936 | - // grab array of IDs for specific registrations to apply changes to |
|
| 1937 | - if (isset($this->_req_data['txn_admin_payment']['registrations'])) { |
|
| 1938 | - $REG_IDs = (array) $this->_req_data['txn_admin_payment']['registrations']; |
|
| 1939 | - } |
|
| 1940 | - // nothing specified ? then get all reg IDs |
|
| 1941 | - if (empty($REG_IDs)) { |
|
| 1942 | - $registrations = $payment->transaction()->registrations(); |
|
| 1943 | - $REG_IDs = ! empty($registrations) |
|
| 1944 | - ? array_keys($registrations) |
|
| 1945 | - : $this->_get_existing_reg_payment_REG_IDs($payment); |
|
| 1946 | - } |
|
| 1947 | - |
|
| 1948 | - // ensure that REG_IDs are integers and NOT strings |
|
| 1949 | - return array_map('intval', $REG_IDs); |
|
| 1950 | - } |
|
| 1951 | - |
|
| 1952 | - |
|
| 1953 | - /** |
|
| 1954 | - * @return array |
|
| 1955 | - */ |
|
| 1956 | - public function existing_reg_payment_REG_IDs() |
|
| 1957 | - { |
|
| 1958 | - return $this->_existing_reg_payment_REG_IDs; |
|
| 1959 | - } |
|
| 1960 | - |
|
| 1961 | - |
|
| 1962 | - /** |
|
| 1963 | - * @param array $existing_reg_payment_REG_IDs |
|
| 1964 | - */ |
|
| 1965 | - public function set_existing_reg_payment_REG_IDs($existing_reg_payment_REG_IDs = null) |
|
| 1966 | - { |
|
| 1967 | - $this->_existing_reg_payment_REG_IDs = $existing_reg_payment_REG_IDs; |
|
| 1968 | - } |
|
| 1969 | - |
|
| 1970 | - |
|
| 1971 | - /** |
|
| 1972 | - * _get_existing_reg_payment_REG_IDs |
|
| 1973 | - * returns a list of registration IDs that the payment is currently related to |
|
| 1974 | - * as recorded in the database |
|
| 1975 | - * |
|
| 1976 | - * @param \EE_Payment $payment |
|
| 1977 | - * @return array |
|
| 1978 | - * @throws EE_Error |
|
| 1979 | - */ |
|
| 1980 | - protected function _get_existing_reg_payment_REG_IDs(EE_Payment $payment) |
|
| 1981 | - { |
|
| 1982 | - if ($this->existing_reg_payment_REG_IDs() === null) { |
|
| 1983 | - // let's get any existing reg payment records for this payment |
|
| 1984 | - $existing_reg_payment_REG_IDs = $payment->get_many_related('Registration'); |
|
| 1985 | - // but we only want the REG IDs, so grab the array keys |
|
| 1986 | - $existing_reg_payment_REG_IDs = ! empty($existing_reg_payment_REG_IDs) |
|
| 1987 | - ? array_keys($existing_reg_payment_REG_IDs) |
|
| 1988 | - : array(); |
|
| 1989 | - $this->set_existing_reg_payment_REG_IDs($existing_reg_payment_REG_IDs); |
|
| 1990 | - } |
|
| 1991 | - |
|
| 1992 | - return $this->existing_reg_payment_REG_IDs(); |
|
| 1993 | - } |
|
| 1994 | - |
|
| 1995 | - |
|
| 1996 | - /** |
|
| 1997 | - * _remove_existing_registration_payments |
|
| 1998 | - * this calculates the difference between existing relations |
|
| 1999 | - * to the supplied payment and the new list registration IDs, |
|
| 2000 | - * removes any related registrations that no longer apply, |
|
| 2001 | - * and then updates the registration paid fields |
|
| 2002 | - * |
|
| 2003 | - * @param \EE_Payment $payment |
|
| 2004 | - * @param int $PAY_ID |
|
| 2005 | - * @return bool; |
|
| 2006 | - * @throws EE_Error |
|
| 2007 | - * @throws InvalidArgumentException |
|
| 2008 | - * @throws ReflectionException |
|
| 2009 | - * @throws InvalidDataTypeException |
|
| 2010 | - * @throws InvalidInterfaceException |
|
| 2011 | - */ |
|
| 2012 | - protected function _remove_existing_registration_payments(EE_Payment $payment, $PAY_ID = 0) |
|
| 2013 | - { |
|
| 2014 | - // newly created payments will have nothing recorded for $PAY_ID |
|
| 2015 | - if ($PAY_ID == 0) { |
|
| 2016 | - return false; |
|
| 2017 | - } |
|
| 2018 | - $existing_reg_payment_REG_IDs = $this->_get_existing_reg_payment_REG_IDs($payment); |
|
| 2019 | - if (empty($existing_reg_payment_REG_IDs)) { |
|
| 2020 | - return false; |
|
| 2021 | - } |
|
| 2022 | - /** @type EE_Transaction_Payments $transaction_payments */ |
|
| 2023 | - $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments'); |
|
| 2024 | - |
|
| 2025 | - return $transaction_payments->delete_registration_payments_and_update_registrations( |
|
| 2026 | - $payment, |
|
| 2027 | - array( |
|
| 2028 | - array( |
|
| 2029 | - 'PAY_ID' => $payment->ID(), |
|
| 2030 | - 'REG_ID' => array('IN', $existing_reg_payment_REG_IDs), |
|
| 2031 | - ), |
|
| 2032 | - ) |
|
| 2033 | - ); |
|
| 2034 | - } |
|
| 2035 | - |
|
| 2036 | - |
|
| 2037 | - /** |
|
| 2038 | - * _update_registration_payments |
|
| 2039 | - * this applies the payments to the selected registrations |
|
| 2040 | - * but only if they have not already been paid for |
|
| 2041 | - * |
|
| 2042 | - * @param EE_Transaction $transaction |
|
| 2043 | - * @param \EE_Payment $payment |
|
| 2044 | - * @param array $REG_IDs |
|
| 2045 | - * @return void |
|
| 2046 | - * @throws EE_Error |
|
| 2047 | - * @throws InvalidArgumentException |
|
| 2048 | - * @throws ReflectionException |
|
| 2049 | - * @throws RuntimeException |
|
| 2050 | - * @throws InvalidDataTypeException |
|
| 2051 | - * @throws InvalidInterfaceException |
|
| 2052 | - */ |
|
| 2053 | - protected function _update_registration_payments( |
|
| 2054 | - EE_Transaction $transaction, |
|
| 2055 | - EE_Payment $payment, |
|
| 2056 | - $REG_IDs = array() |
|
| 2057 | - ) { |
|
| 2058 | - // we can pass our own custom set of registrations to EE_Payment_Processor::process_registration_payments() |
|
| 2059 | - // so let's do that using our set of REG_IDs from the form |
|
| 2060 | - $registration_query_where_params = array( |
|
| 2061 | - 'REG_ID' => array('IN', $REG_IDs), |
|
| 2062 | - ); |
|
| 2063 | - // but add in some conditions regarding payment, |
|
| 2064 | - // so that we don't apply payments to registrations that are free or have already been paid for |
|
| 2065 | - // but ONLY if the payment is NOT a refund ( ie: the payment amount is not negative ) |
|
| 2066 | - if (! $payment->is_a_refund()) { |
|
| 2067 | - $registration_query_where_params['REG_final_price'] = array('!=', 0); |
|
| 2068 | - $registration_query_where_params['REG_final_price*'] = array('!=', 'REG_paid', true); |
|
| 2069 | - } |
|
| 2070 | - $registrations = $transaction->registrations(array($registration_query_where_params)); |
|
| 2071 | - if (! empty($registrations)) { |
|
| 2072 | - /** @type EE_Payment_Processor $payment_processor */ |
|
| 2073 | - $payment_processor = EE_Registry::instance()->load_core('Payment_Processor'); |
|
| 2074 | - $payment_processor->process_registration_payments($transaction, $payment, $registrations); |
|
| 2075 | - } |
|
| 2076 | - } |
|
| 2077 | - |
|
| 2078 | - |
|
| 2079 | - /** |
|
| 2080 | - * _process_registration_status_change |
|
| 2081 | - * This processes requested registration status changes for all the registrations |
|
| 2082 | - * on a given transaction and (optionally) sends out notifications for the changes. |
|
| 2083 | - * |
|
| 2084 | - * @param EE_Transaction $transaction |
|
| 2085 | - * @param array $REG_IDs |
|
| 2086 | - * @return bool |
|
| 2087 | - * @throws EE_Error |
|
| 2088 | - * @throws InvalidArgumentException |
|
| 2089 | - * @throws ReflectionException |
|
| 2090 | - * @throws InvalidDataTypeException |
|
| 2091 | - * @throws InvalidInterfaceException |
|
| 2092 | - */ |
|
| 2093 | - protected function _process_registration_status_change(EE_Transaction $transaction, $REG_IDs = array()) |
|
| 2094 | - { |
|
| 2095 | - // first if there is no change in status then we get out. |
|
| 2096 | - if (! isset($this->_req_data['txn_reg_status_change']['reg_status']) |
|
| 2097 | - || $this->_req_data['txn_reg_status_change']['reg_status'] === 'NAN' |
|
| 2098 | - ) { |
|
| 2099 | - // no error message, no change requested, just nothing to do man. |
|
| 2100 | - return false; |
|
| 2101 | - } |
|
| 2102 | - /** @type EE_Transaction_Processor $transaction_processor */ |
|
| 2103 | - $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor'); |
|
| 2104 | - |
|
| 2105 | - // made it here dude? Oh WOW. K, let's take care of changing the statuses |
|
| 2106 | - return $transaction_processor->manually_update_registration_statuses( |
|
| 2107 | - $transaction, |
|
| 2108 | - sanitize_text_field($this->_req_data['txn_reg_status_change']['reg_status']), |
|
| 2109 | - array(array('REG_ID' => array('IN', $REG_IDs))) |
|
| 2110 | - ); |
|
| 2111 | - } |
|
| 2112 | - |
|
| 2113 | - |
|
| 2114 | - /** |
|
| 2115 | - * _build_payment_json_response |
|
| 2116 | - * |
|
| 2117 | - * @access public |
|
| 2118 | - * @param \EE_Payment $payment |
|
| 2119 | - * @param array $REG_IDs |
|
| 2120 | - * @param bool | null $delete_txn_reg_status_change |
|
| 2121 | - * @return array |
|
| 2122 | - * @throws EE_Error |
|
| 2123 | - * @throws InvalidArgumentException |
|
| 2124 | - * @throws InvalidDataTypeException |
|
| 2125 | - * @throws InvalidInterfaceException |
|
| 2126 | - * @throws ReflectionException |
|
| 2127 | - */ |
|
| 2128 | - protected function _build_payment_json_response( |
|
| 2129 | - EE_Payment $payment, |
|
| 2130 | - $REG_IDs = array(), |
|
| 2131 | - $delete_txn_reg_status_change = null |
|
| 2132 | - ) { |
|
| 2133 | - // was the payment deleted ? |
|
| 2134 | - if (is_bool($delete_txn_reg_status_change)) { |
|
| 2135 | - return array( |
|
| 2136 | - 'PAY_ID' => $payment->ID(), |
|
| 2137 | - 'amount' => $payment->amount(), |
|
| 2138 | - 'total_paid' => $payment->transaction()->paid(), |
|
| 2139 | - 'txn_status' => $payment->transaction()->status_ID(), |
|
| 2140 | - 'pay_status' => $payment->STS_ID(), |
|
| 2141 | - 'registrations' => $this->_registration_payment_data_array($REG_IDs), |
|
| 2142 | - 'delete_txn_reg_status_change' => $delete_txn_reg_status_change, |
|
| 2143 | - ); |
|
| 2144 | - } else { |
|
| 2145 | - $this->_get_payment_status_array(); |
|
| 2146 | - |
|
| 2147 | - return array( |
|
| 2148 | - 'amount' => $payment->amount(), |
|
| 2149 | - 'total_paid' => $payment->transaction()->paid(), |
|
| 2150 | - 'txn_status' => $payment->transaction()->status_ID(), |
|
| 2151 | - 'pay_status' => $payment->STS_ID(), |
|
| 2152 | - 'PAY_ID' => $payment->ID(), |
|
| 2153 | - 'STS_ID' => $payment->STS_ID(), |
|
| 2154 | - 'status' => self::$_pay_status[ $payment->STS_ID() ], |
|
| 2155 | - 'date' => $payment->timestamp('Y-m-d', 'h:i a'), |
|
| 2156 | - 'method' => strtoupper($payment->source()), |
|
| 2157 | - 'PM_ID' => $payment->payment_method() ? $payment->payment_method()->ID() : 1, |
|
| 2158 | - 'gateway' => $payment->payment_method() |
|
| 2159 | - ? $payment->payment_method()->admin_name() |
|
| 2160 | - : esc_html__("Unknown", 'event_espresso'), |
|
| 2161 | - 'gateway_response' => $payment->gateway_response(), |
|
| 2162 | - 'txn_id_chq_nmbr' => $payment->txn_id_chq_nmbr(), |
|
| 2163 | - 'po_number' => $payment->po_number(), |
|
| 2164 | - 'extra_accntng' => $payment->extra_accntng(), |
|
| 2165 | - 'registrations' => $this->_registration_payment_data_array($REG_IDs), |
|
| 2166 | - ); |
|
| 2167 | - } |
|
| 2168 | - } |
|
| 2169 | - |
|
| 2170 | - |
|
| 2171 | - /** |
|
| 2172 | - * delete_payment |
|
| 2173 | - * delete a payment or refund made towards a transaction |
|
| 2174 | - * |
|
| 2175 | - * @access public |
|
| 2176 | - * @return void |
|
| 2177 | - * @throws EE_Error |
|
| 2178 | - * @throws InvalidArgumentException |
|
| 2179 | - * @throws ReflectionException |
|
| 2180 | - * @throws InvalidDataTypeException |
|
| 2181 | - * @throws InvalidInterfaceException |
|
| 2182 | - */ |
|
| 2183 | - public function delete_payment() |
|
| 2184 | - { |
|
| 2185 | - $json_response_data = array('return_data' => false); |
|
| 2186 | - $PAY_ID = isset($this->_req_data['delete_txn_admin_payment']['PAY_ID']) |
|
| 2187 | - ? absint($this->_req_data['delete_txn_admin_payment']['PAY_ID']) |
|
| 2188 | - : 0; |
|
| 2189 | - $can_delete = EE_Registry::instance()->CAP->current_user_can( |
|
| 2190 | - 'ee_delete_payments', |
|
| 2191 | - 'delete_payment_from_registration_details' |
|
| 2192 | - ); |
|
| 2193 | - if ($PAY_ID && $can_delete) { |
|
| 2194 | - $delete_txn_reg_status_change = isset($this->_req_data['delete_txn_reg_status_change']) |
|
| 2195 | - ? $this->_req_data['delete_txn_reg_status_change'] |
|
| 2196 | - : false; |
|
| 2197 | - $payment = EEM_Payment::instance()->get_one_by_ID($PAY_ID); |
|
| 2198 | - if ($payment instanceof EE_Payment) { |
|
| 2199 | - $REG_IDs = $this->_get_existing_reg_payment_REG_IDs($payment); |
|
| 2200 | - /** @type EE_Transaction_Payments $transaction_payments */ |
|
| 2201 | - $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments'); |
|
| 2202 | - if ($transaction_payments->delete_payment_and_update_transaction($payment)) { |
|
| 2203 | - $json_response_data['return_data'] = $this->_build_payment_json_response( |
|
| 2204 | - $payment, |
|
| 2205 | - $REG_IDs, |
|
| 2206 | - $delete_txn_reg_status_change |
|
| 2207 | - ); |
|
| 2208 | - if ($delete_txn_reg_status_change) { |
|
| 2209 | - $this->_req_data['txn_reg_status_change'] = $delete_txn_reg_status_change; |
|
| 2210 | - // MAKE sure we also add the delete_txn_req_status_change to the |
|
| 2211 | - // $_REQUEST global because that's how messages will be looking for it. |
|
| 2212 | - $_REQUEST['txn_reg_status_change'] = $delete_txn_reg_status_change; |
|
| 2213 | - $this->_maybe_send_notifications(); |
|
| 2214 | - $this->_process_registration_status_change($payment->transaction(), $REG_IDs); |
|
| 2215 | - } |
|
| 2216 | - } |
|
| 2217 | - } else { |
|
| 2218 | - EE_Error::add_error( |
|
| 2219 | - esc_html__('Valid Payment data could not be retrieved from the database.', 'event_espresso'), |
|
| 2220 | - __FILE__, |
|
| 2221 | - __FUNCTION__, |
|
| 2222 | - __LINE__ |
|
| 2223 | - ); |
|
| 2224 | - } |
|
| 2225 | - } else { |
|
| 2226 | - if ($can_delete) { |
|
| 2227 | - EE_Error::add_error( |
|
| 2228 | - esc_html__( |
|
| 2229 | - 'A valid Payment ID was not received, therefore payment form data could not be loaded.', |
|
| 2230 | - 'event_espresso' |
|
| 2231 | - ), |
|
| 2232 | - __FILE__, |
|
| 2233 | - __FUNCTION__, |
|
| 2234 | - __LINE__ |
|
| 2235 | - ); |
|
| 2236 | - } else { |
|
| 2237 | - EE_Error::add_error( |
|
| 2238 | - esc_html__( |
|
| 2239 | - 'You do not have access to delete a payment.', |
|
| 2240 | - 'event_espresso' |
|
| 2241 | - ), |
|
| 2242 | - __FILE__, |
|
| 2243 | - __FUNCTION__, |
|
| 2244 | - __LINE__ |
|
| 2245 | - ); |
|
| 2246 | - } |
|
| 2247 | - } |
|
| 2248 | - $notices = EE_Error::get_notices(false, false, false); |
|
| 2249 | - $this->_template_args = array( |
|
| 2250 | - 'data' => $json_response_data, |
|
| 2251 | - 'success' => $notices['success'], |
|
| 2252 | - 'error' => $notices['errors'], |
|
| 2253 | - 'attention' => $notices['attention'], |
|
| 2254 | - ); |
|
| 2255 | - $this->_return_json(); |
|
| 2256 | - } |
|
| 2257 | - |
|
| 2258 | - |
|
| 2259 | - /** |
|
| 2260 | - * _registration_payment_data_array |
|
| 2261 | - * adds info for 'owing' and 'paid' for each registration to the json response |
|
| 2262 | - * |
|
| 2263 | - * @access protected |
|
| 2264 | - * @param array $REG_IDs |
|
| 2265 | - * @return array |
|
| 2266 | - * @throws EE_Error |
|
| 2267 | - * @throws InvalidArgumentException |
|
| 2268 | - * @throws InvalidDataTypeException |
|
| 2269 | - * @throws InvalidInterfaceException |
|
| 2270 | - * @throws ReflectionException |
|
| 2271 | - */ |
|
| 2272 | - protected function _registration_payment_data_array($REG_IDs) |
|
| 2273 | - { |
|
| 2274 | - $registration_payment_data = array(); |
|
| 2275 | - // if non empty reg_ids lets get an array of registrations and update the values for the apply_payment/refund rows. |
|
| 2276 | - if (! empty($REG_IDs)) { |
|
| 2277 | - $registrations = EEM_Registration::instance()->get_all(array(array('REG_ID' => array('IN', $REG_IDs)))); |
|
| 2278 | - foreach ($registrations as $registration) { |
|
| 2279 | - if ($registration instanceof EE_Registration) { |
|
| 2280 | - $registration_payment_data[ $registration->ID() ] = array( |
|
| 2281 | - 'paid' => $registration->pretty_paid(), |
|
| 2282 | - 'owing' => EEH_Template::format_currency($registration->final_price() - $registration->paid()), |
|
| 2283 | - ); |
|
| 2284 | - } |
|
| 2285 | - } |
|
| 2286 | - } |
|
| 2287 | - |
|
| 2288 | - return $registration_payment_data; |
|
| 2289 | - } |
|
| 2290 | - |
|
| 2291 | - |
|
| 2292 | - /** |
|
| 2293 | - * _maybe_send_notifications |
|
| 2294 | - * determines whether or not the admin has indicated that notifications should be sent. |
|
| 2295 | - * If so, will toggle a filter switch for delivering registration notices. |
|
| 2296 | - * If passed an EE_Payment object, then it will trigger payment notifications instead. |
|
| 2297 | - * |
|
| 2298 | - * @access protected |
|
| 2299 | - * @param \EE_Payment | null $payment |
|
| 2300 | - */ |
|
| 2301 | - protected function _maybe_send_notifications($payment = null) |
|
| 2302 | - { |
|
| 2303 | - switch ($payment instanceof EE_Payment) { |
|
| 2304 | - // payment notifications |
|
| 2305 | - case true: |
|
| 2306 | - if (isset( |
|
| 2307 | - $this->_req_data['txn_payments'], |
|
| 2308 | - $this->_req_data['txn_payments']['send_notifications'] |
|
| 2309 | - ) |
|
| 2310 | - && filter_var($this->_req_data['txn_payments']['send_notifications'], FILTER_VALIDATE_BOOLEAN) |
|
| 2311 | - ) { |
|
| 2312 | - $this->_process_payment_notification($payment); |
|
| 2313 | - } |
|
| 2314 | - break; |
|
| 2315 | - // registration notifications |
|
| 2316 | - case false: |
|
| 2317 | - if (isset( |
|
| 2318 | - $this->_req_data['txn_reg_status_change'], |
|
| 2319 | - $this->_req_data['txn_reg_status_change']['send_notifications'] |
|
| 2320 | - ) |
|
| 2321 | - && filter_var($this->_req_data['txn_reg_status_change']['send_notifications'], FILTER_VALIDATE_BOOLEAN) |
|
| 2322 | - ) { |
|
| 2323 | - add_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications', '__return_true'); |
|
| 2324 | - } |
|
| 2325 | - break; |
|
| 2326 | - } |
|
| 2327 | - } |
|
| 2328 | - |
|
| 2329 | - |
|
| 2330 | - /** |
|
| 2331 | - * _send_payment_reminder |
|
| 2332 | - * generates HTML for the View Transaction Details Admin page |
|
| 2333 | - * |
|
| 2334 | - * @access protected |
|
| 2335 | - * @return void |
|
| 2336 | - * @throws EE_Error |
|
| 2337 | - * @throws InvalidArgumentException |
|
| 2338 | - * @throws InvalidDataTypeException |
|
| 2339 | - * @throws InvalidInterfaceException |
|
| 2340 | - */ |
|
| 2341 | - protected function _send_payment_reminder() |
|
| 2342 | - { |
|
| 2343 | - $TXN_ID = ! empty($this->_req_data['TXN_ID']) ? absint($this->_req_data['TXN_ID']) : false; |
|
| 2344 | - $transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID); |
|
| 2345 | - $query_args = isset($this->_req_data['redirect_to']) ? array( |
|
| 2346 | - 'action' => $this->_req_data['redirect_to'], |
|
| 2347 | - 'TXN_ID' => $this->_req_data['TXN_ID'], |
|
| 2348 | - ) : array(); |
|
| 2349 | - do_action( |
|
| 2350 | - 'AHEE__Transactions_Admin_Page___send_payment_reminder__process_admin_payment_reminder', |
|
| 2351 | - $transaction |
|
| 2352 | - ); |
|
| 2353 | - $this->_redirect_after_action( |
|
| 2354 | - false, |
|
| 2355 | - esc_html__('payment reminder', 'event_espresso'), |
|
| 2356 | - esc_html__('sent', 'event_espresso'), |
|
| 2357 | - $query_args, |
|
| 2358 | - true |
|
| 2359 | - ); |
|
| 2360 | - } |
|
| 2361 | - |
|
| 2362 | - |
|
| 2363 | - /** |
|
| 2364 | - * get_transactions |
|
| 2365 | - * get transactions for given parameters (used by list table) |
|
| 2366 | - * |
|
| 2367 | - * @param int $perpage how many transactions displayed per page |
|
| 2368 | - * @param boolean $count return the count or objects |
|
| 2369 | - * @param string $view |
|
| 2370 | - * @return mixed int = count || array of transaction objects |
|
| 2371 | - * @throws EE_Error |
|
| 2372 | - * @throws InvalidArgumentException |
|
| 2373 | - * @throws InvalidDataTypeException |
|
| 2374 | - * @throws InvalidInterfaceException |
|
| 2375 | - */ |
|
| 2376 | - public function get_transactions($perpage, $count = false, $view = '') |
|
| 2377 | - { |
|
| 2378 | - |
|
| 2379 | - $TXN = EEM_Transaction::instance(); |
|
| 2380 | - |
|
| 2381 | - $start_date = isset($this->_req_data['txn-filter-start-date']) |
|
| 2382 | - ? wp_strip_all_tags($this->_req_data['txn-filter-start-date']) |
|
| 2383 | - : date( |
|
| 2384 | - 'm/d/Y', |
|
| 2385 | - strtotime('-10 year') |
|
| 2386 | - ); |
|
| 2387 | - $end_date = isset($this->_req_data['txn-filter-end-date']) |
|
| 2388 | - ? wp_strip_all_tags($this->_req_data['txn-filter-end-date']) |
|
| 2389 | - : date('m/d/Y'); |
|
| 2390 | - |
|
| 2391 | - // make sure our timestamps start and end right at the boundaries for each day |
|
| 2392 | - $start_date = date('Y-m-d', strtotime($start_date)) . ' 00:00:00'; |
|
| 2393 | - $end_date = date('Y-m-d', strtotime($end_date)) . ' 23:59:59'; |
|
| 2394 | - |
|
| 2395 | - |
|
| 2396 | - // convert to timestamps |
|
| 2397 | - $start_date = strtotime($start_date); |
|
| 2398 | - $end_date = strtotime($end_date); |
|
| 2399 | - |
|
| 2400 | - // makes sure start date is the lowest value and vice versa |
|
| 2401 | - $start_date = min($start_date, $end_date); |
|
| 2402 | - $end_date = max($start_date, $end_date); |
|
| 2403 | - |
|
| 2404 | - // convert to correct format for query |
|
| 2405 | - $start_date = EEM_Transaction::instance()->convert_datetime_for_query( |
|
| 2406 | - 'TXN_timestamp', |
|
| 2407 | - date('Y-m-d H:i:s', $start_date), |
|
| 2408 | - 'Y-m-d H:i:s' |
|
| 2409 | - ); |
|
| 2410 | - $end_date = EEM_Transaction::instance()->convert_datetime_for_query( |
|
| 2411 | - 'TXN_timestamp', |
|
| 2412 | - date('Y-m-d H:i:s', $end_date), |
|
| 2413 | - 'Y-m-d H:i:s' |
|
| 2414 | - ); |
|
| 2415 | - |
|
| 2416 | - |
|
| 2417 | - // set orderby |
|
| 2418 | - $this->_req_data['orderby'] = ! empty($this->_req_data['orderby']) ? $this->_req_data['orderby'] : ''; |
|
| 2419 | - |
|
| 2420 | - switch ($this->_req_data['orderby']) { |
|
| 2421 | - case 'TXN_ID': |
|
| 2422 | - $orderby = 'TXN_ID'; |
|
| 2423 | - break; |
|
| 2424 | - case 'ATT_fname': |
|
| 2425 | - $orderby = 'Registration.Attendee.ATT_fname'; |
|
| 2426 | - break; |
|
| 2427 | - case 'event_name': |
|
| 2428 | - $orderby = 'Registration.Event.EVT_name'; |
|
| 2429 | - break; |
|
| 2430 | - default: // 'TXN_timestamp' |
|
| 2431 | - $orderby = 'TXN_timestamp'; |
|
| 2432 | - } |
|
| 2433 | - |
|
| 2434 | - $sort = ! empty($this->_req_data['order']) ? $this->_req_data['order'] : 'DESC'; |
|
| 2435 | - $current_page = ! empty($this->_req_data['paged']) ? $this->_req_data['paged'] : 1; |
|
| 2436 | - $per_page = ! empty($perpage) ? $perpage : 10; |
|
| 2437 | - $per_page = ! empty($this->_req_data['perpage']) ? $this->_req_data['perpage'] : $per_page; |
|
| 2438 | - |
|
| 2439 | - $offset = ($current_page - 1) * $per_page; |
|
| 2440 | - $limit = array($offset, $per_page); |
|
| 2441 | - |
|
| 2442 | - $_where = array( |
|
| 2443 | - 'TXN_timestamp' => array('BETWEEN', array($start_date, $end_date)), |
|
| 2444 | - 'Registration.REG_count' => 1, |
|
| 2445 | - ); |
|
| 2446 | - |
|
| 2447 | - if (isset($this->_req_data['EVT_ID'])) { |
|
| 2448 | - $_where['Registration.EVT_ID'] = $this->_req_data['EVT_ID']; |
|
| 2449 | - } |
|
| 2450 | - |
|
| 2451 | - if (isset($this->_req_data['s'])) { |
|
| 2452 | - $search_string = '%' . $this->_req_data['s'] . '%'; |
|
| 2453 | - $_where['OR'] = array( |
|
| 2454 | - 'Registration.Event.EVT_name' => array('LIKE', $search_string), |
|
| 2455 | - 'Registration.Event.EVT_desc' => array('LIKE', $search_string), |
|
| 2456 | - 'Registration.Event.EVT_short_desc' => array('LIKE', $search_string), |
|
| 2457 | - 'Registration.Attendee.ATT_full_name' => array('LIKE', $search_string), |
|
| 2458 | - 'Registration.Attendee.ATT_fname' => array('LIKE', $search_string), |
|
| 2459 | - 'Registration.Attendee.ATT_lname' => array('LIKE', $search_string), |
|
| 2460 | - 'Registration.Attendee.ATT_short_bio' => array('LIKE', $search_string), |
|
| 2461 | - 'Registration.Attendee.ATT_email' => array('LIKE', $search_string), |
|
| 2462 | - 'Registration.Attendee.ATT_address' => array('LIKE', $search_string), |
|
| 2463 | - 'Registration.Attendee.ATT_address2' => array('LIKE', $search_string), |
|
| 2464 | - 'Registration.Attendee.ATT_city' => array('LIKE', $search_string), |
|
| 2465 | - 'Registration.REG_final_price' => array('LIKE', $search_string), |
|
| 2466 | - 'Registration.REG_code' => array('LIKE', $search_string), |
|
| 2467 | - 'Registration.REG_count' => array('LIKE', $search_string), |
|
| 2468 | - 'Registration.REG_group_size' => array('LIKE', $search_string), |
|
| 2469 | - 'Registration.Ticket.TKT_name' => array('LIKE', $search_string), |
|
| 2470 | - 'Registration.Ticket.TKT_description' => array('LIKE', $search_string), |
|
| 2471 | - 'Payment.PAY_source' => array('LIKE', $search_string), |
|
| 2472 | - 'Payment.Payment_Method.PMD_name' => array('LIKE', $search_string), |
|
| 2473 | - 'TXN_session_data' => array('LIKE', $search_string), |
|
| 2474 | - 'Payment.PAY_txn_id_chq_nmbr' => array('LIKE', $search_string), |
|
| 2475 | - ); |
|
| 2476 | - } |
|
| 2477 | - |
|
| 2478 | - // failed transactions |
|
| 2479 | - $failed = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'failed' && ! $count) |
|
| 2480 | - || ($count && $view === 'failed'); |
|
| 2481 | - $abandoned = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'abandoned' && ! $count) |
|
| 2482 | - || ($count && $view === 'abandoned'); |
|
| 2483 | - $incomplete = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'incomplete' && ! $count) |
|
| 2484 | - || ($count && $view === 'incomplete'); |
|
| 2485 | - |
|
| 2486 | - if ($failed) { |
|
| 2487 | - $_where['STS_ID'] = EEM_Transaction::failed_status_code; |
|
| 2488 | - } elseif ($abandoned) { |
|
| 2489 | - $_where['STS_ID'] = EEM_Transaction::abandoned_status_code; |
|
| 2490 | - } elseif ($incomplete) { |
|
| 2491 | - $_where['STS_ID'] = EEM_Transaction::incomplete_status_code; |
|
| 2492 | - } else { |
|
| 2493 | - $_where['STS_ID'] = array('!=', EEM_Transaction::failed_status_code); |
|
| 2494 | - $_where['STS_ID*'] = array('!=', EEM_Transaction::abandoned_status_code); |
|
| 2495 | - } |
|
| 2496 | - |
|
| 2497 | - $query_params = apply_filters( |
|
| 2498 | - 'FHEE__Transactions_Admin_Page___get_transactions_query_params', |
|
| 2499 | - array( |
|
| 2500 | - $_where, |
|
| 2501 | - 'order_by' => array($orderby => $sort), |
|
| 2502 | - 'limit' => $limit, |
|
| 2503 | - 'default_where_conditions' => EEM_Base::default_where_conditions_this_only, |
|
| 2504 | - ), |
|
| 2505 | - $this->_req_data, |
|
| 2506 | - $view, |
|
| 2507 | - $count |
|
| 2508 | - ); |
|
| 2509 | - |
|
| 2510 | - $transactions = $count |
|
| 2511 | - ? $TXN->count(array($query_params[0]), 'TXN_ID', true) |
|
| 2512 | - : $TXN->get_all($query_params); |
|
| 2513 | - |
|
| 2514 | - return $transactions; |
|
| 2515 | - } |
|
| 17 | + /** |
|
| 18 | + * @var EE_Transaction |
|
| 19 | + */ |
|
| 20 | + private $_transaction; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var EE_Session |
|
| 24 | + */ |
|
| 25 | + private $_session; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var array $_txn_status |
|
| 29 | + */ |
|
| 30 | + private static $_txn_status; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var array $_pay_status |
|
| 34 | + */ |
|
| 35 | + private static $_pay_status; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var array $_existing_reg_payment_REG_IDs |
|
| 39 | + */ |
|
| 40 | + protected $_existing_reg_payment_REG_IDs = null; |
|
| 41 | + |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @Constructor |
|
| 45 | + * @access public |
|
| 46 | + * @param bool $routing |
|
| 47 | + * @throws EE_Error |
|
| 48 | + * @throws InvalidArgumentException |
|
| 49 | + * @throws ReflectionException |
|
| 50 | + * @throws InvalidDataTypeException |
|
| 51 | + * @throws InvalidInterfaceException |
|
| 52 | + */ |
|
| 53 | + public function __construct($routing = true) |
|
| 54 | + { |
|
| 55 | + parent::__construct($routing); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * _init_page_props |
|
| 61 | + * |
|
| 62 | + * @return void |
|
| 63 | + */ |
|
| 64 | + protected function _init_page_props() |
|
| 65 | + { |
|
| 66 | + $this->page_slug = TXN_PG_SLUG; |
|
| 67 | + $this->page_label = esc_html__('Transactions', 'event_espresso'); |
|
| 68 | + $this->_admin_base_url = TXN_ADMIN_URL; |
|
| 69 | + $this->_admin_base_path = TXN_ADMIN; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * _ajax_hooks |
|
| 75 | + * |
|
| 76 | + * @return void |
|
| 77 | + */ |
|
| 78 | + protected function _ajax_hooks() |
|
| 79 | + { |
|
| 80 | + add_action('wp_ajax_espresso_apply_payment', array($this, 'apply_payments_or_refunds')); |
|
| 81 | + add_action('wp_ajax_espresso_apply_refund', array($this, 'apply_payments_or_refunds')); |
|
| 82 | + add_action('wp_ajax_espresso_delete_payment', array($this, 'delete_payment')); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * _define_page_props |
|
| 88 | + * |
|
| 89 | + * @return void |
|
| 90 | + */ |
|
| 91 | + protected function _define_page_props() |
|
| 92 | + { |
|
| 93 | + $this->_admin_page_title = $this->page_label; |
|
| 94 | + $this->_labels = array( |
|
| 95 | + 'buttons' => array( |
|
| 96 | + 'add' => esc_html__('Add New Transaction', 'event_espresso'), |
|
| 97 | + 'edit' => esc_html__('Edit Transaction', 'event_espresso'), |
|
| 98 | + 'delete' => esc_html__('Delete Transaction', 'event_espresso'), |
|
| 99 | + ), |
|
| 100 | + ); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * grab url requests and route them |
|
| 106 | + * |
|
| 107 | + * @access private |
|
| 108 | + * @return void |
|
| 109 | + * @throws EE_Error |
|
| 110 | + * @throws InvalidArgumentException |
|
| 111 | + * @throws InvalidDataTypeException |
|
| 112 | + * @throws InvalidInterfaceException |
|
| 113 | + */ |
|
| 114 | + public function _set_page_routes() |
|
| 115 | + { |
|
| 116 | + |
|
| 117 | + $this->_set_transaction_status_array(); |
|
| 118 | + |
|
| 119 | + $txn_id = ! empty($this->_req_data['TXN_ID']) |
|
| 120 | + && ! is_array($this->_req_data['TXN_ID']) |
|
| 121 | + ? $this->_req_data['TXN_ID'] |
|
| 122 | + : 0; |
|
| 123 | + |
|
| 124 | + $this->_page_routes = array( |
|
| 125 | + |
|
| 126 | + 'default' => array( |
|
| 127 | + 'func' => '_transactions_overview_list_table', |
|
| 128 | + 'capability' => 'ee_read_transactions', |
|
| 129 | + ), |
|
| 130 | + |
|
| 131 | + 'view_transaction' => array( |
|
| 132 | + 'func' => '_transaction_details', |
|
| 133 | + 'capability' => 'ee_read_transaction', |
|
| 134 | + 'obj_id' => $txn_id, |
|
| 135 | + ), |
|
| 136 | + |
|
| 137 | + 'send_payment_reminder' => array( |
|
| 138 | + 'func' => '_send_payment_reminder', |
|
| 139 | + 'noheader' => true, |
|
| 140 | + 'capability' => 'ee_send_message', |
|
| 141 | + ), |
|
| 142 | + |
|
| 143 | + 'espresso_apply_payment' => array( |
|
| 144 | + 'func' => 'apply_payments_or_refunds', |
|
| 145 | + 'noheader' => true, |
|
| 146 | + 'capability' => 'ee_edit_payments', |
|
| 147 | + ), |
|
| 148 | + |
|
| 149 | + 'espresso_apply_refund' => array( |
|
| 150 | + 'func' => 'apply_payments_or_refunds', |
|
| 151 | + 'noheader' => true, |
|
| 152 | + 'capability' => 'ee_edit_payments', |
|
| 153 | + ), |
|
| 154 | + |
|
| 155 | + 'espresso_delete_payment' => array( |
|
| 156 | + 'func' => 'delete_payment', |
|
| 157 | + 'noheader' => true, |
|
| 158 | + 'capability' => 'ee_delete_payments', |
|
| 159 | + ), |
|
| 160 | + |
|
| 161 | + ); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + |
|
| 165 | + protected function _set_page_config() |
|
| 166 | + { |
|
| 167 | + $this->_page_config = array( |
|
| 168 | + 'default' => array( |
|
| 169 | + 'nav' => array( |
|
| 170 | + 'label' => esc_html__('Overview', 'event_espresso'), |
|
| 171 | + 'order' => 10, |
|
| 172 | + ), |
|
| 173 | + 'list_table' => 'EE_Admin_Transactions_List_Table', |
|
| 174 | + 'help_tabs' => array( |
|
| 175 | + 'transactions_overview_help_tab' => array( |
|
| 176 | + 'title' => esc_html__('Transactions Overview', 'event_espresso'), |
|
| 177 | + 'filename' => 'transactions_overview', |
|
| 178 | + ), |
|
| 179 | + 'transactions_overview_table_column_headings_help_tab' => array( |
|
| 180 | + 'title' => esc_html__('Transactions Table Column Headings', 'event_espresso'), |
|
| 181 | + 'filename' => 'transactions_overview_table_column_headings', |
|
| 182 | + ), |
|
| 183 | + 'transactions_overview_views_filters_help_tab' => array( |
|
| 184 | + 'title' => esc_html__('Transaction Views & Filters & Search', 'event_espresso'), |
|
| 185 | + 'filename' => 'transactions_overview_views_filters_search', |
|
| 186 | + ), |
|
| 187 | + ), |
|
| 188 | + 'help_tour' => array('Transactions_Overview_Help_Tour'), |
|
| 189 | + /** |
|
| 190 | + * commented out because currently we are not displaying tips for transaction list table status but this |
|
| 191 | + * may change in a later iteration so want to keep the code for then. |
|
| 192 | + */ |
|
| 193 | + // 'qtips' => array( 'Transactions_List_Table_Tips' ), |
|
| 194 | + 'require_nonce' => false, |
|
| 195 | + ), |
|
| 196 | + 'view_transaction' => array( |
|
| 197 | + 'nav' => array( |
|
| 198 | + 'label' => esc_html__('View Transaction', 'event_espresso'), |
|
| 199 | + 'order' => 5, |
|
| 200 | + 'url' => isset($this->_req_data['TXN_ID']) |
|
| 201 | + ? add_query_arg(array('TXN_ID' => $this->_req_data['TXN_ID']), $this->_current_page_view_url) |
|
| 202 | + : $this->_admin_base_url, |
|
| 203 | + 'persistent' => false, |
|
| 204 | + ), |
|
| 205 | + 'help_tabs' => array( |
|
| 206 | + 'transactions_view_transaction_help_tab' => array( |
|
| 207 | + 'title' => esc_html__('View Transaction', 'event_espresso'), |
|
| 208 | + 'filename' => 'transactions_view_transaction', |
|
| 209 | + ), |
|
| 210 | + 'transactions_view_transaction_transaction_details_table_help_tab' => array( |
|
| 211 | + 'title' => esc_html__('Transaction Details Table', 'event_espresso'), |
|
| 212 | + 'filename' => 'transactions_view_transaction_transaction_details_table', |
|
| 213 | + ), |
|
| 214 | + 'transactions_view_transaction_attendees_registered_help_tab' => array( |
|
| 215 | + 'title' => esc_html__('Attendees Registered', 'event_espresso'), |
|
| 216 | + 'filename' => 'transactions_view_transaction_attendees_registered', |
|
| 217 | + ), |
|
| 218 | + 'transactions_view_transaction_views_primary_registrant_billing_information_help_tab' => array( |
|
| 219 | + 'title' => esc_html__('Primary Registrant & Billing Information', 'event_espresso'), |
|
| 220 | + 'filename' => 'transactions_view_transaction_primary_registrant_billing_information', |
|
| 221 | + ), |
|
| 222 | + ), |
|
| 223 | + 'qtips' => array('Transaction_Details_Tips'), |
|
| 224 | + 'help_tour' => array('Transaction_Details_Help_Tour'), |
|
| 225 | + 'metaboxes' => array('_transaction_details_metaboxes'), |
|
| 226 | + |
|
| 227 | + 'require_nonce' => false, |
|
| 228 | + ), |
|
| 229 | + ); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * The below methods aren't used by this class currently |
|
| 235 | + */ |
|
| 236 | + protected function _add_screen_options() |
|
| 237 | + { |
|
| 238 | + // noop |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + protected function _add_feature_pointers() |
|
| 242 | + { |
|
| 243 | + // noop |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + public function admin_init() |
|
| 247 | + { |
|
| 248 | + // IF a registration was JUST added via the admin... |
|
| 249 | + if (isset( |
|
| 250 | + $this->_req_data['redirect_from'], |
|
| 251 | + $this->_req_data['EVT_ID'], |
|
| 252 | + $this->_req_data['event_name'] |
|
| 253 | + )) { |
|
| 254 | + // then set a cookie so that we can block any attempts to use |
|
| 255 | + // the back button as a way to enter another registration. |
|
| 256 | + setcookie( |
|
| 257 | + 'ee_registration_added', |
|
| 258 | + $this->_req_data['EVT_ID'], |
|
| 259 | + time() + WEEK_IN_SECONDS, |
|
| 260 | + '/' |
|
| 261 | + ); |
|
| 262 | + // and update the global |
|
| 263 | + $_COOKIE['ee_registration_added'] = $this->_req_data['EVT_ID']; |
|
| 264 | + } |
|
| 265 | + EE_Registry::$i18n_js_strings['invalid_server_response'] = esc_html__( |
|
| 266 | + 'An error occurred! Your request may have been processed, but a valid response from the server was not received. Please refresh the page and try again.', |
|
| 267 | + 'event_espresso' |
|
| 268 | + ); |
|
| 269 | + EE_Registry::$i18n_js_strings['error_occurred'] = esc_html__( |
|
| 270 | + 'An error occurred! Please refresh the page and try again.', |
|
| 271 | + 'event_espresso' |
|
| 272 | + ); |
|
| 273 | + EE_Registry::$i18n_js_strings['txn_status_array'] = self::$_txn_status; |
|
| 274 | + EE_Registry::$i18n_js_strings['pay_status_array'] = self::$_pay_status; |
|
| 275 | + EE_Registry::$i18n_js_strings['payments_total'] = esc_html__('Payments Total', 'event_espresso'); |
|
| 276 | + EE_Registry::$i18n_js_strings['transaction_overpaid'] = esc_html__( |
|
| 277 | + 'This transaction has been overpaid ! Payments Total', |
|
| 278 | + 'event_espresso' |
|
| 279 | + ); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + public function admin_notices() |
|
| 283 | + { |
|
| 284 | + // noop |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + public function admin_footer_scripts() |
|
| 288 | + { |
|
| 289 | + // noop |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * _set_transaction_status_array |
|
| 295 | + * sets list of transaction statuses |
|
| 296 | + * |
|
| 297 | + * @access private |
|
| 298 | + * @return void |
|
| 299 | + * @throws EE_Error |
|
| 300 | + * @throws InvalidArgumentException |
|
| 301 | + * @throws InvalidDataTypeException |
|
| 302 | + * @throws InvalidInterfaceException |
|
| 303 | + */ |
|
| 304 | + private function _set_transaction_status_array() |
|
| 305 | + { |
|
| 306 | + self::$_txn_status = EEM_Transaction::instance()->status_array(true); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * get_transaction_status_array |
|
| 312 | + * return the transaction status array for wp_list_table |
|
| 313 | + * |
|
| 314 | + * @access public |
|
| 315 | + * @return array |
|
| 316 | + */ |
|
| 317 | + public function get_transaction_status_array() |
|
| 318 | + { |
|
| 319 | + return self::$_txn_status; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * get list of payment statuses |
|
| 325 | + * |
|
| 326 | + * @access private |
|
| 327 | + * @return void |
|
| 328 | + * @throws EE_Error |
|
| 329 | + * @throws InvalidArgumentException |
|
| 330 | + * @throws InvalidDataTypeException |
|
| 331 | + * @throws InvalidInterfaceException |
|
| 332 | + */ |
|
| 333 | + private function _get_payment_status_array() |
|
| 334 | + { |
|
| 335 | + self::$_pay_status = EEM_Payment::instance()->status_array(true); |
|
| 336 | + $this->_template_args['payment_status'] = self::$_pay_status; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * _add_screen_options_default |
|
| 342 | + * |
|
| 343 | + * @access protected |
|
| 344 | + * @return void |
|
| 345 | + * @throws InvalidArgumentException |
|
| 346 | + * @throws InvalidDataTypeException |
|
| 347 | + * @throws InvalidInterfaceException |
|
| 348 | + */ |
|
| 349 | + protected function _add_screen_options_default() |
|
| 350 | + { |
|
| 351 | + $this->_per_page_screen_option(); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * load_scripts_styles |
|
| 357 | + * |
|
| 358 | + * @access public |
|
| 359 | + * @return void |
|
| 360 | + */ |
|
| 361 | + public function load_scripts_styles() |
|
| 362 | + { |
|
| 363 | + // enqueue style |
|
| 364 | + wp_register_style( |
|
| 365 | + 'espresso_txn', |
|
| 366 | + TXN_ASSETS_URL . 'espresso_transactions_admin.css', |
|
| 367 | + array(), |
|
| 368 | + EVENT_ESPRESSO_VERSION |
|
| 369 | + ); |
|
| 370 | + wp_enqueue_style('espresso_txn'); |
|
| 371 | + // scripts |
|
| 372 | + wp_register_script( |
|
| 373 | + 'espresso_txn', |
|
| 374 | + TXN_ASSETS_URL . 'espresso_transactions_admin.js', |
|
| 375 | + array( |
|
| 376 | + 'ee_admin_js', |
|
| 377 | + 'ee-datepicker', |
|
| 378 | + 'jquery-ui-datepicker', |
|
| 379 | + 'jquery-ui-draggable', |
|
| 380 | + 'ee-dialog', |
|
| 381 | + 'ee-accounting', |
|
| 382 | + 'ee-serialize-full-array', |
|
| 383 | + ), |
|
| 384 | + EVENT_ESPRESSO_VERSION, |
|
| 385 | + true |
|
| 386 | + ); |
|
| 387 | + wp_enqueue_script('espresso_txn'); |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * load_scripts_styles_view_transaction |
|
| 393 | + * |
|
| 394 | + * @access public |
|
| 395 | + * @return void |
|
| 396 | + */ |
|
| 397 | + public function load_scripts_styles_view_transaction() |
|
| 398 | + { |
|
| 399 | + // styles |
|
| 400 | + wp_enqueue_style('espresso-ui-theme'); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * load_scripts_styles_default |
|
| 406 | + * |
|
| 407 | + * @access public |
|
| 408 | + * @return void |
|
| 409 | + */ |
|
| 410 | + public function load_scripts_styles_default() |
|
| 411 | + { |
|
| 412 | + // styles |
|
| 413 | + wp_enqueue_style('espresso-ui-theme'); |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * _set_list_table_views_default |
|
| 419 | + * |
|
| 420 | + * @access protected |
|
| 421 | + * @return void |
|
| 422 | + */ |
|
| 423 | + protected function _set_list_table_views_default() |
|
| 424 | + { |
|
| 425 | + $this->_views = array( |
|
| 426 | + 'all' => array( |
|
| 427 | + 'slug' => 'all', |
|
| 428 | + 'label' => esc_html__('View All Transactions', 'event_espresso'), |
|
| 429 | + 'count' => 0, |
|
| 430 | + ), |
|
| 431 | + 'abandoned' => array( |
|
| 432 | + 'slug' => 'abandoned', |
|
| 433 | + 'label' => esc_html__('Abandoned Transactions', 'event_espresso'), |
|
| 434 | + 'count' => 0, |
|
| 435 | + ), |
|
| 436 | + 'incomplete' => array( |
|
| 437 | + 'slug' => 'incomplete', |
|
| 438 | + 'label' => esc_html__('Incomplete Transactions', 'event_espresso'), |
|
| 439 | + 'count' => 0, |
|
| 440 | + ) |
|
| 441 | + ); |
|
| 442 | + if (/** |
|
| 443 | + * Filters whether a link to the "Failed Transactions" list table |
|
| 444 | + * appears on the Transactions Admin Page list table. |
|
| 445 | + * List display can be turned back on via the following: |
|
| 446 | + * add_filter( |
|
| 447 | + * 'FHEE__Transactions_Admin_Page___set_list_table_views_default__display_failed_txns_list', |
|
| 448 | + * '__return_true' |
|
| 449 | + * ); |
|
| 450 | + * |
|
| 451 | + * @since 4.9.70.p |
|
| 452 | + * @param boolean $display_failed_txns_list |
|
| 453 | + * @param Transactions_Admin_Page $this |
|
| 454 | + */ |
|
| 455 | + apply_filters( |
|
| 456 | + 'FHEE__Transactions_Admin_Page___set_list_table_views_default__display_failed_txns_list', |
|
| 457 | + false, |
|
| 458 | + $this |
|
| 459 | + ) |
|
| 460 | + ) { |
|
| 461 | + $this->_views['failed'] = array( |
|
| 462 | + 'slug' => 'failed', |
|
| 463 | + 'label' => esc_html__('Failed Transactions', 'event_espresso'), |
|
| 464 | + 'count' => 0, |
|
| 465 | + ); |
|
| 466 | + } |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + |
|
| 470 | + /** |
|
| 471 | + * _set_transaction_object |
|
| 472 | + * This sets the _transaction property for the transaction details screen |
|
| 473 | + * |
|
| 474 | + * @access private |
|
| 475 | + * @return void |
|
| 476 | + * @throws EE_Error |
|
| 477 | + * @throws InvalidArgumentException |
|
| 478 | + * @throws RuntimeException |
|
| 479 | + * @throws InvalidDataTypeException |
|
| 480 | + * @throws InvalidInterfaceException |
|
| 481 | + * @throws ReflectionException |
|
| 482 | + */ |
|
| 483 | + private function _set_transaction_object() |
|
| 484 | + { |
|
| 485 | + if ($this->_transaction instanceof EE_Transaction) { |
|
| 486 | + return; |
|
| 487 | + } //get out we've already set the object |
|
| 488 | + |
|
| 489 | + $TXN_ID = ! empty($this->_req_data['TXN_ID']) |
|
| 490 | + ? absint($this->_req_data['TXN_ID']) |
|
| 491 | + : false; |
|
| 492 | + |
|
| 493 | + // get transaction object |
|
| 494 | + $this->_transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID); |
|
| 495 | + $this->_session = $this->_transaction instanceof EE_Transaction |
|
| 496 | + ? $this->_transaction->get('TXN_session_data') |
|
| 497 | + : null; |
|
| 498 | + if ($this->_transaction instanceof EE_Transaction) { |
|
| 499 | + $this->_transaction->verify_abandoned_transaction_status(); |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + if (! $this->_transaction instanceof EE_Transaction) { |
|
| 503 | + $error_msg = sprintf( |
|
| 504 | + esc_html__( |
|
| 505 | + 'An error occurred and the details for the transaction with the ID # %d could not be retrieved.', |
|
| 506 | + 'event_espresso' |
|
| 507 | + ), |
|
| 508 | + $TXN_ID |
|
| 509 | + ); |
|
| 510 | + EE_Error::add_error($error_msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 511 | + } |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + |
|
| 515 | + /** |
|
| 516 | + * _transaction_legend_items |
|
| 517 | + * |
|
| 518 | + * @access protected |
|
| 519 | + * @return array |
|
| 520 | + * @throws EE_Error |
|
| 521 | + * @throws InvalidArgumentException |
|
| 522 | + * @throws ReflectionException |
|
| 523 | + * @throws InvalidDataTypeException |
|
| 524 | + * @throws InvalidInterfaceException |
|
| 525 | + */ |
|
| 526 | + protected function _transaction_legend_items() |
|
| 527 | + { |
|
| 528 | + EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 529 | + $items = array(); |
|
| 530 | + |
|
| 531 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
| 532 | + 'ee_read_global_messages', |
|
| 533 | + 'view_filtered_messages' |
|
| 534 | + )) { |
|
| 535 | + $related_for_icon = EEH_MSG_Template::get_message_action_icon('see_notifications_for'); |
|
| 536 | + if (is_array($related_for_icon) |
|
| 537 | + && isset($related_for_icon['css_class'], $related_for_icon['label']) |
|
| 538 | + ) { |
|
| 539 | + $items['view_related_messages'] = array( |
|
| 540 | + 'class' => $related_for_icon['css_class'], |
|
| 541 | + 'desc' => $related_for_icon['label'], |
|
| 542 | + ); |
|
| 543 | + } |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + $items = apply_filters( |
|
| 547 | + 'FHEE__Transactions_Admin_Page___transaction_legend_items__items', |
|
| 548 | + array_merge( |
|
| 549 | + $items, |
|
| 550 | + array( |
|
| 551 | + 'view_details' => array( |
|
| 552 | + 'class' => 'dashicons dashicons-cart', |
|
| 553 | + 'desc' => esc_html__('View Transaction Details', 'event_espresso'), |
|
| 554 | + ), |
|
| 555 | + 'view_invoice' => array( |
|
| 556 | + 'class' => 'dashicons dashicons-media-spreadsheet', |
|
| 557 | + 'desc' => esc_html__('View Transaction Invoice', 'event_espresso'), |
|
| 558 | + ), |
|
| 559 | + 'view_receipt' => array( |
|
| 560 | + 'class' => 'dashicons dashicons-media-default', |
|
| 561 | + 'desc' => esc_html__('View Transaction Receipt', 'event_espresso'), |
|
| 562 | + ), |
|
| 563 | + 'view_registration' => array( |
|
| 564 | + 'class' => 'dashicons dashicons-clipboard', |
|
| 565 | + 'desc' => esc_html__('View Registration Details', 'event_espresso'), |
|
| 566 | + ), |
|
| 567 | + 'payment_overview_link' => array( |
|
| 568 | + 'class' => 'dashicons dashicons-money', |
|
| 569 | + 'desc' => esc_html__('Make Payment on Frontend', 'event_espresso'), |
|
| 570 | + ), |
|
| 571 | + ) |
|
| 572 | + ) |
|
| 573 | + ); |
|
| 574 | + |
|
| 575 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
| 576 | + 'ee_send_message', |
|
| 577 | + 'espresso_transactions_send_payment_reminder' |
|
| 578 | + )) { |
|
| 579 | + if (EEH_MSG_Template::is_mt_active('payment_reminder')) { |
|
| 580 | + $items['send_payment_reminder'] = array( |
|
| 581 | + 'class' => 'dashicons dashicons-email-alt', |
|
| 582 | + 'desc' => esc_html__('Send Payment Reminder', 'event_espresso'), |
|
| 583 | + ); |
|
| 584 | + } else { |
|
| 585 | + $items['blank*'] = array( |
|
| 586 | + 'class' => '', |
|
| 587 | + 'desc' => '', |
|
| 588 | + ); |
|
| 589 | + } |
|
| 590 | + } else { |
|
| 591 | + $items['blank*'] = array( |
|
| 592 | + 'class' => '', |
|
| 593 | + 'desc' => '', |
|
| 594 | + ); |
|
| 595 | + } |
|
| 596 | + $more_items = apply_filters( |
|
| 597 | + 'FHEE__Transactions_Admin_Page___transaction_legend_items__more_items', |
|
| 598 | + array( |
|
| 599 | + 'overpaid' => array( |
|
| 600 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::overpaid_status_code, |
|
| 601 | + 'desc' => EEH_Template::pretty_status( |
|
| 602 | + EEM_Transaction::overpaid_status_code, |
|
| 603 | + false, |
|
| 604 | + 'sentence' |
|
| 605 | + ), |
|
| 606 | + ), |
|
| 607 | + 'complete' => array( |
|
| 608 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::complete_status_code, |
|
| 609 | + 'desc' => EEH_Template::pretty_status( |
|
| 610 | + EEM_Transaction::complete_status_code, |
|
| 611 | + false, |
|
| 612 | + 'sentence' |
|
| 613 | + ), |
|
| 614 | + ), |
|
| 615 | + 'incomplete' => array( |
|
| 616 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::incomplete_status_code, |
|
| 617 | + 'desc' => EEH_Template::pretty_status( |
|
| 618 | + EEM_Transaction::incomplete_status_code, |
|
| 619 | + false, |
|
| 620 | + 'sentence' |
|
| 621 | + ), |
|
| 622 | + ), |
|
| 623 | + 'abandoned' => array( |
|
| 624 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::abandoned_status_code, |
|
| 625 | + 'desc' => EEH_Template::pretty_status( |
|
| 626 | + EEM_Transaction::abandoned_status_code, |
|
| 627 | + false, |
|
| 628 | + 'sentence' |
|
| 629 | + ), |
|
| 630 | + ), |
|
| 631 | + 'failed' => array( |
|
| 632 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::failed_status_code, |
|
| 633 | + 'desc' => EEH_Template::pretty_status( |
|
| 634 | + EEM_Transaction::failed_status_code, |
|
| 635 | + false, |
|
| 636 | + 'sentence' |
|
| 637 | + ), |
|
| 638 | + ), |
|
| 639 | + ) |
|
| 640 | + ); |
|
| 641 | + |
|
| 642 | + return array_merge($items, $more_items); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + |
|
| 646 | + /** |
|
| 647 | + * _transactions_overview_list_table |
|
| 648 | + * |
|
| 649 | + * @access protected |
|
| 650 | + * @return void |
|
| 651 | + * @throws DomainException |
|
| 652 | + * @throws EE_Error |
|
| 653 | + * @throws InvalidArgumentException |
|
| 654 | + * @throws InvalidDataTypeException |
|
| 655 | + * @throws InvalidInterfaceException |
|
| 656 | + * @throws ReflectionException |
|
| 657 | + */ |
|
| 658 | + protected function _transactions_overview_list_table() |
|
| 659 | + { |
|
| 660 | + $this->_admin_page_title = esc_html__('Transactions', 'event_espresso'); |
|
| 661 | + $event = isset($this->_req_data['EVT_ID']) |
|
| 662 | + ? EEM_Event::instance()->get_one_by_ID($this->_req_data['EVT_ID']) |
|
| 663 | + : null; |
|
| 664 | + $this->_template_args['admin_page_header'] = $event instanceof EE_Event |
|
| 665 | + ? sprintf( |
|
| 666 | + esc_html__( |
|
| 667 | + '%sViewing Transactions for the Event: %s%s', |
|
| 668 | + 'event_espresso' |
|
| 669 | + ), |
|
| 670 | + '<h3>', |
|
| 671 | + '<a href="' |
|
| 672 | + . EE_Admin_Page::add_query_args_and_nonce( |
|
| 673 | + array('action' => 'edit', 'post' => $event->ID()), |
|
| 674 | + EVENTS_ADMIN_URL |
|
| 675 | + ) |
|
| 676 | + . '" title="' |
|
| 677 | + . esc_attr__( |
|
| 678 | + 'Click to Edit event', |
|
| 679 | + 'event_espresso' |
|
| 680 | + ) |
|
| 681 | + . '">' . $event->get('EVT_name') . '</a>', |
|
| 682 | + '</h3>' |
|
| 683 | + ) |
|
| 684 | + : ''; |
|
| 685 | + $this->_template_args['after_list_table'] = $this->_display_legend($this->_transaction_legend_items()); |
|
| 686 | + $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * _transaction_details |
|
| 692 | + * generates HTML for the View Transaction Details Admin page |
|
| 693 | + * |
|
| 694 | + * @access protected |
|
| 695 | + * @return void |
|
| 696 | + * @throws DomainException |
|
| 697 | + * @throws EE_Error |
|
| 698 | + * @throws InvalidArgumentException |
|
| 699 | + * @throws InvalidDataTypeException |
|
| 700 | + * @throws InvalidInterfaceException |
|
| 701 | + * @throws RuntimeException |
|
| 702 | + * @throws ReflectionException |
|
| 703 | + */ |
|
| 704 | + protected function _transaction_details() |
|
| 705 | + { |
|
| 706 | + do_action('AHEE__Transactions_Admin_Page__transaction_details__start', $this->_transaction); |
|
| 707 | + |
|
| 708 | + $this->_set_transaction_status_array(); |
|
| 709 | + |
|
| 710 | + $this->_template_args = array(); |
|
| 711 | + $this->_template_args['transactions_page'] = $this->_wp_page_slug; |
|
| 712 | + |
|
| 713 | + $this->_set_transaction_object(); |
|
| 714 | + |
|
| 715 | + if (! $this->_transaction instanceof EE_Transaction) { |
|
| 716 | + return; |
|
| 717 | + } |
|
| 718 | + $primary_registration = $this->_transaction->primary_registration(); |
|
| 719 | + $attendee = $primary_registration instanceof EE_Registration |
|
| 720 | + ? $primary_registration->attendee() |
|
| 721 | + : null; |
|
| 722 | + |
|
| 723 | + $this->_template_args['txn_nmbr']['value'] = $this->_transaction->ID(); |
|
| 724 | + $this->_template_args['txn_nmbr']['label'] = esc_html__('Transaction Number', 'event_espresso'); |
|
| 725 | + |
|
| 726 | + $this->_template_args['txn_datetime']['value'] = $this->_transaction->get_i18n_datetime('TXN_timestamp'); |
|
| 727 | + $this->_template_args['txn_datetime']['label'] = esc_html__('Date', 'event_espresso'); |
|
| 728 | + |
|
| 729 | + $this->_template_args['txn_status']['value'] = self::$_txn_status[ $this->_transaction->get('STS_ID') ]; |
|
| 730 | + $this->_template_args['txn_status']['label'] = esc_html__('Transaction Status', 'event_espresso'); |
|
| 731 | + $this->_template_args['txn_status']['class'] = 'status-' . $this->_transaction->get('STS_ID'); |
|
| 732 | + |
|
| 733 | + $this->_template_args['grand_total'] = $this->_transaction->get('TXN_total'); |
|
| 734 | + $this->_template_args['total_paid'] = $this->_transaction->get('TXN_paid'); |
|
| 735 | + |
|
| 736 | + $amount_due = $this->_transaction->get('TXN_total') - $this->_transaction->get('TXN_paid'); |
|
| 737 | + $this->_template_args['amount_due'] = EEH_Template::format_currency( |
|
| 738 | + $amount_due, |
|
| 739 | + true |
|
| 740 | + ); |
|
| 741 | + if (EE_Registry::instance()->CFG->currency->sign_b4) { |
|
| 742 | + $this->_template_args['amount_due'] = EE_Registry::instance()->CFG->currency->sign |
|
| 743 | + . $this->_template_args['amount_due']; |
|
| 744 | + } else { |
|
| 745 | + $this->_template_args['amount_due'] .= EE_Registry::instance()->CFG->currency->sign; |
|
| 746 | + } |
|
| 747 | + $this->_template_args['amount_due_class'] = ''; |
|
| 748 | + |
|
| 749 | + if ($this->_transaction->get('TXN_paid') == $this->_transaction->get('TXN_total')) { |
|
| 750 | + // paid in full |
|
| 751 | + $this->_template_args['amount_due'] = false; |
|
| 752 | + } elseif ($this->_transaction->get('TXN_paid') > $this->_transaction->get('TXN_total')) { |
|
| 753 | + // overpaid |
|
| 754 | + $this->_template_args['amount_due_class'] = 'txn-overview-no-payment-spn'; |
|
| 755 | + } elseif ($this->_transaction->get('TXN_total') > 0 |
|
| 756 | + && $this->_transaction->get('TXN_paid') > 0 |
|
| 757 | + ) { |
|
| 758 | + // monies owing |
|
| 759 | + $this->_template_args['amount_due_class'] = 'txn-overview-part-payment-spn'; |
|
| 760 | + } elseif ($this->_transaction->get('TXN_total') > 0 |
|
| 761 | + && $this->_transaction->get('TXN_paid') == 0 |
|
| 762 | + ) { |
|
| 763 | + // no payments made yet |
|
| 764 | + $this->_template_args['amount_due_class'] = 'txn-overview-no-payment-spn'; |
|
| 765 | + } elseif ($this->_transaction->get('TXN_total') == 0) { |
|
| 766 | + // free event |
|
| 767 | + $this->_template_args['amount_due'] = false; |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + $payment_method = $this->_transaction->payment_method(); |
|
| 771 | + |
|
| 772 | + $this->_template_args['method_of_payment_name'] = $payment_method instanceof EE_Payment_Method |
|
| 773 | + ? $payment_method->admin_name() |
|
| 774 | + : esc_html__('Unknown', 'event_espresso'); |
|
| 775 | + |
|
| 776 | + $this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign; |
|
| 777 | + // link back to overview |
|
| 778 | + $this->_template_args['txn_overview_url'] = ! empty($_SERVER['HTTP_REFERER']) |
|
| 779 | + ? $_SERVER['HTTP_REFERER'] |
|
| 780 | + : TXN_ADMIN_URL; |
|
| 781 | + |
|
| 782 | + |
|
| 783 | + // next link |
|
| 784 | + $next_txn = $this->_transaction->next( |
|
| 785 | + null, |
|
| 786 | + array(array('STS_ID' => array('!=', EEM_Transaction::failed_status_code))), |
|
| 787 | + 'TXN_ID' |
|
| 788 | + ); |
|
| 789 | + $this->_template_args['next_transaction'] = $next_txn |
|
| 790 | + ? $this->_next_link( |
|
| 791 | + EE_Admin_Page::add_query_args_and_nonce( |
|
| 792 | + array('action' => 'view_transaction', 'TXN_ID' => $next_txn['TXN_ID']), |
|
| 793 | + TXN_ADMIN_URL |
|
| 794 | + ), |
|
| 795 | + 'dashicons dashicons-arrow-right ee-icon-size-22' |
|
| 796 | + ) |
|
| 797 | + : ''; |
|
| 798 | + // previous link |
|
| 799 | + $previous_txn = $this->_transaction->previous( |
|
| 800 | + null, |
|
| 801 | + array(array('STS_ID' => array('!=', EEM_Transaction::failed_status_code))), |
|
| 802 | + 'TXN_ID' |
|
| 803 | + ); |
|
| 804 | + $this->_template_args['previous_transaction'] = $previous_txn |
|
| 805 | + ? $this->_previous_link( |
|
| 806 | + EE_Admin_Page::add_query_args_and_nonce( |
|
| 807 | + array('action' => 'view_transaction', 'TXN_ID' => $previous_txn['TXN_ID']), |
|
| 808 | + TXN_ADMIN_URL |
|
| 809 | + ), |
|
| 810 | + 'dashicons dashicons-arrow-left ee-icon-size-22' |
|
| 811 | + ) |
|
| 812 | + : ''; |
|
| 813 | + |
|
| 814 | + // were we just redirected here after adding a new registration ??? |
|
| 815 | + if (isset( |
|
| 816 | + $this->_req_data['redirect_from'], |
|
| 817 | + $this->_req_data['EVT_ID'], |
|
| 818 | + $this->_req_data['event_name'] |
|
| 819 | + )) { |
|
| 820 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
| 821 | + 'ee_edit_registrations', |
|
| 822 | + 'espresso_registrations_new_registration', |
|
| 823 | + $this->_req_data['EVT_ID'] |
|
| 824 | + )) { |
|
| 825 | + $this->_admin_page_title .= '<a id="add-new-registration" class="add-new-h2 button-primary" href="'; |
|
| 826 | + $this->_admin_page_title .= EE_Admin_Page::add_query_args_and_nonce( |
|
| 827 | + array( |
|
| 828 | + 'page' => 'espresso_registrations', |
|
| 829 | + 'action' => 'new_registration', |
|
| 830 | + 'return' => 'default', |
|
| 831 | + 'TXN_ID' => $this->_transaction->ID(), |
|
| 832 | + 'event_id' => $this->_req_data['EVT_ID'], |
|
| 833 | + ), |
|
| 834 | + REG_ADMIN_URL |
|
| 835 | + ); |
|
| 836 | + $this->_admin_page_title .= '">'; |
|
| 837 | + |
|
| 838 | + $this->_admin_page_title .= sprintf( |
|
| 839 | + esc_html__('Add Another New Registration to Event: "%1$s" ?', 'event_espresso'), |
|
| 840 | + htmlentities(urldecode($this->_req_data['event_name']), ENT_QUOTES, 'UTF-8') |
|
| 841 | + ); |
|
| 842 | + $this->_admin_page_title .= '</a>'; |
|
| 843 | + } |
|
| 844 | + EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
| 845 | + } |
|
| 846 | + // grab messages at the last second |
|
| 847 | + $this->_template_args['notices'] = EE_Error::get_notices(); |
|
| 848 | + // path to template |
|
| 849 | + $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_header.template.php'; |
|
| 850 | + $this->_template_args['admin_page_header'] = EEH_Template::display_template( |
|
| 851 | + $template_path, |
|
| 852 | + $this->_template_args, |
|
| 853 | + true |
|
| 854 | + ); |
|
| 855 | + |
|
| 856 | + // the details template wrapper |
|
| 857 | + $this->display_admin_page_with_sidebar(); |
|
| 858 | + } |
|
| 859 | + |
|
| 860 | + |
|
| 861 | + /** |
|
| 862 | + * _transaction_details_metaboxes |
|
| 863 | + * |
|
| 864 | + * @access protected |
|
| 865 | + * @return void |
|
| 866 | + * @throws EE_Error |
|
| 867 | + * @throws InvalidArgumentException |
|
| 868 | + * @throws InvalidDataTypeException |
|
| 869 | + * @throws InvalidInterfaceException |
|
| 870 | + * @throws RuntimeException |
|
| 871 | + * @throws ReflectionException |
|
| 872 | + */ |
|
| 873 | + protected function _transaction_details_metaboxes() |
|
| 874 | + { |
|
| 875 | + |
|
| 876 | + $this->_set_transaction_object(); |
|
| 877 | + |
|
| 878 | + if (! $this->_transaction instanceof EE_Transaction) { |
|
| 879 | + return; |
|
| 880 | + } |
|
| 881 | + add_meta_box( |
|
| 882 | + 'edit-txn-details-mbox', |
|
| 883 | + esc_html__('Transaction Details', 'event_espresso'), |
|
| 884 | + array($this, 'txn_details_meta_box'), |
|
| 885 | + $this->_wp_page_slug, |
|
| 886 | + 'normal', |
|
| 887 | + 'high' |
|
| 888 | + ); |
|
| 889 | + add_meta_box( |
|
| 890 | + 'edit-txn-attendees-mbox', |
|
| 891 | + esc_html__('Attendees Registered in this Transaction', 'event_espresso'), |
|
| 892 | + array($this, 'txn_attendees_meta_box'), |
|
| 893 | + $this->_wp_page_slug, |
|
| 894 | + 'normal', |
|
| 895 | + 'high', |
|
| 896 | + array('TXN_ID' => $this->_transaction->ID()) |
|
| 897 | + ); |
|
| 898 | + add_meta_box( |
|
| 899 | + 'edit-txn-registrant-mbox', |
|
| 900 | + esc_html__('Primary Contact', 'event_espresso'), |
|
| 901 | + array($this, 'txn_registrant_side_meta_box'), |
|
| 902 | + $this->_wp_page_slug, |
|
| 903 | + 'side', |
|
| 904 | + 'high' |
|
| 905 | + ); |
|
| 906 | + add_meta_box( |
|
| 907 | + 'edit-txn-billing-info-mbox', |
|
| 908 | + esc_html__('Billing Information', 'event_espresso'), |
|
| 909 | + array($this, 'txn_billing_info_side_meta_box'), |
|
| 910 | + $this->_wp_page_slug, |
|
| 911 | + 'side', |
|
| 912 | + 'high' |
|
| 913 | + ); |
|
| 914 | + } |
|
| 915 | + |
|
| 916 | + |
|
| 917 | + /** |
|
| 918 | + * Callback for transaction actions metabox. |
|
| 919 | + * |
|
| 920 | + * @param EE_Transaction|null $transaction |
|
| 921 | + * @throws DomainException |
|
| 922 | + * @throws EE_Error |
|
| 923 | + * @throws InvalidArgumentException |
|
| 924 | + * @throws InvalidDataTypeException |
|
| 925 | + * @throws InvalidInterfaceException |
|
| 926 | + * @throws ReflectionException |
|
| 927 | + * @throws RuntimeException |
|
| 928 | + */ |
|
| 929 | + public function getActionButtons(EE_Transaction $transaction = null) |
|
| 930 | + { |
|
| 931 | + $content = ''; |
|
| 932 | + $actions = array(); |
|
| 933 | + if (! $transaction instanceof EE_Transaction) { |
|
| 934 | + return $content; |
|
| 935 | + } |
|
| 936 | + /** @var EE_Registration $primary_registration */ |
|
| 937 | + $primary_registration = $transaction->primary_registration(); |
|
| 938 | + $attendee = $primary_registration instanceof EE_Registration |
|
| 939 | + ? $primary_registration->attendee() |
|
| 940 | + : null; |
|
| 941 | + |
|
| 942 | + if ($attendee instanceof EE_Attendee |
|
| 943 | + && EE_Registry::instance()->CAP->current_user_can( |
|
| 944 | + 'ee_send_message', |
|
| 945 | + 'espresso_transactions_send_payment_reminder' |
|
| 946 | + ) |
|
| 947 | + ) { |
|
| 948 | + $actions['payment_reminder'] = |
|
| 949 | + EEH_MSG_Template::is_mt_active('payment_reminder') |
|
| 950 | + && $this->_transaction->get('STS_ID') !== EEM_Transaction::complete_status_code |
|
| 951 | + && $this->_transaction->get('STS_ID') !== EEM_Transaction::overpaid_status_code |
|
| 952 | + ? EEH_Template::get_button_or_link( |
|
| 953 | + EE_Admin_Page::add_query_args_and_nonce( |
|
| 954 | + array( |
|
| 955 | + 'action' => 'send_payment_reminder', |
|
| 956 | + 'TXN_ID' => $this->_transaction->ID(), |
|
| 957 | + 'redirect_to' => 'view_transaction', |
|
| 958 | + ), |
|
| 959 | + TXN_ADMIN_URL |
|
| 960 | + ), |
|
| 961 | + esc_html__(' Send Payment Reminder', 'event_espresso'), |
|
| 962 | + 'button secondary-button', |
|
| 963 | + 'dashicons dashicons-email-alt' |
|
| 964 | + ) |
|
| 965 | + : ''; |
|
| 966 | + } |
|
| 967 | + |
|
| 968 | + if ($primary_registration instanceof EE_Registration |
|
| 969 | + && EEH_MSG_Template::is_mt_active('receipt') |
|
| 970 | + ) { |
|
| 971 | + $actions['receipt'] = EEH_Template::get_button_or_link( |
|
| 972 | + $primary_registration->receipt_url(), |
|
| 973 | + esc_html__('View Receipt', 'event_espresso'), |
|
| 974 | + 'button secondary-button', |
|
| 975 | + 'dashicons dashicons-media-default' |
|
| 976 | + ); |
|
| 977 | + } |
|
| 978 | + |
|
| 979 | + if ($primary_registration instanceof EE_Registration |
|
| 980 | + && EEH_MSG_Template::is_mt_active('invoice') |
|
| 981 | + ) { |
|
| 982 | + $actions['invoice'] = EEH_Template::get_button_or_link( |
|
| 983 | + $primary_registration->invoice_url(), |
|
| 984 | + esc_html__('View Invoice', 'event_espresso'), |
|
| 985 | + 'button secondary-button', |
|
| 986 | + 'dashicons dashicons-media-spreadsheet' |
|
| 987 | + ); |
|
| 988 | + } |
|
| 989 | + $actions = array_filter( |
|
| 990 | + apply_filters('FHEE__Transactions_Admin_Page__getActionButtons__actions', $actions, $transaction) |
|
| 991 | + ); |
|
| 992 | + if ($actions) { |
|
| 993 | + $content = '<ul>'; |
|
| 994 | + $content .= '<li>' . implode('</li><li>', $actions) . '</li>'; |
|
| 995 | + $content .= '</uL>'; |
|
| 996 | + } |
|
| 997 | + return $content; |
|
| 998 | + } |
|
| 999 | + |
|
| 1000 | + |
|
| 1001 | + /** |
|
| 1002 | + * txn_details_meta_box |
|
| 1003 | + * generates HTML for the Transaction main meta box |
|
| 1004 | + * |
|
| 1005 | + * @return void |
|
| 1006 | + * @throws DomainException |
|
| 1007 | + * @throws EE_Error |
|
| 1008 | + * @throws InvalidArgumentException |
|
| 1009 | + * @throws InvalidDataTypeException |
|
| 1010 | + * @throws InvalidInterfaceException |
|
| 1011 | + * @throws RuntimeException |
|
| 1012 | + * @throws ReflectionException |
|
| 1013 | + */ |
|
| 1014 | + public function txn_details_meta_box() |
|
| 1015 | + { |
|
| 1016 | + $this->_set_transaction_object(); |
|
| 1017 | + $this->_template_args['TXN_ID'] = $this->_transaction->ID(); |
|
| 1018 | + $this->_template_args['attendee'] = $this->_transaction->primary_registration() instanceof EE_Registration |
|
| 1019 | + ? $this->_transaction->primary_registration()->attendee() |
|
| 1020 | + : null; |
|
| 1021 | + $this->_template_args['can_edit_payments'] = EE_Registry::instance()->CAP->current_user_can( |
|
| 1022 | + 'ee_edit_payments', |
|
| 1023 | + 'apply_payment_or_refund_from_registration_details' |
|
| 1024 | + ); |
|
| 1025 | + $this->_template_args['can_delete_payments'] = EE_Registry::instance()->CAP->current_user_can( |
|
| 1026 | + 'ee_delete_payments', |
|
| 1027 | + 'delete_payment_from_registration_details' |
|
| 1028 | + ); |
|
| 1029 | + |
|
| 1030 | + // get line table |
|
| 1031 | + EEH_Autoloader::register_line_item_display_autoloaders(); |
|
| 1032 | + $Line_Item_Display = new EE_Line_Item_Display( |
|
| 1033 | + 'admin_table', |
|
| 1034 | + 'EE_Admin_Table_Line_Item_Display_Strategy' |
|
| 1035 | + ); |
|
| 1036 | + $this->_template_args['line_item_table'] = $Line_Item_Display->display_line_item( |
|
| 1037 | + $this->_transaction->total_line_item() |
|
| 1038 | + ); |
|
| 1039 | + $this->_template_args['REG_code'] = $this->_transaction->get_first_related('Registration') |
|
| 1040 | + ->get('REG_code'); |
|
| 1041 | + |
|
| 1042 | + // process taxes |
|
| 1043 | + $taxes = $this->_transaction->get_many_related( |
|
| 1044 | + 'Line_Item', |
|
| 1045 | + array(array('LIN_type' => EEM_Line_Item::type_tax)) |
|
| 1046 | + ); |
|
| 1047 | + $this->_template_args['taxes'] = ! empty($taxes) ? $taxes : false; |
|
| 1048 | + |
|
| 1049 | + $this->_template_args['grand_total'] = EEH_Template::format_currency( |
|
| 1050 | + $this->_transaction->get('TXN_total'), |
|
| 1051 | + false, |
|
| 1052 | + false |
|
| 1053 | + ); |
|
| 1054 | + $this->_template_args['grand_raw_total'] = $this->_transaction->get('TXN_total'); |
|
| 1055 | + $this->_template_args['TXN_status'] = $this->_transaction->get('STS_ID'); |
|
| 1056 | + |
|
| 1057 | + // process payment details |
|
| 1058 | + $payments = $this->_transaction->get_many_related('Payment'); |
|
| 1059 | + if (! empty($payments)) { |
|
| 1060 | + $this->_template_args['payments'] = $payments; |
|
| 1061 | + $this->_template_args['existing_reg_payments'] = $this->_get_registration_payment_IDs($payments); |
|
| 1062 | + } else { |
|
| 1063 | + $this->_template_args['payments'] = false; |
|
| 1064 | + $this->_template_args['existing_reg_payments'] = array(); |
|
| 1065 | + } |
|
| 1066 | + |
|
| 1067 | + $this->_template_args['edit_payment_url'] = add_query_arg(array('action' => 'edit_payment'), TXN_ADMIN_URL); |
|
| 1068 | + $this->_template_args['delete_payment_url'] = add_query_arg( |
|
| 1069 | + array('action' => 'espresso_delete_payment'), |
|
| 1070 | + TXN_ADMIN_URL |
|
| 1071 | + ); |
|
| 1072 | + |
|
| 1073 | + if (isset($txn_details['invoice_number'])) { |
|
| 1074 | + $this->_template_args['txn_details']['invoice_number']['value'] = $this->_template_args['REG_code']; |
|
| 1075 | + $this->_template_args['txn_details']['invoice_number']['label'] = esc_html__( |
|
| 1076 | + 'Invoice Number', |
|
| 1077 | + 'event_espresso' |
|
| 1078 | + ); |
|
| 1079 | + } |
|
| 1080 | + |
|
| 1081 | + $this->_template_args['txn_details']['registration_session']['value'] = $this->_transaction |
|
| 1082 | + ->get_first_related('Registration') |
|
| 1083 | + ->get('REG_session'); |
|
| 1084 | + $this->_template_args['txn_details']['registration_session']['label'] = esc_html__( |
|
| 1085 | + 'Registration Session', |
|
| 1086 | + 'event_espresso' |
|
| 1087 | + ); |
|
| 1088 | + |
|
| 1089 | + $this->_template_args['txn_details']['ip_address']['value'] = isset($this->_session['ip_address']) |
|
| 1090 | + ? $this->_session['ip_address'] |
|
| 1091 | + : ''; |
|
| 1092 | + $this->_template_args['txn_details']['ip_address']['label'] = esc_html__( |
|
| 1093 | + 'Transaction placed from IP', |
|
| 1094 | + 'event_espresso' |
|
| 1095 | + ); |
|
| 1096 | + |
|
| 1097 | + $this->_template_args['txn_details']['user_agent']['value'] = isset($this->_session['user_agent']) |
|
| 1098 | + ? $this->_session['user_agent'] |
|
| 1099 | + : ''; |
|
| 1100 | + $this->_template_args['txn_details']['user_agent']['label'] = esc_html__( |
|
| 1101 | + 'Registrant User Agent', |
|
| 1102 | + 'event_espresso' |
|
| 1103 | + ); |
|
| 1104 | + |
|
| 1105 | + $reg_steps = '<ul>'; |
|
| 1106 | + foreach ($this->_transaction->reg_steps() as $reg_step => $reg_step_status) { |
|
| 1107 | + if ($reg_step_status === true) { |
|
| 1108 | + $reg_steps .= '<li style="color:#70cc50">' |
|
| 1109 | + . sprintf( |
|
| 1110 | + esc_html__('%1$s : Completed', 'event_espresso'), |
|
| 1111 | + ucwords(str_replace('_', ' ', $reg_step)) |
|
| 1112 | + ) |
|
| 1113 | + . '</li>'; |
|
| 1114 | + } elseif (is_numeric($reg_step_status) && $reg_step_status !== false) { |
|
| 1115 | + $reg_steps .= '<li style="color:#2EA2CC">' |
|
| 1116 | + . sprintf( |
|
| 1117 | + esc_html__('%1$s : Initiated %2$s', 'event_espresso'), |
|
| 1118 | + ucwords(str_replace('_', ' ', $reg_step)), |
|
| 1119 | + date( |
|
| 1120 | + get_option('date_format') . ' ' . get_option('time_format'), |
|
| 1121 | + ($reg_step_status + (get_option('gmt_offset') * HOUR_IN_SECONDS)) |
|
| 1122 | + ) |
|
| 1123 | + ) |
|
| 1124 | + . '</li>'; |
|
| 1125 | + } else { |
|
| 1126 | + $reg_steps .= '<li style="color:#E76700">' |
|
| 1127 | + . sprintf( |
|
| 1128 | + esc_html__('%1$s : Never Initiated', 'event_espresso'), |
|
| 1129 | + ucwords(str_replace('_', ' ', $reg_step)) |
|
| 1130 | + ) |
|
| 1131 | + . '</li>'; |
|
| 1132 | + } |
|
| 1133 | + } |
|
| 1134 | + $reg_steps .= '</ul>'; |
|
| 1135 | + $this->_template_args['txn_details']['reg_steps']['value'] = $reg_steps; |
|
| 1136 | + $this->_template_args['txn_details']['reg_steps']['label'] = esc_html__( |
|
| 1137 | + 'Registration Step Progress', |
|
| 1138 | + 'event_espresso' |
|
| 1139 | + ); |
|
| 1140 | + |
|
| 1141 | + |
|
| 1142 | + $this->_get_registrations_to_apply_payment_to(); |
|
| 1143 | + $this->_get_payment_methods($payments); |
|
| 1144 | + $this->_get_payment_status_array(); |
|
| 1145 | + $this->_get_reg_status_selection(); // sets up the template args for the reg status array for the transaction. |
|
| 1146 | + |
|
| 1147 | + $this->_template_args['transaction_form_url'] = add_query_arg( |
|
| 1148 | + array( |
|
| 1149 | + 'action' => 'edit_transaction', |
|
| 1150 | + 'process' => 'transaction', |
|
| 1151 | + ), |
|
| 1152 | + TXN_ADMIN_URL |
|
| 1153 | + ); |
|
| 1154 | + $this->_template_args['apply_payment_form_url'] = add_query_arg( |
|
| 1155 | + array( |
|
| 1156 | + 'page' => 'espresso_transactions', |
|
| 1157 | + 'action' => 'espresso_apply_payment', |
|
| 1158 | + ), |
|
| 1159 | + WP_AJAX_URL |
|
| 1160 | + ); |
|
| 1161 | + $this->_template_args['delete_payment_form_url'] = add_query_arg( |
|
| 1162 | + array( |
|
| 1163 | + 'page' => 'espresso_transactions', |
|
| 1164 | + 'action' => 'espresso_delete_payment', |
|
| 1165 | + ), |
|
| 1166 | + WP_AJAX_URL |
|
| 1167 | + ); |
|
| 1168 | + |
|
| 1169 | + $this->_template_args['action_buttons'] = $this->getActionButtons($this->_transaction); |
|
| 1170 | + |
|
| 1171 | + // 'espresso_delete_payment_nonce' |
|
| 1172 | + |
|
| 1173 | + $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_txn_details.template.php'; |
|
| 1174 | + echo EEH_Template::display_template($template_path, $this->_template_args, true); |
|
| 1175 | + } |
|
| 1176 | + |
|
| 1177 | + |
|
| 1178 | + /** |
|
| 1179 | + * _get_registration_payment_IDs |
|
| 1180 | + * generates an array of Payment IDs and their corresponding Registration IDs |
|
| 1181 | + * |
|
| 1182 | + * @access protected |
|
| 1183 | + * @param EE_Payment[] $payments |
|
| 1184 | + * @return array |
|
| 1185 | + * @throws EE_Error |
|
| 1186 | + * @throws InvalidArgumentException |
|
| 1187 | + * @throws InvalidDataTypeException |
|
| 1188 | + * @throws InvalidInterfaceException |
|
| 1189 | + * @throws ReflectionException |
|
| 1190 | + */ |
|
| 1191 | + protected function _get_registration_payment_IDs($payments = array()) |
|
| 1192 | + { |
|
| 1193 | + $existing_reg_payments = array(); |
|
| 1194 | + // get all reg payments for these payments |
|
| 1195 | + $reg_payments = EEM_Registration_Payment::instance()->get_all( |
|
| 1196 | + array( |
|
| 1197 | + array( |
|
| 1198 | + 'PAY_ID' => array( |
|
| 1199 | + 'IN', |
|
| 1200 | + array_keys($payments), |
|
| 1201 | + ), |
|
| 1202 | + ), |
|
| 1203 | + ) |
|
| 1204 | + ); |
|
| 1205 | + if (! empty($reg_payments)) { |
|
| 1206 | + foreach ($payments as $payment) { |
|
| 1207 | + if (! $payment instanceof EE_Payment) { |
|
| 1208 | + continue; |
|
| 1209 | + } elseif (! isset($existing_reg_payments[ $payment->ID() ])) { |
|
| 1210 | + $existing_reg_payments[ $payment->ID() ] = array(); |
|
| 1211 | + } |
|
| 1212 | + foreach ($reg_payments as $reg_payment) { |
|
| 1213 | + if ($reg_payment instanceof EE_Registration_Payment |
|
| 1214 | + && $reg_payment->payment_ID() === $payment->ID() |
|
| 1215 | + ) { |
|
| 1216 | + $existing_reg_payments[ $payment->ID() ][] = $reg_payment->registration_ID(); |
|
| 1217 | + } |
|
| 1218 | + } |
|
| 1219 | + } |
|
| 1220 | + } |
|
| 1221 | + |
|
| 1222 | + return $existing_reg_payments; |
|
| 1223 | + } |
|
| 1224 | + |
|
| 1225 | + |
|
| 1226 | + /** |
|
| 1227 | + * _get_registrations_to_apply_payment_to |
|
| 1228 | + * generates HTML for displaying a series of checkboxes in the admin payment modal window |
|
| 1229 | + * which allows the admin to only apply the payment to the specific registrations |
|
| 1230 | + * |
|
| 1231 | + * @access protected |
|
| 1232 | + * @return void |
|
| 1233 | + * @throws \EE_Error |
|
| 1234 | + */ |
|
| 1235 | + protected function _get_registrations_to_apply_payment_to() |
|
| 1236 | + { |
|
| 1237 | + // we want any registration with an active status (ie: not deleted or cancelled) |
|
| 1238 | + $query_params = array( |
|
| 1239 | + array( |
|
| 1240 | + 'STS_ID' => array( |
|
| 1241 | + 'IN', |
|
| 1242 | + array( |
|
| 1243 | + EEM_Registration::status_id_approved, |
|
| 1244 | + EEM_Registration::status_id_pending_payment, |
|
| 1245 | + EEM_Registration::status_id_not_approved, |
|
| 1246 | + ), |
|
| 1247 | + ), |
|
| 1248 | + ), |
|
| 1249 | + ); |
|
| 1250 | + $registrations_to_apply_payment_to = EEH_HTML::br() |
|
| 1251 | + . EEH_HTML::div( |
|
| 1252 | + '', |
|
| 1253 | + 'txn-admin-apply-payment-to-registrations-dv', |
|
| 1254 | + '', |
|
| 1255 | + 'clear: both; margin: 1.5em 0 0; display: none;' |
|
| 1256 | + ); |
|
| 1257 | + $registrations_to_apply_payment_to .= EEH_HTML::br() . EEH_HTML::div('', '', 'admin-primary-mbox-tbl-wrap'); |
|
| 1258 | + $registrations_to_apply_payment_to .= EEH_HTML::table('', '', 'admin-primary-mbox-tbl'); |
|
| 1259 | + $registrations_to_apply_payment_to .= EEH_HTML::thead( |
|
| 1260 | + EEH_HTML::tr( |
|
| 1261 | + EEH_HTML::th(esc_html__('ID', 'event_espresso')) . |
|
| 1262 | + EEH_HTML::th(esc_html__('Registrant', 'event_espresso')) . |
|
| 1263 | + EEH_HTML::th(esc_html__('Ticket', 'event_espresso')) . |
|
| 1264 | + EEH_HTML::th(esc_html__('Event', 'event_espresso')) . |
|
| 1265 | + EEH_HTML::th(esc_html__('Paid', 'event_espresso'), '', 'txn-admin-payment-paid-td jst-cntr') . |
|
| 1266 | + EEH_HTML::th(esc_html__('Owing', 'event_espresso'), '', 'txn-admin-payment-owing-td jst-cntr') . |
|
| 1267 | + EEH_HTML::th(esc_html__('Apply', 'event_espresso'), '', 'jst-cntr') |
|
| 1268 | + ) |
|
| 1269 | + ); |
|
| 1270 | + $registrations_to_apply_payment_to .= EEH_HTML::tbody(); |
|
| 1271 | + // get registrations for TXN |
|
| 1272 | + $registrations = $this->_transaction->registrations($query_params); |
|
| 1273 | + $existing_reg_payments = $this->_template_args['existing_reg_payments']; |
|
| 1274 | + foreach ($registrations as $registration) { |
|
| 1275 | + if ($registration instanceof EE_Registration) { |
|
| 1276 | + $attendee_name = $registration->attendee() instanceof EE_Attendee |
|
| 1277 | + ? $registration->attendee()->full_name() |
|
| 1278 | + : esc_html__('Unknown Attendee', 'event_espresso'); |
|
| 1279 | + $owing = $registration->final_price() - $registration->paid(); |
|
| 1280 | + $taxable = $registration->ticket()->taxable() |
|
| 1281 | + ? ' <span class="smaller-text lt-grey-text"> ' . esc_html__('+ tax', 'event_espresso') . '</span>' |
|
| 1282 | + : ''; |
|
| 1283 | + $checked = empty($existing_reg_payments) || in_array($registration->ID(), $existing_reg_payments) |
|
| 1284 | + ? ' checked="checked"' |
|
| 1285 | + : ''; |
|
| 1286 | + $disabled = $registration->final_price() > 0 ? '' : ' disabled'; |
|
| 1287 | + $registrations_to_apply_payment_to .= EEH_HTML::tr( |
|
| 1288 | + EEH_HTML::td($registration->ID()) . |
|
| 1289 | + EEH_HTML::td($attendee_name) . |
|
| 1290 | + EEH_HTML::td( |
|
| 1291 | + $registration->ticket()->name() . ' : ' . $registration->ticket()->pretty_price() . $taxable |
|
| 1292 | + ) . |
|
| 1293 | + EEH_HTML::td($registration->event_name()) . |
|
| 1294 | + EEH_HTML::td($registration->pretty_paid(), '', 'txn-admin-payment-paid-td jst-cntr') . |
|
| 1295 | + EEH_HTML::td(EEH_Template::format_currency($owing), '', 'txn-admin-payment-owing-td jst-cntr') . |
|
| 1296 | + EEH_HTML::td( |
|
| 1297 | + '<input type="checkbox" value="' . $registration->ID() |
|
| 1298 | + . '" name="txn_admin_payment[registrations]"' |
|
| 1299 | + . $checked . $disabled . '>', |
|
| 1300 | + '', |
|
| 1301 | + 'jst-cntr' |
|
| 1302 | + ), |
|
| 1303 | + 'apply-payment-registration-row-' . $registration->ID() |
|
| 1304 | + ); |
|
| 1305 | + } |
|
| 1306 | + } |
|
| 1307 | + $registrations_to_apply_payment_to .= EEH_HTML::tbodyx(); |
|
| 1308 | + $registrations_to_apply_payment_to .= EEH_HTML::tablex(); |
|
| 1309 | + $registrations_to_apply_payment_to .= EEH_HTML::divx(); |
|
| 1310 | + $registrations_to_apply_payment_to .= EEH_HTML::p( |
|
| 1311 | + esc_html__( |
|
| 1312 | + 'The payment will only be applied to the registrations that have a check mark in their corresponding check box. Checkboxes for free registrations have been disabled.', |
|
| 1313 | + 'event_espresso' |
|
| 1314 | + ), |
|
| 1315 | + '', |
|
| 1316 | + 'clear description' |
|
| 1317 | + ); |
|
| 1318 | + $registrations_to_apply_payment_to .= EEH_HTML::divx(); |
|
| 1319 | + $this->_template_args['registrations_to_apply_payment_to'] = $registrations_to_apply_payment_to; |
|
| 1320 | + } |
|
| 1321 | + |
|
| 1322 | + |
|
| 1323 | + /** |
|
| 1324 | + * _get_reg_status_selection |
|
| 1325 | + * |
|
| 1326 | + * @todo this will need to be adjusted either once MER comes along OR we move default reg status to tickets |
|
| 1327 | + * instead of events. |
|
| 1328 | + * @access protected |
|
| 1329 | + * @return void |
|
| 1330 | + * @throws EE_Error |
|
| 1331 | + */ |
|
| 1332 | + protected function _get_reg_status_selection() |
|
| 1333 | + { |
|
| 1334 | + // first get all possible statuses |
|
| 1335 | + $statuses = EEM_Registration::reg_status_array(array(), true); |
|
| 1336 | + // let's add a "don't change" option. |
|
| 1337 | + $status_array['NAN'] = esc_html__('Leave the Same', 'event_espresso'); |
|
| 1338 | + $status_array = array_merge($status_array, $statuses); |
|
| 1339 | + $this->_template_args['status_change_select'] = EEH_Form_Fields::select_input( |
|
| 1340 | + 'txn_reg_status_change[reg_status]', |
|
| 1341 | + $status_array, |
|
| 1342 | + 'NAN', |
|
| 1343 | + 'id="txn-admin-payment-reg-status-inp"', |
|
| 1344 | + 'txn-reg-status-change-reg-status' |
|
| 1345 | + ); |
|
| 1346 | + $this->_template_args['delete_status_change_select'] = EEH_Form_Fields::select_input( |
|
| 1347 | + 'delete_txn_reg_status_change[reg_status]', |
|
| 1348 | + $status_array, |
|
| 1349 | + 'NAN', |
|
| 1350 | + 'delete-txn-admin-payment-reg-status-inp', |
|
| 1351 | + 'delete-txn-reg-status-change-reg-status' |
|
| 1352 | + ); |
|
| 1353 | + } |
|
| 1354 | + |
|
| 1355 | + |
|
| 1356 | + /** |
|
| 1357 | + * _get_payment_methods |
|
| 1358 | + * Gets all the payment methods available generally, or the ones that are already |
|
| 1359 | + * selected on these payments (in case their payment methods are no longer active). |
|
| 1360 | + * Has the side-effect of updating the template args' payment_methods item |
|
| 1361 | + * |
|
| 1362 | + * @access private |
|
| 1363 | + * @param EE_Payment[] to show on this page |
|
| 1364 | + * @return void |
|
| 1365 | + * @throws EE_Error |
|
| 1366 | + * @throws InvalidArgumentException |
|
| 1367 | + * @throws InvalidDataTypeException |
|
| 1368 | + * @throws InvalidInterfaceException |
|
| 1369 | + * @throws ReflectionException |
|
| 1370 | + */ |
|
| 1371 | + private function _get_payment_methods($payments = array()) |
|
| 1372 | + { |
|
| 1373 | + $payment_methods_of_payments = array(); |
|
| 1374 | + foreach ($payments as $payment) { |
|
| 1375 | + if ($payment instanceof EE_Payment) { |
|
| 1376 | + $payment_methods_of_payments[] = $payment->get('PMD_ID'); |
|
| 1377 | + } |
|
| 1378 | + } |
|
| 1379 | + if ($payment_methods_of_payments) { |
|
| 1380 | + $query_args = array( |
|
| 1381 | + array( |
|
| 1382 | + 'OR*payment_method_for_payment' => array( |
|
| 1383 | + 'PMD_ID' => array('IN', $payment_methods_of_payments), |
|
| 1384 | + 'PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%'), |
|
| 1385 | + ), |
|
| 1386 | + ), |
|
| 1387 | + ); |
|
| 1388 | + } else { |
|
| 1389 | + $query_args = array(array('PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%'))); |
|
| 1390 | + } |
|
| 1391 | + $this->_template_args['payment_methods'] = EEM_Payment_Method::instance()->get_all($query_args); |
|
| 1392 | + } |
|
| 1393 | + |
|
| 1394 | + |
|
| 1395 | + /** |
|
| 1396 | + * txn_attendees_meta_box |
|
| 1397 | + * generates HTML for the Attendees Transaction main meta box |
|
| 1398 | + * |
|
| 1399 | + * @access public |
|
| 1400 | + * @param WP_Post $post |
|
| 1401 | + * @param array $metabox |
|
| 1402 | + * @return void |
|
| 1403 | + * @throws DomainException |
|
| 1404 | + * @throws EE_Error |
|
| 1405 | + */ |
|
| 1406 | + public function txn_attendees_meta_box($post, $metabox = array('args' => array())) |
|
| 1407 | + { |
|
| 1408 | + |
|
| 1409 | + /** @noinspection NonSecureExtractUsageInspection */ |
|
| 1410 | + extract($metabox['args']); |
|
| 1411 | + $this->_template_args['post'] = $post; |
|
| 1412 | + $this->_template_args['event_attendees'] = array(); |
|
| 1413 | + // process items in cart |
|
| 1414 | + $line_items = $this->_transaction->get_many_related( |
|
| 1415 | + 'Line_Item', |
|
| 1416 | + array(array('LIN_type' => 'line-item')) |
|
| 1417 | + ); |
|
| 1418 | + if (! empty($line_items)) { |
|
| 1419 | + foreach ($line_items as $item) { |
|
| 1420 | + if ($item instanceof EE_Line_Item) { |
|
| 1421 | + switch ($item->OBJ_type()) { |
|
| 1422 | + case 'Event': |
|
| 1423 | + break; |
|
| 1424 | + case 'Ticket': |
|
| 1425 | + $ticket = $item->ticket(); |
|
| 1426 | + // right now we're only handling tickets here. |
|
| 1427 | + // Cause its expected that only tickets will have attendees right? |
|
| 1428 | + if (! $ticket instanceof EE_Ticket) { |
|
| 1429 | + break; |
|
| 1430 | + } |
|
| 1431 | + try { |
|
| 1432 | + $event_name = $ticket->get_event_name(); |
|
| 1433 | + } catch (Exception $e) { |
|
| 1434 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 1435 | + $event_name = esc_html__('Unknown Event', 'event_espresso'); |
|
| 1436 | + } |
|
| 1437 | + $event_name .= ' - ' . $item->get('LIN_name'); |
|
| 1438 | + $ticket_price = EEH_Template::format_currency($item->get('LIN_unit_price')); |
|
| 1439 | + // now get all of the registrations for this transaction that use this ticket |
|
| 1440 | + $registrations = $ticket->get_many_related( |
|
| 1441 | + 'Registration', |
|
| 1442 | + array(array('TXN_ID' => $this->_transaction->ID())) |
|
| 1443 | + ); |
|
| 1444 | + foreach ($registrations as $registration) { |
|
| 1445 | + if (! $registration instanceof EE_Registration) { |
|
| 1446 | + break; |
|
| 1447 | + } |
|
| 1448 | + $this->_template_args['event_attendees'][ $registration->ID() ]['STS_ID'] |
|
| 1449 | + = $registration->status_ID(); |
|
| 1450 | + $this->_template_args['event_attendees'][ $registration->ID() ]['att_num'] |
|
| 1451 | + = $registration->count(); |
|
| 1452 | + $this->_template_args['event_attendees'][ $registration->ID() ]['event_ticket_name'] |
|
| 1453 | + = $event_name; |
|
| 1454 | + $this->_template_args['event_attendees'][ $registration->ID() ]['ticket_price'] |
|
| 1455 | + = $ticket_price; |
|
| 1456 | + // attendee info |
|
| 1457 | + $attendee = $registration->get_first_related('Attendee'); |
|
| 1458 | + if ($attendee instanceof EE_Attendee) { |
|
| 1459 | + $this->_template_args['event_attendees'][ $registration->ID() ]['att_id'] |
|
| 1460 | + = $attendee->ID(); |
|
| 1461 | + $this->_template_args['event_attendees'][ $registration->ID() ]['attendee'] |
|
| 1462 | + = $attendee->full_name(); |
|
| 1463 | + $this->_template_args['event_attendees'][ $registration->ID() ]['email'] |
|
| 1464 | + = '<a href="mailto:' . $attendee->email() . '?subject=' . $event_name |
|
| 1465 | + . esc_html__( |
|
| 1466 | + ' Event', |
|
| 1467 | + 'event_espresso' |
|
| 1468 | + ) |
|
| 1469 | + . '">' . $attendee->email() . '</a>'; |
|
| 1470 | + $this->_template_args['event_attendees'][ $registration->ID() ]['address'] |
|
| 1471 | + = EEH_Address::format($attendee, 'inline', false, false); |
|
| 1472 | + } else { |
|
| 1473 | + $this->_template_args['event_attendees'][ $registration->ID() ]['att_id'] = ''; |
|
| 1474 | + $this->_template_args['event_attendees'][ $registration->ID() ]['attendee'] = ''; |
|
| 1475 | + $this->_template_args['event_attendees'][ $registration->ID() ]['email'] = ''; |
|
| 1476 | + $this->_template_args['event_attendees'][ $registration->ID() ]['address'] = ''; |
|
| 1477 | + } |
|
| 1478 | + } |
|
| 1479 | + break; |
|
| 1480 | + } |
|
| 1481 | + } |
|
| 1482 | + } |
|
| 1483 | + |
|
| 1484 | + $this->_template_args['transaction_form_url'] = add_query_arg( |
|
| 1485 | + array( |
|
| 1486 | + 'action' => 'edit_transaction', |
|
| 1487 | + 'process' => 'attendees', |
|
| 1488 | + ), |
|
| 1489 | + TXN_ADMIN_URL |
|
| 1490 | + ); |
|
| 1491 | + echo EEH_Template::display_template( |
|
| 1492 | + TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_attendees.template.php', |
|
| 1493 | + $this->_template_args, |
|
| 1494 | + true |
|
| 1495 | + ); |
|
| 1496 | + } else { |
|
| 1497 | + echo sprintf( |
|
| 1498 | + esc_html__( |
|
| 1499 | + '%1$sFor some reason, there are no attendees registered for this transaction. Likely the registration was abandoned in process.%2$s', |
|
| 1500 | + 'event_espresso' |
|
| 1501 | + ), |
|
| 1502 | + '<p class="important-notice">', |
|
| 1503 | + '</p>' |
|
| 1504 | + ); |
|
| 1505 | + } |
|
| 1506 | + } |
|
| 1507 | + |
|
| 1508 | + |
|
| 1509 | + /** |
|
| 1510 | + * txn_registrant_side_meta_box |
|
| 1511 | + * generates HTML for the Edit Transaction side meta box |
|
| 1512 | + * |
|
| 1513 | + * @access public |
|
| 1514 | + * @return void |
|
| 1515 | + * @throws DomainException |
|
| 1516 | + * @throws EE_Error |
|
| 1517 | + * @throws InvalidArgumentException |
|
| 1518 | + * @throws InvalidDataTypeException |
|
| 1519 | + * @throws InvalidInterfaceException |
|
| 1520 | + * @throws ReflectionException |
|
| 1521 | + */ |
|
| 1522 | + public function txn_registrant_side_meta_box() |
|
| 1523 | + { |
|
| 1524 | + $primary_att = $this->_transaction->primary_registration() instanceof EE_Registration |
|
| 1525 | + ? $this->_transaction->primary_registration()->get_first_related('Attendee') |
|
| 1526 | + : null; |
|
| 1527 | + if (! $primary_att instanceof EE_Attendee) { |
|
| 1528 | + $this->_template_args['no_attendee_message'] = esc_html__( |
|
| 1529 | + 'There is no attached contact for this transaction. The transaction either failed due to an error or was abandoned.', |
|
| 1530 | + 'event_espresso' |
|
| 1531 | + ); |
|
| 1532 | + $primary_att = EEM_Attendee::instance()->create_default_object(); |
|
| 1533 | + } |
|
| 1534 | + $this->_template_args['ATT_ID'] = $primary_att->ID(); |
|
| 1535 | + $this->_template_args['prime_reg_fname'] = $primary_att->fname(); |
|
| 1536 | + $this->_template_args['prime_reg_lname'] = $primary_att->lname(); |
|
| 1537 | + $this->_template_args['prime_reg_email'] = $primary_att->email(); |
|
| 1538 | + $this->_template_args['prime_reg_phone'] = $primary_att->phone(); |
|
| 1539 | + $this->_template_args['edit_attendee_url'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 1540 | + array( |
|
| 1541 | + 'action' => 'edit_attendee', |
|
| 1542 | + 'post' => $primary_att->ID(), |
|
| 1543 | + ), |
|
| 1544 | + REG_ADMIN_URL |
|
| 1545 | + ); |
|
| 1546 | + // get formatted address for registrant |
|
| 1547 | + $this->_template_args['formatted_address'] = EEH_Address::format($primary_att); |
|
| 1548 | + echo EEH_Template::display_template( |
|
| 1549 | + TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_registrant.template.php', |
|
| 1550 | + $this->_template_args, |
|
| 1551 | + true |
|
| 1552 | + ); |
|
| 1553 | + } |
|
| 1554 | + |
|
| 1555 | + |
|
| 1556 | + /** |
|
| 1557 | + * txn_billing_info_side_meta_box |
|
| 1558 | + * generates HTML for the Edit Transaction side meta box |
|
| 1559 | + * |
|
| 1560 | + * @access public |
|
| 1561 | + * @return void |
|
| 1562 | + * @throws DomainException |
|
| 1563 | + * @throws EE_Error |
|
| 1564 | + */ |
|
| 1565 | + public function txn_billing_info_side_meta_box() |
|
| 1566 | + { |
|
| 1567 | + |
|
| 1568 | + $this->_template_args['billing_form'] = $this->_transaction->billing_info(); |
|
| 1569 | + $this->_template_args['billing_form_url'] = add_query_arg( |
|
| 1570 | + array('action' => 'edit_transaction', 'process' => 'billing'), |
|
| 1571 | + TXN_ADMIN_URL |
|
| 1572 | + ); |
|
| 1573 | + |
|
| 1574 | + $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_billing_info.template.php'; |
|
| 1575 | + echo EEH_Template::display_template($template_path, $this->_template_args, true);/**/ |
|
| 1576 | + } |
|
| 1577 | + |
|
| 1578 | + |
|
| 1579 | + /** |
|
| 1580 | + * apply_payments_or_refunds |
|
| 1581 | + * registers a payment or refund made towards a transaction |
|
| 1582 | + * |
|
| 1583 | + * @access public |
|
| 1584 | + * @return void |
|
| 1585 | + * @throws EE_Error |
|
| 1586 | + * @throws InvalidArgumentException |
|
| 1587 | + * @throws ReflectionException |
|
| 1588 | + * @throws RuntimeException |
|
| 1589 | + * @throws InvalidDataTypeException |
|
| 1590 | + * @throws InvalidInterfaceException |
|
| 1591 | + */ |
|
| 1592 | + public function apply_payments_or_refunds() |
|
| 1593 | + { |
|
| 1594 | + $json_response_data = array('return_data' => false); |
|
| 1595 | + $valid_data = $this->_validate_payment_request_data(); |
|
| 1596 | + $has_access = EE_Registry::instance()->CAP->current_user_can( |
|
| 1597 | + 'ee_edit_payments', |
|
| 1598 | + 'apply_payment_or_refund_from_registration_details' |
|
| 1599 | + ); |
|
| 1600 | + if (! empty($valid_data) && $has_access) { |
|
| 1601 | + $PAY_ID = $valid_data['PAY_ID']; |
|
| 1602 | + // save the new payment |
|
| 1603 | + $payment = $this->_create_payment_from_request_data($valid_data); |
|
| 1604 | + // get the TXN for this payment |
|
| 1605 | + $transaction = $payment->transaction(); |
|
| 1606 | + // verify transaction |
|
| 1607 | + if ($transaction instanceof EE_Transaction) { |
|
| 1608 | + // calculate_total_payments_and_update_status |
|
| 1609 | + $this->_process_transaction_payments($transaction); |
|
| 1610 | + $REG_IDs = $this->_get_REG_IDs_to_apply_payment_to($payment); |
|
| 1611 | + $this->_remove_existing_registration_payments($payment, $PAY_ID); |
|
| 1612 | + // apply payment to registrations (if applicable) |
|
| 1613 | + if (! empty($REG_IDs)) { |
|
| 1614 | + $this->_update_registration_payments($transaction, $payment, $REG_IDs); |
|
| 1615 | + $this->_maybe_send_notifications(); |
|
| 1616 | + // now process status changes for the same registrations |
|
| 1617 | + $this->_process_registration_status_change($transaction, $REG_IDs); |
|
| 1618 | + } |
|
| 1619 | + $this->_maybe_send_notifications($payment); |
|
| 1620 | + // prepare to render page |
|
| 1621 | + $json_response_data['return_data'] = $this->_build_payment_json_response($payment, $REG_IDs); |
|
| 1622 | + do_action( |
|
| 1623 | + 'AHEE__Transactions_Admin_Page__apply_payments_or_refund__after_recording', |
|
| 1624 | + $transaction, |
|
| 1625 | + $payment |
|
| 1626 | + ); |
|
| 1627 | + } else { |
|
| 1628 | + EE_Error::add_error( |
|
| 1629 | + esc_html__( |
|
| 1630 | + 'A valid Transaction for this payment could not be retrieved.', |
|
| 1631 | + 'event_espresso' |
|
| 1632 | + ), |
|
| 1633 | + __FILE__, |
|
| 1634 | + __FUNCTION__, |
|
| 1635 | + __LINE__ |
|
| 1636 | + ); |
|
| 1637 | + } |
|
| 1638 | + } else { |
|
| 1639 | + if ($has_access) { |
|
| 1640 | + EE_Error::add_error( |
|
| 1641 | + esc_html__( |
|
| 1642 | + 'The payment form data could not be processed. Please try again.', |
|
| 1643 | + 'event_espresso' |
|
| 1644 | + ), |
|
| 1645 | + __FILE__, |
|
| 1646 | + __FUNCTION__, |
|
| 1647 | + __LINE__ |
|
| 1648 | + ); |
|
| 1649 | + } else { |
|
| 1650 | + EE_Error::add_error( |
|
| 1651 | + esc_html__( |
|
| 1652 | + 'You do not have access to apply payments or refunds to a registration.', |
|
| 1653 | + 'event_espresso' |
|
| 1654 | + ), |
|
| 1655 | + __FILE__, |
|
| 1656 | + __FUNCTION__, |
|
| 1657 | + __LINE__ |
|
| 1658 | + ); |
|
| 1659 | + } |
|
| 1660 | + } |
|
| 1661 | + $notices = EE_Error::get_notices( |
|
| 1662 | + false, |
|
| 1663 | + false, |
|
| 1664 | + false |
|
| 1665 | + ); |
|
| 1666 | + $this->_template_args = array( |
|
| 1667 | + 'data' => $json_response_data, |
|
| 1668 | + 'error' => $notices['errors'], |
|
| 1669 | + 'success' => $notices['success'], |
|
| 1670 | + ); |
|
| 1671 | + $this->_return_json(); |
|
| 1672 | + } |
|
| 1673 | + |
|
| 1674 | + |
|
| 1675 | + /** |
|
| 1676 | + * _validate_payment_request_data |
|
| 1677 | + * |
|
| 1678 | + * @return array |
|
| 1679 | + * @throws EE_Error |
|
| 1680 | + */ |
|
| 1681 | + protected function _validate_payment_request_data() |
|
| 1682 | + { |
|
| 1683 | + if (! isset($this->_req_data['txn_admin_payment'])) { |
|
| 1684 | + return false; |
|
| 1685 | + } |
|
| 1686 | + $payment_form = $this->_generate_payment_form_section(); |
|
| 1687 | + try { |
|
| 1688 | + if ($payment_form->was_submitted()) { |
|
| 1689 | + $payment_form->receive_form_submission(); |
|
| 1690 | + if (! $payment_form->is_valid()) { |
|
| 1691 | + $submission_error_messages = array(); |
|
| 1692 | + foreach ($payment_form->get_validation_errors_accumulated() as $validation_error) { |
|
| 1693 | + if ($validation_error instanceof EE_Validation_Error) { |
|
| 1694 | + $submission_error_messages[] = sprintf( |
|
| 1695 | + _x('%s : %s', 'Form Section Name : Form Validation Error', 'event_espresso'), |
|
| 1696 | + $validation_error->get_form_section()->html_label_text(), |
|
| 1697 | + $validation_error->getMessage() |
|
| 1698 | + ); |
|
| 1699 | + } |
|
| 1700 | + } |
|
| 1701 | + EE_Error::add_error( |
|
| 1702 | + implode('<br />', $submission_error_messages), |
|
| 1703 | + __FILE__, |
|
| 1704 | + __FUNCTION__, |
|
| 1705 | + __LINE__ |
|
| 1706 | + ); |
|
| 1707 | + |
|
| 1708 | + return array(); |
|
| 1709 | + } |
|
| 1710 | + } |
|
| 1711 | + } catch (EE_Error $e) { |
|
| 1712 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 1713 | + |
|
| 1714 | + return array(); |
|
| 1715 | + } |
|
| 1716 | + |
|
| 1717 | + return $payment_form->valid_data(); |
|
| 1718 | + } |
|
| 1719 | + |
|
| 1720 | + |
|
| 1721 | + /** |
|
| 1722 | + * _generate_payment_form_section |
|
| 1723 | + * |
|
| 1724 | + * @return EE_Form_Section_Proper |
|
| 1725 | + * @throws EE_Error |
|
| 1726 | + */ |
|
| 1727 | + protected function _generate_payment_form_section() |
|
| 1728 | + { |
|
| 1729 | + return new EE_Form_Section_Proper( |
|
| 1730 | + array( |
|
| 1731 | + 'name' => 'txn_admin_payment', |
|
| 1732 | + 'subsections' => array( |
|
| 1733 | + 'PAY_ID' => new EE_Text_Input( |
|
| 1734 | + array( |
|
| 1735 | + 'default' => 0, |
|
| 1736 | + 'required' => false, |
|
| 1737 | + 'html_label_text' => esc_html__('Payment ID', 'event_espresso'), |
|
| 1738 | + 'validation_strategies' => array(new EE_Int_Normalization()), |
|
| 1739 | + ) |
|
| 1740 | + ), |
|
| 1741 | + 'TXN_ID' => new EE_Text_Input( |
|
| 1742 | + array( |
|
| 1743 | + 'default' => 0, |
|
| 1744 | + 'required' => true, |
|
| 1745 | + 'html_label_text' => esc_html__('Transaction ID', 'event_espresso'), |
|
| 1746 | + 'validation_strategies' => array(new EE_Int_Normalization()), |
|
| 1747 | + ) |
|
| 1748 | + ), |
|
| 1749 | + 'type' => new EE_Text_Input( |
|
| 1750 | + array( |
|
| 1751 | + 'default' => 1, |
|
| 1752 | + 'required' => true, |
|
| 1753 | + 'html_label_text' => esc_html__('Payment or Refund', 'event_espresso'), |
|
| 1754 | + 'validation_strategies' => array(new EE_Int_Normalization()), |
|
| 1755 | + ) |
|
| 1756 | + ), |
|
| 1757 | + 'amount' => new EE_Text_Input( |
|
| 1758 | + array( |
|
| 1759 | + 'default' => 0, |
|
| 1760 | + 'required' => true, |
|
| 1761 | + 'html_label_text' => esc_html__('Payment amount', 'event_espresso'), |
|
| 1762 | + 'validation_strategies' => array(new EE_Float_Normalization()), |
|
| 1763 | + ) |
|
| 1764 | + ), |
|
| 1765 | + 'status' => new EE_Text_Input( |
|
| 1766 | + array( |
|
| 1767 | + 'default' => EEM_Payment::status_id_approved, |
|
| 1768 | + 'required' => true, |
|
| 1769 | + 'html_label_text' => esc_html__('Payment status', 'event_espresso'), |
|
| 1770 | + ) |
|
| 1771 | + ), |
|
| 1772 | + 'PMD_ID' => new EE_Text_Input( |
|
| 1773 | + array( |
|
| 1774 | + 'default' => 2, |
|
| 1775 | + 'required' => true, |
|
| 1776 | + 'html_label_text' => esc_html__('Payment Method', 'event_espresso'), |
|
| 1777 | + 'validation_strategies' => array(new EE_Int_Normalization()), |
|
| 1778 | + ) |
|
| 1779 | + ), |
|
| 1780 | + 'date' => new EE_Text_Input( |
|
| 1781 | + array( |
|
| 1782 | + 'default' => time(), |
|
| 1783 | + 'required' => true, |
|
| 1784 | + 'html_label_text' => esc_html__('Payment date', 'event_espresso'), |
|
| 1785 | + ) |
|
| 1786 | + ), |
|
| 1787 | + 'txn_id_chq_nmbr' => new EE_Text_Input( |
|
| 1788 | + array( |
|
| 1789 | + 'default' => '', |
|
| 1790 | + 'required' => false, |
|
| 1791 | + 'html_label_text' => esc_html__('Transaction or Cheque Number', 'event_espresso'), |
|
| 1792 | + 'validation_strategies' => array( |
|
| 1793 | + new EE_Max_Length_Validation_Strategy( |
|
| 1794 | + esc_html__('Input too long', 'event_espresso'), |
|
| 1795 | + 100 |
|
| 1796 | + ), |
|
| 1797 | + ), |
|
| 1798 | + ) |
|
| 1799 | + ), |
|
| 1800 | + 'po_number' => new EE_Text_Input( |
|
| 1801 | + array( |
|
| 1802 | + 'default' => '', |
|
| 1803 | + 'required' => false, |
|
| 1804 | + 'html_label_text' => esc_html__('Purchase Order Number', 'event_espresso'), |
|
| 1805 | + 'validation_strategies' => array( |
|
| 1806 | + new EE_Max_Length_Validation_Strategy( |
|
| 1807 | + esc_html__('Input too long', 'event_espresso'), |
|
| 1808 | + 100 |
|
| 1809 | + ), |
|
| 1810 | + ), |
|
| 1811 | + ) |
|
| 1812 | + ), |
|
| 1813 | + 'accounting' => new EE_Text_Input( |
|
| 1814 | + array( |
|
| 1815 | + 'default' => '', |
|
| 1816 | + 'required' => false, |
|
| 1817 | + 'html_label_text' => esc_html__('Extra Field for Accounting', 'event_espresso'), |
|
| 1818 | + 'validation_strategies' => array( |
|
| 1819 | + new EE_Max_Length_Validation_Strategy( |
|
| 1820 | + esc_html__('Input too long', 'event_espresso'), |
|
| 1821 | + 100 |
|
| 1822 | + ), |
|
| 1823 | + ), |
|
| 1824 | + ) |
|
| 1825 | + ), |
|
| 1826 | + ), |
|
| 1827 | + ) |
|
| 1828 | + ); |
|
| 1829 | + } |
|
| 1830 | + |
|
| 1831 | + |
|
| 1832 | + /** |
|
| 1833 | + * _create_payment_from_request_data |
|
| 1834 | + * |
|
| 1835 | + * @param array $valid_data |
|
| 1836 | + * @return EE_Payment |
|
| 1837 | + * @throws EE_Error |
|
| 1838 | + */ |
|
| 1839 | + protected function _create_payment_from_request_data($valid_data) |
|
| 1840 | + { |
|
| 1841 | + $PAY_ID = $valid_data['PAY_ID']; |
|
| 1842 | + // get payment amount |
|
| 1843 | + $amount = $valid_data['amount'] ? abs($valid_data['amount']) : 0; |
|
| 1844 | + // payments have a type value of 1 and refunds have a type value of -1 |
|
| 1845 | + // so multiplying amount by type will give a positive value for payments, and negative values for refunds |
|
| 1846 | + $amount = $valid_data['type'] < 0 ? $amount * -1 : $amount; |
|
| 1847 | + // for some reason the date string coming in has extra spaces between the date and time. This fixes that. |
|
| 1848 | + $date = $valid_data['date'] |
|
| 1849 | + ? preg_replace('/\s+/', ' ', $valid_data['date']) |
|
| 1850 | + : date('Y-m-d g:i a', current_time('timestamp')); |
|
| 1851 | + $payment = EE_Payment::new_instance( |
|
| 1852 | + array( |
|
| 1853 | + 'TXN_ID' => $valid_data['TXN_ID'], |
|
| 1854 | + 'STS_ID' => $valid_data['status'], |
|
| 1855 | + 'PAY_timestamp' => $date, |
|
| 1856 | + 'PAY_source' => EEM_Payment_Method::scope_admin, |
|
| 1857 | + 'PMD_ID' => $valid_data['PMD_ID'], |
|
| 1858 | + 'PAY_amount' => $amount, |
|
| 1859 | + 'PAY_txn_id_chq_nmbr' => $valid_data['txn_id_chq_nmbr'], |
|
| 1860 | + 'PAY_po_number' => $valid_data['po_number'], |
|
| 1861 | + 'PAY_extra_accntng' => $valid_data['accounting'], |
|
| 1862 | + 'PAY_details' => $valid_data, |
|
| 1863 | + 'PAY_ID' => $PAY_ID, |
|
| 1864 | + ), |
|
| 1865 | + '', |
|
| 1866 | + array('Y-m-d', 'g:i a') |
|
| 1867 | + ); |
|
| 1868 | + |
|
| 1869 | + if (! $payment->save()) { |
|
| 1870 | + EE_Error::add_error( |
|
| 1871 | + sprintf( |
|
| 1872 | + esc_html__('Payment %1$d has not been successfully saved to the database.', 'event_espresso'), |
|
| 1873 | + $payment->ID() |
|
| 1874 | + ), |
|
| 1875 | + __FILE__, |
|
| 1876 | + __FUNCTION__, |
|
| 1877 | + __LINE__ |
|
| 1878 | + ); |
|
| 1879 | + } |
|
| 1880 | + |
|
| 1881 | + return $payment; |
|
| 1882 | + } |
|
| 1883 | + |
|
| 1884 | + |
|
| 1885 | + /** |
|
| 1886 | + * _process_transaction_payments |
|
| 1887 | + * |
|
| 1888 | + * @param \EE_Transaction $transaction |
|
| 1889 | + * @return void |
|
| 1890 | + * @throws EE_Error |
|
| 1891 | + * @throws InvalidArgumentException |
|
| 1892 | + * @throws ReflectionException |
|
| 1893 | + * @throws InvalidDataTypeException |
|
| 1894 | + * @throws InvalidInterfaceException |
|
| 1895 | + */ |
|
| 1896 | + protected function _process_transaction_payments(EE_Transaction $transaction) |
|
| 1897 | + { |
|
| 1898 | + /** @type EE_Transaction_Payments $transaction_payments */ |
|
| 1899 | + $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments'); |
|
| 1900 | + // update the transaction with this payment |
|
| 1901 | + if ($transaction_payments->calculate_total_payments_and_update_status($transaction)) { |
|
| 1902 | + EE_Error::add_success( |
|
| 1903 | + esc_html__( |
|
| 1904 | + 'The payment has been processed successfully.', |
|
| 1905 | + 'event_espresso' |
|
| 1906 | + ), |
|
| 1907 | + __FILE__, |
|
| 1908 | + __FUNCTION__, |
|
| 1909 | + __LINE__ |
|
| 1910 | + ); |
|
| 1911 | + } else { |
|
| 1912 | + EE_Error::add_error( |
|
| 1913 | + esc_html__( |
|
| 1914 | + 'The payment was processed successfully but the amount paid for the transaction was not updated.', |
|
| 1915 | + 'event_espresso' |
|
| 1916 | + ), |
|
| 1917 | + __FILE__, |
|
| 1918 | + __FUNCTION__, |
|
| 1919 | + __LINE__ |
|
| 1920 | + ); |
|
| 1921 | + } |
|
| 1922 | + } |
|
| 1923 | + |
|
| 1924 | + |
|
| 1925 | + /** |
|
| 1926 | + * _get_REG_IDs_to_apply_payment_to |
|
| 1927 | + * returns a list of registration IDs that the payment will apply to |
|
| 1928 | + * |
|
| 1929 | + * @param \EE_Payment $payment |
|
| 1930 | + * @return array |
|
| 1931 | + * @throws EE_Error |
|
| 1932 | + */ |
|
| 1933 | + protected function _get_REG_IDs_to_apply_payment_to(EE_Payment $payment) |
|
| 1934 | + { |
|
| 1935 | + $REG_IDs = array(); |
|
| 1936 | + // grab array of IDs for specific registrations to apply changes to |
|
| 1937 | + if (isset($this->_req_data['txn_admin_payment']['registrations'])) { |
|
| 1938 | + $REG_IDs = (array) $this->_req_data['txn_admin_payment']['registrations']; |
|
| 1939 | + } |
|
| 1940 | + // nothing specified ? then get all reg IDs |
|
| 1941 | + if (empty($REG_IDs)) { |
|
| 1942 | + $registrations = $payment->transaction()->registrations(); |
|
| 1943 | + $REG_IDs = ! empty($registrations) |
|
| 1944 | + ? array_keys($registrations) |
|
| 1945 | + : $this->_get_existing_reg_payment_REG_IDs($payment); |
|
| 1946 | + } |
|
| 1947 | + |
|
| 1948 | + // ensure that REG_IDs are integers and NOT strings |
|
| 1949 | + return array_map('intval', $REG_IDs); |
|
| 1950 | + } |
|
| 1951 | + |
|
| 1952 | + |
|
| 1953 | + /** |
|
| 1954 | + * @return array |
|
| 1955 | + */ |
|
| 1956 | + public function existing_reg_payment_REG_IDs() |
|
| 1957 | + { |
|
| 1958 | + return $this->_existing_reg_payment_REG_IDs; |
|
| 1959 | + } |
|
| 1960 | + |
|
| 1961 | + |
|
| 1962 | + /** |
|
| 1963 | + * @param array $existing_reg_payment_REG_IDs |
|
| 1964 | + */ |
|
| 1965 | + public function set_existing_reg_payment_REG_IDs($existing_reg_payment_REG_IDs = null) |
|
| 1966 | + { |
|
| 1967 | + $this->_existing_reg_payment_REG_IDs = $existing_reg_payment_REG_IDs; |
|
| 1968 | + } |
|
| 1969 | + |
|
| 1970 | + |
|
| 1971 | + /** |
|
| 1972 | + * _get_existing_reg_payment_REG_IDs |
|
| 1973 | + * returns a list of registration IDs that the payment is currently related to |
|
| 1974 | + * as recorded in the database |
|
| 1975 | + * |
|
| 1976 | + * @param \EE_Payment $payment |
|
| 1977 | + * @return array |
|
| 1978 | + * @throws EE_Error |
|
| 1979 | + */ |
|
| 1980 | + protected function _get_existing_reg_payment_REG_IDs(EE_Payment $payment) |
|
| 1981 | + { |
|
| 1982 | + if ($this->existing_reg_payment_REG_IDs() === null) { |
|
| 1983 | + // let's get any existing reg payment records for this payment |
|
| 1984 | + $existing_reg_payment_REG_IDs = $payment->get_many_related('Registration'); |
|
| 1985 | + // but we only want the REG IDs, so grab the array keys |
|
| 1986 | + $existing_reg_payment_REG_IDs = ! empty($existing_reg_payment_REG_IDs) |
|
| 1987 | + ? array_keys($existing_reg_payment_REG_IDs) |
|
| 1988 | + : array(); |
|
| 1989 | + $this->set_existing_reg_payment_REG_IDs($existing_reg_payment_REG_IDs); |
|
| 1990 | + } |
|
| 1991 | + |
|
| 1992 | + return $this->existing_reg_payment_REG_IDs(); |
|
| 1993 | + } |
|
| 1994 | + |
|
| 1995 | + |
|
| 1996 | + /** |
|
| 1997 | + * _remove_existing_registration_payments |
|
| 1998 | + * this calculates the difference between existing relations |
|
| 1999 | + * to the supplied payment and the new list registration IDs, |
|
| 2000 | + * removes any related registrations that no longer apply, |
|
| 2001 | + * and then updates the registration paid fields |
|
| 2002 | + * |
|
| 2003 | + * @param \EE_Payment $payment |
|
| 2004 | + * @param int $PAY_ID |
|
| 2005 | + * @return bool; |
|
| 2006 | + * @throws EE_Error |
|
| 2007 | + * @throws InvalidArgumentException |
|
| 2008 | + * @throws ReflectionException |
|
| 2009 | + * @throws InvalidDataTypeException |
|
| 2010 | + * @throws InvalidInterfaceException |
|
| 2011 | + */ |
|
| 2012 | + protected function _remove_existing_registration_payments(EE_Payment $payment, $PAY_ID = 0) |
|
| 2013 | + { |
|
| 2014 | + // newly created payments will have nothing recorded for $PAY_ID |
|
| 2015 | + if ($PAY_ID == 0) { |
|
| 2016 | + return false; |
|
| 2017 | + } |
|
| 2018 | + $existing_reg_payment_REG_IDs = $this->_get_existing_reg_payment_REG_IDs($payment); |
|
| 2019 | + if (empty($existing_reg_payment_REG_IDs)) { |
|
| 2020 | + return false; |
|
| 2021 | + } |
|
| 2022 | + /** @type EE_Transaction_Payments $transaction_payments */ |
|
| 2023 | + $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments'); |
|
| 2024 | + |
|
| 2025 | + return $transaction_payments->delete_registration_payments_and_update_registrations( |
|
| 2026 | + $payment, |
|
| 2027 | + array( |
|
| 2028 | + array( |
|
| 2029 | + 'PAY_ID' => $payment->ID(), |
|
| 2030 | + 'REG_ID' => array('IN', $existing_reg_payment_REG_IDs), |
|
| 2031 | + ), |
|
| 2032 | + ) |
|
| 2033 | + ); |
|
| 2034 | + } |
|
| 2035 | + |
|
| 2036 | + |
|
| 2037 | + /** |
|
| 2038 | + * _update_registration_payments |
|
| 2039 | + * this applies the payments to the selected registrations |
|
| 2040 | + * but only if they have not already been paid for |
|
| 2041 | + * |
|
| 2042 | + * @param EE_Transaction $transaction |
|
| 2043 | + * @param \EE_Payment $payment |
|
| 2044 | + * @param array $REG_IDs |
|
| 2045 | + * @return void |
|
| 2046 | + * @throws EE_Error |
|
| 2047 | + * @throws InvalidArgumentException |
|
| 2048 | + * @throws ReflectionException |
|
| 2049 | + * @throws RuntimeException |
|
| 2050 | + * @throws InvalidDataTypeException |
|
| 2051 | + * @throws InvalidInterfaceException |
|
| 2052 | + */ |
|
| 2053 | + protected function _update_registration_payments( |
|
| 2054 | + EE_Transaction $transaction, |
|
| 2055 | + EE_Payment $payment, |
|
| 2056 | + $REG_IDs = array() |
|
| 2057 | + ) { |
|
| 2058 | + // we can pass our own custom set of registrations to EE_Payment_Processor::process_registration_payments() |
|
| 2059 | + // so let's do that using our set of REG_IDs from the form |
|
| 2060 | + $registration_query_where_params = array( |
|
| 2061 | + 'REG_ID' => array('IN', $REG_IDs), |
|
| 2062 | + ); |
|
| 2063 | + // but add in some conditions regarding payment, |
|
| 2064 | + // so that we don't apply payments to registrations that are free or have already been paid for |
|
| 2065 | + // but ONLY if the payment is NOT a refund ( ie: the payment amount is not negative ) |
|
| 2066 | + if (! $payment->is_a_refund()) { |
|
| 2067 | + $registration_query_where_params['REG_final_price'] = array('!=', 0); |
|
| 2068 | + $registration_query_where_params['REG_final_price*'] = array('!=', 'REG_paid', true); |
|
| 2069 | + } |
|
| 2070 | + $registrations = $transaction->registrations(array($registration_query_where_params)); |
|
| 2071 | + if (! empty($registrations)) { |
|
| 2072 | + /** @type EE_Payment_Processor $payment_processor */ |
|
| 2073 | + $payment_processor = EE_Registry::instance()->load_core('Payment_Processor'); |
|
| 2074 | + $payment_processor->process_registration_payments($transaction, $payment, $registrations); |
|
| 2075 | + } |
|
| 2076 | + } |
|
| 2077 | + |
|
| 2078 | + |
|
| 2079 | + /** |
|
| 2080 | + * _process_registration_status_change |
|
| 2081 | + * This processes requested registration status changes for all the registrations |
|
| 2082 | + * on a given transaction and (optionally) sends out notifications for the changes. |
|
| 2083 | + * |
|
| 2084 | + * @param EE_Transaction $transaction |
|
| 2085 | + * @param array $REG_IDs |
|
| 2086 | + * @return bool |
|
| 2087 | + * @throws EE_Error |
|
| 2088 | + * @throws InvalidArgumentException |
|
| 2089 | + * @throws ReflectionException |
|
| 2090 | + * @throws InvalidDataTypeException |
|
| 2091 | + * @throws InvalidInterfaceException |
|
| 2092 | + */ |
|
| 2093 | + protected function _process_registration_status_change(EE_Transaction $transaction, $REG_IDs = array()) |
|
| 2094 | + { |
|
| 2095 | + // first if there is no change in status then we get out. |
|
| 2096 | + if (! isset($this->_req_data['txn_reg_status_change']['reg_status']) |
|
| 2097 | + || $this->_req_data['txn_reg_status_change']['reg_status'] === 'NAN' |
|
| 2098 | + ) { |
|
| 2099 | + // no error message, no change requested, just nothing to do man. |
|
| 2100 | + return false; |
|
| 2101 | + } |
|
| 2102 | + /** @type EE_Transaction_Processor $transaction_processor */ |
|
| 2103 | + $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor'); |
|
| 2104 | + |
|
| 2105 | + // made it here dude? Oh WOW. K, let's take care of changing the statuses |
|
| 2106 | + return $transaction_processor->manually_update_registration_statuses( |
|
| 2107 | + $transaction, |
|
| 2108 | + sanitize_text_field($this->_req_data['txn_reg_status_change']['reg_status']), |
|
| 2109 | + array(array('REG_ID' => array('IN', $REG_IDs))) |
|
| 2110 | + ); |
|
| 2111 | + } |
|
| 2112 | + |
|
| 2113 | + |
|
| 2114 | + /** |
|
| 2115 | + * _build_payment_json_response |
|
| 2116 | + * |
|
| 2117 | + * @access public |
|
| 2118 | + * @param \EE_Payment $payment |
|
| 2119 | + * @param array $REG_IDs |
|
| 2120 | + * @param bool | null $delete_txn_reg_status_change |
|
| 2121 | + * @return array |
|
| 2122 | + * @throws EE_Error |
|
| 2123 | + * @throws InvalidArgumentException |
|
| 2124 | + * @throws InvalidDataTypeException |
|
| 2125 | + * @throws InvalidInterfaceException |
|
| 2126 | + * @throws ReflectionException |
|
| 2127 | + */ |
|
| 2128 | + protected function _build_payment_json_response( |
|
| 2129 | + EE_Payment $payment, |
|
| 2130 | + $REG_IDs = array(), |
|
| 2131 | + $delete_txn_reg_status_change = null |
|
| 2132 | + ) { |
|
| 2133 | + // was the payment deleted ? |
|
| 2134 | + if (is_bool($delete_txn_reg_status_change)) { |
|
| 2135 | + return array( |
|
| 2136 | + 'PAY_ID' => $payment->ID(), |
|
| 2137 | + 'amount' => $payment->amount(), |
|
| 2138 | + 'total_paid' => $payment->transaction()->paid(), |
|
| 2139 | + 'txn_status' => $payment->transaction()->status_ID(), |
|
| 2140 | + 'pay_status' => $payment->STS_ID(), |
|
| 2141 | + 'registrations' => $this->_registration_payment_data_array($REG_IDs), |
|
| 2142 | + 'delete_txn_reg_status_change' => $delete_txn_reg_status_change, |
|
| 2143 | + ); |
|
| 2144 | + } else { |
|
| 2145 | + $this->_get_payment_status_array(); |
|
| 2146 | + |
|
| 2147 | + return array( |
|
| 2148 | + 'amount' => $payment->amount(), |
|
| 2149 | + 'total_paid' => $payment->transaction()->paid(), |
|
| 2150 | + 'txn_status' => $payment->transaction()->status_ID(), |
|
| 2151 | + 'pay_status' => $payment->STS_ID(), |
|
| 2152 | + 'PAY_ID' => $payment->ID(), |
|
| 2153 | + 'STS_ID' => $payment->STS_ID(), |
|
| 2154 | + 'status' => self::$_pay_status[ $payment->STS_ID() ], |
|
| 2155 | + 'date' => $payment->timestamp('Y-m-d', 'h:i a'), |
|
| 2156 | + 'method' => strtoupper($payment->source()), |
|
| 2157 | + 'PM_ID' => $payment->payment_method() ? $payment->payment_method()->ID() : 1, |
|
| 2158 | + 'gateway' => $payment->payment_method() |
|
| 2159 | + ? $payment->payment_method()->admin_name() |
|
| 2160 | + : esc_html__("Unknown", 'event_espresso'), |
|
| 2161 | + 'gateway_response' => $payment->gateway_response(), |
|
| 2162 | + 'txn_id_chq_nmbr' => $payment->txn_id_chq_nmbr(), |
|
| 2163 | + 'po_number' => $payment->po_number(), |
|
| 2164 | + 'extra_accntng' => $payment->extra_accntng(), |
|
| 2165 | + 'registrations' => $this->_registration_payment_data_array($REG_IDs), |
|
| 2166 | + ); |
|
| 2167 | + } |
|
| 2168 | + } |
|
| 2169 | + |
|
| 2170 | + |
|
| 2171 | + /** |
|
| 2172 | + * delete_payment |
|
| 2173 | + * delete a payment or refund made towards a transaction |
|
| 2174 | + * |
|
| 2175 | + * @access public |
|
| 2176 | + * @return void |
|
| 2177 | + * @throws EE_Error |
|
| 2178 | + * @throws InvalidArgumentException |
|
| 2179 | + * @throws ReflectionException |
|
| 2180 | + * @throws InvalidDataTypeException |
|
| 2181 | + * @throws InvalidInterfaceException |
|
| 2182 | + */ |
|
| 2183 | + public function delete_payment() |
|
| 2184 | + { |
|
| 2185 | + $json_response_data = array('return_data' => false); |
|
| 2186 | + $PAY_ID = isset($this->_req_data['delete_txn_admin_payment']['PAY_ID']) |
|
| 2187 | + ? absint($this->_req_data['delete_txn_admin_payment']['PAY_ID']) |
|
| 2188 | + : 0; |
|
| 2189 | + $can_delete = EE_Registry::instance()->CAP->current_user_can( |
|
| 2190 | + 'ee_delete_payments', |
|
| 2191 | + 'delete_payment_from_registration_details' |
|
| 2192 | + ); |
|
| 2193 | + if ($PAY_ID && $can_delete) { |
|
| 2194 | + $delete_txn_reg_status_change = isset($this->_req_data['delete_txn_reg_status_change']) |
|
| 2195 | + ? $this->_req_data['delete_txn_reg_status_change'] |
|
| 2196 | + : false; |
|
| 2197 | + $payment = EEM_Payment::instance()->get_one_by_ID($PAY_ID); |
|
| 2198 | + if ($payment instanceof EE_Payment) { |
|
| 2199 | + $REG_IDs = $this->_get_existing_reg_payment_REG_IDs($payment); |
|
| 2200 | + /** @type EE_Transaction_Payments $transaction_payments */ |
|
| 2201 | + $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments'); |
|
| 2202 | + if ($transaction_payments->delete_payment_and_update_transaction($payment)) { |
|
| 2203 | + $json_response_data['return_data'] = $this->_build_payment_json_response( |
|
| 2204 | + $payment, |
|
| 2205 | + $REG_IDs, |
|
| 2206 | + $delete_txn_reg_status_change |
|
| 2207 | + ); |
|
| 2208 | + if ($delete_txn_reg_status_change) { |
|
| 2209 | + $this->_req_data['txn_reg_status_change'] = $delete_txn_reg_status_change; |
|
| 2210 | + // MAKE sure we also add the delete_txn_req_status_change to the |
|
| 2211 | + // $_REQUEST global because that's how messages will be looking for it. |
|
| 2212 | + $_REQUEST['txn_reg_status_change'] = $delete_txn_reg_status_change; |
|
| 2213 | + $this->_maybe_send_notifications(); |
|
| 2214 | + $this->_process_registration_status_change($payment->transaction(), $REG_IDs); |
|
| 2215 | + } |
|
| 2216 | + } |
|
| 2217 | + } else { |
|
| 2218 | + EE_Error::add_error( |
|
| 2219 | + esc_html__('Valid Payment data could not be retrieved from the database.', 'event_espresso'), |
|
| 2220 | + __FILE__, |
|
| 2221 | + __FUNCTION__, |
|
| 2222 | + __LINE__ |
|
| 2223 | + ); |
|
| 2224 | + } |
|
| 2225 | + } else { |
|
| 2226 | + if ($can_delete) { |
|
| 2227 | + EE_Error::add_error( |
|
| 2228 | + esc_html__( |
|
| 2229 | + 'A valid Payment ID was not received, therefore payment form data could not be loaded.', |
|
| 2230 | + 'event_espresso' |
|
| 2231 | + ), |
|
| 2232 | + __FILE__, |
|
| 2233 | + __FUNCTION__, |
|
| 2234 | + __LINE__ |
|
| 2235 | + ); |
|
| 2236 | + } else { |
|
| 2237 | + EE_Error::add_error( |
|
| 2238 | + esc_html__( |
|
| 2239 | + 'You do not have access to delete a payment.', |
|
| 2240 | + 'event_espresso' |
|
| 2241 | + ), |
|
| 2242 | + __FILE__, |
|
| 2243 | + __FUNCTION__, |
|
| 2244 | + __LINE__ |
|
| 2245 | + ); |
|
| 2246 | + } |
|
| 2247 | + } |
|
| 2248 | + $notices = EE_Error::get_notices(false, false, false); |
|
| 2249 | + $this->_template_args = array( |
|
| 2250 | + 'data' => $json_response_data, |
|
| 2251 | + 'success' => $notices['success'], |
|
| 2252 | + 'error' => $notices['errors'], |
|
| 2253 | + 'attention' => $notices['attention'], |
|
| 2254 | + ); |
|
| 2255 | + $this->_return_json(); |
|
| 2256 | + } |
|
| 2257 | + |
|
| 2258 | + |
|
| 2259 | + /** |
|
| 2260 | + * _registration_payment_data_array |
|
| 2261 | + * adds info for 'owing' and 'paid' for each registration to the json response |
|
| 2262 | + * |
|
| 2263 | + * @access protected |
|
| 2264 | + * @param array $REG_IDs |
|
| 2265 | + * @return array |
|
| 2266 | + * @throws EE_Error |
|
| 2267 | + * @throws InvalidArgumentException |
|
| 2268 | + * @throws InvalidDataTypeException |
|
| 2269 | + * @throws InvalidInterfaceException |
|
| 2270 | + * @throws ReflectionException |
|
| 2271 | + */ |
|
| 2272 | + protected function _registration_payment_data_array($REG_IDs) |
|
| 2273 | + { |
|
| 2274 | + $registration_payment_data = array(); |
|
| 2275 | + // if non empty reg_ids lets get an array of registrations and update the values for the apply_payment/refund rows. |
|
| 2276 | + if (! empty($REG_IDs)) { |
|
| 2277 | + $registrations = EEM_Registration::instance()->get_all(array(array('REG_ID' => array('IN', $REG_IDs)))); |
|
| 2278 | + foreach ($registrations as $registration) { |
|
| 2279 | + if ($registration instanceof EE_Registration) { |
|
| 2280 | + $registration_payment_data[ $registration->ID() ] = array( |
|
| 2281 | + 'paid' => $registration->pretty_paid(), |
|
| 2282 | + 'owing' => EEH_Template::format_currency($registration->final_price() - $registration->paid()), |
|
| 2283 | + ); |
|
| 2284 | + } |
|
| 2285 | + } |
|
| 2286 | + } |
|
| 2287 | + |
|
| 2288 | + return $registration_payment_data; |
|
| 2289 | + } |
|
| 2290 | + |
|
| 2291 | + |
|
| 2292 | + /** |
|
| 2293 | + * _maybe_send_notifications |
|
| 2294 | + * determines whether or not the admin has indicated that notifications should be sent. |
|
| 2295 | + * If so, will toggle a filter switch for delivering registration notices. |
|
| 2296 | + * If passed an EE_Payment object, then it will trigger payment notifications instead. |
|
| 2297 | + * |
|
| 2298 | + * @access protected |
|
| 2299 | + * @param \EE_Payment | null $payment |
|
| 2300 | + */ |
|
| 2301 | + protected function _maybe_send_notifications($payment = null) |
|
| 2302 | + { |
|
| 2303 | + switch ($payment instanceof EE_Payment) { |
|
| 2304 | + // payment notifications |
|
| 2305 | + case true: |
|
| 2306 | + if (isset( |
|
| 2307 | + $this->_req_data['txn_payments'], |
|
| 2308 | + $this->_req_data['txn_payments']['send_notifications'] |
|
| 2309 | + ) |
|
| 2310 | + && filter_var($this->_req_data['txn_payments']['send_notifications'], FILTER_VALIDATE_BOOLEAN) |
|
| 2311 | + ) { |
|
| 2312 | + $this->_process_payment_notification($payment); |
|
| 2313 | + } |
|
| 2314 | + break; |
|
| 2315 | + // registration notifications |
|
| 2316 | + case false: |
|
| 2317 | + if (isset( |
|
| 2318 | + $this->_req_data['txn_reg_status_change'], |
|
| 2319 | + $this->_req_data['txn_reg_status_change']['send_notifications'] |
|
| 2320 | + ) |
|
| 2321 | + && filter_var($this->_req_data['txn_reg_status_change']['send_notifications'], FILTER_VALIDATE_BOOLEAN) |
|
| 2322 | + ) { |
|
| 2323 | + add_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications', '__return_true'); |
|
| 2324 | + } |
|
| 2325 | + break; |
|
| 2326 | + } |
|
| 2327 | + } |
|
| 2328 | + |
|
| 2329 | + |
|
| 2330 | + /** |
|
| 2331 | + * _send_payment_reminder |
|
| 2332 | + * generates HTML for the View Transaction Details Admin page |
|
| 2333 | + * |
|
| 2334 | + * @access protected |
|
| 2335 | + * @return void |
|
| 2336 | + * @throws EE_Error |
|
| 2337 | + * @throws InvalidArgumentException |
|
| 2338 | + * @throws InvalidDataTypeException |
|
| 2339 | + * @throws InvalidInterfaceException |
|
| 2340 | + */ |
|
| 2341 | + protected function _send_payment_reminder() |
|
| 2342 | + { |
|
| 2343 | + $TXN_ID = ! empty($this->_req_data['TXN_ID']) ? absint($this->_req_data['TXN_ID']) : false; |
|
| 2344 | + $transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID); |
|
| 2345 | + $query_args = isset($this->_req_data['redirect_to']) ? array( |
|
| 2346 | + 'action' => $this->_req_data['redirect_to'], |
|
| 2347 | + 'TXN_ID' => $this->_req_data['TXN_ID'], |
|
| 2348 | + ) : array(); |
|
| 2349 | + do_action( |
|
| 2350 | + 'AHEE__Transactions_Admin_Page___send_payment_reminder__process_admin_payment_reminder', |
|
| 2351 | + $transaction |
|
| 2352 | + ); |
|
| 2353 | + $this->_redirect_after_action( |
|
| 2354 | + false, |
|
| 2355 | + esc_html__('payment reminder', 'event_espresso'), |
|
| 2356 | + esc_html__('sent', 'event_espresso'), |
|
| 2357 | + $query_args, |
|
| 2358 | + true |
|
| 2359 | + ); |
|
| 2360 | + } |
|
| 2361 | + |
|
| 2362 | + |
|
| 2363 | + /** |
|
| 2364 | + * get_transactions |
|
| 2365 | + * get transactions for given parameters (used by list table) |
|
| 2366 | + * |
|
| 2367 | + * @param int $perpage how many transactions displayed per page |
|
| 2368 | + * @param boolean $count return the count or objects |
|
| 2369 | + * @param string $view |
|
| 2370 | + * @return mixed int = count || array of transaction objects |
|
| 2371 | + * @throws EE_Error |
|
| 2372 | + * @throws InvalidArgumentException |
|
| 2373 | + * @throws InvalidDataTypeException |
|
| 2374 | + * @throws InvalidInterfaceException |
|
| 2375 | + */ |
|
| 2376 | + public function get_transactions($perpage, $count = false, $view = '') |
|
| 2377 | + { |
|
| 2378 | + |
|
| 2379 | + $TXN = EEM_Transaction::instance(); |
|
| 2380 | + |
|
| 2381 | + $start_date = isset($this->_req_data['txn-filter-start-date']) |
|
| 2382 | + ? wp_strip_all_tags($this->_req_data['txn-filter-start-date']) |
|
| 2383 | + : date( |
|
| 2384 | + 'm/d/Y', |
|
| 2385 | + strtotime('-10 year') |
|
| 2386 | + ); |
|
| 2387 | + $end_date = isset($this->_req_data['txn-filter-end-date']) |
|
| 2388 | + ? wp_strip_all_tags($this->_req_data['txn-filter-end-date']) |
|
| 2389 | + : date('m/d/Y'); |
|
| 2390 | + |
|
| 2391 | + // make sure our timestamps start and end right at the boundaries for each day |
|
| 2392 | + $start_date = date('Y-m-d', strtotime($start_date)) . ' 00:00:00'; |
|
| 2393 | + $end_date = date('Y-m-d', strtotime($end_date)) . ' 23:59:59'; |
|
| 2394 | + |
|
| 2395 | + |
|
| 2396 | + // convert to timestamps |
|
| 2397 | + $start_date = strtotime($start_date); |
|
| 2398 | + $end_date = strtotime($end_date); |
|
| 2399 | + |
|
| 2400 | + // makes sure start date is the lowest value and vice versa |
|
| 2401 | + $start_date = min($start_date, $end_date); |
|
| 2402 | + $end_date = max($start_date, $end_date); |
|
| 2403 | + |
|
| 2404 | + // convert to correct format for query |
|
| 2405 | + $start_date = EEM_Transaction::instance()->convert_datetime_for_query( |
|
| 2406 | + 'TXN_timestamp', |
|
| 2407 | + date('Y-m-d H:i:s', $start_date), |
|
| 2408 | + 'Y-m-d H:i:s' |
|
| 2409 | + ); |
|
| 2410 | + $end_date = EEM_Transaction::instance()->convert_datetime_for_query( |
|
| 2411 | + 'TXN_timestamp', |
|
| 2412 | + date('Y-m-d H:i:s', $end_date), |
|
| 2413 | + 'Y-m-d H:i:s' |
|
| 2414 | + ); |
|
| 2415 | + |
|
| 2416 | + |
|
| 2417 | + // set orderby |
|
| 2418 | + $this->_req_data['orderby'] = ! empty($this->_req_data['orderby']) ? $this->_req_data['orderby'] : ''; |
|
| 2419 | + |
|
| 2420 | + switch ($this->_req_data['orderby']) { |
|
| 2421 | + case 'TXN_ID': |
|
| 2422 | + $orderby = 'TXN_ID'; |
|
| 2423 | + break; |
|
| 2424 | + case 'ATT_fname': |
|
| 2425 | + $orderby = 'Registration.Attendee.ATT_fname'; |
|
| 2426 | + break; |
|
| 2427 | + case 'event_name': |
|
| 2428 | + $orderby = 'Registration.Event.EVT_name'; |
|
| 2429 | + break; |
|
| 2430 | + default: // 'TXN_timestamp' |
|
| 2431 | + $orderby = 'TXN_timestamp'; |
|
| 2432 | + } |
|
| 2433 | + |
|
| 2434 | + $sort = ! empty($this->_req_data['order']) ? $this->_req_data['order'] : 'DESC'; |
|
| 2435 | + $current_page = ! empty($this->_req_data['paged']) ? $this->_req_data['paged'] : 1; |
|
| 2436 | + $per_page = ! empty($perpage) ? $perpage : 10; |
|
| 2437 | + $per_page = ! empty($this->_req_data['perpage']) ? $this->_req_data['perpage'] : $per_page; |
|
| 2438 | + |
|
| 2439 | + $offset = ($current_page - 1) * $per_page; |
|
| 2440 | + $limit = array($offset, $per_page); |
|
| 2441 | + |
|
| 2442 | + $_where = array( |
|
| 2443 | + 'TXN_timestamp' => array('BETWEEN', array($start_date, $end_date)), |
|
| 2444 | + 'Registration.REG_count' => 1, |
|
| 2445 | + ); |
|
| 2446 | + |
|
| 2447 | + if (isset($this->_req_data['EVT_ID'])) { |
|
| 2448 | + $_where['Registration.EVT_ID'] = $this->_req_data['EVT_ID']; |
|
| 2449 | + } |
|
| 2450 | + |
|
| 2451 | + if (isset($this->_req_data['s'])) { |
|
| 2452 | + $search_string = '%' . $this->_req_data['s'] . '%'; |
|
| 2453 | + $_where['OR'] = array( |
|
| 2454 | + 'Registration.Event.EVT_name' => array('LIKE', $search_string), |
|
| 2455 | + 'Registration.Event.EVT_desc' => array('LIKE', $search_string), |
|
| 2456 | + 'Registration.Event.EVT_short_desc' => array('LIKE', $search_string), |
|
| 2457 | + 'Registration.Attendee.ATT_full_name' => array('LIKE', $search_string), |
|
| 2458 | + 'Registration.Attendee.ATT_fname' => array('LIKE', $search_string), |
|
| 2459 | + 'Registration.Attendee.ATT_lname' => array('LIKE', $search_string), |
|
| 2460 | + 'Registration.Attendee.ATT_short_bio' => array('LIKE', $search_string), |
|
| 2461 | + 'Registration.Attendee.ATT_email' => array('LIKE', $search_string), |
|
| 2462 | + 'Registration.Attendee.ATT_address' => array('LIKE', $search_string), |
|
| 2463 | + 'Registration.Attendee.ATT_address2' => array('LIKE', $search_string), |
|
| 2464 | + 'Registration.Attendee.ATT_city' => array('LIKE', $search_string), |
|
| 2465 | + 'Registration.REG_final_price' => array('LIKE', $search_string), |
|
| 2466 | + 'Registration.REG_code' => array('LIKE', $search_string), |
|
| 2467 | + 'Registration.REG_count' => array('LIKE', $search_string), |
|
| 2468 | + 'Registration.REG_group_size' => array('LIKE', $search_string), |
|
| 2469 | + 'Registration.Ticket.TKT_name' => array('LIKE', $search_string), |
|
| 2470 | + 'Registration.Ticket.TKT_description' => array('LIKE', $search_string), |
|
| 2471 | + 'Payment.PAY_source' => array('LIKE', $search_string), |
|
| 2472 | + 'Payment.Payment_Method.PMD_name' => array('LIKE', $search_string), |
|
| 2473 | + 'TXN_session_data' => array('LIKE', $search_string), |
|
| 2474 | + 'Payment.PAY_txn_id_chq_nmbr' => array('LIKE', $search_string), |
|
| 2475 | + ); |
|
| 2476 | + } |
|
| 2477 | + |
|
| 2478 | + // failed transactions |
|
| 2479 | + $failed = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'failed' && ! $count) |
|
| 2480 | + || ($count && $view === 'failed'); |
|
| 2481 | + $abandoned = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'abandoned' && ! $count) |
|
| 2482 | + || ($count && $view === 'abandoned'); |
|
| 2483 | + $incomplete = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'incomplete' && ! $count) |
|
| 2484 | + || ($count && $view === 'incomplete'); |
|
| 2485 | + |
|
| 2486 | + if ($failed) { |
|
| 2487 | + $_where['STS_ID'] = EEM_Transaction::failed_status_code; |
|
| 2488 | + } elseif ($abandoned) { |
|
| 2489 | + $_where['STS_ID'] = EEM_Transaction::abandoned_status_code; |
|
| 2490 | + } elseif ($incomplete) { |
|
| 2491 | + $_where['STS_ID'] = EEM_Transaction::incomplete_status_code; |
|
| 2492 | + } else { |
|
| 2493 | + $_where['STS_ID'] = array('!=', EEM_Transaction::failed_status_code); |
|
| 2494 | + $_where['STS_ID*'] = array('!=', EEM_Transaction::abandoned_status_code); |
|
| 2495 | + } |
|
| 2496 | + |
|
| 2497 | + $query_params = apply_filters( |
|
| 2498 | + 'FHEE__Transactions_Admin_Page___get_transactions_query_params', |
|
| 2499 | + array( |
|
| 2500 | + $_where, |
|
| 2501 | + 'order_by' => array($orderby => $sort), |
|
| 2502 | + 'limit' => $limit, |
|
| 2503 | + 'default_where_conditions' => EEM_Base::default_where_conditions_this_only, |
|
| 2504 | + ), |
|
| 2505 | + $this->_req_data, |
|
| 2506 | + $view, |
|
| 2507 | + $count |
|
| 2508 | + ); |
|
| 2509 | + |
|
| 2510 | + $transactions = $count |
|
| 2511 | + ? $TXN->count(array($query_params[0]), 'TXN_ID', true) |
|
| 2512 | + : $TXN->get_all($query_params); |
|
| 2513 | + |
|
| 2514 | + return $transactions; |
|
| 2515 | + } |
|
| 2516 | 2516 | } |
@@ -363,7 +363,7 @@ discard block |
||
| 363 | 363 | // enqueue style |
| 364 | 364 | wp_register_style( |
| 365 | 365 | 'espresso_txn', |
| 366 | - TXN_ASSETS_URL . 'espresso_transactions_admin.css', |
|
| 366 | + TXN_ASSETS_URL.'espresso_transactions_admin.css', |
|
| 367 | 367 | array(), |
| 368 | 368 | EVENT_ESPRESSO_VERSION |
| 369 | 369 | ); |
@@ -371,7 +371,7 @@ discard block |
||
| 371 | 371 | // scripts |
| 372 | 372 | wp_register_script( |
| 373 | 373 | 'espresso_txn', |
| 374 | - TXN_ASSETS_URL . 'espresso_transactions_admin.js', |
|
| 374 | + TXN_ASSETS_URL.'espresso_transactions_admin.js', |
|
| 375 | 375 | array( |
| 376 | 376 | 'ee_admin_js', |
| 377 | 377 | 'ee-datepicker', |
@@ -499,7 +499,7 @@ discard block |
||
| 499 | 499 | $this->_transaction->verify_abandoned_transaction_status(); |
| 500 | 500 | } |
| 501 | 501 | |
| 502 | - if (! $this->_transaction instanceof EE_Transaction) { |
|
| 502 | + if ( ! $this->_transaction instanceof EE_Transaction) { |
|
| 503 | 503 | $error_msg = sprintf( |
| 504 | 504 | esc_html__( |
| 505 | 505 | 'An error occurred and the details for the transaction with the ID # %d could not be retrieved.', |
@@ -597,7 +597,7 @@ discard block |
||
| 597 | 597 | 'FHEE__Transactions_Admin_Page___transaction_legend_items__more_items', |
| 598 | 598 | array( |
| 599 | 599 | 'overpaid' => array( |
| 600 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::overpaid_status_code, |
|
| 600 | + 'class' => 'ee-status-legend ee-status-legend-'.EEM_Transaction::overpaid_status_code, |
|
| 601 | 601 | 'desc' => EEH_Template::pretty_status( |
| 602 | 602 | EEM_Transaction::overpaid_status_code, |
| 603 | 603 | false, |
@@ -605,7 +605,7 @@ discard block |
||
| 605 | 605 | ), |
| 606 | 606 | ), |
| 607 | 607 | 'complete' => array( |
| 608 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::complete_status_code, |
|
| 608 | + 'class' => 'ee-status-legend ee-status-legend-'.EEM_Transaction::complete_status_code, |
|
| 609 | 609 | 'desc' => EEH_Template::pretty_status( |
| 610 | 610 | EEM_Transaction::complete_status_code, |
| 611 | 611 | false, |
@@ -613,7 +613,7 @@ discard block |
||
| 613 | 613 | ), |
| 614 | 614 | ), |
| 615 | 615 | 'incomplete' => array( |
| 616 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::incomplete_status_code, |
|
| 616 | + 'class' => 'ee-status-legend ee-status-legend-'.EEM_Transaction::incomplete_status_code, |
|
| 617 | 617 | 'desc' => EEH_Template::pretty_status( |
| 618 | 618 | EEM_Transaction::incomplete_status_code, |
| 619 | 619 | false, |
@@ -621,7 +621,7 @@ discard block |
||
| 621 | 621 | ), |
| 622 | 622 | ), |
| 623 | 623 | 'abandoned' => array( |
| 624 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::abandoned_status_code, |
|
| 624 | + 'class' => 'ee-status-legend ee-status-legend-'.EEM_Transaction::abandoned_status_code, |
|
| 625 | 625 | 'desc' => EEH_Template::pretty_status( |
| 626 | 626 | EEM_Transaction::abandoned_status_code, |
| 627 | 627 | false, |
@@ -629,7 +629,7 @@ discard block |
||
| 629 | 629 | ), |
| 630 | 630 | ), |
| 631 | 631 | 'failed' => array( |
| 632 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::failed_status_code, |
|
| 632 | + 'class' => 'ee-status-legend ee-status-legend-'.EEM_Transaction::failed_status_code, |
|
| 633 | 633 | 'desc' => EEH_Template::pretty_status( |
| 634 | 634 | EEM_Transaction::failed_status_code, |
| 635 | 635 | false, |
@@ -678,7 +678,7 @@ discard block |
||
| 678 | 678 | 'Click to Edit event', |
| 679 | 679 | 'event_espresso' |
| 680 | 680 | ) |
| 681 | - . '">' . $event->get('EVT_name') . '</a>', |
|
| 681 | + . '">'.$event->get('EVT_name').'</a>', |
|
| 682 | 682 | '</h3>' |
| 683 | 683 | ) |
| 684 | 684 | : ''; |
@@ -712,7 +712,7 @@ discard block |
||
| 712 | 712 | |
| 713 | 713 | $this->_set_transaction_object(); |
| 714 | 714 | |
| 715 | - if (! $this->_transaction instanceof EE_Transaction) { |
|
| 715 | + if ( ! $this->_transaction instanceof EE_Transaction) { |
|
| 716 | 716 | return; |
| 717 | 717 | } |
| 718 | 718 | $primary_registration = $this->_transaction->primary_registration(); |
@@ -726,9 +726,9 @@ discard block |
||
| 726 | 726 | $this->_template_args['txn_datetime']['value'] = $this->_transaction->get_i18n_datetime('TXN_timestamp'); |
| 727 | 727 | $this->_template_args['txn_datetime']['label'] = esc_html__('Date', 'event_espresso'); |
| 728 | 728 | |
| 729 | - $this->_template_args['txn_status']['value'] = self::$_txn_status[ $this->_transaction->get('STS_ID') ]; |
|
| 729 | + $this->_template_args['txn_status']['value'] = self::$_txn_status[$this->_transaction->get('STS_ID')]; |
|
| 730 | 730 | $this->_template_args['txn_status']['label'] = esc_html__('Transaction Status', 'event_espresso'); |
| 731 | - $this->_template_args['txn_status']['class'] = 'status-' . $this->_transaction->get('STS_ID'); |
|
| 731 | + $this->_template_args['txn_status']['class'] = 'status-'.$this->_transaction->get('STS_ID'); |
|
| 732 | 732 | |
| 733 | 733 | $this->_template_args['grand_total'] = $this->_transaction->get('TXN_total'); |
| 734 | 734 | $this->_template_args['total_paid'] = $this->_transaction->get('TXN_paid'); |
@@ -846,7 +846,7 @@ discard block |
||
| 846 | 846 | // grab messages at the last second |
| 847 | 847 | $this->_template_args['notices'] = EE_Error::get_notices(); |
| 848 | 848 | // path to template |
| 849 | - $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_header.template.php'; |
|
| 849 | + $template_path = TXN_TEMPLATE_PATH.'txn_admin_details_header.template.php'; |
|
| 850 | 850 | $this->_template_args['admin_page_header'] = EEH_Template::display_template( |
| 851 | 851 | $template_path, |
| 852 | 852 | $this->_template_args, |
@@ -875,7 +875,7 @@ discard block |
||
| 875 | 875 | |
| 876 | 876 | $this->_set_transaction_object(); |
| 877 | 877 | |
| 878 | - if (! $this->_transaction instanceof EE_Transaction) { |
|
| 878 | + if ( ! $this->_transaction instanceof EE_Transaction) { |
|
| 879 | 879 | return; |
| 880 | 880 | } |
| 881 | 881 | add_meta_box( |
@@ -930,7 +930,7 @@ discard block |
||
| 930 | 930 | { |
| 931 | 931 | $content = ''; |
| 932 | 932 | $actions = array(); |
| 933 | - if (! $transaction instanceof EE_Transaction) { |
|
| 933 | + if ( ! $transaction instanceof EE_Transaction) { |
|
| 934 | 934 | return $content; |
| 935 | 935 | } |
| 936 | 936 | /** @var EE_Registration $primary_registration */ |
@@ -991,7 +991,7 @@ discard block |
||
| 991 | 991 | ); |
| 992 | 992 | if ($actions) { |
| 993 | 993 | $content = '<ul>'; |
| 994 | - $content .= '<li>' . implode('</li><li>', $actions) . '</li>'; |
|
| 994 | + $content .= '<li>'.implode('</li><li>', $actions).'</li>'; |
|
| 995 | 995 | $content .= '</uL>'; |
| 996 | 996 | } |
| 997 | 997 | return $content; |
@@ -1056,7 +1056,7 @@ discard block |
||
| 1056 | 1056 | |
| 1057 | 1057 | // process payment details |
| 1058 | 1058 | $payments = $this->_transaction->get_many_related('Payment'); |
| 1059 | - if (! empty($payments)) { |
|
| 1059 | + if ( ! empty($payments)) { |
|
| 1060 | 1060 | $this->_template_args['payments'] = $payments; |
| 1061 | 1061 | $this->_template_args['existing_reg_payments'] = $this->_get_registration_payment_IDs($payments); |
| 1062 | 1062 | } else { |
@@ -1117,7 +1117,7 @@ discard block |
||
| 1117 | 1117 | esc_html__('%1$s : Initiated %2$s', 'event_espresso'), |
| 1118 | 1118 | ucwords(str_replace('_', ' ', $reg_step)), |
| 1119 | 1119 | date( |
| 1120 | - get_option('date_format') . ' ' . get_option('time_format'), |
|
| 1120 | + get_option('date_format').' '.get_option('time_format'), |
|
| 1121 | 1121 | ($reg_step_status + (get_option('gmt_offset') * HOUR_IN_SECONDS)) |
| 1122 | 1122 | ) |
| 1123 | 1123 | ) |
@@ -1170,7 +1170,7 @@ discard block |
||
| 1170 | 1170 | |
| 1171 | 1171 | // 'espresso_delete_payment_nonce' |
| 1172 | 1172 | |
| 1173 | - $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_txn_details.template.php'; |
|
| 1173 | + $template_path = TXN_TEMPLATE_PATH.'txn_admin_details_main_meta_box_txn_details.template.php'; |
|
| 1174 | 1174 | echo EEH_Template::display_template($template_path, $this->_template_args, true); |
| 1175 | 1175 | } |
| 1176 | 1176 | |
@@ -1202,18 +1202,18 @@ discard block |
||
| 1202 | 1202 | ), |
| 1203 | 1203 | ) |
| 1204 | 1204 | ); |
| 1205 | - if (! empty($reg_payments)) { |
|
| 1205 | + if ( ! empty($reg_payments)) { |
|
| 1206 | 1206 | foreach ($payments as $payment) { |
| 1207 | - if (! $payment instanceof EE_Payment) { |
|
| 1207 | + if ( ! $payment instanceof EE_Payment) { |
|
| 1208 | 1208 | continue; |
| 1209 | - } elseif (! isset($existing_reg_payments[ $payment->ID() ])) { |
|
| 1210 | - $existing_reg_payments[ $payment->ID() ] = array(); |
|
| 1209 | + } elseif ( ! isset($existing_reg_payments[$payment->ID()])) { |
|
| 1210 | + $existing_reg_payments[$payment->ID()] = array(); |
|
| 1211 | 1211 | } |
| 1212 | 1212 | foreach ($reg_payments as $reg_payment) { |
| 1213 | 1213 | if ($reg_payment instanceof EE_Registration_Payment |
| 1214 | 1214 | && $reg_payment->payment_ID() === $payment->ID() |
| 1215 | 1215 | ) { |
| 1216 | - $existing_reg_payments[ $payment->ID() ][] = $reg_payment->registration_ID(); |
|
| 1216 | + $existing_reg_payments[$payment->ID()][] = $reg_payment->registration_ID(); |
|
| 1217 | 1217 | } |
| 1218 | 1218 | } |
| 1219 | 1219 | } |
@@ -1254,16 +1254,16 @@ discard block |
||
| 1254 | 1254 | '', |
| 1255 | 1255 | 'clear: both; margin: 1.5em 0 0; display: none;' |
| 1256 | 1256 | ); |
| 1257 | - $registrations_to_apply_payment_to .= EEH_HTML::br() . EEH_HTML::div('', '', 'admin-primary-mbox-tbl-wrap'); |
|
| 1257 | + $registrations_to_apply_payment_to .= EEH_HTML::br().EEH_HTML::div('', '', 'admin-primary-mbox-tbl-wrap'); |
|
| 1258 | 1258 | $registrations_to_apply_payment_to .= EEH_HTML::table('', '', 'admin-primary-mbox-tbl'); |
| 1259 | 1259 | $registrations_to_apply_payment_to .= EEH_HTML::thead( |
| 1260 | 1260 | EEH_HTML::tr( |
| 1261 | - EEH_HTML::th(esc_html__('ID', 'event_espresso')) . |
|
| 1262 | - EEH_HTML::th(esc_html__('Registrant', 'event_espresso')) . |
|
| 1263 | - EEH_HTML::th(esc_html__('Ticket', 'event_espresso')) . |
|
| 1264 | - EEH_HTML::th(esc_html__('Event', 'event_espresso')) . |
|
| 1265 | - EEH_HTML::th(esc_html__('Paid', 'event_espresso'), '', 'txn-admin-payment-paid-td jst-cntr') . |
|
| 1266 | - EEH_HTML::th(esc_html__('Owing', 'event_espresso'), '', 'txn-admin-payment-owing-td jst-cntr') . |
|
| 1261 | + EEH_HTML::th(esc_html__('ID', 'event_espresso')). |
|
| 1262 | + EEH_HTML::th(esc_html__('Registrant', 'event_espresso')). |
|
| 1263 | + EEH_HTML::th(esc_html__('Ticket', 'event_espresso')). |
|
| 1264 | + EEH_HTML::th(esc_html__('Event', 'event_espresso')). |
|
| 1265 | + EEH_HTML::th(esc_html__('Paid', 'event_espresso'), '', 'txn-admin-payment-paid-td jst-cntr'). |
|
| 1266 | + EEH_HTML::th(esc_html__('Owing', 'event_espresso'), '', 'txn-admin-payment-owing-td jst-cntr'). |
|
| 1267 | 1267 | EEH_HTML::th(esc_html__('Apply', 'event_espresso'), '', 'jst-cntr') |
| 1268 | 1268 | ) |
| 1269 | 1269 | ); |
@@ -1278,29 +1278,29 @@ discard block |
||
| 1278 | 1278 | : esc_html__('Unknown Attendee', 'event_espresso'); |
| 1279 | 1279 | $owing = $registration->final_price() - $registration->paid(); |
| 1280 | 1280 | $taxable = $registration->ticket()->taxable() |
| 1281 | - ? ' <span class="smaller-text lt-grey-text"> ' . esc_html__('+ tax', 'event_espresso') . '</span>' |
|
| 1281 | + ? ' <span class="smaller-text lt-grey-text"> '.esc_html__('+ tax', 'event_espresso').'</span>' |
|
| 1282 | 1282 | : ''; |
| 1283 | 1283 | $checked = empty($existing_reg_payments) || in_array($registration->ID(), $existing_reg_payments) |
| 1284 | 1284 | ? ' checked="checked"' |
| 1285 | 1285 | : ''; |
| 1286 | 1286 | $disabled = $registration->final_price() > 0 ? '' : ' disabled'; |
| 1287 | 1287 | $registrations_to_apply_payment_to .= EEH_HTML::tr( |
| 1288 | - EEH_HTML::td($registration->ID()) . |
|
| 1289 | - EEH_HTML::td($attendee_name) . |
|
| 1288 | + EEH_HTML::td($registration->ID()). |
|
| 1289 | + EEH_HTML::td($attendee_name). |
|
| 1290 | 1290 | EEH_HTML::td( |
| 1291 | - $registration->ticket()->name() . ' : ' . $registration->ticket()->pretty_price() . $taxable |
|
| 1292 | - ) . |
|
| 1293 | - EEH_HTML::td($registration->event_name()) . |
|
| 1294 | - EEH_HTML::td($registration->pretty_paid(), '', 'txn-admin-payment-paid-td jst-cntr') . |
|
| 1295 | - EEH_HTML::td(EEH_Template::format_currency($owing), '', 'txn-admin-payment-owing-td jst-cntr') . |
|
| 1291 | + $registration->ticket()->name().' : '.$registration->ticket()->pretty_price().$taxable |
|
| 1292 | + ). |
|
| 1293 | + EEH_HTML::td($registration->event_name()). |
|
| 1294 | + EEH_HTML::td($registration->pretty_paid(), '', 'txn-admin-payment-paid-td jst-cntr'). |
|
| 1295 | + EEH_HTML::td(EEH_Template::format_currency($owing), '', 'txn-admin-payment-owing-td jst-cntr'). |
|
| 1296 | 1296 | EEH_HTML::td( |
| 1297 | - '<input type="checkbox" value="' . $registration->ID() |
|
| 1297 | + '<input type="checkbox" value="'.$registration->ID() |
|
| 1298 | 1298 | . '" name="txn_admin_payment[registrations]"' |
| 1299 | - . $checked . $disabled . '>', |
|
| 1299 | + . $checked.$disabled.'>', |
|
| 1300 | 1300 | '', |
| 1301 | 1301 | 'jst-cntr' |
| 1302 | 1302 | ), |
| 1303 | - 'apply-payment-registration-row-' . $registration->ID() |
|
| 1303 | + 'apply-payment-registration-row-'.$registration->ID() |
|
| 1304 | 1304 | ); |
| 1305 | 1305 | } |
| 1306 | 1306 | } |
@@ -1381,12 +1381,12 @@ discard block |
||
| 1381 | 1381 | array( |
| 1382 | 1382 | 'OR*payment_method_for_payment' => array( |
| 1383 | 1383 | 'PMD_ID' => array('IN', $payment_methods_of_payments), |
| 1384 | - 'PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%'), |
|
| 1384 | + 'PMD_scope' => array('LIKE', '%'.EEM_Payment_Method::scope_admin.'%'), |
|
| 1385 | 1385 | ), |
| 1386 | 1386 | ), |
| 1387 | 1387 | ); |
| 1388 | 1388 | } else { |
| 1389 | - $query_args = array(array('PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%'))); |
|
| 1389 | + $query_args = array(array('PMD_scope' => array('LIKE', '%'.EEM_Payment_Method::scope_admin.'%'))); |
|
| 1390 | 1390 | } |
| 1391 | 1391 | $this->_template_args['payment_methods'] = EEM_Payment_Method::instance()->get_all($query_args); |
| 1392 | 1392 | } |
@@ -1415,7 +1415,7 @@ discard block |
||
| 1415 | 1415 | 'Line_Item', |
| 1416 | 1416 | array(array('LIN_type' => 'line-item')) |
| 1417 | 1417 | ); |
| 1418 | - if (! empty($line_items)) { |
|
| 1418 | + if ( ! empty($line_items)) { |
|
| 1419 | 1419 | foreach ($line_items as $item) { |
| 1420 | 1420 | if ($item instanceof EE_Line_Item) { |
| 1421 | 1421 | switch ($item->OBJ_type()) { |
@@ -1425,7 +1425,7 @@ discard block |
||
| 1425 | 1425 | $ticket = $item->ticket(); |
| 1426 | 1426 | // right now we're only handling tickets here. |
| 1427 | 1427 | // Cause its expected that only tickets will have attendees right? |
| 1428 | - if (! $ticket instanceof EE_Ticket) { |
|
| 1428 | + if ( ! $ticket instanceof EE_Ticket) { |
|
| 1429 | 1429 | break; |
| 1430 | 1430 | } |
| 1431 | 1431 | try { |
@@ -1434,7 +1434,7 @@ discard block |
||
| 1434 | 1434 | EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
| 1435 | 1435 | $event_name = esc_html__('Unknown Event', 'event_espresso'); |
| 1436 | 1436 | } |
| 1437 | - $event_name .= ' - ' . $item->get('LIN_name'); |
|
| 1437 | + $event_name .= ' - '.$item->get('LIN_name'); |
|
| 1438 | 1438 | $ticket_price = EEH_Template::format_currency($item->get('LIN_unit_price')); |
| 1439 | 1439 | // now get all of the registrations for this transaction that use this ticket |
| 1440 | 1440 | $registrations = $ticket->get_many_related( |
@@ -1442,38 +1442,38 @@ discard block |
||
| 1442 | 1442 | array(array('TXN_ID' => $this->_transaction->ID())) |
| 1443 | 1443 | ); |
| 1444 | 1444 | foreach ($registrations as $registration) { |
| 1445 | - if (! $registration instanceof EE_Registration) { |
|
| 1445 | + if ( ! $registration instanceof EE_Registration) { |
|
| 1446 | 1446 | break; |
| 1447 | 1447 | } |
| 1448 | - $this->_template_args['event_attendees'][ $registration->ID() ]['STS_ID'] |
|
| 1448 | + $this->_template_args['event_attendees'][$registration->ID()]['STS_ID'] |
|
| 1449 | 1449 | = $registration->status_ID(); |
| 1450 | - $this->_template_args['event_attendees'][ $registration->ID() ]['att_num'] |
|
| 1450 | + $this->_template_args['event_attendees'][$registration->ID()]['att_num'] |
|
| 1451 | 1451 | = $registration->count(); |
| 1452 | - $this->_template_args['event_attendees'][ $registration->ID() ]['event_ticket_name'] |
|
| 1452 | + $this->_template_args['event_attendees'][$registration->ID()]['event_ticket_name'] |
|
| 1453 | 1453 | = $event_name; |
| 1454 | - $this->_template_args['event_attendees'][ $registration->ID() ]['ticket_price'] |
|
| 1454 | + $this->_template_args['event_attendees'][$registration->ID()]['ticket_price'] |
|
| 1455 | 1455 | = $ticket_price; |
| 1456 | 1456 | // attendee info |
| 1457 | 1457 | $attendee = $registration->get_first_related('Attendee'); |
| 1458 | 1458 | if ($attendee instanceof EE_Attendee) { |
| 1459 | - $this->_template_args['event_attendees'][ $registration->ID() ]['att_id'] |
|
| 1459 | + $this->_template_args['event_attendees'][$registration->ID()]['att_id'] |
|
| 1460 | 1460 | = $attendee->ID(); |
| 1461 | - $this->_template_args['event_attendees'][ $registration->ID() ]['attendee'] |
|
| 1461 | + $this->_template_args['event_attendees'][$registration->ID()]['attendee'] |
|
| 1462 | 1462 | = $attendee->full_name(); |
| 1463 | - $this->_template_args['event_attendees'][ $registration->ID() ]['email'] |
|
| 1464 | - = '<a href="mailto:' . $attendee->email() . '?subject=' . $event_name |
|
| 1463 | + $this->_template_args['event_attendees'][$registration->ID()]['email'] |
|
| 1464 | + = '<a href="mailto:'.$attendee->email().'?subject='.$event_name |
|
| 1465 | 1465 | . esc_html__( |
| 1466 | 1466 | ' Event', |
| 1467 | 1467 | 'event_espresso' |
| 1468 | 1468 | ) |
| 1469 | - . '">' . $attendee->email() . '</a>'; |
|
| 1470 | - $this->_template_args['event_attendees'][ $registration->ID() ]['address'] |
|
| 1469 | + . '">'.$attendee->email().'</a>'; |
|
| 1470 | + $this->_template_args['event_attendees'][$registration->ID()]['address'] |
|
| 1471 | 1471 | = EEH_Address::format($attendee, 'inline', false, false); |
| 1472 | 1472 | } else { |
| 1473 | - $this->_template_args['event_attendees'][ $registration->ID() ]['att_id'] = ''; |
|
| 1474 | - $this->_template_args['event_attendees'][ $registration->ID() ]['attendee'] = ''; |
|
| 1475 | - $this->_template_args['event_attendees'][ $registration->ID() ]['email'] = ''; |
|
| 1476 | - $this->_template_args['event_attendees'][ $registration->ID() ]['address'] = ''; |
|
| 1473 | + $this->_template_args['event_attendees'][$registration->ID()]['att_id'] = ''; |
|
| 1474 | + $this->_template_args['event_attendees'][$registration->ID()]['attendee'] = ''; |
|
| 1475 | + $this->_template_args['event_attendees'][$registration->ID()]['email'] = ''; |
|
| 1476 | + $this->_template_args['event_attendees'][$registration->ID()]['address'] = ''; |
|
| 1477 | 1477 | } |
| 1478 | 1478 | } |
| 1479 | 1479 | break; |
@@ -1489,7 +1489,7 @@ discard block |
||
| 1489 | 1489 | TXN_ADMIN_URL |
| 1490 | 1490 | ); |
| 1491 | 1491 | echo EEH_Template::display_template( |
| 1492 | - TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_attendees.template.php', |
|
| 1492 | + TXN_TEMPLATE_PATH.'txn_admin_details_main_meta_box_attendees.template.php', |
|
| 1493 | 1493 | $this->_template_args, |
| 1494 | 1494 | true |
| 1495 | 1495 | ); |
@@ -1524,7 +1524,7 @@ discard block |
||
| 1524 | 1524 | $primary_att = $this->_transaction->primary_registration() instanceof EE_Registration |
| 1525 | 1525 | ? $this->_transaction->primary_registration()->get_first_related('Attendee') |
| 1526 | 1526 | : null; |
| 1527 | - if (! $primary_att instanceof EE_Attendee) { |
|
| 1527 | + if ( ! $primary_att instanceof EE_Attendee) { |
|
| 1528 | 1528 | $this->_template_args['no_attendee_message'] = esc_html__( |
| 1529 | 1529 | 'There is no attached contact for this transaction. The transaction either failed due to an error or was abandoned.', |
| 1530 | 1530 | 'event_espresso' |
@@ -1546,7 +1546,7 @@ discard block |
||
| 1546 | 1546 | // get formatted address for registrant |
| 1547 | 1547 | $this->_template_args['formatted_address'] = EEH_Address::format($primary_att); |
| 1548 | 1548 | echo EEH_Template::display_template( |
| 1549 | - TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_registrant.template.php', |
|
| 1549 | + TXN_TEMPLATE_PATH.'txn_admin_details_side_meta_box_registrant.template.php', |
|
| 1550 | 1550 | $this->_template_args, |
| 1551 | 1551 | true |
| 1552 | 1552 | ); |
@@ -1571,8 +1571,8 @@ discard block |
||
| 1571 | 1571 | TXN_ADMIN_URL |
| 1572 | 1572 | ); |
| 1573 | 1573 | |
| 1574 | - $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_billing_info.template.php'; |
|
| 1575 | - echo EEH_Template::display_template($template_path, $this->_template_args, true);/**/ |
|
| 1574 | + $template_path = TXN_TEMPLATE_PATH.'txn_admin_details_side_meta_box_billing_info.template.php'; |
|
| 1575 | + echo EEH_Template::display_template($template_path, $this->_template_args, true); /**/ |
|
| 1576 | 1576 | } |
| 1577 | 1577 | |
| 1578 | 1578 | |
@@ -1597,7 +1597,7 @@ discard block |
||
| 1597 | 1597 | 'ee_edit_payments', |
| 1598 | 1598 | 'apply_payment_or_refund_from_registration_details' |
| 1599 | 1599 | ); |
| 1600 | - if (! empty($valid_data) && $has_access) { |
|
| 1600 | + if ( ! empty($valid_data) && $has_access) { |
|
| 1601 | 1601 | $PAY_ID = $valid_data['PAY_ID']; |
| 1602 | 1602 | // save the new payment |
| 1603 | 1603 | $payment = $this->_create_payment_from_request_data($valid_data); |
@@ -1610,7 +1610,7 @@ discard block |
||
| 1610 | 1610 | $REG_IDs = $this->_get_REG_IDs_to_apply_payment_to($payment); |
| 1611 | 1611 | $this->_remove_existing_registration_payments($payment, $PAY_ID); |
| 1612 | 1612 | // apply payment to registrations (if applicable) |
| 1613 | - if (! empty($REG_IDs)) { |
|
| 1613 | + if ( ! empty($REG_IDs)) { |
|
| 1614 | 1614 | $this->_update_registration_payments($transaction, $payment, $REG_IDs); |
| 1615 | 1615 | $this->_maybe_send_notifications(); |
| 1616 | 1616 | // now process status changes for the same registrations |
@@ -1680,14 +1680,14 @@ discard block |
||
| 1680 | 1680 | */ |
| 1681 | 1681 | protected function _validate_payment_request_data() |
| 1682 | 1682 | { |
| 1683 | - if (! isset($this->_req_data['txn_admin_payment'])) { |
|
| 1683 | + if ( ! isset($this->_req_data['txn_admin_payment'])) { |
|
| 1684 | 1684 | return false; |
| 1685 | 1685 | } |
| 1686 | 1686 | $payment_form = $this->_generate_payment_form_section(); |
| 1687 | 1687 | try { |
| 1688 | 1688 | if ($payment_form->was_submitted()) { |
| 1689 | 1689 | $payment_form->receive_form_submission(); |
| 1690 | - if (! $payment_form->is_valid()) { |
|
| 1690 | + if ( ! $payment_form->is_valid()) { |
|
| 1691 | 1691 | $submission_error_messages = array(); |
| 1692 | 1692 | foreach ($payment_form->get_validation_errors_accumulated() as $validation_error) { |
| 1693 | 1693 | if ($validation_error instanceof EE_Validation_Error) { |
@@ -1866,7 +1866,7 @@ discard block |
||
| 1866 | 1866 | array('Y-m-d', 'g:i a') |
| 1867 | 1867 | ); |
| 1868 | 1868 | |
| 1869 | - if (! $payment->save()) { |
|
| 1869 | + if ( ! $payment->save()) { |
|
| 1870 | 1870 | EE_Error::add_error( |
| 1871 | 1871 | sprintf( |
| 1872 | 1872 | esc_html__('Payment %1$d has not been successfully saved to the database.', 'event_espresso'), |
@@ -2063,12 +2063,12 @@ discard block |
||
| 2063 | 2063 | // but add in some conditions regarding payment, |
| 2064 | 2064 | // so that we don't apply payments to registrations that are free or have already been paid for |
| 2065 | 2065 | // but ONLY if the payment is NOT a refund ( ie: the payment amount is not negative ) |
| 2066 | - if (! $payment->is_a_refund()) { |
|
| 2066 | + if ( ! $payment->is_a_refund()) { |
|
| 2067 | 2067 | $registration_query_where_params['REG_final_price'] = array('!=', 0); |
| 2068 | 2068 | $registration_query_where_params['REG_final_price*'] = array('!=', 'REG_paid', true); |
| 2069 | 2069 | } |
| 2070 | 2070 | $registrations = $transaction->registrations(array($registration_query_where_params)); |
| 2071 | - if (! empty($registrations)) { |
|
| 2071 | + if ( ! empty($registrations)) { |
|
| 2072 | 2072 | /** @type EE_Payment_Processor $payment_processor */ |
| 2073 | 2073 | $payment_processor = EE_Registry::instance()->load_core('Payment_Processor'); |
| 2074 | 2074 | $payment_processor->process_registration_payments($transaction, $payment, $registrations); |
@@ -2093,7 +2093,7 @@ discard block |
||
| 2093 | 2093 | protected function _process_registration_status_change(EE_Transaction $transaction, $REG_IDs = array()) |
| 2094 | 2094 | { |
| 2095 | 2095 | // first if there is no change in status then we get out. |
| 2096 | - if (! isset($this->_req_data['txn_reg_status_change']['reg_status']) |
|
| 2096 | + if ( ! isset($this->_req_data['txn_reg_status_change']['reg_status']) |
|
| 2097 | 2097 | || $this->_req_data['txn_reg_status_change']['reg_status'] === 'NAN' |
| 2098 | 2098 | ) { |
| 2099 | 2099 | // no error message, no change requested, just nothing to do man. |
@@ -2151,7 +2151,7 @@ discard block |
||
| 2151 | 2151 | 'pay_status' => $payment->STS_ID(), |
| 2152 | 2152 | 'PAY_ID' => $payment->ID(), |
| 2153 | 2153 | 'STS_ID' => $payment->STS_ID(), |
| 2154 | - 'status' => self::$_pay_status[ $payment->STS_ID() ], |
|
| 2154 | + 'status' => self::$_pay_status[$payment->STS_ID()], |
|
| 2155 | 2155 | 'date' => $payment->timestamp('Y-m-d', 'h:i a'), |
| 2156 | 2156 | 'method' => strtoupper($payment->source()), |
| 2157 | 2157 | 'PM_ID' => $payment->payment_method() ? $payment->payment_method()->ID() : 1, |
@@ -2273,11 +2273,11 @@ discard block |
||
| 2273 | 2273 | { |
| 2274 | 2274 | $registration_payment_data = array(); |
| 2275 | 2275 | // if non empty reg_ids lets get an array of registrations and update the values for the apply_payment/refund rows. |
| 2276 | - if (! empty($REG_IDs)) { |
|
| 2276 | + if ( ! empty($REG_IDs)) { |
|
| 2277 | 2277 | $registrations = EEM_Registration::instance()->get_all(array(array('REG_ID' => array('IN', $REG_IDs)))); |
| 2278 | 2278 | foreach ($registrations as $registration) { |
| 2279 | 2279 | if ($registration instanceof EE_Registration) { |
| 2280 | - $registration_payment_data[ $registration->ID() ] = array( |
|
| 2280 | + $registration_payment_data[$registration->ID()] = array( |
|
| 2281 | 2281 | 'paid' => $registration->pretty_paid(), |
| 2282 | 2282 | 'owing' => EEH_Template::format_currency($registration->final_price() - $registration->paid()), |
| 2283 | 2283 | ); |
@@ -2389,8 +2389,8 @@ discard block |
||
| 2389 | 2389 | : date('m/d/Y'); |
| 2390 | 2390 | |
| 2391 | 2391 | // make sure our timestamps start and end right at the boundaries for each day |
| 2392 | - $start_date = date('Y-m-d', strtotime($start_date)) . ' 00:00:00'; |
|
| 2393 | - $end_date = date('Y-m-d', strtotime($end_date)) . ' 23:59:59'; |
|
| 2392 | + $start_date = date('Y-m-d', strtotime($start_date)).' 00:00:00'; |
|
| 2393 | + $end_date = date('Y-m-d', strtotime($end_date)).' 23:59:59'; |
|
| 2394 | 2394 | |
| 2395 | 2395 | |
| 2396 | 2396 | // convert to timestamps |
@@ -2449,7 +2449,7 @@ discard block |
||
| 2449 | 2449 | } |
| 2450 | 2450 | |
| 2451 | 2451 | if (isset($this->_req_data['s'])) { |
| 2452 | - $search_string = '%' . $this->_req_data['s'] . '%'; |
|
| 2452 | + $search_string = '%'.$this->_req_data['s'].'%'; |
|
| 2453 | 2453 | $_where['OR'] = array( |
| 2454 | 2454 | 'Registration.Event.EVT_name' => array('LIKE', $search_string), |
| 2455 | 2455 | 'Registration.Event.EVT_desc' => array('LIKE', $search_string), |
@@ -2476,11 +2476,11 @@ discard block |
||
| 2476 | 2476 | } |
| 2477 | 2477 | |
| 2478 | 2478 | // failed transactions |
| 2479 | - $failed = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'failed' && ! $count) |
|
| 2479 | + $failed = ( ! empty($this->_req_data['status']) && $this->_req_data['status'] === 'failed' && ! $count) |
|
| 2480 | 2480 | || ($count && $view === 'failed'); |
| 2481 | - $abandoned = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'abandoned' && ! $count) |
|
| 2481 | + $abandoned = ( ! empty($this->_req_data['status']) && $this->_req_data['status'] === 'abandoned' && ! $count) |
|
| 2482 | 2482 | || ($count && $view === 'abandoned'); |
| 2483 | - $incomplete = (! empty($this->_req_data['status']) && $this->_req_data['status'] === 'incomplete' && ! $count) |
|
| 2483 | + $incomplete = ( ! empty($this->_req_data['status']) && $this->_req_data['status'] === 'incomplete' && ! $count) |
|
| 2484 | 2484 | || ($count && $view === 'incomplete'); |
| 2485 | 2485 | |
| 2486 | 2486 | if ($failed) { |
@@ -36,6429 +36,6429 @@ |
||
| 36 | 36 | abstract class EEM_Base extends EE_Base implements ResettableInterface |
| 37 | 37 | { |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Flag to indicate whether the values provided to EEM_Base have already been prepared |
|
| 41 | - * by the model object or not (ie, the model object has used the field's _prepare_for_set function on the values). |
|
| 42 | - * They almost always WILL NOT, but it's not necessarily a requirement. |
|
| 43 | - * For example, if you want to run EEM_Event::instance()->get_all(array(array('EVT_ID'=>$_GET['event_id']))); |
|
| 44 | - * |
|
| 45 | - * @var boolean |
|
| 46 | - */ |
|
| 47 | - private $_values_already_prepared_by_model_object = 0; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * when $_values_already_prepared_by_model_object equals this, we assume |
|
| 51 | - * the data is just like form input that needs to have the model fields' |
|
| 52 | - * prepare_for_set and prepare_for_use_in_db called on it |
|
| 53 | - */ |
|
| 54 | - const not_prepared_by_model_object = 0; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * when $_values_already_prepared_by_model_object equals this, we |
|
| 58 | - * assume this value is coming from a model object and doesn't need to have |
|
| 59 | - * prepare_for_set called on it, just prepare_for_use_in_db is used |
|
| 60 | - */ |
|
| 61 | - const prepared_by_model_object = 1; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * when $_values_already_prepared_by_model_object equals this, we assume |
|
| 65 | - * the values are already to be used in the database (ie no processing is done |
|
| 66 | - * on them by the model's fields) |
|
| 67 | - */ |
|
| 68 | - const prepared_for_use_in_db = 2; |
|
| 69 | - |
|
| 70 | - |
|
| 71 | - protected $singular_item = 'Item'; |
|
| 72 | - |
|
| 73 | - protected $plural_item = 'Items'; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @type \EE_Table_Base[] $_tables array of EE_Table objects for defining which tables comprise this model. |
|
| 77 | - */ |
|
| 78 | - protected $_tables; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * with two levels: top-level has array keys which are database table aliases (ie, keys in _tables) |
|
| 82 | - * and the value is an array. Each of those sub-arrays have keys of field names (eg 'ATT_ID', which should also be |
|
| 83 | - * variable names on the model objects (eg, EE_Attendee), and the keys should be children of EE_Model_Field |
|
| 84 | - * |
|
| 85 | - * @var \EE_Model_Field_Base[][] $_fields |
|
| 86 | - */ |
|
| 87 | - protected $_fields; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * array of different kinds of relations |
|
| 91 | - * |
|
| 92 | - * @var \EE_Model_Relation_Base[] $_model_relations |
|
| 93 | - */ |
|
| 94 | - protected $_model_relations; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @var \EE_Index[] $_indexes |
|
| 98 | - */ |
|
| 99 | - protected $_indexes = array(); |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Default strategy for getting where conditions on this model. This strategy is used to get default |
|
| 103 | - * where conditions which are added to get_all, update, and delete queries. They can be overridden |
|
| 104 | - * by setting the same columns as used in these queries in the query yourself. |
|
| 105 | - * |
|
| 106 | - * @var EE_Default_Where_Conditions |
|
| 107 | - */ |
|
| 108 | - protected $_default_where_conditions_strategy; |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Strategy for getting conditions on this model when 'default_where_conditions' equals 'minimum'. |
|
| 112 | - * This is particularly useful when you want something between 'none' and 'default' |
|
| 113 | - * |
|
| 114 | - * @var EE_Default_Where_Conditions |
|
| 115 | - */ |
|
| 116 | - protected $_minimum_where_conditions_strategy; |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * String describing how to find the "owner" of this model's objects. |
|
| 120 | - * When there is a foreign key on this model to the wp_users table, this isn't needed. |
|
| 121 | - * But when there isn't, this indicates which related model, or transiently-related model, |
|
| 122 | - * has the foreign key to the wp_users table. |
|
| 123 | - * Eg, for EEM_Registration this would be 'Event' because registrations are directly |
|
| 124 | - * related to events, and events have a foreign key to wp_users. |
|
| 125 | - * On EEM_Transaction, this would be 'Transaction.Event' |
|
| 126 | - * |
|
| 127 | - * @var string |
|
| 128 | - */ |
|
| 129 | - protected $_model_chain_to_wp_user = ''; |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * String describing how to find the model with a password controlling access to this model. This property has the |
|
| 133 | - * same format as $_model_chain_to_wp_user. This is primarily used by the query param "exclude_protected". |
|
| 134 | - * This value is the path of models to follow to arrive at the model with the password field. |
|
| 135 | - * If it is an empty string, it means this model has the password field. If it is null, it means there is no |
|
| 136 | - * model with a password that should affect reading this on the front-end. |
|
| 137 | - * Eg this is an empty string for the Event model because it has a password. |
|
| 138 | - * This is null for the Registration model, because its event's password has no bearing on whether |
|
| 139 | - * you can read the registration or not on the front-end (it just depends on your capabilities.) |
|
| 140 | - * This is 'Datetime.Event' on the Ticket model, because model queries for tickets that set "exclude_protected" |
|
| 141 | - * should hide tickets for datetimes for events that have a password set. |
|
| 142 | - * @var string |null |
|
| 143 | - */ |
|
| 144 | - protected $model_chain_to_password = null; |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * This is a flag typically set by updates so that we don't load the where strategy on updates because updates |
|
| 148 | - * don't need it (particularly CPT models) |
|
| 149 | - * |
|
| 150 | - * @var bool |
|
| 151 | - */ |
|
| 152 | - protected $_ignore_where_strategy = false; |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * String used in caps relating to this model. Eg, if the caps relating to this |
|
| 156 | - * model are 'ee_edit_events', 'ee_read_events', etc, it would be 'events'. |
|
| 157 | - * |
|
| 158 | - * @var string. If null it hasn't been initialized yet. If false then we |
|
| 159 | - * have indicated capabilities don't apply to this |
|
| 160 | - */ |
|
| 161 | - protected $_caps_slug = null; |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * 2d array where top-level keys are one of EEM_Base::valid_cap_contexts(), |
|
| 165 | - * and next-level keys are capability names, and each's value is a |
|
| 166 | - * EE_Default_Where_Condition. If the requester requests to apply caps to the query, |
|
| 167 | - * they specify which context to use (ie, frontend, backend, edit or delete) |
|
| 168 | - * and then each capability in the corresponding sub-array that they're missing |
|
| 169 | - * adds the where conditions onto the query. |
|
| 170 | - * |
|
| 171 | - * @var array |
|
| 172 | - */ |
|
| 173 | - protected $_cap_restrictions = array( |
|
| 174 | - self::caps_read => array(), |
|
| 175 | - self::caps_read_admin => array(), |
|
| 176 | - self::caps_edit => array(), |
|
| 177 | - self::caps_delete => array(), |
|
| 178 | - ); |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Array defining which cap restriction generators to use to create default |
|
| 182 | - * cap restrictions to put in EEM_Base::_cap_restrictions. |
|
| 183 | - * Array-keys are one of EEM_Base::valid_cap_contexts(), and values are a child of |
|
| 184 | - * EE_Restriction_Generator_Base. If you don't want any cap restrictions generated |
|
| 185 | - * automatically set this to false (not just null). |
|
| 186 | - * |
|
| 187 | - * @var EE_Restriction_Generator_Base[] |
|
| 188 | - */ |
|
| 189 | - protected $_cap_restriction_generators = array(); |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * constants used to categorize capability restrictions on EEM_Base::_caps_restrictions |
|
| 193 | - */ |
|
| 194 | - const caps_read = 'read'; |
|
| 195 | - |
|
| 196 | - const caps_read_admin = 'read_admin'; |
|
| 197 | - |
|
| 198 | - const caps_edit = 'edit'; |
|
| 199 | - |
|
| 200 | - const caps_delete = 'delete'; |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Keys are all the cap contexts (ie constants EEM_Base::_caps_*) and values are their 'action' |
|
| 204 | - * as how they'd be used in capability names. Eg EEM_Base::caps_read ('read_frontend') |
|
| 205 | - * maps to 'read' because when looking for relevant permissions we're going to use |
|
| 206 | - * 'read' in teh capabilities names like 'ee_read_events' etc. |
|
| 207 | - * |
|
| 208 | - * @var array |
|
| 209 | - */ |
|
| 210 | - protected $_cap_contexts_to_cap_action_map = array( |
|
| 211 | - self::caps_read => 'read', |
|
| 212 | - self::caps_read_admin => 'read', |
|
| 213 | - self::caps_edit => 'edit', |
|
| 214 | - self::caps_delete => 'delete', |
|
| 215 | - ); |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Timezone |
|
| 219 | - * This gets set via the constructor so that we know what timezone incoming strings|timestamps are in when there |
|
| 220 | - * are EE_Datetime_Fields in use. This can also be used before a get to set what timezone you want strings coming |
|
| 221 | - * out of the created objects. NOT all EEM_Base child classes use this property but any that use a |
|
| 222 | - * EE_Datetime_Field data type will have access to it. |
|
| 223 | - * |
|
| 224 | - * @var string |
|
| 225 | - */ |
|
| 226 | - protected $_timezone; |
|
| 227 | - |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * This holds the id of the blog currently making the query. Has no bearing on single site but is used for |
|
| 231 | - * multisite. |
|
| 232 | - * |
|
| 233 | - * @var int |
|
| 234 | - */ |
|
| 235 | - protected static $_model_query_blog_id; |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * A copy of _fields, except the array keys are the model names pointed to by |
|
| 239 | - * the field |
|
| 240 | - * |
|
| 241 | - * @var EE_Model_Field_Base[] |
|
| 242 | - */ |
|
| 243 | - private $_cache_foreign_key_to_fields = array(); |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Cached list of all the fields on the model, indexed by their name |
|
| 247 | - * |
|
| 248 | - * @var EE_Model_Field_Base[] |
|
| 249 | - */ |
|
| 250 | - private $_cached_fields = null; |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Cached list of all the fields on the model, except those that are |
|
| 254 | - * marked as only pertinent to the database |
|
| 255 | - * |
|
| 256 | - * @var EE_Model_Field_Base[] |
|
| 257 | - */ |
|
| 258 | - private $_cached_fields_non_db_only = null; |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * A cached reference to the primary key for quick lookup |
|
| 262 | - * |
|
| 263 | - * @var EE_Model_Field_Base |
|
| 264 | - */ |
|
| 265 | - private $_primary_key_field = null; |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * Flag indicating whether this model has a primary key or not |
|
| 269 | - * |
|
| 270 | - * @var boolean |
|
| 271 | - */ |
|
| 272 | - protected $_has_primary_key_field = null; |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Whether or not this model is based off a table in WP core only (CPTs should set |
|
| 276 | - * this to FALSE, but if we were to make an EE_WP_Post model, it should set this to true). |
|
| 277 | - * This should be true for models that deal with data that should exist independent of EE. |
|
| 278 | - * For example, if the model can read and insert data that isn't used by EE, this should be true. |
|
| 279 | - * It would be false, however, if you could guarantee the model would only interact with EE data, |
|
| 280 | - * even if it uses a WP core table (eg event and venue models set this to false for that reason: |
|
| 281 | - * they can only read and insert events and venues custom post types, not arbitrary post types) |
|
| 282 | - * @var boolean |
|
| 283 | - */ |
|
| 284 | - protected $_wp_core_model = false; |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * @var bool stores whether this model has a password field or not. |
|
| 288 | - * null until initialized by hasPasswordField() |
|
| 289 | - */ |
|
| 290 | - protected $has_password_field; |
|
| 39 | + /** |
|
| 40 | + * Flag to indicate whether the values provided to EEM_Base have already been prepared |
|
| 41 | + * by the model object or not (ie, the model object has used the field's _prepare_for_set function on the values). |
|
| 42 | + * They almost always WILL NOT, but it's not necessarily a requirement. |
|
| 43 | + * For example, if you want to run EEM_Event::instance()->get_all(array(array('EVT_ID'=>$_GET['event_id']))); |
|
| 44 | + * |
|
| 45 | + * @var boolean |
|
| 46 | + */ |
|
| 47 | + private $_values_already_prepared_by_model_object = 0; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * when $_values_already_prepared_by_model_object equals this, we assume |
|
| 51 | + * the data is just like form input that needs to have the model fields' |
|
| 52 | + * prepare_for_set and prepare_for_use_in_db called on it |
|
| 53 | + */ |
|
| 54 | + const not_prepared_by_model_object = 0; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * when $_values_already_prepared_by_model_object equals this, we |
|
| 58 | + * assume this value is coming from a model object and doesn't need to have |
|
| 59 | + * prepare_for_set called on it, just prepare_for_use_in_db is used |
|
| 60 | + */ |
|
| 61 | + const prepared_by_model_object = 1; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * when $_values_already_prepared_by_model_object equals this, we assume |
|
| 65 | + * the values are already to be used in the database (ie no processing is done |
|
| 66 | + * on them by the model's fields) |
|
| 67 | + */ |
|
| 68 | + const prepared_for_use_in_db = 2; |
|
| 69 | + |
|
| 70 | + |
|
| 71 | + protected $singular_item = 'Item'; |
|
| 72 | + |
|
| 73 | + protected $plural_item = 'Items'; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @type \EE_Table_Base[] $_tables array of EE_Table objects for defining which tables comprise this model. |
|
| 77 | + */ |
|
| 78 | + protected $_tables; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * with two levels: top-level has array keys which are database table aliases (ie, keys in _tables) |
|
| 82 | + * and the value is an array. Each of those sub-arrays have keys of field names (eg 'ATT_ID', which should also be |
|
| 83 | + * variable names on the model objects (eg, EE_Attendee), and the keys should be children of EE_Model_Field |
|
| 84 | + * |
|
| 85 | + * @var \EE_Model_Field_Base[][] $_fields |
|
| 86 | + */ |
|
| 87 | + protected $_fields; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * array of different kinds of relations |
|
| 91 | + * |
|
| 92 | + * @var \EE_Model_Relation_Base[] $_model_relations |
|
| 93 | + */ |
|
| 94 | + protected $_model_relations; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @var \EE_Index[] $_indexes |
|
| 98 | + */ |
|
| 99 | + protected $_indexes = array(); |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Default strategy for getting where conditions on this model. This strategy is used to get default |
|
| 103 | + * where conditions which are added to get_all, update, and delete queries. They can be overridden |
|
| 104 | + * by setting the same columns as used in these queries in the query yourself. |
|
| 105 | + * |
|
| 106 | + * @var EE_Default_Where_Conditions |
|
| 107 | + */ |
|
| 108 | + protected $_default_where_conditions_strategy; |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Strategy for getting conditions on this model when 'default_where_conditions' equals 'minimum'. |
|
| 112 | + * This is particularly useful when you want something between 'none' and 'default' |
|
| 113 | + * |
|
| 114 | + * @var EE_Default_Where_Conditions |
|
| 115 | + */ |
|
| 116 | + protected $_minimum_where_conditions_strategy; |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * String describing how to find the "owner" of this model's objects. |
|
| 120 | + * When there is a foreign key on this model to the wp_users table, this isn't needed. |
|
| 121 | + * But when there isn't, this indicates which related model, or transiently-related model, |
|
| 122 | + * has the foreign key to the wp_users table. |
|
| 123 | + * Eg, for EEM_Registration this would be 'Event' because registrations are directly |
|
| 124 | + * related to events, and events have a foreign key to wp_users. |
|
| 125 | + * On EEM_Transaction, this would be 'Transaction.Event' |
|
| 126 | + * |
|
| 127 | + * @var string |
|
| 128 | + */ |
|
| 129 | + protected $_model_chain_to_wp_user = ''; |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * String describing how to find the model with a password controlling access to this model. This property has the |
|
| 133 | + * same format as $_model_chain_to_wp_user. This is primarily used by the query param "exclude_protected". |
|
| 134 | + * This value is the path of models to follow to arrive at the model with the password field. |
|
| 135 | + * If it is an empty string, it means this model has the password field. If it is null, it means there is no |
|
| 136 | + * model with a password that should affect reading this on the front-end. |
|
| 137 | + * Eg this is an empty string for the Event model because it has a password. |
|
| 138 | + * This is null for the Registration model, because its event's password has no bearing on whether |
|
| 139 | + * you can read the registration or not on the front-end (it just depends on your capabilities.) |
|
| 140 | + * This is 'Datetime.Event' on the Ticket model, because model queries for tickets that set "exclude_protected" |
|
| 141 | + * should hide tickets for datetimes for events that have a password set. |
|
| 142 | + * @var string |null |
|
| 143 | + */ |
|
| 144 | + protected $model_chain_to_password = null; |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * This is a flag typically set by updates so that we don't load the where strategy on updates because updates |
|
| 148 | + * don't need it (particularly CPT models) |
|
| 149 | + * |
|
| 150 | + * @var bool |
|
| 151 | + */ |
|
| 152 | + protected $_ignore_where_strategy = false; |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * String used in caps relating to this model. Eg, if the caps relating to this |
|
| 156 | + * model are 'ee_edit_events', 'ee_read_events', etc, it would be 'events'. |
|
| 157 | + * |
|
| 158 | + * @var string. If null it hasn't been initialized yet. If false then we |
|
| 159 | + * have indicated capabilities don't apply to this |
|
| 160 | + */ |
|
| 161 | + protected $_caps_slug = null; |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * 2d array where top-level keys are one of EEM_Base::valid_cap_contexts(), |
|
| 165 | + * and next-level keys are capability names, and each's value is a |
|
| 166 | + * EE_Default_Where_Condition. If the requester requests to apply caps to the query, |
|
| 167 | + * they specify which context to use (ie, frontend, backend, edit or delete) |
|
| 168 | + * and then each capability in the corresponding sub-array that they're missing |
|
| 169 | + * adds the where conditions onto the query. |
|
| 170 | + * |
|
| 171 | + * @var array |
|
| 172 | + */ |
|
| 173 | + protected $_cap_restrictions = array( |
|
| 174 | + self::caps_read => array(), |
|
| 175 | + self::caps_read_admin => array(), |
|
| 176 | + self::caps_edit => array(), |
|
| 177 | + self::caps_delete => array(), |
|
| 178 | + ); |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Array defining which cap restriction generators to use to create default |
|
| 182 | + * cap restrictions to put in EEM_Base::_cap_restrictions. |
|
| 183 | + * Array-keys are one of EEM_Base::valid_cap_contexts(), and values are a child of |
|
| 184 | + * EE_Restriction_Generator_Base. If you don't want any cap restrictions generated |
|
| 185 | + * automatically set this to false (not just null). |
|
| 186 | + * |
|
| 187 | + * @var EE_Restriction_Generator_Base[] |
|
| 188 | + */ |
|
| 189 | + protected $_cap_restriction_generators = array(); |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * constants used to categorize capability restrictions on EEM_Base::_caps_restrictions |
|
| 193 | + */ |
|
| 194 | + const caps_read = 'read'; |
|
| 195 | + |
|
| 196 | + const caps_read_admin = 'read_admin'; |
|
| 197 | + |
|
| 198 | + const caps_edit = 'edit'; |
|
| 199 | + |
|
| 200 | + const caps_delete = 'delete'; |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Keys are all the cap contexts (ie constants EEM_Base::_caps_*) and values are their 'action' |
|
| 204 | + * as how they'd be used in capability names. Eg EEM_Base::caps_read ('read_frontend') |
|
| 205 | + * maps to 'read' because when looking for relevant permissions we're going to use |
|
| 206 | + * 'read' in teh capabilities names like 'ee_read_events' etc. |
|
| 207 | + * |
|
| 208 | + * @var array |
|
| 209 | + */ |
|
| 210 | + protected $_cap_contexts_to_cap_action_map = array( |
|
| 211 | + self::caps_read => 'read', |
|
| 212 | + self::caps_read_admin => 'read', |
|
| 213 | + self::caps_edit => 'edit', |
|
| 214 | + self::caps_delete => 'delete', |
|
| 215 | + ); |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Timezone |
|
| 219 | + * This gets set via the constructor so that we know what timezone incoming strings|timestamps are in when there |
|
| 220 | + * are EE_Datetime_Fields in use. This can also be used before a get to set what timezone you want strings coming |
|
| 221 | + * out of the created objects. NOT all EEM_Base child classes use this property but any that use a |
|
| 222 | + * EE_Datetime_Field data type will have access to it. |
|
| 223 | + * |
|
| 224 | + * @var string |
|
| 225 | + */ |
|
| 226 | + protected $_timezone; |
|
| 227 | + |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * This holds the id of the blog currently making the query. Has no bearing on single site but is used for |
|
| 231 | + * multisite. |
|
| 232 | + * |
|
| 233 | + * @var int |
|
| 234 | + */ |
|
| 235 | + protected static $_model_query_blog_id; |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * A copy of _fields, except the array keys are the model names pointed to by |
|
| 239 | + * the field |
|
| 240 | + * |
|
| 241 | + * @var EE_Model_Field_Base[] |
|
| 242 | + */ |
|
| 243 | + private $_cache_foreign_key_to_fields = array(); |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Cached list of all the fields on the model, indexed by their name |
|
| 247 | + * |
|
| 248 | + * @var EE_Model_Field_Base[] |
|
| 249 | + */ |
|
| 250 | + private $_cached_fields = null; |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Cached list of all the fields on the model, except those that are |
|
| 254 | + * marked as only pertinent to the database |
|
| 255 | + * |
|
| 256 | + * @var EE_Model_Field_Base[] |
|
| 257 | + */ |
|
| 258 | + private $_cached_fields_non_db_only = null; |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * A cached reference to the primary key for quick lookup |
|
| 262 | + * |
|
| 263 | + * @var EE_Model_Field_Base |
|
| 264 | + */ |
|
| 265 | + private $_primary_key_field = null; |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * Flag indicating whether this model has a primary key or not |
|
| 269 | + * |
|
| 270 | + * @var boolean |
|
| 271 | + */ |
|
| 272 | + protected $_has_primary_key_field = null; |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Whether or not this model is based off a table in WP core only (CPTs should set |
|
| 276 | + * this to FALSE, but if we were to make an EE_WP_Post model, it should set this to true). |
|
| 277 | + * This should be true for models that deal with data that should exist independent of EE. |
|
| 278 | + * For example, if the model can read and insert data that isn't used by EE, this should be true. |
|
| 279 | + * It would be false, however, if you could guarantee the model would only interact with EE data, |
|
| 280 | + * even if it uses a WP core table (eg event and venue models set this to false for that reason: |
|
| 281 | + * they can only read and insert events and venues custom post types, not arbitrary post types) |
|
| 282 | + * @var boolean |
|
| 283 | + */ |
|
| 284 | + protected $_wp_core_model = false; |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * @var bool stores whether this model has a password field or not. |
|
| 288 | + * null until initialized by hasPasswordField() |
|
| 289 | + */ |
|
| 290 | + protected $has_password_field; |
|
| 291 | 291 | |
| 292 | - /** |
|
| 293 | - * @var EE_Password_Field|null Automatically set when calling getPasswordField() |
|
| 294 | - */ |
|
| 295 | - protected $password_field; |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * List of valid operators that can be used for querying. |
|
| 299 | - * The keys are all operators we'll accept, the values are the real SQL |
|
| 300 | - * operators used |
|
| 301 | - * |
|
| 302 | - * @var array |
|
| 303 | - */ |
|
| 304 | - protected $_valid_operators = array( |
|
| 305 | - '=' => '=', |
|
| 306 | - '<=' => '<=', |
|
| 307 | - '<' => '<', |
|
| 308 | - '>=' => '>=', |
|
| 309 | - '>' => '>', |
|
| 310 | - '!=' => '!=', |
|
| 311 | - 'LIKE' => 'LIKE', |
|
| 312 | - 'like' => 'LIKE', |
|
| 313 | - 'NOT_LIKE' => 'NOT LIKE', |
|
| 314 | - 'not_like' => 'NOT LIKE', |
|
| 315 | - 'NOT LIKE' => 'NOT LIKE', |
|
| 316 | - 'not like' => 'NOT LIKE', |
|
| 317 | - 'IN' => 'IN', |
|
| 318 | - 'in' => 'IN', |
|
| 319 | - 'NOT_IN' => 'NOT IN', |
|
| 320 | - 'not_in' => 'NOT IN', |
|
| 321 | - 'NOT IN' => 'NOT IN', |
|
| 322 | - 'not in' => 'NOT IN', |
|
| 323 | - 'between' => 'BETWEEN', |
|
| 324 | - 'BETWEEN' => 'BETWEEN', |
|
| 325 | - 'IS_NOT_NULL' => 'IS NOT NULL', |
|
| 326 | - 'is_not_null' => 'IS NOT NULL', |
|
| 327 | - 'IS NOT NULL' => 'IS NOT NULL', |
|
| 328 | - 'is not null' => 'IS NOT NULL', |
|
| 329 | - 'IS_NULL' => 'IS NULL', |
|
| 330 | - 'is_null' => 'IS NULL', |
|
| 331 | - 'IS NULL' => 'IS NULL', |
|
| 332 | - 'is null' => 'IS NULL', |
|
| 333 | - 'REGEXP' => 'REGEXP', |
|
| 334 | - 'regexp' => 'REGEXP', |
|
| 335 | - 'NOT_REGEXP' => 'NOT REGEXP', |
|
| 336 | - 'not_regexp' => 'NOT REGEXP', |
|
| 337 | - 'NOT REGEXP' => 'NOT REGEXP', |
|
| 338 | - 'not regexp' => 'NOT REGEXP', |
|
| 339 | - ); |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * operators that work like 'IN', accepting a comma-separated list of values inside brackets. Eg '(1,2,3)' |
|
| 343 | - * |
|
| 344 | - * @var array |
|
| 345 | - */ |
|
| 346 | - protected $_in_style_operators = array('IN', 'NOT IN'); |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * operators that work like 'BETWEEN'. Typically used for datetime calculations, i.e. "BETWEEN '12-1-2011' AND |
|
| 350 | - * '12-31-2012'" |
|
| 351 | - * |
|
| 352 | - * @var array |
|
| 353 | - */ |
|
| 354 | - protected $_between_style_operators = array('BETWEEN'); |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Operators that work like SQL's like: input should be assumed to be a string, already prepared for a LIKE query. |
|
| 358 | - * @var array |
|
| 359 | - */ |
|
| 360 | - protected $_like_style_operators = array('LIKE', 'NOT LIKE'); |
|
| 361 | - /** |
|
| 362 | - * operators that are used for handling NUll and !NULL queries. Typically used for when checking if a row exists |
|
| 363 | - * on a join table. |
|
| 364 | - * |
|
| 365 | - * @var array |
|
| 366 | - */ |
|
| 367 | - protected $_null_style_operators = array('IS NOT NULL', 'IS NULL'); |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Allowed values for $query_params['order'] for ordering in queries |
|
| 371 | - * |
|
| 372 | - * @var array |
|
| 373 | - */ |
|
| 374 | - protected $_allowed_order_values = array('asc', 'desc', 'ASC', 'DESC'); |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * When these are keys in a WHERE or HAVING clause, they are handled much differently |
|
| 378 | - * than regular field names. It is assumed that their values are an array of WHERE conditions |
|
| 379 | - * |
|
| 380 | - * @var array |
|
| 381 | - */ |
|
| 382 | - private $_logic_query_param_keys = array('not', 'and', 'or', 'NOT', 'AND', 'OR'); |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * Allowed keys in $query_params arrays passed into queries. Note that 0 is meant to always be a |
|
| 386 | - * 'where', but 'where' clauses are so common that we thought we'd omit it |
|
| 387 | - * |
|
| 388 | - * @var array |
|
| 389 | - */ |
|
| 390 | - private $_allowed_query_params = array( |
|
| 391 | - 0, |
|
| 392 | - 'limit', |
|
| 393 | - 'order_by', |
|
| 394 | - 'group_by', |
|
| 395 | - 'having', |
|
| 396 | - 'force_join', |
|
| 397 | - 'order', |
|
| 398 | - 'on_join_limit', |
|
| 399 | - 'default_where_conditions', |
|
| 400 | - 'caps', |
|
| 401 | - 'extra_selects', |
|
| 402 | - 'exclude_protected', |
|
| 403 | - ); |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * All the data types that can be used in $wpdb->prepare statements. |
|
| 407 | - * |
|
| 408 | - * @var array |
|
| 409 | - */ |
|
| 410 | - private $_valid_wpdb_data_types = array('%d', '%s', '%f'); |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * @var EE_Registry $EE |
|
| 414 | - */ |
|
| 415 | - protected $EE = null; |
|
| 416 | - |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * Property which, when set, will have this model echo out the next X queries to the page for debugging. |
|
| 420 | - * |
|
| 421 | - * @var int |
|
| 422 | - */ |
|
| 423 | - protected $_show_next_x_db_queries = 0; |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * When using _get_all_wpdb_results, you can specify a custom selection. If you do so, |
|
| 427 | - * it gets saved on this property as an instance of CustomSelects so those selections can be used in |
|
| 428 | - * WHERE, GROUP_BY, etc. |
|
| 429 | - * |
|
| 430 | - * @var CustomSelects |
|
| 431 | - */ |
|
| 432 | - protected $_custom_selections = array(); |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * key => value Entity Map using array( EEM_Base::$_model_query_blog_id => array( ID => model object ) ) |
|
| 436 | - * caches every model object we've fetched from the DB on this request |
|
| 437 | - * |
|
| 438 | - * @var array |
|
| 439 | - */ |
|
| 440 | - protected $_entity_map; |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * @var LoaderInterface $loader |
|
| 444 | - */ |
|
| 445 | - private static $loader; |
|
| 446 | - |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * constant used to show EEM_Base has not yet verified the db on this http request |
|
| 450 | - */ |
|
| 451 | - const db_verified_none = 0; |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * constant used to show EEM_Base has verified the EE core db on this http request, |
|
| 455 | - * but not the addons' dbs |
|
| 456 | - */ |
|
| 457 | - const db_verified_core = 1; |
|
| 458 | - |
|
| 459 | - /** |
|
| 460 | - * constant used to show EEM_Base has verified the addons' dbs (and implicitly |
|
| 461 | - * the EE core db too) |
|
| 462 | - */ |
|
| 463 | - const db_verified_addons = 2; |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * indicates whether an EEM_Base child has already re-verified the DB |
|
| 467 | - * is ok (we don't want to do it repetitively). Should be set to one the constants |
|
| 468 | - * looking like EEM_Base::db_verified_* |
|
| 469 | - * |
|
| 470 | - * @var int - 0 = none, 1 = core, 2 = addons |
|
| 471 | - */ |
|
| 472 | - protected static $_db_verification_level = EEM_Base::db_verified_none; |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * @const constant for 'default_where_conditions' to apply default where conditions to ALL queried models |
|
| 476 | - * (eg, if retrieving registrations ordered by their datetimes, this will only return non-trashed |
|
| 477 | - * registrations for non-trashed tickets for non-trashed datetimes) |
|
| 478 | - */ |
|
| 479 | - const default_where_conditions_all = 'all'; |
|
| 480 | - |
|
| 481 | - /** |
|
| 482 | - * @const constant for 'default_where_conditions' to apply default where conditions to THIS model only, but |
|
| 483 | - * no other models which are joined to (eg, if retrieving registrations ordered by their datetimes, this will |
|
| 484 | - * return non-trashed registrations, regardless of the related datetimes and tickets' statuses). |
|
| 485 | - * It is preferred to use EEM_Base::default_where_conditions_minimum_others because, when joining to |
|
| 486 | - * models which share tables with other models, this can return data for the wrong model. |
|
| 487 | - */ |
|
| 488 | - const default_where_conditions_this_only = 'this_model_only'; |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * @const constant for 'default_where_conditions' to apply default where conditions to other models queried, |
|
| 492 | - * but not the current model (eg, if retrieving registrations ordered by their datetimes, this will |
|
| 493 | - * return all registrations related to non-trashed tickets and non-trashed datetimes) |
|
| 494 | - */ |
|
| 495 | - const default_where_conditions_others_only = 'other_models_only'; |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * @const constant for 'default_where_conditions' to apply minimum where conditions to all models queried. |
|
| 499 | - * For most models this the same as EEM_Base::default_where_conditions_none, except for models which share |
|
| 500 | - * their table with other models, like the Event and Venue models. For example, when querying for events |
|
| 501 | - * ordered by their venues' name, this will be sure to only return real events with associated real venues |
|
| 502 | - * (regardless of whether those events and venues are trashed) |
|
| 503 | - * In contrast, using EEM_Base::default_where_conditions_none would could return WP posts other than EE |
|
| 504 | - * events. |
|
| 505 | - */ |
|
| 506 | - const default_where_conditions_minimum_all = 'minimum'; |
|
| 507 | - |
|
| 508 | - /** |
|
| 509 | - * @const constant for 'default_where_conditions' to apply apply where conditions to other models, and full default |
|
| 510 | - * where conditions for the queried model (eg, when querying events ordered by venues' names, this will |
|
| 511 | - * return non-trashed events for any venues, regardless of whether those associated venues are trashed or |
|
| 512 | - * not) |
|
| 513 | - */ |
|
| 514 | - const default_where_conditions_minimum_others = 'full_this_minimum_others'; |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * @const constant for 'default_where_conditions' to NOT apply any where conditions. This should very rarely be |
|
| 518 | - * used, because when querying from a model which shares its table with another model (eg Events and Venues) |
|
| 519 | - * it's possible it will return table entries for other models. You should use |
|
| 520 | - * EEM_Base::default_where_conditions_minimum_all instead. |
|
| 521 | - */ |
|
| 522 | - const default_where_conditions_none = 'none'; |
|
| 523 | - |
|
| 524 | - |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * About all child constructors: |
|
| 528 | - * they should define the _tables, _fields and _model_relations arrays. |
|
| 529 | - * Should ALWAYS be called after child constructor. |
|
| 530 | - * In order to make the child constructors to be as simple as possible, this parent constructor |
|
| 531 | - * finalizes constructing all the object's attributes. |
|
| 532 | - * Generally, rather than requiring a child to code |
|
| 533 | - * $this->_tables = array( |
|
| 534 | - * 'Event_Post_Table' => new EE_Table('Event_Post_Table','wp_posts') |
|
| 535 | - * ...); |
|
| 536 | - * (thus repeating itself in the array key and in the constructor of the new EE_Table,) |
|
| 537 | - * each EE_Table has a function to set the table's alias after the constructor, using |
|
| 538 | - * the array key ('Event_Post_Table'), instead of repeating it. The model fields and model relations |
|
| 539 | - * do something similar. |
|
| 540 | - * |
|
| 541 | - * @param null $timezone |
|
| 542 | - * @throws EE_Error |
|
| 543 | - */ |
|
| 544 | - protected function __construct($timezone = null) |
|
| 545 | - { |
|
| 546 | - // check that the model has not been loaded too soon |
|
| 547 | - if (! did_action('AHEE__EE_System__load_espresso_addons')) { |
|
| 548 | - throw new EE_Error( |
|
| 549 | - sprintf( |
|
| 550 | - __( |
|
| 551 | - 'The %1$s model can not be loaded before the "AHEE__EE_System__load_espresso_addons" hook has been called. This gives other addons a chance to extend this model.', |
|
| 552 | - 'event_espresso' |
|
| 553 | - ), |
|
| 554 | - get_class($this) |
|
| 555 | - ) |
|
| 556 | - ); |
|
| 557 | - } |
|
| 558 | - /** |
|
| 559 | - * Set blogid for models to current blog. However we ONLY do this if $_model_query_blog_id is not already set. |
|
| 560 | - */ |
|
| 561 | - if (empty(EEM_Base::$_model_query_blog_id)) { |
|
| 562 | - EEM_Base::set_model_query_blog_id(); |
|
| 563 | - } |
|
| 564 | - /** |
|
| 565 | - * Filters the list of tables on a model. It is best to NOT use this directly and instead |
|
| 566 | - * just use EE_Register_Model_Extension |
|
| 567 | - * |
|
| 568 | - * @var EE_Table_Base[] $_tables |
|
| 569 | - */ |
|
| 570 | - $this->_tables = (array) apply_filters('FHEE__' . get_class($this) . '__construct__tables', $this->_tables); |
|
| 571 | - foreach ($this->_tables as $table_alias => $table_obj) { |
|
| 572 | - /** @var $table_obj EE_Table_Base */ |
|
| 573 | - $table_obj->_construct_finalize_with_alias($table_alias); |
|
| 574 | - if ($table_obj instanceof EE_Secondary_Table) { |
|
| 575 | - /** @var $table_obj EE_Secondary_Table */ |
|
| 576 | - $table_obj->_construct_finalize_set_table_to_join_with($this->_get_main_table()); |
|
| 577 | - } |
|
| 578 | - } |
|
| 579 | - /** |
|
| 580 | - * Filters the list of fields on a model. It is best to NOT use this directly and instead just use |
|
| 581 | - * EE_Register_Model_Extension |
|
| 582 | - * |
|
| 583 | - * @param EE_Model_Field_Base[] $_fields |
|
| 584 | - */ |
|
| 585 | - $this->_fields = (array) apply_filters('FHEE__' . get_class($this) . '__construct__fields', $this->_fields); |
|
| 586 | - $this->_invalidate_field_caches(); |
|
| 587 | - foreach ($this->_fields as $table_alias => $fields_for_table) { |
|
| 588 | - if (! array_key_exists($table_alias, $this->_tables)) { |
|
| 589 | - throw new EE_Error(sprintf(__( |
|
| 590 | - "Table alias %s does not exist in EEM_Base child's _tables array. Only tables defined are %s", |
|
| 591 | - 'event_espresso' |
|
| 592 | - ), $table_alias, implode(",", $this->_fields))); |
|
| 593 | - } |
|
| 594 | - foreach ($fields_for_table as $field_name => $field_obj) { |
|
| 595 | - /** @var $field_obj EE_Model_Field_Base | EE_Primary_Key_Field_Base */ |
|
| 596 | - // primary key field base has a slightly different _construct_finalize |
|
| 597 | - /** @var $field_obj EE_Model_Field_Base */ |
|
| 598 | - $field_obj->_construct_finalize($table_alias, $field_name, $this->get_this_model_name()); |
|
| 599 | - } |
|
| 600 | - } |
|
| 601 | - // everything is related to Extra_Meta |
|
| 602 | - if (get_class($this) !== 'EEM_Extra_Meta') { |
|
| 603 | - // make extra meta related to everything, but don't block deleting things just |
|
| 604 | - // because they have related extra meta info. For now just orphan those extra meta |
|
| 605 | - // in the future we should automatically delete them |
|
| 606 | - $this->_model_relations['Extra_Meta'] = new EE_Has_Many_Any_Relation(false); |
|
| 607 | - } |
|
| 608 | - // and change logs |
|
| 609 | - if (get_class($this) !== 'EEM_Change_Log') { |
|
| 610 | - $this->_model_relations['Change_Log'] = new EE_Has_Many_Any_Relation(false); |
|
| 611 | - } |
|
| 612 | - /** |
|
| 613 | - * Filters the list of relations on a model. It is best to NOT use this directly and instead just use |
|
| 614 | - * EE_Register_Model_Extension |
|
| 615 | - * |
|
| 616 | - * @param EE_Model_Relation_Base[] $_model_relations |
|
| 617 | - */ |
|
| 618 | - $this->_model_relations = (array) apply_filters( |
|
| 619 | - 'FHEE__' . get_class($this) . '__construct__model_relations', |
|
| 620 | - $this->_model_relations |
|
| 621 | - ); |
|
| 622 | - foreach ($this->_model_relations as $model_name => $relation_obj) { |
|
| 623 | - /** @var $relation_obj EE_Model_Relation_Base */ |
|
| 624 | - $relation_obj->_construct_finalize_set_models($this->get_this_model_name(), $model_name); |
|
| 625 | - } |
|
| 626 | - foreach ($this->_indexes as $index_name => $index_obj) { |
|
| 627 | - /** @var $index_obj EE_Index */ |
|
| 628 | - $index_obj->_construct_finalize($index_name, $this->get_this_model_name()); |
|
| 629 | - } |
|
| 630 | - $this->set_timezone($timezone); |
|
| 631 | - // finalize default where condition strategy, or set default |
|
| 632 | - if (! $this->_default_where_conditions_strategy) { |
|
| 633 | - // nothing was set during child constructor, so set default |
|
| 634 | - $this->_default_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
| 635 | - } |
|
| 636 | - $this->_default_where_conditions_strategy->_finalize_construct($this); |
|
| 637 | - if (! $this->_minimum_where_conditions_strategy) { |
|
| 638 | - // nothing was set during child constructor, so set default |
|
| 639 | - $this->_minimum_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
| 640 | - } |
|
| 641 | - $this->_minimum_where_conditions_strategy->_finalize_construct($this); |
|
| 642 | - // if the cap slug hasn't been set, and we haven't set it to false on purpose |
|
| 643 | - // to indicate to NOT set it, set it to the logical default |
|
| 644 | - if ($this->_caps_slug === null) { |
|
| 645 | - $this->_caps_slug = EEH_Inflector::pluralize_and_lower($this->get_this_model_name()); |
|
| 646 | - } |
|
| 647 | - // initialize the standard cap restriction generators if none were specified by the child constructor |
|
| 648 | - if ($this->_cap_restriction_generators !== false) { |
|
| 649 | - foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
|
| 650 | - if (! isset($this->_cap_restriction_generators[ $cap_context ])) { |
|
| 651 | - $this->_cap_restriction_generators[ $cap_context ] = apply_filters( |
|
| 652 | - 'FHEE__EEM_Base___construct__standard_cap_restriction_generator', |
|
| 653 | - new EE_Restriction_Generator_Protected(), |
|
| 654 | - $cap_context, |
|
| 655 | - $this |
|
| 656 | - ); |
|
| 657 | - } |
|
| 658 | - } |
|
| 659 | - } |
|
| 660 | - // if there are cap restriction generators, use them to make the default cap restrictions |
|
| 661 | - if ($this->_cap_restriction_generators !== false) { |
|
| 662 | - foreach ($this->_cap_restriction_generators as $context => $generator_object) { |
|
| 663 | - if (! $generator_object) { |
|
| 664 | - continue; |
|
| 665 | - } |
|
| 666 | - if (! $generator_object instanceof EE_Restriction_Generator_Base) { |
|
| 667 | - throw new EE_Error( |
|
| 668 | - sprintf( |
|
| 669 | - __( |
|
| 670 | - 'Index "%1$s" in the model %2$s\'s _cap_restriction_generators is not a child of EE_Restriction_Generator_Base. It should be that or NULL.', |
|
| 671 | - 'event_espresso' |
|
| 672 | - ), |
|
| 673 | - $context, |
|
| 674 | - $this->get_this_model_name() |
|
| 675 | - ) |
|
| 676 | - ); |
|
| 677 | - } |
|
| 678 | - $action = $this->cap_action_for_context($context); |
|
| 679 | - if (! $generator_object->construction_finalized()) { |
|
| 680 | - $generator_object->_construct_finalize($this, $action); |
|
| 681 | - } |
|
| 682 | - } |
|
| 683 | - } |
|
| 684 | - do_action('AHEE__' . get_class($this) . '__construct__end'); |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - |
|
| 688 | - |
|
| 689 | - /** |
|
| 690 | - * Used to set the $_model_query_blog_id static property. |
|
| 691 | - * |
|
| 692 | - * @param int $blog_id If provided then will set the blog_id for the models to this id. If not provided then the |
|
| 693 | - * value for get_current_blog_id() will be used. |
|
| 694 | - */ |
|
| 695 | - public static function set_model_query_blog_id($blog_id = 0) |
|
| 696 | - { |
|
| 697 | - EEM_Base::$_model_query_blog_id = $blog_id > 0 ? (int) $blog_id : get_current_blog_id(); |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - |
|
| 701 | - |
|
| 702 | - /** |
|
| 703 | - * Returns whatever is set as the internal $model_query_blog_id. |
|
| 704 | - * |
|
| 705 | - * @return int |
|
| 706 | - */ |
|
| 707 | - public static function get_model_query_blog_id() |
|
| 708 | - { |
|
| 709 | - return EEM_Base::$_model_query_blog_id; |
|
| 710 | - } |
|
| 711 | - |
|
| 712 | - |
|
| 713 | - |
|
| 714 | - /** |
|
| 715 | - * This function is a singleton method used to instantiate the Espresso_model object |
|
| 716 | - * |
|
| 717 | - * @param string $timezone string representing the timezone we want to set for returned Date Time Strings |
|
| 718 | - * (and any incoming timezone data that gets saved). |
|
| 719 | - * Note this just sends the timezone info to the date time model field objects. |
|
| 720 | - * Default is NULL |
|
| 721 | - * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
|
| 722 | - * @return static (as in the concrete child class) |
|
| 723 | - * @throws EE_Error |
|
| 724 | - * @throws InvalidArgumentException |
|
| 725 | - * @throws InvalidDataTypeException |
|
| 726 | - * @throws InvalidInterfaceException |
|
| 727 | - */ |
|
| 728 | - public static function instance($timezone = null) |
|
| 729 | - { |
|
| 730 | - // check if instance of Espresso_model already exists |
|
| 731 | - if (! static::$_instance instanceof static) { |
|
| 732 | - // instantiate Espresso_model |
|
| 733 | - static::$_instance = new static( |
|
| 734 | - $timezone, |
|
| 735 | - LoaderFactory::getLoader()->load('EventEspresso\core\services\orm\ModelFieldFactory') |
|
| 736 | - ); |
|
| 737 | - } |
|
| 738 | - // we might have a timezone set, let set_timezone decide what to do with it |
|
| 739 | - static::$_instance->set_timezone($timezone); |
|
| 740 | - // Espresso_model object |
|
| 741 | - return static::$_instance; |
|
| 742 | - } |
|
| 743 | - |
|
| 744 | - |
|
| 745 | - |
|
| 746 | - /** |
|
| 747 | - * resets the model and returns it |
|
| 748 | - * |
|
| 749 | - * @param null | string $timezone |
|
| 750 | - * @return EEM_Base|null (if the model was already instantiated, returns it, with |
|
| 751 | - * all its properties reset; if it wasn't instantiated, returns null) |
|
| 752 | - * @throws EE_Error |
|
| 753 | - * @throws ReflectionException |
|
| 754 | - * @throws InvalidArgumentException |
|
| 755 | - * @throws InvalidDataTypeException |
|
| 756 | - * @throws InvalidInterfaceException |
|
| 757 | - */ |
|
| 758 | - public static function reset($timezone = null) |
|
| 759 | - { |
|
| 760 | - if (static::$_instance instanceof EEM_Base) { |
|
| 761 | - // let's try to NOT swap out the current instance for a new one |
|
| 762 | - // because if someone has a reference to it, we can't remove their reference |
|
| 763 | - // so it's best to keep using the same reference, but change the original object |
|
| 764 | - // reset all its properties to their original values as defined in the class |
|
| 765 | - $r = new ReflectionClass(get_class(static::$_instance)); |
|
| 766 | - $static_properties = $r->getStaticProperties(); |
|
| 767 | - foreach ($r->getDefaultProperties() as $property => $value) { |
|
| 768 | - // don't set instance to null like it was originally, |
|
| 769 | - // but it's static anyways, and we're ignoring static properties (for now at least) |
|
| 770 | - if (! isset($static_properties[ $property ])) { |
|
| 771 | - static::$_instance->{$property} = $value; |
|
| 772 | - } |
|
| 773 | - } |
|
| 774 | - // and then directly call its constructor again, like we would if we were creating a new one |
|
| 775 | - static::$_instance->__construct( |
|
| 776 | - $timezone, |
|
| 777 | - LoaderFactory::getLoader()->load('EventEspresso\core\services\orm\ModelFieldFactory') |
|
| 778 | - ); |
|
| 779 | - return self::instance(); |
|
| 780 | - } |
|
| 781 | - return null; |
|
| 782 | - } |
|
| 783 | - |
|
| 784 | - |
|
| 785 | - |
|
| 786 | - /** |
|
| 787 | - * @return LoaderInterface |
|
| 788 | - * @throws InvalidArgumentException |
|
| 789 | - * @throws InvalidDataTypeException |
|
| 790 | - * @throws InvalidInterfaceException |
|
| 791 | - */ |
|
| 792 | - private static function getLoader() |
|
| 793 | - { |
|
| 794 | - if (! EEM_Base::$loader instanceof LoaderInterface) { |
|
| 795 | - EEM_Base::$loader = LoaderFactory::getLoader(); |
|
| 796 | - } |
|
| 797 | - return EEM_Base::$loader; |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - |
|
| 801 | - |
|
| 802 | - /** |
|
| 803 | - * retrieve the status details from esp_status table as an array IF this model has the status table as a relation. |
|
| 804 | - * |
|
| 805 | - * @param boolean $translated return localized strings or JUST the array. |
|
| 806 | - * @return array |
|
| 807 | - * @throws EE_Error |
|
| 808 | - * @throws InvalidArgumentException |
|
| 809 | - * @throws InvalidDataTypeException |
|
| 810 | - * @throws InvalidInterfaceException |
|
| 811 | - */ |
|
| 812 | - public function status_array($translated = false) |
|
| 813 | - { |
|
| 814 | - if (! array_key_exists('Status', $this->_model_relations)) { |
|
| 815 | - return array(); |
|
| 816 | - } |
|
| 817 | - $model_name = $this->get_this_model_name(); |
|
| 818 | - $status_type = str_replace(' ', '_', strtolower(str_replace('_', ' ', $model_name))); |
|
| 819 | - $stati = EEM_Status::instance()->get_all(array(array('STS_type' => $status_type))); |
|
| 820 | - $status_array = array(); |
|
| 821 | - foreach ($stati as $status) { |
|
| 822 | - $status_array[ $status->ID() ] = $status->get('STS_code'); |
|
| 823 | - } |
|
| 824 | - return $translated |
|
| 825 | - ? EEM_Status::instance()->localized_status($status_array, false, 'sentence') |
|
| 826 | - : $status_array; |
|
| 827 | - } |
|
| 828 | - |
|
| 829 | - |
|
| 830 | - |
|
| 831 | - /** |
|
| 832 | - * Gets all the EE_Base_Class objects which match the $query_params, by querying the DB. |
|
| 833 | - * |
|
| 834 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 835 | - * or if you have the development copy of EE you can view this at the path: |
|
| 836 | - * /docs/G--Model-System/model-query-params.md |
|
| 837 | - * @return EE_Base_Class[] *note that there is NO option to pass the output type. If you want results different |
|
| 838 | - * from EE_Base_Class[], use get_all_wpdb_results(). Array keys are object IDs (if there is a primary key on the model. |
|
| 839 | - * if not, numerically indexed) Some full examples: get 10 transactions |
|
| 840 | - * which have Scottish attendees: EEM_Transaction::instance()->get_all( |
|
| 841 | - * array( array( |
|
| 842 | - * 'OR'=>array( |
|
| 843 | - * 'Registration.Attendee.ATT_fname'=>array('like','Mc%'), |
|
| 844 | - * 'Registration.Attendee.ATT_fname*other'=>array('like','Mac%') |
|
| 845 | - * ) |
|
| 846 | - * ), |
|
| 847 | - * 'limit'=>10, |
|
| 848 | - * 'group_by'=>'TXN_ID' |
|
| 849 | - * )); |
|
| 850 | - * get all the answers to the question titled "shirt size" for event with id |
|
| 851 | - * 12, ordered by their answer EEM_Answer::instance()->get_all(array( array( |
|
| 852 | - * 'Question.QST_display_text'=>'shirt size', |
|
| 853 | - * 'Registration.Event.EVT_ID'=>12 |
|
| 854 | - * ), |
|
| 855 | - * 'order_by'=>array('ANS_value'=>'ASC') |
|
| 856 | - * )); |
|
| 857 | - * @throws EE_Error |
|
| 858 | - */ |
|
| 859 | - public function get_all($query_params = array()) |
|
| 860 | - { |
|
| 861 | - if (isset($query_params['limit']) |
|
| 862 | - && ! isset($query_params['group_by']) |
|
| 863 | - ) { |
|
| 864 | - $query_params['group_by'] = array_keys($this->get_combined_primary_key_fields()); |
|
| 865 | - } |
|
| 866 | - return $this->_create_objects($this->_get_all_wpdb_results($query_params, ARRAY_A, null)); |
|
| 867 | - } |
|
| 868 | - |
|
| 869 | - |
|
| 870 | - |
|
| 871 | - /** |
|
| 872 | - * Modifies the query parameters so we only get back model objects |
|
| 873 | - * that "belong" to the current user |
|
| 874 | - * |
|
| 875 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 876 | - * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 877 | - */ |
|
| 878 | - public function alter_query_params_to_only_include_mine($query_params = array()) |
|
| 879 | - { |
|
| 880 | - $wp_user_field_name = $this->wp_user_field_name(); |
|
| 881 | - if ($wp_user_field_name) { |
|
| 882 | - $query_params[0][ $wp_user_field_name ] = get_current_user_id(); |
|
| 883 | - } |
|
| 884 | - return $query_params; |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - |
|
| 888 | - |
|
| 889 | - /** |
|
| 890 | - * Returns the name of the field's name that points to the WP_User table |
|
| 891 | - * on this model (or follows the _model_chain_to_wp_user and uses that model's |
|
| 892 | - * foreign key to the WP_User table) |
|
| 893 | - * |
|
| 894 | - * @return string|boolean string on success, boolean false when there is no |
|
| 895 | - * foreign key to the WP_User table |
|
| 896 | - */ |
|
| 897 | - public function wp_user_field_name() |
|
| 898 | - { |
|
| 899 | - try { |
|
| 900 | - if (! empty($this->_model_chain_to_wp_user)) { |
|
| 901 | - $models_to_follow_to_wp_users = explode('.', $this->_model_chain_to_wp_user); |
|
| 902 | - $last_model_name = end($models_to_follow_to_wp_users); |
|
| 903 | - $model_with_fk_to_wp_users = EE_Registry::instance()->load_model($last_model_name); |
|
| 904 | - $model_chain_to_wp_user = $this->_model_chain_to_wp_user . '.'; |
|
| 905 | - } else { |
|
| 906 | - $model_with_fk_to_wp_users = $this; |
|
| 907 | - $model_chain_to_wp_user = ''; |
|
| 908 | - } |
|
| 909 | - $wp_user_field = $model_with_fk_to_wp_users->get_foreign_key_to('WP_User'); |
|
| 910 | - return $model_chain_to_wp_user . $wp_user_field->get_name(); |
|
| 911 | - } catch (EE_Error $e) { |
|
| 912 | - return false; |
|
| 913 | - } |
|
| 914 | - } |
|
| 915 | - |
|
| 916 | - |
|
| 917 | - |
|
| 918 | - /** |
|
| 919 | - * Returns the _model_chain_to_wp_user string, which indicates which related model |
|
| 920 | - * (or transiently-related model) has a foreign key to the wp_users table; |
|
| 921 | - * useful for finding if model objects of this type are 'owned' by the current user. |
|
| 922 | - * This is an empty string when the foreign key is on this model and when it isn't, |
|
| 923 | - * but is only non-empty when this model's ownership is indicated by a RELATED model |
|
| 924 | - * (or transiently-related model) |
|
| 925 | - * |
|
| 926 | - * @return string |
|
| 927 | - */ |
|
| 928 | - public function model_chain_to_wp_user() |
|
| 929 | - { |
|
| 930 | - return $this->_model_chain_to_wp_user; |
|
| 931 | - } |
|
| 932 | - |
|
| 933 | - |
|
| 934 | - |
|
| 935 | - /** |
|
| 936 | - * Whether this model is 'owned' by a specific wordpress user (even indirectly, |
|
| 937 | - * like how registrations don't have a foreign key to wp_users, but the |
|
| 938 | - * events they are for are), or is unrelated to wp users. |
|
| 939 | - * generally available |
|
| 940 | - * |
|
| 941 | - * @return boolean |
|
| 942 | - */ |
|
| 943 | - public function is_owned() |
|
| 944 | - { |
|
| 945 | - if ($this->model_chain_to_wp_user()) { |
|
| 946 | - return true; |
|
| 947 | - } |
|
| 948 | - try { |
|
| 949 | - $this->get_foreign_key_to('WP_User'); |
|
| 950 | - return true; |
|
| 951 | - } catch (EE_Error $e) { |
|
| 952 | - return false; |
|
| 953 | - } |
|
| 954 | - } |
|
| 955 | - |
|
| 956 | - |
|
| 957 | - /** |
|
| 958 | - * Used internally to get WPDB results, because other functions, besides get_all, may want to do some queries, but |
|
| 959 | - * may want to preserve the WPDB results (eg, update, which first queries to make sure we have all the tables on |
|
| 960 | - * the model) |
|
| 961 | - * |
|
| 962 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 963 | - * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
| 964 | - * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
| 965 | - * fields on the model, and the models we joined to in the query. However, you can |
|
| 966 | - * override this and set the select to "*", or a specific column name, like |
|
| 967 | - * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
| 968 | - * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
| 969 | - * the aliases used to refer to this selection, and values are to be |
|
| 970 | - * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
| 971 | - * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
| 972 | - * @return array | stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
| 973 | - * @throws EE_Error |
|
| 974 | - * @throws InvalidArgumentException |
|
| 975 | - */ |
|
| 976 | - protected function _get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
|
| 977 | - { |
|
| 978 | - $this->_custom_selections = $this->getCustomSelection($query_params, $columns_to_select); |
|
| 979 | - ; |
|
| 980 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 981 | - $select_expressions = $columns_to_select === null |
|
| 982 | - ? $this->_construct_default_select_sql($model_query_info) |
|
| 983 | - : ''; |
|
| 984 | - if ($this->_custom_selections instanceof CustomSelects) { |
|
| 985 | - $custom_expressions = $this->_custom_selections->columnsToSelectExpression(); |
|
| 986 | - $select_expressions .= $select_expressions |
|
| 987 | - ? ', ' . $custom_expressions |
|
| 988 | - : $custom_expressions; |
|
| 989 | - } |
|
| 990 | - |
|
| 991 | - $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 992 | - return $this->_do_wpdb_query('get_results', array($SQL, $output)); |
|
| 993 | - } |
|
| 994 | - |
|
| 995 | - |
|
| 996 | - /** |
|
| 997 | - * Get a CustomSelects object if the $query_params or $columns_to_select allows for it. |
|
| 998 | - * Note: $query_params['extra_selects'] will always override any $columns_to_select values. It is the preferred |
|
| 999 | - * method of including extra select information. |
|
| 1000 | - * |
|
| 1001 | - * @param array $query_params |
|
| 1002 | - * @param null|array|string $columns_to_select |
|
| 1003 | - * @return null|CustomSelects |
|
| 1004 | - * @throws InvalidArgumentException |
|
| 1005 | - */ |
|
| 1006 | - protected function getCustomSelection(array $query_params, $columns_to_select = null) |
|
| 1007 | - { |
|
| 1008 | - if (! isset($query_params['extra_selects']) && $columns_to_select === null) { |
|
| 1009 | - return null; |
|
| 1010 | - } |
|
| 1011 | - $selects = isset($query_params['extra_selects']) ? $query_params['extra_selects'] : $columns_to_select; |
|
| 1012 | - $selects = is_string($selects) ? explode(',', $selects) : $selects; |
|
| 1013 | - return new CustomSelects($selects); |
|
| 1014 | - } |
|
| 1015 | - |
|
| 1016 | - |
|
| 1017 | - |
|
| 1018 | - /** |
|
| 1019 | - * Gets an array of rows from the database just like $wpdb->get_results would, |
|
| 1020 | - * but you can use the model query params to more easily |
|
| 1021 | - * take care of joins, field preparation etc. |
|
| 1022 | - * |
|
| 1023 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1024 | - * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
| 1025 | - * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
| 1026 | - * fields on the model, and the models we joined to in the query. However, you can |
|
| 1027 | - * override this and set the select to "*", or a specific column name, like |
|
| 1028 | - * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
| 1029 | - * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
| 1030 | - * the aliases used to refer to this selection, and values are to be |
|
| 1031 | - * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
| 1032 | - * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
| 1033 | - * @return array|stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
| 1034 | - * @throws EE_Error |
|
| 1035 | - */ |
|
| 1036 | - public function get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
|
| 1037 | - { |
|
| 1038 | - return $this->_get_all_wpdb_results($query_params, $output, $columns_to_select); |
|
| 1039 | - } |
|
| 1040 | - |
|
| 1041 | - |
|
| 1042 | - |
|
| 1043 | - /** |
|
| 1044 | - * For creating a custom select statement |
|
| 1045 | - * |
|
| 1046 | - * @param mixed $columns_to_select either a string to be inserted directly as the select statement, |
|
| 1047 | - * or an array where keys are aliases, and values are arrays where 0=>the selection |
|
| 1048 | - * SQL, and 1=>is the datatype |
|
| 1049 | - * @throws EE_Error |
|
| 1050 | - * @return string |
|
| 1051 | - */ |
|
| 1052 | - private function _construct_select_from_input($columns_to_select) |
|
| 1053 | - { |
|
| 1054 | - if (is_array($columns_to_select)) { |
|
| 1055 | - $select_sql_array = array(); |
|
| 1056 | - foreach ($columns_to_select as $alias => $selection_and_datatype) { |
|
| 1057 | - if (! is_array($selection_and_datatype) || ! isset($selection_and_datatype[1])) { |
|
| 1058 | - throw new EE_Error( |
|
| 1059 | - sprintf( |
|
| 1060 | - __( |
|
| 1061 | - "Custom selection %s (alias %s) needs to be an array like array('COUNT(REG_ID)','%%d')", |
|
| 1062 | - 'event_espresso' |
|
| 1063 | - ), |
|
| 1064 | - $selection_and_datatype, |
|
| 1065 | - $alias |
|
| 1066 | - ) |
|
| 1067 | - ); |
|
| 1068 | - } |
|
| 1069 | - if (! in_array($selection_and_datatype[1], $this->_valid_wpdb_data_types, true)) { |
|
| 1070 | - throw new EE_Error( |
|
| 1071 | - sprintf( |
|
| 1072 | - esc_html__( |
|
| 1073 | - "Datatype %s (for selection '%s' and alias '%s') is not a valid wpdb datatype (eg %%s)", |
|
| 1074 | - 'event_espresso' |
|
| 1075 | - ), |
|
| 1076 | - $selection_and_datatype[1], |
|
| 1077 | - $selection_and_datatype[0], |
|
| 1078 | - $alias, |
|
| 1079 | - implode(', ', $this->_valid_wpdb_data_types) |
|
| 1080 | - ) |
|
| 1081 | - ); |
|
| 1082 | - } |
|
| 1083 | - $select_sql_array[] = "{$selection_and_datatype[0]} AS $alias"; |
|
| 1084 | - } |
|
| 1085 | - $columns_to_select_string = implode(', ', $select_sql_array); |
|
| 1086 | - } else { |
|
| 1087 | - $columns_to_select_string = $columns_to_select; |
|
| 1088 | - } |
|
| 1089 | - return $columns_to_select_string; |
|
| 1090 | - } |
|
| 1091 | - |
|
| 1092 | - |
|
| 1093 | - |
|
| 1094 | - /** |
|
| 1095 | - * Convenient wrapper for getting the primary key field's name. Eg, on Registration, this would be 'REG_ID' |
|
| 1096 | - * |
|
| 1097 | - * @return string |
|
| 1098 | - * @throws EE_Error |
|
| 1099 | - */ |
|
| 1100 | - public function primary_key_name() |
|
| 1101 | - { |
|
| 1102 | - return $this->get_primary_key_field()->get_name(); |
|
| 1103 | - } |
|
| 1104 | - |
|
| 1105 | - |
|
| 1106 | - |
|
| 1107 | - /** |
|
| 1108 | - * Gets a single item for this model from the DB, given only its ID (or null if none is found). |
|
| 1109 | - * If there is no primary key on this model, $id is treated as primary key string |
|
| 1110 | - * |
|
| 1111 | - * @param mixed $id int or string, depending on the type of the model's primary key |
|
| 1112 | - * @return EE_Base_Class |
|
| 1113 | - */ |
|
| 1114 | - public function get_one_by_ID($id) |
|
| 1115 | - { |
|
| 1116 | - if ($this->get_from_entity_map($id)) { |
|
| 1117 | - return $this->get_from_entity_map($id); |
|
| 1118 | - } |
|
| 1119 | - return $this->get_one( |
|
| 1120 | - $this->alter_query_params_to_restrict_by_ID( |
|
| 1121 | - $id, |
|
| 1122 | - array('default_where_conditions' => EEM_Base::default_where_conditions_minimum_all) |
|
| 1123 | - ) |
|
| 1124 | - ); |
|
| 1125 | - } |
|
| 1126 | - |
|
| 1127 | - |
|
| 1128 | - |
|
| 1129 | - /** |
|
| 1130 | - * Alters query parameters to only get items with this ID are returned. |
|
| 1131 | - * Takes into account that the ID might be a string produced by EEM_Base::get_index_primary_key_string(), |
|
| 1132 | - * or could just be a simple primary key ID |
|
| 1133 | - * |
|
| 1134 | - * @param int $id |
|
| 1135 | - * @param array $query_params |
|
| 1136 | - * @return array of normal query params, @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1137 | - * @throws EE_Error |
|
| 1138 | - */ |
|
| 1139 | - public function alter_query_params_to_restrict_by_ID($id, $query_params = array()) |
|
| 1140 | - { |
|
| 1141 | - if (! isset($query_params[0])) { |
|
| 1142 | - $query_params[0] = array(); |
|
| 1143 | - } |
|
| 1144 | - $conditions_from_id = $this->parse_index_primary_key_string($id); |
|
| 1145 | - if ($conditions_from_id === null) { |
|
| 1146 | - $query_params[0][ $this->primary_key_name() ] = $id; |
|
| 1147 | - } else { |
|
| 1148 | - // no primary key, so the $id must be from the get_index_primary_key_string() |
|
| 1149 | - $query_params[0] = array_replace_recursive($query_params[0], $this->parse_index_primary_key_string($id)); |
|
| 1150 | - } |
|
| 1151 | - return $query_params; |
|
| 1152 | - } |
|
| 1153 | - |
|
| 1154 | - |
|
| 1155 | - |
|
| 1156 | - /** |
|
| 1157 | - * Gets a single item for this model from the DB, given the $query_params. Only returns a single class, not an |
|
| 1158 | - * array. If no item is found, null is returned. |
|
| 1159 | - * |
|
| 1160 | - * @param array $query_params like EEM_Base's $query_params variable. |
|
| 1161 | - * @return EE_Base_Class|EE_Soft_Delete_Base_Class|NULL |
|
| 1162 | - * @throws EE_Error |
|
| 1163 | - */ |
|
| 1164 | - public function get_one($query_params = array()) |
|
| 1165 | - { |
|
| 1166 | - if (! is_array($query_params)) { |
|
| 1167 | - EE_Error::doing_it_wrong( |
|
| 1168 | - 'EEM_Base::get_one', |
|
| 1169 | - sprintf( |
|
| 1170 | - __('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1171 | - gettype($query_params) |
|
| 1172 | - ), |
|
| 1173 | - '4.6.0' |
|
| 1174 | - ); |
|
| 1175 | - $query_params = array(); |
|
| 1176 | - } |
|
| 1177 | - $query_params['limit'] = 1; |
|
| 1178 | - $items = $this->get_all($query_params); |
|
| 1179 | - if (empty($items)) { |
|
| 1180 | - return null; |
|
| 1181 | - } |
|
| 1182 | - return array_shift($items); |
|
| 1183 | - } |
|
| 1184 | - |
|
| 1185 | - |
|
| 1186 | - |
|
| 1187 | - /** |
|
| 1188 | - * Returns the next x number of items in sequence from the given value as |
|
| 1189 | - * found in the database matching the given query conditions. |
|
| 1190 | - * |
|
| 1191 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1192 | - * @param null $field_to_order_by What field is used for the |
|
| 1193 | - * reference point. |
|
| 1194 | - * @param int $limit How many to return. |
|
| 1195 | - * @param array $query_params Extra conditions on the query. |
|
| 1196 | - * @param null $columns_to_select If left null, then an array of |
|
| 1197 | - * EE_Base_Class objects is returned, |
|
| 1198 | - * otherwise you can indicate just the |
|
| 1199 | - * columns you want returned. |
|
| 1200 | - * @return EE_Base_Class[]|array |
|
| 1201 | - * @throws EE_Error |
|
| 1202 | - */ |
|
| 1203 | - public function next_x( |
|
| 1204 | - $current_field_value, |
|
| 1205 | - $field_to_order_by = null, |
|
| 1206 | - $limit = 1, |
|
| 1207 | - $query_params = array(), |
|
| 1208 | - $columns_to_select = null |
|
| 1209 | - ) { |
|
| 1210 | - return $this->_get_consecutive( |
|
| 1211 | - $current_field_value, |
|
| 1212 | - '>', |
|
| 1213 | - $field_to_order_by, |
|
| 1214 | - $limit, |
|
| 1215 | - $query_params, |
|
| 1216 | - $columns_to_select |
|
| 1217 | - ); |
|
| 1218 | - } |
|
| 1219 | - |
|
| 1220 | - |
|
| 1221 | - |
|
| 1222 | - /** |
|
| 1223 | - * Returns the previous x number of items in sequence from the given value |
|
| 1224 | - * as found in the database matching the given query conditions. |
|
| 1225 | - * |
|
| 1226 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1227 | - * @param null $field_to_order_by What field is used for the |
|
| 1228 | - * reference point. |
|
| 1229 | - * @param int $limit How many to return. |
|
| 1230 | - * @param array $query_params Extra conditions on the query. |
|
| 1231 | - * @param null $columns_to_select If left null, then an array of |
|
| 1232 | - * EE_Base_Class objects is returned, |
|
| 1233 | - * otherwise you can indicate just the |
|
| 1234 | - * columns you want returned. |
|
| 1235 | - * @return EE_Base_Class[]|array |
|
| 1236 | - * @throws EE_Error |
|
| 1237 | - */ |
|
| 1238 | - public function previous_x( |
|
| 1239 | - $current_field_value, |
|
| 1240 | - $field_to_order_by = null, |
|
| 1241 | - $limit = 1, |
|
| 1242 | - $query_params = array(), |
|
| 1243 | - $columns_to_select = null |
|
| 1244 | - ) { |
|
| 1245 | - return $this->_get_consecutive( |
|
| 1246 | - $current_field_value, |
|
| 1247 | - '<', |
|
| 1248 | - $field_to_order_by, |
|
| 1249 | - $limit, |
|
| 1250 | - $query_params, |
|
| 1251 | - $columns_to_select |
|
| 1252 | - ); |
|
| 1253 | - } |
|
| 1254 | - |
|
| 1255 | - |
|
| 1256 | - |
|
| 1257 | - /** |
|
| 1258 | - * Returns the next item in sequence from the given value as found in the |
|
| 1259 | - * database matching the given query conditions. |
|
| 1260 | - * |
|
| 1261 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1262 | - * @param null $field_to_order_by What field is used for the |
|
| 1263 | - * reference point. |
|
| 1264 | - * @param array $query_params Extra conditions on the query. |
|
| 1265 | - * @param null $columns_to_select If left null, then an EE_Base_Class |
|
| 1266 | - * object is returned, otherwise you |
|
| 1267 | - * can indicate just the columns you |
|
| 1268 | - * want and a single array indexed by |
|
| 1269 | - * the columns will be returned. |
|
| 1270 | - * @return EE_Base_Class|null|array() |
|
| 1271 | - * @throws EE_Error |
|
| 1272 | - */ |
|
| 1273 | - public function next( |
|
| 1274 | - $current_field_value, |
|
| 1275 | - $field_to_order_by = null, |
|
| 1276 | - $query_params = array(), |
|
| 1277 | - $columns_to_select = null |
|
| 1278 | - ) { |
|
| 1279 | - $results = $this->_get_consecutive( |
|
| 1280 | - $current_field_value, |
|
| 1281 | - '>', |
|
| 1282 | - $field_to_order_by, |
|
| 1283 | - 1, |
|
| 1284 | - $query_params, |
|
| 1285 | - $columns_to_select |
|
| 1286 | - ); |
|
| 1287 | - return empty($results) ? null : reset($results); |
|
| 1288 | - } |
|
| 1289 | - |
|
| 1290 | - |
|
| 1291 | - |
|
| 1292 | - /** |
|
| 1293 | - * Returns the previous item in sequence from the given value as found in |
|
| 1294 | - * the database matching the given query conditions. |
|
| 1295 | - * |
|
| 1296 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1297 | - * @param null $field_to_order_by What field is used for the |
|
| 1298 | - * reference point. |
|
| 1299 | - * @param array $query_params Extra conditions on the query. |
|
| 1300 | - * @param null $columns_to_select If left null, then an EE_Base_Class |
|
| 1301 | - * object is returned, otherwise you |
|
| 1302 | - * can indicate just the columns you |
|
| 1303 | - * want and a single array indexed by |
|
| 1304 | - * the columns will be returned. |
|
| 1305 | - * @return EE_Base_Class|null|array() |
|
| 1306 | - * @throws EE_Error |
|
| 1307 | - */ |
|
| 1308 | - public function previous( |
|
| 1309 | - $current_field_value, |
|
| 1310 | - $field_to_order_by = null, |
|
| 1311 | - $query_params = array(), |
|
| 1312 | - $columns_to_select = null |
|
| 1313 | - ) { |
|
| 1314 | - $results = $this->_get_consecutive( |
|
| 1315 | - $current_field_value, |
|
| 1316 | - '<', |
|
| 1317 | - $field_to_order_by, |
|
| 1318 | - 1, |
|
| 1319 | - $query_params, |
|
| 1320 | - $columns_to_select |
|
| 1321 | - ); |
|
| 1322 | - return empty($results) ? null : reset($results); |
|
| 1323 | - } |
|
| 1324 | - |
|
| 1325 | - |
|
| 1326 | - |
|
| 1327 | - /** |
|
| 1328 | - * Returns the a consecutive number of items in sequence from the given |
|
| 1329 | - * value as found in the database matching the given query conditions. |
|
| 1330 | - * |
|
| 1331 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1332 | - * @param string $operand What operand is used for the sequence. |
|
| 1333 | - * @param string $field_to_order_by What field is used for the reference point. |
|
| 1334 | - * @param int $limit How many to return. |
|
| 1335 | - * @param array $query_params Extra conditions on the query. |
|
| 1336 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, |
|
| 1337 | - * otherwise you can indicate just the columns you want returned. |
|
| 1338 | - * @return EE_Base_Class[]|array |
|
| 1339 | - * @throws EE_Error |
|
| 1340 | - */ |
|
| 1341 | - protected function _get_consecutive( |
|
| 1342 | - $current_field_value, |
|
| 1343 | - $operand = '>', |
|
| 1344 | - $field_to_order_by = null, |
|
| 1345 | - $limit = 1, |
|
| 1346 | - $query_params = array(), |
|
| 1347 | - $columns_to_select = null |
|
| 1348 | - ) { |
|
| 1349 | - // if $field_to_order_by is empty then let's assume we're ordering by the primary key. |
|
| 1350 | - if (empty($field_to_order_by)) { |
|
| 1351 | - if ($this->has_primary_key_field()) { |
|
| 1352 | - $field_to_order_by = $this->get_primary_key_field()->get_name(); |
|
| 1353 | - } else { |
|
| 1354 | - if (WP_DEBUG) { |
|
| 1355 | - throw new EE_Error(__( |
|
| 1356 | - 'EEM_Base::_get_consecutive() has been called with no $field_to_order_by argument and there is no primary key on the field. Please provide the field you would like to use as the base for retrieving the next item(s).', |
|
| 1357 | - 'event_espresso' |
|
| 1358 | - )); |
|
| 1359 | - } |
|
| 1360 | - EE_Error::add_error(__('There was an error with the query.', 'event_espresso')); |
|
| 1361 | - return array(); |
|
| 1362 | - } |
|
| 1363 | - } |
|
| 1364 | - if (! is_array($query_params)) { |
|
| 1365 | - EE_Error::doing_it_wrong( |
|
| 1366 | - 'EEM_Base::_get_consecutive', |
|
| 1367 | - sprintf( |
|
| 1368 | - __('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1369 | - gettype($query_params) |
|
| 1370 | - ), |
|
| 1371 | - '4.6.0' |
|
| 1372 | - ); |
|
| 1373 | - $query_params = array(); |
|
| 1374 | - } |
|
| 1375 | - // let's add the where query param for consecutive look up. |
|
| 1376 | - $query_params[0][ $field_to_order_by ] = array($operand, $current_field_value); |
|
| 1377 | - $query_params['limit'] = $limit; |
|
| 1378 | - // set direction |
|
| 1379 | - $incoming_orderby = isset($query_params['order_by']) ? (array) $query_params['order_by'] : array(); |
|
| 1380 | - $query_params['order_by'] = $operand === '>' |
|
| 1381 | - ? array($field_to_order_by => 'ASC') + $incoming_orderby |
|
| 1382 | - : array($field_to_order_by => 'DESC') + $incoming_orderby; |
|
| 1383 | - // if $columns_to_select is empty then that means we're returning EE_Base_Class objects |
|
| 1384 | - if (empty($columns_to_select)) { |
|
| 1385 | - return $this->get_all($query_params); |
|
| 1386 | - } |
|
| 1387 | - // getting just the fields |
|
| 1388 | - return $this->_get_all_wpdb_results($query_params, ARRAY_A, $columns_to_select); |
|
| 1389 | - } |
|
| 1390 | - |
|
| 1391 | - |
|
| 1392 | - |
|
| 1393 | - /** |
|
| 1394 | - * This sets the _timezone property after model object has been instantiated. |
|
| 1395 | - * |
|
| 1396 | - * @param null | string $timezone valid PHP DateTimeZone timezone string |
|
| 1397 | - */ |
|
| 1398 | - public function set_timezone($timezone) |
|
| 1399 | - { |
|
| 1400 | - if ($timezone !== null) { |
|
| 1401 | - $this->_timezone = $timezone; |
|
| 1402 | - } |
|
| 1403 | - // note we need to loop through relations and set the timezone on those objects as well. |
|
| 1404 | - foreach ($this->_model_relations as $relation) { |
|
| 1405 | - $relation->set_timezone($timezone); |
|
| 1406 | - } |
|
| 1407 | - // and finally we do the same for any datetime fields |
|
| 1408 | - foreach ($this->_fields as $field) { |
|
| 1409 | - if ($field instanceof EE_Datetime_Field) { |
|
| 1410 | - $field->set_timezone($timezone); |
|
| 1411 | - } |
|
| 1412 | - } |
|
| 1413 | - } |
|
| 1414 | - |
|
| 1415 | - |
|
| 1416 | - |
|
| 1417 | - /** |
|
| 1418 | - * This just returns whatever is set for the current timezone. |
|
| 1419 | - * |
|
| 1420 | - * @access public |
|
| 1421 | - * @return string |
|
| 1422 | - */ |
|
| 1423 | - public function get_timezone() |
|
| 1424 | - { |
|
| 1425 | - // first validate if timezone is set. If not, then let's set it be whatever is set on the model fields. |
|
| 1426 | - if (empty($this->_timezone)) { |
|
| 1427 | - foreach ($this->_fields as $field) { |
|
| 1428 | - if ($field instanceof EE_Datetime_Field) { |
|
| 1429 | - $this->set_timezone($field->get_timezone()); |
|
| 1430 | - break; |
|
| 1431 | - } |
|
| 1432 | - } |
|
| 1433 | - } |
|
| 1434 | - // if timezone STILL empty then return the default timezone for the site. |
|
| 1435 | - if (empty($this->_timezone)) { |
|
| 1436 | - $this->set_timezone(EEH_DTT_Helper::get_timezone()); |
|
| 1437 | - } |
|
| 1438 | - return $this->_timezone; |
|
| 1439 | - } |
|
| 1440 | - |
|
| 1441 | - |
|
| 1442 | - |
|
| 1443 | - /** |
|
| 1444 | - * This returns the date formats set for the given field name and also ensures that |
|
| 1445 | - * $this->_timezone property is set correctly. |
|
| 1446 | - * |
|
| 1447 | - * @since 4.6.x |
|
| 1448 | - * @param string $field_name The name of the field the formats are being retrieved for. |
|
| 1449 | - * @param bool $pretty Whether to return the pretty formats (true) or not (false). |
|
| 1450 | - * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
| 1451 | - * @return array formats in an array with the date format first, and the time format last. |
|
| 1452 | - */ |
|
| 1453 | - public function get_formats_for($field_name, $pretty = false) |
|
| 1454 | - { |
|
| 1455 | - $field_settings = $this->field_settings_for($field_name); |
|
| 1456 | - // if not a valid EE_Datetime_Field then throw error |
|
| 1457 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1458 | - throw new EE_Error(sprintf(__( |
|
| 1459 | - 'The field sent into EEM_Base::get_formats_for (%s) is not registered as a EE_Datetime_Field. Please check the spelling and make sure you are submitting the right field name to retrieve date_formats for.', |
|
| 1460 | - 'event_espresso' |
|
| 1461 | - ), $field_name)); |
|
| 1462 | - } |
|
| 1463 | - // while we are here, let's make sure the timezone internally in EEM_Base matches what is stored on |
|
| 1464 | - // the field. |
|
| 1465 | - $this->_timezone = $field_settings->get_timezone(); |
|
| 1466 | - return array($field_settings->get_date_format($pretty), $field_settings->get_time_format($pretty)); |
|
| 1467 | - } |
|
| 1468 | - |
|
| 1469 | - |
|
| 1470 | - |
|
| 1471 | - /** |
|
| 1472 | - * This returns the current time in a format setup for a query on this model. |
|
| 1473 | - * Usage of this method makes it easier to setup queries against EE_Datetime_Field columns because |
|
| 1474 | - * it will return: |
|
| 1475 | - * - a formatted string in the timezone and format currently set on the EE_Datetime_Field for the given field for |
|
| 1476 | - * NOW |
|
| 1477 | - * - or a unix timestamp (equivalent to time()) |
|
| 1478 | - * Note: When requesting a formatted string, if the date or time format doesn't include seconds, for example, |
|
| 1479 | - * the time returned, because it uses that format, will also NOT include seconds. For this reason, if you want |
|
| 1480 | - * the time returned to be the current time down to the exact second, set $timestamp to true. |
|
| 1481 | - * @since 4.6.x |
|
| 1482 | - * @param string $field_name The field the current time is needed for. |
|
| 1483 | - * @param bool $timestamp True means to return a unix timestamp. Otherwise a |
|
| 1484 | - * formatted string matching the set format for the field in the set timezone will |
|
| 1485 | - * be returned. |
|
| 1486 | - * @param string $what Whether to return the string in just the time format, the date format, or both. |
|
| 1487 | - * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
| 1488 | - * @return int|string If the given field_name is not of the EE_Datetime_Field type, then an EE_Error |
|
| 1489 | - * exception is triggered. |
|
| 1490 | - */ |
|
| 1491 | - public function current_time_for_query($field_name, $timestamp = false, $what = 'both') |
|
| 1492 | - { |
|
| 1493 | - $formats = $this->get_formats_for($field_name); |
|
| 1494 | - $DateTime = new DateTime("now", new DateTimeZone($this->_timezone)); |
|
| 1495 | - if ($timestamp) { |
|
| 1496 | - return $DateTime->format('U'); |
|
| 1497 | - } |
|
| 1498 | - // not returning timestamp, so return formatted string in timezone. |
|
| 1499 | - switch ($what) { |
|
| 1500 | - case 'time': |
|
| 1501 | - return $DateTime->format($formats[1]); |
|
| 1502 | - break; |
|
| 1503 | - case 'date': |
|
| 1504 | - return $DateTime->format($formats[0]); |
|
| 1505 | - break; |
|
| 1506 | - default: |
|
| 1507 | - return $DateTime->format(implode(' ', $formats)); |
|
| 1508 | - break; |
|
| 1509 | - } |
|
| 1510 | - } |
|
| 1511 | - |
|
| 1512 | - |
|
| 1513 | - |
|
| 1514 | - /** |
|
| 1515 | - * This receives a time string for a given field and ensures that it is setup to match what the internal settings |
|
| 1516 | - * for the model are. Returns a DateTime object. |
|
| 1517 | - * Note: a gotcha for when you send in unix timestamp. Remember a unix timestamp is already timezone agnostic, |
|
| 1518 | - * (functionally the equivalent of UTC+0). So when you send it in, whatever timezone string you include is |
|
| 1519 | - * ignored. |
|
| 1520 | - * |
|
| 1521 | - * @param string $field_name The field being setup. |
|
| 1522 | - * @param string $timestring The date time string being used. |
|
| 1523 | - * @param string $incoming_format The format for the time string. |
|
| 1524 | - * @param string $timezone By default, it is assumed the incoming time string is in timezone for |
|
| 1525 | - * the blog. If this is not the case, then it can be specified here. If incoming |
|
| 1526 | - * format is |
|
| 1527 | - * 'U', this is ignored. |
|
| 1528 | - * @return DateTime |
|
| 1529 | - * @throws EE_Error |
|
| 1530 | - */ |
|
| 1531 | - public function convert_datetime_for_query($field_name, $timestring, $incoming_format, $timezone = '') |
|
| 1532 | - { |
|
| 1533 | - // just using this to ensure the timezone is set correctly internally |
|
| 1534 | - $this->get_formats_for($field_name); |
|
| 1535 | - // load EEH_DTT_Helper |
|
| 1536 | - $set_timezone = empty($timezone) ? EEH_DTT_Helper::get_timezone() : $timezone; |
|
| 1537 | - $incomingDateTime = date_create_from_format($incoming_format, $timestring, new DateTimeZone($set_timezone)); |
|
| 1538 | - EEH_DTT_Helper::setTimezone($incomingDateTime, new DateTimeZone($this->_timezone)); |
|
| 1539 | - return \EventEspresso\core\domain\entities\DbSafeDateTime::createFromDateTime($incomingDateTime); |
|
| 1540 | - } |
|
| 1541 | - |
|
| 1542 | - |
|
| 1543 | - |
|
| 1544 | - /** |
|
| 1545 | - * Gets all the tables comprising this model. Array keys are the table aliases, and values are EE_Table objects |
|
| 1546 | - * |
|
| 1547 | - * @return EE_Table_Base[] |
|
| 1548 | - */ |
|
| 1549 | - public function get_tables() |
|
| 1550 | - { |
|
| 1551 | - return $this->_tables; |
|
| 1552 | - } |
|
| 1553 | - |
|
| 1554 | - |
|
| 1555 | - |
|
| 1556 | - /** |
|
| 1557 | - * Updates all the database entries (in each table for this model) according to $fields_n_values and optionally |
|
| 1558 | - * also updates all the model objects, where the criteria expressed in $query_params are met.. |
|
| 1559 | - * Also note: if this model has multiple tables, this update verifies all the secondary tables have an entry for |
|
| 1560 | - * each row (in the primary table) we're trying to update; if not, it inserts an entry in the secondary table. Eg: |
|
| 1561 | - * if our model has 2 tables: wp_posts (primary), and wp_esp_event (secondary). Let's say we are trying to update a |
|
| 1562 | - * model object with EVT_ID = 1 |
|
| 1563 | - * (which means where wp_posts has ID = 1, because wp_posts.ID is the primary key's column), which exists, but |
|
| 1564 | - * there is no entry in wp_esp_event for this entry in wp_posts. So, this update script will insert a row into |
|
| 1565 | - * wp_esp_event, using any available parameters from $fields_n_values (eg, if "EVT_limit" => 40 is in |
|
| 1566 | - * $fields_n_values, the new entry in wp_esp_event will set EVT_limit = 40, and use default for other columns which |
|
| 1567 | - * are not specified) |
|
| 1568 | - * |
|
| 1569 | - * @param array $fields_n_values keys are model fields (exactly like keys in EEM_Base::_fields, NOT db |
|
| 1570 | - * columns!), values are strings, ints, floats, and maybe arrays if they |
|
| 1571 | - * are to be serialized. Basically, the values are what you'd expect to be |
|
| 1572 | - * values on the model, NOT necessarily what's in the DB. For example, if |
|
| 1573 | - * we wanted to update only the TXN_details on any Transactions where its |
|
| 1574 | - * ID=34, we'd use this method as follows: |
|
| 1575 | - * EEM_Transaction::instance()->update( |
|
| 1576 | - * array('TXN_details'=>array('detail1'=>'monkey','detail2'=>'banana'), |
|
| 1577 | - * array(array('TXN_ID'=>34))); |
|
| 1578 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1579 | - * Eg, consider updating Question's QST_admin_label field is of type |
|
| 1580 | - * Simple_HTML. If you use this function to update that field to $new_value |
|
| 1581 | - * = (note replace 8's with appropriate opening and closing tags in the |
|
| 1582 | - * following example)"8script8alert('I hack all');8/script88b8boom |
|
| 1583 | - * baby8/b8", then if you set $values_already_prepared_by_model_object to |
|
| 1584 | - * TRUE, it is assumed that you've already called |
|
| 1585 | - * EE_Simple_HTML_Field->prepare_for_set($new_value), which removes the |
|
| 1586 | - * malicious javascript. However, if |
|
| 1587 | - * $values_already_prepared_by_model_object is left as FALSE, then |
|
| 1588 | - * EE_Simple_HTML_Field->prepare_for_set($new_value) will be called on it, |
|
| 1589 | - * and every other field, before insertion. We provide this parameter |
|
| 1590 | - * because model objects perform their prepare_for_set function on all |
|
| 1591 | - * their values, and so don't need to be called again (and in many cases, |
|
| 1592 | - * shouldn't be called again. Eg: if we escape HTML characters in the |
|
| 1593 | - * prepare_for_set method...) |
|
| 1594 | - * @param boolean $keep_model_objs_in_sync if TRUE, makes sure we ALSO update model objects |
|
| 1595 | - * in this model's entity map according to $fields_n_values that match |
|
| 1596 | - * $query_params. This obviously has some overhead, so you can disable it |
|
| 1597 | - * by setting this to FALSE, but be aware that model objects being used |
|
| 1598 | - * could get out-of-sync with the database |
|
| 1599 | - * @return int how many rows got updated or FALSE if something went wrong with the query (wp returns FALSE or num |
|
| 1600 | - * rows affected which *could* include 0 which DOES NOT mean the query was |
|
| 1601 | - * bad) |
|
| 1602 | - * @throws EE_Error |
|
| 1603 | - */ |
|
| 1604 | - public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true) |
|
| 1605 | - { |
|
| 1606 | - if (! is_array($query_params)) { |
|
| 1607 | - EE_Error::doing_it_wrong( |
|
| 1608 | - 'EEM_Base::update', |
|
| 1609 | - sprintf( |
|
| 1610 | - __('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1611 | - gettype($query_params) |
|
| 1612 | - ), |
|
| 1613 | - '4.6.0' |
|
| 1614 | - ); |
|
| 1615 | - $query_params = array(); |
|
| 1616 | - } |
|
| 1617 | - /** |
|
| 1618 | - * Action called before a model update call has been made. |
|
| 1619 | - * |
|
| 1620 | - * @param EEM_Base $model |
|
| 1621 | - * @param array $fields_n_values the updated fields and their new values |
|
| 1622 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1623 | - */ |
|
| 1624 | - do_action('AHEE__EEM_Base__update__begin', $this, $fields_n_values, $query_params); |
|
| 1625 | - /** |
|
| 1626 | - * Filters the fields about to be updated given the query parameters. You can provide the |
|
| 1627 | - * $query_params to $this->get_all() to find exactly which records will be updated |
|
| 1628 | - * |
|
| 1629 | - * @param array $fields_n_values fields and their new values |
|
| 1630 | - * @param EEM_Base $model the model being queried |
|
| 1631 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1632 | - */ |
|
| 1633 | - $fields_n_values = (array) apply_filters( |
|
| 1634 | - 'FHEE__EEM_Base__update__fields_n_values', |
|
| 1635 | - $fields_n_values, |
|
| 1636 | - $this, |
|
| 1637 | - $query_params |
|
| 1638 | - ); |
|
| 1639 | - // need to verify that, for any entry we want to update, there are entries in each secondary table. |
|
| 1640 | - // to do that, for each table, verify that it's PK isn't null. |
|
| 1641 | - $tables = $this->get_tables(); |
|
| 1642 | - // and if the other tables don't have a row for each table-to-be-updated, we'll insert one with whatever values available in the current update query |
|
| 1643 | - // NOTE: we should make this code more efficient by NOT querying twice |
|
| 1644 | - // before the real update, but that needs to first go through ALPHA testing |
|
| 1645 | - // as it's dangerous. says Mike August 8 2014 |
|
| 1646 | - // we want to make sure the default_where strategy is ignored |
|
| 1647 | - $this->_ignore_where_strategy = true; |
|
| 1648 | - $wpdb_select_results = $this->_get_all_wpdb_results($query_params); |
|
| 1649 | - foreach ($wpdb_select_results as $wpdb_result) { |
|
| 1650 | - // type cast stdClass as array |
|
| 1651 | - $wpdb_result = (array) $wpdb_result; |
|
| 1652 | - // get the model object's PK, as we'll want this if we need to insert a row into secondary tables |
|
| 1653 | - if ($this->has_primary_key_field()) { |
|
| 1654 | - $main_table_pk_value = $wpdb_result[ $this->get_primary_key_field()->get_qualified_column() ]; |
|
| 1655 | - } else { |
|
| 1656 | - // if there's no primary key, we basically can't support having a 2nd table on the model (we could but it would be lots of work) |
|
| 1657 | - $main_table_pk_value = null; |
|
| 1658 | - } |
|
| 1659 | - // if there are more than 1 tables, we'll want to verify that each table for this model has an entry in the other tables |
|
| 1660 | - // and if the other tables don't have a row for each table-to-be-updated, we'll insert one with whatever values available in the current update query |
|
| 1661 | - if (count($tables) > 1) { |
|
| 1662 | - // foreach matching row in the DB, ensure that each table's PK isn't null. If so, there must not be an entry |
|
| 1663 | - // in that table, and so we'll want to insert one |
|
| 1664 | - foreach ($tables as $table_obj) { |
|
| 1665 | - $this_table_pk_column = $table_obj->get_fully_qualified_pk_column(); |
|
| 1666 | - // if there is no private key for this table on the results, it means there's no entry |
|
| 1667 | - // in this table, right? so insert a row in the current table, using any fields available |
|
| 1668 | - if (! (array_key_exists($this_table_pk_column, $wpdb_result) |
|
| 1669 | - && $wpdb_result[ $this_table_pk_column ]) |
|
| 1670 | - ) { |
|
| 1671 | - $success = $this->_insert_into_specific_table( |
|
| 1672 | - $table_obj, |
|
| 1673 | - $fields_n_values, |
|
| 1674 | - $main_table_pk_value |
|
| 1675 | - ); |
|
| 1676 | - // if we died here, report the error |
|
| 1677 | - if (! $success) { |
|
| 1678 | - return false; |
|
| 1679 | - } |
|
| 1680 | - } |
|
| 1681 | - } |
|
| 1682 | - } |
|
| 1683 | - // //and now check that if we have cached any models by that ID on the model, that |
|
| 1684 | - // //they also get updated properly |
|
| 1685 | - // $model_object = $this->get_from_entity_map( $main_table_pk_value ); |
|
| 1686 | - // if( $model_object ){ |
|
| 1687 | - // foreach( $fields_n_values as $field => $value ){ |
|
| 1688 | - // $model_object->set($field, $value); |
|
| 1689 | - // let's make sure default_where strategy is followed now |
|
| 1690 | - $this->_ignore_where_strategy = false; |
|
| 1691 | - } |
|
| 1692 | - // if we want to keep model objects in sync, AND |
|
| 1693 | - // if this wasn't called from a model object (to update itself) |
|
| 1694 | - // then we want to make sure we keep all the existing |
|
| 1695 | - // model objects in sync with the db |
|
| 1696 | - if ($keep_model_objs_in_sync && ! $this->_values_already_prepared_by_model_object) { |
|
| 1697 | - if ($this->has_primary_key_field()) { |
|
| 1698 | - $model_objs_affected_ids = $this->get_col($query_params); |
|
| 1699 | - } else { |
|
| 1700 | - // we need to select a bunch of columns and then combine them into the the "index primary key string"s |
|
| 1701 | - $models_affected_key_columns = $this->_get_all_wpdb_results($query_params, ARRAY_A); |
|
| 1702 | - $model_objs_affected_ids = array(); |
|
| 1703 | - foreach ($models_affected_key_columns as $row) { |
|
| 1704 | - $combined_index_key = $this->get_index_primary_key_string($row); |
|
| 1705 | - $model_objs_affected_ids[ $combined_index_key ] = $combined_index_key; |
|
| 1706 | - } |
|
| 1707 | - } |
|
| 1708 | - if (! $model_objs_affected_ids) { |
|
| 1709 | - // wait wait wait- if nothing was affected let's stop here |
|
| 1710 | - return 0; |
|
| 1711 | - } |
|
| 1712 | - foreach ($model_objs_affected_ids as $id) { |
|
| 1713 | - $model_obj_in_entity_map = $this->get_from_entity_map($id); |
|
| 1714 | - if ($model_obj_in_entity_map) { |
|
| 1715 | - foreach ($fields_n_values as $field => $new_value) { |
|
| 1716 | - $model_obj_in_entity_map->set($field, $new_value); |
|
| 1717 | - } |
|
| 1718 | - } |
|
| 1719 | - } |
|
| 1720 | - // if there is a primary key on this model, we can now do a slight optimization |
|
| 1721 | - if ($this->has_primary_key_field()) { |
|
| 1722 | - // we already know what we want to update. So let's make the query simpler so it's a little more efficient |
|
| 1723 | - $query_params = array( |
|
| 1724 | - array($this->primary_key_name() => array('IN', $model_objs_affected_ids)), |
|
| 1725 | - 'limit' => count($model_objs_affected_ids), |
|
| 1726 | - 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
| 1727 | - ); |
|
| 1728 | - } |
|
| 1729 | - } |
|
| 1730 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1731 | - $SQL = "UPDATE " |
|
| 1732 | - . $model_query_info->get_full_join_sql() |
|
| 1733 | - . " SET " |
|
| 1734 | - . $this->_construct_update_sql($fields_n_values) |
|
| 1735 | - . $model_query_info->get_where_sql();// note: doesn't use _construct_2nd_half_of_select_query() because doesn't accept LIMIT, ORDER BY, etc. |
|
| 1736 | - $rows_affected = $this->_do_wpdb_query('query', array($SQL)); |
|
| 1737 | - /** |
|
| 1738 | - * Action called after a model update call has been made. |
|
| 1739 | - * |
|
| 1740 | - * @param EEM_Base $model |
|
| 1741 | - * @param array $fields_n_values the updated fields and their new values |
|
| 1742 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1743 | - * @param int $rows_affected |
|
| 1744 | - */ |
|
| 1745 | - do_action('AHEE__EEM_Base__update__end', $this, $fields_n_values, $query_params, $rows_affected); |
|
| 1746 | - return $rows_affected;// how many supposedly got updated |
|
| 1747 | - } |
|
| 1748 | - |
|
| 1749 | - |
|
| 1750 | - |
|
| 1751 | - /** |
|
| 1752 | - * Analogous to $wpdb->get_col, returns a 1-dimensional array where teh values |
|
| 1753 | - * are teh values of the field specified (or by default the primary key field) |
|
| 1754 | - * that matched the query params. Note that you should pass the name of the |
|
| 1755 | - * model FIELD, not the database table's column name. |
|
| 1756 | - * |
|
| 1757 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1758 | - * @param string $field_to_select |
|
| 1759 | - * @return array just like $wpdb->get_col() |
|
| 1760 | - * @throws EE_Error |
|
| 1761 | - */ |
|
| 1762 | - public function get_col($query_params = array(), $field_to_select = null) |
|
| 1763 | - { |
|
| 1764 | - if ($field_to_select) { |
|
| 1765 | - $field = $this->field_settings_for($field_to_select); |
|
| 1766 | - } elseif ($this->has_primary_key_field()) { |
|
| 1767 | - $field = $this->get_primary_key_field(); |
|
| 1768 | - } else { |
|
| 1769 | - // no primary key, just grab the first column |
|
| 1770 | - $field = reset($this->field_settings()); |
|
| 1771 | - } |
|
| 1772 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1773 | - $select_expressions = $field->get_qualified_column(); |
|
| 1774 | - $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 1775 | - return $this->_do_wpdb_query('get_col', array($SQL)); |
|
| 1776 | - } |
|
| 1777 | - |
|
| 1778 | - |
|
| 1779 | - |
|
| 1780 | - /** |
|
| 1781 | - * Returns a single column value for a single row from the database |
|
| 1782 | - * |
|
| 1783 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1784 | - * @param string $field_to_select @see EEM_Base::get_col() |
|
| 1785 | - * @return string |
|
| 1786 | - * @throws EE_Error |
|
| 1787 | - */ |
|
| 1788 | - public function get_var($query_params = array(), $field_to_select = null) |
|
| 1789 | - { |
|
| 1790 | - $query_params['limit'] = 1; |
|
| 1791 | - $col = $this->get_col($query_params, $field_to_select); |
|
| 1792 | - if (! empty($col)) { |
|
| 1793 | - return reset($col); |
|
| 1794 | - } |
|
| 1795 | - return null; |
|
| 1796 | - } |
|
| 1797 | - |
|
| 1798 | - |
|
| 1799 | - |
|
| 1800 | - /** |
|
| 1801 | - * Makes the SQL for after "UPDATE table_X inner join table_Y..." and before "...WHERE". Eg "Question.name='party |
|
| 1802 | - * time?', Question.desc='what do you think?',..." Values are filtered through wpdb->prepare to avoid against SQL |
|
| 1803 | - * injection, but currently no further filtering is done |
|
| 1804 | - * |
|
| 1805 | - * @global $wpdb |
|
| 1806 | - * @param array $fields_n_values array keys are field names on this model, and values are what those fields should |
|
| 1807 | - * be updated to in the DB |
|
| 1808 | - * @return string of SQL |
|
| 1809 | - * @throws EE_Error |
|
| 1810 | - */ |
|
| 1811 | - public function _construct_update_sql($fields_n_values) |
|
| 1812 | - { |
|
| 1813 | - /** @type WPDB $wpdb */ |
|
| 1814 | - global $wpdb; |
|
| 1815 | - $cols_n_values = array(); |
|
| 1816 | - foreach ($fields_n_values as $field_name => $value) { |
|
| 1817 | - $field_obj = $this->field_settings_for($field_name); |
|
| 1818 | - // if the value is NULL, we want to assign the value to that. |
|
| 1819 | - // wpdb->prepare doesn't really handle that properly |
|
| 1820 | - $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
| 1821 | - $value_sql = $prepared_value === null ? 'NULL' |
|
| 1822 | - : $wpdb->prepare($field_obj->get_wpdb_data_type(), $prepared_value); |
|
| 1823 | - $cols_n_values[] = $field_obj->get_qualified_column() . "=" . $value_sql; |
|
| 1824 | - } |
|
| 1825 | - return implode(",", $cols_n_values); |
|
| 1826 | - } |
|
| 1827 | - |
|
| 1828 | - |
|
| 1829 | - |
|
| 1830 | - /** |
|
| 1831 | - * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
| 1832 | - * Performs a HARD delete, meaning the database row should always be removed, |
|
| 1833 | - * not just have a flag field on it switched |
|
| 1834 | - * Wrapper for EEM_Base::delete_permanently() |
|
| 1835 | - * |
|
| 1836 | - * @param mixed $id |
|
| 1837 | - * @param boolean $allow_blocking |
|
| 1838 | - * @return int the number of rows deleted |
|
| 1839 | - * @throws EE_Error |
|
| 1840 | - */ |
|
| 1841 | - public function delete_permanently_by_ID($id, $allow_blocking = true) |
|
| 1842 | - { |
|
| 1843 | - return $this->delete_permanently( |
|
| 1844 | - array( |
|
| 1845 | - array($this->get_primary_key_field()->get_name() => $id), |
|
| 1846 | - 'limit' => 1, |
|
| 1847 | - ), |
|
| 1848 | - $allow_blocking |
|
| 1849 | - ); |
|
| 1850 | - } |
|
| 1851 | - |
|
| 1852 | - |
|
| 1853 | - |
|
| 1854 | - /** |
|
| 1855 | - * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
| 1856 | - * Wrapper for EEM_Base::delete() |
|
| 1857 | - * |
|
| 1858 | - * @param mixed $id |
|
| 1859 | - * @param boolean $allow_blocking |
|
| 1860 | - * @return int the number of rows deleted |
|
| 1861 | - * @throws EE_Error |
|
| 1862 | - */ |
|
| 1863 | - public function delete_by_ID($id, $allow_blocking = true) |
|
| 1864 | - { |
|
| 1865 | - return $this->delete( |
|
| 1866 | - array( |
|
| 1867 | - array($this->get_primary_key_field()->get_name() => $id), |
|
| 1868 | - 'limit' => 1, |
|
| 1869 | - ), |
|
| 1870 | - $allow_blocking |
|
| 1871 | - ); |
|
| 1872 | - } |
|
| 1873 | - |
|
| 1874 | - |
|
| 1875 | - |
|
| 1876 | - /** |
|
| 1877 | - * Identical to delete_permanently, but does a "soft" delete if possible, |
|
| 1878 | - * meaning if the model has a field that indicates its been "trashed" or |
|
| 1879 | - * "soft deleted", we will just set that instead of actually deleting the rows. |
|
| 1880 | - * |
|
| 1881 | - * @see EEM_Base::delete_permanently |
|
| 1882 | - * @param array $query_params |
|
| 1883 | - * @param boolean $allow_blocking |
|
| 1884 | - * @return int how many rows got deleted |
|
| 1885 | - * @throws EE_Error |
|
| 1886 | - */ |
|
| 1887 | - public function delete($query_params, $allow_blocking = true) |
|
| 1888 | - { |
|
| 1889 | - return $this->delete_permanently($query_params, $allow_blocking); |
|
| 1890 | - } |
|
| 1891 | - |
|
| 1892 | - |
|
| 1893 | - |
|
| 1894 | - /** |
|
| 1895 | - * Deletes the model objects that meet the query params. Note: this method is overridden |
|
| 1896 | - * in EEM_Soft_Delete_Base so that soft-deleted model objects are instead only flagged |
|
| 1897 | - * as archived, not actually deleted |
|
| 1898 | - * |
|
| 1899 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1900 | - * @param boolean $allow_blocking if TRUE, matched objects will only be deleted if there is no related model info |
|
| 1901 | - * that blocks it (ie, there' sno other data that depends on this data); if false, |
|
| 1902 | - * deletes regardless of other objects which may depend on it. Its generally |
|
| 1903 | - * advisable to always leave this as TRUE, otherwise you could easily corrupt your |
|
| 1904 | - * DB |
|
| 1905 | - * @return int how many rows got deleted |
|
| 1906 | - * @throws EE_Error |
|
| 1907 | - */ |
|
| 1908 | - public function delete_permanently($query_params, $allow_blocking = true) |
|
| 1909 | - { |
|
| 1910 | - /** |
|
| 1911 | - * Action called just before performing a real deletion query. You can use the |
|
| 1912 | - * model and its $query_params to find exactly which items will be deleted |
|
| 1913 | - * |
|
| 1914 | - * @param EEM_Base $model |
|
| 1915 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1916 | - * @param boolean $allow_blocking whether or not to allow related model objects |
|
| 1917 | - * to block (prevent) this deletion |
|
| 1918 | - */ |
|
| 1919 | - do_action('AHEE__EEM_Base__delete__begin', $this, $query_params, $allow_blocking); |
|
| 1920 | - // some MySQL databases may be running safe mode, which may restrict |
|
| 1921 | - // deletion if there is no KEY column used in the WHERE statement of a deletion. |
|
| 1922 | - // to get around this, we first do a SELECT, get all the IDs, and then run another query |
|
| 1923 | - // to delete them |
|
| 1924 | - $items_for_deletion = $this->_get_all_wpdb_results($query_params); |
|
| 1925 | - $columns_and_ids_for_deleting = $this->_get_ids_for_delete($items_for_deletion, $allow_blocking); |
|
| 1926 | - $deletion_where_query_part = $this->_build_query_part_for_deleting_from_columns_and_values( |
|
| 1927 | - $columns_and_ids_for_deleting |
|
| 1928 | - ); |
|
| 1929 | - /** |
|
| 1930 | - * Allows client code to act on the items being deleted before the query is actually executed. |
|
| 1931 | - * |
|
| 1932 | - * @param EEM_Base $this The model instance being acted on. |
|
| 1933 | - * @param array $query_params The incoming array of query parameters influencing what gets deleted. |
|
| 1934 | - * @param bool $allow_blocking @see param description in method phpdoc block. |
|
| 1935 | - * @param array $columns_and_ids_for_deleting An array indicating what entities will get removed as |
|
| 1936 | - * derived from the incoming query parameters. |
|
| 1937 | - * @see details on the structure of this array in the phpdocs |
|
| 1938 | - * for the `_get_ids_for_delete_method` |
|
| 1939 | - * |
|
| 1940 | - */ |
|
| 1941 | - do_action( |
|
| 1942 | - 'AHEE__EEM_Base__delete__before_query', |
|
| 1943 | - $this, |
|
| 1944 | - $query_params, |
|
| 1945 | - $allow_blocking, |
|
| 1946 | - $columns_and_ids_for_deleting |
|
| 1947 | - ); |
|
| 1948 | - if ($deletion_where_query_part) { |
|
| 1949 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1950 | - $table_aliases = array_keys($this->_tables); |
|
| 1951 | - $SQL = "DELETE " |
|
| 1952 | - . implode(", ", $table_aliases) |
|
| 1953 | - . " FROM " |
|
| 1954 | - . $model_query_info->get_full_join_sql() |
|
| 1955 | - . " WHERE " |
|
| 1956 | - . $deletion_where_query_part; |
|
| 1957 | - $rows_deleted = $this->_do_wpdb_query('query', array($SQL)); |
|
| 1958 | - } else { |
|
| 1959 | - $rows_deleted = 0; |
|
| 1960 | - } |
|
| 1961 | - |
|
| 1962 | - // Next, make sure those items are removed from the entity map; if they could be put into it at all; and if |
|
| 1963 | - // there was no error with the delete query. |
|
| 1964 | - if ($this->has_primary_key_field() |
|
| 1965 | - && $rows_deleted !== false |
|
| 1966 | - && isset($columns_and_ids_for_deleting[ $this->get_primary_key_field()->get_qualified_column() ]) |
|
| 1967 | - ) { |
|
| 1968 | - $ids_for_removal = $columns_and_ids_for_deleting[ $this->get_primary_key_field()->get_qualified_column() ]; |
|
| 1969 | - foreach ($ids_for_removal as $id) { |
|
| 1970 | - if (isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ])) { |
|
| 1971 | - unset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]); |
|
| 1972 | - } |
|
| 1973 | - } |
|
| 1974 | - |
|
| 1975 | - // delete any extra meta attached to the deleted entities but ONLY if this model is not an instance of |
|
| 1976 | - // `EEM_Extra_Meta`. In other words we want to prevent recursion on EEM_Extra_Meta::delete_permanently calls |
|
| 1977 | - // unnecessarily. It's very unlikely that users will have assigned Extra Meta to Extra Meta |
|
| 1978 | - // (although it is possible). |
|
| 1979 | - // Note this can be skipped by using the provided filter and returning false. |
|
| 1980 | - if (apply_filters( |
|
| 1981 | - 'FHEE__EEM_Base__delete_permanently__dont_delete_extra_meta_for_extra_meta', |
|
| 1982 | - ! $this instanceof EEM_Extra_Meta, |
|
| 1983 | - $this |
|
| 1984 | - )) { |
|
| 1985 | - EEM_Extra_Meta::instance()->delete_permanently(array( |
|
| 1986 | - 0 => array( |
|
| 1987 | - 'EXM_type' => $this->get_this_model_name(), |
|
| 1988 | - 'OBJ_ID' => array( |
|
| 1989 | - 'IN', |
|
| 1990 | - $ids_for_removal |
|
| 1991 | - ) |
|
| 1992 | - ) |
|
| 1993 | - )); |
|
| 1994 | - } |
|
| 1995 | - } |
|
| 1996 | - |
|
| 1997 | - /** |
|
| 1998 | - * Action called just after performing a real deletion query. Although at this point the |
|
| 1999 | - * items should have been deleted |
|
| 2000 | - * |
|
| 2001 | - * @param EEM_Base $model |
|
| 2002 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2003 | - * @param int $rows_deleted |
|
| 2004 | - */ |
|
| 2005 | - do_action('AHEE__EEM_Base__delete__end', $this, $query_params, $rows_deleted, $columns_and_ids_for_deleting); |
|
| 2006 | - return $rows_deleted;// how many supposedly got deleted |
|
| 2007 | - } |
|
| 2008 | - |
|
| 2009 | - |
|
| 2010 | - |
|
| 2011 | - /** |
|
| 2012 | - * Checks all the relations that throw error messages when there are blocking related objects |
|
| 2013 | - * for related model objects. If there are any related model objects on those relations, |
|
| 2014 | - * adds an EE_Error, and return true |
|
| 2015 | - * |
|
| 2016 | - * @param EE_Base_Class|int $this_model_obj_or_id |
|
| 2017 | - * @param EE_Base_Class $ignore_this_model_obj a model object like 'EE_Event', or 'EE_Term_Taxonomy', which |
|
| 2018 | - * should be ignored when determining whether there are related |
|
| 2019 | - * model objects which block this model object's deletion. Useful |
|
| 2020 | - * if you know A is related to B and are considering deleting A, |
|
| 2021 | - * but want to see if A has any other objects blocking its deletion |
|
| 2022 | - * before removing the relation between A and B |
|
| 2023 | - * @return boolean |
|
| 2024 | - * @throws EE_Error |
|
| 2025 | - */ |
|
| 2026 | - public function delete_is_blocked_by_related_models($this_model_obj_or_id, $ignore_this_model_obj = null) |
|
| 2027 | - { |
|
| 2028 | - // first, if $ignore_this_model_obj was supplied, get its model |
|
| 2029 | - if ($ignore_this_model_obj && $ignore_this_model_obj instanceof EE_Base_Class) { |
|
| 2030 | - $ignored_model = $ignore_this_model_obj->get_model(); |
|
| 2031 | - } else { |
|
| 2032 | - $ignored_model = null; |
|
| 2033 | - } |
|
| 2034 | - // now check all the relations of $this_model_obj_or_id and see if there |
|
| 2035 | - // are any related model objects blocking it? |
|
| 2036 | - $is_blocked = false; |
|
| 2037 | - foreach ($this->_model_relations as $relation_name => $relation_obj) { |
|
| 2038 | - if ($relation_obj->block_delete_if_related_models_exist()) { |
|
| 2039 | - // if $ignore_this_model_obj was supplied, then for the query |
|
| 2040 | - // on that model needs to be told to ignore $ignore_this_model_obj |
|
| 2041 | - if ($ignored_model && $relation_name === $ignored_model->get_this_model_name()) { |
|
| 2042 | - $related_model_objects = $relation_obj->get_all_related($this_model_obj_or_id, array( |
|
| 2043 | - array( |
|
| 2044 | - $ignored_model->get_primary_key_field()->get_name() => array( |
|
| 2045 | - '!=', |
|
| 2046 | - $ignore_this_model_obj->ID(), |
|
| 2047 | - ), |
|
| 2048 | - ), |
|
| 2049 | - )); |
|
| 2050 | - } else { |
|
| 2051 | - $related_model_objects = $relation_obj->get_all_related($this_model_obj_or_id); |
|
| 2052 | - } |
|
| 2053 | - if ($related_model_objects) { |
|
| 2054 | - EE_Error::add_error($relation_obj->get_deletion_error_message(), __FILE__, __FUNCTION__, __LINE__); |
|
| 2055 | - $is_blocked = true; |
|
| 2056 | - } |
|
| 2057 | - } |
|
| 2058 | - } |
|
| 2059 | - return $is_blocked; |
|
| 2060 | - } |
|
| 2061 | - |
|
| 2062 | - |
|
| 2063 | - /** |
|
| 2064 | - * Builds the columns and values for items to delete from the incoming $row_results_for_deleting array. |
|
| 2065 | - * @param array $row_results_for_deleting |
|
| 2066 | - * @param bool $allow_blocking |
|
| 2067 | - * @return array The shape of this array depends on whether the model `has_primary_key_field` or not. If the |
|
| 2068 | - * model DOES have a primary_key_field, then the array will be a simple single dimension array where |
|
| 2069 | - * the key is the fully qualified primary key column and the value is an array of ids that will be |
|
| 2070 | - * deleted. Example: |
|
| 2071 | - * array('Event.EVT_ID' => array( 1,2,3)) |
|
| 2072 | - * If the model DOES NOT have a primary_key_field, then the array will be a two dimensional array |
|
| 2073 | - * where each element is a group of columns and values that get deleted. Example: |
|
| 2074 | - * array( |
|
| 2075 | - * 0 => array( |
|
| 2076 | - * 'Term_Relationship.object_id' => 1 |
|
| 2077 | - * 'Term_Relationship.term_taxonomy_id' => 5 |
|
| 2078 | - * ), |
|
| 2079 | - * 1 => array( |
|
| 2080 | - * 'Term_Relationship.object_id' => 1 |
|
| 2081 | - * 'Term_Relationship.term_taxonomy_id' => 6 |
|
| 2082 | - * ) |
|
| 2083 | - * ) |
|
| 2084 | - * @throws EE_Error |
|
| 2085 | - */ |
|
| 2086 | - protected function _get_ids_for_delete(array $row_results_for_deleting, $allow_blocking = true) |
|
| 2087 | - { |
|
| 2088 | - $ids_to_delete_indexed_by_column = array(); |
|
| 2089 | - if ($this->has_primary_key_field()) { |
|
| 2090 | - $primary_table = $this->_get_main_table(); |
|
| 2091 | - $primary_table_pk_field = $this->get_field_by_column($primary_table->get_fully_qualified_pk_column()); |
|
| 2092 | - $other_tables = $this->_get_other_tables(); |
|
| 2093 | - $ids_to_delete_indexed_by_column = $query = array(); |
|
| 2094 | - foreach ($row_results_for_deleting as $item_to_delete) { |
|
| 2095 | - // before we mark this item for deletion, |
|
| 2096 | - // make sure there's no related entities blocking its deletion (if we're checking) |
|
| 2097 | - if ($allow_blocking |
|
| 2098 | - && $this->delete_is_blocked_by_related_models( |
|
| 2099 | - $item_to_delete[ $primary_table->get_fully_qualified_pk_column() ] |
|
| 2100 | - ) |
|
| 2101 | - ) { |
|
| 2102 | - continue; |
|
| 2103 | - } |
|
| 2104 | - // primary table deletes |
|
| 2105 | - if (isset($item_to_delete[ $primary_table->get_fully_qualified_pk_column() ])) { |
|
| 2106 | - $ids_to_delete_indexed_by_column[ $primary_table->get_fully_qualified_pk_column() ][] = |
|
| 2107 | - $item_to_delete[ $primary_table->get_fully_qualified_pk_column() ]; |
|
| 2108 | - } |
|
| 2109 | - } |
|
| 2110 | - } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
|
| 2111 | - $fields = $this->get_combined_primary_key_fields(); |
|
| 2112 | - foreach ($row_results_for_deleting as $item_to_delete) { |
|
| 2113 | - $ids_to_delete_indexed_by_column_for_row = array(); |
|
| 2114 | - foreach ($fields as $cpk_field) { |
|
| 2115 | - if ($cpk_field instanceof EE_Model_Field_Base) { |
|
| 2116 | - $ids_to_delete_indexed_by_column_for_row[ $cpk_field->get_qualified_column() ] = |
|
| 2117 | - $item_to_delete[ $cpk_field->get_qualified_column() ]; |
|
| 2118 | - } |
|
| 2119 | - } |
|
| 2120 | - $ids_to_delete_indexed_by_column[] = $ids_to_delete_indexed_by_column_for_row; |
|
| 2121 | - } |
|
| 2122 | - } else { |
|
| 2123 | - // so there's no primary key and no combined key... |
|
| 2124 | - // sorry, can't help you |
|
| 2125 | - throw new EE_Error( |
|
| 2126 | - sprintf( |
|
| 2127 | - __( |
|
| 2128 | - "Cannot delete objects of type %s because there is no primary key NOR combined key", |
|
| 2129 | - "event_espresso" |
|
| 2130 | - ), |
|
| 2131 | - get_class($this) |
|
| 2132 | - ) |
|
| 2133 | - ); |
|
| 2134 | - } |
|
| 2135 | - return $ids_to_delete_indexed_by_column; |
|
| 2136 | - } |
|
| 2137 | - |
|
| 2138 | - |
|
| 2139 | - /** |
|
| 2140 | - * This receives an array of columns and values set to be deleted (as prepared by _get_ids_for_delete) and prepares |
|
| 2141 | - * the corresponding query_part for the query performing the delete. |
|
| 2142 | - * |
|
| 2143 | - * @param array $ids_to_delete_indexed_by_column @see _get_ids_for_delete for how this array might be shaped. |
|
| 2144 | - * @return string |
|
| 2145 | - * @throws EE_Error |
|
| 2146 | - */ |
|
| 2147 | - protected function _build_query_part_for_deleting_from_columns_and_values(array $ids_to_delete_indexed_by_column) |
|
| 2148 | - { |
|
| 2149 | - $query_part = ''; |
|
| 2150 | - if (empty($ids_to_delete_indexed_by_column)) { |
|
| 2151 | - return $query_part; |
|
| 2152 | - } elseif ($this->has_primary_key_field()) { |
|
| 2153 | - $query = array(); |
|
| 2154 | - foreach ($ids_to_delete_indexed_by_column as $column => $ids) { |
|
| 2155 | - // make sure we have unique $ids |
|
| 2156 | - $ids = array_unique($ids); |
|
| 2157 | - $query[] = $column . ' IN(' . implode(',', $ids) . ')'; |
|
| 2158 | - } |
|
| 2159 | - $query_part = ! empty($query) ? implode(' AND ', $query) : $query_part; |
|
| 2160 | - } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
|
| 2161 | - $ways_to_identify_a_row = array(); |
|
| 2162 | - foreach ($ids_to_delete_indexed_by_column as $ids_to_delete_indexed_by_column_for_each_row) { |
|
| 2163 | - $values_for_each_combined_primary_key_for_a_row = array(); |
|
| 2164 | - foreach ($ids_to_delete_indexed_by_column_for_each_row as $column => $id) { |
|
| 2165 | - $values_for_each_combined_primary_key_for_a_row[] = $column . '=' . $id; |
|
| 2166 | - } |
|
| 2167 | - $ways_to_identify_a_row[] = '(' |
|
| 2168 | - . implode(' AND ', $values_for_each_combined_primary_key_for_a_row) |
|
| 2169 | - . ')'; |
|
| 2170 | - } |
|
| 2171 | - $query_part = implode(' OR ', $ways_to_identify_a_row); |
|
| 2172 | - } |
|
| 2173 | - return $query_part; |
|
| 2174 | - } |
|
| 2175 | - |
|
| 2176 | - |
|
| 2177 | - |
|
| 2178 | - /** |
|
| 2179 | - * Gets the model field by the fully qualified name |
|
| 2180 | - * @param string $qualified_column_name eg 'Event_CPT.post_name' or $field_obj->get_qualified_column() |
|
| 2181 | - * @return EE_Model_Field_Base |
|
| 2182 | - */ |
|
| 2183 | - public function get_field_by_column($qualified_column_name) |
|
| 2184 | - { |
|
| 2185 | - foreach ($this->field_settings(true) as $field_name => $field_obj) { |
|
| 2186 | - if ($field_obj->get_qualified_column() === $qualified_column_name) { |
|
| 2187 | - return $field_obj; |
|
| 2188 | - } |
|
| 2189 | - } |
|
| 2190 | - throw new EE_Error( |
|
| 2191 | - sprintf( |
|
| 2192 | - esc_html__('Could not find a field on the model "%1$s" for qualified column "%2$s"', 'event_espresso'), |
|
| 2193 | - $this->get_this_model_name(), |
|
| 2194 | - $qualified_column_name |
|
| 2195 | - ) |
|
| 2196 | - ); |
|
| 2197 | - } |
|
| 2198 | - |
|
| 2199 | - |
|
| 2200 | - |
|
| 2201 | - /** |
|
| 2202 | - * Count all the rows that match criteria the model query params. |
|
| 2203 | - * If $field_to_count isn't provided, the model's primary key is used. Otherwise, we count by field_to_count's |
|
| 2204 | - * column |
|
| 2205 | - * |
|
| 2206 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2207 | - * @param string $field_to_count field on model to count by (not column name) |
|
| 2208 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2209 | - * that by the setting $distinct to TRUE; |
|
| 2210 | - * @return int |
|
| 2211 | - * @throws EE_Error |
|
| 2212 | - */ |
|
| 2213 | - public function count($query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2214 | - { |
|
| 2215 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 2216 | - if ($field_to_count) { |
|
| 2217 | - $field_obj = $this->field_settings_for($field_to_count); |
|
| 2218 | - $column_to_count = $field_obj->get_qualified_column(); |
|
| 2219 | - } elseif ($this->has_primary_key_field()) { |
|
| 2220 | - $pk_field_obj = $this->get_primary_key_field(); |
|
| 2221 | - $column_to_count = $pk_field_obj->get_qualified_column(); |
|
| 2222 | - } else { |
|
| 2223 | - // there's no primary key |
|
| 2224 | - // if we're counting distinct items, and there's no primary key, |
|
| 2225 | - // we need to list out the columns for distinction; |
|
| 2226 | - // otherwise we can just use star |
|
| 2227 | - if ($distinct) { |
|
| 2228 | - $columns_to_use = array(); |
|
| 2229 | - foreach ($this->get_combined_primary_key_fields() as $field_obj) { |
|
| 2230 | - $columns_to_use[] = $field_obj->get_qualified_column(); |
|
| 2231 | - } |
|
| 2232 | - $column_to_count = implode(',', $columns_to_use); |
|
| 2233 | - } else { |
|
| 2234 | - $column_to_count = '*'; |
|
| 2235 | - } |
|
| 2236 | - } |
|
| 2237 | - $column_to_count = $distinct ? "DISTINCT " . $column_to_count : $column_to_count; |
|
| 2238 | - $SQL = "SELECT COUNT(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2239 | - return (int) $this->_do_wpdb_query('get_var', array($SQL)); |
|
| 2240 | - } |
|
| 2241 | - |
|
| 2242 | - |
|
| 2243 | - |
|
| 2244 | - /** |
|
| 2245 | - * Sums up the value of the $field_to_sum (defaults to the primary key, which isn't terribly useful) |
|
| 2246 | - * |
|
| 2247 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2248 | - * @param string $field_to_sum name of field (array key in $_fields array) |
|
| 2249 | - * @return float |
|
| 2250 | - * @throws EE_Error |
|
| 2251 | - */ |
|
| 2252 | - public function sum($query_params, $field_to_sum = null) |
|
| 2253 | - { |
|
| 2254 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 2255 | - if ($field_to_sum) { |
|
| 2256 | - $field_obj = $this->field_settings_for($field_to_sum); |
|
| 2257 | - } else { |
|
| 2258 | - $field_obj = $this->get_primary_key_field(); |
|
| 2259 | - } |
|
| 2260 | - $column_to_count = $field_obj->get_qualified_column(); |
|
| 2261 | - $SQL = "SELECT SUM(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2262 | - $return_value = $this->_do_wpdb_query('get_var', array($SQL)); |
|
| 2263 | - $data_type = $field_obj->get_wpdb_data_type(); |
|
| 2264 | - if ($data_type === '%d' || $data_type === '%s') { |
|
| 2265 | - return (float) $return_value; |
|
| 2266 | - } |
|
| 2267 | - // must be %f |
|
| 2268 | - return (float) $return_value; |
|
| 2269 | - } |
|
| 2270 | - |
|
| 2271 | - |
|
| 2272 | - |
|
| 2273 | - /** |
|
| 2274 | - * Just calls the specified method on $wpdb with the given arguments |
|
| 2275 | - * Consolidates a little extra error handling code |
|
| 2276 | - * |
|
| 2277 | - * @param string $wpdb_method |
|
| 2278 | - * @param array $arguments_to_provide |
|
| 2279 | - * @throws EE_Error |
|
| 2280 | - * @global wpdb $wpdb |
|
| 2281 | - * @return mixed |
|
| 2282 | - */ |
|
| 2283 | - protected function _do_wpdb_query($wpdb_method, $arguments_to_provide) |
|
| 2284 | - { |
|
| 2285 | - // if we're in maintenance mode level 2, DON'T run any queries |
|
| 2286 | - // because level 2 indicates the database needs updating and |
|
| 2287 | - // is probably out of sync with the code |
|
| 2288 | - if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 2289 | - throw new EE_Error(sprintf(__( |
|
| 2290 | - "Event Espresso Level 2 Maintenance mode is active. That means EE can not run ANY database queries until the necessary migration scripts have run which will take EE out of maintenance mode level 2. Please inform support of this error.", |
|
| 2291 | - "event_espresso" |
|
| 2292 | - ))); |
|
| 2293 | - } |
|
| 2294 | - /** @type WPDB $wpdb */ |
|
| 2295 | - global $wpdb; |
|
| 2296 | - if (! method_exists($wpdb, $wpdb_method)) { |
|
| 2297 | - throw new EE_Error(sprintf(__( |
|
| 2298 | - 'There is no method named "%s" on Wordpress\' $wpdb object', |
|
| 2299 | - 'event_espresso' |
|
| 2300 | - ), $wpdb_method)); |
|
| 2301 | - } |
|
| 2302 | - if (WP_DEBUG) { |
|
| 2303 | - $old_show_errors_value = $wpdb->show_errors; |
|
| 2304 | - $wpdb->show_errors(false); |
|
| 2305 | - } |
|
| 2306 | - $result = $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
| 2307 | - $this->show_db_query_if_previously_requested($wpdb->last_query); |
|
| 2308 | - if (WP_DEBUG) { |
|
| 2309 | - $wpdb->show_errors($old_show_errors_value); |
|
| 2310 | - if (! empty($wpdb->last_error)) { |
|
| 2311 | - throw new EE_Error(sprintf(__('WPDB Error: "%s"', 'event_espresso'), $wpdb->last_error)); |
|
| 2312 | - } |
|
| 2313 | - if ($result === false) { |
|
| 2314 | - throw new EE_Error(sprintf(__( |
|
| 2315 | - 'WPDB Error occurred, but no error message was logged by wpdb! The wpdb method called was "%1$s" and the arguments were "%2$s"', |
|
| 2316 | - 'event_espresso' |
|
| 2317 | - ), $wpdb_method, var_export($arguments_to_provide, true))); |
|
| 2318 | - } |
|
| 2319 | - } elseif ($result === false) { |
|
| 2320 | - EE_Error::add_error( |
|
| 2321 | - sprintf( |
|
| 2322 | - __( |
|
| 2323 | - 'A database error has occurred. Turn on WP_DEBUG for more information.||A database error occurred doing wpdb method "%1$s", with arguments "%2$s". The error was "%3$s"', |
|
| 2324 | - 'event_espresso' |
|
| 2325 | - ), |
|
| 2326 | - $wpdb_method, |
|
| 2327 | - var_export($arguments_to_provide, true), |
|
| 2328 | - $wpdb->last_error |
|
| 2329 | - ), |
|
| 2330 | - __FILE__, |
|
| 2331 | - __FUNCTION__, |
|
| 2332 | - __LINE__ |
|
| 2333 | - ); |
|
| 2334 | - } |
|
| 2335 | - return $result; |
|
| 2336 | - } |
|
| 2337 | - |
|
| 2338 | - |
|
| 2339 | - |
|
| 2340 | - /** |
|
| 2341 | - * Attempts to run the indicated WPDB method with the provided arguments, |
|
| 2342 | - * and if there's an error tries to verify the DB is correct. Uses |
|
| 2343 | - * the static property EEM_Base::$_db_verification_level to determine whether |
|
| 2344 | - * we should try to fix the EE core db, the addons, or just give up |
|
| 2345 | - * |
|
| 2346 | - * @param string $wpdb_method |
|
| 2347 | - * @param array $arguments_to_provide |
|
| 2348 | - * @return mixed |
|
| 2349 | - */ |
|
| 2350 | - private function _process_wpdb_query($wpdb_method, $arguments_to_provide) |
|
| 2351 | - { |
|
| 2352 | - /** @type WPDB $wpdb */ |
|
| 2353 | - global $wpdb; |
|
| 2354 | - $wpdb->last_error = null; |
|
| 2355 | - $result = call_user_func_array(array($wpdb, $wpdb_method), $arguments_to_provide); |
|
| 2356 | - // was there an error running the query? but we don't care on new activations |
|
| 2357 | - // (we're going to setup the DB anyway on new activations) |
|
| 2358 | - if (($result === false || ! empty($wpdb->last_error)) |
|
| 2359 | - && EE_System::instance()->detect_req_type() !== EE_System::req_type_new_activation |
|
| 2360 | - ) { |
|
| 2361 | - switch (EEM_Base::$_db_verification_level) { |
|
| 2362 | - case EEM_Base::db_verified_none: |
|
| 2363 | - // let's double-check core's DB |
|
| 2364 | - $error_message = $this->_verify_core_db($wpdb_method, $arguments_to_provide); |
|
| 2365 | - break; |
|
| 2366 | - case EEM_Base::db_verified_core: |
|
| 2367 | - // STILL NO LOVE?? verify all the addons too. Maybe they need to be fixed |
|
| 2368 | - $error_message = $this->_verify_addons_db($wpdb_method, $arguments_to_provide); |
|
| 2369 | - break; |
|
| 2370 | - case EEM_Base::db_verified_addons: |
|
| 2371 | - // ummmm... you in trouble |
|
| 2372 | - return $result; |
|
| 2373 | - break; |
|
| 2374 | - } |
|
| 2375 | - if (! empty($error_message)) { |
|
| 2376 | - EE_Log::instance()->log(__FILE__, __FUNCTION__, $error_message, 'error'); |
|
| 2377 | - trigger_error($error_message); |
|
| 2378 | - } |
|
| 2379 | - return $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
| 2380 | - } |
|
| 2381 | - return $result; |
|
| 2382 | - } |
|
| 2383 | - |
|
| 2384 | - |
|
| 2385 | - |
|
| 2386 | - /** |
|
| 2387 | - * Verifies the EE core database is up-to-date and records that we've done it on |
|
| 2388 | - * EEM_Base::$_db_verification_level |
|
| 2389 | - * |
|
| 2390 | - * @param string $wpdb_method |
|
| 2391 | - * @param array $arguments_to_provide |
|
| 2392 | - * @return string |
|
| 2393 | - */ |
|
| 2394 | - private function _verify_core_db($wpdb_method, $arguments_to_provide) |
|
| 2395 | - { |
|
| 2396 | - /** @type WPDB $wpdb */ |
|
| 2397 | - global $wpdb; |
|
| 2398 | - // ok remember that we've already attempted fixing the core db, in case the problem persists |
|
| 2399 | - EEM_Base::$_db_verification_level = EEM_Base::db_verified_core; |
|
| 2400 | - $error_message = sprintf( |
|
| 2401 | - __( |
|
| 2402 | - 'WPDB Error "%1$s" while running wpdb method "%2$s" with arguments %3$s. Automatically attempting to fix EE Core DB', |
|
| 2403 | - 'event_espresso' |
|
| 2404 | - ), |
|
| 2405 | - $wpdb->last_error, |
|
| 2406 | - $wpdb_method, |
|
| 2407 | - wp_json_encode($arguments_to_provide) |
|
| 2408 | - ); |
|
| 2409 | - EE_System::instance()->initialize_db_if_no_migrations_required(false, true); |
|
| 2410 | - return $error_message; |
|
| 2411 | - } |
|
| 2412 | - |
|
| 2413 | - |
|
| 2414 | - |
|
| 2415 | - /** |
|
| 2416 | - * Verifies the EE addons' database is up-to-date and records that we've done it on |
|
| 2417 | - * EEM_Base::$_db_verification_level |
|
| 2418 | - * |
|
| 2419 | - * @param $wpdb_method |
|
| 2420 | - * @param $arguments_to_provide |
|
| 2421 | - * @return string |
|
| 2422 | - */ |
|
| 2423 | - private function _verify_addons_db($wpdb_method, $arguments_to_provide) |
|
| 2424 | - { |
|
| 2425 | - /** @type WPDB $wpdb */ |
|
| 2426 | - global $wpdb; |
|
| 2427 | - // ok remember that we've already attempted fixing the addons dbs, in case the problem persists |
|
| 2428 | - EEM_Base::$_db_verification_level = EEM_Base::db_verified_addons; |
|
| 2429 | - $error_message = sprintf( |
|
| 2430 | - __( |
|
| 2431 | - 'WPDB AGAIN: Error "%1$s" while running the same method and arguments as before. Automatically attempting to fix EE Addons DB', |
|
| 2432 | - 'event_espresso' |
|
| 2433 | - ), |
|
| 2434 | - $wpdb->last_error, |
|
| 2435 | - $wpdb_method, |
|
| 2436 | - wp_json_encode($arguments_to_provide) |
|
| 2437 | - ); |
|
| 2438 | - EE_System::instance()->initialize_addons(); |
|
| 2439 | - return $error_message; |
|
| 2440 | - } |
|
| 2441 | - |
|
| 2442 | - |
|
| 2443 | - |
|
| 2444 | - /** |
|
| 2445 | - * In order to avoid repeating this code for the get_all, sum, and count functions, put the code parts |
|
| 2446 | - * that are identical in here. Returns a string of SQL of everything in a SELECT query except the beginning |
|
| 2447 | - * SELECT clause, eg " FROM wp_posts AS Event INNER JOIN ... WHERE ... ORDER BY ... LIMIT ... GROUP BY ... HAVING |
|
| 2448 | - * ..." |
|
| 2449 | - * |
|
| 2450 | - * @param EE_Model_Query_Info_Carrier $model_query_info |
|
| 2451 | - * @return string |
|
| 2452 | - */ |
|
| 2453 | - private function _construct_2nd_half_of_select_query(EE_Model_Query_Info_Carrier $model_query_info) |
|
| 2454 | - { |
|
| 2455 | - return " FROM " . $model_query_info->get_full_join_sql() . |
|
| 2456 | - $model_query_info->get_where_sql() . |
|
| 2457 | - $model_query_info->get_group_by_sql() . |
|
| 2458 | - $model_query_info->get_having_sql() . |
|
| 2459 | - $model_query_info->get_order_by_sql() . |
|
| 2460 | - $model_query_info->get_limit_sql(); |
|
| 2461 | - } |
|
| 2462 | - |
|
| 2463 | - |
|
| 2464 | - |
|
| 2465 | - /** |
|
| 2466 | - * Set to easily debug the next X queries ran from this model. |
|
| 2467 | - * |
|
| 2468 | - * @param int $count |
|
| 2469 | - */ |
|
| 2470 | - public function show_next_x_db_queries($count = 1) |
|
| 2471 | - { |
|
| 2472 | - $this->_show_next_x_db_queries = $count; |
|
| 2473 | - } |
|
| 2474 | - |
|
| 2475 | - |
|
| 2476 | - |
|
| 2477 | - /** |
|
| 2478 | - * @param $sql_query |
|
| 2479 | - */ |
|
| 2480 | - public function show_db_query_if_previously_requested($sql_query) |
|
| 2481 | - { |
|
| 2482 | - if ($this->_show_next_x_db_queries > 0) { |
|
| 2483 | - echo $sql_query; |
|
| 2484 | - $this->_show_next_x_db_queries--; |
|
| 2485 | - } |
|
| 2486 | - } |
|
| 2487 | - |
|
| 2488 | - |
|
| 2489 | - |
|
| 2490 | - /** |
|
| 2491 | - * Adds a relationship of the correct type between $modelObject and $otherModelObject. |
|
| 2492 | - * There are the 3 cases: |
|
| 2493 | - * 'belongsTo' relationship: sets $id_or_obj's foreign_key to be $other_model_id_or_obj's primary_key. If |
|
| 2494 | - * $otherModelObject has no ID, it is first saved. |
|
| 2495 | - * 'hasMany' relationship: sets $other_model_id_or_obj's foreign_key to be $id_or_obj's primary_key. If $id_or_obj |
|
| 2496 | - * has no ID, it is first saved. |
|
| 2497 | - * 'hasAndBelongsToMany' relationships: checks that there isn't already an entry in the join table, and adds one. |
|
| 2498 | - * If one of the model Objects has not yet been saved to the database, it is saved before adding the entry in the |
|
| 2499 | - * join table |
|
| 2500 | - * |
|
| 2501 | - * @param EE_Base_Class /int $thisModelObject |
|
| 2502 | - * @param EE_Base_Class /int $id_or_obj EE_base_Class or ID of other Model Object |
|
| 2503 | - * @param string $relationName , key in EEM_Base::_relations |
|
| 2504 | - * an attendee to a group, you also want to specify which role they |
|
| 2505 | - * will have in that group. So you would use this parameter to |
|
| 2506 | - * specify array('role-column-name'=>'role-id') |
|
| 2507 | - * @param array $extra_join_model_fields_n_values This allows you to enter further query params for the relation |
|
| 2508 | - * to for relation to methods that allow you to further specify |
|
| 2509 | - * extra columns to join by (such as HABTM). Keep in mind that the |
|
| 2510 | - * only acceptable query_params is strict "col" => "value" pairs |
|
| 2511 | - * because these will be inserted in any new rows created as well. |
|
| 2512 | - * @return EE_Base_Class which was added as a relation. Object referred to by $other_model_id_or_obj |
|
| 2513 | - * @throws EE_Error |
|
| 2514 | - */ |
|
| 2515 | - public function add_relationship_to( |
|
| 2516 | - $id_or_obj, |
|
| 2517 | - $other_model_id_or_obj, |
|
| 2518 | - $relationName, |
|
| 2519 | - $extra_join_model_fields_n_values = array() |
|
| 2520 | - ) { |
|
| 2521 | - $relation_obj = $this->related_settings_for($relationName); |
|
| 2522 | - return $relation_obj->add_relation_to($id_or_obj, $other_model_id_or_obj, $extra_join_model_fields_n_values); |
|
| 2523 | - } |
|
| 2524 | - |
|
| 2525 | - |
|
| 2526 | - |
|
| 2527 | - /** |
|
| 2528 | - * Removes a relationship of the correct type between $modelObject and $otherModelObject. |
|
| 2529 | - * There are the 3 cases: |
|
| 2530 | - * 'belongsTo' relationship: sets $modelObject's foreign_key to null, if that field is nullable.Otherwise throws an |
|
| 2531 | - * error |
|
| 2532 | - * 'hasMany' relationship: sets $otherModelObject's foreign_key to null,if that field is nullable.Otherwise throws |
|
| 2533 | - * an error |
|
| 2534 | - * 'hasAndBelongsToMany' relationships:removes any existing entry in the join table between the two models. |
|
| 2535 | - * |
|
| 2536 | - * @param EE_Base_Class /int $id_or_obj |
|
| 2537 | - * @param EE_Base_Class /int $other_model_id_or_obj EE_Base_Class or ID of other Model Object |
|
| 2538 | - * @param string $relationName key in EEM_Base::_relations |
|
| 2539 | - * @return boolean of success |
|
| 2540 | - * @throws EE_Error |
|
| 2541 | - * @param array $where_query This allows you to enter further query params for the relation to for relation to |
|
| 2542 | - * methods that allow you to further specify extra columns to join by (such as HABTM). |
|
| 2543 | - * Keep in mind that the only acceptable query_params is strict "col" => "value" pairs |
|
| 2544 | - * because these will be inserted in any new rows created as well. |
|
| 2545 | - */ |
|
| 2546 | - public function remove_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query = array()) |
|
| 2547 | - { |
|
| 2548 | - $relation_obj = $this->related_settings_for($relationName); |
|
| 2549 | - return $relation_obj->remove_relation_to($id_or_obj, $other_model_id_or_obj, $where_query); |
|
| 2550 | - } |
|
| 2551 | - |
|
| 2552 | - |
|
| 2553 | - |
|
| 2554 | - /** |
|
| 2555 | - * @param mixed $id_or_obj |
|
| 2556 | - * @param string $relationName |
|
| 2557 | - * @param array $where_query_params |
|
| 2558 | - * @param EE_Base_Class[] objects to which relations were removed |
|
| 2559 | - * @return \EE_Base_Class[] |
|
| 2560 | - * @throws EE_Error |
|
| 2561 | - */ |
|
| 2562 | - public function remove_relations($id_or_obj, $relationName, $where_query_params = array()) |
|
| 2563 | - { |
|
| 2564 | - $relation_obj = $this->related_settings_for($relationName); |
|
| 2565 | - return $relation_obj->remove_relations($id_or_obj, $where_query_params); |
|
| 2566 | - } |
|
| 2567 | - |
|
| 2568 | - |
|
| 2569 | - |
|
| 2570 | - /** |
|
| 2571 | - * Gets all the related items of the specified $model_name, using $query_params. |
|
| 2572 | - * Note: by default, we remove the "default query params" |
|
| 2573 | - * because we want to get even deleted items etc. |
|
| 2574 | - * |
|
| 2575 | - * @param mixed $id_or_obj EE_Base_Class child or its ID |
|
| 2576 | - * @param string $model_name like 'Event', 'Registration', etc. always singular |
|
| 2577 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2578 | - * @return EE_Base_Class[] |
|
| 2579 | - * @throws EE_Error |
|
| 2580 | - */ |
|
| 2581 | - public function get_all_related($id_or_obj, $model_name, $query_params = null) |
|
| 2582 | - { |
|
| 2583 | - $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2584 | - $relation_settings = $this->related_settings_for($model_name); |
|
| 2585 | - return $relation_settings->get_all_related($model_obj, $query_params); |
|
| 2586 | - } |
|
| 2587 | - |
|
| 2588 | - |
|
| 2589 | - |
|
| 2590 | - /** |
|
| 2591 | - * Deletes all the model objects across the relation indicated by $model_name |
|
| 2592 | - * which are related to $id_or_obj which meet the criteria set in $query_params. |
|
| 2593 | - * However, if the model objects can't be deleted because of blocking related model objects, then |
|
| 2594 | - * they aren't deleted. (Unless the thing that would have been deleted can be soft-deleted, that still happens). |
|
| 2595 | - * |
|
| 2596 | - * @param EE_Base_Class|int|string $id_or_obj |
|
| 2597 | - * @param string $model_name |
|
| 2598 | - * @param array $query_params |
|
| 2599 | - * @return int how many deleted |
|
| 2600 | - * @throws EE_Error |
|
| 2601 | - */ |
|
| 2602 | - public function delete_related($id_or_obj, $model_name, $query_params = array()) |
|
| 2603 | - { |
|
| 2604 | - $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2605 | - $relation_settings = $this->related_settings_for($model_name); |
|
| 2606 | - return $relation_settings->delete_all_related($model_obj, $query_params); |
|
| 2607 | - } |
|
| 2608 | - |
|
| 2609 | - |
|
| 2610 | - |
|
| 2611 | - /** |
|
| 2612 | - * Hard deletes all the model objects across the relation indicated by $model_name |
|
| 2613 | - * which are related to $id_or_obj which meet the criteria set in $query_params. If |
|
| 2614 | - * the model objects can't be hard deleted because of blocking related model objects, |
|
| 2615 | - * just does a soft-delete on them instead. |
|
| 2616 | - * |
|
| 2617 | - * @param EE_Base_Class|int|string $id_or_obj |
|
| 2618 | - * @param string $model_name |
|
| 2619 | - * @param array $query_params |
|
| 2620 | - * @return int how many deleted |
|
| 2621 | - * @throws EE_Error |
|
| 2622 | - */ |
|
| 2623 | - public function delete_related_permanently($id_or_obj, $model_name, $query_params = array()) |
|
| 2624 | - { |
|
| 2625 | - $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2626 | - $relation_settings = $this->related_settings_for($model_name); |
|
| 2627 | - return $relation_settings->delete_related_permanently($model_obj, $query_params); |
|
| 2628 | - } |
|
| 2629 | - |
|
| 2630 | - |
|
| 2631 | - |
|
| 2632 | - /** |
|
| 2633 | - * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2634 | - * unless otherwise specified in the $query_params |
|
| 2635 | - * |
|
| 2636 | - * @param int /EE_Base_Class $id_or_obj |
|
| 2637 | - * @param string $model_name like 'Event', or 'Registration' |
|
| 2638 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2639 | - * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2640 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2641 | - * that by the setting $distinct to TRUE; |
|
| 2642 | - * @return int |
|
| 2643 | - * @throws EE_Error |
|
| 2644 | - */ |
|
| 2645 | - public function count_related( |
|
| 2646 | - $id_or_obj, |
|
| 2647 | - $model_name, |
|
| 2648 | - $query_params = array(), |
|
| 2649 | - $field_to_count = null, |
|
| 2650 | - $distinct = false |
|
| 2651 | - ) { |
|
| 2652 | - $related_model = $this->get_related_model_obj($model_name); |
|
| 2653 | - // we're just going to use the query params on the related model's normal get_all query, |
|
| 2654 | - // except add a condition to say to match the current mod |
|
| 2655 | - if (! isset($query_params['default_where_conditions'])) { |
|
| 2656 | - $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
| 2657 | - } |
|
| 2658 | - $this_model_name = $this->get_this_model_name(); |
|
| 2659 | - $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
| 2660 | - $query_params[0][ $this_model_name . "." . $this_pk_field_name ] = $id_or_obj; |
|
| 2661 | - return $related_model->count($query_params, $field_to_count, $distinct); |
|
| 2662 | - } |
|
| 2663 | - |
|
| 2664 | - |
|
| 2665 | - |
|
| 2666 | - /** |
|
| 2667 | - * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2668 | - * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2669 | - * |
|
| 2670 | - * @param int /EE_Base_Class $id_or_obj |
|
| 2671 | - * @param string $model_name like 'Event', or 'Registration' |
|
| 2672 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2673 | - * @param string $field_to_sum name of field to count by. By default, uses primary key |
|
| 2674 | - * @return float |
|
| 2675 | - * @throws EE_Error |
|
| 2676 | - */ |
|
| 2677 | - public function sum_related($id_or_obj, $model_name, $query_params, $field_to_sum = null) |
|
| 2678 | - { |
|
| 2679 | - $related_model = $this->get_related_model_obj($model_name); |
|
| 2680 | - if (! is_array($query_params)) { |
|
| 2681 | - EE_Error::doing_it_wrong( |
|
| 2682 | - 'EEM_Base::sum_related', |
|
| 2683 | - sprintf( |
|
| 2684 | - __('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 2685 | - gettype($query_params) |
|
| 2686 | - ), |
|
| 2687 | - '4.6.0' |
|
| 2688 | - ); |
|
| 2689 | - $query_params = array(); |
|
| 2690 | - } |
|
| 2691 | - // we're just going to use the query params on the related model's normal get_all query, |
|
| 2692 | - // except add a condition to say to match the current mod |
|
| 2693 | - if (! isset($query_params['default_where_conditions'])) { |
|
| 2694 | - $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
| 2695 | - } |
|
| 2696 | - $this_model_name = $this->get_this_model_name(); |
|
| 2697 | - $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
| 2698 | - $query_params[0][ $this_model_name . "." . $this_pk_field_name ] = $id_or_obj; |
|
| 2699 | - return $related_model->sum($query_params, $field_to_sum); |
|
| 2700 | - } |
|
| 2701 | - |
|
| 2702 | - |
|
| 2703 | - |
|
| 2704 | - /** |
|
| 2705 | - * Uses $this->_relatedModels info to find the first related model object of relation $relationName to the given |
|
| 2706 | - * $modelObject |
|
| 2707 | - * |
|
| 2708 | - * @param int | EE_Base_Class $id_or_obj EE_Base_Class child or its ID |
|
| 2709 | - * @param string $other_model_name , key in $this->_relatedModels, eg 'Registration', or 'Events' |
|
| 2710 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2711 | - * @return EE_Base_Class |
|
| 2712 | - * @throws EE_Error |
|
| 2713 | - */ |
|
| 2714 | - public function get_first_related(EE_Base_Class $id_or_obj, $other_model_name, $query_params) |
|
| 2715 | - { |
|
| 2716 | - $query_params['limit'] = 1; |
|
| 2717 | - $results = $this->get_all_related($id_or_obj, $other_model_name, $query_params); |
|
| 2718 | - if ($results) { |
|
| 2719 | - return array_shift($results); |
|
| 2720 | - } |
|
| 2721 | - return null; |
|
| 2722 | - } |
|
| 2723 | - |
|
| 2724 | - |
|
| 2725 | - |
|
| 2726 | - /** |
|
| 2727 | - * Gets the model's name as it's expected in queries. For example, if this is EEM_Event model, that would be Event |
|
| 2728 | - * |
|
| 2729 | - * @return string |
|
| 2730 | - */ |
|
| 2731 | - public function get_this_model_name() |
|
| 2732 | - { |
|
| 2733 | - return str_replace("EEM_", "", get_class($this)); |
|
| 2734 | - } |
|
| 2735 | - |
|
| 2736 | - |
|
| 2737 | - |
|
| 2738 | - /** |
|
| 2739 | - * Gets the model field on this model which is of type EE_Any_Foreign_Model_Name_Field |
|
| 2740 | - * |
|
| 2741 | - * @return EE_Any_Foreign_Model_Name_Field |
|
| 2742 | - * @throws EE_Error |
|
| 2743 | - */ |
|
| 2744 | - public function get_field_containing_related_model_name() |
|
| 2745 | - { |
|
| 2746 | - foreach ($this->field_settings(true) as $field) { |
|
| 2747 | - if ($field instanceof EE_Any_Foreign_Model_Name_Field) { |
|
| 2748 | - $field_with_model_name = $field; |
|
| 2749 | - } |
|
| 2750 | - } |
|
| 2751 | - if (! isset($field_with_model_name) || ! $field_with_model_name) { |
|
| 2752 | - throw new EE_Error(sprintf( |
|
| 2753 | - __("There is no EE_Any_Foreign_Model_Name field on model %s", "event_espresso"), |
|
| 2754 | - $this->get_this_model_name() |
|
| 2755 | - )); |
|
| 2756 | - } |
|
| 2757 | - return $field_with_model_name; |
|
| 2758 | - } |
|
| 2759 | - |
|
| 2760 | - |
|
| 2761 | - |
|
| 2762 | - /** |
|
| 2763 | - * Inserts a new entry into the database, for each table. |
|
| 2764 | - * Note: does not add the item to the entity map because that is done by EE_Base_Class::save() right after this. |
|
| 2765 | - * If client code uses EEM_Base::insert() directly, then although the item isn't in the entity map, |
|
| 2766 | - * we also know there is no model object with the newly inserted item's ID at the moment (because |
|
| 2767 | - * if there were, then they would already be in the DB and this would fail); and in the future if someone |
|
| 2768 | - * creates a model object with this ID (or grabs it from the DB) then it will be added to the |
|
| 2769 | - * entity map at that time anyways. SO, no need for EEM_Base::insert ot add to the entity map |
|
| 2770 | - * |
|
| 2771 | - * @param array $field_n_values keys are field names, values are their values (in the client code's domain if |
|
| 2772 | - * $values_already_prepared_by_model_object is false, in the model object's domain if |
|
| 2773 | - * $values_already_prepared_by_model_object is true. See comment about this at the top |
|
| 2774 | - * of EEM_Base) |
|
| 2775 | - * @return int|string new primary key on main table that got inserted |
|
| 2776 | - * @throws EE_Error |
|
| 2777 | - */ |
|
| 2778 | - public function insert($field_n_values) |
|
| 2779 | - { |
|
| 2780 | - /** |
|
| 2781 | - * Filters the fields and their values before inserting an item using the models |
|
| 2782 | - * |
|
| 2783 | - * @param array $fields_n_values keys are the fields and values are their new values |
|
| 2784 | - * @param EEM_Base $model the model used |
|
| 2785 | - */ |
|
| 2786 | - $field_n_values = (array) apply_filters('FHEE__EEM_Base__insert__fields_n_values', $field_n_values, $this); |
|
| 2787 | - if ($this->_satisfies_unique_indexes($field_n_values)) { |
|
| 2788 | - $main_table = $this->_get_main_table(); |
|
| 2789 | - $new_id = $this->_insert_into_specific_table($main_table, $field_n_values, false); |
|
| 2790 | - if ($new_id !== false) { |
|
| 2791 | - foreach ($this->_get_other_tables() as $other_table) { |
|
| 2792 | - $this->_insert_into_specific_table($other_table, $field_n_values, $new_id); |
|
| 2793 | - } |
|
| 2794 | - } |
|
| 2795 | - /** |
|
| 2796 | - * Done just after attempting to insert a new model object |
|
| 2797 | - * |
|
| 2798 | - * @param EEM_Base $model used |
|
| 2799 | - * @param array $fields_n_values fields and their values |
|
| 2800 | - * @param int|string the ID of the newly-inserted model object |
|
| 2801 | - */ |
|
| 2802 | - do_action('AHEE__EEM_Base__insert__end', $this, $field_n_values, $new_id); |
|
| 2803 | - return $new_id; |
|
| 2804 | - } |
|
| 2805 | - return false; |
|
| 2806 | - } |
|
| 2807 | - |
|
| 2808 | - |
|
| 2809 | - |
|
| 2810 | - /** |
|
| 2811 | - * Checks that the result would satisfy the unique indexes on this model |
|
| 2812 | - * |
|
| 2813 | - * @param array $field_n_values |
|
| 2814 | - * @param string $action |
|
| 2815 | - * @return boolean |
|
| 2816 | - * @throws EE_Error |
|
| 2817 | - */ |
|
| 2818 | - protected function _satisfies_unique_indexes($field_n_values, $action = 'insert') |
|
| 2819 | - { |
|
| 2820 | - foreach ($this->unique_indexes() as $index_name => $index) { |
|
| 2821 | - $uniqueness_where_params = array_intersect_key($field_n_values, $index->fields()); |
|
| 2822 | - if ($this->exists(array($uniqueness_where_params))) { |
|
| 2823 | - EE_Error::add_error( |
|
| 2824 | - sprintf( |
|
| 2825 | - __( |
|
| 2826 | - "Could not %s %s. %s uniqueness index failed. Fields %s must form a unique set, but an entry already exists with values %s.", |
|
| 2827 | - "event_espresso" |
|
| 2828 | - ), |
|
| 2829 | - $action, |
|
| 2830 | - $this->_get_class_name(), |
|
| 2831 | - $index_name, |
|
| 2832 | - implode(",", $index->field_names()), |
|
| 2833 | - http_build_query($uniqueness_where_params) |
|
| 2834 | - ), |
|
| 2835 | - __FILE__, |
|
| 2836 | - __FUNCTION__, |
|
| 2837 | - __LINE__ |
|
| 2838 | - ); |
|
| 2839 | - return false; |
|
| 2840 | - } |
|
| 2841 | - } |
|
| 2842 | - return true; |
|
| 2843 | - } |
|
| 2844 | - |
|
| 2845 | - |
|
| 2846 | - |
|
| 2847 | - /** |
|
| 2848 | - * Checks the database for an item that conflicts (ie, if this item were |
|
| 2849 | - * saved to the DB would break some uniqueness requirement, like a primary key |
|
| 2850 | - * or an index primary key set) with the item specified. $id_obj_or_fields_array |
|
| 2851 | - * can be either an EE_Base_Class or an array of fields n values |
|
| 2852 | - * |
|
| 2853 | - * @param EE_Base_Class|array $obj_or_fields_array |
|
| 2854 | - * @param boolean $include_primary_key whether to use the model object's primary key |
|
| 2855 | - * when looking for conflicts |
|
| 2856 | - * (ie, if false, we ignore the model object's primary key |
|
| 2857 | - * when finding "conflicts". If true, it's also considered). |
|
| 2858 | - * Only works for INT primary key, |
|
| 2859 | - * STRING primary keys cannot be ignored |
|
| 2860 | - * @throws EE_Error |
|
| 2861 | - * @return EE_Base_Class|array |
|
| 2862 | - */ |
|
| 2863 | - public function get_one_conflicting($obj_or_fields_array, $include_primary_key = true) |
|
| 2864 | - { |
|
| 2865 | - if ($obj_or_fields_array instanceof EE_Base_Class) { |
|
| 2866 | - $fields_n_values = $obj_or_fields_array->model_field_array(); |
|
| 2867 | - } elseif (is_array($obj_or_fields_array)) { |
|
| 2868 | - $fields_n_values = $obj_or_fields_array; |
|
| 2869 | - } else { |
|
| 2870 | - throw new EE_Error( |
|
| 2871 | - sprintf( |
|
| 2872 | - __( |
|
| 2873 | - "%s get_all_conflicting should be called with a model object or an array of field names and values, you provided %d", |
|
| 2874 | - "event_espresso" |
|
| 2875 | - ), |
|
| 2876 | - get_class($this), |
|
| 2877 | - $obj_or_fields_array |
|
| 2878 | - ) |
|
| 2879 | - ); |
|
| 2880 | - } |
|
| 2881 | - $query_params = array(); |
|
| 2882 | - if ($this->has_primary_key_field() |
|
| 2883 | - && ($include_primary_key |
|
| 2884 | - || $this->get_primary_key_field() |
|
| 2885 | - instanceof |
|
| 2886 | - EE_Primary_Key_String_Field) |
|
| 2887 | - && isset($fields_n_values[ $this->primary_key_name() ]) |
|
| 2888 | - ) { |
|
| 2889 | - $query_params[0]['OR'][ $this->primary_key_name() ] = $fields_n_values[ $this->primary_key_name() ]; |
|
| 2890 | - } |
|
| 2891 | - foreach ($this->unique_indexes() as $unique_index_name => $unique_index) { |
|
| 2892 | - $uniqueness_where_params = array_intersect_key($fields_n_values, $unique_index->fields()); |
|
| 2893 | - $query_params[0]['OR'][ 'AND*' . $unique_index_name ] = $uniqueness_where_params; |
|
| 2894 | - } |
|
| 2895 | - // if there is nothing to base this search on, then we shouldn't find anything |
|
| 2896 | - if (empty($query_params)) { |
|
| 2897 | - return array(); |
|
| 2898 | - } |
|
| 2899 | - return $this->get_one($query_params); |
|
| 2900 | - } |
|
| 2901 | - |
|
| 2902 | - |
|
| 2903 | - |
|
| 2904 | - /** |
|
| 2905 | - * Like count, but is optimized and returns a boolean instead of an int |
|
| 2906 | - * |
|
| 2907 | - * @param array $query_params |
|
| 2908 | - * @return boolean |
|
| 2909 | - * @throws EE_Error |
|
| 2910 | - */ |
|
| 2911 | - public function exists($query_params) |
|
| 2912 | - { |
|
| 2913 | - $query_params['limit'] = 1; |
|
| 2914 | - return $this->count($query_params) > 0; |
|
| 2915 | - } |
|
| 2916 | - |
|
| 2917 | - |
|
| 2918 | - |
|
| 2919 | - /** |
|
| 2920 | - * Wrapper for exists, except ignores default query parameters so we're only considering ID |
|
| 2921 | - * |
|
| 2922 | - * @param int|string $id |
|
| 2923 | - * @return boolean |
|
| 2924 | - * @throws EE_Error |
|
| 2925 | - */ |
|
| 2926 | - public function exists_by_ID($id) |
|
| 2927 | - { |
|
| 2928 | - return $this->exists( |
|
| 2929 | - array( |
|
| 2930 | - 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
| 2931 | - array( |
|
| 2932 | - $this->primary_key_name() => $id, |
|
| 2933 | - ), |
|
| 2934 | - ) |
|
| 2935 | - ); |
|
| 2936 | - } |
|
| 2937 | - |
|
| 2938 | - |
|
| 2939 | - |
|
| 2940 | - /** |
|
| 2941 | - * Inserts a new row in $table, using the $cols_n_values which apply to that table. |
|
| 2942 | - * If a $new_id is supplied and if $table is an EE_Other_Table, we assume |
|
| 2943 | - * we need to add a foreign key column to point to $new_id (which should be the primary key's value |
|
| 2944 | - * on the main table) |
|
| 2945 | - * This is protected rather than private because private is not accessible to any child methods and there MAY be |
|
| 2946 | - * cases where we want to call it directly rather than via insert(). |
|
| 2947 | - * |
|
| 2948 | - * @access protected |
|
| 2949 | - * @param EE_Table_Base $table |
|
| 2950 | - * @param array $fields_n_values each key should be in field's keys, and value should be an int, string or |
|
| 2951 | - * float |
|
| 2952 | - * @param int $new_id for now we assume only int keys |
|
| 2953 | - * @throws EE_Error |
|
| 2954 | - * @global WPDB $wpdb only used to get the $wpdb->insert_id after performing an insert |
|
| 2955 | - * @return int ID of new row inserted, or FALSE on failure |
|
| 2956 | - */ |
|
| 2957 | - protected function _insert_into_specific_table(EE_Table_Base $table, $fields_n_values, $new_id = 0) |
|
| 2958 | - { |
|
| 2959 | - global $wpdb; |
|
| 2960 | - $insertion_col_n_values = array(); |
|
| 2961 | - $format_for_insertion = array(); |
|
| 2962 | - $fields_on_table = $this->_get_fields_for_table($table->get_table_alias()); |
|
| 2963 | - foreach ($fields_on_table as $field_name => $field_obj) { |
|
| 2964 | - // check if its an auto-incrementing column, in which case we should just leave it to do its autoincrement thing |
|
| 2965 | - if ($field_obj->is_auto_increment()) { |
|
| 2966 | - continue; |
|
| 2967 | - } |
|
| 2968 | - $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
| 2969 | - // if the value we want to assign it to is NULL, just don't mention it for the insertion |
|
| 2970 | - if ($prepared_value !== null) { |
|
| 2971 | - $insertion_col_n_values[ $field_obj->get_table_column() ] = $prepared_value; |
|
| 2972 | - $format_for_insertion[] = $field_obj->get_wpdb_data_type(); |
|
| 2973 | - } |
|
| 2974 | - } |
|
| 2975 | - if ($table instanceof EE_Secondary_Table && $new_id) { |
|
| 2976 | - // its not the main table, so we should have already saved the main table's PK which we just inserted |
|
| 2977 | - // so add the fk to the main table as a column |
|
| 2978 | - $insertion_col_n_values[ $table->get_fk_on_table() ] = $new_id; |
|
| 2979 | - $format_for_insertion[] = '%d';// yes right now we're only allowing these foreign keys to be INTs |
|
| 2980 | - } |
|
| 2981 | - // insert the new entry |
|
| 2982 | - $result = $this->_do_wpdb_query( |
|
| 2983 | - 'insert', |
|
| 2984 | - array($table->get_table_name(), $insertion_col_n_values, $format_for_insertion) |
|
| 2985 | - ); |
|
| 2986 | - if ($result === false) { |
|
| 2987 | - return false; |
|
| 2988 | - } |
|
| 2989 | - // ok, now what do we return for the ID of the newly-inserted thing? |
|
| 2990 | - if ($this->has_primary_key_field()) { |
|
| 2991 | - if ($this->get_primary_key_field()->is_auto_increment()) { |
|
| 2992 | - return $wpdb->insert_id; |
|
| 2993 | - } |
|
| 2994 | - // it's not an auto-increment primary key, so |
|
| 2995 | - // it must have been supplied |
|
| 2996 | - return $fields_n_values[ $this->get_primary_key_field()->get_name() ]; |
|
| 2997 | - } |
|
| 2998 | - // we can't return a primary key because there is none. instead return |
|
| 2999 | - // a unique string indicating this model |
|
| 3000 | - return $this->get_index_primary_key_string($fields_n_values); |
|
| 3001 | - } |
|
| 3002 | - |
|
| 3003 | - |
|
| 3004 | - |
|
| 3005 | - /** |
|
| 3006 | - * Prepare the $field_obj 's value in $fields_n_values for use in the database. |
|
| 3007 | - * If the field doesn't allow NULL, try to use its default. (If it doesn't allow NULL, |
|
| 3008 | - * and there is no default, we pass it along. WPDB will take care of it) |
|
| 3009 | - * |
|
| 3010 | - * @param EE_Model_Field_Base $field_obj |
|
| 3011 | - * @param array $fields_n_values |
|
| 3012 | - * @return mixed string|int|float depending on what the table column will be expecting |
|
| 3013 | - * @throws EE_Error |
|
| 3014 | - */ |
|
| 3015 | - protected function _prepare_value_or_use_default($field_obj, $fields_n_values) |
|
| 3016 | - { |
|
| 3017 | - // if this field doesn't allow nullable, don't allow it |
|
| 3018 | - if (! $field_obj->is_nullable() |
|
| 3019 | - && ( |
|
| 3020 | - ! isset($fields_n_values[ $field_obj->get_name() ]) |
|
| 3021 | - || $fields_n_values[ $field_obj->get_name() ] === null |
|
| 3022 | - ) |
|
| 3023 | - ) { |
|
| 3024 | - $fields_n_values[ $field_obj->get_name() ] = $field_obj->get_default_value(); |
|
| 3025 | - } |
|
| 3026 | - $unprepared_value = isset($fields_n_values[ $field_obj->get_name() ]) |
|
| 3027 | - ? $fields_n_values[ $field_obj->get_name() ] |
|
| 3028 | - : null; |
|
| 3029 | - return $this->_prepare_value_for_use_in_db($unprepared_value, $field_obj); |
|
| 3030 | - } |
|
| 3031 | - |
|
| 3032 | - |
|
| 3033 | - |
|
| 3034 | - /** |
|
| 3035 | - * Consolidates code for preparing a value supplied to the model for use int eh db. Calls the field's |
|
| 3036 | - * prepare_for_use_in_db method on the value, and depending on $value_already_prepare_by_model_obj, may also call |
|
| 3037 | - * the field's prepare_for_set() method. |
|
| 3038 | - * |
|
| 3039 | - * @param mixed $value value in the client code domain if $value_already_prepared_by_model_object is |
|
| 3040 | - * false, otherwise a value in the model object's domain (see lengthy comment at |
|
| 3041 | - * top of file) |
|
| 3042 | - * @param EE_Model_Field_Base $field field which will be doing the preparing of the value. If null, we assume |
|
| 3043 | - * $value is a custom selection |
|
| 3044 | - * @return mixed a value ready for use in the database for insertions, updating, or in a where clause |
|
| 3045 | - */ |
|
| 3046 | - private function _prepare_value_for_use_in_db($value, $field) |
|
| 3047 | - { |
|
| 3048 | - if ($field && $field instanceof EE_Model_Field_Base) { |
|
| 3049 | - // phpcs:disable PSR2.ControlStructures.SwitchDeclaration.TerminatingComment |
|
| 3050 | - switch ($this->_values_already_prepared_by_model_object) { |
|
| 3051 | - /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 3052 | - case self::not_prepared_by_model_object: |
|
| 3053 | - $value = $field->prepare_for_set($value); |
|
| 3054 | - // purposefully left out "return" |
|
| 3055 | - case self::prepared_by_model_object: |
|
| 3056 | - /** @noinspection SuspiciousAssignmentsInspection */ |
|
| 3057 | - $value = $field->prepare_for_use_in_db($value); |
|
| 3058 | - case self::prepared_for_use_in_db: |
|
| 3059 | - // leave the value alone |
|
| 3060 | - } |
|
| 3061 | - return $value; |
|
| 3062 | - // phpcs:enable |
|
| 3063 | - } |
|
| 3064 | - return $value; |
|
| 3065 | - } |
|
| 3066 | - |
|
| 3067 | - |
|
| 3068 | - |
|
| 3069 | - /** |
|
| 3070 | - * Returns the main table on this model |
|
| 3071 | - * |
|
| 3072 | - * @return EE_Primary_Table |
|
| 3073 | - * @throws EE_Error |
|
| 3074 | - */ |
|
| 3075 | - protected function _get_main_table() |
|
| 3076 | - { |
|
| 3077 | - foreach ($this->_tables as $table) { |
|
| 3078 | - if ($table instanceof EE_Primary_Table) { |
|
| 3079 | - return $table; |
|
| 3080 | - } |
|
| 3081 | - } |
|
| 3082 | - throw new EE_Error(sprintf(__( |
|
| 3083 | - 'There are no main tables on %s. They should be added to _tables array in the constructor', |
|
| 3084 | - 'event_espresso' |
|
| 3085 | - ), get_class($this))); |
|
| 3086 | - } |
|
| 3087 | - |
|
| 3088 | - |
|
| 3089 | - |
|
| 3090 | - /** |
|
| 3091 | - * table |
|
| 3092 | - * returns EE_Primary_Table table name |
|
| 3093 | - * |
|
| 3094 | - * @return string |
|
| 3095 | - * @throws EE_Error |
|
| 3096 | - */ |
|
| 3097 | - public function table() |
|
| 3098 | - { |
|
| 3099 | - return $this->_get_main_table()->get_table_name(); |
|
| 3100 | - } |
|
| 3101 | - |
|
| 3102 | - |
|
| 3103 | - |
|
| 3104 | - /** |
|
| 3105 | - * table |
|
| 3106 | - * returns first EE_Secondary_Table table name |
|
| 3107 | - * |
|
| 3108 | - * @return string |
|
| 3109 | - */ |
|
| 3110 | - public function second_table() |
|
| 3111 | - { |
|
| 3112 | - // grab second table from tables array |
|
| 3113 | - $second_table = end($this->_tables); |
|
| 3114 | - return $second_table instanceof EE_Secondary_Table ? $second_table->get_table_name() : null; |
|
| 3115 | - } |
|
| 3116 | - |
|
| 3117 | - |
|
| 3118 | - |
|
| 3119 | - /** |
|
| 3120 | - * get_table_obj_by_alias |
|
| 3121 | - * returns table name given it's alias |
|
| 3122 | - * |
|
| 3123 | - * @param string $table_alias |
|
| 3124 | - * @return EE_Primary_Table | EE_Secondary_Table |
|
| 3125 | - */ |
|
| 3126 | - public function get_table_obj_by_alias($table_alias = '') |
|
| 3127 | - { |
|
| 3128 | - return isset($this->_tables[ $table_alias ]) ? $this->_tables[ $table_alias ] : null; |
|
| 3129 | - } |
|
| 3130 | - |
|
| 3131 | - |
|
| 3132 | - |
|
| 3133 | - /** |
|
| 3134 | - * Gets all the tables of type EE_Other_Table from EEM_CPT_Basel_Model::_tables |
|
| 3135 | - * |
|
| 3136 | - * @return EE_Secondary_Table[] |
|
| 3137 | - */ |
|
| 3138 | - protected function _get_other_tables() |
|
| 3139 | - { |
|
| 3140 | - $other_tables = array(); |
|
| 3141 | - foreach ($this->_tables as $table_alias => $table) { |
|
| 3142 | - if ($table instanceof EE_Secondary_Table) { |
|
| 3143 | - $other_tables[ $table_alias ] = $table; |
|
| 3144 | - } |
|
| 3145 | - } |
|
| 3146 | - return $other_tables; |
|
| 3147 | - } |
|
| 3148 | - |
|
| 3149 | - |
|
| 3150 | - |
|
| 3151 | - /** |
|
| 3152 | - * Finds all the fields that correspond to the given table |
|
| 3153 | - * |
|
| 3154 | - * @param string $table_alias , array key in EEM_Base::_tables |
|
| 3155 | - * @return EE_Model_Field_Base[] |
|
| 3156 | - */ |
|
| 3157 | - public function _get_fields_for_table($table_alias) |
|
| 3158 | - { |
|
| 3159 | - return $this->_fields[ $table_alias ]; |
|
| 3160 | - } |
|
| 3161 | - |
|
| 3162 | - |
|
| 3163 | - |
|
| 3164 | - /** |
|
| 3165 | - * Recurses through all the where parameters, and finds all the related models we'll need |
|
| 3166 | - * to complete this query. Eg, given where parameters like array('EVT_ID'=>3) from within Event model, we won't |
|
| 3167 | - * need any related models. But if the array were array('Registrations.REG_ID'=>3), we'd need the related |
|
| 3168 | - * Registration model. If it were array('Registrations.Transactions.Payments.PAY_ID'=>3), then we'd need the |
|
| 3169 | - * related Registration, Transaction, and Payment models. |
|
| 3170 | - * |
|
| 3171 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 3172 | - * @return EE_Model_Query_Info_Carrier |
|
| 3173 | - * @throws EE_Error |
|
| 3174 | - */ |
|
| 3175 | - public function _extract_related_models_from_query($query_params) |
|
| 3176 | - { |
|
| 3177 | - $query_info_carrier = new EE_Model_Query_Info_Carrier(); |
|
| 3178 | - if (array_key_exists(0, $query_params)) { |
|
| 3179 | - $this->_extract_related_models_from_sub_params_array_keys($query_params[0], $query_info_carrier, 0); |
|
| 3180 | - } |
|
| 3181 | - if (array_key_exists('group_by', $query_params)) { |
|
| 3182 | - if (is_array($query_params['group_by'])) { |
|
| 3183 | - $this->_extract_related_models_from_sub_params_array_values( |
|
| 3184 | - $query_params['group_by'], |
|
| 3185 | - $query_info_carrier, |
|
| 3186 | - 'group_by' |
|
| 3187 | - ); |
|
| 3188 | - } elseif (! empty($query_params['group_by'])) { |
|
| 3189 | - $this->_extract_related_model_info_from_query_param( |
|
| 3190 | - $query_params['group_by'], |
|
| 3191 | - $query_info_carrier, |
|
| 3192 | - 'group_by' |
|
| 3193 | - ); |
|
| 3194 | - } |
|
| 3195 | - } |
|
| 3196 | - if (array_key_exists('having', $query_params)) { |
|
| 3197 | - $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3198 | - $query_params[0], |
|
| 3199 | - $query_info_carrier, |
|
| 3200 | - 'having' |
|
| 3201 | - ); |
|
| 3202 | - } |
|
| 3203 | - if (array_key_exists('order_by', $query_params)) { |
|
| 3204 | - if (is_array($query_params['order_by'])) { |
|
| 3205 | - $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3206 | - $query_params['order_by'], |
|
| 3207 | - $query_info_carrier, |
|
| 3208 | - 'order_by' |
|
| 3209 | - ); |
|
| 3210 | - } elseif (! empty($query_params['order_by'])) { |
|
| 3211 | - $this->_extract_related_model_info_from_query_param( |
|
| 3212 | - $query_params['order_by'], |
|
| 3213 | - $query_info_carrier, |
|
| 3214 | - 'order_by' |
|
| 3215 | - ); |
|
| 3216 | - } |
|
| 3217 | - } |
|
| 3218 | - if (array_key_exists('force_join', $query_params)) { |
|
| 3219 | - $this->_extract_related_models_from_sub_params_array_values( |
|
| 3220 | - $query_params['force_join'], |
|
| 3221 | - $query_info_carrier, |
|
| 3222 | - 'force_join' |
|
| 3223 | - ); |
|
| 3224 | - } |
|
| 3225 | - $this->extractRelatedModelsFromCustomSelects($query_info_carrier); |
|
| 3226 | - return $query_info_carrier; |
|
| 3227 | - } |
|
| 3228 | - |
|
| 3229 | - |
|
| 3230 | - |
|
| 3231 | - /** |
|
| 3232 | - * For extracting related models from WHERE (0), HAVING (having), ORDER BY (order_by) or forced joins (force_join) |
|
| 3233 | - * |
|
| 3234 | - * @param array $sub_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#-0-where-conditions |
|
| 3235 | - * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
| 3236 | - * @param string $query_param_type one of $this->_allowed_query_params |
|
| 3237 | - * @throws EE_Error |
|
| 3238 | - * @return \EE_Model_Query_Info_Carrier |
|
| 3239 | - */ |
|
| 3240 | - private function _extract_related_models_from_sub_params_array_keys( |
|
| 3241 | - $sub_query_params, |
|
| 3242 | - EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
| 3243 | - $query_param_type |
|
| 3244 | - ) { |
|
| 3245 | - if (! empty($sub_query_params)) { |
|
| 3246 | - $sub_query_params = (array) $sub_query_params; |
|
| 3247 | - foreach ($sub_query_params as $param => $possibly_array_of_params) { |
|
| 3248 | - // $param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
| 3249 | - $this->_extract_related_model_info_from_query_param( |
|
| 3250 | - $param, |
|
| 3251 | - $model_query_info_carrier, |
|
| 3252 | - $query_param_type |
|
| 3253 | - ); |
|
| 3254 | - // if $possibly_array_of_params is an array, try recursing into it, searching for keys which |
|
| 3255 | - // indicate needed joins. Eg, array('NOT'=>array('Registration.TXN_ID'=>23)). In this case, we tried |
|
| 3256 | - // extracting models out of the 'NOT', which obviously wasn't successful, and then we recurse into the value |
|
| 3257 | - // of array('Registration.TXN_ID'=>23) |
|
| 3258 | - $query_param_sans_stars = $this->_remove_stars_and_anything_after_from_condition_query_param_key($param); |
|
| 3259 | - if (in_array($query_param_sans_stars, $this->_logic_query_param_keys, true)) { |
|
| 3260 | - if (! is_array($possibly_array_of_params)) { |
|
| 3261 | - throw new EE_Error(sprintf( |
|
| 3262 | - __( |
|
| 3263 | - "You used a special where query param %s, but the value isn't an array of where query params, it's just %s'. It should be an array, eg array('EVT_ID'=>23,'OR'=>array('Venue.VNU_ID'=>32,'Venue.VNU_name'=>'monkey_land'))", |
|
| 3264 | - "event_espresso" |
|
| 3265 | - ), |
|
| 3266 | - $param, |
|
| 3267 | - $possibly_array_of_params |
|
| 3268 | - )); |
|
| 3269 | - } |
|
| 3270 | - $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3271 | - $possibly_array_of_params, |
|
| 3272 | - $model_query_info_carrier, |
|
| 3273 | - $query_param_type |
|
| 3274 | - ); |
|
| 3275 | - } elseif ($query_param_type === 0 // ie WHERE |
|
| 3276 | - && is_array($possibly_array_of_params) |
|
| 3277 | - && isset($possibly_array_of_params[2]) |
|
| 3278 | - && $possibly_array_of_params[2] == true |
|
| 3279 | - ) { |
|
| 3280 | - // then $possible_array_of_params looks something like array('<','DTT_sold',true) |
|
| 3281 | - // indicating that $possible_array_of_params[1] is actually a field name, |
|
| 3282 | - // from which we should extract query parameters! |
|
| 3283 | - if (! isset($possibly_array_of_params[0], $possibly_array_of_params[1])) { |
|
| 3284 | - throw new EE_Error(sprintf(__( |
|
| 3285 | - "Improperly formed query parameter %s. It should be numerically indexed like array('<','DTT_sold',true); but you provided %s", |
|
| 3286 | - "event_espresso" |
|
| 3287 | - ), $query_param_type, implode(",", $possibly_array_of_params))); |
|
| 3288 | - } |
|
| 3289 | - $this->_extract_related_model_info_from_query_param( |
|
| 3290 | - $possibly_array_of_params[1], |
|
| 3291 | - $model_query_info_carrier, |
|
| 3292 | - $query_param_type |
|
| 3293 | - ); |
|
| 3294 | - } |
|
| 3295 | - } |
|
| 3296 | - } |
|
| 3297 | - return $model_query_info_carrier; |
|
| 3298 | - } |
|
| 3299 | - |
|
| 3300 | - |
|
| 3301 | - |
|
| 3302 | - /** |
|
| 3303 | - * For extracting related models from forced_joins, where the array values contain the info about what |
|
| 3304 | - * models to join with. Eg an array like array('Attendee','Price.Price_Type'); |
|
| 3305 | - * |
|
| 3306 | - * @param array $sub_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 3307 | - * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
| 3308 | - * @param string $query_param_type one of $this->_allowed_query_params |
|
| 3309 | - * @throws EE_Error |
|
| 3310 | - * @return \EE_Model_Query_Info_Carrier |
|
| 3311 | - */ |
|
| 3312 | - private function _extract_related_models_from_sub_params_array_values( |
|
| 3313 | - $sub_query_params, |
|
| 3314 | - EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
| 3315 | - $query_param_type |
|
| 3316 | - ) { |
|
| 3317 | - if (! empty($sub_query_params)) { |
|
| 3318 | - if (! is_array($sub_query_params)) { |
|
| 3319 | - throw new EE_Error(sprintf( |
|
| 3320 | - __("Query parameter %s should be an array, but it isn't.", "event_espresso"), |
|
| 3321 | - $sub_query_params |
|
| 3322 | - )); |
|
| 3323 | - } |
|
| 3324 | - foreach ($sub_query_params as $param) { |
|
| 3325 | - // $param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
| 3326 | - $this->_extract_related_model_info_from_query_param( |
|
| 3327 | - $param, |
|
| 3328 | - $model_query_info_carrier, |
|
| 3329 | - $query_param_type |
|
| 3330 | - ); |
|
| 3331 | - } |
|
| 3332 | - } |
|
| 3333 | - return $model_query_info_carrier; |
|
| 3334 | - } |
|
| 3335 | - |
|
| 3336 | - |
|
| 3337 | - /** |
|
| 3338 | - * Extract all the query parts from model query params |
|
| 3339 | - * and put into a EEM_Related_Model_Info_Carrier for easy extraction into a query. We create this object |
|
| 3340 | - * instead of directly constructing the SQL because often we need to extract info from the $query_params |
|
| 3341 | - * but use them in a different order. Eg, we need to know what models we are querying |
|
| 3342 | - * before we know what joins to perform. However, we need to know what data types correspond to which fields on |
|
| 3343 | - * other models before we can finalize the where clause SQL. |
|
| 3344 | - * |
|
| 3345 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 3346 | - * @throws EE_Error |
|
| 3347 | - * @return EE_Model_Query_Info_Carrier |
|
| 3348 | - * @throws ModelConfigurationException |
|
| 3349 | - */ |
|
| 3350 | - public function _create_model_query_info_carrier($query_params) |
|
| 3351 | - { |
|
| 3352 | - if (! is_array($query_params)) { |
|
| 3353 | - EE_Error::doing_it_wrong( |
|
| 3354 | - 'EEM_Base::_create_model_query_info_carrier', |
|
| 3355 | - sprintf( |
|
| 3356 | - __( |
|
| 3357 | - '$query_params should be an array, you passed a variable of type %s', |
|
| 3358 | - 'event_espresso' |
|
| 3359 | - ), |
|
| 3360 | - gettype($query_params) |
|
| 3361 | - ), |
|
| 3362 | - '4.6.0' |
|
| 3363 | - ); |
|
| 3364 | - $query_params = array(); |
|
| 3365 | - } |
|
| 3366 | - $query_params[0] = isset($query_params[0]) ? $query_params[0] : array(); |
|
| 3367 | - // first check if we should alter the query to account for caps or not |
|
| 3368 | - // because the caps might require us to do extra joins |
|
| 3369 | - if (isset($query_params['caps']) && $query_params['caps'] !== 'none') { |
|
| 3370 | - $query_params[0] = array_replace_recursive( |
|
| 3371 | - $query_params[0], |
|
| 3372 | - $this->caps_where_conditions( |
|
| 3373 | - $query_params['caps'] |
|
| 3374 | - ) |
|
| 3375 | - ); |
|
| 3376 | - } |
|
| 3377 | - |
|
| 3378 | - // check if we should alter the query to remove data related to protected |
|
| 3379 | - // custom post types |
|
| 3380 | - if (isset($query_params['exclude_protected']) && $query_params['exclude_protected'] === true) { |
|
| 3381 | - $where_param_key_for_password = $this->modelChainAndPassword(); |
|
| 3382 | - // only include if related to a cpt where no password has been set |
|
| 3383 | - $query_params[0]['OR*nopassword'] = array( |
|
| 3384 | - $where_param_key_for_password => '', |
|
| 3385 | - $where_param_key_for_password . '*' => array('IS_NULL') |
|
| 3386 | - ); |
|
| 3387 | - } |
|
| 3388 | - $query_object = $this->_extract_related_models_from_query($query_params); |
|
| 3389 | - // verify where_query_params has NO numeric indexes.... that's simply not how you use it! |
|
| 3390 | - foreach ($query_params[0] as $key => $value) { |
|
| 3391 | - if (is_int($key)) { |
|
| 3392 | - throw new EE_Error( |
|
| 3393 | - sprintf( |
|
| 3394 | - __( |
|
| 3395 | - "WHERE query params must NOT be numerically-indexed. You provided the array key '%s' for value '%s' while querying model %s. All the query params provided were '%s' Please read documentation on EEM_Base::get_all.", |
|
| 3396 | - "event_espresso" |
|
| 3397 | - ), |
|
| 3398 | - $key, |
|
| 3399 | - var_export($value, true), |
|
| 3400 | - var_export($query_params, true), |
|
| 3401 | - get_class($this) |
|
| 3402 | - ) |
|
| 3403 | - ); |
|
| 3404 | - } |
|
| 3405 | - } |
|
| 3406 | - if (array_key_exists('default_where_conditions', $query_params) |
|
| 3407 | - && ! empty($query_params['default_where_conditions']) |
|
| 3408 | - ) { |
|
| 3409 | - $use_default_where_conditions = $query_params['default_where_conditions']; |
|
| 3410 | - } else { |
|
| 3411 | - $use_default_where_conditions = EEM_Base::default_where_conditions_all; |
|
| 3412 | - } |
|
| 3413 | - $query_params[0] = array_merge( |
|
| 3414 | - $this->_get_default_where_conditions_for_models_in_query( |
|
| 3415 | - $query_object, |
|
| 3416 | - $use_default_where_conditions, |
|
| 3417 | - $query_params[0] |
|
| 3418 | - ), |
|
| 3419 | - $query_params[0] |
|
| 3420 | - ); |
|
| 3421 | - $query_object->set_where_sql($this->_construct_where_clause($query_params[0])); |
|
| 3422 | - // if this is a "on_join_limit" then we are limiting on on a specific table in a multi_table join. |
|
| 3423 | - // So we need to setup a subquery and use that for the main join. |
|
| 3424 | - // Note for now this only works on the primary table for the model. |
|
| 3425 | - // So for instance, you could set the limit array like this: |
|
| 3426 | - // array( 'on_join_limit' => array('Primary_Table_Alias', array(1,10) ) ) |
|
| 3427 | - if (array_key_exists('on_join_limit', $query_params) && ! empty($query_params['on_join_limit'])) { |
|
| 3428 | - $query_object->set_main_model_join_sql( |
|
| 3429 | - $this->_construct_limit_join_select( |
|
| 3430 | - $query_params['on_join_limit'][0], |
|
| 3431 | - $query_params['on_join_limit'][1] |
|
| 3432 | - ) |
|
| 3433 | - ); |
|
| 3434 | - } |
|
| 3435 | - // set limit |
|
| 3436 | - if (array_key_exists('limit', $query_params)) { |
|
| 3437 | - if (is_array($query_params['limit'])) { |
|
| 3438 | - if (! isset($query_params['limit'][0], $query_params['limit'][1])) { |
|
| 3439 | - $e = sprintf( |
|
| 3440 | - __( |
|
| 3441 | - "Invalid DB query. You passed '%s' for the LIMIT, but only the following are valid: an integer, string representing an integer, a string like 'int,int', or an array like array(int,int)", |
|
| 3442 | - "event_espresso" |
|
| 3443 | - ), |
|
| 3444 | - http_build_query($query_params['limit']) |
|
| 3445 | - ); |
|
| 3446 | - throw new EE_Error($e . "|" . $e); |
|
| 3447 | - } |
|
| 3448 | - // they passed us an array for the limit. Assume it's like array(50,25), meaning offset by 50, and get 25 |
|
| 3449 | - $query_object->set_limit_sql(" LIMIT " . $query_params['limit'][0] . "," . $query_params['limit'][1]); |
|
| 3450 | - } elseif (! empty($query_params['limit'])) { |
|
| 3451 | - $query_object->set_limit_sql(" LIMIT " . $query_params['limit']); |
|
| 3452 | - } |
|
| 3453 | - } |
|
| 3454 | - // set order by |
|
| 3455 | - if (array_key_exists('order_by', $query_params)) { |
|
| 3456 | - if (is_array($query_params['order_by'])) { |
|
| 3457 | - // if they're using 'order_by' as an array, they can't use 'order' (because 'order_by' must |
|
| 3458 | - // specify whether to ascend or descend on each field. Eg 'order_by'=>array('EVT_ID'=>'ASC'). So |
|
| 3459 | - // including 'order' wouldn't make any sense if 'order_by' has already specified which way to order! |
|
| 3460 | - if (array_key_exists('order', $query_params)) { |
|
| 3461 | - throw new EE_Error( |
|
| 3462 | - sprintf( |
|
| 3463 | - __( |
|
| 3464 | - "In querying %s, we are using query parameter 'order_by' as an array (keys:%s,values:%s), and so we can't use query parameter 'order' (value %s). You should just use the 'order_by' parameter ", |
|
| 3465 | - "event_espresso" |
|
| 3466 | - ), |
|
| 3467 | - get_class($this), |
|
| 3468 | - implode(", ", array_keys($query_params['order_by'])), |
|
| 3469 | - implode(", ", $query_params['order_by']), |
|
| 3470 | - $query_params['order'] |
|
| 3471 | - ) |
|
| 3472 | - ); |
|
| 3473 | - } |
|
| 3474 | - $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3475 | - $query_params['order_by'], |
|
| 3476 | - $query_object, |
|
| 3477 | - 'order_by' |
|
| 3478 | - ); |
|
| 3479 | - // assume it's an array of fields to order by |
|
| 3480 | - $order_array = array(); |
|
| 3481 | - foreach ($query_params['order_by'] as $field_name_to_order_by => $order) { |
|
| 3482 | - $order = $this->_extract_order($order); |
|
| 3483 | - $order_array[] = $this->_deduce_column_name_from_query_param($field_name_to_order_by) . SP . $order; |
|
| 3484 | - } |
|
| 3485 | - $query_object->set_order_by_sql(" ORDER BY " . implode(",", $order_array)); |
|
| 3486 | - } elseif (! empty($query_params['order_by'])) { |
|
| 3487 | - $this->_extract_related_model_info_from_query_param( |
|
| 3488 | - $query_params['order_by'], |
|
| 3489 | - $query_object, |
|
| 3490 | - 'order', |
|
| 3491 | - $query_params['order_by'] |
|
| 3492 | - ); |
|
| 3493 | - $order = isset($query_params['order']) |
|
| 3494 | - ? $this->_extract_order($query_params['order']) |
|
| 3495 | - : 'DESC'; |
|
| 3496 | - $query_object->set_order_by_sql( |
|
| 3497 | - " ORDER BY " . $this->_deduce_column_name_from_query_param($query_params['order_by']) . SP . $order |
|
| 3498 | - ); |
|
| 3499 | - } |
|
| 3500 | - } |
|
| 3501 | - // if 'order_by' wasn't set, maybe they are just using 'order' on its own? |
|
| 3502 | - if (! array_key_exists('order_by', $query_params) |
|
| 3503 | - && array_key_exists('order', $query_params) |
|
| 3504 | - && ! empty($query_params['order']) |
|
| 3505 | - ) { |
|
| 3506 | - $pk_field = $this->get_primary_key_field(); |
|
| 3507 | - $order = $this->_extract_order($query_params['order']); |
|
| 3508 | - $query_object->set_order_by_sql(" ORDER BY " . $pk_field->get_qualified_column() . SP . $order); |
|
| 3509 | - } |
|
| 3510 | - // set group by |
|
| 3511 | - if (array_key_exists('group_by', $query_params)) { |
|
| 3512 | - if (is_array($query_params['group_by'])) { |
|
| 3513 | - // it's an array, so assume we'll be grouping by a bunch of stuff |
|
| 3514 | - $group_by_array = array(); |
|
| 3515 | - foreach ($query_params['group_by'] as $field_name_to_group_by) { |
|
| 3516 | - $group_by_array[] = $this->_deduce_column_name_from_query_param($field_name_to_group_by); |
|
| 3517 | - } |
|
| 3518 | - $query_object->set_group_by_sql(" GROUP BY " . implode(", ", $group_by_array)); |
|
| 3519 | - } elseif (! empty($query_params['group_by'])) { |
|
| 3520 | - $query_object->set_group_by_sql( |
|
| 3521 | - " GROUP BY " . $this->_deduce_column_name_from_query_param($query_params['group_by']) |
|
| 3522 | - ); |
|
| 3523 | - } |
|
| 3524 | - } |
|
| 3525 | - // set having |
|
| 3526 | - if (array_key_exists('having', $query_params) && $query_params['having']) { |
|
| 3527 | - $query_object->set_having_sql($this->_construct_having_clause($query_params['having'])); |
|
| 3528 | - } |
|
| 3529 | - // now, just verify they didn't pass anything wack |
|
| 3530 | - foreach ($query_params as $query_key => $query_value) { |
|
| 3531 | - if (! in_array($query_key, $this->_allowed_query_params, true)) { |
|
| 3532 | - throw new EE_Error( |
|
| 3533 | - sprintf( |
|
| 3534 | - __( |
|
| 3535 | - "You passed %s as a query parameter to %s, which is illegal! The allowed query parameters are %s", |
|
| 3536 | - 'event_espresso' |
|
| 3537 | - ), |
|
| 3538 | - $query_key, |
|
| 3539 | - get_class($this), |
|
| 3540 | - // print_r( $this->_allowed_query_params, TRUE ) |
|
| 3541 | - implode(',', $this->_allowed_query_params) |
|
| 3542 | - ) |
|
| 3543 | - ); |
|
| 3544 | - } |
|
| 3545 | - } |
|
| 3546 | - $main_model_join_sql = $query_object->get_main_model_join_sql(); |
|
| 3547 | - if (empty($main_model_join_sql)) { |
|
| 3548 | - $query_object->set_main_model_join_sql($this->_construct_internal_join()); |
|
| 3549 | - } |
|
| 3550 | - return $query_object; |
|
| 3551 | - } |
|
| 3552 | - |
|
| 3553 | - |
|
| 3554 | - |
|
| 3555 | - /** |
|
| 3556 | - * Gets the where conditions that should be imposed on the query based on the |
|
| 3557 | - * context (eg reading frontend, backend, edit or delete). |
|
| 3558 | - * |
|
| 3559 | - * @param string $context one of EEM_Base::valid_cap_contexts() |
|
| 3560 | - * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 3561 | - * @throws EE_Error |
|
| 3562 | - */ |
|
| 3563 | - public function caps_where_conditions($context = self::caps_read) |
|
| 3564 | - { |
|
| 3565 | - EEM_Base::verify_is_valid_cap_context($context); |
|
| 3566 | - $cap_where_conditions = array(); |
|
| 3567 | - $cap_restrictions = $this->caps_missing($context); |
|
| 3568 | - /** |
|
| 3569 | - * @var $cap_restrictions EE_Default_Where_Conditions[] |
|
| 3570 | - */ |
|
| 3571 | - foreach ($cap_restrictions as $cap => $restriction_if_no_cap) { |
|
| 3572 | - $cap_where_conditions = array_replace_recursive( |
|
| 3573 | - $cap_where_conditions, |
|
| 3574 | - $restriction_if_no_cap->get_default_where_conditions() |
|
| 3575 | - ); |
|
| 3576 | - } |
|
| 3577 | - return apply_filters( |
|
| 3578 | - 'FHEE__EEM_Base__caps_where_conditions__return', |
|
| 3579 | - $cap_where_conditions, |
|
| 3580 | - $this, |
|
| 3581 | - $context, |
|
| 3582 | - $cap_restrictions |
|
| 3583 | - ); |
|
| 3584 | - } |
|
| 3585 | - |
|
| 3586 | - |
|
| 3587 | - |
|
| 3588 | - /** |
|
| 3589 | - * Verifies that $should_be_order_string is in $this->_allowed_order_values, |
|
| 3590 | - * otherwise throws an exception |
|
| 3591 | - * |
|
| 3592 | - * @param string $should_be_order_string |
|
| 3593 | - * @return string either ASC, asc, DESC or desc |
|
| 3594 | - * @throws EE_Error |
|
| 3595 | - */ |
|
| 3596 | - private function _extract_order($should_be_order_string) |
|
| 3597 | - { |
|
| 3598 | - if (in_array($should_be_order_string, $this->_allowed_order_values)) { |
|
| 3599 | - return $should_be_order_string; |
|
| 3600 | - } |
|
| 3601 | - throw new EE_Error( |
|
| 3602 | - sprintf( |
|
| 3603 | - __( |
|
| 3604 | - "While performing a query on '%s', tried to use '%s' as an order parameter. ", |
|
| 3605 | - "event_espresso" |
|
| 3606 | - ), |
|
| 3607 | - get_class($this), |
|
| 3608 | - $should_be_order_string |
|
| 3609 | - ) |
|
| 3610 | - ); |
|
| 3611 | - } |
|
| 3612 | - |
|
| 3613 | - |
|
| 3614 | - |
|
| 3615 | - /** |
|
| 3616 | - * Looks at all the models which are included in this query, and asks each |
|
| 3617 | - * for their universal_where_params, and returns them in the same format as $query_params[0] (where), |
|
| 3618 | - * so they can be merged |
|
| 3619 | - * |
|
| 3620 | - * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
| 3621 | - * @param string $use_default_where_conditions can be 'none','other_models_only', or 'all'. |
|
| 3622 | - * 'none' means NO default where conditions will |
|
| 3623 | - * be used AT ALL during this query. |
|
| 3624 | - * 'other_models_only' means default where |
|
| 3625 | - * conditions from other models will be used, but |
|
| 3626 | - * not for this primary model. 'all', the default, |
|
| 3627 | - * means default where conditions will apply as |
|
| 3628 | - * normal |
|
| 3629 | - * @param array $where_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 3630 | - * @throws EE_Error |
|
| 3631 | - * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 3632 | - */ |
|
| 3633 | - private function _get_default_where_conditions_for_models_in_query( |
|
| 3634 | - EE_Model_Query_Info_Carrier $query_info_carrier, |
|
| 3635 | - $use_default_where_conditions = EEM_Base::default_where_conditions_all, |
|
| 3636 | - $where_query_params = array() |
|
| 3637 | - ) { |
|
| 3638 | - $allowed_used_default_where_conditions_values = EEM_Base::valid_default_where_conditions(); |
|
| 3639 | - if (! in_array($use_default_where_conditions, $allowed_used_default_where_conditions_values)) { |
|
| 3640 | - throw new EE_Error(sprintf( |
|
| 3641 | - __( |
|
| 3642 | - "You passed an invalid value to the query parameter 'default_where_conditions' of '%s'. Allowed values are %s", |
|
| 3643 | - "event_espresso" |
|
| 3644 | - ), |
|
| 3645 | - $use_default_where_conditions, |
|
| 3646 | - implode(", ", $allowed_used_default_where_conditions_values) |
|
| 3647 | - )); |
|
| 3648 | - } |
|
| 3649 | - $universal_query_params = array(); |
|
| 3650 | - if ($this->_should_use_default_where_conditions($use_default_where_conditions, true)) { |
|
| 3651 | - $universal_query_params = $this->_get_default_where_conditions(); |
|
| 3652 | - } elseif ($this->_should_use_minimum_where_conditions($use_default_where_conditions, true)) { |
|
| 3653 | - $universal_query_params = $this->_get_minimum_where_conditions(); |
|
| 3654 | - } |
|
| 3655 | - foreach ($query_info_carrier->get_model_names_included() as $model_relation_path => $model_name) { |
|
| 3656 | - $related_model = $this->get_related_model_obj($model_name); |
|
| 3657 | - if ($this->_should_use_default_where_conditions($use_default_where_conditions, false)) { |
|
| 3658 | - $related_model_universal_where_params = $related_model->_get_default_where_conditions($model_relation_path); |
|
| 3659 | - } elseif ($this->_should_use_minimum_where_conditions($use_default_where_conditions, false)) { |
|
| 3660 | - $related_model_universal_where_params = $related_model->_get_minimum_where_conditions($model_relation_path); |
|
| 3661 | - } else { |
|
| 3662 | - // we don't want to add full or even minimum default where conditions from this model, so just continue |
|
| 3663 | - continue; |
|
| 3664 | - } |
|
| 3665 | - $overrides = $this->_override_defaults_or_make_null_friendly( |
|
| 3666 | - $related_model_universal_where_params, |
|
| 3667 | - $where_query_params, |
|
| 3668 | - $related_model, |
|
| 3669 | - $model_relation_path |
|
| 3670 | - ); |
|
| 3671 | - $universal_query_params = EEH_Array::merge_arrays_and_overwrite_keys( |
|
| 3672 | - $universal_query_params, |
|
| 3673 | - $overrides |
|
| 3674 | - ); |
|
| 3675 | - } |
|
| 3676 | - return $universal_query_params; |
|
| 3677 | - } |
|
| 3678 | - |
|
| 3679 | - |
|
| 3680 | - |
|
| 3681 | - /** |
|
| 3682 | - * Determines whether or not we should use default where conditions for the model in question |
|
| 3683 | - * (this model, or other related models). |
|
| 3684 | - * Basically, we should use default where conditions on this model if they have requested to use them on all models, |
|
| 3685 | - * this model only, or to use minimum where conditions on all other models and normal where conditions on this one. |
|
| 3686 | - * We should use default where conditions on related models when they requested to use default where conditions |
|
| 3687 | - * on all models, or specifically just on other related models |
|
| 3688 | - * @param $default_where_conditions_value |
|
| 3689 | - * @param bool $for_this_model false means this is for OTHER related models |
|
| 3690 | - * @return bool |
|
| 3691 | - */ |
|
| 3692 | - private function _should_use_default_where_conditions($default_where_conditions_value, $for_this_model = true) |
|
| 3693 | - { |
|
| 3694 | - return ( |
|
| 3695 | - $for_this_model |
|
| 3696 | - && in_array( |
|
| 3697 | - $default_where_conditions_value, |
|
| 3698 | - array( |
|
| 3699 | - EEM_Base::default_where_conditions_all, |
|
| 3700 | - EEM_Base::default_where_conditions_this_only, |
|
| 3701 | - EEM_Base::default_where_conditions_minimum_others, |
|
| 3702 | - ), |
|
| 3703 | - true |
|
| 3704 | - ) |
|
| 3705 | - ) |
|
| 3706 | - || ( |
|
| 3707 | - ! $for_this_model |
|
| 3708 | - && in_array( |
|
| 3709 | - $default_where_conditions_value, |
|
| 3710 | - array( |
|
| 3711 | - EEM_Base::default_where_conditions_all, |
|
| 3712 | - EEM_Base::default_where_conditions_others_only, |
|
| 3713 | - ), |
|
| 3714 | - true |
|
| 3715 | - ) |
|
| 3716 | - ); |
|
| 3717 | - } |
|
| 3718 | - |
|
| 3719 | - /** |
|
| 3720 | - * Determines whether or not we should use default minimum conditions for the model in question |
|
| 3721 | - * (this model, or other related models). |
|
| 3722 | - * Basically, we should use minimum where conditions on this model only if they requested all models to use minimum |
|
| 3723 | - * where conditions. |
|
| 3724 | - * We should use minimum where conditions on related models if they requested to use minimum where conditions |
|
| 3725 | - * on this model or others |
|
| 3726 | - * @param $default_where_conditions_value |
|
| 3727 | - * @param bool $for_this_model false means this is for OTHER related models |
|
| 3728 | - * @return bool |
|
| 3729 | - */ |
|
| 3730 | - private function _should_use_minimum_where_conditions($default_where_conditions_value, $for_this_model = true) |
|
| 3731 | - { |
|
| 3732 | - return ( |
|
| 3733 | - $for_this_model |
|
| 3734 | - && $default_where_conditions_value === EEM_Base::default_where_conditions_minimum_all |
|
| 3735 | - ) |
|
| 3736 | - || ( |
|
| 3737 | - ! $for_this_model |
|
| 3738 | - && in_array( |
|
| 3739 | - $default_where_conditions_value, |
|
| 3740 | - array( |
|
| 3741 | - EEM_Base::default_where_conditions_minimum_others, |
|
| 3742 | - EEM_Base::default_where_conditions_minimum_all, |
|
| 3743 | - ), |
|
| 3744 | - true |
|
| 3745 | - ) |
|
| 3746 | - ); |
|
| 3747 | - } |
|
| 3748 | - |
|
| 3749 | - |
|
| 3750 | - /** |
|
| 3751 | - * Checks if any of the defaults have been overridden. If there are any that AREN'T overridden, |
|
| 3752 | - * then we also add a special where condition which allows for that model's primary key |
|
| 3753 | - * to be null (which is important for JOINs. Eg, if you want to see all Events ordered by Venue's name, |
|
| 3754 | - * then Event's with NO Venue won't appear unless you allow VNU_ID to be NULL) |
|
| 3755 | - * |
|
| 3756 | - * @param array $default_where_conditions |
|
| 3757 | - * @param array $provided_where_conditions |
|
| 3758 | - * @param EEM_Base $model |
|
| 3759 | - * @param string $model_relation_path like 'Transaction.Payment.' |
|
| 3760 | - * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 3761 | - * @throws EE_Error |
|
| 3762 | - */ |
|
| 3763 | - private function _override_defaults_or_make_null_friendly( |
|
| 3764 | - $default_where_conditions, |
|
| 3765 | - $provided_where_conditions, |
|
| 3766 | - $model, |
|
| 3767 | - $model_relation_path |
|
| 3768 | - ) { |
|
| 3769 | - $null_friendly_where_conditions = array(); |
|
| 3770 | - $none_overridden = true; |
|
| 3771 | - $or_condition_key_for_defaults = 'OR*' . get_class($model); |
|
| 3772 | - foreach ($default_where_conditions as $key => $val) { |
|
| 3773 | - if (isset($provided_where_conditions[ $key ])) { |
|
| 3774 | - $none_overridden = false; |
|
| 3775 | - } else { |
|
| 3776 | - $null_friendly_where_conditions[ $or_condition_key_for_defaults ]['AND'][ $key ] = $val; |
|
| 3777 | - } |
|
| 3778 | - } |
|
| 3779 | - if ($none_overridden && $default_where_conditions) { |
|
| 3780 | - if ($model->has_primary_key_field()) { |
|
| 3781 | - $null_friendly_where_conditions[ $or_condition_key_for_defaults ][ $model_relation_path |
|
| 3782 | - . "." |
|
| 3783 | - . $model->primary_key_name() ] = array('IS NULL'); |
|
| 3784 | - }/*else{ |
|
| 292 | + /** |
|
| 293 | + * @var EE_Password_Field|null Automatically set when calling getPasswordField() |
|
| 294 | + */ |
|
| 295 | + protected $password_field; |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * List of valid operators that can be used for querying. |
|
| 299 | + * The keys are all operators we'll accept, the values are the real SQL |
|
| 300 | + * operators used |
|
| 301 | + * |
|
| 302 | + * @var array |
|
| 303 | + */ |
|
| 304 | + protected $_valid_operators = array( |
|
| 305 | + '=' => '=', |
|
| 306 | + '<=' => '<=', |
|
| 307 | + '<' => '<', |
|
| 308 | + '>=' => '>=', |
|
| 309 | + '>' => '>', |
|
| 310 | + '!=' => '!=', |
|
| 311 | + 'LIKE' => 'LIKE', |
|
| 312 | + 'like' => 'LIKE', |
|
| 313 | + 'NOT_LIKE' => 'NOT LIKE', |
|
| 314 | + 'not_like' => 'NOT LIKE', |
|
| 315 | + 'NOT LIKE' => 'NOT LIKE', |
|
| 316 | + 'not like' => 'NOT LIKE', |
|
| 317 | + 'IN' => 'IN', |
|
| 318 | + 'in' => 'IN', |
|
| 319 | + 'NOT_IN' => 'NOT IN', |
|
| 320 | + 'not_in' => 'NOT IN', |
|
| 321 | + 'NOT IN' => 'NOT IN', |
|
| 322 | + 'not in' => 'NOT IN', |
|
| 323 | + 'between' => 'BETWEEN', |
|
| 324 | + 'BETWEEN' => 'BETWEEN', |
|
| 325 | + 'IS_NOT_NULL' => 'IS NOT NULL', |
|
| 326 | + 'is_not_null' => 'IS NOT NULL', |
|
| 327 | + 'IS NOT NULL' => 'IS NOT NULL', |
|
| 328 | + 'is not null' => 'IS NOT NULL', |
|
| 329 | + 'IS_NULL' => 'IS NULL', |
|
| 330 | + 'is_null' => 'IS NULL', |
|
| 331 | + 'IS NULL' => 'IS NULL', |
|
| 332 | + 'is null' => 'IS NULL', |
|
| 333 | + 'REGEXP' => 'REGEXP', |
|
| 334 | + 'regexp' => 'REGEXP', |
|
| 335 | + 'NOT_REGEXP' => 'NOT REGEXP', |
|
| 336 | + 'not_regexp' => 'NOT REGEXP', |
|
| 337 | + 'NOT REGEXP' => 'NOT REGEXP', |
|
| 338 | + 'not regexp' => 'NOT REGEXP', |
|
| 339 | + ); |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * operators that work like 'IN', accepting a comma-separated list of values inside brackets. Eg '(1,2,3)' |
|
| 343 | + * |
|
| 344 | + * @var array |
|
| 345 | + */ |
|
| 346 | + protected $_in_style_operators = array('IN', 'NOT IN'); |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * operators that work like 'BETWEEN'. Typically used for datetime calculations, i.e. "BETWEEN '12-1-2011' AND |
|
| 350 | + * '12-31-2012'" |
|
| 351 | + * |
|
| 352 | + * @var array |
|
| 353 | + */ |
|
| 354 | + protected $_between_style_operators = array('BETWEEN'); |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Operators that work like SQL's like: input should be assumed to be a string, already prepared for a LIKE query. |
|
| 358 | + * @var array |
|
| 359 | + */ |
|
| 360 | + protected $_like_style_operators = array('LIKE', 'NOT LIKE'); |
|
| 361 | + /** |
|
| 362 | + * operators that are used for handling NUll and !NULL queries. Typically used for when checking if a row exists |
|
| 363 | + * on a join table. |
|
| 364 | + * |
|
| 365 | + * @var array |
|
| 366 | + */ |
|
| 367 | + protected $_null_style_operators = array('IS NOT NULL', 'IS NULL'); |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Allowed values for $query_params['order'] for ordering in queries |
|
| 371 | + * |
|
| 372 | + * @var array |
|
| 373 | + */ |
|
| 374 | + protected $_allowed_order_values = array('asc', 'desc', 'ASC', 'DESC'); |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * When these are keys in a WHERE or HAVING clause, they are handled much differently |
|
| 378 | + * than regular field names. It is assumed that their values are an array of WHERE conditions |
|
| 379 | + * |
|
| 380 | + * @var array |
|
| 381 | + */ |
|
| 382 | + private $_logic_query_param_keys = array('not', 'and', 'or', 'NOT', 'AND', 'OR'); |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * Allowed keys in $query_params arrays passed into queries. Note that 0 is meant to always be a |
|
| 386 | + * 'where', but 'where' clauses are so common that we thought we'd omit it |
|
| 387 | + * |
|
| 388 | + * @var array |
|
| 389 | + */ |
|
| 390 | + private $_allowed_query_params = array( |
|
| 391 | + 0, |
|
| 392 | + 'limit', |
|
| 393 | + 'order_by', |
|
| 394 | + 'group_by', |
|
| 395 | + 'having', |
|
| 396 | + 'force_join', |
|
| 397 | + 'order', |
|
| 398 | + 'on_join_limit', |
|
| 399 | + 'default_where_conditions', |
|
| 400 | + 'caps', |
|
| 401 | + 'extra_selects', |
|
| 402 | + 'exclude_protected', |
|
| 403 | + ); |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * All the data types that can be used in $wpdb->prepare statements. |
|
| 407 | + * |
|
| 408 | + * @var array |
|
| 409 | + */ |
|
| 410 | + private $_valid_wpdb_data_types = array('%d', '%s', '%f'); |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * @var EE_Registry $EE |
|
| 414 | + */ |
|
| 415 | + protected $EE = null; |
|
| 416 | + |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * Property which, when set, will have this model echo out the next X queries to the page for debugging. |
|
| 420 | + * |
|
| 421 | + * @var int |
|
| 422 | + */ |
|
| 423 | + protected $_show_next_x_db_queries = 0; |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * When using _get_all_wpdb_results, you can specify a custom selection. If you do so, |
|
| 427 | + * it gets saved on this property as an instance of CustomSelects so those selections can be used in |
|
| 428 | + * WHERE, GROUP_BY, etc. |
|
| 429 | + * |
|
| 430 | + * @var CustomSelects |
|
| 431 | + */ |
|
| 432 | + protected $_custom_selections = array(); |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * key => value Entity Map using array( EEM_Base::$_model_query_blog_id => array( ID => model object ) ) |
|
| 436 | + * caches every model object we've fetched from the DB on this request |
|
| 437 | + * |
|
| 438 | + * @var array |
|
| 439 | + */ |
|
| 440 | + protected $_entity_map; |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * @var LoaderInterface $loader |
|
| 444 | + */ |
|
| 445 | + private static $loader; |
|
| 446 | + |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * constant used to show EEM_Base has not yet verified the db on this http request |
|
| 450 | + */ |
|
| 451 | + const db_verified_none = 0; |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * constant used to show EEM_Base has verified the EE core db on this http request, |
|
| 455 | + * but not the addons' dbs |
|
| 456 | + */ |
|
| 457 | + const db_verified_core = 1; |
|
| 458 | + |
|
| 459 | + /** |
|
| 460 | + * constant used to show EEM_Base has verified the addons' dbs (and implicitly |
|
| 461 | + * the EE core db too) |
|
| 462 | + */ |
|
| 463 | + const db_verified_addons = 2; |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * indicates whether an EEM_Base child has already re-verified the DB |
|
| 467 | + * is ok (we don't want to do it repetitively). Should be set to one the constants |
|
| 468 | + * looking like EEM_Base::db_verified_* |
|
| 469 | + * |
|
| 470 | + * @var int - 0 = none, 1 = core, 2 = addons |
|
| 471 | + */ |
|
| 472 | + protected static $_db_verification_level = EEM_Base::db_verified_none; |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * @const constant for 'default_where_conditions' to apply default where conditions to ALL queried models |
|
| 476 | + * (eg, if retrieving registrations ordered by their datetimes, this will only return non-trashed |
|
| 477 | + * registrations for non-trashed tickets for non-trashed datetimes) |
|
| 478 | + */ |
|
| 479 | + const default_where_conditions_all = 'all'; |
|
| 480 | + |
|
| 481 | + /** |
|
| 482 | + * @const constant for 'default_where_conditions' to apply default where conditions to THIS model only, but |
|
| 483 | + * no other models which are joined to (eg, if retrieving registrations ordered by their datetimes, this will |
|
| 484 | + * return non-trashed registrations, regardless of the related datetimes and tickets' statuses). |
|
| 485 | + * It is preferred to use EEM_Base::default_where_conditions_minimum_others because, when joining to |
|
| 486 | + * models which share tables with other models, this can return data for the wrong model. |
|
| 487 | + */ |
|
| 488 | + const default_where_conditions_this_only = 'this_model_only'; |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * @const constant for 'default_where_conditions' to apply default where conditions to other models queried, |
|
| 492 | + * but not the current model (eg, if retrieving registrations ordered by their datetimes, this will |
|
| 493 | + * return all registrations related to non-trashed tickets and non-trashed datetimes) |
|
| 494 | + */ |
|
| 495 | + const default_where_conditions_others_only = 'other_models_only'; |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * @const constant for 'default_where_conditions' to apply minimum where conditions to all models queried. |
|
| 499 | + * For most models this the same as EEM_Base::default_where_conditions_none, except for models which share |
|
| 500 | + * their table with other models, like the Event and Venue models. For example, when querying for events |
|
| 501 | + * ordered by their venues' name, this will be sure to only return real events with associated real venues |
|
| 502 | + * (regardless of whether those events and venues are trashed) |
|
| 503 | + * In contrast, using EEM_Base::default_where_conditions_none would could return WP posts other than EE |
|
| 504 | + * events. |
|
| 505 | + */ |
|
| 506 | + const default_where_conditions_minimum_all = 'minimum'; |
|
| 507 | + |
|
| 508 | + /** |
|
| 509 | + * @const constant for 'default_where_conditions' to apply apply where conditions to other models, and full default |
|
| 510 | + * where conditions for the queried model (eg, when querying events ordered by venues' names, this will |
|
| 511 | + * return non-trashed events for any venues, regardless of whether those associated venues are trashed or |
|
| 512 | + * not) |
|
| 513 | + */ |
|
| 514 | + const default_where_conditions_minimum_others = 'full_this_minimum_others'; |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * @const constant for 'default_where_conditions' to NOT apply any where conditions. This should very rarely be |
|
| 518 | + * used, because when querying from a model which shares its table with another model (eg Events and Venues) |
|
| 519 | + * it's possible it will return table entries for other models. You should use |
|
| 520 | + * EEM_Base::default_where_conditions_minimum_all instead. |
|
| 521 | + */ |
|
| 522 | + const default_where_conditions_none = 'none'; |
|
| 523 | + |
|
| 524 | + |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * About all child constructors: |
|
| 528 | + * they should define the _tables, _fields and _model_relations arrays. |
|
| 529 | + * Should ALWAYS be called after child constructor. |
|
| 530 | + * In order to make the child constructors to be as simple as possible, this parent constructor |
|
| 531 | + * finalizes constructing all the object's attributes. |
|
| 532 | + * Generally, rather than requiring a child to code |
|
| 533 | + * $this->_tables = array( |
|
| 534 | + * 'Event_Post_Table' => new EE_Table('Event_Post_Table','wp_posts') |
|
| 535 | + * ...); |
|
| 536 | + * (thus repeating itself in the array key and in the constructor of the new EE_Table,) |
|
| 537 | + * each EE_Table has a function to set the table's alias after the constructor, using |
|
| 538 | + * the array key ('Event_Post_Table'), instead of repeating it. The model fields and model relations |
|
| 539 | + * do something similar. |
|
| 540 | + * |
|
| 541 | + * @param null $timezone |
|
| 542 | + * @throws EE_Error |
|
| 543 | + */ |
|
| 544 | + protected function __construct($timezone = null) |
|
| 545 | + { |
|
| 546 | + // check that the model has not been loaded too soon |
|
| 547 | + if (! did_action('AHEE__EE_System__load_espresso_addons')) { |
|
| 548 | + throw new EE_Error( |
|
| 549 | + sprintf( |
|
| 550 | + __( |
|
| 551 | + 'The %1$s model can not be loaded before the "AHEE__EE_System__load_espresso_addons" hook has been called. This gives other addons a chance to extend this model.', |
|
| 552 | + 'event_espresso' |
|
| 553 | + ), |
|
| 554 | + get_class($this) |
|
| 555 | + ) |
|
| 556 | + ); |
|
| 557 | + } |
|
| 558 | + /** |
|
| 559 | + * Set blogid for models to current blog. However we ONLY do this if $_model_query_blog_id is not already set. |
|
| 560 | + */ |
|
| 561 | + if (empty(EEM_Base::$_model_query_blog_id)) { |
|
| 562 | + EEM_Base::set_model_query_blog_id(); |
|
| 563 | + } |
|
| 564 | + /** |
|
| 565 | + * Filters the list of tables on a model. It is best to NOT use this directly and instead |
|
| 566 | + * just use EE_Register_Model_Extension |
|
| 567 | + * |
|
| 568 | + * @var EE_Table_Base[] $_tables |
|
| 569 | + */ |
|
| 570 | + $this->_tables = (array) apply_filters('FHEE__' . get_class($this) . '__construct__tables', $this->_tables); |
|
| 571 | + foreach ($this->_tables as $table_alias => $table_obj) { |
|
| 572 | + /** @var $table_obj EE_Table_Base */ |
|
| 573 | + $table_obj->_construct_finalize_with_alias($table_alias); |
|
| 574 | + if ($table_obj instanceof EE_Secondary_Table) { |
|
| 575 | + /** @var $table_obj EE_Secondary_Table */ |
|
| 576 | + $table_obj->_construct_finalize_set_table_to_join_with($this->_get_main_table()); |
|
| 577 | + } |
|
| 578 | + } |
|
| 579 | + /** |
|
| 580 | + * Filters the list of fields on a model. It is best to NOT use this directly and instead just use |
|
| 581 | + * EE_Register_Model_Extension |
|
| 582 | + * |
|
| 583 | + * @param EE_Model_Field_Base[] $_fields |
|
| 584 | + */ |
|
| 585 | + $this->_fields = (array) apply_filters('FHEE__' . get_class($this) . '__construct__fields', $this->_fields); |
|
| 586 | + $this->_invalidate_field_caches(); |
|
| 587 | + foreach ($this->_fields as $table_alias => $fields_for_table) { |
|
| 588 | + if (! array_key_exists($table_alias, $this->_tables)) { |
|
| 589 | + throw new EE_Error(sprintf(__( |
|
| 590 | + "Table alias %s does not exist in EEM_Base child's _tables array. Only tables defined are %s", |
|
| 591 | + 'event_espresso' |
|
| 592 | + ), $table_alias, implode(",", $this->_fields))); |
|
| 593 | + } |
|
| 594 | + foreach ($fields_for_table as $field_name => $field_obj) { |
|
| 595 | + /** @var $field_obj EE_Model_Field_Base | EE_Primary_Key_Field_Base */ |
|
| 596 | + // primary key field base has a slightly different _construct_finalize |
|
| 597 | + /** @var $field_obj EE_Model_Field_Base */ |
|
| 598 | + $field_obj->_construct_finalize($table_alias, $field_name, $this->get_this_model_name()); |
|
| 599 | + } |
|
| 600 | + } |
|
| 601 | + // everything is related to Extra_Meta |
|
| 602 | + if (get_class($this) !== 'EEM_Extra_Meta') { |
|
| 603 | + // make extra meta related to everything, but don't block deleting things just |
|
| 604 | + // because they have related extra meta info. For now just orphan those extra meta |
|
| 605 | + // in the future we should automatically delete them |
|
| 606 | + $this->_model_relations['Extra_Meta'] = new EE_Has_Many_Any_Relation(false); |
|
| 607 | + } |
|
| 608 | + // and change logs |
|
| 609 | + if (get_class($this) !== 'EEM_Change_Log') { |
|
| 610 | + $this->_model_relations['Change_Log'] = new EE_Has_Many_Any_Relation(false); |
|
| 611 | + } |
|
| 612 | + /** |
|
| 613 | + * Filters the list of relations on a model. It is best to NOT use this directly and instead just use |
|
| 614 | + * EE_Register_Model_Extension |
|
| 615 | + * |
|
| 616 | + * @param EE_Model_Relation_Base[] $_model_relations |
|
| 617 | + */ |
|
| 618 | + $this->_model_relations = (array) apply_filters( |
|
| 619 | + 'FHEE__' . get_class($this) . '__construct__model_relations', |
|
| 620 | + $this->_model_relations |
|
| 621 | + ); |
|
| 622 | + foreach ($this->_model_relations as $model_name => $relation_obj) { |
|
| 623 | + /** @var $relation_obj EE_Model_Relation_Base */ |
|
| 624 | + $relation_obj->_construct_finalize_set_models($this->get_this_model_name(), $model_name); |
|
| 625 | + } |
|
| 626 | + foreach ($this->_indexes as $index_name => $index_obj) { |
|
| 627 | + /** @var $index_obj EE_Index */ |
|
| 628 | + $index_obj->_construct_finalize($index_name, $this->get_this_model_name()); |
|
| 629 | + } |
|
| 630 | + $this->set_timezone($timezone); |
|
| 631 | + // finalize default where condition strategy, or set default |
|
| 632 | + if (! $this->_default_where_conditions_strategy) { |
|
| 633 | + // nothing was set during child constructor, so set default |
|
| 634 | + $this->_default_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
| 635 | + } |
|
| 636 | + $this->_default_where_conditions_strategy->_finalize_construct($this); |
|
| 637 | + if (! $this->_minimum_where_conditions_strategy) { |
|
| 638 | + // nothing was set during child constructor, so set default |
|
| 639 | + $this->_minimum_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
| 640 | + } |
|
| 641 | + $this->_minimum_where_conditions_strategy->_finalize_construct($this); |
|
| 642 | + // if the cap slug hasn't been set, and we haven't set it to false on purpose |
|
| 643 | + // to indicate to NOT set it, set it to the logical default |
|
| 644 | + if ($this->_caps_slug === null) { |
|
| 645 | + $this->_caps_slug = EEH_Inflector::pluralize_and_lower($this->get_this_model_name()); |
|
| 646 | + } |
|
| 647 | + // initialize the standard cap restriction generators if none were specified by the child constructor |
|
| 648 | + if ($this->_cap_restriction_generators !== false) { |
|
| 649 | + foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
|
| 650 | + if (! isset($this->_cap_restriction_generators[ $cap_context ])) { |
|
| 651 | + $this->_cap_restriction_generators[ $cap_context ] = apply_filters( |
|
| 652 | + 'FHEE__EEM_Base___construct__standard_cap_restriction_generator', |
|
| 653 | + new EE_Restriction_Generator_Protected(), |
|
| 654 | + $cap_context, |
|
| 655 | + $this |
|
| 656 | + ); |
|
| 657 | + } |
|
| 658 | + } |
|
| 659 | + } |
|
| 660 | + // if there are cap restriction generators, use them to make the default cap restrictions |
|
| 661 | + if ($this->_cap_restriction_generators !== false) { |
|
| 662 | + foreach ($this->_cap_restriction_generators as $context => $generator_object) { |
|
| 663 | + if (! $generator_object) { |
|
| 664 | + continue; |
|
| 665 | + } |
|
| 666 | + if (! $generator_object instanceof EE_Restriction_Generator_Base) { |
|
| 667 | + throw new EE_Error( |
|
| 668 | + sprintf( |
|
| 669 | + __( |
|
| 670 | + 'Index "%1$s" in the model %2$s\'s _cap_restriction_generators is not a child of EE_Restriction_Generator_Base. It should be that or NULL.', |
|
| 671 | + 'event_espresso' |
|
| 672 | + ), |
|
| 673 | + $context, |
|
| 674 | + $this->get_this_model_name() |
|
| 675 | + ) |
|
| 676 | + ); |
|
| 677 | + } |
|
| 678 | + $action = $this->cap_action_for_context($context); |
|
| 679 | + if (! $generator_object->construction_finalized()) { |
|
| 680 | + $generator_object->_construct_finalize($this, $action); |
|
| 681 | + } |
|
| 682 | + } |
|
| 683 | + } |
|
| 684 | + do_action('AHEE__' . get_class($this) . '__construct__end'); |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + |
|
| 688 | + |
|
| 689 | + /** |
|
| 690 | + * Used to set the $_model_query_blog_id static property. |
|
| 691 | + * |
|
| 692 | + * @param int $blog_id If provided then will set the blog_id for the models to this id. If not provided then the |
|
| 693 | + * value for get_current_blog_id() will be used. |
|
| 694 | + */ |
|
| 695 | + public static function set_model_query_blog_id($blog_id = 0) |
|
| 696 | + { |
|
| 697 | + EEM_Base::$_model_query_blog_id = $blog_id > 0 ? (int) $blog_id : get_current_blog_id(); |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + |
|
| 701 | + |
|
| 702 | + /** |
|
| 703 | + * Returns whatever is set as the internal $model_query_blog_id. |
|
| 704 | + * |
|
| 705 | + * @return int |
|
| 706 | + */ |
|
| 707 | + public static function get_model_query_blog_id() |
|
| 708 | + { |
|
| 709 | + return EEM_Base::$_model_query_blog_id; |
|
| 710 | + } |
|
| 711 | + |
|
| 712 | + |
|
| 713 | + |
|
| 714 | + /** |
|
| 715 | + * This function is a singleton method used to instantiate the Espresso_model object |
|
| 716 | + * |
|
| 717 | + * @param string $timezone string representing the timezone we want to set for returned Date Time Strings |
|
| 718 | + * (and any incoming timezone data that gets saved). |
|
| 719 | + * Note this just sends the timezone info to the date time model field objects. |
|
| 720 | + * Default is NULL |
|
| 721 | + * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
|
| 722 | + * @return static (as in the concrete child class) |
|
| 723 | + * @throws EE_Error |
|
| 724 | + * @throws InvalidArgumentException |
|
| 725 | + * @throws InvalidDataTypeException |
|
| 726 | + * @throws InvalidInterfaceException |
|
| 727 | + */ |
|
| 728 | + public static function instance($timezone = null) |
|
| 729 | + { |
|
| 730 | + // check if instance of Espresso_model already exists |
|
| 731 | + if (! static::$_instance instanceof static) { |
|
| 732 | + // instantiate Espresso_model |
|
| 733 | + static::$_instance = new static( |
|
| 734 | + $timezone, |
|
| 735 | + LoaderFactory::getLoader()->load('EventEspresso\core\services\orm\ModelFieldFactory') |
|
| 736 | + ); |
|
| 737 | + } |
|
| 738 | + // we might have a timezone set, let set_timezone decide what to do with it |
|
| 739 | + static::$_instance->set_timezone($timezone); |
|
| 740 | + // Espresso_model object |
|
| 741 | + return static::$_instance; |
|
| 742 | + } |
|
| 743 | + |
|
| 744 | + |
|
| 745 | + |
|
| 746 | + /** |
|
| 747 | + * resets the model and returns it |
|
| 748 | + * |
|
| 749 | + * @param null | string $timezone |
|
| 750 | + * @return EEM_Base|null (if the model was already instantiated, returns it, with |
|
| 751 | + * all its properties reset; if it wasn't instantiated, returns null) |
|
| 752 | + * @throws EE_Error |
|
| 753 | + * @throws ReflectionException |
|
| 754 | + * @throws InvalidArgumentException |
|
| 755 | + * @throws InvalidDataTypeException |
|
| 756 | + * @throws InvalidInterfaceException |
|
| 757 | + */ |
|
| 758 | + public static function reset($timezone = null) |
|
| 759 | + { |
|
| 760 | + if (static::$_instance instanceof EEM_Base) { |
|
| 761 | + // let's try to NOT swap out the current instance for a new one |
|
| 762 | + // because if someone has a reference to it, we can't remove their reference |
|
| 763 | + // so it's best to keep using the same reference, but change the original object |
|
| 764 | + // reset all its properties to their original values as defined in the class |
|
| 765 | + $r = new ReflectionClass(get_class(static::$_instance)); |
|
| 766 | + $static_properties = $r->getStaticProperties(); |
|
| 767 | + foreach ($r->getDefaultProperties() as $property => $value) { |
|
| 768 | + // don't set instance to null like it was originally, |
|
| 769 | + // but it's static anyways, and we're ignoring static properties (for now at least) |
|
| 770 | + if (! isset($static_properties[ $property ])) { |
|
| 771 | + static::$_instance->{$property} = $value; |
|
| 772 | + } |
|
| 773 | + } |
|
| 774 | + // and then directly call its constructor again, like we would if we were creating a new one |
|
| 775 | + static::$_instance->__construct( |
|
| 776 | + $timezone, |
|
| 777 | + LoaderFactory::getLoader()->load('EventEspresso\core\services\orm\ModelFieldFactory') |
|
| 778 | + ); |
|
| 779 | + return self::instance(); |
|
| 780 | + } |
|
| 781 | + return null; |
|
| 782 | + } |
|
| 783 | + |
|
| 784 | + |
|
| 785 | + |
|
| 786 | + /** |
|
| 787 | + * @return LoaderInterface |
|
| 788 | + * @throws InvalidArgumentException |
|
| 789 | + * @throws InvalidDataTypeException |
|
| 790 | + * @throws InvalidInterfaceException |
|
| 791 | + */ |
|
| 792 | + private static function getLoader() |
|
| 793 | + { |
|
| 794 | + if (! EEM_Base::$loader instanceof LoaderInterface) { |
|
| 795 | + EEM_Base::$loader = LoaderFactory::getLoader(); |
|
| 796 | + } |
|
| 797 | + return EEM_Base::$loader; |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + |
|
| 801 | + |
|
| 802 | + /** |
|
| 803 | + * retrieve the status details from esp_status table as an array IF this model has the status table as a relation. |
|
| 804 | + * |
|
| 805 | + * @param boolean $translated return localized strings or JUST the array. |
|
| 806 | + * @return array |
|
| 807 | + * @throws EE_Error |
|
| 808 | + * @throws InvalidArgumentException |
|
| 809 | + * @throws InvalidDataTypeException |
|
| 810 | + * @throws InvalidInterfaceException |
|
| 811 | + */ |
|
| 812 | + public function status_array($translated = false) |
|
| 813 | + { |
|
| 814 | + if (! array_key_exists('Status', $this->_model_relations)) { |
|
| 815 | + return array(); |
|
| 816 | + } |
|
| 817 | + $model_name = $this->get_this_model_name(); |
|
| 818 | + $status_type = str_replace(' ', '_', strtolower(str_replace('_', ' ', $model_name))); |
|
| 819 | + $stati = EEM_Status::instance()->get_all(array(array('STS_type' => $status_type))); |
|
| 820 | + $status_array = array(); |
|
| 821 | + foreach ($stati as $status) { |
|
| 822 | + $status_array[ $status->ID() ] = $status->get('STS_code'); |
|
| 823 | + } |
|
| 824 | + return $translated |
|
| 825 | + ? EEM_Status::instance()->localized_status($status_array, false, 'sentence') |
|
| 826 | + : $status_array; |
|
| 827 | + } |
|
| 828 | + |
|
| 829 | + |
|
| 830 | + |
|
| 831 | + /** |
|
| 832 | + * Gets all the EE_Base_Class objects which match the $query_params, by querying the DB. |
|
| 833 | + * |
|
| 834 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 835 | + * or if you have the development copy of EE you can view this at the path: |
|
| 836 | + * /docs/G--Model-System/model-query-params.md |
|
| 837 | + * @return EE_Base_Class[] *note that there is NO option to pass the output type. If you want results different |
|
| 838 | + * from EE_Base_Class[], use get_all_wpdb_results(). Array keys are object IDs (if there is a primary key on the model. |
|
| 839 | + * if not, numerically indexed) Some full examples: get 10 transactions |
|
| 840 | + * which have Scottish attendees: EEM_Transaction::instance()->get_all( |
|
| 841 | + * array( array( |
|
| 842 | + * 'OR'=>array( |
|
| 843 | + * 'Registration.Attendee.ATT_fname'=>array('like','Mc%'), |
|
| 844 | + * 'Registration.Attendee.ATT_fname*other'=>array('like','Mac%') |
|
| 845 | + * ) |
|
| 846 | + * ), |
|
| 847 | + * 'limit'=>10, |
|
| 848 | + * 'group_by'=>'TXN_ID' |
|
| 849 | + * )); |
|
| 850 | + * get all the answers to the question titled "shirt size" for event with id |
|
| 851 | + * 12, ordered by their answer EEM_Answer::instance()->get_all(array( array( |
|
| 852 | + * 'Question.QST_display_text'=>'shirt size', |
|
| 853 | + * 'Registration.Event.EVT_ID'=>12 |
|
| 854 | + * ), |
|
| 855 | + * 'order_by'=>array('ANS_value'=>'ASC') |
|
| 856 | + * )); |
|
| 857 | + * @throws EE_Error |
|
| 858 | + */ |
|
| 859 | + public function get_all($query_params = array()) |
|
| 860 | + { |
|
| 861 | + if (isset($query_params['limit']) |
|
| 862 | + && ! isset($query_params['group_by']) |
|
| 863 | + ) { |
|
| 864 | + $query_params['group_by'] = array_keys($this->get_combined_primary_key_fields()); |
|
| 865 | + } |
|
| 866 | + return $this->_create_objects($this->_get_all_wpdb_results($query_params, ARRAY_A, null)); |
|
| 867 | + } |
|
| 868 | + |
|
| 869 | + |
|
| 870 | + |
|
| 871 | + /** |
|
| 872 | + * Modifies the query parameters so we only get back model objects |
|
| 873 | + * that "belong" to the current user |
|
| 874 | + * |
|
| 875 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 876 | + * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 877 | + */ |
|
| 878 | + public function alter_query_params_to_only_include_mine($query_params = array()) |
|
| 879 | + { |
|
| 880 | + $wp_user_field_name = $this->wp_user_field_name(); |
|
| 881 | + if ($wp_user_field_name) { |
|
| 882 | + $query_params[0][ $wp_user_field_name ] = get_current_user_id(); |
|
| 883 | + } |
|
| 884 | + return $query_params; |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + |
|
| 888 | + |
|
| 889 | + /** |
|
| 890 | + * Returns the name of the field's name that points to the WP_User table |
|
| 891 | + * on this model (or follows the _model_chain_to_wp_user and uses that model's |
|
| 892 | + * foreign key to the WP_User table) |
|
| 893 | + * |
|
| 894 | + * @return string|boolean string on success, boolean false when there is no |
|
| 895 | + * foreign key to the WP_User table |
|
| 896 | + */ |
|
| 897 | + public function wp_user_field_name() |
|
| 898 | + { |
|
| 899 | + try { |
|
| 900 | + if (! empty($this->_model_chain_to_wp_user)) { |
|
| 901 | + $models_to_follow_to_wp_users = explode('.', $this->_model_chain_to_wp_user); |
|
| 902 | + $last_model_name = end($models_to_follow_to_wp_users); |
|
| 903 | + $model_with_fk_to_wp_users = EE_Registry::instance()->load_model($last_model_name); |
|
| 904 | + $model_chain_to_wp_user = $this->_model_chain_to_wp_user . '.'; |
|
| 905 | + } else { |
|
| 906 | + $model_with_fk_to_wp_users = $this; |
|
| 907 | + $model_chain_to_wp_user = ''; |
|
| 908 | + } |
|
| 909 | + $wp_user_field = $model_with_fk_to_wp_users->get_foreign_key_to('WP_User'); |
|
| 910 | + return $model_chain_to_wp_user . $wp_user_field->get_name(); |
|
| 911 | + } catch (EE_Error $e) { |
|
| 912 | + return false; |
|
| 913 | + } |
|
| 914 | + } |
|
| 915 | + |
|
| 916 | + |
|
| 917 | + |
|
| 918 | + /** |
|
| 919 | + * Returns the _model_chain_to_wp_user string, which indicates which related model |
|
| 920 | + * (or transiently-related model) has a foreign key to the wp_users table; |
|
| 921 | + * useful for finding if model objects of this type are 'owned' by the current user. |
|
| 922 | + * This is an empty string when the foreign key is on this model and when it isn't, |
|
| 923 | + * but is only non-empty when this model's ownership is indicated by a RELATED model |
|
| 924 | + * (or transiently-related model) |
|
| 925 | + * |
|
| 926 | + * @return string |
|
| 927 | + */ |
|
| 928 | + public function model_chain_to_wp_user() |
|
| 929 | + { |
|
| 930 | + return $this->_model_chain_to_wp_user; |
|
| 931 | + } |
|
| 932 | + |
|
| 933 | + |
|
| 934 | + |
|
| 935 | + /** |
|
| 936 | + * Whether this model is 'owned' by a specific wordpress user (even indirectly, |
|
| 937 | + * like how registrations don't have a foreign key to wp_users, but the |
|
| 938 | + * events they are for are), or is unrelated to wp users. |
|
| 939 | + * generally available |
|
| 940 | + * |
|
| 941 | + * @return boolean |
|
| 942 | + */ |
|
| 943 | + public function is_owned() |
|
| 944 | + { |
|
| 945 | + if ($this->model_chain_to_wp_user()) { |
|
| 946 | + return true; |
|
| 947 | + } |
|
| 948 | + try { |
|
| 949 | + $this->get_foreign_key_to('WP_User'); |
|
| 950 | + return true; |
|
| 951 | + } catch (EE_Error $e) { |
|
| 952 | + return false; |
|
| 953 | + } |
|
| 954 | + } |
|
| 955 | + |
|
| 956 | + |
|
| 957 | + /** |
|
| 958 | + * Used internally to get WPDB results, because other functions, besides get_all, may want to do some queries, but |
|
| 959 | + * may want to preserve the WPDB results (eg, update, which first queries to make sure we have all the tables on |
|
| 960 | + * the model) |
|
| 961 | + * |
|
| 962 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 963 | + * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
| 964 | + * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
| 965 | + * fields on the model, and the models we joined to in the query. However, you can |
|
| 966 | + * override this and set the select to "*", or a specific column name, like |
|
| 967 | + * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
| 968 | + * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
| 969 | + * the aliases used to refer to this selection, and values are to be |
|
| 970 | + * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
| 971 | + * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
| 972 | + * @return array | stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
| 973 | + * @throws EE_Error |
|
| 974 | + * @throws InvalidArgumentException |
|
| 975 | + */ |
|
| 976 | + protected function _get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
|
| 977 | + { |
|
| 978 | + $this->_custom_selections = $this->getCustomSelection($query_params, $columns_to_select); |
|
| 979 | + ; |
|
| 980 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 981 | + $select_expressions = $columns_to_select === null |
|
| 982 | + ? $this->_construct_default_select_sql($model_query_info) |
|
| 983 | + : ''; |
|
| 984 | + if ($this->_custom_selections instanceof CustomSelects) { |
|
| 985 | + $custom_expressions = $this->_custom_selections->columnsToSelectExpression(); |
|
| 986 | + $select_expressions .= $select_expressions |
|
| 987 | + ? ', ' . $custom_expressions |
|
| 988 | + : $custom_expressions; |
|
| 989 | + } |
|
| 990 | + |
|
| 991 | + $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 992 | + return $this->_do_wpdb_query('get_results', array($SQL, $output)); |
|
| 993 | + } |
|
| 994 | + |
|
| 995 | + |
|
| 996 | + /** |
|
| 997 | + * Get a CustomSelects object if the $query_params or $columns_to_select allows for it. |
|
| 998 | + * Note: $query_params['extra_selects'] will always override any $columns_to_select values. It is the preferred |
|
| 999 | + * method of including extra select information. |
|
| 1000 | + * |
|
| 1001 | + * @param array $query_params |
|
| 1002 | + * @param null|array|string $columns_to_select |
|
| 1003 | + * @return null|CustomSelects |
|
| 1004 | + * @throws InvalidArgumentException |
|
| 1005 | + */ |
|
| 1006 | + protected function getCustomSelection(array $query_params, $columns_to_select = null) |
|
| 1007 | + { |
|
| 1008 | + if (! isset($query_params['extra_selects']) && $columns_to_select === null) { |
|
| 1009 | + return null; |
|
| 1010 | + } |
|
| 1011 | + $selects = isset($query_params['extra_selects']) ? $query_params['extra_selects'] : $columns_to_select; |
|
| 1012 | + $selects = is_string($selects) ? explode(',', $selects) : $selects; |
|
| 1013 | + return new CustomSelects($selects); |
|
| 1014 | + } |
|
| 1015 | + |
|
| 1016 | + |
|
| 1017 | + |
|
| 1018 | + /** |
|
| 1019 | + * Gets an array of rows from the database just like $wpdb->get_results would, |
|
| 1020 | + * but you can use the model query params to more easily |
|
| 1021 | + * take care of joins, field preparation etc. |
|
| 1022 | + * |
|
| 1023 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1024 | + * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
| 1025 | + * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
| 1026 | + * fields on the model, and the models we joined to in the query. However, you can |
|
| 1027 | + * override this and set the select to "*", or a specific column name, like |
|
| 1028 | + * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
| 1029 | + * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
| 1030 | + * the aliases used to refer to this selection, and values are to be |
|
| 1031 | + * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
| 1032 | + * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
| 1033 | + * @return array|stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
| 1034 | + * @throws EE_Error |
|
| 1035 | + */ |
|
| 1036 | + public function get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
|
| 1037 | + { |
|
| 1038 | + return $this->_get_all_wpdb_results($query_params, $output, $columns_to_select); |
|
| 1039 | + } |
|
| 1040 | + |
|
| 1041 | + |
|
| 1042 | + |
|
| 1043 | + /** |
|
| 1044 | + * For creating a custom select statement |
|
| 1045 | + * |
|
| 1046 | + * @param mixed $columns_to_select either a string to be inserted directly as the select statement, |
|
| 1047 | + * or an array where keys are aliases, and values are arrays where 0=>the selection |
|
| 1048 | + * SQL, and 1=>is the datatype |
|
| 1049 | + * @throws EE_Error |
|
| 1050 | + * @return string |
|
| 1051 | + */ |
|
| 1052 | + private function _construct_select_from_input($columns_to_select) |
|
| 1053 | + { |
|
| 1054 | + if (is_array($columns_to_select)) { |
|
| 1055 | + $select_sql_array = array(); |
|
| 1056 | + foreach ($columns_to_select as $alias => $selection_and_datatype) { |
|
| 1057 | + if (! is_array($selection_and_datatype) || ! isset($selection_and_datatype[1])) { |
|
| 1058 | + throw new EE_Error( |
|
| 1059 | + sprintf( |
|
| 1060 | + __( |
|
| 1061 | + "Custom selection %s (alias %s) needs to be an array like array('COUNT(REG_ID)','%%d')", |
|
| 1062 | + 'event_espresso' |
|
| 1063 | + ), |
|
| 1064 | + $selection_and_datatype, |
|
| 1065 | + $alias |
|
| 1066 | + ) |
|
| 1067 | + ); |
|
| 1068 | + } |
|
| 1069 | + if (! in_array($selection_and_datatype[1], $this->_valid_wpdb_data_types, true)) { |
|
| 1070 | + throw new EE_Error( |
|
| 1071 | + sprintf( |
|
| 1072 | + esc_html__( |
|
| 1073 | + "Datatype %s (for selection '%s' and alias '%s') is not a valid wpdb datatype (eg %%s)", |
|
| 1074 | + 'event_espresso' |
|
| 1075 | + ), |
|
| 1076 | + $selection_and_datatype[1], |
|
| 1077 | + $selection_and_datatype[0], |
|
| 1078 | + $alias, |
|
| 1079 | + implode(', ', $this->_valid_wpdb_data_types) |
|
| 1080 | + ) |
|
| 1081 | + ); |
|
| 1082 | + } |
|
| 1083 | + $select_sql_array[] = "{$selection_and_datatype[0]} AS $alias"; |
|
| 1084 | + } |
|
| 1085 | + $columns_to_select_string = implode(', ', $select_sql_array); |
|
| 1086 | + } else { |
|
| 1087 | + $columns_to_select_string = $columns_to_select; |
|
| 1088 | + } |
|
| 1089 | + return $columns_to_select_string; |
|
| 1090 | + } |
|
| 1091 | + |
|
| 1092 | + |
|
| 1093 | + |
|
| 1094 | + /** |
|
| 1095 | + * Convenient wrapper for getting the primary key field's name. Eg, on Registration, this would be 'REG_ID' |
|
| 1096 | + * |
|
| 1097 | + * @return string |
|
| 1098 | + * @throws EE_Error |
|
| 1099 | + */ |
|
| 1100 | + public function primary_key_name() |
|
| 1101 | + { |
|
| 1102 | + return $this->get_primary_key_field()->get_name(); |
|
| 1103 | + } |
|
| 1104 | + |
|
| 1105 | + |
|
| 1106 | + |
|
| 1107 | + /** |
|
| 1108 | + * Gets a single item for this model from the DB, given only its ID (or null if none is found). |
|
| 1109 | + * If there is no primary key on this model, $id is treated as primary key string |
|
| 1110 | + * |
|
| 1111 | + * @param mixed $id int or string, depending on the type of the model's primary key |
|
| 1112 | + * @return EE_Base_Class |
|
| 1113 | + */ |
|
| 1114 | + public function get_one_by_ID($id) |
|
| 1115 | + { |
|
| 1116 | + if ($this->get_from_entity_map($id)) { |
|
| 1117 | + return $this->get_from_entity_map($id); |
|
| 1118 | + } |
|
| 1119 | + return $this->get_one( |
|
| 1120 | + $this->alter_query_params_to_restrict_by_ID( |
|
| 1121 | + $id, |
|
| 1122 | + array('default_where_conditions' => EEM_Base::default_where_conditions_minimum_all) |
|
| 1123 | + ) |
|
| 1124 | + ); |
|
| 1125 | + } |
|
| 1126 | + |
|
| 1127 | + |
|
| 1128 | + |
|
| 1129 | + /** |
|
| 1130 | + * Alters query parameters to only get items with this ID are returned. |
|
| 1131 | + * Takes into account that the ID might be a string produced by EEM_Base::get_index_primary_key_string(), |
|
| 1132 | + * or could just be a simple primary key ID |
|
| 1133 | + * |
|
| 1134 | + * @param int $id |
|
| 1135 | + * @param array $query_params |
|
| 1136 | + * @return array of normal query params, @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1137 | + * @throws EE_Error |
|
| 1138 | + */ |
|
| 1139 | + public function alter_query_params_to_restrict_by_ID($id, $query_params = array()) |
|
| 1140 | + { |
|
| 1141 | + if (! isset($query_params[0])) { |
|
| 1142 | + $query_params[0] = array(); |
|
| 1143 | + } |
|
| 1144 | + $conditions_from_id = $this->parse_index_primary_key_string($id); |
|
| 1145 | + if ($conditions_from_id === null) { |
|
| 1146 | + $query_params[0][ $this->primary_key_name() ] = $id; |
|
| 1147 | + } else { |
|
| 1148 | + // no primary key, so the $id must be from the get_index_primary_key_string() |
|
| 1149 | + $query_params[0] = array_replace_recursive($query_params[0], $this->parse_index_primary_key_string($id)); |
|
| 1150 | + } |
|
| 1151 | + return $query_params; |
|
| 1152 | + } |
|
| 1153 | + |
|
| 1154 | + |
|
| 1155 | + |
|
| 1156 | + /** |
|
| 1157 | + * Gets a single item for this model from the DB, given the $query_params. Only returns a single class, not an |
|
| 1158 | + * array. If no item is found, null is returned. |
|
| 1159 | + * |
|
| 1160 | + * @param array $query_params like EEM_Base's $query_params variable. |
|
| 1161 | + * @return EE_Base_Class|EE_Soft_Delete_Base_Class|NULL |
|
| 1162 | + * @throws EE_Error |
|
| 1163 | + */ |
|
| 1164 | + public function get_one($query_params = array()) |
|
| 1165 | + { |
|
| 1166 | + if (! is_array($query_params)) { |
|
| 1167 | + EE_Error::doing_it_wrong( |
|
| 1168 | + 'EEM_Base::get_one', |
|
| 1169 | + sprintf( |
|
| 1170 | + __('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1171 | + gettype($query_params) |
|
| 1172 | + ), |
|
| 1173 | + '4.6.0' |
|
| 1174 | + ); |
|
| 1175 | + $query_params = array(); |
|
| 1176 | + } |
|
| 1177 | + $query_params['limit'] = 1; |
|
| 1178 | + $items = $this->get_all($query_params); |
|
| 1179 | + if (empty($items)) { |
|
| 1180 | + return null; |
|
| 1181 | + } |
|
| 1182 | + return array_shift($items); |
|
| 1183 | + } |
|
| 1184 | + |
|
| 1185 | + |
|
| 1186 | + |
|
| 1187 | + /** |
|
| 1188 | + * Returns the next x number of items in sequence from the given value as |
|
| 1189 | + * found in the database matching the given query conditions. |
|
| 1190 | + * |
|
| 1191 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1192 | + * @param null $field_to_order_by What field is used for the |
|
| 1193 | + * reference point. |
|
| 1194 | + * @param int $limit How many to return. |
|
| 1195 | + * @param array $query_params Extra conditions on the query. |
|
| 1196 | + * @param null $columns_to_select If left null, then an array of |
|
| 1197 | + * EE_Base_Class objects is returned, |
|
| 1198 | + * otherwise you can indicate just the |
|
| 1199 | + * columns you want returned. |
|
| 1200 | + * @return EE_Base_Class[]|array |
|
| 1201 | + * @throws EE_Error |
|
| 1202 | + */ |
|
| 1203 | + public function next_x( |
|
| 1204 | + $current_field_value, |
|
| 1205 | + $field_to_order_by = null, |
|
| 1206 | + $limit = 1, |
|
| 1207 | + $query_params = array(), |
|
| 1208 | + $columns_to_select = null |
|
| 1209 | + ) { |
|
| 1210 | + return $this->_get_consecutive( |
|
| 1211 | + $current_field_value, |
|
| 1212 | + '>', |
|
| 1213 | + $field_to_order_by, |
|
| 1214 | + $limit, |
|
| 1215 | + $query_params, |
|
| 1216 | + $columns_to_select |
|
| 1217 | + ); |
|
| 1218 | + } |
|
| 1219 | + |
|
| 1220 | + |
|
| 1221 | + |
|
| 1222 | + /** |
|
| 1223 | + * Returns the previous x number of items in sequence from the given value |
|
| 1224 | + * as found in the database matching the given query conditions. |
|
| 1225 | + * |
|
| 1226 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1227 | + * @param null $field_to_order_by What field is used for the |
|
| 1228 | + * reference point. |
|
| 1229 | + * @param int $limit How many to return. |
|
| 1230 | + * @param array $query_params Extra conditions on the query. |
|
| 1231 | + * @param null $columns_to_select If left null, then an array of |
|
| 1232 | + * EE_Base_Class objects is returned, |
|
| 1233 | + * otherwise you can indicate just the |
|
| 1234 | + * columns you want returned. |
|
| 1235 | + * @return EE_Base_Class[]|array |
|
| 1236 | + * @throws EE_Error |
|
| 1237 | + */ |
|
| 1238 | + public function previous_x( |
|
| 1239 | + $current_field_value, |
|
| 1240 | + $field_to_order_by = null, |
|
| 1241 | + $limit = 1, |
|
| 1242 | + $query_params = array(), |
|
| 1243 | + $columns_to_select = null |
|
| 1244 | + ) { |
|
| 1245 | + return $this->_get_consecutive( |
|
| 1246 | + $current_field_value, |
|
| 1247 | + '<', |
|
| 1248 | + $field_to_order_by, |
|
| 1249 | + $limit, |
|
| 1250 | + $query_params, |
|
| 1251 | + $columns_to_select |
|
| 1252 | + ); |
|
| 1253 | + } |
|
| 1254 | + |
|
| 1255 | + |
|
| 1256 | + |
|
| 1257 | + /** |
|
| 1258 | + * Returns the next item in sequence from the given value as found in the |
|
| 1259 | + * database matching the given query conditions. |
|
| 1260 | + * |
|
| 1261 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1262 | + * @param null $field_to_order_by What field is used for the |
|
| 1263 | + * reference point. |
|
| 1264 | + * @param array $query_params Extra conditions on the query. |
|
| 1265 | + * @param null $columns_to_select If left null, then an EE_Base_Class |
|
| 1266 | + * object is returned, otherwise you |
|
| 1267 | + * can indicate just the columns you |
|
| 1268 | + * want and a single array indexed by |
|
| 1269 | + * the columns will be returned. |
|
| 1270 | + * @return EE_Base_Class|null|array() |
|
| 1271 | + * @throws EE_Error |
|
| 1272 | + */ |
|
| 1273 | + public function next( |
|
| 1274 | + $current_field_value, |
|
| 1275 | + $field_to_order_by = null, |
|
| 1276 | + $query_params = array(), |
|
| 1277 | + $columns_to_select = null |
|
| 1278 | + ) { |
|
| 1279 | + $results = $this->_get_consecutive( |
|
| 1280 | + $current_field_value, |
|
| 1281 | + '>', |
|
| 1282 | + $field_to_order_by, |
|
| 1283 | + 1, |
|
| 1284 | + $query_params, |
|
| 1285 | + $columns_to_select |
|
| 1286 | + ); |
|
| 1287 | + return empty($results) ? null : reset($results); |
|
| 1288 | + } |
|
| 1289 | + |
|
| 1290 | + |
|
| 1291 | + |
|
| 1292 | + /** |
|
| 1293 | + * Returns the previous item in sequence from the given value as found in |
|
| 1294 | + * the database matching the given query conditions. |
|
| 1295 | + * |
|
| 1296 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1297 | + * @param null $field_to_order_by What field is used for the |
|
| 1298 | + * reference point. |
|
| 1299 | + * @param array $query_params Extra conditions on the query. |
|
| 1300 | + * @param null $columns_to_select If left null, then an EE_Base_Class |
|
| 1301 | + * object is returned, otherwise you |
|
| 1302 | + * can indicate just the columns you |
|
| 1303 | + * want and a single array indexed by |
|
| 1304 | + * the columns will be returned. |
|
| 1305 | + * @return EE_Base_Class|null|array() |
|
| 1306 | + * @throws EE_Error |
|
| 1307 | + */ |
|
| 1308 | + public function previous( |
|
| 1309 | + $current_field_value, |
|
| 1310 | + $field_to_order_by = null, |
|
| 1311 | + $query_params = array(), |
|
| 1312 | + $columns_to_select = null |
|
| 1313 | + ) { |
|
| 1314 | + $results = $this->_get_consecutive( |
|
| 1315 | + $current_field_value, |
|
| 1316 | + '<', |
|
| 1317 | + $field_to_order_by, |
|
| 1318 | + 1, |
|
| 1319 | + $query_params, |
|
| 1320 | + $columns_to_select |
|
| 1321 | + ); |
|
| 1322 | + return empty($results) ? null : reset($results); |
|
| 1323 | + } |
|
| 1324 | + |
|
| 1325 | + |
|
| 1326 | + |
|
| 1327 | + /** |
|
| 1328 | + * Returns the a consecutive number of items in sequence from the given |
|
| 1329 | + * value as found in the database matching the given query conditions. |
|
| 1330 | + * |
|
| 1331 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1332 | + * @param string $operand What operand is used for the sequence. |
|
| 1333 | + * @param string $field_to_order_by What field is used for the reference point. |
|
| 1334 | + * @param int $limit How many to return. |
|
| 1335 | + * @param array $query_params Extra conditions on the query. |
|
| 1336 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, |
|
| 1337 | + * otherwise you can indicate just the columns you want returned. |
|
| 1338 | + * @return EE_Base_Class[]|array |
|
| 1339 | + * @throws EE_Error |
|
| 1340 | + */ |
|
| 1341 | + protected function _get_consecutive( |
|
| 1342 | + $current_field_value, |
|
| 1343 | + $operand = '>', |
|
| 1344 | + $field_to_order_by = null, |
|
| 1345 | + $limit = 1, |
|
| 1346 | + $query_params = array(), |
|
| 1347 | + $columns_to_select = null |
|
| 1348 | + ) { |
|
| 1349 | + // if $field_to_order_by is empty then let's assume we're ordering by the primary key. |
|
| 1350 | + if (empty($field_to_order_by)) { |
|
| 1351 | + if ($this->has_primary_key_field()) { |
|
| 1352 | + $field_to_order_by = $this->get_primary_key_field()->get_name(); |
|
| 1353 | + } else { |
|
| 1354 | + if (WP_DEBUG) { |
|
| 1355 | + throw new EE_Error(__( |
|
| 1356 | + 'EEM_Base::_get_consecutive() has been called with no $field_to_order_by argument and there is no primary key on the field. Please provide the field you would like to use as the base for retrieving the next item(s).', |
|
| 1357 | + 'event_espresso' |
|
| 1358 | + )); |
|
| 1359 | + } |
|
| 1360 | + EE_Error::add_error(__('There was an error with the query.', 'event_espresso')); |
|
| 1361 | + return array(); |
|
| 1362 | + } |
|
| 1363 | + } |
|
| 1364 | + if (! is_array($query_params)) { |
|
| 1365 | + EE_Error::doing_it_wrong( |
|
| 1366 | + 'EEM_Base::_get_consecutive', |
|
| 1367 | + sprintf( |
|
| 1368 | + __('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1369 | + gettype($query_params) |
|
| 1370 | + ), |
|
| 1371 | + '4.6.0' |
|
| 1372 | + ); |
|
| 1373 | + $query_params = array(); |
|
| 1374 | + } |
|
| 1375 | + // let's add the where query param for consecutive look up. |
|
| 1376 | + $query_params[0][ $field_to_order_by ] = array($operand, $current_field_value); |
|
| 1377 | + $query_params['limit'] = $limit; |
|
| 1378 | + // set direction |
|
| 1379 | + $incoming_orderby = isset($query_params['order_by']) ? (array) $query_params['order_by'] : array(); |
|
| 1380 | + $query_params['order_by'] = $operand === '>' |
|
| 1381 | + ? array($field_to_order_by => 'ASC') + $incoming_orderby |
|
| 1382 | + : array($field_to_order_by => 'DESC') + $incoming_orderby; |
|
| 1383 | + // if $columns_to_select is empty then that means we're returning EE_Base_Class objects |
|
| 1384 | + if (empty($columns_to_select)) { |
|
| 1385 | + return $this->get_all($query_params); |
|
| 1386 | + } |
|
| 1387 | + // getting just the fields |
|
| 1388 | + return $this->_get_all_wpdb_results($query_params, ARRAY_A, $columns_to_select); |
|
| 1389 | + } |
|
| 1390 | + |
|
| 1391 | + |
|
| 1392 | + |
|
| 1393 | + /** |
|
| 1394 | + * This sets the _timezone property after model object has been instantiated. |
|
| 1395 | + * |
|
| 1396 | + * @param null | string $timezone valid PHP DateTimeZone timezone string |
|
| 1397 | + */ |
|
| 1398 | + public function set_timezone($timezone) |
|
| 1399 | + { |
|
| 1400 | + if ($timezone !== null) { |
|
| 1401 | + $this->_timezone = $timezone; |
|
| 1402 | + } |
|
| 1403 | + // note we need to loop through relations and set the timezone on those objects as well. |
|
| 1404 | + foreach ($this->_model_relations as $relation) { |
|
| 1405 | + $relation->set_timezone($timezone); |
|
| 1406 | + } |
|
| 1407 | + // and finally we do the same for any datetime fields |
|
| 1408 | + foreach ($this->_fields as $field) { |
|
| 1409 | + if ($field instanceof EE_Datetime_Field) { |
|
| 1410 | + $field->set_timezone($timezone); |
|
| 1411 | + } |
|
| 1412 | + } |
|
| 1413 | + } |
|
| 1414 | + |
|
| 1415 | + |
|
| 1416 | + |
|
| 1417 | + /** |
|
| 1418 | + * This just returns whatever is set for the current timezone. |
|
| 1419 | + * |
|
| 1420 | + * @access public |
|
| 1421 | + * @return string |
|
| 1422 | + */ |
|
| 1423 | + public function get_timezone() |
|
| 1424 | + { |
|
| 1425 | + // first validate if timezone is set. If not, then let's set it be whatever is set on the model fields. |
|
| 1426 | + if (empty($this->_timezone)) { |
|
| 1427 | + foreach ($this->_fields as $field) { |
|
| 1428 | + if ($field instanceof EE_Datetime_Field) { |
|
| 1429 | + $this->set_timezone($field->get_timezone()); |
|
| 1430 | + break; |
|
| 1431 | + } |
|
| 1432 | + } |
|
| 1433 | + } |
|
| 1434 | + // if timezone STILL empty then return the default timezone for the site. |
|
| 1435 | + if (empty($this->_timezone)) { |
|
| 1436 | + $this->set_timezone(EEH_DTT_Helper::get_timezone()); |
|
| 1437 | + } |
|
| 1438 | + return $this->_timezone; |
|
| 1439 | + } |
|
| 1440 | + |
|
| 1441 | + |
|
| 1442 | + |
|
| 1443 | + /** |
|
| 1444 | + * This returns the date formats set for the given field name and also ensures that |
|
| 1445 | + * $this->_timezone property is set correctly. |
|
| 1446 | + * |
|
| 1447 | + * @since 4.6.x |
|
| 1448 | + * @param string $field_name The name of the field the formats are being retrieved for. |
|
| 1449 | + * @param bool $pretty Whether to return the pretty formats (true) or not (false). |
|
| 1450 | + * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
| 1451 | + * @return array formats in an array with the date format first, and the time format last. |
|
| 1452 | + */ |
|
| 1453 | + public function get_formats_for($field_name, $pretty = false) |
|
| 1454 | + { |
|
| 1455 | + $field_settings = $this->field_settings_for($field_name); |
|
| 1456 | + // if not a valid EE_Datetime_Field then throw error |
|
| 1457 | + if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1458 | + throw new EE_Error(sprintf(__( |
|
| 1459 | + 'The field sent into EEM_Base::get_formats_for (%s) is not registered as a EE_Datetime_Field. Please check the spelling and make sure you are submitting the right field name to retrieve date_formats for.', |
|
| 1460 | + 'event_espresso' |
|
| 1461 | + ), $field_name)); |
|
| 1462 | + } |
|
| 1463 | + // while we are here, let's make sure the timezone internally in EEM_Base matches what is stored on |
|
| 1464 | + // the field. |
|
| 1465 | + $this->_timezone = $field_settings->get_timezone(); |
|
| 1466 | + return array($field_settings->get_date_format($pretty), $field_settings->get_time_format($pretty)); |
|
| 1467 | + } |
|
| 1468 | + |
|
| 1469 | + |
|
| 1470 | + |
|
| 1471 | + /** |
|
| 1472 | + * This returns the current time in a format setup for a query on this model. |
|
| 1473 | + * Usage of this method makes it easier to setup queries against EE_Datetime_Field columns because |
|
| 1474 | + * it will return: |
|
| 1475 | + * - a formatted string in the timezone and format currently set on the EE_Datetime_Field for the given field for |
|
| 1476 | + * NOW |
|
| 1477 | + * - or a unix timestamp (equivalent to time()) |
|
| 1478 | + * Note: When requesting a formatted string, if the date or time format doesn't include seconds, for example, |
|
| 1479 | + * the time returned, because it uses that format, will also NOT include seconds. For this reason, if you want |
|
| 1480 | + * the time returned to be the current time down to the exact second, set $timestamp to true. |
|
| 1481 | + * @since 4.6.x |
|
| 1482 | + * @param string $field_name The field the current time is needed for. |
|
| 1483 | + * @param bool $timestamp True means to return a unix timestamp. Otherwise a |
|
| 1484 | + * formatted string matching the set format for the field in the set timezone will |
|
| 1485 | + * be returned. |
|
| 1486 | + * @param string $what Whether to return the string in just the time format, the date format, or both. |
|
| 1487 | + * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
| 1488 | + * @return int|string If the given field_name is not of the EE_Datetime_Field type, then an EE_Error |
|
| 1489 | + * exception is triggered. |
|
| 1490 | + */ |
|
| 1491 | + public function current_time_for_query($field_name, $timestamp = false, $what = 'both') |
|
| 1492 | + { |
|
| 1493 | + $formats = $this->get_formats_for($field_name); |
|
| 1494 | + $DateTime = new DateTime("now", new DateTimeZone($this->_timezone)); |
|
| 1495 | + if ($timestamp) { |
|
| 1496 | + return $DateTime->format('U'); |
|
| 1497 | + } |
|
| 1498 | + // not returning timestamp, so return formatted string in timezone. |
|
| 1499 | + switch ($what) { |
|
| 1500 | + case 'time': |
|
| 1501 | + return $DateTime->format($formats[1]); |
|
| 1502 | + break; |
|
| 1503 | + case 'date': |
|
| 1504 | + return $DateTime->format($formats[0]); |
|
| 1505 | + break; |
|
| 1506 | + default: |
|
| 1507 | + return $DateTime->format(implode(' ', $formats)); |
|
| 1508 | + break; |
|
| 1509 | + } |
|
| 1510 | + } |
|
| 1511 | + |
|
| 1512 | + |
|
| 1513 | + |
|
| 1514 | + /** |
|
| 1515 | + * This receives a time string for a given field and ensures that it is setup to match what the internal settings |
|
| 1516 | + * for the model are. Returns a DateTime object. |
|
| 1517 | + * Note: a gotcha for when you send in unix timestamp. Remember a unix timestamp is already timezone agnostic, |
|
| 1518 | + * (functionally the equivalent of UTC+0). So when you send it in, whatever timezone string you include is |
|
| 1519 | + * ignored. |
|
| 1520 | + * |
|
| 1521 | + * @param string $field_name The field being setup. |
|
| 1522 | + * @param string $timestring The date time string being used. |
|
| 1523 | + * @param string $incoming_format The format for the time string. |
|
| 1524 | + * @param string $timezone By default, it is assumed the incoming time string is in timezone for |
|
| 1525 | + * the blog. If this is not the case, then it can be specified here. If incoming |
|
| 1526 | + * format is |
|
| 1527 | + * 'U', this is ignored. |
|
| 1528 | + * @return DateTime |
|
| 1529 | + * @throws EE_Error |
|
| 1530 | + */ |
|
| 1531 | + public function convert_datetime_for_query($field_name, $timestring, $incoming_format, $timezone = '') |
|
| 1532 | + { |
|
| 1533 | + // just using this to ensure the timezone is set correctly internally |
|
| 1534 | + $this->get_formats_for($field_name); |
|
| 1535 | + // load EEH_DTT_Helper |
|
| 1536 | + $set_timezone = empty($timezone) ? EEH_DTT_Helper::get_timezone() : $timezone; |
|
| 1537 | + $incomingDateTime = date_create_from_format($incoming_format, $timestring, new DateTimeZone($set_timezone)); |
|
| 1538 | + EEH_DTT_Helper::setTimezone($incomingDateTime, new DateTimeZone($this->_timezone)); |
|
| 1539 | + return \EventEspresso\core\domain\entities\DbSafeDateTime::createFromDateTime($incomingDateTime); |
|
| 1540 | + } |
|
| 1541 | + |
|
| 1542 | + |
|
| 1543 | + |
|
| 1544 | + /** |
|
| 1545 | + * Gets all the tables comprising this model. Array keys are the table aliases, and values are EE_Table objects |
|
| 1546 | + * |
|
| 1547 | + * @return EE_Table_Base[] |
|
| 1548 | + */ |
|
| 1549 | + public function get_tables() |
|
| 1550 | + { |
|
| 1551 | + return $this->_tables; |
|
| 1552 | + } |
|
| 1553 | + |
|
| 1554 | + |
|
| 1555 | + |
|
| 1556 | + /** |
|
| 1557 | + * Updates all the database entries (in each table for this model) according to $fields_n_values and optionally |
|
| 1558 | + * also updates all the model objects, where the criteria expressed in $query_params are met.. |
|
| 1559 | + * Also note: if this model has multiple tables, this update verifies all the secondary tables have an entry for |
|
| 1560 | + * each row (in the primary table) we're trying to update; if not, it inserts an entry in the secondary table. Eg: |
|
| 1561 | + * if our model has 2 tables: wp_posts (primary), and wp_esp_event (secondary). Let's say we are trying to update a |
|
| 1562 | + * model object with EVT_ID = 1 |
|
| 1563 | + * (which means where wp_posts has ID = 1, because wp_posts.ID is the primary key's column), which exists, but |
|
| 1564 | + * there is no entry in wp_esp_event for this entry in wp_posts. So, this update script will insert a row into |
|
| 1565 | + * wp_esp_event, using any available parameters from $fields_n_values (eg, if "EVT_limit" => 40 is in |
|
| 1566 | + * $fields_n_values, the new entry in wp_esp_event will set EVT_limit = 40, and use default for other columns which |
|
| 1567 | + * are not specified) |
|
| 1568 | + * |
|
| 1569 | + * @param array $fields_n_values keys are model fields (exactly like keys in EEM_Base::_fields, NOT db |
|
| 1570 | + * columns!), values are strings, ints, floats, and maybe arrays if they |
|
| 1571 | + * are to be serialized. Basically, the values are what you'd expect to be |
|
| 1572 | + * values on the model, NOT necessarily what's in the DB. For example, if |
|
| 1573 | + * we wanted to update only the TXN_details on any Transactions where its |
|
| 1574 | + * ID=34, we'd use this method as follows: |
|
| 1575 | + * EEM_Transaction::instance()->update( |
|
| 1576 | + * array('TXN_details'=>array('detail1'=>'monkey','detail2'=>'banana'), |
|
| 1577 | + * array(array('TXN_ID'=>34))); |
|
| 1578 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1579 | + * Eg, consider updating Question's QST_admin_label field is of type |
|
| 1580 | + * Simple_HTML. If you use this function to update that field to $new_value |
|
| 1581 | + * = (note replace 8's with appropriate opening and closing tags in the |
|
| 1582 | + * following example)"8script8alert('I hack all');8/script88b8boom |
|
| 1583 | + * baby8/b8", then if you set $values_already_prepared_by_model_object to |
|
| 1584 | + * TRUE, it is assumed that you've already called |
|
| 1585 | + * EE_Simple_HTML_Field->prepare_for_set($new_value), which removes the |
|
| 1586 | + * malicious javascript. However, if |
|
| 1587 | + * $values_already_prepared_by_model_object is left as FALSE, then |
|
| 1588 | + * EE_Simple_HTML_Field->prepare_for_set($new_value) will be called on it, |
|
| 1589 | + * and every other field, before insertion. We provide this parameter |
|
| 1590 | + * because model objects perform their prepare_for_set function on all |
|
| 1591 | + * their values, and so don't need to be called again (and in many cases, |
|
| 1592 | + * shouldn't be called again. Eg: if we escape HTML characters in the |
|
| 1593 | + * prepare_for_set method...) |
|
| 1594 | + * @param boolean $keep_model_objs_in_sync if TRUE, makes sure we ALSO update model objects |
|
| 1595 | + * in this model's entity map according to $fields_n_values that match |
|
| 1596 | + * $query_params. This obviously has some overhead, so you can disable it |
|
| 1597 | + * by setting this to FALSE, but be aware that model objects being used |
|
| 1598 | + * could get out-of-sync with the database |
|
| 1599 | + * @return int how many rows got updated or FALSE if something went wrong with the query (wp returns FALSE or num |
|
| 1600 | + * rows affected which *could* include 0 which DOES NOT mean the query was |
|
| 1601 | + * bad) |
|
| 1602 | + * @throws EE_Error |
|
| 1603 | + */ |
|
| 1604 | + public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true) |
|
| 1605 | + { |
|
| 1606 | + if (! is_array($query_params)) { |
|
| 1607 | + EE_Error::doing_it_wrong( |
|
| 1608 | + 'EEM_Base::update', |
|
| 1609 | + sprintf( |
|
| 1610 | + __('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1611 | + gettype($query_params) |
|
| 1612 | + ), |
|
| 1613 | + '4.6.0' |
|
| 1614 | + ); |
|
| 1615 | + $query_params = array(); |
|
| 1616 | + } |
|
| 1617 | + /** |
|
| 1618 | + * Action called before a model update call has been made. |
|
| 1619 | + * |
|
| 1620 | + * @param EEM_Base $model |
|
| 1621 | + * @param array $fields_n_values the updated fields and their new values |
|
| 1622 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1623 | + */ |
|
| 1624 | + do_action('AHEE__EEM_Base__update__begin', $this, $fields_n_values, $query_params); |
|
| 1625 | + /** |
|
| 1626 | + * Filters the fields about to be updated given the query parameters. You can provide the |
|
| 1627 | + * $query_params to $this->get_all() to find exactly which records will be updated |
|
| 1628 | + * |
|
| 1629 | + * @param array $fields_n_values fields and their new values |
|
| 1630 | + * @param EEM_Base $model the model being queried |
|
| 1631 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1632 | + */ |
|
| 1633 | + $fields_n_values = (array) apply_filters( |
|
| 1634 | + 'FHEE__EEM_Base__update__fields_n_values', |
|
| 1635 | + $fields_n_values, |
|
| 1636 | + $this, |
|
| 1637 | + $query_params |
|
| 1638 | + ); |
|
| 1639 | + // need to verify that, for any entry we want to update, there are entries in each secondary table. |
|
| 1640 | + // to do that, for each table, verify that it's PK isn't null. |
|
| 1641 | + $tables = $this->get_tables(); |
|
| 1642 | + // and if the other tables don't have a row for each table-to-be-updated, we'll insert one with whatever values available in the current update query |
|
| 1643 | + // NOTE: we should make this code more efficient by NOT querying twice |
|
| 1644 | + // before the real update, but that needs to first go through ALPHA testing |
|
| 1645 | + // as it's dangerous. says Mike August 8 2014 |
|
| 1646 | + // we want to make sure the default_where strategy is ignored |
|
| 1647 | + $this->_ignore_where_strategy = true; |
|
| 1648 | + $wpdb_select_results = $this->_get_all_wpdb_results($query_params); |
|
| 1649 | + foreach ($wpdb_select_results as $wpdb_result) { |
|
| 1650 | + // type cast stdClass as array |
|
| 1651 | + $wpdb_result = (array) $wpdb_result; |
|
| 1652 | + // get the model object's PK, as we'll want this if we need to insert a row into secondary tables |
|
| 1653 | + if ($this->has_primary_key_field()) { |
|
| 1654 | + $main_table_pk_value = $wpdb_result[ $this->get_primary_key_field()->get_qualified_column() ]; |
|
| 1655 | + } else { |
|
| 1656 | + // if there's no primary key, we basically can't support having a 2nd table on the model (we could but it would be lots of work) |
|
| 1657 | + $main_table_pk_value = null; |
|
| 1658 | + } |
|
| 1659 | + // if there are more than 1 tables, we'll want to verify that each table for this model has an entry in the other tables |
|
| 1660 | + // and if the other tables don't have a row for each table-to-be-updated, we'll insert one with whatever values available in the current update query |
|
| 1661 | + if (count($tables) > 1) { |
|
| 1662 | + // foreach matching row in the DB, ensure that each table's PK isn't null. If so, there must not be an entry |
|
| 1663 | + // in that table, and so we'll want to insert one |
|
| 1664 | + foreach ($tables as $table_obj) { |
|
| 1665 | + $this_table_pk_column = $table_obj->get_fully_qualified_pk_column(); |
|
| 1666 | + // if there is no private key for this table on the results, it means there's no entry |
|
| 1667 | + // in this table, right? so insert a row in the current table, using any fields available |
|
| 1668 | + if (! (array_key_exists($this_table_pk_column, $wpdb_result) |
|
| 1669 | + && $wpdb_result[ $this_table_pk_column ]) |
|
| 1670 | + ) { |
|
| 1671 | + $success = $this->_insert_into_specific_table( |
|
| 1672 | + $table_obj, |
|
| 1673 | + $fields_n_values, |
|
| 1674 | + $main_table_pk_value |
|
| 1675 | + ); |
|
| 1676 | + // if we died here, report the error |
|
| 1677 | + if (! $success) { |
|
| 1678 | + return false; |
|
| 1679 | + } |
|
| 1680 | + } |
|
| 1681 | + } |
|
| 1682 | + } |
|
| 1683 | + // //and now check that if we have cached any models by that ID on the model, that |
|
| 1684 | + // //they also get updated properly |
|
| 1685 | + // $model_object = $this->get_from_entity_map( $main_table_pk_value ); |
|
| 1686 | + // if( $model_object ){ |
|
| 1687 | + // foreach( $fields_n_values as $field => $value ){ |
|
| 1688 | + // $model_object->set($field, $value); |
|
| 1689 | + // let's make sure default_where strategy is followed now |
|
| 1690 | + $this->_ignore_where_strategy = false; |
|
| 1691 | + } |
|
| 1692 | + // if we want to keep model objects in sync, AND |
|
| 1693 | + // if this wasn't called from a model object (to update itself) |
|
| 1694 | + // then we want to make sure we keep all the existing |
|
| 1695 | + // model objects in sync with the db |
|
| 1696 | + if ($keep_model_objs_in_sync && ! $this->_values_already_prepared_by_model_object) { |
|
| 1697 | + if ($this->has_primary_key_field()) { |
|
| 1698 | + $model_objs_affected_ids = $this->get_col($query_params); |
|
| 1699 | + } else { |
|
| 1700 | + // we need to select a bunch of columns and then combine them into the the "index primary key string"s |
|
| 1701 | + $models_affected_key_columns = $this->_get_all_wpdb_results($query_params, ARRAY_A); |
|
| 1702 | + $model_objs_affected_ids = array(); |
|
| 1703 | + foreach ($models_affected_key_columns as $row) { |
|
| 1704 | + $combined_index_key = $this->get_index_primary_key_string($row); |
|
| 1705 | + $model_objs_affected_ids[ $combined_index_key ] = $combined_index_key; |
|
| 1706 | + } |
|
| 1707 | + } |
|
| 1708 | + if (! $model_objs_affected_ids) { |
|
| 1709 | + // wait wait wait- if nothing was affected let's stop here |
|
| 1710 | + return 0; |
|
| 1711 | + } |
|
| 1712 | + foreach ($model_objs_affected_ids as $id) { |
|
| 1713 | + $model_obj_in_entity_map = $this->get_from_entity_map($id); |
|
| 1714 | + if ($model_obj_in_entity_map) { |
|
| 1715 | + foreach ($fields_n_values as $field => $new_value) { |
|
| 1716 | + $model_obj_in_entity_map->set($field, $new_value); |
|
| 1717 | + } |
|
| 1718 | + } |
|
| 1719 | + } |
|
| 1720 | + // if there is a primary key on this model, we can now do a slight optimization |
|
| 1721 | + if ($this->has_primary_key_field()) { |
|
| 1722 | + // we already know what we want to update. So let's make the query simpler so it's a little more efficient |
|
| 1723 | + $query_params = array( |
|
| 1724 | + array($this->primary_key_name() => array('IN', $model_objs_affected_ids)), |
|
| 1725 | + 'limit' => count($model_objs_affected_ids), |
|
| 1726 | + 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
| 1727 | + ); |
|
| 1728 | + } |
|
| 1729 | + } |
|
| 1730 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1731 | + $SQL = "UPDATE " |
|
| 1732 | + . $model_query_info->get_full_join_sql() |
|
| 1733 | + . " SET " |
|
| 1734 | + . $this->_construct_update_sql($fields_n_values) |
|
| 1735 | + . $model_query_info->get_where_sql();// note: doesn't use _construct_2nd_half_of_select_query() because doesn't accept LIMIT, ORDER BY, etc. |
|
| 1736 | + $rows_affected = $this->_do_wpdb_query('query', array($SQL)); |
|
| 1737 | + /** |
|
| 1738 | + * Action called after a model update call has been made. |
|
| 1739 | + * |
|
| 1740 | + * @param EEM_Base $model |
|
| 1741 | + * @param array $fields_n_values the updated fields and their new values |
|
| 1742 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1743 | + * @param int $rows_affected |
|
| 1744 | + */ |
|
| 1745 | + do_action('AHEE__EEM_Base__update__end', $this, $fields_n_values, $query_params, $rows_affected); |
|
| 1746 | + return $rows_affected;// how many supposedly got updated |
|
| 1747 | + } |
|
| 1748 | + |
|
| 1749 | + |
|
| 1750 | + |
|
| 1751 | + /** |
|
| 1752 | + * Analogous to $wpdb->get_col, returns a 1-dimensional array where teh values |
|
| 1753 | + * are teh values of the field specified (or by default the primary key field) |
|
| 1754 | + * that matched the query params. Note that you should pass the name of the |
|
| 1755 | + * model FIELD, not the database table's column name. |
|
| 1756 | + * |
|
| 1757 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1758 | + * @param string $field_to_select |
|
| 1759 | + * @return array just like $wpdb->get_col() |
|
| 1760 | + * @throws EE_Error |
|
| 1761 | + */ |
|
| 1762 | + public function get_col($query_params = array(), $field_to_select = null) |
|
| 1763 | + { |
|
| 1764 | + if ($field_to_select) { |
|
| 1765 | + $field = $this->field_settings_for($field_to_select); |
|
| 1766 | + } elseif ($this->has_primary_key_field()) { |
|
| 1767 | + $field = $this->get_primary_key_field(); |
|
| 1768 | + } else { |
|
| 1769 | + // no primary key, just grab the first column |
|
| 1770 | + $field = reset($this->field_settings()); |
|
| 1771 | + } |
|
| 1772 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1773 | + $select_expressions = $field->get_qualified_column(); |
|
| 1774 | + $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 1775 | + return $this->_do_wpdb_query('get_col', array($SQL)); |
|
| 1776 | + } |
|
| 1777 | + |
|
| 1778 | + |
|
| 1779 | + |
|
| 1780 | + /** |
|
| 1781 | + * Returns a single column value for a single row from the database |
|
| 1782 | + * |
|
| 1783 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1784 | + * @param string $field_to_select @see EEM_Base::get_col() |
|
| 1785 | + * @return string |
|
| 1786 | + * @throws EE_Error |
|
| 1787 | + */ |
|
| 1788 | + public function get_var($query_params = array(), $field_to_select = null) |
|
| 1789 | + { |
|
| 1790 | + $query_params['limit'] = 1; |
|
| 1791 | + $col = $this->get_col($query_params, $field_to_select); |
|
| 1792 | + if (! empty($col)) { |
|
| 1793 | + return reset($col); |
|
| 1794 | + } |
|
| 1795 | + return null; |
|
| 1796 | + } |
|
| 1797 | + |
|
| 1798 | + |
|
| 1799 | + |
|
| 1800 | + /** |
|
| 1801 | + * Makes the SQL for after "UPDATE table_X inner join table_Y..." and before "...WHERE". Eg "Question.name='party |
|
| 1802 | + * time?', Question.desc='what do you think?',..." Values are filtered through wpdb->prepare to avoid against SQL |
|
| 1803 | + * injection, but currently no further filtering is done |
|
| 1804 | + * |
|
| 1805 | + * @global $wpdb |
|
| 1806 | + * @param array $fields_n_values array keys are field names on this model, and values are what those fields should |
|
| 1807 | + * be updated to in the DB |
|
| 1808 | + * @return string of SQL |
|
| 1809 | + * @throws EE_Error |
|
| 1810 | + */ |
|
| 1811 | + public function _construct_update_sql($fields_n_values) |
|
| 1812 | + { |
|
| 1813 | + /** @type WPDB $wpdb */ |
|
| 1814 | + global $wpdb; |
|
| 1815 | + $cols_n_values = array(); |
|
| 1816 | + foreach ($fields_n_values as $field_name => $value) { |
|
| 1817 | + $field_obj = $this->field_settings_for($field_name); |
|
| 1818 | + // if the value is NULL, we want to assign the value to that. |
|
| 1819 | + // wpdb->prepare doesn't really handle that properly |
|
| 1820 | + $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
| 1821 | + $value_sql = $prepared_value === null ? 'NULL' |
|
| 1822 | + : $wpdb->prepare($field_obj->get_wpdb_data_type(), $prepared_value); |
|
| 1823 | + $cols_n_values[] = $field_obj->get_qualified_column() . "=" . $value_sql; |
|
| 1824 | + } |
|
| 1825 | + return implode(",", $cols_n_values); |
|
| 1826 | + } |
|
| 1827 | + |
|
| 1828 | + |
|
| 1829 | + |
|
| 1830 | + /** |
|
| 1831 | + * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
| 1832 | + * Performs a HARD delete, meaning the database row should always be removed, |
|
| 1833 | + * not just have a flag field on it switched |
|
| 1834 | + * Wrapper for EEM_Base::delete_permanently() |
|
| 1835 | + * |
|
| 1836 | + * @param mixed $id |
|
| 1837 | + * @param boolean $allow_blocking |
|
| 1838 | + * @return int the number of rows deleted |
|
| 1839 | + * @throws EE_Error |
|
| 1840 | + */ |
|
| 1841 | + public function delete_permanently_by_ID($id, $allow_blocking = true) |
|
| 1842 | + { |
|
| 1843 | + return $this->delete_permanently( |
|
| 1844 | + array( |
|
| 1845 | + array($this->get_primary_key_field()->get_name() => $id), |
|
| 1846 | + 'limit' => 1, |
|
| 1847 | + ), |
|
| 1848 | + $allow_blocking |
|
| 1849 | + ); |
|
| 1850 | + } |
|
| 1851 | + |
|
| 1852 | + |
|
| 1853 | + |
|
| 1854 | + /** |
|
| 1855 | + * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
| 1856 | + * Wrapper for EEM_Base::delete() |
|
| 1857 | + * |
|
| 1858 | + * @param mixed $id |
|
| 1859 | + * @param boolean $allow_blocking |
|
| 1860 | + * @return int the number of rows deleted |
|
| 1861 | + * @throws EE_Error |
|
| 1862 | + */ |
|
| 1863 | + public function delete_by_ID($id, $allow_blocking = true) |
|
| 1864 | + { |
|
| 1865 | + return $this->delete( |
|
| 1866 | + array( |
|
| 1867 | + array($this->get_primary_key_field()->get_name() => $id), |
|
| 1868 | + 'limit' => 1, |
|
| 1869 | + ), |
|
| 1870 | + $allow_blocking |
|
| 1871 | + ); |
|
| 1872 | + } |
|
| 1873 | + |
|
| 1874 | + |
|
| 1875 | + |
|
| 1876 | + /** |
|
| 1877 | + * Identical to delete_permanently, but does a "soft" delete if possible, |
|
| 1878 | + * meaning if the model has a field that indicates its been "trashed" or |
|
| 1879 | + * "soft deleted", we will just set that instead of actually deleting the rows. |
|
| 1880 | + * |
|
| 1881 | + * @see EEM_Base::delete_permanently |
|
| 1882 | + * @param array $query_params |
|
| 1883 | + * @param boolean $allow_blocking |
|
| 1884 | + * @return int how many rows got deleted |
|
| 1885 | + * @throws EE_Error |
|
| 1886 | + */ |
|
| 1887 | + public function delete($query_params, $allow_blocking = true) |
|
| 1888 | + { |
|
| 1889 | + return $this->delete_permanently($query_params, $allow_blocking); |
|
| 1890 | + } |
|
| 1891 | + |
|
| 1892 | + |
|
| 1893 | + |
|
| 1894 | + /** |
|
| 1895 | + * Deletes the model objects that meet the query params. Note: this method is overridden |
|
| 1896 | + * in EEM_Soft_Delete_Base so that soft-deleted model objects are instead only flagged |
|
| 1897 | + * as archived, not actually deleted |
|
| 1898 | + * |
|
| 1899 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1900 | + * @param boolean $allow_blocking if TRUE, matched objects will only be deleted if there is no related model info |
|
| 1901 | + * that blocks it (ie, there' sno other data that depends on this data); if false, |
|
| 1902 | + * deletes regardless of other objects which may depend on it. Its generally |
|
| 1903 | + * advisable to always leave this as TRUE, otherwise you could easily corrupt your |
|
| 1904 | + * DB |
|
| 1905 | + * @return int how many rows got deleted |
|
| 1906 | + * @throws EE_Error |
|
| 1907 | + */ |
|
| 1908 | + public function delete_permanently($query_params, $allow_blocking = true) |
|
| 1909 | + { |
|
| 1910 | + /** |
|
| 1911 | + * Action called just before performing a real deletion query. You can use the |
|
| 1912 | + * model and its $query_params to find exactly which items will be deleted |
|
| 1913 | + * |
|
| 1914 | + * @param EEM_Base $model |
|
| 1915 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 1916 | + * @param boolean $allow_blocking whether or not to allow related model objects |
|
| 1917 | + * to block (prevent) this deletion |
|
| 1918 | + */ |
|
| 1919 | + do_action('AHEE__EEM_Base__delete__begin', $this, $query_params, $allow_blocking); |
|
| 1920 | + // some MySQL databases may be running safe mode, which may restrict |
|
| 1921 | + // deletion if there is no KEY column used in the WHERE statement of a deletion. |
|
| 1922 | + // to get around this, we first do a SELECT, get all the IDs, and then run another query |
|
| 1923 | + // to delete them |
|
| 1924 | + $items_for_deletion = $this->_get_all_wpdb_results($query_params); |
|
| 1925 | + $columns_and_ids_for_deleting = $this->_get_ids_for_delete($items_for_deletion, $allow_blocking); |
|
| 1926 | + $deletion_where_query_part = $this->_build_query_part_for_deleting_from_columns_and_values( |
|
| 1927 | + $columns_and_ids_for_deleting |
|
| 1928 | + ); |
|
| 1929 | + /** |
|
| 1930 | + * Allows client code to act on the items being deleted before the query is actually executed. |
|
| 1931 | + * |
|
| 1932 | + * @param EEM_Base $this The model instance being acted on. |
|
| 1933 | + * @param array $query_params The incoming array of query parameters influencing what gets deleted. |
|
| 1934 | + * @param bool $allow_blocking @see param description in method phpdoc block. |
|
| 1935 | + * @param array $columns_and_ids_for_deleting An array indicating what entities will get removed as |
|
| 1936 | + * derived from the incoming query parameters. |
|
| 1937 | + * @see details on the structure of this array in the phpdocs |
|
| 1938 | + * for the `_get_ids_for_delete_method` |
|
| 1939 | + * |
|
| 1940 | + */ |
|
| 1941 | + do_action( |
|
| 1942 | + 'AHEE__EEM_Base__delete__before_query', |
|
| 1943 | + $this, |
|
| 1944 | + $query_params, |
|
| 1945 | + $allow_blocking, |
|
| 1946 | + $columns_and_ids_for_deleting |
|
| 1947 | + ); |
|
| 1948 | + if ($deletion_where_query_part) { |
|
| 1949 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1950 | + $table_aliases = array_keys($this->_tables); |
|
| 1951 | + $SQL = "DELETE " |
|
| 1952 | + . implode(", ", $table_aliases) |
|
| 1953 | + . " FROM " |
|
| 1954 | + . $model_query_info->get_full_join_sql() |
|
| 1955 | + . " WHERE " |
|
| 1956 | + . $deletion_where_query_part; |
|
| 1957 | + $rows_deleted = $this->_do_wpdb_query('query', array($SQL)); |
|
| 1958 | + } else { |
|
| 1959 | + $rows_deleted = 0; |
|
| 1960 | + } |
|
| 1961 | + |
|
| 1962 | + // Next, make sure those items are removed from the entity map; if they could be put into it at all; and if |
|
| 1963 | + // there was no error with the delete query. |
|
| 1964 | + if ($this->has_primary_key_field() |
|
| 1965 | + && $rows_deleted !== false |
|
| 1966 | + && isset($columns_and_ids_for_deleting[ $this->get_primary_key_field()->get_qualified_column() ]) |
|
| 1967 | + ) { |
|
| 1968 | + $ids_for_removal = $columns_and_ids_for_deleting[ $this->get_primary_key_field()->get_qualified_column() ]; |
|
| 1969 | + foreach ($ids_for_removal as $id) { |
|
| 1970 | + if (isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ])) { |
|
| 1971 | + unset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]); |
|
| 1972 | + } |
|
| 1973 | + } |
|
| 1974 | + |
|
| 1975 | + // delete any extra meta attached to the deleted entities but ONLY if this model is not an instance of |
|
| 1976 | + // `EEM_Extra_Meta`. In other words we want to prevent recursion on EEM_Extra_Meta::delete_permanently calls |
|
| 1977 | + // unnecessarily. It's very unlikely that users will have assigned Extra Meta to Extra Meta |
|
| 1978 | + // (although it is possible). |
|
| 1979 | + // Note this can be skipped by using the provided filter and returning false. |
|
| 1980 | + if (apply_filters( |
|
| 1981 | + 'FHEE__EEM_Base__delete_permanently__dont_delete_extra_meta_for_extra_meta', |
|
| 1982 | + ! $this instanceof EEM_Extra_Meta, |
|
| 1983 | + $this |
|
| 1984 | + )) { |
|
| 1985 | + EEM_Extra_Meta::instance()->delete_permanently(array( |
|
| 1986 | + 0 => array( |
|
| 1987 | + 'EXM_type' => $this->get_this_model_name(), |
|
| 1988 | + 'OBJ_ID' => array( |
|
| 1989 | + 'IN', |
|
| 1990 | + $ids_for_removal |
|
| 1991 | + ) |
|
| 1992 | + ) |
|
| 1993 | + )); |
|
| 1994 | + } |
|
| 1995 | + } |
|
| 1996 | + |
|
| 1997 | + /** |
|
| 1998 | + * Action called just after performing a real deletion query. Although at this point the |
|
| 1999 | + * items should have been deleted |
|
| 2000 | + * |
|
| 2001 | + * @param EEM_Base $model |
|
| 2002 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2003 | + * @param int $rows_deleted |
|
| 2004 | + */ |
|
| 2005 | + do_action('AHEE__EEM_Base__delete__end', $this, $query_params, $rows_deleted, $columns_and_ids_for_deleting); |
|
| 2006 | + return $rows_deleted;// how many supposedly got deleted |
|
| 2007 | + } |
|
| 2008 | + |
|
| 2009 | + |
|
| 2010 | + |
|
| 2011 | + /** |
|
| 2012 | + * Checks all the relations that throw error messages when there are blocking related objects |
|
| 2013 | + * for related model objects. If there are any related model objects on those relations, |
|
| 2014 | + * adds an EE_Error, and return true |
|
| 2015 | + * |
|
| 2016 | + * @param EE_Base_Class|int $this_model_obj_or_id |
|
| 2017 | + * @param EE_Base_Class $ignore_this_model_obj a model object like 'EE_Event', or 'EE_Term_Taxonomy', which |
|
| 2018 | + * should be ignored when determining whether there are related |
|
| 2019 | + * model objects which block this model object's deletion. Useful |
|
| 2020 | + * if you know A is related to B and are considering deleting A, |
|
| 2021 | + * but want to see if A has any other objects blocking its deletion |
|
| 2022 | + * before removing the relation between A and B |
|
| 2023 | + * @return boolean |
|
| 2024 | + * @throws EE_Error |
|
| 2025 | + */ |
|
| 2026 | + public function delete_is_blocked_by_related_models($this_model_obj_or_id, $ignore_this_model_obj = null) |
|
| 2027 | + { |
|
| 2028 | + // first, if $ignore_this_model_obj was supplied, get its model |
|
| 2029 | + if ($ignore_this_model_obj && $ignore_this_model_obj instanceof EE_Base_Class) { |
|
| 2030 | + $ignored_model = $ignore_this_model_obj->get_model(); |
|
| 2031 | + } else { |
|
| 2032 | + $ignored_model = null; |
|
| 2033 | + } |
|
| 2034 | + // now check all the relations of $this_model_obj_or_id and see if there |
|
| 2035 | + // are any related model objects blocking it? |
|
| 2036 | + $is_blocked = false; |
|
| 2037 | + foreach ($this->_model_relations as $relation_name => $relation_obj) { |
|
| 2038 | + if ($relation_obj->block_delete_if_related_models_exist()) { |
|
| 2039 | + // if $ignore_this_model_obj was supplied, then for the query |
|
| 2040 | + // on that model needs to be told to ignore $ignore_this_model_obj |
|
| 2041 | + if ($ignored_model && $relation_name === $ignored_model->get_this_model_name()) { |
|
| 2042 | + $related_model_objects = $relation_obj->get_all_related($this_model_obj_or_id, array( |
|
| 2043 | + array( |
|
| 2044 | + $ignored_model->get_primary_key_field()->get_name() => array( |
|
| 2045 | + '!=', |
|
| 2046 | + $ignore_this_model_obj->ID(), |
|
| 2047 | + ), |
|
| 2048 | + ), |
|
| 2049 | + )); |
|
| 2050 | + } else { |
|
| 2051 | + $related_model_objects = $relation_obj->get_all_related($this_model_obj_or_id); |
|
| 2052 | + } |
|
| 2053 | + if ($related_model_objects) { |
|
| 2054 | + EE_Error::add_error($relation_obj->get_deletion_error_message(), __FILE__, __FUNCTION__, __LINE__); |
|
| 2055 | + $is_blocked = true; |
|
| 2056 | + } |
|
| 2057 | + } |
|
| 2058 | + } |
|
| 2059 | + return $is_blocked; |
|
| 2060 | + } |
|
| 2061 | + |
|
| 2062 | + |
|
| 2063 | + /** |
|
| 2064 | + * Builds the columns and values for items to delete from the incoming $row_results_for_deleting array. |
|
| 2065 | + * @param array $row_results_for_deleting |
|
| 2066 | + * @param bool $allow_blocking |
|
| 2067 | + * @return array The shape of this array depends on whether the model `has_primary_key_field` or not. If the |
|
| 2068 | + * model DOES have a primary_key_field, then the array will be a simple single dimension array where |
|
| 2069 | + * the key is the fully qualified primary key column and the value is an array of ids that will be |
|
| 2070 | + * deleted. Example: |
|
| 2071 | + * array('Event.EVT_ID' => array( 1,2,3)) |
|
| 2072 | + * If the model DOES NOT have a primary_key_field, then the array will be a two dimensional array |
|
| 2073 | + * where each element is a group of columns and values that get deleted. Example: |
|
| 2074 | + * array( |
|
| 2075 | + * 0 => array( |
|
| 2076 | + * 'Term_Relationship.object_id' => 1 |
|
| 2077 | + * 'Term_Relationship.term_taxonomy_id' => 5 |
|
| 2078 | + * ), |
|
| 2079 | + * 1 => array( |
|
| 2080 | + * 'Term_Relationship.object_id' => 1 |
|
| 2081 | + * 'Term_Relationship.term_taxonomy_id' => 6 |
|
| 2082 | + * ) |
|
| 2083 | + * ) |
|
| 2084 | + * @throws EE_Error |
|
| 2085 | + */ |
|
| 2086 | + protected function _get_ids_for_delete(array $row_results_for_deleting, $allow_blocking = true) |
|
| 2087 | + { |
|
| 2088 | + $ids_to_delete_indexed_by_column = array(); |
|
| 2089 | + if ($this->has_primary_key_field()) { |
|
| 2090 | + $primary_table = $this->_get_main_table(); |
|
| 2091 | + $primary_table_pk_field = $this->get_field_by_column($primary_table->get_fully_qualified_pk_column()); |
|
| 2092 | + $other_tables = $this->_get_other_tables(); |
|
| 2093 | + $ids_to_delete_indexed_by_column = $query = array(); |
|
| 2094 | + foreach ($row_results_for_deleting as $item_to_delete) { |
|
| 2095 | + // before we mark this item for deletion, |
|
| 2096 | + // make sure there's no related entities blocking its deletion (if we're checking) |
|
| 2097 | + if ($allow_blocking |
|
| 2098 | + && $this->delete_is_blocked_by_related_models( |
|
| 2099 | + $item_to_delete[ $primary_table->get_fully_qualified_pk_column() ] |
|
| 2100 | + ) |
|
| 2101 | + ) { |
|
| 2102 | + continue; |
|
| 2103 | + } |
|
| 2104 | + // primary table deletes |
|
| 2105 | + if (isset($item_to_delete[ $primary_table->get_fully_qualified_pk_column() ])) { |
|
| 2106 | + $ids_to_delete_indexed_by_column[ $primary_table->get_fully_qualified_pk_column() ][] = |
|
| 2107 | + $item_to_delete[ $primary_table->get_fully_qualified_pk_column() ]; |
|
| 2108 | + } |
|
| 2109 | + } |
|
| 2110 | + } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
|
| 2111 | + $fields = $this->get_combined_primary_key_fields(); |
|
| 2112 | + foreach ($row_results_for_deleting as $item_to_delete) { |
|
| 2113 | + $ids_to_delete_indexed_by_column_for_row = array(); |
|
| 2114 | + foreach ($fields as $cpk_field) { |
|
| 2115 | + if ($cpk_field instanceof EE_Model_Field_Base) { |
|
| 2116 | + $ids_to_delete_indexed_by_column_for_row[ $cpk_field->get_qualified_column() ] = |
|
| 2117 | + $item_to_delete[ $cpk_field->get_qualified_column() ]; |
|
| 2118 | + } |
|
| 2119 | + } |
|
| 2120 | + $ids_to_delete_indexed_by_column[] = $ids_to_delete_indexed_by_column_for_row; |
|
| 2121 | + } |
|
| 2122 | + } else { |
|
| 2123 | + // so there's no primary key and no combined key... |
|
| 2124 | + // sorry, can't help you |
|
| 2125 | + throw new EE_Error( |
|
| 2126 | + sprintf( |
|
| 2127 | + __( |
|
| 2128 | + "Cannot delete objects of type %s because there is no primary key NOR combined key", |
|
| 2129 | + "event_espresso" |
|
| 2130 | + ), |
|
| 2131 | + get_class($this) |
|
| 2132 | + ) |
|
| 2133 | + ); |
|
| 2134 | + } |
|
| 2135 | + return $ids_to_delete_indexed_by_column; |
|
| 2136 | + } |
|
| 2137 | + |
|
| 2138 | + |
|
| 2139 | + /** |
|
| 2140 | + * This receives an array of columns and values set to be deleted (as prepared by _get_ids_for_delete) and prepares |
|
| 2141 | + * the corresponding query_part for the query performing the delete. |
|
| 2142 | + * |
|
| 2143 | + * @param array $ids_to_delete_indexed_by_column @see _get_ids_for_delete for how this array might be shaped. |
|
| 2144 | + * @return string |
|
| 2145 | + * @throws EE_Error |
|
| 2146 | + */ |
|
| 2147 | + protected function _build_query_part_for_deleting_from_columns_and_values(array $ids_to_delete_indexed_by_column) |
|
| 2148 | + { |
|
| 2149 | + $query_part = ''; |
|
| 2150 | + if (empty($ids_to_delete_indexed_by_column)) { |
|
| 2151 | + return $query_part; |
|
| 2152 | + } elseif ($this->has_primary_key_field()) { |
|
| 2153 | + $query = array(); |
|
| 2154 | + foreach ($ids_to_delete_indexed_by_column as $column => $ids) { |
|
| 2155 | + // make sure we have unique $ids |
|
| 2156 | + $ids = array_unique($ids); |
|
| 2157 | + $query[] = $column . ' IN(' . implode(',', $ids) . ')'; |
|
| 2158 | + } |
|
| 2159 | + $query_part = ! empty($query) ? implode(' AND ', $query) : $query_part; |
|
| 2160 | + } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
|
| 2161 | + $ways_to_identify_a_row = array(); |
|
| 2162 | + foreach ($ids_to_delete_indexed_by_column as $ids_to_delete_indexed_by_column_for_each_row) { |
|
| 2163 | + $values_for_each_combined_primary_key_for_a_row = array(); |
|
| 2164 | + foreach ($ids_to_delete_indexed_by_column_for_each_row as $column => $id) { |
|
| 2165 | + $values_for_each_combined_primary_key_for_a_row[] = $column . '=' . $id; |
|
| 2166 | + } |
|
| 2167 | + $ways_to_identify_a_row[] = '(' |
|
| 2168 | + . implode(' AND ', $values_for_each_combined_primary_key_for_a_row) |
|
| 2169 | + . ')'; |
|
| 2170 | + } |
|
| 2171 | + $query_part = implode(' OR ', $ways_to_identify_a_row); |
|
| 2172 | + } |
|
| 2173 | + return $query_part; |
|
| 2174 | + } |
|
| 2175 | + |
|
| 2176 | + |
|
| 2177 | + |
|
| 2178 | + /** |
|
| 2179 | + * Gets the model field by the fully qualified name |
|
| 2180 | + * @param string $qualified_column_name eg 'Event_CPT.post_name' or $field_obj->get_qualified_column() |
|
| 2181 | + * @return EE_Model_Field_Base |
|
| 2182 | + */ |
|
| 2183 | + public function get_field_by_column($qualified_column_name) |
|
| 2184 | + { |
|
| 2185 | + foreach ($this->field_settings(true) as $field_name => $field_obj) { |
|
| 2186 | + if ($field_obj->get_qualified_column() === $qualified_column_name) { |
|
| 2187 | + return $field_obj; |
|
| 2188 | + } |
|
| 2189 | + } |
|
| 2190 | + throw new EE_Error( |
|
| 2191 | + sprintf( |
|
| 2192 | + esc_html__('Could not find a field on the model "%1$s" for qualified column "%2$s"', 'event_espresso'), |
|
| 2193 | + $this->get_this_model_name(), |
|
| 2194 | + $qualified_column_name |
|
| 2195 | + ) |
|
| 2196 | + ); |
|
| 2197 | + } |
|
| 2198 | + |
|
| 2199 | + |
|
| 2200 | + |
|
| 2201 | + /** |
|
| 2202 | + * Count all the rows that match criteria the model query params. |
|
| 2203 | + * If $field_to_count isn't provided, the model's primary key is used. Otherwise, we count by field_to_count's |
|
| 2204 | + * column |
|
| 2205 | + * |
|
| 2206 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2207 | + * @param string $field_to_count field on model to count by (not column name) |
|
| 2208 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2209 | + * that by the setting $distinct to TRUE; |
|
| 2210 | + * @return int |
|
| 2211 | + * @throws EE_Error |
|
| 2212 | + */ |
|
| 2213 | + public function count($query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2214 | + { |
|
| 2215 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 2216 | + if ($field_to_count) { |
|
| 2217 | + $field_obj = $this->field_settings_for($field_to_count); |
|
| 2218 | + $column_to_count = $field_obj->get_qualified_column(); |
|
| 2219 | + } elseif ($this->has_primary_key_field()) { |
|
| 2220 | + $pk_field_obj = $this->get_primary_key_field(); |
|
| 2221 | + $column_to_count = $pk_field_obj->get_qualified_column(); |
|
| 2222 | + } else { |
|
| 2223 | + // there's no primary key |
|
| 2224 | + // if we're counting distinct items, and there's no primary key, |
|
| 2225 | + // we need to list out the columns for distinction; |
|
| 2226 | + // otherwise we can just use star |
|
| 2227 | + if ($distinct) { |
|
| 2228 | + $columns_to_use = array(); |
|
| 2229 | + foreach ($this->get_combined_primary_key_fields() as $field_obj) { |
|
| 2230 | + $columns_to_use[] = $field_obj->get_qualified_column(); |
|
| 2231 | + } |
|
| 2232 | + $column_to_count = implode(',', $columns_to_use); |
|
| 2233 | + } else { |
|
| 2234 | + $column_to_count = '*'; |
|
| 2235 | + } |
|
| 2236 | + } |
|
| 2237 | + $column_to_count = $distinct ? "DISTINCT " . $column_to_count : $column_to_count; |
|
| 2238 | + $SQL = "SELECT COUNT(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2239 | + return (int) $this->_do_wpdb_query('get_var', array($SQL)); |
|
| 2240 | + } |
|
| 2241 | + |
|
| 2242 | + |
|
| 2243 | + |
|
| 2244 | + /** |
|
| 2245 | + * Sums up the value of the $field_to_sum (defaults to the primary key, which isn't terribly useful) |
|
| 2246 | + * |
|
| 2247 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2248 | + * @param string $field_to_sum name of field (array key in $_fields array) |
|
| 2249 | + * @return float |
|
| 2250 | + * @throws EE_Error |
|
| 2251 | + */ |
|
| 2252 | + public function sum($query_params, $field_to_sum = null) |
|
| 2253 | + { |
|
| 2254 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 2255 | + if ($field_to_sum) { |
|
| 2256 | + $field_obj = $this->field_settings_for($field_to_sum); |
|
| 2257 | + } else { |
|
| 2258 | + $field_obj = $this->get_primary_key_field(); |
|
| 2259 | + } |
|
| 2260 | + $column_to_count = $field_obj->get_qualified_column(); |
|
| 2261 | + $SQL = "SELECT SUM(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2262 | + $return_value = $this->_do_wpdb_query('get_var', array($SQL)); |
|
| 2263 | + $data_type = $field_obj->get_wpdb_data_type(); |
|
| 2264 | + if ($data_type === '%d' || $data_type === '%s') { |
|
| 2265 | + return (float) $return_value; |
|
| 2266 | + } |
|
| 2267 | + // must be %f |
|
| 2268 | + return (float) $return_value; |
|
| 2269 | + } |
|
| 2270 | + |
|
| 2271 | + |
|
| 2272 | + |
|
| 2273 | + /** |
|
| 2274 | + * Just calls the specified method on $wpdb with the given arguments |
|
| 2275 | + * Consolidates a little extra error handling code |
|
| 2276 | + * |
|
| 2277 | + * @param string $wpdb_method |
|
| 2278 | + * @param array $arguments_to_provide |
|
| 2279 | + * @throws EE_Error |
|
| 2280 | + * @global wpdb $wpdb |
|
| 2281 | + * @return mixed |
|
| 2282 | + */ |
|
| 2283 | + protected function _do_wpdb_query($wpdb_method, $arguments_to_provide) |
|
| 2284 | + { |
|
| 2285 | + // if we're in maintenance mode level 2, DON'T run any queries |
|
| 2286 | + // because level 2 indicates the database needs updating and |
|
| 2287 | + // is probably out of sync with the code |
|
| 2288 | + if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 2289 | + throw new EE_Error(sprintf(__( |
|
| 2290 | + "Event Espresso Level 2 Maintenance mode is active. That means EE can not run ANY database queries until the necessary migration scripts have run which will take EE out of maintenance mode level 2. Please inform support of this error.", |
|
| 2291 | + "event_espresso" |
|
| 2292 | + ))); |
|
| 2293 | + } |
|
| 2294 | + /** @type WPDB $wpdb */ |
|
| 2295 | + global $wpdb; |
|
| 2296 | + if (! method_exists($wpdb, $wpdb_method)) { |
|
| 2297 | + throw new EE_Error(sprintf(__( |
|
| 2298 | + 'There is no method named "%s" on Wordpress\' $wpdb object', |
|
| 2299 | + 'event_espresso' |
|
| 2300 | + ), $wpdb_method)); |
|
| 2301 | + } |
|
| 2302 | + if (WP_DEBUG) { |
|
| 2303 | + $old_show_errors_value = $wpdb->show_errors; |
|
| 2304 | + $wpdb->show_errors(false); |
|
| 2305 | + } |
|
| 2306 | + $result = $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
| 2307 | + $this->show_db_query_if_previously_requested($wpdb->last_query); |
|
| 2308 | + if (WP_DEBUG) { |
|
| 2309 | + $wpdb->show_errors($old_show_errors_value); |
|
| 2310 | + if (! empty($wpdb->last_error)) { |
|
| 2311 | + throw new EE_Error(sprintf(__('WPDB Error: "%s"', 'event_espresso'), $wpdb->last_error)); |
|
| 2312 | + } |
|
| 2313 | + if ($result === false) { |
|
| 2314 | + throw new EE_Error(sprintf(__( |
|
| 2315 | + 'WPDB Error occurred, but no error message was logged by wpdb! The wpdb method called was "%1$s" and the arguments were "%2$s"', |
|
| 2316 | + 'event_espresso' |
|
| 2317 | + ), $wpdb_method, var_export($arguments_to_provide, true))); |
|
| 2318 | + } |
|
| 2319 | + } elseif ($result === false) { |
|
| 2320 | + EE_Error::add_error( |
|
| 2321 | + sprintf( |
|
| 2322 | + __( |
|
| 2323 | + 'A database error has occurred. Turn on WP_DEBUG for more information.||A database error occurred doing wpdb method "%1$s", with arguments "%2$s". The error was "%3$s"', |
|
| 2324 | + 'event_espresso' |
|
| 2325 | + ), |
|
| 2326 | + $wpdb_method, |
|
| 2327 | + var_export($arguments_to_provide, true), |
|
| 2328 | + $wpdb->last_error |
|
| 2329 | + ), |
|
| 2330 | + __FILE__, |
|
| 2331 | + __FUNCTION__, |
|
| 2332 | + __LINE__ |
|
| 2333 | + ); |
|
| 2334 | + } |
|
| 2335 | + return $result; |
|
| 2336 | + } |
|
| 2337 | + |
|
| 2338 | + |
|
| 2339 | + |
|
| 2340 | + /** |
|
| 2341 | + * Attempts to run the indicated WPDB method with the provided arguments, |
|
| 2342 | + * and if there's an error tries to verify the DB is correct. Uses |
|
| 2343 | + * the static property EEM_Base::$_db_verification_level to determine whether |
|
| 2344 | + * we should try to fix the EE core db, the addons, or just give up |
|
| 2345 | + * |
|
| 2346 | + * @param string $wpdb_method |
|
| 2347 | + * @param array $arguments_to_provide |
|
| 2348 | + * @return mixed |
|
| 2349 | + */ |
|
| 2350 | + private function _process_wpdb_query($wpdb_method, $arguments_to_provide) |
|
| 2351 | + { |
|
| 2352 | + /** @type WPDB $wpdb */ |
|
| 2353 | + global $wpdb; |
|
| 2354 | + $wpdb->last_error = null; |
|
| 2355 | + $result = call_user_func_array(array($wpdb, $wpdb_method), $arguments_to_provide); |
|
| 2356 | + // was there an error running the query? but we don't care on new activations |
|
| 2357 | + // (we're going to setup the DB anyway on new activations) |
|
| 2358 | + if (($result === false || ! empty($wpdb->last_error)) |
|
| 2359 | + && EE_System::instance()->detect_req_type() !== EE_System::req_type_new_activation |
|
| 2360 | + ) { |
|
| 2361 | + switch (EEM_Base::$_db_verification_level) { |
|
| 2362 | + case EEM_Base::db_verified_none: |
|
| 2363 | + // let's double-check core's DB |
|
| 2364 | + $error_message = $this->_verify_core_db($wpdb_method, $arguments_to_provide); |
|
| 2365 | + break; |
|
| 2366 | + case EEM_Base::db_verified_core: |
|
| 2367 | + // STILL NO LOVE?? verify all the addons too. Maybe they need to be fixed |
|
| 2368 | + $error_message = $this->_verify_addons_db($wpdb_method, $arguments_to_provide); |
|
| 2369 | + break; |
|
| 2370 | + case EEM_Base::db_verified_addons: |
|
| 2371 | + // ummmm... you in trouble |
|
| 2372 | + return $result; |
|
| 2373 | + break; |
|
| 2374 | + } |
|
| 2375 | + if (! empty($error_message)) { |
|
| 2376 | + EE_Log::instance()->log(__FILE__, __FUNCTION__, $error_message, 'error'); |
|
| 2377 | + trigger_error($error_message); |
|
| 2378 | + } |
|
| 2379 | + return $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
| 2380 | + } |
|
| 2381 | + return $result; |
|
| 2382 | + } |
|
| 2383 | + |
|
| 2384 | + |
|
| 2385 | + |
|
| 2386 | + /** |
|
| 2387 | + * Verifies the EE core database is up-to-date and records that we've done it on |
|
| 2388 | + * EEM_Base::$_db_verification_level |
|
| 2389 | + * |
|
| 2390 | + * @param string $wpdb_method |
|
| 2391 | + * @param array $arguments_to_provide |
|
| 2392 | + * @return string |
|
| 2393 | + */ |
|
| 2394 | + private function _verify_core_db($wpdb_method, $arguments_to_provide) |
|
| 2395 | + { |
|
| 2396 | + /** @type WPDB $wpdb */ |
|
| 2397 | + global $wpdb; |
|
| 2398 | + // ok remember that we've already attempted fixing the core db, in case the problem persists |
|
| 2399 | + EEM_Base::$_db_verification_level = EEM_Base::db_verified_core; |
|
| 2400 | + $error_message = sprintf( |
|
| 2401 | + __( |
|
| 2402 | + 'WPDB Error "%1$s" while running wpdb method "%2$s" with arguments %3$s. Automatically attempting to fix EE Core DB', |
|
| 2403 | + 'event_espresso' |
|
| 2404 | + ), |
|
| 2405 | + $wpdb->last_error, |
|
| 2406 | + $wpdb_method, |
|
| 2407 | + wp_json_encode($arguments_to_provide) |
|
| 2408 | + ); |
|
| 2409 | + EE_System::instance()->initialize_db_if_no_migrations_required(false, true); |
|
| 2410 | + return $error_message; |
|
| 2411 | + } |
|
| 2412 | + |
|
| 2413 | + |
|
| 2414 | + |
|
| 2415 | + /** |
|
| 2416 | + * Verifies the EE addons' database is up-to-date and records that we've done it on |
|
| 2417 | + * EEM_Base::$_db_verification_level |
|
| 2418 | + * |
|
| 2419 | + * @param $wpdb_method |
|
| 2420 | + * @param $arguments_to_provide |
|
| 2421 | + * @return string |
|
| 2422 | + */ |
|
| 2423 | + private function _verify_addons_db($wpdb_method, $arguments_to_provide) |
|
| 2424 | + { |
|
| 2425 | + /** @type WPDB $wpdb */ |
|
| 2426 | + global $wpdb; |
|
| 2427 | + // ok remember that we've already attempted fixing the addons dbs, in case the problem persists |
|
| 2428 | + EEM_Base::$_db_verification_level = EEM_Base::db_verified_addons; |
|
| 2429 | + $error_message = sprintf( |
|
| 2430 | + __( |
|
| 2431 | + 'WPDB AGAIN: Error "%1$s" while running the same method and arguments as before. Automatically attempting to fix EE Addons DB', |
|
| 2432 | + 'event_espresso' |
|
| 2433 | + ), |
|
| 2434 | + $wpdb->last_error, |
|
| 2435 | + $wpdb_method, |
|
| 2436 | + wp_json_encode($arguments_to_provide) |
|
| 2437 | + ); |
|
| 2438 | + EE_System::instance()->initialize_addons(); |
|
| 2439 | + return $error_message; |
|
| 2440 | + } |
|
| 2441 | + |
|
| 2442 | + |
|
| 2443 | + |
|
| 2444 | + /** |
|
| 2445 | + * In order to avoid repeating this code for the get_all, sum, and count functions, put the code parts |
|
| 2446 | + * that are identical in here. Returns a string of SQL of everything in a SELECT query except the beginning |
|
| 2447 | + * SELECT clause, eg " FROM wp_posts AS Event INNER JOIN ... WHERE ... ORDER BY ... LIMIT ... GROUP BY ... HAVING |
|
| 2448 | + * ..." |
|
| 2449 | + * |
|
| 2450 | + * @param EE_Model_Query_Info_Carrier $model_query_info |
|
| 2451 | + * @return string |
|
| 2452 | + */ |
|
| 2453 | + private function _construct_2nd_half_of_select_query(EE_Model_Query_Info_Carrier $model_query_info) |
|
| 2454 | + { |
|
| 2455 | + return " FROM " . $model_query_info->get_full_join_sql() . |
|
| 2456 | + $model_query_info->get_where_sql() . |
|
| 2457 | + $model_query_info->get_group_by_sql() . |
|
| 2458 | + $model_query_info->get_having_sql() . |
|
| 2459 | + $model_query_info->get_order_by_sql() . |
|
| 2460 | + $model_query_info->get_limit_sql(); |
|
| 2461 | + } |
|
| 2462 | + |
|
| 2463 | + |
|
| 2464 | + |
|
| 2465 | + /** |
|
| 2466 | + * Set to easily debug the next X queries ran from this model. |
|
| 2467 | + * |
|
| 2468 | + * @param int $count |
|
| 2469 | + */ |
|
| 2470 | + public function show_next_x_db_queries($count = 1) |
|
| 2471 | + { |
|
| 2472 | + $this->_show_next_x_db_queries = $count; |
|
| 2473 | + } |
|
| 2474 | + |
|
| 2475 | + |
|
| 2476 | + |
|
| 2477 | + /** |
|
| 2478 | + * @param $sql_query |
|
| 2479 | + */ |
|
| 2480 | + public function show_db_query_if_previously_requested($sql_query) |
|
| 2481 | + { |
|
| 2482 | + if ($this->_show_next_x_db_queries > 0) { |
|
| 2483 | + echo $sql_query; |
|
| 2484 | + $this->_show_next_x_db_queries--; |
|
| 2485 | + } |
|
| 2486 | + } |
|
| 2487 | + |
|
| 2488 | + |
|
| 2489 | + |
|
| 2490 | + /** |
|
| 2491 | + * Adds a relationship of the correct type between $modelObject and $otherModelObject. |
|
| 2492 | + * There are the 3 cases: |
|
| 2493 | + * 'belongsTo' relationship: sets $id_or_obj's foreign_key to be $other_model_id_or_obj's primary_key. If |
|
| 2494 | + * $otherModelObject has no ID, it is first saved. |
|
| 2495 | + * 'hasMany' relationship: sets $other_model_id_or_obj's foreign_key to be $id_or_obj's primary_key. If $id_or_obj |
|
| 2496 | + * has no ID, it is first saved. |
|
| 2497 | + * 'hasAndBelongsToMany' relationships: checks that there isn't already an entry in the join table, and adds one. |
|
| 2498 | + * If one of the model Objects has not yet been saved to the database, it is saved before adding the entry in the |
|
| 2499 | + * join table |
|
| 2500 | + * |
|
| 2501 | + * @param EE_Base_Class /int $thisModelObject |
|
| 2502 | + * @param EE_Base_Class /int $id_or_obj EE_base_Class or ID of other Model Object |
|
| 2503 | + * @param string $relationName , key in EEM_Base::_relations |
|
| 2504 | + * an attendee to a group, you also want to specify which role they |
|
| 2505 | + * will have in that group. So you would use this parameter to |
|
| 2506 | + * specify array('role-column-name'=>'role-id') |
|
| 2507 | + * @param array $extra_join_model_fields_n_values This allows you to enter further query params for the relation |
|
| 2508 | + * to for relation to methods that allow you to further specify |
|
| 2509 | + * extra columns to join by (such as HABTM). Keep in mind that the |
|
| 2510 | + * only acceptable query_params is strict "col" => "value" pairs |
|
| 2511 | + * because these will be inserted in any new rows created as well. |
|
| 2512 | + * @return EE_Base_Class which was added as a relation. Object referred to by $other_model_id_or_obj |
|
| 2513 | + * @throws EE_Error |
|
| 2514 | + */ |
|
| 2515 | + public function add_relationship_to( |
|
| 2516 | + $id_or_obj, |
|
| 2517 | + $other_model_id_or_obj, |
|
| 2518 | + $relationName, |
|
| 2519 | + $extra_join_model_fields_n_values = array() |
|
| 2520 | + ) { |
|
| 2521 | + $relation_obj = $this->related_settings_for($relationName); |
|
| 2522 | + return $relation_obj->add_relation_to($id_or_obj, $other_model_id_or_obj, $extra_join_model_fields_n_values); |
|
| 2523 | + } |
|
| 2524 | + |
|
| 2525 | + |
|
| 2526 | + |
|
| 2527 | + /** |
|
| 2528 | + * Removes a relationship of the correct type between $modelObject and $otherModelObject. |
|
| 2529 | + * There are the 3 cases: |
|
| 2530 | + * 'belongsTo' relationship: sets $modelObject's foreign_key to null, if that field is nullable.Otherwise throws an |
|
| 2531 | + * error |
|
| 2532 | + * 'hasMany' relationship: sets $otherModelObject's foreign_key to null,if that field is nullable.Otherwise throws |
|
| 2533 | + * an error |
|
| 2534 | + * 'hasAndBelongsToMany' relationships:removes any existing entry in the join table between the two models. |
|
| 2535 | + * |
|
| 2536 | + * @param EE_Base_Class /int $id_or_obj |
|
| 2537 | + * @param EE_Base_Class /int $other_model_id_or_obj EE_Base_Class or ID of other Model Object |
|
| 2538 | + * @param string $relationName key in EEM_Base::_relations |
|
| 2539 | + * @return boolean of success |
|
| 2540 | + * @throws EE_Error |
|
| 2541 | + * @param array $where_query This allows you to enter further query params for the relation to for relation to |
|
| 2542 | + * methods that allow you to further specify extra columns to join by (such as HABTM). |
|
| 2543 | + * Keep in mind that the only acceptable query_params is strict "col" => "value" pairs |
|
| 2544 | + * because these will be inserted in any new rows created as well. |
|
| 2545 | + */ |
|
| 2546 | + public function remove_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query = array()) |
|
| 2547 | + { |
|
| 2548 | + $relation_obj = $this->related_settings_for($relationName); |
|
| 2549 | + return $relation_obj->remove_relation_to($id_or_obj, $other_model_id_or_obj, $where_query); |
|
| 2550 | + } |
|
| 2551 | + |
|
| 2552 | + |
|
| 2553 | + |
|
| 2554 | + /** |
|
| 2555 | + * @param mixed $id_or_obj |
|
| 2556 | + * @param string $relationName |
|
| 2557 | + * @param array $where_query_params |
|
| 2558 | + * @param EE_Base_Class[] objects to which relations were removed |
|
| 2559 | + * @return \EE_Base_Class[] |
|
| 2560 | + * @throws EE_Error |
|
| 2561 | + */ |
|
| 2562 | + public function remove_relations($id_or_obj, $relationName, $where_query_params = array()) |
|
| 2563 | + { |
|
| 2564 | + $relation_obj = $this->related_settings_for($relationName); |
|
| 2565 | + return $relation_obj->remove_relations($id_or_obj, $where_query_params); |
|
| 2566 | + } |
|
| 2567 | + |
|
| 2568 | + |
|
| 2569 | + |
|
| 2570 | + /** |
|
| 2571 | + * Gets all the related items of the specified $model_name, using $query_params. |
|
| 2572 | + * Note: by default, we remove the "default query params" |
|
| 2573 | + * because we want to get even deleted items etc. |
|
| 2574 | + * |
|
| 2575 | + * @param mixed $id_or_obj EE_Base_Class child or its ID |
|
| 2576 | + * @param string $model_name like 'Event', 'Registration', etc. always singular |
|
| 2577 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2578 | + * @return EE_Base_Class[] |
|
| 2579 | + * @throws EE_Error |
|
| 2580 | + */ |
|
| 2581 | + public function get_all_related($id_or_obj, $model_name, $query_params = null) |
|
| 2582 | + { |
|
| 2583 | + $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2584 | + $relation_settings = $this->related_settings_for($model_name); |
|
| 2585 | + return $relation_settings->get_all_related($model_obj, $query_params); |
|
| 2586 | + } |
|
| 2587 | + |
|
| 2588 | + |
|
| 2589 | + |
|
| 2590 | + /** |
|
| 2591 | + * Deletes all the model objects across the relation indicated by $model_name |
|
| 2592 | + * which are related to $id_or_obj which meet the criteria set in $query_params. |
|
| 2593 | + * However, if the model objects can't be deleted because of blocking related model objects, then |
|
| 2594 | + * they aren't deleted. (Unless the thing that would have been deleted can be soft-deleted, that still happens). |
|
| 2595 | + * |
|
| 2596 | + * @param EE_Base_Class|int|string $id_or_obj |
|
| 2597 | + * @param string $model_name |
|
| 2598 | + * @param array $query_params |
|
| 2599 | + * @return int how many deleted |
|
| 2600 | + * @throws EE_Error |
|
| 2601 | + */ |
|
| 2602 | + public function delete_related($id_or_obj, $model_name, $query_params = array()) |
|
| 2603 | + { |
|
| 2604 | + $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2605 | + $relation_settings = $this->related_settings_for($model_name); |
|
| 2606 | + return $relation_settings->delete_all_related($model_obj, $query_params); |
|
| 2607 | + } |
|
| 2608 | + |
|
| 2609 | + |
|
| 2610 | + |
|
| 2611 | + /** |
|
| 2612 | + * Hard deletes all the model objects across the relation indicated by $model_name |
|
| 2613 | + * which are related to $id_or_obj which meet the criteria set in $query_params. If |
|
| 2614 | + * the model objects can't be hard deleted because of blocking related model objects, |
|
| 2615 | + * just does a soft-delete on them instead. |
|
| 2616 | + * |
|
| 2617 | + * @param EE_Base_Class|int|string $id_or_obj |
|
| 2618 | + * @param string $model_name |
|
| 2619 | + * @param array $query_params |
|
| 2620 | + * @return int how many deleted |
|
| 2621 | + * @throws EE_Error |
|
| 2622 | + */ |
|
| 2623 | + public function delete_related_permanently($id_or_obj, $model_name, $query_params = array()) |
|
| 2624 | + { |
|
| 2625 | + $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2626 | + $relation_settings = $this->related_settings_for($model_name); |
|
| 2627 | + return $relation_settings->delete_related_permanently($model_obj, $query_params); |
|
| 2628 | + } |
|
| 2629 | + |
|
| 2630 | + |
|
| 2631 | + |
|
| 2632 | + /** |
|
| 2633 | + * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2634 | + * unless otherwise specified in the $query_params |
|
| 2635 | + * |
|
| 2636 | + * @param int /EE_Base_Class $id_or_obj |
|
| 2637 | + * @param string $model_name like 'Event', or 'Registration' |
|
| 2638 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2639 | + * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2640 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2641 | + * that by the setting $distinct to TRUE; |
|
| 2642 | + * @return int |
|
| 2643 | + * @throws EE_Error |
|
| 2644 | + */ |
|
| 2645 | + public function count_related( |
|
| 2646 | + $id_or_obj, |
|
| 2647 | + $model_name, |
|
| 2648 | + $query_params = array(), |
|
| 2649 | + $field_to_count = null, |
|
| 2650 | + $distinct = false |
|
| 2651 | + ) { |
|
| 2652 | + $related_model = $this->get_related_model_obj($model_name); |
|
| 2653 | + // we're just going to use the query params on the related model's normal get_all query, |
|
| 2654 | + // except add a condition to say to match the current mod |
|
| 2655 | + if (! isset($query_params['default_where_conditions'])) { |
|
| 2656 | + $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
| 2657 | + } |
|
| 2658 | + $this_model_name = $this->get_this_model_name(); |
|
| 2659 | + $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
| 2660 | + $query_params[0][ $this_model_name . "." . $this_pk_field_name ] = $id_or_obj; |
|
| 2661 | + return $related_model->count($query_params, $field_to_count, $distinct); |
|
| 2662 | + } |
|
| 2663 | + |
|
| 2664 | + |
|
| 2665 | + |
|
| 2666 | + /** |
|
| 2667 | + * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2668 | + * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2669 | + * |
|
| 2670 | + * @param int /EE_Base_Class $id_or_obj |
|
| 2671 | + * @param string $model_name like 'Event', or 'Registration' |
|
| 2672 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2673 | + * @param string $field_to_sum name of field to count by. By default, uses primary key |
|
| 2674 | + * @return float |
|
| 2675 | + * @throws EE_Error |
|
| 2676 | + */ |
|
| 2677 | + public function sum_related($id_or_obj, $model_name, $query_params, $field_to_sum = null) |
|
| 2678 | + { |
|
| 2679 | + $related_model = $this->get_related_model_obj($model_name); |
|
| 2680 | + if (! is_array($query_params)) { |
|
| 2681 | + EE_Error::doing_it_wrong( |
|
| 2682 | + 'EEM_Base::sum_related', |
|
| 2683 | + sprintf( |
|
| 2684 | + __('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 2685 | + gettype($query_params) |
|
| 2686 | + ), |
|
| 2687 | + '4.6.0' |
|
| 2688 | + ); |
|
| 2689 | + $query_params = array(); |
|
| 2690 | + } |
|
| 2691 | + // we're just going to use the query params on the related model's normal get_all query, |
|
| 2692 | + // except add a condition to say to match the current mod |
|
| 2693 | + if (! isset($query_params['default_where_conditions'])) { |
|
| 2694 | + $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
| 2695 | + } |
|
| 2696 | + $this_model_name = $this->get_this_model_name(); |
|
| 2697 | + $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
| 2698 | + $query_params[0][ $this_model_name . "." . $this_pk_field_name ] = $id_or_obj; |
|
| 2699 | + return $related_model->sum($query_params, $field_to_sum); |
|
| 2700 | + } |
|
| 2701 | + |
|
| 2702 | + |
|
| 2703 | + |
|
| 2704 | + /** |
|
| 2705 | + * Uses $this->_relatedModels info to find the first related model object of relation $relationName to the given |
|
| 2706 | + * $modelObject |
|
| 2707 | + * |
|
| 2708 | + * @param int | EE_Base_Class $id_or_obj EE_Base_Class child or its ID |
|
| 2709 | + * @param string $other_model_name , key in $this->_relatedModels, eg 'Registration', or 'Events' |
|
| 2710 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2711 | + * @return EE_Base_Class |
|
| 2712 | + * @throws EE_Error |
|
| 2713 | + */ |
|
| 2714 | + public function get_first_related(EE_Base_Class $id_or_obj, $other_model_name, $query_params) |
|
| 2715 | + { |
|
| 2716 | + $query_params['limit'] = 1; |
|
| 2717 | + $results = $this->get_all_related($id_or_obj, $other_model_name, $query_params); |
|
| 2718 | + if ($results) { |
|
| 2719 | + return array_shift($results); |
|
| 2720 | + } |
|
| 2721 | + return null; |
|
| 2722 | + } |
|
| 2723 | + |
|
| 2724 | + |
|
| 2725 | + |
|
| 2726 | + /** |
|
| 2727 | + * Gets the model's name as it's expected in queries. For example, if this is EEM_Event model, that would be Event |
|
| 2728 | + * |
|
| 2729 | + * @return string |
|
| 2730 | + */ |
|
| 2731 | + public function get_this_model_name() |
|
| 2732 | + { |
|
| 2733 | + return str_replace("EEM_", "", get_class($this)); |
|
| 2734 | + } |
|
| 2735 | + |
|
| 2736 | + |
|
| 2737 | + |
|
| 2738 | + /** |
|
| 2739 | + * Gets the model field on this model which is of type EE_Any_Foreign_Model_Name_Field |
|
| 2740 | + * |
|
| 2741 | + * @return EE_Any_Foreign_Model_Name_Field |
|
| 2742 | + * @throws EE_Error |
|
| 2743 | + */ |
|
| 2744 | + public function get_field_containing_related_model_name() |
|
| 2745 | + { |
|
| 2746 | + foreach ($this->field_settings(true) as $field) { |
|
| 2747 | + if ($field instanceof EE_Any_Foreign_Model_Name_Field) { |
|
| 2748 | + $field_with_model_name = $field; |
|
| 2749 | + } |
|
| 2750 | + } |
|
| 2751 | + if (! isset($field_with_model_name) || ! $field_with_model_name) { |
|
| 2752 | + throw new EE_Error(sprintf( |
|
| 2753 | + __("There is no EE_Any_Foreign_Model_Name field on model %s", "event_espresso"), |
|
| 2754 | + $this->get_this_model_name() |
|
| 2755 | + )); |
|
| 2756 | + } |
|
| 2757 | + return $field_with_model_name; |
|
| 2758 | + } |
|
| 2759 | + |
|
| 2760 | + |
|
| 2761 | + |
|
| 2762 | + /** |
|
| 2763 | + * Inserts a new entry into the database, for each table. |
|
| 2764 | + * Note: does not add the item to the entity map because that is done by EE_Base_Class::save() right after this. |
|
| 2765 | + * If client code uses EEM_Base::insert() directly, then although the item isn't in the entity map, |
|
| 2766 | + * we also know there is no model object with the newly inserted item's ID at the moment (because |
|
| 2767 | + * if there were, then they would already be in the DB and this would fail); and in the future if someone |
|
| 2768 | + * creates a model object with this ID (or grabs it from the DB) then it will be added to the |
|
| 2769 | + * entity map at that time anyways. SO, no need for EEM_Base::insert ot add to the entity map |
|
| 2770 | + * |
|
| 2771 | + * @param array $field_n_values keys are field names, values are their values (in the client code's domain if |
|
| 2772 | + * $values_already_prepared_by_model_object is false, in the model object's domain if |
|
| 2773 | + * $values_already_prepared_by_model_object is true. See comment about this at the top |
|
| 2774 | + * of EEM_Base) |
|
| 2775 | + * @return int|string new primary key on main table that got inserted |
|
| 2776 | + * @throws EE_Error |
|
| 2777 | + */ |
|
| 2778 | + public function insert($field_n_values) |
|
| 2779 | + { |
|
| 2780 | + /** |
|
| 2781 | + * Filters the fields and their values before inserting an item using the models |
|
| 2782 | + * |
|
| 2783 | + * @param array $fields_n_values keys are the fields and values are their new values |
|
| 2784 | + * @param EEM_Base $model the model used |
|
| 2785 | + */ |
|
| 2786 | + $field_n_values = (array) apply_filters('FHEE__EEM_Base__insert__fields_n_values', $field_n_values, $this); |
|
| 2787 | + if ($this->_satisfies_unique_indexes($field_n_values)) { |
|
| 2788 | + $main_table = $this->_get_main_table(); |
|
| 2789 | + $new_id = $this->_insert_into_specific_table($main_table, $field_n_values, false); |
|
| 2790 | + if ($new_id !== false) { |
|
| 2791 | + foreach ($this->_get_other_tables() as $other_table) { |
|
| 2792 | + $this->_insert_into_specific_table($other_table, $field_n_values, $new_id); |
|
| 2793 | + } |
|
| 2794 | + } |
|
| 2795 | + /** |
|
| 2796 | + * Done just after attempting to insert a new model object |
|
| 2797 | + * |
|
| 2798 | + * @param EEM_Base $model used |
|
| 2799 | + * @param array $fields_n_values fields and their values |
|
| 2800 | + * @param int|string the ID of the newly-inserted model object |
|
| 2801 | + */ |
|
| 2802 | + do_action('AHEE__EEM_Base__insert__end', $this, $field_n_values, $new_id); |
|
| 2803 | + return $new_id; |
|
| 2804 | + } |
|
| 2805 | + return false; |
|
| 2806 | + } |
|
| 2807 | + |
|
| 2808 | + |
|
| 2809 | + |
|
| 2810 | + /** |
|
| 2811 | + * Checks that the result would satisfy the unique indexes on this model |
|
| 2812 | + * |
|
| 2813 | + * @param array $field_n_values |
|
| 2814 | + * @param string $action |
|
| 2815 | + * @return boolean |
|
| 2816 | + * @throws EE_Error |
|
| 2817 | + */ |
|
| 2818 | + protected function _satisfies_unique_indexes($field_n_values, $action = 'insert') |
|
| 2819 | + { |
|
| 2820 | + foreach ($this->unique_indexes() as $index_name => $index) { |
|
| 2821 | + $uniqueness_where_params = array_intersect_key($field_n_values, $index->fields()); |
|
| 2822 | + if ($this->exists(array($uniqueness_where_params))) { |
|
| 2823 | + EE_Error::add_error( |
|
| 2824 | + sprintf( |
|
| 2825 | + __( |
|
| 2826 | + "Could not %s %s. %s uniqueness index failed. Fields %s must form a unique set, but an entry already exists with values %s.", |
|
| 2827 | + "event_espresso" |
|
| 2828 | + ), |
|
| 2829 | + $action, |
|
| 2830 | + $this->_get_class_name(), |
|
| 2831 | + $index_name, |
|
| 2832 | + implode(",", $index->field_names()), |
|
| 2833 | + http_build_query($uniqueness_where_params) |
|
| 2834 | + ), |
|
| 2835 | + __FILE__, |
|
| 2836 | + __FUNCTION__, |
|
| 2837 | + __LINE__ |
|
| 2838 | + ); |
|
| 2839 | + return false; |
|
| 2840 | + } |
|
| 2841 | + } |
|
| 2842 | + return true; |
|
| 2843 | + } |
|
| 2844 | + |
|
| 2845 | + |
|
| 2846 | + |
|
| 2847 | + /** |
|
| 2848 | + * Checks the database for an item that conflicts (ie, if this item were |
|
| 2849 | + * saved to the DB would break some uniqueness requirement, like a primary key |
|
| 2850 | + * or an index primary key set) with the item specified. $id_obj_or_fields_array |
|
| 2851 | + * can be either an EE_Base_Class or an array of fields n values |
|
| 2852 | + * |
|
| 2853 | + * @param EE_Base_Class|array $obj_or_fields_array |
|
| 2854 | + * @param boolean $include_primary_key whether to use the model object's primary key |
|
| 2855 | + * when looking for conflicts |
|
| 2856 | + * (ie, if false, we ignore the model object's primary key |
|
| 2857 | + * when finding "conflicts". If true, it's also considered). |
|
| 2858 | + * Only works for INT primary key, |
|
| 2859 | + * STRING primary keys cannot be ignored |
|
| 2860 | + * @throws EE_Error |
|
| 2861 | + * @return EE_Base_Class|array |
|
| 2862 | + */ |
|
| 2863 | + public function get_one_conflicting($obj_or_fields_array, $include_primary_key = true) |
|
| 2864 | + { |
|
| 2865 | + if ($obj_or_fields_array instanceof EE_Base_Class) { |
|
| 2866 | + $fields_n_values = $obj_or_fields_array->model_field_array(); |
|
| 2867 | + } elseif (is_array($obj_or_fields_array)) { |
|
| 2868 | + $fields_n_values = $obj_or_fields_array; |
|
| 2869 | + } else { |
|
| 2870 | + throw new EE_Error( |
|
| 2871 | + sprintf( |
|
| 2872 | + __( |
|
| 2873 | + "%s get_all_conflicting should be called with a model object or an array of field names and values, you provided %d", |
|
| 2874 | + "event_espresso" |
|
| 2875 | + ), |
|
| 2876 | + get_class($this), |
|
| 2877 | + $obj_or_fields_array |
|
| 2878 | + ) |
|
| 2879 | + ); |
|
| 2880 | + } |
|
| 2881 | + $query_params = array(); |
|
| 2882 | + if ($this->has_primary_key_field() |
|
| 2883 | + && ($include_primary_key |
|
| 2884 | + || $this->get_primary_key_field() |
|
| 2885 | + instanceof |
|
| 2886 | + EE_Primary_Key_String_Field) |
|
| 2887 | + && isset($fields_n_values[ $this->primary_key_name() ]) |
|
| 2888 | + ) { |
|
| 2889 | + $query_params[0]['OR'][ $this->primary_key_name() ] = $fields_n_values[ $this->primary_key_name() ]; |
|
| 2890 | + } |
|
| 2891 | + foreach ($this->unique_indexes() as $unique_index_name => $unique_index) { |
|
| 2892 | + $uniqueness_where_params = array_intersect_key($fields_n_values, $unique_index->fields()); |
|
| 2893 | + $query_params[0]['OR'][ 'AND*' . $unique_index_name ] = $uniqueness_where_params; |
|
| 2894 | + } |
|
| 2895 | + // if there is nothing to base this search on, then we shouldn't find anything |
|
| 2896 | + if (empty($query_params)) { |
|
| 2897 | + return array(); |
|
| 2898 | + } |
|
| 2899 | + return $this->get_one($query_params); |
|
| 2900 | + } |
|
| 2901 | + |
|
| 2902 | + |
|
| 2903 | + |
|
| 2904 | + /** |
|
| 2905 | + * Like count, but is optimized and returns a boolean instead of an int |
|
| 2906 | + * |
|
| 2907 | + * @param array $query_params |
|
| 2908 | + * @return boolean |
|
| 2909 | + * @throws EE_Error |
|
| 2910 | + */ |
|
| 2911 | + public function exists($query_params) |
|
| 2912 | + { |
|
| 2913 | + $query_params['limit'] = 1; |
|
| 2914 | + return $this->count($query_params) > 0; |
|
| 2915 | + } |
|
| 2916 | + |
|
| 2917 | + |
|
| 2918 | + |
|
| 2919 | + /** |
|
| 2920 | + * Wrapper for exists, except ignores default query parameters so we're only considering ID |
|
| 2921 | + * |
|
| 2922 | + * @param int|string $id |
|
| 2923 | + * @return boolean |
|
| 2924 | + * @throws EE_Error |
|
| 2925 | + */ |
|
| 2926 | + public function exists_by_ID($id) |
|
| 2927 | + { |
|
| 2928 | + return $this->exists( |
|
| 2929 | + array( |
|
| 2930 | + 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
| 2931 | + array( |
|
| 2932 | + $this->primary_key_name() => $id, |
|
| 2933 | + ), |
|
| 2934 | + ) |
|
| 2935 | + ); |
|
| 2936 | + } |
|
| 2937 | + |
|
| 2938 | + |
|
| 2939 | + |
|
| 2940 | + /** |
|
| 2941 | + * Inserts a new row in $table, using the $cols_n_values which apply to that table. |
|
| 2942 | + * If a $new_id is supplied and if $table is an EE_Other_Table, we assume |
|
| 2943 | + * we need to add a foreign key column to point to $new_id (which should be the primary key's value |
|
| 2944 | + * on the main table) |
|
| 2945 | + * This is protected rather than private because private is not accessible to any child methods and there MAY be |
|
| 2946 | + * cases where we want to call it directly rather than via insert(). |
|
| 2947 | + * |
|
| 2948 | + * @access protected |
|
| 2949 | + * @param EE_Table_Base $table |
|
| 2950 | + * @param array $fields_n_values each key should be in field's keys, and value should be an int, string or |
|
| 2951 | + * float |
|
| 2952 | + * @param int $new_id for now we assume only int keys |
|
| 2953 | + * @throws EE_Error |
|
| 2954 | + * @global WPDB $wpdb only used to get the $wpdb->insert_id after performing an insert |
|
| 2955 | + * @return int ID of new row inserted, or FALSE on failure |
|
| 2956 | + */ |
|
| 2957 | + protected function _insert_into_specific_table(EE_Table_Base $table, $fields_n_values, $new_id = 0) |
|
| 2958 | + { |
|
| 2959 | + global $wpdb; |
|
| 2960 | + $insertion_col_n_values = array(); |
|
| 2961 | + $format_for_insertion = array(); |
|
| 2962 | + $fields_on_table = $this->_get_fields_for_table($table->get_table_alias()); |
|
| 2963 | + foreach ($fields_on_table as $field_name => $field_obj) { |
|
| 2964 | + // check if its an auto-incrementing column, in which case we should just leave it to do its autoincrement thing |
|
| 2965 | + if ($field_obj->is_auto_increment()) { |
|
| 2966 | + continue; |
|
| 2967 | + } |
|
| 2968 | + $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
| 2969 | + // if the value we want to assign it to is NULL, just don't mention it for the insertion |
|
| 2970 | + if ($prepared_value !== null) { |
|
| 2971 | + $insertion_col_n_values[ $field_obj->get_table_column() ] = $prepared_value; |
|
| 2972 | + $format_for_insertion[] = $field_obj->get_wpdb_data_type(); |
|
| 2973 | + } |
|
| 2974 | + } |
|
| 2975 | + if ($table instanceof EE_Secondary_Table && $new_id) { |
|
| 2976 | + // its not the main table, so we should have already saved the main table's PK which we just inserted |
|
| 2977 | + // so add the fk to the main table as a column |
|
| 2978 | + $insertion_col_n_values[ $table->get_fk_on_table() ] = $new_id; |
|
| 2979 | + $format_for_insertion[] = '%d';// yes right now we're only allowing these foreign keys to be INTs |
|
| 2980 | + } |
|
| 2981 | + // insert the new entry |
|
| 2982 | + $result = $this->_do_wpdb_query( |
|
| 2983 | + 'insert', |
|
| 2984 | + array($table->get_table_name(), $insertion_col_n_values, $format_for_insertion) |
|
| 2985 | + ); |
|
| 2986 | + if ($result === false) { |
|
| 2987 | + return false; |
|
| 2988 | + } |
|
| 2989 | + // ok, now what do we return for the ID of the newly-inserted thing? |
|
| 2990 | + if ($this->has_primary_key_field()) { |
|
| 2991 | + if ($this->get_primary_key_field()->is_auto_increment()) { |
|
| 2992 | + return $wpdb->insert_id; |
|
| 2993 | + } |
|
| 2994 | + // it's not an auto-increment primary key, so |
|
| 2995 | + // it must have been supplied |
|
| 2996 | + return $fields_n_values[ $this->get_primary_key_field()->get_name() ]; |
|
| 2997 | + } |
|
| 2998 | + // we can't return a primary key because there is none. instead return |
|
| 2999 | + // a unique string indicating this model |
|
| 3000 | + return $this->get_index_primary_key_string($fields_n_values); |
|
| 3001 | + } |
|
| 3002 | + |
|
| 3003 | + |
|
| 3004 | + |
|
| 3005 | + /** |
|
| 3006 | + * Prepare the $field_obj 's value in $fields_n_values for use in the database. |
|
| 3007 | + * If the field doesn't allow NULL, try to use its default. (If it doesn't allow NULL, |
|
| 3008 | + * and there is no default, we pass it along. WPDB will take care of it) |
|
| 3009 | + * |
|
| 3010 | + * @param EE_Model_Field_Base $field_obj |
|
| 3011 | + * @param array $fields_n_values |
|
| 3012 | + * @return mixed string|int|float depending on what the table column will be expecting |
|
| 3013 | + * @throws EE_Error |
|
| 3014 | + */ |
|
| 3015 | + protected function _prepare_value_or_use_default($field_obj, $fields_n_values) |
|
| 3016 | + { |
|
| 3017 | + // if this field doesn't allow nullable, don't allow it |
|
| 3018 | + if (! $field_obj->is_nullable() |
|
| 3019 | + && ( |
|
| 3020 | + ! isset($fields_n_values[ $field_obj->get_name() ]) |
|
| 3021 | + || $fields_n_values[ $field_obj->get_name() ] === null |
|
| 3022 | + ) |
|
| 3023 | + ) { |
|
| 3024 | + $fields_n_values[ $field_obj->get_name() ] = $field_obj->get_default_value(); |
|
| 3025 | + } |
|
| 3026 | + $unprepared_value = isset($fields_n_values[ $field_obj->get_name() ]) |
|
| 3027 | + ? $fields_n_values[ $field_obj->get_name() ] |
|
| 3028 | + : null; |
|
| 3029 | + return $this->_prepare_value_for_use_in_db($unprepared_value, $field_obj); |
|
| 3030 | + } |
|
| 3031 | + |
|
| 3032 | + |
|
| 3033 | + |
|
| 3034 | + /** |
|
| 3035 | + * Consolidates code for preparing a value supplied to the model for use int eh db. Calls the field's |
|
| 3036 | + * prepare_for_use_in_db method on the value, and depending on $value_already_prepare_by_model_obj, may also call |
|
| 3037 | + * the field's prepare_for_set() method. |
|
| 3038 | + * |
|
| 3039 | + * @param mixed $value value in the client code domain if $value_already_prepared_by_model_object is |
|
| 3040 | + * false, otherwise a value in the model object's domain (see lengthy comment at |
|
| 3041 | + * top of file) |
|
| 3042 | + * @param EE_Model_Field_Base $field field which will be doing the preparing of the value. If null, we assume |
|
| 3043 | + * $value is a custom selection |
|
| 3044 | + * @return mixed a value ready for use in the database for insertions, updating, or in a where clause |
|
| 3045 | + */ |
|
| 3046 | + private function _prepare_value_for_use_in_db($value, $field) |
|
| 3047 | + { |
|
| 3048 | + if ($field && $field instanceof EE_Model_Field_Base) { |
|
| 3049 | + // phpcs:disable PSR2.ControlStructures.SwitchDeclaration.TerminatingComment |
|
| 3050 | + switch ($this->_values_already_prepared_by_model_object) { |
|
| 3051 | + /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 3052 | + case self::not_prepared_by_model_object: |
|
| 3053 | + $value = $field->prepare_for_set($value); |
|
| 3054 | + // purposefully left out "return" |
|
| 3055 | + case self::prepared_by_model_object: |
|
| 3056 | + /** @noinspection SuspiciousAssignmentsInspection */ |
|
| 3057 | + $value = $field->prepare_for_use_in_db($value); |
|
| 3058 | + case self::prepared_for_use_in_db: |
|
| 3059 | + // leave the value alone |
|
| 3060 | + } |
|
| 3061 | + return $value; |
|
| 3062 | + // phpcs:enable |
|
| 3063 | + } |
|
| 3064 | + return $value; |
|
| 3065 | + } |
|
| 3066 | + |
|
| 3067 | + |
|
| 3068 | + |
|
| 3069 | + /** |
|
| 3070 | + * Returns the main table on this model |
|
| 3071 | + * |
|
| 3072 | + * @return EE_Primary_Table |
|
| 3073 | + * @throws EE_Error |
|
| 3074 | + */ |
|
| 3075 | + protected function _get_main_table() |
|
| 3076 | + { |
|
| 3077 | + foreach ($this->_tables as $table) { |
|
| 3078 | + if ($table instanceof EE_Primary_Table) { |
|
| 3079 | + return $table; |
|
| 3080 | + } |
|
| 3081 | + } |
|
| 3082 | + throw new EE_Error(sprintf(__( |
|
| 3083 | + 'There are no main tables on %s. They should be added to _tables array in the constructor', |
|
| 3084 | + 'event_espresso' |
|
| 3085 | + ), get_class($this))); |
|
| 3086 | + } |
|
| 3087 | + |
|
| 3088 | + |
|
| 3089 | + |
|
| 3090 | + /** |
|
| 3091 | + * table |
|
| 3092 | + * returns EE_Primary_Table table name |
|
| 3093 | + * |
|
| 3094 | + * @return string |
|
| 3095 | + * @throws EE_Error |
|
| 3096 | + */ |
|
| 3097 | + public function table() |
|
| 3098 | + { |
|
| 3099 | + return $this->_get_main_table()->get_table_name(); |
|
| 3100 | + } |
|
| 3101 | + |
|
| 3102 | + |
|
| 3103 | + |
|
| 3104 | + /** |
|
| 3105 | + * table |
|
| 3106 | + * returns first EE_Secondary_Table table name |
|
| 3107 | + * |
|
| 3108 | + * @return string |
|
| 3109 | + */ |
|
| 3110 | + public function second_table() |
|
| 3111 | + { |
|
| 3112 | + // grab second table from tables array |
|
| 3113 | + $second_table = end($this->_tables); |
|
| 3114 | + return $second_table instanceof EE_Secondary_Table ? $second_table->get_table_name() : null; |
|
| 3115 | + } |
|
| 3116 | + |
|
| 3117 | + |
|
| 3118 | + |
|
| 3119 | + /** |
|
| 3120 | + * get_table_obj_by_alias |
|
| 3121 | + * returns table name given it's alias |
|
| 3122 | + * |
|
| 3123 | + * @param string $table_alias |
|
| 3124 | + * @return EE_Primary_Table | EE_Secondary_Table |
|
| 3125 | + */ |
|
| 3126 | + public function get_table_obj_by_alias($table_alias = '') |
|
| 3127 | + { |
|
| 3128 | + return isset($this->_tables[ $table_alias ]) ? $this->_tables[ $table_alias ] : null; |
|
| 3129 | + } |
|
| 3130 | + |
|
| 3131 | + |
|
| 3132 | + |
|
| 3133 | + /** |
|
| 3134 | + * Gets all the tables of type EE_Other_Table from EEM_CPT_Basel_Model::_tables |
|
| 3135 | + * |
|
| 3136 | + * @return EE_Secondary_Table[] |
|
| 3137 | + */ |
|
| 3138 | + protected function _get_other_tables() |
|
| 3139 | + { |
|
| 3140 | + $other_tables = array(); |
|
| 3141 | + foreach ($this->_tables as $table_alias => $table) { |
|
| 3142 | + if ($table instanceof EE_Secondary_Table) { |
|
| 3143 | + $other_tables[ $table_alias ] = $table; |
|
| 3144 | + } |
|
| 3145 | + } |
|
| 3146 | + return $other_tables; |
|
| 3147 | + } |
|
| 3148 | + |
|
| 3149 | + |
|
| 3150 | + |
|
| 3151 | + /** |
|
| 3152 | + * Finds all the fields that correspond to the given table |
|
| 3153 | + * |
|
| 3154 | + * @param string $table_alias , array key in EEM_Base::_tables |
|
| 3155 | + * @return EE_Model_Field_Base[] |
|
| 3156 | + */ |
|
| 3157 | + public function _get_fields_for_table($table_alias) |
|
| 3158 | + { |
|
| 3159 | + return $this->_fields[ $table_alias ]; |
|
| 3160 | + } |
|
| 3161 | + |
|
| 3162 | + |
|
| 3163 | + |
|
| 3164 | + /** |
|
| 3165 | + * Recurses through all the where parameters, and finds all the related models we'll need |
|
| 3166 | + * to complete this query. Eg, given where parameters like array('EVT_ID'=>3) from within Event model, we won't |
|
| 3167 | + * need any related models. But if the array were array('Registrations.REG_ID'=>3), we'd need the related |
|
| 3168 | + * Registration model. If it were array('Registrations.Transactions.Payments.PAY_ID'=>3), then we'd need the |
|
| 3169 | + * related Registration, Transaction, and Payment models. |
|
| 3170 | + * |
|
| 3171 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 3172 | + * @return EE_Model_Query_Info_Carrier |
|
| 3173 | + * @throws EE_Error |
|
| 3174 | + */ |
|
| 3175 | + public function _extract_related_models_from_query($query_params) |
|
| 3176 | + { |
|
| 3177 | + $query_info_carrier = new EE_Model_Query_Info_Carrier(); |
|
| 3178 | + if (array_key_exists(0, $query_params)) { |
|
| 3179 | + $this->_extract_related_models_from_sub_params_array_keys($query_params[0], $query_info_carrier, 0); |
|
| 3180 | + } |
|
| 3181 | + if (array_key_exists('group_by', $query_params)) { |
|
| 3182 | + if (is_array($query_params['group_by'])) { |
|
| 3183 | + $this->_extract_related_models_from_sub_params_array_values( |
|
| 3184 | + $query_params['group_by'], |
|
| 3185 | + $query_info_carrier, |
|
| 3186 | + 'group_by' |
|
| 3187 | + ); |
|
| 3188 | + } elseif (! empty($query_params['group_by'])) { |
|
| 3189 | + $this->_extract_related_model_info_from_query_param( |
|
| 3190 | + $query_params['group_by'], |
|
| 3191 | + $query_info_carrier, |
|
| 3192 | + 'group_by' |
|
| 3193 | + ); |
|
| 3194 | + } |
|
| 3195 | + } |
|
| 3196 | + if (array_key_exists('having', $query_params)) { |
|
| 3197 | + $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3198 | + $query_params[0], |
|
| 3199 | + $query_info_carrier, |
|
| 3200 | + 'having' |
|
| 3201 | + ); |
|
| 3202 | + } |
|
| 3203 | + if (array_key_exists('order_by', $query_params)) { |
|
| 3204 | + if (is_array($query_params['order_by'])) { |
|
| 3205 | + $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3206 | + $query_params['order_by'], |
|
| 3207 | + $query_info_carrier, |
|
| 3208 | + 'order_by' |
|
| 3209 | + ); |
|
| 3210 | + } elseif (! empty($query_params['order_by'])) { |
|
| 3211 | + $this->_extract_related_model_info_from_query_param( |
|
| 3212 | + $query_params['order_by'], |
|
| 3213 | + $query_info_carrier, |
|
| 3214 | + 'order_by' |
|
| 3215 | + ); |
|
| 3216 | + } |
|
| 3217 | + } |
|
| 3218 | + if (array_key_exists('force_join', $query_params)) { |
|
| 3219 | + $this->_extract_related_models_from_sub_params_array_values( |
|
| 3220 | + $query_params['force_join'], |
|
| 3221 | + $query_info_carrier, |
|
| 3222 | + 'force_join' |
|
| 3223 | + ); |
|
| 3224 | + } |
|
| 3225 | + $this->extractRelatedModelsFromCustomSelects($query_info_carrier); |
|
| 3226 | + return $query_info_carrier; |
|
| 3227 | + } |
|
| 3228 | + |
|
| 3229 | + |
|
| 3230 | + |
|
| 3231 | + /** |
|
| 3232 | + * For extracting related models from WHERE (0), HAVING (having), ORDER BY (order_by) or forced joins (force_join) |
|
| 3233 | + * |
|
| 3234 | + * @param array $sub_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#-0-where-conditions |
|
| 3235 | + * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
| 3236 | + * @param string $query_param_type one of $this->_allowed_query_params |
|
| 3237 | + * @throws EE_Error |
|
| 3238 | + * @return \EE_Model_Query_Info_Carrier |
|
| 3239 | + */ |
|
| 3240 | + private function _extract_related_models_from_sub_params_array_keys( |
|
| 3241 | + $sub_query_params, |
|
| 3242 | + EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
| 3243 | + $query_param_type |
|
| 3244 | + ) { |
|
| 3245 | + if (! empty($sub_query_params)) { |
|
| 3246 | + $sub_query_params = (array) $sub_query_params; |
|
| 3247 | + foreach ($sub_query_params as $param => $possibly_array_of_params) { |
|
| 3248 | + // $param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
| 3249 | + $this->_extract_related_model_info_from_query_param( |
|
| 3250 | + $param, |
|
| 3251 | + $model_query_info_carrier, |
|
| 3252 | + $query_param_type |
|
| 3253 | + ); |
|
| 3254 | + // if $possibly_array_of_params is an array, try recursing into it, searching for keys which |
|
| 3255 | + // indicate needed joins. Eg, array('NOT'=>array('Registration.TXN_ID'=>23)). In this case, we tried |
|
| 3256 | + // extracting models out of the 'NOT', which obviously wasn't successful, and then we recurse into the value |
|
| 3257 | + // of array('Registration.TXN_ID'=>23) |
|
| 3258 | + $query_param_sans_stars = $this->_remove_stars_and_anything_after_from_condition_query_param_key($param); |
|
| 3259 | + if (in_array($query_param_sans_stars, $this->_logic_query_param_keys, true)) { |
|
| 3260 | + if (! is_array($possibly_array_of_params)) { |
|
| 3261 | + throw new EE_Error(sprintf( |
|
| 3262 | + __( |
|
| 3263 | + "You used a special where query param %s, but the value isn't an array of where query params, it's just %s'. It should be an array, eg array('EVT_ID'=>23,'OR'=>array('Venue.VNU_ID'=>32,'Venue.VNU_name'=>'monkey_land'))", |
|
| 3264 | + "event_espresso" |
|
| 3265 | + ), |
|
| 3266 | + $param, |
|
| 3267 | + $possibly_array_of_params |
|
| 3268 | + )); |
|
| 3269 | + } |
|
| 3270 | + $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3271 | + $possibly_array_of_params, |
|
| 3272 | + $model_query_info_carrier, |
|
| 3273 | + $query_param_type |
|
| 3274 | + ); |
|
| 3275 | + } elseif ($query_param_type === 0 // ie WHERE |
|
| 3276 | + && is_array($possibly_array_of_params) |
|
| 3277 | + && isset($possibly_array_of_params[2]) |
|
| 3278 | + && $possibly_array_of_params[2] == true |
|
| 3279 | + ) { |
|
| 3280 | + // then $possible_array_of_params looks something like array('<','DTT_sold',true) |
|
| 3281 | + // indicating that $possible_array_of_params[1] is actually a field name, |
|
| 3282 | + // from which we should extract query parameters! |
|
| 3283 | + if (! isset($possibly_array_of_params[0], $possibly_array_of_params[1])) { |
|
| 3284 | + throw new EE_Error(sprintf(__( |
|
| 3285 | + "Improperly formed query parameter %s. It should be numerically indexed like array('<','DTT_sold',true); but you provided %s", |
|
| 3286 | + "event_espresso" |
|
| 3287 | + ), $query_param_type, implode(",", $possibly_array_of_params))); |
|
| 3288 | + } |
|
| 3289 | + $this->_extract_related_model_info_from_query_param( |
|
| 3290 | + $possibly_array_of_params[1], |
|
| 3291 | + $model_query_info_carrier, |
|
| 3292 | + $query_param_type |
|
| 3293 | + ); |
|
| 3294 | + } |
|
| 3295 | + } |
|
| 3296 | + } |
|
| 3297 | + return $model_query_info_carrier; |
|
| 3298 | + } |
|
| 3299 | + |
|
| 3300 | + |
|
| 3301 | + |
|
| 3302 | + /** |
|
| 3303 | + * For extracting related models from forced_joins, where the array values contain the info about what |
|
| 3304 | + * models to join with. Eg an array like array('Attendee','Price.Price_Type'); |
|
| 3305 | + * |
|
| 3306 | + * @param array $sub_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 3307 | + * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
| 3308 | + * @param string $query_param_type one of $this->_allowed_query_params |
|
| 3309 | + * @throws EE_Error |
|
| 3310 | + * @return \EE_Model_Query_Info_Carrier |
|
| 3311 | + */ |
|
| 3312 | + private function _extract_related_models_from_sub_params_array_values( |
|
| 3313 | + $sub_query_params, |
|
| 3314 | + EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
| 3315 | + $query_param_type |
|
| 3316 | + ) { |
|
| 3317 | + if (! empty($sub_query_params)) { |
|
| 3318 | + if (! is_array($sub_query_params)) { |
|
| 3319 | + throw new EE_Error(sprintf( |
|
| 3320 | + __("Query parameter %s should be an array, but it isn't.", "event_espresso"), |
|
| 3321 | + $sub_query_params |
|
| 3322 | + )); |
|
| 3323 | + } |
|
| 3324 | + foreach ($sub_query_params as $param) { |
|
| 3325 | + // $param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
| 3326 | + $this->_extract_related_model_info_from_query_param( |
|
| 3327 | + $param, |
|
| 3328 | + $model_query_info_carrier, |
|
| 3329 | + $query_param_type |
|
| 3330 | + ); |
|
| 3331 | + } |
|
| 3332 | + } |
|
| 3333 | + return $model_query_info_carrier; |
|
| 3334 | + } |
|
| 3335 | + |
|
| 3336 | + |
|
| 3337 | + /** |
|
| 3338 | + * Extract all the query parts from model query params |
|
| 3339 | + * and put into a EEM_Related_Model_Info_Carrier for easy extraction into a query. We create this object |
|
| 3340 | + * instead of directly constructing the SQL because often we need to extract info from the $query_params |
|
| 3341 | + * but use them in a different order. Eg, we need to know what models we are querying |
|
| 3342 | + * before we know what joins to perform. However, we need to know what data types correspond to which fields on |
|
| 3343 | + * other models before we can finalize the where clause SQL. |
|
| 3344 | + * |
|
| 3345 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 3346 | + * @throws EE_Error |
|
| 3347 | + * @return EE_Model_Query_Info_Carrier |
|
| 3348 | + * @throws ModelConfigurationException |
|
| 3349 | + */ |
|
| 3350 | + public function _create_model_query_info_carrier($query_params) |
|
| 3351 | + { |
|
| 3352 | + if (! is_array($query_params)) { |
|
| 3353 | + EE_Error::doing_it_wrong( |
|
| 3354 | + 'EEM_Base::_create_model_query_info_carrier', |
|
| 3355 | + sprintf( |
|
| 3356 | + __( |
|
| 3357 | + '$query_params should be an array, you passed a variable of type %s', |
|
| 3358 | + 'event_espresso' |
|
| 3359 | + ), |
|
| 3360 | + gettype($query_params) |
|
| 3361 | + ), |
|
| 3362 | + '4.6.0' |
|
| 3363 | + ); |
|
| 3364 | + $query_params = array(); |
|
| 3365 | + } |
|
| 3366 | + $query_params[0] = isset($query_params[0]) ? $query_params[0] : array(); |
|
| 3367 | + // first check if we should alter the query to account for caps or not |
|
| 3368 | + // because the caps might require us to do extra joins |
|
| 3369 | + if (isset($query_params['caps']) && $query_params['caps'] !== 'none') { |
|
| 3370 | + $query_params[0] = array_replace_recursive( |
|
| 3371 | + $query_params[0], |
|
| 3372 | + $this->caps_where_conditions( |
|
| 3373 | + $query_params['caps'] |
|
| 3374 | + ) |
|
| 3375 | + ); |
|
| 3376 | + } |
|
| 3377 | + |
|
| 3378 | + // check if we should alter the query to remove data related to protected |
|
| 3379 | + // custom post types |
|
| 3380 | + if (isset($query_params['exclude_protected']) && $query_params['exclude_protected'] === true) { |
|
| 3381 | + $where_param_key_for_password = $this->modelChainAndPassword(); |
|
| 3382 | + // only include if related to a cpt where no password has been set |
|
| 3383 | + $query_params[0]['OR*nopassword'] = array( |
|
| 3384 | + $where_param_key_for_password => '', |
|
| 3385 | + $where_param_key_for_password . '*' => array('IS_NULL') |
|
| 3386 | + ); |
|
| 3387 | + } |
|
| 3388 | + $query_object = $this->_extract_related_models_from_query($query_params); |
|
| 3389 | + // verify where_query_params has NO numeric indexes.... that's simply not how you use it! |
|
| 3390 | + foreach ($query_params[0] as $key => $value) { |
|
| 3391 | + if (is_int($key)) { |
|
| 3392 | + throw new EE_Error( |
|
| 3393 | + sprintf( |
|
| 3394 | + __( |
|
| 3395 | + "WHERE query params must NOT be numerically-indexed. You provided the array key '%s' for value '%s' while querying model %s. All the query params provided were '%s' Please read documentation on EEM_Base::get_all.", |
|
| 3396 | + "event_espresso" |
|
| 3397 | + ), |
|
| 3398 | + $key, |
|
| 3399 | + var_export($value, true), |
|
| 3400 | + var_export($query_params, true), |
|
| 3401 | + get_class($this) |
|
| 3402 | + ) |
|
| 3403 | + ); |
|
| 3404 | + } |
|
| 3405 | + } |
|
| 3406 | + if (array_key_exists('default_where_conditions', $query_params) |
|
| 3407 | + && ! empty($query_params['default_where_conditions']) |
|
| 3408 | + ) { |
|
| 3409 | + $use_default_where_conditions = $query_params['default_where_conditions']; |
|
| 3410 | + } else { |
|
| 3411 | + $use_default_where_conditions = EEM_Base::default_where_conditions_all; |
|
| 3412 | + } |
|
| 3413 | + $query_params[0] = array_merge( |
|
| 3414 | + $this->_get_default_where_conditions_for_models_in_query( |
|
| 3415 | + $query_object, |
|
| 3416 | + $use_default_where_conditions, |
|
| 3417 | + $query_params[0] |
|
| 3418 | + ), |
|
| 3419 | + $query_params[0] |
|
| 3420 | + ); |
|
| 3421 | + $query_object->set_where_sql($this->_construct_where_clause($query_params[0])); |
|
| 3422 | + // if this is a "on_join_limit" then we are limiting on on a specific table in a multi_table join. |
|
| 3423 | + // So we need to setup a subquery and use that for the main join. |
|
| 3424 | + // Note for now this only works on the primary table for the model. |
|
| 3425 | + // So for instance, you could set the limit array like this: |
|
| 3426 | + // array( 'on_join_limit' => array('Primary_Table_Alias', array(1,10) ) ) |
|
| 3427 | + if (array_key_exists('on_join_limit', $query_params) && ! empty($query_params['on_join_limit'])) { |
|
| 3428 | + $query_object->set_main_model_join_sql( |
|
| 3429 | + $this->_construct_limit_join_select( |
|
| 3430 | + $query_params['on_join_limit'][0], |
|
| 3431 | + $query_params['on_join_limit'][1] |
|
| 3432 | + ) |
|
| 3433 | + ); |
|
| 3434 | + } |
|
| 3435 | + // set limit |
|
| 3436 | + if (array_key_exists('limit', $query_params)) { |
|
| 3437 | + if (is_array($query_params['limit'])) { |
|
| 3438 | + if (! isset($query_params['limit'][0], $query_params['limit'][1])) { |
|
| 3439 | + $e = sprintf( |
|
| 3440 | + __( |
|
| 3441 | + "Invalid DB query. You passed '%s' for the LIMIT, but only the following are valid: an integer, string representing an integer, a string like 'int,int', or an array like array(int,int)", |
|
| 3442 | + "event_espresso" |
|
| 3443 | + ), |
|
| 3444 | + http_build_query($query_params['limit']) |
|
| 3445 | + ); |
|
| 3446 | + throw new EE_Error($e . "|" . $e); |
|
| 3447 | + } |
|
| 3448 | + // they passed us an array for the limit. Assume it's like array(50,25), meaning offset by 50, and get 25 |
|
| 3449 | + $query_object->set_limit_sql(" LIMIT " . $query_params['limit'][0] . "," . $query_params['limit'][1]); |
|
| 3450 | + } elseif (! empty($query_params['limit'])) { |
|
| 3451 | + $query_object->set_limit_sql(" LIMIT " . $query_params['limit']); |
|
| 3452 | + } |
|
| 3453 | + } |
|
| 3454 | + // set order by |
|
| 3455 | + if (array_key_exists('order_by', $query_params)) { |
|
| 3456 | + if (is_array($query_params['order_by'])) { |
|
| 3457 | + // if they're using 'order_by' as an array, they can't use 'order' (because 'order_by' must |
|
| 3458 | + // specify whether to ascend or descend on each field. Eg 'order_by'=>array('EVT_ID'=>'ASC'). So |
|
| 3459 | + // including 'order' wouldn't make any sense if 'order_by' has already specified which way to order! |
|
| 3460 | + if (array_key_exists('order', $query_params)) { |
|
| 3461 | + throw new EE_Error( |
|
| 3462 | + sprintf( |
|
| 3463 | + __( |
|
| 3464 | + "In querying %s, we are using query parameter 'order_by' as an array (keys:%s,values:%s), and so we can't use query parameter 'order' (value %s). You should just use the 'order_by' parameter ", |
|
| 3465 | + "event_espresso" |
|
| 3466 | + ), |
|
| 3467 | + get_class($this), |
|
| 3468 | + implode(", ", array_keys($query_params['order_by'])), |
|
| 3469 | + implode(", ", $query_params['order_by']), |
|
| 3470 | + $query_params['order'] |
|
| 3471 | + ) |
|
| 3472 | + ); |
|
| 3473 | + } |
|
| 3474 | + $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3475 | + $query_params['order_by'], |
|
| 3476 | + $query_object, |
|
| 3477 | + 'order_by' |
|
| 3478 | + ); |
|
| 3479 | + // assume it's an array of fields to order by |
|
| 3480 | + $order_array = array(); |
|
| 3481 | + foreach ($query_params['order_by'] as $field_name_to_order_by => $order) { |
|
| 3482 | + $order = $this->_extract_order($order); |
|
| 3483 | + $order_array[] = $this->_deduce_column_name_from_query_param($field_name_to_order_by) . SP . $order; |
|
| 3484 | + } |
|
| 3485 | + $query_object->set_order_by_sql(" ORDER BY " . implode(",", $order_array)); |
|
| 3486 | + } elseif (! empty($query_params['order_by'])) { |
|
| 3487 | + $this->_extract_related_model_info_from_query_param( |
|
| 3488 | + $query_params['order_by'], |
|
| 3489 | + $query_object, |
|
| 3490 | + 'order', |
|
| 3491 | + $query_params['order_by'] |
|
| 3492 | + ); |
|
| 3493 | + $order = isset($query_params['order']) |
|
| 3494 | + ? $this->_extract_order($query_params['order']) |
|
| 3495 | + : 'DESC'; |
|
| 3496 | + $query_object->set_order_by_sql( |
|
| 3497 | + " ORDER BY " . $this->_deduce_column_name_from_query_param($query_params['order_by']) . SP . $order |
|
| 3498 | + ); |
|
| 3499 | + } |
|
| 3500 | + } |
|
| 3501 | + // if 'order_by' wasn't set, maybe they are just using 'order' on its own? |
|
| 3502 | + if (! array_key_exists('order_by', $query_params) |
|
| 3503 | + && array_key_exists('order', $query_params) |
|
| 3504 | + && ! empty($query_params['order']) |
|
| 3505 | + ) { |
|
| 3506 | + $pk_field = $this->get_primary_key_field(); |
|
| 3507 | + $order = $this->_extract_order($query_params['order']); |
|
| 3508 | + $query_object->set_order_by_sql(" ORDER BY " . $pk_field->get_qualified_column() . SP . $order); |
|
| 3509 | + } |
|
| 3510 | + // set group by |
|
| 3511 | + if (array_key_exists('group_by', $query_params)) { |
|
| 3512 | + if (is_array($query_params['group_by'])) { |
|
| 3513 | + // it's an array, so assume we'll be grouping by a bunch of stuff |
|
| 3514 | + $group_by_array = array(); |
|
| 3515 | + foreach ($query_params['group_by'] as $field_name_to_group_by) { |
|
| 3516 | + $group_by_array[] = $this->_deduce_column_name_from_query_param($field_name_to_group_by); |
|
| 3517 | + } |
|
| 3518 | + $query_object->set_group_by_sql(" GROUP BY " . implode(", ", $group_by_array)); |
|
| 3519 | + } elseif (! empty($query_params['group_by'])) { |
|
| 3520 | + $query_object->set_group_by_sql( |
|
| 3521 | + " GROUP BY " . $this->_deduce_column_name_from_query_param($query_params['group_by']) |
|
| 3522 | + ); |
|
| 3523 | + } |
|
| 3524 | + } |
|
| 3525 | + // set having |
|
| 3526 | + if (array_key_exists('having', $query_params) && $query_params['having']) { |
|
| 3527 | + $query_object->set_having_sql($this->_construct_having_clause($query_params['having'])); |
|
| 3528 | + } |
|
| 3529 | + // now, just verify they didn't pass anything wack |
|
| 3530 | + foreach ($query_params as $query_key => $query_value) { |
|
| 3531 | + if (! in_array($query_key, $this->_allowed_query_params, true)) { |
|
| 3532 | + throw new EE_Error( |
|
| 3533 | + sprintf( |
|
| 3534 | + __( |
|
| 3535 | + "You passed %s as a query parameter to %s, which is illegal! The allowed query parameters are %s", |
|
| 3536 | + 'event_espresso' |
|
| 3537 | + ), |
|
| 3538 | + $query_key, |
|
| 3539 | + get_class($this), |
|
| 3540 | + // print_r( $this->_allowed_query_params, TRUE ) |
|
| 3541 | + implode(',', $this->_allowed_query_params) |
|
| 3542 | + ) |
|
| 3543 | + ); |
|
| 3544 | + } |
|
| 3545 | + } |
|
| 3546 | + $main_model_join_sql = $query_object->get_main_model_join_sql(); |
|
| 3547 | + if (empty($main_model_join_sql)) { |
|
| 3548 | + $query_object->set_main_model_join_sql($this->_construct_internal_join()); |
|
| 3549 | + } |
|
| 3550 | + return $query_object; |
|
| 3551 | + } |
|
| 3552 | + |
|
| 3553 | + |
|
| 3554 | + |
|
| 3555 | + /** |
|
| 3556 | + * Gets the where conditions that should be imposed on the query based on the |
|
| 3557 | + * context (eg reading frontend, backend, edit or delete). |
|
| 3558 | + * |
|
| 3559 | + * @param string $context one of EEM_Base::valid_cap_contexts() |
|
| 3560 | + * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 3561 | + * @throws EE_Error |
|
| 3562 | + */ |
|
| 3563 | + public function caps_where_conditions($context = self::caps_read) |
|
| 3564 | + { |
|
| 3565 | + EEM_Base::verify_is_valid_cap_context($context); |
|
| 3566 | + $cap_where_conditions = array(); |
|
| 3567 | + $cap_restrictions = $this->caps_missing($context); |
|
| 3568 | + /** |
|
| 3569 | + * @var $cap_restrictions EE_Default_Where_Conditions[] |
|
| 3570 | + */ |
|
| 3571 | + foreach ($cap_restrictions as $cap => $restriction_if_no_cap) { |
|
| 3572 | + $cap_where_conditions = array_replace_recursive( |
|
| 3573 | + $cap_where_conditions, |
|
| 3574 | + $restriction_if_no_cap->get_default_where_conditions() |
|
| 3575 | + ); |
|
| 3576 | + } |
|
| 3577 | + return apply_filters( |
|
| 3578 | + 'FHEE__EEM_Base__caps_where_conditions__return', |
|
| 3579 | + $cap_where_conditions, |
|
| 3580 | + $this, |
|
| 3581 | + $context, |
|
| 3582 | + $cap_restrictions |
|
| 3583 | + ); |
|
| 3584 | + } |
|
| 3585 | + |
|
| 3586 | + |
|
| 3587 | + |
|
| 3588 | + /** |
|
| 3589 | + * Verifies that $should_be_order_string is in $this->_allowed_order_values, |
|
| 3590 | + * otherwise throws an exception |
|
| 3591 | + * |
|
| 3592 | + * @param string $should_be_order_string |
|
| 3593 | + * @return string either ASC, asc, DESC or desc |
|
| 3594 | + * @throws EE_Error |
|
| 3595 | + */ |
|
| 3596 | + private function _extract_order($should_be_order_string) |
|
| 3597 | + { |
|
| 3598 | + if (in_array($should_be_order_string, $this->_allowed_order_values)) { |
|
| 3599 | + return $should_be_order_string; |
|
| 3600 | + } |
|
| 3601 | + throw new EE_Error( |
|
| 3602 | + sprintf( |
|
| 3603 | + __( |
|
| 3604 | + "While performing a query on '%s', tried to use '%s' as an order parameter. ", |
|
| 3605 | + "event_espresso" |
|
| 3606 | + ), |
|
| 3607 | + get_class($this), |
|
| 3608 | + $should_be_order_string |
|
| 3609 | + ) |
|
| 3610 | + ); |
|
| 3611 | + } |
|
| 3612 | + |
|
| 3613 | + |
|
| 3614 | + |
|
| 3615 | + /** |
|
| 3616 | + * Looks at all the models which are included in this query, and asks each |
|
| 3617 | + * for their universal_where_params, and returns them in the same format as $query_params[0] (where), |
|
| 3618 | + * so they can be merged |
|
| 3619 | + * |
|
| 3620 | + * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
| 3621 | + * @param string $use_default_where_conditions can be 'none','other_models_only', or 'all'. |
|
| 3622 | + * 'none' means NO default where conditions will |
|
| 3623 | + * be used AT ALL during this query. |
|
| 3624 | + * 'other_models_only' means default where |
|
| 3625 | + * conditions from other models will be used, but |
|
| 3626 | + * not for this primary model. 'all', the default, |
|
| 3627 | + * means default where conditions will apply as |
|
| 3628 | + * normal |
|
| 3629 | + * @param array $where_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 3630 | + * @throws EE_Error |
|
| 3631 | + * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 3632 | + */ |
|
| 3633 | + private function _get_default_where_conditions_for_models_in_query( |
|
| 3634 | + EE_Model_Query_Info_Carrier $query_info_carrier, |
|
| 3635 | + $use_default_where_conditions = EEM_Base::default_where_conditions_all, |
|
| 3636 | + $where_query_params = array() |
|
| 3637 | + ) { |
|
| 3638 | + $allowed_used_default_where_conditions_values = EEM_Base::valid_default_where_conditions(); |
|
| 3639 | + if (! in_array($use_default_where_conditions, $allowed_used_default_where_conditions_values)) { |
|
| 3640 | + throw new EE_Error(sprintf( |
|
| 3641 | + __( |
|
| 3642 | + "You passed an invalid value to the query parameter 'default_where_conditions' of '%s'. Allowed values are %s", |
|
| 3643 | + "event_espresso" |
|
| 3644 | + ), |
|
| 3645 | + $use_default_where_conditions, |
|
| 3646 | + implode(", ", $allowed_used_default_where_conditions_values) |
|
| 3647 | + )); |
|
| 3648 | + } |
|
| 3649 | + $universal_query_params = array(); |
|
| 3650 | + if ($this->_should_use_default_where_conditions($use_default_where_conditions, true)) { |
|
| 3651 | + $universal_query_params = $this->_get_default_where_conditions(); |
|
| 3652 | + } elseif ($this->_should_use_minimum_where_conditions($use_default_where_conditions, true)) { |
|
| 3653 | + $universal_query_params = $this->_get_minimum_where_conditions(); |
|
| 3654 | + } |
|
| 3655 | + foreach ($query_info_carrier->get_model_names_included() as $model_relation_path => $model_name) { |
|
| 3656 | + $related_model = $this->get_related_model_obj($model_name); |
|
| 3657 | + if ($this->_should_use_default_where_conditions($use_default_where_conditions, false)) { |
|
| 3658 | + $related_model_universal_where_params = $related_model->_get_default_where_conditions($model_relation_path); |
|
| 3659 | + } elseif ($this->_should_use_minimum_where_conditions($use_default_where_conditions, false)) { |
|
| 3660 | + $related_model_universal_where_params = $related_model->_get_minimum_where_conditions($model_relation_path); |
|
| 3661 | + } else { |
|
| 3662 | + // we don't want to add full or even minimum default where conditions from this model, so just continue |
|
| 3663 | + continue; |
|
| 3664 | + } |
|
| 3665 | + $overrides = $this->_override_defaults_or_make_null_friendly( |
|
| 3666 | + $related_model_universal_where_params, |
|
| 3667 | + $where_query_params, |
|
| 3668 | + $related_model, |
|
| 3669 | + $model_relation_path |
|
| 3670 | + ); |
|
| 3671 | + $universal_query_params = EEH_Array::merge_arrays_and_overwrite_keys( |
|
| 3672 | + $universal_query_params, |
|
| 3673 | + $overrides |
|
| 3674 | + ); |
|
| 3675 | + } |
|
| 3676 | + return $universal_query_params; |
|
| 3677 | + } |
|
| 3678 | + |
|
| 3679 | + |
|
| 3680 | + |
|
| 3681 | + /** |
|
| 3682 | + * Determines whether or not we should use default where conditions for the model in question |
|
| 3683 | + * (this model, or other related models). |
|
| 3684 | + * Basically, we should use default where conditions on this model if they have requested to use them on all models, |
|
| 3685 | + * this model only, or to use minimum where conditions on all other models and normal where conditions on this one. |
|
| 3686 | + * We should use default where conditions on related models when they requested to use default where conditions |
|
| 3687 | + * on all models, or specifically just on other related models |
|
| 3688 | + * @param $default_where_conditions_value |
|
| 3689 | + * @param bool $for_this_model false means this is for OTHER related models |
|
| 3690 | + * @return bool |
|
| 3691 | + */ |
|
| 3692 | + private function _should_use_default_where_conditions($default_where_conditions_value, $for_this_model = true) |
|
| 3693 | + { |
|
| 3694 | + return ( |
|
| 3695 | + $for_this_model |
|
| 3696 | + && in_array( |
|
| 3697 | + $default_where_conditions_value, |
|
| 3698 | + array( |
|
| 3699 | + EEM_Base::default_where_conditions_all, |
|
| 3700 | + EEM_Base::default_where_conditions_this_only, |
|
| 3701 | + EEM_Base::default_where_conditions_minimum_others, |
|
| 3702 | + ), |
|
| 3703 | + true |
|
| 3704 | + ) |
|
| 3705 | + ) |
|
| 3706 | + || ( |
|
| 3707 | + ! $for_this_model |
|
| 3708 | + && in_array( |
|
| 3709 | + $default_where_conditions_value, |
|
| 3710 | + array( |
|
| 3711 | + EEM_Base::default_where_conditions_all, |
|
| 3712 | + EEM_Base::default_where_conditions_others_only, |
|
| 3713 | + ), |
|
| 3714 | + true |
|
| 3715 | + ) |
|
| 3716 | + ); |
|
| 3717 | + } |
|
| 3718 | + |
|
| 3719 | + /** |
|
| 3720 | + * Determines whether or not we should use default minimum conditions for the model in question |
|
| 3721 | + * (this model, or other related models). |
|
| 3722 | + * Basically, we should use minimum where conditions on this model only if they requested all models to use minimum |
|
| 3723 | + * where conditions. |
|
| 3724 | + * We should use minimum where conditions on related models if they requested to use minimum where conditions |
|
| 3725 | + * on this model or others |
|
| 3726 | + * @param $default_where_conditions_value |
|
| 3727 | + * @param bool $for_this_model false means this is for OTHER related models |
|
| 3728 | + * @return bool |
|
| 3729 | + */ |
|
| 3730 | + private function _should_use_minimum_where_conditions($default_where_conditions_value, $for_this_model = true) |
|
| 3731 | + { |
|
| 3732 | + return ( |
|
| 3733 | + $for_this_model |
|
| 3734 | + && $default_where_conditions_value === EEM_Base::default_where_conditions_minimum_all |
|
| 3735 | + ) |
|
| 3736 | + || ( |
|
| 3737 | + ! $for_this_model |
|
| 3738 | + && in_array( |
|
| 3739 | + $default_where_conditions_value, |
|
| 3740 | + array( |
|
| 3741 | + EEM_Base::default_where_conditions_minimum_others, |
|
| 3742 | + EEM_Base::default_where_conditions_minimum_all, |
|
| 3743 | + ), |
|
| 3744 | + true |
|
| 3745 | + ) |
|
| 3746 | + ); |
|
| 3747 | + } |
|
| 3748 | + |
|
| 3749 | + |
|
| 3750 | + /** |
|
| 3751 | + * Checks if any of the defaults have been overridden. If there are any that AREN'T overridden, |
|
| 3752 | + * then we also add a special where condition which allows for that model's primary key |
|
| 3753 | + * to be null (which is important for JOINs. Eg, if you want to see all Events ordered by Venue's name, |
|
| 3754 | + * then Event's with NO Venue won't appear unless you allow VNU_ID to be NULL) |
|
| 3755 | + * |
|
| 3756 | + * @param array $default_where_conditions |
|
| 3757 | + * @param array $provided_where_conditions |
|
| 3758 | + * @param EEM_Base $model |
|
| 3759 | + * @param string $model_relation_path like 'Transaction.Payment.' |
|
| 3760 | + * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 3761 | + * @throws EE_Error |
|
| 3762 | + */ |
|
| 3763 | + private function _override_defaults_or_make_null_friendly( |
|
| 3764 | + $default_where_conditions, |
|
| 3765 | + $provided_where_conditions, |
|
| 3766 | + $model, |
|
| 3767 | + $model_relation_path |
|
| 3768 | + ) { |
|
| 3769 | + $null_friendly_where_conditions = array(); |
|
| 3770 | + $none_overridden = true; |
|
| 3771 | + $or_condition_key_for_defaults = 'OR*' . get_class($model); |
|
| 3772 | + foreach ($default_where_conditions as $key => $val) { |
|
| 3773 | + if (isset($provided_where_conditions[ $key ])) { |
|
| 3774 | + $none_overridden = false; |
|
| 3775 | + } else { |
|
| 3776 | + $null_friendly_where_conditions[ $or_condition_key_for_defaults ]['AND'][ $key ] = $val; |
|
| 3777 | + } |
|
| 3778 | + } |
|
| 3779 | + if ($none_overridden && $default_where_conditions) { |
|
| 3780 | + if ($model->has_primary_key_field()) { |
|
| 3781 | + $null_friendly_where_conditions[ $or_condition_key_for_defaults ][ $model_relation_path |
|
| 3782 | + . "." |
|
| 3783 | + . $model->primary_key_name() ] = array('IS NULL'); |
|
| 3784 | + }/*else{ |
|
| 3785 | 3785 | //@todo NO PK, use other defaults |
| 3786 | 3786 | }*/ |
| 3787 | - } |
|
| 3788 | - return $null_friendly_where_conditions; |
|
| 3789 | - } |
|
| 3790 | - |
|
| 3791 | - |
|
| 3792 | - |
|
| 3793 | - /** |
|
| 3794 | - * Uses the _default_where_conditions_strategy set during __construct() to get |
|
| 3795 | - * default where conditions on all get_all, update, and delete queries done by this model. |
|
| 3796 | - * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
| 3797 | - * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
| 3798 | - * |
|
| 3799 | - * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
| 3800 | - * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 3801 | - */ |
|
| 3802 | - private function _get_default_where_conditions($model_relation_path = null) |
|
| 3803 | - { |
|
| 3804 | - if ($this->_ignore_where_strategy) { |
|
| 3805 | - return array(); |
|
| 3806 | - } |
|
| 3807 | - return $this->_default_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
| 3808 | - } |
|
| 3809 | - |
|
| 3810 | - |
|
| 3811 | - |
|
| 3812 | - /** |
|
| 3813 | - * Uses the _minimum_where_conditions_strategy set during __construct() to get |
|
| 3814 | - * minimum where conditions on all get_all, update, and delete queries done by this model. |
|
| 3815 | - * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
| 3816 | - * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
| 3817 | - * Similar to _get_default_where_conditions |
|
| 3818 | - * |
|
| 3819 | - * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
| 3820 | - * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 3821 | - */ |
|
| 3822 | - protected function _get_minimum_where_conditions($model_relation_path = null) |
|
| 3823 | - { |
|
| 3824 | - if ($this->_ignore_where_strategy) { |
|
| 3825 | - return array(); |
|
| 3826 | - } |
|
| 3827 | - return $this->_minimum_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
| 3828 | - } |
|
| 3829 | - |
|
| 3830 | - |
|
| 3831 | - |
|
| 3832 | - /** |
|
| 3833 | - * Creates the string of SQL for the select part of a select query, everything behind SELECT and before FROM. |
|
| 3834 | - * Eg, "Event.post_id, Event.post_name,Event_Detail.EVT_ID..." |
|
| 3835 | - * |
|
| 3836 | - * @param EE_Model_Query_Info_Carrier $model_query_info |
|
| 3837 | - * @return string |
|
| 3838 | - * @throws EE_Error |
|
| 3839 | - */ |
|
| 3840 | - private function _construct_default_select_sql(EE_Model_Query_Info_Carrier $model_query_info) |
|
| 3841 | - { |
|
| 3842 | - $selects = $this->_get_columns_to_select_for_this_model(); |
|
| 3843 | - foreach ($model_query_info->get_model_names_included() as $model_relation_chain => |
|
| 3844 | - $name_of_other_model_included) { |
|
| 3845 | - $other_model_included = $this->get_related_model_obj($name_of_other_model_included); |
|
| 3846 | - $other_model_selects = $other_model_included->_get_columns_to_select_for_this_model($model_relation_chain); |
|
| 3847 | - foreach ($other_model_selects as $key => $value) { |
|
| 3848 | - $selects[] = $value; |
|
| 3849 | - } |
|
| 3850 | - } |
|
| 3851 | - return implode(", ", $selects); |
|
| 3852 | - } |
|
| 3853 | - |
|
| 3854 | - |
|
| 3855 | - |
|
| 3856 | - /** |
|
| 3857 | - * Gets an array of columns to select for this model, which are necessary for it to create its objects. |
|
| 3858 | - * So that's going to be the columns for all the fields on the model |
|
| 3859 | - * |
|
| 3860 | - * @param string $model_relation_chain like 'Question.Question_Group.Event' |
|
| 3861 | - * @return array numerically indexed, values are columns to select and rename, eg "Event.ID AS 'Event.ID'" |
|
| 3862 | - */ |
|
| 3863 | - public function _get_columns_to_select_for_this_model($model_relation_chain = '') |
|
| 3864 | - { |
|
| 3865 | - $fields = $this->field_settings(); |
|
| 3866 | - $selects = array(); |
|
| 3867 | - $table_alias_with_model_relation_chain_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 3868 | - $model_relation_chain, |
|
| 3869 | - $this->get_this_model_name() |
|
| 3870 | - ); |
|
| 3871 | - foreach ($fields as $field_obj) { |
|
| 3872 | - $selects[] = $table_alias_with_model_relation_chain_prefix |
|
| 3873 | - . $field_obj->get_table_alias() |
|
| 3874 | - . "." |
|
| 3875 | - . $field_obj->get_table_column() |
|
| 3876 | - . " AS '" |
|
| 3877 | - . $table_alias_with_model_relation_chain_prefix |
|
| 3878 | - . $field_obj->get_table_alias() |
|
| 3879 | - . "." |
|
| 3880 | - . $field_obj->get_table_column() |
|
| 3881 | - . "'"; |
|
| 3882 | - } |
|
| 3883 | - // make sure we are also getting the PKs of each table |
|
| 3884 | - $tables = $this->get_tables(); |
|
| 3885 | - if (count($tables) > 1) { |
|
| 3886 | - foreach ($tables as $table_obj) { |
|
| 3887 | - $qualified_pk_column = $table_alias_with_model_relation_chain_prefix |
|
| 3888 | - . $table_obj->get_fully_qualified_pk_column(); |
|
| 3889 | - if (! in_array($qualified_pk_column, $selects)) { |
|
| 3890 | - $selects[] = "$qualified_pk_column AS '$qualified_pk_column'"; |
|
| 3891 | - } |
|
| 3892 | - } |
|
| 3893 | - } |
|
| 3894 | - return $selects; |
|
| 3895 | - } |
|
| 3896 | - |
|
| 3897 | - |
|
| 3898 | - |
|
| 3899 | - /** |
|
| 3900 | - * Given a $query_param like 'Registration.Transaction.TXN_ID', pops off 'Registration.', |
|
| 3901 | - * gets the join statement for it; gets the data types for it; and passes the remaining 'Transaction.TXN_ID' |
|
| 3902 | - * onto its related Transaction object to do the same. Returns an EE_Join_And_Data_Types object which contains the |
|
| 3903 | - * SQL for joining, and the data types |
|
| 3904 | - * |
|
| 3905 | - * @param null|string $original_query_param |
|
| 3906 | - * @param string $query_param like Registration.Transaction.TXN_ID |
|
| 3907 | - * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
| 3908 | - * @param string $query_param_type like Registration.Transaction.TXN_ID |
|
| 3909 | - * or 'PAY_ID'. Otherwise, we don't expect there to be a |
|
| 3910 | - * column name. We only want model names, eg 'Event.Venue' |
|
| 3911 | - * or 'Registration's |
|
| 3912 | - * @param string $original_query_param what it originally was (eg |
|
| 3913 | - * Registration.Transaction.TXN_ID). If null, we assume it |
|
| 3914 | - * matches $query_param |
|
| 3915 | - * @throws EE_Error |
|
| 3916 | - * @return void only modifies the EEM_Related_Model_Info_Carrier passed into it |
|
| 3917 | - */ |
|
| 3918 | - private function _extract_related_model_info_from_query_param( |
|
| 3919 | - $query_param, |
|
| 3920 | - EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
| 3921 | - $query_param_type, |
|
| 3922 | - $original_query_param = null |
|
| 3923 | - ) { |
|
| 3924 | - if ($original_query_param === null) { |
|
| 3925 | - $original_query_param = $query_param; |
|
| 3926 | - } |
|
| 3927 | - $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param); |
|
| 3928 | - /** @var $allow_logic_query_params bool whether or not to allow logic_query_params like 'NOT','OR', or 'AND' */ |
|
| 3929 | - $allow_logic_query_params = in_array($query_param_type, array('where', 'having', 0, 'custom_selects'), true); |
|
| 3930 | - $allow_fields = in_array( |
|
| 3931 | - $query_param_type, |
|
| 3932 | - array('where', 'having', 'order_by', 'group_by', 'order', 'custom_selects', 0), |
|
| 3933 | - true |
|
| 3934 | - ); |
|
| 3935 | - // check to see if we have a field on this model |
|
| 3936 | - $this_model_fields = $this->field_settings(true); |
|
| 3937 | - if (array_key_exists($query_param, $this_model_fields)) { |
|
| 3938 | - if ($allow_fields) { |
|
| 3939 | - return; |
|
| 3940 | - } |
|
| 3941 | - throw new EE_Error( |
|
| 3942 | - sprintf( |
|
| 3943 | - __( |
|
| 3944 | - "Using a field name (%s) on model %s is not allowed on this query param type '%s'. Original query param was %s", |
|
| 3945 | - "event_espresso" |
|
| 3946 | - ), |
|
| 3947 | - $query_param, |
|
| 3948 | - get_class($this), |
|
| 3949 | - $query_param_type, |
|
| 3950 | - $original_query_param |
|
| 3951 | - ) |
|
| 3952 | - ); |
|
| 3953 | - } |
|
| 3954 | - // check if this is a special logic query param |
|
| 3955 | - if (in_array($query_param, $this->_logic_query_param_keys, true)) { |
|
| 3956 | - if ($allow_logic_query_params) { |
|
| 3957 | - return; |
|
| 3958 | - } |
|
| 3959 | - throw new EE_Error( |
|
| 3960 | - sprintf( |
|
| 3961 | - __( |
|
| 3962 | - 'Logic query params ("%1$s") are being used incorrectly with the following query param ("%2$s") on model %3$s. %4$sAdditional Info:%4$s%5$s', |
|
| 3963 | - 'event_espresso' |
|
| 3964 | - ), |
|
| 3965 | - implode('", "', $this->_logic_query_param_keys), |
|
| 3966 | - $query_param, |
|
| 3967 | - get_class($this), |
|
| 3968 | - '<br />', |
|
| 3969 | - "\t" |
|
| 3970 | - . ' $passed_in_query_info = <pre>' |
|
| 3971 | - . print_r($passed_in_query_info, true) |
|
| 3972 | - . '</pre>' |
|
| 3973 | - . "\n\t" |
|
| 3974 | - . ' $query_param_type = ' |
|
| 3975 | - . $query_param_type |
|
| 3976 | - . "\n\t" |
|
| 3977 | - . ' $original_query_param = ' |
|
| 3978 | - . $original_query_param |
|
| 3979 | - ) |
|
| 3980 | - ); |
|
| 3981 | - } |
|
| 3982 | - // check if it's a custom selection |
|
| 3983 | - if ($this->_custom_selections instanceof CustomSelects |
|
| 3984 | - && in_array($query_param, $this->_custom_selections->columnAliases(), true) |
|
| 3985 | - ) { |
|
| 3986 | - return; |
|
| 3987 | - } |
|
| 3988 | - // check if has a model name at the beginning |
|
| 3989 | - // and |
|
| 3990 | - // check if it's a field on a related model |
|
| 3991 | - if ($this->extractJoinModelFromQueryParams( |
|
| 3992 | - $passed_in_query_info, |
|
| 3993 | - $query_param, |
|
| 3994 | - $original_query_param, |
|
| 3995 | - $query_param_type |
|
| 3996 | - )) { |
|
| 3997 | - return; |
|
| 3998 | - } |
|
| 3999 | - |
|
| 4000 | - // ok so $query_param didn't start with a model name |
|
| 4001 | - // and we previously confirmed it wasn't a logic query param or field on the current model |
|
| 4002 | - // it's wack, that's what it is |
|
| 4003 | - throw new EE_Error( |
|
| 4004 | - sprintf( |
|
| 4005 | - esc_html__( |
|
| 4006 | - "There is no model named '%s' related to %s. Query param type is %s and original query param is %s", |
|
| 4007 | - "event_espresso" |
|
| 4008 | - ), |
|
| 4009 | - $query_param, |
|
| 4010 | - get_class($this), |
|
| 4011 | - $query_param_type, |
|
| 4012 | - $original_query_param |
|
| 4013 | - ) |
|
| 4014 | - ); |
|
| 4015 | - } |
|
| 4016 | - |
|
| 4017 | - |
|
| 4018 | - /** |
|
| 4019 | - * Extracts any possible join model information from the provided possible_join_string. |
|
| 4020 | - * This method will read the provided $possible_join_string value and determine if there are any possible model join |
|
| 4021 | - * parts that should be added to the query. |
|
| 4022 | - * |
|
| 4023 | - * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
| 4024 | - * @param string $possible_join_string Such as Registration.REG_ID, or Registration |
|
| 4025 | - * @param null|string $original_query_param |
|
| 4026 | - * @param string $query_parameter_type The type for the source of the $possible_join_string |
|
| 4027 | - * ('where', 'order_by', 'group_by', 'custom_selects' etc.) |
|
| 4028 | - * @return bool returns true if a join was added and false if not. |
|
| 4029 | - * @throws EE_Error |
|
| 4030 | - */ |
|
| 4031 | - private function extractJoinModelFromQueryParams( |
|
| 4032 | - EE_Model_Query_Info_Carrier $query_info_carrier, |
|
| 4033 | - $possible_join_string, |
|
| 4034 | - $original_query_param, |
|
| 4035 | - $query_parameter_type |
|
| 4036 | - ) { |
|
| 4037 | - foreach ($this->_model_relations as $valid_related_model_name => $relation_obj) { |
|
| 4038 | - if (strpos($possible_join_string, $valid_related_model_name . ".") === 0) { |
|
| 4039 | - $this->_add_join_to_model($valid_related_model_name, $query_info_carrier, $original_query_param); |
|
| 4040 | - $possible_join_string = substr($possible_join_string, strlen($valid_related_model_name . ".")); |
|
| 4041 | - if ($possible_join_string === '') { |
|
| 4042 | - // nothing left to $query_param |
|
| 4043 | - // we should actually end in a field name, not a model like this! |
|
| 4044 | - throw new EE_Error( |
|
| 4045 | - sprintf( |
|
| 4046 | - esc_html__( |
|
| 4047 | - "Query param '%s' (of type %s on model %s) shouldn't end on a period (.) ", |
|
| 4048 | - "event_espresso" |
|
| 4049 | - ), |
|
| 4050 | - $possible_join_string, |
|
| 4051 | - $query_parameter_type, |
|
| 4052 | - get_class($this), |
|
| 4053 | - $valid_related_model_name |
|
| 4054 | - ) |
|
| 4055 | - ); |
|
| 4056 | - } |
|
| 4057 | - $related_model_obj = $this->get_related_model_obj($valid_related_model_name); |
|
| 4058 | - $related_model_obj->_extract_related_model_info_from_query_param( |
|
| 4059 | - $possible_join_string, |
|
| 4060 | - $query_info_carrier, |
|
| 4061 | - $query_parameter_type, |
|
| 4062 | - $original_query_param |
|
| 4063 | - ); |
|
| 4064 | - return true; |
|
| 4065 | - } |
|
| 4066 | - if ($possible_join_string === $valid_related_model_name) { |
|
| 4067 | - $this->_add_join_to_model( |
|
| 4068 | - $valid_related_model_name, |
|
| 4069 | - $query_info_carrier, |
|
| 4070 | - $original_query_param |
|
| 4071 | - ); |
|
| 4072 | - return true; |
|
| 4073 | - } |
|
| 4074 | - } |
|
| 4075 | - return false; |
|
| 4076 | - } |
|
| 4077 | - |
|
| 4078 | - |
|
| 4079 | - /** |
|
| 4080 | - * Extracts related models from Custom Selects and sets up any joins for those related models. |
|
| 4081 | - * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
| 4082 | - * @throws EE_Error |
|
| 4083 | - */ |
|
| 4084 | - private function extractRelatedModelsFromCustomSelects(EE_Model_Query_Info_Carrier $query_info_carrier) |
|
| 4085 | - { |
|
| 4086 | - if ($this->_custom_selections instanceof CustomSelects |
|
| 4087 | - && ($this->_custom_selections->type() === CustomSelects::TYPE_STRUCTURED |
|
| 4088 | - || $this->_custom_selections->type() == CustomSelects::TYPE_COMPLEX |
|
| 4089 | - ) |
|
| 4090 | - ) { |
|
| 4091 | - $original_selects = $this->_custom_selections->originalSelects(); |
|
| 4092 | - foreach ($original_selects as $alias => $select_configuration) { |
|
| 4093 | - $this->extractJoinModelFromQueryParams( |
|
| 4094 | - $query_info_carrier, |
|
| 4095 | - $select_configuration[0], |
|
| 4096 | - $select_configuration[0], |
|
| 4097 | - 'custom_selects' |
|
| 4098 | - ); |
|
| 4099 | - } |
|
| 4100 | - } |
|
| 4101 | - } |
|
| 4102 | - |
|
| 4103 | - |
|
| 4104 | - |
|
| 4105 | - /** |
|
| 4106 | - * Privately used by _extract_related_model_info_from_query_param to add a join to $model_name |
|
| 4107 | - * and store it on $passed_in_query_info |
|
| 4108 | - * |
|
| 4109 | - * @param string $model_name |
|
| 4110 | - * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
| 4111 | - * @param string $original_query_param used to extract the relation chain between the queried |
|
| 4112 | - * model and $model_name. Eg, if we are querying Event, |
|
| 4113 | - * and are adding a join to 'Payment' with the original |
|
| 4114 | - * query param key |
|
| 4115 | - * 'Registration.Transaction.Payment.PAY_amount', we want |
|
| 4116 | - * to extract 'Registration.Transaction.Payment', in case |
|
| 4117 | - * Payment wants to add default query params so that it |
|
| 4118 | - * will know what models to prepend onto its default query |
|
| 4119 | - * params or in case it wants to rename tables (in case |
|
| 4120 | - * there are multiple joins to the same table) |
|
| 4121 | - * @return void |
|
| 4122 | - * @throws EE_Error |
|
| 4123 | - */ |
|
| 4124 | - private function _add_join_to_model( |
|
| 4125 | - $model_name, |
|
| 4126 | - EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
| 4127 | - $original_query_param |
|
| 4128 | - ) { |
|
| 4129 | - $relation_obj = $this->related_settings_for($model_name); |
|
| 4130 | - $model_relation_chain = EE_Model_Parser::extract_model_relation_chain($model_name, $original_query_param); |
|
| 4131 | - // check if the relation is HABTM, because then we're essentially doing two joins |
|
| 4132 | - // If so, join first to the JOIN table, and add its data types, and then continue as normal |
|
| 4133 | - if ($relation_obj instanceof EE_HABTM_Relation) { |
|
| 4134 | - $join_model_obj = $relation_obj->get_join_model(); |
|
| 4135 | - // replace the model specified with the join model for this relation chain, whi |
|
| 4136 | - $relation_chain_to_join_model = EE_Model_Parser::replace_model_name_with_join_model_name_in_model_relation_chain( |
|
| 4137 | - $model_name, |
|
| 4138 | - $join_model_obj->get_this_model_name(), |
|
| 4139 | - $model_relation_chain |
|
| 4140 | - ); |
|
| 4141 | - $passed_in_query_info->merge( |
|
| 4142 | - new EE_Model_Query_Info_Carrier( |
|
| 4143 | - array($relation_chain_to_join_model => $join_model_obj->get_this_model_name()), |
|
| 4144 | - $relation_obj->get_join_to_intermediate_model_statement($relation_chain_to_join_model) |
|
| 4145 | - ) |
|
| 4146 | - ); |
|
| 4147 | - } |
|
| 4148 | - // now just join to the other table pointed to by the relation object, and add its data types |
|
| 4149 | - $passed_in_query_info->merge( |
|
| 4150 | - new EE_Model_Query_Info_Carrier( |
|
| 4151 | - array($model_relation_chain => $model_name), |
|
| 4152 | - $relation_obj->get_join_statement($model_relation_chain) |
|
| 4153 | - ) |
|
| 4154 | - ); |
|
| 4155 | - } |
|
| 4156 | - |
|
| 4157 | - |
|
| 4158 | - |
|
| 4159 | - /** |
|
| 4160 | - * Constructs SQL for where clause, like "WHERE Event.ID = 23 AND Transaction.amount > 100" etc. |
|
| 4161 | - * |
|
| 4162 | - * @param array $where_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 4163 | - * @return string of SQL |
|
| 4164 | - * @throws EE_Error |
|
| 4165 | - */ |
|
| 4166 | - private function _construct_where_clause($where_params) |
|
| 4167 | - { |
|
| 4168 | - $SQL = $this->_construct_condition_clause_recursive($where_params, ' AND '); |
|
| 4169 | - if ($SQL) { |
|
| 4170 | - return " WHERE " . $SQL; |
|
| 4171 | - } |
|
| 4172 | - return ''; |
|
| 4173 | - } |
|
| 4174 | - |
|
| 4175 | - |
|
| 4176 | - |
|
| 4177 | - /** |
|
| 4178 | - * Just like the _construct_where_clause, except prepends 'HAVING' instead of 'WHERE', |
|
| 4179 | - * and should be passed HAVING parameters, not WHERE parameters |
|
| 4180 | - * |
|
| 4181 | - * @param array $having_params |
|
| 4182 | - * @return string |
|
| 4183 | - * @throws EE_Error |
|
| 4184 | - */ |
|
| 4185 | - private function _construct_having_clause($having_params) |
|
| 4186 | - { |
|
| 4187 | - $SQL = $this->_construct_condition_clause_recursive($having_params, ' AND '); |
|
| 4188 | - if ($SQL) { |
|
| 4189 | - return " HAVING " . $SQL; |
|
| 4190 | - } |
|
| 4191 | - return ''; |
|
| 4192 | - } |
|
| 4193 | - |
|
| 4194 | - |
|
| 4195 | - /** |
|
| 4196 | - * Used for creating nested WHERE conditions. Eg "WHERE ! (Event.ID = 3 OR ( Event_Meta.meta_key = 'bob' AND |
|
| 4197 | - * Event_Meta.meta_value = 'foo'))" |
|
| 4198 | - * |
|
| 4199 | - * @param array $where_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 4200 | - * @param string $glue joins each subclause together. Should really only be " AND " or " OR "... |
|
| 4201 | - * @throws EE_Error |
|
| 4202 | - * @return string of SQL |
|
| 4203 | - */ |
|
| 4204 | - private function _construct_condition_clause_recursive($where_params, $glue = ' AND') |
|
| 4205 | - { |
|
| 4206 | - $where_clauses = array(); |
|
| 4207 | - foreach ($where_params as $query_param => $op_and_value_or_sub_condition) { |
|
| 4208 | - $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param);// str_replace("*",'',$query_param); |
|
| 4209 | - if (in_array($query_param, $this->_logic_query_param_keys)) { |
|
| 4210 | - switch ($query_param) { |
|
| 4211 | - case 'not': |
|
| 4212 | - case 'NOT': |
|
| 4213 | - $where_clauses[] = "! (" |
|
| 4214 | - . $this->_construct_condition_clause_recursive( |
|
| 4215 | - $op_and_value_or_sub_condition, |
|
| 4216 | - $glue |
|
| 4217 | - ) |
|
| 4218 | - . ")"; |
|
| 4219 | - break; |
|
| 4220 | - case 'and': |
|
| 4221 | - case 'AND': |
|
| 4222 | - $where_clauses[] = " (" |
|
| 4223 | - . $this->_construct_condition_clause_recursive( |
|
| 4224 | - $op_and_value_or_sub_condition, |
|
| 4225 | - ' AND ' |
|
| 4226 | - ) |
|
| 4227 | - . ")"; |
|
| 4228 | - break; |
|
| 4229 | - case 'or': |
|
| 4230 | - case 'OR': |
|
| 4231 | - $where_clauses[] = " (" |
|
| 4232 | - . $this->_construct_condition_clause_recursive( |
|
| 4233 | - $op_and_value_or_sub_condition, |
|
| 4234 | - ' OR ' |
|
| 4235 | - ) |
|
| 4236 | - . ")"; |
|
| 4237 | - break; |
|
| 4238 | - } |
|
| 4239 | - } else { |
|
| 4240 | - $field_obj = $this->_deduce_field_from_query_param($query_param); |
|
| 4241 | - // if it's not a normal field, maybe it's a custom selection? |
|
| 4242 | - if (! $field_obj) { |
|
| 4243 | - if ($this->_custom_selections instanceof CustomSelects) { |
|
| 4244 | - $field_obj = $this->_custom_selections->getDataTypeForAlias($query_param); |
|
| 4245 | - } else { |
|
| 4246 | - throw new EE_Error(sprintf(__( |
|
| 4247 | - "%s is neither a valid model field name, nor a custom selection", |
|
| 4248 | - "event_espresso" |
|
| 4249 | - ), $query_param)); |
|
| 4250 | - } |
|
| 4251 | - } |
|
| 4252 | - $op_and_value_sql = $this->_construct_op_and_value($op_and_value_or_sub_condition, $field_obj); |
|
| 4253 | - $where_clauses[] = $this->_deduce_column_name_from_query_param($query_param) . SP . $op_and_value_sql; |
|
| 4254 | - } |
|
| 4255 | - } |
|
| 4256 | - return $where_clauses ? implode($glue, $where_clauses) : ''; |
|
| 4257 | - } |
|
| 4258 | - |
|
| 4259 | - |
|
| 4260 | - |
|
| 4261 | - /** |
|
| 4262 | - * Takes the input parameter and extract the table name (alias) and column name |
|
| 4263 | - * |
|
| 4264 | - * @param string $query_param like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
| 4265 | - * @throws EE_Error |
|
| 4266 | - * @return string table alias and column name for SQL, eg "Transaction.TXN_ID" |
|
| 4267 | - */ |
|
| 4268 | - private function _deduce_column_name_from_query_param($query_param) |
|
| 4269 | - { |
|
| 4270 | - $field = $this->_deduce_field_from_query_param($query_param); |
|
| 4271 | - if ($field) { |
|
| 4272 | - $table_alias_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_from_query_param( |
|
| 4273 | - $field->get_model_name(), |
|
| 4274 | - $query_param |
|
| 4275 | - ); |
|
| 4276 | - return $table_alias_prefix . $field->get_qualified_column(); |
|
| 4277 | - } |
|
| 4278 | - if ($this->_custom_selections instanceof CustomSelects |
|
| 4279 | - && in_array($query_param, $this->_custom_selections->columnAliases(), true) |
|
| 4280 | - ) { |
|
| 4281 | - // maybe it's custom selection item? |
|
| 4282 | - // if so, just use it as the "column name" |
|
| 4283 | - return $query_param; |
|
| 4284 | - } |
|
| 4285 | - $custom_select_aliases = $this->_custom_selections instanceof CustomSelects |
|
| 4286 | - ? implode(',', $this->_custom_selections->columnAliases()) |
|
| 4287 | - : ''; |
|
| 4288 | - throw new EE_Error( |
|
| 4289 | - sprintf( |
|
| 4290 | - __( |
|
| 4291 | - "%s is not a valid field on this model, nor a custom selection (%s)", |
|
| 4292 | - "event_espresso" |
|
| 4293 | - ), |
|
| 4294 | - $query_param, |
|
| 4295 | - $custom_select_aliases |
|
| 4296 | - ) |
|
| 4297 | - ); |
|
| 4298 | - } |
|
| 4299 | - |
|
| 4300 | - |
|
| 4301 | - |
|
| 4302 | - /** |
|
| 4303 | - * Removes the * and anything after it from the condition query param key. It is useful to add the * to condition |
|
| 4304 | - * query param keys (eg, 'OR*', 'EVT_ID') in order for the array keys to still be unique, so that they don't get |
|
| 4305 | - * overwritten Takes a string like 'Event.EVT_ID*', 'TXN_total**', 'OR*1st', and 'DTT_reg_start*foobar' to |
|
| 4306 | - * 'Event.EVT_ID', 'TXN_total', 'OR', and 'DTT_reg_start', respectively. |
|
| 4307 | - * |
|
| 4308 | - * @param string $condition_query_param_key |
|
| 4309 | - * @return string |
|
| 4310 | - */ |
|
| 4311 | - private function _remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
| 4312 | - { |
|
| 4313 | - $pos_of_star = strpos($condition_query_param_key, '*'); |
|
| 4314 | - if ($pos_of_star === false) { |
|
| 4315 | - return $condition_query_param_key; |
|
| 4316 | - } |
|
| 4317 | - $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
| 4318 | - return $condition_query_param_sans_star; |
|
| 4319 | - } |
|
| 4320 | - |
|
| 4321 | - |
|
| 4322 | - |
|
| 4323 | - /** |
|
| 4324 | - * creates the SQL for the operator and the value in a WHERE clause, eg "< 23" or "LIKE '%monkey%'" |
|
| 4325 | - * |
|
| 4326 | - * @param mixed array | string $op_and_value |
|
| 4327 | - * @param EE_Model_Field_Base|string $field_obj . If string, should be one of EEM_Base::_valid_wpdb_data_types |
|
| 4328 | - * @throws EE_Error |
|
| 4329 | - * @return string |
|
| 4330 | - */ |
|
| 4331 | - private function _construct_op_and_value($op_and_value, $field_obj) |
|
| 4332 | - { |
|
| 4333 | - if (is_array($op_and_value)) { |
|
| 4334 | - $operator = isset($op_and_value[0]) ? $this->_prepare_operator_for_sql($op_and_value[0]) : null; |
|
| 4335 | - if (! $operator) { |
|
| 4336 | - $php_array_like_string = array(); |
|
| 4337 | - foreach ($op_and_value as $key => $value) { |
|
| 4338 | - $php_array_like_string[] = "$key=>$value"; |
|
| 4339 | - } |
|
| 4340 | - throw new EE_Error( |
|
| 4341 | - sprintf( |
|
| 4342 | - __( |
|
| 4343 | - "You setup a query parameter like you were going to specify an operator, but didn't. You provided '(%s)', but the operator should be at array key index 0 (eg array('>',32))", |
|
| 4344 | - "event_espresso" |
|
| 4345 | - ), |
|
| 4346 | - implode(",", $php_array_like_string) |
|
| 4347 | - ) |
|
| 4348 | - ); |
|
| 4349 | - } |
|
| 4350 | - $value = isset($op_and_value[1]) ? $op_and_value[1] : null; |
|
| 4351 | - } else { |
|
| 4352 | - $operator = '='; |
|
| 4353 | - $value = $op_and_value; |
|
| 4354 | - } |
|
| 4355 | - // check to see if the value is actually another field |
|
| 4356 | - if (is_array($op_and_value) && isset($op_and_value[2]) && $op_and_value[2] == true) { |
|
| 4357 | - return $operator . SP . $this->_deduce_column_name_from_query_param($value); |
|
| 4358 | - } |
|
| 4359 | - if (in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
| 4360 | - // in this case, the value should be an array, or at least a comma-separated list |
|
| 4361 | - // it will need to handle a little differently |
|
| 4362 | - $cleaned_value = $this->_construct_in_value($value, $field_obj); |
|
| 4363 | - // note: $cleaned_value has already been run through $wpdb->prepare() |
|
| 4364 | - return $operator . SP . $cleaned_value; |
|
| 4365 | - } |
|
| 4366 | - if (in_array($operator, $this->valid_between_style_operators()) && is_array($value)) { |
|
| 4367 | - // the value should be an array with count of two. |
|
| 4368 | - if (count($value) !== 2) { |
|
| 4369 | - throw new EE_Error( |
|
| 4370 | - sprintf( |
|
| 4371 | - __( |
|
| 4372 | - "The '%s' operator must be used with an array of values and there must be exactly TWO values in that array.", |
|
| 4373 | - 'event_espresso' |
|
| 4374 | - ), |
|
| 4375 | - "BETWEEN" |
|
| 4376 | - ) |
|
| 4377 | - ); |
|
| 4378 | - } |
|
| 4379 | - $cleaned_value = $this->_construct_between_value($value, $field_obj); |
|
| 4380 | - return $operator . SP . $cleaned_value; |
|
| 4381 | - } |
|
| 4382 | - if (in_array($operator, $this->valid_null_style_operators())) { |
|
| 4383 | - if ($value !== null) { |
|
| 4384 | - throw new EE_Error( |
|
| 4385 | - sprintf( |
|
| 4386 | - __( |
|
| 4387 | - "You attempted to give a value (%s) while using a NULL-style operator (%s). That isn't valid", |
|
| 4388 | - "event_espresso" |
|
| 4389 | - ), |
|
| 4390 | - $value, |
|
| 4391 | - $operator |
|
| 4392 | - ) |
|
| 4393 | - ); |
|
| 4394 | - } |
|
| 4395 | - return $operator; |
|
| 4396 | - } |
|
| 4397 | - if (in_array($operator, $this->valid_like_style_operators()) && ! is_array($value)) { |
|
| 4398 | - // if the operator is 'LIKE', we want to allow percent signs (%) and not |
|
| 4399 | - // remove other junk. So just treat it as a string. |
|
| 4400 | - return $operator . SP . $this->_wpdb_prepare_using_field($value, '%s'); |
|
| 4401 | - } |
|
| 4402 | - if (! in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
| 4403 | - return $operator . SP . $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4404 | - } |
|
| 4405 | - if (in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
| 4406 | - throw new EE_Error( |
|
| 4407 | - sprintf( |
|
| 4408 | - __( |
|
| 4409 | - "Operator '%s' must be used with an array of values, eg 'Registration.REG_ID' => array('%s',array(1,2,3))", |
|
| 4410 | - 'event_espresso' |
|
| 4411 | - ), |
|
| 4412 | - $operator, |
|
| 4413 | - $operator |
|
| 4414 | - ) |
|
| 4415 | - ); |
|
| 4416 | - } |
|
| 4417 | - if (! in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
| 4418 | - throw new EE_Error( |
|
| 4419 | - sprintf( |
|
| 4420 | - __( |
|
| 4421 | - "Operator '%s' must be used with a single value, not an array. Eg 'Registration.REG_ID => array('%s',23))", |
|
| 4422 | - 'event_espresso' |
|
| 4423 | - ), |
|
| 4424 | - $operator, |
|
| 4425 | - $operator |
|
| 4426 | - ) |
|
| 4427 | - ); |
|
| 4428 | - } |
|
| 4429 | - throw new EE_Error( |
|
| 4430 | - sprintf( |
|
| 4431 | - __( |
|
| 4432 | - "It appears you've provided some totally invalid query parameters. Operator and value were:'%s', which isn't right at all", |
|
| 4433 | - "event_espresso" |
|
| 4434 | - ), |
|
| 4435 | - http_build_query($op_and_value) |
|
| 4436 | - ) |
|
| 4437 | - ); |
|
| 4438 | - } |
|
| 4439 | - |
|
| 4440 | - |
|
| 4441 | - |
|
| 4442 | - /** |
|
| 4443 | - * Creates the operands to be used in a BETWEEN query, eg "'2014-12-31 20:23:33' AND '2015-01-23 12:32:54'" |
|
| 4444 | - * |
|
| 4445 | - * @param array $values |
|
| 4446 | - * @param EE_Model_Field_Base|string $field_obj if string, it should be the datatype to be used when querying, eg |
|
| 4447 | - * '%s' |
|
| 4448 | - * @return string |
|
| 4449 | - * @throws EE_Error |
|
| 4450 | - */ |
|
| 4451 | - public function _construct_between_value($values, $field_obj) |
|
| 4452 | - { |
|
| 4453 | - $cleaned_values = array(); |
|
| 4454 | - foreach ($values as $value) { |
|
| 4455 | - $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4456 | - } |
|
| 4457 | - return $cleaned_values[0] . " AND " . $cleaned_values[1]; |
|
| 4458 | - } |
|
| 4459 | - |
|
| 4460 | - |
|
| 4461 | - |
|
| 4462 | - /** |
|
| 4463 | - * Takes an array or a comma-separated list of $values and cleans them |
|
| 4464 | - * according to $data_type using $wpdb->prepare, and then makes the list a |
|
| 4465 | - * string surrounded by ( and ). Eg, _construct_in_value(array(1,2,3),'%d') would |
|
| 4466 | - * return '(1,2,3)'; _construct_in_value("1,2,hack",'%d') would return '(1,2,1)' (assuming |
|
| 4467 | - * I'm right that a string, when interpreted as a digit, becomes a 1. It might become a 0) |
|
| 4468 | - * |
|
| 4469 | - * @param mixed $values array or comma-separated string |
|
| 4470 | - * @param EE_Model_Field_Base|string $field_obj if string, it should be a wpdb data type like '%s', or '%d' |
|
| 4471 | - * @return string of SQL to follow an 'IN' or 'NOT IN' operator |
|
| 4472 | - * @throws EE_Error |
|
| 4473 | - */ |
|
| 4474 | - public function _construct_in_value($values, $field_obj) |
|
| 4475 | - { |
|
| 4476 | - // check if the value is a CSV list |
|
| 4477 | - if (is_string($values)) { |
|
| 4478 | - // in which case, turn it into an array |
|
| 4479 | - $values = explode(",", $values); |
|
| 4480 | - } |
|
| 4481 | - $cleaned_values = array(); |
|
| 4482 | - foreach ($values as $value) { |
|
| 4483 | - $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4484 | - } |
|
| 4485 | - // we would just LOVE to leave $cleaned_values as an empty array, and return the value as "()", |
|
| 4486 | - // but unfortunately that's invalid SQL. So instead we return a string which we KNOW will evaluate to be the empty set |
|
| 4487 | - // which is effectively equivalent to returning "()". We don't return "(0)" because that only works for auto-incrementing columns |
|
| 4488 | - if (empty($cleaned_values)) { |
|
| 4489 | - $all_fields = $this->field_settings(); |
|
| 4490 | - $a_field = array_shift($all_fields); |
|
| 4491 | - $main_table = $this->_get_main_table(); |
|
| 4492 | - $cleaned_values[] = "SELECT " |
|
| 4493 | - . $a_field->get_table_column() |
|
| 4494 | - . " FROM " |
|
| 4495 | - . $main_table->get_table_name() |
|
| 4496 | - . " WHERE FALSE"; |
|
| 4497 | - } |
|
| 4498 | - return "(" . implode(",", $cleaned_values) . ")"; |
|
| 4499 | - } |
|
| 4500 | - |
|
| 4501 | - |
|
| 4502 | - |
|
| 4503 | - /** |
|
| 4504 | - * @param mixed $value |
|
| 4505 | - * @param EE_Model_Field_Base|string $field_obj if string it should be a wpdb data type like '%d' |
|
| 4506 | - * @throws EE_Error |
|
| 4507 | - * @return false|null|string |
|
| 4508 | - */ |
|
| 4509 | - private function _wpdb_prepare_using_field($value, $field_obj) |
|
| 4510 | - { |
|
| 4511 | - /** @type WPDB $wpdb */ |
|
| 4512 | - global $wpdb; |
|
| 4513 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 4514 | - return $wpdb->prepare( |
|
| 4515 | - $field_obj->get_wpdb_data_type(), |
|
| 4516 | - $this->_prepare_value_for_use_in_db($value, $field_obj) |
|
| 4517 | - ); |
|
| 4518 | - } //$field_obj should really just be a data type |
|
| 4519 | - if (! in_array($field_obj, $this->_valid_wpdb_data_types)) { |
|
| 4520 | - throw new EE_Error( |
|
| 4521 | - sprintf( |
|
| 4522 | - __("%s is not a valid wpdb datatype. Valid ones are %s", "event_espresso"), |
|
| 4523 | - $field_obj, |
|
| 4524 | - implode(",", $this->_valid_wpdb_data_types) |
|
| 4525 | - ) |
|
| 4526 | - ); |
|
| 4527 | - } |
|
| 4528 | - return $wpdb->prepare($field_obj, $value); |
|
| 4529 | - } |
|
| 4530 | - |
|
| 4531 | - |
|
| 4532 | - |
|
| 4533 | - /** |
|
| 4534 | - * Takes the input parameter and finds the model field that it indicates. |
|
| 4535 | - * |
|
| 4536 | - * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
| 4537 | - * @throws EE_Error |
|
| 4538 | - * @return EE_Model_Field_Base |
|
| 4539 | - */ |
|
| 4540 | - protected function _deduce_field_from_query_param($query_param_name) |
|
| 4541 | - { |
|
| 4542 | - // ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
| 4543 | - // which will help us find the database table and column |
|
| 4544 | - $query_param_parts = explode(".", $query_param_name); |
|
| 4545 | - if (empty($query_param_parts)) { |
|
| 4546 | - throw new EE_Error(sprintf(__( |
|
| 4547 | - "_extract_column_name is empty when trying to extract column and table name from %s", |
|
| 4548 | - 'event_espresso' |
|
| 4549 | - ), $query_param_name)); |
|
| 4550 | - } |
|
| 4551 | - $number_of_parts = count($query_param_parts); |
|
| 4552 | - $last_query_param_part = $query_param_parts[ count($query_param_parts) - 1 ]; |
|
| 4553 | - if ($number_of_parts === 1) { |
|
| 4554 | - $field_name = $last_query_param_part; |
|
| 4555 | - $model_obj = $this; |
|
| 4556 | - } else {// $number_of_parts >= 2 |
|
| 4557 | - // the last part is the column name, and there are only 2parts. therefore... |
|
| 4558 | - $field_name = $last_query_param_part; |
|
| 4559 | - $model_obj = $this->get_related_model_obj($query_param_parts[ $number_of_parts - 2 ]); |
|
| 4560 | - } |
|
| 4561 | - try { |
|
| 4562 | - return $model_obj->field_settings_for($field_name); |
|
| 4563 | - } catch (EE_Error $e) { |
|
| 4564 | - return null; |
|
| 4565 | - } |
|
| 4566 | - } |
|
| 4567 | - |
|
| 4568 | - |
|
| 4569 | - |
|
| 4570 | - /** |
|
| 4571 | - * Given a field's name (ie, a key in $this->field_settings()), uses the EE_Model_Field object to get the table's |
|
| 4572 | - * alias and column which corresponds to it |
|
| 4573 | - * |
|
| 4574 | - * @param string $field_name |
|
| 4575 | - * @throws EE_Error |
|
| 4576 | - * @return string |
|
| 4577 | - */ |
|
| 4578 | - public function _get_qualified_column_for_field($field_name) |
|
| 4579 | - { |
|
| 4580 | - $all_fields = $this->field_settings(); |
|
| 4581 | - $field = isset($all_fields[ $field_name ]) ? $all_fields[ $field_name ] : false; |
|
| 4582 | - if ($field) { |
|
| 4583 | - return $field->get_qualified_column(); |
|
| 4584 | - } |
|
| 4585 | - throw new EE_Error( |
|
| 4586 | - sprintf( |
|
| 4587 | - __( |
|
| 4588 | - "There is no field titled %s on model %s. Either the query trying to use it is bad, or you need to add it to the list of fields on the model.", |
|
| 4589 | - 'event_espresso' |
|
| 4590 | - ), |
|
| 4591 | - $field_name, |
|
| 4592 | - get_class($this) |
|
| 4593 | - ) |
|
| 4594 | - ); |
|
| 4595 | - } |
|
| 4596 | - |
|
| 4597 | - |
|
| 4598 | - |
|
| 4599 | - /** |
|
| 4600 | - * similar to \EEM_Base::_get_qualified_column_for_field() but returns an array with data for ALL fields. |
|
| 4601 | - * Example usage: |
|
| 4602 | - * EEM_Ticket::instance()->get_all_wpdb_results( |
|
| 4603 | - * array(), |
|
| 4604 | - * ARRAY_A, |
|
| 4605 | - * EEM_Ticket::instance()->get_qualified_columns_for_all_fields() |
|
| 4606 | - * ); |
|
| 4607 | - * is equivalent to |
|
| 4608 | - * EEM_Ticket::instance()->get_all_wpdb_results( array(), ARRAY_A, '*' ); |
|
| 4609 | - * and |
|
| 4610 | - * EEM_Event::instance()->get_all_wpdb_results( |
|
| 4611 | - * array( |
|
| 4612 | - * array( |
|
| 4613 | - * 'Datetime.Ticket.TKT_ID' => array( '<', 100 ), |
|
| 4614 | - * ), |
|
| 4615 | - * ARRAY_A, |
|
| 4616 | - * implode( |
|
| 4617 | - * ', ', |
|
| 4618 | - * array_merge( |
|
| 4619 | - * EEM_Event::instance()->get_qualified_columns_for_all_fields( '', false ), |
|
| 4620 | - * EEM_Ticket::instance()->get_qualified_columns_for_all_fields( 'Datetime', false ) |
|
| 4621 | - * ) |
|
| 4622 | - * ) |
|
| 4623 | - * ) |
|
| 4624 | - * ); |
|
| 4625 | - * selects rows from the database, selecting all the event and ticket columns, where the ticket ID is below 100 |
|
| 4626 | - * |
|
| 4627 | - * @param string $model_relation_chain the chain of models used to join between the model you want to query |
|
| 4628 | - * and the one whose fields you are selecting for example: when querying |
|
| 4629 | - * tickets model and selecting fields from the tickets model you would |
|
| 4630 | - * leave this parameter empty, because no models are needed to join |
|
| 4631 | - * between the queried model and the selected one. Likewise when |
|
| 4632 | - * querying the datetime model and selecting fields from the tickets |
|
| 4633 | - * model, it would also be left empty, because there is a direct |
|
| 4634 | - * relation from datetimes to tickets, so no model is needed to join |
|
| 4635 | - * them together. However, when querying from the event model and |
|
| 4636 | - * selecting fields from the ticket model, you should provide the string |
|
| 4637 | - * 'Datetime', indicating that the event model must first join to the |
|
| 4638 | - * datetime model in order to find its relation to ticket model. |
|
| 4639 | - * Also, when querying from the venue model and selecting fields from |
|
| 4640 | - * the ticket model, you should provide the string 'Event.Datetime', |
|
| 4641 | - * indicating you need to join the venue model to the event model, |
|
| 4642 | - * to the datetime model, in order to find its relation to the ticket model. |
|
| 4643 | - * This string is used to deduce the prefix that gets added onto the |
|
| 4644 | - * models' tables qualified columns |
|
| 4645 | - * @param bool $return_string if true, will return a string with qualified column names separated |
|
| 4646 | - * by ', ' if false, will simply return a numerically indexed array of |
|
| 4647 | - * qualified column names |
|
| 4648 | - * @return array|string |
|
| 4649 | - */ |
|
| 4650 | - public function get_qualified_columns_for_all_fields($model_relation_chain = '', $return_string = true) |
|
| 4651 | - { |
|
| 4652 | - $table_prefix = str_replace('.', '__', $model_relation_chain) . (empty($model_relation_chain) ? '' : '__'); |
|
| 4653 | - $qualified_columns = array(); |
|
| 4654 | - foreach ($this->field_settings() as $field_name => $field) { |
|
| 4655 | - $qualified_columns[] = $table_prefix . $field->get_qualified_column(); |
|
| 4656 | - } |
|
| 4657 | - return $return_string ? implode(', ', $qualified_columns) : $qualified_columns; |
|
| 4658 | - } |
|
| 4659 | - |
|
| 4660 | - |
|
| 4661 | - |
|
| 4662 | - /** |
|
| 4663 | - * constructs the select use on special limit joins |
|
| 4664 | - * NOTE: for now this has only been tested and will work when the table alias is for the PRIMARY table. Although |
|
| 4665 | - * its setup so the select query will be setup on and just doing the special select join off of the primary table |
|
| 4666 | - * (as that is typically where the limits would be set). |
|
| 4667 | - * |
|
| 4668 | - * @param string $table_alias The table the select is being built for |
|
| 4669 | - * @param mixed|string $limit The limit for this select |
|
| 4670 | - * @return string The final select join element for the query. |
|
| 4671 | - */ |
|
| 4672 | - public function _construct_limit_join_select($table_alias, $limit) |
|
| 4673 | - { |
|
| 4674 | - $SQL = ''; |
|
| 4675 | - foreach ($this->_tables as $table_obj) { |
|
| 4676 | - if ($table_obj instanceof EE_Primary_Table) { |
|
| 4677 | - $SQL .= $table_alias === $table_obj->get_table_alias() |
|
| 4678 | - ? $table_obj->get_select_join_limit($limit) |
|
| 4679 | - : SP . $table_obj->get_table_name() . " AS " . $table_obj->get_table_alias() . SP; |
|
| 4680 | - } elseif ($table_obj instanceof EE_Secondary_Table) { |
|
| 4681 | - $SQL .= $table_alias === $table_obj->get_table_alias() |
|
| 4682 | - ? $table_obj->get_select_join_limit_join($limit) |
|
| 4683 | - : SP . $table_obj->get_join_sql($table_alias) . SP; |
|
| 4684 | - } |
|
| 4685 | - } |
|
| 4686 | - return $SQL; |
|
| 4687 | - } |
|
| 4688 | - |
|
| 4689 | - |
|
| 4690 | - |
|
| 4691 | - /** |
|
| 4692 | - * Constructs the internal join if there are multiple tables, or simply the table's name and alias |
|
| 4693 | - * Eg "wp_post AS Event" or "wp_post AS Event INNER JOIN wp_postmeta Event_Meta ON Event.ID = Event_Meta.post_id" |
|
| 4694 | - * |
|
| 4695 | - * @return string SQL |
|
| 4696 | - * @throws EE_Error |
|
| 4697 | - */ |
|
| 4698 | - public function _construct_internal_join() |
|
| 4699 | - { |
|
| 4700 | - $SQL = $this->_get_main_table()->get_table_sql(); |
|
| 4701 | - $SQL .= $this->_construct_internal_join_to_table_with_alias($this->_get_main_table()->get_table_alias()); |
|
| 4702 | - return $SQL; |
|
| 4703 | - } |
|
| 4704 | - |
|
| 4705 | - |
|
| 4706 | - |
|
| 4707 | - /** |
|
| 4708 | - * Constructs the SQL for joining all the tables on this model. |
|
| 4709 | - * Normally $alias should be the primary table's alias, but in cases where |
|
| 4710 | - * we have already joined to a secondary table (eg, the secondary table has a foreign key and is joined before the |
|
| 4711 | - * primary table) then we should provide that secondary table's alias. Eg, with $alias being the primary table's |
|
| 4712 | - * alias, this will construct SQL like: |
|
| 4713 | - * " INNER JOIN wp_esp_secondary_table AS Secondary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
| 4714 | - * With $alias being a secondary table's alias, this will construct SQL like: |
|
| 4715 | - * " INNER JOIN wp_esp_primary_table AS Primary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
| 4716 | - * |
|
| 4717 | - * @param string $alias_prefixed table alias to join to (this table should already be in the FROM SQL clause) |
|
| 4718 | - * @return string |
|
| 4719 | - */ |
|
| 4720 | - public function _construct_internal_join_to_table_with_alias($alias_prefixed) |
|
| 4721 | - { |
|
| 4722 | - $SQL = ''; |
|
| 4723 | - $alias_sans_prefix = EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($alias_prefixed); |
|
| 4724 | - foreach ($this->_tables as $table_obj) { |
|
| 4725 | - if ($table_obj instanceof EE_Secondary_Table) {// table is secondary table |
|
| 4726 | - if ($alias_sans_prefix === $table_obj->get_table_alias()) { |
|
| 4727 | - // so we're joining to this table, meaning the table is already in |
|
| 4728 | - // the FROM statement, BUT the primary table isn't. So we want |
|
| 4729 | - // to add the inverse join sql |
|
| 4730 | - $SQL .= $table_obj->get_inverse_join_sql($alias_prefixed); |
|
| 4731 | - } else { |
|
| 4732 | - // just add a regular JOIN to this table from the primary table |
|
| 4733 | - $SQL .= $table_obj->get_join_sql($alias_prefixed); |
|
| 4734 | - } |
|
| 4735 | - }//if it's a primary table, dont add any SQL. it should already be in the FROM statement |
|
| 4736 | - } |
|
| 4737 | - return $SQL; |
|
| 4738 | - } |
|
| 4739 | - |
|
| 4740 | - |
|
| 4741 | - |
|
| 4742 | - /** |
|
| 4743 | - * Gets an array for storing all the data types on the next-to-be-executed-query. |
|
| 4744 | - * This should be a growing array of keys being table-columns (eg 'EVT_ID' and 'Event.EVT_ID'), and values being |
|
| 4745 | - * their data type (eg, '%s', '%d', etc) |
|
| 4746 | - * |
|
| 4747 | - * @return array |
|
| 4748 | - */ |
|
| 4749 | - public function _get_data_types() |
|
| 4750 | - { |
|
| 4751 | - $data_types = array(); |
|
| 4752 | - foreach ($this->field_settings() as $field_obj) { |
|
| 4753 | - // $data_types[$field_obj->get_table_column()] = $field_obj->get_wpdb_data_type(); |
|
| 4754 | - /** @var $field_obj EE_Model_Field_Base */ |
|
| 4755 | - $data_types[ $field_obj->get_qualified_column() ] = $field_obj->get_wpdb_data_type(); |
|
| 4756 | - } |
|
| 4757 | - return $data_types; |
|
| 4758 | - } |
|
| 4759 | - |
|
| 4760 | - |
|
| 4761 | - |
|
| 4762 | - /** |
|
| 4763 | - * Gets the model object given the relation's name / model's name (eg, 'Event', 'Registration',etc. Always singular) |
|
| 4764 | - * |
|
| 4765 | - * @param string $model_name |
|
| 4766 | - * @throws EE_Error |
|
| 4767 | - * @return EEM_Base |
|
| 4768 | - */ |
|
| 4769 | - public function get_related_model_obj($model_name) |
|
| 4770 | - { |
|
| 4771 | - $model_classname = "EEM_" . $model_name; |
|
| 4772 | - if (! class_exists($model_classname)) { |
|
| 4773 | - throw new EE_Error(sprintf(__( |
|
| 4774 | - "You specified a related model named %s in your query. No such model exists, if it did, it would have the classname %s", |
|
| 4775 | - 'event_espresso' |
|
| 4776 | - ), $model_name, $model_classname)); |
|
| 4777 | - } |
|
| 4778 | - return call_user_func($model_classname . "::instance"); |
|
| 4779 | - } |
|
| 4780 | - |
|
| 4781 | - |
|
| 4782 | - |
|
| 4783 | - /** |
|
| 4784 | - * Returns the array of EE_ModelRelations for this model. |
|
| 4785 | - * |
|
| 4786 | - * @return EE_Model_Relation_Base[] |
|
| 4787 | - */ |
|
| 4788 | - public function relation_settings() |
|
| 4789 | - { |
|
| 4790 | - return $this->_model_relations; |
|
| 4791 | - } |
|
| 4792 | - |
|
| 4793 | - |
|
| 4794 | - |
|
| 4795 | - /** |
|
| 4796 | - * Gets all related models that this model BELONGS TO. Handy to know sometimes |
|
| 4797 | - * because without THOSE models, this model probably doesn't have much purpose. |
|
| 4798 | - * (Eg, without an event, datetimes have little purpose.) |
|
| 4799 | - * |
|
| 4800 | - * @return EE_Belongs_To_Relation[] |
|
| 4801 | - */ |
|
| 4802 | - public function belongs_to_relations() |
|
| 4803 | - { |
|
| 4804 | - $belongs_to_relations = array(); |
|
| 4805 | - foreach ($this->relation_settings() as $model_name => $relation_obj) { |
|
| 4806 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 4807 | - $belongs_to_relations[ $model_name ] = $relation_obj; |
|
| 4808 | - } |
|
| 4809 | - } |
|
| 4810 | - return $belongs_to_relations; |
|
| 4811 | - } |
|
| 4812 | - |
|
| 4813 | - |
|
| 4814 | - |
|
| 4815 | - /** |
|
| 4816 | - * Returns the specified EE_Model_Relation, or throws an exception |
|
| 4817 | - * |
|
| 4818 | - * @param string $relation_name name of relation, key in $this->_relatedModels |
|
| 4819 | - * @throws EE_Error |
|
| 4820 | - * @return EE_Model_Relation_Base |
|
| 4821 | - */ |
|
| 4822 | - public function related_settings_for($relation_name) |
|
| 4823 | - { |
|
| 4824 | - $relatedModels = $this->relation_settings(); |
|
| 4825 | - if (! array_key_exists($relation_name, $relatedModels)) { |
|
| 4826 | - throw new EE_Error( |
|
| 4827 | - sprintf( |
|
| 4828 | - __( |
|
| 4829 | - 'Cannot get %s related to %s. There is no model relation of that type. There is, however, %s...', |
|
| 4830 | - 'event_espresso' |
|
| 4831 | - ), |
|
| 4832 | - $relation_name, |
|
| 4833 | - $this->_get_class_name(), |
|
| 4834 | - implode(', ', array_keys($relatedModels)) |
|
| 4835 | - ) |
|
| 4836 | - ); |
|
| 4837 | - } |
|
| 4838 | - return $relatedModels[ $relation_name ]; |
|
| 4839 | - } |
|
| 4840 | - |
|
| 4841 | - |
|
| 4842 | - |
|
| 4843 | - /** |
|
| 4844 | - * A convenience method for getting a specific field's settings, instead of getting all field settings for all |
|
| 4845 | - * fields |
|
| 4846 | - * |
|
| 4847 | - * @param string $fieldName |
|
| 4848 | - * @param boolean $include_db_only_fields |
|
| 4849 | - * @throws EE_Error |
|
| 4850 | - * @return EE_Model_Field_Base |
|
| 4851 | - */ |
|
| 4852 | - public function field_settings_for($fieldName, $include_db_only_fields = true) |
|
| 4853 | - { |
|
| 4854 | - $fieldSettings = $this->field_settings($include_db_only_fields); |
|
| 4855 | - if (! array_key_exists($fieldName, $fieldSettings)) { |
|
| 4856 | - throw new EE_Error(sprintf( |
|
| 4857 | - __("There is no field/column '%s' on '%s'", 'event_espresso'), |
|
| 4858 | - $fieldName, |
|
| 4859 | - get_class($this) |
|
| 4860 | - )); |
|
| 4861 | - } |
|
| 4862 | - return $fieldSettings[ $fieldName ]; |
|
| 4863 | - } |
|
| 4864 | - |
|
| 4865 | - |
|
| 4866 | - |
|
| 4867 | - /** |
|
| 4868 | - * Checks if this field exists on this model |
|
| 4869 | - * |
|
| 4870 | - * @param string $fieldName a key in the model's _field_settings array |
|
| 4871 | - * @return boolean |
|
| 4872 | - */ |
|
| 4873 | - public function has_field($fieldName) |
|
| 4874 | - { |
|
| 4875 | - $fieldSettings = $this->field_settings(true); |
|
| 4876 | - if (isset($fieldSettings[ $fieldName ])) { |
|
| 4877 | - return true; |
|
| 4878 | - } |
|
| 4879 | - return false; |
|
| 4880 | - } |
|
| 4881 | - |
|
| 4882 | - |
|
| 4883 | - |
|
| 4884 | - /** |
|
| 4885 | - * Returns whether or not this model has a relation to the specified model |
|
| 4886 | - * |
|
| 4887 | - * @param string $relation_name possibly one of the keys in the relation_settings array |
|
| 4888 | - * @return boolean |
|
| 4889 | - */ |
|
| 4890 | - public function has_relation($relation_name) |
|
| 4891 | - { |
|
| 4892 | - $relations = $this->relation_settings(); |
|
| 4893 | - if (isset($relations[ $relation_name ])) { |
|
| 4894 | - return true; |
|
| 4895 | - } |
|
| 4896 | - return false; |
|
| 4897 | - } |
|
| 4898 | - |
|
| 4899 | - |
|
| 4900 | - |
|
| 4901 | - /** |
|
| 4902 | - * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
| 4903 | - * Eg, on EE_Answer that would be ANS_ID field object |
|
| 4904 | - * |
|
| 4905 | - * @param $field_obj |
|
| 4906 | - * @return boolean |
|
| 4907 | - */ |
|
| 4908 | - public function is_primary_key_field($field_obj) |
|
| 4909 | - { |
|
| 4910 | - return $field_obj instanceof EE_Primary_Key_Field_Base ? true : false; |
|
| 4911 | - } |
|
| 4912 | - |
|
| 4913 | - |
|
| 4914 | - |
|
| 4915 | - /** |
|
| 4916 | - * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
| 4917 | - * Eg, on EE_Answer that would be ANS_ID field object |
|
| 4918 | - * |
|
| 4919 | - * @return EE_Model_Field_Base |
|
| 4920 | - * @throws EE_Error |
|
| 4921 | - */ |
|
| 4922 | - public function get_primary_key_field() |
|
| 4923 | - { |
|
| 4924 | - if ($this->_primary_key_field === null) { |
|
| 4925 | - foreach ($this->field_settings(true) as $field_obj) { |
|
| 4926 | - if ($this->is_primary_key_field($field_obj)) { |
|
| 4927 | - $this->_primary_key_field = $field_obj; |
|
| 4928 | - break; |
|
| 4929 | - } |
|
| 4930 | - } |
|
| 4931 | - if (! $this->_primary_key_field instanceof EE_Primary_Key_Field_Base) { |
|
| 4932 | - throw new EE_Error(sprintf( |
|
| 4933 | - __("There is no Primary Key defined on model %s", 'event_espresso'), |
|
| 4934 | - get_class($this) |
|
| 4935 | - )); |
|
| 4936 | - } |
|
| 4937 | - } |
|
| 4938 | - return $this->_primary_key_field; |
|
| 4939 | - } |
|
| 4940 | - |
|
| 4941 | - |
|
| 4942 | - |
|
| 4943 | - /** |
|
| 4944 | - * Returns whether or not not there is a primary key on this model. |
|
| 4945 | - * Internally does some caching. |
|
| 4946 | - * |
|
| 4947 | - * @return boolean |
|
| 4948 | - */ |
|
| 4949 | - public function has_primary_key_field() |
|
| 4950 | - { |
|
| 4951 | - if ($this->_has_primary_key_field === null) { |
|
| 4952 | - try { |
|
| 4953 | - $this->get_primary_key_field(); |
|
| 4954 | - $this->_has_primary_key_field = true; |
|
| 4955 | - } catch (EE_Error $e) { |
|
| 4956 | - $this->_has_primary_key_field = false; |
|
| 4957 | - } |
|
| 4958 | - } |
|
| 4959 | - return $this->_has_primary_key_field; |
|
| 4960 | - } |
|
| 4961 | - |
|
| 4962 | - |
|
| 4963 | - |
|
| 4964 | - /** |
|
| 4965 | - * Finds the first field of type $field_class_name. |
|
| 4966 | - * |
|
| 4967 | - * @param string $field_class_name class name of field that you want to find. Eg, EE_Datetime_Field, |
|
| 4968 | - * EE_Foreign_Key_Field, etc |
|
| 4969 | - * @return EE_Model_Field_Base or null if none is found |
|
| 4970 | - */ |
|
| 4971 | - public function get_a_field_of_type($field_class_name) |
|
| 4972 | - { |
|
| 4973 | - foreach ($this->field_settings() as $field) { |
|
| 4974 | - if ($field instanceof $field_class_name) { |
|
| 4975 | - return $field; |
|
| 4976 | - } |
|
| 4977 | - } |
|
| 4978 | - return null; |
|
| 4979 | - } |
|
| 4980 | - |
|
| 4981 | - |
|
| 4982 | - |
|
| 4983 | - /** |
|
| 4984 | - * Gets a foreign key field pointing to model. |
|
| 4985 | - * |
|
| 4986 | - * @param string $model_name eg Event, Registration, not EEM_Event |
|
| 4987 | - * @return EE_Foreign_Key_Field_Base |
|
| 4988 | - * @throws EE_Error |
|
| 4989 | - */ |
|
| 4990 | - public function get_foreign_key_to($model_name) |
|
| 4991 | - { |
|
| 4992 | - if (! isset($this->_cache_foreign_key_to_fields[ $model_name ])) { |
|
| 4993 | - foreach ($this->field_settings() as $field) { |
|
| 4994 | - if ($field instanceof EE_Foreign_Key_Field_Base |
|
| 4995 | - && in_array($model_name, $field->get_model_names_pointed_to()) |
|
| 4996 | - ) { |
|
| 4997 | - $this->_cache_foreign_key_to_fields[ $model_name ] = $field; |
|
| 4998 | - break; |
|
| 4999 | - } |
|
| 5000 | - } |
|
| 5001 | - if (! isset($this->_cache_foreign_key_to_fields[ $model_name ])) { |
|
| 5002 | - throw new EE_Error(sprintf(__( |
|
| 5003 | - "There is no foreign key field pointing to model %s on model %s", |
|
| 5004 | - 'event_espresso' |
|
| 5005 | - ), $model_name, get_class($this))); |
|
| 5006 | - } |
|
| 5007 | - } |
|
| 5008 | - return $this->_cache_foreign_key_to_fields[ $model_name ]; |
|
| 5009 | - } |
|
| 5010 | - |
|
| 5011 | - |
|
| 5012 | - |
|
| 5013 | - /** |
|
| 5014 | - * Gets the table name (including $wpdb->prefix) for the table alias |
|
| 5015 | - * |
|
| 5016 | - * @param string $table_alias eg Event, Event_Meta, Registration, Transaction, but maybe |
|
| 5017 | - * a table alias with a model chain prefix, like 'Venue__Event_Venue___Event_Meta'. |
|
| 5018 | - * Either one works |
|
| 5019 | - * @return string |
|
| 5020 | - */ |
|
| 5021 | - public function get_table_for_alias($table_alias) |
|
| 5022 | - { |
|
| 5023 | - $table_alias_sans_model_relation_chain_prefix = EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($table_alias); |
|
| 5024 | - return $this->_tables[ $table_alias_sans_model_relation_chain_prefix ]->get_table_name(); |
|
| 5025 | - } |
|
| 5026 | - |
|
| 5027 | - |
|
| 5028 | - |
|
| 5029 | - /** |
|
| 5030 | - * Returns a flat array of all field son this model, instead of organizing them |
|
| 5031 | - * by table_alias as they are in the constructor. |
|
| 5032 | - * |
|
| 5033 | - * @param bool $include_db_only_fields flag indicating whether or not to include the db-only fields |
|
| 5034 | - * @return EE_Model_Field_Base[] where the keys are the field's name |
|
| 5035 | - */ |
|
| 5036 | - public function field_settings($include_db_only_fields = false) |
|
| 5037 | - { |
|
| 5038 | - if ($include_db_only_fields) { |
|
| 5039 | - if ($this->_cached_fields === null) { |
|
| 5040 | - $this->_cached_fields = array(); |
|
| 5041 | - foreach ($this->_fields as $fields_corresponding_to_table) { |
|
| 5042 | - foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
| 5043 | - $this->_cached_fields[ $field_name ] = $field_obj; |
|
| 5044 | - } |
|
| 5045 | - } |
|
| 5046 | - } |
|
| 5047 | - return $this->_cached_fields; |
|
| 5048 | - } |
|
| 5049 | - if ($this->_cached_fields_non_db_only === null) { |
|
| 5050 | - $this->_cached_fields_non_db_only = array(); |
|
| 5051 | - foreach ($this->_fields as $fields_corresponding_to_table) { |
|
| 5052 | - foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
| 5053 | - /** @var $field_obj EE_Model_Field_Base */ |
|
| 5054 | - if (! $field_obj->is_db_only_field()) { |
|
| 5055 | - $this->_cached_fields_non_db_only[ $field_name ] = $field_obj; |
|
| 5056 | - } |
|
| 5057 | - } |
|
| 5058 | - } |
|
| 5059 | - } |
|
| 5060 | - return $this->_cached_fields_non_db_only; |
|
| 5061 | - } |
|
| 5062 | - |
|
| 5063 | - |
|
| 5064 | - |
|
| 5065 | - /** |
|
| 5066 | - * cycle though array of attendees and create objects out of each item |
|
| 5067 | - * |
|
| 5068 | - * @access private |
|
| 5069 | - * @param array $rows of results of $wpdb->get_results($query,ARRAY_A) |
|
| 5070 | - * @return \EE_Base_Class[] array keys are primary keys (if there is a primary key on the model. if not, |
|
| 5071 | - * numerically indexed) |
|
| 5072 | - * @throws EE_Error |
|
| 5073 | - */ |
|
| 5074 | - protected function _create_objects($rows = array()) |
|
| 5075 | - { |
|
| 5076 | - $array_of_objects = array(); |
|
| 5077 | - if (empty($rows)) { |
|
| 5078 | - return array(); |
|
| 5079 | - } |
|
| 5080 | - $count_if_model_has_no_primary_key = 0; |
|
| 5081 | - $has_primary_key = $this->has_primary_key_field(); |
|
| 5082 | - $primary_key_field = $has_primary_key ? $this->get_primary_key_field() : null; |
|
| 5083 | - foreach ((array) $rows as $row) { |
|
| 5084 | - if (empty($row)) { |
|
| 5085 | - // wp did its weird thing where it returns an array like array(0=>null), which is totally not helpful... |
|
| 5086 | - return array(); |
|
| 5087 | - } |
|
| 5088 | - // check if we've already set this object in the results array, |
|
| 5089 | - // in which case there's no need to process it further (again) |
|
| 5090 | - if ($has_primary_key) { |
|
| 5091 | - $table_pk_value = $this->_get_column_value_with_table_alias_or_not( |
|
| 5092 | - $row, |
|
| 5093 | - $primary_key_field->get_qualified_column(), |
|
| 5094 | - $primary_key_field->get_table_column() |
|
| 5095 | - ); |
|
| 5096 | - if ($table_pk_value && isset($array_of_objects[ $table_pk_value ])) { |
|
| 5097 | - continue; |
|
| 5098 | - } |
|
| 5099 | - } |
|
| 5100 | - $classInstance = $this->instantiate_class_from_array_or_object($row); |
|
| 5101 | - if (! $classInstance) { |
|
| 5102 | - throw new EE_Error( |
|
| 5103 | - sprintf( |
|
| 5104 | - __('Could not create instance of class %s from row %s', 'event_espresso'), |
|
| 5105 | - $this->get_this_model_name(), |
|
| 5106 | - http_build_query($row) |
|
| 5107 | - ) |
|
| 5108 | - ); |
|
| 5109 | - } |
|
| 5110 | - // set the timezone on the instantiated objects |
|
| 5111 | - $classInstance->set_timezone($this->_timezone); |
|
| 5112 | - // make sure if there is any timezone setting present that we set the timezone for the object |
|
| 5113 | - $key = $has_primary_key ? $classInstance->ID() : $count_if_model_has_no_primary_key++; |
|
| 5114 | - $array_of_objects[ $key ] = $classInstance; |
|
| 5115 | - // also, for all the relations of type BelongsTo, see if we can cache |
|
| 5116 | - // those related models |
|
| 5117 | - // (we could do this for other relations too, but if there are conditions |
|
| 5118 | - // that filtered out some fo the results, then we'd be caching an incomplete set |
|
| 5119 | - // so it requires a little more thought than just caching them immediately...) |
|
| 5120 | - foreach ($this->_model_relations as $modelName => $relation_obj) { |
|
| 5121 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 5122 | - // check if this model's INFO is present. If so, cache it on the model |
|
| 5123 | - $other_model = $relation_obj->get_other_model(); |
|
| 5124 | - $other_model_obj_maybe = $other_model->instantiate_class_from_array_or_object($row); |
|
| 5125 | - // if we managed to make a model object from the results, cache it on the main model object |
|
| 5126 | - if ($other_model_obj_maybe) { |
|
| 5127 | - // set timezone on these other model objects if they are present |
|
| 5128 | - $other_model_obj_maybe->set_timezone($this->_timezone); |
|
| 5129 | - $classInstance->cache($modelName, $other_model_obj_maybe); |
|
| 5130 | - } |
|
| 5131 | - } |
|
| 5132 | - } |
|
| 5133 | - // also, if this was a custom select query, let's see if there are any results for the custom select fields |
|
| 5134 | - // and add them to the object as well. We'll convert according to the set data_type if there's any set for |
|
| 5135 | - // the field in the CustomSelects object |
|
| 5136 | - if ($this->_custom_selections instanceof CustomSelects) { |
|
| 5137 | - $classInstance->setCustomSelectsValues( |
|
| 5138 | - $this->getValuesForCustomSelectAliasesFromResults($row) |
|
| 5139 | - ); |
|
| 5140 | - } |
|
| 5141 | - } |
|
| 5142 | - return $array_of_objects; |
|
| 5143 | - } |
|
| 5144 | - |
|
| 5145 | - |
|
| 5146 | - /** |
|
| 5147 | - * This will parse a given row of results from the db and see if any keys in the results match an alias within the |
|
| 5148 | - * current CustomSelects object. This will be used to build an array of values indexed by those keys. |
|
| 5149 | - * |
|
| 5150 | - * @param array $db_results_row |
|
| 5151 | - * @return array |
|
| 5152 | - */ |
|
| 5153 | - protected function getValuesForCustomSelectAliasesFromResults(array $db_results_row) |
|
| 5154 | - { |
|
| 5155 | - $results = array(); |
|
| 5156 | - if ($this->_custom_selections instanceof CustomSelects) { |
|
| 5157 | - foreach ($this->_custom_selections->columnAliases() as $alias) { |
|
| 5158 | - if (isset($db_results_row[ $alias ])) { |
|
| 5159 | - $results[ $alias ] = $this->convertValueToDataType( |
|
| 5160 | - $db_results_row[ $alias ], |
|
| 5161 | - $this->_custom_selections->getDataTypeForAlias($alias) |
|
| 5162 | - ); |
|
| 5163 | - } |
|
| 5164 | - } |
|
| 5165 | - } |
|
| 5166 | - return $results; |
|
| 5167 | - } |
|
| 5168 | - |
|
| 5169 | - |
|
| 5170 | - /** |
|
| 5171 | - * This will set the value for the given alias |
|
| 5172 | - * @param string $value |
|
| 5173 | - * @param string $datatype (one of %d, %s, %f) |
|
| 5174 | - * @return int|string|float (int for %d, string for %s, float for %f) |
|
| 5175 | - */ |
|
| 5176 | - protected function convertValueToDataType($value, $datatype) |
|
| 5177 | - { |
|
| 5178 | - switch ($datatype) { |
|
| 5179 | - case '%f': |
|
| 5180 | - return (float) $value; |
|
| 5181 | - case '%d': |
|
| 5182 | - return (int) $value; |
|
| 5183 | - default: |
|
| 5184 | - return (string) $value; |
|
| 5185 | - } |
|
| 5186 | - } |
|
| 5187 | - |
|
| 5188 | - |
|
| 5189 | - /** |
|
| 5190 | - * The purpose of this method is to allow us to create a model object that is not in the db that holds default |
|
| 5191 | - * values. A typical example of where this is used is when creating a new item and the initial load of a form. We |
|
| 5192 | - * dont' necessarily want to test for if the object is present but just assume it is BUT load the defaults from the |
|
| 5193 | - * object (as set in the model_field!). |
|
| 5194 | - * |
|
| 5195 | - * @return EE_Base_Class single EE_Base_Class object with default values for the properties. |
|
| 5196 | - */ |
|
| 5197 | - public function create_default_object() |
|
| 5198 | - { |
|
| 5199 | - $this_model_fields_and_values = array(); |
|
| 5200 | - // setup the row using default values; |
|
| 5201 | - foreach ($this->field_settings() as $field_name => $field_obj) { |
|
| 5202 | - $this_model_fields_and_values[ $field_name ] = $field_obj->get_default_value(); |
|
| 5203 | - } |
|
| 5204 | - $className = $this->_get_class_name(); |
|
| 5205 | - $classInstance = EE_Registry::instance() |
|
| 5206 | - ->load_class($className, array($this_model_fields_and_values), false, false); |
|
| 5207 | - return $classInstance; |
|
| 5208 | - } |
|
| 5209 | - |
|
| 5210 | - |
|
| 5211 | - |
|
| 5212 | - /** |
|
| 5213 | - * @param mixed $cols_n_values either an array of where each key is the name of a field, and the value is its value |
|
| 5214 | - * or an stdClass where each property is the name of a column, |
|
| 5215 | - * @return EE_Base_Class |
|
| 5216 | - * @throws EE_Error |
|
| 5217 | - */ |
|
| 5218 | - public function instantiate_class_from_array_or_object($cols_n_values) |
|
| 5219 | - { |
|
| 5220 | - if (! is_array($cols_n_values) && is_object($cols_n_values)) { |
|
| 5221 | - $cols_n_values = get_object_vars($cols_n_values); |
|
| 5222 | - } |
|
| 5223 | - $primary_key = null; |
|
| 5224 | - // make sure the array only has keys that are fields/columns on this model |
|
| 5225 | - $this_model_fields_n_values = $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
| 5226 | - if ($this->has_primary_key_field() && isset($this_model_fields_n_values[ $this->primary_key_name() ])) { |
|
| 5227 | - $primary_key = $this_model_fields_n_values[ $this->primary_key_name() ]; |
|
| 5228 | - } |
|
| 5229 | - $className = $this->_get_class_name(); |
|
| 5230 | - // check we actually found results that we can use to build our model object |
|
| 5231 | - // if not, return null |
|
| 5232 | - if ($this->has_primary_key_field()) { |
|
| 5233 | - if (empty($this_model_fields_n_values[ $this->primary_key_name() ])) { |
|
| 5234 | - return null; |
|
| 5235 | - } |
|
| 5236 | - } elseif ($this->unique_indexes()) { |
|
| 5237 | - $first_column = reset($this_model_fields_n_values); |
|
| 5238 | - if (empty($first_column)) { |
|
| 5239 | - return null; |
|
| 5240 | - } |
|
| 5241 | - } |
|
| 5242 | - // if there is no primary key or the object doesn't already exist in the entity map, then create a new instance |
|
| 5243 | - if ($primary_key) { |
|
| 5244 | - $classInstance = $this->get_from_entity_map($primary_key); |
|
| 5245 | - if (! $classInstance) { |
|
| 5246 | - $classInstance = EE_Registry::instance() |
|
| 5247 | - ->load_class( |
|
| 5248 | - $className, |
|
| 5249 | - array($this_model_fields_n_values, $this->_timezone), |
|
| 5250 | - true, |
|
| 5251 | - false |
|
| 5252 | - ); |
|
| 5253 | - // add this new object to the entity map |
|
| 5254 | - $classInstance = $this->add_to_entity_map($classInstance); |
|
| 5255 | - } |
|
| 5256 | - } else { |
|
| 5257 | - $classInstance = EE_Registry::instance() |
|
| 5258 | - ->load_class( |
|
| 5259 | - $className, |
|
| 5260 | - array($this_model_fields_n_values, $this->_timezone), |
|
| 5261 | - true, |
|
| 5262 | - false |
|
| 5263 | - ); |
|
| 5264 | - } |
|
| 5265 | - return $classInstance; |
|
| 5266 | - } |
|
| 5267 | - |
|
| 5268 | - |
|
| 5269 | - |
|
| 5270 | - /** |
|
| 5271 | - * Gets the model object from the entity map if it exists |
|
| 5272 | - * |
|
| 5273 | - * @param int|string $id the ID of the model object |
|
| 5274 | - * @return EE_Base_Class |
|
| 5275 | - */ |
|
| 5276 | - public function get_from_entity_map($id) |
|
| 5277 | - { |
|
| 5278 | - return isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]) |
|
| 5279 | - ? $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ] : null; |
|
| 5280 | - } |
|
| 5281 | - |
|
| 5282 | - |
|
| 5283 | - |
|
| 5284 | - /** |
|
| 5285 | - * add_to_entity_map |
|
| 5286 | - * Adds the object to the model's entity mappings |
|
| 5287 | - * Effectively tells the models "Hey, this model object is the most up-to-date representation of the data, |
|
| 5288 | - * and for the remainder of the request, it's even more up-to-date than what's in the database. |
|
| 5289 | - * So, if the database doesn't agree with what's in the entity mapper, ignore the database" |
|
| 5290 | - * If the database gets updated directly and you want the entity mapper to reflect that change, |
|
| 5291 | - * then this method should be called immediately after the update query |
|
| 5292 | - * Note: The map is indexed by whatever the current blog id is set (via EEM_Base::$_model_query_blog_id). This is |
|
| 5293 | - * so on multisite, the entity map is specific to the query being done for a specific site. |
|
| 5294 | - * |
|
| 5295 | - * @param EE_Base_Class $object |
|
| 5296 | - * @throws EE_Error |
|
| 5297 | - * @return \EE_Base_Class |
|
| 5298 | - */ |
|
| 5299 | - public function add_to_entity_map(EE_Base_Class $object) |
|
| 5300 | - { |
|
| 5301 | - $className = $this->_get_class_name(); |
|
| 5302 | - if (! $object instanceof $className) { |
|
| 5303 | - throw new EE_Error(sprintf( |
|
| 5304 | - __("You tried adding a %s to a mapping of %ss", "event_espresso"), |
|
| 5305 | - is_object($object) ? get_class($object) : $object, |
|
| 5306 | - $className |
|
| 5307 | - )); |
|
| 5308 | - } |
|
| 5309 | - /** @var $object EE_Base_Class */ |
|
| 5310 | - if (! $object->ID()) { |
|
| 5311 | - throw new EE_Error(sprintf(__( |
|
| 5312 | - "You tried storing a model object with NO ID in the %s entity mapper.", |
|
| 5313 | - "event_espresso" |
|
| 5314 | - ), get_class($this))); |
|
| 5315 | - } |
|
| 5316 | - // double check it's not already there |
|
| 5317 | - $classInstance = $this->get_from_entity_map($object->ID()); |
|
| 5318 | - if ($classInstance) { |
|
| 5319 | - return $classInstance; |
|
| 5320 | - } |
|
| 5321 | - $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $object->ID() ] = $object; |
|
| 5322 | - return $object; |
|
| 5323 | - } |
|
| 5324 | - |
|
| 5325 | - |
|
| 5326 | - |
|
| 5327 | - /** |
|
| 5328 | - * if a valid identifier is provided, then that entity is unset from the entity map, |
|
| 5329 | - * if no identifier is provided, then the entire entity map is emptied |
|
| 5330 | - * |
|
| 5331 | - * @param int|string $id the ID of the model object |
|
| 5332 | - * @return boolean |
|
| 5333 | - */ |
|
| 5334 | - public function clear_entity_map($id = null) |
|
| 5335 | - { |
|
| 5336 | - if (empty($id)) { |
|
| 5337 | - $this->_entity_map[ EEM_Base::$_model_query_blog_id ] = array(); |
|
| 5338 | - return true; |
|
| 5339 | - } |
|
| 5340 | - if (isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ])) { |
|
| 5341 | - unset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]); |
|
| 5342 | - return true; |
|
| 5343 | - } |
|
| 5344 | - return false; |
|
| 5345 | - } |
|
| 5346 | - |
|
| 5347 | - |
|
| 5348 | - |
|
| 5349 | - /** |
|
| 5350 | - * Public wrapper for _deduce_fields_n_values_from_cols_n_values. |
|
| 5351 | - * Given an array where keys are column (or column alias) names and values, |
|
| 5352 | - * returns an array of their corresponding field names and database values |
|
| 5353 | - * |
|
| 5354 | - * @param array $cols_n_values |
|
| 5355 | - * @return array |
|
| 5356 | - */ |
|
| 5357 | - public function deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
| 5358 | - { |
|
| 5359 | - return $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
| 5360 | - } |
|
| 5361 | - |
|
| 5362 | - |
|
| 5363 | - |
|
| 5364 | - /** |
|
| 5365 | - * _deduce_fields_n_values_from_cols_n_values |
|
| 5366 | - * Given an array where keys are column (or column alias) names and values, |
|
| 5367 | - * returns an array of their corresponding field names and database values |
|
| 5368 | - * |
|
| 5369 | - * @param string $cols_n_values |
|
| 5370 | - * @return array |
|
| 5371 | - */ |
|
| 5372 | - protected function _deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
| 5373 | - { |
|
| 5374 | - $this_model_fields_n_values = array(); |
|
| 5375 | - foreach ($this->get_tables() as $table_alias => $table_obj) { |
|
| 5376 | - $table_pk_value = $this->_get_column_value_with_table_alias_or_not( |
|
| 5377 | - $cols_n_values, |
|
| 5378 | - $table_obj->get_fully_qualified_pk_column(), |
|
| 5379 | - $table_obj->get_pk_column() |
|
| 5380 | - ); |
|
| 5381 | - // there is a primary key on this table and its not set. Use defaults for all its columns |
|
| 5382 | - if ($table_pk_value === null && $table_obj->get_pk_column()) { |
|
| 5383 | - foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
| 5384 | - if (! $field_obj->is_db_only_field()) { |
|
| 5385 | - // prepare field as if its coming from db |
|
| 5386 | - $prepared_value = $field_obj->prepare_for_set($field_obj->get_default_value()); |
|
| 5387 | - $this_model_fields_n_values[ $field_name ] = $field_obj->prepare_for_use_in_db($prepared_value); |
|
| 5388 | - } |
|
| 5389 | - } |
|
| 5390 | - } else { |
|
| 5391 | - // the table's rows existed. Use their values |
|
| 5392 | - foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
| 5393 | - if (! $field_obj->is_db_only_field()) { |
|
| 5394 | - $this_model_fields_n_values[ $field_name ] = $this->_get_column_value_with_table_alias_or_not( |
|
| 5395 | - $cols_n_values, |
|
| 5396 | - $field_obj->get_qualified_column(), |
|
| 5397 | - $field_obj->get_table_column() |
|
| 5398 | - ); |
|
| 5399 | - } |
|
| 5400 | - } |
|
| 5401 | - } |
|
| 5402 | - } |
|
| 5403 | - return $this_model_fields_n_values; |
|
| 5404 | - } |
|
| 5405 | - |
|
| 5406 | - |
|
| 5407 | - |
|
| 5408 | - /** |
|
| 5409 | - * @param $cols_n_values |
|
| 5410 | - * @param $qualified_column |
|
| 5411 | - * @param $regular_column |
|
| 5412 | - * @return null |
|
| 5413 | - */ |
|
| 5414 | - protected function _get_column_value_with_table_alias_or_not($cols_n_values, $qualified_column, $regular_column) |
|
| 5415 | - { |
|
| 5416 | - $value = null; |
|
| 5417 | - // ask the field what it think it's table_name.column_name should be, and call it the "qualified column" |
|
| 5418 | - // does the field on the model relate to this column retrieved from the db? |
|
| 5419 | - // or is it a db-only field? (not relating to the model) |
|
| 5420 | - if (isset($cols_n_values[ $qualified_column ])) { |
|
| 5421 | - $value = $cols_n_values[ $qualified_column ]; |
|
| 5422 | - } elseif (isset($cols_n_values[ $regular_column ])) { |
|
| 5423 | - $value = $cols_n_values[ $regular_column ]; |
|
| 5424 | - } |
|
| 5425 | - return $value; |
|
| 5426 | - } |
|
| 5427 | - |
|
| 5428 | - |
|
| 5429 | - |
|
| 5430 | - /** |
|
| 5431 | - * refresh_entity_map_from_db |
|
| 5432 | - * Makes sure the model object in the entity map at $id assumes the values |
|
| 5433 | - * of the database (opposite of EE_base_Class::save()) |
|
| 5434 | - * |
|
| 5435 | - * @param int|string $id |
|
| 5436 | - * @return EE_Base_Class |
|
| 5437 | - * @throws EE_Error |
|
| 5438 | - */ |
|
| 5439 | - public function refresh_entity_map_from_db($id) |
|
| 5440 | - { |
|
| 5441 | - $obj_in_map = $this->get_from_entity_map($id); |
|
| 5442 | - if ($obj_in_map) { |
|
| 5443 | - $wpdb_results = $this->_get_all_wpdb_results( |
|
| 5444 | - array(array($this->get_primary_key_field()->get_name() => $id), 'limit' => 1) |
|
| 5445 | - ); |
|
| 5446 | - if ($wpdb_results && is_array($wpdb_results)) { |
|
| 5447 | - $one_row = reset($wpdb_results); |
|
| 5448 | - foreach ($this->_deduce_fields_n_values_from_cols_n_values($one_row) as $field_name => $db_value) { |
|
| 5449 | - $obj_in_map->set_from_db($field_name, $db_value); |
|
| 5450 | - } |
|
| 5451 | - // clear the cache of related model objects |
|
| 5452 | - foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
| 5453 | - $obj_in_map->clear_cache($relation_name, null, true); |
|
| 5454 | - } |
|
| 5455 | - } |
|
| 5456 | - $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ] = $obj_in_map; |
|
| 5457 | - return $obj_in_map; |
|
| 5458 | - } |
|
| 5459 | - return $this->get_one_by_ID($id); |
|
| 5460 | - } |
|
| 5461 | - |
|
| 5462 | - |
|
| 5463 | - |
|
| 5464 | - /** |
|
| 5465 | - * refresh_entity_map_with |
|
| 5466 | - * Leaves the entry in the entity map alone, but updates it to match the provided |
|
| 5467 | - * $replacing_model_obj (which we assume to be its equivalent but somehow NOT in the entity map). |
|
| 5468 | - * This is useful if you have a model object you want to make authoritative over what's in the entity map currently. |
|
| 5469 | - * Note: The old $replacing_model_obj should now be destroyed as it's now un-authoritative |
|
| 5470 | - * |
|
| 5471 | - * @param int|string $id |
|
| 5472 | - * @param EE_Base_Class $replacing_model_obj |
|
| 5473 | - * @return \EE_Base_Class |
|
| 5474 | - * @throws EE_Error |
|
| 5475 | - */ |
|
| 5476 | - public function refresh_entity_map_with($id, $replacing_model_obj) |
|
| 5477 | - { |
|
| 5478 | - $obj_in_map = $this->get_from_entity_map($id); |
|
| 5479 | - if ($obj_in_map) { |
|
| 5480 | - if ($replacing_model_obj instanceof EE_Base_Class) { |
|
| 5481 | - foreach ($replacing_model_obj->model_field_array() as $field_name => $value) { |
|
| 5482 | - $obj_in_map->set($field_name, $value); |
|
| 5483 | - } |
|
| 5484 | - // make the model object in the entity map's cache match the $replacing_model_obj |
|
| 5485 | - foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
| 5486 | - $obj_in_map->clear_cache($relation_name, null, true); |
|
| 5487 | - foreach ($replacing_model_obj->get_all_from_cache($relation_name) as $cache_id => $cached_obj) { |
|
| 5488 | - $obj_in_map->cache($relation_name, $cached_obj, $cache_id); |
|
| 5489 | - } |
|
| 5490 | - } |
|
| 5491 | - } |
|
| 5492 | - return $obj_in_map; |
|
| 5493 | - } |
|
| 5494 | - $this->add_to_entity_map($replacing_model_obj); |
|
| 5495 | - return $replacing_model_obj; |
|
| 5496 | - } |
|
| 5497 | - |
|
| 5498 | - |
|
| 5499 | - |
|
| 5500 | - /** |
|
| 5501 | - * Gets the EE class that corresponds to this model. Eg, for EEM_Answer that |
|
| 5502 | - * would be EE_Answer.To import that class, you'd just add ".class.php" to the name, like so |
|
| 5503 | - * require_once($this->_getClassName().".class.php"); |
|
| 5504 | - * |
|
| 5505 | - * @return string |
|
| 5506 | - */ |
|
| 5507 | - private function _get_class_name() |
|
| 5508 | - { |
|
| 5509 | - return "EE_" . $this->get_this_model_name(); |
|
| 5510 | - } |
|
| 5511 | - |
|
| 5512 | - |
|
| 5513 | - |
|
| 5514 | - /** |
|
| 5515 | - * Get the name of the items this model represents, for the quantity specified. Eg, |
|
| 5516 | - * if $quantity==1, on EEM_Event, it would 'Event' (internationalized), otherwise |
|
| 5517 | - * it would be 'Events'. |
|
| 5518 | - * |
|
| 5519 | - * @param int $quantity |
|
| 5520 | - * @return string |
|
| 5521 | - */ |
|
| 5522 | - public function item_name($quantity = 1) |
|
| 5523 | - { |
|
| 5524 | - return (int) $quantity === 1 ? $this->singular_item : $this->plural_item; |
|
| 5525 | - } |
|
| 5526 | - |
|
| 5527 | - |
|
| 5528 | - |
|
| 5529 | - /** |
|
| 5530 | - * Very handy general function to allow for plugins to extend any child of EE_TempBase. |
|
| 5531 | - * If a method is called on a child of EE_TempBase that doesn't exist, this function is called |
|
| 5532 | - * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
|
| 5533 | - * requiring a plugin to extend the EE_TempBase (which works fine is there's only 1 plugin, but when will that |
|
| 5534 | - * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
|
| 5535 | - * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
|
| 5536 | - * was called, and an array of the original arguments passed to the function. Whatever their callback function |
|
| 5537 | - * returns will be returned by this function. Example: in functions.php (or in a plugin): |
|
| 5538 | - * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
|
| 5539 | - * my_callback($previousReturnValue,EE_TempBase $object,$argsArray){ |
|
| 5540 | - * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 5541 | - * return $previousReturnValue.$returnString; |
|
| 5542 | - * } |
|
| 5543 | - * require('EEM_Answer.model.php'); |
|
| 5544 | - * $answer=EEM_Answer::instance(); |
|
| 5545 | - * echo $answer->my_callback('monkeys',100); |
|
| 5546 | - * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 5547 | - * |
|
| 5548 | - * @param string $methodName name of method which was called on a child of EE_TempBase, but which |
|
| 5549 | - * @param array $args array of original arguments passed to the function |
|
| 5550 | - * @throws EE_Error |
|
| 5551 | - * @return mixed whatever the plugin which calls add_filter decides |
|
| 5552 | - */ |
|
| 5553 | - public function __call($methodName, $args) |
|
| 5554 | - { |
|
| 5555 | - $className = get_class($this); |
|
| 5556 | - $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 5557 | - if (! has_filter($tagName)) { |
|
| 5558 | - throw new EE_Error( |
|
| 5559 | - sprintf( |
|
| 5560 | - __( |
|
| 5561 | - 'Method %1$s on model %2$s does not exist! You can create one with the following code in functions.php or in a plugin: %4$s function my_callback(%4$s \$previousReturnValue, EEM_Base \$object\ $argsArray=NULL ){%4$s /*function body*/%4$s return \$whatever;%4$s }%4$s add_filter( \'%3$s\', \'my_callback\', 10, 3 );', |
|
| 5562 | - 'event_espresso' |
|
| 5563 | - ), |
|
| 5564 | - $methodName, |
|
| 5565 | - $className, |
|
| 5566 | - $tagName, |
|
| 5567 | - '<br />' |
|
| 5568 | - ) |
|
| 5569 | - ); |
|
| 5570 | - } |
|
| 5571 | - return apply_filters($tagName, null, $this, $args); |
|
| 5572 | - } |
|
| 5573 | - |
|
| 5574 | - |
|
| 5575 | - |
|
| 5576 | - /** |
|
| 5577 | - * Ensures $base_class_obj_or_id is of the EE_Base_Class child that corresponds ot this model. |
|
| 5578 | - * If not, assumes its an ID, and uses $this->get_one_by_ID() to get the EE_Base_Class. |
|
| 5579 | - * |
|
| 5580 | - * @param EE_Base_Class|string|int $base_class_obj_or_id either: |
|
| 5581 | - * the EE_Base_Class object that corresponds to this Model, |
|
| 5582 | - * the object's class name |
|
| 5583 | - * or object's ID |
|
| 5584 | - * @param boolean $ensure_is_in_db if set, we will also verify this model object |
|
| 5585 | - * exists in the database. If it does not, we add it |
|
| 5586 | - * @throws EE_Error |
|
| 5587 | - * @return EE_Base_Class |
|
| 5588 | - */ |
|
| 5589 | - public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false) |
|
| 5590 | - { |
|
| 5591 | - $className = $this->_get_class_name(); |
|
| 5592 | - if ($base_class_obj_or_id instanceof $className) { |
|
| 5593 | - $model_object = $base_class_obj_or_id; |
|
| 5594 | - } else { |
|
| 5595 | - $primary_key_field = $this->get_primary_key_field(); |
|
| 5596 | - if ($primary_key_field instanceof EE_Primary_Key_Int_Field |
|
| 5597 | - && ( |
|
| 5598 | - is_int($base_class_obj_or_id) |
|
| 5599 | - || is_string($base_class_obj_or_id) |
|
| 5600 | - ) |
|
| 5601 | - ) { |
|
| 5602 | - // assume it's an ID. |
|
| 5603 | - // either a proper integer or a string representing an integer (eg "101" instead of 101) |
|
| 5604 | - $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
| 5605 | - } elseif ($primary_key_field instanceof EE_Primary_Key_String_Field |
|
| 5606 | - && is_string($base_class_obj_or_id) |
|
| 5607 | - ) { |
|
| 5608 | - // assume its a string representation of the object |
|
| 5609 | - $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
| 5610 | - } else { |
|
| 5611 | - throw new EE_Error( |
|
| 5612 | - sprintf( |
|
| 5613 | - __( |
|
| 5614 | - "'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
| 5615 | - 'event_espresso' |
|
| 5616 | - ), |
|
| 5617 | - $base_class_obj_or_id, |
|
| 5618 | - $this->_get_class_name(), |
|
| 5619 | - print_r($base_class_obj_or_id, true) |
|
| 5620 | - ) |
|
| 5621 | - ); |
|
| 5622 | - } |
|
| 5623 | - } |
|
| 5624 | - if ($ensure_is_in_db && $model_object->ID() !== null) { |
|
| 5625 | - $model_object->save(); |
|
| 5626 | - } |
|
| 5627 | - return $model_object; |
|
| 5628 | - } |
|
| 5629 | - |
|
| 5630 | - |
|
| 5631 | - |
|
| 5632 | - /** |
|
| 5633 | - * Similar to ensure_is_obj(), this method makes sure $base_class_obj_or_id |
|
| 5634 | - * is a value of the this model's primary key. If it's an EE_Base_Class child, |
|
| 5635 | - * returns it ID. |
|
| 5636 | - * |
|
| 5637 | - * @param EE_Base_Class|int|string $base_class_obj_or_id |
|
| 5638 | - * @return int|string depending on the type of this model object's ID |
|
| 5639 | - * @throws EE_Error |
|
| 5640 | - */ |
|
| 5641 | - public function ensure_is_ID($base_class_obj_or_id) |
|
| 5642 | - { |
|
| 5643 | - $className = $this->_get_class_name(); |
|
| 5644 | - if ($base_class_obj_or_id instanceof $className) { |
|
| 5645 | - /** @var $base_class_obj_or_id EE_Base_Class */ |
|
| 5646 | - $id = $base_class_obj_or_id->ID(); |
|
| 5647 | - } elseif (is_int($base_class_obj_or_id)) { |
|
| 5648 | - // assume it's an ID |
|
| 5649 | - $id = $base_class_obj_or_id; |
|
| 5650 | - } elseif (is_string($base_class_obj_or_id)) { |
|
| 5651 | - // assume its a string representation of the object |
|
| 5652 | - $id = $base_class_obj_or_id; |
|
| 5653 | - } else { |
|
| 5654 | - throw new EE_Error(sprintf( |
|
| 5655 | - __( |
|
| 5656 | - "'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
| 5657 | - 'event_espresso' |
|
| 5658 | - ), |
|
| 5659 | - $base_class_obj_or_id, |
|
| 5660 | - $this->_get_class_name(), |
|
| 5661 | - print_r($base_class_obj_or_id, true) |
|
| 5662 | - )); |
|
| 5663 | - } |
|
| 5664 | - return $id; |
|
| 5665 | - } |
|
| 5666 | - |
|
| 5667 | - |
|
| 5668 | - |
|
| 5669 | - /** |
|
| 5670 | - * Sets whether the values passed to the model (eg, values in WHERE, values in INSERT, UPDATE, etc) |
|
| 5671 | - * have already been ran through the appropriate model field's prepare_for_use_in_db method. IE, they have |
|
| 5672 | - * been sanitized and converted into the appropriate domain. |
|
| 5673 | - * Usually the only place you'll want to change the default (which is to assume values have NOT been sanitized by |
|
| 5674 | - * the model object/model field) is when making a method call from WITHIN a model object, which has direct access |
|
| 5675 | - * to its sanitized values. Note: after changing this setting, you should set it back to its previous value (using |
|
| 5676 | - * get_assumption_concerning_values_already_prepared_by_model_object()) eg. |
|
| 5677 | - * $EVT = EEM_Event::instance(); $old_setting = |
|
| 5678 | - * $EVT->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 5679 | - * $EVT->assume_values_already_prepared_by_model_object(true); |
|
| 5680 | - * $EVT->update(array('foo'=>'bar'),array(array('foo'=>'monkey'))); |
|
| 5681 | - * $EVT->assume_values_already_prepared_by_model_object($old_setting); |
|
| 5682 | - * |
|
| 5683 | - * @param int $values_already_prepared like one of the constants on EEM_Base |
|
| 5684 | - * @return void |
|
| 5685 | - */ |
|
| 5686 | - public function assume_values_already_prepared_by_model_object( |
|
| 5687 | - $values_already_prepared = self::not_prepared_by_model_object |
|
| 5688 | - ) { |
|
| 5689 | - $this->_values_already_prepared_by_model_object = $values_already_prepared; |
|
| 5690 | - } |
|
| 5691 | - |
|
| 5692 | - |
|
| 5693 | - |
|
| 5694 | - /** |
|
| 5695 | - * Read comments for assume_values_already_prepared_by_model_object() |
|
| 5696 | - * |
|
| 5697 | - * @return int |
|
| 5698 | - */ |
|
| 5699 | - public function get_assumption_concerning_values_already_prepared_by_model_object() |
|
| 5700 | - { |
|
| 5701 | - return $this->_values_already_prepared_by_model_object; |
|
| 5702 | - } |
|
| 5703 | - |
|
| 5704 | - |
|
| 5705 | - |
|
| 5706 | - /** |
|
| 5707 | - * Gets all the indexes on this model |
|
| 5708 | - * |
|
| 5709 | - * @return EE_Index[] |
|
| 5710 | - */ |
|
| 5711 | - public function indexes() |
|
| 5712 | - { |
|
| 5713 | - return $this->_indexes; |
|
| 5714 | - } |
|
| 5715 | - |
|
| 5716 | - |
|
| 5717 | - |
|
| 5718 | - /** |
|
| 5719 | - * Gets all the Unique Indexes on this model |
|
| 5720 | - * |
|
| 5721 | - * @return EE_Unique_Index[] |
|
| 5722 | - */ |
|
| 5723 | - public function unique_indexes() |
|
| 5724 | - { |
|
| 5725 | - $unique_indexes = array(); |
|
| 5726 | - foreach ($this->_indexes as $name => $index) { |
|
| 5727 | - if ($index instanceof EE_Unique_Index) { |
|
| 5728 | - $unique_indexes [ $name ] = $index; |
|
| 5729 | - } |
|
| 5730 | - } |
|
| 5731 | - return $unique_indexes; |
|
| 5732 | - } |
|
| 5733 | - |
|
| 5734 | - |
|
| 5735 | - |
|
| 5736 | - /** |
|
| 5737 | - * Gets all the fields which, when combined, make the primary key. |
|
| 5738 | - * This is usually just an array with 1 element (the primary key), but in cases |
|
| 5739 | - * where there is no primary key, it's a combination of fields as defined |
|
| 5740 | - * on a primary index |
|
| 5741 | - * |
|
| 5742 | - * @return EE_Model_Field_Base[] indexed by the field's name |
|
| 5743 | - * @throws EE_Error |
|
| 5744 | - */ |
|
| 5745 | - public function get_combined_primary_key_fields() |
|
| 5746 | - { |
|
| 5747 | - foreach ($this->indexes() as $index) { |
|
| 5748 | - if ($index instanceof EE_Primary_Key_Index) { |
|
| 5749 | - return $index->fields(); |
|
| 5750 | - } |
|
| 5751 | - } |
|
| 5752 | - return array($this->primary_key_name() => $this->get_primary_key_field()); |
|
| 5753 | - } |
|
| 5754 | - |
|
| 5755 | - |
|
| 5756 | - |
|
| 5757 | - /** |
|
| 5758 | - * Used to build a primary key string (when the model has no primary key), |
|
| 5759 | - * which can be used a unique string to identify this model object. |
|
| 5760 | - * |
|
| 5761 | - * @param array $fields_n_values keys are field names, values are their values. |
|
| 5762 | - * Note: if you have results from `EEM_Base::get_all_wpdb_results()`, you need to |
|
| 5763 | - * run it through `EEM_Base::deduce_fields_n_values_from_cols_n_values()` |
|
| 5764 | - * before passing it to this function (that will convert it from columns-n-values |
|
| 5765 | - * to field-names-n-values). |
|
| 5766 | - * @return string |
|
| 5767 | - * @throws EE_Error |
|
| 5768 | - */ |
|
| 5769 | - public function get_index_primary_key_string($fields_n_values) |
|
| 5770 | - { |
|
| 5771 | - $cols_n_values_for_primary_key_index = array_intersect_key( |
|
| 5772 | - $fields_n_values, |
|
| 5773 | - $this->get_combined_primary_key_fields() |
|
| 5774 | - ); |
|
| 5775 | - return http_build_query($cols_n_values_for_primary_key_index); |
|
| 5776 | - } |
|
| 5777 | - |
|
| 5778 | - |
|
| 5779 | - |
|
| 5780 | - /** |
|
| 5781 | - * Gets the field values from the primary key string |
|
| 5782 | - * |
|
| 5783 | - * @see EEM_Base::get_combined_primary_key_fields() and EEM_Base::get_index_primary_key_string() |
|
| 5784 | - * @param string $index_primary_key_string |
|
| 5785 | - * @return null|array |
|
| 5786 | - * @throws EE_Error |
|
| 5787 | - */ |
|
| 5788 | - public function parse_index_primary_key_string($index_primary_key_string) |
|
| 5789 | - { |
|
| 5790 | - $key_fields = $this->get_combined_primary_key_fields(); |
|
| 5791 | - // check all of them are in the $id |
|
| 5792 | - $key_vals_in_combined_pk = array(); |
|
| 5793 | - parse_str($index_primary_key_string, $key_vals_in_combined_pk); |
|
| 5794 | - foreach ($key_fields as $key_field_name => $field_obj) { |
|
| 5795 | - if (! isset($key_vals_in_combined_pk[ $key_field_name ])) { |
|
| 5796 | - return null; |
|
| 5797 | - } |
|
| 5798 | - } |
|
| 5799 | - return $key_vals_in_combined_pk; |
|
| 5800 | - } |
|
| 5801 | - |
|
| 5802 | - |
|
| 5803 | - |
|
| 5804 | - /** |
|
| 5805 | - * verifies that an array of key-value pairs for model fields has a key |
|
| 5806 | - * for each field comprising the primary key index |
|
| 5807 | - * |
|
| 5808 | - * @param array $key_vals |
|
| 5809 | - * @return boolean |
|
| 5810 | - * @throws EE_Error |
|
| 5811 | - */ |
|
| 5812 | - public function has_all_combined_primary_key_fields($key_vals) |
|
| 5813 | - { |
|
| 5814 | - $keys_it_should_have = array_keys($this->get_combined_primary_key_fields()); |
|
| 5815 | - foreach ($keys_it_should_have as $key) { |
|
| 5816 | - if (! isset($key_vals[ $key ])) { |
|
| 5817 | - return false; |
|
| 5818 | - } |
|
| 5819 | - } |
|
| 5820 | - return true; |
|
| 5821 | - } |
|
| 5822 | - |
|
| 5823 | - |
|
| 5824 | - |
|
| 5825 | - /** |
|
| 5826 | - * Finds all model objects in the DB that appear to be a copy of $model_object_or_attributes_array. |
|
| 5827 | - * We consider something to be a copy if all the attributes match (except the ID, of course). |
|
| 5828 | - * |
|
| 5829 | - * @param array|EE_Base_Class $model_object_or_attributes_array If its an array, it's field-value pairs |
|
| 5830 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 5831 | - * @throws EE_Error |
|
| 5832 | - * @return \EE_Base_Class[] Array keys are object IDs (if there is a primary key on the model. if not, numerically |
|
| 5833 | - * indexed) |
|
| 5834 | - */ |
|
| 5835 | - public function get_all_copies($model_object_or_attributes_array, $query_params = array()) |
|
| 5836 | - { |
|
| 5837 | - if ($model_object_or_attributes_array instanceof EE_Base_Class) { |
|
| 5838 | - $attributes_array = $model_object_or_attributes_array->model_field_array(); |
|
| 5839 | - } elseif (is_array($model_object_or_attributes_array)) { |
|
| 5840 | - $attributes_array = $model_object_or_attributes_array; |
|
| 5841 | - } else { |
|
| 5842 | - throw new EE_Error(sprintf(__( |
|
| 5843 | - "get_all_copies should be provided with either a model object or an array of field-value-pairs, but was given %s", |
|
| 5844 | - "event_espresso" |
|
| 5845 | - ), $model_object_or_attributes_array)); |
|
| 5846 | - } |
|
| 5847 | - // even copies obviously won't have the same ID, so remove the primary key |
|
| 5848 | - // from the WHERE conditions for finding copies (if there is a primary key, of course) |
|
| 5849 | - if ($this->has_primary_key_field() && isset($attributes_array[ $this->primary_key_name() ])) { |
|
| 5850 | - unset($attributes_array[ $this->primary_key_name() ]); |
|
| 5851 | - } |
|
| 5852 | - if (isset($query_params[0])) { |
|
| 5853 | - $query_params[0] = array_merge($attributes_array, $query_params); |
|
| 5854 | - } else { |
|
| 5855 | - $query_params[0] = $attributes_array; |
|
| 5856 | - } |
|
| 5857 | - return $this->get_all($query_params); |
|
| 5858 | - } |
|
| 5859 | - |
|
| 5860 | - |
|
| 5861 | - |
|
| 5862 | - /** |
|
| 5863 | - * Gets the first copy we find. See get_all_copies for more details |
|
| 5864 | - * |
|
| 5865 | - * @param mixed EE_Base_Class | array $model_object_or_attributes_array |
|
| 5866 | - * @param array $query_params |
|
| 5867 | - * @return EE_Base_Class |
|
| 5868 | - * @throws EE_Error |
|
| 5869 | - */ |
|
| 5870 | - public function get_one_copy($model_object_or_attributes_array, $query_params = array()) |
|
| 5871 | - { |
|
| 5872 | - if (! is_array($query_params)) { |
|
| 5873 | - EE_Error::doing_it_wrong( |
|
| 5874 | - 'EEM_Base::get_one_copy', |
|
| 5875 | - sprintf( |
|
| 5876 | - __('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 5877 | - gettype($query_params) |
|
| 5878 | - ), |
|
| 5879 | - '4.6.0' |
|
| 5880 | - ); |
|
| 5881 | - $query_params = array(); |
|
| 5882 | - } |
|
| 5883 | - $query_params['limit'] = 1; |
|
| 5884 | - $copies = $this->get_all_copies($model_object_or_attributes_array, $query_params); |
|
| 5885 | - if (is_array($copies)) { |
|
| 5886 | - return array_shift($copies); |
|
| 5887 | - } |
|
| 5888 | - return null; |
|
| 5889 | - } |
|
| 5890 | - |
|
| 5891 | - |
|
| 5892 | - |
|
| 5893 | - /** |
|
| 5894 | - * Updates the item with the specified id. Ignores default query parameters because |
|
| 5895 | - * we have specified the ID, and its assumed we KNOW what we're doing |
|
| 5896 | - * |
|
| 5897 | - * @param array $fields_n_values keys are field names, values are their new values |
|
| 5898 | - * @param int|string $id the value of the primary key to update |
|
| 5899 | - * @return int number of rows updated |
|
| 5900 | - * @throws EE_Error |
|
| 5901 | - */ |
|
| 5902 | - public function update_by_ID($fields_n_values, $id) |
|
| 5903 | - { |
|
| 5904 | - $query_params = array( |
|
| 5905 | - 0 => array($this->get_primary_key_field()->get_name() => $id), |
|
| 5906 | - 'default_where_conditions' => EEM_Base::default_where_conditions_others_only, |
|
| 5907 | - ); |
|
| 5908 | - return $this->update($fields_n_values, $query_params); |
|
| 5909 | - } |
|
| 5910 | - |
|
| 5911 | - |
|
| 5912 | - |
|
| 5913 | - /** |
|
| 5914 | - * Changes an operator which was supplied to the models into one usable in SQL |
|
| 5915 | - * |
|
| 5916 | - * @param string $operator_supplied |
|
| 5917 | - * @return string an operator which can be used in SQL |
|
| 5918 | - * @throws EE_Error |
|
| 5919 | - */ |
|
| 5920 | - private function _prepare_operator_for_sql($operator_supplied) |
|
| 5921 | - { |
|
| 5922 | - $sql_operator = isset($this->_valid_operators[ $operator_supplied ]) ? $this->_valid_operators[ $operator_supplied ] |
|
| 5923 | - : null; |
|
| 5924 | - if ($sql_operator) { |
|
| 5925 | - return $sql_operator; |
|
| 5926 | - } |
|
| 5927 | - throw new EE_Error( |
|
| 5928 | - sprintf( |
|
| 5929 | - __( |
|
| 5930 | - "The operator '%s' is not in the list of valid operators: %s", |
|
| 5931 | - "event_espresso" |
|
| 5932 | - ), |
|
| 5933 | - $operator_supplied, |
|
| 5934 | - implode(",", array_keys($this->_valid_operators)) |
|
| 5935 | - ) |
|
| 5936 | - ); |
|
| 5937 | - } |
|
| 5938 | - |
|
| 5939 | - |
|
| 5940 | - |
|
| 5941 | - /** |
|
| 5942 | - * Gets the valid operators |
|
| 5943 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5944 | - */ |
|
| 5945 | - public function valid_operators() |
|
| 5946 | - { |
|
| 5947 | - return $this->_valid_operators; |
|
| 5948 | - } |
|
| 5949 | - |
|
| 5950 | - |
|
| 5951 | - |
|
| 5952 | - /** |
|
| 5953 | - * Gets the between-style operators (take 2 arguments). |
|
| 5954 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5955 | - */ |
|
| 5956 | - public function valid_between_style_operators() |
|
| 5957 | - { |
|
| 5958 | - return array_intersect( |
|
| 5959 | - $this->valid_operators(), |
|
| 5960 | - $this->_between_style_operators |
|
| 5961 | - ); |
|
| 5962 | - } |
|
| 5963 | - |
|
| 5964 | - /** |
|
| 5965 | - * Gets the "like"-style operators (take a single argument, but it may contain wildcards) |
|
| 5966 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5967 | - */ |
|
| 5968 | - public function valid_like_style_operators() |
|
| 5969 | - { |
|
| 5970 | - return array_intersect( |
|
| 5971 | - $this->valid_operators(), |
|
| 5972 | - $this->_like_style_operators |
|
| 5973 | - ); |
|
| 5974 | - } |
|
| 5975 | - |
|
| 5976 | - /** |
|
| 5977 | - * Gets the "in"-style operators |
|
| 5978 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5979 | - */ |
|
| 5980 | - public function valid_in_style_operators() |
|
| 5981 | - { |
|
| 5982 | - return array_intersect( |
|
| 5983 | - $this->valid_operators(), |
|
| 5984 | - $this->_in_style_operators |
|
| 5985 | - ); |
|
| 5986 | - } |
|
| 5987 | - |
|
| 5988 | - /** |
|
| 5989 | - * Gets the "null"-style operators (accept no arguments) |
|
| 5990 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5991 | - */ |
|
| 5992 | - public function valid_null_style_operators() |
|
| 5993 | - { |
|
| 5994 | - return array_intersect( |
|
| 5995 | - $this->valid_operators(), |
|
| 5996 | - $this->_null_style_operators |
|
| 5997 | - ); |
|
| 5998 | - } |
|
| 5999 | - |
|
| 6000 | - /** |
|
| 6001 | - * Gets an array where keys are the primary keys and values are their 'names' |
|
| 6002 | - * (as determined by the model object's name() function, which is often overridden) |
|
| 6003 | - * |
|
| 6004 | - * @param array $query_params like get_all's |
|
| 6005 | - * @return string[] |
|
| 6006 | - * @throws EE_Error |
|
| 6007 | - */ |
|
| 6008 | - public function get_all_names($query_params = array()) |
|
| 6009 | - { |
|
| 6010 | - $objs = $this->get_all($query_params); |
|
| 6011 | - $names = array(); |
|
| 6012 | - foreach ($objs as $obj) { |
|
| 6013 | - $names[ $obj->ID() ] = $obj->name(); |
|
| 6014 | - } |
|
| 6015 | - return $names; |
|
| 6016 | - } |
|
| 6017 | - |
|
| 6018 | - |
|
| 6019 | - |
|
| 6020 | - /** |
|
| 6021 | - * Gets an array of primary keys from the model objects. If you acquired the model objects |
|
| 6022 | - * using EEM_Base::get_all() you don't need to call this (and probably shouldn't because |
|
| 6023 | - * this is duplicated effort and reduces efficiency) you would be better to use |
|
| 6024 | - * array_keys() on $model_objects. |
|
| 6025 | - * |
|
| 6026 | - * @param \EE_Base_Class[] $model_objects |
|
| 6027 | - * @param boolean $filter_out_empty_ids if a model object has an ID of '' or 0, don't bother including it |
|
| 6028 | - * in the returned array |
|
| 6029 | - * @return array |
|
| 6030 | - * @throws EE_Error |
|
| 6031 | - */ |
|
| 6032 | - public function get_IDs($model_objects, $filter_out_empty_ids = false) |
|
| 6033 | - { |
|
| 6034 | - if (! $this->has_primary_key_field()) { |
|
| 6035 | - if (WP_DEBUG) { |
|
| 6036 | - EE_Error::add_error( |
|
| 6037 | - __('Trying to get IDs from a model than has no primary key', 'event_espresso'), |
|
| 6038 | - __FILE__, |
|
| 6039 | - __FUNCTION__, |
|
| 6040 | - __LINE__ |
|
| 6041 | - ); |
|
| 6042 | - } |
|
| 6043 | - } |
|
| 6044 | - $IDs = array(); |
|
| 6045 | - foreach ($model_objects as $model_object) { |
|
| 6046 | - $id = $model_object->ID(); |
|
| 6047 | - if (! $id) { |
|
| 6048 | - if ($filter_out_empty_ids) { |
|
| 6049 | - continue; |
|
| 6050 | - } |
|
| 6051 | - if (WP_DEBUG) { |
|
| 6052 | - EE_Error::add_error( |
|
| 6053 | - __( |
|
| 6054 | - 'Called %1$s on a model object that has no ID and so probably hasn\'t been saved to the database', |
|
| 6055 | - 'event_espresso' |
|
| 6056 | - ), |
|
| 6057 | - __FILE__, |
|
| 6058 | - __FUNCTION__, |
|
| 6059 | - __LINE__ |
|
| 6060 | - ); |
|
| 6061 | - } |
|
| 6062 | - } |
|
| 6063 | - $IDs[] = $id; |
|
| 6064 | - } |
|
| 6065 | - return $IDs; |
|
| 6066 | - } |
|
| 6067 | - |
|
| 6068 | - |
|
| 6069 | - |
|
| 6070 | - /** |
|
| 6071 | - * Returns the string used in capabilities relating to this model. If there |
|
| 6072 | - * are no capabilities that relate to this model returns false |
|
| 6073 | - * |
|
| 6074 | - * @return string|false |
|
| 6075 | - */ |
|
| 6076 | - public function cap_slug() |
|
| 6077 | - { |
|
| 6078 | - return apply_filters('FHEE__EEM_Base__cap_slug', $this->_caps_slug, $this); |
|
| 6079 | - } |
|
| 6080 | - |
|
| 6081 | - |
|
| 6082 | - |
|
| 6083 | - /** |
|
| 6084 | - * Returns the capability-restrictions array (@see EEM_Base::_cap_restrictions). |
|
| 6085 | - * If $context is provided (which should be set to one of EEM_Base::valid_cap_contexts()) |
|
| 6086 | - * only returns the cap restrictions array in that context (ie, the array |
|
| 6087 | - * at that key) |
|
| 6088 | - * |
|
| 6089 | - * @param string $context |
|
| 6090 | - * @return EE_Default_Where_Conditions[] indexed by associated capability |
|
| 6091 | - * @throws EE_Error |
|
| 6092 | - */ |
|
| 6093 | - public function cap_restrictions($context = EEM_Base::caps_read) |
|
| 6094 | - { |
|
| 6095 | - EEM_Base::verify_is_valid_cap_context($context); |
|
| 6096 | - // check if we ought to run the restriction generator first |
|
| 6097 | - if (isset($this->_cap_restriction_generators[ $context ]) |
|
| 6098 | - && $this->_cap_restriction_generators[ $context ] instanceof EE_Restriction_Generator_Base |
|
| 6099 | - && ! $this->_cap_restriction_generators[ $context ]->has_generated_cap_restrictions() |
|
| 6100 | - ) { |
|
| 6101 | - $this->_cap_restrictions[ $context ] = array_merge( |
|
| 6102 | - $this->_cap_restrictions[ $context ], |
|
| 6103 | - $this->_cap_restriction_generators[ $context ]->generate_restrictions() |
|
| 6104 | - ); |
|
| 6105 | - } |
|
| 6106 | - // and make sure we've finalized the construction of each restriction |
|
| 6107 | - foreach ($this->_cap_restrictions[ $context ] as $where_conditions_obj) { |
|
| 6108 | - if ($where_conditions_obj instanceof EE_Default_Where_Conditions) { |
|
| 6109 | - $where_conditions_obj->_finalize_construct($this); |
|
| 6110 | - } |
|
| 6111 | - } |
|
| 6112 | - return $this->_cap_restrictions[ $context ]; |
|
| 6113 | - } |
|
| 6114 | - |
|
| 6115 | - |
|
| 6116 | - |
|
| 6117 | - /** |
|
| 6118 | - * Indicating whether or not this model thinks its a wp core model |
|
| 6119 | - * |
|
| 6120 | - * @return boolean |
|
| 6121 | - */ |
|
| 6122 | - public function is_wp_core_model() |
|
| 6123 | - { |
|
| 6124 | - return $this->_wp_core_model; |
|
| 6125 | - } |
|
| 6126 | - |
|
| 6127 | - |
|
| 6128 | - |
|
| 6129 | - /** |
|
| 6130 | - * Gets all the caps that are missing which impose a restriction on |
|
| 6131 | - * queries made in this context |
|
| 6132 | - * |
|
| 6133 | - * @param string $context one of EEM_Base::caps_ constants |
|
| 6134 | - * @return EE_Default_Where_Conditions[] indexed by capability name |
|
| 6135 | - * @throws EE_Error |
|
| 6136 | - */ |
|
| 6137 | - public function caps_missing($context = EEM_Base::caps_read) |
|
| 6138 | - { |
|
| 6139 | - $missing_caps = array(); |
|
| 6140 | - $cap_restrictions = $this->cap_restrictions($context); |
|
| 6141 | - foreach ($cap_restrictions as $cap => $restriction_if_no_cap) { |
|
| 6142 | - if (! EE_Capabilities::instance() |
|
| 6143 | - ->current_user_can($cap, $this->get_this_model_name() . '_model_applying_caps') |
|
| 6144 | - ) { |
|
| 6145 | - $missing_caps[ $cap ] = $restriction_if_no_cap; |
|
| 6146 | - } |
|
| 6147 | - } |
|
| 6148 | - return $missing_caps; |
|
| 6149 | - } |
|
| 6150 | - |
|
| 6151 | - |
|
| 6152 | - |
|
| 6153 | - /** |
|
| 6154 | - * Gets the mapping from capability contexts to action strings used in capability names |
|
| 6155 | - * |
|
| 6156 | - * @return array keys are one of EEM_Base::valid_cap_contexts(), and values are usually |
|
| 6157 | - * one of 'read', 'edit', or 'delete' |
|
| 6158 | - */ |
|
| 6159 | - public function cap_contexts_to_cap_action_map() |
|
| 6160 | - { |
|
| 6161 | - return apply_filters( |
|
| 6162 | - 'FHEE__EEM_Base__cap_contexts_to_cap_action_map', |
|
| 6163 | - $this->_cap_contexts_to_cap_action_map, |
|
| 6164 | - $this |
|
| 6165 | - ); |
|
| 6166 | - } |
|
| 6167 | - |
|
| 6168 | - |
|
| 6169 | - |
|
| 6170 | - /** |
|
| 6171 | - * Gets the action string for the specified capability context |
|
| 6172 | - * |
|
| 6173 | - * @param string $context |
|
| 6174 | - * @return string one of EEM_Base::cap_contexts_to_cap_action_map() values |
|
| 6175 | - * @throws EE_Error |
|
| 6176 | - */ |
|
| 6177 | - public function cap_action_for_context($context) |
|
| 6178 | - { |
|
| 6179 | - $mapping = $this->cap_contexts_to_cap_action_map(); |
|
| 6180 | - if (isset($mapping[ $context ])) { |
|
| 6181 | - return $mapping[ $context ]; |
|
| 6182 | - } |
|
| 6183 | - if ($action = apply_filters('FHEE__EEM_Base__cap_action_for_context', null, $this, $mapping, $context)) { |
|
| 6184 | - return $action; |
|
| 6185 | - } |
|
| 6186 | - throw new EE_Error( |
|
| 6187 | - sprintf( |
|
| 6188 | - __('Cannot find capability restrictions for context "%1$s", allowed values are:%2$s', 'event_espresso'), |
|
| 6189 | - $context, |
|
| 6190 | - implode(',', array_keys($this->cap_contexts_to_cap_action_map())) |
|
| 6191 | - ) |
|
| 6192 | - ); |
|
| 6193 | - } |
|
| 6194 | - |
|
| 6195 | - |
|
| 6196 | - |
|
| 6197 | - /** |
|
| 6198 | - * Returns all the capability contexts which are valid when querying models |
|
| 6199 | - * |
|
| 6200 | - * @return array |
|
| 6201 | - */ |
|
| 6202 | - public static function valid_cap_contexts() |
|
| 6203 | - { |
|
| 6204 | - return apply_filters('FHEE__EEM_Base__valid_cap_contexts', array( |
|
| 6205 | - self::caps_read, |
|
| 6206 | - self::caps_read_admin, |
|
| 6207 | - self::caps_edit, |
|
| 6208 | - self::caps_delete, |
|
| 6209 | - )); |
|
| 6210 | - } |
|
| 6211 | - |
|
| 6212 | - |
|
| 6213 | - |
|
| 6214 | - /** |
|
| 6215 | - * Returns all valid options for 'default_where_conditions' |
|
| 6216 | - * |
|
| 6217 | - * @return array |
|
| 6218 | - */ |
|
| 6219 | - public static function valid_default_where_conditions() |
|
| 6220 | - { |
|
| 6221 | - return array( |
|
| 6222 | - EEM_Base::default_where_conditions_all, |
|
| 6223 | - EEM_Base::default_where_conditions_this_only, |
|
| 6224 | - EEM_Base::default_where_conditions_others_only, |
|
| 6225 | - EEM_Base::default_where_conditions_minimum_all, |
|
| 6226 | - EEM_Base::default_where_conditions_minimum_others, |
|
| 6227 | - EEM_Base::default_where_conditions_none |
|
| 6228 | - ); |
|
| 6229 | - } |
|
| 6230 | - |
|
| 6231 | - // public static function default_where_conditions_full |
|
| 6232 | - /** |
|
| 6233 | - * Verifies $context is one of EEM_Base::valid_cap_contexts(), if not it throws an exception |
|
| 6234 | - * |
|
| 6235 | - * @param string $context |
|
| 6236 | - * @return bool |
|
| 6237 | - * @throws EE_Error |
|
| 6238 | - */ |
|
| 6239 | - public static function verify_is_valid_cap_context($context) |
|
| 6240 | - { |
|
| 6241 | - $valid_cap_contexts = EEM_Base::valid_cap_contexts(); |
|
| 6242 | - if (in_array($context, $valid_cap_contexts)) { |
|
| 6243 | - return true; |
|
| 6244 | - } |
|
| 6245 | - throw new EE_Error( |
|
| 6246 | - sprintf( |
|
| 6247 | - __( |
|
| 6248 | - 'Context "%1$s" passed into model "%2$s" is not a valid context. They are: %3$s', |
|
| 6249 | - 'event_espresso' |
|
| 6250 | - ), |
|
| 6251 | - $context, |
|
| 6252 | - 'EEM_Base', |
|
| 6253 | - implode(',', $valid_cap_contexts) |
|
| 6254 | - ) |
|
| 6255 | - ); |
|
| 6256 | - } |
|
| 6257 | - |
|
| 6258 | - |
|
| 6259 | - |
|
| 6260 | - /** |
|
| 6261 | - * Clears all the models field caches. This is only useful when a sub-class |
|
| 6262 | - * might have added a field or something and these caches might be invalidated |
|
| 6263 | - */ |
|
| 6264 | - protected function _invalidate_field_caches() |
|
| 6265 | - { |
|
| 6266 | - $this->_cache_foreign_key_to_fields = array(); |
|
| 6267 | - $this->_cached_fields = null; |
|
| 6268 | - $this->_cached_fields_non_db_only = null; |
|
| 6269 | - } |
|
| 6270 | - |
|
| 6271 | - |
|
| 6272 | - |
|
| 6273 | - /** |
|
| 6274 | - * Gets the list of all the where query param keys that relate to logic instead of field names |
|
| 6275 | - * (eg "and", "or", "not"). |
|
| 6276 | - * |
|
| 6277 | - * @return array |
|
| 6278 | - */ |
|
| 6279 | - public function logic_query_param_keys() |
|
| 6280 | - { |
|
| 6281 | - return $this->_logic_query_param_keys; |
|
| 6282 | - } |
|
| 6283 | - |
|
| 6284 | - |
|
| 6285 | - |
|
| 6286 | - /** |
|
| 6287 | - * Determines whether or not the where query param array key is for a logic query param. |
|
| 6288 | - * Eg 'OR', 'not*', and 'and*because-i-say-so' should all return true, whereas |
|
| 6289 | - * 'ATT_fname', 'EVT_name*not-you-or-me', and 'ORG_name' should return false |
|
| 6290 | - * |
|
| 6291 | - * @param $query_param_key |
|
| 6292 | - * @return bool |
|
| 6293 | - */ |
|
| 6294 | - public function is_logic_query_param_key($query_param_key) |
|
| 6295 | - { |
|
| 6296 | - foreach ($this->logic_query_param_keys() as $logic_query_param_key) { |
|
| 6297 | - if ($query_param_key === $logic_query_param_key |
|
| 6298 | - || strpos($query_param_key, $logic_query_param_key . '*') === 0 |
|
| 6299 | - ) { |
|
| 6300 | - return true; |
|
| 6301 | - } |
|
| 6302 | - } |
|
| 6303 | - return false; |
|
| 6304 | - } |
|
| 6305 | - |
|
| 6306 | - /** |
|
| 6307 | - * Returns true if this model has a password field on it (regardless of whether that password field has any content) |
|
| 6308 | - * @since 4.9.74.p |
|
| 6309 | - * @return boolean |
|
| 6310 | - */ |
|
| 6311 | - public function hasPassword() |
|
| 6312 | - { |
|
| 6313 | - // if we don't yet know if there's a password field, find out and remember it for next time. |
|
| 6314 | - if ($this->has_password_field === null) { |
|
| 6315 | - $password_field = $this->getPasswordField(); |
|
| 6316 | - $this->has_password_field = $password_field instanceof EE_Password_Field ? true : false; |
|
| 6317 | - } |
|
| 6318 | - return $this->has_password_field; |
|
| 6319 | - } |
|
| 6320 | - |
|
| 6321 | - /** |
|
| 6322 | - * Returns the password field on this model, if there is one |
|
| 6323 | - * @since 4.9.74.p |
|
| 6324 | - * @return EE_Password_Field|null |
|
| 6325 | - */ |
|
| 6326 | - public function getPasswordField() |
|
| 6327 | - { |
|
| 6328 | - // if we definetely already know there is a password field or not (because has_password_field is true or false) |
|
| 6329 | - // there's no need to search for it. If we don't know yet, then find out |
|
| 6330 | - if ($this->has_password_field === null && $this->password_field === null) { |
|
| 6331 | - $this->password_field = $this->get_a_field_of_type('EE_Password_Field'); |
|
| 6332 | - } |
|
| 6333 | - // don't bother setting has_password_field because that's hasPassword()'s job. |
|
| 6334 | - return $this->password_field; |
|
| 6335 | - } |
|
| 6336 | - |
|
| 6337 | - |
|
| 6338 | - /** |
|
| 6339 | - * Returns the list of field (as EE_Model_Field_Bases) that are protected by the password |
|
| 6340 | - * @since 4.9.74.p |
|
| 6341 | - * @return EE_Model_Field_Base[] |
|
| 6342 | - * @throws EE_Error |
|
| 6343 | - */ |
|
| 6344 | - public function getPasswordProtectedFields() |
|
| 6345 | - { |
|
| 6346 | - $password_field = $this->getPasswordField(); |
|
| 6347 | - $fields = array(); |
|
| 6348 | - if ($password_field instanceof EE_Password_Field) { |
|
| 6349 | - $field_names = $password_field->protectedFields(); |
|
| 6350 | - foreach ($field_names as $field_name) { |
|
| 6351 | - $fields[ $field_name ] = $this->field_settings_for($field_name); |
|
| 6352 | - } |
|
| 6353 | - } |
|
| 6354 | - return $fields; |
|
| 6355 | - } |
|
| 6356 | - |
|
| 6357 | - |
|
| 6358 | - /** |
|
| 6359 | - * Checks if the current user can perform the requested action on this model |
|
| 6360 | - * @since 4.9.74.p |
|
| 6361 | - * @param string $cap_to_check one of the array keys from _cap_contexts_to_cap_action_map |
|
| 6362 | - * @param EE_Base_Class|array $model_obj_or_fields_n_values |
|
| 6363 | - * @return bool |
|
| 6364 | - * @throws EE_Error |
|
| 6365 | - * @throws InvalidArgumentException |
|
| 6366 | - * @throws InvalidDataTypeException |
|
| 6367 | - * @throws InvalidInterfaceException |
|
| 6368 | - * @throws ReflectionException |
|
| 6369 | - * @throws UnexpectedEntityException |
|
| 6370 | - */ |
|
| 6371 | - public function currentUserCan($cap_to_check, $model_obj_or_fields_n_values) |
|
| 6372 | - { |
|
| 6373 | - if ($model_obj_or_fields_n_values instanceof EE_Base_Class) { |
|
| 6374 | - $model_obj_or_fields_n_values = $model_obj_or_fields_n_values->model_field_array(); |
|
| 6375 | - } |
|
| 6376 | - if (!is_array($model_obj_or_fields_n_values)) { |
|
| 6377 | - throw new UnexpectedEntityException( |
|
| 6378 | - $model_obj_or_fields_n_values, |
|
| 6379 | - 'EE_Base_Class', |
|
| 6380 | - sprintf( |
|
| 6381 | - esc_html__('%1$s must be passed an `EE_Base_Class or an array of fields names with their values. You passed in something different.', 'event_espresso'), |
|
| 6382 | - __FUNCTION__ |
|
| 6383 | - ) |
|
| 6384 | - ); |
|
| 6385 | - } |
|
| 6386 | - return $this->exists( |
|
| 6387 | - $this->alter_query_params_to_restrict_by_ID( |
|
| 6388 | - $this->get_index_primary_key_string($model_obj_or_fields_n_values), |
|
| 6389 | - array( |
|
| 6390 | - 'default_where_conditions' => 'none', |
|
| 6391 | - 'caps' => $cap_to_check, |
|
| 6392 | - ) |
|
| 6393 | - ) |
|
| 6394 | - ); |
|
| 6395 | - } |
|
| 6396 | - |
|
| 6397 | - /** |
|
| 6398 | - * Returns the query param where conditions key to the password affecting this model. |
|
| 6399 | - * Eg on EEM_Event this would just be "password", on EEM_Datetime this would be "Event.password", etc. |
|
| 6400 | - * @since 4.9.74.p |
|
| 6401 | - * @return null|string |
|
| 6402 | - * @throws EE_Error |
|
| 6403 | - * @throws InvalidArgumentException |
|
| 6404 | - * @throws InvalidDataTypeException |
|
| 6405 | - * @throws InvalidInterfaceException |
|
| 6406 | - * @throws ModelConfigurationException |
|
| 6407 | - * @throws ReflectionException |
|
| 6408 | - */ |
|
| 6409 | - public function modelChainAndPassword() |
|
| 6410 | - { |
|
| 6411 | - if ($this->model_chain_to_password === null) { |
|
| 6412 | - throw new ModelConfigurationException( |
|
| 6413 | - $this, |
|
| 6414 | - esc_html_x( |
|
| 6415 | - // @codingStandardsIgnoreStart |
|
| 6416 | - 'Cannot exclude protected data because the model has not specified which model has the password.', |
|
| 6417 | - // @codingStandardsIgnoreEnd |
|
| 6418 | - '1: model name', |
|
| 6419 | - 'event_espresso' |
|
| 6420 | - ) |
|
| 6421 | - ); |
|
| 6422 | - } |
|
| 6423 | - if ($this->model_chain_to_password === '') { |
|
| 6424 | - $model_with_password = $this; |
|
| 6425 | - } else { |
|
| 6426 | - if ($pos_of_period = strrpos($this->model_chain_to_password, '.')) { |
|
| 6427 | - $last_model_in_chain = substr($this->model_chain_to_password, $pos_of_period + 1); |
|
| 6428 | - } else { |
|
| 6429 | - $last_model_in_chain = $this->model_chain_to_password; |
|
| 6430 | - } |
|
| 6431 | - $model_with_password = EE_Registry::instance()->load_model($last_model_in_chain); |
|
| 6432 | - } |
|
| 6433 | - |
|
| 6434 | - $password_field = $model_with_password->getPasswordField(); |
|
| 6435 | - if ($password_field instanceof EE_Password_Field) { |
|
| 6436 | - $password_field_name = $password_field->get_name(); |
|
| 6437 | - } else { |
|
| 6438 | - throw new ModelConfigurationException( |
|
| 6439 | - $this, |
|
| 6440 | - sprintf( |
|
| 6441 | - esc_html_x( |
|
| 6442 | - 'This model claims related model "%1$s" should have a password field on it, but none was found. The model relation chain is "%2$s"', |
|
| 6443 | - '1: model name, 2: special string', |
|
| 6444 | - 'event_espresso' |
|
| 6445 | - ), |
|
| 6446 | - $model_with_password->get_this_model_name(), |
|
| 6447 | - $this->model_chain_to_password |
|
| 6448 | - ) |
|
| 6449 | - ); |
|
| 6450 | - } |
|
| 6451 | - return ($this->model_chain_to_password ? $this->model_chain_to_password . '.' : '') . $password_field_name; |
|
| 6452 | - } |
|
| 6453 | - |
|
| 6454 | - /** |
|
| 6455 | - * Returns true if there is a password on a related model which restricts access to some of this model's rows, |
|
| 6456 | - * or if this model itself has a password affecting access to some of its other fields. |
|
| 6457 | - * @since 4.9.74.p |
|
| 6458 | - * @return boolean |
|
| 6459 | - */ |
|
| 6460 | - public function restrictedByRelatedModelPassword() |
|
| 6461 | - { |
|
| 6462 | - return $this->model_chain_to_password !== null; |
|
| 6463 | - } |
|
| 3787 | + } |
|
| 3788 | + return $null_friendly_where_conditions; |
|
| 3789 | + } |
|
| 3790 | + |
|
| 3791 | + |
|
| 3792 | + |
|
| 3793 | + /** |
|
| 3794 | + * Uses the _default_where_conditions_strategy set during __construct() to get |
|
| 3795 | + * default where conditions on all get_all, update, and delete queries done by this model. |
|
| 3796 | + * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
| 3797 | + * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
| 3798 | + * |
|
| 3799 | + * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
| 3800 | + * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 3801 | + */ |
|
| 3802 | + private function _get_default_where_conditions($model_relation_path = null) |
|
| 3803 | + { |
|
| 3804 | + if ($this->_ignore_where_strategy) { |
|
| 3805 | + return array(); |
|
| 3806 | + } |
|
| 3807 | + return $this->_default_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
| 3808 | + } |
|
| 3809 | + |
|
| 3810 | + |
|
| 3811 | + |
|
| 3812 | + /** |
|
| 3813 | + * Uses the _minimum_where_conditions_strategy set during __construct() to get |
|
| 3814 | + * minimum where conditions on all get_all, update, and delete queries done by this model. |
|
| 3815 | + * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
| 3816 | + * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
| 3817 | + * Similar to _get_default_where_conditions |
|
| 3818 | + * |
|
| 3819 | + * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
| 3820 | + * @return array @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 3821 | + */ |
|
| 3822 | + protected function _get_minimum_where_conditions($model_relation_path = null) |
|
| 3823 | + { |
|
| 3824 | + if ($this->_ignore_where_strategy) { |
|
| 3825 | + return array(); |
|
| 3826 | + } |
|
| 3827 | + return $this->_minimum_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
| 3828 | + } |
|
| 3829 | + |
|
| 3830 | + |
|
| 3831 | + |
|
| 3832 | + /** |
|
| 3833 | + * Creates the string of SQL for the select part of a select query, everything behind SELECT and before FROM. |
|
| 3834 | + * Eg, "Event.post_id, Event.post_name,Event_Detail.EVT_ID..." |
|
| 3835 | + * |
|
| 3836 | + * @param EE_Model_Query_Info_Carrier $model_query_info |
|
| 3837 | + * @return string |
|
| 3838 | + * @throws EE_Error |
|
| 3839 | + */ |
|
| 3840 | + private function _construct_default_select_sql(EE_Model_Query_Info_Carrier $model_query_info) |
|
| 3841 | + { |
|
| 3842 | + $selects = $this->_get_columns_to_select_for_this_model(); |
|
| 3843 | + foreach ($model_query_info->get_model_names_included() as $model_relation_chain => |
|
| 3844 | + $name_of_other_model_included) { |
|
| 3845 | + $other_model_included = $this->get_related_model_obj($name_of_other_model_included); |
|
| 3846 | + $other_model_selects = $other_model_included->_get_columns_to_select_for_this_model($model_relation_chain); |
|
| 3847 | + foreach ($other_model_selects as $key => $value) { |
|
| 3848 | + $selects[] = $value; |
|
| 3849 | + } |
|
| 3850 | + } |
|
| 3851 | + return implode(", ", $selects); |
|
| 3852 | + } |
|
| 3853 | + |
|
| 3854 | + |
|
| 3855 | + |
|
| 3856 | + /** |
|
| 3857 | + * Gets an array of columns to select for this model, which are necessary for it to create its objects. |
|
| 3858 | + * So that's going to be the columns for all the fields on the model |
|
| 3859 | + * |
|
| 3860 | + * @param string $model_relation_chain like 'Question.Question_Group.Event' |
|
| 3861 | + * @return array numerically indexed, values are columns to select and rename, eg "Event.ID AS 'Event.ID'" |
|
| 3862 | + */ |
|
| 3863 | + public function _get_columns_to_select_for_this_model($model_relation_chain = '') |
|
| 3864 | + { |
|
| 3865 | + $fields = $this->field_settings(); |
|
| 3866 | + $selects = array(); |
|
| 3867 | + $table_alias_with_model_relation_chain_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 3868 | + $model_relation_chain, |
|
| 3869 | + $this->get_this_model_name() |
|
| 3870 | + ); |
|
| 3871 | + foreach ($fields as $field_obj) { |
|
| 3872 | + $selects[] = $table_alias_with_model_relation_chain_prefix |
|
| 3873 | + . $field_obj->get_table_alias() |
|
| 3874 | + . "." |
|
| 3875 | + . $field_obj->get_table_column() |
|
| 3876 | + . " AS '" |
|
| 3877 | + . $table_alias_with_model_relation_chain_prefix |
|
| 3878 | + . $field_obj->get_table_alias() |
|
| 3879 | + . "." |
|
| 3880 | + . $field_obj->get_table_column() |
|
| 3881 | + . "'"; |
|
| 3882 | + } |
|
| 3883 | + // make sure we are also getting the PKs of each table |
|
| 3884 | + $tables = $this->get_tables(); |
|
| 3885 | + if (count($tables) > 1) { |
|
| 3886 | + foreach ($tables as $table_obj) { |
|
| 3887 | + $qualified_pk_column = $table_alias_with_model_relation_chain_prefix |
|
| 3888 | + . $table_obj->get_fully_qualified_pk_column(); |
|
| 3889 | + if (! in_array($qualified_pk_column, $selects)) { |
|
| 3890 | + $selects[] = "$qualified_pk_column AS '$qualified_pk_column'"; |
|
| 3891 | + } |
|
| 3892 | + } |
|
| 3893 | + } |
|
| 3894 | + return $selects; |
|
| 3895 | + } |
|
| 3896 | + |
|
| 3897 | + |
|
| 3898 | + |
|
| 3899 | + /** |
|
| 3900 | + * Given a $query_param like 'Registration.Transaction.TXN_ID', pops off 'Registration.', |
|
| 3901 | + * gets the join statement for it; gets the data types for it; and passes the remaining 'Transaction.TXN_ID' |
|
| 3902 | + * onto its related Transaction object to do the same. Returns an EE_Join_And_Data_Types object which contains the |
|
| 3903 | + * SQL for joining, and the data types |
|
| 3904 | + * |
|
| 3905 | + * @param null|string $original_query_param |
|
| 3906 | + * @param string $query_param like Registration.Transaction.TXN_ID |
|
| 3907 | + * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
| 3908 | + * @param string $query_param_type like Registration.Transaction.TXN_ID |
|
| 3909 | + * or 'PAY_ID'. Otherwise, we don't expect there to be a |
|
| 3910 | + * column name. We only want model names, eg 'Event.Venue' |
|
| 3911 | + * or 'Registration's |
|
| 3912 | + * @param string $original_query_param what it originally was (eg |
|
| 3913 | + * Registration.Transaction.TXN_ID). If null, we assume it |
|
| 3914 | + * matches $query_param |
|
| 3915 | + * @throws EE_Error |
|
| 3916 | + * @return void only modifies the EEM_Related_Model_Info_Carrier passed into it |
|
| 3917 | + */ |
|
| 3918 | + private function _extract_related_model_info_from_query_param( |
|
| 3919 | + $query_param, |
|
| 3920 | + EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
| 3921 | + $query_param_type, |
|
| 3922 | + $original_query_param = null |
|
| 3923 | + ) { |
|
| 3924 | + if ($original_query_param === null) { |
|
| 3925 | + $original_query_param = $query_param; |
|
| 3926 | + } |
|
| 3927 | + $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param); |
|
| 3928 | + /** @var $allow_logic_query_params bool whether or not to allow logic_query_params like 'NOT','OR', or 'AND' */ |
|
| 3929 | + $allow_logic_query_params = in_array($query_param_type, array('where', 'having', 0, 'custom_selects'), true); |
|
| 3930 | + $allow_fields = in_array( |
|
| 3931 | + $query_param_type, |
|
| 3932 | + array('where', 'having', 'order_by', 'group_by', 'order', 'custom_selects', 0), |
|
| 3933 | + true |
|
| 3934 | + ); |
|
| 3935 | + // check to see if we have a field on this model |
|
| 3936 | + $this_model_fields = $this->field_settings(true); |
|
| 3937 | + if (array_key_exists($query_param, $this_model_fields)) { |
|
| 3938 | + if ($allow_fields) { |
|
| 3939 | + return; |
|
| 3940 | + } |
|
| 3941 | + throw new EE_Error( |
|
| 3942 | + sprintf( |
|
| 3943 | + __( |
|
| 3944 | + "Using a field name (%s) on model %s is not allowed on this query param type '%s'. Original query param was %s", |
|
| 3945 | + "event_espresso" |
|
| 3946 | + ), |
|
| 3947 | + $query_param, |
|
| 3948 | + get_class($this), |
|
| 3949 | + $query_param_type, |
|
| 3950 | + $original_query_param |
|
| 3951 | + ) |
|
| 3952 | + ); |
|
| 3953 | + } |
|
| 3954 | + // check if this is a special logic query param |
|
| 3955 | + if (in_array($query_param, $this->_logic_query_param_keys, true)) { |
|
| 3956 | + if ($allow_logic_query_params) { |
|
| 3957 | + return; |
|
| 3958 | + } |
|
| 3959 | + throw new EE_Error( |
|
| 3960 | + sprintf( |
|
| 3961 | + __( |
|
| 3962 | + 'Logic query params ("%1$s") are being used incorrectly with the following query param ("%2$s") on model %3$s. %4$sAdditional Info:%4$s%5$s', |
|
| 3963 | + 'event_espresso' |
|
| 3964 | + ), |
|
| 3965 | + implode('", "', $this->_logic_query_param_keys), |
|
| 3966 | + $query_param, |
|
| 3967 | + get_class($this), |
|
| 3968 | + '<br />', |
|
| 3969 | + "\t" |
|
| 3970 | + . ' $passed_in_query_info = <pre>' |
|
| 3971 | + . print_r($passed_in_query_info, true) |
|
| 3972 | + . '</pre>' |
|
| 3973 | + . "\n\t" |
|
| 3974 | + . ' $query_param_type = ' |
|
| 3975 | + . $query_param_type |
|
| 3976 | + . "\n\t" |
|
| 3977 | + . ' $original_query_param = ' |
|
| 3978 | + . $original_query_param |
|
| 3979 | + ) |
|
| 3980 | + ); |
|
| 3981 | + } |
|
| 3982 | + // check if it's a custom selection |
|
| 3983 | + if ($this->_custom_selections instanceof CustomSelects |
|
| 3984 | + && in_array($query_param, $this->_custom_selections->columnAliases(), true) |
|
| 3985 | + ) { |
|
| 3986 | + return; |
|
| 3987 | + } |
|
| 3988 | + // check if has a model name at the beginning |
|
| 3989 | + // and |
|
| 3990 | + // check if it's a field on a related model |
|
| 3991 | + if ($this->extractJoinModelFromQueryParams( |
|
| 3992 | + $passed_in_query_info, |
|
| 3993 | + $query_param, |
|
| 3994 | + $original_query_param, |
|
| 3995 | + $query_param_type |
|
| 3996 | + )) { |
|
| 3997 | + return; |
|
| 3998 | + } |
|
| 3999 | + |
|
| 4000 | + // ok so $query_param didn't start with a model name |
|
| 4001 | + // and we previously confirmed it wasn't a logic query param or field on the current model |
|
| 4002 | + // it's wack, that's what it is |
|
| 4003 | + throw new EE_Error( |
|
| 4004 | + sprintf( |
|
| 4005 | + esc_html__( |
|
| 4006 | + "There is no model named '%s' related to %s. Query param type is %s and original query param is %s", |
|
| 4007 | + "event_espresso" |
|
| 4008 | + ), |
|
| 4009 | + $query_param, |
|
| 4010 | + get_class($this), |
|
| 4011 | + $query_param_type, |
|
| 4012 | + $original_query_param |
|
| 4013 | + ) |
|
| 4014 | + ); |
|
| 4015 | + } |
|
| 4016 | + |
|
| 4017 | + |
|
| 4018 | + /** |
|
| 4019 | + * Extracts any possible join model information from the provided possible_join_string. |
|
| 4020 | + * This method will read the provided $possible_join_string value and determine if there are any possible model join |
|
| 4021 | + * parts that should be added to the query. |
|
| 4022 | + * |
|
| 4023 | + * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
| 4024 | + * @param string $possible_join_string Such as Registration.REG_ID, or Registration |
|
| 4025 | + * @param null|string $original_query_param |
|
| 4026 | + * @param string $query_parameter_type The type for the source of the $possible_join_string |
|
| 4027 | + * ('where', 'order_by', 'group_by', 'custom_selects' etc.) |
|
| 4028 | + * @return bool returns true if a join was added and false if not. |
|
| 4029 | + * @throws EE_Error |
|
| 4030 | + */ |
|
| 4031 | + private function extractJoinModelFromQueryParams( |
|
| 4032 | + EE_Model_Query_Info_Carrier $query_info_carrier, |
|
| 4033 | + $possible_join_string, |
|
| 4034 | + $original_query_param, |
|
| 4035 | + $query_parameter_type |
|
| 4036 | + ) { |
|
| 4037 | + foreach ($this->_model_relations as $valid_related_model_name => $relation_obj) { |
|
| 4038 | + if (strpos($possible_join_string, $valid_related_model_name . ".") === 0) { |
|
| 4039 | + $this->_add_join_to_model($valid_related_model_name, $query_info_carrier, $original_query_param); |
|
| 4040 | + $possible_join_string = substr($possible_join_string, strlen($valid_related_model_name . ".")); |
|
| 4041 | + if ($possible_join_string === '') { |
|
| 4042 | + // nothing left to $query_param |
|
| 4043 | + // we should actually end in a field name, not a model like this! |
|
| 4044 | + throw new EE_Error( |
|
| 4045 | + sprintf( |
|
| 4046 | + esc_html__( |
|
| 4047 | + "Query param '%s' (of type %s on model %s) shouldn't end on a period (.) ", |
|
| 4048 | + "event_espresso" |
|
| 4049 | + ), |
|
| 4050 | + $possible_join_string, |
|
| 4051 | + $query_parameter_type, |
|
| 4052 | + get_class($this), |
|
| 4053 | + $valid_related_model_name |
|
| 4054 | + ) |
|
| 4055 | + ); |
|
| 4056 | + } |
|
| 4057 | + $related_model_obj = $this->get_related_model_obj($valid_related_model_name); |
|
| 4058 | + $related_model_obj->_extract_related_model_info_from_query_param( |
|
| 4059 | + $possible_join_string, |
|
| 4060 | + $query_info_carrier, |
|
| 4061 | + $query_parameter_type, |
|
| 4062 | + $original_query_param |
|
| 4063 | + ); |
|
| 4064 | + return true; |
|
| 4065 | + } |
|
| 4066 | + if ($possible_join_string === $valid_related_model_name) { |
|
| 4067 | + $this->_add_join_to_model( |
|
| 4068 | + $valid_related_model_name, |
|
| 4069 | + $query_info_carrier, |
|
| 4070 | + $original_query_param |
|
| 4071 | + ); |
|
| 4072 | + return true; |
|
| 4073 | + } |
|
| 4074 | + } |
|
| 4075 | + return false; |
|
| 4076 | + } |
|
| 4077 | + |
|
| 4078 | + |
|
| 4079 | + /** |
|
| 4080 | + * Extracts related models from Custom Selects and sets up any joins for those related models. |
|
| 4081 | + * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
| 4082 | + * @throws EE_Error |
|
| 4083 | + */ |
|
| 4084 | + private function extractRelatedModelsFromCustomSelects(EE_Model_Query_Info_Carrier $query_info_carrier) |
|
| 4085 | + { |
|
| 4086 | + if ($this->_custom_selections instanceof CustomSelects |
|
| 4087 | + && ($this->_custom_selections->type() === CustomSelects::TYPE_STRUCTURED |
|
| 4088 | + || $this->_custom_selections->type() == CustomSelects::TYPE_COMPLEX |
|
| 4089 | + ) |
|
| 4090 | + ) { |
|
| 4091 | + $original_selects = $this->_custom_selections->originalSelects(); |
|
| 4092 | + foreach ($original_selects as $alias => $select_configuration) { |
|
| 4093 | + $this->extractJoinModelFromQueryParams( |
|
| 4094 | + $query_info_carrier, |
|
| 4095 | + $select_configuration[0], |
|
| 4096 | + $select_configuration[0], |
|
| 4097 | + 'custom_selects' |
|
| 4098 | + ); |
|
| 4099 | + } |
|
| 4100 | + } |
|
| 4101 | + } |
|
| 4102 | + |
|
| 4103 | + |
|
| 4104 | + |
|
| 4105 | + /** |
|
| 4106 | + * Privately used by _extract_related_model_info_from_query_param to add a join to $model_name |
|
| 4107 | + * and store it on $passed_in_query_info |
|
| 4108 | + * |
|
| 4109 | + * @param string $model_name |
|
| 4110 | + * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
| 4111 | + * @param string $original_query_param used to extract the relation chain between the queried |
|
| 4112 | + * model and $model_name. Eg, if we are querying Event, |
|
| 4113 | + * and are adding a join to 'Payment' with the original |
|
| 4114 | + * query param key |
|
| 4115 | + * 'Registration.Transaction.Payment.PAY_amount', we want |
|
| 4116 | + * to extract 'Registration.Transaction.Payment', in case |
|
| 4117 | + * Payment wants to add default query params so that it |
|
| 4118 | + * will know what models to prepend onto its default query |
|
| 4119 | + * params or in case it wants to rename tables (in case |
|
| 4120 | + * there are multiple joins to the same table) |
|
| 4121 | + * @return void |
|
| 4122 | + * @throws EE_Error |
|
| 4123 | + */ |
|
| 4124 | + private function _add_join_to_model( |
|
| 4125 | + $model_name, |
|
| 4126 | + EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
| 4127 | + $original_query_param |
|
| 4128 | + ) { |
|
| 4129 | + $relation_obj = $this->related_settings_for($model_name); |
|
| 4130 | + $model_relation_chain = EE_Model_Parser::extract_model_relation_chain($model_name, $original_query_param); |
|
| 4131 | + // check if the relation is HABTM, because then we're essentially doing two joins |
|
| 4132 | + // If so, join first to the JOIN table, and add its data types, and then continue as normal |
|
| 4133 | + if ($relation_obj instanceof EE_HABTM_Relation) { |
|
| 4134 | + $join_model_obj = $relation_obj->get_join_model(); |
|
| 4135 | + // replace the model specified with the join model for this relation chain, whi |
|
| 4136 | + $relation_chain_to_join_model = EE_Model_Parser::replace_model_name_with_join_model_name_in_model_relation_chain( |
|
| 4137 | + $model_name, |
|
| 4138 | + $join_model_obj->get_this_model_name(), |
|
| 4139 | + $model_relation_chain |
|
| 4140 | + ); |
|
| 4141 | + $passed_in_query_info->merge( |
|
| 4142 | + new EE_Model_Query_Info_Carrier( |
|
| 4143 | + array($relation_chain_to_join_model => $join_model_obj->get_this_model_name()), |
|
| 4144 | + $relation_obj->get_join_to_intermediate_model_statement($relation_chain_to_join_model) |
|
| 4145 | + ) |
|
| 4146 | + ); |
|
| 4147 | + } |
|
| 4148 | + // now just join to the other table pointed to by the relation object, and add its data types |
|
| 4149 | + $passed_in_query_info->merge( |
|
| 4150 | + new EE_Model_Query_Info_Carrier( |
|
| 4151 | + array($model_relation_chain => $model_name), |
|
| 4152 | + $relation_obj->get_join_statement($model_relation_chain) |
|
| 4153 | + ) |
|
| 4154 | + ); |
|
| 4155 | + } |
|
| 4156 | + |
|
| 4157 | + |
|
| 4158 | + |
|
| 4159 | + /** |
|
| 4160 | + * Constructs SQL for where clause, like "WHERE Event.ID = 23 AND Transaction.amount > 100" etc. |
|
| 4161 | + * |
|
| 4162 | + * @param array $where_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 4163 | + * @return string of SQL |
|
| 4164 | + * @throws EE_Error |
|
| 4165 | + */ |
|
| 4166 | + private function _construct_where_clause($where_params) |
|
| 4167 | + { |
|
| 4168 | + $SQL = $this->_construct_condition_clause_recursive($where_params, ' AND '); |
|
| 4169 | + if ($SQL) { |
|
| 4170 | + return " WHERE " . $SQL; |
|
| 4171 | + } |
|
| 4172 | + return ''; |
|
| 4173 | + } |
|
| 4174 | + |
|
| 4175 | + |
|
| 4176 | + |
|
| 4177 | + /** |
|
| 4178 | + * Just like the _construct_where_clause, except prepends 'HAVING' instead of 'WHERE', |
|
| 4179 | + * and should be passed HAVING parameters, not WHERE parameters |
|
| 4180 | + * |
|
| 4181 | + * @param array $having_params |
|
| 4182 | + * @return string |
|
| 4183 | + * @throws EE_Error |
|
| 4184 | + */ |
|
| 4185 | + private function _construct_having_clause($having_params) |
|
| 4186 | + { |
|
| 4187 | + $SQL = $this->_construct_condition_clause_recursive($having_params, ' AND '); |
|
| 4188 | + if ($SQL) { |
|
| 4189 | + return " HAVING " . $SQL; |
|
| 4190 | + } |
|
| 4191 | + return ''; |
|
| 4192 | + } |
|
| 4193 | + |
|
| 4194 | + |
|
| 4195 | + /** |
|
| 4196 | + * Used for creating nested WHERE conditions. Eg "WHERE ! (Event.ID = 3 OR ( Event_Meta.meta_key = 'bob' AND |
|
| 4197 | + * Event_Meta.meta_value = 'foo'))" |
|
| 4198 | + * |
|
| 4199 | + * @param array $where_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 4200 | + * @param string $glue joins each subclause together. Should really only be " AND " or " OR "... |
|
| 4201 | + * @throws EE_Error |
|
| 4202 | + * @return string of SQL |
|
| 4203 | + */ |
|
| 4204 | + private function _construct_condition_clause_recursive($where_params, $glue = ' AND') |
|
| 4205 | + { |
|
| 4206 | + $where_clauses = array(); |
|
| 4207 | + foreach ($where_params as $query_param => $op_and_value_or_sub_condition) { |
|
| 4208 | + $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param);// str_replace("*",'',$query_param); |
|
| 4209 | + if (in_array($query_param, $this->_logic_query_param_keys)) { |
|
| 4210 | + switch ($query_param) { |
|
| 4211 | + case 'not': |
|
| 4212 | + case 'NOT': |
|
| 4213 | + $where_clauses[] = "! (" |
|
| 4214 | + . $this->_construct_condition_clause_recursive( |
|
| 4215 | + $op_and_value_or_sub_condition, |
|
| 4216 | + $glue |
|
| 4217 | + ) |
|
| 4218 | + . ")"; |
|
| 4219 | + break; |
|
| 4220 | + case 'and': |
|
| 4221 | + case 'AND': |
|
| 4222 | + $where_clauses[] = " (" |
|
| 4223 | + . $this->_construct_condition_clause_recursive( |
|
| 4224 | + $op_and_value_or_sub_condition, |
|
| 4225 | + ' AND ' |
|
| 4226 | + ) |
|
| 4227 | + . ")"; |
|
| 4228 | + break; |
|
| 4229 | + case 'or': |
|
| 4230 | + case 'OR': |
|
| 4231 | + $where_clauses[] = " (" |
|
| 4232 | + . $this->_construct_condition_clause_recursive( |
|
| 4233 | + $op_and_value_or_sub_condition, |
|
| 4234 | + ' OR ' |
|
| 4235 | + ) |
|
| 4236 | + . ")"; |
|
| 4237 | + break; |
|
| 4238 | + } |
|
| 4239 | + } else { |
|
| 4240 | + $field_obj = $this->_deduce_field_from_query_param($query_param); |
|
| 4241 | + // if it's not a normal field, maybe it's a custom selection? |
|
| 4242 | + if (! $field_obj) { |
|
| 4243 | + if ($this->_custom_selections instanceof CustomSelects) { |
|
| 4244 | + $field_obj = $this->_custom_selections->getDataTypeForAlias($query_param); |
|
| 4245 | + } else { |
|
| 4246 | + throw new EE_Error(sprintf(__( |
|
| 4247 | + "%s is neither a valid model field name, nor a custom selection", |
|
| 4248 | + "event_espresso" |
|
| 4249 | + ), $query_param)); |
|
| 4250 | + } |
|
| 4251 | + } |
|
| 4252 | + $op_and_value_sql = $this->_construct_op_and_value($op_and_value_or_sub_condition, $field_obj); |
|
| 4253 | + $where_clauses[] = $this->_deduce_column_name_from_query_param($query_param) . SP . $op_and_value_sql; |
|
| 4254 | + } |
|
| 4255 | + } |
|
| 4256 | + return $where_clauses ? implode($glue, $where_clauses) : ''; |
|
| 4257 | + } |
|
| 4258 | + |
|
| 4259 | + |
|
| 4260 | + |
|
| 4261 | + /** |
|
| 4262 | + * Takes the input parameter and extract the table name (alias) and column name |
|
| 4263 | + * |
|
| 4264 | + * @param string $query_param like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
| 4265 | + * @throws EE_Error |
|
| 4266 | + * @return string table alias and column name for SQL, eg "Transaction.TXN_ID" |
|
| 4267 | + */ |
|
| 4268 | + private function _deduce_column_name_from_query_param($query_param) |
|
| 4269 | + { |
|
| 4270 | + $field = $this->_deduce_field_from_query_param($query_param); |
|
| 4271 | + if ($field) { |
|
| 4272 | + $table_alias_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_from_query_param( |
|
| 4273 | + $field->get_model_name(), |
|
| 4274 | + $query_param |
|
| 4275 | + ); |
|
| 4276 | + return $table_alias_prefix . $field->get_qualified_column(); |
|
| 4277 | + } |
|
| 4278 | + if ($this->_custom_selections instanceof CustomSelects |
|
| 4279 | + && in_array($query_param, $this->_custom_selections->columnAliases(), true) |
|
| 4280 | + ) { |
|
| 4281 | + // maybe it's custom selection item? |
|
| 4282 | + // if so, just use it as the "column name" |
|
| 4283 | + return $query_param; |
|
| 4284 | + } |
|
| 4285 | + $custom_select_aliases = $this->_custom_selections instanceof CustomSelects |
|
| 4286 | + ? implode(',', $this->_custom_selections->columnAliases()) |
|
| 4287 | + : ''; |
|
| 4288 | + throw new EE_Error( |
|
| 4289 | + sprintf( |
|
| 4290 | + __( |
|
| 4291 | + "%s is not a valid field on this model, nor a custom selection (%s)", |
|
| 4292 | + "event_espresso" |
|
| 4293 | + ), |
|
| 4294 | + $query_param, |
|
| 4295 | + $custom_select_aliases |
|
| 4296 | + ) |
|
| 4297 | + ); |
|
| 4298 | + } |
|
| 4299 | + |
|
| 4300 | + |
|
| 4301 | + |
|
| 4302 | + /** |
|
| 4303 | + * Removes the * and anything after it from the condition query param key. It is useful to add the * to condition |
|
| 4304 | + * query param keys (eg, 'OR*', 'EVT_ID') in order for the array keys to still be unique, so that they don't get |
|
| 4305 | + * overwritten Takes a string like 'Event.EVT_ID*', 'TXN_total**', 'OR*1st', and 'DTT_reg_start*foobar' to |
|
| 4306 | + * 'Event.EVT_ID', 'TXN_total', 'OR', and 'DTT_reg_start', respectively. |
|
| 4307 | + * |
|
| 4308 | + * @param string $condition_query_param_key |
|
| 4309 | + * @return string |
|
| 4310 | + */ |
|
| 4311 | + private function _remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
| 4312 | + { |
|
| 4313 | + $pos_of_star = strpos($condition_query_param_key, '*'); |
|
| 4314 | + if ($pos_of_star === false) { |
|
| 4315 | + return $condition_query_param_key; |
|
| 4316 | + } |
|
| 4317 | + $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
| 4318 | + return $condition_query_param_sans_star; |
|
| 4319 | + } |
|
| 4320 | + |
|
| 4321 | + |
|
| 4322 | + |
|
| 4323 | + /** |
|
| 4324 | + * creates the SQL for the operator and the value in a WHERE clause, eg "< 23" or "LIKE '%monkey%'" |
|
| 4325 | + * |
|
| 4326 | + * @param mixed array | string $op_and_value |
|
| 4327 | + * @param EE_Model_Field_Base|string $field_obj . If string, should be one of EEM_Base::_valid_wpdb_data_types |
|
| 4328 | + * @throws EE_Error |
|
| 4329 | + * @return string |
|
| 4330 | + */ |
|
| 4331 | + private function _construct_op_and_value($op_and_value, $field_obj) |
|
| 4332 | + { |
|
| 4333 | + if (is_array($op_and_value)) { |
|
| 4334 | + $operator = isset($op_and_value[0]) ? $this->_prepare_operator_for_sql($op_and_value[0]) : null; |
|
| 4335 | + if (! $operator) { |
|
| 4336 | + $php_array_like_string = array(); |
|
| 4337 | + foreach ($op_and_value as $key => $value) { |
|
| 4338 | + $php_array_like_string[] = "$key=>$value"; |
|
| 4339 | + } |
|
| 4340 | + throw new EE_Error( |
|
| 4341 | + sprintf( |
|
| 4342 | + __( |
|
| 4343 | + "You setup a query parameter like you were going to specify an operator, but didn't. You provided '(%s)', but the operator should be at array key index 0 (eg array('>',32))", |
|
| 4344 | + "event_espresso" |
|
| 4345 | + ), |
|
| 4346 | + implode(",", $php_array_like_string) |
|
| 4347 | + ) |
|
| 4348 | + ); |
|
| 4349 | + } |
|
| 4350 | + $value = isset($op_and_value[1]) ? $op_and_value[1] : null; |
|
| 4351 | + } else { |
|
| 4352 | + $operator = '='; |
|
| 4353 | + $value = $op_and_value; |
|
| 4354 | + } |
|
| 4355 | + // check to see if the value is actually another field |
|
| 4356 | + if (is_array($op_and_value) && isset($op_and_value[2]) && $op_and_value[2] == true) { |
|
| 4357 | + return $operator . SP . $this->_deduce_column_name_from_query_param($value); |
|
| 4358 | + } |
|
| 4359 | + if (in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
| 4360 | + // in this case, the value should be an array, or at least a comma-separated list |
|
| 4361 | + // it will need to handle a little differently |
|
| 4362 | + $cleaned_value = $this->_construct_in_value($value, $field_obj); |
|
| 4363 | + // note: $cleaned_value has already been run through $wpdb->prepare() |
|
| 4364 | + return $operator . SP . $cleaned_value; |
|
| 4365 | + } |
|
| 4366 | + if (in_array($operator, $this->valid_between_style_operators()) && is_array($value)) { |
|
| 4367 | + // the value should be an array with count of two. |
|
| 4368 | + if (count($value) !== 2) { |
|
| 4369 | + throw new EE_Error( |
|
| 4370 | + sprintf( |
|
| 4371 | + __( |
|
| 4372 | + "The '%s' operator must be used with an array of values and there must be exactly TWO values in that array.", |
|
| 4373 | + 'event_espresso' |
|
| 4374 | + ), |
|
| 4375 | + "BETWEEN" |
|
| 4376 | + ) |
|
| 4377 | + ); |
|
| 4378 | + } |
|
| 4379 | + $cleaned_value = $this->_construct_between_value($value, $field_obj); |
|
| 4380 | + return $operator . SP . $cleaned_value; |
|
| 4381 | + } |
|
| 4382 | + if (in_array($operator, $this->valid_null_style_operators())) { |
|
| 4383 | + if ($value !== null) { |
|
| 4384 | + throw new EE_Error( |
|
| 4385 | + sprintf( |
|
| 4386 | + __( |
|
| 4387 | + "You attempted to give a value (%s) while using a NULL-style operator (%s). That isn't valid", |
|
| 4388 | + "event_espresso" |
|
| 4389 | + ), |
|
| 4390 | + $value, |
|
| 4391 | + $operator |
|
| 4392 | + ) |
|
| 4393 | + ); |
|
| 4394 | + } |
|
| 4395 | + return $operator; |
|
| 4396 | + } |
|
| 4397 | + if (in_array($operator, $this->valid_like_style_operators()) && ! is_array($value)) { |
|
| 4398 | + // if the operator is 'LIKE', we want to allow percent signs (%) and not |
|
| 4399 | + // remove other junk. So just treat it as a string. |
|
| 4400 | + return $operator . SP . $this->_wpdb_prepare_using_field($value, '%s'); |
|
| 4401 | + } |
|
| 4402 | + if (! in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
| 4403 | + return $operator . SP . $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4404 | + } |
|
| 4405 | + if (in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
| 4406 | + throw new EE_Error( |
|
| 4407 | + sprintf( |
|
| 4408 | + __( |
|
| 4409 | + "Operator '%s' must be used with an array of values, eg 'Registration.REG_ID' => array('%s',array(1,2,3))", |
|
| 4410 | + 'event_espresso' |
|
| 4411 | + ), |
|
| 4412 | + $operator, |
|
| 4413 | + $operator |
|
| 4414 | + ) |
|
| 4415 | + ); |
|
| 4416 | + } |
|
| 4417 | + if (! in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
| 4418 | + throw new EE_Error( |
|
| 4419 | + sprintf( |
|
| 4420 | + __( |
|
| 4421 | + "Operator '%s' must be used with a single value, not an array. Eg 'Registration.REG_ID => array('%s',23))", |
|
| 4422 | + 'event_espresso' |
|
| 4423 | + ), |
|
| 4424 | + $operator, |
|
| 4425 | + $operator |
|
| 4426 | + ) |
|
| 4427 | + ); |
|
| 4428 | + } |
|
| 4429 | + throw new EE_Error( |
|
| 4430 | + sprintf( |
|
| 4431 | + __( |
|
| 4432 | + "It appears you've provided some totally invalid query parameters. Operator and value were:'%s', which isn't right at all", |
|
| 4433 | + "event_espresso" |
|
| 4434 | + ), |
|
| 4435 | + http_build_query($op_and_value) |
|
| 4436 | + ) |
|
| 4437 | + ); |
|
| 4438 | + } |
|
| 4439 | + |
|
| 4440 | + |
|
| 4441 | + |
|
| 4442 | + /** |
|
| 4443 | + * Creates the operands to be used in a BETWEEN query, eg "'2014-12-31 20:23:33' AND '2015-01-23 12:32:54'" |
|
| 4444 | + * |
|
| 4445 | + * @param array $values |
|
| 4446 | + * @param EE_Model_Field_Base|string $field_obj if string, it should be the datatype to be used when querying, eg |
|
| 4447 | + * '%s' |
|
| 4448 | + * @return string |
|
| 4449 | + * @throws EE_Error |
|
| 4450 | + */ |
|
| 4451 | + public function _construct_between_value($values, $field_obj) |
|
| 4452 | + { |
|
| 4453 | + $cleaned_values = array(); |
|
| 4454 | + foreach ($values as $value) { |
|
| 4455 | + $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4456 | + } |
|
| 4457 | + return $cleaned_values[0] . " AND " . $cleaned_values[1]; |
|
| 4458 | + } |
|
| 4459 | + |
|
| 4460 | + |
|
| 4461 | + |
|
| 4462 | + /** |
|
| 4463 | + * Takes an array or a comma-separated list of $values and cleans them |
|
| 4464 | + * according to $data_type using $wpdb->prepare, and then makes the list a |
|
| 4465 | + * string surrounded by ( and ). Eg, _construct_in_value(array(1,2,3),'%d') would |
|
| 4466 | + * return '(1,2,3)'; _construct_in_value("1,2,hack",'%d') would return '(1,2,1)' (assuming |
|
| 4467 | + * I'm right that a string, when interpreted as a digit, becomes a 1. It might become a 0) |
|
| 4468 | + * |
|
| 4469 | + * @param mixed $values array or comma-separated string |
|
| 4470 | + * @param EE_Model_Field_Base|string $field_obj if string, it should be a wpdb data type like '%s', or '%d' |
|
| 4471 | + * @return string of SQL to follow an 'IN' or 'NOT IN' operator |
|
| 4472 | + * @throws EE_Error |
|
| 4473 | + */ |
|
| 4474 | + public function _construct_in_value($values, $field_obj) |
|
| 4475 | + { |
|
| 4476 | + // check if the value is a CSV list |
|
| 4477 | + if (is_string($values)) { |
|
| 4478 | + // in which case, turn it into an array |
|
| 4479 | + $values = explode(",", $values); |
|
| 4480 | + } |
|
| 4481 | + $cleaned_values = array(); |
|
| 4482 | + foreach ($values as $value) { |
|
| 4483 | + $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4484 | + } |
|
| 4485 | + // we would just LOVE to leave $cleaned_values as an empty array, and return the value as "()", |
|
| 4486 | + // but unfortunately that's invalid SQL. So instead we return a string which we KNOW will evaluate to be the empty set |
|
| 4487 | + // which is effectively equivalent to returning "()". We don't return "(0)" because that only works for auto-incrementing columns |
|
| 4488 | + if (empty($cleaned_values)) { |
|
| 4489 | + $all_fields = $this->field_settings(); |
|
| 4490 | + $a_field = array_shift($all_fields); |
|
| 4491 | + $main_table = $this->_get_main_table(); |
|
| 4492 | + $cleaned_values[] = "SELECT " |
|
| 4493 | + . $a_field->get_table_column() |
|
| 4494 | + . " FROM " |
|
| 4495 | + . $main_table->get_table_name() |
|
| 4496 | + . " WHERE FALSE"; |
|
| 4497 | + } |
|
| 4498 | + return "(" . implode(",", $cleaned_values) . ")"; |
|
| 4499 | + } |
|
| 4500 | + |
|
| 4501 | + |
|
| 4502 | + |
|
| 4503 | + /** |
|
| 4504 | + * @param mixed $value |
|
| 4505 | + * @param EE_Model_Field_Base|string $field_obj if string it should be a wpdb data type like '%d' |
|
| 4506 | + * @throws EE_Error |
|
| 4507 | + * @return false|null|string |
|
| 4508 | + */ |
|
| 4509 | + private function _wpdb_prepare_using_field($value, $field_obj) |
|
| 4510 | + { |
|
| 4511 | + /** @type WPDB $wpdb */ |
|
| 4512 | + global $wpdb; |
|
| 4513 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 4514 | + return $wpdb->prepare( |
|
| 4515 | + $field_obj->get_wpdb_data_type(), |
|
| 4516 | + $this->_prepare_value_for_use_in_db($value, $field_obj) |
|
| 4517 | + ); |
|
| 4518 | + } //$field_obj should really just be a data type |
|
| 4519 | + if (! in_array($field_obj, $this->_valid_wpdb_data_types)) { |
|
| 4520 | + throw new EE_Error( |
|
| 4521 | + sprintf( |
|
| 4522 | + __("%s is not a valid wpdb datatype. Valid ones are %s", "event_espresso"), |
|
| 4523 | + $field_obj, |
|
| 4524 | + implode(",", $this->_valid_wpdb_data_types) |
|
| 4525 | + ) |
|
| 4526 | + ); |
|
| 4527 | + } |
|
| 4528 | + return $wpdb->prepare($field_obj, $value); |
|
| 4529 | + } |
|
| 4530 | + |
|
| 4531 | + |
|
| 4532 | + |
|
| 4533 | + /** |
|
| 4534 | + * Takes the input parameter and finds the model field that it indicates. |
|
| 4535 | + * |
|
| 4536 | + * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
| 4537 | + * @throws EE_Error |
|
| 4538 | + * @return EE_Model_Field_Base |
|
| 4539 | + */ |
|
| 4540 | + protected function _deduce_field_from_query_param($query_param_name) |
|
| 4541 | + { |
|
| 4542 | + // ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
| 4543 | + // which will help us find the database table and column |
|
| 4544 | + $query_param_parts = explode(".", $query_param_name); |
|
| 4545 | + if (empty($query_param_parts)) { |
|
| 4546 | + throw new EE_Error(sprintf(__( |
|
| 4547 | + "_extract_column_name is empty when trying to extract column and table name from %s", |
|
| 4548 | + 'event_espresso' |
|
| 4549 | + ), $query_param_name)); |
|
| 4550 | + } |
|
| 4551 | + $number_of_parts = count($query_param_parts); |
|
| 4552 | + $last_query_param_part = $query_param_parts[ count($query_param_parts) - 1 ]; |
|
| 4553 | + if ($number_of_parts === 1) { |
|
| 4554 | + $field_name = $last_query_param_part; |
|
| 4555 | + $model_obj = $this; |
|
| 4556 | + } else {// $number_of_parts >= 2 |
|
| 4557 | + // the last part is the column name, and there are only 2parts. therefore... |
|
| 4558 | + $field_name = $last_query_param_part; |
|
| 4559 | + $model_obj = $this->get_related_model_obj($query_param_parts[ $number_of_parts - 2 ]); |
|
| 4560 | + } |
|
| 4561 | + try { |
|
| 4562 | + return $model_obj->field_settings_for($field_name); |
|
| 4563 | + } catch (EE_Error $e) { |
|
| 4564 | + return null; |
|
| 4565 | + } |
|
| 4566 | + } |
|
| 4567 | + |
|
| 4568 | + |
|
| 4569 | + |
|
| 4570 | + /** |
|
| 4571 | + * Given a field's name (ie, a key in $this->field_settings()), uses the EE_Model_Field object to get the table's |
|
| 4572 | + * alias and column which corresponds to it |
|
| 4573 | + * |
|
| 4574 | + * @param string $field_name |
|
| 4575 | + * @throws EE_Error |
|
| 4576 | + * @return string |
|
| 4577 | + */ |
|
| 4578 | + public function _get_qualified_column_for_field($field_name) |
|
| 4579 | + { |
|
| 4580 | + $all_fields = $this->field_settings(); |
|
| 4581 | + $field = isset($all_fields[ $field_name ]) ? $all_fields[ $field_name ] : false; |
|
| 4582 | + if ($field) { |
|
| 4583 | + return $field->get_qualified_column(); |
|
| 4584 | + } |
|
| 4585 | + throw new EE_Error( |
|
| 4586 | + sprintf( |
|
| 4587 | + __( |
|
| 4588 | + "There is no field titled %s on model %s. Either the query trying to use it is bad, or you need to add it to the list of fields on the model.", |
|
| 4589 | + 'event_espresso' |
|
| 4590 | + ), |
|
| 4591 | + $field_name, |
|
| 4592 | + get_class($this) |
|
| 4593 | + ) |
|
| 4594 | + ); |
|
| 4595 | + } |
|
| 4596 | + |
|
| 4597 | + |
|
| 4598 | + |
|
| 4599 | + /** |
|
| 4600 | + * similar to \EEM_Base::_get_qualified_column_for_field() but returns an array with data for ALL fields. |
|
| 4601 | + * Example usage: |
|
| 4602 | + * EEM_Ticket::instance()->get_all_wpdb_results( |
|
| 4603 | + * array(), |
|
| 4604 | + * ARRAY_A, |
|
| 4605 | + * EEM_Ticket::instance()->get_qualified_columns_for_all_fields() |
|
| 4606 | + * ); |
|
| 4607 | + * is equivalent to |
|
| 4608 | + * EEM_Ticket::instance()->get_all_wpdb_results( array(), ARRAY_A, '*' ); |
|
| 4609 | + * and |
|
| 4610 | + * EEM_Event::instance()->get_all_wpdb_results( |
|
| 4611 | + * array( |
|
| 4612 | + * array( |
|
| 4613 | + * 'Datetime.Ticket.TKT_ID' => array( '<', 100 ), |
|
| 4614 | + * ), |
|
| 4615 | + * ARRAY_A, |
|
| 4616 | + * implode( |
|
| 4617 | + * ', ', |
|
| 4618 | + * array_merge( |
|
| 4619 | + * EEM_Event::instance()->get_qualified_columns_for_all_fields( '', false ), |
|
| 4620 | + * EEM_Ticket::instance()->get_qualified_columns_for_all_fields( 'Datetime', false ) |
|
| 4621 | + * ) |
|
| 4622 | + * ) |
|
| 4623 | + * ) |
|
| 4624 | + * ); |
|
| 4625 | + * selects rows from the database, selecting all the event and ticket columns, where the ticket ID is below 100 |
|
| 4626 | + * |
|
| 4627 | + * @param string $model_relation_chain the chain of models used to join between the model you want to query |
|
| 4628 | + * and the one whose fields you are selecting for example: when querying |
|
| 4629 | + * tickets model and selecting fields from the tickets model you would |
|
| 4630 | + * leave this parameter empty, because no models are needed to join |
|
| 4631 | + * between the queried model and the selected one. Likewise when |
|
| 4632 | + * querying the datetime model and selecting fields from the tickets |
|
| 4633 | + * model, it would also be left empty, because there is a direct |
|
| 4634 | + * relation from datetimes to tickets, so no model is needed to join |
|
| 4635 | + * them together. However, when querying from the event model and |
|
| 4636 | + * selecting fields from the ticket model, you should provide the string |
|
| 4637 | + * 'Datetime', indicating that the event model must first join to the |
|
| 4638 | + * datetime model in order to find its relation to ticket model. |
|
| 4639 | + * Also, when querying from the venue model and selecting fields from |
|
| 4640 | + * the ticket model, you should provide the string 'Event.Datetime', |
|
| 4641 | + * indicating you need to join the venue model to the event model, |
|
| 4642 | + * to the datetime model, in order to find its relation to the ticket model. |
|
| 4643 | + * This string is used to deduce the prefix that gets added onto the |
|
| 4644 | + * models' tables qualified columns |
|
| 4645 | + * @param bool $return_string if true, will return a string with qualified column names separated |
|
| 4646 | + * by ', ' if false, will simply return a numerically indexed array of |
|
| 4647 | + * qualified column names |
|
| 4648 | + * @return array|string |
|
| 4649 | + */ |
|
| 4650 | + public function get_qualified_columns_for_all_fields($model_relation_chain = '', $return_string = true) |
|
| 4651 | + { |
|
| 4652 | + $table_prefix = str_replace('.', '__', $model_relation_chain) . (empty($model_relation_chain) ? '' : '__'); |
|
| 4653 | + $qualified_columns = array(); |
|
| 4654 | + foreach ($this->field_settings() as $field_name => $field) { |
|
| 4655 | + $qualified_columns[] = $table_prefix . $field->get_qualified_column(); |
|
| 4656 | + } |
|
| 4657 | + return $return_string ? implode(', ', $qualified_columns) : $qualified_columns; |
|
| 4658 | + } |
|
| 4659 | + |
|
| 4660 | + |
|
| 4661 | + |
|
| 4662 | + /** |
|
| 4663 | + * constructs the select use on special limit joins |
|
| 4664 | + * NOTE: for now this has only been tested and will work when the table alias is for the PRIMARY table. Although |
|
| 4665 | + * its setup so the select query will be setup on and just doing the special select join off of the primary table |
|
| 4666 | + * (as that is typically where the limits would be set). |
|
| 4667 | + * |
|
| 4668 | + * @param string $table_alias The table the select is being built for |
|
| 4669 | + * @param mixed|string $limit The limit for this select |
|
| 4670 | + * @return string The final select join element for the query. |
|
| 4671 | + */ |
|
| 4672 | + public function _construct_limit_join_select($table_alias, $limit) |
|
| 4673 | + { |
|
| 4674 | + $SQL = ''; |
|
| 4675 | + foreach ($this->_tables as $table_obj) { |
|
| 4676 | + if ($table_obj instanceof EE_Primary_Table) { |
|
| 4677 | + $SQL .= $table_alias === $table_obj->get_table_alias() |
|
| 4678 | + ? $table_obj->get_select_join_limit($limit) |
|
| 4679 | + : SP . $table_obj->get_table_name() . " AS " . $table_obj->get_table_alias() . SP; |
|
| 4680 | + } elseif ($table_obj instanceof EE_Secondary_Table) { |
|
| 4681 | + $SQL .= $table_alias === $table_obj->get_table_alias() |
|
| 4682 | + ? $table_obj->get_select_join_limit_join($limit) |
|
| 4683 | + : SP . $table_obj->get_join_sql($table_alias) . SP; |
|
| 4684 | + } |
|
| 4685 | + } |
|
| 4686 | + return $SQL; |
|
| 4687 | + } |
|
| 4688 | + |
|
| 4689 | + |
|
| 4690 | + |
|
| 4691 | + /** |
|
| 4692 | + * Constructs the internal join if there are multiple tables, or simply the table's name and alias |
|
| 4693 | + * Eg "wp_post AS Event" or "wp_post AS Event INNER JOIN wp_postmeta Event_Meta ON Event.ID = Event_Meta.post_id" |
|
| 4694 | + * |
|
| 4695 | + * @return string SQL |
|
| 4696 | + * @throws EE_Error |
|
| 4697 | + */ |
|
| 4698 | + public function _construct_internal_join() |
|
| 4699 | + { |
|
| 4700 | + $SQL = $this->_get_main_table()->get_table_sql(); |
|
| 4701 | + $SQL .= $this->_construct_internal_join_to_table_with_alias($this->_get_main_table()->get_table_alias()); |
|
| 4702 | + return $SQL; |
|
| 4703 | + } |
|
| 4704 | + |
|
| 4705 | + |
|
| 4706 | + |
|
| 4707 | + /** |
|
| 4708 | + * Constructs the SQL for joining all the tables on this model. |
|
| 4709 | + * Normally $alias should be the primary table's alias, but in cases where |
|
| 4710 | + * we have already joined to a secondary table (eg, the secondary table has a foreign key and is joined before the |
|
| 4711 | + * primary table) then we should provide that secondary table's alias. Eg, with $alias being the primary table's |
|
| 4712 | + * alias, this will construct SQL like: |
|
| 4713 | + * " INNER JOIN wp_esp_secondary_table AS Secondary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
| 4714 | + * With $alias being a secondary table's alias, this will construct SQL like: |
|
| 4715 | + * " INNER JOIN wp_esp_primary_table AS Primary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
| 4716 | + * |
|
| 4717 | + * @param string $alias_prefixed table alias to join to (this table should already be in the FROM SQL clause) |
|
| 4718 | + * @return string |
|
| 4719 | + */ |
|
| 4720 | + public function _construct_internal_join_to_table_with_alias($alias_prefixed) |
|
| 4721 | + { |
|
| 4722 | + $SQL = ''; |
|
| 4723 | + $alias_sans_prefix = EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($alias_prefixed); |
|
| 4724 | + foreach ($this->_tables as $table_obj) { |
|
| 4725 | + if ($table_obj instanceof EE_Secondary_Table) {// table is secondary table |
|
| 4726 | + if ($alias_sans_prefix === $table_obj->get_table_alias()) { |
|
| 4727 | + // so we're joining to this table, meaning the table is already in |
|
| 4728 | + // the FROM statement, BUT the primary table isn't. So we want |
|
| 4729 | + // to add the inverse join sql |
|
| 4730 | + $SQL .= $table_obj->get_inverse_join_sql($alias_prefixed); |
|
| 4731 | + } else { |
|
| 4732 | + // just add a regular JOIN to this table from the primary table |
|
| 4733 | + $SQL .= $table_obj->get_join_sql($alias_prefixed); |
|
| 4734 | + } |
|
| 4735 | + }//if it's a primary table, dont add any SQL. it should already be in the FROM statement |
|
| 4736 | + } |
|
| 4737 | + return $SQL; |
|
| 4738 | + } |
|
| 4739 | + |
|
| 4740 | + |
|
| 4741 | + |
|
| 4742 | + /** |
|
| 4743 | + * Gets an array for storing all the data types on the next-to-be-executed-query. |
|
| 4744 | + * This should be a growing array of keys being table-columns (eg 'EVT_ID' and 'Event.EVT_ID'), and values being |
|
| 4745 | + * their data type (eg, '%s', '%d', etc) |
|
| 4746 | + * |
|
| 4747 | + * @return array |
|
| 4748 | + */ |
|
| 4749 | + public function _get_data_types() |
|
| 4750 | + { |
|
| 4751 | + $data_types = array(); |
|
| 4752 | + foreach ($this->field_settings() as $field_obj) { |
|
| 4753 | + // $data_types[$field_obj->get_table_column()] = $field_obj->get_wpdb_data_type(); |
|
| 4754 | + /** @var $field_obj EE_Model_Field_Base */ |
|
| 4755 | + $data_types[ $field_obj->get_qualified_column() ] = $field_obj->get_wpdb_data_type(); |
|
| 4756 | + } |
|
| 4757 | + return $data_types; |
|
| 4758 | + } |
|
| 4759 | + |
|
| 4760 | + |
|
| 4761 | + |
|
| 4762 | + /** |
|
| 4763 | + * Gets the model object given the relation's name / model's name (eg, 'Event', 'Registration',etc. Always singular) |
|
| 4764 | + * |
|
| 4765 | + * @param string $model_name |
|
| 4766 | + * @throws EE_Error |
|
| 4767 | + * @return EEM_Base |
|
| 4768 | + */ |
|
| 4769 | + public function get_related_model_obj($model_name) |
|
| 4770 | + { |
|
| 4771 | + $model_classname = "EEM_" . $model_name; |
|
| 4772 | + if (! class_exists($model_classname)) { |
|
| 4773 | + throw new EE_Error(sprintf(__( |
|
| 4774 | + "You specified a related model named %s in your query. No such model exists, if it did, it would have the classname %s", |
|
| 4775 | + 'event_espresso' |
|
| 4776 | + ), $model_name, $model_classname)); |
|
| 4777 | + } |
|
| 4778 | + return call_user_func($model_classname . "::instance"); |
|
| 4779 | + } |
|
| 4780 | + |
|
| 4781 | + |
|
| 4782 | + |
|
| 4783 | + /** |
|
| 4784 | + * Returns the array of EE_ModelRelations for this model. |
|
| 4785 | + * |
|
| 4786 | + * @return EE_Model_Relation_Base[] |
|
| 4787 | + */ |
|
| 4788 | + public function relation_settings() |
|
| 4789 | + { |
|
| 4790 | + return $this->_model_relations; |
|
| 4791 | + } |
|
| 4792 | + |
|
| 4793 | + |
|
| 4794 | + |
|
| 4795 | + /** |
|
| 4796 | + * Gets all related models that this model BELONGS TO. Handy to know sometimes |
|
| 4797 | + * because without THOSE models, this model probably doesn't have much purpose. |
|
| 4798 | + * (Eg, without an event, datetimes have little purpose.) |
|
| 4799 | + * |
|
| 4800 | + * @return EE_Belongs_To_Relation[] |
|
| 4801 | + */ |
|
| 4802 | + public function belongs_to_relations() |
|
| 4803 | + { |
|
| 4804 | + $belongs_to_relations = array(); |
|
| 4805 | + foreach ($this->relation_settings() as $model_name => $relation_obj) { |
|
| 4806 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 4807 | + $belongs_to_relations[ $model_name ] = $relation_obj; |
|
| 4808 | + } |
|
| 4809 | + } |
|
| 4810 | + return $belongs_to_relations; |
|
| 4811 | + } |
|
| 4812 | + |
|
| 4813 | + |
|
| 4814 | + |
|
| 4815 | + /** |
|
| 4816 | + * Returns the specified EE_Model_Relation, or throws an exception |
|
| 4817 | + * |
|
| 4818 | + * @param string $relation_name name of relation, key in $this->_relatedModels |
|
| 4819 | + * @throws EE_Error |
|
| 4820 | + * @return EE_Model_Relation_Base |
|
| 4821 | + */ |
|
| 4822 | + public function related_settings_for($relation_name) |
|
| 4823 | + { |
|
| 4824 | + $relatedModels = $this->relation_settings(); |
|
| 4825 | + if (! array_key_exists($relation_name, $relatedModels)) { |
|
| 4826 | + throw new EE_Error( |
|
| 4827 | + sprintf( |
|
| 4828 | + __( |
|
| 4829 | + 'Cannot get %s related to %s. There is no model relation of that type. There is, however, %s...', |
|
| 4830 | + 'event_espresso' |
|
| 4831 | + ), |
|
| 4832 | + $relation_name, |
|
| 4833 | + $this->_get_class_name(), |
|
| 4834 | + implode(', ', array_keys($relatedModels)) |
|
| 4835 | + ) |
|
| 4836 | + ); |
|
| 4837 | + } |
|
| 4838 | + return $relatedModels[ $relation_name ]; |
|
| 4839 | + } |
|
| 4840 | + |
|
| 4841 | + |
|
| 4842 | + |
|
| 4843 | + /** |
|
| 4844 | + * A convenience method for getting a specific field's settings, instead of getting all field settings for all |
|
| 4845 | + * fields |
|
| 4846 | + * |
|
| 4847 | + * @param string $fieldName |
|
| 4848 | + * @param boolean $include_db_only_fields |
|
| 4849 | + * @throws EE_Error |
|
| 4850 | + * @return EE_Model_Field_Base |
|
| 4851 | + */ |
|
| 4852 | + public function field_settings_for($fieldName, $include_db_only_fields = true) |
|
| 4853 | + { |
|
| 4854 | + $fieldSettings = $this->field_settings($include_db_only_fields); |
|
| 4855 | + if (! array_key_exists($fieldName, $fieldSettings)) { |
|
| 4856 | + throw new EE_Error(sprintf( |
|
| 4857 | + __("There is no field/column '%s' on '%s'", 'event_espresso'), |
|
| 4858 | + $fieldName, |
|
| 4859 | + get_class($this) |
|
| 4860 | + )); |
|
| 4861 | + } |
|
| 4862 | + return $fieldSettings[ $fieldName ]; |
|
| 4863 | + } |
|
| 4864 | + |
|
| 4865 | + |
|
| 4866 | + |
|
| 4867 | + /** |
|
| 4868 | + * Checks if this field exists on this model |
|
| 4869 | + * |
|
| 4870 | + * @param string $fieldName a key in the model's _field_settings array |
|
| 4871 | + * @return boolean |
|
| 4872 | + */ |
|
| 4873 | + public function has_field($fieldName) |
|
| 4874 | + { |
|
| 4875 | + $fieldSettings = $this->field_settings(true); |
|
| 4876 | + if (isset($fieldSettings[ $fieldName ])) { |
|
| 4877 | + return true; |
|
| 4878 | + } |
|
| 4879 | + return false; |
|
| 4880 | + } |
|
| 4881 | + |
|
| 4882 | + |
|
| 4883 | + |
|
| 4884 | + /** |
|
| 4885 | + * Returns whether or not this model has a relation to the specified model |
|
| 4886 | + * |
|
| 4887 | + * @param string $relation_name possibly one of the keys in the relation_settings array |
|
| 4888 | + * @return boolean |
|
| 4889 | + */ |
|
| 4890 | + public function has_relation($relation_name) |
|
| 4891 | + { |
|
| 4892 | + $relations = $this->relation_settings(); |
|
| 4893 | + if (isset($relations[ $relation_name ])) { |
|
| 4894 | + return true; |
|
| 4895 | + } |
|
| 4896 | + return false; |
|
| 4897 | + } |
|
| 4898 | + |
|
| 4899 | + |
|
| 4900 | + |
|
| 4901 | + /** |
|
| 4902 | + * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
| 4903 | + * Eg, on EE_Answer that would be ANS_ID field object |
|
| 4904 | + * |
|
| 4905 | + * @param $field_obj |
|
| 4906 | + * @return boolean |
|
| 4907 | + */ |
|
| 4908 | + public function is_primary_key_field($field_obj) |
|
| 4909 | + { |
|
| 4910 | + return $field_obj instanceof EE_Primary_Key_Field_Base ? true : false; |
|
| 4911 | + } |
|
| 4912 | + |
|
| 4913 | + |
|
| 4914 | + |
|
| 4915 | + /** |
|
| 4916 | + * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
| 4917 | + * Eg, on EE_Answer that would be ANS_ID field object |
|
| 4918 | + * |
|
| 4919 | + * @return EE_Model_Field_Base |
|
| 4920 | + * @throws EE_Error |
|
| 4921 | + */ |
|
| 4922 | + public function get_primary_key_field() |
|
| 4923 | + { |
|
| 4924 | + if ($this->_primary_key_field === null) { |
|
| 4925 | + foreach ($this->field_settings(true) as $field_obj) { |
|
| 4926 | + if ($this->is_primary_key_field($field_obj)) { |
|
| 4927 | + $this->_primary_key_field = $field_obj; |
|
| 4928 | + break; |
|
| 4929 | + } |
|
| 4930 | + } |
|
| 4931 | + if (! $this->_primary_key_field instanceof EE_Primary_Key_Field_Base) { |
|
| 4932 | + throw new EE_Error(sprintf( |
|
| 4933 | + __("There is no Primary Key defined on model %s", 'event_espresso'), |
|
| 4934 | + get_class($this) |
|
| 4935 | + )); |
|
| 4936 | + } |
|
| 4937 | + } |
|
| 4938 | + return $this->_primary_key_field; |
|
| 4939 | + } |
|
| 4940 | + |
|
| 4941 | + |
|
| 4942 | + |
|
| 4943 | + /** |
|
| 4944 | + * Returns whether or not not there is a primary key on this model. |
|
| 4945 | + * Internally does some caching. |
|
| 4946 | + * |
|
| 4947 | + * @return boolean |
|
| 4948 | + */ |
|
| 4949 | + public function has_primary_key_field() |
|
| 4950 | + { |
|
| 4951 | + if ($this->_has_primary_key_field === null) { |
|
| 4952 | + try { |
|
| 4953 | + $this->get_primary_key_field(); |
|
| 4954 | + $this->_has_primary_key_field = true; |
|
| 4955 | + } catch (EE_Error $e) { |
|
| 4956 | + $this->_has_primary_key_field = false; |
|
| 4957 | + } |
|
| 4958 | + } |
|
| 4959 | + return $this->_has_primary_key_field; |
|
| 4960 | + } |
|
| 4961 | + |
|
| 4962 | + |
|
| 4963 | + |
|
| 4964 | + /** |
|
| 4965 | + * Finds the first field of type $field_class_name. |
|
| 4966 | + * |
|
| 4967 | + * @param string $field_class_name class name of field that you want to find. Eg, EE_Datetime_Field, |
|
| 4968 | + * EE_Foreign_Key_Field, etc |
|
| 4969 | + * @return EE_Model_Field_Base or null if none is found |
|
| 4970 | + */ |
|
| 4971 | + public function get_a_field_of_type($field_class_name) |
|
| 4972 | + { |
|
| 4973 | + foreach ($this->field_settings() as $field) { |
|
| 4974 | + if ($field instanceof $field_class_name) { |
|
| 4975 | + return $field; |
|
| 4976 | + } |
|
| 4977 | + } |
|
| 4978 | + return null; |
|
| 4979 | + } |
|
| 4980 | + |
|
| 4981 | + |
|
| 4982 | + |
|
| 4983 | + /** |
|
| 4984 | + * Gets a foreign key field pointing to model. |
|
| 4985 | + * |
|
| 4986 | + * @param string $model_name eg Event, Registration, not EEM_Event |
|
| 4987 | + * @return EE_Foreign_Key_Field_Base |
|
| 4988 | + * @throws EE_Error |
|
| 4989 | + */ |
|
| 4990 | + public function get_foreign_key_to($model_name) |
|
| 4991 | + { |
|
| 4992 | + if (! isset($this->_cache_foreign_key_to_fields[ $model_name ])) { |
|
| 4993 | + foreach ($this->field_settings() as $field) { |
|
| 4994 | + if ($field instanceof EE_Foreign_Key_Field_Base |
|
| 4995 | + && in_array($model_name, $field->get_model_names_pointed_to()) |
|
| 4996 | + ) { |
|
| 4997 | + $this->_cache_foreign_key_to_fields[ $model_name ] = $field; |
|
| 4998 | + break; |
|
| 4999 | + } |
|
| 5000 | + } |
|
| 5001 | + if (! isset($this->_cache_foreign_key_to_fields[ $model_name ])) { |
|
| 5002 | + throw new EE_Error(sprintf(__( |
|
| 5003 | + "There is no foreign key field pointing to model %s on model %s", |
|
| 5004 | + 'event_espresso' |
|
| 5005 | + ), $model_name, get_class($this))); |
|
| 5006 | + } |
|
| 5007 | + } |
|
| 5008 | + return $this->_cache_foreign_key_to_fields[ $model_name ]; |
|
| 5009 | + } |
|
| 5010 | + |
|
| 5011 | + |
|
| 5012 | + |
|
| 5013 | + /** |
|
| 5014 | + * Gets the table name (including $wpdb->prefix) for the table alias |
|
| 5015 | + * |
|
| 5016 | + * @param string $table_alias eg Event, Event_Meta, Registration, Transaction, but maybe |
|
| 5017 | + * a table alias with a model chain prefix, like 'Venue__Event_Venue___Event_Meta'. |
|
| 5018 | + * Either one works |
|
| 5019 | + * @return string |
|
| 5020 | + */ |
|
| 5021 | + public function get_table_for_alias($table_alias) |
|
| 5022 | + { |
|
| 5023 | + $table_alias_sans_model_relation_chain_prefix = EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($table_alias); |
|
| 5024 | + return $this->_tables[ $table_alias_sans_model_relation_chain_prefix ]->get_table_name(); |
|
| 5025 | + } |
|
| 5026 | + |
|
| 5027 | + |
|
| 5028 | + |
|
| 5029 | + /** |
|
| 5030 | + * Returns a flat array of all field son this model, instead of organizing them |
|
| 5031 | + * by table_alias as they are in the constructor. |
|
| 5032 | + * |
|
| 5033 | + * @param bool $include_db_only_fields flag indicating whether or not to include the db-only fields |
|
| 5034 | + * @return EE_Model_Field_Base[] where the keys are the field's name |
|
| 5035 | + */ |
|
| 5036 | + public function field_settings($include_db_only_fields = false) |
|
| 5037 | + { |
|
| 5038 | + if ($include_db_only_fields) { |
|
| 5039 | + if ($this->_cached_fields === null) { |
|
| 5040 | + $this->_cached_fields = array(); |
|
| 5041 | + foreach ($this->_fields as $fields_corresponding_to_table) { |
|
| 5042 | + foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
| 5043 | + $this->_cached_fields[ $field_name ] = $field_obj; |
|
| 5044 | + } |
|
| 5045 | + } |
|
| 5046 | + } |
|
| 5047 | + return $this->_cached_fields; |
|
| 5048 | + } |
|
| 5049 | + if ($this->_cached_fields_non_db_only === null) { |
|
| 5050 | + $this->_cached_fields_non_db_only = array(); |
|
| 5051 | + foreach ($this->_fields as $fields_corresponding_to_table) { |
|
| 5052 | + foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
| 5053 | + /** @var $field_obj EE_Model_Field_Base */ |
|
| 5054 | + if (! $field_obj->is_db_only_field()) { |
|
| 5055 | + $this->_cached_fields_non_db_only[ $field_name ] = $field_obj; |
|
| 5056 | + } |
|
| 5057 | + } |
|
| 5058 | + } |
|
| 5059 | + } |
|
| 5060 | + return $this->_cached_fields_non_db_only; |
|
| 5061 | + } |
|
| 5062 | + |
|
| 5063 | + |
|
| 5064 | + |
|
| 5065 | + /** |
|
| 5066 | + * cycle though array of attendees and create objects out of each item |
|
| 5067 | + * |
|
| 5068 | + * @access private |
|
| 5069 | + * @param array $rows of results of $wpdb->get_results($query,ARRAY_A) |
|
| 5070 | + * @return \EE_Base_Class[] array keys are primary keys (if there is a primary key on the model. if not, |
|
| 5071 | + * numerically indexed) |
|
| 5072 | + * @throws EE_Error |
|
| 5073 | + */ |
|
| 5074 | + protected function _create_objects($rows = array()) |
|
| 5075 | + { |
|
| 5076 | + $array_of_objects = array(); |
|
| 5077 | + if (empty($rows)) { |
|
| 5078 | + return array(); |
|
| 5079 | + } |
|
| 5080 | + $count_if_model_has_no_primary_key = 0; |
|
| 5081 | + $has_primary_key = $this->has_primary_key_field(); |
|
| 5082 | + $primary_key_field = $has_primary_key ? $this->get_primary_key_field() : null; |
|
| 5083 | + foreach ((array) $rows as $row) { |
|
| 5084 | + if (empty($row)) { |
|
| 5085 | + // wp did its weird thing where it returns an array like array(0=>null), which is totally not helpful... |
|
| 5086 | + return array(); |
|
| 5087 | + } |
|
| 5088 | + // check if we've already set this object in the results array, |
|
| 5089 | + // in which case there's no need to process it further (again) |
|
| 5090 | + if ($has_primary_key) { |
|
| 5091 | + $table_pk_value = $this->_get_column_value_with_table_alias_or_not( |
|
| 5092 | + $row, |
|
| 5093 | + $primary_key_field->get_qualified_column(), |
|
| 5094 | + $primary_key_field->get_table_column() |
|
| 5095 | + ); |
|
| 5096 | + if ($table_pk_value && isset($array_of_objects[ $table_pk_value ])) { |
|
| 5097 | + continue; |
|
| 5098 | + } |
|
| 5099 | + } |
|
| 5100 | + $classInstance = $this->instantiate_class_from_array_or_object($row); |
|
| 5101 | + if (! $classInstance) { |
|
| 5102 | + throw new EE_Error( |
|
| 5103 | + sprintf( |
|
| 5104 | + __('Could not create instance of class %s from row %s', 'event_espresso'), |
|
| 5105 | + $this->get_this_model_name(), |
|
| 5106 | + http_build_query($row) |
|
| 5107 | + ) |
|
| 5108 | + ); |
|
| 5109 | + } |
|
| 5110 | + // set the timezone on the instantiated objects |
|
| 5111 | + $classInstance->set_timezone($this->_timezone); |
|
| 5112 | + // make sure if there is any timezone setting present that we set the timezone for the object |
|
| 5113 | + $key = $has_primary_key ? $classInstance->ID() : $count_if_model_has_no_primary_key++; |
|
| 5114 | + $array_of_objects[ $key ] = $classInstance; |
|
| 5115 | + // also, for all the relations of type BelongsTo, see if we can cache |
|
| 5116 | + // those related models |
|
| 5117 | + // (we could do this for other relations too, but if there are conditions |
|
| 5118 | + // that filtered out some fo the results, then we'd be caching an incomplete set |
|
| 5119 | + // so it requires a little more thought than just caching them immediately...) |
|
| 5120 | + foreach ($this->_model_relations as $modelName => $relation_obj) { |
|
| 5121 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 5122 | + // check if this model's INFO is present. If so, cache it on the model |
|
| 5123 | + $other_model = $relation_obj->get_other_model(); |
|
| 5124 | + $other_model_obj_maybe = $other_model->instantiate_class_from_array_or_object($row); |
|
| 5125 | + // if we managed to make a model object from the results, cache it on the main model object |
|
| 5126 | + if ($other_model_obj_maybe) { |
|
| 5127 | + // set timezone on these other model objects if they are present |
|
| 5128 | + $other_model_obj_maybe->set_timezone($this->_timezone); |
|
| 5129 | + $classInstance->cache($modelName, $other_model_obj_maybe); |
|
| 5130 | + } |
|
| 5131 | + } |
|
| 5132 | + } |
|
| 5133 | + // also, if this was a custom select query, let's see if there are any results for the custom select fields |
|
| 5134 | + // and add them to the object as well. We'll convert according to the set data_type if there's any set for |
|
| 5135 | + // the field in the CustomSelects object |
|
| 5136 | + if ($this->_custom_selections instanceof CustomSelects) { |
|
| 5137 | + $classInstance->setCustomSelectsValues( |
|
| 5138 | + $this->getValuesForCustomSelectAliasesFromResults($row) |
|
| 5139 | + ); |
|
| 5140 | + } |
|
| 5141 | + } |
|
| 5142 | + return $array_of_objects; |
|
| 5143 | + } |
|
| 5144 | + |
|
| 5145 | + |
|
| 5146 | + /** |
|
| 5147 | + * This will parse a given row of results from the db and see if any keys in the results match an alias within the |
|
| 5148 | + * current CustomSelects object. This will be used to build an array of values indexed by those keys. |
|
| 5149 | + * |
|
| 5150 | + * @param array $db_results_row |
|
| 5151 | + * @return array |
|
| 5152 | + */ |
|
| 5153 | + protected function getValuesForCustomSelectAliasesFromResults(array $db_results_row) |
|
| 5154 | + { |
|
| 5155 | + $results = array(); |
|
| 5156 | + if ($this->_custom_selections instanceof CustomSelects) { |
|
| 5157 | + foreach ($this->_custom_selections->columnAliases() as $alias) { |
|
| 5158 | + if (isset($db_results_row[ $alias ])) { |
|
| 5159 | + $results[ $alias ] = $this->convertValueToDataType( |
|
| 5160 | + $db_results_row[ $alias ], |
|
| 5161 | + $this->_custom_selections->getDataTypeForAlias($alias) |
|
| 5162 | + ); |
|
| 5163 | + } |
|
| 5164 | + } |
|
| 5165 | + } |
|
| 5166 | + return $results; |
|
| 5167 | + } |
|
| 5168 | + |
|
| 5169 | + |
|
| 5170 | + /** |
|
| 5171 | + * This will set the value for the given alias |
|
| 5172 | + * @param string $value |
|
| 5173 | + * @param string $datatype (one of %d, %s, %f) |
|
| 5174 | + * @return int|string|float (int for %d, string for %s, float for %f) |
|
| 5175 | + */ |
|
| 5176 | + protected function convertValueToDataType($value, $datatype) |
|
| 5177 | + { |
|
| 5178 | + switch ($datatype) { |
|
| 5179 | + case '%f': |
|
| 5180 | + return (float) $value; |
|
| 5181 | + case '%d': |
|
| 5182 | + return (int) $value; |
|
| 5183 | + default: |
|
| 5184 | + return (string) $value; |
|
| 5185 | + } |
|
| 5186 | + } |
|
| 5187 | + |
|
| 5188 | + |
|
| 5189 | + /** |
|
| 5190 | + * The purpose of this method is to allow us to create a model object that is not in the db that holds default |
|
| 5191 | + * values. A typical example of where this is used is when creating a new item and the initial load of a form. We |
|
| 5192 | + * dont' necessarily want to test for if the object is present but just assume it is BUT load the defaults from the |
|
| 5193 | + * object (as set in the model_field!). |
|
| 5194 | + * |
|
| 5195 | + * @return EE_Base_Class single EE_Base_Class object with default values for the properties. |
|
| 5196 | + */ |
|
| 5197 | + public function create_default_object() |
|
| 5198 | + { |
|
| 5199 | + $this_model_fields_and_values = array(); |
|
| 5200 | + // setup the row using default values; |
|
| 5201 | + foreach ($this->field_settings() as $field_name => $field_obj) { |
|
| 5202 | + $this_model_fields_and_values[ $field_name ] = $field_obj->get_default_value(); |
|
| 5203 | + } |
|
| 5204 | + $className = $this->_get_class_name(); |
|
| 5205 | + $classInstance = EE_Registry::instance() |
|
| 5206 | + ->load_class($className, array($this_model_fields_and_values), false, false); |
|
| 5207 | + return $classInstance; |
|
| 5208 | + } |
|
| 5209 | + |
|
| 5210 | + |
|
| 5211 | + |
|
| 5212 | + /** |
|
| 5213 | + * @param mixed $cols_n_values either an array of where each key is the name of a field, and the value is its value |
|
| 5214 | + * or an stdClass where each property is the name of a column, |
|
| 5215 | + * @return EE_Base_Class |
|
| 5216 | + * @throws EE_Error |
|
| 5217 | + */ |
|
| 5218 | + public function instantiate_class_from_array_or_object($cols_n_values) |
|
| 5219 | + { |
|
| 5220 | + if (! is_array($cols_n_values) && is_object($cols_n_values)) { |
|
| 5221 | + $cols_n_values = get_object_vars($cols_n_values); |
|
| 5222 | + } |
|
| 5223 | + $primary_key = null; |
|
| 5224 | + // make sure the array only has keys that are fields/columns on this model |
|
| 5225 | + $this_model_fields_n_values = $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
| 5226 | + if ($this->has_primary_key_field() && isset($this_model_fields_n_values[ $this->primary_key_name() ])) { |
|
| 5227 | + $primary_key = $this_model_fields_n_values[ $this->primary_key_name() ]; |
|
| 5228 | + } |
|
| 5229 | + $className = $this->_get_class_name(); |
|
| 5230 | + // check we actually found results that we can use to build our model object |
|
| 5231 | + // if not, return null |
|
| 5232 | + if ($this->has_primary_key_field()) { |
|
| 5233 | + if (empty($this_model_fields_n_values[ $this->primary_key_name() ])) { |
|
| 5234 | + return null; |
|
| 5235 | + } |
|
| 5236 | + } elseif ($this->unique_indexes()) { |
|
| 5237 | + $first_column = reset($this_model_fields_n_values); |
|
| 5238 | + if (empty($first_column)) { |
|
| 5239 | + return null; |
|
| 5240 | + } |
|
| 5241 | + } |
|
| 5242 | + // if there is no primary key or the object doesn't already exist in the entity map, then create a new instance |
|
| 5243 | + if ($primary_key) { |
|
| 5244 | + $classInstance = $this->get_from_entity_map($primary_key); |
|
| 5245 | + if (! $classInstance) { |
|
| 5246 | + $classInstance = EE_Registry::instance() |
|
| 5247 | + ->load_class( |
|
| 5248 | + $className, |
|
| 5249 | + array($this_model_fields_n_values, $this->_timezone), |
|
| 5250 | + true, |
|
| 5251 | + false |
|
| 5252 | + ); |
|
| 5253 | + // add this new object to the entity map |
|
| 5254 | + $classInstance = $this->add_to_entity_map($classInstance); |
|
| 5255 | + } |
|
| 5256 | + } else { |
|
| 5257 | + $classInstance = EE_Registry::instance() |
|
| 5258 | + ->load_class( |
|
| 5259 | + $className, |
|
| 5260 | + array($this_model_fields_n_values, $this->_timezone), |
|
| 5261 | + true, |
|
| 5262 | + false |
|
| 5263 | + ); |
|
| 5264 | + } |
|
| 5265 | + return $classInstance; |
|
| 5266 | + } |
|
| 5267 | + |
|
| 5268 | + |
|
| 5269 | + |
|
| 5270 | + /** |
|
| 5271 | + * Gets the model object from the entity map if it exists |
|
| 5272 | + * |
|
| 5273 | + * @param int|string $id the ID of the model object |
|
| 5274 | + * @return EE_Base_Class |
|
| 5275 | + */ |
|
| 5276 | + public function get_from_entity_map($id) |
|
| 5277 | + { |
|
| 5278 | + return isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]) |
|
| 5279 | + ? $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ] : null; |
|
| 5280 | + } |
|
| 5281 | + |
|
| 5282 | + |
|
| 5283 | + |
|
| 5284 | + /** |
|
| 5285 | + * add_to_entity_map |
|
| 5286 | + * Adds the object to the model's entity mappings |
|
| 5287 | + * Effectively tells the models "Hey, this model object is the most up-to-date representation of the data, |
|
| 5288 | + * and for the remainder of the request, it's even more up-to-date than what's in the database. |
|
| 5289 | + * So, if the database doesn't agree with what's in the entity mapper, ignore the database" |
|
| 5290 | + * If the database gets updated directly and you want the entity mapper to reflect that change, |
|
| 5291 | + * then this method should be called immediately after the update query |
|
| 5292 | + * Note: The map is indexed by whatever the current blog id is set (via EEM_Base::$_model_query_blog_id). This is |
|
| 5293 | + * so on multisite, the entity map is specific to the query being done for a specific site. |
|
| 5294 | + * |
|
| 5295 | + * @param EE_Base_Class $object |
|
| 5296 | + * @throws EE_Error |
|
| 5297 | + * @return \EE_Base_Class |
|
| 5298 | + */ |
|
| 5299 | + public function add_to_entity_map(EE_Base_Class $object) |
|
| 5300 | + { |
|
| 5301 | + $className = $this->_get_class_name(); |
|
| 5302 | + if (! $object instanceof $className) { |
|
| 5303 | + throw new EE_Error(sprintf( |
|
| 5304 | + __("You tried adding a %s to a mapping of %ss", "event_espresso"), |
|
| 5305 | + is_object($object) ? get_class($object) : $object, |
|
| 5306 | + $className |
|
| 5307 | + )); |
|
| 5308 | + } |
|
| 5309 | + /** @var $object EE_Base_Class */ |
|
| 5310 | + if (! $object->ID()) { |
|
| 5311 | + throw new EE_Error(sprintf(__( |
|
| 5312 | + "You tried storing a model object with NO ID in the %s entity mapper.", |
|
| 5313 | + "event_espresso" |
|
| 5314 | + ), get_class($this))); |
|
| 5315 | + } |
|
| 5316 | + // double check it's not already there |
|
| 5317 | + $classInstance = $this->get_from_entity_map($object->ID()); |
|
| 5318 | + if ($classInstance) { |
|
| 5319 | + return $classInstance; |
|
| 5320 | + } |
|
| 5321 | + $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $object->ID() ] = $object; |
|
| 5322 | + return $object; |
|
| 5323 | + } |
|
| 5324 | + |
|
| 5325 | + |
|
| 5326 | + |
|
| 5327 | + /** |
|
| 5328 | + * if a valid identifier is provided, then that entity is unset from the entity map, |
|
| 5329 | + * if no identifier is provided, then the entire entity map is emptied |
|
| 5330 | + * |
|
| 5331 | + * @param int|string $id the ID of the model object |
|
| 5332 | + * @return boolean |
|
| 5333 | + */ |
|
| 5334 | + public function clear_entity_map($id = null) |
|
| 5335 | + { |
|
| 5336 | + if (empty($id)) { |
|
| 5337 | + $this->_entity_map[ EEM_Base::$_model_query_blog_id ] = array(); |
|
| 5338 | + return true; |
|
| 5339 | + } |
|
| 5340 | + if (isset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ])) { |
|
| 5341 | + unset($this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ]); |
|
| 5342 | + return true; |
|
| 5343 | + } |
|
| 5344 | + return false; |
|
| 5345 | + } |
|
| 5346 | + |
|
| 5347 | + |
|
| 5348 | + |
|
| 5349 | + /** |
|
| 5350 | + * Public wrapper for _deduce_fields_n_values_from_cols_n_values. |
|
| 5351 | + * Given an array where keys are column (or column alias) names and values, |
|
| 5352 | + * returns an array of their corresponding field names and database values |
|
| 5353 | + * |
|
| 5354 | + * @param array $cols_n_values |
|
| 5355 | + * @return array |
|
| 5356 | + */ |
|
| 5357 | + public function deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
| 5358 | + { |
|
| 5359 | + return $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
| 5360 | + } |
|
| 5361 | + |
|
| 5362 | + |
|
| 5363 | + |
|
| 5364 | + /** |
|
| 5365 | + * _deduce_fields_n_values_from_cols_n_values |
|
| 5366 | + * Given an array where keys are column (or column alias) names and values, |
|
| 5367 | + * returns an array of their corresponding field names and database values |
|
| 5368 | + * |
|
| 5369 | + * @param string $cols_n_values |
|
| 5370 | + * @return array |
|
| 5371 | + */ |
|
| 5372 | + protected function _deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
| 5373 | + { |
|
| 5374 | + $this_model_fields_n_values = array(); |
|
| 5375 | + foreach ($this->get_tables() as $table_alias => $table_obj) { |
|
| 5376 | + $table_pk_value = $this->_get_column_value_with_table_alias_or_not( |
|
| 5377 | + $cols_n_values, |
|
| 5378 | + $table_obj->get_fully_qualified_pk_column(), |
|
| 5379 | + $table_obj->get_pk_column() |
|
| 5380 | + ); |
|
| 5381 | + // there is a primary key on this table and its not set. Use defaults for all its columns |
|
| 5382 | + if ($table_pk_value === null && $table_obj->get_pk_column()) { |
|
| 5383 | + foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
| 5384 | + if (! $field_obj->is_db_only_field()) { |
|
| 5385 | + // prepare field as if its coming from db |
|
| 5386 | + $prepared_value = $field_obj->prepare_for_set($field_obj->get_default_value()); |
|
| 5387 | + $this_model_fields_n_values[ $field_name ] = $field_obj->prepare_for_use_in_db($prepared_value); |
|
| 5388 | + } |
|
| 5389 | + } |
|
| 5390 | + } else { |
|
| 5391 | + // the table's rows existed. Use their values |
|
| 5392 | + foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
| 5393 | + if (! $field_obj->is_db_only_field()) { |
|
| 5394 | + $this_model_fields_n_values[ $field_name ] = $this->_get_column_value_with_table_alias_or_not( |
|
| 5395 | + $cols_n_values, |
|
| 5396 | + $field_obj->get_qualified_column(), |
|
| 5397 | + $field_obj->get_table_column() |
|
| 5398 | + ); |
|
| 5399 | + } |
|
| 5400 | + } |
|
| 5401 | + } |
|
| 5402 | + } |
|
| 5403 | + return $this_model_fields_n_values; |
|
| 5404 | + } |
|
| 5405 | + |
|
| 5406 | + |
|
| 5407 | + |
|
| 5408 | + /** |
|
| 5409 | + * @param $cols_n_values |
|
| 5410 | + * @param $qualified_column |
|
| 5411 | + * @param $regular_column |
|
| 5412 | + * @return null |
|
| 5413 | + */ |
|
| 5414 | + protected function _get_column_value_with_table_alias_or_not($cols_n_values, $qualified_column, $regular_column) |
|
| 5415 | + { |
|
| 5416 | + $value = null; |
|
| 5417 | + // ask the field what it think it's table_name.column_name should be, and call it the "qualified column" |
|
| 5418 | + // does the field on the model relate to this column retrieved from the db? |
|
| 5419 | + // or is it a db-only field? (not relating to the model) |
|
| 5420 | + if (isset($cols_n_values[ $qualified_column ])) { |
|
| 5421 | + $value = $cols_n_values[ $qualified_column ]; |
|
| 5422 | + } elseif (isset($cols_n_values[ $regular_column ])) { |
|
| 5423 | + $value = $cols_n_values[ $regular_column ]; |
|
| 5424 | + } |
|
| 5425 | + return $value; |
|
| 5426 | + } |
|
| 5427 | + |
|
| 5428 | + |
|
| 5429 | + |
|
| 5430 | + /** |
|
| 5431 | + * refresh_entity_map_from_db |
|
| 5432 | + * Makes sure the model object in the entity map at $id assumes the values |
|
| 5433 | + * of the database (opposite of EE_base_Class::save()) |
|
| 5434 | + * |
|
| 5435 | + * @param int|string $id |
|
| 5436 | + * @return EE_Base_Class |
|
| 5437 | + * @throws EE_Error |
|
| 5438 | + */ |
|
| 5439 | + public function refresh_entity_map_from_db($id) |
|
| 5440 | + { |
|
| 5441 | + $obj_in_map = $this->get_from_entity_map($id); |
|
| 5442 | + if ($obj_in_map) { |
|
| 5443 | + $wpdb_results = $this->_get_all_wpdb_results( |
|
| 5444 | + array(array($this->get_primary_key_field()->get_name() => $id), 'limit' => 1) |
|
| 5445 | + ); |
|
| 5446 | + if ($wpdb_results && is_array($wpdb_results)) { |
|
| 5447 | + $one_row = reset($wpdb_results); |
|
| 5448 | + foreach ($this->_deduce_fields_n_values_from_cols_n_values($one_row) as $field_name => $db_value) { |
|
| 5449 | + $obj_in_map->set_from_db($field_name, $db_value); |
|
| 5450 | + } |
|
| 5451 | + // clear the cache of related model objects |
|
| 5452 | + foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
| 5453 | + $obj_in_map->clear_cache($relation_name, null, true); |
|
| 5454 | + } |
|
| 5455 | + } |
|
| 5456 | + $this->_entity_map[ EEM_Base::$_model_query_blog_id ][ $id ] = $obj_in_map; |
|
| 5457 | + return $obj_in_map; |
|
| 5458 | + } |
|
| 5459 | + return $this->get_one_by_ID($id); |
|
| 5460 | + } |
|
| 5461 | + |
|
| 5462 | + |
|
| 5463 | + |
|
| 5464 | + /** |
|
| 5465 | + * refresh_entity_map_with |
|
| 5466 | + * Leaves the entry in the entity map alone, but updates it to match the provided |
|
| 5467 | + * $replacing_model_obj (which we assume to be its equivalent but somehow NOT in the entity map). |
|
| 5468 | + * This is useful if you have a model object you want to make authoritative over what's in the entity map currently. |
|
| 5469 | + * Note: The old $replacing_model_obj should now be destroyed as it's now un-authoritative |
|
| 5470 | + * |
|
| 5471 | + * @param int|string $id |
|
| 5472 | + * @param EE_Base_Class $replacing_model_obj |
|
| 5473 | + * @return \EE_Base_Class |
|
| 5474 | + * @throws EE_Error |
|
| 5475 | + */ |
|
| 5476 | + public function refresh_entity_map_with($id, $replacing_model_obj) |
|
| 5477 | + { |
|
| 5478 | + $obj_in_map = $this->get_from_entity_map($id); |
|
| 5479 | + if ($obj_in_map) { |
|
| 5480 | + if ($replacing_model_obj instanceof EE_Base_Class) { |
|
| 5481 | + foreach ($replacing_model_obj->model_field_array() as $field_name => $value) { |
|
| 5482 | + $obj_in_map->set($field_name, $value); |
|
| 5483 | + } |
|
| 5484 | + // make the model object in the entity map's cache match the $replacing_model_obj |
|
| 5485 | + foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
| 5486 | + $obj_in_map->clear_cache($relation_name, null, true); |
|
| 5487 | + foreach ($replacing_model_obj->get_all_from_cache($relation_name) as $cache_id => $cached_obj) { |
|
| 5488 | + $obj_in_map->cache($relation_name, $cached_obj, $cache_id); |
|
| 5489 | + } |
|
| 5490 | + } |
|
| 5491 | + } |
|
| 5492 | + return $obj_in_map; |
|
| 5493 | + } |
|
| 5494 | + $this->add_to_entity_map($replacing_model_obj); |
|
| 5495 | + return $replacing_model_obj; |
|
| 5496 | + } |
|
| 5497 | + |
|
| 5498 | + |
|
| 5499 | + |
|
| 5500 | + /** |
|
| 5501 | + * Gets the EE class that corresponds to this model. Eg, for EEM_Answer that |
|
| 5502 | + * would be EE_Answer.To import that class, you'd just add ".class.php" to the name, like so |
|
| 5503 | + * require_once($this->_getClassName().".class.php"); |
|
| 5504 | + * |
|
| 5505 | + * @return string |
|
| 5506 | + */ |
|
| 5507 | + private function _get_class_name() |
|
| 5508 | + { |
|
| 5509 | + return "EE_" . $this->get_this_model_name(); |
|
| 5510 | + } |
|
| 5511 | + |
|
| 5512 | + |
|
| 5513 | + |
|
| 5514 | + /** |
|
| 5515 | + * Get the name of the items this model represents, for the quantity specified. Eg, |
|
| 5516 | + * if $quantity==1, on EEM_Event, it would 'Event' (internationalized), otherwise |
|
| 5517 | + * it would be 'Events'. |
|
| 5518 | + * |
|
| 5519 | + * @param int $quantity |
|
| 5520 | + * @return string |
|
| 5521 | + */ |
|
| 5522 | + public function item_name($quantity = 1) |
|
| 5523 | + { |
|
| 5524 | + return (int) $quantity === 1 ? $this->singular_item : $this->plural_item; |
|
| 5525 | + } |
|
| 5526 | + |
|
| 5527 | + |
|
| 5528 | + |
|
| 5529 | + /** |
|
| 5530 | + * Very handy general function to allow for plugins to extend any child of EE_TempBase. |
|
| 5531 | + * If a method is called on a child of EE_TempBase that doesn't exist, this function is called |
|
| 5532 | + * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
|
| 5533 | + * requiring a plugin to extend the EE_TempBase (which works fine is there's only 1 plugin, but when will that |
|
| 5534 | + * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
|
| 5535 | + * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
|
| 5536 | + * was called, and an array of the original arguments passed to the function. Whatever their callback function |
|
| 5537 | + * returns will be returned by this function. Example: in functions.php (or in a plugin): |
|
| 5538 | + * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
|
| 5539 | + * my_callback($previousReturnValue,EE_TempBase $object,$argsArray){ |
|
| 5540 | + * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 5541 | + * return $previousReturnValue.$returnString; |
|
| 5542 | + * } |
|
| 5543 | + * require('EEM_Answer.model.php'); |
|
| 5544 | + * $answer=EEM_Answer::instance(); |
|
| 5545 | + * echo $answer->my_callback('monkeys',100); |
|
| 5546 | + * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 5547 | + * |
|
| 5548 | + * @param string $methodName name of method which was called on a child of EE_TempBase, but which |
|
| 5549 | + * @param array $args array of original arguments passed to the function |
|
| 5550 | + * @throws EE_Error |
|
| 5551 | + * @return mixed whatever the plugin which calls add_filter decides |
|
| 5552 | + */ |
|
| 5553 | + public function __call($methodName, $args) |
|
| 5554 | + { |
|
| 5555 | + $className = get_class($this); |
|
| 5556 | + $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 5557 | + if (! has_filter($tagName)) { |
|
| 5558 | + throw new EE_Error( |
|
| 5559 | + sprintf( |
|
| 5560 | + __( |
|
| 5561 | + 'Method %1$s on model %2$s does not exist! You can create one with the following code in functions.php or in a plugin: %4$s function my_callback(%4$s \$previousReturnValue, EEM_Base \$object\ $argsArray=NULL ){%4$s /*function body*/%4$s return \$whatever;%4$s }%4$s add_filter( \'%3$s\', \'my_callback\', 10, 3 );', |
|
| 5562 | + 'event_espresso' |
|
| 5563 | + ), |
|
| 5564 | + $methodName, |
|
| 5565 | + $className, |
|
| 5566 | + $tagName, |
|
| 5567 | + '<br />' |
|
| 5568 | + ) |
|
| 5569 | + ); |
|
| 5570 | + } |
|
| 5571 | + return apply_filters($tagName, null, $this, $args); |
|
| 5572 | + } |
|
| 5573 | + |
|
| 5574 | + |
|
| 5575 | + |
|
| 5576 | + /** |
|
| 5577 | + * Ensures $base_class_obj_or_id is of the EE_Base_Class child that corresponds ot this model. |
|
| 5578 | + * If not, assumes its an ID, and uses $this->get_one_by_ID() to get the EE_Base_Class. |
|
| 5579 | + * |
|
| 5580 | + * @param EE_Base_Class|string|int $base_class_obj_or_id either: |
|
| 5581 | + * the EE_Base_Class object that corresponds to this Model, |
|
| 5582 | + * the object's class name |
|
| 5583 | + * or object's ID |
|
| 5584 | + * @param boolean $ensure_is_in_db if set, we will also verify this model object |
|
| 5585 | + * exists in the database. If it does not, we add it |
|
| 5586 | + * @throws EE_Error |
|
| 5587 | + * @return EE_Base_Class |
|
| 5588 | + */ |
|
| 5589 | + public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false) |
|
| 5590 | + { |
|
| 5591 | + $className = $this->_get_class_name(); |
|
| 5592 | + if ($base_class_obj_or_id instanceof $className) { |
|
| 5593 | + $model_object = $base_class_obj_or_id; |
|
| 5594 | + } else { |
|
| 5595 | + $primary_key_field = $this->get_primary_key_field(); |
|
| 5596 | + if ($primary_key_field instanceof EE_Primary_Key_Int_Field |
|
| 5597 | + && ( |
|
| 5598 | + is_int($base_class_obj_or_id) |
|
| 5599 | + || is_string($base_class_obj_or_id) |
|
| 5600 | + ) |
|
| 5601 | + ) { |
|
| 5602 | + // assume it's an ID. |
|
| 5603 | + // either a proper integer or a string representing an integer (eg "101" instead of 101) |
|
| 5604 | + $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
| 5605 | + } elseif ($primary_key_field instanceof EE_Primary_Key_String_Field |
|
| 5606 | + && is_string($base_class_obj_or_id) |
|
| 5607 | + ) { |
|
| 5608 | + // assume its a string representation of the object |
|
| 5609 | + $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
| 5610 | + } else { |
|
| 5611 | + throw new EE_Error( |
|
| 5612 | + sprintf( |
|
| 5613 | + __( |
|
| 5614 | + "'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
| 5615 | + 'event_espresso' |
|
| 5616 | + ), |
|
| 5617 | + $base_class_obj_or_id, |
|
| 5618 | + $this->_get_class_name(), |
|
| 5619 | + print_r($base_class_obj_or_id, true) |
|
| 5620 | + ) |
|
| 5621 | + ); |
|
| 5622 | + } |
|
| 5623 | + } |
|
| 5624 | + if ($ensure_is_in_db && $model_object->ID() !== null) { |
|
| 5625 | + $model_object->save(); |
|
| 5626 | + } |
|
| 5627 | + return $model_object; |
|
| 5628 | + } |
|
| 5629 | + |
|
| 5630 | + |
|
| 5631 | + |
|
| 5632 | + /** |
|
| 5633 | + * Similar to ensure_is_obj(), this method makes sure $base_class_obj_or_id |
|
| 5634 | + * is a value of the this model's primary key. If it's an EE_Base_Class child, |
|
| 5635 | + * returns it ID. |
|
| 5636 | + * |
|
| 5637 | + * @param EE_Base_Class|int|string $base_class_obj_or_id |
|
| 5638 | + * @return int|string depending on the type of this model object's ID |
|
| 5639 | + * @throws EE_Error |
|
| 5640 | + */ |
|
| 5641 | + public function ensure_is_ID($base_class_obj_or_id) |
|
| 5642 | + { |
|
| 5643 | + $className = $this->_get_class_name(); |
|
| 5644 | + if ($base_class_obj_or_id instanceof $className) { |
|
| 5645 | + /** @var $base_class_obj_or_id EE_Base_Class */ |
|
| 5646 | + $id = $base_class_obj_or_id->ID(); |
|
| 5647 | + } elseif (is_int($base_class_obj_or_id)) { |
|
| 5648 | + // assume it's an ID |
|
| 5649 | + $id = $base_class_obj_or_id; |
|
| 5650 | + } elseif (is_string($base_class_obj_or_id)) { |
|
| 5651 | + // assume its a string representation of the object |
|
| 5652 | + $id = $base_class_obj_or_id; |
|
| 5653 | + } else { |
|
| 5654 | + throw new EE_Error(sprintf( |
|
| 5655 | + __( |
|
| 5656 | + "'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
| 5657 | + 'event_espresso' |
|
| 5658 | + ), |
|
| 5659 | + $base_class_obj_or_id, |
|
| 5660 | + $this->_get_class_name(), |
|
| 5661 | + print_r($base_class_obj_or_id, true) |
|
| 5662 | + )); |
|
| 5663 | + } |
|
| 5664 | + return $id; |
|
| 5665 | + } |
|
| 5666 | + |
|
| 5667 | + |
|
| 5668 | + |
|
| 5669 | + /** |
|
| 5670 | + * Sets whether the values passed to the model (eg, values in WHERE, values in INSERT, UPDATE, etc) |
|
| 5671 | + * have already been ran through the appropriate model field's prepare_for_use_in_db method. IE, they have |
|
| 5672 | + * been sanitized and converted into the appropriate domain. |
|
| 5673 | + * Usually the only place you'll want to change the default (which is to assume values have NOT been sanitized by |
|
| 5674 | + * the model object/model field) is when making a method call from WITHIN a model object, which has direct access |
|
| 5675 | + * to its sanitized values. Note: after changing this setting, you should set it back to its previous value (using |
|
| 5676 | + * get_assumption_concerning_values_already_prepared_by_model_object()) eg. |
|
| 5677 | + * $EVT = EEM_Event::instance(); $old_setting = |
|
| 5678 | + * $EVT->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 5679 | + * $EVT->assume_values_already_prepared_by_model_object(true); |
|
| 5680 | + * $EVT->update(array('foo'=>'bar'),array(array('foo'=>'monkey'))); |
|
| 5681 | + * $EVT->assume_values_already_prepared_by_model_object($old_setting); |
|
| 5682 | + * |
|
| 5683 | + * @param int $values_already_prepared like one of the constants on EEM_Base |
|
| 5684 | + * @return void |
|
| 5685 | + */ |
|
| 5686 | + public function assume_values_already_prepared_by_model_object( |
|
| 5687 | + $values_already_prepared = self::not_prepared_by_model_object |
|
| 5688 | + ) { |
|
| 5689 | + $this->_values_already_prepared_by_model_object = $values_already_prepared; |
|
| 5690 | + } |
|
| 5691 | + |
|
| 5692 | + |
|
| 5693 | + |
|
| 5694 | + /** |
|
| 5695 | + * Read comments for assume_values_already_prepared_by_model_object() |
|
| 5696 | + * |
|
| 5697 | + * @return int |
|
| 5698 | + */ |
|
| 5699 | + public function get_assumption_concerning_values_already_prepared_by_model_object() |
|
| 5700 | + { |
|
| 5701 | + return $this->_values_already_prepared_by_model_object; |
|
| 5702 | + } |
|
| 5703 | + |
|
| 5704 | + |
|
| 5705 | + |
|
| 5706 | + /** |
|
| 5707 | + * Gets all the indexes on this model |
|
| 5708 | + * |
|
| 5709 | + * @return EE_Index[] |
|
| 5710 | + */ |
|
| 5711 | + public function indexes() |
|
| 5712 | + { |
|
| 5713 | + return $this->_indexes; |
|
| 5714 | + } |
|
| 5715 | + |
|
| 5716 | + |
|
| 5717 | + |
|
| 5718 | + /** |
|
| 5719 | + * Gets all the Unique Indexes on this model |
|
| 5720 | + * |
|
| 5721 | + * @return EE_Unique_Index[] |
|
| 5722 | + */ |
|
| 5723 | + public function unique_indexes() |
|
| 5724 | + { |
|
| 5725 | + $unique_indexes = array(); |
|
| 5726 | + foreach ($this->_indexes as $name => $index) { |
|
| 5727 | + if ($index instanceof EE_Unique_Index) { |
|
| 5728 | + $unique_indexes [ $name ] = $index; |
|
| 5729 | + } |
|
| 5730 | + } |
|
| 5731 | + return $unique_indexes; |
|
| 5732 | + } |
|
| 5733 | + |
|
| 5734 | + |
|
| 5735 | + |
|
| 5736 | + /** |
|
| 5737 | + * Gets all the fields which, when combined, make the primary key. |
|
| 5738 | + * This is usually just an array with 1 element (the primary key), but in cases |
|
| 5739 | + * where there is no primary key, it's a combination of fields as defined |
|
| 5740 | + * on a primary index |
|
| 5741 | + * |
|
| 5742 | + * @return EE_Model_Field_Base[] indexed by the field's name |
|
| 5743 | + * @throws EE_Error |
|
| 5744 | + */ |
|
| 5745 | + public function get_combined_primary_key_fields() |
|
| 5746 | + { |
|
| 5747 | + foreach ($this->indexes() as $index) { |
|
| 5748 | + if ($index instanceof EE_Primary_Key_Index) { |
|
| 5749 | + return $index->fields(); |
|
| 5750 | + } |
|
| 5751 | + } |
|
| 5752 | + return array($this->primary_key_name() => $this->get_primary_key_field()); |
|
| 5753 | + } |
|
| 5754 | + |
|
| 5755 | + |
|
| 5756 | + |
|
| 5757 | + /** |
|
| 5758 | + * Used to build a primary key string (when the model has no primary key), |
|
| 5759 | + * which can be used a unique string to identify this model object. |
|
| 5760 | + * |
|
| 5761 | + * @param array $fields_n_values keys are field names, values are their values. |
|
| 5762 | + * Note: if you have results from `EEM_Base::get_all_wpdb_results()`, you need to |
|
| 5763 | + * run it through `EEM_Base::deduce_fields_n_values_from_cols_n_values()` |
|
| 5764 | + * before passing it to this function (that will convert it from columns-n-values |
|
| 5765 | + * to field-names-n-values). |
|
| 5766 | + * @return string |
|
| 5767 | + * @throws EE_Error |
|
| 5768 | + */ |
|
| 5769 | + public function get_index_primary_key_string($fields_n_values) |
|
| 5770 | + { |
|
| 5771 | + $cols_n_values_for_primary_key_index = array_intersect_key( |
|
| 5772 | + $fields_n_values, |
|
| 5773 | + $this->get_combined_primary_key_fields() |
|
| 5774 | + ); |
|
| 5775 | + return http_build_query($cols_n_values_for_primary_key_index); |
|
| 5776 | + } |
|
| 5777 | + |
|
| 5778 | + |
|
| 5779 | + |
|
| 5780 | + /** |
|
| 5781 | + * Gets the field values from the primary key string |
|
| 5782 | + * |
|
| 5783 | + * @see EEM_Base::get_combined_primary_key_fields() and EEM_Base::get_index_primary_key_string() |
|
| 5784 | + * @param string $index_primary_key_string |
|
| 5785 | + * @return null|array |
|
| 5786 | + * @throws EE_Error |
|
| 5787 | + */ |
|
| 5788 | + public function parse_index_primary_key_string($index_primary_key_string) |
|
| 5789 | + { |
|
| 5790 | + $key_fields = $this->get_combined_primary_key_fields(); |
|
| 5791 | + // check all of them are in the $id |
|
| 5792 | + $key_vals_in_combined_pk = array(); |
|
| 5793 | + parse_str($index_primary_key_string, $key_vals_in_combined_pk); |
|
| 5794 | + foreach ($key_fields as $key_field_name => $field_obj) { |
|
| 5795 | + if (! isset($key_vals_in_combined_pk[ $key_field_name ])) { |
|
| 5796 | + return null; |
|
| 5797 | + } |
|
| 5798 | + } |
|
| 5799 | + return $key_vals_in_combined_pk; |
|
| 5800 | + } |
|
| 5801 | + |
|
| 5802 | + |
|
| 5803 | + |
|
| 5804 | + /** |
|
| 5805 | + * verifies that an array of key-value pairs for model fields has a key |
|
| 5806 | + * for each field comprising the primary key index |
|
| 5807 | + * |
|
| 5808 | + * @param array $key_vals |
|
| 5809 | + * @return boolean |
|
| 5810 | + * @throws EE_Error |
|
| 5811 | + */ |
|
| 5812 | + public function has_all_combined_primary_key_fields($key_vals) |
|
| 5813 | + { |
|
| 5814 | + $keys_it_should_have = array_keys($this->get_combined_primary_key_fields()); |
|
| 5815 | + foreach ($keys_it_should_have as $key) { |
|
| 5816 | + if (! isset($key_vals[ $key ])) { |
|
| 5817 | + return false; |
|
| 5818 | + } |
|
| 5819 | + } |
|
| 5820 | + return true; |
|
| 5821 | + } |
|
| 5822 | + |
|
| 5823 | + |
|
| 5824 | + |
|
| 5825 | + /** |
|
| 5826 | + * Finds all model objects in the DB that appear to be a copy of $model_object_or_attributes_array. |
|
| 5827 | + * We consider something to be a copy if all the attributes match (except the ID, of course). |
|
| 5828 | + * |
|
| 5829 | + * @param array|EE_Base_Class $model_object_or_attributes_array If its an array, it's field-value pairs |
|
| 5830 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 5831 | + * @throws EE_Error |
|
| 5832 | + * @return \EE_Base_Class[] Array keys are object IDs (if there is a primary key on the model. if not, numerically |
|
| 5833 | + * indexed) |
|
| 5834 | + */ |
|
| 5835 | + public function get_all_copies($model_object_or_attributes_array, $query_params = array()) |
|
| 5836 | + { |
|
| 5837 | + if ($model_object_or_attributes_array instanceof EE_Base_Class) { |
|
| 5838 | + $attributes_array = $model_object_or_attributes_array->model_field_array(); |
|
| 5839 | + } elseif (is_array($model_object_or_attributes_array)) { |
|
| 5840 | + $attributes_array = $model_object_or_attributes_array; |
|
| 5841 | + } else { |
|
| 5842 | + throw new EE_Error(sprintf(__( |
|
| 5843 | + "get_all_copies should be provided with either a model object or an array of field-value-pairs, but was given %s", |
|
| 5844 | + "event_espresso" |
|
| 5845 | + ), $model_object_or_attributes_array)); |
|
| 5846 | + } |
|
| 5847 | + // even copies obviously won't have the same ID, so remove the primary key |
|
| 5848 | + // from the WHERE conditions for finding copies (if there is a primary key, of course) |
|
| 5849 | + if ($this->has_primary_key_field() && isset($attributes_array[ $this->primary_key_name() ])) { |
|
| 5850 | + unset($attributes_array[ $this->primary_key_name() ]); |
|
| 5851 | + } |
|
| 5852 | + if (isset($query_params[0])) { |
|
| 5853 | + $query_params[0] = array_merge($attributes_array, $query_params); |
|
| 5854 | + } else { |
|
| 5855 | + $query_params[0] = $attributes_array; |
|
| 5856 | + } |
|
| 5857 | + return $this->get_all($query_params); |
|
| 5858 | + } |
|
| 5859 | + |
|
| 5860 | + |
|
| 5861 | + |
|
| 5862 | + /** |
|
| 5863 | + * Gets the first copy we find. See get_all_copies for more details |
|
| 5864 | + * |
|
| 5865 | + * @param mixed EE_Base_Class | array $model_object_or_attributes_array |
|
| 5866 | + * @param array $query_params |
|
| 5867 | + * @return EE_Base_Class |
|
| 5868 | + * @throws EE_Error |
|
| 5869 | + */ |
|
| 5870 | + public function get_one_copy($model_object_or_attributes_array, $query_params = array()) |
|
| 5871 | + { |
|
| 5872 | + if (! is_array($query_params)) { |
|
| 5873 | + EE_Error::doing_it_wrong( |
|
| 5874 | + 'EEM_Base::get_one_copy', |
|
| 5875 | + sprintf( |
|
| 5876 | + __('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 5877 | + gettype($query_params) |
|
| 5878 | + ), |
|
| 5879 | + '4.6.0' |
|
| 5880 | + ); |
|
| 5881 | + $query_params = array(); |
|
| 5882 | + } |
|
| 5883 | + $query_params['limit'] = 1; |
|
| 5884 | + $copies = $this->get_all_copies($model_object_or_attributes_array, $query_params); |
|
| 5885 | + if (is_array($copies)) { |
|
| 5886 | + return array_shift($copies); |
|
| 5887 | + } |
|
| 5888 | + return null; |
|
| 5889 | + } |
|
| 5890 | + |
|
| 5891 | + |
|
| 5892 | + |
|
| 5893 | + /** |
|
| 5894 | + * Updates the item with the specified id. Ignores default query parameters because |
|
| 5895 | + * we have specified the ID, and its assumed we KNOW what we're doing |
|
| 5896 | + * |
|
| 5897 | + * @param array $fields_n_values keys are field names, values are their new values |
|
| 5898 | + * @param int|string $id the value of the primary key to update |
|
| 5899 | + * @return int number of rows updated |
|
| 5900 | + * @throws EE_Error |
|
| 5901 | + */ |
|
| 5902 | + public function update_by_ID($fields_n_values, $id) |
|
| 5903 | + { |
|
| 5904 | + $query_params = array( |
|
| 5905 | + 0 => array($this->get_primary_key_field()->get_name() => $id), |
|
| 5906 | + 'default_where_conditions' => EEM_Base::default_where_conditions_others_only, |
|
| 5907 | + ); |
|
| 5908 | + return $this->update($fields_n_values, $query_params); |
|
| 5909 | + } |
|
| 5910 | + |
|
| 5911 | + |
|
| 5912 | + |
|
| 5913 | + /** |
|
| 5914 | + * Changes an operator which was supplied to the models into one usable in SQL |
|
| 5915 | + * |
|
| 5916 | + * @param string $operator_supplied |
|
| 5917 | + * @return string an operator which can be used in SQL |
|
| 5918 | + * @throws EE_Error |
|
| 5919 | + */ |
|
| 5920 | + private function _prepare_operator_for_sql($operator_supplied) |
|
| 5921 | + { |
|
| 5922 | + $sql_operator = isset($this->_valid_operators[ $operator_supplied ]) ? $this->_valid_operators[ $operator_supplied ] |
|
| 5923 | + : null; |
|
| 5924 | + if ($sql_operator) { |
|
| 5925 | + return $sql_operator; |
|
| 5926 | + } |
|
| 5927 | + throw new EE_Error( |
|
| 5928 | + sprintf( |
|
| 5929 | + __( |
|
| 5930 | + "The operator '%s' is not in the list of valid operators: %s", |
|
| 5931 | + "event_espresso" |
|
| 5932 | + ), |
|
| 5933 | + $operator_supplied, |
|
| 5934 | + implode(",", array_keys($this->_valid_operators)) |
|
| 5935 | + ) |
|
| 5936 | + ); |
|
| 5937 | + } |
|
| 5938 | + |
|
| 5939 | + |
|
| 5940 | + |
|
| 5941 | + /** |
|
| 5942 | + * Gets the valid operators |
|
| 5943 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5944 | + */ |
|
| 5945 | + public function valid_operators() |
|
| 5946 | + { |
|
| 5947 | + return $this->_valid_operators; |
|
| 5948 | + } |
|
| 5949 | + |
|
| 5950 | + |
|
| 5951 | + |
|
| 5952 | + /** |
|
| 5953 | + * Gets the between-style operators (take 2 arguments). |
|
| 5954 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5955 | + */ |
|
| 5956 | + public function valid_between_style_operators() |
|
| 5957 | + { |
|
| 5958 | + return array_intersect( |
|
| 5959 | + $this->valid_operators(), |
|
| 5960 | + $this->_between_style_operators |
|
| 5961 | + ); |
|
| 5962 | + } |
|
| 5963 | + |
|
| 5964 | + /** |
|
| 5965 | + * Gets the "like"-style operators (take a single argument, but it may contain wildcards) |
|
| 5966 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5967 | + */ |
|
| 5968 | + public function valid_like_style_operators() |
|
| 5969 | + { |
|
| 5970 | + return array_intersect( |
|
| 5971 | + $this->valid_operators(), |
|
| 5972 | + $this->_like_style_operators |
|
| 5973 | + ); |
|
| 5974 | + } |
|
| 5975 | + |
|
| 5976 | + /** |
|
| 5977 | + * Gets the "in"-style operators |
|
| 5978 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5979 | + */ |
|
| 5980 | + public function valid_in_style_operators() |
|
| 5981 | + { |
|
| 5982 | + return array_intersect( |
|
| 5983 | + $this->valid_operators(), |
|
| 5984 | + $this->_in_style_operators |
|
| 5985 | + ); |
|
| 5986 | + } |
|
| 5987 | + |
|
| 5988 | + /** |
|
| 5989 | + * Gets the "null"-style operators (accept no arguments) |
|
| 5990 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5991 | + */ |
|
| 5992 | + public function valid_null_style_operators() |
|
| 5993 | + { |
|
| 5994 | + return array_intersect( |
|
| 5995 | + $this->valid_operators(), |
|
| 5996 | + $this->_null_style_operators |
|
| 5997 | + ); |
|
| 5998 | + } |
|
| 5999 | + |
|
| 6000 | + /** |
|
| 6001 | + * Gets an array where keys are the primary keys and values are their 'names' |
|
| 6002 | + * (as determined by the model object's name() function, which is often overridden) |
|
| 6003 | + * |
|
| 6004 | + * @param array $query_params like get_all's |
|
| 6005 | + * @return string[] |
|
| 6006 | + * @throws EE_Error |
|
| 6007 | + */ |
|
| 6008 | + public function get_all_names($query_params = array()) |
|
| 6009 | + { |
|
| 6010 | + $objs = $this->get_all($query_params); |
|
| 6011 | + $names = array(); |
|
| 6012 | + foreach ($objs as $obj) { |
|
| 6013 | + $names[ $obj->ID() ] = $obj->name(); |
|
| 6014 | + } |
|
| 6015 | + return $names; |
|
| 6016 | + } |
|
| 6017 | + |
|
| 6018 | + |
|
| 6019 | + |
|
| 6020 | + /** |
|
| 6021 | + * Gets an array of primary keys from the model objects. If you acquired the model objects |
|
| 6022 | + * using EEM_Base::get_all() you don't need to call this (and probably shouldn't because |
|
| 6023 | + * this is duplicated effort and reduces efficiency) you would be better to use |
|
| 6024 | + * array_keys() on $model_objects. |
|
| 6025 | + * |
|
| 6026 | + * @param \EE_Base_Class[] $model_objects |
|
| 6027 | + * @param boolean $filter_out_empty_ids if a model object has an ID of '' or 0, don't bother including it |
|
| 6028 | + * in the returned array |
|
| 6029 | + * @return array |
|
| 6030 | + * @throws EE_Error |
|
| 6031 | + */ |
|
| 6032 | + public function get_IDs($model_objects, $filter_out_empty_ids = false) |
|
| 6033 | + { |
|
| 6034 | + if (! $this->has_primary_key_field()) { |
|
| 6035 | + if (WP_DEBUG) { |
|
| 6036 | + EE_Error::add_error( |
|
| 6037 | + __('Trying to get IDs from a model than has no primary key', 'event_espresso'), |
|
| 6038 | + __FILE__, |
|
| 6039 | + __FUNCTION__, |
|
| 6040 | + __LINE__ |
|
| 6041 | + ); |
|
| 6042 | + } |
|
| 6043 | + } |
|
| 6044 | + $IDs = array(); |
|
| 6045 | + foreach ($model_objects as $model_object) { |
|
| 6046 | + $id = $model_object->ID(); |
|
| 6047 | + if (! $id) { |
|
| 6048 | + if ($filter_out_empty_ids) { |
|
| 6049 | + continue; |
|
| 6050 | + } |
|
| 6051 | + if (WP_DEBUG) { |
|
| 6052 | + EE_Error::add_error( |
|
| 6053 | + __( |
|
| 6054 | + 'Called %1$s on a model object that has no ID and so probably hasn\'t been saved to the database', |
|
| 6055 | + 'event_espresso' |
|
| 6056 | + ), |
|
| 6057 | + __FILE__, |
|
| 6058 | + __FUNCTION__, |
|
| 6059 | + __LINE__ |
|
| 6060 | + ); |
|
| 6061 | + } |
|
| 6062 | + } |
|
| 6063 | + $IDs[] = $id; |
|
| 6064 | + } |
|
| 6065 | + return $IDs; |
|
| 6066 | + } |
|
| 6067 | + |
|
| 6068 | + |
|
| 6069 | + |
|
| 6070 | + /** |
|
| 6071 | + * Returns the string used in capabilities relating to this model. If there |
|
| 6072 | + * are no capabilities that relate to this model returns false |
|
| 6073 | + * |
|
| 6074 | + * @return string|false |
|
| 6075 | + */ |
|
| 6076 | + public function cap_slug() |
|
| 6077 | + { |
|
| 6078 | + return apply_filters('FHEE__EEM_Base__cap_slug', $this->_caps_slug, $this); |
|
| 6079 | + } |
|
| 6080 | + |
|
| 6081 | + |
|
| 6082 | + |
|
| 6083 | + /** |
|
| 6084 | + * Returns the capability-restrictions array (@see EEM_Base::_cap_restrictions). |
|
| 6085 | + * If $context is provided (which should be set to one of EEM_Base::valid_cap_contexts()) |
|
| 6086 | + * only returns the cap restrictions array in that context (ie, the array |
|
| 6087 | + * at that key) |
|
| 6088 | + * |
|
| 6089 | + * @param string $context |
|
| 6090 | + * @return EE_Default_Where_Conditions[] indexed by associated capability |
|
| 6091 | + * @throws EE_Error |
|
| 6092 | + */ |
|
| 6093 | + public function cap_restrictions($context = EEM_Base::caps_read) |
|
| 6094 | + { |
|
| 6095 | + EEM_Base::verify_is_valid_cap_context($context); |
|
| 6096 | + // check if we ought to run the restriction generator first |
|
| 6097 | + if (isset($this->_cap_restriction_generators[ $context ]) |
|
| 6098 | + && $this->_cap_restriction_generators[ $context ] instanceof EE_Restriction_Generator_Base |
|
| 6099 | + && ! $this->_cap_restriction_generators[ $context ]->has_generated_cap_restrictions() |
|
| 6100 | + ) { |
|
| 6101 | + $this->_cap_restrictions[ $context ] = array_merge( |
|
| 6102 | + $this->_cap_restrictions[ $context ], |
|
| 6103 | + $this->_cap_restriction_generators[ $context ]->generate_restrictions() |
|
| 6104 | + ); |
|
| 6105 | + } |
|
| 6106 | + // and make sure we've finalized the construction of each restriction |
|
| 6107 | + foreach ($this->_cap_restrictions[ $context ] as $where_conditions_obj) { |
|
| 6108 | + if ($where_conditions_obj instanceof EE_Default_Where_Conditions) { |
|
| 6109 | + $where_conditions_obj->_finalize_construct($this); |
|
| 6110 | + } |
|
| 6111 | + } |
|
| 6112 | + return $this->_cap_restrictions[ $context ]; |
|
| 6113 | + } |
|
| 6114 | + |
|
| 6115 | + |
|
| 6116 | + |
|
| 6117 | + /** |
|
| 6118 | + * Indicating whether or not this model thinks its a wp core model |
|
| 6119 | + * |
|
| 6120 | + * @return boolean |
|
| 6121 | + */ |
|
| 6122 | + public function is_wp_core_model() |
|
| 6123 | + { |
|
| 6124 | + return $this->_wp_core_model; |
|
| 6125 | + } |
|
| 6126 | + |
|
| 6127 | + |
|
| 6128 | + |
|
| 6129 | + /** |
|
| 6130 | + * Gets all the caps that are missing which impose a restriction on |
|
| 6131 | + * queries made in this context |
|
| 6132 | + * |
|
| 6133 | + * @param string $context one of EEM_Base::caps_ constants |
|
| 6134 | + * @return EE_Default_Where_Conditions[] indexed by capability name |
|
| 6135 | + * @throws EE_Error |
|
| 6136 | + */ |
|
| 6137 | + public function caps_missing($context = EEM_Base::caps_read) |
|
| 6138 | + { |
|
| 6139 | + $missing_caps = array(); |
|
| 6140 | + $cap_restrictions = $this->cap_restrictions($context); |
|
| 6141 | + foreach ($cap_restrictions as $cap => $restriction_if_no_cap) { |
|
| 6142 | + if (! EE_Capabilities::instance() |
|
| 6143 | + ->current_user_can($cap, $this->get_this_model_name() . '_model_applying_caps') |
|
| 6144 | + ) { |
|
| 6145 | + $missing_caps[ $cap ] = $restriction_if_no_cap; |
|
| 6146 | + } |
|
| 6147 | + } |
|
| 6148 | + return $missing_caps; |
|
| 6149 | + } |
|
| 6150 | + |
|
| 6151 | + |
|
| 6152 | + |
|
| 6153 | + /** |
|
| 6154 | + * Gets the mapping from capability contexts to action strings used in capability names |
|
| 6155 | + * |
|
| 6156 | + * @return array keys are one of EEM_Base::valid_cap_contexts(), and values are usually |
|
| 6157 | + * one of 'read', 'edit', or 'delete' |
|
| 6158 | + */ |
|
| 6159 | + public function cap_contexts_to_cap_action_map() |
|
| 6160 | + { |
|
| 6161 | + return apply_filters( |
|
| 6162 | + 'FHEE__EEM_Base__cap_contexts_to_cap_action_map', |
|
| 6163 | + $this->_cap_contexts_to_cap_action_map, |
|
| 6164 | + $this |
|
| 6165 | + ); |
|
| 6166 | + } |
|
| 6167 | + |
|
| 6168 | + |
|
| 6169 | + |
|
| 6170 | + /** |
|
| 6171 | + * Gets the action string for the specified capability context |
|
| 6172 | + * |
|
| 6173 | + * @param string $context |
|
| 6174 | + * @return string one of EEM_Base::cap_contexts_to_cap_action_map() values |
|
| 6175 | + * @throws EE_Error |
|
| 6176 | + */ |
|
| 6177 | + public function cap_action_for_context($context) |
|
| 6178 | + { |
|
| 6179 | + $mapping = $this->cap_contexts_to_cap_action_map(); |
|
| 6180 | + if (isset($mapping[ $context ])) { |
|
| 6181 | + return $mapping[ $context ]; |
|
| 6182 | + } |
|
| 6183 | + if ($action = apply_filters('FHEE__EEM_Base__cap_action_for_context', null, $this, $mapping, $context)) { |
|
| 6184 | + return $action; |
|
| 6185 | + } |
|
| 6186 | + throw new EE_Error( |
|
| 6187 | + sprintf( |
|
| 6188 | + __('Cannot find capability restrictions for context "%1$s", allowed values are:%2$s', 'event_espresso'), |
|
| 6189 | + $context, |
|
| 6190 | + implode(',', array_keys($this->cap_contexts_to_cap_action_map())) |
|
| 6191 | + ) |
|
| 6192 | + ); |
|
| 6193 | + } |
|
| 6194 | + |
|
| 6195 | + |
|
| 6196 | + |
|
| 6197 | + /** |
|
| 6198 | + * Returns all the capability contexts which are valid when querying models |
|
| 6199 | + * |
|
| 6200 | + * @return array |
|
| 6201 | + */ |
|
| 6202 | + public static function valid_cap_contexts() |
|
| 6203 | + { |
|
| 6204 | + return apply_filters('FHEE__EEM_Base__valid_cap_contexts', array( |
|
| 6205 | + self::caps_read, |
|
| 6206 | + self::caps_read_admin, |
|
| 6207 | + self::caps_edit, |
|
| 6208 | + self::caps_delete, |
|
| 6209 | + )); |
|
| 6210 | + } |
|
| 6211 | + |
|
| 6212 | + |
|
| 6213 | + |
|
| 6214 | + /** |
|
| 6215 | + * Returns all valid options for 'default_where_conditions' |
|
| 6216 | + * |
|
| 6217 | + * @return array |
|
| 6218 | + */ |
|
| 6219 | + public static function valid_default_where_conditions() |
|
| 6220 | + { |
|
| 6221 | + return array( |
|
| 6222 | + EEM_Base::default_where_conditions_all, |
|
| 6223 | + EEM_Base::default_where_conditions_this_only, |
|
| 6224 | + EEM_Base::default_where_conditions_others_only, |
|
| 6225 | + EEM_Base::default_where_conditions_minimum_all, |
|
| 6226 | + EEM_Base::default_where_conditions_minimum_others, |
|
| 6227 | + EEM_Base::default_where_conditions_none |
|
| 6228 | + ); |
|
| 6229 | + } |
|
| 6230 | + |
|
| 6231 | + // public static function default_where_conditions_full |
|
| 6232 | + /** |
|
| 6233 | + * Verifies $context is one of EEM_Base::valid_cap_contexts(), if not it throws an exception |
|
| 6234 | + * |
|
| 6235 | + * @param string $context |
|
| 6236 | + * @return bool |
|
| 6237 | + * @throws EE_Error |
|
| 6238 | + */ |
|
| 6239 | + public static function verify_is_valid_cap_context($context) |
|
| 6240 | + { |
|
| 6241 | + $valid_cap_contexts = EEM_Base::valid_cap_contexts(); |
|
| 6242 | + if (in_array($context, $valid_cap_contexts)) { |
|
| 6243 | + return true; |
|
| 6244 | + } |
|
| 6245 | + throw new EE_Error( |
|
| 6246 | + sprintf( |
|
| 6247 | + __( |
|
| 6248 | + 'Context "%1$s" passed into model "%2$s" is not a valid context. They are: %3$s', |
|
| 6249 | + 'event_espresso' |
|
| 6250 | + ), |
|
| 6251 | + $context, |
|
| 6252 | + 'EEM_Base', |
|
| 6253 | + implode(',', $valid_cap_contexts) |
|
| 6254 | + ) |
|
| 6255 | + ); |
|
| 6256 | + } |
|
| 6257 | + |
|
| 6258 | + |
|
| 6259 | + |
|
| 6260 | + /** |
|
| 6261 | + * Clears all the models field caches. This is only useful when a sub-class |
|
| 6262 | + * might have added a field or something and these caches might be invalidated |
|
| 6263 | + */ |
|
| 6264 | + protected function _invalidate_field_caches() |
|
| 6265 | + { |
|
| 6266 | + $this->_cache_foreign_key_to_fields = array(); |
|
| 6267 | + $this->_cached_fields = null; |
|
| 6268 | + $this->_cached_fields_non_db_only = null; |
|
| 6269 | + } |
|
| 6270 | + |
|
| 6271 | + |
|
| 6272 | + |
|
| 6273 | + /** |
|
| 6274 | + * Gets the list of all the where query param keys that relate to logic instead of field names |
|
| 6275 | + * (eg "and", "or", "not"). |
|
| 6276 | + * |
|
| 6277 | + * @return array |
|
| 6278 | + */ |
|
| 6279 | + public function logic_query_param_keys() |
|
| 6280 | + { |
|
| 6281 | + return $this->_logic_query_param_keys; |
|
| 6282 | + } |
|
| 6283 | + |
|
| 6284 | + |
|
| 6285 | + |
|
| 6286 | + /** |
|
| 6287 | + * Determines whether or not the where query param array key is for a logic query param. |
|
| 6288 | + * Eg 'OR', 'not*', and 'and*because-i-say-so' should all return true, whereas |
|
| 6289 | + * 'ATT_fname', 'EVT_name*not-you-or-me', and 'ORG_name' should return false |
|
| 6290 | + * |
|
| 6291 | + * @param $query_param_key |
|
| 6292 | + * @return bool |
|
| 6293 | + */ |
|
| 6294 | + public function is_logic_query_param_key($query_param_key) |
|
| 6295 | + { |
|
| 6296 | + foreach ($this->logic_query_param_keys() as $logic_query_param_key) { |
|
| 6297 | + if ($query_param_key === $logic_query_param_key |
|
| 6298 | + || strpos($query_param_key, $logic_query_param_key . '*') === 0 |
|
| 6299 | + ) { |
|
| 6300 | + return true; |
|
| 6301 | + } |
|
| 6302 | + } |
|
| 6303 | + return false; |
|
| 6304 | + } |
|
| 6305 | + |
|
| 6306 | + /** |
|
| 6307 | + * Returns true if this model has a password field on it (regardless of whether that password field has any content) |
|
| 6308 | + * @since 4.9.74.p |
|
| 6309 | + * @return boolean |
|
| 6310 | + */ |
|
| 6311 | + public function hasPassword() |
|
| 6312 | + { |
|
| 6313 | + // if we don't yet know if there's a password field, find out and remember it for next time. |
|
| 6314 | + if ($this->has_password_field === null) { |
|
| 6315 | + $password_field = $this->getPasswordField(); |
|
| 6316 | + $this->has_password_field = $password_field instanceof EE_Password_Field ? true : false; |
|
| 6317 | + } |
|
| 6318 | + return $this->has_password_field; |
|
| 6319 | + } |
|
| 6320 | + |
|
| 6321 | + /** |
|
| 6322 | + * Returns the password field on this model, if there is one |
|
| 6323 | + * @since 4.9.74.p |
|
| 6324 | + * @return EE_Password_Field|null |
|
| 6325 | + */ |
|
| 6326 | + public function getPasswordField() |
|
| 6327 | + { |
|
| 6328 | + // if we definetely already know there is a password field or not (because has_password_field is true or false) |
|
| 6329 | + // there's no need to search for it. If we don't know yet, then find out |
|
| 6330 | + if ($this->has_password_field === null && $this->password_field === null) { |
|
| 6331 | + $this->password_field = $this->get_a_field_of_type('EE_Password_Field'); |
|
| 6332 | + } |
|
| 6333 | + // don't bother setting has_password_field because that's hasPassword()'s job. |
|
| 6334 | + return $this->password_field; |
|
| 6335 | + } |
|
| 6336 | + |
|
| 6337 | + |
|
| 6338 | + /** |
|
| 6339 | + * Returns the list of field (as EE_Model_Field_Bases) that are protected by the password |
|
| 6340 | + * @since 4.9.74.p |
|
| 6341 | + * @return EE_Model_Field_Base[] |
|
| 6342 | + * @throws EE_Error |
|
| 6343 | + */ |
|
| 6344 | + public function getPasswordProtectedFields() |
|
| 6345 | + { |
|
| 6346 | + $password_field = $this->getPasswordField(); |
|
| 6347 | + $fields = array(); |
|
| 6348 | + if ($password_field instanceof EE_Password_Field) { |
|
| 6349 | + $field_names = $password_field->protectedFields(); |
|
| 6350 | + foreach ($field_names as $field_name) { |
|
| 6351 | + $fields[ $field_name ] = $this->field_settings_for($field_name); |
|
| 6352 | + } |
|
| 6353 | + } |
|
| 6354 | + return $fields; |
|
| 6355 | + } |
|
| 6356 | + |
|
| 6357 | + |
|
| 6358 | + /** |
|
| 6359 | + * Checks if the current user can perform the requested action on this model |
|
| 6360 | + * @since 4.9.74.p |
|
| 6361 | + * @param string $cap_to_check one of the array keys from _cap_contexts_to_cap_action_map |
|
| 6362 | + * @param EE_Base_Class|array $model_obj_or_fields_n_values |
|
| 6363 | + * @return bool |
|
| 6364 | + * @throws EE_Error |
|
| 6365 | + * @throws InvalidArgumentException |
|
| 6366 | + * @throws InvalidDataTypeException |
|
| 6367 | + * @throws InvalidInterfaceException |
|
| 6368 | + * @throws ReflectionException |
|
| 6369 | + * @throws UnexpectedEntityException |
|
| 6370 | + */ |
|
| 6371 | + public function currentUserCan($cap_to_check, $model_obj_or_fields_n_values) |
|
| 6372 | + { |
|
| 6373 | + if ($model_obj_or_fields_n_values instanceof EE_Base_Class) { |
|
| 6374 | + $model_obj_or_fields_n_values = $model_obj_or_fields_n_values->model_field_array(); |
|
| 6375 | + } |
|
| 6376 | + if (!is_array($model_obj_or_fields_n_values)) { |
|
| 6377 | + throw new UnexpectedEntityException( |
|
| 6378 | + $model_obj_or_fields_n_values, |
|
| 6379 | + 'EE_Base_Class', |
|
| 6380 | + sprintf( |
|
| 6381 | + esc_html__('%1$s must be passed an `EE_Base_Class or an array of fields names with their values. You passed in something different.', 'event_espresso'), |
|
| 6382 | + __FUNCTION__ |
|
| 6383 | + ) |
|
| 6384 | + ); |
|
| 6385 | + } |
|
| 6386 | + return $this->exists( |
|
| 6387 | + $this->alter_query_params_to_restrict_by_ID( |
|
| 6388 | + $this->get_index_primary_key_string($model_obj_or_fields_n_values), |
|
| 6389 | + array( |
|
| 6390 | + 'default_where_conditions' => 'none', |
|
| 6391 | + 'caps' => $cap_to_check, |
|
| 6392 | + ) |
|
| 6393 | + ) |
|
| 6394 | + ); |
|
| 6395 | + } |
|
| 6396 | + |
|
| 6397 | + /** |
|
| 6398 | + * Returns the query param where conditions key to the password affecting this model. |
|
| 6399 | + * Eg on EEM_Event this would just be "password", on EEM_Datetime this would be "Event.password", etc. |
|
| 6400 | + * @since 4.9.74.p |
|
| 6401 | + * @return null|string |
|
| 6402 | + * @throws EE_Error |
|
| 6403 | + * @throws InvalidArgumentException |
|
| 6404 | + * @throws InvalidDataTypeException |
|
| 6405 | + * @throws InvalidInterfaceException |
|
| 6406 | + * @throws ModelConfigurationException |
|
| 6407 | + * @throws ReflectionException |
|
| 6408 | + */ |
|
| 6409 | + public function modelChainAndPassword() |
|
| 6410 | + { |
|
| 6411 | + if ($this->model_chain_to_password === null) { |
|
| 6412 | + throw new ModelConfigurationException( |
|
| 6413 | + $this, |
|
| 6414 | + esc_html_x( |
|
| 6415 | + // @codingStandardsIgnoreStart |
|
| 6416 | + 'Cannot exclude protected data because the model has not specified which model has the password.', |
|
| 6417 | + // @codingStandardsIgnoreEnd |
|
| 6418 | + '1: model name', |
|
| 6419 | + 'event_espresso' |
|
| 6420 | + ) |
|
| 6421 | + ); |
|
| 6422 | + } |
|
| 6423 | + if ($this->model_chain_to_password === '') { |
|
| 6424 | + $model_with_password = $this; |
|
| 6425 | + } else { |
|
| 6426 | + if ($pos_of_period = strrpos($this->model_chain_to_password, '.')) { |
|
| 6427 | + $last_model_in_chain = substr($this->model_chain_to_password, $pos_of_period + 1); |
|
| 6428 | + } else { |
|
| 6429 | + $last_model_in_chain = $this->model_chain_to_password; |
|
| 6430 | + } |
|
| 6431 | + $model_with_password = EE_Registry::instance()->load_model($last_model_in_chain); |
|
| 6432 | + } |
|
| 6433 | + |
|
| 6434 | + $password_field = $model_with_password->getPasswordField(); |
|
| 6435 | + if ($password_field instanceof EE_Password_Field) { |
|
| 6436 | + $password_field_name = $password_field->get_name(); |
|
| 6437 | + } else { |
|
| 6438 | + throw new ModelConfigurationException( |
|
| 6439 | + $this, |
|
| 6440 | + sprintf( |
|
| 6441 | + esc_html_x( |
|
| 6442 | + 'This model claims related model "%1$s" should have a password field on it, but none was found. The model relation chain is "%2$s"', |
|
| 6443 | + '1: model name, 2: special string', |
|
| 6444 | + 'event_espresso' |
|
| 6445 | + ), |
|
| 6446 | + $model_with_password->get_this_model_name(), |
|
| 6447 | + $this->model_chain_to_password |
|
| 6448 | + ) |
|
| 6449 | + ); |
|
| 6450 | + } |
|
| 6451 | + return ($this->model_chain_to_password ? $this->model_chain_to_password . '.' : '') . $password_field_name; |
|
| 6452 | + } |
|
| 6453 | + |
|
| 6454 | + /** |
|
| 6455 | + * Returns true if there is a password on a related model which restricts access to some of this model's rows, |
|
| 6456 | + * or if this model itself has a password affecting access to some of its other fields. |
|
| 6457 | + * @since 4.9.74.p |
|
| 6458 | + * @return boolean |
|
| 6459 | + */ |
|
| 6460 | + public function restrictedByRelatedModelPassword() |
|
| 6461 | + { |
|
| 6462 | + return $this->model_chain_to_password !== null; |
|
| 6463 | + } |
|
| 6464 | 6464 | } |
@@ -65,18 +65,18 @@ discard block |
||
| 65 | 65 | // remember the model relation chain to the JOIN model, because we'll |
| 66 | 66 | // need it for get_join_statement() |
| 67 | 67 | $this->_model_relation_chain_to_join_model = $model_relation_chain; |
| 68 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field();// get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 68 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); // get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 69 | 69 | $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
| 70 | 70 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
| 71 | 71 | $model_relation_chain, |
| 72 | 72 | $this->get_this_model()->get_this_model_name() |
| 73 | - ) . $this_table_pk_field->get_table_alias(); |
|
| 73 | + ).$this_table_pk_field->get_table_alias(); |
|
| 74 | 74 | |
| 75 | 75 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
| 76 | 76 | $model_relation_chain, |
| 77 | 77 | $this->get_join_model()->get_this_model_name() |
| 78 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 79 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
| 78 | + ).$join_table_fk_field_to_this_table->get_table_alias(); |
|
| 79 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
| 80 | 80 | // phew! ok, we have all the info we need, now we can create the SQL join string |
| 81 | 81 | $SQL = $this->_left_join( |
| 82 | 82 | $join_table, |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | $join_table_fk_field_to_this_table->get_table_column(), |
| 85 | 85 | $this_table_alias, |
| 86 | 86 | $this_table_pk_field->get_table_column() |
| 87 | - ) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
| 87 | + ).$this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
| 88 | 88 | |
| 89 | 89 | return $SQL; |
| 90 | 90 | } |
@@ -113,14 +113,14 @@ discard block |
||
| 113 | 113 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
| 114 | 114 | $this->_model_relation_chain_to_join_model, |
| 115 | 115 | $this->get_join_model()->get_this_model_name() |
| 116 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 116 | + ).$join_table_fk_field_to_this_table->get_table_alias(); |
|
| 117 | 117 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
| 118 | 118 | $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
| 119 | 119 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
| 120 | 120 | $model_relation_chain, |
| 121 | 121 | $this->get_other_model()->get_this_model_name() |
| 122 | - ) . $other_table_pk_field->get_table_alias(); |
|
| 123 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 122 | + ).$other_table_pk_field->get_table_alias(); |
|
| 123 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 124 | 124 | |
| 125 | 125 | $SQL = $this->_left_join( |
| 126 | 126 | $other_table, |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | $other_table_pk_field->get_table_column(), |
| 129 | 129 | $join_table_alias, |
| 130 | 130 | $join_table_fk_field_to_other_table->get_table_column() |
| 131 | - ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 131 | + ).$this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 132 | 132 | return $SQL; |
| 133 | 133 | } |
| 134 | 134 | |
@@ -161,17 +161,17 @@ discard block |
||
| 161 | 161 | ); |
| 162 | 162 | |
| 163 | 163 | // if $where_query exists lets add them to the query_params. |
| 164 | - if (! empty($extra_join_model_fields_n_values)) { |
|
| 164 | + if ( ! empty($extra_join_model_fields_n_values)) { |
|
| 165 | 165 | // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
| 166 | 166 | // make sure we strip THIS models name from the query param |
| 167 | 167 | $parsed_query = array(); |
| 168 | 168 | foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
| 169 | - $query_param = str_replace( |
|
| 170 | - $this->get_join_model()->get_this_model_name() . ".", |
|
| 169 | + $query_param = str_replace( |
|
| 170 | + $this->get_join_model()->get_this_model_name().".", |
|
| 171 | 171 | "", |
| 172 | 172 | $query_param |
| 173 | 173 | ); |
| 174 | - $parsed_query[ $query_param ] = $val; |
|
| 174 | + $parsed_query[$query_param] = $val; |
|
| 175 | 175 | } |
| 176 | 176 | $all_fields = array_merge($foreign_keys, $parsed_query); |
| 177 | 177 | } |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | // new row. |
| 182 | 182 | // Again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
| 183 | 183 | // the other tables) use the joining model directly! |
| 184 | - if (! $existing_entry_in_join_table) { |
|
| 184 | + if ( ! $existing_entry_in_join_table) { |
|
| 185 | 185 | $this->get_join_model()->insert($all_fields); |
| 186 | 186 | } |
| 187 | 187 | return $other_model_obj; |
@@ -212,17 +212,17 @@ discard block |
||
| 212 | 212 | ); |
| 213 | 213 | |
| 214 | 214 | // if $where_query exists lets add them to the query_params. |
| 215 | - if (! empty($where_query)) { |
|
| 215 | + if ( ! empty($where_query)) { |
|
| 216 | 216 | // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
| 217 | 217 | // make sure we strip THIS models name from the query param |
| 218 | 218 | $parsed_query = array(); |
| 219 | 219 | foreach ($where_query as $query_param => $val) { |
| 220 | - $query_param = str_replace( |
|
| 221 | - $this->get_join_model()->get_this_model_name() . ".", |
|
| 220 | + $query_param = str_replace( |
|
| 221 | + $this->get_join_model()->get_this_model_name().".", |
|
| 222 | 222 | "", |
| 223 | 223 | $query_param |
| 224 | 224 | ); |
| 225 | - $parsed_query[ $query_param ] = $val; |
|
| 225 | + $parsed_query[$query_param] = $val; |
|
| 226 | 226 | } |
| 227 | 227 | $cols_n_values = array_merge($cols_n_values, $parsed_query); |
| 228 | 228 | } |
@@ -9,261 +9,261 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class EE_HABTM_Relation extends EE_Model_Relation_Base |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Model which defines the relation between two other models. Eg, the EE_Event_Question_Group model, |
|
| 14 | - * which joins EE_Event and EE_Question_Group |
|
| 15 | - * |
|
| 16 | - * @var EEM_Base |
|
| 17 | - */ |
|
| 18 | - protected $_joining_model_name; |
|
| 12 | + /** |
|
| 13 | + * Model which defines the relation between two other models. Eg, the EE_Event_Question_Group model, |
|
| 14 | + * which joins EE_Event and EE_Question_Group |
|
| 15 | + * |
|
| 16 | + * @var EEM_Base |
|
| 17 | + */ |
|
| 18 | + protected $_joining_model_name; |
|
| 19 | 19 | |
| 20 | - protected $_model_relation_chain_to_join_model; |
|
| 20 | + protected $_model_relation_chain_to_join_model; |
|
| 21 | 21 | |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
| 25 | - * (and an ee joining-model.) This knows how to join the models, |
|
| 26 | - * get related models across the relation, and add-and-remove the relationships. |
|
| 27 | - * |
|
| 28 | - * @param bool $joining_model_name |
|
| 29 | - * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
| 30 | - * are related models across this relation, block (prevent and add an |
|
| 31 | - * error) the deletion of this model |
|
| 32 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
| 33 | - * default |
|
| 34 | - */ |
|
| 35 | - public function __construct($joining_model_name, $block_deletes = true, $blocking_delete_error_message = '') |
|
| 36 | - { |
|
| 37 | - $this->_joining_model_name = $joining_model_name; |
|
| 38 | - parent::__construct($block_deletes, $blocking_delete_error_message); |
|
| 39 | - } |
|
| 23 | + /** |
|
| 24 | + * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
| 25 | + * (and an ee joining-model.) This knows how to join the models, |
|
| 26 | + * get related models across the relation, and add-and-remove the relationships. |
|
| 27 | + * |
|
| 28 | + * @param bool $joining_model_name |
|
| 29 | + * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
| 30 | + * are related models across this relation, block (prevent and add an |
|
| 31 | + * error) the deletion of this model |
|
| 32 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
| 33 | + * default |
|
| 34 | + */ |
|
| 35 | + public function __construct($joining_model_name, $block_deletes = true, $blocking_delete_error_message = '') |
|
| 36 | + { |
|
| 37 | + $this->_joining_model_name = $joining_model_name; |
|
| 38 | + parent::__construct($block_deletes, $blocking_delete_error_message); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Gets the joining model's object |
|
| 43 | - * |
|
| 44 | - * @return EEM_Base |
|
| 45 | - */ |
|
| 46 | - public function get_join_model() |
|
| 47 | - { |
|
| 48 | - return $this->_get_model($this->_joining_model_name); |
|
| 49 | - } |
|
| 41 | + /** |
|
| 42 | + * Gets the joining model's object |
|
| 43 | + * |
|
| 44 | + * @return EEM_Base |
|
| 45 | + */ |
|
| 46 | + public function get_join_model() |
|
| 47 | + { |
|
| 48 | + return $this->_get_model($this->_joining_model_name); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
| 54 | - * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
| 55 | - * |
|
| 56 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 57 | - * @return string of SQL |
|
| 58 | - * @throws \EE_Error |
|
| 59 | - */ |
|
| 60 | - public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
| 61 | - { |
|
| 62 | - // create sql like |
|
| 63 | - // LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
| 64 | - // LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
| 65 | - // remember the model relation chain to the JOIN model, because we'll |
|
| 66 | - // need it for get_join_statement() |
|
| 67 | - $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
| 68 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field();// get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 69 | - $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 70 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 71 | - $model_relation_chain, |
|
| 72 | - $this->get_this_model()->get_this_model_name() |
|
| 73 | - ) . $this_table_pk_field->get_table_alias(); |
|
| 52 | + /** |
|
| 53 | + * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
| 54 | + * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
| 55 | + * |
|
| 56 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 57 | + * @return string of SQL |
|
| 58 | + * @throws \EE_Error |
|
| 59 | + */ |
|
| 60 | + public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
| 61 | + { |
|
| 62 | + // create sql like |
|
| 63 | + // LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
| 64 | + // LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
| 65 | + // remember the model relation chain to the JOIN model, because we'll |
|
| 66 | + // need it for get_join_statement() |
|
| 67 | + $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
| 68 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field();// get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 69 | + $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 70 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 71 | + $model_relation_chain, |
|
| 72 | + $this->get_this_model()->get_this_model_name() |
|
| 73 | + ) . $this_table_pk_field->get_table_alias(); |
|
| 74 | 74 | |
| 75 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 76 | - $model_relation_chain, |
|
| 77 | - $this->get_join_model()->get_this_model_name() |
|
| 78 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 79 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
| 80 | - // phew! ok, we have all the info we need, now we can create the SQL join string |
|
| 81 | - $SQL = $this->_left_join( |
|
| 82 | - $join_table, |
|
| 83 | - $join_table_alias, |
|
| 84 | - $join_table_fk_field_to_this_table->get_table_column(), |
|
| 85 | - $this_table_alias, |
|
| 86 | - $this_table_pk_field->get_table_column() |
|
| 87 | - ) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
| 75 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 76 | + $model_relation_chain, |
|
| 77 | + $this->get_join_model()->get_this_model_name() |
|
| 78 | + ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 79 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
| 80 | + // phew! ok, we have all the info we need, now we can create the SQL join string |
|
| 81 | + $SQL = $this->_left_join( |
|
| 82 | + $join_table, |
|
| 83 | + $join_table_alias, |
|
| 84 | + $join_table_fk_field_to_this_table->get_table_column(), |
|
| 85 | + $this_table_alias, |
|
| 86 | + $this_table_pk_field->get_table_column() |
|
| 87 | + ) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
| 88 | 88 | |
| 89 | - return $SQL; |
|
| 90 | - } |
|
| 89 | + return $SQL; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
| 95 | - * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
| 96 | - * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
| 97 | - * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
| 98 | - * the intermediate join table, and then to the other model's pk's table |
|
| 99 | - * |
|
| 100 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 101 | - * @return string of SQL |
|
| 102 | - * @throws \EE_Error |
|
| 103 | - */ |
|
| 104 | - public function get_join_statement($model_relation_chain) |
|
| 105 | - { |
|
| 106 | - if ($this->_model_relation_chain_to_join_model === null) { |
|
| 107 | - throw new EE_Error(sprintf(__( |
|
| 108 | - 'When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
| 109 | - 'event_espresso' |
|
| 110 | - ))); |
|
| 111 | - } |
|
| 112 | - $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 113 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 114 | - $this->_model_relation_chain_to_join_model, |
|
| 115 | - $this->get_join_model()->get_this_model_name() |
|
| 116 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 117 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
| 118 | - $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 119 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 120 | - $model_relation_chain, |
|
| 121 | - $this->get_other_model()->get_this_model_name() |
|
| 122 | - ) . $other_table_pk_field->get_table_alias(); |
|
| 123 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 93 | + /** |
|
| 94 | + * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
| 95 | + * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
| 96 | + * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
| 97 | + * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
| 98 | + * the intermediate join table, and then to the other model's pk's table |
|
| 99 | + * |
|
| 100 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 101 | + * @return string of SQL |
|
| 102 | + * @throws \EE_Error |
|
| 103 | + */ |
|
| 104 | + public function get_join_statement($model_relation_chain) |
|
| 105 | + { |
|
| 106 | + if ($this->_model_relation_chain_to_join_model === null) { |
|
| 107 | + throw new EE_Error(sprintf(__( |
|
| 108 | + 'When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
| 109 | + 'event_espresso' |
|
| 110 | + ))); |
|
| 111 | + } |
|
| 112 | + $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 113 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 114 | + $this->_model_relation_chain_to_join_model, |
|
| 115 | + $this->get_join_model()->get_this_model_name() |
|
| 116 | + ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 117 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
| 118 | + $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 119 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 120 | + $model_relation_chain, |
|
| 121 | + $this->get_other_model()->get_this_model_name() |
|
| 122 | + ) . $other_table_pk_field->get_table_alias(); |
|
| 123 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 124 | 124 | |
| 125 | - $SQL = $this->_left_join( |
|
| 126 | - $other_table, |
|
| 127 | - $other_table_alias, |
|
| 128 | - $other_table_pk_field->get_table_column(), |
|
| 129 | - $join_table_alias, |
|
| 130 | - $join_table_fk_field_to_other_table->get_table_column() |
|
| 131 | - ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 132 | - return $SQL; |
|
| 133 | - } |
|
| 125 | + $SQL = $this->_left_join( |
|
| 126 | + $other_table, |
|
| 127 | + $other_table_alias, |
|
| 128 | + $other_table_pk_field->get_table_column(), |
|
| 129 | + $join_table_alias, |
|
| 130 | + $join_table_fk_field_to_other_table->get_table_column() |
|
| 131 | + ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 132 | + return $SQL; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
| 138 | - * If the join table has additional columns (eg, the Event_Question_Group table has a is_primary column), then |
|
| 139 | - * you'll want to directly use the EEM_Event_Question_Group model to add the entry to the table and set those extra |
|
| 140 | - * columns' values |
|
| 141 | - * |
|
| 142 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 143 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 144 | - * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
| 145 | - * checking existing values and for setting new rows if |
|
| 146 | - * no exact matches. |
|
| 147 | - * @return EE_Base_Class |
|
| 148 | - * @throws \EE_Error |
|
| 149 | - */ |
|
| 150 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 151 | - { |
|
| 152 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 153 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 154 | - // check if such a relationship already exists |
|
| 155 | - $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 156 | - $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 136 | + /** |
|
| 137 | + * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
| 138 | + * If the join table has additional columns (eg, the Event_Question_Group table has a is_primary column), then |
|
| 139 | + * you'll want to directly use the EEM_Event_Question_Group model to add the entry to the table and set those extra |
|
| 140 | + * columns' values |
|
| 141 | + * |
|
| 142 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 143 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 144 | + * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
| 145 | + * checking existing values and for setting new rows if |
|
| 146 | + * no exact matches. |
|
| 147 | + * @return EE_Base_Class |
|
| 148 | + * @throws \EE_Error |
|
| 149 | + */ |
|
| 150 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 151 | + { |
|
| 152 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 153 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 154 | + // check if such a relationship already exists |
|
| 155 | + $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 156 | + $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 157 | 157 | |
| 158 | - $foreign_keys = $all_fields = array( |
|
| 159 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 160 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 161 | - ); |
|
| 158 | + $foreign_keys = $all_fields = array( |
|
| 159 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 160 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 161 | + ); |
|
| 162 | 162 | |
| 163 | - // if $where_query exists lets add them to the query_params. |
|
| 164 | - if (! empty($extra_join_model_fields_n_values)) { |
|
| 165 | - // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 166 | - // make sure we strip THIS models name from the query param |
|
| 167 | - $parsed_query = array(); |
|
| 168 | - foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
| 169 | - $query_param = str_replace( |
|
| 170 | - $this->get_join_model()->get_this_model_name() . ".", |
|
| 171 | - "", |
|
| 172 | - $query_param |
|
| 173 | - ); |
|
| 174 | - $parsed_query[ $query_param ] = $val; |
|
| 175 | - } |
|
| 176 | - $all_fields = array_merge($foreign_keys, $parsed_query); |
|
| 177 | - } |
|
| 163 | + // if $where_query exists lets add them to the query_params. |
|
| 164 | + if (! empty($extra_join_model_fields_n_values)) { |
|
| 165 | + // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 166 | + // make sure we strip THIS models name from the query param |
|
| 167 | + $parsed_query = array(); |
|
| 168 | + foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
| 169 | + $query_param = str_replace( |
|
| 170 | + $this->get_join_model()->get_this_model_name() . ".", |
|
| 171 | + "", |
|
| 172 | + $query_param |
|
| 173 | + ); |
|
| 174 | + $parsed_query[ $query_param ] = $val; |
|
| 175 | + } |
|
| 176 | + $all_fields = array_merge($foreign_keys, $parsed_query); |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - $existing_entry_in_join_table = $this->get_join_model()->get_one(array($all_fields)); |
|
| 180 | - // If there is already an entry in the join table, indicating a relationship, update it instead of adding a |
|
| 181 | - // new row. |
|
| 182 | - // Again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
| 183 | - // the other tables) use the joining model directly! |
|
| 184 | - if (! $existing_entry_in_join_table) { |
|
| 185 | - $this->get_join_model()->insert($all_fields); |
|
| 186 | - } |
|
| 187 | - return $other_model_obj; |
|
| 188 | - } |
|
| 179 | + $existing_entry_in_join_table = $this->get_join_model()->get_one(array($all_fields)); |
|
| 180 | + // If there is already an entry in the join table, indicating a relationship, update it instead of adding a |
|
| 181 | + // new row. |
|
| 182 | + // Again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
| 183 | + // the other tables) use the joining model directly! |
|
| 184 | + if (! $existing_entry_in_join_table) { |
|
| 185 | + $this->get_join_model()->insert($all_fields); |
|
| 186 | + } |
|
| 187 | + return $other_model_obj; |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | 190 | |
| 191 | - /** |
|
| 192 | - * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
| 193 | - * |
|
| 194 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 195 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 196 | - * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
| 197 | - * values and for removing existing rows if exact matches exist. |
|
| 198 | - * @return EE_Base_Class |
|
| 199 | - * @throws \EE_Error |
|
| 200 | - */ |
|
| 201 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 202 | - { |
|
| 203 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 204 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 205 | - // check if such a relationship already exists |
|
| 206 | - $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 207 | - $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 191 | + /** |
|
| 192 | + * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
| 193 | + * |
|
| 194 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 195 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 196 | + * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
| 197 | + * values and for removing existing rows if exact matches exist. |
|
| 198 | + * @return EE_Base_Class |
|
| 199 | + * @throws \EE_Error |
|
| 200 | + */ |
|
| 201 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 202 | + { |
|
| 203 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 204 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 205 | + // check if such a relationship already exists |
|
| 206 | + $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 207 | + $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 208 | 208 | |
| 209 | - $cols_n_values = array( |
|
| 210 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 211 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 212 | - ); |
|
| 209 | + $cols_n_values = array( |
|
| 210 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 211 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 212 | + ); |
|
| 213 | 213 | |
| 214 | - // if $where_query exists lets add them to the query_params. |
|
| 215 | - if (! empty($where_query)) { |
|
| 216 | - // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 217 | - // make sure we strip THIS models name from the query param |
|
| 218 | - $parsed_query = array(); |
|
| 219 | - foreach ($where_query as $query_param => $val) { |
|
| 220 | - $query_param = str_replace( |
|
| 221 | - $this->get_join_model()->get_this_model_name() . ".", |
|
| 222 | - "", |
|
| 223 | - $query_param |
|
| 224 | - ); |
|
| 225 | - $parsed_query[ $query_param ] = $val; |
|
| 226 | - } |
|
| 227 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
| 228 | - } |
|
| 214 | + // if $where_query exists lets add them to the query_params. |
|
| 215 | + if (! empty($where_query)) { |
|
| 216 | + // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 217 | + // make sure we strip THIS models name from the query param |
|
| 218 | + $parsed_query = array(); |
|
| 219 | + foreach ($where_query as $query_param => $val) { |
|
| 220 | + $query_param = str_replace( |
|
| 221 | + $this->get_join_model()->get_this_model_name() . ".", |
|
| 222 | + "", |
|
| 223 | + $query_param |
|
| 224 | + ); |
|
| 225 | + $parsed_query[ $query_param ] = $val; |
|
| 226 | + } |
|
| 227 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - $this->get_join_model()->delete(array($cols_n_values)); |
|
| 231 | - return $other_model_obj; |
|
| 232 | - } |
|
| 230 | + $this->get_join_model()->delete(array($cols_n_values)); |
|
| 231 | + return $other_model_obj; |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - /** |
|
| 235 | - * Gets all the non-key fields (ie, not the primary key and not foreign keys) on the join model. |
|
| 236 | - * @since 4.9.76.p |
|
| 237 | - * @return EE_Model_Field_Base[] |
|
| 238 | - * @throws EE_Error |
|
| 239 | - */ |
|
| 240 | - public function getNonKeyFields() |
|
| 241 | - { |
|
| 242 | - // all fields besides the primary key and two foreign keys should be parameters |
|
| 243 | - $join_model = $this->get_join_model(); |
|
| 244 | - $standard_fields = array(); |
|
| 245 | - if ($join_model->has_primary_key_field()) { |
|
| 246 | - $standard_fields[] = $join_model->primary_key_name(); |
|
| 247 | - } |
|
| 248 | - if ($this->get_this_model()->has_primary_key_field()) { |
|
| 249 | - $standard_fields[] = $this->get_this_model()->primary_key_name(); |
|
| 250 | - } |
|
| 251 | - if ($this->get_other_model()->has_primary_key_field()) { |
|
| 252 | - $standard_fields[] = $this->get_other_model()->primary_key_name(); |
|
| 253 | - } |
|
| 254 | - return array_diff_key( |
|
| 255 | - $join_model->field_settings(), |
|
| 256 | - array_flip($standard_fields) |
|
| 257 | - ); |
|
| 258 | - } |
|
| 234 | + /** |
|
| 235 | + * Gets all the non-key fields (ie, not the primary key and not foreign keys) on the join model. |
|
| 236 | + * @since 4.9.76.p |
|
| 237 | + * @return EE_Model_Field_Base[] |
|
| 238 | + * @throws EE_Error |
|
| 239 | + */ |
|
| 240 | + public function getNonKeyFields() |
|
| 241 | + { |
|
| 242 | + // all fields besides the primary key and two foreign keys should be parameters |
|
| 243 | + $join_model = $this->get_join_model(); |
|
| 244 | + $standard_fields = array(); |
|
| 245 | + if ($join_model->has_primary_key_field()) { |
|
| 246 | + $standard_fields[] = $join_model->primary_key_name(); |
|
| 247 | + } |
|
| 248 | + if ($this->get_this_model()->has_primary_key_field()) { |
|
| 249 | + $standard_fields[] = $this->get_this_model()->primary_key_name(); |
|
| 250 | + } |
|
| 251 | + if ($this->get_other_model()->has_primary_key_field()) { |
|
| 252 | + $standard_fields[] = $this->get_other_model()->primary_key_name(); |
|
| 253 | + } |
|
| 254 | + return array_diff_key( |
|
| 255 | + $join_model->field_settings(), |
|
| 256 | + array_flip($standard_fields) |
|
| 257 | + ); |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - /** |
|
| 261 | - * Returns true if the join model has non-key fields (ie, fields that aren't the primary key or foreign keys.) |
|
| 262 | - * @since 4.9.76.p |
|
| 263 | - * @return boolean |
|
| 264 | - */ |
|
| 265 | - public function hasNonKeyFields() |
|
| 266 | - { |
|
| 267 | - return count($this->get_join_model()->field_settings()) > 3; |
|
| 268 | - } |
|
| 260 | + /** |
|
| 261 | + * Returns true if the join model has non-key fields (ie, fields that aren't the primary key or foreign keys.) |
|
| 262 | + * @since 4.9.76.p |
|
| 263 | + * @return boolean |
|
| 264 | + */ |
|
| 265 | + public function hasNonKeyFields() |
|
| 266 | + { |
|
| 267 | + return count($this->get_join_model()->field_settings()) > 3; |
|
| 268 | + } |
|
| 269 | 269 | } |