|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://raw.githubusercontent.com/flipboxfactory/craft-link/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipboxfactory/craft-link |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\craft\link\migrations; |
|
10
|
|
|
|
|
11
|
|
|
use craft\db\Migration; |
|
12
|
|
|
use craft\helpers\ArrayHelper; |
|
13
|
|
|
use craft\helpers\Json; |
|
14
|
|
|
use craft\helpers\StringHelper; |
|
15
|
|
|
use craft\records\Field; |
|
16
|
|
|
use flipbox\craft\link\fields\Link; |
|
17
|
|
|
|
|
18
|
|
|
class m190110_100614_link_namespace extends Migration |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @inheritdoc |
|
22
|
|
|
*/ |
|
23
|
|
|
public function safeUp() |
|
24
|
|
|
{ |
|
25
|
|
|
$records = Field::find() |
|
26
|
|
|
->andWhere( |
|
27
|
|
|
[ |
|
28
|
|
|
'type' => "flipbox\\link\\fields\\Link" |
|
29
|
|
|
] |
|
30
|
|
|
) |
|
31
|
|
|
->all(); |
|
32
|
|
|
|
|
33
|
|
|
$success = true; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Field $record |
|
37
|
|
|
*/ |
|
38
|
|
|
foreach ($records as $record) { |
|
39
|
|
|
$record->type = Link::class; |
|
40
|
|
|
|
|
41
|
|
|
$settings = $record->settings ?? []; |
|
42
|
|
|
if (is_string($settings)) { |
|
43
|
|
|
$settings = Json::decodeIfJson($settings); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$types = ArrayHelper::remove($settings, 'types', []); |
|
47
|
|
|
foreach ($types as &$type) { |
|
48
|
|
|
$class = $type['class'] ?? null; |
|
49
|
|
|
|
|
50
|
|
|
// Match our first party links |
|
51
|
|
|
if (StringHelper::startsWith( |
|
52
|
|
|
$class, |
|
53
|
|
|
"flipbox\\link" |
|
54
|
|
|
) |
|
55
|
|
|
) { |
|
56
|
|
|
// Adjust namespacing |
|
57
|
|
|
$type['class'] = StringHelper::replace( |
|
58
|
|
|
$class, |
|
59
|
|
|
"flipbox\\link", |
|
60
|
|
|
"flipbox\\craft\\link" |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
// Allow text not set? |
|
64
|
|
|
if (!ArrayHelper::keyExists( |
|
65
|
|
|
'allowText', |
|
66
|
|
|
$type |
|
67
|
|
|
) |
|
68
|
|
|
) { |
|
69
|
|
|
$type['allowText'] = (bool)(ArrayHelper::remove( |
|
70
|
|
|
$type, |
|
71
|
|
|
'showText', |
|
72
|
|
|
ArrayHelper::remove( |
|
73
|
|
|
$type, |
|
74
|
|
|
'overrideText' |
|
75
|
|
|
) |
|
76
|
|
|
)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Remove old |
|
80
|
|
|
ArrayHelper::remove($type, 'limit'); |
|
81
|
|
|
ArrayHelper::remove($type, 'localizeRelations'); |
|
82
|
|
|
ArrayHelper::remove($type, 'useSingleFolder'); |
|
83
|
|
|
ArrayHelper::remove($type, 'defaultUploadLocationSource'); |
|
84
|
|
|
ArrayHelper::remove($type, 'defaultUploadLocationSubpath'); |
|
85
|
|
|
ArrayHelper::remove($type, 'singleUploadLocationSource'); |
|
86
|
|
|
ArrayHelper::remove($type, 'singleUploadLocationSubpath'); |
|
87
|
|
|
ArrayHelper::remove($type, 'restrictFiles'); |
|
88
|
|
|
ArrayHelper::remove($type, 'allowedKinds'); |
|
89
|
|
|
|
|
90
|
|
|
// Should already be removed |
|
91
|
|
|
ArrayHelper::remove($type, 'overrideText'); |
|
92
|
|
|
ArrayHelper::remove($type, 'showText'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Set types back to settings |
|
97
|
|
|
$settings['types'] = $types; |
|
98
|
|
|
|
|
99
|
|
|
// Update settings |
|
100
|
|
|
$record->settings = $settings; |
|
101
|
|
|
|
|
102
|
|
|
// Save |
|
103
|
|
|
if (!$record->save()) { |
|
104
|
|
|
$success = false; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $success; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|