Subida del módulo y tema de PrestaShop

This commit is contained in:
Kaloyan
2026-04-09 18:31:51 +02:00
parent 12c253296f
commit 16b3ff9424
39262 changed files with 7418797 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
<?php
/**
* For the full copyright and license information, please view the
* docs/licenses/LICENSE.txt file that was distributed with this source code.
*/
use PrestaShop\PrestaShop\Adapter\BestSales\BestSalesProductSearchProvider;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;
class BestSalesControllerCore extends ProductListingFrontController
{
/** @var string */
public $php_self = 'best-sales';
/**
* Returns canonical URL for best-sales page
*
* @return string
*/
public function getCanonicalURL(): string
{
return $this->buildPaginatedUrl($this->context->link->getPageLink('best-sales'));
}
/**
* Initializes controller.
*
* @see FrontController::init()
*
* @throws PrestaShopException
*/
public function init(): void
{
if (Configuration::get('PS_DISPLAY_BEST_SELLERS')) {
parent::init();
} else {
Tools::redirect('pagenotfound');
}
}
/**
* Assign template vars related to page content.
*
* @see FrontController::initContent()
*/
public function initContent(): void
{
parent::initContent();
$this->doProductSearch('catalog/listing/best-sales', ['entity' => 'best-sales']);
}
/**
* Gets the product search query for the controller. This is a set of information that
* a filtering module or the default provider will use to fetch our products.
*
* @return ProductSearchQuery
*/
protected function getProductSearchQuery(): ProductSearchQuery
{
$query = new ProductSearchQuery();
$query
->setQueryType('best-sales')
->setSortOrder(new SortOrder('product', 'sales', 'desc'));
return $query;
}
/**
* Default product search provider used if no filtering module stood up for the job
*
* @return BestSalesProductSearchProvider
*/
protected function getDefaultProductSearchProvider(): BestSalesProductSearchProvider
{
return new BestSalesProductSearchProvider(
$this->getTranslator()
);
}
public function getListingLabel(): string
{
return $this->getTranslator()->trans('Best sellers', [], 'Shop.Theme.Catalog');
}
public function getBreadcrumbLinks(): array
{
$breadcrumb = parent::getBreadcrumbLinks();
$breadcrumb['links'][] = [
'title' => $this->trans('Best sellers', [], 'Shop.Theme.Catalog'),
'url' => $this->context->link->getPageLink('best-sales'),
];
return $breadcrumb;
}
}

View File

