bebeboutik/modules/privatesales_extrafields/privatesales_extrafields.php
2017-10-30 13:41:50 +01:00

343 lines
11 KiB
PHP
Executable File

<?php
include_once(_PS_ROOT_DIR_.'/modules/privatesales/Sale.php');
class PrivateSales_ExtraFields extends Module {
public function __construct() {
$this->name = 'privatesales_extrafields';
$this->tab = 'pricing_promotion';
$this->author = 'Antadis';
$this->version = '1.0';
parent::__construct();
$this->displayName = $this->l('Private Sales Extra Fields');
$this->description = $this->l('Adds extra fields 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.',
"AdminPrivateSalesExtraFields",
"privatesales_extrafields",
'.$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' => 'Champs suppl.',
'en' => 'Extra Fields',
);
$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_extrafield` (
`id_field` INTEGER NOT NULL AUTO_INCREMENT,
`type` TINYINT NOT NULL,
`blank` BOOLEAN NOT NULL,
PRIMARY KEY(`id_field`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
if(!Db::getInstance()->Execute($query)) {
return FALSE;
}
$query = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'privatesale_extrafield_lang` (
`id_field` INTEGER NOT NULL,
`id_lang` INTEGER NOT NULL,
`name` VARCHAR(255) NOT NULL,
`format` TEXT,
PRIMARY KEY(`id_field`, `id_lang`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
if(!Db::getInstance()->Execute($query)) {
return FALSE;
}
$query = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'privatesale_extrafield_sale` (
`id_field` INTEGER NOT NULL,
`id_sale` INTEGER NOT NULL,
`id_lang` INTEGER NOT NULL,
`value` TEXT,
PRIMARY KEY(`id_field`, `id_sale`, `id_lang`)
) 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')
OR !$this->registerHook('privatesales_listing')) {
return FALSE;
}
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'privatesale_module`
VALUES (
(SELECT `id_module` FROM `'._DB_PREFIX_.'module` WHERE `name` = "privatesales_extrafields"),
"privatesales_extrafields"
)
');
return TRUE;
}
function uninstall() {
$tab = Db::getInstance()->Execute('
SELECT `id_tab`
FROM `'._DB_PREFIX_.'tab`
WHERE `module` = "privatesales_extrafields"
');
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_extrafields"
');
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_extrafield`');
Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'privatesale_extrafield_lang`');
Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'privatesale_extrafield_sale`');
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'privatesale_module` WHERE `modulename` = "privatesales_extrafields"');
return TRUE;
}
public function hookPrivateSales_Edit($params) {
global $cookie;
$iso = Language::getIsoById((int)($cookie->id_lang));
$this->_languages = Language::getLanguages(FALSE);
$this->_defaultFormLanguage = (int) $cookie->id_lang;
$fields = Db::getInstance()->ExecuteS('
SELECT f.`type`, l.`id_field`, l.`name`
FROM `'._DB_PREFIX_.'privatesale_extrafield_lang` l, `'._DB_PREFIX_.'privatesale_extrafield` f
WHERE l.`id_lang` = '.$cookie->id_lang.'
AND f.`id_field` = l.`id_field`
');
$fields_values = array();
if(isset($params['sale']->id)){
foreach(Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'privatesale_extrafield_sale` s
WHERE s.`id_sale` = '.(int) $params['sale']->id.'
') as $value) {
if($fields_values[$value['id_field']] === NULL) {
$fields_values[$value['id_field']] = array();
}
$fields_values[$value['id_field']][$value['id_lang']] = $value['value'];
}
}
else{
$fields_values[] = array();
}
// foreach(Db::getInstance()->ExecuteS('
// SELECT *
// FROM `'._DB_PREFIX_.'privatesale_extrafield_sale` s
// WHERE s.`id_sale` = '.(int) $params['sale']->id.'
// ') as $value) {
// if($fields_values[$value['id_field']] === NULL) {
// $fields_values[$value['id_field']] = array();
// }
// $fields_values[$value['id_field']][$value['id_lang']] = $value['value'];
// }
$imgs = array('salebg','liston','listoff'/*,'fb','trailerbg','trailercenter','thumb'*/);
$images = array();
foreach($imgs as $img) {
$images[] = 'img_'.$img;
}
$divLangName = array();
foreach($fields as $field) {
$divLangName[] = 'm_extrafields_'.$field['id_field'].'_value';
}
$divLangName = implode('¤', $divLangName).'¤description¤video¤'.implode('¤', $images);
foreach($fields as $field) {
echo '<div class="form-group">
<label class="control-label" >'.$field['name'].'</label>
<div class="">';
foreach($this->_languages as $language) {
echo '<div id="m_extrafields_'.$field['id_field'].'_value_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $this->_defaultFormLanguage ? 'block' : 'none').'; float: left;">';
if($field['type'] == 1) {
echo '<textarea class="form-control" name="m_extrafields_'.$field['id_field'].'_value_'.$language['id_lang'].'">'.(isset($fields_values[$field['id_field']][$language['id_lang']])?Tools::safeOutput($fields_values[$field['id_field']][$language['id_lang']], TRUE):'').'</textarea>';
} else {
echo '<input type="text" class="form-control" name="m_extrafields_'.$field['id_field'].'_value_'.$language['id_lang'].'" value="'.(isset($fields_values[$field['id_field']][$language['id_lang']])?Tools::safeOutput($fields_values[$field['id_field']][$language['id_lang']], TRUE):'').'" />';
}
echo '
</div>';
}
$this->displayFlags($this->_languages, $this->_defaultFormLanguage, $divLangName, 'm_extrafields_'.$field['id_field'].'_value');
echo '<div class="clear"></div>
</div>
</div>';
}
}
public function hookPrivateSales_Delete($params) {
Db::getInstance()->Execute('
DELETE FROM `'._DB_PREFIX_.'privatesale_extrafield_sale`
WHERE `id_sale` = '.$params['sale']->id
);
}
public function hookPrivateSales_Create($params) {
$fields = Db::getInstance()->ExecuteS('SELECT * FROM `'._DB_PREFIX_.'privatesale_extrafield`');
$languages = Language::getLanguages(FALSE);
foreach($fields as $field) {
foreach($languages as $lang) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'privatesale_extrafield_sale` VALUES (
'.(int) $field['id_field'].',
'.$params['sale']->id.',
'.(int) $lang['id_lang'].',
"'.pSQL(Tools::getValue('m_extrafields_'.$field['id_field'].'_value_'.$lang['id_lang'], '')).'"
)
');
}
}
}
public function hookPrivateSales_Update($params) {
Db::getInstance()->Execute('
DELETE FROM `'._DB_PREFIX_.'privatesale_extrafield_sale`
WHERE `id_sale` = '.$params['sale']->id
);
$this->hookPrivateSales_Create($params);
}
public function hookPrivateSales_Listing($params) {
global $cookie;
$ids = array();
if(isset($params['sales'])){
foreach($params['sales'] as $sale) {
$ids[] = $sale->id;
}
}
if(isset($params['other_sales_category'])){
foreach($params['other_sales_category'] as $sale) {
$ids[] = $sale['id_sale'];
}
}
if(isset($params['futuresales'])){
foreach($params['futuresales'] as $sale) {
$ids[] = $sale->id;
}
}
$result = array();
if(count($ids) > 0) {
foreach(Db::getInstance()->ExecuteS('
SELECT f.`blank`, l.`name`, l.`format`, s.`value`, s.`id_sale`
FROM `'._DB_PREFIX_.'privatesale_extrafield` f
LEFT JOIN `'._DB_PREFIX_.'privatesale_extrafield_lang` l ON l.`id_field` = f.`id_field`
LEFT JOIN `'._DB_PREFIX_.'privatesale_extrafield_sale` s ON l.`id_field` = s.`id_field` AND s.`id_lang` = l.`id_lang`
WHERE s.`id_sale` IN ('.implode(', ', $ids).')
AND l.`id_lang` = '.(int) $cookie->id_lang.'
') as $field) {
if(!isset($result[$field['id_sale']])) {
$result[$field['id_sale']] = '';
}
$result[$field['id_sale']] .= ((int) $field['blank'] == 1 || (int) $field['blank'] == 0 && $field['value'] != ''? str_replace(array('{name}', '{value}'), array($field['name'], Tools::safeOutput($field['value'])), $field['format']): '');
}
}
return serialize($result);
}
public static function getFieldsForSale($ids)
{
global $cookie;
$result = array();
foreach (Db::getInstance()->ExecuteS('
SELECT f.`blank`, l.`name`, l.`format`, s.`value`, s.`id_sale`, f.id_field
FROM `'._DB_PREFIX_.'privatesale_extrafield` f
LEFT JOIN `'._DB_PREFIX_.'privatesale_extrafield_lang` l ON l.`id_field` = f.`id_field`
LEFT JOIN `'._DB_PREFIX_.'privatesale_extrafield_sale` s ON l.`id_field` = s.`id_field` AND s.`id_lang` = l.`id_lang`
WHERE s.`id_sale` IN ('.implode(', ', $ids).')
AND l.`id_lang` = '.(int) $cookie->id_lang.'
') as $field) {
if (!isset($result[$field['id_sale']])) {
$result[$field['id_sale']] = array();
}
$result[$field['id_sale']][$field['id_field']] = $field['value'];
}
return $result;
}
}