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

UserAssociation::dissociate()   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 4
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\records;
10
11
use Craft;
12
use flipbox\craft\ember\helpers\ModelHelper;
13
use flipbox\craft\ember\records\ActiveRecord;
14
use flipbox\craft\ember\records\IdAttributeTrait;
15
use flipbox\craft\ember\records\SortableTrait;
16
use flipbox\craft\ember\records\UserAttributeTrait;
17
use flipbox\organizations\managers\UserTypeAssociationManager;
18
use flipbox\organizations\Organizations;
19
use flipbox\organizations\queries\UserAssociationQuery;
20
use yii\db\ActiveQueryInterface;
21
22
/**
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 *
26
 * @property int $organizationId
27
 * @property int $organizationOrder The order which an organization lists its users
28
 * @property int $userOrder The order which a user lists its organizations
29
 * @property string $state The user state
30
 * @property Organization $organization
31
 * @property UserType[] $types
32
 */
33
class UserAssociation extends ActiveRecord
34
{
35
    const STATE_ACTIVE = 'active';
36
    const STATE_PENDING = 'pending';
37
    const STATE_INACTIVE = 'inactive';
38
39
    use SortableTrait,
40
        UserAttributeTrait,
41
        IdAttributeTrait,
42
        OrganizationAttributeTrait;
43
44
    /**
45
     * The table name
46
     */
47
    const TABLE_ALIAS = Organization::TABLE_ALIAS . '_user_associations';
48
49
    /**
50
     * @inheritdoc
51
     */
52
    protected $getterPriorityAttributes = ['userId', 'organizationId'];
53
54
    /**
55
     * @var UserTypeAssociationManager
56
     */
57
    private $manager;
58
59
    /**
60
     * @return UserTypeAssociationManager
61
     */
62
    public function getTypeManager(): UserTypeAssociationManager
63
    {
64
        if (null === $this->manager) {
65
            $this->manager = new UserTypeAssociationManager($this);
66
        }
67
68
        return $this->manager;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function init()
75
    {
76
        parent::init();
77
78
        // Default state
79
        if (empty($this->state)) {
80
            $this->state = Organizations::getInstance()->getSettings()->getDefaultUserState();
81
        }
82
    }
83
84
    /**
85
     * @noinspection PhpDocMissingThrowsInspection
86
     *
87
     * @inheritdoc
88
     * @return UserAssociationQuery
89
     */
90
    public static function find()
91
    {
92
        /** @noinspection PhpUnhandledExceptionInspection */
93
        /** @noinspection PhpIncompatibleReturnTypeInspection */
94
        return Craft::createObject(UserAssociationQuery::class, [get_called_class()]);
95
    }
96
97
    /**
98
     * @return array
99
     */
100
    public function rules()
101
    {
102
        return array_merge(
103
            parent::rules(),
104
            $this->idRules(),
105
            $this->userRules(),
106
            $this->organizationRules(),
107
            [
108
                [
109
                    [
110
                        'userId',
111
                        'organizationId',
112
                        'state'
113
                    ],
114
                    'required'
115
                ],
116
                [
117
                    [
118
                        'state'
119
                    ],
120
                    'in',
121
                    'range' => array_keys(Organizations::getInstance()->getSettings()->getUserStates())
122
                ],
123
                [
124
                    [
125
                        'state'
126
                    ],
127
                    'default',
128
                    'value' => Organizations::getInstance()->getSettings()->getDefaultUserState()
129
                ],
130
                [
131
                    [
132
                        'userOrder',
133
                        'organizationOrder'
134
                    ],
135
                    'number',
136
                    'integerOnly' => true
137
                ],
138
                [
139
                    [
140
                        'userId',
141
                        'organizationId'
142
                    ],
143
                    'safe',
144
                    'on' => [
145
                        ModelHelper::SCENARIO_DEFAULT
146
                    ]
147
                ]
148
            ]
149
        );
150
    }
151
152
    /**
153
     * @inheritdoc
154
     */
155
    public function beforeSave($insert)
156
    {
157
        $this->ensureSortOrder(
158
            [
159
                'userId' => $this->userId
160
            ],
161
            'organizationOrder'
162
        );
163
164
        $this->ensureSortOrder(
165
            [
166
                'organizationId' => $this->organizationId
167
            ],
168
            'userOrder'
169
        );
170
171
        return parent::beforeSave($insert);
172
    }
173
174
    /**
175
     * @inheritdoc
176
     * @throws \yii\db\Exception
177
     */
178
    public function afterSave($insert, $changedAttributes)
179
    {
180
        $this->autoReOrder(
181
            'organizationId',
182
            [
183
                'userId' => $this->userId
184
            ],
185
            'organizationOrder'
186
        );
187
188
        $this->autoReOrder(
189
            'userId',
190
            [
191
                'organizationId' => $this->organizationId
192
            ],
193
            'userOrder'
194
        );
195
196
        parent::afterSave($insert, $changedAttributes);
197
    }
198
199
    /**
200
     * @inheritdoc
201
     * @throws \yii\db\Exception
202
     */
203
    public function afterDelete()
204
    {
205
        $this->sequentialOrder(
206
            'organizationId',
207
            [
208
                'userId' => $this->userId
209
            ],
210
            'organizationOrder'
211
        );
212
213
        $this->sequentialOrder(
214
            'userId',
215
            [
216
                'organizationId' => $this->organizationId
217
            ],
218
            'userOrder'
219
        );
220
221
        parent::afterDelete();
222
    }
223
224
    /**
225
     * @return ActiveQueryInterface
226
     */
227
    public function getTypes(): ActiveQueryInterface
228
    {
229
        // Todo - order this by the sortOrder
230
        /** @noinspection PhpUndefinedMethodInspection */
231
        return $this->hasMany(UserType::class, ['id' => 'typeId'])
232
            ->viaTable(
233
                UserTypeAssociation::tableName(),
234
                ['userId' => 'id']
235
            );
236
    }
237
}
238