<?php
namespace App\Service;
use App\Entity\Order;
use App\Entity\Orderregel;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Factory\AdminContextFactory;
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
class AdminReferrer
{
public AdminContext $adminContext;
public static function new(AdminContext $adminContext): self
{
return (new self())
->setAdminContext($adminContext);;
}
public function setAdminContext(AdminContext $adminContext): self
{
$this->adminContext = $adminContext;
return $this;
}
public function getAdminContext(): AdminContext
{
return $this->adminContext;
}
public function getSession(): Session
{
return $this->getAdminContext()->getRequest()->getSession();;
}
public function setReferrer(string $controllerFqcn, string $referrer = ''): self
{
$session = $this->getSession();
$referrerArray = $session->get('referrer');
//als referrer niet is meegegeven, dan de huidige controller met actions, parameters, etc
if ($referrer == '') {
$referrer = $this->getAdminContext()->getRequest()->getRequestUri();
}
$referrerArray[$controllerFqcn] = $referrer;
$session->set('referrer', $referrerArray); // overschrijf de session referrer
return $this;
}
public function getReferrer(string $controllerFqcn): string
{
$referrer = 'admin';
$session = $this->getSession();
if (is_array($session->get('referrer'))) {
if (array_key_exists($controllerFqcn, $session->get('referrer'))) {
if ($session->get('referrer')) {
$referrer = $session->get('referrer')[$controllerFqcn];
}
};
};
return $referrer;
}
public function addParameters(string $controllerFqcn, array $urlParameters): self
{
$session = $this->getSession();
$referrerArray = $session->get('referrer');
if ($referrerArray !== null) {
$referrer = $referrerArray[$controllerFqcn];
$request = Request::create($referrer);
foreach ($urlParameters as $k => $v) {
$request->query->add([$k => $v]);
}
$request->overrideGlobals();
$nwReferrer = str_replace($request->getSchemeAndHttpHost(), "", $request->getUri());
$this->setReferrer($controllerFqcn, $nwReferrer);
}
return $this;
}
public function removeParameters(string $controllerFqcn, array $urlParameters): self
{
$referrer = $this->getReferrer($controllerFqcn);
$request = Request::create($referrer);
foreach ($urlParameters as $urlParameter) {
$request->query->remove($urlParameter);
}
$request->overrideGlobals();
$nwReferrer = str_replace($request->getSchemeAndHttpHost(), "", $request->getUri());
$this->setReferrer($controllerFqcn, $nwReferrer);
return $this;
}
// globale functies
public function getAllReferrers(): array
{
$allRefs = array();
$session = $this->getSession();
if (is_array($session->get('referrer'))) {
$allRefs = $session->get('referrer');
}
return $allRefs;
}
}