247 lines
8.0 KiB
PHP
Raw Normal View History

2015-09-23 16:57:43 +02:00
<?php
include_once dirname(__FILE__).'/../../classes/AdvMenuLink.php';
class AdminAdvMenuController extends ModuleAdminController {
public function __construct() {
$this->table = 'advmenu';
$this->className = 'AdvMenuLink';
$this->identifier = 'id_link';
$this->lang = TRUE;
$this->deleted = FALSE;
$this->bootstrap = TRUE;
$this->fieldImageSettings = array(
'name' => 'image',
'dir' => 'menu'
);
$this->position_identifier = 'id_link';
$this->_defaultOrderBy = 'position';
parent::__construct();
$this->actions = array('view', 'edit', 'delete');
$this->fields_list = array(
'id_link' => array(
'title' => 'ID',
'width' => 25
),
'title' => array(
'title' => $this->module->l('Titre'),
'width' => 45,
),
'url' => array(
'title' => $this->module->l('Url'),
'width' => 45,
),
'position' => array(
'title' => $this->l('Position'),
'align' => 'center',
'position' => 'position',
'filter_key' => 'a!position'
)
);
if (Tools::getValue('id_link')) {
$this->_where = ' AND a.`id_parent` =' . (int)Tools::getValue('id_link');
} else {
$this->_where = ' AND a.`id_parent` = 0';
}
if (Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_ALL){
$this->_join .= 'JOIN `'._DB_PREFIX_.'advmenu_shop` as ashop ON a.`id_link` = ashop.`id_link` AND ashop.`id_shop` IN ('.implode(', ', Shop::getContextListShopID()).') ';
$this->_group .= 'GROUP BY ashop.`id_link`';
}
}
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_link').'&addadvmenu&token='.$this->token,
'desc' => $this->l('Ajouter un nouveau lien', 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' => 'submitAdvMenu',
'title' => $this->l('Save'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Nom'),
'name' => 'title',
'required' => TRUE,
'lang' => TRUE,
'size' => 114
),
array(
'type' => 'text',
'label' => $this->l('Lien'),
'name' => 'url',
'lang' => TRUE,
'required' => FALSE,
'size' => 114
),
array(
'type' => 'hidden',
'name' => 'id_parent',
),
array(
'type' => 'textarea',
'label' => $this->l('Description'),
'name' => 'text',
'autoload_rte' => TRUE,
'cols' => 100,
'rows' => 6,
'lang' => TRUE,
),
array(
'type' => 'select',
'label' => $this->l('Type d\'affichage'),
'name' => 'classbloc',
'required' => FALSE,
'options' => array(
'query' => array(
array('id' => '', 'name' => 'Aucune'),
array('id' => 'arrow', 'name' => 'Flèche'),
array('id' => 'menu-subtitle', 'name' => 'Titre'),
),
'id' => 'id',
'name' => 'name'
)
),
array(
'type' => 'shop',
'label' => $this->l('Shop'),
'form_group_class' => 'fieldhide input_association',
'name' => 'checkBoxShopAsso_advmenu'
)
)
);
if (Tools::getValue('id_parent')) {
$this->fields_value = array(
'id_parent' => (int)Tools::getValue('id_parent')
);
}
else
{
$obj = $this->loadObject(TRUE);
$image = FALSE;
$image_url = '';
$image_size = '';
if($obj)
{
$image = _PS_IMG_DIR_ . 'menu/' . $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 (360x360)'),
'name' => 'image',
'display_image' => TRUE,
'lang' => TRUE,
'image' => $image_url,
'size' => $image_size,
'delete_url' => self::$currentIndex.'&'.$this->identifier.'='.$this->object->id.'&token='.$this->token.'&deleteImage=1'
);
}
return parent::renderForm();
}
protected function copyFromPost(&$object, $table) {
parent::copyFromPost($object, $table);
if(Shop::isFeatureActive())
{
$object->id_shop_list = array();
foreach (Tools::getValue('checkBoxShopAsso_advmenu') as $id_shop => $value)
$object->id_shop_list[] = $id_shop;
}
}
public function postProcess() {
if (Tools::getValue('deleteImage')) {
$this->processForceDeleteImage();
}
return parent::postProcess();
}
public function processForceDeleteImage() {
$link = $this->loadObject(TRUE);
if (Validate::isLoadedObject($link))
{
$link->deleteImage(TRUE);
}
}
protected function postImage($id) {
$ret = parent::postImage($id);
if (isset($_FILES) && count($_FILES) && $_FILES['image']['name'] != NULL && !empty($this->object->id) )
{
return TRUE;
}
return TRUE;
}
public function ajaxProcessUpdatePositions()
{
$way = (int)(Tools::getValue('way'));
$id = (int)(Tools::getValue('id'));
$positions = Tools::getValue('link');
$obj = 'advmenu';
if (is_array($positions)){
foreach ($positions as $position => $value)
{
$pos = explode('_', $value);
if (isset($pos[2]) && (int)$pos[2] === $id)
{
$menu_obj = new AdvMenuLink((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;
}
}
}
}
}