src/Twig/Extension/NavigationExtension.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Twig\Extension;
  3. use Pimcore\Model\Document;
  4. use Pimcore\Twig\Extension\Templating\Navigation;
  5. use Twig\Extension\AbstractExtension;
  6. use Twig\TwigFunction;
  7. use GeoIp2\Database\Reader;
  8. class NavigationExtension extends AbstractExtension
  9. {
  10.         /**
  11.          * @var Navigation
  12.          */
  13.         protected $navigationHelper;
  14.     
  15.            
  16.         /**
  17.          * @param Navigation $navigationHelper
  18.          */
  19.         public function __construct(Navigation $navigationHelper)
  20.         {
  21.             $this->navigationHelper $navigationHelper;
  22.         }
  23.         /**
  24.          * @return array|TwigFunction[]
  25.          */
  26.         public function getFunctions()
  27.         {
  28.             return [
  29.                 new TwigFunction('app_navigation_data_links', [$this'getDataLinks']),
  30.                 new TwigFunction('country_ip', [$this'getCountryIp'])
  31.             ];
  32.         }
  33.         
  34.             /**
  35.              * @param Document $document
  36.              * @param Document $startNode
  37.              *
  38.              * @return \Pimcore\Navigation\Container
  39.              * @throws \Exception
  40.              */
  41.             public function getDataLinks(Document $documentDocument $startNode)
  42.             {
  43.                 $navigation $this->navigationHelper->build([
  44.                     'active' => $document,
  45.                     'root' => $startNode,
  46.                     'pageCallback' => function($page$document) {
  47.                         /** @var \Pimcore\Model\Document $document */
  48.                         /** @var \Pimcore\Navigation\Page\Document $page */
  49.                         if($document->getProperty("data_object")) {
  50.                             $children=$document->getProperty("data_object")->getChildren();
  51.                             //sorting the objects
  52.                             usort($children,function ($a$b) {
  53.                                 $orderA $a->get("order");
  54.                                 $orderB =  $b->get("order");
  55.             
  56.                                 if ($orderA === $orderB) {
  57.                                     return 0;
  58.                                 }
  59.             
  60.                                 return ($orderA $orderB) ? -1;
  61.                             });
  62.                             foreach($children as $object) {
  63.                                 $uri = new \Pimcore\Navigation\Page\Document([
  64.                                     "label" => $object->getName(),
  65.                                     "id" => "object-" $object->getId(),
  66.                                     "uri" =>'/'.$document->getKey().'/'.$object->getUrl(),
  67.                                 ]);
  68.                                 $page->addPage($uri);
  69.                             }
  70.                         }
  71.                     }
  72.                 ]);
  73.         
  74.                 return $navigation;
  75.             }
  76.              /**
  77.      * @param array $values
  78.      * @return array
  79.      */
  80.     public function getCountryIp()
  81.     {
  82.         try {
  83.             $ipaddress '';
  84.             $ip '';
  85.             if (isset($_SERVER['HTTP_CLIENT_IP'])){
  86.                 $ipaddress $_SERVER['HTTP_CLIENT_IP'];
  87.             }else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
  88.                 $ipaddress $_SERVER['HTTP_X_FORWARDED_FOR'];
  89.             }else if(isset($_SERVER['HTTP_X_FORWARDED'])){
  90.                 $ipaddress $_SERVER['HTTP_X_FORWARDED'];
  91.             }else if(isset($_SERVER['HTTP_FORWARDED_FOR'])){
  92.                 $ipaddress $_SERVER['HTTP_FORWARDED_FOR'];
  93.             }else if(isset($_SERVER['HTTP_FORWARDED'])){
  94.                 $ipaddress $_SERVER['HTTP_FORWARDED'];
  95.             }else if(isset($_SERVER['REMOTE_ADDR'])){
  96.                 $ipaddress $_SERVER['REMOTE_ADDR'];
  97.             }else{
  98.                 $ipaddress 'UNKNOWN';
  99.             }
  100.             
  101.         
  102.             if($ipaddress != 'UNKNOWN' && $ipaddress != '127.0.0.1') {
  103.                 $ip_info = new  Reader(__DIR__.'/GeoLite2-Country.mmdb');
  104.                 $record $ip_info->country($ipaddress);
  105.     
  106.                 if($record) {
  107.                     $ip $record->country->isoCode;
  108.                 }
  109.             }else{
  110.                 $ip "AE";
  111.             }
  112.         } catch (Exception $e) {
  113.             $ip '';
  114.         }
  115.        
  116.         return $ip;
  117.     }
  118. }
  119. ?>