|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace mmikkel\reasons\migrations; |
|
5
|
|
|
|
|
6
|
|
|
use Craft; |
|
7
|
|
|
use craft\db\Migration; |
|
8
|
|
|
use craft\db\Query; |
|
9
|
|
|
use craft\db\Table; |
|
10
|
|
|
use craft\helpers\Db; |
|
11
|
|
|
use craft\helpers\Json; |
|
12
|
|
|
use craft\helpers\StringHelper; |
|
13
|
|
|
|
|
14
|
|
|
class m200603_004000_projectconfig extends Migration |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @return bool |
|
19
|
|
|
* @throws \yii\base\ErrorException |
|
20
|
|
|
* @throws \yii\base\Exception |
|
21
|
|
|
* @throws \yii\base\InvalidConfigException |
|
22
|
|
|
* @throws \yii\base\NotSupportedException |
|
23
|
|
|
* @throws \yii\web\ServerErrorHttpException |
|
24
|
|
|
*/ |
|
25
|
|
|
public function safeUp() |
|
26
|
|
|
{ |
|
27
|
|
|
// Don't make the same config changes twice |
|
28
|
|
|
$schemaVersion = Craft::$app->getProjectConfig() |
|
29
|
|
|
->get('plugins.reasons.schemaVersion', true); |
|
30
|
|
|
|
|
31
|
|
|
if (\version_compare($schemaVersion, '2.1.0', '<')) { |
|
32
|
|
|
|
|
33
|
|
|
$rows = (new Query()) |
|
34
|
|
|
->select(['reasons.fieldLayoutId', 'reasons.conditionals', 'reasons.uid', 'fieldlayouts.uid AS fieldLayoutUid']) |
|
35
|
|
|
->innerJoin(['fieldlayouts' => Table::FIELDLAYOUTS], '[[fieldlayouts.id]] = [[reasons.fieldLayoutId]]') |
|
36
|
|
|
->from('{{%reasons}} AS reasons') |
|
37
|
|
|
->all(); |
|
38
|
|
|
|
|
39
|
|
|
foreach ($rows as $row) { |
|
40
|
|
|
$path = "reasons_conditionals.{$row['uid']}"; |
|
41
|
|
|
Craft::$app->getProjectConfig()->set($path, [ |
|
42
|
|
|
'fieldLayoutUid' => $row['fieldLayoutUid'], |
|
43
|
|
|
'conditionals' => $this->prepConditionalsForProjectConfig($row['conditionals']), |
|
44
|
|
|
]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritDoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function safeDown() |
|
56
|
|
|
{ |
|
57
|
|
|
echo "m200603_004000_projectconfig cannot be reverted.\n"; |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string|array $conditionals |
|
63
|
|
|
* @return string|null |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function prepConditionalsForProjectConfig($conditionals): ?string |
|
66
|
|
|
{ |
|
67
|
|
|
if (!$conditionals) { |
|
68
|
|
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
$return = []; |
|
71
|
|
|
$conditionals = Json::decodeIfJson($conditionals); |
|
72
|
|
|
foreach ($conditionals as $targetFieldIdOrUid => $statements) { |
|
73
|
|
|
if (!StringHelper::isUUID($targetFieldIdOrUid)) { |
|
74
|
|
|
$targetFieldUid = Db::uidById(Table::FIELDS, $targetFieldIdOrUid); |
|
75
|
|
|
} else { |
|
76
|
|
|
$targetFieldUid = $targetFieldIdOrUid; |
|
77
|
|
|
} |
|
78
|
|
|
$return[$targetFieldUid] = \array_map(function (array $rules) { |
|
79
|
|
|
return \array_map(function (array $rule) { |
|
80
|
|
|
$fieldIdOrUid = $rule['fieldId'] ?? $rule['field']; |
|
81
|
|
|
if (!StringHelper::isUUID($fieldIdOrUid)) { |
|
82
|
|
|
$fieldUid = Db::uidById(Table::FIELDS, $fieldIdOrUid); |
|
83
|
|
|
} else { |
|
84
|
|
|
$fieldUid = $fieldIdOrUid; |
|
85
|
|
|
} |
|
86
|
|
|
return [ |
|
87
|
|
|
'field' => $fieldUid, |
|
88
|
|
|
'compare' => $rule['compare'], |
|
89
|
|
|
'value' => $rule['value'], |
|
90
|
|
|
]; |
|
91
|
|
|
}, $rules); |
|
92
|
|
|
}, $statements); |
|
93
|
|
|
} |
|
94
|
|
|
return Json::encode($return); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|