384 lines
14 KiB
PHP
Raw Normal View History

2017-07-06 17:41:10 +02:00
<?php
include_once dirname(__FILE__).'/../../classes/AdvPictoClass.php';
class AdminAdvPictoController extends ModuleAdminController
{
public function __construct()
{
$this->table = 'advpicto';
$this->className = 'AdvPictoClass';
$this->identifier = 'id_advpicto';
$this->lang = true;
$this->deleted = false;
$this->bootstrap = true;
$this->explicitSelect = true;
$this->context = Context::getContext();
$this->_defaultOrderBy = 'position';
$this->position_identifier = 'ID';
$this->fieldImageSettings = array(
'name' => 'image',
'dir' => 'picto'
);
$this->_group = 'GROUP BY a.`id_advpicto`';
parent::__construct();
$this->fields_list = array(
'id_advpicto' => array(
'title' => $this->l('ID'),
'align' => 'center',
'width' => 25,
'filter_key' => 'a!id_advpicto'
),
'title' => array(
'title' => $this->l('Nom')
),
'active' => array(
'title' => $this->l('Activé'),
'active' => 'status',
'filter_key' => 'a!active',
'align' => 'text-center',
'type' => 'bool',
'class' => 'fixed-width-sm',
'orderby' => false
),
'position' => array(
'title' => $this->l('Position'),
'align' => 'center',
'position' => 'position',
'filter_key' => 'a!position'
)
);
$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'] = array(
'href' => self::$currentIndex.'&addadvpicto&token='.$this->token,
'desc' => $this->l('Ajouter un nouveau picto', null, null, false),
'icon' => 'process-icon-new'
);
$this->page_header_toolbar_btn['new_product'] = array(
'href' => $this->context->link->getAdminLink('AdminAdvPictoProduct'),
'desc' => $this->l('Association produit', null, null, false),
'icon' => 'process-icon-compress'
);
}
public function renderForm()
{
if (!($obj = $this->loadObject(true))) {
return;
}
$context = Context::getContext();
if ( Tools::isSubmit('id_advpicto') ) {
$id_advpicto = (int)Tools::getValue('id_advpicto');
$image = __PS_BASE_URI__.'img/picto/'.$id_advpicto.'.jpg' ;
} else {
$image = "noimg" ;
}
$selected_categories = $obj->getCategories();
$image = _PS_IMG_DIR_ . 'picto/' . $obj->id.'.jpg';
$image_url = ImageManager::thumbnail($image, $this->table.'_'.(int)$obj->id.'.'.$this->imageType, 350, $this->imageType, TRUE, TRUE);
$image_size = file_exists($image) ? filesize($image) / 1000 : FALSE;
$this->fields_form = array(
'multilang' => true,
'tinymce' => true,
'legend' => array(
'title' => $this->l('Picto'),
),
'submit' => array(
'name' => 'submitBlock',
'title' => $this->l('Enregistrer')
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Titre'),
'name' => 'title',
'lang' => true,
'required' => true,
'size' => 255
),
array(
'type' => 'text',
'label' => $this->l('Alt'),
'name' => 'alt',
'lang' => true,
'size' => 255
),
array(
'type' => 'file',
'label' => $this->l('Choisir une image'),
'name' => 'image',
'display_image' => TRUE,
'image' => $image_url,
'size' => $image_size,
'delete_url' => self::$currentIndex.'&'.$this->identifier.'='.$this->object->id.'&token='.$this->token.'&deleteImage=1'
),
array(
'type' => 'text',
'label' => $this->l('Lien'),
'name' => 'link',
'lang' => true,
'size' => 255
),
array(
'type' => 'switch',
'label' => $this->l('Lien externe'),
'name' => 'external',
'required' => false,
'is_bool' => true,
'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',
'autoload_rte' => true,
'lang' => true,
'cols' => 60,
'rows' => 120
),
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',
),
array(
'type' => 'switch',
'label' => $this->l('Actif'),
'name' => 'active',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1
),
array(
'id' => 'active_off',
'value' => 0
)
),
),
array(
'type' => 'switch',
'label' => $this->l('Affiché dans sa propre catégorie'),
'name' => 'selfcategory_display',
'required' => false,
'class' => 't',
'is_bool' => true,
'hint' => $this->l('Permet de ne pas affiché le pictogramme dans sa propre catégorie (mais dans les autres catégories associées à un produit par exemple)'),
'values' => array(
array(
'id' => 'selfcategory_display_on',
'value' => 1
),
array(
'id' => 'selfcategory_display_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()
{
if (Tools::getValue('deleteImage')) {
$this->processForceDeleteImage();
}
$result = parent::postProcess();
if ( Validate::isLoadedObject($result) ) {
$this->generatePicture($result);
$result->addCategoryAssociation(Tools::getValue('categoryBox'));
}
return $result;
}
public function processForceDeleteImage()
{
$picto = $this->loadObject(TRUE);
if (Validate::isLoadedObject($picto))
{
$picto->deleteImage(TRUE);
}
}
public function generatePicture($object)
{
$namePost = 'image';
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' ) {
if(!is_dir(_PS_IMG_DIR_.'picto')) {
mkdir(_PS_IMG_DIR_.'picto', 0775);
}
$res = move_uploaded_file($fileTemp, _PS_IMG_DIR_.'picto/' . $object->id . '.jpg');
if(!$res)
$this->errors[] = sprintf(Tools::displayError('An error occured during upload of file %s'), $object->id . '.jpg');
else
$this->confirmations[] = sprintf($this->l('File %s has been uploaded'), $object->id . '.jpg');
} else
$this->errors[] = sprintf(Tools::displayError('File %s have not good extension, only .jpg or .png'), $object->id . '.jpg');
}
}
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;
}
public function ajaxProcessUpdatePositions()
{
$way = (int)(Tools::getValue('way'));
$id = (int)(Tools::getValue('id'));
$positions = Tools::getValue('advpicto');
$page = (int)Tools::getValue('page');
$selected_pagination = (int)Tools::getValue('selected_pagination');
$obj = 'position';
if ( is_array($positions) ) {
foreach ($positions as $position => $value) {
$pos = explode('_', $value);
if (isset($pos[2]) && (int)$pos[2] === $id) {
$hook_position = new AdvPictoClass((int)$pos[2]);
if (isset($position) && $hook_position->updatePosition( $way, $position ) ) {
die(true);
} else {
die('{"hasError" : true, errors : "Cannot update blocks position"}');
}
}
}
} else {
die('{"hasError" : true, errors : "Cannot update blocks position"}');
}
}
}