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\organization\modules\configuration\controllers; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\helpers\ArrayHelper; |
13
|
|
|
use flipbox\organization\models\Settings; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Flipbox Factory <[email protected]> |
17
|
|
|
* @since 1.0.0 |
18
|
|
|
*/ |
19
|
|
|
class GeneralController extends AbstractController |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return \yii\web\Response |
24
|
|
|
*/ |
25
|
|
|
public function actionSave() |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
// Admins and Post requests only |
29
|
|
|
$this->requireAdmin(); |
30
|
|
|
$this->requirePostRequest(); |
31
|
|
|
|
32
|
|
|
$request = Craft::$app->getRequest(); |
33
|
|
|
$session = Craft::$app->getSession(); |
34
|
|
|
|
35
|
|
|
/** @var Settings $model */ |
36
|
|
|
$model = $this->module->module->getSettings(); |
37
|
|
|
|
38
|
|
|
// Statuses from post |
39
|
|
|
if ($rawStatuses = $request->getBodyParam('statuses', [])) { |
40
|
|
|
$statusArray = []; |
41
|
|
|
|
42
|
|
|
foreach (ArrayHelper::toArray($rawStatuses) as $rawStatus) { |
43
|
|
|
// Make sure we have a label and value |
44
|
|
|
if (empty($rawStatus['label']) || empty($rawStatus['value'])) { |
45
|
|
|
$model->addError( |
46
|
|
|
'statuses', |
47
|
|
|
Craft::t('organization', 'Each status must have a valid label and value.') |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
break; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Add status |
54
|
|
|
$statusArray[$rawStatus['value']] = $rawStatus['label']; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Set settings array |
59
|
|
|
$model->statuses = !empty($statusArray) ? $statusArray : null; |
|
|
|
|
60
|
|
|
|
61
|
|
|
// Handle each site's url/template settings |
62
|
|
|
foreach (Craft::$app->getSites()->getAllSites() as $site) { |
63
|
|
|
$namespace = 'sites.' . $site->handle; |
64
|
|
|
|
65
|
|
|
$postedSettings = $request->getBodyParam($namespace); |
66
|
|
|
|
67
|
|
|
$siteSettings = $model->getSite($site->id); |
68
|
|
|
$siteSettings->hasUrls = !empty($postedSettings['uriFormat']); |
69
|
|
|
|
70
|
|
|
if ($siteSettings->hasUrls) { |
71
|
|
|
$siteSettings->uriFormat = $postedSettings['uriFormat']; |
72
|
|
|
$siteSettings->template = $postedSettings['template']; |
73
|
|
|
} else { |
74
|
|
|
$siteSettings->uriFormat = null; |
75
|
|
|
$siteSettings->template = null; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Save settings |
80
|
|
|
if (!$this->module->getGeneral()->save($model)) { |
81
|
|
|
// Ajax request |
82
|
|
|
if (!$request->getAcceptsJson()) { |
83
|
|
|
// Fail message |
84
|
|
|
$message = Craft::t('organization', 'Settings NOT saved successfully.'); |
85
|
|
|
|
86
|
|
|
// Flash fail message |
87
|
|
|
$session->setError($message); |
88
|
|
|
|
89
|
|
|
// Send the element back to the template |
90
|
|
|
Craft::$app->getUrlManager()->setRouteParams([ |
91
|
|
|
'settings' => $model |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
// Redirect |
95
|
|
|
return $this->redirectToPostedUrl($model); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->asJson([ |
99
|
|
|
'success' => false, |
100
|
|
|
'errors' => $model->getErrors(), |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Ajax request |
105
|
|
|
if (!$request->getAcceptsJson()) { |
106
|
|
|
// Success message |
107
|
|
|
$message = Craft::t('organization', 'Settings saved successfully.'); |
108
|
|
|
|
109
|
|
|
// Flash success message |
110
|
|
|
$session->setNotice($message); |
111
|
|
|
|
112
|
|
|
return $this->redirectToPostedUrl($model); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->asJson($model); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.