container = $_container; $this->entityManager = $_container->get('doctrine.orm.entity_manager'); $this->eventDispatcher = $_container->get('event_dispatcher'); $this->templating = $_container->get('twig'); $this->agridCacheService = $_container->get('agrid.cache_service'); $this->repository = $this->entityManager->getRepository(AGridPage::class); } public function getAll(){ return $this->repository->findAll(); } /** * @return AGridPage|null * @throws Exception */ public function createPage(): ?AGridPage { $page = new AGridPage(); // Creating the content when the page is created $content = $this->addContent(); $page->setContent($content); if(!$this->update($page)) return null; return $page; } public function addContent(): ?AGridContent { $content = $this->container->get('agrid.content_manager')->createContent(); if (!$content instanceof AGridContent) return null; return $content; } /** * @param AGridPage $element * @return bool */ public function update(AGridPage $element): bool { $this->agridCacheService->invalidate($element->getContent()); return $this->quickUpdate($element); } /** * @param AGridPage $element * @return bool */ public function quickUpdate(AGridPage $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 int $id * @return AGridPage|object|null */ public function getById(int $id) { return $this->repository->findOneBy(['id'=>$id]); } public function getByContent(AGridContent $content) { return $this->repository->findOneBy(['content'=>$content]); } // /** // * @param AGridPage $page // * @param string $lang // * @param string $url // * @return AGridPage // */ // public function addRoute(AGridPage $page, string $lang, string $url): AGridPage // { //// $routingElement = $this->container->get('agrid.route_manager')->create($page, $lang); //// $page->addMapping($lang, $routingElement); //// $this->container->get('agrid.route_manager')->addRoute($routingElement,$lang, $url, $page); // // $this->update($page); // return $page; // } }