<?php
namespace App\Trinity\WebshopBundle\Routing;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class ExtraLoader extends Loader
{
/**
*
* @var \Doctrine\ORM\EntityManager
*/
protected $em;
/**
* Constructor.
*
* @param \Doctrine\ORM\EntityManager $em the Doctrine Entity Manager
*/
public function __construct(\Doctrine\ORM\EntityManager $em)
{
$this->em = $em;
}
public function load($resource, $type = null) { return $collection; }
public function init($collection){
$collection = $this->loadCategory(null, $collection, []);
return $collection;
}
public function loadCategory($parent = null, $collection, $path = []){
$webshops = $this->em->createQuery('SELECT W FROM TrinityWebshopBundle:Webshop W')->execute();
foreach($webshops as $Webshop){
$WebshopSettings = $Webshop->getSettings();
if (empty($WebshopSettings)) {
continue;
}
if(empty($Webshop->getCmsSettings())){
$CmsSettings = $this->em->createQuery('SELECT S FROM CmsBundle:Settings S')->execute();
$Webshop->setCmsSettings($CmsSettings[0]);
}
if (!empty($Webshop->getCmsSettings()->getBaseUri())) {
$uri = $Webshop->getCmsSettings()->getBaseUri() . '/';
// Custom checkout for baseuri locale config
if(!empty($uri)){
$locale = $Webshop->getLanguage()->getLocale();
$Route = new Route($uri . 'checkout/finish/{hash}');
$Route->setDefaults(array(
'_controller' => 'TrinityWebshopBundle:Checkout:finish',
'hash' => null,
));
$Route->setRequirements(array('webshopid' => '.*?'));
$Route->setMethods(array('POST', 'GET'));
$slugkey = 'checkout_finish-' . $locale; // Internally used for page identification
$collection->add($slugkey, $Route);
$Route = new Route($uri . 'checkout/overview/{webshopid}');
$Route->setDefaults(array(
'_controller' => 'TrinityWebshopBundle:Checkout:overview',
'webshopid' => $Webshop->getId(),
));
$Route->setRequirements(array('webshopid' => '\d+'));
$Route->setMethods(array('POST', 'GET'));
$slugkey = 'checkout_overview-' . $locale; // Internally used for page identification
$collection->add($slugkey, $Route);
$Route = new Route($uri . 'checkout/{webshopid}');
$Route->setDefaults(array(
'_controller' => 'TrinityWebshopBundle:Checkout:index',
'webshopid' => $Webshop->getId(),
));
$Route->setRequirements(array('webshopid' => '\d+'));
$Route->setMethods(array('POST', 'GET'));
$slugkey = 'checkout-' . $locale; // Internally used for page identification
$collection->add($slugkey, $Route);
$Route = new Route($uri . 'feeds/channable.{_format}');
$Route->setDefaults(array(
'_controller' => 'TrinityWebshopBundle:Channable:feed',
'_format' => 'csv',
));
//$Route->setRequirements(array('webshopid' => '\d+'));
//$Route->setMethods(array('POST', 'GET'));
$slugkey = 'feeds_channable-' . $locale; // Internally used for page identification
$collection->add($slugkey, $Route);
}
}
}
$sorted = [];
$categories = $this->em->createQuery('SELECT C FROM TrinityWebshopBundle:Category C ORDER BY C.parent desc, C.position desc')->execute();
foreach($categories as $Category){
$Webshop = $Category->getWebshop();
if(empty($Webshop)){
continue;
}
$WebshopSettings = $Webshop->getSettings();
if (empty($WebshopSettings)) {
continue;
}
$num = count($Category->getParents());
$sorted[$num][] = $Category;
}
krsort($sorted);
foreach($sorted as $depth){
foreach($depth as $Category){
$Webshop = $Category->getWebshop();
if(empty($Webshop)){
continue;
}
$WebshopSettings = $Webshop->getSettings();
if (empty($WebshopSettings)) {
continue;
}
$num = count($Category->getParents());
$sorted[$num][] = $Category;
if (empty($Webshop->getCmsSettings()->getBaseUri())) {
$uri = '';
} else {
$uri = $Webshop->getCmsSettings()->getBaseUri() . '/';
}
/*
* If a page is used as basis for the webshop, we can overwrite the uri.
* Since getFullUrl() already take System baseUrl into account.
*/
if($WebshopSettings->getPage()){
$uri = $WebshopSettings->getPage()->getFullUrl() . '/';
}
$slug = $Category->getUri();
$pageFound = false;
if($Category->getPagePriority()){
// Request page above category, check if page exists
$Page = $this->em->getRepository('CmsBundle:Page')->findOneBySlug($slug);
if($Page && $Page->getEnabled()){
$pageFound = true;
}
}
// Only add baseUrl if baseUri is used. Otherwise the slugs will overwrite other slugs of the same category name
// If categories with the same name are used on sub domain shops, depend on
// the code in FrontController::loadAction() to grab the correct Category for the language.
$locale = '';
if (!empty($Category->getWebshop()->getCmsSettings()->getBaseUri())) {
$locale = $Webshop->getLanguage()->getLocale() . '-';
}
if(!$pageFound){
$slugForKey = str_replace('/', '_', $slug);
if(empty($slugForKey)) { continue; }
$slugkey = 'category-'.$locale . $slugForKey; // Internally used for page identification
$Category->setSlugkey($slugkey);
$this->em->persist($Category);
$Route = new Route($uri . $slug . '/{param1}/{param2}/{param3}');
$Route->setDefaults(array(
'_controller' => 'App\\Trinity\\WebshopBundle\\Controller\\FrontController::loadAction',
// '_controller' => 'TrinityWebshopBundle:Front:load',
'categoryId' => $Category->getId(),
'param1' => null, 'param2' => null, 'param3' => null,
));
$Route->setRequirements(array('param1' => '.*?', 'param2' => '.*?', 'param3' => '.*?'));
$Route->setMethods(array('POST', 'GET'));
$collection->add($slugkey, $Route);
}
/* pelikan specific routing */
$locale = $Webshop->getLanguage()->getLocale();
if ($locale == 'nl' || $locale == 'gb' || $locale == 'ie') {
// @TODO vertalingen?
$Route = new Route($uri . 'product' . '/{param1}/{param2}/{param3}');
$Route->setDefaults(array(
'_controller' => 'App\\Trinity\\WebshopBundle\\Controller\\FrontController::loadAction',
// '_controller' => 'TrinityWebshopBundle:Front:load',
'categoryId' => null,
'param1' => null, 'param2' => null, 'param3' => null,
));
$Route->setRequirements(array('param1' => '.*?', 'param2' => '.*?', 'param3' => '.*?'));
$Route->setMethods(array('POST', 'GET'));
$slugkey = $locale . '-category'; // Internally used for page identification
$collection->add($slugkey, $Route);
}
if ($Webshop->getLanguage()->getLocale() == 'de') {
$Route = new Route($uri . 'produkt' . '/{param1}/{param2}/{param3}');
$Route->setDefaults(array(
'_controller' => 'App\\Trinity\\WebshopBundle\\Controller\\FrontController::loadAction',
// '_controller' => 'TrinityWebshopBundle:Front:load',
'categoryId' => null,
'param1' => null, 'param2' => null, 'param3' => null,
));
$Route->setRequirements(array('param1' => '.*?', 'param2' => '.*?', 'param3' => '.*?'));
$Route->setMethods(array('POST', 'GET'));
$slugkey = $locale . '-category'; // Internally used for page identification
$collection->add($slugkey, $Route);
}
if ($Webshop->getLanguage()->getLocale() == 'fr') {
$Route = new Route($uri . 'produit' . '/{param1}/{param2}/{param3}');
$Route->setDefaults(array(
'_controller' => 'App\\Trinity\\WebshopBundle\\Controller\\FrontController::loadAction',
// '_controller' => 'TrinityWebshopBundle:Front:load',
'categoryId' => null,
'param1' => null, 'param2' => null, 'param3' => null,
));
$Route->setRequirements(array('param1' => '.*?', 'param2' => '.*?', 'param3' => '.*?'));
$Route->setMethods(array('POST', 'GET'));
$slugkey = $locale . '-category'; // Internally used for page identification
$collection->add($slugkey, $Route);
}
}
}
return $collection;
}
public function supports($resource, $type = null) { return 'extra' === $type; }
}