<?php
namespace FlexApp\DTO\Blog;
use AdmBundle\Helper\Adm;
use DateTime;
use Exception;
use FlexApp\Entity\Exhibition\ExhEntity;
use FlexApp\Helper\MetaHelper;
use FlexApp\Repository\Exhibition\ExhBrandRepository;
use FlexApp\Repository\Exhibition\ExhRepository;
use FlexApp\Service\Meta\MetaManager;
use FlexApp\ValueObject\LocaleVo;
use WebBundle\Entity\LocaleUrlEntity;
use WebBundle\Entity\Publication;
use WebBundle\Helper\App;
use WebBundle\Helper\PathHelper;
use WebBundle\Helper\UserHelper;
use WebBundle\Repository\FilterRepository;
use WebBundle\Repository\LastUrlRepository;
use WebBundle\Repository\LocaleUrlRepository;
use WebBundle\Repository\PublicationRepository;
class BlogShowData
{
/**
* @var array
*/
private $hideCountries;
/**
* @var LocaleUrlRepository
*/
private $repoLocaleUrl;
/**
* @var PublicationRepository
*/
private $repoBlog;
/**
* @var ExhRepository
*/
private $repoExh;
/**
* @var MetaManager
*/
private $metaManager;
/**
* @var string
*/
private $queryUrl;
/**
* @var bool
*/
private $isPreviewStatus;
/**
* @var LocaleVo
*/
private $lcVo;
/**
* @var array
*/
private $data = [];
/**
* @var bool
*/
private $isExhibition = false;
/**
* @var bool
*/
private $isShowSite;
/**
* @var bool
*/
private $isEnable;
/**
* @var bool
*/
private $isShow;
/**
* @var int
*/
private $id;
/**
* @var string|null
*/
private $slug;
/**
* @var string|null
*/
private $url;
/**
* @var string|null
*/
private $canonical;
/**
* @var string|null
*/
private $title;
/**
* @var string|null
*/
private $body;
/**
* @var array
*/
private $likes;
/**
* @var string|null
*/
private $preview;
/**
* @var string|null
*/
private $metaTitle;
/**
* @var string|null
*/
private $metaDescription;
/**
* @var string|null
*/
private $metaKeywords;
/**
* @var string|null
*/
private $description;
/**
* @var string|null
*/
private $keywords;
/**
* @var string|null
*/
private $editLink;
/**
* @var bool
*/
private $noindex = false;
/**
* @var DateTime
*/
private $publishDate;
/**
* @var string|null
*/
private $unid;
/**
* @var array|ExhBrandsData[]
*/
private $exhBrands;
/**
* @var ExhFilterData[]|array
*/
private $filters;
/**
* @var array
*/
private $filterIDs;
/**
* @var array
*/
private $filtersGroup;
/**
* BlogShowData constructor.
*
* @param $queryUrl
* @param bool $preview
* @param $locale
*
* @throws Exception
*/
public function __construct($queryUrl, $preview, $locale)
{
$this->queryUrl = $queryUrl;
$this->isPreviewStatus = $preview;
$this->lcVo = new LocaleVo($locale);
$this->repoBlog = App::getRepository(Publication::class);
$this->repoExh = App::getRepository(ExhEntity::class);
$this->repoLocaleUrl = App::getRepository(LocaleUrlEntity::class);
$this->data = $this->findInBase();
if ($this->data) {
$this->metaManager = MetaHelper::getManager($this);
}
if ($preview) {
$this->noindex = $preview;
}
}
/**
* @return array
*/
public function getData(): ?array
{
return $this->data;
}
public function setData(?array $data)
{
$this->data = $data;
}
/**
* @return LocaleVo
*/
public function getLcVo(): LocaleVo
{
return $this->lcVo;
}
/**
* обновляем статистику просмотров
* если флаг Preview, отстутсвует, то пишем в стату.
*
*/
public function updView()
{
if (!$this->isPreviewStatus()) {
$pref = $this->isExhibition() ? 'exh' : 'blog';
$nameSessionView = UserHelper::getInstance()->getToken() . '-' . $pref . $this->getId();
if (!isset($_SESSION[$nameSessionView])) {
if ($this->isExhibition()) {
$this->repoExh->updView($this->getId());
} else {
$this->repoBlog->updView($this->getId());
}
$_SESSION[$nameSessionView] = 1;
}
}
}
/**
* @return string|null
*
* @throws Exception
*/
public function findNewSlug()
{
/** @var $repoLastUrl LastUrlRepository */
$repoLastUrl = App::getRepository('WebBundle:LastUrlEntity');
return $repoLastUrl->getActualSlugBlogsOrExh($this->queryUrl, $this->lcVo->getCode());
}
/**
* получаем из базы данные о блоге.
*
* @return array|null
*/
private function findInBase()
{
$blog = $this->repoBlog->getBlogByUrlForDTO($this->queryUrl);
if (!$blog) {
$blog = $this->repoExh->getBlogByUrlForDTO($this->queryUrl);
$this->isExhibition = true;
}
return $blog;
}
/**
* @return bool
*/
public function isPreviewStatus(): bool
{
return $this->isPreviewStatus;
}
/**
* @return bool
*/
public function isExhibition(): bool
{
return $this->isExhibition;
}
/**
* @return bool
*/
public function isEnable(): bool
{
if (null == $this->isEnable) {
$this->isEnable = $this->data['enable'];
}
return $this->isEnable;
}
/**
* @return int
*/
public function getId(): int
{
if (null == $this->id) {
$this->id = $this->data['id'];
}
return $this->id;
}
/**
* @return bool
*/
public function isShow(): bool
{
$lc = $this->lcVo->getCode();
if (null == $this->isShow) {
$isShow = false;
if (isset($this->data['statusShow'][$lc])) {
$isShow = $this->data['statusShow'][$lc];
}
$this->isShow = $isShow;
}
return $this->isShow;
}
/**
* @param null $slug
*
* @return string|null
*
* @throws Exception
*/
public function getUrl($slug = null): ?string
{
if (null == $this->url) {
$slug = $slug ? $slug : $this->getSlug();
$this->url = App::generateUrl('app_publication_single', ['id' => $slug]);
}
return $this->url;
}
/**
* @return string|null
*/
public function getSlug(): ?string
{
$lc = $this->lcVo->getCode();
if (null == $this->slug) {
$slug = null;
if (!empty($this->data['url'][$lc])) {
$slug = $this->data['url'][$lc];
}
$this->slug = $slug;
}
return $this->slug;
}
/**
* @return string|null
*
* @throws Exception
*/
public function getCanonical(): ?string
{
if (null == $this->canonical) {
$this->canonical = $this->getUrl();
}
return $this->canonical;
}
/**
* @return string|null
*/
public function getTitle(): ?string
{
$lc = $this->lcVo->getCode();
if (null == $this->title) {
$title = null;
if (!empty($this->data['title'][$lc])) {
$title = $this->data['title'][$lc];
}
$this->title = $title;
}
return $this->title;
}
/**
* @return string|null
*
* @throws Exception
*/
public function getBody(): ?string
{
$lc = $this->lcVo->getCode();
if (null == $this->body) {
$body = null;
if (!empty($this->data['body'][$lc])) {
$body = $this->data['body'][$lc];
} elseif (!empty($this->data['text'][$lc])) {
$body = $this->data['text'][$lc];
}
if ($body) {
// для пасхи
if (in_array($this->getId(), ['2202'])) {
$body = App::getContainer()->get('app.service.publication')->parsePashaText($body);
}
// для рождества
if (in_array($this->getId(), ['2195'])) {
$body = App::getContainer()->get('app.service.publication')->parseChristmasText($body);
}
}
$this->body = $body;
}
return $this->body;
}
public function setBody($body)
{
$this->body = $body;
}
/**
* @return bool
*
* @throws Exception
*/
public function isShowSite(): bool
{
if (null === $this->isShowSite) {
// если флаг Preview, то разрешаем просмотр
if ($this->isPreviewStatus()) {
$isShow = true;
if (!App::isRole('ROLE_ADMIN')) {
$isShow = false;
}
} else {
$isShow = $this->isEnable() ? true : false;
if ($isShow) {
$isShow = $this->isShow() ? true : false;
}
if ($isShow) {
$isShow = $this->getTitle() ? true : false;
}
if ($isShow) {
$isShow = $this->getBody() ? true : false;
}
if ($isShow) {
if ($hideCountries = $this->getHideCountries()) {
$cc = App::getCurCountry();
$isShow = in_array($cc, $hideCountries) ? false : true;
}
}
}
$this->isShowSite = $isShow;
}
return $this->isShowSite;
}
/**
* @return array
*/
public function getHideCountries(): array
{
if (null == $this->hideCountries) {
$hideCountries = [];
if (!empty($this->data['hideCountries'])) {
$hideCountries = $this->data['hideCountries'];
}
$this->hideCountries = $hideCountries;
}
return $this->hideCountries;
}
/**
* @return array
*/
public function getLikes(): array
{
if (null == $this->likes) {
$likes = [];
if (!empty($this->data['likes'])) {
$likes = $this->data['likes'];
}
$this->likes = $likes;
}
return $this->likes;
}
/**
* формируем полную ссылку на превью.
*
* @return string|null
*/
public function getPreview(): ?string
{
if (null == $this->preview) {
$lc = $this->lcVo->getCode();
$preview = null;
if (!empty($this->data['preview'][$lc])) {
$preview = $this->data['preview'][$lc];
} else {
if (!empty($this->data['preview']['default'])) {
$preview = $this->data['preview']['default'];
}
}
if ($preview) {
$preview = PathHelper::pathGenerate('publication', ['id' => $this->getId(), 'preview' => $preview]);
}
$this->preview = $preview;
}
return $this->preview;
}
/**
* @return string|null
*/
public function getDescription(): ?string
{
$lc = $this->lcVo->getCode();
if (null == $this->description) {
$description = null;
if (!empty($this->data['description'][$lc])) {
$description = $this->data['description'][$lc];
}
$this->description = $description;
}
return $this->description;
}
/**
* @return string|null
*/
public function getKeywords(): ?string
{
$lc = $this->lcVo->getCode();
if (null == $this->keywords) {
$keywords = null;
if (!empty($this->data['keywords'][$lc])) {
$keywords = $this->data['keywords'][$lc];
}
$this->keywords = $keywords;
}
return $this->keywords;
}
/**
* @return string|null
* @throws Exception
*/
public function getMetaTitle(): ?string
{
if (null == $this->metaTitle) {
$this->metaTitle = $this->metaManager->getTitle();
}
return $this->metaTitle;
}
/**
* @return string|null
* @throws Exception
*/
public function getMetaDescription(): ?string
{
if (null == $this->metaDescription) {
$this->metaDescription = $this->metaManager->getDescription();
}
return $this->metaDescription;
}
/**
* @return string|null
*/
public function getMetaKeywords(): ?string
{
if (null == $this->metaKeywords) {
$this->metaKeywords = $this->metaManager->getKeywords();
}
return $this->metaKeywords;
}
/**
* @return string|null
*/
public function getEditLink(): ?string
{
if (null == $this->editLink) {
$link = null;
if (Adm::isGranted('ROLE_ADMIN')) {
if ($this->isExhibition()) {
$link = Adm::linkEdit('adm.exh.edit', $this->getId());
} else {
$link = Adm::linkEdit('adm.blog.edit', $this->getId());
}
}
$this->editLink = $link;
}
return $this->editLink;
}
/**
* @return bool
*/
public function isNoindex(): bool
{
return $this->noindex;
}
/**
* @return DateTime
*/
public function getPublishDate(): DateTime
{
if (null == $this->publishDate) {
$date = null;
if ($this->isExhibition()) {
$date = $this->data['createdAt'];
} else {
$date = $this->data['publishDate'];
}
$this->publishDate = $date;
}
return $this->publishDate;
}
/**
* @return string|null
*/
public function getUnid(): ?string
{
if (null == $this->keywords) {
$unid = null;
if (!empty($this->data['unid'])) {
$unid = $this->data['unid'];
}
$this->unid = $unid;
}
return $this->unid;
}
/**
* @return ExhBrandsData[]|array
*
* @throws Exception
*/
public function getExhBrands()
{
if (null == $this->exhBrands) {
$list = [];
$sort = [];
if (!empty($this->data['exhBrands'])) {
$ids = array_column($this->data['exhBrands'], 'id');
/** @var ExhBrandRepository $repoExhBrand */
$repoExhBrand = App::getRepository('FlexApp\Entity\Exhibition\ExhBrandEntity');
$dataBrands = $repoExhBrand->getBlogsByUrlForDTO($ids);
foreach ($dataBrands as $dataBrand) {
$brandData = new ExhBrandsData($dataBrand, $this);
if ($brandData->isShowSite()) {
$sort[] = $brandData->getName();
$list[] = $brandData;
}
}
}
// сортировка по имени.
// ---------------------------
$_list = [];
if ($list) {
sort($sort);
$sort = array_flip($sort);
//App::dump($sort);
foreach ($list as $brandData) {
$idx = $sort[$brandData->getName()];
$_list[$idx] = $brandData;
}
}
ksort($_list);
$list = $_list;
// ---------------------------
$this->exhBrands = $list;
}
return $this->exhBrands;
}
/**
* @return array
*
* @throws Exception
*/
public function getFilterIDs()
{
if (null == $this->filterIDs) {
$brands = $this->getExhBrands();
$fids = [];
foreach ($brands as $brand) {
$_fids = $brand->getFilterIDs();
if ($_fids) {
$fids = array_merge($fids, $_fids);
$fids = array_unique($fids);
}
}
$this->filterIDs = $fids;
}
return $this->filterIDs;
}
/**
* @return ExhFilterData[]|array
*
* @throws Exception
*/
public function getFilters()
{
if (null == $this->filters) {
$fids = $this->getFilterIDs();
/** @var FilterRepository $repoFilter */
$repoFilter = App::getRepository('WebBundle:FilterEntity');
$filters = $repoFilter->getForExhDTO($fids, $this->getLcVo()->getCode());
if ($filters) {
// сортироуем по имени
$filterSort = array_column($filters, 'name');
array_multisort($filterSort, SORT_ASC, $filters);
foreach ($filters as $i => $filter) {
$filters[$i] = new ExhFilterData($filter, $this);
}
}
$this->filters = $filters;
}
return $this->filters;
}
/**
* @return array
*
* @throws Exception
*/
public function getListFiltersByGroup()
{
if (null == $this->filtersGroup) {
$filters = $this->getFilters();
$list = [];
foreach ($filters as $i => $filter) {
$grName = $filter->getGroupName();
$grAltName = $filter->getGroupAltName();
// перемещаем гранит и вид в отдельную группу
if ('granite' == $grAltName or 'type' == $grAltName) {
$grName = 'other';
}
if (!isset($list[$grName])) {
$list[$grName] = [
'name' => $grName,
'altName' => $filter->getGroupAltName(),
'filters' => [],
];
}
$list[$grName]['filters'][] = $filter;
}
// сортироуем группы по имени
$listSort = array_column($list, 'altName');
array_multisort($listSort, SORT_DESC, $list);
// помещаем other вконец массива
$o = $list['other'];
unset($list['other']);
$list['other'] = $o;
// помещаем Крупноформатную в конец массива
$o = $list['other']['filters'][0];
unset($list['other']['filters'][0]);
array_push($list['other']['filters'], $o);
$this->filtersGroup = $list;
}
return $this->filtersGroup;
}
}