Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use GeoIP;
- use App\Models\Language;
- class BaseController extends Controller
- {
- protected $_locale;
- protected $_location;
- protected $_url = '/';
- protected $_region;
- public function __construct() {
- $this->_locale = \App::getLocale();
- if(!\Illuminate\Support\Facades\Cookie::has('userCity')) {
- $location = GeoIP::getLocation(env('GEO_IP'));
- } else {
- $city = \App\Models\District::getDistricts([
- 'id' => \Illuminate\Support\Facades\Cookie::get('userCity'),
- 'locale' => $this->_locale
- ])->first();
- if ($city) {
- $location['city'] = $city->name;
- $location['state'] = $city->region->state;
- $this->_region = $city;
- } else {
- $location = GeoIP::getLocation(env('GEO_IP'));
- }
- }
- $this->_location = $location;
- $districts = \App\Models\District::getDistricts([
- 'locale' => $this->_locale,
- ]);
- foreach($districts AS $k => $district)
- {
- if($district->name_en == $this->_location['city'] || $district->region->state == $this->_location['state']) {
- $this->_region = $district;
- break;
- }
- }
- if(!$this->_region)
- {
- if(env('DEFAULT_CITY'))
- {
- $this->_region = \App\Models\District::getDistricts([
- "id" => env('DEFAULT_CITY'),
- "locale" => $this->_locale
- ])->first();
- }
- }
- try{
- $setting = \App\Models\User_setting::getSetting([
- 'region' => $this->_region->id,
- 'locale' => $this->_locale
- ]);
- }
- catch(\Exception $e)
- {
- $setting = \App\Models\Setting::find(1);
- }
- $language = new Language();
- view()->share([
- '_languages' => $language->byActive()->get(),
- '_districts' => $districts,
- '_setting' => @$setting,
- '_admin_setting' => \App\Models\Setting::find(1),
- '_location' => $this->_region,
- ]);
- }
- public function ifNeedRedirect()
- {
- $language = new Language();
- if(!$language->byActive()->byCode($this->_locale)->first())
- {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement