Files
interblue.pl/modules/customfeaturetab/controllers/admin/AdminCustomFeatureTabController.php
Jacek Pyziak 2a98067d9e Add Custom Feature Tab module with database integration and AJAX support
- Created main module file `customfeaturetab.php` to manage product tabs based on feature values.
- Implemented database installation and uninstallation methods to create necessary tables.
- Added admin controller files for handling redirects and admin functionalities.
- Introduced AJAX functionality in `admin.js` for dynamic feature value selection based on selected features.
- Included temporary query script for testing feature values.
- Added language support for the module with Polish translations.
- Created necessary view files and JavaScript files for module functionality.
- Added logo image for the module.
2026-02-23 23:24:48 +01:00

247 lines
8.7 KiB
PHP

<?php
/**
* Admin controller for Custom Feature Tab rules.
*
* @author Project-Pro <https://www.project-pro.pl>
* @copyright Project-Pro
* @license Proprietary - paid license
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once _PS_MODULE_DIR_ . 'customfeaturetab/classes/CustomFeatureTabRule.php';
class AdminCustomFeatureTabController extends ModuleAdminController
{
public function __construct()
{
$this->table = 'custom_feature_tab';
$this->className = 'CustomFeatureTabRule';
$this->identifier = 'id_custom_feature_tab';
$this->lang = true;
$this->bootstrap = true;
$this->addRowAction('edit');
$this->addRowAction('delete');
$this->position_identifier = 'position';
parent::__construct();
$this->bulk_actions = array(
'delete' => array(
'text' => $this->l('Delete selected'),
'confirm' => $this->l('Delete selected items?'),
'icon' => 'icon-trash',
),
);
$this->fields_list = array(
'id_custom_feature_tab' => array(
'title' => $this->l('ID'),
'align' => 'center',
'class' => 'fixed-width-xs',
),
'feature_name' => array(
'title' => $this->l('Cecha'),
'filter_key' => 'fl!name',
),
'feature_value_name' => array(
'title' => $this->l('Wartość cechy'),
'filter_key' => 'fvl!value',
),
'title' => array(
'title' => $this->l('Tytuł zakładki'),
'filter_key' => 'b!title',
),
'position' => array(
'title' => $this->l('Pozycja'),
'align' => 'center',
'class' => 'fixed-width-xs',
'position' => 'position',
),
'active' => array(
'title' => $this->l('Aktywna'),
'active' => 'status',
'type' => 'bool',
'align' => 'center',
'class' => 'fixed-width-sm',
),
);
}
/**
* Override getList to JOIN feature/feature_value names.
*/
public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
{
$this->_select = 'fl.`name` AS `feature_name`, fvl.`value` AS `feature_value_name`';
$this->_join = '
LEFT JOIN `' . _DB_PREFIX_ . 'feature_lang` fl
ON (a.`id_feature` = fl.`id_feature` AND fl.`id_lang` = ' . (int) $id_lang . ')
LEFT JOIN `' . _DB_PREFIX_ . 'feature_value_lang` fvl
ON (a.`id_feature_value` = fvl.`id_feature_value` AND fvl.`id_lang` = ' . (int) $id_lang . ')';
parent::getList($id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);
}
/**
* Build the add/edit form.
*/
public function renderForm()
{
$features = Feature::getFeatures($this->context->language->id);
$featureOptions = array();
foreach ($features as $feature) {
$featureOptions[] = array(
'id' => $feature['id_feature'],
'name' => $feature['name'],
);
}
// For edit mode, get values of the currently selected feature
$featureValueOptions = array();
$selectedFeatureValue = 0;
if ($this->object && $this->object->id) {
$selectedFeatureValue = (int) $this->object->id_feature_value;
$sql = 'SELECT fv.`id_feature_value`, fvl.`value`
FROM `' . _DB_PREFIX_ . 'feature_value` fv
LEFT JOIN `' . _DB_PREFIX_ . 'feature_value_lang` fvl
ON fv.`id_feature_value` = fvl.`id_feature_value`
AND fvl.`id_lang` = ' . (int) $this->context->language->id . '
WHERE fv.`id_feature` = ' . (int) $this->object->id_feature . '
ORDER BY fvl.`value` ASC';
$values = Db::getInstance()->executeS($sql);
if ($values) {
foreach ($values as $val) {
$featureValueOptions[] = array(
'id' => $val['id_feature_value'],
'name' => $val['value'],
);
}
}
}
$this->fields_form = array(
'legend' => array(
'title' => $this->l('Reguła karty cechy'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'type' => 'select',
'label' => $this->l('Cecha'),
'name' => 'id_feature',
'required' => true,
'options' => array(
'query' => $featureOptions,
'id' => 'id',
'name' => 'name',
),
),
array(
'type' => 'select',
'label' => $this->l('Wartość cechy'),
'name' => 'id_feature_value',
'required' => true,
'options' => array(
'query' => $featureValueOptions,
'id' => 'id',
'name' => 'name',
),
'desc' => $this->l('Wartości zostaną załadowane po wyborze cechy.'),
),
array(
'type' => 'text',
'label' => $this->l('Tytuł zakładki'),
'name' => 'title',
'lang' => true,
'required' => true,
'size' => 255,
),
array(
'type' => 'textarea',
'label' => $this->l('Treść'),
'name' => 'content',
'lang' => true,
'autoload_rte' => true,
'rows' => 10,
'cols' => 100,
),
array(
'type' => 'text',
'label' => $this->l('Pozycja'),
'name' => 'position',
'class' => 'fixed-width-xs',
),
array(
'type' => 'switch',
'label' => $this->l('Aktywna'),
'name' => 'active',
'is_bool' => true,
'values' => array(
array('id' => 'active_on', 'value' => 1, 'label' => $this->l('Tak')),
array('id' => 'active_off', 'value' => 0, 'label' => $this->l('Nie')),
),
),
),
'submit' => array(
'title' => $this->l('Zapisz'),
),
);
return parent::renderForm();
}
/**
* Add JS for AJAX dropdown and pass the selected value.
*/
public function setMedia($isNewTheme = false)
{
parent::setMedia($isNewTheme);
if (in_array($this->display, array('add', 'edit'))) {
$this->addJS(_PS_MODULE_DIR_ . 'customfeaturetab/views/js/admin.js');
// Pass the preselected feature value to JS for edit mode
if ($this->object && $this->object->id) {
Media::addJsDef(array(
'customfeaturetab_selected_value' => (int) $this->object->id_feature_value,
));
}
}
}
/**
* AJAX endpoint: get feature values for a given feature.
*/
public function ajaxProcessGetFeatureValues()
{
$idFeature = (int) Tools::getValue('id_feature');
$idLang = (int) $this->context->language->id;
// Include ALL values (predefined + custom) for the feature
$sql = 'SELECT fv.`id_feature_value`, fvl.`value`
FROM `' . _DB_PREFIX_ . 'feature_value` fv
LEFT JOIN `' . _DB_PREFIX_ . 'feature_value_lang` fvl
ON fv.`id_feature_value` = fvl.`id_feature_value`
AND fvl.`id_lang` = ' . (int) $idLang . '
WHERE fv.`id_feature` = ' . (int) $idFeature . '
ORDER BY fvl.`value` ASC';
$values = Db::getInstance()->executeS($sql);
$result = array();
if ($values) {
foreach ($values as $val) {
$result[] = array(
'id_feature_value' => $val['id_feature_value'],
'value' => $val['value'],
);
}
}
header('Content-Type: application/json');
die(json_encode($result));
}
}