2015-07-10 15:14:26 +02:00
< ? 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 ();
2015-07-28 11:51:12 +02:00
2015-07-10 15:14:26 +02:00
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 ,
2015-08-21 14:40:48 +02:00
'desc' => $this -> l ( 'Ajouter un article' , NULL , NULL , FALSE ),
2015-07-10 15:14:26 +02:00
'icon' => 'process-icon-new'
);
}
}
public function renderForm () {
2015-08-13 11:16:55 +02:00
if ( ! ( $obj = $this -> loadObject ( TRUE )))
2015-07-28 11:51:12 +02:00
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 ;
2015-07-10 15:14:26 +02:00
$this -> fields_form = array (
'multilang' => TRUE ,
'tinymce' => TRUE ,
'legend' => array (
'title' => $this -> className ,
),
'submit' => array (
'name' => 'submitCMSPSPost' ,
'title' => $this -> l ( 'Save' ),
),
2015-08-21 14:40:48 +02:00
'input' => array (
2015-08-12 12:04:17 +02:00
array (
2015-08-13 11:16:55 +02:00
'type' => 'relation_posts_cms' ,
2015-08-21 14:40:48 +02:00
'label' => $this -> l ( 'Lier des articles' ),
2015-08-13 11:16:55 +02:00
'name' => 'relation_posts_cms' ,
'class' => 'relation_posts_cms' ,
'id' => 'posts-cms' ,
2015-08-12 12:04:17 +02:00
'form_group_class' => 'fieldhide input_assoc' ,
2015-08-13 11:16:55 +02:00
'query' => $this -> object -> getInfosWithValues ( 'postcms' ),
2015-08-12 12:04:17 +02:00
),
array (
2015-08-13 11:16:55 +02:00
'type' => 'relation_posts_product' ,
2015-09-01 18:57:57 +02:00
'label' => $this -> l ( 'Le pack qui me sauve tout de suite' ),
2015-08-13 11:16:55 +02:00
'name' => 'relation_posts_product' ,
'class' => 'relation_posts_product' ,
'id' => 'posts-cms-product' ,
2015-08-12 12:04:17 +02:00
'form_group_class' => 'fieldhide input_assoc' ,
2015-08-13 11:16:55 +02:00
'query' => $this -> object -> getInfosWithValues ( 'postcms_product' ),
2015-08-12 12:04:17 +02:00
),
2015-08-17 17:14:29 +02:00
array (
'type' => 'relation_posts_pack' ,
2015-09-01 18:56:33 +02:00
'label' => $this -> l ( 'Lier un panier' ),
2015-08-17 17:14:29 +02:00
'name' => 'relation_posts_pack' ,
'class' => 'relation_posts_pack' ,
'id' => 'posts-cms-pack' ,
'form_group_class' => 'fieldhide input_assoc' ,
'query' => $this -> object -> getInfosWithValues ( 'postcms_pack' ),
),
2015-08-21 14:40:48 +02:00
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' ,
),
2015-07-10 15:14:26 +02:00
array (
'type' => 'text' ,
2015-07-28 10:33:55 +02:00
'label' => $this -> l ( 'Titre' ),
2015-07-10 15:14:26 +02:00
'name' => 'title' ,
'lang' => TRUE ,
'required' => TRUE ,
'size' => 114 ,
'form_group_class' => 'fieldhide input_content' ,
),
array (
'type' => 'text' ,
2015-07-28 10:33:55 +02:00
'label' => $this -> l ( 'Alias' ),
2015-07-10 15:14:26 +02:00
'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' ,
2015-07-28 10:33:55 +02:00
'label' => $this -> l ( 'Catégorie' ),
2015-07-10 15:14:26 +02:00
'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'
2015-07-28 10:33:55 +02:00
),
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' )
)
)
2015-07-28 11:51:12 +02:00
),
2015-09-21 17:31:45 +02:00
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' )
)
)
),
2015-07-28 11:51:12 +02:00
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' ,
),
2015-07-10 15:14:26 +02:00
)
);
2015-08-21 14:40:48 +02:00
/* 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 ),
);
}
}
2015-07-28 11:51:12 +02:00
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
);
}
2015-07-10 15:14:26 +02:00
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' ))));
}
}
}
2015-07-10 16:29:27 +02:00
public function processSave () {
parent :: processSave ();
2015-08-13 11:16:55 +02:00
$this -> saveRelation ( 'postcms' );
$this -> saveRelation ( 'postcms_product' );
2015-08-17 17:14:29 +02:00
$this -> saveRelation ( 'postcms_pack' );
2015-08-21 14:40:48 +02:00
$this -> saveRelation ( 'cms_extrafields_1' );
$this -> saveRelation ( 'cms_extrafields_2' );
2015-08-13 11:16:55 +02:00
2015-07-10 16:29:27 +02:00
Hook :: exec ( 'seoUrlCmsPost' , array (
'object' => $this -> object
));
}
2015-08-13 11:16:55 +02:00
public function saveRelation ( $type ) {
switch ( $type ) {
2015-08-21 14:40:48 +02:00
case 'cms_extrafields_1' :
$relations = Tools :: getValue ( 'input-posts-cms_extrafields_1' , array ());
2015-09-17 16:54:46 +02:00
$this -> object -> deleteRelations ( $type , 1 );
foreach ( $relations as $key => $relation ) {
$this -> object -> addRelation ( $relation , $type , 1 );
2015-08-21 14:40:48 +02:00
}
break ;
case 'cms_extrafields_2' :
$relations = Tools :: getValue ( 'input-posts-cms_extrafields_2' , array ());
2015-09-17 16:54:46 +02:00
$this -> object -> deleteRelations ( $type , 2 );
foreach ( $relations as $key => $relation ) {
$this -> object -> addRelation ( $relation , $type , 2 );
2015-08-21 14:40:48 +02:00
}
break ;
2015-08-13 11:16:55 +02:00
case 'postcms' :
$relations = Tools :: getValue ( 'input-posts-cms' , array ());
2015-09-17 16:54:46 +02:00
$this -> object -> deleteRelations ( $type );
foreach ( $relations as $key => $relation ) {
$this -> object -> addRelation ( $relation , $type );
2015-08-13 11:16:55 +02:00
}
break ;
case 'postcms_product' :
$relations = Tools :: getValue ( 'input-posts-cms-product' , array ());
2015-09-17 16:54:46 +02:00
$this -> object -> deleteRelations ( $type );
foreach ( $relations as $key => $relation ) {
$this -> object -> addRelation ( $relation , $type );
2015-08-13 11:16:55 +02:00
}
2015-08-17 17:14:29 +02:00
break ;
case 'postcms_pack' :
$relations = Tools :: getValue ( 'input-posts-cms-pack' , array ());
2015-09-17 16:54:46 +02:00
$this -> object -> deleteRelations ( $type );
foreach ( $relations as $key => $relation ) {
$this -> object -> addRelation ( $relation , $type );
2015-08-17 17:14:29 +02:00
}
2015-09-17 16:54:46 +02:00
break ;
2015-08-13 11:16:55 +02:00
default :
return FALSE ;
break ;
}
}
2015-07-28 11:51:12 +02:00
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 ;
2015-09-17 16:54:46 +02:00
$this -> createDirectoryImage ( $dir_dst );
2015-07-28 11:51:12 +02:00
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' ]);
}
}
}
}
2015-09-17 16:54:46 +02:00
private function createDirectoryImage ( $dir_dst ) {
2015-07-28 11:51:12 +02:00
if ( ! is_dir ( $dir_dst )) {
mkdir ( $dir_dst );
}
}
2015-08-13 11:16:55 +02:00
2015-07-10 15:14:26 +02:00
}