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
|
|
|
'defaultUserState' |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public $statusCodeSuccess = 200; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array $config |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
protected function normalizeSiteConfig($config = []): array |
51
|
|
|
{ |
52
|
|
|
return array_merge( |
53
|
|
|
$this->parentNormalizeSiteConfig($config), |
54
|
|
|
[ |
55
|
|
|
'enabledByDefault' => (bool)$config['enabledByDefault'] ?? false |
56
|
|
|
] |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
* @param Settings $model |
63
|
|
|
* @throws \Throwable |
64
|
|
|
*/ |
65
|
|
|
protected function performAction(Model $model): bool |
66
|
|
|
{ |
67
|
|
|
$fieldLayout = $model->getFieldLayout(); |
68
|
|
|
|
69
|
|
|
// Save field layout |
70
|
|
|
if (!Craft::$app->getFields()->saveLayout($fieldLayout)) { |
71
|
|
|
throw new HttpException(401, "Unable to save field layout"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return Craft::$app->getPlugins()->savePluginSettings( |
75
|
|
|
Organizations::getInstance(), |
76
|
|
|
$model->toArray() |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritdoc |
82
|
|
|
* @return Settings |
83
|
|
|
*/ |
84
|
|
|
protected function newModel(array $config = []): Model |
85
|
|
|
{ |
86
|
|
|
return Organizations::getInstance()->getSettings(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/******************************************* |
91
|
|
|
* POPULATE |
92
|
|
|
*******************************************/ |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritdoc |
96
|
|
|
* @param Settings $model |
97
|
|
|
* @return Settings |
98
|
|
|
*/ |
99
|
|
|
protected function populate(Model $model): Model |
100
|
|
|
{ |
101
|
|
|
parent::populate($model); |
102
|
|
|
$this->populateSiteSettings($model); |
|
|
|
|
103
|
|
|
$this->populateSiteLayout($model); |
|
|
|
|
104
|
|
|
|
105
|
|
|
return $model; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Settings $model |
110
|
|
|
* @return Settings |
111
|
|
|
*/ |
112
|
|
|
private function populateSiteLayout(Settings $model): Settings |
113
|
|
|
{ |
114
|
|
|
if ($fieldLayout = Craft::$app->getFields()->assembleLayoutFromPost()) { |
115
|
|
|
$model->setFieldLayout($fieldLayout); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $model; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param Settings $model |
123
|
|
|
* @return Settings |
124
|
|
|
*/ |
125
|
|
|
private function populateSiteSettings(Settings $model): Settings |
126
|
|
|
{ |
127
|
|
|
if (null !== ($sites = $this->sitesSettingsFromBody())) { |
128
|
|
|
$enabledSites = []; |
129
|
|
|
|
130
|
|
|
foreach ($sites as $siteId => $siteConfig) { |
131
|
|
|
if (!($siteConfig['enabled'] ?? false)) { |
132
|
|
|
continue; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$siteConfig['siteId'] = $siteId; |
136
|
|
|
$siteConfig['class'] = SiteSettings::class; |
137
|
|
|
$enabledSites[$siteId] = $siteConfig; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$model->setSiteSettings($enabledSites); |
141
|
|
|
} |
142
|
|
|
return $model; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
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.