2016-01-04 12:49:26 +01:00
< ? php
class AdminLandingPages extends AdminTab {
public function __construct () {
$this -> lang = TRUE ;
parent :: __construct ();
}
public function postProcess () {
global $cookie , $currentIndex ;
if ( Tools :: isSubmit ( 'submitLPAdd' )) {
if ( $name = Tools :: getValue ( 'name' )) {
if ( Db :: getInstance () -> getRow ( '
SELECT `id_landing_page`
FROM `'._DB_PREFIX_.'landing_page`
WHERE `name` = " '.pSQL(Tools::str2url( $name )).' "
' )) {
$this -> _errors [] = $this -> l ( 'A landing page with this name already exists.' );
} else {
Db :: getInstance () -> ExecuteS ( '
INSERT INTO `'._DB_PREFIX_.'landing_page`
VALUES (
DEFAULT ,
'.(int) (bool) Tools::getValue(' enabled ').' ,
2016-04-08 15:31:40 +02:00
'.(int) (bool) Tools::getValue(' connect_button ').' ,
2017-04-28 11:05:12 +02:00
'.(int) (bool) Tools::getValue(' on_home ').' ,
2016-01-04 12:49:26 +01:00
" '.pSQL(Tools::str2url( $name )).' " ,
" '.pSQL(Tools::getValue('text1')).' " ,
" '.pSQL(Tools::getValue('text2')).' " ,
" '.pSQL(Tools::getValue('tag')).' "
)
' );
$id_landing_page = Db :: getInstance () -> Insert_ID ();
foreach ( Language :: getLanguages ( FALSE ) as $language ) {
if ( isset ( $_FILES [ 'img1_' . $language [ 'id_lang' ]]) && $_FILES [ 'img1_' . $language [ 'id_lang' ]][ 'name' ] != '' ) {
if ( ! copy ( $_FILES [ 'img1_' . $language [ 'id_lang' ]][ 'tmp_name' ], _PS_ROOT_DIR_ . '/modules/landingpages/img/' . $id_landing_page . '_1_' . $language [ 'id_lang' ] . '.jpg' )) {
$this -> _errors [] = $this -> l ( 'An error occured during the image upload.' );
}
}
if ( isset ( $_FILES [ 'img2_' . $language [ 'id_lang' ]]) && $_FILES [ 'img2_' . $language [ 'id_lang' ]][ 'name' ] != '' ) {
if ( ! copy ( $_FILES [ 'img2_' . $language [ 'id_lang' ]][ 'tmp_name' ], _PS_ROOT_DIR_ . '/modules/landingpages/img/' . $id_landing_page . '_2_' . $language [ 'id_lang' ] . '.png' )) {
$this -> _errors [] = $this -> l ( 'An error occured during the image upload.' );
}
}
}
echo '<div class="conf">' . $this -> l ( 'Page added successfully' ) . '</div>' ;
}
} else {
$this -> _errors [] = $this -> l ( 'A landing page name is required.' );
}
} elseif ( Tools :: isSubmit ( 'submitLPUpdate' ) && $id_landing_page = ( int ) Tools :: getValue ( 'id_lp' )) {
$this -> curlp = Db :: getInstance () -> getRow ( '
SELECT *
FROM `'._DB_PREFIX_.'landing_page`
WHERE `id_landing_page` = '.$id_landing_page.'
' );
if ( ! $this -> curlp ) {
Tools :: redirectAdmin ( $currentIndex . '&token=' . Tools :: getAdminTokenLite ( 'AdminLandingPage' ));
}
if ( $name = Tools :: getValue ( 'name' )) {
if ( Db :: getInstance () -> getRow ( '
SELECT `id_landing_page`
FROM `'._DB_PREFIX_.'landing_page`
WHERE `name` = " '.pSQL(Tools::str2url( $name )).' "
AND `id_landing_page` != '.$id_landing_page.'
' )) {
$this -> _errors [] = $this -> l ( 'A landing page with this name already exists.' );
} else {
Db :: getInstance () -> ExecuteS ( '
UPDATE `'._DB_PREFIX_.'landing_page`
SET `enabled` = '.(int) (bool) Tools::getValue(' enabled ').' ,
2016-04-08 15:31:40 +02:00
`connect_button` = '.(int) (bool) Tools::getValue(' connect_button ').' ,
2017-04-28 11:05:12 +02:00
`on_home` = '.(int) (bool) Tools::getValue(' on_home ').' ,
2016-01-04 12:49:26 +01:00
`name` = " '.pSQL(Tools::str2url( $name )).' " ,
`text1` = " '.pSQL(Tools::getValue('text1')).' " ,
2016-04-08 15:31:40 +02:00
`text2` = " '.pSQL(Tools::getValue('text2')).' " ,
2016-01-04 12:49:26 +01:00
`tag` = " '.pSQL(Tools::getValue('tag'), true).' "
WHERE `id_landing_page` = '.$id_landing_page.'
' );
foreach ( Language :: getLanguages ( FALSE ) as $language ) {
if ( isset ( $_FILES [ 'img1_' . $language [ 'id_lang' ]]) && $_FILES [ 'img1_' . $language [ 'id_lang' ]][ 'name' ] != '' ) {
if ( ! copy ( $_FILES [ 'img1_' . $language [ 'id_lang' ]][ 'tmp_name' ], _PS_ROOT_DIR_ . '/modules/landingpages/img/' . $id_landing_page . '_1_' . $language [ 'id_lang' ] . '.jpg' )) {
$this -> _errors [] = $this -> l ( 'An error occured during the image upload.' );
}
}
if ( isset ( $_FILES [ 'img2_' . $language [ 'id_lang' ]]) && $_FILES [ 'img2_' . $language [ 'id_lang' ]][ 'name' ] != '' ) {
if ( ! copy ( $_FILES [ 'img2_' . $language [ 'id_lang' ]][ 'tmp_name' ], _PS_ROOT_DIR_ . '/modules/landingpages/img/' . $id_landing_page . '_2_' . $language [ 'id_lang' ] . '.png' )) {
$this -> _errors [] = $this -> l ( 'An error occured during the image upload.' );
}
}
}
echo '<div class="conf">' . $this -> l ( 'Page updated successfully' ) . '</div>' ;
}
} else {
$this -> _errors [] = $this -> l ( 'A landing page name is required.' );
}
}
}
public function display () {
global $cookie , $currentIndex ;
$this -> curlp = FALSE ;
if ( isset ( $_GET [ 'edit_lp' ]) && $id_landing_page = ( int ) Tools :: getValue ( 'id_lp' )) {
$this -> curlp = Db :: getInstance () -> getRow ( '
SELECT *
FROM `'._DB_PREFIX_.'landing_page`
WHERE `id_landing_page` = '.$id_landing_page.'
' );
if ( ! $this -> curlp ) {
Tools :: redirectAdmin ( $currentIndex . '&token=' . Tools :: getAdminTokenLite ( 'AdminLandingPage' ));
}
}
$this -> displayForm ();
$pages = Db :: getInstance () -> ExecuteS ( '
SELECT *
FROM `'._DB_PREFIX_.'landing_page`
ORDER BY `id_landing_page` ASC
' );
$counts = array ();
foreach ( Db :: getInstance () -> ExecuteS ( '
SELECT `id_landing_page` , COUNT ( c . id_customer ) AS `count`
FROM `'._DB_PREFIX_.'landing_page_customer` c
GROUP BY `id_landing_page` ;
' ) as $count ) {
$counts [ $count [ 'id_landing_page' ]] = $count [ 'count' ];
}
echo ' < fieldset class = " space " >
< legend >< img src = " '.__PS_BASE_URI__.'modules/landingpages/prefs.gif " alt = " " title = " " /> '.$this->l(' Landing pages list ').' </ legend >
< table id = " landing_list " class = " table " style = " width: 100%; " >
< tr >
< th id = " id_sort " > '.$this->l(' ID ').' </ th >
< th id = " name_sort " > '.$this->l(' Name ').' </ th >
< th > '.$this->l(' URL chunk ').' </ th >
< th id = " cust_sort " > '.$this->l(' Customers ').' </ th >
< th style = " width: 55px; " > '.$this->l(' Enabled ').' </ th >
2016-04-08 15:31:40 +02:00
< th style = " width: 55px; " > '.$this->l(' Connect ').' </ th >
2017-04-28 11:05:12 +02:00
< th style = " width: 55px; " > '.$this->l(' Home ').' </ th >
2016-01-04 12:49:26 +01:00
< th style = " width: 35px; " > '.$this->l(' Actions ').' </ th >
</ tr > ' ;
foreach ( $pages as $page ) {
echo ' < tr >
< td > '.$page[' id_landing_page '].' </ td >
< td > '.$page[' name '].' </ td >
2017-04-28 11:05:12 +02:00
< td > '.($page[' on_home ']?' https :// '.$_SERVER[' SERVER_NAME '].' ? lpes = '.$page[' name ']:' https :// '.$_SERVER[' SERVER_NAME '].' / authentification ? create_account = 1 & lp = '.$page[' name ']).' </ td >
2016-01-04 12:49:26 +01:00
< td > '.(isset($counts[$page[' id_landing_page ']])? $counts[$page[' id_landing_page ']]: ' 0 ').' </ td >
< td > '.($page[' enabled ']? $this->l(' Yes '): $this->l(' No ')).' </ td >
2016-04-08 15:31:40 +02:00
< td > '.($page[' connect_button ']? $this->l(' Yes '): $this->l(' No ')).' </ td >
2017-04-28 11:05:12 +02:00
< td > '.($page[' on_home ']? $this->l(' Yes '): $this->l(' No ')).' </ td >
2016-01-04 12:49:26 +01:00
< td >
< img style = " cursor: pointer; " onclick = " itemEdition('. $page['id_landing_page'] .') " title = " '. $this->l ('Edit this landing page').' " alt = " " src = " ../img/admin/edit.gif " />
</ td >
</ tr > ' ;
}
echo ' </ table >
</ fieldset >
< script type = " text/javascript " >
<!--
$ ( document ) . ready ( function () {
jQuery . fn . sortElements = ( function (){
var sort = [] . sort ;
return function ( comparator , getSortable ) {
getSortable = getSortable || function (){ return this ;};
var placements = this . map ( function (){
var sortElement = getSortable . call ( this ),
parentNode = sortElement . parentNode ,
nextSibling = parentNode . insertBefore (
document . createTextNode ( \ ' \ ' ),
sortElement . nextSibling
);
return function () {
if ( parentNode === this ) {
throw new Error (
" You can \ 't sort elements if any one is a descendant of another. "
);
}
parentNode . insertBefore ( this , nextSibling );
parentNode . removeChild ( nextSibling );
};
});
return sort . call ( this , comparator ) . each ( function ( i ){
placements [ i ] . call ( getSortable . call ( this ));
});
};
})();
var table = $ ( " #landing_list " );
$ ( " #name_sort " )
. wrapInner ( \ '<span title="' . $this -> l ( 'Sort' ) . ' " /> \ ')
. each ( function (){
var th = $ ( this ),
thIndex = th . index (),
inverse = false ;
th . click ( function (){
table . find ( " td " ) . filter ( function (){
return $ ( this ) . index () === thIndex ;
}) . sortElements ( function ( a , b ){
return $ . text ([ a ]) > $ . text ([ b ]) ?
inverse ? - 1 : 1
: inverse ? 1 : - 1 ;
}, function (){
return this . parentNode ;
});
inverse = ! inverse ;
});
});
$ ( " #id_sort, #cust_sort " )
. wrapInner ( \ '<span title="' . $this -> l ( 'Sort' ) . ' " /> \ ')
. each ( function (){
var th = $ ( this ),
thIndex = th . index (),
inverse = false ;
th . click ( function (){
table . find ( " td " ) . filter ( function (){
return $ ( this ) . index () === thIndex ;
}) . sortElements ( function ( a , b ){
return a > b ?
inverse ? - 1 : 1
: inverse ? 1 : - 1 ;
}, function (){
return this . parentNode ;
});
inverse = ! inverse ;
});
});
});
-->
</ script >
' ;
}
public function displayForm ( $isMainTab = TRUE ) {
global $currentIndex , $cookie ;
parent :: displayForm ();
$iso = Language :: getIsoById (( int )( $cookie -> id_lang ));
$divLangName = 'img1¤img2' ;
echo '<script type="text/javascript" src="' . __PS_BASE_URI__ . ' modules / landingpages / landingpages . js " ></script>
< script type = " text/javascript " >
i18n_delete = " '. $this->l ('Are you sure you want to delete this landing page?').' " ;
current_location = " '. $currentIndex .'&token='.Tools::getAdminTokenLite('AdminLandingPages').' " ;
</ script >
< form action = " '. $currentIndex .'&token='.Tools::getAdminTokenLite('AdminLandingPages').' " method = " post " enctype = " multipart/form-data " >
< fieldset >
< legend >< img src = " ../img/admin/cms.gif " alt = " " title = " " /> '.$this->l(' Edit a landing page ').' </ legend > ' ;
echo '<label>' . $this -> l ( 'Name:' ) . ' </ label >
< div class = " margin-form " >
< div id = " name " style = " float: left; " >
< input name = " name " type = " text " value = " '.( $this->curlp ? $this->curlp ['name']: '').' " style = " width: 150px; " />
< sup > *</ sup >
</ div >
< div class = " clear " ></ div >
</ div > ' ;
echo '<label>' . $this -> l ( 'Enabled:' ) . ' </ label >
< div class = " margin-form " >
< div id = " enabled " style = " float: left; " >
< input name = " enabled " type = " radio " value = " 0 " '.($this->curlp? ($this->curlp[' enabled '] == 0? ' checked = " checked " ': ' '):' checked = " checked " ').' /> '.$this->l(' No ').' & nbsp ; & nbsp ; & nbsp ; < input name = " enabled " type = " radio " value = " 1 " '.($this->curlp && $this->curlp[' enabled '] == 1? ' checked = " checked " ': ' ').' /> '.$this->l(' Yes ').'
< sup > *</ sup >
</ div >
< div class = " clear " ></ div >
</ div > ' ;
2016-04-08 15:31:40 +02:00
echo '<label>' . $this -> l ( 'Connect Button:' ) . ' </ label >
< div class = " margin-form " >
< div id = " enabled " style = " float: left; " >
< input name = " connect_button " type = " radio " value = " 0 " '.($this->curlp? ($this->curlp[' connect_button '] == 0? ' checked = " checked " ': ' '):' checked = " checked " ').' /> '.$this->l(' No ').' & nbsp ; & nbsp ; & nbsp ; < input name = " connect_button " type = " radio " value = " 1 " '.($this->curlp && $this->curlp[' connect_button '] == 1? ' checked = " checked " ': ' ').' /> '.$this->l(' Yes ').'
< sup > *</ sup >
</ div >
< div class = " clear " ></ div >
</ div > ' ;
2017-04-28 11:05:12 +02:00
echo '<label>' . $this -> l ( 'Home:' ) . ' </ label >
< div class = " margin-form " >
< div id = " enabled " style = " float: left; " >
< input name = " on_home " type = " radio " value = " 0 " '.($this->curlp? ($this->curlp[' on_home '] == 0? ' checked = " checked " ': ' '):' checked = " checked " ').' /> '.$this->l(' No ').' & nbsp ; & nbsp ; & nbsp ; < input name = " on_home " type = " radio " value = " 1 " '.($this->curlp && $this->curlp[' on_home '] == 1? ' checked = " checked " ': ' ').' /> '.$this->l(' Yes ').'
< sup > *</ sup >
</ div >
< div class = " clear " ></ div >
</ div > ' ;
2016-01-04 12:49:26 +01:00
echo '<label>' . $this -> l ( 'Text 1:' ) . ' </ label >
< div class = " margin-form " >
< div id = " enabled " style = " float: left; " >
< input name = " text1 " type = " text " value = " '.( $this->curlp ? $this->curlp ['text1']: '').' " />
</ div >
< div class = " clear " ></ div >
</ div > ' ;
echo '<label>' . $this -> l ( 'Text 2:' ) . ' </ label >
< div class = " margin-form " >
< div id = " enabled " style = " float: left; " >
< input name = " text2 " type = " text " value = " '.( $this->curlp ? $this->curlp ['text2']: '').' " />
</ div >
< div class = " clear " ></ div >
</ div > ' ;
echo '<label>' . $this -> l ( 'Tag :' ) . ' </ label >
< div class = " margin-form " >
< div id = " enabled " style = " float: left; " >
< textarea name = " tag " id = " tag " cols = " 30 " rows = " 10 " > '.($this->curlp? $this->curlp[' tag ']: ' ').' </ textarea >
</ div >
< div class = " clear " ></ div >
</ div > ' ;
echo '<label>' . $this -> l ( 'Background image:' ) . ' </ label >
< div class = " margin-form " > ' ;
foreach ( $this -> _languages as $language ) {
echo '<div id="img1_' . $language [ 'id_lang' ] . '" style="display: ' . ( $language [ 'id_lang' ] == $this -> _defaultFormLanguage ? 'block' : 'none' ) . ' ; float : left ; " >
< input type = " file " name = " img1_'. $language['id_lang'] .' " />
< p > '.$this->l(' Size : ').' '.$this->l(' img1 size ').' </ p > ' ;
if ( $this -> curlp && file_exists ( _PS_ROOT_DIR_ . '/modules/landingpages/img/' . $this -> curlp [ 'id_landing_page' ] . '_1_' . $language [ 'id_lang' ] . '.jpg' )) {
echo '<p><a href="' . __PS_BASE_URI__ . 'modules/landingpages/img/' . $this -> curlp [ 'id_landing_page' ] . '_1_' . $language [ 'id_lang' ] . '.jpg" onclick="window.open(this.href); return false;"> ' . $this -> l ( 'Preview' ) . '</a></p>' ;
}
echo '
</ div > ' ;
}
$this -> displayFlags ( $this -> _languages , $this -> _defaultFormLanguage , $divLangName , 'img1' );
echo ' < div class = " clear " ></ div >
</ div > ' ;
echo '<label style="display: none;">' . $this -> l ( 'Brands image:' ) . ' </ label >
< div class = " margin-form " style = " display: none; " > ' ;
foreach ( $this -> _languages as $language ) {
echo '<div id="img2_' . $language [ 'id_lang' ] . '" style="display: ' . ( $language [ 'id_lang' ] == $this -> _defaultFormLanguage ? 'block' : 'none' ) . ' ; float : left ; " >
< input type = " file " name = " img2_'. $language['id_lang'] .' " />
< p > '.$this->l(' Size : ').' '.$this->l(' img2 size ').' </ p > ' ;
if ( $this -> curlp && file_exists ( _PS_ROOT_DIR_ . '/modules/landingpages/img/' . $this -> curlp [ 'id_landing_page' ] . '_2_' . $language [ 'id_lang' ] . '.png' )) {
echo '<p><a href="' . __PS_BASE_URI__ . 'modules/landingpages/img/' . $this -> curlp [ 'id_landing_page' ] . '_2_' . $language [ 'id_lang' ] . '.png" onclick="window.open(this.href); return false;"> ' . $this -> l ( 'Preview' ) . '</a></p>' ;
}
echo '
</ div > ' ;
}
$this -> displayFlags ( $this -> _languages , $this -> _defaultFormLanguage , $divLangName , 'img2' );
echo ' < div class = " clear " ></ div >
</ div > ' ;
echo ' < br class = " clear " />
< div class = " margin-form " >
< input type = " hidden " name = " id_lp " value = " '.( $this->curlp ? $this->curlp ['id_landing_page']: '').' " />
< input type = " submit " class = " button " name = " submitLPAdd " value = " '. $this->l ('Add landing page').' " />
< input type = " submit " class = " button'.( $this->curlp ? '': ' disable " ').' " name= " submitLPUpdate " value= " '.$this->l(' Edit landing page ').' " '.( $this->curlp ? '': ' disabled= " disabled " ').' id= " submitLPUpdate " />
</ div > ' ;
echo ' </ fieldset >
</ form > ' ;
}
}