@@ -0,0 +1,382 @@
<?php
/**
* For the full copyright and license information, please view the
* docs/licenses/LICENSE.txt file that was distributed with this source code.
*/
use PrestaShop\PrestaShop\Adapter\Category\CategoryProductSearchProvider;
use PrestaShop\PrestaShop\Adapter\Image\ImageRetriever;
use PrestaShop\PrestaShop\Adapter\Presenter\Category\CategoryLazyArray;
use PrestaShop\PrestaShop\Adapter\Presenter\Category\CategoryPresenter;
use PrestaShop\PrestaShop\Core\Domain\Category\ValueObject\RedirectType;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;
class CategoryControllerCore extends ProductListingFrontController
{
/** @var string Internal controller name */
public $php_self = 'category';
/** @var bool If set to false, customer cannot view the current category. */
public $customer_access = true;
/** @var bool */
protected $notFound = false;
/**
* @var Category
*/
protected $category;
/** @var CategoryPresenter */
protected $categoryPresenter;
public function canonicalRedirection(string $canonicalURL = ''): void
{
if (Validate::isLoadedObject($this->category)) {
parent::canonicalRedirection($this->context->link->getCategoryLink($this->category));
}
}
/**
* Returns canonical URL for current category
*
* @return string
*/
public function getCanonicalURL(): string
{
if (!Validate::isLoadedObject($this->category)) {
return '';
}
return $this->buildPaginatedUrl($this->context->link->getCategoryLink($this->category));
}
/**
* Initializes category controller.
*
* @see FrontController::init()
*
* @throws PrestaShopException
*/
public function init(): void
{
// Get proper IDs
$id_category = (int) Tools::getValue('id_category');
// Try to load category object
$this->category = new Category($id_category, $this->context->language->id);
/*
* Call of parent::init() must be here, after initializing the category. Inside FrontController class
* there is a call to canonical redirection logic, that needs proper category data for it to work.
* If you move this to the top of init() method, the redirection will stop working.
*/
parent::init();
// Otherwise immediately show 404
if (!Validate::isLoadedObject($this->category)) {
header('HTTP/1.1 404 Not Found');
header('Status: 404 Not Found');
$this->errors[] = $this->trans('This category is no longer available.', [], 'Shop.Notifications.Error');
$this->setTemplate('errors/404');
$this->notFound = true;
return;
}
// If this category is not active or not related to current shop in multistore context,
// we treat it as not available. We will either redirect away or show error, depending
// on settings of the category.
if (!$this->category->active || !$this->category->existsInShop($this->context->shop->id)) {
// If category should redirect and we don't know where, we take the closest parent
if (!$this->category->id_type_redirected && in_array($this->category->redirect_type, [RedirectType::TYPE_PERMANENT, RedirectType::TYPE_TEMPORARY])) {
$this->category->id_type_redirected = $this->getCategoryToRedirectTo();
}
// Now, we do as configured in "Redirection when not displayed" field on the category
switch ($this->category->redirect_type) {
case RedirectType::TYPE_PERMANENT:
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $this->context->link->getCategoryLink($this->category->id_type_redirected));
exit;
case RedirectType::TYPE_TEMPORARY:
header('HTTP/1.1 302 Moved Temporarily');
header('Cache-Control: no-cache');
header('Location: ' . $this->context->link->getCategoryLink($this->category->id_type_redirected));
exit;
case RedirectType::TYPE_GONE:
header('HTTP/1.1 410 Gone');
header('Status: 410 Gone');
$this->errors[] = $this->trans('This category is no longer available.', [], 'Shop.Notifications.Error');
$this->setTemplate('errors/410');
$this->notFound = true;
break;
case RedirectType::TYPE_NOT_FOUND:
default:
header('HTTP/1.1 404 Not Found');
header('Status: 404 Not Found');
$this->errors[] = $this->trans('This category is no longer available.', [], 'Shop.Notifications.Error');
$this->setTemplate('errors/404');
$this->notFound = true;
break;
}
return;
}
// And one last check, we need to validate if current customer is a member
// of at least one group allowed to view this category.
if (!$this->category->checkAccess($this->context->customer->id)) {
header('HTTP/1.1 403 Forbidden');
header('Status: 403 Forbidden');
$this->errors[] = $this->trans('You do not have access to this category.', [], 'Shop.Notifications.Error');
$this->setTemplate('errors/forbidden');
return;
}
// Initialize presenter, we will use it for all cases
$this->categoryPresenter = new CategoryPresenter($this->context->link);
$this->context->smarty->assign([
'category' => $this->getTemplateVarCategory(),
'subcategories' => $this->getTemplateVarSubCategories(),
]);
}
/**
* Assign template vars related to page content.
*
* @see FrontController::initContent()
*/
public function initContent(): void
{
parent::initContent();
if (
Validate::isLoadedObject($this->category)
&& $this->category->active
&& $this->category->checkAccess($this->context->customer->id)
&& $this->category->existsInShop($this->context->shop->id)
) {
$this->doProductSearch(
'catalog/listing/category',
[
'entity' => 'category',
'id' => $this->category->id,
]
);
}
}
/**
* overrides layout if category is not visible.
*
* @return bool|string
*/
public function getLayout(): bool|string
{
if (!$this->category->checkAccess($this->context->customer->id) || $this->notFound) {
return $this->context->shop->theme->getLayoutRelativePathForPage('error');
}
return parent::getLayout();
}
protected function getAjaxProductSearchVariables(): array
{
// Basic data with rendered products, facets, active filters etc.
$data = parent::getAjaxProductSearchVariables();
// Extra data for category pages, so we can dynamically update also these parts
$rendered_category_header = $this->render('catalog/_partials/category-header', ['listing' => $data]);
$data['rendered_products_header'] = $rendered_category_header;
$rendered_category_footer = $this->render('catalog/_partials/category-footer', ['listing' => $data]);
$data['rendered_products_footer'] = $rendered_category_footer;
return $data;
}
/**
* Gets the product search query for the controller. This is a set of information that
* a filtering module or the default provider will use to fetch our products.
*
* @return ProductSearchQuery
*
* @throws PrestaShop\PrestaShop\Core\Product\Search\Exception\InvalidSortOrderDirectionException
*/
protected function getProductSearchQuery(): ProductSearchQuery
{
$query = new ProductSearchQuery();
$query
->setQueryType('category')
->setIdCategory($this->category->id)
->setSortOrder(new SortOrder('product', Tools::getProductsOrder('by'), Tools::getProductsOrder('way')));
return $query;
}
/**
* Default product search provider used if no filtering module stood up for the job
*
* @return CategoryProductSearchProvider
*/
protected function getDefaultProductSearchProvider(): CategoryProductSearchProvider
{
return new CategoryProductSearchProvider(
$this->getTranslator(),
$this->category
);
}
protected function getTemplateVarCategory(): CategoryLazyArray
{
$categoryVar = $this->categoryPresenter->present(
$this->category,
$this->context->language
);
$filteredCategory = Hook::exec(
'filterCategoryContent',
['object' => $categoryVar],
$id_module = null,
$array_return = false,
$check_exceptions = true,
$use_push = false,
$id_shop = null,
$chain = true
);
if (!empty($filteredCategory['object'])) {
$categoryVar = $filteredCategory['object'];
}
return $categoryVar;
}
protected function getTemplateVarSubCategories(): array
{
$subcategories = $this->category->getSubCategories($this->context->language->id);
foreach ($subcategories as &$subcategory) {
$subcategory = $this->categoryPresenter->present(
$subcategory,
$this->context->language
);
}
return $subcategories;
}
/**
* @deprecated since 9.0.0 and will be removed in 10.0.0
*/
protected function getImage(Category $object, int $id_image)
{
$retriever = new ImageRetriever(
$this->context->link
);
return $retriever->getImage($object, $id_image);
}
public function getBreadcrumbLinks(): array
{
$breadcrumb = parent::getBreadcrumbLinks();
foreach ($this->category->getAllParents() as $category) {
/** @var Category $category */
if ($category->id_parent != 0 && !$category->is_root_category && $category->active) {
$breadcrumb['links'][] = [
'title' => $category->name,
'url' => $this->context->link->getCategoryLink($category),
];
}
}
if ($this->category->id_parent != 0 && !$this->category->is_root_category && $this->category->active) {
$breadcrumb['links'][] = [
'title' => $this->category->name,
'url' => $this->context->link->getCategoryLink($this->category),
];
}
return $breadcrumb;
}
/**
* @return Category
*/
public function getCategory(): Category
{
return $this->category;
}
/**
* Initializes a set of commonly used variables related to the current page, available for use
* in the template. @see FrontController::assignGeneralPurposeVariables for more information.
*
* @return array
*/
public function getTemplateVarPage(): array
{
$page = parent::getTemplateVarPage();
if ($this->notFound) {
$page['page_name'] = 'pagenotfound';
$page['body_classes']['pagenotfound'] = true;
$page['title'] = $this->trans('The page you are looking for was not found.', [], 'Shop.Theme.Global');
} else {
$page['body_classes']['category-id-' . $this->category->id] = true;
$page['body_classes']['category-' . $this->category->name] = true;
$page['body_classes']['category-id-parent-' . $this->category->id_parent] = true;
$page['body_classes']['category-depth-level-' . $this->category->level_depth] = true;
}
return $page;
}
public function getListingLabel(): string
{
if (!Validate::isLoadedObject($this->category)) {
$this->category = new Category(
(int) Tools::getValue('id_category'),
$this->context->language->id
);
}
return $this->trans(
'Category: %category_name%',
['%category_name%' => $this->category->name],
'Shop.Theme.Catalog'
);
}
/**
* Returns a category that we will redirect into, in case we need 301/302 redirect.
* We will try to get the closest active parent of the current category.
*
* @return int category ID
*/
private function getCategoryToRedirectTo(): int
{
$categoryToRedirectTo = null;
foreach ($this->category->getParentsCategories() as $category) {
/*
* Or new favourite category is a one:
* that is not the current one
* that is active
* and that is deeper than the last one
*/
if ($category['id_category'] != $this->category->id
&& $category['active'] == 1
&& $category['level_depth'] > $categoryToRedirectTo['level_depth']
) {
$categoryToRedirectTo = $category;
}
}
return $categoryToRedirectTo['id_category'];
}
}

