public function res_admin_insert_out_of_work_callback() { // Check if user is logged in or die Resource_Booking_Ajax_Common::check_if_user_logged_in_or_die(); // Check if user is a labmanager or die Resource_Booking_Ajax_Common::check_if_labmanager_or_die(); // If here, user is labmanager (hopefully) $resource_id = isset($_POST['resource_id']) ? $_POST['resource_id'] : 'none'; $type = isset($_POST['type']) ? $_POST['type'] : 'none'; $reason = isset($_POST['reason']) ? intval($_POST['reason']) : 0; $start = isset($_POST['start']) ? $_POST['start'] : null; $end = isset($_POST['end']) ? $_POST['end'] : null; Resource_Booking_Ajax_Common::check_if_valid_date_with_time_or_die($start); Resource_Booking_Ajax_Common::check_if_valid_date_with_time_or_die($end); if ('none' == $resource_id && 'none' == $type) { // And die wp_send_json_error(array("message" => "Select at least a resource or a type")); } if ('none' != $resource_id && 'none' != $type) { // And die wp_send_json_error(array("message" => "Select only a resource or a type")); } $reason_string = ""; switch ($reason) { case 0: break; case 1: $reason_string = "Clean room closed"; break; case 2: $reason_string = "Resource out of work"; break; case 3: $reason_string = "Holiday"; break; default: wp_send_json_error(array("message" => "Wrong reason")); } $resources = array(); if ('none' != $resource_id) { // Resource selected if ('all' == $resource_id) { //Check if resource exists $resourcesA = $this->rb_db->get_resources_list(); foreach ($resourcesA as $resource) { $resource_id = $resource->resource_id; $resource_info = Resource_Booking_Ajax_Common::get_resource_info_or_die($this->rb_db, $resource_id, "array"); $resources[] = $resource_info; } } else { $resource_id = intval($resource_id, 10); $resource_info = Resource_Booking_Ajax_Common::get_resource_info_or_die($this->rb_db, $resource_id, "array"); $resources[] = $resource_info; } } else { // Type selected $resourcesA = $this->rb_db->get_resources_list($type); foreach ($resourcesA as $resource) { $resource_id = $resource->resource_id; $resource_info = Resource_Booking_Ajax_Common::get_resource_info_or_die($this->rb_db, $resource_id, "array"); $resources[] = $resource_info; } } foreach ($resources as $resource) { $resource_id = $resource->resourceInfo["resource_id"]; $bookingsA = $this->rb_db->list_bookings_by_resource_id_start_end($resource_id, $start, $end); foreach ($bookingsA as $booking) { $booking_id = $booking->id; $this->rb_db->delete_booking($booking_id, $resource_id, null); } if ($reason != 0) { $booking = $this->rb_db->insert_booking($resource_id, 1, $reason_string, $start, $end, $reason_string, $reason); } } $response = new StdClass(); $response->success = true; echo json_encode($response); wp_die(); }
/** * Check date time for insert update delete * * @param $start Date with time string in this format: Y-m-d*H:i:sP i.e. 2015-06-22T21:30:00+02:00 * @param $end Date with time string in this format: Y-m-d*H:i:sP i.e. 2015-06-22T21:30:00+02:00 * @param $resource */ public static function check_if_valid_start_end_i_u_d_or_die($start, $end, $resource) { // Check if start date is valid $start_parsed = Resource_Booking_Ajax_Common::check_if_valid_date_with_time_or_die($start); // Check if end date is valid $end_parsed = Resource_Booking_Ajax_Common::check_if_valid_date_with_time_or_die($end); // Check if start < end Resource_Booking_Ajax_Common::check_if_start_time_before_end_or_die($start_parsed, $end_parsed); // Check if start > now (and because of before, end > now) Resource_Booking_Ajax_Common::check_if_start_time_after_now_or_die($start_parsed); // Perform various tests on the booking interval Resource_Booking_Ajax_Common::check_if_valid_booking_for_resource_or_die($start_parsed, $end_parsed, $resource); }