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', ), 'is_video' => array( 'title' => $this->l('Video'), 'active' => 'is_video', 'type' => 'bool', 'class' => 'fixed-width-xs', 'align' => 'center', 'orderby' => false ), '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('Ajouter un article', 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, ), 'buttons' => array( 0 => array( 'title' => $this->l('Save'), 'type' => 'submit', 'name' => 'submitAddcmsps_posts', 'class' => 'pull-right', 'id' => 'cmsps_posts_form_submit_btn', 'value' => '1', 'icon' => 'process-icon-save' ) ), 'submit' => array( 'name' => 'submitAddcmsps_posts', 'title' => $this->l('Enregistrer et rester'), 'stay' => 1, ), 'input' => array( array( 'type' => 'relation_posts_cms', 'label' => $this->l('Lier 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('Le produit qui me sauve tout de suite'), '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' => 'relation_posts_pack', 'label' => $this->l('Lier un panier'), 'name' => 'relation_posts_pack', 'class' => 'relation_posts_pack', 'id' => 'posts-cms-pack', 'form_group_class' => 'fieldhide input_assoc', 'query' => $this->object->getInfosWithValues('postcms_pack'), ), array( 'type' => 'textarea', 'label' => $this->l('Video'), 'name' => 'video', 'cols' => 100, 'rows' => 6, 'form_group_class' => 'fieldhide input_assoc', ), array( 'type' => 'textarea', 'label' => $this->l('Produit Amazon'), 'name' => 'amazon', 'cols' => 100, 'rows' => 6, 'form_group_class' => 'fieldhide input_assoc', ), 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' => 'switch', 'label' => $this->l('Est une vidéo'), 'name' => 'is_video', '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'), 'desc' => $this->l('Format de l\'image 770x500 pixels'), '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', ), ) ); /* TAGS SYSTEME */ if (Module::isInstalled('cms_extrafields')) { $groups_list = CmsPsExtraFieldsGroup::getAll(Context::getContext()->language->id); foreach ($groups_list as $key => $group) { $this->fields_form['input'][] = array( 'type' => 'relation_cms_extrafields_'.Tools::str2url($group->name), 'label' => $this->l('Tags ') . $group->name, 'name' => 'relation_cms_extrafields_'.Tools::str2url($group->name), 'class' => 'relation_cms_extrafields_'.Tools::str2url($group->name), 'id' => 'posts-cms_extrafields_'.$group->id, 'form_group_class' => 'fieldhide input_assoc', 'query' => $this->object->getInfosWithValuesExtra('cms_extrafields_'.$group->id), ); } } 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'); $this->saveRelation('postcms_pack'); $this->saveRelation('cms_extrafields_1'); $this->saveRelation('cms_extrafields_2'); Hook::exec('seoUrlCmsPost', array( 'object' => $this->object )); } public function saveRelation($type) { switch ($type) { case 'cms_extrafields_1': $relations = Tools::getValue('input-posts-cms_extrafields_1', array()); $this->object->deleteRelations($type, 1); foreach ($relations as $key => $relation) { $this->object->addRelation($relation, $type, 1); } break; case 'cms_extrafields_2': $relations = Tools::getValue('input-posts-cms_extrafields_2', array()); $this->object->deleteRelations($type, 2); foreach ($relations as $key => $relation) { $this->object->addRelation($relation, $type, 2); } break; case 'postcms': $relations = Tools::getValue('input-posts-cms', array()); $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()); $this->object->deleteRelations($type); foreach ($relations as $key => $relation) { $this->object->addRelation($relation, $type); } break; case 'postcms_pack': $relations = Tools::getValue('input-posts-cms-pack', array()); $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']); } } } return true; } private function createDirectoryImage($dir_dst) { if (!is_dir($dir_dst)) { mkdir($dir_dst); } } }