<?php
namespace App\Twig\Components;
use Sulu\Bundle\WebsiteBundle\Navigation\NavigationItem;
use Sulu\Bundle\WebsiteBundle\Twig\Navigation\NavigationTwigExtensionInterface;
use App\Twig\Components\Common\LinkTrait;
use LogicException;
use Sulu\Bundle\SnippetBundle\Twig\SnippetAreaTwigExtension;
use Sulu\Bundle\WebsiteBundle\Twig\Content\ContentTwigExtensionInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Handler for Header component.
*
* @author Michał Wiązek <michal.wiazek@makolab.com>
*/
class Header implements ComponentInterface
{
/** Depth of levels to search in navigation tree. Pages under this number in structure tree will not be returned by native sulu methods. */
private const DEFAULT_DEPTH = 4;
/**
* @var NavigationTwigExtensionInterface
*/
private NavigationTwigExtensionInterface $navigationTwigExtension;
private SnippetAreaTwigExtension $snippetAreaTwigExtension;
private TranslatorInterface $translator;
private ContentTwigExtensionInterface $contentTwigExtension;
/**
* Constructor.
*
* @param NavigationTwigExtensionInterface $navigationMapper
*/
public function __construct(
NavigationTwigExtensionInterface $navigationTwigExtension,
SnippetAreaTwigExtension $snippetAreaTwigExtension,
TranslatorInterface $translator,
ContentTwigExtensionInterface $contentTwigExtension
) {
$this->navigationTwigExtension = $navigationTwigExtension;
$this->snippetAreaTwigExtension = $snippetAreaTwigExtension;
$this->translator = $translator;
$this->contentTwigExtension = $contentTwigExtension;
}
/**
* {@inheritDoc}
*/
public static function getKey(): string
{
return 'header';
}
/**
* Handling data for links attribute.
*/
public function linksAttribute(?array $data)
{
$links = $this->prepareDataByLang($data);
return $links;
}
public function socialMediaAttribute(?array $values = []): array
{
if (empty($values['areaKey'])) {
throw new LogicException('Missing argument arrayKey');
}
$snippet = $this->snippetAreaTwigExtension->loadByArea($values['areaKey']);
$data = [];
if (!$snippet) {
return $data;
}
$links = $snippet && $snippet['content'] ? $snippet['content']['links'] : [];
$heading = $snippet && $snippet['content'] ? $snippet['content']['title'] : '';
$data = ['heading' => $heading, 'links' => []];
foreach ($links as $key => $link) {
$item = [
'id' => $key,
'name' => $link['linkType'],
'url' => $link['url'],
];
$data['links'][] = $item;
}
return $data;
}
/**
* Handling data for selected attribute.
*/
public function selectedAttribute(array $data): int
{
$currentUuid = $data['currentUuid'];
$navigationLinks = $this->prepareData();
$breadcrumbs = $this->navigationTwigExtension->breadcrumbFunction($currentUuid);
/** @var NavigationItem $breadcrumb */
foreach ($breadcrumbs as $breadcrumb) {
$breadcrumbUuid = $breadcrumb->getUuid();
foreach ($navigationLinks as $navigationLink) {
if ($breadcrumbUuid === $navigationLink['uuid']) {
return $navigationLink['id'];
}
}
}
return 0;
}
/**
* Prepare data.
*/
private function prepareData(): array
{
$context = 'main';
$links = [];
foreach (
$this->navigationTwigExtension->treeRootNavigationFunction($context,
self::DEFAULT_DEPTH) as $key => $linkData
) {
$links[] = [
'id' => ++$key,
'uuid' => $linkData['uuid'],
'url' => $linkData['url'],
'name' => $linkData['title'],
];
}
return $links;
}
/**
* Prepare data.
*/
private function prepareDataByLang($data)
{
// $context = 'main';
// $links = [];
//
// if(isset($data['locale'])){
// $mockupLinks = $this->mockupHeader($data['locale']);
// foreach ($this->navigationTwigExtension->treeRootNavigationFunction($context, self::DEFAULT_DEPTH) as $key => $linkData) {
// $links[] = [
// 'id' => $key+1,
// 'uuid' => $linkData['uuid'],
// 'url' => $linkData['url'],
// 'name' => $linkData['title'],
// ];
//
// foreach ($mockupLinks as $link)
// {
// if($links[$key]['name'] == $link['heading'])
// {
// $links[$key]['nested'] = $link['links'];
// }
// }
// }
// }
// else
// {
// foreach ($this->navigationTwigExtension->treeRootNavigationFunction($context, self::DEFAULT_DEPTH) as $key => $linkData) {
// $links[] = [
// 'id' => ++$key,
// 'uuid' => $linkData['uuid'],
// 'url' => $linkData['url'],
// 'name' => $linkData['title'],
// ];
// }
// }
//
//
//
// return $links;
$context = 'main';
$page = array_filter($this->navigationTwigExtension->treeRootNavigationFunction($context, self::DEFAULT_DEPTH), function ($item) {
if (isset($item['url']) && $item['url'] == '/mediabank'){
return true;
}
return false;
});
$links = [];
$snippet = $this->snippetAreaTwigExtension->loadByArea("header", null, $data['locale']);
foreach ($snippet['content']['header'] as $key => $linkData) {
$nested = [];
if (isset($linkData['subItems'])) {
foreach ($linkData['subItems'] as $link) {
$url = !empty($link['page']) ? $data['locale'] . $this->contentTwigExtension->load($link['page'])['content']['url'] : null;
$url = !empty($link['anchor']) ? $url . '#' . $link['anchor'] : $url;
if($this->contentTwigExtension->load($link['page']) !== null){
$nested[] = [
'id' => $key + 1,
'name' => $link['title'],
'url' => $url,
];
}
}
}
$url = !empty($linkData['page']) ? $data['locale'] . $this->contentTwigExtension->load($linkData['page'])['content']['url'] : null;
$url = !empty($linkData['anchor']) ? $url . '#' . $linkData['anchor'] : $url;
$temp_data = [
'id' => $key + 1,
'name' => $linkData['title'],
'nested' => $nested,
'url' => $url
];
if(empty($nested))
{
unset($temp_data['nested']);
}
$links[] = $temp_data;
}
if(!empty($page)) {
$links[] = [
'id' => 111,
'name' => $page[0]['title'],
'url' => $data['locale'] . $page[0]['url'],
];
}
return array_reverse($links);
}
/**
* Return mockup for header navigation. To remove in future.
*
* @return array[]
*/
private function mockupHeader($lang): array
{
return [
[
'id' => 0,
'heading' => $this->translator->trans('navigation.heading.title.products', [], 'translations'),
'url' => '/products',
'links' => [
[
'name' => $this->translator->trans('navigation.heading.title.products.parcel_lockers', [],
'translations'),
'url' => '/products/parcel-lockers',
],
[
'name' => $this->translator->trans('navigation.heading.title.products.conveniq', [],
'translations'),
'url' => '/products/conveniq',
],
[
'name' => $this->translator->trans('navigation.heading.title.products.shelving_systems', [],
'translations'),
'url' => '/products/shelving-systems',
],
[
'name' => $this->translator->trans('navigation.heading.title.products.checkout_counters', [],
'translations'),
'url' => '/products/checkout-counters-evo-seline',
],
[
'name' => $this->translator->trans('navigation.heading.title.products.refrigerators', [],
'translations'),
'url' => '/products/refrigerators',
],
// [
// 'name' => $this->translator->trans('navigation.heading.title.products.self_service', [], 'translations'),
// 'url' => '/'.$lang.'/products/self-service',
// ],
[
'name' => $this->translator->trans('navigation.heading.title.products.warehouse', [],
'translations'),
'url' => '/products/warehouse',
],
],
],
[
'id' => 1,
'heading' => $this->translator->trans('navigation.heading.title.solutions', [], 'translations'),
'links' => [
[
'name' => $this->translator->trans('navigation.heading.title.solutions.design', [],
'translations'),
'url' => '/solutions',
],
[
'name' => $this->translator->trans('navigation.heading.title.solutions.it', [], 'translations'),
'url' => '/solutions',
],
[
'name' => $this->translator->trans('navigation.heading.title.solutions.manufacture', [],
'translations'),
'url' => '/solutions',
],
[
'name' => $this->translator->trans('navigation.heading.title.solutions.consulting', [],
'translations'),
'url' => '/solutions',
],
[
'name' => $this->translator->trans('navigation.heading.title.solutions.post_buy', [],
'translations'),
'url' => '/solutions',
],
],
],
[
'id' => 2,
'heading' => $this->translator->trans('navigation.heading.title.about_us', [], 'translations'),
'url' => '/about-us',
'links' => [
[
'name' => $this->translator->trans('navigation.heading.title.about_us.mision', [],
'translations'),
'url' => '/about-us#mission',
],
[
'name' => $this->translator->trans('navigation.heading.title.about_us.geography', [],
'translations'),
'url' => '/about-us#geography',
],
[
'name' => $this->translator->trans('navigation.heading.title.about_us.worlds', [],
'translations'),
'url' => '/about-us#worlds',
],
[
'name' => $this->translator->trans('navigation.heading.title.about_us.units', [],
'translations'),
'url' => '/about-us#units',
],
[
'name' => $this->translator->trans('navigation.heading.title.about_us.history', [],
'translations'),
'url' => '/about-us#history',
],
[
'name' => $this->translator->trans('navigation.heading.title.about_us.news', [],
'translations'),
'url' => '/about-us#news',
],
[
'name' => $this->translator->trans('navigation.heading.title.career', [], 'translations'),
'url' => '/career',
],
[
'name' => $this->translator->trans('navigation.heading.title.contact', [], 'translations'),
'url' => '/contact',
],
],
],
// [
// 'id' => 3,
// 'heading' => $this->translator->trans('navigation.heading.title.contact', [], 'translations'),
// 'url' => '/contact',
// 'links' => [
// [
// 'name' => $this->translator->trans('navigation.heading.title.contact.offices', [], 'translations'),
// 'url' => '/contact#offices',
// ],
// [
// 'name' => $this->translator->trans('navigation.heading.title.contact.partner', [], 'translations'),
// 'url' => '/contact#contact',
// ],
// [
// 'name' => $this->translator->trans('navigation.heading.title.contact.career', [], 'translations'),
// 'url' => '/contact#contact',
// ],
// [
// 'name' => $this->translator->trans('navigation.heading.title.contact.press', [], 'translations'),
// 'url' => '/contact#press',
// ],
// ],
// ],
];
}
}