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\elements; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use flipbox\craft\ember\helpers\QueryHelper; |
13
|
|
|
use flipbox\organizations\relationships\RelationshipInterface; |
14
|
|
|
use flipbox\organizations\relationships\OrganizationTypeRelationship; |
15
|
|
|
use flipbox\organizations\queries\OrganizationTypeQuery; |
16
|
|
|
use flipbox\organizations\records\OrganizationType; |
17
|
|
|
use Tightenco\Collect\Support\Collection; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Flipbox Factory <[email protected]> |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
* |
23
|
|
|
* @mixin Organization |
24
|
|
|
*/ |
25
|
|
|
trait TypesAttributeTrait |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var RelationshipInterface |
29
|
|
|
*/ |
30
|
|
|
private $typeManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var OrganizationType|false |
34
|
|
|
*/ |
35
|
|
|
private $activeType; |
36
|
|
|
|
37
|
|
|
/************************************************************ |
38
|
|
|
* REQUEST |
39
|
|
|
************************************************************/ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* AssociateUserToOrganization an array of types from request input |
43
|
|
|
* |
44
|
|
|
* @param string $identifier |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function setTypesFromRequest(string $identifier = 'types') |
48
|
|
|
{ |
49
|
|
|
if (null !== ($types = Craft::$app->getRequest()->getBodyParam($identifier))) { |
50
|
|
|
$this->getTypes()->clear()->add($types); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/************************************************************ |
57
|
|
|
* ACTIVE TYPE |
58
|
|
|
************************************************************/ |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param OrganizationType|null $type |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function setActiveType(OrganizationType $type = null) |
65
|
|
|
{ |
66
|
|
|
if ($type) { |
67
|
|
|
$this->getTypes()->add($type); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->activeType = (null === $type) ? false : $type; |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return OrganizationType|null |
76
|
|
|
*/ |
77
|
|
|
public function getActiveType() |
78
|
|
|
{ |
79
|
|
|
if (null === $this->activeType) { |
80
|
|
|
if (!$activeType = $this->getPrimaryType()) { |
81
|
|
|
$activeType = false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->activeType = $activeType; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return (false === $this->activeType) ? null : $this->activeType; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/************************************************************ |
92
|
|
|
* TYPES |
93
|
|
|
************************************************************/ |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get an array of types associated to an organization |
97
|
|
|
* |
98
|
|
|
* @return OrganizationTypeRelationship|RelationshipInterface |
99
|
|
|
*/ |
100
|
|
|
public function getTypes(): RelationshipInterface |
101
|
|
|
{ |
102
|
|
|
if (null === $this->typeManager) { |
103
|
|
|
$this->typeManager = new OrganizationTypeRelationship($this); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->typeManager; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set an array or query of users to an organization |
111
|
|
|
* |
112
|
|
|
* @param $types |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function setTypes($types) |
116
|
|
|
{ |
117
|
|
|
$this->getTypes()->clear()->add($types); |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/************************************************************ |
122
|
|
|
* PRIMARY TYPE |
123
|
|
|
************************************************************/ |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Identify whether a primary type is set |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
public function hasPrimaryType() |
131
|
|
|
{ |
132
|
|
|
return $this->getTypes()->getCollection()->isNotEmpty(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Identify whether the type is primary |
137
|
|
|
* |
138
|
|
|
* @param $type |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function isPrimaryType(OrganizationType $type) |
142
|
|
|
{ |
143
|
|
|
if ($primaryType = $this->getPrimaryType()) { |
144
|
|
|
return $primaryType->id === $type->id; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param OrganizationType $type |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
|
|
public function setPrimaryType(OrganizationType $type = null) |
155
|
|
|
{ |
156
|
|
|
if (null === $type) { |
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->getTypes()->getCollection()->prepend($type); |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the primary type |
167
|
|
|
* |
168
|
|
|
* @return OrganizationType|null |
169
|
|
|
*/ |
170
|
|
|
public function getPrimaryType() |
171
|
|
|
{ |
172
|
|
|
return $this->getTypes()->getCollection()->first(); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|