2016-06-08 17:36:29 +02:00

281 lines
6.8 KiB
PHP

<?php
include_once dirname(__FILE__).'/../../classes/AdvManu.php';
class AdminAdvManufacturerController extends ModuleAdminController
{
public function __construct()
{
$this->table = 'advmanufacturer';
$this->className = 'AdvManu';
$this->identifier = 'id_advmanu';
$this->lang = TRUE;
$this->deleted = FALSE;
$this->bootstrap = TRUE;
$this->fieldImageSettings = array(
'name' => 'image',
'dir' => 'manufacturer'
);
$this->position_identifier = 'id_advmanu';
$this->_defaultOrderBy = 'position';
parent::__construct();
$this->actions = array('edit', 'delete');
$this->fields_list = array(
'id_advmanu' => array(
'title' => 'ID',
'width' => 25
),
'id_brand' => array(
'title' => 'Marque',
'width' => 25,
'callback' => 'getBrandName'
),
'title' => array(
'title' => $this->module->l('Titre'),
),
'url' => array(
'title' => $this->module->l('Url'),
'width' => 45,
),
'position' => array(
'title' => $this->l('Position'),
'align' => 'center',
'position' => 'position',
'filter_key' => 'a!position'
)
);
if (Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_ALL){
$this->_join .= 'JOIN `'._DB_PREFIX_.'advmanufacturer_shop` as ashop ON a.`id_advmanu` = ashop.`id_advmanu` AND ashop.`id_shop` IN ('.implode(', ', Shop::getContextListShopID()).') ';
$this->_group .= 'GROUP BY ashop.`id_advmanu`';
}
}
public static function getBrandName($id_brand)
{
if ($id_brand != 0)
{
return Db::getInstance()->getValue('SELECT name FROM ' . _DB_PREFIX_ . 'manufacturer WHERE id_manufacturer = ' . $id_brand);;
}
else
{
return '--';
}
}
public function initPageHeaderToolbar()
{
parent::initPageHeaderToolbar();
if ($this->display != 'edit' && $this->display != 'add') {
$this->page_header_toolbar_btn['new_link'] = array(
'href' => self::$currentIndex.'&id_parent='.(int)Tools::getValue('id_advmanu').'&addadvmanufacturer&token='.$this->token,
'desc' => $this->l('Ajouter une marque', NULL, NULL, FALSE),
'icon' => 'process-icon-new'
);
}
}
public function renderView()
{
return $this->renderList();
}
public function renderForm()
{
$brands = AdvManu::getAllBrands();
$brands = array_merge($brands, array(array('id' => 0, 'name' => '-- Slide lié à aucune marque --')));
$this->fields_form = array(
'tinymce' => TRUE,
'legend' => array(
'title' => $this->className,
),
'submit' => array(
'name' => 'submitAdvManufacturer',
'title' => $this->l('Save'),
),
'input' => array(
array(
'type' => 'select',
'label' => $this->l('Marque'),
'name' => 'id_brand',
'options' => array(
'query' => $brands,
'id' => 'id',
'name' => 'name'
)
),
array(
'type' => 'switch',
'label' => $this->l('Lien vers la page marque ?'),
'name' => 'link',
'required' => FALSE,
'is_bool' => TRUE,
'default' => 1,
'form_group_class' => 'switch-link',
'values' => array(
array(
'id' => 'link_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'link_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'text',
'label' => $this->l('Lien'),
'name' => 'url',
'lang' => TRUE,
'form_group_class' => 'group-link',
'class' => 'link'
),
array(
'type' => 'text',
'label' => $this->l('Titre'),
'name' => 'title',
'lang' => TRUE,
'form_group_class' => 'customization',
'class' => 'field-customization'
),
array(
'type' => 'switch',
'label' => $this->l('Activé'),
'name' => 'active',
'required' => FALSE,
'is_bool' => TRUE,
'default' => 1,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'shop',
'label' => $this->l('Shop'),
'form_group_class' => 'fieldhide input_association',
'name' => 'checkBoxShopAsso_advmanufacturer'
)
)
);
$obj = $this->loadObject(TRUE);
$image = FALSE;
$image_url = '';
$image_size = '';
if($obj)
{
$image = _PS_IMG_DIR_ . 'manufacturer/' . $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['input'][] = array(
'type' => 'file',
'label' => $this->l('Image'),
'name' => 'image',
'display_image' => TRUE,
'lang' => TRUE,
'image' => $image_url,
'size' => $image_size,
'hint' => 'Si pas d\'image, celle de la marque par défaut sera utilisée',
'delete_url' => self::$currentIndex.'&'.$this->identifier.'='.$this->object->id.'&token='.$this->token.'&deleteImage=1'
);
return parent::renderForm();
}
public function postProcess()
{
if (Tools::getValue('deleteImage')) {
$this->processForceDeleteImage();
$this->refreshPreview();
}
parent::postProcess();
}
public function processForceDeleteImage()
{
$link = $this->loadObject(TRUE);
if (Validate::isLoadedObject($link))
{
$link->deleteImage(TRUE);
}
}
protected function postImage($id) {
$ret = parent::postImage($id);
$this->refreshPreview();
if (isset($_FILES) && count($_FILES) && $_FILES['image']['name'] != NULL && !empty($this->object->id) )
{
return TRUE;
}
return TRUE;
}
public function refreshPreview()
{
$current_preview = _PS_TMP_IMG_DIR_.'advmanufacturer_mini_'.$this->object->id_advmanu.'_'.$this->context->shop->id.'.jpg';
if (file_exists($current_preview)) {
unlink($current_preview);
}
}
public function ajaxProcessUpdatePositions()
{
$way = (int)(Tools::getValue('way'));
$id = (int)(Tools::getValue('id'));
$positions = Tools::getValue('advmanu');
$obj = 'advmanufacturer';
if (is_array($positions)){
foreach ($positions as $position => $value)
{
$pos = explode('_', $value);
if (isset($pos[2]) && (int)$pos[2] === $id)
{
$menu_obj = new AdvManu((int)$pos[2]);
if (Validate::isLoadedObject($menu_obj))
if (isset($position) && $menu_obj->updatePosition($way, $position))
{
echo 'ok position '.(int)$position.' for '.$obj.' '.(int)$pos[2]."\r\n";
}
else
echo '{"hasError" : true, "errors" : "Can not update '.$obj.' '.(int)$id.' to position '.(int)$position.' "}';
else
echo '{"hasError" : true, "errors" : "This '.$obj.' ('.(int)$id.') cannot be loaded"}';
break;
}
}
}
}
}