src/Twig/Components/Header.php line 142

Open in your IDE?
  1. <?php
  2. namespace App\Twig\Components;
  3. use Sulu\Bundle\WebsiteBundle\Navigation\NavigationItem;
  4. use Sulu\Bundle\WebsiteBundle\Twig\Navigation\NavigationTwigExtensionInterface;
  5. use App\Twig\Components\Common\LinkTrait;
  6. use LogicException;
  7. use Sulu\Bundle\SnippetBundle\Twig\SnippetAreaTwigExtension;
  8. use Sulu\Bundle\WebsiteBundle\Twig\Content\ContentTwigExtensionInterface;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. /**
  11.  * Handler for Header component.
  12.  *
  13.  * @author Michał Wiązek <michal.wiazek@makolab.com>
  14.  */
  15. class Header implements ComponentInterface
  16. {
  17.     /** Depth of levels to search in navigation tree. Pages under this number in structure tree will not be returned by native sulu methods. */
  18.     private const DEFAULT_DEPTH 4;
  19.     /**
  20.      * @var NavigationTwigExtensionInterface
  21.      */
  22.     private NavigationTwigExtensionInterface $navigationTwigExtension;
  23.     private SnippetAreaTwigExtension $snippetAreaTwigExtension;
  24.     private TranslatorInterface $translator;
  25.     private ContentTwigExtensionInterface $contentTwigExtension;
  26.     /**
  27.      * Constructor.
  28.      *
  29.      * @param NavigationTwigExtensionInterface $navigationMapper
  30.      */
  31.     public function __construct(
  32.         NavigationTwigExtensionInterface $navigationTwigExtension,
  33.         SnippetAreaTwigExtension $snippetAreaTwigExtension,
  34.         TranslatorInterface $translator,
  35.         ContentTwigExtensionInterface $contentTwigExtension
  36.     ) {
  37.         $this->navigationTwigExtension $navigationTwigExtension;
  38.         $this->snippetAreaTwigExtension $snippetAreaTwigExtension;
  39.         $this->translator $translator;
  40.         $this->contentTwigExtension $contentTwigExtension;
  41.     }
  42.     /**
  43.      * {@inheritDoc}
  44.      */
  45.     public static function getKey(): string
  46.     {
  47.         return 'header';
  48.     }
  49.     /**
  50.      * Handling data for links attribute.
  51.      */
  52.     public function linksAttribute(?array $data)
  53.     {
  54.         $links $this->prepareDataByLang($data);
  55.         return $links;
  56.     }
  57.     public function socialMediaAttribute(?array $values = []): array
  58.     {
  59.         if (empty($values['areaKey'])) {
  60.             throw new LogicException('Missing argument arrayKey');
  61.         }
  62.         $snippet $this->snippetAreaTwigExtension->loadByArea($values['areaKey']);
  63.         $data = [];
  64.         if (!$snippet) {
  65.             return $data;
  66.         }
  67.         $links $snippet && $snippet['content'] ? $snippet['content']['links'] : [];
  68.         $heading $snippet && $snippet['content'] ? $snippet['content']['title'] : '';
  69.         $data = ['heading' => $heading'links' => []];
  70.         foreach ($links as $key => $link) {
  71.             $item = [
  72.                 'id'   => $key,
  73.                 'name' => $link['linkType'],
  74.                 'url'  => $link['url'],
  75.             ];
  76.             $data['links'][] = $item;
  77.         }
  78.         return $data;
  79.     }
  80.     /**
  81.      * Handling data for selected attribute.
  82.      */
  83.     public function selectedAttribute(array $data): int
  84.     {
  85.         $currentUuid $data['currentUuid'];
  86.         $navigationLinks $this->prepareData();
  87.         $breadcrumbs $this->navigationTwigExtension->breadcrumbFunction($currentUuid);
  88.         /** @var NavigationItem $breadcrumb */
  89.         foreach ($breadcrumbs as $breadcrumb) {
  90.             $breadcrumbUuid $breadcrumb->getUuid();
  91.             foreach ($navigationLinks as $navigationLink) {
  92.                 if ($breadcrumbUuid === $navigationLink['uuid']) {
  93.                     return $navigationLink['id'];
  94.                 }
  95.             }
  96.         }
  97.         return 0;
  98.     }
  99.     /**
  100.      * Prepare data.
  101.      */
  102.     private function prepareData(): array
  103.     {
  104.         $context 'main';
  105.         $links = [];
  106.         foreach (
  107.             $this->navigationTwigExtension->treeRootNavigationFunction($context,
  108.                 self::DEFAULT_DEPTH) as $key => $linkData
  109.         ) {
  110.             $links[] = [
  111.                 'id'   => ++$key,
  112.                 'uuid' => $linkData['uuid'],
  113.                 'url'  => $linkData['url'],
  114.                 'name' => $linkData['title'],
  115.             ];
  116.         }
  117.         return $links;
  118.     }
  119.     /**
  120.      * Prepare data.
  121.      */
  122.     private function prepareDataByLang($data)
  123.     {
  124. //        $context = 'main';
  125. //        $links = [];
  126. //
  127. //        if(isset($data['locale'])){
  128. //            $mockupLinks = $this->mockupHeader($data['locale']);
  129. //            foreach ($this->navigationTwigExtension->treeRootNavigationFunction($context, self::DEFAULT_DEPTH) as $key => $linkData) {
  130. //                $links[] = [
  131. //                    'id' => $key+1,
  132. //                    'uuid' => $linkData['uuid'],
  133. //                    'url' => $linkData['url'],
  134. //                    'name' => $linkData['title'],
  135. //                ];
  136. //
  137. //                foreach ($mockupLinks as $link)
  138. //                {
  139. //                    if($links[$key]['name'] == $link['heading'])
  140. //                    {
  141. //                        $links[$key]['nested'] = $link['links'];
  142. //                    }
  143. //                }
  144. //            }
  145. //        }
  146. //        else
  147. //        {
  148. //            foreach ($this->navigationTwigExtension->treeRootNavigationFunction($context, self::DEFAULT_DEPTH) as $key => $linkData) {
  149. //                $links[] = [
  150. //                    'id' => ++$key,
  151. //                    'uuid' => $linkData['uuid'],
  152. //                    'url' => $linkData['url'],
  153. //                    'name' => $linkData['title'],
  154. //                ];
  155. //            }
  156. //        }
  157. //
  158. //
  159. //
  160. //        return $links;
  161.         $context 'main';
  162.         $page array_filter($this->navigationTwigExtension->treeRootNavigationFunction($contextself::DEFAULT_DEPTH), function ($item) {
  163.             if (isset($item['url']) && $item['url'] == '/mediabank'){
  164.                 return true;
  165.             }
  166.             return false;
  167.         });
  168.         $links = [];
  169.         $snippet $this->snippetAreaTwigExtension->loadByArea("header"null$data['locale']);
  170.         foreach ($snippet['content']['header'] as $key => $linkData) {
  171.             $nested = [];
  172.             if (isset($linkData['subItems'])) {
  173.                 foreach ($linkData['subItems'] as $link) {
  174.                     $url = !empty($link['page']) ? $data['locale'] . $this->contentTwigExtension->load($link['page'])['content']['url'] : null;
  175.                     $url = !empty($link['anchor']) ? $url '#' $link['anchor'] : $url;
  176.                     if($this->contentTwigExtension->load($link['page']) !== null){
  177.                         $nested[] = [
  178.                             'id'   => $key 1,
  179.                             'name' => $link['title'],
  180.                             'url'  => $url,
  181.                         ];
  182.                     }
  183.                 }
  184.             }
  185.             $url = !empty($linkData['page']) ? $data['locale'] . $this->contentTwigExtension->load($linkData['page'])['content']['url'] : null;
  186.             $url = !empty($linkData['anchor']) ? $url '#' $linkData['anchor'] : $url;
  187.             $temp_data = [
  188.                 'id'      => $key 1,
  189.                 'name' => $linkData['title'],
  190.                 'nested'   => $nested,
  191.                 'url' => $url
  192.             ];
  193.             if(empty($nested))
  194.             {
  195.                 unset($temp_data['nested']);
  196.             }
  197.             $links[] = $temp_data;
  198.         }
  199.         if(!empty($page)) {
  200.             $links[] = [
  201.                 'id' => 111,
  202.                 'name' => $page[0]['title'],
  203.                 'url' =>  $data['locale'] . $page[0]['url'],
  204.             ];
  205.         }
  206.         return array_reverse($links);
  207.     }
  208.     /**
  209.      * Return mockup for header navigation. To remove in future.
  210.      *
  211.      * @return array[]
  212.      */
  213.     private function mockupHeader($lang): array
  214.     {
  215.         return [
  216.             [
  217.                 'id'      => 0,
  218.                 'heading' => $this->translator->trans('navigation.heading.title.products', [], 'translations'),
  219.                 'url'     => '/products',
  220.                 'links'   => [
  221.                     [
  222.                         'name' => $this->translator->trans('navigation.heading.title.products.parcel_lockers', [],
  223.                             'translations'),
  224.                         'url'  => '/products/parcel-lockers',
  225.                     ],
  226.                     [
  227.                         'name' => $this->translator->trans('navigation.heading.title.products.conveniq', [],
  228.                             'translations'),
  229.                         'url'  => '/products/conveniq',
  230.                     ],
  231.                     [
  232.                         'name' => $this->translator->trans('navigation.heading.title.products.shelving_systems', [],
  233.                             'translations'),
  234.                         'url'  => '/products/shelving-systems',
  235.                     ],
  236.                     [
  237.                         'name' => $this->translator->trans('navigation.heading.title.products.checkout_counters', [],
  238.                             'translations'),
  239.                         'url'  => '/products/checkout-counters-evo-seline',
  240.                     ],
  241.                     [
  242.                         'name' => $this->translator->trans('navigation.heading.title.products.refrigerators', [],
  243.                             'translations'),
  244.                         'url'  => '/products/refrigerators',
  245.                     ],
  246.                     // [
  247.                     //     'name' => $this->translator->trans('navigation.heading.title.products.self_service', [], 'translations'),
  248.                     //     'url' => '/'.$lang.'/products/self-service',
  249.                     // ],
  250.                     [
  251.                         'name' => $this->translator->trans('navigation.heading.title.products.warehouse', [],
  252.                             'translations'),
  253.                         'url'  => '/products/warehouse',
  254.                     ],
  255.                 ],
  256.             ],
  257.             [
  258.                 'id'      => 1,
  259.                 'heading' => $this->translator->trans('navigation.heading.title.solutions', [], 'translations'),
  260.                 'links'   => [
  261.                     [
  262.                         'name' => $this->translator->trans('navigation.heading.title.solutions.design', [],
  263.                             'translations'),
  264.                         'url'  => '/solutions',
  265.                     ],
  266.                     [
  267.                         'name' => $this->translator->trans('navigation.heading.title.solutions.it', [], 'translations'),
  268.                         'url'  => '/solutions',
  269.                     ],
  270.                     [
  271.                         'name' => $this->translator->trans('navigation.heading.title.solutions.manufacture', [],
  272.                             'translations'),
  273.                         'url'  => '/solutions',
  274.                     ],
  275.                     [
  276.                         'name' => $this->translator->trans('navigation.heading.title.solutions.consulting', [],
  277.                             'translations'),
  278.                         'url'  => '/solutions',
  279.                     ],
  280.                     [
  281.                         'name' => $this->translator->trans('navigation.heading.title.solutions.post_buy', [],
  282.                             'translations'),
  283.                         'url'  => '/solutions',
  284.                     ],
  285.                 ],
  286.             ],
  287.             [
  288.                 'id'      => 2,
  289.                 'heading' => $this->translator->trans('navigation.heading.title.about_us', [], 'translations'),
  290.                 'url'     => '/about-us',
  291.                 'links'   => [
  292.                     [
  293.                         'name' => $this->translator->trans('navigation.heading.title.about_us.mision', [],
  294.                             'translations'),
  295.                         'url'  => '/about-us#mission',
  296.                     ],
  297.                     [
  298.                         'name' => $this->translator->trans('navigation.heading.title.about_us.geography', [],
  299.                             'translations'),
  300.                         'url'  => '/about-us#geography',
  301.                     ],
  302.                     [
  303.                         'name' => $this->translator->trans('navigation.heading.title.about_us.worlds', [],
  304.                             'translations'),
  305.                         'url'  => '/about-us#worlds',
  306.                     ],
  307.                     [
  308.                         'name' => $this->translator->trans('navigation.heading.title.about_us.units', [],
  309.                             'translations'),
  310.                         'url'  => '/about-us#units',
  311.                     ],
  312.                     [
  313.                         'name' => $this->translator->trans('navigation.heading.title.about_us.history', [],
  314.                             'translations'),
  315.                         'url'  => '/about-us#history',
  316.                     ],
  317.                     [
  318.                         'name' => $this->translator->trans('navigation.heading.title.about_us.news', [],
  319.                             'translations'),
  320.                         'url'  => '/about-us#news',
  321.                     ],
  322.                     [
  323.                         'name' => $this->translator->trans('navigation.heading.title.career', [], 'translations'),
  324.                         'url'  => '/career',
  325.                     ],
  326.                     [
  327.                         'name' => $this->translator->trans('navigation.heading.title.contact', [], 'translations'),
  328.                         'url'  => '/contact',
  329.                     ],
  330.                 ],
  331.             ],
  332. //            [
  333. //                'id' => 3,
  334. //                'heading' => $this->translator->trans('navigation.heading.title.contact', [], 'translations'),
  335. //                'url' => '/contact',
  336. //                'links' => [
  337. //                    [
  338. //                        'name' => $this->translator->trans('navigation.heading.title.contact.offices', [], 'translations'),
  339. //                        'url' => '/contact#offices',
  340. //                    ],
  341. //                    [
  342. //                        'name' => $this->translator->trans('navigation.heading.title.contact.partner', [], 'translations'),
  343. //                        'url' => '/contact#contact',
  344. //                    ],
  345. //                    [
  346. //                        'name' => $this->translator->trans('navigation.heading.title.contact.career', [], 'translations'),
  347. //                        'url' => '/contact#contact',
  348. //                    ],
  349. //                    [
  350. //                        'name' => $this->translator->trans('navigation.heading.title.contact.press', [], 'translations'),
  351. //                        'url' => '/contact#press',
  352. //                    ],
  353. //                ],
  354. //            ],
  355.         ];
  356.     }
  357. }