<?php
namespace App\Controller\Front;
use App\Entity\MarketBulletin;
use App\Entity\Supplier;
use App\Model\ProductFeaturingFactory;
use App\Model\SupplierFactory;
use App\Model\User\User;
use App\Service\MessageMatcherService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
class HomeController extends FrontController
{
/**
* @Route("/", name="malys_front_home")
*/
public function home(
SupplierFactory $supplierFactory,
EntityManagerInterface $em,
RouterInterface $router,
ParameterBagInterface $parameterBag,
User $user,
ProductFeaturingFactory $prodFeaturingFactory,
MessageMatcherService $messageMatcherService
): Response {
$user->fromCurrentUser();
/*
* if last route is login : set cart > orderToModify to null
* if not : do nothing
*/
$request = Request::createFromGlobals();
$referer = $request->headers->get('referer');
if ($referer) {
$refererPathInfo = Request::create($referer)->getPathInfo();
$welcomePath = $router->getRouteCollection()->get('malys_front_welcome')->getPath();
if ($refererPathInfo === $welcomePath) {
$cartEntity = $user->getCurrentCartEntity();
if ($cartEntity !== null) {
$cartEntity->setOrderToModify(null);
$em->flush($cartEntity);
}
}
}
$shop = $user->getCurrentShop();
// Product featuring
$prodFeaturings = $prodFeaturingFactory->currentFeaturings();
// Messages
$messagematches = $messageMatcherService
->setUser($this->getUser())
->getHomeUserMessages();
// Active suppliers
$supplierRepo = $em->getRepository(Supplier::class);
$activeSuppliersEntities = $supplierRepo->search([
'limit' => 999999,
'workingWith' => $shop->getEntity()
]);
$activeSuppliers = [];
$activeSuppliersIds = [];
foreach ($activeSuppliersEntities as $supplierEntity) {
$supplier = $supplierFactory->createModel();
$supplier->setEntity($supplierEntity);
$activeSuppliers[] = $supplier;
$activeSuppliersIds[] = $supplierEntity->getId();
}
// Market info
$marketBulletinRepo = $em->getRepository(MarketBulletin::class);
$bulletin = current($marketBulletinRepo->findAll());
// Suggested suppliers
$suggestedSuppliers = [];
if (!is_null($shop)) {
$suggestedSupplierEntities = $shop->getEntity()->getSuggestedSuppliers();
if ($suggestedSupplierEntities->count() === 0) {
$defaultSuggestedSuppliersIds = $parameterBag->get('default_suggested_suppliers');
// array_diff car on ne veut pas afficher des fournisseurs suggérés avec lesquels on travaille déjà
$suggestedSupplierEntities = $supplierRepo->search(['ids' => array_diff($defaultSuggestedSuppliersIds, $activeSuppliersIds)]);
}
foreach ($suggestedSupplierEntities as $supplierEntity) {
$supplier = $supplierFactory->createModel();
$supplier->setEntity($supplierEntity);
$suggestedSuppliers[] = $supplier;
}
}
return $this->render('front/home/home.html.twig', [
'prodFeaturings' => $prodFeaturings,
'messagematches' => $messagematches,
'activeSuppliers' => $activeSuppliers,
'suggestedSuppliers' => $suggestedSuppliers,
'currentShop' => $shop,
'bulletin' => $bulletin,
]);
}
/**
* @Route("/login_check", name="malys_front_login_check_redir", methods={"GET"})
*/
public function loginCheckGet(): RedirectResponse
{
return $this->redirectToRoute('malys_front_home');
}
/**
* @Route("/resetting/send-email", name="malys_front_resetting_redir", methods={"GET"})
*/
public function resetEmailGet(): RedirectResponse
{
return $this->redirectToRoute('malys_front_home');
}
}