chocolatdemariage/www/override/controllers/admin/AdminCategoriesController.php
2017-09-04 16:18:00 +02:00

401 lines
18 KiB
PHP

<?php
class AdminCategoriesController extends AdminCategoriesControllerCore
{
public function renderForm()
{
$this->initToolbar();
/** @var Category $obj */
$obj = $this->loadObject(true);
$context = Context::getContext();
$id_shop = $context->shop->id;
$selected_categories = array((isset($obj->id_parent) && $obj->isParentCategoryAvailable($id_shop))? (int)$obj->id_parent : (int)Tools::getValue('id_parent', Category::getRootCategory()->id));
$unidentified = new Group(Configuration::get('PS_UNIDENTIFIED_GROUP'));
$guest = new Group(Configuration::get('PS_GUEST_GROUP'));
$default = new Group(Configuration::get('PS_CUSTOMER_GROUP'));
$unidentified_group_information = sprintf($this->l('%s - All people without a valid customer account.'), '<b>'.$unidentified->name[$this->context->language->id].'</b>');
$guest_group_information = sprintf($this->l('%s - Customer who placed an order with the guest checkout.'), '<b>'.$guest->name[$this->context->language->id].'</b>');
$default_group_information = sprintf($this->l('%s - All people who have created an account on this site.'), '<b>'.$default->name[$this->context->language->id].'</b>');
if (!($obj = $this->loadObject(true))) {
return;
}
$image = _PS_CAT_IMG_DIR_.$obj->id.'.'.$this->imageType;
$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;
$images_types = ImageType::getImagesTypes('categories');
$format = array();
$thumb = $thumb_url = '';
$formated_category= ImageType::getFormatedName('category');
$formated_medium = ImageType::getFormatedName('medium');
foreach ($images_types as $k => $image_type) {
if ($formated_category == $image_type['name']) {
$format['category'] = $image_type;
} elseif ($formated_medium == $image_type['name']) {
$format['medium'] = $image_type;
$thumb = _PS_CAT_IMG_DIR_.$obj->id.'-'.$image_type['name'].'.'.$this->imageType;
if (is_file($thumb)) {
$thumb_url = ImageManager::thumbnail($thumb, $this->table.'_'.(int)$obj->id.'-thumb.'.$this->imageType, (int)$image_type['width'], $this->imageType, true, true);
}
}
}
$image_homeimg = _PS_CAT_IMG_DIR_.'home/'.$obj->id.'.jpg';
$image_homeimg_url = ImageManager::thumbnail($image_homeimg, $this->table.'_homeimg_'.(int)$obj->id.'.'.$this->imageType, 350, $this->imageType, true, true);
$image_iconcat = _PS_CAT_IMG_DIR_.'iconcat/'.$obj->id.'.jpg';
$image_iconcat_url = ImageManager::thumbnail($image_homeimg, $this->table.'_iconcat_'.(int)$obj->id.'.'.$this->imageType, 350, $this->imageType, true, true);
$image_univers = _PS_CAT_IMG_DIR_.'univers/'.$obj->id.'.jpg';
$image_univers_url = ImageManager::thumbnail($image_univers, $this->table.'_univers_'.(int)$obj->id.'.'.$this->imageType, 350, $this->imageType, true, true);
$skins = array();
foreach ( explode(',', Configuration::get('ADVCONSTRUCTOR_TYPE')) as $skin ) {
$skins[] = array(
'id' => $skin,
'name' => $skin
);
}
$categories_univers_list = 'doublecube, vrac, pochette_transparente, simplecube, sachetorganza, pochette, coffret, aurore_chocolats, pyramide';
$categories_univers = array();
foreach ( explode(',', $categories_univers_list) as $cat ) {
$categories_univers[] = array(
'id' => $cat,
'name' => $cat,
);
}
$this->fields_form = array(
'tinymce' => true,
'legend' => array(
'title' => $this->l('Category'),
'icon' => 'icon-tags'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Name'),
'name' => 'name',
'lang' => true,
'required' => true,
'class' => 'copy2friendlyUrl',
'hint' => $this->l('Invalid characters:').' <>;=#{}',
),
array(
'type' => 'switch',
'label' => $this->l('Displayed'),
'name' => 'active',
'required' => false,
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
)
),
array(
'type' => 'switch',
'label' => $this->l('Affichée sur l\'accueil ?'),
'name' => 'home',
'required' => false,
'is_bool' => true,
'default' => 1,
'values' => array(
array(
'id' => 'home_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'home_off',
'value' => 0,
'label' => $this->l('Disabled')
)
)
),
array(
'type' => 'categories',
'label' => $this->l('Parent category'),
'name' => 'id_parent',
'tree' => array(
'id' => 'categories-tree',
'selected_categories' => $selected_categories,
'disabled_categories' => (!Tools::isSubmit('add'.$this->table) && !Tools::isSubmit('submitAdd'.$this->table)) ? array($this->_category->id) : null,
'root_category' => $context->shop->getCategory()
)
),
array(
'type' => 'textarea',
'label' => $this->l('Description'),
'name' => 'description',
'autoload_rte' => true,
'lang' => true,
'hint' => $this->l('Invalid characters:').' <>;=#{}'
),
array(
'type' => 'textarea',
'label' => $this->l('Description longue'),
'name' => 'long_description',
'autoload_rte' => true,
'lang' => true,
'hint' => $this->l('Invalid characters:').' <>;=#{}'
),
array(
'type' => 'file',
'label' => $this->l('Category Cover Image'),
'name' => 'image',
'display_image' => true,
'image' => $image_url ? $image_url : false,
'size' => $image_size,
'delete_url' => self::$currentIndex.'&'.$this->identifier.'='.$this->_category->id.'&token='.$this->token.'&deleteImage=1',
'hint' => $this->l('This is the main image for your category, displayed in the category page. The category description will overlap this image and appear in its top-left corner.'),
'format' => $format['category']
),
array(
'type' => 'file',
'label' => $this->l('Image accueil'),
'name' => 'homeimg',
'display_image' => true,
'image' => $image_homeimg_url ? $image_homeimg_url : false,
'delete_url' => self::$currentIndex.'&'.$this->identifier.'='.$this->_category->id.'&token='.$this->token.'&deleteImage=1&folder=home',
'hint' => $this->l('Upload a category image from your computer.'),
),
array(
'type' => 'select',
'label' => $this->l('Icone category'),
'name' => 'liste_icons_univers',
'hint' => $this->l('Choose a category'),
'options' => array(
'query' => $categories_univers,
'id' => 'id',
'name' => 'name',
)
),
array(
'type' => 'file',
'label' => $this->l('Icon'),
'name' => 'iconcat',
'display_image' => true,
'image' => $image_iconcat_url ? $image_iconcat_url : false,
'delete_url' => self::$currentIndex.'&'.$this->identifier.'='.$this->_category->id.'&token='.$this->token.'&deleteImage=1&folder=iconcat',
'hint' => $this->l('Upload a category image from your computer.'),
),
array(
'type' => 'file',
'label' => $this->l('Image dans le listing Univers'),
'name' => 'univers',
'display_image' => true,
'image' => $image_univers_url ? $image_univers_url : false,
'delete_url' => self::$currentIndex.'&'.$this->identifier.'='.$this->_category->id.'&token='.$this->token.'&deleteImage=1&folder=home',
'hint' => $this->l('Upload a category image from your computer.'),
),
array(
'type' => 'textarea',
'label' => $this->l('Meta title'),
'name' => 'meta_title',
'lang' => true,
'rows' => 5,
'cols' => 100,
'hint' => $this->l('Forbidden characters:').' <>;=#{}'
),
array(
'type' => 'textarea',
'label' => $this->l('Meta description'),
'name' => 'meta_description',
'lang' => true,
'rows' => 5,
'cols' => 100,
'hint' => $this->l('Forbidden characters:').' <>;=#{}'
),
array(
'type' => 'tags',
'label' => $this->l('Meta keywords'),
'name' => 'meta_keywords',
'lang' => true,
'hint' => $this->l('To add "tags," click in the field, write something, and then press "Enter."').'&nbsp;'.$this->l('Forbidden characters:').' <>;=#{}'
),
array(
'type' => 'text',
'label' => $this->l('Friendly URL'),
'name' => 'link_rewrite',
'lang' => true,
'required' => true,
'hint' => $this->l('Only letters, numbers, underscore (_) and the minus (-) character are allowed.')
),
array(
'type' => 'text',
'label' => $this->l('URL spécifique'),
'name' => 'specific_url',
'lang' => true,
'hint' => $this->l('URL de la destination')
),
array(
'type' => 'group',
'label' => $this->l('Group access'),
'name' => 'groupBox',
'values' => Group::getGroups(Context::getContext()->language->id),
'info_introduction' => $this->l('You now have three default customer groups.'),
'unidentified' => $unidentified_group_information,
'guest' => $guest_group_information,
'customer' => $default_group_information,
'hint' => $this->l('Mark all of the customer groups which you would like to have access to this category.')
),
array(
'type' => 'textarea',
'label' => $this->l('Description dans le listing Univers'),
'name' => 'description_univers',
'autoload_rte' => true,
'lang' => true,
'hint' => $this->l('Invalid characters:').' <>;=#{}'
),
array(
'type' => 'text',
'label' => $this->l('URL du produit exclusif'),
'name' => 'linkExclusive',
'lang' => true,
'hint' => $this->l('Only letters, numbers, underscore (_) and the minus (-) character are allowed.'),
),
),
'submit' => array(
'title' => $this->l('Save'),
'name' => 'submitAdd'.$this->table.($this->_category->is_root_category && !Tools::isSubmit('add'.$this->table) && !Tools::isSubmit('add'.$this->table.'root') ? '': 'AndBackToParent')
),
);
$this->tpl_form_vars['shared_category'] = Validate::isLoadedObject($obj) && $obj->hasMultishopEntries();
$this->tpl_form_vars['PS_ALLOW_ACCENTED_CHARS_URL'] = (int)Configuration::get('PS_ALLOW_ACCENTED_CHARS_URL');
$this->tpl_form_vars['displayBackOfficeCategory'] = Hook::exec('displayBackOfficeCategory');
// Display this field only if multistore option is enabled
if (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE') && Tools::isSubmit('add'.$this->table.'root')) {
$this->fields_form['input'][] = array(
'type' => 'switch',
'label' => $this->l('Root Category'),
'name' => 'is_root_category',
'required' => false,
'is_bool' => true,
'values' => array(
array(
'id' => 'is_root_on',
'value' => 1,
'label' => $this->l('Yes')
),
array(
'id' => 'is_root_off',
'value' => 0,
'label' => $this->l('No')
)
)
);
unset($this->fields_form['input'][2], $this->fields_form['input'][3]);
}
// Display this field only if multistore option is enabled AND there are several stores configured
if (Shop::isFeatureActive()) {
$this->fields_form['input'][] = array(
'type' => 'shop',
'label' => $this->l('Shop association'),
'name' => 'checkBoxShopAsso',
);
}
// remove category tree and radio button "is_root_category" if this category has the root category as parent category to avoid any conflict
if ($this->_category->id_parent == (int)Configuration::get('PS_ROOT_CATEGORY') && Tools::isSubmit('updatecategory')) {
foreach ($this->fields_form['input'] as $k => $input) {
if (in_array($input['name'], array('id_parent', 'is_root_category'))) {
unset($this->fields_form['input'][$k]);
}
}
}
if (!($obj = $this->loadObject(true))) {
return;
}
// Added values of object Group
$category_groups_ids = $obj->getGroups();
$groups = Group::getGroups($this->context->language->id);
// if empty $carrier_groups_ids : object creation : we set the default groups
if (empty($category_groups_ids)) {
$preselected = array(Configuration::get('PS_UNIDENTIFIED_GROUP'), Configuration::get('PS_GUEST_GROUP'), Configuration::get('PS_CUSTOMER_GROUP'));
$category_groups_ids = array_merge($category_groups_ids, $preselected);
}
foreach ($groups as $group) {
$this->fields_value['groupBox_'.$group['id_group']] = Tools::getValue('groupBox_'.$group['id_group'], (in_array($group['id_group'], $category_groups_ids)));
}
$this->fields_value['is_root_category'] = (bool)Tools::isSubmit('add'.$this->table.'root');
return AdminController::renderForm();
}
public function postProcess() {
parent::postProcess();
$images = array(
'banner' => 'banners',
'univers' => 'univers',
'homeimg' => 'home',
'iconcat' => 'iconcat'
);
foreach($images as $imageName => $folder)
{
if(isset($_FILES[$imageName]) && !empty($_FILES[$imageName]['tmp_name']))
{
$obj = $this->loadObject(TRUE);
$fileTemp = $_FILES[$imageName]['tmp_name'];
$fileParts = pathinfo($_FILES[$imageName]['name']);
if($fileParts['extension'] == 'jpg' || $fileParts['extension'] == 'png')
{
if(!is_dir(_PS_CAT_IMG_DIR_.$folder))
mkdir(_PS_CAT_IMG_DIR_.$folder, 0775);
$res = move_uploaded_file($fileTemp, _PS_CAT_IMG_DIR_.$folder.'/'.$obj->id.'.jpg');
if(!$res)
{
$this->errors[] = sprintf(Tools::displayError('An error occured during upload of file %s'), $obj->id.'.' . $fileParts['extension']);
}
else
{
$this->confirmations[] = sprintf($this->l('File %s has been uploaded'), $obj->id.'.' . $fileParts['extension']);
}
}
else
{
$this->errors[] = sprintf(Tools::displayError('File %s have not good extension, only .jpg or .png'), $obj->id.'.' . $fileParts['extension']);
}
}
}
}
}