<?php
namespace App\Service;
use App\Entity\SettingsSite;
use App\Entity\Site;
use App\Entity\Theme;
use Symfony\Component\HttpClient\CurlHttpClient;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use App\Entity\Pages;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Serializer\SerializerInterface;
class Sites
{
protected $em;
protected $serializer;
protected $settingsTheme;
public function __construct(EntityManagerInterface $em, SerializerInterface $serializer, Settings $settings)
{
$this->em = $em;
$this->serializer = $serializer;
$this->settingsTheme = $settings;
}
public function create($userID, $host, $alias, $hash, $theme)
{
$this->show($host, $userID);
if (!$this->show($host, $userID)) {
$em = $this->em;
$themes = $em->getRepository(Theme::class)->findBy(array('id' => $theme));
$site = new Site();
$site->setUsercliclishop($userID);
$site->setHashCatalog($hash);
$site->setAlias($alias);
$site->setHost($host);
$site->setDateCreate(new \DateTime());
$site->setStatus(true);
$site->setTheme($themes[0]);
$em->persist($site);
$this->settingsTheme->create($themes[0], $site);
$idSite = $site->getId();
$em->flush();
return array('id' => $idSite,
'text' => 'ok');
} else {
return array('text' => 'Уже существует');
}
}
public function edit($idSite, $userID, $hash, $alias, $host, $idTheme)
{
$em = $this->em;
$site = $em->getRepository(Site::class)->findBy(array('id' => $idSite));
$themes = $em->getRepository(Theme::class)->findBy(array('id' => $idTheme));
$site[0]->setUsercliclishop($userID);
$site[0]->setHashCatalog($hash);
$site[0]->setAlias($alias);
$site[0]->setHost($host);
$site[0]->setDateCreate(new \DateTime());
$site[0]->setStatus(true);
$site[0]->setTheme($themes[0]);
if (!$this->settingsTheme->show($themes[0]->getId(), $idSite)) {
$this->settingsTheme->create($themes[0], $site[0]);
}
$em->persist($site[0]);
$em->flush();
return array('id' => $idSite,
'text' => 'ok');
}
//Все сайты пользователя
public function showAll($idUser)
{
$em = $this->em;
if ($idUser == 2){
$sites = $em->getRepository(Site::class)->findAll();
}
else{
$sites = $em->getRepository(Site::class)->findBy(array('usercliclishop' => $idUser));
}
return $sites;
}
//Все сайты в системе
public function showAllFull()
{
$em = $this->em;
$sites = $em->getRepository(Site::class)->findAll();
return $sites;
}
public function showIdSite($idSite)
{
$em = $this->em;
$site = $em->getRepository(Site::class)->findBy(array('id' => $idSite));
return $site;
}
public function showHash($host)
{
$em = $this->em;
$site = $em->getRepository(Site::class)->findBy(array('host' => $host));
return $site[0];
}
public function show($host, $idUser, $idSite = null)
{
$em = $this->em;
if ($host) {
$site = $em->getRepository(Site::class)->findBy(array('host' => $host));
} else {
$site = $em->getRepository(Site::class)->findBy(array('usercliclishop' => $idUser, 'id' => $idSite));
}
return $site;
}
public function delete($idStore)
{
$em = $this->em;
$store = $em->getRepository(Store::class)->findBy(array('id' => $idStore));
$em->remove($store[0]);
$em->flush();
}
public function showSettings($idSite, $idTheme)
{
$em = $this->em;
$settings = $em->getRepository(SettingsSite::class)->findOneBy(array('theme' => $idTheme, 'site' => $idSite));
if ($settings) {
return $settings;
} else {
return [];
}
}
}