Restoired file events compatibility, factorised code

This commit is contained in:
Franck Allimant
2014-06-26 20:18:06 +02:00
parent fcceaa630d
commit d969f84ae0
11 changed files with 846 additions and 865 deletions

View File

@@ -14,10 +14,8 @@ namespace Thelia\Controller\Admin;
use Propel\Runtime\Exception\PropelException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Thelia\Core\Event\Document\DocumentCreateOrUpdateEvent;
use Thelia\Core\Event\Document\DocumentDeleteEvent;
use Thelia\Core\Event\Image\ImageCreateOrUpdateEvent;
use Thelia\Core\Event\Image\ImageDeleteEvent;
use Thelia\Core\Event\File\FileCreateOrUpdateEvent;
use Thelia\Core\Event\Image\FileDeleteEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Event\UpdateFilePositionEvent;
use Thelia\Core\HttpFoundation\Response;
@@ -56,107 +54,16 @@ class FileController extends BaseAdminController
}
/**
* Manage how a image collection has to be saved
* Manage how a file collection has to be saved
*
* @param int $parentId Parent id owning images being saved
* @param string $parentType Parent Type owning images being saved
* @param int $parentId Parent id owning files being saved
* @param string $parentType Parent Type owning files being saved (product, category, content, etc.)
* @param string $objectType Object type, e.g. image or document
* @param array $validMimeTypes an array of valid mime types. If empty, any mime type is allowed.
*
* @return Response
*/
public function saveImageAjaxAction($parentId, $parentType)
{
$this->checkAuth(AdminResources::retrieve($parentType), array(), AccessManager::UPDATE);
$this->checkXmlHttpRequest();
if ($this->getRequest()->isMethod('POST')) {
/** @var UploadedFile $fileBeingUploaded */
$fileBeingUploaded = $this->getRequest()->files->get('file');
$fileManager = $this->getFileManager();
// 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('upload_max_filesize')),
'core'
);
return new ResponseRest($message, 'text', 403);
}
// Validate if it is a image or file
if (!$fileManager->isImage($fileBeingUploaded->getMimeType())) {
$message = $this->getTranslator()
->trans(
'You can only upload images (.png, .jpg, .jpeg, .gif)',
array(),
'image'
);
return new ResponseRest($message, 'text', 415);
}
$imageModel = $fileManager->getModelInstance('image', $parentType);
$parentModel = $imageModel->getParentFileModel();
if ($parentModel === null || $imageModel === null || $fileBeingUploaded === null) {
return new Response('', 404);
}
$defaultTitle = $parentModel->getTitle();
if (empty($defaultTitle)) {
$defaultTitle = $fileBeingUploaded->getClientOriginalName();
}
$imageModel
->setParentId($parentId)
->setLocale(Lang::getDefaultLanguage()->getLocale())
->setTitle($defaultTitle)
;
$imageCreateOrUpdateEvent = new ImageCreateOrUpdateEvent($parentId);
$imageCreateOrUpdateEvent->setModelImage($imageModel);
$imageCreateOrUpdateEvent->setUploadedFile($fileBeingUploaded);
$imageCreateOrUpdateEvent->setParentName($parentModel->getTitle());
// Dispatch Event to the Action
$this->dispatch(
TheliaEvents::IMAGE_SAVE,
$imageCreateOrUpdateEvent
);
$this->adminLogAppend(
AdminResources::retrieve($parentType),
AccessManager::UPDATE,
$this->container->get('thelia.translator')->trans(
'Saving images for %parentName% parent id %parentId%',
array(
'%parentName%' => $imageCreateOrUpdateEvent->getParentName(),
'%parentId%' => $imageCreateOrUpdateEvent->getParentId()
),
'image'
)
);
return new ResponseRest(array('status' => true, 'message' => ''));
}
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)
public function saveFileAjaxAction($parentId, $parentType, $objectType, $validMimeTypes = array())
{
$this->checkAuth(AdminResources::retrieve($parentType), array(), AccessManager::UPDATE);
$this->checkXmlHttpRequest();
@@ -173,46 +80,76 @@ class FileController extends BaseAdminController
$message = $this->getTranslator()
->trans(
'File is too large, please retry with a file having a size less than %size%.',
array('%size%' => ini_get('post_max_size')),
'document'
array('%size%' => ini_get('upload_max_filesize')),
'core'
);
return new ResponseRest($message, 'text', 403);
}
$documentModel = $fileManager->getModelInstance('document', $parentType);
$parentModel = $documentModel->getParentFileModel($parentType, $parentId);
if (! empty($validMimeTypes)) {
if ($parentModel === null || $documentModel === null || $fileBeingUploaded === null) {
// Check if we have the proper file type
$isValid = false;
$mimeType = $fileBeingUploaded->getMimeType();
if (in_array($mimeType, $validMimeTypes)) {
$isValid = true;
}
if (! $isValid) {
$message = $this->getTranslator()
->trans(
'Only files having the following mime type are allowed: %types%',
[ '%types%' => implode(', ', $validMimeTypes)]
);
return new ResponseRest($message, 'text', 415);
}
}
$fileModel = $fileManager->getModelInstance($objectType, $parentType);
$parentModel = $fileModel->getParentFileModel();
if ($parentModel === null || $fileModel === null || $fileBeingUploaded === null) {
return new Response('', 404);
}
$documentModel->setParentId($parentId);
$documentModel->setLocale(Lang::getDefaultLanguage()->getLocale());
$documentModel->setTitle($fileBeingUploaded->getClientOriginalName());
$defaultTitle = $parentModel->getTitle();
$documentCreateOrUpdateEvent = new DocumentCreateOrUpdateEvent($parentId);
if (empty($defaultTitle)) {
$defaultTitle = $fileBeingUploaded->getClientOriginalName();
}
$documentCreateOrUpdateEvent->setModelDocument($documentModel);
$documentCreateOrUpdateEvent->setUploadedFile($fileBeingUploaded);
$documentCreateOrUpdateEvent->setParentName($parentModel->getTitle());
$fileModel
->setParentId($parentId)
->setLocale(Lang::getDefaultLanguage()->getLocale())
->setTitle($defaultTitle)
;
$fileCreateOrUpdateEvent = new FileCreateOrUpdateEvent($parentId);
$fileCreateOrUpdateEvent->setModel($fileModel);
$fileCreateOrUpdateEvent->setUploadedFile($fileBeingUploaded);
$fileCreateOrUpdateEvent->setParentName($parentModel->getTitle());
// Dispatch Event to the Action
$this->dispatch(
TheliaEvents::DOCUMENT_SAVE,
$documentCreateOrUpdateEvent
TheliaEvents::IMAGE_SAVE,
$fileCreateOrUpdateEvent
);
$this->adminLogAppend(
AdminResources::retrieve($parentType),
AccessManager::UPDATE,
$this->container->get('thelia.translator')->trans(
'Saving document for %parentName% parent id %parentId%',
$this->getTranslator()->trans(
'Saving %obj% for %parentName% parent id %parentId%',
array(
'%parentName%' => $documentCreateOrUpdateEvent->getParentName(),
'%parentId%' => $documentCreateOrUpdateEvent->getParentId()
),
'document'
'%parentName%' => $fileCreateOrUpdateEvent->getParentName(),
'%parentId%' => $fileCreateOrUpdateEvent->getParentId(),
'%obj%' => $objectType
)
)
);
@@ -222,6 +159,32 @@ class FileController extends BaseAdminController
return new Response('', 404);
}
/**
* Manage how a image collection has to be saved
*
* @param int $parentId Parent id owning images being saved
* @param string $parentType Parent Type owning images being saved
*
* @return Response
*/
public function saveImageAjaxAction($parentId, $parentType)
{
return $this->saveFileAjaxAction($parentId, $parentType, 'image', ['image/jpeg' , 'image/png' ,'image/gif']);
}
/**
* 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)
{
return $this->saveFileAjaxAction($parentId, $parentType, 'document');
}
/**
* Manage how a image list will be displayed in AJAX
*
@@ -359,6 +322,106 @@ class FileController extends BaseAdminController
));
}
/**
* Manage how a file is updated
*
* @param int $fileId File identifier
* @param string $parentType Parent Type owning file being saved
* @param string $objectType the type of the file, image or document
* @param string $eventName the event type.
*
* @return FileModelInterface
*/
public function updateFileAction($fileId, $parentType, $objectType, $eventName)
{
$message = false;
$fileManager = $this->getFileManager();
$fileModelInstance = $fileManager->getModelInstance($objectType, $parentType);
$fileUpdateForm = $fileModelInstance->getUpdateFormInstance($this->getRequest());
/** @var FileModelInterface $file */
$file = $fileModelInstance->getQueryInstance()->findPk($fileId);
try {
$oldFile = clone $file;
if (null === $file) {
throw new \InvalidArgumentException(sprintf('%d %s id does not exist', $fileId, $objectType));
}
$data = $this->validateForm($fileUpdateForm)->getData();
$event = new FileCreateOrUpdateEvent(null);
$file->setLocale($data['locale']);
if (isset($data['title'])) {
$file->setTitle($data['title']);
}
if (isset($data['chapo'])) {
$file->setChapo($data['chapo']);
}
if (isset($data['description'])) {
$file->setDescription($data['description']);
}
if (isset($data['file'])) {
$file->setFile($data['file']);
}
if (isset($data['postscriptum'])) {
$file->setPostscriptum($data['postscriptum']);
}
$event->setModel($file);
$event->setOldModel($oldFile);
$files = $this->getRequest()->files;
$fileForm = $files->get($fileUpdateForm->getName());
if (isset($fileForm['file'])) {
$event->setUploadedFile($fileForm['file']);
}
$this->dispatch($eventName, $event);
$fileUpdated = $event->getModel();
$this->adminLogAppend(AdminResources::retrieve($parentType), AccessManager::UPDATE,
sprintf('%s with Ref %s (ID %d) modified', ucfirst($objectType), $fileUpdated->getTitle(), $fileUpdated->getId())
);
if ($this->getRequest()->get('save_mode') == 'close') {
$this->redirect(URL::getInstance()->absoluteUrl($fileModelInstance->getRedirectionUrl($fileId)));
} else {
$this->redirectSuccess($fileUpdateForm);
}
} 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 %s editing : %s.', $objectType, $message));
$fileUpdateForm->setErrorMessage($message);
$this->getParserContext()
->addForm($fileUpdateForm)
->setGeneralError($message);
}
return $fileModelInstance;
}
/**
* Manage how an image is updated
*
@@ -373,72 +436,13 @@ class FileController extends BaseAdminController
return $response;
}
$message = false;
$fileManager = $this->getFileManager();
$modelInstance = $fileManager->getModelInstance('image', $parentType);
$imageModification = $modelInstance->getUpdateFormInstance($this->getRequest());
/** @var FileModelInterface $image */
$image = $modelInstance->getQueryInstance()->findPk($imageId);
try {
$oldImage = clone $image;
if (null === $image) {
throw new \InvalidArgumentException(sprintf('%d image id does not exist', $imageId));
}
$form = $this->validateForm($imageModification);
$event = $this->createImageEventInstance($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(AdminResources::retrieve($parentType), AccessManager::UPDATE, sprintf('Image with Ref %s (ID %d) modified', $imageUpdated->getTitle(), $imageUpdated->getId()));
if ($this->getRequest()->get('save_mode') == 'close') {
$this->redirect(URL::getInstance()->absoluteUrl($modelInstance->getRedirectionUrl($imageId)));
} 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);
}
$redirectUrl = $modelInstance->getRedirectionUrl($imageId);
$imageInstance = $this->updateFileAction($imageId, $parentType, 'image', TheliaEvents::IMAGE_UPDATE);
return $this->render('image-edit', array(
'imageId' => $imageId,
'imageType' => $parentType,
'redirectUrl' => $redirectUrl,
'formId' => $modelInstance->getUpdateFormId()
'redirectUrl' => $imageInstance,
'formId' => $imageInstance->getUpdateFormId()
));
}
@@ -452,79 +456,97 @@ class FileController extends BaseAdminController
*/
public function updateDocumentAction($documentId, $parentType)
{
if (null !== $response = $this->checkAuth(AdminResources::retrieve($parentType), array(), AccessManager::UPDATE)) {
return $response;
}
$message = false;
$fileManager = $this->getFileManager();
$modelInstance = $fileManager->getModelInstance('document', $parentType);
$documentModification = $modelInstance->getUpdateFormInstance($this->getRequest());
/** @var FileModelInterface $document */
$document = $modelInstance->getQueryInstance()->findPk($documentId);
try {
$oldDocument = clone $document;
if (null === $document) {
throw new \InvalidArgumentException(sprintf('%d document id does not exist', $documentId));
}
$form = $this->validateForm($documentModification);
$event = $this->createDocumentEventInstance($parentType, $document, $form->getData());
$event->setOldModelDocument($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->getModelDocument();
$this->adminLogAppend(AdminResources::retrieve($parentType), AccessManager::UPDATE, sprintf('Document with Ref %s (ID %d) modified', $documentUpdated->getTitle(), $documentUpdated->getId()));
if ($this->getRequest()->get('save_mode') == 'close') {
$this->redirect(URL::getInstance()->absoluteUrl($modelInstance->getRedirectionUrl($documentId)));
} 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);
}
$redirectUrl = $modelInstance->getRedirectionUrl($documentId);
$documentInstance = $this->updateFileAction($documentId, $parentType, 'document', TheliaEvents::DOCUMENT_UPDATE);
return $this->render('document-edit', array(
'documentId' => $documentId,
'documentType' => $parentType,
'redirectUrl' => $redirectUrl,
'formId' => $modelInstance->getUpdateFormId()
'redirectUrl' => $documentInstance->getRedirectionUrl($documentId),
'formId' => $documentInstance->getUpdateFormId()
));
}
/**
* Manage how a image has to be deleted (AJAX)
*
* @param int $fileId Parent id owning image being deleted
* @param string $parentType Parent Type owning image being deleted
* @param string $objectType the type of the file, image or document
* @param string $eventName the event type.
*
* @return Response
*/
public function deleteFileAction($fileId, $parentType, $objectType, $eventName)
{
$message = null;
$this->checkAuth(AdminResources::retrieve($parentType), array(), AccessManager::UPDATE);
$this->checkXmlHttpRequest();
$fileManager = $this->getFileManager();
$modelInstance = $fileManager->getModelInstance($objectType, $parentType);
$model = $modelInstance->getQueryInstance()->findPk($fileId);
if ($model == null) {
return $this->pageNotFound();
}
// Feed event
$fileDeleteEvent = new FileDeleteEvent($model);
// Dispatch Event to the Action
try {
$this->dispatch($eventName, $fileDeleteEvent);
$this->adminLogAppend(
AdminResources::retrieve($parentType),
AccessManager::UPDATE,
$this->getTranslator()->trans(
'Deleting %obj% for %id% with parent id %parentId%',
array(
'%obj%' => $objectType,
'%id%' => $fileDeleteEvent->getFileToDelete()->getId(),
'%parentId%' => $fileDeleteEvent->getFileToDelete()->getParentId(),
)
)
);
} catch (\Exception $e) {
$message = $this->getTranslator()->trans(
'Fail to delete %obj% for %id% with parent id %parentId% (Exception : %e%)',
array(
'%obj%' => $objectType,
'%id%' => $fileDeleteEvent->getFileToDelete()->getId(),
'%parentId%' => $fileDeleteEvent->getFileToDelete()->getParentId(),
'%e%' => $e->getMessage()
)
);
$this->adminLogAppend(
AdminResources::retrieve($parentType),
AccessManager::UPDATE,
$message
);
}
if (null === $message) {
$message = $this->getTranslator()->trans(
'%obj%s deleted successfully',
['%obj%' => ucfirst($objectType)],
'image'
);
}
return new Response($message);
}
/**
* Manage how a image has to be deleted (AJAX)
*
@@ -535,189 +557,7 @@ class FileController extends BaseAdminController
*/
public function deleteImageAction($imageId, $parentType)
{
$message = null;
$this->checkAuth(AdminResources::retrieve($parentType), array(), AccessManager::UPDATE);
$this->checkXmlHttpRequest();
$fileManager = $this->getFileManager();
$modelInstance = $fileManager->getModelInstance('image', $parentType);
$model = $modelInstance->getQueryInstance()->findPk($imageId);
if ($model == null) {
return $this->pageNotFound();
}
// Feed event
$imageDeleteEvent = new ImageDeleteEvent(
$model,
$parentType
);
// Dispatch Event to the Action
try {
$this->dispatch(
TheliaEvents::IMAGE_DELETE,
$imageDeleteEvent
);
$this->adminLogAppend(
AdminResources::retrieve($parentType),
AccessManager::UPDATE,
$this->container->get('thelia.translator')->trans(
'Deleting image for %id% with parent id %parentId%',
array(
'%id%' => $imageDeleteEvent->getImageToDelete()->getId(),
'%parentId%' => $imageDeleteEvent->getImageToDelete()->getParentId(),
),
'image'
)
);
} catch (\Exception $e) {
$this->adminLogAppend(
AdminResources::retrieve($parentType),
AccessManager::UPDATE,
$this->container->get('thelia.translator')->trans(
'Fail to delete image for %id% with parent id %parentId% (Exception : %e%)',
array(
'%id%' => $imageDeleteEvent->getImageToDelete()->getId(),
'%parentId%' => $imageDeleteEvent->getImageToDelete()->getParentId(),
'%e%' => $e->getMessage()
),
'image'
)
);
$message = $this->getTranslator()
->trans(
'Fail to delete image for %id% with parent id %parentId% (Exception : %e%)',
array(
'%id%' => $imageDeleteEvent->getImageToDelete()->getId(),
'%parentId%' => $imageDeleteEvent->getImageToDelete()->getParentId(),
'%e%' => $e->getMessage()
),
'image'
);
}
if (null === $message) {
$message = $this->getTranslator()
->trans(
'Images deleted successfully',
array(),
'image'
);
}
return new Response($message);
}
public function updateImagePositionAction($parentType, /** @noinspection PhpUnusedParameterInspection */ $parentId)
{
$message = null;
$imageId = $this->getRequest()->request->get('image_id');
$position = $this->getRequest()->request->get('position');
$this->checkAuth(AdminResources::retrieve($parentType), array(), AccessManager::UPDATE);
$this->checkXmlHttpRequest();
$fileManager = $this->getFileManager();
$modelInstance = $fileManager->getModelInstance('image', $parentType);
$model = $modelInstance->getQueryInstance()->findPk($imageId);
if ($model === null || $position === null) {
return $this->pageNotFound();
}
// Feed event
$imageUpdateImagePositionEvent = new UpdateFilePositionEvent(
$modelInstance->getQueryInstance($parentType),
$imageId,
UpdateFilePositionEvent::POSITION_ABSOLUTE,
$position
);
// Dispatch Event to the Action
try {
$this->dispatch(
TheliaEvents::IMAGE_UPDATE_POSITION,
$imageUpdateImagePositionEvent
);
} catch (\Exception $e) {
$message = $this->getTranslator()
->trans(
'Fail to update image position',
array(),
'image'
) . $e->getMessage();
}
if (null === $message) {
$message = $this->getTranslator()
->trans(
'Image position updated',
array(),
'image'
);
}
return new Response($message);
}
public function updateDocumentPositionAction($parentType, /** @noinspection PhpUnusedParameterInspection */ $parentId)
{
$message = null;
$documentId = $this->getRequest()->request->get('document_id');
$position = $this->getRequest()->request->get('position');
$this->checkAuth(AdminResources::retrieve($parentType), array(), AccessManager::UPDATE);
$this->checkXmlHttpRequest();
$fileManager = $this->getFileManager();
$modelInstance = $fileManager->getModelInstance('document', $parentType);
$model = $modelInstance->getQueryInstance()->findPk($documentId);
if ($model === null || $position === null) {
return $this->pageNotFound();
}
// Feed event
$documentUpdateDocumentPositionEvent = new UpdateFilePositionEvent(
$modelInstance->getQueryInstance(),
$documentId,
UpdateFilePositionEvent::POSITION_ABSOLUTE,
$position
);
// Dispatch Event to the Action
try {
$this->dispatch(
TheliaEvents::DOCUMENT_UPDATE_POSITION,
$documentUpdateDocumentPositionEvent
);
} catch (\Exception $e) {
$message = $this->getTranslator()
->trans(
'Fail to update document position',
array(),
'document'
) . $e->getMessage();
}
if (null === $message) {
$message = $this->getTranslator()
->trans(
'Document position updated',
array(),
'document'
);
}
return new Response($message);
return $this->deleteFileAction($imageId, $parentType, 'image', TheliaEvents::IMAGE_DELETE);
}
/**
@@ -730,160 +570,66 @@ class FileController extends BaseAdminController
*/
public function deleteDocumentAction($documentId, $parentType)
{
return $this->deleteFileAction($documentId, $parentType, 'document', TheliaEvents::DOCUMENT_DELETE);
}
public function updateFilePositionAction($parentType, $parentId, $objectType, $eventName)
{
$message = null;
$position = $this->getRequest()->request->get('position');
$this->checkAuth(AdminResources::retrieve($parentType), array(), AccessManager::UPDATE);
$this->checkXmlHttpRequest();
$fileManager = $this->getFileManager();
$modelInstance = $fileManager->getModelInstance('document', $parentType);
$model = $modelInstance->getQueryInstance()->findPk($documentId);
$modelInstance = $fileManager->getModelInstance($objectType, $parentType);
$model = $modelInstance->getQueryInstance()->findPk($parentId);
if ($model == null) {
if ($model === null || $position === null) {
return $this->pageNotFound();
}
// Feed event
$documentDeleteEvent = new DocumentDeleteEvent(
$model,
$parentType
$event = new UpdateFilePositionEvent(
$modelInstance->getQueryInstance($parentType),
$parentId,
UpdateFilePositionEvent::POSITION_ABSOLUTE,
$position
);
// Dispatch Event to the Action
try {
$this->dispatch(
TheliaEvents::DOCUMENT_DELETE,
$documentDeleteEvent
);
$this->adminLogAppend(
AdminResources::retrieve($parentType),
AccessManager::UPDATE,
$this->container->get('thelia.translator')->trans(
'Deleting document for %id% with parent id %parentId%',
array(
'%id%' => $documentDeleteEvent->getDocumentToDelete()->getId(),
'%parentId%' => $documentDeleteEvent->getDocumentToDelete()->getParentId(),
),
'document'
)
);
$this->dispatch($eventName,$event);
} catch (\Exception $e) {
$this->adminLogAppend(
AdminResources::retrieve($parentType),
AccessManager::UPDATE,
$this->container->get('thelia.translator')->trans(
'Fail to delete document for %id% with parent id %parentId% (Exception : %e%)',
array(
'%id%' => $documentDeleteEvent->getDocumentToDelete()->getId(),
'%parentId%' => $documentDeleteEvent->getDocumentToDelete()->getParentId(),
'%e%' => $e->getMessage()
),
'document'
)
);
$message = $this->getTranslator()->trans(
'Fail to update %type% position: %err%',
[ '%type%' => $objectType, '%err%' => $e->getMessage() ]
);
}
$message = $this->getTranslator()
->trans(
'Document deleted successfully',
array(),
'document'
if (null === $message) {
$message = $this->getTranslator()->trans(
'%type% position updated',
[ '%type%' => ucfirst($objectType) ]
);
}
return new Response($message);
}
/**
* Log error message
*
* @param string $parentType Parent type
* @param string $action Creation|Update|Delete
* @param string $message Message to log
* @param \Exception $e Exception to log
*
* @return $this
*/
protected function logError($parentType, $action, $message, $e)
public function updateImagePositionAction($parentType, /** @noinspection PhpUnusedParameterInspection */ $parentId)
{
Tlog::getInstance()->error(
sprintf(
'Error during ' . $parentType . ' ' . $action . ' process : %s. Exception was %s',
$message,
$e->getMessage()
)
);
$imageId = $this->getRequest()->request->get('image_id');
return $this;
return $this->updateFilePositionAction($parentType, $imageId, 'image', TheliaEvents::IMAGE_UPDATE_POSITION);
}
/**
* Create Image Event instance
*
* @param string $parentType Parent Type owning images being saved
* @param FileModelInterface $model the model
* @param array $data Post data
*
* @return ImageCreateOrUpdateEvent
*/
protected function createImageEventInstance($parentType, $model, $data)
public function updateDocumentPositionAction($parentType, /** @noinspection PhpUnusedParameterInspection */ $parentId)
{
$imageCreateEvent = new ImageCreateOrUpdateEvent(null);
$documentId = $this->getRequest()->request->get('document_id');
$model->setLocale($data['locale']);
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']);
}
$imageCreateEvent->setModelImage($model);
return $imageCreateEvent;
return $this->updateFilePositionAction($parentType, $documentId, 'document', TheliaEvents::DOCUMENT_UPDATE_POSITION);
}
/**
* Create Document Event instance
*
* @param string $parentType Parent Type owning documents being saved
* @param FileModelInterface $model the model
* @param array $data Post data
*
* @return DocumentCreateOrUpdateEvent
*/
protected function createDocumentEventInstance($parentType, $model, $data)
{
$documentCreateEvent = new DocumentCreateOrUpdateEvent(null);
$model->setLocale($data['locale']);
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;
}
}
}