Completed
Push — develop ( 43936d...b3a70f )
by Nate
02:02
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('domains', '{attribute} should contain at least {min, number} {min, plural, one{domain} other{domains}}.'),
92
                'tooMany' => Craft::t('domains', '{attribute} should contain at most {max, number} {max, plural, one{domain} other{domains}}.'),
93
                'skipOnEmpty' => false
94
            ]
95
        ];
96
    }
97
98
99
    /**
100
     * @inheritdoc
101
     */
102
    public function modifyElementsQuery(ElementQueryInterface $query, $value)
103
    {
104
        return DomainsPlugin::getInstance()->getFields()->modifyElementsQuery($this, $query, $value);
105
    }
106
107
    /**
108
     * @inheritdoc
109
     * @return DomainsQuery
110
     */
111
    public function normalizeValue($value, ElementInterface $element = null)
112
    {
113
        return DomainsPlugin::getInstance()->getFields()->normalizeValue($this, $value, $element);
114
    }
115
116
117
    /**
118
     * @param DomainsQuery $value
119
     * @inheritdoc
120
     */
121
    public function getSearchKeywords($value, ElementInterface $element): string
122
    {
123
        $domains = [];
124
125
        foreach ($value->all() as $association) {
126
            array_push($domains, $association->domain);
127
        }
128
129
        return parent::getSearchKeywords($domains, $element);
130
    }
131
132
133
    /*******************************************
134
     * VIEWS
135
     *******************************************/
136
137
    /**
138
     * @inheritdoc
139
     */
140
    public function getSettingsHtml()
141
    {
142
        return DomainsPlugin::getInstance()->getFields()->getSettingsHtml($this);
143
    }
144
145
    /**
146
     * @param DomainsQuery $value
147
     * @inheritdoc
148
     */
149
    public function getInputHtml($value, ElementInterface $element = null): string
150
    {
151
        return DomainsPlugin::getInstance()->getFields()->getInputHtml($this, $value, false);
152
    }
153
154
155
    /*******************************************
156
     * EVENTS
157
     *******************************************/
158
159
    /**
160
     * @inheritdoc
161
     */
162
    public function afterElementSave(ElementInterface $element, bool $isNew)
163
    {
164
        DomainsPlugin::getInstance()->getAssociations()->save(
165
            $element->{$this->handle}
166
        );
167
168
        parent::afterElementSave($element, $isNew);
169
    }
170
}
171