bebeboutik/modules/privatesales/privatesales.php

514 lines
18 KiB
PHP
Raw Normal View History

2016-01-04 12:49:26 +01:00
<?php
include_once(_PS_ROOT_DIR_.'/modules/privatesales/Sale.php');
class PrivateSales extends Module {
public function __construct() {
$this->name = 'privatesales';
$this->tab = 'pricing_promotion';
$this->author = 'Antadis';
$this->version = '2.5';
parent::__construct();
$this->displayName = $this->l('Private Sales');
$this->description = $this->l('Private sales management for Prestashop');
}
public function install() {
# Install admin tabs
$tab_ids = array();
Db::getInstance()->Execute('
UPDATE `'._DB_PREFIX_.'tab`
SET `position` = `position` + 1
WHERE `id_parent` = 0 AND `position` > 5
');
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'tab`
VALUES (DEFAULT, 0, "AdminPrivateSales", "privatesales", 6)
');
$tab_ids[] = Db::getInstance()->Insert_ID();
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'access`
VALUES (1, '.$tab_ids[0].', 1, 1, 1, 1)
');
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'tab`
VALUES (DEFAULT, '.$tab_ids[0].', "AdminPrivateSalesSales", "privatesales", 1)
');
$tab_ids[] = Db::getInstance()->Insert_ID();
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'access`
VALUES (1, '.$tab_ids[1].', 1, 1, 1, 1)
');
$tabs_i18n = array(
'fr' => array(
'Ventes Privées',
'Gestion des ventes',
),
'en' => array(
'Private Sales',
'Sales management',
),
);
$langs = Db::getInstance()->ExecuteS('
SELECT `id_lang`, `iso_code`
FROM `'._DB_PREFIX_.'lang`
');
foreach($langs as $lang) {
if(isset($tabs_i18n[$lang['iso_code']])) {
for($i=0,$l=count($tab_ids); $i < $l;$i++) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'tab_lang`
VALUES ('.$tab_ids[$i].', '.$lang['id_lang'].', "'.$tabs_i18n[$lang['iso_code']][$i].'")
');
}
} else {
for($i=0,$l=count($tab_ids); $i < $l;$i++) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'tab_lang`
VALUES ('.$tab_ids[$i].', '.$lang['id_lang'].', "'.$tabs_i18n['en'][$i].'")
');
}
}
}
# Add custom hooks
$hooks = array(
'privatesales_create' => array('Private Sales - Create', 'Called on a private sale creation'),
'privatesales_update' => array('Private Sales - Update', 'Called on a private sale update'),
'privatesales_delete' => array('Private Sales - Delete', 'Called on a private sale deletion'),
'privatesales_edit' => array('Private Sales - Edit', 'Called on the private sale edition page'),
'privatesales_listing' => array('Private Sales - Listing', 'Called on the private sales list'),
'privatesales_product' => array('Private Sales - Product', 'Called on each private sale category page'),
'privatesales_category' => array('Private Sales - Category', 'Called on each private sale product page'),
'privatesales_end' => array('Private Sales - End', 'Called when a sale ends'),
'preprocess' => array('Pre Process', 'Called before the page processing'),
);
foreach($hooks as $k => $v) {
if(count(Db::getInstance()->ExecuteS('
SELECT `id_hook`
FROM `'._DB_PREFIX_.'hook`
WHERE `name` = "'.$k.'"
LIMIT 1
')) == 0) {
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'hook`
VALUES (DEFAULT, "'.$k.'", "'.$v[0].'", "'.$v[1].'", 0, 0)
');
}
}
# Add private sales tables
$query = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'privatesale` (
`id_sale` INTEGER NOT NULL AUTO_INCREMENT,
`date_start` DATETIME NOT NULL,
`date_end` DATETIME NOT NULL,
`enabled` BOOL NOT NULL,
`featured` BOOL NOT NULL,
`logout` BOOL NOT NULL,
`public` BOOL NOT NULL,
`id_category` INTEGER NOT NULL,
`date_upd` DATETIME NOT NULL,
PRIMARY KEY(`id_sale`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
if(!Db::getInstance()->Execute($query)) {
return FALSE;
}
$query = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'privatesale_lang` (
`id_sale` INTEGER NOT NULL,
`id_lang` INTEGER NOT NULL,
`description` TEXT,
`video` VARCHAR(255) NOT NULL,
PRIMARY KEY(`id_sale`, `id_lang`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
if(!Db::getInstance()->Execute($query)) {
return FALSE;
}
$query = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'privatesale_group` (
`id_sale` INTEGER NOT NULL,
`id_group` INTEGER NOT NULL,
PRIMARY KEY(`id_sale`, `id_group`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
if(!Db::getInstance()->Execute($query)) {
return FALSE;
}
$query = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'privatesale_category` (
`id_sale` INTEGER NOT NULL,
`id_category` INTEGER NOT NULL,
PRIMARY KEY(`id_sale`, `id_category`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
if(!Db::getInstance()->Execute($query)) {
return FALSE;
}
$query = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'privatesale_carrier` (
`id_sale` INTEGER NOT NULL,
`id_carrier` INTEGER NOT NULL,
PRIMARY KEY(`id_sale`, `id_carrier`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
if(!Db::getInstance()->Execute($query)) {
return FALSE;
}
$query = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'privatesale_notify` (
`id_sale` INTEGER NOT NULL,
`email` VARCHAR(255) NOT NULL,
`customer` INTEGER NOT NULL,
`deleted` BOOL NOT NULL,
PRIMARY KEY(`id_sale`, `email`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
if(!Db::getInstance()->Execute($query)) {
return FALSE;
}
$query = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'privatesale_module` (
`id_module` INTEGER NOT NULL,
`modulename` VARCHAR(255) NOT NULL,
PRIMARY KEY(`id_module`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
if(!Db::getInstance()->Execute($query)) {
return false;
}
# Register hooks
if(!parent::install()
|| !$this->registerHook('preprocess')
|| !$this->registerHook('home')
|| !$this->registerHook('categoryAddition')
|| !$this->registerHook('categoryUpdate')
|| !$this->registerHook('categoryDeletion')
|| !$this->registerHook('privatesales_category')
|| !$this->registerHook('privatesales_product')) {
return FALSE;
}
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'privatesale_module`
VALUES (
(SELECT `id_module` FROM `'._DB_PREFIX_.'module` WHERE `name` = "privatesales"),
"privatesales"
)
');
# Add image type
if(count(Db::getInstance()->ExecuteS('
SELECT `name` FROM `'._DB_PREFIX_.'image_type` WHERE `name` = "privatesales"
')) == 0) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'image_type` VALUES (
DEFAULT, "privatesales", 980, 480, 0, 1, 0, 0, 0, 0
)
');
}
# Set default configuration values
Configuration::updateValue('PRIVATESALES_ROOT', 1);
Configuration::updateValue('PRIVATESALES_SHOW_PUBLIC', 0);
Configuration::updateValue('PRIVATESALES_FENCE', 0);
Configuration::updateValue('PRIVATESALES_CARRIERFENCE', 0);
Configuration::updateValue('PRIVATESALES_GUESTLIST', 1);
Configuration::updateValue('PRIVATESALES_FUTURELIMIT', 0);
Configuration::updateValue('PRIVATESALES_FEATURED_IGNORE', 0);
Configuration::updateValue('PRIVATESALES_FEATURED_CURRENT', 0);
Configuration::updateValue('PRIVATESALES_FEATURED_PQTY', 4);
Configuration::updateValue('PRIVATESALES_FEATURED_ORDER', 0);
Configuration::updateValue('PRIVATESALES_TRAILER_NOTIFY', 1);
Configuration::updateValue('PRIVATESALES_TRAILER_SIGNIN', 0);
return TRUE;
}
public function uninstall() {
$tabs = Db::getInstance()->Execute('
SELECT `id_tab`
FROM `'._DB_PREFIX_.'tab`
WHERE `module` = "privatesales"
');
$tab_position = Db::getInstance()->Execute('
SELECT `id_tab`
FROM `'._DB_PREFIX_.'tab`
WHERE `module` = "privatesales" AND `id_parent` = 0
');
$tab_position = $tab_position[0]['position'];
foreach($tabs as $tab) {
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"
');
Db::getInstance()->Execute('
UPDATE `'._DB_PREFIX_.'tab`
SET `position` = `position` - 1
WHERE `id_parent` = 0 AND `position` > '.$tab_position
);
if(parent::uninstall() == FALSE) {
return FALSE;
}
Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'privatesale`');
Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'privatesale_lang`');
Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'privatesale_category`');
Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'privatesale_carrier`');
Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'privatesale_notify`');
Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'privatesale_module`');
Configuration::deleteByName('PRIVATESALES_ROOT');
Configuration::deleteByName('PRIVATESALES_SHOW_PUBLIC');
Configuration::deleteByName('PRIVATESALES_FENCE');
Configuration::deleteByName('PRIVATESALES_CARRIERFENCE');
Configuration::deleteByName('PRIVATESALES_GUESTLIST');
Configuration::deleteByName('PRIVATESALES_FUTURELIMIT');
Configuration::deleteByName('PRIVATESALES_FEATURED_IGNORE');
Configuration::deleteByName('PRIVATESALES_FEATURED_CURRENT');
Configuration::deleteByName('PRIVATESALES_FEATURED_PQTY');
Configuration::deleteByName('PRIVATESALES_FEATURED_ORDER');
Configuration::deleteByName('PRIVATESALES_TRAILER_NOTIFY');
Configuration::deleteByName('PRIVATESALES_TRAILER_SIGNIN');
2016-03-16 15:24:12 +01:00
Configuration::deleteByName('PRIVATESALES_EXPENDABLE');
2016-01-04 12:49:26 +01:00
$files = glob(dirname(__FILE__).'/img/*');
foreach($files as $file) {
unlink($file);
}
return TRUE;
}
public function hookPreprocess($params) {
global $cookie, $smarty, $page_name;
$smarty->assign(array(
'privatesales' => 1,
));
if($page_name == 'category' || $page_name == 'product' || $page_name == 'cart') {
$id_category = Tools::getValue('id_category');
$id_product = Tools::getValue('id_product');
if($id_product) {
$id_category = Db::getInstance()->ExecuteS('
SELECT `id_category_default`
FROM `'._DB_PREFIX_.'product`
WHERE `id_product`='.intval($id_product)
);
$id_category = $id_category[0]['id_category_default'];
}
if($id_category) {
$sale = Sale::getSaleFromCategory($id_category);
if($sale !== NULL) {
$addclass = "";
if($page_name != 'product') {
// if($sale->sale_type == 1) {
// $addclass = ' vp-bebe';
// } elseif($sale->sale_type == 2) {
// $addclass = ' vp-enfant';
// } elseif($sale->sale_type == 3) {
// $addclass = ' vp-maman';
// }
}
$smarty->assign('bodyClass', $smarty->getTemplateVars('bodyClass').$addclass);
2016-03-16 15:24:12 +01:00
if($cookie->isLogged()){
2016-01-04 12:49:26 +01:00
$customer = new Customer((int) $cookie->id_customer);
if($customer->isMemberOfGroup(2)){
return;
}
}
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
if( !empty($_SERVER['QUERY_STRING']) ) {
$query_string = $_SERVER['QUERY_STRING'];
$utm = substr($query_string, strpos($query_string, '&'));
$count = explode("&", $utm);
if(count($count) < 2 ) {
unset($utm);
}
}
if(!$sale->enabled
|| strtotime('now') > strtotime($sale->date_end)
|| strtotime('now') < strtotime($sale->date_start)
|| (!$cookie->isLogged() && $sale->pub == 0)
|| count(array_intersect($cookie->isLogged()? Customer::getGroupsStatic($cookie->id_customer): array(1), $sale->groups)) == 0) {
if (!empty($utm)) {
Tools::redirect('/?'. $utm);
} else {
Tools::redirect('/');
}
}
if(Configuration::get('PRIVATESALES_FENCE') == 1) {
global $cart;
$cart_products = $cart->getProducts();
$sale_categories = Sale::getCategoriesFromCache($sale->id);
if(count($cart_products) > 0) {
for($i=0, $l=count($cart_products); $i < $l; $i++) {
if(!in_array($cart_products[$i]['id_category_default'], $sale_categories)) {
// We are entering in a sale but we have some products from another sale in our cart
if($page_name !== 'cart') {
Tools::redirect('/modules/privatesales/salechange.php?next='.urlencode($_SERVER['REQUEST_URI']).'&back='.(isset($_SERVER['HTTP_REFERER'])? urlencode($_SERVER['HTTP_REFERER']): ''));
}
exit;
}
}
}
}
}
}
}
return;
}
public function hookHome($params) {
global $smarty, $cookie, $site_version_front;
$langs = Language::getLanguages();
$lang = (int) Tools::getValue('id_lang', Configuration::get('PS_LANG_DEFAULT'));
$isolang = Language::getIsoById($lang);
$trailer_i18n = array(
'fr' => 'bande-annonce',
'en' => 'trailer',
);
try {
smartyRegisterFunction($smarty, 'modifier', 'str2url', array('Tools', 'str2url'));
} catch(Exception $e) {}
if(Configuration::get('PS_REWRITING_SETTINGS')) {
$smarty->assign(array(
'rewriting' => TRUE,
'base_url' => (count($langs) > 1? (isset($trailer_i18n[$isolang])? $isolang.'/'.$trailer_i18n[$isolang]: $isolang.'/'.$trailer_i18n['en']): (isset($trailer_i18n[$isolang])? $trailer_i18n[$isolang]: $trailer_i18n['en'])),
));
} else {
$smarty->assign('rewriting', FALSE);
}
$display_limit = Configuration::get('PRIVATESALES_FUTURELIMIT');
2016-03-16 15:24:12 +01:00
2016-03-21 10:22:14 +01:00
$sales = Sale::getSales(TRUE, NULL, NULL, 'current', FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'), '`position` DESC', NULL, NULL, $site_version_front,TRUE);
2016-01-04 12:49:26 +01:00
if($display_limit == 0) {
2016-03-21 10:22:14 +01:00
$futuresales = Sale::getSales(TRUE, NULL, NULL, TRUE, FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'), '`position` ASC', NULL, NULL, $site_version_front,TRUE);
2016-01-04 12:49:26 +01:00
} else {
2016-03-21 10:22:14 +01:00
$futuresales = Sale::getSales(TRUE, NULL, NULL, (int) $display_limit, FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'), '`position` ASC', NULL, NULL, $site_version_front,TRUE);
2016-01-04 12:49:26 +01:00
}
$smarty->assign(array(
'path' => __PS_BASE_URI__.'modules/privatesales/',
'sales' => $sales,
'HOOK_PRIVATESALES_LISTING' => Module::hookExec('privatesales_listing', array('sales' => $sales, 'futuresales' => $futuresales)),
'futuresales' => $futuresales,
'showtoguests' => Configuration::get('PRIVATESALES_GUESTLIST'),
'customer_groups' => ($cookie->isLogged()? Customer::getGroupsStatic((int) $cookie->id_customer): array(1)),
'bestSaleHome' => Module::hookExec('bestSaleHome'),
));
$smarty->assign('newsletter', (int)Module::getInstanceByName('blocknewsletter')->active);
/* Call a hook to display more information on form */
$smarty->assign(array(
'HOOK_CREATE_ACCOUNT_FORM' => Module::hookExec('createAccountForm'),
'HOOK_CREATE_ACCOUNT_TOP' => Module::hookExec('createAccountTop')
));
2016-03-16 15:24:12 +01:00
2016-01-04 12:49:26 +01:00
return $this->display(__FILE__, 'home.tpl');
}
public function hookFooter($params) {
global $smarty, $cookie;
$display_limit = Configuration::get('PRIVATESALES_FUTURELIMIT');
if($cookie->isLogged()) {
2016-03-21 10:22:14 +01:00
$sales = Sale::getSales(TRUE, NULL, NULL, 'current', FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'),'`date_end` ASC', NULL, NULL, FALSE, TRUE);
2016-01-04 12:49:26 +01:00
if($display_limit == 0) {
2016-03-21 10:22:14 +01:00
$futuresales = Sale::getSales(TRUE, NULL, NULL, TRUE, FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'),'`date_end` ASC', NULL, NULL, FALSE, TRUE);
2016-01-04 12:49:26 +01:00
} else {
2016-03-21 10:22:14 +01:00
$futuresales = Sale::getSales(TRUE, NULL, NULL, (int) $display_limit, FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'),'`date_end` ASC', NULL, NULL, FALSE, TRUE);
2016-01-04 12:49:26 +01:00
}
} else {
2016-03-21 10:22:14 +01:00
$sales = Sale::getSales(TRUE, TRUE, NULL, 'current', FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'),'`date_end` ASC', NULL, NULL, FALSE, TRUE);
2016-01-04 12:49:26 +01:00
if($display_limit == 0) {
2016-03-21 10:22:14 +01:00
$futuresales = Sale::getSales(TRUE, TRUE, NULL, TRUE, FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'),'`date_end` ASC', NULL, NULL, FALSE, TRUE);
2016-01-04 12:49:26 +01:00
} else {
2016-03-21 10:22:14 +01:00
$futuresales = Sale::getSales(TRUE, TRUE, NULL, (int) $display_limit, FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'),'`date_end` ASC', NULL, NULL, FALSE, TRUE);
2016-01-04 12:49:26 +01:00
}
}
$smarty->assign(array(
'path' => __PS_BASE_URI__.'modules/privatesales/',
'sales' => $sales,
'HOOK_PRIVATESALES_LISTING' => Module::hookExec('privatesales_listing', array('sales' => $sales, 'futuresales' => $futuresales)),
'futuresales' => $futuresales,
'showtoguests' => Configuration::get('PRIVATESALES_GUESTLIST'),
'customer_groups' => ($cookie->isLogged()? Customer::getGroupsStatic((int) $cookie->id_customer): array(1)),
));
return $this->display(__FILE__, 'footer.tpl');
}
public function hookHeader($params) {
return $this->display(__FILE__, 'header.tpl');
}
public function hookCategoryAddition($params) {
$sale = Sale::getSaleFromCategory($params['category']->id, FALSE);
if($sale !== NULL) {
$sale->buildCategoryCache();
}
}
public function hookCategoryUpdate($params) {
$this->hookCategoryAddition($params);
}
public function hookCategoryDeletion($params) {
$sale = Sale::getSaleFromCategory($params['category']->id, FALSE);
if($sale !== NULL) {
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_category` WHERE `id_sale` = '.$sale->id);
}
}
public function hookPrivateSales_Product($params) {
global $cookie, $smarty;
$product = new Product(Tools::getValue('id_product'));
$sale = Sale::getSaleFromCategory($product->id_category_default);
$smarty->assign(array(
'category' => new Category($product->id_category_default, $cookie->id_lang),
'sale' => $sale,
2016-03-16 17:56:03 +01:00
'consumable' => ((int) _SHOP_PRIVATESALES_CONSUMABLE == (int) $sale->id)? true : false,
2016-01-04 12:49:26 +01:00
));
return $this->display(__FILE__, 'product.tpl');
}
public function hookPrivateSales_Category($params) {
global $cookie, $smarty;
$sale = Sale::getSaleFromCategory(Tools::getValue('id_category'));
$smarty->assign(array(
'category' => new Category(Tools::getValue('id_category'), $cookie->id_lang),
'sale' => $sale,
2016-06-22 17:23:34 +02:00
'path_saleimg' => dirname(__FILE__).'/img/'.$sale->id.'/',
'has_salebg' => is_file(dirname(__FILE__).'/img/'.$sale->id.'/salebg_'.$cookie->id_lang.'.jpg'),
2016-03-16 17:56:03 +01:00
'consumable' => ((int) _SHOP_PRIVATESALES_CONSUMABLE == (int) $sale->id)? true : false,
2016-01-04 12:49:26 +01:00
));
return $this->display(__FILE__, 'category.tpl');
}
}