src/Trinity/WebshopBundle/Routing/ExtraLoader.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Trinity\WebshopBundle\Routing;
  3. use Symfony\Component\Config\Loader\Loader;
  4. use Symfony\Component\Routing\Route;
  5. use Symfony\Component\Routing\RouteCollection;
  6. class ExtraLoader extends Loader
  7. {
  8.     /**
  9.      *
  10.      * @var \Doctrine\ORM\EntityManager
  11.      */
  12.     protected $em;
  13.     /**
  14.      * Constructor.
  15.      *
  16.      * @param \Doctrine\ORM\EntityManager $em the Doctrine Entity Manager
  17.      */
  18.     public function __construct(\Doctrine\ORM\EntityManager $em)
  19.     {
  20.         $this->em $em;
  21.     }
  22.     public function load($resource$type null) { return $collection; }
  23.     public function init($collection){
  24.         $collection $this->loadCategory(null$collection, []);
  25.         return $collection;
  26.     }
  27.     public function loadCategory($parent null$collection$path = []){
  28.         $webshops $this->em->createQuery('SELECT W FROM TrinityWebshopBundle:Webshop W')->execute();
  29.         foreach($webshops as $Webshop){
  30.             $WebshopSettings =  $Webshop->getSettings();
  31.             if (empty($WebshopSettings)) {
  32.                 continue;
  33.             }
  34.             if(empty($Webshop->getCmsSettings())){
  35.                 $CmsSettings $this->em->createQuery('SELECT S FROM CmsBundle:Settings S')->execute();
  36.                 $Webshop->setCmsSettings($CmsSettings[0]);
  37.             }
  38.             if (!empty($Webshop->getCmsSettings()->getBaseUri())) {
  39.                 $uri $Webshop->getCmsSettings()->getBaseUri() . '/';
  40.                 // Custom checkout for baseuri locale config
  41.                 if(!empty($uri)){
  42.                     $locale $Webshop->getLanguage()->getLocale();
  43.                     $Route = new Route($uri 'checkout/finish/{hash}');
  44.                     $Route->setDefaults(array(
  45.                         '_controller' => 'TrinityWebshopBundle:Checkout:finish',
  46.                         'hash' => null,
  47.                     ));                    
  48.                     $Route->setRequirements(array('webshopid' => '.*?'));
  49.                     $Route->setMethods(array('POST''GET'));
  50.                     $slugkey 'checkout_finish-' $locale// Internally used for page identification
  51.                     $collection->add($slugkey$Route);
  52.                     $Route = new Route($uri 'checkout/overview/{webshopid}');
  53.                     $Route->setDefaults(array(
  54.                         '_controller' => 'TrinityWebshopBundle:Checkout:overview',
  55.                         'webshopid' => $Webshop->getId(),
  56.                     ));                    
  57.                     $Route->setRequirements(array('webshopid' => '\d+'));
  58.                     $Route->setMethods(array('POST''GET'));
  59.                     $slugkey 'checkout_overview-' $locale// Internally used for page identification
  60.                     $collection->add($slugkey$Route);
  61.                     $Route = new Route($uri 'checkout/{webshopid}');
  62.                     $Route->setDefaults(array(
  63.                         '_controller' => 'TrinityWebshopBundle:Checkout:index',
  64.                         'webshopid' => $Webshop->getId(),
  65.                     ));                    
  66.                     $Route->setRequirements(array('webshopid' => '\d+'));
  67.                     $Route->setMethods(array('POST''GET'));
  68.                     $slugkey 'checkout-' $locale// Internally used for page identification
  69.                     $collection->add($slugkey$Route);
  70.                     $Route = new Route($uri 'feeds/channable.{_format}');
  71.                     $Route->setDefaults(array(
  72.                         '_controller' => 'TrinityWebshopBundle:Channable:feed',
  73.                         '_format' => 'csv',
  74.                     ));                    
  75.                     //$Route->setRequirements(array('webshopid' => '\d+'));
  76.                     //$Route->setMethods(array('POST', 'GET'));
  77.                     $slugkey 'feeds_channable-' $locale// Internally used for page identification
  78.                     $collection->add($slugkey$Route);
  79.                 }
  80.             }
  81.         }
  82.         $sorted = [];
  83.         $categories $this->em->createQuery('SELECT C FROM TrinityWebshopBundle:Category C ORDER BY C.parent desc, C.position desc')->execute();
  84.         foreach($categories as $Category){
  85.             $Webshop $Category->getWebshop();
  86.             if(empty($Webshop)){
  87.                 continue;
  88.             }
  89.             $WebshopSettings =  $Webshop->getSettings();
  90.             if (empty($WebshopSettings)) {
  91.                 continue;
  92.             }
  93.             $num count($Category->getParents());
  94.             $sorted[$num][] = $Category;
  95.         }
  96.         krsort($sorted);
  97.         foreach($sorted as $depth){
  98.             foreach($depth as $Category){
  99.                 $Webshop $Category->getWebshop();
  100.                 if(empty($Webshop)){
  101.                     continue;
  102.                 }
  103.                 $WebshopSettings =  $Webshop->getSettings();
  104.                 if (empty($WebshopSettings)) {
  105.                     continue;
  106.                 }
  107.                 $num count($Category->getParents());
  108.                 $sorted[$num][] = $Category;
  109.                 if (empty($Webshop->getCmsSettings()->getBaseUri())) {
  110.                     $uri '';
  111.                 } else {
  112.                     $uri $Webshop->getCmsSettings()->getBaseUri() . '/';
  113.                 }
  114.                 /*
  115.                  * If a page is used as basis for the webshop, we can overwrite the uri.
  116.                  * Since getFullUrl() already take System baseUrl into account.
  117.                  */
  118.                 if($WebshopSettings->getPage()){
  119.                     $uri $WebshopSettings->getPage()->getFullUrl() . '/';
  120.                 }
  121.                 $slug $Category->getUri();
  122.                 $pageFound false;
  123.                 if($Category->getPagePriority()){
  124.                     // Request page above category, check if page exists
  125.                     $Page $this->em->getRepository('CmsBundle:Page')->findOneBySlug($slug);
  126.                     if($Page && $Page->getEnabled()){
  127.                         $pageFound true;
  128.                     }
  129.                 }
  130.                 // Only add baseUrl if baseUri is used. Otherwise the slugs will overwrite other slugs of the same category name
  131.                 // If categories with the same name are used on sub domain shops, depend on
  132.                 // the code in FrontController::loadAction() to grab the correct Category for the language.
  133.                 $locale '';
  134.                 if (!empty($Category->getWebshop()->getCmsSettings()->getBaseUri())) {
  135.                     $locale $Webshop->getLanguage()->getLocale() . '-';
  136.                 }
  137.                 if(!$pageFound){
  138.                     $slugForKey str_replace('/''_'$slug);
  139.                     if(empty($slugForKey)) { continue; }
  140.                     $slugkey 'category-'.$locale $slugForKey// Internally used for page identification
  141.                     $Category->setSlugkey($slugkey);
  142.                     $this->em->persist($Category);
  143.                     $Route = new Route($uri $slug '/{param1}/{param2}/{param3}');
  144.                     $Route->setDefaults(array(
  145.                         '_controller' => 'App\\Trinity\\WebshopBundle\\Controller\\FrontController::loadAction',
  146.                         // '_controller' => 'TrinityWebshopBundle:Front:load',
  147.                         'categoryId' => $Category->getId(),
  148.                         'param1' => null'param2' => null'param3' => null,
  149.                     ));
  150.                     $Route->setRequirements(array('param1' => '.*?''param2' => '.*?''param3' => '.*?'));
  151.                     $Route->setMethods(array('POST''GET'));
  152.                     $collection->add($slugkey$Route);
  153.                 }
  154.                 /* pelikan specific routing */
  155.                 $locale $Webshop->getLanguage()->getLocale();
  156.                 if ($locale == 'nl' || $locale == 'gb' || $locale == 'ie') {
  157.                     // @TODO vertalingen?
  158.                     $Route = new Route($uri 'product' '/{param1}/{param2}/{param3}');
  159.                     $Route->setDefaults(array(
  160.                         '_controller' => 'App\\Trinity\\WebshopBundle\\Controller\\FrontController::loadAction',
  161.                         // '_controller' => 'TrinityWebshopBundle:Front:load',
  162.                         'categoryId' => null,
  163.                         'param1' => null'param2' => null'param3' => null,
  164.                     ));                    
  165.                     $Route->setRequirements(array('param1' => '.*?''param2' => '.*?''param3' => '.*?'));
  166.                     $Route->setMethods(array('POST''GET'));
  167.                     $slugkey $locale '-category'// Internally used for page identification
  168.                     $collection->add($slugkey$Route);
  169.                 }
  170.                 if ($Webshop->getLanguage()->getLocale() == 'de') {
  171.                     $Route = new Route($uri 'produkt' '/{param1}/{param2}/{param3}');
  172.                     $Route->setDefaults(array(
  173.                         '_controller' => 'App\\Trinity\\WebshopBundle\\Controller\\FrontController::loadAction',
  174.                         // '_controller' => 'TrinityWebshopBundle:Front:load',
  175.                         'categoryId' => null,
  176.                         'param1' => null'param2' => null'param3' => null,
  177.                     ));                    
  178.                     $Route->setRequirements(array('param1' => '.*?''param2' => '.*?''param3' => '.*?'));
  179.                     $Route->setMethods(array('POST''GET'));
  180.                     $slugkey $locale '-category'// Internally used for page identification
  181.                     $collection->add($slugkey$Route);
  182.                 }
  183.                 if ($Webshop->getLanguage()->getLocale() == 'fr') {
  184.                     $Route = new Route($uri 'produit' '/{param1}/{param2}/{param3}');
  185.                     $Route->setDefaults(array(
  186.                         '_controller' => 'App\\Trinity\\WebshopBundle\\Controller\\FrontController::loadAction',
  187.                         // '_controller' => 'TrinityWebshopBundle:Front:load',
  188.                         'categoryId' => null,
  189.                         'param1' => null'param2' => null'param3' => null,
  190.                     ));
  191.                     $Route->setRequirements(array('param1' => '.*?''param2' => '.*?''param3' => '.*?'));
  192.                     $Route->setMethods(array('POST''GET'));
  193.                     $slugkey $locale '-category'// Internally used for page identification
  194.                     $collection->add($slugkey$Route);
  195.                 }
  196.             }
  197.         }
  198.         return $collection;
  199.     }
  200.     public function supports($resource$type null) { return 'extra' === $type; }
  201. }