<?php
namespace App\Controller;
use App\Service\Page;
use App\Service\Sites;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpClient\CurlHttpClient;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpClient\HttpClient;
//use Symfony\Component\Routing\Annotation\Route;
class MainController extends AbstractController
{
public function findSitebyURl()
{
}
/**
* @Route("/", name="main")
*/
public function index(Request $request, Sites $sites, Page $page)
{
if ($request->getHost() == "site.cliclishop.ru") {
return $this->redirectToRoute('admin');
} else {
$response2 =$this->checkCookie();
$client = new CurlHttpClient(['verify_peer' => false, 'verify_host' => false]);
$data = array();
$host = $request->getHost();
$infoSite = $sites->showHash($host);
$settingsSite = $sites->showSettings($infoSite->getId(), $infoSite->getTheme()->getId());
$pages = $page->showAllbyId($infoSite->getId());
$response = $client->request('POST', 'http://dev-api-cliclishop-ru-nginx/site/' . $infoSite->getHashCatalog() . '/categories/null', ['json' => $data]);
// $statusCode = $response->getStatusCode();
// if($statusCode === 500){
// return $this->render('error500.html.twig', array(
//
//
// ));
// }
$content = $response->toArray();
// $categories2 = $content2['categories'];
// $j = file_get_contents($this->getParameter('image_directory_server') . $host . '.json'); // в примере все файлы в корне
// $data = json_decode($j);
if ($settingsSite) {
// var_dump($settingsSite->getSettings());
$data = $settingsSite->getSettings();
}
if ($infoSite->getTheme()->getType() == 'Индивудуальный') {
$type = 'individualTheme';
} else if ($infoSite->getTheme()->getType() == 'Общий') {
$type = 'templatesTheme';
} else if ($infoSite->getTheme()->getType() == 'Черновик') {
$type = 'templatesDraft';
}
$dir = $type . '/' . $infoSite->getTheme()->getNamedir() . '/';
$categories = $content['categories'];
$headings = $content['headings'];
$settings = [];
if ($settingsSite) {
$settings = $settingsSite->getSettings();
}
return $this->render($dir . 'main.html.twig', array(
'categories' => $categories,
'headings' => $headings,
'pages' => $pages,
'dir' => $dir,
'custom' => $data,
'products' => $content['products'],
'settings' => $settings
),$response2);
}
}
/**
* @Route("/catalog/{id}", name="catalog")
*/
public function catalog($id, Request $request, Sites $sites, Page $page)
{
$client = new CurlHttpClient(['verify_peer' => false, 'verify_host' => false]);
$data = array();
$host = $request->getHost();
$infoSite = $sites->showHash($host);
$response = $client->request('POST', 'http://dev-api-cliclishop-ru-nginx/site/' . $infoSite->getHashCatalog() . '/categories/' . $id, [
'json' => $data]);
$content = $response->toArray();
// var_dump();
// var_dump($content);
$products = $content['products'];
$categories = $content['categories'];
$pages = $page->showAllbyId($infoSite->getId());
$infoSite = $sites->showHash($request->getHost());
$settingsSite = $sites->showSettings($infoSite->getId(), $infoSite->getTheme()->getId());
if ($infoSite->getTheme()->getType() == 'Индивудуальный') {
$type = 'individualTheme';
} else if ($infoSite->getTheme()->getType() == 'Общий') {
$type = 'templatesTheme';
} else if ($infoSite->getTheme()->getType() == 'Черновик') {
$type = 'templatesDraft';
}
// var_dump($infoSite->getTheme()->getNamedir());
$dir = $type . '/' . $infoSite->getTheme()->getNamedir() . '/';
// $j = file_get_contents($this->getParameter('image_directory_server') . $host . '.json'); // в примере все файлы в корне
// $data = json_decode($j);
if ($settingsSite) {
// var_dump($settingsSite->getSettings());
$data = $settingsSite->getSettings();
}
$settings = [];
if ($settingsSite) {
$settings = $settingsSite->getSettings();
}
return $this->render($dir . 'catalog.html.twig', array(
'products' => $products,
'ActiveCategory' => $id,
'categories' => $categories,
'dir' => $dir,
'pages' => $pages,
'custom' => $data,
'settings' => $settings
));
}
/**
* @Route("/catalog/{id}/product/{idProduct}", name="productItem")
*/
public function productItem($id, $idProduct, Request $request, Sites $sites, Page $page)
{
$client = new CurlHttpClient(['verify_peer' => false, 'verify_host' => false]);
$data = array();
$host = $request->getHost();
$infoSite = $sites->showHash($host);
$settingsSite = $sites->showSettings($infoSite->getId(), $infoSite->getTheme()->getId());
$response = $client->request('POST', 'http://dev-api-cliclishop-ru-nginx/site/' . $infoSite->getHashCatalog() . '/product/' . $idProduct, [
'json' => $data]);
$content = $response->toArray();
$pages = $page->showAllbyId($infoSite->getId());
// var_dump($_COOKIE{'CliCLiShop'});
$products = $content['products'];
$specifications = $content['specifications'];
$reviews = $content['reviews'];
$images = $content['image'];
$attr = $content['attr'];
$categories = $content['categories'];
$hash = $_COOKIE{'CliCLiShop'};
// $j = file_get_contents($this->getParameter('image_directory_server') . $request->getHost() . '.json'); // в примере все файлы в корне
// $data = json_decode($j);
$infoSite = $sites->showHash($request->getHost());
if ($settingsSite) {
// var_dump($settingsSite->getSettings());
$data = $settingsSite->getSettings();
}
$settings = [];
if ($settingsSite) {
$settings = $settingsSite->getSettings();
}
if ($infoSite->getTheme()->getType() == 'Индивудуальный') {
$type = 'individualTheme';
} else if ($infoSite->getTheme()->getType() == 'Общий') {
$type = 'templatesTheme';
} else if ($infoSite->getTheme()->getType() == 'Черновик') {
$type = 'templatesDraft';
}
$dir = $type . '/' . $infoSite->getTheme()->getNamedir() . '/';
return $this->render($dir . 'productItem.html.twig', array(
'products' => $products,
'attr' => $attr,
'images' => $images,
'ActiveCategory' => $id,
'categories' => $categories,
'custom' => $data,
'dir' => $dir,
'pages' => $pages,
'settings' => $settings,
'hash' => $hash,
'specifications' => $specifications,
'reviews' => $reviews,
));
}
/**
* @Route("/basket/product/{id_product}/attr/{id_attr}", name="statusBasketProduct")
*/
public function statusBasketProduct($id_attr, $id_product, Request $request, SerializerInterface $serializer)
{
$em = $this->getDoctrine()->getManager();
$data = $request->getContent();
$hash = $_COOKIE{'CliCLiShop'};
$client = new CurlHttpClient(['verify_peer' => false, 'verify_host' => false]);
$response = $client->request('POST', 'http://dev-api-cliclishop-ru-nginx/site/product/' . $id_product . '/attr/' . $id_attr.'/hash/'.$hash, [
'json' => $data]);
$content = $response->toArray();
$status = $content['status'];
return new JsonResponse($content);
}
/**
* @Route("/theme/test", name="themeTest")
*/
public function themeTest(Request $request, Sites $sites)
{
$client = new CurlHttpClient(['verify_peer' => false, 'verify_host' => false]);
$data = array(
'nameSite' => 'Кафе Венеция'
);
$dir = 'templatesTheme/2_start/';
return $this->render($dir . 'main.html.twig', array(
'custom' => $data,
));
}
/**
* @Route("/addBasket/product/{id}/{idAttr}", name="addBasket")
*/
public function addBasket($id, $idAttr, Sites $sites)
{
$client = new CurlHttpClient(['verify_peer' => false, 'verify_host' => false]);
$data = array(
'hash' => $_COOKIE{'CliCLiShop'},
'id' => $id,
'idAttr' => $idAttr,
);
$response = $client->request('POST', 'http://dev-api-cliclishop-ru-nginx/widget/basket/add/', [
'json' => $data]);
$content = $response->toArray();
return $content['text'];
}
/**
* @Route("/pages", name="pages")
*/
public function pages(Request $request, Sites $sites, Page $page)
{
$client = new CurlHttpClient(['verify_peer' => false, 'verify_host' => false]);
$data = array();
$host = $request->getHost();
$infoSite = $sites->showHash($host);
$settingsSite = $sites->showSettings($infoSite->getId(), $infoSite->getTheme()->getId());
$response = $client->request('POST', 'http://dev-api-cliclishop-ru-nginx/site/' . $infoSite->getHashCatalog() . '/default', [
'json' => $data]);
$content = $response->toArray();
$categories = $content['categories'];
$pages = $page->showAllbyId($infoSite->getId());
$j = file_get_contents($this->getParameter('image_directory_server') . $request->getHost() . '.json'); // в примере все файлы в корне
$data = json_decode($j);
$infoSite = $sites->showHash($request->getHost());
if ($infoSite->getTheme()->getType() == 'Индивудуальный') {
$type = 'individualTheme';
} else if ($infoSite->getTheme()->getType() == 'Общий') {
$type = 'templatesTheme';
} else if ($infoSite->getTheme()->getType() == 'Черновик') {
$type = 'templatesDraft';
}
$dir = $type . '/' . $infoSite->getTheme()->getNamedir() . '/';
return $this->render($dir . 'pages.html.twig', array(
'categories' => $categories,
'custom' => $data,
'pages' => $pages,
'dir' => $dir,
'settings' => $settingsSite
));
}
/**
* @Route("/page/{id}", name="page")
*/
public function page($id, Request $request, Sites $sites, Page $page)
{
$client = new CurlHttpClient(['verify_peer' => false, 'verify_host' => false]);
$data = array();
$host = $request->getHost();
$infoSite = $sites->showHash($host);
$settingsSite = $sites->showSettings($infoSite->getId(), $infoSite->getTheme()->getId());
$response = $client->request('POST', 'http://dev-api-cliclishop-ru-nginx/site/' . $infoSite->getHashCatalog() . '/default', [
'json' => $data]);
$content = $response->toArray();
$categories = $content['categories'];
$pages = $page->showAllbyId($infoSite->getId());
$infoPage = $page->show($id, $infoSite->getUsercliclishop());
$j = file_get_contents($this->getParameter('image_directory_server') . $request->getHost() . '.json'); // в примере все файлы в корне
$data = json_decode($j);
$infoSite = $sites->showHash($request->getHost());
if ($infoSite->getTheme()->getType() == 'Индивудуальный') {
$type = 'individualTheme';
} else if ($infoSite->getTheme()->getType() == 'Общий') {
$type = 'templatesTheme';
} else if ($infoSite->getTheme()->getType() == 'Черновик') {
$type = 'templatesDraft';
}
$dir = $type . '/' . $infoSite->getTheme()->getNamedir() . '/';
return $this->render($dir . 'page.html.twig', array(
'activeId' => $id,
'categories' => $categories,
'custom' => $data,
'pages' => $pages,
'page' => $infoPage[0],
'dir' => $dir,
'settings' => $settingsSite
));
}
/**
* @Route("/basket", name="basket")
*/
public function basket(Request $request, Sites $sites, Page $page)
{
$client = new CurlHttpClient(['verify_peer' => false, 'verify_host' => false]);
$data = array();
$host = $request->getHost();
$infoSite = $sites->showHash($host);
$response = $client->request('POST', 'http://dev-api-cliclishop-ru-nginx/site/' . $infoSite->getHashCatalog() . '/default', [
'json' => $data]);
$content = $response->toArray();
$pages = $page->showAllbyId($infoSite->getId());
$categories = $content['categories'];
$j = file_get_contents($this->getParameter('image_directory_server') . $request->getHost() . '.json'); // в примере все файлы в корне
$data = json_decode($j);
$infoSite = $sites->showHash($request->getHost());
$settingsSite = $sites->showSettings($infoSite->getId(), $infoSite->getTheme()->getId());
if ($infoSite->getTheme()->getType() == 'Индивудуальный') {
$type = 'individualTheme';
} else if ($infoSite->getTheme()->getType() == 'Общий') {
$type = 'templatesTheme';
} else if ($infoSite->getTheme()->getType() == 'Черновик') {
$type = 'templatesDraft';
}
$dir = $type . '/' . $infoSite->getTheme()->getNamedir() . '/';
return $this->render($dir . 'basket.html.twig', array(
'categories' => $categories,
'custom' => $data,
'dir' => $dir,
'pages' => $pages,
'hash' => $infoSite->getHashCatalog(),
'settings' => $settingsSite
));
}
/**
* @Route("/my-order", name="myOrder")
*/
public function myOrder(Request $request, Sites $sites, Page $page)
{
$client = new CurlHttpClient(['verify_peer' => false, 'verify_host' => false]);
$data = array();
$host = $request->getHost();
$infoSite = $sites->showHash($host);
$response = $client->request('POST', 'http://dev-api-cliclishop-ru-nginx/site/' . $infoSite->getHashCatalog() . '/default', [
'json' => $data]);
$content = $response->toArray();
$pages = $page->showAllbyId($infoSite->getId());
$categories = $content['categories'];
$j = file_get_contents($this->getParameter('image_directory_server') . $request->getHost() . '.json'); // в примере все файлы в корне
$data = json_decode($j);
$infoSite = $sites->showHash($request->getHost());
$settingsSite = $sites->showSettings($infoSite->getId(), $infoSite->getTheme()->getId());
if ($infoSite->getTheme()->getType() == 'Индивудуальный') {
$type = 'individualTheme';
} else if ($infoSite->getTheme()->getType() == 'Общий') {
$type = 'templatesTheme';
} else if ($infoSite->getTheme()->getType() == 'Черновик') {
$type = 'templatesDraft';
}
$dir = $type . '/' . $infoSite->getTheme()->getNamedir() . '/';
return $this->render($dir . 'myRecord.html.twig', array(
'categories' => $categories,
'custom' => $data,
'dir' => $dir,
'pages' => $pages,
'hash' => $infoSite->getHashCatalog(),
'settings' => $settingsSite
));
}
/**
* @Route("/about", name="about")
*/
public function about(Request $request, Sites $sites, Page $page)
{
$client = new CurlHttpClient(['verify_peer' => false, 'verify_host' => false]);
$data = array();
$host = $request->getHost();
$infoSite = $sites->showHash($host);
$response = $client->request('POST', 'http://dev-api-cliclishop-ru-nginx/site/' . $infoSite->getHashCatalog() . '/default', [
'json' => $data]);
$content = $response->toArray();
$pages = $page->showAllbyId($infoSite->getId());
$categories = $content['categories'];
$j = file_get_contents($this->getParameter('image_directory_server') . $request->getHost() . '.json'); // в примере все файлы в корне
$data = json_decode($j);
$infoSite = $sites->showHash($request->getHost());
$settingsSite = $sites->showSettings($infoSite->getId(), $infoSite->getTheme()->getId());
if ($infoSite->getTheme()->getType() == 'Индивудуальный') {
$type = 'individualTheme';
} else if ($infoSite->getTheme()->getType() == 'Общий') {
$type = 'templatesTheme';
} else if ($infoSite->getTheme()->getType() == 'Черновик') {
$type = 'templatesDraft';
}
$dir = $type . '/' . $infoSite->getTheme()->getNamedir() . '/';
return $this->render($dir . 'basket.html.twig', array(
'categories' => $categories,
'custom' => $data,
'dir' => $dir,
'pages' => $pages,
'hash' => $infoSite->getHashCatalog(),
'settings' => $settingsSite
));
}
private function checkCookie()
{
$hash = null;
$hash2 = null;
$res = new Response();
if (empty($_COOKIE{'CliCLiShop'})) {
$hash = md5(uniqid());
$cookie = new Cookie(
'CliCLiShop', // Cookie name.
$hash, // Cookie value.
time() + (2 * 365 * 24 * 60 * 60) // Expires 2 years.
, '/',
null,
false,
false
);
$res->headers->setCookie($cookie);
}
if (empty($_COOKIE{'_CliCLiShop'})) {
if ($hash == null ){
$hash2 = $_COOKIE{'CliCLiShop'};
}
$cookie2 = new Cookie(
'_CliCLiShop', // Cookie name.
$hash2, // Cookie value.
time() + (2 * 365 * 24 * 60 * 60) // Expires 2 years.
, 'api.cliclishop.ru',
null,
false,
false
);
$res->headers->setCookie($cookie2);
}
if($hash == null and $hash2 = null){
if($_COOKIE{'CliCLiShop'} != $_COOKIE{'_CliCLiShop'}){
$cookie2 = new Cookie(
'_CliCLiShop', // Cookie name.
$_COOKIE{'CliCLiShop'}, // Cookie value.
time() + (2 * 365 * 24 * 60 * 60) // Expires 2 years.
, 'api.cliclishop.ru',
null,
false,
false
);
}
}
return $res;
}
/**
* @Route("/yml", name="yml")
*/
public function yml(Request $request)
{
$client = new CurlHttpClient();
$data = array();
$response = $client->request('POST', 'http://dev-api-cliclishop-ru-nginx/site/' . $request->getHost(), [
'json' => $data]);
$content = $response->toArray();
// var_dump($content['products']);
$products = $content['products'];
$categories = $content['categories'];
$j = file_get_contents($this->getParameter('image_directory_server') . $request->getHost() . '.json'); // в примере все файлы в корне
$data = json_decode($j);
return $this->render('basket.html.twig', array(
'categories' => $categories,
'custom' => $data,
));
}
}