Completed
Push — develop ( 136153...525928 )
by Nate
03:34
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
     * The default HubSpot Resource Id (if none exists)
55
     */
56
    const DEFAULT_HUBSPOT_ID = 'UNKNOWN_ID';
57
58
    /**
59
     * @var string
60
     */
61
    public $object;
62
63
    /**
64
     * @var int|null
65
     */
66
    public $min;
67
68
    /**
69
     * @var int|null
70
     */
71
    public $max;
72
73
    /**
74
     * @var string
75
     */
76
    public $viewUrl = '';
77
78
    /**
79
     * @var string
80
     */
81
    public $listUrl = '';
82
83
    /**
84
     * @var array
85
     */
86
    public $selectedActions = [];
87
88
    /**
89
     * @var array
90
     */
91
    public $selectedItemActions = [];
92
93
    /**
94
     * @var string|null
95
     */
96
    public $selectionLabel;
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public static function displayName(): string
102
    {
103
        return Craft::t('hubspot', 'HubSpot Objects');
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109
    public static function defaultSelectionLabel(): string
110
    {
111
        return Craft::t('hubspot', 'Add a HubSpot Objects');
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117
    public static function hasContentColumn(): bool
118
    {
119
        return false;
120
    }
121
122
    /*******************************************
123
     * VALIDATION
124
     *******************************************/
125
126
    /**
127
     * @inheritdoc
128
     */
129
    public function getElementValidationRules(): array
130
    {
131
        return [
132
            [
133
                MinMaxValidator::class,
134
                'min' => $this->min,
135
                'max' => $this->max,
136
                'tooFew' => Craft::t(
137
                    'hubspot',
138
                    '{attribute} should contain at least {min, number} {min, plural, one{domain} other{domains}}.'
139
                ),
140
                'tooMany' => Craft::t(
141
                    'hubspot',
142
                    '{attribute} should contain at most {max, number} {max, plural, one{domain} other{domains}}.'
143
                ),
144
                'skipOnEmpty' => false
145
            ]
146
        ];
147
    }
148
149
    /*******************************************
150
     * CRUD
151
     *******************************************/
152
153
    /**
154
     * @return CRUDInterface
155
     * @throws InvalidConfigException
156
     */
157
    public function getResource(): CRUDInterface
158
    {
159
        $service = HubSpot::getInstance()->getResources()->get($this->object);
160
161
        if (!$service instanceof CRUDInterface) {
162
            throw new InvalidConfigException(sprintf(
163
                "Resource must be an instance of '%s', '%s' given",
164
                CRUDInterface::class,
165
                get_class($service)
166
            ));
167
        }
168
169
        return $service;
170
    }
171
172
    /*******************************************
173
     * VALUE
174
     *******************************************/
175
176
    /**
177
     * @inheritdoc
178
     */
179
    public function normalizeValue($value, ElementInterface $element = null)
180
    {
181
        return HubSpot::getInstance()->getObjectsField()->normalizeValue(
182
            $this,
183
            $value,
184
            $element
185
        );
186
    }
187
188
189
    /*******************************************
190
     * ELEMENT
191
     *******************************************/
192
193
    /**
194
     * @inheritdoc
195
     */
196
    public function modifyElementsQuery(ElementQueryInterface $query, $value)
197
    {
198
        return HubSpot::getInstance()->getObjectsField()->modifyElementsQuery(
199
            $this,
200
            $query,
201
            $value
202
        );
203
    }
204
205
206
    /*******************************************
207
     * RULES
208
     *******************************************/
209
210
    /**
211
     * @inheritdoc
212
     */
213
    public function rules()
214
    {
215
        return array_merge(
216
            parent::rules(),
217
            [
218
                [
219
                    'object',
220
                    'required',
221
                    'message' => Craft::t('hubspot', 'Hubspot Object cannot be empty.')
222
                ],
223
                [
224
                    [
225
                        'object',
226
                        'min',
227
                        'max',
228
                        'viewUrl',
229
                        'listUrl',
230
                        'selectionLabel'
231
                    ],
232
                    'safe',
233
                    'on' => [
234
                        ModelHelper::SCENARIO_DEFAULT
235
                    ]
236
                ]
237
            ]
238
        );
239
    }
240
241
    /*******************************************
242
     * SEARCH
243
     *******************************************/
244
245
    /**
246
     * @param ObjectAssociationQuery $value
247
     * @inheritdoc
248
     */
249
    public function getSearchKeywords($value, ElementInterface $element): string
250
    {
251
        $objects = [];
252
253
        foreach ($value->all() as $model) {
254
            array_push($objects, $model->objectId);
255
        }
256
257
        return parent::getSearchKeywords($objects, $element);
258
    }
259
260
    /*******************************************
261
     * VIEWS
262
     *******************************************/
263
264
    /**
265
     * @param ObjectAssociationQuery $value
266
     * @inheritdoc
267
     * @throws \Twig_Error_Loader
268
     * @throws \yii\base\Exception
269
     */
270
    public function getInputHtml($value, ElementInterface $element = null): string
271
    {
272
        $value->limit(null);
273
        return HubSpot::getInstance()->getObjectsField()->getInputHtml($this, $value, $element, false);
274
    }
275
276
    /**
277
     * @inheritdoc
278
     * @throws \Twig_Error_Loader
279
     * @throws \yii\base\Exception
280
     */
281
    public function getSettingsHtml()
282
    {
283
        return HubSpot::getInstance()->getObjectsField()->getSettingsHtml($this);
284
    }
285
286
    /*******************************************
287
     * EVENTS
288
     *******************************************/
289
290
    /**
291
     * @inheritdoc
292
     * @throws \Exception
293
     */
294
    public function afterElementSave(ElementInterface $element, bool $isNew)
295
    {
296
        /** @var ObjectAssociationQuery $value */
297
        $value = $element->getFieldValue($this->handle);
298
299
        HubSpot::getInstance()->getObjectAssociations()->save($value);
300
301
        parent::afterElementSave($element, $isNew);
302
    }
303
}
304