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\records\ActiveRecord; |
13
|
|
|
use flipbox\craft\ember\records\SortableTrait; |
14
|
|
|
use flipbox\organizations\Organizations; |
15
|
|
|
use flipbox\organizations\queries\OrganizationTypeAssociationQuery; |
16
|
|
|
use yii\helpers\Json; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
* |
22
|
|
|
* @property int $typeId |
23
|
|
|
* @property int $organizationId |
24
|
|
|
* @property int $sortOrder |
25
|
|
|
* @property OrganizationType $type |
26
|
|
|
* @property Organization $organization |
27
|
|
|
*/ |
28
|
|
|
class OrganizationTypeAssociation extends ActiveRecord |
29
|
|
|
{ |
30
|
|
|
use SortableTrait, |
31
|
|
|
OrganizationTypeAttributeTrait, |
32
|
|
|
OrganizationAttributeTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The table name |
36
|
|
|
*/ |
37
|
|
|
const TABLE_ALIAS = OrganizationType::TABLE_ALIAS . '_associations'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
protected $getterPriorityAttributes = ['typeId', 'organizationId']; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
46
|
|
|
* |
47
|
|
|
* @inheritdoc |
48
|
|
|
* @return OrganizationTypeAssociationQuery |
49
|
|
|
*/ |
50
|
|
|
public static function find() |
51
|
|
|
{ |
52
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
53
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
54
|
|
|
return Craft::createObject(OrganizationTypeAssociationQuery::class, [get_called_class()]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function rules() |
61
|
|
|
{ |
62
|
|
|
return array_merge( |
63
|
|
|
parent::rules(), |
64
|
|
|
$this->typeRules(), |
65
|
|
|
$this->organizationRules(), |
66
|
|
|
[ |
67
|
|
|
[ |
68
|
|
|
[ |
69
|
|
|
'typeId', |
70
|
|
|
'organizationId' |
71
|
|
|
], |
72
|
|
|
'required' |
73
|
|
|
], |
74
|
|
|
[ |
75
|
|
|
[ |
76
|
|
|
'typeId' |
77
|
|
|
], |
78
|
|
|
'unique', |
79
|
|
|
'targetAttribute' => [ |
80
|
|
|
'typeId', |
81
|
|
|
'organizationId' |
82
|
|
|
] |
83
|
|
|
] |
84
|
|
|
] |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritdoc |
90
|
|
|
*/ |
91
|
|
|
public function beforeSave($insert) |
92
|
|
|
{ |
93
|
|
|
$this->ensureSortOrder( |
94
|
|
|
[ |
95
|
|
|
'organizationId' => $this->organizationId |
96
|
|
|
] |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
return parent::beforeSave($insert); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritdoc |
104
|
|
|
* @throws \yii\db\Exception |
105
|
|
|
*/ |
106
|
|
|
public function afterSave($insert, $changedAttributes) |
107
|
|
|
{ |
108
|
|
|
try { |
109
|
|
|
$this->autoReOrder( |
110
|
|
|
'typeId', |
111
|
|
|
[ |
112
|
|
|
'organizationId' => $this->organizationId |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
} catch (\Exception $e) { |
116
|
|
|
Organizations::error( |
117
|
|
|
sprintf( |
118
|
|
|
"Exception caught while trying to reorder '%s'. Exception: [%s].", |
119
|
|
|
(string)get_class($this), |
120
|
|
|
(string)Json::encode([ |
121
|
|
|
'Trace' => $e->getTraceAsString(), |
122
|
|
|
'File' => $e->getFile(), |
123
|
|
|
'Line' => $e->getLine(), |
124
|
|
|
'Code' => $e->getCode(), |
125
|
|
|
'Message' => $e->getMessage() |
126
|
|
|
]) |
127
|
|
|
), |
128
|
|
|
__METHOD__ |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
parent::afterSave($insert, $changedAttributes); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritdoc |
137
|
|
|
* @throws \yii\db\Exception |
138
|
|
|
*/ |
139
|
|
|
public function afterDelete() |
140
|
|
|
{ |
141
|
|
|
$this->sequentialOrder( |
142
|
|
|
'typeId', |
143
|
|
|
[ |
144
|
|
|
'organizationId' => $this->organizationId |
145
|
|
|
] |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
parent::afterDelete(); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|