|
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\cp\actions\fields\traits; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\base\ElementInterface; |
|
13
|
|
|
use flipbox\hubspot\db\ObjectAssociationQuery; |
|
14
|
|
|
use flipbox\hubspot\fields\Objects; |
|
15
|
|
|
use flipbox\hubspot\records\ObjectAssociation; |
|
16
|
|
|
use yii\web\HttpException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Flipbox Factory <[email protected]> |
|
20
|
|
|
* @since 1.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
trait FieldResolverTrait |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @param string $field |
|
26
|
|
|
* @return Objects |
|
27
|
|
|
* @throws HttpException |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function resolveField(string $field): Objects |
|
30
|
|
|
{ |
|
31
|
|
|
$field = is_numeric($field) ? |
|
32
|
|
|
Craft::$app->getFields()->getFieldbyId($field) : |
|
33
|
|
|
Craft::$app->getFields()->getFieldByHandle($field); |
|
34
|
|
|
|
|
35
|
|
|
if (!$field instanceof Objects) { |
|
36
|
|
|
throw new HttpException(400, sprintf( |
|
37
|
|
|
"Field must be an instance of '%s', '%s' given.", |
|
38
|
|
|
Objects::class, |
|
39
|
|
|
get_class($field) |
|
40
|
|
|
)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $field; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param Objects $field |
|
48
|
|
|
* @param ElementInterface $element |
|
49
|
|
|
* @param string $objectId |
|
50
|
|
|
* @return ObjectAssociation |
|
51
|
|
|
* @throws HttpException |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function resolveRecord(Objects $field, ElementInterface $element, string $objectId): ObjectAssociation |
|
54
|
|
|
{ |
|
55
|
|
|
/** @var ObjectAssociationQuery $query */ |
|
56
|
|
|
if (null === ($query = $element->getFieldValue($field->handle))) { |
|
57
|
|
|
throw new HttpException(400, 'Field is not associated to element'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (null === ($criteria = $query->objectId($objectId)->one())) { |
|
61
|
|
|
throw new HttpException(400, 'Invalid value'); |
|
62
|
|
|
}; |
|
63
|
|
|
|
|
64
|
|
|
return $criteria; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|