src/Controller/RegionController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Pimcore\Controller\FrontendController;
  4. use Pimcore\Localization\LocaleService;
  5. use Pimcore\Model\DataObject;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Zend\Paginator\Paginator;
  8. use Pimcore\Targeting\VisitorInfoStorageInterface;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Pimcore\Model\Tool\Targeting;
  11. use Pimcore\Model\Tool\Targeting\TargetGroup;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use GeoIp2\Database\Reader;
  14. use Symfony\Component\HttpFoundation\Response;
  15. class RegionController extends FrontendController {
  16.     /**
  17.      * @var LocaleService
  18.      */
  19.     private $visitorInfoStorage;
  20.     protected $locale;
  21.     public function __construct(LocaleService $localeVisitorInfoStorageInterface $visitorInfoStorage) {
  22.         $this->locale $locale;
  23.         $this->visitorInfoStorage $visitorInfoStorage;
  24.     }
  25.     public function regionidAction(Request $request) {
  26.         //To Get The Country Name Builtin Function
  27.         // $regionArray = $this->locale->getDisplayRegions();
  28.         // if(!$this->visitorInfoStorage->getVisitorInfo() instanceof \Pimcore\Targeting\Model\VisitorInfo){
  29.         //   exit;
  30.         // }
  31.         try {
  32.             $ipaddress '';
  33.             if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  34.                 $ipaddress $_SERVER['HTTP_CLIENT_IP'];
  35.             } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  36.                 $ipaddress $_SERVER['HTTP_X_FORWARDED_FOR'];
  37.             } else if (isset($_SERVER['HTTP_X_FORWARDED'])) {
  38.                 $ipaddress $_SERVER['HTTP_X_FORWARDED'];
  39.             } else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
  40.                 $ipaddress $_SERVER['HTTP_FORWARDED_FOR'];
  41.             } else if (isset($_SERVER['HTTP_FORWARDED'])) {
  42.                 $ipaddress $_SERVER['HTTP_FORWARDED'];
  43.             } else if (isset($_SERVER['REMOTE_ADDR'])) {
  44.                 $ipaddress $_SERVER['REMOTE_ADDR'];
  45.             } else {
  46.                 $ipaddress 'UNKNOWN';
  47.             }
  48.             if ($ipaddress != 'UNKNOWN') {
  49.                 $ip_info = new  Reader(__DIR__ '/GeoLite2-Country.mmdb');
  50.                 $record $ip_info->country($ipaddress);
  51.                 if ($record) {
  52.                     $ip $record->country->isoCode;
  53.                 }
  54.             }
  55.         } catch (\Exception $e) {
  56.             $ip '';
  57.         }
  58.         // $visitorInfo = $this->visitorInfoStorage->getVisitorInfo();
  59.         // $groups = $visitorInfo->getAssignedTargetGroups();
  60.         $groupids = [];
  61.         // $city = $visitorInfo->get('geoip');
  62.         // $code = $city['country']['iso_code'];
  63.         $code $ip;
  64.         if (!$code) {
  65.             $code 'AE';
  66.         }
  67.         if ($code == 'AE') {
  68.             $groupids[] = '29';
  69.         } elseif ($code == 'SA') {
  70.             $groupids[] = '27';
  71.         } elseif ($code == 'OM' || $code == 'QA' || $code == 'KW' || $code == 'BH') {
  72.             $groupids[] = '28';
  73.         }
  74.         if (!$groupids) {
  75.             $groupids[] = '29';
  76.         }
  77.         // return $this->json(array('code' => $code, 'regions' => $groupids, 'city' => $city, 'regionArray' => $regionArray));
  78.         return new Response($code);
  79.         //return $code;
  80.     }
  81. }