1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-integration/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\integration\actions\objects; |
10
|
|
|
|
11
|
|
|
use flipbox\craft\ember\actions\ManageTrait; |
12
|
|
|
use flipbox\craft\ember\helpers\SiteHelper; |
13
|
|
|
use flipbox\craft\integration\actions\ResolverTrait; |
14
|
|
|
use flipbox\craft\integration\records\IntegrationAssociation; |
15
|
|
|
use yii\base\Action; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 2.0.0 |
20
|
|
|
*/ |
21
|
|
|
class AssociateObject extends Action |
22
|
|
|
{ |
23
|
|
|
use ManageTrait, |
24
|
|
|
ResolverTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Validate that the HubSpot Object exists prior to associating |
28
|
|
|
* |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
public $validate = true; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param IntegrationAssociation $record |
35
|
|
|
* @return bool |
36
|
|
|
* @throws \Exception |
37
|
|
|
*/ |
38
|
|
|
protected function validate(IntegrationAssociation $record): bool |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
return true; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $field |
45
|
|
|
* @param string $element |
46
|
|
|
* @param string $newObjectId |
47
|
|
|
* @param string|null $objectId |
48
|
|
|
* @param int|null $siteId |
49
|
|
|
* @param int|null $sortOrder |
50
|
|
|
* @return IntegrationAssociation |
51
|
|
|
* @throws \yii\web\HttpException |
52
|
|
|
*/ |
53
|
|
|
public function run( |
54
|
|
|
string $field, |
55
|
|
|
string $element, |
56
|
|
|
string $newObjectId, |
57
|
|
|
string $objectId = null, |
58
|
|
|
int $siteId = null, |
59
|
|
|
int $sortOrder = null |
60
|
|
|
) { |
61
|
|
|
|
62
|
|
|
// Resolve Field |
63
|
|
|
$field = $this->resolveField($field); |
64
|
|
|
$element = $this->resolveElement($element); |
65
|
|
|
|
66
|
|
|
$siteId = SiteHelper::ensureSiteId($siteId ?: $element->siteId); |
67
|
|
|
|
68
|
|
|
/** @var IntegrationAssociation $recordClass */ |
69
|
|
|
$recordClass = $field::recordClass(); |
70
|
|
|
|
71
|
|
|
// Find existing? |
72
|
|
|
if (!empty($objectId)) { |
73
|
|
|
$association = $recordClass::findOne([ |
74
|
|
|
'element' => $element, |
75
|
|
|
'field' => $field, |
76
|
|
|
'objectId' => $objectId, |
77
|
|
|
'siteId' => $siteId, |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (empty($association)) { |
82
|
|
|
/** @var IntegrationAssociation $association */ |
83
|
|
|
$association = new $recordClass(); |
84
|
|
|
$association->setField($field) |
85
|
|
|
->setElement($element) |
86
|
|
|
->setSiteId(SiteHelper::ensureSiteId($siteId ?: $element->siteId)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$association->objectId = $newObjectId; |
90
|
|
|
$association->sortOrder = $sortOrder; |
91
|
|
|
|
92
|
|
|
return $this->runInternal($association); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritdoc |
97
|
|
|
* @param IntegrationAssociation $record |
98
|
|
|
* @throws \Exception |
99
|
|
|
*/ |
100
|
|
|
protected function performAction(IntegrationAssociation $record): bool |
101
|
|
|
{ |
102
|
|
|
if ($this->validate === true && !$this->validate($record)) { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $record->save(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.