vendor/score/cms/src/Services/CmsTwigManager.php line 139

Open in your IDE?
  1. <?php
  2. namespace Score\CmsBundle\Services;
  3. use Score\BaseBundle\Services\AdjacencyArrayTreeManager;
  4. use Score\CmsBundle\Entity\Block\Accordion;
  5. use Score\CmsBundle\Entity\Document\Document;
  6. use Score\CmsBundle\Entity\Menu;
  7. use Score\CmsBundle\Entity\Option;
  8. use Score\CmsBundle\Entity\Widget\Widget;
  9. class CmsTwigManager {
  10.     protected $em;
  11.     protected $optionTexts = [];
  12. //    protected $adjacencyArrayTreeManager;, AdjacencyArrayTreeManager $adjacencyArrayTreeManager
  13.     public function __construct($em) {
  14.         $this->em = $em;
  15. //        $this->adjacencyArrayTreeManager = $adjacencyArrayTreeManager;
  16.         $this->optionTexts = $this->initOptionTexts();
  17.     }
  18.     protected function initOptionTexts() {
  19.         $qb = $this->em->createQueryBuilder();
  20.         $options = $qb->select("o")
  21.             ->from(Option::class, "o")
  22.             ->where($qb->expr()->isNotNull("o.code"))
  23.             ->andWhere('o.visibility = :p')->setParameter('p', true)
  24.             ->getQuery()->getResult();
  25.         //$options = $this->em->getRepository(Option::class)->findBy(["visibility"=>true]);
  26.         $texts = [];
  27.         if ($options) {
  28.             foreach ($options as $option) {
  29.                 $texts[$option->getCode()] = $option->getValue();
  30.             }
  31.         }
  32.         return $texts;
  33.     }
  34.     public function replaceAllOptionText($text) {
  35.         $arr = explode("CMS_TEXT(", $text);
  36.         $result = array_shift($arr);
  37.         if ($arr) {
  38.             foreach ($arr as $a) {
  39.                 $ex = explode(")", $a, 2);
  40.                 $result .= $this->replaceOptionText($ex[0]);
  41.                 $result .= $ex[1];
  42.                 //array_shift($arr);
  43.             }
  44.         }
  45.         return $result;
  46.     }
  47.     public function replaceOptionText($text) {
  48.         return array_key_exists($text, $this->optionTexts) ? $this->optionTexts[$text] : "-";
  49.     }
  50.     public function getAccordion($id) {
  51.         return $this->em->getRepository(Accordion::class)->find($id);
  52.     }
  53.     public function replaceWidget($params) {
  54.         $p = explode(",", $params, 2);
  55.         $p = array_map('trim', $p);
  56.         $widget = $this->em->getRepository(Widget::class)->findOneByCode($p[0]);
  57.         if (!$widget) {
  58.             return ("no widget");
  59.         }
  60.         $fw = $widget->getWidgetValues()->filter(function ($element) use ($p) {
  61.             return $element->getCode() === $p[1];
  62.         });
  63.         $value = $fw->first();
  64.         if (!$value || !$value->getDataArr()) {
  65.             return ("–");
  66.         }
  67.         $data = $value->getDataArr();
  68.         $template = $widget->getTemplate();
  69.         return $this->parseWidgetTemplate($template, $data);
  70.     }
  71.     protected function parseWidgetTemplate($template, $data) {
  72.         $arr = explode("CMS_WIDGET_VALUE(", $template);
  73.         $result = [];
  74.         $result[] = array_shift($arr);
  75.         if ($arr) {
  76.             foreach ($arr as $a) {
  77.                 $ex = explode(")", $a, 2);
  78.                 $code = trim($ex[0]);
  79.                 $result[] = array_key_exists($code, $data) ? $data[$code] : "*";
  80.                 $result[] = $ex[1];
  81.             }
  82.         }
  83.         return implode("", $result);
  84.     }
  85.     public function replaceAllWidgets($text) {
  86.         $arr = explode("CMS_WIDGET(", $text);
  87.         $result = array_shift($arr);
  88.         if ($arr) {
  89.             foreach ($arr as $a) {
  90.                 $ex = explode(")", $a, 2);
  91.                 $result .= $this->replaceWidget($ex[0]);
  92.                 $result .= $ex[1];
  93.             }
  94.         }
  95.         return $result;
  96.     }
  97.     public function getMenuTree($id) {
  98.         if ($id === 'root') {
  99.             $roots = $this->em->getRepository(Menu::class)->findBy(['rootId' => null], ['createdAt' => 'ASC', 'id' => 'ASC']);
  100.             if ($roots && count($roots)) {
  101.                 return $roots[0]->getChildren();
  102.             } else {
  103.                 return [];
  104.             }
  105.         }
  106.         $menu = $this->em->getRepository(Menu::class)->find($id);
  107.         if (!$menu)
  108.             return [];
  109.         return $menu->getChildren();
  110.     }
  111.     public function updateDocumentLinks($html) {
  112.         if (strpos($html, ' data-cms="document-link"')) {
  113.             $regex = '/(<a([^<>]+)? data-cms\=\"document-link\"([^<>]+)?>([^<>]+)?<\/a>)+/';
  114.             return preg_replace_callback($regex, [$this, 'parseLink'], $html, 120);
  115.         }
  116.         return $html;
  117.     }
  118.     protected function parseLink($link) {
  119.         preg_match("/(?<=code=\")([^\"]+)?/", $link[0], $code);
  120.         $identifier = is_array($code) && array_key_exists(0, $code) ? $code[0] : "0";
  121.         $document = $this->em->getRepository(Document::class)->findOneByCode($identifier);
  122.         if ($document && $document->getPublic()) {
  123.             preg_match("/(?<=domain=\")([^\"]+)?/", $link[0], $domain);
  124.             preg_match("/(?<=action=\")([^\"]+)?/", $link[0], $action);
  125.             $text = $url = $download = "";
  126.             if ($action[0] === "download" || !$document->getHasDetail()) { // download document
  127.                 $url = "/dokument/f/" . $document->getSeoId() . $document->getExtension();
  128.                 $text = $document->getName() . $document->getInfo();
  129.                 $download = " download=\"" . $document->getSeoId() . $document->getExtension() . "\"";
  130.             } else { // show detail
  131.                 $url = "/dokument/" . $document->getSeoId();
  132.                 $url .= $domain[0] ? ("#" . $domain[0]) : "";
  133.                 $text = $document->getName();
  134.             }
  135.             $result = "<a href=\"" . $url . "\" data-document-code=\"" . $code[0] . "\" data-document-domain=\"" . $domain[0] . "\" data-document-action=\"" . $action[0] . "\" data-cms=\"document-link\"" . $download . ">" . $text . "</a>";
  136.             return $result;
  137.         }
  138.         return ""; // Returns empty string if document doesnt exist... // return $link[0]; // can return original link string
  139.     }
  140. }