WIP : upload documents : add action, ctrl, event

This commit is contained in:
gmorel
2013-09-24 10:54:52 +02:00
parent 695b2b4d94
commit 084f66589a
24 changed files with 1507 additions and 284 deletions

View File

@@ -24,24 +24,38 @@ namespace Thelia\Tools;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Thelia\Core\Event\ImagesCreateOrUpdateEvent;
use Thelia\Core\Event\DocumentCreateOrUpdateEvent;
use Thelia\Core\Event\ImageCreateOrUpdateEvent;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\Translation\Translator;
use Thelia\Exception\ImageException;
use Thelia\Form\CategoryDocumentModification;
use Thelia\Form\CategoryImageModification;
use Thelia\Form\ContentDocumentModification;
use Thelia\Form\ContentImageModification;
use Thelia\Form\FolderDocumentModification;
use Thelia\Form\FolderImageModification;
use Thelia\Form\ProductDocumentModification;
use Thelia\Form\ProductImageModification;
use Thelia\Model\AdminLog;
use Thelia\Model\CategoryDocument;
use Thelia\Model\CategoryDocumentQuery;
use Thelia\Model\CategoryImage;
use Thelia\Model\CategoryImageQuery;
use Thelia\Model\CategoryQuery;
use Thelia\Model\ContentDocument;
use Thelia\Model\ContentDocumentQuery;
use Thelia\Model\ContentImage;
use Thelia\Model\ContentImageQuery;
use Thelia\Model\ContentQuery;
use Thelia\Model\Exception\InvalidArgumentException;
use Thelia\Model\FolderDocument;
use Thelia\Model\FolderDocumentQuery;
use Thelia\Model\FolderImage;
use Thelia\Model\FolderImageQuery;
use Thelia\Model\FolderQuery;
use Thelia\Model\ProductDocument;
use Thelia\Model\ProductDocumentQuery;
use Thelia\Model\ProductImage;
use Thelia\Model\ProductImageQuery;
use Thelia\Model\ProductQuery;
@@ -65,6 +79,9 @@ class FileManager
CONST TYPE_FOLDER = 'folder';
CONST TYPE_MODULE = 'module';
CONST FILE_TYPE_IMAGES = 'images';
CONST FILE_TYPE_DOCUMENTS = 'documents';
/** @var ContainerInterface Service Container */
protected $container = null;
@@ -85,42 +102,45 @@ class FileManager
/**
* Copy UploadedFile into the server storage directory
*
* @param int $parentId Parent id
* @param string $imageType Image type
* @param FolderImage|ContentImage|CategoryImage|ProductImage $modelImage Image saved
* @param UploadedFile $uploadedFile Ready to be uploaded file
* @param int $parentId Parent id
* @param string $fileType Image type
* @param FolderImage|ContentImage|CategoryImage|ProductImage|FolderDocument|ContentDocument|CategoryDocument|ProductDocument $model Model saved
* @param UploadedFile $uploadedFile Ready to be uploaded file
*
* @throws \Thelia\Exception\ImageException
* @return UploadedFile
*/
public function copyUploadedFile($parentId, $imageType, $modelImage, $uploadedFile)
public function copyUploadedFile($parentId, $fileType, $model, $uploadedFile)
{
$newUploadedFile = null;
if ($uploadedFile !== null) {
$directory = $this->getUploadDir($imageType);
$fileName = $this->renameFile($modelImage->getId(), $uploadedFile);
$directory = $this->getUploadDir($fileType, FileManager::FILE_TYPE_IMAGES);
$fileName = $this->renameFile($model->getId(), $uploadedFile);
$this->adminLogAppend(
$this->translator->trans(
'Uploading picture %pictureName% to %directory% for parent_id %parentId% (%parentType%)',
'Uploading %type% %fileName% to %directory% for parent_id %parentId% (%parentType%)',
array(
'%pictureName%' => $uploadedFile->getClientOriginalName(),
'%type%' => $fileType,
'%fileName%' => $uploadedFile->getClientOriginalName(),
'%directory%' => $directory . '/' . $fileName,
'%parentId%' => $parentId,
'%parentType%' => $imageType
'%parentType%' => $fileType
),
'image'
)
);
$newUploadedFile = $uploadedFile->move($directory, $fileName);
$modelImage->setFile($fileName);
$model->setFile($fileName);
if (!$modelImage->save()) {
if (!$model->save()) {
throw new ImageException(
sprintf(
'Image %s (%s) failed to be saved (image file)',
$modelImage->getFile(),
$imageType
'*s %s (%s) failed to be saved (image file)',
ucfirst($fileType),
$model->getFile(),
$fileType
)
);
}
@@ -132,32 +152,32 @@ class FileManager
/**
* Save image into the database
*
* @param ImagesCreateOrUpdateEvent $event Image event
* @param ImageCreateOrUpdateEvent $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(ImagesCreateOrUpdateEvent $event, $modelImage)
public function saveImage(ImageCreateOrUpdateEvent $event, $modelImage)
{
$nbModifiedLines = 0;
if ($modelImage->getFile() !== null) {
switch ($event->getImageType()) {
case ImagesCreateOrUpdateEvent::TYPE_PRODUCT:
case self::TYPE_PRODUCT:
/** @var ProductImage $modelImage */
$modelImage->setProductId($event->getParentId());
break;
case ImagesCreateOrUpdateEvent::TYPE_CATEGORY:
case self::TYPE_CATEGORY:
/** @var CategoryImage $modelImage */
$modelImage->setCategoryId($event->getParentId());
break;
case ImagesCreateOrUpdateEvent::TYPE_CONTENT:
case self::TYPE_CONTENT:
/** @var ContentImage $modelImage */
$modelImage->setContentId($event->getParentId());
break;
case ImagesCreateOrUpdateEvent::TYPE_FOLDER:
case self::TYPE_FOLDER:
/** @var FolderImage $modelImage */
$modelImage->setFolderId($event->getParentId());
break;
@@ -167,7 +187,7 @@ class FileManager
'Picture parent type is unknown (available types : %s)',
implode(
',',
$event->getAvailableType()
self::getAvailableTypes()
)
)
);
@@ -187,6 +207,64 @@ class FileManager
return $nbModifiedLines;
}
/**
* Save document into the database
*
* @param DocumentCreateOrUpdateEvent $event Image event
* @param FolderDocument|ContentDocument|CategoryDocument|ProductDocument $modelDocument Document to save
*
* @throws \Thelia\Model\Exception\InvalidArgumentException
* @return int Nb lines modified
* @todo refactor make all documents using propel inheritance and factorise image behaviour into one single clean action
*/
public function saveDocument(DocumentCreateOrUpdateEvent $event, $modelDocument)
{
$nbModifiedLines = 0;
if ($modelDocument->getFile() !== null) {
switch ($event->getDocumentType()) {
case self::TYPE_PRODUCT:
/** @var ProductImage $modelImage */
$modelDocument->setProductId($event->getParentId());
break;
case self::TYPE_CATEGORY:
/** @var CategoryImage $modelImage */
$modelDocument->setCategoryId($event->getParentId());
break;
case self::TYPE_CONTENT:
/** @var ContentImage $modelImage */
$modelDocument->setContentId($event->getParentId());
break;
case self::TYPE_FOLDER:
/** @var FolderImage $modelImage */
$modelDocument->setFolderId($event->getParentId());
break;
default:
throw new InvalidArgumentException(
sprintf(
'Document parent type is unknown (available types : %s)',
implode(
',',
self::getAvailableTypes()
)
)
);
}
$nbModifiedLines = $modelDocument->save();
if (!$nbModifiedLines) {
throw new InvalidArgumentException(
sprintf(
'Document %s failed to be saved (document content)',
$modelDocument->getFile()
)
);
}
}
return $nbModifiedLines;
}
/**
* Sanitizes a filename replacing whitespace with dashes
*
@@ -226,14 +304,17 @@ class FileManager
/**
* Delete image from file storage and database
*
* @param CategoryImage|ProductImage|ContentImage|FolderImage $imageModel Image being deleted
* @param string $parentType Parent type
* @param CategoryImage|ProductImage|ContentImage|FolderImage|CategoryDocument|ProductDocument|ContentDocument|FolderDocument $model File being deleted
* @param string $parentType Parent type
* @param string $fileType File type ex FileManager::FILE_TYPE_DOCUMENTS
*
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
*/
public function deleteImage($imageModel, $parentType)
public function deleteFile($model, $parentType, $fileType)
{
$url = $this->getUploadDir($parentType) . '/' . $imageModel->getFile();
$url = $this->getUploadDir($parentType, $fileType) . '/' . $model->getFile();
unlink(str_replace('..', '', $url));
$imageModel->delete();
$model->delete();
}
@@ -249,16 +330,16 @@ class FileManager
public function getImageModel($parentType)
{
switch ($parentType) {
case ImagesCreateOrUpdateEvent::TYPE_PRODUCT:
case self::TYPE_PRODUCT:
$model = new ProductImage();
break;
case ImagesCreateOrUpdateEvent::TYPE_CATEGORY:
case self::TYPE_CATEGORY:
$model = new CategoryImage();
break;
case ImagesCreateOrUpdateEvent::TYPE_CONTENT:
case self::TYPE_CONTENT:
$model = new ContentImage();
break;
case ImagesCreateOrUpdateEvent::TYPE_FOLDER:
case self::TYPE_FOLDER:
$model = new FolderImage();
break;
default:
@@ -268,6 +349,37 @@ class FileManager
return $model;
}
/**
* Get document model from type
*
* @param string $parentType Parent type
*
* @return null|ProductDocument|CategoryDocument|ContentDocument|FolderDocument
*
* @todo refactor make all documents using propel inheritance and factorise image behaviour into one single clean action
*/
public function getDocumentModel($parentType)
{
switch ($parentType) {
case self::TYPE_PRODUCT:
$model = new ProductDocument();
break;
case self::TYPE_CATEGORY:
$model = new CategoryDocument();
break;
case self::TYPE_CONTENT:
$model = new ContentDocument();
break;
case self::TYPE_FOLDER:
$model = new FolderDocument();
break;
default:
$model = null;
}
return $model;
}
/**
* Get image model query from type
*
@@ -280,16 +392,16 @@ class FileManager
public function getImageModelQuery($parentType)
{
switch ($parentType) {
case ImagesCreateOrUpdateEvent::TYPE_PRODUCT:
case self::TYPE_PRODUCT:
$model = new ProductImageQuery();
break;
case ImagesCreateOrUpdateEvent::TYPE_CATEGORY:
case self::TYPE_CATEGORY:
$model = new CategoryImageQuery();
break;
case ImagesCreateOrUpdateEvent::TYPE_CONTENT:
case self::TYPE_CONTENT:
$model = new ContentImageQuery();
break;
case ImagesCreateOrUpdateEvent::TYPE_FOLDER:
case self::TYPE_FOLDER:
$model = new FolderImageQuery();
break;
default:
@@ -299,6 +411,37 @@ class FileManager
return $model;
}
/**
* Get document model query from type
*
* @param string $parentType
*
* @return null|ProductDocumentQuery|CategoryDocumentQuery|ContentDocumentQuery|FolderDocumentQuery
*
* @todo refactor make all documents using propel inheritance and factorise image behaviour into one single clean action
*/
public function getDocumentModelQuery($parentType)
{
switch ($parentType) {
case self::TYPE_PRODUCT:
$model = new ProductDocumentQuery();
break;
case self::TYPE_CATEGORY:
$model = new CategoryDocumentQuery();
break;
case self::TYPE_CONTENT:
$model = new ContentDocumentQuery();
break;
case self::TYPE_FOLDER:
$model = new FolderDocumentQuery();
break;
default:
$model = null;
}
return $model;
}
/**
* Get image parent model from type
*
@@ -309,19 +452,19 @@ class FileManager
*
* @todo refactor make all pictures using propel inheritance and factorise image behaviour into one single clean action
*/
public function getParentImageModel($parentType, $parentId)
public function getParentFIleModel($parentType, $parentId)
{
switch ($parentType) {
case ImagesCreateOrUpdateEvent::TYPE_PRODUCT:
case self::TYPE_PRODUCT:
$model = ProductQuery::create()->findPk($parentId);
break;
case ImagesCreateOrUpdateEvent::TYPE_CATEGORY:
case self::TYPE_CATEGORY:
$model = CategoryQuery::create()->findPk($parentId);
break;
case ImagesCreateOrUpdateEvent::TYPE_CONTENT:
case self::TYPE_CONTENT:
$model = ContentQuery::create()->findPk($parentId);
break;
case ImagesCreateOrUpdateEvent::TYPE_FOLDER:
case self::TYPE_FOLDER:
$model = FolderQuery::create()->findPk($parentId);
break;
default:
@@ -343,20 +486,52 @@ class FileManager
public function getImageForm($parentType, Request $request)
{
switch ($parentType) {
case ImagesCreateOrUpdateEvent::TYPE_PRODUCT:
case self::TYPE_PRODUCT:
$form = new ProductImageModification($request);
break;
case ImagesCreateOrUpdateEvent::TYPE_CATEGORY:
case self::TYPE_CATEGORY:
$form = new CategoryImageModification($request);
break;
case ImagesCreateOrUpdateEvent::TYPE_CONTENT:
case self::TYPE_CONTENT:
$form = new ContentImageModification($request);
break;
case ImagesCreateOrUpdateEvent::TYPE_FOLDER:
case self::TYPE_FOLDER:
$form = new FolderImageModification($request);
break;
default:
$model = null;
$form = null;
}
return $form;
}
/**
* Get document parent model from type
*
* @param string $parentType Parent type
* @param Request $request Request service
*
* @todo refactor make all document using propel inheritance and factorise image behaviour into one single clean action
* @return ProductDocumentModification|CategoryDocumentModification|ContentDocumentModification|FolderDocumentModification
*/
public function getDocumentForm($parentType, Request $request)
{
switch ($parentType) {
case self::TYPE_PRODUCT:
$form = new ProductDocumentModification($request);
break;
case self::TYPE_CATEGORY:
$form = new CategoryDocumentModification($request);
break;
case self::TYPE_CONTENT:
$form = new ContentDocumentModification($request);
break;
case self::TYPE_FOLDER:
$form = new FolderDocumentModification($request);
break;
default:
$form = null;
}
return $form;
@@ -366,27 +541,32 @@ class FileManager
/**
* Get image upload dir
*
* @param string $parentType Parent type
* @param string $parentType Parent type ex FileManager::TYPE_PRODUCT
* @param string $fileType File type
*
* @return string Uri
*/
public function getUploadDir($parentType)
public function getUploadDir($parentType, $fileType)
{
if (!in_array($fileType, self::$availableFileType)) {
return false;
}
switch ($parentType) {
case ImagesCreateOrUpdateEvent::TYPE_PRODUCT:
$uri = THELIA_LOCAL_DIR . 'media/images/' . ImagesCreateOrUpdateEvent::TYPE_PRODUCT;
case self::TYPE_PRODUCT:
$uri = THELIA_LOCAL_DIR . 'media/' . $fileType . '/' . self::TYPE_PRODUCT;
break;
case ImagesCreateOrUpdateEvent::TYPE_CATEGORY:
$uri = THELIA_LOCAL_DIR . 'media/images/' . ImagesCreateOrUpdateEvent::TYPE_CATEGORY;
case self::TYPE_CATEGORY:
$uri = THELIA_LOCAL_DIR . 'media/' . $fileType . '/' . self::TYPE_CATEGORY;
break;
case ImagesCreateOrUpdateEvent::TYPE_CONTENT:
$uri = THELIA_LOCAL_DIR . 'media/images/' . ImagesCreateOrUpdateEvent::TYPE_CONTENT;
case self::TYPE_CONTENT:
$uri = THELIA_LOCAL_DIR . 'media/' . $fileType . '/' . self::TYPE_CONTENT;
break;
case ImagesCreateOrUpdateEvent::TYPE_FOLDER:
$uri = THELIA_LOCAL_DIR . 'media/images/' . ImagesCreateOrUpdateEvent::TYPE_FOLDER;
case self::TYPE_FOLDER:
$uri = THELIA_LOCAL_DIR . 'media/' . $fileType . '/' . self::TYPE_FOLDER;
break;
default:
$uri = null;
$uri = false;
}
return $uri;
@@ -403,16 +583,16 @@ class FileManager
public function getRedirectionUrl($parentType, $parentId)
{
switch ($parentType) {
case ImagesCreateOrUpdateEvent::TYPE_PRODUCT:
case self::TYPE_PRODUCT:
$uri = '/admin/products/update?product_id=' . $parentId . '&current_tab=images';
break;
case ImagesCreateOrUpdateEvent::TYPE_CATEGORY:
case self::TYPE_CATEGORY:
$uri = '/admin/categories/update?category_id=' . $parentId . '&current_tab=images';
break;
case ImagesCreateOrUpdateEvent::TYPE_CONTENT:
case self::TYPE_CONTENT:
$uri = '/admin/content/update/' . $parentId . '&current_tab=images';
break;
case ImagesCreateOrUpdateEvent::TYPE_FOLDER:
case self::TYPE_FOLDER:
$uri = '/admin/folders/update/' . $parentId . '&current_tab=images';
break;
default:
@@ -423,7 +603,7 @@ class FileManager
}
/** @var array Available image parent type */
/** @var array Available file parent type */
public static $availableType = array(
self::TYPE_PRODUCT,
self::TYPE_CATEGORY,
@@ -432,6 +612,12 @@ class FileManager
self::TYPE_MODULE
);
/** @var array Available file type type */
public static $availableFileType = array(
self::FILE_TYPE_DOCUMENTS,
self::FILE_TYPE_IMAGES
);
/**
* Rename file with image model id
*
@@ -470,4 +656,20 @@ class FileManager
return $isValid;
}
/**
* Return all document and image types
*
* @return array
*/
public static function getAvailableTypes()
{
return array(
self::TYPE_CATEGORY,
self::TYPE_CONTENT,
self::TYPE_FOLDER,
self::TYPE_PRODUCT,
self::TYPE_MODULE,
);
}
}