chocolatdemariage/www/modules/antadisconfigurator/controllers/admin/AdminAntadisConfiguratorController.php
2017-08-22 17:33:26 +02:00

578 lines
23 KiB
PHP

<?php
class AdminAntadisConfiguratorController extends ModuleAdminController
{
public function __construct()
{
$this->table = 'configurator_opt_group';
$this->className = 'ConfiguratorOptGroup';
$this->lang = true;
// Set fields list
$this->fields_list = array(
'id_configurator_opt_group' => array(
'title' => $this->l('ID'),
'align' => 'center',
'width' => 25
),
'name' => array(
'title' => $this->l('Nom'),
),
'type' => array(
'title' => $this->l('Type'),
),
'reference' => array(
'title' => $this->l('Reference'),
),
);
// Enable bootstrap
$this->bootstrap = true;
// Call of the parent constructor method
parent::__construct();
}
/**
* AdminController::renderList() override
* @see AdminController::renderList()
*/
public function renderList()
{
$this->addRowAction('view');
$this->addRowAction('edit');
$this->addRowAction('delete');
return parent::renderList();
}
public function renderView()
{
// Afficher la liste des valeurs de l'option
if (($id = Tools::getValue('id_configurator_opt_group'))) {
$this->table = 'configurator_opt';
$this->className = 'ConfiguratorOpt';
$this->identifier = 'id_configurator_opt';
$this->position_identifier = 'id_configurator_opt';
$this->list_id = 'values';
$this->lang = true;
$this->context->smarty->assign(array(
'current' => self::$currentIndex.'&id_configurator_opt_group='.(int)$id.'&viewconfigurator_opt_group'
));
if (!Validate::isLoadedObject($obj = new ConfiguratorOptGroup((int)$id))) {
$this->errors[] = Tools::displayError('An error occurred while updating the status for an object.').' <b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
return;
}
$this->fields_list = array(
'id_configurator_opt' => array(
'title' => $this->l('ID'),
'align' => 'center',
'class' => 'fixed-width-xs'
),
'name' => array(
'title' => $this->l('Name'),
'width' => 'auto',
),
'price' => array(
'title' => $this->l('Price'),
'type' => 'price',
),
);
// Add position to order on fields type
switch($obj->type) {
case 'radio':
case 'checkbox':
case 'select':
$this->fields_list['position'] = array(
'title' => $this->l('Position'),
'filter_key' => 'a!position',
'position' => 'position',
'class' => 'fixed-width-md'
);
break;
}
$this->_where = 'AND a.`id_configurator_opt_group` = '.(int)$id;
$this->_orderBy = 'position';
// Restriction
$this->getList($this->context->language->id);
switch($obj->type) {
case 'text':
case 'date':
if (count($this->_list) >= 1) {
unset($this->toolbar_btn['new']);
}
break;
}
// Row action
$this->addRowAction('edit');
$this->addRowAction('delete');
self::$currentIndex = self::$currentIndex.'&id_configurator_opt_group='.(int)$id.'&viewconfigurator_opt_group';
$this->processFilter();
return parent::renderList();
}
}
/**
* AdminController::init() override
* @see AdminController::init()
*/
public function init()
{
if (Tools::isSubmit('updateconfigurator_opt')) {
$this->display = 'editValues';
} elseif (Tools::isSubmit('submitAddconfigurator_opt')) {
$this->display = 'editValues';
} elseif (Tools::isSubmit('submitAddconfigurator_opt_group')) {
$this->display = 'add';
}
parent::init();
}
public function initContent()
{
$this->initToolbar();
$this->initPageHeaderToolbar();
if ($this->display == 'edit' || $this->display == 'add') {
if (!($this->object = $this->loadObject(true))) {
return;
}
$this->content .= $this->renderForm();
} elseif ($this->display == 'editValues') {
if (!$this->object = new ConfiguratorOpt((int)Tools::getValue('id_configurator_opt'))) {
return;
}
$this->content .= $this->renderFormOpt();
} elseif ($this->display != 'view' && !$this->ajax) {
$this->content .= $this->renderList();
//$this->content .= $this->renderOptions();
} elseif ($this->display == 'view' && !$this->ajax) {
$this->content = $this->renderView();
}
$this->context->smarty->assign(array(
'table' => $this->table,
'current' => self::$currentIndex,
'token' => $this->token,
'content' => $this->content,
'url_post' => self::$currentIndex.'&token='.$this->token,
'show_page_header_toolbar' => $this->show_page_header_toolbar,
'page_header_toolbar_title' => $this->page_header_toolbar_title,
'page_header_toolbar_btn' => $this->page_header_toolbar_btn
));
}
public function initPageHeaderToolbar()
{
if (empty($this->display)) {
$this->page_header_toolbar_btn['new_configurator_opt_group'] = array(
'href' => self::$currentIndex.'&addconfigurator_opt_group&token='.$this->token,
'desc' => $this->l('Add New Option', null, null, false),
'icon' => 'process-icon-new'
);
}
parent::initPageHeaderToolbar();
}
public function initToolbar()
{
switch ($this->display) {
case 'add':
case 'edit':
case 'editValues':
// Default save button - action dynamically handled in javascript
$this->toolbar_btn['save'] = array(
'href' => '#',
'desc' => $this->l('Save')
);
$this->toolbar_btn['back'] = array(
'href' => self::$currentIndex.'&token='.$this->token,
'desc' => $this->l('Back to list', null, null, false)
);
break;
break;
case 'view':
$this->toolbar_btn['new'] = array(
'href' => self::$currentIndex.'&updateconfigurator_opt&id_configurator_opt_group='.(int)Tools::getValue('id_configurator_opt_group').'&token='.$this->token,
'desc' => $this->l('Add New Value', null, null, false),
'class' => 'toolbar-new'
);
$this->toolbar_btn['back'] = array(
'href' => self::$currentIndex.'&token='.$this->token,
'desc' => $this->l('Back to list', null, null, false)
);
break;
default: // list
$this->toolbar_btn['new'] = array(
'href' => self::$currentIndex.'&add'.$this->table.'&token='.$this->token,
'desc' => $this->l('Add New Option', null, null, false)
);
break;
}
}
public function initProcess()
{
$this->setTypeOpt();
if (Tools::getIsset('viewconfigurator_opt_group')) {
$this->list_id = 'values';
if (isset($_POST['submitReset'.$this->list_id])) {
$this->processResetFilters();
}
} else {
$this->list_id = 'group';
}
parent::initProcess();
if ($this->table == 'configurator_opt') {
$this->display = 'editValues';
$this->id_configurator_opt = (int)Tools::getValue('id_configurator_opt');
}
}
protected function setTypeOpt()
{
if (Tools::isSubmit('updateconfigurator_opt') || Tools::isSubmit('deleteconfigurator_opt') ||
Tools::isSubmit('submitAddconfigurator_opt')) {
$this->table = 'configurator_opt';
$this->className = 'ConfiguratorOpt';
$this->identifier = 'id_configurator_opt';
if ($this->display == 'edit') {
$this->display = 'editValues';
}
}
}
/**
* Form option
* {@inheritDoc}
* @see AdminControllerCore::renderForm()
*/
public function renderForm()
{
$this->table = 'configurator_opt_group';
$this->identifier = 'id_configurator_opt_group';
$this->show_form_cancel_button = true;
$this->fields_form = array(
'legend' => array('title' => $this->l('Add / Edit')),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Name'),
'desc' => $this->l("Nom du groupe de l'option à afficher."),
'name' => 'name',
'empty_message' => $this->l('Le champs nom ne peut pas être vide.'),
'required' => true,
'lang' => true
),
array(
'type' => 'text',
'label' => $this->l('Reference'),
'desc' => $this->l("Votre réference interne (en majuscule, sans espace ou autres caractères spéciaux). Utiliser dans l'administration et pour l'affichage"),
'name' => 'reference',
'empty_message' => $this->l('Le champs reference ne peut pas être vide.'),
'required' => true
),
array(
'type' => 'select',
'label' => $this->l('Type'),
'desc' => $this->l("Type de l'option pour générer le formulaire de sélection sur la page produit."),
'name' => 'type',
'required' => true,
'options' => array(
'id' => 'id',
'name' => 'name',
'query' => array(
array('id' => 'text', 'name' => 'Zone saisie une ligne'),
array('id' => 'textarea', 'name' => 'Zone de texte'),
array('id' => 'date', 'name' => 'Date'),
array('id' => 'select', 'name' => 'Selection'),
array('id' => 'radio', 'name' => 'Choix unique'),
array('id' => 'checkbox', 'name' => 'Choix multiple'),
array('id' => 'file', 'name' => 'Fichier'),
array('id' => 'hidden', 'name' => 'Champ caché'),
array('id' => 'quantities', 'name' => 'Quantités'),
array('id' => 'label', 'name' => 'Libellé / Séparateur'),
),
),
),
array(
'type' => 'textarea',
'autoload_rte' => true,
'label' => $this->l('Description'),
'desc' => $this->l("Description pour l'affichage"),
'name' => 'description',
'required' => false,
'lang' => true,
),
),
'submit' => array('title' => $this->l('Save'))
);
return parent::renderForm();
}
/**
* Form option value
* @return string
*/
public function renderFormOpt()
{
$this->table = 'configurator_opt';
$this->identifier = 'id_configurator_opt';
$this->show_form_cancel_button = true;
$this->fields_form = array(
'legend' => array('title' => $this->l('Add / Edit')),
'input' => array(
array(
'type' => 'hidden',
'name' => 'id_configurator_opt_group',
'required' => true,
),
array(
'type' => 'text',
'label' => $this->l('Name'),
'desc' => $this->l("Nom de l'option à afficher si nécessaire."),
'name' => 'name',
'empty_message' => $this->l('Le champs nom ne peut pas être vide.'),
'required' => true,
'lang' => true
),
array(
'type' => 'text',
'label' => $this->l('Price'),
'desc' => $this->l("Prix génerique de l'option"),
'name' => 'price',
'empty_message' => $this->l('Le champs prix ne peut pas être vide.'),
'required' => true
),
),
'submit' => array(
'title' => $this->l('Save')
),
);
$this->fields_value['id_configurator_opt_group'] = (int)Tools::getValue('id_configurator_opt_group');
$this->fields_value['price'] = 0;
// Override var of Controller
$this->table = 'configurator_opt';
$this->className = 'ConfiguratorOpt';
$this->identifier = 'id_configurator_opt';
$this->lang = true;
self::$currentIndex = self::$currentIndex.'&id_configurator_opt_group='.$this->fields_value['id_configurator_opt_group'].'&viewconfigurator_opt_group';
// Create object Attribute
if (!$obj = new ConfiguratorOpt((int)Tools::getValue($this->identifier))) {
return;
}
return parent::renderForm();
}
public function postProcess()
{
if (!Tools::getValue($this->identifier) && Tools::getValue('id_configurator_opt') && !Tools::getValue('attributeOrderby')) {
// Override var of Controller
$this->table = 'configurator_opt';
$this->className = 'ConfiguratorOpt';
$this->identifier = 'id_configurator_opt';
}
// If Opt, load object
if (Tools::getValue('updateconfigurator_opt') || Tools::isSubmit('deleteconfigurator_opt') || Tools::isSubmit('submitAddconfigurator_opt')) {
if (!$object = new ConfiguratorOpt((int)Tools::getValue($this->identifier))) {
$this->errors[] = Tools::displayError('An error occurred while updating the status for an object.').' <b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
}
if (Tools::getValue('position') !== false && Tools::getValue('id_configurator_opt')) {
$_POST['id_configurator_opt_group'] = $object->id_configurator_opt_group;
if (!$object->updatePosition((int)Tools::getValue('way'), (int)Tools::getValue('position'))) {
$this->errors[] = Tools::displayError('Failed to update the position.');
} else {
Tools::redirectAdmin(self::$currentIndex.'&conf=5&token='.Tools::getAdminTokenLite('AdminAntadisConfigurator').'#details_details_'.$object->id_configurator_opt_group);
}
} elseif (Tools::isSubmit('deleteconfigurator_opt') && Tools::getValue('id_configurator_opt')) {
if (!$object->delete()) {
$this->errors[] = Tools::displayError('Failed to delete the values.');
} else {
Tools::redirectAdmin(self::$currentIndex.'&conf=1&token='.Tools::getAdminTokenLite('AdminAntadisConfigurator'));
}
} elseif (Tools::isSubmit('submitAddconfigurator_opt')) {
Hook::exec('actionObjectConfiguratorOptAddBefore');
$this->action = 'save';
$id_configurator_opt = (int)Tools::getValue('id_configurator_opt');
// Adding last position to the attribute if not exist
if ($id_configurator_opt <= 0) {
$sql = 'SELECT `position`+1
FROM `'._DB_PREFIX_.'configurator_opt`
WHERE `id_configurator_opt_group` = '.(int)Tools::getValue('id_configurator_opt_group').'
ORDER BY position DESC';
// set the position of the new group attribute in $_POST for postProcess() method
$_POST['position'] = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
}
$_POST['id_parent'] = 0;
$this->processSave($this->token);
}
} else {
// Validation
parent::postProcess();
}
}
public function processSave()
{
if ($this->display == 'add' || $this->display == 'edit') {
$this->identifier = 'id_configurator_opt_group';
}
if ($this->display == 'editValues') {
$id_configurator_opt_group = (int)Tools::getValue('id_configurator_opt_group');
$this->redirect_after = self::$currentIndex.'&id_configurator_opt_group='.(int)$id_configurator_opt_group.'&viewconfigurator_opt_group&token='.Tools::getAdminTokenLite('AdminAntadisConfigurator');
}
if (!$this->id_object) {
return $this->processAdd();
} else {
return $this->processUpdate();
}
}
public function processDelete()
{
Tools::dieObject("@todo : Gérer la suppression avec les sous éléments");
}
public function processPosition()
{
if (Tools::getIsset('viewconfigurator_opt_group')) {
$object = new ConfiguratorOpt((int)Tools::getValue('id_configurator_opt'));
self::$currentIndex = self::$currentIndex.'&viewconfigurator_opt_group';
}
if (!Validate::isLoadedObject($object)) {
$this->errors[] = Tools::displayError('An error occurred while updating the status for an object.').
' <b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
} elseif (!$object->updatePosition((int)Tools::getValue('way'), (int)Tools::getValue('position'))) {
$this->errors[] = Tools::displayError('Failed to update the position.');
} else {
$id_identifier_str = ($id_identifier = (int)Tools::getValue($this->identifier)) ? '&'.$this->identifier.'='.$id_identifier : '';
$redirect = self::$currentIndex.'&'.$this->table.'Orderby=position&'.$this->table.'Orderway=asc&conf=5'.$id_identifier_str.'&token='.$this->token;
$this->redirect_after = $redirect;
}
return $object;
}
/**
* Modify position
*/
public function ajaxProcessUpdatePositions()
{
$way = (int)Tools::getValue('way');
$id_configurator_opt = (int)Tools::getValue('id');
$positions = Tools::getValue('configurator_opt');
$return = array(
'hasError' => true,
'errors' => '"Can not update values to position"',
);
if (is_array($positions)) {
foreach ($positions as $position => $value) {
$pos = explode('_', $value);
if ((isset($pos[1]) && isset($pos[2])) && (int)$pos[2] === $id_configurator_opt) {
if ($opt = new ConfiguratorOpt((int)$pos[2])) {
if (isset($position) && $opt->updatePosition($way, $position)) {
echo 'ok position '.(int)$position.' for values '.(int)$pos[2]; exit;
} else {
$return = array(
'hasError' => true,
'errors' => '"Can not update the '.(int)$id_configurator_opt.' values to position '.(int)$position.'"',
);
}
} else {
$return = array(
'hasError' => true,
'errors' => '"The ('.(int)$id_configurator_opt.') values cannot be loaded"',
);
}
break;
}
}
}
echo Tools::jsonEncode($return);
}
public function ajaxProcessCopyOptions()
{
$id_product_from = Tools::getValue('id_product_from');
$id_product_to = Tools::getValue('id_product_to');
if (empty($id_product_from) || empty($id_product_to)) {
$return = array(
'hasError' => true,
'errors' => 'ID product cannot be null',
);
} else {
$result1 = Db::getInstance()->execute(
'INSERT INTO `'._DB_PREFIX_.'product_configurator_opt_group` (`id_product`, `id_configurator_opt_group`, `required`, `visibility`, `position`)
SELECT '.(int)$id_product_to.' AS `id_product`, `id_configurator_opt_group`, `required`, `visibility`, `position`
FROM `'._DB_PREFIX_.'product_configurator_opt_group` WHERE `id_product` = '.(int)$id_product_from);
$groupSql = 'SELECT `id_product_configurator_opt_group`, `id_configurator_opt_group`
FROM `'._DB_PREFIX_.'product_configurator_opt_group` WHERE `id_product` = '.(int)$id_product_to;
$groupResult = Db::getInstance()->executeS($groupSql);
foreach ($groupResult as $item) {
$impactSql = 'INSERT INTO `'._DB_PREFIX_.'product_configurator_opt_impact` (`id_product`, `id_product_configurator_opt_group`, `id_configurator_opt`, `price`)
SELECT '.(int)$id_product_to.' AS `id_product`, '.$item['id_product_configurator_opt_group'].' AS `id_product_configurator_opt_group`, `id_configurator_opt`, `price`
FROM `'._DB_PREFIX_.'configurator_opt` WHERE `id_configurator_opt_group`='.(int)$item['id_configurator_opt_group'];
$result2 = Db::getInstance()->execute($impactSql);
if(!$result2) {
break;
}
}
if (!$result1 || !$result2) {
$return = array(
'hasError' => true,
'errors' => 'Error when copying options',
);
} else {
$return = array(
'hasError' => false,
'errors' => '',
);
}
}
echo Tools::jsonEncode($return);
}
}