projectRepository = $projectRepository; $this->exportsRepository = $exportsRepository; $this->elementTypesTemplatesRepository = $elementTypesTemplatesRepository; $this->elementTypeRepository = $elementTypeRepository; $this->exportTemplateOptionRepository = $exportTemplateOptionRepository; $this->entityManager = $entityManager; $this->draftQuery = $draftQuery; $this->productSaveQuery = $productSaveQuery; $this->story = $story; } /** * @param ExportsSaveView $view [description] * * @return bool|Exception [description] */ public function save(ExportsSaveView $view) { // TODO : Add controls $exports = $view->exports['data']; $project = $this->projectRepository->findById($view->projectId); $storyExports = []; $diffElements = []; $noElementType = $this->elementTypeRepository->findNoElementTypeByIdProject($view->projectId); foreach ($exports as $export) { $previousElement =[]; $exportTypeId = $export['exportType']; $typeTabs = $export['typeTabs']; $entityExport = null; if (isset($export['id'])) { $entityExport = $this->exportsRepository->findById($export['id']); if(!empty($entityExport)){ $templates = $this->elementTypesTemplatesRepository->findElementTypesTemplatesWithExportByExportId($export['id']); if ($templates) { foreach ($templates as $template) { $previousElement[$template->getElementType()->getId()] = $template->getElementsPIM(); $entityExport->removeTemplate($template); } } } } if(empty($entityExport)) { $entityExport = new Exports(); } $entityExport->setName($export['title']); $entityExport->setDescription($export['description']); $entityExport->setProject($project); $entityExport->setExportType($exportTypeId); $entityExport->setCanal($export['canal']); $elementTypeStory = []; $entityTemplateNoElementType = new ElementTypesTemplates(); $entityTemplateNoElementType->setElementType($noElementType); $entityTemplateNoElementType->setExport($entityExport); if(isset($export['templates'])){ $entityTemplateNoElementType->setTemplates($export['templates']); } else { $entityTemplateNoElementType->setTemplates([]); } $entityExport->addTemplate($entityTemplateNoElementType); $this->entityManager->persist($entityTemplateNoElementType); foreach ($typeTabs as $typeTab) { $elementType = $this->elementTypeRepository->findElementTypeByIdProject($typeTab['id'], $view->projectId); $templates = $typeTab['templates']; $defaultTemplateId = isset($typeTab['defaultTemplate']) && !empty($typeTab['defaultTemplate']) ? reset($typeTab['defaultTemplate'])['id'] : null; if (!empty($typeTab['templates']) && empty($defaultTemplateId) && 1 === count($typeTab['templates'])) { $defaultTemplateId = reset($typeTab['templates'])['id']; } $elementsPim = []; if (!empty($typeTab['elements'])) { foreach ($typeTab['elements'] as $element) { $elementsPim[] = $element['id']; } } $entityTemplate = new ElementTypesTemplates(); $entityTemplate->setElementType($elementType); $entityTemplate->setExport($entityExport); if (empty($templates)) { $templates = []; } if(array_key_exists($elementType->getId(), $previousElement)){ $diffElements = array_merge($diffElements, array_diff($previousElement[$elementType->getId()], $elementsPim)); } $entityTemplate->setTemplates($templates); if (empty($defaultTemplateId)) { $defaultTemplateId = null; } $entityTemplate->setDefaultTemplate($defaultTemplateId); $entityTemplate->setElementsPIM($elementsPim); $entityExport->addTemplate($entityTemplate); if (empty($templates)) { $templates = []; } $this->entityManager->persist($entityTemplate); $elementTypeStory[] = new ElementTypeExportStoryView( $typeTab['id'], $defaultTemplateId, $templates, $elementsPim ); } $storyExports[] = new ExportStoryView( $export['title'], $export['description'], $exportTypeId, $elementTypeStory ); } try { $this->entityManager->persist($entityExport); $this->entityManager->flush(); if (!empty($diffElements)){ $this->productSaveQuery->preparePageToGeneViaElement($diffElements, $view->projectId); } if($project->getCurrentStep() < 2){ $project->setCurrentStep(2); $this->projectRepository->create($project); } if (!empty($storyExports)) { $this->story->execute('Step 3', ['value' => $storyExports], $view->projectId, get_class($project), 0); } if (isset($view->exports['lastStep']) && $view->exports['lastStep']) { $this->draftQuery->changeDraft($view->projectId); } $result = true; } catch (Exception $exception) { $result = $exception; } return $result; } /** * @param ExportOptionView $view [description] * * @return [type] [description] */ public function saveOption(ExportOptionView $view) { $option = null; if ($view->id) { $option = $this->exportTemplateOptionRepository->findById($view->id); } if (!isset($option)) { $option = new ExportTemplateOption(); } $export = $this->exportsRepository->findById($view->export_id); if (!$export) { return null; } $option->setExport($export); $option->setValue($view->value); if($view->variableProject){ $variableProjectRepo = $this->entityManager->getRepository(VariableProject::class); $variableProject = $variableProjectRepo->findOneBy(["id" => $view->variableProject]); if(!empty($variableProject)){ $option->setVariableProject($variableProject); } } $option->setFlatplanPageId($view->flatplan_page_id); if($view->pageDuplicate_id){ $pageDupliRepo = $this->entityManager->getRepository(PageDuplicate::class); $pageDuplicate = $pageDupliRepo->findOneBy(["id" => $view->pageDuplicate_id]); if(!empty($pageDuplicate)){ $option->setPageDuplicate($pageDuplicate); } } $option = $this->exportTemplateOptionRepository->save($option); return $option->getId(); } }