|
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\sortable\associations\records\SortableAssociationInterface; |
|
13
|
|
|
use flipbox\ember\helpers\ModelHelper; |
|
14
|
|
|
use flipbox\ember\records\traits\IdAttribute; |
|
15
|
|
|
use flipbox\ember\records\traits\UserAttribute; |
|
16
|
|
|
use flipbox\organizations\db\UserAssociationQuery; |
|
17
|
|
|
use flipbox\organizations\Organizations as OrganizationPlugin; |
|
18
|
|
|
use yii\db\ActiveQueryInterface; |
|
19
|
|
|
use flipbox\ember\records\ActiveRecord; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Flipbox Factory <[email protected]> |
|
23
|
|
|
* @since 1.0.0 |
|
24
|
|
|
* |
|
25
|
|
|
* @property int $organizationId |
|
26
|
|
|
* @property int $organizationOrder |
|
27
|
|
|
* @property int $userOrder |
|
28
|
|
|
* @property Organization $organization |
|
29
|
|
|
* @property UserType[] $types |
|
30
|
|
|
*/ |
|
31
|
|
|
class UserAssociation extends ActiveRecord implements SortableAssociationInterface |
|
32
|
|
|
{ |
|
33
|
|
|
use UserAttribute, |
|
34
|
|
|
IdAttribute, |
|
35
|
|
|
traits\OrganizationAttribute; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The table name |
|
39
|
|
|
*/ |
|
40
|
|
|
const TABLE_ALIAS = Organization::TABLE_ALIAS . '_user_associations'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
const TARGET_ATTRIBUTE = 'userId'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritdoc |
|
49
|
|
|
*/ |
|
50
|
|
|
const SOURCE_ATTRIBUTE = 'organizationId'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function find() |
|
56
|
|
|
{ |
|
57
|
|
|
return Craft::createObject(UserAssociationQuery::class, [get_called_class()]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @inheritdoc |
|
62
|
|
|
*/ |
|
63
|
|
|
public function associate(bool $autoReorder = true): bool |
|
64
|
|
|
{ |
|
65
|
|
|
return OrganizationPlugin::getInstance()->getUserAssociations()->associate( |
|
66
|
|
|
$this, |
|
67
|
|
|
$autoReorder |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritdoc |
|
73
|
|
|
*/ |
|
74
|
|
|
public function dissociate(bool $autoReorder = true): bool |
|
75
|
|
|
{ |
|
76
|
|
|
return OrganizationPlugin::getInstance()->getUserAssociations()->dissociate( |
|
77
|
|
|
$this, |
|
78
|
|
|
$autoReorder |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
public function rules() |
|
86
|
|
|
{ |
|
87
|
|
|
return array_merge( |
|
88
|
|
|
parent::rules(), |
|
89
|
|
|
$this->idRules(), |
|
90
|
|
|
$this->userRules(), |
|
91
|
|
|
$this->organizationRules(), |
|
92
|
|
|
$this->auditRules(), |
|
93
|
|
|
[ |
|
94
|
|
|
[ |
|
95
|
|
|
[ |
|
96
|
|
|
static::SOURCE_ATTRIBUTE, |
|
97
|
|
|
static::TARGET_ATTRIBUTE, |
|
98
|
|
|
], |
|
99
|
|
|
'required' |
|
100
|
|
|
], |
|
101
|
|
|
[ |
|
102
|
|
|
[ |
|
103
|
|
|
'userOrder', |
|
104
|
|
|
'organizationOrder' |
|
105
|
|
|
], |
|
106
|
|
|
'number', |
|
107
|
|
|
'integerOnly' => true |
|
108
|
|
|
], |
|
109
|
|
|
[ |
|
110
|
|
|
[ |
|
111
|
|
|
static::SOURCE_ATTRIBUTE, |
|
112
|
|
|
static::TARGET_ATTRIBUTE, |
|
113
|
|
|
], |
|
114
|
|
|
'safe', |
|
115
|
|
|
'on' => [ |
|
116
|
|
|
ModelHelper::SCENARIO_DEFAULT |
|
117
|
|
|
] |
|
118
|
|
|
] |
|
119
|
|
|
] |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return ActiveQueryInterface |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getTypes(): ActiveQueryInterface |
|
127
|
|
|
{ |
|
128
|
|
|
// Todo - order this by the sortOrder |
|
129
|
|
|
return $this->hasMany(UserType::class, ['id' => 'typeId']) |
|
130
|
|
|
->viaTable( |
|
131
|
|
|
UserTypeAssociation::tableName(), |
|
132
|
|
|
['userId' => 'id'] |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|