Completed
Push — master ( 136153...d5a3eb )
by Nate
19:29 queued 17:38
created

Objects::getResourceCriteriaMap()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 20
cp 0
rs 9.552
c 0
b 0
f 0
cc 4
nc 1
nop 0
crap 20

1 Method

Rating   Name   Duplication   Size   Complexity  
A Objects::modifyElementsQuery() 0 8 1
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\fields;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use craft\base\Field;
14
use craft\elements\db\ElementQueryInterface;
15
use flipbox\ember\helpers\ModelHelper;
16
use flipbox\ember\validators\MinMaxValidator;
17
use flipbox\hubspot\db\ObjectAssociationQuery;
18
use flipbox\hubspot\HubSpot;
19
use flipbox\hubspot\services\resources\CRUDInterface;
20
use yii\base\InvalidConfigException;
21
22
/**
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 */
26
class Objects extends Field
27
{
28
    /**
29
     * The action event name
30
     */
31
    const EVENT_REGISTER_ACTIONS = 'registerActions';
32
33
    /**
34
     * The action event name
35
     */
36
    const EVENT_REGISTER_AVAILABLE_ACTIONS = 'registerAvailableActions';
37
38
    /**
39
     * The item action event name
40
     */
41
    const EVENT_REGISTER_ITEM_ACTIONS = 'registerItemActions';
42
43
    /**
44
     * The item action event name
45
     */
46
    const EVENT_REGISTER_AVAILABLE_ITEM_ACTIONS = 'registerAvailableItemActions';
47
48
    /**
49
     * The input template path
50
     */
51
    const INPUT_TEMPLATE_PATH = 'hubspot/_components/fieldtypes/Objects/input';
52
53
    /**
54
     * @var string
55
     */
56
    public $object;
57
58
    /**
59
     * @var int|null
60
     */
61
    public $min;
62
63
    /**
64
     * @var int|null
65
     */
66
    public $max;
67
68
    /**
69
     * @var string
70
     */
71
    public $viewUrl = '';
72
73
    /**
74
     * @var string
75
     */
76
    public $listUrl = '';
77
78
    /**
79
     * @var array
80
     */
81
    public $selectedActions = [];
82
83
    /**
84
     * @var array
85
     */
86
    public $selectedItemActions = [];
87
88
    /**
89
     * @var string|null
90
     */
91
    public $selectionLabel;
92
93
    /**
94
     * Indicates whether the full sync operation should be preformed if a matching HubSpot Object was found but not
95
     * currently associated to the element.  For example, when attempting to Sync a Craft User to a HubSpot Contact, if
96
     * the HubSpot Contact already exists; true would override data in HubSpot while false would just perform
97
     * an association (note, a subsequent sync operation could be preformed)
98
     * @var bool
99
     */
100
    public $syncToHubSpotOnMatch = false;
101
102
    /**
103
     * @inheritdoc
104
     */
105
    public static function displayName(): string
106
    {
107
        return Craft::t('hubspot', 'HubSpot Objects');
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113
    public static function defaultSelectionLabel(): string
114
    {
115
        return Craft::t('hubspot', 'Add a HubSpot Objects');
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    public static function hasContentColumn(): bool
122
    {
123
        return false;
124
    }
125
126
    /*******************************************
127
     * VALIDATION
128
     *******************************************/
129
130
    /**
131
     * @inheritdoc
132
     */
133
    public function getElementValidationRules(): array
134
    {
135
        return [
136
            [
137
                MinMaxValidator::class,
138
                'min' => $this->min,
139
                'max' => $this->max,
140
                'tooFew' => Craft::t(
141
                    'hubspot',
142
                    '{attribute} should contain at least {min, number} {min, plural, one{domain} other{domains}}.'
143
                ),
144
                'tooMany' => Craft::t(
145
                    'hubspot',
146
                    '{attribute} should contain at most {max, number} {max, plural, one{domain} other{domains}}.'
147
                ),
148
                'skipOnEmpty' => false
149
            ]
150
        ];
151
    }
152
153
    /*******************************************
154
     * CRUD
155
     *******************************************/
156
157
    /**
158
     * @return CRUDInterface
159
     * @throws InvalidConfigException
160
     */
161
    public function getResource(): CRUDInterface
162
    {
163
        $service = HubSpot::getInstance()->getResources()->get($this->object);
164
165
        if (!$service instanceof CRUDInterface) {
166
            throw new InvalidConfigException(sprintf(
167
                "Resource must be an instance of '%s', '%s' given",
168
                CRUDInterface::class,
169
                get_class($service)
170
            ));
171
        }
172
173
        return $service;
174
    }
175
176
    /*******************************************
177
     * VALUE
178
     *******************************************/
179
180
    /**
181
     * @inheritdoc
182
     */
183
    public function normalizeValue($value, ElementInterface $element = null)
184
    {
185
        return HubSpot::getInstance()->getObjectsField()->normalizeValue(
186
            $this,
187
            $value,
188
            $element
189
        );
190
    }
191
192
193
    /*******************************************
194
     * ELEMENT
195
     *******************************************/
196
197
    /**
198
     * @inheritdoc
199
     */
200
    public function modifyElementsQuery(ElementQueryInterface $query, $value)
201
    {
202
        return HubSpot::getInstance()->getObjectsField()->modifyElementsQuery(
203
            $this,
204
            $query,
205
            $value
206
        );
207
    }
208
209
210
    /*******************************************
211
     * RULES
212
     *******************************************/
213
214
    /**
215
     * @inheritdoc
216
     */
217
    public function rules()
218
    {
219
        return array_merge(
220
            parent::rules(),
221
            [
222
                [
223
                    'object',
224
                    'required',
225
                    'message' => Craft::t('hubspot', 'Hubspot Object cannot be empty.')
226
                ],
227
                [
228
                    [
229
                        'object',
230
                        'min',
231
                        'max',
232
                        'viewUrl',
233
                        'listUrl',
234
                        'selectionLabel'
235
                    ],
236
                    'safe',
237
                    'on' => [
238
                        ModelHelper::SCENARIO_DEFAULT
239
                    ]
240
                ]
241
            ]
242
        );
243
    }
244
245
    /*******************************************
246
     * SEARCH
247
     *******************************************/
248
249
    /**
250
     * @param ObjectAssociationQuery $value
251
     * @inheritdoc
252
     */
253
    public function getSearchKeywords($value, ElementInterface $element): string
254
    {
255
        $objects = [];
256
257
        foreach ($value->all() as $model) {
258
            array_push($objects, $model->objectId);
259
        }
260
261
        return parent::getSearchKeywords($objects, $element);
262
    }
263
264
    /*******************************************
265
     * VIEWS
266
     *******************************************/
267
268
    /**
269
     * @param ObjectAssociationQuery $value
270
     * @inheritdoc
271
     * @throws \Twig_Error_Loader
272
     * @throws \yii\base\Exception
273
     */
274
    public function getInputHtml($value, ElementInterface $element = null): string
275
    {
276
        $value->limit(null);
277
        return HubSpot::getInstance()->getObjectsField()->getInputHtml($this, $value, $element, false);
278
    }
279
280
    /**
281
     * @inheritdoc
282
     * @throws \Twig_Error_Loader
283
     * @throws \yii\base\Exception
284
     */
285
    public function getSettingsHtml()
286
    {
287
        return HubSpot::getInstance()->getObjectsField()->getSettingsHtml($this);
288
    }
289
290
    /*******************************************
291
     * EVENTS
292
     *******************************************/
293
294
    /**
295
     * @inheritdoc
296
     * @throws \Exception
297
     */
298
    public function afterElementSave(ElementInterface $element, bool $isNew)
299
    {
300
        /** @var ObjectAssociationQuery $value */
301
        $value = $element->getFieldValue($this->handle);
302
303
        HubSpot::getInstance()->getObjectAssociations()->save($value);
304
305
        parent::afterElementSave($element, $isNew);
306
    }
307
}
308