add image management in post object

This commit is contained in:
Thibault 2015-07-28 11:51:12 +02:00
parent 43601e13bd
commit 2cf15abaf4
2 changed files with 79 additions and 6 deletions

View File

@ -17,6 +17,19 @@ class CmsPsPost extends ObjectModel {
public $meta_title;
public $meta_desc;
public $img_size = array(
array(
'name' => 'small',
'width' => 400,
'height' => 240
),
array(
'name' => 'big',
'width' => 770,
'height' => 500
)
);
/**
* @see ObjectModel::$definition
*/
@ -46,7 +59,6 @@ class CmsPsPost extends ObjectModel {
public function __construct($id = NULL, $id_lang = NULL, $id_shop = NULL) {
parent::__construct($id, $id_lang, $id_shop);
//Adding feedback to multishop
}
}

View File

@ -13,7 +13,7 @@ class AdminCmsPsPostsController extends ModuleAdminController {
$this->lang = TRUE;
$this->bootstrap = TRUE;
$this->context = Context::getContext();
parent::__construct();
$this->actions = array('edit','delete');
@ -70,7 +70,13 @@ class AdminCmsPsPostsController extends ModuleAdminController {
}
public function renderForm() {
$groups = Group::getGroups($this->default_form_language, TRUE);
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,
@ -195,7 +201,6 @@ class AdminCmsPsPostsController extends ModuleAdminController {
)
)
),
array(
'type' => 'switch',
'label' => $this->l('Astuce du jour'),
@ -215,9 +220,26 @@ class AdminCmsPsPostsController extends ModuleAdminController {
'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();
}
@ -256,7 +278,6 @@ class AdminCmsPsPostsController extends ModuleAdminController {
}
}
public function processSave() {
parent::processSave();
@ -265,4 +286,44 @@ class AdminCmsPsPostsController extends ModuleAdminController {
));
}
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);
}
}
}