From 25a8d80ae93a81ea27f4f95644b49d1563131de9 Mon Sep 17 00:00:00 2001 From: varun kewlani Date: Fri, 3 Jul 2026 21:40:55 +0530 Subject: [PATCH] Fixed code as per changes discussed Co-authored-by: Name --- classes/Product.php | 51 ++++++++ controllers/admin/AdminFeaturesController.php | 63 +--------- controllers/admin/AdminProductsController.php | 114 +----------------- controllers/admin/AdminSearchController.php | 8 +- .../classes/HotelAmenities.php | 81 ++++++------- .../classes/HotelBookingDetail.php | 2 +- .../classes/HotelBranchAmenities.php | 58 ++++++--- .../classes/HotelReservationSystemDb.php | 20 +-- .../classes/HotelRoomTypeAmenities.php | 77 ++++++++---- .../admin/AdminAddHotelController.php | 28 +---- .../admin/AdminHotelAmenitiesController.php | 2 +- .../hotel_amenities/helpers/view/view.tpl | 2 +- modules/qlocleaner/qlocleaner.php | 6 +- .../views/templates/hook/htlfilterblock.tpl | 2 +- 14 files changed, 217 insertions(+), 297 deletions(-) diff --git a/classes/Product.php b/classes/Product.php index 78ddbe631..805d6e372 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -6871,4 +6871,55 @@ class ProductCore extends ObjectModel return $price * (int)$quantity; } + + /** + * Prepare grouped core features and values for a room type, with selection state. + * + * @param int $idProduct + * @param int $idLang defaults to current context language + * @return array + */ + public static function getRoomTypeFeatureData($idProduct, $idLang = 0) + { + if (!$idLang) { + $idLang = Context::getContext()->language->id; + } + + $product = new self((int) $idProduct); + $features = Feature::getFeatures( + $idLang, + (Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP) + ); + $selectedFeatureValues = array(); + foreach ($product->getFeatures() as $productFeature) { + $selectedFeatureValues[(int) $productFeature['id_feature']][] = (int) $productFeature['id_feature_value']; + } + + foreach ($features as &$feature) { + $featureValues = FeatureValue::getFeatureValuesWithLang( + $idLang, + (int) $feature['id_feature'] + ); + $feature['value'] = (int) $feature['id_feature']; + $feature['input_name'] = 'feature_value_parents'; + $feature['children'] = array(); + $selectedValues = isset($selectedFeatureValues[(int) $feature['id_feature']]) + ? $selectedFeatureValues[(int) $feature['id_feature']] + : array(); + + foreach ($featureValues as $featureValue) { + $featureValue['name'] = $featureValue['value']; + $featureValue['value'] = (int) $featureValue['id_feature_value']; + $featureValue['input_name'] = 'feature_values['.(int) $feature['id_feature'].']'; + $featureValue['selected'] = in_array((int) $featureValue['id_feature_value'], $selectedValues); + $feature['children'][] = $featureValue; + } + + if ($feature['children'] && count($selectedValues) === count($feature['children'])) { + $feature['selected'] = true; + } + } + + return $features; + } } diff --git a/controllers/admin/AdminFeaturesController.php b/controllers/admin/AdminFeaturesController.php index 8d89d407a..ad2df2e4d 100644 --- a/controllers/admin/AdminFeaturesController.php +++ b/controllers/admin/AdminFeaturesController.php @@ -89,10 +89,6 @@ class AdminFeaturesControllerCore extends AdminController ) ); parent::__construct(); - - if (!Tools::getIsset('viewfeature')) { - $this->addRowAction('view'); - } } /** @@ -102,6 +98,7 @@ class AdminFeaturesControllerCore extends AdminController public function renderList() { $this->addRowAction('edit'); + $this->addRowAction('view'); $this->addRowAction('delete'); return parent::renderList(); @@ -127,31 +124,6 @@ class AdminFeaturesControllerCore extends AdminController $this->identifier = 'id_feature'; } - /** - * Return a translated field value with a safe fallback when the current language is missing. - * - * @param array $values - * @param int $idLang - * @param string $default - * - * @return string - */ - protected function getTranslatedFieldValue(array $values, $idLang, $default = '') - { - $idLang = (int) $idLang; - if (isset($values[$idLang]) && '' !== trim((string) $values[$idLang])) { - return $values[$idLang]; - } - - foreach ($values as $value) { - if ('' !== trim((string) $value)) { - return $value; - } - } - - return $default; - } - public function renderView() { if (($id = Tools::getValue('id_feature'))) { @@ -168,12 +140,8 @@ class AdminFeaturesControllerCore extends AdminController return; } - $this->feature_name = is_array($obj->name) ? $obj->name : array(); - $this->toolbar_title = $this->getTranslatedFieldValue( - $this->feature_name, - $this->context->employee->id_lang, - $this->l('Feature') - ); + $this->feature_name = $obj->name; + $this->toolbar_title = $this->feature_name[$this->context->employee->id_lang]; $this->fields_list = array( 'id_feature_value' => array( 'title' => $this->l('ID'), @@ -354,11 +322,7 @@ class AdminFeaturesControllerCore extends AdminController break; case 'view': - $bread_extended[] = $this->getTranslatedFieldValue( - is_array($this->feature_name) ? $this->feature_name : array(), - $this->context->employee->id_lang, - $this->l('Feature') - ); + $bread_extended[] = $this->feature_name[$this->context->employee->id_lang]; $this->addMetaTitle($bread_extended[count($bread_extended) - 1]); break; @@ -366,22 +330,11 @@ class AdminFeaturesControllerCore extends AdminController if (($idFeature_value = Tools::getValue('id_feature_value'))) { if (($id = Tools::getValue('id_feature'))) { if (Validate::isLoadedObject($obj = new Feature((int)$id))) { - $bread_extended[] = ''.$this->getTranslatedFieldValue( - is_array($obj->name) ? $obj->name : array(), - $this->context->employee->id_lang, - $this->l('Feature') - ).''; + $bread_extended[] = ''.$obj->name[$this->context->employee->id_lang].''; } if (Validate::isLoadedObject($obj = new FeatureValue((int)Tools::getValue('id_feature_value')))) { - $bread_extended[] = sprintf( - $this->l('Edit: %s'), - $this->getTranslatedFieldValue( - is_array($obj->value) ? $obj->value : array(), - $this->context->employee->id_lang, - $this->l('Value') - ) - ); + $bread_extended[] = sprintf($this->l('Edit: %s'), $obj->value[$this->context->employee->id_lang]); } } else { $bread_extended[] = $this->l('Edit Value'); @@ -639,10 +592,6 @@ class AdminFeaturesControllerCore extends AdminController */ public function processSave() { - if ('feature_value' === $this->table) { - return parent::processSave(); - } - if ($this->table == 'feature') { $idFeature = (int)Tools::getValue('id_feature'); // Adding last position to the feature if not exist diff --git a/controllers/admin/AdminProductsController.php b/controllers/admin/AdminProductsController.php index 24e77b1c1..fee91601b 100644 --- a/controllers/admin/AdminProductsController.php +++ b/controllers/admin/AdminProductsController.php @@ -1168,9 +1168,6 @@ class AdminProductsControllerCore extends AdminController if (!$idFeature) { continue; } - if (!is_array($selectedValues)) { - $selectedValues = array($selectedValues); - } foreach (array_unique(array_map('intval', $selectedValues)) as $idFeatureValue) { if ($idFeatureValue) { $product->addFeaturesToDB($idFeature, $idFeatureValue); @@ -1181,99 +1178,6 @@ class AdminProductsControllerCore extends AdminController return true; } - /** - * Prepare amenities tree data for room type form. - * - * @param int $idProduct - * - * @return array - */ - protected function getRoomTypeAmenitiesTree($idProduct) - { - $selectedAmenityRows = array(); - foreach (HotelRoomTypeAmenities::getAmenityIds($idProduct) as $selectedAmenityId) { - $selectedAmenityRows[] = array('amenity_id' => $selectedAmenityId); - } - - $objHotelAmenities = new HotelAmenities(); - $roomTypeAmenities = $objHotelAmenities->hotelBranchSelectedAmenitiesArray( - $selectedAmenityRows, - $this->context->language->id - ); - - if (!$roomTypeAmenities) { - return array(); - } - - foreach ($roomTypeAmenities as $idAmenityGroup => &$amenityGroup) { - $amenityGroup['value'] = (int) $idAmenityGroup; - $amenityGroup['input_name'] = 'room_type_amenity_parents'; - - $selectedAmenities = 0; - if (!empty($amenityGroup['children'])) { - foreach ($amenityGroup['children'] as &$amenity) { - $amenity['value'] = (int) $amenity['id']; - $amenity['input_name'] = 'room_type_amenities'; - if (!empty($amenity['selected'])) { - ++$selectedAmenities; - } - } - - if ($selectedAmenities === count($amenityGroup['children'])) { - $amenityGroup['selected'] = true; - } - } - } - - return $roomTypeAmenities; - } - - /** - * Prepare grouped core features and values for the room type feature tree. - * - * @param Product $product - * - * @return array - */ - protected function getRoomTypeFeatureTree(Product $product) - { - $features = Feature::getFeatures( - $this->context->language->id, - (Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP) - ); - $selectedFeatureValues = array(); - foreach ($product->getFeatures() as $productFeature) { - $selectedFeatureValues[(int) $productFeature['id_feature']][] = (int) $productFeature['id_feature_value']; - } - - foreach ($features as &$feature) { - $featureValues = FeatureValue::getFeatureValuesWithLang( - $this->context->language->id, - (int) $feature['id_feature'] - ); - $feature['value'] = (int) $feature['id_feature']; - $feature['input_name'] = 'feature_value_parents'; - $feature['children'] = array(); - $selectedValues = isset($selectedFeatureValues[(int) $feature['id_feature']]) - ? $selectedFeatureValues[(int) $feature['id_feature']] - : array(); - - foreach ($featureValues as $featureValue) { - $featureValue['name'] = $featureValue['value']; - $featureValue['value'] = (int) $featureValue['id_feature_value']; - $featureValue['input_name'] = 'feature_values['.(int) $feature['id_feature'].']'; - $featureValue['selected'] = in_array((int) $featureValue['id_feature_value'], $selectedValues); - $feature['children'][] = $featureValue; - } - - if ($feature['children'] && count($selectedValues) === count($feature['children'])) { - $feature['selected'] = true; - } - } - - return $features; - } - /** * This function is never called at the moment (specific prices cannot be edited) */ @@ -2040,13 +1944,7 @@ class AdminProductsControllerCore extends AdminController return false; } $selectedAmenities = Tools::getValue('room_type_amenities', array()); - if (!is_array($selectedAmenities)) { - $selectedAmenities = array($selectedAmenities); - } $featuredAmenities = Tools::getValue('featured_amenities', array()); - if (!is_array($featuredAmenities)) { - $featuredAmenities = array($featuredAmenities); - } if (!(new HotelRoomTypeAmenities())->saveRoomTypeAmenities((int)$this->object->id, $selectedAmenities, $featuredAmenities)) { $this->errors[] = Tools::displayError('An error occurred while saving room type amenities.'); return false; @@ -2300,13 +2198,7 @@ class AdminProductsControllerCore extends AdminController if ($object->update()) { if ($this->isTabSubmitted('Amenities')) { $selectedAmenities = Tools::getValue('room_type_amenities', array()); - if (!is_array($selectedAmenities)) { - $selectedAmenities = array($selectedAmenities); - } $featuredAmenities = Tools::getValue('featured_amenities', array()); - if (!is_array($featuredAmenities)) { - $featuredAmenities = array($featuredAmenities); - } if (!(new HotelRoomTypeAmenities())->saveRoomTypeAmenities((int)$object->id, $selectedAmenities, $featuredAmenities)) { $this->errors[] = Tools::displayError('An error occurred while saving room type amenities.'); return false; @@ -4973,7 +4865,7 @@ class AdminProductsControllerCore extends AdminController } else { if ($obj->id) { if ($this->product_exists_in_shop) { - $features = $this->getRoomTypeFeatureTree($obj); + $features = Product::getRoomTypeFeatureData($obj->id); if ($features) { $tree = new HelperTree('room-type-features-tree', $features); $tree->setShowCollapseExpandButton(true) @@ -5019,7 +4911,7 @@ class AdminProductsControllerCore extends AdminController if ($obj->id) { if ($this->product_exists_in_shop) { - $roomTypeAmenities = $this->getRoomTypeAmenitiesTree((int) $obj->id); + $roomTypeAmenities = HotelRoomTypeAmenities::getRoomTypeAmenitiesData((int) $obj->id); if ($roomTypeAmenities) { $tree = new HelperTree('room-type-amenities-tree', $roomTypeAmenities); $tree->setShowCollapseExpandButton(true) @@ -5029,7 +4921,7 @@ class AdminProductsControllerCore extends AdminController ->setUseSearch(true) ->setHeaderTemplate('tree_header_room.tpl'); $data->assign('room_type_amenities_tree', $tree->render()); - $data->assign('featured_amenity_ids', HotelRoomTypeAmenities::getAmenityIds((int)$obj->id, true)); + $data->assign('featured_amenity_ids', array_column(HotelRoomTypeAmenities::getAmenities((int)$obj->id, 0, true), 'id')); } } else { $this->displayWarning($this->l('You must save the room type in this shop before adding amenities.')); diff --git a/controllers/admin/AdminSearchController.php b/controllers/admin/AdminSearchController.php index 462c60230..c2ac5e28f 100644 --- a/controllers/admin/AdminSearchController.php +++ b/controllers/admin/AdminSearchController.php @@ -436,11 +436,11 @@ class AdminSearchControllerCore extends AdminController if ($hotelAmenities = $objHotelAmenities->searchByName($this->query, $this->context->language->id)) { $amenities = array(); foreach ($hotelAmenities as $key => $hotelAmenity) { - $amenities[$hotelAmenity['id_htl_amenity']]['name'] = $hotelAmenity['name']; + $amenities[$hotelAmenity['id_amenity']]['name'] = $hotelAmenity['name']; if ($hotelAmenity['id_parent']) { - $amenities[$hotelAmenity['id_htl_amenity']]['id'] = $hotelAmenity['id_parent']; + $amenities[$hotelAmenity['id_amenity']]['id'] = $hotelAmenity['id_parent']; } else { - $amenities[$hotelAmenity['id_htl_amenity']]['id'] = $hotelAmenity['id_htl_amenity']; + $amenities[$hotelAmenity['id_amenity']]['id'] = $hotelAmenity['id_amenity']; } } @@ -936,7 +936,7 @@ class AdminSearchControllerCore extends AdminController $helper = new HelperList(); $helper->shopLinkType = ''; $helper->simple_header = true; - $helper->identifier = 'id_htl_amenity'; + $helper->identifier = 'id_amenity'; $helper->actions = array('edit', 'view'); $helper->show_toolbar = false; $helper->table = 'htl_amenity'; diff --git a/modules/hotelreservationsystem/classes/HotelAmenities.php b/modules/hotelreservationsystem/classes/HotelAmenities.php index 203fd0432..9fdf20d52 100644 --- a/modules/hotelreservationsystem/classes/HotelAmenities.php +++ b/modules/hotelreservationsystem/classes/HotelAmenities.php @@ -35,7 +35,7 @@ class HotelAmenities extends ObjectModel public static $definition = array( 'table' => 'htl_amenity', - 'primary' => 'id_htl_amenity', + 'primary' => 'id_amenity', 'multilang' => true, 'fields' => array( 'id_parent' => array('type' => self::TYPE_INT, 'required' => true), @@ -94,37 +94,37 @@ class HotelAmenities extends ObjectModel $result = array(); $parents = Db::getInstance()->executeS( - 'SELECT ha.`id_htl_amenity`, ha.`position`, hal.`name` + 'SELECT ha.`id_amenity`, ha.`position`, hal.`name` FROM `'._DB_PREFIX_.'htl_amenity` ha LEFT JOIN `'._DB_PREFIX_.'htl_amenity_lang` hal - ON (hal.`id_htl_amenity` = ha.`id_htl_amenity` AND hal.`id_lang` = '.(int)$idLang.') + ON (hal.`id_amenity` = ha.`id_amenity` AND hal.`id_lang` = '.(int)$idLang.') WHERE ha.`id_parent` = 0 ORDER BY ha.`position` ASC' ); if ($parents) { foreach ($parents as $parent) { - $result[$parent['id_htl_amenity']] = array( - 'id' => $parent['id_htl_amenity'], + $result[$parent['id_amenity']] = array( + 'id' => $parent['id_amenity'], 'name' => $parent['name'], 'position' => $parent['position'], 'children' => array(), ); $children = Db::getInstance()->executeS( - 'SELECT ha.`id_htl_amenity`, ha.`active`, ha.`logo_type`, ha.`logo`, hal.`name` + 'SELECT ha.`id_amenity`, ha.`active`, ha.`logo_type`, ha.`logo`, hal.`name` FROM `'._DB_PREFIX_.'htl_amenity` ha LEFT JOIN `'._DB_PREFIX_.'htl_amenity_lang` hal - ON (hal.`id_htl_amenity` = ha.`id_htl_amenity` AND hal.`id_lang` = '.(int)$idLang.') - WHERE ha.`id_parent` = '.(int)$parent['id_htl_amenity'] + ON (hal.`id_amenity` = ha.`id_amenity` AND hal.`id_lang` = '.(int)$idLang.') + WHERE ha.`id_parent` = '.(int)$parent['id_amenity'] ); if ($children) { - // normalize: rename id_htl_amenity → id for template compatibility + // normalize: rename id_amenity → id for template compatibility foreach ($children as &$child) { - $child['id'] = $child['id_htl_amenity']; + $child['id'] = $child['id_amenity']; } - $result[$parent['id_htl_amenity']]['children'] = $children; + $result[$parent['id_amenity']]['children'] = $children; } } } @@ -135,11 +135,11 @@ class HotelAmenities extends ObjectModel /** * Returns all amenity categories with children, marking which are selected for a hotel/room type. * - * @param array $branchAmenities rows from htl_branch_amenity for a hotel + * @param array $selectedIds flat array of selected amenity IDs * @param int $idLang * @return array|false */ - public function hotelBranchSelectedAmenitiesArray($branchAmenities, $idLang = 0) + public function hotelBranchSelectedAmenitiesArray($selectedIds, $idLang = 0) { if (!$idLang) { $idLang = Context::getContext()->language->id; @@ -147,38 +147,29 @@ class HotelAmenities extends ObjectModel $result = array(); $parents = Db::getInstance()->executeS( - 'SELECT ha.`id_htl_amenity`, hal.`name` + 'SELECT ha.`id_amenity`, hal.`name` FROM `'._DB_PREFIX_.'htl_amenity` ha LEFT JOIN `'._DB_PREFIX_.'htl_amenity_lang` hal - ON (hal.`id_htl_amenity` = ha.`id_htl_amenity` AND hal.`id_lang` = '.(int)$idLang.') + ON (hal.`id_amenity` = ha.`id_amenity` AND hal.`id_lang` = '.(int)$idLang.') WHERE ha.`id_parent` = 0' ); if ($parents) { foreach ($parents as $parent) { - $result[$parent['id_htl_amenity']]['name'] = $parent['name']; + $result[$parent['id_amenity']]['name'] = $parent['name']; $children = Db::getInstance()->executeS( - 'SELECT ha.`id_htl_amenity`, hal.`name` + 'SELECT ha.`id_amenity`, hal.`name` FROM `'._DB_PREFIX_.'htl_amenity` ha LEFT JOIN `'._DB_PREFIX_.'htl_amenity_lang` hal - ON (hal.`id_htl_amenity` = ha.`id_htl_amenity` AND hal.`id_lang` = '.(int)$idLang.') - WHERE ha.`id_parent` = '.(int)$parent['id_htl_amenity'] + ON (hal.`id_amenity` = ha.`id_amenity` AND hal.`id_lang` = '.(int)$idLang.') + WHERE ha.`id_parent` = '.(int)$parent['id_amenity'] ); if ($children) { foreach ($children as $child) { - $selected = 0; - if ($branchAmenities) { - foreach ($branchAmenities as $row) { - if ($child['id_htl_amenity'] == $row['amenity_id']) { - $selected = 1; - break; - } - } - } - $child['id'] = $child['id_htl_amenity']; - $child['selected'] = $selected; - $result[$parent['id_htl_amenity']]['children'][] = $child; + $child['id'] = $child['id_amenity']; + $child['selected'] = in_array($child['id_amenity'], $selectedIds) ? 1 : 0; + $result[$parent['id_amenity']]['children'][] = $child; } } } @@ -198,11 +189,11 @@ class HotelAmenities extends ObjectModel $idLang = Context::getContext()->language->id; } return Db::getInstance()->getRow( - 'SELECT ha.`id_htl_amenity`, hal.`name` + 'SELECT ha.`id_amenity`, hal.`name` FROM `'._DB_PREFIX_.'htl_amenity` ha LEFT JOIN `'._DB_PREFIX_.'htl_amenity_lang` hal - ON (hal.`id_htl_amenity` = ha.`id_htl_amenity` AND hal.`id_lang` = '.(int)$idLang.') - WHERE ha.`id_htl_amenity` = '.(int)$id + ON (hal.`id_amenity` = ha.`id_amenity` AND hal.`id_lang` = '.(int)$idLang.') + WHERE ha.`id_amenity` = '.(int)$id ); } @@ -213,7 +204,7 @@ class HotelAmenities extends ObjectModel public function getChildAmenitiesByParentAmenityId($parentId) { return Db::getInstance()->executeS( - 'SELECT `id_htl_amenity` FROM `'._DB_PREFIX_.'htl_amenity` WHERE `id_parent` = '.(int)$parentId + 'SELECT `id_amenity` FROM `'._DB_PREFIX_.'htl_amenity` WHERE `id_parent` = '.(int)$parentId ); } @@ -226,13 +217,13 @@ class HotelAmenities extends ObjectModel public function deleteCategory($idCategory) { $rows = Db::getInstance()->executeS( - 'SELECT `id_htl_amenity` FROM `'._DB_PREFIX_.'htl_amenity` - WHERE `id_parent` = '.(int)$idCategory.' OR `id_htl_amenity` = '.(int)$idCategory + 'SELECT `id_amenity` FROM `'._DB_PREFIX_.'htl_amenity` + WHERE `id_parent` = '.(int)$idCategory.' OR `id_amenity` = '.(int)$idCategory ); if ($rows) { foreach ($rows as $row) { - $objHotelAmenities = new HotelAmenities((int)$row['id_htl_amenity']); + $objHotelAmenities = new HotelAmenities((int)$row['id_amenity']); $objHotelAmenities->delete(); } } @@ -253,7 +244,7 @@ class HotelAmenities extends ObjectModel return Db::getInstance()->executeS( 'SELECT ha.*, hal.* FROM `'._DB_PREFIX_.'htl_amenity` ha - LEFT JOIN `'._DB_PREFIX_.'htl_amenity_lang` hal ON hal.`id_htl_amenity` = ha.`id_htl_amenity` + LEFT JOIN `'._DB_PREFIX_.'htl_amenity_lang` hal ON hal.`id_amenity` = ha.`id_amenity` WHERE hal.`name` LIKE \'%'.pSQL($query).'%\' AND hal.`id_lang` = '.(int)$idLang ); @@ -265,17 +256,17 @@ class HotelAmenities extends ObjectModel * @param int $idLang * @return array */ - public static function getCategories($idLang = 0): array + public static function getCategories($idLang = 0) { if (!$idLang) { $idLang = Context::getContext()->language->id; } return Db::getInstance()->executeS( - 'SELECT ha.`id_htl_amenity`, hal.`name` + 'SELECT ha.`id_amenity`, hal.`name` FROM `'._DB_PREFIX_.'htl_amenity` ha LEFT JOIN `'._DB_PREFIX_.'htl_amenity_lang` hal - ON (hal.`id_htl_amenity` = ha.`id_htl_amenity` AND hal.`id_lang` = '.(int)$idLang.') + ON (hal.`id_amenity` = ha.`id_amenity` AND hal.`id_lang` = '.(int)$idLang.') WHERE ha.`id_parent` = 0 ORDER BY ha.`position` ASC' ) ?: array(); @@ -287,17 +278,17 @@ class HotelAmenities extends ObjectModel * @param int $idLang * @return array */ - public static function getAmenities($idLang = 0): array + public static function getAmenities($idLang = 0) { if (!$idLang) { $idLang = Context::getContext()->language->id; } return Db::getInstance()->executeS( - 'SELECT ha.`id_htl_amenity`, hal.`name` + 'SELECT ha.`id_amenity`, hal.`name` FROM `'._DB_PREFIX_.'htl_amenity` ha LEFT JOIN `'._DB_PREFIX_.'htl_amenity_lang` hal - ON (hal.`id_htl_amenity` = ha.`id_htl_amenity` AND hal.`id_lang` = '.(int)$idLang.') + ON (hal.`id_amenity` = ha.`id_amenity` AND hal.`id_lang` = '.(int)$idLang.') WHERE ha.`id_parent` > 0 AND ha.`active` = 1 ORDER BY hal.`name` ASC' diff --git a/modules/hotelreservationsystem/classes/HotelBookingDetail.php b/modules/hotelreservationsystem/classes/HotelBookingDetail.php index f2111c446..d81761f40 100644 --- a/modules/hotelreservationsystem/classes/HotelBookingDetail.php +++ b/modules/hotelreservationsystem/classes/HotelBookingDetail.php @@ -1707,7 +1707,7 @@ class HotelBookingDetail extends ObjectModel true ); if (!empty($amenities) && $amenities) { - $roomAmenityIds = HotelRoomTypeAmenities::getAmenityIds($value['id_product']); + $roomAmenityIds = array_column(HotelRoomTypeAmenities::getAmenities($value['id_product']), 'id'); if (array_diff($amenities, $roomAmenityIds)) { unset($bookingData['rm_data'][$key]); continue; diff --git a/modules/hotelreservationsystem/classes/HotelBranchAmenities.php b/modules/hotelreservationsystem/classes/HotelBranchAmenities.php index ea938b6bb..62627a26d 100644 --- a/modules/hotelreservationsystem/classes/HotelBranchAmenities.php +++ b/modules/hotelreservationsystem/classes/HotelBranchAmenities.php @@ -32,7 +32,7 @@ class HotelBranchAmenities extends ObjectModel public static $definition = array( 'table' => 'htl_branch_amenity', - 'primary' => 'id_htl_branch_amenity', + 'primary' => 'id_branch_amenity', 'fields' => array( 'id_hotel' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), 'amenity_id' => array('type' => self::TYPE_INT), @@ -48,7 +48,7 @@ class HotelBranchAmenities extends ObjectModel * @param bool $featuredOnly true = is_featured=1 only; false = all active child amenities * @return array */ - public static function getAmenities($idHotel, $idLang = 0, $featuredOnly = false): array + public static function getAmenities($idHotel, $idLang = 0, $featuredOnly = false) { if (!$idLang) { $idLang = Context::getContext()->language->id; @@ -57,12 +57,12 @@ class HotelBranchAmenities extends ObjectModel $filter = $featuredOnly ? 'AND hba.`is_featured` = 1' : 'AND ha.`id_parent` > 0'; return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS( - 'SELECT ha.`id_htl_amenity` AS `id`, ha.`logo_type`, ha.`logo`, hal.`name` + 'SELECT ha.`id_amenity` AS `id`, ha.`logo_type`, ha.`logo`, hal.`name` FROM `'._DB_PREFIX_.'htl_branch_amenity` hba INNER JOIN `'._DB_PREFIX_.'htl_amenity` ha - ON (ha.`id_htl_amenity` = hba.`amenity_id`) + ON (ha.`id_amenity` = hba.`amenity_id`) INNER JOIN `'._DB_PREFIX_.'htl_amenity_lang` hal - ON (hal.`id_htl_amenity` = hba.`amenity_id` AND hal.`id_lang` = '.(int)$idLang.') + ON (hal.`id_amenity` = hba.`amenity_id` AND hal.`id_lang` = '.(int)$idLang.') WHERE hba.`id_hotel` = '.(int)$idHotel.' AND ha.`active` = 1 '.$filter.' @@ -71,19 +71,49 @@ class HotelBranchAmenities extends ObjectModel } /** + * Prepare amenities tree data for a hotel form, with selection state. + * * @param int $idHotel - * @return array amenity IDs where is_featured = 1 + * @param int $idLang defaults to current context language + * @return array */ - public static function getFeaturedAmenityIds($idHotel): array + public static function getBranchAmenitiesData($idHotel, $idLang = 0) { - $rows = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS( - 'SELECT `amenity_id` - FROM `'._DB_PREFIX_.'htl_branch_amenity` - WHERE `id_hotel` = '.(int)$idHotel.' - AND `is_featured` = 1' + if (!$idLang) { + $idLang = Context::getContext()->language->id; + } + + $objHotelAmenities = new HotelAmenities(); + $amenities = $objHotelAmenities->hotelBranchSelectedAmenitiesArray( + array_column(self::getAmenities($idHotel), 'id'), + $idLang ); - return $rows ? array_map('intval', array_column($rows, 'amenity_id')) : array(); + if (!$amenities) { + return array(); + } + + foreach ($amenities as $idAmenity => &$amenity) { + $amenity['value'] = $idAmenity; + $amenity['input_name'] = 'id_amenity_parents'; + + $selectedChildAmenities = 0; + if (!empty($amenity['children'])) { + foreach ($amenity['children'] as &$childAmenity) { + $childAmenity['value'] = $childAmenity['id']; + $childAmenity['input_name'] = 'id_amenities'; + if (!empty($childAmenity['selected'])) { + $selectedChildAmenities++; + } + } + + if ($selectedChildAmenities === count($amenity['children'])) { + $amenity['selected'] = true; + } + } + } + + return $amenities; } /** @@ -94,7 +124,7 @@ class HotelBranchAmenities extends ObjectModel * @param array $featuredIds amenity IDs that should be marked is_featured=1 * @return bool */ - public function saveBranchAmenities($idHotel, array $amenityIds, array $featuredIds = array()): bool + public function saveBranchAmenities($idHotel, array $amenityIds, array $featuredIds = array()) { Db::getInstance()->delete('htl_branch_amenity', '`id_hotel` = '.(int)$idHotel); diff --git a/modules/hotelreservationsystem/classes/HotelReservationSystemDb.php b/modules/hotelreservationsystem/classes/HotelReservationSystemDb.php index 0f648dd1d..ef1d2aa3b 100644 --- a/modules/hotelreservationsystem/classes/HotelReservationSystemDb.php +++ b/modules/hotelreservationsystem/classes/HotelReservationSystemDb.php @@ -92,7 +92,7 @@ class HotelReservationSystemDb ) ENGINE="._MYSQL_ENGINE_." DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;", "CREATE TABLE IF NOT EXISTS `"._DB_PREFIX_."htl_amenity` ( - `id_htl_amenity` int(10) unsigned NOT NULL AUTO_INCREMENT, + `id_amenity` int(10) unsigned NOT NULL AUTO_INCREMENT, `id_parent` int(10) unsigned NOT NULL, `position` int(10) unsigned NOT NULL, `active` int(2) NOT NULL DEFAULT '0', @@ -100,39 +100,39 @@ class HotelReservationSystemDb `logo` varchar(255) NOT NULL DEFAULT '', `date_add` datetime NOT NULL, `date_upd` datetime NOT NULL, - PRIMARY KEY (`id_htl_amenity`) + PRIMARY KEY (`id_amenity`) ) ENGINE="._MYSQL_ENGINE_." DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;", "CREATE TABLE IF NOT EXISTS `"._DB_PREFIX_."htl_amenity_lang` ( - `id_htl_amenity` int(10) unsigned NOT NULL, + `id_amenity` int(10) unsigned NOT NULL, `id_lang` int(10) unsigned NOT NULL, `name` varchar(255) NOT NULL, - PRIMARY KEY (`id_htl_amenity`, `id_lang`) + PRIMARY KEY (`id_amenity`, `id_lang`) ) ENGINE="._MYSQL_ENGINE_." DEFAULT CHARSET=utf8;", "CREATE TABLE IF NOT EXISTS `"._DB_PREFIX_."htl_branch_amenity` ( - `id_htl_branch_amenity` int(10) unsigned NOT NULL AUTO_INCREMENT, + `id_branch_amenity` int(10) unsigned NOT NULL AUTO_INCREMENT, `id_hotel` int(10) unsigned NOT NULL, `amenity_id` int(10) unsigned NOT NULL, `is_featured` tinyint(1) NOT NULL DEFAULT '0', `date_add` datetime NOT NULL, `date_upd` datetime NOT NULL, - PRIMARY KEY (`id_htl_branch_amenity`), + PRIMARY KEY (`id_branch_amenity`), CONSTRAINT `fk_htl_branch_amenity_amenity` FOREIGN KEY (`amenity_id`) - REFERENCES `"._DB_PREFIX_."htl_amenity` (`id_htl_amenity`) ON DELETE CASCADE + REFERENCES `"._DB_PREFIX_."htl_amenity` (`id_amenity`) ON DELETE CASCADE ) ENGINE="._MYSQL_ENGINE_." DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;", "CREATE TABLE IF NOT EXISTS `"._DB_PREFIX_."htl_room_type_amenity` ( - `id_htl_room_type_amenity` int(10) unsigned NOT NULL AUTO_INCREMENT, + `id_room_type_amenity` int(10) unsigned NOT NULL AUTO_INCREMENT, `id_product` int(10) unsigned NOT NULL, `amenity_id` int(10) unsigned NOT NULL, `is_featured` tinyint(1) NOT NULL DEFAULT '0', `date_add` datetime NOT NULL, `date_upd` datetime NOT NULL, - PRIMARY KEY (`id_htl_room_type_amenity`), + PRIMARY KEY (`id_room_type_amenity`), KEY `id_product` (`id_product`), CONSTRAINT `fk_htl_room_type_amenity_amenity` FOREIGN KEY (`amenity_id`) - REFERENCES `"._DB_PREFIX_."htl_amenity` (`id_htl_amenity`) ON DELETE CASCADE + REFERENCES `"._DB_PREFIX_."htl_amenity` (`id_amenity`) ON DELETE CASCADE ) ENGINE="._MYSQL_ENGINE_." DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;", "CREATE TABLE IF NOT EXISTS `"._DB_PREFIX_."htl_cart_booking_data` ( diff --git a/modules/hotelreservationsystem/classes/HotelRoomTypeAmenities.php b/modules/hotelreservationsystem/classes/HotelRoomTypeAmenities.php index dbbc9002b..556c91fa0 100644 --- a/modules/hotelreservationsystem/classes/HotelRoomTypeAmenities.php +++ b/modules/hotelreservationsystem/classes/HotelRoomTypeAmenities.php @@ -32,7 +32,7 @@ class HotelRoomTypeAmenities extends ObjectModel public static $definition = array( 'table' => 'htl_room_type_amenity', - 'primary' => 'id_htl_room_type_amenity', + 'primary' => 'id_room_type_amenity', 'fields' => array( 'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), 'amenity_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), @@ -42,30 +42,13 @@ class HotelRoomTypeAmenities extends ObjectModel ), ); - /** - * @param int $idProduct - * @param bool $featuredOnly true = is_featured=1 only; false = all - * @return array - */ - public static function getAmenityIds($idProduct, bool $featuredOnly = false): array - { - $rows = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS( - 'SELECT `amenity_id` - FROM `'._DB_PREFIX_.'htl_room_type_amenity` - WHERE `id_product` = '.(int)$idProduct. - ($featuredOnly ? ' AND `is_featured` = 1' : '') - ); - - return $rows ? array_map('intval', array_column($rows, 'amenity_id')) : array(); - } - /** * @param int $idProduct * @param int $idLang defaults to current context language * @param bool $featuredOnly true = is_featured=1 only; false = all active child amenities * @return array */ - public static function getAmenities($idProduct, $idLang = 0, $featuredOnly = false): array + public static function getAmenities($idProduct, $idLang = 0, $featuredOnly = false) { if (!$idLang) { $idLang = Context::getContext()->language->id; @@ -74,12 +57,12 @@ class HotelRoomTypeAmenities extends ObjectModel $filter = $featuredOnly ? 'AND hrta.`is_featured` = 1' : 'AND ha.`id_parent` > 0'; return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS( - 'SELECT ha.`id_htl_amenity` AS `id`, ha.`logo_type`, ha.`logo`, hal.`name` + 'SELECT ha.`id_amenity` AS `id`, ha.`logo_type`, ha.`logo`, hal.`name`, hrta.`is_featured` FROM `'._DB_PREFIX_.'htl_room_type_amenity` hrta INNER JOIN `'._DB_PREFIX_.'htl_amenity` ha - ON (ha.`id_htl_amenity` = hrta.`amenity_id`) + ON (ha.`id_amenity` = hrta.`amenity_id`) INNER JOIN `'._DB_PREFIX_.'htl_amenity_lang` hal - ON (hal.`id_htl_amenity` = hrta.`amenity_id` AND hal.`id_lang` = '.(int)$idLang.') + ON (hal.`id_amenity` = hrta.`amenity_id` AND hal.`id_lang` = '.(int)$idLang.') WHERE hrta.`id_product` = '.(int)$idProduct.' AND ha.`active` = 1 '.$filter.' @@ -91,11 +74,57 @@ class HotelRoomTypeAmenities extends ObjectModel * @param int $idProduct * @return bool */ - public static function deleteByProduct($idProduct): bool + public static function deleteByProduct($idProduct) { return (bool)Db::getInstance()->delete('htl_room_type_amenity', '`id_product` = '.(int)$idProduct); } + /** + * Prepare amenities tree data for a room type form, with selection state. + * + * @param int $idProduct + * @param int $idLang defaults to current context language + * @return array + */ + public static function getRoomTypeAmenitiesData($idProduct, $idLang = 0) + { + if (!$idLang) { + $idLang = Context::getContext()->language->id; + } + + $objHotelAmenities = new HotelAmenities(); + $amenities = $objHotelAmenities->hotelBranchSelectedAmenitiesArray( + array_column(self::getAmenities($idProduct), 'id'), + $idLang + ); + + if (!$amenities) { + return array(); + } + + foreach ($amenities as $idAmenityGroup => &$amenityGroup) { + $amenityGroup['value'] = (int) $idAmenityGroup; + $amenityGroup['input_name'] = 'room_type_amenity_parents'; + + $selectedAmenities = 0; + if (!empty($amenityGroup['children'])) { + foreach ($amenityGroup['children'] as &$amenity) { + $amenity['value'] = (int) $amenity['id']; + $amenity['input_name'] = 'room_type_amenities'; + if (!empty($amenity['selected'])) { + ++$selectedAmenities; + } + } + + if ($selectedAmenities === count($amenityGroup['children'])) { + $amenityGroup['selected'] = true; + } + } + } + + return $amenities; + } + /** * Delete existing assignments then save new ones for a room type. * @@ -104,7 +133,7 @@ class HotelRoomTypeAmenities extends ObjectModel * @param array $featuredIds amenity IDs that should be marked is_featured=1 * @return bool */ - public function saveRoomTypeAmenities($idProduct, array $amenityIds, array $featuredIds = array()): bool + public function saveRoomTypeAmenities($idProduct, array $amenityIds, array $featuredIds = array()) { static::deleteByProduct($idProduct); diff --git a/modules/hotelreservationsystem/controllers/admin/AdminAddHotelController.php b/modules/hotelreservationsystem/controllers/admin/AdminAddHotelController.php index 5d58a80ee..03b61125f 100644 --- a/modules/hotelreservationsystem/controllers/admin/AdminAddHotelController.php +++ b/modules/hotelreservationsystem/controllers/admin/AdminAddHotelController.php @@ -176,37 +176,15 @@ class AdminAddHotelController extends ModuleAdminController } $smartyVars['order_restrict_date_info'] = HotelOrderRestrictDate::getDataByHotelId($idHotel); - $objHotelAmenities = new HotelAmenities(); - $hotelAmenities = $this->object->getAmenitiesOfHotelByHotelId($this->object->id); - if ($amenities = $objHotelAmenities->hotelBranchSelectedAmenitiesArray($hotelAmenities)) { - foreach ($amenities as $idAmenity => $amenity) { - $amenities[$idAmenity]['value'] = $idAmenity; - $amenities[$idAmenity]['input_name'] = 'id_amenity_parents'; - if (isset($amenity['children']) && $amenity['children']) { - $selectedChildAmenities = 0; - foreach ($amenity['children'] as $childKey => $childAmenity) { - $amenities[$idAmenity]['children'][$childKey]['value'] = $childAmenity['id']; - $amenities[$idAmenity]['children'][$childKey]['input_name'] = 'id_amenities'; - if (isset($childAmenity['selected']) && $childAmenity['selected']) { - $selectedChildAmenities++; - } - } - - if ($selectedChildAmenities == count($amenity['children'])) { - $amenities[$idAmenity]['selected'] = true; - } - } - } - + if ($amenities = HotelBranchAmenities::getBranchAmenitiesData((int)$idHotel)) { $tree = new HelperTree('hotel-amenities-tree', $amenities); $tree->setShowCollapseExpandButton(true) ->setUseCheckBox(true) ->setAutoSelectChildren(true) ->setUseBulkActions(true) ->setUseSearch(true); - $treeContent = $tree->render(); - $smartyVars['hotel_amenity_tree'] = $treeContent; - $smartyVars['hotel_featured_amenity_ids'] = HotelBranchAmenities::getFeaturedAmenityIds((int)$idHotel); + $smartyVars['hotel_amenity_tree'] = $tree->render(); + $smartyVars['hotel_featured_amenity_ids'] = array_column(HotelBranchAmenities::getAmenities((int)$idHotel, 0, true), 'id'); } $smartyVars['rewrite_url'] = []; diff --git a/modules/hotelreservationsystem/controllers/admin/AdminHotelAmenitiesController.php b/modules/hotelreservationsystem/controllers/admin/AdminHotelAmenitiesController.php index 9da7beb88..e75a4ab5f 100644 --- a/modules/hotelreservationsystem/controllers/admin/AdminHotelAmenitiesController.php +++ b/modules/hotelreservationsystem/controllers/admin/AdminHotelAmenitiesController.php @@ -31,7 +31,7 @@ class AdminHotelAmenitiesController extends ModuleAdminController $this->bootstrap = true; $this->table = 'htl_amenity'; $this->className = 'HotelAmenities'; - $this->identifier = 'id_htl_amenity'; + $this->identifier = 'id_amenity'; $this->toolbar_title = $this->l('Manage Amenities'); parent::__construct(); diff --git a/modules/hotelreservationsystem/views/templates/admin/hotel_amenities/helpers/view/view.tpl b/modules/hotelreservationsystem/views/templates/admin/hotel_amenities/helpers/view/view.tpl index 7c592001a..1a1585bef 100644 --- a/modules/hotelreservationsystem/views/templates/admin/hotel_amenities/helpers/view/view.tpl +++ b/modules/hotelreservationsystem/views/templates/admin/hotel_amenities/helpers/view/view.tpl @@ -80,7 +80,7 @@ {$amenity.name|escape:'htmlall':'UTF-8'} diff --git a/modules/qlocleaner/qlocleaner.php b/modules/qlocleaner/qlocleaner.php index 0ba31d734..c44060b28 100644 --- a/modules/qlocleaner/qlocleaner.php +++ b/modules/qlocleaner/qlocleaner.php @@ -691,7 +691,7 @@ class QloCleaner extends Module array('htl_branch_info_lang', 'id_lang', 'lang', 'id_lang'), array('htl_branch_info_lang', 'id', 'htl_branch_info', 'id'), array('htl_amenity_lang', 'id_lang', 'lang', 'id_lang'), - array('htl_amenity_lang', 'id_htl_amenity', 'htl_amenity', 'id_htl_amenity'), + array('htl_amenity_lang', 'id_amenity', 'htl_amenity', 'id_amenity'), array('htl_room_type_demand', 'id_global_demand', 'htl_room_type_global_demand', 'id_global_demand'), array('htl_room_type_demand', 'id_product', 'product', 'id_product'), array('htl_room_type_demand_price', 'id_product', 'product', 'id_product'), @@ -705,7 +705,7 @@ class QloCleaner extends Module array('htl_cart_booking_data', 'id_order', 'orders', 'id_order'), array('htl_cart_booking_data', 'id_customer', 'customer', 'id_customer'), array('htl_branch_amenity', 'id_hotel', 'htl_branch_info', 'id'), - array('htl_branch_amenity', 'amenity_id', 'htl_amenity', 'id_htl_amenity'), + array('htl_branch_amenity', 'amenity_id', 'htl_amenity', 'id_amenity'), array('htl_image', 'id_hotel', 'htl_branch_info', 'id'), array('htl_branch_info', 'id_category', 'category', 'id_category'), array('htl_room_information', 'id_product', 'product', 'id_product'), @@ -729,7 +729,7 @@ class QloCleaner extends Module array('htl_room_type_service_product_price', 'id_product', 'product', 'id_product'), array('htl_room_type_service_product_price', 'id_element', 'product', 'id_product'), array('htl_room_type_amenity', 'id_product', 'product', 'id_product'), - array('htl_room_type_amenity', 'amenity_id', 'htl_amenity', 'id_htl_amenity'), + array('htl_room_type_amenity', 'amenity_id', 'htl_amenity', 'id_amenity'), ) ); diff --git a/modules/wkhotelfilterblock/views/templates/hook/htlfilterblock.tpl b/modules/wkhotelfilterblock/views/templates/hook/htlfilterblock.tpl index 71e6f5d50..7f72fdca6 100644 --- a/modules/wkhotelfilterblock/views/templates/hook/htlfilterblock.tpl +++ b/modules/wkhotelfilterblock/views/templates/hook/htlfilterblock.tpl @@ -38,7 +38,7 @@
{foreach $all_amenities as $amenity}
- + {$amenity.name}
{/foreach}