@@ -1,39 +1,39 @@ discard block |
||
| 1 | 1 | <div id="ee-single-page-checkout-dv" class=""> |
| 2 | 2 | <?php |
| 3 | -if ( ! $empty_cart ) { |
|
| 4 | - if ( apply_filters( 'FHEE__registration_page_wrapper_template__display_time_limit', false ) ) { ?> |
|
| 3 | +if ( ! $empty_cart) { |
|
| 4 | + if (apply_filters('FHEE__registration_page_wrapper_template__display_time_limit', false)) { ?> |
|
| 5 | 5 | <p id="spco-registration-time-limit-pg" class="spco-steps-pg ee-attention important-notice" style="display: none;"> |
| 6 | 6 | <?php echo sprintf( |
| 7 | 7 | apply_filters( |
| 8 | 8 | 'FHEE__registration_page_wrapper_template___time_limit', |
| 9 | - __( 'You have %1$s to complete your registration.', 'event_espresso' ) |
|
| 9 | + __('You have %1$s to complete your registration.', 'event_espresso') |
|
| 10 | 10 | ), |
| 11 | - '<span id="spco-registration-time-limit-spn" class="spco-registration-time-limit-spn">' . $registration_time_limit . '</span>' |
|
| 11 | + '<span id="spco-registration-time-limit-spn" class="spco-registration-time-limit-spn">'.$registration_time_limit.'</span>' |
|
| 12 | 12 | ); |
| 13 | 13 | ?> |
| 14 | 14 | <span id="spco-registration-expiration-spn" class="" style="display:none;"></span> |
| 15 | 15 | </p> |
| 16 | 16 | <?php } |
| 17 | - if ( ! $revisit && apply_filters( 'FHEE__registration_page_wrapper_template__steps_display', TRUE )) { |
|
| 17 | + if ( ! $revisit && apply_filters('FHEE__registration_page_wrapper_template__steps_display', TRUE)) { |
|
| 18 | 18 | ?> |
| 19 | 19 | <h2 id="spco-steps-big-hdr" class="spco-steps-big-hdr"><?php _e(' Steps', 'event_espresso'); ?></h2> |
| 20 | 20 | |
| 21 | 21 | <div id="spco-steps-display-dv"> |
| 22 | 22 | <?php |
| 23 | 23 | $step_nmbr = 1; |
| 24 | - $total_steps = count( $reg_steps ) - 1; |
|
| 25 | - foreach ( $reg_steps as $reg_step ) { |
|
| 26 | - if ( $reg_step instanceof EE_SPCO_Reg_Step && $reg_step->slug() != 'finalize_registration' ) { |
|
| 24 | + $total_steps = count($reg_steps) - 1; |
|
| 25 | + foreach ($reg_steps as $reg_step) { |
|
| 26 | + if ($reg_step instanceof EE_SPCO_Reg_Step && $reg_step->slug() != 'finalize_registration') { |
|
| 27 | 27 | $slug = $reg_step->slug(); |
| 28 | 28 | $step_display_dv_class = $reg_step->is_current_step() ? 'active-step' : 'inactive-step'; |
| 29 | 29 | ?> |
| 30 | 30 | <div id="spco-step-<?php echo $slug; ?>-display-dv" class="spco-step-display-dv <?php echo $step_display_dv_class; ?> steps-<?php echo $total_steps; ?>"> |
| 31 | 31 | <h4 id="spco-step-<?php echo $slug; ?>-display-hdr" class="spco-steps-display-hdr"> |
| 32 | - <span class="spco-step-big-nmbr"><?php echo $step_nmbr; ?></span> <span class="spco-step-name"><?php echo str_replace( ' ', '<br/> ', $reg_step->name() ); ?></span> |
|
| 32 | + <span class="spco-step-big-nmbr"><?php echo $step_nmbr; ?></span> <span class="spco-step-name"><?php echo str_replace(' ', '<br/> ', $reg_step->name()); ?></span> |
|
| 33 | 33 | </h4> |
| 34 | 34 | </div> |
| 35 | 35 | |
| 36 | - <?php if ( $step_nmbr < $total_steps ) { ?> |
|
| 36 | + <?php if ($step_nmbr < $total_steps) { ?> |
|
| 37 | 37 | <div class="spco-step-arrow-dv">»</div> |
| 38 | 38 | <?php |
| 39 | 39 | } |
@@ -47,22 +47,22 @@ discard block |
||
| 47 | 47 | <?php |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - do_action( 'AHEE__SPCO__before_registration_steps' ); |
|
| 50 | + do_action('AHEE__SPCO__before_registration_steps'); |
|
| 51 | 51 | $step_nmbr = 1; |
| 52 | - foreach ( $reg_steps as $reg_step ) { |
|
| 53 | - if ( $reg_step instanceof EE_SPCO_Reg_Step && $reg_step->slug() != 'finalize_registration' ) { |
|
| 52 | + foreach ($reg_steps as $reg_step) { |
|
| 53 | + if ($reg_step instanceof EE_SPCO_Reg_Step && $reg_step->slug() != 'finalize_registration') { |
|
| 54 | 54 | $slug = $reg_step->slug(); |
| 55 | - do_action( 'AHEE__' . $slug . '__reg_step_start', $reg_step ); |
|
| 55 | + do_action('AHEE__'.$slug.'__reg_step_start', $reg_step); |
|
| 56 | 56 | // todo: deprecate hook AHEE__registration_page_attendee_information__start |
| 57 | 57 | ?> |
| 58 | 58 | <div id="spco-<?php echo $slug; ?>-dv" class="spco-step-dv <?php echo $reg_step->div_class(); ?>"> |
| 59 | 59 | <?php echo $reg_step->display_reg_form(); ?> |
| 60 | - <?php do_action( 'AHEE__SPCO_after_reg_step_form', $slug, $next_step ); ?> |
|
| 60 | + <?php do_action('AHEE__SPCO_after_reg_step_form', $slug, $next_step); ?> |
|
| 61 | 61 | </div> |
| 62 | 62 | <?php $step_nmbr++; |
| 63 | 63 | } |
| 64 | 64 | } |
| 65 | - do_action( 'AHEE__SPCO__after_registration_steps' ); |
|
| 65 | + do_action('AHEE__SPCO__after_registration_steps'); |
|
| 66 | 66 | |
| 67 | 67 | } else { |
| 68 | 68 | ?> |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | <?php echo $cookies_not_set_msg; ?> |
| 72 | 72 | <?php |
| 73 | 73 | } |
| 74 | -do_action( 'AHEE__SPCO__reg_form_footer' ); |
|
| 74 | +do_action('AHEE__SPCO__reg_form_footer'); |
|
| 75 | 75 | ?> |
| 76 | 76 | |
| 77 | 77 | </div> |
@@ -603,7 +603,7 @@ discard block |
||
| 603 | 603 | /** |
| 604 | 604 | * returns any events attached to this attendee ($_Event property); |
| 605 | 605 | * |
| 606 | - * @return array |
|
| 606 | + * @return EE_Base_Class[] |
|
| 607 | 607 | * @throws EE_Error |
| 608 | 608 | */ |
| 609 | 609 | public function events() |
@@ -618,7 +618,7 @@ discard block |
||
| 618 | 618 | * used to save the billing info |
| 619 | 619 | * |
| 620 | 620 | * @param EE_Payment_Method $payment_method the _gateway_name property on the gateway class |
| 621 | - * @return EE_Form_Section_Proper|null |
|
| 621 | + * @return null|EE_Billing_Info_Form |
|
| 622 | 622 | * @throws EE_Error |
| 623 | 623 | */ |
| 624 | 624 | public function billing_info_for_payment_method($payment_method) |
@@ -27,734 +27,734 @@ |
||
| 27 | 27 | class EE_Attendee extends EE_CPT_Base implements EEI_Contact, EEI_Address, EEI_Admin_Links, EEI_Attendee |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Sets some dynamic defaults |
|
| 32 | - * |
|
| 33 | - * @param array $fieldValues |
|
| 34 | - * @param bool $bydb |
|
| 35 | - * @param string $timezone |
|
| 36 | - * @param array $date_formats |
|
| 37 | - * @throws EE_Error |
|
| 38 | - */ |
|
| 39 | - protected function __construct($fieldValues = null, $bydb = false, $timezone = null, $date_formats = array()) |
|
| 40 | - { |
|
| 41 | - if (! isset($fieldValues['ATT_full_name'])) { |
|
| 42 | - $fname = isset($fieldValues['ATT_fname']) ? $fieldValues['ATT_fname'] . ' ' : ''; |
|
| 43 | - $lname = isset($fieldValues['ATT_lname']) ? $fieldValues['ATT_lname'] : ''; |
|
| 44 | - $fieldValues['ATT_full_name'] = $fname . $lname; |
|
| 45 | - } |
|
| 46 | - if (! isset($fieldValues['ATT_slug'])) { |
|
| 47 | - // $fieldValues['ATT_slug'] = sanitize_key(wp_generate_password(20)); |
|
| 48 | - $fieldValues['ATT_slug'] = sanitize_title($fieldValues['ATT_full_name']); |
|
| 49 | - } |
|
| 50 | - if (! isset($fieldValues['ATT_short_bio']) && isset($fieldValues['ATT_bio'])) { |
|
| 51 | - $fieldValues['ATT_short_bio'] = substr($fieldValues['ATT_bio'], 0, 50); |
|
| 52 | - } |
|
| 53 | - parent::__construct($fieldValues, $bydb, $timezone, $date_formats); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param array $props_n_values incoming values |
|
| 59 | - * @param string $timezone incoming timezone (if not set the timezone set for the website will be |
|
| 60 | - * used.) |
|
| 61 | - * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 62 | - * date_format and the second value is the time format |
|
| 63 | - * @return EE_Attendee |
|
| 64 | - * @throws EE_Error |
|
| 65 | - */ |
|
| 66 | - public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array()) |
|
| 67 | - { |
|
| 68 | - $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats); |
|
| 69 | - return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @param array $props_n_values incoming values from the database |
|
| 75 | - * @param string $timezone incoming timezone as set by the model. If not set the timezone for |
|
| 76 | - * the website will be used. |
|
| 77 | - * @return EE_Attendee |
|
| 78 | - */ |
|
| 79 | - public static function new_instance_from_db($props_n_values = array(), $timezone = null) |
|
| 80 | - { |
|
| 81 | - return new self($props_n_values, true, $timezone); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Set Attendee First Name |
|
| 87 | - * |
|
| 88 | - * @access public |
|
| 89 | - * @param string $fname |
|
| 90 | - * @throws EE_Error |
|
| 91 | - */ |
|
| 92 | - public function set_fname($fname = '') |
|
| 93 | - { |
|
| 94 | - $this->set('ATT_fname', $fname); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Set Attendee Last Name |
|
| 100 | - * |
|
| 101 | - * @access public |
|
| 102 | - * @param string $lname |
|
| 103 | - * @throws EE_Error |
|
| 104 | - */ |
|
| 105 | - public function set_lname($lname = '') |
|
| 106 | - { |
|
| 107 | - $this->set('ATT_lname', $lname); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Set Attendee Address |
|
| 113 | - * |
|
| 114 | - * @access public |
|
| 115 | - * @param string $address |
|
| 116 | - * @throws EE_Error |
|
| 117 | - */ |
|
| 118 | - public function set_address($address = '') |
|
| 119 | - { |
|
| 120 | - $this->set('ATT_address', $address); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Set Attendee Address2 |
|
| 126 | - * |
|
| 127 | - * @access public |
|
| 128 | - * @param string $address2 |
|
| 129 | - * @throws EE_Error |
|
| 130 | - */ |
|
| 131 | - public function set_address2($address2 = '') |
|
| 132 | - { |
|
| 133 | - $this->set('ATT_address2', $address2); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Set Attendee City |
|
| 139 | - * |
|
| 140 | - * @access public |
|
| 141 | - * @param string $city |
|
| 142 | - * @throws EE_Error |
|
| 143 | - */ |
|
| 144 | - public function set_city($city = '') |
|
| 145 | - { |
|
| 146 | - $this->set('ATT_city', $city); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Set Attendee State ID |
|
| 152 | - * |
|
| 153 | - * @access public |
|
| 154 | - * @param int $STA_ID |
|
| 155 | - * @throws EE_Error |
|
| 156 | - */ |
|
| 157 | - public function set_state($STA_ID = 0) |
|
| 158 | - { |
|
| 159 | - $this->set('STA_ID', $STA_ID); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Set Attendee Country ISO Code |
|
| 165 | - * |
|
| 166 | - * @access public |
|
| 167 | - * @param string $CNT_ISO |
|
| 168 | - * @throws EE_Error |
|
| 169 | - */ |
|
| 170 | - public function set_country($CNT_ISO = '') |
|
| 171 | - { |
|
| 172 | - $this->set('CNT_ISO', $CNT_ISO); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Set Attendee Zip/Postal Code |
|
| 178 | - * |
|
| 179 | - * @access public |
|
| 180 | - * @param string $zip |
|
| 181 | - * @throws EE_Error |
|
| 182 | - */ |
|
| 183 | - public function set_zip($zip = '') |
|
| 184 | - { |
|
| 185 | - $this->set('ATT_zip', $zip); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Set Attendee Email Address |
|
| 191 | - * |
|
| 192 | - * @access public |
|
| 193 | - * @param string $email |
|
| 194 | - * @throws EE_Error |
|
| 195 | - */ |
|
| 196 | - public function set_email($email = '') |
|
| 197 | - { |
|
| 198 | - $this->set('ATT_email', $email); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Set Attendee Phone |
|
| 204 | - * |
|
| 205 | - * @access public |
|
| 206 | - * @param string $phone |
|
| 207 | - * @throws EE_Error |
|
| 208 | - */ |
|
| 209 | - public function set_phone($phone = '') |
|
| 210 | - { |
|
| 211 | - $this->set('ATT_phone', $phone); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * set deleted |
|
| 217 | - * |
|
| 218 | - * @access public |
|
| 219 | - * @param bool $ATT_deleted |
|
| 220 | - * @throws EE_Error |
|
| 221 | - */ |
|
| 222 | - public function set_deleted($ATT_deleted = false) |
|
| 223 | - { |
|
| 224 | - $this->set('ATT_deleted', $ATT_deleted); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Returns the value for the post_author id saved with the cpt |
|
| 230 | - * |
|
| 231 | - * @since 4.5.0 |
|
| 232 | - * @return int |
|
| 233 | - * @throws EE_Error |
|
| 234 | - */ |
|
| 235 | - public function wp_user() |
|
| 236 | - { |
|
| 237 | - return $this->get('ATT_author'); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * get Attendee First Name |
|
| 243 | - * |
|
| 244 | - * @access public |
|
| 245 | - * @return string |
|
| 246 | - * @throws EE_Error |
|
| 247 | - */ |
|
| 248 | - public function fname() |
|
| 249 | - { |
|
| 250 | - return $this->get('ATT_fname'); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * echoes out the attendee's first name |
|
| 256 | - * |
|
| 257 | - * @return void |
|
| 258 | - * @throws EE_Error |
|
| 259 | - */ |
|
| 260 | - public function e_full_name() |
|
| 261 | - { |
|
| 262 | - echo $this->full_name(); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Returns the first and last name concatenated together with a space. |
|
| 268 | - * |
|
| 269 | - * @param bool $apply_html_entities |
|
| 270 | - * @return string |
|
| 271 | - * @throws EE_Error |
|
| 272 | - */ |
|
| 273 | - public function full_name($apply_html_entities = false) |
|
| 274 | - { |
|
| 275 | - $full_name = array( |
|
| 276 | - $this->fname(), |
|
| 277 | - $this->lname(), |
|
| 278 | - ); |
|
| 279 | - $full_name = array_filter($full_name); |
|
| 280 | - $full_name = implode(' ', $full_name); |
|
| 281 | - return $apply_html_entities ? htmlentities($full_name, ENT_QUOTES, 'UTF-8') : $full_name; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * This returns the value of the `ATT_full_name` field which is usually equivalent to calling `full_name()` unless |
|
| 287 | - * the post_title field has been directly modified in the db for the post (espresso_attendees post type) for this |
|
| 288 | - * attendee. |
|
| 289 | - * |
|
| 290 | - * @param bool $apply_html_entities |
|
| 291 | - * @return string |
|
| 292 | - * @throws EE_Error |
|
| 293 | - */ |
|
| 294 | - public function ATT_full_name($apply_html_entities = false) |
|
| 295 | - { |
|
| 296 | - return $apply_html_entities |
|
| 297 | - ? htmlentities($this->get('ATT_full_name'), ENT_QUOTES, 'UTF-8') |
|
| 298 | - : $this->get('ATT_full_name'); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * get Attendee Last Name |
|
| 304 | - * |
|
| 305 | - * @access public |
|
| 306 | - * @return string |
|
| 307 | - * @throws EE_Error |
|
| 308 | - */ |
|
| 309 | - public function lname() |
|
| 310 | - { |
|
| 311 | - return $this->get('ATT_lname'); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Gets the attendee's full address as an array so client code can decide hwo to display it |
|
| 317 | - * |
|
| 318 | - * @return array numerically indexed, with each part of the address that is known. |
|
| 319 | - * Eg, if the user only responded to state and country, |
|
| 320 | - * it would be array(0=>'Alabama',1=>'USA') |
|
| 321 | - * @return array |
|
| 322 | - * @throws EE_Error |
|
| 323 | - */ |
|
| 324 | - public function full_address_as_array() |
|
| 325 | - { |
|
| 326 | - $full_address_array = array(); |
|
| 327 | - $initial_address_fields = array('ATT_address', 'ATT_address2', 'ATT_city',); |
|
| 328 | - foreach ($initial_address_fields as $address_field_name) { |
|
| 329 | - $address_fields_value = $this->get($address_field_name); |
|
| 330 | - if (! empty($address_fields_value)) { |
|
| 331 | - $full_address_array[] = $address_fields_value; |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - //now handle state and country |
|
| 335 | - $state_obj = $this->state_obj(); |
|
| 336 | - if ($state_obj instanceof EE_State) { |
|
| 337 | - $full_address_array[] = $state_obj->name(); |
|
| 338 | - } |
|
| 339 | - $country_obj = $this->country_obj(); |
|
| 340 | - if ($country_obj instanceof EE_Country) { |
|
| 341 | - $full_address_array[] = $country_obj->name(); |
|
| 342 | - } |
|
| 343 | - //lastly get the xip |
|
| 344 | - $zip_value = $this->zip(); |
|
| 345 | - if (! empty($zip_value)) { |
|
| 346 | - $full_address_array[] = $zip_value; |
|
| 347 | - } |
|
| 348 | - return $full_address_array; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * get Attendee Address |
|
| 354 | - * |
|
| 355 | - * @return string |
|
| 356 | - * @throws EE_Error |
|
| 357 | - */ |
|
| 358 | - public function address() |
|
| 359 | - { |
|
| 360 | - return $this->get('ATT_address'); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * get Attendee Address2 |
|
| 366 | - * |
|
| 367 | - * @return string |
|
| 368 | - * @throws EE_Error |
|
| 369 | - */ |
|
| 370 | - public function address2() |
|
| 371 | - { |
|
| 372 | - return $this->get('ATT_address2'); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * get Attendee City |
|
| 378 | - * |
|
| 379 | - * @return string |
|
| 380 | - * @throws EE_Error |
|
| 381 | - */ |
|
| 382 | - public function city() |
|
| 383 | - { |
|
| 384 | - return $this->get('ATT_city'); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * get Attendee State ID |
|
| 390 | - * |
|
| 391 | - * @return string |
|
| 392 | - * @throws EE_Error |
|
| 393 | - */ |
|
| 394 | - public function state_ID() |
|
| 395 | - { |
|
| 396 | - return $this->get('STA_ID'); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * @return string |
|
| 402 | - * @throws EE_Error |
|
| 403 | - */ |
|
| 404 | - public function state_abbrev() |
|
| 405 | - { |
|
| 406 | - return $this->state_obj() instanceof EE_State ? $this->state_obj()->abbrev() : ''; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Gets the state set to this attendee |
|
| 412 | - * |
|
| 413 | - * @return EE_State |
|
| 414 | - * @throws EE_Error |
|
| 415 | - */ |
|
| 416 | - public function state_obj() |
|
| 417 | - { |
|
| 418 | - return $this->get_first_related('State'); |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Returns the state's name, otherwise 'Unknown' |
|
| 424 | - * |
|
| 425 | - * @return string |
|
| 426 | - * @throws EE_Error |
|
| 427 | - */ |
|
| 428 | - public function state_name() |
|
| 429 | - { |
|
| 430 | - if ($this->state_obj()) { |
|
| 431 | - return $this->state_obj()->name(); |
|
| 432 | - } else { |
|
| 433 | - return ''; |
|
| 434 | - } |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * either displays the state abbreviation or the state name, as determined |
|
| 440 | - * by the "FHEE__EEI_Address__state__use_abbreviation" filter. |
|
| 441 | - * defaults to abbreviation |
|
| 442 | - * |
|
| 443 | - * @return string |
|
| 444 | - * @throws EE_Error |
|
| 445 | - */ |
|
| 446 | - public function state() |
|
| 447 | - { |
|
| 448 | - if (apply_filters('FHEE__EEI_Address__state__use_abbreviation', true, $this->state_obj())) { |
|
| 449 | - return $this->state_abbrev(); |
|
| 450 | - } |
|
| 451 | - return $this->state_name(); |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * get Attendee Country ISO Code |
|
| 457 | - * |
|
| 458 | - * @return string |
|
| 459 | - * @throws EE_Error |
|
| 460 | - */ |
|
| 461 | - public function country_ID() |
|
| 462 | - { |
|
| 463 | - return $this->get('CNT_ISO'); |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Gets country set for this attendee |
|
| 469 | - * |
|
| 470 | - * @return EE_Country |
|
| 471 | - * @throws EE_Error |
|
| 472 | - */ |
|
| 473 | - public function country_obj() |
|
| 474 | - { |
|
| 475 | - return $this->get_first_related('Country'); |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Returns the country's name if known, otherwise 'Unknown' |
|
| 481 | - * |
|
| 482 | - * @return string |
|
| 483 | - * @throws EE_Error |
|
| 484 | - */ |
|
| 485 | - public function country_name() |
|
| 486 | - { |
|
| 487 | - if ($this->country_obj()) { |
|
| 488 | - return $this->country_obj()->name(); |
|
| 489 | - } |
|
| 490 | - return ''; |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * either displays the country ISO2 code or the country name, as determined |
|
| 496 | - * by the "FHEE__EEI_Address__country__use_abbreviation" filter. |
|
| 497 | - * defaults to abbreviation |
|
| 498 | - * |
|
| 499 | - * @return string |
|
| 500 | - * @throws EE_Error |
|
| 501 | - */ |
|
| 502 | - public function country() |
|
| 503 | - { |
|
| 504 | - if (apply_filters('FHEE__EEI_Address__country__use_abbreviation', true, $this->country_obj())) { |
|
| 505 | - return $this->country_ID(); |
|
| 506 | - } |
|
| 507 | - return $this->country_name(); |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - |
|
| 511 | - /** |
|
| 512 | - * get Attendee Zip/Postal Code |
|
| 513 | - * |
|
| 514 | - * @return string |
|
| 515 | - * @throws EE_Error |
|
| 516 | - */ |
|
| 517 | - public function zip() |
|
| 518 | - { |
|
| 519 | - return $this->get('ATT_zip'); |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * get Attendee Email Address |
|
| 525 | - * |
|
| 526 | - * @return string |
|
| 527 | - * @throws EE_Error |
|
| 528 | - */ |
|
| 529 | - public function email() |
|
| 530 | - { |
|
| 531 | - return $this->get('ATT_email'); |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - |
|
| 535 | - /** |
|
| 536 | - * get Attendee Phone # |
|
| 537 | - * |
|
| 538 | - * @return string |
|
| 539 | - * @throws EE_Error |
|
| 540 | - */ |
|
| 541 | - public function phone() |
|
| 542 | - { |
|
| 543 | - return $this->get('ATT_phone'); |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - |
|
| 547 | - /** |
|
| 548 | - * get deleted |
|
| 549 | - * |
|
| 550 | - * @return bool |
|
| 551 | - * @throws EE_Error |
|
| 552 | - */ |
|
| 553 | - public function deleted() |
|
| 554 | - { |
|
| 555 | - return $this->get('ATT_deleted'); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - |
|
| 559 | - /** |
|
| 560 | - * Gets registrations of this attendee |
|
| 561 | - * |
|
| 562 | - * @param array $query_params |
|
| 563 | - * @return EE_Registration[] |
|
| 564 | - * @throws EE_Error |
|
| 565 | - */ |
|
| 566 | - public function get_registrations($query_params = array()) |
|
| 567 | - { |
|
| 568 | - return $this->get_many_related('Registration', $query_params); |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - |
|
| 572 | - /** |
|
| 573 | - * Gets the most recent registration of this attendee |
|
| 574 | - * |
|
| 575 | - * @return EE_Registration |
|
| 576 | - * @throws EE_Error |
|
| 577 | - */ |
|
| 578 | - public function get_most_recent_registration() |
|
| 579 | - { |
|
| 580 | - return $this->get_first_related( |
|
| 581 | - 'Registration', |
|
| 582 | - array('order_by' => array('REG_date' => 'DESC')) |
|
| 583 | - ); //null, 'REG_date', 'DESC', '=', 'OBJECT_K'); |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * Gets the most recent registration for this attend at this event |
|
| 589 | - * |
|
| 590 | - * @param int $event_id |
|
| 591 | - * @return EE_Registration |
|
| 592 | - * @throws EE_Error |
|
| 593 | - */ |
|
| 594 | - public function get_most_recent_registration_for_event($event_id) |
|
| 595 | - { |
|
| 596 | - return $this->get_first_related( |
|
| 597 | - 'Registration', |
|
| 598 | - array(array('EVT_ID' => $event_id), 'order_by' => array('REG_date' => 'DESC')) |
|
| 599 | - ); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - |
|
| 603 | - /** |
|
| 604 | - * returns any events attached to this attendee ($_Event property); |
|
| 605 | - * |
|
| 606 | - * @return array |
|
| 607 | - * @throws EE_Error |
|
| 608 | - */ |
|
| 609 | - public function events() |
|
| 610 | - { |
|
| 611 | - return $this->get_many_related('Event'); |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * Gets the billing info array where keys match espresso_reg_page_billing_inputs(), |
|
| 617 | - * and keys are their cleaned values. @see EE_Attendee::save_and_clean_billing_info_for_payment_method() which was |
|
| 618 | - * used to save the billing info |
|
| 619 | - * |
|
| 620 | - * @param EE_Payment_Method $payment_method the _gateway_name property on the gateway class |
|
| 621 | - * @return EE_Form_Section_Proper|null |
|
| 622 | - * @throws EE_Error |
|
| 623 | - */ |
|
| 624 | - public function billing_info_for_payment_method($payment_method) |
|
| 625 | - { |
|
| 626 | - $pm_type = $payment_method->type_obj(); |
|
| 627 | - if (! $pm_type instanceof EE_PMT_Base) { |
|
| 628 | - return null; |
|
| 629 | - } |
|
| 630 | - $billing_info = $this->get_post_meta($this->get_billing_info_postmeta_name($payment_method), true); |
|
| 631 | - if (! $billing_info) { |
|
| 632 | - return null; |
|
| 633 | - } |
|
| 634 | - $billing_form = $pm_type->billing_form(); |
|
| 635 | - if ($billing_form instanceof EE_Form_Section_Proper) { |
|
| 636 | - $billing_form->receive_form_submission(array($billing_form->name() => $billing_info), false); |
|
| 637 | - } |
|
| 638 | - return $billing_form; |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - |
|
| 642 | - /** |
|
| 643 | - * Gets the postmeta key that holds this attendee's billing info for the |
|
| 644 | - * specified payment method |
|
| 645 | - * |
|
| 646 | - * @param EE_Payment_Method $payment_method |
|
| 647 | - * @return string |
|
| 648 | - * @throws EE_Error |
|
| 649 | - */ |
|
| 650 | - public function get_billing_info_postmeta_name($payment_method) |
|
| 651 | - { |
|
| 652 | - if ($payment_method->type_obj() instanceof EE_PMT_Base) { |
|
| 653 | - return 'billing_info_' . $payment_method->type_obj()->system_name(); |
|
| 654 | - } |
|
| 655 | - return null; |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - |
|
| 659 | - /** |
|
| 660 | - * Saves the billing info to the attendee. @see EE_Attendee::billing_info_for_payment_method() which is used to |
|
| 661 | - * retrieve it |
|
| 662 | - * |
|
| 663 | - * @param EE_Billing_Attendee_Info_Form $billing_form |
|
| 664 | - * @param EE_Payment_Method $payment_method |
|
| 665 | - * @return boolean |
|
| 666 | - * @throws EE_Error |
|
| 667 | - */ |
|
| 668 | - public function save_and_clean_billing_info_for_payment_method($billing_form, $payment_method) |
|
| 669 | - { |
|
| 670 | - if (! $billing_form instanceof EE_Billing_Attendee_Info_Form) { |
|
| 671 | - EE_Error::add_error(__('Cannot save billing info because there is none.', 'event_espresso')); |
|
| 672 | - return false; |
|
| 673 | - } |
|
| 674 | - $billing_form->clean_sensitive_data(); |
|
| 675 | - return update_post_meta( |
|
| 676 | - $this->ID(), |
|
| 677 | - $this->get_billing_info_postmeta_name($payment_method), |
|
| 678 | - $billing_form->input_values(true) |
|
| 679 | - ); |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - |
|
| 683 | - /** |
|
| 684 | - * Return the link to the admin details for the object. |
|
| 685 | - * |
|
| 686 | - * @return string |
|
| 687 | - * @throws EE_Error |
|
| 688 | - * @throws InvalidArgumentException |
|
| 689 | - * @throws InvalidDataTypeException |
|
| 690 | - * @throws InvalidInterfaceException |
|
| 691 | - * @throws ReflectionException |
|
| 692 | - */ |
|
| 693 | - public function get_admin_details_link() |
|
| 694 | - { |
|
| 695 | - return $this->get_admin_edit_link(); |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - |
|
| 699 | - /** |
|
| 700 | - * Returns the link to the editor for the object. Sometimes this is the same as the details. |
|
| 701 | - * |
|
| 702 | - * @return string |
|
| 703 | - * @throws EE_Error |
|
| 704 | - * @throws InvalidArgumentException |
|
| 705 | - * @throws ReflectionException |
|
| 706 | - * @throws InvalidDataTypeException |
|
| 707 | - * @throws InvalidInterfaceException |
|
| 708 | - */ |
|
| 709 | - public function get_admin_edit_link() |
|
| 710 | - { |
|
| 711 | - EE_Registry::instance()->load_helper('URL'); |
|
| 712 | - return EEH_URL::add_query_args_and_nonce( |
|
| 713 | - array( |
|
| 714 | - 'page' => 'espresso_registrations', |
|
| 715 | - 'action' => 'edit_attendee', |
|
| 716 | - 'post' => $this->ID(), |
|
| 717 | - ), |
|
| 718 | - admin_url('admin.php') |
|
| 719 | - ); |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - |
|
| 723 | - /** |
|
| 724 | - * Returns the link to a settings page for the object. |
|
| 725 | - * |
|
| 726 | - * @return string |
|
| 727 | - * @throws EE_Error |
|
| 728 | - * @throws InvalidArgumentException |
|
| 729 | - * @throws InvalidDataTypeException |
|
| 730 | - * @throws InvalidInterfaceException |
|
| 731 | - * @throws ReflectionException |
|
| 732 | - */ |
|
| 733 | - public function get_admin_settings_link() |
|
| 734 | - { |
|
| 735 | - return $this->get_admin_edit_link(); |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - |
|
| 739 | - /** |
|
| 740 | - * Returns the link to the "overview" for the object (typically the "list table" view). |
|
| 741 | - * |
|
| 742 | - * @return string |
|
| 743 | - * @throws EE_Error |
|
| 744 | - * @throws InvalidArgumentException |
|
| 745 | - * @throws ReflectionException |
|
| 746 | - * @throws InvalidDataTypeException |
|
| 747 | - * @throws InvalidInterfaceException |
|
| 748 | - */ |
|
| 749 | - public function get_admin_overview_link() |
|
| 750 | - { |
|
| 751 | - EE_Registry::instance()->load_helper('URL'); |
|
| 752 | - return EEH_URL::add_query_args_and_nonce( |
|
| 753 | - array( |
|
| 754 | - 'page' => 'espresso_registrations', |
|
| 755 | - 'action' => 'contact_list', |
|
| 756 | - ), |
|
| 757 | - admin_url('admin.php') |
|
| 758 | - ); |
|
| 759 | - } |
|
| 30 | + /** |
|
| 31 | + * Sets some dynamic defaults |
|
| 32 | + * |
|
| 33 | + * @param array $fieldValues |
|
| 34 | + * @param bool $bydb |
|
| 35 | + * @param string $timezone |
|
| 36 | + * @param array $date_formats |
|
| 37 | + * @throws EE_Error |
|
| 38 | + */ |
|
| 39 | + protected function __construct($fieldValues = null, $bydb = false, $timezone = null, $date_formats = array()) |
|
| 40 | + { |
|
| 41 | + if (! isset($fieldValues['ATT_full_name'])) { |
|
| 42 | + $fname = isset($fieldValues['ATT_fname']) ? $fieldValues['ATT_fname'] . ' ' : ''; |
|
| 43 | + $lname = isset($fieldValues['ATT_lname']) ? $fieldValues['ATT_lname'] : ''; |
|
| 44 | + $fieldValues['ATT_full_name'] = $fname . $lname; |
|
| 45 | + } |
|
| 46 | + if (! isset($fieldValues['ATT_slug'])) { |
|
| 47 | + // $fieldValues['ATT_slug'] = sanitize_key(wp_generate_password(20)); |
|
| 48 | + $fieldValues['ATT_slug'] = sanitize_title($fieldValues['ATT_full_name']); |
|
| 49 | + } |
|
| 50 | + if (! isset($fieldValues['ATT_short_bio']) && isset($fieldValues['ATT_bio'])) { |
|
| 51 | + $fieldValues['ATT_short_bio'] = substr($fieldValues['ATT_bio'], 0, 50); |
|
| 52 | + } |
|
| 53 | + parent::__construct($fieldValues, $bydb, $timezone, $date_formats); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param array $props_n_values incoming values |
|
| 59 | + * @param string $timezone incoming timezone (if not set the timezone set for the website will be |
|
| 60 | + * used.) |
|
| 61 | + * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 62 | + * date_format and the second value is the time format |
|
| 63 | + * @return EE_Attendee |
|
| 64 | + * @throws EE_Error |
|
| 65 | + */ |
|
| 66 | + public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array()) |
|
| 67 | + { |
|
| 68 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats); |
|
| 69 | + return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @param array $props_n_values incoming values from the database |
|
| 75 | + * @param string $timezone incoming timezone as set by the model. If not set the timezone for |
|
| 76 | + * the website will be used. |
|
| 77 | + * @return EE_Attendee |
|
| 78 | + */ |
|
| 79 | + public static function new_instance_from_db($props_n_values = array(), $timezone = null) |
|
| 80 | + { |
|
| 81 | + return new self($props_n_values, true, $timezone); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Set Attendee First Name |
|
| 87 | + * |
|
| 88 | + * @access public |
|
| 89 | + * @param string $fname |
|
| 90 | + * @throws EE_Error |
|
| 91 | + */ |
|
| 92 | + public function set_fname($fname = '') |
|
| 93 | + { |
|
| 94 | + $this->set('ATT_fname', $fname); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Set Attendee Last Name |
|
| 100 | + * |
|
| 101 | + * @access public |
|
| 102 | + * @param string $lname |
|
| 103 | + * @throws EE_Error |
|
| 104 | + */ |
|
| 105 | + public function set_lname($lname = '') |
|
| 106 | + { |
|
| 107 | + $this->set('ATT_lname', $lname); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Set Attendee Address |
|
| 113 | + * |
|
| 114 | + * @access public |
|
| 115 | + * @param string $address |
|
| 116 | + * @throws EE_Error |
|
| 117 | + */ |
|
| 118 | + public function set_address($address = '') |
|
| 119 | + { |
|
| 120 | + $this->set('ATT_address', $address); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Set Attendee Address2 |
|
| 126 | + * |
|
| 127 | + * @access public |
|
| 128 | + * @param string $address2 |
|
| 129 | + * @throws EE_Error |
|
| 130 | + */ |
|
| 131 | + public function set_address2($address2 = '') |
|
| 132 | + { |
|
| 133 | + $this->set('ATT_address2', $address2); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Set Attendee City |
|
| 139 | + * |
|
| 140 | + * @access public |
|
| 141 | + * @param string $city |
|
| 142 | + * @throws EE_Error |
|
| 143 | + */ |
|
| 144 | + public function set_city($city = '') |
|
| 145 | + { |
|
| 146 | + $this->set('ATT_city', $city); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Set Attendee State ID |
|
| 152 | + * |
|
| 153 | + * @access public |
|
| 154 | + * @param int $STA_ID |
|
| 155 | + * @throws EE_Error |
|
| 156 | + */ |
|
| 157 | + public function set_state($STA_ID = 0) |
|
| 158 | + { |
|
| 159 | + $this->set('STA_ID', $STA_ID); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Set Attendee Country ISO Code |
|
| 165 | + * |
|
| 166 | + * @access public |
|
| 167 | + * @param string $CNT_ISO |
|
| 168 | + * @throws EE_Error |
|
| 169 | + */ |
|
| 170 | + public function set_country($CNT_ISO = '') |
|
| 171 | + { |
|
| 172 | + $this->set('CNT_ISO', $CNT_ISO); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Set Attendee Zip/Postal Code |
|
| 178 | + * |
|
| 179 | + * @access public |
|
| 180 | + * @param string $zip |
|
| 181 | + * @throws EE_Error |
|
| 182 | + */ |
|
| 183 | + public function set_zip($zip = '') |
|
| 184 | + { |
|
| 185 | + $this->set('ATT_zip', $zip); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Set Attendee Email Address |
|
| 191 | + * |
|
| 192 | + * @access public |
|
| 193 | + * @param string $email |
|
| 194 | + * @throws EE_Error |
|
| 195 | + */ |
|
| 196 | + public function set_email($email = '') |
|
| 197 | + { |
|
| 198 | + $this->set('ATT_email', $email); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Set Attendee Phone |
|
| 204 | + * |
|
| 205 | + * @access public |
|
| 206 | + * @param string $phone |
|
| 207 | + * @throws EE_Error |
|
| 208 | + */ |
|
| 209 | + public function set_phone($phone = '') |
|
| 210 | + { |
|
| 211 | + $this->set('ATT_phone', $phone); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * set deleted |
|
| 217 | + * |
|
| 218 | + * @access public |
|
| 219 | + * @param bool $ATT_deleted |
|
| 220 | + * @throws EE_Error |
|
| 221 | + */ |
|
| 222 | + public function set_deleted($ATT_deleted = false) |
|
| 223 | + { |
|
| 224 | + $this->set('ATT_deleted', $ATT_deleted); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Returns the value for the post_author id saved with the cpt |
|
| 230 | + * |
|
| 231 | + * @since 4.5.0 |
|
| 232 | + * @return int |
|
| 233 | + * @throws EE_Error |
|
| 234 | + */ |
|
| 235 | + public function wp_user() |
|
| 236 | + { |
|
| 237 | + return $this->get('ATT_author'); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * get Attendee First Name |
|
| 243 | + * |
|
| 244 | + * @access public |
|
| 245 | + * @return string |
|
| 246 | + * @throws EE_Error |
|
| 247 | + */ |
|
| 248 | + public function fname() |
|
| 249 | + { |
|
| 250 | + return $this->get('ATT_fname'); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * echoes out the attendee's first name |
|
| 256 | + * |
|
| 257 | + * @return void |
|
| 258 | + * @throws EE_Error |
|
| 259 | + */ |
|
| 260 | + public function e_full_name() |
|
| 261 | + { |
|
| 262 | + echo $this->full_name(); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Returns the first and last name concatenated together with a space. |
|
| 268 | + * |
|
| 269 | + * @param bool $apply_html_entities |
|
| 270 | + * @return string |
|
| 271 | + * @throws EE_Error |
|
| 272 | + */ |
|
| 273 | + public function full_name($apply_html_entities = false) |
|
| 274 | + { |
|
| 275 | + $full_name = array( |
|
| 276 | + $this->fname(), |
|
| 277 | + $this->lname(), |
|
| 278 | + ); |
|
| 279 | + $full_name = array_filter($full_name); |
|
| 280 | + $full_name = implode(' ', $full_name); |
|
| 281 | + return $apply_html_entities ? htmlentities($full_name, ENT_QUOTES, 'UTF-8') : $full_name; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * This returns the value of the `ATT_full_name` field which is usually equivalent to calling `full_name()` unless |
|
| 287 | + * the post_title field has been directly modified in the db for the post (espresso_attendees post type) for this |
|
| 288 | + * attendee. |
|
| 289 | + * |
|
| 290 | + * @param bool $apply_html_entities |
|
| 291 | + * @return string |
|
| 292 | + * @throws EE_Error |
|
| 293 | + */ |
|
| 294 | + public function ATT_full_name($apply_html_entities = false) |
|
| 295 | + { |
|
| 296 | + return $apply_html_entities |
|
| 297 | + ? htmlentities($this->get('ATT_full_name'), ENT_QUOTES, 'UTF-8') |
|
| 298 | + : $this->get('ATT_full_name'); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * get Attendee Last Name |
|
| 304 | + * |
|
| 305 | + * @access public |
|
| 306 | + * @return string |
|
| 307 | + * @throws EE_Error |
|
| 308 | + */ |
|
| 309 | + public function lname() |
|
| 310 | + { |
|
| 311 | + return $this->get('ATT_lname'); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Gets the attendee's full address as an array so client code can decide hwo to display it |
|
| 317 | + * |
|
| 318 | + * @return array numerically indexed, with each part of the address that is known. |
|
| 319 | + * Eg, if the user only responded to state and country, |
|
| 320 | + * it would be array(0=>'Alabama',1=>'USA') |
|
| 321 | + * @return array |
|
| 322 | + * @throws EE_Error |
|
| 323 | + */ |
|
| 324 | + public function full_address_as_array() |
|
| 325 | + { |
|
| 326 | + $full_address_array = array(); |
|
| 327 | + $initial_address_fields = array('ATT_address', 'ATT_address2', 'ATT_city',); |
|
| 328 | + foreach ($initial_address_fields as $address_field_name) { |
|
| 329 | + $address_fields_value = $this->get($address_field_name); |
|
| 330 | + if (! empty($address_fields_value)) { |
|
| 331 | + $full_address_array[] = $address_fields_value; |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + //now handle state and country |
|
| 335 | + $state_obj = $this->state_obj(); |
|
| 336 | + if ($state_obj instanceof EE_State) { |
|
| 337 | + $full_address_array[] = $state_obj->name(); |
|
| 338 | + } |
|
| 339 | + $country_obj = $this->country_obj(); |
|
| 340 | + if ($country_obj instanceof EE_Country) { |
|
| 341 | + $full_address_array[] = $country_obj->name(); |
|
| 342 | + } |
|
| 343 | + //lastly get the xip |
|
| 344 | + $zip_value = $this->zip(); |
|
| 345 | + if (! empty($zip_value)) { |
|
| 346 | + $full_address_array[] = $zip_value; |
|
| 347 | + } |
|
| 348 | + return $full_address_array; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * get Attendee Address |
|
| 354 | + * |
|
| 355 | + * @return string |
|
| 356 | + * @throws EE_Error |
|
| 357 | + */ |
|
| 358 | + public function address() |
|
| 359 | + { |
|
| 360 | + return $this->get('ATT_address'); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * get Attendee Address2 |
|
| 366 | + * |
|
| 367 | + * @return string |
|
| 368 | + * @throws EE_Error |
|
| 369 | + */ |
|
| 370 | + public function address2() |
|
| 371 | + { |
|
| 372 | + return $this->get('ATT_address2'); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * get Attendee City |
|
| 378 | + * |
|
| 379 | + * @return string |
|
| 380 | + * @throws EE_Error |
|
| 381 | + */ |
|
| 382 | + public function city() |
|
| 383 | + { |
|
| 384 | + return $this->get('ATT_city'); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * get Attendee State ID |
|
| 390 | + * |
|
| 391 | + * @return string |
|
| 392 | + * @throws EE_Error |
|
| 393 | + */ |
|
| 394 | + public function state_ID() |
|
| 395 | + { |
|
| 396 | + return $this->get('STA_ID'); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * @return string |
|
| 402 | + * @throws EE_Error |
|
| 403 | + */ |
|
| 404 | + public function state_abbrev() |
|
| 405 | + { |
|
| 406 | + return $this->state_obj() instanceof EE_State ? $this->state_obj()->abbrev() : ''; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Gets the state set to this attendee |
|
| 412 | + * |
|
| 413 | + * @return EE_State |
|
| 414 | + * @throws EE_Error |
|
| 415 | + */ |
|
| 416 | + public function state_obj() |
|
| 417 | + { |
|
| 418 | + return $this->get_first_related('State'); |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Returns the state's name, otherwise 'Unknown' |
|
| 424 | + * |
|
| 425 | + * @return string |
|
| 426 | + * @throws EE_Error |
|
| 427 | + */ |
|
| 428 | + public function state_name() |
|
| 429 | + { |
|
| 430 | + if ($this->state_obj()) { |
|
| 431 | + return $this->state_obj()->name(); |
|
| 432 | + } else { |
|
| 433 | + return ''; |
|
| 434 | + } |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * either displays the state abbreviation or the state name, as determined |
|
| 440 | + * by the "FHEE__EEI_Address__state__use_abbreviation" filter. |
|
| 441 | + * defaults to abbreviation |
|
| 442 | + * |
|
| 443 | + * @return string |
|
| 444 | + * @throws EE_Error |
|
| 445 | + */ |
|
| 446 | + public function state() |
|
| 447 | + { |
|
| 448 | + if (apply_filters('FHEE__EEI_Address__state__use_abbreviation', true, $this->state_obj())) { |
|
| 449 | + return $this->state_abbrev(); |
|
| 450 | + } |
|
| 451 | + return $this->state_name(); |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * get Attendee Country ISO Code |
|
| 457 | + * |
|
| 458 | + * @return string |
|
| 459 | + * @throws EE_Error |
|
| 460 | + */ |
|
| 461 | + public function country_ID() |
|
| 462 | + { |
|
| 463 | + return $this->get('CNT_ISO'); |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Gets country set for this attendee |
|
| 469 | + * |
|
| 470 | + * @return EE_Country |
|
| 471 | + * @throws EE_Error |
|
| 472 | + */ |
|
| 473 | + public function country_obj() |
|
| 474 | + { |
|
| 475 | + return $this->get_first_related('Country'); |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * Returns the country's name if known, otherwise 'Unknown' |
|
| 481 | + * |
|
| 482 | + * @return string |
|
| 483 | + * @throws EE_Error |
|
| 484 | + */ |
|
| 485 | + public function country_name() |
|
| 486 | + { |
|
| 487 | + if ($this->country_obj()) { |
|
| 488 | + return $this->country_obj()->name(); |
|
| 489 | + } |
|
| 490 | + return ''; |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * either displays the country ISO2 code or the country name, as determined |
|
| 496 | + * by the "FHEE__EEI_Address__country__use_abbreviation" filter. |
|
| 497 | + * defaults to abbreviation |
|
| 498 | + * |
|
| 499 | + * @return string |
|
| 500 | + * @throws EE_Error |
|
| 501 | + */ |
|
| 502 | + public function country() |
|
| 503 | + { |
|
| 504 | + if (apply_filters('FHEE__EEI_Address__country__use_abbreviation', true, $this->country_obj())) { |
|
| 505 | + return $this->country_ID(); |
|
| 506 | + } |
|
| 507 | + return $this->country_name(); |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + |
|
| 511 | + /** |
|
| 512 | + * get Attendee Zip/Postal Code |
|
| 513 | + * |
|
| 514 | + * @return string |
|
| 515 | + * @throws EE_Error |
|
| 516 | + */ |
|
| 517 | + public function zip() |
|
| 518 | + { |
|
| 519 | + return $this->get('ATT_zip'); |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * get Attendee Email Address |
|
| 525 | + * |
|
| 526 | + * @return string |
|
| 527 | + * @throws EE_Error |
|
| 528 | + */ |
|
| 529 | + public function email() |
|
| 530 | + { |
|
| 531 | + return $this->get('ATT_email'); |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + |
|
| 535 | + /** |
|
| 536 | + * get Attendee Phone # |
|
| 537 | + * |
|
| 538 | + * @return string |
|
| 539 | + * @throws EE_Error |
|
| 540 | + */ |
|
| 541 | + public function phone() |
|
| 542 | + { |
|
| 543 | + return $this->get('ATT_phone'); |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + |
|
| 547 | + /** |
|
| 548 | + * get deleted |
|
| 549 | + * |
|
| 550 | + * @return bool |
|
| 551 | + * @throws EE_Error |
|
| 552 | + */ |
|
| 553 | + public function deleted() |
|
| 554 | + { |
|
| 555 | + return $this->get('ATT_deleted'); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + |
|
| 559 | + /** |
|
| 560 | + * Gets registrations of this attendee |
|
| 561 | + * |
|
| 562 | + * @param array $query_params |
|
| 563 | + * @return EE_Registration[] |
|
| 564 | + * @throws EE_Error |
|
| 565 | + */ |
|
| 566 | + public function get_registrations($query_params = array()) |
|
| 567 | + { |
|
| 568 | + return $this->get_many_related('Registration', $query_params); |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + |
|
| 572 | + /** |
|
| 573 | + * Gets the most recent registration of this attendee |
|
| 574 | + * |
|
| 575 | + * @return EE_Registration |
|
| 576 | + * @throws EE_Error |
|
| 577 | + */ |
|
| 578 | + public function get_most_recent_registration() |
|
| 579 | + { |
|
| 580 | + return $this->get_first_related( |
|
| 581 | + 'Registration', |
|
| 582 | + array('order_by' => array('REG_date' => 'DESC')) |
|
| 583 | + ); //null, 'REG_date', 'DESC', '=', 'OBJECT_K'); |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * Gets the most recent registration for this attend at this event |
|
| 589 | + * |
|
| 590 | + * @param int $event_id |
|
| 591 | + * @return EE_Registration |
|
| 592 | + * @throws EE_Error |
|
| 593 | + */ |
|
| 594 | + public function get_most_recent_registration_for_event($event_id) |
|
| 595 | + { |
|
| 596 | + return $this->get_first_related( |
|
| 597 | + 'Registration', |
|
| 598 | + array(array('EVT_ID' => $event_id), 'order_by' => array('REG_date' => 'DESC')) |
|
| 599 | + ); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + |
|
| 603 | + /** |
|
| 604 | + * returns any events attached to this attendee ($_Event property); |
|
| 605 | + * |
|
| 606 | + * @return array |
|
| 607 | + * @throws EE_Error |
|
| 608 | + */ |
|
| 609 | + public function events() |
|
| 610 | + { |
|
| 611 | + return $this->get_many_related('Event'); |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * Gets the billing info array where keys match espresso_reg_page_billing_inputs(), |
|
| 617 | + * and keys are their cleaned values. @see EE_Attendee::save_and_clean_billing_info_for_payment_method() which was |
|
| 618 | + * used to save the billing info |
|
| 619 | + * |
|
| 620 | + * @param EE_Payment_Method $payment_method the _gateway_name property on the gateway class |
|
| 621 | + * @return EE_Form_Section_Proper|null |
|
| 622 | + * @throws EE_Error |
|
| 623 | + */ |
|
| 624 | + public function billing_info_for_payment_method($payment_method) |
|
| 625 | + { |
|
| 626 | + $pm_type = $payment_method->type_obj(); |
|
| 627 | + if (! $pm_type instanceof EE_PMT_Base) { |
|
| 628 | + return null; |
|
| 629 | + } |
|
| 630 | + $billing_info = $this->get_post_meta($this->get_billing_info_postmeta_name($payment_method), true); |
|
| 631 | + if (! $billing_info) { |
|
| 632 | + return null; |
|
| 633 | + } |
|
| 634 | + $billing_form = $pm_type->billing_form(); |
|
| 635 | + if ($billing_form instanceof EE_Form_Section_Proper) { |
|
| 636 | + $billing_form->receive_form_submission(array($billing_form->name() => $billing_info), false); |
|
| 637 | + } |
|
| 638 | + return $billing_form; |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + |
|
| 642 | + /** |
|
| 643 | + * Gets the postmeta key that holds this attendee's billing info for the |
|
| 644 | + * specified payment method |
|
| 645 | + * |
|
| 646 | + * @param EE_Payment_Method $payment_method |
|
| 647 | + * @return string |
|
| 648 | + * @throws EE_Error |
|
| 649 | + */ |
|
| 650 | + public function get_billing_info_postmeta_name($payment_method) |
|
| 651 | + { |
|
| 652 | + if ($payment_method->type_obj() instanceof EE_PMT_Base) { |
|
| 653 | + return 'billing_info_' . $payment_method->type_obj()->system_name(); |
|
| 654 | + } |
|
| 655 | + return null; |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + |
|
| 659 | + /** |
|
| 660 | + * Saves the billing info to the attendee. @see EE_Attendee::billing_info_for_payment_method() which is used to |
|
| 661 | + * retrieve it |
|
| 662 | + * |
|
| 663 | + * @param EE_Billing_Attendee_Info_Form $billing_form |
|
| 664 | + * @param EE_Payment_Method $payment_method |
|
| 665 | + * @return boolean |
|
| 666 | + * @throws EE_Error |
|
| 667 | + */ |
|
| 668 | + public function save_and_clean_billing_info_for_payment_method($billing_form, $payment_method) |
|
| 669 | + { |
|
| 670 | + if (! $billing_form instanceof EE_Billing_Attendee_Info_Form) { |
|
| 671 | + EE_Error::add_error(__('Cannot save billing info because there is none.', 'event_espresso')); |
|
| 672 | + return false; |
|
| 673 | + } |
|
| 674 | + $billing_form->clean_sensitive_data(); |
|
| 675 | + return update_post_meta( |
|
| 676 | + $this->ID(), |
|
| 677 | + $this->get_billing_info_postmeta_name($payment_method), |
|
| 678 | + $billing_form->input_values(true) |
|
| 679 | + ); |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + |
|
| 683 | + /** |
|
| 684 | + * Return the link to the admin details for the object. |
|
| 685 | + * |
|
| 686 | + * @return string |
|
| 687 | + * @throws EE_Error |
|
| 688 | + * @throws InvalidArgumentException |
|
| 689 | + * @throws InvalidDataTypeException |
|
| 690 | + * @throws InvalidInterfaceException |
|
| 691 | + * @throws ReflectionException |
|
| 692 | + */ |
|
| 693 | + public function get_admin_details_link() |
|
| 694 | + { |
|
| 695 | + return $this->get_admin_edit_link(); |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + |
|
| 699 | + /** |
|
| 700 | + * Returns the link to the editor for the object. Sometimes this is the same as the details. |
|
| 701 | + * |
|
| 702 | + * @return string |
|
| 703 | + * @throws EE_Error |
|
| 704 | + * @throws InvalidArgumentException |
|
| 705 | + * @throws ReflectionException |
|
| 706 | + * @throws InvalidDataTypeException |
|
| 707 | + * @throws InvalidInterfaceException |
|
| 708 | + */ |
|
| 709 | + public function get_admin_edit_link() |
|
| 710 | + { |
|
| 711 | + EE_Registry::instance()->load_helper('URL'); |
|
| 712 | + return EEH_URL::add_query_args_and_nonce( |
|
| 713 | + array( |
|
| 714 | + 'page' => 'espresso_registrations', |
|
| 715 | + 'action' => 'edit_attendee', |
|
| 716 | + 'post' => $this->ID(), |
|
| 717 | + ), |
|
| 718 | + admin_url('admin.php') |
|
| 719 | + ); |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + |
|
| 723 | + /** |
|
| 724 | + * Returns the link to a settings page for the object. |
|
| 725 | + * |
|
| 726 | + * @return string |
|
| 727 | + * @throws EE_Error |
|
| 728 | + * @throws InvalidArgumentException |
|
| 729 | + * @throws InvalidDataTypeException |
|
| 730 | + * @throws InvalidInterfaceException |
|
| 731 | + * @throws ReflectionException |
|
| 732 | + */ |
|
| 733 | + public function get_admin_settings_link() |
|
| 734 | + { |
|
| 735 | + return $this->get_admin_edit_link(); |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + |
|
| 739 | + /** |
|
| 740 | + * Returns the link to the "overview" for the object (typically the "list table" view). |
|
| 741 | + * |
|
| 742 | + * @return string |
|
| 743 | + * @throws EE_Error |
|
| 744 | + * @throws InvalidArgumentException |
|
| 745 | + * @throws ReflectionException |
|
| 746 | + * @throws InvalidDataTypeException |
|
| 747 | + * @throws InvalidInterfaceException |
|
| 748 | + */ |
|
| 749 | + public function get_admin_overview_link() |
|
| 750 | + { |
|
| 751 | + EE_Registry::instance()->load_helper('URL'); |
|
| 752 | + return EEH_URL::add_query_args_and_nonce( |
|
| 753 | + array( |
|
| 754 | + 'page' => 'espresso_registrations', |
|
| 755 | + 'action' => 'contact_list', |
|
| 756 | + ), |
|
| 757 | + admin_url('admin.php') |
|
| 758 | + ); |
|
| 759 | + } |
|
| 760 | 760 | } |
@@ -38,16 +38,16 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | protected function __construct($fieldValues = null, $bydb = false, $timezone = null, $date_formats = array()) |
| 40 | 40 | { |
| 41 | - if (! isset($fieldValues['ATT_full_name'])) { |
|
| 42 | - $fname = isset($fieldValues['ATT_fname']) ? $fieldValues['ATT_fname'] . ' ' : ''; |
|
| 41 | + if ( ! isset($fieldValues['ATT_full_name'])) { |
|
| 42 | + $fname = isset($fieldValues['ATT_fname']) ? $fieldValues['ATT_fname'].' ' : ''; |
|
| 43 | 43 | $lname = isset($fieldValues['ATT_lname']) ? $fieldValues['ATT_lname'] : ''; |
| 44 | - $fieldValues['ATT_full_name'] = $fname . $lname; |
|
| 44 | + $fieldValues['ATT_full_name'] = $fname.$lname; |
|
| 45 | 45 | } |
| 46 | - if (! isset($fieldValues['ATT_slug'])) { |
|
| 46 | + if ( ! isset($fieldValues['ATT_slug'])) { |
|
| 47 | 47 | // $fieldValues['ATT_slug'] = sanitize_key(wp_generate_password(20)); |
| 48 | 48 | $fieldValues['ATT_slug'] = sanitize_title($fieldValues['ATT_full_name']); |
| 49 | 49 | } |
| 50 | - if (! isset($fieldValues['ATT_short_bio']) && isset($fieldValues['ATT_bio'])) { |
|
| 50 | + if ( ! isset($fieldValues['ATT_short_bio']) && isset($fieldValues['ATT_bio'])) { |
|
| 51 | 51 | $fieldValues['ATT_short_bio'] = substr($fieldValues['ATT_bio'], 0, 50); |
| 52 | 52 | } |
| 53 | 53 | parent::__construct($fieldValues, $bydb, $timezone, $date_formats); |
@@ -327,7 +327,7 @@ discard block |
||
| 327 | 327 | $initial_address_fields = array('ATT_address', 'ATT_address2', 'ATT_city',); |
| 328 | 328 | foreach ($initial_address_fields as $address_field_name) { |
| 329 | 329 | $address_fields_value = $this->get($address_field_name); |
| 330 | - if (! empty($address_fields_value)) { |
|
| 330 | + if ( ! empty($address_fields_value)) { |
|
| 331 | 331 | $full_address_array[] = $address_fields_value; |
| 332 | 332 | } |
| 333 | 333 | } |
@@ -342,7 +342,7 @@ discard block |
||
| 342 | 342 | } |
| 343 | 343 | //lastly get the xip |
| 344 | 344 | $zip_value = $this->zip(); |
| 345 | - if (! empty($zip_value)) { |
|
| 345 | + if ( ! empty($zip_value)) { |
|
| 346 | 346 | $full_address_array[] = $zip_value; |
| 347 | 347 | } |
| 348 | 348 | return $full_address_array; |
@@ -624,11 +624,11 @@ discard block |
||
| 624 | 624 | public function billing_info_for_payment_method($payment_method) |
| 625 | 625 | { |
| 626 | 626 | $pm_type = $payment_method->type_obj(); |
| 627 | - if (! $pm_type instanceof EE_PMT_Base) { |
|
| 627 | + if ( ! $pm_type instanceof EE_PMT_Base) { |
|
| 628 | 628 | return null; |
| 629 | 629 | } |
| 630 | 630 | $billing_info = $this->get_post_meta($this->get_billing_info_postmeta_name($payment_method), true); |
| 631 | - if (! $billing_info) { |
|
| 631 | + if ( ! $billing_info) { |
|
| 632 | 632 | return null; |
| 633 | 633 | } |
| 634 | 634 | $billing_form = $pm_type->billing_form(); |
@@ -650,7 +650,7 @@ discard block |
||
| 650 | 650 | public function get_billing_info_postmeta_name($payment_method) |
| 651 | 651 | { |
| 652 | 652 | if ($payment_method->type_obj() instanceof EE_PMT_Base) { |
| 653 | - return 'billing_info_' . $payment_method->type_obj()->system_name(); |
|
| 653 | + return 'billing_info_'.$payment_method->type_obj()->system_name(); |
|
| 654 | 654 | } |
| 655 | 655 | return null; |
| 656 | 656 | } |
@@ -667,7 +667,7 @@ discard block |
||
| 667 | 667 | */ |
| 668 | 668 | public function save_and_clean_billing_info_for_payment_method($billing_form, $payment_method) |
| 669 | 669 | { |
| 670 | - if (! $billing_form instanceof EE_Billing_Attendee_Info_Form) { |
|
| 670 | + if ( ! $billing_form instanceof EE_Billing_Attendee_Info_Form) { |
|
| 671 | 671 | EE_Error::add_error(__('Cannot save billing info because there is none.', 'event_espresso')); |
| 672 | 672 | return false; |
| 673 | 673 | } |
@@ -34,6184 +34,6184 @@ |
||
| 34 | 34 | abstract class EEM_Base extends EE_Base implements ResettableInterface |
| 35 | 35 | { |
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Flag to indicate whether the values provided to EEM_Base have already been prepared |
|
| 39 | - * by the model object or not (ie, the model object has used the field's _prepare_for_set function on the values). |
|
| 40 | - * They almost always WILL NOT, but it's not necessarily a requirement. |
|
| 41 | - * For example, if you want to run EEM_Event::instance()->get_all(array(array('EVT_ID'=>$_GET['event_id']))); |
|
| 42 | - * |
|
| 43 | - * @var boolean |
|
| 44 | - */ |
|
| 45 | - private $_values_already_prepared_by_model_object = 0; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * when $_values_already_prepared_by_model_object equals this, we assume |
|
| 49 | - * the data is just like form input that needs to have the model fields' |
|
| 50 | - * prepare_for_set and prepare_for_use_in_db called on it |
|
| 51 | - */ |
|
| 52 | - const not_prepared_by_model_object = 0; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * when $_values_already_prepared_by_model_object equals this, we |
|
| 56 | - * assume this value is coming from a model object and doesn't need to have |
|
| 57 | - * prepare_for_set called on it, just prepare_for_use_in_db is used |
|
| 58 | - */ |
|
| 59 | - const prepared_by_model_object = 1; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * when $_values_already_prepared_by_model_object equals this, we assume |
|
| 63 | - * the values are already to be used in the database (ie no processing is done |
|
| 64 | - * on them by the model's fields) |
|
| 65 | - */ |
|
| 66 | - const prepared_for_use_in_db = 2; |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - protected $singular_item = 'Item'; |
|
| 70 | - |
|
| 71 | - protected $plural_item = 'Items'; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @type \EE_Table_Base[] $_tables array of EE_Table objects for defining which tables comprise this model. |
|
| 75 | - */ |
|
| 76 | - protected $_tables; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * with two levels: top-level has array keys which are database table aliases (ie, keys in _tables) |
|
| 80 | - * and the value is an array. Each of those sub-arrays have keys of field names (eg 'ATT_ID', which should also be |
|
| 81 | - * variable names on the model objects (eg, EE_Attendee), and the keys should be children of EE_Model_Field |
|
| 82 | - * |
|
| 83 | - * @var \EE_Model_Field_Base[][] $_fields |
|
| 84 | - */ |
|
| 85 | - protected $_fields; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * array of different kinds of relations |
|
| 89 | - * |
|
| 90 | - * @var \EE_Model_Relation_Base[] $_model_relations |
|
| 91 | - */ |
|
| 92 | - protected $_model_relations; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @var \EE_Index[] $_indexes |
|
| 96 | - */ |
|
| 97 | - protected $_indexes = array(); |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Default strategy for getting where conditions on this model. This strategy is used to get default |
|
| 101 | - * where conditions which are added to get_all, update, and delete queries. They can be overridden |
|
| 102 | - * by setting the same columns as used in these queries in the query yourself. |
|
| 103 | - * |
|
| 104 | - * @var EE_Default_Where_Conditions |
|
| 105 | - */ |
|
| 106 | - protected $_default_where_conditions_strategy; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Strategy for getting conditions on this model when 'default_where_conditions' equals 'minimum'. |
|
| 110 | - * This is particularly useful when you want something between 'none' and 'default' |
|
| 111 | - * |
|
| 112 | - * @var EE_Default_Where_Conditions |
|
| 113 | - */ |
|
| 114 | - protected $_minimum_where_conditions_strategy; |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * String describing how to find the "owner" of this model's objects. |
|
| 118 | - * When there is a foreign key on this model to the wp_users table, this isn't needed. |
|
| 119 | - * But when there isn't, this indicates which related model, or transiently-related model, |
|
| 120 | - * has the foreign key to the wp_users table. |
|
| 121 | - * Eg, for EEM_Registration this would be 'Event' because registrations are directly |
|
| 122 | - * related to events, and events have a foreign key to wp_users. |
|
| 123 | - * On EEM_Transaction, this would be 'Transaction.Event' |
|
| 124 | - * |
|
| 125 | - * @var string |
|
| 126 | - */ |
|
| 127 | - protected $_model_chain_to_wp_user = ''; |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * This is a flag typically set by updates so that we don't load the where strategy on updates because updates |
|
| 131 | - * don't need it (particularly CPT models) |
|
| 132 | - * |
|
| 133 | - * @var bool |
|
| 134 | - */ |
|
| 135 | - protected $_ignore_where_strategy = false; |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * String used in caps relating to this model. Eg, if the caps relating to this |
|
| 139 | - * model are 'ee_edit_events', 'ee_read_events', etc, it would be 'events'. |
|
| 140 | - * |
|
| 141 | - * @var string. If null it hasn't been initialized yet. If false then we |
|
| 142 | - * have indicated capabilities don't apply to this |
|
| 143 | - */ |
|
| 144 | - protected $_caps_slug = null; |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * 2d array where top-level keys are one of EEM_Base::valid_cap_contexts(), |
|
| 148 | - * and next-level keys are capability names, and each's value is a |
|
| 149 | - * EE_Default_Where_Condition. If the requester requests to apply caps to the query, |
|
| 150 | - * they specify which context to use (ie, frontend, backend, edit or delete) |
|
| 151 | - * and then each capability in the corresponding sub-array that they're missing |
|
| 152 | - * adds the where conditions onto the query. |
|
| 153 | - * |
|
| 154 | - * @var array |
|
| 155 | - */ |
|
| 156 | - protected $_cap_restrictions = array( |
|
| 157 | - self::caps_read => array(), |
|
| 158 | - self::caps_read_admin => array(), |
|
| 159 | - self::caps_edit => array(), |
|
| 160 | - self::caps_delete => array(), |
|
| 161 | - ); |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Array defining which cap restriction generators to use to create default |
|
| 165 | - * cap restrictions to put in EEM_Base::_cap_restrictions. |
|
| 166 | - * Array-keys are one of EEM_Base::valid_cap_contexts(), and values are a child of |
|
| 167 | - * EE_Restriction_Generator_Base. If you don't want any cap restrictions generated |
|
| 168 | - * automatically set this to false (not just null). |
|
| 169 | - * |
|
| 170 | - * @var EE_Restriction_Generator_Base[] |
|
| 171 | - */ |
|
| 172 | - protected $_cap_restriction_generators = array(); |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * constants used to categorize capability restrictions on EEM_Base::_caps_restrictions |
|
| 176 | - */ |
|
| 177 | - const caps_read = 'read'; |
|
| 178 | - |
|
| 179 | - const caps_read_admin = 'read_admin'; |
|
| 180 | - |
|
| 181 | - const caps_edit = 'edit'; |
|
| 182 | - |
|
| 183 | - const caps_delete = 'delete'; |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Keys are all the cap contexts (ie constants EEM_Base::_caps_*) and values are their 'action' |
|
| 187 | - * as how they'd be used in capability names. Eg EEM_Base::caps_read ('read_frontend') |
|
| 188 | - * maps to 'read' because when looking for relevant permissions we're going to use |
|
| 189 | - * 'read' in teh capabilities names like 'ee_read_events' etc. |
|
| 190 | - * |
|
| 191 | - * @var array |
|
| 192 | - */ |
|
| 193 | - protected $_cap_contexts_to_cap_action_map = array( |
|
| 194 | - self::caps_read => 'read', |
|
| 195 | - self::caps_read_admin => 'read', |
|
| 196 | - self::caps_edit => 'edit', |
|
| 197 | - self::caps_delete => 'delete', |
|
| 198 | - ); |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Timezone |
|
| 202 | - * This gets set via the constructor so that we know what timezone incoming strings|timestamps are in when there |
|
| 203 | - * are EE_Datetime_Fields in use. This can also be used before a get to set what timezone you want strings coming |
|
| 204 | - * out of the created objects. NOT all EEM_Base child classes use this property but any that use a |
|
| 205 | - * EE_Datetime_Field data type will have access to it. |
|
| 206 | - * |
|
| 207 | - * @var string |
|
| 208 | - */ |
|
| 209 | - protected $_timezone; |
|
| 210 | - |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * This holds the id of the blog currently making the query. Has no bearing on single site but is used for |
|
| 214 | - * multisite. |
|
| 215 | - * |
|
| 216 | - * @var int |
|
| 217 | - */ |
|
| 218 | - protected static $_model_query_blog_id; |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * A copy of _fields, except the array keys are the model names pointed to by |
|
| 222 | - * the field |
|
| 223 | - * |
|
| 224 | - * @var EE_Model_Field_Base[] |
|
| 225 | - */ |
|
| 226 | - private $_cache_foreign_key_to_fields = array(); |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Cached list of all the fields on the model, indexed by their name |
|
| 230 | - * |
|
| 231 | - * @var EE_Model_Field_Base[] |
|
| 232 | - */ |
|
| 233 | - private $_cached_fields = null; |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Cached list of all the fields on the model, except those that are |
|
| 237 | - * marked as only pertinent to the database |
|
| 238 | - * |
|
| 239 | - * @var EE_Model_Field_Base[] |
|
| 240 | - */ |
|
| 241 | - private $_cached_fields_non_db_only = null; |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * A cached reference to the primary key for quick lookup |
|
| 245 | - * |
|
| 246 | - * @var EE_Model_Field_Base |
|
| 247 | - */ |
|
| 248 | - private $_primary_key_field = null; |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Flag indicating whether this model has a primary key or not |
|
| 252 | - * |
|
| 253 | - * @var boolean |
|
| 254 | - */ |
|
| 255 | - protected $_has_primary_key_field = null; |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Whether or not this model is based off a table in WP core only (CPTs should set |
|
| 259 | - * this to FALSE, but if we were to make an EE_WP_Post model, it should set this to true). |
|
| 260 | - * This should be true for models that deal with data that should exist independent of EE. |
|
| 261 | - * For example, if the model can read and insert data that isn't used by EE, this should be true. |
|
| 262 | - * It would be false, however, if you could guarantee the model would only interact with EE data, |
|
| 263 | - * even if it uses a WP core table (eg event and venue models set this to false for that reason: |
|
| 264 | - * they can only read and insert events and venues custom post types, not arbitrary post types) |
|
| 265 | - * @var boolean |
|
| 266 | - */ |
|
| 267 | - protected $_wp_core_model = false; |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * List of valid operators that can be used for querying. |
|
| 271 | - * The keys are all operators we'll accept, the values are the real SQL |
|
| 272 | - * operators used |
|
| 273 | - * |
|
| 274 | - * @var array |
|
| 275 | - */ |
|
| 276 | - protected $_valid_operators = array( |
|
| 277 | - '=' => '=', |
|
| 278 | - '<=' => '<=', |
|
| 279 | - '<' => '<', |
|
| 280 | - '>=' => '>=', |
|
| 281 | - '>' => '>', |
|
| 282 | - '!=' => '!=', |
|
| 283 | - 'LIKE' => 'LIKE', |
|
| 284 | - 'like' => 'LIKE', |
|
| 285 | - 'NOT_LIKE' => 'NOT LIKE', |
|
| 286 | - 'not_like' => 'NOT LIKE', |
|
| 287 | - 'NOT LIKE' => 'NOT LIKE', |
|
| 288 | - 'not like' => 'NOT LIKE', |
|
| 289 | - 'IN' => 'IN', |
|
| 290 | - 'in' => 'IN', |
|
| 291 | - 'NOT_IN' => 'NOT IN', |
|
| 292 | - 'not_in' => 'NOT IN', |
|
| 293 | - 'NOT IN' => 'NOT IN', |
|
| 294 | - 'not in' => 'NOT IN', |
|
| 295 | - 'between' => 'BETWEEN', |
|
| 296 | - 'BETWEEN' => 'BETWEEN', |
|
| 297 | - 'IS_NOT_NULL' => 'IS NOT NULL', |
|
| 298 | - 'is_not_null' => 'IS NOT NULL', |
|
| 299 | - 'IS NOT NULL' => 'IS NOT NULL', |
|
| 300 | - 'is not null' => 'IS NOT NULL', |
|
| 301 | - 'IS_NULL' => 'IS NULL', |
|
| 302 | - 'is_null' => 'IS NULL', |
|
| 303 | - 'IS NULL' => 'IS NULL', |
|
| 304 | - 'is null' => 'IS NULL', |
|
| 305 | - 'REGEXP' => 'REGEXP', |
|
| 306 | - 'regexp' => 'REGEXP', |
|
| 307 | - 'NOT_REGEXP' => 'NOT REGEXP', |
|
| 308 | - 'not_regexp' => 'NOT REGEXP', |
|
| 309 | - 'NOT REGEXP' => 'NOT REGEXP', |
|
| 310 | - 'not regexp' => 'NOT REGEXP', |
|
| 311 | - ); |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * operators that work like 'IN', accepting a comma-separated list of values inside brackets. Eg '(1,2,3)' |
|
| 315 | - * |
|
| 316 | - * @var array |
|
| 317 | - */ |
|
| 318 | - protected $_in_style_operators = array('IN', 'NOT IN'); |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * operators that work like 'BETWEEN'. Typically used for datetime calculations, i.e. "BETWEEN '12-1-2011' AND |
|
| 322 | - * '12-31-2012'" |
|
| 323 | - * |
|
| 324 | - * @var array |
|
| 325 | - */ |
|
| 326 | - protected $_between_style_operators = array('BETWEEN'); |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Operators that work like SQL's like: input should be assumed to be a string, already prepared for a LIKE query. |
|
| 330 | - * @var array |
|
| 331 | - */ |
|
| 332 | - protected $_like_style_operators = array('LIKE', 'NOT LIKE'); |
|
| 333 | - /** |
|
| 334 | - * operators that are used for handling NUll and !NULL queries. Typically used for when checking if a row exists |
|
| 335 | - * on a join table. |
|
| 336 | - * |
|
| 337 | - * @var array |
|
| 338 | - */ |
|
| 339 | - protected $_null_style_operators = array('IS NOT NULL', 'IS NULL'); |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Allowed values for $query_params['order'] for ordering in queries |
|
| 343 | - * |
|
| 344 | - * @var array |
|
| 345 | - */ |
|
| 346 | - protected $_allowed_order_values = array('asc', 'desc', 'ASC', 'DESC'); |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * When these are keys in a WHERE or HAVING clause, they are handled much differently |
|
| 350 | - * than regular field names. It is assumed that their values are an array of WHERE conditions |
|
| 351 | - * |
|
| 352 | - * @var array |
|
| 353 | - */ |
|
| 354 | - private $_logic_query_param_keys = array('not', 'and', 'or', 'NOT', 'AND', 'OR'); |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Allowed keys in $query_params arrays passed into queries. Note that 0 is meant to always be a |
|
| 358 | - * 'where', but 'where' clauses are so common that we thought we'd omit it |
|
| 359 | - * |
|
| 360 | - * @var array |
|
| 361 | - */ |
|
| 362 | - private $_allowed_query_params = array( |
|
| 363 | - 0, |
|
| 364 | - 'limit', |
|
| 365 | - 'order_by', |
|
| 366 | - 'group_by', |
|
| 367 | - 'having', |
|
| 368 | - 'force_join', |
|
| 369 | - 'order', |
|
| 370 | - 'on_join_limit', |
|
| 371 | - 'default_where_conditions', |
|
| 372 | - 'caps', |
|
| 373 | - 'extra_selects' |
|
| 374 | - ); |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * All the data types that can be used in $wpdb->prepare statements. |
|
| 378 | - * |
|
| 379 | - * @var array |
|
| 380 | - */ |
|
| 381 | - private $_valid_wpdb_data_types = array('%d', '%s', '%f'); |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * @var EE_Registry $EE |
|
| 385 | - */ |
|
| 386 | - protected $EE = null; |
|
| 387 | - |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Property which, when set, will have this model echo out the next X queries to the page for debugging. |
|
| 391 | - * |
|
| 392 | - * @var int |
|
| 393 | - */ |
|
| 394 | - protected $_show_next_x_db_queries = 0; |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * When using _get_all_wpdb_results, you can specify a custom selection. If you do so, |
|
| 398 | - * it gets saved on this property as an instance of CustomSelects so those selections can be used in |
|
| 399 | - * WHERE, GROUP_BY, etc. |
|
| 400 | - * |
|
| 401 | - * @var CustomSelects |
|
| 402 | - */ |
|
| 403 | - protected $_custom_selections = array(); |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * key => value Entity Map using array( EEM_Base::$_model_query_blog_id => array( ID => model object ) ) |
|
| 407 | - * caches every model object we've fetched from the DB on this request |
|
| 408 | - * |
|
| 409 | - * @var array |
|
| 410 | - */ |
|
| 411 | - protected $_entity_map; |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * @var LoaderInterface $loader |
|
| 415 | - */ |
|
| 416 | - private static $loader; |
|
| 417 | - |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * constant used to show EEM_Base has not yet verified the db on this http request |
|
| 421 | - */ |
|
| 422 | - const db_verified_none = 0; |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * constant used to show EEM_Base has verified the EE core db on this http request, |
|
| 426 | - * but not the addons' dbs |
|
| 427 | - */ |
|
| 428 | - const db_verified_core = 1; |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * constant used to show EEM_Base has verified the addons' dbs (and implicitly |
|
| 432 | - * the EE core db too) |
|
| 433 | - */ |
|
| 434 | - const db_verified_addons = 2; |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * indicates whether an EEM_Base child has already re-verified the DB |
|
| 438 | - * is ok (we don't want to do it repetitively). Should be set to one the constants |
|
| 439 | - * looking like EEM_Base::db_verified_* |
|
| 440 | - * |
|
| 441 | - * @var int - 0 = none, 1 = core, 2 = addons |
|
| 442 | - */ |
|
| 443 | - protected static $_db_verification_level = EEM_Base::db_verified_none; |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * @const constant for 'default_where_conditions' to apply default where conditions to ALL queried models |
|
| 447 | - * (eg, if retrieving registrations ordered by their datetimes, this will only return non-trashed |
|
| 448 | - * registrations for non-trashed tickets for non-trashed datetimes) |
|
| 449 | - */ |
|
| 450 | - const default_where_conditions_all = 'all'; |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * @const constant for 'default_where_conditions' to apply default where conditions to THIS model only, but |
|
| 454 | - * no other models which are joined to (eg, if retrieving registrations ordered by their datetimes, this will |
|
| 455 | - * return non-trashed registrations, regardless of the related datetimes and tickets' statuses). |
|
| 456 | - * It is preferred to use EEM_Base::default_where_conditions_minimum_others because, when joining to |
|
| 457 | - * models which share tables with other models, this can return data for the wrong model. |
|
| 458 | - */ |
|
| 459 | - const default_where_conditions_this_only = 'this_model_only'; |
|
| 460 | - |
|
| 461 | - /** |
|
| 462 | - * @const constant for 'default_where_conditions' to apply default where conditions to other models queried, |
|
| 463 | - * but not the current model (eg, if retrieving registrations ordered by their datetimes, this will |
|
| 464 | - * return all registrations related to non-trashed tickets and non-trashed datetimes) |
|
| 465 | - */ |
|
| 466 | - const default_where_conditions_others_only = 'other_models_only'; |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * @const constant for 'default_where_conditions' to apply minimum where conditions to all models queried. |
|
| 470 | - * For most models this the same as EEM_Base::default_where_conditions_none, except for models which share |
|
| 471 | - * their table with other models, like the Event and Venue models. For example, when querying for events |
|
| 472 | - * ordered by their venues' name, this will be sure to only return real events with associated real venues |
|
| 473 | - * (regardless of whether those events and venues are trashed) |
|
| 474 | - * In contrast, using EEM_Base::default_where_conditions_none would could return WP posts other than EE |
|
| 475 | - * events. |
|
| 476 | - */ |
|
| 477 | - const default_where_conditions_minimum_all = 'minimum'; |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * @const constant for 'default_where_conditions' to apply apply where conditions to other models, and full default |
|
| 481 | - * where conditions for the queried model (eg, when querying events ordered by venues' names, this will |
|
| 482 | - * return non-trashed events for any venues, regardless of whether those associated venues are trashed or |
|
| 483 | - * not) |
|
| 484 | - */ |
|
| 485 | - const default_where_conditions_minimum_others = 'full_this_minimum_others'; |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * @const constant for 'default_where_conditions' to NOT apply any where conditions. This should very rarely be |
|
| 489 | - * used, because when querying from a model which shares its table with another model (eg Events and Venues) |
|
| 490 | - * it's possible it will return table entries for other models. You should use |
|
| 491 | - * EEM_Base::default_where_conditions_minimum_all instead. |
|
| 492 | - */ |
|
| 493 | - const default_where_conditions_none = 'none'; |
|
| 494 | - |
|
| 495 | - |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * About all child constructors: |
|
| 499 | - * they should define the _tables, _fields and _model_relations arrays. |
|
| 500 | - * Should ALWAYS be called after child constructor. |
|
| 501 | - * In order to make the child constructors to be as simple as possible, this parent constructor |
|
| 502 | - * finalizes constructing all the object's attributes. |
|
| 503 | - * Generally, rather than requiring a child to code |
|
| 504 | - * $this->_tables = array( |
|
| 505 | - * 'Event_Post_Table' => new EE_Table('Event_Post_Table','wp_posts') |
|
| 506 | - * ...); |
|
| 507 | - * (thus repeating itself in the array key and in the constructor of the new EE_Table,) |
|
| 508 | - * each EE_Table has a function to set the table's alias after the constructor, using |
|
| 509 | - * the array key ('Event_Post_Table'), instead of repeating it. The model fields and model relations |
|
| 510 | - * do something similar. |
|
| 511 | - * |
|
| 512 | - * @param null $timezone |
|
| 513 | - * @throws EE_Error |
|
| 514 | - */ |
|
| 515 | - protected function __construct($timezone = null) |
|
| 516 | - { |
|
| 517 | - // check that the model has not been loaded too soon |
|
| 518 | - if (! did_action('AHEE__EE_System__load_espresso_addons')) { |
|
| 519 | - throw new EE_Error ( |
|
| 520 | - sprintf( |
|
| 521 | - __('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.', |
|
| 522 | - 'event_espresso'), |
|
| 523 | - get_class($this) |
|
| 524 | - ) |
|
| 525 | - ); |
|
| 526 | - } |
|
| 527 | - /** |
|
| 528 | - * Set blogid for models to current blog. However we ONLY do this if $_model_query_blog_id is not already set. |
|
| 529 | - */ |
|
| 530 | - if (empty(EEM_Base::$_model_query_blog_id)) { |
|
| 531 | - EEM_Base::set_model_query_blog_id(); |
|
| 532 | - } |
|
| 533 | - /** |
|
| 534 | - * Filters the list of tables on a model. It is best to NOT use this directly and instead |
|
| 535 | - * just use EE_Register_Model_Extension |
|
| 536 | - * |
|
| 537 | - * @var EE_Table_Base[] $_tables |
|
| 538 | - */ |
|
| 539 | - $this->_tables = (array)apply_filters('FHEE__' . get_class($this) . '__construct__tables', $this->_tables); |
|
| 540 | - foreach ($this->_tables as $table_alias => $table_obj) { |
|
| 541 | - /** @var $table_obj EE_Table_Base */ |
|
| 542 | - $table_obj->_construct_finalize_with_alias($table_alias); |
|
| 543 | - if ($table_obj instanceof EE_Secondary_Table) { |
|
| 544 | - /** @var $table_obj EE_Secondary_Table */ |
|
| 545 | - $table_obj->_construct_finalize_set_table_to_join_with($this->_get_main_table()); |
|
| 546 | - } |
|
| 547 | - } |
|
| 548 | - /** |
|
| 549 | - * Filters the list of fields on a model. It is best to NOT use this directly and instead just use |
|
| 550 | - * EE_Register_Model_Extension |
|
| 551 | - * |
|
| 552 | - * @param EE_Model_Field_Base[] $_fields |
|
| 553 | - */ |
|
| 554 | - $this->_fields = (array)apply_filters('FHEE__' . get_class($this) . '__construct__fields', $this->_fields); |
|
| 555 | - $this->_invalidate_field_caches(); |
|
| 556 | - foreach ($this->_fields as $table_alias => $fields_for_table) { |
|
| 557 | - if (! array_key_exists($table_alias, $this->_tables)) { |
|
| 558 | - throw new EE_Error(sprintf(__("Table alias %s does not exist in EEM_Base child's _tables array. Only tables defined are %s", |
|
| 559 | - 'event_espresso'), $table_alias, implode(",", $this->_fields))); |
|
| 560 | - } |
|
| 561 | - foreach ($fields_for_table as $field_name => $field_obj) { |
|
| 562 | - /** @var $field_obj EE_Model_Field_Base | EE_Primary_Key_Field_Base */ |
|
| 563 | - //primary key field base has a slightly different _construct_finalize |
|
| 564 | - /** @var $field_obj EE_Model_Field_Base */ |
|
| 565 | - $field_obj->_construct_finalize($table_alias, $field_name, $this->get_this_model_name()); |
|
| 566 | - } |
|
| 567 | - } |
|
| 568 | - // everything is related to Extra_Meta |
|
| 569 | - if (get_class($this) !== 'EEM_Extra_Meta') { |
|
| 570 | - //make extra meta related to everything, but don't block deleting things just |
|
| 571 | - //because they have related extra meta info. For now just orphan those extra meta |
|
| 572 | - //in the future we should automatically delete them |
|
| 573 | - $this->_model_relations['Extra_Meta'] = new EE_Has_Many_Any_Relation(false); |
|
| 574 | - } |
|
| 575 | - //and change logs |
|
| 576 | - if (get_class($this) !== 'EEM_Change_Log') { |
|
| 577 | - $this->_model_relations['Change_Log'] = new EE_Has_Many_Any_Relation(false); |
|
| 578 | - } |
|
| 579 | - /** |
|
| 580 | - * Filters the list of relations 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_Relation_Base[] $_model_relations |
|
| 584 | - */ |
|
| 585 | - $this->_model_relations = (array)apply_filters('FHEE__' . get_class($this) . '__construct__model_relations', |
|
| 586 | - $this->_model_relations); |
|
| 587 | - foreach ($this->_model_relations as $model_name => $relation_obj) { |
|
| 588 | - /** @var $relation_obj EE_Model_Relation_Base */ |
|
| 589 | - $relation_obj->_construct_finalize_set_models($this->get_this_model_name(), $model_name); |
|
| 590 | - } |
|
| 591 | - foreach ($this->_indexes as $index_name => $index_obj) { |
|
| 592 | - /** @var $index_obj EE_Index */ |
|
| 593 | - $index_obj->_construct_finalize($index_name, $this->get_this_model_name()); |
|
| 594 | - } |
|
| 595 | - $this->set_timezone($timezone); |
|
| 596 | - //finalize default where condition strategy, or set default |
|
| 597 | - if (! $this->_default_where_conditions_strategy) { |
|
| 598 | - //nothing was set during child constructor, so set default |
|
| 599 | - $this->_default_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
| 600 | - } |
|
| 601 | - $this->_default_where_conditions_strategy->_finalize_construct($this); |
|
| 602 | - if (! $this->_minimum_where_conditions_strategy) { |
|
| 603 | - //nothing was set during child constructor, so set default |
|
| 604 | - $this->_minimum_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
| 605 | - } |
|
| 606 | - $this->_minimum_where_conditions_strategy->_finalize_construct($this); |
|
| 607 | - //if the cap slug hasn't been set, and we haven't set it to false on purpose |
|
| 608 | - //to indicate to NOT set it, set it to the logical default |
|
| 609 | - if ($this->_caps_slug === null) { |
|
| 610 | - $this->_caps_slug = EEH_Inflector::pluralize_and_lower($this->get_this_model_name()); |
|
| 611 | - } |
|
| 612 | - //initialize the standard cap restriction generators if none were specified by the child constructor |
|
| 613 | - if ($this->_cap_restriction_generators !== false) { |
|
| 614 | - foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
|
| 615 | - if (! isset($this->_cap_restriction_generators[$cap_context])) { |
|
| 616 | - $this->_cap_restriction_generators[$cap_context] = apply_filters( |
|
| 617 | - 'FHEE__EEM_Base___construct__standard_cap_restriction_generator', |
|
| 618 | - new EE_Restriction_Generator_Protected(), |
|
| 619 | - $cap_context, |
|
| 620 | - $this |
|
| 621 | - ); |
|
| 622 | - } |
|
| 623 | - } |
|
| 624 | - } |
|
| 625 | - //if there are cap restriction generators, use them to make the default cap restrictions |
|
| 626 | - if ($this->_cap_restriction_generators !== false) { |
|
| 627 | - foreach ($this->_cap_restriction_generators as $context => $generator_object) { |
|
| 628 | - if (! $generator_object) { |
|
| 629 | - continue; |
|
| 630 | - } |
|
| 631 | - if (! $generator_object instanceof EE_Restriction_Generator_Base) { |
|
| 632 | - throw new EE_Error( |
|
| 633 | - sprintf( |
|
| 634 | - __('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.', |
|
| 635 | - 'event_espresso'), |
|
| 636 | - $context, |
|
| 637 | - $this->get_this_model_name() |
|
| 638 | - ) |
|
| 639 | - ); |
|
| 640 | - } |
|
| 641 | - $action = $this->cap_action_for_context($context); |
|
| 642 | - if (! $generator_object->construction_finalized()) { |
|
| 643 | - $generator_object->_construct_finalize($this, $action); |
|
| 644 | - } |
|
| 645 | - } |
|
| 646 | - } |
|
| 647 | - do_action('AHEE__' . get_class($this) . '__construct__end'); |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - |
|
| 651 | - |
|
| 652 | - /** |
|
| 653 | - * Used to set the $_model_query_blog_id static property. |
|
| 654 | - * |
|
| 655 | - * @param int $blog_id If provided then will set the blog_id for the models to this id. If not provided then the |
|
| 656 | - * value for get_current_blog_id() will be used. |
|
| 657 | - */ |
|
| 658 | - public static function set_model_query_blog_id($blog_id = 0) |
|
| 659 | - { |
|
| 660 | - EEM_Base::$_model_query_blog_id = $blog_id > 0 ? (int)$blog_id : get_current_blog_id(); |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - |
|
| 664 | - |
|
| 665 | - /** |
|
| 666 | - * Returns whatever is set as the internal $model_query_blog_id. |
|
| 667 | - * |
|
| 668 | - * @return int |
|
| 669 | - */ |
|
| 670 | - public static function get_model_query_blog_id() |
|
| 671 | - { |
|
| 672 | - return EEM_Base::$_model_query_blog_id; |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - |
|
| 676 | - |
|
| 677 | - /** |
|
| 678 | - * This function is a singleton method used to instantiate the Espresso_model object |
|
| 679 | - * |
|
| 680 | - * @param string $timezone string representing the timezone we want to set for returned Date Time Strings |
|
| 681 | - * (and any incoming timezone data that gets saved). |
|
| 682 | - * Note this just sends the timezone info to the date time model field objects. |
|
| 683 | - * Default is NULL |
|
| 684 | - * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
|
| 685 | - * @return static (as in the concrete child class) |
|
| 686 | - * @throws EE_Error |
|
| 687 | - * @throws InvalidArgumentException |
|
| 688 | - * @throws InvalidDataTypeException |
|
| 689 | - * @throws InvalidInterfaceException |
|
| 690 | - */ |
|
| 691 | - public static function instance($timezone = null) |
|
| 692 | - { |
|
| 693 | - // check if instance of Espresso_model already exists |
|
| 694 | - if (! static::$_instance instanceof static) { |
|
| 695 | - // instantiate Espresso_model |
|
| 696 | - static::$_instance = new static( |
|
| 697 | - $timezone, |
|
| 698 | - LoaderFactory::getLoader()->load('EventEspresso\core\services\orm\ModelFieldFactory') |
|
| 699 | - ); |
|
| 700 | - } |
|
| 701 | - //we might have a timezone set, let set_timezone decide what to do with it |
|
| 702 | - static::$_instance->set_timezone($timezone); |
|
| 703 | - // Espresso_model object |
|
| 704 | - return static::$_instance; |
|
| 705 | - } |
|
| 706 | - |
|
| 707 | - |
|
| 708 | - |
|
| 709 | - /** |
|
| 710 | - * resets the model and returns it |
|
| 711 | - * |
|
| 712 | - * @param null | string $timezone |
|
| 713 | - * @return EEM_Base|null (if the model was already instantiated, returns it, with |
|
| 714 | - * all its properties reset; if it wasn't instantiated, returns null) |
|
| 715 | - * @throws EE_Error |
|
| 716 | - * @throws ReflectionException |
|
| 717 | - * @throws InvalidArgumentException |
|
| 718 | - * @throws InvalidDataTypeException |
|
| 719 | - * @throws InvalidInterfaceException |
|
| 720 | - */ |
|
| 721 | - public static function reset($timezone = null) |
|
| 722 | - { |
|
| 723 | - if (static::$_instance instanceof EEM_Base) { |
|
| 724 | - //let's try to NOT swap out the current instance for a new one |
|
| 725 | - //because if someone has a reference to it, we can't remove their reference |
|
| 726 | - //so it's best to keep using the same reference, but change the original object |
|
| 727 | - //reset all its properties to their original values as defined in the class |
|
| 728 | - $r = new ReflectionClass(get_class(static::$_instance)); |
|
| 729 | - $static_properties = $r->getStaticProperties(); |
|
| 730 | - foreach ($r->getDefaultProperties() as $property => $value) { |
|
| 731 | - //don't set instance to null like it was originally, |
|
| 732 | - //but it's static anyways, and we're ignoring static properties (for now at least) |
|
| 733 | - if (! isset($static_properties[$property])) { |
|
| 734 | - static::$_instance->{$property} = $value; |
|
| 735 | - } |
|
| 736 | - } |
|
| 737 | - //and then directly call its constructor again, like we would if we were creating a new one |
|
| 738 | - static::$_instance->__construct( |
|
| 739 | - $timezone, |
|
| 740 | - LoaderFactory::getLoader()->load('EventEspresso\core\services\orm\ModelFieldFactory') |
|
| 741 | - ); |
|
| 742 | - return self::instance(); |
|
| 743 | - } |
|
| 744 | - return null; |
|
| 745 | - } |
|
| 746 | - |
|
| 747 | - |
|
| 748 | - |
|
| 749 | - /** |
|
| 750 | - * @return LoaderInterface |
|
| 751 | - * @throws InvalidArgumentException |
|
| 752 | - * @throws InvalidDataTypeException |
|
| 753 | - * @throws InvalidInterfaceException |
|
| 754 | - */ |
|
| 755 | - private static function getLoader() |
|
| 756 | - { |
|
| 757 | - if(! EEM_Base::$loader instanceof LoaderInterface) { |
|
| 758 | - EEM_Base::$loader = LoaderFactory::getLoader(); |
|
| 759 | - } |
|
| 760 | - return EEM_Base::$loader; |
|
| 761 | - } |
|
| 762 | - |
|
| 763 | - |
|
| 764 | - |
|
| 765 | - /** |
|
| 766 | - * retrieve the status details from esp_status table as an array IF this model has the status table as a relation. |
|
| 767 | - * |
|
| 768 | - * @param boolean $translated return localized strings or JUST the array. |
|
| 769 | - * @return array |
|
| 770 | - * @throws EE_Error |
|
| 771 | - * @throws InvalidArgumentException |
|
| 772 | - * @throws InvalidDataTypeException |
|
| 773 | - * @throws InvalidInterfaceException |
|
| 774 | - */ |
|
| 775 | - public function status_array($translated = false) |
|
| 776 | - { |
|
| 777 | - if (! array_key_exists('Status', $this->_model_relations)) { |
|
| 778 | - return array(); |
|
| 779 | - } |
|
| 780 | - $model_name = $this->get_this_model_name(); |
|
| 781 | - $status_type = str_replace(' ', '_', strtolower(str_replace('_', ' ', $model_name))); |
|
| 782 | - $stati = EEM_Status::instance()->get_all(array(array('STS_type' => $status_type))); |
|
| 783 | - $status_array = array(); |
|
| 784 | - foreach ($stati as $status) { |
|
| 785 | - $status_array[$status->ID()] = $status->get('STS_code'); |
|
| 786 | - } |
|
| 787 | - return $translated |
|
| 788 | - ? EEM_Status::instance()->localized_status($status_array, false, 'sentence') |
|
| 789 | - : $status_array; |
|
| 790 | - } |
|
| 791 | - |
|
| 792 | - |
|
| 793 | - |
|
| 794 | - /** |
|
| 795 | - * Gets all the EE_Base_Class objects which match the $query_params, by querying the DB. |
|
| 796 | - * |
|
| 797 | - * @param array $query_params { |
|
| 798 | - * @var array $0 (where) array { |
|
| 799 | - * eg: array('QST_display_text'=>'Are you bob?','QST_admin_text'=>'Determine |
|
| 800 | - * if user is bob') becomes SQL >> "...WHERE QST_display_text = 'Are you |
|
| 801 | - * bob?' AND QST_admin_text = 'Determine if user is bob'...") To add WHERE |
|
| 802 | - * conditions based on related models (and even |
|
| 803 | - * models-related-to-related-models) prepend the model's name onto the field |
|
| 804 | - * name. Eg, |
|
| 805 | - * EEM_Event::instance()->get_all(array(array('Venue.VNU_ID'=>12))); becomes |
|
| 806 | - * SQL >> "SELECT * FROM wp_posts AS Event_CPT LEFT JOIN wp_esp_event_meta |
|
| 807 | - * AS Event_Meta ON Event_CPT.ID = Event_Meta.EVT_ID LEFT JOIN |
|
| 808 | - * wp_esp_event_venue AS Event_Venue ON Event_Venue.EVT_ID=Event_CPT.ID LEFT |
|
| 809 | - * JOIN wp_posts AS Venue_CPT ON Venue_CPT.ID=Event_Venue.VNU_ID LEFT JOIN |
|
| 810 | - * wp_esp_venue_meta AS Venue_Meta ON Venue_CPT.ID = Venue_Meta.VNU_ID WHERE |
|
| 811 | - * Venue_CPT.ID = 12 Notice that automatically took care of joining Events |
|
| 812 | - * to Venues (even when each of those models actually consisted of two |
|
| 813 | - * tables). Also, you may chain the model relations together. Eg instead of |
|
| 814 | - * just having |
|
| 815 | - * "Venue.VNU_ID", you could have |
|
| 816 | - * "Registration.Attendee.ATT_ID" as a field on a query for events (because |
|
| 817 | - * events are related to Registrations, which are related to Attendees). You |
|
| 818 | - * can take it even further with |
|
| 819 | - * "Registration.Transaction.Payment.PAY_amount" etc. To change the operator |
|
| 820 | - * (from the default of '='), change the value to an numerically-indexed |
|
| 821 | - * array, where the first item in the list is the operator. eg: array( |
|
| 822 | - * 'QST_display_text' => array('LIKE','%bob%'), 'QST_ID' => array('<',34), |
|
| 823 | - * 'QST_wp_user' => array('in',array(1,2,7,23))) becomes SQL >> "...WHERE |
|
| 824 | - * QST_display_text LIKE '%bob%' AND QST_ID < 34 AND QST_wp_user IN |
|
| 825 | - * (1,2,7,23)...". Valid operators so far: =, !=, <, <=, >, >=, LIKE, NOT |
|
| 826 | - * LIKE, IN (followed by numeric-indexed array), NOT IN (dido), BETWEEN |
|
| 827 | - * (followed by an array with exactly 2 date strings), IS NULL, and IS NOT |
|
| 828 | - * NULL Values can be a string, int, or float. They can also be arrays IFF |
|
| 829 | - * the operator is IN. Also, values can actually be field names. To indicate |
|
| 830 | - * the value is a field, simply provide a third array item (true) to the |
|
| 831 | - * operator-value array like so: eg: array( 'DTT_reg_limit' => array('>', |
|
| 832 | - * 'DTT_sold', TRUE) ) becomes SQL >> "...WHERE DTT_reg_limit > DTT_sold" |
|
| 833 | - * Note: you can also use related model field names like you would any other |
|
| 834 | - * field name. eg: |
|
| 835 | - * array('Datetime.DTT_reg_limit'=>array('=','Datetime.DTT_sold',TRUE) could |
|
| 836 | - * be used if you were querying EEM_Tickets (because Datetime is directly related to tickets) Also, by default all the where conditions are AND'd together. To override this, add an array key 'OR' (or 'AND') and the array to be OR'd together eg: array('OR'=>array('TXN_ID' => 23 , 'TXN_timestamp__>' => |
|
| 837 | - * 345678912)) becomes SQL >> "...WHERE TXN_ID = 23 OR TXN_timestamp = |
|
| 838 | - * 345678912...". Also, to negate an entire set of conditions, use 'NOT' as |
|
| 839 | - * an array key. eg: array('NOT'=>array('TXN_total' => |
|
| 840 | - * 50, 'TXN_paid'=>23) becomes SQL >> "...where ! (TXN_total =50 AND |
|
| 841 | - * TXN_paid =23) Note: the 'glue' used to join each condition will continue |
|
| 842 | - * to be what you last specified. IE, "AND"s by default, but if you had |
|
| 843 | - * previously specified to use ORs to join, ORs will continue to be used. |
|
| 844 | - * So, if you specify to use an "OR" to join conditions, it will continue to |
|
| 845 | - * "stick" until you specify an AND. eg |
|
| 846 | - * array('OR'=>array('NOT'=>array('TXN_total' => 50, |
|
| 847 | - * 'TXN_paid'=>23)),AND=>array('TXN_ID'=>1,'STS_ID'=>'TIN') becomes SQL >> |
|
| 848 | - * "...where ! (TXN_total =50 OR TXN_paid =23) AND TXN_ID=1 AND |
|
| 849 | - * STS_ID='TIN'" They can be nested indefinitely. eg: |
|
| 850 | - * array('OR'=>array('TXN_total' => 23, 'NOT'=> array( 'TXN_timestamp'=> 345678912, 'AND'=>array('TXN_paid' => 53, 'STS_ID' => 'TIN')))) becomes SQL >> "...WHERE TXN_total = 23 OR ! (TXN_timestamp = 345678912 OR (TXN_paid = 53 AND STS_ID = 'TIN'))..." GOTCHA: because this is an array, array keys must be unique, making it impossible to place two or more where conditions applying to the same field. eg: array('PAY_timestamp'=>array('>',$start_date),'PAY_timestamp'=>array('<',$end_date),'PAY_timestamp'=>array('!=',$special_date)), as PHP enforces that the array keys must be unique, thus removing the first two array entries with key 'PAY_timestamp'. becomes SQL >> "PAY_timestamp != 4234232", ignoring the first two PAY_timestamp conditions). To overcome this, you can add a '*' character to the end of the field's name, followed by anything. These will be removed when generating the SQL string, but allow for the array keys to be unique. eg: you could rewrite the previous query as: array('PAY_timestamp'=>array('>',$start_date),'PAY_timestamp*1st'=>array('<',$end_date),'PAY_timestamp*2nd'=>array('!=',$special_date)) which correctly becomes SQL >> |
|
| 851 | - * "PAY_timestamp > 123412341 AND PAY_timestamp < 2354235235234 AND |
|
| 852 | - * PAY_timestamp != 1241234123" This can be applied to condition operators |
|
| 853 | - * too, eg: |
|
| 854 | - * array('OR'=>array('REG_ID'=>3,'Transaction.TXN_ID'=>23),'OR*whatever'=>array('Attendee.ATT_fname'=>'bob','Attendee.ATT_lname'=>'wilson'))); |
|
| 855 | - * @var mixed $limit int|array adds a limit to the query just like the SQL limit clause, so |
|
| 856 | - * limits of "23", "25,50", and array(23,42) are all valid would become SQL |
|
| 857 | - * "...LIMIT 23", "...LIMIT 25,50", and "...LIMIT 23,42" respectively. |
|
| 858 | - * Remember when you provide two numbers for the limit, the 1st number is |
|
| 859 | - * the OFFSET, the 2nd is the LIMIT |
|
| 860 | - * @var array $on_join_limit allows the setting of a special select join with a internal limit so you |
|
| 861 | - * can do paging on one-to-many multi-table-joins. Send an array in the |
|
| 862 | - * following format array('on_join_limit' |
|
| 863 | - * => array( 'table_alias', array(1,2) ) ). |
|
| 864 | - * @var mixed $order_by name of a column to order by, or an array where keys are field names and |
|
| 865 | - * values are either 'ASC' or 'DESC'. |
|
| 866 | - * 'limit'=>array('STS_ID'=>'ASC','REG_date'=>'DESC'), which would becomes |
|
| 867 | - * SQL "...ORDER BY TXN_timestamp..." and "...ORDER BY STS_ID ASC, REG_date |
|
| 868 | - * DESC..." respectively. Like the |
|
| 869 | - * 'where' conditions, these fields can be on related models. Eg |
|
| 870 | - * 'order_by'=>array('Registration.Transaction.TXN_amount'=>'ASC') is |
|
| 871 | - * perfectly valid from any model related to 'Registration' (like Event, |
|
| 872 | - * Attendee, Price, Datetime, etc.) |
|
| 873 | - * @var string $order If 'order_by' is used and its value is a string (NOT an array), then |
|
| 874 | - * 'order' specifies whether to order the field specified in 'order_by' in |
|
| 875 | - * ascending or descending order. Acceptable values are 'ASC' or 'DESC'. If, |
|
| 876 | - * 'order_by' isn't used, but 'order' is, then it is assumed you want to |
|
| 877 | - * order by the primary key. Eg, |
|
| 878 | - * EEM_Event::instance()->get_all(array('order_by'=>'Datetime.DTT_EVT_start','order'=>'ASC'); |
|
| 879 | - * //(will join with the Datetime model's table(s) and order by its field |
|
| 880 | - * DTT_EVT_start) or |
|
| 881 | - * EEM_Registration::instance()->get_all(array('order'=>'ASC'));//will make |
|
| 882 | - * SQL "SELECT * FROM wp_esp_registration ORDER BY REG_ID ASC" |
|
| 883 | - * @var mixed $group_by name of field to order by, or an array of fields. Eg either |
|
| 884 | - * 'group_by'=>'VNU_ID', or |
|
| 885 | - * 'group_by'=>array('EVT_name','Registration.Transaction.TXN_total') Note: |
|
| 886 | - * if no |
|
| 887 | - * $group_by is specified, and a limit is set, automatically groups by the |
|
| 888 | - * model's primary key (or combined primary keys). This avoids some |
|
| 889 | - * weirdness that results when using limits, tons of joins, and no group by, |
|
| 890 | - * see https://events.codebasehq.com/projects/event-espresso/tickets/9389 |
|
| 891 | - * @var array $having exactly like WHERE parameters array, except these conditions apply to the |
|
| 892 | - * grouped results (whereas WHERE conditions apply to the pre-grouped |
|
| 893 | - * results) |
|
| 894 | - * @var array $force_join forces a join with the models named. Should be a numerically-indexed |
|
| 895 | - * array where values are models to be joined in the query.Eg |
|
| 896 | - * array('Attendee','Payment','Datetime'). You may join with transient |
|
| 897 | - * models using period, eg "Registration.Transaction.Payment". You will |
|
| 898 | - * probably only want to do this in hopes of increasing efficiency, as |
|
| 899 | - * related models which belongs to the current model |
|
| 900 | - * (ie, the current model has a foreign key to them, like how Registration |
|
| 901 | - * belongs to Attendee) can be cached in order to avoid future queries |
|
| 902 | - * @var string $default_where_conditions can be set to 'none', 'this_model_only', 'other_models_only', or 'all'. |
|
| 903 | - * set this to 'none' to disable all default where conditions. Eg, usually |
|
| 904 | - * soft-deleted objects are filtered-out if you want to include them, set |
|
| 905 | - * this query param to 'none'. If you want to ONLY disable THIS model's |
|
| 906 | - * default where conditions set it to 'other_models_only'. If you only want |
|
| 907 | - * this model's default where conditions added to the query, use |
|
| 908 | - * 'this_model_only'. If you want to use all default where conditions |
|
| 909 | - * (default), set to 'all'. |
|
| 910 | - * @var string $caps controls what capability requirements to apply to the query; ie, should |
|
| 911 | - * we just NOT apply any capabilities/permissions/restrictions and return |
|
| 912 | - * everything? Or should we only show the current user items they should be |
|
| 913 | - * able to view on the frontend, backend, edit, or delete? can be set to |
|
| 914 | - * 'none' (default), 'read_frontend', 'read_backend', 'edit' or 'delete' |
|
| 915 | - * } |
|
| 916 | - * @return EE_Base_Class[] *note that there is NO option to pass the output type. If you want results different |
|
| 917 | - * from EE_Base_Class[], use _get_all_wpdb_results()and make it public |
|
| 918 | - * again. Array keys are object IDs (if there is a primary key on the model. |
|
| 919 | - * if not, numerically indexed) Some full examples: get 10 transactions |
|
| 920 | - * which have Scottish attendees: EEM_Transaction::instance()->get_all( |
|
| 921 | - * array( array( |
|
| 922 | - * 'OR'=>array( |
|
| 923 | - * 'Registration.Attendee.ATT_fname'=>array('like','Mc%'), |
|
| 924 | - * 'Registration.Attendee.ATT_fname*other'=>array('like','Mac%') |
|
| 925 | - * ) |
|
| 926 | - * ), |
|
| 927 | - * 'limit'=>10, |
|
| 928 | - * 'group_by'=>'TXN_ID' |
|
| 929 | - * )); |
|
| 930 | - * get all the answers to the question titled "shirt size" for event with id |
|
| 931 | - * 12, ordered by their answer EEM_Answer::instance()->get_all(array( array( |
|
| 932 | - * 'Question.QST_display_text'=>'shirt size', |
|
| 933 | - * 'Registration.Event.EVT_ID'=>12 |
|
| 934 | - * ), |
|
| 935 | - * 'order_by'=>array('ANS_value'=>'ASC') |
|
| 936 | - * )); |
|
| 937 | - * @throws EE_Error |
|
| 938 | - */ |
|
| 939 | - public function get_all($query_params = array()) |
|
| 940 | - { |
|
| 941 | - if (isset($query_params['limit']) |
|
| 942 | - && ! isset($query_params['group_by']) |
|
| 943 | - ) { |
|
| 944 | - $query_params['group_by'] = array_keys($this->get_combined_primary_key_fields()); |
|
| 945 | - } |
|
| 946 | - return $this->_create_objects($this->_get_all_wpdb_results($query_params, ARRAY_A, null)); |
|
| 947 | - } |
|
| 948 | - |
|
| 949 | - |
|
| 950 | - |
|
| 951 | - /** |
|
| 952 | - * Modifies the query parameters so we only get back model objects |
|
| 953 | - * that "belong" to the current user |
|
| 954 | - * |
|
| 955 | - * @param array $query_params @see EEM_Base::get_all() |
|
| 956 | - * @return array like EEM_Base::get_all |
|
| 957 | - */ |
|
| 958 | - public function alter_query_params_to_only_include_mine($query_params = array()) |
|
| 959 | - { |
|
| 960 | - $wp_user_field_name = $this->wp_user_field_name(); |
|
| 961 | - if ($wp_user_field_name) { |
|
| 962 | - $query_params[0][$wp_user_field_name] = get_current_user_id(); |
|
| 963 | - } |
|
| 964 | - return $query_params; |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - |
|
| 968 | - |
|
| 969 | - /** |
|
| 970 | - * Returns the name of the field's name that points to the WP_User table |
|
| 971 | - * on this model (or follows the _model_chain_to_wp_user and uses that model's |
|
| 972 | - * foreign key to the WP_User table) |
|
| 973 | - * |
|
| 974 | - * @return string|boolean string on success, boolean false when there is no |
|
| 975 | - * foreign key to the WP_User table |
|
| 976 | - */ |
|
| 977 | - public function wp_user_field_name() |
|
| 978 | - { |
|
| 979 | - try { |
|
| 980 | - if (! empty($this->_model_chain_to_wp_user)) { |
|
| 981 | - $models_to_follow_to_wp_users = explode('.', $this->_model_chain_to_wp_user); |
|
| 982 | - $last_model_name = end($models_to_follow_to_wp_users); |
|
| 983 | - $model_with_fk_to_wp_users = EE_Registry::instance()->load_model($last_model_name); |
|
| 984 | - $model_chain_to_wp_user = $this->_model_chain_to_wp_user . '.'; |
|
| 985 | - } else { |
|
| 986 | - $model_with_fk_to_wp_users = $this; |
|
| 987 | - $model_chain_to_wp_user = ''; |
|
| 988 | - } |
|
| 989 | - $wp_user_field = $model_with_fk_to_wp_users->get_foreign_key_to('WP_User'); |
|
| 990 | - return $model_chain_to_wp_user . $wp_user_field->get_name(); |
|
| 991 | - } catch (EE_Error $e) { |
|
| 992 | - return false; |
|
| 993 | - } |
|
| 994 | - } |
|
| 995 | - |
|
| 996 | - |
|
| 997 | - |
|
| 998 | - /** |
|
| 999 | - * Returns the _model_chain_to_wp_user string, which indicates which related model |
|
| 1000 | - * (or transiently-related model) has a foreign key to the wp_users table; |
|
| 1001 | - * useful for finding if model objects of this type are 'owned' by the current user. |
|
| 1002 | - * This is an empty string when the foreign key is on this model and when it isn't, |
|
| 1003 | - * but is only non-empty when this model's ownership is indicated by a RELATED model |
|
| 1004 | - * (or transiently-related model) |
|
| 1005 | - * |
|
| 1006 | - * @return string |
|
| 1007 | - */ |
|
| 1008 | - public function model_chain_to_wp_user() |
|
| 1009 | - { |
|
| 1010 | - return $this->_model_chain_to_wp_user; |
|
| 1011 | - } |
|
| 1012 | - |
|
| 1013 | - |
|
| 1014 | - |
|
| 1015 | - /** |
|
| 1016 | - * Whether this model is 'owned' by a specific wordpress user (even indirectly, |
|
| 1017 | - * like how registrations don't have a foreign key to wp_users, but the |
|
| 1018 | - * events they are for are), or is unrelated to wp users. |
|
| 1019 | - * generally available |
|
| 1020 | - * |
|
| 1021 | - * @return boolean |
|
| 1022 | - */ |
|
| 1023 | - public function is_owned() |
|
| 1024 | - { |
|
| 1025 | - if ($this->model_chain_to_wp_user()) { |
|
| 1026 | - return true; |
|
| 1027 | - } |
|
| 1028 | - try { |
|
| 1029 | - $this->get_foreign_key_to('WP_User'); |
|
| 1030 | - return true; |
|
| 1031 | - } catch (EE_Error $e) { |
|
| 1032 | - return false; |
|
| 1033 | - } |
|
| 1034 | - } |
|
| 1035 | - |
|
| 1036 | - |
|
| 1037 | - /** |
|
| 1038 | - * Used internally to get WPDB results, because other functions, besides get_all, may want to do some queries, but |
|
| 1039 | - * may want to preserve the WPDB results (eg, update, which first queries to make sure we have all the tables on |
|
| 1040 | - * the model) |
|
| 1041 | - * |
|
| 1042 | - * @param array $query_params like EEM_Base::get_all's $query_params |
|
| 1043 | - * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
| 1044 | - * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
| 1045 | - * fields on the model, and the models we joined to in the query. However, you can |
|
| 1046 | - * override this and set the select to "*", or a specific column name, like |
|
| 1047 | - * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
| 1048 | - * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
| 1049 | - * the aliases used to refer to this selection, and values are to be |
|
| 1050 | - * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
| 1051 | - * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
| 1052 | - * @return array | stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
| 1053 | - * @throws EE_Error |
|
| 1054 | - * @throws InvalidArgumentException |
|
| 1055 | - */ |
|
| 1056 | - protected function _get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
|
| 1057 | - { |
|
| 1058 | - $this->_custom_selections = $this->getCustomSelection($query_params, $columns_to_select);; |
|
| 1059 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1060 | - $select_expressions = $columns_to_select === null |
|
| 1061 | - ? $this->_construct_default_select_sql($model_query_info) |
|
| 1062 | - : ''; |
|
| 1063 | - if ($this->_custom_selections instanceof CustomSelects) { |
|
| 1064 | - $custom_expressions = $this->_custom_selections->columnsToSelectExpression(); |
|
| 1065 | - $select_expressions .= $select_expressions |
|
| 1066 | - ? ', ' . $custom_expressions |
|
| 1067 | - : $custom_expressions; |
|
| 1068 | - } |
|
| 1069 | - |
|
| 1070 | - $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 1071 | - return $this->_do_wpdb_query('get_results', array($SQL, $output)); |
|
| 1072 | - } |
|
| 1073 | - |
|
| 1074 | - |
|
| 1075 | - /** |
|
| 1076 | - * Get a CustomSelects object if the $query_params or $columns_to_select allows for it. |
|
| 1077 | - * Note: $query_params['extra_selects'] will always override any $columns_to_select values. It is the preferred |
|
| 1078 | - * method of including extra select information. |
|
| 1079 | - * |
|
| 1080 | - * @param array $query_params |
|
| 1081 | - * @param null|array|string $columns_to_select |
|
| 1082 | - * @return null|CustomSelects |
|
| 1083 | - * @throws InvalidArgumentException |
|
| 1084 | - */ |
|
| 1085 | - protected function getCustomSelection(array $query_params, $columns_to_select = null) |
|
| 1086 | - { |
|
| 1087 | - if (! isset($query_params['extra_selects']) && $columns_to_select === null) { |
|
| 1088 | - return null; |
|
| 1089 | - } |
|
| 1090 | - $selects = isset($query_params['extra_selects']) ? $query_params['extra_selects'] : $columns_to_select; |
|
| 1091 | - $selects = is_string($selects) ? explode(',', $selects) : $selects; |
|
| 1092 | - return new CustomSelects($selects); |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - |
|
| 1096 | - |
|
| 1097 | - /** |
|
| 1098 | - * Gets an array of rows from the database just like $wpdb->get_results would, |
|
| 1099 | - * but you can use the $query_params like on EEM_Base::get_all() to more easily |
|
| 1100 | - * take care of joins, field preparation etc. |
|
| 1101 | - * |
|
| 1102 | - * @param array $query_params like EEM_Base::get_all's $query_params |
|
| 1103 | - * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
| 1104 | - * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
| 1105 | - * fields on the model, and the models we joined to in the query. However, you can |
|
| 1106 | - * override this and set the select to "*", or a specific column name, like |
|
| 1107 | - * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
| 1108 | - * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
| 1109 | - * the aliases used to refer to this selection, and values are to be |
|
| 1110 | - * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
| 1111 | - * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
| 1112 | - * @return array|stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
| 1113 | - * @throws EE_Error |
|
| 1114 | - */ |
|
| 1115 | - public function get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
|
| 1116 | - { |
|
| 1117 | - return $this->_get_all_wpdb_results($query_params, $output, $columns_to_select); |
|
| 1118 | - } |
|
| 1119 | - |
|
| 1120 | - |
|
| 1121 | - |
|
| 1122 | - /** |
|
| 1123 | - * For creating a custom select statement |
|
| 1124 | - * |
|
| 1125 | - * @param mixed $columns_to_select either a string to be inserted directly as the select statement, |
|
| 1126 | - * or an array where keys are aliases, and values are arrays where 0=>the selection |
|
| 1127 | - * SQL, and 1=>is the datatype |
|
| 1128 | - * @throws EE_Error |
|
| 1129 | - * @return string |
|
| 1130 | - */ |
|
| 1131 | - private function _construct_select_from_input($columns_to_select) |
|
| 1132 | - { |
|
| 1133 | - if (is_array($columns_to_select)) { |
|
| 1134 | - $select_sql_array = array(); |
|
| 1135 | - foreach ($columns_to_select as $alias => $selection_and_datatype) { |
|
| 1136 | - if (! is_array($selection_and_datatype) || ! isset($selection_and_datatype[1])) { |
|
| 1137 | - throw new EE_Error( |
|
| 1138 | - sprintf( |
|
| 1139 | - __( |
|
| 1140 | - "Custom selection %s (alias %s) needs to be an array like array('COUNT(REG_ID)','%%d')", |
|
| 1141 | - 'event_espresso' |
|
| 1142 | - ), |
|
| 1143 | - $selection_and_datatype, |
|
| 1144 | - $alias |
|
| 1145 | - ) |
|
| 1146 | - ); |
|
| 1147 | - } |
|
| 1148 | - if (! in_array($selection_and_datatype[1], $this->_valid_wpdb_data_types, true)) { |
|
| 1149 | - throw new EE_Error( |
|
| 1150 | - sprintf( |
|
| 1151 | - esc_html__( |
|
| 1152 | - "Datatype %s (for selection '%s' and alias '%s') is not a valid wpdb datatype (eg %%s)", |
|
| 1153 | - 'event_espresso' |
|
| 1154 | - ), |
|
| 1155 | - $selection_and_datatype[1], |
|
| 1156 | - $selection_and_datatype[0], |
|
| 1157 | - $alias, |
|
| 1158 | - implode(', ', $this->_valid_wpdb_data_types) |
|
| 1159 | - ) |
|
| 1160 | - ); |
|
| 1161 | - } |
|
| 1162 | - $select_sql_array[] = "{$selection_and_datatype[0]} AS $alias"; |
|
| 1163 | - } |
|
| 1164 | - $columns_to_select_string = implode(', ', $select_sql_array); |
|
| 1165 | - } else { |
|
| 1166 | - $columns_to_select_string = $columns_to_select; |
|
| 1167 | - } |
|
| 1168 | - return $columns_to_select_string; |
|
| 1169 | - } |
|
| 1170 | - |
|
| 1171 | - |
|
| 1172 | - |
|
| 1173 | - /** |
|
| 1174 | - * Convenient wrapper for getting the primary key field's name. Eg, on Registration, this would be 'REG_ID' |
|
| 1175 | - * |
|
| 1176 | - * @return string |
|
| 1177 | - * @throws EE_Error |
|
| 1178 | - */ |
|
| 1179 | - public function primary_key_name() |
|
| 1180 | - { |
|
| 1181 | - return $this->get_primary_key_field()->get_name(); |
|
| 1182 | - } |
|
| 1183 | - |
|
| 1184 | - |
|
| 1185 | - |
|
| 1186 | - /** |
|
| 1187 | - * Gets a single item for this model from the DB, given only its ID (or null if none is found). |
|
| 1188 | - * If there is no primary key on this model, $id is treated as primary key string |
|
| 1189 | - * |
|
| 1190 | - * @param mixed $id int or string, depending on the type of the model's primary key |
|
| 1191 | - * @return EE_Base_Class |
|
| 1192 | - */ |
|
| 1193 | - public function get_one_by_ID($id) |
|
| 1194 | - { |
|
| 1195 | - if ($this->get_from_entity_map($id)) { |
|
| 1196 | - return $this->get_from_entity_map($id); |
|
| 1197 | - } |
|
| 1198 | - return $this->get_one( |
|
| 1199 | - $this->alter_query_params_to_restrict_by_ID( |
|
| 1200 | - $id, |
|
| 1201 | - array('default_where_conditions' => EEM_Base::default_where_conditions_minimum_all) |
|
| 1202 | - ) |
|
| 1203 | - ); |
|
| 1204 | - } |
|
| 1205 | - |
|
| 1206 | - |
|
| 1207 | - |
|
| 1208 | - /** |
|
| 1209 | - * Alters query parameters to only get items with this ID are returned. |
|
| 1210 | - * Takes into account that the ID might be a string produced by EEM_Base::get_index_primary_key_string(), |
|
| 1211 | - * or could just be a simple primary key ID |
|
| 1212 | - * |
|
| 1213 | - * @param int $id |
|
| 1214 | - * @param array $query_params |
|
| 1215 | - * @return array of normal query params, @see EEM_Base::get_all |
|
| 1216 | - * @throws EE_Error |
|
| 1217 | - */ |
|
| 1218 | - public function alter_query_params_to_restrict_by_ID($id, $query_params = array()) |
|
| 1219 | - { |
|
| 1220 | - if (! isset($query_params[0])) { |
|
| 1221 | - $query_params[0] = array(); |
|
| 1222 | - } |
|
| 1223 | - $conditions_from_id = $this->parse_index_primary_key_string($id); |
|
| 1224 | - if ($conditions_from_id === null) { |
|
| 1225 | - $query_params[0][$this->primary_key_name()] = $id; |
|
| 1226 | - } else { |
|
| 1227 | - //no primary key, so the $id must be from the get_index_primary_key_string() |
|
| 1228 | - $query_params[0] = array_replace_recursive($query_params[0], $this->parse_index_primary_key_string($id)); |
|
| 1229 | - } |
|
| 1230 | - return $query_params; |
|
| 1231 | - } |
|
| 1232 | - |
|
| 1233 | - |
|
| 1234 | - |
|
| 1235 | - /** |
|
| 1236 | - * Gets a single item for this model from the DB, given the $query_params. Only returns a single class, not an |
|
| 1237 | - * array. If no item is found, null is returned. |
|
| 1238 | - * |
|
| 1239 | - * @param array $query_params like EEM_Base's $query_params variable. |
|
| 1240 | - * @return EE_Base_Class|EE_Soft_Delete_Base_Class|NULL |
|
| 1241 | - * @throws EE_Error |
|
| 1242 | - */ |
|
| 1243 | - public function get_one($query_params = array()) |
|
| 1244 | - { |
|
| 1245 | - if (! is_array($query_params)) { |
|
| 1246 | - EE_Error::doing_it_wrong('EEM_Base::get_one', |
|
| 1247 | - sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1248 | - gettype($query_params)), '4.6.0'); |
|
| 1249 | - $query_params = array(); |
|
| 1250 | - } |
|
| 1251 | - $query_params['limit'] = 1; |
|
| 1252 | - $items = $this->get_all($query_params); |
|
| 1253 | - if (empty($items)) { |
|
| 1254 | - return null; |
|
| 1255 | - } |
|
| 1256 | - return array_shift($items); |
|
| 1257 | - } |
|
| 1258 | - |
|
| 1259 | - |
|
| 1260 | - |
|
| 1261 | - /** |
|
| 1262 | - * Returns the next x number of items in sequence from the given value as |
|
| 1263 | - * found in the database matching the given query conditions. |
|
| 1264 | - * |
|
| 1265 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1266 | - * @param null $field_to_order_by What field is used for the |
|
| 1267 | - * reference point. |
|
| 1268 | - * @param int $limit How many to return. |
|
| 1269 | - * @param array $query_params Extra conditions on the query. |
|
| 1270 | - * @param null $columns_to_select If left null, then an array of |
|
| 1271 | - * EE_Base_Class objects is returned, |
|
| 1272 | - * otherwise you can indicate just the |
|
| 1273 | - * columns you want returned. |
|
| 1274 | - * @return EE_Base_Class[]|array |
|
| 1275 | - * @throws EE_Error |
|
| 1276 | - */ |
|
| 1277 | - public function next_x( |
|
| 1278 | - $current_field_value, |
|
| 1279 | - $field_to_order_by = null, |
|
| 1280 | - $limit = 1, |
|
| 1281 | - $query_params = array(), |
|
| 1282 | - $columns_to_select = null |
|
| 1283 | - ) { |
|
| 1284 | - return $this->_get_consecutive( |
|
| 1285 | - $current_field_value, |
|
| 1286 | - '>', |
|
| 1287 | - $field_to_order_by, |
|
| 1288 | - $limit, |
|
| 1289 | - $query_params, |
|
| 1290 | - $columns_to_select |
|
| 1291 | - ); |
|
| 1292 | - } |
|
| 1293 | - |
|
| 1294 | - |
|
| 1295 | - |
|
| 1296 | - /** |
|
| 1297 | - * Returns the previous x number of items in sequence from the given value |
|
| 1298 | - * as found in the database matching the given query conditions. |
|
| 1299 | - * |
|
| 1300 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1301 | - * @param null $field_to_order_by What field is used for the |
|
| 1302 | - * reference point. |
|
| 1303 | - * @param int $limit How many to return. |
|
| 1304 | - * @param array $query_params Extra conditions on the query. |
|
| 1305 | - * @param null $columns_to_select If left null, then an array of |
|
| 1306 | - * EE_Base_Class objects is returned, |
|
| 1307 | - * otherwise you can indicate just the |
|
| 1308 | - * columns you want returned. |
|
| 1309 | - * @return EE_Base_Class[]|array |
|
| 1310 | - * @throws EE_Error |
|
| 1311 | - */ |
|
| 1312 | - public function previous_x( |
|
| 1313 | - $current_field_value, |
|
| 1314 | - $field_to_order_by = null, |
|
| 1315 | - $limit = 1, |
|
| 1316 | - $query_params = array(), |
|
| 1317 | - $columns_to_select = null |
|
| 1318 | - ) { |
|
| 1319 | - return $this->_get_consecutive( |
|
| 1320 | - $current_field_value, |
|
| 1321 | - '<', |
|
| 1322 | - $field_to_order_by, |
|
| 1323 | - $limit, |
|
| 1324 | - $query_params, |
|
| 1325 | - $columns_to_select |
|
| 1326 | - ); |
|
| 1327 | - } |
|
| 1328 | - |
|
| 1329 | - |
|
| 1330 | - |
|
| 1331 | - /** |
|
| 1332 | - * Returns the next item in sequence from the given value as found in the |
|
| 1333 | - * database matching the given query conditions. |
|
| 1334 | - * |
|
| 1335 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1336 | - * @param null $field_to_order_by What field is used for the |
|
| 1337 | - * reference point. |
|
| 1338 | - * @param array $query_params Extra conditions on the query. |
|
| 1339 | - * @param null $columns_to_select If left null, then an EE_Base_Class |
|
| 1340 | - * object is returned, otherwise you |
|
| 1341 | - * can indicate just the columns you |
|
| 1342 | - * want and a single array indexed by |
|
| 1343 | - * the columns will be returned. |
|
| 1344 | - * @return EE_Base_Class|null|array() |
|
| 1345 | - * @throws EE_Error |
|
| 1346 | - */ |
|
| 1347 | - public function next( |
|
| 1348 | - $current_field_value, |
|
| 1349 | - $field_to_order_by = null, |
|
| 1350 | - $query_params = array(), |
|
| 1351 | - $columns_to_select = null |
|
| 1352 | - ) { |
|
| 1353 | - $results = $this->_get_consecutive( |
|
| 1354 | - $current_field_value, |
|
| 1355 | - '>', |
|
| 1356 | - $field_to_order_by, |
|
| 1357 | - 1, |
|
| 1358 | - $query_params, |
|
| 1359 | - $columns_to_select |
|
| 1360 | - ); |
|
| 1361 | - return empty($results) ? null : reset($results); |
|
| 1362 | - } |
|
| 1363 | - |
|
| 1364 | - |
|
| 1365 | - |
|
| 1366 | - /** |
|
| 1367 | - * Returns the previous item in sequence from the given value as found in |
|
| 1368 | - * the database matching the given query conditions. |
|
| 1369 | - * |
|
| 1370 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1371 | - * @param null $field_to_order_by What field is used for the |
|
| 1372 | - * reference point. |
|
| 1373 | - * @param array $query_params Extra conditions on the query. |
|
| 1374 | - * @param null $columns_to_select If left null, then an EE_Base_Class |
|
| 1375 | - * object is returned, otherwise you |
|
| 1376 | - * can indicate just the columns you |
|
| 1377 | - * want and a single array indexed by |
|
| 1378 | - * the columns will be returned. |
|
| 1379 | - * @return EE_Base_Class|null|array() |
|
| 1380 | - * @throws EE_Error |
|
| 1381 | - */ |
|
| 1382 | - public function previous( |
|
| 1383 | - $current_field_value, |
|
| 1384 | - $field_to_order_by = null, |
|
| 1385 | - $query_params = array(), |
|
| 1386 | - $columns_to_select = null |
|
| 1387 | - ) { |
|
| 1388 | - $results = $this->_get_consecutive( |
|
| 1389 | - $current_field_value, |
|
| 1390 | - '<', |
|
| 1391 | - $field_to_order_by, |
|
| 1392 | - 1, |
|
| 1393 | - $query_params, |
|
| 1394 | - $columns_to_select |
|
| 1395 | - ); |
|
| 1396 | - return empty($results) ? null : reset($results); |
|
| 1397 | - } |
|
| 1398 | - |
|
| 1399 | - |
|
| 1400 | - |
|
| 1401 | - /** |
|
| 1402 | - * Returns the a consecutive number of items in sequence from the given |
|
| 1403 | - * value as found in the database matching the given query conditions. |
|
| 1404 | - * |
|
| 1405 | - * @param mixed $current_field_value Value used for the reference point. |
|
| 1406 | - * @param string $operand What operand is used for the sequence. |
|
| 1407 | - * @param string $field_to_order_by What field is used for the reference point. |
|
| 1408 | - * @param int $limit How many to return. |
|
| 1409 | - * @param array $query_params Extra conditions on the query. |
|
| 1410 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, |
|
| 1411 | - * otherwise you can indicate just the columns you want returned. |
|
| 1412 | - * @return EE_Base_Class[]|array |
|
| 1413 | - * @throws EE_Error |
|
| 1414 | - */ |
|
| 1415 | - protected function _get_consecutive( |
|
| 1416 | - $current_field_value, |
|
| 1417 | - $operand = '>', |
|
| 1418 | - $field_to_order_by = null, |
|
| 1419 | - $limit = 1, |
|
| 1420 | - $query_params = array(), |
|
| 1421 | - $columns_to_select = null |
|
| 1422 | - ) { |
|
| 1423 | - //if $field_to_order_by is empty then let's assume we're ordering by the primary key. |
|
| 1424 | - if (empty($field_to_order_by)) { |
|
| 1425 | - if ($this->has_primary_key_field()) { |
|
| 1426 | - $field_to_order_by = $this->get_primary_key_field()->get_name(); |
|
| 1427 | - } else { |
|
| 1428 | - if (WP_DEBUG) { |
|
| 1429 | - throw new EE_Error(__('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).', |
|
| 1430 | - 'event_espresso')); |
|
| 1431 | - } |
|
| 1432 | - EE_Error::add_error(__('There was an error with the query.', 'event_espresso')); |
|
| 1433 | - return array(); |
|
| 1434 | - } |
|
| 1435 | - } |
|
| 1436 | - if (! is_array($query_params)) { |
|
| 1437 | - EE_Error::doing_it_wrong('EEM_Base::_get_consecutive', |
|
| 1438 | - sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1439 | - gettype($query_params)), '4.6.0'); |
|
| 1440 | - $query_params = array(); |
|
| 1441 | - } |
|
| 1442 | - //let's add the where query param for consecutive look up. |
|
| 1443 | - $query_params[0][$field_to_order_by] = array($operand, $current_field_value); |
|
| 1444 | - $query_params['limit'] = $limit; |
|
| 1445 | - //set direction |
|
| 1446 | - $incoming_orderby = isset($query_params['order_by']) ? (array)$query_params['order_by'] : array(); |
|
| 1447 | - $query_params['order_by'] = $operand === '>' |
|
| 1448 | - ? array($field_to_order_by => 'ASC') + $incoming_orderby |
|
| 1449 | - : array($field_to_order_by => 'DESC') + $incoming_orderby; |
|
| 1450 | - //if $columns_to_select is empty then that means we're returning EE_Base_Class objects |
|
| 1451 | - if (empty($columns_to_select)) { |
|
| 1452 | - return $this->get_all($query_params); |
|
| 1453 | - } |
|
| 1454 | - //getting just the fields |
|
| 1455 | - return $this->_get_all_wpdb_results($query_params, ARRAY_A, $columns_to_select); |
|
| 1456 | - } |
|
| 1457 | - |
|
| 1458 | - |
|
| 1459 | - |
|
| 1460 | - /** |
|
| 1461 | - * This sets the _timezone property after model object has been instantiated. |
|
| 1462 | - * |
|
| 1463 | - * @param null | string $timezone valid PHP DateTimeZone timezone string |
|
| 1464 | - */ |
|
| 1465 | - public function set_timezone($timezone) |
|
| 1466 | - { |
|
| 1467 | - if ($timezone !== null) { |
|
| 1468 | - $this->_timezone = $timezone; |
|
| 1469 | - } |
|
| 1470 | - //note we need to loop through relations and set the timezone on those objects as well. |
|
| 1471 | - foreach ($this->_model_relations as $relation) { |
|
| 1472 | - $relation->set_timezone($timezone); |
|
| 1473 | - } |
|
| 1474 | - //and finally we do the same for any datetime fields |
|
| 1475 | - foreach ($this->_fields as $field) { |
|
| 1476 | - if ($field instanceof EE_Datetime_Field) { |
|
| 1477 | - $field->set_timezone($timezone); |
|
| 1478 | - } |
|
| 1479 | - } |
|
| 1480 | - } |
|
| 1481 | - |
|
| 1482 | - |
|
| 1483 | - |
|
| 1484 | - /** |
|
| 1485 | - * This just returns whatever is set for the current timezone. |
|
| 1486 | - * |
|
| 1487 | - * @access public |
|
| 1488 | - * @return string |
|
| 1489 | - */ |
|
| 1490 | - public function get_timezone() |
|
| 1491 | - { |
|
| 1492 | - //first validate if timezone is set. If not, then let's set it be whatever is set on the model fields. |
|
| 1493 | - if (empty($this->_timezone)) { |
|
| 1494 | - foreach ($this->_fields as $field) { |
|
| 1495 | - if ($field instanceof EE_Datetime_Field) { |
|
| 1496 | - $this->set_timezone($field->get_timezone()); |
|
| 1497 | - break; |
|
| 1498 | - } |
|
| 1499 | - } |
|
| 1500 | - } |
|
| 1501 | - //if timezone STILL empty then return the default timezone for the site. |
|
| 1502 | - if (empty($this->_timezone)) { |
|
| 1503 | - $this->set_timezone(EEH_DTT_Helper::get_timezone()); |
|
| 1504 | - } |
|
| 1505 | - return $this->_timezone; |
|
| 1506 | - } |
|
| 1507 | - |
|
| 1508 | - |
|
| 1509 | - |
|
| 1510 | - /** |
|
| 1511 | - * This returns the date formats set for the given field name and also ensures that |
|
| 1512 | - * $this->_timezone property is set correctly. |
|
| 1513 | - * |
|
| 1514 | - * @since 4.6.x |
|
| 1515 | - * @param string $field_name The name of the field the formats are being retrieved for. |
|
| 1516 | - * @param bool $pretty Whether to return the pretty formats (true) or not (false). |
|
| 1517 | - * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
| 1518 | - * @return array formats in an array with the date format first, and the time format last. |
|
| 1519 | - */ |
|
| 1520 | - public function get_formats_for($field_name, $pretty = false) |
|
| 1521 | - { |
|
| 1522 | - $field_settings = $this->field_settings_for($field_name); |
|
| 1523 | - //if not a valid EE_Datetime_Field then throw error |
|
| 1524 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1525 | - throw new EE_Error(sprintf(__('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.', |
|
| 1526 | - 'event_espresso'), $field_name)); |
|
| 1527 | - } |
|
| 1528 | - //while we are here, let's make sure the timezone internally in EEM_Base matches what is stored on |
|
| 1529 | - //the field. |
|
| 1530 | - $this->_timezone = $field_settings->get_timezone(); |
|
| 1531 | - return array($field_settings->get_date_format($pretty), $field_settings->get_time_format($pretty)); |
|
| 1532 | - } |
|
| 1533 | - |
|
| 1534 | - |
|
| 1535 | - |
|
| 1536 | - /** |
|
| 1537 | - * This returns the current time in a format setup for a query on this model. |
|
| 1538 | - * Usage of this method makes it easier to setup queries against EE_Datetime_Field columns because |
|
| 1539 | - * it will return: |
|
| 1540 | - * - a formatted string in the timezone and format currently set on the EE_Datetime_Field for the given field for |
|
| 1541 | - * NOW |
|
| 1542 | - * - or a unix timestamp (equivalent to time()) |
|
| 1543 | - * Note: When requesting a formatted string, if the date or time format doesn't include seconds, for example, |
|
| 1544 | - * the time returned, because it uses that format, will also NOT include seconds. For this reason, if you want |
|
| 1545 | - * the time returned to be the current time down to the exact second, set $timestamp to true. |
|
| 1546 | - * @since 4.6.x |
|
| 1547 | - * @param string $field_name The field the current time is needed for. |
|
| 1548 | - * @param bool $timestamp True means to return a unix timestamp. Otherwise a |
|
| 1549 | - * formatted string matching the set format for the field in the set timezone will |
|
| 1550 | - * be returned. |
|
| 1551 | - * @param string $what Whether to return the string in just the time format, the date format, or both. |
|
| 1552 | - * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
| 1553 | - * @return int|string If the given field_name is not of the EE_Datetime_Field type, then an EE_Error |
|
| 1554 | - * exception is triggered. |
|
| 1555 | - */ |
|
| 1556 | - public function current_time_for_query($field_name, $timestamp = false, $what = 'both') |
|
| 1557 | - { |
|
| 1558 | - $formats = $this->get_formats_for($field_name); |
|
| 1559 | - $DateTime = new DateTime("now", new DateTimeZone($this->_timezone)); |
|
| 1560 | - if ($timestamp) { |
|
| 1561 | - return $DateTime->format('U'); |
|
| 1562 | - } |
|
| 1563 | - //not returning timestamp, so return formatted string in timezone. |
|
| 1564 | - switch ($what) { |
|
| 1565 | - case 'time' : |
|
| 1566 | - return $DateTime->format($formats[1]); |
|
| 1567 | - break; |
|
| 1568 | - case 'date' : |
|
| 1569 | - return $DateTime->format($formats[0]); |
|
| 1570 | - break; |
|
| 1571 | - default : |
|
| 1572 | - return $DateTime->format(implode(' ', $formats)); |
|
| 1573 | - break; |
|
| 1574 | - } |
|
| 1575 | - } |
|
| 1576 | - |
|
| 1577 | - |
|
| 1578 | - |
|
| 1579 | - /** |
|
| 1580 | - * This receives a time string for a given field and ensures that it is setup to match what the internal settings |
|
| 1581 | - * for the model are. Returns a DateTime object. |
|
| 1582 | - * Note: a gotcha for when you send in unix timestamp. Remember a unix timestamp is already timezone agnostic, |
|
| 1583 | - * (functionally the equivalent of UTC+0). So when you send it in, whatever timezone string you include is |
|
| 1584 | - * ignored. |
|
| 1585 | - * |
|
| 1586 | - * @param string $field_name The field being setup. |
|
| 1587 | - * @param string $timestring The date time string being used. |
|
| 1588 | - * @param string $incoming_format The format for the time string. |
|
| 1589 | - * @param string $timezone By default, it is assumed the incoming time string is in timezone for |
|
| 1590 | - * the blog. If this is not the case, then it can be specified here. If incoming |
|
| 1591 | - * format is |
|
| 1592 | - * 'U', this is ignored. |
|
| 1593 | - * @return DateTime |
|
| 1594 | - * @throws EE_Error |
|
| 1595 | - */ |
|
| 1596 | - public function convert_datetime_for_query($field_name, $timestring, $incoming_format, $timezone = '') |
|
| 1597 | - { |
|
| 1598 | - //just using this to ensure the timezone is set correctly internally |
|
| 1599 | - $this->get_formats_for($field_name); |
|
| 1600 | - //load EEH_DTT_Helper |
|
| 1601 | - $set_timezone = empty($timezone) ? EEH_DTT_Helper::get_timezone() : $timezone; |
|
| 1602 | - $incomingDateTime = date_create_from_format($incoming_format, $timestring, new DateTimeZone($set_timezone)); |
|
| 1603 | - return \EventEspresso\core\domain\entities\DbSafeDateTime::createFromDateTime( $incomingDateTime->setTimezone(new DateTimeZone($this->_timezone)) ); |
|
| 1604 | - } |
|
| 1605 | - |
|
| 1606 | - |
|
| 1607 | - |
|
| 1608 | - /** |
|
| 1609 | - * Gets all the tables comprising this model. Array keys are the table aliases, and values are EE_Table objects |
|
| 1610 | - * |
|
| 1611 | - * @return EE_Table_Base[] |
|
| 1612 | - */ |
|
| 1613 | - public function get_tables() |
|
| 1614 | - { |
|
| 1615 | - return $this->_tables; |
|
| 1616 | - } |
|
| 1617 | - |
|
| 1618 | - |
|
| 1619 | - |
|
| 1620 | - /** |
|
| 1621 | - * Updates all the database entries (in each table for this model) according to $fields_n_values and optionally |
|
| 1622 | - * also updates all the model objects, where the criteria expressed in $query_params are met.. |
|
| 1623 | - * Also note: if this model has multiple tables, this update verifies all the secondary tables have an entry for |
|
| 1624 | - * each row (in the primary table) we're trying to update; if not, it inserts an entry in the secondary table. Eg: |
|
| 1625 | - * if our model has 2 tables: wp_posts (primary), and wp_esp_event (secondary). Let's say we are trying to update a |
|
| 1626 | - * model object with EVT_ID = 1 |
|
| 1627 | - * (which means where wp_posts has ID = 1, because wp_posts.ID is the primary key's column), which exists, but |
|
| 1628 | - * there is no entry in wp_esp_event for this entry in wp_posts. So, this update script will insert a row into |
|
| 1629 | - * wp_esp_event, using any available parameters from $fields_n_values (eg, if "EVT_limit" => 40 is in |
|
| 1630 | - * $fields_n_values, the new entry in wp_esp_event will set EVT_limit = 40, and use default for other columns which |
|
| 1631 | - * are not specified) |
|
| 1632 | - * |
|
| 1633 | - * @param array $fields_n_values keys are model fields (exactly like keys in EEM_Base::_fields, NOT db |
|
| 1634 | - * columns!), values are strings, ints, floats, and maybe arrays if they |
|
| 1635 | - * are to be serialized. Basically, the values are what you'd expect to be |
|
| 1636 | - * values on the model, NOT necessarily what's in the DB. For example, if |
|
| 1637 | - * we wanted to update only the TXN_details on any Transactions where its |
|
| 1638 | - * ID=34, we'd use this method as follows: |
|
| 1639 | - * EEM_Transaction::instance()->update( |
|
| 1640 | - * array('TXN_details'=>array('detail1'=>'monkey','detail2'=>'banana'), |
|
| 1641 | - * array(array('TXN_ID'=>34))); |
|
| 1642 | - * @param array $query_params very much like EEM_Base::get_all's $query_params |
|
| 1643 | - * in client code into what's expected to be stored on each field. Eg, |
|
| 1644 | - * consider updating Question's QST_admin_label field is of type |
|
| 1645 | - * Simple_HTML. If you use this function to update that field to $new_value |
|
| 1646 | - * = (note replace 8's with appropriate opening and closing tags in the |
|
| 1647 | - * following example)"8script8alert('I hack all');8/script88b8boom |
|
| 1648 | - * baby8/b8", then if you set $values_already_prepared_by_model_object to |
|
| 1649 | - * TRUE, it is assumed that you've already called |
|
| 1650 | - * EE_Simple_HTML_Field->prepare_for_set($new_value), which removes the |
|
| 1651 | - * malicious javascript. However, if |
|
| 1652 | - * $values_already_prepared_by_model_object is left as FALSE, then |
|
| 1653 | - * EE_Simple_HTML_Field->prepare_for_set($new_value) will be called on it, |
|
| 1654 | - * and every other field, before insertion. We provide this parameter |
|
| 1655 | - * because model objects perform their prepare_for_set function on all |
|
| 1656 | - * their values, and so don't need to be called again (and in many cases, |
|
| 1657 | - * shouldn't be called again. Eg: if we escape HTML characters in the |
|
| 1658 | - * prepare_for_set method...) |
|
| 1659 | - * @param boolean $keep_model_objs_in_sync if TRUE, makes sure we ALSO update model objects |
|
| 1660 | - * in this model's entity map according to $fields_n_values that match |
|
| 1661 | - * $query_params. This obviously has some overhead, so you can disable it |
|
| 1662 | - * by setting this to FALSE, but be aware that model objects being used |
|
| 1663 | - * could get out-of-sync with the database |
|
| 1664 | - * @return int how many rows got updated or FALSE if something went wrong with the query (wp returns FALSE or num |
|
| 1665 | - * rows affected which *could* include 0 which DOES NOT mean the query was |
|
| 1666 | - * bad) |
|
| 1667 | - * @throws EE_Error |
|
| 1668 | - */ |
|
| 1669 | - public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true) |
|
| 1670 | - { |
|
| 1671 | - if (! is_array($query_params)) { |
|
| 1672 | - EE_Error::doing_it_wrong('EEM_Base::update', |
|
| 1673 | - sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1674 | - gettype($query_params)), '4.6.0'); |
|
| 1675 | - $query_params = array(); |
|
| 1676 | - } |
|
| 1677 | - /** |
|
| 1678 | - * Action called before a model update call has been made. |
|
| 1679 | - * |
|
| 1680 | - * @param EEM_Base $model |
|
| 1681 | - * @param array $fields_n_values the updated fields and their new values |
|
| 1682 | - * @param array $query_params @see EEM_Base::get_all() |
|
| 1683 | - */ |
|
| 1684 | - do_action('AHEE__EEM_Base__update__begin', $this, $fields_n_values, $query_params); |
|
| 1685 | - /** |
|
| 1686 | - * Filters the fields about to be updated given the query parameters. You can provide the |
|
| 1687 | - * $query_params to $this->get_all() to find exactly which records will be updated |
|
| 1688 | - * |
|
| 1689 | - * @param array $fields_n_values fields and their new values |
|
| 1690 | - * @param EEM_Base $model the model being queried |
|
| 1691 | - * @param array $query_params see EEM_Base::get_all() |
|
| 1692 | - */ |
|
| 1693 | - $fields_n_values = (array)apply_filters('FHEE__EEM_Base__update__fields_n_values', $fields_n_values, $this, |
|
| 1694 | - $query_params); |
|
| 1695 | - //need to verify that, for any entry we want to update, there are entries in each secondary table. |
|
| 1696 | - //to do that, for each table, verify that it's PK isn't null. |
|
| 1697 | - $tables = $this->get_tables(); |
|
| 1698 | - //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 |
|
| 1699 | - //NOTE: we should make this code more efficient by NOT querying twice |
|
| 1700 | - //before the real update, but that needs to first go through ALPHA testing |
|
| 1701 | - //as it's dangerous. says Mike August 8 2014 |
|
| 1702 | - //we want to make sure the default_where strategy is ignored |
|
| 1703 | - $this->_ignore_where_strategy = true; |
|
| 1704 | - $wpdb_select_results = $this->_get_all_wpdb_results($query_params); |
|
| 1705 | - foreach ($wpdb_select_results as $wpdb_result) { |
|
| 1706 | - // type cast stdClass as array |
|
| 1707 | - $wpdb_result = (array)$wpdb_result; |
|
| 1708 | - //get the model object's PK, as we'll want this if we need to insert a row into secondary tables |
|
| 1709 | - if ($this->has_primary_key_field()) { |
|
| 1710 | - $main_table_pk_value = $wpdb_result[$this->get_primary_key_field()->get_qualified_column()]; |
|
| 1711 | - } else { |
|
| 1712 | - //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) |
|
| 1713 | - $main_table_pk_value = null; |
|
| 1714 | - } |
|
| 1715 | - //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 |
|
| 1716 | - //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 |
|
| 1717 | - if (count($tables) > 1) { |
|
| 1718 | - //foreach matching row in the DB, ensure that each table's PK isn't null. If so, there must not be an entry |
|
| 1719 | - //in that table, and so we'll want to insert one |
|
| 1720 | - foreach ($tables as $table_obj) { |
|
| 1721 | - $this_table_pk_column = $table_obj->get_fully_qualified_pk_column(); |
|
| 1722 | - //if there is no private key for this table on the results, it means there's no entry |
|
| 1723 | - //in this table, right? so insert a row in the current table, using any fields available |
|
| 1724 | - if (! (array_key_exists($this_table_pk_column, $wpdb_result) |
|
| 1725 | - && $wpdb_result[$this_table_pk_column]) |
|
| 1726 | - ) { |
|
| 1727 | - $success = $this->_insert_into_specific_table($table_obj, $fields_n_values, |
|
| 1728 | - $main_table_pk_value); |
|
| 1729 | - //if we died here, report the error |
|
| 1730 | - if (! $success) { |
|
| 1731 | - return false; |
|
| 1732 | - } |
|
| 1733 | - } |
|
| 1734 | - } |
|
| 1735 | - } |
|
| 1736 | - // //and now check that if we have cached any models by that ID on the model, that |
|
| 1737 | - // //they also get updated properly |
|
| 1738 | - // $model_object = $this->get_from_entity_map( $main_table_pk_value ); |
|
| 1739 | - // if( $model_object ){ |
|
| 1740 | - // foreach( $fields_n_values as $field => $value ){ |
|
| 1741 | - // $model_object->set($field, $value); |
|
| 1742 | - //let's make sure default_where strategy is followed now |
|
| 1743 | - $this->_ignore_where_strategy = false; |
|
| 1744 | - } |
|
| 1745 | - //if we want to keep model objects in sync, AND |
|
| 1746 | - //if this wasn't called from a model object (to update itself) |
|
| 1747 | - //then we want to make sure we keep all the existing |
|
| 1748 | - //model objects in sync with the db |
|
| 1749 | - if ($keep_model_objs_in_sync && ! $this->_values_already_prepared_by_model_object) { |
|
| 1750 | - if ($this->has_primary_key_field()) { |
|
| 1751 | - $model_objs_affected_ids = $this->get_col($query_params); |
|
| 1752 | - } else { |
|
| 1753 | - //we need to select a bunch of columns and then combine them into the the "index primary key string"s |
|
| 1754 | - $models_affected_key_columns = $this->_get_all_wpdb_results($query_params, ARRAY_A); |
|
| 1755 | - $model_objs_affected_ids = array(); |
|
| 1756 | - foreach ($models_affected_key_columns as $row) { |
|
| 1757 | - $combined_index_key = $this->get_index_primary_key_string($row); |
|
| 1758 | - $model_objs_affected_ids[$combined_index_key] = $combined_index_key; |
|
| 1759 | - } |
|
| 1760 | - } |
|
| 1761 | - if (! $model_objs_affected_ids) { |
|
| 1762 | - //wait wait wait- if nothing was affected let's stop here |
|
| 1763 | - return 0; |
|
| 1764 | - } |
|
| 1765 | - foreach ($model_objs_affected_ids as $id) { |
|
| 1766 | - $model_obj_in_entity_map = $this->get_from_entity_map($id); |
|
| 1767 | - if ($model_obj_in_entity_map) { |
|
| 1768 | - foreach ($fields_n_values as $field => $new_value) { |
|
| 1769 | - $model_obj_in_entity_map->set($field, $new_value); |
|
| 1770 | - } |
|
| 1771 | - } |
|
| 1772 | - } |
|
| 1773 | - //if there is a primary key on this model, we can now do a slight optimization |
|
| 1774 | - if ($this->has_primary_key_field()) { |
|
| 1775 | - //we already know what we want to update. So let's make the query simpler so it's a little more efficient |
|
| 1776 | - $query_params = array( |
|
| 1777 | - array($this->primary_key_name() => array('IN', $model_objs_affected_ids)), |
|
| 1778 | - 'limit' => count($model_objs_affected_ids), |
|
| 1779 | - 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
| 1780 | - ); |
|
| 1781 | - } |
|
| 1782 | - } |
|
| 1783 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1784 | - $SQL = "UPDATE " |
|
| 1785 | - . $model_query_info->get_full_join_sql() |
|
| 1786 | - . " SET " |
|
| 1787 | - . $this->_construct_update_sql($fields_n_values) |
|
| 1788 | - . $model_query_info->get_where_sql();//note: doesn't use _construct_2nd_half_of_select_query() because doesn't accept LIMIT, ORDER BY, etc. |
|
| 1789 | - $rows_affected = $this->_do_wpdb_query('query', array($SQL)); |
|
| 1790 | - /** |
|
| 1791 | - * Action called after a model update call has been made. |
|
| 1792 | - * |
|
| 1793 | - * @param EEM_Base $model |
|
| 1794 | - * @param array $fields_n_values the updated fields and their new values |
|
| 1795 | - * @param array $query_params @see EEM_Base::get_all() |
|
| 1796 | - * @param int $rows_affected |
|
| 1797 | - */ |
|
| 1798 | - do_action('AHEE__EEM_Base__update__end', $this, $fields_n_values, $query_params, $rows_affected); |
|
| 1799 | - return $rows_affected;//how many supposedly got updated |
|
| 1800 | - } |
|
| 1801 | - |
|
| 1802 | - |
|
| 1803 | - |
|
| 1804 | - /** |
|
| 1805 | - * Analogous to $wpdb->get_col, returns a 1-dimensional array where teh values |
|
| 1806 | - * are teh values of the field specified (or by default the primary key field) |
|
| 1807 | - * that matched the query params. Note that you should pass the name of the |
|
| 1808 | - * model FIELD, not the database table's column name. |
|
| 1809 | - * |
|
| 1810 | - * @param array $query_params @see EEM_Base::get_all() |
|
| 1811 | - * @param string $field_to_select |
|
| 1812 | - * @return array just like $wpdb->get_col() |
|
| 1813 | - * @throws EE_Error |
|
| 1814 | - */ |
|
| 1815 | - public function get_col($query_params = array(), $field_to_select = null) |
|
| 1816 | - { |
|
| 1817 | - if ($field_to_select) { |
|
| 1818 | - $field = $this->field_settings_for($field_to_select); |
|
| 1819 | - } elseif ($this->has_primary_key_field()) { |
|
| 1820 | - $field = $this->get_primary_key_field(); |
|
| 1821 | - } else { |
|
| 1822 | - //no primary key, just grab the first column |
|
| 1823 | - $field = reset($this->field_settings()); |
|
| 1824 | - } |
|
| 1825 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1826 | - $select_expressions = $field->get_qualified_column(); |
|
| 1827 | - $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 1828 | - return $this->_do_wpdb_query('get_col', array($SQL)); |
|
| 1829 | - } |
|
| 1830 | - |
|
| 1831 | - |
|
| 1832 | - |
|
| 1833 | - /** |
|
| 1834 | - * Returns a single column value for a single row from the database |
|
| 1835 | - * |
|
| 1836 | - * @param array $query_params @see EEM_Base::get_all() |
|
| 1837 | - * @param string $field_to_select @see EEM_Base::get_col() |
|
| 1838 | - * @return string |
|
| 1839 | - * @throws EE_Error |
|
| 1840 | - */ |
|
| 1841 | - public function get_var($query_params = array(), $field_to_select = null) |
|
| 1842 | - { |
|
| 1843 | - $query_params['limit'] = 1; |
|
| 1844 | - $col = $this->get_col($query_params, $field_to_select); |
|
| 1845 | - if (! empty($col)) { |
|
| 1846 | - return reset($col); |
|
| 1847 | - } |
|
| 1848 | - return null; |
|
| 1849 | - } |
|
| 1850 | - |
|
| 1851 | - |
|
| 1852 | - |
|
| 1853 | - /** |
|
| 1854 | - * Makes the SQL for after "UPDATE table_X inner join table_Y..." and before "...WHERE". Eg "Question.name='party |
|
| 1855 | - * time?', Question.desc='what do you think?',..." Values are filtered through wpdb->prepare to avoid against SQL |
|
| 1856 | - * injection, but currently no further filtering is done |
|
| 1857 | - * |
|
| 1858 | - * @global $wpdb |
|
| 1859 | - * @param array $fields_n_values array keys are field names on this model, and values are what those fields should |
|
| 1860 | - * be updated to in the DB |
|
| 1861 | - * @return string of SQL |
|
| 1862 | - * @throws EE_Error |
|
| 1863 | - */ |
|
| 1864 | - public function _construct_update_sql($fields_n_values) |
|
| 1865 | - { |
|
| 1866 | - /** @type WPDB $wpdb */ |
|
| 1867 | - global $wpdb; |
|
| 1868 | - $cols_n_values = array(); |
|
| 1869 | - foreach ($fields_n_values as $field_name => $value) { |
|
| 1870 | - $field_obj = $this->field_settings_for($field_name); |
|
| 1871 | - //if the value is NULL, we want to assign the value to that. |
|
| 1872 | - //wpdb->prepare doesn't really handle that properly |
|
| 1873 | - $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
| 1874 | - $value_sql = $prepared_value === null ? 'NULL' |
|
| 1875 | - : $wpdb->prepare($field_obj->get_wpdb_data_type(), $prepared_value); |
|
| 1876 | - $cols_n_values[] = $field_obj->get_qualified_column() . "=" . $value_sql; |
|
| 1877 | - } |
|
| 1878 | - return implode(",", $cols_n_values); |
|
| 1879 | - } |
|
| 1880 | - |
|
| 1881 | - |
|
| 1882 | - |
|
| 1883 | - /** |
|
| 1884 | - * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
| 1885 | - * Performs a HARD delete, meaning the database row should always be removed, |
|
| 1886 | - * not just have a flag field on it switched |
|
| 1887 | - * Wrapper for EEM_Base::delete_permanently() |
|
| 1888 | - * |
|
| 1889 | - * @param mixed $id |
|
| 1890 | - * @param boolean $allow_blocking |
|
| 1891 | - * @return int the number of rows deleted |
|
| 1892 | - * @throws EE_Error |
|
| 1893 | - */ |
|
| 1894 | - public function delete_permanently_by_ID($id, $allow_blocking = true) |
|
| 1895 | - { |
|
| 1896 | - return $this->delete_permanently( |
|
| 1897 | - array( |
|
| 1898 | - array($this->get_primary_key_field()->get_name() => $id), |
|
| 1899 | - 'limit' => 1, |
|
| 1900 | - ), |
|
| 1901 | - $allow_blocking |
|
| 1902 | - ); |
|
| 1903 | - } |
|
| 1904 | - |
|
| 1905 | - |
|
| 1906 | - |
|
| 1907 | - /** |
|
| 1908 | - * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
| 1909 | - * Wrapper for EEM_Base::delete() |
|
| 1910 | - * |
|
| 1911 | - * @param mixed $id |
|
| 1912 | - * @param boolean $allow_blocking |
|
| 1913 | - * @return int the number of rows deleted |
|
| 1914 | - * @throws EE_Error |
|
| 1915 | - */ |
|
| 1916 | - public function delete_by_ID($id, $allow_blocking = true) |
|
| 1917 | - { |
|
| 1918 | - return $this->delete( |
|
| 1919 | - array( |
|
| 1920 | - array($this->get_primary_key_field()->get_name() => $id), |
|
| 1921 | - 'limit' => 1, |
|
| 1922 | - ), |
|
| 1923 | - $allow_blocking |
|
| 1924 | - ); |
|
| 1925 | - } |
|
| 1926 | - |
|
| 1927 | - |
|
| 1928 | - |
|
| 1929 | - /** |
|
| 1930 | - * Identical to delete_permanently, but does a "soft" delete if possible, |
|
| 1931 | - * meaning if the model has a field that indicates its been "trashed" or |
|
| 1932 | - * "soft deleted", we will just set that instead of actually deleting the rows. |
|
| 1933 | - * |
|
| 1934 | - * @see EEM_Base::delete_permanently |
|
| 1935 | - * @param array $query_params |
|
| 1936 | - * @param boolean $allow_blocking |
|
| 1937 | - * @return int how many rows got deleted |
|
| 1938 | - * @throws EE_Error |
|
| 1939 | - */ |
|
| 1940 | - public function delete($query_params, $allow_blocking = true) |
|
| 1941 | - { |
|
| 1942 | - return $this->delete_permanently($query_params, $allow_blocking); |
|
| 1943 | - } |
|
| 1944 | - |
|
| 1945 | - |
|
| 1946 | - |
|
| 1947 | - /** |
|
| 1948 | - * Deletes the model objects that meet the query params. Note: this method is overridden |
|
| 1949 | - * in EEM_Soft_Delete_Base so that soft-deleted model objects are instead only flagged |
|
| 1950 | - * as archived, not actually deleted |
|
| 1951 | - * |
|
| 1952 | - * @param array $query_params very much like EEM_Base::get_all's $query_params |
|
| 1953 | - * @param boolean $allow_blocking if TRUE, matched objects will only be deleted if there is no related model info |
|
| 1954 | - * that blocks it (ie, there' sno other data that depends on this data); if false, |
|
| 1955 | - * deletes regardless of other objects which may depend on it. Its generally |
|
| 1956 | - * advisable to always leave this as TRUE, otherwise you could easily corrupt your |
|
| 1957 | - * DB |
|
| 1958 | - * @return int how many rows got deleted |
|
| 1959 | - * @throws EE_Error |
|
| 1960 | - */ |
|
| 1961 | - public function delete_permanently($query_params, $allow_blocking = true) |
|
| 1962 | - { |
|
| 1963 | - /** |
|
| 1964 | - * Action called just before performing a real deletion query. You can use the |
|
| 1965 | - * model and its $query_params to find exactly which items will be deleted |
|
| 1966 | - * |
|
| 1967 | - * @param EEM_Base $model |
|
| 1968 | - * @param array $query_params @see EEM_Base::get_all() |
|
| 1969 | - * @param boolean $allow_blocking whether or not to allow related model objects |
|
| 1970 | - * to block (prevent) this deletion |
|
| 1971 | - */ |
|
| 1972 | - do_action('AHEE__EEM_Base__delete__begin', $this, $query_params, $allow_blocking); |
|
| 1973 | - //some MySQL databases may be running safe mode, which may restrict |
|
| 1974 | - //deletion if there is no KEY column used in the WHERE statement of a deletion. |
|
| 1975 | - //to get around this, we first do a SELECT, get all the IDs, and then run another query |
|
| 1976 | - //to delete them |
|
| 1977 | - $items_for_deletion = $this->_get_all_wpdb_results($query_params); |
|
| 1978 | - $columns_and_ids_for_deleting = $this->_get_ids_for_delete($items_for_deletion, $allow_blocking); |
|
| 1979 | - $deletion_where_query_part = $this->_build_query_part_for_deleting_from_columns_and_values( |
|
| 1980 | - $columns_and_ids_for_deleting |
|
| 1981 | - ); |
|
| 1982 | - /** |
|
| 1983 | - * Allows client code to act on the items being deleted before the query is actually executed. |
|
| 1984 | - * |
|
| 1985 | - * @param EEM_Base $this The model instance being acted on. |
|
| 1986 | - * @param array $query_params The incoming array of query parameters influencing what gets deleted. |
|
| 1987 | - * @param bool $allow_blocking @see param description in method phpdoc block. |
|
| 1988 | - * @param array $columns_and_ids_for_deleting An array indicating what entities will get removed as |
|
| 1989 | - * derived from the incoming query parameters. |
|
| 1990 | - * @see details on the structure of this array in the phpdocs |
|
| 1991 | - * for the `_get_ids_for_delete_method` |
|
| 1992 | - * |
|
| 1993 | - */ |
|
| 1994 | - do_action('AHEE__EEM_Base__delete__before_query', |
|
| 1995 | - $this, |
|
| 1996 | - $query_params, |
|
| 1997 | - $allow_blocking, |
|
| 1998 | - $columns_and_ids_for_deleting |
|
| 1999 | - ); |
|
| 2000 | - if ($deletion_where_query_part) { |
|
| 2001 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 2002 | - $table_aliases = array_keys($this->_tables); |
|
| 2003 | - $SQL = "DELETE " |
|
| 2004 | - . implode(", ", $table_aliases) |
|
| 2005 | - . " FROM " |
|
| 2006 | - . $model_query_info->get_full_join_sql() |
|
| 2007 | - . " WHERE " |
|
| 2008 | - . $deletion_where_query_part; |
|
| 2009 | - $rows_deleted = $this->_do_wpdb_query('query', array($SQL)); |
|
| 2010 | - } else { |
|
| 2011 | - $rows_deleted = 0; |
|
| 2012 | - } |
|
| 2013 | - |
|
| 2014 | - //Next, make sure those items are removed from the entity map; if they could be put into it at all; and if |
|
| 2015 | - //there was no error with the delete query. |
|
| 2016 | - if ($this->has_primary_key_field() |
|
| 2017 | - && $rows_deleted !== false |
|
| 2018 | - && isset($columns_and_ids_for_deleting[$this->get_primary_key_field()->get_qualified_column()]) |
|
| 2019 | - ) { |
|
| 2020 | - $ids_for_removal = $columns_and_ids_for_deleting[$this->get_primary_key_field()->get_qualified_column()]; |
|
| 2021 | - foreach ($ids_for_removal as $id) { |
|
| 2022 | - if (isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id])) { |
|
| 2023 | - unset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id]); |
|
| 2024 | - } |
|
| 2025 | - } |
|
| 2026 | - |
|
| 2027 | - // delete any extra meta attached to the deleted entities but ONLY if this model is not an instance of |
|
| 2028 | - //`EEM_Extra_Meta`. In other words we want to prevent recursion on EEM_Extra_Meta::delete_permanently calls |
|
| 2029 | - //unnecessarily. It's very unlikely that users will have assigned Extra Meta to Extra Meta |
|
| 2030 | - // (although it is possible). |
|
| 2031 | - //Note this can be skipped by using the provided filter and returning false. |
|
| 2032 | - if (apply_filters( |
|
| 2033 | - 'FHEE__EEM_Base__delete_permanently__dont_delete_extra_meta_for_extra_meta', |
|
| 2034 | - ! $this instanceof EEM_Extra_Meta, |
|
| 2035 | - $this |
|
| 2036 | - )) { |
|
| 2037 | - EEM_Extra_Meta::instance()->delete_permanently(array( |
|
| 2038 | - 0 => array( |
|
| 2039 | - 'EXM_type' => $this->get_this_model_name(), |
|
| 2040 | - 'OBJ_ID' => array( |
|
| 2041 | - 'IN', |
|
| 2042 | - $ids_for_removal |
|
| 2043 | - ) |
|
| 2044 | - ) |
|
| 2045 | - )); |
|
| 2046 | - } |
|
| 2047 | - } |
|
| 2048 | - |
|
| 2049 | - /** |
|
| 2050 | - * Action called just after performing a real deletion query. Although at this point the |
|
| 2051 | - * items should have been deleted |
|
| 2052 | - * |
|
| 2053 | - * @param EEM_Base $model |
|
| 2054 | - * @param array $query_params @see EEM_Base::get_all() |
|
| 2055 | - * @param int $rows_deleted |
|
| 2056 | - */ |
|
| 2057 | - do_action('AHEE__EEM_Base__delete__end', $this, $query_params, $rows_deleted, $columns_and_ids_for_deleting); |
|
| 2058 | - return $rows_deleted;//how many supposedly got deleted |
|
| 2059 | - } |
|
| 2060 | - |
|
| 2061 | - |
|
| 2062 | - |
|
| 2063 | - /** |
|
| 2064 | - * Checks all the relations that throw error messages when there are blocking related objects |
|
| 2065 | - * for related model objects. If there are any related model objects on those relations, |
|
| 2066 | - * adds an EE_Error, and return true |
|
| 2067 | - * |
|
| 2068 | - * @param EE_Base_Class|int $this_model_obj_or_id |
|
| 2069 | - * @param EE_Base_Class $ignore_this_model_obj a model object like 'EE_Event', or 'EE_Term_Taxonomy', which |
|
| 2070 | - * should be ignored when determining whether there are related |
|
| 2071 | - * model objects which block this model object's deletion. Useful |
|
| 2072 | - * if you know A is related to B and are considering deleting A, |
|
| 2073 | - * but want to see if A has any other objects blocking its deletion |
|
| 2074 | - * before removing the relation between A and B |
|
| 2075 | - * @return boolean |
|
| 2076 | - * @throws EE_Error |
|
| 2077 | - */ |
|
| 2078 | - public function delete_is_blocked_by_related_models($this_model_obj_or_id, $ignore_this_model_obj = null) |
|
| 2079 | - { |
|
| 2080 | - //first, if $ignore_this_model_obj was supplied, get its model |
|
| 2081 | - if ($ignore_this_model_obj && $ignore_this_model_obj instanceof EE_Base_Class) { |
|
| 2082 | - $ignored_model = $ignore_this_model_obj->get_model(); |
|
| 2083 | - } else { |
|
| 2084 | - $ignored_model = null; |
|
| 2085 | - } |
|
| 2086 | - //now check all the relations of $this_model_obj_or_id and see if there |
|
| 2087 | - //are any related model objects blocking it? |
|
| 2088 | - $is_blocked = false; |
|
| 2089 | - foreach ($this->_model_relations as $relation_name => $relation_obj) { |
|
| 2090 | - if ($relation_obj->block_delete_if_related_models_exist()) { |
|
| 2091 | - //if $ignore_this_model_obj was supplied, then for the query |
|
| 2092 | - //on that model needs to be told to ignore $ignore_this_model_obj |
|
| 2093 | - if ($ignored_model && $relation_name === $ignored_model->get_this_model_name()) { |
|
| 2094 | - $related_model_objects = $relation_obj->get_all_related($this_model_obj_or_id, array( |
|
| 2095 | - array( |
|
| 2096 | - $ignored_model->get_primary_key_field()->get_name() => array( |
|
| 2097 | - '!=', |
|
| 2098 | - $ignore_this_model_obj->ID(), |
|
| 2099 | - ), |
|
| 2100 | - ), |
|
| 2101 | - )); |
|
| 2102 | - } else { |
|
| 2103 | - $related_model_objects = $relation_obj->get_all_related($this_model_obj_or_id); |
|
| 2104 | - } |
|
| 2105 | - if ($related_model_objects) { |
|
| 2106 | - EE_Error::add_error($relation_obj->get_deletion_error_message(), __FILE__, __FUNCTION__, __LINE__); |
|
| 2107 | - $is_blocked = true; |
|
| 2108 | - } |
|
| 2109 | - } |
|
| 2110 | - } |
|
| 2111 | - return $is_blocked; |
|
| 2112 | - } |
|
| 2113 | - |
|
| 2114 | - |
|
| 2115 | - /** |
|
| 2116 | - * Builds the columns and values for items to delete from the incoming $row_results_for_deleting array. |
|
| 2117 | - * @param array $row_results_for_deleting |
|
| 2118 | - * @param bool $allow_blocking |
|
| 2119 | - * @return array The shape of this array depends on whether the model `has_primary_key_field` or not. If the |
|
| 2120 | - * model DOES have a primary_key_field, then the array will be a simple single dimension array where |
|
| 2121 | - * the key is the fully qualified primary key column and the value is an array of ids that will be |
|
| 2122 | - * deleted. Example: |
|
| 2123 | - * array('Event.EVT_ID' => array( 1,2,3)) |
|
| 2124 | - * If the model DOES NOT have a primary_key_field, then the array will be a two dimensional array |
|
| 2125 | - * where each element is a group of columns and values that get deleted. Example: |
|
| 2126 | - * array( |
|
| 2127 | - * 0 => array( |
|
| 2128 | - * 'Term_Relationship.object_id' => 1 |
|
| 2129 | - * 'Term_Relationship.term_taxonomy_id' => 5 |
|
| 2130 | - * ), |
|
| 2131 | - * 1 => array( |
|
| 2132 | - * 'Term_Relationship.object_id' => 1 |
|
| 2133 | - * 'Term_Relationship.term_taxonomy_id' => 6 |
|
| 2134 | - * ) |
|
| 2135 | - * ) |
|
| 2136 | - * @throws EE_Error |
|
| 2137 | - */ |
|
| 2138 | - protected function _get_ids_for_delete(array $row_results_for_deleting, $allow_blocking = true) |
|
| 2139 | - { |
|
| 2140 | - $ids_to_delete_indexed_by_column = array(); |
|
| 2141 | - if ($this->has_primary_key_field()) { |
|
| 2142 | - $primary_table = $this->_get_main_table(); |
|
| 2143 | - $primary_table_pk_field = $this->get_field_by_column($primary_table->get_fully_qualified_pk_column()); |
|
| 2144 | - $other_tables = $this->_get_other_tables(); |
|
| 2145 | - $ids_to_delete_indexed_by_column = $query = array(); |
|
| 2146 | - foreach ($row_results_for_deleting as $item_to_delete) { |
|
| 2147 | - //before we mark this item for deletion, |
|
| 2148 | - //make sure there's no related entities blocking its deletion (if we're checking) |
|
| 2149 | - if ( |
|
| 2150 | - $allow_blocking |
|
| 2151 | - && $this->delete_is_blocked_by_related_models( |
|
| 2152 | - $item_to_delete[$primary_table->get_fully_qualified_pk_column()] |
|
| 2153 | - ) |
|
| 2154 | - ) { |
|
| 2155 | - continue; |
|
| 2156 | - } |
|
| 2157 | - //primary table deletes |
|
| 2158 | - if (isset($item_to_delete[$primary_table->get_fully_qualified_pk_column()])) { |
|
| 2159 | - $ids_to_delete_indexed_by_column[$primary_table->get_fully_qualified_pk_column()][] = |
|
| 2160 | - $item_to_delete[$primary_table->get_fully_qualified_pk_column()]; |
|
| 2161 | - } |
|
| 2162 | - } |
|
| 2163 | - } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
|
| 2164 | - $fields = $this->get_combined_primary_key_fields(); |
|
| 2165 | - foreach ($row_results_for_deleting as $item_to_delete) { |
|
| 2166 | - $ids_to_delete_indexed_by_column_for_row = array(); |
|
| 2167 | - foreach ($fields as $cpk_field) { |
|
| 2168 | - if ($cpk_field instanceof EE_Model_Field_Base) { |
|
| 2169 | - $ids_to_delete_indexed_by_column_for_row[$cpk_field->get_qualified_column()] = |
|
| 2170 | - $item_to_delete[$cpk_field->get_qualified_column()]; |
|
| 2171 | - } |
|
| 2172 | - } |
|
| 2173 | - $ids_to_delete_indexed_by_column[] = $ids_to_delete_indexed_by_column_for_row; |
|
| 2174 | - } |
|
| 2175 | - } else { |
|
| 2176 | - //so there's no primary key and no combined key... |
|
| 2177 | - //sorry, can't help you |
|
| 2178 | - throw new EE_Error( |
|
| 2179 | - sprintf( |
|
| 2180 | - __( |
|
| 2181 | - "Cannot delete objects of type %s because there is no primary key NOR combined key", |
|
| 2182 | - "event_espresso" |
|
| 2183 | - ), get_class($this) |
|
| 2184 | - ) |
|
| 2185 | - ); |
|
| 2186 | - } |
|
| 2187 | - return $ids_to_delete_indexed_by_column; |
|
| 2188 | - } |
|
| 2189 | - |
|
| 2190 | - |
|
| 2191 | - /** |
|
| 2192 | - * This receives an array of columns and values set to be deleted (as prepared by _get_ids_for_delete) and prepares |
|
| 2193 | - * the corresponding query_part for the query performing the delete. |
|
| 2194 | - * |
|
| 2195 | - * @param array $ids_to_delete_indexed_by_column @see _get_ids_for_delete for how this array might be shaped. |
|
| 2196 | - * @return string |
|
| 2197 | - * @throws EE_Error |
|
| 2198 | - */ |
|
| 2199 | - protected function _build_query_part_for_deleting_from_columns_and_values(array $ids_to_delete_indexed_by_column) { |
|
| 2200 | - $query_part = ''; |
|
| 2201 | - if (empty($ids_to_delete_indexed_by_column)) { |
|
| 2202 | - return $query_part; |
|
| 2203 | - } elseif ($this->has_primary_key_field()) { |
|
| 2204 | - $query = array(); |
|
| 2205 | - foreach ($ids_to_delete_indexed_by_column as $column => $ids) { |
|
| 2206 | - //make sure we have unique $ids |
|
| 2207 | - $ids = array_unique($ids); |
|
| 2208 | - $query[] = $column . ' IN(' . implode(',', $ids) . ')'; |
|
| 2209 | - } |
|
| 2210 | - $query_part = ! empty($query) ? implode(' AND ', $query) : $query_part; |
|
| 2211 | - } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
|
| 2212 | - $ways_to_identify_a_row = array(); |
|
| 2213 | - foreach ($ids_to_delete_indexed_by_column as $ids_to_delete_indexed_by_column_for_each_row) { |
|
| 2214 | - $values_for_each_combined_primary_key_for_a_row = array(); |
|
| 2215 | - foreach ($ids_to_delete_indexed_by_column_for_each_row as $column => $id) { |
|
| 2216 | - $values_for_each_combined_primary_key_for_a_row[] = $column . '=' . $id; |
|
| 2217 | - } |
|
| 2218 | - $ways_to_identify_a_row[] = '(' |
|
| 2219 | - . implode(' AND ', $values_for_each_combined_primary_key_for_a_row) |
|
| 2220 | - . ')'; |
|
| 2221 | - } |
|
| 2222 | - $query_part = implode(' OR ', $ways_to_identify_a_row); |
|
| 2223 | - } |
|
| 2224 | - return $query_part; |
|
| 2225 | - } |
|
| 2226 | - |
|
| 2227 | - |
|
| 2228 | - |
|
| 2229 | - /** |
|
| 2230 | - * Gets the model field by the fully qualified name |
|
| 2231 | - * @param string $qualified_column_name eg 'Event_CPT.post_name' or $field_obj->get_qualified_column() |
|
| 2232 | - * @return EE_Model_Field_Base |
|
| 2233 | - */ |
|
| 2234 | - public function get_field_by_column($qualified_column_name) |
|
| 2235 | - { |
|
| 2236 | - foreach($this->field_settings(true) as $field_name => $field_obj){ |
|
| 2237 | - if($field_obj->get_qualified_column() === $qualified_column_name){ |
|
| 2238 | - return $field_obj; |
|
| 2239 | - } |
|
| 2240 | - } |
|
| 2241 | - throw new EE_Error( |
|
| 2242 | - sprintf( |
|
| 2243 | - esc_html__('Could not find a field on the model "%1$s" for qualified column "%2$s"', 'event_espresso'), |
|
| 2244 | - $this->get_this_model_name(), |
|
| 2245 | - $qualified_column_name |
|
| 2246 | - ) |
|
| 2247 | - ); |
|
| 2248 | - } |
|
| 2249 | - |
|
| 2250 | - |
|
| 2251 | - |
|
| 2252 | - /** |
|
| 2253 | - * Count all the rows that match criteria expressed in $query_params (an array just like arg to EEM_Base::get_all). |
|
| 2254 | - * If $field_to_count isn't provided, the model's primary key is used. Otherwise, we count by field_to_count's |
|
| 2255 | - * column |
|
| 2256 | - * |
|
| 2257 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2258 | - * @param string $field_to_count field on model to count by (not column name) |
|
| 2259 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2260 | - * that by the setting $distinct to TRUE; |
|
| 2261 | - * @return int |
|
| 2262 | - * @throws EE_Error |
|
| 2263 | - */ |
|
| 2264 | - public function count($query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2265 | - { |
|
| 2266 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 2267 | - if ($field_to_count) { |
|
| 2268 | - $field_obj = $this->field_settings_for($field_to_count); |
|
| 2269 | - $column_to_count = $field_obj->get_qualified_column(); |
|
| 2270 | - } elseif ($this->has_primary_key_field()) { |
|
| 2271 | - $pk_field_obj = $this->get_primary_key_field(); |
|
| 2272 | - $column_to_count = $pk_field_obj->get_qualified_column(); |
|
| 2273 | - } else { |
|
| 2274 | - //there's no primary key |
|
| 2275 | - //if we're counting distinct items, and there's no primary key, |
|
| 2276 | - //we need to list out the columns for distinction; |
|
| 2277 | - //otherwise we can just use star |
|
| 2278 | - if ($distinct) { |
|
| 2279 | - $columns_to_use = array(); |
|
| 2280 | - foreach ($this->get_combined_primary_key_fields() as $field_obj) { |
|
| 2281 | - $columns_to_use[] = $field_obj->get_qualified_column(); |
|
| 2282 | - } |
|
| 2283 | - $column_to_count = implode(',', $columns_to_use); |
|
| 2284 | - } else { |
|
| 2285 | - $column_to_count = '*'; |
|
| 2286 | - } |
|
| 2287 | - } |
|
| 2288 | - $column_to_count = $distinct ? "DISTINCT " . $column_to_count : $column_to_count; |
|
| 2289 | - $SQL = "SELECT COUNT(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2290 | - return (int)$this->_do_wpdb_query('get_var', array($SQL)); |
|
| 2291 | - } |
|
| 2292 | - |
|
| 2293 | - |
|
| 2294 | - |
|
| 2295 | - /** |
|
| 2296 | - * Sums up the value of the $field_to_sum (defaults to the primary key, which isn't terribly useful) |
|
| 2297 | - * |
|
| 2298 | - * @param array $query_params like EEM_Base::get_all |
|
| 2299 | - * @param string $field_to_sum name of field (array key in $_fields array) |
|
| 2300 | - * @return float |
|
| 2301 | - * @throws EE_Error |
|
| 2302 | - */ |
|
| 2303 | - public function sum($query_params, $field_to_sum = null) |
|
| 2304 | - { |
|
| 2305 | - $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 2306 | - if ($field_to_sum) { |
|
| 2307 | - $field_obj = $this->field_settings_for($field_to_sum); |
|
| 2308 | - } else { |
|
| 2309 | - $field_obj = $this->get_primary_key_field(); |
|
| 2310 | - } |
|
| 2311 | - $column_to_count = $field_obj->get_qualified_column(); |
|
| 2312 | - $SQL = "SELECT SUM(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2313 | - $return_value = $this->_do_wpdb_query('get_var', array($SQL)); |
|
| 2314 | - $data_type = $field_obj->get_wpdb_data_type(); |
|
| 2315 | - if ($data_type === '%d' || $data_type === '%s') { |
|
| 2316 | - return (float)$return_value; |
|
| 2317 | - } |
|
| 2318 | - //must be %f |
|
| 2319 | - return (float)$return_value; |
|
| 2320 | - } |
|
| 2321 | - |
|
| 2322 | - |
|
| 2323 | - |
|
| 2324 | - /** |
|
| 2325 | - * Just calls the specified method on $wpdb with the given arguments |
|
| 2326 | - * Consolidates a little extra error handling code |
|
| 2327 | - * |
|
| 2328 | - * @param string $wpdb_method |
|
| 2329 | - * @param array $arguments_to_provide |
|
| 2330 | - * @throws EE_Error |
|
| 2331 | - * @global wpdb $wpdb |
|
| 2332 | - * @return mixed |
|
| 2333 | - */ |
|
| 2334 | - protected function _do_wpdb_query($wpdb_method, $arguments_to_provide) |
|
| 2335 | - { |
|
| 2336 | - //if we're in maintenance mode level 2, DON'T run any queries |
|
| 2337 | - //because level 2 indicates the database needs updating and |
|
| 2338 | - //is probably out of sync with the code |
|
| 2339 | - if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 2340 | - throw new EE_Error(sprintf(__("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.", |
|
| 2341 | - "event_espresso"))); |
|
| 2342 | - } |
|
| 2343 | - /** @type WPDB $wpdb */ |
|
| 2344 | - global $wpdb; |
|
| 2345 | - if (! method_exists($wpdb, $wpdb_method)) { |
|
| 2346 | - throw new EE_Error(sprintf(__('There is no method named "%s" on Wordpress\' $wpdb object', |
|
| 2347 | - 'event_espresso'), $wpdb_method)); |
|
| 2348 | - } |
|
| 2349 | - if (WP_DEBUG) { |
|
| 2350 | - $old_show_errors_value = $wpdb->show_errors; |
|
| 2351 | - $wpdb->show_errors(false); |
|
| 2352 | - } |
|
| 2353 | - $result = $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
| 2354 | - $this->show_db_query_if_previously_requested($wpdb->last_query); |
|
| 2355 | - if (WP_DEBUG) { |
|
| 2356 | - $wpdb->show_errors($old_show_errors_value); |
|
| 2357 | - if (! empty($wpdb->last_error)) { |
|
| 2358 | - throw new EE_Error(sprintf(__('WPDB Error: "%s"', 'event_espresso'), $wpdb->last_error)); |
|
| 2359 | - } |
|
| 2360 | - if ($result === false) { |
|
| 2361 | - throw new EE_Error(sprintf(__('WPDB Error occurred, but no error message was logged by wpdb! The wpdb method called was "%1$s" and the arguments were "%2$s"', |
|
| 2362 | - 'event_espresso'), $wpdb_method, var_export($arguments_to_provide, true))); |
|
| 2363 | - } |
|
| 2364 | - } elseif ($result === false) { |
|
| 2365 | - EE_Error::add_error( |
|
| 2366 | - sprintf( |
|
| 2367 | - __('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"', |
|
| 2368 | - 'event_espresso'), |
|
| 2369 | - $wpdb_method, |
|
| 2370 | - var_export($arguments_to_provide, true), |
|
| 2371 | - $wpdb->last_error |
|
| 2372 | - ), |
|
| 2373 | - __FILE__, |
|
| 2374 | - __FUNCTION__, |
|
| 2375 | - __LINE__ |
|
| 2376 | - ); |
|
| 2377 | - } |
|
| 2378 | - return $result; |
|
| 2379 | - } |
|
| 2380 | - |
|
| 2381 | - |
|
| 2382 | - |
|
| 2383 | - /** |
|
| 2384 | - * Attempts to run the indicated WPDB method with the provided arguments, |
|
| 2385 | - * and if there's an error tries to verify the DB is correct. Uses |
|
| 2386 | - * the static property EEM_Base::$_db_verification_level to determine whether |
|
| 2387 | - * we should try to fix the EE core db, the addons, or just give up |
|
| 2388 | - * |
|
| 2389 | - * @param string $wpdb_method |
|
| 2390 | - * @param array $arguments_to_provide |
|
| 2391 | - * @return mixed |
|
| 2392 | - */ |
|
| 2393 | - private function _process_wpdb_query($wpdb_method, $arguments_to_provide) |
|
| 2394 | - { |
|
| 2395 | - /** @type WPDB $wpdb */ |
|
| 2396 | - global $wpdb; |
|
| 2397 | - $wpdb->last_error = null; |
|
| 2398 | - $result = call_user_func_array(array($wpdb, $wpdb_method), $arguments_to_provide); |
|
| 2399 | - // was there an error running the query? but we don't care on new activations |
|
| 2400 | - // (we're going to setup the DB anyway on new activations) |
|
| 2401 | - if (($result === false || ! empty($wpdb->last_error)) |
|
| 2402 | - && EE_System::instance()->detect_req_type() !== EE_System::req_type_new_activation |
|
| 2403 | - ) { |
|
| 2404 | - switch (EEM_Base::$_db_verification_level) { |
|
| 2405 | - case EEM_Base::db_verified_none : |
|
| 2406 | - // let's double-check core's DB |
|
| 2407 | - $error_message = $this->_verify_core_db($wpdb_method, $arguments_to_provide); |
|
| 2408 | - break; |
|
| 2409 | - case EEM_Base::db_verified_core : |
|
| 2410 | - // STILL NO LOVE?? verify all the addons too. Maybe they need to be fixed |
|
| 2411 | - $error_message = $this->_verify_addons_db($wpdb_method, $arguments_to_provide); |
|
| 2412 | - break; |
|
| 2413 | - case EEM_Base::db_verified_addons : |
|
| 2414 | - // ummmm... you in trouble |
|
| 2415 | - return $result; |
|
| 2416 | - break; |
|
| 2417 | - } |
|
| 2418 | - if (! empty($error_message)) { |
|
| 2419 | - EE_Log::instance()->log(__FILE__, __FUNCTION__, $error_message, 'error'); |
|
| 2420 | - trigger_error($error_message); |
|
| 2421 | - } |
|
| 2422 | - return $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
| 2423 | - } |
|
| 2424 | - return $result; |
|
| 2425 | - } |
|
| 2426 | - |
|
| 2427 | - |
|
| 2428 | - |
|
| 2429 | - /** |
|
| 2430 | - * Verifies the EE core database is up-to-date and records that we've done it on |
|
| 2431 | - * EEM_Base::$_db_verification_level |
|
| 2432 | - * |
|
| 2433 | - * @param string $wpdb_method |
|
| 2434 | - * @param array $arguments_to_provide |
|
| 2435 | - * @return string |
|
| 2436 | - */ |
|
| 2437 | - private function _verify_core_db($wpdb_method, $arguments_to_provide) |
|
| 2438 | - { |
|
| 2439 | - /** @type WPDB $wpdb */ |
|
| 2440 | - global $wpdb; |
|
| 2441 | - //ok remember that we've already attempted fixing the core db, in case the problem persists |
|
| 2442 | - EEM_Base::$_db_verification_level = EEM_Base::db_verified_core; |
|
| 2443 | - $error_message = sprintf( |
|
| 2444 | - __('WPDB Error "%1$s" while running wpdb method "%2$s" with arguments %3$s. Automatically attempting to fix EE Core DB', |
|
| 2445 | - 'event_espresso'), |
|
| 2446 | - $wpdb->last_error, |
|
| 2447 | - $wpdb_method, |
|
| 2448 | - wp_json_encode($arguments_to_provide) |
|
| 2449 | - ); |
|
| 2450 | - EE_System::instance()->initialize_db_if_no_migrations_required(false, true); |
|
| 2451 | - return $error_message; |
|
| 2452 | - } |
|
| 2453 | - |
|
| 2454 | - |
|
| 2455 | - |
|
| 2456 | - /** |
|
| 2457 | - * Verifies the EE addons' database is up-to-date and records that we've done it on |
|
| 2458 | - * EEM_Base::$_db_verification_level |
|
| 2459 | - * |
|
| 2460 | - * @param $wpdb_method |
|
| 2461 | - * @param $arguments_to_provide |
|
| 2462 | - * @return string |
|
| 2463 | - */ |
|
| 2464 | - private function _verify_addons_db($wpdb_method, $arguments_to_provide) |
|
| 2465 | - { |
|
| 2466 | - /** @type WPDB $wpdb */ |
|
| 2467 | - global $wpdb; |
|
| 2468 | - //ok remember that we've already attempted fixing the addons dbs, in case the problem persists |
|
| 2469 | - EEM_Base::$_db_verification_level = EEM_Base::db_verified_addons; |
|
| 2470 | - $error_message = sprintf( |
|
| 2471 | - __('WPDB AGAIN: Error "%1$s" while running the same method and arguments as before. Automatically attempting to fix EE Addons DB', |
|
| 2472 | - 'event_espresso'), |
|
| 2473 | - $wpdb->last_error, |
|
| 2474 | - $wpdb_method, |
|
| 2475 | - wp_json_encode($arguments_to_provide) |
|
| 2476 | - ); |
|
| 2477 | - EE_System::instance()->initialize_addons(); |
|
| 2478 | - return $error_message; |
|
| 2479 | - } |
|
| 2480 | - |
|
| 2481 | - |
|
| 2482 | - |
|
| 2483 | - /** |
|
| 2484 | - * In order to avoid repeating this code for the get_all, sum, and count functions, put the code parts |
|
| 2485 | - * that are identical in here. Returns a string of SQL of everything in a SELECT query except the beginning |
|
| 2486 | - * SELECT clause, eg " FROM wp_posts AS Event INNER JOIN ... WHERE ... ORDER BY ... LIMIT ... GROUP BY ... HAVING |
|
| 2487 | - * ..." |
|
| 2488 | - * |
|
| 2489 | - * @param EE_Model_Query_Info_Carrier $model_query_info |
|
| 2490 | - * @return string |
|
| 2491 | - */ |
|
| 2492 | - private function _construct_2nd_half_of_select_query(EE_Model_Query_Info_Carrier $model_query_info) |
|
| 2493 | - { |
|
| 2494 | - return " FROM " . $model_query_info->get_full_join_sql() . |
|
| 2495 | - $model_query_info->get_where_sql() . |
|
| 2496 | - $model_query_info->get_group_by_sql() . |
|
| 2497 | - $model_query_info->get_having_sql() . |
|
| 2498 | - $model_query_info->get_order_by_sql() . |
|
| 2499 | - $model_query_info->get_limit_sql(); |
|
| 2500 | - } |
|
| 2501 | - |
|
| 2502 | - |
|
| 2503 | - |
|
| 2504 | - /** |
|
| 2505 | - * Set to easily debug the next X queries ran from this model. |
|
| 2506 | - * |
|
| 2507 | - * @param int $count |
|
| 2508 | - */ |
|
| 2509 | - public function show_next_x_db_queries($count = 1) |
|
| 2510 | - { |
|
| 2511 | - $this->_show_next_x_db_queries = $count; |
|
| 2512 | - } |
|
| 2513 | - |
|
| 2514 | - |
|
| 2515 | - |
|
| 2516 | - /** |
|
| 2517 | - * @param $sql_query |
|
| 2518 | - */ |
|
| 2519 | - public function show_db_query_if_previously_requested($sql_query) |
|
| 2520 | - { |
|
| 2521 | - if ($this->_show_next_x_db_queries > 0) { |
|
| 2522 | - echo $sql_query; |
|
| 2523 | - $this->_show_next_x_db_queries--; |
|
| 2524 | - } |
|
| 2525 | - } |
|
| 2526 | - |
|
| 2527 | - |
|
| 2528 | - |
|
| 2529 | - /** |
|
| 2530 | - * Adds a relationship of the correct type between $modelObject and $otherModelObject. |
|
| 2531 | - * There are the 3 cases: |
|
| 2532 | - * 'belongsTo' relationship: sets $id_or_obj's foreign_key to be $other_model_id_or_obj's primary_key. If |
|
| 2533 | - * $otherModelObject has no ID, it is first saved. |
|
| 2534 | - * 'hasMany' relationship: sets $other_model_id_or_obj's foreign_key to be $id_or_obj's primary_key. If $id_or_obj |
|
| 2535 | - * has no ID, it is first saved. |
|
| 2536 | - * 'hasAndBelongsToMany' relationships: checks that there isn't already an entry in the join table, and adds one. |
|
| 2537 | - * If one of the model Objects has not yet been saved to the database, it is saved before adding the entry in the |
|
| 2538 | - * join table |
|
| 2539 | - * |
|
| 2540 | - * @param EE_Base_Class /int $thisModelObject |
|
| 2541 | - * @param EE_Base_Class /int $id_or_obj EE_base_Class or ID of other Model Object |
|
| 2542 | - * @param string $relationName , key in EEM_Base::_relations |
|
| 2543 | - * an attendee to a group, you also want to specify which role they |
|
| 2544 | - * will have in that group. So you would use this parameter to |
|
| 2545 | - * specify array('role-column-name'=>'role-id') |
|
| 2546 | - * @param array $extra_join_model_fields_n_values This allows you to enter further query params for the relation |
|
| 2547 | - * to for relation to methods that allow you to further specify |
|
| 2548 | - * extra columns to join by (such as HABTM). Keep in mind that the |
|
| 2549 | - * only acceptable query_params is strict "col" => "value" pairs |
|
| 2550 | - * because these will be inserted in any new rows created as well. |
|
| 2551 | - * @return EE_Base_Class which was added as a relation. Object referred to by $other_model_id_or_obj |
|
| 2552 | - * @throws EE_Error |
|
| 2553 | - */ |
|
| 2554 | - public function add_relationship_to( |
|
| 2555 | - $id_or_obj, |
|
| 2556 | - $other_model_id_or_obj, |
|
| 2557 | - $relationName, |
|
| 2558 | - $extra_join_model_fields_n_values = array() |
|
| 2559 | - ) { |
|
| 2560 | - $relation_obj = $this->related_settings_for($relationName); |
|
| 2561 | - return $relation_obj->add_relation_to($id_or_obj, $other_model_id_or_obj, $extra_join_model_fields_n_values); |
|
| 2562 | - } |
|
| 2563 | - |
|
| 2564 | - |
|
| 2565 | - |
|
| 2566 | - /** |
|
| 2567 | - * Removes a relationship of the correct type between $modelObject and $otherModelObject. |
|
| 2568 | - * There are the 3 cases: |
|
| 2569 | - * 'belongsTo' relationship: sets $modelObject's foreign_key to null, if that field is nullable.Otherwise throws an |
|
| 2570 | - * error |
|
| 2571 | - * 'hasMany' relationship: sets $otherModelObject's foreign_key to null,if that field is nullable.Otherwise throws |
|
| 2572 | - * an error |
|
| 2573 | - * 'hasAndBelongsToMany' relationships:removes any existing entry in the join table between the two models. |
|
| 2574 | - * |
|
| 2575 | - * @param EE_Base_Class /int $id_or_obj |
|
| 2576 | - * @param EE_Base_Class /int $other_model_id_or_obj EE_Base_Class or ID of other Model Object |
|
| 2577 | - * @param string $relationName key in EEM_Base::_relations |
|
| 2578 | - * @return boolean of success |
|
| 2579 | - * @throws EE_Error |
|
| 2580 | - * @param array $where_query This allows you to enter further query params for the relation to for relation to |
|
| 2581 | - * methods that allow you to further specify extra columns to join by (such as HABTM). |
|
| 2582 | - * Keep in mind that the only acceptable query_params is strict "col" => "value" pairs |
|
| 2583 | - * because these will be inserted in any new rows created as well. |
|
| 2584 | - */ |
|
| 2585 | - public function remove_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query = array()) |
|
| 2586 | - { |
|
| 2587 | - $relation_obj = $this->related_settings_for($relationName); |
|
| 2588 | - return $relation_obj->remove_relation_to($id_or_obj, $other_model_id_or_obj, $where_query); |
|
| 2589 | - } |
|
| 2590 | - |
|
| 2591 | - |
|
| 2592 | - |
|
| 2593 | - /** |
|
| 2594 | - * @param mixed $id_or_obj |
|
| 2595 | - * @param string $relationName |
|
| 2596 | - * @param array $where_query_params |
|
| 2597 | - * @param EE_Base_Class[] objects to which relations were removed |
|
| 2598 | - * @return \EE_Base_Class[] |
|
| 2599 | - * @throws EE_Error |
|
| 2600 | - */ |
|
| 2601 | - public function remove_relations($id_or_obj, $relationName, $where_query_params = array()) |
|
| 2602 | - { |
|
| 2603 | - $relation_obj = $this->related_settings_for($relationName); |
|
| 2604 | - return $relation_obj->remove_relations($id_or_obj, $where_query_params); |
|
| 2605 | - } |
|
| 2606 | - |
|
| 2607 | - |
|
| 2608 | - |
|
| 2609 | - /** |
|
| 2610 | - * Gets all the related items of the specified $model_name, using $query_params. |
|
| 2611 | - * Note: by default, we remove the "default query params" |
|
| 2612 | - * because we want to get even deleted items etc. |
|
| 2613 | - * |
|
| 2614 | - * @param mixed $id_or_obj EE_Base_Class child or its ID |
|
| 2615 | - * @param string $model_name like 'Event', 'Registration', etc. always singular |
|
| 2616 | - * @param array $query_params like EEM_Base::get_all |
|
| 2617 | - * @return EE_Base_Class[] |
|
| 2618 | - * @throws EE_Error |
|
| 2619 | - */ |
|
| 2620 | - public function get_all_related($id_or_obj, $model_name, $query_params = null) |
|
| 2621 | - { |
|
| 2622 | - $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2623 | - $relation_settings = $this->related_settings_for($model_name); |
|
| 2624 | - return $relation_settings->get_all_related($model_obj, $query_params); |
|
| 2625 | - } |
|
| 2626 | - |
|
| 2627 | - |
|
| 2628 | - |
|
| 2629 | - /** |
|
| 2630 | - * Deletes all the model objects across the relation indicated by $model_name |
|
| 2631 | - * which are related to $id_or_obj which meet the criteria set in $query_params. |
|
| 2632 | - * However, if the model objects can't be deleted because of blocking related model objects, then |
|
| 2633 | - * they aren't deleted. (Unless the thing that would have been deleted can be soft-deleted, that still happens). |
|
| 2634 | - * |
|
| 2635 | - * @param EE_Base_Class|int|string $id_or_obj |
|
| 2636 | - * @param string $model_name |
|
| 2637 | - * @param array $query_params |
|
| 2638 | - * @return int how many deleted |
|
| 2639 | - * @throws EE_Error |
|
| 2640 | - */ |
|
| 2641 | - public function delete_related($id_or_obj, $model_name, $query_params = array()) |
|
| 2642 | - { |
|
| 2643 | - $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2644 | - $relation_settings = $this->related_settings_for($model_name); |
|
| 2645 | - return $relation_settings->delete_all_related($model_obj, $query_params); |
|
| 2646 | - } |
|
| 2647 | - |
|
| 2648 | - |
|
| 2649 | - |
|
| 2650 | - /** |
|
| 2651 | - * Hard deletes all the model objects across the relation indicated by $model_name |
|
| 2652 | - * which are related to $id_or_obj which meet the criteria set in $query_params. If |
|
| 2653 | - * the model objects can't be hard deleted because of blocking related model objects, |
|
| 2654 | - * just does a soft-delete on them instead. |
|
| 2655 | - * |
|
| 2656 | - * @param EE_Base_Class|int|string $id_or_obj |
|
| 2657 | - * @param string $model_name |
|
| 2658 | - * @param array $query_params |
|
| 2659 | - * @return int how many deleted |
|
| 2660 | - * @throws EE_Error |
|
| 2661 | - */ |
|
| 2662 | - public function delete_related_permanently($id_or_obj, $model_name, $query_params = array()) |
|
| 2663 | - { |
|
| 2664 | - $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2665 | - $relation_settings = $this->related_settings_for($model_name); |
|
| 2666 | - return $relation_settings->delete_related_permanently($model_obj, $query_params); |
|
| 2667 | - } |
|
| 2668 | - |
|
| 2669 | - |
|
| 2670 | - |
|
| 2671 | - /** |
|
| 2672 | - * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2673 | - * unless otherwise specified in the $query_params |
|
| 2674 | - * |
|
| 2675 | - * @param int /EE_Base_Class $id_or_obj |
|
| 2676 | - * @param string $model_name like 'Event', or 'Registration' |
|
| 2677 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2678 | - * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2679 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2680 | - * that by the setting $distinct to TRUE; |
|
| 2681 | - * @return int |
|
| 2682 | - * @throws EE_Error |
|
| 2683 | - */ |
|
| 2684 | - public function count_related( |
|
| 2685 | - $id_or_obj, |
|
| 2686 | - $model_name, |
|
| 2687 | - $query_params = array(), |
|
| 2688 | - $field_to_count = null, |
|
| 2689 | - $distinct = false |
|
| 2690 | - ) { |
|
| 2691 | - $related_model = $this->get_related_model_obj($model_name); |
|
| 2692 | - //we're just going to use the query params on the related model's normal get_all query, |
|
| 2693 | - //except add a condition to say to match the current mod |
|
| 2694 | - if (! isset($query_params['default_where_conditions'])) { |
|
| 2695 | - $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
| 2696 | - } |
|
| 2697 | - $this_model_name = $this->get_this_model_name(); |
|
| 2698 | - $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
| 2699 | - $query_params[0][$this_model_name . "." . $this_pk_field_name] = $id_or_obj; |
|
| 2700 | - return $related_model->count($query_params, $field_to_count, $distinct); |
|
| 2701 | - } |
|
| 2702 | - |
|
| 2703 | - |
|
| 2704 | - |
|
| 2705 | - /** |
|
| 2706 | - * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2707 | - * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2708 | - * |
|
| 2709 | - * @param int /EE_Base_Class $id_or_obj |
|
| 2710 | - * @param string $model_name like 'Event', or 'Registration' |
|
| 2711 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2712 | - * @param string $field_to_sum name of field to count by. By default, uses primary key |
|
| 2713 | - * @return float |
|
| 2714 | - * @throws EE_Error |
|
| 2715 | - */ |
|
| 2716 | - public function sum_related($id_or_obj, $model_name, $query_params, $field_to_sum = null) |
|
| 2717 | - { |
|
| 2718 | - $related_model = $this->get_related_model_obj($model_name); |
|
| 2719 | - if (! is_array($query_params)) { |
|
| 2720 | - EE_Error::doing_it_wrong('EEM_Base::sum_related', |
|
| 2721 | - sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 2722 | - gettype($query_params)), '4.6.0'); |
|
| 2723 | - $query_params = array(); |
|
| 2724 | - } |
|
| 2725 | - //we're just going to use the query params on the related model's normal get_all query, |
|
| 2726 | - //except add a condition to say to match the current mod |
|
| 2727 | - if (! isset($query_params['default_where_conditions'])) { |
|
| 2728 | - $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
| 2729 | - } |
|
| 2730 | - $this_model_name = $this->get_this_model_name(); |
|
| 2731 | - $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
| 2732 | - $query_params[0][$this_model_name . "." . $this_pk_field_name] = $id_or_obj; |
|
| 2733 | - return $related_model->sum($query_params, $field_to_sum); |
|
| 2734 | - } |
|
| 2735 | - |
|
| 2736 | - |
|
| 2737 | - |
|
| 2738 | - /** |
|
| 2739 | - * Uses $this->_relatedModels info to find the first related model object of relation $relationName to the given |
|
| 2740 | - * $modelObject |
|
| 2741 | - * |
|
| 2742 | - * @param int | EE_Base_Class $id_or_obj EE_Base_Class child or its ID |
|
| 2743 | - * @param string $other_model_name , key in $this->_relatedModels, eg 'Registration', or 'Events' |
|
| 2744 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2745 | - * @return EE_Base_Class |
|
| 2746 | - * @throws EE_Error |
|
| 2747 | - */ |
|
| 2748 | - public function get_first_related(EE_Base_Class $id_or_obj, $other_model_name, $query_params) |
|
| 2749 | - { |
|
| 2750 | - $query_params['limit'] = 1; |
|
| 2751 | - $results = $this->get_all_related($id_or_obj, $other_model_name, $query_params); |
|
| 2752 | - if ($results) { |
|
| 2753 | - return array_shift($results); |
|
| 2754 | - } |
|
| 2755 | - return null; |
|
| 2756 | - } |
|
| 2757 | - |
|
| 2758 | - |
|
| 2759 | - |
|
| 2760 | - /** |
|
| 2761 | - * Gets the model's name as it's expected in queries. For example, if this is EEM_Event model, that would be Event |
|
| 2762 | - * |
|
| 2763 | - * @return string |
|
| 2764 | - */ |
|
| 2765 | - public function get_this_model_name() |
|
| 2766 | - { |
|
| 2767 | - return str_replace("EEM_", "", get_class($this)); |
|
| 2768 | - } |
|
| 2769 | - |
|
| 2770 | - |
|
| 2771 | - |
|
| 2772 | - /** |
|
| 2773 | - * Gets the model field on this model which is of type EE_Any_Foreign_Model_Name_Field |
|
| 2774 | - * |
|
| 2775 | - * @return EE_Any_Foreign_Model_Name_Field |
|
| 2776 | - * @throws EE_Error |
|
| 2777 | - */ |
|
| 2778 | - public function get_field_containing_related_model_name() |
|
| 2779 | - { |
|
| 2780 | - foreach ($this->field_settings(true) as $field) { |
|
| 2781 | - if ($field instanceof EE_Any_Foreign_Model_Name_Field) { |
|
| 2782 | - $field_with_model_name = $field; |
|
| 2783 | - } |
|
| 2784 | - } |
|
| 2785 | - if (! isset($field_with_model_name) || ! $field_with_model_name) { |
|
| 2786 | - throw new EE_Error(sprintf(__("There is no EE_Any_Foreign_Model_Name field on model %s", "event_espresso"), |
|
| 2787 | - $this->get_this_model_name())); |
|
| 2788 | - } |
|
| 2789 | - return $field_with_model_name; |
|
| 2790 | - } |
|
| 2791 | - |
|
| 2792 | - |
|
| 2793 | - |
|
| 2794 | - /** |
|
| 2795 | - * Inserts a new entry into the database, for each table. |
|
| 2796 | - * Note: does not add the item to the entity map because that is done by EE_Base_Class::save() right after this. |
|
| 2797 | - * If client code uses EEM_Base::insert() directly, then although the item isn't in the entity map, |
|
| 2798 | - * we also know there is no model object with the newly inserted item's ID at the moment (because |
|
| 2799 | - * if there were, then they would already be in the DB and this would fail); and in the future if someone |
|
| 2800 | - * creates a model object with this ID (or grabs it from the DB) then it will be added to the |
|
| 2801 | - * entity map at that time anyways. SO, no need for EEM_Base::insert ot add to the entity map |
|
| 2802 | - * |
|
| 2803 | - * @param array $field_n_values keys are field names, values are their values (in the client code's domain if |
|
| 2804 | - * $values_already_prepared_by_model_object is false, in the model object's domain if |
|
| 2805 | - * $values_already_prepared_by_model_object is true. See comment about this at the top |
|
| 2806 | - * of EEM_Base) |
|
| 2807 | - * @return int new primary key on main table that got inserted |
|
| 2808 | - * @throws EE_Error |
|
| 2809 | - */ |
|
| 2810 | - public function insert($field_n_values) |
|
| 2811 | - { |
|
| 2812 | - /** |
|
| 2813 | - * Filters the fields and their values before inserting an item using the models |
|
| 2814 | - * |
|
| 2815 | - * @param array $fields_n_values keys are the fields and values are their new values |
|
| 2816 | - * @param EEM_Base $model the model used |
|
| 2817 | - */ |
|
| 2818 | - $field_n_values = (array)apply_filters('FHEE__EEM_Base__insert__fields_n_values', $field_n_values, $this); |
|
| 2819 | - if ($this->_satisfies_unique_indexes($field_n_values)) { |
|
| 2820 | - $main_table = $this->_get_main_table(); |
|
| 2821 | - $new_id = $this->_insert_into_specific_table($main_table, $field_n_values, false); |
|
| 2822 | - if ($new_id !== false) { |
|
| 2823 | - foreach ($this->_get_other_tables() as $other_table) { |
|
| 2824 | - $this->_insert_into_specific_table($other_table, $field_n_values, $new_id); |
|
| 2825 | - } |
|
| 2826 | - } |
|
| 2827 | - /** |
|
| 2828 | - * Done just after attempting to insert a new model object |
|
| 2829 | - * |
|
| 2830 | - * @param EEM_Base $model used |
|
| 2831 | - * @param array $fields_n_values fields and their values |
|
| 2832 | - * @param int|string the ID of the newly-inserted model object |
|
| 2833 | - */ |
|
| 2834 | - do_action('AHEE__EEM_Base__insert__end', $this, $field_n_values, $new_id); |
|
| 2835 | - return $new_id; |
|
| 2836 | - } |
|
| 2837 | - return false; |
|
| 2838 | - } |
|
| 2839 | - |
|
| 2840 | - |
|
| 2841 | - |
|
| 2842 | - /** |
|
| 2843 | - * Checks that the result would satisfy the unique indexes on this model |
|
| 2844 | - * |
|
| 2845 | - * @param array $field_n_values |
|
| 2846 | - * @param string $action |
|
| 2847 | - * @return boolean |
|
| 2848 | - * @throws EE_Error |
|
| 2849 | - */ |
|
| 2850 | - protected function _satisfies_unique_indexes($field_n_values, $action = 'insert') |
|
| 2851 | - { |
|
| 2852 | - foreach ($this->unique_indexes() as $index_name => $index) { |
|
| 2853 | - $uniqueness_where_params = array_intersect_key($field_n_values, $index->fields()); |
|
| 2854 | - if ($this->exists(array($uniqueness_where_params))) { |
|
| 2855 | - EE_Error::add_error( |
|
| 2856 | - sprintf( |
|
| 2857 | - __( |
|
| 2858 | - "Could not %s %s. %s uniqueness index failed. Fields %s must form a unique set, but an entry already exists with values %s.", |
|
| 2859 | - "event_espresso" |
|
| 2860 | - ), |
|
| 2861 | - $action, |
|
| 2862 | - $this->_get_class_name(), |
|
| 2863 | - $index_name, |
|
| 2864 | - implode(",", $index->field_names()), |
|
| 2865 | - http_build_query($uniqueness_where_params) |
|
| 2866 | - ), |
|
| 2867 | - __FILE__, |
|
| 2868 | - __FUNCTION__, |
|
| 2869 | - __LINE__ |
|
| 2870 | - ); |
|
| 2871 | - return false; |
|
| 2872 | - } |
|
| 2873 | - } |
|
| 2874 | - return true; |
|
| 2875 | - } |
|
| 2876 | - |
|
| 2877 | - |
|
| 2878 | - |
|
| 2879 | - /** |
|
| 2880 | - * Checks the database for an item that conflicts (ie, if this item were |
|
| 2881 | - * saved to the DB would break some uniqueness requirement, like a primary key |
|
| 2882 | - * or an index primary key set) with the item specified. $id_obj_or_fields_array |
|
| 2883 | - * can be either an EE_Base_Class or an array of fields n values |
|
| 2884 | - * |
|
| 2885 | - * @param EE_Base_Class|array $obj_or_fields_array |
|
| 2886 | - * @param boolean $include_primary_key whether to use the model object's primary key |
|
| 2887 | - * when looking for conflicts |
|
| 2888 | - * (ie, if false, we ignore the model object's primary key |
|
| 2889 | - * when finding "conflicts". If true, it's also considered). |
|
| 2890 | - * Only works for INT primary key, |
|
| 2891 | - * STRING primary keys cannot be ignored |
|
| 2892 | - * @throws EE_Error |
|
| 2893 | - * @return EE_Base_Class|array |
|
| 2894 | - */ |
|
| 2895 | - public function get_one_conflicting($obj_or_fields_array, $include_primary_key = true) |
|
| 2896 | - { |
|
| 2897 | - if ($obj_or_fields_array instanceof EE_Base_Class) { |
|
| 2898 | - $fields_n_values = $obj_or_fields_array->model_field_array(); |
|
| 2899 | - } elseif (is_array($obj_or_fields_array)) { |
|
| 2900 | - $fields_n_values = $obj_or_fields_array; |
|
| 2901 | - } else { |
|
| 2902 | - throw new EE_Error( |
|
| 2903 | - sprintf( |
|
| 2904 | - __( |
|
| 2905 | - "%s get_all_conflicting should be called with a model object or an array of field names and values, you provided %d", |
|
| 2906 | - "event_espresso" |
|
| 2907 | - ), |
|
| 2908 | - get_class($this), |
|
| 2909 | - $obj_or_fields_array |
|
| 2910 | - ) |
|
| 2911 | - ); |
|
| 2912 | - } |
|
| 2913 | - $query_params = array(); |
|
| 2914 | - if ($this->has_primary_key_field() |
|
| 2915 | - && ($include_primary_key |
|
| 2916 | - || $this->get_primary_key_field() |
|
| 2917 | - instanceof |
|
| 2918 | - EE_Primary_Key_String_Field) |
|
| 2919 | - && isset($fields_n_values[$this->primary_key_name()]) |
|
| 2920 | - ) { |
|
| 2921 | - $query_params[0]['OR'][$this->primary_key_name()] = $fields_n_values[$this->primary_key_name()]; |
|
| 2922 | - } |
|
| 2923 | - foreach ($this->unique_indexes() as $unique_index_name => $unique_index) { |
|
| 2924 | - $uniqueness_where_params = array_intersect_key($fields_n_values, $unique_index->fields()); |
|
| 2925 | - $query_params[0]['OR']['AND*' . $unique_index_name] = $uniqueness_where_params; |
|
| 2926 | - } |
|
| 2927 | - //if there is nothing to base this search on, then we shouldn't find anything |
|
| 2928 | - if (empty($query_params)) { |
|
| 2929 | - return array(); |
|
| 2930 | - } |
|
| 2931 | - return $this->get_one($query_params); |
|
| 2932 | - } |
|
| 2933 | - |
|
| 2934 | - |
|
| 2935 | - |
|
| 2936 | - /** |
|
| 2937 | - * Like count, but is optimized and returns a boolean instead of an int |
|
| 2938 | - * |
|
| 2939 | - * @param array $query_params |
|
| 2940 | - * @return boolean |
|
| 2941 | - * @throws EE_Error |
|
| 2942 | - */ |
|
| 2943 | - public function exists($query_params) |
|
| 2944 | - { |
|
| 2945 | - $query_params['limit'] = 1; |
|
| 2946 | - return $this->count($query_params) > 0; |
|
| 2947 | - } |
|
| 2948 | - |
|
| 2949 | - |
|
| 2950 | - |
|
| 2951 | - /** |
|
| 2952 | - * Wrapper for exists, except ignores default query parameters so we're only considering ID |
|
| 2953 | - * |
|
| 2954 | - * @param int|string $id |
|
| 2955 | - * @return boolean |
|
| 2956 | - * @throws EE_Error |
|
| 2957 | - */ |
|
| 2958 | - public function exists_by_ID($id) |
|
| 2959 | - { |
|
| 2960 | - return $this->exists( |
|
| 2961 | - array( |
|
| 2962 | - 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
| 2963 | - array( |
|
| 2964 | - $this->primary_key_name() => $id, |
|
| 2965 | - ), |
|
| 2966 | - ) |
|
| 2967 | - ); |
|
| 2968 | - } |
|
| 2969 | - |
|
| 2970 | - |
|
| 2971 | - |
|
| 2972 | - /** |
|
| 2973 | - * Inserts a new row in $table, using the $cols_n_values which apply to that table. |
|
| 2974 | - * If a $new_id is supplied and if $table is an EE_Other_Table, we assume |
|
| 2975 | - * we need to add a foreign key column to point to $new_id (which should be the primary key's value |
|
| 2976 | - * on the main table) |
|
| 2977 | - * This is protected rather than private because private is not accessible to any child methods and there MAY be |
|
| 2978 | - * cases where we want to call it directly rather than via insert(). |
|
| 2979 | - * |
|
| 2980 | - * @access protected |
|
| 2981 | - * @param EE_Table_Base $table |
|
| 2982 | - * @param array $fields_n_values each key should be in field's keys, and value should be an int, string or |
|
| 2983 | - * float |
|
| 2984 | - * @param int $new_id for now we assume only int keys |
|
| 2985 | - * @throws EE_Error |
|
| 2986 | - * @global WPDB $wpdb only used to get the $wpdb->insert_id after performing an insert |
|
| 2987 | - * @return int ID of new row inserted, or FALSE on failure |
|
| 2988 | - */ |
|
| 2989 | - protected function _insert_into_specific_table(EE_Table_Base $table, $fields_n_values, $new_id = 0) |
|
| 2990 | - { |
|
| 2991 | - global $wpdb; |
|
| 2992 | - $insertion_col_n_values = array(); |
|
| 2993 | - $format_for_insertion = array(); |
|
| 2994 | - $fields_on_table = $this->_get_fields_for_table($table->get_table_alias()); |
|
| 2995 | - foreach ($fields_on_table as $field_name => $field_obj) { |
|
| 2996 | - //check if its an auto-incrementing column, in which case we should just leave it to do its autoincrement thing |
|
| 2997 | - if ($field_obj->is_auto_increment()) { |
|
| 2998 | - continue; |
|
| 2999 | - } |
|
| 3000 | - $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
| 3001 | - //if the value we want to assign it to is NULL, just don't mention it for the insertion |
|
| 3002 | - if ($prepared_value !== null) { |
|
| 3003 | - $insertion_col_n_values[$field_obj->get_table_column()] = $prepared_value; |
|
| 3004 | - $format_for_insertion[] = $field_obj->get_wpdb_data_type(); |
|
| 3005 | - } |
|
| 3006 | - } |
|
| 3007 | - if ($table instanceof EE_Secondary_Table && $new_id) { |
|
| 3008 | - //its not the main table, so we should have already saved the main table's PK which we just inserted |
|
| 3009 | - //so add the fk to the main table as a column |
|
| 3010 | - $insertion_col_n_values[$table->get_fk_on_table()] = $new_id; |
|
| 3011 | - $format_for_insertion[] = '%d';//yes right now we're only allowing these foreign keys to be INTs |
|
| 3012 | - } |
|
| 3013 | - //insert the new entry |
|
| 3014 | - $result = $this->_do_wpdb_query('insert', |
|
| 3015 | - array($table->get_table_name(), $insertion_col_n_values, $format_for_insertion)); |
|
| 3016 | - if ($result === false) { |
|
| 3017 | - return false; |
|
| 3018 | - } |
|
| 3019 | - //ok, now what do we return for the ID of the newly-inserted thing? |
|
| 3020 | - if ($this->has_primary_key_field()) { |
|
| 3021 | - if ($this->get_primary_key_field()->is_auto_increment()) { |
|
| 3022 | - return $wpdb->insert_id; |
|
| 3023 | - } |
|
| 3024 | - //it's not an auto-increment primary key, so |
|
| 3025 | - //it must have been supplied |
|
| 3026 | - return $fields_n_values[$this->get_primary_key_field()->get_name()]; |
|
| 3027 | - } |
|
| 3028 | - //we can't return a primary key because there is none. instead return |
|
| 3029 | - //a unique string indicating this model |
|
| 3030 | - return $this->get_index_primary_key_string($fields_n_values); |
|
| 3031 | - } |
|
| 3032 | - |
|
| 3033 | - |
|
| 3034 | - |
|
| 3035 | - /** |
|
| 3036 | - * Prepare the $field_obj 's value in $fields_n_values for use in the database. |
|
| 3037 | - * If the field doesn't allow NULL, try to use its default. (If it doesn't allow NULL, |
|
| 3038 | - * and there is no default, we pass it along. WPDB will take care of it) |
|
| 3039 | - * |
|
| 3040 | - * @param EE_Model_Field_Base $field_obj |
|
| 3041 | - * @param array $fields_n_values |
|
| 3042 | - * @return mixed string|int|float depending on what the table column will be expecting |
|
| 3043 | - * @throws EE_Error |
|
| 3044 | - */ |
|
| 3045 | - protected function _prepare_value_or_use_default($field_obj, $fields_n_values) |
|
| 3046 | - { |
|
| 3047 | - //if this field doesn't allow nullable, don't allow it |
|
| 3048 | - if ( |
|
| 3049 | - ! $field_obj->is_nullable() |
|
| 3050 | - && ( |
|
| 3051 | - ! isset($fields_n_values[$field_obj->get_name()]) |
|
| 3052 | - || $fields_n_values[$field_obj->get_name()] === null |
|
| 3053 | - ) |
|
| 3054 | - ) { |
|
| 3055 | - $fields_n_values[$field_obj->get_name()] = $field_obj->get_default_value(); |
|
| 3056 | - } |
|
| 3057 | - $unprepared_value = isset($fields_n_values[$field_obj->get_name()]) |
|
| 3058 | - ? $fields_n_values[$field_obj->get_name()] |
|
| 3059 | - : null; |
|
| 3060 | - return $this->_prepare_value_for_use_in_db($unprepared_value, $field_obj); |
|
| 3061 | - } |
|
| 3062 | - |
|
| 3063 | - |
|
| 3064 | - |
|
| 3065 | - /** |
|
| 3066 | - * Consolidates code for preparing a value supplied to the model for use int eh db. Calls the field's |
|
| 3067 | - * prepare_for_use_in_db method on the value, and depending on $value_already_prepare_by_model_obj, may also call |
|
| 3068 | - * the field's prepare_for_set() method. |
|
| 3069 | - * |
|
| 3070 | - * @param mixed $value value in the client code domain if $value_already_prepared_by_model_object is |
|
| 3071 | - * false, otherwise a value in the model object's domain (see lengthy comment at |
|
| 3072 | - * top of file) |
|
| 3073 | - * @param EE_Model_Field_Base $field field which will be doing the preparing of the value. If null, we assume |
|
| 3074 | - * $value is a custom selection |
|
| 3075 | - * @return mixed a value ready for use in the database for insertions, updating, or in a where clause |
|
| 3076 | - */ |
|
| 3077 | - private function _prepare_value_for_use_in_db($value, $field) |
|
| 3078 | - { |
|
| 3079 | - if ($field && $field instanceof EE_Model_Field_Base) { |
|
| 3080 | - switch ($this->_values_already_prepared_by_model_object) { |
|
| 3081 | - /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 3082 | - case self::not_prepared_by_model_object: |
|
| 3083 | - $value = $field->prepare_for_set($value); |
|
| 3084 | - //purposefully left out "return" |
|
| 3085 | - case self::prepared_by_model_object: |
|
| 3086 | - /** @noinspection SuspiciousAssignmentsInspection */ |
|
| 3087 | - $value = $field->prepare_for_use_in_db($value); |
|
| 3088 | - case self::prepared_for_use_in_db: |
|
| 3089 | - //leave the value alone |
|
| 3090 | - } |
|
| 3091 | - return $value; |
|
| 3092 | - } |
|
| 3093 | - return $value; |
|
| 3094 | - } |
|
| 3095 | - |
|
| 3096 | - |
|
| 3097 | - |
|
| 3098 | - /** |
|
| 3099 | - * Returns the main table on this model |
|
| 3100 | - * |
|
| 3101 | - * @return EE_Primary_Table |
|
| 3102 | - * @throws EE_Error |
|
| 3103 | - */ |
|
| 3104 | - protected function _get_main_table() |
|
| 3105 | - { |
|
| 3106 | - foreach ($this->_tables as $table) { |
|
| 3107 | - if ($table instanceof EE_Primary_Table) { |
|
| 3108 | - return $table; |
|
| 3109 | - } |
|
| 3110 | - } |
|
| 3111 | - throw new EE_Error(sprintf(__('There are no main tables on %s. They should be added to _tables array in the constructor', |
|
| 3112 | - 'event_espresso'), get_class($this))); |
|
| 3113 | - } |
|
| 3114 | - |
|
| 3115 | - |
|
| 3116 | - |
|
| 3117 | - /** |
|
| 3118 | - * table |
|
| 3119 | - * returns EE_Primary_Table table name |
|
| 3120 | - * |
|
| 3121 | - * @return string |
|
| 3122 | - * @throws EE_Error |
|
| 3123 | - */ |
|
| 3124 | - public function table() |
|
| 3125 | - { |
|
| 3126 | - return $this->_get_main_table()->get_table_name(); |
|
| 3127 | - } |
|
| 3128 | - |
|
| 3129 | - |
|
| 3130 | - |
|
| 3131 | - /** |
|
| 3132 | - * table |
|
| 3133 | - * returns first EE_Secondary_Table table name |
|
| 3134 | - * |
|
| 3135 | - * @return string |
|
| 3136 | - */ |
|
| 3137 | - public function second_table() |
|
| 3138 | - { |
|
| 3139 | - // grab second table from tables array |
|
| 3140 | - $second_table = end($this->_tables); |
|
| 3141 | - return $second_table instanceof EE_Secondary_Table ? $second_table->get_table_name() : null; |
|
| 3142 | - } |
|
| 3143 | - |
|
| 3144 | - |
|
| 3145 | - |
|
| 3146 | - /** |
|
| 3147 | - * get_table_obj_by_alias |
|
| 3148 | - * returns table name given it's alias |
|
| 3149 | - * |
|
| 3150 | - * @param string $table_alias |
|
| 3151 | - * @return EE_Primary_Table | EE_Secondary_Table |
|
| 3152 | - */ |
|
| 3153 | - public function get_table_obj_by_alias($table_alias = '') |
|
| 3154 | - { |
|
| 3155 | - return isset($this->_tables[$table_alias]) ? $this->_tables[$table_alias] : null; |
|
| 3156 | - } |
|
| 3157 | - |
|
| 3158 | - |
|
| 3159 | - |
|
| 3160 | - /** |
|
| 3161 | - * Gets all the tables of type EE_Other_Table from EEM_CPT_Basel_Model::_tables |
|
| 3162 | - * |
|
| 3163 | - * @return EE_Secondary_Table[] |
|
| 3164 | - */ |
|
| 3165 | - protected function _get_other_tables() |
|
| 3166 | - { |
|
| 3167 | - $other_tables = array(); |
|
| 3168 | - foreach ($this->_tables as $table_alias => $table) { |
|
| 3169 | - if ($table instanceof EE_Secondary_Table) { |
|
| 3170 | - $other_tables[$table_alias] = $table; |
|
| 3171 | - } |
|
| 3172 | - } |
|
| 3173 | - return $other_tables; |
|
| 3174 | - } |
|
| 3175 | - |
|
| 3176 | - |
|
| 3177 | - |
|
| 3178 | - /** |
|
| 3179 | - * Finds all the fields that correspond to the given table |
|
| 3180 | - * |
|
| 3181 | - * @param string $table_alias , array key in EEM_Base::_tables |
|
| 3182 | - * @return EE_Model_Field_Base[] |
|
| 3183 | - */ |
|
| 3184 | - public function _get_fields_for_table($table_alias) |
|
| 3185 | - { |
|
| 3186 | - return $this->_fields[$table_alias]; |
|
| 3187 | - } |
|
| 3188 | - |
|
| 3189 | - |
|
| 3190 | - |
|
| 3191 | - /** |
|
| 3192 | - * Recurses through all the where parameters, and finds all the related models we'll need |
|
| 3193 | - * to complete this query. Eg, given where parameters like array('EVT_ID'=>3) from within Event model, we won't |
|
| 3194 | - * need any related models. But if the array were array('Registrations.REG_ID'=>3), we'd need the related |
|
| 3195 | - * Registration model. If it were array('Registrations.Transactions.Payments.PAY_ID'=>3), then we'd need the |
|
| 3196 | - * related Registration, Transaction, and Payment models. |
|
| 3197 | - * |
|
| 3198 | - * @param array $query_params like EEM_Base::get_all's $query_parameters['where'] |
|
| 3199 | - * @return EE_Model_Query_Info_Carrier |
|
| 3200 | - * @throws EE_Error |
|
| 3201 | - */ |
|
| 3202 | - public function _extract_related_models_from_query($query_params) |
|
| 3203 | - { |
|
| 3204 | - $query_info_carrier = new EE_Model_Query_Info_Carrier(); |
|
| 3205 | - if (array_key_exists(0, $query_params)) { |
|
| 3206 | - $this->_extract_related_models_from_sub_params_array_keys($query_params[0], $query_info_carrier, 0); |
|
| 3207 | - } |
|
| 3208 | - if (array_key_exists('group_by', $query_params)) { |
|
| 3209 | - if (is_array($query_params['group_by'])) { |
|
| 3210 | - $this->_extract_related_models_from_sub_params_array_values( |
|
| 3211 | - $query_params['group_by'], |
|
| 3212 | - $query_info_carrier, |
|
| 3213 | - 'group_by' |
|
| 3214 | - ); |
|
| 3215 | - } elseif (! empty ($query_params['group_by'])) { |
|
| 3216 | - $this->_extract_related_model_info_from_query_param( |
|
| 3217 | - $query_params['group_by'], |
|
| 3218 | - $query_info_carrier, |
|
| 3219 | - 'group_by' |
|
| 3220 | - ); |
|
| 3221 | - } |
|
| 3222 | - } |
|
| 3223 | - if (array_key_exists('having', $query_params)) { |
|
| 3224 | - $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3225 | - $query_params[0], |
|
| 3226 | - $query_info_carrier, |
|
| 3227 | - 'having' |
|
| 3228 | - ); |
|
| 3229 | - } |
|
| 3230 | - if (array_key_exists('order_by', $query_params)) { |
|
| 3231 | - if (is_array($query_params['order_by'])) { |
|
| 3232 | - $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3233 | - $query_params['order_by'], |
|
| 3234 | - $query_info_carrier, |
|
| 3235 | - 'order_by' |
|
| 3236 | - ); |
|
| 3237 | - } elseif (! empty($query_params['order_by'])) { |
|
| 3238 | - $this->_extract_related_model_info_from_query_param( |
|
| 3239 | - $query_params['order_by'], |
|
| 3240 | - $query_info_carrier, |
|
| 3241 | - 'order_by' |
|
| 3242 | - ); |
|
| 3243 | - } |
|
| 3244 | - } |
|
| 3245 | - if (array_key_exists('force_join', $query_params)) { |
|
| 3246 | - $this->_extract_related_models_from_sub_params_array_values( |
|
| 3247 | - $query_params['force_join'], |
|
| 3248 | - $query_info_carrier, |
|
| 3249 | - 'force_join' |
|
| 3250 | - ); |
|
| 3251 | - } |
|
| 3252 | - $this->extractRelatedModelsFromCustomSelects($query_info_carrier); |
|
| 3253 | - return $query_info_carrier; |
|
| 3254 | - } |
|
| 3255 | - |
|
| 3256 | - |
|
| 3257 | - |
|
| 3258 | - /** |
|
| 3259 | - * For extracting related models from WHERE (0), HAVING (having), ORDER BY (order_by) or forced joins (force_join) |
|
| 3260 | - * |
|
| 3261 | - * @param array $sub_query_params like EEM_Base::get_all's $query_params[0] or |
|
| 3262 | - * $query_params['having'] |
|
| 3263 | - * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
| 3264 | - * @param string $query_param_type one of $this->_allowed_query_params |
|
| 3265 | - * @throws EE_Error |
|
| 3266 | - * @return \EE_Model_Query_Info_Carrier |
|
| 3267 | - */ |
|
| 3268 | - private function _extract_related_models_from_sub_params_array_keys( |
|
| 3269 | - $sub_query_params, |
|
| 3270 | - EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
| 3271 | - $query_param_type |
|
| 3272 | - ) { |
|
| 3273 | - if (! empty($sub_query_params)) { |
|
| 3274 | - $sub_query_params = (array)$sub_query_params; |
|
| 3275 | - foreach ($sub_query_params as $param => $possibly_array_of_params) { |
|
| 3276 | - //$param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
| 3277 | - $this->_extract_related_model_info_from_query_param($param, $model_query_info_carrier, |
|
| 3278 | - $query_param_type); |
|
| 3279 | - //if $possibly_array_of_params is an array, try recursing into it, searching for keys which |
|
| 3280 | - //indicate needed joins. Eg, array('NOT'=>array('Registration.TXN_ID'=>23)). In this case, we tried |
|
| 3281 | - //extracting models out of the 'NOT', which obviously wasn't successful, and then we recurse into the value |
|
| 3282 | - //of array('Registration.TXN_ID'=>23) |
|
| 3283 | - $query_param_sans_stars = $this->_remove_stars_and_anything_after_from_condition_query_param_key($param); |
|
| 3284 | - if (in_array($query_param_sans_stars, $this->_logic_query_param_keys, true)) { |
|
| 3285 | - if (! is_array($possibly_array_of_params)) { |
|
| 3286 | - throw new EE_Error(sprintf(__("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'))", |
|
| 3287 | - "event_espresso"), |
|
| 3288 | - $param, $possibly_array_of_params)); |
|
| 3289 | - } |
|
| 3290 | - $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3291 | - $possibly_array_of_params, |
|
| 3292 | - $model_query_info_carrier, $query_param_type |
|
| 3293 | - ); |
|
| 3294 | - } elseif ($query_param_type === 0 //ie WHERE |
|
| 3295 | - && is_array($possibly_array_of_params) |
|
| 3296 | - && isset($possibly_array_of_params[2]) |
|
| 3297 | - && $possibly_array_of_params[2] == true |
|
| 3298 | - ) { |
|
| 3299 | - //then $possible_array_of_params looks something like array('<','DTT_sold',true) |
|
| 3300 | - //indicating that $possible_array_of_params[1] is actually a field name, |
|
| 3301 | - //from which we should extract query parameters! |
|
| 3302 | - if (! isset($possibly_array_of_params[0], $possibly_array_of_params[1])) { |
|
| 3303 | - throw new EE_Error(sprintf(__("Improperly formed query parameter %s. It should be numerically indexed like array('<','DTT_sold',true); but you provided %s", |
|
| 3304 | - "event_espresso"), $query_param_type, implode(",", $possibly_array_of_params))); |
|
| 3305 | - } |
|
| 3306 | - $this->_extract_related_model_info_from_query_param($possibly_array_of_params[1], |
|
| 3307 | - $model_query_info_carrier, $query_param_type); |
|
| 3308 | - } |
|
| 3309 | - } |
|
| 3310 | - } |
|
| 3311 | - return $model_query_info_carrier; |
|
| 3312 | - } |
|
| 3313 | - |
|
| 3314 | - |
|
| 3315 | - |
|
| 3316 | - /** |
|
| 3317 | - * For extracting related models from forced_joins, where the array values contain the info about what |
|
| 3318 | - * models to join with. Eg an array like array('Attendee','Price.Price_Type'); |
|
| 3319 | - * |
|
| 3320 | - * @param array $sub_query_params like EEM_Base::get_all's $query_params[0] or |
|
| 3321 | - * $query_params['having'] |
|
| 3322 | - * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
| 3323 | - * @param string $query_param_type one of $this->_allowed_query_params |
|
| 3324 | - * @throws EE_Error |
|
| 3325 | - * @return \EE_Model_Query_Info_Carrier |
|
| 3326 | - */ |
|
| 3327 | - private function _extract_related_models_from_sub_params_array_values( |
|
| 3328 | - $sub_query_params, |
|
| 3329 | - EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
| 3330 | - $query_param_type |
|
| 3331 | - ) { |
|
| 3332 | - if (! empty($sub_query_params)) { |
|
| 3333 | - if (! is_array($sub_query_params)) { |
|
| 3334 | - throw new EE_Error(sprintf(__("Query parameter %s should be an array, but it isn't.", "event_espresso"), |
|
| 3335 | - $sub_query_params)); |
|
| 3336 | - } |
|
| 3337 | - foreach ($sub_query_params as $param) { |
|
| 3338 | - //$param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
| 3339 | - $this->_extract_related_model_info_from_query_param($param, $model_query_info_carrier, |
|
| 3340 | - $query_param_type); |
|
| 3341 | - } |
|
| 3342 | - } |
|
| 3343 | - return $model_query_info_carrier; |
|
| 3344 | - } |
|
| 3345 | - |
|
| 3346 | - |
|
| 3347 | - |
|
| 3348 | - /** |
|
| 3349 | - * Extract all the query parts from $query_params (an array like whats passed to EEM_Base::get_all) |
|
| 3350 | - * and put into a EEM_Related_Model_Info_Carrier for easy extraction into a query. We create this object |
|
| 3351 | - * instead of directly constructing the SQL because often we need to extract info from the $query_params |
|
| 3352 | - * but use them in a different order. Eg, we need to know what models we are querying |
|
| 3353 | - * before we know what joins to perform. However, we need to know what data types correspond to which fields on |
|
| 3354 | - * other models before we can finalize the where clause SQL. |
|
| 3355 | - * |
|
| 3356 | - * @param array $query_params |
|
| 3357 | - * @throws EE_Error |
|
| 3358 | - * @return EE_Model_Query_Info_Carrier |
|
| 3359 | - */ |
|
| 3360 | - public function _create_model_query_info_carrier($query_params) |
|
| 3361 | - { |
|
| 3362 | - if (! is_array($query_params)) { |
|
| 3363 | - EE_Error::doing_it_wrong( |
|
| 3364 | - 'EEM_Base::_create_model_query_info_carrier', |
|
| 3365 | - sprintf( |
|
| 3366 | - __( |
|
| 3367 | - '$query_params should be an array, you passed a variable of type %s', |
|
| 3368 | - 'event_espresso' |
|
| 3369 | - ), |
|
| 3370 | - gettype($query_params) |
|
| 3371 | - ), |
|
| 3372 | - '4.6.0' |
|
| 3373 | - ); |
|
| 3374 | - $query_params = array(); |
|
| 3375 | - } |
|
| 3376 | - $where_query_params = isset($query_params[0]) ? $query_params[0] : array(); |
|
| 3377 | - //first check if we should alter the query to account for caps or not |
|
| 3378 | - //because the caps might require us to do extra joins |
|
| 3379 | - if (isset($query_params['caps']) && $query_params['caps'] !== 'none') { |
|
| 3380 | - $query_params[0] = $where_query_params = array_replace_recursive( |
|
| 3381 | - $where_query_params, |
|
| 3382 | - $this->caps_where_conditions( |
|
| 3383 | - $query_params['caps'] |
|
| 3384 | - ) |
|
| 3385 | - ); |
|
| 3386 | - } |
|
| 3387 | - $query_object = $this->_extract_related_models_from_query($query_params); |
|
| 3388 | - //verify where_query_params has NO numeric indexes.... that's simply not how you use it! |
|
| 3389 | - foreach ($where_query_params as $key => $value) { |
|
| 3390 | - if (is_int($key)) { |
|
| 3391 | - throw new EE_Error( |
|
| 3392 | - sprintf( |
|
| 3393 | - __( |
|
| 3394 | - "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.", |
|
| 3395 | - "event_espresso" |
|
| 3396 | - ), |
|
| 3397 | - $key, |
|
| 3398 | - var_export($value, true), |
|
| 3399 | - var_export($query_params, true), |
|
| 3400 | - get_class($this) |
|
| 3401 | - ) |
|
| 3402 | - ); |
|
| 3403 | - } |
|
| 3404 | - } |
|
| 3405 | - if ( |
|
| 3406 | - 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 | - $where_query_params = array_merge( |
|
| 3414 | - $this->_get_default_where_conditions_for_models_in_query( |
|
| 3415 | - $query_object, |
|
| 3416 | - $use_default_where_conditions, |
|
| 3417 | - $where_query_params |
|
| 3418 | - ), |
|
| 3419 | - $where_query_params |
|
| 3420 | - ); |
|
| 3421 | - $query_object->set_where_sql($this->_construct_where_clause($where_query_params)); |
|
| 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 like EEM_Base::get_all() 's $query_params[0] |
|
| 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($cap_where_conditions, |
|
| 3573 | - $restriction_if_no_cap->get_default_where_conditions()); |
|
| 3574 | - } |
|
| 3575 | - return apply_filters('FHEE__EEM_Base__caps_where_conditions__return', $cap_where_conditions, $this, $context, |
|
| 3576 | - $cap_restrictions); |
|
| 3577 | - } |
|
| 3578 | - |
|
| 3579 | - |
|
| 3580 | - |
|
| 3581 | - /** |
|
| 3582 | - * Verifies that $should_be_order_string is in $this->_allowed_order_values, |
|
| 3583 | - * otherwise throws an exception |
|
| 3584 | - * |
|
| 3585 | - * @param string $should_be_order_string |
|
| 3586 | - * @return string either ASC, asc, DESC or desc |
|
| 3587 | - * @throws EE_Error |
|
| 3588 | - */ |
|
| 3589 | - private function _extract_order($should_be_order_string) |
|
| 3590 | - { |
|
| 3591 | - if (in_array($should_be_order_string, $this->_allowed_order_values)) { |
|
| 3592 | - return $should_be_order_string; |
|
| 3593 | - } |
|
| 3594 | - throw new EE_Error( |
|
| 3595 | - sprintf( |
|
| 3596 | - __( |
|
| 3597 | - "While performing a query on '%s', tried to use '%s' as an order parameter. ", |
|
| 3598 | - "event_espresso" |
|
| 3599 | - ), get_class($this), $should_be_order_string |
|
| 3600 | - ) |
|
| 3601 | - ); |
|
| 3602 | - } |
|
| 3603 | - |
|
| 3604 | - |
|
| 3605 | - |
|
| 3606 | - /** |
|
| 3607 | - * Looks at all the models which are included in this query, and asks each |
|
| 3608 | - * for their universal_where_params, and returns them in the same format as $query_params[0] (where), |
|
| 3609 | - * so they can be merged |
|
| 3610 | - * |
|
| 3611 | - * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
| 3612 | - * @param string $use_default_where_conditions can be 'none','other_models_only', or 'all'. |
|
| 3613 | - * 'none' means NO default where conditions will |
|
| 3614 | - * be used AT ALL during this query. |
|
| 3615 | - * 'other_models_only' means default where |
|
| 3616 | - * conditions from other models will be used, but |
|
| 3617 | - * not for this primary model. 'all', the default, |
|
| 3618 | - * means default where conditions will apply as |
|
| 3619 | - * normal |
|
| 3620 | - * @param array $where_query_params like EEM_Base::get_all's $query_params[0] |
|
| 3621 | - * @throws EE_Error |
|
| 3622 | - * @return array like $query_params[0], see EEM_Base::get_all for documentation |
|
| 3623 | - */ |
|
| 3624 | - private function _get_default_where_conditions_for_models_in_query( |
|
| 3625 | - EE_Model_Query_Info_Carrier $query_info_carrier, |
|
| 3626 | - $use_default_where_conditions = EEM_Base::default_where_conditions_all, |
|
| 3627 | - $where_query_params = array() |
|
| 3628 | - ) { |
|
| 3629 | - $allowed_used_default_where_conditions_values = EEM_Base::valid_default_where_conditions(); |
|
| 3630 | - if (! in_array($use_default_where_conditions, $allowed_used_default_where_conditions_values)) { |
|
| 3631 | - throw new EE_Error(sprintf(__("You passed an invalid value to the query parameter 'default_where_conditions' of '%s'. Allowed values are %s", |
|
| 3632 | - "event_espresso"), $use_default_where_conditions, |
|
| 3633 | - implode(", ", $allowed_used_default_where_conditions_values))); |
|
| 3634 | - } |
|
| 3635 | - $universal_query_params = array(); |
|
| 3636 | - if ($this->_should_use_default_where_conditions( $use_default_where_conditions, true)) { |
|
| 3637 | - $universal_query_params = $this->_get_default_where_conditions(); |
|
| 3638 | - } else if ($this->_should_use_minimum_where_conditions( $use_default_where_conditions, true)) { |
|
| 3639 | - $universal_query_params = $this->_get_minimum_where_conditions(); |
|
| 3640 | - } |
|
| 3641 | - foreach ($query_info_carrier->get_model_names_included() as $model_relation_path => $model_name) { |
|
| 3642 | - $related_model = $this->get_related_model_obj($model_name); |
|
| 3643 | - if ( $this->_should_use_default_where_conditions( $use_default_where_conditions, false)) { |
|
| 3644 | - $related_model_universal_where_params = $related_model->_get_default_where_conditions($model_relation_path); |
|
| 3645 | - } elseif ($this->_should_use_minimum_where_conditions( $use_default_where_conditions, false)) { |
|
| 3646 | - $related_model_universal_where_params = $related_model->_get_minimum_where_conditions($model_relation_path); |
|
| 3647 | - } else { |
|
| 3648 | - //we don't want to add full or even minimum default where conditions from this model, so just continue |
|
| 3649 | - continue; |
|
| 3650 | - } |
|
| 3651 | - $overrides = $this->_override_defaults_or_make_null_friendly( |
|
| 3652 | - $related_model_universal_where_params, |
|
| 3653 | - $where_query_params, |
|
| 3654 | - $related_model, |
|
| 3655 | - $model_relation_path |
|
| 3656 | - ); |
|
| 3657 | - $universal_query_params = EEH_Array::merge_arrays_and_overwrite_keys( |
|
| 3658 | - $universal_query_params, |
|
| 3659 | - $overrides |
|
| 3660 | - ); |
|
| 3661 | - } |
|
| 3662 | - return $universal_query_params; |
|
| 3663 | - } |
|
| 3664 | - |
|
| 3665 | - |
|
| 3666 | - |
|
| 3667 | - /** |
|
| 3668 | - * Determines whether or not we should use default where conditions for the model in question |
|
| 3669 | - * (this model, or other related models). |
|
| 3670 | - * Basically, we should use default where conditions on this model if they have requested to use them on all models, |
|
| 3671 | - * this model only, or to use minimum where conditions on all other models and normal where conditions on this one. |
|
| 3672 | - * We should use default where conditions on related models when they requested to use default where conditions |
|
| 3673 | - * on all models, or specifically just on other related models |
|
| 3674 | - * @param $default_where_conditions_value |
|
| 3675 | - * @param bool $for_this_model false means this is for OTHER related models |
|
| 3676 | - * @return bool |
|
| 3677 | - */ |
|
| 3678 | - private function _should_use_default_where_conditions( $default_where_conditions_value, $for_this_model = true ) |
|
| 3679 | - { |
|
| 3680 | - return ( |
|
| 3681 | - $for_this_model |
|
| 3682 | - && in_array( |
|
| 3683 | - $default_where_conditions_value, |
|
| 3684 | - array( |
|
| 3685 | - EEM_Base::default_where_conditions_all, |
|
| 3686 | - EEM_Base::default_where_conditions_this_only, |
|
| 3687 | - EEM_Base::default_where_conditions_minimum_others, |
|
| 3688 | - ), |
|
| 3689 | - true |
|
| 3690 | - ) |
|
| 3691 | - ) |
|
| 3692 | - || ( |
|
| 3693 | - ! $for_this_model |
|
| 3694 | - && in_array( |
|
| 3695 | - $default_where_conditions_value, |
|
| 3696 | - array( |
|
| 3697 | - EEM_Base::default_where_conditions_all, |
|
| 3698 | - EEM_Base::default_where_conditions_others_only, |
|
| 3699 | - ), |
|
| 3700 | - true |
|
| 3701 | - ) |
|
| 3702 | - ); |
|
| 3703 | - } |
|
| 3704 | - |
|
| 3705 | - /** |
|
| 3706 | - * Determines whether or not we should use default minimum conditions for the model in question |
|
| 3707 | - * (this model, or other related models). |
|
| 3708 | - * Basically, we should use minimum where conditions on this model only if they requested all models to use minimum |
|
| 3709 | - * where conditions. |
|
| 3710 | - * We should use minimum where conditions on related models if they requested to use minimum where conditions |
|
| 3711 | - * on this model or others |
|
| 3712 | - * @param $default_where_conditions_value |
|
| 3713 | - * @param bool $for_this_model false means this is for OTHER related models |
|
| 3714 | - * @return bool |
|
| 3715 | - */ |
|
| 3716 | - private function _should_use_minimum_where_conditions($default_where_conditions_value, $for_this_model = true) |
|
| 3717 | - { |
|
| 3718 | - return ( |
|
| 3719 | - $for_this_model |
|
| 3720 | - && $default_where_conditions_value === EEM_Base::default_where_conditions_minimum_all |
|
| 3721 | - ) |
|
| 3722 | - || ( |
|
| 3723 | - ! $for_this_model |
|
| 3724 | - && in_array( |
|
| 3725 | - $default_where_conditions_value, |
|
| 3726 | - array( |
|
| 3727 | - EEM_Base::default_where_conditions_minimum_others, |
|
| 3728 | - EEM_Base::default_where_conditions_minimum_all, |
|
| 3729 | - ), |
|
| 3730 | - true |
|
| 3731 | - ) |
|
| 3732 | - ); |
|
| 3733 | - } |
|
| 3734 | - |
|
| 3735 | - |
|
| 3736 | - /** |
|
| 3737 | - * Checks if any of the defaults have been overridden. If there are any that AREN'T overridden, |
|
| 3738 | - * then we also add a special where condition which allows for that model's primary key |
|
| 3739 | - * to be null (which is important for JOINs. Eg, if you want to see all Events ordered by Venue's name, |
|
| 3740 | - * then Event's with NO Venue won't appear unless you allow VNU_ID to be NULL) |
|
| 3741 | - * |
|
| 3742 | - * @param array $default_where_conditions |
|
| 3743 | - * @param array $provided_where_conditions |
|
| 3744 | - * @param EEM_Base $model |
|
| 3745 | - * @param string $model_relation_path like 'Transaction.Payment.' |
|
| 3746 | - * @return array like EEM_Base::get_all's $query_params[0] |
|
| 3747 | - * @throws EE_Error |
|
| 3748 | - */ |
|
| 3749 | - private function _override_defaults_or_make_null_friendly( |
|
| 3750 | - $default_where_conditions, |
|
| 3751 | - $provided_where_conditions, |
|
| 3752 | - $model, |
|
| 3753 | - $model_relation_path |
|
| 3754 | - ) { |
|
| 3755 | - $null_friendly_where_conditions = array(); |
|
| 3756 | - $none_overridden = true; |
|
| 3757 | - $or_condition_key_for_defaults = 'OR*' . get_class($model); |
|
| 3758 | - foreach ($default_where_conditions as $key => $val) { |
|
| 3759 | - if (isset($provided_where_conditions[$key])) { |
|
| 3760 | - $none_overridden = false; |
|
| 3761 | - } else { |
|
| 3762 | - $null_friendly_where_conditions[$or_condition_key_for_defaults]['AND'][$key] = $val; |
|
| 3763 | - } |
|
| 3764 | - } |
|
| 3765 | - if ($none_overridden && $default_where_conditions) { |
|
| 3766 | - if ($model->has_primary_key_field()) { |
|
| 3767 | - $null_friendly_where_conditions[$or_condition_key_for_defaults][$model_relation_path |
|
| 3768 | - . "." |
|
| 3769 | - . $model->primary_key_name()] = array('IS NULL'); |
|
| 3770 | - }/*else{ |
|
| 37 | + /** |
|
| 38 | + * Flag to indicate whether the values provided to EEM_Base have already been prepared |
|
| 39 | + * by the model object or not (ie, the model object has used the field's _prepare_for_set function on the values). |
|
| 40 | + * They almost always WILL NOT, but it's not necessarily a requirement. |
|
| 41 | + * For example, if you want to run EEM_Event::instance()->get_all(array(array('EVT_ID'=>$_GET['event_id']))); |
|
| 42 | + * |
|
| 43 | + * @var boolean |
|
| 44 | + */ |
|
| 45 | + private $_values_already_prepared_by_model_object = 0; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * when $_values_already_prepared_by_model_object equals this, we assume |
|
| 49 | + * the data is just like form input that needs to have the model fields' |
|
| 50 | + * prepare_for_set and prepare_for_use_in_db called on it |
|
| 51 | + */ |
|
| 52 | + const not_prepared_by_model_object = 0; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * when $_values_already_prepared_by_model_object equals this, we |
|
| 56 | + * assume this value is coming from a model object and doesn't need to have |
|
| 57 | + * prepare_for_set called on it, just prepare_for_use_in_db is used |
|
| 58 | + */ |
|
| 59 | + const prepared_by_model_object = 1; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * when $_values_already_prepared_by_model_object equals this, we assume |
|
| 63 | + * the values are already to be used in the database (ie no processing is done |
|
| 64 | + * on them by the model's fields) |
|
| 65 | + */ |
|
| 66 | + const prepared_for_use_in_db = 2; |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + protected $singular_item = 'Item'; |
|
| 70 | + |
|
| 71 | + protected $plural_item = 'Items'; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @type \EE_Table_Base[] $_tables array of EE_Table objects for defining which tables comprise this model. |
|
| 75 | + */ |
|
| 76 | + protected $_tables; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * with two levels: top-level has array keys which are database table aliases (ie, keys in _tables) |
|
| 80 | + * and the value is an array. Each of those sub-arrays have keys of field names (eg 'ATT_ID', which should also be |
|
| 81 | + * variable names on the model objects (eg, EE_Attendee), and the keys should be children of EE_Model_Field |
|
| 82 | + * |
|
| 83 | + * @var \EE_Model_Field_Base[][] $_fields |
|
| 84 | + */ |
|
| 85 | + protected $_fields; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * array of different kinds of relations |
|
| 89 | + * |
|
| 90 | + * @var \EE_Model_Relation_Base[] $_model_relations |
|
| 91 | + */ |
|
| 92 | + protected $_model_relations; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @var \EE_Index[] $_indexes |
|
| 96 | + */ |
|
| 97 | + protected $_indexes = array(); |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Default strategy for getting where conditions on this model. This strategy is used to get default |
|
| 101 | + * where conditions which are added to get_all, update, and delete queries. They can be overridden |
|
| 102 | + * by setting the same columns as used in these queries in the query yourself. |
|
| 103 | + * |
|
| 104 | + * @var EE_Default_Where_Conditions |
|
| 105 | + */ |
|
| 106 | + protected $_default_where_conditions_strategy; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Strategy for getting conditions on this model when 'default_where_conditions' equals 'minimum'. |
|
| 110 | + * This is particularly useful when you want something between 'none' and 'default' |
|
| 111 | + * |
|
| 112 | + * @var EE_Default_Where_Conditions |
|
| 113 | + */ |
|
| 114 | + protected $_minimum_where_conditions_strategy; |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * String describing how to find the "owner" of this model's objects. |
|
| 118 | + * When there is a foreign key on this model to the wp_users table, this isn't needed. |
|
| 119 | + * But when there isn't, this indicates which related model, or transiently-related model, |
|
| 120 | + * has the foreign key to the wp_users table. |
|
| 121 | + * Eg, for EEM_Registration this would be 'Event' because registrations are directly |
|
| 122 | + * related to events, and events have a foreign key to wp_users. |
|
| 123 | + * On EEM_Transaction, this would be 'Transaction.Event' |
|
| 124 | + * |
|
| 125 | + * @var string |
|
| 126 | + */ |
|
| 127 | + protected $_model_chain_to_wp_user = ''; |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * This is a flag typically set by updates so that we don't load the where strategy on updates because updates |
|
| 131 | + * don't need it (particularly CPT models) |
|
| 132 | + * |
|
| 133 | + * @var bool |
|
| 134 | + */ |
|
| 135 | + protected $_ignore_where_strategy = false; |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * String used in caps relating to this model. Eg, if the caps relating to this |
|
| 139 | + * model are 'ee_edit_events', 'ee_read_events', etc, it would be 'events'. |
|
| 140 | + * |
|
| 141 | + * @var string. If null it hasn't been initialized yet. If false then we |
|
| 142 | + * have indicated capabilities don't apply to this |
|
| 143 | + */ |
|
| 144 | + protected $_caps_slug = null; |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * 2d array where top-level keys are one of EEM_Base::valid_cap_contexts(), |
|
| 148 | + * and next-level keys are capability names, and each's value is a |
|
| 149 | + * EE_Default_Where_Condition. If the requester requests to apply caps to the query, |
|
| 150 | + * they specify which context to use (ie, frontend, backend, edit or delete) |
|
| 151 | + * and then each capability in the corresponding sub-array that they're missing |
|
| 152 | + * adds the where conditions onto the query. |
|
| 153 | + * |
|
| 154 | + * @var array |
|
| 155 | + */ |
|
| 156 | + protected $_cap_restrictions = array( |
|
| 157 | + self::caps_read => array(), |
|
| 158 | + self::caps_read_admin => array(), |
|
| 159 | + self::caps_edit => array(), |
|
| 160 | + self::caps_delete => array(), |
|
| 161 | + ); |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Array defining which cap restriction generators to use to create default |
|
| 165 | + * cap restrictions to put in EEM_Base::_cap_restrictions. |
|
| 166 | + * Array-keys are one of EEM_Base::valid_cap_contexts(), and values are a child of |
|
| 167 | + * EE_Restriction_Generator_Base. If you don't want any cap restrictions generated |
|
| 168 | + * automatically set this to false (not just null). |
|
| 169 | + * |
|
| 170 | + * @var EE_Restriction_Generator_Base[] |
|
| 171 | + */ |
|
| 172 | + protected $_cap_restriction_generators = array(); |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * constants used to categorize capability restrictions on EEM_Base::_caps_restrictions |
|
| 176 | + */ |
|
| 177 | + const caps_read = 'read'; |
|
| 178 | + |
|
| 179 | + const caps_read_admin = 'read_admin'; |
|
| 180 | + |
|
| 181 | + const caps_edit = 'edit'; |
|
| 182 | + |
|
| 183 | + const caps_delete = 'delete'; |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Keys are all the cap contexts (ie constants EEM_Base::_caps_*) and values are their 'action' |
|
| 187 | + * as how they'd be used in capability names. Eg EEM_Base::caps_read ('read_frontend') |
|
| 188 | + * maps to 'read' because when looking for relevant permissions we're going to use |
|
| 189 | + * 'read' in teh capabilities names like 'ee_read_events' etc. |
|
| 190 | + * |
|
| 191 | + * @var array |
|
| 192 | + */ |
|
| 193 | + protected $_cap_contexts_to_cap_action_map = array( |
|
| 194 | + self::caps_read => 'read', |
|
| 195 | + self::caps_read_admin => 'read', |
|
| 196 | + self::caps_edit => 'edit', |
|
| 197 | + self::caps_delete => 'delete', |
|
| 198 | + ); |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Timezone |
|
| 202 | + * This gets set via the constructor so that we know what timezone incoming strings|timestamps are in when there |
|
| 203 | + * are EE_Datetime_Fields in use. This can also be used before a get to set what timezone you want strings coming |
|
| 204 | + * out of the created objects. NOT all EEM_Base child classes use this property but any that use a |
|
| 205 | + * EE_Datetime_Field data type will have access to it. |
|
| 206 | + * |
|
| 207 | + * @var string |
|
| 208 | + */ |
|
| 209 | + protected $_timezone; |
|
| 210 | + |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * This holds the id of the blog currently making the query. Has no bearing on single site but is used for |
|
| 214 | + * multisite. |
|
| 215 | + * |
|
| 216 | + * @var int |
|
| 217 | + */ |
|
| 218 | + protected static $_model_query_blog_id; |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * A copy of _fields, except the array keys are the model names pointed to by |
|
| 222 | + * the field |
|
| 223 | + * |
|
| 224 | + * @var EE_Model_Field_Base[] |
|
| 225 | + */ |
|
| 226 | + private $_cache_foreign_key_to_fields = array(); |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Cached list of all the fields on the model, indexed by their name |
|
| 230 | + * |
|
| 231 | + * @var EE_Model_Field_Base[] |
|
| 232 | + */ |
|
| 233 | + private $_cached_fields = null; |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Cached list of all the fields on the model, except those that are |
|
| 237 | + * marked as only pertinent to the database |
|
| 238 | + * |
|
| 239 | + * @var EE_Model_Field_Base[] |
|
| 240 | + */ |
|
| 241 | + private $_cached_fields_non_db_only = null; |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * A cached reference to the primary key for quick lookup |
|
| 245 | + * |
|
| 246 | + * @var EE_Model_Field_Base |
|
| 247 | + */ |
|
| 248 | + private $_primary_key_field = null; |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Flag indicating whether this model has a primary key or not |
|
| 252 | + * |
|
| 253 | + * @var boolean |
|
| 254 | + */ |
|
| 255 | + protected $_has_primary_key_field = null; |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Whether or not this model is based off a table in WP core only (CPTs should set |
|
| 259 | + * this to FALSE, but if we were to make an EE_WP_Post model, it should set this to true). |
|
| 260 | + * This should be true for models that deal with data that should exist independent of EE. |
|
| 261 | + * For example, if the model can read and insert data that isn't used by EE, this should be true. |
|
| 262 | + * It would be false, however, if you could guarantee the model would only interact with EE data, |
|
| 263 | + * even if it uses a WP core table (eg event and venue models set this to false for that reason: |
|
| 264 | + * they can only read and insert events and venues custom post types, not arbitrary post types) |
|
| 265 | + * @var boolean |
|
| 266 | + */ |
|
| 267 | + protected $_wp_core_model = false; |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * List of valid operators that can be used for querying. |
|
| 271 | + * The keys are all operators we'll accept, the values are the real SQL |
|
| 272 | + * operators used |
|
| 273 | + * |
|
| 274 | + * @var array |
|
| 275 | + */ |
|
| 276 | + protected $_valid_operators = array( |
|
| 277 | + '=' => '=', |
|
| 278 | + '<=' => '<=', |
|
| 279 | + '<' => '<', |
|
| 280 | + '>=' => '>=', |
|
| 281 | + '>' => '>', |
|
| 282 | + '!=' => '!=', |
|
| 283 | + 'LIKE' => 'LIKE', |
|
| 284 | + 'like' => 'LIKE', |
|
| 285 | + 'NOT_LIKE' => 'NOT LIKE', |
|
| 286 | + 'not_like' => 'NOT LIKE', |
|
| 287 | + 'NOT LIKE' => 'NOT LIKE', |
|
| 288 | + 'not like' => 'NOT LIKE', |
|
| 289 | + 'IN' => 'IN', |
|
| 290 | + 'in' => 'IN', |
|
| 291 | + 'NOT_IN' => 'NOT IN', |
|
| 292 | + 'not_in' => 'NOT IN', |
|
| 293 | + 'NOT IN' => 'NOT IN', |
|
| 294 | + 'not in' => 'NOT IN', |
|
| 295 | + 'between' => 'BETWEEN', |
|
| 296 | + 'BETWEEN' => 'BETWEEN', |
|
| 297 | + 'IS_NOT_NULL' => 'IS NOT NULL', |
|
| 298 | + 'is_not_null' => 'IS NOT NULL', |
|
| 299 | + 'IS NOT NULL' => 'IS NOT NULL', |
|
| 300 | + 'is not null' => 'IS NOT NULL', |
|
| 301 | + 'IS_NULL' => 'IS NULL', |
|
| 302 | + 'is_null' => 'IS NULL', |
|
| 303 | + 'IS NULL' => 'IS NULL', |
|
| 304 | + 'is null' => 'IS NULL', |
|
| 305 | + 'REGEXP' => 'REGEXP', |
|
| 306 | + 'regexp' => 'REGEXP', |
|
| 307 | + 'NOT_REGEXP' => 'NOT REGEXP', |
|
| 308 | + 'not_regexp' => 'NOT REGEXP', |
|
| 309 | + 'NOT REGEXP' => 'NOT REGEXP', |
|
| 310 | + 'not regexp' => 'NOT REGEXP', |
|
| 311 | + ); |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * operators that work like 'IN', accepting a comma-separated list of values inside brackets. Eg '(1,2,3)' |
|
| 315 | + * |
|
| 316 | + * @var array |
|
| 317 | + */ |
|
| 318 | + protected $_in_style_operators = array('IN', 'NOT IN'); |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * operators that work like 'BETWEEN'. Typically used for datetime calculations, i.e. "BETWEEN '12-1-2011' AND |
|
| 322 | + * '12-31-2012'" |
|
| 323 | + * |
|
| 324 | + * @var array |
|
| 325 | + */ |
|
| 326 | + protected $_between_style_operators = array('BETWEEN'); |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Operators that work like SQL's like: input should be assumed to be a string, already prepared for a LIKE query. |
|
| 330 | + * @var array |
|
| 331 | + */ |
|
| 332 | + protected $_like_style_operators = array('LIKE', 'NOT LIKE'); |
|
| 333 | + /** |
|
| 334 | + * operators that are used for handling NUll and !NULL queries. Typically used for when checking if a row exists |
|
| 335 | + * on a join table. |
|
| 336 | + * |
|
| 337 | + * @var array |
|
| 338 | + */ |
|
| 339 | + protected $_null_style_operators = array('IS NOT NULL', 'IS NULL'); |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Allowed values for $query_params['order'] for ordering in queries |
|
| 343 | + * |
|
| 344 | + * @var array |
|
| 345 | + */ |
|
| 346 | + protected $_allowed_order_values = array('asc', 'desc', 'ASC', 'DESC'); |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * When these are keys in a WHERE or HAVING clause, they are handled much differently |
|
| 350 | + * than regular field names. It is assumed that their values are an array of WHERE conditions |
|
| 351 | + * |
|
| 352 | + * @var array |
|
| 353 | + */ |
|
| 354 | + private $_logic_query_param_keys = array('not', 'and', 'or', 'NOT', 'AND', 'OR'); |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Allowed keys in $query_params arrays passed into queries. Note that 0 is meant to always be a |
|
| 358 | + * 'where', but 'where' clauses are so common that we thought we'd omit it |
|
| 359 | + * |
|
| 360 | + * @var array |
|
| 361 | + */ |
|
| 362 | + private $_allowed_query_params = array( |
|
| 363 | + 0, |
|
| 364 | + 'limit', |
|
| 365 | + 'order_by', |
|
| 366 | + 'group_by', |
|
| 367 | + 'having', |
|
| 368 | + 'force_join', |
|
| 369 | + 'order', |
|
| 370 | + 'on_join_limit', |
|
| 371 | + 'default_where_conditions', |
|
| 372 | + 'caps', |
|
| 373 | + 'extra_selects' |
|
| 374 | + ); |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * All the data types that can be used in $wpdb->prepare statements. |
|
| 378 | + * |
|
| 379 | + * @var array |
|
| 380 | + */ |
|
| 381 | + private $_valid_wpdb_data_types = array('%d', '%s', '%f'); |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * @var EE_Registry $EE |
|
| 385 | + */ |
|
| 386 | + protected $EE = null; |
|
| 387 | + |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Property which, when set, will have this model echo out the next X queries to the page for debugging. |
|
| 391 | + * |
|
| 392 | + * @var int |
|
| 393 | + */ |
|
| 394 | + protected $_show_next_x_db_queries = 0; |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * When using _get_all_wpdb_results, you can specify a custom selection. If you do so, |
|
| 398 | + * it gets saved on this property as an instance of CustomSelects so those selections can be used in |
|
| 399 | + * WHERE, GROUP_BY, etc. |
|
| 400 | + * |
|
| 401 | + * @var CustomSelects |
|
| 402 | + */ |
|
| 403 | + protected $_custom_selections = array(); |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * key => value Entity Map using array( EEM_Base::$_model_query_blog_id => array( ID => model object ) ) |
|
| 407 | + * caches every model object we've fetched from the DB on this request |
|
| 408 | + * |
|
| 409 | + * @var array |
|
| 410 | + */ |
|
| 411 | + protected $_entity_map; |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * @var LoaderInterface $loader |
|
| 415 | + */ |
|
| 416 | + private static $loader; |
|
| 417 | + |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * constant used to show EEM_Base has not yet verified the db on this http request |
|
| 421 | + */ |
|
| 422 | + const db_verified_none = 0; |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * constant used to show EEM_Base has verified the EE core db on this http request, |
|
| 426 | + * but not the addons' dbs |
|
| 427 | + */ |
|
| 428 | + const db_verified_core = 1; |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * constant used to show EEM_Base has verified the addons' dbs (and implicitly |
|
| 432 | + * the EE core db too) |
|
| 433 | + */ |
|
| 434 | + const db_verified_addons = 2; |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * indicates whether an EEM_Base child has already re-verified the DB |
|
| 438 | + * is ok (we don't want to do it repetitively). Should be set to one the constants |
|
| 439 | + * looking like EEM_Base::db_verified_* |
|
| 440 | + * |
|
| 441 | + * @var int - 0 = none, 1 = core, 2 = addons |
|
| 442 | + */ |
|
| 443 | + protected static $_db_verification_level = EEM_Base::db_verified_none; |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * @const constant for 'default_where_conditions' to apply default where conditions to ALL queried models |
|
| 447 | + * (eg, if retrieving registrations ordered by their datetimes, this will only return non-trashed |
|
| 448 | + * registrations for non-trashed tickets for non-trashed datetimes) |
|
| 449 | + */ |
|
| 450 | + const default_where_conditions_all = 'all'; |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * @const constant for 'default_where_conditions' to apply default where conditions to THIS model only, but |
|
| 454 | + * no other models which are joined to (eg, if retrieving registrations ordered by their datetimes, this will |
|
| 455 | + * return non-trashed registrations, regardless of the related datetimes and tickets' statuses). |
|
| 456 | + * It is preferred to use EEM_Base::default_where_conditions_minimum_others because, when joining to |
|
| 457 | + * models which share tables with other models, this can return data for the wrong model. |
|
| 458 | + */ |
|
| 459 | + const default_where_conditions_this_only = 'this_model_only'; |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * @const constant for 'default_where_conditions' to apply default where conditions to other models queried, |
|
| 463 | + * but not the current model (eg, if retrieving registrations ordered by their datetimes, this will |
|
| 464 | + * return all registrations related to non-trashed tickets and non-trashed datetimes) |
|
| 465 | + */ |
|
| 466 | + const default_where_conditions_others_only = 'other_models_only'; |
|
| 467 | + |
|
| 468 | + /** |
|
| 469 | + * @const constant for 'default_where_conditions' to apply minimum where conditions to all models queried. |
|
| 470 | + * For most models this the same as EEM_Base::default_where_conditions_none, except for models which share |
|
| 471 | + * their table with other models, like the Event and Venue models. For example, when querying for events |
|
| 472 | + * ordered by their venues' name, this will be sure to only return real events with associated real venues |
|
| 473 | + * (regardless of whether those events and venues are trashed) |
|
| 474 | + * In contrast, using EEM_Base::default_where_conditions_none would could return WP posts other than EE |
|
| 475 | + * events. |
|
| 476 | + */ |
|
| 477 | + const default_where_conditions_minimum_all = 'minimum'; |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * @const constant for 'default_where_conditions' to apply apply where conditions to other models, and full default |
|
| 481 | + * where conditions for the queried model (eg, when querying events ordered by venues' names, this will |
|
| 482 | + * return non-trashed events for any venues, regardless of whether those associated venues are trashed or |
|
| 483 | + * not) |
|
| 484 | + */ |
|
| 485 | + const default_where_conditions_minimum_others = 'full_this_minimum_others'; |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * @const constant for 'default_where_conditions' to NOT apply any where conditions. This should very rarely be |
|
| 489 | + * used, because when querying from a model which shares its table with another model (eg Events and Venues) |
|
| 490 | + * it's possible it will return table entries for other models. You should use |
|
| 491 | + * EEM_Base::default_where_conditions_minimum_all instead. |
|
| 492 | + */ |
|
| 493 | + const default_where_conditions_none = 'none'; |
|
| 494 | + |
|
| 495 | + |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * About all child constructors: |
|
| 499 | + * they should define the _tables, _fields and _model_relations arrays. |
|
| 500 | + * Should ALWAYS be called after child constructor. |
|
| 501 | + * In order to make the child constructors to be as simple as possible, this parent constructor |
|
| 502 | + * finalizes constructing all the object's attributes. |
|
| 503 | + * Generally, rather than requiring a child to code |
|
| 504 | + * $this->_tables = array( |
|
| 505 | + * 'Event_Post_Table' => new EE_Table('Event_Post_Table','wp_posts') |
|
| 506 | + * ...); |
|
| 507 | + * (thus repeating itself in the array key and in the constructor of the new EE_Table,) |
|
| 508 | + * each EE_Table has a function to set the table's alias after the constructor, using |
|
| 509 | + * the array key ('Event_Post_Table'), instead of repeating it. The model fields and model relations |
|
| 510 | + * do something similar. |
|
| 511 | + * |
|
| 512 | + * @param null $timezone |
|
| 513 | + * @throws EE_Error |
|
| 514 | + */ |
|
| 515 | + protected function __construct($timezone = null) |
|
| 516 | + { |
|
| 517 | + // check that the model has not been loaded too soon |
|
| 518 | + if (! did_action('AHEE__EE_System__load_espresso_addons')) { |
|
| 519 | + throw new EE_Error ( |
|
| 520 | + sprintf( |
|
| 521 | + __('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.', |
|
| 522 | + 'event_espresso'), |
|
| 523 | + get_class($this) |
|
| 524 | + ) |
|
| 525 | + ); |
|
| 526 | + } |
|
| 527 | + /** |
|
| 528 | + * Set blogid for models to current blog. However we ONLY do this if $_model_query_blog_id is not already set. |
|
| 529 | + */ |
|
| 530 | + if (empty(EEM_Base::$_model_query_blog_id)) { |
|
| 531 | + EEM_Base::set_model_query_blog_id(); |
|
| 532 | + } |
|
| 533 | + /** |
|
| 534 | + * Filters the list of tables on a model. It is best to NOT use this directly and instead |
|
| 535 | + * just use EE_Register_Model_Extension |
|
| 536 | + * |
|
| 537 | + * @var EE_Table_Base[] $_tables |
|
| 538 | + */ |
|
| 539 | + $this->_tables = (array)apply_filters('FHEE__' . get_class($this) . '__construct__tables', $this->_tables); |
|
| 540 | + foreach ($this->_tables as $table_alias => $table_obj) { |
|
| 541 | + /** @var $table_obj EE_Table_Base */ |
|
| 542 | + $table_obj->_construct_finalize_with_alias($table_alias); |
|
| 543 | + if ($table_obj instanceof EE_Secondary_Table) { |
|
| 544 | + /** @var $table_obj EE_Secondary_Table */ |
|
| 545 | + $table_obj->_construct_finalize_set_table_to_join_with($this->_get_main_table()); |
|
| 546 | + } |
|
| 547 | + } |
|
| 548 | + /** |
|
| 549 | + * Filters the list of fields on a model. It is best to NOT use this directly and instead just use |
|
| 550 | + * EE_Register_Model_Extension |
|
| 551 | + * |
|
| 552 | + * @param EE_Model_Field_Base[] $_fields |
|
| 553 | + */ |
|
| 554 | + $this->_fields = (array)apply_filters('FHEE__' . get_class($this) . '__construct__fields', $this->_fields); |
|
| 555 | + $this->_invalidate_field_caches(); |
|
| 556 | + foreach ($this->_fields as $table_alias => $fields_for_table) { |
|
| 557 | + if (! array_key_exists($table_alias, $this->_tables)) { |
|
| 558 | + throw new EE_Error(sprintf(__("Table alias %s does not exist in EEM_Base child's _tables array. Only tables defined are %s", |
|
| 559 | + 'event_espresso'), $table_alias, implode(",", $this->_fields))); |
|
| 560 | + } |
|
| 561 | + foreach ($fields_for_table as $field_name => $field_obj) { |
|
| 562 | + /** @var $field_obj EE_Model_Field_Base | EE_Primary_Key_Field_Base */ |
|
| 563 | + //primary key field base has a slightly different _construct_finalize |
|
| 564 | + /** @var $field_obj EE_Model_Field_Base */ |
|
| 565 | + $field_obj->_construct_finalize($table_alias, $field_name, $this->get_this_model_name()); |
|
| 566 | + } |
|
| 567 | + } |
|
| 568 | + // everything is related to Extra_Meta |
|
| 569 | + if (get_class($this) !== 'EEM_Extra_Meta') { |
|
| 570 | + //make extra meta related to everything, but don't block deleting things just |
|
| 571 | + //because they have related extra meta info. For now just orphan those extra meta |
|
| 572 | + //in the future we should automatically delete them |
|
| 573 | + $this->_model_relations['Extra_Meta'] = new EE_Has_Many_Any_Relation(false); |
|
| 574 | + } |
|
| 575 | + //and change logs |
|
| 576 | + if (get_class($this) !== 'EEM_Change_Log') { |
|
| 577 | + $this->_model_relations['Change_Log'] = new EE_Has_Many_Any_Relation(false); |
|
| 578 | + } |
|
| 579 | + /** |
|
| 580 | + * Filters the list of relations 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_Relation_Base[] $_model_relations |
|
| 584 | + */ |
|
| 585 | + $this->_model_relations = (array)apply_filters('FHEE__' . get_class($this) . '__construct__model_relations', |
|
| 586 | + $this->_model_relations); |
|
| 587 | + foreach ($this->_model_relations as $model_name => $relation_obj) { |
|
| 588 | + /** @var $relation_obj EE_Model_Relation_Base */ |
|
| 589 | + $relation_obj->_construct_finalize_set_models($this->get_this_model_name(), $model_name); |
|
| 590 | + } |
|
| 591 | + foreach ($this->_indexes as $index_name => $index_obj) { |
|
| 592 | + /** @var $index_obj EE_Index */ |
|
| 593 | + $index_obj->_construct_finalize($index_name, $this->get_this_model_name()); |
|
| 594 | + } |
|
| 595 | + $this->set_timezone($timezone); |
|
| 596 | + //finalize default where condition strategy, or set default |
|
| 597 | + if (! $this->_default_where_conditions_strategy) { |
|
| 598 | + //nothing was set during child constructor, so set default |
|
| 599 | + $this->_default_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
| 600 | + } |
|
| 601 | + $this->_default_where_conditions_strategy->_finalize_construct($this); |
|
| 602 | + if (! $this->_minimum_where_conditions_strategy) { |
|
| 603 | + //nothing was set during child constructor, so set default |
|
| 604 | + $this->_minimum_where_conditions_strategy = new EE_Default_Where_Conditions(); |
|
| 605 | + } |
|
| 606 | + $this->_minimum_where_conditions_strategy->_finalize_construct($this); |
|
| 607 | + //if the cap slug hasn't been set, and we haven't set it to false on purpose |
|
| 608 | + //to indicate to NOT set it, set it to the logical default |
|
| 609 | + if ($this->_caps_slug === null) { |
|
| 610 | + $this->_caps_slug = EEH_Inflector::pluralize_and_lower($this->get_this_model_name()); |
|
| 611 | + } |
|
| 612 | + //initialize the standard cap restriction generators if none were specified by the child constructor |
|
| 613 | + if ($this->_cap_restriction_generators !== false) { |
|
| 614 | + foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
|
| 615 | + if (! isset($this->_cap_restriction_generators[$cap_context])) { |
|
| 616 | + $this->_cap_restriction_generators[$cap_context] = apply_filters( |
|
| 617 | + 'FHEE__EEM_Base___construct__standard_cap_restriction_generator', |
|
| 618 | + new EE_Restriction_Generator_Protected(), |
|
| 619 | + $cap_context, |
|
| 620 | + $this |
|
| 621 | + ); |
|
| 622 | + } |
|
| 623 | + } |
|
| 624 | + } |
|
| 625 | + //if there are cap restriction generators, use them to make the default cap restrictions |
|
| 626 | + if ($this->_cap_restriction_generators !== false) { |
|
| 627 | + foreach ($this->_cap_restriction_generators as $context => $generator_object) { |
|
| 628 | + if (! $generator_object) { |
|
| 629 | + continue; |
|
| 630 | + } |
|
| 631 | + if (! $generator_object instanceof EE_Restriction_Generator_Base) { |
|
| 632 | + throw new EE_Error( |
|
| 633 | + sprintf( |
|
| 634 | + __('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.', |
|
| 635 | + 'event_espresso'), |
|
| 636 | + $context, |
|
| 637 | + $this->get_this_model_name() |
|
| 638 | + ) |
|
| 639 | + ); |
|
| 640 | + } |
|
| 641 | + $action = $this->cap_action_for_context($context); |
|
| 642 | + if (! $generator_object->construction_finalized()) { |
|
| 643 | + $generator_object->_construct_finalize($this, $action); |
|
| 644 | + } |
|
| 645 | + } |
|
| 646 | + } |
|
| 647 | + do_action('AHEE__' . get_class($this) . '__construct__end'); |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + |
|
| 651 | + |
|
| 652 | + /** |
|
| 653 | + * Used to set the $_model_query_blog_id static property. |
|
| 654 | + * |
|
| 655 | + * @param int $blog_id If provided then will set the blog_id for the models to this id. If not provided then the |
|
| 656 | + * value for get_current_blog_id() will be used. |
|
| 657 | + */ |
|
| 658 | + public static function set_model_query_blog_id($blog_id = 0) |
|
| 659 | + { |
|
| 660 | + EEM_Base::$_model_query_blog_id = $blog_id > 0 ? (int)$blog_id : get_current_blog_id(); |
|
| 661 | + } |
|
| 662 | + |
|
| 663 | + |
|
| 664 | + |
|
| 665 | + /** |
|
| 666 | + * Returns whatever is set as the internal $model_query_blog_id. |
|
| 667 | + * |
|
| 668 | + * @return int |
|
| 669 | + */ |
|
| 670 | + public static function get_model_query_blog_id() |
|
| 671 | + { |
|
| 672 | + return EEM_Base::$_model_query_blog_id; |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + |
|
| 676 | + |
|
| 677 | + /** |
|
| 678 | + * This function is a singleton method used to instantiate the Espresso_model object |
|
| 679 | + * |
|
| 680 | + * @param string $timezone string representing the timezone we want to set for returned Date Time Strings |
|
| 681 | + * (and any incoming timezone data that gets saved). |
|
| 682 | + * Note this just sends the timezone info to the date time model field objects. |
|
| 683 | + * Default is NULL |
|
| 684 | + * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
|
| 685 | + * @return static (as in the concrete child class) |
|
| 686 | + * @throws EE_Error |
|
| 687 | + * @throws InvalidArgumentException |
|
| 688 | + * @throws InvalidDataTypeException |
|
| 689 | + * @throws InvalidInterfaceException |
|
| 690 | + */ |
|
| 691 | + public static function instance($timezone = null) |
|
| 692 | + { |
|
| 693 | + // check if instance of Espresso_model already exists |
|
| 694 | + if (! static::$_instance instanceof static) { |
|
| 695 | + // instantiate Espresso_model |
|
| 696 | + static::$_instance = new static( |
|
| 697 | + $timezone, |
|
| 698 | + LoaderFactory::getLoader()->load('EventEspresso\core\services\orm\ModelFieldFactory') |
|
| 699 | + ); |
|
| 700 | + } |
|
| 701 | + //we might have a timezone set, let set_timezone decide what to do with it |
|
| 702 | + static::$_instance->set_timezone($timezone); |
|
| 703 | + // Espresso_model object |
|
| 704 | + return static::$_instance; |
|
| 705 | + } |
|
| 706 | + |
|
| 707 | + |
|
| 708 | + |
|
| 709 | + /** |
|
| 710 | + * resets the model and returns it |
|
| 711 | + * |
|
| 712 | + * @param null | string $timezone |
|
| 713 | + * @return EEM_Base|null (if the model was already instantiated, returns it, with |
|
| 714 | + * all its properties reset; if it wasn't instantiated, returns null) |
|
| 715 | + * @throws EE_Error |
|
| 716 | + * @throws ReflectionException |
|
| 717 | + * @throws InvalidArgumentException |
|
| 718 | + * @throws InvalidDataTypeException |
|
| 719 | + * @throws InvalidInterfaceException |
|
| 720 | + */ |
|
| 721 | + public static function reset($timezone = null) |
|
| 722 | + { |
|
| 723 | + if (static::$_instance instanceof EEM_Base) { |
|
| 724 | + //let's try to NOT swap out the current instance for a new one |
|
| 725 | + //because if someone has a reference to it, we can't remove their reference |
|
| 726 | + //so it's best to keep using the same reference, but change the original object |
|
| 727 | + //reset all its properties to their original values as defined in the class |
|
| 728 | + $r = new ReflectionClass(get_class(static::$_instance)); |
|
| 729 | + $static_properties = $r->getStaticProperties(); |
|
| 730 | + foreach ($r->getDefaultProperties() as $property => $value) { |
|
| 731 | + //don't set instance to null like it was originally, |
|
| 732 | + //but it's static anyways, and we're ignoring static properties (for now at least) |
|
| 733 | + if (! isset($static_properties[$property])) { |
|
| 734 | + static::$_instance->{$property} = $value; |
|
| 735 | + } |
|
| 736 | + } |
|
| 737 | + //and then directly call its constructor again, like we would if we were creating a new one |
|
| 738 | + static::$_instance->__construct( |
|
| 739 | + $timezone, |
|
| 740 | + LoaderFactory::getLoader()->load('EventEspresso\core\services\orm\ModelFieldFactory') |
|
| 741 | + ); |
|
| 742 | + return self::instance(); |
|
| 743 | + } |
|
| 744 | + return null; |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + |
|
| 748 | + |
|
| 749 | + /** |
|
| 750 | + * @return LoaderInterface |
|
| 751 | + * @throws InvalidArgumentException |
|
| 752 | + * @throws InvalidDataTypeException |
|
| 753 | + * @throws InvalidInterfaceException |
|
| 754 | + */ |
|
| 755 | + private static function getLoader() |
|
| 756 | + { |
|
| 757 | + if(! EEM_Base::$loader instanceof LoaderInterface) { |
|
| 758 | + EEM_Base::$loader = LoaderFactory::getLoader(); |
|
| 759 | + } |
|
| 760 | + return EEM_Base::$loader; |
|
| 761 | + } |
|
| 762 | + |
|
| 763 | + |
|
| 764 | + |
|
| 765 | + /** |
|
| 766 | + * retrieve the status details from esp_status table as an array IF this model has the status table as a relation. |
|
| 767 | + * |
|
| 768 | + * @param boolean $translated return localized strings or JUST the array. |
|
| 769 | + * @return array |
|
| 770 | + * @throws EE_Error |
|
| 771 | + * @throws InvalidArgumentException |
|
| 772 | + * @throws InvalidDataTypeException |
|
| 773 | + * @throws InvalidInterfaceException |
|
| 774 | + */ |
|
| 775 | + public function status_array($translated = false) |
|
| 776 | + { |
|
| 777 | + if (! array_key_exists('Status', $this->_model_relations)) { |
|
| 778 | + return array(); |
|
| 779 | + } |
|
| 780 | + $model_name = $this->get_this_model_name(); |
|
| 781 | + $status_type = str_replace(' ', '_', strtolower(str_replace('_', ' ', $model_name))); |
|
| 782 | + $stati = EEM_Status::instance()->get_all(array(array('STS_type' => $status_type))); |
|
| 783 | + $status_array = array(); |
|
| 784 | + foreach ($stati as $status) { |
|
| 785 | + $status_array[$status->ID()] = $status->get('STS_code'); |
|
| 786 | + } |
|
| 787 | + return $translated |
|
| 788 | + ? EEM_Status::instance()->localized_status($status_array, false, 'sentence') |
|
| 789 | + : $status_array; |
|
| 790 | + } |
|
| 791 | + |
|
| 792 | + |
|
| 793 | + |
|
| 794 | + /** |
|
| 795 | + * Gets all the EE_Base_Class objects which match the $query_params, by querying the DB. |
|
| 796 | + * |
|
| 797 | + * @param array $query_params { |
|
| 798 | + * @var array $0 (where) array { |
|
| 799 | + * eg: array('QST_display_text'=>'Are you bob?','QST_admin_text'=>'Determine |
|
| 800 | + * if user is bob') becomes SQL >> "...WHERE QST_display_text = 'Are you |
|
| 801 | + * bob?' AND QST_admin_text = 'Determine if user is bob'...") To add WHERE |
|
| 802 | + * conditions based on related models (and even |
|
| 803 | + * models-related-to-related-models) prepend the model's name onto the field |
|
| 804 | + * name. Eg, |
|
| 805 | + * EEM_Event::instance()->get_all(array(array('Venue.VNU_ID'=>12))); becomes |
|
| 806 | + * SQL >> "SELECT * FROM wp_posts AS Event_CPT LEFT JOIN wp_esp_event_meta |
|
| 807 | + * AS Event_Meta ON Event_CPT.ID = Event_Meta.EVT_ID LEFT JOIN |
|
| 808 | + * wp_esp_event_venue AS Event_Venue ON Event_Venue.EVT_ID=Event_CPT.ID LEFT |
|
| 809 | + * JOIN wp_posts AS Venue_CPT ON Venue_CPT.ID=Event_Venue.VNU_ID LEFT JOIN |
|
| 810 | + * wp_esp_venue_meta AS Venue_Meta ON Venue_CPT.ID = Venue_Meta.VNU_ID WHERE |
|
| 811 | + * Venue_CPT.ID = 12 Notice that automatically took care of joining Events |
|
| 812 | + * to Venues (even when each of those models actually consisted of two |
|
| 813 | + * tables). Also, you may chain the model relations together. Eg instead of |
|
| 814 | + * just having |
|
| 815 | + * "Venue.VNU_ID", you could have |
|
| 816 | + * "Registration.Attendee.ATT_ID" as a field on a query for events (because |
|
| 817 | + * events are related to Registrations, which are related to Attendees). You |
|
| 818 | + * can take it even further with |
|
| 819 | + * "Registration.Transaction.Payment.PAY_amount" etc. To change the operator |
|
| 820 | + * (from the default of '='), change the value to an numerically-indexed |
|
| 821 | + * array, where the first item in the list is the operator. eg: array( |
|
| 822 | + * 'QST_display_text' => array('LIKE','%bob%'), 'QST_ID' => array('<',34), |
|
| 823 | + * 'QST_wp_user' => array('in',array(1,2,7,23))) becomes SQL >> "...WHERE |
|
| 824 | + * QST_display_text LIKE '%bob%' AND QST_ID < 34 AND QST_wp_user IN |
|
| 825 | + * (1,2,7,23)...". Valid operators so far: =, !=, <, <=, >, >=, LIKE, NOT |
|
| 826 | + * LIKE, IN (followed by numeric-indexed array), NOT IN (dido), BETWEEN |
|
| 827 | + * (followed by an array with exactly 2 date strings), IS NULL, and IS NOT |
|
| 828 | + * NULL Values can be a string, int, or float. They can also be arrays IFF |
|
| 829 | + * the operator is IN. Also, values can actually be field names. To indicate |
|
| 830 | + * the value is a field, simply provide a third array item (true) to the |
|
| 831 | + * operator-value array like so: eg: array( 'DTT_reg_limit' => array('>', |
|
| 832 | + * 'DTT_sold', TRUE) ) becomes SQL >> "...WHERE DTT_reg_limit > DTT_sold" |
|
| 833 | + * Note: you can also use related model field names like you would any other |
|
| 834 | + * field name. eg: |
|
| 835 | + * array('Datetime.DTT_reg_limit'=>array('=','Datetime.DTT_sold',TRUE) could |
|
| 836 | + * be used if you were querying EEM_Tickets (because Datetime is directly related to tickets) Also, by default all the where conditions are AND'd together. To override this, add an array key 'OR' (or 'AND') and the array to be OR'd together eg: array('OR'=>array('TXN_ID' => 23 , 'TXN_timestamp__>' => |
|
| 837 | + * 345678912)) becomes SQL >> "...WHERE TXN_ID = 23 OR TXN_timestamp = |
|
| 838 | + * 345678912...". Also, to negate an entire set of conditions, use 'NOT' as |
|
| 839 | + * an array key. eg: array('NOT'=>array('TXN_total' => |
|
| 840 | + * 50, 'TXN_paid'=>23) becomes SQL >> "...where ! (TXN_total =50 AND |
|
| 841 | + * TXN_paid =23) Note: the 'glue' used to join each condition will continue |
|
| 842 | + * to be what you last specified. IE, "AND"s by default, but if you had |
|
| 843 | + * previously specified to use ORs to join, ORs will continue to be used. |
|
| 844 | + * So, if you specify to use an "OR" to join conditions, it will continue to |
|
| 845 | + * "stick" until you specify an AND. eg |
|
| 846 | + * array('OR'=>array('NOT'=>array('TXN_total' => 50, |
|
| 847 | + * 'TXN_paid'=>23)),AND=>array('TXN_ID'=>1,'STS_ID'=>'TIN') becomes SQL >> |
|
| 848 | + * "...where ! (TXN_total =50 OR TXN_paid =23) AND TXN_ID=1 AND |
|
| 849 | + * STS_ID='TIN'" They can be nested indefinitely. eg: |
|
| 850 | + * array('OR'=>array('TXN_total' => 23, 'NOT'=> array( 'TXN_timestamp'=> 345678912, 'AND'=>array('TXN_paid' => 53, 'STS_ID' => 'TIN')))) becomes SQL >> "...WHERE TXN_total = 23 OR ! (TXN_timestamp = 345678912 OR (TXN_paid = 53 AND STS_ID = 'TIN'))..." GOTCHA: because this is an array, array keys must be unique, making it impossible to place two or more where conditions applying to the same field. eg: array('PAY_timestamp'=>array('>',$start_date),'PAY_timestamp'=>array('<',$end_date),'PAY_timestamp'=>array('!=',$special_date)), as PHP enforces that the array keys must be unique, thus removing the first two array entries with key 'PAY_timestamp'. becomes SQL >> "PAY_timestamp != 4234232", ignoring the first two PAY_timestamp conditions). To overcome this, you can add a '*' character to the end of the field's name, followed by anything. These will be removed when generating the SQL string, but allow for the array keys to be unique. eg: you could rewrite the previous query as: array('PAY_timestamp'=>array('>',$start_date),'PAY_timestamp*1st'=>array('<',$end_date),'PAY_timestamp*2nd'=>array('!=',$special_date)) which correctly becomes SQL >> |
|
| 851 | + * "PAY_timestamp > 123412341 AND PAY_timestamp < 2354235235234 AND |
|
| 852 | + * PAY_timestamp != 1241234123" This can be applied to condition operators |
|
| 853 | + * too, eg: |
|
| 854 | + * array('OR'=>array('REG_ID'=>3,'Transaction.TXN_ID'=>23),'OR*whatever'=>array('Attendee.ATT_fname'=>'bob','Attendee.ATT_lname'=>'wilson'))); |
|
| 855 | + * @var mixed $limit int|array adds a limit to the query just like the SQL limit clause, so |
|
| 856 | + * limits of "23", "25,50", and array(23,42) are all valid would become SQL |
|
| 857 | + * "...LIMIT 23", "...LIMIT 25,50", and "...LIMIT 23,42" respectively. |
|
| 858 | + * Remember when you provide two numbers for the limit, the 1st number is |
|
| 859 | + * the OFFSET, the 2nd is the LIMIT |
|
| 860 | + * @var array $on_join_limit allows the setting of a special select join with a internal limit so you |
|
| 861 | + * can do paging on one-to-many multi-table-joins. Send an array in the |
|
| 862 | + * following format array('on_join_limit' |
|
| 863 | + * => array( 'table_alias', array(1,2) ) ). |
|
| 864 | + * @var mixed $order_by name of a column to order by, or an array where keys are field names and |
|
| 865 | + * values are either 'ASC' or 'DESC'. |
|
| 866 | + * 'limit'=>array('STS_ID'=>'ASC','REG_date'=>'DESC'), which would becomes |
|
| 867 | + * SQL "...ORDER BY TXN_timestamp..." and "...ORDER BY STS_ID ASC, REG_date |
|
| 868 | + * DESC..." respectively. Like the |
|
| 869 | + * 'where' conditions, these fields can be on related models. Eg |
|
| 870 | + * 'order_by'=>array('Registration.Transaction.TXN_amount'=>'ASC') is |
|
| 871 | + * perfectly valid from any model related to 'Registration' (like Event, |
|
| 872 | + * Attendee, Price, Datetime, etc.) |
|
| 873 | + * @var string $order If 'order_by' is used and its value is a string (NOT an array), then |
|
| 874 | + * 'order' specifies whether to order the field specified in 'order_by' in |
|
| 875 | + * ascending or descending order. Acceptable values are 'ASC' or 'DESC'. If, |
|
| 876 | + * 'order_by' isn't used, but 'order' is, then it is assumed you want to |
|
| 877 | + * order by the primary key. Eg, |
|
| 878 | + * EEM_Event::instance()->get_all(array('order_by'=>'Datetime.DTT_EVT_start','order'=>'ASC'); |
|
| 879 | + * //(will join with the Datetime model's table(s) and order by its field |
|
| 880 | + * DTT_EVT_start) or |
|
| 881 | + * EEM_Registration::instance()->get_all(array('order'=>'ASC'));//will make |
|
| 882 | + * SQL "SELECT * FROM wp_esp_registration ORDER BY REG_ID ASC" |
|
| 883 | + * @var mixed $group_by name of field to order by, or an array of fields. Eg either |
|
| 884 | + * 'group_by'=>'VNU_ID', or |
|
| 885 | + * 'group_by'=>array('EVT_name','Registration.Transaction.TXN_total') Note: |
|
| 886 | + * if no |
|
| 887 | + * $group_by is specified, and a limit is set, automatically groups by the |
|
| 888 | + * model's primary key (or combined primary keys). This avoids some |
|
| 889 | + * weirdness that results when using limits, tons of joins, and no group by, |
|
| 890 | + * see https://events.codebasehq.com/projects/event-espresso/tickets/9389 |
|
| 891 | + * @var array $having exactly like WHERE parameters array, except these conditions apply to the |
|
| 892 | + * grouped results (whereas WHERE conditions apply to the pre-grouped |
|
| 893 | + * results) |
|
| 894 | + * @var array $force_join forces a join with the models named. Should be a numerically-indexed |
|
| 895 | + * array where values are models to be joined in the query.Eg |
|
| 896 | + * array('Attendee','Payment','Datetime'). You may join with transient |
|
| 897 | + * models using period, eg "Registration.Transaction.Payment". You will |
|
| 898 | + * probably only want to do this in hopes of increasing efficiency, as |
|
| 899 | + * related models which belongs to the current model |
|
| 900 | + * (ie, the current model has a foreign key to them, like how Registration |
|
| 901 | + * belongs to Attendee) can be cached in order to avoid future queries |
|
| 902 | + * @var string $default_where_conditions can be set to 'none', 'this_model_only', 'other_models_only', or 'all'. |
|
| 903 | + * set this to 'none' to disable all default where conditions. Eg, usually |
|
| 904 | + * soft-deleted objects are filtered-out if you want to include them, set |
|
| 905 | + * this query param to 'none'. If you want to ONLY disable THIS model's |
|
| 906 | + * default where conditions set it to 'other_models_only'. If you only want |
|
| 907 | + * this model's default where conditions added to the query, use |
|
| 908 | + * 'this_model_only'. If you want to use all default where conditions |
|
| 909 | + * (default), set to 'all'. |
|
| 910 | + * @var string $caps controls what capability requirements to apply to the query; ie, should |
|
| 911 | + * we just NOT apply any capabilities/permissions/restrictions and return |
|
| 912 | + * everything? Or should we only show the current user items they should be |
|
| 913 | + * able to view on the frontend, backend, edit, or delete? can be set to |
|
| 914 | + * 'none' (default), 'read_frontend', 'read_backend', 'edit' or 'delete' |
|
| 915 | + * } |
|
| 916 | + * @return EE_Base_Class[] *note that there is NO option to pass the output type. If you want results different |
|
| 917 | + * from EE_Base_Class[], use _get_all_wpdb_results()and make it public |
|
| 918 | + * again. Array keys are object IDs (if there is a primary key on the model. |
|
| 919 | + * if not, numerically indexed) Some full examples: get 10 transactions |
|
| 920 | + * which have Scottish attendees: EEM_Transaction::instance()->get_all( |
|
| 921 | + * array( array( |
|
| 922 | + * 'OR'=>array( |
|
| 923 | + * 'Registration.Attendee.ATT_fname'=>array('like','Mc%'), |
|
| 924 | + * 'Registration.Attendee.ATT_fname*other'=>array('like','Mac%') |
|
| 925 | + * ) |
|
| 926 | + * ), |
|
| 927 | + * 'limit'=>10, |
|
| 928 | + * 'group_by'=>'TXN_ID' |
|
| 929 | + * )); |
|
| 930 | + * get all the answers to the question titled "shirt size" for event with id |
|
| 931 | + * 12, ordered by their answer EEM_Answer::instance()->get_all(array( array( |
|
| 932 | + * 'Question.QST_display_text'=>'shirt size', |
|
| 933 | + * 'Registration.Event.EVT_ID'=>12 |
|
| 934 | + * ), |
|
| 935 | + * 'order_by'=>array('ANS_value'=>'ASC') |
|
| 936 | + * )); |
|
| 937 | + * @throws EE_Error |
|
| 938 | + */ |
|
| 939 | + public function get_all($query_params = array()) |
|
| 940 | + { |
|
| 941 | + if (isset($query_params['limit']) |
|
| 942 | + && ! isset($query_params['group_by']) |
|
| 943 | + ) { |
|
| 944 | + $query_params['group_by'] = array_keys($this->get_combined_primary_key_fields()); |
|
| 945 | + } |
|
| 946 | + return $this->_create_objects($this->_get_all_wpdb_results($query_params, ARRAY_A, null)); |
|
| 947 | + } |
|
| 948 | + |
|
| 949 | + |
|
| 950 | + |
|
| 951 | + /** |
|
| 952 | + * Modifies the query parameters so we only get back model objects |
|
| 953 | + * that "belong" to the current user |
|
| 954 | + * |
|
| 955 | + * @param array $query_params @see EEM_Base::get_all() |
|
| 956 | + * @return array like EEM_Base::get_all |
|
| 957 | + */ |
|
| 958 | + public function alter_query_params_to_only_include_mine($query_params = array()) |
|
| 959 | + { |
|
| 960 | + $wp_user_field_name = $this->wp_user_field_name(); |
|
| 961 | + if ($wp_user_field_name) { |
|
| 962 | + $query_params[0][$wp_user_field_name] = get_current_user_id(); |
|
| 963 | + } |
|
| 964 | + return $query_params; |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + |
|
| 968 | + |
|
| 969 | + /** |
|
| 970 | + * Returns the name of the field's name that points to the WP_User table |
|
| 971 | + * on this model (or follows the _model_chain_to_wp_user and uses that model's |
|
| 972 | + * foreign key to the WP_User table) |
|
| 973 | + * |
|
| 974 | + * @return string|boolean string on success, boolean false when there is no |
|
| 975 | + * foreign key to the WP_User table |
|
| 976 | + */ |
|
| 977 | + public function wp_user_field_name() |
|
| 978 | + { |
|
| 979 | + try { |
|
| 980 | + if (! empty($this->_model_chain_to_wp_user)) { |
|
| 981 | + $models_to_follow_to_wp_users = explode('.', $this->_model_chain_to_wp_user); |
|
| 982 | + $last_model_name = end($models_to_follow_to_wp_users); |
|
| 983 | + $model_with_fk_to_wp_users = EE_Registry::instance()->load_model($last_model_name); |
|
| 984 | + $model_chain_to_wp_user = $this->_model_chain_to_wp_user . '.'; |
|
| 985 | + } else { |
|
| 986 | + $model_with_fk_to_wp_users = $this; |
|
| 987 | + $model_chain_to_wp_user = ''; |
|
| 988 | + } |
|
| 989 | + $wp_user_field = $model_with_fk_to_wp_users->get_foreign_key_to('WP_User'); |
|
| 990 | + return $model_chain_to_wp_user . $wp_user_field->get_name(); |
|
| 991 | + } catch (EE_Error $e) { |
|
| 992 | + return false; |
|
| 993 | + } |
|
| 994 | + } |
|
| 995 | + |
|
| 996 | + |
|
| 997 | + |
|
| 998 | + /** |
|
| 999 | + * Returns the _model_chain_to_wp_user string, which indicates which related model |
|
| 1000 | + * (or transiently-related model) has a foreign key to the wp_users table; |
|
| 1001 | + * useful for finding if model objects of this type are 'owned' by the current user. |
|
| 1002 | + * This is an empty string when the foreign key is on this model and when it isn't, |
|
| 1003 | + * but is only non-empty when this model's ownership is indicated by a RELATED model |
|
| 1004 | + * (or transiently-related model) |
|
| 1005 | + * |
|
| 1006 | + * @return string |
|
| 1007 | + */ |
|
| 1008 | + public function model_chain_to_wp_user() |
|
| 1009 | + { |
|
| 1010 | + return $this->_model_chain_to_wp_user; |
|
| 1011 | + } |
|
| 1012 | + |
|
| 1013 | + |
|
| 1014 | + |
|
| 1015 | + /** |
|
| 1016 | + * Whether this model is 'owned' by a specific wordpress user (even indirectly, |
|
| 1017 | + * like how registrations don't have a foreign key to wp_users, but the |
|
| 1018 | + * events they are for are), or is unrelated to wp users. |
|
| 1019 | + * generally available |
|
| 1020 | + * |
|
| 1021 | + * @return boolean |
|
| 1022 | + */ |
|
| 1023 | + public function is_owned() |
|
| 1024 | + { |
|
| 1025 | + if ($this->model_chain_to_wp_user()) { |
|
| 1026 | + return true; |
|
| 1027 | + } |
|
| 1028 | + try { |
|
| 1029 | + $this->get_foreign_key_to('WP_User'); |
|
| 1030 | + return true; |
|
| 1031 | + } catch (EE_Error $e) { |
|
| 1032 | + return false; |
|
| 1033 | + } |
|
| 1034 | + } |
|
| 1035 | + |
|
| 1036 | + |
|
| 1037 | + /** |
|
| 1038 | + * Used internally to get WPDB results, because other functions, besides get_all, may want to do some queries, but |
|
| 1039 | + * may want to preserve the WPDB results (eg, update, which first queries to make sure we have all the tables on |
|
| 1040 | + * the model) |
|
| 1041 | + * |
|
| 1042 | + * @param array $query_params like EEM_Base::get_all's $query_params |
|
| 1043 | + * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
| 1044 | + * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
| 1045 | + * fields on the model, and the models we joined to in the query. However, you can |
|
| 1046 | + * override this and set the select to "*", or a specific column name, like |
|
| 1047 | + * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
| 1048 | + * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
| 1049 | + * the aliases used to refer to this selection, and values are to be |
|
| 1050 | + * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
| 1051 | + * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
| 1052 | + * @return array | stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
| 1053 | + * @throws EE_Error |
|
| 1054 | + * @throws InvalidArgumentException |
|
| 1055 | + */ |
|
| 1056 | + protected function _get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
|
| 1057 | + { |
|
| 1058 | + $this->_custom_selections = $this->getCustomSelection($query_params, $columns_to_select);; |
|
| 1059 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1060 | + $select_expressions = $columns_to_select === null |
|
| 1061 | + ? $this->_construct_default_select_sql($model_query_info) |
|
| 1062 | + : ''; |
|
| 1063 | + if ($this->_custom_selections instanceof CustomSelects) { |
|
| 1064 | + $custom_expressions = $this->_custom_selections->columnsToSelectExpression(); |
|
| 1065 | + $select_expressions .= $select_expressions |
|
| 1066 | + ? ', ' . $custom_expressions |
|
| 1067 | + : $custom_expressions; |
|
| 1068 | + } |
|
| 1069 | + |
|
| 1070 | + $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 1071 | + return $this->_do_wpdb_query('get_results', array($SQL, $output)); |
|
| 1072 | + } |
|
| 1073 | + |
|
| 1074 | + |
|
| 1075 | + /** |
|
| 1076 | + * Get a CustomSelects object if the $query_params or $columns_to_select allows for it. |
|
| 1077 | + * Note: $query_params['extra_selects'] will always override any $columns_to_select values. It is the preferred |
|
| 1078 | + * method of including extra select information. |
|
| 1079 | + * |
|
| 1080 | + * @param array $query_params |
|
| 1081 | + * @param null|array|string $columns_to_select |
|
| 1082 | + * @return null|CustomSelects |
|
| 1083 | + * @throws InvalidArgumentException |
|
| 1084 | + */ |
|
| 1085 | + protected function getCustomSelection(array $query_params, $columns_to_select = null) |
|
| 1086 | + { |
|
| 1087 | + if (! isset($query_params['extra_selects']) && $columns_to_select === null) { |
|
| 1088 | + return null; |
|
| 1089 | + } |
|
| 1090 | + $selects = isset($query_params['extra_selects']) ? $query_params['extra_selects'] : $columns_to_select; |
|
| 1091 | + $selects = is_string($selects) ? explode(',', $selects) : $selects; |
|
| 1092 | + return new CustomSelects($selects); |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + |
|
| 1096 | + |
|
| 1097 | + /** |
|
| 1098 | + * Gets an array of rows from the database just like $wpdb->get_results would, |
|
| 1099 | + * but you can use the $query_params like on EEM_Base::get_all() to more easily |
|
| 1100 | + * take care of joins, field preparation etc. |
|
| 1101 | + * |
|
| 1102 | + * @param array $query_params like EEM_Base::get_all's $query_params |
|
| 1103 | + * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
|
| 1104 | + * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
|
| 1105 | + * fields on the model, and the models we joined to in the query. However, you can |
|
| 1106 | + * override this and set the select to "*", or a specific column name, like |
|
| 1107 | + * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
|
| 1108 | + * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
|
| 1109 | + * the aliases used to refer to this selection, and values are to be |
|
| 1110 | + * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
|
| 1111 | + * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
|
| 1112 | + * @return array|stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
|
| 1113 | + * @throws EE_Error |
|
| 1114 | + */ |
|
| 1115 | + public function get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
|
| 1116 | + { |
|
| 1117 | + return $this->_get_all_wpdb_results($query_params, $output, $columns_to_select); |
|
| 1118 | + } |
|
| 1119 | + |
|
| 1120 | + |
|
| 1121 | + |
|
| 1122 | + /** |
|
| 1123 | + * For creating a custom select statement |
|
| 1124 | + * |
|
| 1125 | + * @param mixed $columns_to_select either a string to be inserted directly as the select statement, |
|
| 1126 | + * or an array where keys are aliases, and values are arrays where 0=>the selection |
|
| 1127 | + * SQL, and 1=>is the datatype |
|
| 1128 | + * @throws EE_Error |
|
| 1129 | + * @return string |
|
| 1130 | + */ |
|
| 1131 | + private function _construct_select_from_input($columns_to_select) |
|
| 1132 | + { |
|
| 1133 | + if (is_array($columns_to_select)) { |
|
| 1134 | + $select_sql_array = array(); |
|
| 1135 | + foreach ($columns_to_select as $alias => $selection_and_datatype) { |
|
| 1136 | + if (! is_array($selection_and_datatype) || ! isset($selection_and_datatype[1])) { |
|
| 1137 | + throw new EE_Error( |
|
| 1138 | + sprintf( |
|
| 1139 | + __( |
|
| 1140 | + "Custom selection %s (alias %s) needs to be an array like array('COUNT(REG_ID)','%%d')", |
|
| 1141 | + 'event_espresso' |
|
| 1142 | + ), |
|
| 1143 | + $selection_and_datatype, |
|
| 1144 | + $alias |
|
| 1145 | + ) |
|
| 1146 | + ); |
|
| 1147 | + } |
|
| 1148 | + if (! in_array($selection_and_datatype[1], $this->_valid_wpdb_data_types, true)) { |
|
| 1149 | + throw new EE_Error( |
|
| 1150 | + sprintf( |
|
| 1151 | + esc_html__( |
|
| 1152 | + "Datatype %s (for selection '%s' and alias '%s') is not a valid wpdb datatype (eg %%s)", |
|
| 1153 | + 'event_espresso' |
|
| 1154 | + ), |
|
| 1155 | + $selection_and_datatype[1], |
|
| 1156 | + $selection_and_datatype[0], |
|
| 1157 | + $alias, |
|
| 1158 | + implode(', ', $this->_valid_wpdb_data_types) |
|
| 1159 | + ) |
|
| 1160 | + ); |
|
| 1161 | + } |
|
| 1162 | + $select_sql_array[] = "{$selection_and_datatype[0]} AS $alias"; |
|
| 1163 | + } |
|
| 1164 | + $columns_to_select_string = implode(', ', $select_sql_array); |
|
| 1165 | + } else { |
|
| 1166 | + $columns_to_select_string = $columns_to_select; |
|
| 1167 | + } |
|
| 1168 | + return $columns_to_select_string; |
|
| 1169 | + } |
|
| 1170 | + |
|
| 1171 | + |
|
| 1172 | + |
|
| 1173 | + /** |
|
| 1174 | + * Convenient wrapper for getting the primary key field's name. Eg, on Registration, this would be 'REG_ID' |
|
| 1175 | + * |
|
| 1176 | + * @return string |
|
| 1177 | + * @throws EE_Error |
|
| 1178 | + */ |
|
| 1179 | + public function primary_key_name() |
|
| 1180 | + { |
|
| 1181 | + return $this->get_primary_key_field()->get_name(); |
|
| 1182 | + } |
|
| 1183 | + |
|
| 1184 | + |
|
| 1185 | + |
|
| 1186 | + /** |
|
| 1187 | + * Gets a single item for this model from the DB, given only its ID (or null if none is found). |
|
| 1188 | + * If there is no primary key on this model, $id is treated as primary key string |
|
| 1189 | + * |
|
| 1190 | + * @param mixed $id int or string, depending on the type of the model's primary key |
|
| 1191 | + * @return EE_Base_Class |
|
| 1192 | + */ |
|
| 1193 | + public function get_one_by_ID($id) |
|
| 1194 | + { |
|
| 1195 | + if ($this->get_from_entity_map($id)) { |
|
| 1196 | + return $this->get_from_entity_map($id); |
|
| 1197 | + } |
|
| 1198 | + return $this->get_one( |
|
| 1199 | + $this->alter_query_params_to_restrict_by_ID( |
|
| 1200 | + $id, |
|
| 1201 | + array('default_where_conditions' => EEM_Base::default_where_conditions_minimum_all) |
|
| 1202 | + ) |
|
| 1203 | + ); |
|
| 1204 | + } |
|
| 1205 | + |
|
| 1206 | + |
|
| 1207 | + |
|
| 1208 | + /** |
|
| 1209 | + * Alters query parameters to only get items with this ID are returned. |
|
| 1210 | + * Takes into account that the ID might be a string produced by EEM_Base::get_index_primary_key_string(), |
|
| 1211 | + * or could just be a simple primary key ID |
|
| 1212 | + * |
|
| 1213 | + * @param int $id |
|
| 1214 | + * @param array $query_params |
|
| 1215 | + * @return array of normal query params, @see EEM_Base::get_all |
|
| 1216 | + * @throws EE_Error |
|
| 1217 | + */ |
|
| 1218 | + public function alter_query_params_to_restrict_by_ID($id, $query_params = array()) |
|
| 1219 | + { |
|
| 1220 | + if (! isset($query_params[0])) { |
|
| 1221 | + $query_params[0] = array(); |
|
| 1222 | + } |
|
| 1223 | + $conditions_from_id = $this->parse_index_primary_key_string($id); |
|
| 1224 | + if ($conditions_from_id === null) { |
|
| 1225 | + $query_params[0][$this->primary_key_name()] = $id; |
|
| 1226 | + } else { |
|
| 1227 | + //no primary key, so the $id must be from the get_index_primary_key_string() |
|
| 1228 | + $query_params[0] = array_replace_recursive($query_params[0], $this->parse_index_primary_key_string($id)); |
|
| 1229 | + } |
|
| 1230 | + return $query_params; |
|
| 1231 | + } |
|
| 1232 | + |
|
| 1233 | + |
|
| 1234 | + |
|
| 1235 | + /** |
|
| 1236 | + * Gets a single item for this model from the DB, given the $query_params. Only returns a single class, not an |
|
| 1237 | + * array. If no item is found, null is returned. |
|
| 1238 | + * |
|
| 1239 | + * @param array $query_params like EEM_Base's $query_params variable. |
|
| 1240 | + * @return EE_Base_Class|EE_Soft_Delete_Base_Class|NULL |
|
| 1241 | + * @throws EE_Error |
|
| 1242 | + */ |
|
| 1243 | + public function get_one($query_params = array()) |
|
| 1244 | + { |
|
| 1245 | + if (! is_array($query_params)) { |
|
| 1246 | + EE_Error::doing_it_wrong('EEM_Base::get_one', |
|
| 1247 | + sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1248 | + gettype($query_params)), '4.6.0'); |
|
| 1249 | + $query_params = array(); |
|
| 1250 | + } |
|
| 1251 | + $query_params['limit'] = 1; |
|
| 1252 | + $items = $this->get_all($query_params); |
|
| 1253 | + if (empty($items)) { |
|
| 1254 | + return null; |
|
| 1255 | + } |
|
| 1256 | + return array_shift($items); |
|
| 1257 | + } |
|
| 1258 | + |
|
| 1259 | + |
|
| 1260 | + |
|
| 1261 | + /** |
|
| 1262 | + * Returns the next x number of items in sequence from the given value as |
|
| 1263 | + * found in the database matching the given query conditions. |
|
| 1264 | + * |
|
| 1265 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1266 | + * @param null $field_to_order_by What field is used for the |
|
| 1267 | + * reference point. |
|
| 1268 | + * @param int $limit How many to return. |
|
| 1269 | + * @param array $query_params Extra conditions on the query. |
|
| 1270 | + * @param null $columns_to_select If left null, then an array of |
|
| 1271 | + * EE_Base_Class objects is returned, |
|
| 1272 | + * otherwise you can indicate just the |
|
| 1273 | + * columns you want returned. |
|
| 1274 | + * @return EE_Base_Class[]|array |
|
| 1275 | + * @throws EE_Error |
|
| 1276 | + */ |
|
| 1277 | + public function next_x( |
|
| 1278 | + $current_field_value, |
|
| 1279 | + $field_to_order_by = null, |
|
| 1280 | + $limit = 1, |
|
| 1281 | + $query_params = array(), |
|
| 1282 | + $columns_to_select = null |
|
| 1283 | + ) { |
|
| 1284 | + return $this->_get_consecutive( |
|
| 1285 | + $current_field_value, |
|
| 1286 | + '>', |
|
| 1287 | + $field_to_order_by, |
|
| 1288 | + $limit, |
|
| 1289 | + $query_params, |
|
| 1290 | + $columns_to_select |
|
| 1291 | + ); |
|
| 1292 | + } |
|
| 1293 | + |
|
| 1294 | + |
|
| 1295 | + |
|
| 1296 | + /** |
|
| 1297 | + * Returns the previous x number of items in sequence from the given value |
|
| 1298 | + * as found in the database matching the given query conditions. |
|
| 1299 | + * |
|
| 1300 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1301 | + * @param null $field_to_order_by What field is used for the |
|
| 1302 | + * reference point. |
|
| 1303 | + * @param int $limit How many to return. |
|
| 1304 | + * @param array $query_params Extra conditions on the query. |
|
| 1305 | + * @param null $columns_to_select If left null, then an array of |
|
| 1306 | + * EE_Base_Class objects is returned, |
|
| 1307 | + * otherwise you can indicate just the |
|
| 1308 | + * columns you want returned. |
|
| 1309 | + * @return EE_Base_Class[]|array |
|
| 1310 | + * @throws EE_Error |
|
| 1311 | + */ |
|
| 1312 | + public function previous_x( |
|
| 1313 | + $current_field_value, |
|
| 1314 | + $field_to_order_by = null, |
|
| 1315 | + $limit = 1, |
|
| 1316 | + $query_params = array(), |
|
| 1317 | + $columns_to_select = null |
|
| 1318 | + ) { |
|
| 1319 | + return $this->_get_consecutive( |
|
| 1320 | + $current_field_value, |
|
| 1321 | + '<', |
|
| 1322 | + $field_to_order_by, |
|
| 1323 | + $limit, |
|
| 1324 | + $query_params, |
|
| 1325 | + $columns_to_select |
|
| 1326 | + ); |
|
| 1327 | + } |
|
| 1328 | + |
|
| 1329 | + |
|
| 1330 | + |
|
| 1331 | + /** |
|
| 1332 | + * Returns the next item in sequence from the given value as found in the |
|
| 1333 | + * database matching the given query conditions. |
|
| 1334 | + * |
|
| 1335 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1336 | + * @param null $field_to_order_by What field is used for the |
|
| 1337 | + * reference point. |
|
| 1338 | + * @param array $query_params Extra conditions on the query. |
|
| 1339 | + * @param null $columns_to_select If left null, then an EE_Base_Class |
|
| 1340 | + * object is returned, otherwise you |
|
| 1341 | + * can indicate just the columns you |
|
| 1342 | + * want and a single array indexed by |
|
| 1343 | + * the columns will be returned. |
|
| 1344 | + * @return EE_Base_Class|null|array() |
|
| 1345 | + * @throws EE_Error |
|
| 1346 | + */ |
|
| 1347 | + public function next( |
|
| 1348 | + $current_field_value, |
|
| 1349 | + $field_to_order_by = null, |
|
| 1350 | + $query_params = array(), |
|
| 1351 | + $columns_to_select = null |
|
| 1352 | + ) { |
|
| 1353 | + $results = $this->_get_consecutive( |
|
| 1354 | + $current_field_value, |
|
| 1355 | + '>', |
|
| 1356 | + $field_to_order_by, |
|
| 1357 | + 1, |
|
| 1358 | + $query_params, |
|
| 1359 | + $columns_to_select |
|
| 1360 | + ); |
|
| 1361 | + return empty($results) ? null : reset($results); |
|
| 1362 | + } |
|
| 1363 | + |
|
| 1364 | + |
|
| 1365 | + |
|
| 1366 | + /** |
|
| 1367 | + * Returns the previous item in sequence from the given value as found in |
|
| 1368 | + * the database matching the given query conditions. |
|
| 1369 | + * |
|
| 1370 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1371 | + * @param null $field_to_order_by What field is used for the |
|
| 1372 | + * reference point. |
|
| 1373 | + * @param array $query_params Extra conditions on the query. |
|
| 1374 | + * @param null $columns_to_select If left null, then an EE_Base_Class |
|
| 1375 | + * object is returned, otherwise you |
|
| 1376 | + * can indicate just the columns you |
|
| 1377 | + * want and a single array indexed by |
|
| 1378 | + * the columns will be returned. |
|
| 1379 | + * @return EE_Base_Class|null|array() |
|
| 1380 | + * @throws EE_Error |
|
| 1381 | + */ |
|
| 1382 | + public function previous( |
|
| 1383 | + $current_field_value, |
|
| 1384 | + $field_to_order_by = null, |
|
| 1385 | + $query_params = array(), |
|
| 1386 | + $columns_to_select = null |
|
| 1387 | + ) { |
|
| 1388 | + $results = $this->_get_consecutive( |
|
| 1389 | + $current_field_value, |
|
| 1390 | + '<', |
|
| 1391 | + $field_to_order_by, |
|
| 1392 | + 1, |
|
| 1393 | + $query_params, |
|
| 1394 | + $columns_to_select |
|
| 1395 | + ); |
|
| 1396 | + return empty($results) ? null : reset($results); |
|
| 1397 | + } |
|
| 1398 | + |
|
| 1399 | + |
|
| 1400 | + |
|
| 1401 | + /** |
|
| 1402 | + * Returns the a consecutive number of items in sequence from the given |
|
| 1403 | + * value as found in the database matching the given query conditions. |
|
| 1404 | + * |
|
| 1405 | + * @param mixed $current_field_value Value used for the reference point. |
|
| 1406 | + * @param string $operand What operand is used for the sequence. |
|
| 1407 | + * @param string $field_to_order_by What field is used for the reference point. |
|
| 1408 | + * @param int $limit How many to return. |
|
| 1409 | + * @param array $query_params Extra conditions on the query. |
|
| 1410 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, |
|
| 1411 | + * otherwise you can indicate just the columns you want returned. |
|
| 1412 | + * @return EE_Base_Class[]|array |
|
| 1413 | + * @throws EE_Error |
|
| 1414 | + */ |
|
| 1415 | + protected function _get_consecutive( |
|
| 1416 | + $current_field_value, |
|
| 1417 | + $operand = '>', |
|
| 1418 | + $field_to_order_by = null, |
|
| 1419 | + $limit = 1, |
|
| 1420 | + $query_params = array(), |
|
| 1421 | + $columns_to_select = null |
|
| 1422 | + ) { |
|
| 1423 | + //if $field_to_order_by is empty then let's assume we're ordering by the primary key. |
|
| 1424 | + if (empty($field_to_order_by)) { |
|
| 1425 | + if ($this->has_primary_key_field()) { |
|
| 1426 | + $field_to_order_by = $this->get_primary_key_field()->get_name(); |
|
| 1427 | + } else { |
|
| 1428 | + if (WP_DEBUG) { |
|
| 1429 | + throw new EE_Error(__('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).', |
|
| 1430 | + 'event_espresso')); |
|
| 1431 | + } |
|
| 1432 | + EE_Error::add_error(__('There was an error with the query.', 'event_espresso')); |
|
| 1433 | + return array(); |
|
| 1434 | + } |
|
| 1435 | + } |
|
| 1436 | + if (! is_array($query_params)) { |
|
| 1437 | + EE_Error::doing_it_wrong('EEM_Base::_get_consecutive', |
|
| 1438 | + sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1439 | + gettype($query_params)), '4.6.0'); |
|
| 1440 | + $query_params = array(); |
|
| 1441 | + } |
|
| 1442 | + //let's add the where query param for consecutive look up. |
|
| 1443 | + $query_params[0][$field_to_order_by] = array($operand, $current_field_value); |
|
| 1444 | + $query_params['limit'] = $limit; |
|
| 1445 | + //set direction |
|
| 1446 | + $incoming_orderby = isset($query_params['order_by']) ? (array)$query_params['order_by'] : array(); |
|
| 1447 | + $query_params['order_by'] = $operand === '>' |
|
| 1448 | + ? array($field_to_order_by => 'ASC') + $incoming_orderby |
|
| 1449 | + : array($field_to_order_by => 'DESC') + $incoming_orderby; |
|
| 1450 | + //if $columns_to_select is empty then that means we're returning EE_Base_Class objects |
|
| 1451 | + if (empty($columns_to_select)) { |
|
| 1452 | + return $this->get_all($query_params); |
|
| 1453 | + } |
|
| 1454 | + //getting just the fields |
|
| 1455 | + return $this->_get_all_wpdb_results($query_params, ARRAY_A, $columns_to_select); |
|
| 1456 | + } |
|
| 1457 | + |
|
| 1458 | + |
|
| 1459 | + |
|
| 1460 | + /** |
|
| 1461 | + * This sets the _timezone property after model object has been instantiated. |
|
| 1462 | + * |
|
| 1463 | + * @param null | string $timezone valid PHP DateTimeZone timezone string |
|
| 1464 | + */ |
|
| 1465 | + public function set_timezone($timezone) |
|
| 1466 | + { |
|
| 1467 | + if ($timezone !== null) { |
|
| 1468 | + $this->_timezone = $timezone; |
|
| 1469 | + } |
|
| 1470 | + //note we need to loop through relations and set the timezone on those objects as well. |
|
| 1471 | + foreach ($this->_model_relations as $relation) { |
|
| 1472 | + $relation->set_timezone($timezone); |
|
| 1473 | + } |
|
| 1474 | + //and finally we do the same for any datetime fields |
|
| 1475 | + foreach ($this->_fields as $field) { |
|
| 1476 | + if ($field instanceof EE_Datetime_Field) { |
|
| 1477 | + $field->set_timezone($timezone); |
|
| 1478 | + } |
|
| 1479 | + } |
|
| 1480 | + } |
|
| 1481 | + |
|
| 1482 | + |
|
| 1483 | + |
|
| 1484 | + /** |
|
| 1485 | + * This just returns whatever is set for the current timezone. |
|
| 1486 | + * |
|
| 1487 | + * @access public |
|
| 1488 | + * @return string |
|
| 1489 | + */ |
|
| 1490 | + public function get_timezone() |
|
| 1491 | + { |
|
| 1492 | + //first validate if timezone is set. If not, then let's set it be whatever is set on the model fields. |
|
| 1493 | + if (empty($this->_timezone)) { |
|
| 1494 | + foreach ($this->_fields as $field) { |
|
| 1495 | + if ($field instanceof EE_Datetime_Field) { |
|
| 1496 | + $this->set_timezone($field->get_timezone()); |
|
| 1497 | + break; |
|
| 1498 | + } |
|
| 1499 | + } |
|
| 1500 | + } |
|
| 1501 | + //if timezone STILL empty then return the default timezone for the site. |
|
| 1502 | + if (empty($this->_timezone)) { |
|
| 1503 | + $this->set_timezone(EEH_DTT_Helper::get_timezone()); |
|
| 1504 | + } |
|
| 1505 | + return $this->_timezone; |
|
| 1506 | + } |
|
| 1507 | + |
|
| 1508 | + |
|
| 1509 | + |
|
| 1510 | + /** |
|
| 1511 | + * This returns the date formats set for the given field name and also ensures that |
|
| 1512 | + * $this->_timezone property is set correctly. |
|
| 1513 | + * |
|
| 1514 | + * @since 4.6.x |
|
| 1515 | + * @param string $field_name The name of the field the formats are being retrieved for. |
|
| 1516 | + * @param bool $pretty Whether to return the pretty formats (true) or not (false). |
|
| 1517 | + * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
| 1518 | + * @return array formats in an array with the date format first, and the time format last. |
|
| 1519 | + */ |
|
| 1520 | + public function get_formats_for($field_name, $pretty = false) |
|
| 1521 | + { |
|
| 1522 | + $field_settings = $this->field_settings_for($field_name); |
|
| 1523 | + //if not a valid EE_Datetime_Field then throw error |
|
| 1524 | + if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1525 | + throw new EE_Error(sprintf(__('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.', |
|
| 1526 | + 'event_espresso'), $field_name)); |
|
| 1527 | + } |
|
| 1528 | + //while we are here, let's make sure the timezone internally in EEM_Base matches what is stored on |
|
| 1529 | + //the field. |
|
| 1530 | + $this->_timezone = $field_settings->get_timezone(); |
|
| 1531 | + return array($field_settings->get_date_format($pretty), $field_settings->get_time_format($pretty)); |
|
| 1532 | + } |
|
| 1533 | + |
|
| 1534 | + |
|
| 1535 | + |
|
| 1536 | + /** |
|
| 1537 | + * This returns the current time in a format setup for a query on this model. |
|
| 1538 | + * Usage of this method makes it easier to setup queries against EE_Datetime_Field columns because |
|
| 1539 | + * it will return: |
|
| 1540 | + * - a formatted string in the timezone and format currently set on the EE_Datetime_Field for the given field for |
|
| 1541 | + * NOW |
|
| 1542 | + * - or a unix timestamp (equivalent to time()) |
|
| 1543 | + * Note: When requesting a formatted string, if the date or time format doesn't include seconds, for example, |
|
| 1544 | + * the time returned, because it uses that format, will also NOT include seconds. For this reason, if you want |
|
| 1545 | + * the time returned to be the current time down to the exact second, set $timestamp to true. |
|
| 1546 | + * @since 4.6.x |
|
| 1547 | + * @param string $field_name The field the current time is needed for. |
|
| 1548 | + * @param bool $timestamp True means to return a unix timestamp. Otherwise a |
|
| 1549 | + * formatted string matching the set format for the field in the set timezone will |
|
| 1550 | + * be returned. |
|
| 1551 | + * @param string $what Whether to return the string in just the time format, the date format, or both. |
|
| 1552 | + * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
|
| 1553 | + * @return int|string If the given field_name is not of the EE_Datetime_Field type, then an EE_Error |
|
| 1554 | + * exception is triggered. |
|
| 1555 | + */ |
|
| 1556 | + public function current_time_for_query($field_name, $timestamp = false, $what = 'both') |
|
| 1557 | + { |
|
| 1558 | + $formats = $this->get_formats_for($field_name); |
|
| 1559 | + $DateTime = new DateTime("now", new DateTimeZone($this->_timezone)); |
|
| 1560 | + if ($timestamp) { |
|
| 1561 | + return $DateTime->format('U'); |
|
| 1562 | + } |
|
| 1563 | + //not returning timestamp, so return formatted string in timezone. |
|
| 1564 | + switch ($what) { |
|
| 1565 | + case 'time' : |
|
| 1566 | + return $DateTime->format($formats[1]); |
|
| 1567 | + break; |
|
| 1568 | + case 'date' : |
|
| 1569 | + return $DateTime->format($formats[0]); |
|
| 1570 | + break; |
|
| 1571 | + default : |
|
| 1572 | + return $DateTime->format(implode(' ', $formats)); |
|
| 1573 | + break; |
|
| 1574 | + } |
|
| 1575 | + } |
|
| 1576 | + |
|
| 1577 | + |
|
| 1578 | + |
|
| 1579 | + /** |
|
| 1580 | + * This receives a time string for a given field and ensures that it is setup to match what the internal settings |
|
| 1581 | + * for the model are. Returns a DateTime object. |
|
| 1582 | + * Note: a gotcha for when you send in unix timestamp. Remember a unix timestamp is already timezone agnostic, |
|
| 1583 | + * (functionally the equivalent of UTC+0). So when you send it in, whatever timezone string you include is |
|
| 1584 | + * ignored. |
|
| 1585 | + * |
|
| 1586 | + * @param string $field_name The field being setup. |
|
| 1587 | + * @param string $timestring The date time string being used. |
|
| 1588 | + * @param string $incoming_format The format for the time string. |
|
| 1589 | + * @param string $timezone By default, it is assumed the incoming time string is in timezone for |
|
| 1590 | + * the blog. If this is not the case, then it can be specified here. If incoming |
|
| 1591 | + * format is |
|
| 1592 | + * 'U', this is ignored. |
|
| 1593 | + * @return DateTime |
|
| 1594 | + * @throws EE_Error |
|
| 1595 | + */ |
|
| 1596 | + public function convert_datetime_for_query($field_name, $timestring, $incoming_format, $timezone = '') |
|
| 1597 | + { |
|
| 1598 | + //just using this to ensure the timezone is set correctly internally |
|
| 1599 | + $this->get_formats_for($field_name); |
|
| 1600 | + //load EEH_DTT_Helper |
|
| 1601 | + $set_timezone = empty($timezone) ? EEH_DTT_Helper::get_timezone() : $timezone; |
|
| 1602 | + $incomingDateTime = date_create_from_format($incoming_format, $timestring, new DateTimeZone($set_timezone)); |
|
| 1603 | + return \EventEspresso\core\domain\entities\DbSafeDateTime::createFromDateTime( $incomingDateTime->setTimezone(new DateTimeZone($this->_timezone)) ); |
|
| 1604 | + } |
|
| 1605 | + |
|
| 1606 | + |
|
| 1607 | + |
|
| 1608 | + /** |
|
| 1609 | + * Gets all the tables comprising this model. Array keys are the table aliases, and values are EE_Table objects |
|
| 1610 | + * |
|
| 1611 | + * @return EE_Table_Base[] |
|
| 1612 | + */ |
|
| 1613 | + public function get_tables() |
|
| 1614 | + { |
|
| 1615 | + return $this->_tables; |
|
| 1616 | + } |
|
| 1617 | + |
|
| 1618 | + |
|
| 1619 | + |
|
| 1620 | + /** |
|
| 1621 | + * Updates all the database entries (in each table for this model) according to $fields_n_values and optionally |
|
| 1622 | + * also updates all the model objects, where the criteria expressed in $query_params are met.. |
|
| 1623 | + * Also note: if this model has multiple tables, this update verifies all the secondary tables have an entry for |
|
| 1624 | + * each row (in the primary table) we're trying to update; if not, it inserts an entry in the secondary table. Eg: |
|
| 1625 | + * if our model has 2 tables: wp_posts (primary), and wp_esp_event (secondary). Let's say we are trying to update a |
|
| 1626 | + * model object with EVT_ID = 1 |
|
| 1627 | + * (which means where wp_posts has ID = 1, because wp_posts.ID is the primary key's column), which exists, but |
|
| 1628 | + * there is no entry in wp_esp_event for this entry in wp_posts. So, this update script will insert a row into |
|
| 1629 | + * wp_esp_event, using any available parameters from $fields_n_values (eg, if "EVT_limit" => 40 is in |
|
| 1630 | + * $fields_n_values, the new entry in wp_esp_event will set EVT_limit = 40, and use default for other columns which |
|
| 1631 | + * are not specified) |
|
| 1632 | + * |
|
| 1633 | + * @param array $fields_n_values keys are model fields (exactly like keys in EEM_Base::_fields, NOT db |
|
| 1634 | + * columns!), values are strings, ints, floats, and maybe arrays if they |
|
| 1635 | + * are to be serialized. Basically, the values are what you'd expect to be |
|
| 1636 | + * values on the model, NOT necessarily what's in the DB. For example, if |
|
| 1637 | + * we wanted to update only the TXN_details on any Transactions where its |
|
| 1638 | + * ID=34, we'd use this method as follows: |
|
| 1639 | + * EEM_Transaction::instance()->update( |
|
| 1640 | + * array('TXN_details'=>array('detail1'=>'monkey','detail2'=>'banana'), |
|
| 1641 | + * array(array('TXN_ID'=>34))); |
|
| 1642 | + * @param array $query_params very much like EEM_Base::get_all's $query_params |
|
| 1643 | + * in client code into what's expected to be stored on each field. Eg, |
|
| 1644 | + * consider updating Question's QST_admin_label field is of type |
|
| 1645 | + * Simple_HTML. If you use this function to update that field to $new_value |
|
| 1646 | + * = (note replace 8's with appropriate opening and closing tags in the |
|
| 1647 | + * following example)"8script8alert('I hack all');8/script88b8boom |
|
| 1648 | + * baby8/b8", then if you set $values_already_prepared_by_model_object to |
|
| 1649 | + * TRUE, it is assumed that you've already called |
|
| 1650 | + * EE_Simple_HTML_Field->prepare_for_set($new_value), which removes the |
|
| 1651 | + * malicious javascript. However, if |
|
| 1652 | + * $values_already_prepared_by_model_object is left as FALSE, then |
|
| 1653 | + * EE_Simple_HTML_Field->prepare_for_set($new_value) will be called on it, |
|
| 1654 | + * and every other field, before insertion. We provide this parameter |
|
| 1655 | + * because model objects perform their prepare_for_set function on all |
|
| 1656 | + * their values, and so don't need to be called again (and in many cases, |
|
| 1657 | + * shouldn't be called again. Eg: if we escape HTML characters in the |
|
| 1658 | + * prepare_for_set method...) |
|
| 1659 | + * @param boolean $keep_model_objs_in_sync if TRUE, makes sure we ALSO update model objects |
|
| 1660 | + * in this model's entity map according to $fields_n_values that match |
|
| 1661 | + * $query_params. This obviously has some overhead, so you can disable it |
|
| 1662 | + * by setting this to FALSE, but be aware that model objects being used |
|
| 1663 | + * could get out-of-sync with the database |
|
| 1664 | + * @return int how many rows got updated or FALSE if something went wrong with the query (wp returns FALSE or num |
|
| 1665 | + * rows affected which *could* include 0 which DOES NOT mean the query was |
|
| 1666 | + * bad) |
|
| 1667 | + * @throws EE_Error |
|
| 1668 | + */ |
|
| 1669 | + public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true) |
|
| 1670 | + { |
|
| 1671 | + if (! is_array($query_params)) { |
|
| 1672 | + EE_Error::doing_it_wrong('EEM_Base::update', |
|
| 1673 | + sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 1674 | + gettype($query_params)), '4.6.0'); |
|
| 1675 | + $query_params = array(); |
|
| 1676 | + } |
|
| 1677 | + /** |
|
| 1678 | + * Action called before a model update call has been made. |
|
| 1679 | + * |
|
| 1680 | + * @param EEM_Base $model |
|
| 1681 | + * @param array $fields_n_values the updated fields and their new values |
|
| 1682 | + * @param array $query_params @see EEM_Base::get_all() |
|
| 1683 | + */ |
|
| 1684 | + do_action('AHEE__EEM_Base__update__begin', $this, $fields_n_values, $query_params); |
|
| 1685 | + /** |
|
| 1686 | + * Filters the fields about to be updated given the query parameters. You can provide the |
|
| 1687 | + * $query_params to $this->get_all() to find exactly which records will be updated |
|
| 1688 | + * |
|
| 1689 | + * @param array $fields_n_values fields and their new values |
|
| 1690 | + * @param EEM_Base $model the model being queried |
|
| 1691 | + * @param array $query_params see EEM_Base::get_all() |
|
| 1692 | + */ |
|
| 1693 | + $fields_n_values = (array)apply_filters('FHEE__EEM_Base__update__fields_n_values', $fields_n_values, $this, |
|
| 1694 | + $query_params); |
|
| 1695 | + //need to verify that, for any entry we want to update, there are entries in each secondary table. |
|
| 1696 | + //to do that, for each table, verify that it's PK isn't null. |
|
| 1697 | + $tables = $this->get_tables(); |
|
| 1698 | + //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 |
|
| 1699 | + //NOTE: we should make this code more efficient by NOT querying twice |
|
| 1700 | + //before the real update, but that needs to first go through ALPHA testing |
|
| 1701 | + //as it's dangerous. says Mike August 8 2014 |
|
| 1702 | + //we want to make sure the default_where strategy is ignored |
|
| 1703 | + $this->_ignore_where_strategy = true; |
|
| 1704 | + $wpdb_select_results = $this->_get_all_wpdb_results($query_params); |
|
| 1705 | + foreach ($wpdb_select_results as $wpdb_result) { |
|
| 1706 | + // type cast stdClass as array |
|
| 1707 | + $wpdb_result = (array)$wpdb_result; |
|
| 1708 | + //get the model object's PK, as we'll want this if we need to insert a row into secondary tables |
|
| 1709 | + if ($this->has_primary_key_field()) { |
|
| 1710 | + $main_table_pk_value = $wpdb_result[$this->get_primary_key_field()->get_qualified_column()]; |
|
| 1711 | + } else { |
|
| 1712 | + //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) |
|
| 1713 | + $main_table_pk_value = null; |
|
| 1714 | + } |
|
| 1715 | + //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 |
|
| 1716 | + //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 |
|
| 1717 | + if (count($tables) > 1) { |
|
| 1718 | + //foreach matching row in the DB, ensure that each table's PK isn't null. If so, there must not be an entry |
|
| 1719 | + //in that table, and so we'll want to insert one |
|
| 1720 | + foreach ($tables as $table_obj) { |
|
| 1721 | + $this_table_pk_column = $table_obj->get_fully_qualified_pk_column(); |
|
| 1722 | + //if there is no private key for this table on the results, it means there's no entry |
|
| 1723 | + //in this table, right? so insert a row in the current table, using any fields available |
|
| 1724 | + if (! (array_key_exists($this_table_pk_column, $wpdb_result) |
|
| 1725 | + && $wpdb_result[$this_table_pk_column]) |
|
| 1726 | + ) { |
|
| 1727 | + $success = $this->_insert_into_specific_table($table_obj, $fields_n_values, |
|
| 1728 | + $main_table_pk_value); |
|
| 1729 | + //if we died here, report the error |
|
| 1730 | + if (! $success) { |
|
| 1731 | + return false; |
|
| 1732 | + } |
|
| 1733 | + } |
|
| 1734 | + } |
|
| 1735 | + } |
|
| 1736 | + // //and now check that if we have cached any models by that ID on the model, that |
|
| 1737 | + // //they also get updated properly |
|
| 1738 | + // $model_object = $this->get_from_entity_map( $main_table_pk_value ); |
|
| 1739 | + // if( $model_object ){ |
|
| 1740 | + // foreach( $fields_n_values as $field => $value ){ |
|
| 1741 | + // $model_object->set($field, $value); |
|
| 1742 | + //let's make sure default_where strategy is followed now |
|
| 1743 | + $this->_ignore_where_strategy = false; |
|
| 1744 | + } |
|
| 1745 | + //if we want to keep model objects in sync, AND |
|
| 1746 | + //if this wasn't called from a model object (to update itself) |
|
| 1747 | + //then we want to make sure we keep all the existing |
|
| 1748 | + //model objects in sync with the db |
|
| 1749 | + if ($keep_model_objs_in_sync && ! $this->_values_already_prepared_by_model_object) { |
|
| 1750 | + if ($this->has_primary_key_field()) { |
|
| 1751 | + $model_objs_affected_ids = $this->get_col($query_params); |
|
| 1752 | + } else { |
|
| 1753 | + //we need to select a bunch of columns and then combine them into the the "index primary key string"s |
|
| 1754 | + $models_affected_key_columns = $this->_get_all_wpdb_results($query_params, ARRAY_A); |
|
| 1755 | + $model_objs_affected_ids = array(); |
|
| 1756 | + foreach ($models_affected_key_columns as $row) { |
|
| 1757 | + $combined_index_key = $this->get_index_primary_key_string($row); |
|
| 1758 | + $model_objs_affected_ids[$combined_index_key] = $combined_index_key; |
|
| 1759 | + } |
|
| 1760 | + } |
|
| 1761 | + if (! $model_objs_affected_ids) { |
|
| 1762 | + //wait wait wait- if nothing was affected let's stop here |
|
| 1763 | + return 0; |
|
| 1764 | + } |
|
| 1765 | + foreach ($model_objs_affected_ids as $id) { |
|
| 1766 | + $model_obj_in_entity_map = $this->get_from_entity_map($id); |
|
| 1767 | + if ($model_obj_in_entity_map) { |
|
| 1768 | + foreach ($fields_n_values as $field => $new_value) { |
|
| 1769 | + $model_obj_in_entity_map->set($field, $new_value); |
|
| 1770 | + } |
|
| 1771 | + } |
|
| 1772 | + } |
|
| 1773 | + //if there is a primary key on this model, we can now do a slight optimization |
|
| 1774 | + if ($this->has_primary_key_field()) { |
|
| 1775 | + //we already know what we want to update. So let's make the query simpler so it's a little more efficient |
|
| 1776 | + $query_params = array( |
|
| 1777 | + array($this->primary_key_name() => array('IN', $model_objs_affected_ids)), |
|
| 1778 | + 'limit' => count($model_objs_affected_ids), |
|
| 1779 | + 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
| 1780 | + ); |
|
| 1781 | + } |
|
| 1782 | + } |
|
| 1783 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1784 | + $SQL = "UPDATE " |
|
| 1785 | + . $model_query_info->get_full_join_sql() |
|
| 1786 | + . " SET " |
|
| 1787 | + . $this->_construct_update_sql($fields_n_values) |
|
| 1788 | + . $model_query_info->get_where_sql();//note: doesn't use _construct_2nd_half_of_select_query() because doesn't accept LIMIT, ORDER BY, etc. |
|
| 1789 | + $rows_affected = $this->_do_wpdb_query('query', array($SQL)); |
|
| 1790 | + /** |
|
| 1791 | + * Action called after a model update call has been made. |
|
| 1792 | + * |
|
| 1793 | + * @param EEM_Base $model |
|
| 1794 | + * @param array $fields_n_values the updated fields and their new values |
|
| 1795 | + * @param array $query_params @see EEM_Base::get_all() |
|
| 1796 | + * @param int $rows_affected |
|
| 1797 | + */ |
|
| 1798 | + do_action('AHEE__EEM_Base__update__end', $this, $fields_n_values, $query_params, $rows_affected); |
|
| 1799 | + return $rows_affected;//how many supposedly got updated |
|
| 1800 | + } |
|
| 1801 | + |
|
| 1802 | + |
|
| 1803 | + |
|
| 1804 | + /** |
|
| 1805 | + * Analogous to $wpdb->get_col, returns a 1-dimensional array where teh values |
|
| 1806 | + * are teh values of the field specified (or by default the primary key field) |
|
| 1807 | + * that matched the query params. Note that you should pass the name of the |
|
| 1808 | + * model FIELD, not the database table's column name. |
|
| 1809 | + * |
|
| 1810 | + * @param array $query_params @see EEM_Base::get_all() |
|
| 1811 | + * @param string $field_to_select |
|
| 1812 | + * @return array just like $wpdb->get_col() |
|
| 1813 | + * @throws EE_Error |
|
| 1814 | + */ |
|
| 1815 | + public function get_col($query_params = array(), $field_to_select = null) |
|
| 1816 | + { |
|
| 1817 | + if ($field_to_select) { |
|
| 1818 | + $field = $this->field_settings_for($field_to_select); |
|
| 1819 | + } elseif ($this->has_primary_key_field()) { |
|
| 1820 | + $field = $this->get_primary_key_field(); |
|
| 1821 | + } else { |
|
| 1822 | + //no primary key, just grab the first column |
|
| 1823 | + $field = reset($this->field_settings()); |
|
| 1824 | + } |
|
| 1825 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 1826 | + $select_expressions = $field->get_qualified_column(); |
|
| 1827 | + $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 1828 | + return $this->_do_wpdb_query('get_col', array($SQL)); |
|
| 1829 | + } |
|
| 1830 | + |
|
| 1831 | + |
|
| 1832 | + |
|
| 1833 | + /** |
|
| 1834 | + * Returns a single column value for a single row from the database |
|
| 1835 | + * |
|
| 1836 | + * @param array $query_params @see EEM_Base::get_all() |
|
| 1837 | + * @param string $field_to_select @see EEM_Base::get_col() |
|
| 1838 | + * @return string |
|
| 1839 | + * @throws EE_Error |
|
| 1840 | + */ |
|
| 1841 | + public function get_var($query_params = array(), $field_to_select = null) |
|
| 1842 | + { |
|
| 1843 | + $query_params['limit'] = 1; |
|
| 1844 | + $col = $this->get_col($query_params, $field_to_select); |
|
| 1845 | + if (! empty($col)) { |
|
| 1846 | + return reset($col); |
|
| 1847 | + } |
|
| 1848 | + return null; |
|
| 1849 | + } |
|
| 1850 | + |
|
| 1851 | + |
|
| 1852 | + |
|
| 1853 | + /** |
|
| 1854 | + * Makes the SQL for after "UPDATE table_X inner join table_Y..." and before "...WHERE". Eg "Question.name='party |
|
| 1855 | + * time?', Question.desc='what do you think?',..." Values are filtered through wpdb->prepare to avoid against SQL |
|
| 1856 | + * injection, but currently no further filtering is done |
|
| 1857 | + * |
|
| 1858 | + * @global $wpdb |
|
| 1859 | + * @param array $fields_n_values array keys are field names on this model, and values are what those fields should |
|
| 1860 | + * be updated to in the DB |
|
| 1861 | + * @return string of SQL |
|
| 1862 | + * @throws EE_Error |
|
| 1863 | + */ |
|
| 1864 | + public function _construct_update_sql($fields_n_values) |
|
| 1865 | + { |
|
| 1866 | + /** @type WPDB $wpdb */ |
|
| 1867 | + global $wpdb; |
|
| 1868 | + $cols_n_values = array(); |
|
| 1869 | + foreach ($fields_n_values as $field_name => $value) { |
|
| 1870 | + $field_obj = $this->field_settings_for($field_name); |
|
| 1871 | + //if the value is NULL, we want to assign the value to that. |
|
| 1872 | + //wpdb->prepare doesn't really handle that properly |
|
| 1873 | + $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
| 1874 | + $value_sql = $prepared_value === null ? 'NULL' |
|
| 1875 | + : $wpdb->prepare($field_obj->get_wpdb_data_type(), $prepared_value); |
|
| 1876 | + $cols_n_values[] = $field_obj->get_qualified_column() . "=" . $value_sql; |
|
| 1877 | + } |
|
| 1878 | + return implode(",", $cols_n_values); |
|
| 1879 | + } |
|
| 1880 | + |
|
| 1881 | + |
|
| 1882 | + |
|
| 1883 | + /** |
|
| 1884 | + * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
| 1885 | + * Performs a HARD delete, meaning the database row should always be removed, |
|
| 1886 | + * not just have a flag field on it switched |
|
| 1887 | + * Wrapper for EEM_Base::delete_permanently() |
|
| 1888 | + * |
|
| 1889 | + * @param mixed $id |
|
| 1890 | + * @param boolean $allow_blocking |
|
| 1891 | + * @return int the number of rows deleted |
|
| 1892 | + * @throws EE_Error |
|
| 1893 | + */ |
|
| 1894 | + public function delete_permanently_by_ID($id, $allow_blocking = true) |
|
| 1895 | + { |
|
| 1896 | + return $this->delete_permanently( |
|
| 1897 | + array( |
|
| 1898 | + array($this->get_primary_key_field()->get_name() => $id), |
|
| 1899 | + 'limit' => 1, |
|
| 1900 | + ), |
|
| 1901 | + $allow_blocking |
|
| 1902 | + ); |
|
| 1903 | + } |
|
| 1904 | + |
|
| 1905 | + |
|
| 1906 | + |
|
| 1907 | + /** |
|
| 1908 | + * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
|
| 1909 | + * Wrapper for EEM_Base::delete() |
|
| 1910 | + * |
|
| 1911 | + * @param mixed $id |
|
| 1912 | + * @param boolean $allow_blocking |
|
| 1913 | + * @return int the number of rows deleted |
|
| 1914 | + * @throws EE_Error |
|
| 1915 | + */ |
|
| 1916 | + public function delete_by_ID($id, $allow_blocking = true) |
|
| 1917 | + { |
|
| 1918 | + return $this->delete( |
|
| 1919 | + array( |
|
| 1920 | + array($this->get_primary_key_field()->get_name() => $id), |
|
| 1921 | + 'limit' => 1, |
|
| 1922 | + ), |
|
| 1923 | + $allow_blocking |
|
| 1924 | + ); |
|
| 1925 | + } |
|
| 1926 | + |
|
| 1927 | + |
|
| 1928 | + |
|
| 1929 | + /** |
|
| 1930 | + * Identical to delete_permanently, but does a "soft" delete if possible, |
|
| 1931 | + * meaning if the model has a field that indicates its been "trashed" or |
|
| 1932 | + * "soft deleted", we will just set that instead of actually deleting the rows. |
|
| 1933 | + * |
|
| 1934 | + * @see EEM_Base::delete_permanently |
|
| 1935 | + * @param array $query_params |
|
| 1936 | + * @param boolean $allow_blocking |
|
| 1937 | + * @return int how many rows got deleted |
|
| 1938 | + * @throws EE_Error |
|
| 1939 | + */ |
|
| 1940 | + public function delete($query_params, $allow_blocking = true) |
|
| 1941 | + { |
|
| 1942 | + return $this->delete_permanently($query_params, $allow_blocking); |
|
| 1943 | + } |
|
| 1944 | + |
|
| 1945 | + |
|
| 1946 | + |
|
| 1947 | + /** |
|
| 1948 | + * Deletes the model objects that meet the query params. Note: this method is overridden |
|
| 1949 | + * in EEM_Soft_Delete_Base so that soft-deleted model objects are instead only flagged |
|
| 1950 | + * as archived, not actually deleted |
|
| 1951 | + * |
|
| 1952 | + * @param array $query_params very much like EEM_Base::get_all's $query_params |
|
| 1953 | + * @param boolean $allow_blocking if TRUE, matched objects will only be deleted if there is no related model info |
|
| 1954 | + * that blocks it (ie, there' sno other data that depends on this data); if false, |
|
| 1955 | + * deletes regardless of other objects which may depend on it. Its generally |
|
| 1956 | + * advisable to always leave this as TRUE, otherwise you could easily corrupt your |
|
| 1957 | + * DB |
|
| 1958 | + * @return int how many rows got deleted |
|
| 1959 | + * @throws EE_Error |
|
| 1960 | + */ |
|
| 1961 | + public function delete_permanently($query_params, $allow_blocking = true) |
|
| 1962 | + { |
|
| 1963 | + /** |
|
| 1964 | + * Action called just before performing a real deletion query. You can use the |
|
| 1965 | + * model and its $query_params to find exactly which items will be deleted |
|
| 1966 | + * |
|
| 1967 | + * @param EEM_Base $model |
|
| 1968 | + * @param array $query_params @see EEM_Base::get_all() |
|
| 1969 | + * @param boolean $allow_blocking whether or not to allow related model objects |
|
| 1970 | + * to block (prevent) this deletion |
|
| 1971 | + */ |
|
| 1972 | + do_action('AHEE__EEM_Base__delete__begin', $this, $query_params, $allow_blocking); |
|
| 1973 | + //some MySQL databases may be running safe mode, which may restrict |
|
| 1974 | + //deletion if there is no KEY column used in the WHERE statement of a deletion. |
|
| 1975 | + //to get around this, we first do a SELECT, get all the IDs, and then run another query |
|
| 1976 | + //to delete them |
|
| 1977 | + $items_for_deletion = $this->_get_all_wpdb_results($query_params); |
|
| 1978 | + $columns_and_ids_for_deleting = $this->_get_ids_for_delete($items_for_deletion, $allow_blocking); |
|
| 1979 | + $deletion_where_query_part = $this->_build_query_part_for_deleting_from_columns_and_values( |
|
| 1980 | + $columns_and_ids_for_deleting |
|
| 1981 | + ); |
|
| 1982 | + /** |
|
| 1983 | + * Allows client code to act on the items being deleted before the query is actually executed. |
|
| 1984 | + * |
|
| 1985 | + * @param EEM_Base $this The model instance being acted on. |
|
| 1986 | + * @param array $query_params The incoming array of query parameters influencing what gets deleted. |
|
| 1987 | + * @param bool $allow_blocking @see param description in method phpdoc block. |
|
| 1988 | + * @param array $columns_and_ids_for_deleting An array indicating what entities will get removed as |
|
| 1989 | + * derived from the incoming query parameters. |
|
| 1990 | + * @see details on the structure of this array in the phpdocs |
|
| 1991 | + * for the `_get_ids_for_delete_method` |
|
| 1992 | + * |
|
| 1993 | + */ |
|
| 1994 | + do_action('AHEE__EEM_Base__delete__before_query', |
|
| 1995 | + $this, |
|
| 1996 | + $query_params, |
|
| 1997 | + $allow_blocking, |
|
| 1998 | + $columns_and_ids_for_deleting |
|
| 1999 | + ); |
|
| 2000 | + if ($deletion_where_query_part) { |
|
| 2001 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 2002 | + $table_aliases = array_keys($this->_tables); |
|
| 2003 | + $SQL = "DELETE " |
|
| 2004 | + . implode(", ", $table_aliases) |
|
| 2005 | + . " FROM " |
|
| 2006 | + . $model_query_info->get_full_join_sql() |
|
| 2007 | + . " WHERE " |
|
| 2008 | + . $deletion_where_query_part; |
|
| 2009 | + $rows_deleted = $this->_do_wpdb_query('query', array($SQL)); |
|
| 2010 | + } else { |
|
| 2011 | + $rows_deleted = 0; |
|
| 2012 | + } |
|
| 2013 | + |
|
| 2014 | + //Next, make sure those items are removed from the entity map; if they could be put into it at all; and if |
|
| 2015 | + //there was no error with the delete query. |
|
| 2016 | + if ($this->has_primary_key_field() |
|
| 2017 | + && $rows_deleted !== false |
|
| 2018 | + && isset($columns_and_ids_for_deleting[$this->get_primary_key_field()->get_qualified_column()]) |
|
| 2019 | + ) { |
|
| 2020 | + $ids_for_removal = $columns_and_ids_for_deleting[$this->get_primary_key_field()->get_qualified_column()]; |
|
| 2021 | + foreach ($ids_for_removal as $id) { |
|
| 2022 | + if (isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id])) { |
|
| 2023 | + unset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id]); |
|
| 2024 | + } |
|
| 2025 | + } |
|
| 2026 | + |
|
| 2027 | + // delete any extra meta attached to the deleted entities but ONLY if this model is not an instance of |
|
| 2028 | + //`EEM_Extra_Meta`. In other words we want to prevent recursion on EEM_Extra_Meta::delete_permanently calls |
|
| 2029 | + //unnecessarily. It's very unlikely that users will have assigned Extra Meta to Extra Meta |
|
| 2030 | + // (although it is possible). |
|
| 2031 | + //Note this can be skipped by using the provided filter and returning false. |
|
| 2032 | + if (apply_filters( |
|
| 2033 | + 'FHEE__EEM_Base__delete_permanently__dont_delete_extra_meta_for_extra_meta', |
|
| 2034 | + ! $this instanceof EEM_Extra_Meta, |
|
| 2035 | + $this |
|
| 2036 | + )) { |
|
| 2037 | + EEM_Extra_Meta::instance()->delete_permanently(array( |
|
| 2038 | + 0 => array( |
|
| 2039 | + 'EXM_type' => $this->get_this_model_name(), |
|
| 2040 | + 'OBJ_ID' => array( |
|
| 2041 | + 'IN', |
|
| 2042 | + $ids_for_removal |
|
| 2043 | + ) |
|
| 2044 | + ) |
|
| 2045 | + )); |
|
| 2046 | + } |
|
| 2047 | + } |
|
| 2048 | + |
|
| 2049 | + /** |
|
| 2050 | + * Action called just after performing a real deletion query. Although at this point the |
|
| 2051 | + * items should have been deleted |
|
| 2052 | + * |
|
| 2053 | + * @param EEM_Base $model |
|
| 2054 | + * @param array $query_params @see EEM_Base::get_all() |
|
| 2055 | + * @param int $rows_deleted |
|
| 2056 | + */ |
|
| 2057 | + do_action('AHEE__EEM_Base__delete__end', $this, $query_params, $rows_deleted, $columns_and_ids_for_deleting); |
|
| 2058 | + return $rows_deleted;//how many supposedly got deleted |
|
| 2059 | + } |
|
| 2060 | + |
|
| 2061 | + |
|
| 2062 | + |
|
| 2063 | + /** |
|
| 2064 | + * Checks all the relations that throw error messages when there are blocking related objects |
|
| 2065 | + * for related model objects. If there are any related model objects on those relations, |
|
| 2066 | + * adds an EE_Error, and return true |
|
| 2067 | + * |
|
| 2068 | + * @param EE_Base_Class|int $this_model_obj_or_id |
|
| 2069 | + * @param EE_Base_Class $ignore_this_model_obj a model object like 'EE_Event', or 'EE_Term_Taxonomy', which |
|
| 2070 | + * should be ignored when determining whether there are related |
|
| 2071 | + * model objects which block this model object's deletion. Useful |
|
| 2072 | + * if you know A is related to B and are considering deleting A, |
|
| 2073 | + * but want to see if A has any other objects blocking its deletion |
|
| 2074 | + * before removing the relation between A and B |
|
| 2075 | + * @return boolean |
|
| 2076 | + * @throws EE_Error |
|
| 2077 | + */ |
|
| 2078 | + public function delete_is_blocked_by_related_models($this_model_obj_or_id, $ignore_this_model_obj = null) |
|
| 2079 | + { |
|
| 2080 | + //first, if $ignore_this_model_obj was supplied, get its model |
|
| 2081 | + if ($ignore_this_model_obj && $ignore_this_model_obj instanceof EE_Base_Class) { |
|
| 2082 | + $ignored_model = $ignore_this_model_obj->get_model(); |
|
| 2083 | + } else { |
|
| 2084 | + $ignored_model = null; |
|
| 2085 | + } |
|
| 2086 | + //now check all the relations of $this_model_obj_or_id and see if there |
|
| 2087 | + //are any related model objects blocking it? |
|
| 2088 | + $is_blocked = false; |
|
| 2089 | + foreach ($this->_model_relations as $relation_name => $relation_obj) { |
|
| 2090 | + if ($relation_obj->block_delete_if_related_models_exist()) { |
|
| 2091 | + //if $ignore_this_model_obj was supplied, then for the query |
|
| 2092 | + //on that model needs to be told to ignore $ignore_this_model_obj |
|
| 2093 | + if ($ignored_model && $relation_name === $ignored_model->get_this_model_name()) { |
|
| 2094 | + $related_model_objects = $relation_obj->get_all_related($this_model_obj_or_id, array( |
|
| 2095 | + array( |
|
| 2096 | + $ignored_model->get_primary_key_field()->get_name() => array( |
|
| 2097 | + '!=', |
|
| 2098 | + $ignore_this_model_obj->ID(), |
|
| 2099 | + ), |
|
| 2100 | + ), |
|
| 2101 | + )); |
|
| 2102 | + } else { |
|
| 2103 | + $related_model_objects = $relation_obj->get_all_related($this_model_obj_or_id); |
|
| 2104 | + } |
|
| 2105 | + if ($related_model_objects) { |
|
| 2106 | + EE_Error::add_error($relation_obj->get_deletion_error_message(), __FILE__, __FUNCTION__, __LINE__); |
|
| 2107 | + $is_blocked = true; |
|
| 2108 | + } |
|
| 2109 | + } |
|
| 2110 | + } |
|
| 2111 | + return $is_blocked; |
|
| 2112 | + } |
|
| 2113 | + |
|
| 2114 | + |
|
| 2115 | + /** |
|
| 2116 | + * Builds the columns and values for items to delete from the incoming $row_results_for_deleting array. |
|
| 2117 | + * @param array $row_results_for_deleting |
|
| 2118 | + * @param bool $allow_blocking |
|
| 2119 | + * @return array The shape of this array depends on whether the model `has_primary_key_field` or not. If the |
|
| 2120 | + * model DOES have a primary_key_field, then the array will be a simple single dimension array where |
|
| 2121 | + * the key is the fully qualified primary key column and the value is an array of ids that will be |
|
| 2122 | + * deleted. Example: |
|
| 2123 | + * array('Event.EVT_ID' => array( 1,2,3)) |
|
| 2124 | + * If the model DOES NOT have a primary_key_field, then the array will be a two dimensional array |
|
| 2125 | + * where each element is a group of columns and values that get deleted. Example: |
|
| 2126 | + * array( |
|
| 2127 | + * 0 => array( |
|
| 2128 | + * 'Term_Relationship.object_id' => 1 |
|
| 2129 | + * 'Term_Relationship.term_taxonomy_id' => 5 |
|
| 2130 | + * ), |
|
| 2131 | + * 1 => array( |
|
| 2132 | + * 'Term_Relationship.object_id' => 1 |
|
| 2133 | + * 'Term_Relationship.term_taxonomy_id' => 6 |
|
| 2134 | + * ) |
|
| 2135 | + * ) |
|
| 2136 | + * @throws EE_Error |
|
| 2137 | + */ |
|
| 2138 | + protected function _get_ids_for_delete(array $row_results_for_deleting, $allow_blocking = true) |
|
| 2139 | + { |
|
| 2140 | + $ids_to_delete_indexed_by_column = array(); |
|
| 2141 | + if ($this->has_primary_key_field()) { |
|
| 2142 | + $primary_table = $this->_get_main_table(); |
|
| 2143 | + $primary_table_pk_field = $this->get_field_by_column($primary_table->get_fully_qualified_pk_column()); |
|
| 2144 | + $other_tables = $this->_get_other_tables(); |
|
| 2145 | + $ids_to_delete_indexed_by_column = $query = array(); |
|
| 2146 | + foreach ($row_results_for_deleting as $item_to_delete) { |
|
| 2147 | + //before we mark this item for deletion, |
|
| 2148 | + //make sure there's no related entities blocking its deletion (if we're checking) |
|
| 2149 | + if ( |
|
| 2150 | + $allow_blocking |
|
| 2151 | + && $this->delete_is_blocked_by_related_models( |
|
| 2152 | + $item_to_delete[$primary_table->get_fully_qualified_pk_column()] |
|
| 2153 | + ) |
|
| 2154 | + ) { |
|
| 2155 | + continue; |
|
| 2156 | + } |
|
| 2157 | + //primary table deletes |
|
| 2158 | + if (isset($item_to_delete[$primary_table->get_fully_qualified_pk_column()])) { |
|
| 2159 | + $ids_to_delete_indexed_by_column[$primary_table->get_fully_qualified_pk_column()][] = |
|
| 2160 | + $item_to_delete[$primary_table->get_fully_qualified_pk_column()]; |
|
| 2161 | + } |
|
| 2162 | + } |
|
| 2163 | + } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
|
| 2164 | + $fields = $this->get_combined_primary_key_fields(); |
|
| 2165 | + foreach ($row_results_for_deleting as $item_to_delete) { |
|
| 2166 | + $ids_to_delete_indexed_by_column_for_row = array(); |
|
| 2167 | + foreach ($fields as $cpk_field) { |
|
| 2168 | + if ($cpk_field instanceof EE_Model_Field_Base) { |
|
| 2169 | + $ids_to_delete_indexed_by_column_for_row[$cpk_field->get_qualified_column()] = |
|
| 2170 | + $item_to_delete[$cpk_field->get_qualified_column()]; |
|
| 2171 | + } |
|
| 2172 | + } |
|
| 2173 | + $ids_to_delete_indexed_by_column[] = $ids_to_delete_indexed_by_column_for_row; |
|
| 2174 | + } |
|
| 2175 | + } else { |
|
| 2176 | + //so there's no primary key and no combined key... |
|
| 2177 | + //sorry, can't help you |
|
| 2178 | + throw new EE_Error( |
|
| 2179 | + sprintf( |
|
| 2180 | + __( |
|
| 2181 | + "Cannot delete objects of type %s because there is no primary key NOR combined key", |
|
| 2182 | + "event_espresso" |
|
| 2183 | + ), get_class($this) |
|
| 2184 | + ) |
|
| 2185 | + ); |
|
| 2186 | + } |
|
| 2187 | + return $ids_to_delete_indexed_by_column; |
|
| 2188 | + } |
|
| 2189 | + |
|
| 2190 | + |
|
| 2191 | + /** |
|
| 2192 | + * This receives an array of columns and values set to be deleted (as prepared by _get_ids_for_delete) and prepares |
|
| 2193 | + * the corresponding query_part for the query performing the delete. |
|
| 2194 | + * |
|
| 2195 | + * @param array $ids_to_delete_indexed_by_column @see _get_ids_for_delete for how this array might be shaped. |
|
| 2196 | + * @return string |
|
| 2197 | + * @throws EE_Error |
|
| 2198 | + */ |
|
| 2199 | + protected function _build_query_part_for_deleting_from_columns_and_values(array $ids_to_delete_indexed_by_column) { |
|
| 2200 | + $query_part = ''; |
|
| 2201 | + if (empty($ids_to_delete_indexed_by_column)) { |
|
| 2202 | + return $query_part; |
|
| 2203 | + } elseif ($this->has_primary_key_field()) { |
|
| 2204 | + $query = array(); |
|
| 2205 | + foreach ($ids_to_delete_indexed_by_column as $column => $ids) { |
|
| 2206 | + //make sure we have unique $ids |
|
| 2207 | + $ids = array_unique($ids); |
|
| 2208 | + $query[] = $column . ' IN(' . implode(',', $ids) . ')'; |
|
| 2209 | + } |
|
| 2210 | + $query_part = ! empty($query) ? implode(' AND ', $query) : $query_part; |
|
| 2211 | + } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
|
| 2212 | + $ways_to_identify_a_row = array(); |
|
| 2213 | + foreach ($ids_to_delete_indexed_by_column as $ids_to_delete_indexed_by_column_for_each_row) { |
|
| 2214 | + $values_for_each_combined_primary_key_for_a_row = array(); |
|
| 2215 | + foreach ($ids_to_delete_indexed_by_column_for_each_row as $column => $id) { |
|
| 2216 | + $values_for_each_combined_primary_key_for_a_row[] = $column . '=' . $id; |
|
| 2217 | + } |
|
| 2218 | + $ways_to_identify_a_row[] = '(' |
|
| 2219 | + . implode(' AND ', $values_for_each_combined_primary_key_for_a_row) |
|
| 2220 | + . ')'; |
|
| 2221 | + } |
|
| 2222 | + $query_part = implode(' OR ', $ways_to_identify_a_row); |
|
| 2223 | + } |
|
| 2224 | + return $query_part; |
|
| 2225 | + } |
|
| 2226 | + |
|
| 2227 | + |
|
| 2228 | + |
|
| 2229 | + /** |
|
| 2230 | + * Gets the model field by the fully qualified name |
|
| 2231 | + * @param string $qualified_column_name eg 'Event_CPT.post_name' or $field_obj->get_qualified_column() |
|
| 2232 | + * @return EE_Model_Field_Base |
|
| 2233 | + */ |
|
| 2234 | + public function get_field_by_column($qualified_column_name) |
|
| 2235 | + { |
|
| 2236 | + foreach($this->field_settings(true) as $field_name => $field_obj){ |
|
| 2237 | + if($field_obj->get_qualified_column() === $qualified_column_name){ |
|
| 2238 | + return $field_obj; |
|
| 2239 | + } |
|
| 2240 | + } |
|
| 2241 | + throw new EE_Error( |
|
| 2242 | + sprintf( |
|
| 2243 | + esc_html__('Could not find a field on the model "%1$s" for qualified column "%2$s"', 'event_espresso'), |
|
| 2244 | + $this->get_this_model_name(), |
|
| 2245 | + $qualified_column_name |
|
| 2246 | + ) |
|
| 2247 | + ); |
|
| 2248 | + } |
|
| 2249 | + |
|
| 2250 | + |
|
| 2251 | + |
|
| 2252 | + /** |
|
| 2253 | + * Count all the rows that match criteria expressed in $query_params (an array just like arg to EEM_Base::get_all). |
|
| 2254 | + * If $field_to_count isn't provided, the model's primary key is used. Otherwise, we count by field_to_count's |
|
| 2255 | + * column |
|
| 2256 | + * |
|
| 2257 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2258 | + * @param string $field_to_count field on model to count by (not column name) |
|
| 2259 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2260 | + * that by the setting $distinct to TRUE; |
|
| 2261 | + * @return int |
|
| 2262 | + * @throws EE_Error |
|
| 2263 | + */ |
|
| 2264 | + public function count($query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2265 | + { |
|
| 2266 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 2267 | + if ($field_to_count) { |
|
| 2268 | + $field_obj = $this->field_settings_for($field_to_count); |
|
| 2269 | + $column_to_count = $field_obj->get_qualified_column(); |
|
| 2270 | + } elseif ($this->has_primary_key_field()) { |
|
| 2271 | + $pk_field_obj = $this->get_primary_key_field(); |
|
| 2272 | + $column_to_count = $pk_field_obj->get_qualified_column(); |
|
| 2273 | + } else { |
|
| 2274 | + //there's no primary key |
|
| 2275 | + //if we're counting distinct items, and there's no primary key, |
|
| 2276 | + //we need to list out the columns for distinction; |
|
| 2277 | + //otherwise we can just use star |
|
| 2278 | + if ($distinct) { |
|
| 2279 | + $columns_to_use = array(); |
|
| 2280 | + foreach ($this->get_combined_primary_key_fields() as $field_obj) { |
|
| 2281 | + $columns_to_use[] = $field_obj->get_qualified_column(); |
|
| 2282 | + } |
|
| 2283 | + $column_to_count = implode(',', $columns_to_use); |
|
| 2284 | + } else { |
|
| 2285 | + $column_to_count = '*'; |
|
| 2286 | + } |
|
| 2287 | + } |
|
| 2288 | + $column_to_count = $distinct ? "DISTINCT " . $column_to_count : $column_to_count; |
|
| 2289 | + $SQL = "SELECT COUNT(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2290 | + return (int)$this->_do_wpdb_query('get_var', array($SQL)); |
|
| 2291 | + } |
|
| 2292 | + |
|
| 2293 | + |
|
| 2294 | + |
|
| 2295 | + /** |
|
| 2296 | + * Sums up the value of the $field_to_sum (defaults to the primary key, which isn't terribly useful) |
|
| 2297 | + * |
|
| 2298 | + * @param array $query_params like EEM_Base::get_all |
|
| 2299 | + * @param string $field_to_sum name of field (array key in $_fields array) |
|
| 2300 | + * @return float |
|
| 2301 | + * @throws EE_Error |
|
| 2302 | + */ |
|
| 2303 | + public function sum($query_params, $field_to_sum = null) |
|
| 2304 | + { |
|
| 2305 | + $model_query_info = $this->_create_model_query_info_carrier($query_params); |
|
| 2306 | + if ($field_to_sum) { |
|
| 2307 | + $field_obj = $this->field_settings_for($field_to_sum); |
|
| 2308 | + } else { |
|
| 2309 | + $field_obj = $this->get_primary_key_field(); |
|
| 2310 | + } |
|
| 2311 | + $column_to_count = $field_obj->get_qualified_column(); |
|
| 2312 | + $SQL = "SELECT SUM(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2313 | + $return_value = $this->_do_wpdb_query('get_var', array($SQL)); |
|
| 2314 | + $data_type = $field_obj->get_wpdb_data_type(); |
|
| 2315 | + if ($data_type === '%d' || $data_type === '%s') { |
|
| 2316 | + return (float)$return_value; |
|
| 2317 | + } |
|
| 2318 | + //must be %f |
|
| 2319 | + return (float)$return_value; |
|
| 2320 | + } |
|
| 2321 | + |
|
| 2322 | + |
|
| 2323 | + |
|
| 2324 | + /** |
|
| 2325 | + * Just calls the specified method on $wpdb with the given arguments |
|
| 2326 | + * Consolidates a little extra error handling code |
|
| 2327 | + * |
|
| 2328 | + * @param string $wpdb_method |
|
| 2329 | + * @param array $arguments_to_provide |
|
| 2330 | + * @throws EE_Error |
|
| 2331 | + * @global wpdb $wpdb |
|
| 2332 | + * @return mixed |
|
| 2333 | + */ |
|
| 2334 | + protected function _do_wpdb_query($wpdb_method, $arguments_to_provide) |
|
| 2335 | + { |
|
| 2336 | + //if we're in maintenance mode level 2, DON'T run any queries |
|
| 2337 | + //because level 2 indicates the database needs updating and |
|
| 2338 | + //is probably out of sync with the code |
|
| 2339 | + if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 2340 | + throw new EE_Error(sprintf(__("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.", |
|
| 2341 | + "event_espresso"))); |
|
| 2342 | + } |
|
| 2343 | + /** @type WPDB $wpdb */ |
|
| 2344 | + global $wpdb; |
|
| 2345 | + if (! method_exists($wpdb, $wpdb_method)) { |
|
| 2346 | + throw new EE_Error(sprintf(__('There is no method named "%s" on Wordpress\' $wpdb object', |
|
| 2347 | + 'event_espresso'), $wpdb_method)); |
|
| 2348 | + } |
|
| 2349 | + if (WP_DEBUG) { |
|
| 2350 | + $old_show_errors_value = $wpdb->show_errors; |
|
| 2351 | + $wpdb->show_errors(false); |
|
| 2352 | + } |
|
| 2353 | + $result = $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
| 2354 | + $this->show_db_query_if_previously_requested($wpdb->last_query); |
|
| 2355 | + if (WP_DEBUG) { |
|
| 2356 | + $wpdb->show_errors($old_show_errors_value); |
|
| 2357 | + if (! empty($wpdb->last_error)) { |
|
| 2358 | + throw new EE_Error(sprintf(__('WPDB Error: "%s"', 'event_espresso'), $wpdb->last_error)); |
|
| 2359 | + } |
|
| 2360 | + if ($result === false) { |
|
| 2361 | + throw new EE_Error(sprintf(__('WPDB Error occurred, but no error message was logged by wpdb! The wpdb method called was "%1$s" and the arguments were "%2$s"', |
|
| 2362 | + 'event_espresso'), $wpdb_method, var_export($arguments_to_provide, true))); |
|
| 2363 | + } |
|
| 2364 | + } elseif ($result === false) { |
|
| 2365 | + EE_Error::add_error( |
|
| 2366 | + sprintf( |
|
| 2367 | + __('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"', |
|
| 2368 | + 'event_espresso'), |
|
| 2369 | + $wpdb_method, |
|
| 2370 | + var_export($arguments_to_provide, true), |
|
| 2371 | + $wpdb->last_error |
|
| 2372 | + ), |
|
| 2373 | + __FILE__, |
|
| 2374 | + __FUNCTION__, |
|
| 2375 | + __LINE__ |
|
| 2376 | + ); |
|
| 2377 | + } |
|
| 2378 | + return $result; |
|
| 2379 | + } |
|
| 2380 | + |
|
| 2381 | + |
|
| 2382 | + |
|
| 2383 | + /** |
|
| 2384 | + * Attempts to run the indicated WPDB method with the provided arguments, |
|
| 2385 | + * and if there's an error tries to verify the DB is correct. Uses |
|
| 2386 | + * the static property EEM_Base::$_db_verification_level to determine whether |
|
| 2387 | + * we should try to fix the EE core db, the addons, or just give up |
|
| 2388 | + * |
|
| 2389 | + * @param string $wpdb_method |
|
| 2390 | + * @param array $arguments_to_provide |
|
| 2391 | + * @return mixed |
|
| 2392 | + */ |
|
| 2393 | + private function _process_wpdb_query($wpdb_method, $arguments_to_provide) |
|
| 2394 | + { |
|
| 2395 | + /** @type WPDB $wpdb */ |
|
| 2396 | + global $wpdb; |
|
| 2397 | + $wpdb->last_error = null; |
|
| 2398 | + $result = call_user_func_array(array($wpdb, $wpdb_method), $arguments_to_provide); |
|
| 2399 | + // was there an error running the query? but we don't care on new activations |
|
| 2400 | + // (we're going to setup the DB anyway on new activations) |
|
| 2401 | + if (($result === false || ! empty($wpdb->last_error)) |
|
| 2402 | + && EE_System::instance()->detect_req_type() !== EE_System::req_type_new_activation |
|
| 2403 | + ) { |
|
| 2404 | + switch (EEM_Base::$_db_verification_level) { |
|
| 2405 | + case EEM_Base::db_verified_none : |
|
| 2406 | + // let's double-check core's DB |
|
| 2407 | + $error_message = $this->_verify_core_db($wpdb_method, $arguments_to_provide); |
|
| 2408 | + break; |
|
| 2409 | + case EEM_Base::db_verified_core : |
|
| 2410 | + // STILL NO LOVE?? verify all the addons too. Maybe they need to be fixed |
|
| 2411 | + $error_message = $this->_verify_addons_db($wpdb_method, $arguments_to_provide); |
|
| 2412 | + break; |
|
| 2413 | + case EEM_Base::db_verified_addons : |
|
| 2414 | + // ummmm... you in trouble |
|
| 2415 | + return $result; |
|
| 2416 | + break; |
|
| 2417 | + } |
|
| 2418 | + if (! empty($error_message)) { |
|
| 2419 | + EE_Log::instance()->log(__FILE__, __FUNCTION__, $error_message, 'error'); |
|
| 2420 | + trigger_error($error_message); |
|
| 2421 | + } |
|
| 2422 | + return $this->_process_wpdb_query($wpdb_method, $arguments_to_provide); |
|
| 2423 | + } |
|
| 2424 | + return $result; |
|
| 2425 | + } |
|
| 2426 | + |
|
| 2427 | + |
|
| 2428 | + |
|
| 2429 | + /** |
|
| 2430 | + * Verifies the EE core database is up-to-date and records that we've done it on |
|
| 2431 | + * EEM_Base::$_db_verification_level |
|
| 2432 | + * |
|
| 2433 | + * @param string $wpdb_method |
|
| 2434 | + * @param array $arguments_to_provide |
|
| 2435 | + * @return string |
|
| 2436 | + */ |
|
| 2437 | + private function _verify_core_db($wpdb_method, $arguments_to_provide) |
|
| 2438 | + { |
|
| 2439 | + /** @type WPDB $wpdb */ |
|
| 2440 | + global $wpdb; |
|
| 2441 | + //ok remember that we've already attempted fixing the core db, in case the problem persists |
|
| 2442 | + EEM_Base::$_db_verification_level = EEM_Base::db_verified_core; |
|
| 2443 | + $error_message = sprintf( |
|
| 2444 | + __('WPDB Error "%1$s" while running wpdb method "%2$s" with arguments %3$s. Automatically attempting to fix EE Core DB', |
|
| 2445 | + 'event_espresso'), |
|
| 2446 | + $wpdb->last_error, |
|
| 2447 | + $wpdb_method, |
|
| 2448 | + wp_json_encode($arguments_to_provide) |
|
| 2449 | + ); |
|
| 2450 | + EE_System::instance()->initialize_db_if_no_migrations_required(false, true); |
|
| 2451 | + return $error_message; |
|
| 2452 | + } |
|
| 2453 | + |
|
| 2454 | + |
|
| 2455 | + |
|
| 2456 | + /** |
|
| 2457 | + * Verifies the EE addons' database is up-to-date and records that we've done it on |
|
| 2458 | + * EEM_Base::$_db_verification_level |
|
| 2459 | + * |
|
| 2460 | + * @param $wpdb_method |
|
| 2461 | + * @param $arguments_to_provide |
|
| 2462 | + * @return string |
|
| 2463 | + */ |
|
| 2464 | + private function _verify_addons_db($wpdb_method, $arguments_to_provide) |
|
| 2465 | + { |
|
| 2466 | + /** @type WPDB $wpdb */ |
|
| 2467 | + global $wpdb; |
|
| 2468 | + //ok remember that we've already attempted fixing the addons dbs, in case the problem persists |
|
| 2469 | + EEM_Base::$_db_verification_level = EEM_Base::db_verified_addons; |
|
| 2470 | + $error_message = sprintf( |
|
| 2471 | + __('WPDB AGAIN: Error "%1$s" while running the same method and arguments as before. Automatically attempting to fix EE Addons DB', |
|
| 2472 | + 'event_espresso'), |
|
| 2473 | + $wpdb->last_error, |
|
| 2474 | + $wpdb_method, |
|
| 2475 | + wp_json_encode($arguments_to_provide) |
|
| 2476 | + ); |
|
| 2477 | + EE_System::instance()->initialize_addons(); |
|
| 2478 | + return $error_message; |
|
| 2479 | + } |
|
| 2480 | + |
|
| 2481 | + |
|
| 2482 | + |
|
| 2483 | + /** |
|
| 2484 | + * In order to avoid repeating this code for the get_all, sum, and count functions, put the code parts |
|
| 2485 | + * that are identical in here. Returns a string of SQL of everything in a SELECT query except the beginning |
|
| 2486 | + * SELECT clause, eg " FROM wp_posts AS Event INNER JOIN ... WHERE ... ORDER BY ... LIMIT ... GROUP BY ... HAVING |
|
| 2487 | + * ..." |
|
| 2488 | + * |
|
| 2489 | + * @param EE_Model_Query_Info_Carrier $model_query_info |
|
| 2490 | + * @return string |
|
| 2491 | + */ |
|
| 2492 | + private function _construct_2nd_half_of_select_query(EE_Model_Query_Info_Carrier $model_query_info) |
|
| 2493 | + { |
|
| 2494 | + return " FROM " . $model_query_info->get_full_join_sql() . |
|
| 2495 | + $model_query_info->get_where_sql() . |
|
| 2496 | + $model_query_info->get_group_by_sql() . |
|
| 2497 | + $model_query_info->get_having_sql() . |
|
| 2498 | + $model_query_info->get_order_by_sql() . |
|
| 2499 | + $model_query_info->get_limit_sql(); |
|
| 2500 | + } |
|
| 2501 | + |
|
| 2502 | + |
|
| 2503 | + |
|
| 2504 | + /** |
|
| 2505 | + * Set to easily debug the next X queries ran from this model. |
|
| 2506 | + * |
|
| 2507 | + * @param int $count |
|
| 2508 | + */ |
|
| 2509 | + public function show_next_x_db_queries($count = 1) |
|
| 2510 | + { |
|
| 2511 | + $this->_show_next_x_db_queries = $count; |
|
| 2512 | + } |
|
| 2513 | + |
|
| 2514 | + |
|
| 2515 | + |
|
| 2516 | + /** |
|
| 2517 | + * @param $sql_query |
|
| 2518 | + */ |
|
| 2519 | + public function show_db_query_if_previously_requested($sql_query) |
|
| 2520 | + { |
|
| 2521 | + if ($this->_show_next_x_db_queries > 0) { |
|
| 2522 | + echo $sql_query; |
|
| 2523 | + $this->_show_next_x_db_queries--; |
|
| 2524 | + } |
|
| 2525 | + } |
|
| 2526 | + |
|
| 2527 | + |
|
| 2528 | + |
|
| 2529 | + /** |
|
| 2530 | + * Adds a relationship of the correct type between $modelObject and $otherModelObject. |
|
| 2531 | + * There are the 3 cases: |
|
| 2532 | + * 'belongsTo' relationship: sets $id_or_obj's foreign_key to be $other_model_id_or_obj's primary_key. If |
|
| 2533 | + * $otherModelObject has no ID, it is first saved. |
|
| 2534 | + * 'hasMany' relationship: sets $other_model_id_or_obj's foreign_key to be $id_or_obj's primary_key. If $id_or_obj |
|
| 2535 | + * has no ID, it is first saved. |
|
| 2536 | + * 'hasAndBelongsToMany' relationships: checks that there isn't already an entry in the join table, and adds one. |
|
| 2537 | + * If one of the model Objects has not yet been saved to the database, it is saved before adding the entry in the |
|
| 2538 | + * join table |
|
| 2539 | + * |
|
| 2540 | + * @param EE_Base_Class /int $thisModelObject |
|
| 2541 | + * @param EE_Base_Class /int $id_or_obj EE_base_Class or ID of other Model Object |
|
| 2542 | + * @param string $relationName , key in EEM_Base::_relations |
|
| 2543 | + * an attendee to a group, you also want to specify which role they |
|
| 2544 | + * will have in that group. So you would use this parameter to |
|
| 2545 | + * specify array('role-column-name'=>'role-id') |
|
| 2546 | + * @param array $extra_join_model_fields_n_values This allows you to enter further query params for the relation |
|
| 2547 | + * to for relation to methods that allow you to further specify |
|
| 2548 | + * extra columns to join by (such as HABTM). Keep in mind that the |
|
| 2549 | + * only acceptable query_params is strict "col" => "value" pairs |
|
| 2550 | + * because these will be inserted in any new rows created as well. |
|
| 2551 | + * @return EE_Base_Class which was added as a relation. Object referred to by $other_model_id_or_obj |
|
| 2552 | + * @throws EE_Error |
|
| 2553 | + */ |
|
| 2554 | + public function add_relationship_to( |
|
| 2555 | + $id_or_obj, |
|
| 2556 | + $other_model_id_or_obj, |
|
| 2557 | + $relationName, |
|
| 2558 | + $extra_join_model_fields_n_values = array() |
|
| 2559 | + ) { |
|
| 2560 | + $relation_obj = $this->related_settings_for($relationName); |
|
| 2561 | + return $relation_obj->add_relation_to($id_or_obj, $other_model_id_or_obj, $extra_join_model_fields_n_values); |
|
| 2562 | + } |
|
| 2563 | + |
|
| 2564 | + |
|
| 2565 | + |
|
| 2566 | + /** |
|
| 2567 | + * Removes a relationship of the correct type between $modelObject and $otherModelObject. |
|
| 2568 | + * There are the 3 cases: |
|
| 2569 | + * 'belongsTo' relationship: sets $modelObject's foreign_key to null, if that field is nullable.Otherwise throws an |
|
| 2570 | + * error |
|
| 2571 | + * 'hasMany' relationship: sets $otherModelObject's foreign_key to null,if that field is nullable.Otherwise throws |
|
| 2572 | + * an error |
|
| 2573 | + * 'hasAndBelongsToMany' relationships:removes any existing entry in the join table between the two models. |
|
| 2574 | + * |
|
| 2575 | + * @param EE_Base_Class /int $id_or_obj |
|
| 2576 | + * @param EE_Base_Class /int $other_model_id_or_obj EE_Base_Class or ID of other Model Object |
|
| 2577 | + * @param string $relationName key in EEM_Base::_relations |
|
| 2578 | + * @return boolean of success |
|
| 2579 | + * @throws EE_Error |
|
| 2580 | + * @param array $where_query This allows you to enter further query params for the relation to for relation to |
|
| 2581 | + * methods that allow you to further specify extra columns to join by (such as HABTM). |
|
| 2582 | + * Keep in mind that the only acceptable query_params is strict "col" => "value" pairs |
|
| 2583 | + * because these will be inserted in any new rows created as well. |
|
| 2584 | + */ |
|
| 2585 | + public function remove_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query = array()) |
|
| 2586 | + { |
|
| 2587 | + $relation_obj = $this->related_settings_for($relationName); |
|
| 2588 | + return $relation_obj->remove_relation_to($id_or_obj, $other_model_id_or_obj, $where_query); |
|
| 2589 | + } |
|
| 2590 | + |
|
| 2591 | + |
|
| 2592 | + |
|
| 2593 | + /** |
|
| 2594 | + * @param mixed $id_or_obj |
|
| 2595 | + * @param string $relationName |
|
| 2596 | + * @param array $where_query_params |
|
| 2597 | + * @param EE_Base_Class[] objects to which relations were removed |
|
| 2598 | + * @return \EE_Base_Class[] |
|
| 2599 | + * @throws EE_Error |
|
| 2600 | + */ |
|
| 2601 | + public function remove_relations($id_or_obj, $relationName, $where_query_params = array()) |
|
| 2602 | + { |
|
| 2603 | + $relation_obj = $this->related_settings_for($relationName); |
|
| 2604 | + return $relation_obj->remove_relations($id_or_obj, $where_query_params); |
|
| 2605 | + } |
|
| 2606 | + |
|
| 2607 | + |
|
| 2608 | + |
|
| 2609 | + /** |
|
| 2610 | + * Gets all the related items of the specified $model_name, using $query_params. |
|
| 2611 | + * Note: by default, we remove the "default query params" |
|
| 2612 | + * because we want to get even deleted items etc. |
|
| 2613 | + * |
|
| 2614 | + * @param mixed $id_or_obj EE_Base_Class child or its ID |
|
| 2615 | + * @param string $model_name like 'Event', 'Registration', etc. always singular |
|
| 2616 | + * @param array $query_params like EEM_Base::get_all |
|
| 2617 | + * @return EE_Base_Class[] |
|
| 2618 | + * @throws EE_Error |
|
| 2619 | + */ |
|
| 2620 | + public function get_all_related($id_or_obj, $model_name, $query_params = null) |
|
| 2621 | + { |
|
| 2622 | + $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2623 | + $relation_settings = $this->related_settings_for($model_name); |
|
| 2624 | + return $relation_settings->get_all_related($model_obj, $query_params); |
|
| 2625 | + } |
|
| 2626 | + |
|
| 2627 | + |
|
| 2628 | + |
|
| 2629 | + /** |
|
| 2630 | + * Deletes all the model objects across the relation indicated by $model_name |
|
| 2631 | + * which are related to $id_or_obj which meet the criteria set in $query_params. |
|
| 2632 | + * However, if the model objects can't be deleted because of blocking related model objects, then |
|
| 2633 | + * they aren't deleted. (Unless the thing that would have been deleted can be soft-deleted, that still happens). |
|
| 2634 | + * |
|
| 2635 | + * @param EE_Base_Class|int|string $id_or_obj |
|
| 2636 | + * @param string $model_name |
|
| 2637 | + * @param array $query_params |
|
| 2638 | + * @return int how many deleted |
|
| 2639 | + * @throws EE_Error |
|
| 2640 | + */ |
|
| 2641 | + public function delete_related($id_or_obj, $model_name, $query_params = array()) |
|
| 2642 | + { |
|
| 2643 | + $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2644 | + $relation_settings = $this->related_settings_for($model_name); |
|
| 2645 | + return $relation_settings->delete_all_related($model_obj, $query_params); |
|
| 2646 | + } |
|
| 2647 | + |
|
| 2648 | + |
|
| 2649 | + |
|
| 2650 | + /** |
|
| 2651 | + * Hard deletes all the model objects across the relation indicated by $model_name |
|
| 2652 | + * which are related to $id_or_obj which meet the criteria set in $query_params. If |
|
| 2653 | + * the model objects can't be hard deleted because of blocking related model objects, |
|
| 2654 | + * just does a soft-delete on them instead. |
|
| 2655 | + * |
|
| 2656 | + * @param EE_Base_Class|int|string $id_or_obj |
|
| 2657 | + * @param string $model_name |
|
| 2658 | + * @param array $query_params |
|
| 2659 | + * @return int how many deleted |
|
| 2660 | + * @throws EE_Error |
|
| 2661 | + */ |
|
| 2662 | + public function delete_related_permanently($id_or_obj, $model_name, $query_params = array()) |
|
| 2663 | + { |
|
| 2664 | + $model_obj = $this->ensure_is_obj($id_or_obj); |
|
| 2665 | + $relation_settings = $this->related_settings_for($model_name); |
|
| 2666 | + return $relation_settings->delete_related_permanently($model_obj, $query_params); |
|
| 2667 | + } |
|
| 2668 | + |
|
| 2669 | + |
|
| 2670 | + |
|
| 2671 | + /** |
|
| 2672 | + * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2673 | + * unless otherwise specified in the $query_params |
|
| 2674 | + * |
|
| 2675 | + * @param int /EE_Base_Class $id_or_obj |
|
| 2676 | + * @param string $model_name like 'Event', or 'Registration' |
|
| 2677 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2678 | + * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2679 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2680 | + * that by the setting $distinct to TRUE; |
|
| 2681 | + * @return int |
|
| 2682 | + * @throws EE_Error |
|
| 2683 | + */ |
|
| 2684 | + public function count_related( |
|
| 2685 | + $id_or_obj, |
|
| 2686 | + $model_name, |
|
| 2687 | + $query_params = array(), |
|
| 2688 | + $field_to_count = null, |
|
| 2689 | + $distinct = false |
|
| 2690 | + ) { |
|
| 2691 | + $related_model = $this->get_related_model_obj($model_name); |
|
| 2692 | + //we're just going to use the query params on the related model's normal get_all query, |
|
| 2693 | + //except add a condition to say to match the current mod |
|
| 2694 | + if (! isset($query_params['default_where_conditions'])) { |
|
| 2695 | + $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
| 2696 | + } |
|
| 2697 | + $this_model_name = $this->get_this_model_name(); |
|
| 2698 | + $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
| 2699 | + $query_params[0][$this_model_name . "." . $this_pk_field_name] = $id_or_obj; |
|
| 2700 | + return $related_model->count($query_params, $field_to_count, $distinct); |
|
| 2701 | + } |
|
| 2702 | + |
|
| 2703 | + |
|
| 2704 | + |
|
| 2705 | + /** |
|
| 2706 | + * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2707 | + * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2708 | + * |
|
| 2709 | + * @param int /EE_Base_Class $id_or_obj |
|
| 2710 | + * @param string $model_name like 'Event', or 'Registration' |
|
| 2711 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2712 | + * @param string $field_to_sum name of field to count by. By default, uses primary key |
|
| 2713 | + * @return float |
|
| 2714 | + * @throws EE_Error |
|
| 2715 | + */ |
|
| 2716 | + public function sum_related($id_or_obj, $model_name, $query_params, $field_to_sum = null) |
|
| 2717 | + { |
|
| 2718 | + $related_model = $this->get_related_model_obj($model_name); |
|
| 2719 | + if (! is_array($query_params)) { |
|
| 2720 | + EE_Error::doing_it_wrong('EEM_Base::sum_related', |
|
| 2721 | + sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 2722 | + gettype($query_params)), '4.6.0'); |
|
| 2723 | + $query_params = array(); |
|
| 2724 | + } |
|
| 2725 | + //we're just going to use the query params on the related model's normal get_all query, |
|
| 2726 | + //except add a condition to say to match the current mod |
|
| 2727 | + if (! isset($query_params['default_where_conditions'])) { |
|
| 2728 | + $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
|
| 2729 | + } |
|
| 2730 | + $this_model_name = $this->get_this_model_name(); |
|
| 2731 | + $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
|
| 2732 | + $query_params[0][$this_model_name . "." . $this_pk_field_name] = $id_or_obj; |
|
| 2733 | + return $related_model->sum($query_params, $field_to_sum); |
|
| 2734 | + } |
|
| 2735 | + |
|
| 2736 | + |
|
| 2737 | + |
|
| 2738 | + /** |
|
| 2739 | + * Uses $this->_relatedModels info to find the first related model object of relation $relationName to the given |
|
| 2740 | + * $modelObject |
|
| 2741 | + * |
|
| 2742 | + * @param int | EE_Base_Class $id_or_obj EE_Base_Class child or its ID |
|
| 2743 | + * @param string $other_model_name , key in $this->_relatedModels, eg 'Registration', or 'Events' |
|
| 2744 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2745 | + * @return EE_Base_Class |
|
| 2746 | + * @throws EE_Error |
|
| 2747 | + */ |
|
| 2748 | + public function get_first_related(EE_Base_Class $id_or_obj, $other_model_name, $query_params) |
|
| 2749 | + { |
|
| 2750 | + $query_params['limit'] = 1; |
|
| 2751 | + $results = $this->get_all_related($id_or_obj, $other_model_name, $query_params); |
|
| 2752 | + if ($results) { |
|
| 2753 | + return array_shift($results); |
|
| 2754 | + } |
|
| 2755 | + return null; |
|
| 2756 | + } |
|
| 2757 | + |
|
| 2758 | + |
|
| 2759 | + |
|
| 2760 | + /** |
|
| 2761 | + * Gets the model's name as it's expected in queries. For example, if this is EEM_Event model, that would be Event |
|
| 2762 | + * |
|
| 2763 | + * @return string |
|
| 2764 | + */ |
|
| 2765 | + public function get_this_model_name() |
|
| 2766 | + { |
|
| 2767 | + return str_replace("EEM_", "", get_class($this)); |
|
| 2768 | + } |
|
| 2769 | + |
|
| 2770 | + |
|
| 2771 | + |
|
| 2772 | + /** |
|
| 2773 | + * Gets the model field on this model which is of type EE_Any_Foreign_Model_Name_Field |
|
| 2774 | + * |
|
| 2775 | + * @return EE_Any_Foreign_Model_Name_Field |
|
| 2776 | + * @throws EE_Error |
|
| 2777 | + */ |
|
| 2778 | + public function get_field_containing_related_model_name() |
|
| 2779 | + { |
|
| 2780 | + foreach ($this->field_settings(true) as $field) { |
|
| 2781 | + if ($field instanceof EE_Any_Foreign_Model_Name_Field) { |
|
| 2782 | + $field_with_model_name = $field; |
|
| 2783 | + } |
|
| 2784 | + } |
|
| 2785 | + if (! isset($field_with_model_name) || ! $field_with_model_name) { |
|
| 2786 | + throw new EE_Error(sprintf(__("There is no EE_Any_Foreign_Model_Name field on model %s", "event_espresso"), |
|
| 2787 | + $this->get_this_model_name())); |
|
| 2788 | + } |
|
| 2789 | + return $field_with_model_name; |
|
| 2790 | + } |
|
| 2791 | + |
|
| 2792 | + |
|
| 2793 | + |
|
| 2794 | + /** |
|
| 2795 | + * Inserts a new entry into the database, for each table. |
|
| 2796 | + * Note: does not add the item to the entity map because that is done by EE_Base_Class::save() right after this. |
|
| 2797 | + * If client code uses EEM_Base::insert() directly, then although the item isn't in the entity map, |
|
| 2798 | + * we also know there is no model object with the newly inserted item's ID at the moment (because |
|
| 2799 | + * if there were, then they would already be in the DB and this would fail); and in the future if someone |
|
| 2800 | + * creates a model object with this ID (or grabs it from the DB) then it will be added to the |
|
| 2801 | + * entity map at that time anyways. SO, no need for EEM_Base::insert ot add to the entity map |
|
| 2802 | + * |
|
| 2803 | + * @param array $field_n_values keys are field names, values are their values (in the client code's domain if |
|
| 2804 | + * $values_already_prepared_by_model_object is false, in the model object's domain if |
|
| 2805 | + * $values_already_prepared_by_model_object is true. See comment about this at the top |
|
| 2806 | + * of EEM_Base) |
|
| 2807 | + * @return int new primary key on main table that got inserted |
|
| 2808 | + * @throws EE_Error |
|
| 2809 | + */ |
|
| 2810 | + public function insert($field_n_values) |
|
| 2811 | + { |
|
| 2812 | + /** |
|
| 2813 | + * Filters the fields and their values before inserting an item using the models |
|
| 2814 | + * |
|
| 2815 | + * @param array $fields_n_values keys are the fields and values are their new values |
|
| 2816 | + * @param EEM_Base $model the model used |
|
| 2817 | + */ |
|
| 2818 | + $field_n_values = (array)apply_filters('FHEE__EEM_Base__insert__fields_n_values', $field_n_values, $this); |
|
| 2819 | + if ($this->_satisfies_unique_indexes($field_n_values)) { |
|
| 2820 | + $main_table = $this->_get_main_table(); |
|
| 2821 | + $new_id = $this->_insert_into_specific_table($main_table, $field_n_values, false); |
|
| 2822 | + if ($new_id !== false) { |
|
| 2823 | + foreach ($this->_get_other_tables() as $other_table) { |
|
| 2824 | + $this->_insert_into_specific_table($other_table, $field_n_values, $new_id); |
|
| 2825 | + } |
|
| 2826 | + } |
|
| 2827 | + /** |
|
| 2828 | + * Done just after attempting to insert a new model object |
|
| 2829 | + * |
|
| 2830 | + * @param EEM_Base $model used |
|
| 2831 | + * @param array $fields_n_values fields and their values |
|
| 2832 | + * @param int|string the ID of the newly-inserted model object |
|
| 2833 | + */ |
|
| 2834 | + do_action('AHEE__EEM_Base__insert__end', $this, $field_n_values, $new_id); |
|
| 2835 | + return $new_id; |
|
| 2836 | + } |
|
| 2837 | + return false; |
|
| 2838 | + } |
|
| 2839 | + |
|
| 2840 | + |
|
| 2841 | + |
|
| 2842 | + /** |
|
| 2843 | + * Checks that the result would satisfy the unique indexes on this model |
|
| 2844 | + * |
|
| 2845 | + * @param array $field_n_values |
|
| 2846 | + * @param string $action |
|
| 2847 | + * @return boolean |
|
| 2848 | + * @throws EE_Error |
|
| 2849 | + */ |
|
| 2850 | + protected function _satisfies_unique_indexes($field_n_values, $action = 'insert') |
|
| 2851 | + { |
|
| 2852 | + foreach ($this->unique_indexes() as $index_name => $index) { |
|
| 2853 | + $uniqueness_where_params = array_intersect_key($field_n_values, $index->fields()); |
|
| 2854 | + if ($this->exists(array($uniqueness_where_params))) { |
|
| 2855 | + EE_Error::add_error( |
|
| 2856 | + sprintf( |
|
| 2857 | + __( |
|
| 2858 | + "Could not %s %s. %s uniqueness index failed. Fields %s must form a unique set, but an entry already exists with values %s.", |
|
| 2859 | + "event_espresso" |
|
| 2860 | + ), |
|
| 2861 | + $action, |
|
| 2862 | + $this->_get_class_name(), |
|
| 2863 | + $index_name, |
|
| 2864 | + implode(",", $index->field_names()), |
|
| 2865 | + http_build_query($uniqueness_where_params) |
|
| 2866 | + ), |
|
| 2867 | + __FILE__, |
|
| 2868 | + __FUNCTION__, |
|
| 2869 | + __LINE__ |
|
| 2870 | + ); |
|
| 2871 | + return false; |
|
| 2872 | + } |
|
| 2873 | + } |
|
| 2874 | + return true; |
|
| 2875 | + } |
|
| 2876 | + |
|
| 2877 | + |
|
| 2878 | + |
|
| 2879 | + /** |
|
| 2880 | + * Checks the database for an item that conflicts (ie, if this item were |
|
| 2881 | + * saved to the DB would break some uniqueness requirement, like a primary key |
|
| 2882 | + * or an index primary key set) with the item specified. $id_obj_or_fields_array |
|
| 2883 | + * can be either an EE_Base_Class or an array of fields n values |
|
| 2884 | + * |
|
| 2885 | + * @param EE_Base_Class|array $obj_or_fields_array |
|
| 2886 | + * @param boolean $include_primary_key whether to use the model object's primary key |
|
| 2887 | + * when looking for conflicts |
|
| 2888 | + * (ie, if false, we ignore the model object's primary key |
|
| 2889 | + * when finding "conflicts". If true, it's also considered). |
|
| 2890 | + * Only works for INT primary key, |
|
| 2891 | + * STRING primary keys cannot be ignored |
|
| 2892 | + * @throws EE_Error |
|
| 2893 | + * @return EE_Base_Class|array |
|
| 2894 | + */ |
|
| 2895 | + public function get_one_conflicting($obj_or_fields_array, $include_primary_key = true) |
|
| 2896 | + { |
|
| 2897 | + if ($obj_or_fields_array instanceof EE_Base_Class) { |
|
| 2898 | + $fields_n_values = $obj_or_fields_array->model_field_array(); |
|
| 2899 | + } elseif (is_array($obj_or_fields_array)) { |
|
| 2900 | + $fields_n_values = $obj_or_fields_array; |
|
| 2901 | + } else { |
|
| 2902 | + throw new EE_Error( |
|
| 2903 | + sprintf( |
|
| 2904 | + __( |
|
| 2905 | + "%s get_all_conflicting should be called with a model object or an array of field names and values, you provided %d", |
|
| 2906 | + "event_espresso" |
|
| 2907 | + ), |
|
| 2908 | + get_class($this), |
|
| 2909 | + $obj_or_fields_array |
|
| 2910 | + ) |
|
| 2911 | + ); |
|
| 2912 | + } |
|
| 2913 | + $query_params = array(); |
|
| 2914 | + if ($this->has_primary_key_field() |
|
| 2915 | + && ($include_primary_key |
|
| 2916 | + || $this->get_primary_key_field() |
|
| 2917 | + instanceof |
|
| 2918 | + EE_Primary_Key_String_Field) |
|
| 2919 | + && isset($fields_n_values[$this->primary_key_name()]) |
|
| 2920 | + ) { |
|
| 2921 | + $query_params[0]['OR'][$this->primary_key_name()] = $fields_n_values[$this->primary_key_name()]; |
|
| 2922 | + } |
|
| 2923 | + foreach ($this->unique_indexes() as $unique_index_name => $unique_index) { |
|
| 2924 | + $uniqueness_where_params = array_intersect_key($fields_n_values, $unique_index->fields()); |
|
| 2925 | + $query_params[0]['OR']['AND*' . $unique_index_name] = $uniqueness_where_params; |
|
| 2926 | + } |
|
| 2927 | + //if there is nothing to base this search on, then we shouldn't find anything |
|
| 2928 | + if (empty($query_params)) { |
|
| 2929 | + return array(); |
|
| 2930 | + } |
|
| 2931 | + return $this->get_one($query_params); |
|
| 2932 | + } |
|
| 2933 | + |
|
| 2934 | + |
|
| 2935 | + |
|
| 2936 | + /** |
|
| 2937 | + * Like count, but is optimized and returns a boolean instead of an int |
|
| 2938 | + * |
|
| 2939 | + * @param array $query_params |
|
| 2940 | + * @return boolean |
|
| 2941 | + * @throws EE_Error |
|
| 2942 | + */ |
|
| 2943 | + public function exists($query_params) |
|
| 2944 | + { |
|
| 2945 | + $query_params['limit'] = 1; |
|
| 2946 | + return $this->count($query_params) > 0; |
|
| 2947 | + } |
|
| 2948 | + |
|
| 2949 | + |
|
| 2950 | + |
|
| 2951 | + /** |
|
| 2952 | + * Wrapper for exists, except ignores default query parameters so we're only considering ID |
|
| 2953 | + * |
|
| 2954 | + * @param int|string $id |
|
| 2955 | + * @return boolean |
|
| 2956 | + * @throws EE_Error |
|
| 2957 | + */ |
|
| 2958 | + public function exists_by_ID($id) |
|
| 2959 | + { |
|
| 2960 | + return $this->exists( |
|
| 2961 | + array( |
|
| 2962 | + 'default_where_conditions' => EEM_Base::default_where_conditions_none, |
|
| 2963 | + array( |
|
| 2964 | + $this->primary_key_name() => $id, |
|
| 2965 | + ), |
|
| 2966 | + ) |
|
| 2967 | + ); |
|
| 2968 | + } |
|
| 2969 | + |
|
| 2970 | + |
|
| 2971 | + |
|
| 2972 | + /** |
|
| 2973 | + * Inserts a new row in $table, using the $cols_n_values which apply to that table. |
|
| 2974 | + * If a $new_id is supplied and if $table is an EE_Other_Table, we assume |
|
| 2975 | + * we need to add a foreign key column to point to $new_id (which should be the primary key's value |
|
| 2976 | + * on the main table) |
|
| 2977 | + * This is protected rather than private because private is not accessible to any child methods and there MAY be |
|
| 2978 | + * cases where we want to call it directly rather than via insert(). |
|
| 2979 | + * |
|
| 2980 | + * @access protected |
|
| 2981 | + * @param EE_Table_Base $table |
|
| 2982 | + * @param array $fields_n_values each key should be in field's keys, and value should be an int, string or |
|
| 2983 | + * float |
|
| 2984 | + * @param int $new_id for now we assume only int keys |
|
| 2985 | + * @throws EE_Error |
|
| 2986 | + * @global WPDB $wpdb only used to get the $wpdb->insert_id after performing an insert |
|
| 2987 | + * @return int ID of new row inserted, or FALSE on failure |
|
| 2988 | + */ |
|
| 2989 | + protected function _insert_into_specific_table(EE_Table_Base $table, $fields_n_values, $new_id = 0) |
|
| 2990 | + { |
|
| 2991 | + global $wpdb; |
|
| 2992 | + $insertion_col_n_values = array(); |
|
| 2993 | + $format_for_insertion = array(); |
|
| 2994 | + $fields_on_table = $this->_get_fields_for_table($table->get_table_alias()); |
|
| 2995 | + foreach ($fields_on_table as $field_name => $field_obj) { |
|
| 2996 | + //check if its an auto-incrementing column, in which case we should just leave it to do its autoincrement thing |
|
| 2997 | + if ($field_obj->is_auto_increment()) { |
|
| 2998 | + continue; |
|
| 2999 | + } |
|
| 3000 | + $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
|
| 3001 | + //if the value we want to assign it to is NULL, just don't mention it for the insertion |
|
| 3002 | + if ($prepared_value !== null) { |
|
| 3003 | + $insertion_col_n_values[$field_obj->get_table_column()] = $prepared_value; |
|
| 3004 | + $format_for_insertion[] = $field_obj->get_wpdb_data_type(); |
|
| 3005 | + } |
|
| 3006 | + } |
|
| 3007 | + if ($table instanceof EE_Secondary_Table && $new_id) { |
|
| 3008 | + //its not the main table, so we should have already saved the main table's PK which we just inserted |
|
| 3009 | + //so add the fk to the main table as a column |
|
| 3010 | + $insertion_col_n_values[$table->get_fk_on_table()] = $new_id; |
|
| 3011 | + $format_for_insertion[] = '%d';//yes right now we're only allowing these foreign keys to be INTs |
|
| 3012 | + } |
|
| 3013 | + //insert the new entry |
|
| 3014 | + $result = $this->_do_wpdb_query('insert', |
|
| 3015 | + array($table->get_table_name(), $insertion_col_n_values, $format_for_insertion)); |
|
| 3016 | + if ($result === false) { |
|
| 3017 | + return false; |
|
| 3018 | + } |
|
| 3019 | + //ok, now what do we return for the ID of the newly-inserted thing? |
|
| 3020 | + if ($this->has_primary_key_field()) { |
|
| 3021 | + if ($this->get_primary_key_field()->is_auto_increment()) { |
|
| 3022 | + return $wpdb->insert_id; |
|
| 3023 | + } |
|
| 3024 | + //it's not an auto-increment primary key, so |
|
| 3025 | + //it must have been supplied |
|
| 3026 | + return $fields_n_values[$this->get_primary_key_field()->get_name()]; |
|
| 3027 | + } |
|
| 3028 | + //we can't return a primary key because there is none. instead return |
|
| 3029 | + //a unique string indicating this model |
|
| 3030 | + return $this->get_index_primary_key_string($fields_n_values); |
|
| 3031 | + } |
|
| 3032 | + |
|
| 3033 | + |
|
| 3034 | + |
|
| 3035 | + /** |
|
| 3036 | + * Prepare the $field_obj 's value in $fields_n_values for use in the database. |
|
| 3037 | + * If the field doesn't allow NULL, try to use its default. (If it doesn't allow NULL, |
|
| 3038 | + * and there is no default, we pass it along. WPDB will take care of it) |
|
| 3039 | + * |
|
| 3040 | + * @param EE_Model_Field_Base $field_obj |
|
| 3041 | + * @param array $fields_n_values |
|
| 3042 | + * @return mixed string|int|float depending on what the table column will be expecting |
|
| 3043 | + * @throws EE_Error |
|
| 3044 | + */ |
|
| 3045 | + protected function _prepare_value_or_use_default($field_obj, $fields_n_values) |
|
| 3046 | + { |
|
| 3047 | + //if this field doesn't allow nullable, don't allow it |
|
| 3048 | + if ( |
|
| 3049 | + ! $field_obj->is_nullable() |
|
| 3050 | + && ( |
|
| 3051 | + ! isset($fields_n_values[$field_obj->get_name()]) |
|
| 3052 | + || $fields_n_values[$field_obj->get_name()] === null |
|
| 3053 | + ) |
|
| 3054 | + ) { |
|
| 3055 | + $fields_n_values[$field_obj->get_name()] = $field_obj->get_default_value(); |
|
| 3056 | + } |
|
| 3057 | + $unprepared_value = isset($fields_n_values[$field_obj->get_name()]) |
|
| 3058 | + ? $fields_n_values[$field_obj->get_name()] |
|
| 3059 | + : null; |
|
| 3060 | + return $this->_prepare_value_for_use_in_db($unprepared_value, $field_obj); |
|
| 3061 | + } |
|
| 3062 | + |
|
| 3063 | + |
|
| 3064 | + |
|
| 3065 | + /** |
|
| 3066 | + * Consolidates code for preparing a value supplied to the model for use int eh db. Calls the field's |
|
| 3067 | + * prepare_for_use_in_db method on the value, and depending on $value_already_prepare_by_model_obj, may also call |
|
| 3068 | + * the field's prepare_for_set() method. |
|
| 3069 | + * |
|
| 3070 | + * @param mixed $value value in the client code domain if $value_already_prepared_by_model_object is |
|
| 3071 | + * false, otherwise a value in the model object's domain (see lengthy comment at |
|
| 3072 | + * top of file) |
|
| 3073 | + * @param EE_Model_Field_Base $field field which will be doing the preparing of the value. If null, we assume |
|
| 3074 | + * $value is a custom selection |
|
| 3075 | + * @return mixed a value ready for use in the database for insertions, updating, or in a where clause |
|
| 3076 | + */ |
|
| 3077 | + private function _prepare_value_for_use_in_db($value, $field) |
|
| 3078 | + { |
|
| 3079 | + if ($field && $field instanceof EE_Model_Field_Base) { |
|
| 3080 | + switch ($this->_values_already_prepared_by_model_object) { |
|
| 3081 | + /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 3082 | + case self::not_prepared_by_model_object: |
|
| 3083 | + $value = $field->prepare_for_set($value); |
|
| 3084 | + //purposefully left out "return" |
|
| 3085 | + case self::prepared_by_model_object: |
|
| 3086 | + /** @noinspection SuspiciousAssignmentsInspection */ |
|
| 3087 | + $value = $field->prepare_for_use_in_db($value); |
|
| 3088 | + case self::prepared_for_use_in_db: |
|
| 3089 | + //leave the value alone |
|
| 3090 | + } |
|
| 3091 | + return $value; |
|
| 3092 | + } |
|
| 3093 | + return $value; |
|
| 3094 | + } |
|
| 3095 | + |
|
| 3096 | + |
|
| 3097 | + |
|
| 3098 | + /** |
|
| 3099 | + * Returns the main table on this model |
|
| 3100 | + * |
|
| 3101 | + * @return EE_Primary_Table |
|
| 3102 | + * @throws EE_Error |
|
| 3103 | + */ |
|
| 3104 | + protected function _get_main_table() |
|
| 3105 | + { |
|
| 3106 | + foreach ($this->_tables as $table) { |
|
| 3107 | + if ($table instanceof EE_Primary_Table) { |
|
| 3108 | + return $table; |
|
| 3109 | + } |
|
| 3110 | + } |
|
| 3111 | + throw new EE_Error(sprintf(__('There are no main tables on %s. They should be added to _tables array in the constructor', |
|
| 3112 | + 'event_espresso'), get_class($this))); |
|
| 3113 | + } |
|
| 3114 | + |
|
| 3115 | + |
|
| 3116 | + |
|
| 3117 | + /** |
|
| 3118 | + * table |
|
| 3119 | + * returns EE_Primary_Table table name |
|
| 3120 | + * |
|
| 3121 | + * @return string |
|
| 3122 | + * @throws EE_Error |
|
| 3123 | + */ |
|
| 3124 | + public function table() |
|
| 3125 | + { |
|
| 3126 | + return $this->_get_main_table()->get_table_name(); |
|
| 3127 | + } |
|
| 3128 | + |
|
| 3129 | + |
|
| 3130 | + |
|
| 3131 | + /** |
|
| 3132 | + * table |
|
| 3133 | + * returns first EE_Secondary_Table table name |
|
| 3134 | + * |
|
| 3135 | + * @return string |
|
| 3136 | + */ |
|
| 3137 | + public function second_table() |
|
| 3138 | + { |
|
| 3139 | + // grab second table from tables array |
|
| 3140 | + $second_table = end($this->_tables); |
|
| 3141 | + return $second_table instanceof EE_Secondary_Table ? $second_table->get_table_name() : null; |
|
| 3142 | + } |
|
| 3143 | + |
|
| 3144 | + |
|
| 3145 | + |
|
| 3146 | + /** |
|
| 3147 | + * get_table_obj_by_alias |
|
| 3148 | + * returns table name given it's alias |
|
| 3149 | + * |
|
| 3150 | + * @param string $table_alias |
|
| 3151 | + * @return EE_Primary_Table | EE_Secondary_Table |
|
| 3152 | + */ |
|
| 3153 | + public function get_table_obj_by_alias($table_alias = '') |
|
| 3154 | + { |
|
| 3155 | + return isset($this->_tables[$table_alias]) ? $this->_tables[$table_alias] : null; |
|
| 3156 | + } |
|
| 3157 | + |
|
| 3158 | + |
|
| 3159 | + |
|
| 3160 | + /** |
|
| 3161 | + * Gets all the tables of type EE_Other_Table from EEM_CPT_Basel_Model::_tables |
|
| 3162 | + * |
|
| 3163 | + * @return EE_Secondary_Table[] |
|
| 3164 | + */ |
|
| 3165 | + protected function _get_other_tables() |
|
| 3166 | + { |
|
| 3167 | + $other_tables = array(); |
|
| 3168 | + foreach ($this->_tables as $table_alias => $table) { |
|
| 3169 | + if ($table instanceof EE_Secondary_Table) { |
|
| 3170 | + $other_tables[$table_alias] = $table; |
|
| 3171 | + } |
|
| 3172 | + } |
|
| 3173 | + return $other_tables; |
|
| 3174 | + } |
|
| 3175 | + |
|
| 3176 | + |
|
| 3177 | + |
|
| 3178 | + /** |
|
| 3179 | + * Finds all the fields that correspond to the given table |
|
| 3180 | + * |
|
| 3181 | + * @param string $table_alias , array key in EEM_Base::_tables |
|
| 3182 | + * @return EE_Model_Field_Base[] |
|
| 3183 | + */ |
|
| 3184 | + public function _get_fields_for_table($table_alias) |
|
| 3185 | + { |
|
| 3186 | + return $this->_fields[$table_alias]; |
|
| 3187 | + } |
|
| 3188 | + |
|
| 3189 | + |
|
| 3190 | + |
|
| 3191 | + /** |
|
| 3192 | + * Recurses through all the where parameters, and finds all the related models we'll need |
|
| 3193 | + * to complete this query. Eg, given where parameters like array('EVT_ID'=>3) from within Event model, we won't |
|
| 3194 | + * need any related models. But if the array were array('Registrations.REG_ID'=>3), we'd need the related |
|
| 3195 | + * Registration model. If it were array('Registrations.Transactions.Payments.PAY_ID'=>3), then we'd need the |
|
| 3196 | + * related Registration, Transaction, and Payment models. |
|
| 3197 | + * |
|
| 3198 | + * @param array $query_params like EEM_Base::get_all's $query_parameters['where'] |
|
| 3199 | + * @return EE_Model_Query_Info_Carrier |
|
| 3200 | + * @throws EE_Error |
|
| 3201 | + */ |
|
| 3202 | + public function _extract_related_models_from_query($query_params) |
|
| 3203 | + { |
|
| 3204 | + $query_info_carrier = new EE_Model_Query_Info_Carrier(); |
|
| 3205 | + if (array_key_exists(0, $query_params)) { |
|
| 3206 | + $this->_extract_related_models_from_sub_params_array_keys($query_params[0], $query_info_carrier, 0); |
|
| 3207 | + } |
|
| 3208 | + if (array_key_exists('group_by', $query_params)) { |
|
| 3209 | + if (is_array($query_params['group_by'])) { |
|
| 3210 | + $this->_extract_related_models_from_sub_params_array_values( |
|
| 3211 | + $query_params['group_by'], |
|
| 3212 | + $query_info_carrier, |
|
| 3213 | + 'group_by' |
|
| 3214 | + ); |
|
| 3215 | + } elseif (! empty ($query_params['group_by'])) { |
|
| 3216 | + $this->_extract_related_model_info_from_query_param( |
|
| 3217 | + $query_params['group_by'], |
|
| 3218 | + $query_info_carrier, |
|
| 3219 | + 'group_by' |
|
| 3220 | + ); |
|
| 3221 | + } |
|
| 3222 | + } |
|
| 3223 | + if (array_key_exists('having', $query_params)) { |
|
| 3224 | + $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3225 | + $query_params[0], |
|
| 3226 | + $query_info_carrier, |
|
| 3227 | + 'having' |
|
| 3228 | + ); |
|
| 3229 | + } |
|
| 3230 | + if (array_key_exists('order_by', $query_params)) { |
|
| 3231 | + if (is_array($query_params['order_by'])) { |
|
| 3232 | + $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3233 | + $query_params['order_by'], |
|
| 3234 | + $query_info_carrier, |
|
| 3235 | + 'order_by' |
|
| 3236 | + ); |
|
| 3237 | + } elseif (! empty($query_params['order_by'])) { |
|
| 3238 | + $this->_extract_related_model_info_from_query_param( |
|
| 3239 | + $query_params['order_by'], |
|
| 3240 | + $query_info_carrier, |
|
| 3241 | + 'order_by' |
|
| 3242 | + ); |
|
| 3243 | + } |
|
| 3244 | + } |
|
| 3245 | + if (array_key_exists('force_join', $query_params)) { |
|
| 3246 | + $this->_extract_related_models_from_sub_params_array_values( |
|
| 3247 | + $query_params['force_join'], |
|
| 3248 | + $query_info_carrier, |
|
| 3249 | + 'force_join' |
|
| 3250 | + ); |
|
| 3251 | + } |
|
| 3252 | + $this->extractRelatedModelsFromCustomSelects($query_info_carrier); |
|
| 3253 | + return $query_info_carrier; |
|
| 3254 | + } |
|
| 3255 | + |
|
| 3256 | + |
|
| 3257 | + |
|
| 3258 | + /** |
|
| 3259 | + * For extracting related models from WHERE (0), HAVING (having), ORDER BY (order_by) or forced joins (force_join) |
|
| 3260 | + * |
|
| 3261 | + * @param array $sub_query_params like EEM_Base::get_all's $query_params[0] or |
|
| 3262 | + * $query_params['having'] |
|
| 3263 | + * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
| 3264 | + * @param string $query_param_type one of $this->_allowed_query_params |
|
| 3265 | + * @throws EE_Error |
|
| 3266 | + * @return \EE_Model_Query_Info_Carrier |
|
| 3267 | + */ |
|
| 3268 | + private function _extract_related_models_from_sub_params_array_keys( |
|
| 3269 | + $sub_query_params, |
|
| 3270 | + EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
| 3271 | + $query_param_type |
|
| 3272 | + ) { |
|
| 3273 | + if (! empty($sub_query_params)) { |
|
| 3274 | + $sub_query_params = (array)$sub_query_params; |
|
| 3275 | + foreach ($sub_query_params as $param => $possibly_array_of_params) { |
|
| 3276 | + //$param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
| 3277 | + $this->_extract_related_model_info_from_query_param($param, $model_query_info_carrier, |
|
| 3278 | + $query_param_type); |
|
| 3279 | + //if $possibly_array_of_params is an array, try recursing into it, searching for keys which |
|
| 3280 | + //indicate needed joins. Eg, array('NOT'=>array('Registration.TXN_ID'=>23)). In this case, we tried |
|
| 3281 | + //extracting models out of the 'NOT', which obviously wasn't successful, and then we recurse into the value |
|
| 3282 | + //of array('Registration.TXN_ID'=>23) |
|
| 3283 | + $query_param_sans_stars = $this->_remove_stars_and_anything_after_from_condition_query_param_key($param); |
|
| 3284 | + if (in_array($query_param_sans_stars, $this->_logic_query_param_keys, true)) { |
|
| 3285 | + if (! is_array($possibly_array_of_params)) { |
|
| 3286 | + throw new EE_Error(sprintf(__("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'))", |
|
| 3287 | + "event_espresso"), |
|
| 3288 | + $param, $possibly_array_of_params)); |
|
| 3289 | + } |
|
| 3290 | + $this->_extract_related_models_from_sub_params_array_keys( |
|
| 3291 | + $possibly_array_of_params, |
|
| 3292 | + $model_query_info_carrier, $query_param_type |
|
| 3293 | + ); |
|
| 3294 | + } elseif ($query_param_type === 0 //ie WHERE |
|
| 3295 | + && is_array($possibly_array_of_params) |
|
| 3296 | + && isset($possibly_array_of_params[2]) |
|
| 3297 | + && $possibly_array_of_params[2] == true |
|
| 3298 | + ) { |
|
| 3299 | + //then $possible_array_of_params looks something like array('<','DTT_sold',true) |
|
| 3300 | + //indicating that $possible_array_of_params[1] is actually a field name, |
|
| 3301 | + //from which we should extract query parameters! |
|
| 3302 | + if (! isset($possibly_array_of_params[0], $possibly_array_of_params[1])) { |
|
| 3303 | + throw new EE_Error(sprintf(__("Improperly formed query parameter %s. It should be numerically indexed like array('<','DTT_sold',true); but you provided %s", |
|
| 3304 | + "event_espresso"), $query_param_type, implode(",", $possibly_array_of_params))); |
|
| 3305 | + } |
|
| 3306 | + $this->_extract_related_model_info_from_query_param($possibly_array_of_params[1], |
|
| 3307 | + $model_query_info_carrier, $query_param_type); |
|
| 3308 | + } |
|
| 3309 | + } |
|
| 3310 | + } |
|
| 3311 | + return $model_query_info_carrier; |
|
| 3312 | + } |
|
| 3313 | + |
|
| 3314 | + |
|
| 3315 | + |
|
| 3316 | + /** |
|
| 3317 | + * For extracting related models from forced_joins, where the array values contain the info about what |
|
| 3318 | + * models to join with. Eg an array like array('Attendee','Price.Price_Type'); |
|
| 3319 | + * |
|
| 3320 | + * @param array $sub_query_params like EEM_Base::get_all's $query_params[0] or |
|
| 3321 | + * $query_params['having'] |
|
| 3322 | + * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
|
| 3323 | + * @param string $query_param_type one of $this->_allowed_query_params |
|
| 3324 | + * @throws EE_Error |
|
| 3325 | + * @return \EE_Model_Query_Info_Carrier |
|
| 3326 | + */ |
|
| 3327 | + private function _extract_related_models_from_sub_params_array_values( |
|
| 3328 | + $sub_query_params, |
|
| 3329 | + EE_Model_Query_Info_Carrier $model_query_info_carrier, |
|
| 3330 | + $query_param_type |
|
| 3331 | + ) { |
|
| 3332 | + if (! empty($sub_query_params)) { |
|
| 3333 | + if (! is_array($sub_query_params)) { |
|
| 3334 | + throw new EE_Error(sprintf(__("Query parameter %s should be an array, but it isn't.", "event_espresso"), |
|
| 3335 | + $sub_query_params)); |
|
| 3336 | + } |
|
| 3337 | + foreach ($sub_query_params as $param) { |
|
| 3338 | + //$param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
|
| 3339 | + $this->_extract_related_model_info_from_query_param($param, $model_query_info_carrier, |
|
| 3340 | + $query_param_type); |
|
| 3341 | + } |
|
| 3342 | + } |
|
| 3343 | + return $model_query_info_carrier; |
|
| 3344 | + } |
|
| 3345 | + |
|
| 3346 | + |
|
| 3347 | + |
|
| 3348 | + /** |
|
| 3349 | + * Extract all the query parts from $query_params (an array like whats passed to EEM_Base::get_all) |
|
| 3350 | + * and put into a EEM_Related_Model_Info_Carrier for easy extraction into a query. We create this object |
|
| 3351 | + * instead of directly constructing the SQL because often we need to extract info from the $query_params |
|
| 3352 | + * but use them in a different order. Eg, we need to know what models we are querying |
|
| 3353 | + * before we know what joins to perform. However, we need to know what data types correspond to which fields on |
|
| 3354 | + * other models before we can finalize the where clause SQL. |
|
| 3355 | + * |
|
| 3356 | + * @param array $query_params |
|
| 3357 | + * @throws EE_Error |
|
| 3358 | + * @return EE_Model_Query_Info_Carrier |
|
| 3359 | + */ |
|
| 3360 | + public function _create_model_query_info_carrier($query_params) |
|
| 3361 | + { |
|
| 3362 | + if (! is_array($query_params)) { |
|
| 3363 | + EE_Error::doing_it_wrong( |
|
| 3364 | + 'EEM_Base::_create_model_query_info_carrier', |
|
| 3365 | + sprintf( |
|
| 3366 | + __( |
|
| 3367 | + '$query_params should be an array, you passed a variable of type %s', |
|
| 3368 | + 'event_espresso' |
|
| 3369 | + ), |
|
| 3370 | + gettype($query_params) |
|
| 3371 | + ), |
|
| 3372 | + '4.6.0' |
|
| 3373 | + ); |
|
| 3374 | + $query_params = array(); |
|
| 3375 | + } |
|
| 3376 | + $where_query_params = isset($query_params[0]) ? $query_params[0] : array(); |
|
| 3377 | + //first check if we should alter the query to account for caps or not |
|
| 3378 | + //because the caps might require us to do extra joins |
|
| 3379 | + if (isset($query_params['caps']) && $query_params['caps'] !== 'none') { |
|
| 3380 | + $query_params[0] = $where_query_params = array_replace_recursive( |
|
| 3381 | + $where_query_params, |
|
| 3382 | + $this->caps_where_conditions( |
|
| 3383 | + $query_params['caps'] |
|
| 3384 | + ) |
|
| 3385 | + ); |
|
| 3386 | + } |
|
| 3387 | + $query_object = $this->_extract_related_models_from_query($query_params); |
|
| 3388 | + //verify where_query_params has NO numeric indexes.... that's simply not how you use it! |
|
| 3389 | + foreach ($where_query_params as $key => $value) { |
|
| 3390 | + if (is_int($key)) { |
|
| 3391 | + throw new EE_Error( |
|
| 3392 | + sprintf( |
|
| 3393 | + __( |
|
| 3394 | + "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.", |
|
| 3395 | + "event_espresso" |
|
| 3396 | + ), |
|
| 3397 | + $key, |
|
| 3398 | + var_export($value, true), |
|
| 3399 | + var_export($query_params, true), |
|
| 3400 | + get_class($this) |
|
| 3401 | + ) |
|
| 3402 | + ); |
|
| 3403 | + } |
|
| 3404 | + } |
|
| 3405 | + if ( |
|
| 3406 | + 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 | + $where_query_params = array_merge( |
|
| 3414 | + $this->_get_default_where_conditions_for_models_in_query( |
|
| 3415 | + $query_object, |
|
| 3416 | + $use_default_where_conditions, |
|
| 3417 | + $where_query_params |
|
| 3418 | + ), |
|
| 3419 | + $where_query_params |
|
| 3420 | + ); |
|
| 3421 | + $query_object->set_where_sql($this->_construct_where_clause($where_query_params)); |
|
| 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 like EEM_Base::get_all() 's $query_params[0] |
|
| 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($cap_where_conditions, |
|
| 3573 | + $restriction_if_no_cap->get_default_where_conditions()); |
|
| 3574 | + } |
|
| 3575 | + return apply_filters('FHEE__EEM_Base__caps_where_conditions__return', $cap_where_conditions, $this, $context, |
|
| 3576 | + $cap_restrictions); |
|
| 3577 | + } |
|
| 3578 | + |
|
| 3579 | + |
|
| 3580 | + |
|
| 3581 | + /** |
|
| 3582 | + * Verifies that $should_be_order_string is in $this->_allowed_order_values, |
|
| 3583 | + * otherwise throws an exception |
|
| 3584 | + * |
|
| 3585 | + * @param string $should_be_order_string |
|
| 3586 | + * @return string either ASC, asc, DESC or desc |
|
| 3587 | + * @throws EE_Error |
|
| 3588 | + */ |
|
| 3589 | + private function _extract_order($should_be_order_string) |
|
| 3590 | + { |
|
| 3591 | + if (in_array($should_be_order_string, $this->_allowed_order_values)) { |
|
| 3592 | + return $should_be_order_string; |
|
| 3593 | + } |
|
| 3594 | + throw new EE_Error( |
|
| 3595 | + sprintf( |
|
| 3596 | + __( |
|
| 3597 | + "While performing a query on '%s', tried to use '%s' as an order parameter. ", |
|
| 3598 | + "event_espresso" |
|
| 3599 | + ), get_class($this), $should_be_order_string |
|
| 3600 | + ) |
|
| 3601 | + ); |
|
| 3602 | + } |
|
| 3603 | + |
|
| 3604 | + |
|
| 3605 | + |
|
| 3606 | + /** |
|
| 3607 | + * Looks at all the models which are included in this query, and asks each |
|
| 3608 | + * for their universal_where_params, and returns them in the same format as $query_params[0] (where), |
|
| 3609 | + * so they can be merged |
|
| 3610 | + * |
|
| 3611 | + * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
| 3612 | + * @param string $use_default_where_conditions can be 'none','other_models_only', or 'all'. |
|
| 3613 | + * 'none' means NO default where conditions will |
|
| 3614 | + * be used AT ALL during this query. |
|
| 3615 | + * 'other_models_only' means default where |
|
| 3616 | + * conditions from other models will be used, but |
|
| 3617 | + * not for this primary model. 'all', the default, |
|
| 3618 | + * means default where conditions will apply as |
|
| 3619 | + * normal |
|
| 3620 | + * @param array $where_query_params like EEM_Base::get_all's $query_params[0] |
|
| 3621 | + * @throws EE_Error |
|
| 3622 | + * @return array like $query_params[0], see EEM_Base::get_all for documentation |
|
| 3623 | + */ |
|
| 3624 | + private function _get_default_where_conditions_for_models_in_query( |
|
| 3625 | + EE_Model_Query_Info_Carrier $query_info_carrier, |
|
| 3626 | + $use_default_where_conditions = EEM_Base::default_where_conditions_all, |
|
| 3627 | + $where_query_params = array() |
|
| 3628 | + ) { |
|
| 3629 | + $allowed_used_default_where_conditions_values = EEM_Base::valid_default_where_conditions(); |
|
| 3630 | + if (! in_array($use_default_where_conditions, $allowed_used_default_where_conditions_values)) { |
|
| 3631 | + throw new EE_Error(sprintf(__("You passed an invalid value to the query parameter 'default_where_conditions' of '%s'. Allowed values are %s", |
|
| 3632 | + "event_espresso"), $use_default_where_conditions, |
|
| 3633 | + implode(", ", $allowed_used_default_where_conditions_values))); |
|
| 3634 | + } |
|
| 3635 | + $universal_query_params = array(); |
|
| 3636 | + if ($this->_should_use_default_where_conditions( $use_default_where_conditions, true)) { |
|
| 3637 | + $universal_query_params = $this->_get_default_where_conditions(); |
|
| 3638 | + } else if ($this->_should_use_minimum_where_conditions( $use_default_where_conditions, true)) { |
|
| 3639 | + $universal_query_params = $this->_get_minimum_where_conditions(); |
|
| 3640 | + } |
|
| 3641 | + foreach ($query_info_carrier->get_model_names_included() as $model_relation_path => $model_name) { |
|
| 3642 | + $related_model = $this->get_related_model_obj($model_name); |
|
| 3643 | + if ( $this->_should_use_default_where_conditions( $use_default_where_conditions, false)) { |
|
| 3644 | + $related_model_universal_where_params = $related_model->_get_default_where_conditions($model_relation_path); |
|
| 3645 | + } elseif ($this->_should_use_minimum_where_conditions( $use_default_where_conditions, false)) { |
|
| 3646 | + $related_model_universal_where_params = $related_model->_get_minimum_where_conditions($model_relation_path); |
|
| 3647 | + } else { |
|
| 3648 | + //we don't want to add full or even minimum default where conditions from this model, so just continue |
|
| 3649 | + continue; |
|
| 3650 | + } |
|
| 3651 | + $overrides = $this->_override_defaults_or_make_null_friendly( |
|
| 3652 | + $related_model_universal_where_params, |
|
| 3653 | + $where_query_params, |
|
| 3654 | + $related_model, |
|
| 3655 | + $model_relation_path |
|
| 3656 | + ); |
|
| 3657 | + $universal_query_params = EEH_Array::merge_arrays_and_overwrite_keys( |
|
| 3658 | + $universal_query_params, |
|
| 3659 | + $overrides |
|
| 3660 | + ); |
|
| 3661 | + } |
|
| 3662 | + return $universal_query_params; |
|
| 3663 | + } |
|
| 3664 | + |
|
| 3665 | + |
|
| 3666 | + |
|
| 3667 | + /** |
|
| 3668 | + * Determines whether or not we should use default where conditions for the model in question |
|
| 3669 | + * (this model, or other related models). |
|
| 3670 | + * Basically, we should use default where conditions on this model if they have requested to use them on all models, |
|
| 3671 | + * this model only, or to use minimum where conditions on all other models and normal where conditions on this one. |
|
| 3672 | + * We should use default where conditions on related models when they requested to use default where conditions |
|
| 3673 | + * on all models, or specifically just on other related models |
|
| 3674 | + * @param $default_where_conditions_value |
|
| 3675 | + * @param bool $for_this_model false means this is for OTHER related models |
|
| 3676 | + * @return bool |
|
| 3677 | + */ |
|
| 3678 | + private function _should_use_default_where_conditions( $default_where_conditions_value, $for_this_model = true ) |
|
| 3679 | + { |
|
| 3680 | + return ( |
|
| 3681 | + $for_this_model |
|
| 3682 | + && in_array( |
|
| 3683 | + $default_where_conditions_value, |
|
| 3684 | + array( |
|
| 3685 | + EEM_Base::default_where_conditions_all, |
|
| 3686 | + EEM_Base::default_where_conditions_this_only, |
|
| 3687 | + EEM_Base::default_where_conditions_minimum_others, |
|
| 3688 | + ), |
|
| 3689 | + true |
|
| 3690 | + ) |
|
| 3691 | + ) |
|
| 3692 | + || ( |
|
| 3693 | + ! $for_this_model |
|
| 3694 | + && in_array( |
|
| 3695 | + $default_where_conditions_value, |
|
| 3696 | + array( |
|
| 3697 | + EEM_Base::default_where_conditions_all, |
|
| 3698 | + EEM_Base::default_where_conditions_others_only, |
|
| 3699 | + ), |
|
| 3700 | + true |
|
| 3701 | + ) |
|
| 3702 | + ); |
|
| 3703 | + } |
|
| 3704 | + |
|
| 3705 | + /** |
|
| 3706 | + * Determines whether or not we should use default minimum conditions for the model in question |
|
| 3707 | + * (this model, or other related models). |
|
| 3708 | + * Basically, we should use minimum where conditions on this model only if they requested all models to use minimum |
|
| 3709 | + * where conditions. |
|
| 3710 | + * We should use minimum where conditions on related models if they requested to use minimum where conditions |
|
| 3711 | + * on this model or others |
|
| 3712 | + * @param $default_where_conditions_value |
|
| 3713 | + * @param bool $for_this_model false means this is for OTHER related models |
|
| 3714 | + * @return bool |
|
| 3715 | + */ |
|
| 3716 | + private function _should_use_minimum_where_conditions($default_where_conditions_value, $for_this_model = true) |
|
| 3717 | + { |
|
| 3718 | + return ( |
|
| 3719 | + $for_this_model |
|
| 3720 | + && $default_where_conditions_value === EEM_Base::default_where_conditions_minimum_all |
|
| 3721 | + ) |
|
| 3722 | + || ( |
|
| 3723 | + ! $for_this_model |
|
| 3724 | + && in_array( |
|
| 3725 | + $default_where_conditions_value, |
|
| 3726 | + array( |
|
| 3727 | + EEM_Base::default_where_conditions_minimum_others, |
|
| 3728 | + EEM_Base::default_where_conditions_minimum_all, |
|
| 3729 | + ), |
|
| 3730 | + true |
|
| 3731 | + ) |
|
| 3732 | + ); |
|
| 3733 | + } |
|
| 3734 | + |
|
| 3735 | + |
|
| 3736 | + /** |
|
| 3737 | + * Checks if any of the defaults have been overridden. If there are any that AREN'T overridden, |
|
| 3738 | + * then we also add a special where condition which allows for that model's primary key |
|
| 3739 | + * to be null (which is important for JOINs. Eg, if you want to see all Events ordered by Venue's name, |
|
| 3740 | + * then Event's with NO Venue won't appear unless you allow VNU_ID to be NULL) |
|
| 3741 | + * |
|
| 3742 | + * @param array $default_where_conditions |
|
| 3743 | + * @param array $provided_where_conditions |
|
| 3744 | + * @param EEM_Base $model |
|
| 3745 | + * @param string $model_relation_path like 'Transaction.Payment.' |
|
| 3746 | + * @return array like EEM_Base::get_all's $query_params[0] |
|
| 3747 | + * @throws EE_Error |
|
| 3748 | + */ |
|
| 3749 | + private function _override_defaults_or_make_null_friendly( |
|
| 3750 | + $default_where_conditions, |
|
| 3751 | + $provided_where_conditions, |
|
| 3752 | + $model, |
|
| 3753 | + $model_relation_path |
|
| 3754 | + ) { |
|
| 3755 | + $null_friendly_where_conditions = array(); |
|
| 3756 | + $none_overridden = true; |
|
| 3757 | + $or_condition_key_for_defaults = 'OR*' . get_class($model); |
|
| 3758 | + foreach ($default_where_conditions as $key => $val) { |
|
| 3759 | + if (isset($provided_where_conditions[$key])) { |
|
| 3760 | + $none_overridden = false; |
|
| 3761 | + } else { |
|
| 3762 | + $null_friendly_where_conditions[$or_condition_key_for_defaults]['AND'][$key] = $val; |
|
| 3763 | + } |
|
| 3764 | + } |
|
| 3765 | + if ($none_overridden && $default_where_conditions) { |
|
| 3766 | + if ($model->has_primary_key_field()) { |
|
| 3767 | + $null_friendly_where_conditions[$or_condition_key_for_defaults][$model_relation_path |
|
| 3768 | + . "." |
|
| 3769 | + . $model->primary_key_name()] = array('IS NULL'); |
|
| 3770 | + }/*else{ |
|
| 3771 | 3771 | //@todo NO PK, use other defaults |
| 3772 | 3772 | }*/ |
| 3773 | - } |
|
| 3774 | - return $null_friendly_where_conditions; |
|
| 3775 | - } |
|
| 3776 | - |
|
| 3777 | - |
|
| 3778 | - |
|
| 3779 | - /** |
|
| 3780 | - * Uses the _default_where_conditions_strategy set during __construct() to get |
|
| 3781 | - * default where conditions on all get_all, update, and delete queries done by this model. |
|
| 3782 | - * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
| 3783 | - * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
| 3784 | - * |
|
| 3785 | - * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
| 3786 | - * @return array like EEM_Base::get_all's $query_params[0] (where conditions) |
|
| 3787 | - */ |
|
| 3788 | - private function _get_default_where_conditions($model_relation_path = null) |
|
| 3789 | - { |
|
| 3790 | - if ($this->_ignore_where_strategy) { |
|
| 3791 | - return array(); |
|
| 3792 | - } |
|
| 3793 | - return $this->_default_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
| 3794 | - } |
|
| 3795 | - |
|
| 3796 | - |
|
| 3797 | - |
|
| 3798 | - /** |
|
| 3799 | - * Uses the _minimum_where_conditions_strategy set during __construct() to get |
|
| 3800 | - * minimum where conditions on all get_all, update, and delete queries done by this model. |
|
| 3801 | - * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
| 3802 | - * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
| 3803 | - * Similar to _get_default_where_conditions |
|
| 3804 | - * |
|
| 3805 | - * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
| 3806 | - * @return array like EEM_Base::get_all's $query_params[0] (where conditions) |
|
| 3807 | - */ |
|
| 3808 | - protected function _get_minimum_where_conditions($model_relation_path = null) |
|
| 3809 | - { |
|
| 3810 | - if ($this->_ignore_where_strategy) { |
|
| 3811 | - return array(); |
|
| 3812 | - } |
|
| 3813 | - return $this->_minimum_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
| 3814 | - } |
|
| 3815 | - |
|
| 3816 | - |
|
| 3817 | - |
|
| 3818 | - /** |
|
| 3819 | - * Creates the string of SQL for the select part of a select query, everything behind SELECT and before FROM. |
|
| 3820 | - * Eg, "Event.post_id, Event.post_name,Event_Detail.EVT_ID..." |
|
| 3821 | - * |
|
| 3822 | - * @param EE_Model_Query_Info_Carrier $model_query_info |
|
| 3823 | - * @return string |
|
| 3824 | - * @throws EE_Error |
|
| 3825 | - */ |
|
| 3826 | - private function _construct_default_select_sql(EE_Model_Query_Info_Carrier $model_query_info) |
|
| 3827 | - { |
|
| 3828 | - $selects = $this->_get_columns_to_select_for_this_model(); |
|
| 3829 | - foreach ( |
|
| 3830 | - $model_query_info->get_model_names_included() as $model_relation_chain => |
|
| 3831 | - $name_of_other_model_included |
|
| 3832 | - ) { |
|
| 3833 | - $other_model_included = $this->get_related_model_obj($name_of_other_model_included); |
|
| 3834 | - $other_model_selects = $other_model_included->_get_columns_to_select_for_this_model($model_relation_chain); |
|
| 3835 | - foreach ($other_model_selects as $key => $value) { |
|
| 3836 | - $selects[] = $value; |
|
| 3837 | - } |
|
| 3838 | - } |
|
| 3839 | - return implode(", ", $selects); |
|
| 3840 | - } |
|
| 3841 | - |
|
| 3842 | - |
|
| 3843 | - |
|
| 3844 | - /** |
|
| 3845 | - * Gets an array of columns to select for this model, which are necessary for it to create its objects. |
|
| 3846 | - * So that's going to be the columns for all the fields on the model |
|
| 3847 | - * |
|
| 3848 | - * @param string $model_relation_chain like 'Question.Question_Group.Event' |
|
| 3849 | - * @return array numerically indexed, values are columns to select and rename, eg "Event.ID AS 'Event.ID'" |
|
| 3850 | - */ |
|
| 3851 | - public function _get_columns_to_select_for_this_model($model_relation_chain = '') |
|
| 3852 | - { |
|
| 3853 | - $fields = $this->field_settings(); |
|
| 3854 | - $selects = array(); |
|
| 3855 | - $table_alias_with_model_relation_chain_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 3856 | - $this->get_this_model_name()); |
|
| 3857 | - foreach ($fields as $field_obj) { |
|
| 3858 | - $selects[] = $table_alias_with_model_relation_chain_prefix |
|
| 3859 | - . $field_obj->get_table_alias() |
|
| 3860 | - . "." |
|
| 3861 | - . $field_obj->get_table_column() |
|
| 3862 | - . " AS '" |
|
| 3863 | - . $table_alias_with_model_relation_chain_prefix |
|
| 3864 | - . $field_obj->get_table_alias() |
|
| 3865 | - . "." |
|
| 3866 | - . $field_obj->get_table_column() |
|
| 3867 | - . "'"; |
|
| 3868 | - } |
|
| 3869 | - //make sure we are also getting the PKs of each table |
|
| 3870 | - $tables = $this->get_tables(); |
|
| 3871 | - if (count($tables) > 1) { |
|
| 3872 | - foreach ($tables as $table_obj) { |
|
| 3873 | - $qualified_pk_column = $table_alias_with_model_relation_chain_prefix |
|
| 3874 | - . $table_obj->get_fully_qualified_pk_column(); |
|
| 3875 | - if (! in_array($qualified_pk_column, $selects)) { |
|
| 3876 | - $selects[] = "$qualified_pk_column AS '$qualified_pk_column'"; |
|
| 3877 | - } |
|
| 3878 | - } |
|
| 3879 | - } |
|
| 3880 | - return $selects; |
|
| 3881 | - } |
|
| 3882 | - |
|
| 3883 | - |
|
| 3884 | - |
|
| 3885 | - /** |
|
| 3886 | - * Given a $query_param like 'Registration.Transaction.TXN_ID', pops off 'Registration.', |
|
| 3887 | - * gets the join statement for it; gets the data types for it; and passes the remaining 'Transaction.TXN_ID' |
|
| 3888 | - * onto its related Transaction object to do the same. Returns an EE_Join_And_Data_Types object which contains the |
|
| 3889 | - * SQL for joining, and the data types |
|
| 3890 | - * |
|
| 3891 | - * @param null|string $original_query_param |
|
| 3892 | - * @param string $query_param like Registration.Transaction.TXN_ID |
|
| 3893 | - * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
| 3894 | - * @param string $query_param_type like Registration.Transaction.TXN_ID |
|
| 3895 | - * or 'PAY_ID'. Otherwise, we don't expect there to be a |
|
| 3896 | - * column name. We only want model names, eg 'Event.Venue' |
|
| 3897 | - * or 'Registration's |
|
| 3898 | - * @param string $original_query_param what it originally was (eg |
|
| 3899 | - * Registration.Transaction.TXN_ID). If null, we assume it |
|
| 3900 | - * matches $query_param |
|
| 3901 | - * @throws EE_Error |
|
| 3902 | - * @return void only modifies the EEM_Related_Model_Info_Carrier passed into it |
|
| 3903 | - */ |
|
| 3904 | - private function _extract_related_model_info_from_query_param( |
|
| 3905 | - $query_param, |
|
| 3906 | - EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
| 3907 | - $query_param_type, |
|
| 3908 | - $original_query_param = null |
|
| 3909 | - ) { |
|
| 3910 | - if ($original_query_param === null) { |
|
| 3911 | - $original_query_param = $query_param; |
|
| 3912 | - } |
|
| 3913 | - $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param); |
|
| 3914 | - /** @var $allow_logic_query_params bool whether or not to allow logic_query_params like 'NOT','OR', or 'AND' */ |
|
| 3915 | - $allow_logic_query_params = in_array($query_param_type, array('where', 'having', 0, 'custom_selects'), true); |
|
| 3916 | - $allow_fields = in_array( |
|
| 3917 | - $query_param_type, |
|
| 3918 | - array('where', 'having', 'order_by', 'group_by', 'order', 'custom_selects', 0), |
|
| 3919 | - true |
|
| 3920 | - ); |
|
| 3921 | - //check to see if we have a field on this model |
|
| 3922 | - $this_model_fields = $this->field_settings(true); |
|
| 3923 | - if (array_key_exists($query_param, $this_model_fields)) { |
|
| 3924 | - if ($allow_fields) { |
|
| 3925 | - return; |
|
| 3926 | - } |
|
| 3927 | - throw new EE_Error( |
|
| 3928 | - sprintf( |
|
| 3929 | - __( |
|
| 3930 | - "Using a field name (%s) on model %s is not allowed on this query param type '%s'. Original query param was %s", |
|
| 3931 | - "event_espresso" |
|
| 3932 | - ), |
|
| 3933 | - $query_param, get_class($this), $query_param_type, $original_query_param |
|
| 3934 | - ) |
|
| 3935 | - ); |
|
| 3936 | - } |
|
| 3937 | - //check if this is a special logic query param |
|
| 3938 | - if (in_array($query_param, $this->_logic_query_param_keys, true)) { |
|
| 3939 | - if ($allow_logic_query_params) { |
|
| 3940 | - return; |
|
| 3941 | - } |
|
| 3942 | - throw new EE_Error( |
|
| 3943 | - sprintf( |
|
| 3944 | - __( |
|
| 3945 | - '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', |
|
| 3946 | - 'event_espresso' |
|
| 3947 | - ), |
|
| 3948 | - implode('", "', $this->_logic_query_param_keys), |
|
| 3949 | - $query_param, |
|
| 3950 | - get_class($this), |
|
| 3951 | - '<br />', |
|
| 3952 | - "\t" |
|
| 3953 | - . ' $passed_in_query_info = <pre>' |
|
| 3954 | - . print_r($passed_in_query_info, true) |
|
| 3955 | - . '</pre>' |
|
| 3956 | - . "\n\t" |
|
| 3957 | - . ' $query_param_type = ' |
|
| 3958 | - . $query_param_type |
|
| 3959 | - . "\n\t" |
|
| 3960 | - . ' $original_query_param = ' |
|
| 3961 | - . $original_query_param |
|
| 3962 | - ) |
|
| 3963 | - ); |
|
| 3964 | - } |
|
| 3965 | - //check if it's a custom selection |
|
| 3966 | - if ($this->_custom_selections instanceof CustomSelects |
|
| 3967 | - && in_array($query_param, $this->_custom_selections->columnAliases(), true) |
|
| 3968 | - ) { |
|
| 3969 | - return; |
|
| 3970 | - } |
|
| 3971 | - //check if has a model name at the beginning |
|
| 3972 | - //and |
|
| 3973 | - //check if it's a field on a related model |
|
| 3974 | - if ($this->extractJoinModelFromQueryParams( |
|
| 3975 | - $passed_in_query_info, |
|
| 3976 | - $query_param, |
|
| 3977 | - $original_query_param, |
|
| 3978 | - $query_param_type |
|
| 3979 | - )) { |
|
| 3980 | - return; |
|
| 3981 | - } |
|
| 3982 | - |
|
| 3983 | - //ok so $query_param didn't start with a model name |
|
| 3984 | - //and we previously confirmed it wasn't a logic query param or field on the current model |
|
| 3985 | - //it's wack, that's what it is |
|
| 3986 | - throw new EE_Error( |
|
| 3987 | - sprintf( |
|
| 3988 | - esc_html__( |
|
| 3989 | - "There is no model named '%s' related to %s. Query param type is %s and original query param is %s", |
|
| 3990 | - "event_espresso" |
|
| 3991 | - ), |
|
| 3992 | - $query_param, |
|
| 3993 | - get_class($this), |
|
| 3994 | - $query_param_type, |
|
| 3995 | - $original_query_param |
|
| 3996 | - ) |
|
| 3997 | - ); |
|
| 3998 | - } |
|
| 3999 | - |
|
| 4000 | - |
|
| 4001 | - /** |
|
| 4002 | - * Extracts any possible join model information from the provided possible_join_string. |
|
| 4003 | - * This method will read the provided $possible_join_string value and determine if there are any possible model join |
|
| 4004 | - * parts that should be added to the query. |
|
| 4005 | - * |
|
| 4006 | - * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
| 4007 | - * @param string $possible_join_string Such as Registration.REG_ID, or Registration |
|
| 4008 | - * @param null|string $original_query_param |
|
| 4009 | - * @param string $query_parameter_type The type for the source of the $possible_join_string |
|
| 4010 | - * ('where', 'order_by', 'group_by', 'custom_selects' etc.) |
|
| 4011 | - * @return bool returns true if a join was added and false if not. |
|
| 4012 | - * @throws EE_Error |
|
| 4013 | - */ |
|
| 4014 | - private function extractJoinModelFromQueryParams( |
|
| 4015 | - EE_Model_Query_Info_Carrier $query_info_carrier, |
|
| 4016 | - $possible_join_string, |
|
| 4017 | - $original_query_param, |
|
| 4018 | - $query_parameter_type |
|
| 4019 | - ) { |
|
| 4020 | - foreach ($this->_model_relations as $valid_related_model_name => $relation_obj) { |
|
| 4021 | - if (strpos($possible_join_string, $valid_related_model_name . ".") === 0) { |
|
| 4022 | - $this->_add_join_to_model($valid_related_model_name, $query_info_carrier, $original_query_param); |
|
| 4023 | - $possible_join_string = substr($possible_join_string, strlen($valid_related_model_name . ".")); |
|
| 4024 | - if ($possible_join_string === '') { |
|
| 4025 | - //nothing left to $query_param |
|
| 4026 | - //we should actually end in a field name, not a model like this! |
|
| 4027 | - throw new EE_Error( |
|
| 4028 | - sprintf( |
|
| 4029 | - esc_html__( |
|
| 4030 | - "Query param '%s' (of type %s on model %s) shouldn't end on a period (.) ", |
|
| 4031 | - "event_espresso" |
|
| 4032 | - ), |
|
| 4033 | - $possible_join_string, |
|
| 4034 | - $query_parameter_type, |
|
| 4035 | - get_class($this), |
|
| 4036 | - $valid_related_model_name |
|
| 4037 | - ) |
|
| 4038 | - ); |
|
| 4039 | - } |
|
| 4040 | - $related_model_obj = $this->get_related_model_obj($valid_related_model_name); |
|
| 4041 | - $related_model_obj->_extract_related_model_info_from_query_param( |
|
| 4042 | - $possible_join_string, |
|
| 4043 | - $query_info_carrier, |
|
| 4044 | - $query_parameter_type, |
|
| 4045 | - $original_query_param |
|
| 4046 | - ); |
|
| 4047 | - return true; |
|
| 4048 | - } |
|
| 4049 | - if ($possible_join_string === $valid_related_model_name) { |
|
| 4050 | - $this->_add_join_to_model( |
|
| 4051 | - $valid_related_model_name, |
|
| 4052 | - $query_info_carrier, |
|
| 4053 | - $original_query_param |
|
| 4054 | - ); |
|
| 4055 | - return true; |
|
| 4056 | - } |
|
| 4057 | - } |
|
| 4058 | - return false; |
|
| 4059 | - } |
|
| 4060 | - |
|
| 4061 | - |
|
| 4062 | - /** |
|
| 4063 | - * Extracts related models from Custom Selects and sets up any joins for those related models. |
|
| 4064 | - * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
| 4065 | - * @throws EE_Error |
|
| 4066 | - */ |
|
| 4067 | - private function extractRelatedModelsFromCustomSelects(EE_Model_Query_Info_Carrier $query_info_carrier) |
|
| 4068 | - { |
|
| 4069 | - if ($this->_custom_selections instanceof CustomSelects |
|
| 4070 | - && ($this->_custom_selections->type() === CustomSelects::TYPE_STRUCTURED |
|
| 4071 | - || $this->_custom_selections->type() == CustomSelects::TYPE_COMPLEX |
|
| 4072 | - ) |
|
| 4073 | - ) { |
|
| 4074 | - $original_selects = $this->_custom_selections->originalSelects(); |
|
| 4075 | - foreach ($original_selects as $alias => $select_configuration) { |
|
| 4076 | - $this->extractJoinModelFromQueryParams( |
|
| 4077 | - $query_info_carrier, |
|
| 4078 | - $select_configuration[0], |
|
| 4079 | - $select_configuration[0], |
|
| 4080 | - 'custom_selects' |
|
| 4081 | - ); |
|
| 4082 | - } |
|
| 4083 | - } |
|
| 4084 | - } |
|
| 4085 | - |
|
| 4086 | - |
|
| 4087 | - |
|
| 4088 | - /** |
|
| 4089 | - * Privately used by _extract_related_model_info_from_query_param to add a join to $model_name |
|
| 4090 | - * and store it on $passed_in_query_info |
|
| 4091 | - * |
|
| 4092 | - * @param string $model_name |
|
| 4093 | - * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
| 4094 | - * @param string $original_query_param used to extract the relation chain between the queried |
|
| 4095 | - * model and $model_name. Eg, if we are querying Event, |
|
| 4096 | - * and are adding a join to 'Payment' with the original |
|
| 4097 | - * query param key |
|
| 4098 | - * 'Registration.Transaction.Payment.PAY_amount', we want |
|
| 4099 | - * to extract 'Registration.Transaction.Payment', in case |
|
| 4100 | - * Payment wants to add default query params so that it |
|
| 4101 | - * will know what models to prepend onto its default query |
|
| 4102 | - * params or in case it wants to rename tables (in case |
|
| 4103 | - * there are multiple joins to the same table) |
|
| 4104 | - * @return void |
|
| 4105 | - * @throws EE_Error |
|
| 4106 | - */ |
|
| 4107 | - private function _add_join_to_model( |
|
| 4108 | - $model_name, |
|
| 4109 | - EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
| 4110 | - $original_query_param |
|
| 4111 | - ) { |
|
| 4112 | - $relation_obj = $this->related_settings_for($model_name); |
|
| 4113 | - $model_relation_chain = EE_Model_Parser::extract_model_relation_chain($model_name, $original_query_param); |
|
| 4114 | - //check if the relation is HABTM, because then we're essentially doing two joins |
|
| 4115 | - //If so, join first to the JOIN table, and add its data types, and then continue as normal |
|
| 4116 | - if ($relation_obj instanceof EE_HABTM_Relation) { |
|
| 4117 | - $join_model_obj = $relation_obj->get_join_model(); |
|
| 4118 | - //replace the model specified with the join model for this relation chain, whi |
|
| 4119 | - $relation_chain_to_join_model = EE_Model_Parser::replace_model_name_with_join_model_name_in_model_relation_chain($model_name, |
|
| 4120 | - $join_model_obj->get_this_model_name(), $model_relation_chain); |
|
| 4121 | - $passed_in_query_info->merge( |
|
| 4122 | - new EE_Model_Query_Info_Carrier( |
|
| 4123 | - array($relation_chain_to_join_model => $join_model_obj->get_this_model_name()), |
|
| 4124 | - $relation_obj->get_join_to_intermediate_model_statement($relation_chain_to_join_model) |
|
| 4125 | - ) |
|
| 4126 | - ); |
|
| 4127 | - } |
|
| 4128 | - //now just join to the other table pointed to by the relation object, and add its data types |
|
| 4129 | - $passed_in_query_info->merge( |
|
| 4130 | - new EE_Model_Query_Info_Carrier( |
|
| 4131 | - array($model_relation_chain => $model_name), |
|
| 4132 | - $relation_obj->get_join_statement($model_relation_chain) |
|
| 4133 | - ) |
|
| 4134 | - ); |
|
| 4135 | - } |
|
| 4136 | - |
|
| 4137 | - |
|
| 4138 | - |
|
| 4139 | - /** |
|
| 4140 | - * Constructs SQL for where clause, like "WHERE Event.ID = 23 AND Transaction.amount > 100" etc. |
|
| 4141 | - * |
|
| 4142 | - * @param array $where_params like EEM_Base::get_all |
|
| 4143 | - * @return string of SQL |
|
| 4144 | - * @throws EE_Error |
|
| 4145 | - */ |
|
| 4146 | - private function _construct_where_clause($where_params) |
|
| 4147 | - { |
|
| 4148 | - $SQL = $this->_construct_condition_clause_recursive($where_params, ' AND '); |
|
| 4149 | - if ($SQL) { |
|
| 4150 | - return " WHERE " . $SQL; |
|
| 4151 | - } |
|
| 4152 | - return ''; |
|
| 4153 | - } |
|
| 4154 | - |
|
| 4155 | - |
|
| 4156 | - |
|
| 4157 | - /** |
|
| 4158 | - * Just like the _construct_where_clause, except prepends 'HAVING' instead of 'WHERE', |
|
| 4159 | - * and should be passed HAVING parameters, not WHERE parameters |
|
| 4160 | - * |
|
| 4161 | - * @param array $having_params |
|
| 4162 | - * @return string |
|
| 4163 | - * @throws EE_Error |
|
| 4164 | - */ |
|
| 4165 | - private function _construct_having_clause($having_params) |
|
| 4166 | - { |
|
| 4167 | - $SQL = $this->_construct_condition_clause_recursive($having_params, ' AND '); |
|
| 4168 | - if ($SQL) { |
|
| 4169 | - return " HAVING " . $SQL; |
|
| 4170 | - } |
|
| 4171 | - return ''; |
|
| 4172 | - } |
|
| 4173 | - |
|
| 4174 | - |
|
| 4175 | - /** |
|
| 4176 | - * Used for creating nested WHERE conditions. Eg "WHERE ! (Event.ID = 3 OR ( Event_Meta.meta_key = 'bob' AND |
|
| 4177 | - * Event_Meta.meta_value = 'foo'))" |
|
| 4178 | - * |
|
| 4179 | - * @param array $where_params see EEM_Base::get_all for documentation |
|
| 4180 | - * @param string $glue joins each subclause together. Should really only be " AND " or " OR "... |
|
| 4181 | - * @throws EE_Error |
|
| 4182 | - * @return string of SQL |
|
| 4183 | - */ |
|
| 4184 | - private function _construct_condition_clause_recursive($where_params, $glue = ' AND') |
|
| 4185 | - { |
|
| 4186 | - $where_clauses = array(); |
|
| 4187 | - foreach ($where_params as $query_param => $op_and_value_or_sub_condition) { |
|
| 4188 | - $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param);//str_replace("*",'',$query_param); |
|
| 4189 | - if (in_array($query_param, $this->_logic_query_param_keys)) { |
|
| 4190 | - switch ($query_param) { |
|
| 4191 | - case 'not': |
|
| 4192 | - case 'NOT': |
|
| 4193 | - $where_clauses[] = "! (" |
|
| 4194 | - . $this->_construct_condition_clause_recursive($op_and_value_or_sub_condition, |
|
| 4195 | - $glue) |
|
| 4196 | - . ")"; |
|
| 4197 | - break; |
|
| 4198 | - case 'and': |
|
| 4199 | - case 'AND': |
|
| 4200 | - $where_clauses[] = " (" |
|
| 4201 | - . $this->_construct_condition_clause_recursive($op_and_value_or_sub_condition, |
|
| 4202 | - ' AND ') |
|
| 4203 | - . ")"; |
|
| 4204 | - break; |
|
| 4205 | - case 'or': |
|
| 4206 | - case 'OR': |
|
| 4207 | - $where_clauses[] = " (" |
|
| 4208 | - . $this->_construct_condition_clause_recursive($op_and_value_or_sub_condition, |
|
| 4209 | - ' OR ') |
|
| 4210 | - . ")"; |
|
| 4211 | - break; |
|
| 4212 | - } |
|
| 4213 | - } else { |
|
| 4214 | - $field_obj = $this->_deduce_field_from_query_param($query_param); |
|
| 4215 | - //if it's not a normal field, maybe it's a custom selection? |
|
| 4216 | - if (! $field_obj) { |
|
| 4217 | - if ($this->_custom_selections instanceof CustomSelects) { |
|
| 4218 | - $field_obj = $this->_custom_selections->getDataTypeForAlias($query_param); |
|
| 4219 | - } else { |
|
| 4220 | - throw new EE_Error(sprintf(__("%s is neither a valid model field name, nor a custom selection", |
|
| 4221 | - "event_espresso"), $query_param)); |
|
| 4222 | - } |
|
| 4223 | - } |
|
| 4224 | - $op_and_value_sql = $this->_construct_op_and_value($op_and_value_or_sub_condition, $field_obj); |
|
| 4225 | - $where_clauses[] = $this->_deduce_column_name_from_query_param($query_param) . SP . $op_and_value_sql; |
|
| 4226 | - } |
|
| 4227 | - } |
|
| 4228 | - return $where_clauses ? implode($glue, $where_clauses) : ''; |
|
| 4229 | - } |
|
| 4230 | - |
|
| 4231 | - |
|
| 4232 | - |
|
| 4233 | - /** |
|
| 4234 | - * Takes the input parameter and extract the table name (alias) and column name |
|
| 4235 | - * |
|
| 4236 | - * @param string $query_param like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
| 4237 | - * @throws EE_Error |
|
| 4238 | - * @return string table alias and column name for SQL, eg "Transaction.TXN_ID" |
|
| 4239 | - */ |
|
| 4240 | - private function _deduce_column_name_from_query_param($query_param) |
|
| 4241 | - { |
|
| 4242 | - $field = $this->_deduce_field_from_query_param($query_param); |
|
| 4243 | - if ($field) { |
|
| 4244 | - $table_alias_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_from_query_param($field->get_model_name(), |
|
| 4245 | - $query_param); |
|
| 4246 | - return $table_alias_prefix . $field->get_qualified_column(); |
|
| 4247 | - } |
|
| 4248 | - if ($this->_custom_selections instanceof CustomSelects |
|
| 4249 | - && in_array($query_param, $this->_custom_selections->columnAliases(), true) |
|
| 4250 | - ) { |
|
| 4251 | - //maybe it's custom selection item? |
|
| 4252 | - //if so, just use it as the "column name" |
|
| 4253 | - return $query_param; |
|
| 4254 | - } |
|
| 4255 | - $custom_select_aliases = $this->_custom_selections instanceof CustomSelects |
|
| 4256 | - ? implode(',', $this->_custom_selections->columnAliases()) |
|
| 4257 | - : ''; |
|
| 4258 | - throw new EE_Error( |
|
| 4259 | - sprintf( |
|
| 4260 | - __( |
|
| 4261 | - "%s is not a valid field on this model, nor a custom selection (%s)", |
|
| 4262 | - "event_espresso" |
|
| 4263 | - ), $query_param, $custom_select_aliases |
|
| 4264 | - ) |
|
| 4265 | - ); |
|
| 4266 | - } |
|
| 4267 | - |
|
| 4268 | - |
|
| 4269 | - |
|
| 4270 | - /** |
|
| 4271 | - * Removes the * and anything after it from the condition query param key. It is useful to add the * to condition |
|
| 4272 | - * query param keys (eg, 'OR*', 'EVT_ID') in order for the array keys to still be unique, so that they don't get |
|
| 4273 | - * overwritten Takes a string like 'Event.EVT_ID*', 'TXN_total**', 'OR*1st', and 'DTT_reg_start*foobar' to |
|
| 4274 | - * 'Event.EVT_ID', 'TXN_total', 'OR', and 'DTT_reg_start', respectively. |
|
| 4275 | - * |
|
| 4276 | - * @param string $condition_query_param_key |
|
| 4277 | - * @return string |
|
| 4278 | - */ |
|
| 4279 | - private function _remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
| 4280 | - { |
|
| 4281 | - $pos_of_star = strpos($condition_query_param_key, '*'); |
|
| 4282 | - if ($pos_of_star === false) { |
|
| 4283 | - return $condition_query_param_key; |
|
| 4284 | - } |
|
| 4285 | - $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
| 4286 | - return $condition_query_param_sans_star; |
|
| 4287 | - } |
|
| 4288 | - |
|
| 4289 | - |
|
| 4290 | - |
|
| 4291 | - /** |
|
| 4292 | - * creates the SQL for the operator and the value in a WHERE clause, eg "< 23" or "LIKE '%monkey%'" |
|
| 4293 | - * |
|
| 4294 | - * @param mixed array | string $op_and_value |
|
| 4295 | - * @param EE_Model_Field_Base|string $field_obj . If string, should be one of EEM_Base::_valid_wpdb_data_types |
|
| 4296 | - * @throws EE_Error |
|
| 4297 | - * @return string |
|
| 4298 | - */ |
|
| 4299 | - private function _construct_op_and_value($op_and_value, $field_obj) |
|
| 4300 | - { |
|
| 4301 | - if (is_array($op_and_value)) { |
|
| 4302 | - $operator = isset($op_and_value[0]) ? $this->_prepare_operator_for_sql($op_and_value[0]) : null; |
|
| 4303 | - if (! $operator) { |
|
| 4304 | - $php_array_like_string = array(); |
|
| 4305 | - foreach ($op_and_value as $key => $value) { |
|
| 4306 | - $php_array_like_string[] = "$key=>$value"; |
|
| 4307 | - } |
|
| 4308 | - throw new EE_Error( |
|
| 4309 | - sprintf( |
|
| 4310 | - __( |
|
| 4311 | - "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))", |
|
| 4312 | - "event_espresso" |
|
| 4313 | - ), |
|
| 4314 | - implode(",", $php_array_like_string) |
|
| 4315 | - ) |
|
| 4316 | - ); |
|
| 4317 | - } |
|
| 4318 | - $value = isset($op_and_value[1]) ? $op_and_value[1] : null; |
|
| 4319 | - } else { |
|
| 4320 | - $operator = '='; |
|
| 4321 | - $value = $op_and_value; |
|
| 4322 | - } |
|
| 4323 | - //check to see if the value is actually another field |
|
| 4324 | - if (is_array($op_and_value) && isset($op_and_value[2]) && $op_and_value[2] == true) { |
|
| 4325 | - return $operator . SP . $this->_deduce_column_name_from_query_param($value); |
|
| 4326 | - } |
|
| 4327 | - if (in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
| 4328 | - //in this case, the value should be an array, or at least a comma-separated list |
|
| 4329 | - //it will need to handle a little differently |
|
| 4330 | - $cleaned_value = $this->_construct_in_value($value, $field_obj); |
|
| 4331 | - //note: $cleaned_value has already been run through $wpdb->prepare() |
|
| 4332 | - return $operator . SP . $cleaned_value; |
|
| 4333 | - } |
|
| 4334 | - if (in_array($operator, $this->valid_between_style_operators()) && is_array($value)) { |
|
| 4335 | - //the value should be an array with count of two. |
|
| 4336 | - if (count($value) !== 2) { |
|
| 4337 | - throw new EE_Error( |
|
| 4338 | - sprintf( |
|
| 4339 | - __( |
|
| 4340 | - "The '%s' operator must be used with an array of values and there must be exactly TWO values in that array.", |
|
| 4341 | - 'event_espresso' |
|
| 4342 | - ), |
|
| 4343 | - "BETWEEN" |
|
| 4344 | - ) |
|
| 4345 | - ); |
|
| 4346 | - } |
|
| 4347 | - $cleaned_value = $this->_construct_between_value($value, $field_obj); |
|
| 4348 | - return $operator . SP . $cleaned_value; |
|
| 4349 | - } |
|
| 4350 | - if (in_array($operator, $this->valid_null_style_operators())) { |
|
| 4351 | - if ($value !== null) { |
|
| 4352 | - throw new EE_Error( |
|
| 4353 | - sprintf( |
|
| 4354 | - __( |
|
| 4355 | - "You attempted to give a value (%s) while using a NULL-style operator (%s). That isn't valid", |
|
| 4356 | - "event_espresso" |
|
| 4357 | - ), |
|
| 4358 | - $value, |
|
| 4359 | - $operator |
|
| 4360 | - ) |
|
| 4361 | - ); |
|
| 4362 | - } |
|
| 4363 | - return $operator; |
|
| 4364 | - } |
|
| 4365 | - if (in_array($operator, $this->valid_like_style_operators()) && ! is_array($value)) { |
|
| 4366 | - //if the operator is 'LIKE', we want to allow percent signs (%) and not |
|
| 4367 | - //remove other junk. So just treat it as a string. |
|
| 4368 | - return $operator . SP . $this->_wpdb_prepare_using_field($value, '%s'); |
|
| 4369 | - } |
|
| 4370 | - if (! in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
| 4371 | - return $operator . SP . $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4372 | - } |
|
| 4373 | - if (in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
| 4374 | - throw new EE_Error( |
|
| 4375 | - sprintf( |
|
| 4376 | - __( |
|
| 4377 | - "Operator '%s' must be used with an array of values, eg 'Registration.REG_ID' => array('%s',array(1,2,3))", |
|
| 4378 | - 'event_espresso' |
|
| 4379 | - ), |
|
| 4380 | - $operator, |
|
| 4381 | - $operator |
|
| 4382 | - ) |
|
| 4383 | - ); |
|
| 4384 | - } |
|
| 4385 | - if (! in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
| 4386 | - throw new EE_Error( |
|
| 4387 | - sprintf( |
|
| 4388 | - __( |
|
| 4389 | - "Operator '%s' must be used with a single value, not an array. Eg 'Registration.REG_ID => array('%s',23))", |
|
| 4390 | - 'event_espresso' |
|
| 4391 | - ), |
|
| 4392 | - $operator, |
|
| 4393 | - $operator |
|
| 4394 | - ) |
|
| 4395 | - ); |
|
| 4396 | - } |
|
| 4397 | - throw new EE_Error( |
|
| 4398 | - sprintf( |
|
| 4399 | - __( |
|
| 4400 | - "It appears you've provided some totally invalid query parameters. Operator and value were:'%s', which isn't right at all", |
|
| 4401 | - "event_espresso" |
|
| 4402 | - ), |
|
| 4403 | - http_build_query($op_and_value) |
|
| 4404 | - ) |
|
| 4405 | - ); |
|
| 4406 | - } |
|
| 4407 | - |
|
| 4408 | - |
|
| 4409 | - |
|
| 4410 | - /** |
|
| 4411 | - * Creates the operands to be used in a BETWEEN query, eg "'2014-12-31 20:23:33' AND '2015-01-23 12:32:54'" |
|
| 4412 | - * |
|
| 4413 | - * @param array $values |
|
| 4414 | - * @param EE_Model_Field_Base|string $field_obj if string, it should be the datatype to be used when querying, eg |
|
| 4415 | - * '%s' |
|
| 4416 | - * @return string |
|
| 4417 | - * @throws EE_Error |
|
| 4418 | - */ |
|
| 4419 | - public function _construct_between_value($values, $field_obj) |
|
| 4420 | - { |
|
| 4421 | - $cleaned_values = array(); |
|
| 4422 | - foreach ($values as $value) { |
|
| 4423 | - $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4424 | - } |
|
| 4425 | - return $cleaned_values[0] . " AND " . $cleaned_values[1]; |
|
| 4426 | - } |
|
| 4427 | - |
|
| 4428 | - |
|
| 4429 | - |
|
| 4430 | - /** |
|
| 4431 | - * Takes an array or a comma-separated list of $values and cleans them |
|
| 4432 | - * according to $data_type using $wpdb->prepare, and then makes the list a |
|
| 4433 | - * string surrounded by ( and ). Eg, _construct_in_value(array(1,2,3),'%d') would |
|
| 4434 | - * return '(1,2,3)'; _construct_in_value("1,2,hack",'%d') would return '(1,2,1)' (assuming |
|
| 4435 | - * I'm right that a string, when interpreted as a digit, becomes a 1. It might become a 0) |
|
| 4436 | - * |
|
| 4437 | - * @param mixed $values array or comma-separated string |
|
| 4438 | - * @param EE_Model_Field_Base|string $field_obj if string, it should be a wpdb data type like '%s', or '%d' |
|
| 4439 | - * @return string of SQL to follow an 'IN' or 'NOT IN' operator |
|
| 4440 | - * @throws EE_Error |
|
| 4441 | - */ |
|
| 4442 | - public function _construct_in_value($values, $field_obj) |
|
| 4443 | - { |
|
| 4444 | - //check if the value is a CSV list |
|
| 4445 | - if (is_string($values)) { |
|
| 4446 | - //in which case, turn it into an array |
|
| 4447 | - $values = explode(",", $values); |
|
| 4448 | - } |
|
| 4449 | - $cleaned_values = array(); |
|
| 4450 | - foreach ($values as $value) { |
|
| 4451 | - $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4452 | - } |
|
| 4453 | - //we would just LOVE to leave $cleaned_values as an empty array, and return the value as "()", |
|
| 4454 | - //but unfortunately that's invalid SQL. So instead we return a string which we KNOW will evaluate to be the empty set |
|
| 4455 | - //which is effectively equivalent to returning "()". We don't return "(0)" because that only works for auto-incrementing columns |
|
| 4456 | - if (empty($cleaned_values)) { |
|
| 4457 | - $all_fields = $this->field_settings(); |
|
| 4458 | - $a_field = array_shift($all_fields); |
|
| 4459 | - $main_table = $this->_get_main_table(); |
|
| 4460 | - $cleaned_values[] = "SELECT " |
|
| 4461 | - . $a_field->get_table_column() |
|
| 4462 | - . " FROM " |
|
| 4463 | - . $main_table->get_table_name() |
|
| 4464 | - . " WHERE FALSE"; |
|
| 4465 | - } |
|
| 4466 | - return "(" . implode(",", $cleaned_values) . ")"; |
|
| 4467 | - } |
|
| 4468 | - |
|
| 4469 | - |
|
| 4470 | - |
|
| 4471 | - /** |
|
| 4472 | - * @param mixed $value |
|
| 4473 | - * @param EE_Model_Field_Base|string $field_obj if string it should be a wpdb data type like '%d' |
|
| 4474 | - * @throws EE_Error |
|
| 4475 | - * @return false|null|string |
|
| 4476 | - */ |
|
| 4477 | - private function _wpdb_prepare_using_field($value, $field_obj) |
|
| 4478 | - { |
|
| 4479 | - /** @type WPDB $wpdb */ |
|
| 4480 | - global $wpdb; |
|
| 4481 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 4482 | - return $wpdb->prepare($field_obj->get_wpdb_data_type(), |
|
| 4483 | - $this->_prepare_value_for_use_in_db($value, $field_obj)); |
|
| 4484 | - } //$field_obj should really just be a data type |
|
| 4485 | - if (! in_array($field_obj, $this->_valid_wpdb_data_types)) { |
|
| 4486 | - throw new EE_Error( |
|
| 4487 | - sprintf( |
|
| 4488 | - __("%s is not a valid wpdb datatype. Valid ones are %s", "event_espresso"), |
|
| 4489 | - $field_obj, implode(",", $this->_valid_wpdb_data_types) |
|
| 4490 | - ) |
|
| 4491 | - ); |
|
| 4492 | - } |
|
| 4493 | - return $wpdb->prepare($field_obj, $value); |
|
| 4494 | - } |
|
| 4495 | - |
|
| 4496 | - |
|
| 4497 | - |
|
| 4498 | - /** |
|
| 4499 | - * Takes the input parameter and finds the model field that it indicates. |
|
| 4500 | - * |
|
| 4501 | - * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
| 4502 | - * @throws EE_Error |
|
| 4503 | - * @return EE_Model_Field_Base |
|
| 4504 | - */ |
|
| 4505 | - protected function _deduce_field_from_query_param($query_param_name) |
|
| 4506 | - { |
|
| 4507 | - //ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
| 4508 | - //which will help us find the database table and column |
|
| 4509 | - $query_param_parts = explode(".", $query_param_name); |
|
| 4510 | - if (empty($query_param_parts)) { |
|
| 4511 | - throw new EE_Error(sprintf(__("_extract_column_name is empty when trying to extract column and table name from %s", |
|
| 4512 | - 'event_espresso'), $query_param_name)); |
|
| 4513 | - } |
|
| 4514 | - $number_of_parts = count($query_param_parts); |
|
| 4515 | - $last_query_param_part = $query_param_parts[count($query_param_parts) - 1]; |
|
| 4516 | - if ($number_of_parts === 1) { |
|
| 4517 | - $field_name = $last_query_param_part; |
|
| 4518 | - $model_obj = $this; |
|
| 4519 | - } else {// $number_of_parts >= 2 |
|
| 4520 | - //the last part is the column name, and there are only 2parts. therefore... |
|
| 4521 | - $field_name = $last_query_param_part; |
|
| 4522 | - $model_obj = $this->get_related_model_obj($query_param_parts[$number_of_parts - 2]); |
|
| 4523 | - } |
|
| 4524 | - try { |
|
| 4525 | - return $model_obj->field_settings_for($field_name); |
|
| 4526 | - } catch (EE_Error $e) { |
|
| 4527 | - return null; |
|
| 4528 | - } |
|
| 4529 | - } |
|
| 4530 | - |
|
| 4531 | - |
|
| 4532 | - |
|
| 4533 | - /** |
|
| 4534 | - * Given a field's name (ie, a key in $this->field_settings()), uses the EE_Model_Field object to get the table's |
|
| 4535 | - * alias and column which corresponds to it |
|
| 4536 | - * |
|
| 4537 | - * @param string $field_name |
|
| 4538 | - * @throws EE_Error |
|
| 4539 | - * @return string |
|
| 4540 | - */ |
|
| 4541 | - public function _get_qualified_column_for_field($field_name) |
|
| 4542 | - { |
|
| 4543 | - $all_fields = $this->field_settings(); |
|
| 4544 | - $field = isset($all_fields[$field_name]) ? $all_fields[$field_name] : false; |
|
| 4545 | - if ($field) { |
|
| 4546 | - return $field->get_qualified_column(); |
|
| 4547 | - } |
|
| 4548 | - throw new EE_Error( |
|
| 4549 | - sprintf( |
|
| 4550 | - __( |
|
| 4551 | - "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.", |
|
| 4552 | - 'event_espresso' |
|
| 4553 | - ), $field_name, get_class($this) |
|
| 4554 | - ) |
|
| 4555 | - ); |
|
| 4556 | - } |
|
| 4557 | - |
|
| 4558 | - |
|
| 4559 | - |
|
| 4560 | - /** |
|
| 4561 | - * similar to \EEM_Base::_get_qualified_column_for_field() but returns an array with data for ALL fields. |
|
| 4562 | - * Example usage: |
|
| 4563 | - * EEM_Ticket::instance()->get_all_wpdb_results( |
|
| 4564 | - * array(), |
|
| 4565 | - * ARRAY_A, |
|
| 4566 | - * EEM_Ticket::instance()->get_qualified_columns_for_all_fields() |
|
| 4567 | - * ); |
|
| 4568 | - * is equivalent to |
|
| 4569 | - * EEM_Ticket::instance()->get_all_wpdb_results( array(), ARRAY_A, '*' ); |
|
| 4570 | - * and |
|
| 4571 | - * EEM_Event::instance()->get_all_wpdb_results( |
|
| 4572 | - * array( |
|
| 4573 | - * array( |
|
| 4574 | - * 'Datetime.Ticket.TKT_ID' => array( '<', 100 ), |
|
| 4575 | - * ), |
|
| 4576 | - * ARRAY_A, |
|
| 4577 | - * implode( |
|
| 4578 | - * ', ', |
|
| 4579 | - * array_merge( |
|
| 4580 | - * EEM_Event::instance()->get_qualified_columns_for_all_fields( '', false ), |
|
| 4581 | - * EEM_Ticket::instance()->get_qualified_columns_for_all_fields( 'Datetime', false ) |
|
| 4582 | - * ) |
|
| 4583 | - * ) |
|
| 4584 | - * ) |
|
| 4585 | - * ); |
|
| 4586 | - * selects rows from the database, selecting all the event and ticket columns, where the ticket ID is below 100 |
|
| 4587 | - * |
|
| 4588 | - * @param string $model_relation_chain the chain of models used to join between the model you want to query |
|
| 4589 | - * and the one whose fields you are selecting for example: when querying |
|
| 4590 | - * tickets model and selecting fields from the tickets model you would |
|
| 4591 | - * leave this parameter empty, because no models are needed to join |
|
| 4592 | - * between the queried model and the selected one. Likewise when |
|
| 4593 | - * querying the datetime model and selecting fields from the tickets |
|
| 4594 | - * model, it would also be left empty, because there is a direct |
|
| 4595 | - * relation from datetimes to tickets, so no model is needed to join |
|
| 4596 | - * them together. However, when querying from the event model and |
|
| 4597 | - * selecting fields from the ticket model, you should provide the string |
|
| 4598 | - * 'Datetime', indicating that the event model must first join to the |
|
| 4599 | - * datetime model in order to find its relation to ticket model. |
|
| 4600 | - * Also, when querying from the venue model and selecting fields from |
|
| 4601 | - * the ticket model, you should provide the string 'Event.Datetime', |
|
| 4602 | - * indicating you need to join the venue model to the event model, |
|
| 4603 | - * to the datetime model, in order to find its relation to the ticket model. |
|
| 4604 | - * This string is used to deduce the prefix that gets added onto the |
|
| 4605 | - * models' tables qualified columns |
|
| 4606 | - * @param bool $return_string if true, will return a string with qualified column names separated |
|
| 4607 | - * by ', ' if false, will simply return a numerically indexed array of |
|
| 4608 | - * qualified column names |
|
| 4609 | - * @return array|string |
|
| 4610 | - */ |
|
| 4611 | - public function get_qualified_columns_for_all_fields($model_relation_chain = '', $return_string = true) |
|
| 4612 | - { |
|
| 4613 | - $table_prefix = str_replace('.', '__', $model_relation_chain) . (empty($model_relation_chain) ? '' : '__'); |
|
| 4614 | - $qualified_columns = array(); |
|
| 4615 | - foreach ($this->field_settings() as $field_name => $field) { |
|
| 4616 | - $qualified_columns[] = $table_prefix . $field->get_qualified_column(); |
|
| 4617 | - } |
|
| 4618 | - return $return_string ? implode(', ', $qualified_columns) : $qualified_columns; |
|
| 4619 | - } |
|
| 4620 | - |
|
| 4621 | - |
|
| 4622 | - |
|
| 4623 | - /** |
|
| 4624 | - * constructs the select use on special limit joins |
|
| 4625 | - * NOTE: for now this has only been tested and will work when the table alias is for the PRIMARY table. Although |
|
| 4626 | - * its setup so the select query will be setup on and just doing the special select join off of the primary table |
|
| 4627 | - * (as that is typically where the limits would be set). |
|
| 4628 | - * |
|
| 4629 | - * @param string $table_alias The table the select is being built for |
|
| 4630 | - * @param mixed|string $limit The limit for this select |
|
| 4631 | - * @return string The final select join element for the query. |
|
| 4632 | - */ |
|
| 4633 | - public function _construct_limit_join_select($table_alias, $limit) |
|
| 4634 | - { |
|
| 4635 | - $SQL = ''; |
|
| 4636 | - foreach ($this->_tables as $table_obj) { |
|
| 4637 | - if ($table_obj instanceof EE_Primary_Table) { |
|
| 4638 | - $SQL .= $table_alias === $table_obj->get_table_alias() |
|
| 4639 | - ? $table_obj->get_select_join_limit($limit) |
|
| 4640 | - : SP . $table_obj->get_table_name() . " AS " . $table_obj->get_table_alias() . SP; |
|
| 4641 | - } elseif ($table_obj instanceof EE_Secondary_Table) { |
|
| 4642 | - $SQL .= $table_alias === $table_obj->get_table_alias() |
|
| 4643 | - ? $table_obj->get_select_join_limit_join($limit) |
|
| 4644 | - : SP . $table_obj->get_join_sql($table_alias) . SP; |
|
| 4645 | - } |
|
| 4646 | - } |
|
| 4647 | - return $SQL; |
|
| 4648 | - } |
|
| 4649 | - |
|
| 4650 | - |
|
| 4651 | - |
|
| 4652 | - /** |
|
| 4653 | - * Constructs the internal join if there are multiple tables, or simply the table's name and alias |
|
| 4654 | - * Eg "wp_post AS Event" or "wp_post AS Event INNER JOIN wp_postmeta Event_Meta ON Event.ID = Event_Meta.post_id" |
|
| 4655 | - * |
|
| 4656 | - * @return string SQL |
|
| 4657 | - * @throws EE_Error |
|
| 4658 | - */ |
|
| 4659 | - public function _construct_internal_join() |
|
| 4660 | - { |
|
| 4661 | - $SQL = $this->_get_main_table()->get_table_sql(); |
|
| 4662 | - $SQL .= $this->_construct_internal_join_to_table_with_alias($this->_get_main_table()->get_table_alias()); |
|
| 4663 | - return $SQL; |
|
| 4664 | - } |
|
| 4665 | - |
|
| 4666 | - |
|
| 4667 | - |
|
| 4668 | - /** |
|
| 4669 | - * Constructs the SQL for joining all the tables on this model. |
|
| 4670 | - * Normally $alias should be the primary table's alias, but in cases where |
|
| 4671 | - * we have already joined to a secondary table (eg, the secondary table has a foreign key and is joined before the |
|
| 4672 | - * primary table) then we should provide that secondary table's alias. Eg, with $alias being the primary table's |
|
| 4673 | - * alias, this will construct SQL like: |
|
| 4674 | - * " INNER JOIN wp_esp_secondary_table AS Secondary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
| 4675 | - * With $alias being a secondary table's alias, this will construct SQL like: |
|
| 4676 | - * " INNER JOIN wp_esp_primary_table AS Primary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
| 4677 | - * |
|
| 4678 | - * @param string $alias_prefixed table alias to join to (this table should already be in the FROM SQL clause) |
|
| 4679 | - * @return string |
|
| 4680 | - */ |
|
| 4681 | - public function _construct_internal_join_to_table_with_alias($alias_prefixed) |
|
| 4682 | - { |
|
| 4683 | - $SQL = ''; |
|
| 4684 | - $alias_sans_prefix = EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($alias_prefixed); |
|
| 4685 | - foreach ($this->_tables as $table_obj) { |
|
| 4686 | - if ($table_obj instanceof EE_Secondary_Table) {//table is secondary table |
|
| 4687 | - if ($alias_sans_prefix === $table_obj->get_table_alias()) { |
|
| 4688 | - //so we're joining to this table, meaning the table is already in |
|
| 4689 | - //the FROM statement, BUT the primary table isn't. So we want |
|
| 4690 | - //to add the inverse join sql |
|
| 4691 | - $SQL .= $table_obj->get_inverse_join_sql($alias_prefixed); |
|
| 4692 | - } else { |
|
| 4693 | - //just add a regular JOIN to this table from the primary table |
|
| 4694 | - $SQL .= $table_obj->get_join_sql($alias_prefixed); |
|
| 4695 | - } |
|
| 4696 | - }//if it's a primary table, dont add any SQL. it should already be in the FROM statement |
|
| 4697 | - } |
|
| 4698 | - return $SQL; |
|
| 4699 | - } |
|
| 4700 | - |
|
| 4701 | - |
|
| 4702 | - |
|
| 4703 | - /** |
|
| 4704 | - * Gets an array for storing all the data types on the next-to-be-executed-query. |
|
| 4705 | - * This should be a growing array of keys being table-columns (eg 'EVT_ID' and 'Event.EVT_ID'), and values being |
|
| 4706 | - * their data type (eg, '%s', '%d', etc) |
|
| 4707 | - * |
|
| 4708 | - * @return array |
|
| 4709 | - */ |
|
| 4710 | - public function _get_data_types() |
|
| 4711 | - { |
|
| 4712 | - $data_types = array(); |
|
| 4713 | - foreach ($this->field_settings() as $field_obj) { |
|
| 4714 | - //$data_types[$field_obj->get_table_column()] = $field_obj->get_wpdb_data_type(); |
|
| 4715 | - /** @var $field_obj EE_Model_Field_Base */ |
|
| 4716 | - $data_types[$field_obj->get_qualified_column()] = $field_obj->get_wpdb_data_type(); |
|
| 4717 | - } |
|
| 4718 | - return $data_types; |
|
| 4719 | - } |
|
| 4720 | - |
|
| 4721 | - |
|
| 4722 | - |
|
| 4723 | - /** |
|
| 4724 | - * Gets the model object given the relation's name / model's name (eg, 'Event', 'Registration',etc. Always singular) |
|
| 4725 | - * |
|
| 4726 | - * @param string $model_name |
|
| 4727 | - * @throws EE_Error |
|
| 4728 | - * @return EEM_Base |
|
| 4729 | - */ |
|
| 4730 | - public function get_related_model_obj($model_name) |
|
| 4731 | - { |
|
| 4732 | - $model_classname = "EEM_" . $model_name; |
|
| 4733 | - if (! class_exists($model_classname)) { |
|
| 4734 | - throw new EE_Error(sprintf(__("You specified a related model named %s in your query. No such model exists, if it did, it would have the classname %s", |
|
| 4735 | - 'event_espresso'), $model_name, $model_classname)); |
|
| 4736 | - } |
|
| 4737 | - return call_user_func($model_classname . "::instance"); |
|
| 4738 | - } |
|
| 4739 | - |
|
| 4740 | - |
|
| 4741 | - |
|
| 4742 | - /** |
|
| 4743 | - * Returns the array of EE_ModelRelations for this model. |
|
| 4744 | - * |
|
| 4745 | - * @return EE_Model_Relation_Base[] |
|
| 4746 | - */ |
|
| 4747 | - public function relation_settings() |
|
| 4748 | - { |
|
| 4749 | - return $this->_model_relations; |
|
| 4750 | - } |
|
| 4751 | - |
|
| 4752 | - |
|
| 4753 | - |
|
| 4754 | - /** |
|
| 4755 | - * Gets all related models that this model BELONGS TO. Handy to know sometimes |
|
| 4756 | - * because without THOSE models, this model probably doesn't have much purpose. |
|
| 4757 | - * (Eg, without an event, datetimes have little purpose.) |
|
| 4758 | - * |
|
| 4759 | - * @return EE_Belongs_To_Relation[] |
|
| 4760 | - */ |
|
| 4761 | - public function belongs_to_relations() |
|
| 4762 | - { |
|
| 4763 | - $belongs_to_relations = array(); |
|
| 4764 | - foreach ($this->relation_settings() as $model_name => $relation_obj) { |
|
| 4765 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 4766 | - $belongs_to_relations[$model_name] = $relation_obj; |
|
| 4767 | - } |
|
| 4768 | - } |
|
| 4769 | - return $belongs_to_relations; |
|
| 4770 | - } |
|
| 4771 | - |
|
| 4772 | - |
|
| 4773 | - |
|
| 4774 | - /** |
|
| 4775 | - * Returns the specified EE_Model_Relation, or throws an exception |
|
| 4776 | - * |
|
| 4777 | - * @param string $relation_name name of relation, key in $this->_relatedModels |
|
| 4778 | - * @throws EE_Error |
|
| 4779 | - * @return EE_Model_Relation_Base |
|
| 4780 | - */ |
|
| 4781 | - public function related_settings_for($relation_name) |
|
| 4782 | - { |
|
| 4783 | - $relatedModels = $this->relation_settings(); |
|
| 4784 | - if (! array_key_exists($relation_name, $relatedModels)) { |
|
| 4785 | - throw new EE_Error( |
|
| 4786 | - sprintf( |
|
| 4787 | - __('Cannot get %s related to %s. There is no model relation of that type. There is, however, %s...', |
|
| 4788 | - 'event_espresso'), |
|
| 4789 | - $relation_name, |
|
| 4790 | - $this->_get_class_name(), |
|
| 4791 | - implode(', ', array_keys($relatedModels)) |
|
| 4792 | - ) |
|
| 4793 | - ); |
|
| 4794 | - } |
|
| 4795 | - return $relatedModels[$relation_name]; |
|
| 4796 | - } |
|
| 4797 | - |
|
| 4798 | - |
|
| 4799 | - |
|
| 4800 | - /** |
|
| 4801 | - * A convenience method for getting a specific field's settings, instead of getting all field settings for all |
|
| 4802 | - * fields |
|
| 4803 | - * |
|
| 4804 | - * @param string $fieldName |
|
| 4805 | - * @param boolean $include_db_only_fields |
|
| 4806 | - * @throws EE_Error |
|
| 4807 | - * @return EE_Model_Field_Base |
|
| 4808 | - */ |
|
| 4809 | - public function field_settings_for($fieldName, $include_db_only_fields = true) |
|
| 4810 | - { |
|
| 4811 | - $fieldSettings = $this->field_settings($include_db_only_fields); |
|
| 4812 | - if (! array_key_exists($fieldName, $fieldSettings)) { |
|
| 4813 | - throw new EE_Error(sprintf(__("There is no field/column '%s' on '%s'", 'event_espresso'), $fieldName, |
|
| 4814 | - get_class($this))); |
|
| 4815 | - } |
|
| 4816 | - return $fieldSettings[$fieldName]; |
|
| 4817 | - } |
|
| 4818 | - |
|
| 4819 | - |
|
| 4820 | - |
|
| 4821 | - /** |
|
| 4822 | - * Checks if this field exists on this model |
|
| 4823 | - * |
|
| 4824 | - * @param string $fieldName a key in the model's _field_settings array |
|
| 4825 | - * @return boolean |
|
| 4826 | - */ |
|
| 4827 | - public function has_field($fieldName) |
|
| 4828 | - { |
|
| 4829 | - $fieldSettings = $this->field_settings(true); |
|
| 4830 | - if (isset($fieldSettings[$fieldName])) { |
|
| 4831 | - return true; |
|
| 4832 | - } |
|
| 4833 | - return false; |
|
| 4834 | - } |
|
| 4835 | - |
|
| 4836 | - |
|
| 4837 | - |
|
| 4838 | - /** |
|
| 4839 | - * Returns whether or not this model has a relation to the specified model |
|
| 4840 | - * |
|
| 4841 | - * @param string $relation_name possibly one of the keys in the relation_settings array |
|
| 4842 | - * @return boolean |
|
| 4843 | - */ |
|
| 4844 | - public function has_relation($relation_name) |
|
| 4845 | - { |
|
| 4846 | - $relations = $this->relation_settings(); |
|
| 4847 | - if (isset($relations[$relation_name])) { |
|
| 4848 | - return true; |
|
| 4849 | - } |
|
| 4850 | - return false; |
|
| 4851 | - } |
|
| 4852 | - |
|
| 4853 | - |
|
| 4854 | - |
|
| 4855 | - /** |
|
| 4856 | - * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
| 4857 | - * Eg, on EE_Answer that would be ANS_ID field object |
|
| 4858 | - * |
|
| 4859 | - * @param $field_obj |
|
| 4860 | - * @return boolean |
|
| 4861 | - */ |
|
| 4862 | - public function is_primary_key_field($field_obj) |
|
| 4863 | - { |
|
| 4864 | - return $field_obj instanceof EE_Primary_Key_Field_Base ? true : false; |
|
| 4865 | - } |
|
| 4866 | - |
|
| 4867 | - |
|
| 4868 | - |
|
| 4869 | - /** |
|
| 4870 | - * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
| 4871 | - * Eg, on EE_Answer that would be ANS_ID field object |
|
| 4872 | - * |
|
| 4873 | - * @return EE_Model_Field_Base |
|
| 4874 | - * @throws EE_Error |
|
| 4875 | - */ |
|
| 4876 | - public function get_primary_key_field() |
|
| 4877 | - { |
|
| 4878 | - if ($this->_primary_key_field === null) { |
|
| 4879 | - foreach ($this->field_settings(true) as $field_obj) { |
|
| 4880 | - if ($this->is_primary_key_field($field_obj)) { |
|
| 4881 | - $this->_primary_key_field = $field_obj; |
|
| 4882 | - break; |
|
| 4883 | - } |
|
| 4884 | - } |
|
| 4885 | - if (! $this->_primary_key_field instanceof EE_Primary_Key_Field_Base) { |
|
| 4886 | - throw new EE_Error(sprintf(__("There is no Primary Key defined on model %s", 'event_espresso'), |
|
| 4887 | - get_class($this))); |
|
| 4888 | - } |
|
| 4889 | - } |
|
| 4890 | - return $this->_primary_key_field; |
|
| 4891 | - } |
|
| 4892 | - |
|
| 4893 | - |
|
| 4894 | - |
|
| 4895 | - /** |
|
| 4896 | - * Returns whether or not not there is a primary key on this model. |
|
| 4897 | - * Internally does some caching. |
|
| 4898 | - * |
|
| 4899 | - * @return boolean |
|
| 4900 | - */ |
|
| 4901 | - public function has_primary_key_field() |
|
| 4902 | - { |
|
| 4903 | - if ($this->_has_primary_key_field === null) { |
|
| 4904 | - try { |
|
| 4905 | - $this->get_primary_key_field(); |
|
| 4906 | - $this->_has_primary_key_field = true; |
|
| 4907 | - } catch (EE_Error $e) { |
|
| 4908 | - $this->_has_primary_key_field = false; |
|
| 4909 | - } |
|
| 4910 | - } |
|
| 4911 | - return $this->_has_primary_key_field; |
|
| 4912 | - } |
|
| 4913 | - |
|
| 4914 | - |
|
| 4915 | - |
|
| 4916 | - /** |
|
| 4917 | - * Finds the first field of type $field_class_name. |
|
| 4918 | - * |
|
| 4919 | - * @param string $field_class_name class name of field that you want to find. Eg, EE_Datetime_Field, |
|
| 4920 | - * EE_Foreign_Key_Field, etc |
|
| 4921 | - * @return EE_Model_Field_Base or null if none is found |
|
| 4922 | - */ |
|
| 4923 | - public function get_a_field_of_type($field_class_name) |
|
| 4924 | - { |
|
| 4925 | - foreach ($this->field_settings() as $field) { |
|
| 4926 | - if ($field instanceof $field_class_name) { |
|
| 4927 | - return $field; |
|
| 4928 | - } |
|
| 4929 | - } |
|
| 4930 | - return null; |
|
| 4931 | - } |
|
| 4932 | - |
|
| 4933 | - |
|
| 4934 | - |
|
| 4935 | - /** |
|
| 4936 | - * Gets a foreign key field pointing to model. |
|
| 4937 | - * |
|
| 4938 | - * @param string $model_name eg Event, Registration, not EEM_Event |
|
| 4939 | - * @return EE_Foreign_Key_Field_Base |
|
| 4940 | - * @throws EE_Error |
|
| 4941 | - */ |
|
| 4942 | - public function get_foreign_key_to($model_name) |
|
| 4943 | - { |
|
| 4944 | - if (! isset($this->_cache_foreign_key_to_fields[$model_name])) { |
|
| 4945 | - foreach ($this->field_settings() as $field) { |
|
| 4946 | - if ( |
|
| 4947 | - $field instanceof EE_Foreign_Key_Field_Base |
|
| 4948 | - && in_array($model_name, $field->get_model_names_pointed_to()) |
|
| 4949 | - ) { |
|
| 4950 | - $this->_cache_foreign_key_to_fields[$model_name] = $field; |
|
| 4951 | - break; |
|
| 4952 | - } |
|
| 4953 | - } |
|
| 4954 | - if (! isset($this->_cache_foreign_key_to_fields[$model_name])) { |
|
| 4955 | - throw new EE_Error(sprintf(__("There is no foreign key field pointing to model %s on model %s", |
|
| 4956 | - 'event_espresso'), $model_name, get_class($this))); |
|
| 4957 | - } |
|
| 4958 | - } |
|
| 4959 | - return $this->_cache_foreign_key_to_fields[$model_name]; |
|
| 4960 | - } |
|
| 4961 | - |
|
| 4962 | - |
|
| 4963 | - |
|
| 4964 | - /** |
|
| 4965 | - * Gets the table name (including $wpdb->prefix) for the table alias |
|
| 4966 | - * |
|
| 4967 | - * @param string $table_alias eg Event, Event_Meta, Registration, Transaction, but maybe |
|
| 4968 | - * a table alias with a model chain prefix, like 'Venue__Event_Venue___Event_Meta'. |
|
| 4969 | - * Either one works |
|
| 4970 | - * @return string |
|
| 4971 | - */ |
|
| 4972 | - public function get_table_for_alias($table_alias) |
|
| 4973 | - { |
|
| 4974 | - $table_alias_sans_model_relation_chain_prefix = EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($table_alias); |
|
| 4975 | - return $this->_tables[$table_alias_sans_model_relation_chain_prefix]->get_table_name(); |
|
| 4976 | - } |
|
| 4977 | - |
|
| 4978 | - |
|
| 4979 | - |
|
| 4980 | - /** |
|
| 4981 | - * Returns a flat array of all field son this model, instead of organizing them |
|
| 4982 | - * by table_alias as they are in the constructor. |
|
| 4983 | - * |
|
| 4984 | - * @param bool $include_db_only_fields flag indicating whether or not to include the db-only fields |
|
| 4985 | - * @return EE_Model_Field_Base[] where the keys are the field's name |
|
| 4986 | - */ |
|
| 4987 | - public function field_settings($include_db_only_fields = false) |
|
| 4988 | - { |
|
| 4989 | - if ($include_db_only_fields) { |
|
| 4990 | - if ($this->_cached_fields === null) { |
|
| 4991 | - $this->_cached_fields = array(); |
|
| 4992 | - foreach ($this->_fields as $fields_corresponding_to_table) { |
|
| 4993 | - foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
| 4994 | - $this->_cached_fields[$field_name] = $field_obj; |
|
| 4995 | - } |
|
| 4996 | - } |
|
| 4997 | - } |
|
| 4998 | - return $this->_cached_fields; |
|
| 4999 | - } |
|
| 5000 | - if ($this->_cached_fields_non_db_only === null) { |
|
| 5001 | - $this->_cached_fields_non_db_only = array(); |
|
| 5002 | - foreach ($this->_fields as $fields_corresponding_to_table) { |
|
| 5003 | - foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
| 5004 | - /** @var $field_obj EE_Model_Field_Base */ |
|
| 5005 | - if (! $field_obj->is_db_only_field()) { |
|
| 5006 | - $this->_cached_fields_non_db_only[$field_name] = $field_obj; |
|
| 5007 | - } |
|
| 5008 | - } |
|
| 5009 | - } |
|
| 5010 | - } |
|
| 5011 | - return $this->_cached_fields_non_db_only; |
|
| 5012 | - } |
|
| 5013 | - |
|
| 5014 | - |
|
| 5015 | - |
|
| 5016 | - /** |
|
| 5017 | - * cycle though array of attendees and create objects out of each item |
|
| 5018 | - * |
|
| 5019 | - * @access private |
|
| 5020 | - * @param array $rows of results of $wpdb->get_results($query,ARRAY_A) |
|
| 5021 | - * @return \EE_Base_Class[] array keys are primary keys (if there is a primary key on the model. if not, |
|
| 5022 | - * numerically indexed) |
|
| 5023 | - * @throws EE_Error |
|
| 5024 | - */ |
|
| 5025 | - protected function _create_objects($rows = array()) |
|
| 5026 | - { |
|
| 5027 | - $array_of_objects = array(); |
|
| 5028 | - if (empty($rows)) { |
|
| 5029 | - return array(); |
|
| 5030 | - } |
|
| 5031 | - $count_if_model_has_no_primary_key = 0; |
|
| 5032 | - $has_primary_key = $this->has_primary_key_field(); |
|
| 5033 | - $primary_key_field = $has_primary_key ? $this->get_primary_key_field() : null; |
|
| 5034 | - foreach ((array)$rows as $row) { |
|
| 5035 | - if (empty($row)) { |
|
| 5036 | - //wp did its weird thing where it returns an array like array(0=>null), which is totally not helpful... |
|
| 5037 | - return array(); |
|
| 5038 | - } |
|
| 5039 | - //check if we've already set this object in the results array, |
|
| 5040 | - //in which case there's no need to process it further (again) |
|
| 5041 | - if ($has_primary_key) { |
|
| 5042 | - $table_pk_value = $this->_get_column_value_with_table_alias_or_not( |
|
| 5043 | - $row, |
|
| 5044 | - $primary_key_field->get_qualified_column(), |
|
| 5045 | - $primary_key_field->get_table_column() |
|
| 5046 | - ); |
|
| 5047 | - if ($table_pk_value && isset($array_of_objects[$table_pk_value])) { |
|
| 5048 | - continue; |
|
| 5049 | - } |
|
| 5050 | - } |
|
| 5051 | - $classInstance = $this->instantiate_class_from_array_or_object($row); |
|
| 5052 | - if (! $classInstance) { |
|
| 5053 | - throw new EE_Error( |
|
| 5054 | - sprintf( |
|
| 5055 | - __('Could not create instance of class %s from row %s', 'event_espresso'), |
|
| 5056 | - $this->get_this_model_name(), |
|
| 5057 | - http_build_query($row) |
|
| 5058 | - ) |
|
| 5059 | - ); |
|
| 5060 | - } |
|
| 5061 | - //set the timezone on the instantiated objects |
|
| 5062 | - $classInstance->set_timezone($this->_timezone); |
|
| 5063 | - //make sure if there is any timezone setting present that we set the timezone for the object |
|
| 5064 | - $key = $has_primary_key ? $classInstance->ID() : $count_if_model_has_no_primary_key++; |
|
| 5065 | - $array_of_objects[$key] = $classInstance; |
|
| 5066 | - //also, for all the relations of type BelongsTo, see if we can cache |
|
| 5067 | - //those related models |
|
| 5068 | - //(we could do this for other relations too, but if there are conditions |
|
| 5069 | - //that filtered out some fo the results, then we'd be caching an incomplete set |
|
| 5070 | - //so it requires a little more thought than just caching them immediately...) |
|
| 5071 | - foreach ($this->_model_relations as $modelName => $relation_obj) { |
|
| 5072 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 5073 | - //check if this model's INFO is present. If so, cache it on the model |
|
| 5074 | - $other_model = $relation_obj->get_other_model(); |
|
| 5075 | - $other_model_obj_maybe = $other_model->instantiate_class_from_array_or_object($row); |
|
| 5076 | - //if we managed to make a model object from the results, cache it on the main model object |
|
| 5077 | - if ($other_model_obj_maybe) { |
|
| 5078 | - //set timezone on these other model objects if they are present |
|
| 5079 | - $other_model_obj_maybe->set_timezone($this->_timezone); |
|
| 5080 | - $classInstance->cache($modelName, $other_model_obj_maybe); |
|
| 5081 | - } |
|
| 5082 | - } |
|
| 5083 | - } |
|
| 5084 | - //also, if this was a custom select query, let's see if there are any results for the custom select fields |
|
| 5085 | - //and add them to the object as well. We'll convert according to the set data_type if there's any set for |
|
| 5086 | - //the field in the CustomSelects object |
|
| 5087 | - if ($this->_custom_selections instanceof CustomSelects) { |
|
| 5088 | - $classInstance->setCustomSelectsValues( |
|
| 5089 | - $this->getValuesForCustomSelectAliasesFromResults($row) |
|
| 5090 | - ); |
|
| 5091 | - } |
|
| 5092 | - } |
|
| 5093 | - return $array_of_objects; |
|
| 5094 | - } |
|
| 5095 | - |
|
| 5096 | - |
|
| 5097 | - /** |
|
| 5098 | - * This will parse a given row of results from the db and see if any keys in the results match an alias within the |
|
| 5099 | - * current CustomSelects object. This will be used to build an array of values indexed by those keys. |
|
| 5100 | - * |
|
| 5101 | - * @param array $db_results_row |
|
| 5102 | - * @return array |
|
| 5103 | - */ |
|
| 5104 | - protected function getValuesForCustomSelectAliasesFromResults(array $db_results_row) |
|
| 5105 | - { |
|
| 5106 | - $results = array(); |
|
| 5107 | - if ($this->_custom_selections instanceof CustomSelects) { |
|
| 5108 | - foreach ($this->_custom_selections->columnAliases() as $alias) { |
|
| 5109 | - if (isset($db_results_row[$alias])) { |
|
| 5110 | - $results[$alias] = $this->convertValueToDataType( |
|
| 5111 | - $db_results_row[$alias], |
|
| 5112 | - $this->_custom_selections->getDataTypeForAlias($alias) |
|
| 5113 | - ); |
|
| 5114 | - } |
|
| 5115 | - } |
|
| 5116 | - } |
|
| 5117 | - return $results; |
|
| 5118 | - } |
|
| 5119 | - |
|
| 5120 | - |
|
| 5121 | - /** |
|
| 5122 | - * This will set the value for the given alias |
|
| 5123 | - * @param string $value |
|
| 5124 | - * @param string $datatype (one of %d, %s, %f) |
|
| 5125 | - * @return int|string|float (int for %d, string for %s, float for %f) |
|
| 5126 | - */ |
|
| 5127 | - protected function convertValueToDataType($value, $datatype) |
|
| 5128 | - { |
|
| 5129 | - switch ($datatype) { |
|
| 5130 | - case '%f': |
|
| 5131 | - return (float) $value; |
|
| 5132 | - case '%d': |
|
| 5133 | - return (int) $value; |
|
| 5134 | - default: |
|
| 5135 | - return (string) $value; |
|
| 5136 | - } |
|
| 5137 | - } |
|
| 5138 | - |
|
| 5139 | - |
|
| 5140 | - /** |
|
| 5141 | - * The purpose of this method is to allow us to create a model object that is not in the db that holds default |
|
| 5142 | - * values. A typical example of where this is used is when creating a new item and the initial load of a form. We |
|
| 5143 | - * dont' necessarily want to test for if the object is present but just assume it is BUT load the defaults from the |
|
| 5144 | - * object (as set in the model_field!). |
|
| 5145 | - * |
|
| 5146 | - * @return EE_Base_Class single EE_Base_Class object with default values for the properties. |
|
| 5147 | - */ |
|
| 5148 | - public function create_default_object() |
|
| 5149 | - { |
|
| 5150 | - $this_model_fields_and_values = array(); |
|
| 5151 | - //setup the row using default values; |
|
| 5152 | - foreach ($this->field_settings() as $field_name => $field_obj) { |
|
| 5153 | - $this_model_fields_and_values[$field_name] = $field_obj->get_default_value(); |
|
| 5154 | - } |
|
| 5155 | - $className = $this->_get_class_name(); |
|
| 5156 | - $classInstance = EE_Registry::instance() |
|
| 5157 | - ->load_class($className, array($this_model_fields_and_values), false, false); |
|
| 5158 | - return $classInstance; |
|
| 5159 | - } |
|
| 5160 | - |
|
| 5161 | - |
|
| 5162 | - |
|
| 5163 | - /** |
|
| 5164 | - * @param mixed $cols_n_values either an array of where each key is the name of a field, and the value is its value |
|
| 5165 | - * or an stdClass where each property is the name of a column, |
|
| 5166 | - * @return EE_Base_Class |
|
| 5167 | - * @throws EE_Error |
|
| 5168 | - */ |
|
| 5169 | - public function instantiate_class_from_array_or_object($cols_n_values) |
|
| 5170 | - { |
|
| 5171 | - if (! is_array($cols_n_values) && is_object($cols_n_values)) { |
|
| 5172 | - $cols_n_values = get_object_vars($cols_n_values); |
|
| 5173 | - } |
|
| 5174 | - $primary_key = null; |
|
| 5175 | - //make sure the array only has keys that are fields/columns on this model |
|
| 5176 | - $this_model_fields_n_values = $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
| 5177 | - if ($this->has_primary_key_field() && isset($this_model_fields_n_values[$this->primary_key_name()])) { |
|
| 5178 | - $primary_key = $this_model_fields_n_values[$this->primary_key_name()]; |
|
| 5179 | - } |
|
| 5180 | - $className = $this->_get_class_name(); |
|
| 5181 | - //check we actually found results that we can use to build our model object |
|
| 5182 | - //if not, return null |
|
| 5183 | - if ($this->has_primary_key_field()) { |
|
| 5184 | - if (empty($this_model_fields_n_values[$this->primary_key_name()])) { |
|
| 5185 | - return null; |
|
| 5186 | - } |
|
| 5187 | - } else if ($this->unique_indexes()) { |
|
| 5188 | - $first_column = reset($this_model_fields_n_values); |
|
| 5189 | - if (empty($first_column)) { |
|
| 5190 | - return null; |
|
| 5191 | - } |
|
| 5192 | - } |
|
| 5193 | - // if there is no primary key or the object doesn't already exist in the entity map, then create a new instance |
|
| 5194 | - if ($primary_key) { |
|
| 5195 | - $classInstance = $this->get_from_entity_map($primary_key); |
|
| 5196 | - if (! $classInstance) { |
|
| 5197 | - $classInstance = EE_Registry::instance() |
|
| 5198 | - ->load_class($className, |
|
| 5199 | - array($this_model_fields_n_values, $this->_timezone), true, false); |
|
| 5200 | - // add this new object to the entity map |
|
| 5201 | - $classInstance = $this->add_to_entity_map($classInstance); |
|
| 5202 | - } |
|
| 5203 | - } else { |
|
| 5204 | - $classInstance = EE_Registry::instance() |
|
| 5205 | - ->load_class($className, array($this_model_fields_n_values, $this->_timezone), |
|
| 5206 | - true, false); |
|
| 5207 | - } |
|
| 5208 | - return $classInstance; |
|
| 5209 | - } |
|
| 5210 | - |
|
| 5211 | - |
|
| 5212 | - |
|
| 5213 | - /** |
|
| 5214 | - * Gets the model object from the entity map if it exists |
|
| 5215 | - * |
|
| 5216 | - * @param int|string $id the ID of the model object |
|
| 5217 | - * @return EE_Base_Class |
|
| 5218 | - */ |
|
| 5219 | - public function get_from_entity_map($id) |
|
| 5220 | - { |
|
| 5221 | - return isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id]) |
|
| 5222 | - ? $this->_entity_map[EEM_Base::$_model_query_blog_id][$id] : null; |
|
| 5223 | - } |
|
| 5224 | - |
|
| 5225 | - |
|
| 5226 | - |
|
| 5227 | - /** |
|
| 5228 | - * add_to_entity_map |
|
| 5229 | - * Adds the object to the model's entity mappings |
|
| 5230 | - * Effectively tells the models "Hey, this model object is the most up-to-date representation of the data, |
|
| 5231 | - * and for the remainder of the request, it's even more up-to-date than what's in the database. |
|
| 5232 | - * So, if the database doesn't agree with what's in the entity mapper, ignore the database" |
|
| 5233 | - * If the database gets updated directly and you want the entity mapper to reflect that change, |
|
| 5234 | - * then this method should be called immediately after the update query |
|
| 5235 | - * Note: The map is indexed by whatever the current blog id is set (via EEM_Base::$_model_query_blog_id). This is |
|
| 5236 | - * so on multisite, the entity map is specific to the query being done for a specific site. |
|
| 5237 | - * |
|
| 5238 | - * @param EE_Base_Class $object |
|
| 5239 | - * @throws EE_Error |
|
| 5240 | - * @return \EE_Base_Class |
|
| 5241 | - */ |
|
| 5242 | - public function add_to_entity_map(EE_Base_Class $object) |
|
| 5243 | - { |
|
| 5244 | - $className = $this->_get_class_name(); |
|
| 5245 | - if (! $object instanceof $className) { |
|
| 5246 | - throw new EE_Error(sprintf(__("You tried adding a %s to a mapping of %ss", "event_espresso"), |
|
| 5247 | - is_object($object) ? get_class($object) : $object, $className)); |
|
| 5248 | - } |
|
| 5249 | - /** @var $object EE_Base_Class */ |
|
| 5250 | - if (! $object->ID()) { |
|
| 5251 | - throw new EE_Error(sprintf(__("You tried storing a model object with NO ID in the %s entity mapper.", |
|
| 5252 | - "event_espresso"), get_class($this))); |
|
| 5253 | - } |
|
| 5254 | - // double check it's not already there |
|
| 5255 | - $classInstance = $this->get_from_entity_map($object->ID()); |
|
| 5256 | - if ($classInstance) { |
|
| 5257 | - return $classInstance; |
|
| 5258 | - } |
|
| 5259 | - $this->_entity_map[EEM_Base::$_model_query_blog_id][$object->ID()] = $object; |
|
| 5260 | - return $object; |
|
| 5261 | - } |
|
| 5262 | - |
|
| 5263 | - |
|
| 5264 | - |
|
| 5265 | - /** |
|
| 5266 | - * if a valid identifier is provided, then that entity is unset from the entity map, |
|
| 5267 | - * if no identifier is provided, then the entire entity map is emptied |
|
| 5268 | - * |
|
| 5269 | - * @param int|string $id the ID of the model object |
|
| 5270 | - * @return boolean |
|
| 5271 | - */ |
|
| 5272 | - public function clear_entity_map($id = null) |
|
| 5273 | - { |
|
| 5274 | - if (empty($id)) { |
|
| 5275 | - $this->_entity_map[EEM_Base::$_model_query_blog_id] = array(); |
|
| 5276 | - return true; |
|
| 5277 | - } |
|
| 5278 | - if (isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id])) { |
|
| 5279 | - unset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id]); |
|
| 5280 | - return true; |
|
| 5281 | - } |
|
| 5282 | - return false; |
|
| 5283 | - } |
|
| 5284 | - |
|
| 5285 | - |
|
| 5286 | - |
|
| 5287 | - /** |
|
| 5288 | - * Public wrapper for _deduce_fields_n_values_from_cols_n_values. |
|
| 5289 | - * Given an array where keys are column (or column alias) names and values, |
|
| 5290 | - * returns an array of their corresponding field names and database values |
|
| 5291 | - * |
|
| 5292 | - * @param array $cols_n_values |
|
| 5293 | - * @return array |
|
| 5294 | - */ |
|
| 5295 | - public function deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
| 5296 | - { |
|
| 5297 | - return $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
| 5298 | - } |
|
| 5299 | - |
|
| 5300 | - |
|
| 5301 | - |
|
| 5302 | - /** |
|
| 5303 | - * _deduce_fields_n_values_from_cols_n_values |
|
| 5304 | - * Given an array where keys are column (or column alias) names and values, |
|
| 5305 | - * returns an array of their corresponding field names and database values |
|
| 5306 | - * |
|
| 5307 | - * @param string $cols_n_values |
|
| 5308 | - * @return array |
|
| 5309 | - */ |
|
| 5310 | - protected function _deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
| 5311 | - { |
|
| 5312 | - $this_model_fields_n_values = array(); |
|
| 5313 | - foreach ($this->get_tables() as $table_alias => $table_obj) { |
|
| 5314 | - $table_pk_value = $this->_get_column_value_with_table_alias_or_not($cols_n_values, |
|
| 5315 | - $table_obj->get_fully_qualified_pk_column(), $table_obj->get_pk_column()); |
|
| 5316 | - //there is a primary key on this table and its not set. Use defaults for all its columns |
|
| 5317 | - if ($table_pk_value === null && $table_obj->get_pk_column()) { |
|
| 5318 | - foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
| 5319 | - if (! $field_obj->is_db_only_field()) { |
|
| 5320 | - //prepare field as if its coming from db |
|
| 5321 | - $prepared_value = $field_obj->prepare_for_set($field_obj->get_default_value()); |
|
| 5322 | - $this_model_fields_n_values[$field_name] = $field_obj->prepare_for_use_in_db($prepared_value); |
|
| 5323 | - } |
|
| 5324 | - } |
|
| 5325 | - } else { |
|
| 5326 | - //the table's rows existed. Use their values |
|
| 5327 | - foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
| 5328 | - if (! $field_obj->is_db_only_field()) { |
|
| 5329 | - $this_model_fields_n_values[$field_name] = $this->_get_column_value_with_table_alias_or_not( |
|
| 5330 | - $cols_n_values, $field_obj->get_qualified_column(), |
|
| 5331 | - $field_obj->get_table_column() |
|
| 5332 | - ); |
|
| 5333 | - } |
|
| 5334 | - } |
|
| 5335 | - } |
|
| 5336 | - } |
|
| 5337 | - return $this_model_fields_n_values; |
|
| 5338 | - } |
|
| 5339 | - |
|
| 5340 | - |
|
| 5341 | - |
|
| 5342 | - /** |
|
| 5343 | - * @param $cols_n_values |
|
| 5344 | - * @param $qualified_column |
|
| 5345 | - * @param $regular_column |
|
| 5346 | - * @return null |
|
| 5347 | - */ |
|
| 5348 | - protected function _get_column_value_with_table_alias_or_not($cols_n_values, $qualified_column, $regular_column) |
|
| 5349 | - { |
|
| 5350 | - $value = null; |
|
| 5351 | - //ask the field what it think it's table_name.column_name should be, and call it the "qualified column" |
|
| 5352 | - //does the field on the model relate to this column retrieved from the db? |
|
| 5353 | - //or is it a db-only field? (not relating to the model) |
|
| 5354 | - if (isset($cols_n_values[$qualified_column])) { |
|
| 5355 | - $value = $cols_n_values[$qualified_column]; |
|
| 5356 | - } elseif (isset($cols_n_values[$regular_column])) { |
|
| 5357 | - $value = $cols_n_values[$regular_column]; |
|
| 5358 | - } |
|
| 5359 | - return $value; |
|
| 5360 | - } |
|
| 5361 | - |
|
| 5362 | - |
|
| 5363 | - |
|
| 5364 | - /** |
|
| 5365 | - * refresh_entity_map_from_db |
|
| 5366 | - * Makes sure the model object in the entity map at $id assumes the values |
|
| 5367 | - * of the database (opposite of EE_base_Class::save()) |
|
| 5368 | - * |
|
| 5369 | - * @param int|string $id |
|
| 5370 | - * @return EE_Base_Class |
|
| 5371 | - * @throws EE_Error |
|
| 5372 | - */ |
|
| 5373 | - public function refresh_entity_map_from_db($id) |
|
| 5374 | - { |
|
| 5375 | - $obj_in_map = $this->get_from_entity_map($id); |
|
| 5376 | - if ($obj_in_map) { |
|
| 5377 | - $wpdb_results = $this->_get_all_wpdb_results( |
|
| 5378 | - array(array($this->get_primary_key_field()->get_name() => $id), 'limit' => 1) |
|
| 5379 | - ); |
|
| 5380 | - if ($wpdb_results && is_array($wpdb_results)) { |
|
| 5381 | - $one_row = reset($wpdb_results); |
|
| 5382 | - foreach ($this->_deduce_fields_n_values_from_cols_n_values($one_row) as $field_name => $db_value) { |
|
| 5383 | - $obj_in_map->set_from_db($field_name, $db_value); |
|
| 5384 | - } |
|
| 5385 | - //clear the cache of related model objects |
|
| 5386 | - foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
| 5387 | - $obj_in_map->clear_cache($relation_name, null, true); |
|
| 5388 | - } |
|
| 5389 | - } |
|
| 5390 | - $this->_entity_map[EEM_Base::$_model_query_blog_id][$id] = $obj_in_map; |
|
| 5391 | - return $obj_in_map; |
|
| 5392 | - } |
|
| 5393 | - return $this->get_one_by_ID($id); |
|
| 5394 | - } |
|
| 5395 | - |
|
| 5396 | - |
|
| 5397 | - |
|
| 5398 | - /** |
|
| 5399 | - * refresh_entity_map_with |
|
| 5400 | - * Leaves the entry in the entity map alone, but updates it to match the provided |
|
| 5401 | - * $replacing_model_obj (which we assume to be its equivalent but somehow NOT in the entity map). |
|
| 5402 | - * This is useful if you have a model object you want to make authoritative over what's in the entity map currently. |
|
| 5403 | - * Note: The old $replacing_model_obj should now be destroyed as it's now un-authoritative |
|
| 5404 | - * |
|
| 5405 | - * @param int|string $id |
|
| 5406 | - * @param EE_Base_Class $replacing_model_obj |
|
| 5407 | - * @return \EE_Base_Class |
|
| 5408 | - * @throws EE_Error |
|
| 5409 | - */ |
|
| 5410 | - public function refresh_entity_map_with($id, $replacing_model_obj) |
|
| 5411 | - { |
|
| 5412 | - $obj_in_map = $this->get_from_entity_map($id); |
|
| 5413 | - if ($obj_in_map) { |
|
| 5414 | - if ($replacing_model_obj instanceof EE_Base_Class) { |
|
| 5415 | - foreach ($replacing_model_obj->model_field_array() as $field_name => $value) { |
|
| 5416 | - $obj_in_map->set($field_name, $value); |
|
| 5417 | - } |
|
| 5418 | - //make the model object in the entity map's cache match the $replacing_model_obj |
|
| 5419 | - foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
| 5420 | - $obj_in_map->clear_cache($relation_name, null, true); |
|
| 5421 | - foreach ($replacing_model_obj->get_all_from_cache($relation_name) as $cache_id => $cached_obj) { |
|
| 5422 | - $obj_in_map->cache($relation_name, $cached_obj, $cache_id); |
|
| 5423 | - } |
|
| 5424 | - } |
|
| 5425 | - } |
|
| 5426 | - return $obj_in_map; |
|
| 5427 | - } |
|
| 5428 | - $this->add_to_entity_map($replacing_model_obj); |
|
| 5429 | - return $replacing_model_obj; |
|
| 5430 | - } |
|
| 5431 | - |
|
| 5432 | - |
|
| 5433 | - |
|
| 5434 | - /** |
|
| 5435 | - * Gets the EE class that corresponds to this model. Eg, for EEM_Answer that |
|
| 5436 | - * would be EE_Answer.To import that class, you'd just add ".class.php" to the name, like so |
|
| 5437 | - * require_once($this->_getClassName().".class.php"); |
|
| 5438 | - * |
|
| 5439 | - * @return string |
|
| 5440 | - */ |
|
| 5441 | - private function _get_class_name() |
|
| 5442 | - { |
|
| 5443 | - return "EE_" . $this->get_this_model_name(); |
|
| 5444 | - } |
|
| 5445 | - |
|
| 5446 | - |
|
| 5447 | - |
|
| 5448 | - /** |
|
| 5449 | - * Get the name of the items this model represents, for the quantity specified. Eg, |
|
| 5450 | - * if $quantity==1, on EEM_Event, it would 'Event' (internationalized), otherwise |
|
| 5451 | - * it would be 'Events'. |
|
| 5452 | - * |
|
| 5453 | - * @param int $quantity |
|
| 5454 | - * @return string |
|
| 5455 | - */ |
|
| 5456 | - public function item_name($quantity = 1) |
|
| 5457 | - { |
|
| 5458 | - return (int)$quantity === 1 ? $this->singular_item : $this->plural_item; |
|
| 5459 | - } |
|
| 5460 | - |
|
| 5461 | - |
|
| 5462 | - |
|
| 5463 | - /** |
|
| 5464 | - * Very handy general function to allow for plugins to extend any child of EE_TempBase. |
|
| 5465 | - * If a method is called on a child of EE_TempBase that doesn't exist, this function is called |
|
| 5466 | - * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
|
| 5467 | - * requiring a plugin to extend the EE_TempBase (which works fine is there's only 1 plugin, but when will that |
|
| 5468 | - * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
|
| 5469 | - * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
|
| 5470 | - * was called, and an array of the original arguments passed to the function. Whatever their callback function |
|
| 5471 | - * returns will be returned by this function. Example: in functions.php (or in a plugin): |
|
| 5472 | - * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
|
| 5473 | - * my_callback($previousReturnValue,EE_TempBase $object,$argsArray){ |
|
| 5474 | - * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 5475 | - * return $previousReturnValue.$returnString; |
|
| 5476 | - * } |
|
| 5477 | - * require('EEM_Answer.model.php'); |
|
| 5478 | - * $answer=EEM_Answer::instance(); |
|
| 5479 | - * echo $answer->my_callback('monkeys',100); |
|
| 5480 | - * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 5481 | - * |
|
| 5482 | - * @param string $methodName name of method which was called on a child of EE_TempBase, but which |
|
| 5483 | - * @param array $args array of original arguments passed to the function |
|
| 5484 | - * @throws EE_Error |
|
| 5485 | - * @return mixed whatever the plugin which calls add_filter decides |
|
| 5486 | - */ |
|
| 5487 | - public function __call($methodName, $args) |
|
| 5488 | - { |
|
| 5489 | - $className = get_class($this); |
|
| 5490 | - $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 5491 | - if (! has_filter($tagName)) { |
|
| 5492 | - throw new EE_Error( |
|
| 5493 | - sprintf( |
|
| 5494 | - __('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 );', |
|
| 5495 | - 'event_espresso'), |
|
| 5496 | - $methodName, |
|
| 5497 | - $className, |
|
| 5498 | - $tagName, |
|
| 5499 | - '<br />' |
|
| 5500 | - ) |
|
| 5501 | - ); |
|
| 5502 | - } |
|
| 5503 | - return apply_filters($tagName, null, $this, $args); |
|
| 5504 | - } |
|
| 5505 | - |
|
| 5506 | - |
|
| 5507 | - |
|
| 5508 | - /** |
|
| 5509 | - * Ensures $base_class_obj_or_id is of the EE_Base_Class child that corresponds ot this model. |
|
| 5510 | - * If not, assumes its an ID, and uses $this->get_one_by_ID() to get the EE_Base_Class. |
|
| 5511 | - * |
|
| 5512 | - * @param EE_Base_Class|string|int $base_class_obj_or_id either: |
|
| 5513 | - * the EE_Base_Class object that corresponds to this Model, |
|
| 5514 | - * the object's class name |
|
| 5515 | - * or object's ID |
|
| 5516 | - * @param boolean $ensure_is_in_db if set, we will also verify this model object |
|
| 5517 | - * exists in the database. If it does not, we add it |
|
| 5518 | - * @throws EE_Error |
|
| 5519 | - * @return EE_Base_Class |
|
| 5520 | - */ |
|
| 5521 | - public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false) |
|
| 5522 | - { |
|
| 5523 | - $className = $this->_get_class_name(); |
|
| 5524 | - if ($base_class_obj_or_id instanceof $className) { |
|
| 5525 | - $model_object = $base_class_obj_or_id; |
|
| 5526 | - } else { |
|
| 5527 | - $primary_key_field = $this->get_primary_key_field(); |
|
| 5528 | - if ( |
|
| 5529 | - $primary_key_field instanceof EE_Primary_Key_Int_Field |
|
| 5530 | - && ( |
|
| 5531 | - is_int($base_class_obj_or_id) |
|
| 5532 | - || is_string($base_class_obj_or_id) |
|
| 5533 | - ) |
|
| 5534 | - ) { |
|
| 5535 | - // assume it's an ID. |
|
| 5536 | - // either a proper integer or a string representing an integer (eg "101" instead of 101) |
|
| 5537 | - $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
| 5538 | - } else if ( |
|
| 5539 | - $primary_key_field instanceof EE_Primary_Key_String_Field |
|
| 5540 | - && is_string($base_class_obj_or_id) |
|
| 5541 | - ) { |
|
| 5542 | - // assume its a string representation of the object |
|
| 5543 | - $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
| 5544 | - } else { |
|
| 5545 | - throw new EE_Error( |
|
| 5546 | - sprintf( |
|
| 5547 | - __( |
|
| 5548 | - "'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
| 5549 | - 'event_espresso' |
|
| 5550 | - ), |
|
| 5551 | - $base_class_obj_or_id, |
|
| 5552 | - $this->_get_class_name(), |
|
| 5553 | - print_r($base_class_obj_or_id, true) |
|
| 5554 | - ) |
|
| 5555 | - ); |
|
| 5556 | - } |
|
| 5557 | - } |
|
| 5558 | - if ($ensure_is_in_db && $model_object->ID() !== null) { |
|
| 5559 | - $model_object->save(); |
|
| 5560 | - } |
|
| 5561 | - return $model_object; |
|
| 5562 | - } |
|
| 5563 | - |
|
| 5564 | - |
|
| 5565 | - |
|
| 5566 | - /** |
|
| 5567 | - * Similar to ensure_is_obj(), this method makes sure $base_class_obj_or_id |
|
| 5568 | - * is a value of the this model's primary key. If it's an EE_Base_Class child, |
|
| 5569 | - * returns it ID. |
|
| 5570 | - * |
|
| 5571 | - * @param EE_Base_Class|int|string $base_class_obj_or_id |
|
| 5572 | - * @return int|string depending on the type of this model object's ID |
|
| 5573 | - * @throws EE_Error |
|
| 5574 | - */ |
|
| 5575 | - public function ensure_is_ID($base_class_obj_or_id) |
|
| 5576 | - { |
|
| 5577 | - $className = $this->_get_class_name(); |
|
| 5578 | - if ($base_class_obj_or_id instanceof $className) { |
|
| 5579 | - /** @var $base_class_obj_or_id EE_Base_Class */ |
|
| 5580 | - $id = $base_class_obj_or_id->ID(); |
|
| 5581 | - } elseif (is_int($base_class_obj_or_id)) { |
|
| 5582 | - //assume it's an ID |
|
| 5583 | - $id = $base_class_obj_or_id; |
|
| 5584 | - } elseif (is_string($base_class_obj_or_id)) { |
|
| 5585 | - //assume its a string representation of the object |
|
| 5586 | - $id = $base_class_obj_or_id; |
|
| 5587 | - } else { |
|
| 5588 | - throw new EE_Error(sprintf(__("'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
| 5589 | - 'event_espresso'), $base_class_obj_or_id, $this->_get_class_name(), |
|
| 5590 | - print_r($base_class_obj_or_id, true))); |
|
| 5591 | - } |
|
| 5592 | - return $id; |
|
| 5593 | - } |
|
| 5594 | - |
|
| 5595 | - |
|
| 5596 | - |
|
| 5597 | - /** |
|
| 5598 | - * Sets whether the values passed to the model (eg, values in WHERE, values in INSERT, UPDATE, etc) |
|
| 5599 | - * have already been ran through the appropriate model field's prepare_for_use_in_db method. IE, they have |
|
| 5600 | - * been sanitized and converted into the appropriate domain. |
|
| 5601 | - * Usually the only place you'll want to change the default (which is to assume values have NOT been sanitized by |
|
| 5602 | - * the model object/model field) is when making a method call from WITHIN a model object, which has direct access |
|
| 5603 | - * to its sanitized values. Note: after changing this setting, you should set it back to its previous value (using |
|
| 5604 | - * get_assumption_concerning_values_already_prepared_by_model_object()) eg. |
|
| 5605 | - * $EVT = EEM_Event::instance(); $old_setting = |
|
| 5606 | - * $EVT->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 5607 | - * $EVT->assume_values_already_prepared_by_model_object(true); |
|
| 5608 | - * $EVT->update(array('foo'=>'bar'),array(array('foo'=>'monkey'))); |
|
| 5609 | - * $EVT->assume_values_already_prepared_by_model_object($old_setting); |
|
| 5610 | - * |
|
| 5611 | - * @param int $values_already_prepared like one of the constants on EEM_Base |
|
| 5612 | - * @return void |
|
| 5613 | - */ |
|
| 5614 | - public function assume_values_already_prepared_by_model_object( |
|
| 5615 | - $values_already_prepared = self::not_prepared_by_model_object |
|
| 5616 | - ) { |
|
| 5617 | - $this->_values_already_prepared_by_model_object = $values_already_prepared; |
|
| 5618 | - } |
|
| 5619 | - |
|
| 5620 | - |
|
| 5621 | - |
|
| 5622 | - /** |
|
| 5623 | - * Read comments for assume_values_already_prepared_by_model_object() |
|
| 5624 | - * |
|
| 5625 | - * @return int |
|
| 5626 | - */ |
|
| 5627 | - public function get_assumption_concerning_values_already_prepared_by_model_object() |
|
| 5628 | - { |
|
| 5629 | - return $this->_values_already_prepared_by_model_object; |
|
| 5630 | - } |
|
| 5631 | - |
|
| 5632 | - |
|
| 5633 | - |
|
| 5634 | - /** |
|
| 5635 | - * Gets all the indexes on this model |
|
| 5636 | - * |
|
| 5637 | - * @return EE_Index[] |
|
| 5638 | - */ |
|
| 5639 | - public function indexes() |
|
| 5640 | - { |
|
| 5641 | - return $this->_indexes; |
|
| 5642 | - } |
|
| 5643 | - |
|
| 5644 | - |
|
| 5645 | - |
|
| 5646 | - /** |
|
| 5647 | - * Gets all the Unique Indexes on this model |
|
| 5648 | - * |
|
| 5649 | - * @return EE_Unique_Index[] |
|
| 5650 | - */ |
|
| 5651 | - public function unique_indexes() |
|
| 5652 | - { |
|
| 5653 | - $unique_indexes = array(); |
|
| 5654 | - foreach ($this->_indexes as $name => $index) { |
|
| 5655 | - if ($index instanceof EE_Unique_Index) { |
|
| 5656 | - $unique_indexes [$name] = $index; |
|
| 5657 | - } |
|
| 5658 | - } |
|
| 5659 | - return $unique_indexes; |
|
| 5660 | - } |
|
| 5661 | - |
|
| 5662 | - |
|
| 5663 | - |
|
| 5664 | - /** |
|
| 5665 | - * Gets all the fields which, when combined, make the primary key. |
|
| 5666 | - * This is usually just an array with 1 element (the primary key), but in cases |
|
| 5667 | - * where there is no primary key, it's a combination of fields as defined |
|
| 5668 | - * on a primary index |
|
| 5669 | - * |
|
| 5670 | - * @return EE_Model_Field_Base[] indexed by the field's name |
|
| 5671 | - * @throws EE_Error |
|
| 5672 | - */ |
|
| 5673 | - public function get_combined_primary_key_fields() |
|
| 5674 | - { |
|
| 5675 | - foreach ($this->indexes() as $index) { |
|
| 5676 | - if ($index instanceof EE_Primary_Key_Index) { |
|
| 5677 | - return $index->fields(); |
|
| 5678 | - } |
|
| 5679 | - } |
|
| 5680 | - return array($this->primary_key_name() => $this->get_primary_key_field()); |
|
| 5681 | - } |
|
| 5682 | - |
|
| 5683 | - |
|
| 5684 | - |
|
| 5685 | - /** |
|
| 5686 | - * Used to build a primary key string (when the model has no primary key), |
|
| 5687 | - * which can be used a unique string to identify this model object. |
|
| 5688 | - * |
|
| 5689 | - * @param array $cols_n_values keys are field names, values are their values |
|
| 5690 | - * @return string |
|
| 5691 | - * @throws EE_Error |
|
| 5692 | - */ |
|
| 5693 | - public function get_index_primary_key_string($cols_n_values) |
|
| 5694 | - { |
|
| 5695 | - $cols_n_values_for_primary_key_index = array_intersect_key($cols_n_values, |
|
| 5696 | - $this->get_combined_primary_key_fields()); |
|
| 5697 | - return http_build_query($cols_n_values_for_primary_key_index); |
|
| 5698 | - } |
|
| 5699 | - |
|
| 5700 | - |
|
| 5701 | - |
|
| 5702 | - /** |
|
| 5703 | - * Gets the field values from the primary key string |
|
| 5704 | - * |
|
| 5705 | - * @see EEM_Base::get_combined_primary_key_fields() and EEM_Base::get_index_primary_key_string() |
|
| 5706 | - * @param string $index_primary_key_string |
|
| 5707 | - * @return null|array |
|
| 5708 | - * @throws EE_Error |
|
| 5709 | - */ |
|
| 5710 | - public function parse_index_primary_key_string($index_primary_key_string) |
|
| 5711 | - { |
|
| 5712 | - $key_fields = $this->get_combined_primary_key_fields(); |
|
| 5713 | - //check all of them are in the $id |
|
| 5714 | - $key_vals_in_combined_pk = array(); |
|
| 5715 | - parse_str($index_primary_key_string, $key_vals_in_combined_pk); |
|
| 5716 | - foreach ($key_fields as $key_field_name => $field_obj) { |
|
| 5717 | - if (! isset($key_vals_in_combined_pk[$key_field_name])) { |
|
| 5718 | - return null; |
|
| 5719 | - } |
|
| 5720 | - } |
|
| 5721 | - return $key_vals_in_combined_pk; |
|
| 5722 | - } |
|
| 5723 | - |
|
| 5724 | - |
|
| 5725 | - |
|
| 5726 | - /** |
|
| 5727 | - * verifies that an array of key-value pairs for model fields has a key |
|
| 5728 | - * for each field comprising the primary key index |
|
| 5729 | - * |
|
| 5730 | - * @param array $key_vals |
|
| 5731 | - * @return boolean |
|
| 5732 | - * @throws EE_Error |
|
| 5733 | - */ |
|
| 5734 | - public function has_all_combined_primary_key_fields($key_vals) |
|
| 5735 | - { |
|
| 5736 | - $keys_it_should_have = array_keys($this->get_combined_primary_key_fields()); |
|
| 5737 | - foreach ($keys_it_should_have as $key) { |
|
| 5738 | - if (! isset($key_vals[$key])) { |
|
| 5739 | - return false; |
|
| 5740 | - } |
|
| 5741 | - } |
|
| 5742 | - return true; |
|
| 5743 | - } |
|
| 5744 | - |
|
| 5745 | - |
|
| 5746 | - |
|
| 5747 | - /** |
|
| 5748 | - * Finds all model objects in the DB that appear to be a copy of $model_object_or_attributes_array. |
|
| 5749 | - * We consider something to be a copy if all the attributes match (except the ID, of course). |
|
| 5750 | - * |
|
| 5751 | - * @param array|EE_Base_Class $model_object_or_attributes_array If its an array, it's field-value pairs |
|
| 5752 | - * @param array $query_params like EEM_Base::get_all's query_params. |
|
| 5753 | - * @throws EE_Error |
|
| 5754 | - * @return \EE_Base_Class[] Array keys are object IDs (if there is a primary key on the model. if not, numerically |
|
| 5755 | - * indexed) |
|
| 5756 | - */ |
|
| 5757 | - public function get_all_copies($model_object_or_attributes_array, $query_params = array()) |
|
| 5758 | - { |
|
| 5759 | - if ($model_object_or_attributes_array instanceof EE_Base_Class) { |
|
| 5760 | - $attributes_array = $model_object_or_attributes_array->model_field_array(); |
|
| 5761 | - } elseif (is_array($model_object_or_attributes_array)) { |
|
| 5762 | - $attributes_array = $model_object_or_attributes_array; |
|
| 5763 | - } else { |
|
| 5764 | - throw new EE_Error(sprintf(__("get_all_copies should be provided with either a model object or an array of field-value-pairs, but was given %s", |
|
| 5765 | - "event_espresso"), $model_object_or_attributes_array)); |
|
| 5766 | - } |
|
| 5767 | - //even copies obviously won't have the same ID, so remove the primary key |
|
| 5768 | - //from the WHERE conditions for finding copies (if there is a primary key, of course) |
|
| 5769 | - if ($this->has_primary_key_field() && isset($attributes_array[$this->primary_key_name()])) { |
|
| 5770 | - unset($attributes_array[$this->primary_key_name()]); |
|
| 5771 | - } |
|
| 5772 | - if (isset($query_params[0])) { |
|
| 5773 | - $query_params[0] = array_merge($attributes_array, $query_params); |
|
| 5774 | - } else { |
|
| 5775 | - $query_params[0] = $attributes_array; |
|
| 5776 | - } |
|
| 5777 | - return $this->get_all($query_params); |
|
| 5778 | - } |
|
| 5779 | - |
|
| 5780 | - |
|
| 5781 | - |
|
| 5782 | - /** |
|
| 5783 | - * Gets the first copy we find. See get_all_copies for more details |
|
| 5784 | - * |
|
| 5785 | - * @param mixed EE_Base_Class | array $model_object_or_attributes_array |
|
| 5786 | - * @param array $query_params |
|
| 5787 | - * @return EE_Base_Class |
|
| 5788 | - * @throws EE_Error |
|
| 5789 | - */ |
|
| 5790 | - public function get_one_copy($model_object_or_attributes_array, $query_params = array()) |
|
| 5791 | - { |
|
| 5792 | - if (! is_array($query_params)) { |
|
| 5793 | - EE_Error::doing_it_wrong('EEM_Base::get_one_copy', |
|
| 5794 | - sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 5795 | - gettype($query_params)), '4.6.0'); |
|
| 5796 | - $query_params = array(); |
|
| 5797 | - } |
|
| 5798 | - $query_params['limit'] = 1; |
|
| 5799 | - $copies = $this->get_all_copies($model_object_or_attributes_array, $query_params); |
|
| 5800 | - if (is_array($copies)) { |
|
| 5801 | - return array_shift($copies); |
|
| 5802 | - } |
|
| 5803 | - return null; |
|
| 5804 | - } |
|
| 5805 | - |
|
| 5806 | - |
|
| 5807 | - |
|
| 5808 | - /** |
|
| 5809 | - * Updates the item with the specified id. Ignores default query parameters because |
|
| 5810 | - * we have specified the ID, and its assumed we KNOW what we're doing |
|
| 5811 | - * |
|
| 5812 | - * @param array $fields_n_values keys are field names, values are their new values |
|
| 5813 | - * @param int|string $id the value of the primary key to update |
|
| 5814 | - * @return int number of rows updated |
|
| 5815 | - * @throws EE_Error |
|
| 5816 | - */ |
|
| 5817 | - public function update_by_ID($fields_n_values, $id) |
|
| 5818 | - { |
|
| 5819 | - $query_params = array( |
|
| 5820 | - 0 => array($this->get_primary_key_field()->get_name() => $id), |
|
| 5821 | - 'default_where_conditions' => EEM_Base::default_where_conditions_others_only, |
|
| 5822 | - ); |
|
| 5823 | - return $this->update($fields_n_values, $query_params); |
|
| 5824 | - } |
|
| 5825 | - |
|
| 5826 | - |
|
| 5827 | - |
|
| 5828 | - /** |
|
| 5829 | - * Changes an operator which was supplied to the models into one usable in SQL |
|
| 5830 | - * |
|
| 5831 | - * @param string $operator_supplied |
|
| 5832 | - * @return string an operator which can be used in SQL |
|
| 5833 | - * @throws EE_Error |
|
| 5834 | - */ |
|
| 5835 | - private function _prepare_operator_for_sql($operator_supplied) |
|
| 5836 | - { |
|
| 5837 | - $sql_operator = isset($this->_valid_operators[$operator_supplied]) ? $this->_valid_operators[$operator_supplied] |
|
| 5838 | - : null; |
|
| 5839 | - if ($sql_operator) { |
|
| 5840 | - return $sql_operator; |
|
| 5841 | - } |
|
| 5842 | - throw new EE_Error( |
|
| 5843 | - sprintf( |
|
| 5844 | - __( |
|
| 5845 | - "The operator '%s' is not in the list of valid operators: %s", |
|
| 5846 | - "event_espresso" |
|
| 5847 | - ), $operator_supplied, implode(",", array_keys($this->_valid_operators)) |
|
| 5848 | - ) |
|
| 5849 | - ); |
|
| 5850 | - } |
|
| 5851 | - |
|
| 5852 | - |
|
| 5853 | - |
|
| 5854 | - /** |
|
| 5855 | - * Gets the valid operators |
|
| 5856 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5857 | - */ |
|
| 5858 | - public function valid_operators(){ |
|
| 5859 | - return $this->_valid_operators; |
|
| 5860 | - } |
|
| 5861 | - |
|
| 5862 | - |
|
| 5863 | - |
|
| 5864 | - /** |
|
| 5865 | - * Gets the between-style operators (take 2 arguments). |
|
| 5866 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5867 | - */ |
|
| 5868 | - public function valid_between_style_operators() |
|
| 5869 | - { |
|
| 5870 | - return array_intersect( |
|
| 5871 | - $this->valid_operators(), |
|
| 5872 | - $this->_between_style_operators |
|
| 5873 | - ); |
|
| 5874 | - } |
|
| 5875 | - |
|
| 5876 | - /** |
|
| 5877 | - * Gets the "like"-style operators (take a single argument, but it may contain wildcards) |
|
| 5878 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5879 | - */ |
|
| 5880 | - public function valid_like_style_operators() |
|
| 5881 | - { |
|
| 5882 | - return array_intersect( |
|
| 5883 | - $this->valid_operators(), |
|
| 5884 | - $this->_like_style_operators |
|
| 5885 | - ); |
|
| 5886 | - } |
|
| 5887 | - |
|
| 5888 | - /** |
|
| 5889 | - * Gets the "in"-style operators |
|
| 5890 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5891 | - */ |
|
| 5892 | - public function valid_in_style_operators() |
|
| 5893 | - { |
|
| 5894 | - return array_intersect( |
|
| 5895 | - $this->valid_operators(), |
|
| 5896 | - $this->_in_style_operators |
|
| 5897 | - ); |
|
| 5898 | - } |
|
| 5899 | - |
|
| 5900 | - /** |
|
| 5901 | - * Gets the "null"-style operators (accept no arguments) |
|
| 5902 | - * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5903 | - */ |
|
| 5904 | - public function valid_null_style_operators() |
|
| 5905 | - { |
|
| 5906 | - return array_intersect( |
|
| 5907 | - $this->valid_operators(), |
|
| 5908 | - $this->_null_style_operators |
|
| 5909 | - ); |
|
| 5910 | - } |
|
| 5911 | - |
|
| 5912 | - /** |
|
| 5913 | - * Gets an array where keys are the primary keys and values are their 'names' |
|
| 5914 | - * (as determined by the model object's name() function, which is often overridden) |
|
| 5915 | - * |
|
| 5916 | - * @param array $query_params like get_all's |
|
| 5917 | - * @return string[] |
|
| 5918 | - * @throws EE_Error |
|
| 5919 | - */ |
|
| 5920 | - public function get_all_names($query_params = array()) |
|
| 5921 | - { |
|
| 5922 | - $objs = $this->get_all($query_params); |
|
| 5923 | - $names = array(); |
|
| 5924 | - foreach ($objs as $obj) { |
|
| 5925 | - $names[$obj->ID()] = $obj->name(); |
|
| 5926 | - } |
|
| 5927 | - return $names; |
|
| 5928 | - } |
|
| 5929 | - |
|
| 5930 | - |
|
| 5931 | - |
|
| 5932 | - /** |
|
| 5933 | - * Gets an array of primary keys from the model objects. If you acquired the model objects |
|
| 5934 | - * using EEM_Base::get_all() you don't need to call this (and probably shouldn't because |
|
| 5935 | - * this is duplicated effort and reduces efficiency) you would be better to use |
|
| 5936 | - * array_keys() on $model_objects. |
|
| 5937 | - * |
|
| 5938 | - * @param \EE_Base_Class[] $model_objects |
|
| 5939 | - * @param boolean $filter_out_empty_ids if a model object has an ID of '' or 0, don't bother including it |
|
| 5940 | - * in the returned array |
|
| 5941 | - * @return array |
|
| 5942 | - * @throws EE_Error |
|
| 5943 | - */ |
|
| 5944 | - public function get_IDs($model_objects, $filter_out_empty_ids = false) |
|
| 5945 | - { |
|
| 5946 | - if (! $this->has_primary_key_field()) { |
|
| 5947 | - if (WP_DEBUG) { |
|
| 5948 | - EE_Error::add_error( |
|
| 5949 | - __('Trying to get IDs from a model than has no primary key', 'event_espresso'), |
|
| 5950 | - __FILE__, |
|
| 5951 | - __FUNCTION__, |
|
| 5952 | - __LINE__ |
|
| 5953 | - ); |
|
| 5954 | - } |
|
| 5955 | - } |
|
| 5956 | - $IDs = array(); |
|
| 5957 | - foreach ($model_objects as $model_object) { |
|
| 5958 | - $id = $model_object->ID(); |
|
| 5959 | - if (! $id) { |
|
| 5960 | - if ($filter_out_empty_ids) { |
|
| 5961 | - continue; |
|
| 5962 | - } |
|
| 5963 | - if (WP_DEBUG) { |
|
| 5964 | - EE_Error::add_error( |
|
| 5965 | - __( |
|
| 5966 | - 'Called %1$s on a model object that has no ID and so probably hasn\'t been saved to the database', |
|
| 5967 | - 'event_espresso' |
|
| 5968 | - ), |
|
| 5969 | - __FILE__, |
|
| 5970 | - __FUNCTION__, |
|
| 5971 | - __LINE__ |
|
| 5972 | - ); |
|
| 5973 | - } |
|
| 5974 | - } |
|
| 5975 | - $IDs[] = $id; |
|
| 5976 | - } |
|
| 5977 | - return $IDs; |
|
| 5978 | - } |
|
| 5979 | - |
|
| 5980 | - |
|
| 5981 | - |
|
| 5982 | - /** |
|
| 5983 | - * Returns the string used in capabilities relating to this model. If there |
|
| 5984 | - * are no capabilities that relate to this model returns false |
|
| 5985 | - * |
|
| 5986 | - * @return string|false |
|
| 5987 | - */ |
|
| 5988 | - public function cap_slug() |
|
| 5989 | - { |
|
| 5990 | - return apply_filters('FHEE__EEM_Base__cap_slug', $this->_caps_slug, $this); |
|
| 5991 | - } |
|
| 5992 | - |
|
| 5993 | - |
|
| 5994 | - |
|
| 5995 | - /** |
|
| 5996 | - * Returns the capability-restrictions array (@see EEM_Base::_cap_restrictions). |
|
| 5997 | - * If $context is provided (which should be set to one of EEM_Base::valid_cap_contexts()) |
|
| 5998 | - * only returns the cap restrictions array in that context (ie, the array |
|
| 5999 | - * at that key) |
|
| 6000 | - * |
|
| 6001 | - * @param string $context |
|
| 6002 | - * @return EE_Default_Where_Conditions[] indexed by associated capability |
|
| 6003 | - * @throws EE_Error |
|
| 6004 | - */ |
|
| 6005 | - public function cap_restrictions($context = EEM_Base::caps_read) |
|
| 6006 | - { |
|
| 6007 | - EEM_Base::verify_is_valid_cap_context($context); |
|
| 6008 | - //check if we ought to run the restriction generator first |
|
| 6009 | - if ( |
|
| 6010 | - isset($this->_cap_restriction_generators[$context]) |
|
| 6011 | - && $this->_cap_restriction_generators[$context] instanceof EE_Restriction_Generator_Base |
|
| 6012 | - && ! $this->_cap_restriction_generators[$context]->has_generated_cap_restrictions() |
|
| 6013 | - ) { |
|
| 6014 | - $this->_cap_restrictions[$context] = array_merge( |
|
| 6015 | - $this->_cap_restrictions[$context], |
|
| 6016 | - $this->_cap_restriction_generators[$context]->generate_restrictions() |
|
| 6017 | - ); |
|
| 6018 | - } |
|
| 6019 | - //and make sure we've finalized the construction of each restriction |
|
| 6020 | - foreach ($this->_cap_restrictions[$context] as $where_conditions_obj) { |
|
| 6021 | - if ($where_conditions_obj instanceof EE_Default_Where_Conditions) { |
|
| 6022 | - $where_conditions_obj->_finalize_construct($this); |
|
| 6023 | - } |
|
| 6024 | - } |
|
| 6025 | - return $this->_cap_restrictions[$context]; |
|
| 6026 | - } |
|
| 6027 | - |
|
| 6028 | - |
|
| 6029 | - |
|
| 6030 | - /** |
|
| 6031 | - * Indicating whether or not this model thinks its a wp core model |
|
| 6032 | - * |
|
| 6033 | - * @return boolean |
|
| 6034 | - */ |
|
| 6035 | - public function is_wp_core_model() |
|
| 6036 | - { |
|
| 6037 | - return $this->_wp_core_model; |
|
| 6038 | - } |
|
| 6039 | - |
|
| 6040 | - |
|
| 6041 | - |
|
| 6042 | - /** |
|
| 6043 | - * Gets all the caps that are missing which impose a restriction on |
|
| 6044 | - * queries made in this context |
|
| 6045 | - * |
|
| 6046 | - * @param string $context one of EEM_Base::caps_ constants |
|
| 6047 | - * @return EE_Default_Where_Conditions[] indexed by capability name |
|
| 6048 | - * @throws EE_Error |
|
| 6049 | - */ |
|
| 6050 | - public function caps_missing($context = EEM_Base::caps_read) |
|
| 6051 | - { |
|
| 6052 | - $missing_caps = array(); |
|
| 6053 | - $cap_restrictions = $this->cap_restrictions($context); |
|
| 6054 | - foreach ($cap_restrictions as $cap => $restriction_if_no_cap) { |
|
| 6055 | - if (! EE_Capabilities::instance() |
|
| 6056 | - ->current_user_can($cap, $this->get_this_model_name() . '_model_applying_caps') |
|
| 6057 | - ) { |
|
| 6058 | - $missing_caps[$cap] = $restriction_if_no_cap; |
|
| 6059 | - } |
|
| 6060 | - } |
|
| 6061 | - return $missing_caps; |
|
| 6062 | - } |
|
| 6063 | - |
|
| 6064 | - |
|
| 6065 | - |
|
| 6066 | - /** |
|
| 6067 | - * Gets the mapping from capability contexts to action strings used in capability names |
|
| 6068 | - * |
|
| 6069 | - * @return array keys are one of EEM_Base::valid_cap_contexts(), and values are usually |
|
| 6070 | - * one of 'read', 'edit', or 'delete' |
|
| 6071 | - */ |
|
| 6072 | - public function cap_contexts_to_cap_action_map() |
|
| 6073 | - { |
|
| 6074 | - return apply_filters('FHEE__EEM_Base__cap_contexts_to_cap_action_map', $this->_cap_contexts_to_cap_action_map, |
|
| 6075 | - $this); |
|
| 6076 | - } |
|
| 6077 | - |
|
| 6078 | - |
|
| 6079 | - |
|
| 6080 | - /** |
|
| 6081 | - * Gets the action string for the specified capability context |
|
| 6082 | - * |
|
| 6083 | - * @param string $context |
|
| 6084 | - * @return string one of EEM_Base::cap_contexts_to_cap_action_map() values |
|
| 6085 | - * @throws EE_Error |
|
| 6086 | - */ |
|
| 6087 | - public function cap_action_for_context($context) |
|
| 6088 | - { |
|
| 6089 | - $mapping = $this->cap_contexts_to_cap_action_map(); |
|
| 6090 | - if (isset($mapping[$context])) { |
|
| 6091 | - return $mapping[$context]; |
|
| 6092 | - } |
|
| 6093 | - if ($action = apply_filters('FHEE__EEM_Base__cap_action_for_context', null, $this, $mapping, $context)) { |
|
| 6094 | - return $action; |
|
| 6095 | - } |
|
| 6096 | - throw new EE_Error( |
|
| 6097 | - sprintf( |
|
| 6098 | - __('Cannot find capability restrictions for context "%1$s", allowed values are:%2$s', 'event_espresso'), |
|
| 6099 | - $context, |
|
| 6100 | - implode(',', array_keys($this->cap_contexts_to_cap_action_map())) |
|
| 6101 | - ) |
|
| 6102 | - ); |
|
| 6103 | - } |
|
| 6104 | - |
|
| 6105 | - |
|
| 6106 | - |
|
| 6107 | - /** |
|
| 6108 | - * Returns all the capability contexts which are valid when querying models |
|
| 6109 | - * |
|
| 6110 | - * @return array |
|
| 6111 | - */ |
|
| 6112 | - public static function valid_cap_contexts() |
|
| 6113 | - { |
|
| 6114 | - return apply_filters('FHEE__EEM_Base__valid_cap_contexts', array( |
|
| 6115 | - self::caps_read, |
|
| 6116 | - self::caps_read_admin, |
|
| 6117 | - self::caps_edit, |
|
| 6118 | - self::caps_delete, |
|
| 6119 | - )); |
|
| 6120 | - } |
|
| 6121 | - |
|
| 6122 | - |
|
| 6123 | - |
|
| 6124 | - /** |
|
| 6125 | - * Returns all valid options for 'default_where_conditions' |
|
| 6126 | - * |
|
| 6127 | - * @return array |
|
| 6128 | - */ |
|
| 6129 | - public static function valid_default_where_conditions() |
|
| 6130 | - { |
|
| 6131 | - return array( |
|
| 6132 | - EEM_Base::default_where_conditions_all, |
|
| 6133 | - EEM_Base::default_where_conditions_this_only, |
|
| 6134 | - EEM_Base::default_where_conditions_others_only, |
|
| 6135 | - EEM_Base::default_where_conditions_minimum_all, |
|
| 6136 | - EEM_Base::default_where_conditions_minimum_others, |
|
| 6137 | - EEM_Base::default_where_conditions_none |
|
| 6138 | - ); |
|
| 6139 | - } |
|
| 6140 | - |
|
| 6141 | - // public static function default_where_conditions_full |
|
| 6142 | - /** |
|
| 6143 | - * Verifies $context is one of EEM_Base::valid_cap_contexts(), if not it throws an exception |
|
| 6144 | - * |
|
| 6145 | - * @param string $context |
|
| 6146 | - * @return bool |
|
| 6147 | - * @throws EE_Error |
|
| 6148 | - */ |
|
| 6149 | - static public function verify_is_valid_cap_context($context) |
|
| 6150 | - { |
|
| 6151 | - $valid_cap_contexts = EEM_Base::valid_cap_contexts(); |
|
| 6152 | - if (in_array($context, $valid_cap_contexts)) { |
|
| 6153 | - return true; |
|
| 6154 | - } |
|
| 6155 | - throw new EE_Error( |
|
| 6156 | - sprintf( |
|
| 6157 | - __( |
|
| 6158 | - 'Context "%1$s" passed into model "%2$s" is not a valid context. They are: %3$s', |
|
| 6159 | - 'event_espresso' |
|
| 6160 | - ), |
|
| 6161 | - $context, |
|
| 6162 | - 'EEM_Base', |
|
| 6163 | - implode(',', $valid_cap_contexts) |
|
| 6164 | - ) |
|
| 6165 | - ); |
|
| 6166 | - } |
|
| 6167 | - |
|
| 6168 | - |
|
| 6169 | - |
|
| 6170 | - /** |
|
| 6171 | - * Clears all the models field caches. This is only useful when a sub-class |
|
| 6172 | - * might have added a field or something and these caches might be invalidated |
|
| 6173 | - */ |
|
| 6174 | - protected function _invalidate_field_caches() |
|
| 6175 | - { |
|
| 6176 | - $this->_cache_foreign_key_to_fields = array(); |
|
| 6177 | - $this->_cached_fields = null; |
|
| 6178 | - $this->_cached_fields_non_db_only = null; |
|
| 6179 | - } |
|
| 6180 | - |
|
| 6181 | - |
|
| 6182 | - |
|
| 6183 | - /** |
|
| 6184 | - * Gets the list of all the where query param keys that relate to logic instead of field names |
|
| 6185 | - * (eg "and", "or", "not"). |
|
| 6186 | - * |
|
| 6187 | - * @return array |
|
| 6188 | - */ |
|
| 6189 | - public function logic_query_param_keys() |
|
| 6190 | - { |
|
| 6191 | - return $this->_logic_query_param_keys; |
|
| 6192 | - } |
|
| 6193 | - |
|
| 6194 | - |
|
| 6195 | - |
|
| 6196 | - /** |
|
| 6197 | - * Determines whether or not the where query param array key is for a logic query param. |
|
| 6198 | - * Eg 'OR', 'not*', and 'and*because-i-say-so' should all return true, whereas |
|
| 6199 | - * 'ATT_fname', 'EVT_name*not-you-or-me', and 'ORG_name' should return false |
|
| 6200 | - * |
|
| 6201 | - * @param $query_param_key |
|
| 6202 | - * @return bool |
|
| 6203 | - */ |
|
| 6204 | - public function is_logic_query_param_key($query_param_key) |
|
| 6205 | - { |
|
| 6206 | - foreach ($this->logic_query_param_keys() as $logic_query_param_key) { |
|
| 6207 | - if ($query_param_key === $logic_query_param_key |
|
| 6208 | - || strpos($query_param_key, $logic_query_param_key . '*') === 0 |
|
| 6209 | - ) { |
|
| 6210 | - return true; |
|
| 6211 | - } |
|
| 6212 | - } |
|
| 6213 | - return false; |
|
| 6214 | - } |
|
| 3773 | + } |
|
| 3774 | + return $null_friendly_where_conditions; |
|
| 3775 | + } |
|
| 3776 | + |
|
| 3777 | + |
|
| 3778 | + |
|
| 3779 | + /** |
|
| 3780 | + * Uses the _default_where_conditions_strategy set during __construct() to get |
|
| 3781 | + * default where conditions on all get_all, update, and delete queries done by this model. |
|
| 3782 | + * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
| 3783 | + * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
| 3784 | + * |
|
| 3785 | + * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
| 3786 | + * @return array like EEM_Base::get_all's $query_params[0] (where conditions) |
|
| 3787 | + */ |
|
| 3788 | + private function _get_default_where_conditions($model_relation_path = null) |
|
| 3789 | + { |
|
| 3790 | + if ($this->_ignore_where_strategy) { |
|
| 3791 | + return array(); |
|
| 3792 | + } |
|
| 3793 | + return $this->_default_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
| 3794 | + } |
|
| 3795 | + |
|
| 3796 | + |
|
| 3797 | + |
|
| 3798 | + /** |
|
| 3799 | + * Uses the _minimum_where_conditions_strategy set during __construct() to get |
|
| 3800 | + * minimum where conditions on all get_all, update, and delete queries done by this model. |
|
| 3801 | + * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
|
| 3802 | + * NOT array('Event_CPT.post_type'=>'esp_event'). |
|
| 3803 | + * Similar to _get_default_where_conditions |
|
| 3804 | + * |
|
| 3805 | + * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
|
| 3806 | + * @return array like EEM_Base::get_all's $query_params[0] (where conditions) |
|
| 3807 | + */ |
|
| 3808 | + protected function _get_minimum_where_conditions($model_relation_path = null) |
|
| 3809 | + { |
|
| 3810 | + if ($this->_ignore_where_strategy) { |
|
| 3811 | + return array(); |
|
| 3812 | + } |
|
| 3813 | + return $this->_minimum_where_conditions_strategy->get_default_where_conditions($model_relation_path); |
|
| 3814 | + } |
|
| 3815 | + |
|
| 3816 | + |
|
| 3817 | + |
|
| 3818 | + /** |
|
| 3819 | + * Creates the string of SQL for the select part of a select query, everything behind SELECT and before FROM. |
|
| 3820 | + * Eg, "Event.post_id, Event.post_name,Event_Detail.EVT_ID..." |
|
| 3821 | + * |
|
| 3822 | + * @param EE_Model_Query_Info_Carrier $model_query_info |
|
| 3823 | + * @return string |
|
| 3824 | + * @throws EE_Error |
|
| 3825 | + */ |
|
| 3826 | + private function _construct_default_select_sql(EE_Model_Query_Info_Carrier $model_query_info) |
|
| 3827 | + { |
|
| 3828 | + $selects = $this->_get_columns_to_select_for_this_model(); |
|
| 3829 | + foreach ( |
|
| 3830 | + $model_query_info->get_model_names_included() as $model_relation_chain => |
|
| 3831 | + $name_of_other_model_included |
|
| 3832 | + ) { |
|
| 3833 | + $other_model_included = $this->get_related_model_obj($name_of_other_model_included); |
|
| 3834 | + $other_model_selects = $other_model_included->_get_columns_to_select_for_this_model($model_relation_chain); |
|
| 3835 | + foreach ($other_model_selects as $key => $value) { |
|
| 3836 | + $selects[] = $value; |
|
| 3837 | + } |
|
| 3838 | + } |
|
| 3839 | + return implode(", ", $selects); |
|
| 3840 | + } |
|
| 3841 | + |
|
| 3842 | + |
|
| 3843 | + |
|
| 3844 | + /** |
|
| 3845 | + * Gets an array of columns to select for this model, which are necessary for it to create its objects. |
|
| 3846 | + * So that's going to be the columns for all the fields on the model |
|
| 3847 | + * |
|
| 3848 | + * @param string $model_relation_chain like 'Question.Question_Group.Event' |
|
| 3849 | + * @return array numerically indexed, values are columns to select and rename, eg "Event.ID AS 'Event.ID'" |
|
| 3850 | + */ |
|
| 3851 | + public function _get_columns_to_select_for_this_model($model_relation_chain = '') |
|
| 3852 | + { |
|
| 3853 | + $fields = $this->field_settings(); |
|
| 3854 | + $selects = array(); |
|
| 3855 | + $table_alias_with_model_relation_chain_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, |
|
| 3856 | + $this->get_this_model_name()); |
|
| 3857 | + foreach ($fields as $field_obj) { |
|
| 3858 | + $selects[] = $table_alias_with_model_relation_chain_prefix |
|
| 3859 | + . $field_obj->get_table_alias() |
|
| 3860 | + . "." |
|
| 3861 | + . $field_obj->get_table_column() |
|
| 3862 | + . " AS '" |
|
| 3863 | + . $table_alias_with_model_relation_chain_prefix |
|
| 3864 | + . $field_obj->get_table_alias() |
|
| 3865 | + . "." |
|
| 3866 | + . $field_obj->get_table_column() |
|
| 3867 | + . "'"; |
|
| 3868 | + } |
|
| 3869 | + //make sure we are also getting the PKs of each table |
|
| 3870 | + $tables = $this->get_tables(); |
|
| 3871 | + if (count($tables) > 1) { |
|
| 3872 | + foreach ($tables as $table_obj) { |
|
| 3873 | + $qualified_pk_column = $table_alias_with_model_relation_chain_prefix |
|
| 3874 | + . $table_obj->get_fully_qualified_pk_column(); |
|
| 3875 | + if (! in_array($qualified_pk_column, $selects)) { |
|
| 3876 | + $selects[] = "$qualified_pk_column AS '$qualified_pk_column'"; |
|
| 3877 | + } |
|
| 3878 | + } |
|
| 3879 | + } |
|
| 3880 | + return $selects; |
|
| 3881 | + } |
|
| 3882 | + |
|
| 3883 | + |
|
| 3884 | + |
|
| 3885 | + /** |
|
| 3886 | + * Given a $query_param like 'Registration.Transaction.TXN_ID', pops off 'Registration.', |
|
| 3887 | + * gets the join statement for it; gets the data types for it; and passes the remaining 'Transaction.TXN_ID' |
|
| 3888 | + * onto its related Transaction object to do the same. Returns an EE_Join_And_Data_Types object which contains the |
|
| 3889 | + * SQL for joining, and the data types |
|
| 3890 | + * |
|
| 3891 | + * @param null|string $original_query_param |
|
| 3892 | + * @param string $query_param like Registration.Transaction.TXN_ID |
|
| 3893 | + * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
| 3894 | + * @param string $query_param_type like Registration.Transaction.TXN_ID |
|
| 3895 | + * or 'PAY_ID'. Otherwise, we don't expect there to be a |
|
| 3896 | + * column name. We only want model names, eg 'Event.Venue' |
|
| 3897 | + * or 'Registration's |
|
| 3898 | + * @param string $original_query_param what it originally was (eg |
|
| 3899 | + * Registration.Transaction.TXN_ID). If null, we assume it |
|
| 3900 | + * matches $query_param |
|
| 3901 | + * @throws EE_Error |
|
| 3902 | + * @return void only modifies the EEM_Related_Model_Info_Carrier passed into it |
|
| 3903 | + */ |
|
| 3904 | + private function _extract_related_model_info_from_query_param( |
|
| 3905 | + $query_param, |
|
| 3906 | + EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
| 3907 | + $query_param_type, |
|
| 3908 | + $original_query_param = null |
|
| 3909 | + ) { |
|
| 3910 | + if ($original_query_param === null) { |
|
| 3911 | + $original_query_param = $query_param; |
|
| 3912 | + } |
|
| 3913 | + $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param); |
|
| 3914 | + /** @var $allow_logic_query_params bool whether or not to allow logic_query_params like 'NOT','OR', or 'AND' */ |
|
| 3915 | + $allow_logic_query_params = in_array($query_param_type, array('where', 'having', 0, 'custom_selects'), true); |
|
| 3916 | + $allow_fields = in_array( |
|
| 3917 | + $query_param_type, |
|
| 3918 | + array('where', 'having', 'order_by', 'group_by', 'order', 'custom_selects', 0), |
|
| 3919 | + true |
|
| 3920 | + ); |
|
| 3921 | + //check to see if we have a field on this model |
|
| 3922 | + $this_model_fields = $this->field_settings(true); |
|
| 3923 | + if (array_key_exists($query_param, $this_model_fields)) { |
|
| 3924 | + if ($allow_fields) { |
|
| 3925 | + return; |
|
| 3926 | + } |
|
| 3927 | + throw new EE_Error( |
|
| 3928 | + sprintf( |
|
| 3929 | + __( |
|
| 3930 | + "Using a field name (%s) on model %s is not allowed on this query param type '%s'. Original query param was %s", |
|
| 3931 | + "event_espresso" |
|
| 3932 | + ), |
|
| 3933 | + $query_param, get_class($this), $query_param_type, $original_query_param |
|
| 3934 | + ) |
|
| 3935 | + ); |
|
| 3936 | + } |
|
| 3937 | + //check if this is a special logic query param |
|
| 3938 | + if (in_array($query_param, $this->_logic_query_param_keys, true)) { |
|
| 3939 | + if ($allow_logic_query_params) { |
|
| 3940 | + return; |
|
| 3941 | + } |
|
| 3942 | + throw new EE_Error( |
|
| 3943 | + sprintf( |
|
| 3944 | + __( |
|
| 3945 | + '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', |
|
| 3946 | + 'event_espresso' |
|
| 3947 | + ), |
|
| 3948 | + implode('", "', $this->_logic_query_param_keys), |
|
| 3949 | + $query_param, |
|
| 3950 | + get_class($this), |
|
| 3951 | + '<br />', |
|
| 3952 | + "\t" |
|
| 3953 | + . ' $passed_in_query_info = <pre>' |
|
| 3954 | + . print_r($passed_in_query_info, true) |
|
| 3955 | + . '</pre>' |
|
| 3956 | + . "\n\t" |
|
| 3957 | + . ' $query_param_type = ' |
|
| 3958 | + . $query_param_type |
|
| 3959 | + . "\n\t" |
|
| 3960 | + . ' $original_query_param = ' |
|
| 3961 | + . $original_query_param |
|
| 3962 | + ) |
|
| 3963 | + ); |
|
| 3964 | + } |
|
| 3965 | + //check if it's a custom selection |
|
| 3966 | + if ($this->_custom_selections instanceof CustomSelects |
|
| 3967 | + && in_array($query_param, $this->_custom_selections->columnAliases(), true) |
|
| 3968 | + ) { |
|
| 3969 | + return; |
|
| 3970 | + } |
|
| 3971 | + //check if has a model name at the beginning |
|
| 3972 | + //and |
|
| 3973 | + //check if it's a field on a related model |
|
| 3974 | + if ($this->extractJoinModelFromQueryParams( |
|
| 3975 | + $passed_in_query_info, |
|
| 3976 | + $query_param, |
|
| 3977 | + $original_query_param, |
|
| 3978 | + $query_param_type |
|
| 3979 | + )) { |
|
| 3980 | + return; |
|
| 3981 | + } |
|
| 3982 | + |
|
| 3983 | + //ok so $query_param didn't start with a model name |
|
| 3984 | + //and we previously confirmed it wasn't a logic query param or field on the current model |
|
| 3985 | + //it's wack, that's what it is |
|
| 3986 | + throw new EE_Error( |
|
| 3987 | + sprintf( |
|
| 3988 | + esc_html__( |
|
| 3989 | + "There is no model named '%s' related to %s. Query param type is %s and original query param is %s", |
|
| 3990 | + "event_espresso" |
|
| 3991 | + ), |
|
| 3992 | + $query_param, |
|
| 3993 | + get_class($this), |
|
| 3994 | + $query_param_type, |
|
| 3995 | + $original_query_param |
|
| 3996 | + ) |
|
| 3997 | + ); |
|
| 3998 | + } |
|
| 3999 | + |
|
| 4000 | + |
|
| 4001 | + /** |
|
| 4002 | + * Extracts any possible join model information from the provided possible_join_string. |
|
| 4003 | + * This method will read the provided $possible_join_string value and determine if there are any possible model join |
|
| 4004 | + * parts that should be added to the query. |
|
| 4005 | + * |
|
| 4006 | + * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
| 4007 | + * @param string $possible_join_string Such as Registration.REG_ID, or Registration |
|
| 4008 | + * @param null|string $original_query_param |
|
| 4009 | + * @param string $query_parameter_type The type for the source of the $possible_join_string |
|
| 4010 | + * ('where', 'order_by', 'group_by', 'custom_selects' etc.) |
|
| 4011 | + * @return bool returns true if a join was added and false if not. |
|
| 4012 | + * @throws EE_Error |
|
| 4013 | + */ |
|
| 4014 | + private function extractJoinModelFromQueryParams( |
|
| 4015 | + EE_Model_Query_Info_Carrier $query_info_carrier, |
|
| 4016 | + $possible_join_string, |
|
| 4017 | + $original_query_param, |
|
| 4018 | + $query_parameter_type |
|
| 4019 | + ) { |
|
| 4020 | + foreach ($this->_model_relations as $valid_related_model_name => $relation_obj) { |
|
| 4021 | + if (strpos($possible_join_string, $valid_related_model_name . ".") === 0) { |
|
| 4022 | + $this->_add_join_to_model($valid_related_model_name, $query_info_carrier, $original_query_param); |
|
| 4023 | + $possible_join_string = substr($possible_join_string, strlen($valid_related_model_name . ".")); |
|
| 4024 | + if ($possible_join_string === '') { |
|
| 4025 | + //nothing left to $query_param |
|
| 4026 | + //we should actually end in a field name, not a model like this! |
|
| 4027 | + throw new EE_Error( |
|
| 4028 | + sprintf( |
|
| 4029 | + esc_html__( |
|
| 4030 | + "Query param '%s' (of type %s on model %s) shouldn't end on a period (.) ", |
|
| 4031 | + "event_espresso" |
|
| 4032 | + ), |
|
| 4033 | + $possible_join_string, |
|
| 4034 | + $query_parameter_type, |
|
| 4035 | + get_class($this), |
|
| 4036 | + $valid_related_model_name |
|
| 4037 | + ) |
|
| 4038 | + ); |
|
| 4039 | + } |
|
| 4040 | + $related_model_obj = $this->get_related_model_obj($valid_related_model_name); |
|
| 4041 | + $related_model_obj->_extract_related_model_info_from_query_param( |
|
| 4042 | + $possible_join_string, |
|
| 4043 | + $query_info_carrier, |
|
| 4044 | + $query_parameter_type, |
|
| 4045 | + $original_query_param |
|
| 4046 | + ); |
|
| 4047 | + return true; |
|
| 4048 | + } |
|
| 4049 | + if ($possible_join_string === $valid_related_model_name) { |
|
| 4050 | + $this->_add_join_to_model( |
|
| 4051 | + $valid_related_model_name, |
|
| 4052 | + $query_info_carrier, |
|
| 4053 | + $original_query_param |
|
| 4054 | + ); |
|
| 4055 | + return true; |
|
| 4056 | + } |
|
| 4057 | + } |
|
| 4058 | + return false; |
|
| 4059 | + } |
|
| 4060 | + |
|
| 4061 | + |
|
| 4062 | + /** |
|
| 4063 | + * Extracts related models from Custom Selects and sets up any joins for those related models. |
|
| 4064 | + * @param EE_Model_Query_Info_Carrier $query_info_carrier |
|
| 4065 | + * @throws EE_Error |
|
| 4066 | + */ |
|
| 4067 | + private function extractRelatedModelsFromCustomSelects(EE_Model_Query_Info_Carrier $query_info_carrier) |
|
| 4068 | + { |
|
| 4069 | + if ($this->_custom_selections instanceof CustomSelects |
|
| 4070 | + && ($this->_custom_selections->type() === CustomSelects::TYPE_STRUCTURED |
|
| 4071 | + || $this->_custom_selections->type() == CustomSelects::TYPE_COMPLEX |
|
| 4072 | + ) |
|
| 4073 | + ) { |
|
| 4074 | + $original_selects = $this->_custom_selections->originalSelects(); |
|
| 4075 | + foreach ($original_selects as $alias => $select_configuration) { |
|
| 4076 | + $this->extractJoinModelFromQueryParams( |
|
| 4077 | + $query_info_carrier, |
|
| 4078 | + $select_configuration[0], |
|
| 4079 | + $select_configuration[0], |
|
| 4080 | + 'custom_selects' |
|
| 4081 | + ); |
|
| 4082 | + } |
|
| 4083 | + } |
|
| 4084 | + } |
|
| 4085 | + |
|
| 4086 | + |
|
| 4087 | + |
|
| 4088 | + /** |
|
| 4089 | + * Privately used by _extract_related_model_info_from_query_param to add a join to $model_name |
|
| 4090 | + * and store it on $passed_in_query_info |
|
| 4091 | + * |
|
| 4092 | + * @param string $model_name |
|
| 4093 | + * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
|
| 4094 | + * @param string $original_query_param used to extract the relation chain between the queried |
|
| 4095 | + * model and $model_name. Eg, if we are querying Event, |
|
| 4096 | + * and are adding a join to 'Payment' with the original |
|
| 4097 | + * query param key |
|
| 4098 | + * 'Registration.Transaction.Payment.PAY_amount', we want |
|
| 4099 | + * to extract 'Registration.Transaction.Payment', in case |
|
| 4100 | + * Payment wants to add default query params so that it |
|
| 4101 | + * will know what models to prepend onto its default query |
|
| 4102 | + * params or in case it wants to rename tables (in case |
|
| 4103 | + * there are multiple joins to the same table) |
|
| 4104 | + * @return void |
|
| 4105 | + * @throws EE_Error |
|
| 4106 | + */ |
|
| 4107 | + private function _add_join_to_model( |
|
| 4108 | + $model_name, |
|
| 4109 | + EE_Model_Query_Info_Carrier $passed_in_query_info, |
|
| 4110 | + $original_query_param |
|
| 4111 | + ) { |
|
| 4112 | + $relation_obj = $this->related_settings_for($model_name); |
|
| 4113 | + $model_relation_chain = EE_Model_Parser::extract_model_relation_chain($model_name, $original_query_param); |
|
| 4114 | + //check if the relation is HABTM, because then we're essentially doing two joins |
|
| 4115 | + //If so, join first to the JOIN table, and add its data types, and then continue as normal |
|
| 4116 | + if ($relation_obj instanceof EE_HABTM_Relation) { |
|
| 4117 | + $join_model_obj = $relation_obj->get_join_model(); |
|
| 4118 | + //replace the model specified with the join model for this relation chain, whi |
|
| 4119 | + $relation_chain_to_join_model = EE_Model_Parser::replace_model_name_with_join_model_name_in_model_relation_chain($model_name, |
|
| 4120 | + $join_model_obj->get_this_model_name(), $model_relation_chain); |
|
| 4121 | + $passed_in_query_info->merge( |
|
| 4122 | + new EE_Model_Query_Info_Carrier( |
|
| 4123 | + array($relation_chain_to_join_model => $join_model_obj->get_this_model_name()), |
|
| 4124 | + $relation_obj->get_join_to_intermediate_model_statement($relation_chain_to_join_model) |
|
| 4125 | + ) |
|
| 4126 | + ); |
|
| 4127 | + } |
|
| 4128 | + //now just join to the other table pointed to by the relation object, and add its data types |
|
| 4129 | + $passed_in_query_info->merge( |
|
| 4130 | + new EE_Model_Query_Info_Carrier( |
|
| 4131 | + array($model_relation_chain => $model_name), |
|
| 4132 | + $relation_obj->get_join_statement($model_relation_chain) |
|
| 4133 | + ) |
|
| 4134 | + ); |
|
| 4135 | + } |
|
| 4136 | + |
|
| 4137 | + |
|
| 4138 | + |
|
| 4139 | + /** |
|
| 4140 | + * Constructs SQL for where clause, like "WHERE Event.ID = 23 AND Transaction.amount > 100" etc. |
|
| 4141 | + * |
|
| 4142 | + * @param array $where_params like EEM_Base::get_all |
|
| 4143 | + * @return string of SQL |
|
| 4144 | + * @throws EE_Error |
|
| 4145 | + */ |
|
| 4146 | + private function _construct_where_clause($where_params) |
|
| 4147 | + { |
|
| 4148 | + $SQL = $this->_construct_condition_clause_recursive($where_params, ' AND '); |
|
| 4149 | + if ($SQL) { |
|
| 4150 | + return " WHERE " . $SQL; |
|
| 4151 | + } |
|
| 4152 | + return ''; |
|
| 4153 | + } |
|
| 4154 | + |
|
| 4155 | + |
|
| 4156 | + |
|
| 4157 | + /** |
|
| 4158 | + * Just like the _construct_where_clause, except prepends 'HAVING' instead of 'WHERE', |
|
| 4159 | + * and should be passed HAVING parameters, not WHERE parameters |
|
| 4160 | + * |
|
| 4161 | + * @param array $having_params |
|
| 4162 | + * @return string |
|
| 4163 | + * @throws EE_Error |
|
| 4164 | + */ |
|
| 4165 | + private function _construct_having_clause($having_params) |
|
| 4166 | + { |
|
| 4167 | + $SQL = $this->_construct_condition_clause_recursive($having_params, ' AND '); |
|
| 4168 | + if ($SQL) { |
|
| 4169 | + return " HAVING " . $SQL; |
|
| 4170 | + } |
|
| 4171 | + return ''; |
|
| 4172 | + } |
|
| 4173 | + |
|
| 4174 | + |
|
| 4175 | + /** |
|
| 4176 | + * Used for creating nested WHERE conditions. Eg "WHERE ! (Event.ID = 3 OR ( Event_Meta.meta_key = 'bob' AND |
|
| 4177 | + * Event_Meta.meta_value = 'foo'))" |
|
| 4178 | + * |
|
| 4179 | + * @param array $where_params see EEM_Base::get_all for documentation |
|
| 4180 | + * @param string $glue joins each subclause together. Should really only be " AND " or " OR "... |
|
| 4181 | + * @throws EE_Error |
|
| 4182 | + * @return string of SQL |
|
| 4183 | + */ |
|
| 4184 | + private function _construct_condition_clause_recursive($where_params, $glue = ' AND') |
|
| 4185 | + { |
|
| 4186 | + $where_clauses = array(); |
|
| 4187 | + foreach ($where_params as $query_param => $op_and_value_or_sub_condition) { |
|
| 4188 | + $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param);//str_replace("*",'',$query_param); |
|
| 4189 | + if (in_array($query_param, $this->_logic_query_param_keys)) { |
|
| 4190 | + switch ($query_param) { |
|
| 4191 | + case 'not': |
|
| 4192 | + case 'NOT': |
|
| 4193 | + $where_clauses[] = "! (" |
|
| 4194 | + . $this->_construct_condition_clause_recursive($op_and_value_or_sub_condition, |
|
| 4195 | + $glue) |
|
| 4196 | + . ")"; |
|
| 4197 | + break; |
|
| 4198 | + case 'and': |
|
| 4199 | + case 'AND': |
|
| 4200 | + $where_clauses[] = " (" |
|
| 4201 | + . $this->_construct_condition_clause_recursive($op_and_value_or_sub_condition, |
|
| 4202 | + ' AND ') |
|
| 4203 | + . ")"; |
|
| 4204 | + break; |
|
| 4205 | + case 'or': |
|
| 4206 | + case 'OR': |
|
| 4207 | + $where_clauses[] = " (" |
|
| 4208 | + . $this->_construct_condition_clause_recursive($op_and_value_or_sub_condition, |
|
| 4209 | + ' OR ') |
|
| 4210 | + . ")"; |
|
| 4211 | + break; |
|
| 4212 | + } |
|
| 4213 | + } else { |
|
| 4214 | + $field_obj = $this->_deduce_field_from_query_param($query_param); |
|
| 4215 | + //if it's not a normal field, maybe it's a custom selection? |
|
| 4216 | + if (! $field_obj) { |
|
| 4217 | + if ($this->_custom_selections instanceof CustomSelects) { |
|
| 4218 | + $field_obj = $this->_custom_selections->getDataTypeForAlias($query_param); |
|
| 4219 | + } else { |
|
| 4220 | + throw new EE_Error(sprintf(__("%s is neither a valid model field name, nor a custom selection", |
|
| 4221 | + "event_espresso"), $query_param)); |
|
| 4222 | + } |
|
| 4223 | + } |
|
| 4224 | + $op_and_value_sql = $this->_construct_op_and_value($op_and_value_or_sub_condition, $field_obj); |
|
| 4225 | + $where_clauses[] = $this->_deduce_column_name_from_query_param($query_param) . SP . $op_and_value_sql; |
|
| 4226 | + } |
|
| 4227 | + } |
|
| 4228 | + return $where_clauses ? implode($glue, $where_clauses) : ''; |
|
| 4229 | + } |
|
| 4230 | + |
|
| 4231 | + |
|
| 4232 | + |
|
| 4233 | + /** |
|
| 4234 | + * Takes the input parameter and extract the table name (alias) and column name |
|
| 4235 | + * |
|
| 4236 | + * @param string $query_param like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
| 4237 | + * @throws EE_Error |
|
| 4238 | + * @return string table alias and column name for SQL, eg "Transaction.TXN_ID" |
|
| 4239 | + */ |
|
| 4240 | + private function _deduce_column_name_from_query_param($query_param) |
|
| 4241 | + { |
|
| 4242 | + $field = $this->_deduce_field_from_query_param($query_param); |
|
| 4243 | + if ($field) { |
|
| 4244 | + $table_alias_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_from_query_param($field->get_model_name(), |
|
| 4245 | + $query_param); |
|
| 4246 | + return $table_alias_prefix . $field->get_qualified_column(); |
|
| 4247 | + } |
|
| 4248 | + if ($this->_custom_selections instanceof CustomSelects |
|
| 4249 | + && in_array($query_param, $this->_custom_selections->columnAliases(), true) |
|
| 4250 | + ) { |
|
| 4251 | + //maybe it's custom selection item? |
|
| 4252 | + //if so, just use it as the "column name" |
|
| 4253 | + return $query_param; |
|
| 4254 | + } |
|
| 4255 | + $custom_select_aliases = $this->_custom_selections instanceof CustomSelects |
|
| 4256 | + ? implode(',', $this->_custom_selections->columnAliases()) |
|
| 4257 | + : ''; |
|
| 4258 | + throw new EE_Error( |
|
| 4259 | + sprintf( |
|
| 4260 | + __( |
|
| 4261 | + "%s is not a valid field on this model, nor a custom selection (%s)", |
|
| 4262 | + "event_espresso" |
|
| 4263 | + ), $query_param, $custom_select_aliases |
|
| 4264 | + ) |
|
| 4265 | + ); |
|
| 4266 | + } |
|
| 4267 | + |
|
| 4268 | + |
|
| 4269 | + |
|
| 4270 | + /** |
|
| 4271 | + * Removes the * and anything after it from the condition query param key. It is useful to add the * to condition |
|
| 4272 | + * query param keys (eg, 'OR*', 'EVT_ID') in order for the array keys to still be unique, so that they don't get |
|
| 4273 | + * overwritten Takes a string like 'Event.EVT_ID*', 'TXN_total**', 'OR*1st', and 'DTT_reg_start*foobar' to |
|
| 4274 | + * 'Event.EVT_ID', 'TXN_total', 'OR', and 'DTT_reg_start', respectively. |
|
| 4275 | + * |
|
| 4276 | + * @param string $condition_query_param_key |
|
| 4277 | + * @return string |
|
| 4278 | + */ |
|
| 4279 | + private function _remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
| 4280 | + { |
|
| 4281 | + $pos_of_star = strpos($condition_query_param_key, '*'); |
|
| 4282 | + if ($pos_of_star === false) { |
|
| 4283 | + return $condition_query_param_key; |
|
| 4284 | + } |
|
| 4285 | + $condition_query_param_sans_star = substr($condition_query_param_key, 0, $pos_of_star); |
|
| 4286 | + return $condition_query_param_sans_star; |
|
| 4287 | + } |
|
| 4288 | + |
|
| 4289 | + |
|
| 4290 | + |
|
| 4291 | + /** |
|
| 4292 | + * creates the SQL for the operator and the value in a WHERE clause, eg "< 23" or "LIKE '%monkey%'" |
|
| 4293 | + * |
|
| 4294 | + * @param mixed array | string $op_and_value |
|
| 4295 | + * @param EE_Model_Field_Base|string $field_obj . If string, should be one of EEM_Base::_valid_wpdb_data_types |
|
| 4296 | + * @throws EE_Error |
|
| 4297 | + * @return string |
|
| 4298 | + */ |
|
| 4299 | + private function _construct_op_and_value($op_and_value, $field_obj) |
|
| 4300 | + { |
|
| 4301 | + if (is_array($op_and_value)) { |
|
| 4302 | + $operator = isset($op_and_value[0]) ? $this->_prepare_operator_for_sql($op_and_value[0]) : null; |
|
| 4303 | + if (! $operator) { |
|
| 4304 | + $php_array_like_string = array(); |
|
| 4305 | + foreach ($op_and_value as $key => $value) { |
|
| 4306 | + $php_array_like_string[] = "$key=>$value"; |
|
| 4307 | + } |
|
| 4308 | + throw new EE_Error( |
|
| 4309 | + sprintf( |
|
| 4310 | + __( |
|
| 4311 | + "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))", |
|
| 4312 | + "event_espresso" |
|
| 4313 | + ), |
|
| 4314 | + implode(",", $php_array_like_string) |
|
| 4315 | + ) |
|
| 4316 | + ); |
|
| 4317 | + } |
|
| 4318 | + $value = isset($op_and_value[1]) ? $op_and_value[1] : null; |
|
| 4319 | + } else { |
|
| 4320 | + $operator = '='; |
|
| 4321 | + $value = $op_and_value; |
|
| 4322 | + } |
|
| 4323 | + //check to see if the value is actually another field |
|
| 4324 | + if (is_array($op_and_value) && isset($op_and_value[2]) && $op_and_value[2] == true) { |
|
| 4325 | + return $operator . SP . $this->_deduce_column_name_from_query_param($value); |
|
| 4326 | + } |
|
| 4327 | + if (in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
| 4328 | + //in this case, the value should be an array, or at least a comma-separated list |
|
| 4329 | + //it will need to handle a little differently |
|
| 4330 | + $cleaned_value = $this->_construct_in_value($value, $field_obj); |
|
| 4331 | + //note: $cleaned_value has already been run through $wpdb->prepare() |
|
| 4332 | + return $operator . SP . $cleaned_value; |
|
| 4333 | + } |
|
| 4334 | + if (in_array($operator, $this->valid_between_style_operators()) && is_array($value)) { |
|
| 4335 | + //the value should be an array with count of two. |
|
| 4336 | + if (count($value) !== 2) { |
|
| 4337 | + throw new EE_Error( |
|
| 4338 | + sprintf( |
|
| 4339 | + __( |
|
| 4340 | + "The '%s' operator must be used with an array of values and there must be exactly TWO values in that array.", |
|
| 4341 | + 'event_espresso' |
|
| 4342 | + ), |
|
| 4343 | + "BETWEEN" |
|
| 4344 | + ) |
|
| 4345 | + ); |
|
| 4346 | + } |
|
| 4347 | + $cleaned_value = $this->_construct_between_value($value, $field_obj); |
|
| 4348 | + return $operator . SP . $cleaned_value; |
|
| 4349 | + } |
|
| 4350 | + if (in_array($operator, $this->valid_null_style_operators())) { |
|
| 4351 | + if ($value !== null) { |
|
| 4352 | + throw new EE_Error( |
|
| 4353 | + sprintf( |
|
| 4354 | + __( |
|
| 4355 | + "You attempted to give a value (%s) while using a NULL-style operator (%s). That isn't valid", |
|
| 4356 | + "event_espresso" |
|
| 4357 | + ), |
|
| 4358 | + $value, |
|
| 4359 | + $operator |
|
| 4360 | + ) |
|
| 4361 | + ); |
|
| 4362 | + } |
|
| 4363 | + return $operator; |
|
| 4364 | + } |
|
| 4365 | + if (in_array($operator, $this->valid_like_style_operators()) && ! is_array($value)) { |
|
| 4366 | + //if the operator is 'LIKE', we want to allow percent signs (%) and not |
|
| 4367 | + //remove other junk. So just treat it as a string. |
|
| 4368 | + return $operator . SP . $this->_wpdb_prepare_using_field($value, '%s'); |
|
| 4369 | + } |
|
| 4370 | + if (! in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
| 4371 | + return $operator . SP . $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4372 | + } |
|
| 4373 | + if (in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
| 4374 | + throw new EE_Error( |
|
| 4375 | + sprintf( |
|
| 4376 | + __( |
|
| 4377 | + "Operator '%s' must be used with an array of values, eg 'Registration.REG_ID' => array('%s',array(1,2,3))", |
|
| 4378 | + 'event_espresso' |
|
| 4379 | + ), |
|
| 4380 | + $operator, |
|
| 4381 | + $operator |
|
| 4382 | + ) |
|
| 4383 | + ); |
|
| 4384 | + } |
|
| 4385 | + if (! in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
| 4386 | + throw new EE_Error( |
|
| 4387 | + sprintf( |
|
| 4388 | + __( |
|
| 4389 | + "Operator '%s' must be used with a single value, not an array. Eg 'Registration.REG_ID => array('%s',23))", |
|
| 4390 | + 'event_espresso' |
|
| 4391 | + ), |
|
| 4392 | + $operator, |
|
| 4393 | + $operator |
|
| 4394 | + ) |
|
| 4395 | + ); |
|
| 4396 | + } |
|
| 4397 | + throw new EE_Error( |
|
| 4398 | + sprintf( |
|
| 4399 | + __( |
|
| 4400 | + "It appears you've provided some totally invalid query parameters. Operator and value were:'%s', which isn't right at all", |
|
| 4401 | + "event_espresso" |
|
| 4402 | + ), |
|
| 4403 | + http_build_query($op_and_value) |
|
| 4404 | + ) |
|
| 4405 | + ); |
|
| 4406 | + } |
|
| 4407 | + |
|
| 4408 | + |
|
| 4409 | + |
|
| 4410 | + /** |
|
| 4411 | + * Creates the operands to be used in a BETWEEN query, eg "'2014-12-31 20:23:33' AND '2015-01-23 12:32:54'" |
|
| 4412 | + * |
|
| 4413 | + * @param array $values |
|
| 4414 | + * @param EE_Model_Field_Base|string $field_obj if string, it should be the datatype to be used when querying, eg |
|
| 4415 | + * '%s' |
|
| 4416 | + * @return string |
|
| 4417 | + * @throws EE_Error |
|
| 4418 | + */ |
|
| 4419 | + public function _construct_between_value($values, $field_obj) |
|
| 4420 | + { |
|
| 4421 | + $cleaned_values = array(); |
|
| 4422 | + foreach ($values as $value) { |
|
| 4423 | + $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4424 | + } |
|
| 4425 | + return $cleaned_values[0] . " AND " . $cleaned_values[1]; |
|
| 4426 | + } |
|
| 4427 | + |
|
| 4428 | + |
|
| 4429 | + |
|
| 4430 | + /** |
|
| 4431 | + * Takes an array or a comma-separated list of $values and cleans them |
|
| 4432 | + * according to $data_type using $wpdb->prepare, and then makes the list a |
|
| 4433 | + * string surrounded by ( and ). Eg, _construct_in_value(array(1,2,3),'%d') would |
|
| 4434 | + * return '(1,2,3)'; _construct_in_value("1,2,hack",'%d') would return '(1,2,1)' (assuming |
|
| 4435 | + * I'm right that a string, when interpreted as a digit, becomes a 1. It might become a 0) |
|
| 4436 | + * |
|
| 4437 | + * @param mixed $values array or comma-separated string |
|
| 4438 | + * @param EE_Model_Field_Base|string $field_obj if string, it should be a wpdb data type like '%s', or '%d' |
|
| 4439 | + * @return string of SQL to follow an 'IN' or 'NOT IN' operator |
|
| 4440 | + * @throws EE_Error |
|
| 4441 | + */ |
|
| 4442 | + public function _construct_in_value($values, $field_obj) |
|
| 4443 | + { |
|
| 4444 | + //check if the value is a CSV list |
|
| 4445 | + if (is_string($values)) { |
|
| 4446 | + //in which case, turn it into an array |
|
| 4447 | + $values = explode(",", $values); |
|
| 4448 | + } |
|
| 4449 | + $cleaned_values = array(); |
|
| 4450 | + foreach ($values as $value) { |
|
| 4451 | + $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4452 | + } |
|
| 4453 | + //we would just LOVE to leave $cleaned_values as an empty array, and return the value as "()", |
|
| 4454 | + //but unfortunately that's invalid SQL. So instead we return a string which we KNOW will evaluate to be the empty set |
|
| 4455 | + //which is effectively equivalent to returning "()". We don't return "(0)" because that only works for auto-incrementing columns |
|
| 4456 | + if (empty($cleaned_values)) { |
|
| 4457 | + $all_fields = $this->field_settings(); |
|
| 4458 | + $a_field = array_shift($all_fields); |
|
| 4459 | + $main_table = $this->_get_main_table(); |
|
| 4460 | + $cleaned_values[] = "SELECT " |
|
| 4461 | + . $a_field->get_table_column() |
|
| 4462 | + . " FROM " |
|
| 4463 | + . $main_table->get_table_name() |
|
| 4464 | + . " WHERE FALSE"; |
|
| 4465 | + } |
|
| 4466 | + return "(" . implode(",", $cleaned_values) . ")"; |
|
| 4467 | + } |
|
| 4468 | + |
|
| 4469 | + |
|
| 4470 | + |
|
| 4471 | + /** |
|
| 4472 | + * @param mixed $value |
|
| 4473 | + * @param EE_Model_Field_Base|string $field_obj if string it should be a wpdb data type like '%d' |
|
| 4474 | + * @throws EE_Error |
|
| 4475 | + * @return false|null|string |
|
| 4476 | + */ |
|
| 4477 | + private function _wpdb_prepare_using_field($value, $field_obj) |
|
| 4478 | + { |
|
| 4479 | + /** @type WPDB $wpdb */ |
|
| 4480 | + global $wpdb; |
|
| 4481 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 4482 | + return $wpdb->prepare($field_obj->get_wpdb_data_type(), |
|
| 4483 | + $this->_prepare_value_for_use_in_db($value, $field_obj)); |
|
| 4484 | + } //$field_obj should really just be a data type |
|
| 4485 | + if (! in_array($field_obj, $this->_valid_wpdb_data_types)) { |
|
| 4486 | + throw new EE_Error( |
|
| 4487 | + sprintf( |
|
| 4488 | + __("%s is not a valid wpdb datatype. Valid ones are %s", "event_espresso"), |
|
| 4489 | + $field_obj, implode(",", $this->_valid_wpdb_data_types) |
|
| 4490 | + ) |
|
| 4491 | + ); |
|
| 4492 | + } |
|
| 4493 | + return $wpdb->prepare($field_obj, $value); |
|
| 4494 | + } |
|
| 4495 | + |
|
| 4496 | + |
|
| 4497 | + |
|
| 4498 | + /** |
|
| 4499 | + * Takes the input parameter and finds the model field that it indicates. |
|
| 4500 | + * |
|
| 4501 | + * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
|
| 4502 | + * @throws EE_Error |
|
| 4503 | + * @return EE_Model_Field_Base |
|
| 4504 | + */ |
|
| 4505 | + protected function _deduce_field_from_query_param($query_param_name) |
|
| 4506 | + { |
|
| 4507 | + //ok, now proceed with deducing which part is the model's name, and which is the field's name |
|
| 4508 | + //which will help us find the database table and column |
|
| 4509 | + $query_param_parts = explode(".", $query_param_name); |
|
| 4510 | + if (empty($query_param_parts)) { |
|
| 4511 | + throw new EE_Error(sprintf(__("_extract_column_name is empty when trying to extract column and table name from %s", |
|
| 4512 | + 'event_espresso'), $query_param_name)); |
|
| 4513 | + } |
|
| 4514 | + $number_of_parts = count($query_param_parts); |
|
| 4515 | + $last_query_param_part = $query_param_parts[count($query_param_parts) - 1]; |
|
| 4516 | + if ($number_of_parts === 1) { |
|
| 4517 | + $field_name = $last_query_param_part; |
|
| 4518 | + $model_obj = $this; |
|
| 4519 | + } else {// $number_of_parts >= 2 |
|
| 4520 | + //the last part is the column name, and there are only 2parts. therefore... |
|
| 4521 | + $field_name = $last_query_param_part; |
|
| 4522 | + $model_obj = $this->get_related_model_obj($query_param_parts[$number_of_parts - 2]); |
|
| 4523 | + } |
|
| 4524 | + try { |
|
| 4525 | + return $model_obj->field_settings_for($field_name); |
|
| 4526 | + } catch (EE_Error $e) { |
|
| 4527 | + return null; |
|
| 4528 | + } |
|
| 4529 | + } |
|
| 4530 | + |
|
| 4531 | + |
|
| 4532 | + |
|
| 4533 | + /** |
|
| 4534 | + * Given a field's name (ie, a key in $this->field_settings()), uses the EE_Model_Field object to get the table's |
|
| 4535 | + * alias and column which corresponds to it |
|
| 4536 | + * |
|
| 4537 | + * @param string $field_name |
|
| 4538 | + * @throws EE_Error |
|
| 4539 | + * @return string |
|
| 4540 | + */ |
|
| 4541 | + public function _get_qualified_column_for_field($field_name) |
|
| 4542 | + { |
|
| 4543 | + $all_fields = $this->field_settings(); |
|
| 4544 | + $field = isset($all_fields[$field_name]) ? $all_fields[$field_name] : false; |
|
| 4545 | + if ($field) { |
|
| 4546 | + return $field->get_qualified_column(); |
|
| 4547 | + } |
|
| 4548 | + throw new EE_Error( |
|
| 4549 | + sprintf( |
|
| 4550 | + __( |
|
| 4551 | + "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.", |
|
| 4552 | + 'event_espresso' |
|
| 4553 | + ), $field_name, get_class($this) |
|
| 4554 | + ) |
|
| 4555 | + ); |
|
| 4556 | + } |
|
| 4557 | + |
|
| 4558 | + |
|
| 4559 | + |
|
| 4560 | + /** |
|
| 4561 | + * similar to \EEM_Base::_get_qualified_column_for_field() but returns an array with data for ALL fields. |
|
| 4562 | + * Example usage: |
|
| 4563 | + * EEM_Ticket::instance()->get_all_wpdb_results( |
|
| 4564 | + * array(), |
|
| 4565 | + * ARRAY_A, |
|
| 4566 | + * EEM_Ticket::instance()->get_qualified_columns_for_all_fields() |
|
| 4567 | + * ); |
|
| 4568 | + * is equivalent to |
|
| 4569 | + * EEM_Ticket::instance()->get_all_wpdb_results( array(), ARRAY_A, '*' ); |
|
| 4570 | + * and |
|
| 4571 | + * EEM_Event::instance()->get_all_wpdb_results( |
|
| 4572 | + * array( |
|
| 4573 | + * array( |
|
| 4574 | + * 'Datetime.Ticket.TKT_ID' => array( '<', 100 ), |
|
| 4575 | + * ), |
|
| 4576 | + * ARRAY_A, |
|
| 4577 | + * implode( |
|
| 4578 | + * ', ', |
|
| 4579 | + * array_merge( |
|
| 4580 | + * EEM_Event::instance()->get_qualified_columns_for_all_fields( '', false ), |
|
| 4581 | + * EEM_Ticket::instance()->get_qualified_columns_for_all_fields( 'Datetime', false ) |
|
| 4582 | + * ) |
|
| 4583 | + * ) |
|
| 4584 | + * ) |
|
| 4585 | + * ); |
|
| 4586 | + * selects rows from the database, selecting all the event and ticket columns, where the ticket ID is below 100 |
|
| 4587 | + * |
|
| 4588 | + * @param string $model_relation_chain the chain of models used to join between the model you want to query |
|
| 4589 | + * and the one whose fields you are selecting for example: when querying |
|
| 4590 | + * tickets model and selecting fields from the tickets model you would |
|
| 4591 | + * leave this parameter empty, because no models are needed to join |
|
| 4592 | + * between the queried model and the selected one. Likewise when |
|
| 4593 | + * querying the datetime model and selecting fields from the tickets |
|
| 4594 | + * model, it would also be left empty, because there is a direct |
|
| 4595 | + * relation from datetimes to tickets, so no model is needed to join |
|
| 4596 | + * them together. However, when querying from the event model and |
|
| 4597 | + * selecting fields from the ticket model, you should provide the string |
|
| 4598 | + * 'Datetime', indicating that the event model must first join to the |
|
| 4599 | + * datetime model in order to find its relation to ticket model. |
|
| 4600 | + * Also, when querying from the venue model and selecting fields from |
|
| 4601 | + * the ticket model, you should provide the string 'Event.Datetime', |
|
| 4602 | + * indicating you need to join the venue model to the event model, |
|
| 4603 | + * to the datetime model, in order to find its relation to the ticket model. |
|
| 4604 | + * This string is used to deduce the prefix that gets added onto the |
|
| 4605 | + * models' tables qualified columns |
|
| 4606 | + * @param bool $return_string if true, will return a string with qualified column names separated |
|
| 4607 | + * by ', ' if false, will simply return a numerically indexed array of |
|
| 4608 | + * qualified column names |
|
| 4609 | + * @return array|string |
|
| 4610 | + */ |
|
| 4611 | + public function get_qualified_columns_for_all_fields($model_relation_chain = '', $return_string = true) |
|
| 4612 | + { |
|
| 4613 | + $table_prefix = str_replace('.', '__', $model_relation_chain) . (empty($model_relation_chain) ? '' : '__'); |
|
| 4614 | + $qualified_columns = array(); |
|
| 4615 | + foreach ($this->field_settings() as $field_name => $field) { |
|
| 4616 | + $qualified_columns[] = $table_prefix . $field->get_qualified_column(); |
|
| 4617 | + } |
|
| 4618 | + return $return_string ? implode(', ', $qualified_columns) : $qualified_columns; |
|
| 4619 | + } |
|
| 4620 | + |
|
| 4621 | + |
|
| 4622 | + |
|
| 4623 | + /** |
|
| 4624 | + * constructs the select use on special limit joins |
|
| 4625 | + * NOTE: for now this has only been tested and will work when the table alias is for the PRIMARY table. Although |
|
| 4626 | + * its setup so the select query will be setup on and just doing the special select join off of the primary table |
|
| 4627 | + * (as that is typically where the limits would be set). |
|
| 4628 | + * |
|
| 4629 | + * @param string $table_alias The table the select is being built for |
|
| 4630 | + * @param mixed|string $limit The limit for this select |
|
| 4631 | + * @return string The final select join element for the query. |
|
| 4632 | + */ |
|
| 4633 | + public function _construct_limit_join_select($table_alias, $limit) |
|
| 4634 | + { |
|
| 4635 | + $SQL = ''; |
|
| 4636 | + foreach ($this->_tables as $table_obj) { |
|
| 4637 | + if ($table_obj instanceof EE_Primary_Table) { |
|
| 4638 | + $SQL .= $table_alias === $table_obj->get_table_alias() |
|
| 4639 | + ? $table_obj->get_select_join_limit($limit) |
|
| 4640 | + : SP . $table_obj->get_table_name() . " AS " . $table_obj->get_table_alias() . SP; |
|
| 4641 | + } elseif ($table_obj instanceof EE_Secondary_Table) { |
|
| 4642 | + $SQL .= $table_alias === $table_obj->get_table_alias() |
|
| 4643 | + ? $table_obj->get_select_join_limit_join($limit) |
|
| 4644 | + : SP . $table_obj->get_join_sql($table_alias) . SP; |
|
| 4645 | + } |
|
| 4646 | + } |
|
| 4647 | + return $SQL; |
|
| 4648 | + } |
|
| 4649 | + |
|
| 4650 | + |
|
| 4651 | + |
|
| 4652 | + /** |
|
| 4653 | + * Constructs the internal join if there are multiple tables, or simply the table's name and alias |
|
| 4654 | + * Eg "wp_post AS Event" or "wp_post AS Event INNER JOIN wp_postmeta Event_Meta ON Event.ID = Event_Meta.post_id" |
|
| 4655 | + * |
|
| 4656 | + * @return string SQL |
|
| 4657 | + * @throws EE_Error |
|
| 4658 | + */ |
|
| 4659 | + public function _construct_internal_join() |
|
| 4660 | + { |
|
| 4661 | + $SQL = $this->_get_main_table()->get_table_sql(); |
|
| 4662 | + $SQL .= $this->_construct_internal_join_to_table_with_alias($this->_get_main_table()->get_table_alias()); |
|
| 4663 | + return $SQL; |
|
| 4664 | + } |
|
| 4665 | + |
|
| 4666 | + |
|
| 4667 | + |
|
| 4668 | + /** |
|
| 4669 | + * Constructs the SQL for joining all the tables on this model. |
|
| 4670 | + * Normally $alias should be the primary table's alias, but in cases where |
|
| 4671 | + * we have already joined to a secondary table (eg, the secondary table has a foreign key and is joined before the |
|
| 4672 | + * primary table) then we should provide that secondary table's alias. Eg, with $alias being the primary table's |
|
| 4673 | + * alias, this will construct SQL like: |
|
| 4674 | + * " INNER JOIN wp_esp_secondary_table AS Secondary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
| 4675 | + * With $alias being a secondary table's alias, this will construct SQL like: |
|
| 4676 | + * " INNER JOIN wp_esp_primary_table AS Primary_Table ON Primary_Table.pk = Secondary_Table.fk". |
|
| 4677 | + * |
|
| 4678 | + * @param string $alias_prefixed table alias to join to (this table should already be in the FROM SQL clause) |
|
| 4679 | + * @return string |
|
| 4680 | + */ |
|
| 4681 | + public function _construct_internal_join_to_table_with_alias($alias_prefixed) |
|
| 4682 | + { |
|
| 4683 | + $SQL = ''; |
|
| 4684 | + $alias_sans_prefix = EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($alias_prefixed); |
|
| 4685 | + foreach ($this->_tables as $table_obj) { |
|
| 4686 | + if ($table_obj instanceof EE_Secondary_Table) {//table is secondary table |
|
| 4687 | + if ($alias_sans_prefix === $table_obj->get_table_alias()) { |
|
| 4688 | + //so we're joining to this table, meaning the table is already in |
|
| 4689 | + //the FROM statement, BUT the primary table isn't. So we want |
|
| 4690 | + //to add the inverse join sql |
|
| 4691 | + $SQL .= $table_obj->get_inverse_join_sql($alias_prefixed); |
|
| 4692 | + } else { |
|
| 4693 | + //just add a regular JOIN to this table from the primary table |
|
| 4694 | + $SQL .= $table_obj->get_join_sql($alias_prefixed); |
|
| 4695 | + } |
|
| 4696 | + }//if it's a primary table, dont add any SQL. it should already be in the FROM statement |
|
| 4697 | + } |
|
| 4698 | + return $SQL; |
|
| 4699 | + } |
|
| 4700 | + |
|
| 4701 | + |
|
| 4702 | + |
|
| 4703 | + /** |
|
| 4704 | + * Gets an array for storing all the data types on the next-to-be-executed-query. |
|
| 4705 | + * This should be a growing array of keys being table-columns (eg 'EVT_ID' and 'Event.EVT_ID'), and values being |
|
| 4706 | + * their data type (eg, '%s', '%d', etc) |
|
| 4707 | + * |
|
| 4708 | + * @return array |
|
| 4709 | + */ |
|
| 4710 | + public function _get_data_types() |
|
| 4711 | + { |
|
| 4712 | + $data_types = array(); |
|
| 4713 | + foreach ($this->field_settings() as $field_obj) { |
|
| 4714 | + //$data_types[$field_obj->get_table_column()] = $field_obj->get_wpdb_data_type(); |
|
| 4715 | + /** @var $field_obj EE_Model_Field_Base */ |
|
| 4716 | + $data_types[$field_obj->get_qualified_column()] = $field_obj->get_wpdb_data_type(); |
|
| 4717 | + } |
|
| 4718 | + return $data_types; |
|
| 4719 | + } |
|
| 4720 | + |
|
| 4721 | + |
|
| 4722 | + |
|
| 4723 | + /** |
|
| 4724 | + * Gets the model object given the relation's name / model's name (eg, 'Event', 'Registration',etc. Always singular) |
|
| 4725 | + * |
|
| 4726 | + * @param string $model_name |
|
| 4727 | + * @throws EE_Error |
|
| 4728 | + * @return EEM_Base |
|
| 4729 | + */ |
|
| 4730 | + public function get_related_model_obj($model_name) |
|
| 4731 | + { |
|
| 4732 | + $model_classname = "EEM_" . $model_name; |
|
| 4733 | + if (! class_exists($model_classname)) { |
|
| 4734 | + throw new EE_Error(sprintf(__("You specified a related model named %s in your query. No such model exists, if it did, it would have the classname %s", |
|
| 4735 | + 'event_espresso'), $model_name, $model_classname)); |
|
| 4736 | + } |
|
| 4737 | + return call_user_func($model_classname . "::instance"); |
|
| 4738 | + } |
|
| 4739 | + |
|
| 4740 | + |
|
| 4741 | + |
|
| 4742 | + /** |
|
| 4743 | + * Returns the array of EE_ModelRelations for this model. |
|
| 4744 | + * |
|
| 4745 | + * @return EE_Model_Relation_Base[] |
|
| 4746 | + */ |
|
| 4747 | + public function relation_settings() |
|
| 4748 | + { |
|
| 4749 | + return $this->_model_relations; |
|
| 4750 | + } |
|
| 4751 | + |
|
| 4752 | + |
|
| 4753 | + |
|
| 4754 | + /** |
|
| 4755 | + * Gets all related models that this model BELONGS TO. Handy to know sometimes |
|
| 4756 | + * because without THOSE models, this model probably doesn't have much purpose. |
|
| 4757 | + * (Eg, without an event, datetimes have little purpose.) |
|
| 4758 | + * |
|
| 4759 | + * @return EE_Belongs_To_Relation[] |
|
| 4760 | + */ |
|
| 4761 | + public function belongs_to_relations() |
|
| 4762 | + { |
|
| 4763 | + $belongs_to_relations = array(); |
|
| 4764 | + foreach ($this->relation_settings() as $model_name => $relation_obj) { |
|
| 4765 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 4766 | + $belongs_to_relations[$model_name] = $relation_obj; |
|
| 4767 | + } |
|
| 4768 | + } |
|
| 4769 | + return $belongs_to_relations; |
|
| 4770 | + } |
|
| 4771 | + |
|
| 4772 | + |
|
| 4773 | + |
|
| 4774 | + /** |
|
| 4775 | + * Returns the specified EE_Model_Relation, or throws an exception |
|
| 4776 | + * |
|
| 4777 | + * @param string $relation_name name of relation, key in $this->_relatedModels |
|
| 4778 | + * @throws EE_Error |
|
| 4779 | + * @return EE_Model_Relation_Base |
|
| 4780 | + */ |
|
| 4781 | + public function related_settings_for($relation_name) |
|
| 4782 | + { |
|
| 4783 | + $relatedModels = $this->relation_settings(); |
|
| 4784 | + if (! array_key_exists($relation_name, $relatedModels)) { |
|
| 4785 | + throw new EE_Error( |
|
| 4786 | + sprintf( |
|
| 4787 | + __('Cannot get %s related to %s. There is no model relation of that type. There is, however, %s...', |
|
| 4788 | + 'event_espresso'), |
|
| 4789 | + $relation_name, |
|
| 4790 | + $this->_get_class_name(), |
|
| 4791 | + implode(', ', array_keys($relatedModels)) |
|
| 4792 | + ) |
|
| 4793 | + ); |
|
| 4794 | + } |
|
| 4795 | + return $relatedModels[$relation_name]; |
|
| 4796 | + } |
|
| 4797 | + |
|
| 4798 | + |
|
| 4799 | + |
|
| 4800 | + /** |
|
| 4801 | + * A convenience method for getting a specific field's settings, instead of getting all field settings for all |
|
| 4802 | + * fields |
|
| 4803 | + * |
|
| 4804 | + * @param string $fieldName |
|
| 4805 | + * @param boolean $include_db_only_fields |
|
| 4806 | + * @throws EE_Error |
|
| 4807 | + * @return EE_Model_Field_Base |
|
| 4808 | + */ |
|
| 4809 | + public function field_settings_for($fieldName, $include_db_only_fields = true) |
|
| 4810 | + { |
|
| 4811 | + $fieldSettings = $this->field_settings($include_db_only_fields); |
|
| 4812 | + if (! array_key_exists($fieldName, $fieldSettings)) { |
|
| 4813 | + throw new EE_Error(sprintf(__("There is no field/column '%s' on '%s'", 'event_espresso'), $fieldName, |
|
| 4814 | + get_class($this))); |
|
| 4815 | + } |
|
| 4816 | + return $fieldSettings[$fieldName]; |
|
| 4817 | + } |
|
| 4818 | + |
|
| 4819 | + |
|
| 4820 | + |
|
| 4821 | + /** |
|
| 4822 | + * Checks if this field exists on this model |
|
| 4823 | + * |
|
| 4824 | + * @param string $fieldName a key in the model's _field_settings array |
|
| 4825 | + * @return boolean |
|
| 4826 | + */ |
|
| 4827 | + public function has_field($fieldName) |
|
| 4828 | + { |
|
| 4829 | + $fieldSettings = $this->field_settings(true); |
|
| 4830 | + if (isset($fieldSettings[$fieldName])) { |
|
| 4831 | + return true; |
|
| 4832 | + } |
|
| 4833 | + return false; |
|
| 4834 | + } |
|
| 4835 | + |
|
| 4836 | + |
|
| 4837 | + |
|
| 4838 | + /** |
|
| 4839 | + * Returns whether or not this model has a relation to the specified model |
|
| 4840 | + * |
|
| 4841 | + * @param string $relation_name possibly one of the keys in the relation_settings array |
|
| 4842 | + * @return boolean |
|
| 4843 | + */ |
|
| 4844 | + public function has_relation($relation_name) |
|
| 4845 | + { |
|
| 4846 | + $relations = $this->relation_settings(); |
|
| 4847 | + if (isset($relations[$relation_name])) { |
|
| 4848 | + return true; |
|
| 4849 | + } |
|
| 4850 | + return false; |
|
| 4851 | + } |
|
| 4852 | + |
|
| 4853 | + |
|
| 4854 | + |
|
| 4855 | + /** |
|
| 4856 | + * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
| 4857 | + * Eg, on EE_Answer that would be ANS_ID field object |
|
| 4858 | + * |
|
| 4859 | + * @param $field_obj |
|
| 4860 | + * @return boolean |
|
| 4861 | + */ |
|
| 4862 | + public function is_primary_key_field($field_obj) |
|
| 4863 | + { |
|
| 4864 | + return $field_obj instanceof EE_Primary_Key_Field_Base ? true : false; |
|
| 4865 | + } |
|
| 4866 | + |
|
| 4867 | + |
|
| 4868 | + |
|
| 4869 | + /** |
|
| 4870 | + * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
|
| 4871 | + * Eg, on EE_Answer that would be ANS_ID field object |
|
| 4872 | + * |
|
| 4873 | + * @return EE_Model_Field_Base |
|
| 4874 | + * @throws EE_Error |
|
| 4875 | + */ |
|
| 4876 | + public function get_primary_key_field() |
|
| 4877 | + { |
|
| 4878 | + if ($this->_primary_key_field === null) { |
|
| 4879 | + foreach ($this->field_settings(true) as $field_obj) { |
|
| 4880 | + if ($this->is_primary_key_field($field_obj)) { |
|
| 4881 | + $this->_primary_key_field = $field_obj; |
|
| 4882 | + break; |
|
| 4883 | + } |
|
| 4884 | + } |
|
| 4885 | + if (! $this->_primary_key_field instanceof EE_Primary_Key_Field_Base) { |
|
| 4886 | + throw new EE_Error(sprintf(__("There is no Primary Key defined on model %s", 'event_espresso'), |
|
| 4887 | + get_class($this))); |
|
| 4888 | + } |
|
| 4889 | + } |
|
| 4890 | + return $this->_primary_key_field; |
|
| 4891 | + } |
|
| 4892 | + |
|
| 4893 | + |
|
| 4894 | + |
|
| 4895 | + /** |
|
| 4896 | + * Returns whether or not not there is a primary key on this model. |
|
| 4897 | + * Internally does some caching. |
|
| 4898 | + * |
|
| 4899 | + * @return boolean |
|
| 4900 | + */ |
|
| 4901 | + public function has_primary_key_field() |
|
| 4902 | + { |
|
| 4903 | + if ($this->_has_primary_key_field === null) { |
|
| 4904 | + try { |
|
| 4905 | + $this->get_primary_key_field(); |
|
| 4906 | + $this->_has_primary_key_field = true; |
|
| 4907 | + } catch (EE_Error $e) { |
|
| 4908 | + $this->_has_primary_key_field = false; |
|
| 4909 | + } |
|
| 4910 | + } |
|
| 4911 | + return $this->_has_primary_key_field; |
|
| 4912 | + } |
|
| 4913 | + |
|
| 4914 | + |
|
| 4915 | + |
|
| 4916 | + /** |
|
| 4917 | + * Finds the first field of type $field_class_name. |
|
| 4918 | + * |
|
| 4919 | + * @param string $field_class_name class name of field that you want to find. Eg, EE_Datetime_Field, |
|
| 4920 | + * EE_Foreign_Key_Field, etc |
|
| 4921 | + * @return EE_Model_Field_Base or null if none is found |
|
| 4922 | + */ |
|
| 4923 | + public function get_a_field_of_type($field_class_name) |
|
| 4924 | + { |
|
| 4925 | + foreach ($this->field_settings() as $field) { |
|
| 4926 | + if ($field instanceof $field_class_name) { |
|
| 4927 | + return $field; |
|
| 4928 | + } |
|
| 4929 | + } |
|
| 4930 | + return null; |
|
| 4931 | + } |
|
| 4932 | + |
|
| 4933 | + |
|
| 4934 | + |
|
| 4935 | + /** |
|
| 4936 | + * Gets a foreign key field pointing to model. |
|
| 4937 | + * |
|
| 4938 | + * @param string $model_name eg Event, Registration, not EEM_Event |
|
| 4939 | + * @return EE_Foreign_Key_Field_Base |
|
| 4940 | + * @throws EE_Error |
|
| 4941 | + */ |
|
| 4942 | + public function get_foreign_key_to($model_name) |
|
| 4943 | + { |
|
| 4944 | + if (! isset($this->_cache_foreign_key_to_fields[$model_name])) { |
|
| 4945 | + foreach ($this->field_settings() as $field) { |
|
| 4946 | + if ( |
|
| 4947 | + $field instanceof EE_Foreign_Key_Field_Base |
|
| 4948 | + && in_array($model_name, $field->get_model_names_pointed_to()) |
|
| 4949 | + ) { |
|
| 4950 | + $this->_cache_foreign_key_to_fields[$model_name] = $field; |
|
| 4951 | + break; |
|
| 4952 | + } |
|
| 4953 | + } |
|
| 4954 | + if (! isset($this->_cache_foreign_key_to_fields[$model_name])) { |
|
| 4955 | + throw new EE_Error(sprintf(__("There is no foreign key field pointing to model %s on model %s", |
|
| 4956 | + 'event_espresso'), $model_name, get_class($this))); |
|
| 4957 | + } |
|
| 4958 | + } |
|
| 4959 | + return $this->_cache_foreign_key_to_fields[$model_name]; |
|
| 4960 | + } |
|
| 4961 | + |
|
| 4962 | + |
|
| 4963 | + |
|
| 4964 | + /** |
|
| 4965 | + * Gets the table name (including $wpdb->prefix) for the table alias |
|
| 4966 | + * |
|
| 4967 | + * @param string $table_alias eg Event, Event_Meta, Registration, Transaction, but maybe |
|
| 4968 | + * a table alias with a model chain prefix, like 'Venue__Event_Venue___Event_Meta'. |
|
| 4969 | + * Either one works |
|
| 4970 | + * @return string |
|
| 4971 | + */ |
|
| 4972 | + public function get_table_for_alias($table_alias) |
|
| 4973 | + { |
|
| 4974 | + $table_alias_sans_model_relation_chain_prefix = EE_Model_Parser::remove_table_alias_model_relation_chain_prefix($table_alias); |
|
| 4975 | + return $this->_tables[$table_alias_sans_model_relation_chain_prefix]->get_table_name(); |
|
| 4976 | + } |
|
| 4977 | + |
|
| 4978 | + |
|
| 4979 | + |
|
| 4980 | + /** |
|
| 4981 | + * Returns a flat array of all field son this model, instead of organizing them |
|
| 4982 | + * by table_alias as they are in the constructor. |
|
| 4983 | + * |
|
| 4984 | + * @param bool $include_db_only_fields flag indicating whether or not to include the db-only fields |
|
| 4985 | + * @return EE_Model_Field_Base[] where the keys are the field's name |
|
| 4986 | + */ |
|
| 4987 | + public function field_settings($include_db_only_fields = false) |
|
| 4988 | + { |
|
| 4989 | + if ($include_db_only_fields) { |
|
| 4990 | + if ($this->_cached_fields === null) { |
|
| 4991 | + $this->_cached_fields = array(); |
|
| 4992 | + foreach ($this->_fields as $fields_corresponding_to_table) { |
|
| 4993 | + foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
| 4994 | + $this->_cached_fields[$field_name] = $field_obj; |
|
| 4995 | + } |
|
| 4996 | + } |
|
| 4997 | + } |
|
| 4998 | + return $this->_cached_fields; |
|
| 4999 | + } |
|
| 5000 | + if ($this->_cached_fields_non_db_only === null) { |
|
| 5001 | + $this->_cached_fields_non_db_only = array(); |
|
| 5002 | + foreach ($this->_fields as $fields_corresponding_to_table) { |
|
| 5003 | + foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
|
| 5004 | + /** @var $field_obj EE_Model_Field_Base */ |
|
| 5005 | + if (! $field_obj->is_db_only_field()) { |
|
| 5006 | + $this->_cached_fields_non_db_only[$field_name] = $field_obj; |
|
| 5007 | + } |
|
| 5008 | + } |
|
| 5009 | + } |
|
| 5010 | + } |
|
| 5011 | + return $this->_cached_fields_non_db_only; |
|
| 5012 | + } |
|
| 5013 | + |
|
| 5014 | + |
|
| 5015 | + |
|
| 5016 | + /** |
|
| 5017 | + * cycle though array of attendees and create objects out of each item |
|
| 5018 | + * |
|
| 5019 | + * @access private |
|
| 5020 | + * @param array $rows of results of $wpdb->get_results($query,ARRAY_A) |
|
| 5021 | + * @return \EE_Base_Class[] array keys are primary keys (if there is a primary key on the model. if not, |
|
| 5022 | + * numerically indexed) |
|
| 5023 | + * @throws EE_Error |
|
| 5024 | + */ |
|
| 5025 | + protected function _create_objects($rows = array()) |
|
| 5026 | + { |
|
| 5027 | + $array_of_objects = array(); |
|
| 5028 | + if (empty($rows)) { |
|
| 5029 | + return array(); |
|
| 5030 | + } |
|
| 5031 | + $count_if_model_has_no_primary_key = 0; |
|
| 5032 | + $has_primary_key = $this->has_primary_key_field(); |
|
| 5033 | + $primary_key_field = $has_primary_key ? $this->get_primary_key_field() : null; |
|
| 5034 | + foreach ((array)$rows as $row) { |
|
| 5035 | + if (empty($row)) { |
|
| 5036 | + //wp did its weird thing where it returns an array like array(0=>null), which is totally not helpful... |
|
| 5037 | + return array(); |
|
| 5038 | + } |
|
| 5039 | + //check if we've already set this object in the results array, |
|
| 5040 | + //in which case there's no need to process it further (again) |
|
| 5041 | + if ($has_primary_key) { |
|
| 5042 | + $table_pk_value = $this->_get_column_value_with_table_alias_or_not( |
|
| 5043 | + $row, |
|
| 5044 | + $primary_key_field->get_qualified_column(), |
|
| 5045 | + $primary_key_field->get_table_column() |
|
| 5046 | + ); |
|
| 5047 | + if ($table_pk_value && isset($array_of_objects[$table_pk_value])) { |
|
| 5048 | + continue; |
|
| 5049 | + } |
|
| 5050 | + } |
|
| 5051 | + $classInstance = $this->instantiate_class_from_array_or_object($row); |
|
| 5052 | + if (! $classInstance) { |
|
| 5053 | + throw new EE_Error( |
|
| 5054 | + sprintf( |
|
| 5055 | + __('Could not create instance of class %s from row %s', 'event_espresso'), |
|
| 5056 | + $this->get_this_model_name(), |
|
| 5057 | + http_build_query($row) |
|
| 5058 | + ) |
|
| 5059 | + ); |
|
| 5060 | + } |
|
| 5061 | + //set the timezone on the instantiated objects |
|
| 5062 | + $classInstance->set_timezone($this->_timezone); |
|
| 5063 | + //make sure if there is any timezone setting present that we set the timezone for the object |
|
| 5064 | + $key = $has_primary_key ? $classInstance->ID() : $count_if_model_has_no_primary_key++; |
|
| 5065 | + $array_of_objects[$key] = $classInstance; |
|
| 5066 | + //also, for all the relations of type BelongsTo, see if we can cache |
|
| 5067 | + //those related models |
|
| 5068 | + //(we could do this for other relations too, but if there are conditions |
|
| 5069 | + //that filtered out some fo the results, then we'd be caching an incomplete set |
|
| 5070 | + //so it requires a little more thought than just caching them immediately...) |
|
| 5071 | + foreach ($this->_model_relations as $modelName => $relation_obj) { |
|
| 5072 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 5073 | + //check if this model's INFO is present. If so, cache it on the model |
|
| 5074 | + $other_model = $relation_obj->get_other_model(); |
|
| 5075 | + $other_model_obj_maybe = $other_model->instantiate_class_from_array_or_object($row); |
|
| 5076 | + //if we managed to make a model object from the results, cache it on the main model object |
|
| 5077 | + if ($other_model_obj_maybe) { |
|
| 5078 | + //set timezone on these other model objects if they are present |
|
| 5079 | + $other_model_obj_maybe->set_timezone($this->_timezone); |
|
| 5080 | + $classInstance->cache($modelName, $other_model_obj_maybe); |
|
| 5081 | + } |
|
| 5082 | + } |
|
| 5083 | + } |
|
| 5084 | + //also, if this was a custom select query, let's see if there are any results for the custom select fields |
|
| 5085 | + //and add them to the object as well. We'll convert according to the set data_type if there's any set for |
|
| 5086 | + //the field in the CustomSelects object |
|
| 5087 | + if ($this->_custom_selections instanceof CustomSelects) { |
|
| 5088 | + $classInstance->setCustomSelectsValues( |
|
| 5089 | + $this->getValuesForCustomSelectAliasesFromResults($row) |
|
| 5090 | + ); |
|
| 5091 | + } |
|
| 5092 | + } |
|
| 5093 | + return $array_of_objects; |
|
| 5094 | + } |
|
| 5095 | + |
|
| 5096 | + |
|
| 5097 | + /** |
|
| 5098 | + * This will parse a given row of results from the db and see if any keys in the results match an alias within the |
|
| 5099 | + * current CustomSelects object. This will be used to build an array of values indexed by those keys. |
|
| 5100 | + * |
|
| 5101 | + * @param array $db_results_row |
|
| 5102 | + * @return array |
|
| 5103 | + */ |
|
| 5104 | + protected function getValuesForCustomSelectAliasesFromResults(array $db_results_row) |
|
| 5105 | + { |
|
| 5106 | + $results = array(); |
|
| 5107 | + if ($this->_custom_selections instanceof CustomSelects) { |
|
| 5108 | + foreach ($this->_custom_selections->columnAliases() as $alias) { |
|
| 5109 | + if (isset($db_results_row[$alias])) { |
|
| 5110 | + $results[$alias] = $this->convertValueToDataType( |
|
| 5111 | + $db_results_row[$alias], |
|
| 5112 | + $this->_custom_selections->getDataTypeForAlias($alias) |
|
| 5113 | + ); |
|
| 5114 | + } |
|
| 5115 | + } |
|
| 5116 | + } |
|
| 5117 | + return $results; |
|
| 5118 | + } |
|
| 5119 | + |
|
| 5120 | + |
|
| 5121 | + /** |
|
| 5122 | + * This will set the value for the given alias |
|
| 5123 | + * @param string $value |
|
| 5124 | + * @param string $datatype (one of %d, %s, %f) |
|
| 5125 | + * @return int|string|float (int for %d, string for %s, float for %f) |
|
| 5126 | + */ |
|
| 5127 | + protected function convertValueToDataType($value, $datatype) |
|
| 5128 | + { |
|
| 5129 | + switch ($datatype) { |
|
| 5130 | + case '%f': |
|
| 5131 | + return (float) $value; |
|
| 5132 | + case '%d': |
|
| 5133 | + return (int) $value; |
|
| 5134 | + default: |
|
| 5135 | + return (string) $value; |
|
| 5136 | + } |
|
| 5137 | + } |
|
| 5138 | + |
|
| 5139 | + |
|
| 5140 | + /** |
|
| 5141 | + * The purpose of this method is to allow us to create a model object that is not in the db that holds default |
|
| 5142 | + * values. A typical example of where this is used is when creating a new item and the initial load of a form. We |
|
| 5143 | + * dont' necessarily want to test for if the object is present but just assume it is BUT load the defaults from the |
|
| 5144 | + * object (as set in the model_field!). |
|
| 5145 | + * |
|
| 5146 | + * @return EE_Base_Class single EE_Base_Class object with default values for the properties. |
|
| 5147 | + */ |
|
| 5148 | + public function create_default_object() |
|
| 5149 | + { |
|
| 5150 | + $this_model_fields_and_values = array(); |
|
| 5151 | + //setup the row using default values; |
|
| 5152 | + foreach ($this->field_settings() as $field_name => $field_obj) { |
|
| 5153 | + $this_model_fields_and_values[$field_name] = $field_obj->get_default_value(); |
|
| 5154 | + } |
|
| 5155 | + $className = $this->_get_class_name(); |
|
| 5156 | + $classInstance = EE_Registry::instance() |
|
| 5157 | + ->load_class($className, array($this_model_fields_and_values), false, false); |
|
| 5158 | + return $classInstance; |
|
| 5159 | + } |
|
| 5160 | + |
|
| 5161 | + |
|
| 5162 | + |
|
| 5163 | + /** |
|
| 5164 | + * @param mixed $cols_n_values either an array of where each key is the name of a field, and the value is its value |
|
| 5165 | + * or an stdClass where each property is the name of a column, |
|
| 5166 | + * @return EE_Base_Class |
|
| 5167 | + * @throws EE_Error |
|
| 5168 | + */ |
|
| 5169 | + public function instantiate_class_from_array_or_object($cols_n_values) |
|
| 5170 | + { |
|
| 5171 | + if (! is_array($cols_n_values) && is_object($cols_n_values)) { |
|
| 5172 | + $cols_n_values = get_object_vars($cols_n_values); |
|
| 5173 | + } |
|
| 5174 | + $primary_key = null; |
|
| 5175 | + //make sure the array only has keys that are fields/columns on this model |
|
| 5176 | + $this_model_fields_n_values = $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
| 5177 | + if ($this->has_primary_key_field() && isset($this_model_fields_n_values[$this->primary_key_name()])) { |
|
| 5178 | + $primary_key = $this_model_fields_n_values[$this->primary_key_name()]; |
|
| 5179 | + } |
|
| 5180 | + $className = $this->_get_class_name(); |
|
| 5181 | + //check we actually found results that we can use to build our model object |
|
| 5182 | + //if not, return null |
|
| 5183 | + if ($this->has_primary_key_field()) { |
|
| 5184 | + if (empty($this_model_fields_n_values[$this->primary_key_name()])) { |
|
| 5185 | + return null; |
|
| 5186 | + } |
|
| 5187 | + } else if ($this->unique_indexes()) { |
|
| 5188 | + $first_column = reset($this_model_fields_n_values); |
|
| 5189 | + if (empty($first_column)) { |
|
| 5190 | + return null; |
|
| 5191 | + } |
|
| 5192 | + } |
|
| 5193 | + // if there is no primary key or the object doesn't already exist in the entity map, then create a new instance |
|
| 5194 | + if ($primary_key) { |
|
| 5195 | + $classInstance = $this->get_from_entity_map($primary_key); |
|
| 5196 | + if (! $classInstance) { |
|
| 5197 | + $classInstance = EE_Registry::instance() |
|
| 5198 | + ->load_class($className, |
|
| 5199 | + array($this_model_fields_n_values, $this->_timezone), true, false); |
|
| 5200 | + // add this new object to the entity map |
|
| 5201 | + $classInstance = $this->add_to_entity_map($classInstance); |
|
| 5202 | + } |
|
| 5203 | + } else { |
|
| 5204 | + $classInstance = EE_Registry::instance() |
|
| 5205 | + ->load_class($className, array($this_model_fields_n_values, $this->_timezone), |
|
| 5206 | + true, false); |
|
| 5207 | + } |
|
| 5208 | + return $classInstance; |
|
| 5209 | + } |
|
| 5210 | + |
|
| 5211 | + |
|
| 5212 | + |
|
| 5213 | + /** |
|
| 5214 | + * Gets the model object from the entity map if it exists |
|
| 5215 | + * |
|
| 5216 | + * @param int|string $id the ID of the model object |
|
| 5217 | + * @return EE_Base_Class |
|
| 5218 | + */ |
|
| 5219 | + public function get_from_entity_map($id) |
|
| 5220 | + { |
|
| 5221 | + return isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id]) |
|
| 5222 | + ? $this->_entity_map[EEM_Base::$_model_query_blog_id][$id] : null; |
|
| 5223 | + } |
|
| 5224 | + |
|
| 5225 | + |
|
| 5226 | + |
|
| 5227 | + /** |
|
| 5228 | + * add_to_entity_map |
|
| 5229 | + * Adds the object to the model's entity mappings |
|
| 5230 | + * Effectively tells the models "Hey, this model object is the most up-to-date representation of the data, |
|
| 5231 | + * and for the remainder of the request, it's even more up-to-date than what's in the database. |
|
| 5232 | + * So, if the database doesn't agree with what's in the entity mapper, ignore the database" |
|
| 5233 | + * If the database gets updated directly and you want the entity mapper to reflect that change, |
|
| 5234 | + * then this method should be called immediately after the update query |
|
| 5235 | + * Note: The map is indexed by whatever the current blog id is set (via EEM_Base::$_model_query_blog_id). This is |
|
| 5236 | + * so on multisite, the entity map is specific to the query being done for a specific site. |
|
| 5237 | + * |
|
| 5238 | + * @param EE_Base_Class $object |
|
| 5239 | + * @throws EE_Error |
|
| 5240 | + * @return \EE_Base_Class |
|
| 5241 | + */ |
|
| 5242 | + public function add_to_entity_map(EE_Base_Class $object) |
|
| 5243 | + { |
|
| 5244 | + $className = $this->_get_class_name(); |
|
| 5245 | + if (! $object instanceof $className) { |
|
| 5246 | + throw new EE_Error(sprintf(__("You tried adding a %s to a mapping of %ss", "event_espresso"), |
|
| 5247 | + is_object($object) ? get_class($object) : $object, $className)); |
|
| 5248 | + } |
|
| 5249 | + /** @var $object EE_Base_Class */ |
|
| 5250 | + if (! $object->ID()) { |
|
| 5251 | + throw new EE_Error(sprintf(__("You tried storing a model object with NO ID in the %s entity mapper.", |
|
| 5252 | + "event_espresso"), get_class($this))); |
|
| 5253 | + } |
|
| 5254 | + // double check it's not already there |
|
| 5255 | + $classInstance = $this->get_from_entity_map($object->ID()); |
|
| 5256 | + if ($classInstance) { |
|
| 5257 | + return $classInstance; |
|
| 5258 | + } |
|
| 5259 | + $this->_entity_map[EEM_Base::$_model_query_blog_id][$object->ID()] = $object; |
|
| 5260 | + return $object; |
|
| 5261 | + } |
|
| 5262 | + |
|
| 5263 | + |
|
| 5264 | + |
|
| 5265 | + /** |
|
| 5266 | + * if a valid identifier is provided, then that entity is unset from the entity map, |
|
| 5267 | + * if no identifier is provided, then the entire entity map is emptied |
|
| 5268 | + * |
|
| 5269 | + * @param int|string $id the ID of the model object |
|
| 5270 | + * @return boolean |
|
| 5271 | + */ |
|
| 5272 | + public function clear_entity_map($id = null) |
|
| 5273 | + { |
|
| 5274 | + if (empty($id)) { |
|
| 5275 | + $this->_entity_map[EEM_Base::$_model_query_blog_id] = array(); |
|
| 5276 | + return true; |
|
| 5277 | + } |
|
| 5278 | + if (isset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id])) { |
|
| 5279 | + unset($this->_entity_map[EEM_Base::$_model_query_blog_id][$id]); |
|
| 5280 | + return true; |
|
| 5281 | + } |
|
| 5282 | + return false; |
|
| 5283 | + } |
|
| 5284 | + |
|
| 5285 | + |
|
| 5286 | + |
|
| 5287 | + /** |
|
| 5288 | + * Public wrapper for _deduce_fields_n_values_from_cols_n_values. |
|
| 5289 | + * Given an array where keys are column (or column alias) names and values, |
|
| 5290 | + * returns an array of their corresponding field names and database values |
|
| 5291 | + * |
|
| 5292 | + * @param array $cols_n_values |
|
| 5293 | + * @return array |
|
| 5294 | + */ |
|
| 5295 | + public function deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
| 5296 | + { |
|
| 5297 | + return $this->_deduce_fields_n_values_from_cols_n_values($cols_n_values); |
|
| 5298 | + } |
|
| 5299 | + |
|
| 5300 | + |
|
| 5301 | + |
|
| 5302 | + /** |
|
| 5303 | + * _deduce_fields_n_values_from_cols_n_values |
|
| 5304 | + * Given an array where keys are column (or column alias) names and values, |
|
| 5305 | + * returns an array of their corresponding field names and database values |
|
| 5306 | + * |
|
| 5307 | + * @param string $cols_n_values |
|
| 5308 | + * @return array |
|
| 5309 | + */ |
|
| 5310 | + protected function _deduce_fields_n_values_from_cols_n_values($cols_n_values) |
|
| 5311 | + { |
|
| 5312 | + $this_model_fields_n_values = array(); |
|
| 5313 | + foreach ($this->get_tables() as $table_alias => $table_obj) { |
|
| 5314 | + $table_pk_value = $this->_get_column_value_with_table_alias_or_not($cols_n_values, |
|
| 5315 | + $table_obj->get_fully_qualified_pk_column(), $table_obj->get_pk_column()); |
|
| 5316 | + //there is a primary key on this table and its not set. Use defaults for all its columns |
|
| 5317 | + if ($table_pk_value === null && $table_obj->get_pk_column()) { |
|
| 5318 | + foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
| 5319 | + if (! $field_obj->is_db_only_field()) { |
|
| 5320 | + //prepare field as if its coming from db |
|
| 5321 | + $prepared_value = $field_obj->prepare_for_set($field_obj->get_default_value()); |
|
| 5322 | + $this_model_fields_n_values[$field_name] = $field_obj->prepare_for_use_in_db($prepared_value); |
|
| 5323 | + } |
|
| 5324 | + } |
|
| 5325 | + } else { |
|
| 5326 | + //the table's rows existed. Use their values |
|
| 5327 | + foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
|
| 5328 | + if (! $field_obj->is_db_only_field()) { |
|
| 5329 | + $this_model_fields_n_values[$field_name] = $this->_get_column_value_with_table_alias_or_not( |
|
| 5330 | + $cols_n_values, $field_obj->get_qualified_column(), |
|
| 5331 | + $field_obj->get_table_column() |
|
| 5332 | + ); |
|
| 5333 | + } |
|
| 5334 | + } |
|
| 5335 | + } |
|
| 5336 | + } |
|
| 5337 | + return $this_model_fields_n_values; |
|
| 5338 | + } |
|
| 5339 | + |
|
| 5340 | + |
|
| 5341 | + |
|
| 5342 | + /** |
|
| 5343 | + * @param $cols_n_values |
|
| 5344 | + * @param $qualified_column |
|
| 5345 | + * @param $regular_column |
|
| 5346 | + * @return null |
|
| 5347 | + */ |
|
| 5348 | + protected function _get_column_value_with_table_alias_or_not($cols_n_values, $qualified_column, $regular_column) |
|
| 5349 | + { |
|
| 5350 | + $value = null; |
|
| 5351 | + //ask the field what it think it's table_name.column_name should be, and call it the "qualified column" |
|
| 5352 | + //does the field on the model relate to this column retrieved from the db? |
|
| 5353 | + //or is it a db-only field? (not relating to the model) |
|
| 5354 | + if (isset($cols_n_values[$qualified_column])) { |
|
| 5355 | + $value = $cols_n_values[$qualified_column]; |
|
| 5356 | + } elseif (isset($cols_n_values[$regular_column])) { |
|
| 5357 | + $value = $cols_n_values[$regular_column]; |
|
| 5358 | + } |
|
| 5359 | + return $value; |
|
| 5360 | + } |
|
| 5361 | + |
|
| 5362 | + |
|
| 5363 | + |
|
| 5364 | + /** |
|
| 5365 | + * refresh_entity_map_from_db |
|
| 5366 | + * Makes sure the model object in the entity map at $id assumes the values |
|
| 5367 | + * of the database (opposite of EE_base_Class::save()) |
|
| 5368 | + * |
|
| 5369 | + * @param int|string $id |
|
| 5370 | + * @return EE_Base_Class |
|
| 5371 | + * @throws EE_Error |
|
| 5372 | + */ |
|
| 5373 | + public function refresh_entity_map_from_db($id) |
|
| 5374 | + { |
|
| 5375 | + $obj_in_map = $this->get_from_entity_map($id); |
|
| 5376 | + if ($obj_in_map) { |
|
| 5377 | + $wpdb_results = $this->_get_all_wpdb_results( |
|
| 5378 | + array(array($this->get_primary_key_field()->get_name() => $id), 'limit' => 1) |
|
| 5379 | + ); |
|
| 5380 | + if ($wpdb_results && is_array($wpdb_results)) { |
|
| 5381 | + $one_row = reset($wpdb_results); |
|
| 5382 | + foreach ($this->_deduce_fields_n_values_from_cols_n_values($one_row) as $field_name => $db_value) { |
|
| 5383 | + $obj_in_map->set_from_db($field_name, $db_value); |
|
| 5384 | + } |
|
| 5385 | + //clear the cache of related model objects |
|
| 5386 | + foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
| 5387 | + $obj_in_map->clear_cache($relation_name, null, true); |
|
| 5388 | + } |
|
| 5389 | + } |
|
| 5390 | + $this->_entity_map[EEM_Base::$_model_query_blog_id][$id] = $obj_in_map; |
|
| 5391 | + return $obj_in_map; |
|
| 5392 | + } |
|
| 5393 | + return $this->get_one_by_ID($id); |
|
| 5394 | + } |
|
| 5395 | + |
|
| 5396 | + |
|
| 5397 | + |
|
| 5398 | + /** |
|
| 5399 | + * refresh_entity_map_with |
|
| 5400 | + * Leaves the entry in the entity map alone, but updates it to match the provided |
|
| 5401 | + * $replacing_model_obj (which we assume to be its equivalent but somehow NOT in the entity map). |
|
| 5402 | + * This is useful if you have a model object you want to make authoritative over what's in the entity map currently. |
|
| 5403 | + * Note: The old $replacing_model_obj should now be destroyed as it's now un-authoritative |
|
| 5404 | + * |
|
| 5405 | + * @param int|string $id |
|
| 5406 | + * @param EE_Base_Class $replacing_model_obj |
|
| 5407 | + * @return \EE_Base_Class |
|
| 5408 | + * @throws EE_Error |
|
| 5409 | + */ |
|
| 5410 | + public function refresh_entity_map_with($id, $replacing_model_obj) |
|
| 5411 | + { |
|
| 5412 | + $obj_in_map = $this->get_from_entity_map($id); |
|
| 5413 | + if ($obj_in_map) { |
|
| 5414 | + if ($replacing_model_obj instanceof EE_Base_Class) { |
|
| 5415 | + foreach ($replacing_model_obj->model_field_array() as $field_name => $value) { |
|
| 5416 | + $obj_in_map->set($field_name, $value); |
|
| 5417 | + } |
|
| 5418 | + //make the model object in the entity map's cache match the $replacing_model_obj |
|
| 5419 | + foreach ($this->relation_settings() as $relation_name => $relation_obj) { |
|
| 5420 | + $obj_in_map->clear_cache($relation_name, null, true); |
|
| 5421 | + foreach ($replacing_model_obj->get_all_from_cache($relation_name) as $cache_id => $cached_obj) { |
|
| 5422 | + $obj_in_map->cache($relation_name, $cached_obj, $cache_id); |
|
| 5423 | + } |
|
| 5424 | + } |
|
| 5425 | + } |
|
| 5426 | + return $obj_in_map; |
|
| 5427 | + } |
|
| 5428 | + $this->add_to_entity_map($replacing_model_obj); |
|
| 5429 | + return $replacing_model_obj; |
|
| 5430 | + } |
|
| 5431 | + |
|
| 5432 | + |
|
| 5433 | + |
|
| 5434 | + /** |
|
| 5435 | + * Gets the EE class that corresponds to this model. Eg, for EEM_Answer that |
|
| 5436 | + * would be EE_Answer.To import that class, you'd just add ".class.php" to the name, like so |
|
| 5437 | + * require_once($this->_getClassName().".class.php"); |
|
| 5438 | + * |
|
| 5439 | + * @return string |
|
| 5440 | + */ |
|
| 5441 | + private function _get_class_name() |
|
| 5442 | + { |
|
| 5443 | + return "EE_" . $this->get_this_model_name(); |
|
| 5444 | + } |
|
| 5445 | + |
|
| 5446 | + |
|
| 5447 | + |
|
| 5448 | + /** |
|
| 5449 | + * Get the name of the items this model represents, for the quantity specified. Eg, |
|
| 5450 | + * if $quantity==1, on EEM_Event, it would 'Event' (internationalized), otherwise |
|
| 5451 | + * it would be 'Events'. |
|
| 5452 | + * |
|
| 5453 | + * @param int $quantity |
|
| 5454 | + * @return string |
|
| 5455 | + */ |
|
| 5456 | + public function item_name($quantity = 1) |
|
| 5457 | + { |
|
| 5458 | + return (int)$quantity === 1 ? $this->singular_item : $this->plural_item; |
|
| 5459 | + } |
|
| 5460 | + |
|
| 5461 | + |
|
| 5462 | + |
|
| 5463 | + /** |
|
| 5464 | + * Very handy general function to allow for plugins to extend any child of EE_TempBase. |
|
| 5465 | + * If a method is called on a child of EE_TempBase that doesn't exist, this function is called |
|
| 5466 | + * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
|
| 5467 | + * requiring a plugin to extend the EE_TempBase (which works fine is there's only 1 plugin, but when will that |
|
| 5468 | + * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
|
| 5469 | + * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
|
| 5470 | + * was called, and an array of the original arguments passed to the function. Whatever their callback function |
|
| 5471 | + * returns will be returned by this function. Example: in functions.php (or in a plugin): |
|
| 5472 | + * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
|
| 5473 | + * my_callback($previousReturnValue,EE_TempBase $object,$argsArray){ |
|
| 5474 | + * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 5475 | + * return $previousReturnValue.$returnString; |
|
| 5476 | + * } |
|
| 5477 | + * require('EEM_Answer.model.php'); |
|
| 5478 | + * $answer=EEM_Answer::instance(); |
|
| 5479 | + * echo $answer->my_callback('monkeys',100); |
|
| 5480 | + * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 5481 | + * |
|
| 5482 | + * @param string $methodName name of method which was called on a child of EE_TempBase, but which |
|
| 5483 | + * @param array $args array of original arguments passed to the function |
|
| 5484 | + * @throws EE_Error |
|
| 5485 | + * @return mixed whatever the plugin which calls add_filter decides |
|
| 5486 | + */ |
|
| 5487 | + public function __call($methodName, $args) |
|
| 5488 | + { |
|
| 5489 | + $className = get_class($this); |
|
| 5490 | + $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 5491 | + if (! has_filter($tagName)) { |
|
| 5492 | + throw new EE_Error( |
|
| 5493 | + sprintf( |
|
| 5494 | + __('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 );', |
|
| 5495 | + 'event_espresso'), |
|
| 5496 | + $methodName, |
|
| 5497 | + $className, |
|
| 5498 | + $tagName, |
|
| 5499 | + '<br />' |
|
| 5500 | + ) |
|
| 5501 | + ); |
|
| 5502 | + } |
|
| 5503 | + return apply_filters($tagName, null, $this, $args); |
|
| 5504 | + } |
|
| 5505 | + |
|
| 5506 | + |
|
| 5507 | + |
|
| 5508 | + /** |
|
| 5509 | + * Ensures $base_class_obj_or_id is of the EE_Base_Class child that corresponds ot this model. |
|
| 5510 | + * If not, assumes its an ID, and uses $this->get_one_by_ID() to get the EE_Base_Class. |
|
| 5511 | + * |
|
| 5512 | + * @param EE_Base_Class|string|int $base_class_obj_or_id either: |
|
| 5513 | + * the EE_Base_Class object that corresponds to this Model, |
|
| 5514 | + * the object's class name |
|
| 5515 | + * or object's ID |
|
| 5516 | + * @param boolean $ensure_is_in_db if set, we will also verify this model object |
|
| 5517 | + * exists in the database. If it does not, we add it |
|
| 5518 | + * @throws EE_Error |
|
| 5519 | + * @return EE_Base_Class |
|
| 5520 | + */ |
|
| 5521 | + public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false) |
|
| 5522 | + { |
|
| 5523 | + $className = $this->_get_class_name(); |
|
| 5524 | + if ($base_class_obj_or_id instanceof $className) { |
|
| 5525 | + $model_object = $base_class_obj_or_id; |
|
| 5526 | + } else { |
|
| 5527 | + $primary_key_field = $this->get_primary_key_field(); |
|
| 5528 | + if ( |
|
| 5529 | + $primary_key_field instanceof EE_Primary_Key_Int_Field |
|
| 5530 | + && ( |
|
| 5531 | + is_int($base_class_obj_or_id) |
|
| 5532 | + || is_string($base_class_obj_or_id) |
|
| 5533 | + ) |
|
| 5534 | + ) { |
|
| 5535 | + // assume it's an ID. |
|
| 5536 | + // either a proper integer or a string representing an integer (eg "101" instead of 101) |
|
| 5537 | + $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
| 5538 | + } else if ( |
|
| 5539 | + $primary_key_field instanceof EE_Primary_Key_String_Field |
|
| 5540 | + && is_string($base_class_obj_or_id) |
|
| 5541 | + ) { |
|
| 5542 | + // assume its a string representation of the object |
|
| 5543 | + $model_object = $this->get_one_by_ID($base_class_obj_or_id); |
|
| 5544 | + } else { |
|
| 5545 | + throw new EE_Error( |
|
| 5546 | + sprintf( |
|
| 5547 | + __( |
|
| 5548 | + "'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
| 5549 | + 'event_espresso' |
|
| 5550 | + ), |
|
| 5551 | + $base_class_obj_or_id, |
|
| 5552 | + $this->_get_class_name(), |
|
| 5553 | + print_r($base_class_obj_or_id, true) |
|
| 5554 | + ) |
|
| 5555 | + ); |
|
| 5556 | + } |
|
| 5557 | + } |
|
| 5558 | + if ($ensure_is_in_db && $model_object->ID() !== null) { |
|
| 5559 | + $model_object->save(); |
|
| 5560 | + } |
|
| 5561 | + return $model_object; |
|
| 5562 | + } |
|
| 5563 | + |
|
| 5564 | + |
|
| 5565 | + |
|
| 5566 | + /** |
|
| 5567 | + * Similar to ensure_is_obj(), this method makes sure $base_class_obj_or_id |
|
| 5568 | + * is a value of the this model's primary key. If it's an EE_Base_Class child, |
|
| 5569 | + * returns it ID. |
|
| 5570 | + * |
|
| 5571 | + * @param EE_Base_Class|int|string $base_class_obj_or_id |
|
| 5572 | + * @return int|string depending on the type of this model object's ID |
|
| 5573 | + * @throws EE_Error |
|
| 5574 | + */ |
|
| 5575 | + public function ensure_is_ID($base_class_obj_or_id) |
|
| 5576 | + { |
|
| 5577 | + $className = $this->_get_class_name(); |
|
| 5578 | + if ($base_class_obj_or_id instanceof $className) { |
|
| 5579 | + /** @var $base_class_obj_or_id EE_Base_Class */ |
|
| 5580 | + $id = $base_class_obj_or_id->ID(); |
|
| 5581 | + } elseif (is_int($base_class_obj_or_id)) { |
|
| 5582 | + //assume it's an ID |
|
| 5583 | + $id = $base_class_obj_or_id; |
|
| 5584 | + } elseif (is_string($base_class_obj_or_id)) { |
|
| 5585 | + //assume its a string representation of the object |
|
| 5586 | + $id = $base_class_obj_or_id; |
|
| 5587 | + } else { |
|
| 5588 | + throw new EE_Error(sprintf(__("'%s' is neither an object of type %s, nor an ID! Its full value is '%s'", |
|
| 5589 | + 'event_espresso'), $base_class_obj_or_id, $this->_get_class_name(), |
|
| 5590 | + print_r($base_class_obj_or_id, true))); |
|
| 5591 | + } |
|
| 5592 | + return $id; |
|
| 5593 | + } |
|
| 5594 | + |
|
| 5595 | + |
|
| 5596 | + |
|
| 5597 | + /** |
|
| 5598 | + * Sets whether the values passed to the model (eg, values in WHERE, values in INSERT, UPDATE, etc) |
|
| 5599 | + * have already been ran through the appropriate model field's prepare_for_use_in_db method. IE, they have |
|
| 5600 | + * been sanitized and converted into the appropriate domain. |
|
| 5601 | + * Usually the only place you'll want to change the default (which is to assume values have NOT been sanitized by |
|
| 5602 | + * the model object/model field) is when making a method call from WITHIN a model object, which has direct access |
|
| 5603 | + * to its sanitized values. Note: after changing this setting, you should set it back to its previous value (using |
|
| 5604 | + * get_assumption_concerning_values_already_prepared_by_model_object()) eg. |
|
| 5605 | + * $EVT = EEM_Event::instance(); $old_setting = |
|
| 5606 | + * $EVT->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 5607 | + * $EVT->assume_values_already_prepared_by_model_object(true); |
|
| 5608 | + * $EVT->update(array('foo'=>'bar'),array(array('foo'=>'monkey'))); |
|
| 5609 | + * $EVT->assume_values_already_prepared_by_model_object($old_setting); |
|
| 5610 | + * |
|
| 5611 | + * @param int $values_already_prepared like one of the constants on EEM_Base |
|
| 5612 | + * @return void |
|
| 5613 | + */ |
|
| 5614 | + public function assume_values_already_prepared_by_model_object( |
|
| 5615 | + $values_already_prepared = self::not_prepared_by_model_object |
|
| 5616 | + ) { |
|
| 5617 | + $this->_values_already_prepared_by_model_object = $values_already_prepared; |
|
| 5618 | + } |
|
| 5619 | + |
|
| 5620 | + |
|
| 5621 | + |
|
| 5622 | + /** |
|
| 5623 | + * Read comments for assume_values_already_prepared_by_model_object() |
|
| 5624 | + * |
|
| 5625 | + * @return int |
|
| 5626 | + */ |
|
| 5627 | + public function get_assumption_concerning_values_already_prepared_by_model_object() |
|
| 5628 | + { |
|
| 5629 | + return $this->_values_already_prepared_by_model_object; |
|
| 5630 | + } |
|
| 5631 | + |
|
| 5632 | + |
|
| 5633 | + |
|
| 5634 | + /** |
|
| 5635 | + * Gets all the indexes on this model |
|
| 5636 | + * |
|
| 5637 | + * @return EE_Index[] |
|
| 5638 | + */ |
|
| 5639 | + public function indexes() |
|
| 5640 | + { |
|
| 5641 | + return $this->_indexes; |
|
| 5642 | + } |
|
| 5643 | + |
|
| 5644 | + |
|
| 5645 | + |
|
| 5646 | + /** |
|
| 5647 | + * Gets all the Unique Indexes on this model |
|
| 5648 | + * |
|
| 5649 | + * @return EE_Unique_Index[] |
|
| 5650 | + */ |
|
| 5651 | + public function unique_indexes() |
|
| 5652 | + { |
|
| 5653 | + $unique_indexes = array(); |
|
| 5654 | + foreach ($this->_indexes as $name => $index) { |
|
| 5655 | + if ($index instanceof EE_Unique_Index) { |
|
| 5656 | + $unique_indexes [$name] = $index; |
|
| 5657 | + } |
|
| 5658 | + } |
|
| 5659 | + return $unique_indexes; |
|
| 5660 | + } |
|
| 5661 | + |
|
| 5662 | + |
|
| 5663 | + |
|
| 5664 | + /** |
|
| 5665 | + * Gets all the fields which, when combined, make the primary key. |
|
| 5666 | + * This is usually just an array with 1 element (the primary key), but in cases |
|
| 5667 | + * where there is no primary key, it's a combination of fields as defined |
|
| 5668 | + * on a primary index |
|
| 5669 | + * |
|
| 5670 | + * @return EE_Model_Field_Base[] indexed by the field's name |
|
| 5671 | + * @throws EE_Error |
|
| 5672 | + */ |
|
| 5673 | + public function get_combined_primary_key_fields() |
|
| 5674 | + { |
|
| 5675 | + foreach ($this->indexes() as $index) { |
|
| 5676 | + if ($index instanceof EE_Primary_Key_Index) { |
|
| 5677 | + return $index->fields(); |
|
| 5678 | + } |
|
| 5679 | + } |
|
| 5680 | + return array($this->primary_key_name() => $this->get_primary_key_field()); |
|
| 5681 | + } |
|
| 5682 | + |
|
| 5683 | + |
|
| 5684 | + |
|
| 5685 | + /** |
|
| 5686 | + * Used to build a primary key string (when the model has no primary key), |
|
| 5687 | + * which can be used a unique string to identify this model object. |
|
| 5688 | + * |
|
| 5689 | + * @param array $cols_n_values keys are field names, values are their values |
|
| 5690 | + * @return string |
|
| 5691 | + * @throws EE_Error |
|
| 5692 | + */ |
|
| 5693 | + public function get_index_primary_key_string($cols_n_values) |
|
| 5694 | + { |
|
| 5695 | + $cols_n_values_for_primary_key_index = array_intersect_key($cols_n_values, |
|
| 5696 | + $this->get_combined_primary_key_fields()); |
|
| 5697 | + return http_build_query($cols_n_values_for_primary_key_index); |
|
| 5698 | + } |
|
| 5699 | + |
|
| 5700 | + |
|
| 5701 | + |
|
| 5702 | + /** |
|
| 5703 | + * Gets the field values from the primary key string |
|
| 5704 | + * |
|
| 5705 | + * @see EEM_Base::get_combined_primary_key_fields() and EEM_Base::get_index_primary_key_string() |
|
| 5706 | + * @param string $index_primary_key_string |
|
| 5707 | + * @return null|array |
|
| 5708 | + * @throws EE_Error |
|
| 5709 | + */ |
|
| 5710 | + public function parse_index_primary_key_string($index_primary_key_string) |
|
| 5711 | + { |
|
| 5712 | + $key_fields = $this->get_combined_primary_key_fields(); |
|
| 5713 | + //check all of them are in the $id |
|
| 5714 | + $key_vals_in_combined_pk = array(); |
|
| 5715 | + parse_str($index_primary_key_string, $key_vals_in_combined_pk); |
|
| 5716 | + foreach ($key_fields as $key_field_name => $field_obj) { |
|
| 5717 | + if (! isset($key_vals_in_combined_pk[$key_field_name])) { |
|
| 5718 | + return null; |
|
| 5719 | + } |
|
| 5720 | + } |
|
| 5721 | + return $key_vals_in_combined_pk; |
|
| 5722 | + } |
|
| 5723 | + |
|
| 5724 | + |
|
| 5725 | + |
|
| 5726 | + /** |
|
| 5727 | + * verifies that an array of key-value pairs for model fields has a key |
|
| 5728 | + * for each field comprising the primary key index |
|
| 5729 | + * |
|
| 5730 | + * @param array $key_vals |
|
| 5731 | + * @return boolean |
|
| 5732 | + * @throws EE_Error |
|
| 5733 | + */ |
|
| 5734 | + public function has_all_combined_primary_key_fields($key_vals) |
|
| 5735 | + { |
|
| 5736 | + $keys_it_should_have = array_keys($this->get_combined_primary_key_fields()); |
|
| 5737 | + foreach ($keys_it_should_have as $key) { |
|
| 5738 | + if (! isset($key_vals[$key])) { |
|
| 5739 | + return false; |
|
| 5740 | + } |
|
| 5741 | + } |
|
| 5742 | + return true; |
|
| 5743 | + } |
|
| 5744 | + |
|
| 5745 | + |
|
| 5746 | + |
|
| 5747 | + /** |
|
| 5748 | + * Finds all model objects in the DB that appear to be a copy of $model_object_or_attributes_array. |
|
| 5749 | + * We consider something to be a copy if all the attributes match (except the ID, of course). |
|
| 5750 | + * |
|
| 5751 | + * @param array|EE_Base_Class $model_object_or_attributes_array If its an array, it's field-value pairs |
|
| 5752 | + * @param array $query_params like EEM_Base::get_all's query_params. |
|
| 5753 | + * @throws EE_Error |
|
| 5754 | + * @return \EE_Base_Class[] Array keys are object IDs (if there is a primary key on the model. if not, numerically |
|
| 5755 | + * indexed) |
|
| 5756 | + */ |
|
| 5757 | + public function get_all_copies($model_object_or_attributes_array, $query_params = array()) |
|
| 5758 | + { |
|
| 5759 | + if ($model_object_or_attributes_array instanceof EE_Base_Class) { |
|
| 5760 | + $attributes_array = $model_object_or_attributes_array->model_field_array(); |
|
| 5761 | + } elseif (is_array($model_object_or_attributes_array)) { |
|
| 5762 | + $attributes_array = $model_object_or_attributes_array; |
|
| 5763 | + } else { |
|
| 5764 | + throw new EE_Error(sprintf(__("get_all_copies should be provided with either a model object or an array of field-value-pairs, but was given %s", |
|
| 5765 | + "event_espresso"), $model_object_or_attributes_array)); |
|
| 5766 | + } |
|
| 5767 | + //even copies obviously won't have the same ID, so remove the primary key |
|
| 5768 | + //from the WHERE conditions for finding copies (if there is a primary key, of course) |
|
| 5769 | + if ($this->has_primary_key_field() && isset($attributes_array[$this->primary_key_name()])) { |
|
| 5770 | + unset($attributes_array[$this->primary_key_name()]); |
|
| 5771 | + } |
|
| 5772 | + if (isset($query_params[0])) { |
|
| 5773 | + $query_params[0] = array_merge($attributes_array, $query_params); |
|
| 5774 | + } else { |
|
| 5775 | + $query_params[0] = $attributes_array; |
|
| 5776 | + } |
|
| 5777 | + return $this->get_all($query_params); |
|
| 5778 | + } |
|
| 5779 | + |
|
| 5780 | + |
|
| 5781 | + |
|
| 5782 | + /** |
|
| 5783 | + * Gets the first copy we find. See get_all_copies for more details |
|
| 5784 | + * |
|
| 5785 | + * @param mixed EE_Base_Class | array $model_object_or_attributes_array |
|
| 5786 | + * @param array $query_params |
|
| 5787 | + * @return EE_Base_Class |
|
| 5788 | + * @throws EE_Error |
|
| 5789 | + */ |
|
| 5790 | + public function get_one_copy($model_object_or_attributes_array, $query_params = array()) |
|
| 5791 | + { |
|
| 5792 | + if (! is_array($query_params)) { |
|
| 5793 | + EE_Error::doing_it_wrong('EEM_Base::get_one_copy', |
|
| 5794 | + sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
|
| 5795 | + gettype($query_params)), '4.6.0'); |
|
| 5796 | + $query_params = array(); |
|
| 5797 | + } |
|
| 5798 | + $query_params['limit'] = 1; |
|
| 5799 | + $copies = $this->get_all_copies($model_object_or_attributes_array, $query_params); |
|
| 5800 | + if (is_array($copies)) { |
|
| 5801 | + return array_shift($copies); |
|
| 5802 | + } |
|
| 5803 | + return null; |
|
| 5804 | + } |
|
| 5805 | + |
|
| 5806 | + |
|
| 5807 | + |
|
| 5808 | + /** |
|
| 5809 | + * Updates the item with the specified id. Ignores default query parameters because |
|
| 5810 | + * we have specified the ID, and its assumed we KNOW what we're doing |
|
| 5811 | + * |
|
| 5812 | + * @param array $fields_n_values keys are field names, values are their new values |
|
| 5813 | + * @param int|string $id the value of the primary key to update |
|
| 5814 | + * @return int number of rows updated |
|
| 5815 | + * @throws EE_Error |
|
| 5816 | + */ |
|
| 5817 | + public function update_by_ID($fields_n_values, $id) |
|
| 5818 | + { |
|
| 5819 | + $query_params = array( |
|
| 5820 | + 0 => array($this->get_primary_key_field()->get_name() => $id), |
|
| 5821 | + 'default_where_conditions' => EEM_Base::default_where_conditions_others_only, |
|
| 5822 | + ); |
|
| 5823 | + return $this->update($fields_n_values, $query_params); |
|
| 5824 | + } |
|
| 5825 | + |
|
| 5826 | + |
|
| 5827 | + |
|
| 5828 | + /** |
|
| 5829 | + * Changes an operator which was supplied to the models into one usable in SQL |
|
| 5830 | + * |
|
| 5831 | + * @param string $operator_supplied |
|
| 5832 | + * @return string an operator which can be used in SQL |
|
| 5833 | + * @throws EE_Error |
|
| 5834 | + */ |
|
| 5835 | + private function _prepare_operator_for_sql($operator_supplied) |
|
| 5836 | + { |
|
| 5837 | + $sql_operator = isset($this->_valid_operators[$operator_supplied]) ? $this->_valid_operators[$operator_supplied] |
|
| 5838 | + : null; |
|
| 5839 | + if ($sql_operator) { |
|
| 5840 | + return $sql_operator; |
|
| 5841 | + } |
|
| 5842 | + throw new EE_Error( |
|
| 5843 | + sprintf( |
|
| 5844 | + __( |
|
| 5845 | + "The operator '%s' is not in the list of valid operators: %s", |
|
| 5846 | + "event_espresso" |
|
| 5847 | + ), $operator_supplied, implode(",", array_keys($this->_valid_operators)) |
|
| 5848 | + ) |
|
| 5849 | + ); |
|
| 5850 | + } |
|
| 5851 | + |
|
| 5852 | + |
|
| 5853 | + |
|
| 5854 | + /** |
|
| 5855 | + * Gets the valid operators |
|
| 5856 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5857 | + */ |
|
| 5858 | + public function valid_operators(){ |
|
| 5859 | + return $this->_valid_operators; |
|
| 5860 | + } |
|
| 5861 | + |
|
| 5862 | + |
|
| 5863 | + |
|
| 5864 | + /** |
|
| 5865 | + * Gets the between-style operators (take 2 arguments). |
|
| 5866 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5867 | + */ |
|
| 5868 | + public function valid_between_style_operators() |
|
| 5869 | + { |
|
| 5870 | + return array_intersect( |
|
| 5871 | + $this->valid_operators(), |
|
| 5872 | + $this->_between_style_operators |
|
| 5873 | + ); |
|
| 5874 | + } |
|
| 5875 | + |
|
| 5876 | + /** |
|
| 5877 | + * Gets the "like"-style operators (take a single argument, but it may contain wildcards) |
|
| 5878 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5879 | + */ |
|
| 5880 | + public function valid_like_style_operators() |
|
| 5881 | + { |
|
| 5882 | + return array_intersect( |
|
| 5883 | + $this->valid_operators(), |
|
| 5884 | + $this->_like_style_operators |
|
| 5885 | + ); |
|
| 5886 | + } |
|
| 5887 | + |
|
| 5888 | + /** |
|
| 5889 | + * Gets the "in"-style operators |
|
| 5890 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5891 | + */ |
|
| 5892 | + public function valid_in_style_operators() |
|
| 5893 | + { |
|
| 5894 | + return array_intersect( |
|
| 5895 | + $this->valid_operators(), |
|
| 5896 | + $this->_in_style_operators |
|
| 5897 | + ); |
|
| 5898 | + } |
|
| 5899 | + |
|
| 5900 | + /** |
|
| 5901 | + * Gets the "null"-style operators (accept no arguments) |
|
| 5902 | + * @return array keys are accepted strings, values are the SQL they are converted to |
|
| 5903 | + */ |
|
| 5904 | + public function valid_null_style_operators() |
|
| 5905 | + { |
|
| 5906 | + return array_intersect( |
|
| 5907 | + $this->valid_operators(), |
|
| 5908 | + $this->_null_style_operators |
|
| 5909 | + ); |
|
| 5910 | + } |
|
| 5911 | + |
|
| 5912 | + /** |
|
| 5913 | + * Gets an array where keys are the primary keys and values are their 'names' |
|
| 5914 | + * (as determined by the model object's name() function, which is often overridden) |
|
| 5915 | + * |
|
| 5916 | + * @param array $query_params like get_all's |
|
| 5917 | + * @return string[] |
|
| 5918 | + * @throws EE_Error |
|
| 5919 | + */ |
|
| 5920 | + public function get_all_names($query_params = array()) |
|
| 5921 | + { |
|
| 5922 | + $objs = $this->get_all($query_params); |
|
| 5923 | + $names = array(); |
|
| 5924 | + foreach ($objs as $obj) { |
|
| 5925 | + $names[$obj->ID()] = $obj->name(); |
|
| 5926 | + } |
|
| 5927 | + return $names; |
|
| 5928 | + } |
|
| 5929 | + |
|
| 5930 | + |
|
| 5931 | + |
|
| 5932 | + /** |
|
| 5933 | + * Gets an array of primary keys from the model objects. If you acquired the model objects |
|
| 5934 | + * using EEM_Base::get_all() you don't need to call this (and probably shouldn't because |
|
| 5935 | + * this is duplicated effort and reduces efficiency) you would be better to use |
|
| 5936 | + * array_keys() on $model_objects. |
|
| 5937 | + * |
|
| 5938 | + * @param \EE_Base_Class[] $model_objects |
|
| 5939 | + * @param boolean $filter_out_empty_ids if a model object has an ID of '' or 0, don't bother including it |
|
| 5940 | + * in the returned array |
|
| 5941 | + * @return array |
|
| 5942 | + * @throws EE_Error |
|
| 5943 | + */ |
|
| 5944 | + public function get_IDs($model_objects, $filter_out_empty_ids = false) |
|
| 5945 | + { |
|
| 5946 | + if (! $this->has_primary_key_field()) { |
|
| 5947 | + if (WP_DEBUG) { |
|
| 5948 | + EE_Error::add_error( |
|
| 5949 | + __('Trying to get IDs from a model than has no primary key', 'event_espresso'), |
|
| 5950 | + __FILE__, |
|
| 5951 | + __FUNCTION__, |
|
| 5952 | + __LINE__ |
|
| 5953 | + ); |
|
| 5954 | + } |
|
| 5955 | + } |
|
| 5956 | + $IDs = array(); |
|
| 5957 | + foreach ($model_objects as $model_object) { |
|
| 5958 | + $id = $model_object->ID(); |
|
| 5959 | + if (! $id) { |
|
| 5960 | + if ($filter_out_empty_ids) { |
|
| 5961 | + continue; |
|
| 5962 | + } |
|
| 5963 | + if (WP_DEBUG) { |
|
| 5964 | + EE_Error::add_error( |
|
| 5965 | + __( |
|
| 5966 | + 'Called %1$s on a model object that has no ID and so probably hasn\'t been saved to the database', |
|
| 5967 | + 'event_espresso' |
|
| 5968 | + ), |
|
| 5969 | + __FILE__, |
|
| 5970 | + __FUNCTION__, |
|
| 5971 | + __LINE__ |
|
| 5972 | + ); |
|
| 5973 | + } |
|
| 5974 | + } |
|
| 5975 | + $IDs[] = $id; |
|
| 5976 | + } |
|
| 5977 | + return $IDs; |
|
| 5978 | + } |
|
| 5979 | + |
|
| 5980 | + |
|
| 5981 | + |
|
| 5982 | + /** |
|
| 5983 | + * Returns the string used in capabilities relating to this model. If there |
|
| 5984 | + * are no capabilities that relate to this model returns false |
|
| 5985 | + * |
|
| 5986 | + * @return string|false |
|
| 5987 | + */ |
|
| 5988 | + public function cap_slug() |
|
| 5989 | + { |
|
| 5990 | + return apply_filters('FHEE__EEM_Base__cap_slug', $this->_caps_slug, $this); |
|
| 5991 | + } |
|
| 5992 | + |
|
| 5993 | + |
|
| 5994 | + |
|
| 5995 | + /** |
|
| 5996 | + * Returns the capability-restrictions array (@see EEM_Base::_cap_restrictions). |
|
| 5997 | + * If $context is provided (which should be set to one of EEM_Base::valid_cap_contexts()) |
|
| 5998 | + * only returns the cap restrictions array in that context (ie, the array |
|
| 5999 | + * at that key) |
|
| 6000 | + * |
|
| 6001 | + * @param string $context |
|
| 6002 | + * @return EE_Default_Where_Conditions[] indexed by associated capability |
|
| 6003 | + * @throws EE_Error |
|
| 6004 | + */ |
|
| 6005 | + public function cap_restrictions($context = EEM_Base::caps_read) |
|
| 6006 | + { |
|
| 6007 | + EEM_Base::verify_is_valid_cap_context($context); |
|
| 6008 | + //check if we ought to run the restriction generator first |
|
| 6009 | + if ( |
|
| 6010 | + isset($this->_cap_restriction_generators[$context]) |
|
| 6011 | + && $this->_cap_restriction_generators[$context] instanceof EE_Restriction_Generator_Base |
|
| 6012 | + && ! $this->_cap_restriction_generators[$context]->has_generated_cap_restrictions() |
|
| 6013 | + ) { |
|
| 6014 | + $this->_cap_restrictions[$context] = array_merge( |
|
| 6015 | + $this->_cap_restrictions[$context], |
|
| 6016 | + $this->_cap_restriction_generators[$context]->generate_restrictions() |
|
| 6017 | + ); |
|
| 6018 | + } |
|
| 6019 | + //and make sure we've finalized the construction of each restriction |
|
| 6020 | + foreach ($this->_cap_restrictions[$context] as $where_conditions_obj) { |
|
| 6021 | + if ($where_conditions_obj instanceof EE_Default_Where_Conditions) { |
|
| 6022 | + $where_conditions_obj->_finalize_construct($this); |
|
| 6023 | + } |
|
| 6024 | + } |
|
| 6025 | + return $this->_cap_restrictions[$context]; |
|
| 6026 | + } |
|
| 6027 | + |
|
| 6028 | + |
|
| 6029 | + |
|
| 6030 | + /** |
|
| 6031 | + * Indicating whether or not this model thinks its a wp core model |
|
| 6032 | + * |
|
| 6033 | + * @return boolean |
|
| 6034 | + */ |
|
| 6035 | + public function is_wp_core_model() |
|
| 6036 | + { |
|
| 6037 | + return $this->_wp_core_model; |
|
| 6038 | + } |
|
| 6039 | + |
|
| 6040 | + |
|
| 6041 | + |
|
| 6042 | + /** |
|
| 6043 | + * Gets all the caps that are missing which impose a restriction on |
|
| 6044 | + * queries made in this context |
|
| 6045 | + * |
|
| 6046 | + * @param string $context one of EEM_Base::caps_ constants |
|
| 6047 | + * @return EE_Default_Where_Conditions[] indexed by capability name |
|
| 6048 | + * @throws EE_Error |
|
| 6049 | + */ |
|
| 6050 | + public function caps_missing($context = EEM_Base::caps_read) |
|
| 6051 | + { |
|
| 6052 | + $missing_caps = array(); |
|
| 6053 | + $cap_restrictions = $this->cap_restrictions($context); |
|
| 6054 | + foreach ($cap_restrictions as $cap => $restriction_if_no_cap) { |
|
| 6055 | + if (! EE_Capabilities::instance() |
|
| 6056 | + ->current_user_can($cap, $this->get_this_model_name() . '_model_applying_caps') |
|
| 6057 | + ) { |
|
| 6058 | + $missing_caps[$cap] = $restriction_if_no_cap; |
|
| 6059 | + } |
|
| 6060 | + } |
|
| 6061 | + return $missing_caps; |
|
| 6062 | + } |
|
| 6063 | + |
|
| 6064 | + |
|
| 6065 | + |
|
| 6066 | + /** |
|
| 6067 | + * Gets the mapping from capability contexts to action strings used in capability names |
|
| 6068 | + * |
|
| 6069 | + * @return array keys are one of EEM_Base::valid_cap_contexts(), and values are usually |
|
| 6070 | + * one of 'read', 'edit', or 'delete' |
|
| 6071 | + */ |
|
| 6072 | + public function cap_contexts_to_cap_action_map() |
|
| 6073 | + { |
|
| 6074 | + return apply_filters('FHEE__EEM_Base__cap_contexts_to_cap_action_map', $this->_cap_contexts_to_cap_action_map, |
|
| 6075 | + $this); |
|
| 6076 | + } |
|
| 6077 | + |
|
| 6078 | + |
|
| 6079 | + |
|
| 6080 | + /** |
|
| 6081 | + * Gets the action string for the specified capability context |
|
| 6082 | + * |
|
| 6083 | + * @param string $context |
|
| 6084 | + * @return string one of EEM_Base::cap_contexts_to_cap_action_map() values |
|
| 6085 | + * @throws EE_Error |
|
| 6086 | + */ |
|
| 6087 | + public function cap_action_for_context($context) |
|
| 6088 | + { |
|
| 6089 | + $mapping = $this->cap_contexts_to_cap_action_map(); |
|
| 6090 | + if (isset($mapping[$context])) { |
|
| 6091 | + return $mapping[$context]; |
|
| 6092 | + } |
|
| 6093 | + if ($action = apply_filters('FHEE__EEM_Base__cap_action_for_context', null, $this, $mapping, $context)) { |
|
| 6094 | + return $action; |
|
| 6095 | + } |
|
| 6096 | + throw new EE_Error( |
|
| 6097 | + sprintf( |
|
| 6098 | + __('Cannot find capability restrictions for context "%1$s", allowed values are:%2$s', 'event_espresso'), |
|
| 6099 | + $context, |
|
| 6100 | + implode(',', array_keys($this->cap_contexts_to_cap_action_map())) |
|
| 6101 | + ) |
|
| 6102 | + ); |
|
| 6103 | + } |
|
| 6104 | + |
|
| 6105 | + |
|
| 6106 | + |
|
| 6107 | + /** |
|
| 6108 | + * Returns all the capability contexts which are valid when querying models |
|
| 6109 | + * |
|
| 6110 | + * @return array |
|
| 6111 | + */ |
|
| 6112 | + public static function valid_cap_contexts() |
|
| 6113 | + { |
|
| 6114 | + return apply_filters('FHEE__EEM_Base__valid_cap_contexts', array( |
|
| 6115 | + self::caps_read, |
|
| 6116 | + self::caps_read_admin, |
|
| 6117 | + self::caps_edit, |
|
| 6118 | + self::caps_delete, |
|
| 6119 | + )); |
|
| 6120 | + } |
|
| 6121 | + |
|
| 6122 | + |
|
| 6123 | + |
|
| 6124 | + /** |
|
| 6125 | + * Returns all valid options for 'default_where_conditions' |
|
| 6126 | + * |
|
| 6127 | + * @return array |
|
| 6128 | + */ |
|
| 6129 | + public static function valid_default_where_conditions() |
|
| 6130 | + { |
|
| 6131 | + return array( |
|
| 6132 | + EEM_Base::default_where_conditions_all, |
|
| 6133 | + EEM_Base::default_where_conditions_this_only, |
|
| 6134 | + EEM_Base::default_where_conditions_others_only, |
|
| 6135 | + EEM_Base::default_where_conditions_minimum_all, |
|
| 6136 | + EEM_Base::default_where_conditions_minimum_others, |
|
| 6137 | + EEM_Base::default_where_conditions_none |
|
| 6138 | + ); |
|
| 6139 | + } |
|
| 6140 | + |
|
| 6141 | + // public static function default_where_conditions_full |
|
| 6142 | + /** |
|
| 6143 | + * Verifies $context is one of EEM_Base::valid_cap_contexts(), if not it throws an exception |
|
| 6144 | + * |
|
| 6145 | + * @param string $context |
|
| 6146 | + * @return bool |
|
| 6147 | + * @throws EE_Error |
|
| 6148 | + */ |
|
| 6149 | + static public function verify_is_valid_cap_context($context) |
|
| 6150 | + { |
|
| 6151 | + $valid_cap_contexts = EEM_Base::valid_cap_contexts(); |
|
| 6152 | + if (in_array($context, $valid_cap_contexts)) { |
|
| 6153 | + return true; |
|
| 6154 | + } |
|
| 6155 | + throw new EE_Error( |
|
| 6156 | + sprintf( |
|
| 6157 | + __( |
|
| 6158 | + 'Context "%1$s" passed into model "%2$s" is not a valid context. They are: %3$s', |
|
| 6159 | + 'event_espresso' |
|
| 6160 | + ), |
|
| 6161 | + $context, |
|
| 6162 | + 'EEM_Base', |
|
| 6163 | + implode(',', $valid_cap_contexts) |
|
| 6164 | + ) |
|
| 6165 | + ); |
|
| 6166 | + } |
|
| 6167 | + |
|
| 6168 | + |
|
| 6169 | + |
|
| 6170 | + /** |
|
| 6171 | + * Clears all the models field caches. This is only useful when a sub-class |
|
| 6172 | + * might have added a field or something and these caches might be invalidated |
|
| 6173 | + */ |
|
| 6174 | + protected function _invalidate_field_caches() |
|
| 6175 | + { |
|
| 6176 | + $this->_cache_foreign_key_to_fields = array(); |
|
| 6177 | + $this->_cached_fields = null; |
|
| 6178 | + $this->_cached_fields_non_db_only = null; |
|
| 6179 | + } |
|
| 6180 | + |
|
| 6181 | + |
|
| 6182 | + |
|
| 6183 | + /** |
|
| 6184 | + * Gets the list of all the where query param keys that relate to logic instead of field names |
|
| 6185 | + * (eg "and", "or", "not"). |
|
| 6186 | + * |
|
| 6187 | + * @return array |
|
| 6188 | + */ |
|
| 6189 | + public function logic_query_param_keys() |
|
| 6190 | + { |
|
| 6191 | + return $this->_logic_query_param_keys; |
|
| 6192 | + } |
|
| 6193 | + |
|
| 6194 | + |
|
| 6195 | + |
|
| 6196 | + /** |
|
| 6197 | + * Determines whether or not the where query param array key is for a logic query param. |
|
| 6198 | + * Eg 'OR', 'not*', and 'and*because-i-say-so' should all return true, whereas |
|
| 6199 | + * 'ATT_fname', 'EVT_name*not-you-or-me', and 'ORG_name' should return false |
|
| 6200 | + * |
|
| 6201 | + * @param $query_param_key |
|
| 6202 | + * @return bool |
|
| 6203 | + */ |
|
| 6204 | + public function is_logic_query_param_key($query_param_key) |
|
| 6205 | + { |
|
| 6206 | + foreach ($this->logic_query_param_keys() as $logic_query_param_key) { |
|
| 6207 | + if ($query_param_key === $logic_query_param_key |
|
| 6208 | + || strpos($query_param_key, $logic_query_param_key . '*') === 0 |
|
| 6209 | + ) { |
|
| 6210 | + return true; |
|
| 6211 | + } |
|
| 6212 | + } |
|
| 6213 | + return false; |
|
| 6214 | + } |
|
| 6215 | 6215 | |
| 6216 | 6216 | |
| 6217 | 6217 | |
@@ -515,8 +515,8 @@ discard block |
||
| 515 | 515 | protected function __construct($timezone = null) |
| 516 | 516 | { |
| 517 | 517 | // check that the model has not been loaded too soon |
| 518 | - if (! did_action('AHEE__EE_System__load_espresso_addons')) { |
|
| 519 | - throw new EE_Error ( |
|
| 518 | + if ( ! did_action('AHEE__EE_System__load_espresso_addons')) { |
|
| 519 | + throw new EE_Error( |
|
| 520 | 520 | sprintf( |
| 521 | 521 | __('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.', |
| 522 | 522 | 'event_espresso'), |
@@ -536,7 +536,7 @@ discard block |
||
| 536 | 536 | * |
| 537 | 537 | * @var EE_Table_Base[] $_tables |
| 538 | 538 | */ |
| 539 | - $this->_tables = (array)apply_filters('FHEE__' . get_class($this) . '__construct__tables', $this->_tables); |
|
| 539 | + $this->_tables = (array) apply_filters('FHEE__'.get_class($this).'__construct__tables', $this->_tables); |
|
| 540 | 540 | foreach ($this->_tables as $table_alias => $table_obj) { |
| 541 | 541 | /** @var $table_obj EE_Table_Base */ |
| 542 | 542 | $table_obj->_construct_finalize_with_alias($table_alias); |
@@ -551,10 +551,10 @@ discard block |
||
| 551 | 551 | * |
| 552 | 552 | * @param EE_Model_Field_Base[] $_fields |
| 553 | 553 | */ |
| 554 | - $this->_fields = (array)apply_filters('FHEE__' . get_class($this) . '__construct__fields', $this->_fields); |
|
| 554 | + $this->_fields = (array) apply_filters('FHEE__'.get_class($this).'__construct__fields', $this->_fields); |
|
| 555 | 555 | $this->_invalidate_field_caches(); |
| 556 | 556 | foreach ($this->_fields as $table_alias => $fields_for_table) { |
| 557 | - if (! array_key_exists($table_alias, $this->_tables)) { |
|
| 557 | + if ( ! array_key_exists($table_alias, $this->_tables)) { |
|
| 558 | 558 | throw new EE_Error(sprintf(__("Table alias %s does not exist in EEM_Base child's _tables array. Only tables defined are %s", |
| 559 | 559 | 'event_espresso'), $table_alias, implode(",", $this->_fields))); |
| 560 | 560 | } |
@@ -582,7 +582,7 @@ discard block |
||
| 582 | 582 | * |
| 583 | 583 | * @param EE_Model_Relation_Base[] $_model_relations |
| 584 | 584 | */ |
| 585 | - $this->_model_relations = (array)apply_filters('FHEE__' . get_class($this) . '__construct__model_relations', |
|
| 585 | + $this->_model_relations = (array) apply_filters('FHEE__'.get_class($this).'__construct__model_relations', |
|
| 586 | 586 | $this->_model_relations); |
| 587 | 587 | foreach ($this->_model_relations as $model_name => $relation_obj) { |
| 588 | 588 | /** @var $relation_obj EE_Model_Relation_Base */ |
@@ -594,12 +594,12 @@ discard block |
||
| 594 | 594 | } |
| 595 | 595 | $this->set_timezone($timezone); |
| 596 | 596 | //finalize default where condition strategy, or set default |
| 597 | - if (! $this->_default_where_conditions_strategy) { |
|
| 597 | + if ( ! $this->_default_where_conditions_strategy) { |
|
| 598 | 598 | //nothing was set during child constructor, so set default |
| 599 | 599 | $this->_default_where_conditions_strategy = new EE_Default_Where_Conditions(); |
| 600 | 600 | } |
| 601 | 601 | $this->_default_where_conditions_strategy->_finalize_construct($this); |
| 602 | - if (! $this->_minimum_where_conditions_strategy) { |
|
| 602 | + if ( ! $this->_minimum_where_conditions_strategy) { |
|
| 603 | 603 | //nothing was set during child constructor, so set default |
| 604 | 604 | $this->_minimum_where_conditions_strategy = new EE_Default_Where_Conditions(); |
| 605 | 605 | } |
@@ -612,7 +612,7 @@ discard block |
||
| 612 | 612 | //initialize the standard cap restriction generators if none were specified by the child constructor |
| 613 | 613 | if ($this->_cap_restriction_generators !== false) { |
| 614 | 614 | foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { |
| 615 | - if (! isset($this->_cap_restriction_generators[$cap_context])) { |
|
| 615 | + if ( ! isset($this->_cap_restriction_generators[$cap_context])) { |
|
| 616 | 616 | $this->_cap_restriction_generators[$cap_context] = apply_filters( |
| 617 | 617 | 'FHEE__EEM_Base___construct__standard_cap_restriction_generator', |
| 618 | 618 | new EE_Restriction_Generator_Protected(), |
@@ -625,10 +625,10 @@ discard block |
||
| 625 | 625 | //if there are cap restriction generators, use them to make the default cap restrictions |
| 626 | 626 | if ($this->_cap_restriction_generators !== false) { |
| 627 | 627 | foreach ($this->_cap_restriction_generators as $context => $generator_object) { |
| 628 | - if (! $generator_object) { |
|
| 628 | + if ( ! $generator_object) { |
|
| 629 | 629 | continue; |
| 630 | 630 | } |
| 631 | - if (! $generator_object instanceof EE_Restriction_Generator_Base) { |
|
| 631 | + if ( ! $generator_object instanceof EE_Restriction_Generator_Base) { |
|
| 632 | 632 | throw new EE_Error( |
| 633 | 633 | sprintf( |
| 634 | 634 | __('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.', |
@@ -639,12 +639,12 @@ discard block |
||
| 639 | 639 | ); |
| 640 | 640 | } |
| 641 | 641 | $action = $this->cap_action_for_context($context); |
| 642 | - if (! $generator_object->construction_finalized()) { |
|
| 642 | + if ( ! $generator_object->construction_finalized()) { |
|
| 643 | 643 | $generator_object->_construct_finalize($this, $action); |
| 644 | 644 | } |
| 645 | 645 | } |
| 646 | 646 | } |
| 647 | - do_action('AHEE__' . get_class($this) . '__construct__end'); |
|
| 647 | + do_action('AHEE__'.get_class($this).'__construct__end'); |
|
| 648 | 648 | } |
| 649 | 649 | |
| 650 | 650 | |
@@ -657,7 +657,7 @@ discard block |
||
| 657 | 657 | */ |
| 658 | 658 | public static function set_model_query_blog_id($blog_id = 0) |
| 659 | 659 | { |
| 660 | - EEM_Base::$_model_query_blog_id = $blog_id > 0 ? (int)$blog_id : get_current_blog_id(); |
|
| 660 | + EEM_Base::$_model_query_blog_id = $blog_id > 0 ? (int) $blog_id : get_current_blog_id(); |
|
| 661 | 661 | } |
| 662 | 662 | |
| 663 | 663 | |
@@ -691,7 +691,7 @@ discard block |
||
| 691 | 691 | public static function instance($timezone = null) |
| 692 | 692 | { |
| 693 | 693 | // check if instance of Espresso_model already exists |
| 694 | - if (! static::$_instance instanceof static) { |
|
| 694 | + if ( ! static::$_instance instanceof static) { |
|
| 695 | 695 | // instantiate Espresso_model |
| 696 | 696 | static::$_instance = new static( |
| 697 | 697 | $timezone, |
@@ -730,7 +730,7 @@ discard block |
||
| 730 | 730 | foreach ($r->getDefaultProperties() as $property => $value) { |
| 731 | 731 | //don't set instance to null like it was originally, |
| 732 | 732 | //but it's static anyways, and we're ignoring static properties (for now at least) |
| 733 | - if (! isset($static_properties[$property])) { |
|
| 733 | + if ( ! isset($static_properties[$property])) { |
|
| 734 | 734 | static::$_instance->{$property} = $value; |
| 735 | 735 | } |
| 736 | 736 | } |
@@ -754,7 +754,7 @@ discard block |
||
| 754 | 754 | */ |
| 755 | 755 | private static function getLoader() |
| 756 | 756 | { |
| 757 | - if(! EEM_Base::$loader instanceof LoaderInterface) { |
|
| 757 | + if ( ! EEM_Base::$loader instanceof LoaderInterface) { |
|
| 758 | 758 | EEM_Base::$loader = LoaderFactory::getLoader(); |
| 759 | 759 | } |
| 760 | 760 | return EEM_Base::$loader; |
@@ -774,7 +774,7 @@ discard block |
||
| 774 | 774 | */ |
| 775 | 775 | public function status_array($translated = false) |
| 776 | 776 | { |
| 777 | - if (! array_key_exists('Status', $this->_model_relations)) { |
|
| 777 | + if ( ! array_key_exists('Status', $this->_model_relations)) { |
|
| 778 | 778 | return array(); |
| 779 | 779 | } |
| 780 | 780 | $model_name = $this->get_this_model_name(); |
@@ -977,17 +977,17 @@ discard block |
||
| 977 | 977 | public function wp_user_field_name() |
| 978 | 978 | { |
| 979 | 979 | try { |
| 980 | - if (! empty($this->_model_chain_to_wp_user)) { |
|
| 980 | + if ( ! empty($this->_model_chain_to_wp_user)) { |
|
| 981 | 981 | $models_to_follow_to_wp_users = explode('.', $this->_model_chain_to_wp_user); |
| 982 | 982 | $last_model_name = end($models_to_follow_to_wp_users); |
| 983 | 983 | $model_with_fk_to_wp_users = EE_Registry::instance()->load_model($last_model_name); |
| 984 | - $model_chain_to_wp_user = $this->_model_chain_to_wp_user . '.'; |
|
| 984 | + $model_chain_to_wp_user = $this->_model_chain_to_wp_user.'.'; |
|
| 985 | 985 | } else { |
| 986 | 986 | $model_with_fk_to_wp_users = $this; |
| 987 | 987 | $model_chain_to_wp_user = ''; |
| 988 | 988 | } |
| 989 | 989 | $wp_user_field = $model_with_fk_to_wp_users->get_foreign_key_to('WP_User'); |
| 990 | - return $model_chain_to_wp_user . $wp_user_field->get_name(); |
|
| 990 | + return $model_chain_to_wp_user.$wp_user_field->get_name(); |
|
| 991 | 991 | } catch (EE_Error $e) { |
| 992 | 992 | return false; |
| 993 | 993 | } |
@@ -1055,7 +1055,7 @@ discard block |
||
| 1055 | 1055 | */ |
| 1056 | 1056 | protected function _get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
| 1057 | 1057 | { |
| 1058 | - $this->_custom_selections = $this->getCustomSelection($query_params, $columns_to_select);; |
|
| 1058 | + $this->_custom_selections = $this->getCustomSelection($query_params, $columns_to_select); ; |
|
| 1059 | 1059 | $model_query_info = $this->_create_model_query_info_carrier($query_params); |
| 1060 | 1060 | $select_expressions = $columns_to_select === null |
| 1061 | 1061 | ? $this->_construct_default_select_sql($model_query_info) |
@@ -1063,11 +1063,11 @@ discard block |
||
| 1063 | 1063 | if ($this->_custom_selections instanceof CustomSelects) { |
| 1064 | 1064 | $custom_expressions = $this->_custom_selections->columnsToSelectExpression(); |
| 1065 | 1065 | $select_expressions .= $select_expressions |
| 1066 | - ? ', ' . $custom_expressions |
|
| 1066 | + ? ', '.$custom_expressions |
|
| 1067 | 1067 | : $custom_expressions; |
| 1068 | 1068 | } |
| 1069 | 1069 | |
| 1070 | - $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 1070 | + $SQL = "SELECT $select_expressions ".$this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 1071 | 1071 | return $this->_do_wpdb_query('get_results', array($SQL, $output)); |
| 1072 | 1072 | } |
| 1073 | 1073 | |
@@ -1084,7 +1084,7 @@ discard block |
||
| 1084 | 1084 | */ |
| 1085 | 1085 | protected function getCustomSelection(array $query_params, $columns_to_select = null) |
| 1086 | 1086 | { |
| 1087 | - if (! isset($query_params['extra_selects']) && $columns_to_select === null) { |
|
| 1087 | + if ( ! isset($query_params['extra_selects']) && $columns_to_select === null) { |
|
| 1088 | 1088 | return null; |
| 1089 | 1089 | } |
| 1090 | 1090 | $selects = isset($query_params['extra_selects']) ? $query_params['extra_selects'] : $columns_to_select; |
@@ -1133,7 +1133,7 @@ discard block |
||
| 1133 | 1133 | if (is_array($columns_to_select)) { |
| 1134 | 1134 | $select_sql_array = array(); |
| 1135 | 1135 | foreach ($columns_to_select as $alias => $selection_and_datatype) { |
| 1136 | - if (! is_array($selection_and_datatype) || ! isset($selection_and_datatype[1])) { |
|
| 1136 | + if ( ! is_array($selection_and_datatype) || ! isset($selection_and_datatype[1])) { |
|
| 1137 | 1137 | throw new EE_Error( |
| 1138 | 1138 | sprintf( |
| 1139 | 1139 | __( |
@@ -1145,7 +1145,7 @@ discard block |
||
| 1145 | 1145 | ) |
| 1146 | 1146 | ); |
| 1147 | 1147 | } |
| 1148 | - if (! in_array($selection_and_datatype[1], $this->_valid_wpdb_data_types, true)) { |
|
| 1148 | + if ( ! in_array($selection_and_datatype[1], $this->_valid_wpdb_data_types, true)) { |
|
| 1149 | 1149 | throw new EE_Error( |
| 1150 | 1150 | sprintf( |
| 1151 | 1151 | esc_html__( |
@@ -1217,7 +1217,7 @@ discard block |
||
| 1217 | 1217 | */ |
| 1218 | 1218 | public function alter_query_params_to_restrict_by_ID($id, $query_params = array()) |
| 1219 | 1219 | { |
| 1220 | - if (! isset($query_params[0])) { |
|
| 1220 | + if ( ! isset($query_params[0])) { |
|
| 1221 | 1221 | $query_params[0] = array(); |
| 1222 | 1222 | } |
| 1223 | 1223 | $conditions_from_id = $this->parse_index_primary_key_string($id); |
@@ -1242,7 +1242,7 @@ discard block |
||
| 1242 | 1242 | */ |
| 1243 | 1243 | public function get_one($query_params = array()) |
| 1244 | 1244 | { |
| 1245 | - if (! is_array($query_params)) { |
|
| 1245 | + if ( ! is_array($query_params)) { |
|
| 1246 | 1246 | EE_Error::doing_it_wrong('EEM_Base::get_one', |
| 1247 | 1247 | sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
| 1248 | 1248 | gettype($query_params)), '4.6.0'); |
@@ -1433,7 +1433,7 @@ discard block |
||
| 1433 | 1433 | return array(); |
| 1434 | 1434 | } |
| 1435 | 1435 | } |
| 1436 | - if (! is_array($query_params)) { |
|
| 1436 | + if ( ! is_array($query_params)) { |
|
| 1437 | 1437 | EE_Error::doing_it_wrong('EEM_Base::_get_consecutive', |
| 1438 | 1438 | sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
| 1439 | 1439 | gettype($query_params)), '4.6.0'); |
@@ -1443,7 +1443,7 @@ discard block |
||
| 1443 | 1443 | $query_params[0][$field_to_order_by] = array($operand, $current_field_value); |
| 1444 | 1444 | $query_params['limit'] = $limit; |
| 1445 | 1445 | //set direction |
| 1446 | - $incoming_orderby = isset($query_params['order_by']) ? (array)$query_params['order_by'] : array(); |
|
| 1446 | + $incoming_orderby = isset($query_params['order_by']) ? (array) $query_params['order_by'] : array(); |
|
| 1447 | 1447 | $query_params['order_by'] = $operand === '>' |
| 1448 | 1448 | ? array($field_to_order_by => 'ASC') + $incoming_orderby |
| 1449 | 1449 | : array($field_to_order_by => 'DESC') + $incoming_orderby; |
@@ -1521,7 +1521,7 @@ discard block |
||
| 1521 | 1521 | { |
| 1522 | 1522 | $field_settings = $this->field_settings_for($field_name); |
| 1523 | 1523 | //if not a valid EE_Datetime_Field then throw error |
| 1524 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1524 | + if ( ! $field_settings instanceof EE_Datetime_Field) { |
|
| 1525 | 1525 | throw new EE_Error(sprintf(__('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.', |
| 1526 | 1526 | 'event_espresso'), $field_name)); |
| 1527 | 1527 | } |
@@ -1600,7 +1600,7 @@ discard block |
||
| 1600 | 1600 | //load EEH_DTT_Helper |
| 1601 | 1601 | $set_timezone = empty($timezone) ? EEH_DTT_Helper::get_timezone() : $timezone; |
| 1602 | 1602 | $incomingDateTime = date_create_from_format($incoming_format, $timestring, new DateTimeZone($set_timezone)); |
| 1603 | - return \EventEspresso\core\domain\entities\DbSafeDateTime::createFromDateTime( $incomingDateTime->setTimezone(new DateTimeZone($this->_timezone)) ); |
|
| 1603 | + return \EventEspresso\core\domain\entities\DbSafeDateTime::createFromDateTime($incomingDateTime->setTimezone(new DateTimeZone($this->_timezone))); |
|
| 1604 | 1604 | } |
| 1605 | 1605 | |
| 1606 | 1606 | |
@@ -1668,7 +1668,7 @@ discard block |
||
| 1668 | 1668 | */ |
| 1669 | 1669 | public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true) |
| 1670 | 1670 | { |
| 1671 | - if (! is_array($query_params)) { |
|
| 1671 | + if ( ! is_array($query_params)) { |
|
| 1672 | 1672 | EE_Error::doing_it_wrong('EEM_Base::update', |
| 1673 | 1673 | sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
| 1674 | 1674 | gettype($query_params)), '4.6.0'); |
@@ -1690,7 +1690,7 @@ discard block |
||
| 1690 | 1690 | * @param EEM_Base $model the model being queried |
| 1691 | 1691 | * @param array $query_params see EEM_Base::get_all() |
| 1692 | 1692 | */ |
| 1693 | - $fields_n_values = (array)apply_filters('FHEE__EEM_Base__update__fields_n_values', $fields_n_values, $this, |
|
| 1693 | + $fields_n_values = (array) apply_filters('FHEE__EEM_Base__update__fields_n_values', $fields_n_values, $this, |
|
| 1694 | 1694 | $query_params); |
| 1695 | 1695 | //need to verify that, for any entry we want to update, there are entries in each secondary table. |
| 1696 | 1696 | //to do that, for each table, verify that it's PK isn't null. |
@@ -1704,7 +1704,7 @@ discard block |
||
| 1704 | 1704 | $wpdb_select_results = $this->_get_all_wpdb_results($query_params); |
| 1705 | 1705 | foreach ($wpdb_select_results as $wpdb_result) { |
| 1706 | 1706 | // type cast stdClass as array |
| 1707 | - $wpdb_result = (array)$wpdb_result; |
|
| 1707 | + $wpdb_result = (array) $wpdb_result; |
|
| 1708 | 1708 | //get the model object's PK, as we'll want this if we need to insert a row into secondary tables |
| 1709 | 1709 | if ($this->has_primary_key_field()) { |
| 1710 | 1710 | $main_table_pk_value = $wpdb_result[$this->get_primary_key_field()->get_qualified_column()]; |
@@ -1721,13 +1721,13 @@ discard block |
||
| 1721 | 1721 | $this_table_pk_column = $table_obj->get_fully_qualified_pk_column(); |
| 1722 | 1722 | //if there is no private key for this table on the results, it means there's no entry |
| 1723 | 1723 | //in this table, right? so insert a row in the current table, using any fields available |
| 1724 | - if (! (array_key_exists($this_table_pk_column, $wpdb_result) |
|
| 1724 | + if ( ! (array_key_exists($this_table_pk_column, $wpdb_result) |
|
| 1725 | 1725 | && $wpdb_result[$this_table_pk_column]) |
| 1726 | 1726 | ) { |
| 1727 | 1727 | $success = $this->_insert_into_specific_table($table_obj, $fields_n_values, |
| 1728 | 1728 | $main_table_pk_value); |
| 1729 | 1729 | //if we died here, report the error |
| 1730 | - if (! $success) { |
|
| 1730 | + if ( ! $success) { |
|
| 1731 | 1731 | return false; |
| 1732 | 1732 | } |
| 1733 | 1733 | } |
@@ -1758,7 +1758,7 @@ discard block |
||
| 1758 | 1758 | $model_objs_affected_ids[$combined_index_key] = $combined_index_key; |
| 1759 | 1759 | } |
| 1760 | 1760 | } |
| 1761 | - if (! $model_objs_affected_ids) { |
|
| 1761 | + if ( ! $model_objs_affected_ids) { |
|
| 1762 | 1762 | //wait wait wait- if nothing was affected let's stop here |
| 1763 | 1763 | return 0; |
| 1764 | 1764 | } |
@@ -1785,7 +1785,7 @@ discard block |
||
| 1785 | 1785 | . $model_query_info->get_full_join_sql() |
| 1786 | 1786 | . " SET " |
| 1787 | 1787 | . $this->_construct_update_sql($fields_n_values) |
| 1788 | - . $model_query_info->get_where_sql();//note: doesn't use _construct_2nd_half_of_select_query() because doesn't accept LIMIT, ORDER BY, etc. |
|
| 1788 | + . $model_query_info->get_where_sql(); //note: doesn't use _construct_2nd_half_of_select_query() because doesn't accept LIMIT, ORDER BY, etc. |
|
| 1789 | 1789 | $rows_affected = $this->_do_wpdb_query('query', array($SQL)); |
| 1790 | 1790 | /** |
| 1791 | 1791 | * Action called after a model update call has been made. |
@@ -1796,7 +1796,7 @@ discard block |
||
| 1796 | 1796 | * @param int $rows_affected |
| 1797 | 1797 | */ |
| 1798 | 1798 | do_action('AHEE__EEM_Base__update__end', $this, $fields_n_values, $query_params, $rows_affected); |
| 1799 | - return $rows_affected;//how many supposedly got updated |
|
| 1799 | + return $rows_affected; //how many supposedly got updated |
|
| 1800 | 1800 | } |
| 1801 | 1801 | |
| 1802 | 1802 | |
@@ -1824,7 +1824,7 @@ discard block |
||
| 1824 | 1824 | } |
| 1825 | 1825 | $model_query_info = $this->_create_model_query_info_carrier($query_params); |
| 1826 | 1826 | $select_expressions = $field->get_qualified_column(); |
| 1827 | - $SQL = "SELECT $select_expressions " . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 1827 | + $SQL = "SELECT $select_expressions ".$this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 1828 | 1828 | return $this->_do_wpdb_query('get_col', array($SQL)); |
| 1829 | 1829 | } |
| 1830 | 1830 | |
@@ -1842,7 +1842,7 @@ discard block |
||
| 1842 | 1842 | { |
| 1843 | 1843 | $query_params['limit'] = 1; |
| 1844 | 1844 | $col = $this->get_col($query_params, $field_to_select); |
| 1845 | - if (! empty($col)) { |
|
| 1845 | + if ( ! empty($col)) { |
|
| 1846 | 1846 | return reset($col); |
| 1847 | 1847 | } |
| 1848 | 1848 | return null; |
@@ -1873,7 +1873,7 @@ discard block |
||
| 1873 | 1873 | $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); |
| 1874 | 1874 | $value_sql = $prepared_value === null ? 'NULL' |
| 1875 | 1875 | : $wpdb->prepare($field_obj->get_wpdb_data_type(), $prepared_value); |
| 1876 | - $cols_n_values[] = $field_obj->get_qualified_column() . "=" . $value_sql; |
|
| 1876 | + $cols_n_values[] = $field_obj->get_qualified_column()."=".$value_sql; |
|
| 1877 | 1877 | } |
| 1878 | 1878 | return implode(",", $cols_n_values); |
| 1879 | 1879 | } |
@@ -2055,7 +2055,7 @@ discard block |
||
| 2055 | 2055 | * @param int $rows_deleted |
| 2056 | 2056 | */ |
| 2057 | 2057 | do_action('AHEE__EEM_Base__delete__end', $this, $query_params, $rows_deleted, $columns_and_ids_for_deleting); |
| 2058 | - return $rows_deleted;//how many supposedly got deleted |
|
| 2058 | + return $rows_deleted; //how many supposedly got deleted |
|
| 2059 | 2059 | } |
| 2060 | 2060 | |
| 2061 | 2061 | |
@@ -2205,7 +2205,7 @@ discard block |
||
| 2205 | 2205 | foreach ($ids_to_delete_indexed_by_column as $column => $ids) { |
| 2206 | 2206 | //make sure we have unique $ids |
| 2207 | 2207 | $ids = array_unique($ids); |
| 2208 | - $query[] = $column . ' IN(' . implode(',', $ids) . ')'; |
|
| 2208 | + $query[] = $column.' IN('.implode(',', $ids).')'; |
|
| 2209 | 2209 | } |
| 2210 | 2210 | $query_part = ! empty($query) ? implode(' AND ', $query) : $query_part; |
| 2211 | 2211 | } elseif (count($this->get_combined_primary_key_fields()) > 1) { |
@@ -2213,7 +2213,7 @@ discard block |
||
| 2213 | 2213 | foreach ($ids_to_delete_indexed_by_column as $ids_to_delete_indexed_by_column_for_each_row) { |
| 2214 | 2214 | $values_for_each_combined_primary_key_for_a_row = array(); |
| 2215 | 2215 | foreach ($ids_to_delete_indexed_by_column_for_each_row as $column => $id) { |
| 2216 | - $values_for_each_combined_primary_key_for_a_row[] = $column . '=' . $id; |
|
| 2216 | + $values_for_each_combined_primary_key_for_a_row[] = $column.'='.$id; |
|
| 2217 | 2217 | } |
| 2218 | 2218 | $ways_to_identify_a_row[] = '(' |
| 2219 | 2219 | . implode(' AND ', $values_for_each_combined_primary_key_for_a_row) |
@@ -2233,8 +2233,8 @@ discard block |
||
| 2233 | 2233 | */ |
| 2234 | 2234 | public function get_field_by_column($qualified_column_name) |
| 2235 | 2235 | { |
| 2236 | - foreach($this->field_settings(true) as $field_name => $field_obj){ |
|
| 2237 | - if($field_obj->get_qualified_column() === $qualified_column_name){ |
|
| 2236 | + foreach ($this->field_settings(true) as $field_name => $field_obj) { |
|
| 2237 | + if ($field_obj->get_qualified_column() === $qualified_column_name) { |
|
| 2238 | 2238 | return $field_obj; |
| 2239 | 2239 | } |
| 2240 | 2240 | } |
@@ -2285,9 +2285,9 @@ discard block |
||
| 2285 | 2285 | $column_to_count = '*'; |
| 2286 | 2286 | } |
| 2287 | 2287 | } |
| 2288 | - $column_to_count = $distinct ? "DISTINCT " . $column_to_count : $column_to_count; |
|
| 2289 | - $SQL = "SELECT COUNT(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2290 | - return (int)$this->_do_wpdb_query('get_var', array($SQL)); |
|
| 2288 | + $column_to_count = $distinct ? "DISTINCT ".$column_to_count : $column_to_count; |
|
| 2289 | + $SQL = "SELECT COUNT(".$column_to_count.")".$this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2290 | + return (int) $this->_do_wpdb_query('get_var', array($SQL)); |
|
| 2291 | 2291 | } |
| 2292 | 2292 | |
| 2293 | 2293 | |
@@ -2309,14 +2309,14 @@ discard block |
||
| 2309 | 2309 | $field_obj = $this->get_primary_key_field(); |
| 2310 | 2310 | } |
| 2311 | 2311 | $column_to_count = $field_obj->get_qualified_column(); |
| 2312 | - $SQL = "SELECT SUM(" . $column_to_count . ")" . $this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2312 | + $SQL = "SELECT SUM(".$column_to_count.")".$this->_construct_2nd_half_of_select_query($model_query_info); |
|
| 2313 | 2313 | $return_value = $this->_do_wpdb_query('get_var', array($SQL)); |
| 2314 | 2314 | $data_type = $field_obj->get_wpdb_data_type(); |
| 2315 | 2315 | if ($data_type === '%d' || $data_type === '%s') { |
| 2316 | - return (float)$return_value; |
|
| 2316 | + return (float) $return_value; |
|
| 2317 | 2317 | } |
| 2318 | 2318 | //must be %f |
| 2319 | - return (float)$return_value; |
|
| 2319 | + return (float) $return_value; |
|
| 2320 | 2320 | } |
| 2321 | 2321 | |
| 2322 | 2322 | |
@@ -2336,13 +2336,13 @@ discard block |
||
| 2336 | 2336 | //if we're in maintenance mode level 2, DON'T run any queries |
| 2337 | 2337 | //because level 2 indicates the database needs updating and |
| 2338 | 2338 | //is probably out of sync with the code |
| 2339 | - if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 2339 | + if ( ! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 2340 | 2340 | throw new EE_Error(sprintf(__("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.", |
| 2341 | 2341 | "event_espresso"))); |
| 2342 | 2342 | } |
| 2343 | 2343 | /** @type WPDB $wpdb */ |
| 2344 | 2344 | global $wpdb; |
| 2345 | - if (! method_exists($wpdb, $wpdb_method)) { |
|
| 2345 | + if ( ! method_exists($wpdb, $wpdb_method)) { |
|
| 2346 | 2346 | throw new EE_Error(sprintf(__('There is no method named "%s" on Wordpress\' $wpdb object', |
| 2347 | 2347 | 'event_espresso'), $wpdb_method)); |
| 2348 | 2348 | } |
@@ -2354,7 +2354,7 @@ discard block |
||
| 2354 | 2354 | $this->show_db_query_if_previously_requested($wpdb->last_query); |
| 2355 | 2355 | if (WP_DEBUG) { |
| 2356 | 2356 | $wpdb->show_errors($old_show_errors_value); |
| 2357 | - if (! empty($wpdb->last_error)) { |
|
| 2357 | + if ( ! empty($wpdb->last_error)) { |
|
| 2358 | 2358 | throw new EE_Error(sprintf(__('WPDB Error: "%s"', 'event_espresso'), $wpdb->last_error)); |
| 2359 | 2359 | } |
| 2360 | 2360 | if ($result === false) { |
@@ -2415,7 +2415,7 @@ discard block |
||
| 2415 | 2415 | return $result; |
| 2416 | 2416 | break; |
| 2417 | 2417 | } |
| 2418 | - if (! empty($error_message)) { |
|
| 2418 | + if ( ! empty($error_message)) { |
|
| 2419 | 2419 | EE_Log::instance()->log(__FILE__, __FUNCTION__, $error_message, 'error'); |
| 2420 | 2420 | trigger_error($error_message); |
| 2421 | 2421 | } |
@@ -2491,11 +2491,11 @@ discard block |
||
| 2491 | 2491 | */ |
| 2492 | 2492 | private function _construct_2nd_half_of_select_query(EE_Model_Query_Info_Carrier $model_query_info) |
| 2493 | 2493 | { |
| 2494 | - return " FROM " . $model_query_info->get_full_join_sql() . |
|
| 2495 | - $model_query_info->get_where_sql() . |
|
| 2496 | - $model_query_info->get_group_by_sql() . |
|
| 2497 | - $model_query_info->get_having_sql() . |
|
| 2498 | - $model_query_info->get_order_by_sql() . |
|
| 2494 | + return " FROM ".$model_query_info->get_full_join_sql(). |
|
| 2495 | + $model_query_info->get_where_sql(). |
|
| 2496 | + $model_query_info->get_group_by_sql(). |
|
| 2497 | + $model_query_info->get_having_sql(). |
|
| 2498 | + $model_query_info->get_order_by_sql(). |
|
| 2499 | 2499 | $model_query_info->get_limit_sql(); |
| 2500 | 2500 | } |
| 2501 | 2501 | |
@@ -2691,12 +2691,12 @@ discard block |
||
| 2691 | 2691 | $related_model = $this->get_related_model_obj($model_name); |
| 2692 | 2692 | //we're just going to use the query params on the related model's normal get_all query, |
| 2693 | 2693 | //except add a condition to say to match the current mod |
| 2694 | - if (! isset($query_params['default_where_conditions'])) { |
|
| 2694 | + if ( ! isset($query_params['default_where_conditions'])) { |
|
| 2695 | 2695 | $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
| 2696 | 2696 | } |
| 2697 | 2697 | $this_model_name = $this->get_this_model_name(); |
| 2698 | 2698 | $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
| 2699 | - $query_params[0][$this_model_name . "." . $this_pk_field_name] = $id_or_obj; |
|
| 2699 | + $query_params[0][$this_model_name.".".$this_pk_field_name] = $id_or_obj; |
|
| 2700 | 2700 | return $related_model->count($query_params, $field_to_count, $distinct); |
| 2701 | 2701 | } |
| 2702 | 2702 | |
@@ -2716,7 +2716,7 @@ discard block |
||
| 2716 | 2716 | public function sum_related($id_or_obj, $model_name, $query_params, $field_to_sum = null) |
| 2717 | 2717 | { |
| 2718 | 2718 | $related_model = $this->get_related_model_obj($model_name); |
| 2719 | - if (! is_array($query_params)) { |
|
| 2719 | + if ( ! is_array($query_params)) { |
|
| 2720 | 2720 | EE_Error::doing_it_wrong('EEM_Base::sum_related', |
| 2721 | 2721 | sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
| 2722 | 2722 | gettype($query_params)), '4.6.0'); |
@@ -2724,12 +2724,12 @@ discard block |
||
| 2724 | 2724 | } |
| 2725 | 2725 | //we're just going to use the query params on the related model's normal get_all query, |
| 2726 | 2726 | //except add a condition to say to match the current mod |
| 2727 | - if (! isset($query_params['default_where_conditions'])) { |
|
| 2727 | + if ( ! isset($query_params['default_where_conditions'])) { |
|
| 2728 | 2728 | $query_params['default_where_conditions'] = EEM_Base::default_where_conditions_none; |
| 2729 | 2729 | } |
| 2730 | 2730 | $this_model_name = $this->get_this_model_name(); |
| 2731 | 2731 | $this_pk_field_name = $this->get_primary_key_field()->get_name(); |
| 2732 | - $query_params[0][$this_model_name . "." . $this_pk_field_name] = $id_or_obj; |
|
| 2732 | + $query_params[0][$this_model_name.".".$this_pk_field_name] = $id_or_obj; |
|
| 2733 | 2733 | return $related_model->sum($query_params, $field_to_sum); |
| 2734 | 2734 | } |
| 2735 | 2735 | |
@@ -2782,7 +2782,7 @@ discard block |
||
| 2782 | 2782 | $field_with_model_name = $field; |
| 2783 | 2783 | } |
| 2784 | 2784 | } |
| 2785 | - if (! isset($field_with_model_name) || ! $field_with_model_name) { |
|
| 2785 | + if ( ! isset($field_with_model_name) || ! $field_with_model_name) { |
|
| 2786 | 2786 | throw new EE_Error(sprintf(__("There is no EE_Any_Foreign_Model_Name field on model %s", "event_espresso"), |
| 2787 | 2787 | $this->get_this_model_name())); |
| 2788 | 2788 | } |
@@ -2815,7 +2815,7 @@ discard block |
||
| 2815 | 2815 | * @param array $fields_n_values keys are the fields and values are their new values |
| 2816 | 2816 | * @param EEM_Base $model the model used |
| 2817 | 2817 | */ |
| 2818 | - $field_n_values = (array)apply_filters('FHEE__EEM_Base__insert__fields_n_values', $field_n_values, $this); |
|
| 2818 | + $field_n_values = (array) apply_filters('FHEE__EEM_Base__insert__fields_n_values', $field_n_values, $this); |
|
| 2819 | 2819 | if ($this->_satisfies_unique_indexes($field_n_values)) { |
| 2820 | 2820 | $main_table = $this->_get_main_table(); |
| 2821 | 2821 | $new_id = $this->_insert_into_specific_table($main_table, $field_n_values, false); |
@@ -2922,7 +2922,7 @@ discard block |
||
| 2922 | 2922 | } |
| 2923 | 2923 | foreach ($this->unique_indexes() as $unique_index_name => $unique_index) { |
| 2924 | 2924 | $uniqueness_where_params = array_intersect_key($fields_n_values, $unique_index->fields()); |
| 2925 | - $query_params[0]['OR']['AND*' . $unique_index_name] = $uniqueness_where_params; |
|
| 2925 | + $query_params[0]['OR']['AND*'.$unique_index_name] = $uniqueness_where_params; |
|
| 2926 | 2926 | } |
| 2927 | 2927 | //if there is nothing to base this search on, then we shouldn't find anything |
| 2928 | 2928 | if (empty($query_params)) { |
@@ -3008,7 +3008,7 @@ discard block |
||
| 3008 | 3008 | //its not the main table, so we should have already saved the main table's PK which we just inserted |
| 3009 | 3009 | //so add the fk to the main table as a column |
| 3010 | 3010 | $insertion_col_n_values[$table->get_fk_on_table()] = $new_id; |
| 3011 | - $format_for_insertion[] = '%d';//yes right now we're only allowing these foreign keys to be INTs |
|
| 3011 | + $format_for_insertion[] = '%d'; //yes right now we're only allowing these foreign keys to be INTs |
|
| 3012 | 3012 | } |
| 3013 | 3013 | //insert the new entry |
| 3014 | 3014 | $result = $this->_do_wpdb_query('insert', |
@@ -3212,7 +3212,7 @@ discard block |
||
| 3212 | 3212 | $query_info_carrier, |
| 3213 | 3213 | 'group_by' |
| 3214 | 3214 | ); |
| 3215 | - } elseif (! empty ($query_params['group_by'])) { |
|
| 3215 | + } elseif ( ! empty ($query_params['group_by'])) { |
|
| 3216 | 3216 | $this->_extract_related_model_info_from_query_param( |
| 3217 | 3217 | $query_params['group_by'], |
| 3218 | 3218 | $query_info_carrier, |
@@ -3234,7 +3234,7 @@ discard block |
||
| 3234 | 3234 | $query_info_carrier, |
| 3235 | 3235 | 'order_by' |
| 3236 | 3236 | ); |
| 3237 | - } elseif (! empty($query_params['order_by'])) { |
|
| 3237 | + } elseif ( ! empty($query_params['order_by'])) { |
|
| 3238 | 3238 | $this->_extract_related_model_info_from_query_param( |
| 3239 | 3239 | $query_params['order_by'], |
| 3240 | 3240 | $query_info_carrier, |
@@ -3270,8 +3270,8 @@ discard block |
||
| 3270 | 3270 | EE_Model_Query_Info_Carrier $model_query_info_carrier, |
| 3271 | 3271 | $query_param_type |
| 3272 | 3272 | ) { |
| 3273 | - if (! empty($sub_query_params)) { |
|
| 3274 | - $sub_query_params = (array)$sub_query_params; |
|
| 3273 | + if ( ! empty($sub_query_params)) { |
|
| 3274 | + $sub_query_params = (array) $sub_query_params; |
|
| 3275 | 3275 | foreach ($sub_query_params as $param => $possibly_array_of_params) { |
| 3276 | 3276 | //$param could be simply 'EVT_ID', or it could be 'Registrations.REG_ID', or even 'Registrations.Transactions.Payments.PAY_amount' |
| 3277 | 3277 | $this->_extract_related_model_info_from_query_param($param, $model_query_info_carrier, |
@@ -3282,7 +3282,7 @@ discard block |
||
| 3282 | 3282 | //of array('Registration.TXN_ID'=>23) |
| 3283 | 3283 | $query_param_sans_stars = $this->_remove_stars_and_anything_after_from_condition_query_param_key($param); |
| 3284 | 3284 | if (in_array($query_param_sans_stars, $this->_logic_query_param_keys, true)) { |
| 3285 | - if (! is_array($possibly_array_of_params)) { |
|
| 3285 | + if ( ! is_array($possibly_array_of_params)) { |
|
| 3286 | 3286 | throw new EE_Error(sprintf(__("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'))", |
| 3287 | 3287 | "event_espresso"), |
| 3288 | 3288 | $param, $possibly_array_of_params)); |
@@ -3299,7 +3299,7 @@ discard block |
||
| 3299 | 3299 | //then $possible_array_of_params looks something like array('<','DTT_sold',true) |
| 3300 | 3300 | //indicating that $possible_array_of_params[1] is actually a field name, |
| 3301 | 3301 | //from which we should extract query parameters! |
| 3302 | - if (! isset($possibly_array_of_params[0], $possibly_array_of_params[1])) { |
|
| 3302 | + if ( ! isset($possibly_array_of_params[0], $possibly_array_of_params[1])) { |
|
| 3303 | 3303 | throw new EE_Error(sprintf(__("Improperly formed query parameter %s. It should be numerically indexed like array('<','DTT_sold',true); but you provided %s", |
| 3304 | 3304 | "event_espresso"), $query_param_type, implode(",", $possibly_array_of_params))); |
| 3305 | 3305 | } |
@@ -3329,8 +3329,8 @@ discard block |
||
| 3329 | 3329 | EE_Model_Query_Info_Carrier $model_query_info_carrier, |
| 3330 | 3330 | $query_param_type |
| 3331 | 3331 | ) { |
| 3332 | - if (! empty($sub_query_params)) { |
|
| 3333 | - if (! is_array($sub_query_params)) { |
|
| 3332 | + if ( ! empty($sub_query_params)) { |
|
| 3333 | + if ( ! is_array($sub_query_params)) { |
|
| 3334 | 3334 | throw new EE_Error(sprintf(__("Query parameter %s should be an array, but it isn't.", "event_espresso"), |
| 3335 | 3335 | $sub_query_params)); |
| 3336 | 3336 | } |
@@ -3359,7 +3359,7 @@ discard block |
||
| 3359 | 3359 | */ |
| 3360 | 3360 | public function _create_model_query_info_carrier($query_params) |
| 3361 | 3361 | { |
| 3362 | - if (! is_array($query_params)) { |
|
| 3362 | + if ( ! is_array($query_params)) { |
|
| 3363 | 3363 | EE_Error::doing_it_wrong( |
| 3364 | 3364 | 'EEM_Base::_create_model_query_info_carrier', |
| 3365 | 3365 | sprintf( |
@@ -3435,7 +3435,7 @@ discard block |
||
| 3435 | 3435 | //set limit |
| 3436 | 3436 | if (array_key_exists('limit', $query_params)) { |
| 3437 | 3437 | if (is_array($query_params['limit'])) { |
| 3438 | - if (! isset($query_params['limit'][0], $query_params['limit'][1])) { |
|
| 3438 | + if ( ! isset($query_params['limit'][0], $query_params['limit'][1])) { |
|
| 3439 | 3439 | $e = sprintf( |
| 3440 | 3440 | __( |
| 3441 | 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)", |
@@ -3443,12 +3443,12 @@ discard block |
||
| 3443 | 3443 | ), |
| 3444 | 3444 | http_build_query($query_params['limit']) |
| 3445 | 3445 | ); |
| 3446 | - throw new EE_Error($e . "|" . $e); |
|
| 3446 | + throw new EE_Error($e."|".$e); |
|
| 3447 | 3447 | } |
| 3448 | 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']); |
|
| 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 | 3452 | } |
| 3453 | 3453 | } |
| 3454 | 3454 | //set order by |
@@ -3480,10 +3480,10 @@ discard block |
||
| 3480 | 3480 | $order_array = array(); |
| 3481 | 3481 | foreach ($query_params['order_by'] as $field_name_to_order_by => $order) { |
| 3482 | 3482 | $order = $this->_extract_order($order); |
| 3483 | - $order_array[] = $this->_deduce_column_name_from_query_param($field_name_to_order_by) . SP . $order; |
|
| 3483 | + $order_array[] = $this->_deduce_column_name_from_query_param($field_name_to_order_by).SP.$order; |
|
| 3484 | 3484 | } |
| 3485 | - $query_object->set_order_by_sql(" ORDER BY " . implode(",", $order_array)); |
|
| 3486 | - } elseif (! empty ($query_params['order_by'])) { |
|
| 3485 | + $query_object->set_order_by_sql(" ORDER BY ".implode(",", $order_array)); |
|
| 3486 | + } elseif ( ! empty ($query_params['order_by'])) { |
|
| 3487 | 3487 | $this->_extract_related_model_info_from_query_param( |
| 3488 | 3488 | $query_params['order_by'], |
| 3489 | 3489 | $query_object, |
@@ -3494,18 +3494,18 @@ discard block |
||
| 3494 | 3494 | ? $this->_extract_order($query_params['order']) |
| 3495 | 3495 | : 'DESC'; |
| 3496 | 3496 | $query_object->set_order_by_sql( |
| 3497 | - " ORDER BY " . $this->_deduce_column_name_from_query_param($query_params['order_by']) . SP . $order |
|
| 3497 | + " ORDER BY ".$this->_deduce_column_name_from_query_param($query_params['order_by']).SP.$order |
|
| 3498 | 3498 | ); |
| 3499 | 3499 | } |
| 3500 | 3500 | } |
| 3501 | 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) |
|
| 3502 | + if ( ! array_key_exists('order_by', $query_params) |
|
| 3503 | 3503 | && array_key_exists('order', $query_params) |
| 3504 | 3504 | && ! empty($query_params['order']) |
| 3505 | 3505 | ) { |
| 3506 | 3506 | $pk_field = $this->get_primary_key_field(); |
| 3507 | 3507 | $order = $this->_extract_order($query_params['order']); |
| 3508 | - $query_object->set_order_by_sql(" ORDER BY " . $pk_field->get_qualified_column() . SP . $order); |
|
| 3508 | + $query_object->set_order_by_sql(" ORDER BY ".$pk_field->get_qualified_column().SP.$order); |
|
| 3509 | 3509 | } |
| 3510 | 3510 | //set group by |
| 3511 | 3511 | if (array_key_exists('group_by', $query_params)) { |
@@ -3515,10 +3515,10 @@ discard block |
||
| 3515 | 3515 | foreach ($query_params['group_by'] as $field_name_to_group_by) { |
| 3516 | 3516 | $group_by_array[] = $this->_deduce_column_name_from_query_param($field_name_to_group_by); |
| 3517 | 3517 | } |
| 3518 | - $query_object->set_group_by_sql(" GROUP BY " . implode(", ", $group_by_array)); |
|
| 3519 | - } elseif (! empty ($query_params['group_by'])) { |
|
| 3518 | + $query_object->set_group_by_sql(" GROUP BY ".implode(", ", $group_by_array)); |
|
| 3519 | + } elseif ( ! empty ($query_params['group_by'])) { |
|
| 3520 | 3520 | $query_object->set_group_by_sql( |
| 3521 | - " GROUP BY " . $this->_deduce_column_name_from_query_param($query_params['group_by']) |
|
| 3521 | + " GROUP BY ".$this->_deduce_column_name_from_query_param($query_params['group_by']) |
|
| 3522 | 3522 | ); |
| 3523 | 3523 | } |
| 3524 | 3524 | } |
@@ -3528,7 +3528,7 @@ discard block |
||
| 3528 | 3528 | } |
| 3529 | 3529 | //now, just verify they didn't pass anything wack |
| 3530 | 3530 | foreach ($query_params as $query_key => $query_value) { |
| 3531 | - if (! in_array($query_key, $this->_allowed_query_params, true)) { |
|
| 3531 | + if ( ! in_array($query_key, $this->_allowed_query_params, true)) { |
|
| 3532 | 3532 | throw new EE_Error( |
| 3533 | 3533 | sprintf( |
| 3534 | 3534 | __( |
@@ -3627,22 +3627,22 @@ discard block |
||
| 3627 | 3627 | $where_query_params = array() |
| 3628 | 3628 | ) { |
| 3629 | 3629 | $allowed_used_default_where_conditions_values = EEM_Base::valid_default_where_conditions(); |
| 3630 | - if (! in_array($use_default_where_conditions, $allowed_used_default_where_conditions_values)) { |
|
| 3630 | + if ( ! in_array($use_default_where_conditions, $allowed_used_default_where_conditions_values)) { |
|
| 3631 | 3631 | throw new EE_Error(sprintf(__("You passed an invalid value to the query parameter 'default_where_conditions' of '%s'. Allowed values are %s", |
| 3632 | 3632 | "event_espresso"), $use_default_where_conditions, |
| 3633 | 3633 | implode(", ", $allowed_used_default_where_conditions_values))); |
| 3634 | 3634 | } |
| 3635 | 3635 | $universal_query_params = array(); |
| 3636 | - if ($this->_should_use_default_where_conditions( $use_default_where_conditions, true)) { |
|
| 3636 | + if ($this->_should_use_default_where_conditions($use_default_where_conditions, true)) { |
|
| 3637 | 3637 | $universal_query_params = $this->_get_default_where_conditions(); |
| 3638 | - } else if ($this->_should_use_minimum_where_conditions( $use_default_where_conditions, true)) { |
|
| 3638 | + } else if ($this->_should_use_minimum_where_conditions($use_default_where_conditions, true)) { |
|
| 3639 | 3639 | $universal_query_params = $this->_get_minimum_where_conditions(); |
| 3640 | 3640 | } |
| 3641 | 3641 | foreach ($query_info_carrier->get_model_names_included() as $model_relation_path => $model_name) { |
| 3642 | 3642 | $related_model = $this->get_related_model_obj($model_name); |
| 3643 | - if ( $this->_should_use_default_where_conditions( $use_default_where_conditions, false)) { |
|
| 3643 | + if ($this->_should_use_default_where_conditions($use_default_where_conditions, false)) { |
|
| 3644 | 3644 | $related_model_universal_where_params = $related_model->_get_default_where_conditions($model_relation_path); |
| 3645 | - } elseif ($this->_should_use_minimum_where_conditions( $use_default_where_conditions, false)) { |
|
| 3645 | + } elseif ($this->_should_use_minimum_where_conditions($use_default_where_conditions, false)) { |
|
| 3646 | 3646 | $related_model_universal_where_params = $related_model->_get_minimum_where_conditions($model_relation_path); |
| 3647 | 3647 | } else { |
| 3648 | 3648 | //we don't want to add full or even minimum default where conditions from this model, so just continue |
@@ -3675,7 +3675,7 @@ discard block |
||
| 3675 | 3675 | * @param bool $for_this_model false means this is for OTHER related models |
| 3676 | 3676 | * @return bool |
| 3677 | 3677 | */ |
| 3678 | - private function _should_use_default_where_conditions( $default_where_conditions_value, $for_this_model = true ) |
|
| 3678 | + private function _should_use_default_where_conditions($default_where_conditions_value, $for_this_model = true) |
|
| 3679 | 3679 | { |
| 3680 | 3680 | return ( |
| 3681 | 3681 | $for_this_model |
@@ -3754,7 +3754,7 @@ discard block |
||
| 3754 | 3754 | ) { |
| 3755 | 3755 | $null_friendly_where_conditions = array(); |
| 3756 | 3756 | $none_overridden = true; |
| 3757 | - $or_condition_key_for_defaults = 'OR*' . get_class($model); |
|
| 3757 | + $or_condition_key_for_defaults = 'OR*'.get_class($model); |
|
| 3758 | 3758 | foreach ($default_where_conditions as $key => $val) { |
| 3759 | 3759 | if (isset($provided_where_conditions[$key])) { |
| 3760 | 3760 | $none_overridden = false; |
@@ -3872,7 +3872,7 @@ discard block |
||
| 3872 | 3872 | foreach ($tables as $table_obj) { |
| 3873 | 3873 | $qualified_pk_column = $table_alias_with_model_relation_chain_prefix |
| 3874 | 3874 | . $table_obj->get_fully_qualified_pk_column(); |
| 3875 | - if (! in_array($qualified_pk_column, $selects)) { |
|
| 3875 | + if ( ! in_array($qualified_pk_column, $selects)) { |
|
| 3876 | 3876 | $selects[] = "$qualified_pk_column AS '$qualified_pk_column'"; |
| 3877 | 3877 | } |
| 3878 | 3878 | } |
@@ -4018,9 +4018,9 @@ discard block |
||
| 4018 | 4018 | $query_parameter_type |
| 4019 | 4019 | ) { |
| 4020 | 4020 | foreach ($this->_model_relations as $valid_related_model_name => $relation_obj) { |
| 4021 | - if (strpos($possible_join_string, $valid_related_model_name . ".") === 0) { |
|
| 4021 | + if (strpos($possible_join_string, $valid_related_model_name.".") === 0) { |
|
| 4022 | 4022 | $this->_add_join_to_model($valid_related_model_name, $query_info_carrier, $original_query_param); |
| 4023 | - $possible_join_string = substr($possible_join_string, strlen($valid_related_model_name . ".")); |
|
| 4023 | + $possible_join_string = substr($possible_join_string, strlen($valid_related_model_name.".")); |
|
| 4024 | 4024 | if ($possible_join_string === '') { |
| 4025 | 4025 | //nothing left to $query_param |
| 4026 | 4026 | //we should actually end in a field name, not a model like this! |
@@ -4147,7 +4147,7 @@ discard block |
||
| 4147 | 4147 | { |
| 4148 | 4148 | $SQL = $this->_construct_condition_clause_recursive($where_params, ' AND '); |
| 4149 | 4149 | if ($SQL) { |
| 4150 | - return " WHERE " . $SQL; |
|
| 4150 | + return " WHERE ".$SQL; |
|
| 4151 | 4151 | } |
| 4152 | 4152 | return ''; |
| 4153 | 4153 | } |
@@ -4166,7 +4166,7 @@ discard block |
||
| 4166 | 4166 | { |
| 4167 | 4167 | $SQL = $this->_construct_condition_clause_recursive($having_params, ' AND '); |
| 4168 | 4168 | if ($SQL) { |
| 4169 | - return " HAVING " . $SQL; |
|
| 4169 | + return " HAVING ".$SQL; |
|
| 4170 | 4170 | } |
| 4171 | 4171 | return ''; |
| 4172 | 4172 | } |
@@ -4185,7 +4185,7 @@ discard block |
||
| 4185 | 4185 | { |
| 4186 | 4186 | $where_clauses = array(); |
| 4187 | 4187 | foreach ($where_params as $query_param => $op_and_value_or_sub_condition) { |
| 4188 | - $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param);//str_replace("*",'',$query_param); |
|
| 4188 | + $query_param = $this->_remove_stars_and_anything_after_from_condition_query_param_key($query_param); //str_replace("*",'',$query_param); |
|
| 4189 | 4189 | if (in_array($query_param, $this->_logic_query_param_keys)) { |
| 4190 | 4190 | switch ($query_param) { |
| 4191 | 4191 | case 'not': |
@@ -4213,7 +4213,7 @@ discard block |
||
| 4213 | 4213 | } else { |
| 4214 | 4214 | $field_obj = $this->_deduce_field_from_query_param($query_param); |
| 4215 | 4215 | //if it's not a normal field, maybe it's a custom selection? |
| 4216 | - if (! $field_obj) { |
|
| 4216 | + if ( ! $field_obj) { |
|
| 4217 | 4217 | if ($this->_custom_selections instanceof CustomSelects) { |
| 4218 | 4218 | $field_obj = $this->_custom_selections->getDataTypeForAlias($query_param); |
| 4219 | 4219 | } else { |
@@ -4222,7 +4222,7 @@ discard block |
||
| 4222 | 4222 | } |
| 4223 | 4223 | } |
| 4224 | 4224 | $op_and_value_sql = $this->_construct_op_and_value($op_and_value_or_sub_condition, $field_obj); |
| 4225 | - $where_clauses[] = $this->_deduce_column_name_from_query_param($query_param) . SP . $op_and_value_sql; |
|
| 4225 | + $where_clauses[] = $this->_deduce_column_name_from_query_param($query_param).SP.$op_and_value_sql; |
|
| 4226 | 4226 | } |
| 4227 | 4227 | } |
| 4228 | 4228 | return $where_clauses ? implode($glue, $where_clauses) : ''; |
@@ -4243,7 +4243,7 @@ discard block |
||
| 4243 | 4243 | if ($field) { |
| 4244 | 4244 | $table_alias_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_from_query_param($field->get_model_name(), |
| 4245 | 4245 | $query_param); |
| 4246 | - return $table_alias_prefix . $field->get_qualified_column(); |
|
| 4246 | + return $table_alias_prefix.$field->get_qualified_column(); |
|
| 4247 | 4247 | } |
| 4248 | 4248 | if ($this->_custom_selections instanceof CustomSelects |
| 4249 | 4249 | && in_array($query_param, $this->_custom_selections->columnAliases(), true) |
@@ -4300,7 +4300,7 @@ discard block |
||
| 4300 | 4300 | { |
| 4301 | 4301 | if (is_array($op_and_value)) { |
| 4302 | 4302 | $operator = isset($op_and_value[0]) ? $this->_prepare_operator_for_sql($op_and_value[0]) : null; |
| 4303 | - if (! $operator) { |
|
| 4303 | + if ( ! $operator) { |
|
| 4304 | 4304 | $php_array_like_string = array(); |
| 4305 | 4305 | foreach ($op_and_value as $key => $value) { |
| 4306 | 4306 | $php_array_like_string[] = "$key=>$value"; |
@@ -4322,14 +4322,14 @@ discard block |
||
| 4322 | 4322 | } |
| 4323 | 4323 | //check to see if the value is actually another field |
| 4324 | 4324 | if (is_array($op_and_value) && isset($op_and_value[2]) && $op_and_value[2] == true) { |
| 4325 | - return $operator . SP . $this->_deduce_column_name_from_query_param($value); |
|
| 4325 | + return $operator.SP.$this->_deduce_column_name_from_query_param($value); |
|
| 4326 | 4326 | } |
| 4327 | 4327 | if (in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
| 4328 | 4328 | //in this case, the value should be an array, or at least a comma-separated list |
| 4329 | 4329 | //it will need to handle a little differently |
| 4330 | 4330 | $cleaned_value = $this->_construct_in_value($value, $field_obj); |
| 4331 | 4331 | //note: $cleaned_value has already been run through $wpdb->prepare() |
| 4332 | - return $operator . SP . $cleaned_value; |
|
| 4332 | + return $operator.SP.$cleaned_value; |
|
| 4333 | 4333 | } |
| 4334 | 4334 | if (in_array($operator, $this->valid_between_style_operators()) && is_array($value)) { |
| 4335 | 4335 | //the value should be an array with count of two. |
@@ -4345,7 +4345,7 @@ discard block |
||
| 4345 | 4345 | ); |
| 4346 | 4346 | } |
| 4347 | 4347 | $cleaned_value = $this->_construct_between_value($value, $field_obj); |
| 4348 | - return $operator . SP . $cleaned_value; |
|
| 4348 | + return $operator.SP.$cleaned_value; |
|
| 4349 | 4349 | } |
| 4350 | 4350 | if (in_array($operator, $this->valid_null_style_operators())) { |
| 4351 | 4351 | if ($value !== null) { |
@@ -4365,10 +4365,10 @@ discard block |
||
| 4365 | 4365 | if (in_array($operator, $this->valid_like_style_operators()) && ! is_array($value)) { |
| 4366 | 4366 | //if the operator is 'LIKE', we want to allow percent signs (%) and not |
| 4367 | 4367 | //remove other junk. So just treat it as a string. |
| 4368 | - return $operator . SP . $this->_wpdb_prepare_using_field($value, '%s'); |
|
| 4368 | + return $operator.SP.$this->_wpdb_prepare_using_field($value, '%s'); |
|
| 4369 | 4369 | } |
| 4370 | - if (! in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
| 4371 | - return $operator . SP . $this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4370 | + if ( ! in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
|
| 4371 | + return $operator.SP.$this->_wpdb_prepare_using_field($value, $field_obj); |
|
| 4372 | 4372 | } |
| 4373 | 4373 | if (in_array($operator, $this->valid_in_style_operators()) && ! is_array($value)) { |
| 4374 | 4374 | throw new EE_Error( |
@@ -4382,7 +4382,7 @@ discard block |
||
| 4382 | 4382 | ) |
| 4383 | 4383 | ); |
| 4384 | 4384 | } |
| 4385 | - if (! in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
| 4385 | + if ( ! in_array($operator, $this->valid_in_style_operators()) && is_array($value)) { |
|
| 4386 | 4386 | throw new EE_Error( |
| 4387 | 4387 | sprintf( |
| 4388 | 4388 | __( |
@@ -4422,7 +4422,7 @@ discard block |
||
| 4422 | 4422 | foreach ($values as $value) { |
| 4423 | 4423 | $cleaned_values[] = $this->_wpdb_prepare_using_field($value, $field_obj); |
| 4424 | 4424 | } |
| 4425 | - return $cleaned_values[0] . " AND " . $cleaned_values[1]; |
|
| 4425 | + return $cleaned_values[0]." AND ".$cleaned_values[1]; |
|
| 4426 | 4426 | } |
| 4427 | 4427 | |
| 4428 | 4428 | |
@@ -4463,7 +4463,7 @@ discard block |
||
| 4463 | 4463 | . $main_table->get_table_name() |
| 4464 | 4464 | . " WHERE FALSE"; |
| 4465 | 4465 | } |
| 4466 | - return "(" . implode(",", $cleaned_values) . ")"; |
|
| 4466 | + return "(".implode(",", $cleaned_values).")"; |
|
| 4467 | 4467 | } |
| 4468 | 4468 | |
| 4469 | 4469 | |
@@ -4482,7 +4482,7 @@ discard block |
||
| 4482 | 4482 | return $wpdb->prepare($field_obj->get_wpdb_data_type(), |
| 4483 | 4483 | $this->_prepare_value_for_use_in_db($value, $field_obj)); |
| 4484 | 4484 | } //$field_obj should really just be a data type |
| 4485 | - if (! in_array($field_obj, $this->_valid_wpdb_data_types)) { |
|
| 4485 | + if ( ! in_array($field_obj, $this->_valid_wpdb_data_types)) { |
|
| 4486 | 4486 | throw new EE_Error( |
| 4487 | 4487 | sprintf( |
| 4488 | 4488 | __("%s is not a valid wpdb datatype. Valid ones are %s", "event_espresso"), |
@@ -4610,10 +4610,10 @@ discard block |
||
| 4610 | 4610 | */ |
| 4611 | 4611 | public function get_qualified_columns_for_all_fields($model_relation_chain = '', $return_string = true) |
| 4612 | 4612 | { |
| 4613 | - $table_prefix = str_replace('.', '__', $model_relation_chain) . (empty($model_relation_chain) ? '' : '__'); |
|
| 4613 | + $table_prefix = str_replace('.', '__', $model_relation_chain).(empty($model_relation_chain) ? '' : '__'); |
|
| 4614 | 4614 | $qualified_columns = array(); |
| 4615 | 4615 | foreach ($this->field_settings() as $field_name => $field) { |
| 4616 | - $qualified_columns[] = $table_prefix . $field->get_qualified_column(); |
|
| 4616 | + $qualified_columns[] = $table_prefix.$field->get_qualified_column(); |
|
| 4617 | 4617 | } |
| 4618 | 4618 | return $return_string ? implode(', ', $qualified_columns) : $qualified_columns; |
| 4619 | 4619 | } |
@@ -4637,11 +4637,11 @@ discard block |
||
| 4637 | 4637 | if ($table_obj instanceof EE_Primary_Table) { |
| 4638 | 4638 | $SQL .= $table_alias === $table_obj->get_table_alias() |
| 4639 | 4639 | ? $table_obj->get_select_join_limit($limit) |
| 4640 | - : SP . $table_obj->get_table_name() . " AS " . $table_obj->get_table_alias() . SP; |
|
| 4640 | + : SP.$table_obj->get_table_name()." AS ".$table_obj->get_table_alias().SP; |
|
| 4641 | 4641 | } elseif ($table_obj instanceof EE_Secondary_Table) { |
| 4642 | 4642 | $SQL .= $table_alias === $table_obj->get_table_alias() |
| 4643 | 4643 | ? $table_obj->get_select_join_limit_join($limit) |
| 4644 | - : SP . $table_obj->get_join_sql($table_alias) . SP; |
|
| 4644 | + : SP.$table_obj->get_join_sql($table_alias).SP; |
|
| 4645 | 4645 | } |
| 4646 | 4646 | } |
| 4647 | 4647 | return $SQL; |
@@ -4729,12 +4729,12 @@ discard block |
||
| 4729 | 4729 | */ |
| 4730 | 4730 | public function get_related_model_obj($model_name) |
| 4731 | 4731 | { |
| 4732 | - $model_classname = "EEM_" . $model_name; |
|
| 4733 | - if (! class_exists($model_classname)) { |
|
| 4732 | + $model_classname = "EEM_".$model_name; |
|
| 4733 | + if ( ! class_exists($model_classname)) { |
|
| 4734 | 4734 | throw new EE_Error(sprintf(__("You specified a related model named %s in your query. No such model exists, if it did, it would have the classname %s", |
| 4735 | 4735 | 'event_espresso'), $model_name, $model_classname)); |
| 4736 | 4736 | } |
| 4737 | - return call_user_func($model_classname . "::instance"); |
|
| 4737 | + return call_user_func($model_classname."::instance"); |
|
| 4738 | 4738 | } |
| 4739 | 4739 | |
| 4740 | 4740 | |
@@ -4781,7 +4781,7 @@ discard block |
||
| 4781 | 4781 | public function related_settings_for($relation_name) |
| 4782 | 4782 | { |
| 4783 | 4783 | $relatedModels = $this->relation_settings(); |
| 4784 | - if (! array_key_exists($relation_name, $relatedModels)) { |
|
| 4784 | + if ( ! array_key_exists($relation_name, $relatedModels)) { |
|
| 4785 | 4785 | throw new EE_Error( |
| 4786 | 4786 | sprintf( |
| 4787 | 4787 | __('Cannot get %s related to %s. There is no model relation of that type. There is, however, %s...', |
@@ -4809,7 +4809,7 @@ discard block |
||
| 4809 | 4809 | public function field_settings_for($fieldName, $include_db_only_fields = true) |
| 4810 | 4810 | { |
| 4811 | 4811 | $fieldSettings = $this->field_settings($include_db_only_fields); |
| 4812 | - if (! array_key_exists($fieldName, $fieldSettings)) { |
|
| 4812 | + if ( ! array_key_exists($fieldName, $fieldSettings)) { |
|
| 4813 | 4813 | throw new EE_Error(sprintf(__("There is no field/column '%s' on '%s'", 'event_espresso'), $fieldName, |
| 4814 | 4814 | get_class($this))); |
| 4815 | 4815 | } |
@@ -4882,7 +4882,7 @@ discard block |
||
| 4882 | 4882 | break; |
| 4883 | 4883 | } |
| 4884 | 4884 | } |
| 4885 | - if (! $this->_primary_key_field instanceof EE_Primary_Key_Field_Base) { |
|
| 4885 | + if ( ! $this->_primary_key_field instanceof EE_Primary_Key_Field_Base) { |
|
| 4886 | 4886 | throw new EE_Error(sprintf(__("There is no Primary Key defined on model %s", 'event_espresso'), |
| 4887 | 4887 | get_class($this))); |
| 4888 | 4888 | } |
@@ -4941,7 +4941,7 @@ discard block |
||
| 4941 | 4941 | */ |
| 4942 | 4942 | public function get_foreign_key_to($model_name) |
| 4943 | 4943 | { |
| 4944 | - if (! isset($this->_cache_foreign_key_to_fields[$model_name])) { |
|
| 4944 | + if ( ! isset($this->_cache_foreign_key_to_fields[$model_name])) { |
|
| 4945 | 4945 | foreach ($this->field_settings() as $field) { |
| 4946 | 4946 | if ( |
| 4947 | 4947 | $field instanceof EE_Foreign_Key_Field_Base |
@@ -4951,7 +4951,7 @@ discard block |
||
| 4951 | 4951 | break; |
| 4952 | 4952 | } |
| 4953 | 4953 | } |
| 4954 | - if (! isset($this->_cache_foreign_key_to_fields[$model_name])) { |
|
| 4954 | + if ( ! isset($this->_cache_foreign_key_to_fields[$model_name])) { |
|
| 4955 | 4955 | throw new EE_Error(sprintf(__("There is no foreign key field pointing to model %s on model %s", |
| 4956 | 4956 | 'event_espresso'), $model_name, get_class($this))); |
| 4957 | 4957 | } |
@@ -5002,7 +5002,7 @@ discard block |
||
| 5002 | 5002 | foreach ($this->_fields as $fields_corresponding_to_table) { |
| 5003 | 5003 | foreach ($fields_corresponding_to_table as $field_name => $field_obj) { |
| 5004 | 5004 | /** @var $field_obj EE_Model_Field_Base */ |
| 5005 | - if (! $field_obj->is_db_only_field()) { |
|
| 5005 | + if ( ! $field_obj->is_db_only_field()) { |
|
| 5006 | 5006 | $this->_cached_fields_non_db_only[$field_name] = $field_obj; |
| 5007 | 5007 | } |
| 5008 | 5008 | } |
@@ -5031,7 +5031,7 @@ discard block |
||
| 5031 | 5031 | $count_if_model_has_no_primary_key = 0; |
| 5032 | 5032 | $has_primary_key = $this->has_primary_key_field(); |
| 5033 | 5033 | $primary_key_field = $has_primary_key ? $this->get_primary_key_field() : null; |
| 5034 | - foreach ((array)$rows as $row) { |
|
| 5034 | + foreach ((array) $rows as $row) { |
|
| 5035 | 5035 | if (empty($row)) { |
| 5036 | 5036 | //wp did its weird thing where it returns an array like array(0=>null), which is totally not helpful... |
| 5037 | 5037 | return array(); |
@@ -5049,7 +5049,7 @@ discard block |
||
| 5049 | 5049 | } |
| 5050 | 5050 | } |
| 5051 | 5051 | $classInstance = $this->instantiate_class_from_array_or_object($row); |
| 5052 | - if (! $classInstance) { |
|
| 5052 | + if ( ! $classInstance) { |
|
| 5053 | 5053 | throw new EE_Error( |
| 5054 | 5054 | sprintf( |
| 5055 | 5055 | __('Could not create instance of class %s from row %s', 'event_espresso'), |
@@ -5168,7 +5168,7 @@ discard block |
||
| 5168 | 5168 | */ |
| 5169 | 5169 | public function instantiate_class_from_array_or_object($cols_n_values) |
| 5170 | 5170 | { |
| 5171 | - if (! is_array($cols_n_values) && is_object($cols_n_values)) { |
|
| 5171 | + if ( ! is_array($cols_n_values) && is_object($cols_n_values)) { |
|
| 5172 | 5172 | $cols_n_values = get_object_vars($cols_n_values); |
| 5173 | 5173 | } |
| 5174 | 5174 | $primary_key = null; |
@@ -5193,7 +5193,7 @@ discard block |
||
| 5193 | 5193 | // if there is no primary key or the object doesn't already exist in the entity map, then create a new instance |
| 5194 | 5194 | if ($primary_key) { |
| 5195 | 5195 | $classInstance = $this->get_from_entity_map($primary_key); |
| 5196 | - if (! $classInstance) { |
|
| 5196 | + if ( ! $classInstance) { |
|
| 5197 | 5197 | $classInstance = EE_Registry::instance() |
| 5198 | 5198 | ->load_class($className, |
| 5199 | 5199 | array($this_model_fields_n_values, $this->_timezone), true, false); |
@@ -5242,12 +5242,12 @@ discard block |
||
| 5242 | 5242 | public function add_to_entity_map(EE_Base_Class $object) |
| 5243 | 5243 | { |
| 5244 | 5244 | $className = $this->_get_class_name(); |
| 5245 | - if (! $object instanceof $className) { |
|
| 5245 | + if ( ! $object instanceof $className) { |
|
| 5246 | 5246 | throw new EE_Error(sprintf(__("You tried adding a %s to a mapping of %ss", "event_espresso"), |
| 5247 | 5247 | is_object($object) ? get_class($object) : $object, $className)); |
| 5248 | 5248 | } |
| 5249 | 5249 | /** @var $object EE_Base_Class */ |
| 5250 | - if (! $object->ID()) { |
|
| 5250 | + if ( ! $object->ID()) { |
|
| 5251 | 5251 | throw new EE_Error(sprintf(__("You tried storing a model object with NO ID in the %s entity mapper.", |
| 5252 | 5252 | "event_espresso"), get_class($this))); |
| 5253 | 5253 | } |
@@ -5316,7 +5316,7 @@ discard block |
||
| 5316 | 5316 | //there is a primary key on this table and its not set. Use defaults for all its columns |
| 5317 | 5317 | if ($table_pk_value === null && $table_obj->get_pk_column()) { |
| 5318 | 5318 | foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
| 5319 | - if (! $field_obj->is_db_only_field()) { |
|
| 5319 | + if ( ! $field_obj->is_db_only_field()) { |
|
| 5320 | 5320 | //prepare field as if its coming from db |
| 5321 | 5321 | $prepared_value = $field_obj->prepare_for_set($field_obj->get_default_value()); |
| 5322 | 5322 | $this_model_fields_n_values[$field_name] = $field_obj->prepare_for_use_in_db($prepared_value); |
@@ -5325,7 +5325,7 @@ discard block |
||
| 5325 | 5325 | } else { |
| 5326 | 5326 | //the table's rows existed. Use their values |
| 5327 | 5327 | foreach ($this->_get_fields_for_table($table_alias) as $field_name => $field_obj) { |
| 5328 | - if (! $field_obj->is_db_only_field()) { |
|
| 5328 | + if ( ! $field_obj->is_db_only_field()) { |
|
| 5329 | 5329 | $this_model_fields_n_values[$field_name] = $this->_get_column_value_with_table_alias_or_not( |
| 5330 | 5330 | $cols_n_values, $field_obj->get_qualified_column(), |
| 5331 | 5331 | $field_obj->get_table_column() |
@@ -5440,7 +5440,7 @@ discard block |
||
| 5440 | 5440 | */ |
| 5441 | 5441 | private function _get_class_name() |
| 5442 | 5442 | { |
| 5443 | - return "EE_" . $this->get_this_model_name(); |
|
| 5443 | + return "EE_".$this->get_this_model_name(); |
|
| 5444 | 5444 | } |
| 5445 | 5445 | |
| 5446 | 5446 | |
@@ -5455,7 +5455,7 @@ discard block |
||
| 5455 | 5455 | */ |
| 5456 | 5456 | public function item_name($quantity = 1) |
| 5457 | 5457 | { |
| 5458 | - return (int)$quantity === 1 ? $this->singular_item : $this->plural_item; |
|
| 5458 | + return (int) $quantity === 1 ? $this->singular_item : $this->plural_item; |
|
| 5459 | 5459 | } |
| 5460 | 5460 | |
| 5461 | 5461 | |
@@ -5488,7 +5488,7 @@ discard block |
||
| 5488 | 5488 | { |
| 5489 | 5489 | $className = get_class($this); |
| 5490 | 5490 | $tagName = "FHEE__{$className}__{$methodName}"; |
| 5491 | - if (! has_filter($tagName)) { |
|
| 5491 | + if ( ! has_filter($tagName)) { |
|
| 5492 | 5492 | throw new EE_Error( |
| 5493 | 5493 | sprintf( |
| 5494 | 5494 | __('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 );', |
@@ -5714,7 +5714,7 @@ discard block |
||
| 5714 | 5714 | $key_vals_in_combined_pk = array(); |
| 5715 | 5715 | parse_str($index_primary_key_string, $key_vals_in_combined_pk); |
| 5716 | 5716 | foreach ($key_fields as $key_field_name => $field_obj) { |
| 5717 | - if (! isset($key_vals_in_combined_pk[$key_field_name])) { |
|
| 5717 | + if ( ! isset($key_vals_in_combined_pk[$key_field_name])) { |
|
| 5718 | 5718 | return null; |
| 5719 | 5719 | } |
| 5720 | 5720 | } |
@@ -5735,7 +5735,7 @@ discard block |
||
| 5735 | 5735 | { |
| 5736 | 5736 | $keys_it_should_have = array_keys($this->get_combined_primary_key_fields()); |
| 5737 | 5737 | foreach ($keys_it_should_have as $key) { |
| 5738 | - if (! isset($key_vals[$key])) { |
|
| 5738 | + if ( ! isset($key_vals[$key])) { |
|
| 5739 | 5739 | return false; |
| 5740 | 5740 | } |
| 5741 | 5741 | } |
@@ -5789,7 +5789,7 @@ discard block |
||
| 5789 | 5789 | */ |
| 5790 | 5790 | public function get_one_copy($model_object_or_attributes_array, $query_params = array()) |
| 5791 | 5791 | { |
| 5792 | - if (! is_array($query_params)) { |
|
| 5792 | + if ( ! is_array($query_params)) { |
|
| 5793 | 5793 | EE_Error::doing_it_wrong('EEM_Base::get_one_copy', |
| 5794 | 5794 | sprintf(__('$query_params should be an array, you passed a variable of type %s', 'event_espresso'), |
| 5795 | 5795 | gettype($query_params)), '4.6.0'); |
@@ -5855,7 +5855,7 @@ discard block |
||
| 5855 | 5855 | * Gets the valid operators |
| 5856 | 5856 | * @return array keys are accepted strings, values are the SQL they are converted to |
| 5857 | 5857 | */ |
| 5858 | - public function valid_operators(){ |
|
| 5858 | + public function valid_operators() { |
|
| 5859 | 5859 | return $this->_valid_operators; |
| 5860 | 5860 | } |
| 5861 | 5861 | |
@@ -5943,7 +5943,7 @@ discard block |
||
| 5943 | 5943 | */ |
| 5944 | 5944 | public function get_IDs($model_objects, $filter_out_empty_ids = false) |
| 5945 | 5945 | { |
| 5946 | - if (! $this->has_primary_key_field()) { |
|
| 5946 | + if ( ! $this->has_primary_key_field()) { |
|
| 5947 | 5947 | if (WP_DEBUG) { |
| 5948 | 5948 | EE_Error::add_error( |
| 5949 | 5949 | __('Trying to get IDs from a model than has no primary key', 'event_espresso'), |
@@ -5956,7 +5956,7 @@ discard block |
||
| 5956 | 5956 | $IDs = array(); |
| 5957 | 5957 | foreach ($model_objects as $model_object) { |
| 5958 | 5958 | $id = $model_object->ID(); |
| 5959 | - if (! $id) { |
|
| 5959 | + if ( ! $id) { |
|
| 5960 | 5960 | if ($filter_out_empty_ids) { |
| 5961 | 5961 | continue; |
| 5962 | 5962 | } |
@@ -6052,8 +6052,8 @@ discard block |
||
| 6052 | 6052 | $missing_caps = array(); |
| 6053 | 6053 | $cap_restrictions = $this->cap_restrictions($context); |
| 6054 | 6054 | foreach ($cap_restrictions as $cap => $restriction_if_no_cap) { |
| 6055 | - if (! EE_Capabilities::instance() |
|
| 6056 | - ->current_user_can($cap, $this->get_this_model_name() . '_model_applying_caps') |
|
| 6055 | + if ( ! EE_Capabilities::instance() |
|
| 6056 | + ->current_user_can($cap, $this->get_this_model_name().'_model_applying_caps') |
|
| 6057 | 6057 | ) { |
| 6058 | 6058 | $missing_caps[$cap] = $restriction_if_no_cap; |
| 6059 | 6059 | } |
@@ -6205,7 +6205,7 @@ discard block |
||
| 6205 | 6205 | { |
| 6206 | 6206 | foreach ($this->logic_query_param_keys() as $logic_query_param_key) { |
| 6207 | 6207 | if ($query_param_key === $logic_query_param_key |
| 6208 | - || strpos($query_param_key, $logic_query_param_key . '*') === 0 |
|
| 6208 | + || strpos($query_param_key, $logic_query_param_key.'*') === 0 |
|
| 6209 | 6209 | ) { |
| 6210 | 6210 | return true; |
| 6211 | 6211 | } |
@@ -14,326 +14,326 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class CustomSelects |
| 16 | 16 | { |
| 17 | - const TYPE_SIMPLE = 'simple'; |
|
| 18 | - const TYPE_COMPLEX = 'complex'; |
|
| 19 | - const TYPE_STRUCTURED = 'structured'; |
|
| 20 | - |
|
| 21 | - private $valid_operators = array('COUNT', 'SUM'); |
|
| 22 | - |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Original incoming select array |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - private $original_selects; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Select string that can be added to the query |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - private $columns_to_select_expression; |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * An array of aliases for the columns included in the incoming select array. |
|
| 39 | - * @var array |
|
| 40 | - */ |
|
| 41 | - private $column_aliases_in_select; |
|
| 42 | - |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Enum representation of the "type" of array coming into this value object. |
|
| 46 | - * @var string |
|
| 47 | - * |
|
| 48 | - */ |
|
| 49 | - private $type = ''; |
|
| 50 | - |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * CustomSelects constructor. |
|
| 54 | - * Incoming selects can be in one of the following formats: |
|
| 55 | - * ---- self::TYPE_SIMPLE array ---- |
|
| 56 | - * This is considered the "simple" type. In this case the array is an numerically indexed array with single or |
|
| 57 | - * multiple columns to select as the values. |
|
| 58 | - * eg. array( 'ATT_ID', 'REG_ID' ) |
|
| 59 | - * eg. array( '*' ) |
|
| 60 | - * If you want to use the columns in any WHERE, GROUP BY, or HAVING clauses, you must instead use the "complex" or |
|
| 61 | - * "structured" method. |
|
| 62 | - * ---- self::TYPE_COMPLEX array ---- |
|
| 63 | - * This is considered the "complex" type. In this case the array is indexed by arbitrary strings that serve as |
|
| 64 | - * column alias, and the value is an numerically indexed array where there are two values. The first value (0) is |
|
| 65 | - * the selection and the second value (1) is the data type. Data types must be one of the types defined in |
|
| 66 | - * EEM_Base::$_valid_wpdb_data_types. |
|
| 67 | - * eg. array( 'count' => array('count(REG_ID)', '%d') ) |
|
| 68 | - * Complex array configuration allows for using the column alias in any WHERE, GROUP BY, or HAVING clauses. |
|
| 69 | - * ---- self::TYPE_STRUCTURED array --- |
|
| 70 | - * This is considered the "structured" type. This type is similar to the complex type except that the array attached |
|
| 71 | - * to the column alias contains three values. The first value is the qualified column name (which can include |
|
| 72 | - * join syntax for models). The second value is the operator performed on the column (i.e. 'COUNT', 'SUM' etc)., |
|
| 73 | - * the third value is the data type. Note, if the select does not have an operator, you can use an empty string for |
|
| 74 | - * the second value. |
|
| 75 | - * Note: for now SUM is only for simple single column expressions (i.e. SUM(Transaction.TXN_total)) |
|
| 76 | - * eg. array( 'registration_count' => array('Registration.REG_ID', 'count', '%d') ); |
|
| 77 | - * |
|
| 78 | - * NOTE: mixing array types in the incoming $select will cause errors. |
|
| 79 | - * |
|
| 80 | - * @param array $selects |
|
| 81 | - * @throws InvalidArgumentException |
|
| 82 | - */ |
|
| 83 | - public function __construct(array $selects) |
|
| 84 | - { |
|
| 85 | - $this->original_selects = $selects; |
|
| 86 | - $this->deriveType($selects); |
|
| 87 | - $this->deriveParts($selects); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Derives what type of custom select has been sent in. |
|
| 93 | - * @param array $selects |
|
| 94 | - * @throws InvalidArgumentException |
|
| 95 | - */ |
|
| 96 | - private function deriveType(array $selects) |
|
| 97 | - { |
|
| 98 | - //first if the first key for this array is an integer then its coming in as a simple format, so we'll also |
|
| 99 | - // ensure all elements of the array are simple. |
|
| 100 | - if (is_int(key($selects))) { |
|
| 101 | - //let's ensure all keys are ints |
|
| 102 | - $invalid_keys = array_filter( |
|
| 103 | - array_keys($selects), |
|
| 104 | - function ($value) { |
|
| 105 | - return ! is_int($value); |
|
| 106 | - } |
|
| 107 | - ); |
|
| 108 | - if (! empty($invalid_keys)) { |
|
| 109 | - throw new InvalidArgumentException( |
|
| 110 | - sprintf( |
|
| 111 | - esc_html__( |
|
| 112 | - 'Incoming array looks like its formatted for "%1$s" type selects, however it has elements that are not indexed numerically', |
|
| 113 | - 'event_espresso' |
|
| 114 | - ), |
|
| 115 | - self::TYPE_SIMPLE |
|
| 116 | - ) |
|
| 117 | - ); |
|
| 118 | - } |
|
| 119 | - $this->type = self::TYPE_SIMPLE; |
|
| 120 | - return; |
|
| 121 | - } |
|
| 122 | - //made it here so that means we've got either complex or structured selects. Let's find out which by popping |
|
| 123 | - //the first array element off. |
|
| 124 | - $first_element = reset($selects); |
|
| 125 | - |
|
| 126 | - if (! is_array($first_element)) { |
|
| 127 | - throw new InvalidArgumentException( |
|
| 128 | - sprintf( |
|
| 129 | - esc_html__( |
|
| 130 | - 'Incoming array looks like its formatted as a "%1$s" or "%2$%s" type. However, the values in the array must be arrays themselves and they are not.', |
|
| 131 | - 'event_espresso' |
|
| 132 | - ), |
|
| 133 | - self::TYPE_COMPLEX, |
|
| 134 | - self::TYPE_STRUCTURED |
|
| 135 | - ) |
|
| 136 | - ); |
|
| 137 | - } |
|
| 138 | - $this->type = count($first_element) === 2 |
|
| 139 | - ? self::TYPE_COMPLEX |
|
| 140 | - : self::TYPE_STRUCTURED; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Sets up the various properties for the vo depending on type. |
|
| 146 | - * @param array $selects |
|
| 147 | - * @throws InvalidArgumentException |
|
| 148 | - */ |
|
| 149 | - private function deriveParts(array $selects) |
|
| 150 | - { |
|
| 151 | - $column_parts = array(); |
|
| 152 | - switch ($this->type) { |
|
| 153 | - case self::TYPE_SIMPLE: |
|
| 154 | - $column_parts = $selects; |
|
| 155 | - $this->column_aliases_in_select = $selects; |
|
| 156 | - break; |
|
| 157 | - case self::TYPE_COMPLEX: |
|
| 158 | - foreach ($selects as $alias => $parts) { |
|
| 159 | - $this->validateSelectValueForType($parts, $alias); |
|
| 160 | - $column_parts[] = "{$parts[0]} AS {$alias}"; |
|
| 161 | - $this->column_aliases_in_select[] = $alias; |
|
| 162 | - } |
|
| 163 | - break; |
|
| 164 | - case self::TYPE_STRUCTURED: |
|
| 165 | - foreach ($selects as $alias => $parts) { |
|
| 166 | - $this->validateSelectValueForType($parts, $alias); |
|
| 167 | - $column_parts[] = $parts[1] !== '' |
|
| 168 | - ? $this->assembleSelectStringWithOperator($parts, $alias) |
|
| 169 | - : "{$parts[0]} AS {$alias}"; |
|
| 170 | - $this->column_aliases_in_select[] = $alias; |
|
| 171 | - } |
|
| 172 | - break; |
|
| 173 | - } |
|
| 174 | - $this->columns_to_select_expression = implode(', ', $column_parts); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Validates self::TYPE_COMPLEX and self::TYPE_STRUCTURED select statement parts. |
|
| 180 | - * @param array $select_parts |
|
| 181 | - * @param string $alias |
|
| 182 | - * @throws InvalidArgumentException |
|
| 183 | - */ |
|
| 184 | - private function validateSelectValueForType(array $select_parts, $alias) |
|
| 185 | - { |
|
| 186 | - $valid_data_types = array('%d', '%s', '%f'); |
|
| 187 | - if (count($select_parts) !== $this->expectedSelectPartCountForType()) { |
|
| 188 | - throw new InvalidArgumentException( |
|
| 189 | - sprintf( |
|
| 190 | - esc_html__( |
|
| 191 | - 'The provided select part array for the %1$s column is expected to have a count of %2$d because the incoming select array is of type %3$s. However the count was %4$d.', |
|
| 192 | - 'event_espresso' |
|
| 193 | - ), |
|
| 194 | - $alias, |
|
| 195 | - $this->expectedSelectPartCountForType(), |
|
| 196 | - $this->type, |
|
| 197 | - count($select_parts) |
|
| 198 | - ) |
|
| 199 | - ); |
|
| 200 | - } |
|
| 201 | - //validate data type. |
|
| 202 | - $data_type = $this->type === self::TYPE_COMPLEX ? $select_parts[1] : ''; |
|
| 203 | - $data_type = $this->type === self::TYPE_STRUCTURED ? $select_parts[2] : $data_type; |
|
| 204 | - |
|
| 205 | - if (! in_array($data_type, $valid_data_types, true)) { |
|
| 206 | - throw new InvalidArgumentException( |
|
| 207 | - sprintf( |
|
| 208 | - esc_html__( |
|
| 209 | - 'Datatype %1$s (for selection "%2$s" and alias "%3$s") is not a valid wpdb datatype (eg %%s)', |
|
| 210 | - 'event_espresso' |
|
| 211 | - ), |
|
| 212 | - $data_type, |
|
| 213 | - $select_parts[0], |
|
| 214 | - $alias, |
|
| 215 | - implode(', ', $valid_data_types) |
|
| 216 | - ) |
|
| 217 | - ); |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Each type will have an expected count of array elements, this returns what that expected count is. |
|
| 224 | - * @param string $type |
|
| 225 | - * @return int |
|
| 226 | - */ |
|
| 227 | - private function expectedSelectPartCountForType($type = '') { |
|
| 228 | - $type = $type === '' ? $this->type : $type; |
|
| 229 | - $types_count_map = array( |
|
| 230 | - self::TYPE_COMPLEX => 2, |
|
| 231 | - self::TYPE_STRUCTURED => 3 |
|
| 232 | - ); |
|
| 233 | - return isset($types_count_map[$type]) ? $types_count_map[$type] : 0; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Prepares the select statement part for for structured type selects. |
|
| 239 | - * @param array $select_parts |
|
| 240 | - * @param string $alias |
|
| 241 | - * @return string |
|
| 242 | - * @throws InvalidArgumentException |
|
| 243 | - */ |
|
| 244 | - private function assembleSelectStringWithOperator(array $select_parts, $alias) |
|
| 245 | - { |
|
| 246 | - $operator = strtoupper($select_parts[1]); |
|
| 247 | - //validate operator |
|
| 248 | - if (! in_array($operator, $this->valid_operators, true)) { |
|
| 249 | - throw new InvalidArgumentException( |
|
| 250 | - sprintf( |
|
| 251 | - esc_html__( |
|
| 252 | - 'An invalid operator has been provided (%1$s) for the column %2$s. Valid operators must be one of the following: %3$s.', |
|
| 253 | - 'event_espresso' |
|
| 254 | - ), |
|
| 255 | - $operator, |
|
| 256 | - $alias, |
|
| 257 | - implode(', ', $this->valid_operators) |
|
| 258 | - ) |
|
| 259 | - ); |
|
| 260 | - } |
|
| 261 | - return $operator . '(' . $select_parts[0] . ') AS ' . $alias; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * Return the datatype from the given select part. |
|
| 267 | - * Remember the select_part has already been validated on object instantiation. |
|
| 268 | - * @param array $select_part |
|
| 269 | - * @return string |
|
| 270 | - */ |
|
| 271 | - private function getDataTypeForSelectType(array $select_part) |
|
| 272 | - { |
|
| 273 | - switch ($this->type) { |
|
| 274 | - case self::TYPE_COMPLEX: |
|
| 275 | - return $select_part[1]; |
|
| 276 | - case self::TYPE_STRUCTURED: |
|
| 277 | - return $select_part[2]; |
|
| 278 | - default: |
|
| 279 | - return ''; |
|
| 280 | - } |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Returns the original select array sent into the VO. |
|
| 286 | - * @return array |
|
| 287 | - */ |
|
| 288 | - public function originalSelects() |
|
| 289 | - { |
|
| 290 | - return $this->original_selects; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Returns the final assembled select expression derived from the incoming select array. |
|
| 296 | - * @return string |
|
| 297 | - */ |
|
| 298 | - public function columnsToSelectExpression() |
|
| 299 | - { |
|
| 300 | - return $this->columns_to_select_expression; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Returns all the column aliases derived from the incoming select array. |
|
| 306 | - * @return array |
|
| 307 | - */ |
|
| 308 | - public function columnAliases() |
|
| 309 | - { |
|
| 310 | - return $this->column_aliases_in_select; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * Returns the enum type for the incoming select array. |
|
| 316 | - * @return string |
|
| 317 | - */ |
|
| 318 | - public function type() |
|
| 319 | - { |
|
| 320 | - return $this->type; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * Return the datatype for the given column_alias |
|
| 327 | - * @param string $column_alias |
|
| 328 | - * @return string (if there's no data type we return string as the default). |
|
| 329 | - */ |
|
| 330 | - public function getDataTypeForAlias($column_alias) |
|
| 331 | - { |
|
| 332 | - if (isset($this->original_selects[$column_alias]) |
|
| 333 | - && in_array($column_alias, $this->columnAliases(), true) |
|
| 334 | - ) { |
|
| 335 | - return $this->getDataTypeForSelectType($this->original_selects[$column_alias]); |
|
| 336 | - } |
|
| 337 | - return '%s'; |
|
| 338 | - } |
|
| 17 | + const TYPE_SIMPLE = 'simple'; |
|
| 18 | + const TYPE_COMPLEX = 'complex'; |
|
| 19 | + const TYPE_STRUCTURED = 'structured'; |
|
| 20 | + |
|
| 21 | + private $valid_operators = array('COUNT', 'SUM'); |
|
| 22 | + |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Original incoming select array |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + private $original_selects; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Select string that can be added to the query |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + private $columns_to_select_expression; |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * An array of aliases for the columns included in the incoming select array. |
|
| 39 | + * @var array |
|
| 40 | + */ |
|
| 41 | + private $column_aliases_in_select; |
|
| 42 | + |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Enum representation of the "type" of array coming into this value object. |
|
| 46 | + * @var string |
|
| 47 | + * |
|
| 48 | + */ |
|
| 49 | + private $type = ''; |
|
| 50 | + |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * CustomSelects constructor. |
|
| 54 | + * Incoming selects can be in one of the following formats: |
|
| 55 | + * ---- self::TYPE_SIMPLE array ---- |
|
| 56 | + * This is considered the "simple" type. In this case the array is an numerically indexed array with single or |
|
| 57 | + * multiple columns to select as the values. |
|
| 58 | + * eg. array( 'ATT_ID', 'REG_ID' ) |
|
| 59 | + * eg. array( '*' ) |
|
| 60 | + * If you want to use the columns in any WHERE, GROUP BY, or HAVING clauses, you must instead use the "complex" or |
|
| 61 | + * "structured" method. |
|
| 62 | + * ---- self::TYPE_COMPLEX array ---- |
|
| 63 | + * This is considered the "complex" type. In this case the array is indexed by arbitrary strings that serve as |
|
| 64 | + * column alias, and the value is an numerically indexed array where there are two values. The first value (0) is |
|
| 65 | + * the selection and the second value (1) is the data type. Data types must be one of the types defined in |
|
| 66 | + * EEM_Base::$_valid_wpdb_data_types. |
|
| 67 | + * eg. array( 'count' => array('count(REG_ID)', '%d') ) |
|
| 68 | + * Complex array configuration allows for using the column alias in any WHERE, GROUP BY, or HAVING clauses. |
|
| 69 | + * ---- self::TYPE_STRUCTURED array --- |
|
| 70 | + * This is considered the "structured" type. This type is similar to the complex type except that the array attached |
|
| 71 | + * to the column alias contains three values. The first value is the qualified column name (which can include |
|
| 72 | + * join syntax for models). The second value is the operator performed on the column (i.e. 'COUNT', 'SUM' etc)., |
|
| 73 | + * the third value is the data type. Note, if the select does not have an operator, you can use an empty string for |
|
| 74 | + * the second value. |
|
| 75 | + * Note: for now SUM is only for simple single column expressions (i.e. SUM(Transaction.TXN_total)) |
|
| 76 | + * eg. array( 'registration_count' => array('Registration.REG_ID', 'count', '%d') ); |
|
| 77 | + * |
|
| 78 | + * NOTE: mixing array types in the incoming $select will cause errors. |
|
| 79 | + * |
|
| 80 | + * @param array $selects |
|
| 81 | + * @throws InvalidArgumentException |
|
| 82 | + */ |
|
| 83 | + public function __construct(array $selects) |
|
| 84 | + { |
|
| 85 | + $this->original_selects = $selects; |
|
| 86 | + $this->deriveType($selects); |
|
| 87 | + $this->deriveParts($selects); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Derives what type of custom select has been sent in. |
|
| 93 | + * @param array $selects |
|
| 94 | + * @throws InvalidArgumentException |
|
| 95 | + */ |
|
| 96 | + private function deriveType(array $selects) |
|
| 97 | + { |
|
| 98 | + //first if the first key for this array is an integer then its coming in as a simple format, so we'll also |
|
| 99 | + // ensure all elements of the array are simple. |
|
| 100 | + if (is_int(key($selects))) { |
|
| 101 | + //let's ensure all keys are ints |
|
| 102 | + $invalid_keys = array_filter( |
|
| 103 | + array_keys($selects), |
|
| 104 | + function ($value) { |
|
| 105 | + return ! is_int($value); |
|
| 106 | + } |
|
| 107 | + ); |
|
| 108 | + if (! empty($invalid_keys)) { |
|
| 109 | + throw new InvalidArgumentException( |
|
| 110 | + sprintf( |
|
| 111 | + esc_html__( |
|
| 112 | + 'Incoming array looks like its formatted for "%1$s" type selects, however it has elements that are not indexed numerically', |
|
| 113 | + 'event_espresso' |
|
| 114 | + ), |
|
| 115 | + self::TYPE_SIMPLE |
|
| 116 | + ) |
|
| 117 | + ); |
|
| 118 | + } |
|
| 119 | + $this->type = self::TYPE_SIMPLE; |
|
| 120 | + return; |
|
| 121 | + } |
|
| 122 | + //made it here so that means we've got either complex or structured selects. Let's find out which by popping |
|
| 123 | + //the first array element off. |
|
| 124 | + $first_element = reset($selects); |
|
| 125 | + |
|
| 126 | + if (! is_array($first_element)) { |
|
| 127 | + throw new InvalidArgumentException( |
|
| 128 | + sprintf( |
|
| 129 | + esc_html__( |
|
| 130 | + 'Incoming array looks like its formatted as a "%1$s" or "%2$%s" type. However, the values in the array must be arrays themselves and they are not.', |
|
| 131 | + 'event_espresso' |
|
| 132 | + ), |
|
| 133 | + self::TYPE_COMPLEX, |
|
| 134 | + self::TYPE_STRUCTURED |
|
| 135 | + ) |
|
| 136 | + ); |
|
| 137 | + } |
|
| 138 | + $this->type = count($first_element) === 2 |
|
| 139 | + ? self::TYPE_COMPLEX |
|
| 140 | + : self::TYPE_STRUCTURED; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Sets up the various properties for the vo depending on type. |
|
| 146 | + * @param array $selects |
|
| 147 | + * @throws InvalidArgumentException |
|
| 148 | + */ |
|
| 149 | + private function deriveParts(array $selects) |
|
| 150 | + { |
|
| 151 | + $column_parts = array(); |
|
| 152 | + switch ($this->type) { |
|
| 153 | + case self::TYPE_SIMPLE: |
|
| 154 | + $column_parts = $selects; |
|
| 155 | + $this->column_aliases_in_select = $selects; |
|
| 156 | + break; |
|
| 157 | + case self::TYPE_COMPLEX: |
|
| 158 | + foreach ($selects as $alias => $parts) { |
|
| 159 | + $this->validateSelectValueForType($parts, $alias); |
|
| 160 | + $column_parts[] = "{$parts[0]} AS {$alias}"; |
|
| 161 | + $this->column_aliases_in_select[] = $alias; |
|
| 162 | + } |
|
| 163 | + break; |
|
| 164 | + case self::TYPE_STRUCTURED: |
|
| 165 | + foreach ($selects as $alias => $parts) { |
|
| 166 | + $this->validateSelectValueForType($parts, $alias); |
|
| 167 | + $column_parts[] = $parts[1] !== '' |
|
| 168 | + ? $this->assembleSelectStringWithOperator($parts, $alias) |
|
| 169 | + : "{$parts[0]} AS {$alias}"; |
|
| 170 | + $this->column_aliases_in_select[] = $alias; |
|
| 171 | + } |
|
| 172 | + break; |
|
| 173 | + } |
|
| 174 | + $this->columns_to_select_expression = implode(', ', $column_parts); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Validates self::TYPE_COMPLEX and self::TYPE_STRUCTURED select statement parts. |
|
| 180 | + * @param array $select_parts |
|
| 181 | + * @param string $alias |
|
| 182 | + * @throws InvalidArgumentException |
|
| 183 | + */ |
|
| 184 | + private function validateSelectValueForType(array $select_parts, $alias) |
|
| 185 | + { |
|
| 186 | + $valid_data_types = array('%d', '%s', '%f'); |
|
| 187 | + if (count($select_parts) !== $this->expectedSelectPartCountForType()) { |
|
| 188 | + throw new InvalidArgumentException( |
|
| 189 | + sprintf( |
|
| 190 | + esc_html__( |
|
| 191 | + 'The provided select part array for the %1$s column is expected to have a count of %2$d because the incoming select array is of type %3$s. However the count was %4$d.', |
|
| 192 | + 'event_espresso' |
|
| 193 | + ), |
|
| 194 | + $alias, |
|
| 195 | + $this->expectedSelectPartCountForType(), |
|
| 196 | + $this->type, |
|
| 197 | + count($select_parts) |
|
| 198 | + ) |
|
| 199 | + ); |
|
| 200 | + } |
|
| 201 | + //validate data type. |
|
| 202 | + $data_type = $this->type === self::TYPE_COMPLEX ? $select_parts[1] : ''; |
|
| 203 | + $data_type = $this->type === self::TYPE_STRUCTURED ? $select_parts[2] : $data_type; |
|
| 204 | + |
|
| 205 | + if (! in_array($data_type, $valid_data_types, true)) { |
|
| 206 | + throw new InvalidArgumentException( |
|
| 207 | + sprintf( |
|
| 208 | + esc_html__( |
|
| 209 | + 'Datatype %1$s (for selection "%2$s" and alias "%3$s") is not a valid wpdb datatype (eg %%s)', |
|
| 210 | + 'event_espresso' |
|
| 211 | + ), |
|
| 212 | + $data_type, |
|
| 213 | + $select_parts[0], |
|
| 214 | + $alias, |
|
| 215 | + implode(', ', $valid_data_types) |
|
| 216 | + ) |
|
| 217 | + ); |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Each type will have an expected count of array elements, this returns what that expected count is. |
|
| 224 | + * @param string $type |
|
| 225 | + * @return int |
|
| 226 | + */ |
|
| 227 | + private function expectedSelectPartCountForType($type = '') { |
|
| 228 | + $type = $type === '' ? $this->type : $type; |
|
| 229 | + $types_count_map = array( |
|
| 230 | + self::TYPE_COMPLEX => 2, |
|
| 231 | + self::TYPE_STRUCTURED => 3 |
|
| 232 | + ); |
|
| 233 | + return isset($types_count_map[$type]) ? $types_count_map[$type] : 0; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Prepares the select statement part for for structured type selects. |
|
| 239 | + * @param array $select_parts |
|
| 240 | + * @param string $alias |
|
| 241 | + * @return string |
|
| 242 | + * @throws InvalidArgumentException |
|
| 243 | + */ |
|
| 244 | + private function assembleSelectStringWithOperator(array $select_parts, $alias) |
|
| 245 | + { |
|
| 246 | + $operator = strtoupper($select_parts[1]); |
|
| 247 | + //validate operator |
|
| 248 | + if (! in_array($operator, $this->valid_operators, true)) { |
|
| 249 | + throw new InvalidArgumentException( |
|
| 250 | + sprintf( |
|
| 251 | + esc_html__( |
|
| 252 | + 'An invalid operator has been provided (%1$s) for the column %2$s. Valid operators must be one of the following: %3$s.', |
|
| 253 | + 'event_espresso' |
|
| 254 | + ), |
|
| 255 | + $operator, |
|
| 256 | + $alias, |
|
| 257 | + implode(', ', $this->valid_operators) |
|
| 258 | + ) |
|
| 259 | + ); |
|
| 260 | + } |
|
| 261 | + return $operator . '(' . $select_parts[0] . ') AS ' . $alias; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * Return the datatype from the given select part. |
|
| 267 | + * Remember the select_part has already been validated on object instantiation. |
|
| 268 | + * @param array $select_part |
|
| 269 | + * @return string |
|
| 270 | + */ |
|
| 271 | + private function getDataTypeForSelectType(array $select_part) |
|
| 272 | + { |
|
| 273 | + switch ($this->type) { |
|
| 274 | + case self::TYPE_COMPLEX: |
|
| 275 | + return $select_part[1]; |
|
| 276 | + case self::TYPE_STRUCTURED: |
|
| 277 | + return $select_part[2]; |
|
| 278 | + default: |
|
| 279 | + return ''; |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Returns the original select array sent into the VO. |
|
| 286 | + * @return array |
|
| 287 | + */ |
|
| 288 | + public function originalSelects() |
|
| 289 | + { |
|
| 290 | + return $this->original_selects; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Returns the final assembled select expression derived from the incoming select array. |
|
| 296 | + * @return string |
|
| 297 | + */ |
|
| 298 | + public function columnsToSelectExpression() |
|
| 299 | + { |
|
| 300 | + return $this->columns_to_select_expression; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Returns all the column aliases derived from the incoming select array. |
|
| 306 | + * @return array |
|
| 307 | + */ |
|
| 308 | + public function columnAliases() |
|
| 309 | + { |
|
| 310 | + return $this->column_aliases_in_select; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * Returns the enum type for the incoming select array. |
|
| 316 | + * @return string |
|
| 317 | + */ |
|
| 318 | + public function type() |
|
| 319 | + { |
|
| 320 | + return $this->type; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * Return the datatype for the given column_alias |
|
| 327 | + * @param string $column_alias |
|
| 328 | + * @return string (if there's no data type we return string as the default). |
|
| 329 | + */ |
|
| 330 | + public function getDataTypeForAlias($column_alias) |
|
| 331 | + { |
|
| 332 | + if (isset($this->original_selects[$column_alias]) |
|
| 333 | + && in_array($column_alias, $this->columnAliases(), true) |
|
| 334 | + ) { |
|
| 335 | + return $this->getDataTypeForSelectType($this->original_selects[$column_alias]); |
|
| 336 | + } |
|
| 337 | + return '%s'; |
|
| 338 | + } |
|
| 339 | 339 | } |
@@ -101,11 +101,11 @@ discard block |
||
| 101 | 101 | //let's ensure all keys are ints |
| 102 | 102 | $invalid_keys = array_filter( |
| 103 | 103 | array_keys($selects), |
| 104 | - function ($value) { |
|
| 104 | + function($value) { |
|
| 105 | 105 | return ! is_int($value); |
| 106 | 106 | } |
| 107 | 107 | ); |
| 108 | - if (! empty($invalid_keys)) { |
|
| 108 | + if ( ! empty($invalid_keys)) { |
|
| 109 | 109 | throw new InvalidArgumentException( |
| 110 | 110 | sprintf( |
| 111 | 111 | esc_html__( |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | //the first array element off. |
| 124 | 124 | $first_element = reset($selects); |
| 125 | 125 | |
| 126 | - if (! is_array($first_element)) { |
|
| 126 | + if ( ! is_array($first_element)) { |
|
| 127 | 127 | throw new InvalidArgumentException( |
| 128 | 128 | sprintf( |
| 129 | 129 | esc_html__( |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | $data_type = $this->type === self::TYPE_COMPLEX ? $select_parts[1] : ''; |
| 203 | 203 | $data_type = $this->type === self::TYPE_STRUCTURED ? $select_parts[2] : $data_type; |
| 204 | 204 | |
| 205 | - if (! in_array($data_type, $valid_data_types, true)) { |
|
| 205 | + if ( ! in_array($data_type, $valid_data_types, true)) { |
|
| 206 | 206 | throw new InvalidArgumentException( |
| 207 | 207 | sprintf( |
| 208 | 208 | esc_html__( |
@@ -245,7 +245,7 @@ discard block |
||
| 245 | 245 | { |
| 246 | 246 | $operator = strtoupper($select_parts[1]); |
| 247 | 247 | //validate operator |
| 248 | - if (! in_array($operator, $this->valid_operators, true)) { |
|
| 248 | + if ( ! in_array($operator, $this->valid_operators, true)) { |
|
| 249 | 249 | throw new InvalidArgumentException( |
| 250 | 250 | sprintf( |
| 251 | 251 | esc_html__( |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | ) |
| 259 | 259 | ); |
| 260 | 260 | } |
| 261 | - return $operator . '(' . $select_parts[0] . ') AS ' . $alias; |
|
| 261 | + return $operator.'('.$select_parts[0].') AS '.$alias; |
|
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | |
@@ -303,7 +303,7 @@ discard block |
||
| 303 | 303 | * ATT_address column |
| 304 | 304 | * |
| 305 | 305 | * @param EE_Attendee $attendee |
| 306 | - * @return mixed |
|
| 306 | + * @return string |
|
| 307 | 307 | * @throws EE_Error |
| 308 | 308 | */ |
| 309 | 309 | public function column_ATT_address(EE_Attendee $attendee) |
@@ -316,7 +316,7 @@ discard block |
||
| 316 | 316 | * ATT_city column |
| 317 | 317 | * |
| 318 | 318 | * @param EE_Attendee $attendee |
| 319 | - * @return mixed |
|
| 319 | + * @return string |
|
| 320 | 320 | * @throws EE_Error |
| 321 | 321 | */ |
| 322 | 322 | public function column_ATT_city(EE_Attendee $attendee) |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | * Phone Number column |
| 370 | 370 | * |
| 371 | 371 | * @param EE_Attendee $attendee |
| 372 | - * @return mixed |
|
| 372 | + * @return string |
|
| 373 | 373 | * @throws EE_Error |
| 374 | 374 | */ |
| 375 | 375 | public function column_ATT_phone(EE_Attendee $attendee) |
@@ -15,365 +15,365 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class EE_Attendee_Contact_List_Table extends EE_Admin_List_Table |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Initial setup of data (called by parent). |
|
| 20 | - */ |
|
| 21 | - protected function _setup_data() |
|
| 22 | - { |
|
| 23 | - $this->_data = $this->_view !== 'trash' |
|
| 24 | - ? $this->_admin_page->get_attendees($this->_per_page) |
|
| 25 | - : $this->_admin_page->get_attendees($this->_per_page, false, true); |
|
| 26 | - $this->_all_data_count = $this->_view !== 'trash' |
|
| 27 | - ? $this->_admin_page->get_attendees($this->_per_page, true) |
|
| 28 | - : $this->_admin_page->get_attendees($this->_per_page, true, true); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Initial setup of properties. |
|
| 34 | - */ |
|
| 35 | - protected function _set_properties() |
|
| 36 | - { |
|
| 37 | - $this->_wp_list_args = array( |
|
| 38 | - 'singular' => esc_html__('attendee', 'event_espresso'), |
|
| 39 | - 'plural' => esc_html__('attendees', 'event_espresso'), |
|
| 40 | - 'ajax' => true, |
|
| 41 | - 'screen' => $this->_admin_page->get_current_screen()->id, |
|
| 42 | - ); |
|
| 43 | - |
|
| 44 | - $this->_columns = array( |
|
| 45 | - 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text |
|
| 46 | - 'ATT_ID' => esc_html__('ID', 'event_espresso'), |
|
| 47 | - 'ATT_fname' => esc_html__('First Name', 'event_espresso'), |
|
| 48 | - 'ATT_lname' => esc_html__('Last Name', 'event_espresso'), |
|
| 49 | - 'ATT_email' => esc_html__('Email Address', 'event_espresso'), |
|
| 50 | - 'Registration_Count' => esc_html__('# Registrations', 'event_espresso'), |
|
| 51 | - 'ATT_phone' => esc_html__('Phone', 'event_espresso'), |
|
| 52 | - 'ATT_address' => esc_html__('Address', 'event_espresso'), |
|
| 53 | - 'ATT_city' => esc_html__('City', 'event_espresso'), |
|
| 54 | - 'STA_ID' => esc_html__('State/Province', 'event_espresso'), |
|
| 55 | - 'CNT_ISO' => esc_html__('Country', 'event_espresso'), |
|
| 56 | - ); |
|
| 57 | - |
|
| 58 | - $this->_sortable_columns = array( |
|
| 59 | - 'ATT_ID' => array('ATT_ID' => false), |
|
| 60 | - 'ATT_lname' => array('ATT_lname' => true), //true means its already sorted |
|
| 61 | - 'ATT_fname' => array('ATT_fname' => false), |
|
| 62 | - 'ATT_email' => array('ATT_email' => false), |
|
| 63 | - 'Registration_Count' => array('Registration_Count' => false), |
|
| 64 | - 'ATT_city' => array('ATT_city' => false), |
|
| 65 | - 'STA_ID' => array('STA_ID' => false), |
|
| 66 | - 'CNT_ISO' => array('CNT_ISO' => false), |
|
| 67 | - ); |
|
| 68 | - |
|
| 69 | - $this->_hidden_columns = array( |
|
| 70 | - 'ATT_phone', |
|
| 71 | - 'ATT_address', |
|
| 72 | - 'ATT_city', |
|
| 73 | - 'STA_ID', |
|
| 74 | - 'CNT_ISO' |
|
| 75 | - ); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Initial setup of filters |
|
| 81 | - * @return array |
|
| 82 | - */ |
|
| 83 | - protected function _get_table_filters() |
|
| 84 | - { |
|
| 85 | - return array(); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Initial setup of counts for views |
|
| 91 | - * @throws InvalidArgumentException |
|
| 92 | - * @throws InvalidDataTypeException |
|
| 93 | - * @throws InvalidInterfaceException |
|
| 94 | - */ |
|
| 95 | - protected function _add_view_counts() |
|
| 96 | - { |
|
| 97 | - $this->_views['in_use']['count'] = $this->_admin_page->get_attendees($this->_per_page, true); |
|
| 98 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
| 99 | - 'ee_delete_contacts', |
|
| 100 | - 'espresso_registrations_delete_registration' |
|
| 101 | - )) { |
|
| 102 | - $this->_views['trash']['count'] = $this->_admin_page->get_attendees($this->_per_page, true, true); |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Get count of attendees. |
|
| 109 | - * @return int |
|
| 110 | - * @throws EE_Error |
|
| 111 | - * @throws InvalidArgumentException |
|
| 112 | - * @throws InvalidDataTypeException |
|
| 113 | - * @throws InvalidInterfaceException |
|
| 114 | - */ |
|
| 115 | - protected function _get_attendees_count() |
|
| 116 | - { |
|
| 117 | - return EEM_Attendee::instance()->count(); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Checkbox column |
|
| 123 | - * |
|
| 124 | - * @param EE_Attendee $attendee Unable to typehint this method because overrides parent. |
|
| 125 | - * @return string |
|
| 126 | - * @throws EE_Error |
|
| 127 | - */ |
|
| 128 | - public function column_cb($attendee) |
|
| 129 | - { |
|
| 130 | - if (! $attendee instanceof EE_Attendee) { |
|
| 131 | - return ''; |
|
| 132 | - } |
|
| 133 | - return sprintf( |
|
| 134 | - '<input type="checkbox" name="checkbox[%1$s]" value="%1$s" />', |
|
| 135 | - $attendee->ID() |
|
| 136 | - ); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * ATT_ID column |
|
| 142 | - * |
|
| 143 | - * @param EE_Attendee $attendee |
|
| 144 | - * @return string |
|
| 145 | - * @throws EE_Error |
|
| 146 | - */ |
|
| 147 | - public function column_ATT_ID(EE_Attendee $attendee) |
|
| 148 | - { |
|
| 149 | - $content = $attendee->ID(); |
|
| 150 | - $attendee_name = $attendee instanceof EE_Attendee ? $attendee->full_name() : ''; |
|
| 151 | - $content .= ' <span class="show-on-mobile-view-only">' . $attendee_name . '</span>'; |
|
| 152 | - return $content; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * ATT_lname column |
|
| 158 | - * |
|
| 159 | - * @param EE_Attendee $attendee |
|
| 160 | - * @return string |
|
| 161 | - * @throws InvalidArgumentException |
|
| 162 | - * @throws InvalidDataTypeException |
|
| 163 | - * @throws InvalidInterfaceException |
|
| 164 | - * @throws EE_Error |
|
| 165 | - */ |
|
| 166 | - public function column_ATT_lname(EE_Attendee $attendee) |
|
| 167 | - { |
|
| 168 | - // edit attendee link |
|
| 169 | - $edit_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
| 170 | - array( |
|
| 171 | - 'action' => 'edit_attendee', |
|
| 172 | - 'post' => $attendee->ID(), |
|
| 173 | - ), |
|
| 174 | - REG_ADMIN_URL |
|
| 175 | - ); |
|
| 176 | - $name_link = EE_Registry::instance()->CAP->current_user_can( |
|
| 177 | - 'ee_edit_contacts', |
|
| 178 | - 'espresso_registrations_edit_attendee' |
|
| 179 | - ) |
|
| 180 | - ? '<a href="' . $edit_lnk_url . '" title="' |
|
| 181 | - . esc_attr__('Edit Contact', 'event_espresso') . '">' |
|
| 182 | - . $attendee->lname() . '</a>' |
|
| 183 | - : $attendee->lname(); |
|
| 184 | - return $name_link; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * ATT_fname column |
|
| 190 | - * |
|
| 191 | - * @param EE_Attendee $attendee |
|
| 192 | - * @return string |
|
| 193 | - * @throws InvalidArgumentException |
|
| 194 | - * @throws InvalidDataTypeException |
|
| 195 | - * @throws InvalidInterfaceException |
|
| 196 | - * @throws EE_Error |
|
| 197 | - */ |
|
| 198 | - public function column_ATT_fname(EE_Attendee $attendee) |
|
| 199 | - { |
|
| 200 | - //Build row actions |
|
| 201 | - $actions = array(); |
|
| 202 | - // edit attendee link |
|
| 203 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
| 204 | - 'ee_edit_contacts', |
|
| 205 | - 'espresso_registrations_edit_attendee' |
|
| 206 | - )) { |
|
| 207 | - $edit_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
| 208 | - array( |
|
| 209 | - 'action' => 'edit_attendee', |
|
| 210 | - 'post' => $attendee->ID(), |
|
| 211 | - ), |
|
| 212 | - REG_ADMIN_URL |
|
| 213 | - ); |
|
| 214 | - $actions['edit'] = '<a href="' . $edit_lnk_url . '" title="' |
|
| 215 | - . esc_attr__('Edit Contact', 'event_espresso') . '">' |
|
| 216 | - . esc_html__('Edit', 'event_espresso') . '</a>'; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - if ($this->_view === 'in_use') { |
|
| 220 | - // trash attendee link |
|
| 221 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
| 222 | - 'ee_delete_contacts', |
|
| 223 | - 'espresso_registrations_trash_attendees' |
|
| 224 | - )) { |
|
| 225 | - $trash_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
| 226 | - array( |
|
| 227 | - 'action' => 'trash_attendee', |
|
| 228 | - 'ATT_ID' => $attendee->ID(), |
|
| 229 | - ), |
|
| 230 | - REG_ADMIN_URL |
|
| 231 | - ); |
|
| 232 | - $actions['trash'] = '<a href="' . $trash_lnk_url . '" title="' |
|
| 233 | - . esc_attr__('Move Contact to Trash', 'event_espresso') |
|
| 234 | - . '">' . esc_html__('Trash', 'event_espresso') . '</a>'; |
|
| 235 | - } |
|
| 236 | - } else { |
|
| 237 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
| 238 | - 'ee_delete_contacts', |
|
| 239 | - 'espresso_registrations_restore_attendees' |
|
| 240 | - )) { |
|
| 241 | - // restore attendee link |
|
| 242 | - $restore_lnk_url = EE_Admin_Page::add_query_args_and_nonce(array( |
|
| 243 | - 'action' => 'restore_attendees', |
|
| 244 | - 'ATT_ID' => $attendee->ID(), |
|
| 245 | - ), REG_ADMIN_URL); |
|
| 246 | - $actions['restore'] = '<a href="' . $restore_lnk_url . '" title="' |
|
| 247 | - . esc_attr__('Restore Contact', 'event_espresso') . '">' |
|
| 248 | - . esc_html__('Restore', 'event_espresso') . '</a>'; |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - $edit_lnk_url = EE_Admin_Page::add_query_args_and_nonce(array( |
|
| 253 | - 'action' => 'edit_attendee', |
|
| 254 | - 'post' => $attendee->ID(), |
|
| 255 | - ), REG_ADMIN_URL); |
|
| 256 | - $name_link = EE_Registry::instance()->CAP->current_user_can( |
|
| 257 | - 'ee_edit_contacts', |
|
| 258 | - 'espresso_registrations_edit_attendee' |
|
| 259 | - ) |
|
| 260 | - ? '<a href="' . $edit_lnk_url . '" title="' |
|
| 261 | - . esc_attr__('Edit Contact', 'event_espresso') . '">' . $attendee->fname() . '</a>' |
|
| 262 | - : $attendee->fname(); |
|
| 263 | - |
|
| 264 | - //Return the name contents |
|
| 265 | - return sprintf('%1$s %2$s', $name_link, $this->row_actions($actions)); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * Email Column |
|
| 271 | - * |
|
| 272 | - * @param EE_Attendee $attendee |
|
| 273 | - * @return string |
|
| 274 | - * @throws EE_Error |
|
| 275 | - */ |
|
| 276 | - public function column_ATT_email(EE_Attendee $attendee) |
|
| 277 | - { |
|
| 278 | - return '<a href="mailto:' . $attendee->email() . '">' . $attendee->email() . '</a>'; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Column displaying count of registrations attached to Attendee. |
|
| 284 | - * |
|
| 285 | - * @param EE_Attendee $attendee |
|
| 286 | - * @return string |
|
| 287 | - * @throws EE_Error |
|
| 288 | - */ |
|
| 289 | - public function column_Registration_Count(EE_Attendee $attendee) |
|
| 290 | - { |
|
| 291 | - $link = EEH_URL::add_query_args_and_nonce( |
|
| 292 | - array( |
|
| 293 | - 'action' => 'default', |
|
| 294 | - 'ATT_ID' => $attendee->ID() |
|
| 295 | - ), |
|
| 296 | - REG_ADMIN_URL |
|
| 297 | - ); |
|
| 298 | - return '<a href="' . $link . '">' . $attendee->getCustomSelect('Registration_Count') . '</a>'; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * ATT_address column |
|
| 304 | - * |
|
| 305 | - * @param EE_Attendee $attendee |
|
| 306 | - * @return mixed |
|
| 307 | - * @throws EE_Error |
|
| 308 | - */ |
|
| 309 | - public function column_ATT_address(EE_Attendee $attendee) |
|
| 310 | - { |
|
| 311 | - return $attendee->address(); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * ATT_city column |
|
| 317 | - * |
|
| 318 | - * @param EE_Attendee $attendee |
|
| 319 | - * @return mixed |
|
| 320 | - * @throws EE_Error |
|
| 321 | - */ |
|
| 322 | - public function column_ATT_city(EE_Attendee $attendee) |
|
| 323 | - { |
|
| 324 | - return $attendee->city(); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * State Column |
|
| 330 | - * |
|
| 331 | - * @param EE_Attendee $attendee |
|
| 332 | - * @return string |
|
| 333 | - * @throws EE_Error |
|
| 334 | - * @throws InvalidArgumentException |
|
| 335 | - * @throws InvalidDataTypeException |
|
| 336 | - * @throws InvalidInterfaceException |
|
| 337 | - */ |
|
| 338 | - public function column_STA_ID(EE_Attendee $attendee) |
|
| 339 | - { |
|
| 340 | - $states = EEM_State::instance()->get_all_states(); |
|
| 341 | - $state = isset($states[$attendee->state_ID()]) |
|
| 342 | - ? $states[$attendee->state_ID()]->get('STA_name') |
|
| 343 | - : $attendee->state_ID(); |
|
| 344 | - return ! is_numeric($state) ? $state : ''; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Country Column |
|
| 350 | - * |
|
| 351 | - * @param EE_Attendee $attendee |
|
| 352 | - * @return string |
|
| 353 | - * @throws EE_Error |
|
| 354 | - * @throws InvalidArgumentException |
|
| 355 | - * @throws InvalidDataTypeException |
|
| 356 | - * @throws InvalidInterfaceException |
|
| 357 | - */ |
|
| 358 | - public function column_CNT_ISO(EE_Attendee $attendee) |
|
| 359 | - { |
|
| 360 | - $countries = EEM_Country::instance()->get_all_countries(); |
|
| 361 | - $country = isset($countries[$attendee->country_ID()]) |
|
| 362 | - ? $countries[$attendee->country_ID()]->get('CNT_name') |
|
| 363 | - : $attendee->country_ID(); |
|
| 364 | - return ! is_numeric($country) ? $country : ''; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Phone Number column |
|
| 370 | - * |
|
| 371 | - * @param EE_Attendee $attendee |
|
| 372 | - * @return mixed |
|
| 373 | - * @throws EE_Error |
|
| 374 | - */ |
|
| 375 | - public function column_ATT_phone(EE_Attendee $attendee) |
|
| 376 | - { |
|
| 377 | - return $attendee->phone(); |
|
| 378 | - } |
|
| 18 | + /** |
|
| 19 | + * Initial setup of data (called by parent). |
|
| 20 | + */ |
|
| 21 | + protected function _setup_data() |
|
| 22 | + { |
|
| 23 | + $this->_data = $this->_view !== 'trash' |
|
| 24 | + ? $this->_admin_page->get_attendees($this->_per_page) |
|
| 25 | + : $this->_admin_page->get_attendees($this->_per_page, false, true); |
|
| 26 | + $this->_all_data_count = $this->_view !== 'trash' |
|
| 27 | + ? $this->_admin_page->get_attendees($this->_per_page, true) |
|
| 28 | + : $this->_admin_page->get_attendees($this->_per_page, true, true); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Initial setup of properties. |
|
| 34 | + */ |
|
| 35 | + protected function _set_properties() |
|
| 36 | + { |
|
| 37 | + $this->_wp_list_args = array( |
|
| 38 | + 'singular' => esc_html__('attendee', 'event_espresso'), |
|
| 39 | + 'plural' => esc_html__('attendees', 'event_espresso'), |
|
| 40 | + 'ajax' => true, |
|
| 41 | + 'screen' => $this->_admin_page->get_current_screen()->id, |
|
| 42 | + ); |
|
| 43 | + |
|
| 44 | + $this->_columns = array( |
|
| 45 | + 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text |
|
| 46 | + 'ATT_ID' => esc_html__('ID', 'event_espresso'), |
|
| 47 | + 'ATT_fname' => esc_html__('First Name', 'event_espresso'), |
|
| 48 | + 'ATT_lname' => esc_html__('Last Name', 'event_espresso'), |
|
| 49 | + 'ATT_email' => esc_html__('Email Address', 'event_espresso'), |
|
| 50 | + 'Registration_Count' => esc_html__('# Registrations', 'event_espresso'), |
|
| 51 | + 'ATT_phone' => esc_html__('Phone', 'event_espresso'), |
|
| 52 | + 'ATT_address' => esc_html__('Address', 'event_espresso'), |
|
| 53 | + 'ATT_city' => esc_html__('City', 'event_espresso'), |
|
| 54 | + 'STA_ID' => esc_html__('State/Province', 'event_espresso'), |
|
| 55 | + 'CNT_ISO' => esc_html__('Country', 'event_espresso'), |
|
| 56 | + ); |
|
| 57 | + |
|
| 58 | + $this->_sortable_columns = array( |
|
| 59 | + 'ATT_ID' => array('ATT_ID' => false), |
|
| 60 | + 'ATT_lname' => array('ATT_lname' => true), //true means its already sorted |
|
| 61 | + 'ATT_fname' => array('ATT_fname' => false), |
|
| 62 | + 'ATT_email' => array('ATT_email' => false), |
|
| 63 | + 'Registration_Count' => array('Registration_Count' => false), |
|
| 64 | + 'ATT_city' => array('ATT_city' => false), |
|
| 65 | + 'STA_ID' => array('STA_ID' => false), |
|
| 66 | + 'CNT_ISO' => array('CNT_ISO' => false), |
|
| 67 | + ); |
|
| 68 | + |
|
| 69 | + $this->_hidden_columns = array( |
|
| 70 | + 'ATT_phone', |
|
| 71 | + 'ATT_address', |
|
| 72 | + 'ATT_city', |
|
| 73 | + 'STA_ID', |
|
| 74 | + 'CNT_ISO' |
|
| 75 | + ); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Initial setup of filters |
|
| 81 | + * @return array |
|
| 82 | + */ |
|
| 83 | + protected function _get_table_filters() |
|
| 84 | + { |
|
| 85 | + return array(); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Initial setup of counts for views |
|
| 91 | + * @throws InvalidArgumentException |
|
| 92 | + * @throws InvalidDataTypeException |
|
| 93 | + * @throws InvalidInterfaceException |
|
| 94 | + */ |
|
| 95 | + protected function _add_view_counts() |
|
| 96 | + { |
|
| 97 | + $this->_views['in_use']['count'] = $this->_admin_page->get_attendees($this->_per_page, true); |
|
| 98 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
| 99 | + 'ee_delete_contacts', |
|
| 100 | + 'espresso_registrations_delete_registration' |
|
| 101 | + )) { |
|
| 102 | + $this->_views['trash']['count'] = $this->_admin_page->get_attendees($this->_per_page, true, true); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Get count of attendees. |
|
| 109 | + * @return int |
|
| 110 | + * @throws EE_Error |
|
| 111 | + * @throws InvalidArgumentException |
|
| 112 | + * @throws InvalidDataTypeException |
|
| 113 | + * @throws InvalidInterfaceException |
|
| 114 | + */ |
|
| 115 | + protected function _get_attendees_count() |
|
| 116 | + { |
|
| 117 | + return EEM_Attendee::instance()->count(); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Checkbox column |
|
| 123 | + * |
|
| 124 | + * @param EE_Attendee $attendee Unable to typehint this method because overrides parent. |
|
| 125 | + * @return string |
|
| 126 | + * @throws EE_Error |
|
| 127 | + */ |
|
| 128 | + public function column_cb($attendee) |
|
| 129 | + { |
|
| 130 | + if (! $attendee instanceof EE_Attendee) { |
|
| 131 | + return ''; |
|
| 132 | + } |
|
| 133 | + return sprintf( |
|
| 134 | + '<input type="checkbox" name="checkbox[%1$s]" value="%1$s" />', |
|
| 135 | + $attendee->ID() |
|
| 136 | + ); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * ATT_ID column |
|
| 142 | + * |
|
| 143 | + * @param EE_Attendee $attendee |
|
| 144 | + * @return string |
|
| 145 | + * @throws EE_Error |
|
| 146 | + */ |
|
| 147 | + public function column_ATT_ID(EE_Attendee $attendee) |
|
| 148 | + { |
|
| 149 | + $content = $attendee->ID(); |
|
| 150 | + $attendee_name = $attendee instanceof EE_Attendee ? $attendee->full_name() : ''; |
|
| 151 | + $content .= ' <span class="show-on-mobile-view-only">' . $attendee_name . '</span>'; |
|
| 152 | + return $content; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * ATT_lname column |
|
| 158 | + * |
|
| 159 | + * @param EE_Attendee $attendee |
|
| 160 | + * @return string |
|
| 161 | + * @throws InvalidArgumentException |
|
| 162 | + * @throws InvalidDataTypeException |
|
| 163 | + * @throws InvalidInterfaceException |
|
| 164 | + * @throws EE_Error |
|
| 165 | + */ |
|
| 166 | + public function column_ATT_lname(EE_Attendee $attendee) |
|
| 167 | + { |
|
| 168 | + // edit attendee link |
|
| 169 | + $edit_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
| 170 | + array( |
|
| 171 | + 'action' => 'edit_attendee', |
|
| 172 | + 'post' => $attendee->ID(), |
|
| 173 | + ), |
|
| 174 | + REG_ADMIN_URL |
|
| 175 | + ); |
|
| 176 | + $name_link = EE_Registry::instance()->CAP->current_user_can( |
|
| 177 | + 'ee_edit_contacts', |
|
| 178 | + 'espresso_registrations_edit_attendee' |
|
| 179 | + ) |
|
| 180 | + ? '<a href="' . $edit_lnk_url . '" title="' |
|
| 181 | + . esc_attr__('Edit Contact', 'event_espresso') . '">' |
|
| 182 | + . $attendee->lname() . '</a>' |
|
| 183 | + : $attendee->lname(); |
|
| 184 | + return $name_link; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * ATT_fname column |
|
| 190 | + * |
|
| 191 | + * @param EE_Attendee $attendee |
|
| 192 | + * @return string |
|
| 193 | + * @throws InvalidArgumentException |
|
| 194 | + * @throws InvalidDataTypeException |
|
| 195 | + * @throws InvalidInterfaceException |
|
| 196 | + * @throws EE_Error |
|
| 197 | + */ |
|
| 198 | + public function column_ATT_fname(EE_Attendee $attendee) |
|
| 199 | + { |
|
| 200 | + //Build row actions |
|
| 201 | + $actions = array(); |
|
| 202 | + // edit attendee link |
|
| 203 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
| 204 | + 'ee_edit_contacts', |
|
| 205 | + 'espresso_registrations_edit_attendee' |
|
| 206 | + )) { |
|
| 207 | + $edit_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
| 208 | + array( |
|
| 209 | + 'action' => 'edit_attendee', |
|
| 210 | + 'post' => $attendee->ID(), |
|
| 211 | + ), |
|
| 212 | + REG_ADMIN_URL |
|
| 213 | + ); |
|
| 214 | + $actions['edit'] = '<a href="' . $edit_lnk_url . '" title="' |
|
| 215 | + . esc_attr__('Edit Contact', 'event_espresso') . '">' |
|
| 216 | + . esc_html__('Edit', 'event_espresso') . '</a>'; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + if ($this->_view === 'in_use') { |
|
| 220 | + // trash attendee link |
|
| 221 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
| 222 | + 'ee_delete_contacts', |
|
| 223 | + 'espresso_registrations_trash_attendees' |
|
| 224 | + )) { |
|
| 225 | + $trash_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
| 226 | + array( |
|
| 227 | + 'action' => 'trash_attendee', |
|
| 228 | + 'ATT_ID' => $attendee->ID(), |
|
| 229 | + ), |
|
| 230 | + REG_ADMIN_URL |
|
| 231 | + ); |
|
| 232 | + $actions['trash'] = '<a href="' . $trash_lnk_url . '" title="' |
|
| 233 | + . esc_attr__('Move Contact to Trash', 'event_espresso') |
|
| 234 | + . '">' . esc_html__('Trash', 'event_espresso') . '</a>'; |
|
| 235 | + } |
|
| 236 | + } else { |
|
| 237 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
| 238 | + 'ee_delete_contacts', |
|
| 239 | + 'espresso_registrations_restore_attendees' |
|
| 240 | + )) { |
|
| 241 | + // restore attendee link |
|
| 242 | + $restore_lnk_url = EE_Admin_Page::add_query_args_and_nonce(array( |
|
| 243 | + 'action' => 'restore_attendees', |
|
| 244 | + 'ATT_ID' => $attendee->ID(), |
|
| 245 | + ), REG_ADMIN_URL); |
|
| 246 | + $actions['restore'] = '<a href="' . $restore_lnk_url . '" title="' |
|
| 247 | + . esc_attr__('Restore Contact', 'event_espresso') . '">' |
|
| 248 | + . esc_html__('Restore', 'event_espresso') . '</a>'; |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + $edit_lnk_url = EE_Admin_Page::add_query_args_and_nonce(array( |
|
| 253 | + 'action' => 'edit_attendee', |
|
| 254 | + 'post' => $attendee->ID(), |
|
| 255 | + ), REG_ADMIN_URL); |
|
| 256 | + $name_link = EE_Registry::instance()->CAP->current_user_can( |
|
| 257 | + 'ee_edit_contacts', |
|
| 258 | + 'espresso_registrations_edit_attendee' |
|
| 259 | + ) |
|
| 260 | + ? '<a href="' . $edit_lnk_url . '" title="' |
|
| 261 | + . esc_attr__('Edit Contact', 'event_espresso') . '">' . $attendee->fname() . '</a>' |
|
| 262 | + : $attendee->fname(); |
|
| 263 | + |
|
| 264 | + //Return the name contents |
|
| 265 | + return sprintf('%1$s %2$s', $name_link, $this->row_actions($actions)); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * Email Column |
|
| 271 | + * |
|
| 272 | + * @param EE_Attendee $attendee |
|
| 273 | + * @return string |
|
| 274 | + * @throws EE_Error |
|
| 275 | + */ |
|
| 276 | + public function column_ATT_email(EE_Attendee $attendee) |
|
| 277 | + { |
|
| 278 | + return '<a href="mailto:' . $attendee->email() . '">' . $attendee->email() . '</a>'; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Column displaying count of registrations attached to Attendee. |
|
| 284 | + * |
|
| 285 | + * @param EE_Attendee $attendee |
|
| 286 | + * @return string |
|
| 287 | + * @throws EE_Error |
|
| 288 | + */ |
|
| 289 | + public function column_Registration_Count(EE_Attendee $attendee) |
|
| 290 | + { |
|
| 291 | + $link = EEH_URL::add_query_args_and_nonce( |
|
| 292 | + array( |
|
| 293 | + 'action' => 'default', |
|
| 294 | + 'ATT_ID' => $attendee->ID() |
|
| 295 | + ), |
|
| 296 | + REG_ADMIN_URL |
|
| 297 | + ); |
|
| 298 | + return '<a href="' . $link . '">' . $attendee->getCustomSelect('Registration_Count') . '</a>'; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * ATT_address column |
|
| 304 | + * |
|
| 305 | + * @param EE_Attendee $attendee |
|
| 306 | + * @return mixed |
|
| 307 | + * @throws EE_Error |
|
| 308 | + */ |
|
| 309 | + public function column_ATT_address(EE_Attendee $attendee) |
|
| 310 | + { |
|
| 311 | + return $attendee->address(); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * ATT_city column |
|
| 317 | + * |
|
| 318 | + * @param EE_Attendee $attendee |
|
| 319 | + * @return mixed |
|
| 320 | + * @throws EE_Error |
|
| 321 | + */ |
|
| 322 | + public function column_ATT_city(EE_Attendee $attendee) |
|
| 323 | + { |
|
| 324 | + return $attendee->city(); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * State Column |
|
| 330 | + * |
|
| 331 | + * @param EE_Attendee $attendee |
|
| 332 | + * @return string |
|
| 333 | + * @throws EE_Error |
|
| 334 | + * @throws InvalidArgumentException |
|
| 335 | + * @throws InvalidDataTypeException |
|
| 336 | + * @throws InvalidInterfaceException |
|
| 337 | + */ |
|
| 338 | + public function column_STA_ID(EE_Attendee $attendee) |
|
| 339 | + { |
|
| 340 | + $states = EEM_State::instance()->get_all_states(); |
|
| 341 | + $state = isset($states[$attendee->state_ID()]) |
|
| 342 | + ? $states[$attendee->state_ID()]->get('STA_name') |
|
| 343 | + : $attendee->state_ID(); |
|
| 344 | + return ! is_numeric($state) ? $state : ''; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Country Column |
|
| 350 | + * |
|
| 351 | + * @param EE_Attendee $attendee |
|
| 352 | + * @return string |
|
| 353 | + * @throws EE_Error |
|
| 354 | + * @throws InvalidArgumentException |
|
| 355 | + * @throws InvalidDataTypeException |
|
| 356 | + * @throws InvalidInterfaceException |
|
| 357 | + */ |
|
| 358 | + public function column_CNT_ISO(EE_Attendee $attendee) |
|
| 359 | + { |
|
| 360 | + $countries = EEM_Country::instance()->get_all_countries(); |
|
| 361 | + $country = isset($countries[$attendee->country_ID()]) |
|
| 362 | + ? $countries[$attendee->country_ID()]->get('CNT_name') |
|
| 363 | + : $attendee->country_ID(); |
|
| 364 | + return ! is_numeric($country) ? $country : ''; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Phone Number column |
|
| 370 | + * |
|
| 371 | + * @param EE_Attendee $attendee |
|
| 372 | + * @return mixed |
|
| 373 | + * @throws EE_Error |
|
| 374 | + */ |
|
| 375 | + public function column_ATT_phone(EE_Attendee $attendee) |
|
| 376 | + { |
|
| 377 | + return $attendee->phone(); |
|
| 378 | + } |
|
| 379 | 379 | } |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | */ |
| 128 | 128 | public function column_cb($attendee) |
| 129 | 129 | { |
| 130 | - if (! $attendee instanceof EE_Attendee) { |
|
| 130 | + if ( ! $attendee instanceof EE_Attendee) { |
|
| 131 | 131 | return ''; |
| 132 | 132 | } |
| 133 | 133 | return sprintf( |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | { |
| 149 | 149 | $content = $attendee->ID(); |
| 150 | 150 | $attendee_name = $attendee instanceof EE_Attendee ? $attendee->full_name() : ''; |
| 151 | - $content .= ' <span class="show-on-mobile-view-only">' . $attendee_name . '</span>'; |
|
| 151 | + $content .= ' <span class="show-on-mobile-view-only">'.$attendee_name.'</span>'; |
|
| 152 | 152 | return $content; |
| 153 | 153 | } |
| 154 | 154 | |
@@ -177,9 +177,9 @@ discard block |
||
| 177 | 177 | 'ee_edit_contacts', |
| 178 | 178 | 'espresso_registrations_edit_attendee' |
| 179 | 179 | ) |
| 180 | - ? '<a href="' . $edit_lnk_url . '" title="' |
|
| 181 | - . esc_attr__('Edit Contact', 'event_espresso') . '">' |
|
| 182 | - . $attendee->lname() . '</a>' |
|
| 180 | + ? '<a href="'.$edit_lnk_url.'" title="' |
|
| 181 | + . esc_attr__('Edit Contact', 'event_espresso').'">' |
|
| 182 | + . $attendee->lname().'</a>' |
|
| 183 | 183 | : $attendee->lname(); |
| 184 | 184 | return $name_link; |
| 185 | 185 | } |
@@ -211,9 +211,9 @@ discard block |
||
| 211 | 211 | ), |
| 212 | 212 | REG_ADMIN_URL |
| 213 | 213 | ); |
| 214 | - $actions['edit'] = '<a href="' . $edit_lnk_url . '" title="' |
|
| 215 | - . esc_attr__('Edit Contact', 'event_espresso') . '">' |
|
| 216 | - . esc_html__('Edit', 'event_espresso') . '</a>'; |
|
| 214 | + $actions['edit'] = '<a href="'.$edit_lnk_url.'" title="' |
|
| 215 | + . esc_attr__('Edit Contact', 'event_espresso').'">' |
|
| 216 | + . esc_html__('Edit', 'event_espresso').'</a>'; |
|
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | if ($this->_view === 'in_use') { |
@@ -229,9 +229,9 @@ discard block |
||
| 229 | 229 | ), |
| 230 | 230 | REG_ADMIN_URL |
| 231 | 231 | ); |
| 232 | - $actions['trash'] = '<a href="' . $trash_lnk_url . '" title="' |
|
| 232 | + $actions['trash'] = '<a href="'.$trash_lnk_url.'" title="' |
|
| 233 | 233 | . esc_attr__('Move Contact to Trash', 'event_espresso') |
| 234 | - . '">' . esc_html__('Trash', 'event_espresso') . '</a>'; |
|
| 234 | + . '">'.esc_html__('Trash', 'event_espresso').'</a>'; |
|
| 235 | 235 | } |
| 236 | 236 | } else { |
| 237 | 237 | if (EE_Registry::instance()->CAP->current_user_can( |
@@ -243,9 +243,9 @@ discard block |
||
| 243 | 243 | 'action' => 'restore_attendees', |
| 244 | 244 | 'ATT_ID' => $attendee->ID(), |
| 245 | 245 | ), REG_ADMIN_URL); |
| 246 | - $actions['restore'] = '<a href="' . $restore_lnk_url . '" title="' |
|
| 247 | - . esc_attr__('Restore Contact', 'event_espresso') . '">' |
|
| 248 | - . esc_html__('Restore', 'event_espresso') . '</a>'; |
|
| 246 | + $actions['restore'] = '<a href="'.$restore_lnk_url.'" title="' |
|
| 247 | + . esc_attr__('Restore Contact', 'event_espresso').'">' |
|
| 248 | + . esc_html__('Restore', 'event_espresso').'</a>'; |
|
| 249 | 249 | } |
| 250 | 250 | } |
| 251 | 251 | |
@@ -257,8 +257,8 @@ discard block |
||
| 257 | 257 | 'ee_edit_contacts', |
| 258 | 258 | 'espresso_registrations_edit_attendee' |
| 259 | 259 | ) |
| 260 | - ? '<a href="' . $edit_lnk_url . '" title="' |
|
| 261 | - . esc_attr__('Edit Contact', 'event_espresso') . '">' . $attendee->fname() . '</a>' |
|
| 260 | + ? '<a href="'.$edit_lnk_url.'" title="' |
|
| 261 | + . esc_attr__('Edit Contact', 'event_espresso').'">'.$attendee->fname().'</a>' |
|
| 262 | 262 | : $attendee->fname(); |
| 263 | 263 | |
| 264 | 264 | //Return the name contents |
@@ -275,7 +275,7 @@ discard block |
||
| 275 | 275 | */ |
| 276 | 276 | public function column_ATT_email(EE_Attendee $attendee) |
| 277 | 277 | { |
| 278 | - return '<a href="mailto:' . $attendee->email() . '">' . $attendee->email() . '</a>'; |
|
| 278 | + return '<a href="mailto:'.$attendee->email().'">'.$attendee->email().'</a>'; |
|
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | ), |
| 296 | 296 | REG_ADMIN_URL |
| 297 | 297 | ); |
| 298 | - return '<a href="' . $link . '">' . $attendee->getCustomSelect('Registration_Count') . '</a>'; |
|
| 298 | + return '<a href="'.$link.'">'.$attendee->getCustomSelect('Registration_Count').'</a>'; |
|
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | |
@@ -30,2339 +30,2339 @@ discard block |
||
| 30 | 30 | class Registrations_Admin_Page extends EE_Admin_Page_CPT |
| 31 | 31 | { |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @var EE_Registration |
|
| 35 | - */ |
|
| 36 | - private $_registration; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var EE_Event |
|
| 40 | - */ |
|
| 41 | - private $_reg_event; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var EE_Session |
|
| 45 | - */ |
|
| 46 | - private $_session; |
|
| 47 | - |
|
| 48 | - private static $_reg_status; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Form for displaying the custom questions for this registration. |
|
| 52 | - * This gets used a few times throughout the request so its best to cache it |
|
| 53 | - * |
|
| 54 | - * @var EE_Registration_Custom_Questions_Form |
|
| 55 | - */ |
|
| 56 | - protected $_reg_custom_questions_form = null; |
|
| 57 | - |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * constructor |
|
| 61 | - * |
|
| 62 | - * @Constructor |
|
| 63 | - * @access public |
|
| 64 | - * @param bool $routing |
|
| 65 | - * @return Registrations_Admin_Page |
|
| 66 | - */ |
|
| 67 | - public function __construct($routing = true) |
|
| 68 | - { |
|
| 69 | - parent::__construct($routing); |
|
| 70 | - add_action('wp_loaded', array($this, 'wp_loaded')); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - public function wp_loaded() |
|
| 75 | - { |
|
| 76 | - // when adding a new registration... |
|
| 77 | - if (isset($this->_req_data['action']) && $this->_req_data['action'] === 'new_registration') { |
|
| 78 | - EE_System::do_not_cache(); |
|
| 79 | - if (! isset($this->_req_data['processing_registration']) |
|
| 80 | - || absint($this->_req_data['processing_registration']) !== 1 |
|
| 81 | - ) { |
|
| 82 | - // and it's NOT the attendee information reg step |
|
| 83 | - // force cookie expiration by setting time to last week |
|
| 84 | - setcookie('ee_registration_added', 0, time() - WEEK_IN_SECONDS, '/'); |
|
| 85 | - // and update the global |
|
| 86 | - $_COOKIE['ee_registration_added'] = 0; |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - protected function _init_page_props() |
|
| 93 | - { |
|
| 94 | - $this->page_slug = REG_PG_SLUG; |
|
| 95 | - $this->_admin_base_url = REG_ADMIN_URL; |
|
| 96 | - $this->_admin_base_path = REG_ADMIN; |
|
| 97 | - $this->page_label = esc_html__('Registrations', 'event_espresso'); |
|
| 98 | - $this->_cpt_routes = array( |
|
| 99 | - 'add_new_attendee' => 'espresso_attendees', |
|
| 100 | - 'edit_attendee' => 'espresso_attendees', |
|
| 101 | - 'insert_attendee' => 'espresso_attendees', |
|
| 102 | - 'update_attendee' => 'espresso_attendees', |
|
| 103 | - ); |
|
| 104 | - $this->_cpt_model_names = array( |
|
| 105 | - 'add_new_attendee' => 'EEM_Attendee', |
|
| 106 | - 'edit_attendee' => 'EEM_Attendee', |
|
| 107 | - ); |
|
| 108 | - $this->_cpt_edit_routes = array( |
|
| 109 | - 'espresso_attendees' => 'edit_attendee', |
|
| 110 | - ); |
|
| 111 | - $this->_pagenow_map = array( |
|
| 112 | - 'add_new_attendee' => 'post-new.php', |
|
| 113 | - 'edit_attendee' => 'post.php', |
|
| 114 | - 'trash' => 'post.php', |
|
| 115 | - ); |
|
| 116 | - add_action('edit_form_after_title', array($this, 'after_title_form_fields'), 10); |
|
| 117 | - //add filters so that the comment urls don't take users to a confusing 404 page |
|
| 118 | - add_filter('get_comment_link', array($this, 'clear_comment_link'), 10, 3); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - |
|
| 122 | - public function clear_comment_link($link, $comment, $args) |
|
| 123 | - { |
|
| 124 | - //gotta make sure this only happens on this route |
|
| 125 | - $post_type = get_post_type($comment->comment_post_ID); |
|
| 126 | - if ($post_type === 'espresso_attendees') { |
|
| 127 | - return '#commentsdiv'; |
|
| 128 | - } |
|
| 129 | - return $link; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - |
|
| 133 | - protected function _ajax_hooks() |
|
| 134 | - { |
|
| 135 | - //todo: all hooks for registrations ajax goes in here |
|
| 136 | - add_action('wp_ajax_toggle_checkin_status', array($this, 'toggle_checkin_status')); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - |
|
| 140 | - protected function _define_page_props() |
|
| 141 | - { |
|
| 142 | - $this->_admin_page_title = $this->page_label; |
|
| 143 | - $this->_labels = array( |
|
| 144 | - 'buttons' => array( |
|
| 145 | - 'add-registrant' => esc_html__('Add New Registration', 'event_espresso'), |
|
| 146 | - 'add-attendee' => esc_html__('Add Contact', 'event_espresso'), |
|
| 147 | - 'edit' => esc_html__('Edit Contact', 'event_espresso'), |
|
| 148 | - 'report' => esc_html__("Event Registrations CSV Report", "event_espresso"), |
|
| 149 | - 'report_all' => esc_html__('All Registrations CSV Report', 'event_espresso'), |
|
| 150 | - 'report_filtered' => esc_html__('Filtered CSV Report', 'event_espresso'), |
|
| 151 | - 'contact_list_report' => esc_html__('Contact List Report', 'event_espresso'), |
|
| 152 | - 'contact_list_export' => esc_html__("Export Data", "event_espresso"), |
|
| 153 | - ), |
|
| 154 | - 'publishbox' => array( |
|
| 155 | - 'add_new_attendee' => esc_html__("Add Contact Record", 'event_espresso'), |
|
| 156 | - 'edit_attendee' => esc_html__("Update Contact Record", 'event_espresso'), |
|
| 157 | - ), |
|
| 158 | - 'hide_add_button_on_cpt_route' => array( |
|
| 159 | - 'edit_attendee' => true, |
|
| 160 | - ), |
|
| 161 | - ); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * grab url requests and route them |
|
| 167 | - * |
|
| 168 | - * @access private |
|
| 169 | - * @return void |
|
| 170 | - */ |
|
| 171 | - public function _set_page_routes() |
|
| 172 | - { |
|
| 173 | - $this->_get_registration_status_array(); |
|
| 174 | - $reg_id = ! empty($this->_req_data['_REG_ID']) && ! is_array($this->_req_data['_REG_ID']) |
|
| 175 | - ? $this->_req_data['_REG_ID'] : 0; |
|
| 176 | - $reg_id = empty($reg_id) && ! empty($this->_req_data['reg_status_change_form']['REG_ID']) |
|
| 177 | - ? $this->_req_data['reg_status_change_form']['REG_ID'] |
|
| 178 | - : $reg_id; |
|
| 179 | - $att_id = ! empty($this->_req_data['ATT_ID']) && ! is_array($this->_req_data['ATT_ID']) |
|
| 180 | - ? $this->_req_data['ATT_ID'] : 0; |
|
| 181 | - $att_id = ! empty($this->_req_data['post']) && ! is_array($this->_req_data['post']) |
|
| 182 | - ? $this->_req_data['post'] |
|
| 183 | - : $att_id; |
|
| 184 | - $this->_page_routes = array( |
|
| 185 | - 'default' => array( |
|
| 186 | - 'func' => '_registrations_overview_list_table', |
|
| 187 | - 'capability' => 'ee_read_registrations', |
|
| 188 | - ), |
|
| 189 | - 'view_registration' => array( |
|
| 190 | - 'func' => '_registration_details', |
|
| 191 | - 'capability' => 'ee_read_registration', |
|
| 192 | - 'obj_id' => $reg_id, |
|
| 193 | - ), |
|
| 194 | - 'edit_registration' => array( |
|
| 195 | - 'func' => '_update_attendee_registration_form', |
|
| 196 | - 'noheader' => true, |
|
| 197 | - 'headers_sent_route' => 'view_registration', |
|
| 198 | - 'capability' => 'ee_edit_registration', |
|
| 199 | - 'obj_id' => $reg_id, |
|
| 200 | - '_REG_ID' => $reg_id, |
|
| 201 | - ), |
|
| 202 | - 'trash_registrations' => array( |
|
| 203 | - 'func' => '_trash_or_restore_registrations', |
|
| 204 | - 'args' => array('trash' => true), |
|
| 205 | - 'noheader' => true, |
|
| 206 | - 'capability' => 'ee_delete_registrations', |
|
| 207 | - ), |
|
| 208 | - 'restore_registrations' => array( |
|
| 209 | - 'func' => '_trash_or_restore_registrations', |
|
| 210 | - 'args' => array('trash' => false), |
|
| 211 | - 'noheader' => true, |
|
| 212 | - 'capability' => 'ee_delete_registrations', |
|
| 213 | - ), |
|
| 214 | - 'delete_registrations' => array( |
|
| 215 | - 'func' => '_delete_registrations', |
|
| 216 | - 'noheader' => true, |
|
| 217 | - 'capability' => 'ee_delete_registrations', |
|
| 218 | - ), |
|
| 219 | - 'new_registration' => array( |
|
| 220 | - 'func' => 'new_registration', |
|
| 221 | - 'capability' => 'ee_edit_registrations', |
|
| 222 | - ), |
|
| 223 | - 'process_reg_step' => array( |
|
| 224 | - 'func' => 'process_reg_step', |
|
| 225 | - 'noheader' => true, |
|
| 226 | - 'capability' => 'ee_edit_registrations', |
|
| 227 | - ), |
|
| 228 | - 'redirect_to_txn' => array( |
|
| 229 | - 'func' => 'redirect_to_txn', |
|
| 230 | - 'noheader' => true, |
|
| 231 | - 'capability' => 'ee_edit_registrations', |
|
| 232 | - ), |
|
| 233 | - 'change_reg_status' => array( |
|
| 234 | - 'func' => '_change_reg_status', |
|
| 235 | - 'noheader' => true, |
|
| 236 | - 'capability' => 'ee_edit_registration', |
|
| 237 | - 'obj_id' => $reg_id, |
|
| 238 | - ), |
|
| 239 | - 'approve_registration' => array( |
|
| 240 | - 'func' => 'approve_registration', |
|
| 241 | - 'noheader' => true, |
|
| 242 | - 'capability' => 'ee_edit_registration', |
|
| 243 | - 'obj_id' => $reg_id, |
|
| 244 | - ), |
|
| 245 | - 'approve_and_notify_registration' => array( |
|
| 246 | - 'func' => 'approve_registration', |
|
| 247 | - 'noheader' => true, |
|
| 248 | - 'args' => array(true), |
|
| 249 | - 'capability' => 'ee_edit_registration', |
|
| 250 | - 'obj_id' => $reg_id, |
|
| 251 | - ), |
|
| 252 | - 'approve_registrations' => array( |
|
| 253 | - 'func' => 'bulk_action_on_registrations', |
|
| 254 | - 'noheader' => true, |
|
| 255 | - 'capability' => 'ee_edit_registrations', |
|
| 256 | - 'args' => array('approve') |
|
| 257 | - ), |
|
| 258 | - 'approve_and_notify_registrations' => array( |
|
| 259 | - 'func' => 'bulk_action_on_registrations', |
|
| 260 | - 'noheader' => true, |
|
| 261 | - 'capability' => 'ee_edit_registrations', |
|
| 262 | - 'args' => array('approve', true) |
|
| 263 | - ), |
|
| 264 | - 'decline_registration' => array( |
|
| 265 | - 'func' => 'decline_registration', |
|
| 266 | - 'noheader' => true, |
|
| 267 | - 'capability' => 'ee_edit_registration', |
|
| 268 | - 'obj_id' => $reg_id, |
|
| 269 | - ), |
|
| 270 | - 'decline_and_notify_registration' => array( |
|
| 271 | - 'func' => 'decline_registration', |
|
| 272 | - 'noheader' => true, |
|
| 273 | - 'args' => array(true), |
|
| 274 | - 'capability' => 'ee_edit_registration', |
|
| 275 | - 'obj_id' => $reg_id, |
|
| 276 | - ), |
|
| 277 | - 'decline_registrations' => array( |
|
| 278 | - 'func' => 'bulk_action_on_registrations', |
|
| 279 | - 'noheader' => true, |
|
| 280 | - 'capability' => 'ee_edit_registrations', |
|
| 281 | - 'args' => array('decline') |
|
| 282 | - ), |
|
| 283 | - 'decline_and_notify_registrations' => array( |
|
| 284 | - 'func' => 'bulk_action_on_registrations', |
|
| 285 | - 'noheader' => true, |
|
| 286 | - 'capability' => 'ee_edit_registrations', |
|
| 287 | - 'args' => array('decline', true) |
|
| 288 | - ), |
|
| 289 | - 'pending_registration' => array( |
|
| 290 | - 'func' => 'pending_registration', |
|
| 291 | - 'noheader' => true, |
|
| 292 | - 'capability' => 'ee_edit_registration', |
|
| 293 | - 'obj_id' => $reg_id, |
|
| 294 | - ), |
|
| 295 | - 'pending_and_notify_registration' => array( |
|
| 296 | - 'func' => 'pending_registration', |
|
| 297 | - 'noheader' => true, |
|
| 298 | - 'args' => array(true), |
|
| 299 | - 'capability' => 'ee_edit_registration', |
|
| 300 | - 'obj_id' => $reg_id, |
|
| 301 | - ), |
|
| 302 | - 'pending_registrations' => array( |
|
| 303 | - 'func' => 'bulk_action_on_registrations', |
|
| 304 | - 'noheader' => true, |
|
| 305 | - 'capability' => 'ee_edit_registrations', |
|
| 306 | - 'args' => array('pending') |
|
| 307 | - ), |
|
| 308 | - 'pending_and_notify_registrations' => array( |
|
| 309 | - 'func' => 'bulk_action_on_registrations', |
|
| 310 | - 'noheader' => true, |
|
| 311 | - 'capability' => 'ee_edit_registrations', |
|
| 312 | - 'args' => array('pending', true) |
|
| 313 | - ), |
|
| 314 | - 'no_approve_registration' => array( |
|
| 315 | - 'func' => 'not_approve_registration', |
|
| 316 | - 'noheader' => true, |
|
| 317 | - 'capability' => 'ee_edit_registration', |
|
| 318 | - 'obj_id' => $reg_id, |
|
| 319 | - ), |
|
| 320 | - 'no_approve_and_notify_registration' => array( |
|
| 321 | - 'func' => 'not_approve_registration', |
|
| 322 | - 'noheader' => true, |
|
| 323 | - 'args' => array(true), |
|
| 324 | - 'capability' => 'ee_edit_registration', |
|
| 325 | - 'obj_id' => $reg_id, |
|
| 326 | - ), |
|
| 327 | - 'no_approve_registrations' => array( |
|
| 328 | - 'func' => 'bulk_action_on_registrations', |
|
| 329 | - 'noheader' => true, |
|
| 330 | - 'capability' => 'ee_edit_registrations', |
|
| 331 | - 'args' => array('not_approve') |
|
| 332 | - ), |
|
| 333 | - 'no_approve_and_notify_registrations' => array( |
|
| 334 | - 'func' => 'bulk_action_on_registrations', |
|
| 335 | - 'noheader' => true, |
|
| 336 | - 'capability' => 'ee_edit_registrations', |
|
| 337 | - 'args' => array('not_approve', true) |
|
| 338 | - ), |
|
| 339 | - 'cancel_registration' => array( |
|
| 340 | - 'func' => 'cancel_registration', |
|
| 341 | - 'noheader' => true, |
|
| 342 | - 'capability' => 'ee_edit_registration', |
|
| 343 | - 'obj_id' => $reg_id, |
|
| 344 | - ), |
|
| 345 | - 'cancel_and_notify_registration' => array( |
|
| 346 | - 'func' => 'cancel_registration', |
|
| 347 | - 'noheader' => true, |
|
| 348 | - 'args' => array(true), |
|
| 349 | - 'capability' => 'ee_edit_registration', |
|
| 350 | - 'obj_id' => $reg_id, |
|
| 351 | - ), |
|
| 352 | - 'cancel_registrations' => array( |
|
| 353 | - 'func' => 'bulk_action_on_registrations', |
|
| 354 | - 'noheader' => true, |
|
| 355 | - 'capability' => 'ee_edit_registrations', |
|
| 356 | - 'args' => array('cancel') |
|
| 357 | - ), |
|
| 358 | - 'cancel_and_notify_registrations' => array( |
|
| 359 | - 'func' => 'bulk_action_on_registrations', |
|
| 360 | - 'noheader' => true, |
|
| 361 | - 'capability' => 'ee_edit_registrations', |
|
| 362 | - 'args' => array('cancel', true) |
|
| 363 | - ), |
|
| 364 | - 'wait_list_registration' => array( |
|
| 365 | - 'func' => 'wait_list_registration', |
|
| 366 | - 'noheader' => true, |
|
| 367 | - 'capability' => 'ee_edit_registration', |
|
| 368 | - 'obj_id' => $reg_id, |
|
| 369 | - ), |
|
| 370 | - 'wait_list_and_notify_registration' => array( |
|
| 371 | - 'func' => 'wait_list_registration', |
|
| 372 | - 'noheader' => true, |
|
| 373 | - 'args' => array(true), |
|
| 374 | - 'capability' => 'ee_edit_registration', |
|
| 375 | - 'obj_id' => $reg_id, |
|
| 376 | - ), |
|
| 377 | - 'contact_list' => array( |
|
| 378 | - 'func' => '_attendee_contact_list_table', |
|
| 379 | - 'capability' => 'ee_read_contacts', |
|
| 380 | - ), |
|
| 381 | - 'add_new_attendee' => array( |
|
| 382 | - 'func' => '_create_new_cpt_item', |
|
| 383 | - 'args' => array( |
|
| 384 | - 'new_attendee' => true, |
|
| 385 | - 'capability' => 'ee_edit_contacts', |
|
| 386 | - ), |
|
| 387 | - ), |
|
| 388 | - 'edit_attendee' => array( |
|
| 389 | - 'func' => '_edit_cpt_item', |
|
| 390 | - 'capability' => 'ee_edit_contacts', |
|
| 391 | - 'obj_id' => $att_id, |
|
| 392 | - ), |
|
| 393 | - 'duplicate_attendee' => array( |
|
| 394 | - 'func' => '_duplicate_attendee', |
|
| 395 | - 'noheader' => true, |
|
| 396 | - 'capability' => 'ee_edit_contacts', |
|
| 397 | - 'obj_id' => $att_id, |
|
| 398 | - ), |
|
| 399 | - 'insert_attendee' => array( |
|
| 400 | - 'func' => '_insert_or_update_attendee', |
|
| 401 | - 'args' => array( |
|
| 402 | - 'new_attendee' => true, |
|
| 403 | - ), |
|
| 404 | - 'noheader' => true, |
|
| 405 | - 'capability' => 'ee_edit_contacts', |
|
| 406 | - ), |
|
| 407 | - 'update_attendee' => array( |
|
| 408 | - 'func' => '_insert_or_update_attendee', |
|
| 409 | - 'args' => array( |
|
| 410 | - 'new_attendee' => false, |
|
| 411 | - ), |
|
| 412 | - 'noheader' => true, |
|
| 413 | - 'capability' => 'ee_edit_contacts', |
|
| 414 | - 'obj_id' => $att_id, |
|
| 415 | - ), |
|
| 416 | - 'trash_attendees' => array( |
|
| 417 | - 'func' => '_trash_or_restore_attendees', |
|
| 418 | - 'args' => array( |
|
| 419 | - 'trash' => 'true' |
|
| 420 | - ), |
|
| 421 | - 'noheader' => true, |
|
| 422 | - 'capability' => 'ee_delete_contacts' |
|
| 423 | - ), |
|
| 424 | - 'trash_attendee' => array( |
|
| 425 | - 'func' => '_trash_or_restore_attendees', |
|
| 426 | - 'args' => array( |
|
| 427 | - 'trash' => true, |
|
| 428 | - ), |
|
| 429 | - 'noheader' => true, |
|
| 430 | - 'capability' => 'ee_delete_contacts', |
|
| 431 | - 'obj_id' => $att_id, |
|
| 432 | - ), |
|
| 433 | - 'restore_attendees' => array( |
|
| 434 | - 'func' => '_trash_or_restore_attendees', |
|
| 435 | - 'args' => array( |
|
| 436 | - 'trash' => false, |
|
| 437 | - ), |
|
| 438 | - 'noheader' => true, |
|
| 439 | - 'capability' => 'ee_delete_contacts', |
|
| 440 | - 'obj_id' => $att_id, |
|
| 441 | - ), |
|
| 442 | - 'resend_registration' => array( |
|
| 443 | - 'func' => '_resend_registration', |
|
| 444 | - 'noheader' => true, |
|
| 445 | - 'capability' => 'ee_send_message', |
|
| 446 | - ), |
|
| 447 | - 'registrations_report' => array( |
|
| 448 | - 'func' => '_registrations_report', |
|
| 449 | - 'noheader' => true, |
|
| 450 | - 'capability' => 'ee_read_registrations', |
|
| 451 | - ), |
|
| 452 | - 'contact_list_export' => array( |
|
| 453 | - 'func' => '_contact_list_export', |
|
| 454 | - 'noheader' => true, |
|
| 455 | - 'capability' => 'export', |
|
| 456 | - ), |
|
| 457 | - 'contact_list_report' => array( |
|
| 458 | - 'func' => '_contact_list_report', |
|
| 459 | - 'noheader' => true, |
|
| 460 | - 'capability' => 'ee_read_contacts', |
|
| 461 | - ), |
|
| 462 | - ); |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - |
|
| 466 | - protected function _set_page_config() |
|
| 467 | - { |
|
| 468 | - $this->_page_config = array( |
|
| 469 | - 'default' => array( |
|
| 470 | - 'nav' => array( |
|
| 471 | - 'label' => esc_html__('Overview', 'event_espresso'), |
|
| 472 | - 'order' => 5, |
|
| 473 | - ), |
|
| 474 | - 'help_tabs' => array( |
|
| 475 | - 'registrations_overview_help_tab' => array( |
|
| 476 | - 'title' => esc_html__('Registrations Overview', 'event_espresso'), |
|
| 477 | - 'filename' => 'registrations_overview', |
|
| 478 | - ), |
|
| 479 | - 'registrations_overview_table_column_headings_help_tab' => array( |
|
| 480 | - 'title' => esc_html__('Registrations Table Column Headings', 'event_espresso'), |
|
| 481 | - 'filename' => 'registrations_overview_table_column_headings', |
|
| 482 | - ), |
|
| 483 | - 'registrations_overview_filters_help_tab' => array( |
|
| 484 | - 'title' => esc_html__('Registration Filters', 'event_espresso'), |
|
| 485 | - 'filename' => 'registrations_overview_filters', |
|
| 486 | - ), |
|
| 487 | - 'registrations_overview_views_help_tab' => array( |
|
| 488 | - 'title' => esc_html__('Registration Views', 'event_espresso'), |
|
| 489 | - 'filename' => 'registrations_overview_views', |
|
| 490 | - ), |
|
| 491 | - 'registrations_regoverview_other_help_tab' => array( |
|
| 492 | - 'title' => esc_html__('Registrations Other', 'event_espresso'), |
|
| 493 | - 'filename' => 'registrations_overview_other', |
|
| 494 | - ), |
|
| 495 | - ), |
|
| 496 | - 'help_tour' => array('Registration_Overview_Help_Tour'), |
|
| 497 | - 'qtips' => array('Registration_List_Table_Tips'), |
|
| 498 | - 'list_table' => 'EE_Registrations_List_Table', |
|
| 499 | - 'require_nonce' => false, |
|
| 500 | - ), |
|
| 501 | - 'view_registration' => array( |
|
| 502 | - 'nav' => array( |
|
| 503 | - 'label' => esc_html__('REG Details', 'event_espresso'), |
|
| 504 | - 'order' => 15, |
|
| 505 | - 'url' => isset($this->_req_data['_REG_ID']) |
|
| 506 | - ? add_query_arg(array('_REG_ID' => $this->_req_data['_REG_ID']), $this->_current_page_view_url) |
|
| 507 | - : $this->_admin_base_url, |
|
| 508 | - 'persistent' => false, |
|
| 509 | - ), |
|
| 510 | - 'help_tabs' => array( |
|
| 511 | - 'registrations_details_help_tab' => array( |
|
| 512 | - 'title' => esc_html__('Registration Details', 'event_espresso'), |
|
| 513 | - 'filename' => 'registrations_details', |
|
| 514 | - ), |
|
| 515 | - 'registrations_details_table_help_tab' => array( |
|
| 516 | - 'title' => esc_html__('Registration Details Table', 'event_espresso'), |
|
| 517 | - 'filename' => 'registrations_details_table', |
|
| 518 | - ), |
|
| 519 | - 'registrations_details_form_answers_help_tab' => array( |
|
| 520 | - 'title' => esc_html__('Registration Form Answers', 'event_espresso'), |
|
| 521 | - 'filename' => 'registrations_details_form_answers', |
|
| 522 | - ), |
|
| 523 | - 'registrations_details_registrant_details_help_tab' => array( |
|
| 524 | - 'title' => esc_html__('Contact Details', 'event_espresso'), |
|
| 525 | - 'filename' => 'registrations_details_registrant_details', |
|
| 526 | - ), |
|
| 527 | - ), |
|
| 528 | - 'help_tour' => array('Registration_Details_Help_Tour'), |
|
| 529 | - 'metaboxes' => array_merge( |
|
| 530 | - $this->_default_espresso_metaboxes, |
|
| 531 | - array('_registration_details_metaboxes') |
|
| 532 | - ), |
|
| 533 | - 'require_nonce' => false, |
|
| 534 | - ), |
|
| 535 | - 'new_registration' => array( |
|
| 536 | - 'nav' => array( |
|
| 537 | - 'label' => esc_html__('Add New Registration', 'event_espresso'), |
|
| 538 | - 'url' => '#', |
|
| 539 | - 'order' => 15, |
|
| 540 | - 'persistent' => false, |
|
| 541 | - ), |
|
| 542 | - 'metaboxes' => $this->_default_espresso_metaboxes, |
|
| 543 | - 'labels' => array( |
|
| 544 | - 'publishbox' => esc_html__('Save Registration', 'event_espresso'), |
|
| 545 | - ), |
|
| 546 | - 'require_nonce' => false, |
|
| 547 | - ), |
|
| 548 | - 'add_new_attendee' => array( |
|
| 549 | - 'nav' => array( |
|
| 550 | - 'label' => esc_html__('Add Contact', 'event_espresso'), |
|
| 551 | - 'order' => 15, |
|
| 552 | - 'persistent' => false, |
|
| 553 | - ), |
|
| 554 | - 'metaboxes' => array_merge( |
|
| 555 | - $this->_default_espresso_metaboxes, |
|
| 556 | - array('_publish_post_box', 'attendee_editor_metaboxes') |
|
| 557 | - ), |
|
| 558 | - 'require_nonce' => false, |
|
| 559 | - ), |
|
| 560 | - 'edit_attendee' => array( |
|
| 561 | - 'nav' => array( |
|
| 562 | - 'label' => esc_html__('Edit Contact', 'event_espresso'), |
|
| 563 | - 'order' => 15, |
|
| 564 | - 'persistent' => false, |
|
| 565 | - 'url' => isset($this->_req_data['ATT_ID']) |
|
| 566 | - ? add_query_arg(array('ATT_ID' => $this->_req_data['ATT_ID']), $this->_current_page_view_url) |
|
| 567 | - : $this->_admin_base_url, |
|
| 568 | - ), |
|
| 569 | - 'metaboxes' => array('attendee_editor_metaboxes'), |
|
| 570 | - 'require_nonce' => false, |
|
| 571 | - ), |
|
| 572 | - 'contact_list' => array( |
|
| 573 | - 'nav' => array( |
|
| 574 | - 'label' => esc_html__('Contact List', 'event_espresso'), |
|
| 575 | - 'order' => 20, |
|
| 576 | - ), |
|
| 577 | - 'list_table' => 'EE_Attendee_Contact_List_Table', |
|
| 578 | - 'help_tabs' => array( |
|
| 579 | - 'registrations_contact_list_help_tab' => array( |
|
| 580 | - 'title' => esc_html__('Registrations Contact List', 'event_espresso'), |
|
| 581 | - 'filename' => 'registrations_contact_list', |
|
| 582 | - ), |
|
| 583 | - 'registrations_contact-list_table_column_headings_help_tab' => array( |
|
| 584 | - 'title' => esc_html__('Contact List Table Column Headings', 'event_espresso'), |
|
| 585 | - 'filename' => 'registrations_contact_list_table_column_headings', |
|
| 586 | - ), |
|
| 587 | - 'registrations_contact_list_views_help_tab' => array( |
|
| 588 | - 'title' => esc_html__('Contact List Views', 'event_espresso'), |
|
| 589 | - 'filename' => 'registrations_contact_list_views', |
|
| 590 | - ), |
|
| 591 | - 'registrations_contact_list_other_help_tab' => array( |
|
| 592 | - 'title' => esc_html__('Contact List Other', 'event_espresso'), |
|
| 593 | - 'filename' => 'registrations_contact_list_other', |
|
| 594 | - ), |
|
| 595 | - ), |
|
| 596 | - 'help_tour' => array('Contact_List_Help_Tour'), |
|
| 597 | - 'metaboxes' => array(), |
|
| 598 | - 'require_nonce' => false, |
|
| 599 | - ), |
|
| 600 | - //override default cpt routes |
|
| 601 | - 'create_new' => '', |
|
| 602 | - 'edit' => '', |
|
| 603 | - ); |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - |
|
| 607 | - /** |
|
| 608 | - * The below methods aren't used by this class currently |
|
| 609 | - */ |
|
| 610 | - protected function _add_screen_options() |
|
| 611 | - { |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - |
|
| 615 | - protected function _add_feature_pointers() |
|
| 616 | - { |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - |
|
| 620 | - public function admin_init() |
|
| 621 | - { |
|
| 622 | - EE_Registry::$i18n_js_strings['update_att_qstns'] = esc_html__( |
|
| 623 | - 'click "Update Registration Questions" to save your changes', |
|
| 624 | - 'event_espresso' |
|
| 625 | - ); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - |
|
| 629 | - public function admin_notices() |
|
| 630 | - { |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - |
|
| 634 | - public function admin_footer_scripts() |
|
| 635 | - { |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * get list of registration statuses |
|
| 641 | - * |
|
| 642 | - * @access private |
|
| 643 | - * @return void |
|
| 644 | - * @throws EE_Error |
|
| 645 | - */ |
|
| 646 | - private function _get_registration_status_array() |
|
| 647 | - { |
|
| 648 | - self::$_reg_status = EEM_Registration::reg_status_array(array(), true); |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - |
|
| 652 | - protected function _add_screen_options_default() |
|
| 653 | - { |
|
| 654 | - $this->_per_page_screen_option(); |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - |
|
| 658 | - protected function _add_screen_options_contact_list() |
|
| 659 | - { |
|
| 660 | - $page_title = $this->_admin_page_title; |
|
| 661 | - $this->_admin_page_title = esc_html__("Contacts", 'event_espresso'); |
|
| 662 | - $this->_per_page_screen_option(); |
|
| 663 | - $this->_admin_page_title = $page_title; |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - |
|
| 667 | - public function load_scripts_styles() |
|
| 668 | - { |
|
| 669 | - //style |
|
| 670 | - wp_register_style( |
|
| 671 | - 'espresso_reg', |
|
| 672 | - REG_ASSETS_URL . 'espresso_registrations_admin.css', |
|
| 673 | - array('ee-admin-css'), |
|
| 674 | - EVENT_ESPRESSO_VERSION |
|
| 675 | - ); |
|
| 676 | - wp_enqueue_style('espresso_reg'); |
|
| 677 | - //script |
|
| 678 | - wp_register_script( |
|
| 679 | - 'espresso_reg', |
|
| 680 | - REG_ASSETS_URL . 'espresso_registrations_admin.js', |
|
| 681 | - array('jquery-ui-datepicker', 'jquery-ui-draggable', 'ee_admin_js'), |
|
| 682 | - EVENT_ESPRESSO_VERSION, |
|
| 683 | - true |
|
| 684 | - ); |
|
| 685 | - wp_enqueue_script('espresso_reg'); |
|
| 686 | - } |
|
| 687 | - |
|
| 688 | - |
|
| 689 | - public function load_scripts_styles_edit_attendee() |
|
| 690 | - { |
|
| 691 | - //stuff to only show up on our attendee edit details page. |
|
| 692 | - $attendee_details_translations = array( |
|
| 693 | - 'att_publish_text' => sprintf( |
|
| 694 | - esc_html__('Created on: <b>%1$s</b>', 'event_espresso'), |
|
| 695 | - $this->_cpt_model_obj->get_datetime('ATT_created') |
|
| 696 | - ), |
|
| 697 | - ); |
|
| 698 | - wp_localize_script('espresso_reg', 'ATTENDEE_DETAILS', $attendee_details_translations); |
|
| 699 | - wp_enqueue_script('jquery-validate'); |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - |
|
| 703 | - public function load_scripts_styles_view_registration() |
|
| 704 | - { |
|
| 705 | - //styles |
|
| 706 | - wp_enqueue_style('espresso-ui-theme'); |
|
| 707 | - //scripts |
|
| 708 | - $this->_get_reg_custom_questions_form($this->_registration->ID()); |
|
| 709 | - $this->_reg_custom_questions_form->wp_enqueue_scripts(true); |
|
| 710 | - } |
|
| 711 | - |
|
| 712 | - |
|
| 713 | - public function load_scripts_styles_contact_list() |
|
| 714 | - { |
|
| 715 | - wp_deregister_style('espresso_reg'); |
|
| 716 | - wp_register_style( |
|
| 717 | - 'espresso_att', |
|
| 718 | - REG_ASSETS_URL . 'espresso_attendees_admin.css', |
|
| 719 | - array('ee-admin-css'), |
|
| 720 | - EVENT_ESPRESSO_VERSION |
|
| 721 | - ); |
|
| 722 | - wp_enqueue_style('espresso_att'); |
|
| 723 | - } |
|
| 724 | - |
|
| 725 | - |
|
| 726 | - public function load_scripts_styles_new_registration() |
|
| 727 | - { |
|
| 728 | - wp_register_script( |
|
| 729 | - 'ee-spco-for-admin', |
|
| 730 | - REG_ASSETS_URL . 'spco_for_admin.js', |
|
| 731 | - array('underscore', 'jquery'), |
|
| 732 | - EVENT_ESPRESSO_VERSION, |
|
| 733 | - true |
|
| 734 | - ); |
|
| 735 | - wp_enqueue_script('ee-spco-for-admin'); |
|
| 736 | - add_filter('FHEE__EED_Ticket_Selector__load_tckt_slctr_assets', '__return_true'); |
|
| 737 | - EE_Form_Section_Proper::wp_enqueue_scripts(); |
|
| 738 | - EED_Ticket_Selector::load_tckt_slctr_assets(); |
|
| 739 | - EE_Datepicker_Input::enqueue_styles_and_scripts(); |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - |
|
| 743 | - public function AHEE__EE_Admin_Page__route_admin_request_resend_registration() |
|
| 744 | - { |
|
| 745 | - add_filter('FHEE_load_EE_messages', '__return_true'); |
|
| 746 | - } |
|
| 747 | - |
|
| 748 | - |
|
| 749 | - public function AHEE__EE_Admin_Page__route_admin_request_approve_registration() |
|
| 750 | - { |
|
| 751 | - add_filter('FHEE_load_EE_messages', '__return_true'); |
|
| 752 | - } |
|
| 753 | - |
|
| 754 | - |
|
| 755 | - protected function _set_list_table_views_default() |
|
| 756 | - { |
|
| 757 | - //for notification related bulk actions we need to make sure only active messengers have an option. |
|
| 758 | - EED_Messages::set_autoloaders(); |
|
| 759 | - /** @type EE_Message_Resource_Manager $message_resource_manager */ |
|
| 760 | - $message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
|
| 761 | - $active_mts = $message_resource_manager->list_of_active_message_types(); |
|
| 762 | - //key= bulk_action_slug, value= message type. |
|
| 763 | - $match_array = array( |
|
| 764 | - 'approve_registrations' => 'registration', |
|
| 765 | - 'decline_registrations' => 'declined_registration', |
|
| 766 | - 'pending_registrations' => 'pending_approval', |
|
| 767 | - 'no_approve_registrations' => 'not_approved_registration', |
|
| 768 | - 'cancel_registrations' => 'cancelled_registration', |
|
| 769 | - ); |
|
| 770 | - $can_send = EE_Registry::instance()->CAP->current_user_can( |
|
| 771 | - 'ee_send_message', |
|
| 772 | - 'batch_send_messages' |
|
| 773 | - ); |
|
| 774 | - /** setup reg status bulk actions **/ |
|
| 775 | - $def_reg_status_actions['approve_registrations'] = esc_html__('Approve Registrations', 'event_espresso'); |
|
| 776 | - if ($can_send && in_array($match_array['approve_registrations'], $active_mts, true)) { |
|
| 777 | - $def_reg_status_actions['approve_and_notify_registrations'] = esc_html__( |
|
| 778 | - 'Approve and Notify Registrations', |
|
| 779 | - 'event_espresso' |
|
| 780 | - ); |
|
| 781 | - } |
|
| 782 | - $def_reg_status_actions['decline_registrations'] = esc_html__('Decline Registrations', 'event_espresso'); |
|
| 783 | - if ($can_send && in_array($match_array['decline_registrations'], $active_mts, true)) { |
|
| 784 | - $def_reg_status_actions['decline_and_notify_registrations'] = esc_html__( |
|
| 785 | - 'Decline and Notify Registrations', |
|
| 786 | - 'event_espresso' |
|
| 787 | - ); |
|
| 788 | - } |
|
| 789 | - $def_reg_status_actions['pending_registrations'] = esc_html__( |
|
| 790 | - 'Set Registrations to Pending Payment', |
|
| 791 | - 'event_espresso' |
|
| 792 | - ); |
|
| 793 | - if ($can_send && in_array($match_array['pending_registrations'], $active_mts, true)) { |
|
| 794 | - $def_reg_status_actions['pending_and_notify_registrations'] = esc_html__( |
|
| 795 | - 'Set Registrations to Pending Payment and Notify', |
|
| 796 | - 'event_espresso' |
|
| 797 | - ); |
|
| 798 | - } |
|
| 799 | - $def_reg_status_actions['no_approve_registrations'] = esc_html__( |
|
| 800 | - 'Set Registrations to Not Approved', |
|
| 801 | - 'event_espresso' |
|
| 802 | - ); |
|
| 803 | - if ($can_send && in_array($match_array['no_approve_registrations'], $active_mts, true)) { |
|
| 804 | - $def_reg_status_actions['no_approve_and_notify_registrations'] = esc_html__( |
|
| 805 | - 'Set Registrations to Not Approved and Notify', |
|
| 806 | - 'event_espresso' |
|
| 807 | - ); |
|
| 808 | - } |
|
| 809 | - $def_reg_status_actions['cancel_registrations'] = esc_html__('Cancel Registrations', 'event_espresso'); |
|
| 810 | - if ($can_send && in_array($match_array['cancel_registrations'], $active_mts, true)) { |
|
| 811 | - $def_reg_status_actions['cancel_and_notify_registrations'] = esc_html__( |
|
| 812 | - 'Cancel Registrations and Notify', |
|
| 813 | - 'event_espresso' |
|
| 814 | - ); |
|
| 815 | - } |
|
| 816 | - $def_reg_status_actions = apply_filters( |
|
| 817 | - 'FHEE__Registrations_Admin_Page___set_list_table_views_default__def_reg_status_actions_array', |
|
| 818 | - $def_reg_status_actions, |
|
| 819 | - $active_mts, |
|
| 820 | - $can_send |
|
| 821 | - ); |
|
| 822 | - |
|
| 823 | - $this->_views = array( |
|
| 824 | - 'all' => array( |
|
| 825 | - 'slug' => 'all', |
|
| 826 | - 'label' => esc_html__('View All Registrations', 'event_espresso'), |
|
| 827 | - 'count' => 0, |
|
| 828 | - 'bulk_action' => array_merge($def_reg_status_actions, array( |
|
| 829 | - 'trash_registrations' => esc_html__('Trash Registrations', 'event_espresso'), |
|
| 830 | - )), |
|
| 831 | - ), |
|
| 832 | - 'month' => array( |
|
| 833 | - 'slug' => 'month', |
|
| 834 | - 'label' => esc_html__('This Month', 'event_espresso'), |
|
| 835 | - 'count' => 0, |
|
| 836 | - 'bulk_action' => array_merge($def_reg_status_actions, array( |
|
| 837 | - 'trash_registrations' => esc_html__('Trash Registrations', 'event_espresso'), |
|
| 838 | - )), |
|
| 839 | - ), |
|
| 840 | - 'today' => array( |
|
| 841 | - 'slug' => 'today', |
|
| 842 | - 'label' => sprintf( |
|
| 843 | - esc_html__('Today - %s', 'event_espresso'), |
|
| 844 | - date('M d, Y', current_time('timestamp')) |
|
| 845 | - ), |
|
| 846 | - 'count' => 0, |
|
| 847 | - 'bulk_action' => array_merge($def_reg_status_actions, array( |
|
| 848 | - 'trash_registrations' => esc_html__('Trash Registrations', 'event_espresso'), |
|
| 849 | - )), |
|
| 850 | - ), |
|
| 851 | - ); |
|
| 852 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
| 853 | - 'ee_delete_registrations', |
|
| 854 | - 'espresso_registrations_delete_registration' |
|
| 855 | - )) { |
|
| 856 | - $this->_views['incomplete'] = array( |
|
| 857 | - 'slug' => 'incomplete', |
|
| 858 | - 'label' => esc_html__('Incomplete', 'event_espresso'), |
|
| 859 | - 'count' => 0, |
|
| 860 | - 'bulk_action' => array( |
|
| 861 | - 'trash_registrations' => esc_html__('Trash Registrations', 'event_espresso'), |
|
| 862 | - ), |
|
| 863 | - ); |
|
| 864 | - $this->_views['trash'] = array( |
|
| 865 | - 'slug' => 'trash', |
|
| 866 | - 'label' => esc_html__('Trash', 'event_espresso'), |
|
| 867 | - 'count' => 0, |
|
| 868 | - 'bulk_action' => array( |
|
| 869 | - 'restore_registrations' => esc_html__('Restore Registrations', 'event_espresso'), |
|
| 870 | - 'delete_registrations' => esc_html__('Delete Registrations Permanently', 'event_espresso'), |
|
| 871 | - ), |
|
| 872 | - ); |
|
| 873 | - } |
|
| 874 | - } |
|
| 875 | - |
|
| 876 | - |
|
| 877 | - protected function _set_list_table_views_contact_list() |
|
| 878 | - { |
|
| 879 | - $this->_views = array( |
|
| 880 | - 'in_use' => array( |
|
| 881 | - 'slug' => 'in_use', |
|
| 882 | - 'label' => esc_html__('In Use', 'event_espresso'), |
|
| 883 | - 'count' => 0, |
|
| 884 | - 'bulk_action' => array( |
|
| 885 | - 'trash_attendees' => esc_html__('Move to Trash', 'event_espresso'), |
|
| 886 | - ), |
|
| 887 | - ), |
|
| 888 | - ); |
|
| 889 | - if (EE_Registry::instance()->CAP->current_user_can('ee_delete_contacts', |
|
| 890 | - 'espresso_registrations_trash_attendees') |
|
| 891 | - ) { |
|
| 892 | - $this->_views['trash'] = array( |
|
| 893 | - 'slug' => 'trash', |
|
| 894 | - 'label' => esc_html__('Trash', 'event_espresso'), |
|
| 895 | - 'count' => 0, |
|
| 896 | - 'bulk_action' => array( |
|
| 897 | - 'restore_attendees' => esc_html__('Restore from Trash', 'event_espresso'), |
|
| 898 | - ), |
|
| 899 | - ); |
|
| 900 | - } |
|
| 901 | - } |
|
| 902 | - |
|
| 903 | - |
|
| 904 | - protected function _registration_legend_items() |
|
| 905 | - { |
|
| 906 | - $fc_items = array( |
|
| 907 | - 'star-icon' => array( |
|
| 908 | - 'class' => 'dashicons dashicons-star-filled lt-blue-icon ee-icon-size-8', |
|
| 909 | - 'desc' => esc_html__('This is the Primary Registrant', 'event_espresso'), |
|
| 910 | - ), |
|
| 911 | - 'view_details' => array( |
|
| 912 | - 'class' => 'dashicons dashicons-clipboard', |
|
| 913 | - 'desc' => esc_html__('View Registration Details', 'event_espresso'), |
|
| 914 | - ), |
|
| 915 | - 'edit_attendee' => array( |
|
| 916 | - 'class' => 'ee-icon ee-icon-user-edit ee-icon-size-16', |
|
| 917 | - 'desc' => esc_html__('Edit Contact Details', 'event_espresso'), |
|
| 918 | - ), |
|
| 919 | - 'view_transaction' => array( |
|
| 920 | - 'class' => 'dashicons dashicons-cart', |
|
| 921 | - 'desc' => esc_html__('View Transaction Details', 'event_espresso'), |
|
| 922 | - ), |
|
| 923 | - 'view_invoice' => array( |
|
| 924 | - 'class' => 'dashicons dashicons-media-spreadsheet', |
|
| 925 | - 'desc' => esc_html__('View Transaction Invoice', 'event_espresso'), |
|
| 926 | - ), |
|
| 927 | - ); |
|
| 928 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
| 929 | - 'ee_send_message', |
|
| 930 | - 'espresso_registrations_resend_registration' |
|
| 931 | - )) { |
|
| 932 | - $fc_items['resend_registration'] = array( |
|
| 933 | - 'class' => 'dashicons dashicons-email-alt', |
|
| 934 | - 'desc' => esc_html__('Resend Registration Details', 'event_espresso'), |
|
| 935 | - ); |
|
| 936 | - } else { |
|
| 937 | - $fc_items['blank'] = array('class' => 'blank', 'desc' => ''); |
|
| 938 | - } |
|
| 939 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
| 940 | - 'ee_read_global_messages', |
|
| 941 | - 'view_filtered_messages' |
|
| 942 | - )) { |
|
| 943 | - $related_for_icon = EEH_MSG_Template::get_message_action_icon('see_notifications_for'); |
|
| 944 | - if (isset($related_for_icon['css_class']) && isset($related_for_icon['label'])) { |
|
| 945 | - $fc_items['view_related_messages'] = array( |
|
| 946 | - 'class' => $related_for_icon['css_class'], |
|
| 947 | - 'desc' => $related_for_icon['label'], |
|
| 948 | - ); |
|
| 949 | - } |
|
| 950 | - } |
|
| 951 | - $sc_items = array( |
|
| 952 | - 'approved_status' => array( |
|
| 953 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_approved, |
|
| 954 | - 'desc' => EEH_Template::pretty_status( |
|
| 955 | - EEM_Registration::status_id_approved, |
|
| 956 | - false, |
|
| 957 | - 'sentence' |
|
| 958 | - ), |
|
| 959 | - ), |
|
| 960 | - 'pending_status' => array( |
|
| 961 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_pending_payment, |
|
| 962 | - 'desc' => EEH_Template::pretty_status( |
|
| 963 | - EEM_Registration::status_id_pending_payment, |
|
| 964 | - false, |
|
| 965 | - 'sentence' |
|
| 966 | - ), |
|
| 967 | - ), |
|
| 968 | - 'wait_list' => array( |
|
| 969 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_wait_list, |
|
| 970 | - 'desc' => EEH_Template::pretty_status( |
|
| 971 | - EEM_Registration::status_id_wait_list, |
|
| 972 | - false, |
|
| 973 | - 'sentence' |
|
| 974 | - ), |
|
| 975 | - ), |
|
| 976 | - 'incomplete_status' => array( |
|
| 977 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_incomplete, |
|
| 978 | - 'desc' => EEH_Template::pretty_status( |
|
| 979 | - EEM_Registration::status_id_incomplete, |
|
| 980 | - false, |
|
| 981 | - 'sentence' |
|
| 982 | - ), |
|
| 983 | - ), |
|
| 984 | - 'not_approved' => array( |
|
| 985 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_not_approved, |
|
| 986 | - 'desc' => EEH_Template::pretty_status( |
|
| 987 | - EEM_Registration::status_id_not_approved, |
|
| 988 | - false, |
|
| 989 | - 'sentence' |
|
| 990 | - ), |
|
| 991 | - ), |
|
| 992 | - 'declined_status' => array( |
|
| 993 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_declined, |
|
| 994 | - 'desc' => EEH_Template::pretty_status( |
|
| 995 | - EEM_Registration::status_id_declined, |
|
| 996 | - false, |
|
| 997 | - 'sentence' |
|
| 998 | - ), |
|
| 999 | - ), |
|
| 1000 | - 'cancelled_status' => array( |
|
| 1001 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_cancelled, |
|
| 1002 | - 'desc' => EEH_Template::pretty_status( |
|
| 1003 | - EEM_Registration::status_id_cancelled, |
|
| 1004 | - false, |
|
| 1005 | - 'sentence' |
|
| 1006 | - ), |
|
| 1007 | - ), |
|
| 1008 | - ); |
|
| 1009 | - return array_merge($fc_items, $sc_items); |
|
| 1010 | - } |
|
| 1011 | - |
|
| 1012 | - |
|
| 1013 | - |
|
| 1014 | - /*************************************** REGISTRATION OVERVIEW **************************************/ |
|
| 1015 | - /** |
|
| 1016 | - * @throws \EE_Error |
|
| 1017 | - */ |
|
| 1018 | - protected function _registrations_overview_list_table() |
|
| 1019 | - { |
|
| 1020 | - $this->_template_args['admin_page_header'] = ''; |
|
| 1021 | - $EVT_ID = ! empty($this->_req_data['event_id']) |
|
| 1022 | - ? absint($this->_req_data['event_id']) |
|
| 1023 | - : 0; |
|
| 1024 | - $ATT_ID = !empty($this->_req_data['ATT_ID']) |
|
| 1025 | - ? absint($this->_req_data['ATT_ID']) |
|
| 1026 | - : 0; |
|
| 1027 | - if ($ATT_ID) { |
|
| 1028 | - $attendee = EEM_Attendee::instance()->get_one_by_ID($ATT_ID); |
|
| 1029 | - if ($attendee instanceof EE_Attendee) { |
|
| 1030 | - $this->_template_args['admin_page_header'] = sprintf( |
|
| 1031 | - esc_html__( |
|
| 1032 | - '%1$s Viewing registrations for %2$s%3$s', |
|
| 1033 | - 'event_espresso' |
|
| 1034 | - ), |
|
| 1035 | - '<h3 style="line-height:1.5em;">', |
|
| 1036 | - '<a href="' . EE_Admin_Page::add_query_args_and_nonce( |
|
| 1037 | - array( |
|
| 1038 | - 'action' => 'edit_attendee', |
|
| 1039 | - 'post' => $ATT_ID |
|
| 1040 | - ), |
|
| 1041 | - REG_ADMIN_URL |
|
| 1042 | - ) . '">' . $attendee->full_name() . '</a>', |
|
| 1043 | - '</h3>' |
|
| 1044 | - ); |
|
| 1045 | - } |
|
| 1046 | - } |
|
| 1047 | - if ($EVT_ID) { |
|
| 1048 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
| 1049 | - 'ee_edit_registrations', |
|
| 1050 | - 'espresso_registrations_new_registration', |
|
| 1051 | - $EVT_ID |
|
| 1052 | - )) { |
|
| 1053 | - $this->_admin_page_title .= ' ' . $this->get_action_link_or_button( |
|
| 1054 | - 'new_registration', |
|
| 1055 | - 'add-registrant', |
|
| 1056 | - array('event_id' => $EVT_ID), |
|
| 1057 | - 'add-new-h2' |
|
| 1058 | - ); |
|
| 1059 | - } |
|
| 1060 | - $event = EEM_Event::instance()->get_one_by_ID($EVT_ID); |
|
| 1061 | - if ($event instanceof EE_Event) { |
|
| 1062 | - $this->_template_args['admin_page_header'] = sprintf( |
|
| 1063 | - esc_html__( |
|
| 1064 | - '%s Viewing registrations for the event: %s%s', |
|
| 1065 | - 'event_espresso' |
|
| 1066 | - ), |
|
| 1067 | - '<h3 style="line-height:1.5em;">', |
|
| 1068 | - '<br /><a href="' |
|
| 1069 | - . EE_Admin_Page::add_query_args_and_nonce( |
|
| 1070 | - array( |
|
| 1071 | - 'action' => 'edit', |
|
| 1072 | - 'post' => $event->ID(), |
|
| 1073 | - ), |
|
| 1074 | - EVENTS_ADMIN_URL |
|
| 1075 | - ) |
|
| 1076 | - . '"> ' |
|
| 1077 | - . $event->get('EVT_name') |
|
| 1078 | - . ' </a> ', |
|
| 1079 | - '</h3>' |
|
| 1080 | - ); |
|
| 1081 | - } |
|
| 1082 | - $DTT_ID = ! empty($this->_req_data['datetime_id']) ? absint($this->_req_data['datetime_id']) : 0; |
|
| 1083 | - $datetime = EEM_Datetime::instance()->get_one_by_ID($DTT_ID); |
|
| 1084 | - if ($datetime instanceof EE_Datetime && $this->_template_args['admin_page_header'] !== '') { |
|
| 1085 | - $this->_template_args['admin_page_header'] = substr( |
|
| 1086 | - $this->_template_args['admin_page_header'], |
|
| 1087 | - 0, |
|
| 1088 | - -5 |
|
| 1089 | - ); |
|
| 1090 | - $this->_template_args['admin_page_header'] .= ' <span class="drk-grey-text">'; |
|
| 1091 | - $this->_template_args['admin_page_header'] .= '<span class="dashicons dashicons-calendar"></span>'; |
|
| 1092 | - $this->_template_args['admin_page_header'] .= $datetime->name(); |
|
| 1093 | - $this->_template_args['admin_page_header'] .= ' ( ' . $datetime->start_date() . ' )'; |
|
| 1094 | - $this->_template_args['admin_page_header'] .= '</span></h3>'; |
|
| 1095 | - } |
|
| 1096 | - } |
|
| 1097 | - $this->_template_args['after_list_table'] = $this->_display_legend($this->_registration_legend_items()); |
|
| 1098 | - $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 1099 | - } |
|
| 1100 | - |
|
| 1101 | - |
|
| 1102 | - /** |
|
| 1103 | - * This sets the _registration property for the registration details screen |
|
| 1104 | - * |
|
| 1105 | - * @access private |
|
| 1106 | - * @return bool |
|
| 1107 | - * @throws EE_Error |
|
| 1108 | - * @throws InvalidArgumentException |
|
| 1109 | - * @throws InvalidDataTypeException |
|
| 1110 | - * @throws InvalidInterfaceException |
|
| 1111 | - */ |
|
| 1112 | - private function _set_registration_object() |
|
| 1113 | - { |
|
| 1114 | - //get out if we've already set the object |
|
| 1115 | - if ($this->_registration instanceof EE_Registration) { |
|
| 1116 | - return true; |
|
| 1117 | - } |
|
| 1118 | - $REG = EEM_Registration::instance(); |
|
| 1119 | - $REG_ID = ( ! empty($this->_req_data['_REG_ID'])) ? absint($this->_req_data['_REG_ID']) : false; |
|
| 1120 | - if ($this->_registration = $REG->get_one_by_ID($REG_ID)) { |
|
| 1121 | - return true; |
|
| 1122 | - } else { |
|
| 1123 | - $error_msg = sprintf( |
|
| 1124 | - esc_html__( |
|
| 1125 | - 'An error occurred and the details for Registration ID #%s could not be retrieved.', |
|
| 1126 | - 'event_espresso' |
|
| 1127 | - ), |
|
| 1128 | - $REG_ID |
|
| 1129 | - ); |
|
| 1130 | - EE_Error::add_error($error_msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 1131 | - $this->_registration = null; |
|
| 1132 | - return false; |
|
| 1133 | - } |
|
| 1134 | - } |
|
| 1135 | - |
|
| 1136 | - |
|
| 1137 | - /** |
|
| 1138 | - * Used to retrieve registrations for the list table. |
|
| 1139 | - * |
|
| 1140 | - * @param int $per_page |
|
| 1141 | - * @param bool $count |
|
| 1142 | - * @param bool $this_month |
|
| 1143 | - * @param bool $today |
|
| 1144 | - * @return EE_Registration[]|int |
|
| 1145 | - * @throws EE_Error |
|
| 1146 | - * @throws InvalidArgumentException |
|
| 1147 | - * @throws InvalidDataTypeException |
|
| 1148 | - * @throws InvalidInterfaceException |
|
| 1149 | - */ |
|
| 1150 | - public function get_registrations( |
|
| 1151 | - $per_page = 10, |
|
| 1152 | - $count = false, |
|
| 1153 | - $this_month = false, |
|
| 1154 | - $today = false |
|
| 1155 | - ) { |
|
| 1156 | - if ($this_month) { |
|
| 1157 | - $this->_req_data['status'] = 'month'; |
|
| 1158 | - } |
|
| 1159 | - if ($today) { |
|
| 1160 | - $this->_req_data['status'] = 'today'; |
|
| 1161 | - } |
|
| 1162 | - $query_params = $this->_get_registration_query_parameters($this->_req_data, $per_page, $count); |
|
| 1163 | - /** |
|
| 1164 | - * Override the default groupby added by EEM_Base so that sorts with multiple order bys work as expected |
|
| 1165 | - * @link https://events.codebasehq.com/projects/event-espresso/tickets/10093 |
|
| 1166 | - * @see EEM_Base::get_all() |
|
| 1167 | - */ |
|
| 1168 | - $query_params['group_by'] = ''; |
|
| 1169 | - |
|
| 1170 | - return $count |
|
| 1171 | - ? EEM_Registration::instance()->count($query_params) |
|
| 1172 | - /** @type EE_Registration[] */ |
|
| 1173 | - : EEM_Registration::instance()->get_all($query_params); |
|
| 1174 | - } |
|
| 1175 | - |
|
| 1176 | - |
|
| 1177 | - /** |
|
| 1178 | - * Retrieves the query parameters to be used by the Registration model for getting registrations. |
|
| 1179 | - * Note: this listens to values on the request for some of the query parameters. |
|
| 1180 | - * |
|
| 1181 | - * @param array $request |
|
| 1182 | - * @param int $per_page |
|
| 1183 | - * @param bool $count |
|
| 1184 | - * @return array |
|
| 1185 | - * @throws EE_Error |
|
| 1186 | - */ |
|
| 1187 | - protected function _get_registration_query_parameters( |
|
| 1188 | - $request = array(), |
|
| 1189 | - $per_page = 10, |
|
| 1190 | - $count = false |
|
| 1191 | - ) { |
|
| 1192 | - |
|
| 1193 | - $query_params = array( |
|
| 1194 | - 0 => $this->_get_where_conditions_for_registrations_query( |
|
| 1195 | - $request |
|
| 1196 | - ), |
|
| 1197 | - 'caps' => EEM_Registration::caps_read_admin, |
|
| 1198 | - 'default_where_conditions' => 'this_model_only', |
|
| 1199 | - ); |
|
| 1200 | - if (! $count) { |
|
| 1201 | - $query_params = array_merge( |
|
| 1202 | - $query_params, |
|
| 1203 | - $this->_get_orderby_for_registrations_query(), |
|
| 1204 | - $this->_get_limit($per_page) |
|
| 1205 | - ); |
|
| 1206 | - } |
|
| 1207 | - |
|
| 1208 | - return $query_params; |
|
| 1209 | - } |
|
| 1210 | - |
|
| 1211 | - |
|
| 1212 | - /** |
|
| 1213 | - * This will add ATT_ID to the provided $where array for EE model query parameters. |
|
| 1214 | - * |
|
| 1215 | - * @param array $request usually the same as $this->_req_data but not necessarily |
|
| 1216 | - * @return array |
|
| 1217 | - */ |
|
| 1218 | - protected function addAttendeeIdToWhereConditions(array $request) |
|
| 1219 | - { |
|
| 1220 | - $where = array(); |
|
| 1221 | - if (! empty($request['ATT_ID'])) { |
|
| 1222 | - $where['ATT_ID'] = absint($request['ATT_ID']); |
|
| 1223 | - } |
|
| 1224 | - return $where; |
|
| 1225 | - } |
|
| 1226 | - |
|
| 1227 | - |
|
| 1228 | - /** |
|
| 1229 | - * This will add EVT_ID to the provided $where array for EE model query parameters. |
|
| 1230 | - * |
|
| 1231 | - * @param array $request usually the same as $this->_req_data but not necessarily |
|
| 1232 | - * @return array |
|
| 1233 | - */ |
|
| 1234 | - protected function _add_event_id_to_where_conditions(array $request) |
|
| 1235 | - { |
|
| 1236 | - $where = array(); |
|
| 1237 | - if (! empty($request['event_id'])) { |
|
| 1238 | - $where['EVT_ID'] = absint($request['event_id']); |
|
| 1239 | - } |
|
| 1240 | - return $where; |
|
| 1241 | - } |
|
| 1242 | - |
|
| 1243 | - |
|
| 1244 | - /** |
|
| 1245 | - * Adds category ID if it exists in the request to the where conditions for the registrations query. |
|
| 1246 | - * |
|
| 1247 | - * @param array $request usually the same as $this->_req_data but not necessarily |
|
| 1248 | - * @return array |
|
| 1249 | - */ |
|
| 1250 | - protected function _add_category_id_to_where_conditions(array $request) |
|
| 1251 | - { |
|
| 1252 | - $where = array(); |
|
| 1253 | - if (! empty($request['EVT_CAT']) && (int)$request['EVT_CAT'] !== -1) { |
|
| 1254 | - $where['Event.Term_Taxonomy.term_id'] = absint($request['EVT_CAT']); |
|
| 1255 | - } |
|
| 1256 | - return $where; |
|
| 1257 | - } |
|
| 1258 | - |
|
| 1259 | - |
|
| 1260 | - /** |
|
| 1261 | - * Adds the datetime ID if it exists in the request to the where conditions for the registrations query. |
|
| 1262 | - * |
|
| 1263 | - * @param array $request usually the same as $this->_req_data but not necessarily |
|
| 1264 | - * @return array |
|
| 1265 | - */ |
|
| 1266 | - protected function _add_datetime_id_to_where_conditions(array $request) |
|
| 1267 | - { |
|
| 1268 | - $where = array(); |
|
| 1269 | - if (! empty($request['datetime_id'])) { |
|
| 1270 | - $where['Ticket.Datetime.DTT_ID'] = absint($request['datetime_id']); |
|
| 1271 | - } |
|
| 1272 | - if (! empty($request['DTT_ID'])) { |
|
| 1273 | - $where['Ticket.Datetime.DTT_ID'] = absint($request['DTT_ID']); |
|
| 1274 | - } |
|
| 1275 | - return $where; |
|
| 1276 | - } |
|
| 1277 | - |
|
| 1278 | - |
|
| 1279 | - /** |
|
| 1280 | - * Adds the correct registration status to the where conditions for the registrations query. |
|
| 1281 | - * |
|
| 1282 | - * @param array $request usually the same as $this->_req_data but not necessarily |
|
| 1283 | - * @return array |
|
| 1284 | - */ |
|
| 1285 | - protected function _add_registration_status_to_where_conditions(array $request) |
|
| 1286 | - { |
|
| 1287 | - $where = array(); |
|
| 1288 | - $view = EEH_Array::is_set($request, 'status', ''); |
|
| 1289 | - $registration_status = ! empty($request['_reg_status']) |
|
| 1290 | - ? sanitize_text_field($request['_reg_status']) |
|
| 1291 | - : ''; |
|
| 1292 | - |
|
| 1293 | - /* |
|
| 33 | + /** |
|
| 34 | + * @var EE_Registration |
|
| 35 | + */ |
|
| 36 | + private $_registration; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var EE_Event |
|
| 40 | + */ |
|
| 41 | + private $_reg_event; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var EE_Session |
|
| 45 | + */ |
|
| 46 | + private $_session; |
|
| 47 | + |
|
| 48 | + private static $_reg_status; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Form for displaying the custom questions for this registration. |
|
| 52 | + * This gets used a few times throughout the request so its best to cache it |
|
| 53 | + * |
|
| 54 | + * @var EE_Registration_Custom_Questions_Form |
|
| 55 | + */ |
|
| 56 | + protected $_reg_custom_questions_form = null; |
|
| 57 | + |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * constructor |
|
| 61 | + * |
|
| 62 | + * @Constructor |
|
| 63 | + * @access public |
|
| 64 | + * @param bool $routing |
|
| 65 | + * @return Registrations_Admin_Page |
|
| 66 | + */ |
|
| 67 | + public function __construct($routing = true) |
|
| 68 | + { |
|
| 69 | + parent::__construct($routing); |
|
| 70 | + add_action('wp_loaded', array($this, 'wp_loaded')); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + public function wp_loaded() |
|
| 75 | + { |
|
| 76 | + // when adding a new registration... |
|
| 77 | + if (isset($this->_req_data['action']) && $this->_req_data['action'] === 'new_registration') { |
|
| 78 | + EE_System::do_not_cache(); |
|
| 79 | + if (! isset($this->_req_data['processing_registration']) |
|
| 80 | + || absint($this->_req_data['processing_registration']) !== 1 |
|
| 81 | + ) { |
|
| 82 | + // and it's NOT the attendee information reg step |
|
| 83 | + // force cookie expiration by setting time to last week |
|
| 84 | + setcookie('ee_registration_added', 0, time() - WEEK_IN_SECONDS, '/'); |
|
| 85 | + // and update the global |
|
| 86 | + $_COOKIE['ee_registration_added'] = 0; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + protected function _init_page_props() |
|
| 93 | + { |
|
| 94 | + $this->page_slug = REG_PG_SLUG; |
|
| 95 | + $this->_admin_base_url = REG_ADMIN_URL; |
|
| 96 | + $this->_admin_base_path = REG_ADMIN; |
|
| 97 | + $this->page_label = esc_html__('Registrations', 'event_espresso'); |
|
| 98 | + $this->_cpt_routes = array( |
|
| 99 | + 'add_new_attendee' => 'espresso_attendees', |
|
| 100 | + 'edit_attendee' => 'espresso_attendees', |
|
| 101 | + 'insert_attendee' => 'espresso_attendees', |
|
| 102 | + 'update_attendee' => 'espresso_attendees', |
|
| 103 | + ); |
|
| 104 | + $this->_cpt_model_names = array( |
|
| 105 | + 'add_new_attendee' => 'EEM_Attendee', |
|
| 106 | + 'edit_attendee' => 'EEM_Attendee', |
|
| 107 | + ); |
|
| 108 | + $this->_cpt_edit_routes = array( |
|
| 109 | + 'espresso_attendees' => 'edit_attendee', |
|
| 110 | + ); |
|
| 111 | + $this->_pagenow_map = array( |
|
| 112 | + 'add_new_attendee' => 'post-new.php', |
|
| 113 | + 'edit_attendee' => 'post.php', |
|
| 114 | + 'trash' => 'post.php', |
|
| 115 | + ); |
|
| 116 | + add_action('edit_form_after_title', array($this, 'after_title_form_fields'), 10); |
|
| 117 | + //add filters so that the comment urls don't take users to a confusing 404 page |
|
| 118 | + add_filter('get_comment_link', array($this, 'clear_comment_link'), 10, 3); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + |
|
| 122 | + public function clear_comment_link($link, $comment, $args) |
|
| 123 | + { |
|
| 124 | + //gotta make sure this only happens on this route |
|
| 125 | + $post_type = get_post_type($comment->comment_post_ID); |
|
| 126 | + if ($post_type === 'espresso_attendees') { |
|
| 127 | + return '#commentsdiv'; |
|
| 128 | + } |
|
| 129 | + return $link; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + |
|
| 133 | + protected function _ajax_hooks() |
|
| 134 | + { |
|
| 135 | + //todo: all hooks for registrations ajax goes in here |
|
| 136 | + add_action('wp_ajax_toggle_checkin_status', array($this, 'toggle_checkin_status')); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + protected function _define_page_props() |
|
| 141 | + { |
|
| 142 | + $this->_admin_page_title = $this->page_label; |
|
| 143 | + $this->_labels = array( |
|
| 144 | + 'buttons' => array( |
|
| 145 | + 'add-registrant' => esc_html__('Add New Registration', 'event_espresso'), |
|
| 146 | + 'add-attendee' => esc_html__('Add Contact', 'event_espresso'), |
|
| 147 | + 'edit' => esc_html__('Edit Contact', 'event_espresso'), |
|
| 148 | + 'report' => esc_html__("Event Registrations CSV Report", "event_espresso"), |
|
| 149 | + 'report_all' => esc_html__('All Registrations CSV Report', 'event_espresso'), |
|
| 150 | + 'report_filtered' => esc_html__('Filtered CSV Report', 'event_espresso'), |
|
| 151 | + 'contact_list_report' => esc_html__('Contact List Report', 'event_espresso'), |
|
| 152 | + 'contact_list_export' => esc_html__("Export Data", "event_espresso"), |
|
| 153 | + ), |
|
| 154 | + 'publishbox' => array( |
|
| 155 | + 'add_new_attendee' => esc_html__("Add Contact Record", 'event_espresso'), |
|
| 156 | + 'edit_attendee' => esc_html__("Update Contact Record", 'event_espresso'), |
|
| 157 | + ), |
|
| 158 | + 'hide_add_button_on_cpt_route' => array( |
|
| 159 | + 'edit_attendee' => true, |
|
| 160 | + ), |
|
| 161 | + ); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * grab url requests and route them |
|
| 167 | + * |
|
| 168 | + * @access private |
|
| 169 | + * @return void |
|
| 170 | + */ |
|
| 171 | + public function _set_page_routes() |
|
| 172 | + { |
|
| 173 | + $this->_get_registration_status_array(); |
|
| 174 | + $reg_id = ! empty($this->_req_data['_REG_ID']) && ! is_array($this->_req_data['_REG_ID']) |
|
| 175 | + ? $this->_req_data['_REG_ID'] : 0; |
|
| 176 | + $reg_id = empty($reg_id) && ! empty($this->_req_data['reg_status_change_form']['REG_ID']) |
|
| 177 | + ? $this->_req_data['reg_status_change_form']['REG_ID'] |
|
| 178 | + : $reg_id; |
|
| 179 | + $att_id = ! empty($this->_req_data['ATT_ID']) && ! is_array($this->_req_data['ATT_ID']) |
|
| 180 | + ? $this->_req_data['ATT_ID'] : 0; |
|
| 181 | + $att_id = ! empty($this->_req_data['post']) && ! is_array($this->_req_data['post']) |
|
| 182 | + ? $this->_req_data['post'] |
|
| 183 | + : $att_id; |
|
| 184 | + $this->_page_routes = array( |
|
| 185 | + 'default' => array( |
|
| 186 | + 'func' => '_registrations_overview_list_table', |
|
| 187 | + 'capability' => 'ee_read_registrations', |
|
| 188 | + ), |
|
| 189 | + 'view_registration' => array( |
|
| 190 | + 'func' => '_registration_details', |
|
| 191 | + 'capability' => 'ee_read_registration', |
|
| 192 | + 'obj_id' => $reg_id, |
|
| 193 | + ), |
|
| 194 | + 'edit_registration' => array( |
|
| 195 | + 'func' => '_update_attendee_registration_form', |
|
| 196 | + 'noheader' => true, |
|
| 197 | + 'headers_sent_route' => 'view_registration', |
|
| 198 | + 'capability' => 'ee_edit_registration', |
|
| 199 | + 'obj_id' => $reg_id, |
|
| 200 | + '_REG_ID' => $reg_id, |
|
| 201 | + ), |
|
| 202 | + 'trash_registrations' => array( |
|
| 203 | + 'func' => '_trash_or_restore_registrations', |
|
| 204 | + 'args' => array('trash' => true), |
|
| 205 | + 'noheader' => true, |
|
| 206 | + 'capability' => 'ee_delete_registrations', |
|
| 207 | + ), |
|
| 208 | + 'restore_registrations' => array( |
|
| 209 | + 'func' => '_trash_or_restore_registrations', |
|
| 210 | + 'args' => array('trash' => false), |
|
| 211 | + 'noheader' => true, |
|
| 212 | + 'capability' => 'ee_delete_registrations', |
|
| 213 | + ), |
|
| 214 | + 'delete_registrations' => array( |
|
| 215 | + 'func' => '_delete_registrations', |
|
| 216 | + 'noheader' => true, |
|
| 217 | + 'capability' => 'ee_delete_registrations', |
|
| 218 | + ), |
|
| 219 | + 'new_registration' => array( |
|
| 220 | + 'func' => 'new_registration', |
|
| 221 | + 'capability' => 'ee_edit_registrations', |
|
| 222 | + ), |
|
| 223 | + 'process_reg_step' => array( |
|
| 224 | + 'func' => 'process_reg_step', |
|
| 225 | + 'noheader' => true, |
|
| 226 | + 'capability' => 'ee_edit_registrations', |
|
| 227 | + ), |
|
| 228 | + 'redirect_to_txn' => array( |
|
| 229 | + 'func' => 'redirect_to_txn', |
|
| 230 | + 'noheader' => true, |
|
| 231 | + 'capability' => 'ee_edit_registrations', |
|
| 232 | + ), |
|
| 233 | + 'change_reg_status' => array( |
|
| 234 | + 'func' => '_change_reg_status', |
|
| 235 | + 'noheader' => true, |
|
| 236 | + 'capability' => 'ee_edit_registration', |
|
| 237 | + 'obj_id' => $reg_id, |
|
| 238 | + ), |
|
| 239 | + 'approve_registration' => array( |
|
| 240 | + 'func' => 'approve_registration', |
|
| 241 | + 'noheader' => true, |
|
| 242 | + 'capability' => 'ee_edit_registration', |
|
| 243 | + 'obj_id' => $reg_id, |
|
| 244 | + ), |
|
| 245 | + 'approve_and_notify_registration' => array( |
|
| 246 | + 'func' => 'approve_registration', |
|
| 247 | + 'noheader' => true, |
|
| 248 | + 'args' => array(true), |
|
| 249 | + 'capability' => 'ee_edit_registration', |
|
| 250 | + 'obj_id' => $reg_id, |
|
| 251 | + ), |
|
| 252 | + 'approve_registrations' => array( |
|
| 253 | + 'func' => 'bulk_action_on_registrations', |
|
| 254 | + 'noheader' => true, |
|
| 255 | + 'capability' => 'ee_edit_registrations', |
|
| 256 | + 'args' => array('approve') |
|
| 257 | + ), |
|
| 258 | + 'approve_and_notify_registrations' => array( |
|
| 259 | + 'func' => 'bulk_action_on_registrations', |
|
| 260 | + 'noheader' => true, |
|
| 261 | + 'capability' => 'ee_edit_registrations', |
|
| 262 | + 'args' => array('approve', true) |
|
| 263 | + ), |
|
| 264 | + 'decline_registration' => array( |
|
| 265 | + 'func' => 'decline_registration', |
|
| 266 | + 'noheader' => true, |
|
| 267 | + 'capability' => 'ee_edit_registration', |
|
| 268 | + 'obj_id' => $reg_id, |
|
| 269 | + ), |
|
| 270 | + 'decline_and_notify_registration' => array( |
|
| 271 | + 'func' => 'decline_registration', |
|
| 272 | + 'noheader' => true, |
|
| 273 | + 'args' => array(true), |
|
| 274 | + 'capability' => 'ee_edit_registration', |
|
| 275 | + 'obj_id' => $reg_id, |
|
| 276 | + ), |
|
| 277 | + 'decline_registrations' => array( |
|
| 278 | + 'func' => 'bulk_action_on_registrations', |
|
| 279 | + 'noheader' => true, |
|
| 280 | + 'capability' => 'ee_edit_registrations', |
|
| 281 | + 'args' => array('decline') |
|
| 282 | + ), |
|
| 283 | + 'decline_and_notify_registrations' => array( |
|
| 284 | + 'func' => 'bulk_action_on_registrations', |
|
| 285 | + 'noheader' => true, |
|
| 286 | + 'capability' => 'ee_edit_registrations', |
|
| 287 | + 'args' => array('decline', true) |
|
| 288 | + ), |
|
| 289 | + 'pending_registration' => array( |
|
| 290 | + 'func' => 'pending_registration', |
|
| 291 | + 'noheader' => true, |
|
| 292 | + 'capability' => 'ee_edit_registration', |
|
| 293 | + 'obj_id' => $reg_id, |
|
| 294 | + ), |
|
| 295 | + 'pending_and_notify_registration' => array( |
|
| 296 | + 'func' => 'pending_registration', |
|
| 297 | + 'noheader' => true, |
|
| 298 | + 'args' => array(true), |
|
| 299 | + 'capability' => 'ee_edit_registration', |
|
| 300 | + 'obj_id' => $reg_id, |
|
| 301 | + ), |
|
| 302 | + 'pending_registrations' => array( |
|
| 303 | + 'func' => 'bulk_action_on_registrations', |
|
| 304 | + 'noheader' => true, |
|
| 305 | + 'capability' => 'ee_edit_registrations', |
|
| 306 | + 'args' => array('pending') |
|
| 307 | + ), |
|
| 308 | + 'pending_and_notify_registrations' => array( |
|
| 309 | + 'func' => 'bulk_action_on_registrations', |
|
| 310 | + 'noheader' => true, |
|
| 311 | + 'capability' => 'ee_edit_registrations', |
|
| 312 | + 'args' => array('pending', true) |
|
| 313 | + ), |
|
| 314 | + 'no_approve_registration' => array( |
|
| 315 | + 'func' => 'not_approve_registration', |
|
| 316 | + 'noheader' => true, |
|
| 317 | + 'capability' => 'ee_edit_registration', |
|
| 318 | + 'obj_id' => $reg_id, |
|
| 319 | + ), |
|
| 320 | + 'no_approve_and_notify_registration' => array( |
|
| 321 | + 'func' => 'not_approve_registration', |
|
| 322 | + 'noheader' => true, |
|
| 323 | + 'args' => array(true), |
|
| 324 | + 'capability' => 'ee_edit_registration', |
|
| 325 | + 'obj_id' => $reg_id, |
|
| 326 | + ), |
|
| 327 | + 'no_approve_registrations' => array( |
|
| 328 | + 'func' => 'bulk_action_on_registrations', |
|
| 329 | + 'noheader' => true, |
|
| 330 | + 'capability' => 'ee_edit_registrations', |
|
| 331 | + 'args' => array('not_approve') |
|
| 332 | + ), |
|
| 333 | + 'no_approve_and_notify_registrations' => array( |
|
| 334 | + 'func' => 'bulk_action_on_registrations', |
|
| 335 | + 'noheader' => true, |
|
| 336 | + 'capability' => 'ee_edit_registrations', |
|
| 337 | + 'args' => array('not_approve', true) |
|
| 338 | + ), |
|
| 339 | + 'cancel_registration' => array( |
|
| 340 | + 'func' => 'cancel_registration', |
|
| 341 | + 'noheader' => true, |
|
| 342 | + 'capability' => 'ee_edit_registration', |
|
| 343 | + 'obj_id' => $reg_id, |
|
| 344 | + ), |
|
| 345 | + 'cancel_and_notify_registration' => array( |
|
| 346 | + 'func' => 'cancel_registration', |
|
| 347 | + 'noheader' => true, |
|
| 348 | + 'args' => array(true), |
|
| 349 | + 'capability' => 'ee_edit_registration', |
|
| 350 | + 'obj_id' => $reg_id, |
|
| 351 | + ), |
|
| 352 | + 'cancel_registrations' => array( |
|
| 353 | + 'func' => 'bulk_action_on_registrations', |
|
| 354 | + 'noheader' => true, |
|
| 355 | + 'capability' => 'ee_edit_registrations', |
|
| 356 | + 'args' => array('cancel') |
|
| 357 | + ), |
|
| 358 | + 'cancel_and_notify_registrations' => array( |
|
| 359 | + 'func' => 'bulk_action_on_registrations', |
|
| 360 | + 'noheader' => true, |
|
| 361 | + 'capability' => 'ee_edit_registrations', |
|
| 362 | + 'args' => array('cancel', true) |
|
| 363 | + ), |
|
| 364 | + 'wait_list_registration' => array( |
|
| 365 | + 'func' => 'wait_list_registration', |
|
| 366 | + 'noheader' => true, |
|
| 367 | + 'capability' => 'ee_edit_registration', |
|
| 368 | + 'obj_id' => $reg_id, |
|
| 369 | + ), |
|
| 370 | + 'wait_list_and_notify_registration' => array( |
|
| 371 | + 'func' => 'wait_list_registration', |
|
| 372 | + 'noheader' => true, |
|
| 373 | + 'args' => array(true), |
|
| 374 | + 'capability' => 'ee_edit_registration', |
|
| 375 | + 'obj_id' => $reg_id, |
|
| 376 | + ), |
|
| 377 | + 'contact_list' => array( |
|
| 378 | + 'func' => '_attendee_contact_list_table', |
|
| 379 | + 'capability' => 'ee_read_contacts', |
|
| 380 | + ), |
|
| 381 | + 'add_new_attendee' => array( |
|
| 382 | + 'func' => '_create_new_cpt_item', |
|
| 383 | + 'args' => array( |
|
| 384 | + 'new_attendee' => true, |
|
| 385 | + 'capability' => 'ee_edit_contacts', |
|
| 386 | + ), |
|
| 387 | + ), |
|
| 388 | + 'edit_attendee' => array( |
|
| 389 | + 'func' => '_edit_cpt_item', |
|
| 390 | + 'capability' => 'ee_edit_contacts', |
|
| 391 | + 'obj_id' => $att_id, |
|
| 392 | + ), |
|
| 393 | + 'duplicate_attendee' => array( |
|
| 394 | + 'func' => '_duplicate_attendee', |
|
| 395 | + 'noheader' => true, |
|
| 396 | + 'capability' => 'ee_edit_contacts', |
|
| 397 | + 'obj_id' => $att_id, |
|
| 398 | + ), |
|
| 399 | + 'insert_attendee' => array( |
|
| 400 | + 'func' => '_insert_or_update_attendee', |
|
| 401 | + 'args' => array( |
|
| 402 | + 'new_attendee' => true, |
|
| 403 | + ), |
|
| 404 | + 'noheader' => true, |
|
| 405 | + 'capability' => 'ee_edit_contacts', |
|
| 406 | + ), |
|
| 407 | + 'update_attendee' => array( |
|
| 408 | + 'func' => '_insert_or_update_attendee', |
|
| 409 | + 'args' => array( |
|
| 410 | + 'new_attendee' => false, |
|
| 411 | + ), |
|
| 412 | + 'noheader' => true, |
|
| 413 | + 'capability' => 'ee_edit_contacts', |
|
| 414 | + 'obj_id' => $att_id, |
|
| 415 | + ), |
|
| 416 | + 'trash_attendees' => array( |
|
| 417 | + 'func' => '_trash_or_restore_attendees', |
|
| 418 | + 'args' => array( |
|
| 419 | + 'trash' => 'true' |
|
| 420 | + ), |
|
| 421 | + 'noheader' => true, |
|
| 422 | + 'capability' => 'ee_delete_contacts' |
|
| 423 | + ), |
|
| 424 | + 'trash_attendee' => array( |
|
| 425 | + 'func' => '_trash_or_restore_attendees', |
|
| 426 | + 'args' => array( |
|
| 427 | + 'trash' => true, |
|
| 428 | + ), |
|
| 429 | + 'noheader' => true, |
|
| 430 | + 'capability' => 'ee_delete_contacts', |
|
| 431 | + 'obj_id' => $att_id, |
|
| 432 | + ), |
|
| 433 | + 'restore_attendees' => array( |
|
| 434 | + 'func' => '_trash_or_restore_attendees', |
|
| 435 | + 'args' => array( |
|
| 436 | + 'trash' => false, |
|
| 437 | + ), |
|
| 438 | + 'noheader' => true, |
|
| 439 | + 'capability' => 'ee_delete_contacts', |
|
| 440 | + 'obj_id' => $att_id, |
|
| 441 | + ), |
|
| 442 | + 'resend_registration' => array( |
|
| 443 | + 'func' => '_resend_registration', |
|
| 444 | + 'noheader' => true, |
|
| 445 | + 'capability' => 'ee_send_message', |
|
| 446 | + ), |
|
| 447 | + 'registrations_report' => array( |
|
| 448 | + 'func' => '_registrations_report', |
|
| 449 | + 'noheader' => true, |
|
| 450 | + 'capability' => 'ee_read_registrations', |
|
| 451 | + ), |
|
| 452 | + 'contact_list_export' => array( |
|
| 453 | + 'func' => '_contact_list_export', |
|
| 454 | + 'noheader' => true, |
|
| 455 | + 'capability' => 'export', |
|
| 456 | + ), |
|
| 457 | + 'contact_list_report' => array( |
|
| 458 | + 'func' => '_contact_list_report', |
|
| 459 | + 'noheader' => true, |
|
| 460 | + 'capability' => 'ee_read_contacts', |
|
| 461 | + ), |
|
| 462 | + ); |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + |
|
| 466 | + protected function _set_page_config() |
|
| 467 | + { |
|
| 468 | + $this->_page_config = array( |
|
| 469 | + 'default' => array( |
|
| 470 | + 'nav' => array( |
|
| 471 | + 'label' => esc_html__('Overview', 'event_espresso'), |
|
| 472 | + 'order' => 5, |
|
| 473 | + ), |
|
| 474 | + 'help_tabs' => array( |
|
| 475 | + 'registrations_overview_help_tab' => array( |
|
| 476 | + 'title' => esc_html__('Registrations Overview', 'event_espresso'), |
|
| 477 | + 'filename' => 'registrations_overview', |
|
| 478 | + ), |
|
| 479 | + 'registrations_overview_table_column_headings_help_tab' => array( |
|
| 480 | + 'title' => esc_html__('Registrations Table Column Headings', 'event_espresso'), |
|
| 481 | + 'filename' => 'registrations_overview_table_column_headings', |
|
| 482 | + ), |
|
| 483 | + 'registrations_overview_filters_help_tab' => array( |
|
| 484 | + 'title' => esc_html__('Registration Filters', 'event_espresso'), |
|
| 485 | + 'filename' => 'registrations_overview_filters', |
|
| 486 | + ), |
|
| 487 | + 'registrations_overview_views_help_tab' => array( |
|
| 488 | + 'title' => esc_html__('Registration Views', 'event_espresso'), |
|
| 489 | + 'filename' => 'registrations_overview_views', |
|
| 490 | + ), |
|
| 491 | + 'registrations_regoverview_other_help_tab' => array( |
|
| 492 | + 'title' => esc_html__('Registrations Other', 'event_espresso'), |
|
| 493 | + 'filename' => 'registrations_overview_other', |
|
| 494 | + ), |
|
| 495 | + ), |
|
| 496 | + 'help_tour' => array('Registration_Overview_Help_Tour'), |
|
| 497 | + 'qtips' => array('Registration_List_Table_Tips'), |
|
| 498 | + 'list_table' => 'EE_Registrations_List_Table', |
|
| 499 | + 'require_nonce' => false, |
|
| 500 | + ), |
|
| 501 | + 'view_registration' => array( |
|
| 502 | + 'nav' => array( |
|
| 503 | + 'label' => esc_html__('REG Details', 'event_espresso'), |
|
| 504 | + 'order' => 15, |
|
| 505 | + 'url' => isset($this->_req_data['_REG_ID']) |
|
| 506 | + ? add_query_arg(array('_REG_ID' => $this->_req_data['_REG_ID']), $this->_current_page_view_url) |
|
| 507 | + : $this->_admin_base_url, |
|
| 508 | + 'persistent' => false, |
|
| 509 | + ), |
|
| 510 | + 'help_tabs' => array( |
|
| 511 | + 'registrations_details_help_tab' => array( |
|
| 512 | + 'title' => esc_html__('Registration Details', 'event_espresso'), |
|
| 513 | + 'filename' => 'registrations_details', |
|
| 514 | + ), |
|
| 515 | + 'registrations_details_table_help_tab' => array( |
|
| 516 | + 'title' => esc_html__('Registration Details Table', 'event_espresso'), |
|
| 517 | + 'filename' => 'registrations_details_table', |
|
| 518 | + ), |
|
| 519 | + 'registrations_details_form_answers_help_tab' => array( |
|
| 520 | + 'title' => esc_html__('Registration Form Answers', 'event_espresso'), |
|
| 521 | + 'filename' => 'registrations_details_form_answers', |
|
| 522 | + ), |
|
| 523 | + 'registrations_details_registrant_details_help_tab' => array( |
|
| 524 | + 'title' => esc_html__('Contact Details', 'event_espresso'), |
|
| 525 | + 'filename' => 'registrations_details_registrant_details', |
|
| 526 | + ), |
|
| 527 | + ), |
|
| 528 | + 'help_tour' => array('Registration_Details_Help_Tour'), |
|
| 529 | + 'metaboxes' => array_merge( |
|
| 530 | + $this->_default_espresso_metaboxes, |
|
| 531 | + array('_registration_details_metaboxes') |
|
| 532 | + ), |
|
| 533 | + 'require_nonce' => false, |
|
| 534 | + ), |
|
| 535 | + 'new_registration' => array( |
|
| 536 | + 'nav' => array( |
|
| 537 | + 'label' => esc_html__('Add New Registration', 'event_espresso'), |
|
| 538 | + 'url' => '#', |
|
| 539 | + 'order' => 15, |
|
| 540 | + 'persistent' => false, |
|
| 541 | + ), |
|
| 542 | + 'metaboxes' => $this->_default_espresso_metaboxes, |
|
| 543 | + 'labels' => array( |
|
| 544 | + 'publishbox' => esc_html__('Save Registration', 'event_espresso'), |
|
| 545 | + ), |
|
| 546 | + 'require_nonce' => false, |
|
| 547 | + ), |
|
| 548 | + 'add_new_attendee' => array( |
|
| 549 | + 'nav' => array( |
|
| 550 | + 'label' => esc_html__('Add Contact', 'event_espresso'), |
|
| 551 | + 'order' => 15, |
|
| 552 | + 'persistent' => false, |
|
| 553 | + ), |
|
| 554 | + 'metaboxes' => array_merge( |
|
| 555 | + $this->_default_espresso_metaboxes, |
|
| 556 | + array('_publish_post_box', 'attendee_editor_metaboxes') |
|
| 557 | + ), |
|
| 558 | + 'require_nonce' => false, |
|
| 559 | + ), |
|
| 560 | + 'edit_attendee' => array( |
|
| 561 | + 'nav' => array( |
|
| 562 | + 'label' => esc_html__('Edit Contact', 'event_espresso'), |
|
| 563 | + 'order' => 15, |
|
| 564 | + 'persistent' => false, |
|
| 565 | + 'url' => isset($this->_req_data['ATT_ID']) |
|
| 566 | + ? add_query_arg(array('ATT_ID' => $this->_req_data['ATT_ID']), $this->_current_page_view_url) |
|
| 567 | + : $this->_admin_base_url, |
|
| 568 | + ), |
|
| 569 | + 'metaboxes' => array('attendee_editor_metaboxes'), |
|
| 570 | + 'require_nonce' => false, |
|
| 571 | + ), |
|
| 572 | + 'contact_list' => array( |
|
| 573 | + 'nav' => array( |
|
| 574 | + 'label' => esc_html__('Contact List', 'event_espresso'), |
|
| 575 | + 'order' => 20, |
|
| 576 | + ), |
|
| 577 | + 'list_table' => 'EE_Attendee_Contact_List_Table', |
|
| 578 | + 'help_tabs' => array( |
|
| 579 | + 'registrations_contact_list_help_tab' => array( |
|
| 580 | + 'title' => esc_html__('Registrations Contact List', 'event_espresso'), |
|
| 581 | + 'filename' => 'registrations_contact_list', |
|
| 582 | + ), |
|
| 583 | + 'registrations_contact-list_table_column_headings_help_tab' => array( |
|
| 584 | + 'title' => esc_html__('Contact List Table Column Headings', 'event_espresso'), |
|
| 585 | + 'filename' => 'registrations_contact_list_table_column_headings', |
|
| 586 | + ), |
|
| 587 | + 'registrations_contact_list_views_help_tab' => array( |
|
| 588 | + 'title' => esc_html__('Contact List Views', 'event_espresso'), |
|
| 589 | + 'filename' => 'registrations_contact_list_views', |
|
| 590 | + ), |
|
| 591 | + 'registrations_contact_list_other_help_tab' => array( |
|
| 592 | + 'title' => esc_html__('Contact List Other', 'event_espresso'), |
|
| 593 | + 'filename' => 'registrations_contact_list_other', |
|
| 594 | + ), |
|
| 595 | + ), |
|
| 596 | + 'help_tour' => array('Contact_List_Help_Tour'), |
|
| 597 | + 'metaboxes' => array(), |
|
| 598 | + 'require_nonce' => false, |
|
| 599 | + ), |
|
| 600 | + //override default cpt routes |
|
| 601 | + 'create_new' => '', |
|
| 602 | + 'edit' => '', |
|
| 603 | + ); |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * The below methods aren't used by this class currently |
|
| 609 | + */ |
|
| 610 | + protected function _add_screen_options() |
|
| 611 | + { |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + |
|
| 615 | + protected function _add_feature_pointers() |
|
| 616 | + { |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + |
|
| 620 | + public function admin_init() |
|
| 621 | + { |
|
| 622 | + EE_Registry::$i18n_js_strings['update_att_qstns'] = esc_html__( |
|
| 623 | + 'click "Update Registration Questions" to save your changes', |
|
| 624 | + 'event_espresso' |
|
| 625 | + ); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + |
|
| 629 | + public function admin_notices() |
|
| 630 | + { |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + |
|
| 634 | + public function admin_footer_scripts() |
|
| 635 | + { |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * get list of registration statuses |
|
| 641 | + * |
|
| 642 | + * @access private |
|
| 643 | + * @return void |
|
| 644 | + * @throws EE_Error |
|
| 645 | + */ |
|
| 646 | + private function _get_registration_status_array() |
|
| 647 | + { |
|
| 648 | + self::$_reg_status = EEM_Registration::reg_status_array(array(), true); |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + |
|
| 652 | + protected function _add_screen_options_default() |
|
| 653 | + { |
|
| 654 | + $this->_per_page_screen_option(); |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + |
|
| 658 | + protected function _add_screen_options_contact_list() |
|
| 659 | + { |
|
| 660 | + $page_title = $this->_admin_page_title; |
|
| 661 | + $this->_admin_page_title = esc_html__("Contacts", 'event_espresso'); |
|
| 662 | + $this->_per_page_screen_option(); |
|
| 663 | + $this->_admin_page_title = $page_title; |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + |
|
| 667 | + public function load_scripts_styles() |
|
| 668 | + { |
|
| 669 | + //style |
|
| 670 | + wp_register_style( |
|
| 671 | + 'espresso_reg', |
|
| 672 | + REG_ASSETS_URL . 'espresso_registrations_admin.css', |
|
| 673 | + array('ee-admin-css'), |
|
| 674 | + EVENT_ESPRESSO_VERSION |
|
| 675 | + ); |
|
| 676 | + wp_enqueue_style('espresso_reg'); |
|
| 677 | + //script |
|
| 678 | + wp_register_script( |
|
| 679 | + 'espresso_reg', |
|
| 680 | + REG_ASSETS_URL . 'espresso_registrations_admin.js', |
|
| 681 | + array('jquery-ui-datepicker', 'jquery-ui-draggable', 'ee_admin_js'), |
|
| 682 | + EVENT_ESPRESSO_VERSION, |
|
| 683 | + true |
|
| 684 | + ); |
|
| 685 | + wp_enqueue_script('espresso_reg'); |
|
| 686 | + } |
|
| 687 | + |
|
| 688 | + |
|
| 689 | + public function load_scripts_styles_edit_attendee() |
|
| 690 | + { |
|
| 691 | + //stuff to only show up on our attendee edit details page. |
|
| 692 | + $attendee_details_translations = array( |
|
| 693 | + 'att_publish_text' => sprintf( |
|
| 694 | + esc_html__('Created on: <b>%1$s</b>', 'event_espresso'), |
|
| 695 | + $this->_cpt_model_obj->get_datetime('ATT_created') |
|
| 696 | + ), |
|
| 697 | + ); |
|
| 698 | + wp_localize_script('espresso_reg', 'ATTENDEE_DETAILS', $attendee_details_translations); |
|
| 699 | + wp_enqueue_script('jquery-validate'); |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + |
|
| 703 | + public function load_scripts_styles_view_registration() |
|
| 704 | + { |
|
| 705 | + //styles |
|
| 706 | + wp_enqueue_style('espresso-ui-theme'); |
|
| 707 | + //scripts |
|
| 708 | + $this->_get_reg_custom_questions_form($this->_registration->ID()); |
|
| 709 | + $this->_reg_custom_questions_form->wp_enqueue_scripts(true); |
|
| 710 | + } |
|
| 711 | + |
|
| 712 | + |
|
| 713 | + public function load_scripts_styles_contact_list() |
|
| 714 | + { |
|
| 715 | + wp_deregister_style('espresso_reg'); |
|
| 716 | + wp_register_style( |
|
| 717 | + 'espresso_att', |
|
| 718 | + REG_ASSETS_URL . 'espresso_attendees_admin.css', |
|
| 719 | + array('ee-admin-css'), |
|
| 720 | + EVENT_ESPRESSO_VERSION |
|
| 721 | + ); |
|
| 722 | + wp_enqueue_style('espresso_att'); |
|
| 723 | + } |
|
| 724 | + |
|
| 725 | + |
|
| 726 | + public function load_scripts_styles_new_registration() |
|
| 727 | + { |
|
| 728 | + wp_register_script( |
|
| 729 | + 'ee-spco-for-admin', |
|
| 730 | + REG_ASSETS_URL . 'spco_for_admin.js', |
|
| 731 | + array('underscore', 'jquery'), |
|
| 732 | + EVENT_ESPRESSO_VERSION, |
|
| 733 | + true |
|
| 734 | + ); |
|
| 735 | + wp_enqueue_script('ee-spco-for-admin'); |
|
| 736 | + add_filter('FHEE__EED_Ticket_Selector__load_tckt_slctr_assets', '__return_true'); |
|
| 737 | + EE_Form_Section_Proper::wp_enqueue_scripts(); |
|
| 738 | + EED_Ticket_Selector::load_tckt_slctr_assets(); |
|
| 739 | + EE_Datepicker_Input::enqueue_styles_and_scripts(); |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + |
|
| 743 | + public function AHEE__EE_Admin_Page__route_admin_request_resend_registration() |
|
| 744 | + { |
|
| 745 | + add_filter('FHEE_load_EE_messages', '__return_true'); |
|
| 746 | + } |
|
| 747 | + |
|
| 748 | + |
|
| 749 | + public function AHEE__EE_Admin_Page__route_admin_request_approve_registration() |
|
| 750 | + { |
|
| 751 | + add_filter('FHEE_load_EE_messages', '__return_true'); |
|
| 752 | + } |
|
| 753 | + |
|
| 754 | + |
|
| 755 | + protected function _set_list_table_views_default() |
|
| 756 | + { |
|
| 757 | + //for notification related bulk actions we need to make sure only active messengers have an option. |
|
| 758 | + EED_Messages::set_autoloaders(); |
|
| 759 | + /** @type EE_Message_Resource_Manager $message_resource_manager */ |
|
| 760 | + $message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
|
| 761 | + $active_mts = $message_resource_manager->list_of_active_message_types(); |
|
| 762 | + //key= bulk_action_slug, value= message type. |
|
| 763 | + $match_array = array( |
|
| 764 | + 'approve_registrations' => 'registration', |
|
| 765 | + 'decline_registrations' => 'declined_registration', |
|
| 766 | + 'pending_registrations' => 'pending_approval', |
|
| 767 | + 'no_approve_registrations' => 'not_approved_registration', |
|
| 768 | + 'cancel_registrations' => 'cancelled_registration', |
|
| 769 | + ); |
|
| 770 | + $can_send = EE_Registry::instance()->CAP->current_user_can( |
|
| 771 | + 'ee_send_message', |
|
| 772 | + 'batch_send_messages' |
|
| 773 | + ); |
|
| 774 | + /** setup reg status bulk actions **/ |
|
| 775 | + $def_reg_status_actions['approve_registrations'] = esc_html__('Approve Registrations', 'event_espresso'); |
|
| 776 | + if ($can_send && in_array($match_array['approve_registrations'], $active_mts, true)) { |
|
| 777 | + $def_reg_status_actions['approve_and_notify_registrations'] = esc_html__( |
|
| 778 | + 'Approve and Notify Registrations', |
|
| 779 | + 'event_espresso' |
|
| 780 | + ); |
|
| 781 | + } |
|
| 782 | + $def_reg_status_actions['decline_registrations'] = esc_html__('Decline Registrations', 'event_espresso'); |
|
| 783 | + if ($can_send && in_array($match_array['decline_registrations'], $active_mts, true)) { |
|
| 784 | + $def_reg_status_actions['decline_and_notify_registrations'] = esc_html__( |
|
| 785 | + 'Decline and Notify Registrations', |
|
| 786 | + 'event_espresso' |
|
| 787 | + ); |
|
| 788 | + } |
|
| 789 | + $def_reg_status_actions['pending_registrations'] = esc_html__( |
|
| 790 | + 'Set Registrations to Pending Payment', |
|
| 791 | + 'event_espresso' |
|
| 792 | + ); |
|
| 793 | + if ($can_send && in_array($match_array['pending_registrations'], $active_mts, true)) { |
|
| 794 | + $def_reg_status_actions['pending_and_notify_registrations'] = esc_html__( |
|
| 795 | + 'Set Registrations to Pending Payment and Notify', |
|
| 796 | + 'event_espresso' |
|
| 797 | + ); |
|
| 798 | + } |
|
| 799 | + $def_reg_status_actions['no_approve_registrations'] = esc_html__( |
|
| 800 | + 'Set Registrations to Not Approved', |
|
| 801 | + 'event_espresso' |
|
| 802 | + ); |
|
| 803 | + if ($can_send && in_array($match_array['no_approve_registrations'], $active_mts, true)) { |
|
| 804 | + $def_reg_status_actions['no_approve_and_notify_registrations'] = esc_html__( |
|
| 805 | + 'Set Registrations to Not Approved and Notify', |
|
| 806 | + 'event_espresso' |
|
| 807 | + ); |
|
| 808 | + } |
|
| 809 | + $def_reg_status_actions['cancel_registrations'] = esc_html__('Cancel Registrations', 'event_espresso'); |
|
| 810 | + if ($can_send && in_array($match_array['cancel_registrations'], $active_mts, true)) { |
|
| 811 | + $def_reg_status_actions['cancel_and_notify_registrations'] = esc_html__( |
|
| 812 | + 'Cancel Registrations and Notify', |
|
| 813 | + 'event_espresso' |
|
| 814 | + ); |
|
| 815 | + } |
|
| 816 | + $def_reg_status_actions = apply_filters( |
|
| 817 | + 'FHEE__Registrations_Admin_Page___set_list_table_views_default__def_reg_status_actions_array', |
|
| 818 | + $def_reg_status_actions, |
|
| 819 | + $active_mts, |
|
| 820 | + $can_send |
|
| 821 | + ); |
|
| 822 | + |
|
| 823 | + $this->_views = array( |
|
| 824 | + 'all' => array( |
|
| 825 | + 'slug' => 'all', |
|
| 826 | + 'label' => esc_html__('View All Registrations', 'event_espresso'), |
|
| 827 | + 'count' => 0, |
|
| 828 | + 'bulk_action' => array_merge($def_reg_status_actions, array( |
|
| 829 | + 'trash_registrations' => esc_html__('Trash Registrations', 'event_espresso'), |
|
| 830 | + )), |
|
| 831 | + ), |
|
| 832 | + 'month' => array( |
|
| 833 | + 'slug' => 'month', |
|
| 834 | + 'label' => esc_html__('This Month', 'event_espresso'), |
|
| 835 | + 'count' => 0, |
|
| 836 | + 'bulk_action' => array_merge($def_reg_status_actions, array( |
|
| 837 | + 'trash_registrations' => esc_html__('Trash Registrations', 'event_espresso'), |
|
| 838 | + )), |
|
| 839 | + ), |
|
| 840 | + 'today' => array( |
|
| 841 | + 'slug' => 'today', |
|
| 842 | + 'label' => sprintf( |
|
| 843 | + esc_html__('Today - %s', 'event_espresso'), |
|
| 844 | + date('M d, Y', current_time('timestamp')) |
|
| 845 | + ), |
|
| 846 | + 'count' => 0, |
|
| 847 | + 'bulk_action' => array_merge($def_reg_status_actions, array( |
|
| 848 | + 'trash_registrations' => esc_html__('Trash Registrations', 'event_espresso'), |
|
| 849 | + )), |
|
| 850 | + ), |
|
| 851 | + ); |
|
| 852 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
| 853 | + 'ee_delete_registrations', |
|
| 854 | + 'espresso_registrations_delete_registration' |
|
| 855 | + )) { |
|
| 856 | + $this->_views['incomplete'] = array( |
|
| 857 | + 'slug' => 'incomplete', |
|
| 858 | + 'label' => esc_html__('Incomplete', 'event_espresso'), |
|
| 859 | + 'count' => 0, |
|
| 860 | + 'bulk_action' => array( |
|
| 861 | + 'trash_registrations' => esc_html__('Trash Registrations', 'event_espresso'), |
|
| 862 | + ), |
|
| 863 | + ); |
|
| 864 | + $this->_views['trash'] = array( |
|
| 865 | + 'slug' => 'trash', |
|
| 866 | + 'label' => esc_html__('Trash', 'event_espresso'), |
|
| 867 | + 'count' => 0, |
|
| 868 | + 'bulk_action' => array( |
|
| 869 | + 'restore_registrations' => esc_html__('Restore Registrations', 'event_espresso'), |
|
| 870 | + 'delete_registrations' => esc_html__('Delete Registrations Permanently', 'event_espresso'), |
|
| 871 | + ), |
|
| 872 | + ); |
|
| 873 | + } |
|
| 874 | + } |
|
| 875 | + |
|
| 876 | + |
|
| 877 | + protected function _set_list_table_views_contact_list() |
|
| 878 | + { |
|
| 879 | + $this->_views = array( |
|
| 880 | + 'in_use' => array( |
|
| 881 | + 'slug' => 'in_use', |
|
| 882 | + 'label' => esc_html__('In Use', 'event_espresso'), |
|
| 883 | + 'count' => 0, |
|
| 884 | + 'bulk_action' => array( |
|
| 885 | + 'trash_attendees' => esc_html__('Move to Trash', 'event_espresso'), |
|
| 886 | + ), |
|
| 887 | + ), |
|
| 888 | + ); |
|
| 889 | + if (EE_Registry::instance()->CAP->current_user_can('ee_delete_contacts', |
|
| 890 | + 'espresso_registrations_trash_attendees') |
|
| 891 | + ) { |
|
| 892 | + $this->_views['trash'] = array( |
|
| 893 | + 'slug' => 'trash', |
|
| 894 | + 'label' => esc_html__('Trash', 'event_espresso'), |
|
| 895 | + 'count' => 0, |
|
| 896 | + 'bulk_action' => array( |
|
| 897 | + 'restore_attendees' => esc_html__('Restore from Trash', 'event_espresso'), |
|
| 898 | + ), |
|
| 899 | + ); |
|
| 900 | + } |
|
| 901 | + } |
|
| 902 | + |
|
| 903 | + |
|
| 904 | + protected function _registration_legend_items() |
|
| 905 | + { |
|
| 906 | + $fc_items = array( |
|
| 907 | + 'star-icon' => array( |
|
| 908 | + 'class' => 'dashicons dashicons-star-filled lt-blue-icon ee-icon-size-8', |
|
| 909 | + 'desc' => esc_html__('This is the Primary Registrant', 'event_espresso'), |
|
| 910 | + ), |
|
| 911 | + 'view_details' => array( |
|
| 912 | + 'class' => 'dashicons dashicons-clipboard', |
|
| 913 | + 'desc' => esc_html__('View Registration Details', 'event_espresso'), |
|
| 914 | + ), |
|
| 915 | + 'edit_attendee' => array( |
|
| 916 | + 'class' => 'ee-icon ee-icon-user-edit ee-icon-size-16', |
|
| 917 | + 'desc' => esc_html__('Edit Contact Details', 'event_espresso'), |
|
| 918 | + ), |
|
| 919 | + 'view_transaction' => array( |
|
| 920 | + 'class' => 'dashicons dashicons-cart', |
|
| 921 | + 'desc' => esc_html__('View Transaction Details', 'event_espresso'), |
|
| 922 | + ), |
|
| 923 | + 'view_invoice' => array( |
|
| 924 | + 'class' => 'dashicons dashicons-media-spreadsheet', |
|
| 925 | + 'desc' => esc_html__('View Transaction Invoice', 'event_espresso'), |
|
| 926 | + ), |
|
| 927 | + ); |
|
| 928 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
| 929 | + 'ee_send_message', |
|
| 930 | + 'espresso_registrations_resend_registration' |
|
| 931 | + )) { |
|
| 932 | + $fc_items['resend_registration'] = array( |
|
| 933 | + 'class' => 'dashicons dashicons-email-alt', |
|
| 934 | + 'desc' => esc_html__('Resend Registration Details', 'event_espresso'), |
|
| 935 | + ); |
|
| 936 | + } else { |
|
| 937 | + $fc_items['blank'] = array('class' => 'blank', 'desc' => ''); |
|
| 938 | + } |
|
| 939 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
| 940 | + 'ee_read_global_messages', |
|
| 941 | + 'view_filtered_messages' |
|
| 942 | + )) { |
|
| 943 | + $related_for_icon = EEH_MSG_Template::get_message_action_icon('see_notifications_for'); |
|
| 944 | + if (isset($related_for_icon['css_class']) && isset($related_for_icon['label'])) { |
|
| 945 | + $fc_items['view_related_messages'] = array( |
|
| 946 | + 'class' => $related_for_icon['css_class'], |
|
| 947 | + 'desc' => $related_for_icon['label'], |
|
| 948 | + ); |
|
| 949 | + } |
|
| 950 | + } |
|
| 951 | + $sc_items = array( |
|
| 952 | + 'approved_status' => array( |
|
| 953 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_approved, |
|
| 954 | + 'desc' => EEH_Template::pretty_status( |
|
| 955 | + EEM_Registration::status_id_approved, |
|
| 956 | + false, |
|
| 957 | + 'sentence' |
|
| 958 | + ), |
|
| 959 | + ), |
|
| 960 | + 'pending_status' => array( |
|
| 961 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_pending_payment, |
|
| 962 | + 'desc' => EEH_Template::pretty_status( |
|
| 963 | + EEM_Registration::status_id_pending_payment, |
|
| 964 | + false, |
|
| 965 | + 'sentence' |
|
| 966 | + ), |
|
| 967 | + ), |
|
| 968 | + 'wait_list' => array( |
|
| 969 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_wait_list, |
|
| 970 | + 'desc' => EEH_Template::pretty_status( |
|
| 971 | + EEM_Registration::status_id_wait_list, |
|
| 972 | + false, |
|
| 973 | + 'sentence' |
|
| 974 | + ), |
|
| 975 | + ), |
|
| 976 | + 'incomplete_status' => array( |
|
| 977 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_incomplete, |
|
| 978 | + 'desc' => EEH_Template::pretty_status( |
|
| 979 | + EEM_Registration::status_id_incomplete, |
|
| 980 | + false, |
|
| 981 | + 'sentence' |
|
| 982 | + ), |
|
| 983 | + ), |
|
| 984 | + 'not_approved' => array( |
|
| 985 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_not_approved, |
|
| 986 | + 'desc' => EEH_Template::pretty_status( |
|
| 987 | + EEM_Registration::status_id_not_approved, |
|
| 988 | + false, |
|
| 989 | + 'sentence' |
|
| 990 | + ), |
|
| 991 | + ), |
|
| 992 | + 'declined_status' => array( |
|
| 993 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_declined, |
|
| 994 | + 'desc' => EEH_Template::pretty_status( |
|
| 995 | + EEM_Registration::status_id_declined, |
|
| 996 | + false, |
|
| 997 | + 'sentence' |
|
| 998 | + ), |
|
| 999 | + ), |
|
| 1000 | + 'cancelled_status' => array( |
|
| 1001 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_cancelled, |
|
| 1002 | + 'desc' => EEH_Template::pretty_status( |
|
| 1003 | + EEM_Registration::status_id_cancelled, |
|
| 1004 | + false, |
|
| 1005 | + 'sentence' |
|
| 1006 | + ), |
|
| 1007 | + ), |
|
| 1008 | + ); |
|
| 1009 | + return array_merge($fc_items, $sc_items); |
|
| 1010 | + } |
|
| 1011 | + |
|
| 1012 | + |
|
| 1013 | + |
|
| 1014 | + /*************************************** REGISTRATION OVERVIEW **************************************/ |
|
| 1015 | + /** |
|
| 1016 | + * @throws \EE_Error |
|
| 1017 | + */ |
|
| 1018 | + protected function _registrations_overview_list_table() |
|
| 1019 | + { |
|
| 1020 | + $this->_template_args['admin_page_header'] = ''; |
|
| 1021 | + $EVT_ID = ! empty($this->_req_data['event_id']) |
|
| 1022 | + ? absint($this->_req_data['event_id']) |
|
| 1023 | + : 0; |
|
| 1024 | + $ATT_ID = !empty($this->_req_data['ATT_ID']) |
|
| 1025 | + ? absint($this->_req_data['ATT_ID']) |
|
| 1026 | + : 0; |
|
| 1027 | + if ($ATT_ID) { |
|
| 1028 | + $attendee = EEM_Attendee::instance()->get_one_by_ID($ATT_ID); |
|
| 1029 | + if ($attendee instanceof EE_Attendee) { |
|
| 1030 | + $this->_template_args['admin_page_header'] = sprintf( |
|
| 1031 | + esc_html__( |
|
| 1032 | + '%1$s Viewing registrations for %2$s%3$s', |
|
| 1033 | + 'event_espresso' |
|
| 1034 | + ), |
|
| 1035 | + '<h3 style="line-height:1.5em;">', |
|
| 1036 | + '<a href="' . EE_Admin_Page::add_query_args_and_nonce( |
|
| 1037 | + array( |
|
| 1038 | + 'action' => 'edit_attendee', |
|
| 1039 | + 'post' => $ATT_ID |
|
| 1040 | + ), |
|
| 1041 | + REG_ADMIN_URL |
|
| 1042 | + ) . '">' . $attendee->full_name() . '</a>', |
|
| 1043 | + '</h3>' |
|
| 1044 | + ); |
|
| 1045 | + } |
|
| 1046 | + } |
|
| 1047 | + if ($EVT_ID) { |
|
| 1048 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
| 1049 | + 'ee_edit_registrations', |
|
| 1050 | + 'espresso_registrations_new_registration', |
|
| 1051 | + $EVT_ID |
|
| 1052 | + )) { |
|
| 1053 | + $this->_admin_page_title .= ' ' . $this->get_action_link_or_button( |
|
| 1054 | + 'new_registration', |
|
| 1055 | + 'add-registrant', |
|
| 1056 | + array('event_id' => $EVT_ID), |
|
| 1057 | + 'add-new-h2' |
|
| 1058 | + ); |
|
| 1059 | + } |
|
| 1060 | + $event = EEM_Event::instance()->get_one_by_ID($EVT_ID); |
|
| 1061 | + if ($event instanceof EE_Event) { |
|
| 1062 | + $this->_template_args['admin_page_header'] = sprintf( |
|
| 1063 | + esc_html__( |
|
| 1064 | + '%s Viewing registrations for the event: %s%s', |
|
| 1065 | + 'event_espresso' |
|
| 1066 | + ), |
|
| 1067 | + '<h3 style="line-height:1.5em;">', |
|
| 1068 | + '<br /><a href="' |
|
| 1069 | + . EE_Admin_Page::add_query_args_and_nonce( |
|
| 1070 | + array( |
|
| 1071 | + 'action' => 'edit', |
|
| 1072 | + 'post' => $event->ID(), |
|
| 1073 | + ), |
|
| 1074 | + EVENTS_ADMIN_URL |
|
| 1075 | + ) |
|
| 1076 | + . '"> ' |
|
| 1077 | + . $event->get('EVT_name') |
|
| 1078 | + . ' </a> ', |
|
| 1079 | + '</h3>' |
|
| 1080 | + ); |
|
| 1081 | + } |
|
| 1082 | + $DTT_ID = ! empty($this->_req_data['datetime_id']) ? absint($this->_req_data['datetime_id']) : 0; |
|
| 1083 | + $datetime = EEM_Datetime::instance()->get_one_by_ID($DTT_ID); |
|
| 1084 | + if ($datetime instanceof EE_Datetime && $this->_template_args['admin_page_header'] !== '') { |
|
| 1085 | + $this->_template_args['admin_page_header'] = substr( |
|
| 1086 | + $this->_template_args['admin_page_header'], |
|
| 1087 | + 0, |
|
| 1088 | + -5 |
|
| 1089 | + ); |
|
| 1090 | + $this->_template_args['admin_page_header'] .= ' <span class="drk-grey-text">'; |
|
| 1091 | + $this->_template_args['admin_page_header'] .= '<span class="dashicons dashicons-calendar"></span>'; |
|
| 1092 | + $this->_template_args['admin_page_header'] .= $datetime->name(); |
|
| 1093 | + $this->_template_args['admin_page_header'] .= ' ( ' . $datetime->start_date() . ' )'; |
|
| 1094 | + $this->_template_args['admin_page_header'] .= '</span></h3>'; |
|
| 1095 | + } |
|
| 1096 | + } |
|
| 1097 | + $this->_template_args['after_list_table'] = $this->_display_legend($this->_registration_legend_items()); |
|
| 1098 | + $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 1099 | + } |
|
| 1100 | + |
|
| 1101 | + |
|
| 1102 | + /** |
|
| 1103 | + * This sets the _registration property for the registration details screen |
|
| 1104 | + * |
|
| 1105 | + * @access private |
|
| 1106 | + * @return bool |
|
| 1107 | + * @throws EE_Error |
|
| 1108 | + * @throws InvalidArgumentException |
|
| 1109 | + * @throws InvalidDataTypeException |
|
| 1110 | + * @throws InvalidInterfaceException |
|
| 1111 | + */ |
|
| 1112 | + private function _set_registration_object() |
|
| 1113 | + { |
|
| 1114 | + //get out if we've already set the object |
|
| 1115 | + if ($this->_registration instanceof EE_Registration) { |
|
| 1116 | + return true; |
|
| 1117 | + } |
|
| 1118 | + $REG = EEM_Registration::instance(); |
|
| 1119 | + $REG_ID = ( ! empty($this->_req_data['_REG_ID'])) ? absint($this->_req_data['_REG_ID']) : false; |
|
| 1120 | + if ($this->_registration = $REG->get_one_by_ID($REG_ID)) { |
|
| 1121 | + return true; |
|
| 1122 | + } else { |
|
| 1123 | + $error_msg = sprintf( |
|
| 1124 | + esc_html__( |
|
| 1125 | + 'An error occurred and the details for Registration ID #%s could not be retrieved.', |
|
| 1126 | + 'event_espresso' |
|
| 1127 | + ), |
|
| 1128 | + $REG_ID |
|
| 1129 | + ); |
|
| 1130 | + EE_Error::add_error($error_msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 1131 | + $this->_registration = null; |
|
| 1132 | + return false; |
|
| 1133 | + } |
|
| 1134 | + } |
|
| 1135 | + |
|
| 1136 | + |
|
| 1137 | + /** |
|
| 1138 | + * Used to retrieve registrations for the list table. |
|
| 1139 | + * |
|
| 1140 | + * @param int $per_page |
|
| 1141 | + * @param bool $count |
|
| 1142 | + * @param bool $this_month |
|
| 1143 | + * @param bool $today |
|
| 1144 | + * @return EE_Registration[]|int |
|
| 1145 | + * @throws EE_Error |
|
| 1146 | + * @throws InvalidArgumentException |
|
| 1147 | + * @throws InvalidDataTypeException |
|
| 1148 | + * @throws InvalidInterfaceException |
|
| 1149 | + */ |
|
| 1150 | + public function get_registrations( |
|
| 1151 | + $per_page = 10, |
|
| 1152 | + $count = false, |
|
| 1153 | + $this_month = false, |
|
| 1154 | + $today = false |
|
| 1155 | + ) { |
|
| 1156 | + if ($this_month) { |
|
| 1157 | + $this->_req_data['status'] = 'month'; |
|
| 1158 | + } |
|
| 1159 | + if ($today) { |
|
| 1160 | + $this->_req_data['status'] = 'today'; |
|
| 1161 | + } |
|
| 1162 | + $query_params = $this->_get_registration_query_parameters($this->_req_data, $per_page, $count); |
|
| 1163 | + /** |
|
| 1164 | + * Override the default groupby added by EEM_Base so that sorts with multiple order bys work as expected |
|
| 1165 | + * @link https://events.codebasehq.com/projects/event-espresso/tickets/10093 |
|
| 1166 | + * @see EEM_Base::get_all() |
|
| 1167 | + */ |
|
| 1168 | + $query_params['group_by'] = ''; |
|
| 1169 | + |
|
| 1170 | + return $count |
|
| 1171 | + ? EEM_Registration::instance()->count($query_params) |
|
| 1172 | + /** @type EE_Registration[] */ |
|
| 1173 | + : EEM_Registration::instance()->get_all($query_params); |
|
| 1174 | + } |
|
| 1175 | + |
|
| 1176 | + |
|
| 1177 | + /** |
|
| 1178 | + * Retrieves the query parameters to be used by the Registration model for getting registrations. |
|
| 1179 | + * Note: this listens to values on the request for some of the query parameters. |
|
| 1180 | + * |
|
| 1181 | + * @param array $request |
|
| 1182 | + * @param int $per_page |
|
| 1183 | + * @param bool $count |
|
| 1184 | + * @return array |
|
| 1185 | + * @throws EE_Error |
|
| 1186 | + */ |
|
| 1187 | + protected function _get_registration_query_parameters( |
|
| 1188 | + $request = array(), |
|
| 1189 | + $per_page = 10, |
|
| 1190 | + $count = false |
|
| 1191 | + ) { |
|
| 1192 | + |
|
| 1193 | + $query_params = array( |
|
| 1194 | + 0 => $this->_get_where_conditions_for_registrations_query( |
|
| 1195 | + $request |
|
| 1196 | + ), |
|
| 1197 | + 'caps' => EEM_Registration::caps_read_admin, |
|
| 1198 | + 'default_where_conditions' => 'this_model_only', |
|
| 1199 | + ); |
|
| 1200 | + if (! $count) { |
|
| 1201 | + $query_params = array_merge( |
|
| 1202 | + $query_params, |
|
| 1203 | + $this->_get_orderby_for_registrations_query(), |
|
| 1204 | + $this->_get_limit($per_page) |
|
| 1205 | + ); |
|
| 1206 | + } |
|
| 1207 | + |
|
| 1208 | + return $query_params; |
|
| 1209 | + } |
|
| 1210 | + |
|
| 1211 | + |
|
| 1212 | + /** |
|
| 1213 | + * This will add ATT_ID to the provided $where array for EE model query parameters. |
|
| 1214 | + * |
|
| 1215 | + * @param array $request usually the same as $this->_req_data but not necessarily |
|
| 1216 | + * @return array |
|
| 1217 | + */ |
|
| 1218 | + protected function addAttendeeIdToWhereConditions(array $request) |
|
| 1219 | + { |
|
| 1220 | + $where = array(); |
|
| 1221 | + if (! empty($request['ATT_ID'])) { |
|
| 1222 | + $where['ATT_ID'] = absint($request['ATT_ID']); |
|
| 1223 | + } |
|
| 1224 | + return $where; |
|
| 1225 | + } |
|
| 1226 | + |
|
| 1227 | + |
|
| 1228 | + /** |
|
| 1229 | + * This will add EVT_ID to the provided $where array for EE model query parameters. |
|
| 1230 | + * |
|
| 1231 | + * @param array $request usually the same as $this->_req_data but not necessarily |
|
| 1232 | + * @return array |
|
| 1233 | + */ |
|
| 1234 | + protected function _add_event_id_to_where_conditions(array $request) |
|
| 1235 | + { |
|
| 1236 | + $where = array(); |
|
| 1237 | + if (! empty($request['event_id'])) { |
|
| 1238 | + $where['EVT_ID'] = absint($request['event_id']); |
|
| 1239 | + } |
|
| 1240 | + return $where; |
|
| 1241 | + } |
|
| 1242 | + |
|
| 1243 | + |
|
| 1244 | + /** |
|
| 1245 | + * Adds category ID if it exists in the request to the where conditions for the registrations query. |
|
| 1246 | + * |
|
| 1247 | + * @param array $request usually the same as $this->_req_data but not necessarily |
|
| 1248 | + * @return array |
|
| 1249 | + */ |
|
| 1250 | + protected function _add_category_id_to_where_conditions(array $request) |
|
| 1251 | + { |
|
| 1252 | + $where = array(); |
|
| 1253 | + if (! empty($request['EVT_CAT']) && (int)$request['EVT_CAT'] !== -1) { |
|
| 1254 | + $where['Event.Term_Taxonomy.term_id'] = absint($request['EVT_CAT']); |
|
| 1255 | + } |
|
| 1256 | + return $where; |
|
| 1257 | + } |
|
| 1258 | + |
|
| 1259 | + |
|
| 1260 | + /** |
|
| 1261 | + * Adds the datetime ID if it exists in the request to the where conditions for the registrations query. |
|
| 1262 | + * |
|
| 1263 | + * @param array $request usually the same as $this->_req_data but not necessarily |
|
| 1264 | + * @return array |
|
| 1265 | + */ |
|
| 1266 | + protected function _add_datetime_id_to_where_conditions(array $request) |
|
| 1267 | + { |
|
| 1268 | + $where = array(); |
|
| 1269 | + if (! empty($request['datetime_id'])) { |
|
| 1270 | + $where['Ticket.Datetime.DTT_ID'] = absint($request['datetime_id']); |
|
| 1271 | + } |
|
| 1272 | + if (! empty($request['DTT_ID'])) { |
|
| 1273 | + $where['Ticket.Datetime.DTT_ID'] = absint($request['DTT_ID']); |
|
| 1274 | + } |
|
| 1275 | + return $where; |
|
| 1276 | + } |
|
| 1277 | + |
|
| 1278 | + |
|
| 1279 | + /** |
|
| 1280 | + * Adds the correct registration status to the where conditions for the registrations query. |
|
| 1281 | + * |
|
| 1282 | + * @param array $request usually the same as $this->_req_data but not necessarily |
|
| 1283 | + * @return array |
|
| 1284 | + */ |
|
| 1285 | + protected function _add_registration_status_to_where_conditions(array $request) |
|
| 1286 | + { |
|
| 1287 | + $where = array(); |
|
| 1288 | + $view = EEH_Array::is_set($request, 'status', ''); |
|
| 1289 | + $registration_status = ! empty($request['_reg_status']) |
|
| 1290 | + ? sanitize_text_field($request['_reg_status']) |
|
| 1291 | + : ''; |
|
| 1292 | + |
|
| 1293 | + /* |
|
| 1294 | 1294 | * If filtering by registration status, then we show registrations matching that status. |
| 1295 | 1295 | * If not filtering by specified status, then we show all registrations excluding incomplete registrations |
| 1296 | 1296 | * UNLESS viewing trashed registrations. |
| 1297 | 1297 | */ |
| 1298 | - if (! empty($registration_status)) { |
|
| 1299 | - $where['STS_ID'] = $registration_status; |
|
| 1300 | - } else { |
|
| 1301 | - //make sure we exclude incomplete registrations, but only if not trashed. |
|
| 1302 | - if ($view === 'trash') { |
|
| 1303 | - $where['REG_deleted'] = true; |
|
| 1304 | - } elseif ($view === 'incomplete') { |
|
| 1305 | - $where['STS_ID'] = EEM_Registration::status_id_incomplete; |
|
| 1306 | - } else { |
|
| 1307 | - $where['STS_ID'] = array('!=', EEM_Registration::status_id_incomplete); |
|
| 1308 | - } |
|
| 1309 | - } |
|
| 1310 | - return $where; |
|
| 1311 | - } |
|
| 1312 | - |
|
| 1313 | - |
|
| 1314 | - /** |
|
| 1315 | - * Adds any provided date restraints to the where conditions for the registrations query. |
|
| 1316 | - * |
|
| 1317 | - * @param array $request usually the same as $this->_req_data but not necessarily |
|
| 1318 | - * @return array |
|
| 1319 | - * @throws EE_Error |
|
| 1320 | - * @throws InvalidArgumentException |
|
| 1321 | - * @throws InvalidDataTypeException |
|
| 1322 | - * @throws InvalidInterfaceException |
|
| 1323 | - */ |
|
| 1324 | - protected function _add_date_to_where_conditions(array $request) |
|
| 1325 | - { |
|
| 1326 | - $where = array(); |
|
| 1327 | - $view = EEH_Array::is_set($request, 'status', ''); |
|
| 1328 | - $month_range = ! empty($request['month_range']) |
|
| 1329 | - ? sanitize_text_field($request['month_range']) |
|
| 1330 | - : ''; |
|
| 1331 | - $retrieve_for_today = $view === 'today'; |
|
| 1332 | - $retrieve_for_this_month = $view === 'month'; |
|
| 1333 | - |
|
| 1334 | - if ($retrieve_for_today) { |
|
| 1335 | - $now = date('Y-m-d', current_time('timestamp')); |
|
| 1336 | - $where['REG_date'] = array( |
|
| 1337 | - 'BETWEEN', |
|
| 1338 | - array( |
|
| 1339 | - EEM_Registration::instance()->convert_datetime_for_query( |
|
| 1340 | - 'REG_date', |
|
| 1341 | - $now . ' 00:00:00', |
|
| 1342 | - 'Y-m-d H:i:s' |
|
| 1343 | - ), |
|
| 1344 | - EEM_Registration::instance()->convert_datetime_for_query( |
|
| 1345 | - 'REG_date', |
|
| 1346 | - $now . ' 23:59:59', |
|
| 1347 | - 'Y-m-d H:i:s' |
|
| 1348 | - ), |
|
| 1349 | - ), |
|
| 1350 | - ); |
|
| 1351 | - } elseif ($retrieve_for_this_month) { |
|
| 1352 | - $current_year_and_month = date('Y-m', current_time('timestamp')); |
|
| 1353 | - $days_this_month = date('t', current_time('timestamp')); |
|
| 1354 | - $where['REG_date'] = array( |
|
| 1355 | - 'BETWEEN', |
|
| 1356 | - array( |
|
| 1357 | - EEM_Registration::instance()->convert_datetime_for_query( |
|
| 1358 | - 'REG_date', |
|
| 1359 | - $current_year_and_month . '-01 00:00:00', |
|
| 1360 | - 'Y-m-d H:i:s' |
|
| 1361 | - ), |
|
| 1362 | - EEM_Registration::instance()->convert_datetime_for_query( |
|
| 1363 | - 'REG_date', |
|
| 1364 | - $current_year_and_month . '-' . $days_this_month . ' 23:59:59', |
|
| 1365 | - 'Y-m-d H:i:s' |
|
| 1366 | - ), |
|
| 1367 | - ), |
|
| 1368 | - ); |
|
| 1369 | - } elseif ($month_range) { |
|
| 1370 | - $pieces = explode(' ', $month_range, 3); |
|
| 1371 | - $month_requested = ! empty($pieces[0]) |
|
| 1372 | - ? date('m', \EEH_DTT_Helper::first_of_month_timestamp($pieces[0])) |
|
| 1373 | - : ''; |
|
| 1374 | - $year_requested = ! empty($pieces[1]) |
|
| 1375 | - ? $pieces[1] |
|
| 1376 | - : ''; |
|
| 1377 | - //if there is not a month or year then we can't go further |
|
| 1378 | - if ($month_requested && $year_requested) { |
|
| 1379 | - $days_in_month = date('t', strtotime($year_requested . '-' . $month_requested . '-' . '01')); |
|
| 1380 | - $where['REG_date'] = array( |
|
| 1381 | - 'BETWEEN', |
|
| 1382 | - array( |
|
| 1383 | - EEM_Registration::instance()->convert_datetime_for_query( |
|
| 1384 | - 'REG_date', |
|
| 1385 | - $year_requested . '-' . $month_requested . '-01 00:00:00', |
|
| 1386 | - 'Y-m-d H:i:s' |
|
| 1387 | - ), |
|
| 1388 | - EEM_Registration::instance()->convert_datetime_for_query( |
|
| 1389 | - 'REG_date', |
|
| 1390 | - $year_requested . '-' . $month_requested . '-' . $days_in_month . ' 23:59:59', |
|
| 1391 | - 'Y-m-d H:i:s' |
|
| 1392 | - ), |
|
| 1393 | - ), |
|
| 1394 | - ); |
|
| 1395 | - } |
|
| 1396 | - } |
|
| 1397 | - return $where; |
|
| 1398 | - } |
|
| 1399 | - |
|
| 1400 | - |
|
| 1401 | - /** |
|
| 1402 | - * Adds any provided search restraints to the where conditions for the registrations query |
|
| 1403 | - * |
|
| 1404 | - * @param array $request usually the same as $this->_req_data but not necessarily |
|
| 1405 | - * @return array |
|
| 1406 | - */ |
|
| 1407 | - protected function _add_search_to_where_conditions(array $request) |
|
| 1408 | - { |
|
| 1409 | - $where = array(); |
|
| 1410 | - if (! empty($request['s'])) { |
|
| 1411 | - $search_string = '%' . sanitize_text_field($request['s']) . '%'; |
|
| 1412 | - $where['OR*search_conditions'] = array( |
|
| 1413 | - 'Event.EVT_name' => array('LIKE', $search_string), |
|
| 1414 | - 'Event.EVT_desc' => array('LIKE', $search_string), |
|
| 1415 | - 'Event.EVT_short_desc' => array('LIKE', $search_string), |
|
| 1416 | - 'Attendee.ATT_full_name' => array('LIKE', $search_string), |
|
| 1417 | - 'Attendee.ATT_fname' => array('LIKE', $search_string), |
|
| 1418 | - 'Attendee.ATT_lname' => array('LIKE', $search_string), |
|
| 1419 | - 'Attendee.ATT_short_bio' => array('LIKE', $search_string), |
|
| 1420 | - 'Attendee.ATT_email' => array('LIKE', $search_string), |
|
| 1421 | - 'Attendee.ATT_address' => array('LIKE', $search_string), |
|
| 1422 | - 'Attendee.ATT_address2' => array('LIKE', $search_string), |
|
| 1423 | - 'Attendee.ATT_city' => array('LIKE', $search_string), |
|
| 1424 | - 'REG_final_price' => array('LIKE', $search_string), |
|
| 1425 | - 'REG_code' => array('LIKE', $search_string), |
|
| 1426 | - 'REG_count' => array('LIKE', $search_string), |
|
| 1427 | - 'REG_group_size' => array('LIKE', $search_string), |
|
| 1428 | - 'Ticket.TKT_name' => array('LIKE', $search_string), |
|
| 1429 | - 'Ticket.TKT_description' => array('LIKE', $search_string), |
|
| 1430 | - 'Transaction.Payment.PAY_txn_id_chq_nmbr' => array('LIKE', $search_string), |
|
| 1431 | - ); |
|
| 1432 | - } |
|
| 1433 | - return $where; |
|
| 1434 | - } |
|
| 1435 | - |
|
| 1436 | - |
|
| 1437 | - /** |
|
| 1438 | - * Sets up the where conditions for the registrations query. |
|
| 1439 | - * |
|
| 1440 | - * @param array $request |
|
| 1441 | - * @return array |
|
| 1442 | - * @throws EE_Error |
|
| 1443 | - */ |
|
| 1444 | - protected function _get_where_conditions_for_registrations_query($request) |
|
| 1445 | - { |
|
| 1446 | - return apply_filters( |
|
| 1447 | - 'FHEE__Registrations_Admin_Page___get_where_conditions_for_registrations_query', |
|
| 1448 | - array_merge( |
|
| 1449 | - $this->addAttendeeIdToWhereConditions($request), |
|
| 1450 | - $this->_add_event_id_to_where_conditions($request), |
|
| 1451 | - $this->_add_category_id_to_where_conditions($request), |
|
| 1452 | - $this->_add_datetime_id_to_where_conditions($request), |
|
| 1453 | - $this->_add_registration_status_to_where_conditions($request), |
|
| 1454 | - $this->_add_date_to_where_conditions($request), |
|
| 1455 | - $this->_add_search_to_where_conditions($request) |
|
| 1456 | - ), |
|
| 1457 | - $request |
|
| 1458 | - ); |
|
| 1459 | - } |
|
| 1460 | - |
|
| 1461 | - |
|
| 1462 | - /** |
|
| 1463 | - * Sets up the orderby for the registrations query. |
|
| 1464 | - * |
|
| 1465 | - * @return array |
|
| 1466 | - */ |
|
| 1467 | - protected function _get_orderby_for_registrations_query() |
|
| 1468 | - { |
|
| 1469 | - $orderby_field = ! empty($this->_req_data['orderby']) |
|
| 1470 | - ? sanitize_text_field($this->_req_data['orderby']) |
|
| 1471 | - : ''; |
|
| 1472 | - switch ($orderby_field) { |
|
| 1473 | - case '_REG_ID': |
|
| 1474 | - $orderby = array('REG_ID'); |
|
| 1475 | - break; |
|
| 1476 | - case '_Reg_status': |
|
| 1477 | - $orderby = array('STS_ID'); |
|
| 1478 | - break; |
|
| 1479 | - case 'ATT_fname': |
|
| 1480 | - $orderby = array('Attendee.ATT_fname', 'Attendee.ATT_lname'); |
|
| 1481 | - break; |
|
| 1482 | - case 'ATT_lname': |
|
| 1483 | - $orderby = array('Attendee.ATT_lname', 'Attendee.ATT_fname'); |
|
| 1484 | - break; |
|
| 1485 | - case 'event_name': |
|
| 1486 | - $orderby = array('Event.EVT_name'); |
|
| 1487 | - break; |
|
| 1488 | - case 'DTT_EVT_start': |
|
| 1489 | - $orderby = array('Event.Datetime.DTT_EVT_start'); |
|
| 1490 | - break; |
|
| 1491 | - default: //'REG_date' |
|
| 1492 | - $orderby = array('REG_date'); |
|
| 1493 | - } |
|
| 1494 | - |
|
| 1495 | - //order |
|
| 1496 | - $order = ! empty($this->_req_data['order']) |
|
| 1497 | - ? sanitize_text_field($this->_req_data['order']) |
|
| 1498 | - : 'DESC'; |
|
| 1499 | - $orderby = array_combine( |
|
| 1500 | - $orderby, |
|
| 1501 | - array_fill(0, count($orderby), $order) |
|
| 1502 | - ); |
|
| 1503 | - //because there are many registrations with the same date, define |
|
| 1504 | - //a secondary way to order them, otherwise MySQL seems to be a bit random |
|
| 1505 | - if (empty($orderby['REG_ID'])) { |
|
| 1506 | - $orderby['REG_ID'] = $order; |
|
| 1507 | - } |
|
| 1508 | - return array('order_by' => $orderby); |
|
| 1509 | - } |
|
| 1510 | - |
|
| 1511 | - |
|
| 1512 | - /** |
|
| 1513 | - * Sets up the limit for the registrations query. |
|
| 1514 | - * |
|
| 1515 | - * @param $per_page |
|
| 1516 | - * @return array |
|
| 1517 | - */ |
|
| 1518 | - protected function _get_limit($per_page) |
|
| 1519 | - { |
|
| 1520 | - $current_page = ! empty($this->_req_data['paged']) |
|
| 1521 | - ? absint($this->_req_data['paged']) |
|
| 1522 | - : 1; |
|
| 1523 | - $per_page = ! empty($this->_req_data['perpage']) |
|
| 1524 | - ? $this->_req_data['perpage'] |
|
| 1525 | - : $per_page; |
|
| 1526 | - |
|
| 1527 | - //-1 means return all results so get out if that's set. |
|
| 1528 | - if ((int)$per_page === -1) { |
|
| 1529 | - return array(); |
|
| 1530 | - } |
|
| 1531 | - $per_page = absint($per_page); |
|
| 1532 | - $offset = ($current_page - 1) * $per_page; |
|
| 1533 | - return array('limit' => array($offset, $per_page)); |
|
| 1534 | - } |
|
| 1535 | - |
|
| 1536 | - |
|
| 1537 | - public function get_registration_status_array() |
|
| 1538 | - { |
|
| 1539 | - return self::$_reg_status; |
|
| 1540 | - } |
|
| 1541 | - |
|
| 1542 | - |
|
| 1543 | - |
|
| 1544 | - |
|
| 1545 | - /*************************************** REGISTRATION DETAILS ***************************************/ |
|
| 1546 | - /** |
|
| 1547 | - * generates HTML for the View Registration Details Admin page |
|
| 1548 | - * |
|
| 1549 | - * @access protected |
|
| 1550 | - * @return void |
|
| 1551 | - * @throws DomainException |
|
| 1552 | - * @throws EE_Error |
|
| 1553 | - * @throws InvalidArgumentException |
|
| 1554 | - * @throws InvalidDataTypeException |
|
| 1555 | - * @throws InvalidInterfaceException |
|
| 1556 | - * @throws EntityNotFoundException |
|
| 1557 | - */ |
|
| 1558 | - protected function _registration_details() |
|
| 1559 | - { |
|
| 1560 | - $this->_template_args = array(); |
|
| 1561 | - $this->_set_registration_object(); |
|
| 1562 | - if (is_object($this->_registration)) { |
|
| 1563 | - $transaction = $this->_registration->transaction() |
|
| 1564 | - ? $this->_registration->transaction() |
|
| 1565 | - : EE_Transaction::new_instance(); |
|
| 1566 | - $this->_session = $transaction->session_data(); |
|
| 1567 | - $event_id = $this->_registration->event_ID(); |
|
| 1568 | - $this->_template_args['reg_nmbr']['value'] = $this->_registration->ID(); |
|
| 1569 | - $this->_template_args['reg_nmbr']['label'] = esc_html__('Registration Number', 'event_espresso'); |
|
| 1570 | - $this->_template_args['reg_datetime']['value'] = $this->_registration->get_i18n_datetime('REG_date'); |
|
| 1571 | - $this->_template_args['reg_datetime']['label'] = esc_html__('Date', 'event_espresso'); |
|
| 1572 | - $this->_template_args['grand_total'] = $transaction->total(); |
|
| 1573 | - $this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign; |
|
| 1574 | - // link back to overview |
|
| 1575 | - $this->_template_args['reg_overview_url'] = REG_ADMIN_URL; |
|
| 1576 | - $this->_template_args['registration'] = $this->_registration; |
|
| 1577 | - $this->_template_args['filtered_registrations_link'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 1578 | - array( |
|
| 1579 | - 'action' => 'default', |
|
| 1580 | - 'event_id' => $event_id, |
|
| 1581 | - ), |
|
| 1582 | - REG_ADMIN_URL |
|
| 1583 | - ); |
|
| 1584 | - $this->_template_args['filtered_transactions_link'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 1585 | - array( |
|
| 1586 | - 'action' => 'default', |
|
| 1587 | - 'EVT_ID' => $event_id, |
|
| 1588 | - 'page' => 'espresso_transactions', |
|
| 1589 | - ), |
|
| 1590 | - admin_url('admin.php') |
|
| 1591 | - ); |
|
| 1592 | - $this->_template_args['event_link'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 1593 | - array( |
|
| 1594 | - 'page' => 'espresso_events', |
|
| 1595 | - 'action' => 'edit', |
|
| 1596 | - 'post' => $event_id, |
|
| 1597 | - ), |
|
| 1598 | - admin_url('admin.php') |
|
| 1599 | - ); |
|
| 1600 | - //next and previous links |
|
| 1601 | - $next_reg = $this->_registration->next( |
|
| 1602 | - null, |
|
| 1603 | - array(), |
|
| 1604 | - 'REG_ID' |
|
| 1605 | - ); |
|
| 1606 | - $this->_template_args['next_registration'] = $next_reg |
|
| 1607 | - ? $this->_next_link( |
|
| 1608 | - EE_Admin_Page::add_query_args_and_nonce( |
|
| 1609 | - array( |
|
| 1610 | - 'action' => 'view_registration', |
|
| 1611 | - '_REG_ID' => $next_reg['REG_ID'], |
|
| 1612 | - ), |
|
| 1613 | - REG_ADMIN_URL |
|
| 1614 | - ), |
|
| 1615 | - 'dashicons dashicons-arrow-right ee-icon-size-22' |
|
| 1616 | - ) |
|
| 1617 | - : ''; |
|
| 1618 | - $previous_reg = $this->_registration->previous( |
|
| 1619 | - null, |
|
| 1620 | - array(), |
|
| 1621 | - 'REG_ID' |
|
| 1622 | - ); |
|
| 1623 | - $this->_template_args['previous_registration'] = $previous_reg |
|
| 1624 | - ? $this->_previous_link( |
|
| 1625 | - EE_Admin_Page::add_query_args_and_nonce( |
|
| 1626 | - array( |
|
| 1627 | - 'action' => 'view_registration', |
|
| 1628 | - '_REG_ID' => $previous_reg['REG_ID'], |
|
| 1629 | - ), |
|
| 1630 | - REG_ADMIN_URL |
|
| 1631 | - ), |
|
| 1632 | - 'dashicons dashicons-arrow-left ee-icon-size-22' |
|
| 1633 | - ) |
|
| 1634 | - : ''; |
|
| 1635 | - // grab header |
|
| 1636 | - $template_path = REG_TEMPLATE_PATH . 'reg_admin_details_header.template.php'; |
|
| 1637 | - $this->_template_args['REG_ID'] = $this->_registration->ID(); |
|
| 1638 | - $this->_template_args['admin_page_header'] = EEH_Template::display_template( |
|
| 1639 | - $template_path, |
|
| 1640 | - $this->_template_args, |
|
| 1641 | - true |
|
| 1642 | - ); |
|
| 1643 | - } else { |
|
| 1644 | - $this->_template_args['admin_page_header'] = $this->display_espresso_notices(); |
|
| 1645 | - } |
|
| 1646 | - // the details template wrapper |
|
| 1647 | - $this->display_admin_page_with_sidebar(); |
|
| 1648 | - } |
|
| 1649 | - |
|
| 1650 | - |
|
| 1651 | - protected function _registration_details_metaboxes() |
|
| 1652 | - { |
|
| 1653 | - do_action('AHEE__Registrations_Admin_Page___registration_details_metabox__start', $this); |
|
| 1654 | - $this->_set_registration_object(); |
|
| 1655 | - $attendee = $this->_registration instanceof EE_Registration ? $this->_registration->attendee() : null; |
|
| 1656 | - add_meta_box('edit-reg-status-mbox', esc_html__('Registration Status', 'event_espresso'), |
|
| 1657 | - array($this, 'set_reg_status_buttons_metabox'), $this->wp_page_slug, 'normal', 'high'); |
|
| 1658 | - add_meta_box('edit-reg-details-mbox', esc_html__('Registration Details', 'event_espresso'), |
|
| 1659 | - array($this, '_reg_details_meta_box'), $this->wp_page_slug, 'normal', 'high'); |
|
| 1660 | - if ($attendee instanceof EE_Attendee |
|
| 1661 | - && EE_Registry::instance()->CAP->current_user_can( |
|
| 1662 | - 'ee_edit_registration', |
|
| 1663 | - 'edit-reg-questions-mbox', |
|
| 1664 | - $this->_registration->ID() |
|
| 1665 | - ) |
|
| 1666 | - ) { |
|
| 1667 | - add_meta_box( |
|
| 1668 | - 'edit-reg-questions-mbox', |
|
| 1669 | - esc_html__('Registration Form Answers', 'event_espresso'), |
|
| 1670 | - array($this, '_reg_questions_meta_box'), |
|
| 1671 | - $this->wp_page_slug, |
|
| 1672 | - 'normal', |
|
| 1673 | - 'high' |
|
| 1674 | - ); |
|
| 1675 | - } |
|
| 1676 | - add_meta_box( |
|
| 1677 | - 'edit-reg-registrant-mbox', |
|
| 1678 | - esc_html__('Contact Details', 'event_espresso'), |
|
| 1679 | - array($this, '_reg_registrant_side_meta_box'), |
|
| 1680 | - $this->wp_page_slug, |
|
| 1681 | - 'side', |
|
| 1682 | - 'high' |
|
| 1683 | - ); |
|
| 1684 | - if ($this->_registration->group_size() > 1) { |
|
| 1685 | - add_meta_box( |
|
| 1686 | - 'edit-reg-attendees-mbox', |
|
| 1687 | - esc_html__('Other Registrations in this Transaction', 'event_espresso'), |
|
| 1688 | - array($this, '_reg_attendees_meta_box'), |
|
| 1689 | - $this->wp_page_slug, |
|
| 1690 | - 'normal', |
|
| 1691 | - 'high' |
|
| 1692 | - ); |
|
| 1693 | - } |
|
| 1694 | - } |
|
| 1695 | - |
|
| 1696 | - |
|
| 1697 | - /** |
|
| 1698 | - * set_reg_status_buttons_metabox |
|
| 1699 | - * |
|
| 1700 | - * @access protected |
|
| 1701 | - * @return string |
|
| 1702 | - * @throws \EE_Error |
|
| 1703 | - */ |
|
| 1704 | - public function set_reg_status_buttons_metabox() |
|
| 1705 | - { |
|
| 1706 | - $this->_set_registration_object(); |
|
| 1707 | - $change_reg_status_form = $this->_generate_reg_status_change_form(); |
|
| 1708 | - echo $change_reg_status_form->form_open( |
|
| 1709 | - self::add_query_args_and_nonce( |
|
| 1710 | - array( |
|
| 1711 | - 'action' => 'change_reg_status', |
|
| 1712 | - ), |
|
| 1713 | - REG_ADMIN_URL |
|
| 1714 | - ) |
|
| 1715 | - ); |
|
| 1716 | - echo $change_reg_status_form->get_html(); |
|
| 1717 | - echo $change_reg_status_form->form_close(); |
|
| 1718 | - } |
|
| 1719 | - |
|
| 1720 | - |
|
| 1721 | - /** |
|
| 1722 | - * @return EE_Form_Section_Proper |
|
| 1723 | - * @throws EE_Error |
|
| 1724 | - * @throws InvalidArgumentException |
|
| 1725 | - * @throws InvalidDataTypeException |
|
| 1726 | - * @throws InvalidInterfaceException |
|
| 1727 | - * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 1728 | - */ |
|
| 1729 | - protected function _generate_reg_status_change_form() |
|
| 1730 | - { |
|
| 1731 | - return new EE_Form_Section_Proper(array( |
|
| 1732 | - 'name' => 'reg_status_change_form', |
|
| 1733 | - 'html_id' => 'reg-status-change-form', |
|
| 1734 | - 'layout_strategy' => new EE_Admin_Two_Column_Layout(), |
|
| 1735 | - 'subsections' => array( |
|
| 1736 | - 'return' => new EE_Hidden_Input(array( |
|
| 1737 | - 'name' => 'return', |
|
| 1738 | - 'default' => 'view_registration', |
|
| 1739 | - )), |
|
| 1740 | - 'REG_ID' => new EE_Hidden_Input(array( |
|
| 1741 | - 'name' => 'REG_ID', |
|
| 1742 | - 'default' => $this->_registration->ID(), |
|
| 1743 | - )), |
|
| 1744 | - 'current_status' => new EE_Form_Section_HTML( |
|
| 1745 | - EEH_HTML::tr( |
|
| 1746 | - EEH_HTML::th( |
|
| 1747 | - EEH_HTML::label( |
|
| 1748 | - EEH_HTML::strong(esc_html__('Current Registration Status', 'event_espresso') |
|
| 1749 | - ) |
|
| 1750 | - ) |
|
| 1751 | - ) |
|
| 1752 | - . EEH_HTML::td( |
|
| 1753 | - EEH_HTML::strong( |
|
| 1754 | - $this->_registration->pretty_status(), |
|
| 1755 | - '', |
|
| 1756 | - 'status-' . $this->_registration->status_ID(), |
|
| 1757 | - 'line-height: 1em; font-size: 1.5em; font-weight: bold;' |
|
| 1758 | - ) |
|
| 1759 | - ) |
|
| 1760 | - ) |
|
| 1761 | - ), |
|
| 1762 | - 'reg_status' => new EE_Select_Input( |
|
| 1763 | - $this->_get_reg_statuses(), |
|
| 1764 | - array( |
|
| 1765 | - 'html_label_text' => esc_html__('Change Registration Status to', 'event_espresso'), |
|
| 1766 | - 'default' => $this->_registration->status_ID(), |
|
| 1767 | - ) |
|
| 1768 | - ), |
|
| 1769 | - 'send_notifications' => new EE_Yes_No_Input( |
|
| 1770 | - array( |
|
| 1771 | - 'html_label_text' => esc_html__('Send Related Messages', 'event_espresso'), |
|
| 1772 | - 'default' => false, |
|
| 1773 | - 'html_help_text' => esc_html__( |
|
| 1774 | - 'If set to "Yes", then the related messages will be sent to the registrant.', |
|
| 1775 | - 'event_espresso' |
|
| 1776 | - ), |
|
| 1777 | - ) |
|
| 1778 | - ), |
|
| 1779 | - 'submit' => new EE_Submit_Input( |
|
| 1780 | - array( |
|
| 1781 | - 'html_class' => 'button-primary', |
|
| 1782 | - 'html_label_text' => ' ', |
|
| 1783 | - 'default' => esc_html__('Update Registration Status', 'event_espresso'), |
|
| 1784 | - ) |
|
| 1785 | - ), |
|
| 1786 | - ), |
|
| 1787 | - )); |
|
| 1788 | - } |
|
| 1789 | - |
|
| 1790 | - |
|
| 1791 | - /** |
|
| 1792 | - * Returns an array of all the buttons for the various statuses and switch status actions |
|
| 1793 | - * |
|
| 1794 | - * @return array |
|
| 1795 | - * @throws EE_Error |
|
| 1796 | - * @throws InvalidArgumentException |
|
| 1797 | - * @throws InvalidDataTypeException |
|
| 1798 | - * @throws InvalidInterfaceException |
|
| 1799 | - * @throws EntityNotFoundException |
|
| 1800 | - */ |
|
| 1801 | - protected function _get_reg_statuses() |
|
| 1802 | - { |
|
| 1803 | - $reg_status_array = EEM_Registration::instance()->reg_status_array(); |
|
| 1804 | - unset ($reg_status_array[EEM_Registration::status_id_incomplete]); |
|
| 1805 | - // get current reg status |
|
| 1806 | - $current_status = $this->_registration->status_ID(); |
|
| 1807 | - // is registration for free event? This will determine whether to display the pending payment option |
|
| 1808 | - if ( |
|
| 1809 | - $current_status !== EEM_Registration::status_id_pending_payment |
|
| 1810 | - && EEH_Money::compare_floats($this->_registration->ticket()->price(), 0.00) |
|
| 1811 | - ) { |
|
| 1812 | - unset($reg_status_array[EEM_Registration::status_id_pending_payment]); |
|
| 1813 | - } |
|
| 1814 | - return EEM_Status::instance()->localized_status($reg_status_array, false, 'sentence'); |
|
| 1815 | - } |
|
| 1816 | - |
|
| 1817 | - |
|
| 1818 | - /** |
|
| 1819 | - * This method is used when using _REG_ID from request which may or may not be an array of reg_ids. |
|
| 1820 | - * |
|
| 1821 | - * @param bool $status REG status given for changing registrations to. |
|
| 1822 | - * @param bool $notify Whether to send messages notifications or not. |
|
| 1823 | - * @return array (array with reg_id(s) updated and whether update was successful. |
|
| 1824 | - * @throws EE_Error |
|
| 1825 | - * @throws InvalidArgumentException |
|
| 1826 | - * @throws InvalidDataTypeException |
|
| 1827 | - * @throws InvalidInterfaceException |
|
| 1828 | - * @throws ReflectionException |
|
| 1829 | - * @throws RuntimeException |
|
| 1830 | - * @throws EntityNotFoundException |
|
| 1831 | - */ |
|
| 1832 | - protected function _set_registration_status_from_request($status = false, $notify = false) |
|
| 1833 | - { |
|
| 1834 | - if (isset($this->_req_data['reg_status_change_form'])) { |
|
| 1835 | - $REG_IDs = isset($this->_req_data['reg_status_change_form']['REG_ID']) |
|
| 1836 | - ? (array)$this->_req_data['reg_status_change_form']['REG_ID'] |
|
| 1837 | - : array(); |
|
| 1838 | - } else { |
|
| 1839 | - $REG_IDs = isset($this->_req_data['_REG_ID']) |
|
| 1840 | - ? (array)$this->_req_data['_REG_ID'] |
|
| 1841 | - : array(); |
|
| 1842 | - } |
|
| 1843 | - // sanitize $REG_IDs |
|
| 1844 | - $REG_IDs = array_map('absint', $REG_IDs); |
|
| 1845 | - // and remove empty entries |
|
| 1846 | - $REG_IDs = array_filter($REG_IDs); |
|
| 1847 | - |
|
| 1848 | - $result = $this->_set_registration_status($REG_IDs, $status, $notify); |
|
| 1849 | - |
|
| 1850 | - /** |
|
| 1851 | - * Set and filter $_req_data['_REG_ID'] for any potential future messages notifications. |
|
| 1852 | - * Currently this value is used downstream by the _process_resend_registration method. |
|
| 1853 | - * |
|
| 1854 | - * @param int|array $registration_ids The registration ids that have had their status changed successfully. |
|
| 1855 | - * @param bool $status The status registrations were changed to. |
|
| 1856 | - * @param bool $success If the status was changed successfully for all registrations. |
|
| 1857 | - * @param Registrations_Admin_Page $admin_page_object |
|
| 1858 | - */ |
|
| 1859 | - $this->_req_data['_REG_ID'] = apply_filters( |
|
| 1860 | - 'FHEE__Registrations_Admin_Page___set_registration_status_from_request__REG_IDs', |
|
| 1861 | - $result['REG_ID'], |
|
| 1862 | - $status, |
|
| 1863 | - $result['success'], |
|
| 1864 | - $this |
|
| 1865 | - ); |
|
| 1866 | - |
|
| 1867 | - //notify? |
|
| 1868 | - if ($notify |
|
| 1869 | - && $result['success'] |
|
| 1870 | - && ! empty($this->_req_data['_REG_ID']) |
|
| 1871 | - && EE_Registry::instance()->CAP->current_user_can( |
|
| 1872 | - 'ee_send_message', |
|
| 1873 | - 'espresso_registrations_resend_registration' |
|
| 1874 | - ) |
|
| 1875 | - ) { |
|
| 1876 | - $this->_process_resend_registration(); |
|
| 1877 | - } |
|
| 1878 | - return $result; |
|
| 1879 | - } |
|
| 1880 | - |
|
| 1881 | - |
|
| 1882 | - /** |
|
| 1883 | - * Set the registration status for the given reg_id (which may or may not be an array, it gets typecast to an |
|
| 1884 | - * array). Note, this method does NOT take care of possible notifications. That is required by calling code. |
|
| 1885 | - * |
|
| 1886 | - * @param array $REG_IDs |
|
| 1887 | - * @param string $status |
|
| 1888 | - * @param bool $notify Used to indicate whether notification was requested or not. This determines the context |
|
| 1889 | - * slug sent with setting the registration status. |
|
| 1890 | - * @return array (an array with 'success' key representing whether status change was successful, and 'REG_ID' as |
|
| 1891 | - * @throws EE_Error |
|
| 1892 | - * @throws InvalidArgumentException |
|
| 1893 | - * @throws InvalidDataTypeException |
|
| 1894 | - * @throws InvalidInterfaceException |
|
| 1895 | - * @throws ReflectionException |
|
| 1896 | - * @throws RuntimeException |
|
| 1897 | - * @throws EntityNotFoundException |
|
| 1898 | - */ |
|
| 1899 | - protected function _set_registration_status($REG_IDs = array(), $status = '', $notify = false) |
|
| 1900 | - { |
|
| 1901 | - $success = false; |
|
| 1902 | - // typecast $REG_IDs |
|
| 1903 | - $REG_IDs = (array)$REG_IDs; |
|
| 1904 | - if ( ! empty($REG_IDs)) { |
|
| 1905 | - $success = true; |
|
| 1906 | - // set default status if none is passed |
|
| 1907 | - $status = $status ? $status : EEM_Registration::status_id_pending_payment; |
|
| 1908 | - $status_context = $notify |
|
| 1909 | - ? Domain::CONTEXT_REGISTRATION_STATUS_CHANGE_REGISTRATION_ADMIN_NOTIFY |
|
| 1910 | - : Domain::CONTEXT_REGISTRATION_STATUS_CHANGE_REGISTRATION_ADMIN; |
|
| 1911 | - //loop through REG_ID's and change status |
|
| 1912 | - foreach ($REG_IDs as $REG_ID) { |
|
| 1913 | - $registration = EEM_Registration::instance()->get_one_by_ID($REG_ID); |
|
| 1914 | - if ($registration instanceof EE_Registration) { |
|
| 1915 | - $registration->set_status( |
|
| 1916 | - $status, |
|
| 1917 | - false, |
|
| 1918 | - new Context( |
|
| 1919 | - $status_context, |
|
| 1920 | - esc_html__( |
|
| 1921 | - 'Manually triggered status change on a Registration Admin Page route.', |
|
| 1922 | - 'event_espresso' |
|
| 1923 | - ) |
|
| 1924 | - ) |
|
| 1925 | - ); |
|
| 1926 | - $result = $registration->save(); |
|
| 1927 | - // verifying explicit fails because update *may* just return 0 for 0 rows affected |
|
| 1928 | - $success = $result !== false ? $success : false; |
|
| 1929 | - } |
|
| 1930 | - } |
|
| 1931 | - } |
|
| 1932 | - |
|
| 1933 | - //return $success and processed registrations |
|
| 1934 | - return array('REG_ID' => $REG_IDs, 'success' => $success); |
|
| 1935 | - } |
|
| 1936 | - |
|
| 1937 | - |
|
| 1938 | - /** |
|
| 1939 | - * Common logic for setting up success message and redirecting to appropriate route |
|
| 1940 | - * |
|
| 1941 | - * @param string $STS_ID status id for the registration changed to |
|
| 1942 | - * @param bool $notify indicates whether the _set_registration_status_from_request does notifications or not. |
|
| 1943 | - * @return void |
|
| 1944 | - * @throws EE_Error |
|
| 1945 | - */ |
|
| 1946 | - protected function _reg_status_change_return($STS_ID, $notify = false) |
|
| 1947 | - { |
|
| 1948 | - $result = ! empty($STS_ID) ? $this->_set_registration_status_from_request($STS_ID, $notify) |
|
| 1949 | - : array('success' => false); |
|
| 1950 | - $success = isset($result['success']) && $result['success']; |
|
| 1951 | - //setup success message |
|
| 1952 | - if ($success) { |
|
| 1953 | - if (is_array($result['REG_ID']) && count($result['REG_ID']) === 1) { |
|
| 1954 | - $msg = sprintf(esc_html__('Registration status has been set to %s', 'event_espresso'), |
|
| 1955 | - EEH_Template::pretty_status($STS_ID, false, 'lower')); |
|
| 1956 | - } else { |
|
| 1957 | - $msg = sprintf(esc_html__('Registrations have been set to %s.', 'event_espresso'), |
|
| 1958 | - EEH_Template::pretty_status($STS_ID, false, 'lower')); |
|
| 1959 | - } |
|
| 1960 | - EE_Error::add_success($msg); |
|
| 1961 | - } else { |
|
| 1962 | - EE_Error::add_error( |
|
| 1963 | - esc_html__( |
|
| 1964 | - 'Something went wrong, and the status was not changed', |
|
| 1965 | - 'event_espresso' |
|
| 1966 | - ), __FILE__, __LINE__, __FUNCTION__ |
|
| 1967 | - ); |
|
| 1968 | - } |
|
| 1969 | - if (isset($this->_req_data['return']) && $this->_req_data['return'] == 'view_registration') { |
|
| 1970 | - $route = array('action' => 'view_registration', '_REG_ID' => reset($result['REG_ID'])); |
|
| 1971 | - } else { |
|
| 1972 | - $route = array('action' => 'default'); |
|
| 1973 | - } |
|
| 1974 | - //unset nonces |
|
| 1975 | - foreach ($this->_req_data as $ref => $value) { |
|
| 1976 | - if (strpos($ref, 'nonce') !== false) { |
|
| 1977 | - unset($this->_req_data[$ref]); |
|
| 1978 | - continue; |
|
| 1979 | - } |
|
| 1980 | - $value = is_array($value) ? array_map('urlencode', $value) : urlencode($value); |
|
| 1981 | - $this->_req_data[$ref] = $value; |
|
| 1982 | - } |
|
| 1983 | - //merge request vars so that the reloaded list table contains any existing filter query params |
|
| 1984 | - $route = array_merge($this->_req_data, $route); |
|
| 1985 | - $this->_redirect_after_action($success, '', '', $route, true); |
|
| 1986 | - } |
|
| 1987 | - |
|
| 1988 | - |
|
| 1989 | - /** |
|
| 1990 | - * incoming reg status change from reg details page. |
|
| 1991 | - * |
|
| 1992 | - * @return void |
|
| 1993 | - */ |
|
| 1994 | - protected function _change_reg_status() |
|
| 1995 | - { |
|
| 1996 | - $this->_req_data['return'] = 'view_registration'; |
|
| 1997 | - //set notify based on whether the send notifications toggle is set or not |
|
| 1998 | - $notify = ! empty($this->_req_data['reg_status_change_form']['send_notifications']); |
|
| 1999 | - //$notify = ! empty( $this->_req_data['txn_reg_status_change']['send_notifications'] ); |
|
| 2000 | - $this->_req_data['reg_status_change_form']['reg_status'] = isset($this->_req_data['reg_status_change_form']['reg_status']) |
|
| 2001 | - ? $this->_req_data['reg_status_change_form']['reg_status'] : ''; |
|
| 2002 | - switch ($this->_req_data['reg_status_change_form']['reg_status']) { |
|
| 2003 | - case EEM_Registration::status_id_approved : |
|
| 2004 | - case EEH_Template::pretty_status(EEM_Registration::status_id_approved, false, 'sentence') : |
|
| 2005 | - $this->approve_registration($notify); |
|
| 2006 | - break; |
|
| 2007 | - case EEM_Registration::status_id_pending_payment : |
|
| 2008 | - case EEH_Template::pretty_status(EEM_Registration::status_id_pending_payment, false, 'sentence') : |
|
| 2009 | - $this->pending_registration($notify); |
|
| 2010 | - break; |
|
| 2011 | - case EEM_Registration::status_id_not_approved : |
|
| 2012 | - case EEH_Template::pretty_status(EEM_Registration::status_id_not_approved, false, 'sentence') : |
|
| 2013 | - $this->not_approve_registration($notify); |
|
| 2014 | - break; |
|
| 2015 | - case EEM_Registration::status_id_declined : |
|
| 2016 | - case EEH_Template::pretty_status(EEM_Registration::status_id_declined, false, 'sentence') : |
|
| 2017 | - $this->decline_registration($notify); |
|
| 2018 | - break; |
|
| 2019 | - case EEM_Registration::status_id_cancelled : |
|
| 2020 | - case EEH_Template::pretty_status(EEM_Registration::status_id_cancelled, false, 'sentence') : |
|
| 2021 | - $this->cancel_registration($notify); |
|
| 2022 | - break; |
|
| 2023 | - case EEM_Registration::status_id_wait_list : |
|
| 2024 | - case EEH_Template::pretty_status(EEM_Registration::status_id_wait_list, false, 'sentence') : |
|
| 2025 | - $this->wait_list_registration($notify); |
|
| 2026 | - break; |
|
| 2027 | - case EEM_Registration::status_id_incomplete : |
|
| 2028 | - default : |
|
| 2029 | - $result['success'] = false; |
|
| 2030 | - unset($this->_req_data['return']); |
|
| 2031 | - $this->_reg_status_change_return('', false); |
|
| 2032 | - break; |
|
| 2033 | - } |
|
| 2034 | - } |
|
| 2035 | - |
|
| 2036 | - |
|
| 2037 | - /** |
|
| 2038 | - * Callback for bulk action routes. |
|
| 2039 | - * Note: although we could just register the singular route callbacks for each bulk action route as well, this |
|
| 2040 | - * method was chosen so there is one central place all the registration status bulk actions are going through. |
|
| 2041 | - * Potentially, this provides an easier place to locate logic that is specific to these bulk actions (as opposed to |
|
| 2042 | - * when an action is happening on just a single registration). |
|
| 2043 | - * @param $action |
|
| 2044 | - * @param bool $notify |
|
| 2045 | - */ |
|
| 2046 | - protected function bulk_action_on_registrations($action, $notify = false) { |
|
| 2047 | - do_action( |
|
| 2048 | - 'AHEE__Registrations_Admin_Page__bulk_action_on_registrations__before_execution', |
|
| 2049 | - $this, |
|
| 2050 | - $action, |
|
| 2051 | - $notify |
|
| 2052 | - ); |
|
| 2053 | - $method = $action . '_registration'; |
|
| 2054 | - if (method_exists($this, $method)) { |
|
| 2055 | - $this->$method($notify); |
|
| 2056 | - } |
|
| 2057 | - } |
|
| 2058 | - |
|
| 2059 | - |
|
| 2060 | - /** |
|
| 2061 | - * approve_registration |
|
| 2062 | - * |
|
| 2063 | - * @access protected |
|
| 2064 | - * @param bool $notify whether or not to notify the registrant about their approval. |
|
| 2065 | - * @return void |
|
| 2066 | - */ |
|
| 2067 | - protected function approve_registration($notify = false) |
|
| 2068 | - { |
|
| 2069 | - $this->_reg_status_change_return(EEM_Registration::status_id_approved, $notify); |
|
| 2070 | - } |
|
| 2071 | - |
|
| 2072 | - |
|
| 2073 | - /** |
|
| 2074 | - * decline_registration |
|
| 2075 | - * |
|
| 2076 | - * @access protected |
|
| 2077 | - * @param bool $notify whether or not to notify the registrant about their status change. |
|
| 2078 | - * @return void |
|
| 2079 | - */ |
|
| 2080 | - protected function decline_registration($notify = false) |
|
| 2081 | - { |
|
| 2082 | - $this->_reg_status_change_return(EEM_Registration::status_id_declined, $notify); |
|
| 2083 | - } |
|
| 2084 | - |
|
| 2085 | - |
|
| 2086 | - /** |
|
| 2087 | - * cancel_registration |
|
| 2088 | - * |
|
| 2089 | - * @access protected |
|
| 2090 | - * @param bool $notify whether or not to notify the registrant about their status change. |
|
| 2091 | - * @return void |
|
| 2092 | - */ |
|
| 2093 | - protected function cancel_registration($notify = false) |
|
| 2094 | - { |
|
| 2095 | - $this->_reg_status_change_return(EEM_Registration::status_id_cancelled, $notify); |
|
| 2096 | - } |
|
| 2097 | - |
|
| 2098 | - |
|
| 2099 | - /** |
|
| 2100 | - * not_approve_registration |
|
| 2101 | - * |
|
| 2102 | - * @access protected |
|
| 2103 | - * @param bool $notify whether or not to notify the registrant about their status change. |
|
| 2104 | - * @return void |
|
| 2105 | - */ |
|
| 2106 | - protected function not_approve_registration($notify = false) |
|
| 2107 | - { |
|
| 2108 | - $this->_reg_status_change_return(EEM_Registration::status_id_not_approved, $notify); |
|
| 2109 | - } |
|
| 2110 | - |
|
| 2111 | - |
|
| 2112 | - /** |
|
| 2113 | - * decline_registration |
|
| 2114 | - * |
|
| 2115 | - * @access protected |
|
| 2116 | - * @param bool $notify whether or not to notify the registrant about their status change. |
|
| 2117 | - * @return void |
|
| 2118 | - */ |
|
| 2119 | - protected function pending_registration($notify = false) |
|
| 2120 | - { |
|
| 2121 | - $this->_reg_status_change_return(EEM_Registration::status_id_pending_payment, $notify); |
|
| 2122 | - } |
|
| 2123 | - |
|
| 2124 | - |
|
| 2125 | - /** |
|
| 2126 | - * waitlist_registration |
|
| 2127 | - * |
|
| 2128 | - * @access protected |
|
| 2129 | - * @param bool $notify whether or not to notify the registrant about their status change. |
|
| 2130 | - * @return void |
|
| 2131 | - */ |
|
| 2132 | - protected function wait_list_registration($notify = false) |
|
| 2133 | - { |
|
| 2134 | - $this->_reg_status_change_return(EEM_Registration::status_id_wait_list, $notify); |
|
| 2135 | - } |
|
| 2136 | - |
|
| 2137 | - |
|
| 2138 | - /** |
|
| 2139 | - * generates HTML for the Registration main meta box |
|
| 2140 | - * |
|
| 2141 | - * @access public |
|
| 2142 | - * @return void |
|
| 2143 | - * @throws DomainException |
|
| 2144 | - * @throws EE_Error |
|
| 2145 | - * @throws InvalidArgumentException |
|
| 2146 | - * @throws InvalidDataTypeException |
|
| 2147 | - * @throws InvalidInterfaceException |
|
| 2148 | - * @throws ReflectionException |
|
| 2149 | - * @throws EntityNotFoundException |
|
| 2150 | - */ |
|
| 2151 | - public function _reg_details_meta_box() |
|
| 2152 | - { |
|
| 2153 | - EEH_Autoloader::register_line_item_display_autoloaders(); |
|
| 2154 | - EEH_Autoloader::register_line_item_filter_autoloaders(); |
|
| 2155 | - EE_Registry::instance()->load_helper('Line_Item'); |
|
| 2156 | - $transaction = $this->_registration->transaction() ? $this->_registration->transaction() |
|
| 2157 | - : EE_Transaction::new_instance(); |
|
| 2158 | - $this->_session = $transaction->session_data(); |
|
| 2159 | - $filters = new EE_Line_Item_Filter_Collection(); |
|
| 2160 | - //$filters->add( new EE_Non_Zero_Line_Item_Filter() ); |
|
| 2161 | - $filters->add(new EE_Single_Registration_Line_Item_Filter($this->_registration)); |
|
| 2162 | - $line_item_filter_processor = new EE_Line_Item_Filter_Processor($filters, |
|
| 2163 | - $transaction->total_line_item()); |
|
| 2164 | - $filtered_line_item_tree = $line_item_filter_processor->process(); |
|
| 2165 | - $line_item_display = new EE_Line_Item_Display('reg_admin_table', |
|
| 2166 | - 'EE_Admin_Table_Registration_Line_Item_Display_Strategy'); |
|
| 2167 | - $this->_template_args['line_item_table'] = $line_item_display->display_line_item( |
|
| 2168 | - $filtered_line_item_tree, |
|
| 2169 | - array('EE_Registration' => $this->_registration) |
|
| 2170 | - ); |
|
| 2171 | - $attendee = $this->_registration->attendee(); |
|
| 2172 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
| 2173 | - 'ee_read_transaction', |
|
| 2174 | - 'espresso_transactions_view_transaction' |
|
| 2175 | - )) { |
|
| 2176 | - $this->_template_args['view_transaction_button'] = EEH_Template::get_button_or_link( |
|
| 2177 | - EE_Admin_Page::add_query_args_and_nonce( |
|
| 2178 | - array( |
|
| 2179 | - 'action' => 'view_transaction', |
|
| 2180 | - 'TXN_ID' => $transaction->ID(), |
|
| 2181 | - ), |
|
| 2182 | - TXN_ADMIN_URL |
|
| 2183 | - ), |
|
| 2184 | - esc_html__(' View Transaction', 'event_espresso'), |
|
| 2185 | - 'button secondary-button right', |
|
| 2186 | - 'dashicons dashicons-cart' |
|
| 2187 | - ); |
|
| 2188 | - } else { |
|
| 2189 | - $this->_template_args['view_transaction_button'] = ''; |
|
| 2190 | - } |
|
| 2191 | - if ($attendee instanceof EE_Attendee |
|
| 2192 | - && EE_Registry::instance()->CAP->current_user_can( |
|
| 2193 | - 'ee_send_message', |
|
| 2194 | - 'espresso_registrations_resend_registration' |
|
| 2195 | - ) |
|
| 2196 | - ) { |
|
| 2197 | - $this->_template_args['resend_registration_button'] = EEH_Template::get_button_or_link( |
|
| 2198 | - EE_Admin_Page::add_query_args_and_nonce( |
|
| 2199 | - array( |
|
| 2200 | - 'action' => 'resend_registration', |
|
| 2201 | - '_REG_ID' => $this->_registration->ID(), |
|
| 2202 | - 'redirect_to' => 'view_registration', |
|
| 2203 | - ), |
|
| 2204 | - REG_ADMIN_URL |
|
| 2205 | - ), |
|
| 2206 | - esc_html__(' Resend Registration', 'event_espresso'), |
|
| 2207 | - 'button secondary-button right', |
|
| 2208 | - 'dashicons dashicons-email-alt' |
|
| 2209 | - ); |
|
| 2210 | - } else { |
|
| 2211 | - $this->_template_args['resend_registration_button'] = ''; |
|
| 2212 | - } |
|
| 2213 | - $this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign; |
|
| 2214 | - $payment = $transaction->get_first_related('Payment'); |
|
| 2215 | - $payment = ! $payment instanceof EE_Payment |
|
| 2216 | - ? EE_Payment::new_instance() |
|
| 2217 | - : $payment; |
|
| 2218 | - $payment_method = $payment->get_first_related('Payment_Method'); |
|
| 2219 | - $payment_method = ! $payment_method instanceof EE_Payment_Method |
|
| 2220 | - ? EE_Payment_Method::new_instance() |
|
| 2221 | - : $payment_method; |
|
| 2222 | - $reg_details = array( |
|
| 2223 | - 'payment_method' => $payment_method->name(), |
|
| 2224 | - 'response_msg' => $payment->gateway_response(), |
|
| 2225 | - 'registration_id' => $this->_registration->get('REG_code'), |
|
| 2226 | - 'registration_session' => $this->_registration->session_ID(), |
|
| 2227 | - 'ip_address' => isset($this->_session['ip_address']) ? $this->_session['ip_address'] : '', |
|
| 2228 | - 'user_agent' => isset($this->_session['user_agent']) ? $this->_session['user_agent'] : '', |
|
| 2229 | - ); |
|
| 2230 | - if (isset($reg_details['registration_id'])) { |
|
| 2231 | - $this->_template_args['reg_details']['registration_id']['value'] = $reg_details['registration_id']; |
|
| 2232 | - $this->_template_args['reg_details']['registration_id']['label'] = esc_html__( |
|
| 2233 | - 'Registration ID', |
|
| 2234 | - 'event_espresso' |
|
| 2235 | - ); |
|
| 2236 | - $this->_template_args['reg_details']['registration_id']['class'] = 'regular-text'; |
|
| 2237 | - } |
|
| 2238 | - if (isset($reg_details['payment_method'])) { |
|
| 2239 | - $this->_template_args['reg_details']['payment_method']['value'] = $reg_details['payment_method']; |
|
| 2240 | - $this->_template_args['reg_details']['payment_method']['label'] = esc_html__( |
|
| 2241 | - 'Most Recent Payment Method', |
|
| 2242 | - 'event_espresso' |
|
| 2243 | - ); |
|
| 2244 | - $this->_template_args['reg_details']['payment_method']['class'] = 'regular-text'; |
|
| 2245 | - $this->_template_args['reg_details']['response_msg']['value'] = $reg_details['response_msg']; |
|
| 2246 | - $this->_template_args['reg_details']['response_msg']['label'] = esc_html__( |
|
| 2247 | - 'Payment method response', |
|
| 2248 | - 'event_espresso' |
|
| 2249 | - ); |
|
| 2250 | - $this->_template_args['reg_details']['response_msg']['class'] = 'regular-text'; |
|
| 2251 | - } |
|
| 2252 | - $this->_template_args['reg_details']['registration_session']['value'] = $reg_details['registration_session']; |
|
| 2253 | - $this->_template_args['reg_details']['registration_session']['label'] = esc_html__( |
|
| 2254 | - 'Registration Session', |
|
| 2255 | - 'event_espresso' |
|
| 2256 | - ); |
|
| 2257 | - $this->_template_args['reg_details']['registration_session']['class'] = 'regular-text'; |
|
| 2258 | - $this->_template_args['reg_details']['ip_address']['value'] = $reg_details['ip_address']; |
|
| 2259 | - $this->_template_args['reg_details']['ip_address']['label'] = esc_html__( |
|
| 2260 | - 'Registration placed from IP', |
|
| 2261 | - 'event_espresso' |
|
| 2262 | - ); |
|
| 2263 | - $this->_template_args['reg_details']['ip_address']['class'] = 'regular-text'; |
|
| 2264 | - $this->_template_args['reg_details']['user_agent']['value'] = $reg_details['user_agent']; |
|
| 2265 | - $this->_template_args['reg_details']['user_agent']['label'] = esc_html__('Registrant User Agent', |
|
| 2266 | - 'event_espresso'); |
|
| 2267 | - $this->_template_args['reg_details']['user_agent']['class'] = 'large-text'; |
|
| 2268 | - $this->_template_args['event_link'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 2269 | - array( |
|
| 2270 | - 'action' => 'default', |
|
| 2271 | - 'event_id' => $this->_registration->event_ID(), |
|
| 2272 | - ), |
|
| 2273 | - REG_ADMIN_URL |
|
| 2274 | - ); |
|
| 2275 | - $this->_template_args['REG_ID'] = $this->_registration->ID(); |
|
| 2276 | - $this->_template_args['event_id'] = $this->_registration->event_ID(); |
|
| 2277 | - $template_path = |
|
| 2278 | - REG_TEMPLATE_PATH . 'reg_admin_details_main_meta_box_reg_details.template.php'; |
|
| 2279 | - echo EEH_Template::display_template($template_path, $this->_template_args, true); |
|
| 2280 | - } |
|
| 2281 | - |
|
| 2282 | - |
|
| 2283 | - /** |
|
| 2284 | - * generates HTML for the Registration Questions meta box. |
|
| 2285 | - * If pre-4.8.32.rc.000 hooks are used, uses old methods (with its filters), |
|
| 2286 | - * otherwise uses new forms system |
|
| 2287 | - * |
|
| 2288 | - * @access public |
|
| 2289 | - * @return void |
|
| 2290 | - * @throws DomainException |
|
| 2291 | - * @throws EE_Error |
|
| 2292 | - */ |
|
| 2293 | - public function _reg_questions_meta_box() |
|
| 2294 | - { |
|
| 2295 | - //allow someone to override this method entirely |
|
| 2296 | - if (apply_filters('FHEE__Registrations_Admin_Page___reg_questions_meta_box__do_default', true, $this, |
|
| 2297 | - $this->_registration)) { |
|
| 2298 | - $form = $this->_get_reg_custom_questions_form( |
|
| 2299 | - $this->_registration->ID() |
|
| 2300 | - ); |
|
| 2301 | - $this->_template_args['att_questions'] = count($form->subforms()) > 0 |
|
| 2302 | - ? $form->get_html_and_js() |
|
| 2303 | - : ''; |
|
| 2304 | - $this->_template_args['reg_questions_form_action'] = 'edit_registration'; |
|
| 2305 | - $this->_template_args['REG_ID'] = $this->_registration->ID(); |
|
| 2306 | - $template_path = |
|
| 2307 | - REG_TEMPLATE_PATH . 'reg_admin_details_main_meta_box_reg_questions.template.php'; |
|
| 2308 | - echo EEH_Template::display_template($template_path, $this->_template_args, true); |
|
| 2309 | - } |
|
| 2310 | - } |
|
| 2311 | - |
|
| 2312 | - |
|
| 2313 | - /** |
|
| 2314 | - * form_before_question_group |
|
| 2315 | - * |
|
| 2316 | - * @deprecated as of 4.8.32.rc.000 |
|
| 2317 | - * @access public |
|
| 2318 | - * @param string $output |
|
| 2319 | - * @return string |
|
| 2320 | - */ |
|
| 2321 | - public function form_before_question_group($output) |
|
| 2322 | - { |
|
| 2323 | - EE_Error::doing_it_wrong( |
|
| 2324 | - __CLASS__ . '::' . __FUNCTION__, |
|
| 2325 | - esc_html__( |
|
| 2326 | - 'This method would have been protected but was used on a filter callback so needed to be public. Please discontinue usage as it will be removed soon.', |
|
| 2327 | - 'event_espresso' |
|
| 2328 | - ), |
|
| 2329 | - '4.8.32.rc.000' |
|
| 2330 | - ); |
|
| 2331 | - return ' |
|
| 1298 | + if (! empty($registration_status)) { |
|
| 1299 | + $where['STS_ID'] = $registration_status; |
|
| 1300 | + } else { |
|
| 1301 | + //make sure we exclude incomplete registrations, but only if not trashed. |
|
| 1302 | + if ($view === 'trash') { |
|
| 1303 | + $where['REG_deleted'] = true; |
|
| 1304 | + } elseif ($view === 'incomplete') { |
|
| 1305 | + $where['STS_ID'] = EEM_Registration::status_id_incomplete; |
|
| 1306 | + } else { |
|
| 1307 | + $where['STS_ID'] = array('!=', EEM_Registration::status_id_incomplete); |
|
| 1308 | + } |
|
| 1309 | + } |
|
| 1310 | + return $where; |
|
| 1311 | + } |
|
| 1312 | + |
|
| 1313 | + |
|
| 1314 | + /** |
|
| 1315 | + * Adds any provided date restraints to the where conditions for the registrations query. |
|
| 1316 | + * |
|
| 1317 | + * @param array $request usually the same as $this->_req_data but not necessarily |
|
| 1318 | + * @return array |
|
| 1319 | + * @throws EE_Error |
|
| 1320 | + * @throws InvalidArgumentException |
|
| 1321 | + * @throws InvalidDataTypeException |
|
| 1322 | + * @throws InvalidInterfaceException |
|
| 1323 | + */ |
|
| 1324 | + protected function _add_date_to_where_conditions(array $request) |
|
| 1325 | + { |
|
| 1326 | + $where = array(); |
|
| 1327 | + $view = EEH_Array::is_set($request, 'status', ''); |
|
| 1328 | + $month_range = ! empty($request['month_range']) |
|
| 1329 | + ? sanitize_text_field($request['month_range']) |
|
| 1330 | + : ''; |
|
| 1331 | + $retrieve_for_today = $view === 'today'; |
|
| 1332 | + $retrieve_for_this_month = $view === 'month'; |
|
| 1333 | + |
|
| 1334 | + if ($retrieve_for_today) { |
|
| 1335 | + $now = date('Y-m-d', current_time('timestamp')); |
|
| 1336 | + $where['REG_date'] = array( |
|
| 1337 | + 'BETWEEN', |
|
| 1338 | + array( |
|
| 1339 | + EEM_Registration::instance()->convert_datetime_for_query( |
|
| 1340 | + 'REG_date', |
|
| 1341 | + $now . ' 00:00:00', |
|
| 1342 | + 'Y-m-d H:i:s' |
|
| 1343 | + ), |
|
| 1344 | + EEM_Registration::instance()->convert_datetime_for_query( |
|
| 1345 | + 'REG_date', |
|
| 1346 | + $now . ' 23:59:59', |
|
| 1347 | + 'Y-m-d H:i:s' |
|
| 1348 | + ), |
|
| 1349 | + ), |
|
| 1350 | + ); |
|
| 1351 | + } elseif ($retrieve_for_this_month) { |
|
| 1352 | + $current_year_and_month = date('Y-m', current_time('timestamp')); |
|
| 1353 | + $days_this_month = date('t', current_time('timestamp')); |
|
| 1354 | + $where['REG_date'] = array( |
|
| 1355 | + 'BETWEEN', |
|
| 1356 | + array( |
|
| 1357 | + EEM_Registration::instance()->convert_datetime_for_query( |
|
| 1358 | + 'REG_date', |
|
| 1359 | + $current_year_and_month . '-01 00:00:00', |
|
| 1360 | + 'Y-m-d H:i:s' |
|
| 1361 | + ), |
|
| 1362 | + EEM_Registration::instance()->convert_datetime_for_query( |
|
| 1363 | + 'REG_date', |
|
| 1364 | + $current_year_and_month . '-' . $days_this_month . ' 23:59:59', |
|
| 1365 | + 'Y-m-d H:i:s' |
|
| 1366 | + ), |
|
| 1367 | + ), |
|
| 1368 | + ); |
|
| 1369 | + } elseif ($month_range) { |
|
| 1370 | + $pieces = explode(' ', $month_range, 3); |
|
| 1371 | + $month_requested = ! empty($pieces[0]) |
|
| 1372 | + ? date('m', \EEH_DTT_Helper::first_of_month_timestamp($pieces[0])) |
|
| 1373 | + : ''; |
|
| 1374 | + $year_requested = ! empty($pieces[1]) |
|
| 1375 | + ? $pieces[1] |
|
| 1376 | + : ''; |
|
| 1377 | + //if there is not a month or year then we can't go further |
|
| 1378 | + if ($month_requested && $year_requested) { |
|
| 1379 | + $days_in_month = date('t', strtotime($year_requested . '-' . $month_requested . '-' . '01')); |
|
| 1380 | + $where['REG_date'] = array( |
|
| 1381 | + 'BETWEEN', |
|
| 1382 | + array( |
|
| 1383 | + EEM_Registration::instance()->convert_datetime_for_query( |
|
| 1384 | + 'REG_date', |
|
| 1385 | + $year_requested . '-' . $month_requested . '-01 00:00:00', |
|
| 1386 | + 'Y-m-d H:i:s' |
|
| 1387 | + ), |
|
| 1388 | + EEM_Registration::instance()->convert_datetime_for_query( |
|
| 1389 | + 'REG_date', |
|
| 1390 | + $year_requested . '-' . $month_requested . '-' . $days_in_month . ' 23:59:59', |
|
| 1391 | + 'Y-m-d H:i:s' |
|
| 1392 | + ), |
|
| 1393 | + ), |
|
| 1394 | + ); |
|
| 1395 | + } |
|
| 1396 | + } |
|
| 1397 | + return $where; |
|
| 1398 | + } |
|
| 1399 | + |
|
| 1400 | + |
|
| 1401 | + /** |
|
| 1402 | + * Adds any provided search restraints to the where conditions for the registrations query |
|
| 1403 | + * |
|
| 1404 | + * @param array $request usually the same as $this->_req_data but not necessarily |
|
| 1405 | + * @return array |
|
| 1406 | + */ |
|
| 1407 | + protected function _add_search_to_where_conditions(array $request) |
|
| 1408 | + { |
|
| 1409 | + $where = array(); |
|
| 1410 | + if (! empty($request['s'])) { |
|
| 1411 | + $search_string = '%' . sanitize_text_field($request['s']) . '%'; |
|
| 1412 | + $where['OR*search_conditions'] = array( |
|
| 1413 | + 'Event.EVT_name' => array('LIKE', $search_string), |
|
| 1414 | + 'Event.EVT_desc' => array('LIKE', $search_string), |
|
| 1415 | + 'Event.EVT_short_desc' => array('LIKE', $search_string), |
|
| 1416 | + 'Attendee.ATT_full_name' => array('LIKE', $search_string), |
|
| 1417 | + 'Attendee.ATT_fname' => array('LIKE', $search_string), |
|
| 1418 | + 'Attendee.ATT_lname' => array('LIKE', $search_string), |
|
| 1419 | + 'Attendee.ATT_short_bio' => array('LIKE', $search_string), |
|
| 1420 | + 'Attendee.ATT_email' => array('LIKE', $search_string), |
|
| 1421 | + 'Attendee.ATT_address' => array('LIKE', $search_string), |
|
| 1422 | + 'Attendee.ATT_address2' => array('LIKE', $search_string), |
|
| 1423 | + 'Attendee.ATT_city' => array('LIKE', $search_string), |
|
| 1424 | + 'REG_final_price' => array('LIKE', $search_string), |
|
| 1425 | + 'REG_code' => array('LIKE', $search_string), |
|
| 1426 | + 'REG_count' => array('LIKE', $search_string), |
|
| 1427 | + 'REG_group_size' => array('LIKE', $search_string), |
|
| 1428 | + 'Ticket.TKT_name' => array('LIKE', $search_string), |
|
| 1429 | + 'Ticket.TKT_description' => array('LIKE', $search_string), |
|
| 1430 | + 'Transaction.Payment.PAY_txn_id_chq_nmbr' => array('LIKE', $search_string), |
|
| 1431 | + ); |
|
| 1432 | + } |
|
| 1433 | + return $where; |
|
| 1434 | + } |
|
| 1435 | + |
|
| 1436 | + |
|
| 1437 | + /** |
|
| 1438 | + * Sets up the where conditions for the registrations query. |
|
| 1439 | + * |
|
| 1440 | + * @param array $request |
|
| 1441 | + * @return array |
|
| 1442 | + * @throws EE_Error |
|
| 1443 | + */ |
|
| 1444 | + protected function _get_where_conditions_for_registrations_query($request) |
|
| 1445 | + { |
|
| 1446 | + return apply_filters( |
|
| 1447 | + 'FHEE__Registrations_Admin_Page___get_where_conditions_for_registrations_query', |
|
| 1448 | + array_merge( |
|
| 1449 | + $this->addAttendeeIdToWhereConditions($request), |
|
| 1450 | + $this->_add_event_id_to_where_conditions($request), |
|
| 1451 | + $this->_add_category_id_to_where_conditions($request), |
|
| 1452 | + $this->_add_datetime_id_to_where_conditions($request), |
|
| 1453 | + $this->_add_registration_status_to_where_conditions($request), |
|
| 1454 | + $this->_add_date_to_where_conditions($request), |
|
| 1455 | + $this->_add_search_to_where_conditions($request) |
|
| 1456 | + ), |
|
| 1457 | + $request |
|
| 1458 | + ); |
|
| 1459 | + } |
|
| 1460 | + |
|
| 1461 | + |
|
| 1462 | + /** |
|
| 1463 | + * Sets up the orderby for the registrations query. |
|
| 1464 | + * |
|
| 1465 | + * @return array |
|
| 1466 | + */ |
|
| 1467 | + protected function _get_orderby_for_registrations_query() |
|
| 1468 | + { |
|
| 1469 | + $orderby_field = ! empty($this->_req_data['orderby']) |
|
| 1470 | + ? sanitize_text_field($this->_req_data['orderby']) |
|
| 1471 | + : ''; |
|
| 1472 | + switch ($orderby_field) { |
|
| 1473 | + case '_REG_ID': |
|
| 1474 | + $orderby = array('REG_ID'); |
|
| 1475 | + break; |
|
| 1476 | + case '_Reg_status': |
|
| 1477 | + $orderby = array('STS_ID'); |
|
| 1478 | + break; |
|
| 1479 | + case 'ATT_fname': |
|
| 1480 | + $orderby = array('Attendee.ATT_fname', 'Attendee.ATT_lname'); |
|
| 1481 | + break; |
|
| 1482 | + case 'ATT_lname': |
|
| 1483 | + $orderby = array('Attendee.ATT_lname', 'Attendee.ATT_fname'); |
|
| 1484 | + break; |
|
| 1485 | + case 'event_name': |
|
| 1486 | + $orderby = array('Event.EVT_name'); |
|
| 1487 | + break; |
|
| 1488 | + case 'DTT_EVT_start': |
|
| 1489 | + $orderby = array('Event.Datetime.DTT_EVT_start'); |
|
| 1490 | + break; |
|
| 1491 | + default: //'REG_date' |
|
| 1492 | + $orderby = array('REG_date'); |
|
| 1493 | + } |
|
| 1494 | + |
|
| 1495 | + //order |
|
| 1496 | + $order = ! empty($this->_req_data['order']) |
|
| 1497 | + ? sanitize_text_field($this->_req_data['order']) |
|
| 1498 | + : 'DESC'; |
|
| 1499 | + $orderby = array_combine( |
|
| 1500 | + $orderby, |
|
| 1501 | + array_fill(0, count($orderby), $order) |
|
| 1502 | + ); |
|
| 1503 | + //because there are many registrations with the same date, define |
|
| 1504 | + //a secondary way to order them, otherwise MySQL seems to be a bit random |
|
| 1505 | + if (empty($orderby['REG_ID'])) { |
|
| 1506 | + $orderby['REG_ID'] = $order; |
|
| 1507 | + } |
|
| 1508 | + return array('order_by' => $orderby); |
|
| 1509 | + } |
|
| 1510 | + |
|
| 1511 | + |
|
| 1512 | + /** |
|
| 1513 | + * Sets up the limit for the registrations query. |
|
| 1514 | + * |
|
| 1515 | + * @param $per_page |
|
| 1516 | + * @return array |
|
| 1517 | + */ |
|
| 1518 | + protected function _get_limit($per_page) |
|
| 1519 | + { |
|
| 1520 | + $current_page = ! empty($this->_req_data['paged']) |
|
| 1521 | + ? absint($this->_req_data['paged']) |
|
| 1522 | + : 1; |
|
| 1523 | + $per_page = ! empty($this->_req_data['perpage']) |
|
| 1524 | + ? $this->_req_data['perpage'] |
|
| 1525 | + : $per_page; |
|
| 1526 | + |
|
| 1527 | + //-1 means return all results so get out if that's set. |
|
| 1528 | + if ((int)$per_page === -1) { |
|
| 1529 | + return array(); |
|
| 1530 | + } |
|
| 1531 | + $per_page = absint($per_page); |
|
| 1532 | + $offset = ($current_page - 1) * $per_page; |
|
| 1533 | + return array('limit' => array($offset, $per_page)); |
|
| 1534 | + } |
|
| 1535 | + |
|
| 1536 | + |
|
| 1537 | + public function get_registration_status_array() |
|
| 1538 | + { |
|
| 1539 | + return self::$_reg_status; |
|
| 1540 | + } |
|
| 1541 | + |
|
| 1542 | + |
|
| 1543 | + |
|
| 1544 | + |
|
| 1545 | + /*************************************** REGISTRATION DETAILS ***************************************/ |
|
| 1546 | + /** |
|
| 1547 | + * generates HTML for the View Registration Details Admin page |
|
| 1548 | + * |
|
| 1549 | + * @access protected |
|
| 1550 | + * @return void |
|
| 1551 | + * @throws DomainException |
|
| 1552 | + * @throws EE_Error |
|
| 1553 | + * @throws InvalidArgumentException |
|
| 1554 | + * @throws InvalidDataTypeException |
|
| 1555 | + * @throws InvalidInterfaceException |
|
| 1556 | + * @throws EntityNotFoundException |
|
| 1557 | + */ |
|
| 1558 | + protected function _registration_details() |
|
| 1559 | + { |
|
| 1560 | + $this->_template_args = array(); |
|
| 1561 | + $this->_set_registration_object(); |
|
| 1562 | + if (is_object($this->_registration)) { |
|
| 1563 | + $transaction = $this->_registration->transaction() |
|
| 1564 | + ? $this->_registration->transaction() |
|
| 1565 | + : EE_Transaction::new_instance(); |
|
| 1566 | + $this->_session = $transaction->session_data(); |
|
| 1567 | + $event_id = $this->_registration->event_ID(); |
|
| 1568 | + $this->_template_args['reg_nmbr']['value'] = $this->_registration->ID(); |
|
| 1569 | + $this->_template_args['reg_nmbr']['label'] = esc_html__('Registration Number', 'event_espresso'); |
|
| 1570 | + $this->_template_args['reg_datetime']['value'] = $this->_registration->get_i18n_datetime('REG_date'); |
|
| 1571 | + $this->_template_args['reg_datetime']['label'] = esc_html__('Date', 'event_espresso'); |
|
| 1572 | + $this->_template_args['grand_total'] = $transaction->total(); |
|
| 1573 | + $this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign; |
|
| 1574 | + // link back to overview |
|
| 1575 | + $this->_template_args['reg_overview_url'] = REG_ADMIN_URL; |
|
| 1576 | + $this->_template_args['registration'] = $this->_registration; |
|
| 1577 | + $this->_template_args['filtered_registrations_link'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 1578 | + array( |
|
| 1579 | + 'action' => 'default', |
|
| 1580 | + 'event_id' => $event_id, |
|
| 1581 | + ), |
|
| 1582 | + REG_ADMIN_URL |
|
| 1583 | + ); |
|
| 1584 | + $this->_template_args['filtered_transactions_link'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 1585 | + array( |
|
| 1586 | + 'action' => 'default', |
|
| 1587 | + 'EVT_ID' => $event_id, |
|
| 1588 | + 'page' => 'espresso_transactions', |
|
| 1589 | + ), |
|
| 1590 | + admin_url('admin.php') |
|
| 1591 | + ); |
|
| 1592 | + $this->_template_args['event_link'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 1593 | + array( |
|
| 1594 | + 'page' => 'espresso_events', |
|
| 1595 | + 'action' => 'edit', |
|
| 1596 | + 'post' => $event_id, |
|
| 1597 | + ), |
|
| 1598 | + admin_url('admin.php') |
|
| 1599 | + ); |
|
| 1600 | + //next and previous links |
|
| 1601 | + $next_reg = $this->_registration->next( |
|
| 1602 | + null, |
|
| 1603 | + array(), |
|
| 1604 | + 'REG_ID' |
|
| 1605 | + ); |
|
| 1606 | + $this->_template_args['next_registration'] = $next_reg |
|
| 1607 | + ? $this->_next_link( |
|
| 1608 | + EE_Admin_Page::add_query_args_and_nonce( |
|
| 1609 | + array( |
|
| 1610 | + 'action' => 'view_registration', |
|
| 1611 | + '_REG_ID' => $next_reg['REG_ID'], |
|
| 1612 | + ), |
|
| 1613 | + REG_ADMIN_URL |
|
| 1614 | + ), |
|
| 1615 | + 'dashicons dashicons-arrow-right ee-icon-size-22' |
|
| 1616 | + ) |
|
| 1617 | + : ''; |
|
| 1618 | + $previous_reg = $this->_registration->previous( |
|
| 1619 | + null, |
|
| 1620 | + array(), |
|
| 1621 | + 'REG_ID' |
|
| 1622 | + ); |
|
| 1623 | + $this->_template_args['previous_registration'] = $previous_reg |
|
| 1624 | + ? $this->_previous_link( |
|
| 1625 | + EE_Admin_Page::add_query_args_and_nonce( |
|
| 1626 | + array( |
|
| 1627 | + 'action' => 'view_registration', |
|
| 1628 | + '_REG_ID' => $previous_reg['REG_ID'], |
|
| 1629 | + ), |
|
| 1630 | + REG_ADMIN_URL |
|
| 1631 | + ), |
|
| 1632 | + 'dashicons dashicons-arrow-left ee-icon-size-22' |
|
| 1633 | + ) |
|
| 1634 | + : ''; |
|
| 1635 | + // grab header |
|
| 1636 | + $template_path = REG_TEMPLATE_PATH . 'reg_admin_details_header.template.php'; |
|
| 1637 | + $this->_template_args['REG_ID'] = $this->_registration->ID(); |
|
| 1638 | + $this->_template_args['admin_page_header'] = EEH_Template::display_template( |
|
| 1639 | + $template_path, |
|
| 1640 | + $this->_template_args, |
|
| 1641 | + true |
|
| 1642 | + ); |
|
| 1643 | + } else { |
|
| 1644 | + $this->_template_args['admin_page_header'] = $this->display_espresso_notices(); |
|
| 1645 | + } |
|
| 1646 | + // the details template wrapper |
|
| 1647 | + $this->display_admin_page_with_sidebar(); |
|
| 1648 | + } |
|
| 1649 | + |
|
| 1650 | + |
|
| 1651 | + protected function _registration_details_metaboxes() |
|
| 1652 | + { |
|
| 1653 | + do_action('AHEE__Registrations_Admin_Page___registration_details_metabox__start', $this); |
|
| 1654 | + $this->_set_registration_object(); |
|
| 1655 | + $attendee = $this->_registration instanceof EE_Registration ? $this->_registration->attendee() : null; |
|
| 1656 | + add_meta_box('edit-reg-status-mbox', esc_html__('Registration Status', 'event_espresso'), |
|
| 1657 | + array($this, 'set_reg_status_buttons_metabox'), $this->wp_page_slug, 'normal', 'high'); |
|
| 1658 | + add_meta_box('edit-reg-details-mbox', esc_html__('Registration Details', 'event_espresso'), |
|
| 1659 | + array($this, '_reg_details_meta_box'), $this->wp_page_slug, 'normal', 'high'); |
|
| 1660 | + if ($attendee instanceof EE_Attendee |
|
| 1661 | + && EE_Registry::instance()->CAP->current_user_can( |
|
| 1662 | + 'ee_edit_registration', |
|
| 1663 | + 'edit-reg-questions-mbox', |
|
| 1664 | + $this->_registration->ID() |
|
| 1665 | + ) |
|
| 1666 | + ) { |
|
| 1667 | + add_meta_box( |
|
| 1668 | + 'edit-reg-questions-mbox', |
|
| 1669 | + esc_html__('Registration Form Answers', 'event_espresso'), |
|
| 1670 | + array($this, '_reg_questions_meta_box'), |
|
| 1671 | + $this->wp_page_slug, |
|
| 1672 | + 'normal', |
|
| 1673 | + 'high' |
|
| 1674 | + ); |
|
| 1675 | + } |
|
| 1676 | + add_meta_box( |
|
| 1677 | + 'edit-reg-registrant-mbox', |
|
| 1678 | + esc_html__('Contact Details', 'event_espresso'), |
|
| 1679 | + array($this, '_reg_registrant_side_meta_box'), |
|
| 1680 | + $this->wp_page_slug, |
|
| 1681 | + 'side', |
|
| 1682 | + 'high' |
|
| 1683 | + ); |
|
| 1684 | + if ($this->_registration->group_size() > 1) { |
|
| 1685 | + add_meta_box( |
|
| 1686 | + 'edit-reg-attendees-mbox', |
|
| 1687 | + esc_html__('Other Registrations in this Transaction', 'event_espresso'), |
|
| 1688 | + array($this, '_reg_attendees_meta_box'), |
|
| 1689 | + $this->wp_page_slug, |
|
| 1690 | + 'normal', |
|
| 1691 | + 'high' |
|
| 1692 | + ); |
|
| 1693 | + } |
|
| 1694 | + } |
|
| 1695 | + |
|
| 1696 | + |
|
| 1697 | + /** |
|
| 1698 | + * set_reg_status_buttons_metabox |
|
| 1699 | + * |
|
| 1700 | + * @access protected |
|
| 1701 | + * @return string |
|
| 1702 | + * @throws \EE_Error |
|
| 1703 | + */ |
|
| 1704 | + public function set_reg_status_buttons_metabox() |
|
| 1705 | + { |
|
| 1706 | + $this->_set_registration_object(); |
|
| 1707 | + $change_reg_status_form = $this->_generate_reg_status_change_form(); |
|
| 1708 | + echo $change_reg_status_form->form_open( |
|
| 1709 | + self::add_query_args_and_nonce( |
|
| 1710 | + array( |
|
| 1711 | + 'action' => 'change_reg_status', |
|
| 1712 | + ), |
|
| 1713 | + REG_ADMIN_URL |
|
| 1714 | + ) |
|
| 1715 | + ); |
|
| 1716 | + echo $change_reg_status_form->get_html(); |
|
| 1717 | + echo $change_reg_status_form->form_close(); |
|
| 1718 | + } |
|
| 1719 | + |
|
| 1720 | + |
|
| 1721 | + /** |
|
| 1722 | + * @return EE_Form_Section_Proper |
|
| 1723 | + * @throws EE_Error |
|
| 1724 | + * @throws InvalidArgumentException |
|
| 1725 | + * @throws InvalidDataTypeException |
|
| 1726 | + * @throws InvalidInterfaceException |
|
| 1727 | + * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 1728 | + */ |
|
| 1729 | + protected function _generate_reg_status_change_form() |
|
| 1730 | + { |
|
| 1731 | + return new EE_Form_Section_Proper(array( |
|
| 1732 | + 'name' => 'reg_status_change_form', |
|
| 1733 | + 'html_id' => 'reg-status-change-form', |
|
| 1734 | + 'layout_strategy' => new EE_Admin_Two_Column_Layout(), |
|
| 1735 | + 'subsections' => array( |
|
| 1736 | + 'return' => new EE_Hidden_Input(array( |
|
| 1737 | + 'name' => 'return', |
|
| 1738 | + 'default' => 'view_registration', |
|
| 1739 | + )), |
|
| 1740 | + 'REG_ID' => new EE_Hidden_Input(array( |
|
| 1741 | + 'name' => 'REG_ID', |
|
| 1742 | + 'default' => $this->_registration->ID(), |
|
| 1743 | + )), |
|
| 1744 | + 'current_status' => new EE_Form_Section_HTML( |
|
| 1745 | + EEH_HTML::tr( |
|
| 1746 | + EEH_HTML::th( |
|
| 1747 | + EEH_HTML::label( |
|
| 1748 | + EEH_HTML::strong(esc_html__('Current Registration Status', 'event_espresso') |
|
| 1749 | + ) |
|
| 1750 | + ) |
|
| 1751 | + ) |
|
| 1752 | + . EEH_HTML::td( |
|
| 1753 | + EEH_HTML::strong( |
|
| 1754 | + $this->_registration->pretty_status(), |
|
| 1755 | + '', |
|
| 1756 | + 'status-' . $this->_registration->status_ID(), |
|
| 1757 | + 'line-height: 1em; font-size: 1.5em; font-weight: bold;' |
|
| 1758 | + ) |
|
| 1759 | + ) |
|
| 1760 | + ) |
|
| 1761 | + ), |
|
| 1762 | + 'reg_status' => new EE_Select_Input( |
|
| 1763 | + $this->_get_reg_statuses(), |
|
| 1764 | + array( |
|
| 1765 | + 'html_label_text' => esc_html__('Change Registration Status to', 'event_espresso'), |
|
| 1766 | + 'default' => $this->_registration->status_ID(), |
|
| 1767 | + ) |
|
| 1768 | + ), |
|
| 1769 | + 'send_notifications' => new EE_Yes_No_Input( |
|
| 1770 | + array( |
|
| 1771 | + 'html_label_text' => esc_html__('Send Related Messages', 'event_espresso'), |
|
| 1772 | + 'default' => false, |
|
| 1773 | + 'html_help_text' => esc_html__( |
|
| 1774 | + 'If set to "Yes", then the related messages will be sent to the registrant.', |
|
| 1775 | + 'event_espresso' |
|
| 1776 | + ), |
|
| 1777 | + ) |
|
| 1778 | + ), |
|
| 1779 | + 'submit' => new EE_Submit_Input( |
|
| 1780 | + array( |
|
| 1781 | + 'html_class' => 'button-primary', |
|
| 1782 | + 'html_label_text' => ' ', |
|
| 1783 | + 'default' => esc_html__('Update Registration Status', 'event_espresso'), |
|
| 1784 | + ) |
|
| 1785 | + ), |
|
| 1786 | + ), |
|
| 1787 | + )); |
|
| 1788 | + } |
|
| 1789 | + |
|
| 1790 | + |
|
| 1791 | + /** |
|
| 1792 | + * Returns an array of all the buttons for the various statuses and switch status actions |
|
| 1793 | + * |
|
| 1794 | + * @return array |
|
| 1795 | + * @throws EE_Error |
|
| 1796 | + * @throws InvalidArgumentException |
|
| 1797 | + * @throws InvalidDataTypeException |
|
| 1798 | + * @throws InvalidInterfaceException |
|
| 1799 | + * @throws EntityNotFoundException |
|
| 1800 | + */ |
|
| 1801 | + protected function _get_reg_statuses() |
|
| 1802 | + { |
|
| 1803 | + $reg_status_array = EEM_Registration::instance()->reg_status_array(); |
|
| 1804 | + unset ($reg_status_array[EEM_Registration::status_id_incomplete]); |
|
| 1805 | + // get current reg status |
|
| 1806 | + $current_status = $this->_registration->status_ID(); |
|
| 1807 | + // is registration for free event? This will determine whether to display the pending payment option |
|
| 1808 | + if ( |
|
| 1809 | + $current_status !== EEM_Registration::status_id_pending_payment |
|
| 1810 | + && EEH_Money::compare_floats($this->_registration->ticket()->price(), 0.00) |
|
| 1811 | + ) { |
|
| 1812 | + unset($reg_status_array[EEM_Registration::status_id_pending_payment]); |
|
| 1813 | + } |
|
| 1814 | + return EEM_Status::instance()->localized_status($reg_status_array, false, 'sentence'); |
|
| 1815 | + } |
|
| 1816 | + |
|
| 1817 | + |
|
| 1818 | + /** |
|
| 1819 | + * This method is used when using _REG_ID from request which may or may not be an array of reg_ids. |
|
| 1820 | + * |
|
| 1821 | + * @param bool $status REG status given for changing registrations to. |
|
| 1822 | + * @param bool $notify Whether to send messages notifications or not. |
|
| 1823 | + * @return array (array with reg_id(s) updated and whether update was successful. |
|
| 1824 | + * @throws EE_Error |
|
| 1825 | + * @throws InvalidArgumentException |
|
| 1826 | + * @throws InvalidDataTypeException |
|
| 1827 | + * @throws InvalidInterfaceException |
|
| 1828 | + * @throws ReflectionException |
|
| 1829 | + * @throws RuntimeException |
|
| 1830 | + * @throws EntityNotFoundException |
|
| 1831 | + */ |
|
| 1832 | + protected function _set_registration_status_from_request($status = false, $notify = false) |
|
| 1833 | + { |
|
| 1834 | + if (isset($this->_req_data['reg_status_change_form'])) { |
|
| 1835 | + $REG_IDs = isset($this->_req_data['reg_status_change_form']['REG_ID']) |
|
| 1836 | + ? (array)$this->_req_data['reg_status_change_form']['REG_ID'] |
|
| 1837 | + : array(); |
|
| 1838 | + } else { |
|
| 1839 | + $REG_IDs = isset($this->_req_data['_REG_ID']) |
|
| 1840 | + ? (array)$this->_req_data['_REG_ID'] |
|
| 1841 | + : array(); |
|
| 1842 | + } |
|
| 1843 | + // sanitize $REG_IDs |
|
| 1844 | + $REG_IDs = array_map('absint', $REG_IDs); |
|
| 1845 | + // and remove empty entries |
|
| 1846 | + $REG_IDs = array_filter($REG_IDs); |
|
| 1847 | + |
|
| 1848 | + $result = $this->_set_registration_status($REG_IDs, $status, $notify); |
|
| 1849 | + |
|
| 1850 | + /** |
|
| 1851 | + * Set and filter $_req_data['_REG_ID'] for any potential future messages notifications. |
|
| 1852 | + * Currently this value is used downstream by the _process_resend_registration method. |
|
| 1853 | + * |
|
| 1854 | + * @param int|array $registration_ids The registration ids that have had their status changed successfully. |
|
| 1855 | + * @param bool $status The status registrations were changed to. |
|
| 1856 | + * @param bool $success If the status was changed successfully for all registrations. |
|
| 1857 | + * @param Registrations_Admin_Page $admin_page_object |
|
| 1858 | + */ |
|
| 1859 | + $this->_req_data['_REG_ID'] = apply_filters( |
|
| 1860 | + 'FHEE__Registrations_Admin_Page___set_registration_status_from_request__REG_IDs', |
|
| 1861 | + $result['REG_ID'], |
|
| 1862 | + $status, |
|
| 1863 | + $result['success'], |
|
| 1864 | + $this |
|
| 1865 | + ); |
|
| 1866 | + |
|
| 1867 | + //notify? |
|
| 1868 | + if ($notify |
|
| 1869 | + && $result['success'] |
|
| 1870 | + && ! empty($this->_req_data['_REG_ID']) |
|
| 1871 | + && EE_Registry::instance()->CAP->current_user_can( |
|
| 1872 | + 'ee_send_message', |
|
| 1873 | + 'espresso_registrations_resend_registration' |
|
| 1874 | + ) |
|
| 1875 | + ) { |
|
| 1876 | + $this->_process_resend_registration(); |
|
| 1877 | + } |
|
| 1878 | + return $result; |
|
| 1879 | + } |
|
| 1880 | + |
|
| 1881 | + |
|
| 1882 | + /** |
|
| 1883 | + * Set the registration status for the given reg_id (which may or may not be an array, it gets typecast to an |
|
| 1884 | + * array). Note, this method does NOT take care of possible notifications. That is required by calling code. |
|
| 1885 | + * |
|
| 1886 | + * @param array $REG_IDs |
|
| 1887 | + * @param string $status |
|
| 1888 | + * @param bool $notify Used to indicate whether notification was requested or not. This determines the context |
|
| 1889 | + * slug sent with setting the registration status. |
|
| 1890 | + * @return array (an array with 'success' key representing whether status change was successful, and 'REG_ID' as |
|
| 1891 | + * @throws EE_Error |
|
| 1892 | + * @throws InvalidArgumentException |
|
| 1893 | + * @throws InvalidDataTypeException |
|
| 1894 | + * @throws InvalidInterfaceException |
|
| 1895 | + * @throws ReflectionException |
|
| 1896 | + * @throws RuntimeException |
|
| 1897 | + * @throws EntityNotFoundException |
|
| 1898 | + */ |
|
| 1899 | + protected function _set_registration_status($REG_IDs = array(), $status = '', $notify = false) |
|
| 1900 | + { |
|
| 1901 | + $success = false; |
|
| 1902 | + // typecast $REG_IDs |
|
| 1903 | + $REG_IDs = (array)$REG_IDs; |
|
| 1904 | + if ( ! empty($REG_IDs)) { |
|
| 1905 | + $success = true; |
|
| 1906 | + // set default status if none is passed |
|
| 1907 | + $status = $status ? $status : EEM_Registration::status_id_pending_payment; |
|
| 1908 | + $status_context = $notify |
|
| 1909 | + ? Domain::CONTEXT_REGISTRATION_STATUS_CHANGE_REGISTRATION_ADMIN_NOTIFY |
|
| 1910 | + : Domain::CONTEXT_REGISTRATION_STATUS_CHANGE_REGISTRATION_ADMIN; |
|
| 1911 | + //loop through REG_ID's and change status |
|
| 1912 | + foreach ($REG_IDs as $REG_ID) { |
|
| 1913 | + $registration = EEM_Registration::instance()->get_one_by_ID($REG_ID); |
|
| 1914 | + if ($registration instanceof EE_Registration) { |
|
| 1915 | + $registration->set_status( |
|
| 1916 | + $status, |
|
| 1917 | + false, |
|
| 1918 | + new Context( |
|
| 1919 | + $status_context, |
|
| 1920 | + esc_html__( |
|
| 1921 | + 'Manually triggered status change on a Registration Admin Page route.', |
|
| 1922 | + 'event_espresso' |
|
| 1923 | + ) |
|
| 1924 | + ) |
|
| 1925 | + ); |
|
| 1926 | + $result = $registration->save(); |
|
| 1927 | + // verifying explicit fails because update *may* just return 0 for 0 rows affected |
|
| 1928 | + $success = $result !== false ? $success : false; |
|
| 1929 | + } |
|
| 1930 | + } |
|
| 1931 | + } |
|
| 1932 | + |
|
| 1933 | + //return $success and processed registrations |
|
| 1934 | + return array('REG_ID' => $REG_IDs, 'success' => $success); |
|
| 1935 | + } |
|
| 1936 | + |
|
| 1937 | + |
|
| 1938 | + /** |
|
| 1939 | + * Common logic for setting up success message and redirecting to appropriate route |
|
| 1940 | + * |
|
| 1941 | + * @param string $STS_ID status id for the registration changed to |
|
| 1942 | + * @param bool $notify indicates whether the _set_registration_status_from_request does notifications or not. |
|
| 1943 | + * @return void |
|
| 1944 | + * @throws EE_Error |
|
| 1945 | + */ |
|
| 1946 | + protected function _reg_status_change_return($STS_ID, $notify = false) |
|
| 1947 | + { |
|
| 1948 | + $result = ! empty($STS_ID) ? $this->_set_registration_status_from_request($STS_ID, $notify) |
|
| 1949 | + : array('success' => false); |
|
| 1950 | + $success = isset($result['success']) && $result['success']; |
|
| 1951 | + //setup success message |
|
| 1952 | + if ($success) { |
|
| 1953 | + if (is_array($result['REG_ID']) && count($result['REG_ID']) === 1) { |
|
| 1954 | + $msg = sprintf(esc_html__('Registration status has been set to %s', 'event_espresso'), |
|
| 1955 | + EEH_Template::pretty_status($STS_ID, false, 'lower')); |
|
| 1956 | + } else { |
|
| 1957 | + $msg = sprintf(esc_html__('Registrations have been set to %s.', 'event_espresso'), |
|
| 1958 | + EEH_Template::pretty_status($STS_ID, false, 'lower')); |
|
| 1959 | + } |
|
| 1960 | + EE_Error::add_success($msg); |
|
| 1961 | + } else { |
|
| 1962 | + EE_Error::add_error( |
|
| 1963 | + esc_html__( |
|
| 1964 | + 'Something went wrong, and the status was not changed', |
|
| 1965 | + 'event_espresso' |
|
| 1966 | + ), __FILE__, __LINE__, __FUNCTION__ |
|
| 1967 | + ); |
|
| 1968 | + } |
|
| 1969 | + if (isset($this->_req_data['return']) && $this->_req_data['return'] == 'view_registration') { |
|
| 1970 | + $route = array('action' => 'view_registration', '_REG_ID' => reset($result['REG_ID'])); |
|
| 1971 | + } else { |
|
| 1972 | + $route = array('action' => 'default'); |
|
| 1973 | + } |
|
| 1974 | + //unset nonces |
|
| 1975 | + foreach ($this->_req_data as $ref => $value) { |
|
| 1976 | + if (strpos($ref, 'nonce') !== false) { |
|
| 1977 | + unset($this->_req_data[$ref]); |
|
| 1978 | + continue; |
|
| 1979 | + } |
|
| 1980 | + $value = is_array($value) ? array_map('urlencode', $value) : urlencode($value); |
|
| 1981 | + $this->_req_data[$ref] = $value; |
|
| 1982 | + } |
|
| 1983 | + //merge request vars so that the reloaded list table contains any existing filter query params |
|
| 1984 | + $route = array_merge($this->_req_data, $route); |
|
| 1985 | + $this->_redirect_after_action($success, '', '', $route, true); |
|
| 1986 | + } |
|
| 1987 | + |
|
| 1988 | + |
|
| 1989 | + /** |
|
| 1990 | + * incoming reg status change from reg details page. |
|
| 1991 | + * |
|
| 1992 | + * @return void |
|
| 1993 | + */ |
|
| 1994 | + protected function _change_reg_status() |
|
| 1995 | + { |
|
| 1996 | + $this->_req_data['return'] = 'view_registration'; |
|
| 1997 | + //set notify based on whether the send notifications toggle is set or not |
|
| 1998 | + $notify = ! empty($this->_req_data['reg_status_change_form']['send_notifications']); |
|
| 1999 | + //$notify = ! empty( $this->_req_data['txn_reg_status_change']['send_notifications'] ); |
|
| 2000 | + $this->_req_data['reg_status_change_form']['reg_status'] = isset($this->_req_data['reg_status_change_form']['reg_status']) |
|
| 2001 | + ? $this->_req_data['reg_status_change_form']['reg_status'] : ''; |
|
| 2002 | + switch ($this->_req_data['reg_status_change_form']['reg_status']) { |
|
| 2003 | + case EEM_Registration::status_id_approved : |
|
| 2004 | + case EEH_Template::pretty_status(EEM_Registration::status_id_approved, false, 'sentence') : |
|
| 2005 | + $this->approve_registration($notify); |
|
| 2006 | + break; |
|
| 2007 | + case EEM_Registration::status_id_pending_payment : |
|
| 2008 | + case EEH_Template::pretty_status(EEM_Registration::status_id_pending_payment, false, 'sentence') : |
|
| 2009 | + $this->pending_registration($notify); |
|
| 2010 | + break; |
|
| 2011 | + case EEM_Registration::status_id_not_approved : |
|
| 2012 | + case EEH_Template::pretty_status(EEM_Registration::status_id_not_approved, false, 'sentence') : |
|
| 2013 | + $this->not_approve_registration($notify); |
|
| 2014 | + break; |
|
| 2015 | + case EEM_Registration::status_id_declined : |
|
| 2016 | + case EEH_Template::pretty_status(EEM_Registration::status_id_declined, false, 'sentence') : |
|
| 2017 | + $this->decline_registration($notify); |
|
| 2018 | + break; |
|
| 2019 | + case EEM_Registration::status_id_cancelled : |
|
| 2020 | + case EEH_Template::pretty_status(EEM_Registration::status_id_cancelled, false, 'sentence') : |
|
| 2021 | + $this->cancel_registration($notify); |
|
| 2022 | + break; |
|
| 2023 | + case EEM_Registration::status_id_wait_list : |
|
| 2024 | + case EEH_Template::pretty_status(EEM_Registration::status_id_wait_list, false, 'sentence') : |
|
| 2025 | + $this->wait_list_registration($notify); |
|
| 2026 | + break; |
|
| 2027 | + case EEM_Registration::status_id_incomplete : |
|
| 2028 | + default : |
|
| 2029 | + $result['success'] = false; |
|
| 2030 | + unset($this->_req_data['return']); |
|
| 2031 | + $this->_reg_status_change_return('', false); |
|
| 2032 | + break; |
|
| 2033 | + } |
|
| 2034 | + } |
|
| 2035 | + |
|
| 2036 | + |
|
| 2037 | + /** |
|
| 2038 | + * Callback for bulk action routes. |
|
| 2039 | + * Note: although we could just register the singular route callbacks for each bulk action route as well, this |
|
| 2040 | + * method was chosen so there is one central place all the registration status bulk actions are going through. |
|
| 2041 | + * Potentially, this provides an easier place to locate logic that is specific to these bulk actions (as opposed to |
|
| 2042 | + * when an action is happening on just a single registration). |
|
| 2043 | + * @param $action |
|
| 2044 | + * @param bool $notify |
|
| 2045 | + */ |
|
| 2046 | + protected function bulk_action_on_registrations($action, $notify = false) { |
|
| 2047 | + do_action( |
|
| 2048 | + 'AHEE__Registrations_Admin_Page__bulk_action_on_registrations__before_execution', |
|
| 2049 | + $this, |
|
| 2050 | + $action, |
|
| 2051 | + $notify |
|
| 2052 | + ); |
|
| 2053 | + $method = $action . '_registration'; |
|
| 2054 | + if (method_exists($this, $method)) { |
|
| 2055 | + $this->$method($notify); |
|
| 2056 | + } |
|
| 2057 | + } |
|
| 2058 | + |
|
| 2059 | + |
|
| 2060 | + /** |
|
| 2061 | + * approve_registration |
|
| 2062 | + * |
|
| 2063 | + * @access protected |
|
| 2064 | + * @param bool $notify whether or not to notify the registrant about their approval. |
|
| 2065 | + * @return void |
|
| 2066 | + */ |
|
| 2067 | + protected function approve_registration($notify = false) |
|
| 2068 | + { |
|
| 2069 | + $this->_reg_status_change_return(EEM_Registration::status_id_approved, $notify); |
|
| 2070 | + } |
|
| 2071 | + |
|
| 2072 | + |
|
| 2073 | + /** |
|
| 2074 | + * decline_registration |
|
| 2075 | + * |
|
| 2076 | + * @access protected |
|
| 2077 | + * @param bool $notify whether or not to notify the registrant about their status change. |
|
| 2078 | + * @return void |
|
| 2079 | + */ |
|
| 2080 | + protected function decline_registration($notify = false) |
|
| 2081 | + { |
|
| 2082 | + $this->_reg_status_change_return(EEM_Registration::status_id_declined, $notify); |
|
| 2083 | + } |
|
| 2084 | + |
|
| 2085 | + |
|
| 2086 | + /** |
|
| 2087 | + * cancel_registration |
|
| 2088 | + * |
|
| 2089 | + * @access protected |
|
| 2090 | + * @param bool $notify whether or not to notify the registrant about their status change. |
|
| 2091 | + * @return void |
|
| 2092 | + */ |
|
| 2093 | + protected function cancel_registration($notify = false) |
|
| 2094 | + { |
|
| 2095 | + $this->_reg_status_change_return(EEM_Registration::status_id_cancelled, $notify); |
|
| 2096 | + } |
|
| 2097 | + |
|
| 2098 | + |
|
| 2099 | + /** |
|
| 2100 | + * not_approve_registration |
|
| 2101 | + * |
|
| 2102 | + * @access protected |
|
| 2103 | + * @param bool $notify whether or not to notify the registrant about their status change. |
|
| 2104 | + * @return void |
|
| 2105 | + */ |
|
| 2106 | + protected function not_approve_registration($notify = false) |
|
| 2107 | + { |
|
| 2108 | + $this->_reg_status_change_return(EEM_Registration::status_id_not_approved, $notify); |
|
| 2109 | + } |
|
| 2110 | + |
|
| 2111 | + |
|
| 2112 | + /** |
|
| 2113 | + * decline_registration |
|
| 2114 | + * |
|
| 2115 | + * @access protected |
|
| 2116 | + * @param bool $notify whether or not to notify the registrant about their status change. |
|
| 2117 | + * @return void |
|
| 2118 | + */ |
|
| 2119 | + protected function pending_registration($notify = false) |
|
| 2120 | + { |
|
| 2121 | + $this->_reg_status_change_return(EEM_Registration::status_id_pending_payment, $notify); |
|
| 2122 | + } |
|
| 2123 | + |
|
| 2124 | + |
|
| 2125 | + /** |
|
| 2126 | + * waitlist_registration |
|
| 2127 | + * |
|
| 2128 | + * @access protected |
|
| 2129 | + * @param bool $notify whether or not to notify the registrant about their status change. |
|
| 2130 | + * @return void |
|
| 2131 | + */ |
|
| 2132 | + protected function wait_list_registration($notify = false) |
|
| 2133 | + { |
|
| 2134 | + $this->_reg_status_change_return(EEM_Registration::status_id_wait_list, $notify); |
|
| 2135 | + } |
|
| 2136 | + |
|
| 2137 | + |
|
| 2138 | + /** |
|
| 2139 | + * generates HTML for the Registration main meta box |
|
| 2140 | + * |
|
| 2141 | + * @access public |
|
| 2142 | + * @return void |
|
| 2143 | + * @throws DomainException |
|
| 2144 | + * @throws EE_Error |
|
| 2145 | + * @throws InvalidArgumentException |
|
| 2146 | + * @throws InvalidDataTypeException |
|
| 2147 | + * @throws InvalidInterfaceException |
|
| 2148 | + * @throws ReflectionException |
|
| 2149 | + * @throws EntityNotFoundException |
|
| 2150 | + */ |
|
| 2151 | + public function _reg_details_meta_box() |
|
| 2152 | + { |
|
| 2153 | + EEH_Autoloader::register_line_item_display_autoloaders(); |
|
| 2154 | + EEH_Autoloader::register_line_item_filter_autoloaders(); |
|
| 2155 | + EE_Registry::instance()->load_helper('Line_Item'); |
|
| 2156 | + $transaction = $this->_registration->transaction() ? $this->_registration->transaction() |
|
| 2157 | + : EE_Transaction::new_instance(); |
|
| 2158 | + $this->_session = $transaction->session_data(); |
|
| 2159 | + $filters = new EE_Line_Item_Filter_Collection(); |
|
| 2160 | + //$filters->add( new EE_Non_Zero_Line_Item_Filter() ); |
|
| 2161 | + $filters->add(new EE_Single_Registration_Line_Item_Filter($this->_registration)); |
|
| 2162 | + $line_item_filter_processor = new EE_Line_Item_Filter_Processor($filters, |
|
| 2163 | + $transaction->total_line_item()); |
|
| 2164 | + $filtered_line_item_tree = $line_item_filter_processor->process(); |
|
| 2165 | + $line_item_display = new EE_Line_Item_Display('reg_admin_table', |
|
| 2166 | + 'EE_Admin_Table_Registration_Line_Item_Display_Strategy'); |
|
| 2167 | + $this->_template_args['line_item_table'] = $line_item_display->display_line_item( |
|
| 2168 | + $filtered_line_item_tree, |
|
| 2169 | + array('EE_Registration' => $this->_registration) |
|
| 2170 | + ); |
|
| 2171 | + $attendee = $this->_registration->attendee(); |
|
| 2172 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
| 2173 | + 'ee_read_transaction', |
|
| 2174 | + 'espresso_transactions_view_transaction' |
|
| 2175 | + )) { |
|
| 2176 | + $this->_template_args['view_transaction_button'] = EEH_Template::get_button_or_link( |
|
| 2177 | + EE_Admin_Page::add_query_args_and_nonce( |
|
| 2178 | + array( |
|
| 2179 | + 'action' => 'view_transaction', |
|
| 2180 | + 'TXN_ID' => $transaction->ID(), |
|
| 2181 | + ), |
|
| 2182 | + TXN_ADMIN_URL |
|
| 2183 | + ), |
|
| 2184 | + esc_html__(' View Transaction', 'event_espresso'), |
|
| 2185 | + 'button secondary-button right', |
|
| 2186 | + 'dashicons dashicons-cart' |
|
| 2187 | + ); |
|
| 2188 | + } else { |
|
| 2189 | + $this->_template_args['view_transaction_button'] = ''; |
|
| 2190 | + } |
|
| 2191 | + if ($attendee instanceof EE_Attendee |
|
| 2192 | + && EE_Registry::instance()->CAP->current_user_can( |
|
| 2193 | + 'ee_send_message', |
|
| 2194 | + 'espresso_registrations_resend_registration' |
|
| 2195 | + ) |
|
| 2196 | + ) { |
|
| 2197 | + $this->_template_args['resend_registration_button'] = EEH_Template::get_button_or_link( |
|
| 2198 | + EE_Admin_Page::add_query_args_and_nonce( |
|
| 2199 | + array( |
|
| 2200 | + 'action' => 'resend_registration', |
|
| 2201 | + '_REG_ID' => $this->_registration->ID(), |
|
| 2202 | + 'redirect_to' => 'view_registration', |
|
| 2203 | + ), |
|
| 2204 | + REG_ADMIN_URL |
|
| 2205 | + ), |
|
| 2206 | + esc_html__(' Resend Registration', 'event_espresso'), |
|
| 2207 | + 'button secondary-button right', |
|
| 2208 | + 'dashicons dashicons-email-alt' |
|
| 2209 | + ); |
|
| 2210 | + } else { |
|
| 2211 | + $this->_template_args['resend_registration_button'] = ''; |
|
| 2212 | + } |
|
| 2213 | + $this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign; |
|
| 2214 | + $payment = $transaction->get_first_related('Payment'); |
|
| 2215 | + $payment = ! $payment instanceof EE_Payment |
|
| 2216 | + ? EE_Payment::new_instance() |
|
| 2217 | + : $payment; |
|
| 2218 | + $payment_method = $payment->get_first_related('Payment_Method'); |
|
| 2219 | + $payment_method = ! $payment_method instanceof EE_Payment_Method |
|
| 2220 | + ? EE_Payment_Method::new_instance() |
|
| 2221 | + : $payment_method; |
|
| 2222 | + $reg_details = array( |
|
| 2223 | + 'payment_method' => $payment_method->name(), |
|
| 2224 | + 'response_msg' => $payment->gateway_response(), |
|
| 2225 | + 'registration_id' => $this->_registration->get('REG_code'), |
|
| 2226 | + 'registration_session' => $this->_registration->session_ID(), |
|
| 2227 | + 'ip_address' => isset($this->_session['ip_address']) ? $this->_session['ip_address'] : '', |
|
| 2228 | + 'user_agent' => isset($this->_session['user_agent']) ? $this->_session['user_agent'] : '', |
|
| 2229 | + ); |
|
| 2230 | + if (isset($reg_details['registration_id'])) { |
|
| 2231 | + $this->_template_args['reg_details']['registration_id']['value'] = $reg_details['registration_id']; |
|
| 2232 | + $this->_template_args['reg_details']['registration_id']['label'] = esc_html__( |
|
| 2233 | + 'Registration ID', |
|
| 2234 | + 'event_espresso' |
|
| 2235 | + ); |
|
| 2236 | + $this->_template_args['reg_details']['registration_id']['class'] = 'regular-text'; |
|
| 2237 | + } |
|
| 2238 | + if (isset($reg_details['payment_method'])) { |
|
| 2239 | + $this->_template_args['reg_details']['payment_method']['value'] = $reg_details['payment_method']; |
|
| 2240 | + $this->_template_args['reg_details']['payment_method']['label'] = esc_html__( |
|
| 2241 | + 'Most Recent Payment Method', |
|
| 2242 | + 'event_espresso' |
|
| 2243 | + ); |
|
| 2244 | + $this->_template_args['reg_details']['payment_method']['class'] = 'regular-text'; |
|
| 2245 | + $this->_template_args['reg_details']['response_msg']['value'] = $reg_details['response_msg']; |
|
| 2246 | + $this->_template_args['reg_details']['response_msg']['label'] = esc_html__( |
|
| 2247 | + 'Payment method response', |
|
| 2248 | + 'event_espresso' |
|
| 2249 | + ); |
|
| 2250 | + $this->_template_args['reg_details']['response_msg']['class'] = 'regular-text'; |
|
| 2251 | + } |
|
| 2252 | + $this->_template_args['reg_details']['registration_session']['value'] = $reg_details['registration_session']; |
|
| 2253 | + $this->_template_args['reg_details']['registration_session']['label'] = esc_html__( |
|
| 2254 | + 'Registration Session', |
|
| 2255 | + 'event_espresso' |
|
| 2256 | + ); |
|
| 2257 | + $this->_template_args['reg_details']['registration_session']['class'] = 'regular-text'; |
|
| 2258 | + $this->_template_args['reg_details']['ip_address']['value'] = $reg_details['ip_address']; |
|
| 2259 | + $this->_template_args['reg_details']['ip_address']['label'] = esc_html__( |
|
| 2260 | + 'Registration placed from IP', |
|
| 2261 | + 'event_espresso' |
|
| 2262 | + ); |
|
| 2263 | + $this->_template_args['reg_details']['ip_address']['class'] = 'regular-text'; |
|
| 2264 | + $this->_template_args['reg_details']['user_agent']['value'] = $reg_details['user_agent']; |
|
| 2265 | + $this->_template_args['reg_details']['user_agent']['label'] = esc_html__('Registrant User Agent', |
|
| 2266 | + 'event_espresso'); |
|
| 2267 | + $this->_template_args['reg_details']['user_agent']['class'] = 'large-text'; |
|
| 2268 | + $this->_template_args['event_link'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 2269 | + array( |
|
| 2270 | + 'action' => 'default', |
|
| 2271 | + 'event_id' => $this->_registration->event_ID(), |
|
| 2272 | + ), |
|
| 2273 | + REG_ADMIN_URL |
|
| 2274 | + ); |
|
| 2275 | + $this->_template_args['REG_ID'] = $this->_registration->ID(); |
|
| 2276 | + $this->_template_args['event_id'] = $this->_registration->event_ID(); |
|
| 2277 | + $template_path = |
|
| 2278 | + REG_TEMPLATE_PATH . 'reg_admin_details_main_meta_box_reg_details.template.php'; |
|
| 2279 | + echo EEH_Template::display_template($template_path, $this->_template_args, true); |
|
| 2280 | + } |
|
| 2281 | + |
|
| 2282 | + |
|
| 2283 | + /** |
|
| 2284 | + * generates HTML for the Registration Questions meta box. |
|
| 2285 | + * If pre-4.8.32.rc.000 hooks are used, uses old methods (with its filters), |
|
| 2286 | + * otherwise uses new forms system |
|
| 2287 | + * |
|
| 2288 | + * @access public |
|
| 2289 | + * @return void |
|
| 2290 | + * @throws DomainException |
|
| 2291 | + * @throws EE_Error |
|
| 2292 | + */ |
|
| 2293 | + public function _reg_questions_meta_box() |
|
| 2294 | + { |
|
| 2295 | + //allow someone to override this method entirely |
|
| 2296 | + if (apply_filters('FHEE__Registrations_Admin_Page___reg_questions_meta_box__do_default', true, $this, |
|
| 2297 | + $this->_registration)) { |
|
| 2298 | + $form = $this->_get_reg_custom_questions_form( |
|
| 2299 | + $this->_registration->ID() |
|
| 2300 | + ); |
|
| 2301 | + $this->_template_args['att_questions'] = count($form->subforms()) > 0 |
|
| 2302 | + ? $form->get_html_and_js() |
|
| 2303 | + : ''; |
|
| 2304 | + $this->_template_args['reg_questions_form_action'] = 'edit_registration'; |
|
| 2305 | + $this->_template_args['REG_ID'] = $this->_registration->ID(); |
|
| 2306 | + $template_path = |
|
| 2307 | + REG_TEMPLATE_PATH . 'reg_admin_details_main_meta_box_reg_questions.template.php'; |
|
| 2308 | + echo EEH_Template::display_template($template_path, $this->_template_args, true); |
|
| 2309 | + } |
|
| 2310 | + } |
|
| 2311 | + |
|
| 2312 | + |
|
| 2313 | + /** |
|
| 2314 | + * form_before_question_group |
|
| 2315 | + * |
|
| 2316 | + * @deprecated as of 4.8.32.rc.000 |
|
| 2317 | + * @access public |
|
| 2318 | + * @param string $output |
|
| 2319 | + * @return string |
|
| 2320 | + */ |
|
| 2321 | + public function form_before_question_group($output) |
|
| 2322 | + { |
|
| 2323 | + EE_Error::doing_it_wrong( |
|
| 2324 | + __CLASS__ . '::' . __FUNCTION__, |
|
| 2325 | + esc_html__( |
|
| 2326 | + 'This method would have been protected but was used on a filter callback so needed to be public. Please discontinue usage as it will be removed soon.', |
|
| 2327 | + 'event_espresso' |
|
| 2328 | + ), |
|
| 2329 | + '4.8.32.rc.000' |
|
| 2330 | + ); |
|
| 2331 | + return ' |
|
| 2332 | 2332 | <table class="form-table ee-width-100"> |
| 2333 | 2333 | <tbody> |
| 2334 | 2334 | '; |
| 2335 | - } |
|
| 2336 | - |
|
| 2337 | - |
|
| 2338 | - /** |
|
| 2339 | - * form_after_question_group |
|
| 2340 | - * |
|
| 2341 | - * @deprecated as of 4.8.32.rc.000 |
|
| 2342 | - * @access public |
|
| 2343 | - * @param string $output |
|
| 2344 | - * @return string |
|
| 2345 | - */ |
|
| 2346 | - public function form_after_question_group($output) |
|
| 2347 | - { |
|
| 2348 | - EE_Error::doing_it_wrong( |
|
| 2349 | - __CLASS__ . '::' . __FUNCTION__, |
|
| 2350 | - esc_html__( |
|
| 2351 | - 'This method would have been protected but was used on a filter callback so needed to be public. Please discontinue usage as it will be removed soon.', |
|
| 2352 | - 'event_espresso' |
|
| 2353 | - ), |
|
| 2354 | - '4.8.32.rc.000' |
|
| 2355 | - ); |
|
| 2356 | - return ' |
|
| 2335 | + } |
|
| 2336 | + |
|
| 2337 | + |
|
| 2338 | + /** |
|
| 2339 | + * form_after_question_group |
|
| 2340 | + * |
|
| 2341 | + * @deprecated as of 4.8.32.rc.000 |
|
| 2342 | + * @access public |
|
| 2343 | + * @param string $output |
|
| 2344 | + * @return string |
|
| 2345 | + */ |
|
| 2346 | + public function form_after_question_group($output) |
|
| 2347 | + { |
|
| 2348 | + EE_Error::doing_it_wrong( |
|
| 2349 | + __CLASS__ . '::' . __FUNCTION__, |
|
| 2350 | + esc_html__( |
|
| 2351 | + 'This method would have been protected but was used on a filter callback so needed to be public. Please discontinue usage as it will be removed soon.', |
|
| 2352 | + 'event_espresso' |
|
| 2353 | + ), |
|
| 2354 | + '4.8.32.rc.000' |
|
| 2355 | + ); |
|
| 2356 | + return ' |
|
| 2357 | 2357 | <tr class="hide-if-no-js"> |
| 2358 | 2358 | <th> </th> |
| 2359 | 2359 | <td class="reg-admin-edit-attendee-question-td"> |
| 2360 | 2360 | <a class="reg-admin-edit-attendee-question-lnk" href="#" title="' |
| 2361 | - . esc_attr__('click to edit question', 'event_espresso') |
|
| 2362 | - . '"> |
|
| 2361 | + . esc_attr__('click to edit question', 'event_espresso') |
|
| 2362 | + . '"> |
|
| 2363 | 2363 | <span class="reg-admin-edit-question-group-spn lt-grey-txt">' |
| 2364 | - . esc_html__('edit the above question group', 'event_espresso') |
|
| 2365 | - . '</span> |
|
| 2364 | + . esc_html__('edit the above question group', 'event_espresso') |
|
| 2365 | + . '</span> |
|
| 2366 | 2366 | <div class="dashicons dashicons-edit"></div> |
| 2367 | 2367 | </a> |
| 2368 | 2368 | </td> |
@@ -2370,579 +2370,579 @@ discard block |
||
| 2370 | 2370 | </tbody> |
| 2371 | 2371 | </table> |
| 2372 | 2372 | '; |
| 2373 | - } |
|
| 2374 | - |
|
| 2375 | - |
|
| 2376 | - /** |
|
| 2377 | - * form_form_field_label_wrap |
|
| 2378 | - * |
|
| 2379 | - * @deprecated as of 4.8.32.rc.000 |
|
| 2380 | - * @access public |
|
| 2381 | - * @param string $label |
|
| 2382 | - * @return string |
|
| 2383 | - */ |
|
| 2384 | - public function form_form_field_label_wrap($label) |
|
| 2385 | - { |
|
| 2386 | - EE_Error::doing_it_wrong( |
|
| 2387 | - __CLASS__ . '::' . __FUNCTION__, |
|
| 2388 | - esc_html__( |
|
| 2389 | - 'This method would have been protected but was used on a filter callback so needed to be public. Please discontinue usage as it will be removed soon.', |
|
| 2390 | - 'event_espresso' |
|
| 2391 | - ), |
|
| 2392 | - '4.8.32.rc.000' |
|
| 2393 | - ); |
|
| 2394 | - return ' |
|
| 2373 | + } |
|
| 2374 | + |
|
| 2375 | + |
|
| 2376 | + /** |
|
| 2377 | + * form_form_field_label_wrap |
|
| 2378 | + * |
|
| 2379 | + * @deprecated as of 4.8.32.rc.000 |
|
| 2380 | + * @access public |
|
| 2381 | + * @param string $label |
|
| 2382 | + * @return string |
|
| 2383 | + */ |
|
| 2384 | + public function form_form_field_label_wrap($label) |
|
| 2385 | + { |
|
| 2386 | + EE_Error::doing_it_wrong( |
|
| 2387 | + __CLASS__ . '::' . __FUNCTION__, |
|
| 2388 | + esc_html__( |
|
| 2389 | + 'This method would have been protected but was used on a filter callback so needed to be public. Please discontinue usage as it will be removed soon.', |
|
| 2390 | + 'event_espresso' |
|
| 2391 | + ), |
|
| 2392 | + '4.8.32.rc.000' |
|
| 2393 | + ); |
|
| 2394 | + return ' |
|
| 2395 | 2395 | <tr> |
| 2396 | 2396 | <th> |
| 2397 | 2397 | ' . $label . ' |
| 2398 | 2398 | </th>'; |
| 2399 | - } |
|
| 2400 | - |
|
| 2401 | - |
|
| 2402 | - /** |
|
| 2403 | - * form_form_field_input__wrap |
|
| 2404 | - * |
|
| 2405 | - * @deprecated as of 4.8.32.rc.000 |
|
| 2406 | - * @access public |
|
| 2407 | - * @param string $input |
|
| 2408 | - * @return string |
|
| 2409 | - */ |
|
| 2410 | - public function form_form_field_input__wrap($input) |
|
| 2411 | - { |
|
| 2412 | - EE_Error::doing_it_wrong( |
|
| 2413 | - __CLASS__ . '::' . __FUNCTION__, |
|
| 2414 | - esc_html__( |
|
| 2415 | - 'This method would have been protected but was used on a filter callback so needed to be public. Please discontinue usage as it will be removed soon.', |
|
| 2416 | - 'event_espresso' |
|
| 2417 | - ), |
|
| 2418 | - '4.8.32.rc.000' |
|
| 2419 | - ); |
|
| 2420 | - return ' |
|
| 2399 | + } |
|
| 2400 | + |
|
| 2401 | + |
|
| 2402 | + /** |
|
| 2403 | + * form_form_field_input__wrap |
|
| 2404 | + * |
|
| 2405 | + * @deprecated as of 4.8.32.rc.000 |
|
| 2406 | + * @access public |
|
| 2407 | + * @param string $input |
|
| 2408 | + * @return string |
|
| 2409 | + */ |
|
| 2410 | + public function form_form_field_input__wrap($input) |
|
| 2411 | + { |
|
| 2412 | + EE_Error::doing_it_wrong( |
|
| 2413 | + __CLASS__ . '::' . __FUNCTION__, |
|
| 2414 | + esc_html__( |
|
| 2415 | + 'This method would have been protected but was used on a filter callback so needed to be public. Please discontinue usage as it will be removed soon.', |
|
| 2416 | + 'event_espresso' |
|
| 2417 | + ), |
|
| 2418 | + '4.8.32.rc.000' |
|
| 2419 | + ); |
|
| 2420 | + return ' |
|
| 2421 | 2421 | <td class="reg-admin-attendee-questions-input-td disabled-input"> |
| 2422 | 2422 | ' . $input . ' |
| 2423 | 2423 | </td> |
| 2424 | 2424 | </tr>'; |
| 2425 | - } |
|
| 2426 | - |
|
| 2427 | - |
|
| 2428 | - /** |
|
| 2429 | - * Updates the registration's custom questions according to the form info, if the form is submitted. |
|
| 2430 | - * If it's not a post, the "view_registrations" route will be called next on the SAME request |
|
| 2431 | - * to display the page |
|
| 2432 | - * |
|
| 2433 | - * @access protected |
|
| 2434 | - * @return void |
|
| 2435 | - * @throws EE_Error |
|
| 2436 | - */ |
|
| 2437 | - protected function _update_attendee_registration_form() |
|
| 2438 | - { |
|
| 2439 | - do_action('AHEE__Registrations_Admin_Page___update_attendee_registration_form__start', $this); |
|
| 2440 | - if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
|
| 2441 | - $REG_ID = isset($this->_req_data['_REG_ID']) ? absint($this->_req_data['_REG_ID']) : false; |
|
| 2442 | - $success = $this->_save_reg_custom_questions_form($REG_ID); |
|
| 2443 | - if ($success) { |
|
| 2444 | - $what = esc_html__('Registration Form', 'event_espresso'); |
|
| 2445 | - $route = $REG_ID ? array('action' => 'view_registration', '_REG_ID' => $REG_ID) |
|
| 2446 | - : array('action' => 'default'); |
|
| 2447 | - $this->_redirect_after_action($success, $what, esc_html__('updated', 'event_espresso'), $route); |
|
| 2448 | - } |
|
| 2449 | - } |
|
| 2450 | - } |
|
| 2451 | - |
|
| 2452 | - |
|
| 2453 | - /** |
|
| 2454 | - * Gets the form for saving registrations custom questions (if done |
|
| 2455 | - * previously retrieves the cached form object, which may have validation errors in it) |
|
| 2456 | - * |
|
| 2457 | - * @param int $REG_ID |
|
| 2458 | - * @return EE_Registration_Custom_Questions_Form |
|
| 2459 | - * @throws EE_Error |
|
| 2460 | - * @throws InvalidArgumentException |
|
| 2461 | - * @throws InvalidDataTypeException |
|
| 2462 | - * @throws InvalidInterfaceException |
|
| 2463 | - */ |
|
| 2464 | - protected function _get_reg_custom_questions_form($REG_ID) |
|
| 2465 | - { |
|
| 2466 | - if ( ! $this->_reg_custom_questions_form) { |
|
| 2467 | - require_once(REG_ADMIN . 'form_sections' . DS . 'EE_Registration_Custom_Questions_Form.form.php'); |
|
| 2468 | - $this->_reg_custom_questions_form = new EE_Registration_Custom_Questions_Form( |
|
| 2469 | - EEM_Registration::instance()->get_one_by_ID($REG_ID) |
|
| 2470 | - ); |
|
| 2471 | - $this->_reg_custom_questions_form->_construct_finalize(null, null); |
|
| 2472 | - } |
|
| 2473 | - return $this->_reg_custom_questions_form; |
|
| 2474 | - } |
|
| 2475 | - |
|
| 2476 | - |
|
| 2477 | - /** |
|
| 2478 | - * Saves |
|
| 2479 | - * |
|
| 2480 | - * @access private |
|
| 2481 | - * @param bool $REG_ID |
|
| 2482 | - * @return bool |
|
| 2483 | - * @throws EE_Error |
|
| 2484 | - * @throws InvalidArgumentException |
|
| 2485 | - * @throws InvalidDataTypeException |
|
| 2486 | - * @throws InvalidInterfaceException |
|
| 2487 | - */ |
|
| 2488 | - private function _save_reg_custom_questions_form($REG_ID = false) |
|
| 2489 | - { |
|
| 2490 | - if ( ! $REG_ID) { |
|
| 2491 | - EE_Error::add_error( |
|
| 2492 | - esc_html__( |
|
| 2493 | - 'An error occurred. No registration ID was received.', 'event_espresso'), |
|
| 2494 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 2495 | - ); |
|
| 2496 | - } |
|
| 2497 | - $form = $this->_get_reg_custom_questions_form($REG_ID); |
|
| 2498 | - $form->receive_form_submission($this->_req_data); |
|
| 2499 | - $success = false; |
|
| 2500 | - if ($form->is_valid()) { |
|
| 2501 | - foreach ($form->subforms() as $question_group_id => $question_group_form) { |
|
| 2502 | - foreach ($question_group_form->inputs() as $question_id => $input) { |
|
| 2503 | - $where_conditions = array( |
|
| 2504 | - 'QST_ID' => $question_id, |
|
| 2505 | - 'REG_ID' => $REG_ID, |
|
| 2506 | - ); |
|
| 2507 | - $possibly_new_values = array( |
|
| 2508 | - 'ANS_value' => $input->normalized_value(), |
|
| 2509 | - ); |
|
| 2510 | - $answer = EEM_Answer::instance()->get_one(array($where_conditions)); |
|
| 2511 | - if ($answer instanceof EE_Answer) { |
|
| 2512 | - $success = $answer->save($possibly_new_values); |
|
| 2513 | - } else { |
|
| 2514 | - //insert it then |
|
| 2515 | - $cols_n_vals = array_merge($where_conditions, $possibly_new_values); |
|
| 2516 | - $answer = EE_Answer::new_instance($cols_n_vals); |
|
| 2517 | - $success = $answer->save(); |
|
| 2518 | - } |
|
| 2519 | - } |
|
| 2520 | - } |
|
| 2521 | - } else { |
|
| 2522 | - EE_Error::add_error($form->get_validation_error_string(), __FILE__, __FUNCTION__, __LINE__); |
|
| 2523 | - } |
|
| 2524 | - return $success; |
|
| 2525 | - } |
|
| 2526 | - |
|
| 2527 | - |
|
| 2528 | - /** |
|
| 2529 | - * generates HTML for the Registration main meta box |
|
| 2530 | - * |
|
| 2531 | - * @access public |
|
| 2532 | - * @return void |
|
| 2533 | - * @throws DomainException |
|
| 2534 | - * @throws EE_Error |
|
| 2535 | - * @throws InvalidArgumentException |
|
| 2536 | - * @throws InvalidDataTypeException |
|
| 2537 | - * @throws InvalidInterfaceException |
|
| 2538 | - */ |
|
| 2539 | - public function _reg_attendees_meta_box() |
|
| 2540 | - { |
|
| 2541 | - $REG = EEM_Registration::instance(); |
|
| 2542 | - //get all other registrations on this transaction, and cache |
|
| 2543 | - //the attendees for them so we don't have to run another query using force_join |
|
| 2544 | - $registrations = $REG->get_all(array( |
|
| 2545 | - array( |
|
| 2546 | - 'TXN_ID' => $this->_registration->transaction_ID(), |
|
| 2547 | - 'REG_ID' => array('!=', $this->_registration->ID()), |
|
| 2548 | - ), |
|
| 2549 | - 'force_join' => array('Attendee'), |
|
| 2550 | - )); |
|
| 2551 | - $this->_template_args['attendees'] = array(); |
|
| 2552 | - $this->_template_args['attendee_notice'] = ''; |
|
| 2553 | - if (empty($registrations) |
|
| 2554 | - || (is_array($registrations) |
|
| 2555 | - && ! EEH_Array::get_one_item_from_array($registrations)) |
|
| 2556 | - ) { |
|
| 2557 | - EE_Error::add_error( |
|
| 2558 | - esc_html__( |
|
| 2559 | - 'There are no records attached to this registration. Something may have gone wrong with the registration', |
|
| 2560 | - 'event_espresso' |
|
| 2561 | - ), __FILE__, __FUNCTION__, __LINE__ |
|
| 2562 | - ); |
|
| 2563 | - $this->_template_args['attendee_notice'] = EE_Error::get_notices(); |
|
| 2564 | - } else { |
|
| 2565 | - $att_nmbr = 1; |
|
| 2566 | - foreach ($registrations as $registration) { |
|
| 2567 | - /* @var $registration EE_Registration */ |
|
| 2568 | - $attendee = $registration->attendee() |
|
| 2569 | - ? $registration->attendee() |
|
| 2570 | - : EEM_Attendee::instance() |
|
| 2571 | - ->create_default_object(); |
|
| 2572 | - $this->_template_args['attendees'][$att_nmbr]['STS_ID'] = $registration->status_ID(); |
|
| 2573 | - $this->_template_args['attendees'][$att_nmbr]['fname'] = $attendee->fname(); |
|
| 2574 | - $this->_template_args['attendees'][$att_nmbr]['lname'] = $attendee->lname(); |
|
| 2575 | - $this->_template_args['attendees'][$att_nmbr]['email'] = $attendee->email(); |
|
| 2576 | - $this->_template_args['attendees'][$att_nmbr]['final_price'] = $registration->final_price(); |
|
| 2577 | - $this->_template_args['attendees'][$att_nmbr]['address'] = implode( |
|
| 2578 | - ', ', |
|
| 2579 | - $attendee->full_address_as_array() |
|
| 2580 | - ); |
|
| 2581 | - $this->_template_args['attendees'][$att_nmbr]['att_link'] = self::add_query_args_and_nonce( |
|
| 2582 | - array( |
|
| 2583 | - 'action' => 'edit_attendee', |
|
| 2584 | - 'post' => $attendee->ID(), |
|
| 2585 | - ), |
|
| 2586 | - REG_ADMIN_URL |
|
| 2587 | - ); |
|
| 2588 | - $this->_template_args['attendees'][$att_nmbr]['event_name'] = $registration->event_obj()->name(); |
|
| 2589 | - $att_nmbr++; |
|
| 2590 | - } |
|
| 2591 | - $this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign; |
|
| 2592 | - } |
|
| 2593 | - $template_path = REG_TEMPLATE_PATH . 'reg_admin_details_main_meta_box_attendees.template.php'; |
|
| 2594 | - echo EEH_Template::display_template($template_path, $this->_template_args, true); |
|
| 2595 | - } |
|
| 2596 | - |
|
| 2597 | - |
|
| 2598 | - /** |
|
| 2599 | - * generates HTML for the Edit Registration side meta box |
|
| 2600 | - * |
|
| 2601 | - * @access public |
|
| 2602 | - * @return void |
|
| 2603 | - * @throws DomainException |
|
| 2604 | - * @throws EE_Error |
|
| 2605 | - * @throws InvalidArgumentException |
|
| 2606 | - * @throws InvalidDataTypeException |
|
| 2607 | - * @throws InvalidInterfaceException |
|
| 2608 | - */ |
|
| 2609 | - public function _reg_registrant_side_meta_box() |
|
| 2610 | - { |
|
| 2611 | - /*@var $attendee EE_Attendee */ |
|
| 2612 | - $att_check = $this->_registration->attendee(); |
|
| 2613 | - $attendee = $att_check instanceof EE_Attendee ? $att_check : EEM_Attendee::instance()->create_default_object(); |
|
| 2614 | - //now let's determine if this is not the primary registration. If it isn't then we set the |
|
| 2615 | - //primary_registration object for reference BUT ONLY if the Attendee object loaded is not the same as the |
|
| 2616 | - //primary registration object (that way we know if we need to show create button or not) |
|
| 2617 | - if ( ! $this->_registration->is_primary_registrant()) { |
|
| 2618 | - $primary_registration = $this->_registration->get_primary_registration(); |
|
| 2619 | - $primary_attendee = $primary_registration instanceof EE_Registration ? $primary_registration->attendee() |
|
| 2620 | - : null; |
|
| 2621 | - if ( ! $primary_attendee instanceof EE_Attendee || $attendee->ID() !== $primary_attendee->ID()) { |
|
| 2622 | - //in here? This means the displayed registration is not the primary registrant but ALREADY HAS its own |
|
| 2623 | - //custom attendee object so let's not worry about the primary reg. |
|
| 2624 | - $primary_registration = null; |
|
| 2625 | - } |
|
| 2626 | - } else { |
|
| 2627 | - $primary_registration = null; |
|
| 2628 | - } |
|
| 2629 | - $this->_template_args['ATT_ID'] = $attendee->ID(); |
|
| 2630 | - $this->_template_args['fname'] = $attendee->fname(); |
|
| 2631 | - $this->_template_args['lname'] = $attendee->lname(); |
|
| 2632 | - $this->_template_args['email'] = $attendee->email(); |
|
| 2633 | - $this->_template_args['phone'] = $attendee->phone(); |
|
| 2634 | - $this->_template_args['formatted_address'] = EEH_Address::format($attendee); |
|
| 2635 | - //edit link |
|
| 2636 | - $this->_template_args['att_edit_link'] = EE_Admin_Page::add_query_args_and_nonce(array( |
|
| 2637 | - 'action' => 'edit_attendee', |
|
| 2638 | - 'post' => $attendee->ID(), |
|
| 2639 | - ), REG_ADMIN_URL); |
|
| 2640 | - $this->_template_args['att_edit_label'] = esc_html__('View/Edit Contact', 'event_espresso'); |
|
| 2641 | - //create link |
|
| 2642 | - $this->_template_args['create_link'] = $primary_registration instanceof EE_Registration |
|
| 2643 | - ? EE_Admin_Page::add_query_args_and_nonce(array( |
|
| 2644 | - 'action' => 'duplicate_attendee', |
|
| 2645 | - '_REG_ID' => $this->_registration->ID(), |
|
| 2646 | - ), REG_ADMIN_URL) : ''; |
|
| 2647 | - $this->_template_args['create_label'] = esc_html__('Create Contact', 'event_espresso'); |
|
| 2648 | - $this->_template_args['att_check'] = $att_check; |
|
| 2649 | - $template_path = REG_TEMPLATE_PATH . 'reg_admin_details_side_meta_box_registrant.template.php'; |
|
| 2650 | - echo EEH_Template::display_template($template_path, $this->_template_args, true); |
|
| 2651 | - } |
|
| 2652 | - |
|
| 2653 | - |
|
| 2654 | - /** |
|
| 2655 | - * trash or restore registrations |
|
| 2656 | - * |
|
| 2657 | - * @param boolean $trash whether to archive or restore |
|
| 2658 | - * @return void |
|
| 2659 | - * @throws EE_Error |
|
| 2660 | - * @throws InvalidArgumentException |
|
| 2661 | - * @throws InvalidDataTypeException |
|
| 2662 | - * @throws InvalidInterfaceException |
|
| 2663 | - * @throws RuntimeException |
|
| 2664 | - * @access protected |
|
| 2665 | - */ |
|
| 2666 | - protected function _trash_or_restore_registrations($trash = true) |
|
| 2667 | - { |
|
| 2668 | - //if empty _REG_ID then get out because there's nothing to do |
|
| 2669 | - if (empty($this->_req_data['_REG_ID'])) { |
|
| 2670 | - EE_Error::add_error( |
|
| 2671 | - sprintf( |
|
| 2672 | - esc_html__( |
|
| 2673 | - 'In order to %1$s registrations you must select which ones you wish to %1$s by clicking the checkboxes.', |
|
| 2674 | - 'event_espresso' |
|
| 2675 | - ), |
|
| 2676 | - $trash ? 'trash' : 'restore' |
|
| 2677 | - ), |
|
| 2678 | - __FILE__, __LINE__, __FUNCTION__ |
|
| 2679 | - ); |
|
| 2680 | - $this->_redirect_after_action(false, '', '', array(), true); |
|
| 2681 | - } |
|
| 2682 | - $success = 0; |
|
| 2683 | - $overwrite_msgs = false; |
|
| 2684 | - //Checkboxes |
|
| 2685 | - if ( ! is_array($this->_req_data['_REG_ID'])) { |
|
| 2686 | - $this->_req_data['_REG_ID'] = array($this->_req_data['_REG_ID']); |
|
| 2687 | - } |
|
| 2688 | - $reg_count = count($this->_req_data['_REG_ID']); |
|
| 2689 | - // cycle thru checkboxes |
|
| 2690 | - foreach ($this->_req_data['_REG_ID'] as $REG_ID) { |
|
| 2691 | - /** @var EE_Registration $REG */ |
|
| 2692 | - $REG = EEM_Registration::instance()->get_one_by_ID($REG_ID); |
|
| 2693 | - $payments = $REG->registration_payments(); |
|
| 2694 | - if (! empty($payments)) { |
|
| 2695 | - $name = $REG->attendee() instanceof EE_Attendee |
|
| 2696 | - ? $REG->attendee()->full_name() |
|
| 2697 | - : esc_html__('Unknown Attendee', 'event_espresso'); |
|
| 2698 | - $overwrite_msgs = true; |
|
| 2699 | - EE_Error::add_error( |
|
| 2700 | - sprintf( |
|
| 2701 | - esc_html__( |
|
| 2702 | - 'The registration for %s could not be trashed because it has payments attached to the related transaction. If you wish to trash this registration you must first delete the payments on the related transaction.', |
|
| 2703 | - 'event_espresso' |
|
| 2704 | - ), |
|
| 2705 | - $name |
|
| 2706 | - ), |
|
| 2707 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 2708 | - ); |
|
| 2709 | - //can't trash this registration because it has payments. |
|
| 2710 | - continue; |
|
| 2711 | - } |
|
| 2712 | - $updated = $trash ? $REG->delete() : $REG->restore(); |
|
| 2713 | - if ($updated) { |
|
| 2714 | - $success++; |
|
| 2715 | - } |
|
| 2716 | - } |
|
| 2717 | - $this->_redirect_after_action( |
|
| 2718 | - $success === $reg_count, // were ALL registrations affected? |
|
| 2719 | - $success > 1 |
|
| 2720 | - ? esc_html__('Registrations', 'event_espresso') |
|
| 2721 | - : esc_html__('Registration', 'event_espresso'), |
|
| 2722 | - $trash |
|
| 2723 | - ? esc_html__('moved to the trash', 'event_espresso') |
|
| 2724 | - : esc_html__('restored', 'event_espresso'), |
|
| 2725 | - array('action' => 'default'), |
|
| 2726 | - $overwrite_msgs |
|
| 2727 | - ); |
|
| 2728 | - } |
|
| 2729 | - |
|
| 2730 | - |
|
| 2731 | - /** |
|
| 2732 | - * This is used to permanently delete registrations. Note, this will handle not only deleting permanently the |
|
| 2733 | - * registration but also. |
|
| 2734 | - * 1. Removing relations to EE_Attendee |
|
| 2735 | - * 2. Deleting permanently the related transaction, but ONLY if all related registrations to the transaction are |
|
| 2736 | - * ALSO trashed. |
|
| 2737 | - * 3. Deleting permanently any related Line items but only if the above conditions are met. |
|
| 2738 | - * 4. Removing relationships between all tickets and the related registrations |
|
| 2739 | - * 5. Deleting permanently any related Answers (and the answers for other related registrations that were deleted.) |
|
| 2740 | - * 6. Deleting permanently any related Checkins. |
|
| 2741 | - * |
|
| 2742 | - * @return void |
|
| 2743 | - * @throws EE_Error |
|
| 2744 | - * @throws InvalidArgumentException |
|
| 2745 | - * @throws InvalidDataTypeException |
|
| 2746 | - * @throws InvalidInterfaceException |
|
| 2747 | - */ |
|
| 2748 | - protected function _delete_registrations() |
|
| 2749 | - { |
|
| 2750 | - $REG_MDL = EEM_Registration::instance(); |
|
| 2751 | - $success = 1; |
|
| 2752 | - //Checkboxes |
|
| 2753 | - if ( ! empty($this->_req_data['_REG_ID']) && is_array($this->_req_data['_REG_ID'])) { |
|
| 2754 | - // if array has more than one element than success message should be plural |
|
| 2755 | - $success = count($this->_req_data['_REG_ID']) > 1 ? 2 : 1; |
|
| 2756 | - // cycle thru checkboxes |
|
| 2757 | - while (list($ind, $REG_ID) = each($this->_req_data['_REG_ID'])) { |
|
| 2758 | - $REG = $REG_MDL->get_one_by_ID($REG_ID); |
|
| 2759 | - if ( ! $REG instanceof EE_Registration) { |
|
| 2760 | - continue; |
|
| 2761 | - } |
|
| 2762 | - $deleted = $this->_delete_registration($REG); |
|
| 2763 | - if ( ! $deleted) { |
|
| 2764 | - $success = 0; |
|
| 2765 | - } |
|
| 2766 | - } |
|
| 2767 | - } else { |
|
| 2768 | - // grab single id and delete |
|
| 2769 | - $REG_ID = $this->_req_data['_REG_ID']; |
|
| 2770 | - $REG = $REG_MDL->get_one_by_ID($REG_ID); |
|
| 2771 | - $deleted = $this->_delete_registration($REG); |
|
| 2772 | - if ( ! $deleted) { |
|
| 2773 | - $success = 0; |
|
| 2774 | - } |
|
| 2775 | - } |
|
| 2776 | - $what = $success > 1 |
|
| 2777 | - ? esc_html__('Registrations', 'event_espresso') |
|
| 2778 | - : esc_html__('Registration', 'event_espresso'); |
|
| 2779 | - $action_desc = esc_html__('permanently deleted.', 'event_espresso'); |
|
| 2780 | - $this->_redirect_after_action( |
|
| 2781 | - $success, |
|
| 2782 | - $what, |
|
| 2783 | - $action_desc, |
|
| 2784 | - array('action' => 'default'), |
|
| 2785 | - true |
|
| 2786 | - ); |
|
| 2787 | - } |
|
| 2788 | - |
|
| 2789 | - |
|
| 2790 | - /** |
|
| 2791 | - * handles the permanent deletion of a registration. See comments with _delete_registrations() for details on what |
|
| 2792 | - * models get affected. |
|
| 2793 | - * |
|
| 2794 | - * @param EE_Registration $REG registration to be deleted permenantly |
|
| 2795 | - * @return bool true = successful deletion, false = fail. |
|
| 2796 | - * @throws EE_Error |
|
| 2797 | - */ |
|
| 2798 | - protected function _delete_registration(EE_Registration $REG) |
|
| 2799 | - { |
|
| 2800 | - //first we start with the transaction... ultimately, we WILL not delete permanently if there are any related |
|
| 2801 | - //registrations on the transaction that are NOT trashed. |
|
| 2802 | - $TXN = $REG->get_first_related('Transaction'); |
|
| 2803 | - $REGS = $TXN->get_many_related('Registration'); |
|
| 2804 | - $all_trashed = true; |
|
| 2805 | - foreach ($REGS as $registration) { |
|
| 2806 | - if ( ! $registration->get('REG_deleted')) { |
|
| 2807 | - $all_trashed = false; |
|
| 2808 | - } |
|
| 2809 | - } |
|
| 2810 | - if ( ! $all_trashed) { |
|
| 2811 | - EE_Error::add_error( |
|
| 2812 | - esc_html__( |
|
| 2813 | - 'Unable to permanently delete this registration. Before this registration can be permanently deleted, all registrations made in the same transaction must be trashed as well. These registrations will be permanently deleted in the same action.', |
|
| 2814 | - 'event_espresso' |
|
| 2815 | - ), |
|
| 2816 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 2817 | - ); |
|
| 2818 | - return false; |
|
| 2819 | - } |
|
| 2820 | - //k made it here so that means we can delete all the related transactions and their answers (but let's do them |
|
| 2821 | - //separately from THIS one). |
|
| 2822 | - foreach ($REGS as $registration) { |
|
| 2823 | - //delete related answers |
|
| 2824 | - $registration->delete_related_permanently('Answer'); |
|
| 2825 | - //remove relationship to EE_Attendee (but we ALWAYS leave the contact record intact) |
|
| 2826 | - $attendee = $registration->get_first_related('Attendee'); |
|
| 2827 | - if ($attendee instanceof EE_Attendee) { |
|
| 2828 | - $registration->_remove_relation_to($attendee, 'Attendee'); |
|
| 2829 | - } |
|
| 2830 | - //now remove relationships to tickets on this registration. |
|
| 2831 | - $registration->_remove_relations('Ticket'); |
|
| 2832 | - //now delete permanently the checkins related to this registration. |
|
| 2833 | - $registration->delete_related_permanently('Checkin'); |
|
| 2834 | - if ($registration->ID() === $REG->ID()) { |
|
| 2835 | - continue; |
|
| 2836 | - } //we don't want to delete permanently the existing registration just yet. |
|
| 2837 | - //remove relation to transaction for these registrations if NOT the existing registrations |
|
| 2838 | - $registration->_remove_relations('Transaction'); |
|
| 2839 | - //delete permanently any related messages. |
|
| 2840 | - $registration->delete_related_permanently('Message'); |
|
| 2841 | - //now delete this registration permanently |
|
| 2842 | - $registration->delete_permanently(); |
|
| 2843 | - } |
|
| 2844 | - //now all related registrations on the transaction are handled. So let's just handle this registration itself |
|
| 2845 | - // (the transaction and line items should be all that's left). |
|
| 2846 | - // delete the line items related to the transaction for this registration. |
|
| 2847 | - $TXN->delete_related_permanently('Line_Item'); |
|
| 2848 | - //we need to remove all the relationships on the transaction |
|
| 2849 | - $TXN->delete_related_permanently('Payment'); |
|
| 2850 | - $TXN->delete_related_permanently('Extra_Meta'); |
|
| 2851 | - $TXN->delete_related_permanently('Message'); |
|
| 2852 | - //now we can delete this REG permanently (and the transaction of course) |
|
| 2853 | - $REG->delete_related_permanently('Transaction'); |
|
| 2854 | - return $REG->delete_permanently(); |
|
| 2855 | - } |
|
| 2856 | - |
|
| 2857 | - |
|
| 2858 | - /** |
|
| 2859 | - * generates HTML for the Register New Attendee Admin page |
|
| 2860 | - * |
|
| 2861 | - * @access private |
|
| 2862 | - * @throws DomainException |
|
| 2863 | - * @throws EE_Error |
|
| 2864 | - */ |
|
| 2865 | - public function new_registration() |
|
| 2866 | - { |
|
| 2867 | - if ( ! $this->_set_reg_event()) { |
|
| 2868 | - throw new EE_Error( |
|
| 2869 | - esc_html__( |
|
| 2870 | - 'Unable to continue with registering because there is no Event ID in the request', |
|
| 2871 | - 'event_espresso' |
|
| 2872 | - ) |
|
| 2873 | - ); |
|
| 2874 | - } |
|
| 2875 | - EE_Registry::instance()->REQ->set_espresso_page(true); |
|
| 2876 | - // gotta start with a clean slate if we're not coming here via ajax |
|
| 2877 | - if ( ! defined('DOING_AJAX') |
|
| 2878 | - && ( ! isset($this->_req_data['processing_registration']) || isset($this->_req_data['step_error'])) |
|
| 2879 | - ) { |
|
| 2880 | - EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
| 2881 | - } |
|
| 2882 | - $this->_template_args['event_name'] = ''; |
|
| 2883 | - // event name |
|
| 2884 | - if ($this->_reg_event) { |
|
| 2885 | - $this->_template_args['event_name'] = $this->_reg_event->name(); |
|
| 2886 | - $edit_event_url = self::add_query_args_and_nonce(array( |
|
| 2887 | - 'action' => 'edit', |
|
| 2888 | - 'post' => $this->_reg_event->ID(), |
|
| 2889 | - ), EVENTS_ADMIN_URL); |
|
| 2890 | - $edit_event_lnk = '<a href="' |
|
| 2891 | - . $edit_event_url |
|
| 2892 | - . '" title="' |
|
| 2893 | - . esc_attr__('Edit ', 'event_espresso') |
|
| 2894 | - . $this->_reg_event->name() |
|
| 2895 | - . '">' |
|
| 2896 | - . esc_html__('Edit Event', 'event_espresso') |
|
| 2897 | - . '</a>'; |
|
| 2898 | - $this->_template_args['event_name'] .= ' <span class="admin-page-header-edit-lnk not-bold">' |
|
| 2899 | - . $edit_event_lnk |
|
| 2900 | - . '</span>'; |
|
| 2901 | - } |
|
| 2902 | - $this->_template_args['step_content'] = $this->_get_registration_step_content(); |
|
| 2903 | - if (defined('DOING_AJAX')) { |
|
| 2904 | - $this->_return_json(); |
|
| 2905 | - } |
|
| 2906 | - // grab header |
|
| 2907 | - $template_path = |
|
| 2908 | - REG_TEMPLATE_PATH . 'reg_admin_register_new_attendee.template.php'; |
|
| 2909 | - $this->_template_args['admin_page_content'] = EEH_Template::display_template($template_path, |
|
| 2910 | - $this->_template_args, true); |
|
| 2911 | - //$this->_set_publish_post_box_vars( NULL, FALSE, FALSE, NULL, FALSE ); |
|
| 2912 | - // the details template wrapper |
|
| 2913 | - $this->display_admin_page_with_sidebar(); |
|
| 2914 | - } |
|
| 2915 | - |
|
| 2916 | - |
|
| 2917 | - /** |
|
| 2918 | - * This returns the content for a registration step |
|
| 2919 | - * |
|
| 2920 | - * @access protected |
|
| 2921 | - * @return string html |
|
| 2922 | - * @throws DomainException |
|
| 2923 | - * @throws EE_Error |
|
| 2924 | - * @throws InvalidArgumentException |
|
| 2925 | - * @throws InvalidDataTypeException |
|
| 2926 | - * @throws InvalidInterfaceException |
|
| 2927 | - */ |
|
| 2928 | - protected function _get_registration_step_content() |
|
| 2929 | - { |
|
| 2930 | - if (isset($_COOKIE['ee_registration_added']) && $_COOKIE['ee_registration_added']) { |
|
| 2931 | - $warning_msg = sprintf( |
|
| 2932 | - esc_html__( |
|
| 2933 | - '%2$sWARNING!!!%3$s%1$sPlease do not use the back button to return to this page for the purpose of adding another registration.%1$sThis can result in lost and/or corrupted data.%1$sIf you wish to add another registration, then please click the%1$s%7$s"Add Another New Registration to Event"%8$s button%1$son the Transaction details page, after you are redirected.%1$s%1$s%4$s redirecting in %5$s seconds %6$s', |
|
| 2934 | - 'event_espresso' |
|
| 2935 | - ), |
|
| 2936 | - '<br />', |
|
| 2937 | - '<h3 class="important-notice">', |
|
| 2938 | - '</h3>', |
|
| 2939 | - '<div class="float-right">', |
|
| 2940 | - '<span id="redirect_timer" class="important-notice">30</span>', |
|
| 2941 | - '</div>', |
|
| 2942 | - '<b>', |
|
| 2943 | - '</b>' |
|
| 2944 | - ); |
|
| 2945 | - return ' |
|
| 2425 | + } |
|
| 2426 | + |
|
| 2427 | + |
|
| 2428 | + /** |
|
| 2429 | + * Updates the registration's custom questions according to the form info, if the form is submitted. |
|
| 2430 | + * If it's not a post, the "view_registrations" route will be called next on the SAME request |
|
| 2431 | + * to display the page |
|
| 2432 | + * |
|
| 2433 | + * @access protected |
|
| 2434 | + * @return void |
|
| 2435 | + * @throws EE_Error |
|
| 2436 | + */ |
|
| 2437 | + protected function _update_attendee_registration_form() |
|
| 2438 | + { |
|
| 2439 | + do_action('AHEE__Registrations_Admin_Page___update_attendee_registration_form__start', $this); |
|
| 2440 | + if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
|
| 2441 | + $REG_ID = isset($this->_req_data['_REG_ID']) ? absint($this->_req_data['_REG_ID']) : false; |
|
| 2442 | + $success = $this->_save_reg_custom_questions_form($REG_ID); |
|
| 2443 | + if ($success) { |
|
| 2444 | + $what = esc_html__('Registration Form', 'event_espresso'); |
|
| 2445 | + $route = $REG_ID ? array('action' => 'view_registration', '_REG_ID' => $REG_ID) |
|
| 2446 | + : array('action' => 'default'); |
|
| 2447 | + $this->_redirect_after_action($success, $what, esc_html__('updated', 'event_espresso'), $route); |
|
| 2448 | + } |
|
| 2449 | + } |
|
| 2450 | + } |
|
| 2451 | + |
|
| 2452 | + |
|
| 2453 | + /** |
|
| 2454 | + * Gets the form for saving registrations custom questions (if done |
|
| 2455 | + * previously retrieves the cached form object, which may have validation errors in it) |
|
| 2456 | + * |
|
| 2457 | + * @param int $REG_ID |
|
| 2458 | + * @return EE_Registration_Custom_Questions_Form |
|
| 2459 | + * @throws EE_Error |
|
| 2460 | + * @throws InvalidArgumentException |
|
| 2461 | + * @throws InvalidDataTypeException |
|
| 2462 | + * @throws InvalidInterfaceException |
|
| 2463 | + */ |
|
| 2464 | + protected function _get_reg_custom_questions_form($REG_ID) |
|
| 2465 | + { |
|
| 2466 | + if ( ! $this->_reg_custom_questions_form) { |
|
| 2467 | + require_once(REG_ADMIN . 'form_sections' . DS . 'EE_Registration_Custom_Questions_Form.form.php'); |
|
| 2468 | + $this->_reg_custom_questions_form = new EE_Registration_Custom_Questions_Form( |
|
| 2469 | + EEM_Registration::instance()->get_one_by_ID($REG_ID) |
|
| 2470 | + ); |
|
| 2471 | + $this->_reg_custom_questions_form->_construct_finalize(null, null); |
|
| 2472 | + } |
|
| 2473 | + return $this->_reg_custom_questions_form; |
|
| 2474 | + } |
|
| 2475 | + |
|
| 2476 | + |
|
| 2477 | + /** |
|
| 2478 | + * Saves |
|
| 2479 | + * |
|
| 2480 | + * @access private |
|
| 2481 | + * @param bool $REG_ID |
|
| 2482 | + * @return bool |
|
| 2483 | + * @throws EE_Error |
|
| 2484 | + * @throws InvalidArgumentException |
|
| 2485 | + * @throws InvalidDataTypeException |
|
| 2486 | + * @throws InvalidInterfaceException |
|
| 2487 | + */ |
|
| 2488 | + private function _save_reg_custom_questions_form($REG_ID = false) |
|
| 2489 | + { |
|
| 2490 | + if ( ! $REG_ID) { |
|
| 2491 | + EE_Error::add_error( |
|
| 2492 | + esc_html__( |
|
| 2493 | + 'An error occurred. No registration ID was received.', 'event_espresso'), |
|
| 2494 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 2495 | + ); |
|
| 2496 | + } |
|
| 2497 | + $form = $this->_get_reg_custom_questions_form($REG_ID); |
|
| 2498 | + $form->receive_form_submission($this->_req_data); |
|
| 2499 | + $success = false; |
|
| 2500 | + if ($form->is_valid()) { |
|
| 2501 | + foreach ($form->subforms() as $question_group_id => $question_group_form) { |
|
| 2502 | + foreach ($question_group_form->inputs() as $question_id => $input) { |
|
| 2503 | + $where_conditions = array( |
|
| 2504 | + 'QST_ID' => $question_id, |
|
| 2505 | + 'REG_ID' => $REG_ID, |
|
| 2506 | + ); |
|
| 2507 | + $possibly_new_values = array( |
|
| 2508 | + 'ANS_value' => $input->normalized_value(), |
|
| 2509 | + ); |
|
| 2510 | + $answer = EEM_Answer::instance()->get_one(array($where_conditions)); |
|
| 2511 | + if ($answer instanceof EE_Answer) { |
|
| 2512 | + $success = $answer->save($possibly_new_values); |
|
| 2513 | + } else { |
|
| 2514 | + //insert it then |
|
| 2515 | + $cols_n_vals = array_merge($where_conditions, $possibly_new_values); |
|
| 2516 | + $answer = EE_Answer::new_instance($cols_n_vals); |
|
| 2517 | + $success = $answer->save(); |
|
| 2518 | + } |
|
| 2519 | + } |
|
| 2520 | + } |
|
| 2521 | + } else { |
|
| 2522 | + EE_Error::add_error($form->get_validation_error_string(), __FILE__, __FUNCTION__, __LINE__); |
|
| 2523 | + } |
|
| 2524 | + return $success; |
|
| 2525 | + } |
|
| 2526 | + |
|
| 2527 | + |
|
| 2528 | + /** |
|
| 2529 | + * generates HTML for the Registration main meta box |
|
| 2530 | + * |
|
| 2531 | + * @access public |
|
| 2532 | + * @return void |
|
| 2533 | + * @throws DomainException |
|
| 2534 | + * @throws EE_Error |
|
| 2535 | + * @throws InvalidArgumentException |
|
| 2536 | + * @throws InvalidDataTypeException |
|
| 2537 | + * @throws InvalidInterfaceException |
|
| 2538 | + */ |
|
| 2539 | + public function _reg_attendees_meta_box() |
|
| 2540 | + { |
|
| 2541 | + $REG = EEM_Registration::instance(); |
|
| 2542 | + //get all other registrations on this transaction, and cache |
|
| 2543 | + //the attendees for them so we don't have to run another query using force_join |
|
| 2544 | + $registrations = $REG->get_all(array( |
|
| 2545 | + array( |
|
| 2546 | + 'TXN_ID' => $this->_registration->transaction_ID(), |
|
| 2547 | + 'REG_ID' => array('!=', $this->_registration->ID()), |
|
| 2548 | + ), |
|
| 2549 | + 'force_join' => array('Attendee'), |
|
| 2550 | + )); |
|
| 2551 | + $this->_template_args['attendees'] = array(); |
|
| 2552 | + $this->_template_args['attendee_notice'] = ''; |
|
| 2553 | + if (empty($registrations) |
|
| 2554 | + || (is_array($registrations) |
|
| 2555 | + && ! EEH_Array::get_one_item_from_array($registrations)) |
|
| 2556 | + ) { |
|
| 2557 | + EE_Error::add_error( |
|
| 2558 | + esc_html__( |
|
| 2559 | + 'There are no records attached to this registration. Something may have gone wrong with the registration', |
|
| 2560 | + 'event_espresso' |
|
| 2561 | + ), __FILE__, __FUNCTION__, __LINE__ |
|
| 2562 | + ); |
|
| 2563 | + $this->_template_args['attendee_notice'] = EE_Error::get_notices(); |
|
| 2564 | + } else { |
|
| 2565 | + $att_nmbr = 1; |
|
| 2566 | + foreach ($registrations as $registration) { |
|
| 2567 | + /* @var $registration EE_Registration */ |
|
| 2568 | + $attendee = $registration->attendee() |
|
| 2569 | + ? $registration->attendee() |
|
| 2570 | + : EEM_Attendee::instance() |
|
| 2571 | + ->create_default_object(); |
|
| 2572 | + $this->_template_args['attendees'][$att_nmbr]['STS_ID'] = $registration->status_ID(); |
|
| 2573 | + $this->_template_args['attendees'][$att_nmbr]['fname'] = $attendee->fname(); |
|
| 2574 | + $this->_template_args['attendees'][$att_nmbr]['lname'] = $attendee->lname(); |
|
| 2575 | + $this->_template_args['attendees'][$att_nmbr]['email'] = $attendee->email(); |
|
| 2576 | + $this->_template_args['attendees'][$att_nmbr]['final_price'] = $registration->final_price(); |
|
| 2577 | + $this->_template_args['attendees'][$att_nmbr]['address'] = implode( |
|
| 2578 | + ', ', |
|
| 2579 | + $attendee->full_address_as_array() |
|
| 2580 | + ); |
|
| 2581 | + $this->_template_args['attendees'][$att_nmbr]['att_link'] = self::add_query_args_and_nonce( |
|
| 2582 | + array( |
|
| 2583 | + 'action' => 'edit_attendee', |
|
| 2584 | + 'post' => $attendee->ID(), |
|
| 2585 | + ), |
|
| 2586 | + REG_ADMIN_URL |
|
| 2587 | + ); |
|
| 2588 | + $this->_template_args['attendees'][$att_nmbr]['event_name'] = $registration->event_obj()->name(); |
|
| 2589 | + $att_nmbr++; |
|
| 2590 | + } |
|
| 2591 | + $this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign; |
|
| 2592 | + } |
|
| 2593 | + $template_path = REG_TEMPLATE_PATH . 'reg_admin_details_main_meta_box_attendees.template.php'; |
|
| 2594 | + echo EEH_Template::display_template($template_path, $this->_template_args, true); |
|
| 2595 | + } |
|
| 2596 | + |
|
| 2597 | + |
|
| 2598 | + /** |
|
| 2599 | + * generates HTML for the Edit Registration side meta box |
|
| 2600 | + * |
|
| 2601 | + * @access public |
|
| 2602 | + * @return void |
|
| 2603 | + * @throws DomainException |
|
| 2604 | + * @throws EE_Error |
|
| 2605 | + * @throws InvalidArgumentException |
|
| 2606 | + * @throws InvalidDataTypeException |
|
| 2607 | + * @throws InvalidInterfaceException |
|
| 2608 | + */ |
|
| 2609 | + public function _reg_registrant_side_meta_box() |
|
| 2610 | + { |
|
| 2611 | + /*@var $attendee EE_Attendee */ |
|
| 2612 | + $att_check = $this->_registration->attendee(); |
|
| 2613 | + $attendee = $att_check instanceof EE_Attendee ? $att_check : EEM_Attendee::instance()->create_default_object(); |
|
| 2614 | + //now let's determine if this is not the primary registration. If it isn't then we set the |
|
| 2615 | + //primary_registration object for reference BUT ONLY if the Attendee object loaded is not the same as the |
|
| 2616 | + //primary registration object (that way we know if we need to show create button or not) |
|
| 2617 | + if ( ! $this->_registration->is_primary_registrant()) { |
|
| 2618 | + $primary_registration = $this->_registration->get_primary_registration(); |
|
| 2619 | + $primary_attendee = $primary_registration instanceof EE_Registration ? $primary_registration->attendee() |
|
| 2620 | + : null; |
|
| 2621 | + if ( ! $primary_attendee instanceof EE_Attendee || $attendee->ID() !== $primary_attendee->ID()) { |
|
| 2622 | + //in here? This means the displayed registration is not the primary registrant but ALREADY HAS its own |
|
| 2623 | + //custom attendee object so let's not worry about the primary reg. |
|
| 2624 | + $primary_registration = null; |
|
| 2625 | + } |
|
| 2626 | + } else { |
|
| 2627 | + $primary_registration = null; |
|
| 2628 | + } |
|
| 2629 | + $this->_template_args['ATT_ID'] = $attendee->ID(); |
|
| 2630 | + $this->_template_args['fname'] = $attendee->fname(); |
|
| 2631 | + $this->_template_args['lname'] = $attendee->lname(); |
|
| 2632 | + $this->_template_args['email'] = $attendee->email(); |
|
| 2633 | + $this->_template_args['phone'] = $attendee->phone(); |
|
| 2634 | + $this->_template_args['formatted_address'] = EEH_Address::format($attendee); |
|
| 2635 | + //edit link |
|
| 2636 | + $this->_template_args['att_edit_link'] = EE_Admin_Page::add_query_args_and_nonce(array( |
|
| 2637 | + 'action' => 'edit_attendee', |
|
| 2638 | + 'post' => $attendee->ID(), |
|
| 2639 | + ), REG_ADMIN_URL); |
|
| 2640 | + $this->_template_args['att_edit_label'] = esc_html__('View/Edit Contact', 'event_espresso'); |
|
| 2641 | + //create link |
|
| 2642 | + $this->_template_args['create_link'] = $primary_registration instanceof EE_Registration |
|
| 2643 | + ? EE_Admin_Page::add_query_args_and_nonce(array( |
|
| 2644 | + 'action' => 'duplicate_attendee', |
|
| 2645 | + '_REG_ID' => $this->_registration->ID(), |
|
| 2646 | + ), REG_ADMIN_URL) : ''; |
|
| 2647 | + $this->_template_args['create_label'] = esc_html__('Create Contact', 'event_espresso'); |
|
| 2648 | + $this->_template_args['att_check'] = $att_check; |
|
| 2649 | + $template_path = REG_TEMPLATE_PATH . 'reg_admin_details_side_meta_box_registrant.template.php'; |
|
| 2650 | + echo EEH_Template::display_template($template_path, $this->_template_args, true); |
|
| 2651 | + } |
|
| 2652 | + |
|
| 2653 | + |
|
| 2654 | + /** |
|
| 2655 | + * trash or restore registrations |
|
| 2656 | + * |
|
| 2657 | + * @param boolean $trash whether to archive or restore |
|
| 2658 | + * @return void |
|
| 2659 | + * @throws EE_Error |
|
| 2660 | + * @throws InvalidArgumentException |
|
| 2661 | + * @throws InvalidDataTypeException |
|
| 2662 | + * @throws InvalidInterfaceException |
|
| 2663 | + * @throws RuntimeException |
|
| 2664 | + * @access protected |
|
| 2665 | + */ |
|
| 2666 | + protected function _trash_or_restore_registrations($trash = true) |
|
| 2667 | + { |
|
| 2668 | + //if empty _REG_ID then get out because there's nothing to do |
|
| 2669 | + if (empty($this->_req_data['_REG_ID'])) { |
|
| 2670 | + EE_Error::add_error( |
|
| 2671 | + sprintf( |
|
| 2672 | + esc_html__( |
|
| 2673 | + 'In order to %1$s registrations you must select which ones you wish to %1$s by clicking the checkboxes.', |
|
| 2674 | + 'event_espresso' |
|
| 2675 | + ), |
|
| 2676 | + $trash ? 'trash' : 'restore' |
|
| 2677 | + ), |
|
| 2678 | + __FILE__, __LINE__, __FUNCTION__ |
|
| 2679 | + ); |
|
| 2680 | + $this->_redirect_after_action(false, '', '', array(), true); |
|
| 2681 | + } |
|
| 2682 | + $success = 0; |
|
| 2683 | + $overwrite_msgs = false; |
|
| 2684 | + //Checkboxes |
|
| 2685 | + if ( ! is_array($this->_req_data['_REG_ID'])) { |
|
| 2686 | + $this->_req_data['_REG_ID'] = array($this->_req_data['_REG_ID']); |
|
| 2687 | + } |
|
| 2688 | + $reg_count = count($this->_req_data['_REG_ID']); |
|
| 2689 | + // cycle thru checkboxes |
|
| 2690 | + foreach ($this->_req_data['_REG_ID'] as $REG_ID) { |
|
| 2691 | + /** @var EE_Registration $REG */ |
|
| 2692 | + $REG = EEM_Registration::instance()->get_one_by_ID($REG_ID); |
|
| 2693 | + $payments = $REG->registration_payments(); |
|
| 2694 | + if (! empty($payments)) { |
|
| 2695 | + $name = $REG->attendee() instanceof EE_Attendee |
|
| 2696 | + ? $REG->attendee()->full_name() |
|
| 2697 | + : esc_html__('Unknown Attendee', 'event_espresso'); |
|
| 2698 | + $overwrite_msgs = true; |
|
| 2699 | + EE_Error::add_error( |
|
| 2700 | + sprintf( |
|
| 2701 | + esc_html__( |
|
| 2702 | + 'The registration for %s could not be trashed because it has payments attached to the related transaction. If you wish to trash this registration you must first delete the payments on the related transaction.', |
|
| 2703 | + 'event_espresso' |
|
| 2704 | + ), |
|
| 2705 | + $name |
|
| 2706 | + ), |
|
| 2707 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 2708 | + ); |
|
| 2709 | + //can't trash this registration because it has payments. |
|
| 2710 | + continue; |
|
| 2711 | + } |
|
| 2712 | + $updated = $trash ? $REG->delete() : $REG->restore(); |
|
| 2713 | + if ($updated) { |
|
| 2714 | + $success++; |
|
| 2715 | + } |
|
| 2716 | + } |
|
| 2717 | + $this->_redirect_after_action( |
|
| 2718 | + $success === $reg_count, // were ALL registrations affected? |
|
| 2719 | + $success > 1 |
|
| 2720 | + ? esc_html__('Registrations', 'event_espresso') |
|
| 2721 | + : esc_html__('Registration', 'event_espresso'), |
|
| 2722 | + $trash |
|
| 2723 | + ? esc_html__('moved to the trash', 'event_espresso') |
|
| 2724 | + : esc_html__('restored', 'event_espresso'), |
|
| 2725 | + array('action' => 'default'), |
|
| 2726 | + $overwrite_msgs |
|
| 2727 | + ); |
|
| 2728 | + } |
|
| 2729 | + |
|
| 2730 | + |
|
| 2731 | + /** |
|
| 2732 | + * This is used to permanently delete registrations. Note, this will handle not only deleting permanently the |
|
| 2733 | + * registration but also. |
|
| 2734 | + * 1. Removing relations to EE_Attendee |
|
| 2735 | + * 2. Deleting permanently the related transaction, but ONLY if all related registrations to the transaction are |
|
| 2736 | + * ALSO trashed. |
|
| 2737 | + * 3. Deleting permanently any related Line items but only if the above conditions are met. |
|
| 2738 | + * 4. Removing relationships between all tickets and the related registrations |
|
| 2739 | + * 5. Deleting permanently any related Answers (and the answers for other related registrations that were deleted.) |
|
| 2740 | + * 6. Deleting permanently any related Checkins. |
|
| 2741 | + * |
|
| 2742 | + * @return void |
|
| 2743 | + * @throws EE_Error |
|
| 2744 | + * @throws InvalidArgumentException |
|
| 2745 | + * @throws InvalidDataTypeException |
|
| 2746 | + * @throws InvalidInterfaceException |
|
| 2747 | + */ |
|
| 2748 | + protected function _delete_registrations() |
|
| 2749 | + { |
|
| 2750 | + $REG_MDL = EEM_Registration::instance(); |
|
| 2751 | + $success = 1; |
|
| 2752 | + //Checkboxes |
|
| 2753 | + if ( ! empty($this->_req_data['_REG_ID']) && is_array($this->_req_data['_REG_ID'])) { |
|
| 2754 | + // if array has more than one element than success message should be plural |
|
| 2755 | + $success = count($this->_req_data['_REG_ID']) > 1 ? 2 : 1; |
|
| 2756 | + // cycle thru checkboxes |
|
| 2757 | + while (list($ind, $REG_ID) = each($this->_req_data['_REG_ID'])) { |
|
| 2758 | + $REG = $REG_MDL->get_one_by_ID($REG_ID); |
|
| 2759 | + if ( ! $REG instanceof EE_Registration) { |
|
| 2760 | + continue; |
|
| 2761 | + } |
|
| 2762 | + $deleted = $this->_delete_registration($REG); |
|
| 2763 | + if ( ! $deleted) { |
|
| 2764 | + $success = 0; |
|
| 2765 | + } |
|
| 2766 | + } |
|
| 2767 | + } else { |
|
| 2768 | + // grab single id and delete |
|
| 2769 | + $REG_ID = $this->_req_data['_REG_ID']; |
|
| 2770 | + $REG = $REG_MDL->get_one_by_ID($REG_ID); |
|
| 2771 | + $deleted = $this->_delete_registration($REG); |
|
| 2772 | + if ( ! $deleted) { |
|
| 2773 | + $success = 0; |
|
| 2774 | + } |
|
| 2775 | + } |
|
| 2776 | + $what = $success > 1 |
|
| 2777 | + ? esc_html__('Registrations', 'event_espresso') |
|
| 2778 | + : esc_html__('Registration', 'event_espresso'); |
|
| 2779 | + $action_desc = esc_html__('permanently deleted.', 'event_espresso'); |
|
| 2780 | + $this->_redirect_after_action( |
|
| 2781 | + $success, |
|
| 2782 | + $what, |
|
| 2783 | + $action_desc, |
|
| 2784 | + array('action' => 'default'), |
|
| 2785 | + true |
|
| 2786 | + ); |
|
| 2787 | + } |
|
| 2788 | + |
|
| 2789 | + |
|
| 2790 | + /** |
|
| 2791 | + * handles the permanent deletion of a registration. See comments with _delete_registrations() for details on what |
|
| 2792 | + * models get affected. |
|
| 2793 | + * |
|
| 2794 | + * @param EE_Registration $REG registration to be deleted permenantly |
|
| 2795 | + * @return bool true = successful deletion, false = fail. |
|
| 2796 | + * @throws EE_Error |
|
| 2797 | + */ |
|
| 2798 | + protected function _delete_registration(EE_Registration $REG) |
|
| 2799 | + { |
|
| 2800 | + //first we start with the transaction... ultimately, we WILL not delete permanently if there are any related |
|
| 2801 | + //registrations on the transaction that are NOT trashed. |
|
| 2802 | + $TXN = $REG->get_first_related('Transaction'); |
|
| 2803 | + $REGS = $TXN->get_many_related('Registration'); |
|
| 2804 | + $all_trashed = true; |
|
| 2805 | + foreach ($REGS as $registration) { |
|
| 2806 | + if ( ! $registration->get('REG_deleted')) { |
|
| 2807 | + $all_trashed = false; |
|
| 2808 | + } |
|
| 2809 | + } |
|
| 2810 | + if ( ! $all_trashed) { |
|
| 2811 | + EE_Error::add_error( |
|
| 2812 | + esc_html__( |
|
| 2813 | + 'Unable to permanently delete this registration. Before this registration can be permanently deleted, all registrations made in the same transaction must be trashed as well. These registrations will be permanently deleted in the same action.', |
|
| 2814 | + 'event_espresso' |
|
| 2815 | + ), |
|
| 2816 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 2817 | + ); |
|
| 2818 | + return false; |
|
| 2819 | + } |
|
| 2820 | + //k made it here so that means we can delete all the related transactions and their answers (but let's do them |
|
| 2821 | + //separately from THIS one). |
|
| 2822 | + foreach ($REGS as $registration) { |
|
| 2823 | + //delete related answers |
|
| 2824 | + $registration->delete_related_permanently('Answer'); |
|
| 2825 | + //remove relationship to EE_Attendee (but we ALWAYS leave the contact record intact) |
|
| 2826 | + $attendee = $registration->get_first_related('Attendee'); |
|
| 2827 | + if ($attendee instanceof EE_Attendee) { |
|
| 2828 | + $registration->_remove_relation_to($attendee, 'Attendee'); |
|
| 2829 | + } |
|
| 2830 | + //now remove relationships to tickets on this registration. |
|
| 2831 | + $registration->_remove_relations('Ticket'); |
|
| 2832 | + //now delete permanently the checkins related to this registration. |
|
| 2833 | + $registration->delete_related_permanently('Checkin'); |
|
| 2834 | + if ($registration->ID() === $REG->ID()) { |
|
| 2835 | + continue; |
|
| 2836 | + } //we don't want to delete permanently the existing registration just yet. |
|
| 2837 | + //remove relation to transaction for these registrations if NOT the existing registrations |
|
| 2838 | + $registration->_remove_relations('Transaction'); |
|
| 2839 | + //delete permanently any related messages. |
|
| 2840 | + $registration->delete_related_permanently('Message'); |
|
| 2841 | + //now delete this registration permanently |
|
| 2842 | + $registration->delete_permanently(); |
|
| 2843 | + } |
|
| 2844 | + //now all related registrations on the transaction are handled. So let's just handle this registration itself |
|
| 2845 | + // (the transaction and line items should be all that's left). |
|
| 2846 | + // delete the line items related to the transaction for this registration. |
|
| 2847 | + $TXN->delete_related_permanently('Line_Item'); |
|
| 2848 | + //we need to remove all the relationships on the transaction |
|
| 2849 | + $TXN->delete_related_permanently('Payment'); |
|
| 2850 | + $TXN->delete_related_permanently('Extra_Meta'); |
|
| 2851 | + $TXN->delete_related_permanently('Message'); |
|
| 2852 | + //now we can delete this REG permanently (and the transaction of course) |
|
| 2853 | + $REG->delete_related_permanently('Transaction'); |
|
| 2854 | + return $REG->delete_permanently(); |
|
| 2855 | + } |
|
| 2856 | + |
|
| 2857 | + |
|
| 2858 | + /** |
|
| 2859 | + * generates HTML for the Register New Attendee Admin page |
|
| 2860 | + * |
|
| 2861 | + * @access private |
|
| 2862 | + * @throws DomainException |
|
| 2863 | + * @throws EE_Error |
|
| 2864 | + */ |
|
| 2865 | + public function new_registration() |
|
| 2866 | + { |
|
| 2867 | + if ( ! $this->_set_reg_event()) { |
|
| 2868 | + throw new EE_Error( |
|
| 2869 | + esc_html__( |
|
| 2870 | + 'Unable to continue with registering because there is no Event ID in the request', |
|
| 2871 | + 'event_espresso' |
|
| 2872 | + ) |
|
| 2873 | + ); |
|
| 2874 | + } |
|
| 2875 | + EE_Registry::instance()->REQ->set_espresso_page(true); |
|
| 2876 | + // gotta start with a clean slate if we're not coming here via ajax |
|
| 2877 | + if ( ! defined('DOING_AJAX') |
|
| 2878 | + && ( ! isset($this->_req_data['processing_registration']) || isset($this->_req_data['step_error'])) |
|
| 2879 | + ) { |
|
| 2880 | + EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
| 2881 | + } |
|
| 2882 | + $this->_template_args['event_name'] = ''; |
|
| 2883 | + // event name |
|
| 2884 | + if ($this->_reg_event) { |
|
| 2885 | + $this->_template_args['event_name'] = $this->_reg_event->name(); |
|
| 2886 | + $edit_event_url = self::add_query_args_and_nonce(array( |
|
| 2887 | + 'action' => 'edit', |
|
| 2888 | + 'post' => $this->_reg_event->ID(), |
|
| 2889 | + ), EVENTS_ADMIN_URL); |
|
| 2890 | + $edit_event_lnk = '<a href="' |
|
| 2891 | + . $edit_event_url |
|
| 2892 | + . '" title="' |
|
| 2893 | + . esc_attr__('Edit ', 'event_espresso') |
|
| 2894 | + . $this->_reg_event->name() |
|
| 2895 | + . '">' |
|
| 2896 | + . esc_html__('Edit Event', 'event_espresso') |
|
| 2897 | + . '</a>'; |
|
| 2898 | + $this->_template_args['event_name'] .= ' <span class="admin-page-header-edit-lnk not-bold">' |
|
| 2899 | + . $edit_event_lnk |
|
| 2900 | + . '</span>'; |
|
| 2901 | + } |
|
| 2902 | + $this->_template_args['step_content'] = $this->_get_registration_step_content(); |
|
| 2903 | + if (defined('DOING_AJAX')) { |
|
| 2904 | + $this->_return_json(); |
|
| 2905 | + } |
|
| 2906 | + // grab header |
|
| 2907 | + $template_path = |
|
| 2908 | + REG_TEMPLATE_PATH . 'reg_admin_register_new_attendee.template.php'; |
|
| 2909 | + $this->_template_args['admin_page_content'] = EEH_Template::display_template($template_path, |
|
| 2910 | + $this->_template_args, true); |
|
| 2911 | + //$this->_set_publish_post_box_vars( NULL, FALSE, FALSE, NULL, FALSE ); |
|
| 2912 | + // the details template wrapper |
|
| 2913 | + $this->display_admin_page_with_sidebar(); |
|
| 2914 | + } |
|
| 2915 | + |
|
| 2916 | + |
|
| 2917 | + /** |
|
| 2918 | + * This returns the content for a registration step |
|
| 2919 | + * |
|
| 2920 | + * @access protected |
|
| 2921 | + * @return string html |
|
| 2922 | + * @throws DomainException |
|
| 2923 | + * @throws EE_Error |
|
| 2924 | + * @throws InvalidArgumentException |
|
| 2925 | + * @throws InvalidDataTypeException |
|
| 2926 | + * @throws InvalidInterfaceException |
|
| 2927 | + */ |
|
| 2928 | + protected function _get_registration_step_content() |
|
| 2929 | + { |
|
| 2930 | + if (isset($_COOKIE['ee_registration_added']) && $_COOKIE['ee_registration_added']) { |
|
| 2931 | + $warning_msg = sprintf( |
|
| 2932 | + esc_html__( |
|
| 2933 | + '%2$sWARNING!!!%3$s%1$sPlease do not use the back button to return to this page for the purpose of adding another registration.%1$sThis can result in lost and/or corrupted data.%1$sIf you wish to add another registration, then please click the%1$s%7$s"Add Another New Registration to Event"%8$s button%1$son the Transaction details page, after you are redirected.%1$s%1$s%4$s redirecting in %5$s seconds %6$s', |
|
| 2934 | + 'event_espresso' |
|
| 2935 | + ), |
|
| 2936 | + '<br />', |
|
| 2937 | + '<h3 class="important-notice">', |
|
| 2938 | + '</h3>', |
|
| 2939 | + '<div class="float-right">', |
|
| 2940 | + '<span id="redirect_timer" class="important-notice">30</span>', |
|
| 2941 | + '</div>', |
|
| 2942 | + '<b>', |
|
| 2943 | + '</b>' |
|
| 2944 | + ); |
|
| 2945 | + return ' |
|
| 2946 | 2946 | <div id="ee-add-reg-back-button-dv"><p>' . $warning_msg . '</p></div> |
| 2947 | 2947 | <script > |
| 2948 | 2948 | // WHOAH !!! it appears that someone is using the back button from the Transaction admin page |
@@ -2955,841 +2955,841 @@ discard block |
||
| 2955 | 2955 | } |
| 2956 | 2956 | }, 800 ); |
| 2957 | 2957 | </script >'; |
| 2958 | - } |
|
| 2959 | - $template_args = array( |
|
| 2960 | - 'title' => '', |
|
| 2961 | - 'content' => '', |
|
| 2962 | - 'step_button_text' => '', |
|
| 2963 | - 'show_notification_toggle' => false, |
|
| 2964 | - ); |
|
| 2965 | - //to indicate we're processing a new registration |
|
| 2966 | - $hidden_fields = array( |
|
| 2967 | - 'processing_registration' => array( |
|
| 2968 | - 'type' => 'hidden', |
|
| 2969 | - 'value' => 0, |
|
| 2970 | - ), |
|
| 2971 | - 'event_id' => array( |
|
| 2972 | - 'type' => 'hidden', |
|
| 2973 | - 'value' => $this->_reg_event->ID(), |
|
| 2974 | - ), |
|
| 2975 | - ); |
|
| 2976 | - //if the cart is empty then we know we're at step one so we'll display ticket selector |
|
| 2977 | - $cart = EE_Registry::instance()->SSN->cart(); |
|
| 2978 | - $step = ! $cart instanceof EE_Cart ? 'ticket' : 'questions'; |
|
| 2979 | - switch ($step) { |
|
| 2980 | - case 'ticket' : |
|
| 2981 | - $hidden_fields['processing_registration']['value'] = 1; |
|
| 2982 | - $template_args['title'] = esc_html__( |
|
| 2983 | - 'Step One: Select the Ticket for this registration', |
|
| 2984 | - 'event_espresso' |
|
| 2985 | - ); |
|
| 2986 | - $template_args['content'] = |
|
| 2987 | - EED_Ticket_Selector::instance()->display_ticket_selector($this->_reg_event); |
|
| 2988 | - $template_args['step_button_text'] = esc_html__( |
|
| 2989 | - 'Add Tickets and Continue to Registrant Details', |
|
| 2990 | - 'event_espresso' |
|
| 2991 | - ); |
|
| 2992 | - $template_args['show_notification_toggle'] = false; |
|
| 2993 | - break; |
|
| 2994 | - case 'questions' : |
|
| 2995 | - $hidden_fields['processing_registration']['value'] = 2; |
|
| 2996 | - $template_args['title'] = esc_html__( |
|
| 2997 | - 'Step Two: Add Registrant Details for this Registration', |
|
| 2998 | - 'event_espresso' |
|
| 2999 | - ); |
|
| 3000 | - //in theory we should be able to run EED_SPCO at this point because the cart should have been setup |
|
| 3001 | - // properly by the first process_reg_step run. |
|
| 3002 | - $template_args['content'] = |
|
| 3003 | - EED_Single_Page_Checkout::registration_checkout_for_admin(); |
|
| 3004 | - $template_args['step_button_text'] = esc_html__( |
|
| 3005 | - 'Save Registration and Continue to Details', |
|
| 3006 | - 'event_espresso' |
|
| 3007 | - ); |
|
| 3008 | - $template_args['show_notification_toggle'] = true; |
|
| 3009 | - break; |
|
| 3010 | - } |
|
| 3011 | - //we come back to the process_registration_step route. |
|
| 3012 | - $this->_set_add_edit_form_tags('process_reg_step', $hidden_fields); |
|
| 3013 | - return EEH_Template::display_template( |
|
| 3014 | - REG_TEMPLATE_PATH . 'reg_admin_register_new_attendee_step_content.template.php', |
|
| 3015 | - $template_args, |
|
| 3016 | - true |
|
| 3017 | - ); |
|
| 3018 | - } |
|
| 3019 | - |
|
| 3020 | - |
|
| 3021 | - /** |
|
| 3022 | - * set_reg_event |
|
| 3023 | - * |
|
| 3024 | - * @access private |
|
| 3025 | - * @return bool |
|
| 3026 | - * @throws EE_Error |
|
| 3027 | - * @throws InvalidArgumentException |
|
| 3028 | - * @throws InvalidDataTypeException |
|
| 3029 | - * @throws InvalidInterfaceException |
|
| 3030 | - */ |
|
| 3031 | - private function _set_reg_event() |
|
| 3032 | - { |
|
| 3033 | - if (is_object($this->_reg_event)) { |
|
| 3034 | - return true; |
|
| 3035 | - } |
|
| 3036 | - $EVT_ID = (! empty($this->_req_data['event_id'])) ? absint($this->_req_data['event_id']) : false; |
|
| 3037 | - if ( ! $EVT_ID) { |
|
| 3038 | - return false; |
|
| 3039 | - } |
|
| 3040 | - $this->_reg_event = EEM_Event::instance()->get_one_by_ID($EVT_ID); |
|
| 3041 | - return true; |
|
| 3042 | - } |
|
| 3043 | - |
|
| 3044 | - |
|
| 3045 | - /** |
|
| 3046 | - * process_reg_step |
|
| 3047 | - * |
|
| 3048 | - * @access public |
|
| 3049 | - * @return string |
|
| 3050 | - * @throws DomainException |
|
| 3051 | - * @throws EE_Error |
|
| 3052 | - * @throws InvalidArgumentException |
|
| 3053 | - * @throws InvalidDataTypeException |
|
| 3054 | - * @throws InvalidInterfaceException |
|
| 3055 | - * @throws ReflectionException |
|
| 3056 | - * @throws RuntimeException |
|
| 3057 | - */ |
|
| 3058 | - public function process_reg_step() |
|
| 3059 | - { |
|
| 3060 | - EE_System::do_not_cache(); |
|
| 3061 | - $this->_set_reg_event(); |
|
| 3062 | - EE_Registry::instance()->REQ->set_espresso_page(true); |
|
| 3063 | - EE_Registry::instance()->REQ->set('uts', time()); |
|
| 3064 | - //what step are we on? |
|
| 3065 | - $cart = EE_Registry::instance()->SSN->cart(); |
|
| 3066 | - $step = ! $cart instanceof EE_Cart ? 'ticket' : 'questions'; |
|
| 3067 | - //if doing ajax then we need to verify the nonce |
|
| 3068 | - if (defined('DOING_AJAX')) { |
|
| 3069 | - $nonce = isset($this->_req_data[$this->_req_nonce]) |
|
| 3070 | - ? sanitize_text_field($this->_req_data[$this->_req_nonce]) : ''; |
|
| 3071 | - $this->_verify_nonce($nonce, $this->_req_nonce); |
|
| 3072 | - } |
|
| 3073 | - switch ($step) { |
|
| 3074 | - case 'ticket' : |
|
| 3075 | - //process ticket selection |
|
| 3076 | - $success = EED_Ticket_Selector::instance()->process_ticket_selections(); |
|
| 3077 | - if ($success) { |
|
| 3078 | - EE_Error::add_success( |
|
| 3079 | - esc_html__( |
|
| 3080 | - 'Tickets Selected. Now complete the registration.', |
|
| 3081 | - 'event_espresso' |
|
| 3082 | - ) |
|
| 3083 | - ); |
|
| 3084 | - } else { |
|
| 3085 | - $query_args['step_error'] = $this->_req_data['step_error'] = true; |
|
| 3086 | - } |
|
| 3087 | - if (defined('DOING_AJAX')) { |
|
| 3088 | - $this->new_registration(); //display next step |
|
| 3089 | - } else { |
|
| 3090 | - $query_args = array( |
|
| 3091 | - 'action' => 'new_registration', |
|
| 3092 | - 'processing_registration' => 1, |
|
| 3093 | - 'event_id' => $this->_reg_event->ID(), |
|
| 3094 | - 'uts' => time(), |
|
| 3095 | - ); |
|
| 3096 | - $this->_redirect_after_action( |
|
| 3097 | - false, |
|
| 3098 | - '', |
|
| 3099 | - '', |
|
| 3100 | - $query_args, |
|
| 3101 | - true |
|
| 3102 | - ); |
|
| 3103 | - } |
|
| 3104 | - break; |
|
| 3105 | - case 'questions' : |
|
| 3106 | - if (! isset( |
|
| 3107 | - $this->_req_data['txn_reg_status_change'], |
|
| 3108 | - $this->_req_data['txn_reg_status_change']['send_notifications']) |
|
| 3109 | - ) { |
|
| 3110 | - add_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications', '__return_false', 15); |
|
| 3111 | - } |
|
| 3112 | - //process registration |
|
| 3113 | - $transaction = EED_Single_Page_Checkout::instance()->process_registration_from_admin(); |
|
| 3114 | - if ($cart instanceof EE_Cart) { |
|
| 3115 | - $grand_total = $cart->get_cart_grand_total(); |
|
| 3116 | - if ($grand_total instanceof EE_Line_Item) { |
|
| 3117 | - $grand_total->save_this_and_descendants_to_txn(); |
|
| 3118 | - } |
|
| 3119 | - } |
|
| 3120 | - if ( ! $transaction instanceof EE_Transaction) { |
|
| 3121 | - $query_args = array( |
|
| 3122 | - 'action' => 'new_registration', |
|
| 3123 | - 'processing_registration' => 2, |
|
| 3124 | - 'event_id' => $this->_reg_event->ID(), |
|
| 3125 | - 'uts' => time(), |
|
| 3126 | - ); |
|
| 3127 | - if (defined('DOING_AJAX')) { |
|
| 3128 | - //display registration form again because there are errors (maybe validation?) |
|
| 3129 | - $this->new_registration(); |
|
| 3130 | - return; |
|
| 3131 | - } else { |
|
| 3132 | - $this->_redirect_after_action( |
|
| 3133 | - false, |
|
| 3134 | - '', |
|
| 3135 | - '', |
|
| 3136 | - $query_args, |
|
| 3137 | - true |
|
| 3138 | - ); |
|
| 3139 | - return; |
|
| 3140 | - } |
|
| 3141 | - } |
|
| 3142 | - // maybe update status, and make sure to save transaction if not done already |
|
| 3143 | - if ( ! $transaction->update_status_based_on_total_paid()) { |
|
| 3144 | - $transaction->save(); |
|
| 3145 | - } |
|
| 3146 | - EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
| 3147 | - $this->_req_data = array(); |
|
| 3148 | - $query_args = array( |
|
| 3149 | - 'action' => 'redirect_to_txn', |
|
| 3150 | - 'TXN_ID' => $transaction->ID(), |
|
| 3151 | - 'EVT_ID' => $this->_reg_event->ID(), |
|
| 3152 | - 'event_name' => urlencode($this->_reg_event->name()), |
|
| 3153 | - 'redirect_from' => 'new_registration', |
|
| 3154 | - ); |
|
| 3155 | - $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 3156 | - break; |
|
| 3157 | - } |
|
| 3158 | - //what are you looking here for? Should be nothing to do at this point. |
|
| 3159 | - } |
|
| 3160 | - |
|
| 3161 | - |
|
| 3162 | - /** |
|
| 3163 | - * redirect_to_txn |
|
| 3164 | - * |
|
| 3165 | - * @access public |
|
| 3166 | - * @return void |
|
| 3167 | - * @throws EE_Error |
|
| 3168 | - * @throws InvalidArgumentException |
|
| 3169 | - * @throws InvalidDataTypeException |
|
| 3170 | - * @throws InvalidInterfaceException |
|
| 3171 | - */ |
|
| 3172 | - public function redirect_to_txn() |
|
| 3173 | - { |
|
| 3174 | - EE_System::do_not_cache(); |
|
| 3175 | - EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
| 3176 | - $query_args = array( |
|
| 3177 | - 'action' => 'view_transaction', |
|
| 3178 | - 'TXN_ID' => isset($this->_req_data['TXN_ID']) ? absint($this->_req_data['TXN_ID']) : 0, |
|
| 3179 | - 'page' => 'espresso_transactions', |
|
| 3180 | - ); |
|
| 3181 | - if (isset($this->_req_data['EVT_ID'], $this->_req_data['redirect_from'])) { |
|
| 3182 | - $query_args['EVT_ID'] = $this->_req_data['EVT_ID']; |
|
| 3183 | - $query_args['event_name'] = urlencode($this->_req_data['event_name']); |
|
| 3184 | - $query_args['redirect_from'] = $this->_req_data['redirect_from']; |
|
| 3185 | - } |
|
| 3186 | - EE_Error::add_success( |
|
| 3187 | - esc_html__( |
|
| 3188 | - 'Registration Created. Please review the transaction and add any payments as necessary', |
|
| 3189 | - 'event_espresso' |
|
| 3190 | - ) |
|
| 3191 | - ); |
|
| 3192 | - $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 3193 | - } |
|
| 3194 | - |
|
| 3195 | - |
|
| 3196 | - /** |
|
| 3197 | - * generates HTML for the Attendee Contact List |
|
| 3198 | - * |
|
| 3199 | - * @access protected |
|
| 3200 | - * @return void |
|
| 3201 | - */ |
|
| 3202 | - protected function _attendee_contact_list_table() |
|
| 3203 | - { |
|
| 3204 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 3205 | - $this->_search_btn_label = esc_html__('Contacts', 'event_espresso'); |
|
| 3206 | - $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 3207 | - } |
|
| 3208 | - |
|
| 3209 | - |
|
| 3210 | - /** |
|
| 3211 | - * get_attendees |
|
| 3212 | - * |
|
| 3213 | - * @param $per_page |
|
| 3214 | - * @param bool $count whether to return count or data. |
|
| 3215 | - * @param bool $trash |
|
| 3216 | - * @return array |
|
| 3217 | - * @throws EE_Error |
|
| 3218 | - * @throws InvalidArgumentException |
|
| 3219 | - * @throws InvalidDataTypeException |
|
| 3220 | - * @throws InvalidInterfaceException |
|
| 3221 | - * @access public |
|
| 3222 | - */ |
|
| 3223 | - public function get_attendees($per_page, $count = false, $trash = false) |
|
| 3224 | - { |
|
| 3225 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 3226 | - require_once(REG_ADMIN . 'EE_Attendee_Contact_List_Table.class.php'); |
|
| 3227 | - $ATT_MDL = EEM_Attendee::instance(); |
|
| 3228 | - $this->_req_data['orderby'] = ! empty($this->_req_data['orderby']) ? $this->_req_data['orderby'] : ''; |
|
| 3229 | - switch ($this->_req_data['orderby']) { |
|
| 3230 | - case 'ATT_ID': |
|
| 3231 | - $orderby = 'ATT_ID'; |
|
| 3232 | - break; |
|
| 3233 | - case 'ATT_fname': |
|
| 3234 | - $orderby = 'ATT_fname'; |
|
| 3235 | - break; |
|
| 3236 | - case 'ATT_email': |
|
| 3237 | - $orderby = 'ATT_email'; |
|
| 3238 | - break; |
|
| 3239 | - case 'ATT_city': |
|
| 3240 | - $orderby = 'ATT_city'; |
|
| 3241 | - break; |
|
| 3242 | - case 'STA_ID': |
|
| 3243 | - $orderby = 'STA_ID'; |
|
| 3244 | - break; |
|
| 3245 | - case 'CNT_ID': |
|
| 3246 | - $orderby = 'CNT_ID'; |
|
| 3247 | - break; |
|
| 3248 | - case 'Registration_Count': |
|
| 3249 | - $orderby = 'Registration_Count'; |
|
| 3250 | - break; |
|
| 3251 | - default: |
|
| 3252 | - $orderby = 'ATT_lname'; |
|
| 3253 | - } |
|
| 3254 | - $sort = (isset($this->_req_data['order']) && ! empty($this->_req_data['order'])) |
|
| 3255 | - ? $this->_req_data['order'] |
|
| 3256 | - : 'ASC'; |
|
| 3257 | - $current_page = isset($this->_req_data['paged']) && ! empty($this->_req_data['paged']) |
|
| 3258 | - ? $this->_req_data['paged'] |
|
| 3259 | - : 1; |
|
| 3260 | - $per_page = isset($per_page) && ! empty($per_page) ? $per_page : 10; |
|
| 3261 | - $per_page = isset($this->_req_data['perpage']) && ! empty($this->_req_data['perpage']) |
|
| 3262 | - ? $this->_req_data['perpage'] |
|
| 3263 | - : $per_page; |
|
| 3264 | - $_where = array(); |
|
| 3265 | - if ( ! empty($this->_req_data['s'])) { |
|
| 3266 | - $sstr = '%' . $this->_req_data['s'] . '%'; |
|
| 3267 | - $_where['OR'] = array( |
|
| 3268 | - 'Registration.Event.EVT_name' => array('LIKE', $sstr), |
|
| 3269 | - 'Registration.Event.EVT_desc' => array('LIKE', $sstr), |
|
| 3270 | - 'Registration.Event.EVT_short_desc' => array('LIKE', $sstr), |
|
| 3271 | - 'ATT_fname' => array('LIKE', $sstr), |
|
| 3272 | - 'ATT_lname' => array('LIKE', $sstr), |
|
| 3273 | - 'ATT_short_bio' => array('LIKE', $sstr), |
|
| 3274 | - 'ATT_email' => array('LIKE', $sstr), |
|
| 3275 | - 'ATT_address' => array('LIKE', $sstr), |
|
| 3276 | - 'ATT_address2' => array('LIKE', $sstr), |
|
| 3277 | - 'ATT_city' => array('LIKE', $sstr), |
|
| 3278 | - 'Country.CNT_name' => array('LIKE', $sstr), |
|
| 3279 | - 'State.STA_name' => array('LIKE', $sstr), |
|
| 3280 | - 'ATT_phone' => array('LIKE', $sstr), |
|
| 3281 | - 'Registration.REG_final_price' => array('LIKE', $sstr), |
|
| 3282 | - 'Registration.REG_code' => array('LIKE', $sstr), |
|
| 3283 | - 'Registration.REG_group_size' => array('LIKE', $sstr), |
|
| 3284 | - ); |
|
| 3285 | - } |
|
| 3286 | - $offset = ($current_page - 1) * $per_page; |
|
| 3287 | - $limit = $count ? null : array($offset, $per_page); |
|
| 3288 | - $query_args = array( |
|
| 3289 | - $_where, |
|
| 3290 | - 'extra_selects' => array('Registration_Count' => array('Registration.REG_ID', 'count', '%d')), |
|
| 3291 | - 'limit' => $limit |
|
| 3292 | - ); |
|
| 3293 | - if (! $count) { |
|
| 3294 | - $query_args['order_by'] = array($orderby => $sort); |
|
| 3295 | - } |
|
| 3296 | - if ($trash) { |
|
| 3297 | - $query_args[0]['status'] = array('!=', 'publish'); |
|
| 3298 | - $all_attendees = $count |
|
| 3299 | - ? $ATT_MDL->count($query_args, 'ATT_ID', true) |
|
| 3300 | - : $ATT_MDL->get_all($query_args); |
|
| 3301 | - } else { |
|
| 3302 | - $query_args[0]['status'] = array('IN', array('publish')); |
|
| 3303 | - $all_attendees = $count |
|
| 3304 | - ? $ATT_MDL->count($query_args, 'ATT_ID', true) |
|
| 3305 | - : $ATT_MDL->get_all($query_args); |
|
| 3306 | - } |
|
| 3307 | - return $all_attendees; |
|
| 3308 | - } |
|
| 3309 | - |
|
| 3310 | - |
|
| 3311 | - /** |
|
| 3312 | - * This is just taking care of resending the registration confirmation |
|
| 3313 | - * |
|
| 3314 | - * @access protected |
|
| 3315 | - * @return void |
|
| 3316 | - */ |
|
| 3317 | - protected function _resend_registration() |
|
| 3318 | - { |
|
| 3319 | - $this->_process_resend_registration(); |
|
| 3320 | - $query_args = isset($this->_req_data['redirect_to']) |
|
| 3321 | - ? array('action' => $this->_req_data['redirect_to'], '_REG_ID' => $this->_req_data['_REG_ID']) |
|
| 3322 | - : array('action' => 'default'); |
|
| 3323 | - $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 3324 | - } |
|
| 3325 | - |
|
| 3326 | - /** |
|
| 3327 | - * Creates a registration report, but accepts the name of a method to use for preparing the query parameters |
|
| 3328 | - * to use when selecting registrations |
|
| 3329 | - * @param string $method_name_for_getting_query_params the name of the method (on this class) to use for preparing |
|
| 3330 | - * the query parameters from the request |
|
| 3331 | - * @return void ends the request with a redirect or download |
|
| 3332 | - */ |
|
| 3333 | - public function _registrations_report_base( $method_name_for_getting_query_params ) |
|
| 3334 | - { |
|
| 3335 | - if (! defined('EE_USE_OLD_CSV_REPORT_CLASS')) { |
|
| 3336 | - wp_redirect(EE_Admin_Page::add_query_args_and_nonce( |
|
| 3337 | - array( |
|
| 3338 | - 'page' => 'espresso_batch', |
|
| 3339 | - 'batch' => 'file', |
|
| 3340 | - 'EVT_ID' => isset($this->_req_data['EVT_ID']) ? $this->_req_data['EVT_ID'] : null, |
|
| 3341 | - 'filters' => urlencode( |
|
| 3342 | - serialize( |
|
| 3343 | - call_user_func( |
|
| 3344 | - array( $this, $method_name_for_getting_query_params ), |
|
| 3345 | - EEH_Array::is_set( |
|
| 3346 | - $this->_req_data, |
|
| 3347 | - 'filters', |
|
| 3348 | - array() |
|
| 3349 | - ) |
|
| 3350 | - ) |
|
| 3351 | - ) |
|
| 3352 | - ), |
|
| 3353 | - 'use_filters' => EEH_Array::is_set($this->_req_data, 'use_filters', false), |
|
| 3354 | - 'job_handler' => urlencode('EventEspressoBatchRequest\JobHandlers\RegistrationsReport'), |
|
| 3355 | - 'return_url' => urlencode($this->_req_data['return_url']), |
|
| 3356 | - ))); |
|
| 3357 | - } else { |
|
| 3358 | - $new_request_args = array( |
|
| 3359 | - 'export' => 'report', |
|
| 3360 | - 'action' => 'registrations_report_for_event', |
|
| 3361 | - 'EVT_ID' => isset($this->_req_data['EVT_ID']) ? $this->_req_data['EVT_ID'] : null, |
|
| 3362 | - ); |
|
| 3363 | - $this->_req_data = array_merge($this->_req_data, $new_request_args); |
|
| 3364 | - if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 3365 | - require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 3366 | - $EE_Export = EE_Export::instance($this->_req_data); |
|
| 3367 | - $EE_Export->export(); |
|
| 3368 | - } |
|
| 3369 | - } |
|
| 3370 | - } |
|
| 3371 | - |
|
| 3372 | - |
|
| 3373 | - |
|
| 3374 | - /** |
|
| 3375 | - * Creates a registration report using only query parameters in the request |
|
| 3376 | - * @return void |
|
| 3377 | - */ |
|
| 3378 | - public function _registrations_report() |
|
| 3379 | - { |
|
| 3380 | - $this->_registrations_report_base('_get_registration_query_parameters'); |
|
| 3381 | - } |
|
| 3382 | - |
|
| 3383 | - |
|
| 3384 | - public function _contact_list_export() |
|
| 3385 | - { |
|
| 3386 | - if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 3387 | - require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 3388 | - $EE_Export = EE_Export::instance($this->_req_data); |
|
| 3389 | - $EE_Export->export_attendees(); |
|
| 3390 | - } |
|
| 3391 | - } |
|
| 3392 | - |
|
| 3393 | - |
|
| 3394 | - public function _contact_list_report() |
|
| 3395 | - { |
|
| 3396 | - if ( ! defined('EE_USE_OLD_CSV_REPORT_CLASS')) { |
|
| 3397 | - wp_redirect(EE_Admin_Page::add_query_args_and_nonce(array( |
|
| 3398 | - 'page' => 'espresso_batch', |
|
| 3399 | - 'batch' => 'file', |
|
| 3400 | - 'job_handler' => urlencode('EventEspressoBatchRequest\JobHandlers\AttendeesReport'), |
|
| 3401 | - 'return_url' => urlencode($this->_req_data['return_url']), |
|
| 3402 | - ))); |
|
| 3403 | - } else { |
|
| 3404 | - if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 3405 | - require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 3406 | - $EE_Export = EE_Export::instance($this->_req_data); |
|
| 3407 | - $EE_Export->report_attendees(); |
|
| 3408 | - } |
|
| 3409 | - } |
|
| 3410 | - } |
|
| 3411 | - |
|
| 3412 | - |
|
| 3413 | - |
|
| 3414 | - |
|
| 3415 | - |
|
| 3416 | - /*************************************** ATTENDEE DETAILS ***************************************/ |
|
| 3417 | - /** |
|
| 3418 | - * This duplicates the attendee object for the given incoming registration id and attendee_id. |
|
| 3419 | - * |
|
| 3420 | - * @return void |
|
| 3421 | - * @throws EE_Error |
|
| 3422 | - * @throws InvalidArgumentException |
|
| 3423 | - * @throws InvalidDataTypeException |
|
| 3424 | - * @throws InvalidInterfaceException |
|
| 3425 | - */ |
|
| 3426 | - protected function _duplicate_attendee() |
|
| 3427 | - { |
|
| 3428 | - $action = ! empty($this->_req_data['return']) ? $this->_req_data['return'] : 'default'; |
|
| 3429 | - //verify we have necessary info |
|
| 3430 | - if (empty($this->_req_data['_REG_ID'])) { |
|
| 3431 | - EE_Error::add_error( |
|
| 3432 | - esc_html__( |
|
| 3433 | - 'Unable to create the contact for the registration because the required parameters are not present (_REG_ID )', |
|
| 3434 | - 'event_espresso' |
|
| 3435 | - ), __FILE__, __LINE__, __FUNCTION__ |
|
| 3436 | - ); |
|
| 3437 | - $query_args = array('action' => $action); |
|
| 3438 | - $this->_redirect_after_action('', '', '', $query_args, true); |
|
| 3439 | - } |
|
| 3440 | - //okay necessary deets present... let's dupe the incoming attendee and attach to incoming registration. |
|
| 3441 | - $registration = EEM_Registration::instance()->get_one_by_ID($this->_req_data['_REG_ID']); |
|
| 3442 | - $attendee = $registration->attendee(); |
|
| 3443 | - //remove relation of existing attendee on registration |
|
| 3444 | - $registration->_remove_relation_to($attendee, 'Attendee'); |
|
| 3445 | - //new attendee |
|
| 3446 | - $new_attendee = clone $attendee; |
|
| 3447 | - $new_attendee->set('ATT_ID', 0); |
|
| 3448 | - $new_attendee->save(); |
|
| 3449 | - //add new attendee to reg |
|
| 3450 | - $registration->_add_relation_to($new_attendee, 'Attendee'); |
|
| 3451 | - EE_Error::add_success( |
|
| 3452 | - esc_html__( |
|
| 3453 | - 'New Contact record created. Now make any edits you wish to make for this contact.', |
|
| 3454 | - 'event_espresso' |
|
| 3455 | - ) |
|
| 3456 | - ); |
|
| 3457 | - //redirect to edit page for attendee |
|
| 3458 | - $query_args = array('post' => $new_attendee->ID(), 'action' => 'edit_attendee'); |
|
| 3459 | - $this->_redirect_after_action('', '', '', $query_args, true); |
|
| 3460 | - } |
|
| 3461 | - |
|
| 3462 | - |
|
| 3463 | - /** |
|
| 3464 | - * Callback invoked by parent EE_Admin_CPT class hooked in on `save_post` wp hook. |
|
| 3465 | - * @param int $post_id |
|
| 3466 | - * @param WP_POST $post |
|
| 3467 | - * @throws DomainException |
|
| 3468 | - * @throws EE_Error |
|
| 3469 | - * @throws InvalidArgumentException |
|
| 3470 | - * @throws InvalidDataTypeException |
|
| 3471 | - * @throws InvalidInterfaceException |
|
| 3472 | - * @throws LogicException |
|
| 3473 | - * @throws InvalidFormSubmissionException |
|
| 3474 | - */ |
|
| 3475 | - protected function _insert_update_cpt_item($post_id, $post) |
|
| 3476 | - { |
|
| 3477 | - $success = true; |
|
| 3478 | - $attendee = $post instanceof WP_Post && $post->post_type === 'espresso_attendees' |
|
| 3479 | - ? EEM_Attendee::instance()->get_one_by_ID($post_id) |
|
| 3480 | - : null; |
|
| 3481 | - //for attendee updates |
|
| 3482 | - if ($attendee instanceof EE_Attendee) { |
|
| 3483 | - //note we should only be UPDATING attendees at this point. |
|
| 3484 | - $updated_fields = array( |
|
| 3485 | - 'ATT_fname' => $this->_req_data['ATT_fname'], |
|
| 3486 | - 'ATT_lname' => $this->_req_data['ATT_lname'], |
|
| 3487 | - 'ATT_full_name' => $this->_req_data['ATT_fname'] . ' ' . $this->_req_data['ATT_lname'], |
|
| 3488 | - 'ATT_address' => isset($this->_req_data['ATT_address']) ? $this->_req_data['ATT_address'] : '', |
|
| 3489 | - 'ATT_address2' => isset($this->_req_data['ATT_address2']) ? $this->_req_data['ATT_address2'] : '', |
|
| 3490 | - 'ATT_city' => isset($this->_req_data['ATT_city']) ? $this->_req_data['ATT_city'] : '', |
|
| 3491 | - 'STA_ID' => isset($this->_req_data['STA_ID']) ? $this->_req_data['STA_ID'] : '', |
|
| 3492 | - 'CNT_ISO' => isset($this->_req_data['CNT_ISO']) ? $this->_req_data['CNT_ISO'] : '', |
|
| 3493 | - 'ATT_zip' => isset($this->_req_data['ATT_zip']) ? $this->_req_data['ATT_zip'] : '', |
|
| 3494 | - ); |
|
| 3495 | - foreach ($updated_fields as $field => $value) { |
|
| 3496 | - $attendee->set($field, $value); |
|
| 3497 | - } |
|
| 3498 | - |
|
| 3499 | - //process contact details metabox form handler (which will also save the attendee) |
|
| 3500 | - $contact_details_form = $this->getAttendeeContactDetailsMetaboxFormHandler($attendee); |
|
| 3501 | - $success = $contact_details_form->process($this->_req_data); |
|
| 3502 | - |
|
| 3503 | - $attendee_update_callbacks = apply_filters( |
|
| 3504 | - 'FHEE__Registrations_Admin_Page__insert_update_cpt_item__attendee_update', |
|
| 3505 | - array() |
|
| 3506 | - ); |
|
| 3507 | - foreach ($attendee_update_callbacks as $a_callback) { |
|
| 3508 | - if (false === call_user_func_array($a_callback, array($attendee, $this->_req_data))) { |
|
| 3509 | - throw new EE_Error( |
|
| 3510 | - sprintf( |
|
| 3511 | - esc_html__( |
|
| 3512 | - 'The %s callback given for the "FHEE__Registrations_Admin_Page__insert_update_cpt_item__attendee_update" filter is not a valid callback. Please check the spelling.', |
|
| 3513 | - 'event_espresso' |
|
| 3514 | - ), |
|
| 3515 | - $a_callback |
|
| 3516 | - ) |
|
| 3517 | - ); |
|
| 3518 | - } |
|
| 3519 | - } |
|
| 3520 | - } |
|
| 3521 | - |
|
| 3522 | - if ($success === false) { |
|
| 3523 | - EE_Error::add_error( |
|
| 3524 | - esc_html__( |
|
| 3525 | - 'Something went wrong with updating the meta table data for the registration.', |
|
| 3526 | - 'event_espresso' |
|
| 3527 | - ), |
|
| 3528 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 3529 | - ); |
|
| 3530 | - } |
|
| 3531 | - } |
|
| 3532 | - |
|
| 3533 | - |
|
| 3534 | - public function trash_cpt_item($post_id) |
|
| 3535 | - { |
|
| 3536 | - } |
|
| 3537 | - |
|
| 3538 | - |
|
| 3539 | - public function delete_cpt_item($post_id) |
|
| 3540 | - { |
|
| 3541 | - } |
|
| 3542 | - |
|
| 3543 | - |
|
| 3544 | - public function restore_cpt_item($post_id) |
|
| 3545 | - { |
|
| 3546 | - } |
|
| 3547 | - |
|
| 3548 | - |
|
| 3549 | - protected function _restore_cpt_item($post_id, $revision_id) |
|
| 3550 | - { |
|
| 3551 | - } |
|
| 3552 | - |
|
| 3553 | - |
|
| 3554 | - public function attendee_editor_metaboxes() |
|
| 3555 | - { |
|
| 3556 | - $this->verify_cpt_object(); |
|
| 3557 | - remove_meta_box( |
|
| 3558 | - 'postexcerpt', |
|
| 3559 | - esc_html__('Excerpt', 'event_espresso'), |
|
| 3560 | - 'post_excerpt_meta_box', |
|
| 3561 | - $this->_cpt_routes[$this->_req_action], |
|
| 3562 | - 'normal', |
|
| 3563 | - 'core' |
|
| 3564 | - ); |
|
| 3565 | - remove_meta_box('commentstatusdiv', $this->_cpt_routes[$this->_req_action], 'normal', 'core'); |
|
| 3566 | - if (post_type_supports('espresso_attendees', 'excerpt')) { |
|
| 3567 | - add_meta_box( |
|
| 3568 | - 'postexcerpt', |
|
| 3569 | - esc_html__('Short Biography', 'event_espresso'), |
|
| 3570 | - 'post_excerpt_meta_box', |
|
| 3571 | - $this->_cpt_routes[$this->_req_action], |
|
| 3572 | - 'normal' |
|
| 3573 | - ); |
|
| 3574 | - } |
|
| 3575 | - if (post_type_supports('espresso_attendees', 'comments')) { |
|
| 3576 | - add_meta_box( |
|
| 3577 | - 'commentsdiv', |
|
| 3578 | - esc_html__('Notes on the Contact', 'event_espresso'), |
|
| 3579 | - 'post_comment_meta_box', |
|
| 3580 | - $this->_cpt_routes[$this->_req_action], |
|
| 3581 | - 'normal', |
|
| 3582 | - 'core' |
|
| 3583 | - ); |
|
| 3584 | - } |
|
| 3585 | - add_meta_box( |
|
| 3586 | - 'attendee_contact_info', |
|
| 3587 | - esc_html__('Contact Info', 'event_espresso'), |
|
| 3588 | - array($this, 'attendee_contact_info'), |
|
| 3589 | - $this->_cpt_routes[$this->_req_action], |
|
| 3590 | - 'side', |
|
| 3591 | - 'core' |
|
| 3592 | - ); |
|
| 3593 | - add_meta_box( |
|
| 3594 | - 'attendee_details_address', |
|
| 3595 | - esc_html__('Address Details', 'event_espresso'), |
|
| 3596 | - array($this, 'attendee_address_details'), |
|
| 3597 | - $this->_cpt_routes[$this->_req_action], |
|
| 3598 | - 'normal', |
|
| 3599 | - 'core' |
|
| 3600 | - ); |
|
| 3601 | - add_meta_box( |
|
| 3602 | - 'attendee_registrations', |
|
| 3603 | - esc_html__('Registrations for this Contact', 'event_espresso'), |
|
| 3604 | - array($this, 'attendee_registrations_meta_box'), |
|
| 3605 | - $this->_cpt_routes[$this->_req_action], |
|
| 3606 | - 'normal', |
|
| 3607 | - 'high' |
|
| 3608 | - ); |
|
| 3609 | - } |
|
| 3610 | - |
|
| 3611 | - |
|
| 3612 | - /** |
|
| 3613 | - * Metabox for attendee contact info |
|
| 3614 | - * |
|
| 3615 | - * @param WP_Post $post wp post object |
|
| 3616 | - * @return string attendee contact info ( and form ) |
|
| 3617 | - * @throws EE_Error |
|
| 3618 | - * @throws InvalidArgumentException |
|
| 3619 | - * @throws InvalidDataTypeException |
|
| 3620 | - * @throws InvalidInterfaceException |
|
| 3621 | - * @throws LogicException |
|
| 3622 | - * @throws DomainException |
|
| 3623 | - */ |
|
| 3624 | - public function attendee_contact_info($post) |
|
| 3625 | - { |
|
| 3626 | - //get attendee object ( should already have it ) |
|
| 3627 | - $form = $this->getAttendeeContactDetailsMetaboxFormHandler($this->_cpt_model_obj); |
|
| 3628 | - $form->enqueueStylesAndScripts(); |
|
| 3629 | - echo $form->display(); |
|
| 3630 | - } |
|
| 3631 | - |
|
| 3632 | - |
|
| 3633 | - /** |
|
| 3634 | - * Return form handler for the contact details metabox |
|
| 3635 | - * |
|
| 3636 | - * @param EE_Attendee $attendee |
|
| 3637 | - * @return AttendeeContactDetailsMetaboxFormHandler |
|
| 3638 | - * @throws DomainException |
|
| 3639 | - * @throws InvalidArgumentException |
|
| 3640 | - * @throws InvalidDataTypeException |
|
| 3641 | - * @throws InvalidInterfaceException |
|
| 3642 | - */ |
|
| 3643 | - protected function getAttendeeContactDetailsMetaboxFormHandler(EE_Attendee $attendee) |
|
| 3644 | - { |
|
| 3645 | - return new AttendeeContactDetailsMetaboxFormHandler($attendee, EE_Registry::instance()); |
|
| 3646 | - } |
|
| 3647 | - |
|
| 3648 | - |
|
| 3649 | - /** |
|
| 3650 | - * Metabox for attendee details |
|
| 3651 | - * |
|
| 3652 | - * @param WP_Post $post wp post object |
|
| 3653 | - * @throws DomainException |
|
| 3654 | - */ |
|
| 3655 | - public function attendee_address_details($post) |
|
| 3656 | - { |
|
| 3657 | - //get attendee object (should already have it) |
|
| 3658 | - $this->_template_args['attendee'] = $this->_cpt_model_obj; |
|
| 3659 | - $this->_template_args['state_html'] = EEH_Form_Fields::generate_form_input( |
|
| 3660 | - new EE_Question_Form_Input( |
|
| 3661 | - EE_Question::new_instance( |
|
| 3662 | - array( |
|
| 3663 | - 'QST_ID' => 0, |
|
| 3664 | - 'QST_display_text' => esc_html__('State/Province', 'event_espresso'), |
|
| 3665 | - 'QST_system' => 'admin-state', |
|
| 3666 | - ) |
|
| 3667 | - ), |
|
| 3668 | - EE_Answer::new_instance( |
|
| 3669 | - array( |
|
| 3670 | - 'ANS_ID' => 0, |
|
| 3671 | - 'ANS_value' => $this->_cpt_model_obj->state_ID(), |
|
| 3672 | - ) |
|
| 3673 | - ), |
|
| 3674 | - array( |
|
| 3675 | - 'input_id' => 'STA_ID', |
|
| 3676 | - 'input_name' => 'STA_ID', |
|
| 3677 | - 'input_prefix' => '', |
|
| 3678 | - 'append_qstn_id' => false, |
|
| 3679 | - ) |
|
| 3680 | - ) |
|
| 3681 | - ); |
|
| 3682 | - $this->_template_args['country_html'] = EEH_Form_Fields::generate_form_input( |
|
| 3683 | - new EE_Question_Form_Input( |
|
| 3684 | - EE_Question::new_instance( |
|
| 3685 | - array( |
|
| 3686 | - 'QST_ID' => 0, |
|
| 3687 | - 'QST_display_text' => esc_html__('Country', 'event_espresso'), |
|
| 3688 | - 'QST_system' => 'admin-country', |
|
| 3689 | - ) |
|
| 3690 | - ), |
|
| 3691 | - EE_Answer::new_instance( |
|
| 3692 | - array( |
|
| 3693 | - 'ANS_ID' => 0, |
|
| 3694 | - 'ANS_value' => $this->_cpt_model_obj->country_ID(), |
|
| 3695 | - ) |
|
| 3696 | - ), |
|
| 3697 | - array( |
|
| 3698 | - 'input_id' => 'CNT_ISO', |
|
| 3699 | - 'input_name' => 'CNT_ISO', |
|
| 3700 | - 'input_prefix' => '', |
|
| 3701 | - 'append_qstn_id' => false, |
|
| 3702 | - ) |
|
| 3703 | - ) |
|
| 3704 | - ); |
|
| 3705 | - $template = |
|
| 3706 | - REG_TEMPLATE_PATH . 'attendee_address_details_metabox_content.template.php'; |
|
| 3707 | - EEH_Template::display_template($template, $this->_template_args); |
|
| 3708 | - } |
|
| 3709 | - |
|
| 3710 | - |
|
| 3711 | - /** |
|
| 3712 | - * _attendee_details |
|
| 3713 | - * |
|
| 3714 | - * @access protected |
|
| 3715 | - * @param $post |
|
| 3716 | - * @return void |
|
| 3717 | - * @throws DomainException |
|
| 3718 | - * @throws EE_Error |
|
| 3719 | - */ |
|
| 3720 | - public function attendee_registrations_meta_box($post) |
|
| 3721 | - { |
|
| 3722 | - $this->_template_args['attendee'] = $this->_cpt_model_obj; |
|
| 3723 | - $this->_template_args['registrations'] = $this->_cpt_model_obj->get_many_related('Registration'); |
|
| 3724 | - $template = |
|
| 3725 | - REG_TEMPLATE_PATH . 'attendee_registrations_main_meta_box.template.php'; |
|
| 3726 | - EEH_Template::display_template($template, $this->_template_args); |
|
| 3727 | - } |
|
| 3728 | - |
|
| 3729 | - |
|
| 3730 | - /** |
|
| 3731 | - * add in the form fields for the attendee edit |
|
| 3732 | - * |
|
| 3733 | - * @param WP_Post $post wp post object |
|
| 3734 | - * @return string html for new form. |
|
| 3735 | - * @throws DomainException |
|
| 3736 | - */ |
|
| 3737 | - public function after_title_form_fields($post) |
|
| 3738 | - { |
|
| 3739 | - if ($post->post_type == 'espresso_attendees') { |
|
| 3740 | - $template = REG_TEMPLATE_PATH . 'attendee_details_after_title_form_fields.template.php'; |
|
| 3741 | - $template_args['attendee'] = $this->_cpt_model_obj; |
|
| 3742 | - EEH_Template::display_template($template, $template_args); |
|
| 3743 | - } |
|
| 3744 | - } |
|
| 3745 | - |
|
| 3746 | - |
|
| 3747 | - /** |
|
| 3748 | - * _trash_or_restore_attendee |
|
| 3749 | - * |
|
| 3750 | - * @param boolean $trash - whether to move item to trash (TRUE) or restore it (FALSE) |
|
| 3751 | - * @return void |
|
| 3752 | - * @throws EE_Error |
|
| 3753 | - * @throws InvalidArgumentException |
|
| 3754 | - * @throws InvalidDataTypeException |
|
| 3755 | - * @throws InvalidInterfaceException |
|
| 3756 | - * @access protected |
|
| 3757 | - */ |
|
| 3758 | - protected function _trash_or_restore_attendees($trash = true) |
|
| 3759 | - { |
|
| 3760 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 3761 | - $ATT_MDL = EEM_Attendee::instance(); |
|
| 3762 | - $success = 1; |
|
| 3763 | - //Checkboxes |
|
| 3764 | - if ( ! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 3765 | - // if array has more than one element than success message should be plural |
|
| 3766 | - $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1; |
|
| 3767 | - // cycle thru checkboxes |
|
| 3768 | - while (list($ATT_ID, $value) = each($this->_req_data['checkbox'])) { |
|
| 3769 | - $updated = $trash ? $ATT_MDL->update_by_ID(array('status' => 'trash'), $ATT_ID) |
|
| 3770 | - : $ATT_MDL->update_by_ID(array('status' => 'publish'), $ATT_ID); |
|
| 3771 | - if ( ! $updated) { |
|
| 3772 | - $success = 0; |
|
| 3773 | - } |
|
| 3774 | - } |
|
| 3775 | - } else { |
|
| 3776 | - // grab single id and delete |
|
| 3777 | - $ATT_ID = absint($this->_req_data['ATT_ID']); |
|
| 3778 | - //get attendee |
|
| 3779 | - $att = $ATT_MDL->get_one_by_ID($ATT_ID); |
|
| 3780 | - $updated = $trash ? $att->set_status('trash') : $att->set_status('publish'); |
|
| 3781 | - $updated = $att->save(); |
|
| 3782 | - if ( ! $updated) { |
|
| 3783 | - $success = 0; |
|
| 3784 | - } |
|
| 3785 | - } |
|
| 3786 | - $what = $success > 1 |
|
| 3787 | - ? esc_html__('Contacts', 'event_espresso') |
|
| 3788 | - : esc_html__('Contact', 'event_espresso'); |
|
| 3789 | - $action_desc = $trash |
|
| 3790 | - ? esc_html__('moved to the trash', 'event_espresso') |
|
| 3791 | - : esc_html__('restored', 'event_espresso'); |
|
| 3792 | - $this->_redirect_after_action($success, $what, $action_desc, array('action' => 'contact_list')); |
|
| 3793 | - } |
|
| 2958 | + } |
|
| 2959 | + $template_args = array( |
|
| 2960 | + 'title' => '', |
|
| 2961 | + 'content' => '', |
|
| 2962 | + 'step_button_text' => '', |
|
| 2963 | + 'show_notification_toggle' => false, |
|
| 2964 | + ); |
|
| 2965 | + //to indicate we're processing a new registration |
|
| 2966 | + $hidden_fields = array( |
|
| 2967 | + 'processing_registration' => array( |
|
| 2968 | + 'type' => 'hidden', |
|
| 2969 | + 'value' => 0, |
|
| 2970 | + ), |
|
| 2971 | + 'event_id' => array( |
|
| 2972 | + 'type' => 'hidden', |
|
| 2973 | + 'value' => $this->_reg_event->ID(), |
|
| 2974 | + ), |
|
| 2975 | + ); |
|
| 2976 | + //if the cart is empty then we know we're at step one so we'll display ticket selector |
|
| 2977 | + $cart = EE_Registry::instance()->SSN->cart(); |
|
| 2978 | + $step = ! $cart instanceof EE_Cart ? 'ticket' : 'questions'; |
|
| 2979 | + switch ($step) { |
|
| 2980 | + case 'ticket' : |
|
| 2981 | + $hidden_fields['processing_registration']['value'] = 1; |
|
| 2982 | + $template_args['title'] = esc_html__( |
|
| 2983 | + 'Step One: Select the Ticket for this registration', |
|
| 2984 | + 'event_espresso' |
|
| 2985 | + ); |
|
| 2986 | + $template_args['content'] = |
|
| 2987 | + EED_Ticket_Selector::instance()->display_ticket_selector($this->_reg_event); |
|
| 2988 | + $template_args['step_button_text'] = esc_html__( |
|
| 2989 | + 'Add Tickets and Continue to Registrant Details', |
|
| 2990 | + 'event_espresso' |
|
| 2991 | + ); |
|
| 2992 | + $template_args['show_notification_toggle'] = false; |
|
| 2993 | + break; |
|
| 2994 | + case 'questions' : |
|
| 2995 | + $hidden_fields['processing_registration']['value'] = 2; |
|
| 2996 | + $template_args['title'] = esc_html__( |
|
| 2997 | + 'Step Two: Add Registrant Details for this Registration', |
|
| 2998 | + 'event_espresso' |
|
| 2999 | + ); |
|
| 3000 | + //in theory we should be able to run EED_SPCO at this point because the cart should have been setup |
|
| 3001 | + // properly by the first process_reg_step run. |
|
| 3002 | + $template_args['content'] = |
|
| 3003 | + EED_Single_Page_Checkout::registration_checkout_for_admin(); |
|
| 3004 | + $template_args['step_button_text'] = esc_html__( |
|
| 3005 | + 'Save Registration and Continue to Details', |
|
| 3006 | + 'event_espresso' |
|
| 3007 | + ); |
|
| 3008 | + $template_args['show_notification_toggle'] = true; |
|
| 3009 | + break; |
|
| 3010 | + } |
|
| 3011 | + //we come back to the process_registration_step route. |
|
| 3012 | + $this->_set_add_edit_form_tags('process_reg_step', $hidden_fields); |
|
| 3013 | + return EEH_Template::display_template( |
|
| 3014 | + REG_TEMPLATE_PATH . 'reg_admin_register_new_attendee_step_content.template.php', |
|
| 3015 | + $template_args, |
|
| 3016 | + true |
|
| 3017 | + ); |
|
| 3018 | + } |
|
| 3019 | + |
|
| 3020 | + |
|
| 3021 | + /** |
|
| 3022 | + * set_reg_event |
|
| 3023 | + * |
|
| 3024 | + * @access private |
|
| 3025 | + * @return bool |
|
| 3026 | + * @throws EE_Error |
|
| 3027 | + * @throws InvalidArgumentException |
|
| 3028 | + * @throws InvalidDataTypeException |
|
| 3029 | + * @throws InvalidInterfaceException |
|
| 3030 | + */ |
|
| 3031 | + private function _set_reg_event() |
|
| 3032 | + { |
|
| 3033 | + if (is_object($this->_reg_event)) { |
|
| 3034 | + return true; |
|
| 3035 | + } |
|
| 3036 | + $EVT_ID = (! empty($this->_req_data['event_id'])) ? absint($this->_req_data['event_id']) : false; |
|
| 3037 | + if ( ! $EVT_ID) { |
|
| 3038 | + return false; |
|
| 3039 | + } |
|
| 3040 | + $this->_reg_event = EEM_Event::instance()->get_one_by_ID($EVT_ID); |
|
| 3041 | + return true; |
|
| 3042 | + } |
|
| 3043 | + |
|
| 3044 | + |
|
| 3045 | + /** |
|
| 3046 | + * process_reg_step |
|
| 3047 | + * |
|
| 3048 | + * @access public |
|
| 3049 | + * @return string |
|
| 3050 | + * @throws DomainException |
|
| 3051 | + * @throws EE_Error |
|
| 3052 | + * @throws InvalidArgumentException |
|
| 3053 | + * @throws InvalidDataTypeException |
|
| 3054 | + * @throws InvalidInterfaceException |
|
| 3055 | + * @throws ReflectionException |
|
| 3056 | + * @throws RuntimeException |
|
| 3057 | + */ |
|
| 3058 | + public function process_reg_step() |
|
| 3059 | + { |
|
| 3060 | + EE_System::do_not_cache(); |
|
| 3061 | + $this->_set_reg_event(); |
|
| 3062 | + EE_Registry::instance()->REQ->set_espresso_page(true); |
|
| 3063 | + EE_Registry::instance()->REQ->set('uts', time()); |
|
| 3064 | + //what step are we on? |
|
| 3065 | + $cart = EE_Registry::instance()->SSN->cart(); |
|
| 3066 | + $step = ! $cart instanceof EE_Cart ? 'ticket' : 'questions'; |
|
| 3067 | + //if doing ajax then we need to verify the nonce |
|
| 3068 | + if (defined('DOING_AJAX')) { |
|
| 3069 | + $nonce = isset($this->_req_data[$this->_req_nonce]) |
|
| 3070 | + ? sanitize_text_field($this->_req_data[$this->_req_nonce]) : ''; |
|
| 3071 | + $this->_verify_nonce($nonce, $this->_req_nonce); |
|
| 3072 | + } |
|
| 3073 | + switch ($step) { |
|
| 3074 | + case 'ticket' : |
|
| 3075 | + //process ticket selection |
|
| 3076 | + $success = EED_Ticket_Selector::instance()->process_ticket_selections(); |
|
| 3077 | + if ($success) { |
|
| 3078 | + EE_Error::add_success( |
|
| 3079 | + esc_html__( |
|
| 3080 | + 'Tickets Selected. Now complete the registration.', |
|
| 3081 | + 'event_espresso' |
|
| 3082 | + ) |
|
| 3083 | + ); |
|
| 3084 | + } else { |
|
| 3085 | + $query_args['step_error'] = $this->_req_data['step_error'] = true; |
|
| 3086 | + } |
|
| 3087 | + if (defined('DOING_AJAX')) { |
|
| 3088 | + $this->new_registration(); //display next step |
|
| 3089 | + } else { |
|
| 3090 | + $query_args = array( |
|
| 3091 | + 'action' => 'new_registration', |
|
| 3092 | + 'processing_registration' => 1, |
|
| 3093 | + 'event_id' => $this->_reg_event->ID(), |
|
| 3094 | + 'uts' => time(), |
|
| 3095 | + ); |
|
| 3096 | + $this->_redirect_after_action( |
|
| 3097 | + false, |
|
| 3098 | + '', |
|
| 3099 | + '', |
|
| 3100 | + $query_args, |
|
| 3101 | + true |
|
| 3102 | + ); |
|
| 3103 | + } |
|
| 3104 | + break; |
|
| 3105 | + case 'questions' : |
|
| 3106 | + if (! isset( |
|
| 3107 | + $this->_req_data['txn_reg_status_change'], |
|
| 3108 | + $this->_req_data['txn_reg_status_change']['send_notifications']) |
|
| 3109 | + ) { |
|
| 3110 | + add_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications', '__return_false', 15); |
|
| 3111 | + } |
|
| 3112 | + //process registration |
|
| 3113 | + $transaction = EED_Single_Page_Checkout::instance()->process_registration_from_admin(); |
|
| 3114 | + if ($cart instanceof EE_Cart) { |
|
| 3115 | + $grand_total = $cart->get_cart_grand_total(); |
|
| 3116 | + if ($grand_total instanceof EE_Line_Item) { |
|
| 3117 | + $grand_total->save_this_and_descendants_to_txn(); |
|
| 3118 | + } |
|
| 3119 | + } |
|
| 3120 | + if ( ! $transaction instanceof EE_Transaction) { |
|
| 3121 | + $query_args = array( |
|
| 3122 | + 'action' => 'new_registration', |
|
| 3123 | + 'processing_registration' => 2, |
|
| 3124 | + 'event_id' => $this->_reg_event->ID(), |
|
| 3125 | + 'uts' => time(), |
|
| 3126 | + ); |
|
| 3127 | + if (defined('DOING_AJAX')) { |
|
| 3128 | + //display registration form again because there are errors (maybe validation?) |
|
| 3129 | + $this->new_registration(); |
|
| 3130 | + return; |
|
| 3131 | + } else { |
|
| 3132 | + $this->_redirect_after_action( |
|
| 3133 | + false, |
|
| 3134 | + '', |
|
| 3135 | + '', |
|
| 3136 | + $query_args, |
|
| 3137 | + true |
|
| 3138 | + ); |
|
| 3139 | + return; |
|
| 3140 | + } |
|
| 3141 | + } |
|
| 3142 | + // maybe update status, and make sure to save transaction if not done already |
|
| 3143 | + if ( ! $transaction->update_status_based_on_total_paid()) { |
|
| 3144 | + $transaction->save(); |
|
| 3145 | + } |
|
| 3146 | + EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
| 3147 | + $this->_req_data = array(); |
|
| 3148 | + $query_args = array( |
|
| 3149 | + 'action' => 'redirect_to_txn', |
|
| 3150 | + 'TXN_ID' => $transaction->ID(), |
|
| 3151 | + 'EVT_ID' => $this->_reg_event->ID(), |
|
| 3152 | + 'event_name' => urlencode($this->_reg_event->name()), |
|
| 3153 | + 'redirect_from' => 'new_registration', |
|
| 3154 | + ); |
|
| 3155 | + $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 3156 | + break; |
|
| 3157 | + } |
|
| 3158 | + //what are you looking here for? Should be nothing to do at this point. |
|
| 3159 | + } |
|
| 3160 | + |
|
| 3161 | + |
|
| 3162 | + /** |
|
| 3163 | + * redirect_to_txn |
|
| 3164 | + * |
|
| 3165 | + * @access public |
|
| 3166 | + * @return void |
|
| 3167 | + * @throws EE_Error |
|
| 3168 | + * @throws InvalidArgumentException |
|
| 3169 | + * @throws InvalidDataTypeException |
|
| 3170 | + * @throws InvalidInterfaceException |
|
| 3171 | + */ |
|
| 3172 | + public function redirect_to_txn() |
|
| 3173 | + { |
|
| 3174 | + EE_System::do_not_cache(); |
|
| 3175 | + EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
| 3176 | + $query_args = array( |
|
| 3177 | + 'action' => 'view_transaction', |
|
| 3178 | + 'TXN_ID' => isset($this->_req_data['TXN_ID']) ? absint($this->_req_data['TXN_ID']) : 0, |
|
| 3179 | + 'page' => 'espresso_transactions', |
|
| 3180 | + ); |
|
| 3181 | + if (isset($this->_req_data['EVT_ID'], $this->_req_data['redirect_from'])) { |
|
| 3182 | + $query_args['EVT_ID'] = $this->_req_data['EVT_ID']; |
|
| 3183 | + $query_args['event_name'] = urlencode($this->_req_data['event_name']); |
|
| 3184 | + $query_args['redirect_from'] = $this->_req_data['redirect_from']; |
|
| 3185 | + } |
|
| 3186 | + EE_Error::add_success( |
|
| 3187 | + esc_html__( |
|
| 3188 | + 'Registration Created. Please review the transaction and add any payments as necessary', |
|
| 3189 | + 'event_espresso' |
|
| 3190 | + ) |
|
| 3191 | + ); |
|
| 3192 | + $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 3193 | + } |
|
| 3194 | + |
|
| 3195 | + |
|
| 3196 | + /** |
|
| 3197 | + * generates HTML for the Attendee Contact List |
|
| 3198 | + * |
|
| 3199 | + * @access protected |
|
| 3200 | + * @return void |
|
| 3201 | + */ |
|
| 3202 | + protected function _attendee_contact_list_table() |
|
| 3203 | + { |
|
| 3204 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 3205 | + $this->_search_btn_label = esc_html__('Contacts', 'event_espresso'); |
|
| 3206 | + $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 3207 | + } |
|
| 3208 | + |
|
| 3209 | + |
|
| 3210 | + /** |
|
| 3211 | + * get_attendees |
|
| 3212 | + * |
|
| 3213 | + * @param $per_page |
|
| 3214 | + * @param bool $count whether to return count or data. |
|
| 3215 | + * @param bool $trash |
|
| 3216 | + * @return array |
|
| 3217 | + * @throws EE_Error |
|
| 3218 | + * @throws InvalidArgumentException |
|
| 3219 | + * @throws InvalidDataTypeException |
|
| 3220 | + * @throws InvalidInterfaceException |
|
| 3221 | + * @access public |
|
| 3222 | + */ |
|
| 3223 | + public function get_attendees($per_page, $count = false, $trash = false) |
|
| 3224 | + { |
|
| 3225 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 3226 | + require_once(REG_ADMIN . 'EE_Attendee_Contact_List_Table.class.php'); |
|
| 3227 | + $ATT_MDL = EEM_Attendee::instance(); |
|
| 3228 | + $this->_req_data['orderby'] = ! empty($this->_req_data['orderby']) ? $this->_req_data['orderby'] : ''; |
|
| 3229 | + switch ($this->_req_data['orderby']) { |
|
| 3230 | + case 'ATT_ID': |
|
| 3231 | + $orderby = 'ATT_ID'; |
|
| 3232 | + break; |
|
| 3233 | + case 'ATT_fname': |
|
| 3234 | + $orderby = 'ATT_fname'; |
|
| 3235 | + break; |
|
| 3236 | + case 'ATT_email': |
|
| 3237 | + $orderby = 'ATT_email'; |
|
| 3238 | + break; |
|
| 3239 | + case 'ATT_city': |
|
| 3240 | + $orderby = 'ATT_city'; |
|
| 3241 | + break; |
|
| 3242 | + case 'STA_ID': |
|
| 3243 | + $orderby = 'STA_ID'; |
|
| 3244 | + break; |
|
| 3245 | + case 'CNT_ID': |
|
| 3246 | + $orderby = 'CNT_ID'; |
|
| 3247 | + break; |
|
| 3248 | + case 'Registration_Count': |
|
| 3249 | + $orderby = 'Registration_Count'; |
|
| 3250 | + break; |
|
| 3251 | + default: |
|
| 3252 | + $orderby = 'ATT_lname'; |
|
| 3253 | + } |
|
| 3254 | + $sort = (isset($this->_req_data['order']) && ! empty($this->_req_data['order'])) |
|
| 3255 | + ? $this->_req_data['order'] |
|
| 3256 | + : 'ASC'; |
|
| 3257 | + $current_page = isset($this->_req_data['paged']) && ! empty($this->_req_data['paged']) |
|
| 3258 | + ? $this->_req_data['paged'] |
|
| 3259 | + : 1; |
|
| 3260 | + $per_page = isset($per_page) && ! empty($per_page) ? $per_page : 10; |
|
| 3261 | + $per_page = isset($this->_req_data['perpage']) && ! empty($this->_req_data['perpage']) |
|
| 3262 | + ? $this->_req_data['perpage'] |
|
| 3263 | + : $per_page; |
|
| 3264 | + $_where = array(); |
|
| 3265 | + if ( ! empty($this->_req_data['s'])) { |
|
| 3266 | + $sstr = '%' . $this->_req_data['s'] . '%'; |
|
| 3267 | + $_where['OR'] = array( |
|
| 3268 | + 'Registration.Event.EVT_name' => array('LIKE', $sstr), |
|
| 3269 | + 'Registration.Event.EVT_desc' => array('LIKE', $sstr), |
|
| 3270 | + 'Registration.Event.EVT_short_desc' => array('LIKE', $sstr), |
|
| 3271 | + 'ATT_fname' => array('LIKE', $sstr), |
|
| 3272 | + 'ATT_lname' => array('LIKE', $sstr), |
|
| 3273 | + 'ATT_short_bio' => array('LIKE', $sstr), |
|
| 3274 | + 'ATT_email' => array('LIKE', $sstr), |
|
| 3275 | + 'ATT_address' => array('LIKE', $sstr), |
|
| 3276 | + 'ATT_address2' => array('LIKE', $sstr), |
|
| 3277 | + 'ATT_city' => array('LIKE', $sstr), |
|
| 3278 | + 'Country.CNT_name' => array('LIKE', $sstr), |
|
| 3279 | + 'State.STA_name' => array('LIKE', $sstr), |
|
| 3280 | + 'ATT_phone' => array('LIKE', $sstr), |
|
| 3281 | + 'Registration.REG_final_price' => array('LIKE', $sstr), |
|
| 3282 | + 'Registration.REG_code' => array('LIKE', $sstr), |
|
| 3283 | + 'Registration.REG_group_size' => array('LIKE', $sstr), |
|
| 3284 | + ); |
|
| 3285 | + } |
|
| 3286 | + $offset = ($current_page - 1) * $per_page; |
|
| 3287 | + $limit = $count ? null : array($offset, $per_page); |
|
| 3288 | + $query_args = array( |
|
| 3289 | + $_where, |
|
| 3290 | + 'extra_selects' => array('Registration_Count' => array('Registration.REG_ID', 'count', '%d')), |
|
| 3291 | + 'limit' => $limit |
|
| 3292 | + ); |
|
| 3293 | + if (! $count) { |
|
| 3294 | + $query_args['order_by'] = array($orderby => $sort); |
|
| 3295 | + } |
|
| 3296 | + if ($trash) { |
|
| 3297 | + $query_args[0]['status'] = array('!=', 'publish'); |
|
| 3298 | + $all_attendees = $count |
|
| 3299 | + ? $ATT_MDL->count($query_args, 'ATT_ID', true) |
|
| 3300 | + : $ATT_MDL->get_all($query_args); |
|
| 3301 | + } else { |
|
| 3302 | + $query_args[0]['status'] = array('IN', array('publish')); |
|
| 3303 | + $all_attendees = $count |
|
| 3304 | + ? $ATT_MDL->count($query_args, 'ATT_ID', true) |
|
| 3305 | + : $ATT_MDL->get_all($query_args); |
|
| 3306 | + } |
|
| 3307 | + return $all_attendees; |
|
| 3308 | + } |
|
| 3309 | + |
|
| 3310 | + |
|
| 3311 | + /** |
|
| 3312 | + * This is just taking care of resending the registration confirmation |
|
| 3313 | + * |
|
| 3314 | + * @access protected |
|
| 3315 | + * @return void |
|
| 3316 | + */ |
|
| 3317 | + protected function _resend_registration() |
|
| 3318 | + { |
|
| 3319 | + $this->_process_resend_registration(); |
|
| 3320 | + $query_args = isset($this->_req_data['redirect_to']) |
|
| 3321 | + ? array('action' => $this->_req_data['redirect_to'], '_REG_ID' => $this->_req_data['_REG_ID']) |
|
| 3322 | + : array('action' => 'default'); |
|
| 3323 | + $this->_redirect_after_action(false, '', '', $query_args, true); |
|
| 3324 | + } |
|
| 3325 | + |
|
| 3326 | + /** |
|
| 3327 | + * Creates a registration report, but accepts the name of a method to use for preparing the query parameters |
|
| 3328 | + * to use when selecting registrations |
|
| 3329 | + * @param string $method_name_for_getting_query_params the name of the method (on this class) to use for preparing |
|
| 3330 | + * the query parameters from the request |
|
| 3331 | + * @return void ends the request with a redirect or download |
|
| 3332 | + */ |
|
| 3333 | + public function _registrations_report_base( $method_name_for_getting_query_params ) |
|
| 3334 | + { |
|
| 3335 | + if (! defined('EE_USE_OLD_CSV_REPORT_CLASS')) { |
|
| 3336 | + wp_redirect(EE_Admin_Page::add_query_args_and_nonce( |
|
| 3337 | + array( |
|
| 3338 | + 'page' => 'espresso_batch', |
|
| 3339 | + 'batch' => 'file', |
|
| 3340 | + 'EVT_ID' => isset($this->_req_data['EVT_ID']) ? $this->_req_data['EVT_ID'] : null, |
|
| 3341 | + 'filters' => urlencode( |
|
| 3342 | + serialize( |
|
| 3343 | + call_user_func( |
|
| 3344 | + array( $this, $method_name_for_getting_query_params ), |
|
| 3345 | + EEH_Array::is_set( |
|
| 3346 | + $this->_req_data, |
|
| 3347 | + 'filters', |
|
| 3348 | + array() |
|
| 3349 | + ) |
|
| 3350 | + ) |
|
| 3351 | + ) |
|
| 3352 | + ), |
|
| 3353 | + 'use_filters' => EEH_Array::is_set($this->_req_data, 'use_filters', false), |
|
| 3354 | + 'job_handler' => urlencode('EventEspressoBatchRequest\JobHandlers\RegistrationsReport'), |
|
| 3355 | + 'return_url' => urlencode($this->_req_data['return_url']), |
|
| 3356 | + ))); |
|
| 3357 | + } else { |
|
| 3358 | + $new_request_args = array( |
|
| 3359 | + 'export' => 'report', |
|
| 3360 | + 'action' => 'registrations_report_for_event', |
|
| 3361 | + 'EVT_ID' => isset($this->_req_data['EVT_ID']) ? $this->_req_data['EVT_ID'] : null, |
|
| 3362 | + ); |
|
| 3363 | + $this->_req_data = array_merge($this->_req_data, $new_request_args); |
|
| 3364 | + if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 3365 | + require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 3366 | + $EE_Export = EE_Export::instance($this->_req_data); |
|
| 3367 | + $EE_Export->export(); |
|
| 3368 | + } |
|
| 3369 | + } |
|
| 3370 | + } |
|
| 3371 | + |
|
| 3372 | + |
|
| 3373 | + |
|
| 3374 | + /** |
|
| 3375 | + * Creates a registration report using only query parameters in the request |
|
| 3376 | + * @return void |
|
| 3377 | + */ |
|
| 3378 | + public function _registrations_report() |
|
| 3379 | + { |
|
| 3380 | + $this->_registrations_report_base('_get_registration_query_parameters'); |
|
| 3381 | + } |
|
| 3382 | + |
|
| 3383 | + |
|
| 3384 | + public function _contact_list_export() |
|
| 3385 | + { |
|
| 3386 | + if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 3387 | + require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 3388 | + $EE_Export = EE_Export::instance($this->_req_data); |
|
| 3389 | + $EE_Export->export_attendees(); |
|
| 3390 | + } |
|
| 3391 | + } |
|
| 3392 | + |
|
| 3393 | + |
|
| 3394 | + public function _contact_list_report() |
|
| 3395 | + { |
|
| 3396 | + if ( ! defined('EE_USE_OLD_CSV_REPORT_CLASS')) { |
|
| 3397 | + wp_redirect(EE_Admin_Page::add_query_args_and_nonce(array( |
|
| 3398 | + 'page' => 'espresso_batch', |
|
| 3399 | + 'batch' => 'file', |
|
| 3400 | + 'job_handler' => urlencode('EventEspressoBatchRequest\JobHandlers\AttendeesReport'), |
|
| 3401 | + 'return_url' => urlencode($this->_req_data['return_url']), |
|
| 3402 | + ))); |
|
| 3403 | + } else { |
|
| 3404 | + if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 3405 | + require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 3406 | + $EE_Export = EE_Export::instance($this->_req_data); |
|
| 3407 | + $EE_Export->report_attendees(); |
|
| 3408 | + } |
|
| 3409 | + } |
|
| 3410 | + } |
|
| 3411 | + |
|
| 3412 | + |
|
| 3413 | + |
|
| 3414 | + |
|
| 3415 | + |
|
| 3416 | + /*************************************** ATTENDEE DETAILS ***************************************/ |
|
| 3417 | + /** |
|
| 3418 | + * This duplicates the attendee object for the given incoming registration id and attendee_id. |
|
| 3419 | + * |
|
| 3420 | + * @return void |
|
| 3421 | + * @throws EE_Error |
|
| 3422 | + * @throws InvalidArgumentException |
|
| 3423 | + * @throws InvalidDataTypeException |
|
| 3424 | + * @throws InvalidInterfaceException |
|
| 3425 | + */ |
|
| 3426 | + protected function _duplicate_attendee() |
|
| 3427 | + { |
|
| 3428 | + $action = ! empty($this->_req_data['return']) ? $this->_req_data['return'] : 'default'; |
|
| 3429 | + //verify we have necessary info |
|
| 3430 | + if (empty($this->_req_data['_REG_ID'])) { |
|
| 3431 | + EE_Error::add_error( |
|
| 3432 | + esc_html__( |
|
| 3433 | + 'Unable to create the contact for the registration because the required parameters are not present (_REG_ID )', |
|
| 3434 | + 'event_espresso' |
|
| 3435 | + ), __FILE__, __LINE__, __FUNCTION__ |
|
| 3436 | + ); |
|
| 3437 | + $query_args = array('action' => $action); |
|
| 3438 | + $this->_redirect_after_action('', '', '', $query_args, true); |
|
| 3439 | + } |
|
| 3440 | + //okay necessary deets present... let's dupe the incoming attendee and attach to incoming registration. |
|
| 3441 | + $registration = EEM_Registration::instance()->get_one_by_ID($this->_req_data['_REG_ID']); |
|
| 3442 | + $attendee = $registration->attendee(); |
|
| 3443 | + //remove relation of existing attendee on registration |
|
| 3444 | + $registration->_remove_relation_to($attendee, 'Attendee'); |
|
| 3445 | + //new attendee |
|
| 3446 | + $new_attendee = clone $attendee; |
|
| 3447 | + $new_attendee->set('ATT_ID', 0); |
|
| 3448 | + $new_attendee->save(); |
|
| 3449 | + //add new attendee to reg |
|
| 3450 | + $registration->_add_relation_to($new_attendee, 'Attendee'); |
|
| 3451 | + EE_Error::add_success( |
|
| 3452 | + esc_html__( |
|
| 3453 | + 'New Contact record created. Now make any edits you wish to make for this contact.', |
|
| 3454 | + 'event_espresso' |
|
| 3455 | + ) |
|
| 3456 | + ); |
|
| 3457 | + //redirect to edit page for attendee |
|
| 3458 | + $query_args = array('post' => $new_attendee->ID(), 'action' => 'edit_attendee'); |
|
| 3459 | + $this->_redirect_after_action('', '', '', $query_args, true); |
|
| 3460 | + } |
|
| 3461 | + |
|
| 3462 | + |
|
| 3463 | + /** |
|
| 3464 | + * Callback invoked by parent EE_Admin_CPT class hooked in on `save_post` wp hook. |
|
| 3465 | + * @param int $post_id |
|
| 3466 | + * @param WP_POST $post |
|
| 3467 | + * @throws DomainException |
|
| 3468 | + * @throws EE_Error |
|
| 3469 | + * @throws InvalidArgumentException |
|
| 3470 | + * @throws InvalidDataTypeException |
|
| 3471 | + * @throws InvalidInterfaceException |
|
| 3472 | + * @throws LogicException |
|
| 3473 | + * @throws InvalidFormSubmissionException |
|
| 3474 | + */ |
|
| 3475 | + protected function _insert_update_cpt_item($post_id, $post) |
|
| 3476 | + { |
|
| 3477 | + $success = true; |
|
| 3478 | + $attendee = $post instanceof WP_Post && $post->post_type === 'espresso_attendees' |
|
| 3479 | + ? EEM_Attendee::instance()->get_one_by_ID($post_id) |
|
| 3480 | + : null; |
|
| 3481 | + //for attendee updates |
|
| 3482 | + if ($attendee instanceof EE_Attendee) { |
|
| 3483 | + //note we should only be UPDATING attendees at this point. |
|
| 3484 | + $updated_fields = array( |
|
| 3485 | + 'ATT_fname' => $this->_req_data['ATT_fname'], |
|
| 3486 | + 'ATT_lname' => $this->_req_data['ATT_lname'], |
|
| 3487 | + 'ATT_full_name' => $this->_req_data['ATT_fname'] . ' ' . $this->_req_data['ATT_lname'], |
|
| 3488 | + 'ATT_address' => isset($this->_req_data['ATT_address']) ? $this->_req_data['ATT_address'] : '', |
|
| 3489 | + 'ATT_address2' => isset($this->_req_data['ATT_address2']) ? $this->_req_data['ATT_address2'] : '', |
|
| 3490 | + 'ATT_city' => isset($this->_req_data['ATT_city']) ? $this->_req_data['ATT_city'] : '', |
|
| 3491 | + 'STA_ID' => isset($this->_req_data['STA_ID']) ? $this->_req_data['STA_ID'] : '', |
|
| 3492 | + 'CNT_ISO' => isset($this->_req_data['CNT_ISO']) ? $this->_req_data['CNT_ISO'] : '', |
|
| 3493 | + 'ATT_zip' => isset($this->_req_data['ATT_zip']) ? $this->_req_data['ATT_zip'] : '', |
|
| 3494 | + ); |
|
| 3495 | + foreach ($updated_fields as $field => $value) { |
|
| 3496 | + $attendee->set($field, $value); |
|
| 3497 | + } |
|
| 3498 | + |
|
| 3499 | + //process contact details metabox form handler (which will also save the attendee) |
|
| 3500 | + $contact_details_form = $this->getAttendeeContactDetailsMetaboxFormHandler($attendee); |
|
| 3501 | + $success = $contact_details_form->process($this->_req_data); |
|
| 3502 | + |
|
| 3503 | + $attendee_update_callbacks = apply_filters( |
|
| 3504 | + 'FHEE__Registrations_Admin_Page__insert_update_cpt_item__attendee_update', |
|
| 3505 | + array() |
|
| 3506 | + ); |
|
| 3507 | + foreach ($attendee_update_callbacks as $a_callback) { |
|
| 3508 | + if (false === call_user_func_array($a_callback, array($attendee, $this->_req_data))) { |
|
| 3509 | + throw new EE_Error( |
|
| 3510 | + sprintf( |
|
| 3511 | + esc_html__( |
|
| 3512 | + 'The %s callback given for the "FHEE__Registrations_Admin_Page__insert_update_cpt_item__attendee_update" filter is not a valid callback. Please check the spelling.', |
|
| 3513 | + 'event_espresso' |
|
| 3514 | + ), |
|
| 3515 | + $a_callback |
|
| 3516 | + ) |
|
| 3517 | + ); |
|
| 3518 | + } |
|
| 3519 | + } |
|
| 3520 | + } |
|
| 3521 | + |
|
| 3522 | + if ($success === false) { |
|
| 3523 | + EE_Error::add_error( |
|
| 3524 | + esc_html__( |
|
| 3525 | + 'Something went wrong with updating the meta table data for the registration.', |
|
| 3526 | + 'event_espresso' |
|
| 3527 | + ), |
|
| 3528 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 3529 | + ); |
|
| 3530 | + } |
|
| 3531 | + } |
|
| 3532 | + |
|
| 3533 | + |
|
| 3534 | + public function trash_cpt_item($post_id) |
|
| 3535 | + { |
|
| 3536 | + } |
|
| 3537 | + |
|
| 3538 | + |
|
| 3539 | + public function delete_cpt_item($post_id) |
|
| 3540 | + { |
|
| 3541 | + } |
|
| 3542 | + |
|
| 3543 | + |
|
| 3544 | + public function restore_cpt_item($post_id) |
|
| 3545 | + { |
|
| 3546 | + } |
|
| 3547 | + |
|
| 3548 | + |
|
| 3549 | + protected function _restore_cpt_item($post_id, $revision_id) |
|
| 3550 | + { |
|
| 3551 | + } |
|
| 3552 | + |
|
| 3553 | + |
|
| 3554 | + public function attendee_editor_metaboxes() |
|
| 3555 | + { |
|
| 3556 | + $this->verify_cpt_object(); |
|
| 3557 | + remove_meta_box( |
|
| 3558 | + 'postexcerpt', |
|
| 3559 | + esc_html__('Excerpt', 'event_espresso'), |
|
| 3560 | + 'post_excerpt_meta_box', |
|
| 3561 | + $this->_cpt_routes[$this->_req_action], |
|
| 3562 | + 'normal', |
|
| 3563 | + 'core' |
|
| 3564 | + ); |
|
| 3565 | + remove_meta_box('commentstatusdiv', $this->_cpt_routes[$this->_req_action], 'normal', 'core'); |
|
| 3566 | + if (post_type_supports('espresso_attendees', 'excerpt')) { |
|
| 3567 | + add_meta_box( |
|
| 3568 | + 'postexcerpt', |
|
| 3569 | + esc_html__('Short Biography', 'event_espresso'), |
|
| 3570 | + 'post_excerpt_meta_box', |
|
| 3571 | + $this->_cpt_routes[$this->_req_action], |
|
| 3572 | + 'normal' |
|
| 3573 | + ); |
|
| 3574 | + } |
|
| 3575 | + if (post_type_supports('espresso_attendees', 'comments')) { |
|
| 3576 | + add_meta_box( |
|
| 3577 | + 'commentsdiv', |
|
| 3578 | + esc_html__('Notes on the Contact', 'event_espresso'), |
|
| 3579 | + 'post_comment_meta_box', |
|
| 3580 | + $this->_cpt_routes[$this->_req_action], |
|
| 3581 | + 'normal', |
|
| 3582 | + 'core' |
|
| 3583 | + ); |
|
| 3584 | + } |
|
| 3585 | + add_meta_box( |
|
| 3586 | + 'attendee_contact_info', |
|
| 3587 | + esc_html__('Contact Info', 'event_espresso'), |
|
| 3588 | + array($this, 'attendee_contact_info'), |
|
| 3589 | + $this->_cpt_routes[$this->_req_action], |
|
| 3590 | + 'side', |
|
| 3591 | + 'core' |
|
| 3592 | + ); |
|
| 3593 | + add_meta_box( |
|
| 3594 | + 'attendee_details_address', |
|
| 3595 | + esc_html__('Address Details', 'event_espresso'), |
|
| 3596 | + array($this, 'attendee_address_details'), |
|
| 3597 | + $this->_cpt_routes[$this->_req_action], |
|
| 3598 | + 'normal', |
|
| 3599 | + 'core' |
|
| 3600 | + ); |
|
| 3601 | + add_meta_box( |
|
| 3602 | + 'attendee_registrations', |
|
| 3603 | + esc_html__('Registrations for this Contact', 'event_espresso'), |
|
| 3604 | + array($this, 'attendee_registrations_meta_box'), |
|
| 3605 | + $this->_cpt_routes[$this->_req_action], |
|
| 3606 | + 'normal', |
|
| 3607 | + 'high' |
|
| 3608 | + ); |
|
| 3609 | + } |
|
| 3610 | + |
|
| 3611 | + |
|
| 3612 | + /** |
|
| 3613 | + * Metabox for attendee contact info |
|
| 3614 | + * |
|
| 3615 | + * @param WP_Post $post wp post object |
|
| 3616 | + * @return string attendee contact info ( and form ) |
|
| 3617 | + * @throws EE_Error |
|
| 3618 | + * @throws InvalidArgumentException |
|
| 3619 | + * @throws InvalidDataTypeException |
|
| 3620 | + * @throws InvalidInterfaceException |
|
| 3621 | + * @throws LogicException |
|
| 3622 | + * @throws DomainException |
|
| 3623 | + */ |
|
| 3624 | + public function attendee_contact_info($post) |
|
| 3625 | + { |
|
| 3626 | + //get attendee object ( should already have it ) |
|
| 3627 | + $form = $this->getAttendeeContactDetailsMetaboxFormHandler($this->_cpt_model_obj); |
|
| 3628 | + $form->enqueueStylesAndScripts(); |
|
| 3629 | + echo $form->display(); |
|
| 3630 | + } |
|
| 3631 | + |
|
| 3632 | + |
|
| 3633 | + /** |
|
| 3634 | + * Return form handler for the contact details metabox |
|
| 3635 | + * |
|
| 3636 | + * @param EE_Attendee $attendee |
|
| 3637 | + * @return AttendeeContactDetailsMetaboxFormHandler |
|
| 3638 | + * @throws DomainException |
|
| 3639 | + * @throws InvalidArgumentException |
|
| 3640 | + * @throws InvalidDataTypeException |
|
| 3641 | + * @throws InvalidInterfaceException |
|
| 3642 | + */ |
|
| 3643 | + protected function getAttendeeContactDetailsMetaboxFormHandler(EE_Attendee $attendee) |
|
| 3644 | + { |
|
| 3645 | + return new AttendeeContactDetailsMetaboxFormHandler($attendee, EE_Registry::instance()); |
|
| 3646 | + } |
|
| 3647 | + |
|
| 3648 | + |
|
| 3649 | + /** |
|
| 3650 | + * Metabox for attendee details |
|
| 3651 | + * |
|
| 3652 | + * @param WP_Post $post wp post object |
|
| 3653 | + * @throws DomainException |
|
| 3654 | + */ |
|
| 3655 | + public function attendee_address_details($post) |
|
| 3656 | + { |
|
| 3657 | + //get attendee object (should already have it) |
|
| 3658 | + $this->_template_args['attendee'] = $this->_cpt_model_obj; |
|
| 3659 | + $this->_template_args['state_html'] = EEH_Form_Fields::generate_form_input( |
|
| 3660 | + new EE_Question_Form_Input( |
|
| 3661 | + EE_Question::new_instance( |
|
| 3662 | + array( |
|
| 3663 | + 'QST_ID' => 0, |
|
| 3664 | + 'QST_display_text' => esc_html__('State/Province', 'event_espresso'), |
|
| 3665 | + 'QST_system' => 'admin-state', |
|
| 3666 | + ) |
|
| 3667 | + ), |
|
| 3668 | + EE_Answer::new_instance( |
|
| 3669 | + array( |
|
| 3670 | + 'ANS_ID' => 0, |
|
| 3671 | + 'ANS_value' => $this->_cpt_model_obj->state_ID(), |
|
| 3672 | + ) |
|
| 3673 | + ), |
|
| 3674 | + array( |
|
| 3675 | + 'input_id' => 'STA_ID', |
|
| 3676 | + 'input_name' => 'STA_ID', |
|
| 3677 | + 'input_prefix' => '', |
|
| 3678 | + 'append_qstn_id' => false, |
|
| 3679 | + ) |
|
| 3680 | + ) |
|
| 3681 | + ); |
|
| 3682 | + $this->_template_args['country_html'] = EEH_Form_Fields::generate_form_input( |
|
| 3683 | + new EE_Question_Form_Input( |
|
| 3684 | + EE_Question::new_instance( |
|
| 3685 | + array( |
|
| 3686 | + 'QST_ID' => 0, |
|
| 3687 | + 'QST_display_text' => esc_html__('Country', 'event_espresso'), |
|
| 3688 | + 'QST_system' => 'admin-country', |
|
| 3689 | + ) |
|
| 3690 | + ), |
|
| 3691 | + EE_Answer::new_instance( |
|
| 3692 | + array( |
|
| 3693 | + 'ANS_ID' => 0, |
|
| 3694 | + 'ANS_value' => $this->_cpt_model_obj->country_ID(), |
|
| 3695 | + ) |
|
| 3696 | + ), |
|
| 3697 | + array( |
|
| 3698 | + 'input_id' => 'CNT_ISO', |
|
| 3699 | + 'input_name' => 'CNT_ISO', |
|
| 3700 | + 'input_prefix' => '', |
|
| 3701 | + 'append_qstn_id' => false, |
|
| 3702 | + ) |
|
| 3703 | + ) |
|
| 3704 | + ); |
|
| 3705 | + $template = |
|
| 3706 | + REG_TEMPLATE_PATH . 'attendee_address_details_metabox_content.template.php'; |
|
| 3707 | + EEH_Template::display_template($template, $this->_template_args); |
|
| 3708 | + } |
|
| 3709 | + |
|
| 3710 | + |
|
| 3711 | + /** |
|
| 3712 | + * _attendee_details |
|
| 3713 | + * |
|
| 3714 | + * @access protected |
|
| 3715 | + * @param $post |
|
| 3716 | + * @return void |
|
| 3717 | + * @throws DomainException |
|
| 3718 | + * @throws EE_Error |
|
| 3719 | + */ |
|
| 3720 | + public function attendee_registrations_meta_box($post) |
|
| 3721 | + { |
|
| 3722 | + $this->_template_args['attendee'] = $this->_cpt_model_obj; |
|
| 3723 | + $this->_template_args['registrations'] = $this->_cpt_model_obj->get_many_related('Registration'); |
|
| 3724 | + $template = |
|
| 3725 | + REG_TEMPLATE_PATH . 'attendee_registrations_main_meta_box.template.php'; |
|
| 3726 | + EEH_Template::display_template($template, $this->_template_args); |
|
| 3727 | + } |
|
| 3728 | + |
|
| 3729 | + |
|
| 3730 | + /** |
|
| 3731 | + * add in the form fields for the attendee edit |
|
| 3732 | + * |
|
| 3733 | + * @param WP_Post $post wp post object |
|
| 3734 | + * @return string html for new form. |
|
| 3735 | + * @throws DomainException |
|
| 3736 | + */ |
|
| 3737 | + public function after_title_form_fields($post) |
|
| 3738 | + { |
|
| 3739 | + if ($post->post_type == 'espresso_attendees') { |
|
| 3740 | + $template = REG_TEMPLATE_PATH . 'attendee_details_after_title_form_fields.template.php'; |
|
| 3741 | + $template_args['attendee'] = $this->_cpt_model_obj; |
|
| 3742 | + EEH_Template::display_template($template, $template_args); |
|
| 3743 | + } |
|
| 3744 | + } |
|
| 3745 | + |
|
| 3746 | + |
|
| 3747 | + /** |
|
| 3748 | + * _trash_or_restore_attendee |
|
| 3749 | + * |
|
| 3750 | + * @param boolean $trash - whether to move item to trash (TRUE) or restore it (FALSE) |
|
| 3751 | + * @return void |
|
| 3752 | + * @throws EE_Error |
|
| 3753 | + * @throws InvalidArgumentException |
|
| 3754 | + * @throws InvalidDataTypeException |
|
| 3755 | + * @throws InvalidInterfaceException |
|
| 3756 | + * @access protected |
|
| 3757 | + */ |
|
| 3758 | + protected function _trash_or_restore_attendees($trash = true) |
|
| 3759 | + { |
|
| 3760 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 3761 | + $ATT_MDL = EEM_Attendee::instance(); |
|
| 3762 | + $success = 1; |
|
| 3763 | + //Checkboxes |
|
| 3764 | + if ( ! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { |
|
| 3765 | + // if array has more than one element than success message should be plural |
|
| 3766 | + $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1; |
|
| 3767 | + // cycle thru checkboxes |
|
| 3768 | + while (list($ATT_ID, $value) = each($this->_req_data['checkbox'])) { |
|
| 3769 | + $updated = $trash ? $ATT_MDL->update_by_ID(array('status' => 'trash'), $ATT_ID) |
|
| 3770 | + : $ATT_MDL->update_by_ID(array('status' => 'publish'), $ATT_ID); |
|
| 3771 | + if ( ! $updated) { |
|
| 3772 | + $success = 0; |
|
| 3773 | + } |
|
| 3774 | + } |
|
| 3775 | + } else { |
|
| 3776 | + // grab single id and delete |
|
| 3777 | + $ATT_ID = absint($this->_req_data['ATT_ID']); |
|
| 3778 | + //get attendee |
|
| 3779 | + $att = $ATT_MDL->get_one_by_ID($ATT_ID); |
|
| 3780 | + $updated = $trash ? $att->set_status('trash') : $att->set_status('publish'); |
|
| 3781 | + $updated = $att->save(); |
|
| 3782 | + if ( ! $updated) { |
|
| 3783 | + $success = 0; |
|
| 3784 | + } |
|
| 3785 | + } |
|
| 3786 | + $what = $success > 1 |
|
| 3787 | + ? esc_html__('Contacts', 'event_espresso') |
|
| 3788 | + : esc_html__('Contact', 'event_espresso'); |
|
| 3789 | + $action_desc = $trash |
|
| 3790 | + ? esc_html__('moved to the trash', 'event_espresso') |
|
| 3791 | + : esc_html__('restored', 'event_espresso'); |
|
| 3792 | + $this->_redirect_after_action($success, $what, $action_desc, array('action' => 'contact_list')); |
|
| 3793 | + } |
|
| 3794 | 3794 | |
| 3795 | 3795 | } |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | // when adding a new registration... |
| 77 | 77 | if (isset($this->_req_data['action']) && $this->_req_data['action'] === 'new_registration') { |
| 78 | 78 | EE_System::do_not_cache(); |
| 79 | - if (! isset($this->_req_data['processing_registration']) |
|
| 79 | + if ( ! isset($this->_req_data['processing_registration']) |
|
| 80 | 80 | || absint($this->_req_data['processing_registration']) !== 1 |
| 81 | 81 | ) { |
| 82 | 82 | // and it's NOT the attendee information reg step |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | public function _set_page_routes() |
| 172 | 172 | { |
| 173 | 173 | $this->_get_registration_status_array(); |
| 174 | - $reg_id = ! empty($this->_req_data['_REG_ID']) && ! is_array($this->_req_data['_REG_ID']) |
|
| 174 | + $reg_id = ! empty($this->_req_data['_REG_ID']) && ! is_array($this->_req_data['_REG_ID']) |
|
| 175 | 175 | ? $this->_req_data['_REG_ID'] : 0; |
| 176 | 176 | $reg_id = empty($reg_id) && ! empty($this->_req_data['reg_status_change_form']['REG_ID']) |
| 177 | 177 | ? $this->_req_data['reg_status_change_form']['REG_ID'] |
@@ -669,7 +669,7 @@ discard block |
||
| 669 | 669 | //style |
| 670 | 670 | wp_register_style( |
| 671 | 671 | 'espresso_reg', |
| 672 | - REG_ASSETS_URL . 'espresso_registrations_admin.css', |
|
| 672 | + REG_ASSETS_URL.'espresso_registrations_admin.css', |
|
| 673 | 673 | array('ee-admin-css'), |
| 674 | 674 | EVENT_ESPRESSO_VERSION |
| 675 | 675 | ); |
@@ -677,7 +677,7 @@ discard block |
||
| 677 | 677 | //script |
| 678 | 678 | wp_register_script( |
| 679 | 679 | 'espresso_reg', |
| 680 | - REG_ASSETS_URL . 'espresso_registrations_admin.js', |
|
| 680 | + REG_ASSETS_URL.'espresso_registrations_admin.js', |
|
| 681 | 681 | array('jquery-ui-datepicker', 'jquery-ui-draggable', 'ee_admin_js'), |
| 682 | 682 | EVENT_ESPRESSO_VERSION, |
| 683 | 683 | true |
@@ -715,7 +715,7 @@ discard block |
||
| 715 | 715 | wp_deregister_style('espresso_reg'); |
| 716 | 716 | wp_register_style( |
| 717 | 717 | 'espresso_att', |
| 718 | - REG_ASSETS_URL . 'espresso_attendees_admin.css', |
|
| 718 | + REG_ASSETS_URL.'espresso_attendees_admin.css', |
|
| 719 | 719 | array('ee-admin-css'), |
| 720 | 720 | EVENT_ESPRESSO_VERSION |
| 721 | 721 | ); |
@@ -727,7 +727,7 @@ discard block |
||
| 727 | 727 | { |
| 728 | 728 | wp_register_script( |
| 729 | 729 | 'ee-spco-for-admin', |
| 730 | - REG_ASSETS_URL . 'spco_for_admin.js', |
|
| 730 | + REG_ASSETS_URL.'spco_for_admin.js', |
|
| 731 | 731 | array('underscore', 'jquery'), |
| 732 | 732 | EVENT_ESPRESSO_VERSION, |
| 733 | 733 | true |
@@ -861,7 +861,7 @@ discard block |
||
| 861 | 861 | 'trash_registrations' => esc_html__('Trash Registrations', 'event_espresso'), |
| 862 | 862 | ), |
| 863 | 863 | ); |
| 864 | - $this->_views['trash'] = array( |
|
| 864 | + $this->_views['trash'] = array( |
|
| 865 | 865 | 'slug' => 'trash', |
| 866 | 866 | 'label' => esc_html__('Trash', 'event_espresso'), |
| 867 | 867 | 'count' => 0, |
@@ -950,7 +950,7 @@ discard block |
||
| 950 | 950 | } |
| 951 | 951 | $sc_items = array( |
| 952 | 952 | 'approved_status' => array( |
| 953 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_approved, |
|
| 953 | + 'class' => 'ee-status-legend ee-status-legend-'.EEM_Registration::status_id_approved, |
|
| 954 | 954 | 'desc' => EEH_Template::pretty_status( |
| 955 | 955 | EEM_Registration::status_id_approved, |
| 956 | 956 | false, |
@@ -958,7 +958,7 @@ discard block |
||
| 958 | 958 | ), |
| 959 | 959 | ), |
| 960 | 960 | 'pending_status' => array( |
| 961 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_pending_payment, |
|
| 961 | + 'class' => 'ee-status-legend ee-status-legend-'.EEM_Registration::status_id_pending_payment, |
|
| 962 | 962 | 'desc' => EEH_Template::pretty_status( |
| 963 | 963 | EEM_Registration::status_id_pending_payment, |
| 964 | 964 | false, |
@@ -966,7 +966,7 @@ discard block |
||
| 966 | 966 | ), |
| 967 | 967 | ), |
| 968 | 968 | 'wait_list' => array( |
| 969 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_wait_list, |
|
| 969 | + 'class' => 'ee-status-legend ee-status-legend-'.EEM_Registration::status_id_wait_list, |
|
| 970 | 970 | 'desc' => EEH_Template::pretty_status( |
| 971 | 971 | EEM_Registration::status_id_wait_list, |
| 972 | 972 | false, |
@@ -974,7 +974,7 @@ discard block |
||
| 974 | 974 | ), |
| 975 | 975 | ), |
| 976 | 976 | 'incomplete_status' => array( |
| 977 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_incomplete, |
|
| 977 | + 'class' => 'ee-status-legend ee-status-legend-'.EEM_Registration::status_id_incomplete, |
|
| 978 | 978 | 'desc' => EEH_Template::pretty_status( |
| 979 | 979 | EEM_Registration::status_id_incomplete, |
| 980 | 980 | false, |
@@ -982,7 +982,7 @@ discard block |
||
| 982 | 982 | ), |
| 983 | 983 | ), |
| 984 | 984 | 'not_approved' => array( |
| 985 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_not_approved, |
|
| 985 | + 'class' => 'ee-status-legend ee-status-legend-'.EEM_Registration::status_id_not_approved, |
|
| 986 | 986 | 'desc' => EEH_Template::pretty_status( |
| 987 | 987 | EEM_Registration::status_id_not_approved, |
| 988 | 988 | false, |
@@ -990,7 +990,7 @@ discard block |
||
| 990 | 990 | ), |
| 991 | 991 | ), |
| 992 | 992 | 'declined_status' => array( |
| 993 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_declined, |
|
| 993 | + 'class' => 'ee-status-legend ee-status-legend-'.EEM_Registration::status_id_declined, |
|
| 994 | 994 | 'desc' => EEH_Template::pretty_status( |
| 995 | 995 | EEM_Registration::status_id_declined, |
| 996 | 996 | false, |
@@ -998,7 +998,7 @@ discard block |
||
| 998 | 998 | ), |
| 999 | 999 | ), |
| 1000 | 1000 | 'cancelled_status' => array( |
| 1001 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Registration::status_id_cancelled, |
|
| 1001 | + 'class' => 'ee-status-legend ee-status-legend-'.EEM_Registration::status_id_cancelled, |
|
| 1002 | 1002 | 'desc' => EEH_Template::pretty_status( |
| 1003 | 1003 | EEM_Registration::status_id_cancelled, |
| 1004 | 1004 | false, |
@@ -1021,7 +1021,7 @@ discard block |
||
| 1021 | 1021 | $EVT_ID = ! empty($this->_req_data['event_id']) |
| 1022 | 1022 | ? absint($this->_req_data['event_id']) |
| 1023 | 1023 | : 0; |
| 1024 | - $ATT_ID = !empty($this->_req_data['ATT_ID']) |
|
| 1024 | + $ATT_ID = ! empty($this->_req_data['ATT_ID']) |
|
| 1025 | 1025 | ? absint($this->_req_data['ATT_ID']) |
| 1026 | 1026 | : 0; |
| 1027 | 1027 | if ($ATT_ID) { |
@@ -1033,13 +1033,13 @@ discard block |
||
| 1033 | 1033 | 'event_espresso' |
| 1034 | 1034 | ), |
| 1035 | 1035 | '<h3 style="line-height:1.5em;">', |
| 1036 | - '<a href="' . EE_Admin_Page::add_query_args_and_nonce( |
|
| 1036 | + '<a href="'.EE_Admin_Page::add_query_args_and_nonce( |
|
| 1037 | 1037 | array( |
| 1038 | 1038 | 'action' => 'edit_attendee', |
| 1039 | 1039 | 'post' => $ATT_ID |
| 1040 | 1040 | ), |
| 1041 | 1041 | REG_ADMIN_URL |
| 1042 | - ) . '">' . $attendee->full_name() . '</a>', |
|
| 1042 | + ).'">'.$attendee->full_name().'</a>', |
|
| 1043 | 1043 | '</h3>' |
| 1044 | 1044 | ); |
| 1045 | 1045 | } |
@@ -1050,7 +1050,7 @@ discard block |
||
| 1050 | 1050 | 'espresso_registrations_new_registration', |
| 1051 | 1051 | $EVT_ID |
| 1052 | 1052 | )) { |
| 1053 | - $this->_admin_page_title .= ' ' . $this->get_action_link_or_button( |
|
| 1053 | + $this->_admin_page_title .= ' '.$this->get_action_link_or_button( |
|
| 1054 | 1054 | 'new_registration', |
| 1055 | 1055 | 'add-registrant', |
| 1056 | 1056 | array('event_id' => $EVT_ID), |
@@ -1090,7 +1090,7 @@ discard block |
||
| 1090 | 1090 | $this->_template_args['admin_page_header'] .= ' <span class="drk-grey-text">'; |
| 1091 | 1091 | $this->_template_args['admin_page_header'] .= '<span class="dashicons dashicons-calendar"></span>'; |
| 1092 | 1092 | $this->_template_args['admin_page_header'] .= $datetime->name(); |
| 1093 | - $this->_template_args['admin_page_header'] .= ' ( ' . $datetime->start_date() . ' )'; |
|
| 1093 | + $this->_template_args['admin_page_header'] .= ' ( '.$datetime->start_date().' )'; |
|
| 1094 | 1094 | $this->_template_args['admin_page_header'] .= '</span></h3>'; |
| 1095 | 1095 | } |
| 1096 | 1096 | } |
@@ -1197,7 +1197,7 @@ discard block |
||
| 1197 | 1197 | 'caps' => EEM_Registration::caps_read_admin, |
| 1198 | 1198 | 'default_where_conditions' => 'this_model_only', |
| 1199 | 1199 | ); |
| 1200 | - if (! $count) { |
|
| 1200 | + if ( ! $count) { |
|
| 1201 | 1201 | $query_params = array_merge( |
| 1202 | 1202 | $query_params, |
| 1203 | 1203 | $this->_get_orderby_for_registrations_query(), |
@@ -1218,7 +1218,7 @@ discard block |
||
| 1218 | 1218 | protected function addAttendeeIdToWhereConditions(array $request) |
| 1219 | 1219 | { |
| 1220 | 1220 | $where = array(); |
| 1221 | - if (! empty($request['ATT_ID'])) { |
|
| 1221 | + if ( ! empty($request['ATT_ID'])) { |
|
| 1222 | 1222 | $where['ATT_ID'] = absint($request['ATT_ID']); |
| 1223 | 1223 | } |
| 1224 | 1224 | return $where; |
@@ -1234,7 +1234,7 @@ discard block |
||
| 1234 | 1234 | protected function _add_event_id_to_where_conditions(array $request) |
| 1235 | 1235 | { |
| 1236 | 1236 | $where = array(); |
| 1237 | - if (! empty($request['event_id'])) { |
|
| 1237 | + if ( ! empty($request['event_id'])) { |
|
| 1238 | 1238 | $where['EVT_ID'] = absint($request['event_id']); |
| 1239 | 1239 | } |
| 1240 | 1240 | return $where; |
@@ -1250,7 +1250,7 @@ discard block |
||
| 1250 | 1250 | protected function _add_category_id_to_where_conditions(array $request) |
| 1251 | 1251 | { |
| 1252 | 1252 | $where = array(); |
| 1253 | - if (! empty($request['EVT_CAT']) && (int)$request['EVT_CAT'] !== -1) { |
|
| 1253 | + if ( ! empty($request['EVT_CAT']) && (int) $request['EVT_CAT'] !== -1) { |
|
| 1254 | 1254 | $where['Event.Term_Taxonomy.term_id'] = absint($request['EVT_CAT']); |
| 1255 | 1255 | } |
| 1256 | 1256 | return $where; |
@@ -1266,10 +1266,10 @@ discard block |
||
| 1266 | 1266 | protected function _add_datetime_id_to_where_conditions(array $request) |
| 1267 | 1267 | { |
| 1268 | 1268 | $where = array(); |
| 1269 | - if (! empty($request['datetime_id'])) { |
|
| 1269 | + if ( ! empty($request['datetime_id'])) { |
|
| 1270 | 1270 | $where['Ticket.Datetime.DTT_ID'] = absint($request['datetime_id']); |
| 1271 | 1271 | } |
| 1272 | - if (! empty($request['DTT_ID'])) { |
|
| 1272 | + if ( ! empty($request['DTT_ID'])) { |
|
| 1273 | 1273 | $where['Ticket.Datetime.DTT_ID'] = absint($request['DTT_ID']); |
| 1274 | 1274 | } |
| 1275 | 1275 | return $where; |
@@ -1295,7 +1295,7 @@ discard block |
||
| 1295 | 1295 | * If not filtering by specified status, then we show all registrations excluding incomplete registrations |
| 1296 | 1296 | * UNLESS viewing trashed registrations. |
| 1297 | 1297 | */ |
| 1298 | - if (! empty($registration_status)) { |
|
| 1298 | + if ( ! empty($registration_status)) { |
|
| 1299 | 1299 | $where['STS_ID'] = $registration_status; |
| 1300 | 1300 | } else { |
| 1301 | 1301 | //make sure we exclude incomplete registrations, but only if not trashed. |
@@ -1338,12 +1338,12 @@ discard block |
||
| 1338 | 1338 | array( |
| 1339 | 1339 | EEM_Registration::instance()->convert_datetime_for_query( |
| 1340 | 1340 | 'REG_date', |
| 1341 | - $now . ' 00:00:00', |
|
| 1341 | + $now.' 00:00:00', |
|
| 1342 | 1342 | 'Y-m-d H:i:s' |
| 1343 | 1343 | ), |
| 1344 | 1344 | EEM_Registration::instance()->convert_datetime_for_query( |
| 1345 | 1345 | 'REG_date', |
| 1346 | - $now . ' 23:59:59', |
|
| 1346 | + $now.' 23:59:59', |
|
| 1347 | 1347 | 'Y-m-d H:i:s' |
| 1348 | 1348 | ), |
| 1349 | 1349 | ), |
@@ -1356,12 +1356,12 @@ discard block |
||
| 1356 | 1356 | array( |
| 1357 | 1357 | EEM_Registration::instance()->convert_datetime_for_query( |
| 1358 | 1358 | 'REG_date', |
| 1359 | - $current_year_and_month . '-01 00:00:00', |
|
| 1359 | + $current_year_and_month.'-01 00:00:00', |
|
| 1360 | 1360 | 'Y-m-d H:i:s' |
| 1361 | 1361 | ), |
| 1362 | 1362 | EEM_Registration::instance()->convert_datetime_for_query( |
| 1363 | 1363 | 'REG_date', |
| 1364 | - $current_year_and_month . '-' . $days_this_month . ' 23:59:59', |
|
| 1364 | + $current_year_and_month.'-'.$days_this_month.' 23:59:59', |
|
| 1365 | 1365 | 'Y-m-d H:i:s' |
| 1366 | 1366 | ), |
| 1367 | 1367 | ), |
@@ -1376,18 +1376,18 @@ discard block |
||
| 1376 | 1376 | : ''; |
| 1377 | 1377 | //if there is not a month or year then we can't go further |
| 1378 | 1378 | if ($month_requested && $year_requested) { |
| 1379 | - $days_in_month = date('t', strtotime($year_requested . '-' . $month_requested . '-' . '01')); |
|
| 1379 | + $days_in_month = date('t', strtotime($year_requested.'-'.$month_requested.'-'.'01')); |
|
| 1380 | 1380 | $where['REG_date'] = array( |
| 1381 | 1381 | 'BETWEEN', |
| 1382 | 1382 | array( |
| 1383 | 1383 | EEM_Registration::instance()->convert_datetime_for_query( |
| 1384 | 1384 | 'REG_date', |
| 1385 | - $year_requested . '-' . $month_requested . '-01 00:00:00', |
|
| 1385 | + $year_requested.'-'.$month_requested.'-01 00:00:00', |
|
| 1386 | 1386 | 'Y-m-d H:i:s' |
| 1387 | 1387 | ), |
| 1388 | 1388 | EEM_Registration::instance()->convert_datetime_for_query( |
| 1389 | 1389 | 'REG_date', |
| 1390 | - $year_requested . '-' . $month_requested . '-' . $days_in_month . ' 23:59:59', |
|
| 1390 | + $year_requested.'-'.$month_requested.'-'.$days_in_month.' 23:59:59', |
|
| 1391 | 1391 | 'Y-m-d H:i:s' |
| 1392 | 1392 | ), |
| 1393 | 1393 | ), |
@@ -1407,8 +1407,8 @@ discard block |
||
| 1407 | 1407 | protected function _add_search_to_where_conditions(array $request) |
| 1408 | 1408 | { |
| 1409 | 1409 | $where = array(); |
| 1410 | - if (! empty($request['s'])) { |
|
| 1411 | - $search_string = '%' . sanitize_text_field($request['s']) . '%'; |
|
| 1410 | + if ( ! empty($request['s'])) { |
|
| 1411 | + $search_string = '%'.sanitize_text_field($request['s']).'%'; |
|
| 1412 | 1412 | $where['OR*search_conditions'] = array( |
| 1413 | 1413 | 'Event.EVT_name' => array('LIKE', $search_string), |
| 1414 | 1414 | 'Event.EVT_desc' => array('LIKE', $search_string), |
@@ -1525,7 +1525,7 @@ discard block |
||
| 1525 | 1525 | : $per_page; |
| 1526 | 1526 | |
| 1527 | 1527 | //-1 means return all results so get out if that's set. |
| 1528 | - if ((int)$per_page === -1) { |
|
| 1528 | + if ((int) $per_page === -1) { |
|
| 1529 | 1529 | return array(); |
| 1530 | 1530 | } |
| 1531 | 1531 | $per_page = absint($per_page); |
@@ -1581,7 +1581,7 @@ discard block |
||
| 1581 | 1581 | ), |
| 1582 | 1582 | REG_ADMIN_URL |
| 1583 | 1583 | ); |
| 1584 | - $this->_template_args['filtered_transactions_link'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 1584 | + $this->_template_args['filtered_transactions_link'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 1585 | 1585 | array( |
| 1586 | 1586 | 'action' => 'default', |
| 1587 | 1587 | 'EVT_ID' => $event_id, |
@@ -1589,7 +1589,7 @@ discard block |
||
| 1589 | 1589 | ), |
| 1590 | 1590 | admin_url('admin.php') |
| 1591 | 1591 | ); |
| 1592 | - $this->_template_args['event_link'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 1592 | + $this->_template_args['event_link'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 1593 | 1593 | array( |
| 1594 | 1594 | 'page' => 'espresso_events', |
| 1595 | 1595 | 'action' => 'edit', |
@@ -1598,12 +1598,12 @@ discard block |
||
| 1598 | 1598 | admin_url('admin.php') |
| 1599 | 1599 | ); |
| 1600 | 1600 | //next and previous links |
| 1601 | - $next_reg = $this->_registration->next( |
|
| 1601 | + $next_reg = $this->_registration->next( |
|
| 1602 | 1602 | null, |
| 1603 | 1603 | array(), |
| 1604 | 1604 | 'REG_ID' |
| 1605 | 1605 | ); |
| 1606 | - $this->_template_args['next_registration'] = $next_reg |
|
| 1606 | + $this->_template_args['next_registration'] = $next_reg |
|
| 1607 | 1607 | ? $this->_next_link( |
| 1608 | 1608 | EE_Admin_Page::add_query_args_and_nonce( |
| 1609 | 1609 | array( |
@@ -1615,7 +1615,7 @@ discard block |
||
| 1615 | 1615 | 'dashicons dashicons-arrow-right ee-icon-size-22' |
| 1616 | 1616 | ) |
| 1617 | 1617 | : ''; |
| 1618 | - $previous_reg = $this->_registration->previous( |
|
| 1618 | + $previous_reg = $this->_registration->previous( |
|
| 1619 | 1619 | null, |
| 1620 | 1620 | array(), |
| 1621 | 1621 | 'REG_ID' |
@@ -1633,7 +1633,7 @@ discard block |
||
| 1633 | 1633 | ) |
| 1634 | 1634 | : ''; |
| 1635 | 1635 | // grab header |
| 1636 | - $template_path = REG_TEMPLATE_PATH . 'reg_admin_details_header.template.php'; |
|
| 1636 | + $template_path = REG_TEMPLATE_PATH.'reg_admin_details_header.template.php'; |
|
| 1637 | 1637 | $this->_template_args['REG_ID'] = $this->_registration->ID(); |
| 1638 | 1638 | $this->_template_args['admin_page_header'] = EEH_Template::display_template( |
| 1639 | 1639 | $template_path, |
@@ -1753,7 +1753,7 @@ discard block |
||
| 1753 | 1753 | EEH_HTML::strong( |
| 1754 | 1754 | $this->_registration->pretty_status(), |
| 1755 | 1755 | '', |
| 1756 | - 'status-' . $this->_registration->status_ID(), |
|
| 1756 | + 'status-'.$this->_registration->status_ID(), |
|
| 1757 | 1757 | 'line-height: 1em; font-size: 1.5em; font-weight: bold;' |
| 1758 | 1758 | ) |
| 1759 | 1759 | ) |
@@ -1833,11 +1833,11 @@ discard block |
||
| 1833 | 1833 | { |
| 1834 | 1834 | if (isset($this->_req_data['reg_status_change_form'])) { |
| 1835 | 1835 | $REG_IDs = isset($this->_req_data['reg_status_change_form']['REG_ID']) |
| 1836 | - ? (array)$this->_req_data['reg_status_change_form']['REG_ID'] |
|
| 1836 | + ? (array) $this->_req_data['reg_status_change_form']['REG_ID'] |
|
| 1837 | 1837 | : array(); |
| 1838 | 1838 | } else { |
| 1839 | 1839 | $REG_IDs = isset($this->_req_data['_REG_ID']) |
| 1840 | - ? (array)$this->_req_data['_REG_ID'] |
|
| 1840 | + ? (array) $this->_req_data['_REG_ID'] |
|
| 1841 | 1841 | : array(); |
| 1842 | 1842 | } |
| 1843 | 1843 | // sanitize $REG_IDs |
@@ -1900,7 +1900,7 @@ discard block |
||
| 1900 | 1900 | { |
| 1901 | 1901 | $success = false; |
| 1902 | 1902 | // typecast $REG_IDs |
| 1903 | - $REG_IDs = (array)$REG_IDs; |
|
| 1903 | + $REG_IDs = (array) $REG_IDs; |
|
| 1904 | 1904 | if ( ! empty($REG_IDs)) { |
| 1905 | 1905 | $success = true; |
| 1906 | 1906 | // set default status if none is passed |
@@ -2050,7 +2050,7 @@ discard block |
||
| 2050 | 2050 | $action, |
| 2051 | 2051 | $notify |
| 2052 | 2052 | ); |
| 2053 | - $method = $action . '_registration'; |
|
| 2053 | + $method = $action.'_registration'; |
|
| 2054 | 2054 | if (method_exists($this, $method)) { |
| 2055 | 2055 | $this->$method($notify); |
| 2056 | 2056 | } |
@@ -2168,7 +2168,7 @@ discard block |
||
| 2168 | 2168 | $filtered_line_item_tree, |
| 2169 | 2169 | array('EE_Registration' => $this->_registration) |
| 2170 | 2170 | ); |
| 2171 | - $attendee = $this->_registration->attendee(); |
|
| 2171 | + $attendee = $this->_registration->attendee(); |
|
| 2172 | 2172 | if (EE_Registry::instance()->CAP->current_user_can( |
| 2173 | 2173 | 'ee_read_transaction', |
| 2174 | 2174 | 'espresso_transactions_view_transaction' |
@@ -2247,7 +2247,7 @@ discard block |
||
| 2247 | 2247 | 'Payment method response', |
| 2248 | 2248 | 'event_espresso' |
| 2249 | 2249 | ); |
| 2250 | - $this->_template_args['reg_details']['response_msg']['class'] = 'regular-text'; |
|
| 2250 | + $this->_template_args['reg_details']['response_msg']['class'] = 'regular-text'; |
|
| 2251 | 2251 | } |
| 2252 | 2252 | $this->_template_args['reg_details']['registration_session']['value'] = $reg_details['registration_session']; |
| 2253 | 2253 | $this->_template_args['reg_details']['registration_session']['label'] = esc_html__( |
@@ -2275,7 +2275,7 @@ discard block |
||
| 2275 | 2275 | $this->_template_args['REG_ID'] = $this->_registration->ID(); |
| 2276 | 2276 | $this->_template_args['event_id'] = $this->_registration->event_ID(); |
| 2277 | 2277 | $template_path = |
| 2278 | - REG_TEMPLATE_PATH . 'reg_admin_details_main_meta_box_reg_details.template.php'; |
|
| 2278 | + REG_TEMPLATE_PATH.'reg_admin_details_main_meta_box_reg_details.template.php'; |
|
| 2279 | 2279 | echo EEH_Template::display_template($template_path, $this->_template_args, true); |
| 2280 | 2280 | } |
| 2281 | 2281 | |
@@ -2304,7 +2304,7 @@ discard block |
||
| 2304 | 2304 | $this->_template_args['reg_questions_form_action'] = 'edit_registration'; |
| 2305 | 2305 | $this->_template_args['REG_ID'] = $this->_registration->ID(); |
| 2306 | 2306 | $template_path = |
| 2307 | - REG_TEMPLATE_PATH . 'reg_admin_details_main_meta_box_reg_questions.template.php'; |
|
| 2307 | + REG_TEMPLATE_PATH.'reg_admin_details_main_meta_box_reg_questions.template.php'; |
|
| 2308 | 2308 | echo EEH_Template::display_template($template_path, $this->_template_args, true); |
| 2309 | 2309 | } |
| 2310 | 2310 | } |
@@ -2321,7 +2321,7 @@ discard block |
||
| 2321 | 2321 | public function form_before_question_group($output) |
| 2322 | 2322 | { |
| 2323 | 2323 | EE_Error::doing_it_wrong( |
| 2324 | - __CLASS__ . '::' . __FUNCTION__, |
|
| 2324 | + __CLASS__.'::'.__FUNCTION__, |
|
| 2325 | 2325 | esc_html__( |
| 2326 | 2326 | 'This method would have been protected but was used on a filter callback so needed to be public. Please discontinue usage as it will be removed soon.', |
| 2327 | 2327 | 'event_espresso' |
@@ -2346,7 +2346,7 @@ discard block |
||
| 2346 | 2346 | public function form_after_question_group($output) |
| 2347 | 2347 | { |
| 2348 | 2348 | EE_Error::doing_it_wrong( |
| 2349 | - __CLASS__ . '::' . __FUNCTION__, |
|
| 2349 | + __CLASS__.'::'.__FUNCTION__, |
|
| 2350 | 2350 | esc_html__( |
| 2351 | 2351 | 'This method would have been protected but was used on a filter callback so needed to be public. Please discontinue usage as it will be removed soon.', |
| 2352 | 2352 | 'event_espresso' |
@@ -2384,7 +2384,7 @@ discard block |
||
| 2384 | 2384 | public function form_form_field_label_wrap($label) |
| 2385 | 2385 | { |
| 2386 | 2386 | EE_Error::doing_it_wrong( |
| 2387 | - __CLASS__ . '::' . __FUNCTION__, |
|
| 2387 | + __CLASS__.'::'.__FUNCTION__, |
|
| 2388 | 2388 | esc_html__( |
| 2389 | 2389 | 'This method would have been protected but was used on a filter callback so needed to be public. Please discontinue usage as it will be removed soon.', |
| 2390 | 2390 | 'event_espresso' |
@@ -2394,7 +2394,7 @@ discard block |
||
| 2394 | 2394 | return ' |
| 2395 | 2395 | <tr> |
| 2396 | 2396 | <th> |
| 2397 | - ' . $label . ' |
|
| 2397 | + ' . $label.' |
|
| 2398 | 2398 | </th>'; |
| 2399 | 2399 | } |
| 2400 | 2400 | |
@@ -2410,7 +2410,7 @@ discard block |
||
| 2410 | 2410 | public function form_form_field_input__wrap($input) |
| 2411 | 2411 | { |
| 2412 | 2412 | EE_Error::doing_it_wrong( |
| 2413 | - __CLASS__ . '::' . __FUNCTION__, |
|
| 2413 | + __CLASS__.'::'.__FUNCTION__, |
|
| 2414 | 2414 | esc_html__( |
| 2415 | 2415 | 'This method would have been protected but was used on a filter callback so needed to be public. Please discontinue usage as it will be removed soon.', |
| 2416 | 2416 | 'event_espresso' |
@@ -2419,7 +2419,7 @@ discard block |
||
| 2419 | 2419 | ); |
| 2420 | 2420 | return ' |
| 2421 | 2421 | <td class="reg-admin-attendee-questions-input-td disabled-input"> |
| 2422 | - ' . $input . ' |
|
| 2422 | + ' . $input.' |
|
| 2423 | 2423 | </td> |
| 2424 | 2424 | </tr>'; |
| 2425 | 2425 | } |
@@ -2464,7 +2464,7 @@ discard block |
||
| 2464 | 2464 | protected function _get_reg_custom_questions_form($REG_ID) |
| 2465 | 2465 | { |
| 2466 | 2466 | if ( ! $this->_reg_custom_questions_form) { |
| 2467 | - require_once(REG_ADMIN . 'form_sections' . DS . 'EE_Registration_Custom_Questions_Form.form.php'); |
|
| 2467 | + require_once(REG_ADMIN.'form_sections'.DS.'EE_Registration_Custom_Questions_Form.form.php'); |
|
| 2468 | 2468 | $this->_reg_custom_questions_form = new EE_Registration_Custom_Questions_Form( |
| 2469 | 2469 | EEM_Registration::instance()->get_one_by_ID($REG_ID) |
| 2470 | 2470 | ); |
@@ -2500,7 +2500,7 @@ discard block |
||
| 2500 | 2500 | if ($form->is_valid()) { |
| 2501 | 2501 | foreach ($form->subforms() as $question_group_id => $question_group_form) { |
| 2502 | 2502 | foreach ($question_group_form->inputs() as $question_id => $input) { |
| 2503 | - $where_conditions = array( |
|
| 2503 | + $where_conditions = array( |
|
| 2504 | 2504 | 'QST_ID' => $question_id, |
| 2505 | 2505 | 'REG_ID' => $REG_ID, |
| 2506 | 2506 | ); |
@@ -2541,7 +2541,7 @@ discard block |
||
| 2541 | 2541 | $REG = EEM_Registration::instance(); |
| 2542 | 2542 | //get all other registrations on this transaction, and cache |
| 2543 | 2543 | //the attendees for them so we don't have to run another query using force_join |
| 2544 | - $registrations = $REG->get_all(array( |
|
| 2544 | + $registrations = $REG->get_all(array( |
|
| 2545 | 2545 | array( |
| 2546 | 2546 | 'TXN_ID' => $this->_registration->transaction_ID(), |
| 2547 | 2547 | 'REG_ID' => array('!=', $this->_registration->ID()), |
@@ -2565,7 +2565,7 @@ discard block |
||
| 2565 | 2565 | $att_nmbr = 1; |
| 2566 | 2566 | foreach ($registrations as $registration) { |
| 2567 | 2567 | /* @var $registration EE_Registration */ |
| 2568 | - $attendee = $registration->attendee() |
|
| 2568 | + $attendee = $registration->attendee() |
|
| 2569 | 2569 | ? $registration->attendee() |
| 2570 | 2570 | : EEM_Attendee::instance() |
| 2571 | 2571 | ->create_default_object(); |
@@ -2578,19 +2578,19 @@ discard block |
||
| 2578 | 2578 | ', ', |
| 2579 | 2579 | $attendee->full_address_as_array() |
| 2580 | 2580 | ); |
| 2581 | - $this->_template_args['attendees'][$att_nmbr]['att_link'] = self::add_query_args_and_nonce( |
|
| 2581 | + $this->_template_args['attendees'][$att_nmbr]['att_link'] = self::add_query_args_and_nonce( |
|
| 2582 | 2582 | array( |
| 2583 | 2583 | 'action' => 'edit_attendee', |
| 2584 | 2584 | 'post' => $attendee->ID(), |
| 2585 | 2585 | ), |
| 2586 | 2586 | REG_ADMIN_URL |
| 2587 | 2587 | ); |
| 2588 | - $this->_template_args['attendees'][$att_nmbr]['event_name'] = $registration->event_obj()->name(); |
|
| 2588 | + $this->_template_args['attendees'][$att_nmbr]['event_name'] = $registration->event_obj()->name(); |
|
| 2589 | 2589 | $att_nmbr++; |
| 2590 | 2590 | } |
| 2591 | 2591 | $this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign; |
| 2592 | 2592 | } |
| 2593 | - $template_path = REG_TEMPLATE_PATH . 'reg_admin_details_main_meta_box_attendees.template.php'; |
|
| 2593 | + $template_path = REG_TEMPLATE_PATH.'reg_admin_details_main_meta_box_attendees.template.php'; |
|
| 2594 | 2594 | echo EEH_Template::display_template($template_path, $this->_template_args, true); |
| 2595 | 2595 | } |
| 2596 | 2596 | |
@@ -2633,20 +2633,20 @@ discard block |
||
| 2633 | 2633 | $this->_template_args['phone'] = $attendee->phone(); |
| 2634 | 2634 | $this->_template_args['formatted_address'] = EEH_Address::format($attendee); |
| 2635 | 2635 | //edit link |
| 2636 | - $this->_template_args['att_edit_link'] = EE_Admin_Page::add_query_args_and_nonce(array( |
|
| 2636 | + $this->_template_args['att_edit_link'] = EE_Admin_Page::add_query_args_and_nonce(array( |
|
| 2637 | 2637 | 'action' => 'edit_attendee', |
| 2638 | 2638 | 'post' => $attendee->ID(), |
| 2639 | 2639 | ), REG_ADMIN_URL); |
| 2640 | 2640 | $this->_template_args['att_edit_label'] = esc_html__('View/Edit Contact', 'event_espresso'); |
| 2641 | 2641 | //create link |
| 2642 | - $this->_template_args['create_link'] = $primary_registration instanceof EE_Registration |
|
| 2642 | + $this->_template_args['create_link'] = $primary_registration instanceof EE_Registration |
|
| 2643 | 2643 | ? EE_Admin_Page::add_query_args_and_nonce(array( |
| 2644 | 2644 | 'action' => 'duplicate_attendee', |
| 2645 | 2645 | '_REG_ID' => $this->_registration->ID(), |
| 2646 | 2646 | ), REG_ADMIN_URL) : ''; |
| 2647 | 2647 | $this->_template_args['create_label'] = esc_html__('Create Contact', 'event_espresso'); |
| 2648 | 2648 | $this->_template_args['att_check'] = $att_check; |
| 2649 | - $template_path = REG_TEMPLATE_PATH . 'reg_admin_details_side_meta_box_registrant.template.php'; |
|
| 2649 | + $template_path = REG_TEMPLATE_PATH.'reg_admin_details_side_meta_box_registrant.template.php'; |
|
| 2650 | 2650 | echo EEH_Template::display_template($template_path, $this->_template_args, true); |
| 2651 | 2651 | } |
| 2652 | 2652 | |
@@ -2691,7 +2691,7 @@ discard block |
||
| 2691 | 2691 | /** @var EE_Registration $REG */ |
| 2692 | 2692 | $REG = EEM_Registration::instance()->get_one_by_ID($REG_ID); |
| 2693 | 2693 | $payments = $REG->registration_payments(); |
| 2694 | - if (! empty($payments)) { |
|
| 2694 | + if ( ! empty($payments)) { |
|
| 2695 | 2695 | $name = $REG->attendee() instanceof EE_Attendee |
| 2696 | 2696 | ? $REG->attendee()->full_name() |
| 2697 | 2697 | : esc_html__('Unknown Attendee', 'event_espresso'); |
@@ -2887,7 +2887,7 @@ discard block |
||
| 2887 | 2887 | 'action' => 'edit', |
| 2888 | 2888 | 'post' => $this->_reg_event->ID(), |
| 2889 | 2889 | ), EVENTS_ADMIN_URL); |
| 2890 | - $edit_event_lnk = '<a href="' |
|
| 2890 | + $edit_event_lnk = '<a href="' |
|
| 2891 | 2891 | . $edit_event_url |
| 2892 | 2892 | . '" title="' |
| 2893 | 2893 | . esc_attr__('Edit ', 'event_espresso') |
@@ -2905,7 +2905,7 @@ discard block |
||
| 2905 | 2905 | } |
| 2906 | 2906 | // grab header |
| 2907 | 2907 | $template_path = |
| 2908 | - REG_TEMPLATE_PATH . 'reg_admin_register_new_attendee.template.php'; |
|
| 2908 | + REG_TEMPLATE_PATH.'reg_admin_register_new_attendee.template.php'; |
|
| 2909 | 2909 | $this->_template_args['admin_page_content'] = EEH_Template::display_template($template_path, |
| 2910 | 2910 | $this->_template_args, true); |
| 2911 | 2911 | //$this->_set_publish_post_box_vars( NULL, FALSE, FALSE, NULL, FALSE ); |
@@ -2943,7 +2943,7 @@ discard block |
||
| 2943 | 2943 | '</b>' |
| 2944 | 2944 | ); |
| 2945 | 2945 | return ' |
| 2946 | - <div id="ee-add-reg-back-button-dv"><p>' . $warning_msg . '</p></div> |
|
| 2946 | + <div id="ee-add-reg-back-button-dv"><p>' . $warning_msg.'</p></div> |
|
| 2947 | 2947 | <script > |
| 2948 | 2948 | // WHOAH !!! it appears that someone is using the back button from the Transaction admin page |
| 2949 | 2949 | // after just adding a new registration... we gotta try to put a stop to that !!! |
@@ -3011,7 +3011,7 @@ discard block |
||
| 3011 | 3011 | //we come back to the process_registration_step route. |
| 3012 | 3012 | $this->_set_add_edit_form_tags('process_reg_step', $hidden_fields); |
| 3013 | 3013 | return EEH_Template::display_template( |
| 3014 | - REG_TEMPLATE_PATH . 'reg_admin_register_new_attendee_step_content.template.php', |
|
| 3014 | + REG_TEMPLATE_PATH.'reg_admin_register_new_attendee_step_content.template.php', |
|
| 3015 | 3015 | $template_args, |
| 3016 | 3016 | true |
| 3017 | 3017 | ); |
@@ -3033,7 +3033,7 @@ discard block |
||
| 3033 | 3033 | if (is_object($this->_reg_event)) { |
| 3034 | 3034 | return true; |
| 3035 | 3035 | } |
| 3036 | - $EVT_ID = (! empty($this->_req_data['event_id'])) ? absint($this->_req_data['event_id']) : false; |
|
| 3036 | + $EVT_ID = ( ! empty($this->_req_data['event_id'])) ? absint($this->_req_data['event_id']) : false; |
|
| 3037 | 3037 | if ( ! $EVT_ID) { |
| 3038 | 3038 | return false; |
| 3039 | 3039 | } |
@@ -3103,7 +3103,7 @@ discard block |
||
| 3103 | 3103 | } |
| 3104 | 3104 | break; |
| 3105 | 3105 | case 'questions' : |
| 3106 | - if (! isset( |
|
| 3106 | + if ( ! isset( |
|
| 3107 | 3107 | $this->_req_data['txn_reg_status_change'], |
| 3108 | 3108 | $this->_req_data['txn_reg_status_change']['send_notifications']) |
| 3109 | 3109 | ) { |
@@ -3223,7 +3223,7 @@ discard block |
||
| 3223 | 3223 | public function get_attendees($per_page, $count = false, $trash = false) |
| 3224 | 3224 | { |
| 3225 | 3225 | do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
| 3226 | - require_once(REG_ADMIN . 'EE_Attendee_Contact_List_Table.class.php'); |
|
| 3226 | + require_once(REG_ADMIN.'EE_Attendee_Contact_List_Table.class.php'); |
|
| 3227 | 3227 | $ATT_MDL = EEM_Attendee::instance(); |
| 3228 | 3228 | $this->_req_data['orderby'] = ! empty($this->_req_data['orderby']) ? $this->_req_data['orderby'] : ''; |
| 3229 | 3229 | switch ($this->_req_data['orderby']) { |
@@ -3263,7 +3263,7 @@ discard block |
||
| 3263 | 3263 | : $per_page; |
| 3264 | 3264 | $_where = array(); |
| 3265 | 3265 | if ( ! empty($this->_req_data['s'])) { |
| 3266 | - $sstr = '%' . $this->_req_data['s'] . '%'; |
|
| 3266 | + $sstr = '%'.$this->_req_data['s'].'%'; |
|
| 3267 | 3267 | $_where['OR'] = array( |
| 3268 | 3268 | 'Registration.Event.EVT_name' => array('LIKE', $sstr), |
| 3269 | 3269 | 'Registration.Event.EVT_desc' => array('LIKE', $sstr), |
@@ -3290,17 +3290,17 @@ discard block |
||
| 3290 | 3290 | 'extra_selects' => array('Registration_Count' => array('Registration.REG_ID', 'count', '%d')), |
| 3291 | 3291 | 'limit' => $limit |
| 3292 | 3292 | ); |
| 3293 | - if (! $count) { |
|
| 3293 | + if ( ! $count) { |
|
| 3294 | 3294 | $query_args['order_by'] = array($orderby => $sort); |
| 3295 | 3295 | } |
| 3296 | 3296 | if ($trash) { |
| 3297 | 3297 | $query_args[0]['status'] = array('!=', 'publish'); |
| 3298 | - $all_attendees = $count |
|
| 3298 | + $all_attendees = $count |
|
| 3299 | 3299 | ? $ATT_MDL->count($query_args, 'ATT_ID', true) |
| 3300 | 3300 | : $ATT_MDL->get_all($query_args); |
| 3301 | 3301 | } else { |
| 3302 | 3302 | $query_args[0]['status'] = array('IN', array('publish')); |
| 3303 | - $all_attendees = $count |
|
| 3303 | + $all_attendees = $count |
|
| 3304 | 3304 | ? $ATT_MDL->count($query_args, 'ATT_ID', true) |
| 3305 | 3305 | : $ATT_MDL->get_all($query_args); |
| 3306 | 3306 | } |
@@ -3330,9 +3330,9 @@ discard block |
||
| 3330 | 3330 | * the query parameters from the request |
| 3331 | 3331 | * @return void ends the request with a redirect or download |
| 3332 | 3332 | */ |
| 3333 | - public function _registrations_report_base( $method_name_for_getting_query_params ) |
|
| 3333 | + public function _registrations_report_base($method_name_for_getting_query_params) |
|
| 3334 | 3334 | { |
| 3335 | - if (! defined('EE_USE_OLD_CSV_REPORT_CLASS')) { |
|
| 3335 | + if ( ! defined('EE_USE_OLD_CSV_REPORT_CLASS')) { |
|
| 3336 | 3336 | wp_redirect(EE_Admin_Page::add_query_args_and_nonce( |
| 3337 | 3337 | array( |
| 3338 | 3338 | 'page' => 'espresso_batch', |
@@ -3341,7 +3341,7 @@ discard block |
||
| 3341 | 3341 | 'filters' => urlencode( |
| 3342 | 3342 | serialize( |
| 3343 | 3343 | call_user_func( |
| 3344 | - array( $this, $method_name_for_getting_query_params ), |
|
| 3344 | + array($this, $method_name_for_getting_query_params), |
|
| 3345 | 3345 | EEH_Array::is_set( |
| 3346 | 3346 | $this->_req_data, |
| 3347 | 3347 | 'filters', |
@@ -3361,8 +3361,8 @@ discard block |
||
| 3361 | 3361 | 'EVT_ID' => isset($this->_req_data['EVT_ID']) ? $this->_req_data['EVT_ID'] : null, |
| 3362 | 3362 | ); |
| 3363 | 3363 | $this->_req_data = array_merge($this->_req_data, $new_request_args); |
| 3364 | - if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 3365 | - require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 3364 | + if (is_readable(EE_CLASSES.'EE_Export.class.php')) { |
|
| 3365 | + require_once(EE_CLASSES.'EE_Export.class.php'); |
|
| 3366 | 3366 | $EE_Export = EE_Export::instance($this->_req_data); |
| 3367 | 3367 | $EE_Export->export(); |
| 3368 | 3368 | } |
@@ -3383,8 +3383,8 @@ discard block |
||
| 3383 | 3383 | |
| 3384 | 3384 | public function _contact_list_export() |
| 3385 | 3385 | { |
| 3386 | - if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 3387 | - require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 3386 | + if (is_readable(EE_CLASSES.'EE_Export.class.php')) { |
|
| 3387 | + require_once(EE_CLASSES.'EE_Export.class.php'); |
|
| 3388 | 3388 | $EE_Export = EE_Export::instance($this->_req_data); |
| 3389 | 3389 | $EE_Export->export_attendees(); |
| 3390 | 3390 | } |
@@ -3401,8 +3401,8 @@ discard block |
||
| 3401 | 3401 | 'return_url' => urlencode($this->_req_data['return_url']), |
| 3402 | 3402 | ))); |
| 3403 | 3403 | } else { |
| 3404 | - if (is_readable(EE_CLASSES . 'EE_Export.class.php')) { |
|
| 3405 | - require_once(EE_CLASSES . 'EE_Export.class.php'); |
|
| 3404 | + if (is_readable(EE_CLASSES.'EE_Export.class.php')) { |
|
| 3405 | + require_once(EE_CLASSES.'EE_Export.class.php'); |
|
| 3406 | 3406 | $EE_Export = EE_Export::instance($this->_req_data); |
| 3407 | 3407 | $EE_Export->report_attendees(); |
| 3408 | 3408 | } |
@@ -3484,7 +3484,7 @@ discard block |
||
| 3484 | 3484 | $updated_fields = array( |
| 3485 | 3485 | 'ATT_fname' => $this->_req_data['ATT_fname'], |
| 3486 | 3486 | 'ATT_lname' => $this->_req_data['ATT_lname'], |
| 3487 | - 'ATT_full_name' => $this->_req_data['ATT_fname'] . ' ' . $this->_req_data['ATT_lname'], |
|
| 3487 | + 'ATT_full_name' => $this->_req_data['ATT_fname'].' '.$this->_req_data['ATT_lname'], |
|
| 3488 | 3488 | 'ATT_address' => isset($this->_req_data['ATT_address']) ? $this->_req_data['ATT_address'] : '', |
| 3489 | 3489 | 'ATT_address2' => isset($this->_req_data['ATT_address2']) ? $this->_req_data['ATT_address2'] : '', |
| 3490 | 3490 | 'ATT_city' => isset($this->_req_data['ATT_city']) ? $this->_req_data['ATT_city'] : '', |
@@ -3702,8 +3702,8 @@ discard block |
||
| 3702 | 3702 | ) |
| 3703 | 3703 | ) |
| 3704 | 3704 | ); |
| 3705 | - $template = |
|
| 3706 | - REG_TEMPLATE_PATH . 'attendee_address_details_metabox_content.template.php'; |
|
| 3705 | + $template = |
|
| 3706 | + REG_TEMPLATE_PATH.'attendee_address_details_metabox_content.template.php'; |
|
| 3707 | 3707 | EEH_Template::display_template($template, $this->_template_args); |
| 3708 | 3708 | } |
| 3709 | 3709 | |
@@ -3722,7 +3722,7 @@ discard block |
||
| 3722 | 3722 | $this->_template_args['attendee'] = $this->_cpt_model_obj; |
| 3723 | 3723 | $this->_template_args['registrations'] = $this->_cpt_model_obj->get_many_related('Registration'); |
| 3724 | 3724 | $template = |
| 3725 | - REG_TEMPLATE_PATH . 'attendee_registrations_main_meta_box.template.php'; |
|
| 3725 | + REG_TEMPLATE_PATH.'attendee_registrations_main_meta_box.template.php'; |
|
| 3726 | 3726 | EEH_Template::display_template($template, $this->_template_args); |
| 3727 | 3727 | } |
| 3728 | 3728 | |
@@ -3737,7 +3737,7 @@ discard block |
||
| 3737 | 3737 | public function after_title_form_fields($post) |
| 3738 | 3738 | { |
| 3739 | 3739 | if ($post->post_type == 'espresso_attendees') { |
| 3740 | - $template = REG_TEMPLATE_PATH . 'attendee_details_after_title_form_fields.template.php'; |
|
| 3740 | + $template = REG_TEMPLATE_PATH.'attendee_details_after_title_form_fields.template.php'; |
|
| 3741 | 3741 | $template_args['attendee'] = $this->_cpt_model_obj; |
| 3742 | 3742 | EEH_Template::display_template($template, $template_args); |
| 3743 | 3743 | } |
@@ -28,336 +28,336 @@ |
||
| 28 | 28 | class PersistentAdminNotice implements RequiresCapCheckInterface |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @var string $name |
|
| 33 | - */ |
|
| 34 | - protected $name = ''; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var string $message |
|
| 38 | - */ |
|
| 39 | - protected $message = ''; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var boolean $force_update |
|
| 43 | - */ |
|
| 44 | - protected $force_update = false; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var string $capability |
|
| 48 | - */ |
|
| 49 | - protected $capability = 'manage_options'; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var string $cap_context |
|
| 53 | - */ |
|
| 54 | - protected $cap_context = 'view persistent admin notice'; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var boolean $dismissed |
|
| 58 | - */ |
|
| 59 | - protected $dismissed = false; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var CapCheckInterface $cap_check |
|
| 63 | - */ |
|
| 64 | - protected $cap_check; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * if true, then this notice will be deleted from the database |
|
| 68 | - * |
|
| 69 | - * @var boolean $purge |
|
| 70 | - */ |
|
| 71 | - protected $purge = false; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * gets set to true if notice is successfully registered with the PersistentAdminNoticeManager |
|
| 75 | - * if false, and WP_DEBUG is on, then an exception will be thrown in the admin footer |
|
| 76 | - * |
|
| 77 | - * @var boolean $registered |
|
| 78 | - */ |
|
| 79 | - private $registered = false; |
|
| 80 | - |
|
| 81 | - |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * PersistentAdminNotice constructor |
|
| 85 | - * |
|
| 86 | - * @param string $name [required] the name, or key of the Persistent Admin Notice to be stored |
|
| 87 | - * @param string $message [required] the message to be stored persistently until dismissed |
|
| 88 | - * @param bool $force_update enforce the reappearance of a persistent message |
|
| 89 | - * @param string $capability user capability required to view this notice |
|
| 90 | - * @param string $cap_context description for why the cap check is being performed |
|
| 91 | - * @param bool $dismissed whether or not the user has already dismissed/viewed this notice |
|
| 92 | - * @throws InvalidDataTypeException |
|
| 93 | - */ |
|
| 94 | - public function __construct( |
|
| 95 | - $name, |
|
| 96 | - $message, |
|
| 97 | - $force_update = false, |
|
| 98 | - $capability = 'manage_options', |
|
| 99 | - $cap_context = 'view persistent admin notice', |
|
| 100 | - $dismissed = false |
|
| 101 | - ) { |
|
| 102 | - $this->setName($name); |
|
| 103 | - $this->setMessage($message); |
|
| 104 | - $this->setForceUpdate($force_update); |
|
| 105 | - $this->setCapability($capability); |
|
| 106 | - $this->setCapContext($cap_context); |
|
| 107 | - $this->setDismissed($dismissed); |
|
| 108 | - add_action( |
|
| 109 | - 'AHEE__EventEspresso_core_services_notifications_PersistentAdminNoticeManager__registerNotices', |
|
| 110 | - array($this, 'registerPersistentAdminNotice') |
|
| 111 | - ); |
|
| 112 | - add_action('shutdown', array($this, 'confirmRegistered'), 999); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - public function getName() |
|
| 121 | - { |
|
| 122 | - return $this->name; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * @param string $name |
|
| 129 | - * @throws InvalidDataTypeException |
|
| 130 | - */ |
|
| 131 | - private function setName($name) |
|
| 132 | - { |
|
| 133 | - if (! is_string($name)) { |
|
| 134 | - throw new InvalidDataTypeException('$name', $name, 'string'); |
|
| 135 | - } |
|
| 136 | - $this->name = sanitize_key($name); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @return string |
|
| 143 | - */ |
|
| 144 | - public function getMessage() |
|
| 145 | - { |
|
| 146 | - return $this->message; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @param string $message |
|
| 153 | - * @throws InvalidDataTypeException |
|
| 154 | - */ |
|
| 155 | - private function setMessage($message) |
|
| 156 | - { |
|
| 157 | - if (! is_string($message)) { |
|
| 158 | - throw new InvalidDataTypeException('$message', $message, 'string'); |
|
| 159 | - } |
|
| 160 | - global $allowedtags; |
|
| 161 | - $allowedtags['br'] = array(); |
|
| 162 | - $this->message = wp_kses($message, $allowedtags); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @return bool |
|
| 169 | - */ |
|
| 170 | - public function getForceUpdate() |
|
| 171 | - { |
|
| 172 | - return $this->force_update; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * @param bool $force_update |
|
| 179 | - */ |
|
| 180 | - private function setForceUpdate($force_update) |
|
| 181 | - { |
|
| 182 | - $this->force_update = filter_var($force_update, FILTER_VALIDATE_BOOLEAN); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * @return string |
|
| 189 | - */ |
|
| 190 | - public function getCapability() |
|
| 191 | - { |
|
| 192 | - return $this->capability; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @param string $capability |
|
| 199 | - * @throws InvalidDataTypeException |
|
| 200 | - */ |
|
| 201 | - private function setCapability($capability) |
|
| 202 | - { |
|
| 203 | - if (! is_string($capability)) { |
|
| 204 | - throw new InvalidDataTypeException('$capability', $capability, 'string'); |
|
| 205 | - } |
|
| 206 | - $this->capability = ! empty($capability) ? $capability : 'manage_options'; |
|
| 207 | - } |
|
| 31 | + /** |
|
| 32 | + * @var string $name |
|
| 33 | + */ |
|
| 34 | + protected $name = ''; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var string $message |
|
| 38 | + */ |
|
| 39 | + protected $message = ''; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var boolean $force_update |
|
| 43 | + */ |
|
| 44 | + protected $force_update = false; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var string $capability |
|
| 48 | + */ |
|
| 49 | + protected $capability = 'manage_options'; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var string $cap_context |
|
| 53 | + */ |
|
| 54 | + protected $cap_context = 'view persistent admin notice'; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var boolean $dismissed |
|
| 58 | + */ |
|
| 59 | + protected $dismissed = false; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var CapCheckInterface $cap_check |
|
| 63 | + */ |
|
| 64 | + protected $cap_check; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * if true, then this notice will be deleted from the database |
|
| 68 | + * |
|
| 69 | + * @var boolean $purge |
|
| 70 | + */ |
|
| 71 | + protected $purge = false; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * gets set to true if notice is successfully registered with the PersistentAdminNoticeManager |
|
| 75 | + * if false, and WP_DEBUG is on, then an exception will be thrown in the admin footer |
|
| 76 | + * |
|
| 77 | + * @var boolean $registered |
|
| 78 | + */ |
|
| 79 | + private $registered = false; |
|
| 80 | + |
|
| 81 | + |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * PersistentAdminNotice constructor |
|
| 85 | + * |
|
| 86 | + * @param string $name [required] the name, or key of the Persistent Admin Notice to be stored |
|
| 87 | + * @param string $message [required] the message to be stored persistently until dismissed |
|
| 88 | + * @param bool $force_update enforce the reappearance of a persistent message |
|
| 89 | + * @param string $capability user capability required to view this notice |
|
| 90 | + * @param string $cap_context description for why the cap check is being performed |
|
| 91 | + * @param bool $dismissed whether or not the user has already dismissed/viewed this notice |
|
| 92 | + * @throws InvalidDataTypeException |
|
| 93 | + */ |
|
| 94 | + public function __construct( |
|
| 95 | + $name, |
|
| 96 | + $message, |
|
| 97 | + $force_update = false, |
|
| 98 | + $capability = 'manage_options', |
|
| 99 | + $cap_context = 'view persistent admin notice', |
|
| 100 | + $dismissed = false |
|
| 101 | + ) { |
|
| 102 | + $this->setName($name); |
|
| 103 | + $this->setMessage($message); |
|
| 104 | + $this->setForceUpdate($force_update); |
|
| 105 | + $this->setCapability($capability); |
|
| 106 | + $this->setCapContext($cap_context); |
|
| 107 | + $this->setDismissed($dismissed); |
|
| 108 | + add_action( |
|
| 109 | + 'AHEE__EventEspresso_core_services_notifications_PersistentAdminNoticeManager__registerNotices', |
|
| 110 | + array($this, 'registerPersistentAdminNotice') |
|
| 111 | + ); |
|
| 112 | + add_action('shutdown', array($this, 'confirmRegistered'), 999); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + public function getName() |
|
| 121 | + { |
|
| 122 | + return $this->name; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * @param string $name |
|
| 129 | + * @throws InvalidDataTypeException |
|
| 130 | + */ |
|
| 131 | + private function setName($name) |
|
| 132 | + { |
|
| 133 | + if (! is_string($name)) { |
|
| 134 | + throw new InvalidDataTypeException('$name', $name, 'string'); |
|
| 135 | + } |
|
| 136 | + $this->name = sanitize_key($name); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @return string |
|
| 143 | + */ |
|
| 144 | + public function getMessage() |
|
| 145 | + { |
|
| 146 | + return $this->message; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @param string $message |
|
| 153 | + * @throws InvalidDataTypeException |
|
| 154 | + */ |
|
| 155 | + private function setMessage($message) |
|
| 156 | + { |
|
| 157 | + if (! is_string($message)) { |
|
| 158 | + throw new InvalidDataTypeException('$message', $message, 'string'); |
|
| 159 | + } |
|
| 160 | + global $allowedtags; |
|
| 161 | + $allowedtags['br'] = array(); |
|
| 162 | + $this->message = wp_kses($message, $allowedtags); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @return bool |
|
| 169 | + */ |
|
| 170 | + public function getForceUpdate() |
|
| 171 | + { |
|
| 172 | + return $this->force_update; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * @param bool $force_update |
|
| 179 | + */ |
|
| 180 | + private function setForceUpdate($force_update) |
|
| 181 | + { |
|
| 182 | + $this->force_update = filter_var($force_update, FILTER_VALIDATE_BOOLEAN); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * @return string |
|
| 189 | + */ |
|
| 190 | + public function getCapability() |
|
| 191 | + { |
|
| 192 | + return $this->capability; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @param string $capability |
|
| 199 | + * @throws InvalidDataTypeException |
|
| 200 | + */ |
|
| 201 | + private function setCapability($capability) |
|
| 202 | + { |
|
| 203 | + if (! is_string($capability)) { |
|
| 204 | + throw new InvalidDataTypeException('$capability', $capability, 'string'); |
|
| 205 | + } |
|
| 206 | + $this->capability = ! empty($capability) ? $capability : 'manage_options'; |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | 209 | |
| 210 | 210 | |
| 211 | - /** |
|
| 212 | - * @return string |
|
| 213 | - */ |
|
| 214 | - public function getCapContext() |
|
| 215 | - { |
|
| 216 | - return $this->cap_context; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @param string $cap_context |
|
| 223 | - * @throws InvalidDataTypeException |
|
| 224 | - */ |
|
| 225 | - private function setCapContext($cap_context) |
|
| 226 | - { |
|
| 227 | - if (! is_string($cap_context)) { |
|
| 228 | - throw new InvalidDataTypeException('$cap_context', $cap_context, 'string'); |
|
| 229 | - } |
|
| 230 | - $this->cap_context = ! empty($cap_context) ? $cap_context : 'view persistent admin notice'; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * @return bool |
|
| 237 | - */ |
|
| 238 | - public function getDismissed() |
|
| 239 | - { |
|
| 240 | - return $this->dismissed; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * @param bool $dismissed |
|
| 247 | - */ |
|
| 248 | - public function setDismissed($dismissed) |
|
| 249 | - { |
|
| 250 | - $this->dismissed = filter_var($dismissed, FILTER_VALIDATE_BOOLEAN); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * @return CapCheckInterface |
|
| 257 | - * @throws InvalidDataTypeException |
|
| 258 | - */ |
|
| 259 | - public function getCapCheck() |
|
| 260 | - { |
|
| 261 | - if (! $this->cap_check instanceof CapCheckInterface) { |
|
| 262 | - $this->setCapCheck( |
|
| 263 | - new CapCheck( |
|
| 264 | - $this->capability, |
|
| 265 | - $this->cap_context |
|
| 266 | - ) |
|
| 267 | - ); |
|
| 268 | - } |
|
| 269 | - return $this->cap_check; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * @param CapCheckInterface $cap_check |
|
| 276 | - */ |
|
| 277 | - private function setCapCheck(CapCheckInterface $cap_check) |
|
| 278 | - { |
|
| 279 | - $this->cap_check = $cap_check; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * @return bool |
|
| 286 | - */ |
|
| 287 | - public function getPurge() |
|
| 288 | - { |
|
| 289 | - return $this->purge; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * @param bool $purge |
|
| 296 | - */ |
|
| 297 | - public function setPurge($purge) |
|
| 298 | - { |
|
| 299 | - $this->purge = filter_var($purge, FILTER_VALIDATE_BOOLEAN); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * given a valid PersistentAdminNotice Collection, |
|
| 306 | - * this notice will be added if it is not already found in the collection (using its name as the identifier) |
|
| 307 | - * if an existing notice is found that has already been dismissed, |
|
| 308 | - * but we are overriding with a forced update, then we will toggle its dismissed state, |
|
| 309 | - * so that the notice is displayed again |
|
| 310 | - * |
|
| 311 | - * @param Collection $persistent_admin_notice_collection |
|
| 312 | - * @throws InvalidEntityException |
|
| 313 | - * @throws InvalidDataTypeException |
|
| 314 | - */ |
|
| 315 | - public function registerPersistentAdminNotice(Collection $persistent_admin_notice_collection) |
|
| 316 | - { |
|
| 317 | - if ($this->registered) { |
|
| 318 | - return; |
|
| 319 | - } |
|
| 320 | - // first check if this notice has already been added to the collection |
|
| 321 | - if ($persistent_admin_notice_collection->has($this->name)) { |
|
| 322 | - /** @var PersistentAdminNotice $existing */ |
|
| 323 | - $existing = $persistent_admin_notice_collection->get($this->name); |
|
| 324 | - // we don't need to add it again (we can't actually) |
|
| 325 | - // but if it has already been dismissed, and we are overriding with a forced update |
|
| 326 | - if ($existing->getDismissed() && $this->getForceUpdate()) { |
|
| 327 | - // then toggle the notice's dismissed state to true |
|
| 328 | - // so that it gets displayed again |
|
| 329 | - $existing->setDismissed(false); |
|
| 330 | - // and make sure the message is set |
|
| 331 | - $existing->setMessage($this->message); |
|
| 332 | - } |
|
| 333 | - } else { |
|
| 334 | - $persistent_admin_notice_collection->add($this, $this->name); |
|
| 335 | - } |
|
| 336 | - $this->registered = true; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * @throws Exception |
|
| 343 | - */ |
|
| 344 | - public function confirmRegistered() |
|
| 345 | - { |
|
| 346 | - if (! apply_filters('PersistentAdminNoticeManager__registerAndSaveNotices__complete', false)) { |
|
| 347 | - PersistentAdminNoticeManager::loadRegisterAndSaveNotices(); |
|
| 348 | - } |
|
| 349 | - if (! $this->registered && WP_DEBUG) { |
|
| 350 | - throw new DomainException( |
|
| 351 | - sprintf( |
|
| 352 | - esc_html__( |
|
| 353 | - 'The "%1$s" PersistentAdminNotice was not successfully registered. Please ensure that it is being created prior to either the "admin_notices" or "network_admin_notices" hooks being triggered.', |
|
| 354 | - 'event_espresso' |
|
| 355 | - ), |
|
| 356 | - $this->name |
|
| 357 | - ) |
|
| 358 | - ); |
|
| 359 | - } |
|
| 360 | - } |
|
| 211 | + /** |
|
| 212 | + * @return string |
|
| 213 | + */ |
|
| 214 | + public function getCapContext() |
|
| 215 | + { |
|
| 216 | + return $this->cap_context; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @param string $cap_context |
|
| 223 | + * @throws InvalidDataTypeException |
|
| 224 | + */ |
|
| 225 | + private function setCapContext($cap_context) |
|
| 226 | + { |
|
| 227 | + if (! is_string($cap_context)) { |
|
| 228 | + throw new InvalidDataTypeException('$cap_context', $cap_context, 'string'); |
|
| 229 | + } |
|
| 230 | + $this->cap_context = ! empty($cap_context) ? $cap_context : 'view persistent admin notice'; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * @return bool |
|
| 237 | + */ |
|
| 238 | + public function getDismissed() |
|
| 239 | + { |
|
| 240 | + return $this->dismissed; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * @param bool $dismissed |
|
| 247 | + */ |
|
| 248 | + public function setDismissed($dismissed) |
|
| 249 | + { |
|
| 250 | + $this->dismissed = filter_var($dismissed, FILTER_VALIDATE_BOOLEAN); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * @return CapCheckInterface |
|
| 257 | + * @throws InvalidDataTypeException |
|
| 258 | + */ |
|
| 259 | + public function getCapCheck() |
|
| 260 | + { |
|
| 261 | + if (! $this->cap_check instanceof CapCheckInterface) { |
|
| 262 | + $this->setCapCheck( |
|
| 263 | + new CapCheck( |
|
| 264 | + $this->capability, |
|
| 265 | + $this->cap_context |
|
| 266 | + ) |
|
| 267 | + ); |
|
| 268 | + } |
|
| 269 | + return $this->cap_check; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * @param CapCheckInterface $cap_check |
|
| 276 | + */ |
|
| 277 | + private function setCapCheck(CapCheckInterface $cap_check) |
|
| 278 | + { |
|
| 279 | + $this->cap_check = $cap_check; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * @return bool |
|
| 286 | + */ |
|
| 287 | + public function getPurge() |
|
| 288 | + { |
|
| 289 | + return $this->purge; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * @param bool $purge |
|
| 296 | + */ |
|
| 297 | + public function setPurge($purge) |
|
| 298 | + { |
|
| 299 | + $this->purge = filter_var($purge, FILTER_VALIDATE_BOOLEAN); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * given a valid PersistentAdminNotice Collection, |
|
| 306 | + * this notice will be added if it is not already found in the collection (using its name as the identifier) |
|
| 307 | + * if an existing notice is found that has already been dismissed, |
|
| 308 | + * but we are overriding with a forced update, then we will toggle its dismissed state, |
|
| 309 | + * so that the notice is displayed again |
|
| 310 | + * |
|
| 311 | + * @param Collection $persistent_admin_notice_collection |
|
| 312 | + * @throws InvalidEntityException |
|
| 313 | + * @throws InvalidDataTypeException |
|
| 314 | + */ |
|
| 315 | + public function registerPersistentAdminNotice(Collection $persistent_admin_notice_collection) |
|
| 316 | + { |
|
| 317 | + if ($this->registered) { |
|
| 318 | + return; |
|
| 319 | + } |
|
| 320 | + // first check if this notice has already been added to the collection |
|
| 321 | + if ($persistent_admin_notice_collection->has($this->name)) { |
|
| 322 | + /** @var PersistentAdminNotice $existing */ |
|
| 323 | + $existing = $persistent_admin_notice_collection->get($this->name); |
|
| 324 | + // we don't need to add it again (we can't actually) |
|
| 325 | + // but if it has already been dismissed, and we are overriding with a forced update |
|
| 326 | + if ($existing->getDismissed() && $this->getForceUpdate()) { |
|
| 327 | + // then toggle the notice's dismissed state to true |
|
| 328 | + // so that it gets displayed again |
|
| 329 | + $existing->setDismissed(false); |
|
| 330 | + // and make sure the message is set |
|
| 331 | + $existing->setMessage($this->message); |
|
| 332 | + } |
|
| 333 | + } else { |
|
| 334 | + $persistent_admin_notice_collection->add($this, $this->name); |
|
| 335 | + } |
|
| 336 | + $this->registered = true; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * @throws Exception |
|
| 343 | + */ |
|
| 344 | + public function confirmRegistered() |
|
| 345 | + { |
|
| 346 | + if (! apply_filters('PersistentAdminNoticeManager__registerAndSaveNotices__complete', false)) { |
|
| 347 | + PersistentAdminNoticeManager::loadRegisterAndSaveNotices(); |
|
| 348 | + } |
|
| 349 | + if (! $this->registered && WP_DEBUG) { |
|
| 350 | + throw new DomainException( |
|
| 351 | + sprintf( |
|
| 352 | + esc_html__( |
|
| 353 | + 'The "%1$s" PersistentAdminNotice was not successfully registered. Please ensure that it is being created prior to either the "admin_notices" or "network_admin_notices" hooks being triggered.', |
|
| 354 | + 'event_espresso' |
|
| 355 | + ), |
|
| 356 | + $this->name |
|
| 357 | + ) |
|
| 358 | + ); |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | 361 | |
| 362 | 362 | |
| 363 | 363 | } |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | */ |
| 131 | 131 | private function setName($name) |
| 132 | 132 | { |
| 133 | - if (! is_string($name)) { |
|
| 133 | + if ( ! is_string($name)) { |
|
| 134 | 134 | throw new InvalidDataTypeException('$name', $name, 'string'); |
| 135 | 135 | } |
| 136 | 136 | $this->name = sanitize_key($name); |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | */ |
| 155 | 155 | private function setMessage($message) |
| 156 | 156 | { |
| 157 | - if (! is_string($message)) { |
|
| 157 | + if ( ! is_string($message)) { |
|
| 158 | 158 | throw new InvalidDataTypeException('$message', $message, 'string'); |
| 159 | 159 | } |
| 160 | 160 | global $allowedtags; |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | */ |
| 201 | 201 | private function setCapability($capability) |
| 202 | 202 | { |
| 203 | - if (! is_string($capability)) { |
|
| 203 | + if ( ! is_string($capability)) { |
|
| 204 | 204 | throw new InvalidDataTypeException('$capability', $capability, 'string'); |
| 205 | 205 | } |
| 206 | 206 | $this->capability = ! empty($capability) ? $capability : 'manage_options'; |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | */ |
| 225 | 225 | private function setCapContext($cap_context) |
| 226 | 226 | { |
| 227 | - if (! is_string($cap_context)) { |
|
| 227 | + if ( ! is_string($cap_context)) { |
|
| 228 | 228 | throw new InvalidDataTypeException('$cap_context', $cap_context, 'string'); |
| 229 | 229 | } |
| 230 | 230 | $this->cap_context = ! empty($cap_context) ? $cap_context : 'view persistent admin notice'; |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | */ |
| 259 | 259 | public function getCapCheck() |
| 260 | 260 | { |
| 261 | - if (! $this->cap_check instanceof CapCheckInterface) { |
|
| 261 | + if ( ! $this->cap_check instanceof CapCheckInterface) { |
|
| 262 | 262 | $this->setCapCheck( |
| 263 | 263 | new CapCheck( |
| 264 | 264 | $this->capability, |
@@ -343,10 +343,10 @@ discard block |
||
| 343 | 343 | */ |
| 344 | 344 | public function confirmRegistered() |
| 345 | 345 | { |
| 346 | - if (! apply_filters('PersistentAdminNoticeManager__registerAndSaveNotices__complete', false)) { |
|
| 346 | + if ( ! apply_filters('PersistentAdminNoticeManager__registerAndSaveNotices__complete', false)) { |
|
| 347 | 347 | PersistentAdminNoticeManager::loadRegisterAndSaveNotices(); |
| 348 | 348 | } |
| 349 | - if (! $this->registered && WP_DEBUG) { |
|
| 349 | + if ( ! $this->registered && WP_DEBUG) { |
|
| 350 | 350 | throw new DomainException( |
| 351 | 351 | sprintf( |
| 352 | 352 | esc_html__( |
@@ -32,390 +32,390 @@ |
||
| 32 | 32 | class PersistentAdminNoticeManager |
| 33 | 33 | { |
| 34 | 34 | |
| 35 | - const WP_OPTION_KEY = 'ee_pers_admin_notices'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var Collection|PersistentAdminNotice[] $notice_collection |
|
| 39 | - */ |
|
| 40 | - private $notice_collection; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * if AJAX is not enabled, then the return URL will be used for redirecting back to the admin page where the |
|
| 44 | - * persistent admin notice was displayed, and ultimately dismissed from. |
|
| 45 | - * |
|
| 46 | - * @var string $return_url |
|
| 47 | - */ |
|
| 48 | - private $return_url; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var CapabilitiesChecker $capabilities_checker |
|
| 52 | - */ |
|
| 53 | - private $capabilities_checker; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @var EE_Request $request |
|
| 57 | - */ |
|
| 58 | - private $request; |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * PersistentAdminNoticeManager constructor |
|
| 64 | - * |
|
| 65 | - * @param string $return_url where to redirect to after dismissing notices |
|
| 66 | - * @param CapabilitiesChecker $capabilities_checker |
|
| 67 | - * @param EE_Request $request |
|
| 68 | - * @throws InvalidDataTypeException |
|
| 69 | - */ |
|
| 70 | - public function __construct($return_url = '', CapabilitiesChecker $capabilities_checker, EE_Request $request) |
|
| 71 | - { |
|
| 72 | - $this->setReturnUrl($return_url); |
|
| 73 | - $this->capabilities_checker = $capabilities_checker; |
|
| 74 | - $this->request = $request; |
|
| 75 | - // setup up notices at priority 9 because `EE_Admin::display_admin_notices()` runs at priority 10, |
|
| 76 | - // and we want to retrieve and generate any nag notices at the last possible moment |
|
| 77 | - add_action('admin_notices', array($this, 'displayNotices'), 9); |
|
| 78 | - add_action('network_admin_notices', array($this, 'displayNotices'), 9); |
|
| 79 | - add_action('wp_ajax_dismiss_ee_nag_notice', array($this, 'dismissNotice')); |
|
| 80 | - add_action('shutdown', array($this, 'registerAndSaveNotices'), 998); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param string $return_url |
|
| 87 | - * @throws InvalidDataTypeException |
|
| 88 | - */ |
|
| 89 | - public function setReturnUrl($return_url) |
|
| 90 | - { |
|
| 91 | - if (! is_string($return_url)) { |
|
| 92 | - throw new InvalidDataTypeException('$return_url', $return_url, 'string'); |
|
| 93 | - } |
|
| 94 | - $this->return_url = $return_url; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @return Collection |
|
| 101 | - * @throws InvalidEntityException |
|
| 102 | - * @throws InvalidInterfaceException |
|
| 103 | - * @throws InvalidDataTypeException |
|
| 104 | - * @throws DomainException |
|
| 105 | - */ |
|
| 106 | - protected function getPersistentAdminNoticeCollection() |
|
| 107 | - { |
|
| 108 | - if (! $this->notice_collection instanceof Collection) { |
|
| 109 | - $this->notice_collection = new Collection( |
|
| 110 | - 'EventEspresso\core\domain\entities\notifications\PersistentAdminNotice' |
|
| 111 | - ); |
|
| 112 | - $this->retrieveStoredNotices(); |
|
| 113 | - $this->registerNotices(); |
|
| 114 | - } |
|
| 115 | - return $this->notice_collection; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * generates PersistentAdminNotice objects for all non-dismissed notices saved to the db |
|
| 122 | - * |
|
| 123 | - * @return void |
|
| 124 | - * @throws InvalidEntityException |
|
| 125 | - * @throws DomainException |
|
| 126 | - * @throws InvalidDataTypeException |
|
| 127 | - */ |
|
| 128 | - protected function retrieveStoredNotices() |
|
| 129 | - { |
|
| 130 | - $persistent_admin_notices = get_option(PersistentAdminNoticeManager::WP_OPTION_KEY, array()); |
|
| 131 | - // \EEH_Debug_Tools::printr($persistent_admin_notices, '$persistent_admin_notices', __FILE__, __LINE__); |
|
| 132 | - if (! empty($persistent_admin_notices)) { |
|
| 133 | - foreach ($persistent_admin_notices as $name => $details) { |
|
| 134 | - if (is_array($details)) { |
|
| 135 | - if ( |
|
| 136 | - ! isset( |
|
| 137 | - $details['message'], |
|
| 138 | - $details['capability'], |
|
| 139 | - $details['cap_context'], |
|
| 140 | - $details['dismissed'] |
|
| 141 | - ) |
|
| 142 | - ) { |
|
| 143 | - throw new DomainException( |
|
| 144 | - sprintf( |
|
| 145 | - esc_html__( |
|
| 146 | - 'The "%1$s" PersistentAdminNotice could not be retrieved from the database.', |
|
| 147 | - 'event_espresso' |
|
| 148 | - ), |
|
| 149 | - $name |
|
| 150 | - ) |
|
| 151 | - ); |
|
| 152 | - } |
|
| 153 | - // new format for nag notices |
|
| 154 | - $this->notice_collection->add( |
|
| 155 | - new PersistentAdminNotice( |
|
| 156 | - $name, |
|
| 157 | - $details['message'], |
|
| 158 | - false, |
|
| 159 | - $details['capability'], |
|
| 160 | - $details['cap_context'], |
|
| 161 | - $details['dismissed'] |
|
| 162 | - ), |
|
| 163 | - $name |
|
| 164 | - ); |
|
| 165 | - } else { |
|
| 166 | - try { |
|
| 167 | - // old nag notices, that we want to convert to the new format |
|
| 168 | - $this->notice_collection->add( |
|
| 169 | - new PersistentAdminNotice( |
|
| 170 | - $name, |
|
| 171 | - (string)$details, |
|
| 172 | - false, |
|
| 173 | - '', |
|
| 174 | - '', |
|
| 175 | - empty($details) |
|
| 176 | - ), |
|
| 177 | - $name |
|
| 178 | - ); |
|
| 179 | - } catch (Exception $e) { |
|
| 180 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - // each notice will self register when the action hook in registerNotices is triggered |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * exposes the Persistent Admin Notice Collection via an action |
|
| 192 | - * so that PersistentAdminNotice objects can be added and/or removed |
|
| 193 | - * without compromising the actual collection like a filter would |
|
| 194 | - */ |
|
| 195 | - protected function registerNotices() |
|
| 196 | - { |
|
| 197 | - do_action( |
|
| 198 | - 'AHEE__EventEspresso_core_services_notifications_PersistentAdminNoticeManager__registerNotices', |
|
| 199 | - $this->notice_collection |
|
| 200 | - ); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * @throws DomainException |
|
| 207 | - * @throws InvalidClassException |
|
| 208 | - * @throws InvalidDataTypeException |
|
| 209 | - * @throws InvalidInterfaceException |
|
| 210 | - * @throws InvalidEntityException |
|
| 211 | - */ |
|
| 212 | - public function displayNotices() |
|
| 213 | - { |
|
| 214 | - $this->notice_collection = $this->getPersistentAdminNoticeCollection(); |
|
| 215 | - if ($this->notice_collection->hasObjects()) { |
|
| 216 | - $enqueue_assets = false; |
|
| 217 | - // and display notices |
|
| 218 | - foreach ($this->notice_collection as $persistent_admin_notice) { |
|
| 219 | - /** @var PersistentAdminNotice $persistent_admin_notice */ |
|
| 220 | - // don't display notices that have already been dismissed |
|
| 221 | - if ($persistent_admin_notice->getDismissed()) { |
|
| 222 | - continue; |
|
| 223 | - } |
|
| 224 | - try { |
|
| 225 | - $this->capabilities_checker->processCapCheck( |
|
| 226 | - $persistent_admin_notice->getCapCheck() |
|
| 227 | - ); |
|
| 228 | - } catch (InsufficientPermissionsException $e) { |
|
| 229 | - // user does not have required cap, so skip to next notice |
|
| 230 | - // and just eat the exception - nom nom nom nom |
|
| 231 | - continue; |
|
| 232 | - } |
|
| 233 | - if ($persistent_admin_notice->getMessage() === '') { |
|
| 234 | - continue; |
|
| 235 | - } |
|
| 236 | - $this->displayPersistentAdminNotice($persistent_admin_notice); |
|
| 237 | - $enqueue_assets = true; |
|
| 238 | - } |
|
| 239 | - if ($enqueue_assets) { |
|
| 240 | - $this->enqueueAssets(); |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * does what it's named |
|
| 249 | - * |
|
| 250 | - * @return void |
|
| 251 | - */ |
|
| 252 | - public function enqueueAssets() |
|
| 253 | - { |
|
| 254 | - wp_register_script( |
|
| 255 | - 'espresso_core', |
|
| 256 | - EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
|
| 257 | - array('jquery'), |
|
| 258 | - EVENT_ESPRESSO_VERSION, |
|
| 259 | - true |
|
| 260 | - ); |
|
| 261 | - wp_register_script( |
|
| 262 | - 'ee_error_js', |
|
| 263 | - EE_GLOBAL_ASSETS_URL . 'scripts/EE_Error.js', |
|
| 264 | - array('espresso_core'), |
|
| 265 | - EVENT_ESPRESSO_VERSION, |
|
| 266 | - true |
|
| 267 | - ); |
|
| 268 | - wp_localize_script( |
|
| 269 | - 'ee_error_js', |
|
| 270 | - 'ee_dismiss', |
|
| 271 | - array( |
|
| 272 | - 'return_url' => urlencode($this->return_url), |
|
| 273 | - 'ajax_url' => WP_AJAX_URL, |
|
| 274 | - 'unknown_error' => esc_html__( |
|
| 275 | - 'An unknown error has occurred on the server while attempting to dismiss this notice.', |
|
| 276 | - 'event_espresso' |
|
| 277 | - ), |
|
| 278 | - ) |
|
| 279 | - ); |
|
| 280 | - wp_enqueue_script('ee_error_js'); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * displayPersistentAdminNoticeHtml |
|
| 287 | - * |
|
| 288 | - * @param PersistentAdminNotice $persistent_admin_notice |
|
| 289 | - */ |
|
| 290 | - protected function displayPersistentAdminNotice(PersistentAdminNotice $persistent_admin_notice) |
|
| 291 | - { |
|
| 292 | - // used in template |
|
| 293 | - $persistent_admin_notice_name = $persistent_admin_notice->getName(); |
|
| 294 | - $persistent_admin_notice_message = $persistent_admin_notice->getMessage(); |
|
| 295 | - require EE_TEMPLATES . DS . 'notifications' . DS . 'persistent_admin_notice.template.php'; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * dismissNotice |
|
| 302 | - * |
|
| 303 | - * @param string $pan_name the name, or key of the Persistent Admin Notice to be dismissed |
|
| 304 | - * @param bool $purge if true, then delete it from the db |
|
| 305 | - * @param bool $return forget all of this AJAX or redirect nonsense, and just return |
|
| 306 | - * @return void |
|
| 307 | - * @throws InvalidEntityException |
|
| 308 | - * @throws InvalidInterfaceException |
|
| 309 | - * @throws InvalidDataTypeException |
|
| 310 | - * @throws DomainException |
|
| 311 | - */ |
|
| 312 | - public function dismissNotice($pan_name = '', $purge = false, $return = false) |
|
| 313 | - { |
|
| 314 | - $pan_name = $this->request->get('ee_nag_notice', $pan_name); |
|
| 315 | - $this->notice_collection = $this->getPersistentAdminNoticeCollection(); |
|
| 316 | - if (! empty($pan_name) && $this->notice_collection->has($pan_name)) { |
|
| 317 | - /** @var PersistentAdminNotice $persistent_admin_notice */ |
|
| 318 | - $persistent_admin_notice = $this->notice_collection->get($pan_name); |
|
| 319 | - $persistent_admin_notice->setDismissed(true); |
|
| 320 | - $persistent_admin_notice->setPurge($purge); |
|
| 321 | - $this->saveNotices(); |
|
| 322 | - } |
|
| 323 | - if ($return) { |
|
| 324 | - return; |
|
| 325 | - } |
|
| 326 | - if ($this->request->ajax) { |
|
| 327 | - // grab any notices and concatenate into string |
|
| 328 | - echo wp_json_encode( |
|
| 329 | - array( |
|
| 330 | - 'errors' => implode('<br />', EE_Error::get_notices(false)), |
|
| 331 | - ) |
|
| 332 | - ); |
|
| 333 | - exit(); |
|
| 334 | - } |
|
| 335 | - // save errors to a transient to be displayed on next request (after redirect) |
|
| 336 | - EE_Error::get_notices(false, true); |
|
| 337 | - wp_safe_redirect( |
|
| 338 | - urldecode( |
|
| 339 | - $this->request->get('return_url', '') |
|
| 340 | - ) |
|
| 341 | - ); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * saveNotices |
|
| 348 | - * |
|
| 349 | - * @throws DomainException |
|
| 350 | - * @throws InvalidDataTypeException |
|
| 351 | - * @throws InvalidInterfaceException |
|
| 352 | - * @throws InvalidEntityException |
|
| 353 | - */ |
|
| 354 | - public function saveNotices() |
|
| 355 | - { |
|
| 356 | - $this->notice_collection = $this->getPersistentAdminNoticeCollection(); |
|
| 357 | - if ($this->notice_collection->hasObjects()) { |
|
| 358 | - $persistent_admin_notices = get_option(PersistentAdminNoticeManager::WP_OPTION_KEY, array()); |
|
| 359 | - //maybe initialize persistent_admin_notices |
|
| 360 | - if (empty($persistent_admin_notices)) { |
|
| 361 | - add_option(PersistentAdminNoticeManager::WP_OPTION_KEY, array(), '', 'no'); |
|
| 362 | - } |
|
| 363 | - foreach ($this->notice_collection as $persistent_admin_notice) { |
|
| 364 | - // are we deleting this notice ? |
|
| 365 | - if ($persistent_admin_notice->getPurge()) { |
|
| 366 | - unset($persistent_admin_notices[$persistent_admin_notice->getName()]); |
|
| 367 | - } else { |
|
| 368 | - /** @var PersistentAdminNotice $persistent_admin_notice */ |
|
| 369 | - $persistent_admin_notices[$persistent_admin_notice->getName()] = array( |
|
| 370 | - 'message' => $persistent_admin_notice->getMessage(), |
|
| 371 | - 'capability' => $persistent_admin_notice->getCapability(), |
|
| 372 | - 'cap_context' => $persistent_admin_notice->getCapContext(), |
|
| 373 | - 'dismissed' => $persistent_admin_notice->getDismissed(), |
|
| 374 | - ); |
|
| 375 | - } |
|
| 376 | - } |
|
| 377 | - update_option(PersistentAdminNoticeManager::WP_OPTION_KEY, $persistent_admin_notices); |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * @throws DomainException |
|
| 385 | - * @throws InvalidDataTypeException |
|
| 386 | - * @throws InvalidEntityException |
|
| 387 | - * @throws InvalidInterfaceException |
|
| 388 | - */ |
|
| 389 | - public function registerAndSaveNotices() |
|
| 390 | - { |
|
| 391 | - $this->getPersistentAdminNoticeCollection(); |
|
| 392 | - $this->registerNotices(); |
|
| 393 | - $this->saveNotices(); |
|
| 394 | - add_filter( |
|
| 395 | - 'PersistentAdminNoticeManager__registerAndSaveNotices__complete', |
|
| 396 | - '__return_true' |
|
| 397 | - ); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * @throws DomainException |
|
| 403 | - * @throws InvalidDataTypeException |
|
| 404 | - * @throws InvalidEntityException |
|
| 405 | - * @throws InvalidInterfaceException |
|
| 406 | - * @throws InvalidArgumentException |
|
| 407 | - */ |
|
| 408 | - public static function loadRegisterAndSaveNotices() |
|
| 409 | - { |
|
| 410 | - /** @var PersistentAdminNoticeManager $persistent_admin_notice_manager */ |
|
| 411 | - $persistent_admin_notice_manager = LoaderFactory::getLoader()->getShared( |
|
| 412 | - 'EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 413 | - ); |
|
| 414 | - // if shutdown has already run, then call registerAndSaveNotices() manually |
|
| 415 | - if(did_action('shutdown')){ |
|
| 416 | - $persistent_admin_notice_manager->registerAndSaveNotices(); |
|
| 417 | - } |
|
| 418 | - } |
|
| 35 | + const WP_OPTION_KEY = 'ee_pers_admin_notices'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var Collection|PersistentAdminNotice[] $notice_collection |
|
| 39 | + */ |
|
| 40 | + private $notice_collection; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * if AJAX is not enabled, then the return URL will be used for redirecting back to the admin page where the |
|
| 44 | + * persistent admin notice was displayed, and ultimately dismissed from. |
|
| 45 | + * |
|
| 46 | + * @var string $return_url |
|
| 47 | + */ |
|
| 48 | + private $return_url; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var CapabilitiesChecker $capabilities_checker |
|
| 52 | + */ |
|
| 53 | + private $capabilities_checker; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @var EE_Request $request |
|
| 57 | + */ |
|
| 58 | + private $request; |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * PersistentAdminNoticeManager constructor |
|
| 64 | + * |
|
| 65 | + * @param string $return_url where to redirect to after dismissing notices |
|
| 66 | + * @param CapabilitiesChecker $capabilities_checker |
|
| 67 | + * @param EE_Request $request |
|
| 68 | + * @throws InvalidDataTypeException |
|
| 69 | + */ |
|
| 70 | + public function __construct($return_url = '', CapabilitiesChecker $capabilities_checker, EE_Request $request) |
|
| 71 | + { |
|
| 72 | + $this->setReturnUrl($return_url); |
|
| 73 | + $this->capabilities_checker = $capabilities_checker; |
|
| 74 | + $this->request = $request; |
|
| 75 | + // setup up notices at priority 9 because `EE_Admin::display_admin_notices()` runs at priority 10, |
|
| 76 | + // and we want to retrieve and generate any nag notices at the last possible moment |
|
| 77 | + add_action('admin_notices', array($this, 'displayNotices'), 9); |
|
| 78 | + add_action('network_admin_notices', array($this, 'displayNotices'), 9); |
|
| 79 | + add_action('wp_ajax_dismiss_ee_nag_notice', array($this, 'dismissNotice')); |
|
| 80 | + add_action('shutdown', array($this, 'registerAndSaveNotices'), 998); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param string $return_url |
|
| 87 | + * @throws InvalidDataTypeException |
|
| 88 | + */ |
|
| 89 | + public function setReturnUrl($return_url) |
|
| 90 | + { |
|
| 91 | + if (! is_string($return_url)) { |
|
| 92 | + throw new InvalidDataTypeException('$return_url', $return_url, 'string'); |
|
| 93 | + } |
|
| 94 | + $this->return_url = $return_url; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @return Collection |
|
| 101 | + * @throws InvalidEntityException |
|
| 102 | + * @throws InvalidInterfaceException |
|
| 103 | + * @throws InvalidDataTypeException |
|
| 104 | + * @throws DomainException |
|
| 105 | + */ |
|
| 106 | + protected function getPersistentAdminNoticeCollection() |
|
| 107 | + { |
|
| 108 | + if (! $this->notice_collection instanceof Collection) { |
|
| 109 | + $this->notice_collection = new Collection( |
|
| 110 | + 'EventEspresso\core\domain\entities\notifications\PersistentAdminNotice' |
|
| 111 | + ); |
|
| 112 | + $this->retrieveStoredNotices(); |
|
| 113 | + $this->registerNotices(); |
|
| 114 | + } |
|
| 115 | + return $this->notice_collection; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * generates PersistentAdminNotice objects for all non-dismissed notices saved to the db |
|
| 122 | + * |
|
| 123 | + * @return void |
|
| 124 | + * @throws InvalidEntityException |
|
| 125 | + * @throws DomainException |
|
| 126 | + * @throws InvalidDataTypeException |
|
| 127 | + */ |
|
| 128 | + protected function retrieveStoredNotices() |
|
| 129 | + { |
|
| 130 | + $persistent_admin_notices = get_option(PersistentAdminNoticeManager::WP_OPTION_KEY, array()); |
|
| 131 | + // \EEH_Debug_Tools::printr($persistent_admin_notices, '$persistent_admin_notices', __FILE__, __LINE__); |
|
| 132 | + if (! empty($persistent_admin_notices)) { |
|
| 133 | + foreach ($persistent_admin_notices as $name => $details) { |
|
| 134 | + if (is_array($details)) { |
|
| 135 | + if ( |
|
| 136 | + ! isset( |
|
| 137 | + $details['message'], |
|
| 138 | + $details['capability'], |
|
| 139 | + $details['cap_context'], |
|
| 140 | + $details['dismissed'] |
|
| 141 | + ) |
|
| 142 | + ) { |
|
| 143 | + throw new DomainException( |
|
| 144 | + sprintf( |
|
| 145 | + esc_html__( |
|
| 146 | + 'The "%1$s" PersistentAdminNotice could not be retrieved from the database.', |
|
| 147 | + 'event_espresso' |
|
| 148 | + ), |
|
| 149 | + $name |
|
| 150 | + ) |
|
| 151 | + ); |
|
| 152 | + } |
|
| 153 | + // new format for nag notices |
|
| 154 | + $this->notice_collection->add( |
|
| 155 | + new PersistentAdminNotice( |
|
| 156 | + $name, |
|
| 157 | + $details['message'], |
|
| 158 | + false, |
|
| 159 | + $details['capability'], |
|
| 160 | + $details['cap_context'], |
|
| 161 | + $details['dismissed'] |
|
| 162 | + ), |
|
| 163 | + $name |
|
| 164 | + ); |
|
| 165 | + } else { |
|
| 166 | + try { |
|
| 167 | + // old nag notices, that we want to convert to the new format |
|
| 168 | + $this->notice_collection->add( |
|
| 169 | + new PersistentAdminNotice( |
|
| 170 | + $name, |
|
| 171 | + (string)$details, |
|
| 172 | + false, |
|
| 173 | + '', |
|
| 174 | + '', |
|
| 175 | + empty($details) |
|
| 176 | + ), |
|
| 177 | + $name |
|
| 178 | + ); |
|
| 179 | + } catch (Exception $e) { |
|
| 180 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + // each notice will self register when the action hook in registerNotices is triggered |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * exposes the Persistent Admin Notice Collection via an action |
|
| 192 | + * so that PersistentAdminNotice objects can be added and/or removed |
|
| 193 | + * without compromising the actual collection like a filter would |
|
| 194 | + */ |
|
| 195 | + protected function registerNotices() |
|
| 196 | + { |
|
| 197 | + do_action( |
|
| 198 | + 'AHEE__EventEspresso_core_services_notifications_PersistentAdminNoticeManager__registerNotices', |
|
| 199 | + $this->notice_collection |
|
| 200 | + ); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * @throws DomainException |
|
| 207 | + * @throws InvalidClassException |
|
| 208 | + * @throws InvalidDataTypeException |
|
| 209 | + * @throws InvalidInterfaceException |
|
| 210 | + * @throws InvalidEntityException |
|
| 211 | + */ |
|
| 212 | + public function displayNotices() |
|
| 213 | + { |
|
| 214 | + $this->notice_collection = $this->getPersistentAdminNoticeCollection(); |
|
| 215 | + if ($this->notice_collection->hasObjects()) { |
|
| 216 | + $enqueue_assets = false; |
|
| 217 | + // and display notices |
|
| 218 | + foreach ($this->notice_collection as $persistent_admin_notice) { |
|
| 219 | + /** @var PersistentAdminNotice $persistent_admin_notice */ |
|
| 220 | + // don't display notices that have already been dismissed |
|
| 221 | + if ($persistent_admin_notice->getDismissed()) { |
|
| 222 | + continue; |
|
| 223 | + } |
|
| 224 | + try { |
|
| 225 | + $this->capabilities_checker->processCapCheck( |
|
| 226 | + $persistent_admin_notice->getCapCheck() |
|
| 227 | + ); |
|
| 228 | + } catch (InsufficientPermissionsException $e) { |
|
| 229 | + // user does not have required cap, so skip to next notice |
|
| 230 | + // and just eat the exception - nom nom nom nom |
|
| 231 | + continue; |
|
| 232 | + } |
|
| 233 | + if ($persistent_admin_notice->getMessage() === '') { |
|
| 234 | + continue; |
|
| 235 | + } |
|
| 236 | + $this->displayPersistentAdminNotice($persistent_admin_notice); |
|
| 237 | + $enqueue_assets = true; |
|
| 238 | + } |
|
| 239 | + if ($enqueue_assets) { |
|
| 240 | + $this->enqueueAssets(); |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * does what it's named |
|
| 249 | + * |
|
| 250 | + * @return void |
|
| 251 | + */ |
|
| 252 | + public function enqueueAssets() |
|
| 253 | + { |
|
| 254 | + wp_register_script( |
|
| 255 | + 'espresso_core', |
|
| 256 | + EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
|
| 257 | + array('jquery'), |
|
| 258 | + EVENT_ESPRESSO_VERSION, |
|
| 259 | + true |
|
| 260 | + ); |
|
| 261 | + wp_register_script( |
|
| 262 | + 'ee_error_js', |
|
| 263 | + EE_GLOBAL_ASSETS_URL . 'scripts/EE_Error.js', |
|
| 264 | + array('espresso_core'), |
|
| 265 | + EVENT_ESPRESSO_VERSION, |
|
| 266 | + true |
|
| 267 | + ); |
|
| 268 | + wp_localize_script( |
|
| 269 | + 'ee_error_js', |
|
| 270 | + 'ee_dismiss', |
|
| 271 | + array( |
|
| 272 | + 'return_url' => urlencode($this->return_url), |
|
| 273 | + 'ajax_url' => WP_AJAX_URL, |
|
| 274 | + 'unknown_error' => esc_html__( |
|
| 275 | + 'An unknown error has occurred on the server while attempting to dismiss this notice.', |
|
| 276 | + 'event_espresso' |
|
| 277 | + ), |
|
| 278 | + ) |
|
| 279 | + ); |
|
| 280 | + wp_enqueue_script('ee_error_js'); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * displayPersistentAdminNoticeHtml |
|
| 287 | + * |
|
| 288 | + * @param PersistentAdminNotice $persistent_admin_notice |
|
| 289 | + */ |
|
| 290 | + protected function displayPersistentAdminNotice(PersistentAdminNotice $persistent_admin_notice) |
|
| 291 | + { |
|
| 292 | + // used in template |
|
| 293 | + $persistent_admin_notice_name = $persistent_admin_notice->getName(); |
|
| 294 | + $persistent_admin_notice_message = $persistent_admin_notice->getMessage(); |
|
| 295 | + require EE_TEMPLATES . DS . 'notifications' . DS . 'persistent_admin_notice.template.php'; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * dismissNotice |
|
| 302 | + * |
|
| 303 | + * @param string $pan_name the name, or key of the Persistent Admin Notice to be dismissed |
|
| 304 | + * @param bool $purge if true, then delete it from the db |
|
| 305 | + * @param bool $return forget all of this AJAX or redirect nonsense, and just return |
|
| 306 | + * @return void |
|
| 307 | + * @throws InvalidEntityException |
|
| 308 | + * @throws InvalidInterfaceException |
|
| 309 | + * @throws InvalidDataTypeException |
|
| 310 | + * @throws DomainException |
|
| 311 | + */ |
|
| 312 | + public function dismissNotice($pan_name = '', $purge = false, $return = false) |
|
| 313 | + { |
|
| 314 | + $pan_name = $this->request->get('ee_nag_notice', $pan_name); |
|
| 315 | + $this->notice_collection = $this->getPersistentAdminNoticeCollection(); |
|
| 316 | + if (! empty($pan_name) && $this->notice_collection->has($pan_name)) { |
|
| 317 | + /** @var PersistentAdminNotice $persistent_admin_notice */ |
|
| 318 | + $persistent_admin_notice = $this->notice_collection->get($pan_name); |
|
| 319 | + $persistent_admin_notice->setDismissed(true); |
|
| 320 | + $persistent_admin_notice->setPurge($purge); |
|
| 321 | + $this->saveNotices(); |
|
| 322 | + } |
|
| 323 | + if ($return) { |
|
| 324 | + return; |
|
| 325 | + } |
|
| 326 | + if ($this->request->ajax) { |
|
| 327 | + // grab any notices and concatenate into string |
|
| 328 | + echo wp_json_encode( |
|
| 329 | + array( |
|
| 330 | + 'errors' => implode('<br />', EE_Error::get_notices(false)), |
|
| 331 | + ) |
|
| 332 | + ); |
|
| 333 | + exit(); |
|
| 334 | + } |
|
| 335 | + // save errors to a transient to be displayed on next request (after redirect) |
|
| 336 | + EE_Error::get_notices(false, true); |
|
| 337 | + wp_safe_redirect( |
|
| 338 | + urldecode( |
|
| 339 | + $this->request->get('return_url', '') |
|
| 340 | + ) |
|
| 341 | + ); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * saveNotices |
|
| 348 | + * |
|
| 349 | + * @throws DomainException |
|
| 350 | + * @throws InvalidDataTypeException |
|
| 351 | + * @throws InvalidInterfaceException |
|
| 352 | + * @throws InvalidEntityException |
|
| 353 | + */ |
|
| 354 | + public function saveNotices() |
|
| 355 | + { |
|
| 356 | + $this->notice_collection = $this->getPersistentAdminNoticeCollection(); |
|
| 357 | + if ($this->notice_collection->hasObjects()) { |
|
| 358 | + $persistent_admin_notices = get_option(PersistentAdminNoticeManager::WP_OPTION_KEY, array()); |
|
| 359 | + //maybe initialize persistent_admin_notices |
|
| 360 | + if (empty($persistent_admin_notices)) { |
|
| 361 | + add_option(PersistentAdminNoticeManager::WP_OPTION_KEY, array(), '', 'no'); |
|
| 362 | + } |
|
| 363 | + foreach ($this->notice_collection as $persistent_admin_notice) { |
|
| 364 | + // are we deleting this notice ? |
|
| 365 | + if ($persistent_admin_notice->getPurge()) { |
|
| 366 | + unset($persistent_admin_notices[$persistent_admin_notice->getName()]); |
|
| 367 | + } else { |
|
| 368 | + /** @var PersistentAdminNotice $persistent_admin_notice */ |
|
| 369 | + $persistent_admin_notices[$persistent_admin_notice->getName()] = array( |
|
| 370 | + 'message' => $persistent_admin_notice->getMessage(), |
|
| 371 | + 'capability' => $persistent_admin_notice->getCapability(), |
|
| 372 | + 'cap_context' => $persistent_admin_notice->getCapContext(), |
|
| 373 | + 'dismissed' => $persistent_admin_notice->getDismissed(), |
|
| 374 | + ); |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | + update_option(PersistentAdminNoticeManager::WP_OPTION_KEY, $persistent_admin_notices); |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * @throws DomainException |
|
| 385 | + * @throws InvalidDataTypeException |
|
| 386 | + * @throws InvalidEntityException |
|
| 387 | + * @throws InvalidInterfaceException |
|
| 388 | + */ |
|
| 389 | + public function registerAndSaveNotices() |
|
| 390 | + { |
|
| 391 | + $this->getPersistentAdminNoticeCollection(); |
|
| 392 | + $this->registerNotices(); |
|
| 393 | + $this->saveNotices(); |
|
| 394 | + add_filter( |
|
| 395 | + 'PersistentAdminNoticeManager__registerAndSaveNotices__complete', |
|
| 396 | + '__return_true' |
|
| 397 | + ); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * @throws DomainException |
|
| 403 | + * @throws InvalidDataTypeException |
|
| 404 | + * @throws InvalidEntityException |
|
| 405 | + * @throws InvalidInterfaceException |
|
| 406 | + * @throws InvalidArgumentException |
|
| 407 | + */ |
|
| 408 | + public static function loadRegisterAndSaveNotices() |
|
| 409 | + { |
|
| 410 | + /** @var PersistentAdminNoticeManager $persistent_admin_notice_manager */ |
|
| 411 | + $persistent_admin_notice_manager = LoaderFactory::getLoader()->getShared( |
|
| 412 | + 'EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 413 | + ); |
|
| 414 | + // if shutdown has already run, then call registerAndSaveNotices() manually |
|
| 415 | + if(did_action('shutdown')){ |
|
| 416 | + $persistent_admin_notice_manager->registerAndSaveNotices(); |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | 419 | |
| 420 | 420 | |
| 421 | 421 | } |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | */ |
| 89 | 89 | public function setReturnUrl($return_url) |
| 90 | 90 | { |
| 91 | - if (! is_string($return_url)) { |
|
| 91 | + if ( ! is_string($return_url)) { |
|
| 92 | 92 | throw new InvalidDataTypeException('$return_url', $return_url, 'string'); |
| 93 | 93 | } |
| 94 | 94 | $this->return_url = $return_url; |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | */ |
| 106 | 106 | protected function getPersistentAdminNoticeCollection() |
| 107 | 107 | { |
| 108 | - if (! $this->notice_collection instanceof Collection) { |
|
| 108 | + if ( ! $this->notice_collection instanceof Collection) { |
|
| 109 | 109 | $this->notice_collection = new Collection( |
| 110 | 110 | 'EventEspresso\core\domain\entities\notifications\PersistentAdminNotice' |
| 111 | 111 | ); |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | { |
| 130 | 130 | $persistent_admin_notices = get_option(PersistentAdminNoticeManager::WP_OPTION_KEY, array()); |
| 131 | 131 | // \EEH_Debug_Tools::printr($persistent_admin_notices, '$persistent_admin_notices', __FILE__, __LINE__); |
| 132 | - if (! empty($persistent_admin_notices)) { |
|
| 132 | + if ( ! empty($persistent_admin_notices)) { |
|
| 133 | 133 | foreach ($persistent_admin_notices as $name => $details) { |
| 134 | 134 | if (is_array($details)) { |
| 135 | 135 | if ( |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | $this->notice_collection->add( |
| 169 | 169 | new PersistentAdminNotice( |
| 170 | 170 | $name, |
| 171 | - (string)$details, |
|
| 171 | + (string) $details, |
|
| 172 | 172 | false, |
| 173 | 173 | '', |
| 174 | 174 | '', |
@@ -253,14 +253,14 @@ discard block |
||
| 253 | 253 | { |
| 254 | 254 | wp_register_script( |
| 255 | 255 | 'espresso_core', |
| 256 | - EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
|
| 256 | + EE_GLOBAL_ASSETS_URL.'scripts/espresso_core.js', |
|
| 257 | 257 | array('jquery'), |
| 258 | 258 | EVENT_ESPRESSO_VERSION, |
| 259 | 259 | true |
| 260 | 260 | ); |
| 261 | 261 | wp_register_script( |
| 262 | 262 | 'ee_error_js', |
| 263 | - EE_GLOBAL_ASSETS_URL . 'scripts/EE_Error.js', |
|
| 263 | + EE_GLOBAL_ASSETS_URL.'scripts/EE_Error.js', |
|
| 264 | 264 | array('espresso_core'), |
| 265 | 265 | EVENT_ESPRESSO_VERSION, |
| 266 | 266 | true |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | // used in template |
| 293 | 293 | $persistent_admin_notice_name = $persistent_admin_notice->getName(); |
| 294 | 294 | $persistent_admin_notice_message = $persistent_admin_notice->getMessage(); |
| 295 | - require EE_TEMPLATES . DS . 'notifications' . DS . 'persistent_admin_notice.template.php'; |
|
| 295 | + require EE_TEMPLATES.DS.'notifications'.DS.'persistent_admin_notice.template.php'; |
|
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | |
@@ -313,7 +313,7 @@ discard block |
||
| 313 | 313 | { |
| 314 | 314 | $pan_name = $this->request->get('ee_nag_notice', $pan_name); |
| 315 | 315 | $this->notice_collection = $this->getPersistentAdminNoticeCollection(); |
| 316 | - if (! empty($pan_name) && $this->notice_collection->has($pan_name)) { |
|
| 316 | + if ( ! empty($pan_name) && $this->notice_collection->has($pan_name)) { |
|
| 317 | 317 | /** @var PersistentAdminNotice $persistent_admin_notice */ |
| 318 | 318 | $persistent_admin_notice = $this->notice_collection->get($pan_name); |
| 319 | 319 | $persistent_admin_notice->setDismissed(true); |
@@ -412,7 +412,7 @@ discard block |
||
| 412 | 412 | 'EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
| 413 | 413 | ); |
| 414 | 414 | // if shutdown has already run, then call registerAndSaveNotices() manually |
| 415 | - if(did_action('shutdown')){ |
|
| 415 | + if (did_action('shutdown')) { |
|
| 416 | 416 | $persistent_admin_notice_manager->registerAndSaveNotices(); |
| 417 | 417 | } |
| 418 | 418 | } |
@@ -22,457 +22,457 @@ discard block |
||
| 22 | 22 | final class EE_Admin implements InterminableInterface |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var EE_Admin $_instance |
|
| 27 | - */ |
|
| 28 | - private static $_instance; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var PersistentAdminNoticeManager $persistent_admin_notice_manager |
|
| 32 | - */ |
|
| 33 | - private $persistent_admin_notice_manager; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @singleton method used to instantiate class object |
|
| 37 | - * @return EE_Admin |
|
| 38 | - * @throws EE_Error |
|
| 39 | - */ |
|
| 40 | - public static function instance() |
|
| 41 | - { |
|
| 42 | - // check if class object is instantiated |
|
| 43 | - if (! self::$_instance instanceof EE_Admin) { |
|
| 44 | - self::$_instance = new self(); |
|
| 45 | - } |
|
| 46 | - return self::$_instance; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @return EE_Admin |
|
| 52 | - * @throws EE_Error |
|
| 53 | - */ |
|
| 54 | - public static function reset() |
|
| 55 | - { |
|
| 56 | - self::$_instance = null; |
|
| 57 | - return self::instance(); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * class constructor |
|
| 63 | - * |
|
| 64 | - * @throws EE_Error |
|
| 65 | - * @throws InvalidDataTypeException |
|
| 66 | - * @throws InvalidInterfaceException |
|
| 67 | - * @throws InvalidArgumentException |
|
| 68 | - */ |
|
| 69 | - protected function __construct() |
|
| 70 | - { |
|
| 71 | - // define global EE_Admin constants |
|
| 72 | - $this->_define_all_constants(); |
|
| 73 | - // set autoloaders for our admin page classes based on included path information |
|
| 74 | - EEH_Autoloader::instance()->register_autoloaders_for_each_file_in_folder(EE_ADMIN); |
|
| 75 | - // admin hooks |
|
| 76 | - add_filter('plugin_action_links', array($this, 'filter_plugin_actions'), 10, 2); |
|
| 77 | - // load EE_Request_Handler early |
|
| 78 | - add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'get_request')); |
|
| 79 | - add_action('AHEE__EE_System__initialize_last', array($this, 'init')); |
|
| 80 | - add_action('AHEE__EE_Admin_Page__route_admin_request', array($this, 'route_admin_request'), 100, 2); |
|
| 81 | - add_action('wp_loaded', array($this, 'wp_loaded'), 100); |
|
| 82 | - add_action('admin_init', array($this, 'admin_init'), 100); |
|
| 83 | - add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'), 20); |
|
| 84 | - add_action('admin_notices', array($this, 'display_admin_notices'), 10); |
|
| 85 | - add_action('network_admin_notices', array($this, 'display_admin_notices'), 10); |
|
| 86 | - add_filter('pre_update_option', array($this, 'check_for_invalid_datetime_formats'), 100, 2); |
|
| 87 | - add_filter('admin_footer_text', array($this, 'espresso_admin_footer')); |
|
| 88 | - //reset Environment config (we only do this on admin page loads); |
|
| 89 | - EE_Registry::instance()->CFG->environment->recheck_values(); |
|
| 90 | - do_action('AHEE__EE_Admin__loaded'); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * _define_all_constants |
|
| 97 | - * define constants that are set globally for all admin pages |
|
| 98 | - * |
|
| 99 | - * @return void |
|
| 100 | - */ |
|
| 101 | - private function _define_all_constants() |
|
| 102 | - { |
|
| 103 | - if (! defined('EE_ADMIN_URL')) { |
|
| 104 | - define('EE_ADMIN_URL', EE_PLUGIN_DIR_URL . 'core/admin/'); |
|
| 105 | - define('EE_ADMIN_PAGES_URL', EE_PLUGIN_DIR_URL . 'admin_pages/'); |
|
| 106 | - define('EE_ADMIN_TEMPLATE', EE_ADMIN . 'templates' . DS); |
|
| 107 | - define('WP_ADMIN_PATH', ABSPATH . 'wp-admin/'); |
|
| 108 | - define('WP_AJAX_URL', admin_url('admin-ajax.php')); |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * filter_plugin_actions - adds links to the Plugins page listing |
|
| 115 | - * |
|
| 116 | - * @param array $links |
|
| 117 | - * @param string $plugin |
|
| 118 | - * @return array |
|
| 119 | - */ |
|
| 120 | - public function filter_plugin_actions($links, $plugin) |
|
| 121 | - { |
|
| 122 | - // set $main_file in stone |
|
| 123 | - static $main_file; |
|
| 124 | - // if $main_file is not set yet |
|
| 125 | - if (! $main_file) { |
|
| 126 | - $main_file = plugin_basename(EVENT_ESPRESSO_MAIN_FILE); |
|
| 127 | - } |
|
| 128 | - if ($plugin === $main_file) { |
|
| 129 | - // compare current plugin to this one |
|
| 130 | - if (EE_Maintenance_Mode::instance()->level() === EE_Maintenance_Mode::level_2_complete_maintenance) { |
|
| 131 | - $maintenance_link = '<a href="admin.php?page=espresso_maintenance_settings"' |
|
| 132 | - . ' title="Event Espresso is in maintenance mode. Click this link to learn why.">' |
|
| 133 | - . esc_html__('Maintenance Mode Active', 'event_espresso') |
|
| 134 | - . '</a>'; |
|
| 135 | - array_unshift($links, $maintenance_link); |
|
| 136 | - } else { |
|
| 137 | - $org_settings_link = '<a href="admin.php?page=espresso_general_settings">' |
|
| 138 | - . esc_html__('Settings', 'event_espresso') |
|
| 139 | - . '</a>'; |
|
| 140 | - $events_link = '<a href="admin.php?page=espresso_events">' |
|
| 141 | - . esc_html__('Events', 'event_espresso') |
|
| 142 | - . '</a>'; |
|
| 143 | - // add before other links |
|
| 144 | - array_unshift($links, $org_settings_link, $events_link); |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - return $links; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * _get_request |
|
| 153 | - * |
|
| 154 | - * @return void |
|
| 155 | - * @throws EE_Error |
|
| 156 | - * @throws InvalidArgumentException |
|
| 157 | - * @throws InvalidDataTypeException |
|
| 158 | - * @throws InvalidInterfaceException |
|
| 159 | - * @throws ReflectionException |
|
| 160 | - */ |
|
| 161 | - public function get_request() |
|
| 162 | - { |
|
| 163 | - EE_Registry::instance()->load_core('Request_Handler'); |
|
| 164 | - EE_Registry::instance()->load_core('CPT_Strategy'); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * hide_admin_pages_except_maintenance_mode |
|
| 171 | - * |
|
| 172 | - * @param array $admin_page_folder_names |
|
| 173 | - * @return array |
|
| 174 | - */ |
|
| 175 | - public function hide_admin_pages_except_maintenance_mode($admin_page_folder_names = array()) |
|
| 176 | - { |
|
| 177 | - return array( |
|
| 178 | - 'maintenance' => EE_ADMIN_PAGES . 'maintenance' . DS, |
|
| 179 | - 'about' => EE_ADMIN_PAGES . 'about' . DS, |
|
| 180 | - 'support' => EE_ADMIN_PAGES . 'support' . DS, |
|
| 181 | - ); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * init- should fire after shortcode, module, addon, other plugin (default priority), and even |
|
| 188 | - * EE_Front_Controller's init phases have run |
|
| 189 | - * |
|
| 190 | - * @return void |
|
| 191 | - * @throws EE_Error |
|
| 192 | - * @throws InvalidArgumentException |
|
| 193 | - * @throws InvalidDataTypeException |
|
| 194 | - * @throws InvalidInterfaceException |
|
| 195 | - * @throws ReflectionException |
|
| 196 | - * @throws ServiceNotFoundException |
|
| 197 | - */ |
|
| 198 | - public function init() |
|
| 199 | - { |
|
| 200 | - //only enable most of the EE_Admin IF we're not in full maintenance mode |
|
| 201 | - if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 202 | - //ok so we want to enable the entire admin |
|
| 203 | - $this->persistent_admin_notice_manager = LoaderFactory::getLoader()->getShared( |
|
| 204 | - 'EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 205 | - ); |
|
| 206 | - $this->persistent_admin_notice_manager->setReturnUrl( |
|
| 207 | - EE_Admin_Page::add_query_args_and_nonce( |
|
| 208 | - array( |
|
| 209 | - 'page' => EE_Registry::instance()->REQ->get('page', ''), |
|
| 210 | - 'action' => EE_Registry::instance()->REQ->get('action', ''), |
|
| 211 | - ), |
|
| 212 | - EE_ADMIN_URL |
|
| 213 | - ) |
|
| 214 | - ); |
|
| 215 | - $this->maybeSetDatetimeWarningNotice(); |
|
| 216 | - //at a glance dashboard widget |
|
| 217 | - add_filter('dashboard_glance_items', array($this, 'dashboard_glance_items'), 10); |
|
| 218 | - //filter for get_edit_post_link used on comments for custom post types |
|
| 219 | - add_filter('get_edit_post_link', array($this, 'modify_edit_post_link'), 10, 2); |
|
| 220 | - } |
|
| 221 | - // run the admin page factory but ONLY if we are doing an ee admin ajax request |
|
| 222 | - if (! defined('DOING_AJAX') || EE_ADMIN_AJAX) { |
|
| 223 | - try { |
|
| 224 | - //this loads the controller for the admin pages which will setup routing etc |
|
| 225 | - EE_Registry::instance()->load_core('Admin_Page_Loader'); |
|
| 226 | - } catch (EE_Error $e) { |
|
| 227 | - $e->get_error(); |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - add_filter('content_save_pre', array($this, 'its_eSpresso'), 10, 1); |
|
| 231 | - //make sure our CPTs and custom taxonomy metaboxes get shown for first time users |
|
| 232 | - add_action('admin_head', array($this, 'enable_hidden_ee_nav_menu_metaboxes'), 10); |
|
| 233 | - add_action('admin_head', array($this, 'register_custom_nav_menu_boxes'), 10); |
|
| 234 | - //exclude EE critical pages from all nav menus and wp_list_pages |
|
| 235 | - add_filter('nav_menu_meta_box_object', array($this, 'remove_pages_from_nav_menu'), 10); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * get_persistent_admin_notices |
|
| 241 | - * |
|
| 242 | - * @access public |
|
| 243 | - * @return void |
|
| 244 | - * @throws EE_Error |
|
| 245 | - * @throws InvalidArgumentException |
|
| 246 | - * @throws InvalidDataTypeException |
|
| 247 | - * @throws InvalidInterfaceException |
|
| 248 | - */ |
|
| 249 | - public function maybeSetDatetimeWarningNotice() |
|
| 250 | - { |
|
| 251 | - //add dismissable notice for datetime changes. Only valid if site does not have a timezone_string set. |
|
| 252 | - //@todo This needs to stay in core for a bit to catch anyone upgrading from a version without this to a version |
|
| 253 | - //with this. But after enough time (indeterminate at this point) we can just remove this notice. |
|
| 254 | - //this was added with https://events.codebasehq.com/projects/event-espresso/tickets/10626 |
|
| 255 | - if (apply_filters('FHEE__EE_Admin__maybeSetDatetimeWarningNotice', true) |
|
| 256 | - && ! get_option('timezone_string') |
|
| 257 | - && EEM_Event::instance()->count() > 0 |
|
| 258 | - ) { |
|
| 259 | - new PersistentAdminNotice( |
|
| 260 | - 'datetime_fix_notice', |
|
| 261 | - sprintf( |
|
| 262 | - esc_html__( |
|
| 263 | - '%1$sImportant announcement related to your install of Event Espresso%2$s: There are some changes made to your site that could affect how dates display for your events and other related items with dates and times. Read more about it %3$shere%4$s. If your dates and times are displaying incorrectly (incorrect offset), you can fix it using the tool on %5$sthis page%4$s.', |
|
| 264 | - 'event_espresso' |
|
| 265 | - ), |
|
| 266 | - '<strong>', |
|
| 267 | - '</strong>', |
|
| 268 | - '<a href="https://eventespresso.com/2017/08/important-upcoming-changes-dates-times">', |
|
| 269 | - '</a>', |
|
| 270 | - '<a href="' . EE_Admin_Page::add_query_args_and_nonce( |
|
| 271 | - array( |
|
| 272 | - 'page' => 'espresso_maintenance_settings', |
|
| 273 | - 'action' => 'datetime_tools' |
|
| 274 | - ), |
|
| 275 | - admin_url('admin.php') |
|
| 276 | - ) . '">' |
|
| 277 | - ), |
|
| 278 | - false, |
|
| 279 | - 'manage_options', |
|
| 280 | - 'datetime_fix_persistent_notice' |
|
| 281 | - ); |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * this simply hooks into the nav menu setup of pages metabox and makes sure that we remove EE critical pages from |
|
| 289 | - * the list of options. the wp function "wp_nav_menu_item_post_type_meta_box" found in |
|
| 290 | - * wp-admin/includes/nav-menu.php looks for the "_default_query" property on the post_type object and it uses that |
|
| 291 | - * to override any queries found in the existing query for the given post type. Note that _default_query is not a |
|
| 292 | - * normal property on the post_type object. It's found ONLY in this particular context. |
|
| 293 | - * |
|
| 294 | - * @param WP_Post $post_type WP post type object |
|
| 295 | - * @return WP_Post |
|
| 296 | - * @throws InvalidArgumentException |
|
| 297 | - * @throws InvalidDataTypeException |
|
| 298 | - * @throws InvalidInterfaceException |
|
| 299 | - */ |
|
| 300 | - public function remove_pages_from_nav_menu($post_type) |
|
| 301 | - { |
|
| 302 | - //if this isn't the "pages" post type let's get out |
|
| 303 | - if ($post_type->name !== 'page') { |
|
| 304 | - return $post_type; |
|
| 305 | - } |
|
| 306 | - $critical_pages = EE_Registry::instance()->CFG->core->get_critical_pages_array(); |
|
| 307 | - $post_type->_default_query = array( |
|
| 308 | - 'post__not_in' => $critical_pages, |
|
| 309 | - ); |
|
| 310 | - return $post_type; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * WP by default only shows three metaboxes in "nav-menus.php" for first times users. We want to make sure our |
|
| 317 | - * metaboxes get shown as well |
|
| 318 | - * |
|
| 319 | - * @return void |
|
| 320 | - */ |
|
| 321 | - public function enable_hidden_ee_nav_menu_metaboxes() |
|
| 322 | - { |
|
| 323 | - global $wp_meta_boxes, $pagenow; |
|
| 324 | - if (! is_array($wp_meta_boxes) || $pagenow !== 'nav-menus.php') { |
|
| 325 | - return; |
|
| 326 | - } |
|
| 327 | - $user = wp_get_current_user(); |
|
| 328 | - //has this been done yet? |
|
| 329 | - if (get_user_option('ee_nav_menu_initialized', $user->ID)) { |
|
| 330 | - return; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - $hidden_meta_boxes = get_user_option('metaboxhidden_nav-menus', $user->ID); |
|
| 334 | - $initial_meta_boxes = apply_filters( |
|
| 335 | - 'FHEE__EE_Admin__enable_hidden_ee_nav_menu_boxes__initial_meta_boxes', |
|
| 336 | - array( |
|
| 337 | - 'nav-menu-theme-locations', |
|
| 338 | - 'add-page', |
|
| 339 | - 'add-custom-links', |
|
| 340 | - 'add-category', |
|
| 341 | - 'add-espresso_events', |
|
| 342 | - 'add-espresso_venues', |
|
| 343 | - 'add-espresso_event_categories', |
|
| 344 | - 'add-espresso_venue_categories', |
|
| 345 | - 'add-post-type-post', |
|
| 346 | - 'add-post-type-page', |
|
| 347 | - ) |
|
| 348 | - ); |
|
| 349 | - |
|
| 350 | - if (is_array($hidden_meta_boxes)) { |
|
| 351 | - foreach ($hidden_meta_boxes as $key => $meta_box_id) { |
|
| 352 | - if (in_array($meta_box_id, $initial_meta_boxes, true)) { |
|
| 353 | - unset($hidden_meta_boxes[$key]); |
|
| 354 | - } |
|
| 355 | - } |
|
| 356 | - } |
|
| 357 | - update_user_option($user->ID, 'metaboxhidden_nav-menus', $hidden_meta_boxes, true); |
|
| 358 | - update_user_option($user->ID, 'ee_nav_menu_initialized', 1, true); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * This method simply registers custom nav menu boxes for "nav_menus.php route" |
|
| 365 | - * Currently EE is using this to make sure there are menu options for our CPT archive page routes. |
|
| 366 | - * |
|
| 367 | - * @todo modify this so its more dynamic and automatic for all ee CPTs and setups and can also be hooked into by |
|
| 368 | - * addons etc. |
|
| 369 | - * @return void |
|
| 370 | - */ |
|
| 371 | - public function register_custom_nav_menu_boxes() |
|
| 372 | - { |
|
| 373 | - add_meta_box( |
|
| 374 | - 'add-extra-nav-menu-pages', |
|
| 375 | - esc_html__('Event Espresso Pages', 'event_espresso'), |
|
| 376 | - array($this, 'ee_cpt_archive_pages'), |
|
| 377 | - 'nav-menus', |
|
| 378 | - 'side', |
|
| 379 | - 'core' |
|
| 380 | - ); |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Use this to edit the post link for our cpts so that the edit link points to the correct page. |
|
| 387 | - * |
|
| 388 | - * @since 4.3.0 |
|
| 389 | - * @param string $link the original link generated by wp |
|
| 390 | - * @param int $id post id |
|
| 391 | - * @return string the (maybe) modified link |
|
| 392 | - */ |
|
| 393 | - public function modify_edit_post_link($link, $id) |
|
| 394 | - { |
|
| 395 | - if (! $post = get_post($id)) { |
|
| 396 | - return $link; |
|
| 397 | - } |
|
| 398 | - if ($post->post_type === 'espresso_attendees') { |
|
| 399 | - $query_args = array( |
|
| 400 | - 'action' => 'edit_attendee', |
|
| 401 | - 'post' => $id, |
|
| 402 | - ); |
|
| 403 | - return EEH_URL::add_query_args_and_nonce( |
|
| 404 | - $query_args, |
|
| 405 | - admin_url('admin.php?page=espresso_registrations') |
|
| 406 | - ); |
|
| 407 | - } |
|
| 408 | - return $link; |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - |
|
| 412 | - |
|
| 413 | - public function ee_cpt_archive_pages() |
|
| 414 | - { |
|
| 415 | - global $nav_menu_selected_id; |
|
| 416 | - $db_fields = false; |
|
| 417 | - $walker = new Walker_Nav_Menu_Checklist($db_fields); |
|
| 418 | - $current_tab = 'event-archives'; |
|
| 419 | - $removed_args = array( |
|
| 420 | - 'action', |
|
| 421 | - 'customlink-tab', |
|
| 422 | - 'edit-menu-item', |
|
| 423 | - 'menu-item', |
|
| 424 | - 'page-tab', |
|
| 425 | - '_wpnonce', |
|
| 426 | - ); |
|
| 427 | - ?> |
|
| 25 | + /** |
|
| 26 | + * @var EE_Admin $_instance |
|
| 27 | + */ |
|
| 28 | + private static $_instance; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var PersistentAdminNoticeManager $persistent_admin_notice_manager |
|
| 32 | + */ |
|
| 33 | + private $persistent_admin_notice_manager; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @singleton method used to instantiate class object |
|
| 37 | + * @return EE_Admin |
|
| 38 | + * @throws EE_Error |
|
| 39 | + */ |
|
| 40 | + public static function instance() |
|
| 41 | + { |
|
| 42 | + // check if class object is instantiated |
|
| 43 | + if (! self::$_instance instanceof EE_Admin) { |
|
| 44 | + self::$_instance = new self(); |
|
| 45 | + } |
|
| 46 | + return self::$_instance; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @return EE_Admin |
|
| 52 | + * @throws EE_Error |
|
| 53 | + */ |
|
| 54 | + public static function reset() |
|
| 55 | + { |
|
| 56 | + self::$_instance = null; |
|
| 57 | + return self::instance(); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * class constructor |
|
| 63 | + * |
|
| 64 | + * @throws EE_Error |
|
| 65 | + * @throws InvalidDataTypeException |
|
| 66 | + * @throws InvalidInterfaceException |
|
| 67 | + * @throws InvalidArgumentException |
|
| 68 | + */ |
|
| 69 | + protected function __construct() |
|
| 70 | + { |
|
| 71 | + // define global EE_Admin constants |
|
| 72 | + $this->_define_all_constants(); |
|
| 73 | + // set autoloaders for our admin page classes based on included path information |
|
| 74 | + EEH_Autoloader::instance()->register_autoloaders_for_each_file_in_folder(EE_ADMIN); |
|
| 75 | + // admin hooks |
|
| 76 | + add_filter('plugin_action_links', array($this, 'filter_plugin_actions'), 10, 2); |
|
| 77 | + // load EE_Request_Handler early |
|
| 78 | + add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'get_request')); |
|
| 79 | + add_action('AHEE__EE_System__initialize_last', array($this, 'init')); |
|
| 80 | + add_action('AHEE__EE_Admin_Page__route_admin_request', array($this, 'route_admin_request'), 100, 2); |
|
| 81 | + add_action('wp_loaded', array($this, 'wp_loaded'), 100); |
|
| 82 | + add_action('admin_init', array($this, 'admin_init'), 100); |
|
| 83 | + add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'), 20); |
|
| 84 | + add_action('admin_notices', array($this, 'display_admin_notices'), 10); |
|
| 85 | + add_action('network_admin_notices', array($this, 'display_admin_notices'), 10); |
|
| 86 | + add_filter('pre_update_option', array($this, 'check_for_invalid_datetime_formats'), 100, 2); |
|
| 87 | + add_filter('admin_footer_text', array($this, 'espresso_admin_footer')); |
|
| 88 | + //reset Environment config (we only do this on admin page loads); |
|
| 89 | + EE_Registry::instance()->CFG->environment->recheck_values(); |
|
| 90 | + do_action('AHEE__EE_Admin__loaded'); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * _define_all_constants |
|
| 97 | + * define constants that are set globally for all admin pages |
|
| 98 | + * |
|
| 99 | + * @return void |
|
| 100 | + */ |
|
| 101 | + private function _define_all_constants() |
|
| 102 | + { |
|
| 103 | + if (! defined('EE_ADMIN_URL')) { |
|
| 104 | + define('EE_ADMIN_URL', EE_PLUGIN_DIR_URL . 'core/admin/'); |
|
| 105 | + define('EE_ADMIN_PAGES_URL', EE_PLUGIN_DIR_URL . 'admin_pages/'); |
|
| 106 | + define('EE_ADMIN_TEMPLATE', EE_ADMIN . 'templates' . DS); |
|
| 107 | + define('WP_ADMIN_PATH', ABSPATH . 'wp-admin/'); |
|
| 108 | + define('WP_AJAX_URL', admin_url('admin-ajax.php')); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * filter_plugin_actions - adds links to the Plugins page listing |
|
| 115 | + * |
|
| 116 | + * @param array $links |
|
| 117 | + * @param string $plugin |
|
| 118 | + * @return array |
|
| 119 | + */ |
|
| 120 | + public function filter_plugin_actions($links, $plugin) |
|
| 121 | + { |
|
| 122 | + // set $main_file in stone |
|
| 123 | + static $main_file; |
|
| 124 | + // if $main_file is not set yet |
|
| 125 | + if (! $main_file) { |
|
| 126 | + $main_file = plugin_basename(EVENT_ESPRESSO_MAIN_FILE); |
|
| 127 | + } |
|
| 128 | + if ($plugin === $main_file) { |
|
| 129 | + // compare current plugin to this one |
|
| 130 | + if (EE_Maintenance_Mode::instance()->level() === EE_Maintenance_Mode::level_2_complete_maintenance) { |
|
| 131 | + $maintenance_link = '<a href="admin.php?page=espresso_maintenance_settings"' |
|
| 132 | + . ' title="Event Espresso is in maintenance mode. Click this link to learn why.">' |
|
| 133 | + . esc_html__('Maintenance Mode Active', 'event_espresso') |
|
| 134 | + . '</a>'; |
|
| 135 | + array_unshift($links, $maintenance_link); |
|
| 136 | + } else { |
|
| 137 | + $org_settings_link = '<a href="admin.php?page=espresso_general_settings">' |
|
| 138 | + . esc_html__('Settings', 'event_espresso') |
|
| 139 | + . '</a>'; |
|
| 140 | + $events_link = '<a href="admin.php?page=espresso_events">' |
|
| 141 | + . esc_html__('Events', 'event_espresso') |
|
| 142 | + . '</a>'; |
|
| 143 | + // add before other links |
|
| 144 | + array_unshift($links, $org_settings_link, $events_link); |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + return $links; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * _get_request |
|
| 153 | + * |
|
| 154 | + * @return void |
|
| 155 | + * @throws EE_Error |
|
| 156 | + * @throws InvalidArgumentException |
|
| 157 | + * @throws InvalidDataTypeException |
|
| 158 | + * @throws InvalidInterfaceException |
|
| 159 | + * @throws ReflectionException |
|
| 160 | + */ |
|
| 161 | + public function get_request() |
|
| 162 | + { |
|
| 163 | + EE_Registry::instance()->load_core('Request_Handler'); |
|
| 164 | + EE_Registry::instance()->load_core('CPT_Strategy'); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * hide_admin_pages_except_maintenance_mode |
|
| 171 | + * |
|
| 172 | + * @param array $admin_page_folder_names |
|
| 173 | + * @return array |
|
| 174 | + */ |
|
| 175 | + public function hide_admin_pages_except_maintenance_mode($admin_page_folder_names = array()) |
|
| 176 | + { |
|
| 177 | + return array( |
|
| 178 | + 'maintenance' => EE_ADMIN_PAGES . 'maintenance' . DS, |
|
| 179 | + 'about' => EE_ADMIN_PAGES . 'about' . DS, |
|
| 180 | + 'support' => EE_ADMIN_PAGES . 'support' . DS, |
|
| 181 | + ); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * init- should fire after shortcode, module, addon, other plugin (default priority), and even |
|
| 188 | + * EE_Front_Controller's init phases have run |
|
| 189 | + * |
|
| 190 | + * @return void |
|
| 191 | + * @throws EE_Error |
|
| 192 | + * @throws InvalidArgumentException |
|
| 193 | + * @throws InvalidDataTypeException |
|
| 194 | + * @throws InvalidInterfaceException |
|
| 195 | + * @throws ReflectionException |
|
| 196 | + * @throws ServiceNotFoundException |
|
| 197 | + */ |
|
| 198 | + public function init() |
|
| 199 | + { |
|
| 200 | + //only enable most of the EE_Admin IF we're not in full maintenance mode |
|
| 201 | + if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
| 202 | + //ok so we want to enable the entire admin |
|
| 203 | + $this->persistent_admin_notice_manager = LoaderFactory::getLoader()->getShared( |
|
| 204 | + 'EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 205 | + ); |
|
| 206 | + $this->persistent_admin_notice_manager->setReturnUrl( |
|
| 207 | + EE_Admin_Page::add_query_args_and_nonce( |
|
| 208 | + array( |
|
| 209 | + 'page' => EE_Registry::instance()->REQ->get('page', ''), |
|
| 210 | + 'action' => EE_Registry::instance()->REQ->get('action', ''), |
|
| 211 | + ), |
|
| 212 | + EE_ADMIN_URL |
|
| 213 | + ) |
|
| 214 | + ); |
|
| 215 | + $this->maybeSetDatetimeWarningNotice(); |
|
| 216 | + //at a glance dashboard widget |
|
| 217 | + add_filter('dashboard_glance_items', array($this, 'dashboard_glance_items'), 10); |
|
| 218 | + //filter for get_edit_post_link used on comments for custom post types |
|
| 219 | + add_filter('get_edit_post_link', array($this, 'modify_edit_post_link'), 10, 2); |
|
| 220 | + } |
|
| 221 | + // run the admin page factory but ONLY if we are doing an ee admin ajax request |
|
| 222 | + if (! defined('DOING_AJAX') || EE_ADMIN_AJAX) { |
|
| 223 | + try { |
|
| 224 | + //this loads the controller for the admin pages which will setup routing etc |
|
| 225 | + EE_Registry::instance()->load_core('Admin_Page_Loader'); |
|
| 226 | + } catch (EE_Error $e) { |
|
| 227 | + $e->get_error(); |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + add_filter('content_save_pre', array($this, 'its_eSpresso'), 10, 1); |
|
| 231 | + //make sure our CPTs and custom taxonomy metaboxes get shown for first time users |
|
| 232 | + add_action('admin_head', array($this, 'enable_hidden_ee_nav_menu_metaboxes'), 10); |
|
| 233 | + add_action('admin_head', array($this, 'register_custom_nav_menu_boxes'), 10); |
|
| 234 | + //exclude EE critical pages from all nav menus and wp_list_pages |
|
| 235 | + add_filter('nav_menu_meta_box_object', array($this, 'remove_pages_from_nav_menu'), 10); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * get_persistent_admin_notices |
|
| 241 | + * |
|
| 242 | + * @access public |
|
| 243 | + * @return void |
|
| 244 | + * @throws EE_Error |
|
| 245 | + * @throws InvalidArgumentException |
|
| 246 | + * @throws InvalidDataTypeException |
|
| 247 | + * @throws InvalidInterfaceException |
|
| 248 | + */ |
|
| 249 | + public function maybeSetDatetimeWarningNotice() |
|
| 250 | + { |
|
| 251 | + //add dismissable notice for datetime changes. Only valid if site does not have a timezone_string set. |
|
| 252 | + //@todo This needs to stay in core for a bit to catch anyone upgrading from a version without this to a version |
|
| 253 | + //with this. But after enough time (indeterminate at this point) we can just remove this notice. |
|
| 254 | + //this was added with https://events.codebasehq.com/projects/event-espresso/tickets/10626 |
|
| 255 | + if (apply_filters('FHEE__EE_Admin__maybeSetDatetimeWarningNotice', true) |
|
| 256 | + && ! get_option('timezone_string') |
|
| 257 | + && EEM_Event::instance()->count() > 0 |
|
| 258 | + ) { |
|
| 259 | + new PersistentAdminNotice( |
|
| 260 | + 'datetime_fix_notice', |
|
| 261 | + sprintf( |
|
| 262 | + esc_html__( |
|
| 263 | + '%1$sImportant announcement related to your install of Event Espresso%2$s: There are some changes made to your site that could affect how dates display for your events and other related items with dates and times. Read more about it %3$shere%4$s. If your dates and times are displaying incorrectly (incorrect offset), you can fix it using the tool on %5$sthis page%4$s.', |
|
| 264 | + 'event_espresso' |
|
| 265 | + ), |
|
| 266 | + '<strong>', |
|
| 267 | + '</strong>', |
|
| 268 | + '<a href="https://eventespresso.com/2017/08/important-upcoming-changes-dates-times">', |
|
| 269 | + '</a>', |
|
| 270 | + '<a href="' . EE_Admin_Page::add_query_args_and_nonce( |
|
| 271 | + array( |
|
| 272 | + 'page' => 'espresso_maintenance_settings', |
|
| 273 | + 'action' => 'datetime_tools' |
|
| 274 | + ), |
|
| 275 | + admin_url('admin.php') |
|
| 276 | + ) . '">' |
|
| 277 | + ), |
|
| 278 | + false, |
|
| 279 | + 'manage_options', |
|
| 280 | + 'datetime_fix_persistent_notice' |
|
| 281 | + ); |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * this simply hooks into the nav menu setup of pages metabox and makes sure that we remove EE critical pages from |
|
| 289 | + * the list of options. the wp function "wp_nav_menu_item_post_type_meta_box" found in |
|
| 290 | + * wp-admin/includes/nav-menu.php looks for the "_default_query" property on the post_type object and it uses that |
|
| 291 | + * to override any queries found in the existing query for the given post type. Note that _default_query is not a |
|
| 292 | + * normal property on the post_type object. It's found ONLY in this particular context. |
|
| 293 | + * |
|
| 294 | + * @param WP_Post $post_type WP post type object |
|
| 295 | + * @return WP_Post |
|
| 296 | + * @throws InvalidArgumentException |
|
| 297 | + * @throws InvalidDataTypeException |
|
| 298 | + * @throws InvalidInterfaceException |
|
| 299 | + */ |
|
| 300 | + public function remove_pages_from_nav_menu($post_type) |
|
| 301 | + { |
|
| 302 | + //if this isn't the "pages" post type let's get out |
|
| 303 | + if ($post_type->name !== 'page') { |
|
| 304 | + return $post_type; |
|
| 305 | + } |
|
| 306 | + $critical_pages = EE_Registry::instance()->CFG->core->get_critical_pages_array(); |
|
| 307 | + $post_type->_default_query = array( |
|
| 308 | + 'post__not_in' => $critical_pages, |
|
| 309 | + ); |
|
| 310 | + return $post_type; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * WP by default only shows three metaboxes in "nav-menus.php" for first times users. We want to make sure our |
|
| 317 | + * metaboxes get shown as well |
|
| 318 | + * |
|
| 319 | + * @return void |
|
| 320 | + */ |
|
| 321 | + public function enable_hidden_ee_nav_menu_metaboxes() |
|
| 322 | + { |
|
| 323 | + global $wp_meta_boxes, $pagenow; |
|
| 324 | + if (! is_array($wp_meta_boxes) || $pagenow !== 'nav-menus.php') { |
|
| 325 | + return; |
|
| 326 | + } |
|
| 327 | + $user = wp_get_current_user(); |
|
| 328 | + //has this been done yet? |
|
| 329 | + if (get_user_option('ee_nav_menu_initialized', $user->ID)) { |
|
| 330 | + return; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + $hidden_meta_boxes = get_user_option('metaboxhidden_nav-menus', $user->ID); |
|
| 334 | + $initial_meta_boxes = apply_filters( |
|
| 335 | + 'FHEE__EE_Admin__enable_hidden_ee_nav_menu_boxes__initial_meta_boxes', |
|
| 336 | + array( |
|
| 337 | + 'nav-menu-theme-locations', |
|
| 338 | + 'add-page', |
|
| 339 | + 'add-custom-links', |
|
| 340 | + 'add-category', |
|
| 341 | + 'add-espresso_events', |
|
| 342 | + 'add-espresso_venues', |
|
| 343 | + 'add-espresso_event_categories', |
|
| 344 | + 'add-espresso_venue_categories', |
|
| 345 | + 'add-post-type-post', |
|
| 346 | + 'add-post-type-page', |
|
| 347 | + ) |
|
| 348 | + ); |
|
| 349 | + |
|
| 350 | + if (is_array($hidden_meta_boxes)) { |
|
| 351 | + foreach ($hidden_meta_boxes as $key => $meta_box_id) { |
|
| 352 | + if (in_array($meta_box_id, $initial_meta_boxes, true)) { |
|
| 353 | + unset($hidden_meta_boxes[$key]); |
|
| 354 | + } |
|
| 355 | + } |
|
| 356 | + } |
|
| 357 | + update_user_option($user->ID, 'metaboxhidden_nav-menus', $hidden_meta_boxes, true); |
|
| 358 | + update_user_option($user->ID, 'ee_nav_menu_initialized', 1, true); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * This method simply registers custom nav menu boxes for "nav_menus.php route" |
|
| 365 | + * Currently EE is using this to make sure there are menu options for our CPT archive page routes. |
|
| 366 | + * |
|
| 367 | + * @todo modify this so its more dynamic and automatic for all ee CPTs and setups and can also be hooked into by |
|
| 368 | + * addons etc. |
|
| 369 | + * @return void |
|
| 370 | + */ |
|
| 371 | + public function register_custom_nav_menu_boxes() |
|
| 372 | + { |
|
| 373 | + add_meta_box( |
|
| 374 | + 'add-extra-nav-menu-pages', |
|
| 375 | + esc_html__('Event Espresso Pages', 'event_espresso'), |
|
| 376 | + array($this, 'ee_cpt_archive_pages'), |
|
| 377 | + 'nav-menus', |
|
| 378 | + 'side', |
|
| 379 | + 'core' |
|
| 380 | + ); |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Use this to edit the post link for our cpts so that the edit link points to the correct page. |
|
| 387 | + * |
|
| 388 | + * @since 4.3.0 |
|
| 389 | + * @param string $link the original link generated by wp |
|
| 390 | + * @param int $id post id |
|
| 391 | + * @return string the (maybe) modified link |
|
| 392 | + */ |
|
| 393 | + public function modify_edit_post_link($link, $id) |
|
| 394 | + { |
|
| 395 | + if (! $post = get_post($id)) { |
|
| 396 | + return $link; |
|
| 397 | + } |
|
| 398 | + if ($post->post_type === 'espresso_attendees') { |
|
| 399 | + $query_args = array( |
|
| 400 | + 'action' => 'edit_attendee', |
|
| 401 | + 'post' => $id, |
|
| 402 | + ); |
|
| 403 | + return EEH_URL::add_query_args_and_nonce( |
|
| 404 | + $query_args, |
|
| 405 | + admin_url('admin.php?page=espresso_registrations') |
|
| 406 | + ); |
|
| 407 | + } |
|
| 408 | + return $link; |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + |
|
| 412 | + |
|
| 413 | + public function ee_cpt_archive_pages() |
|
| 414 | + { |
|
| 415 | + global $nav_menu_selected_id; |
|
| 416 | + $db_fields = false; |
|
| 417 | + $walker = new Walker_Nav_Menu_Checklist($db_fields); |
|
| 418 | + $current_tab = 'event-archives'; |
|
| 419 | + $removed_args = array( |
|
| 420 | + 'action', |
|
| 421 | + 'customlink-tab', |
|
| 422 | + 'edit-menu-item', |
|
| 423 | + 'menu-item', |
|
| 424 | + 'page-tab', |
|
| 425 | + '_wpnonce', |
|
| 426 | + ); |
|
| 427 | + ?> |
|
| 428 | 428 | <div id="posttype-extra-nav-menu-pages" class="posttypediv"> |
| 429 | 429 | <ul id="posttype-extra-nav-menu-pages-tabs" class="posttype-tabs add-menu-item-tabs"> |
| 430 | 430 | <li <?php echo('event-archives' === $current_tab ? ' class="tabs"' : ''); ?>> |
| 431 | 431 | <a class="nav-tab-link" data-type="tabs-panel-posttype-extra-nav-menu-pages-event-archives" |
| 432 | 432 | href="<?php if ($nav_menu_selected_id) { |
| 433 | - echo esc_url( |
|
| 434 | - add_query_arg( |
|
| 435 | - 'extra-nav-menu-pages-tab', |
|
| 436 | - 'event-archives', |
|
| 437 | - remove_query_arg($removed_args) |
|
| 438 | - ) |
|
| 439 | - ); |
|
| 440 | - } ?>#tabs-panel-posttype-extra-nav-menu-pages-event-archives"> |
|
| 433 | + echo esc_url( |
|
| 434 | + add_query_arg( |
|
| 435 | + 'extra-nav-menu-pages-tab', |
|
| 436 | + 'event-archives', |
|
| 437 | + remove_query_arg($removed_args) |
|
| 438 | + ) |
|
| 439 | + ); |
|
| 440 | + } ?>#tabs-panel-posttype-extra-nav-menu-pages-event-archives"> |
|
| 441 | 441 | <?php _e('Event Archive Pages', 'event_espresso'); ?> |
| 442 | 442 | </a> |
| 443 | 443 | </li> |
| 444 | 444 | </ul><!-- .posttype-tabs --> |
| 445 | 445 | |
| 446 | 446 | <div id="tabs-panel-posttype-extra-nav-menu-pages-event-archives" class="tabs-panel <?php |
| 447 | - echo('event-archives' === $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive'); |
|
| 448 | - ?>"> |
|
| 447 | + echo('event-archives' === $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive'); |
|
| 448 | + ?>"> |
|
| 449 | 449 | <ul id="extra-nav-menu-pageschecklist-event-archives" class="categorychecklist form-no-clear"> |
| 450 | 450 | <?php |
| 451 | - $pages = $this->_get_extra_nav_menu_pages_items(); |
|
| 452 | - $args['walker'] = $walker; |
|
| 453 | - echo walk_nav_menu_tree( |
|
| 454 | - array_map( |
|
| 455 | - array($this, '_setup_extra_nav_menu_pages_items'), |
|
| 456 | - $pages |
|
| 457 | - ), |
|
| 458 | - 0, |
|
| 459 | - (object) $args |
|
| 460 | - ); |
|
| 461 | - ?> |
|
| 451 | + $pages = $this->_get_extra_nav_menu_pages_items(); |
|
| 452 | + $args['walker'] = $walker; |
|
| 453 | + echo walk_nav_menu_tree( |
|
| 454 | + array_map( |
|
| 455 | + array($this, '_setup_extra_nav_menu_pages_items'), |
|
| 456 | + $pages |
|
| 457 | + ), |
|
| 458 | + 0, |
|
| 459 | + (object) $args |
|
| 460 | + ); |
|
| 461 | + ?> |
|
| 462 | 462 | </ul> |
| 463 | 463 | </div><!-- /.tabs-panel --> |
| 464 | 464 | |
| 465 | 465 | <p class="button-controls"> |
| 466 | 466 | <span class="list-controls"> |
| 467 | 467 | <a href="<?php |
| 468 | - echo esc_url(add_query_arg( |
|
| 469 | - array( |
|
| 470 | - 'extra-nav-menu-pages-tab' => 'event-archives', |
|
| 471 | - 'selectall' => 1, |
|
| 472 | - ), |
|
| 473 | - remove_query_arg($removed_args) |
|
| 474 | - )); |
|
| 475 | - ?>#posttype-extra-nav-menu-pages>" class="select-all"><?php _e('Select All'); ?></a> |
|
| 468 | + echo esc_url(add_query_arg( |
|
| 469 | + array( |
|
| 470 | + 'extra-nav-menu-pages-tab' => 'event-archives', |
|
| 471 | + 'selectall' => 1, |
|
| 472 | + ), |
|
| 473 | + remove_query_arg($removed_args) |
|
| 474 | + )); |
|
| 475 | + ?>#posttype-extra-nav-menu-pages>" class="select-all"><?php _e('Select All'); ?></a> |
|
| 476 | 476 | </span> |
| 477 | 477 | <span class="add-to-menu"> |
| 478 | 478 | <input type="submit"<?php wp_nav_menu_disabled_check($nav_menu_selected_id); ?> |
@@ -485,471 +485,471 @@ discard block |
||
| 485 | 485 | |
| 486 | 486 | </div><!-- /.posttypediv --> |
| 487 | 487 | <?php |
| 488 | - } |
|
| 489 | - |
|
| 490 | - |
|
| 491 | - /** |
|
| 492 | - * Returns an array of event archive nav items. |
|
| 493 | - * |
|
| 494 | - * @todo for now this method is just in place so when it gets abstracted further we can substitute in whatever |
|
| 495 | - * method we use for getting the extra nav menu items |
|
| 496 | - * @return array |
|
| 497 | - */ |
|
| 498 | - private function _get_extra_nav_menu_pages_items() |
|
| 499 | - { |
|
| 500 | - $menuitems[] = array( |
|
| 501 | - 'title' => esc_html__('Event List', 'event_espresso'), |
|
| 502 | - 'url' => get_post_type_archive_link('espresso_events'), |
|
| 503 | - 'description' => esc_html__('Archive page for all events.', 'event_espresso'), |
|
| 504 | - ); |
|
| 505 | - return apply_filters('FHEE__EE_Admin__get_extra_nav_menu_pages_items', $menuitems); |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - |
|
| 509 | - /** |
|
| 510 | - * Setup nav menu walker item for usage in the event archive nav menu metabox. It receives a menu_item array with |
|
| 511 | - * the properties and converts it to the menu item object. |
|
| 512 | - * |
|
| 513 | - * @see wp_setup_nav_menu_item() in wp-includes/nav-menu.php |
|
| 514 | - * @param $menu_item_values |
|
| 515 | - * @return stdClass |
|
| 516 | - */ |
|
| 517 | - private function _setup_extra_nav_menu_pages_items($menu_item_values) |
|
| 518 | - { |
|
| 519 | - $menu_item = new stdClass(); |
|
| 520 | - $keys = array( |
|
| 521 | - 'ID' => 0, |
|
| 522 | - 'db_id' => 0, |
|
| 523 | - 'menu_item_parent' => 0, |
|
| 524 | - 'object_id' => -1, |
|
| 525 | - 'post_parent' => 0, |
|
| 526 | - 'type' => 'custom', |
|
| 527 | - 'object' => '', |
|
| 528 | - 'type_label' => esc_html__('Extra Nav Menu Item', 'event_espresso'), |
|
| 529 | - 'title' => '', |
|
| 530 | - 'url' => '', |
|
| 531 | - 'target' => '', |
|
| 532 | - 'attr_title' => '', |
|
| 533 | - 'description' => '', |
|
| 534 | - 'classes' => array(), |
|
| 535 | - 'xfn' => '', |
|
| 536 | - ); |
|
| 537 | - |
|
| 538 | - foreach ($keys as $key => $value) { |
|
| 539 | - $menu_item->{$key} = isset($menu_item_values[$key]) ? $menu_item_values[$key] : $value; |
|
| 540 | - } |
|
| 541 | - return $menu_item; |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * This is the action hook for the AHEE__EE_Admin_Page__route_admin_request hook that fires off right before an |
|
| 547 | - * EE_Admin_Page route is called. |
|
| 548 | - * |
|
| 549 | - * @return void |
|
| 550 | - */ |
|
| 551 | - public function route_admin_request() |
|
| 552 | - { |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * wp_loaded should fire on the WordPress wp_loaded hook. This fires on a VERY late priority. |
|
| 558 | - * |
|
| 559 | - * @return void |
|
| 560 | - */ |
|
| 561 | - public function wp_loaded() |
|
| 562 | - { |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * admin_init |
|
| 568 | - * |
|
| 569 | - * @return void |
|
| 570 | - * @throws EE_Error |
|
| 571 | - * @throws InvalidArgumentException |
|
| 572 | - * @throws InvalidDataTypeException |
|
| 573 | - * @throws InvalidInterfaceException |
|
| 574 | - * @throws ReflectionException |
|
| 575 | - */ |
|
| 576 | - public function admin_init() |
|
| 577 | - { |
|
| 578 | - |
|
| 579 | - /** |
|
| 580 | - * our cpt models must be instantiated on WordPress post processing routes (wp-admin/post.php), |
|
| 581 | - * so any hooking into core WP routes is taken care of. So in this next few lines of code: |
|
| 582 | - * - check if doing post processing. |
|
| 583 | - * - check if doing post processing of one of EE CPTs |
|
| 584 | - * - instantiate the corresponding EE CPT model for the post_type being processed. |
|
| 585 | - */ |
|
| 586 | - if (isset($_POST['action'], $_POST['post_type']) && $_POST['action'] === 'editpost') { |
|
| 587 | - EE_Registry::instance()->load_core('Register_CPTs'); |
|
| 588 | - EE_Register_CPTs::instantiate_cpt_models($_POST['post_type']); |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - |
|
| 592 | - /** |
|
| 593 | - * This code excludes EE critical pages anywhere `wp_dropdown_pages` is used to create a dropdown for selecting |
|
| 594 | - * critical pages. The only place critical pages need included in a generated dropdown is on the "Critical |
|
| 595 | - * Pages" tab in the EE General Settings Admin page. |
|
| 596 | - * This is for user-proofing. |
|
| 597 | - */ |
|
| 598 | - add_filter('wp_dropdown_pages', array($this, 'modify_dropdown_pages')); |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * Callback for wp_dropdown_pages hook to remove ee critical pages from the dropdown selection. |
|
| 604 | - * |
|
| 605 | - * @param string $output Current output. |
|
| 606 | - * @return string |
|
| 607 | - * @throws InvalidArgumentException |
|
| 608 | - * @throws InvalidDataTypeException |
|
| 609 | - * @throws InvalidInterfaceException |
|
| 610 | - */ |
|
| 611 | - public function modify_dropdown_pages($output) |
|
| 612 | - { |
|
| 613 | - //get critical pages |
|
| 614 | - $critical_pages = EE_Registry::instance()->CFG->core->get_critical_pages_array(); |
|
| 615 | - |
|
| 616 | - //split current output by line break for easier parsing. |
|
| 617 | - $split_output = explode("\n", $output); |
|
| 618 | - |
|
| 619 | - //loop through to remove any critical pages from the array. |
|
| 620 | - foreach ($critical_pages as $page_id) { |
|
| 621 | - $needle = 'value="' . $page_id . '"'; |
|
| 622 | - foreach ($split_output as $key => $haystack) { |
|
| 623 | - if (strpos($haystack, $needle) !== false) { |
|
| 624 | - unset($split_output[$key]); |
|
| 625 | - } |
|
| 626 | - } |
|
| 627 | - } |
|
| 628 | - //replace output with the new contents |
|
| 629 | - return implode("\n", $split_output); |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - |
|
| 633 | - /** |
|
| 634 | - * enqueue all admin scripts that need loaded for admin pages |
|
| 635 | - * |
|
| 636 | - * @return void |
|
| 637 | - */ |
|
| 638 | - public function enqueue_admin_scripts() |
|
| 639 | - { |
|
| 640 | - // this javascript is loaded on every admin page to catch any injections ee needs to add to wp run js. |
|
| 641 | - // Note: the intention of this script is to only do TARGETED injections. I.E, only injecting on certain script |
|
| 642 | - // calls. |
|
| 643 | - wp_enqueue_script( |
|
| 644 | - 'ee-inject-wp', |
|
| 645 | - EE_ADMIN_URL . 'assets/ee-cpt-wp-injects.js', |
|
| 646 | - array('jquery'), |
|
| 647 | - EVENT_ESPRESSO_VERSION, |
|
| 648 | - true |
|
| 649 | - ); |
|
| 650 | - // register cookie script for future dependencies |
|
| 651 | - wp_register_script( |
|
| 652 | - 'jquery-cookie', |
|
| 653 | - EE_THIRD_PARTY_URL . 'joyride/jquery.cookie.js', |
|
| 654 | - array('jquery'), |
|
| 655 | - '2.1', |
|
| 656 | - true |
|
| 657 | - ); |
|
| 658 | - //joyride is turned OFF by default, but prior to the admin_enqueue_scripts hook, can be turned back on again |
|
| 659 | - // via: add_filter('FHEE_load_joyride', '__return_true' ); |
|
| 660 | - if (apply_filters('FHEE_load_joyride', false)) { |
|
| 661 | - //joyride style |
|
| 662 | - wp_register_style('joyride-css', EE_THIRD_PARTY_URL . 'joyride/joyride-2.1.css', array(), '2.1'); |
|
| 663 | - wp_register_style( |
|
| 664 | - 'ee-joyride-css', |
|
| 665 | - EE_GLOBAL_ASSETS_URL . 'css/ee-joyride-styles.css', |
|
| 666 | - array('joyride-css'), |
|
| 667 | - EVENT_ESPRESSO_VERSION |
|
| 668 | - ); |
|
| 669 | - wp_register_script( |
|
| 670 | - 'joyride-modernizr', |
|
| 671 | - EE_THIRD_PARTY_URL . 'joyride/modernizr.mq.js', |
|
| 672 | - array(), |
|
| 673 | - '2.1', |
|
| 674 | - true |
|
| 675 | - ); |
|
| 676 | - //joyride JS |
|
| 677 | - wp_register_script( |
|
| 678 | - 'jquery-joyride', |
|
| 679 | - EE_THIRD_PARTY_URL . 'joyride/jquery.joyride-2.1.js', |
|
| 680 | - array('jquery-cookie', 'joyride-modernizr'), |
|
| 681 | - '2.1', |
|
| 682 | - true |
|
| 683 | - ); |
|
| 684 | - // wanna go for a joyride? |
|
| 685 | - wp_enqueue_style('ee-joyride-css'); |
|
| 686 | - wp_enqueue_script('jquery-joyride'); |
|
| 687 | - } |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - |
|
| 691 | - /** |
|
| 692 | - * display_admin_notices |
|
| 693 | - * |
|
| 694 | - * @return void |
|
| 695 | - */ |
|
| 696 | - public function display_admin_notices() |
|
| 697 | - { |
|
| 698 | - echo EE_Error::get_notices(); |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - |
|
| 702 | - |
|
| 703 | - /** |
|
| 704 | - * @param array $elements |
|
| 705 | - * @return array |
|
| 706 | - * @throws EE_Error |
|
| 707 | - * @throws InvalidArgumentException |
|
| 708 | - * @throws InvalidDataTypeException |
|
| 709 | - * @throws InvalidInterfaceException |
|
| 710 | - */ |
|
| 711 | - public function dashboard_glance_items($elements) |
|
| 712 | - { |
|
| 713 | - $elements = is_array($elements) ? $elements : array($elements); |
|
| 714 | - $events = EEM_Event::instance()->count(); |
|
| 715 | - $items['events']['url'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 716 | - array('page' => 'espresso_events'), |
|
| 717 | - admin_url('admin.php') |
|
| 718 | - ); |
|
| 719 | - $items['events']['text'] = sprintf(_n('%s Event', '%s Events', $events), number_format_i18n($events)); |
|
| 720 | - $items['events']['title'] = esc_html__('Click to view all Events', 'event_espresso'); |
|
| 721 | - $registrations = EEM_Registration::instance()->count( |
|
| 722 | - array( |
|
| 723 | - array( |
|
| 724 | - 'STS_ID' => array('!=', EEM_Registration::status_id_incomplete), |
|
| 725 | - ), |
|
| 726 | - ) |
|
| 727 | - ); |
|
| 728 | - $items['registrations']['url'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 729 | - array('page' => 'espresso_registrations'), |
|
| 730 | - admin_url('admin.php') |
|
| 731 | - ); |
|
| 732 | - $items['registrations']['text'] = sprintf( |
|
| 733 | - _n('%s Registration', '%s Registrations', $registrations), |
|
| 734 | - number_format_i18n($registrations) |
|
| 735 | - ); |
|
| 736 | - $items['registrations']['title'] = esc_html__('Click to view all registrations', 'event_espresso'); |
|
| 737 | - |
|
| 738 | - $items = (array)apply_filters('FHEE__EE_Admin__dashboard_glance_items__items', $items); |
|
| 739 | - |
|
| 740 | - foreach ($items as $type => $item_properties) { |
|
| 741 | - $elements[] = sprintf( |
|
| 742 | - '<a class="ee-dashboard-link-' . $type . '" href="%s" title="%s">%s</a>', |
|
| 743 | - $item_properties['url'], |
|
| 744 | - $item_properties['title'], |
|
| 745 | - $item_properties['text'] |
|
| 746 | - ); |
|
| 747 | - } |
|
| 748 | - return $elements; |
|
| 749 | - } |
|
| 750 | - |
|
| 751 | - |
|
| 752 | - /** |
|
| 753 | - * check_for_invalid_datetime_formats |
|
| 754 | - * if an admin changes their date or time format settings on the WP General Settings admin page, verify that |
|
| 755 | - * their selected format can be parsed by PHP |
|
| 756 | - * |
|
| 757 | - * @param $value |
|
| 758 | - * @param $option |
|
| 759 | - * @throws EE_Error |
|
| 760 | - * @return string |
|
| 761 | - */ |
|
| 762 | - public function check_for_invalid_datetime_formats($value, $option) |
|
| 763 | - { |
|
| 764 | - // check for date_format or time_format |
|
| 765 | - switch ($option) { |
|
| 766 | - case 'date_format': |
|
| 767 | - $date_time_format = $value . ' ' . get_option('time_format'); |
|
| 768 | - break; |
|
| 769 | - case 'time_format': |
|
| 770 | - $date_time_format = get_option('date_format') . ' ' . $value; |
|
| 771 | - break; |
|
| 772 | - default: |
|
| 773 | - $date_time_format = false; |
|
| 774 | - } |
|
| 775 | - // do we have a date_time format to check ? |
|
| 776 | - if ($date_time_format) { |
|
| 777 | - $error_msg = EEH_DTT_Helper::validate_format_string($date_time_format); |
|
| 778 | - |
|
| 779 | - if (is_array($error_msg)) { |
|
| 780 | - $msg = '<p>' |
|
| 781 | - . sprintf( |
|
| 782 | - esc_html__( |
|
| 783 | - 'The following date time "%s" ( %s ) is difficult to be properly parsed by PHP for the following reasons:', |
|
| 784 | - 'event_espresso' |
|
| 785 | - ), |
|
| 786 | - date($date_time_format), |
|
| 787 | - $date_time_format |
|
| 788 | - ) |
|
| 789 | - . '</p><p><ul>'; |
|
| 790 | - |
|
| 791 | - |
|
| 792 | - foreach ($error_msg as $error) { |
|
| 793 | - $msg .= '<li>' . $error . '</li>'; |
|
| 794 | - } |
|
| 795 | - |
|
| 796 | - $msg .= '</ul></p><p>' |
|
| 797 | - . sprintf( |
|
| 798 | - esc_html__( |
|
| 799 | - '%sPlease note that your date and time formats have been reset to "F j, Y" and "g:i a" respectively.%s', |
|
| 800 | - 'event_espresso' |
|
| 801 | - ), |
|
| 802 | - '<span style="color:#D54E21;">', |
|
| 803 | - '</span>' |
|
| 804 | - ) |
|
| 805 | - . '</p>'; |
|
| 806 | - |
|
| 807 | - // trigger WP settings error |
|
| 808 | - add_settings_error( |
|
| 809 | - 'date_format', |
|
| 810 | - 'date_format', |
|
| 811 | - $msg |
|
| 812 | - ); |
|
| 813 | - |
|
| 814 | - // set format to something valid |
|
| 815 | - switch ($option) { |
|
| 816 | - case 'date_format': |
|
| 817 | - $value = 'F j, Y'; |
|
| 818 | - break; |
|
| 819 | - case 'time_format': |
|
| 820 | - $value = 'g:i a'; |
|
| 821 | - break; |
|
| 822 | - } |
|
| 823 | - } |
|
| 824 | - } |
|
| 825 | - return $value; |
|
| 826 | - } |
|
| 827 | - |
|
| 828 | - |
|
| 829 | - /** |
|
| 830 | - * its_eSpresso - converts the less commonly used spelling of "Expresso" to "Espresso" |
|
| 831 | - * |
|
| 832 | - * @param $content |
|
| 833 | - * @return string |
|
| 834 | - */ |
|
| 835 | - public function its_eSpresso($content) |
|
| 836 | - { |
|
| 837 | - return str_replace('[EXPRESSO_', '[ESPRESSO_', $content); |
|
| 838 | - } |
|
| 839 | - |
|
| 840 | - |
|
| 841 | - /** |
|
| 842 | - * espresso_admin_footer |
|
| 843 | - * |
|
| 844 | - * @return string |
|
| 845 | - */ |
|
| 846 | - public function espresso_admin_footer() |
|
| 847 | - { |
|
| 848 | - return \EEH_Template::powered_by_event_espresso('aln-cntr', '', array('utm_content' => 'admin_footer')); |
|
| 849 | - } |
|
| 850 | - |
|
| 851 | - |
|
| 852 | - /** |
|
| 853 | - * static method for registering ee admin page. |
|
| 854 | - * This method is deprecated in favor of the new location in EE_Register_Admin_Page::register. |
|
| 855 | - * |
|
| 856 | - * @since 4.3.0 |
|
| 857 | - * @deprecated 4.3.0 Use EE_Register_Admin_Page::register() instead |
|
| 858 | - * @see EE_Register_Admin_Page::register() |
|
| 859 | - * @param $page_basename |
|
| 860 | - * @param $page_path |
|
| 861 | - * @param array $config |
|
| 862 | - * @return void |
|
| 863 | - * @throws EE_Error |
|
| 864 | - */ |
|
| 865 | - public static function register_ee_admin_page($page_basename, $page_path, $config = array()) |
|
| 866 | - { |
|
| 867 | - EE_Error::doing_it_wrong( |
|
| 868 | - __METHOD__, |
|
| 869 | - sprintf( |
|
| 870 | - esc_html__( |
|
| 871 | - 'Usage is deprecated. Use EE_Register_Admin_Page::register() for registering the %s admin page.', |
|
| 872 | - 'event_espresso' |
|
| 873 | - ), |
|
| 874 | - $page_basename |
|
| 875 | - ), |
|
| 876 | - '4.3' |
|
| 877 | - ); |
|
| 878 | - if (class_exists('EE_Register_Admin_Page')) { |
|
| 879 | - $config['page_path'] = $page_path; |
|
| 880 | - } |
|
| 881 | - EE_Register_Admin_Page::register($page_basename, $config); |
|
| 882 | - } |
|
| 883 | - |
|
| 884 | - |
|
| 885 | - /** |
|
| 886 | - * @deprecated 4.8.41 |
|
| 887 | - * @param int $post_ID |
|
| 888 | - * @param \WP_Post $post |
|
| 889 | - * @return void |
|
| 890 | - */ |
|
| 891 | - public static function parse_post_content_on_save($post_ID, $post) |
|
| 892 | - { |
|
| 893 | - EE_Error::doing_it_wrong( |
|
| 894 | - __METHOD__, |
|
| 895 | - esc_html__('Usage is deprecated', 'event_espresso'), |
|
| 896 | - '4.8.41' |
|
| 897 | - ); |
|
| 898 | - } |
|
| 899 | - |
|
| 900 | - |
|
| 901 | - /** |
|
| 902 | - * @deprecated 4.8.41 |
|
| 903 | - * @param $option |
|
| 904 | - * @param $old_value |
|
| 905 | - * @param $value |
|
| 906 | - * @return void |
|
| 907 | - */ |
|
| 908 | - public function reset_page_for_posts_on_change($option, $old_value, $value) |
|
| 909 | - { |
|
| 910 | - EE_Error::doing_it_wrong( |
|
| 911 | - __METHOD__, |
|
| 912 | - esc_html__('Usage is deprecated', 'event_espresso'), |
|
| 913 | - '4.8.41' |
|
| 914 | - ); |
|
| 915 | - } |
|
| 916 | - |
|
| 917 | - |
|
| 918 | - |
|
| 919 | - /** |
|
| 920 | - * @deprecated 4.9.27 |
|
| 921 | - * @return void |
|
| 922 | - */ |
|
| 923 | - public function get_persistent_admin_notices() |
|
| 924 | - { |
|
| 925 | - EE_Error::doing_it_wrong( |
|
| 926 | - __METHOD__, |
|
| 927 | - sprintf( |
|
| 928 | - __('Usage is deprecated. Use "%1$s" instead.', 'event_espresso'), |
|
| 929 | - '\EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 930 | - ), |
|
| 931 | - '4.9.27' |
|
| 932 | - ); |
|
| 933 | - } |
|
| 934 | - |
|
| 935 | - |
|
| 936 | - |
|
| 937 | - /** |
|
| 938 | - * @deprecated 4.9.27 |
|
| 939 | - * @throws InvalidInterfaceException |
|
| 940 | - * @throws InvalidDataTypeException |
|
| 941 | - * @throws DomainException |
|
| 942 | - */ |
|
| 943 | - public function dismiss_ee_nag_notice_callback() |
|
| 944 | - { |
|
| 945 | - EE_Error::doing_it_wrong( |
|
| 946 | - __METHOD__, |
|
| 947 | - sprintf( |
|
| 948 | - __('Usage is deprecated. Use "%1$s" instead.', 'event_espresso'), |
|
| 949 | - '\EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 950 | - ), |
|
| 951 | - '4.9.27' |
|
| 952 | - ); |
|
| 953 | - $this->persistent_admin_notice_manager->dismissNotice(); |
|
| 954 | - } |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * Returns an array of event archive nav items. |
|
| 493 | + * |
|
| 494 | + * @todo for now this method is just in place so when it gets abstracted further we can substitute in whatever |
|
| 495 | + * method we use for getting the extra nav menu items |
|
| 496 | + * @return array |
|
| 497 | + */ |
|
| 498 | + private function _get_extra_nav_menu_pages_items() |
|
| 499 | + { |
|
| 500 | + $menuitems[] = array( |
|
| 501 | + 'title' => esc_html__('Event List', 'event_espresso'), |
|
| 502 | + 'url' => get_post_type_archive_link('espresso_events'), |
|
| 503 | + 'description' => esc_html__('Archive page for all events.', 'event_espresso'), |
|
| 504 | + ); |
|
| 505 | + return apply_filters('FHEE__EE_Admin__get_extra_nav_menu_pages_items', $menuitems); |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + |
|
| 509 | + /** |
|
| 510 | + * Setup nav menu walker item for usage in the event archive nav menu metabox. It receives a menu_item array with |
|
| 511 | + * the properties and converts it to the menu item object. |
|
| 512 | + * |
|
| 513 | + * @see wp_setup_nav_menu_item() in wp-includes/nav-menu.php |
|
| 514 | + * @param $menu_item_values |
|
| 515 | + * @return stdClass |
|
| 516 | + */ |
|
| 517 | + private function _setup_extra_nav_menu_pages_items($menu_item_values) |
|
| 518 | + { |
|
| 519 | + $menu_item = new stdClass(); |
|
| 520 | + $keys = array( |
|
| 521 | + 'ID' => 0, |
|
| 522 | + 'db_id' => 0, |
|
| 523 | + 'menu_item_parent' => 0, |
|
| 524 | + 'object_id' => -1, |
|
| 525 | + 'post_parent' => 0, |
|
| 526 | + 'type' => 'custom', |
|
| 527 | + 'object' => '', |
|
| 528 | + 'type_label' => esc_html__('Extra Nav Menu Item', 'event_espresso'), |
|
| 529 | + 'title' => '', |
|
| 530 | + 'url' => '', |
|
| 531 | + 'target' => '', |
|
| 532 | + 'attr_title' => '', |
|
| 533 | + 'description' => '', |
|
| 534 | + 'classes' => array(), |
|
| 535 | + 'xfn' => '', |
|
| 536 | + ); |
|
| 537 | + |
|
| 538 | + foreach ($keys as $key => $value) { |
|
| 539 | + $menu_item->{$key} = isset($menu_item_values[$key]) ? $menu_item_values[$key] : $value; |
|
| 540 | + } |
|
| 541 | + return $menu_item; |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * This is the action hook for the AHEE__EE_Admin_Page__route_admin_request hook that fires off right before an |
|
| 547 | + * EE_Admin_Page route is called. |
|
| 548 | + * |
|
| 549 | + * @return void |
|
| 550 | + */ |
|
| 551 | + public function route_admin_request() |
|
| 552 | + { |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * wp_loaded should fire on the WordPress wp_loaded hook. This fires on a VERY late priority. |
|
| 558 | + * |
|
| 559 | + * @return void |
|
| 560 | + */ |
|
| 561 | + public function wp_loaded() |
|
| 562 | + { |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * admin_init |
|
| 568 | + * |
|
| 569 | + * @return void |
|
| 570 | + * @throws EE_Error |
|
| 571 | + * @throws InvalidArgumentException |
|
| 572 | + * @throws InvalidDataTypeException |
|
| 573 | + * @throws InvalidInterfaceException |
|
| 574 | + * @throws ReflectionException |
|
| 575 | + */ |
|
| 576 | + public function admin_init() |
|
| 577 | + { |
|
| 578 | + |
|
| 579 | + /** |
|
| 580 | + * our cpt models must be instantiated on WordPress post processing routes (wp-admin/post.php), |
|
| 581 | + * so any hooking into core WP routes is taken care of. So in this next few lines of code: |
|
| 582 | + * - check if doing post processing. |
|
| 583 | + * - check if doing post processing of one of EE CPTs |
|
| 584 | + * - instantiate the corresponding EE CPT model for the post_type being processed. |
|
| 585 | + */ |
|
| 586 | + if (isset($_POST['action'], $_POST['post_type']) && $_POST['action'] === 'editpost') { |
|
| 587 | + EE_Registry::instance()->load_core('Register_CPTs'); |
|
| 588 | + EE_Register_CPTs::instantiate_cpt_models($_POST['post_type']); |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + |
|
| 592 | + /** |
|
| 593 | + * This code excludes EE critical pages anywhere `wp_dropdown_pages` is used to create a dropdown for selecting |
|
| 594 | + * critical pages. The only place critical pages need included in a generated dropdown is on the "Critical |
|
| 595 | + * Pages" tab in the EE General Settings Admin page. |
|
| 596 | + * This is for user-proofing. |
|
| 597 | + */ |
|
| 598 | + add_filter('wp_dropdown_pages', array($this, 'modify_dropdown_pages')); |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * Callback for wp_dropdown_pages hook to remove ee critical pages from the dropdown selection. |
|
| 604 | + * |
|
| 605 | + * @param string $output Current output. |
|
| 606 | + * @return string |
|
| 607 | + * @throws InvalidArgumentException |
|
| 608 | + * @throws InvalidDataTypeException |
|
| 609 | + * @throws InvalidInterfaceException |
|
| 610 | + */ |
|
| 611 | + public function modify_dropdown_pages($output) |
|
| 612 | + { |
|
| 613 | + //get critical pages |
|
| 614 | + $critical_pages = EE_Registry::instance()->CFG->core->get_critical_pages_array(); |
|
| 615 | + |
|
| 616 | + //split current output by line break for easier parsing. |
|
| 617 | + $split_output = explode("\n", $output); |
|
| 618 | + |
|
| 619 | + //loop through to remove any critical pages from the array. |
|
| 620 | + foreach ($critical_pages as $page_id) { |
|
| 621 | + $needle = 'value="' . $page_id . '"'; |
|
| 622 | + foreach ($split_output as $key => $haystack) { |
|
| 623 | + if (strpos($haystack, $needle) !== false) { |
|
| 624 | + unset($split_output[$key]); |
|
| 625 | + } |
|
| 626 | + } |
|
| 627 | + } |
|
| 628 | + //replace output with the new contents |
|
| 629 | + return implode("\n", $split_output); |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + |
|
| 633 | + /** |
|
| 634 | + * enqueue all admin scripts that need loaded for admin pages |
|
| 635 | + * |
|
| 636 | + * @return void |
|
| 637 | + */ |
|
| 638 | + public function enqueue_admin_scripts() |
|
| 639 | + { |
|
| 640 | + // this javascript is loaded on every admin page to catch any injections ee needs to add to wp run js. |
|
| 641 | + // Note: the intention of this script is to only do TARGETED injections. I.E, only injecting on certain script |
|
| 642 | + // calls. |
|
| 643 | + wp_enqueue_script( |
|
| 644 | + 'ee-inject-wp', |
|
| 645 | + EE_ADMIN_URL . 'assets/ee-cpt-wp-injects.js', |
|
| 646 | + array('jquery'), |
|
| 647 | + EVENT_ESPRESSO_VERSION, |
|
| 648 | + true |
|
| 649 | + ); |
|
| 650 | + // register cookie script for future dependencies |
|
| 651 | + wp_register_script( |
|
| 652 | + 'jquery-cookie', |
|
| 653 | + EE_THIRD_PARTY_URL . 'joyride/jquery.cookie.js', |
|
| 654 | + array('jquery'), |
|
| 655 | + '2.1', |
|
| 656 | + true |
|
| 657 | + ); |
|
| 658 | + //joyride is turned OFF by default, but prior to the admin_enqueue_scripts hook, can be turned back on again |
|
| 659 | + // via: add_filter('FHEE_load_joyride', '__return_true' ); |
|
| 660 | + if (apply_filters('FHEE_load_joyride', false)) { |
|
| 661 | + //joyride style |
|
| 662 | + wp_register_style('joyride-css', EE_THIRD_PARTY_URL . 'joyride/joyride-2.1.css', array(), '2.1'); |
|
| 663 | + wp_register_style( |
|
| 664 | + 'ee-joyride-css', |
|
| 665 | + EE_GLOBAL_ASSETS_URL . 'css/ee-joyride-styles.css', |
|
| 666 | + array('joyride-css'), |
|
| 667 | + EVENT_ESPRESSO_VERSION |
|
| 668 | + ); |
|
| 669 | + wp_register_script( |
|
| 670 | + 'joyride-modernizr', |
|
| 671 | + EE_THIRD_PARTY_URL . 'joyride/modernizr.mq.js', |
|
| 672 | + array(), |
|
| 673 | + '2.1', |
|
| 674 | + true |
|
| 675 | + ); |
|
| 676 | + //joyride JS |
|
| 677 | + wp_register_script( |
|
| 678 | + 'jquery-joyride', |
|
| 679 | + EE_THIRD_PARTY_URL . 'joyride/jquery.joyride-2.1.js', |
|
| 680 | + array('jquery-cookie', 'joyride-modernizr'), |
|
| 681 | + '2.1', |
|
| 682 | + true |
|
| 683 | + ); |
|
| 684 | + // wanna go for a joyride? |
|
| 685 | + wp_enqueue_style('ee-joyride-css'); |
|
| 686 | + wp_enqueue_script('jquery-joyride'); |
|
| 687 | + } |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + |
|
| 691 | + /** |
|
| 692 | + * display_admin_notices |
|
| 693 | + * |
|
| 694 | + * @return void |
|
| 695 | + */ |
|
| 696 | + public function display_admin_notices() |
|
| 697 | + { |
|
| 698 | + echo EE_Error::get_notices(); |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + |
|
| 702 | + |
|
| 703 | + /** |
|
| 704 | + * @param array $elements |
|
| 705 | + * @return array |
|
| 706 | + * @throws EE_Error |
|
| 707 | + * @throws InvalidArgumentException |
|
| 708 | + * @throws InvalidDataTypeException |
|
| 709 | + * @throws InvalidInterfaceException |
|
| 710 | + */ |
|
| 711 | + public function dashboard_glance_items($elements) |
|
| 712 | + { |
|
| 713 | + $elements = is_array($elements) ? $elements : array($elements); |
|
| 714 | + $events = EEM_Event::instance()->count(); |
|
| 715 | + $items['events']['url'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 716 | + array('page' => 'espresso_events'), |
|
| 717 | + admin_url('admin.php') |
|
| 718 | + ); |
|
| 719 | + $items['events']['text'] = sprintf(_n('%s Event', '%s Events', $events), number_format_i18n($events)); |
|
| 720 | + $items['events']['title'] = esc_html__('Click to view all Events', 'event_espresso'); |
|
| 721 | + $registrations = EEM_Registration::instance()->count( |
|
| 722 | + array( |
|
| 723 | + array( |
|
| 724 | + 'STS_ID' => array('!=', EEM_Registration::status_id_incomplete), |
|
| 725 | + ), |
|
| 726 | + ) |
|
| 727 | + ); |
|
| 728 | + $items['registrations']['url'] = EE_Admin_Page::add_query_args_and_nonce( |
|
| 729 | + array('page' => 'espresso_registrations'), |
|
| 730 | + admin_url('admin.php') |
|
| 731 | + ); |
|
| 732 | + $items['registrations']['text'] = sprintf( |
|
| 733 | + _n('%s Registration', '%s Registrations', $registrations), |
|
| 734 | + number_format_i18n($registrations) |
|
| 735 | + ); |
|
| 736 | + $items['registrations']['title'] = esc_html__('Click to view all registrations', 'event_espresso'); |
|
| 737 | + |
|
| 738 | + $items = (array)apply_filters('FHEE__EE_Admin__dashboard_glance_items__items', $items); |
|
| 739 | + |
|
| 740 | + foreach ($items as $type => $item_properties) { |
|
| 741 | + $elements[] = sprintf( |
|
| 742 | + '<a class="ee-dashboard-link-' . $type . '" href="%s" title="%s">%s</a>', |
|
| 743 | + $item_properties['url'], |
|
| 744 | + $item_properties['title'], |
|
| 745 | + $item_properties['text'] |
|
| 746 | + ); |
|
| 747 | + } |
|
| 748 | + return $elements; |
|
| 749 | + } |
|
| 750 | + |
|
| 751 | + |
|
| 752 | + /** |
|
| 753 | + * check_for_invalid_datetime_formats |
|
| 754 | + * if an admin changes their date or time format settings on the WP General Settings admin page, verify that |
|
| 755 | + * their selected format can be parsed by PHP |
|
| 756 | + * |
|
| 757 | + * @param $value |
|
| 758 | + * @param $option |
|
| 759 | + * @throws EE_Error |
|
| 760 | + * @return string |
|
| 761 | + */ |
|
| 762 | + public function check_for_invalid_datetime_formats($value, $option) |
|
| 763 | + { |
|
| 764 | + // check for date_format or time_format |
|
| 765 | + switch ($option) { |
|
| 766 | + case 'date_format': |
|
| 767 | + $date_time_format = $value . ' ' . get_option('time_format'); |
|
| 768 | + break; |
|
| 769 | + case 'time_format': |
|
| 770 | + $date_time_format = get_option('date_format') . ' ' . $value; |
|
| 771 | + break; |
|
| 772 | + default: |
|
| 773 | + $date_time_format = false; |
|
| 774 | + } |
|
| 775 | + // do we have a date_time format to check ? |
|
| 776 | + if ($date_time_format) { |
|
| 777 | + $error_msg = EEH_DTT_Helper::validate_format_string($date_time_format); |
|
| 778 | + |
|
| 779 | + if (is_array($error_msg)) { |
|
| 780 | + $msg = '<p>' |
|
| 781 | + . sprintf( |
|
| 782 | + esc_html__( |
|
| 783 | + 'The following date time "%s" ( %s ) is difficult to be properly parsed by PHP for the following reasons:', |
|
| 784 | + 'event_espresso' |
|
| 785 | + ), |
|
| 786 | + date($date_time_format), |
|
| 787 | + $date_time_format |
|
| 788 | + ) |
|
| 789 | + . '</p><p><ul>'; |
|
| 790 | + |
|
| 791 | + |
|
| 792 | + foreach ($error_msg as $error) { |
|
| 793 | + $msg .= '<li>' . $error . '</li>'; |
|
| 794 | + } |
|
| 795 | + |
|
| 796 | + $msg .= '</ul></p><p>' |
|
| 797 | + . sprintf( |
|
| 798 | + esc_html__( |
|
| 799 | + '%sPlease note that your date and time formats have been reset to "F j, Y" and "g:i a" respectively.%s', |
|
| 800 | + 'event_espresso' |
|
| 801 | + ), |
|
| 802 | + '<span style="color:#D54E21;">', |
|
| 803 | + '</span>' |
|
| 804 | + ) |
|
| 805 | + . '</p>'; |
|
| 806 | + |
|
| 807 | + // trigger WP settings error |
|
| 808 | + add_settings_error( |
|
| 809 | + 'date_format', |
|
| 810 | + 'date_format', |
|
| 811 | + $msg |
|
| 812 | + ); |
|
| 813 | + |
|
| 814 | + // set format to something valid |
|
| 815 | + switch ($option) { |
|
| 816 | + case 'date_format': |
|
| 817 | + $value = 'F j, Y'; |
|
| 818 | + break; |
|
| 819 | + case 'time_format': |
|
| 820 | + $value = 'g:i a'; |
|
| 821 | + break; |
|
| 822 | + } |
|
| 823 | + } |
|
| 824 | + } |
|
| 825 | + return $value; |
|
| 826 | + } |
|
| 827 | + |
|
| 828 | + |
|
| 829 | + /** |
|
| 830 | + * its_eSpresso - converts the less commonly used spelling of "Expresso" to "Espresso" |
|
| 831 | + * |
|
| 832 | + * @param $content |
|
| 833 | + * @return string |
|
| 834 | + */ |
|
| 835 | + public function its_eSpresso($content) |
|
| 836 | + { |
|
| 837 | + return str_replace('[EXPRESSO_', '[ESPRESSO_', $content); |
|
| 838 | + } |
|
| 839 | + |
|
| 840 | + |
|
| 841 | + /** |
|
| 842 | + * espresso_admin_footer |
|
| 843 | + * |
|
| 844 | + * @return string |
|
| 845 | + */ |
|
| 846 | + public function espresso_admin_footer() |
|
| 847 | + { |
|
| 848 | + return \EEH_Template::powered_by_event_espresso('aln-cntr', '', array('utm_content' => 'admin_footer')); |
|
| 849 | + } |
|
| 850 | + |
|
| 851 | + |
|
| 852 | + /** |
|
| 853 | + * static method for registering ee admin page. |
|
| 854 | + * This method is deprecated in favor of the new location in EE_Register_Admin_Page::register. |
|
| 855 | + * |
|
| 856 | + * @since 4.3.0 |
|
| 857 | + * @deprecated 4.3.0 Use EE_Register_Admin_Page::register() instead |
|
| 858 | + * @see EE_Register_Admin_Page::register() |
|
| 859 | + * @param $page_basename |
|
| 860 | + * @param $page_path |
|
| 861 | + * @param array $config |
|
| 862 | + * @return void |
|
| 863 | + * @throws EE_Error |
|
| 864 | + */ |
|
| 865 | + public static function register_ee_admin_page($page_basename, $page_path, $config = array()) |
|
| 866 | + { |
|
| 867 | + EE_Error::doing_it_wrong( |
|
| 868 | + __METHOD__, |
|
| 869 | + sprintf( |
|
| 870 | + esc_html__( |
|
| 871 | + 'Usage is deprecated. Use EE_Register_Admin_Page::register() for registering the %s admin page.', |
|
| 872 | + 'event_espresso' |
|
| 873 | + ), |
|
| 874 | + $page_basename |
|
| 875 | + ), |
|
| 876 | + '4.3' |
|
| 877 | + ); |
|
| 878 | + if (class_exists('EE_Register_Admin_Page')) { |
|
| 879 | + $config['page_path'] = $page_path; |
|
| 880 | + } |
|
| 881 | + EE_Register_Admin_Page::register($page_basename, $config); |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + |
|
| 885 | + /** |
|
| 886 | + * @deprecated 4.8.41 |
|
| 887 | + * @param int $post_ID |
|
| 888 | + * @param \WP_Post $post |
|
| 889 | + * @return void |
|
| 890 | + */ |
|
| 891 | + public static function parse_post_content_on_save($post_ID, $post) |
|
| 892 | + { |
|
| 893 | + EE_Error::doing_it_wrong( |
|
| 894 | + __METHOD__, |
|
| 895 | + esc_html__('Usage is deprecated', 'event_espresso'), |
|
| 896 | + '4.8.41' |
|
| 897 | + ); |
|
| 898 | + } |
|
| 899 | + |
|
| 900 | + |
|
| 901 | + /** |
|
| 902 | + * @deprecated 4.8.41 |
|
| 903 | + * @param $option |
|
| 904 | + * @param $old_value |
|
| 905 | + * @param $value |
|
| 906 | + * @return void |
|
| 907 | + */ |
|
| 908 | + public function reset_page_for_posts_on_change($option, $old_value, $value) |
|
| 909 | + { |
|
| 910 | + EE_Error::doing_it_wrong( |
|
| 911 | + __METHOD__, |
|
| 912 | + esc_html__('Usage is deprecated', 'event_espresso'), |
|
| 913 | + '4.8.41' |
|
| 914 | + ); |
|
| 915 | + } |
|
| 916 | + |
|
| 917 | + |
|
| 918 | + |
|
| 919 | + /** |
|
| 920 | + * @deprecated 4.9.27 |
|
| 921 | + * @return void |
|
| 922 | + */ |
|
| 923 | + public function get_persistent_admin_notices() |
|
| 924 | + { |
|
| 925 | + EE_Error::doing_it_wrong( |
|
| 926 | + __METHOD__, |
|
| 927 | + sprintf( |
|
| 928 | + __('Usage is deprecated. Use "%1$s" instead.', 'event_espresso'), |
|
| 929 | + '\EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 930 | + ), |
|
| 931 | + '4.9.27' |
|
| 932 | + ); |
|
| 933 | + } |
|
| 934 | + |
|
| 935 | + |
|
| 936 | + |
|
| 937 | + /** |
|
| 938 | + * @deprecated 4.9.27 |
|
| 939 | + * @throws InvalidInterfaceException |
|
| 940 | + * @throws InvalidDataTypeException |
|
| 941 | + * @throws DomainException |
|
| 942 | + */ |
|
| 943 | + public function dismiss_ee_nag_notice_callback() |
|
| 944 | + { |
|
| 945 | + EE_Error::doing_it_wrong( |
|
| 946 | + __METHOD__, |
|
| 947 | + sprintf( |
|
| 948 | + __('Usage is deprecated. Use "%1$s" instead.', 'event_espresso'), |
|
| 949 | + '\EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 950 | + ), |
|
| 951 | + '4.9.27' |
|
| 952 | + ); |
|
| 953 | + $this->persistent_admin_notice_manager->dismissNotice(); |
|
| 954 | + } |
|
| 955 | 955 | } |