2015-09-23 16:57:43 +02:00
< ? php
2016-06-02 12:35:53 +02:00
class AdvMenuLink extends ObjectModel
{
const ID_CMS = 337 ;
const ID_BOUTIQUE = 9 ;
2015-09-23 16:57:43 +02:00
public $id_link ;
public $id_parent ;
public $text ;
public $title ;
public $url ;
public $classbloc ;
public $position ;
public static $definition = array (
'table' => 'advmenu' ,
'primary' => 'id_link' ,
'multilang' => TRUE ,
'fields' => array (
'id_link' => array ( 'type' => self :: TYPE_INT , 'validate' => 'isInt' ),
'id_parent' => array ( 'type' => self :: TYPE_INT , 'validate' => 'isInt' ),
'position' => array ( 'type' => self :: TYPE_INT , 'validate' => 'isInt' ),
'classbloc' => array ( 'type' => self :: TYPE_STRING , 'validate' => 'isGenericName' ),
// Lang fields
'url' => array ( 'type' => self :: TYPE_STRING , 'lang' => TRUE , 'validate' => 'isUrl' , 'required' => FALSE , 'size' => 255 ),
2015-11-03 14:52:12 +01:00
'title' => array ( 'type' => self :: TYPE_STRING , 'lang' => TRUE , 'validate' => 'isGenericName' , 'required' => TRUE , 'size' => 255 ),
2015-09-23 16:57:43 +02:00
'text' => array ( 'type' => self :: TYPE_HTML , 'lang' => TRUE , 'validate' => 'isCleanHtml' )
)
);
public function __construct ( $id = NULL , $id_lang = NULL , $id_shop = NULL ) {
parent :: __construct ( $id , $id_lang , $id_shop );
}
public function add ( $null_values = false , $autodate = true )
{
$result = parent :: add ( $null_values , $autodate );
//Add and remove shop association
if ( $result && Shop :: isFeatureActive ()) {
//Delete record if shop has been removed from the list
$result &= Db :: getInstance () -> delete ( $this -> def [ 'table' ] . '_shop' , '`' . $this -> def [ 'primary' ] . '` =' . ( int ) $this -> id . ' AND id_shop NOT IN (' . implode ( ', ' , $this -> id_shop_list ) . ')' );
// Insert new record if shop has been added in the list
$insert = '' ;
foreach ( $this -> id_shop_list as $id_shop ) {
if ( $insert != '' )
$insert .= ', ' ;
$insert .= '(' . ( int ) $this -> id . ' , ' . $id_shop . ') ' ;
}
$result &= Db :: getInstance () -> execute ( '
INSERT IGNORE INTO `'._DB_PREFIX_.$this->def['table'].'_shop` ( `'.$this->def['primary'].'` , `id_shop` )
VALUES ' . $insert
);
}
Hook :: exec ( 'actionSaveMenu' );
return $result ;
}
public function update ( $null_values = FALSE ) {
$result = parent :: update ( $null_values );
//Add and remove shop association
if ( $result && Shop :: isFeatureActive ()) {
//Delete record if shop has been removed from the list
$result &= Db :: getInstance () -> delete ( $this -> def [ 'table' ] . '_shop' , '`' . $this -> def [ 'primary' ] . '` =' . ( int ) $this -> id . ' AND id_shop NOT IN (' . implode ( ', ' , $this -> id_shop_list ) . ')' );
// Insert new record if shop has been added in the list
$insert = '' ;
foreach ( $this -> id_shop_list as $id_shop ) {
if ( $insert != '' )
$insert .= ', ' ;
$insert .= '(' . ( int ) $this -> id . ' , ' . $id_shop . ') ' ;
}
$result &= Db :: getInstance () -> execute ( '
INSERT IGNORE INTO `'._DB_PREFIX_.$this->def['table'].'_shop` ( `'.$this->def['primary'].'` , `id_shop` )
VALUES ' . $insert
);
}
Hook :: exec ( 'actionSaveMenu' );
return $result ;
}
public static function getMenu ()
{
$context = Context :: getContext ();
$menuLinks = self :: getLinks ( 0 , $context );
foreach ( $menuLinks as $i => $link )
{
2016-06-02 12:35:53 +02:00
2015-09-23 16:57:43 +02:00
$menuLinks [ $i ][ 'children' ] = self :: getLinks ( $link [ 'id_link' ], $context );
$menuLinks [ $i ][ 'nbChildren' ] = count ( $menuLinks [ $i ][ 'children' ]);
$menuLinks [ $i ][ 'ctaImgExist' ] = $menuLinks [ $i ][ 'nbChildren' ] < 5 && file_exists ( _PS_IMG_DIR_ . 'menu/' . $link [ 'id_link' ] . '.jpg' );
2016-06-02 12:35:53 +02:00
if ( $menuLinks [ $i ][ 'children' ])
2015-09-23 16:57:43 +02:00
{
2016-06-02 12:35:53 +02:00
foreach ( $menuLinks [ $i ][ 'children' ] as $y => $children )
{
$menuLinks [ $i ][ 'children' ][ $y ][ 'children' ] = self :: getLinks ( $children [ 'id_link' ], $context );
if ( $menuLinks [ $i ][ 'children' ][ $y ][ 'children' ])
{
foreach ( $menuLinks [ $i ][ 'children' ][ $y ][ 'children' ] as $z => $subchildren )
{
$menuLinks [ $i ][ 'children' ][ $y ][ 'children' ][ $z ][ 'children' ] = self :: getLinks ( $subchildren [ 'id_link' ], $context );
}
}
}
2015-09-23 16:57:43 +02:00
}
}
return $menuLinks ;
}
public static function getLinks ( $id_parent , $context )
{
$links = Db :: getInstance () -> executeS ( '
SELECT adv . `id_link` , `title` , `url` , `classbloc` , `text`
FROM `'._DB_PREFIX_.'advmenu` adv
JOIN `'._DB_PREFIX_.'advmenu_lang` advl ON adv . `id_link` = advl . `id_link` AND id_lang = '. (int)$context->cookie->id_lang . '
WHERE `id_parent` = ' . (int)$id_parent . '
ORDER BY `position` ASC
' );
return $links ;
}
public function cleanPositions (){
return Db :: getInstance () -> execute ( 'UPDATE `' . _DB_PREFIX_ . ' advmenu `
SET `position` = `position` - 1
WHERE `id_link` = '.(int)$this->id_link.'
AND `position` > ' . ( int ) $this -> position );
}
public function updatePosition ( $way , $position )
{
$sql = ' SELECT `position` , `id_link`
FROM `'._DB_PREFIX_.'advmenu`
WHERE `id_parent` = '.(int)$this->id_parent.'
ORDER BY `position` ASC ' ;
if ( ! $res = Db :: getInstance () -> executeS ( $sql ))
return false ;
foreach ( $res as $row )
if (( int ) $row [ 'id_link' ] == ( int ) $this -> id_link )
$moved_row = $row ;
if ( ! isset ( $moved_row ) || ! isset ( $position ))
return false ;
// < and > statements rather than BETWEEN operator
// since BETWEEN is treated differently according to databases
$res = Db :: getInstance () -> execute ( '
UPDATE `'._DB_PREFIX_.'advmenu`
SET `position` = `position` '.($way ? ' - 1 ' : ' + 1 ').'
WHERE `id_parent` = '.(int)$this->id_parent.'
AND `position`
' . ( $way
? '> ' . ( int ) $moved_row [ 'position' ] . ' AND `position` <= ' . ( int ) $position
: '< ' . ( int ) $moved_row [ 'position' ] . ' AND `position` >= ' . ( int ) $position )
)
&& Db :: getInstance () -> execute ( '
UPDATE `'._DB_PREFIX_.'advmenu`
SET `position` = '.(int)$position.'
WHERE `id_link` = '.(int)$moved_row[' id_link ' ]
);
$this -> refreshPositions ();
Hook :: exec ( 'actionSaveMenu' );
return $res ;
}
public function refreshPositions (){
$sql = ' SELECT `id_link`
FROM `'._DB_PREFIX_.'advmenu`
WHERE `id_parent` = '.(int)$this->id_parent.'
ORDER BY `position` ASC ' ;
if ( ! $blocks = Db :: getInstance () -> executeS ( $sql ))
return false ;
$pos = 0 ;
foreach ( $blocks as $block ) {
Db :: getInstance () -> execute ( '
UPDATE `'._DB_PREFIX_.'advmenu`
SET `position` = '.(int)$pos.'
WHERE `id_link` = '.(int)$block[' id_link ' ]);
$pos ++ ;
}
}
}