2015-08-13 11:16:55 +02:00

377 lines
10 KiB
PHP

<?php
include_once dirname(__FILE__).'/../../classes/CmsPsCategory.php';
include_once dirname(__FILE__).'/../../classes/CmsPsPost.php';
class AdminCmsPsPostsController extends ModuleAdminController {
public function __construct() {
$this->table = 'cmsps_posts';
$this->className = 'CmsPsPost';
$this->identifier = 'id_post';
$this->deleted = FALSE;
$this->lang = TRUE;
$this->bootstrap = TRUE;
$this->context = Context::getContext();
parent::__construct();
$this->actions = array('edit','delete');
$this->_select = ' ca.`title` as category_title';
$this->_join .= 'JOIN `'._DB_PREFIX_.'cmsps_categories_lang` ca ON (ca.`id_category` = a.`id_category` AND ca.`id_lang` ='. $this->context->language->id .')';
$this->fields_list = array(
'id_post' => array(
'title' => 'ID',
'width' => 25
),
'title' => array(
'title' => $this->module->l('Titre'),
'width' => 200,
'filter_key' => 'b!title',
),
'slug' => array(
'title' => $this->module->l('Alias'),
'width' => 100,
),
'category_title' => array(
'title' => $this->module->l('Catégorie'),
'width' => 200,
'filter_key' => 'ca!title',
),
'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_posts_shop` acs ON a.`id_post` = acs.`id_post` AND acs.`id_shop` IN ('.implode(', ', Shop::getContextListShopID()).')';
$this->_group .= 'GROUP BY acs.`id_post`';
}
}
public function initPageHeaderToolbar() {
parent::initPageHeaderToolbar();
if ($this->display != 'edit' && $this->display != 'add') {
$this->page_header_toolbar_btn['new_sales'] = array(
'href' => self::$currentIndex.'&addcmsps_posts&token='.$this->token,
'desc' => $this->l('Add new Post', NULL, NULL, FALSE),
'icon' => 'process-icon-new'
);
}
}
public function renderForm() {
if (!($obj = $this->loadObject(TRUE)))
return;
$image = _CMS_POST_IMG_DIR_.$obj->id.'/small.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' => 'submitCMSPSPost',
'title' => $this->l('Save'),
),
'input' => array(
array(
'type' => 'relation_posts_cms',
'label' => $this->l('Sélectionner des articles'),
'name' => 'relation_posts_cms',
'class' => 'relation_posts_cms',
'id' => 'posts-cms',
'form_group_class' => 'fieldhide input_assoc',
'query' => $this->object->getInfosWithValues('postcms'),
),
array(
'type' => 'relation_posts_product',
'label' => $this->l('Sélectionner des produits'),
'name' => 'relation_posts_product',
'class' => 'relation_posts_product',
'id' => 'posts-cms-product',
'form_group_class' => 'fieldhide input_assoc',
'query' => $this->object->getInfosWithValues('postcms_product'),
),
array(
'type' => 'text',
'label' => $this->l('Titre'),
'name' => 'title',
'lang' => TRUE,
'required' => TRUE,
'size' => 114,
'form_group_class' => 'fieldhide input_content',
),
array(
'type' => 'text',
'label' => $this->l('Alias'),
'name' => 'slug',
'lang' => TRUE,
'required' => TRUE,
'size' => 114,
'form_group_class' => 'fieldhide input_content'
),
array(
'type' => 'textarea',
'label' => $this->l('Introduction'),
'name' => 'intro',
'autoload_rte' => TRUE,
'cols' => 100,
'rows' => 6,
'lang' => TRUE,
'form_group_class' => 'fieldhide input_content',
),
array(
'type' => 'textarea',
'label' => $this->l('Contenu'),
'name' => 'content',
'autoload_rte' => TRUE,
'cols' => 100,
'rows' => 6,
'lang' => TRUE,
'form_group_class' => 'fieldhide input_content',
),
array(
'type' => 'select',
'label' => $this->l('Catégorie'),
'name' => 'id_category',
'required' => TRUE,
'form_group_class' => 'fieldhide input_content',
'options' => array(
'query' => $this->getParentPossibility(),
'id' => 'id',
'name' => 'name'
)
),
array(
'type' => 'text',
'label' => $this->l('Meta title'),
'name' => 'meta_title',
'lang' => TRUE,
'size' => 114,
'form_group_class' => 'fieldhide input_seo',
),
array(
'type' => 'text',
'label' => $this->l('Meta desc'),
'name' => 'meta_desc',
'lang' => TRUE,
'size' => 114,
'form_group_class' => 'fieldhide input_seo',
),
array(
'type' => 'shop',
'label' => $this->l('Shop'),
'form_group_class' => 'fieldhide input_content',
'name' => 'checkBoxShopAsso_cmsps_posts'
),
array(
'type' => 'switch',
'label' => $this->l('Actif'),
'name' => 'active',
'required' => false,
'is_bool' => true,
'form_group_class' => 'fieldhide input_content',
'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('Visible en accueil'),
'name' => 'show_home',
'required' => false,
'is_bool' => true,
'form_group_class' => 'fieldhide input_content',
'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('Astuce du jour'),
'name' => 'trick',
'required' => false,
'is_bool' => true,
'form_group_class' => 'fieldhide input_content',
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
)
),
array(
'type' => 'file',
'label' => $this->l('Image introduction'),
'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',
'form_group_class' => 'fieldhide input_content',
),
)
);
if(file_exists($image)) {
$this->fields_value = array(
'image' => $image ? $image : FALSE,
'size' => $image ? filesize(_CMS_POST_IMG_DIR_.$obj->id.'/small.jpg') / 1000 : FALSE
);
}
return parent::renderForm();
}
public function getParentPossibility() {
$categories = array();
$categories_list = CmsPsCategory::getAllCategories(Context::getContext()->language->id, Context::getContext()->shop->id);
foreach ($categories_list as $key => $category) {
$categories[] = array(
'id' => $category['id_category'],
'name' => $category['title']
);
}
return $categories;
}
public function setMedia() {
parent::setMedia();
$this->addJs(_PS_MODULE_DIR_.'cmsps/js/admin.js');
}
public function ajaxProcessstatuscmspsPosts() {
if (!$id_post = (int)Tools::getValue('id_post'))
die(Tools::jsonEncode(array('success' => false, 'error' => true, 'text' => $this->l('Failed to update the status'))));
else
{
$post = new CmsPsPost((int)$id_post);
if (Validate::isLoadedObject($post))
{
$post->active = $post->active == 1 ? 0 : 1;
$post->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();
$this->saveRelation('postcms');
$this->saveRelation('postcms_product');
Hook::exec('seoUrlCmsPost', array(
'object' => $this->object
));
}
public function saveRelation($type) {
switch ($type) {
case 'postcms':
$relations = Tools::getValue('input-posts-cms', array());
if ($this->object->deleteRelations($type)) {
foreach ($relations as $key => $relation) {
$this->object->addRelation($relation, $type);
}
}
break;
case 'postcms_product':
$relations = Tools::getValue('input-posts-cms-product', array());
if ($this->object->deleteRelations($type)) {
foreach ($relations as $key => $relation) {
$this->object->addRelation($relation, $type);
}
}
break;
default:
return FALSE;
break;
}
}
public function postProcess() {
if (Tools::getValue('deleteImage')) {
$this->processForceDeleteImage();
}
return parent::postProcess();
}
public function processForceDeleteImage() {
if (!($obj = $this->loadObject(TRUE)))
return;
$dir_dst = _CMS_POST_IMG_DIR_.$obj->id;
foreach ($this->object->img_size as $key => $img) {
$dest = $dir_dst.'/'.$img['name'].'.jpg';
unlink($dest);
}
}
protected function postImage($id) {
$ret = parent::postImage($id);
if (isset($_FILES)
&& count($_FILES)
&& $_FILES['image']['name'] != NULL
&& !empty($this->object->id) ) {
$dir_dst = _CMS_POST_IMG_DIR_.$this->object->id;
$this->_createDirectoryImage($dir_dst);
foreach ($_FILES as $key => $file) {
foreach ($this->object->img_size as $key => $img) {
$dest = $dir_dst.'/'.$img['name'].'.jpg';
ImageManager::resize($file['tmp_name'], $dest, $img['width'], $img['height']);
}
}
}
}
private function _createDirectoryImage($dir_dst) {
if (!is_dir($dir_dst)) {
mkdir($dir_dst);
}
}
}