<?php
namespace Shopping\Controller;
use App\Controller\Traits\FunctionsController;
use App\Entity\Association;
use App\Entity\Shop;
use Core\Controller\Traits\BaseController;
use Shopping\Controller\Traits\BaseShoppingController;
use Shopping\Entity\Category;
use Shopping\Entity\Feature;
use Shopping\Entity\FeatureGroup;
use Shopping\Entity\Product;
use Shopping\Entity\ProductFeature;
use Shopping\Entity\ProductGabarit;
use Shopping\Entity\ProductRedirect;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use User\Controller\Traits\BaseUserController;
use Symfony\Component\HttpFoundation\Request;
/**
* Controller répondant aux methods "GET" de l'application
*/
class PageController extends AbstractController
{
use BaseController;
use BaseUserController;
use BaseShoppingController;
use FunctionsController;
const SHOPPING_PRODUCTS_PAGINATION = 9;
/**
* Liste de toutes les catégories
*/
public function category(Request $request)
{
// Catégories
$this->vars['categories'] = $this->getDoctrine()->getRepository(Category::class)->findBy([
'parent' => NULL,
'is_enabled' => 1
], ['sortorder' => 'ASC']);
return $this->generateTemplate('@app/shopping/categories', []);
}
/**
* Liste d'une catégorie ( sous-catégories, produits, ... )
*/
public function products(Request $request)
{
// Définition de la page de pagination
$pagination = 1;
$filters = [];
if (isset($this->vars['uri']['parameters']['page'])) {
$pagination = $this->vars['uri']['parameters']['page'];
}
$filters['order'] = '1';
if (isset($this->vars['uri']['parameters']['order'])) {
$filters['order'] = $this->vars['uri']['parameters']['order'];
}
foreach ($this->vars['uri']['parameters'] as $key => $value) {
if ($key != 'page' && $key != 'order') {
$default_state = 1;
$regex = "/([0-9]+)_[0-9]+/";
preg_match($regex, $key, $matches);
if (count($matches) >= 2) {
$key = $matches[1];
}
$item = [
"groupe_id" => $key,
"feature_designation" => $value,
"state" => $default_state,
];
$filters['filters'][] = $item;
}
}
// Récupération des filtres pour la catégorie
$filters['filters_all'] = $this->getFiltersByCategory($this->vars['category']);
// Produits
$categories_list = $this->getCategoriesChildrenById($this->vars['category']);
$this->vars['products'] = $products = $this->getEntitiesPerPagination(Product::class, $pagination, $filters, self::SHOPPING_PRODUCTS_PAGINATION, true, [
'is_enabled' => true,
'categories' => $categories_list,
'parent' => null
]);
// Catégories
$this->vars['categories'] = $this->getDoctrine()->getRepository(Category::class)->findBy([
'parent' => NULL,
'is_enabled' => 1
], ['sortorder' => 'ASC']);
return $this->generateTemplate('@app/shopping/products', []);
}
/**
* Affichage d'un produit
*/
public function product(Request $request)
{
// Vérifie l'existance et la mise en ligne du produit
$redirectionType = $this->vars['product']->getRedirect();
if (is_array($this->vars['product']) || !$this->vars['product']->getIsEnabled() || $redirectionType != ProductRedirect::NO_REDIRECTION) {
$redirectionURL = $this->vars['product']->getRedirectLink();
if ($redirectionURL == '') {
$redirectionURL = '/';
}
switch ($redirectionType) {
case ProductRedirect::NO_REDIRECTION:
return $this->redirectNotFoundException($request);
case ProductRedirect::TEMPORARY_PRODUCT:
return $this->redirect($redirectionURL, 302);
case ProductRedirect::PERMANENT_PRODUCT:
return $this->redirect($redirectionURL, 301);
}
}
$item = new \Shopping\Service\Product\Product($this->getDoctrine(), $this->vars['account']['user']);
$item->createProduct($this->vars['product']->getId());
$related_products = [];
foreach ($this->vars['product']->getRelatedProducts() as $r_object) {
$r_item = new \Shopping\Service\Product\Product($this->getDoctrine(), $this->vars['account']['user']);
$r_item->createProduct($r_object->getId());
$related_products[] = [
'product_manager' => $r_item,
'product' => $r_object
];
}
$results = $this->getDoctrine()->getRepository(Product::class)->findBy([
], [], 8);
foreach ($results as $object) {
$element = new \Shopping\Service\Product\Product($this->getDoctrine(), $this->vars['account']['user']);
$element->createProduct($object->getId());
$this->vars['slider_products'][] = $element;
}
//$this->vars['uri']['alternate'] = [
// 'fr' => $this->vars['uri']['protocol'] . $this->vars['uri']['host'] . '/fr/produits' . $slugs
//];
return $this->generateTemplate('@app/shopping/product', [
'product_manager' => $item,
'product' => $item->getProduct(),
'related_products' => $related_products,
]);
}
/**
* Panier
*/
public function cart(Request $request)
{
$this->vars['slider_products'] = $this->getEntitiesRandom(Product::class);
return $this->generateTemplate('@app/shopping/cart');
}
/**
* Passage de la commande ( adresses, paiements, ... )
*/
public function order(Request $request)
{
// Redirection vers la page du panier s'il est vide
if ($this->vars['cart_manager']->getCartTotalProductsCount() <= 0) {
$uri = $this->vars['uri']['protocol'] . $this->vars['uri']['host'] .
$this->vars['pages']['list']['page_default_5fb7958391f0c']->translate($this->vars['languages']['locale'])->getUri();
return $this->redirect($uri);
}
$this->setDeliveryPrice();
// Définition des montants
$this->updateOrderPrices();
// Récupération des moyens de livraison
$this->vars['delivery_methods'] = [];
$delivery_methods = $this->findDeliveryMethods($request);
foreach ($delivery_methods as $method) {
$this->vars['delivery_methods'][] = [
'method' => $method,
'prices' => $this->getDeliveryPrice($method['id'])
];
}
// Récupération des moyens de paiements
$this->vars['payment_methods'] = [];
$this->vars['payment_methods'] = $this->findPaymentMethods($request);
// Récupération de l'étape de commande
// $this->vars['order_processus_state'] = $this->session->get('order_processus_state');
$this->vars['order_processus_state'] = 1;
//
$shop = $this->vars['order_manager']->getOrder()['delivery_shop'];
if ($shop != null) {
$this->vars['order_delivery_shop'] = $this->getDoctrine()->getRepository(Shop::class)->find($shop);
}
return $this->generateTemplate('@app/shopping/order', [
'disable_navigation_cart' => 1,
'order_processus_state' => 1
]);
}
}