function check_validate() { $data = array(); $order_item_id = STInput::request('order_item_id', ''); $st_first_name = STInput::request('st_first_name', ''); if (empty($st_first_name)) { STAdmin::set_message(__('The firstname field is not empty.', ST_TEXTDOMAIN), 'danger'); return false; } $st_last_name = STInput::request('st_last_name', ''); if (empty($st_last_name)) { STAdmin::set_message(__('The lastname field is not empty.', ST_TEXTDOMAIN), 'danger'); return false; } $st_email = STInput::request('st_email', ''); if (empty($st_email)) { STAdmin::set_message(__('The email field is not empty.', ST_TEXTDOMAIN), 'danger'); return false; } $st_phone = STInput::request('st_phone', ''); if (empty($st_phone)) { STAdmin::set_message(__('The phone field is not empty.', ST_TEXTDOMAIN), 'danger'); return false; } if (STInput::request('section', '') != 'edit_order_item') { $item_id = intval(STInput::request('item_id', '')); if ($item_id <= 0 || get_post_type($item_id) != 'st_cars') { STAdmin::set_message(__('The car field is not empty.', ST_TEXTDOMAIN), 'danger'); return false; } $pick_up = STInput::request('pick_up', ''); $drop_off = STInput::request('drop_off', ''); if (!empty($pick_up) && !empty($drop_off)) { $pickup_country = get_post_meta($pick_up, 'location_country', true); if (!$pickup_country) { STAdmin::set_message(__('The \'country\' field not set for the \'\'', ST_TEXTDOMAIN) . get_the_title($pick_up), 'danger'); $pass_validate = false; return false; } $dropoff_country = get_post_meta($drop_off, 'location_country', true); if (!$dropoff_country) { STAdmin::set_message(__('The \'country\' field not set for the \'\'', ST_TEXTDOMAIN) . get_the_title($drop_off), 'danger'); $pass_validate = false; return false; } if ($pickup_country != $dropoff_country) { STAdmin::set_message(__('The country is not same', ST_TEXTDOMAIN), 'danger'); $pass_validate = false; return false; } } $check_in = STInput::request('check_in', ''); if (empty($check_in)) { STAdmin::set_message(__('The check in field is not empty.', ST_TEXTDOMAIN), 'danger'); return false; } $check_in_time = STInput::request('check_in_time', ''); if (empty($check_in_time)) { STAdmin::set_message(__('The check in time field is not empty.', ST_TEXTDOMAIN), 'danger'); return false; } $check_out = STInput::request('check_out', ''); if (empty($check_out)) { STAdmin::set_message(__('The check out field is not empty.', ST_TEXTDOMAIN), 'danger'); return false; } $check_out_time = STInput::request('check_out_time', ''); if (empty($check_out_time)) { STAdmin::set_message(__('The check out time field is not empty.', ST_TEXTDOMAIN), 'danger'); return false; } $check_in_full = $check_in . ' ' . strtoupper($check_in_time); $check_out_full = $check_out . ' ' . strtoupper($check_out_time); $check_in_timestamp = strtotime($check_in_full); $check_out_timestamp = strtotime($check_out_full); $today = date('m/d/Y'); $period = TravelHelper::dateDiff($today, $check_in); $compare = TravelHelper::dateCompare($today, $check_out); $booking_period = intval(get_post_meta($item_id, 'cars_booking_period', true)); if ($booking_period <= 0) { $booking_period = 0; } if ($check_in_timestamp - $check_out_timestamp >= 0) { STAdmin::set_message(__('The drop off datetime is later than the pick up datetime.', ST_TEXTDOMAIN), 'danger'); $pass_validate = false; return false; } if ($compare < 0) { STAdmin::set_message(__('You can not set check-in date in the past', ST_TEXTDOMAIN), 'danger'); $pass_validate = false; return false; } if ($period < $booking_period) { STAdmin::set_message(sprintf(__('This car allow minimum booking is %d day(s)', ST_TEXTDOMAIN), $booking_period), 'danger'); $pass_validate = false; return false; } if (!CarHelper::_get_car_cant_order_by_id($item_id, $check_in_timestamp, $check_out_timestamp, $order_item_id)) { STAdmin::set_message(__('This car is full order', ST_TEXTDOMAIN), 'danger'); $pass_validate = false; return false; } $data = array('order_item_id' => $order_item_id, 'item_id' => $item_id, 'location_id_pick_up' => $pick_up, 'location_id_drop_off' => $drop_off, 'check_in' => date('Y-m-d', strtotime($check_in)), 'check_out' => date('Y-m-d', strtotime($check_out)), 'check_in_timestamp' => $check_in_timestamp, 'check_out_timestamp' => $check_out_timestamp, 'st_booking_post_type' => 'st_cars', 'st_booking_id' => $item_id, 'check_in_time' => $check_in_time, 'check_out_time' => $check_out_time); } return $data; }
$list = "''"; if (is_array($result) && count($result)) { $list = array(); foreach ($result as $key => $val) { $list[] = $val['st_booking_id']; } $list = implode(',', $list); } return $list; } static function _get_car_cant_order_by_id($car_id, $check_in, $check_out, $order_item_id = '') { if (!TravelHelper::checkTableDuplicate('st_cars')) { return true; } global $wpdb; $string = ""; if (!empty($order_item_id)) { $string = " AND order_item_id NOT IN ('{$order_item_id}') "; } $sql = "SELECT\r\n\t\t\tst_booking_id as car_id,\r\n\t\t\tmt.meta_value as number_car,\r\n\t\t\tmt.meta_value - COUNT(st_booking_id) as car_free\r\n\t\t\tFROM {$wpdb->prefix}st_order_item_meta\r\n\t\t\tINNER JOIN {$wpdb->prefix}postmeta as mt ON mt.post_id = st_booking_id AND mt.meta_key = 'number_car'\r\n\t\t\tWHERE\r\n\t\t\tst_booking_post_type = 'st_cars'\r\n\t\t\tAND status NOT IN ('trash', 'canceled')\r\n\t\t\tAND st_booking_id = '{$car_id}'\r\n\t\t\tAND (\r\n\t\t\t\t({$check_in} < check_in_timestamp AND {$check_out} > check_out_timestamp)\r\n\t\t\t\tOR(\r\n\t\t\t\t{$check_in} BETWEEN check_in_timestamp AND check_out_timestamp\r\n\t\t\t)\r\n\t\t\t\tOR(\r\n\t\t\t\t{$check_out} BETWEEN check_in_timestamp AND check_out_timestamp\r\n\t\t\t)\r\n\t\t\t)\r\n\t\t\t{$string}\r\n\t\t\tGROUP BY st_booking_id\r\n\t\t\tHAVING (mt.meta_value - COUNT(st_booking_id) <= 0)"; $result = $wpdb->get_results($sql, ARRAY_A); if (is_array($result) && count($result)) { return false; } return true; } } $carhelper = new CarHelper(); $carhelper->init(); }
function _get_where_query($where) { if (!TravelHelper::checkTableDuplicate('st_cars')) { return $where; } global $wpdb; if (!empty($_REQUEST['location_id_pick_up']) or !empty($_REQUEST['location_id'])) { $location_id = intval(STInput::get('location_id_pick_up', '')); if (empty($location_id)) { $location_id = STInput::request('location_id'); } $list = TravelHelper::getLocationByParent($location_id); if (is_array($list) && count($list)) { $where .= " AND ("; $where_tmp = ""; foreach ($list as $item) { if (empty($where_tmp)) { $where_tmp .= "tb.multi_location LIKE '%_{$item}_%'"; } else { $where_tmp .= " OR tb.multi_location LIKE '%_{$item}_%'"; } } $list = implode(',', $list); $where_tmp .= " OR tb.id_location IN ({$list})"; $where .= $where_tmp . ")"; } else { $where .= " AND (tb.multi_location LIKE '%_{$location_id}_%' OR tb.id_location IN ('{$location_id}')) "; } } elseif (!empty($_REQUEST['pick-up']) or !empty($_REQUEST['location_name'])) { $location_name = STInput::request('pick-up', ''); if (empty($location_name)) { $location_name = STInput::request('location_name'); } $ids_location = TravelerObject::_get_location_by_name($location_name); if (is_array($ids_location) && count($ids_location)) { $ids_location_tmp = array(); foreach ($ids_location as $item) { $list = TravelHelper::getLocationByParent($item); if (is_array($list) && count($list)) { foreach ($list as $item) { $ids_location_tmp[] = $item; } } } if (count($ids_location_tmp)) { $ids_location = $ids_location_tmp; } } if (!empty($ids_location)) { $where .= " AND (("; $where_tmp = ""; foreach ($ids_location as $item) { if (empty($where_tmp)) { $where_tmp .= "tb.multi_location LIKE '%_{$item}_%'"; } else { $where_tmp .= " OR tb.multi_location LIKE '%_{$item}_%'"; } } $ids_location = implode(',', $ids_location); $where_tmp .= " OR tb.id_location IN ({$ids_location})"; $where .= $where_tmp . ")"; $where .= " OR (tb.cars_address LIKE '%{$location_name}%'"; $where .= " OR {$wpdb->prefix}posts.post_title LIKE '%{$location_name}%'))"; } else { if (!empty($_REQUEST['search_all'])) { $where .= " AND (tb.cars_address LIKE '%{$location_name}%'"; $where .= " OR {$wpdb->prefix}posts.post_title LIKE '%{$location_name}%')"; } } } if (isset($_REQUEST['price_range'])) { $price = STInput::get('price_range', '0;0'); $priceobj = explode(';', $price); $priceobj[0] = TravelHelper::convert_money_to_default($priceobj[0]); $priceobj[1] = TravelHelper::convert_money_to_default($priceobj[1]); $where .= " AND (tb.sale_price >= {$priceobj[0]})"; if (isset($priceobj[1])) { $priceobj[1] = TravelHelper::convert_money_to_default($priceobj[1]); $where .= " AND (tb.sale_price <= {$priceobj[1]})"; } } if (isset($_REQUEST['pick-up-date']) && isset($_REQUEST['drop-off-date']) && !empty($_REQUEST['pick-up-date']) && !empty($_REQUEST['drop-off-date'])) { $pick_up_date = TravelHelper::convertDateFormat(STInput::request('pick-up-date')); $drop_off_date = TravelHelper::convertDateFormat(STInput::request('drop-off-date')); $pick_up_time = ""; $drop_off_time = ""; if (isset($_REQUEST['pick-up-time']) && !empty($_REQUEST['pick-up-time'])) { $pick_up_time = STInput::request('pick-up-time', '12:00 PM'); } if (isset($_REQUEST['drop-off-time']) && !empty($_REQUEST['drop-off-time'])) { $drop_off_time = STInput::request('drop-off-time', '12:00 PM'); } $check_in = $pick_up_date . ' ' . $pick_up_time; $check_in = strtotime($check_in); $check_out = $drop_off_date . ' ' . $drop_off_time; $check_out = strtotime($check_out); $list_date = CarHelper::_get_car_cant_order($check_in, $check_out); $where .= " AND ({$wpdb->posts}.ID NOT IN ({$list_date}))"; $today = date('Y-m-d'); $check_in = date('Y-m-d', $check_in); $period = TravelHelper::dateDiff($today, $check_in); $where .= " AND (CAST(tb.cars_booking_period AS UNSIGNED) <= {$period})"; } if (isset($_REQUEST['item_id']) and !empty($_REQUEST['item_id'])) { $item_id = STInput::request('item_id', ''); $where .= " AND ({$wpdb->prefix}posts.ID = '{$item_id}')"; } if (isset($_REQUEST['location_id'])) { $table = $wpdb->prefix . 'st_cars'; $id = STInput::request('location_id', ''); $where .= " AND (\r\n tb.multi_location LIKE '%_{$id}_%'\r\n or tb.id_location = '{$id}'\r\n )"; } if (isset($_REQUEST['range']) and isset($_REQUEST['location_id_pick_up'])) { $range = STInput::request('range', '5'); $location_id = STInput::request('location_id_pick_up'); $post_type = get_query_var('post_type'); $map_lat = (double) get_post_meta($location_id, 'map_lat', true); $map_lng = (double) get_post_meta($location_id, 'map_lng', true); global $wpdb; $where .= "\r\n AND {$wpdb->posts}.ID IN (\r\n SELECT ID FROM (\r\n SELECT {$wpdb->posts}.*,( 6371 * acos( cos( radians({$map_lat}) ) * cos( radians( mt1.meta_value ) ) *\r\n cos( radians( mt2.meta_value ) - radians({$map_lng}) ) + sin( radians({$map_lat}) ) *\r\n sin( radians( mt1.meta_value ) ) ) ) AS distance\r\n FROM {$wpdb->posts}, {$wpdb->postmeta} as mt1,{$wpdb->postmeta} as mt2\r\n WHERE {$wpdb->posts}.ID = mt1.post_id\r\n and {$wpdb->posts}.ID=mt2.post_id\r\n AND mt1.meta_key = 'map_lat'\r\n and mt2.meta_key = 'map_lng'\r\n AND {$wpdb->posts}.post_status = 'publish'\r\n AND {$wpdb->posts}.post_type = '{$post_type}'\r\n AND {$wpdb->posts}.post_date < NOW()\r\n GROUP BY {$wpdb->posts}.ID HAVING distance<{$range}\r\n ORDER BY distance ASC\r\n ) as st_data\r\n\t )"; } return $where; }