Completed
Push — master ( e1fcb3...04b0bc )
by Nate
04:43 queued 03:24
created

Domains::getSettingsHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/domains/license
6
 * @link       https://www.flipboxfactory.com/software/domains/
7
 */
8
9
namespace flipbox\domains\fields;
10
11
use Craft;
12
use craft\base\Element;
13
use craft\base\ElementInterface;
14
use craft\base\Field;
15
use craft\base\FieldInterface;
16
use craft\elements\db\ElementQueryInterface;
17
use craft\validators\ArrayValidator;
18
use flipbox\domains\db\DomainsQuery;
19
use flipbox\domains\Domains as DomainsPlugin;
20
use flipbox\domains\validators\DomainsValidator;
21
use flipbox\domains\validators\MinMaxValidator;
22
23
/**
24
 * @author Flipbox Factory <[email protected]>
25
 * @since 1.0.0
26
 */
27
class Domains extends Field implements FieldInterface
28
{
29
    /**
30
     * @var bool
31
     */
32
    public $unique = true;
33
34
    /**
35
     * @var int|null
36
     */
37
    public $min;
38
39
    /**
40
     * @var int|null
41
     */
42
    public $max;
43
44
    /**
45
     * @var string
46
     */
47
    public $defaultStatus = 'pending';
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public static function displayName(): string
53
    {
54
        return Craft::t('domains', 'Domains');
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public static function getStatuses(): array
61
    {
62
        return [
63
            'enabled' => Craft::t('domains', 'Enabled'),
64
            'pending' => Craft::t('domains', 'Pending'),
65
            'disabled' => Craft::t('domains', 'Disabled')
66
        ];
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public static function hasContentColumn(): bool
73
    {
74
        return false;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function getElementValidationRules(): array
81
    {
82
        return [
83
            [
84
                DomainsValidator::class,
85
                'field' => $this
86
            ],
87
            [
88
                MinMaxValidator::class,
89
                'min' => $this->min,
90
                'max' => $this->max,
91
                'tooFew' => Craft::t(
92
                    'domains',
93
                    '{attribute} should contain at least {min, number} {min, plural, one{domain} other{domains}}.'
94
                ),
95
                'tooMany' => Craft::t(
96
                    'domains',
97
                    '{attribute} should contain at most {max, number} {max, plural, one{domain} other{domains}}.'
98
                ),
99
                'skipOnEmpty' => false
100
            ]
101
        ];
102
    }
103
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function modifyElementsQuery(ElementQueryInterface $query, $value)
109
    {
110
        return DomainsPlugin::getInstance()->getFields()->modifyElementsQuery($this, $query, $value);
111
    }
112
113
    /**
114
     * @inheritdoc
115
     * @return DomainsQuery
116
     */
117
    public function normalizeValue($value, ElementInterface $element = null)
118
    {
119
        return DomainsPlugin::getInstance()->getFields()->normalizeValue($this, $value, $element);
120
    }
121
122
123
    /**
124
     * @param DomainsQuery $value
125
     * @inheritdoc
126
     */
127
    public function getSearchKeywords($value, ElementInterface $element): string
128
    {
129
        $domains = [];
130
131
        foreach ($value->all() as $association) {
132
            array_push($domains, $association->domain);
133
        }
134
135
        return parent::getSearchKeywords($domains, $element);
136
    }
137
138
139
    /*******************************************
140
     * VIEWS
141
     *******************************************/
142
143
    /**
144
     * @inheritdoc
145
     */
146
    public function getSettingsHtml()
147
    {
148
        return DomainsPlugin::getInstance()->getFields()->getSettingsHtml($this);
149
    }
150
151
    /**
152
     * @param DomainsQuery $value
153
     * @inheritdoc
154
     */
155
    public function getInputHtml($value, ElementInterface $element = null): string
156
    {
157
        return DomainsPlugin::getInstance()->getFields()->getInputHtml($this, $value, false);
158
    }
159
160
161
    /*******************************************
162
     * EVENTS
163
     *******************************************/
164
165
    /**
166
     * @inheritdoc
167
     */
168
    public function afterElementSave(ElementInterface $element, bool $isNew)
169
    {
170
        DomainsPlugin::getInstance()->getAssociations()->save(
171
            $element->{$this->handle},
172
            false
173
        );
174
175
        parent::afterElementSave($element, $isNew);
176
    }
177
}
178