1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace dukt\analytics\migrations; |
4
|
|
|
|
5
|
|
|
use craft\db\Migration; |
6
|
|
|
use craft\db\Query; |
7
|
|
|
use craft\helpers\Json; |
8
|
|
|
use dukt\analytics\models\Info; |
9
|
|
|
use dukt\analytics\Plugin; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* m180529_125418_info migration. |
13
|
|
|
*/ |
14
|
|
|
class m180529_125418_info extends Migration |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @inheritdoc |
18
|
|
|
*/ |
19
|
|
|
public function safeUp() |
20
|
|
|
{ |
21
|
|
|
$this->createTables(); |
22
|
|
|
$this->insertDefaultData(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Creates the tables. |
27
|
|
|
*/ |
28
|
|
|
public function createTables() |
29
|
|
|
{ |
30
|
|
|
$this->createTable( |
31
|
|
|
'{{%analytics_info}}', |
32
|
|
|
[ |
33
|
|
|
'id' => $this->primaryKey(), |
34
|
|
|
'forceConnect' => $this->boolean()->defaultValue(false)->notNull(), |
35
|
|
|
'token' => $this->text(), |
36
|
|
|
|
37
|
|
|
'dateCreated' => $this->dateTime()->notNull(), |
38
|
|
|
'dateUpdated' => $this->dateTime()->notNull(), |
39
|
|
|
'uid' => $this->uid() |
40
|
|
|
] |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Populates the DB with the default data. |
46
|
|
|
*/ |
47
|
|
|
public function insertDefaultData() |
48
|
|
|
{ |
49
|
|
|
$row = (new Query()) |
50
|
|
|
->select('*') |
51
|
|
|
->from(['{{%plugins}}']) |
52
|
|
|
->where(['handle' => 'analytics']) |
53
|
|
|
->one(); |
54
|
|
|
|
55
|
|
|
if (!$row) { |
56
|
|
|
echo " done\n"; |
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!isset($row['settings'])) { |
61
|
|
|
echo " done\n"; |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$forceConnect = false; |
66
|
|
|
$token = null; |
67
|
|
|
$settingsJson = $row['settings']; |
68
|
|
|
$settings = Json::decode($settingsJson); |
69
|
|
|
|
70
|
|
|
if($settings) { |
71
|
|
|
$forceConnect = $settings['forceConnect']; |
72
|
|
|
|
73
|
|
|
if(isset($settings['token'])) { |
74
|
|
|
$token = $settings['token']; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Populate the analytics_info table |
79
|
|
|
echo ' > populate the analytics_info table ...'; |
80
|
|
|
Plugin::getInstance()->saveInfo(new Info([ |
81
|
|
|
'forceConnect' => $forceConnect, |
82
|
|
|
'token' => $token, |
83
|
|
|
])); |
84
|
|
|
echo " done\n"; |
85
|
|
|
|
86
|
|
|
// Get rid of the old plugin settings |
87
|
|
|
echo ' > remove old plugin settings ...'; |
88
|
|
|
if($settings) { |
89
|
|
|
unset($settings['forceConnect']); |
90
|
|
|
|
91
|
|
|
if(isset($settings['token'])) { |
92
|
|
|
unset($settings['token']); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$settingsJson = Json::encode($settings); |
96
|
|
|
$this->update('{{%plugins}}', ['settings' => $settingsJson], ['id' => $row['id']]); |
97
|
|
|
} |
98
|
|
|
echo " done\n"; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritdoc |
103
|
|
|
*/ |
104
|
|
|
public function safeDown() |
105
|
|
|
{ |
106
|
|
|
echo "m180529_125418_info cannot be reverted.\n"; |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|