1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/hubspot/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/hubspot/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\hubspot\records; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use flipbox\craft\sortable\associations\records\SortableAssociation; |
13
|
|
|
use flipbox\craft\sortable\associations\services\SortableAssociations; |
14
|
|
|
use flipbox\ember\helpers\ModelHelper; |
15
|
|
|
use flipbox\ember\records\traits\ElementAttribute; |
16
|
|
|
use flipbox\ember\records\traits\SiteAttribute; |
17
|
|
|
use flipbox\hubspot\db\ObjectAssociationQuery; |
18
|
|
|
use flipbox\hubspot\fields\Objects; |
19
|
|
|
use flipbox\hubspot\HubSpot; |
20
|
|
|
use flipbox\hubspot\services\ObjectAssociations; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Flipbox Factory <[email protected]> |
24
|
|
|
* @since 1.0.0 |
25
|
|
|
* |
26
|
|
|
* @property int $fieldId |
27
|
|
|
* @property string $objectId |
28
|
|
|
*/ |
29
|
|
|
class ObjectAssociation extends SortableAssociation |
30
|
|
|
{ |
31
|
|
|
use SiteAttribute, |
32
|
|
|
ElementAttribute, |
33
|
|
|
traits\FieldAttribute; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The table alias |
37
|
|
|
*/ |
38
|
|
|
const TABLE_ALIAS = 'hubspot_objects'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
const TARGET_ATTRIBUTE = 'objectId'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
const SOURCE_ATTRIBUTE = 'elementId'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The default HubSpot Resource Id (if none exists) |
52
|
|
|
*/ |
53
|
|
|
const DEFAULT_ID = 'UNKNOWN_ID'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
|
|
protected $getterPriorityAttributes = ['fieldId', 'elementId', 'siteId']; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
* @throws \Throwable |
63
|
|
|
*/ |
64
|
|
|
public function __construct($config = []) |
65
|
|
|
{ |
66
|
|
|
HubSpot::getInstance()->getObjectAssociations()->ensureTableExists(); |
67
|
|
|
parent::__construct($config); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
72
|
|
|
* @return ObjectAssociationQuery |
73
|
|
|
*/ |
74
|
|
|
public static function find(): ObjectAssociationQuery |
75
|
|
|
{ |
76
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
77
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
78
|
|
|
return Craft::createObject(ObjectAssociationQuery::class, [get_called_class()]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
3 |
|
public static function tableAlias() |
85
|
|
|
{ |
86
|
3 |
|
return parent::tableAlias() . HubSpot::getInstance()->getSettings()->environmentTablePostfix; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
* @return ObjectAssociations |
92
|
|
|
*/ |
93
|
|
|
protected function associationService(): SortableAssociations |
94
|
|
|
{ |
95
|
|
|
return HubSpot::getInstance()->getObjectAssociations(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $criteria |
100
|
|
|
* @return mixed|null |
101
|
|
|
* @throws \yii\base\InvalidConfigException |
102
|
|
|
*/ |
103
|
|
|
public function getResource(array $criteria = []) |
104
|
|
|
{ |
105
|
|
|
if (null === ($field = $this->getField())) { |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (!$field instanceof Objects) { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$base = [ |
114
|
|
|
'connection' => $field->getConnection(), |
115
|
|
|
'cache' => $field->getCache() |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
$resource = $field->getResource(); |
119
|
|
|
|
120
|
|
|
// Can't override these... |
121
|
|
|
$criteria['id'] = $this->{self::TARGET_ATTRIBUTE} ?: self::DEFAULT_ID; |
122
|
|
|
$criteria['object'] = $field->object; |
123
|
|
|
|
124
|
|
|
return $resource->read( |
125
|
|
|
$resource->getAccessorCriteria( |
126
|
|
|
array_merge( |
127
|
|
|
$base, |
128
|
|
|
$criteria |
129
|
|
|
) |
130
|
|
|
) |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
public function rules(): array |
138
|
|
|
{ |
139
|
|
|
return array_merge( |
140
|
|
|
parent::rules(), |
141
|
|
|
$this->siteRules(), |
142
|
|
|
$this->elementRules(), |
143
|
|
|
$this->fieldRules(), |
144
|
|
|
[ |
145
|
|
|
[ |
146
|
|
|
[ |
147
|
|
|
self::TARGET_ATTRIBUTE, |
148
|
|
|
], |
149
|
|
|
'required' |
150
|
|
|
], |
151
|
|
|
[ |
152
|
|
|
self::TARGET_ATTRIBUTE, |
153
|
|
|
'unique', |
154
|
|
|
'targetAttribute' => [ |
155
|
|
|
'elementId', |
156
|
|
|
'fieldId', |
157
|
|
|
'siteId', |
158
|
|
|
self::TARGET_ATTRIBUTE |
159
|
|
|
] |
160
|
|
|
], |
161
|
|
|
[ |
162
|
|
|
[ |
163
|
|
|
self::TARGET_ATTRIBUTE |
164
|
|
|
], |
165
|
|
|
'safe', |
166
|
|
|
'on' => [ |
167
|
|
|
ModelHelper::SCENARIO_DEFAULT |
168
|
|
|
] |
169
|
|
|
] |
170
|
|
|
] |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|