container = $_container; $this->entity_manager = $this->container->get('doctrine.orm.entity_manager'); $this->token_storage = $this->container->get('security.token_storage'); $this->class = $_class; $this->templating = $this->container->get('twig'); } /** * @param array $languages * @param string|null $redirectURL * @param string|null $tag * @return FormForgeReason|null * @throws \Exception */ public function create(array $languages, ?string $redirectURL, ?string $tag){ $formForgeReason = new FormForgeReason(); $nameLinguistica = new LinguisticaTradItem(); $nameLinguistica->setStrValues($languages); $formForgeReason->setDefault($nameLinguistica); $formForgeReason->setRedirectURL($redirectURL); $formForgeReason->setTag($tag); if(!$this->quickUpdate($formForgeReason)) return null; foreach ($formForgeReason->getName()->getStrValues() as $key=>$value){ $slug_element = $this->container->get('net15.slug_manager')->create($key, $value, 'FormForgeBundle\Entity\FormForgeReason', $formForgeReason->getId()); } return $formForgeReason; } /** * @param string $publicKey * @return FormForgeReason|null|object */ public function getByPublicKey(string $publicKey){ return $this->entity_manager->getRepository($this->class)->findOneBy(['publicKey'=>$publicKey,'deleted'=>false]); } /** * @param string $tag * @return object|null */ public function getByTag(string $tag){ return $this->entity_manager->getRepository($this->class)->findOneBy(['tag'=>$tag,'deleted'=>false]); } /** * @param string $tag * @return array|object[] */ public function getAllByTag(?string $tag){ if($tag === null) return []; return $this->entity_manager->getRepository($this->class)->findBy(['tag'=>$tag,'deleted'=>false]); } /** * @return FormForgeReasonInterface[]|array|object[] */ public function getAll(){ return $this->entity_manager->getRepository($this->class)->findBy(['deleted'=>false]); } /** * @param FormForgeReasonInterface $element * @return bool */ public function quickUpdate(FormForgeReasonInterface $element): bool{ try { if(!$this->entity_manager->contains($element)){ $r = $this->checkAndValidatePublicKey($element); if($r) $this->entity_manager->persist($element); else return false; } $this->checkAndValidateTag($element); $this->entity_manager->flush(); } catch (OptimisticLockException $e) { //die('Exception : '.$e->getMessage()); //throw new Exception('Exception : '.$e->getMessage()); return false; } catch (ORMException $e) { return false; } return true; } /** * @param FormForgeReasonInterface $element * @return void */ public function checkAndValidateTag(FormForgeReasonInterface $element):void{ $elements = $this->getAllByTag($element->getTag()); foreach ($elements as $compare){ /** * @var $compare FormForgeReasonInterface */ if($compare->getId() != $element->getId()){ $element->setTag($element->getTag().'_'.uniqid()); } } } /** * @param FormForgeReasonInterface $element * @return bool */ public function checkAndValidatePublicKey(FormForgeReasonInterface $element):bool{ $attempts = 0; while(true){ $u = $this->getByPublicKey($element->getPublicKey()); if(is_null($u)) return true; $element->regeneratePublicKey(); $attempts++; if($attempts > 20) return false; } return false; } /** * @param FormForgeReasonInterface $element * @return bool */ public function update(FormForgeReasonInterface $element){ if(empty($element)) return false; return $this->quickUpdate($element); } /** * @param FormForgeReasonInterface $element * @param CustomUserInterface|null $user * @return bool */ public function canEdit(FormForgeReasonInterface $element, ?CustomUserInterface $user = null){ if(is_null($user)) { $user = $this->token_storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } if($element->isDeleted()) return false; if($user->hasRole('ROLE_SUPPORT_ADMIN')) return true; return false; } /** * @param FormForgeReasonInterface $element * @param CustomUserInterface|null $user * @return bool */ public function canView(FormForgeReasonInterface $element, ?CustomUserInterface $user = null){ if(is_null($user)) { $user = $this->token_storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } if($element->isDeleted()) return false; if($user->hasRole('ROLE_SUPPORT') || $user->hasRole('ROLE_SUPPORT_ADMIN')) return true; return false; } /** * @param CustomUserInterface|null $user * @return bool */ public function canList(?CustomUserInterface $user = null){ if(is_null($user)) { $user = $this->token_storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } if($user->hasRole('ROLE_SUPPORT') || $user->hasRole('ROLE_SUPPORT_ADMIN')) return true; return false; } /** * @param FormForgeReasonInterface $element * @param CustomUserInterface|null $user * @return bool */ public function canDelete(FormForgeReasonInterface $element, ?CustomUserInterface $user = null){ if(is_null($user)) { $user = $this->token_storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } if($element->isDeleted()) return false; //TODO hasRole on User if($user->hasRole('ROLE_SUPPORT_ADMIN')) return true; return false; } /** * @param FormForgeReasonInterface $element * @param CompanyService $compagnyService * @return bool */ public function addService(FormForgeReasonInterface $element, CompanyService $compagnyService){ $element->setCompanyService($compagnyService); return $this->quickUpdate($element); } /** * @param FormForgeReasonInterface $element * @param CompanyService $compagnyService * @return bool */ public function updateService(FormForgeReasonInterface $element, CompanyService $compagnyService){ $element->setCompanyService($compagnyService); return $this->quickUpdate($element); } /** * @param FormForgeReasonInterface $element * @param CompanyService $compagnyService * @return bool */ public function removeService(FormForgeReasonInterface $element, CompanyService $compagnyService){ $element->setCompanyService($compagnyService); return $this->quickUpdate($element); } /** * @param FormForgeReasonInterface $element * @param UserGroup $group * @return bool */ public function addUserGroup(FormForgeReasonInterface $element, UserGroup $group){ $element->setSelfGroup($group); return $this->quickUpdate($element); } /** * @param FormForgeReasonInterface $element * @param UserGroup $group * @return bool */ public function updateSelfGroup(FormForgeReasonInterface $element, UserGroup $group){ $element->setSelfGroup($group); return $this->quickUpdate($element); } /** * @param FormForgeReasonInterface $element * @param UserGroup $group * @return bool */ public function removeUserGroup(FormForgeReasonInterface $element, UserGroup $group){ $element->setSelfGroup($group); return $this->quickUpdate($element); } /** * @param string $publicKey * @return bool */ public function delete(string $publicKey){ $element = $this->getByPublicKey($publicKey); if (!$element instanceof FormForgeReasonInterface) return null; $element->setDeleted(true); return $this->quickUpdate($element); } /** * @param FormForgeReasonInterface $element * @return bool */ private function deleteOnError(FormForgeReasonInterface $element) { try { $this->entity_manager->remove($element); $this->entity_manager->flush(); } catch (ORMException $e) { return false; } return true; } /** * @param FormForgeReasonInterface $element * @param FormForgeFieldInterface $formForgeFieldElement * @return bool */ public function addFormForgeField(FormForgeReasonInterface $element, FormForgeFieldInterface $formForgeFieldElement){ $element->addFormForgeField($formForgeFieldElement); return $this->quickUpdate($element); } /** * @param string $publicKey * @param array $languages * @param string $redirectURL * @param string $tag * @return FormForgeReason|bool|null|object */ public function updateReason(string $publicKey, array $languages, string $redirectURL, string $tag){ if(empty($publicKey) || empty($languages)) return false; $formForgeReason = $this->getByPublicKey($publicKey); $formForgeReason->getName()->setStrValues($languages); $formForgeReason->setRedirectURL($redirectURL); $formForgeReason->setTag($tag); $this->quickUpdate($formForgeReason); return $formForgeReason; } public function getOneById($id) { return $this->entity_manager->getRepository($this->class)->findOneBy(['id'=>$id,'deleted'=>false]); } /** * @param string $tag * @param string $locale * @param array|null $parameters * @return string * @throws \Twig\Error\LoaderError * @throws \Twig\Error\RuntimeError * @throws \Twig\Error\SyntaxError */ public function renderer(string $tag, string $locale) { $reason = $this->getByTag($tag); return $this->templating->render('@FormForge/FormForgeGenerate/generate.twig.form.html.twig',[ 'reason' => $reason, 'reasonPK' => $reason->getPublicKey(), 'csrf_key' => $reason->getId(), 'customFieldTicket' =>$reason->getFormForgeFields()->getValues(), 'lang' => $locale ]); } /** * @param string $tag * @param string $locale * @param array $parameters * @return string * @throws \Twig\Error\LoaderError * @throws \Twig\Error\RuntimeError * @throws \Twig\Error\SyntaxError */ public function rendererWithParameters(string $tag, string $locale, array $parameters) { $reason = $this->getByTag($tag); return $this->templating->render('@FormForge/FormForgeGenerate/generate.twig.form.html.twig',[ 'reason' => $reason, 'reasonPK' => $reason->getPublicKey(), 'csrf_key' => $reason->getId(), 'customFieldTicket' =>$reason->getFormForgeFields()->getValues(), 'lang' => $locale, 'parameters' => $parameters ]); } }