Working : Image management set on Category

This commit is contained in:
gmorel
2013-09-22 20:50:42 +02:00
parent cf7e6d6952
commit b91a11536b
31 changed files with 3620 additions and 510 deletions

View File

@@ -28,6 +28,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Thelia\Core\Event\ImageCreateOrUpdateEvent;
use Thelia\Core\Event\ImagesCreateOrUpdateEvent;
use Thelia\Core\Event\ImageDeleteEvent;
use Thelia\Core\Event\ImageEvent;
use Thelia\Model\CategoryImage;
@@ -252,21 +253,20 @@ class Image extends BaseCachedFile implements EventSubscriberInterface
}
/**
* Take care of saving images in the database and file storage
* Take care of saving image in the database and file storage
*
* @param ImageCreateOrUpdateEvent $event Image event
*
* @throws \Thelia\Exception\ImageException
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
*/
public function saveImages(ImageCreateOrUpdateEvent $event)
public function saveImage(ImageCreateOrUpdateEvent $event)
{
$fileManager = new FileManager($this->container);
$this->adminLogAppend(
$this->container->get('thelia.translator')->trans(
'Saving images for parent id %parentId% (%parentType%)',
'Saving images for %parentName% parent id %parentId% (%parentType%)',
array(
'%parentName%' => $event->getParentName(),
'%parentId%' => $event->getParentId(),
'%parentType%' => $event->getImageType()
),
@@ -274,37 +274,75 @@ class Image extends BaseCachedFile implements EventSubscriberInterface
)
);
$newUploadedFiles = array();
$uploadedFiles = $event->getUploadedFiles();
$fileManager = new FileManager($this->container);
$model = $event->getModelImage();
foreach ($event->getModelImages() as $i => $modelImage) {
// Save image to database in order to get image id
$fileManager->saveImage($event, $modelImage);
$nbModifiedLines = $model->save();
$event->setModelImage($model);
if (isset($uploadedFiles) && isset($uploadedFiles[$i])) {
/** @var UploadedFile $uploadedFile */
$uploadedFile = $uploadedFiles[$i]['file'];
// Copy uploaded file into the storage directory
$newUploadedFiles = $fileManager->copyUploadedFile($event->getParentId(), $event->getImageType(), $modelImage, $uploadedFile, $newUploadedFiles);
} else {
throw new ImageException(
sprintf(
'File with name %s not found on the server',
$modelImage->getFile()
)
);
}
$event->setUploadedFiles($newUploadedFiles);
if (!$nbModifiedLines) {
throw new ImageException(
sprintf(
'Image "%s" with parent id %s (%s) failed to be saved',
$event->getParentName(),
$event->getParentId(),
$event->getImageType()
)
);
}
$newUploadedFile = $fileManager->copyUploadedFile($event->getParentId(), $event->getImageType(), $event->getModelImage(), $event->getUploadedFile());
$event->setUploadedFile($newUploadedFile);
}
/**
* Take care of updating image in the database and file storage
*
* @param ImageCreateOrUpdateEvent $event Image event
*
* @throws \Thelia\Exception\ImageException
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
*/
public function updateImage(ImageCreateOrUpdateEvent $event)
{
$this->adminLogAppend(
$this->container->get('thelia.translator')->trans(
'Updating images for %parentName% parent id %parentId% (%parentType%)',
array(
'%parentName%' => $event->getParentName(),
'%parentId%' => $event->getParentId(),
'%parentType%' => $event->getImageType()
),
'image'
)
);
$fileManager = new FileManager($this->container);
// Copy and save file
if ($event->getUploadedFile()) {
// Remove old picture file from file storage
$url = $fileManager->getUploadDir($event->getImageType()) . '/' . $event->getOldModelImage()->getFile();
unlink(str_replace('..', '', $url));
$newUploadedFile = $fileManager->copyUploadedFile(
$event->getModelImage()->getParentId(),
$event->getImageType(),
$event->getModelImage(), $event->getUploadedFile());
$event->setUploadedFile($newUploadedFile);
}
// Update image modifications
$event->getModelImage()->save();
$event->setModelImage($event->getModelImage());
}
/**
* Take care of deleting image in the database and file storage
*
* @param ImageCreateOrUpdateEvent $event Image event
* @param ImageDeleteEvent $event Image event
*
* @throws \Thelia\Exception\ImageException
* @throws \Exception
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
*/
public function deleteImage(ImageDeleteEvent $event)
@@ -312,7 +350,7 @@ class Image extends BaseCachedFile implements EventSubscriberInterface
$fileManager = new FileManager($this->container);
try {
$fileManager->deleteImage($event->getImageToDelete());
$fileManager->deleteImage($event->getImageToDelete(), $event->getImageType());
$this->adminLogAppend(
$this->container->get('thelia.translator')->trans(
@@ -336,22 +374,10 @@ class Image extends BaseCachedFile implements EventSubscriberInterface
'image'
)
);
throw $e;
}
}
/**
* The absolute directory path where uploaded
* documents should be saved
*
* @param $modelImage Image model
*
* @return string
*/
public function getUploadRootDir($modelImage)
{
return __DIR__.'/../../../../' . $modelImage->getUploadDir();
}
/**
* Process image resizing, with borders or cropping. If $dest_width and $dest_height
* are both null, no resize is performed.
@@ -474,8 +500,9 @@ class Image extends BaseCachedFile implements EventSubscriberInterface
return array(
TheliaEvents::IMAGE_PROCESS => array("processImage", 128),
TheliaEvents::IMAGE_CLEAR_CACHE => array("clearCache", 128),
TheliaEvents::IMAGE_SAVE => array("saveImages", 128),
TheliaEvents::IMAGE_DELETE => array("deleteImage", 128),
TheliaEvents::IMAGE_SAVE => array("saveImage", 128),
TheliaEvents::IMAGE_UPDATE => array("updateImage", 128),
);
}
}

View File

@@ -58,7 +58,7 @@
<form name="thelia.admin.category.creation" class="Thelia\Form\CategoryCreationForm"/>
<form name="thelia.admin.category.modification" class="Thelia\Form\CategoryModificationForm"/>
<form name="thelia.admin.category.image.creation" class="Thelia\Form\CategoryImageCreationForm"/>
<form name="thelia.admin.category.image.modification" class="Thelia\Form\CategoryImageModification"/>
<form name="thelia.admin.product.creation" class="Thelia\Form\ProductCreationForm"/>
<form name="thelia.admin.product.modification" class="Thelia\Form\ProductModificationForm"/>

View File

@@ -39,11 +39,24 @@
<!-- Route to the file controller -->
<route id="admin.image.save" path="/admin/image/type/{parentType}/{parentId}/save">
<default key="_controller">Thelia\Controller\Admin\FileController::saveImagesAction</default>
<route id="admin.image.save-ajax" path="/admin/image/type/{parentType}/{parentId}/save-ajax">
<default key="_controller">Thelia\Controller\Admin\FileController::saveImageAjaxAction</default>
<requirement key="parentType">.*</requirement>
<requirement key="parentId">\d+</requirement>
</route>
<route id="admin.image.list-ajax" path="/admin/image/type/{parentType}/{parentId}/list-ajax">
<default key="_controller">Thelia\Controller\Admin\FileController::getImageListAjaxAction</default>
<requirement key="parentType">.*</requirement>
<requirement key="parentId">\d+</requirement>
</route>
<route id="admin.image.update.view" path="/admin/image/type/{parentType}/{imageId}/update" methods="get">
<default key="_controller">Thelia\Controller\Admin\FileController::viewImageAction</default>
<requirement key="customer_id">\d+</requirement>
</route>
<route id="admin.image.update.process" path="/admin/image/type/{parentType}/{imageId}/update" methods="post">
<default key="_controller">Thelia\Controller\Admin\FileController::updateImageAction</default>
<requirement key="customer_id">\d+</requirement>
</route>
<route id="admin.image.delete" path="/admin/image/type/{parentType}/delete/{imageId}">
<default key="_controller">Thelia\Controller\Admin\FileController::deleteImagesAction</default>
<requirement key="parentType">.*</requirement>

View File

@@ -23,20 +23,20 @@
namespace Thelia\Controller\Admin;
use Propel\Runtime\Exception\PropelException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Router;
use Thelia\Core\Event\ImageCreateOrUpdateEvent;
use Thelia\Core\Event\ImagesCreateOrUpdateEvent;
use Thelia\Core\Event\ImageDeleteEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Translation\Translator;
use Thelia\Form\CategoryImageCreationForm;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Log\Tlog;
use Thelia\Model\CategoryImageQuery;
use Thelia\Model\ContentImageQuery;
use Thelia\Model\FolderImageQuery;
use Thelia\Model\ProductImageQuery;
use Thelia\Tools\FileManager;
use Thelia\Tools\Rest\ResponseRest;
/**
* Created by JetBrains PhpStorm.
@@ -57,13 +57,14 @@ class FileController extends BaseAdminController
*
* @param int $parentId Parent id owning files being saved
* @param string $parentType Parent Type owning files being saved
* @param string $successUrl Success URL to be redirected to
*
* @return Response
*/
public function saveFilesAction($parentId, $parentType, $successUrl)
public function saveFilesAction($parentId, $parentType)
{
}
/**
@@ -74,77 +75,165 @@ class FileController extends BaseAdminController
*
* @return Response
*/
public function saveImagesAction($parentId, $parentType)
public function saveImageAjaxAction($parentId, $parentType)
{
// Check current user authorization
if (null !== $response = $this->checkAuth("admin.image.save")) {
return $response;
}
$message = $this->getTranslator()
->trans(
'Images saved successfully',
array(),
'image'
);
$this->checkAuth('ADMIN', 'admin.image.save');
$this->checkXmlHttpRequest();
if ($this->isParentTypeValid($parentType)) {
if ($this->getRequest()->isMethod('POST')) {
// Create the form from the request
$creationForm = $this->getImageForm($parentType, $this->getRequest());
try {
// Check the form against constraints violations
$form = $this->validateForm($creationForm, 'POST');
/** @var UploadedFile $fileBeingUploaded */
$fileBeingUploaded = $this->getRequest()->files->get('file');
// Get the form field values
$data = $form->getData();
$fileManager = new FileManager($this->container);
$parentModel = $fileManager->getParentImageModel($parentType, $parentId);
$imageModel = $fileManager->getImageModel($parentType);
// Feed event
$imageCreateOrUpdateEvent = new ImageCreateOrUpdateEvent(
$parentType,
$parentId
);
if (isset($data) && isset($data['pictures'])) {
$imageCreateOrUpdateEvent->setModelImages($data['pictures']);
$imageCreateOrUpdateEvent->setUploadedFiles($this->getRequest()->files->get($creationForm->getName())['pictures']);
}
// Dispatch Event to the Action
$this->dispatch(
TheliaEvents::IMAGE_SAVE,
$imageCreateOrUpdateEvent
);
} catch (FormValidationException $e) {
// Invalid data entered
$message = 'Please check your input:';
$this->logError($parentType, 'image saving', $message, $e);
} catch (\Exception $e) {
// Any other error
$message = 'Sorry, an error occurred:';
$this->logError($parentType, 'image saving', $message, $e);
if ($parentModel === null || $imageModel === null || $fileBeingUploaded === null) {
return new Response('', 404);
}
if ($message !== false) {
// Mark the form as with error
$creationForm->setErrorMessage($message);
$defaultTitle = $parentModel->getTitle();
$imageModel->setParentId($parentId);
$imageModel->setTitle($defaultTitle);
// Send the form and the error to the parser
$this->getParserContext()
->addForm($creationForm)
->setGeneralError($message);
$imageCreateOrUpdateEvent = new ImageCreateOrUpdateEvent(
$parentType,
$parentId
);
$imageCreateOrUpdateEvent->setModelImage($imageModel);
$imageCreateOrUpdateEvent->setUploadedFile($fileBeingUploaded);
$imageCreateOrUpdateEvent->setParentName($parentModel->getTitle());
// Set flash message to be displayed
$flashMessage = $this->getSession()->get('flashMessage');
$flashMessage['imageMessage'] = $message;
$this->getSession()->set('flashMessage', $flashMessage);
}
// Dispatch Event to the Action
$this->dispatch(
TheliaEvents::IMAGE_SAVE,
$imageCreateOrUpdateEvent
);
return new ResponseRest(array('status' => true, 'message' => ''));
}
}
$this->redirectSuccess($creationForm);
return new Response('', 404);
}
/**
* Manage how a image list will be displayed in AJAX
*
* @param int $parentId Parent id owning images being saved
* @param string $parentType Parent Type owning images being saved
*
* @return Response
*/
public function getImageListAjaxAction($parentId, $parentType)
{
$this->checkAuth('ADMIN', 'admin.image.save');
$this->checkXmlHttpRequest();
$args = array('imageType' => $parentType, 'parentId' => $parentId);
return $this->render('includes/image-upload-list-ajax', $args);
}
/**
* Manage how an image is viewed
*
* @param int $imageId Parent id owning images being saved
* @param string $parentType Parent Type owning images being saved
*
* @return Response
*/
public function viewImageAction($imageId, $parentType)
{
if (null !== $response = $this->checkAuth('admin.image.view')) return $response;
try {
$fileManager = new FileManager($this->container);
$image = $fileManager->getImageModelQuery($parentType)->findPk($imageId);
$redirectUrl = $fileManager->getRedirectionUrl($parentType, $image->getParentId());
return $this->render('image-edit', array(
'imageId' => $imageId,
'imageType' => $parentType,
'redirectUrl' => $redirectUrl
));
} catch (Exception $e) {
$this->pageNotFound();
}
}
/**
* Manage how an image is updated
*
* @param int $imageId Parent id owning images being saved
* @param string $parentType Parent Type owning images being saved
*
* @return Response
*/
public function updateImageAction($imageId, $parentType)
{
if (null !== $response = $this->checkAuth('admin.image.update')) return $response;
$message = false;
$fileManager = new FileManager($this->container);
$imageModification = $fileManager->getImageForm($parentType, $this->getRequest());
try {
$image = $fileManager->getImageModelQuery($parentType)->findPk($imageId);
$oldImage = clone $image;
if(null === $image) {
throw new \InvalidArgumentException(sprintf('%d image id does not exists', $imageId));
}
$form = $this->validateForm($imageModification);
$event = $this->createEventInstance($parentType, $image, $form->getData());
$event->setOldModelImage($oldImage);
$files = $this->getRequest()->files;
$fileForm = $files->get($imageModification->getName());
if(isset($fileForm['file'])) {
$event->setUploadedFile($fileForm['file']);
}
$this->dispatch(TheliaEvents::IMAGE_UPDATE, $event);
$imageUpdated = $event->getModelImage();
$this->adminLogAppend(sprintf('Image with Ref %s (ID %d) modified', $imageUpdated->getTitle() , $imageUpdated->getId()));
if($this->getRequest()->get('save_mode') == 'close') {
$this->redirectToRoute('admin.images');
} else {
$this->redirectSuccess($imageModification);
}
} catch (FormValidationException $e) {
$message = sprintf('Please check your input: %s', $e->getMessage());
} catch (PropelException $e) {
$message = $e->getMessage();
} catch (\Exception $e) {
$message = sprintf('Sorry, an error occurred: %s', $e->getMessage().' '.$e->getFile());
}
if ($message !== false) {
Tlog::getInstance()->error(sprintf('Error during image editing : %s.', $message));
$imageModification->setErrorMessage($message);
$this->getParserContext()
->addForm($imageModification)
->setGeneralError($message)
;
}
return $this->render('image-edit', array(
'imageId' => $imageId,
'imageType' => $parentType
));
}
/**
@@ -160,14 +249,18 @@ class FileController extends BaseAdminController
$this->checkAuth('ADMIN', 'admin.image.delete');
$this->checkXmlHttpRequest();
$model = $this->getImageModel($parentType, $imageId);
$fileManager = new FileManager($this->container);
$imageModelQuery = $fileManager->getImageModelQuery($parentType);
$model = $imageModelQuery->findPk($imageId);
if ($model == null) {
return $this->pageNotFound();
}
// Feed event
$imageDeleteEvent = new ImageDeleteEvent(
$model
$model,
$parentType
);
// Dispatch Event to the Action
@@ -218,73 +311,42 @@ class FileController extends BaseAdminController
*/
public function isParentTypeValid($parentType)
{
return (in_array($parentType, ImageCreateOrUpdateEvent::getAvailableType()));
return (in_array($parentType, ImagesCreateOrUpdateEvent::getAvailableType()));
}
/**
* Get Image form
* Create Event instance
*
* @param string $parentType Parent type
* @param Request $request Request Service
* @param string $parentType Parent Type owning images being saved
* @param \Thelia\Model\CategoryImage|\Thelia\Model\ProductImage|\Thelia\Model\ContentImage|\Thelia\Model\FolderImage $model Image model
* @param array $data Post data
*
* @return null|CategoryImageCreationForm|ContentImageCreationForm|FolderImageCreationForm|ProductImageCreationForm
*
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
* @return ImageCreateOrUpdateEvent
*/
public function getImageForm($parentType, Request $request)
private function createEventInstance($parentType, $model, $data)
{
// @todo implement other forms
switch ($parentType) {
// case ImageCreateOrUpdateEvent::TYPE_PRODUCT:
// $creationForm = new ProductImageCreationForm($request);
// break;
case ImageCreateOrUpdateEvent::TYPE_CATEGORY:
$creationForm = new CategoryImageCreationForm($request);
break;
// case ImageCreateOrUpdateEvent::TYPE_CONTENT:
// $creationForm = new ContentImageCreationForm($request);
// break;
// case ImageCreateOrUpdateEvent::TYPE_FOLDER:
// $creationForm = new FolderImageCreationForm($request);
// break;
default:
$creationForm = null;
$imageCreateEvent = new ImageCreateOrUpdateEvent($parentType, null);
if (isset($data['title'])) {
$model->setTitle($data['title']);
}
if (isset($data['chapo'])) {
$model->setChapo($data['chapo']);
}
if (isset($data['description'])) {
$model->setDescription($data['description']);
}
if (isset($data['file'])) {
$model->setFile($data['file']);
}
if (isset($data['postscriptum'])) {
$model->setPostscriptum($data['postscriptum']);
}
return $creationForm;
$imageCreateEvent->setModelImage($model);
return $imageCreateEvent;
}
/**
* Get image model from type
*
* @param string $parentType
* @param int $imageId
*
* @return null|\Thelia\Model\CategoryImage|\Thelia\Model\ContentImage|\Thelia\Model\FolderImage|\Thelia\Model\ProductImage
*
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
*/
public function getImageModel($parentType, $imageId)
{
switch ($parentType) {
case ImageCreateOrUpdateEvent::TYPE_PRODUCT:
$model = ProductImageQuery::create()->findPk($imageId);
break;
case ImageCreateOrUpdateEvent::TYPE_CATEGORY:
$model = CategoryImageQuery::create()->findPk($imageId);
break;
case ImageCreateOrUpdateEvent::TYPE_CONTENT:
$model = ContentImageQuery::create()->findPk($imageId);
break;
case ImageCreateOrUpdateEvent::TYPE_FOLDER:
$model = FolderImageQuery::create()->findPk($imageId);
break;
default:
$model = null;
}
return $model;
}
}

View File

@@ -22,13 +22,14 @@
/*************************************************************************************/
namespace Thelia\Core\Event;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Created by JetBrains PhpStorm.
* Date: 9/18/13
* Time: 3:56 PM
*
* Occurring when a Image list is saved
* Occurring when an Image is saved
*
* @package Image
* @author Guillaume MOREL <gmorel@openstudio.fr>
@@ -36,16 +37,15 @@ namespace Thelia\Core\Event;
*/
class ImageCreateOrUpdateEvent extends ActionEvent
{
CONST TYPE_PRODUCT = 'product';
CONST TYPE_CATEGORY = 'category';
CONST TYPE_CONTENT = 'content';
CONST TYPE_FOLDER = 'folder';
/** @var array Images model to save */
protected $modelImages = array();
/** @var \Thelia\Model\CategoryImage|\Thelia\Model\ProductImage|\Thelia\Model\ContentImage|\Thelia\Model\FolderImage model to save */
protected $modelImage = array();
/** @var array Images file to save */
protected $uploadedFiles = array();
/** @var \Thelia\Model\CategoryImage|\Thelia\Model\ProductImage|\Thelia\Model\ContentImage|\Thelia\Model\FolderImage model to save */
protected $oldModelImage = array();
/** @var UploadedFile Image file to save */
protected $uploadedFile = null;
/** @var int Image parent id */
protected $parentId = null;
@@ -53,85 +53,56 @@ class ImageCreateOrUpdateEvent extends ActionEvent
/** @var string Image type */
protected $imageType = null;
/** @var array Available image parent type */
protected static $availableType = array(
self::TYPE_PRODUCT,
self::TYPE_CATEGORY,
self::TYPE_CONTENT,
self::TYPE_FOLDER,
);
/** @var string Parent name */
protected $parentName = null;
/**
* Constructor
*
* @param string $pictureType Picture type
* ex : ImageCreateOrUpdateEvent::TYPE_CATEGORY
* @param string $imageType Image type
* ex : FileManager::TYPE_CATEGORY
* @param int $parentId Image parent id
*/
public function __construct($pictureType, $parentId)
public function __construct($imageType, $parentId)
{
$this->imageType = $pictureType;
$this->imageType = $imageType;
$this->parentId = $parentId;
}
/**
* Set Images to save
* Set Image to save
*
* @param array $images Thelia\Model\CategoryImage Array
* @param $image \Thelia\Model\CategoryImage|\Thelia\Model\ProductImage|\Thelia\Model\ContentImage|\Thelia\Model\FolderImage
*
* @return $this
*/
public function setModelImages($images)
public function setModelImage($image)
{
$this->modelImages = $images;
$this->modelImage = $image;
return $this;
}
/**
* Get Images being saved
* Get Image being saved
*
* @return array Array of Thelia\Model\CategoryImage
* @return \Thelia\Model\CategoryImage|\Thelia\Model\ProductImage|\Thelia\Model\ContentImage|\Thelia\Model\FolderImage
*/
public function getModelImages()
public function getModelImage()
{
return $this->modelImages;
}
/**
* Set Images to save
*
* @param array $images Thelia\Model\CategoryImage Array
*
* @return $this
*/
public function setUploadedFiles($images)
{
$this->uploadedFiles = $images;
return $this;
}
/**
* Get Images being saved
*
* @return array Array of Thelia\Model\CategoryImage
*/
public function getUploadedFiles()
{
return $this->uploadedFiles;
return $this->modelImage;
}
/**
* Set picture type
*
* @param string $pictureType Picture type
* @param string $imageType Image type
*
* @return $this
*/
public function setImageType($pictureType)
public function setImageType($imageType)
{
$this->imageType = $pictureType;
$this->imageType = $imageType;
return $this;
}
@@ -146,16 +117,6 @@ class ImageCreateOrUpdateEvent extends ActionEvent
return $this->imageType;
}
/**
* Get all image parent type available
*
* @return array
*/
public static function getAvailableType()
{
return self::$availableType;
}
/**
* Set Image parent id
*
@@ -180,5 +141,73 @@ class ImageCreateOrUpdateEvent extends ActionEvent
return $this->parentId;
}
/**
* Set uploaded file
*
* @param UploadedFile $uploadedFile File being uploaded
*
* @return $this
*/
public function setUploadedFile($uploadedFile)
{
$this->uploadedFile = $uploadedFile;
return $this;
}
/**
* Get uploaded file
*
* @return UploadedFile
*/
public function getUploadedFile()
{
return $this->uploadedFile;
}
/**
* Set parent name
*
* @param string $parentName Parent name
*
* @return $this
*/
public function setParentName($parentName)
{
$this->parentName = $parentName;
return $this;
}
/**
* Get parent name
*
* @return string
*/
public function getParentName()
{
return $this->parentName;
}
/**
* Set old model value
*
* @param \Thelia\Model\CategoryImage|\Thelia\Model\ContentImage|\Thelia\Model\FolderImage|\Thelia\Model\ProductImage $oldModelImage
*/
public function setOldModelImage($oldModelImage)
{
$this->oldModelImage = $oldModelImage;
}
/**
* Get old model value
*
* @return \Thelia\Model\CategoryImage|\Thelia\Model\ContentImage|\Thelia\Model\FolderImage|\Thelia\Model\ProductImage
*/
public function getOldModelImage()
{
return $this->oldModelImage;
}
}

