twigRepository = $twigRepository; $this->entityManager = $entityManager; $this->variableRepository = $variableRepository; $this->inputRepository = $inputRepository; } /** * @param CommonIdView $view [description] * * @return Twig|null [description] */ public function read(CommonIdView $view): ?Twig { return $this->twigRepository->findById($view->id); } /** * @return array [description] */ public function list(): array { $twigs = $this->twigRepository->list(); $view = []; if (!empty($twigs)) { foreach ($twigs as $twig) { $view[] = new TwigListView( $twig->getId(), $twig->getName(), $twig->getExportType() ); } } return $view; } /** * @param string $name [description] * * @return bool [description] */ public function isNameAlreadyExist(string $name, ?int $id = null): bool { if (empty($id)) { $twig = $this->twigRepository->findByName($name); } else { $twig = $this->twigRepository->findByNameAndDifferentId($name, $id); } if ($twig) { return true; } return false; } /** * @param Twig $twig [description] * * @return TwigView [description] */ public function getTwigView(CommonIdView $view): TwigView { $twig = $this->read($view); $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 int $twigId [description] * * @return array [description] */ public function getVariables(int $twigId){ $commonIdView = new CommonIdView( $twigId ); $twig = $this->read($commonIdView); $result = []; if(!empty($twig) && !$twig->getVariables()->isEmpty()){ $variables = $twig->getVariables(); $criteriaObj = new Criteria(); $criteriaObj->orderBy(["position" => Criteria::ASC, "id" => Criteria::ASC]); $variables = $variables->matching($criteriaObj); $valueRepository = $this->entityManager->getRepository(Value::class); foreach($variables as $variable){ if($variable->getDeletedAt() == null){ $values = $valueRepository->findBy([ "templateFieldId" => $variable->getId(), "deletedAt" => null ]); $inputs = []; if(!$variable->getInputs()->isEmpty()){ $inputsVariable = $variable->getInputs(); $inputsVariable = $inputsVariable->matching($criteriaObj); foreach ($inputsVariable as $input) { if($input->getDeletedAt() == null){ $inputs[] = [ "id" => $input->getId(), "name" => $input->getVariableName(), "type" => $input->getFieldType(), "listValue" => json_decode($input->getListValue(), true) != null ? json_decode($input->getListValue(), true) : [], "variableProject" => !empty($input->getVariableProject()) ? ["id" => $input->getVariableProject()->getId(), "name" => $input->getVariableProject()->getName()] : null, ]; } } } $result[] = [ "id" => $variable->getId(), "name" => $variable->getTwigVar(), "type" => $variable->getFieldType(), "listValue" => json_decode($variable->getListValue(), true) != null ? json_decode($variable->getListValue(), true) : [], "input" => $inputs, "forElement" => $variable->getForElement(), "isUse" => !empty($values) ? true : false, "variableProject" => !empty($variable->getVariableProject()) ? ["id" => $variable->getVariableProject()->getId(), "name" => $variable->getVariableProject()->getName()] : null, ]; } } } return $result; } /** * @param array $variables [description] * * @return bool [description] */ public function UpdateVariable(array $variables, int $twigId){ if(!empty($variables)){ $commonIdView = new CommonIdView( $twigId ); $twig = $this->read($commonIdView); $position = 1; $variableRepo = $this->entityManager->getRepository(Variable::class); $variableProjRepo = $this->entityManager->getRepository(VariableProject::class); $inputRepo = $this->entityManager->getRepository(Input::class); $idVariables = array_map(function($var) { if($var['id'] > 0){ return $var['id']; } }, $variables); $idVariables = array_filter($idVariables, fn ($value) => !is_null($value)); $this->deleteVariables($twig, $idVariables); foreach ($variables as $variable) { $var = $variableRepo->findOneBy([ "id" => $variable['id'] ]); $varProj = null; if(!empty($variable['variableProject'])){ $varProj = $variableProjRepo->findOneBy([ "id" => $variable['variableProject'][0]["id"] ]); } if(empty($var)){ $var = new Variable(); } $var->setName(trim(str_replace(['__','_'],' ',$variable['name']))); $var->setTwigVar($variable['name']); $var->setTwig($twig); $var->setForElement($variable['forElement']); $var->setFieldType($variable['type']); $var->setListValue(json_encode($variable['listValue'])); $var->setPosition($position); if(!empty($varProj)){ $var->setVariableProject($varProj); } else { $varProj = $variableProjRepo->findOneBy([ "name" => $variable['name'], "categorie" => "variable", "fieldType" => $variable['type'] ]); if(empty($varProj)){ $varProj = new VariableProject(); $varProj->setName($variable['name']); $varProj->setCategorie("variable"); $varProj->setFieldType($variable['type']); $this->entityManager->persist($varProj); } $var->setVariableProject($varProj); } if(!empty($variable['inputs'])){ if(!empty($var->getId())){ $idInputs = array_map(function($inp) { if($inp['id'] > 0){ return $inp['id']; } }, $variable['inputs']); $this->deleteInput($var, $idInputs); } $positionInput = 1; foreach ($variable['inputs'] as $input) { $varInputProj = null; $inp = $inputRepo->findOneBy([ "id" => $input['id'] ]); if(empty($inp)){ $inp = new Input(); $inp->setVariable($var); } $inp->setName(trim(str_replace(['__','_'],' ',$input['name']))); $inp->setVariableName($input['name']); $inp->setFieldType($input['type']); $inp->setListValue(json_encode($input['listValue'])); $inp->setPosition($positionInput); if(!empty($input['variableProject'])){ $varInputProj = $variableProjRepo->findOneBy([ "id" => $input['variableProject'][0]['id'] ]); } else { $varInputProj = $variableProjRepo->findOneBy([ "name" => $input['name'], "categorie" => "input", "fieldType" => $input['type'] ]); if(empty($varInputProj)){ $varInputProj = new VariableProject(); $varInputProj->setName($input['name']); $varInputProj->setCategorie("input"); $varInputProj->setFieldType($input['type']); $this->entityManager->persist($varInputProj); } $inp->setVariableProject($varInputProj); } $this->entityManager->persist($inp); $positionInput ++; } } $this->entityManager->persist($var); $position ++; } $this->entityManager->flush(); } return true; } private function deleteVariables(Twig $twig, array $idVariables){ $criteriaObj = new Criteria(); $criteriaObj->where(Criteria::expr()->notIn('id', $idVariables)) ->andWhere(Criteria::expr()->eq('deletedAt', null)); $variables = $twig->getVariables()->matching($criteriaObj); if(!empty($variables)){ $this->variableRepository->removeMultiple($variables->toArray()); } } private function deleteInput(Variable $variable, array $idInputs){ $criteriaObj = new Criteria(); $criteriaObj->where(Criteria::expr()->notIn('id', $idInputs)) ->andWhere(Criteria::expr()->eq('deletedAt', null)); $inputs = $variable->getInputs()->matching($criteriaObj); if(!empty($inputs)){ $this->inputRepository->removeMultiple($inputs->toArray()); } } }