src/Twig/Extension/ConectoExtension.php line 99

Open in your IDE?
  1. <?php
  2. namespace App\Twig\Extension;
  3. use Pimcore\Config;
  4. use Pimcore\Model\Document;
  5. use Pimcore\Navigation\Container;
  6. use Pimcore\Navigation\Builder;
  7. use Symfony\Component\Intl\Countries;
  8. use Twig\Extension\AbstractExtension;
  9. use Twig\TwigFunction;
  10. final class ConectoExtension extends AbstractExtension
  11. {
  12.     /**
  13.      * @var Builder
  14.      */
  15.     private $navigationHelper;
  16.     /**
  17.      * @param Builder $navigationHelper
  18.      */
  19.     public function __construct(Builder $navigationHelper)
  20.     {
  21.         $this->navigationHelper = $navigationHelper;
  22.     }
  23.     /**
  24.      * @return \Twig_Function[]
  25.      */
  26.     public function getFunctions(): array
  27.     {
  28.         return [
  29.             new TwigFunction('date_string', [$this, 'getDate']),
  30.             new TwigFunction('date_in_days', [$this, 'getDateInDays']),
  31.             new TwigFunction('conecto_country_list', [$this, 'buildCountryList']),
  32.             new TwigFunction('conecto_file_get_contents', [$this, 'fileGetContents']),
  33.             new TwigFunction('conecto_build_nav', [$this, 'buildMainNavigation']),
  34.             new TwigFunction('conecto_get_editable', [$this, 'getEditable']),
  35.             new TwigFunction('conecto_get_parent_editable', [$this, 'getParentEditable']),
  36.             new TwigFunction('conecto_get_icons', [$this, 'getIcons']),
  37.             new TwigFunction('conecto_get_areabricks', [$this, 'getAreaBricks']),
  38.             new TwigFunction('conecto_get_thumbnailconf', [$this, 'getThumbnailConf']),
  39.             new TwigFunction('conecto_get_hostname', [$this, 'getHostname']),
  40.         ];
  41.     }
  42.     /**
  43.      * @return string
  44.      */
  45.     public function getDate(): string
  46.     {
  47.         $date = new \DateTimeImmutable();
  48.         return $date->format('Y-m-d');
  49.     }
  50.     /**
  51.      * @param int $days
  52.      * @return string
  53.      */
  54.     public function getDateInDays(int $days = 1): string
  55.     {
  56.         $date = new \DateTime();
  57.         $date->add(new \DateInterval('P'.$days.'D'));
  58.         return $date->format('Y-m-d');
  59.     }
  60.     /**
  61.      * @param $locale
  62.      * @return mixed
  63.      */
  64.     public function buildCountryList($locale)
  65.     {
  66.         return Countries::getNames($locale);
  67.     }
  68.     /**
  69.      * @param string $path
  70.      * @return string
  71.      */
  72.     public function fileGetContents(string $path)
  73.     {
  74.         $fileContent = '';
  75.         if(file_exists(PIMCORE_WEB_ROOT . '/' . $path)) {
  76.             $fileContent = file_get_contents(PIMCORE_WEB_ROOT . '/' . $path);
  77.         }
  78.         return $fileContent;
  79.     }
  80.     /**
  81.      * @param Document $activeDocument
  82.      * @param Document $navigationRootDocument
  83.      * @return Container
  84.      */
  85.     public function buildMainNavigation(Document $activeDocument, Document $navigationRootDocument): Container
  86.     {
  87.         return $this->navigationHelper->getNavigation(
  88.             $activeDocument,
  89.             $navigationRootDocument,
  90.             null,
  91.             function (\Pimcore\Navigation\Page\Document $page, $doc) {
  92.                 if (!$doc instanceof Document) {
  93.                     return;
  94.                 }
  95.                 $page->setCustomSetting("menuAsButton", $doc->getProperty("menu-as-button"));
  96.                 $page->setCustomSetting("navIcon", $doc->getProperty("nav_icon"));
  97.                 return $page;
  98.             }
  99.         );
  100.     }
  101.     /**
  102.      * @param Document $document
  103.      * @param string $editableName
  104.      * @return mixed
  105.      */
  106.     public function getEditable(Document $document, string $editableName)
  107.     {
  108.         return $document->getEditable($editableName);
  109.     }
  110.     /**
  111.      * @param Document $document
  112.      * @param string $editableName
  113.      * @return mixed
  114.      */
  115.     public function getParentEditable(Document $document, string $editableName)
  116.     {
  117.         $parent = $document->getParent();
  118.         if($parent) {
  119.             if( method_exists($parent, 'getEditable') ) {
  120.                 $parentEditable = $parent->getEditable($editableName);
  121.                 if($parentEditable instanceof \Pimcore\Model\Document\Editable\Relation && $parentEditable->getId() > 0) {
  122.                     return $parentEditable;
  123.                 }
  124.             }
  125.             return $this->getParentEditable($parent, $editableName);
  126.         }
  127.         return null;
  128.     }
  129.     /**
  130.      * @return array
  131.      */
  132.     public function getIcons(): array
  133.     {
  134.         return \App\Utility\Icon::getIconData();
  135.     }
  136.     /**
  137.      * Function to automatically get all available area bricks,
  138.      * because this is needed in different templates.
  139.      * Use "disallowed" parameter to remove specific area bricks
  140.      * from the list.
  141.      * The sorting of the area bricks can be defined in the
  142.      * config php files of the area bricks
  143.      *
  144.      * @param array $disallowed disallowed areabricks to remove from list
  145.      * @param array $additional additional areabricks to add to list
  146.      * @return array
  147.      */
  148.     public function getAreabricks(array $disallowed = [], array $additional = []): array
  149.     {
  150.         $areaBricksDir = PIMCORE_PROJECT_ROOT . '/src/Document/Areabrick/';
  151.         $areaBricks = [];
  152.         if(is_dir($areaBricksDir)) {
  153.             $areaBricksFilenames = array_diff(scandir($areaBricksDir), ['.', '..', 'AbstractAreabrick.php']);
  154.             // Get sorting from AreaBrick php file
  155.             foreach($areaBricksFilenames as $filename) {
  156.                 $customSorting = 9999;
  157.                 if(file_exists($areaBricksDir . $filename)) {
  158.                     $fileContent = @file_get_contents($areaBricksDir . $filename);
  159.                     if($fileContent) {
  160.                         $lines = explode(PHP_EOL, $fileContent);
  161.                         $lines = array_map(function($line) {
  162.                             return trim($line);
  163.                         }, $lines);
  164.                         $sortingCommentStart = '/* custom-sorting:';
  165.                         // Icon Path auslesen
  166.                         foreach ($lines as $index => $line) {
  167.                             if (strpos($line, $sortingCommentStart) !== false) {
  168.                                 $customSorting = trim(str_replace([$sortingCommentStart, "*/"], '', $line));
  169.                                 break;
  170.                             }
  171.                         }
  172.                     }
  173.                 }
  174.                 $areaBricks[] = [
  175.                     'sorting' => $customSorting,
  176.                     'name' => strtolower(preg_replace('/([a-zA-Z])(?=[A-Z])/', '$1-', str_replace('.php', '', $filename))),
  177.                 ];
  178.             }
  179.         }
  180.         if($additional && sizeof($additional)) {
  181.             foreach($additional as $areaBrick) {
  182.                 $areaBricks[] = [
  183.                     'sorting' => 999999,
  184.                     'name' => $areaBrick,
  185.                 ];
  186.             }
  187.         }
  188.         uasort($areaBricks, function($a, $b) {
  189.             return $a['sorting'] - $b['sorting'];
  190.         });
  191.         return sizeof($disallowed) > 0 ? array_diff(array_column($areaBricks, 'name'), $disallowed) : array_column($areaBricks, 'name');
  192.     }
  193.     /**
  194.      * Get dynamic thumbnail conf
  195.      *
  196.      * @param string $name name for thumbnail conf
  197.      * @param float $ratio thumbnail ratio - defaults to 3/2
  198.      * @param float $cols cols to calculate image width - defaults to 1
  199.      * @return \Pimcore\Model\Asset\Image\Thumbnail\Config
  200.      */
  201.     public function getThumbnailConf(string $name, float $ratio = 1.5, float $cols = 1): \Pimcore\Model\Asset\Image\Thumbnail\Config
  202.     {
  203.         // thumbnail conf test
  204.         $baseWidth = 1920;
  205.         $minWidth = 380;
  206.         $minHeight = floor($minWidth / $ratio);
  207.         $breakpoints = [
  208.             [
  209.                 'minWidth' => 576,
  210.                 'imgWidth' => 767,
  211.                 'maxCols' => 2
  212.             ],
  213.             [
  214.                 'minWidth' => 768,
  215.                 'imgWidth' => 991,
  216.                 'maxCols' => 3
  217.             ],
  218.             [
  219.                 'minWidth' => 992,
  220.                 'imgWidth' => 1199,
  221.                 'maxCols' => 4
  222.             ],
  223.             [
  224.                 'minWidth' => 1200,
  225.                 'imgWidth' => $baseWidth,
  226.                 'maxCols' => 6
  227.             ]
  228.         ];
  229.         $medias = [];
  230.         $confName = $name . '_' . str_replace('.', '-', round($ratio, 2)) . '_' . str_replace('.', '-', round($cols, 2));
  231.         $conf = new \Pimcore\Model\Asset\Image\Thumbnail\Config();
  232.         $conf->setName($confName);
  233.         $conf->setFormat('SOURCE');
  234.         $conf->setQuality(85);
  235.         $conf->setHighResolution(1);
  236.         $conf->setPreserveColor(false);
  237.         $conf->setPreserveMetaData(false);
  238.         $conf->setRasterizeSVG(false);
  239.         $conf->setDownloadable(false);
  240.         // Default size (smartphones)
  241.         $conf->addItem('cover', [
  242.             "width" => $minWidth,
  243.             "height" => $minHeight,
  244.             "positioning" => "center",
  245.             "forceResize" => true
  246.         ]);
  247.         // Media queries
  248.         for($i = 0; $i < sizeof($breakpoints); $i++) {
  249.             $maxThumbWidth = $breakpoints[$i]['imgWidth'];
  250.             $tmpCols = $cols > $breakpoints[$i]['maxCols'] ? $breakpoints[$i]['maxCols'] : $cols;
  251.             $thumbWidth = floor($maxThumbWidth / $tmpCols);
  252.             $thumbHeight = floor($thumbWidth / $ratio);
  253.             if(isset($breakpoints[$i]['minWidth'])) {
  254.                 $mediaQuery = "(min-width: " . $breakpoints[$i]['minWidth'] . "px)";
  255.                 if (isset($breakpoints[$i + 1]) && is_array($breakpoints[$i + 1])) {
  256.                     $mediaQuery .= "and (max-width: " . $breakpoints[$i + 1]['minWidth'] . "px)";
  257.                 }
  258.             } else if(isset($breakpoints[$i]['maxWidth'])) {
  259.                 $mediaQuery = "(max-width: " . $breakpoints[$i]['maxWidth'] . "px)";
  260.             }
  261.             $medias[$mediaQuery][] = [
  262.                 "method" => "cover",
  263.                 "arguments" => [
  264.                     "width" => $thumbWidth,
  265.                     "height" => $thumbHeight,
  266.                     "positioning" => "center",
  267.                     "forceResize" => true
  268.                 ]
  269.             ];
  270.         }
  271.         $conf->setMedias($medias);
  272.         return $conf;
  273.     }
  274.     /**
  275.      * Gets Hostname
  276.      *
  277.      * @return string
  278.      */
  279.     public function getHostname(): string
  280.     {
  281.         return \Pimcore\Tool::getRequestScheme() . '://' . \Pimcore\Tool::getHostname();
  282.     }
  283. }