Completed
Push — develop ( e0c8ea...d3f44b )
by Nate
04:31
created

Settings::setStates()   A

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
     * @return string
50
     */
51 3
    public static function siteSettingsClass(): string
52
    {
53 3
        return SiteSettings::class;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    protected static function fieldLayoutType(): string
60
    {
61
        return Organization::class;
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function getUserStates(): array
68
    {
69
        return [
70
            UserAssociation::STATE_ACTIVE => Organizations::t('Active'),
71
            UserAssociation::STATE_PENDING => Organizations::t('Pending'),
72
            UserAssociation::STATE_INACTIVE => Organizations::t('Inactive')
73
        ];
74
    }
75
76
    /**
77
     * Get the User tab order found on the Organization entry page.
78
     */
79
    public function getUsersTabOrder(): int
80
    {
81
        return $this->usersTabOrder;
82
    }
83
84
    /**
85
     * Set the User tab order found on the Organization entry page.
86
     *
87
     * @param int $order
88
     * @return $this
89
     */
90
    public function setUsersTabOrder(int $order)
91
    {
92
        $this->usersTabOrder = $order;
93
        return $this;
94
    }
95
96
    /**
97
     * Get the User tab label found on the Organization entry page.
98
     */
99
    public function getUsersTabLabel(): string
100
    {
101
        return $this->usersTabLabel;
102
    }
103
104
    /**
105
     * Set the User tab label found on the Organization entry page.
106
     *
107
     * @param string $label
108
     * @return $this
109
     */
110
    public function setUsersTabLabel(string $label)
111
    {
112
        $this->usersTabLabel = $label;
113
        return $this;
114
    }
115
116
    /**
117
     * Get the default user state to be applied to a user/organization association
118
     *
119
     * @return string
120
     */
121
    public function getDefaultUserState(): string
122
    {
123
        return $this->defaultUserState;
124
    }
125
126
    /**
127
     * Set the default user state to be applied to a user/organization association
128
     *
129
     * @param string $state
130
     * @return $this
131
     */
132
    public function setDefaultUserState(string $state)
133
    {
134
        $this->defaultUserState = $state;
135
        return $this;
136
    }
137
138
    /**
139
     * @inheritdoc
140
     */
141
    public function rules()
142
    {
143
        return array_merge(
144
            parent::rules(),
145
            $this->fieldLayoutRules(),
146
            [
147
                [
148
                    [
149
                        'siteSettings'
150
                    ],
151
                    ModelValidator::class
152
                ],
153
                [
154
                    [
155
                        'usersTabOrder'
156
                    ],
157
                    'number',
158
                    'integerOnly' => true
159
                ],
160
                [
161
                    [
162
                        'siteSettings',
163
                        'usersTabOrder',
164
                        'usersTabLabel',
165
                        'defaultUserState'
166
                    ],
167
                    'safe',
168
                    'on' => [
169
                        static::SCENARIO_DEFAULT
170
                    ]
171
                ]
172
            ]
173
        );
174
    }
175
176
    /**
177
     * @inheritdoc
178
     */
179
    public function attributes()
180
    {
181
        return array_merge(
182
            parent::attributes(),
183
            $this->fieldLayoutAttributes(),
184
            [
185
                'siteSettings',
186
                'usersTabOrder',
187
                'usersTabLabel',
188
                'defaultUserState'
189
190
            ]
191
        );
192
    }
193
194
    /**
195
     * @inheritdoc
196
     */
197
    public function attributeLabels()
198
    {
199
        return array_merge(
200
            parent::attributeLabels(),
201
            $this->fieldLayoutAttributeLabels(),
202
            [
203
                'siteSettings' => Organizations::t('Site Settings'),
204
                'usersTabOrder' => Organizations::t('User\'s Tab Order'),
205
                'usersTabLabel' => Organizations::t('User\'s Tab Label'),
206
                'defaultUserState' => Organizations::t('Default User State')
207
            ]
208
        );
209
    }
210
211
    /*******************************************
212
     * SITE SETTINGS
213
     *******************************************/
214
215
    /**
216
     * @param int|null $siteId
217
     * @return bool
218
     * @throws \craft\errors\SiteNotFoundException
219
     */
220
    public function isSiteEnabled(int $siteId = null): bool
221
    {
222
        $siteSettings = $this->getSiteSettings();
223
        return array_key_exists(
224
            SiteHelper::ensureSiteId($siteId),
225
            $siteSettings
226
        );
227
    }
228
229
    /**
230
     * @return array
231
     * @throws \craft\errors\SiteNotFoundException
232
     */
233
    public function getEnabledSiteIds(): array
234
    {
235
        return array_keys($this->getSiteSettings());
236
    }
237
}
238