Completed
Push — master ( 67ce08...91cbf1 )
by Nate
05:42
created

OrganizationType::resolveFieldLayout()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
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\records;
10
11
use Craft;
12
use flipbox\ember\helpers\ObjectHelper;
13
use flipbox\ember\records\ActiveRecordWithId;
14
use flipbox\ember\records\traits\FieldLayoutAttribute;
15
use flipbox\ember\traits\HandleRules;
16
use flipbox\ember\validators\ModelValidator;
17
use flipbox\organizations\db\OrganizationTypeQuery;
18
use flipbox\organizations\Organizations as OrganizationPlugin;
19
use yii\db\ActiveQueryInterface;
20
use yii\validators\UniqueValidator;
21
22
/**
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 *
26
 * @method FieldLayoutModel parentResolveFieldLayout()
27
 * @property string $name
28
 * @property OrganizationTypeSiteSettings[] $siteSettingRecords
29
 */
30
class OrganizationType extends ActiveRecordWithId
31
{
32
    use FieldLayoutAttribute,
33
        HandleRules {
34
        resolveFieldLayout as parentResolveFieldLayout;
35
    }
36
37
    /**
38
     * The table name
39
     */
40
    const TABLE_ALIAS = Organization::TABLE_ALIAS . '_types';
41
42
    /**
43
     * @inheritdoc
44
     */
45
    protected $getterPriorityAttributes = ['fieldLayoutId'];
46
47
    /**
48
     * @inheritdoc
49
     */
50
    protected static function fieldLayoutType(): string
51
    {
52
        return self::class;
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    protected function resolveFieldLayout()
59
    {
60
        if (null === ($fieldLayout = $this->parentResolveFieldLayout())) {
61
            $fieldLayout = OrganizationPlugin::getInstance()->getSettings()->getFieldLayout();
62
        }
63
64
        return $fieldLayout;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     * @return OrganizationTypeQuery
70
     */
71
    public static function find()
72
    {
73
        return new OrganizationTypeQuery;
74
    }
75
76
    /*******************************************
77
     * EVENTS
78
     *******************************************/
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function beforeSave($insert)
84
    {
85
        if (false === OrganizationPlugin::getInstance()->getOrganizationTypes()->beforeSave($this)) {
86
            return false;
87
        }
88
89
        return parent::beforeSave($insert);
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95
    public function afterSave($insert, $changedAttributes)
96
    {
97
        OrganizationPlugin::getInstance()->getOrganizationTypes()->afterSave($this);
98
        parent::afterSave($insert, $changedAttributes);
99
    }
100
101
    /*******************************************
102
     * SITE SETTINGS
103
     *******************************************/
104
105
    /**
106
     * @return OrganizationTypeSiteSettings[]
107
     * @throws \craft\errors\SiteNotFoundException
108
     */
109
    public function getSiteSettings(): array
110
    {
111
        if (empty($this->siteSettingRecords)) {
112
            $this->addPrimarySiteSettings();
113
        }
114
115
        return $this->siteSettingRecords;
116
    }
117
118
    /**
119
     * @param array $siteSettings
120
     * @return $this
121
     */
122
    public function setSiteSettings(array $siteSettings = [])
123
    {
124
        foreach ($siteSettings as $siteId => &$site) {
125
            $site = $this->resolveSiteSettings($siteId, $site);
126
        }
127
128
        $this->populateRelation('siteSettingRecords', $siteSettings);
129
        return $this;
130
    }
131
132
    /**
133
     * @return $this
134
     * @throws \craft\errors\SiteNotFoundException
135
     */
136
    protected function addPrimarySiteSettings()
137
    {
138
        $primarySite = Craft::$app->getSites()->getPrimarySite();
139
140
        if ($primarySite->id !== null) {
141
            $this->addSiteSettings($primarySite->id, ['site' => $primarySite]);
142
        }
143
144
        return $this;
145
    }
146
147
    /**
148
     * @param int $siteId
149
     * @param $site
150
     * @return OrganizationTypeSiteSettings
151
     */
152
    protected function resolveSiteSettings(int $siteId, $site): OrganizationTypeSiteSettings
153
    {
154
        if (!$record = $this->siteSettingRecords[$siteId] ?? null) {
155
            $record = new OrganizationTypeSiteSettings();
156
        }
157
158
        return ObjectHelper::populate(
159
            $record,
160
            $site
161
        );
162
    }
163
164
    /**
165
     * @param int $siteId
166
     * @param $site
167
     * @return $this
168
     */
169
    protected function addSiteSettings(int $siteId, $site)
170
    {
171
        $site = $this->resolveSiteSettings($siteId, $site);
172
        $this->populateRelation('siteSettingRecords', (
173
            $this->siteSettingRecords +
174
            [
175
                $site->getSiteId() => $site
176
            ]
177
        ));
178
179
        return $this;
180
    }
181
182
183
    /**
184
     * @inheritdoc
185
     */
186
    public function rules()
187
    {
188
        return array_merge(
189
            parent::rules(),
190
            $this->handleRules(),
191
            $this->fieldLayoutRules(),
192
            [
193
                [
194
                    [
195
                        'name'
196
                    ],
197
                    'required'
198
                ],
199
                [
200
                    [
201
                        'siteSettings'
202
                    ],
203
                    ModelValidator::class
204
                ],
205
                [
206
                    [
207
                        'name',
208
                    ],
209
                    'string',
210
                    'max' => 255
211
                ],
212
                [
213
                    [
214
                        'handle'
215
                    ],
216
                    UniqueValidator::class
217
                ]
218
            ]
219
        );
220
    }
221
222
    /**
223
     * Returns the type’s site settings.
224
     *
225
     * @return ActiveQueryInterface The relational query object.
226
     */
227
    protected function getSiteSettingRecords(): ActiveQueryInterface
228
    {
229
        return $this->hasMany(OrganizationTypeSiteSettings::class, ['typeId' => 'id'])
230
            ->where([
231
                'siteId' => OrganizationPlugin::getInstance()->getSettings()->getEnabledSiteIds()
232
            ])
233
            ->indexBy('siteId');
234
    }
235
236
    /**
237
     * @inheritdoc
238
     */
239
    public function __toString()
240
    {
241
        return (string) $this->getAttribute('name');
242
    }
243
}
244