2017-07-07 20:01:01 +02:00
< ? php
include_once dirname ( __FILE__ ) . '/../../classes/AdvLink2.php' ;
class AdminAdvBlockLink2Controller extends ModuleAdminController {
private $current_link ;
public function __construct () {
$this -> table = 'advblocklink2' ;
$this -> className = 'AdvLink2' ;
$this -> identifier = 'id_link' ;
$this -> lang = TRUE ;
$this -> deleted = FALSE ;
$this -> bootstrap = TRUE ;
$this -> position_identifier = 'id_link' ;
$this -> _defaultOrderBy = 'position' ;
parent :: __construct ();
$this -> actions = Tools :: getValue ( 'id_link' , 0 ) ? array ( 'edit' , 'delete' ) : array ( 'view' , 'edit' , 'delete' );
$this -> fields_list = array (
'id_link' => array (
'title' => 'ID' ,
'width' => 25
),
'title' => array (
'title' => $this -> module -> l ( 'Titre' ),
'width' => 45 ,
),
'url' => array (
'title' => $this -> module -> l ( 'Url' ),
'width' => 45 ,
),
'position' => array (
'title' => $this -> l ( 'Position' ),
'align' => 'center' ,
'position' => 'position' ,
'filter_key' => 'a!position'
)
);
$this -> current_link = isset ( $_GET [ 'deleteadvblocklink' ]) ? new AdvLink (( int )( Tools :: getValue ( 'id_link' , 0 ))) : new AdvLink2 (( int )( Tools :: getValue ( 'id_link' , Tools :: getValue ( 'id_parent' , 0 ))));
$this -> _where = ' AND a.`id_parent` =' . ( int ) $this -> current_link -> id ;
if ( Shop :: isFeatureActive () && Shop :: getContext () != Shop :: CONTEXT_ALL ){
$this -> _join .= 'JOIN `' . _DB_PREFIX_ . 'advblocklink2_shop` as ashop ON a.`id_link` = ashop.`id_link` AND ashop.`id_shop` IN (' . implode ( ', ' , Shop :: getContextListShopID ()) . ') ' ;
$this -> _group .= 'GROUP BY ashop.`id_link`' ;
}
}
public function initPageHeaderToolbar () {
parent :: initPageHeaderToolbar ();
if ( $this -> display != 'edit' && $this -> display != 'add' ) {
$this -> page_header_toolbar_btn [ 'new_link' ] = array (
'href' => self :: $currentIndex . '&id_parent=' . $this -> current_link -> id . '&addadvblocklink2&token=' . $this -> token ,
'desc' => $this -> l ( 'Ajouter un nouveau lien' , NULL , NULL , FALSE ),
'icon' => 'process-icon-new'
);
}
}
public function renderList () {
return parent :: renderList ();
}
public function initContent () {
if ( $this -> display == 'view' ) {
$this -> display = 'list' ;
}
parent :: initContent ();
}
public function renderForm () {
$dbLinks = array ();
$id = Tools :: getValue ( 'id_link' );
AdvLink2 :: getLinksTree ( $dbLinks , 0 , Context :: getContext () -> cookie -> id_lang , $id ? $id :- 1 );
$links [] = array (
'id' => '0' ,
'name' => $this -> l ( 'Aucun parent' )
);
foreach ( $dbLinks as $link ) {
$links [] = array (
" id " => $link [ 'id_link' ],
" name " => str_repeat ( '--' , $link [ 'level' ]) . ' ' . $link [ 'title' ]
);
}
$this -> fields_form = array (
'tinymce' => TRUE ,
'legend' => array (
'title' => $this -> className ,
),
'submit' => array (
'name' => 'submitadvblocklink' ,
'title' => $this -> l ( 'Save' ),
),
'input' => array (
array (
'type' => 'hidden' ,
'name' => 'id_link_parent'
),
array (
'type' => 'select' ,
'label' => $this -> l ( 'Lien parent' ),
'name' => 'id_parent' ,
'required' => FALSE ,
'options' => array (
'query' => $links ,
'id' => 'id' ,
'name' => 'name'
)
),
array (
'type' => 'text' ,
'label' => $this -> l ( 'Nom' ),
'name' => 'title' ,
'required' => TRUE ,
'lang' => TRUE ,
'size' => 114
),
array (
'type' => 'text' ,
'label' => $this -> l ( 'Lien' ),
'name' => 'url' ,
'lang' => TRUE ,
'required' => FALSE ,
'size' => 114
),
array (
'type' => 'switch' ,
'label' => $this -> l ( 'Activé' ),
'name' => 'active' ,
'required' => FALSE ,
'is_bool' => TRUE ,
'default' => 1 ,
'values' => array (
array (
'id' => 'active_on' ,
'value' => 1 ,
'label' => $this -> l ( 'Yes' )
),
array (
'id' => 'active_off' ,
'value' => 0 ,
'label' => $this -> l ( 'No' )
)
)
),
array (
'type' => 'switch' ,
'label' => $this -> l ( 'Lien externe ?' ),
'name' => 'external' ,
'required' => FALSE ,
'is_bool' => TRUE ,
'default' => 1 ,
'values' => array (
array (
'id' => 'external_on' ,
'value' => 1 ,
'label' => $this -> l ( 'Yes' )
),
array (
'id' => 'external_off' ,
'value' => 0 ,
'label' => $this -> l ( 'No' )
)
)
),
array (
'type' => 'shop' ,
'label' => $this -> l ( 'Shop' ),
'form_group_class' => 'fieldhide input_association' ,
2017-07-12 15:04:20 +02:00
'name' => 'checkBoxShopAsso_advblocklink' ,
2017-07-07 20:01:01 +02:00
)
)
);
$this -> fields_value = array (
'id_link_parent' => ( int ) Tools :: getValue ( 'id_link_parent' )
);
self :: $currentIndex .= '&id_link_parent=' . Tools :: getValue ( 'id_parent' , Tools :: getValue ( 'id_link_parent' ));
return parent :: renderForm ();
}
public function processAdd () {
$result = parent :: processAdd ();
$this -> redirect_after = self :: $currentIndex . '&conf=3&token=' . $this -> token . '&id_link=' . Tools :: getValue ( 'id_parent' );
return $result ;
}
public function processUpdate () {
$result = parent :: processUpdate ();
$this -> redirect_after = self :: $currentIndex . '&conf=4&token=' . $this -> token . '&id_link=' . Tools :: getValue ( 'id_parent' );
return $result ;
}
public function processDelete () {
$id_parent = $this -> current_link -> id_parent ;
$object = parent :: processDelete ();
if ( $object -> hasError ()) {
$this -> errors = [ Tools :: displayError ( $this -> l ( 'This link can\'t be deleted : it contains at least one sub link.' ))];
}
else {
$this -> redirect_after = self :: $currentIndex . '&conf=1&token=' . $this -> token . '&id_link=' . $id_parent ;
}
return $object ;
}
protected function copyFromPost ( & $object , $table ) {
parent :: copyFromPost ( $object , $table );
if ( Shop :: isFeatureActive ())
{
$object -> id_shop_list = array ();
2017-07-12 15:04:20 +02:00
foreach ( Tools :: getValue ( 'checkBoxShopAsso_advblocklink2' ) as $id_shop => $value )
2017-07-07 20:01:01 +02:00
$object -> id_shop_list [] = $id_shop ;
}
}
public function ajaxProcessUpdatePositions ()
{
$way = ( int )( Tools :: getValue ( 'way' ));
$id = ( int )( Tools :: getValue ( 'id' ));
$positions = Tools :: getValue ( 'link' );
$obj = 'advblocklink2' ;
if ( is_array ( $positions )){
foreach ( $positions as $position => $value )
{
$pos = explode ( '_' , $value );
if ( isset ( $pos [ 2 ]) && ( int ) $pos [ 2 ] === $id )
{
$menu_obj = new AdvLink2 (( int ) $pos [ 2 ]);
if ( Validate :: isLoadedObject ( $menu_obj ))
if ( isset ( $position ) && $menu_obj -> updatePosition ( $way , $position ))
{
echo 'ok position ' . ( int ) $position . ' for ' . $obj . ' ' . ( int ) $pos [ 2 ] . " \r \n " ;
}
else
echo '{"hasError" : true, "errors" : "Can not update ' . $obj . ' ' . ( int ) $id . ' to position ' . ( int ) $position . ' "}' ;
else
echo '{"hasError" : true, "errors" : "This ' . $obj . ' (' . ( int ) $id . ') cannot be loaded"}' ;
break ;
}
}
}
}
}