|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Reasons plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* Adds conditionals to field layouts. |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://vaersaagod.no |
|
8
|
|
|
* @copyright Copyright (c) 2020 Mats Mikkel Rummelhoff |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace mmikkel\reasons\migrations; |
|
12
|
|
|
|
|
13
|
|
|
use craft\db\Table; |
|
14
|
|
|
use mmikkel\reasons\Reasons; |
|
15
|
|
|
|
|
16
|
|
|
use Craft; |
|
17
|
|
|
use craft\config\DbConfig; |
|
18
|
|
|
use craft\db\Migration; |
|
19
|
|
|
use craft\db\Query; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Mats Mikkel Rummelhoff |
|
23
|
|
|
* @package Reasons |
|
24
|
|
|
* @since 2.0.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
class Install extends Migration |
|
27
|
|
|
{ |
|
28
|
|
|
// Public Properties |
|
29
|
|
|
// ========================================================================= |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string The database driver to use |
|
33
|
|
|
*/ |
|
34
|
|
|
public $driver; |
|
35
|
|
|
|
|
36
|
|
|
// Public Methods |
|
37
|
|
|
// ========================================================================= |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @inheritdoc |
|
41
|
|
|
*/ |
|
42
|
|
|
public function safeUp() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->driver = Craft::$app->getConfig()->getDb()->driver; |
|
45
|
|
|
|
|
46
|
|
|
if ($this->createTables()) { |
|
47
|
|
|
$this->addForeignKeys(); |
|
48
|
|
|
// Refresh the db schema caches |
|
49
|
|
|
Craft::$app->db->schema->refresh(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritdoc |
|
57
|
|
|
*/ |
|
58
|
|
|
public function safeDown() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->driver = Craft::$app->getConfig()->getDb()->driver; |
|
61
|
|
|
$this->removeTables(); |
|
62
|
|
|
|
|
63
|
|
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Protected Methods |
|
67
|
|
|
// ========================================================================= |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function createTables() |
|
73
|
|
|
{ |
|
74
|
|
|
$tablesCreated = false; |
|
75
|
|
|
|
|
76
|
|
|
$tableSchema = Craft::$app->db->schema->getTableSchema('{{%reasons}}'); |
|
77
|
|
|
|
|
78
|
|
|
if ($tableSchema === null) { |
|
79
|
|
|
$this->createTable( |
|
80
|
|
|
'{{%reasons}}', |
|
81
|
|
|
[ |
|
82
|
|
|
'id' => $this->primaryKey(), |
|
83
|
|
|
'fieldLayoutId' => $this->integer()->notNull(), |
|
84
|
|
|
'conditionals' => $this->text(), |
|
85
|
|
|
'dateCreated' => $this->dateTime()->notNull(), |
|
86
|
|
|
'dateUpdated' => $this->dateTime()->notNull(), |
|
87
|
|
|
'uid' => $this->uid(), |
|
88
|
|
|
] |
|
89
|
|
|
); |
|
90
|
|
|
$tablesCreated = true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $tablesCreated; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function addForeignKeys() |
|
100
|
|
|
{ |
|
101
|
|
|
$this->addForeignKey( |
|
102
|
|
|
$this->db->getForeignKeyName(), |
|
103
|
|
|
'{{%reasons}}', |
|
104
|
|
|
'fieldLayoutId', |
|
105
|
|
|
Table::FIELDLAYOUTS, |
|
106
|
|
|
'id', |
|
107
|
|
|
'CASCADE', |
|
108
|
|
|
'CASCADE' |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function removeTables() |
|
116
|
|
|
{ |
|
117
|
|
|
$this->dropTableIfExists('{{%reasons}}'); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|