templateRepository = $templateRepository; $this->mediaRepository = $mediaRepository; $this->optionRepository = $optionRepository; $this->pageOptionsRepository = $pageOptionsRepository; $this->templateDeleteQuery = $templateDeleteQuery; $this->commonFileRepository = $commonFileRepository; $this->dedicatedFileRepository = $dedicatedFileRepository; $this->fileTypeRepository = $fileTypeRepository; $this->twigRepository = $twigRepository; $this->exportsRepository = $exportsRepository; $this->variableProjectRepository = $variableProjectRepository; $this->pageRepository = $pageRepository; $this->pageAutoGeneQuery = $pageAutoGeneQuery; $this->entityManager = $entityManager; $this->fileTypeName = "Indesign"; $this->aiExtension = "ai"; $this->indtExtension = "indt"; $this->jsxExtension = "jsx"; $this->params = $params; } /** * @return array [description] */ public function list(): array { $templates = $this->templateRepository->list(); $view = []; if (!empty($templates)) { foreach ($templates as $template) { $view[] = new TemplateListView( $template->getId(), $template->getName(), null !== $template->getUpdatedAt() ? $template->getUpdatedAt()->format('Y-m-d') : $template->getCreatedAt()->format('Y-m-d'), $template->getNbPage(), $template->getNbElement(), $template->getTwig()->getExportType() ); } } return $view; } /** * @param CommonIdView $view * * @return Template|null */ public function read(CommonIdView $view): ?Template { $template = $this->templateRepository->findById($view->id); if (null !== $template) { return $template; } else { throw new NotFoundMaestroException("Template for id $view->id.", "template", "findById"); } } /** * @param Template $template [description] * * @return TemplateView [description] */ public function getTemplateData(Template $template): TemplateView { $templateView = $this->getTemplateReadView($template); $twig = $template->getTwig(); $twigView = $this->getTwigView($twig); $templateDataView = new TemplateView( $templateView, $twigView ); return $templateDataView; } /** * @param Template $template [description] * * @return TemplateReadView [description] */ private function getTemplateReadView(Template $template): TemplateReadView { $options = $template->getOptions(); $optionsView = $this->getTemplateOptionReadViews($options->getValues()); $medias = $template->getMedias(); $mediasView = $this->getTemplateMediaReadViews($medias->getValues()); $dedicatedFiles = $template->getDedicatedFiles(); $dedicatedFilesView = $this->getDedicatedFileReadViews($dedicatedFiles->getValues()); $commonFiles = $template->getCommonFiles(); $commonFilesView = $this->getCommonFileReadViews($commonFiles->getValues()); $templateReadView = new TemplateReadView( $template->getId(), $template->getName(), $template->getRegeneratedAt() ? $template->getRegeneratedAt()->format('Y-m-d') : '', $template->getNbPage(), $template->getNbElement(), $template->getSuffix(), $template->getFontPath(), $optionsView, $mediasView, $dedicatedFilesView, $commonFilesView ); return $templateReadView; } /** * @param array $options [description] * * @return array [description] */ private function getTemplateOptionReadViews(array $options): array { $optionsView = []; foreach ($options as $option) { if (!$option->getDeletedAt()) { $listValues = $option->getListValue() ? explode(";", $option->getListValue()) : []; $templateOptionReadView = new OptionReadView( $option->getId(), $option->getName(), $option->getType(), $option->getTwigVar(), $listValues, !empty($option->getVariableProject()) ? ["id" => $option->getVariableProject()->getId(), "name" => $option->getVariableProject()->getName()] : [] ); $optionsView[] = $templateOptionReadView; } } return $optionsView; } /** * @param array $medias [description] * * @return array [description] */ private function getTemplateMediaReadViews(array $medias): array { $mediasView = []; foreach ($medias as $media) { if (!$media->getDeletedAt()) { $templateMediaReadView = new MediaReadView( $media->getId(), $media->getName(), $media->getMarker(), $media->getWidth(), $media->getHeight(), $media->getOptional(), $media->getAdjustmentOption(), $media->getPosition(), $media->getForElement(), !empty($media->getVariableProject()) ? ["id" => $media->getVariableProject()->getId(), "name" => $media->getVariableProject()->getName()] : [] ); $mediasView[] = $templateMediaReadView; } } return $mediasView; } /** * @param array $dedicatedFiles [description] * * @return array [description] */ private function getDedicatedFileReadViews(array $dedicatedFiles): array { $dedicatedFilesView = []; foreach ($dedicatedFiles as $dedicatedFile) { if (!$dedicatedFile->getArchivedAt() && !$dedicatedFile->getDeletedAt()) { $extension = $dedicatedFile->getFileType()->getExtension(); $dedicatedFileReadView = new DedicatedFileReadView( $dedicatedFile->getOriginalName(), $extension, $dedicatedFile->getFileType()->getName() ); if (in_array($extension, ['jpg', 'jpeg', 'png', 'gif'])) { $dedicatedFilesView['previews'][] = $dedicatedFileReadView; } else { $dedicatedFilesView[$extension][] = $dedicatedFileReadView; } } } return $dedicatedFilesView; } /** * @param int $idTemplate [Id template to read] * @param string $extensionToKeep [The extension of the dedicated file to keep] * * @return string [blob of the dedicated file] */ public function getLastDedicatedFileByExtension(int $idTemplate, string $extensionToKeep): string { $blobLastJsx = NULL; $objTemplate = $this->read(new CommonIdView($idTemplate)); $dedicatedFiles = $objTemplate->getDedicatedFiles(); foreach ($dedicatedFiles as $dedicatedFile) { if (!$dedicatedFile->getArchivedAt() && !$dedicatedFile->getDeletedAt()) { $extension = $dedicatedFile->getFileType()->getExtension(); if ($extension == $extensionToKeep) { $blobLastJsx = $dedicatedFile->getFile(); } } } return base64_encode(stream_get_contents($blobLastJsx)); } /** * @param int $idTemplate [Id template to read] * * @return string [blob of the dedicated files] */ public function getPreviewDedicatedFiles(int $idTemplate): string { $blobLastJsx = NULL; $objTemplate = $this->read(new CommonIdView($idTemplate)); $dedicatedFiles = $objTemplate->getDedicatedFiles(); foreach ($dedicatedFiles as $dedicatedFile) { if (!$dedicatedFile->getArchivedAt() && !$dedicatedFile->getDeletedAt()) { $extension = $dedicatedFile->getFileType()->getExtension(); if ($extension == $extensionToKeep) { $blobLastJsx = $dedicatedFile->getFile(); } } } return base64_encode(stream_get_contents($blobLastJsx)); } /** * @param array $commonFiles [description] * * @return array [description] */ private function getCommonFileReadViews(array $commonFiles): array { $commonFilesView = []; foreach ($commonFiles as $commonFile) { if (!$commonFile->getDeletedAt()) { $extension = $commonFile->getFileType()->getExtension(); $commonFileReadView = new CommonFileReadView( $commonFile->getOriginalName(), $commonFile->getIdDam(), $extension, $commonFile->getFileType()->getName() ); $commonFilesView[$extension][] = $commonFileReadView; } } return $commonFilesView; } /** * @param Twig $twig [description] * * @return TwigView [description] */ private function getTwigView(Twig $twig): TwigView { $templates = []; if (!$twig->getTemplates()->isEmpty()) { foreach ($twig->getTemplates() as $template) { $templates[] = [ "id" => $template->getId(), "name" => $template->getName() ]; } } $twigView = new TwigView( $twig->getId(), $twig->getName(), $twig->getExportType(), $twig->getContent(), $templates ); return $twigView; } /** * @param Twig $twig * @param TemplateCreateView $view * * @return Template */ public function create(Twig $twig, TemplateCreateView $view): Template { $template = new Template(); $templateName = $view->name; $template->setName($templateName); $template->setRegeneratedAt(new DateTime($view->regeneratedAt . date('H:i:s'))); $template->setNbPage($view->page); $template->setNbElement($view->element); $template->setSuffix($view->suffix); $template->setFontPath($view->fontPath); if (!empty($view->options)) { $this->setOptions($view->options, $templateName, $template); } if (!empty($view->medias)) { $this->setMedias($view->medias, $templateName, $template); } if (!empty($view->pictos)) { $this->setCommonPictos($view->pictos, $template); } if ($view->jsxFile) { $this->setJsxFile($view->jsxFile, $template); } if ($view->indtFile) { $this->setIndtFile($view->indtFile, $template); } if (!empty($view->htmlFile)) { foreach ($view->htmlFile as $htmlFile) { $this->setFile($htmlFile, $template, "HTML"); } } if (!empty($view->previewfiles)) { foreach ($view->previewfiles as $previewFile) { $this->setFile($previewFile, $template, "Image"); } } $template->setTwig($twig); return $this->templateRepository->save($template); } /** * @param array $options [description] * @param string $templateName [description] * @param Template $template [description] * * @return void [description] */ private function setOptions(array $options, string $templateName, Template $template): void { $optionIds = []; $variableProjRepo = $this->entityManager->getRepository(VariableProject::class); foreach ($options as $key => $opt) { $defaultName = $templateName . '-Option' . (int) ($key++); if (isset($opt['id']) && $opt['id'] > 0) { $option = $this->optionRepository->findById($opt['id']); $optionIds[] = $opt['id']; } else { $option = new Option(); } $option->setName(isset($opt['name']) && $opt['name'] ? $opt['name'] : $defaultName); $option->setType(isset($opt['type']) && $opt['type'] ? $opt['type'] : 'text'); $option->setTwigVar(isset($opt['twigVar']) && $opt['twigVar'] ? $opt['twigVar'] : null); $option->setListValue(isset($opt['listValue']) && $opt['listValue'] ? $opt['listValue'] : null); if(isset($opt['variableProject']) && !empty($opt['variableProject'])){ $variableProject = $this->variableProjectRepository->findById($opt['variableProject'][0]["id"]); if(!empty($variableProject)){ $option->setVariableProject($variableProject); } } else { $varProj = $variableProjRepo->findOneBy([ "name" => $opt['name'], "fieldType" => $opt['type'], "categorie" => "option" ]); if(empty($varProj)){ $varProj = new VariableProject(); $varProj->setName($opt['name']); $varProj->setCategorie("option"); $varProj->setFieldType($opt['type']); $this->entityManager->persist($varProj); } $option->setVariableProject($varProj); } $option->setTemplate($template); $template->addOption($option); } if (!empty($optionIds) && $template->getId()) { $this->removeOldOptions($optionIds, $template->getId()); } } /** * @param array $medias [description] * @param string $templateName [description] * @param Template $template [description] * * @return void [description] */ private function setMedias(array $medias, string $templateName, Template $template): void { $mediaIds = []; $variableProjRepo = $this->entityManager->getRepository(VariableProject::class); foreach ($medias as $key => $med) { $iteration = (int) ($key++); $defaultName = $templateName . '-Media' . (int) ($key++); if (isset($med['id']) && $med['id'] > 0) { $media = $this->mediaRepository->findById($med['id']); $mediaIds[] = $med['id']; } else { $media = new Media(); } $media->setName(isset($med['name']) && $med['name'] ? $med['name'] : $defaultName); $media->setMarker(isset($med['marker']) && $med['marker'] ? $med['marker'] : ''); // TODO : See if it can be nullable or has a default value $media->setWidth(isset($med['width']) && $med['width'] ? $med['width'] : 0); $media->setHeight(isset($med['height']) && $med['height'] ? $med['height'] : 0); $media->setOptional(isset($med['optional']) ? $med['optional'] : false); $media->setAdjustmentOption(isset($med['adjustmentOption']) ? $med['adjustmentOption'] : 0); // TODO : See if it can be nullable or has a default value $media->setPosition(isset($med['position']) ? $med['position'] : $iteration); // TODO : See if it can be nullable or has a default value $media->setForElement(isset($med['forElement']) ? $med['forElement'] : false); if(isset($med['variableProject']) && !empty($med['variableProject'])){ $variableProject = $this->variableProjectRepository->findById($med['variableProject'][0]["id"]); if(!empty($variableProject)){ $media->setVariableProject($variableProject); } } else { $varProj = $variableProjRepo->findOneBy([ "name" => $med['name'], "categorie" => "media" ]); if(empty($varProj)){ $varProj = new VariableProject(); $varProj->setName($med['name']); $varProj->setCategorie("media"); $varProj->setFieldType("-1"); $this->entityManager->persist($varProj); } $media->setVariableProject($varProj); } $media->setTemplate($template); $template->addMedia($media); } if ($template->getId()) { $this->removeOldMedias($mediaIds, $template->getId()); } } /** * @param array $pictos * @param Template $template * * @return void */ private function setCommonPictos(array $pictos, Template $template): void { $fileType = $this->fileTypeRepository->findByNameAndExtension($this->fileTypeName, $this->aiExtension); if ($fileType) { $this->removeOldPictos($template, $pictos); $this->setPictos($pictos, $template, $fileType); } else { $this->getNoFileNameAndExtensionMessage($this->fileTypeName, $this->aiExtension); } } /** * @param UploadedFile $jsxFile * @param Template $template * * @return void */ private function setJsxFile(UploadedFile $jsxFile, Template $template): void { $jsxExtension = $jsxFile->getClientOriginalExtension(); $fileType = $this->fileTypeRepository->findByNameAndExtension($this->fileTypeName, $jsxExtension); if ($fileType) { $this->setDedicatedFile($jsxFile, $template, $fileType); } else { $this->getNoFileNameAndExtensionMessage($this->fileTypeName, $jsxExtension); } } /** * @param UploadedFile $indtFile * @param Template $template * * @return void */ private function setIndtFile(UploadedFile $indtFile, Template $template): void { $indtExtension = $indtFile->getClientOriginalExtension(); $fileType = $this->fileTypeRepository->findByNameAndExtension($this->fileTypeName, $indtExtension); if ($fileType) { $this->setDedicatedFile($indtFile, $template, $fileType); } else { $this->getNoFileNameAndExtensionMessage($this->fileTypeName, $indtExtension); } } /** * @param UploadedFile $indtFile * @param Template $template * * @return void */ private function setFile(UploadedFile $file, Template $template, $fileTypeName): void { $extension = $file->getClientOriginalExtension(); $fileType = $this->fileTypeRepository->findByNameAndExtension($fileTypeName, $extension); $withDelete = $fileTypeName !== 'Image'; if ($fileType) { $this->setDedicatedFile($file, $template, $fileType, $withDelete); } else { $this->getNoFileNameAndExtensionMessage($fileTypeName, $extension); } } /** * @param Template $template * * @return bool */ public function delete(Template $template): bool { // $pageOptions = $this->cdfCollect->deletePageOption($template->getId()); // An array of pageOptions that were associated to template $pageOptions = $this->deleteTemplateOnPageOption($template->getId()); if (!empty($pageOptions)) { $this->templateDeleteQuery->deletedByPages($pageOptions, $template->getId()); $exportIds = array_column($this->exportsRepository->listIds(), 'id'); $this->templateDeleteQuery->deleteExportsTemplate($exportIds, $template->getId()); // $this->projectCollect->deleteValue($pageOptions, $template->getId()); // Remove project export values linked to template $templateMediaIds = $template->getMedias()->map(function ($m) { return $m->getId(); }); $this->templateDeleteQuery->deleteByMediaIds($templateMediaIds->getValues()); // $this->projectCollect->deleteMedias($templateMediaIds->getValues()); // Remove project export medias linked to template } $this->templateRepository->delete($template); return true; } /** * @param CommonIdView $view [description] * * @return array [description] */ public function listByTwigsExport(CommonIdView $view): array { $templates = $this->templateRepository->listByTwigsExport($view->id); $view = []; if (!empty($templates)) { foreach ($templates as $template) { $view[] = new CommonListView( $template->getId(), $template->getName() ); } } return $view; } /** * @param TemplatesCdfView $templatesCdfView [description] * * @return array [description] */ public function pageNameCdf(TemplatesCdfView $templatesCdfView): array { $templates = $this->templateRepository->nameCdf($templatesCdfView->templates); $view = []; if ($templates) { foreach ($templates as $template) { $view[$template->getId()] = new CommonTextView( $template->getName() ); } } return $view; } /** * @param CommonTextView $view [description] * * @return array [description] */ public function listByTemplateIds(CommonTextView $view): array { return $this->templateRepository->findByIds($view->text); } /** * @param string $name [description] * * @return bool [description] */ public function isNameAlreadyExist(string $name): bool { $template = $this->templateRepository->findByName($name); if ($template) { return true; } return false; } /** * {@inheritDoc} */ public function getGenerationData(CommonIdView $view, string $exportType): array { if ('xml' !== strtolower($exportType)) { $data = $this->templateRepository->findGenerationData($view->id, $this->fileTypeName, $this->indtExtension); } else { $data = $this->templateRepository->findGenerationData($view->id); } if (!empty($data)) { return $data; } else { throw new UnprocessableEntityMaestroException("missing indt file", "findGenrationData", "getGenerationData"); } } /** * @param TemplateIdsNumberPageProductView $view * * @return array */ public function readByIdsAndNumberPageProduct(TemplateIdsNumberPageProductView $view) { $result = []; $templates = $this->templateRepository->findByIdsAndNumberPageProduct($view->ids, $view->nbPage, $view->nbProduct); foreach ($templates as $template) { $result[] = ['id' => $template->getId(), 'name' => $template->getName()]; } return $result; } /** * @param int $view * * @return array */ public function readByExport(int $id) { $result = []; $export = $this->exportsRepository->findById($id); $exportTemplates = $export->getTemplates(); $templatesId = []; foreach ($exportTemplates as $exportTemplate) { foreach($exportTemplate->getTemplates() as $template){ $templatesId[] = $template["id"]; } } if(!empty($templatesId)) { $templates = $this->templateRepository->findByIds(implode(",",$templatesId)); foreach ($templates as $template) { $result[] = ['id' => $template->getId(), 'name' => $template->getName(), 'nbPage' => $template->getNbPage(), 'nbProduct' => $template->getNbElement()]; } } return $result; } /** * @param CommonIdView $view * * @return array */ public function getPictoIds(CommonIdView $view): array { $result = $this->commonFileRepository->findCommonIdsByTemplateIdAndFileTypeProperties($view->id, $this->fileTypeName, $this->aiExtension); if (!empty($result)) { $result = array_column($result, 'commonFileId'); } return $result; } /** * @param CommonIdView $view * * @return array */ public function getDedicatedFiles(CommonIdView $view): array { $data = []; $data['indt'] = $this->dedicatedFileRepository->findByTemplateIdAndFileTypeProperties($view->id, $this->fileTypeName, $this->indtExtension); $data['jsx'] = $this->dedicatedFileRepository->findByTemplateIdAndFileTypeProperties($view->id, $this->fileTypeName, $this->jsxExtension); $data['defaultJsx'] = null; return $this->getDedicatedFilesContent($data); } /** * @param array $ids [description] * @param int $templateId [description] * * @return void [description] */ private function removeOldOptions(array $ids, int $templateId): void { $options = $this->optionRepository->listNotIn($ids, $templateId); foreach ($options as $option) { $this->optionRepository->remove($option); } } /** * @param array $ids [description] * @param int $templateId [description] * * @return void [description] */ private function removeOldMedias(array $ids, int $templateId): void { if (!empty($ids)) { $medias = $this->mediaRepository->listNotIn($ids, $templateId); } else { $medias = $this->mediaRepository->list($templateId); } if (!empty($medias)) { $mediaIds = array_map(function ($m) { return $m->getId(); }, $medias); $this->mediaRepository->removeMultiple($medias); // $this->projectCollect->deleteMedias($mediaIds); } } /** * @param array $viewPictos [description] * @param Template $template [description] * @param FileType $fileType [description] * * @return void [description] */ private function setPictos(array $viewPictos, Template $template, FileType $fileType): void { $existingCommonFilePictos = $this->getCommonFilePictos($viewPictos); $existingCommonFilePictoIds = []; $newCommonFilePictos = []; if (!empty($existingCommonFilePictos)) { foreach ($existingCommonFilePictos as $existingCommonFilePicto) { $existingCommonFilePictoIds[] = $existingCommonFilePicto->getIdDam(); $template->addCommonFile($existingCommonFilePicto); } $newPictos = $this->getNewPictos($viewPictos, $existingCommonFilePictoIds); if (!empty($newPictos)) { $newCommonFilePictos = $this->createCommonFilePictos($newPictos, $fileType); } } else { $newCommonFilePictos = $this->createCommonFilePictos($viewPictos, $fileType); } if (!empty($newCommonFilePictos)) { foreach ($newCommonFilePictos as $newCommonFilePicto) { $template->addCommonFile($newCommonFilePicto); } } } /** * @param array $pictos [description] * * @return array [description] */ private function getCommonFilePictos(array $pictos): array { $ids = array_column($pictos, 'id'); if (!empty($ids)) { $result = $this->commonFileRepository->findByDamIds($ids); } else { $result = []; } return $result; } /** * @param array $viewPictos [description] * @param array $existingIds [description] * * @return array [description] */ private function getNewPictos(array $viewPictos, array $existingIds): array { $newPictos = []; for ($i = 0; $i < count($viewPictos); $i++) { if (isset($viewPictos[$i]['id']) && !in_array($viewPictos[$i]['id'], $existingIds)) { $newPictos[] = $viewPictos[$i]; } } return $newPictos; } /** * @param array $pictos [description] * * @return array [description] */ private function createCommonFilePictos(array $pictos, FileType $fileType): array { $commonFilePictos = []; foreach ($pictos as $picto) { if (isset($picto['id'])) { $commonFile = new CommonFile(); $commonFile->setIdDam($picto['id']); if ($picto['name']) { $fileName = $picto['name']; $fileUniqName = $this->getUniqName($fileName); $commonFile->setFileName($fileUniqName); $commonFile->setOriginalName($fileName); } $commonFile->setFileType($fileType); $commonFilePictos[] = $commonFile; } } return $commonFilePictos; } /** * @param Template $template [description] * @param array $viewPictos [description] * * @return void [description] */ private function removeOldPictos(Template $template, array $viewPictos): void { $commonFiles = $template->getCommonFiles(); if (!empty($commonFiles)) { if (!empty($viewPictos)) { $ids = array_column($viewPictos, 'id'); if (!empty($ids)) { foreach ($commonFiles as $commonFile) { if (!in_array($commonFile->getId(), $ids)) { $template->removeCommonFile($commonFile); } } } } else { foreach ($commonFiles as $commonFile) { $template->removeCommonFile($commonFile); } } } } /** * @param UploadedFile $file [description] * @param Template $template [description] * @param FileType $fileType [description] * * @return void [description] */ private function setDedicatedFile(UploadedFile $file, Template $template, FileType $fileType, $withDelete = true): void { if ($withDelete && $template->getId()) { $this->archiveOldDedicatedFile($template->getId(), $fileType->getId()); } $dedicatedFile = new DedicatedFile(); $fileOriginalName = $file->getClientOriginalName(); $fileUniqName = $this->getUniqName($fileOriginalName); $dedicatedFile->setFileName($fileUniqName); $dedicatedFile->setOriginalName($fileOriginalName); $tmpFile = $file->getRealPath(); $fileContent = file_get_contents($tmpFile); $dedicatedFile->setFile($fileContent); // $dedicatedFile->setUploadedFile($file); // TODO : If not useful remove from entity $dedicatedFile->setFileType($fileType); $template->addDedicatedFile($dedicatedFile); } /** * @param int $templateId [description] * @param string $fileType [description] * * @return void [description] */ private function archiveOldDedicatedFile(int $templateId, string $fileType): void { $oldDedicatedFiles = $this->dedicatedFileRepository->findOldDedicatedFiles($templateId, $fileType); foreach ($oldDedicatedFiles as $oldDedicatedFile) { $oldDedicatedFile->setArchivedAt(new DateTime()); } } /** * @param string $name [description] * * @return string [description] */ private function getUniqName(string $name): string { $result = uniqid($name); return $result; } /** * @param string $name * @param string $extension * * @throws NotFoundMaestroException * * @return void */ private function getNoFileNameAndExtensionMessage($name, $extension): void { $message = "File type's name : '$name' and extension : '$extension'."; throw new NotFoundMaestroException($message, $extension, "getNoFileNameAndExtensionMessage"); } /** * @param array $dedicatedFiles [description] * * @return array [description] */ private function getDedicatedFilesContent(array $dedicatedFiles): array { $result = []; foreach ($dedicatedFiles as $key => $dedicatedFile) { $viewFile = []; if (!empty($dedicatedFile)) { $stream = base64_encode(stream_get_contents($dedicatedFile->getFile())); $viewFile = new FileView( $dedicatedFile->getOriginalName(), $stream, $key ); } else if($key == "defaultJsx") { $stream = base64_encode(file_get_contents($this->params->get("arborescence_base")."/".$this->params->get('global_jsx'))); $viewFile = new FileView( "global.jsx", $stream, $key ); } $result[$key] = $viewFile; } return $result; } /** * @param TemplateUpdateView $view * * @throws InternalServerErrorMaestroException * * @return Template [Template] */ public function update(TemplateUpdateView $view): Template { $template = $this->read(new CommonIdView($view->id)); $this->controlTwig($template, $view->twigId); $templateName = $view->name; if ($templateName !== $template->getName()) { $duplicateName = $this->templateRepository->findDuplicateByName($templateName, $template->getId()); if (!$duplicateName) { $template->setName($templateName); } else { throw new InternalServerErrorMaestroException("template\'s name", "template", "findDuplicateByName"); } } $template->setRegeneratedAt(new DateTime($view->regeneratedAt . date('H:i:s'))); $template->setNbPage($view->page); $template->setNbElement($view->element); $template->setSuffix($view->suffix); $template->setFontPath($view->fontPath); if (!empty($view->options)) { $this->setOptions($view->options, $templateName, $template); } $this->setMedias($view->medias, $templateName, $template); if (isset($view->pictos)) { // Can be empty $this->setCommonPictos($view->pictos, $template); } if ($view->jsxFile) { $this->setJsxFile($view->jsxFile, $template); } if ($view->indtFile) { $this->setIndtFile($view->indtFile, $template); } if (!empty($view->htmlFile)) { foreach ($view->htmlFile as $htmlFile) { $this->setFile($htmlFile, $template, "HTML"); } } if (!empty($view->previewfiles)) { $this->archiveOldDedicatedFile($template->getId(), 'Image'); foreach ($view->previewfiles as $previewFile) { $this->setFile($previewFile, $template, "Image"); } } $result = $this->templateRepository->save($template); $this->addTemplatePageToGenerate($template); return $result; } public function addTemplatePageToGenerate($template){ $pages = $this->pageRepository->findByTemplateAndNotArchived($template->getId()); $pageToGenerate = []; if(!empty($pages)){ foreach($pages as $page){ $pageToGenerate[] = ["id" => $page->getId() , "pageDuplicateId" => null]; if(!$page->getPageDuplicates()->isEmpty()){ foreach($page->getPageDuplicates() as $dupli){ $pageToGenerate[] = ["id" => $page->getId() , "pageDuplicateId" => $dupli->getId()]; } } } } if(!empty($pageToGenerate)){ $this->pageAutoGeneQuery->addPageToGenerate($pageToGenerate); } } /** * @param Template $template * @param int $twigId * * @throws NotFoundMaestroException * * @return Template */ private function controlTwig(Template $template, int $twigId) { if ($twigId !== $template->getTwig()->getId()) { $newTwig = $this->twigRepository->findById($twigId); if ($newTwig) { $result = $template->setTwig($newTwig); } else { throw new NotFoundMaestroException("Twig for id $twigId.", "twig", "findById"); } } else { $result = $template; } return $result; } public function geTHTMLFile(int $templateId) { $files = $this->dedicatedFileRepository->findFilesByTemplateIdAndFileTypeProperties($templateId, 'HTML', ['css', 'js', 'html']); // $js = $this->dedicatedFileRepository->findByTemplateIdAndFileTypeProperties($templateId, 'HTML', 'js'); // $html = $this->dedicatedFileRepository->findByTemplateIdAndFileTypeProperties($templateId, 'HTML', 'html'); $result = []; if (!empty($files)) { foreach ($files as $file) { $extension = $file->getFileType()->getExtension(); $stream = base64_encode(stream_get_contents($file->getFile())); $viewFile = new FileView( $file->getOriginalName(), $stream, $extension ); $result['html'][] = $viewFile; } } return $result; } /** * @param int $templateId [description] * * @return array [description] */ private function deleteTemplateOnPageOption(int $templateId): array { $options = $this->pageOptionsRepository->findByTemplate($templateId); $pages = []; if (count($options) > 0) { foreach ($options as $option) { if (count($option->getPages())) { foreach ($option->getPages() as $page) { $pages[] = $page->getId(); } } $option->setTemplate(null); $this->pageOptionsRepository->save($option); } } return $pages; } public function getProjectByTemplateId(int $templateId) { $projects = $this->templateRepository->getProjectNameByTemplate($templateId); $result = []; foreach ($projects as $project) { $result[] = $project['name']; } return implode(", ", $result); } }