Skip to content
This repository has been archived by the owner on Dec 1, 2020. It is now read-only.

Commit

Permalink
Ability to open a link as a popup window
Browse files Browse the repository at this point in the history
The window dimensions are hardcode at the moment, but at least it gives
webmaster a place to modify these settings while getting the basic working feature
already implemented.
  • Loading branch information
Raphaël Droz committed Aug 7, 2015
1 parent 277c610 commit c4eafc4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
49 changes: 44 additions & 5 deletions blocktopmenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function installDb()
`id_linksmenutop` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`id_shop` INT(11) UNSIGNED NOT NULL,
`new_window` TINYINT( 1 ) NOT NULL,
`new_popup` TINYINT( 1 ) NOT NULL,
INDEX (`id_shop`)
) ENGINE = '._MYSQL_ENGINE_.' CHARACTER SET utf8 COLLATE utf8_general_ci;') &&
Db::getInstance()->execute('
Expand Down Expand Up @@ -226,7 +227,7 @@ public function getContent()
$shops = Shop::getContextListShopID();

foreach ($shops as $shop_id) {
$added = MenuTopLinks::add($links_label, $labels, Tools::getValue('new_window', 0), (int)$shop_id);
$added = MenuTopLinks::add($links_label, $labels, Tools::getValue('new_window', 0), (int)$shop_id, Tools::getValue('new_popup', 0));

if (!$added) {
$shop = new Shop($shop_id);
Expand Down Expand Up @@ -272,13 +273,14 @@ public function getContent()
$link = array();
$label = array();
$new_window = (int)Tools::getValue('new_window', 0);
$new_popup = (int)Tools::getValue('new_popup', 0);

foreach (Language::getLanguages(false) as $lang) {
$link[$lang['id_lang']] = Tools::getValue('link_'.(int)$lang['id_lang']);
$label[$lang['id_lang']] = Tools::getValue('label_'.(int)$lang['id_lang']);
}

MenuTopLinks::update($link, $label, $new_window, (int)$id_shop, (int)$id_linksmenutop, (int)$id_linksmenutop);
MenuTopLinks::update($link, $label, $new_window, (int)$id_shop, (int)$id_linksmenutop, (int)$id_linksmenutop, $new_popup);
$this->_html .= $this->displayConfirmation($this->l('The link has been edited.'));
}
$update_cache = true;
Expand Down Expand Up @@ -565,7 +567,17 @@ protected function makeMenu()
$default_language = Configuration::get('PS_LANG_DEFAULT');
$link = MenuTopLinks::get($link[0]['id_linksmenutop'], $default_language, (int)Shop::getContextShopID());
}
$this->_menu .= '<li><a href="'.Tools::HtmlEntitiesUTF8($link[0]['link']).'"'.(($link[0]['new_window']) ? ' onclick="return !window.open(this.href);"': '').' title="'.Tools::safeOutput($link[0]['label']).'">'.Tools::safeOutput($link[0]['label']).'</a></li>'.PHP_EOL;
$this->_menu .= '<li><a href="'.Tools::HtmlEntitiesUTF8($link[0]['link']).'"';
if($link[0]['new_window']) {
if($link[0]['new_popup']) {
// https://developer.mozilla.org/fr/docs/Web/API/Window/open
$this->_menu .= ' onclick="return !window.open(this.href, \'prestashop-popup-blocktopmenu\', \'menubar=no, status=no, scrollbars=no, menubar=no, status=no, width=400, height=300\');"';
} else {
$this->_menu .= ' onclick="return !window.open(this.href);"';
}
}
$this->_menu .= ' title="'.Tools::safeOutput($link[0]['label']).'">'.Tools::safeOutput($link[0]['label']).'</a></li>'.PHP_EOL;

}
break;
}
Expand Down Expand Up @@ -915,8 +927,8 @@ public function hookActionShopDataDuplication($params)

foreach ($linksmenutop as $id => $link) {
Db::getInstance()->execute('
INSERT IGNORE INTO '._DB_PREFIX_.'linksmenutop (id_linksmenutop, id_shop, new_window)
VALUES (null, '.(int)$params['new_id_shop'].', '.(int)$link['new_window'].')');
INSERT IGNORE INTO '._DB_PREFIX_.'linksmenutop (id_linksmenutop, id_shop, new_window, new_popup)
VALUES (null, '.(int)$params['new_id_shop'].', '.(int)$link['new_window'].', '.(int)$link['new_popup'].')');

$linksmenutop[$id]['new_id_linksmenutop'] = Db::getInstance()->Insert_ID();
}
Expand Down Expand Up @@ -1076,6 +1088,24 @@ public function renderAddForm()
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'switch',
'label' => $this->l('New popup'),
'name' => 'new_popup',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
)
),
'submit' => array(
Expand Down Expand Up @@ -1275,6 +1305,7 @@ public function getAddLinkFieldsValues()
$links_label_edit = '';
$labels_edit = '';
$new_window_edit = '';
$new_popup_edit = '';

if (Tools::isSubmit('updatelinksmenutop')) {
$link = MenuTopLinks::getLinkLang(Tools::getValue('id_linksmenutop'), (int)Shop::getContextShopID());
Expand All @@ -1286,10 +1317,12 @@ public function getAddLinkFieldsValues()
$links_label_edit = $link['link'];
$labels_edit = $link['label'];
$new_window_edit = $link['new_window'];
$new_popup_edit = $link['new_popup'];
}

$fields_values = array(
'new_window' => Tools::getValue('new_window', $new_window_edit),
'new_popup' => Tools::getValue('new_popup', $new_popup_edit),
'id_linksmenutop' => Tools::getValue('id_linksmenutop'),
);

Expand Down Expand Up @@ -1341,6 +1374,12 @@ public function renderList()
'type' => 'bool',
'align' => 'center',
'active' => 'status',
),
'new_popup' => array(
'title' => $this->l('New popup'),
'type' => 'bool',
'align' => 'center',
'active' => 'status',
)
);

