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\models; |
10
|
|
|
|
11
|
|
|
use craft\base\Model; |
12
|
|
|
use flipbox\craft\ember\helpers\SiteHelper; |
13
|
|
|
use flipbox\craft\ember\models\FieldLayoutAttributeTrait; |
14
|
|
|
use flipbox\craft\ember\validators\ModelValidator; |
15
|
|
|
use flipbox\organizations\elements\Organization; |
16
|
|
|
use flipbox\organizations\Organizations; |
17
|
|
|
use flipbox\organizations\records\UserAssociation; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Flipbox Factory <[email protected]> |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
class Settings extends Model |
24
|
|
|
{ |
25
|
|
|
use FieldLayoutAttributeTrait, |
26
|
|
|
SiteSettingAttributeTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int|null|false |
30
|
|
|
*/ |
31
|
|
|
public $userSidebarTemplate = 'organizations/_components/hooks/users/details'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $usersTabOrder = 10; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $usersTabLabel = 'Users'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $defaultUserState = UserAssociation::STATE_ACTIVE; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
private $enforceUserSortOrder = false; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var bool |
55
|
|
|
*/ |
56
|
|
|
private $enforceOrganizationSortOrder = false; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
3 |
|
public static function siteSettingsClass(): string |
62
|
|
|
{ |
63
|
3 |
|
return SiteSettings::class; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
protected static function fieldLayoutType(): string |
70
|
|
|
{ |
71
|
|
|
return Organization::class; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function getUserStates(): array |
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
|
|
UserAssociation::STATE_ACTIVE => Organizations::t('Active'), |
81
|
|
|
UserAssociation::STATE_PENDING => Organizations::t('Pending'), |
82
|
|
|
UserAssociation::STATE_INACTIVE => Organizations::t('Inactive') |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the User tab order found on the Organization entry page. |
88
|
|
|
*/ |
89
|
|
|
public function getUsersTabOrder(): int |
90
|
|
|
{ |
91
|
|
|
return $this->usersTabOrder; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set the User tab order found on the Organization entry page. |
96
|
|
|
* |
97
|
|
|
* @param int $order |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function setUsersTabOrder(int $order) |
101
|
|
|
{ |
102
|
|
|
$this->usersTabOrder = $order; |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the User tab label found on the Organization entry page. |
108
|
|
|
*/ |
109
|
|
|
public function getUsersTabLabel(): string |
110
|
|
|
{ |
111
|
|
|
return $this->usersTabLabel; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set the User tab label found on the Organization entry page. |
116
|
|
|
* |
117
|
|
|
* @param string $label |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function setUsersTabLabel(string $label) |
121
|
|
|
{ |
122
|
|
|
$this->usersTabLabel = $label; |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the default user state to be applied to a user/organization association |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function getDefaultUserState(): string |
132
|
|
|
{ |
133
|
|
|
return $this->defaultUserState; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set the default user state to be applied to a user/organization association |
138
|
|
|
* |
139
|
|
|
* @param string $state |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function setDefaultUserState(string $state) |
143
|
|
|
{ |
144
|
|
|
$this->defaultUserState = $state; |
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Identify whether users should be sortable and in a sequential order. |
150
|
|
|
* |
151
|
|
|
* Note: When enabled, each association save will check to ensure an association has a proper, sequential |
152
|
|
|
* order. For large association lists, this may result in excessive processing resources. |
153
|
|
|
* |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public function getEnforceUserSortOrder(): bool |
157
|
|
|
{ |
158
|
|
|
return $this->enforceUserSortOrder; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param bool $enforce |
163
|
|
|
* @return Settings |
164
|
|
|
*/ |
165
|
|
|
public function setEnforceUserSortOrder(bool $enforce): self |
166
|
|
|
{ |
167
|
|
|
$this->enforceUserSortOrder = $enforce; |
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Identify whether organizations should be sortable and in a sequential order. |
173
|
|
|
* |
174
|
|
|
* Note: When enabled, each association save will check to ensure an association has a proper, sequential |
175
|
|
|
* order. For large association lists, this may result in excessive processing resources. |
176
|
|
|
* |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
public function getEnforceOrganizationSortOrder(): bool |
180
|
|
|
{ |
181
|
|
|
return $this->enforceOrganizationSortOrder; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param bool $enforce |
186
|
|
|
* @return Settings |
187
|
|
|
*/ |
188
|
|
|
public function setEnforceOrganizationSortOrder(bool $enforce): self |
189
|
|
|
{ |
190
|
|
|
$this->enforceOrganizationSortOrder = $enforce; |
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @inheritdoc |
196
|
|
|
*/ |
197
|
|
|
public function rules() |
198
|
|
|
{ |
199
|
|
|
return array_merge( |
200
|
|
|
parent::rules(), |
201
|
|
|
$this->fieldLayoutRules(), |
202
|
|
|
[ |
203
|
|
|
[ |
204
|
|
|
[ |
205
|
|
|
'siteSettings' |
206
|
|
|
], |
207
|
|
|
ModelValidator::class |
208
|
|
|
], |
209
|
|
|
[ |
210
|
|
|
[ |
211
|
|
|
'usersTabOrder' |
212
|
|
|
], |
213
|
|
|
'number', |
214
|
|
|
'integerOnly' => true |
215
|
|
|
], |
216
|
|
|
[ |
217
|
|
|
[ |
218
|
|
|
'enforceUserSortOrder', |
219
|
|
|
'enforceOrganizationSortOrder' |
220
|
|
|
], |
221
|
|
|
'boolean' |
222
|
|
|
], |
223
|
|
|
[ |
224
|
|
|
[ |
225
|
|
|
'siteSettings', |
226
|
|
|
'usersTabOrder', |
227
|
|
|
'usersTabLabel', |
228
|
|
|
'defaultUserState', |
229
|
|
|
'enforceUserSortOrder', |
230
|
|
|
'enforceOrganizationSortOrder' |
231
|
|
|
], |
232
|
|
|
'safe', |
233
|
|
|
'on' => [ |
234
|
|
|
static::SCENARIO_DEFAULT |
235
|
|
|
] |
236
|
|
|
] |
237
|
|
|
] |
238
|
|
|
); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @inheritdoc |
243
|
|
|
*/ |
244
|
|
|
public function attributes() |
245
|
|
|
{ |
246
|
|
|
return array_merge( |
247
|
|
|
parent::attributes(), |
248
|
|
|
$this->fieldLayoutAttributes(), |
249
|
|
|
[ |
250
|
|
|
'siteSettings', |
251
|
|
|
'usersTabOrder', |
252
|
|
|
'usersTabLabel', |
253
|
|
|
'defaultUserState', |
254
|
|
|
'enforceUserSortOrder', |
255
|
|
|
'enforceOrganizationSortOrder' |
256
|
|
|
|
257
|
|
|
] |
258
|
|
|
); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @inheritdoc |
263
|
|
|
*/ |
264
|
|
|
public function attributeLabels() |
265
|
|
|
{ |
266
|
|
|
return array_merge( |
267
|
|
|
parent::attributeLabels(), |
268
|
|
|
$this->fieldLayoutAttributeLabels(), |
269
|
|
|
[ |
270
|
|
|
'siteSettings' => Organizations::t('Site Settings'), |
271
|
|
|
'usersTabOrder' => Organizations::t('User\'s Tab Order'), |
272
|
|
|
'usersTabLabel' => Organizations::t('User\'s Tab Label'), |
273
|
|
|
'defaultUserState' => Organizations::t('Default User State'), |
274
|
|
|
'enforceUserSortOrder' => Organizations::t('Enforce User sort order'), |
275
|
|
|
'enforceOrganizationSortOrder' => Organizations::t('Enforce Organization sort order') |
276
|
|
|
] |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/******************************************* |
281
|
|
|
* SITE SETTINGS |
282
|
|
|
*******************************************/ |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param int|null $siteId |
286
|
|
|
* @return bool |
287
|
|
|
* @throws \craft\errors\SiteNotFoundException |
288
|
|
|
*/ |
289
|
|
|
public function isSiteEnabled(int $siteId = null): bool |
290
|
|
|
{ |
291
|
|
|
$siteSettings = $this->getSiteSettings(); |
292
|
|
|
return array_key_exists( |
293
|
|
|
SiteHelper::ensureSiteId($siteId), |
294
|
|
|
$siteSettings |
295
|
|
|
); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @return array |
300
|
|
|
* @throws \craft\errors\SiteNotFoundException |
301
|
|
|
*/ |
302
|
|
|
public function getEnabledSiteIds(): array |
303
|
|
|
{ |
304
|
|
|
return array_keys($this->getSiteSettings()); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|