entityManager = $_container->get('doctrine.orm.entity_manager'); $this->repository = $this->entityManager->getRepository(RoutingElement::class); $this->route_repository = $this->entityManager->getRepository(Route::class); } /** * @param RoutableInterface $element * @param string $lang * @return RoutingElement|null */ public function create(RoutableInterface $element, string $lang) { //on verifie l'existance de l'élément $routingelement = $this->getByMappedEntity($element->getMappedID($lang)); if(!$routingelement instanceof RoutingElement){ //create $routingelement = new RoutingElement($lang, $element); if(!$this->quickUpdate($routingelement)) return null; } return $routingelement; } /** * @param RoutingElement $routingElement * @param string $lang * @param string $url * @param mixed $entity * @return RoutingElement */ public function addRoute(RoutingElement $routingElement, string $lang, string $url, $entity): RoutingElement { $route = $this->route_repository->findOneBy(['name'=>$routingElement->getMappedID($lang)]); if($route instanceof Route){ //il y avait une ancienne route on la renome $route->setName($routingElement->getMappedID($lang)."-".time()); $route->setOption('redirect',301); $route->setPosition(301); $this->quickUpdateRoute($route); } $route = new Route(); $route->setName($routingElement->getMappedID($lang)); $route->setOption('lang',$lang); $clean_url = str_replace('//', '/', '/'.$lang.'/'.$url); $route->setStaticPrefix($clean_url); $route->setDefault(RouteObjectInterface::CONTENT_ID, $routingElement->getContentID()); $route->setContent($entity); // $route->setVariablePattern('/cms/{id}'); // $route->setRequirement('id', '\d+'); // $route->setDefault ( 'id' , 1 ); // $route->setDefault ( '_controller' , 'AGridBundle\Controller\AGridPageController:default' ); $routingElement->addNewRoute($route, $clean_url); $this->quickUpdate($routingElement); return $routingElement; } public function updateSEO(RoutingElement $routingElement, string $title, string $description, string $keywords, string $image, string $siteName, string $twittercard, string $ogtype): bool { $routingElement->setTitle($title); $routingElement->setDescription($description); $routingElement->setKeywords($keywords); $routingElement->setSeoAbsolutePathImage($image); $routingElement->setSeoOgSiteName($siteName); $routingElement->setTwitterCard($twittercard); $routingElement->setOgType($ogtype); return $this->quickUpdate($routingElement); } /** * @param string $mappedEntity * @return RoutingElement|null */ public function getByMappedEntity(string $mappedEntity): ?RoutingElement { return $this->repository->findOneBy(['mappedEntity'=>$mappedEntity]); } /** * @param string $name * @return Route|null */ public function getByName(string $name): ?Route { return $this->route_repository->findOneBy(['name'=>$name]); } /** * @param int $id * @return Route|null */ public function getById(int $id): ?Route { return $this->route_repository->find($id); } /** * @param RoutingElement $element * @return bool */ public function update(RoutingElement $element): bool { return $this->quickUpdate($element); } /** * @param RoutingElement $element * @return bool */ public function quickUpdate(RoutingElement $element): bool { try { if (!$this->entityManager->contains($element)) { if ($element) $this->entityManager->persist($element); else return false; } $this->entityManager->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } return true; } /** * @param Route $element * @return bool */ public function quickUpdateRoute(Route $element): bool { try { if (!$this->entityManager->contains($element)) { if ($element) $this->entityManager->persist($element); else return false; } $this->entityManager->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } return true; } /** * @param RoutingElement|null $routingElement * @param string $name * @return bool */ public function removeRouteByName(?RoutingElement $routingElement, string $name): bool { $route = $this->route_repository->findOneBy(['name'=>$name]); if($routingElement instanceof RoutingElement && $route instanceof Route){ $routingElement->removeRoute($route); $this->quickUpdate($routingElement); try { $this->entityManager->remove($route); return true; } catch (ORMException $e) { return false; } } return false; } }