Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use GeoIP;
  6. use App\Models\Language;
  7.  
  8. class BaseController extends Controller
  9. {
  10. protected $_locale;
  11. protected $_location;
  12. protected $_url = '/';
  13. protected $_region;
  14.  
  15. public function __construct() {
  16. $this->_locale = \App::getLocale();
  17.  
  18. if(!\Illuminate\Support\Facades\Cookie::has('userCity')) {
  19. $location = GeoIP::getLocation(env('GEO_IP'));
  20. } else {
  21. $city = \App\Models\District::getDistricts([
  22. 'id' => \Illuminate\Support\Facades\Cookie::get('userCity'),
  23. 'locale' => $this->_locale
  24. ])->first();
  25.  
  26. if ($city) {
  27. $location['city'] = $city->name;
  28. $location['state'] = $city->region->state;
  29.  
  30. $this->_region = $city;
  31. } else {
  32. $location = GeoIP::getLocation(env('GEO_IP'));
  33. }
  34. }
  35.  
  36. $this->_location = $location;
  37.  
  38. $districts = \App\Models\District::getDistricts([
  39. 'locale' => $this->_locale,
  40. ]);
  41.  
  42. foreach($districts AS $k => $district)
  43. {
  44. if($district->name_en == $this->_location['city'] || $district->region->state == $this->_location['state']) {
  45. $this->_region = $district;
  46. break;
  47. }
  48. }
  49.  
  50. if(!$this->_region)
  51. {
  52. if(env('DEFAULT_CITY'))
  53. {
  54. $this->_region = \App\Models\District::getDistricts([
  55. "id" => env('DEFAULT_CITY'),
  56. "locale" => $this->_locale
  57. ])->first();
  58. }
  59. }
  60.  
  61. try{
  62. $setting = \App\Models\User_setting::getSetting([
  63. 'region' => $this->_region->id,
  64. 'locale' => $this->_locale
  65. ]);
  66. }
  67. catch(\Exception $e)
  68. {
  69. $setting = \App\Models\Setting::find(1);
  70. }
  71.  
  72. $language = new Language();
  73.  
  74. view()->share([
  75. '_languages' => $language->byActive()->get(),
  76. '_districts' => $districts,
  77. '_setting' => @$setting,
  78. '_admin_setting' => \App\Models\Setting::find(1),
  79. '_location' => $this->_region,
  80. ]);
  81. }
  82.  
  83. public function ifNeedRedirect()
  84. {
  85. $language = new Language();
  86.  
  87. if(!$language->byActive()->byCode($this->_locale)->first())
  88. {
  89. return true;
  90. }
  91.  
  92. return false;
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement