2017-07-06 17:41:10 +02:00

469 lines
18 KiB
PHP

<?php
include_once dirname(__FILE__).'/../../classes/AdvConstructorBlock.php';
include_once dirname(__FILE__).'/../../classes/AdvConstructorHook.php';
class AdminAdvConstructorController extends ModuleAdminController
{
public function __construct()
{
$this->table = 'advconstructor';
$this->className = 'AdvConstructorBlock';
$this->identifier = 'id_advconstructor';
$this->lang = true;
$this->deleted = false;
$this->bootstrap = true;
$this->explicitSelect = true;
$this->context = Context::getContext();
$this->_defaultOrderBy = 'id_advconstructor';
if ( Tools::getValue('hookid') ) {
$blocks = AdvConstructorBlock::getBlocksByHookIDLite(Tools::getValue('hookid'));
$blocks_id = "0";
if($blocks){
$blocks_id = implode(',',$blocks);
}
$this->_where = ' AND a.`id_advconstructor` IN ('.$blocks_id.')';
}
parent::__construct();
$this->fields_list = array(
'id_advconstructor' => array(
'title' => $this->l('ID'),
'align' => 'center',
'width' => 25,
'filter_key' => 'a!id_advconstructor'
),
'title' => array(
'title' => $this->l('Nom')
),
'skin' => array(
'title' => $this->l('Apparence')
),
'active' => array(
'title' => $this->l('Activé'),
'active' => 'status',
'filter_key' => 'a!active',
'align' => 'text-center',
'type' => 'bool',
'class' => 'fixed-width-sm',
'orderby' => false
),
);
$this->actions = array('edit','delete');
$this->bulk_actions = array(
'delete' => array(
'text' => $this->l('Supprimer la liste'),
'icon' => 'icon-trash',
'confirm' => $this->l('Supprimer les blocs selectionné?')
)
);
$this->specificConfirmDelete = false;
}
public function initPageHeaderToolbar()
{
parent::initPageHeaderToolbar();
if ($this->display == 'edit' || $this->display == 'add') {
$this->page_header_toolbar_btn['back_to_list'] = array(
'href' => self::$currentIndex.'&token='.$this->token,
'desc' => $this->l('Retour à la liste', null, null, false),
'icon' => 'process-icon-back'
);
}
$this->page_header_toolbar_btn['new_constructor'] = array(
'href' => self::$currentIndex.'&addadvconstructor&token='.$this->token,
'desc' => $this->l('Ajouter un nouveau bloc', null, null, false),
'icon' => 'process-icon-new'
);
$this->page_header_toolbar_btn['position'] = array(
'href' => $this->context->link->getAdminLink('AdminAdvConstructorPosition'),
'desc' => $this->l('Gérer les positions', null, null, false),
'icon' => 'process-icon-modules-list'
);
}
public function renderForm()
{
if (!($obj = $this->loadObject(true))) {
return;
}
$context = Context::getContext();
$hooks_id = explode( ",", Configuration::get('ADVCONSTRUCTOR_HOOKS') );
$hooks = array();
foreach ($hooks_id as $hook_id) {
$hook = new Hook($hook_id, $context->language->id);
$hooks[] = array(
'id_hook' => $hook->id,
'name' => $hook->name,
'title' => $hook->title,
);
}
$block_hooks = array();
if (!empty($this->object->id)) {
$block_hooks = $this->object->getHooks();
}
foreach ($hooks as $key => $hook) {
$hook['selected'] = (bool) in_array($hook['id_hook'], $block_hooks);
$hooks[$key] = $hook;
}
$skins = array();
foreach ( explode(',', Configuration::get('ADVCONSTRUCTOR_TYPE')) as $skin ) {
$skins[] = array(
'id' => $skin,
'name' => $skin
);
}
$icons = array();
$icons[] = array('id' => '', 'name' => 'Standard');
foreach ( explode(',', Configuration::get('ADVCONSTRUCTOR_ICON')) as $key => $icon ) {
$icons[] = array(
'id' => $key+1,
'name' => ucfirst($icon)
);
}
$id_lang = $this->context->language->id;
if ( Tools::isSubmit('id_advconstructor') ) {
$id_advconstructor = (int)Tools::getValue('id_advconstructor');
$image = __PS_BASE_URI__.'img/constructor/image/'.$id_advconstructor;
$image_icon_url = __PS_BASE_URI__.'img/constructor/image_icon_'.$id_advconstructor;
$hover = __PS_BASE_URI__.'img/constructor/hover/'.$id_advconstructor ;
} else {
$image = "noimg" ;
$image_icon_url = "noimg" ;
$hover = "noimg" ;
}
$selected_categories = !empty($this->object->id) ? $this->object->getCategory() : array();
$this->fields_form = array(
'multilang' => true,
'tinymce' => true,
'legend' => array(
'title' => $this->l('Bloc'),
),
'submit' => array(
'name' => 'submitBlock',
'title' => $this->l('Enregistrer')
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Titre'),
'name' => 'title',
'lang' => true,
'required' => true,
'form_group_class' => 'fieldhide input_info',
'size' => 255
),
array(
'type' => 'text',
'label' => $this->l('Label du lien'),
'name' => 'subtitle',
'lang' => true,
'form_group_class' => 'fieldhide input_info',
'size' => 255
),
array(
'type' => 'text',
'label' => $this->l('Lien'),
'name' => 'link',
'lang' => true,
'form_group_class' => 'fieldhide input_info',
'size' => 255
),
array(
'type' => 'switch',
'label' => $this->l('Lien externe'),
'name' => 'external',
'required' => false,
'is_bool' => true,
'form_group_class' => 'fieldhide input_info',
'values' => array(
array(
'id' => 'external_on',
'value' => 1,
'label' => $this->l('Yes')
),
array(
'id' => 'external_off',
'value' => 0,
'label' => $this->l('No')
)
)
),
array(
'type' => 'textarea',
'label' => $this->l('Contenu'),
'name' => 'content',
'form_group_class' => 'fieldhide input_info',
'autoload_rte' => true,
'lang' => true,
'cols' => 60,
'rows' => 120
),
array(
'type' => 'datetime',
'label' => $this->l('Date de début'),
'name' => 'date_from',
'form_group_class' => 'fieldhide input_dates',
'size' => 33,
),
array(
'type' => 'datetime',
'label' => $this->l('Date de fin'),
'name' => 'date_to',
'form_group_class' => 'fieldhide input_dates',
'size' => 33,
),
array(
'type' => 'select',
'label' => $this->l('Apparence du bloc'),
'name' => 'skin',
'required' => true,
'form_group_class' => 'fieldhide input_association',
'options' => array(
'query' => $skins,
'id' => 'id',
'name' => 'name'
)
),
array(
'type' => 'hook',
'label' => $this->l('Hook'),
'name' => 'hookBox',
'required' => true,
'form_group_class' => 'fieldhide input_association',
'values' => $hooks,
'col' => '9',
),
array(
'type' => 'categories',
'label' => $this->l('Parent category'),
'name' => 'categoryBox',
'tree' => array(
'id' => 'categories-tree',
'selected_categories' => $selected_categories,
'use_checkbox' => true,
'root_category' => $context->shop->getCategory()
),
'col' => '9',
'form_group_class' => 'fieldhide input_association'
),
array(
'type' => 'select',
'label' => $this->l('Type d\'affichage'),
'name' => 'icon',
'required' => FALSE,
'form_group_class' => 'fieldhide input_img',
'options' => array(
'query' => $icons,
'id' => 'id',
'name' => 'name'
)
),
array(
'type' => 'file',
'label' => $this->l('Choisisser une image'),
'name' => 'image',
'form_group_class' => 'fieldhide input_img',
'url' => $image
),
array(
'type' => 'file',
'label' => $this->l('Choisisser une icone'),
'name' => 'image_icon',
'form_group_class' => 'fieldhide input_img',
'url' => $image_icon_url
),
array(
'type' => 'switch',
'label' => $this->l('Actif'),
'name' => 'active',
'required' => false,
'class' => 't',
'form_group_class' => 'fieldhide input_info',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1
),
array(
'id' => 'active_off',
'value' => 0
)
),
),
)
);
if (Shop::isFeatureActive()) {
$this->fields_form['input'][] = array(
'type' => 'shop',
'label' => $this->l('Boutique'),
'name' => 'checkBoxShopAsso',
'form_group_class' => 'fieldhide input_association'
);
}
return parent::renderForm();
}
public function init()
{
parent::init();
if (Shop::getContext() == Shop::CONTEXT_SHOP) {
$this->_join .= ' LEFT JOIN `'._DB_PREFIX_. $this->table .'_shop` sa ON (a.`' . $this->identifier . '` = sa.`' . $this->identifier . '` AND sa.id_shop = '.(int)$this->context->shop->id.') ';
}
if (Shop::getContext() == Shop::CONTEXT_SHOP && Shop::isFeatureActive()) {
$this->_where = ' AND sa.`id_shop` = '.(int)Context::getContext()->shop->id;
}
}
public function postProcess()
{
$id_lang = $this->context->language->id;
// $images = array(
// 'image_icon' => $image_icon_url
// );
// foreach($images as $imageName => $imageName_url)
// {
// if(isset($_FILES[$imageName]) && !empty($_FILES[$imageName]['tmp_name']))
// {
// $obj = $this->loadObject(TRUE);
// $fileTemp = $_FILES[$imageName]['tmp_name'];
// $fileParts = pathinfo($_FILES[$imageName]['name']);
// if(file_exists(_PS_IMG_DIR_.'constructor/image/'.$imageName.'_'.$obj->id.'.png')) {
// unlink(_PS_IMG_DIR_.'constructor/image/'.$imageName.'_'.$obj->id.'.png');
// }
// if($fileParts['extension'] == 'png' || $fileParts['extension'] == 'jpg')
// {
// $res = move_uploaded_file($fileTemp, _PS_IMG_DIR_.'constructor/image/'.$imageName.'_'.$obj->id.'.png');
// if(!$res)
// {
// $this->errors[] = sprintf(Tools::displayError('An error occured during upload of file %s'), $obj->id.'.png');
// }
// else
// {
// $this->confirmations[] = sprintf($this->l('File %s has been uploaded'), _PS_IMG_DIR_.'constructor/image/'.$imageName.'_'.$obj->id.'_'.$id_lang.'.png');
// }
// }
// else
// {
// $this->errors[] = sprintf(Tools::displayError('File %s have not good extension, only .jpg or .png'), $obj->id.'.jpg');
// }
// }
// }
$result = parent::postProcess();
if ( Validate::isLoadedObject($result) ) {
$this->generatePicture($result);
$result->addHooksAssociation(Tools::getValue('hookBox'));
$result->addCategoryAssociation(Tools::getValue('categoryBox'));
$this->redirect_after = self::$currentIndex.'&id_advconstructor='.(int)$result->id.'&update'.$this->table.'&token='.$this->token;
}
return $result;
}
public function generatePicture($object)
{
$languages = Language::getLanguages(false);
foreach ($languages as $language) {
$tabimgs = array(
"image",
"hover",
"image_icon",
);
foreach ( $tabimgs as $tabimg ) {
$namePost = $tabimg.'_'.$language['id_lang'];
if (isset($_FILES[$namePost]) && !empty($_FILES[$namePost]['tmp_name']) ) {
$fileTemp = $_FILES[$namePost]['tmp_name'];
$fileParts = pathinfo($_FILES[$namePost]['name']);
if ( $fileParts['extension'] == 'jpg' || $fileParts['extension'] == 'png' || $fileParts['extension'] == 'gif' ) {
if(!is_dir(_PS_IMG_DIR_.'constructor/'.$tabimg)) {
mkdir(_PS_IMG_DIR_.'constructor/'.$tabimg, 0775);
}
$res = move_uploaded_file($fileTemp, _PS_IMG_DIR_.'constructor/'.$tabimg.'/'. $object->id . '_'.$language['id_lang'].'.'.$fileParts['extension']);
if(!$res)
$this->errors[] = sprintf(Tools::displayError('An error occured during upload of file %s'), $object->id .'.'. $fileParts['extension']);
else
$this->confirmations[] = sprintf($this->l('File %s has been uploaded'), $object->id .'.'. $fileParts['extension']);
} else
$this->errors[] = sprintf(Tools::displayError('File %s have not good extension, only .jpg, .png or .gif'), $object->id .'.'. $fileParts['extension']);
}
}
}
}
protected function updateAssoShop($id_object)
{
if (!Shop::isFeatureActive()) {
return;
}
$assos_data = $this->getSelectedAssoShop($this->table, $id_object);
$exclude_ids = $assos_data;
foreach (Db::getInstance()->executeS('SELECT id_shop FROM ' . _DB_PREFIX_ . 'shop') as $row) {
if (!$this->context->employee->hasAuthOnShop($row['id_shop'])) {
$exclude_ids[] = $row['id_shop'];
}
}
Db::getInstance()->delete($this->table . '_shop', '`' . $this->identifier . '` = ' . (int) $id_object . ($exclude_ids ? ' AND id_shop NOT IN (' . implode(', ', $exclude_ids) . ')' : ''));
$insert = array();
foreach ($assos_data as $id_shop) {
$insert[] = array(
$this->identifier => $id_object,
'id_shop' => (int) $id_shop,
);
}
return Db::getInstance()->insert($this->table . '_shop', $insert, FALSE, TRUE, Db::INSERT_IGNORE);
}
protected function getSelectedAssoShop($table)
{
if (!Shop::isFeatureActive()) {
return array();
}
$shops = Shop::getShops(TRUE, NULL, TRUE);
if (count($shops) == 1 && isset($shops[0])) {
return array($shops[0], 'shop');
}
$assos = array();
if (Tools::isSubmit('checkBoxShopAsso_' . $table)) {
foreach (Tools::getValue('checkBoxShopAsso_' . $table) as $id_shop => $value) {
$assos[] = (int) $id_shop;
}
} else if (Shop::getTotalShops(FALSE) == 1) {
// if we do not have the checkBox multishop, we can have an admin with only one shop and being in multishop
$assos[] = (int) Shop::getContextShopID();
}
return $assos;
}
}