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->container = $_container; $this->repository = $this->entityManager->getRepository(AGridCol::class); } /** * @param AGridRow $row * @return AGridCol|null */ public function create( AGridRow $row): ?AGridCol { $order = count($row->getCols()); $col = new AGridCol($row); $col->setOrdered($order); if(!$this->update($col)) return null; return $col; } /** * @param AGridCol $element * @return bool */ public function update(AGridCol $element): bool { $this->agridCacheService->invalidate($element->getParentRow()->getParentContainer()); return $this->quickUpdate($element); } /** * @param AGridCol $element * @return bool */ public function quickUpdate(AGridCol $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 AGridCol|object|null */ public function getById(int $id) { return $this->repository->findOneBy(['id'=>$id]); } /** * @param int $col_id * @param int $order * @return bool */ public function changeOrder(int $col_id, int $order): bool { $col = $this->getById($col_id); if($col instanceof AGridCol){ $col->setOrdered($order); $this->update($col); return true; } return false; } public function changeParentRow(int $col_id, AGridRow $row){ $col = $this->getById($col_id); if($col instanceof AGridCol){ $col->setParentRow($row); $this->update($col); return true; } return false; } /** * @param int $col_id * @param string $mode * @param int $size * @return string */ public function setSize(int $col_id, string $mode, int $size): string { $col = $this->getById($col_id); if($col instanceof AGridCol) { switch ($mode) { case 's': $col->setSizeS($size); break; case 'm': $col->setSizeM($size); break; case 'l': $col->setSizeL($size); break; case 'xl': $col->setSizeXl($size); break; } $this->update($col); } return 'col '.$col->gridSize($mode) ; } public function delete(AGridCol $col){ try { $container = $col->getParentRow()->getParentContainer(); foreach ($col->getRows() as $row){ $this->container->get('agrid.row_manager')->delete($row); } //on supprime les liaisons avec agrid_content_col_module $this->container->get('agrid.content_col_module.manager')->delete($col); $this->entityManager->remove($col); $this->entityManager->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } $this->agridCacheService->invalidate($container); return true; } }