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 ;
}
2017-06-09 17:55:17 +02:00
2017-06-08 12:23:50 +02:00
$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' );
self :: $currentIndex = self :: $currentIndex . '&id_configurator_opt_group=' . ( int ) $id . '&viewconfigurator_opt_group' ;
$this -> processFilter ();
2017-06-09 17:55:17 +02:00
//@todo : Re-Init all products with this option group on value change
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 ();
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' ),
'hint' => $this -> l ( " Nom du groupe de l'option à afficher. " ),
'name' => 'name' ,
'required' => true ,
'lang' => true
),
array (
'type' => 'select' ,
'label' => $this -> l ( 'Type' ),
'hint' => $this -> l ( " Type de l'option pour générer le formulaire de sélection sur la page produit. " ),
'name' => 'type' ,
'required' => true ,
2017-06-08 12:23:50 +02:00
'options' => array (
'id' => 'id' ,
'name' => 'name' ,
'query' => array (
array ( 'id' => 'text' , 'name' => 'Texte saisie' ),
array ( 'id' => 'date' , 'name' => 'Date' ),
array ( 'id' => 'select' , 'name' => 'Selection' ),
array ( 'id' => 'radio' , 'name' => 'Choix unique' ),
array ( 'id' => 'checkbox' , 'name' => 'Choix multiple' ),
),
),
),
2017-06-09 11:10:25 +02:00
array (
'type' => 'text' ,
'label' => $this -> l ( 'Reference' ),
'hint' => $this -> l ( 'Votre réference interne.' ),
'name' => 'reference' ,
'required' => true
),
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-09 11:10:25 +02:00
'hint' => $this -> l ( 'Nom de l\'option à afficher' ),
2017-06-08 12:23:50 +02:00
'name' => 'name' ,
'required' => true ,
'lang' => true
),
array (
'type' => 'text' ,
'label' => $this -> l ( 'Value' ),
2017-06-09 11:10:25 +02:00
'hint' => $this -> l ( 'Référence ou valeur de l\'option' ),
2017-06-08 12:23:50 +02:00
'name' => 'value' ,
'required' => true ,
),
array (
'type' => 'text' ,
'label' => $this -> l ( 'Price' ),
2017-06-09 11:10:25 +02:00
'hint' => $this -> l ( 'Prix génerique de l\'option' ),
2017-06-08 12:23:50 +02:00
'name' => 'price' ,
2017-06-09 11:10:25 +02:00
'value' => 0 ,
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' );
// 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 ()) {
$this -> errors [] = Tools :: displayError ( 'Failed to delete the attribute.' );
} 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-13 16:48:15 +02:00
2017-06-08 12:23:50 +02:00
$this -> processSave ( $this -> token );
}
} else {
parent :: postProcess ();
}
}
public function processSave ()
{
if ( $this -> display == 'add' || $this -> display == 'edit' ) {
$this -> identifier = 'id_configurator_opt_group' ;
}
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_configurator_opt' );
$id_configurator_opt_group = ( int ) Tools :: getValue ( 'id_configurator_opt_group' );
$positions = Tools :: getValue ( 'configurator_opt' );
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 ] . '\r\n' ;
} else {
echo '{"hasError" : true, "errors" : "Can not update the ' . ( int ) $id_configurator_opt . ' values to position ' . ( int ) $position . ' "}' ;
}
} else {
echo '{"hasError" : true, "errors" : "The (' . ( int ) $id_configurator_opt . ') values cannot be loaded"}' ;
}
break ;
}
}
}
}
}