container = $_container; $this->entityManager = $_container->get('doctrine.orm.entity_manager'); $this->templating = $_container->get('twig'); $this->request = $_container->get('request_stack'); $this->repository = $this->entityManager->getRepository(AGridContentColModule::class); } /** * @param AGridContentColModule $element * @return bool */ public function quickUpdate(AGridContentColModule $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 AGridContentColModule $element * @return bool */ public function update(AGridContentColModule $element): bool { $this->container->get('agrid.cache_service')->invalidate($element->getAGridContent()); return $this->quickUpdate($element); } public function getByCol(AGridCol $col): AGridContentColModule { return $this->repository->findOneBy(['aGridCol'=> $col]); } public function getByModule(AGridModule $module): AGridContentColModule { return $this->repository->findOneBy(['aGridModule'=> $module]); } public function getByContentAndCol(AGridCol $col, AGridContent $content):AGridContentColModule { return $this->repository->findOneBy(['aGridContent'=>$content, 'aGridCol'=>$col]); } public function create(AGridContent $content, AGridCol $col){ $element = new AGridContentColModule($content, $col); if(!$this->update($element)) return null; return $element; } /** * @param AGridContent $content * @param AGridCol $col * @param string $mode * @param string $lang * @return false|string * @throws \Twig\Error\LoaderError * @throws \Twig\Error\RuntimeError * @throws \Twig\Error\SyntaxError */ public function rendererBtn(AGridContent $content, AGridCol $col, string $mode, string $lang){ $element = $this->getByCol($col); $module = $element->getAgridModule(); if ($module == null){ //Affcihe la creation de module return $this->templating->render('@AGrid/agrid_modules/btns/create.module.html.twig',[ 'content'=>$content, 'col'=>$col, 'module'=>$module, 'mode' => $mode, 'lang' => $lang ]); } else { switch ($module->getTypeId()->getTypeUniqId()){ case 'agrid-text-editor': return $this->templating->render('@AGrid/agrid_modules/btns/text-editor.html.twig',[ 'content'=>$content, 'col'=>$col, 'module'=>$module, 'mode' => $mode, 'lang' => $lang ]); break; default: return $this->templating->render('@AGrid/agrid_modules/btns/module.default.html.twig',[ 'content'=>$content, 'col'=>$col, 'module'=>$module, 'mode' => $mode, 'lang' => $lang ]); break; } } } /** * @param AGridContent $content * @param AGridCol $col * @param string $mode * @param string $lang * @return string * @throws \Twig\Error\LoaderError * @throws \Twig\Error\RuntimeError * @throws \Twig\Error\SyntaxError */ public function rendererModuleContainer(AGridContent $content, AGridCol $col, string $mode, string $lang){ $element = $this->getByContentAndCol($col, $content); return $this->templating->render('@AGrid/agrid_modules/module_container.html.twig', ['contentColModule'=>$element,'lang'=>$lang,'mode'=>$mode]); } public function addModule(AGridCol $col, AGridContent $content, string $type){ //Recuperate AgridPageColModule by col and page $contentColModule = $this->getByContentAndCol($col, $content); if (!$contentColModule instanceof AGridContentColModule) return null; //Recuperate AgridModuleType $moduleType = $this->container->get('agrid.module_type.manager')->getByTypeUniqId($type); if (!$moduleType instanceof AGridModuleType) return null; //Instantiate new AgridModule and set it in AgridPageColModule object $module = $this->container->get('agrid.module_manager')->create($content, $moduleType); // switch($type){ // case 'agrid-text-editor': // $module->addParameter('twig','@AGrid/agrid_modules/renderer.html.twig'); // break; // } $contentColModule->setAgridModule($module); if (!$this->update($contentColModule)) return null; return $contentColModule; } public function delete(AGridCol $col): bool { $agridContentColModule = $this->getByCol($col); $container = $agridContentColModule->getAGridContent(); try { $this->entityManager->remove($agridContentColModule); $this->entityManager->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } $this->container->get('agrid.cache_service')->invalidate($container); return true; } }