Fixex: Refactor category initialization and validation checks

Co-authored-by: Name <sumit201@webkul.in
This commit is contained in:
amit sharma
2026-07-21 16:52:46 +05:30
parent c82764dca8
commit 3f8b7bea2f
2 changed files with 40 additions and 31 deletions

View File

@@ -92,43 +92,45 @@ class CategoryControllerCore extends FrontController
public function init()
{
// Get category ID
$id_category = (int)Tools::getValue('id_category');
if (!$id_category || !Validate::isUnsignedId($id_category)) {
$this->errors[] = Tools::displayError('Missing category ID');
}
$idCategory = (int) Tools::getValue('id_category');
$this->category = new Category($idCategory, $this->context->language->id);
// validate dates if available
$dateFrom = Tools::getValue('date_from');
$dateTo = Tools::getValue('date_to');
$idHotel = HotelBranchInformation::getHotelIdByIdCategory($id_category);
// If this category belongs to a hotel but that hotel is disabled, redirect to home page
if ($idHotel && !(new HotelBranchInformation())->hotelBranchInfoByCategoryId($id_category)) {
Tools::redirect($this->context->link->getPageLink('pagenotfound'));
}
if (!HotelHelper::validateDateRangeForHotel($dateFrom, $dateTo, $idHotel)) {
Tools::redirect($this->context->link->getPageLink('pagenotfound'));
}
$idHotel = HotelBranchInformation::getHotelIdByIdCategory($idCategory);
// Instantiate category
$this->category = new Category($id_category, $this->context->language->id);
parent::init();
// Check if the category is active and return 404 error if is disable.
if (!$this->category->active || !Validate::isLoadedObject($this->category) || !$this->category->inShop() || !$this->category->isAssociatedToShop() || in_array($this->category->id, array(Configuration::get('PS_HOME_CATEGORY'), Configuration::get('PS_ROOT_CATEGORY')))) {
header('HTTP/1.1 404 Not Found');
header('Status: 404 Not Found');
if (!$idCategory || !Validate::isUnsignedId($idCategory)) {
Tools::redirect($this->context->link->getPageLink('pagenotfound'));
} else
// Check if category can be accessible by current customer and return 403 if not
if (!$this->category->checkAccess($this->context->customer->id)) {
} elseif (!Validate::isLoadedObject($this->category) || !$this->category->active) {
Tools::redirect($this->context->link->getPageLink('pagenotfound'));
} elseif ($idHotel && !(new HotelBranchInformation())->hotelBranchInfoByCategoryId($idCategory)) {
Tools::redirect($this->context->link->getPageLink('pagenotfound'));
} elseif ($idHotel && !HotelHelper::validateDateRangeForHotel($dateFrom, $dateTo, $idHotel)) {
Tools::redirect($this->context->link->getPageLink('pagenotfound'));
} else {
parent::init();
if (
!$this->category->inShop()
|| !$this->category->isAssociatedToShop()
|| in_array(
$this->category->id,
array(
Configuration::get('PS_HOME_CATEGORY'),
Configuration::get('PS_ROOT_CATEGORY')
)
)
) {
header('HTTP/1.1 404 Not Found');
header('Status: 404 Not Found');
Tools::redirect($this->context->link->getPageLink('pagenotfound'));
} elseif (!$this->category->checkAccess($this->context->customer->id)) {
header('HTTP/1.1 403 Forbidden');
header('Status: 403 Forbidden');
$this->errors[] = Tools::displayError('You do not have access to this category.');
$this->customer_access = false;
}
}
}
/**
@@ -136,14 +138,15 @@ class CategoryControllerCore extends FrontController
*/
public function initContent()
{
if (!$this->customer_access) {
$this->display_column_left = false;
$this->display_column_right = false;
}
parent::initContent();
$this->setTemplate(_PS_THEME_DIR_.'category.tpl');
if (!$this->customer_access) {
return;
}
$id_category = Tools::getValue('id_category');
$displayAllRoomsTypes = false;

View File

@@ -24,6 +24,11 @@
*}
{block name='category'}
{block name='errors'}
{include file="$tpl_dir./errors.tpl"}
{/block}
{if !isset($errors) OR !sizeof($errors)}
<div class="row cat_cont">
<div class="col-sm-12">
{block name='category_top_left'}
@@ -54,6 +59,7 @@
</div>
</div>
</div>
{/if}
{block name='category_js_vars'}
{strip}