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

Fields::ensureField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 6
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
    const TABLE_ALIAS = Domain::TABLE_ALIAS;
44
45
    /**
46
     * @param FieldInterface $field
47
     * @throws Exception
48
     */
49
    private function ensureField(FieldInterface $field)
50
    {
51
        if (!$field instanceof DomainsField) {
52
            throw new Exception(sprintf(
53
                "The field must be an instance of '%s', '%s' given.",
54
                (string)DomainsField::class,
55
                (string)get_class($field)
56
            ));
57
        }
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function getQuery(
64
        FieldInterface $field,
65
        ElementInterface $element = null
66
    ): SortableAssociationQueryInterface {
67
        /** @var DomainsField $field */
68
        $this->ensureField($field);
69
70
        $query = DomainsPlugin::getInstance()->getAssociations()->getQuery();
71
72
        $query->siteId = $this->targetSiteId($element);
73
        $query->fieldId = $field->id;
74
75
        return $query;
76
    }
77
78
    /*******************************************
79
     * NORMALIZE VALUE
80
     *******************************************/
81
82
    /**
83
     * @inheritdoc
84
     */
85
    protected function normalizeQueryInputValue(
86
        FieldInterface $field,
87
        $value,
88
        int &$sortOrder,
89
        ElementInterface $element = null
90
    ): SortableAssociationInterface {
91
        /** @var DomainsField $field */
92
        $this->ensureField($field);
93
94
        if (!is_array($value)) {
95
            $value = [
96
                'domain' => $value,
97
                'status' => $field->defaultStatus
98
            ];
99
        }
100
101
        return new Domain(
102
            [
103
                'fieldId' => $field->id,
104
                'domain' => ArrayHelper::getValue($value, 'domain'),
105
                'elementId' => $element ? $element->getId() : false,
106
                'status' => ArrayHelper::getValue($value, 'status'),
107
                'siteId' => $this->targetSiteId($element),
108
                'sortOrder' => $sortOrder++
109
            ]
110
        );
111
    }
112
113
    /**
114
     * @param DomainsField $field
115
     * @param DomainsQuery $query
116
     * @param bool $static
117
     * @return null|string
118
     * @throws Exception
119
     * @throws \Twig_Error_Loader
120
     */
121
    public function getTableHtml(DomainsField $field, DomainsQuery $query, bool $static)
122
    {
123
        $columns = [
124
            'domain' => [
125
                'heading' => 'Domain',
126
                'handle' => 'domain',
127
                'type' => 'singleline'
128
            ],
129
            'status' => [
130
                'heading' => 'Status',
131
                'handle' => 'status',
132
                'class' => 'thin',
133
                'type' => 'select',
134
                'options' => $field->getStatuses()
135
            ]
136
        ];
137
138
        // Translate the column headings
139
        foreach ($columns as &$column) {
140
            $heading = (string)$column['heading'];
141
            if ($heading !== null) {
142
                $column['heading'] = Craft::t('site', $heading);
143
            }
144
        }
145
        unset($column);
146
147
        return Craft::$app->getView()->renderTemplate(
148
            'domains/_components/fieldtypes/Domains/input',
149
            [
150
                'id' => Craft::$app->getView()->formatInputId($field->handle),
151
                'name' => $field->handle,
152
                'cols' => $columns,
153
                'rows' => $query->all(),
154
                'static' => $static
155
            ]
156
        );
157
    }
158
}
159