Add new templates and update existing ones for An Home Products module

- Created new index.php files for various directories to establish structure.
- Added form.tpl and index.php for form helpers to enhance form handling.
- Introduced suggestions.tpl and top.tpl for improved admin interface.
- Implemented ajax-products.tpl and banners.tpl for front-end product display.
- Developed content.tpl and widget-blocks.tpl for dynamic content rendering.
- Enhanced widget-tabs.tpl and widget-wrapper.tpl for better tabbed navigation.
- Included necessary licensing information in all new files.
This commit is contained in:
2025-05-16 14:21:29 +02:00
parent b65352c452
commit 64bcc1a6be
140 changed files with 5459 additions and 7457 deletions

View File

@@ -0,0 +1,63 @@
<?php
/**
* 2024 Anvanto
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
*
* @author Anvanto <anvantoco@gmail.com>
* @copyright 2024 Anvanto
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
use PrestaShop\PrestaShop\Core\Domain\Product\Query\SearchProducts;
class AdminAnhomeproductsAjaxController extends ModuleAdminController
{
public function initContent()
{
$result = [];
if (Tools::isSubmit('action')) {
$actionName = Tools::getValue('action', '') . 'Action';
if (method_exists($this, $actionName)) {
$result = $this->$actionName();
}
}
die(json_encode($result));
}
public function searchProductsAction()
{
// $defaultCurrencyId = (int) $this->get('prestashop.adapter.legacy.configuration')->get('PS_CURRENCY_DEFAULT');
//$searchPhrase = $request->query->get('search_phrase');
$searchPhrase = Tools::getValue('q');
if (!$searchPhrase){
return [];
}
$foundProducts = Product::searchByName((int) $this->context->language->id, pSQL($searchPhrase));
if (!$foundProducts){
return [];
}
$products = [];
foreach ($foundProducts as $fProduct){
$products[] = [
'id' => $fProduct['id_product'],
'name' => $fProduct['name']
];
}
return $products;
}
}

View File

@@ -0,0 +1,604 @@
<?php
/**
* 2024 Anvanto
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
*
* @author Anvanto <anvantoco@gmail.com>
* @copyright 2024 Anvanto
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once _PS_MODULE_DIR_ . 'an_homeproducts/classes/anHomeProductsBanners.php';
require_once _PS_MODULE_DIR_ . 'an_homeproducts/classes/anHomeProductFiles.php';
class AdminAnhomeproductsBannersController extends ModuleAdminController
{
protected $_module = null;
protected $position_identifier = 'position';
protected $_defaultOrderBy = 'position';
protected $_defaultOrderWay = 'ASC';
public function __construct()
{
$this->bootstrap = true;
$this->context = Context::getContext();
$this->table = 'an_homeproducts_banners';
$this->identifier = 'id_banner';
$this->className = 'anHomeProductsBanners';
$this->lang = true;
$this->addRowAction('edit');
$this->addRowAction('delete');
$this->name = 'AdminAnhomeproductsBannersController';
parent::__construct();
$this->fields_list = [
'id_banner' => [
'title' => $this->l('ID'),
'width' => 25,
'search' => false
],
'image' => [
'title' => $this->l('Image'),
'search' => false,
'type' => 'image'
],
'title' => [
'title' => $this->trans('Title', [], 'Admin.Global'),
'search' => false
],
'block' => [
'title' => $this->l('Block'),
'search' => false
],
'block_position' => [
'title' => $this->l('Block position'),
'search' => false
],
'position' => [
'title' => $this->trans('Position', [], 'Admin.Global'),
'search' => false,
'position' => true
],
'col' => [
'title' => $this->l('Col'),
'search' => false
],
'active' => [
'title' => $this->trans('Active', [], 'Admin.Global'),
'width' => 40,
'active' => 'update',
'align' => 'center',
'type' => 'bool',
'search' => false,
'orderby' => false
]
];
if (Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_ALL) {
$this->_where .= ' AND a.' . $this->identifier . ' IN (
SELECT sa.' . $this->identifier . '
FROM `' . _DB_PREFIX_ . $this->table . '_shop` sa
WHERE sa.id_shop IN (' . implode(', ', Shop::getContextListShopID()) . ')
)';
}
}
public function l($string, $class = null, $addslashes = false, $htmlentities = true)
{
return parent::l($string, pathinfo(__FILE__, PATHINFO_FILENAME), $addslashes, $htmlentities);
}
public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
{
parent::getList($id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);
$blocksOriginal = anHomeProductsBlocks::getBlocks(false, true);
$blocks = [];
foreach ($blocksOriginal as $block){
$blocks[$block['special_id_block']] = $block;
}
foreach ($this->_list as &$list) {
if ($list['block'] == 0){
$list['block'] = $this->l('Global');
} else if (isset($blocks[$list['block']])) {
$list['block'] = $blocks[$list['block']]['title'];
}
if ($list['block_position'] == 0){
$list['block_position'] = $this->l('Top');
} else {
$list['block_position'] = $this->l('Bottom');
}
if ($list['image'] !='' && Tools::file_exists_no_cache(anHomeProductsBanners::imgDir.$list['image'])) {
$list['image'] = anHomeProductsBanners::imgUrl.$list['image'];
} else {
$list['image'] = '';
}
}
}
public function setMedia($isNewTheme = false)
{
parent::setMedia($isNewTheme);
$this->addJquery();
$this->js_files[] = _MODULE_DIR_ . 'an_homeproducts/views/js/back.js';
$this->css_files[_MODULE_DIR_ . 'an_homeproducts/views/css/back.css'] = 'all';
}
public function renderList()
{
return parent::renderList() . $this->module->topPromo();
}
public function initToolBarTitle()
{
$this->toolbar_title[] = $this->l('Home Products: Banners');
}
public function initHeader()
{
parent::initHeader();
$id_lang = $this->context->language->id;
$link = $this->context->link;
$tabs = &$this->context->smarty->tpl_vars['tabs']->value;
foreach ($tabs as &$tab0) {
if ($tab0['class_name'] == 'IMPROVE') {
foreach ($tab0['sub_tabs'] as &$tab1) {
if ($tab1['class_name'] == 'AdminParentModulesSf') {
foreach ($tab1['sub_tabs'] as &$tab2) {
if ($tab2['class_name'] == 'AdminAnhomeproductsBanners') {
$sub_tabs = &$tab2['sub_tabs'];
$tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnhomeproductsBlocks'));
$tab['current'] = false;
$tab['href'] = $link->getAdminLink('AdminAnhomeproductsBlocks');
$tab['active'] = true;
$tab['name'] = $this->l('Products to display');
$sub_tabs[] = $tab;
$tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnhomeproductsBanners'));
$tab['current'] = true;
$tab['href'] = $link->getAdminLink('AdminAnhomeproductsBanners');
$tab['active'] = true;
$tab['name'] = $this->l('Banners');
$sub_tabs[] = $tab;
$tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnhomeproductsSettings'));
$tab['current'] = false;
$tab['href'] = $link->getAdminLink('AdminAnhomeproductsSettings');
$tab['active'] = true;
$tab['name'] = $this->trans('Settings', [], 'Admin.Global');
$sub_tabs[] = $tab;
break;
}
}
break;
}
}
break;
}
}
}
public function initContent()
{
$this->context->smarty->assign('current_tab_level', 3);
return parent::initContent();
}
public function initPageHeaderToolbar()
{
if (empty($this->display)) {
$this->page_header_toolbar_btn['add_field'] = array(
'href' => self::$currentIndex . '&addan_homeproducts_banners&token=' . $this->token,
'desc' => $this->trans('Add new', array(), 'Admin.Actions'),
'icon' => 'process-icon-new',
);
}
parent::initPageHeaderToolbar();
}
public function renderForm()
{
$this->initToolbar();
if (!$this->loadObject(true)) {
return;
}
$this->fields_form = array(
'tinymce' => false,
'legend' => ['title' => $this->l('Home Products: Banners')],
'input' => [],
'buttons' => [
[
'type' => 'submit',
'title' => $this->l('Save'),
'icon' => 'process-icon-save',
'class' => 'pull-right',
'name' => 'submit'.$this->table
],
[
'type' => 'submit',
'title' => $this->l('Save and stay'),
'icon' => 'process-icon-save',
'class' => 'pull-right',
'name' => 'submit'.$this->table.'AndStay'
],
],
);
$this->fields_form['input'][] = [
'type' => 'switch',
'name' => 'active',
'label' => $this->l('Active'),
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
]
],
];
$blocks = anHomeProductsBlocks::getBlocks(false, true);
$this->fields_form['input'][] = [
'type' => 'select',
'label' => $this->l('Block'),
'name' => 'block',
'options' => [
'query' => array_merge(
[
['special_id_block' => '0', 'title' => $this->l('Global')],
],
$blocks
),
'id' => 'special_id_block',
'name' => 'title',
],
];
$this->fields_form['input'][] = [
'type' => 'select',
'label' => $this->module->l('Block position'),
'name' => 'block_position',
'options' => [
'query' => array_merge(
[
['id' => '0', 'name' => $this->l('Top')],
['id' => '1', 'name' => $this->l('Bottom')]
]
),
'id' => 'id',
'name' => 'name',
],
];
$listCols = [];
for ($i=3; $i <= 12; $i++){
$listCols[] = ['id' => $i, 'name' => $i];
}
$this->fields_form['input'][] = [
'type' => 'select',
'label' => $this->module->l('Col'),
'desc' => $this->l('From 2 to 12'),
'name' => 'col',
'options' => [
'query' => array_merge(
[
['id' => '2', 'name' => '2'],
],
$listCols
),
'id' => 'id',
'name' => 'name',
],
];
$bannerTpls = anHomeProductsBanners::getListTpl();
if (count($bannerTpls) > 1){
$this->fields_form['input'][] = [
'type' => 'select',
'label' => $this->module->l('Template'),
'name' => 'template',
'options' => [
'query' => array_merge(
anHomeProductsBanners::getListTpl()
),
'id' => 'file',
'name' => 'file',
],
];
}
/*
$this->fields_form['input'][] = [
'type' => 'radio',
'label' => $this->l('Show on:'),
'name' => 'show_on',
'default_value' => 0,
'class' => 'an-hp-block-type',
'values' => [
[
'id' => 'desktop_mobile',
'value' => '0',
'label' => $this->l('Desktop & Mobile')
],
[
'id' => 'desktop',
'value' => '1',
'label' => $this->l('Desktop'),
],
[
'id' => 'mobile',
'value' => '2',
'label' => $this->l('Mobile')
],
],
];
*/
$this->fields_form['input'][] = [
'type' => 'file_lang',
'label' => $this->l('Image'),
'lang' => true,
'name' => 'image',
'display_image' => true,
'delete_url' => '',
];
$this->fields_form['input'][] = [
'type' => 'text',
'name' => 'link',
'label' => $this->l('Link'),
'lang' => true,
];
$this->fields_form['input'][] = [
'type' => 'text',
'name' => 'title',
'label' => $this->trans('Title', [], 'Admin.Global'),
'lang' => true,
];
$this->fields_form['input'][] = [
'type' => 'textarea',
'class' => 'autoload_rte',
'name' => 'text',
'label' => $this->l('Text'),
'lang' => true,
];
if (Shop::isFeatureActive()) {
$this->fields_form['input'][] = [
'required' => true,
'type' => 'shop',
'label' => $this->l('Shop association'),
'name' => 'checkBoxShopAsso',
];
}
return parent::renderForm();
}
public function processSave()
{
$languages = Language::getLanguages(false);
$isUpdateImage = false;
foreach ($languages as $lang) {
if (isset($_FILES['image_'.$lang['id_lang']]) && isset($_FILES['image_'.$lang['id_lang']]['tmp_name'])
&& !empty($_FILES['image_'.$lang['id_lang']]['tmp_name'])) {
if ($error = $this->validateUpload($_FILES['image_'.$lang['id_lang']])) {
$this->errors[] = $error;
}
}
}
if (!empty($this->errors)) {
$this->display = 'edit';
return false;
}
$object = parent::processSave();
if (isset($object->id) && $object->id) {
$deleteImage = Tools::getValue('delete_image');
if ($deleteImage && is_array($deleteImage)){
foreach ($deleteImage as $id){
@unlink(anHomeProductsBanners::imgDir . $object->image[$id]);
$object->image[$id] = '';
}
$isUpdateImage = true;
}
foreach ($languages as $lang) {
if (isset($_FILES['image_'.$lang['id_lang']]) && isset($_FILES['image_'.$lang['id_lang']]['tmp_name'])
&& !empty($_FILES['image_'.$lang['id_lang']]['tmp_name'])) {
$ext = substr($_FILES['image_'.$lang['id_lang']]['name'], strrpos($_FILES['image_'.$lang['id_lang']]['name'], '.') + 1);
$fileName = md5(uniqid()) . '_' . $lang['id_lang'] . '.' . $ext;
if (!move_uploaded_file($_FILES['image_'.$lang['id_lang']]['tmp_name'], anHomeProductsBanners::imgDir . $fileName)) {
// return $this->displayError($this->trans('An error occurred while attempting to upload the file.', [], 'Admin.Notifications.Error'));
}
if (isset($object->image[$lang['id_lang']]) && $object->image[$lang['id_lang']] !=''){
@unlink(anHomeProductsBanners::imgDir . $object->image[$lang['id_lang']]);
}
$object->image[$lang['id_lang']] = $fileName;
$isUpdateImage = true;
}
}
if ($isUpdateImage){
$object->save();
}
}
anHomeProductsBanners::exportJsonBanners($this->module->filesManager);
$this->module->_clearCache('*');
if (Tools::getIsset('submit'.$this->table.'AndStay')) {
$this->redirect_after = $this->context->link->getAdminLink($this->controller_name).'&conf=4&updatean_homeproducts_banners&token='.$this->token.'&id_banner='.$object->id;
}
return $object;
}
public function validateUpload($file)
{
$maxFileSize = 4000000;
$types = ['gif', 'jpg', 'jpeg', 'jpe', 'png', 'svg', 'webp'];
if ((int) $maxFileSize > 0 && $file['size'] > (int) $maxFileSize) {
return Context::getContext()->getTranslator()->trans('Image is too large (%1$d kB). Maximum allowed: %2$d kB', [$file['size'] / 1024, $maxFileSize / 1024], 'Admin.Notifications.Error');
}
if (!ImageManager::isCorrectImageFileExt($file['name'], $types) || preg_match('/\%00/', $file['name'])) {
return Context::getContext()->getTranslator()->trans('Image format not recognized, allowed formats are: .gif, .jpg, .png, .svg', [], 'Admin.Notifications.Error');
}
if ($file['error']) {
return Context::getContext()->getTranslator()->trans('Error while uploading image; please change your server\'s settings. (Error code: %s)', [$file['error']], 'Admin.Notifications.Error');
}
return false;
}
public function processDelete()
{
$object = parent::processDelete();
$this->module->_clearCache('*');
anHomeProductsBanners::exportJsonBanners($this->module->filesManager);
return $object;
}
public function ajaxProcessUpdatePositions()
{
$status = false;
$position = 0;
$widget = (array)Tools::getValue('banner');
foreach ($widget as $key => $item){
$ids = explode('_', $item);
$sql = 'UPDATE `' . _DB_PREFIX_ . $this->table .'` SET position="'.(int) $position.'" WHERE '.$this->identifier.'="'.(int) $ids['2'].'" ';
Db::getInstance(_PS_USE_SQL_SLAVE_)->execute($sql);
$position++;
}
if (count($widget) > 0){
$status = true;
}
anHomeProductsBanners::exportJsonBanners($this->module->filesManager);
$this->module->_clearCache('*');
return $this->setJsonResponse(array(
'success' => $status,
'message' => $this->l($status ? 'Blocks reordered successfully' : 'An error occurred')
));
}
protected function setJsonResponse($response)
{
header('Content-Type: application/json; charset=utf8');
print(json_encode($response));
exit;
}
protected function updateAssoShop($id_object)
{
if (!Shop::isFeatureActive()) {
return;
}
$assos_data = $this->getSelectedAssoShop($this->table, $id_object);
$exclude_ids = $assos_data;
foreach (Db::getInstance()->executeS('SELECT id_shop FROM ' . _DB_PREFIX_ . 'shop') as $row) {
if (!$this->context->employee->hasAuthOnShop($row['id_shop'])) {
$exclude_ids[] = $row['id_shop'];
}
}
Db::getInstance()->delete($this->table . '_shop', '`' . $this->identifier . '` = ' . (int) $id_object . ($exclude_ids ? ' AND id_shop NOT IN (' . implode(', ', $exclude_ids) . ')' : ''));
$insert = array();
foreach ($assos_data as $id_shop) {
$insert[] = array(
$this->identifier => $id_object,
'id_shop' => (int) $id_shop,
);
}
return Db::getInstance()->insert($this->table . '_shop', $insert, false, true, Db::INSERT_IGNORE);
}
protected function getSelectedAssoShop($table)
{
if (!Shop::isFeatureActive()) {
return array();
}
$shops = Shop::getShops(true, null, true);
if (count($shops) == 1 && isset($shops[0])) {
return array($shops[0], 'shop');
}
$assos = array();
if (Tools::isSubmit('checkBoxShopAsso_' . $table)) {
foreach (Tools::getValue('checkBoxShopAsso_' . $table) as $id_shop => $value) {
$assos[] = (int) $id_shop;
}
} else if (Shop::getTotalShops(false) == 1) {
// if we do not have the checkBox multishop, we can have an admin with only one shop and being in multishop
$assos[] = (int) Shop::getContextShopID();
}
return $assos;
}
}

