| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to handle all sales rep database interactions for the Order Tracking plugin |
| 4 |
*/ |
| 5 |
|
| 6 |
if ( !defined( 'ABSPATH' ) ) |
| 7 |
exit; |
| 8 |
|
| 9 |
if ( !class_exists( 'ewdotpSalesRepManager' ) ) { |
| 10 |
class ewdotpSalesRepManager { |
| 11 |
|
| 12 |
// The name of the sales rep table, set in the constructor |
| 13 |
public $sales_reps_table_name; |
| 14 |
|
| 15 |
// The name of the meta table, set in the constructor |
| 16 |
public $meta_table_name; |
| 17 |
|
| 18 |
// Array containing the arguments for the query |
| 19 |
public $args = array(); |
| 20 |
|
| 21 |
// Array containing retrieved sales rep objects |
| 22 |
public $sales_reps = array(); |
| 23 |
|
| 24 |
public function __construct() { |
| 25 |
global $wpdb; |
| 26 |
|
| 27 |
$this->sales_reps_table_name = $wpdb->prefix . "EWD_OTP_Sales_Reps"; |
| 28 |
$this->meta_table_name = $wpdb->prefix . "EWD_OTP_Fields_Meta"; |
| 29 |
|
| 30 |
if ( get_transient( 'ewd-otp-update-tables' ) ) { |
| 31 |
|
| 32 |
add_action( 'plugins_loaded', array( $this, 'create_tables' ) ); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Creates the tables used to store sales rep and their meta information |
| 38 |
* @since 3.0.0 |
| 39 |
*/ |
| 40 |
public function create_tables() { |
| 41 |
|
| 42 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 43 |
|
| 44 |
$sql = "CREATE TABLE $this->sales_reps_table_name ( |
| 45 |
Sales_Rep_ID mediumint(9) NOT NULL AUTO_INCREMENT, |
| 46 |
Sales_Rep_Number text DEFAULT '' NOT NULL, |
| 47 |
Sales_Rep_First_Name text DEFAULT '' NOT NULL, |
| 48 |
Sales_Rep_Last_Name text DEFAULT '' NOT NULL, |
| 49 |
Sales_Rep_Email text DEFAULT '' NOT NULL, |
| 50 |
Sales_Rep_WP_ID mediumint(9) DEFAULT 0 NOT NULL, |
| 51 |
Sales_Rep_Created datetime DEFAULT '0000-00-00 00:00:00' NULL, |
| 52 |
UNIQUE KEY id (Sales_Rep_ID) |
| 53 |
) |
| 54 |
DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"; |
| 55 |
|
| 56 |
dbDelta($sql); |
| 57 |
|
| 58 |
$sql = "CREATE TABLE $this->meta_table_name ( |
| 59 |
Meta_ID mediumint(9) NOT NULL AUTO_INCREMENT, |
| 60 |
Field_ID mediumint(9) DEFAULT '0', |
| 61 |
Order_ID mediumint(9) DEFAULT '0', |
| 62 |
Customer_ID mediumint(9) DEFAULT '0', |
| 63 |
Sales_Rep_ID mediumint(9) DEFAULT '0', |
| 64 |
Meta_Value text DEFAULT '' NOT NULL, |
| 65 |
UNIQUE KEY id (Meta_ID) |
| 66 |
) |
| 67 |
DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"; |
| 68 |
|
| 69 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 70 |
|
| 71 |
dbDelta($sql); |
| 72 |
|
| 73 |
$this->copy_sales_rep_id_to_number_field(); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Copies the sales rep ID database field to the number field, when the number field is blank |
| 78 |
* @since 3.2.0 |
| 79 |
*/ |
| 80 |
public function copy_sales_rep_id_to_number_field() { |
| 81 |
global $wpdb; |
| 82 |
|
| 83 |
$wpdb->query( "UPDATE $this->sales_reps_table_name SET Sales_Rep_Number=Sales_Rep_ID WHERE Sales_Rep_Number=''" ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns a single sales rep given their sales rep ID |
| 88 |
* @since 3.0.0 |
| 89 |
*/ |
| 90 |
public function get_sales_rep_from_id( $sales_rep_id ) { |
| 91 |
global $wpdb; |
| 92 |
|
| 93 |
$db_sales_rep = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->sales_reps_table_name WHERE Sales_Rep_ID=%d", $sales_rep_id ) ); |
| 94 |
|
| 95 |
return $db_sales_rep; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns a single sales rep given their WP ID |
| 100 |
* @since 3.0.0 |
| 101 |
*/ |
| 102 |
public function get_sales_rep_from_wp_id( $wp_id ) { |
| 103 |
global $wpdb; |
| 104 |
|
| 105 |
$db_sales_rep = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->sales_reps_table_name WHERE Sales_Rep_WP_ID=%d", $wp_id ) ); |
| 106 |
|
| 107 |
return $db_sales_rep; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Returns a sales rep's ID given their WP ID |
| 112 |
* @since 3.0.0 |
| 113 |
*/ |
| 114 |
public function get_sales_rep_id_from_wp_id( $wp_id ) { |
| 115 |
global $wpdb; |
| 116 |
|
| 117 |
$sales_rep_id = $wpdb->get_var( $wpdb->prepare( "SELECT Sales_Rep_ID FROM $this->sales_reps_table_name WHERE Sales_Rep_WP_ID=%d", $wp_id ) ); |
| 118 |
|
| 119 |
return $sales_rep_id; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns a sales rep's ID given their WP ID |
| 124 |
* @since 3.0.0 |
| 125 |
*/ |
| 126 |
public function get_sales_rep_id_from_number( $sales_rep_number ) { |
| 127 |
global $wpdb; |
| 128 |
|
| 129 |
$sales_rep_id = $wpdb->get_var( $wpdb->prepare( "SELECT Sales_Rep_ID FROM $this->sales_reps_table_name WHERE Sales_Rep_Number=%d", $sales_rep_number ) ); |
| 130 |
|
| 131 |
return $sales_rep_id; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Returns sales reps matching the arguments supplied |
| 136 |
* @since 3.0.0 |
| 137 |
*/ |
| 138 |
public function get_matching_sales_reps( $args ) { |
| 139 |
|
| 140 |
$this->sales_reps = array(); |
| 141 |
|
| 142 |
$defaults = array( |
| 143 |
'sales_reps_per_page' => 20, |
| 144 |
'order' => 'ASC', |
| 145 |
'paged' => 1, |
| 146 |
); |
| 147 |
|
| 148 |
$this->args = wp_parse_args( $args, $defaults ); |
| 149 |
|
| 150 |
$this->run_query(); |
| 151 |
|
| 152 |
return $this->sales_reps; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Return the counts for sales reps being displayed on the admin sales reps page |
| 157 |
* @since 3.0.0 |
| 158 |
*/ |
| 159 |
public function get_sales_rep_counts( $args ) { |
| 160 |
global $wpdb; |
| 161 |
|
| 162 |
$query_string = "SELECT count( * ) AS num_sales_reps |
| 163 |
FROM $this->sales_reps_table_name |
| 164 |
WHERE 1=%d |
| 165 |
"; |
| 166 |
|
| 167 |
$query_args = array(1); |
| 168 |
|
| 169 |
if ( ! empty( $args['first_name'] ) ) { |
| 170 |
|
| 171 |
$query_string .= " AND Sales_Rep_First_Name=%s"; |
| 172 |
$query_args[] = sanitize_text_field( $args['first_name'] ); |
| 173 |
} |
| 174 |
|
| 175 |
if ( ! empty( $args['last_name'] ) ) { |
| 176 |
|
| 177 |
$query_string .= " AND Sales_Rep_Last_Name=%s"; |
| 178 |
$query_args[] = sanitize_text_field( $args['last_name'] ); |
| 179 |
} |
| 180 |
|
| 181 |
if ( ! empty( $args['email'] ) ) { |
| 182 |
|
| 183 |
$query_string .= " AND Sales_Rep_Email=%s"; |
| 184 |
$query_args[] = sanitize_text_field( $args['email'] ); |
| 185 |
} |
| 186 |
|
| 187 |
$result = $wpdb->get_results( $wpdb->prepare( $query_string, $query_args ) ); |
| 188 |
|
| 189 |
$counts = array( |
| 190 |
'total' => $result[0]->num_sales_reps |
| 191 |
); |
| 192 |
|
| 193 |
return $counts; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Create and run the SQL query based on the arguments received |
| 198 |
* @since 3.0.0 |
| 199 |
*/ |
| 200 |
public function run_query() { |
| 201 |
global $wpdb; |
| 202 |
|
| 203 |
$args = $this->args; |
| 204 |
|
| 205 |
$query_string = "SELECT * FROM $this->sales_reps_table_name WHERE 1=%d"; |
| 206 |
|
| 207 |
$query_args = array( 1 ); |
| 208 |
|
| 209 |
if ( ! empty( $args['id'] ) ) { |
| 210 |
|
| 211 |
$query_string .= " AND Sales_Rep_ID=%d"; |
| 212 |
$query_args[] = intval( $args['id'] ); |
| 213 |
} |
| 214 |
|
| 215 |
if ( ! empty( $args['number'] ) ) { |
| 216 |
|
| 217 |
$query_string .= " AND Sales_Rep_Number=%s"; |
| 218 |
$query_args[] = $args['number']; |
| 219 |
} |
| 220 |
|
| 221 |
if ( ! empty( $args['first_name'] ) ) { |
| 222 |
|
| 223 |
$query_string .= " AND Sales_Rep_First_Name=%s"; |
| 224 |
$query_args[] = $args['first_name']; |
| 225 |
} |
| 226 |
|
| 227 |
if ( ! empty( $args['last_name'] ) ) { |
| 228 |
|
| 229 |
$query_string .= " AND Sales_Rep_Last_Name=%s"; |
| 230 |
$query_args[] = $args['last_name']; |
| 231 |
} |
| 232 |
|
| 233 |
if ( ! empty( $args['wp_id'] ) ) { |
| 234 |
|
| 235 |
$query_string .= " AND Sales_Rep_WP_ID=%d"; |
| 236 |
$query_args[] = intval( $args['wp_id'] ); |
| 237 |
} |
| 238 |
|
| 239 |
if ( ! empty( $args['email'] ) ) { |
| 240 |
|
| 241 |
$query_string .= " AND Sales_Rep_Email=%s"; |
| 242 |
$query_args[] = $args['email']; |
| 243 |
} |
| 244 |
|
| 245 |
if ( ! empty( $args['orderby'] ) ) { |
| 246 |
|
| 247 |
$query_string .= ' ORDER BY ' . esc_sql( $args['orderby'] ) . ' ' . ( strtolower( $args['order'] ) == 'desc' ? 'DESC' : 'ASC' ); |
| 248 |
} |
| 249 |
|
| 250 |
if ( $args['sales_reps_per_page'] > 0 ) { |
| 251 |
|
| 252 |
$query_string .= ' LIMIT ' . intval( ( $args['paged'] - 1 ) * $args['sales_reps_per_page'] ) . ', ' . intval( $args['sales_reps_per_page'] ); |
| 253 |
} |
| 254 |
|
| 255 |
$db_sales_reps = $wpdb->get_results( $wpdb->prepare( $query_string, $query_args ) ); |
| 256 |
|
| 257 |
foreach ( $db_sales_reps as $db_sales_rep ) { |
| 258 |
|
| 259 |
$sales_rep = new ewdotpSalesRep(); |
| 260 |
|
| 261 |
$sales_rep->load_sales_rep( $db_sales_rep ); |
| 262 |
|
| 263 |
$this->sales_reps[] = $sales_rep; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Returns the value for a given field/sales rep id pair |
| 269 |
* @since 3.0.0 |
| 270 |
*/ |
| 271 |
public function get_sales_rep_field( $field, $sales_rep_id ) { |
| 272 |
global $wpdb; |
| 273 |
|
| 274 |
$db_sales_rep = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->sales_reps_table_name WHERE Sales_Rep_ID=%d", $sales_rep_id ) ); |
| 275 |
|
| 276 |
$sales_rep = new ewdotpSalesRep(); |
| 277 |
$sales_rep->load_sales_rep( $db_sales_rep ); |
| 278 |
|
| 279 |
return ! empty( $sales_rep->$field ) ? $sales_rep->$field : ''; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Returns the value for a given custom_field/sales rep pair |
| 284 |
* @since 3.0.0 |
| 285 |
*/ |
| 286 |
public function get_field_value( $custom_field_id, $sales_rep_id ) { |
| 287 |
global $wpdb; |
| 288 |
|
| 289 |
return $wpdb->get_var( $wpdb->prepare( "SELECT Meta_Value FROM $this->meta_table_name WHERE Field_ID=%d AND Sales_Rep_ID=%d", $custom_field_id, $sales_rep_id ) ); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
/** |
| 294 |
* Accepts a sales rep object, inserts it into the database, and returns the ID of the newly inserted sales rep |
| 295 |
* @since 3.0.0 |
| 296 |
*/ |
| 297 |
public function insert_sales_rep( $sales_rep ) { |
| 298 |
global $wpdb; |
| 299 |
global $ewd_otp_controller; |
| 300 |
|
| 301 |
$query_args = array( |
| 302 |
'Sales_Rep_Number' => ! empty( $sales_rep->number ) ? $sales_rep->number : '', |
| 303 |
'Sales_Rep_First_Name' => ! empty( $sales_rep->first_name ) ? $sales_rep->first_name : '', |
| 304 |
'Sales_Rep_Last_Name' => ! empty( $sales_rep->last_name ) ? $sales_rep->last_name : '', |
| 305 |
'Sales_Rep_Email' => ! empty( $sales_rep->email ) ? $sales_rep->email : '', |
| 306 |
'Sales_Rep_WP_ID' => ! empty( $sales_rep->wp_id ) ? $sales_rep->wp_id : 0, |
| 307 |
'Sales_Rep_Created' => date( 'Y-m-d H:i:s' ), |
| 308 |
); |
| 309 |
|
| 310 |
$wpdb->insert( |
| 311 |
$this->sales_reps_table_name, |
| 312 |
$query_args |
| 313 |
); |
| 314 |
|
| 315 |
$sales_rep_id = $wpdb->insert_id; |
| 316 |
|
| 317 |
if ( ! $sales_rep_id ) { return $sales_rep_id; } |
| 318 |
|
| 319 |
$custom_fields = $ewd_otp_controller->settings->get_sales_rep_custom_fields(); |
| 320 |
|
| 321 |
foreach ( $custom_fields as $custom_field ) { |
| 322 |
|
| 323 |
if ( empty( $sales_rep->custom_fields[ $custom_field->id ] ) ) { continue; } |
| 324 |
|
| 325 |
$query_args = array( |
| 326 |
'Field_ID' => $custom_field->id, |
| 327 |
'Sales_Rep_ID' => $sales_rep_id, |
| 328 |
'Meta_Value' => $sales_rep->custom_fields[ $custom_field->id ] |
| 329 |
); |
| 330 |
|
| 331 |
$wpdb->insert( |
| 332 |
$this->meta_table_name, |
| 333 |
$query_args |
| 334 |
); |
| 335 |
} |
| 336 |
|
| 337 |
return $sales_rep_id; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Accepts a sales rep object, updates it in the database, and returns the ID if successful or false otherwise |
| 342 |
* @since 3.0.0 |
| 343 |
*/ |
| 344 |
public function update_sales_rep( $sales_rep ) { |
| 345 |
global $wpdb; |
| 346 |
global $ewd_otp_controller; |
| 347 |
|
| 348 |
if ( empty( $sales_rep->id ) ) { return false; } |
| 349 |
|
| 350 |
$query_args = array( |
| 351 |
'Sales_Rep_Number' => ! empty( $sales_rep->number ) ? $sales_rep->number : '', |
| 352 |
'Sales_Rep_First_Name' => ! empty( $sales_rep->first_name ) ? $sales_rep->first_name : '', |
| 353 |
'Sales_Rep_Last_Name' => ! empty( $sales_rep->last_name ) ? $sales_rep->last_name : '', |
| 354 |
'Sales_Rep_Email' => ! empty( $sales_rep->email ) ? $sales_rep->email : '', |
| 355 |
'Sales_Rep_WP_ID' => ! empty( $sales_rep->wp_id ) ? $sales_rep->wp_id : 0, |
| 356 |
); |
| 357 |
|
| 358 |
$wpdb->update( |
| 359 |
$this->sales_reps_table_name, |
| 360 |
$query_args, |
| 361 |
array( 'Sales_Rep_ID' => $sales_rep->id ) |
| 362 |
); |
| 363 |
|
| 364 |
$custom_fields = $ewd_otp_controller->settings->get_sales_rep_custom_fields(); |
| 365 |
|
| 366 |
foreach ( $custom_fields as $custom_field ) { |
| 367 |
|
| 368 |
$wpdb->get_var( $wpdb->prepare( "SELECT Meta_Value from $this->meta_table_name WHERE Field_ID=%d AND Sales_Rep_ID=%d ORDER BY Meta_ID DESC", $custom_field->id, $sales_rep->id ) ); |
| 369 |
|
| 370 |
$update = $wpdb->num_rows ? true : false; |
| 371 |
|
| 372 |
if ( empty( $sales_rep->custom_fields[ $custom_field->id ] ) ) { |
| 373 |
|
| 374 |
$where_args = array( |
| 375 |
'Field_ID' => $custom_field->id, |
| 376 |
'Sales_Rep_ID' => $sales_rep->id, |
| 377 |
); |
| 378 |
|
| 379 |
$wpdb->delete( |
| 380 |
$this->meta_table_name, |
| 381 |
$where_args |
| 382 |
); |
| 383 |
} |
| 384 |
elseif ( $update ) { |
| 385 |
|
| 386 |
$query_args = array( |
| 387 |
'Meta_Value' => $sales_rep->custom_fields[ $custom_field->id ] |
| 388 |
); |
| 389 |
|
| 390 |
$where_args = array( |
| 391 |
'Field_ID' => $custom_field->id, |
| 392 |
'Sales_Rep_ID' => $sales_rep->id, |
| 393 |
); |
| 394 |
|
| 395 |
$wpdb->update( |
| 396 |
$this->meta_table_name, |
| 397 |
$query_args, |
| 398 |
$where_args |
| 399 |
); |
| 400 |
} |
| 401 |
else { |
| 402 |
|
| 403 |
$query_args = array( |
| 404 |
'Meta_Value' => $sales_rep->custom_fields[ $custom_field->id ], |
| 405 |
'Field_ID' => $custom_field->id, |
| 406 |
'Sales_Rep_ID' => $sales_rep->id, |
| 407 |
); |
| 408 |
|
| 409 |
$wpdb->insert( |
| 410 |
$this->meta_table_name, |
| 411 |
$query_args |
| 412 |
); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
return $sales_rep->id; |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Accepts a sales rep id, deletes the corresponding sales rep |
| 421 |
* @since 3.0.0 |
| 422 |
*/ |
| 423 |
public function delete_sales_rep( $sales_rep_id ) { |
| 424 |
global $wpdb; |
| 425 |
|
| 426 |
$wpdb->delete( |
| 427 |
$this->sales_reps_table_name, |
| 428 |
array( 'Sales_Rep_ID' => $sales_rep_id ) |
| 429 |
); |
| 430 |
|
| 431 |
$wpdb->delete( |
| 432 |
$this->meta_table_name, |
| 433 |
array( 'Sales_Rep_ID' => $sales_rep_id ) |
| 434 |
); |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Accepts an email and sales rep id, and verify that they match the saved data, |
| 439 |
* @since 3.0.0 |
| 440 |
*/ |
| 441 |
public function verify_sales_rep_email( $email, $sales_rep_id ) { |
| 442 |
global $wpdb; |
| 443 |
|
| 444 |
$sales_rep_email = $wpdb->get_var( $wpdb->prepare( "SELECT Sales_Rep_Email FROM $this->sales_reps_table_name WHERE Sales_Rep_ID=%d", $sales_rep_id ) ); |
| 445 |
|
| 446 |
if ( $sales_rep_email == $email ) { |
| 447 |
|
| 448 |
return true; |
| 449 |
} |
| 450 |
|
| 451 |
return false; |
| 452 |
} |
| 453 |
} |
| 454 |
} |