Completed
Push — develop ( f8c4bd...63ca71 )
by Nate
02:47
created

Objects::getSettingsHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\criteria\CompanyCriteria;
18
use flipbox\hubspot\criteria\ContactCriteria;
19
use flipbox\hubspot\criteria\ContactListCriteria;
20
use flipbox\hubspot\criteria\ObjectCriteria;
21
use flipbox\hubspot\criteria\ObjectCriteriaInterface;
22
use flipbox\hubspot\db\ObjectAssociationQuery;
23
use flipbox\hubspot\HubSpot;
24
use flipbox\hubspot\records\ObjectAssociation;
25
use flipbox\hubspot\services\resources\Companies;
26
use flipbox\hubspot\services\resources\ContactLists;
27
use flipbox\hubspot\services\resources\Contacts;
28
29
/**
30
 * @author Flipbox Factory <[email protected]>
31
 * @since 1.0.0
32
 */
33
class Objects extends Field
34
{
35
    /**
36
     * The action event name
37
     */
38
    const EVENT_REGISTER_ACTIONS = 'registerActions';
39
40
    /**
41
     * The item action event name
42
     */
43
    const EVENT_REGISTER_ITEM_ACTIONS = 'registerItemActions';
44
45
    /**
46
     * The default HubSpot Resource Id (if none exists)
47
     */
48
    const DEFAULT_HUBSPOT_ID = 'UNKNOWN_ID';
49
50
    /**
51
     * @var string
52
     */
53
    public $resource;
54
55
    /**
56
     * @var int|null
57
     */
58
    public $min;
59
60
    /**
61
     * @var int|null
62
     */
63
    public $max;
64
65
    /**
66
     * @var string
67
     */
68
    public $viewUrl = '';
69
70
    /**
71
     * @var string
72
     */
73
    public $listUrl = '';
74
75
    /**
76
     * @var string|null
77
     */
78
    public $selectionLabel;
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public static function displayName(): string
84
    {
85
        return Craft::t('hubspot', 'HubSpot Objects');
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public static function defaultSelectionLabel(): string
92
    {
93
        return Craft::t('hubspot', 'Add a HubSpot Objects');
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    public static function hasContentColumn(): bool
100
    {
101
        return false;
102
    }
103
104
    /*******************************************
105
     * VALIDATION
106
     *******************************************/
107
108
    /**
109
     * @inheritdoc
110
     */
111
    public function getElementValidationRules(): array
112
    {
113
        return [
114
            [
115
                MinMaxValidator::class,
116
                'min' => $this->min,
117
                'max' => $this->max,
118
                'tooFew' => Craft::t(
119
                    'hubspot',
120
                    '{attribute} should contain at least {min, number} {min, plural, one{domain} other{domains}}.'
121
                ),
122
                'tooMany' => Craft::t(
123
                    'hubspot',
124
                    '{attribute} should contain at most {max, number} {max, plural, one{domain} other{domains}}.'
125
                ),
126
                'skipOnEmpty' => false
127
            ]
128
        ];
129
    }
130
131
    /*******************************************
132
     * CRITERIA
133
     *******************************************/
134
135
    /**
136
     * @return array
137
     */
138
    protected function getResourceCriteriaMap(): array
139
    {
140
        return [
141
            Companies::HUBSPOT_RESOURCE => function (ObjectAssociation $record = null) {
142
                return [
143
                    'class' => CompanyCriteria::class,
144
                    'id' => $record ? $record->objectId : self::DEFAULT_HUBSPOT_ID
145
                ];
146
            },
147
            Contacts::HUBSPOT_RESOURCE => function (ObjectAssociation $record = null) {
148
                return [
149
                    'class' => ContactCriteria::class,
150
                    'id' => $record ? $record->objectId : self::DEFAULT_HUBSPOT_ID
151
                ];
152
            },
153
            ContactLists::HUBSPOT_RESOURCE => function (ObjectAssociation $record = null) {
154
                return [
155
                    'class' => ContactListCriteria::class,
156
                    'id' => $record ? $record->objectId : self::DEFAULT_HUBSPOT_ID
157
                ];
158
            }
159
        ];
160
    }
161
162
    /**
163
     * @param ObjectAssociation|null $record
164
     * @param array $config
165
     * @return ObjectCriteriaInterface
166
     */
167
    public function createResourceCriteria(
168
        ObjectAssociation $record = null,
169
        array $config = []
1 ignored issue
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
170
    ): ObjectCriteriaInterface {
171
        $resourceMap = $this->getResourceCriteriaMap();
172
173
        $criteria = $resourceMap[$this->resource] ?? ObjectCriteria::class;
174
175
        // Closure check
176
        if (is_callable($criteria)) {
177
            $criteria = call_user_func_array($criteria, ['record' => $record]);
178
        }
179
180
        // Ensure Criteria
181
        if (!$criteria instanceof ObjectCriteriaInterface) {
182
            $criteria = \flipbox\ember\helpers\ObjectHelper::create(
183
                $criteria,
184
                ObjectCriteriaInterface::class
185
            );
186
        }
187
188
        if (!$criteria instanceof ObjectCriteriaInterface) {
189
            $criteria = new ObjectCriteria();
190
        }
191
192
        // TODO - apply the $config to the criteria
193
194
        return $criteria;
195
    }
196
197
    /*******************************************
198
     * VALUE
199
     *******************************************/
200
201
    /**
202
     * @inheritdoc
203
     */
204
    public function normalizeValue($value, ElementInterface $element = null)
205
    {
206
        return HubSpot::getInstance()->getObjectsField()->normalizeValue(
207
            $this,
208
            $value,
209
            $element
210
        );
211
    }
212
213
214
    /*******************************************
215
     * ELEMENT
216
     *******************************************/
217
218
    /**
219
     * @inheritdoc
220
     */
221
    public function modifyElementsQuery(ElementQueryInterface $query, $value)
222
    {
223
        return HubSpot::getInstance()->getObjectsField()->modifyElementsQuery(
224
            $this,
225
            $query,
226
            $value
227
        );
228
    }
229
230
231
    /*******************************************
232
     * RULES
233
     *******************************************/
234
235
    /**
236
     * @inheritdoc
237
     */
238
    public function rules()
239
    {
240
        return array_merge(
241
            parent::rules(),
242
            [
243
                [
244
                    'resource',
245
                    'required',
246
                    'message' => Craft::t('hubspot', 'Hubspot Object cannot be empty.')
247
                ],
248
                [
249
                    [
250
                        'resource',
251
                        'min',
252
                        'max',
253
                        'viewUrl',
254
                        'listUrl',
255
                        'selectionLabel'
256
                    ],
257
                    'safe',
258
                    'on' => [
259
                        ModelHelper::SCENARIO_DEFAULT
260
                    ]
261
                ]
262
            ]
263
        );
264
    }
265
266
    /*******************************************
267
     * SEARCH
268
     *******************************************/
269
270
    /**
271
     * @param ObjectAssociationQuery $value
272
     * @inheritdoc
273
     */
274
    public function getSearchKeywords($value, ElementInterface $element): string
275
    {
276
        $objects = [];
277
278
        foreach ($value->all() as $model) {
279
            array_push($objects, $model->objectId);
280
        }
281
282
        return parent::getSearchKeywords($objects, $element);
283
    }
284
285
    /*******************************************
286
     * VIEWS
287
     *******************************************/
288
289
    /**
290
     * @param ObjectAssociationQuery $value
291
     * @inheritdoc
292
     * @throws \Twig_Error_Loader
293
     * @throws \yii\base\Exception
294
     */
295
    public function getInputHtml($value, ElementInterface $element = null): string
296
    {
297
        $value->limit(null);
298
        return HubSpot::getInstance()->getObjectsField()->getInputHtml($this, $value, $element, false);
299
    }
300
301
    /**
302
     * @inheritdoc
303
     * @throws \Twig_Error_Loader
304
     * @throws \yii\base\Exception
305
     */
306
    public function getSettingsHtml()
307
    {
308
        return HubSpot::getInstance()->getObjectsField()->getSettingsHtml($this);
309
    }
310
311
    /*******************************************
312
     * EVENTS
313
     *******************************************/
314
315
    /**
316
     * @inheritdoc
317
     * @throws \Exception
318
     */
319
    public function afterElementSave(ElementInterface $element, bool $isNew)
320
    {
321
        /** @var ObjectAssociationQuery $value */
322
        $value = $element->getFieldValue($this->handle);
323
324
        HubSpot::getInstance()->getObjectAssociations()->save($value);
325
326
        parent::afterElementSave($element, $isNew);
327
    }
328
}
329