Completed
Push — develop ( 43936d...b3a70f )
by Nate
02:02
created

Fields::normalizeQueryInputValue()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 24
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 18
nc 2
nop 4
crap 12
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\services;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use craft\base\FieldInterface;
14
use craft\helpers\ArrayHelper;
15
use flipbox\craft\sortable\associations\db\SortableAssociationQueryInterface;
16
use flipbox\craft\sortable\associations\records\SortableAssociationInterface;
17
use flipbox\craft\sortable\associations\services\SortableFields;
18
use flipbox\domains\db\DomainsQuery;
19
use flipbox\domains\Domains as DomainsPlugin;
20
use flipbox\domains\fields\Domains as DomainsField;
21
use flipbox\domains\records\Domain;
22
use yii\base\Exception;
23
24
/**
25
 * @author Flipbox Factory <[email protected]>
26
 * @since  1.0.0
27
 */
28
class Fields extends SortableFields
29
{
30
    /**
31
     * @inheritdoc
32
     */
33
    const SOURCE_ATTRIBUTE = Domain::SOURCE_ATTRIBUTE;
34
35
    /**
36
     * @inheritdoc
37
     */
38
    const TARGET_ATTRIBUTE = Domain::TARGET_ATTRIBUTE;
39
40
    /**
41
     * @inheritdoc
42
     */
43
    protected static function tableAlias(): string
44
    {
45
        return Domain::tableAlias();
46
    }
47
48
    /**
49
     * @param FieldInterface $field
50
     * @throws Exception
51
     */
52
    private function ensureField(FieldInterface $field)
53
    {
54
        if (!$field instanceof DomainsField) {
55
            throw new Exception(sprintf(
56
                "The field must be an instance of '%s', '%s' given.",
57
                (string)DomainsField::class,
58
                (string)get_class($field)
59
            ));
60
        }
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function getQuery(
67
        FieldInterface $field,
68
        ElementInterface $element = null
69
    ): SortableAssociationQueryInterface {
70
        /** @var DomainsField $field */
71
        $this->ensureField($field);
72
73
        $query = DomainsPlugin::getInstance()->getAssociations()->getQuery();
74
75
        $query->siteId = $this->targetSiteId($element);
76
        $query->fieldId = $field->id;
77
78
        return $query;
79
    }
80
81
    /*******************************************
82
     * NORMALIZE VALUE
83
     *******************************************/
84
85
    /**
86
     * @inheritdoc
87
     */
88
    protected function normalizeQueryInputValue(
89
        FieldInterface $field,
90
        $value,
91
        int &$sortOrder,
92
        ElementInterface $element = null
93
    ): SortableAssociationInterface {
94
        /** @var DomainsField $field */
95
        $this->ensureField($field);
96
97
        if (!is_array($value)) {
98
            $value = [
99
                'domain' => $value,
100
                'status' => $field->defaultStatus
101
            ];
102
        }
103
104
        return new Domain(
105
            [
106
                'fieldId' => $field->id,
107
                'domain' => ArrayHelper::getValue($value, 'domain'),
108
                'elementId' => $element ? $element->getId() : false,
109
                'status' => ArrayHelper::getValue($value, 'status'),
110
                'siteId' => $this->targetSiteId($element),
111
                'sortOrder' => $sortOrder++
112
            ]
113
        );
114
    }
115
116
    /**
117
     * @param DomainsField $field
118
     * @return null|string
119
     * @throws Exception
120
     * @throws \Twig_Error_Loader
121
     */
122
    public function getSettingsHtml(
123
        DomainsField $field
124
    ) {
125
        return Craft::$app->getView()->renderTemplate(
126
            'domains/_components/fieldtypes/Domains/settings',
127
            [
128
                'field' => $field
129
            ]
130
        );
131
    }
132
133
    /**
134
     * @param DomainsField $field
135
     * @param DomainsQuery $query
136
     * @param bool $static
137
     * @return null|string
138
     * @throws Exception
139
     * @throws \Twig_Error_Loader
140
     */
141
    public function getInputHtml(DomainsField $field, DomainsQuery $query, bool $static)
142
    {
143
        $columns = [
144
            'domain' => [
145
                'heading' => 'Domain',
146
                'handle' => 'domain',
147
                'type' => 'singleline'
148
            ],
149
            'status' => [
150
                'heading' => 'Status',
151
                'handle' => 'status',
152
                'class' => 'thin',
153
                'type' => 'select',
154
                'options' => $field->getStatuses()
155
            ]
156
        ];
157
158
        // Translate the column headings
159
        foreach ($columns as &$column) {
160
            $heading = (string)$column['heading'];
161
            if ($heading !== null) {
162
                $column['heading'] = Craft::t('site', $heading);
163
            }
164
        }
165
        unset($column);
166
167
        return Craft::$app->getView()->renderTemplate(
168
            'domains/_components/fieldtypes/Domains/input',
169
            [
170
                'id' => Craft::$app->getView()->formatInputId($field->handle),
171
                'name' => $field->handle,
172
                'cols' => $columns,
173
                'rows' => $query->all(),
174
                'static' => $static,
175
                'field' => $field
176
            ]
177
        );
178
    }
179
}
180