public function uploadImage()
 {
     if (null !== ($response = $this->checkAuth(AdminResources::MODULE, ['carousel'], AccessManager::CREATE))) {
         return $response;
     }
     $request = $this->getRequest();
     $form = $this->createForm('carousel.image');
     $error_message = null;
     try {
         $this->validateForm($form);
         /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $fileBeingUploaded */
         $fileBeingUploaded = $request->files->get(sprintf('%s[file]', $form->getName()), null, true);
         $fileModel = new Carousel();
         $fileCreateOrUpdateEvent = new FileCreateOrUpdateEvent(1);
         $fileCreateOrUpdateEvent->setModel($fileModel);
         $fileCreateOrUpdateEvent->setUploadedFile($fileBeingUploaded);
         $this->dispatch(TheliaEvents::IMAGE_SAVE, $fileCreateOrUpdateEvent);
         // Compensate issue #1005
         $langs = LangQuery::create()->find();
         /** @var Lang $lang */
         foreach ($langs as $lang) {
             $fileCreateOrUpdateEvent->getModel()->setLocale($lang->getLocale())->setTitle('')->save();
         }
         $response = $this->redirectToConfigurationPage();
     } catch (FormValidationException $e) {
         $error_message = $this->createStandardFormValidationErrorMessage($e);
     }
     if (null !== $error_message) {
         $this->setupFormErrorContext('carousel upload', $error_message, $form);
         $response = $this->render("module-configure", ['module_code' => 'Carousel']);
     }
     return $response;
 }
 /**
  * Updating the image. Inspired by FileController::updateFileAction().
  *
  * @param DiaporamaImage $file   Diaporama Image ID.
  * @param string $eventName  the event type.
  *
  * @return DiaporamaImage
  */
 protected function updateImageFile(DiaporamaImage $file)
 {
     $fileUpdateForm = $this->createForm($file->getUpdateFormId());
     try {
         if (null === $file) {
             throw new \InvalidArgumentException(sprintf('%d image id does not exist', $file->getId()));
         }
         $event = new FileCreateOrUpdateEvent(null);
         $event->setModel($file);
         $event->setOldModel($file);
         $fileForm = $this->getRequest()->files->get($fileUpdateForm->getName());
         if (isset($fileForm['file'])) {
             $event->setUploadedFile($fileForm['file']);
         }
         $this->dispatch(TheliaEvents::IMAGE_SAVE, $event);
         $fileUpdated = $event->getModel();
         $this->adminLogAppend(AdminResources::MODULE, AccessManager::UPDATE, sprintf('Image with Ref %s (ID %d) modified', $fileUpdated->getTitle(), $fileUpdated->getId()));
         if ($this->getRequest()->get('save_mode') == 'close') {
             return $this->generateRedirect(URL::getInstance()->absoluteUrl($file->getRedirectionUrl(), ['current_tab' => 'images']));
         } else {
             return $this->generateSuccessRedirect($fileUpdateForm);
         }
     } catch (PropelException $e) {
         $message = $e->getMessage();
     } catch (\Exception $e) {
         $message = sprintf('Sorry, an error occurred: %s', $e->getMessage() . ' ' . $e->getFile());
     }
     if (isset($message)) {
         Tlog::getInstance()->error(sprintf('Error during image editing : %s.', $message));
         $fileUpdateForm->setErrorMessage($message);
         $this->getParserContext()->addForm($fileUpdateForm)->setGeneralError($message);
     }
     return $file;
 }
 /**
  * Set Document to save
  *
  * @param $document FileModelInterface
  *
  * @return $this
  * @deprecated deprecated since version 2.0.3. Use FileCreateOrUpdateEvent instead
  */
 public function setModelDocument($document)
 {
     parent::setModel($document);
 }
Пример #4
0
 /**
  * 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
  */
 protected 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);
         if (array_key_exists('visible', $data)) {
             $file->setVisible($data['visible'] ? 1 : 0);
         }
         $file->setLocale($data['locale']);
         if (array_key_exists('title', $data)) {
             $file->setTitle($data['title']);
         }
         if (array_key_exists('chapo', $data)) {
             $file->setChapo($data['chapo']);
         }
         if (array_key_exists('description', $data)) {
             $file->setDescription($data['description']);
         }
         if (array_key_exists('postscriptum', $data)) {
             $file->setPostscriptum($data['postscriptum']);
         }
         if (isset($data['file'])) {
             $file->setFile($data['file']);
         }
         $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') {
             if ($objectType == 'document') {
                 $tab = 'documents';
             } else {
                 $tab = 'images';
             }
             return $this->generateRedirect(URL::getInstance()->absoluteUrl($file->getRedirectionUrl(), ['current_tab' => $tab]));
         } else {
             return $this->generateSuccessRedirect($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 $file;
 }
Пример #5
0
 public function cloneFileI18n($originalProductFileI18ns, $clonedProductFile, $type, ProductCloneEvent $event, EventDispatcherInterface $dispatcher)
 {
     // Set clone files I18n
     /** @var ProductDocumentI18n $originalProductFileI18n */
     foreach ($originalProductFileI18ns as $originalProductFileI18n) {
         // Update file with current I18n info. Update or create I18n according to existing or absent Locale in DB
         $clonedProductFile->setLocale($originalProductFileI18n->getLocale())->setTitle($originalProductFileI18n->getTitle())->setDescription($originalProductFileI18n->getDescription())->setChapo($originalProductFileI18n->getChapo())->setPostscriptum($originalProductFileI18n->getPostscriptum());
         // Create and dispatch event
         $clonedProductUpdateFileEvent = new FileCreateOrUpdateEvent($event->getClonedProduct()->getId());
         $clonedProductUpdateFileEvent->setModel($clonedProductFile);
         switch ($type) {
             case 'images':
                 $dispatcher->dispatch(TheliaEvents::IMAGE_UPDATE, $clonedProductUpdateFileEvent);
                 break;
             case 'documents':
                 $dispatcher->dispatch(TheliaEvents::DOCUMENT_UPDATE, $clonedProductUpdateFileEvent);
                 break;
         }
     }
 }
Пример #6
0
 /**
  * Take care of updating file in the database and file storage
  *
  * @param FileCreateOrUpdateEvent $event Image event
  *
  * @throws \Thelia\Exception\FileException
  */
 public function updateFile(FileCreateOrUpdateEvent $event)
 {
     // Copy and save file
     if ($event->getUploadedFile()) {
         // Remove old picture file from file storage
         $url = $event->getModel()->getUploadDir() . '/' . $event->getOldModel()->getFile();
         unlink(str_replace('..', '', $url));
         $newUploadedFile = $this->fileManager->copyUploadedFile($event->getModel(), $event->getUploadedFile());
         $event->setUploadedFile($newUploadedFile);
     }
     // Update image modifications
     $event->getModel()->save();
     $event->setModel($event->getModel());
 }
Пример #7
0
 /**
  * Set Image to save
  *
  * @param $image FileModelInterface
  *
  * @return $this
  * @deprecated deprecated since version 2.0.3. Use FileCreateOrUpdateEvent instead
  */
 public function setModelImage($image)
 {
     parent::setModel($image);
 }