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

355 lines
10 KiB
PHP

<?php
include_once dirname(__FILE__).'/../../classes/AdvSlide2.php';
class AdminAdvSlider2Controller extends ModuleAdminController {
public function __construct() {
$this->table = 'advslider2';
$this->className = 'AdvSlide2';
$this->identifier = 'id_slide';
$this->lang = TRUE;
$this->deleted = FALSE;
$this->bootstrap = TRUE;
$this->fieldImageSettings = array(
'name' => 'image',
'dir' => 'slider2'
);
$this->position_identifier = 'id_slide';
$this->_defaultOrderBy = 'position';
parent::__construct();
$this->actions = array('edit', 'delete');
$this->fields_list = array(
'id_slide' => array(
'title' => 'ID',
'width' => 25
),
'image_desktop' => array(
'title' => $this->module->l('Image'),
'image' => $this->fieldImageSettings['dir'],
'width' => 75
),
'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_.'advslider2_shop` as ashop ON a.`id_slide` = ashop.`id_slide` AND ashop.`id_shop` IN ('.implode(', ', Shop::getContextListShopID()).') ';
$this->_group .= 'GROUP BY ashop.`id_slide`';
}
}
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_slide').'&addadvslider&token='.$this->token,
'desc' => $this->l('Ajouter une nouvelle slide', NULL, NULL, FALSE),
'icon' => 'process-icon-new'
);
}
}
public function renderView() {
return $this->renderList();
}
public function renderForm() {
$this->fields_form = array(
'tinymce' => TRUE,
'legend' => array(
'title' => $this->className,
),
'submit' => array(
'name' => 'submitAdvSlider',
'title' => $this->l('Save'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Titre'),
'name' => 'title',
'lang' => TRUE,
),
array(
'type' => 'text',
'label' => $this->l('Sous-titre'),
'name' => 'subtitle',
'lang' => TRUE,
),
array(
'type' => 'text',
'label' => $this->l('Label lien'),
'name' => 'label',
'lang' => TRUE
),
array(
'type' => 'text',
'label' => $this->l('Lien'),
'name' => 'url',
'lang' => TRUE
),
array(
'type' => 'textarea',
'label' => $this->l('Description'),
'name' => 'description',
'autoload_rte' => TRUE,
'lang' => TRUE
),
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_advslider'
)
)
);
$obj = $this->loadObject(TRUE);
$image = FALSE;
$image_url = '';
$image_size = '';
$image_icon = FALSE;
$image_url_icon = '';
$image_size_icon = '';
$image_mobile = FALSE;
$image_url_mobile = '';
$image_size_mobile = '';
if($obj)
{
$image = _PS_IMG_DIR_ . 'slider2/' . $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;
$image_icon = _PS_IMG_DIR_ . 'slider2/' . $obj->id.'-image_icon.png';
$image_url_icon = ImageManager::thumbnail($image_icon, $this->table.'_'.(int)$obj->id.'-image_icon.'.$this->imageType, 350, $this->imageType, TRUE, TRUE);
$image_size_icon = file_exists($image_icon) ? filesize($image_icon) / 1000 : FALSE;
$image_mobile = _PS_IMG_DIR_ . 'slider2/' . $obj->id.'-image_mobile.jpg';
$image_url_mobile = ImageManager::thumbnail($image_mobile, $this->table.'_'.(int)$obj->id.'-image_mobile.'.$this->imageType, 350, $this->imageType, TRUE, TRUE);
$image_size_mobile = file_exists($image_mobile) ? filesize($image_mobile) / 1000 : FALSE;
}
$this->fields_form['input'][] = array(
'type' => 'file',
'label' => $this->l('Image'),
'name' => 'image',
'display_image' => TRUE,
'lang' => TRUE,
'required' => TRUE,
'image' => $image_url,
'size' => $image_size,
'delete_url' => self::$currentIndex.'&'.$this->identifier.'='.$this->object->id.'&token='.$this->token.'&deleteImage=1'
);
$this->fields_form['input'][] = array(
'type' => 'file',
'label' => $this->l('Icon'),
'name' => 'image_icon',
'display_image' => TRUE,
'lang' => TRUE,
'required' => TRUE,
'image' => $image_url_icon,
'size' => $image_size_icon,
'delete_url' => self::$currentIndex.'&'.$this->identifier.'='.$this->object->id.'&token='.$this->token.'&deleteImage=1&imgName=image_icon'
);
$this->fields_form['input'][] = array(
'type' => 'file',
'label' => $this->l('Image Mobile 500x500'),
'name' => 'image_mobile',
'display_image' => TRUE,
'lang' => TRUE,
'required' => TRUE,
'image' => $image_url_mobile,
'size' => $image_size_mobile,
'delete_url' => self::$currentIndex.'&'.$this->identifier.'='.$this->object->id.'&token='.$this->token.'&deleteImage=1&imgName=image_mobile'
);
return parent::renderForm();
}
protected function copyFromPost(&$object, $table) {
parent::copyFromPost($object, $table);
if(Shop::isFeatureActive())
{
$object->id_shop_list = array();
foreach (Tools::getValue('checkBoxShopAsso_advslider') as $id_shop => $value)
$object->id_shop_list[] = $id_shop;
}
}
public function postProcess() {
if (Tools::getValue('deleteImage')) {
$this->processForceDeleteImage();
$this->refreshPreview();
$images = array('image_icon', 'image_mobile');
foreach($images as $imageName)
{ $obj = $this->loadObject(TRUE);
if(trim(Tools::getValue('imgName')) == trim($imageName)) {
if($imageName == 'image_icon') {
unlink(_PS_IMG_DIR_.'slider2/'.$obj->id.'-'.$imageName.'.png');
} else {
unlink(_PS_IMG_DIR_.'slider2/'.$obj->id.'-'.$imageName.'.jpg');
}
}
}
}
parent::postProcess();
$images = array('image_icon', 'image_mobile');
foreach($images as $imageName)
{
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_.'slider2/'.$obj->id.'-'.$imageName.'.jpg'))
{
unlink(_PS_IMG_DIR_.'slider2/'.$obj->id.'-'.$imageName.'.jpg');
}
if($fileParts['extension'] == 'jpg')
{
$res = move_uploaded_file($fileTemp, _PS_IMG_DIR_.'slider2/'.$obj->id.'-'.$imageName.'.jpg');
if(!$res)
{
$this->errors[] = sprintf(Tools::displayError('An error occured during upload of file %s'), $obj->id.'.jpg');
}
else
{
$this->confirmations[] = sprintf($this->l('File %s has been uploaded'), $obj->id.'.jpg');
}
} elseif($fileParts['extension'] == 'png')
{
$res = move_uploaded_file($fileTemp, _PS_IMG_DIR_.'slider2/'.$obj->id.'-'.$imageName.'.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'), $obj->id.'.png');
}
}
else
{
$this->errors[] = sprintf(Tools::displayError('File %s have not good extension, only .jpg or .png'), $obj->id.'.jpg');
}
}
}
}
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_.'advslider2_mini_'.$this->object->id_slide.'_'.$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('slide');
$obj = 'advslider2';
if (is_array($positions)){
foreach ($positions as $position => $value)
{
$pos = explode('_', $value);
if (isset($pos[2]) && (int)$pos[2] === $id)
{
$menu_obj = new AdvSlide2((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;
}
}
}
}
}