src/FlexApp/DTO/Blog/BlogShowData.php line 647

Open in your IDE?
  1. <?php
  2. namespace FlexApp\DTO\Blog;
  3. use AdmBundle\Helper\Adm;
  4. use DateTime;
  5. use Exception;
  6. use FlexApp\Entity\Exhibition\ExhEntity;
  7. use FlexApp\Helper\MetaHelper;
  8. use FlexApp\Repository\Exhibition\ExhBrandRepository;
  9. use FlexApp\Repository\Exhibition\ExhRepository;
  10. use FlexApp\Service\Meta\MetaManager;
  11. use FlexApp\ValueObject\LocaleVo;
  12. use WebBundle\Entity\LocaleUrlEntity;
  13. use WebBundle\Entity\Publication;
  14. use WebBundle\Helper\App;
  15. use WebBundle\Helper\PathHelper;
  16. use WebBundle\Helper\UserHelper;
  17. use WebBundle\Repository\FilterRepository;
  18. use WebBundle\Repository\LastUrlRepository;
  19. use WebBundle\Repository\LocaleUrlRepository;
  20. use WebBundle\Repository\PublicationRepository;
  21. class BlogShowData
  22. {
  23.     /**
  24.      * @var array
  25.      */
  26.     private $hideCountries;
  27.     /**
  28.      * @var LocaleUrlRepository
  29.      */
  30.     private $repoLocaleUrl;
  31.     /**
  32.      * @var PublicationRepository
  33.      */
  34.     private $repoBlog;
  35.     /**
  36.      * @var ExhRepository
  37.      */
  38.     private $repoExh;
  39.     /**
  40.      * @var MetaManager
  41.      */
  42.     private $metaManager;
  43.     /**
  44.      * @var string
  45.      */
  46.     private $queryUrl;
  47.     /**
  48.      * @var bool
  49.      */
  50.     private $isPreviewStatus;
  51.     /**
  52.      * @var LocaleVo
  53.      */
  54.     private $lcVo;
  55.     /**
  56.      * @var array
  57.      */
  58.     private $data = [];
  59.     /**
  60.      * @var bool
  61.      */
  62.     private $isExhibition false;
  63.     /**
  64.      * @var bool
  65.      */
  66.     private $isShowSite;
  67.     /**
  68.      * @var bool
  69.      */
  70.     private $isEnable;
  71.     /**
  72.      * @var bool
  73.      */
  74.     private $isShow;
  75.     /**
  76.      * @var int
  77.      */
  78.     private $id;
  79.     /**
  80.      * @var string|null
  81.      */
  82.     private $slug;
  83.     /**
  84.      * @var string|null
  85.      */
  86.     private $url;
  87.     /**
  88.      * @var string|null
  89.      */
  90.     private $canonical;
  91.     /**
  92.      * @var string|null
  93.      */
  94.     private $title;
  95.     /**
  96.      * @var string|null
  97.      */
  98.     private $body;
  99.     /**
  100.      * @var array
  101.      */
  102.     private $likes;
  103.     /**
  104.      * @var string|null
  105.      */
  106.     private $preview;
  107.     /**
  108.      * @var string|null
  109.      */
  110.     private $metaTitle;
  111.     /**
  112.      * @var string|null
  113.      */
  114.     private $metaDescription;
  115.     /**
  116.      * @var string|null
  117.      */
  118.     private $metaKeywords;
  119.     /**
  120.      * @var string|null
  121.      */
  122.     private $description;
  123.     /**
  124.      * @var string|null
  125.      */
  126.     private $keywords;
  127.     /**
  128.      * @var string|null
  129.      */
  130.     private $editLink;
  131.     /**
  132.      * @var bool
  133.      */
  134.     private $noindex false;
  135.     /**
  136.      * @var DateTime
  137.      */
  138.     private $publishDate;
  139.     /**
  140.      * @var string|null
  141.      */
  142.     private $unid;
  143.     /**
  144.      * @var array|ExhBrandsData[]
  145.      */
  146.     private $exhBrands;
  147.     /**
  148.      * @var ExhFilterData[]|array
  149.      */
  150.     private $filters;
  151.     /**
  152.      * @var array
  153.      */
  154.     private $filterIDs;
  155.     /**
  156.      * @var array
  157.      */
  158.     private $filtersGroup;
  159.     /**
  160.      * BlogShowData constructor.
  161.      *
  162.      * @param      $queryUrl
  163.      * @param bool $preview
  164.      * @param      $locale
  165.      *
  166.      * @throws Exception
  167.      */
  168.     public function __construct($queryUrl$preview$locale)
  169.     {
  170.         $this->queryUrl $queryUrl;
  171.         $this->isPreviewStatus $preview;
  172.         $this->lcVo = new LocaleVo($locale);
  173.         $this->repoBlog App::getRepository(Publication::class);
  174.         $this->repoExh App::getRepository(ExhEntity::class);
  175.         $this->repoLocaleUrl App::getRepository(LocaleUrlEntity::class);
  176.         $this->data $this->findInBase();
  177.         if ($this->data) {
  178.             $this->metaManager MetaHelper::getManager($this);
  179.         }
  180.         if ($preview) {
  181.             $this->noindex $preview;
  182.         }
  183.     }
  184.     /**
  185.      * @return array
  186.      */
  187.     public function getData(): ?array
  188.     {
  189.         return $this->data;
  190.     }
  191.     public function setData(?array $data)
  192.     {
  193.         $this->data $data;
  194.     }
  195.     /**
  196.      * @return LocaleVo
  197.      */
  198.     public function getLcVo(): LocaleVo
  199.     {
  200.         return $this->lcVo;
  201.     }
  202.     /**
  203.      * обновляем статистику просмотров
  204.      * если флаг Preview, отстутсвует, то пишем в стату.
  205.      *
  206.      */
  207.     public function updView()
  208.     {
  209.         if (!$this->isPreviewStatus()) {
  210.             $pref $this->isExhibition() ? 'exh' 'blog';
  211.             $nameSessionView UserHelper::getInstance()->getToken() . '-' $pref $this->getId();
  212.             if (!isset($_SESSION[$nameSessionView])) {
  213.                 if ($this->isExhibition()) {
  214.                     $this->repoExh->updView($this->getId());
  215.                 } else {
  216.                     $this->repoBlog->updView($this->getId());
  217.                 }
  218.                 $_SESSION[$nameSessionView] = 1;
  219.             }
  220.         }
  221.     }
  222.     /**
  223.      * @return string|null
  224.      *
  225.      * @throws Exception
  226.      */
  227.     public function findNewSlug()
  228.     {
  229.         /** @var $repoLastUrl LastUrlRepository */
  230.         $repoLastUrl App::getRepository('WebBundle:LastUrlEntity');
  231.         return $repoLastUrl->getActualSlugBlogsOrExh($this->queryUrl$this->lcVo->getCode());
  232.     }
  233.     /**
  234.      * получаем из базы данные о блоге.
  235.      *
  236.      * @return array|null
  237.      */
  238.     private function findInBase()
  239.     {
  240.         $blog $this->repoBlog->getBlogByUrlForDTO($this->queryUrl);
  241.         if (!$blog) {
  242.             $blog $this->repoExh->getBlogByUrlForDTO($this->queryUrl);
  243.             $this->isExhibition true;
  244.         }
  245.         return $blog;
  246.     }
  247.     /**
  248.      * @return bool
  249.      */
  250.     public function isPreviewStatus(): bool
  251.     {
  252.         return $this->isPreviewStatus;
  253.     }
  254.     /**
  255.      * @return bool
  256.      */
  257.     public function isExhibition(): bool
  258.     {
  259.         return $this->isExhibition;
  260.     }
  261.     /**
  262.      * @return bool
  263.      */
  264.     public function isEnable(): bool
  265.     {
  266.         if (null == $this->isEnable) {
  267.             $this->isEnable $this->data['enable'];
  268.         }
  269.         return $this->isEnable;
  270.     }
  271.     /**
  272.      * @return int
  273.      */
  274.     public function getId(): int
  275.     {
  276.         if (null == $this->id) {
  277.             $this->id $this->data['id'];
  278.         }
  279.         return $this->id;
  280.     }
  281.     /**
  282.      * @return bool
  283.      */
  284.     public function isShow(): bool
  285.     {
  286.         $lc $this->lcVo->getCode();
  287.         if (null == $this->isShow) {
  288.             $isShow false;
  289.             if (isset($this->data['statusShow'][$lc])) {
  290.                 $isShow $this->data['statusShow'][$lc];
  291.             }
  292.             $this->isShow $isShow;
  293.         }
  294.         return $this->isShow;
  295.     }
  296.     /**
  297.      * @param null $slug
  298.      *
  299.      * @return string|null
  300.      *
  301.      * @throws Exception
  302.      */
  303.     public function getUrl($slug null): ?string
  304.     {
  305.         if (null == $this->url) {
  306.             $slug $slug $slug $this->getSlug();
  307.             $this->url App::generateUrl('app_publication_single', ['id' => $slug]);
  308.         }
  309.         return $this->url;
  310.     }
  311.     /**
  312.      * @return string|null
  313.      */
  314.     public function getSlug(): ?string
  315.     {
  316.         $lc $this->lcVo->getCode();
  317.         if (null == $this->slug) {
  318.             $slug null;
  319.             if (!empty($this->data['url'][$lc])) {
  320.                 $slug $this->data['url'][$lc];
  321.             }
  322.             $this->slug $slug;
  323.         }
  324.         return $this->slug;
  325.     }
  326.     /**
  327.      * @return string|null
  328.      *
  329.      * @throws Exception
  330.      */
  331.     public function getCanonical(): ?string
  332.     {
  333.         if (null == $this->canonical) {
  334.             $this->canonical $this->getUrl();
  335.         }
  336.         return $this->canonical;
  337.     }
  338.     /**
  339.      * @return string|null
  340.      */
  341.     public function getTitle(): ?string
  342.     {
  343.         $lc $this->lcVo->getCode();
  344.         if (null == $this->title) {
  345.             $title null;
  346.             if (!empty($this->data['title'][$lc])) {
  347.                 $title $this->data['title'][$lc];
  348.             }
  349.             $this->title $title;
  350.         }
  351.         return $this->title;
  352.     }
  353.     /**
  354.      * @return string|null
  355.      *
  356.      * @throws Exception
  357.      */
  358.     public function getBody(): ?string
  359.     {
  360.         $lc $this->lcVo->getCode();
  361.         if (null == $this->body) {
  362.             $body null;
  363.             if (!empty($this->data['body'][$lc])) {
  364.                 $body $this->data['body'][$lc];
  365.             } elseif (!empty($this->data['text'][$lc])) {
  366.                 $body $this->data['text'][$lc];
  367.             }
  368.             if ($body) {
  369.                 // для пасхи
  370.                 if (in_array($this->getId(), ['2202'])) {
  371.                     $body App::getContainer()->get('app.service.publication')->parsePashaText($body);
  372.                 }
  373.                 // для рождества
  374.                 if (in_array($this->getId(), ['2195'])) {
  375.                     $body App::getContainer()->get('app.service.publication')->parseChristmasText($body);
  376.                 }
  377.             }
  378.             $this->body $body;
  379.         }
  380.         return $this->body;
  381.     }
  382.     public function setBody($body)
  383.     {
  384.         $this->body $body;
  385.     }
  386.     /**
  387.      * @return bool
  388.      *
  389.      * @throws Exception
  390.      */
  391.     public function isShowSite(): bool
  392.     {
  393.         if (null === $this->isShowSite) {
  394.             // если флаг Preview, то разрешаем просмотр
  395.             if ($this->isPreviewStatus()) {
  396.                 $isShow true;
  397.                 if (!App::isRole('ROLE_ADMIN')) {
  398.                     $isShow false;
  399.                 }
  400.             } else {
  401.                 $isShow $this->isEnable() ? true false;
  402.                 if ($isShow) {
  403.                     $isShow $this->isShow() ? true false;
  404.                 }
  405.                 if ($isShow) {
  406.                     $isShow $this->getTitle() ? true false;
  407.                 }
  408.                 if ($isShow) {
  409.                     $isShow $this->getBody() ? true false;
  410.                 }
  411.                 if ($isShow) {
  412.                     if ($hideCountries $this->getHideCountries()) {
  413.                         $cc App::getCurCountry();
  414.                         $isShow in_array($cc$hideCountries) ? false true;
  415.                     }
  416.                 }
  417.             }
  418.             $this->isShowSite $isShow;
  419.         }
  420.         return $this->isShowSite;
  421.     }
  422.     /**
  423.      * @return array
  424.      */
  425.     public function getHideCountries(): array
  426.     {
  427.         if (null == $this->hideCountries) {
  428.             $hideCountries = [];
  429.             if (!empty($this->data['hideCountries'])) {
  430.                 $hideCountries $this->data['hideCountries'];
  431.             }
  432.             $this->hideCountries $hideCountries;
  433.         }
  434.         return $this->hideCountries;
  435.     }
  436.     /**
  437.      * @return array
  438.      */
  439.     public function getLikes(): array
  440.     {
  441.         if (null == $this->likes) {
  442.             $likes = [];
  443.             if (!empty($this->data['likes'])) {
  444.                 $likes $this->data['likes'];
  445.             }
  446.             $this->likes $likes;
  447.         }
  448.         return $this->likes;
  449.     }
  450.     /**
  451.      * формируем полную ссылку на превью.
  452.      *
  453.      * @return string|null
  454.      */
  455.     public function getPreview(): ?string
  456.     {
  457.         if (null == $this->preview) {
  458.             $lc $this->lcVo->getCode();
  459.             $preview null;
  460.             if (!empty($this->data['preview'][$lc])) {
  461.                 $preview $this->data['preview'][$lc];
  462.             } else {
  463.                 if (!empty($this->data['preview']['default'])) {
  464.                     $preview $this->data['preview']['default'];
  465.                 }
  466.             }
  467.             if ($preview) {
  468.                 $preview PathHelper::pathGenerate('publication', ['id' => $this->getId(), 'preview' => $preview]);
  469.             }
  470.             $this->preview $preview;
  471.         }
  472.         return $this->preview;
  473.     }
  474.     /**
  475.      * @return string|null
  476.      */
  477.     public function getDescription(): ?string
  478.     {
  479.         $lc $this->lcVo->getCode();
  480.         if (null == $this->description) {
  481.             $description null;
  482.             if (!empty($this->data['description'][$lc])) {
  483.                 $description $this->data['description'][$lc];
  484.             }
  485.             $this->description $description;
  486.         }
  487.         return $this->description;
  488.     }
  489.     /**
  490.      * @return string|null
  491.      */
  492.     public function getKeywords(): ?string
  493.     {
  494.         $lc $this->lcVo->getCode();
  495.         if (null == $this->keywords) {
  496.             $keywords null;
  497.             if (!empty($this->data['keywords'][$lc])) {
  498.                 $keywords $this->data['keywords'][$lc];
  499.             }
  500.             $this->keywords $keywords;
  501.         }
  502.         return $this->keywords;
  503.     }
  504.     /**
  505.      * @return string|null
  506.      * @throws Exception
  507.      */
  508.     public function getMetaTitle(): ?string
  509.     {
  510.         if (null == $this->metaTitle) {
  511.             $this->metaTitle $this->metaManager->getTitle();
  512.         }
  513.         return $this->metaTitle;
  514.     }
  515.     /**
  516.      * @return string|null
  517.      * @throws Exception
  518.      */
  519.     public function getMetaDescription(): ?string
  520.     {
  521.         if (null == $this->metaDescription) {
  522.             $this->metaDescription $this->metaManager->getDescription();
  523.         }
  524.         return $this->metaDescription;
  525.     }
  526.     /**
  527.      * @return string|null
  528.      */
  529.     public function getMetaKeywords(): ?string
  530.     {
  531.         if (null == $this->metaKeywords) {
  532.             $this->metaKeywords $this->metaManager->getKeywords();
  533.         }
  534.         return $this->metaKeywords;
  535.     }
  536.     /**
  537.      * @return string|null
  538.      */
  539.     public function getEditLink(): ?string
  540.     {
  541.         if (null == $this->editLink) {
  542.             $link null;
  543.             if (Adm::isGranted('ROLE_ADMIN')) {
  544.                 if ($this->isExhibition()) {
  545.                     $link Adm::linkEdit('adm.exh.edit'$this->getId());
  546.                 } else {
  547.                     $link Adm::linkEdit('adm.blog.edit'$this->getId());
  548.                 }
  549.             }
  550.             $this->editLink $link;
  551.         }
  552.         return $this->editLink;
  553.     }
  554.     /**
  555.      * @return bool
  556.      */
  557.     public function isNoindex(): bool
  558.     {
  559.         return $this->noindex;
  560.     }
  561.     /**
  562.      * @return DateTime
  563.      */
  564.     public function getPublishDate(): DateTime
  565.     {
  566.         if (null == $this->publishDate) {
  567.             $date null;
  568.             if ($this->isExhibition()) {
  569.                 $date $this->data['createdAt'];
  570.             } else {
  571.                 $date $this->data['publishDate'];
  572.             }
  573.             $this->publishDate $date;
  574.         }
  575.         return $this->publishDate;
  576.     }
  577.     /**
  578.      * @return string|null
  579.      */
  580.     public function getUnid(): ?string
  581.     {
  582.         if (null == $this->keywords) {
  583.             $unid null;
  584.             if (!empty($this->data['unid'])) {
  585.                 $unid $this->data['unid'];
  586.             }
  587.             $this->unid $unid;
  588.         }
  589.         return $this->unid;
  590.     }
  591.     /**
  592.      * @return ExhBrandsData[]|array
  593.      *
  594.      * @throws Exception
  595.      */
  596.     public function getExhBrands()
  597.     {
  598.         if (null == $this->exhBrands) {
  599.             $list = [];
  600.             $sort = [];
  601.             if (!empty($this->data['exhBrands'])) {
  602.                 $ids array_column($this->data['exhBrands'], 'id');
  603.                 /** @var ExhBrandRepository $repoExhBrand */
  604.                 $repoExhBrand App::getRepository('FlexApp\Entity\Exhibition\ExhBrandEntity');
  605.                 $dataBrands $repoExhBrand->getBlogsByUrlForDTO($ids);
  606.                 foreach ($dataBrands as $dataBrand) {
  607.                     $brandData = new ExhBrandsData($dataBrand$this);
  608.                     if ($brandData->isShowSite()) {
  609.                         $sort[] = $brandData->getName();
  610.                         $list[] = $brandData;
  611.                     }
  612.                 }
  613.             }
  614.             // сортировка по имени.
  615.             // ---------------------------
  616.             $_list = [];
  617.             if ($list) {
  618.                 sort($sort);
  619.                 $sort array_flip($sort);
  620.                 //App::dump($sort);
  621.                 foreach ($list as $brandData) {
  622.                     $idx $sort[$brandData->getName()];
  623.                     $_list[$idx] = $brandData;
  624.                 }
  625.             }
  626.             ksort($_list);
  627.             $list $_list;
  628.             // ---------------------------
  629.             $this->exhBrands $list;
  630.         }
  631.         return $this->exhBrands;
  632.     }
  633.     /**
  634.      * @return array
  635.      *
  636.      * @throws Exception
  637.      */
  638.     public function getFilterIDs()
  639.     {
  640.         if (null == $this->filterIDs) {
  641.             $brands $this->getExhBrands();
  642.             $fids = [];
  643.             foreach ($brands as $brand) {
  644.                 $_fids $brand->getFilterIDs();
  645.                 if ($_fids) {
  646.                     $fids array_merge($fids$_fids);
  647.                     $fids array_unique($fids);
  648.                 }
  649.             }
  650.             $this->filterIDs $fids;
  651.         }
  652.         return $this->filterIDs;
  653.     }
  654.     /**
  655.      * @return ExhFilterData[]|array
  656.      *
  657.      * @throws Exception
  658.      */
  659.     public function getFilters()
  660.     {
  661.         if (null == $this->filters) {
  662.             $fids $this->getFilterIDs();
  663.             /** @var FilterRepository $repoFilter */
  664.             $repoFilter App::getRepository('WebBundle:FilterEntity');
  665.             $filters $repoFilter->getForExhDTO($fids$this->getLcVo()->getCode());
  666.             if ($filters) {
  667.                 // сортироуем по имени
  668.                 $filterSort array_column($filters'name');
  669.                 array_multisort($filterSortSORT_ASC$filters);
  670.                 foreach ($filters as $i => $filter) {
  671.                     $filters[$i] = new ExhFilterData($filter$this);
  672.                 }
  673.             }
  674.             $this->filters $filters;
  675.         }
  676.         return $this->filters;
  677.     }
  678.     /**
  679.      * @return array
  680.      *
  681.      * @throws Exception
  682.      */
  683.     public function getListFiltersByGroup()
  684.     {
  685.         if (null == $this->filtersGroup) {
  686.             $filters $this->getFilters();
  687.             $list = [];
  688.             foreach ($filters as $i => $filter) {
  689.                 $grName $filter->getGroupName();
  690.                 $grAltName $filter->getGroupAltName();
  691.                 // перемещаем гранит и вид в отдельную группу
  692.                 if ('granite' == $grAltName or 'type' == $grAltName) {
  693.                     $grName 'other';
  694.                 }
  695.                 if (!isset($list[$grName])) {
  696.                     $list[$grName] = [
  697.                         'name'    => $grName,
  698.                         'altName' => $filter->getGroupAltName(),
  699.                         'filters' => [],
  700.                     ];
  701.                 }
  702.                 $list[$grName]['filters'][] = $filter;
  703.             }
  704.             // сортироуем группы по имени
  705.             $listSort array_column($list'altName');
  706.             array_multisort($listSortSORT_DESC$list);
  707.             // помещаем other вконец массива
  708.             $o $list['other'];
  709.             unset($list['other']);
  710.             $list['other'] = $o;
  711.             // помещаем Крупноформатную в конец массива
  712.             $o $list['other']['filters'][0];
  713.             unset($list['other']['filters'][0]);
  714.             array_push($list['other']['filters'], $o);
  715.             $this->filtersGroup $list;
  716.         }
  717.         return $this->filtersGroup;
  718.     }
  719. }