View File

@@ -0,0 +1,263 @@
<?php
/**
* For the full copyright and license information, please view the
* docs/licenses/LICENSE.txt file that was distributed with this source code.
*/
use PrestaShop\PrestaShop\Adapter\Manufacturer\ManufacturerProductSearchProvider;
use PrestaShop\PrestaShop\Adapter\Presenter\Manufacturer\ManufacturerPresenter;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;
class ManufacturerControllerCore extends ProductListingFrontController
{
/** @var string */
public $php_self = 'manufacturer';
/** @var Manufacturer|null */
protected $manufacturer;
protected $label;
/** @var ManufacturerPresenter */
protected $manufacturerPresenter;
public function canonicalRedirection(string $canonicalURL = ''): void
{
if (Validate::isLoadedObject($this->manufacturer)) {
parent::canonicalRedirection($this->context->link->getManufacturerLink($this->manufacturer));
} elseif ($canonicalURL) {
parent::canonicalRedirection($canonicalURL);
}
}
/**
* Returns canonical URL for current manufacturer or a manufacturer list
*
* @return string
*/
public function getCanonicalURL(): string
{
if (Validate::isLoadedObject($this->manufacturer)) {
return $this->buildPaginatedUrl($this->context->link->getManufacturerLink($this->manufacturer));
}
return $this->context->link->getPageLink('manufacturer');
}
/**
* Initialize manufaturer controller.
*
* @see FrontController::init()
*/
public function init(): void
{
if ($id_manufacturer = Tools::getValue('id_manufacturer')) {
$this->manufacturer = new Manufacturer((int) $id_manufacturer, $this->context->language->id);
if (!Validate::isLoadedObject($this->manufacturer) || !$this->manufacturer->active || !$this->manufacturer->isAssociatedToShop()) {
$this->redirect_after = '404';
$this->redirect();
} else {
$this->canonicalRedirection();
}
}
// Initialize presenter, we will use it for all cases
$this->manufacturerPresenter = new ManufacturerPresenter($this->context->link);
parent::init();
}
/**
* Assign template vars related to page content.
*
* @see FrontController::initContent()
*/
public function initContent(): void
{
if (Configuration::get('PS_DISPLAY_MANUFACTURERS')) {
parent::initContent();
if (Validate::isLoadedObject($this->manufacturer) && $this->manufacturer->active && $this->manufacturer->isAssociatedToShop()) {
$this->assignManufacturer();
$this->label = $this->trans(
'List of products by brand %brand_name%',
[
'%brand_name%' => $this->manufacturer->name,
],
'Shop.Theme.Catalog'
);
$this->doProductSearch(
'catalog/listing/manufacturer',
['entity' => 'manufacturer', 'id' => $this->manufacturer->id]
);
} else {
$this->assignAll();
$this->label = $this->trans(
'List of all brands',
[],
'Shop.Theme.Catalog'
);
$this->setTemplate('catalog/manufacturers', ['entity' => 'manufacturers']);
}
} else {
$this->redirect_after = '404';
$this->redirect();
}
}
/**
* Gets the product search query for the controller. This is a set of information that
* a filtering module or the default provider will use to fetch our products.
*
* @return ProductSearchQuery
*
* @throws PrestaShop\PrestaShop\Core\Product\Search\Exception\InvalidSortOrderDirectionException
*/
protected function getProductSearchQuery(): ProductSearchQuery
{
$query = new ProductSearchQuery();
$query
->setQueryType('manufacturer')
->setIdManufacturer($this->manufacturer->id)
->setSortOrder(new SortOrder('product', Tools::getProductsOrder('by'), Tools::getProductsOrder('way')));
return $query;
}
/**
* Default product search provider used if no filtering module stood up for the job
*
* @return ManufacturerProductSearchProvider
*/
protected function getDefaultProductSearchProvider(): ManufacturerProductSearchProvider
{
return new ManufacturerProductSearchProvider(
$this->getTranslator(),
$this->manufacturer
);
}
/**
* Assign template vars if displaying one manufacturer.
*/
protected function assignManufacturer(): void
{
$manufacturerVar = $this->manufacturerPresenter->present(
$this->manufacturer,
$this->context->language
);
// Chained hook call - if multiple modules are hooked here, they will receive the result of the previous one as a parameter
$filteredManufacturer = Hook::exec(
'filterManufacturerContent',
['object' => $manufacturerVar],
$id_module = null,
$array_return = false,
$check_exceptions = true,
$use_push = false,
$id_shop = null,
$chain = true
);
if (!empty($filteredManufacturer['object'])) {
$manufacturerVar = $filteredManufacturer['object'];
}
$this->context->smarty->assign([
'manufacturer' => $manufacturerVar,
]);
}
/**
* Assign template vars if displaying the manufacturer list.
*/
protected function assignAll(): void
{
$manufacturersVar = $this->getTemplateVarManufacturers();
if (!empty($manufacturersVar)) {
foreach ($manufacturersVar as $k => $manufacturer) {
// Chained hook call - if multiple modules are hooked here, they will receive the result of the previous one as a parameter
$filteredManufacturer = Hook::exec(
'filterManufacturerContent',
['object' => $manufacturer],
$id_module = null,
$array_return = false,
$check_exceptions = true,
$use_push = false,
$id_shop = null,
$chain = true
);
if (!empty($filteredManufacturer['object'])) {
$manufacturersVar[$k] = $filteredManufacturer['object'];
}
}
}
$this->context->smarty->assign([
'brands' => $manufacturersVar,
]);
}
public function getTemplateVarManufacturers(): array
{
$manufacturers = Manufacturer::getManufacturers(true, $this->context->language->id);
foreach ($manufacturers as &$manufacturer) {
$manufacturer = $this->manufacturerPresenter->present(
$manufacturer,
$this->context->language
);
}
return $manufacturers;
}
public function getListingLabel(): string
{
return $this->label;
}
public function getBreadcrumbLinks(): array
{
$breadcrumb = parent::getBreadcrumbLinks();
$breadcrumb['links'][] = [
'title' => $this->getTranslator()->trans('Brands', [], 'Shop.Theme.Global'),
'url' => $this->context->link->getPageLink('manufacturer'),
];
if (!empty($this->manufacturer)) {
$breadcrumb['links'][] = [
'title' => $this->manufacturer->name,
'url' => $this->context->link->getManufacturerLink($this->manufacturer),
];
}
return $breadcrumb;
}
/**
* Initializes a set of commonly used variables related to the current page, available for use
* in the template. @see FrontController::assignGeneralPurposeVariables for more information.
*
* @return array
*/
public function getTemplateVarPage(): array
{
$page = parent::getTemplateVarPage();
if (!empty($this->manufacturer)) {
$page['body_classes']['manufacturer-id-' . $this->manufacturer->id] = true;
$page['body_classes']['manufacturer-' . $this->manufacturer->name] = true;
}
return $page;
}
/**
* @return Manufacturer|null
*/
public function getManufacturer(): ?Manufacturer
{
return $this->manufacturer;
}
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* For the full copyright and license information, please view the
* docs/licenses/LICENSE.txt file that was distributed with this source code.
*/
use PrestaShop\PrestaShop\Adapter\NewProducts\NewProductsProductSearchProvider;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;
class NewProductsControllerCore extends ProductListingFrontController
{
/** @var string */
public $php_self = 'new-products';
/**
* Returns canonical URL for new-products page
*
* @return string
*/
public function getCanonicalURL(): string
{
return $this->buildPaginatedUrl($this->context->link->getPageLink('new-products'));
}
/**
* Assign template vars related to page content.
*
* @see FrontController::initContent()
*/
public function initContent(): void
{
parent::initContent();
$this->doProductSearch('catalog/listing/new-products', ['entity' => 'new-products']);
}
/**
* Gets the product search query for the controller. This is a set of information that
* a filtering module or the default provider will use to fetch our products.
*
* @return ProductSearchQuery
*/
protected function getProductSearchQuery(): ProductSearchQuery
{
$query = new ProductSearchQuery();
$query
->setQueryType('new-products')
->setSortOrder(new SortOrder('product', 'date_add', 'desc'));
return $query;
}
/**
* Default product search provider used if no filtering module stood up for the job
*
* @return NewProductsProductSearchProvider
*/
protected function getDefaultProductSearchProvider(): NewProductsProductSearchProvider
{
return new NewProductsProductSearchProvider(
$this->getTranslator()
);
}
public function getListingLabel(): string
{
return $this->trans(
'New products',
[],
'Shop.Theme.Catalog'
);
}
public function getBreadcrumbLinks(): array
{
$breadcrumb = parent::getBreadcrumbLinks();
$breadcrumb['links'][] = [
'title' => $this->trans('New products', [], 'Shop.Theme.Catalog'),
'url' => $this->context->link->getPageLink('new-products'),
];
return $breadcrumb;
}
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* For the full copyright and license information, please view the
* docs/licenses/LICENSE.txt file that was distributed with this source code.
*/
use PrestaShop\PrestaShop\Adapter\PricesDrop\PricesDropProductSearchProvider;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;
class PricesDropControllerCore extends ProductListingFrontController
{
/** @var string */
public $php_self = 'prices-drop';
/**
* Returns canonical URL for prices-drop page
*
* @return string
*/
public function getCanonicalURL(): string
{
return $this->buildPaginatedUrl($this->context->link->getPageLink('prices-drop'));
}
/**
* Assign template vars related to page content.
*
* @see FrontController::initContent()
*/
public function initContent(): void
{
parent::initContent();
$this->doProductSearch('catalog/listing/prices-drop', ['entity' => 'prices-drop']);
}
/**
* Gets the product search query for the controller. This is a set of information that
* a filtering module or the default provider will use to fetch our products.
*
* @return ProductSearchQuery
*/
protected function getProductSearchQuery(): ProductSearchQuery
{
$query = new ProductSearchQuery();
$query
->setQueryType('prices-drop')
->setSortOrder(new SortOrder('product', 'name', 'asc'));
return $query;
}
/**
* Default product search provider used if no filtering module stood up for the job
*
* @return PricesDropProductSearchProvider
*/
protected function getDefaultProductSearchProvider(): PricesDropProductSearchProvider
{
return new PricesDropProductSearchProvider(
$this->getTranslator()
);
}
public function getListingLabel(): string
{
return $this->trans(
'Prices drop',
[],
'Shop.Theme.Catalog'
);
}
public function getBreadcrumbLinks(): array
{
$breadcrumb = parent::getBreadcrumbLinks();
$breadcrumb['links'][] = [
'title' => $this->trans('Prices drop', [], 'Shop.Theme.Catalog'),
'url' => $this->context->link->getPageLink('prices-drop'),
];
return $breadcrumb;
}
}

View File

@@ -0,0 +1,128 @@
<?php
/**
* For the full copyright and license information, please view the
* docs/licenses/LICENSE.txt file that was distributed with this source code.
*/
use PrestaShop\PrestaShop\Adapter\Search\SearchProductSearchProvider;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;
class SearchControllerCore extends ProductListingFrontController
{
/** @var string */
public $php_self = 'search';
public $instant_search;
public $ajax_search;
protected $search_string;
protected $search_tag;
/**
* Initialize the controller.
*
* @see FrontController::init()
*/
public function init(): void
{
parent::init();
$this->search_string = Tools::getValue('s');
if (!$this->search_string) {
$this->search_string = Tools::getValue('search_query');
}
$this->search_tag = Tools::getValue('tag');
$this->context->smarty->assign(
[
'search_string' => $this->search_string,
'search_tag' => $this->search_tag,
'subcategories' => [],
]
);
}
/**
* Returns canonical URL for a search page with this term
*
* @return string
*/
public function getCanonicalURL(): string
{
return $this->buildPaginatedUrl($this->context->link->getPageLink('search', null, null, ['s' => $this->search_string]));
}
/**
* Initializes a set of commonly used variables related to the current page, available for use
* in the template. @see FrontController::assignGeneralPurposeVariables for more information.
*
* @return array
*/
public function getTemplateVarPage(): array
{
$page = parent::getTemplateVarPage();
// Ensure that no search results page is indexed by search engines.
$page['meta']['robots'] = 'noindex';
return $page;
}
/**
* Assign template vars related to page content.
*
* @see FrontController::initContent()
*/
public function initContent(): void
{
parent::initContent();
$this->doProductSearch('catalog/listing/search', ['entity' => 'search']);
}
/**
* Gets the product search query for the controller. This is a set of information that
* a filtering module or the default provider will use to fetch our products.
*
* @return ProductSearchQuery
*/
protected function getProductSearchQuery(): ProductSearchQuery
{
$query = new ProductSearchQuery();
$query
->setQueryType('search')
->setSortOrder(new SortOrder('product', 'position', 'desc'))
->setSearchString($this->search_string)
->setSearchTag($this->search_tag);
return $query;
}
/**
* Default product search provider used if no filtering module stood up for the job
*
* @return SearchProductSearchProvider
*/
protected function getDefaultProductSearchProvider(): SearchProductSearchProvider
{
return new SearchProductSearchProvider(
$this->getTranslator()
);
}
public function getListingLabel(): string
{
return $this->getTranslator()->trans('Search results', [], 'Shop.Theme.Catalog');
}
public function getBreadcrumbLinks(): array
{
$breadcrumb = parent::getBreadcrumbLinks();
$breadcrumb['links'][] = [
'title' => $this->getTranslator()->trans('Search results', [], 'Shop.Theme.Catalog'),
'url' => $this->getCurrentUrl(),
];
return $breadcrumb;
}
}

View File

@@ -0,0 +1,261 @@
<?php
/**
* For the full copyright and license information, please view the
* docs/licenses/LICENSE.txt file that was distributed with this source code.
*/
use PrestaShop\PrestaShop\Adapter\Presenter\Supplier\SupplierPresenter;
use PrestaShop\PrestaShop\Adapter\Supplier\SupplierProductSearchProvider;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;
class SupplierControllerCore extends ProductListingFrontController
{
/** @var string */
public $php_self = 'supplier';
/** @var Supplier|null */
protected $supplier;
protected $label;
/** @var SupplierPresenter */
protected $supplierPresenter;
public function canonicalRedirection(string $canonicalURL = ''): void
{
if (Validate::isLoadedObject($this->supplier)) {
parent::canonicalRedirection($this->context->link->getSupplierLink($this->supplier));
} elseif ($canonicalURL) {
parent::canonicalRedirection($canonicalURL);
}
}
/**
* Returns canonical URL for current supplier or a supplier list
*
* @return string
*/
public function getCanonicalURL(): string
{
if (Validate::isLoadedObject($this->supplier)) {
return $this->buildPaginatedUrl($this->context->link->getSupplierLink($this->supplier));
}
return $this->context->link->getPageLink('supplier');
}
/**
* Initialize supplier controller.
*
* @see FrontController::init()
*/
public function init(): void
{
if ($id_supplier = (int) Tools::getValue('id_supplier')) {
$this->supplier = new Supplier($id_supplier, $this->context->language->id);
if (!Validate::isLoadedObject($this->supplier) || !$this->supplier->active) {
$this->redirect_after = '404';
$this->redirect();
} else {
$this->canonicalRedirection();
}
}
// Initialize presenter, we will use it for all cases
$this->supplierPresenter = new SupplierPresenter($this->context->link);
parent::init();
}
/**
* Assign template vars related to page content.
*
* @see FrontController::initContent()
*/
public function initContent(): void
{
if (Configuration::get('PS_DISPLAY_SUPPLIERS')) {
parent::initContent();
if (Validate::isLoadedObject($this->supplier) && $this->supplier->active && $this->supplier->isAssociatedToShop()) {
$this->assignSupplier();
$this->label = $this->trans(
'List of products by supplier %supplier_name%',
[
'%supplier_name%' => $this->supplier->name,
],
'Shop.Theme.Catalog'
);
$this->doProductSearch(
'catalog/listing/supplier',
['entity' => 'supplier', 'id' => $this->supplier->id]
);
} else {
$this->assignAll();
$this->label = $this->trans(
'List of all suppliers',
[],
'Shop.Theme.Catalog'
);
$this->setTemplate('catalog/suppliers', ['entity' => 'suppliers']);
}
} else {
$this->redirect_after = '404';
$this->redirect();
}
}
/**
* Gets the product search query for the controller. This is a set of information that
* a filtering module or the default provider will use to fetch our products.
*
* @return ProductSearchQuery
*/
protected function getProductSearchQuery(): ProductSearchQuery
{
$query = new ProductSearchQuery();
$query
->setQueryType('supplier')
->setIdSupplier($this->supplier->id)
->setSortOrder(new SortOrder('product', 'position', 'asc'));
return $query;
}
/**
* Default product search provider used if no filtering module stood up for the job
*
* @return SupplierProductSearchProvider
*/
protected function getDefaultProductSearchProvider(): SupplierProductSearchProvider
{
return new SupplierProductSearchProvider(
$this->getTranslator(),
$this->supplier
);
}
/**
* Assign template vars if displaying one supplier.
*/
protected function assignSupplier(): void
{
$supplierVar = $this->supplierPresenter->present(
$this->supplier,
$this->context->language
);
// Chained hook call - if multiple modules are hooked here, they will receive the result of the previous one as a parameter
$filteredSupplier = Hook::exec(
'filterSupplierContent',
['object' => $supplierVar],
null,
false,
true,
false,
null,
true
);
if (!empty($filteredSupplier['object'])) {
$supplierVar = $filteredSupplier['object'];
}
$this->context->smarty->assign([
'supplier' => $supplierVar,
]);
}
/**
* Assign template vars if displaying the supplier list.
*/
protected function assignAll(): void
{
$suppliersVar = $this->getTemplateVarSuppliers();
if (!empty($suppliersVar)) {
foreach ($suppliersVar as $k => $supplier) {
// Chained hook call - if multiple modules are hooked here, they will receive the result of the previous one as a parameter
$filteredSupplier = Hook::exec(
'filterSupplierContent',
['object' => $supplier],
null,
false,
true,
false,
null,
true
);
if (!empty($filteredSupplier['object'])) {
$suppliersVar[$k] = $filteredSupplier['object'];
}
}
}
$this->context->smarty->assign([
'suppliers' => $suppliersVar,
]);
}
public function getTemplateVarSuppliers(): array
{
$suppliers = Supplier::getSuppliers(true, $this->context->language->id, true);
foreach ($suppliers as &$supplier) {
$supplier = $this->supplierPresenter->present(
$supplier,
$this->context->language
);
}
return $suppliers;
}
public function getListingLabel(): string
{
return $this->label;
}
public function getBreadcrumbLinks(): array
{
$breadcrumb = parent::getBreadcrumbLinks();
$breadcrumb['links'][] = [
'title' => $this->trans('Suppliers', [], 'Shop.Theme.Catalog'),
'url' => $this->context->link->getPageLink('supplier'),
];
if (!empty($this->supplier)) {
$breadcrumb['links'][] = [
'title' => $this->supplier->name,
'url' => $this->context->link->getSupplierLink($this->supplier),
];
}
return $breadcrumb;
}
/**
* Initializes a set of commonly used variables related to the current page, available for use
* in the template. @see FrontController::assignGeneralPurposeVariables for more information.
*
* @return array
*/
public function getTemplateVarPage(): array
{
$page = parent::getTemplateVarPage();
if (!empty($this->supplier)) {
$page['body_classes']['supplier-id-' . $this->supplier->id] = true;
$page['body_classes']['supplier-' . $this->supplier->name] = true;
}
return $page;
}
/**
* @return Supplier|null
*/
public function getSupplier(): ?Supplier
{
return $this->supplier;
}
}

View File

@@ -0,0 +1,14 @@
<?php
/**
* For the full copyright and license information, please view the
* docs/licenses/LICENSE.txt file that was distributed with this source code.
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;