2017-06-08 12:23:50 +02:00
< ? 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 (
2017-06-09 11:10:25 +02:00
'title' => $this -> l ( 'Nom' ),
2017-06-08 12:23:50 +02:00
),
'type' => array (
2017-06-09 11:10:25 +02:00
'title' => $this -> l ( 'Type' ),
2017-06-08 12:23:50 +02:00
),
'reference' => array (
2017-06-09 11:10:25 +02:00
'title' => $this -> l ( 'Reference' ),
2017-06-08 12:23:50 +02:00
),
);
// 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' ,
),
);
2017-06-09 17:55:17 +02:00
// Add position to order on fields type
2017-06-08 12:23:50 +02:00
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'
2017-06-09 17:55:17 +02:00
);
2017-06-08 12:23:50 +02:00
break ;
}
$this -> _where = 'AND a.`id_configurator_opt_group` = ' . ( int ) $id ;
$this -> _orderBy = 'position' ;
2017-06-09 17:55:17 +02:00
// 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
2017-06-08 12:23:50 +02:00
$this -> addRowAction ( 'edit' );
$this -> addRowAction ( 'delete' );
2017-06-14 15:14:07 +02:00
2017-06-08 12:23:50 +02:00
self :: $currentIndex = self :: $currentIndex . '&id_configurator_opt_group=' . ( int ) $id . '&viewconfigurator_opt_group' ;
$this -> processFilter ();
2017-06-09 17:55:17 +02:00
2017-06-08 12:23:50 +02:00
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 ();
2017-06-27 11:26:08 +02:00
2017-06-08 12:23:50 +02:00
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 (
2017-06-09 11:10:25 +02:00
array (
'type' => 'text' ,
'label' => $this -> l ( 'Name' ),
2017-06-28 16:50:16 +02:00
'desc' => $this -> l ( " Nom du groupe de l'option à afficher. " ),
2017-06-09 11:10:25 +02:00
'name' => 'name' ,
2017-06-28 17:52:28 +02:00
'empty_message' => $this -> l ( 'Le champs nom ne peut pas être vide.' ),
2017-06-09 11:10:25 +02:00
'required' => true ,
'lang' => true
),
2017-07-10 12:03:22 +02:00
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
),
2017-06-09 11:10:25 +02:00
array (
'type' => 'select' ,
'label' => $this -> l ( 'Type' ),
2017-06-28 16:50:16 +02:00
'desc' => $this -> l ( " Type de l'option pour générer le formulaire de sélection sur la page produit. " ),
2017-06-09 11:10:25 +02:00
'name' => 'type' ,
'required' => true ,
2017-06-08 12:23:50 +02:00
'options' => array (
'id' => 'id' ,
'name' => 'name' ,
'query' => array (
2017-06-23 17:07:55 +02:00
array ( 'id' => 'text' , 'name' => 'Zone saisie une ligne' ),
array ( 'id' => 'textarea' , 'name' => 'Zone de texte' ),
2017-06-08 12:23:50 +02:00
array ( 'id' => 'date' , 'name' => 'Date' ),
array ( 'id' => 'select' , 'name' => 'Selection' ),
array ( 'id' => 'radio' , 'name' => 'Choix unique' ),
array ( 'id' => 'checkbox' , 'name' => 'Choix multiple' ),
2017-06-23 17:07:55 +02:00
array ( 'id' => 'file' , 'name' => 'Fichier' ),
2017-06-29 16:11:40 +02:00
array ( 'id' => 'hidden' , 'name' => 'Champ caché' ),
2017-07-10 12:03:22 +02:00
array ( 'id' => 'quantities' , 'name' => 'Quantités' ),
2017-06-08 12:23:50 +02:00
),
),
),
2017-06-09 11:10:25 +02:00
array (
2017-07-10 12:03:22 +02:00
'type' => 'textarea' ,
'label' => $this -> l ( 'Description' ),
'desc' => $this -> l ( " Description pour l'affichage " ),
'name' => 'description' ,
'required' => false ,
'lang' => true ,
2017-06-09 11:10:25 +02:00
),
2017-06-08 12:23:50 +02:00
),
'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' ),
2017-06-28 17:52:28 +02:00
'desc' => $this -> l ( " Nom de l'option à afficher si nécessaire. " ),
2017-06-08 12:23:50 +02:00
'name' => 'name' ,
2017-06-28 17:52:28 +02:00
'empty_message' => $this -> l ( 'Le champs nom ne peut pas être vide.' ),
2017-06-08 12:23:50 +02:00
'required' => true ,
'lang' => true
),
array (
'type' => 'text' ,
'label' => $this -> l ( 'Price' ),
2017-06-28 16:50:16 +02:00
'desc' => $this -> l ( " Prix génerique de l'option " ),
2017-06-08 12:23:50 +02:00
'name' => 'price' ,
2017-06-28 17:52:28 +02:00
'empty_message' => $this -> l ( 'Le champs prix ne peut pas être vide.' ),
2017-06-08 12:23:50 +02:00
'required' => true
),
),
'submit' => array (
'title' => $this -> l ( 'Save' )
),
);
$this -> fields_value [ 'id_configurator_opt_group' ] = ( int ) Tools :: getValue ( 'id_configurator_opt_group' );
2017-06-15 13:00:11 +02:00
$this -> fields_value [ 'price' ] = 0 ;
2017-06-08 12:23:50 +02:00
// Override var of Controller
$this -> table = 'configurator_opt' ;
$this -> className = 'ConfiguratorOpt' ;
$this -> identifier = 'id_configurator_opt' ;
$this -> lang = true ;
2017-06-13 16:48:15 +02:00
self :: $currentIndex = self :: $currentIndex . '&id_configurator_opt_group=' . $this -> fields_value [ 'id_configurator_opt_group' ] . '&viewconfigurator_opt_group' ;
2017-06-08 12:23:50 +02:00
// 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 ()) {
2017-06-19 12:22:54 +02:00
$this -> errors [] = Tools :: displayError ( 'Failed to delete the values.' );
2017-06-08 12:23:50 +02:00
} 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 ;
2017-06-27 11:26:08 +02:00
2017-06-08 12:23:50 +02:00
$this -> processSave ( $this -> token );
}
} else {
2017-06-28 16:50:16 +02:00
// Validation
2017-06-08 12:23:50 +02:00
parent :: postProcess ();
}
}
public function processSave ()
{
if ( $this -> display == 'add' || $this -> display == 'edit' ) {
$this -> identifier = 'id_configurator_opt_group' ;
}
2017-06-27 11:26:08 +02:00
if ( $this -> display == 'editValues' ) {
$id_configurator_opt_group = ( int ) Tools :: getValue ( 'id_configurator_opt_group' );
2017-06-29 15:48:06 +02:00
$this -> redirect_after = self :: $currentIndex . '&id_configurator_opt_group=' . ( int ) $id_configurator_opt_group . '&viewconfigurator_opt_group&token=' . Tools :: getAdminTokenLite ( 'AdminAntadisConfigurator' );
2017-06-27 11:26:08 +02:00
}
2017-06-08 12:23:50 +02:00
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' );
2017-06-14 15:14:07 +02:00
$id_configurator_opt = ( int ) Tools :: getValue ( 'id' );
2017-06-08 12:23:50 +02:00
$positions = Tools :: getValue ( 'configurator_opt' );
2017-06-14 15:14:07 +02:00
$return = array (
'hasError' => true ,
'errors' => '"Can not update values to position"' ,
);
2017-06-08 12:23:50 +02:00
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 )) {
2017-06-14 15:14:07 +02:00
echo 'ok position ' . ( int ) $position . ' for values ' . ( int ) $pos [ 2 ]; exit ;
2017-06-08 12:23:50 +02:00
} else {
2017-06-14 15:14:07 +02:00
$return = array (
'hasError' => true ,
'errors' => '"Can not update the ' . ( int ) $id_configurator_opt . ' values to position ' . ( int ) $position . '"' ,
);
2017-06-08 12:23:50 +02:00
}
} else {
2017-06-14 15:14:07 +02:00
$return = array (
'hasError' => true ,
'errors' => '"The (' . ( int ) $id_configurator_opt . ') values cannot be loaded"' ,
);
2017-06-08 12:23:50 +02:00
}
break ;
}
}
}
2017-06-14 15:14:07 +02:00
echo Tools :: jsonEncode ( $return );
2017-06-08 12:23:50 +02:00
}
2017-06-28 16:50:16 +02:00
public function ajaxProcessCopyOptions ()
{
$id_product_from = Tools :: getValue ( 'id_product_from' );
$id_product_to = Tools :: getValue ( 'id_product_to' );
echo Tools :: jsonEncode ( array (
'hasError' => true ,
'errors' => 'ID product cannot be null' ,
));
if ( empty ( $id_product_from ) || empty ( $id_product_to )) {
$return = array (
'hasError' => true ,
'errors' => 'ID product cannot be null' ,
);
} else {
$result1 = Db :: getInstance () -> execute (
2017-06-30 15:08:11 +02:00
'INSERT INTO `' . _DB_PREFIX_ . ' product_configurator_opt_group ` (` id_product `, ` id_configurator_opt_group `, ` required `, ` position ` )
2017-07-20 14:28:43 +02:00
SELECT '.(int)$id_product_to.' AS `id_product` , `id_configurator_opt_group` , `required` , `position`
FROM `'._DB_PREFIX_.'product_configurator_opt_group` WHERE `id_product` = ' . ( int ) $id_product_from );
2017-06-28 16:50:16 +02:00
$result2 = Db :: getInstance () -> execute (
2017-07-20 14:28:43 +02:00
'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` , `id_product_configurator_opt_group` , `id_configurator_opt` , `price`
FROM `'._DB_PREFIX_.'product_configurator_opt_impact` WHERE `id_product` = ' . ( int ) $id_product_from );
2017-06-28 16:50:16 +02:00
if ( ! $result1 || ! $result2 ) {
$return = array (
'hasError' => true ,
'errors' => 'Error when copying options' ,
);
} else {
$return = array (
'hasError' => false ,
'errors' => '' ,
);
}
}
echo Tools :: jsonEncode ( $return );
}
2017-06-08 12:23:50 +02:00
}