src/Controller/RegionController.php line 31

  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\Bundle\PersonalizationBundle\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. /**
  16.  * 
  17.  *@IgnoreAnnotation("Template") 
  18.  * @author PcCom
  19.  */
  20. class RegionController extends FrontendController {
  21.     /**
  22.      * @var LocaleService
  23.      */
  24.     private $visitorInfoStorage;
  25.     protected $locale;
  26.     public function __construct(LocaleService $localeVisitorInfoStorageInterface $visitorInfoStorage) {
  27.         $this->locale $locale;
  28.         $this->visitorInfoStorage $visitorInfoStorage;
  29.     }
  30.     public function regionidAction(Request $request) {
  31.         //To Get The Country Name Builtin Function
  32.         // $regionArray = $this->locale->getDisplayRegions();
  33.         // if(!$this->visitorInfoStorage->getVisitorInfo() instanceof \Pimcore\Targeting\Model\VisitorInfo){
  34.         //   exit;
  35.         // }
  36.         try {
  37.             $ipaddress '';
  38.             if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  39.                 $ipaddress $_SERVER['HTTP_CLIENT_IP'];
  40.             } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  41.                 $ipaddress $_SERVER['HTTP_X_FORWARDED_FOR'];
  42.             } else if (isset($_SERVER['HTTP_X_FORWARDED'])) {
  43.                 $ipaddress $_SERVER['HTTP_X_FORWARDED'];
  44.             } else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
  45.                 $ipaddress $_SERVER['HTTP_FORWARDED_FOR'];
  46.             } else if (isset($_SERVER['HTTP_FORWARDED'])) {
  47.                 $ipaddress $_SERVER['HTTP_FORWARDED'];
  48.             } else if (isset($_SERVER['REMOTE_ADDR'])) {
  49.                 $ipaddress $_SERVER['REMOTE_ADDR'];
  50.             } else {
  51.                 $ipaddress 'UNKNOWN';
  52.             }
  53.             if ($ipaddress != 'UNKNOWN') {
  54.                 $ip_info = new  Reader(__DIR__ '/GeoLite2-Country.mmdb');
  55.                 $record $ip_info->country($ipaddress);
  56.                 if ($record) {
  57.                     $ip $record->country->isoCode;
  58.                 }
  59.             }
  60.         } catch (\Exception $e) {
  61.             $ip '';
  62.         }
  63.         // $visitorInfo = $this->visitorInfoStorage->getVisitorInfo();
  64.         // $groups = $visitorInfo->getAssignedTargetGroups();
  65.         $groupids = [];
  66.         // $city = $visitorInfo->get('geoip');
  67.         // $code = $city['country']['iso_code'];
  68.         $code $ip;
  69.         if (!$code) {
  70.             $code 'AE';
  71.         }
  72.         if ($code == 'AE') {
  73.             $groupids[] = '29';
  74.         } elseif ($code == 'SA') {
  75.             $groupids[] = '27';
  76.         } elseif ($code == 'OM' || $code == 'QA' || $code == 'KW' || $code == 'BH') {
  77.             $groupids[] = '28';
  78.         }
  79.         if (!$groupids) {
  80.             $groupids[] = '29';
  81.         }
  82.         // return $this->json(array('code' => $code, 'regions' => $groupids, 'city' => $city, 'regionArray' => $regionArray));
  83.         return new Response($code);
  84.         //return $code;
  85.     }
  86. }