|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://flipboxfactory.com/software/organization/license |
|
6
|
|
|
* @link https://www.flipboxfactory.com/software/organization/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\organizations\cp\actions\general; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use flipbox\craft\ember\actions\models\CreateModel; |
|
13
|
|
|
use flipbox\organizations\cp\actions\general\traits\SiteSettingAttributesTrait; |
|
14
|
|
|
use flipbox\organizations\models\Settings; |
|
15
|
|
|
use flipbox\organizations\models\SiteSettings; |
|
16
|
|
|
use flipbox\organizations\Organizations; |
|
17
|
|
|
use yii\base\Model; |
|
18
|
|
|
use yii\web\HttpException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Flipbox Factory <[email protected]> |
|
22
|
|
|
* @since 1.0.0 |
|
23
|
|
|
* |
|
24
|
|
|
* @method array parentNormalizeSiteConfig($config = []) |
|
25
|
|
|
*/ |
|
26
|
|
|
class Update extends CreateModel |
|
27
|
|
|
{ |
|
28
|
|
|
use SiteSettingAttributesTrait { |
|
29
|
|
|
normalizeSiteConfig as parentNormalizeSiteConfig; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* These are the default body params that we're accepting. You can lock down specific Client attributes this way. |
|
34
|
|
|
* |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
|
|
public $validBodyParams = [ |
|
38
|
|
|
'requireOwner', |
|
39
|
|
|
'uniqueOwner' |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public $statusCodeSuccess = 200; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param array $config |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function normalizeSiteConfig($config = []): array |
|
52
|
|
|
{ |
|
53
|
|
|
return array_merge( |
|
54
|
|
|
$this->parentNormalizeSiteConfig($config), |
|
55
|
|
|
[ |
|
56
|
|
|
'enabledByDefault' => (bool)$config['enabledByDefault'] ?? false |
|
57
|
|
|
] |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritdoc |
|
63
|
|
|
* @param Settings $model |
|
64
|
|
|
* @throws \Throwable |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function performAction(Model $model): bool |
|
67
|
|
|
{ |
|
68
|
|
|
$fieldLayout = $model->getFieldLayout(); |
|
69
|
|
|
|
|
70
|
|
|
// Save field layout |
|
71
|
|
|
if (!Craft::$app->getFields()->saveLayout($fieldLayout)) { |
|
72
|
|
|
throw new HttpException(401, "Unable to save field layout"); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return Craft::$app->getPlugins()->savePluginSettings( |
|
76
|
|
|
Organizations::getInstance(), |
|
77
|
|
|
$model->toArray() |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @inheritdoc |
|
83
|
|
|
* @return Settings |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function newModel(array $config = []): Model |
|
86
|
|
|
{ |
|
87
|
|
|
return Organizations::getInstance()->getSettings(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/******************************************* |
|
92
|
|
|
* POPULATE |
|
93
|
|
|
*******************************************/ |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @inheritdoc |
|
97
|
|
|
* @param Settings $model |
|
98
|
|
|
* @return Settings |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function populate(Model $model): Model |
|
101
|
|
|
{ |
|
102
|
|
|
parent::populate($model); |
|
103
|
|
|
$this->populateSiteSettings($model); |
|
|
|
|
|
|
104
|
|
|
$this->populateSiteLayout($model); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
return $model; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param Settings $model |
|
111
|
|
|
* @return Settings |
|
112
|
|
|
*/ |
|
113
|
|
|
private function populateSiteLayout(Settings $model): Settings |
|
114
|
|
|
{ |
|
115
|
|
|
if ($fieldLayout = Craft::$app->getFields()->assembleLayoutFromPost()) { |
|
116
|
|
|
$model->setFieldLayout($fieldLayout); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $model; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param Settings $model |
|
124
|
|
|
* @return Settings |
|
125
|
|
|
*/ |
|
126
|
|
|
private function populateSiteSettings(Settings $model): Settings |
|
127
|
|
|
{ |
|
128
|
|
|
if (null !== ($sites = $this->sitesSettingsFromBody())) { |
|
129
|
|
|
$enabledSites = []; |
|
130
|
|
|
|
|
131
|
|
|
foreach ($sites as $siteId => $siteConfig) { |
|
132
|
|
|
if (!($siteConfig['enabled'] ?? false)) { |
|
133
|
|
|
continue; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$siteConfig['siteId'] = $siteId; |
|
137
|
|
|
$siteConfig['class'] = SiteSettings::class; |
|
138
|
|
|
$enabledSites[$siteId] = $siteConfig; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$model->setSiteSettings($enabledSites); |
|
142
|
|
|
} |
|
143
|
|
|
return $model; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.