src/Controller/PageInfo.php line 158

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: dkouril
  5.  * Date: 20.3.2019
  6.  * Time: 14:00
  7.  */
  8. namespace App\Controller;
  9. use App\Controller\Breadcrumb\Breadcrumb;
  10. use App\Controller\Breadcrumb\BreadcrumbItem;
  11. use App\Controller\Navbar\Navbar;
  12. use App\Controller\Navbar\NavbarItem;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use App\Controller\PageInfoModel;
  16. use App\Controller\Objects\Link;
  17. use Twig\Environment;
  18. class PageInfo
  19. {
  20.     /**
  21.      * @var string
  22.      */
  23.     private $title = 'KULTURA A SPORT CHOMUTOV s.r.o.';
  24.     /**
  25.      * @var string
  26.      */
  27.     private $html_title = 'KULTURA A SPORT CHOMUTOV s.r.o.';
  28.     /**
  29.      * @var string
  30.      */
  31.     private $description = 'MĚSTSKÉ DIVADLO | KINO SVĚT | LETNÍ KINO | AQUASVĚT | ROCKNET ARÉNA - ZIMNÍ STADION | MĚSTSKÁ SPORTOVNÍ HALA | ATLETICKÝ STADION | FOTBALOVÝ STADION | IN-LINE DRÁHA | DĚTSKÉ HŘIŠTĚ';
  32.     /**
  33.      * @var Navbar
  34.      */
  35.     private $navbar;
  36.     /**
  37.      * @var Navbar
  38.      */
  39.     private $navbar_onscroll;
  40.     /**
  41.      * @var Breadcrumb
  42.      */
  43.     private $breadcrumb;
  44.     /**
  45.      * @var string[]
  46.      */
  47.     private $activeLinks = [];
  48.     /**
  49.      * @var string
  50.      */
  51.     private $activeLink;
  52.     /**
  53.      * @var Request
  54.      */
  55.     private $request;
  56.     /**
  57.      * @var string
  58.      */
  59.     private $searchedText = '';
  60.     /**
  61.      * @var string
  62.      */
  63.     private $paginationLink = '';
  64.     /**
  65.      * @var bool
  66.      */
  67.     private $fromSeznam = false;
  68.     /**
  69.      * @var bool
  70.      */
  71.     private $secondLevelNavbar = false;
  72.     /**
  73.      * @var int
  74.      */
  75.     private $adid = 1;
  76.     /**
  77.      * @var int
  78.      */
  79.     private $adid_html = 100;
  80.     /**
  81.      * @var array
  82.      */
  83.     private $footbar_links = [];
  84.     /**
  85.      * @var PageInfoModel
  86.      */
  87.     private $model;
  88.     /**
  89.      * @var string
  90.      */
  91.     private $info;
  92.     /**
  93.      * @var string
  94.      */
  95.     private $mobile;
  96.     /**
  97.      * @var bool
  98.      */
  99.     private bool $ipad = false;
  100.     /**
  101.      * PageInfo constructor.
  102.      * @param RequestStack $requestStack
  103.      */
  104.     public function __construct(RequestStack $requestStack, Database $db)
  105.     {
  106.         $this->request = $requestStack->getCurrentRequest();
  107.         if ($this->request !== null) {
  108.             $this->activeLink = $this->request->getPathInfo();
  109.             $this->setActiveLinks();
  110.         }
  111.         $this->model = new PageInfoModel($db);
  112.         $this->breadcrumb = new Breadcrumb();
  113.         $this->breadcrumb->addItem(new BreadcrumbItem('Hlavní stránka', '/'));
  114.         $this->initNavbar();
  115.         $this->footbar_links = $this->model->getFootbarLinks();
  116.         $this->info = $this->model->getInfo($this->request->getRequestUri());
  117.         $this->mobile = self::isMobile();
  118.         if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
  119.             $this->ipad = true;
  120.         }
  121.     }
  122.     /**
  123.      * @return false|int
  124.      */
  125.     public function isMobile() {
  126.         return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
  127.     }
  128.     /**
  129.      * @return void
  130.      */
  131.     private function setActiveLinks(): void
  132.     {
  133.         $this->activeLinks = [];
  134.         $links = explode('/', trim($this->request->getPathInfo(), '/'));
  135.         //fix různých vydání fotra na tripu
  136.         $links = str_replace(['fotr-na-snehu', 'fotr-v-cesku', 'fotr-na-tripu-2021', 'fotr-na-tripu-2022'], 'fotr-na-tripu', $links);
  137.         for ($i = 0, $iMax = count($links); $i < $iMax; $i++) {
  138.             if ($i === 0) {
  139.                 $this->activeLinks[] = '/' . $links[$i];
  140.             } else {
  141.                 $prevLink = $this->activeLinks[$i - 1];
  142.                 $this->activeLinks[] = $prevLink . '/' . $links[$i];
  143.             }
  144.         }
  145.         //fix fotr na tripu a blondyna
  146.         if (in_array('/fotr-na-tripu', $this->activeLinks) && in_array('/fotr-na-tripu/videa', $this->activeLinks)) {
  147.             unset($this->activeLinks[0]);
  148.         }
  149.         else if (in_array('/blondyna-kutil-a-zabijak', $this->activeLinks) && in_array('/blondyna-kutil-a-zabijak/videa', $this->activeLinks)) {
  150.             unset($this->activeLinks[0]);
  151.         }
  152.     }
  153.     /**
  154.      * @param NavbarItem[] $items
  155.      * @param string[] $links
  156.      * @param bool $found
  157.      */
  158.     private function findActiveNavbarItems($items, $links, &$found = false): void
  159.     {
  160.         $found = false;
  161.         foreach ($items as $item) {
  162.             //Porovnám linky
  163.             $index = array_search($item->getLink(), $links);
  164.             //Pokud jsem našel link
  165.             if ($index !== false) {
  166.                 $item->setIsActive(true);
  167.                 if (!$item->isOnlySlidebar()) {
  168.                     $this->addBreadcrumb($item->getName(), $item->getLink());
  169.                 }
  170.                 $found = true;
  171.                 unset($links[$index]);
  172.                 if (count($links) > 0) {
  173.                     //Prohledám ještě subitems
  174.                     $this->findActiveNavbarItems($item->getItems(), $links);
  175.                 }
  176.             } else if (count($item->getItems()) > 0) {
  177.                 //Prohledám subitems
  178.                 $this->findActiveNavbarItems($item->getItems(), $links, $found);
  179.                 //Pokud jsem našel
  180.                 if ($found) {
  181.                     $item->setIsActive(true);
  182.                     break;
  183.                 }
  184.             }
  185.         }
  186.     }
  187.     private function initNavbar(): void
  188.     {
  189.         //normal
  190.         $this->navbar = new Navbar();
  191.         $this->navbar->addItem(new NavbarItem('Aquasvět', '/aquasvet'));
  192.         $this->navbar->addItem(new NavbarItem('Kino Svět', '/kino-svet'));
  193.         $this->navbar->addItem(new NavbarItem('Městské divadlo', '/mestske-divadlo'));
  194.         $this->navbar->addItem(new NavbarItem('Městská sportovní hala', '/mestska-sportovni-hala'));
  195.         $items = [];
  196.         $items[] = new NavbarItem('Rocknet aréna', '/zimni-stadion-rocknet-arena');
  197.         $items[] = new NavbarItem('Tréninková hala', '/zimni-stadion-treninkova-hala');
  198.         $this->navbar->addItem(new NavbarItem('Zimní stadion', '', $items));
  199.         $this->navbar->addItem(new NavbarItem('Fotbalový stadion', '/fotbalovy-stadion'));
  200.         $this->navbar->addItem(new NavbarItem('Atletický stadion', '/atleticky-stadion'));
  201.         $this->navbar->addItem(new NavbarItem('Letní kino', '/letni-kino'));
  202.         $items = [];
  203.         $items[] = new NavbarItem('Hřiště', '/hriste');
  204.         $items[] = new NavbarItem('In-line dráha', '/in-line-draha');
  205.         $items[] = new NavbarItem('Sportovní areál TzŠ', '/sportovni-areal-tomase-ze-stitneho');
  206.         $this->navbar->addItem(new NavbarItem('Další', '', $items));
  207.         $this->findActiveNavbarItems($this->navbar->getItems(), $this->activeLinks);
  208.         //onscroll
  209.         $this->navbar_onscroll = new Navbar();
  210.         $this->navbar_onscroll->addItem(new NavbarItem('Aquasvět', '/aquasvet'));
  211.         $this->navbar_onscroll->addItem(new NavbarItem('Kino Svět', '/kino-svet'));
  212.         $this->navbar_onscroll->addItem(new NavbarItem('Městské divadlo', '/mestske-divadlo'));
  213.         $this->navbar_onscroll->addItem(new NavbarItem('Městská sportovní hala', '/mestska-sportovni-hala'));
  214.         $items = [];
  215.         $items[] = new NavbarItem('Rocknet aréna', '/zimni-stadion-rocknet-arena');
  216.         $items[] = new NavbarItem('Tréninková hala', '/zimni-stadion-treninkova-hala');
  217.         $this->navbar_onscroll->addItem(new NavbarItem('Zimní stadion', '', $items));
  218.         $items = [];
  219.         $items[] = new NavbarItem('Fotbalový stadion', '/fotbalovy-stadion');
  220.         $items[] = new NavbarItem('Atletický stadion', '/atleticky-stadion');
  221.         $items[] = new NavbarItem('Letní kino', '/letni-kino');
  222.         $items[] = new NavbarItem('Hřiště', '/hriste');
  223.         $items[] = new NavbarItem('In-line dráha', '/in-line-draha');
  224.         $items[] = new NavbarItem('Sportovní areál TzŠ', '/sportovni-areal-tomase-ze-stitneho');
  225.         $this->navbar_onscroll->addItem(new NavbarItem('Další', '', $items));
  226.         $this->findActiveNavbarItems($this->navbar_onscroll->getItems(), $this->activeLinks);
  227.         //dump($this->navbar);
  228.     }
  229.     /**
  230.      * @return string
  231.      */
  232.     public function getTitle(): string
  233.     {
  234.         return $this->title;
  235.     }
  236.     /**
  237.      * @param string $title
  238.      * @param string $suffix
  239.      * @return PageInfo
  240.      */
  241.     public function setTitle(string $title, string $suffix = ' | Autosalon TV'): PageInfo
  242.     {
  243.         $this->title = $title . $suffix;
  244.         $this->html_title = $title . $suffix;
  245.         return $this;
  246.     }
  247.     /**
  248.      * @return string
  249.      */
  250.     public function getDescription(): string
  251.     {
  252.         return $this->description;
  253.     }
  254.     /**
  255.      * @param string $description
  256.      * @return PageInfo
  257.      */
  258.     public function setDescription(string $description): PageInfo
  259.     {
  260.         $this->description = $description;
  261.         return $this;
  262.     }
  263.     /**
  264.      * @param string $title
  265.      * @return PageInfo
  266.      */
  267.     public function addToTitle(string $title): PageInfo
  268.     {
  269.         $this->title = $title . '|' . $this->title;
  270.         return $this;
  271.     }
  272.     /**
  273.      * @return Navbar
  274.      */
  275.     public function getNavbar(): Navbar
  276.     {
  277.         return $this->navbar;
  278.     }
  279.     /**
  280.      * @return Navbar
  281.      */
  282.     public function getNavbarOnScroll(): Navbar
  283.     {
  284.         return $this->navbar_onscroll;
  285.     }
  286.     /**
  287.      * @return Breadcrumb
  288.      */
  289.     public function getBreadcrumb(): Breadcrumb
  290.     {
  291.         return $this->breadcrumb;
  292.     }
  293.     /**
  294.      * @param Breadcrumb $breadcrumb
  295.      * @return PageInfo
  296.      */
  297.     public function setBreadcrumb(Breadcrumb $breadcrumb): PageInfo
  298.     {
  299.         $this->breadcrumb = $breadcrumb;
  300.         return $this;
  301.     }
  302.     /**
  303.      * @param string $name
  304.      * @param string $link
  305.      * @return PageInfo
  306.      */
  307.     public function addBreadcrumb(string $name, string $link): PageInfo
  308.     {
  309.         $this->breadcrumb->addItem(new BreadcrumbItem($name, $link));
  310.         return $this;
  311.     }
  312.     /**
  313.      * @param BreadcrumbItem $item
  314.      * @return PageInfo
  315.      */
  316.     public function addBreadcrumbItem(BreadcrumbItem $item): PageInfo
  317.     {
  318.         $this->breadcrumb->addItem($item);
  319.         return $this;
  320.     }
  321.     /**
  322.      * @return string
  323.      */
  324.     public function getActiveLink(): string
  325.     {
  326.         return $this->activeLink;
  327.     }
  328.     /**
  329.      * @return string[]
  330.      */
  331.     public function getActiveLinks(): array
  332.     {
  333.         return $this->activeLinks;
  334.     }
  335.     /**
  336.      * @return Request
  337.      */
  338.     public function getRequest(): Request
  339.     {
  340.         return $this->request;
  341.     }
  342.     /**
  343.      * @param Request $request
  344.      * @return PageInfo
  345.      */
  346.     public function setRequest(Request $request): PageInfo
  347.     {
  348.         $this->request = $request;
  349.         return $this;
  350.     }
  351.     /**
  352.      * @return string
  353.      */
  354.     public function getSearchedText(): string
  355.     {
  356.         return $this->searchedText;
  357.     }
  358.     /**
  359.      * @param string $searchedText
  360.      * @return PageInfo
  361.      */
  362.     public function setSearchedText(string $searchedText): PageInfo
  363.     {
  364.         $this->searchedText = $searchedText;
  365.         return $this;
  366.     }
  367.     /**
  368.      * @return string
  369.      */
  370.     public function getPaginationLink(): string
  371.     {
  372.         return $this->paginationLink;
  373.     }
  374.     /**
  375.      * @param string $paginationLink
  376.      * @return PageInfo
  377.      */
  378.     public function setPaginationLink(string $paginationLink): PageInfo
  379.     {
  380.         $this->paginationLink = $paginationLink;
  381.         return $this;
  382.     }
  383.     /**
  384.      * @return false|string
  385.      */
  386.     public function jsonLDSearchbox()
  387.     {
  388.         $url = getenv('APP_URL');
  389.         $json = [
  390.             '@context' => 'https://schema.org/',
  391.             '@type' => 'WebSite',
  392.             'name' => 'Autosalon TV',
  393.             'url' => $url,
  394.             'potentialAction' => [
  395.                 '@type' => 'SearchAction',
  396.                 'target' => $url . '/hledani?q={search_term_string}',
  397.                 'query-input' => 'required name=search_term_string'
  398.             ]
  399.         ];
  400.         return json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  401.     }
  402.     /**
  403.      * @return string
  404.      */
  405.     public function jsonLDBreadcrumb(): string
  406.     {
  407.         $items = [];
  408.         $pos = 1;
  409.         $url = getenv('APP_URL');
  410.         foreach ($this->breadcrumb->getItems() as $item) {
  411.             $items[] = [
  412.                 '@type' => 'ListItem',
  413.                 'position' => $pos,
  414.                 'name' => $item->getName(),
  415.                 'item' => $url . $item->getLink()
  416.             ];
  417.             $pos++;
  418.         }
  419.         $json = [
  420.             '@context' => 'https://schema.org/',
  421.             '@type' => 'BreadcrumbList',
  422.             'itemListElement' => $items
  423.         ];
  424.         return json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  425.     }
  426.     /**
  427.      * @return int
  428.      */
  429.     public function isFromSeznam(): int
  430.     {
  431.         return (int)$this->fromSeznam;
  432.     }
  433.     /**
  434.      * @return bool
  435.      */
  436.     public function isSecondLevelNavbar(): bool
  437.     {
  438.         return $this->secondLevelNavbar;
  439.     }
  440.     /**
  441.      * @return int
  442.      */
  443.     public function getAdid(): int
  444.     {
  445.         return $this->adid++;
  446.     }
  447.     /**
  448.      * @return int
  449.      */
  450.     public function getAdidHtml(): int
  451.     {
  452.         return $this->adid_html++;
  453.     }
  454.     /**
  455.      * @return mixed
  456.      */
  457.     public function getRobots()
  458.     {
  459.         return getenv('APP_ROBOTS');
  460.     }
  461.     /**
  462.      *
  463.      */
  464.     public function getSASkeywords() {
  465.         $breadcrumb = $this->breadcrumb->getItems();
  466.         $keywords = [];
  467.         if (count($breadcrumb)>1) {
  468.             unset($breadcrumb[0]);
  469.             foreach ($breadcrumb as $item) {
  470.                $keywords[] = system::makeLink($item->getName());
  471.             }
  472.         }
  473.         return implode(',', $keywords);
  474.     }
  475.     /**
  476.      * @return string
  477.      */
  478.     public function getHtmlTitle(): string
  479.     {
  480.         return $this->html_title;
  481.     }
  482.     /**
  483.      * @param string $html_title
  484.      */
  485.     public function setHtmlTitle(string $html_title, string $suffix = ' | Autosalon TV'): PageInfo
  486.     {
  487.         if ($html_title != '') $this->html_title = $html_title.$suffix;
  488.         return $this;
  489.     }
  490.     /**
  491.      * @return array
  492.      */
  493.     public function getFootbarLinks(): array
  494.     {
  495.         return $this->footbar_links;
  496.     }
  497.     /**
  498.      * @return string
  499.      */
  500.     public function getInfo(): string
  501.     {
  502.         return $this->info;
  503.     }
  504.     /**
  505.      * @return false|int|string
  506.      */
  507.     public function getMobile(): bool|int|string
  508.     {
  509.         return $this->mobile;
  510.     }
  511.     /**
  512.      * @param false|int|string $mobile
  513.      */
  514.     public function setMobile(bool|int|string $mobile): void
  515.     {
  516.         $this->mobile = $mobile;
  517.     }
  518.     /**
  519.      * @return bool
  520.      */
  521.     public function isIpad(): bool
  522.     {
  523.         return $this->ipad;
  524.     }
  525. }