Settings::getUsersTabOrder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
            UserAssociation::STATE_INVITED => Organizations::t('Invited')
84
        ];
85
    }
86
87
    /**
88
     * Get the User tab order found on the Organization entry page.
89
     */
90
    public function getUsersTabOrder(): int
91
    {
92
        return $this->usersTabOrder;
93
    }
94
95
    /**
96
     * Set the User tab order found on the Organization entry page.
97
     *
98
     * @param int $order
99
     * @return $this
100
     */
101
    public function setUsersTabOrder(int $order)
102
    {
103
        $this->usersTabOrder = $order;
104
        return $this;
105
    }
106
107
    /**
108
     * Get the User tab label found on the Organization entry page.
109
     */
110
    public function getUsersTabLabel(): string
111
    {
112
        return $this->usersTabLabel;
113
    }
114
115
    /**
116
     * Set the User tab label found on the Organization entry page.
117
     *
118
     * @param string $label
119
     * @return $this
120
     */
121
    public function setUsersTabLabel(string $label)
122
    {
123
        $this->usersTabLabel = $label;
124
        return $this;
125
    }
126
127
    /**
128
     * Get the default user state to be applied to a user/organization association
129
     *
130
     * @return string
131
     */
132
    public function getDefaultUserState(): string
133
    {
134
        return $this->defaultUserState;
135
    }
136
137
    /**
138
     * Set the default user state to be applied to a user/organization association
139
     *
140
     * @param string $state
141
     * @return $this
142
     */
143
    public function setDefaultUserState(string $state)
144
    {
145
        $this->defaultUserState = $state;
146
        return $this;
147
    }
148
149
    /**
150
     * Identify whether users should be sortable and in a sequential order.
151
     *
152
     * Note: When enabled, each association save will check to ensure an association has a proper, sequential
153
     * order.  For large association lists, this may result in excessive processing resources.
154
     *
155
     * @return bool
156
     */
157
    public function getEnforceUserSortOrder(): bool
158
    {
159
        return $this->enforceUserSortOrder;
160
    }
161
162
    /**
163
     * @param bool $enforce
164
     * @return Settings
165
     */
166
    public function setEnforceUserSortOrder(bool $enforce): self
167
    {
168
        $this->enforceUserSortOrder = $enforce;
169
        return $this;
170
    }
171
172
    /**
173
     * Identify whether organizations should be sortable and in a sequential order.
174
     *
175
     * Note: When enabled, each association save will check to ensure an association has a proper, sequential
176
     * order.  For large association lists, this may result in excessive processing resources.
177
     *
178
     * @return bool
179
     */
180
    public function getEnforceOrganizationSortOrder(): bool
181
    {
182
        return $this->enforceOrganizationSortOrder;
183
    }
184
185
    /**
186
     * @param bool $enforce
187
     * @return Settings
188
     */
189
    public function setEnforceOrganizationSortOrder(bool $enforce): self
190
    {
191
        $this->enforceOrganizationSortOrder = $enforce;
192
        return $this;
193
    }
194
195
    /**
196
     * @inheritdoc
197
     */
198
    public function rules()
199
    {
200
        return array_merge(
201
            parent::rules(),
202
            $this->fieldLayoutRules(),
203
            [
204
                [
205
                    [
206
                        'siteSettings'
207
                    ],
208
                    ModelValidator::class
209
                ],
210
                [
211
                    [
212
                        'usersTabOrder'
213
                    ],
214
                    'number',
215
                    'integerOnly' => true
216
                ],
217
                [
218
                    [
219
                        'enforceUserSortOrder',
220
                        'enforceOrganizationSortOrder'
221
                    ],
222
                    'boolean'
223
                ],
224
                [
225
                    [
226
                        'siteSettings',
227
                        'usersTabOrder',
228
                        'usersTabLabel',
229
                        'defaultUserState',
230
                        'enforceUserSortOrder',
231
                        'enforceOrganizationSortOrder'
232
                    ],
233
                    'safe',
234
                    'on' => [
235
                        static::SCENARIO_DEFAULT
236
                    ]
237
                ]
238
            ]
239
        );
240
    }
241
242
    /**
243
     * @inheritdoc
244
     */
245
    public function attributes()
246
    {
247
        return array_merge(
248
            parent::attributes(),
249
            $this->fieldLayoutAttributes(),
250
            [
251
                'siteSettings',
252
                'usersTabOrder',
253
                'usersTabLabel',
254
                'defaultUserState',
255
                'enforceUserSortOrder',
256
                'enforceOrganizationSortOrder'
257
258
            ]
259
        );
260
    }
261
262
    /**
263
     * @inheritdoc
264
     */
265
    public function attributeLabels()
266
    {
267
        return array_merge(
268
            parent::attributeLabels(),
269
            $this->fieldLayoutAttributeLabels(),
270
            [
271
                'siteSettings' => Organizations::t('Site Settings'),
272
                'usersTabOrder' => Organizations::t('User\'s Tab Order'),
273
                'usersTabLabel' => Organizations::t('User\'s Tab Label'),
274
                'defaultUserState' => Organizations::t('Default User State'),
275
                'enforceUserSortOrder' => Organizations::t('Enforce User sort order'),
276
                'enforceOrganizationSortOrder' => Organizations::t('Enforce Organization sort order')
277
            ]
278
        );
279
    }
280
281
    /*******************************************
282
     * SITE SETTINGS
283
     *******************************************/
284
285
    /**
286
     * @param int|null $siteId
287
     * @return bool
288
     * @throws \craft\errors\SiteNotFoundException
289
     */
290
    public function isSiteEnabled(int $siteId = null): bool
291
    {
292
        $siteSettings = $this->getSiteSettings();
293
        return array_key_exists(
294
            SiteHelper::ensureSiteId($siteId),
295
            $siteSettings
296
        );
297
    }
298
299
    /**
300
     * @return array
301
     * @throws \craft\errors\SiteNotFoundException
302
     */
303
    public function getEnabledSiteIds(): array
304
    {
305
        return array_keys($this->getSiteSettings());
306
    }
307
}
308