Expand Down
14 changes: 9 additions & 5 deletions menutoplinks.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MenuTopLinks
{
public static function gets($id_lang, $id_linksmenutop = null, $id_shop)
{
$sql = 'SELECT l.id_linksmenutop, l.new_window, s.name, ll.link, ll.label
$sql = 'SELECT l.id_linksmenutop, l.new_window, l.new_popup, s.name, ll.link, ll.label
FROM '._DB_PREFIX_.'linksmenutop l
LEFT JOIN '._DB_PREFIX_.'linksmenutop_lang ll ON (l.id_linksmenutop = ll.id_linksmenutop AND ll.id_lang = '.(int)$id_lang.' AND ll.id_shop='.(int)$id_shop.')
LEFT JOIN '._DB_PREFIX_.'shop s ON l.id_shop = s.id_shop
Expand All @@ -46,7 +46,7 @@ public static function get($id_linksmenutop, $id_lang, $id_shop)
public static function getLinkLang($id_linksmenutop, $id_shop)
{
$ret = Db::getInstance()->executeS('
SELECT l.id_linksmenutop, l.new_window, ll.link, ll.label, ll.id_lang
SELECT l.id_linksmenutop, l.new_window, l.new_popup, ll.link, ll.label, ll.id_lang
FROM '._DB_PREFIX_.'linksmenutop l
LEFT JOIN '._DB_PREFIX_.'linksmenutop_lang ll ON (l.id_linksmenutop = ll.id_linksmenutop AND ll.id_shop='.(int)$id_shop.')
WHERE 1
Expand All @@ -57,17 +57,19 @@ public static function getLinkLang($id_linksmenutop, $id_shop)
$link = array();
$label = array();
$new_window = false;
$new_popup = false;

foreach ($ret as $line) {
$link[$line['id_lang']] = Tools::safeOutput($line['link']);
$label[$line['id_lang']] = Tools::safeOutput($line['label']);
$new_window = (bool)$line['new_window'];
$new_popup = (bool)$line['new_popup'];
}

return array('link' => $link, 'label' => $label, 'new_window' => $new_window);
return array('link' => $link, 'label' => $label, 'new_window' => $new_window, 'new_popup' => $new_popup);
}

public static function add($link, $label, $newWindow = 0, $id_shop)
public static function add($link, $label, $newWindow = 0, $id_shop, $newPopup = 0)
{
if (!is_array($label)) {
return false;
Expand All @@ -80,6 +82,7 @@ public static function add($link, $label, $newWindow = 0, $id_shop)
'linksmenutop',
array(
'new_window'=>(int)$newWindow,
'new_popup'=>(int)$newPopup,
'id_shop' => (int)$id_shop
)
);
Expand All @@ -103,7 +106,7 @@ public static function add($link, $label, $newWindow = 0, $id_shop)
return $result;
}

public static function update($link, $labels, $newWindow = 0, $id_shop, $id_link)
public static function update($link, $labels, $newWindow = 0, $id_shop, $id_link, $newPopup = 0)
{
if (!is_array($labels)) {
return false;
Expand All @@ -116,6 +119,7 @@ public static function update($link, $labels, $newWindow = 0, $id_shop, $id_link
'linksmenutop',
array(
'new_window'=>(int)$newWindow,
'new_popup'=>(int)$newPopup,
'id_shop' => (int)$id_shop
),
'id_linksmenutop = '.(int)$id_link
Expand Down
12 changes: 12 additions & 0 deletions upgrade/install-2.3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

if (!defined('_PS_VERSION_')) {
exit;
}

function upgrade_module_2_3($object)
{
if (Db::getInstance()->ExecuteS('SHOW COLUMNS FROM `'._DB_PREFIX_.'linksmenutop` LIKE \'new_popup\'') == false)
Db::getInstance()->Execute('ALTER TABLE `'._DB_PREFIX_.'linksmenutop` ADD `new_popup` TINYINT( 1 ) NOT NULL DEFAULT 0 AFTER `new_window`');
return true;
}

0 comments on commit c4eafc4

Please sign in to comment.