Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validations for Hotel address when add a new hotel #1070

Merged
merged 9 commits into from
Aug 22, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public function processSave()
}
}

if (!$phone) {
if (!$phone = trim($phone)) {
$this->errors[] = $this->l('Phone number is required field.');
} elseif (!Validate::isPhoneNumber($phone)) {
$this->errors[] = $this->l('Please enter a valid phone number.');
Expand All @@ -300,7 +300,7 @@ public function processSave()
$this->errors[] = $this->l('Rating is required field.');
}

if ($address == '') {
if (!$address = trim($address)) {
$this->errors[] = $this->l('Address is required field.');
}

Expand Down Expand Up @@ -329,7 +329,39 @@ public function processSave()
if ($city == '') {
$this->errors[] = $this->l('City is required field.');
} elseif (!Validate::isCityName($city)) {
$this->errors[] = $this->l('Enter a Valid City Name.');
$this->errors[] = $this->l('Enter a valid city name.');
}

//Since the address for the hotel is saved in the address table. We are validating the hotel address here manually.
$addressValidation = Address::getValidationRules('Address');
foreach ($addressValidation['size'] as $field => $maxSize) {
if ('phone' == $field && Tools::strlen($phone) > $maxSize) {
$this->errors[] = sprintf(
Tools::displayError('The Hotel phone number is too long (%1$d chars max).'),
$maxSize
);
} else if ('address1' == $field && Tools::strlen($address) > $maxSize) {
$this->errors[] = sprintf(
Tools::displayError('The Hotel address is too long (%1$d chars max).'),
$maxSize
);
} else if ('city' == $field && Tools::strlen($city) > $maxSize) {
$this->errors[] = sprintf(
Tools::displayError('The Hotel city name is too long (%1$d chars max).'),
$maxSize
);
} else if ('postcode' == $field && Tools::strlen($zipcode) > $maxSize) {
$this->errors[] = sprintf(
Tools::displayError('The Hotel zip code is too long (%1$d chars max).'),
$maxSize
);
} else if (($value = Tools::getValue($field)) && Tools::strlen($value) > $maxSize) {
$this->errors[] = sprintf(
Tools::displayError('The Hotel %1$s field is too long (%2$d chars max).'),
$field,
$maxSize
);
}
}

if ($idHotel) {
Expand Down Expand Up @@ -381,13 +413,13 @@ public function processSave()
$linkRewriteArray = array();
foreach ($languages as $lang) {
if (!trim(Tools::getValue('hotel_name_'.$lang['id_lang']))) {
$objHotelBranch->hotel_name[$lang['id_lang']] = Tools::getValue(
$objHotelBranch->hotel_name[$lang['id_lang']] = trim(Tools::getValue(
'hotel_name_'.$defaultLangId
);
));
} else {
$objHotelBranch->hotel_name[$lang['id_lang']] = Tools::getValue(
$objHotelBranch->hotel_name[$lang['id_lang']] = trim(Tools::getValue(
'hotel_name_'.$lang['id_lang']
);
));
}

if (!trim(Tools::getValue('link_rewrite_'.$lang['id_lang']))) {
Expand Down Expand Up @@ -476,19 +508,32 @@ public function processSave()
} else {
$objAddress = new Address();
}

$objAddress->id_hotel = $newIdHotel;
$objAddress->id_country = $country;
$objAddress->id_state = $state;
$objAddress->city = $city;
$objAddress->postcode = $zipcode;
$hotelName = $objHotelBranch->hotel_name[$defaultLangId];
$objAddress->alias = $hotelName;
$hotelName = preg_replace('/[0-9!<>,;?=+()@#"°{}_$%:]*$/u', '', $hotelName);
$objAddress->lastname = $hotelName;
$objAddress->firstname = $hotelName;
$hotelName = trim(preg_replace('/[0-9!<>,;?=+()@#"°{}_$%:]*$/u', '', $hotelName));
$objAddress->alias = trim(substr($hotelName, 0, 32));
$addressFirstName = $hotelName;
$addressLastName = $hotelName;
// If hotel name is length is greater than 32 then we split it into two
if (Tools::strlen($hotelName) > 32) {
// Slicing and removing the extra spaces after slicing
$addressFirstName = trim(substr($hotelName, 0, 32));
// To remove the excess space from last name
if (!$addressLastName = trim(substr($hotelName, 32, 32))) {
// since the last name can also be an empty space we will then use first name as last name
$addressLastName = $addressFirstName;
}
}

$objAddress->firstname = $addressFirstName;
$objAddress->lastname = $addressLastName;
$objAddress->address1 = $address;
$objAddress->phone = $phone;

$objAddress->save();

// Save refund rules of the hotels
Expand Down Expand Up @@ -537,6 +582,7 @@ public function processSave()
if ($objHotelBranch->id_category) {
$objCategory = new Category($objHotelBranch->id_category);
$objCategory->name = $objHotelBranch->hotel_name;
$objCategory->link_rewrite = $linkRewriteArray;
$objCategory->id_parent = $catCity;
$objCategory->save();
Category::regenerateEntireNtree();
Expand Down