Completed
Push — develop ( 4b1148...27b476 )
by Nate
04:35
created

Settings::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 7
cp 0
rs 9.536
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;
12
use craft\base\Model;
13
use flipbox\craft\ember\helpers\ModelHelper;
14
use flipbox\craft\ember\helpers\SiteHelper;
15
use flipbox\craft\ember\models\FieldLayoutAttributeTrait;
16
use flipbox\craft\ember\validators\ModelValidator;
17
use flipbox\organizations\elements\Organization;
18
use yii\caching\Dependency;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class Settings extends Model
25
{
26
    use FieldLayoutAttributeTrait,
27
        SiteSettingAttributeTrait;
28
29
    /**
30
     * @var int|null|false
31
     */
32
    public $userSidebarTemplate = 'organizations/_components/hooks/users/details';
33
34
    /**
35
     * @var int
36
     */
37
    private $usersTabOrder = 2;
38
39
    /**
40
     * @var string
41
     */
42
    private $usersTabLabel = 'Users';
43
44
    /**
45
     * @return string
46
     */
47 3
    public static function siteSettingsClass(): string
48
    {
49 3
        return SiteSettings::class;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    protected static function fieldLayoutType(): string
56
    {
57
        return Organization::class;
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function getUserStates(): array
64
    {
65
        return [
66
            'active' => Craft::t('organizations', 'Active'),
67
            'pending' => Craft::t('organizations', 'Pending'),
68
            'inactive' => Craft::t('organizations', 'InActive')
69
        ];
70
    }
71
72
    /**
73
     * Get the User tab order found on the Organization entry page.
74
     */
75
    public function getUsersTabOrder(): int
76
    {
77
        return $this->usersTabOrder;
78
    }
79
80
    /**
81
     * Set the User tab order found on the Organization entry page.
82
     *
83
     * @param int $order
84
     * @return $this
85
     */
86
    public function setUsersTabOrder(int $order)
87
    {
88
        $this->usersTabOrder = $order;
89
        return $this;
90
    }
91
92
    /**
93
     * Get the User tab label found on the Organization entry page.
94
     */
95
    public function getUsersTabLabel(): string
96
    {
97
        return $this->usersTabLabel;
98
    }
99
100
    /**
101
     * Set the User tab label found on the Organization entry page.
102
     *
103
     * @param string $label
104
     * @return $this
105
     */
106
    public function setUsersTabLabel(string $label)
107
    {
108
        $this->usersTabLabel = $label;
109
        return $this;
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getDefaultUserState(): string
116
    {
117
        return 'active';
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123
    public function rules()
124
    {
125
        return array_merge(
126
            parent::rules(),
127
            $this->fieldLayoutRules(),
128
            [
129
                [
130
                    [
131
                        'siteSettings'
132
                    ],
133
                    ModelValidator::class
134
                ],
135
                [
136
                    [
137
                        'siteSettings'
138
                    ],
139
                    'safe',
140
                    'on' => [
141
                        ModelHelper::SCENARIO_DEFAULT
142
                    ]
143
                ]
144
            ]
145
        );
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function attributes()
152
    {
153
        return array_merge(
154
            parent::attributes(),
155
            $this->fieldLayoutAttributes(),
156
            [
157
                'siteSettings'
158
            ]
159
        );
160
    }
161
162
    /**
163
     * @inheritdoc
164
     */
165
    public function attributeLabels()
166
    {
167
        return array_merge(
168
            parent::attributeLabels(),
169
            $this->fieldLayoutAttributeLabels(),
170
            [
171
                'siteSettings' => Craft::t('organizations', 'Site Settings')
172
            ]
173
        );
174
    }
175
176
177
    /*******************************************
178
     * STATES
179
     *******************************************/
180
181
    /**
182
     * @return $this
183
     * @deprecated
184
     */
185
    public function setStates()
186
    {
187
        return $this;
188
    }
189
190
    /**
191
     * @return $this
192
     * @deprecated
193
     */
194
    public function setDefaultStates()
195
    {
196
        return $this;
197
    }
198
199
200
    /*******************************************
201
     * SITE SETTINGS
202
     *******************************************/
203
204
    /**
205
     * @param int|null $siteId
206
     * @return bool
207
     * @throws \craft\errors\SiteNotFoundException
208
     */
209
    public function isSiteEnabled(int $siteId = null): bool
210
    {
211
        $siteSettings = $this->getSiteSettings();
212
        return array_key_exists(
213
            SiteHelper::ensureSiteId($siteId),
214
            $siteSettings
215
        );
216
    }
217
218
    /**
219
     * @return array
220
     * @throws \craft\errors\SiteNotFoundException
221
     */
222
    public function getEnabledSiteIds(): array
223
    {
224
        return array_keys($this->getSiteSettings());
225
    }
226
}
227