container = $container; $this->entityManager = $this->container->get('doctrine.orm.entity_manager'); $this->session = $this->container->get('session'); $this->repository = $this->entityManager->getRepository(AGridSlugElement::class); } /** * @param String $lang * @param string $mappedEntity * @param int $id * * @return string */ public function getSlug(String $lang, string $mappedEntity, int $id): string { $slugElement = $this->repository->findOneBy(['lang'=>$lang,'mappedEntity'=>$mappedEntity, 'mappedEntity_id'=> $id]); if($slugElement instanceof AGridSlugElement) return $slugElement->getSlug(); return 'not-found'; } /** * @param int $id * * @return AGridSlugElement|object|null */ public function getById(int $id){ return $this->repository->find($id); } public function getByMappedElement(string $mappedEntity, int $mappedEntity_id, string $lang){ $element = $this->repository->findOneBy(['mappedEntity'=>$mappedEntity,'mappedEntity_id'=>$mappedEntity_id,'lang'=>$lang]); if($element == null){ try { $element = $this->create($lang, $mappedEntity_id.'-'.$lang, $mappedEntity, $mappedEntity_id, '', '', ''); } catch (Exception $e) { return null; } } return $element; } /** * @return AGridSlugElement[]|array|object[] */ public function getAll(): array { return $this->repository->findBy(['mappedEntity'=>'static']); } /** * @param string $lang * @param string $slug * @param string $mappedEntity * @return EntityRepository|ObjectRepository|object|null */ public function findSlug(string $lang, string $slug, string $mappedEntity = '') { if($mappedEntity === '') { $slugElement = $this->repository->findOneBy(['lang' => $lang, 'slug' => $slug]); } else { $slugElement = $this->repository->findOneBy(['lang' => $lang, 'slug' => $slug, 'mappedEntity' => $mappedEntity]); } return $slugElement; } /** * @param string $lang * @param string $slug * @param string $mappedEntity * * @return object|null */ public function getMappedElement(string $lang, string $slug, string $mappedEntity = '') { if($mappedEntity === '') { $slugElement = $this->repository->findOneBy(['lang' => $lang, 'slug' => $slug]); } else { $slugElement = $this->repository->findOneBy(['lang' => $lang, 'slug' => $slug, 'mappedEntity' => $mappedEntity]); } if($slugElement instanceof AGridSlugElement) { $mappedElement= $this->entityManager->getRepository($slugElement->getMappedEntity())->find($slugElement->getMappedEntityId()); if(method_exists($mappedElement,'setSlug')) { $mappedElement->setSlug($slugElement); } return $mappedElement; } return null; } /** * @param AGridSlugElement $slugElement * @return object|null */ public function getMappedElementFromSlug(AGridSlugElement $slugElement) { if($slugElement instanceof AGridSlugElement) { return $this->entityManager->getRepository($slugElement->getMappedEntity())->find($slugElement->getMappedEntityId()); } return null; } /** * @param string $lang * @param string $title * @param string $mappedEntity * @param int $id * @param string $seo_title * @param string $seo_description * @param string $image * @return AGridSlugElement $slugElement * @throws Exception */ public function create(string $lang, string $title, string $mappedEntity, int $id,string $seo_title = '', string $seo_description='', string $image = ''): AGridSlugElement { $slug = $this->sanitize($title); $slugElement = new AGridSlugElement($lang, $slug, $mappedEntity, $id); $slugElement->initSEO(); $slugElement->setDescription($seo_description); $slugElement->setTitle($seo_title); $slugElement->setKeywords($seo_title); $slugElement->setSeoAbsolutePathImage($image); $this->update($slugElement); return $slugElement; } /** * @param AGridSlugElement $slugElement * @param array $dataArray * * @return bool|null */ public function edit(AGridSlugElement $slugElement, array $dataArray): ?bool { if ($slugElement == null || empty($dataArray)) return null; $slugElement->hydrate($dataArray); if (!$this->quickUpdate($slugElement)) return null; return true; } /** * @param AGridSlugElement $slugElement * * @return bool|null */ public function deleteSlug(AGridSlugElement $slugElement): ?bool { if ($slugElement == null) return null; if (!$this->delete($slugElement)) return null; return true; } /** * @param AGridSlugElement $element * @return bool */ public function update(AGridSlugElement $element): bool { return $this->quickUpdate($element); } /** * @param AGridSlugElement $element * @return bool */ public function quickUpdate(AGridSlugElement $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 AGridSlugElement $element * @return bool */ public function quickPersist(AGridSlugElement $element): bool { try { if (!$this->entityManager->contains($element)) { if ($element) $this->entityManager->persist($element); else return false; } } catch (ORMException $e) { return false; } return true; } /** * @return bool */ public function flush(): bool { try { $this->entityManager->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } return true; } /** * @param AGridSlugElement $element * @return bool */ public function delete(AGridSlugElement $element): bool { if($this->entityManager->contains($element)){ try { $this->entityManager->remove($element); $this->entityManager->flush(); } catch (ORMException $e) { return false; } } return true; } /** * @param $title * * @return mixed|string|string[]|null */ private function sanitize($title) { $table = array( 'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r', '/' => '-', ' ' => '-', "'" => '-', ' & ' => '-', ' - ' => '-' ); // -- Remove duplicated spaces $title = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $title); // -- Returns the slug $title = strtolower(strtr($title, $table)); $search = array ( '@[ ]@i', '@[^a-zA-Z0-9_-]@' ); $replace = array ( '-', '-' ); $return = preg_replace ( $search, $replace, $title ); $return = str_replace(['---','--'],'-',$return); $return = trim($return,'-'); return $return; } /** * @param string $mappedEntity * @throws DBALException */ public function clean(string $mappedEntity): void { $mappedEntity = str_replace('\\','\\\\',$mappedEntity); $sql = "DELETE FROM slug_element WHERE mapped_entity ='$mappedEntity'"; $conn = $this->entityManager->getConnection(); $stmt = $conn->prepare($sql); $stmt->execute(); } }