Completed
Push — master ( e219aa...e1fcb3 )
by Nate
06:24 queued 05:08
created

Domains::modifyElementsQuery()   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 2
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\ElementInterface;
13
use craft\base\Field;
14
use craft\base\FieldInterface;
15
use craft\elements\db\ElementQueryInterface;
16
use flipbox\domains\db\DomainsQuery;
17
use flipbox\domains\Domains as DomainsPlugin;
18
use flipbox\domains\validators\DomainsValidator;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class Domains extends Field implements FieldInterface
25
{
26
    /**
27
     * @var bool
28
     */
29
    public $unique = true;
30
31
    /**
32
     * @var int|null The maximum number of relations this field can have (used if [[allowLimit]] is set to true)
33
     */
34
    public $limit;
35
36
    /**
37
     * @var bool Whether to allow the Limit setting
38
     */
39
    public $allowLimit = true;
40
41
    /**
42
     * @var string
43
     */
44
    public $defaultStatus = 'pending';
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public static function displayName(): string
50
    {
51
        return Craft::t('domains', 'Domains');
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public static function getStatuses(): array
58
    {
59
        return [
60
            'enabled' => Craft::t('domains', 'Enabled'),
61
            'pending' => Craft::t('domains', 'Pending'),
62
            'disabled' => Craft::t('domains', 'Disabled')
63
        ];
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public static function hasContentColumn(): bool
70
    {
71
        return false;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function getElementValidationRules(): array
78
    {
79
        $rules = parent::getElementValidationRules();
80
81
        $rules[] = [
82
            DomainsValidator::class,
83
            'field' => $this
84
        ];
85
86
        return $rules;
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function modifyElementsQuery(ElementQueryInterface $query, $value)
93
    {
94
        return DomainsPlugin::getInstance()->getFields()->modifyElementsQuery($this, $query, $value);
95
    }
96
97
    /**
98
     * @inheritdoc
99
     * @return DomainsQuery
100
     */
101
    public function normalizeValue($value, ElementInterface $element = null)
102
    {
103
        return DomainsPlugin::getInstance()->getFields()->normalizeValue($this, $value, $element);
104
    }
105
106
107
    /**
108
     * @param DomainsQuery $value
109
     * @inheritdoc
110
     */
111
    public function getSearchKeywords($value, ElementInterface $element): string
112
    {
113
        $domains = [];
114
115
        foreach ($value->all() as $association) {
116
            array_push($domains, $association->domain);
117
        }
118
119
        return parent::getSearchKeywords($domains, $element);
120
    }
121
122
123
    /*******************************************
124
     * VIEWS
125
     *******************************************/
126
127
    /**
128
     * @param DomainsQuery $value
129
     * @inheritdoc
130
     */
131
    public function getInputHtml($value, ElementInterface $element = null): string
132
    {
133
        return DomainsPlugin::getInstance()->getFields()->getTableHtml($this, $value, false);
134
    }
135
136
137
    /*******************************************
138
     * EVENTS
139
     *******************************************/
140
141
    /**
142
     * @inheritdoc
143
     */
144
    public function afterElementSave(ElementInterface $element, bool $isNew)
145
    {
146
        DomainsPlugin::getInstance()->getAssociations()->save(
147
            $element->getFieldValue($this->handle)
148
        );
149
150
        parent::afterElementSave($element, $isNew);
151
    }
152
}
153