Completed
Push — master ( 065a6e...42280b )
by Nate
11:59 queued 10:16
created

Type::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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