toutpratique/modules/cmsps/controllers/admin/AdminCmsPsCategories.php
Marion Muszynski 1014c59941 blocknewsletter
2015-09-23 12:45:32 +02:00

315 lines
8.5 KiB
PHP

<?php
include_once dirname(__FILE__).'/../../cmsps.php';
class AdminCmsPsCategoriesController extends ModuleAdminController {
public function __construct() {
$this->table = 'cmsps_categories';
$this->className = 'CmsPsCategory';
$this->identifier = 'id_category';
$this->lang = TRUE;
$this->bootstrap = TRUE;
$this->context = Context::getContext();
$this->fieldImageSettings = array(
'name' => 'image',
'dir' => 'cms_c'
);
parent::__construct();
if (Tools::getValue('id_category')) {
$this->_where = ' AND a.`id_parent` =' . (int)Tools::getValue('id_category');
} else {
$this->_where = ' AND a.`id_parent` = 0';
}
$this->actions = array('view', 'edit','delete');
$this->fields_list = array(
'id_category' => array(
'title' => 'ID',
'width' => 25
),
'title' => array(
'title' => $this->module->l('Title'),
'width' => 45,
),
'slug' => array(
'title' => $this->module->l('Alias'),
'width' => 45,
),
'active' => array(
'title' => $this->l('Active'),
'active' => 'status',
'type' => 'bool',
'class' => 'fixed-width-xs',
'align' => 'center',
'ajax' => true,
'orderby' => false
)
);
if (Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_ALL){
$this->_join .= 'JOIN `'._DB_PREFIX_.'cmsps_categories_shop` acs ON a.`id_category` = acs.`id_category` AND acs.`id_shop` IN ('.implode(', ', Shop::getContextListShopID()).') ';
$this->_group .= 'GROUP BY acs.`id_category`';
}
}
public function initPageHeaderToolbar() {
parent::initPageHeaderToolbar();
if ($this->display != 'edit' && $this->display != 'add') {
$this->page_header_toolbar_btn['new_sales'] = array(
'href' => self::$currentIndex.'&addcmsps_categories&token='.$this->token,
'desc' => $this->l('Add new Category', NULL, NULL, FALSE),
'icon' => 'process-icon-new'
);
}
}
public function renderForm() {
if (!($obj = $this->loadObject(TRUE)))
return;
$image = '';
$image_url = '';
$image_size = '';
if(file_exists(_CMS_CAT_IMG_DIR_.$obj->id.'.jpg')) {
$image = _CMS_CAT_IMG_DIR_.$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->className,
),
'submit' => array(
'name' => 'submitCMSPSCategory',
'title' => $this->l('Save'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Title'),
'name' => 'title',
'lang' => TRUE,
'required' => TRUE,
'size' => 114
),
array(
'type' => 'text',
'label' => $this->l('Slug'),
'name' => 'slug',
'lang' => TRUE,
'required' => TRUE,
'size' => 114
),
array(
'type' => 'select',
'label' => $this->l('Parent Category'),
'name' => 'id_parent',
'required' => TRUE,
'options' => array(
'query' => $this->getParentPossibility(),
'id' => 'id',
'name' => 'name'
)
),
array(
'type' => 'textarea',
'label' => $this->l('Texte libre'),
'name' => 'description',
'autoload_rte' => TRUE,
'cols' => 100,
'rows' => 6,
'lang' => TRUE,
),
array(
'type' => 'file',
'label' => $this->l('Image'),
'name' => 'image',
'display_image' => true,
'image' => $image_url ? $image_url : false,
'size' => $image_size,
'delete_url' => self::$currentIndex.'&'.$this->identifier.'='.$this->object->id.'&token='.$this->token.'&deleteImage=1',
),
array(
'type' => 'file',
'label' => $this->l('PDF (newsletter)'),
'name' => 'pdf',
'hint' => $this->l('Upload a pdf from your computer.'),
),
array(
'type' => 'text',
'label' => $this->l('Meta title'),
'name' => 'meta_title',
'lang' => TRUE,
'size' => 114
),
array(
'type' => 'text',
'label' => $this->l('Meta desc'),
'name' => 'meta_desc',
'lang' => TRUE,
'size' => 114
),
array(
'type' => 'shop',
'label' => $this->l('Shop'),
'name' => 'checkBoxShopAsso_cmsps_categories'
),
array(
'type' => 'switch',
'label' => $this->l('Active'),
'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')
)
)
)
)
);
$this->fields_value = array(
'image' => $image ? $image : FALSE,
'size' => $image ? filesize(_CMS_CAT_IMG_DIR_.'/'.$obj->id.'.jpg') / 1000 : FALSE
);
return parent::renderForm();
}
public function renderView() {
if (($id = Tools::getValue('id_category'))) {
return parent::renderList();
}
}
public function getParentPossibility() {
$categories_list = CmsPsCategory::getParentPossibility($this->context->language->id, $this->context->shop->id, $this->object->id_category);
$categories = array(
array(
'id' => 0,
'name' => $this->l('-- Root Category --'),
),
);
foreach ($categories_list as $key => $category) {
$categories[] = array(
'id' => $category['id_category'],
'name' => $category['title']
);
}
return $categories;
}
protected function copyFromPost(&$object, $table) {
parent::copyFromPost($object, $table);
}
public function setMedia() {
parent::setMedia();
$this->addJs(_PS_MODULE_DIR_.'cmsps/js/admin.js');
}
public function postProcess() {
if (Tools::getValue('deleteImage')) {
$this->processForceDeleteImage();
}
if(isset($_FILES['pdf']) && !empty($_FILES['pdf']['tmp_name'])){
$obj = $this->loadObject(TRUE);
$fileTemp = $_FILES['pdf']['tmp_name'];
$fileParts = pathinfo($_FILES['pdf']['name']);
$filename = Tools::getValue('slug_'.$this->context->language->id);
if($fileParts['extension'] == 'pdf'){
if(!is_dir(_CMS_CAT_IMG_DIR_.'pdf'))
mkdir(_CMS_CAT_IMG_DIR_.'pdf', 0775);
$res = move_uploaded_file($fileTemp, _CMS_CAT_IMG_DIR_.'pdf/Tout-pratique-'.$filename.'.pdf');
if(!$res){
$this->errors[] = sprintf(Tools::displayError('An error occured during upload of file %s'),'Tout-pratique-'.$filename.'.pdf');
}
else{
$this->confirmations[] = sprintf($this->l('File %s has been uploaded'), 'Tout-pratique-'.$filename.'.pdf');
}
} else{
$this->errors[] = sprintf(Tools::displayError('File %s have not good extension, only .jpg or .png'), 'Tout-pratique-'.$filename.'.pdf');
}
}
return parent::postProcess();
}
public function processForceDeleteImage() {
$category = $this->loadObject(true);
if (Validate::isLoadedObject($category))
$category->deleteImage(true);
}
public function processDelete() {
if (Validate::isLoadedObject($category = $this->loadObject())) {
if ($category->hasChildren()) {
$this->errors[] = Tools::displayError('Impossible : This category has children.');
return true;
} elseif($category->hasPosts()) {
$this->errors[] = Tools::displayError('Impossible : This category has posts.');
return true;
} else {
$category->deleteImage(true);
parent::processDelete();
}
}
}
// DEFINIR LES FORMATS D'IMAGE POUR LE RESIZE
protected function postImage($id) {
$ret = parent::postImage($id);
if (isset($_FILES)
&& count($_FILES)
&& $_FILES['image']['name'] != NULL
&& !empty($this->object->id) ) {
// p($_FILES);die;
return TRUE;
}
return TRUE;
}
public function ajaxProcessstatuscmspsCategories() {
if (!$id_category = (int)Tools::getValue('id_category'))
die(Tools::jsonEncode(array('success' => false, 'error' => true, 'text' => $this->l('Failed to update the status'))));
else
{
$category = new CmsPsCategory((int)$id_category);
if (Validate::isLoadedObject($category))
{
$category->active = $category->active == 1 ? 0 : 1;
$category->save() ?
die(Tools::jsonEncode(array('success' => true, 'text' => $this->l('The status has been updated successfully')))) :
die(Tools::jsonEncode(array('success' => false, 'error' => true, 'text' => $this->l('Failed to update the status'))));
}
}
}
public function processSave() {
parent::processSave();
Hook::exec('seoUrlCmsCategory', array(
'object' => $this->object
));
}
}