2015-07-10 15:14:26 +02:00

199 lines
5.3 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() {
$groups = Group::getGroups($this->default_form_language, TRUE);
$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' => 'text',
'label' => $this->l('Title'),
'name' => 'title',
'lang' => TRUE,
'required' => TRUE,
'size' => 114,
'form_group_class' => 'fieldhide input_content',
),
array(
'type' => 'text',
'label' => $this->l('Slug'),
'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('Category'),
'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'
)
)
);
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'))));
}
}
}
}