View File

@@ -0,0 +1,656 @@
<?php
/**
* 2024 Anvanto
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
*
* @author Anvanto <anvantoco@gmail.com>
* @copyright 2024 Anvanto
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once _PS_MODULE_DIR_ . 'an_homeproducts/classes/anHomeProductsBlocks.php';
require_once _PS_MODULE_DIR_ . 'an_homeproducts/classes/anHomeProductFiles.php';
class AdminAnhomeproductsBlocksController extends ModuleAdminController
{
protected $_module = null;
protected $position_identifier = 'position';
protected $_defaultOrderBy = 'position';
protected $_defaultOrderWay = 'ASC';
public function __construct()
{
$this->bootstrap = true;
$this->context = Context::getContext();
$this->table = 'an_homeproducts_blocks';
$this->identifier = 'id_block';
$this->className = 'anHomeProductsBlocks';
$this->lang = true;
$this->addRowAction('edit');
$this->addRowAction('delete');
$this->name = 'AdminAnhomeproductsBlocksController';
parent::__construct();
$this->fields_list = [
'id_block' => [
'title' => $this->trans('ID', [], 'Admin.Global'),
'width' => 25,
'search' => false,
],
'title' => [
'title' => $this->trans('Title', [], 'Admin.Global'),
'search' => false,
],
'type' => [
'title' => $this->trans('Type', [], 'Admin.Global'),
'search' => false,
],
'products_display' => [
'title' => $this->l('Products to display'),
'search' => false,
],
'position' => [
'title' => $this->trans('Position', [], 'Admin.Global'),
'search' => false,
'position' => true
],
'active' => [
'title' => $this->trans('Active', [], 'Admin.Global'),
'width' => 40,
'active' => 'update',
'align' => 'center',
'type' => 'bool',
'search' => false,
'orderby' => false
]
];
if (Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_ALL) {
$this->_where .= ' AND a.' . $this->identifier . ' IN (
SELECT sa.' . $this->identifier . '
FROM `' . _DB_PREFIX_ . $this->table . '_shop` sa
WHERE sa.id_shop IN (' . implode(', ', Shop::getContextListShopID()) . ')
)';
}
}
public function l($string, $class = null, $addslashes = false, $htmlentities = true)
{
return parent::l($string, pathinfo(__FILE__, PATHINFO_FILENAME), $addslashes, $htmlentities);
}
// public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
// {
// parent::getList($id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);
// foreach ($this->_list as &$list) {
// var_dump($list);
// }
// }
public function setMedia($isNewTheme = false)
{
parent::setMedia($isNewTheme);
$this->addJquery();
$this->js_files[] = _MODULE_DIR_ . 'an_homeproducts/views/js/back.js';
$this->js_files[] = _MODULE_DIR_ . 'an_homeproducts/views/js/anSearchProducts.js';
$this->css_files[_MODULE_DIR_ . 'an_homeproducts/views/css/back.css'] = 'all';
$this->css_files[_MODULE_DIR_ . 'an_homeproducts/views/css/anSearchProducts.css'] = 'all';
$this->js_files[] = _MODULE_DIR_ . 'an_homeproducts/views/js/Sortable.min.js';
$this->js_files[] = _MODULE_DIR_ . 'an_homeproducts/views/js/sorting.js';
}
public function renderList()
{
return parent::renderList() . $this->module->topPromo();
}
public function initToolBarTitle()
{
$this->toolbar_title[] = $this->l('Home Products: Products to display');
}
public function initHeader()
{
parent::initHeader();
$id_lang = $this->context->language->id;
$link = $this->context->link;
$tabs = &$this->context->smarty->tpl_vars['tabs']->value;
foreach ($tabs as &$tab0) {
if ($tab0['class_name'] == 'IMPROVE') {
foreach ($tab0['sub_tabs'] as &$tab1) {
if ($tab1['class_name'] == 'AdminParentModulesSf') {
foreach ($tab1['sub_tabs'] as &$tab2) {
if ($tab2['class_name'] == 'AdminAnhomeproductsBlocks') {
$sub_tabs = &$tab2['sub_tabs'];
$tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnhomeproductsBlocks'));
$tab['current'] = true;
$tab['href'] = $link->getAdminLink('AdminAnhomeproductsBlocks');
$tab['active'] = true;
$tab['name'] = $this->l('Products to display');
$sub_tabs[] = $tab;
$tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnhomeproductsBanners'));
$tab['current'] = false;
$tab['href'] = $link->getAdminLink('AdminAnhomeproductsBanners');
$tab['active'] = true;
$tab['name'] = $this->l('Banners');
$sub_tabs[] = $tab;
$tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnhomeproductsSettings'));
$tab['current'] = false;
$tab['href'] = $link->getAdminLink('AdminAnhomeproductsSettings');
$tab['active'] = true;
$tab['name'] = $this->trans('Settings', [], 'Admin.Global');
$sub_tabs[] = $tab;
break;
}
}
break;
}
}
break;
}
}
}
public function initContent()
{
$this->context->smarty->assign('current_tab_level', 3);
return parent::initContent();
}
public function initPageHeaderToolbar()
{
if (empty($this->display)) {
$this->page_header_toolbar_btn['add_field'] = array(
'href' => self::$currentIndex . '&addan_homeproducts_blocks&token=' . $this->token,
'desc' => $this->trans('Add new', array(), 'Admin.Actions'),
'icon' => 'process-icon-new',
);
}
parent::initPageHeaderToolbar();
}
public function renderForm()
{
$this->initToolbar();
if (!$this->loadObject(true)) {
return;
}
$this->fields_form = array(
'tinymce' => false,
'legend' => ['title' => $this->l('Home Products: Products to display')],
'input' => [],
'buttons' => [
[
'type' => 'submit',
'title' => $this->l('Save'),
'icon' => 'process-icon-save',
'class' => 'pull-right',
'name' => 'submit'.$this->table
],
[
'type' => 'submit',
'title' => $this->l('Save and stay'),
'icon' => 'process-icon-save',
'class' => 'pull-right',
'name' => 'submit'.$this->table.'AndStay'
],
],
);
$this->fields_form['input'][] = [
'type' => 'switch',
'name' => 'active',
'label' => $this->l('Active'),
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
]
],
];
$this->fields_form['input'][] = [
'type' => 'number',
'name' => 'products_display',
'label' => $this->l('Products to display'),
'col' => 1,
'min' => 1,
'max' => 50,
// 'desc' => $this->l('Define the number of products to be displayed in this block.'),
// 'class' => 'fixed-width-xs',
];
$this->fields_form['input'][] = [
'type' => 'radio',
'label' => $this->trans('Type', [], 'Admin.Global'),
'name' => 'type',
'default_value' => 'new-products',
'class' => 'an-hp-block-type',
'values' => [
[
'id' => 'new-products',
'value' => 'new-products',
'label' => $this->l('New arrivals')
],
[
'id' => 'best-sales',
'value' => 'best-sales',
'label' => $this->l('Best sellers'),
],
[
'id' => 'prices-drop',
'value' => 'prices-drop',
'label' => $this->l('Specials'),
],
// [
// 'id' => 'prices-drop-percentage',
// 'value' => 'prices-drop-percentage',
// 'label' => $this->l('Specials (only percentage discount)'),
// ],
[
'id' => 'category',
'value' => 'category',
'label' => $this->l('Category'),
],
[
'id' => 'categories',
'value' => 'categories',
'label' => $this->l('Categories (multiple)'),
],
[
'id' => 'products',
'value' => 'products',
'label' => $this->l('Products'),
],
],
];
$this->fields_form['input'][] = array(
'type' => 'anSearchProductsList',
'ignore' => true,
'name' => 'productIds[]',
'class' => 'js-an_productfields_products js-an_productfields-searchProducts-components',
'classSarchInput' => 'js-an_productfields-search-input',
'label' => '',
'searchProdutsController' => $this->context->link->getAdminLink('AdminAnhomeproductsAjax', true, [], ['ajax'=>1, 'action' =>'searchProducts']),
'form_group_class' => 'js-an_productfields-search-group'
);
$this->fields_form['input'][] = array(
'type' => 'text',
'anSearchProductsInput' => true,
'ignore' => true,
'label' => $this->l('Products'),
'name' => 'products_input',
'size' => 50,
'maxlength' => 50,
'col' => 4,
'placeholder' => $this->l('Search and add a product'),
'class' => 'js-an_productfields-search-input js-an_productfields-searchProducts-components anSearchProducts-input',
'form_group_class' => 'js-an_productfields-search-group'
);
$this->fields_value['productIds[]'] = anHomeProductsBlocks::getProducsByIdBlock(Tools::getValue('id_block'));
$this->fields_form['input'][] = [
'type' => 'select',
'label' => $this->module->l('Category'),
'name' => 'id_category',
'form_group_class' => 'js-hp-block-category',
'options' => [
'query' => array_merge(
[
['id_category' => '0', 'name' => '-'],
],
$tr = $this->getSimpleTreeCategories()
),
'id' => 'id_category',
'name' => 'name',
],
];
$this->fields_form['input'][] = [
'type' => 'switch',
'name' => 'show_sub_cat',
'form_group_class' => 'js-hp-block-show_sub_cat',
'label' => $this->l('Show sub categories'),
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
]
],
];
$this->fields_form['input'][] = [
'type' => 'categories',
'label' => $this->l('Categories'),
'name' => 'ids_categories',
'form_group_class' => 'js-hp-block-categories',
'tree' => [
'id' => 'id_root_category',
'use_checkbox' => true,
'selected_categories' => anHomeProductsBlocks::getBlockCategories(Tools::getValue('id_block'))
]
];
$this->fields_form['input'][] = [
'type' => 'switch',
'name' => 'show_sort',
'form_group_class' => 'js-hp-block-show_sort',
'label' => $this->l('Show sort'),
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
]
],
];
$this->fields_form['input'][] = [
'type' => 'html',
'name' => 'line1',
'html_content' => 'hr',
];
$this->fields_form['input'][] = [
'type' => 'text',
'name' => 'link',
'label' => $this->l('Link'),
'lang' => true,
];
$this->fields_form['input'][] = [
'type' => 'text',
'name' => 'title',
'label' => $this->l('Title'),
'required' => true,
'lang' => true,
];
$this->fields_form['input'][] = [
'type' => 'textarea',
'class' => 'autoload_rte',
'name' => 'text',
'label' => $this->l('Text'),
'lang' => true,
];
if (Shop::isFeatureActive()) {
$this->fields_form['input'][] = [
'required' => true,
'type' => 'shop',
'label' => $this->l('Shop association'),
'name' => 'checkBoxShopAsso',
];
}
return parent::renderForm();
}
public function getSimpleTreeCategories()
{
// getCategories
// getAllCategoriesName
// getChildren
// getNestedCategories
// Category::getRootCategory()->id
// $cat = new Category (Category::getRootCategory()->id, $this->context->language->id);
// $tr = $cat->recurseLiteCategTree();
$cats = Category::getAllCategoriesName(Category::getRootCategory()->id, $this->context->language->id);
return $cats;
}
public function processSave()
{
if (Tools::getIsset('title_' . $this->context->language->id)) {
if (Tools::getValue('title_' . $this->context->language->id) == '') {
$this->errors[] = $this->l('Please enter a Title');
}
}
if (!empty($this->errors)) {
$this->display = 'edit';
return false;
}
$object = parent::processSave();
if (isset($object->id) && $object->id) {
switch (Tools::getValue('type')){
case 'categories':
$this->updateCategories(Tools::getValue('ids_categories'), $object->id);
break;
case 'products':
$this->updateProducts(Tools::getValue('productIds'), $object->id);
break;
default:
$this->updateCategories(Tools::getValue('ids_categories'), $object->id);
}
}
$this->module->_clearCache('*');
anHomeProductsBlocks::exportJsonBlocks($this->module->filesManager);
if (Tools::getIsset('submit'.$this->table.'AndStay')) {
$this->redirect_after = $this->context->link->getAdminLink($this->controller_name).'&conf=4&updatean_homeproducts_blocks&token='.$this->token.'&id_block='.$object->id;
}
if ($object->type == 'best-sales' || $object->type == 'categories' || $object->type == 'products'){
$object->show_sort = 0;
$object->save();
}
return $object;
}
public function processDelete()
{
$object = parent::processDelete();
$this->module->_clearCache('*');
anHomeProductsBlocks::exportJsonBlocks($this->module->filesManager);
return $object;
}
protected function updateCategories($ids, $idBlock)
{
if (!$idBlock){
return false;
}
Db::getInstance(_PS_USE_SQL_SLAVE_)->Execute('DELETE FROM `'._DB_PREFIX_.'an_homeproducts_blocks_categories` WHERE `id_block`='.(int) $idBlock.' ');
if (!$ids || count($ids) == 0) {
$ids[] = 0;
}
$ids = array_unique($ids);
foreach ($ids as $id) {
$sql = 'INSERT INTO `'._DB_PREFIX_.'an_homeproducts_blocks_categories` (`id_block`, `id_category`)
VALUES ("'.(int) $idBlock.'", "'.(int) $id.'")';
Db::getInstance(_PS_USE_SQL_SLAVE_)->Execute($sql);
}
return true;
}
protected function updateProducts($ids, $idBlock)
{
if (!$idBlock){
return false;
}
Db::getInstance(_PS_USE_SQL_SLAVE_)->Execute('DELETE FROM `'._DB_PREFIX_.'an_homeproducts_blocks_products` WHERE `id_block`='.(int) $idBlock.' ');
if (!$ids || count($ids) == 0) {
$ids[] = 0;
}
$ids = array_unique($ids);
$position = 1;
foreach ($ids as $id) {
$sql = 'INSERT INTO `'._DB_PREFIX_.'an_homeproducts_blocks_products` (`id_block`, `id_product`, `position`)
VALUES ("'.(int) $idBlock.'", "'.(int) $id.'", "'.(int) $position.'")';
Db::getInstance(_PS_USE_SQL_SLAVE_)->Execute($sql);
$position++;
}
return true;
}
public function ajaxProcessUpdatePositions()
{
$status = false;
$position = 0;
$widget = (array)Tools::getValue('block');
foreach ($widget as $key => $item){
$ids = explode('_', $item);
$sql = 'UPDATE `' . _DB_PREFIX_ . $this->table .'` SET position="'.(int) $position.'" WHERE '.$this->identifier.'="'.(int) $ids['2'].'" ';
Db::getInstance(_PS_USE_SQL_SLAVE_)->execute($sql);
$position++;
}
if (count($widget) > 0){
$status = true;
}
anHomeProductsBlocks::exportJsonBlocks($this->module->filesManager);
$this->module->_clearCache('*');
return $this->setJsonResponse(array(
'success' => $status,
'message' => $this->l($status ? 'Blocks reordered successfully' : 'An error occurred')
));
}
protected function setJsonResponse($response)
{
header('Content-Type: application/json; charset=utf8');
print(json_encode($response));
exit;
}
protected function updateAssoShop($id_object)
{
if (!Shop::isFeatureActive()) {
return;
}
$assos_data = $this->getSelectedAssoShop($this->table, $id_object);
$exclude_ids = $assos_data;
foreach (Db::getInstance()->executeS('SELECT id_shop FROM ' . _DB_PREFIX_ . 'shop') as $row) {
if (!$this->context->employee->hasAuthOnShop($row['id_shop'])) {
$exclude_ids[] = $row['id_shop'];
}
}
Db::getInstance()->delete($this->table . '_shop', '`' . $this->identifier . '` = ' . (int) $id_object . ($exclude_ids ? ' AND id_shop NOT IN (' . implode(', ', $exclude_ids) . ')' : ''));
$insert = array();
foreach ($assos_data as $id_shop) {
$insert[] = array(
$this->identifier => $id_object,
'id_shop' => (int) $id_shop,
);
}
return Db::getInstance()->insert($this->table . '_shop', $insert, false, true, Db::INSERT_IGNORE);
}
protected function getSelectedAssoShop($table)
{
if (!Shop::isFeatureActive()) {
return array();
}
$shops = Shop::getShops(true, null, true);
if (count($shops) == 1 && isset($shops[0])) {
return array($shops[0], 'shop');
}
$assos = array();
if (Tools::isSubmit('checkBoxShopAsso_' . $table)) {
foreach (Tools::getValue('checkBoxShopAsso_' . $table) as $id_shop => $value) {
$assos[] = (int) $id_shop;
}
} else if (Shop::getTotalShops(false) == 1) {
// if we do not have the checkBox multishop, we can have an admin with only one shop and being in multishop
$assos[] = (int) Shop::getContextShopID();
}
return $assos;
}
}

View File

@@ -0,0 +1,347 @@
<?php
/**
* 2024 Anvanto
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
*
* @author Anvanto <anvantoco@gmail.com>
* @copyright 2024 Anvanto
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once _PS_MODULE_DIR_ . 'an_homeproducts/classes/anHomeProductsBlocks.php';
class AdminAnhomeproductsSettingsController extends ModuleAdminController
{
protected $_module = null;
public function __construct()
{
$this->bootstrap = true;
$this->display = 'view';
$this->name = 'AdminAnhomeproductsSettingsController';
parent::__construct();
}
public function l($string, $class = null, $addslashes = false, $htmlentities = true)
{
return parent::l($string, pathinfo(__FILE__, PATHINFO_FILENAME), $addslashes, $htmlentities);
}
public function setMedia($isNewTheme = false)
{
parent::setMedia($isNewTheme);
$this->addJquery();
$this->js_files[] = _MODULE_DIR_ . 'an_homeproducts/views/js/back_settings.js';
}
public function initToolBarTitle()
{
$this->toolbar_title[] = $this->module->l('Home Products: Settings');
}
public function initHeader()
{
parent::initHeader();
$id_lang = $this->context->language->id;
$link = $this->context->link;
$tabs = &$this->context->smarty->tpl_vars['tabs']->value;
foreach ($tabs as &$tab0) {
if ($tab0['class_name'] == 'IMPROVE') {
foreach ($tab0['sub_tabs'] as &$tab1) {
if ($tab1['class_name'] == 'AdminParentModulesSf') {
foreach ($tab1['sub_tabs'] as &$tab2) {
if ($tab2['class_name'] == 'AdminAnhomeproductsSettings') {
$sub_tabs = &$tab2['sub_tabs'];
$tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnhomeproductsBlocks'));
$tab['current'] = false;
$tab['href'] = $link->getAdminLink('AdminAnhomeproductsBlocks');
$tab['active'] = true;
$tab['name'] = $this->l('Products to display');
$sub_tabs[] = $tab;
$tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnhomeproductsBanners'));
$tab['current'] = false;
$tab['href'] = $link->getAdminLink('AdminAnhomeproductsBanners');
$tab['active'] = true;
$tab['name'] = $this->l('Banners');
$sub_tabs[] = $tab;
$tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnhomeproductsSettings'));
$tab['current'] = true;
$tab['href'] = $link->getAdminLink('AdminAnhomeproductsSettings');
$tab['active'] = true;
$tab['name'] = $this->trans('Settings', [], 'Admin.Global');
$sub_tabs[] = $tab;
break;
}
}
break;
}
}
break;
}
}
}
public function initContent()
{
$this->context->smarty->assign('current_tab_level', 3);
return parent::initContent();
}
/**
* Create the structure of your form.
*/
protected function getSettingsForm()
{
$form['form']['legend'] = [
'title' => $this->l('Settings'),
];
$form['form']['submit'] = [
'name' => 'save',
'title' => $this->l('Save'),
];
$form['form']['input'][] = [
'type' => 'switch',
'name' => an_homeproducts::PREFIX . 'use_cache',
'label' => $this->l('Use cache'),
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
]
],
'form_group_class' => 'hide',
];
$form['form']['input'][] = [
'type' => 'radio',
'label' => $this->l('Type view'),
'name' => an_homeproducts::PREFIX . 'view_type',
'values' => [
[
'id' => 'blocks',
'value' => 'blocks',
'label' => $this->l('Blocks')
],
[
'id' => 'tabs',
'value' => 'tabs',
'label' => $this->l('Tabs'),
],
],
];
$form['form']['input'][] = [
'type' => 'switch',
'name' => an_homeproducts::PREFIX . 'slider',
'label' => $this->l('Slider'),
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
]
],
];
$form['form']['input'][] = [
'type' => 'switch',
'name' => an_homeproducts::PREFIX . 'slider_nav',
'label' => $this->l('Slider Nav'),
'form_group_class' => 'js-hp-setting-slider-option',
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
]
],
];
$form['form']['input'][] = [
'type' => 'switch',
'name' => an_homeproducts::PREFIX . 'slider_dots',
'label' => $this->l('Slider Dots'),
'form_group_class' => 'js-hp-setting-slider-option',
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
]
],
];
$form['form']['input'][] = [
'type' => 'switch',
'name' => an_homeproducts::PREFIX . 'slider_loop',
'label' => $this->l('Slider Loop'),
'form_group_class' => 'js-hp-setting-slider-option',
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
]
],
];
$form['form']['input'][] = [
'type' => 'switch',
'name' => an_homeproducts::PREFIX . 'show_load_more',
'label' => $this->l('Show "Load more"'),
'desc' => $this->l('Not used for Categories (multiple) and Products'),
'form_group_class' => 'js-hp-setting-load_more',
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
]
],
];
$form['form']['input'][] = [
'type' => 'text',
'label' => $this->l('Title'),
'name' => an_homeproducts::PREFIX . 'title',
'lang' => true,
];
$form['form']['input'][] = [
'type' => 'textarea',
'class' => 'autoload_rte',
'name' => an_homeproducts::PREFIX . 'text',
'label' => $this->l('Text'),
'lang' => true,
'html' => true,
];
return $form;
}
public function renderView()
{
$languages = $this->context->controller->getLanguages();
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->name_controller = $this->name;
$helper->submit_action = $this->name;
$helper->currentIndex = $this->context->link->getAdminLink('AdminAnhomeproductsSettings', false);
$helper->token = Tools::getAdminTokenLite('AdminAnhomeproductsSettings');
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->tpl_vars = [
'uri' => $this->module->getPathUri(),
'languages' => $languages,
'id_language' => $this->context->language->id
];
$form = $this->getSettingsForm();
foreach ($form['form']['input'] as $input){
if (isset($input['lang']) && $input['lang']){
$value = [];
foreach ($languages as $language){
$value[$language['id_lang']] = Configuration::get($input['name'], $language['id_lang']);
}
$helper->tpl_vars['fields_value'][$input['name']] = $value;
} else {
$helper->tpl_vars['fields_value'][$input['name']] = Configuration::get($input['name']);
}
}
return $helper->generateForm([$form]);
}
public function postProcess()
{
$form = $this->getSettingsForm();
if (Tools::isSubmit($form['form']['submit']['name'])) {
$languages = Language::getLanguages(false);
foreach ($form['form']['input'] as $input){
$html = false;
if (isset($input['html']) && $input['html']){
$html = true;
}
if (isset($input['lang']) && $input['lang']){
$value = [];
foreach ($languages as $language){
$value[$language['id_lang']] = Tools::getValue($input['name'].'_' . $language['id_lang']);
}
Configuration::updateValue($input['name'], $value, $html);
} else {
Configuration::updateValue($input['name'], Tools::getValue($input['name']), $html);
}
}
$this->module->_clearCache('*');
$currentIndex = $this->context->link->getAdminLink('AdminAnhomeproductsSettings', false);
$token = Tools::getAdminTokenLite('AdminAnhomeproductsSettings');
Tools::redirectAdmin($currentIndex . '&token=' . $token . '&conf=4');
}
return true;
}
}