<?php
namespace App\Controller;
use App\Entity\Basket;
use App\Entity\Chilidocument;
use App\Entity\Settings;
use App\Entity\Whishlist;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
class MainController extends AbstractController
{
public function getCurrentBasket() {
$basketcookie = getBasketCookie();
$em = $this->getDoctrine()->getManager();
if ($this->getUser()) {
$basket = $em->getRepository(Basket::class)->findBy(['user' => $this->getUser()->getId(), 'is_order' => false]);
} else {
$basket = $em->getRepository(Basket::class)->findBy(['csrftoken' => $basketcookie, 'is_order' => false]);
}
if ($basket) {
$basket = $basket[0];
} else {
$basket = new Basket();
}
// Check for duplicates and clean up
// Make sure basket items is attached to correct basket
return $basket;
}
public function getLeartsHeader() {
$response = $this->forward('App\Controller\BasketController::getNumberOfCurrentBasketItems');
$content = json_decode($response->getContent(),1);
$numbasketitems = $content['num'];
$basketcookie = getBasketCookie();
$whishlist = $this->getDoctrine()->getRepository(Whishlist::class)->findBy(['user' => $this->getUser()]);
$numonwhishlist = count($whishlist);
$folders = $this->getDoctrine()->getRepository(Chilidocument::class)->findBy(['is_folder' => true],['sortorder' => 'asc','name' => 'asc']);
foreach ($folders as $folder) {
$f[$folder->getMother()][] = $folder->getName();
}
return $this->render('theme_learts/header.html.twig', [
'numbasketitems' => $numbasketitems,
'basketcookie' => $basketcookie,
'numonwhishlist' => $numonwhishlist,
'folders' => $f,
]);
}
public function getLeartsOffcanvasMobileMenu() {
$response = $this->forward('App\Controller\BasketController::getNumberOfCurrentBasketItems');
$content = json_decode($response->getContent(),1);
$numbasketitems = $content['num'];
$basketcookie = getBasketCookie();
$whishlist = $this->getDoctrine()->getRepository(Whishlist::class)->findBy(['user' => $this->getUser()]);
$numonwhishlist = count($whishlist);
$folders = $this->getDoctrine()->getRepository(Chilidocument::class)->findBy(['is_folder' => true],['sortorder' => 'asc','name' => 'asc']);
foreach ($folders as $folder) {
$f[$folder->getMother()][] = $folder->getName();
}
return $this->render('theme_learts/offcanvasmobilemenu.html.twig', [
'numbasketitems' => $numbasketitems,
'basketcookie' => $basketcookie,
'numonwhishlist' => $numonwhishlist,
'folders' => $f,
]);
}
public function getLeartsOffcanvasCart() {
$basketcookie = getBasketCookie();
$em = $this->getDoctrine()->getManager();
if ($this->getUser()) {
$basket = $em->getRepository(Basket::class)->findBy(['user' => $this->getUser()->getId(), 'is_order' => false]);
} else {
$basket = $em->getRepository(Basket::class)->findBy(['csrftoken' => $basketcookie, 'is_order' => false]);
}
if ($basket) {
$basket = $basket[0];
} else {
$basket = new Basket();
}
// $current_basket = $this->forward('App\Controller\BasketController::getCurrentBasket');
return $this->render('theme_learts/offcanvascart.html.twig', [
'basket' => $basket,
]);
}
public function getLeartsWhishlist() {
$whishlist = $this->getDoctrine()->getRepository(Whishlist::class)->findBy(['user' => $this->getUser()]);
return $this->render('theme_learts/whishlist.html.twig', [
'whishlist' => $whishlist,
]);
}
public function getLeartsDebugInfo() {
$current_basket = $this->getCurrentBasket();
return $this->render('theme_learts/debuginfo.html.twig', [
'current_basket' => $current_basket,
]);
}
public function getNavbar() {
$response = $this->forward('App\Controller\BasketController::getNumberOfCurrentBasketItems');
$content = json_decode($response->getContent(),1);
$numbasketitems = $content['num'];
$basketcookie = getBasketCookie();
return $this->render('theme_' . config_theme . '/navbar.html.twig', [
'numbasketitems' => $numbasketitems,
'basketcookie' => $basketcookie,
]);
}
public function getBreadcrumb($level) {
return $this->render('theme_' . config_theme . '/breadcrumb.html.twig', [
'level' => explode('/',$level),
]);
}
/**
* @Route("/", name="homepage")
*/
public function homepage(): Response
{
$em = $this->getDoctrine()->getManager();
$chilidocs = $em->getRepository(Chilidocument::class)->findBy(['on_front' => true, 'is_active' => true]);
$favourites = $em->getRepository(Whishlist::class)->findBy(['user' => $this->getUser()]);
$favs = [];
foreach ($favourites as $f) {
array_push($favs,$f->getChilidocument()->getId());
}
return $this->render('theme_' . config_theme . '/index.html.twig', [
'theme' => config_theme,
'chilidocs' => $chilidocs,
'favourites' => $favs,
]);
}
/**
* @Route("/terms", name="termspage")
*/
public function terms(): Response
{
$settings = $this->getDoctrine()->getRepository(Settings::class)->find(1);
return $this->render('theme_' . config_theme . '/terms.html.twig', [
'settings' => $settings,
]);
}
/**
* @Route("/aboutus", name="aboutuspage")
*/
public function aboutuspage(): Response
{
$settings = $this->getDoctrine()->getRepository(Settings::class)->find(1);
return $this->render('theme_' . config_theme . '/aboutus.html.twig', [
'settings' => $settings,
]);
}
/**
* @Route("/setbrowserwidth", name="setbrowserwidth_page")
*/
public function setbrowserwidth(Request $request, Session $session): Response
{
if ($request->getMethod() == 'POST') {
$p = $_POST;
$session->set('browserwidth',$p['width']);
return $this->redirectToRoute('Handlekurv');
}
return $this->render('main/setbrowserwidth.html.twig');
}
}