src/Controller/RegionController.php line 31
<?php
namespace App\Controller;
use Pimcore\Controller\FrontendController;
use Pimcore\Localization\LocaleService;
use Pimcore\Model\DataObject;
use Symfony\Component\HttpFoundation\Request;
use Zend\Paginator\Paginator;
use Pimcore\Bundle\PersonalizationBundle\Targeting\VisitorInfoStorageInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Pimcore\Model\Tool\Targeting;
use Pimcore\Model\Tool\Targeting\TargetGroup;
use Symfony\Component\Routing\Annotation\Route;
use GeoIp2\Database\Reader;
use Symfony\Component\HttpFoundation\Response;
/**
*
*@IgnoreAnnotation("Template")
* @author PcCom
*/
class RegionController extends FrontendController {
/**
* @var LocaleService
*/
private $visitorInfoStorage;
protected $locale;
public function __construct(LocaleService $locale, VisitorInfoStorageInterface $visitorInfoStorage) {
$this->locale = $locale;
$this->visitorInfoStorage = $visitorInfoStorage;
}
public function regionidAction(Request $request) {
//To Get The Country Name Builtin Function
// $regionArray = $this->locale->getDisplayRegions();
// if(!$this->visitorInfoStorage->getVisitorInfo() instanceof \Pimcore\Targeting\Model\VisitorInfo){
// exit;
// }
try {
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
} else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else if (isset($_SERVER['HTTP_X_FORWARDED'])) {
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
} else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
} else if (isset($_SERVER['HTTP_FORWARDED'])) {
$ipaddress = $_SERVER['HTTP_FORWARDED'];
} else if (isset($_SERVER['REMOTE_ADDR'])) {
$ipaddress = $_SERVER['REMOTE_ADDR'];
} else {
$ipaddress = 'UNKNOWN';
}
if ($ipaddress != 'UNKNOWN') {
$ip_info = new Reader(__DIR__ . '/GeoLite2-Country.mmdb');
$record = $ip_info->country($ipaddress);
if ($record) {
$ip = $record->country->isoCode;
}
}
} catch (\Exception $e) {
$ip = '';
}
// $visitorInfo = $this->visitorInfoStorage->getVisitorInfo();
// $groups = $visitorInfo->getAssignedTargetGroups();
$groupids = [];
// $city = $visitorInfo->get('geoip');
// $code = $city['country']['iso_code'];
$code = $ip;
if (!$code) {
$code = 'AE';
}
if ($code == 'AE') {
$groupids[] = '29';
} elseif ($code == 'SA') {
$groupids[] = '27';
} elseif ($code == 'OM' || $code == 'QA' || $code == 'KW' || $code == 'BH') {
$groupids[] = '28';
}
if (!$groupids) {
$groupids[] = '29';
}
// return $this->json(array('code' => $code, 'regions' => $groupids, 'city' => $city, 'regionArray' => $regionArray));
return new Response($code);
//return $code;
}
}