<?php
/**
* Created by PhpStorm.
* User: adv
* Date: 04.12.15
* Time: 20:15
*/
namespace Slivki\Repository;
use Doctrine\ORM\EntityRepository;
use Slivki\Entity\Category;
use Slivki\Entity\Seo;
use Slivki\Util\CommonUtil;
use Slivki\Util\SoftCache;
/**
* @deprecated use \Slivki\Repository\Seo\SeoRepositoryInterface instead
* Move the required functions SeoRepositoryInterface
*/
final class SeoRepository extends EntityRepository
{
const CACHE_EXPIRE_PERIOD = 10 * 60;
const CACHE_NAME = "seoAliases-20";
const CACHE_KEY_PREFIX = 'entity-30-';
const CACHE_KEY_ALIAS = 'alias-19-';
const RESOURCE_URL_OFFER_DETAILS = 'Slivki:Default:details';
const RESOURCE_URL_SALE_DETAILS = 'Slivki:Sale:details';
const RESOURCE_URL_OFFER_CATEGORY = 'Slivki:Default:category';
const RESOURCE_URL_SALE_CATEGORY = 'Slivki:Sale:category';
const RESOURCE_URL_PAGES = 'Slivki:Page:details';
const RESOURCE_URL_CATEGORY_COMMENTS = 'Slivki:Default:commentsByCategory';
const SALE_URL = 'skidki-i-rasprodazhi';
const RESOURCE_URL_CITY = 'Slivki:Default:index';
const RESOURCE_URL_MALL = 'Slivki:Mall:details';
const RESOURCE_URL_SALE_INDEX = 'Slivki:Sale:index';
const RESOURCE_URL_PARTNER_OFFERS_INDEX = 'Slivki:PartnerOffer:index';
const RESOURCE_URL_DIRECTOR_DETAILS = 'Slivki:Director:details';
const RESOURCE_URL_SALE_PRODUCT_DETAILS = 'Slivki:SaleProduct:details';
const RESOURCE_URL_CATEGORY_RATING = 'Slivki:Default:categoryRating';
const FLOWERS_CATEGORY_ID = 867;
/**
* @return \Slivki\Entity\Seo
*/
public function getByAlias($alias, $domain = 'www', $baseDomain = '') {
$softCache = new SoftCache(self::CACHE_NAME);
$cacheKey = self::CACHE_KEY_ALIAS . $domain . '-' . $alias;
$seoCached = $softCache->get($cacheKey);
if (!$seoCached) {
$seoCached = $this->findBy(['domain' => $domain, 'mainAlias' => $alias, 'active' => true], ['ID' => 'desc'], 1);
if (!$seoCached && $domain != 'www' && $alias == '/') {
$seoCached = $this->findBy(['domain' => $domain, 'mainAlias' => '/' . $domain], ['ID' => 'desc'], 1);
}
$seoCached = count($seoCached) > 0 ? $seoCached[0] : false;
if (!$seoCached) {
$seoList = $this->findBy(['mainAlias' => $alias], ['ID' => 'desc']);
if ($seoList) {
foreach ($seoList as $seo) {
if ($seo->isActive()) {
$this->getEntityManager()->detach($seo);
$domain = $seo->getDomain();
$seo->setActive(false);
$seo->setRedirectURL('https://' . $domain . $baseDomain . $seo->getMainAlias());
return $seo;
}
}
}
}
if ($seoCached && !$seoCached->isActive()) {
$seo = $this->getSeoForEntity($seoCached->getResourceURL(), $seoCached->getEntityID());
$seoCached->setRedirectURL($seo->getMainAlias());
}
$softCache->set($cacheKey, $seoCached, self::CACHE_EXPIRE_PERIOD);
}
return $seoCached;
}
public function getByEntity($resourceURL, $entityID) {
$softCache = new SoftCache(self::CACHE_NAME);
$cacheKey = self::CACHE_KEY_PREFIX . $resourceURL . "-" . $entityID;
$seoCached = $softCache->get($cacheKey);
if (!$seoCached) {
$seoCached = $this->getSeoForEntity($resourceURL, $entityID);
$softCache->set($cacheKey, $seoCached, self::CACHE_EXPIRE_PERIOD);
}
return $seoCached;
}
public function setAlias(Seo $seo) {
$mainAlias = trim($seo->getMainAlias());
if ($mainAlias != "") {
if($mainAlias[0] != '/' && !CommonUtil::isBeginsWith($mainAlias, 'http')) {
$mainAlias = '/' . $mainAlias;
}
$seo->setMainAlias($mainAlias);
}
/** @var Seo $seoExisting */
$seoExisting = $this->findOneBy(['mainAlias' => $seo->getMainAlias(), 'active' => true, 'domain' => $seo->getDomain()]);
$entityManager = $this->getEntityManager();
if ($seoExisting) {
if ($seoExisting->getEntityID() != $seo->getEntityID() || $seoExisting->getResourceURL() != $seo->getResourceURL()) {
return false;
} else {
if (!$seoExisting->isEqual($seo)) {
$seoExisting->setTitle($seo->getTitle());
$seoExisting->setMetaTitle($seo->getMetaTitle());
$seoExisting->setMetaDescription($seo->getMetaDescription());
$seoExisting->setMetaKeywords($seo->getMetaKeywords());
$seoExisting->setPageDescription($seo->getPageDescription());
$entityManager->flush($seoExisting);
$this->reloadCacheForEntity($seo->getResourceURL(), $seo->getEntityID());
}
return true;
}
}
$currentSeo = $this->findOneBy(['entityID' => $seo->getEntityID(), 'resourceURL' => $seo->getResourceURL(), 'active' => true]);
if ($currentSeo) {
$currentSeo->setActive(false);
$entityManager->flush($currentSeo);
}
$entityManager->persist($seo);
$entityManager->flush($seo);
$this->reloadCacheForSeo($seo, $currentSeo);
return true;
}
public function reloadCacheForSeo(Seo $newSeo, Seo $oldSeo = null) {
$this->reloadCacheForEntity($newSeo->getResourceURL(), $newSeo->getEntityID(), $oldSeo);
}
public function reloadCacheForEntity($resourceURL, $entityID, Seo $oldSeo = null) {
$seo = $this->getSeoForEntity($resourceURL, $entityID);
$softCache = new SoftCache(self::CACHE_NAME);
$cacheKey = self::CACHE_KEY_PREFIX . $resourceURL . "-" . $entityID;
$softCache->set($cacheKey, $seo, self::CACHE_EXPIRE_PERIOD);
$cacheKey = self::CACHE_KEY_ALIAS . $seo->getMainAlias();
$softCache->set($cacheKey, $seo, self::CACHE_EXPIRE_PERIOD);
if ($oldSeo) {
$softCache->delete(self::CACHE_KEY_ALIAS . $oldSeo->getMainAlias());
}
}
/**
* @deprecated
*/
public function getSeoForEntity($resourceURL, $entityID) {
$criteria = array(
"resourceURL" => $resourceURL,
"entityID" => $entityID,
"active" => true
);
$seo = $this->findOneBy($criteria, ['ID' => 'desc']);
if (!$seo) {
$seo = new Seo();
$seo->setMainAlias("");
}
return $seo;
}
public function getOfferURL($offerID) {
return $this->getSeoForEntity(self::RESOURCE_URL_OFFER_DETAILS, $offerID);
}
public function getCategoryCommentsSeo(Category $category) {
$seo = $this->findOneBy(['resourceURL' => self::RESOURCE_URL_CATEGORY_COMMENTS, 'entityID' => $category->getID(), 'active' => true]);
if ($seo) {
return $seo;
}
$categorySeo = $this->findOneBy(['resourceURL' => [self::RESOURCE_URL_OFFER_CATEGORY, self::RESOURCE_URL_SALE_CATEGORY], 'entityID' => $category->getID(), 'active' => true]);
if (!$categorySeo) {
return false;
}
$categoryName = trim($category->getName());
$seo = new Seo();
$seo->setActive(true);
$seo->setEntityID($category->getID());
$seo->setResourceURL(self::RESOURCE_URL_CATEGORY_COMMENTS);
$seo->setMainAlias('/otzyvy' . $categorySeo->getMainAlias());
$seo->setTitle('Отзывы на ' . $categoryName . ' в Минске');
$seo->setMetaTitle($seo->getTitle());
$seo->setMetaKeywords('"' . $categoryName . '", отзывы');
$seo->setMetaDescription('Отзывы на ' . $categoryName . ' в Минске на Slivki.by. Только реальные отзывы пользователей об услугах и товарах!');
return $seo;
}
public function getCategoryURL(Category $category) {
switch ($category->getDomainObjectID()) {
case Category::OFFER_CATEGORY_ID:
return $this->getByEntity(self::RESOURCE_URL_OFFER_CATEGORY, $category->getID())->getMainAlias();
case Category::SALE_CATEGORY_ID:
return $this->getByEntity(self::RESOURCE_URL_SALE_CATEGORY, $category->getID())->getMainAlias();
}
return '';
}
public function getCityURL($cityID) {
$seo = $this->getByEntity(self::RESOURCE_URL_CITY, $cityID);
return $seo ? $seo->getMainAlias() : false;
}
}