| 1 |
<?php |
| 2 |
|
| 3 |
namespace Bookit\Classes\Admin; |
| 4 |
|
| 5 |
use Bookit\Classes\Database\Appointments; |
| 6 |
use Bookit\Classes\Database\Services; |
| 7 |
use Bookit\Classes\Database\Categories; |
| 8 |
use Bookit\Classes\Template; |
| 9 |
use Bookit\Helpers\CleanHelper; |
| 10 |
|
| 11 |
class ServicesController extends DashboardController { |
| 12 |
|
| 13 |
private static function getCleanRules() { |
| 14 |
return array( |
| 15 |
'id' => array( 'type' => 'intval' ), |
| 16 |
'limit' => array( 'type' => 'intval' ), |
| 17 |
'offset' => array( 'type' => 'intval' ), |
| 18 |
'price' => array( |
| 19 |
'function' => array( |
| 20 |
'custom' => true, |
| 21 |
'name' => 'custom_sanitize_price', |
| 22 |
), |
| 23 |
), |
| 24 |
'category_id' => array( 'type' => 'intval' ), |
| 25 |
'title' => array( 'type' => 'strval' ), |
| 26 |
); |
| 27 |
} |
| 28 |
/** |
| 29 |
* Display Rendered Template |
| 30 |
* @return bool|string |
| 31 |
*/ |
| 32 |
public static function render() { |
| 33 |
$services = Services::get_all(); |
| 34 |
$categories = Categories::get_all(); |
| 35 |
|
| 36 |
if ( ! empty( $services ) ) { |
| 37 |
foreach ( $services as &$service ) { |
| 38 |
$service['unset']['media_url'] = ( ! empty( $service['icon_id'] ) ) ? wp_get_attachment_url( $service['icon_id'] ) : null; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
self::enqueue_styles_scripts(); |
| 43 |
wp_enqueue_media(); |
| 44 |
|
| 45 |
return Template::load_template( |
| 46 |
'dashboard/bookit-services', |
| 47 |
array( |
| 48 |
'services' => $services, |
| 49 |
'categories' => $categories, |
| 50 |
), |
| 51 |
true |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get Services with Pagination |
| 57 |
*/ |
| 58 |
public static function get_services() { |
| 59 |
|
| 60 |
$data = CleanHelper::cleanData( $_GET, self::getCleanRules() ); |
| 61 |
if ( ! empty( $data['limit'] ) ) { |
| 62 |
$response['services'] = Services::get_paged( $data['limit'], $data['offset'] ); |
| 63 |
$response['categories'] = Categories::get_all(); |
| 64 |
$response['total'] = Services::get_count(); |
| 65 |
|
| 66 |
wp_send_json_success( $response ); |
| 67 |
} |
| 68 |
|
| 69 |
wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) ); |
| 70 |
} |
| 71 |
|
| 72 |
/** Get Service Assosiated data by id **/ |
| 73 |
public static function get_assosiated_total_data_by_id() { |
| 74 |
check_ajax_referer( 'bookit_get_service_assosiated_total_data', 'nonce' ); |
| 75 |
|
| 76 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
$data = CleanHelper::cleanData( $_POST, self::getCleanRules() ); |
| 81 |
|
| 82 |
if ( empty( $data['id'] ) ) { |
| 83 |
wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) ); |
| 84 |
} |
| 85 |
|
| 86 |
$staff = Services::get_service_total_staff( $data['id'] ); |
| 87 |
$appointments = Appointments::get_total_active_assosiated_appointments( $data['id'] ); |
| 88 |
|
| 89 |
$response = array( |
| 90 |
'total' => array( |
| 91 |
'staff' => $staff, |
| 92 |
'appointments' => $appointments, |
| 93 |
), |
| 94 |
); |
| 95 |
wp_send_json_success( $response ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Validate post fields |
| 100 |
*/ |
| 101 |
public static function validate( $data ) { |
| 102 |
$errors = array(); |
| 103 |
|
| 104 |
if ( null == $data['price'] || $data['price'] && preg_match( '/^\d+(\.\d{2})?$/', $data['price'] ) == '0' ) { |
| 105 |
$errors['price'] = __( 'Price must be a number', 'bookit' ); |
| 106 |
} |
| 107 |
|
| 108 |
if ( ! $data['category_id'] ) { |
| 109 |
$errors['category_id'] = __( 'Category is required', 'bookit' ); |
| 110 |
} |
| 111 |
|
| 112 |
if ( $data['title'] ) { |
| 113 |
$data['title'] = preg_replace( '/\s\s+/', ' ', $data['title'] ); |
| 114 |
if ( strlen( $data['title'] ) < 3 ) { |
| 115 |
$errors['title'] = __( 'Title must be greater than 3 characters', 'bookit' ); |
| 116 |
} |
| 117 |
} else { |
| 118 |
$errors['title'] = __( 'Title is required', 'bookit' ); |
| 119 |
} |
| 120 |
|
| 121 |
if ( count( $errors ) > 0 ) { |
| 122 |
wp_send_json_error( |
| 123 |
array( |
| 124 |
'errors' => $errors, |
| 125 |
'message' => __( 'Error occurred!', 'bookit' ), |
| 126 |
) |
| 127 |
); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Save Service |
| 133 |
*/ |
| 134 |
public static function save() { |
| 135 |
check_ajax_referer( 'bookit_save_item', 'nonce' ); |
| 136 |
|
| 137 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
$data = CleanHelper::cleanData( $_POST, self::getCleanRules() ); |
| 142 |
self::validate( $data ); |
| 143 |
|
| 144 |
if ( ! empty( $data ) ) { |
| 145 |
if ( ! empty( $data['id'] ) ) { |
| 146 |
Services::update( $data, array( 'id' => $data['id'] ) ); |
| 147 |
} else { |
| 148 |
Services::insert( $data ); |
| 149 |
$data['id'] = Services::insert_id(); |
| 150 |
} |
| 151 |
|
| 152 |
do_action( 'bookit_service_saved', $data['id'] ); |
| 153 |
|
| 154 |
wp_send_json_success( |
| 155 |
array( |
| 156 |
'id' => $data['id'], |
| 157 |
'message' => __( 'Service Saved!', 'bookit' ), |
| 158 |
) |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Delete the Service |
| 167 |
*/ |
| 168 |
public static function delete() { |
| 169 |
check_ajax_referer( 'bookit_delete_item', 'nonce' ); |
| 170 |
|
| 171 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
$data = CleanHelper::cleanData( $_GET, self::getCleanRules() ); |
| 176 |
|
| 177 |
if ( isset( $data['id'] ) ) { |
| 178 |
$id = $data['id']; |
| 179 |
|
| 180 |
Services::deleteService( $id ); |
| 181 |
do_action( 'bookit_service_deleted', $id ); |
| 182 |
|
| 183 |
wp_send_json_success( |
| 184 |
array( 'message' => __( 'Service Deleted!', 'bookit' ) ) |
| 185 |
); |
| 186 |
} |
| 187 |
wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) ); |
| 188 |
} |
| 189 |
} |
| 190 |
|