Skip to content

Commit 19fbebc

Browse files
committed
LocationSetter component & handleLoginEvent
1 parent 1a9a7bf commit 19fbebc

File tree

6 files changed

+106
-20
lines changed

6 files changed

+106
-20
lines changed

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@
1515
"email": "e9139905539@gmail.com"
1616
}
1717
],
18+
"repositories": [
19+
{
20+
"type": "composer",
21+
"url": "https://asset-packagist.org"
22+
}
23+
],
1824
"support": {
1925
"issues": "https://github.com/uranum/yii2-define-location/issues?state=open",
2026
"source": "https://github.com/uranum/yii2-define-location"
2127
},
2228
"require": {
2329
"php": ">=7.0.0",
24-
"yiisoft/yii2": "~2.0",
30+
"yiisoft/yii2": "~2.0.11",
2531
"himiklab/yii2-ipgeobase-component": "1.0.2"
2632
},
2733
"autoload": {
@@ -31,12 +37,6 @@
3137
}
3238
},
3339
"config": {
34-
"process-timeout": 1800,
35-
"fxp-asset": {
36-
"installer-paths": {
37-
"npm-asset-library": "vendor/npm",
38-
"bower-asset-library": "vendor/bower"
39-
}
40-
}
40+
"process-timeout": 1800
4141
}
4242
}

src/Module.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace uranum\location;
44

5+
use uranum\location\components\LocationSetter;
56
use Yii;
67
use yii\base\Application;
78
use yii\base\BootstrapInterface;
@@ -36,6 +37,8 @@ class Module extends \yii\base\Module implements BootstrapInterface
3637
public $userModelClass;
3738
public $userTableName;
3839
public $controllerNamespace = 'uranum\location\controllers';
40+
/** @var \himiklab\ipgeobase\IpGeoBase */
41+
public $ipGeoComponent;
3942
const TABLE_NAME = '{{%userIp}}';
4043
const USER_CITY = 'userCity';
4144

@@ -46,9 +49,7 @@ class Module extends \yii\base\Module implements BootstrapInterface
4649
public function bootstrap($app)
4750
{
4851
if ($app instanceof \yii\web\Application) {
49-
$app->user->on(User::EVENT_AFTER_LOGIN, function ($event) {
50-
Yii::$app->session->remove(self::USER_CITY);
51-
});
52+
$app->user->on(User::EVENT_AFTER_LOGIN, [LocationSetter::className(), 'handleLoginEvent']);
5253
}
5354
}
5455

@@ -59,7 +60,20 @@ public function init()
5960
{
6061
parent::init();
6162
$this->registerTranslations();
63+
$this->initIpGeo();
6264
$this->userTableName = call_user_func([$this->userModelClass, 'tableName']);
65+
66+
}
67+
68+
private function initIpGeo()
69+
{
70+
$components = Yii::$app->getComponents();
71+
foreach ($components as $name => $component) {
72+
if ($component['class'] == 'himiklab\ipgeobase\IpGeoBase') {
73+
$this->ipGeoComponent = Yii::$app->get($name);
74+
break;
75+
}
76+
}
6377
}
6478

6579
public function registerTranslations()

src/components/LocationSetter.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Евгений Емельянов <e9139905539@gmail.com>
5+
*/
6+
7+
namespace uranum\location\components;
8+
9+
10+
use uranum\location\models\UserIp;
11+
use uranum\location\Module;
12+
use Yii;
13+
use yii\base\Component;
14+
use yii\base\Event;
15+
use yii\helpers\VarDumper;
16+
use yii\web\Session;
17+
18+
class LocationSetter extends Component
19+
{
20+
const EVENT_SET_LOCATION = 'setLocation';
21+
const EVENT_USER_LOGIN = 'userLogin';
22+
const EVENT_USER_SIGNUP = 'userSignup';
23+
24+
private $session;
25+
private $module;
26+
27+
public function __construct(Module $module, Session $session, array $config = [])
28+
{
29+
parent::__construct($config);
30+
$this->session = \Yii::$app->session;
31+
$this->module = $module;
32+
}
33+
34+
/**
35+
* @param $event Event
36+
*/
37+
public function handleLoginEvent($event)
38+
{
39+
// проверить базу и записать в сессию из базы
40+
// оставить то, что в сессии
41+
// записать в сессию из гео
42+
43+
// VarDumper::dump($event->sender,5, true);
44+
// die();
45+
46+
$storage = $this->hasUserCityInStorage($event->sender->id);
47+
48+
if (null !== $storage) {
49+
$this->session->set(Module::USER_CITY, $storage);
50+
} else {
51+
$this->setUserCityFromGeo();
52+
}
53+
}
54+
55+
protected function hasUserCityInStorage($id)
56+
{
57+
return UserIp::findOne(['user_id' => $id]);
58+
}
59+
60+
protected function setUserCityFromGeo()
61+
{
62+
if (!$this->session->has(Module::USER_CITY)) {
63+
$this->session->set(Module::USER_CITY, $this->module->ipGeoComponent->getLocation(Yii::$app->request->userIP));
64+
}
65+
}
66+
}

src/controllers/DefaultController.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ class DefaultController extends Controller
1212
{
1313
public function actionSendCity()
1414
{
15-
$city = Yii::$app->request->post('city');
15+
$city = Yii::$app->request->getBodyParam('city');
1616
$session = Yii::$app->session;
17+
18+
/**
19+
* основное действие:
20+
* запись города в базу для логгед юзера
21+
* запись города в сессию
22+
* при успешной записи выбранного города в базу
23+
*/
1724

1825
if (Yii::$app->request->isAjax) {
1926
if (!Yii::$app->user->isGuest) {

src/widget/Location.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace uranum\location\widget;
44

55

6-
use uranum\location\models\UserIp;
76
use uranum\location\Module;
87
use Yii;
98
use yii\base\InvalidConfigException;
@@ -34,10 +33,10 @@ class Location extends Widget
3433
public $sendUrl;
3534
public $city;
3635
public $cssPredefinedCities = 'ur-predefined-block';
37-
/** @var \himiklab\ipgeobase\IpGeoBase */
38-
public $ipGeoComponent;
3936
/** @var Session $session */
4037
private $session;
38+
/** @var $module Module */
39+
private $module;
4140

4241
public function init()
4342
{
@@ -99,16 +98,16 @@ public function init()
9998
private function setCity()
10099
{
101100
if ($this->isCityInSession()) {
102-
$this->city = $this->session->get(Module::USER_CITY);
103-
} else {
101+
$this->city = $this->session->get(Module::USER_CITY);
102+
} else {
104103
$this->city = $this->getCityFromGeo();
105104
}
106105
}
107106

108107
private function getCityFromGeo()
109108
{
110-
$result = $this->ipGeoComponent->getLocation(Yii::$app->request->userIP);
111-
return (empty($result['city'])) ? $this->chooseTitle : $result['city'];
109+
$result = $this->module->ipGeoComponent->getLocation(Yii::$app->request->userIP);
110+
return ($result['city']) ?? $this->chooseTitle;
112111
}
113112

114113
private function isCityInSession()

src/widget/assets/js/scripts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function sendCity(url) {
3232
city: city.value
3333
},
3434
success : function(data) {
35-
if(data == 'good') {
35+
if(data === 'good') {
3636
$('#ur-city-link').html(city.value);
3737
slideUpChooseBlock();
3838
}

0 commit comments

Comments
 (0)