View File

@@ -41,6 +41,9 @@ use Thelia\Model\ProductImage;
*/
class ImageDeleteEvent extends ActionEvent
{
/** @var string Image type */
protected $imageType = null;
/** @var CategoryImage|ProductImage|ContentImage|FolderImage Image about to be deleted */
protected $imageToDelete = null;
@@ -48,10 +51,37 @@ class ImageDeleteEvent extends ActionEvent
* Constructor
*
* @param CategoryImage|ProductImage|ContentImage|FolderImage $imageToDelete Image about to be deleted
* @param string $imageType Image type
* ex : FileManager::TYPE_CATEGORY
*/
public function __construct($imageToDelete)
public function __construct($imageToDelete, $imageType)
{
$this->imageToDelete = $imageToDelete;
$this->imageType = $imageType;
}
/**
* Set picture type
*
* @param string $imageType Image type
*
* @return $this
*/
public function setImageType($imageType)
{
$this->imageType = $imageType;
return $this;
}
/**
* Get picture type
*
* @return string
*/
public function getImageType()
{
return $this->imageType;
}
/**

View File

@@ -0,0 +1,184 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Event;
/**
* Created by JetBrains PhpStorm.
* Date: 9/18/13
* Time: 3:56 PM
*
* Occurring when a Image list is saved
*
* @package Image
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class ImagesCreateOrUpdateEvent extends ActionEvent
{
CONST TYPE_PRODUCT = 'product';
CONST TYPE_CATEGORY = 'category';
CONST TYPE_CONTENT = 'content';
CONST TYPE_FOLDER = 'folder';
/** @var array Images model to save */
protected $modelImages = array();
/** @var array Images file to save */
protected $uploadedFiles = array();
/** @var int Image parent id */
protected $parentId = null;
/** @var string Image type */
protected $imageType = null;
/** @var array Available image parent type */
protected static $availableType = array(
self::TYPE_PRODUCT,
self::TYPE_CATEGORY,
self::TYPE_CONTENT,
self::TYPE_FOLDER,
);
/**
* Constructor
*
* @param string $pictureType Picture type
* ex : ImageCreateOrUpdateEvent::TYPE_CATEGORY
* @param int $parentId Image parent id
*/
public function __construct($pictureType, $parentId)
{
$this->imageType = $pictureType;
$this->parentId = $parentId;
}
/**
* Set Images to save
*
* @param array $images Thelia\Model\CategoryImage Array
*
* @return $this
*/
public function setModelImages($images)
{
$this->modelImages = $images;
return $this;
}
/**
* Get Images being saved
*
* @return array Array of Thelia\Model\CategoryImage
*/
public function getModelImages()
{
return $this->modelImages;
}
/**
* Set Images to save
*
* @param array $images Thelia\Model\CategoryImage Array
*
* @return $this
*/
public function setUploadedFiles($images)
{
$this->uploadedFiles = $images;
return $this;
}
/**
* Get Images being saved
*
* @return array Array of Thelia\Model\CategoryImage
*/
public function getUploadedFiles()
{
return $this->uploadedFiles;
}
/**
* Set picture type
*
* @param string $pictureType Picture type
*
* @return $this
*/
public function setImageType($pictureType)
{
$this->imageType = $pictureType;
return $this;
}
/**
* Get picture type
*
* @return string
*/
public function getImageType()
{
return $this->imageType;
}
/**
* Get all image parent type available
*
* @return array
*/
public static function getAvailableType()
{
return self::$availableType;
}
/**
* Set Image parent id
*
* @param int $parentId Image parent id
*
* @return $this
*/
public function setParentId($parentId)
{
$this->parentId = $parentId;
return $this;
}
/**
* Get Image parent id
*
* @return int
*/
public function getParentId()
{
return $this->parentId;
}
}

View File

@@ -304,6 +304,11 @@ final class TheliaEvents
*/
const IMAGE_SAVE = "action.saveImages";
/**
* Save given images
*/
const IMAGE_UPDATE = "action.updateImages";
/**
* Delete given image
*/

View File

@@ -22,8 +22,9 @@
/*************************************************************************************/
namespace Thelia\Form;
use Thelia\Core\Translation\Translator;
use Thelia\Form\Type\ImageCategoryType;
use Thelia\Form\Image\ImageModification;
/**
* Created by JetBrains PhpStorm.
@@ -36,36 +37,17 @@ use Thelia\Form\Type\ImageCategoryType;
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class CategoryImageCreationForm extends BaseForm
class CategoryImageModification extends ImageModification
{
/**
* Allow to build a form
*/
protected function buildForm()
{
$this->formBuilder
->add('pictures',
'collection',
array(
'type' => new ImageCategoryType(),
'options' => array(
'required' => false
),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
)
)
->add('formSuccessUrl');
}
/**
* Get form name
* This name must be unique
*
* @return string
*/
public function getName()
{
return 'thelia_category_image_creation';
return 'thelia_category_image_modification';
}
}

View File

@@ -20,60 +20,34 @@
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Form\Type;
namespace Thelia\Form;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Thelia\Form\Type\ImageType;
use Thelia\Core\Translation\Translator;
use Thelia\Form\Image\ImageModification;
/**
* Created by JetBrains PhpStorm.
* Date: 9/18/13
* Time: 3:56 PM
*
* Form allowing to process a category picture
* Form allowing to process an image collection
*
* @package Image
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class ImageCategoryType extends ImageType
class ContentImageModification extends ImageModification
{
/**
* Build a Picture form
*
* @param FormBuilderInterface $builder Form builder
* @param array $options Form options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('category_id', 'integer');
parent::buildForm($builder, $options);
}
/**
* Set default options
* Map the form to the given Model
*
* @param OptionsResolverInterface $resolver Option resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
array(
'data_class' => 'Thelia\Model\CategoryImage'
)
);
}
/**
* Get form name
* This name must be unique
*
* @return string
*/
public function getName()
{
return 'thelia_category_picture_creation_type';
return 'thelia_content_image_modification';
}
}

View File

@@ -20,63 +20,34 @@
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Form\Type;
namespace Thelia\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Constraints\NotBlank;
use Thelia\Core\Translation\Translator;
use Thelia\Form\Image\ImageModification;
/**
* Created by JetBrains PhpStorm.
* Date: 9/18/13
* Time: 3:56 PM
*
* Form allowing to process a picture
*
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
* Form allowing to process an image collection
*
* @package Image
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
abstract class ImageType extends AbstractType
class FolderImageModification extends ImageModification
{
/**
* Build a Picture form
* Get form name
* This name must be unique
*
* @param FormBuilderInterface $builder Form builder
* @param array $options Form options
* @return string
*/
public function buildForm(FormBuilderInterface $builder, array $options)
public function getName()
{
// $builder->add('position');
$builder->add(
'title',
'text',
array(
'constraints' => new NotBlank()
)
);
$builder->add(
'file',
'file',
array(
'constraints' => array(
new NotBlank(),
new Image(
array(
'minWidth' => 200,
// 'maxWidth' => 400,
'minHeight' => 200,
// 'maxHeight' => 400,
)
)
)
)
);
// $builder->add('description');
// $builder->add('chapo');
// $builder->add('postscriptum');
return 'thelia_folder_image_modification';
}
}

View File

@@ -0,0 +1,183 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Form\Image;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Constraints\NotBlank;
use Thelia\Core\Translation\Translator;
use Thelia\Form\BaseForm;
/**
* Created by JetBrains PhpStorm.
* Date: 9/18/13
* Time: 3:56 PM
*
* Form allowing to process an image
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
*
* @package Image
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
abstract class ImageModification extends BaseForm
{
// public function __construct(Request $request, $type= "form", $data = array(), $options = array(), $isUpdate = false)
// {
// parent::__construct($request, $type, $data, $options);
// $this->setIsUpdate($isUpdate);
// }
// /** @var bool Flag for update/create mode */
// protected $isUpdate = false;
/**
*
* in this function you add all the fields you need for your Form.
* Form this you have to call add method on $this->form attribute :
*
* $this->form->add('name', 'text')
* ->add('email', 'email', array(
* 'attr' => array(
* 'class' => 'field'
* ),
* 'label' => 'email',
* 'constraints' => array(
* new NotBlank()
* )
* )
* )
* ->add('age', 'integer');
*
* @return null
*/
protected function buildForm()
{
// if (false === $this->isUpdate) {
$this->formBuilder->add(
'file',
'file',
array(
'constraints' => array(
// new NotBlank(),
new Image(
array(
'minWidth' => 200,
'minHeight' => 200
)
)
),
'label' => Translator::getInstance()->trans('File'),
'label_attr' => array(
'for' => 'file'
)
)
);
// }
$this->formBuilder
->add(
'title',
'text',
array(
'constraints' => array(
new NotBlank()
),
'label' => Translator::getInstance()->trans('Title'),
'label_attr' => array(
'for' => 'title'
)
)
)
->add(
'description',
'text',
array(
'constraints' => array(),
'label' => Translator::getInstance()->trans('Description'),
'label_attr' => array(
'for' => 'description'
)
)
)
->add(
'chapo',
'text',
array(
'constraints' => array(),
'label' => Translator::getInstance()->trans('Chapo'),
'label_attr' => array(
'for' => 'chapo'
)
)
)
->add(
'postscriptum',
'text',
array(
'constraints' => array(),
'label' => Translator::getInstance()->trans('Post Scriptum'),
'label_attr' => array(
'for' => 'postscriptum'
)
)
)
->add(
'postscriptum',
'text',
array(
'constraints' => array(),
'label' => Translator::getInstance()->trans('Post Scriptum'),
'label_attr' => array(
'for' => 'postscriptum'
)
)
);
}
// /**
// * Set form in update or create mode
// *
// * @param boolean $isUpdate
// */
// public function setIsUpdate($isUpdate)
// {
// $this->isUpdate = $isUpdate;
// }
//
// /**
// * Get for mode
// *
// * @return boolean
// */
// public function getIsUpdate()
// {
// return $this->isUpdate;
// }
}

