Example #1
0
 /**
  * @param $archiveCreator
  * @return $this
  */
 public function add(AbstractFormatter $formatter)
 {
     if (null !== $formatter) {
         $this->formatters[$formatter->getName()] = $formatter;
     }
     return $this;
 }
Example #2
0
 /**
  * @param  AbstractFormatter      $formatter
  * @param  ExportHandler          $handler
  * @param  AbstractArchiveBuilder $archiveBuilder
  * @param  bool                   $includeImages
  * @param  bool                   $includeDocuments
  * @return Response
  *
  * Processes an export by returning a response with the export's content.
  */
 protected function processExport(AbstractFormatter $formatter, ExportHandler $handler, AbstractArchiveBuilder $archiveBuilder = null, Lang $lang, $includeImages = false, $includeDocuments = false)
 {
     /**
      * Build an event containing the formatter and the handler.
      * Used for specific configuration (e.g: XML node names)
      */
     $event = new ImportExportEvent($formatter, $handler);
     $filename = $formatter::FILENAME . "." . $formatter->getExtension();
     if ($archiveBuilder === null) {
         $data = $handler->buildData($lang);
         $event->setData($data);
         $this->dispatch(TheliaEvents::EXPORT_BEFORE_ENCODE, $event);
         $formattedContent = $formatter->setOrder($handler->getOrder())->encode($data);
         $this->dispatch(TheliaEvents::EXPORT_AFTER_ENCODE, $event->setContent($formattedContent));
         return new Response($event->getContent(), 200, ["Content-Type" => $formatter->getMimeType(), "Content-Disposition" => "attachment; filename=\"" . $filename . "\""]);
     } else {
         $event->setArchiveBuilder($archiveBuilder);
         if ($includeImages && $handler instanceof ImagesExportInterface) {
             $this->processExportImages($handler, $archiveBuilder);
             $handler->setImageExport(true);
         }
         if ($includeDocuments && $handler instanceof DocumentsExportInterface) {
             $this->processExportDocuments($handler, $archiveBuilder);
             $handler->setDocumentExport(true);
         }
         $data = $handler->buildData($lang)->setLang($lang);
         $this->dispatch(TheliaEvents::EXPORT_BEFORE_ENCODE, $event);
         $formattedContent = $formatter->setOrder($handler->getOrder())->encode($data);
         $this->dispatch(TheliaEvents::EXPORT_AFTER_ENCODE, $event->setContent($formattedContent));
         $archiveBuilder->addFileFromString($event->getContent(), $filename);
         return $archiveBuilder->buildArchiveResponse($formatter::FILENAME);
     }
 }
 public function processImport($content, ImportHandler $handler, AbstractFormatter $formatter = null, AbstractArchiveBuilder $archiveBuilder = null, Lang $lang = null)
 {
     $event = new ImportExportEvent($formatter, $handler, null, $archiveBuilder);
     $event->setContent($content);
     $this->dispatch(TheliaEvents::IMPORT_AFTER_DECODE, $event);
     $data = $formatter->decode($event->getContent())->setLang($lang);
     $event->setContent(null)->setData($data);
     $this->dispatch(TheliaEvents::IMPORT_AFTER_DECODE, $event);
     $errors = $handler->retrieveFromFormatterData($data);
     if (!empty($errors)) {
         throw new \Exception($this->getTranslator()->trans("Errors occurred while importing the file: %errors", ["%errors" => implode(", ", $errors)]));
     }
     return $this->getTranslator()->trans("Import successfully done, %numb row(s) have been changed", ["%numb" => $handler->getImportedRows()]);
 }