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\craft\hubspot\fields\actions; |
10
|
|
|
|
11
|
|
|
use craft\base\ElementInterface; |
12
|
|
|
use flipbox\craft\hubspot\fields\ObjectsFieldInterface; |
13
|
|
|
use flipbox\craft\hubspot\HubSpot; |
14
|
|
|
use flipbox\craft\integration\fields\actions\AbstractIntegrationAction; |
15
|
|
|
use flipbox\craft\integration\fields\Integrations; |
16
|
|
|
use flipbox\craft\integration\queries\IntegrationAssociationQuery; |
17
|
|
|
use yii\web\HttpException; |
18
|
|
|
|
19
|
|
|
class SyncTo extends AbstractIntegrationAction |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @inheritdoc |
23
|
|
|
*/ |
24
|
|
|
public function getTriggerLabel(): string |
25
|
|
|
{ |
26
|
|
|
return HubSpot::t('Create HubSpot Object from Element'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
public function getConfirmationMessage() |
33
|
|
|
{ |
34
|
|
|
return HubSpot::t( |
35
|
|
|
"This element will be used to create a new HubSpot Object. Please confirm to continue." |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
* @throws HttpException |
42
|
|
|
* @throws \Throwable |
43
|
|
|
*/ |
44
|
|
|
public function performAction(Integrations $field, ElementInterface $element): bool |
45
|
|
|
{ |
46
|
|
|
if (!$field instanceof ObjectsFieldInterface) { |
47
|
|
|
$this->setMessage("Invalid field type."); |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @var IntegrationAssociationQuery $query */ |
52
|
|
|
if (null === ($query = $element->getFieldValue($field->handle))) { |
53
|
|
|
throw new HttpException(400, 'Field is not associated to element'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!$field->syncToHubSpot($element)) { |
57
|
|
|
$this->setMessage("Failed to create HubSpot " . $field->getObjectLabel()); |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Reset |
62
|
|
|
$element->setFieldValue($field->handle, null); |
63
|
|
|
|
64
|
|
|
$this->id = $query->select(['objectId'])->scalar(); |
65
|
|
|
|
66
|
|
|
$this->setMessage("Sync to HubSpot executed successfully"); |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|