From 68e84e19a4be269a44fc75bef15e9b83e1242aa9 Mon Sep 17 00:00:00 2001 From: Sumit Panwar Date: Sat, 30 Sep 2023 17:31:19 +0530 Subject: [PATCH 1/7] Fixed: Issues in getting(GET), creating(POST) and updating(PUT) hotel with apis --- .../WebserviceSpecificManagementImages.php | 1 + .../classes/HotelBranchInformation.php | 358 ++++++++++++++---- 2 files changed, 287 insertions(+), 72 deletions(-) diff --git a/classes/webservice/WebserviceSpecificManagementImages.php b/classes/webservice/WebserviceSpecificManagementImages.php index f69e2c88b..a72e615ec 100644 --- a/classes/webservice/WebserviceSpecificManagementImages.php +++ b/classes/webservice/WebserviceSpecificManagementImages.php @@ -521,6 +521,7 @@ protected function manageListDeclinatedImages($directory, $normal_image_sizes) $ids[] = $image['id_hotel']; } $ids = array_unique($ids, SORT_NUMERIC); + asort($ids); foreach ($ids as $id) { $this->output .= $this->objOutput->getObjectRender()->renderNodeHeader('image', array(), array('id' => $id, 'xlink_resource'=>$this->wsObject->wsUrl.'images/'.'hotels'.'/'.$id), false); } diff --git a/modules/hotelreservationsystem/classes/HotelBranchInformation.php b/modules/hotelreservationsystem/classes/HotelBranchInformation.php index 39e805901..d7043adb8 100644 --- a/modules/hotelreservationsystem/classes/HotelBranchInformation.php +++ b/modules/hotelreservationsystem/classes/HotelBranchInformation.php @@ -44,10 +44,10 @@ class HotelBranchInformation extends ObjectModel 'multilang' => true, 'fields' => array( 'id_category' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), - 'email' => array('type' => self::TYPE_STRING,'validate' => 'isEmail', 'size' => 255), - 'rating' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), - 'check_in' => array('type' => self::TYPE_STRING), - 'check_out' => array('type' => self::TYPE_STRING), + 'email' => array('type' => self::TYPE_STRING,'validate' => 'isEmail', 'size' => 255, 'required' => true), + 'rating' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true), + 'check_in' => array('type' => self::TYPE_STRING, 'required' => true), + 'check_out' => array('type' => self::TYPE_STRING, 'required' => true), 'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'), 'latitude' => array('type' => self::TYPE_FLOAT), 'longitude' => array('type' => self::TYPE_FLOAT), @@ -56,13 +56,26 @@ class HotelBranchInformation extends ObjectModel 'active_refund' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'), 'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false), 'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false), + //lang fields 'policies' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'lang' => true), - 'hotel_name' => array('type' => self::TYPE_STRING, 'lang' => true), + 'hotel_name' => array('type' => self::TYPE_STRING, 'lang' => true, 'required' => true), 'description' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'lang' => true), 'short_description' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'lang' => true), ), ); + // Webservice fields + public $id_country; + public $id_state; + public $city; + public $zipcode; + public $address; + public $phone; + public $use_global_max_order_date; + public $max_order_date; + public $use_global_preparation_time; + public $preparation_time; + protected $webserviceParameters = array( 'objectsNodeName' => 'hotels', 'objectNodeName' => 'hotel', @@ -71,28 +84,40 @@ class HotelBranchInformation extends ObjectModel 'delete' => 'deleteWs', 'update' => 'updateWs', ), - 'fields' => array( + 'phone' => array(), + 'address' => array(), + 'id_country' => array( + 'required' => true, + 'xlink_resource'=> 'countries', + ), + 'id_state' => array( + 'xlink_resource'=> 'states', + ), + 'city' => array( + 'required' => true, + ), + 'zipcode' => array(), 'id_default_image' => array( 'getter' => 'getCoverWs', + 'setter' => 'setCoverWs', 'xlink_resource' => array( - 'resourceName' => 'hotel_images', + 'resourceName' => 'images', + 'subResourceName' => 'hotels' ) ), - 'max_order_date' => array( - 'getter' => 'getWsMaxOrderDate', - 'setter' => 'setWsMaxOrderDate', - ), + 'use_global_max_order_date' => array(), + 'max_order_date' => array(), + 'use_global_preparation_time' => array(), + 'preparation_time' => array(), ), - 'associations' => array( 'room_types' => array( 'setter' => false, 'resource' => 'room_types', 'fields' => array('id' => array()) ), - 'hotel_images' => array( - 'setter' => false, + 'images' => array( 'resource' => 'image', 'fields' => array('id' => array()) ), @@ -111,6 +136,25 @@ public function __construct($id = null, $id_lang = null, $id_shop = null) { $this->moduleInstance = Module::getInstanceByName('hotelreservationsystem'); parent::__construct($id, $id_lang, $id_shop); + + $this->use_global_max_order_date = $this->max_order_date = $this->use_global_preparation_time = $this->preparation_time = $this->id_country = $this->id_state = $this->city = $this->zipcode = $this->address = $this->phone = null; + if ($id) { + if ($hotelAddress = $this->getAddress($id)) { + $this->id_country = $hotelAddress['id_country']; + $this->id_state = $hotelAddress['id_state']; + $this->city = $hotelAddress['city']; + $this->zipcode = $hotelAddress['postcode']; + $this->address = $hotelAddress['address1']; + $this->phone = $hotelAddress['phone']; + } + + if ($hotelRestrictions = HotelOrderRestrictDate::getDataByHotelId($id)) { + $this->use_global_max_order_date = $hotelRestrictions['use_global_max_order_date']; + $this->max_order_date = $hotelRestrictions['max_order_date']; + $this->use_global_preparation_time = $hotelRestrictions['use_global_preparation_time']; + $this->preparation_time = $hotelRestrictions['preparation_time']; + } + } } public function add($autodate = true, $null_values = false) @@ -136,10 +180,10 @@ protected function initAccess($idHotel, Context $context = null) /* Query definition */ $query = 'REPLACE INTO `'._DB_PREFIX_.'htl_access` (`id_profile`, `id_hotel`, `access`)'; - $query .= ' VALUES '.'('.(int) _PS_ADMIN_PROFILE_.', '.(int)$idHotel.', 1)'; + $query .= ' VALUES '.'(1, '.(int)$idHotel.', 1)'; /* Profile selection */ - $profiles = Db::getInstance()->executeS('SELECT `id_profile` FROM '._DB_PREFIX_.'profile WHERE `id_profile` != '.(int) _PS_ADMIN_PROFILE_); - if ($profiles) { + $profiles = Db::getInstance()->executeS('SELECT `id_profile` FROM '._DB_PREFIX_.'profile WHERE `id_profile` != 1'); + if (!$profiles || empty($profiles)) { foreach ($profiles as $profile) { $access = 0; if (isset($context->employee->id_profile)) { @@ -800,7 +844,7 @@ public function getCoverWs() { if ($result = HotelImage::getCover($this->id)) { // we are sending id_hotel/id_image as per the url set for the hotel images - return $result['id_hotel'].'/'.$result['id']; + return $result['id']; } return false; } @@ -811,12 +855,14 @@ public function getCoverWs() */ public function setCoverWs($id_image) { - // first unset the cover - Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'htl_image` SET `cover` = NULL - WHERE `id_hotel` = '.(int)$this->id); + if ($id_image) { + // first unset the cover + Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'htl_image` SET `cover` = NULL + WHERE `id_hotel` = '.(int)$this->id); - // set the sent id of the image to the cover of the hotel - Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'htl_image` SET `cover` = 1 WHERE `id_image` = '.(int)$id_image); + // set the sent id of the image to the cover of the hotel + Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'htl_image` SET `cover` = 1 WHERE `id` = '.(int)$id_image.' AND `id_hotel` = '.(int)$this->id); + } return true; } @@ -828,7 +874,7 @@ public function getWsHotelImages() if ($hotelImages = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'htl_image` WHERE `id_hotel` = '.(int)$this->id)) { foreach ($hotelImages as $key => $image) { // we are sending id_hotel/id_image as per the url set for the hotel images - $ids[$key]['id'] = $image['id_hotel'].'/'.$image['id']; + $ids[$key]['id'] = $image['id']; } } return $ids; @@ -852,7 +898,7 @@ public function setWsHotelFeatures($branchFeatures) ); foreach ($branchFeatures as $feature) { - Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'htl_branch_features` (`id_hotel`, `feature_id`) VALUES ('.(int)$this->id.', '.(int)$feature['id'].')'); + Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'htl_branch_features` (`id_hotel`, `feature_id`, `date_add`, `date_upd`) VALUES ('.(int)$this->id.', '.(int)$feature['id'].', NOW(), NOW())'); } return true; @@ -875,7 +921,7 @@ public function setWsHotelRefundRules($refundRules) ); foreach ($refundRules as $rule) { - Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'htl_branch_refund_rules` (`id_hotel`, `id_refund_rule`) VALUES ('.(int)$this->id.', '.(int)$rule['id'].')'); + Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'htl_branch_refund_rules` (`id_hotel`, `id_refund_rule`, `date_add`, `date_upd`) VALUES ('.(int)$this->id.', '.(int)$rule['id'].', NOW(), NOW())'); } return true; @@ -889,49 +935,22 @@ public function getWsRoomTypes() ); } - // Webservice :: get max order date of the hotel - public function getWsMaxOrderDate() - { - return Db::getInstance()->getValue( - 'SELECT `max_order_date` FROM `'._DB_PREFIX_.'htl_order_restrict_date` WHERE `id_hotel` = '.(int)$this->id.' ORDER BY `id` ASC' - ); - } - - // Webservice :: set max order date of the hotel - public function setWsMaxOrderDate($maxOrderDate) - { - if ($this->id) { - // delete previous - Db::getInstance()->execute(' - DELETE FROM `'._DB_PREFIX_.'htl_order_restrict_date` - WHERE `id_hotel` = '.(int)$this->id - ); - - // set max_order_date for the hotel from request - return Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'htl_order_restrict_date` (`id_hotel`, `max_order_date`) VALUES ('.(int)$this->id.', \''.pSQL($maxOrderDate).'\')'); - } - } - // Webservice :: function will run when hotel added from API public function addWs($autodate = true, $null_values = false) { if ($this->add($autodate, $null_values)) { - // set categories of the hotel - $this->setWsHotelCategories(); + // Set hotel address + $this->setWsHotelAddress(); - $postData = trim(file_get_contents('php://input')); - libxml_use_internal_errors(true); - $xml = simplexml_load_string(utf8_decode($postData)); + // set hotel restrictions + $this->setWsHotelRestrictions(); - $hotelData = json_decode(json_encode($xml)); - if (isset($hotelData->hotel->max_order_date)) { - if ($maxOrderDate = $hotelData->hotel->max_order_date) { - $this->setWsMaxOrderDate($maxOrderDate); - } - } + // set categories of the hotel + $this->setWsHotelCategories(); return true; } + return false; } @@ -939,23 +958,217 @@ public function addWs($autodate = true, $null_values = false) public function updateWs($null_values = false) { if ($this->update($null_values)) { + // Set hotel address + $this->setWsHotelAddress(); + + // set hotel restrictions + $this->setWsHotelRestrictions(); + // set categories of the hotel $this->setWsHotelCategories(); - $postData = trim(file_get_contents('php://input')); - libxml_use_internal_errors(true); - $xml = simplexml_load_string(utf8_decode($postData)); + return true; + } + + return false; + } + + public function setWsHotelAddress() + { + if ($idHotelAddress = $this->getHotelIdAddress()) { + $objAddress = new Address($idHotelAddress); + } else { + $objAddress = new Address(); + } + + $hotelName = $this->hotel_name[Configuration::get('PS_LANG_DEFAULT')]; + $hotelName = preg_replace('/[0-9!<>,;?=+()@#"°{}_$%:]*$/u', '', $hotelName); + + $objAddress->id_hotel = $this->id; + $objAddress->id_country = $this->id_country; + $objAddress->id_state = $this->id_state; + $objAddress->city = $this->city; + $objAddress->postcode = $this->zipcode; + $objAddress->lastname = $hotelName; + $objAddress->firstname = $hotelName; + $objAddress->alias = $hotelName; + $objAddress->address1 = $this->address; + $objAddress->phone = $this->phone; + + return $objAddress->save(); + } + + public function setWsHotelRestrictions() + { + if ($this->id) { + // save maximum booking date and preparation time + if ($restrictDateInfo = HotelOrderRestrictDate::getDataByHotelId($this->id)) { + $objHotelRestrictDate = new HotelOrderRestrictDate($restrictDateInfo['id']); + } else { + $objHotelRestrictDate = new HotelOrderRestrictDate(); + } + + $objHotelRestrictDate->id_hotel = $this->id; + $objHotelRestrictDate->use_global_max_order_date = $this->use_global_max_order_date; + if (!$this->use_global_max_order_date) { + $objHotelRestrictDate->max_order_date = $this->max_order_date; + } + $objHotelRestrictDate->use_global_preparation_time = $this->use_global_preparation_time; + if (!$this->use_global_preparation_time) { + $objHotelRestrictDate->preparation_time = $this->preparation_time; + } + + return $objHotelRestrictDate->save(); + } + } - $hotelData = json_decode(json_encode($xml)); - if (isset($hotelData->hotel->max_order_date)) { - if ($maxOrderDate = $hotelData->hotel->max_order_date) { - $this->setWsMaxOrderDate($maxOrderDate); + public function validateFields($die = true, $error_return = false) + { + // set additional hotel address and restriction fields validations here + if (isset($this->webservice_validation) && $this->webservice_validation) { + if ($this->rating < 1 || $this->rating > 5) { + $message = Tools::displayError('Rating must be between 1 and 5.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } + if ($this->check_in && $this->check_out && strtotime($this->check_out) > strtotime($this->check_in)) { + $message = Tools::displayError('Check Out time must be before Check In time.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } + if (!$this->phone) { + $message = Tools::displayError('Phone number is required field.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } elseif (!Validate::isPhoneNumber($this->phone)) { + $message = Tools::displayError('Please enter a valid phone number.'); + if ($die) { + throw new PrestaShopException($message); } + return $error_return ? $message : false; + } + if ($this->address == '') { + $message = Tools::displayError('Address is required field.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; } - return true; + if (!$this->id_country) { + $message = Tools::displayError('Country is required field.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } else { + if (Validate::isLoadedObject($objCountry = new Country($this->id_country))) { + $statesbycountry = State::getStatesByIdCountry($this->id_country); + /*If selected country has states only the validate state field*/ + if ($this->id_state) { + $objState = new State($this->id_state); + if ($objState->id_country != $this->id_country) { + $message = Tools::displayError('State is invalid.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } + } else { + if ($statesbycountry) { + $message = Tools::displayError('State is required field.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } + } + /* Check zip code format */ + if ($objCountry->zip_code_format && !$objCountry->checkZipCode($this->zipcode)) { + + } elseif (empty($this->zipcode) && $objCountry->need_zip_code) { + $message = Tools::displayError('A Zip / Postal code is required.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } elseif ($this->zipcode && !Validate::isPostCode($this->zipcode)) { + $message = Tools::displayError('The Zip / Postal code is invalid.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } + } else { + $message = Tools::displayError('country is invalid.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } + } + + if ($this->city == '') { + $message = Tools::displayError('City is required field.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } elseif (!Validate::isCityName($this->city)) { + $message = Tools::displayError('Enter a Valid City Name.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } + + if (!$this->use_global_max_order_date) { + $maxOrderDateFormatted = date('Y-m-d', strtotime($this->max_order_date)); + if ($this->max_order_date == '') { + $message = Tools::displayError('Maximum date to book a room is required.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } elseif (!Validate::isDate($this->max_order_date)) { + $message = Tools::displayError('Maximum date to book a room is invalid.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } elseif (strtotime($maxOrderDateFormatted) < strtotime(date('Y-m-d'))) { + $message = Tools::displayError('Maximum Global Date to book a room can not be a past date. Please use a future date.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } + } + + if (!$this->use_global_preparation_time) { + if ($this->preparation_time === '') { + $message = Tools::displayError('Preparation time is a required.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } elseif ($this->preparation_time !== '0' && !Validate::isUnsignedInt($this->preparation_time)) { + $message = Tools::displayError('Preparation time is invalid.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } + } } - return false; + + return parent::validateFields($die, $error_return); } // Webservice :: function will run when hotel deleted from API @@ -983,18 +1196,19 @@ public function setWsHotelCategories() } } $objCountry = new Country(); - $countryName = $objCountry->getNameById($idLang, $this->country_id); + $countryName = $objCountry->getNameById($idLang, $this->id_country); if ($catCountry = $this->addCategory($countryName, false, $groupIds)) { if ($this->state_id) { $objState = new State(); - $stateName = $objState->getNameById($this->state_id); + $stateName = $objState->getNameById($this->id_state); + $catState = $this->addCategory($stateName, $catCountry, $groupIds); } else { $catState = $this->addCategory($this->city, $catCountry, $groupIds); } if ($catState) { if ($catCity = $this->addCategory($this->city, $catState, $groupIds)) { - $hotelCatName = $this->hotel_name; + $hotelCatName = $this->hotel_name[Configuration::get('PS_LANG_DEFAULT')]; if ($catHotel = $this->addCategory( $hotelCatName, $catCity, $groupIds, 1, $this->id )) { From 2c21795a099ac7de9847eeb6f1145a27ccc7af72 Mon Sep 17 00:00:00 2001 From: Sumit Panwar Date: Fri, 6 Oct 2023 11:13:17 +0530 Subject: [PATCH 2/7] In hotel api, id_category is added in hidden fields of webserviceParameters --- .../hotelreservationsystem/classes/HotelBranchInformation.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/hotelreservationsystem/classes/HotelBranchInformation.php b/modules/hotelreservationsystem/classes/HotelBranchInformation.php index d7043adb8..becda897c 100644 --- a/modules/hotelreservationsystem/classes/HotelBranchInformation.php +++ b/modules/hotelreservationsystem/classes/HotelBranchInformation.php @@ -111,6 +111,9 @@ class HotelBranchInformation extends ObjectModel 'use_global_preparation_time' => array(), 'preparation_time' => array(), ), + 'hidden_fields' => array ( + 'id_category', + ), 'associations' => array( 'room_types' => array( 'setter' => false, From 17363b100f000cc4277406ad39acc75f38639010 Mon Sep 17 00:00:00 2001 From: Sumit Panwar Date: Fri, 6 Oct 2023 17:46:51 +0530 Subject: [PATCH 3/7] Fixed:state_id is used in place of id_state while creating hotel categories --- .../hotelreservationsystem/classes/HotelBranchInformation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/hotelreservationsystem/classes/HotelBranchInformation.php b/modules/hotelreservationsystem/classes/HotelBranchInformation.php index becda897c..dbf0004c6 100644 --- a/modules/hotelreservationsystem/classes/HotelBranchInformation.php +++ b/modules/hotelreservationsystem/classes/HotelBranchInformation.php @@ -1201,7 +1201,7 @@ public function setWsHotelCategories() $objCountry = new Country(); $countryName = $objCountry->getNameById($idLang, $this->id_country); if ($catCountry = $this->addCategory($countryName, false, $groupIds)) { - if ($this->state_id) { + if ($this->id_state) { $objState = new State(); $stateName = $objState->getNameById($this->id_state); From 7195ed943ada89c98e93c9ca80e65f9f089a3b6a Mon Sep 17 00:00:00 2001 From: Sumit Panwar Date: Mon, 9 Oct 2023 15:29:04 +0530 Subject: [PATCH 4/7] Validations added to the hotel fields in the Hotel API --- .../classes/HotelBranchInformation.php | 50 +++++++++++++++++-- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/modules/hotelreservationsystem/classes/HotelBranchInformation.php b/modules/hotelreservationsystem/classes/HotelBranchInformation.php index dbf0004c6..151a738c1 100644 --- a/modules/hotelreservationsystem/classes/HotelBranchInformation.php +++ b/modules/hotelreservationsystem/classes/HotelBranchInformation.php @@ -49,8 +49,8 @@ class HotelBranchInformation extends ObjectModel 'check_in' => array('type' => self::TYPE_STRING, 'required' => true), 'check_out' => array('type' => self::TYPE_STRING, 'required' => true), 'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'), - 'latitude' => array('type' => self::TYPE_FLOAT), - 'longitude' => array('type' => self::TYPE_FLOAT), + 'latitude' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat'), + 'longitude' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat'), 'map_formated_address' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml'), 'map_input_text' => array('type' => self::TYPE_STRING, 'validate' => 'isString'), 'active_refund' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'), @@ -1036,7 +1036,23 @@ public function validateFields($die = true, $error_return = false) } return $error_return ? $message : false; } - if ($this->check_in && $this->check_out && strtotime($this->check_out) > strtotime($this->check_in)) { + + if (!strtotime($this->check_in)) { + $message = Tools::displayError('Check In time is invalid.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } + if (!strtotime($this->check_out)) { + $message = Tools::displayError('Check out time is invalid.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } + + if ($this->check_in && $this->check_out && strtotime($this->check_out) >= strtotime($this->check_in)) { $message = Tools::displayError('Check Out time must be before Check In time.'); if ($die) { throw new PrestaShopException($message); @@ -1062,6 +1078,12 @@ public function validateFields($die = true, $error_return = false) throw new PrestaShopException($message); } return $error_return ? $message : false; + } elseif (!Validate::isAddress($this->address)) { + $message = Tools::displayError('Address is invalid.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; } if (!$this->id_country) { @@ -1094,7 +1116,11 @@ public function validateFields($die = true, $error_return = false) } /* Check zip code format */ if ($objCountry->zip_code_format && !$objCountry->checkZipCode($this->zipcode)) { - + $message = sprintf(Tools::displayError('The Zip/Postal code you have entered is invalid. It must follow this format: %s'), str_replace('C', $objCountry->iso_code, str_replace('N', '0', str_replace('L', 'A', $objCountry->zip_code_format)))); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; } elseif (empty($this->zipcode) && $objCountry->need_zip_code) { $message = Tools::displayError('A Zip / Postal code is required.'); if ($die) { @@ -1131,6 +1157,22 @@ public function validateFields($die = true, $error_return = false) return $error_return ? $message : false; } + if (!Validate::isBool($this->use_global_max_order_date)) { + $message = Tools::displayError('invalid value for use_global_max_order_date.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } + + if (!Validate::isBool($this->use_global_preparation_time)) { + $message = Tools::displayError('invalid value for use_global_preparation_time.'); + if ($die) { + throw new PrestaShopException($message); + } + return $error_return ? $message : false; + } + if (!$this->use_global_max_order_date) { $maxOrderDateFormatted = date('Y-m-d', strtotime($this->max_order_date)); if ($this->max_order_date == '') { From 4db786d95d4ddeb46e3c9a99c44b2470325ef01f Mon Sep 17 00:00:00 2001 From: Sumit Panwar Date: Thu, 12 Oct 2023 11:36:35 +0530 Subject: [PATCH 5/7] hotel name validation added --- .../hotelreservationsystem/classes/HotelBranchInformation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/hotelreservationsystem/classes/HotelBranchInformation.php b/modules/hotelreservationsystem/classes/HotelBranchInformation.php index 151a738c1..278a33949 100644 --- a/modules/hotelreservationsystem/classes/HotelBranchInformation.php +++ b/modules/hotelreservationsystem/classes/HotelBranchInformation.php @@ -59,7 +59,7 @@ class HotelBranchInformation extends ObjectModel //lang fields 'policies' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'lang' => true), - 'hotel_name' => array('type' => self::TYPE_STRING, 'lang' => true, 'required' => true), + 'hotel_name' => array('type' => self::TYPE_STRING, 'lang' => true, 'required' => true, 'validate' => 'isCatalogName'), 'description' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'lang' => true), 'short_description' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'lang' => true), ), ); From 82dae8c9bfd6eed1bea37f1ee12792c2bb897f10 Mon Sep 17 00:00:00 2001 From: Sumit Panwar Date: Thu, 12 Oct 2023 15:38:18 +0530 Subject: [PATCH 6/7] Validations on api fields ar updated --- .../hotelreservationsystem/classes/HotelBranchInformation.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/hotelreservationsystem/classes/HotelBranchInformation.php b/modules/hotelreservationsystem/classes/HotelBranchInformation.php index 278a33949..45eccc623 100644 --- a/modules/hotelreservationsystem/classes/HotelBranchInformation.php +++ b/modules/hotelreservationsystem/classes/HotelBranchInformation.php @@ -59,7 +59,7 @@ class HotelBranchInformation extends ObjectModel //lang fields 'policies' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'lang' => true), - 'hotel_name' => array('type' => self::TYPE_STRING, 'lang' => true, 'required' => true, 'validate' => 'isCatalogName'), + 'hotel_name' => array('type' => self::TYPE_STRING, 'lang' => true, 'required' => true, 'validate' => 'isGenericName'), 'description' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'lang' => true), 'short_description' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'lang' => true), ), ); @@ -985,7 +985,7 @@ public function setWsHotelAddress() } $hotelName = $this->hotel_name[Configuration::get('PS_LANG_DEFAULT')]; - $hotelName = preg_replace('/[0-9!<>,;?=+()@#"°{}_$%:]*$/u', '', $hotelName); + $hotelName = preg_replace('/[0-9!<>,;"?=+()@#°{}_$%:]*/u', '', $hotelName); $objAddress->id_hotel = $this->id; $objAddress->id_country = $this->id_country; From b49020ee9a6632e156e8f151e95fc20fcaafc1af Mon Sep 17 00:00:00 2001 From: Sumit Panwar Date: Sun, 21 Jan 2024 10:58:10 +0530 Subject: [PATCH 7/7] Fixed:synopsis issues and image issue in Hotel API --- .../classes/HotelBranchInformation.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/hotelreservationsystem/classes/HotelBranchInformation.php b/modules/hotelreservationsystem/classes/HotelBranchInformation.php index 45eccc623..8a59f4c80 100644 --- a/modules/hotelreservationsystem/classes/HotelBranchInformation.php +++ b/modules/hotelreservationsystem/classes/HotelBranchInformation.php @@ -85,8 +85,8 @@ class HotelBranchInformation extends ObjectModel 'update' => 'updateWs', ), 'fields' => array( - 'phone' => array(), - 'address' => array(), + 'phone' => array('required' => true), + 'address' => array('required' => true), 'id_country' => array( 'required' => true, 'xlink_resource'=> 'countries', @@ -871,7 +871,7 @@ public function setCoverWs($id_image) } // Webservice:: function to prepare id parameter for hotel images in a hotel api - public function getWsHotelImages() + public function getWsImages() { $ids = array(); if ($hotelImages = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'htl_image` WHERE `id_hotel` = '.(int)$this->id)) { @@ -1115,14 +1115,14 @@ public function validateFields($die = true, $error_return = false) } } /* Check zip code format */ - if ($objCountry->zip_code_format && !$objCountry->checkZipCode($this->zipcode)) { - $message = sprintf(Tools::displayError('The Zip/Postal code you have entered is invalid. It must follow this format: %s'), str_replace('C', $objCountry->iso_code, str_replace('N', '0', str_replace('L', 'A', $objCountry->zip_code_format)))); + if (empty($this->zipcode) && $objCountry->need_zip_code) { + $message = Tools::displayError('A Zip / Postal code is required.'); if ($die) { throw new PrestaShopException($message); } return $error_return ? $message : false; - } elseif (empty($this->zipcode) && $objCountry->need_zip_code) { - $message = Tools::displayError('A Zip / Postal code is required.'); + } elseif ($objCountry->zip_code_format && !$objCountry->checkZipCode($this->zipcode)) { + $message = sprintf(Tools::displayError('The Zip/Postal code you have entered is invalid. It must follow this format: %s'), str_replace('C', $objCountry->iso_code, str_replace('N', '0', str_replace('L', 'A', $objCountry->zip_code_format)))); if ($die) { throw new PrestaShopException($message); }