bebeboutik/modules/privatesales_shipping/privatesales_shipping.php
2016-11-21 10:11:27 +01:00

225 lines
6.4 KiB
PHP
Executable File

<?php
include_once(_PS_ROOT_DIR_.'/modules/privatesales/Sale.php');
class PrivateSales_Shipping extends Module {
public function __construct() {
$this->name = 'privatesales_shipping';
$this->tab = 'pricing_promotion';
$this->author = 'Antadis';
$this->version = '1.0';
parent::__construct();
$this->displayName = $this->l('Private Sales Shipping');
$this->description = $this->l('Adds a shipping method to private sales');
}
public function install() {
if(!Module::isInstalled('privatesales')) {
return FALSE;
}
# Install admin tab
$id_mod = Db::getInstance()->ExecuteS('
SELECT `id_tab`
FROM `'._DB_PREFIX_.'tab`
WHERE `class_name` = "AdminPrivateSales"
');
if(count($id_mod) == 0) {
return FALSE;
}
$id_mod = $id_mod[0]['id_tab'];
$posmax = Db::getInstance()->ExecuteS('SELECT MAX(`position`) + 1 AS `max` FROM `'._DB_PREFIX_.'tab` WHERE `id_parent` = '.$id_mod);
$posmax = $posmax[0]['max'];
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'tab`
VALUES (
DEFAULT,
'.$id_mod.',
"AdminPrivateSalesShipping",
"privatesales_shipping",
'.$posmax.'
)
');
$tab_id = Db::getInstance()->Insert_ID();
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'access`
VALUES (1, '.$tab_id.', 1, 1, 1, 1)
');
$tabs_i18n = array(
'fr' => 'Transport',
'en' => 'Shipping',
);
$langs = Db::getInstance()->ExecuteS('
SELECT `id_lang`, `iso_code`
FROM `'._DB_PREFIX_.'lang`
');
foreach($langs as $lang) {
if(isset($tabs_i18n[$lang['iso_code']])) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'tab_lang`
VALUES ('.$tab_id.', '.$lang['id_lang'].', "'.$tabs_i18n[$lang['iso_code']].'")
');
} else {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'tab_lang`
VALUES ('.$tab_id.', '.$lang['id_lang'].', "'.$tabs_i18n['en'].'")
');
}
}
# Add tags tables
$query = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'privatesale_shipping` (
`id_shipping` INTEGER NOT NULL AUTO_INCREMENT,
PRIMARY KEY(`id_shipping`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
if(!Db::getInstance()->Execute($query)) {
return FALSE;
}
$query = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'privatesale_shipping_lang` (
`id_shipping` INTEGER NOT NULL,
`id_lang` INTEGER NOT NULL,
`value` TEXT,
PRIMARY KEY(`id_shipping`, `id_lang`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
if(!Db::getInstance()->Execute($query)) {
return FALSE;
}
$query = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'privatesale_shipping_sale` (
`id_shipping` INTEGER NOT NULL,
`id_sale` INTEGER NOT NULL,
PRIMARY KEY(`id_shipping`, `id_sale`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
if(!Db::getInstance()->Execute($query)) {
return FALSE;
}
# Register hooks
if(!parent::install()
OR !$this->registerHook('privatesales_create')
OR !$this->registerHook('privatesales_update')
OR !$this->registerHook('privatesales_delete')
OR !$this->registerHook('privatesales_edit')) {
return FALSE;
}
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'privatesale_module`
VALUES (
(SELECT `id_module` FROM `'._DB_PREFIX_.'module` WHERE `name` = "privatesales_shipping"),
"privatesales_shipping"
)
');
return TRUE;
}
function uninstall() {
$tab = Db::getInstance()->Execute('
SELECT `id_tab`
FROM `'._DB_PREFIX_.'tab`
WHERE `module` = "privatesales_shipping"
');
if(count($tab) == 0) {
return FALSE;
}
$tab = $tab[0];
Db::getInstance()->Execute('
DELETE FROM `'._DB_PREFIX_.'tab_lang`
WHERE `id_tab` = '.$tab['id_tab']
);
Db::getInstance()->Execute('
DELETE FROM `'._DB_PREFIX_.'access`
WHERE `id_tab` = '.$tab['id_tab']
);
Db::getInstance()->Execute('
DELETE FROM `'._DB_PREFIX_.'tab`
WHERE `module` = "privatesales_shipping"
');
Db::getInstance()->Execute('
UPDATE `'._DB_PREFIX_.'tab`
SET `position` = `position` - 1
WHERE `id_parent` = '.$tab['id_parent'].' AND `position` > '.$tab['position']
);
if(parent::uninstall() == FALSE) {
return FALSE;
}
Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'privatesale_shipping`');
Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'privatesale_shipping_lang`');
Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'privatesale_shipping_sale`');
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_module` WHERE `modulename` = "privatesales_shipping"');
return TRUE;
}
public function hookPrivateSales_Edit($params) {
global $cookie;
$shipping = Db::getInstance()->ExecuteS('
SELECT l.`id_shipping`, l.`value`'.($params['sale'] !== NULL? ', (
SELECT IFNULL(s.`id_shipping`, NULL)
FROM `'._DB_PREFIX_.'privatesale_shipping_sale` s
WHERE l.`id_shipping` = s.`id_shipping` AND s.`id_sale` = '.$params['sale']->id.'
) AS `id_sale`': '').'
FROM `'._DB_PREFIX_.'privatesale_shipping_lang` l
WHERE l.`id_lang` = '.$cookie->id_lang
);
if(count($shipping) == 0) {
echo '<label>'.$this->l('Shipping:').'</label>
<div class="margin-form">
<div id="tags" style="float: left; padding-top: 5px;">'.$this->l('No shipping method available').'</div>
<div class="clear"></div>
</div>';
} else {
echo '
<label>'.$this->l('Shipping:').'</label>
<div class="margin-form">
<div id="tags" style="float: left;">
<select id="s_shipping" name="s_shipping">
<option value=""></option>';
foreach($shipping as $ship) {
echo '<option value="'.$ship['id_shipping'].'"'.(isset($ship['id_shipping']) && $ship['id_sale'] !== NULL?' selected="selected"':'').'>'.$ship['value'].'</option>';
}
echo '
</select>
</div>
<div class="clear"></div>
</div>';
}
}
public function hookPrivateSales_Delete($params) {
Db::getInstance()->Execute('
DELETE FROM `'._DB_PREFIX_.'privatesale_shipping_sale`
WHERE `id_sale` = '.$params['sale']->id
);
}
public function hookPrivateSales_Create($params) {
if($shipping = Tools::getValue('s_shipping')) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'privatesale_shipping_sale` VALUES (
'.(int) $shipping.', '.$params['sale']->id.'
)
');
}
}
public function hookPrivateSales_Update($params) {
Db::getInstance()->Execute('
DELETE FROM `'._DB_PREFIX_.'privatesale_shipping_sale`
WHERE `id_sale` = '.$params['sale']->id
);
$this->hookPrivateSales_Create($params);
}
}