WIP : upload documents : add action, ctrl, event
This commit is contained in:
@@ -28,19 +28,21 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Router;
|
||||
use Symfony\Component\Validator\Constraints\Image;
|
||||
use Symfony\Component\Validator\Constraints\ImageValidator;
|
||||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
||||
use Thelia\Core\Event\DocumentCreateOrUpdateEvent;
|
||||
use Thelia\Core\Event\DocumentDeleteEvent;
|
||||
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\Exception\FormValidationException;
|
||||
use Thelia\Log\Tlog;
|
||||
use Thelia\Model\CategoryDocument;
|
||||
use Thelia\Model\CategoryImage;
|
||||
use Thelia\Model\ContentDocument;
|
||||
use Thelia\Model\ContentImage;
|
||||
use Thelia\Model\FolderDocument;
|
||||
use Thelia\Model\FolderImage;
|
||||
use Thelia\Model\ProductDocument;
|
||||
use Thelia\Model\ProductImage;
|
||||
use Thelia\Tools\FileManager;
|
||||
use Thelia\Tools\Rest\ResponseRest;
|
||||
@@ -59,21 +61,8 @@ use Thelia\Tools\Rest\ResponseRest;
|
||||
*/
|
||||
class FileController extends BaseAdminController
|
||||
{
|
||||
/**
|
||||
* Manage how a file collection has to be saved
|
||||
*
|
||||
* @param int $parentId Parent id owning files being saved
|
||||
* @param string $parentType Parent Type owning files being saved
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function saveFilesAction($parentId, $parentType)
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage how a image collection has to be saved
|
||||
*
|
||||
@@ -118,7 +107,7 @@ class FileController extends BaseAdminController
|
||||
return new ResponseRest($message, 'text', 415);
|
||||
}
|
||||
|
||||
$parentModel = $fileManager->getParentImageModel($parentType, $parentId);
|
||||
$parentModel = $fileManager->getParentFIleModel($parentType, $parentId);
|
||||
$imageModel = $fileManager->getImageModel($parentType);
|
||||
|
||||
if ($parentModel === null || $imageModel === null || $fileBeingUploaded === null) {
|
||||
@@ -152,6 +141,73 @@ class FileController extends BaseAdminController
|
||||
return new Response('', 404);
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage how a document collection has to be saved
|
||||
*
|
||||
* @param int $parentId Parent id owning documents being saved
|
||||
* @param string $parentType Parent Type owning documents being saved
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function saveDocumentAjaxAction($parentId, $parentType)
|
||||
{
|
||||
$this->checkAuth('ADMIN', 'admin.document.save');
|
||||
$this->checkXmlHttpRequest();
|
||||
|
||||
if ($this->isParentTypeValid($parentType)) {
|
||||
if ($this->getRequest()->isMethod('POST')) {
|
||||
|
||||
/** @var UploadedFile $fileBeingUploaded */
|
||||
$fileBeingUploaded = $this->getRequest()->files->get('file');
|
||||
|
||||
$fileManager = new FileManager($this->container);
|
||||
|
||||
// Validate if file is too big
|
||||
if ($fileBeingUploaded->getError() == 1) {
|
||||
$message = $this->getTranslator()
|
||||
->trans(
|
||||
'File is too heavy, please retry with a file having a size less than %size%.',
|
||||
array('%size%' => ini_get('post_max_size')),
|
||||
'document'
|
||||
);
|
||||
|
||||
return new ResponseRest($message, 'text', 403);
|
||||
}
|
||||
|
||||
$parentModel = $fileManager->getParentFileModel($parentType, $parentId);
|
||||
$documentModel = $fileManager->getDocumentModel($parentType);
|
||||
|
||||
if ($parentModel === null || $documentModel === null || $fileBeingUploaded === null) {
|
||||
return new Response('', 404);
|
||||
}
|
||||
|
||||
$defaultTitle = $parentModel->getTitle();
|
||||
$documentModel->setParentId($parentId);
|
||||
$documentModel->setTitle($defaultTitle);
|
||||
|
||||
$documentCreateOrUpdateEvent = new DocumentCreateOrUpdateEvent(
|
||||
$parentType,
|
||||
$parentId
|
||||
);
|
||||
$documentCreateOrUpdateEvent->setModelDocument($documentModel);
|
||||
$documentCreateOrUpdateEvent->setUploadedFile($fileBeingUploaded);
|
||||
$documentCreateOrUpdateEvent->setParentName($parentModel->getTitle());
|
||||
|
||||
|
||||
// Dispatch Event to the Action
|
||||
$this->dispatch(
|
||||
TheliaEvents::DOCUMENT_SAVE,
|
||||
$documentCreateOrUpdateEvent
|
||||
);
|
||||
|
||||
|
||||
return new ResponseRest(array('status' => true, 'message' => ''));
|
||||
}
|
||||
}
|
||||
|
||||
return new Response('', 404);
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage how a image list will be displayed in AJAX
|
||||
*
|
||||
@@ -169,6 +225,23 @@ class FileController extends BaseAdminController
|
||||
return $this->render('includes/image-upload-list-ajax', $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage how a document list will be displayed in AJAX
|
||||
*
|
||||
* @param int $parentId Parent id owning documents being saved
|
||||
* @param string $parentType Parent Type owning documents being saved
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function getDocumentListAjaxAction($parentId, $parentType)
|
||||
{
|
||||
$this->checkAuth('ADMIN', 'admin.document.save');
|
||||
$this->checkXmlHttpRequest();
|
||||
$args = array('documentType' => $parentType, 'parentId' => $parentId);
|
||||
|
||||
return $this->render('includes/document-upload-list-ajax', $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage how an image list will be uploaded in AJAX
|
||||
*
|
||||
@@ -186,6 +259,23 @@ class FileController extends BaseAdminController
|
||||
return $this->render('includes/image-upload-form', $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage how an document list will be uploaded in AJAX
|
||||
*
|
||||
* @param int $parentId Parent id owning documents being saved
|
||||
* @param string $parentType Parent Type owning documents being saved
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function getDocumentFormAjaxAction($parentId, $parentType)
|
||||
{
|
||||
$this->checkAuth('ADMIN', 'admin.document.save');
|
||||
$this->checkXmlHttpRequest();
|
||||
$args = array('documentType' => $parentType, 'parentId' => $parentId);
|
||||
|
||||
return $this->render('includes/document-upload-form', $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage how an image is viewed
|
||||
*
|
||||
@@ -209,7 +299,35 @@ class FileController extends BaseAdminController
|
||||
'imageType' => $parentType,
|
||||
'redirectUrl' => $redirectUrl
|
||||
));
|
||||
} catch (Exception $e) {
|
||||
} catch (\Exception $e) {
|
||||
$this->pageNotFound();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage how an document is viewed
|
||||
*
|
||||
* @param int $documentId Parent id owning images being saved
|
||||
* @param string $parentType Parent Type owning images being saved
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function viewDocumentAction($documentId, $parentType)
|
||||
{
|
||||
if (null !== $response = $this->checkAuth('admin.document.view')) {
|
||||
return $response;
|
||||
}
|
||||
try {
|
||||
$fileManager = new FileManager($this->container);
|
||||
$document = $fileManager->getDocumentModelQuery($parentType)->findPk($documentId);
|
||||
$redirectUrl = $fileManager->getRedirectionUrl($parentType, $document->getParentId());
|
||||
|
||||
return $this->render('document-edit', array(
|
||||
'documentId' => $documentId,
|
||||
'documentType' => $parentType,
|
||||
'redirectUrl' => $redirectUrl
|
||||
));
|
||||
} catch (\Exception $e) {
|
||||
$this->pageNotFound();
|
||||
}
|
||||
}
|
||||
@@ -237,12 +355,12 @@ class FileController extends BaseAdminController
|
||||
$image = $fileManager->getImageModelQuery($parentType)->findPk($imageId);
|
||||
$oldImage = clone $image;
|
||||
if (null === $image) {
|
||||
throw new \InvalidArgumentException(sprintf('%d image id does not exists', $imageId));
|
||||
throw new \InvalidArgumentException(sprintf('%d image id does not exist', $imageId));
|
||||
}
|
||||
|
||||
$form = $this->validateForm($imageModification);
|
||||
|
||||
$event = $this->createEventInstance($parentType, $image, $form->getData());
|
||||
$event = $this->createImageEventInstance($parentType, $image, $form->getData());
|
||||
$event->setOldModelImage($oldImage);
|
||||
|
||||
$files = $this->getRequest()->files;
|
||||
@@ -288,14 +406,87 @@ class FileController extends BaseAdminController
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage how a image has to be deleted (AJAX)
|
||||
* Manage how an document is updated
|
||||
*
|
||||
* @param int $imageId Parent id owning images being saved
|
||||
* @param string $parentType Parent Type owning images being saved
|
||||
* @param int $documentId Parent id owning documents being saved
|
||||
* @param string $parentType Parent Type owning documents being saved
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function deleteImagesAction($imageId, $parentType)
|
||||
public function updateDocumentAction($documentId, $parentType)
|
||||
{
|
||||
if (null !== $response = $this->checkAuth('admin.image.update')) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$message = false;
|
||||
|
||||
$fileManager = new FileManager($this->container);
|
||||
$documentModification = $fileManager->getImageForm($parentType, $this->getRequest());
|
||||
|
||||
try {
|
||||
$document = $fileManager->getDocumentModelQuery($parentType)->findPk($documentId);
|
||||
$oldDocument = clone $document;
|
||||
if (null === $document) {
|
||||
throw new \InvalidArgumentException(sprintf('%d document id does not exist', $documentId));
|
||||
}
|
||||
|
||||
$form = $this->validateForm($documentModification);
|
||||
|
||||
$event = $this->createImageEventInstance($parentType, $document, $form->getData());
|
||||
$event->setOldModelImage($oldDocument);
|
||||
|
||||
$files = $this->getRequest()->files;
|
||||
$fileForm = $files->get($documentModification->getName());
|
||||
if (isset($fileForm['file'])) {
|
||||
$event->setUploadedFile($fileForm['file']);
|
||||
}
|
||||
|
||||
$this->dispatch(TheliaEvents::DOCUMENT_UPDATE, $event);
|
||||
|
||||
$documentUpdated = $event->getModelImage();
|
||||
|
||||
$this->adminLogAppend(sprintf('Document with Ref %s (ID %d) modified', $documentUpdated->getTitle(), $documentUpdated->getId()));
|
||||
|
||||
if ($this->getRequest()->get('save_mode') == 'close') {
|
||||
$this->redirectToRoute('admin.documents');
|
||||
} else {
|
||||
$this->redirectSuccess($documentModification);
|
||||
}
|
||||
|
||||
} 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 document editing : %s.', $message));
|
||||
|
||||
$documentModification->setErrorMessage($message);
|
||||
|
||||
$this->getParserContext()
|
||||
->addForm($documentModification)
|
||||
->setGeneralError($message);
|
||||
}
|
||||
|
||||
return $this->render('document-edit', array(
|
||||
'documentId' => $documentId,
|
||||
'documentType' => $parentType
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage how a image has to be deleted (AJAX)
|
||||
*
|
||||
* @param int $imageId Parent id owning image being deleted
|
||||
* @param string $parentType Parent Type owning image being deleted
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function deleteImageAction($imageId, $parentType)
|
||||
{
|
||||
$this->checkAuth('ADMIN', 'admin.image.delete');
|
||||
$this->checkXmlHttpRequest();
|
||||
@@ -330,6 +521,49 @@ class FileController extends BaseAdminController
|
||||
return new Response($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage how a document has to be deleted (AJAX)
|
||||
*
|
||||
* @param int $documentId Parent id owning document being deleted
|
||||
* @param string $parentType Parent Type owning document being deleted
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function deleteDocumentAction($documentId, $parentType)
|
||||
{
|
||||
$this->checkAuth('ADMIN', 'admin.document.delete');
|
||||
$this->checkXmlHttpRequest();
|
||||
|
||||
$fileManager = new FileManager($this->container);
|
||||
$documentModelQuery = $fileManager->getDocumentModelQuery($parentType);
|
||||
$model = $documentModelQuery->findPk($documentId);
|
||||
|
||||
if ($model == null) {
|
||||
return $this->pageNotFound();
|
||||
}
|
||||
|
||||
// Feed event
|
||||
$documentDeleteEvent = new DocumentDeleteEvent(
|
||||
$model,
|
||||
$parentType
|
||||
);
|
||||
|
||||
// Dispatch Event to the Action
|
||||
$this->dispatch(
|
||||
TheliaEvents::DOCUMENT_DELETE,
|
||||
$documentDeleteEvent
|
||||
);
|
||||
|
||||
$message = $this->getTranslator()
|
||||
->trans(
|
||||
'Document deleted successfully',
|
||||
array(),
|
||||
'document'
|
||||
);
|
||||
|
||||
return new Response($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log error message
|
||||
*
|
||||
@@ -362,11 +596,11 @@ class FileController extends BaseAdminController
|
||||
*/
|
||||
public function isParentTypeValid($parentType)
|
||||
{
|
||||
return (in_array($parentType, ImagesCreateOrUpdateEvent::getAvailableType()));
|
||||
return (in_array($parentType, FileManager::getAvailableTypes()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Event instance
|
||||
* Create Image Event instance
|
||||
*
|
||||
* @param string $parentType Parent Type owning images being saved
|
||||
* @param CategoryImage|ProductImage|ContentImage|FolderImage $model Image model
|
||||
@@ -374,7 +608,7 @@ class FileController extends BaseAdminController
|
||||
*
|
||||
* @return ImageCreateOrUpdateEvent
|
||||
*/
|
||||
private function createEventInstance($parentType, $model, $data)
|
||||
protected function createImageEventInstance($parentType, $model, $data)
|
||||
{
|
||||
$imageCreateEvent = new ImageCreateOrUpdateEvent($parentType, null);
|
||||
|
||||
@@ -399,5 +633,39 @@ class FileController extends BaseAdminController
|
||||
return $imageCreateEvent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Document Event instance
|
||||
*
|
||||
* @param string $parentType Parent Type owning documents being saved
|
||||
* @param CategoryDocument|ProductDocument|ContentDocument|FolderDocument $model Document model
|
||||
* @param array $data Post data
|
||||
*
|
||||
* @return ImageCreateOrUpdateEvent
|
||||
*/
|
||||
protected function createDocumentEventInstance($parentType, $model, $data)
|
||||
{
|
||||
$documentCreateEvent = new DocumentCreateOrUpdateEvent($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']);
|
||||
}
|
||||
|
||||
$documentCreateEvent->setModelDocument($model);
|
||||
|
||||
return $documentCreateEvent;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user