View File

@@ -0,0 +1,53 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Form;
use Thelia\Core\Translation\Translator;
use Thelia\Form\Image\ImageModification;
/**
* Created by JetBrains PhpStorm.
* Date: 9/18/13
* Time: 3:56 PM
*
* Form allowing to process an image collection
*
* @package Image
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class ProductImageModification extends ImageModification
{
/**
* Get form name
* This name must be unique
*
* @return string
*/
public function getName()
{
return 'thelia_product_image_modification';
}
}

View File

@@ -29,40 +29,17 @@ class CategoryImage extends BaseCategoryImage
}
/**
* Get picture absolute path
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
* Set Image parent id
*
* @return null|string
*/
public function getAbsolutePath()
{
return null === $this->file
? null
: $this->getUploadDir().'/'.$this->file;
}
/**
* Get picture web path
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
* @param int $parentId parent id
*
* @return null|string
* @return $this
*/
public function getWebPath()
public function setParentId($parentId)
{
return null === $this->file
? null
: $this->getUploadDir().'/'.$this->file;
}
$this->setCategoryId($parentId);
/**
* Get rid of the __DIR__ so it doesn't screw up
* when displaying uploaded doc/image in the view.
* @return string
*/
public function getUploadDir()
{
return THELIA_LOCAL_DIR . 'media/images/category';
return $this;
}
/**

View File

@@ -26,6 +26,20 @@ class ContentImage extends BaseContentImage
return true;
}
/**
* Set Image parent id
*
* @param int $parentId parent id
*
* @return $this
*/
public function setParentId($parentId)
{
$this->setContentId($parentId);
return $this;
}
/**
* Get Image parent id
*

View File

@@ -26,6 +26,20 @@ class FolderImage extends BaseFolderImage
return true;
}
/**
* Set Image parent id
*
* @param int $parentId parent id
*
* @return $this
*/
public function setParentId($parentId)
{
$this->setFolderId($parentId);
return $this;
}
/**
* Get Image parent id
*

View File

@@ -26,6 +26,20 @@ class ProductImage extends BaseProductImage
return true;
}
/**
* Set Image parent id
*
* @param int $parentId parent id
*
* @return $this
*/
public function setParentId($parentId)
{
$this->setProductId($parentId);
return $this;
}
/**
* Get Image parent id
*

View File

@@ -10,7 +10,7 @@
namespace Thelia\Tests\Type;
use Thelia\Core\Event\ImageCreateOrUpdateEvent;
use Thelia\Core\Event\ImagesCreateOrUpdateEvent;
use Thelia\Exception\ImageException;
use Thelia\Model\Admin;
use Thelia\Model\ProductImage;
@@ -94,7 +94,7 @@ class FileManagerTest extends \PHPUnit_Framework_TestCase {
$newUploadedFiles = array();
$actual = $fileManager->copyUploadedFile(24, ImageCreateOrUpdateEvent::TYPE_PRODUCT, $stubProductImage, $stubUploadedFile, $newUploadedFiles);
$actual = $fileManager->copyUploadedFile(24, ImagesCreateOrUpdateEvent::TYPE_PRODUCT, $stubProductImage, $stubUploadedFile, $newUploadedFiles);
$this->assertCount(1, $actual);
}
@@ -176,7 +176,7 @@ class FileManagerTest extends \PHPUnit_Framework_TestCase {
$newUploadedFiles = array();
$actual = $fileManager->copyUploadedFile(24, ImageCreateOrUpdateEvent::TYPE_PRODUCT, $stubProductImage, $stubUploadedFile, $newUploadedFiles);
$actual = $fileManager->copyUploadedFile(24, ImagesCreateOrUpdateEvent::TYPE_PRODUCT, $stubProductImage, $stubUploadedFile, $newUploadedFiles);
}
@@ -198,7 +198,7 @@ class FileManagerTest extends \PHPUnit_Framework_TestCase {
$fileManager = new FileManager($stubContainer);
$event = new ImageCreateOrUpdateEvent(ImageCreateOrUpdateEvent::TYPE_PRODUCT, 24);
$event = new ImagesCreateOrUpdateEvent(ImagesCreateOrUpdateEvent::TYPE_PRODUCT, 24);
$expected = 10;
$actual = $fileManager->saveImage($event, $stubProductImage);
@@ -224,7 +224,7 @@ class FileManagerTest extends \PHPUnit_Framework_TestCase {
$fileManager = new FileManager($stubContainer);
$event = new ImageCreateOrUpdateEvent(ImageCreateOrUpdateEvent::TYPE_CATEGORY, 24);
$event = new ImagesCreateOrUpdateEvent(ImagesCreateOrUpdateEvent::TYPE_CATEGORY, 24);
$expected = 10;
$actual = $fileManager->saveImage($event, $stubCategoryImage);
@@ -250,7 +250,7 @@ class FileManagerTest extends \PHPUnit_Framework_TestCase {
$fileManager = new FileManager($stubContainer);
$event = new ImageCreateOrUpdateEvent(ImageCreateOrUpdateEvent::TYPE_FOLDER, 24);
$event = new ImagesCreateOrUpdateEvent(ImagesCreateOrUpdateEvent::TYPE_FOLDER, 24);
$expected = 10;
$actual = $fileManager->saveImage($event, $stubFolderImage);
@@ -276,7 +276,7 @@ class FileManagerTest extends \PHPUnit_Framework_TestCase {
$fileManager = new FileManager($stubContainer);
$event = new ImageCreateOrUpdateEvent(ImageCreateOrUpdateEvent::TYPE_CONTENT, 24);
$event = new ImagesCreateOrUpdateEvent(ImagesCreateOrUpdateEvent::TYPE_CONTENT, 24);
$expected = 10;
$actual = $fileManager->saveImage($event, $stubContentImage);
@@ -302,7 +302,7 @@ class FileManagerTest extends \PHPUnit_Framework_TestCase {
->method('save')
->will($this->returnValue(10));
$event = new ImageCreateOrUpdateEvent('bad', 24);
$event = new ImagesCreateOrUpdateEvent('bad', 24);
$modelImage = new ProductImage();
$fileManager->saveImage($event, $modelImage);
@@ -326,7 +326,7 @@ class FileManagerTest extends \PHPUnit_Framework_TestCase {
->method('save')
->will($this->returnValue(0));
$event = new ImageCreateOrUpdateEvent(ImageCreateOrUpdateEvent::TYPE_PRODUCT, 24);
$event = new ImagesCreateOrUpdateEvent(ImagesCreateOrUpdateEvent::TYPE_PRODUCT, 24);
$fileManager->saveImage($event, $stubProductImage);
}

View File

@@ -24,14 +24,27 @@ namespace Thelia\Tools;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Thelia\Core\Event\ImageCreateOrUpdateEvent;
use Thelia\Core\Event\ImageDeleteEvent;
use Thelia\Core\Event\ImagesCreateOrUpdateEvent;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\Translation\Translator;
use Thelia\Exception\ImageException;
use Thelia\Form\CategoryImageModification;
use Thelia\Form\ContentImageModification;
use Thelia\Form\FolderImageModification;
use Thelia\Form\ProductImageModification;
use Thelia\Model\AdminLog;
use Thelia\Model\Base\CategoryImage;
use Thelia\Model\CategoryImage;
use Thelia\Model\CategoryImageQuery;
use Thelia\Model\CategoryQuery;
use Thelia\Model\ContentImage;
use Thelia\Model\ContentImageQuery;
use Thelia\Model\ContentQuery;
use Thelia\Model\FolderImage;
use Thelia\Model\FolderImageQuery;
use Thelia\Model\FolderQuery;
use Thelia\Model\ProductImage;
use Thelia\Model\ProductImageQuery;
use Thelia\Model\ProductQuery;
/**
* Created by JetBrains PhpStorm.
@@ -46,6 +59,12 @@ use Thelia\Model\ProductImage;
*/
class FileManager
{
CONST TYPE_PRODUCT = 'product';
CONST TYPE_CATEGORY = 'category';
CONST TYPE_CONTENT = 'content';
CONST TYPE_FOLDER = 'folder';
CONST TYPE_MODULE = 'module';
/** @var ContainerInterface Service Container */
protected $container = null;
@@ -70,20 +89,15 @@ class FileManager
* @param string $imageType Image type
* @param FolderImage|ContentImage|CategoryImage|ProductImage $modelImage Image saved
* @param UploadedFile $uploadedFile Ready to be uploaded file
* @param array $newUploadedFiles UploadedFile array to update
*
* @throws \Thelia\Exception\ImageException
* @return array Updated UploadedFile array
* @return UploadedFile
*/
public function copyUploadedFile($parentId, $imageType, $modelImage, $uploadedFile, $newUploadedFiles)
public function copyUploadedFile($parentId, $imageType, $modelImage, $uploadedFile)
{
if ($uploadedFile !== null) {
$directory = $modelImage->getUploadDir();
$fileName = $this->sanitizeFileName(
$uploadedFile->getClientOriginalName() . "-" . $modelImage->getId() . "." . strtolower(
$uploadedFile->getClientOriginalExtension()
)
);
$directory = $this->getUploadDir($imageType);
$fileName = $this->renameFile($modelImage->getId(), $uploadedFile);
$this->adminLogAppend(
$this->translator->trans(
@@ -98,7 +112,7 @@ class FileManager
)
);
$newUploadedFiles[] = array('file' => $uploadedFile->move($directory, $fileName));
$newUploadedFile = $uploadedFile->move($directory, $fileName);
$modelImage->setFile($fileName);
if (!$modelImage->save()) {
@@ -112,38 +126,38 @@ class FileManager
}
}
return $newUploadedFiles;
return $newUploadedFile;
}
/**
* Save image into the database
*
* @param ImageCreateOrUpdateEvent $event Image event
* @param ImagesCreateOrUpdateEvent $event Image event
* @param FolderImage|ContentImage|CategoryImage|ProductImage $modelImage Image to save
*
* @return int Nb lines modified
* @throws \Thelia\Exception\ImageException
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
*/
public function saveImage(ImageCreateOrUpdateEvent $event, $modelImage)
public function saveImage(ImagesCreateOrUpdateEvent $event, $modelImage)
{
$nbModifiedLines = 0;
if ($modelImage->getFile() !== null) {
switch ($event->getImageType()) {
case ImageCreateOrUpdateEvent::TYPE_PRODUCT:
case ImagesCreateOrUpdateEvent::TYPE_PRODUCT:
/** @var ProductImage $modelImage */
$modelImage->setProductId($event->getParentId());
break;
case ImageCreateOrUpdateEvent::TYPE_CATEGORY:
case ImagesCreateOrUpdateEvent::TYPE_CATEGORY:
/** @var CategoryImage $modelImage */
$modelImage->setCategoryId($event->getParentId());
break;
case ImageCreateOrUpdateEvent::TYPE_CONTENT:
case ImagesCreateOrUpdateEvent::TYPE_CONTENT:
/** @var ContentImage $modelImage */
$modelImage->setContentId($event->getParentId());
break;
case ImageCreateOrUpdateEvent::TYPE_FOLDER:
case ImagesCreateOrUpdateEvent::TYPE_FOLDER:
/** @var FolderImage $modelImage */
$modelImage->setFolderId($event->getParentId());
break;
@@ -213,10 +227,227 @@ class FileManager
* Delete image from file storage and database
*
* @param CategoryImage|ProductImage|ContentImage|FolderImage $imageModel Image being deleted
* @param string $parentType Parent type
*/
public function deleteImage($imageModel)
public function deleteImage($imageModel, $parentType)
{
unlink($imageModel->getAbsolutePath());
$url = $this->getUploadDir($parentType) . '/' . $imageModel->getFile();
unlink(str_replace('..', '', $url));
$imageModel->delete();
}
/**
* Get image model from type
*
* @param string $parentType Parent type
*
* @return null|\Thelia\Model\CategoryImage|\Thelia\Model\ContentImage|\Thelia\Model\FolderImage|\Thelia\Model\ProductImage
*
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
*/
public function getImageModel($parentType)
{
switch ($parentType) {
case ImagesCreateOrUpdateEvent::TYPE_PRODUCT:
$model = new ProductImage();
break;
case ImagesCreateOrUpdateEvent::TYPE_CATEGORY:
$model = new CategoryImage();
break;
case ImagesCreateOrUpdateEvent::TYPE_CONTENT:
$model = new ContentImage();
break;
case ImagesCreateOrUpdateEvent::TYPE_FOLDER:
$model = new FolderImage();
break;
default:
$model = null;
}
return $model;
}
/**
* Get image model query from type
*
* @param string $parentType
*
* @return null|\Thelia\Model\CategoryImageQuery|\Thelia\Model\ContentImageQuery|\Thelia\Model\FolderImageQuery|\Thelia\Model\ProductImageQuery
*
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
*/
public function getImageModelQuery($parentType)
{
switch ($parentType) {
case ImagesCreateOrUpdateEvent::TYPE_PRODUCT:
$model = new ProductImageQuery();
break;
case ImagesCreateOrUpdateEvent::TYPE_CATEGORY:
$model = new CategoryImageQuery();
break;
case ImagesCreateOrUpdateEvent::TYPE_CONTENT:
$model = new ContentImageQuery();
break;
case ImagesCreateOrUpdateEvent::TYPE_FOLDER:
$model = new FolderImageQuery();
break;
default:
$model = null;
}
return $model;
}
/**
* Get image parent model from type
*
* @param string $parentType
* @param int $parentId
*
* @return null|\Thelia\Model\Category|\Thelia\Model\Content|\Thelia\Model\Folder|\Thelia\Model\Product
*
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
*/
public function getParentImageModel($parentType, $parentId)
{
switch ($parentType) {
case ImagesCreateOrUpdateEvent::TYPE_PRODUCT:
$model = ProductQuery::create()->findPk($parentId);
break;
case ImagesCreateOrUpdateEvent::TYPE_CATEGORY:
$model = CategoryQuery::create()->findPk($parentId);
break;
case ImagesCreateOrUpdateEvent::TYPE_CONTENT:
$model = ContentQuery::create()->findPk($parentId);
break;
case ImagesCreateOrUpdateEvent::TYPE_FOLDER:
$model = FolderQuery::create()->findPk($parentId);
break;
default:
$model = null;
}
return $model;
}
/**
* Get image parent model from type
*
* @param string $parentType Parent type
* @param Request $request Request service
*
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
* @return ProductImageModification|CategoryImageModification|ContentImageModification|FolderImageModification
*/
public function getImageForm($parentType, Request $request)
{
switch ($parentType) {
case ImagesCreateOrUpdateEvent::TYPE_PRODUCT:
$form = new ProductImageModification($request);
break;
case ImagesCreateOrUpdateEvent::TYPE_CATEGORY:
$form = new CategoryImageModification($request);
break;
case ImagesCreateOrUpdateEvent::TYPE_CONTENT:
$form = new ContentImageModification($request);
break;
case ImagesCreateOrUpdateEvent::TYPE_FOLDER:
$form = new FolderImageModification($request);
break;
default:
$model = null;
}
return $form;
}
/**
* Get image upload dir
*
* @param string $parentType Parent type
*
* @return string Uri
*/
public function getUploadDir($parentType)
{
switch ($parentType) {
case ImagesCreateOrUpdateEvent::TYPE_PRODUCT:
$uri = THELIA_LOCAL_DIR . 'media/images/' . ImagesCreateOrUpdateEvent::TYPE_PRODUCT;
break;
case ImagesCreateOrUpdateEvent::TYPE_CATEGORY:
$uri = THELIA_LOCAL_DIR . 'media/images/' . ImagesCreateOrUpdateEvent::TYPE_CATEGORY;
break;
case ImagesCreateOrUpdateEvent::TYPE_CONTENT:
$uri = THELIA_LOCAL_DIR . 'media/images/' . ImagesCreateOrUpdateEvent::TYPE_CONTENT;
break;
case ImagesCreateOrUpdateEvent::TYPE_FOLDER:
$uri = THELIA_LOCAL_DIR . 'media/images/' . ImagesCreateOrUpdateEvent::TYPE_FOLDER;
break;
default:
$uri = null;
}
return $uri;
}
/**
* Deduce image redirecting URL from parent type
*
* @param string $parentType Parent type
* @param int $parentId Parent id
* @return string
*/
public function getRedirectionUrl($parentType, $parentId)
{
switch ($parentType) {
case ImagesCreateOrUpdateEvent::TYPE_PRODUCT:
$uri = '/admin/products/update?product_id=' . $parentId;
break;
case ImagesCreateOrUpdateEvent::TYPE_CATEGORY:
$uri = '/admin/categories/update?category_id=' . $parentId;
break;
case ImagesCreateOrUpdateEvent::TYPE_CONTENT:
$uri = '/admin/content/update/' . $parentId;
break;
case ImagesCreateOrUpdateEvent::TYPE_FOLDER:
$uri = '/admin/folders/update?folder_id=' . $parentId;
break;
default:
$uri = false;
}
return $uri;
}
/** @var array Available image parent type */
public static $availableType = array(
self::TYPE_PRODUCT,
self::TYPE_CATEGORY,
self::TYPE_CONTENT,
self::TYPE_FOLDER,
self::TYPE_MODULE
);
/**
* Rename file with image model id
*
* @param int $modelId Model id
* @param UploadedFile $uploadedFile File being saved
*
* @return string
*/
public function renameFile($modelId, $uploadedFile)
{
$extension = $uploadedFile->getClientOriginalExtension();
$fileName = $this->sanitizeFileName(
str_replace('.' . $extension, '', $uploadedFile->getClientOriginalName()) . "-" . $modelId . "." . strtolower(
$extension
)
);
return $fileName;
}
}