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

Fields::tableAlias()   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\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
     * @var DomainsField[]
42
     */
43
    private $fields = [];
44
45
    /**
46
     * @inheritdoc
47
     */
48
    protected static function tableAlias(): string
49
    {
50
        return Domain::tableAlias();
51
    }
52
53
    /**
54
     * @param int $id
55
     * @return DomainsField|null
56
     */
57
    public function findById(int $id)
58
    {
59
        if (null === ($field = ($this->fields[$id] ?? null))) {
60
            $field = Craft::$app->getFields()->getFieldById($id);
61
62
            if (!$field instanceof DomainsField) {
63
                $field = false;
64
            }
65
66
            $this->fields[$id] = $field;
67
        }
68
69
        return $field instanceof DomainsField ? $field : null;
70
    }
71
72
73
    /**
74
     * @param FieldInterface $field
75
     * @throws Exception
76
     */
77
    private function ensureField(FieldInterface $field)
78
    {
79
        if (!$field instanceof DomainsField) {
80
            throw new Exception(sprintf(
81
                "The field must be an instance of '%s', '%s' given.",
82
                (string)DomainsField::class,
83
                (string)get_class($field)
84
            ));
85
        }
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function getQuery(
92
        FieldInterface $field,
93
        ElementInterface $element = null
94
    ): SortableAssociationQueryInterface {
95
        /** @var DomainsField $field */
96
        $this->ensureField($field);
97
98
        $query = DomainsPlugin::getInstance()->getAssociations()->getQuery();
99
100
        $query->siteId = $this->targetSiteId($element);
101
        $query->fieldId = $field->id;
102
103
        return $query;
104
    }
105
106
    /*******************************************
107
     * NORMALIZE VALUE
108
     *******************************************/
109
110
    /**
111
     * @inheritdoc
112
     */
113
    protected function normalizeQueryInputValue(
114
        FieldInterface $field,
115
        $value,
116
        int &$sortOrder,
117
        ElementInterface $element = null
118
    ): SortableAssociationInterface {
119
        /** @var DomainsField $field */
120
        $this->ensureField($field);
121
122
        if (!is_array($value)) {
123
            $value = [
124
                'domain' => $value,
125
                'status' => $field->defaultStatus
126
            ];
127
        }
128
129
        return new Domain(
130
            [
131
                'fieldId' => $field->id,
132
                'domain' => ArrayHelper::getValue($value, 'domain'),
133
                'elementId' => $element ? $element->getId() : false,
134
                'status' => ArrayHelper::getValue($value, 'status'),
135
                'siteId' => $this->targetSiteId($element),
136
                'sortOrder' => $sortOrder++
137
            ]
138
        );
139
    }
140
141
    /**
142
     * @param DomainsField $field
143
     * @return null|string
144
     * @throws Exception
145
     * @throws \Twig_Error_Loader
146
     */
147
    public function getSettingsHtml(
148
        DomainsField $field
149
    ) {
150
        return Craft::$app->getView()->renderTemplate(
151
            'domains/_components/fieldtypes/Domains/settings',
152
            [
153
                'field' => $field
154
            ]
155
        );
156
    }
157
158
    /**
159
     * @param DomainsField $field
160
     * @param DomainsQuery $query
161
     * @param bool $static
162
     * @return null|string
163
     * @throws Exception
164
     * @throws \Twig_Error_Loader
165
     */
166
    public function getInputHtml(DomainsField $field, DomainsQuery $query, bool $static)
167
    {
168
        $columns = [
169
            'domain' => [
170
                'heading' => 'Domain',
171
                'handle' => 'domain',
172
                'type' => 'singleline'
173
            ],
174
            'status' => [
175
                'heading' => 'Status',
176
                'handle' => 'status',
177
                'class' => 'thin',
178
                'type' => 'select',
179
                'options' => $field->getStatuses()
180
            ]
181
        ];
182
183
        // Translate the column headings
184
        foreach ($columns as &$column) {
185
            $heading = (string)$column['heading'];
186
            if ($heading !== null) {
187
                $column['heading'] = Craft::t('site', $heading);
188
            }
189
        }
190
        unset($column);
191
192
        return Craft::$app->getView()->renderTemplate(
193
            'domains/_components/fieldtypes/Domains/input',
194
            [
195
                'id' => Craft::$app->getView()->formatInputId($field->handle),
196
                'name' => $field->handle,
197
                'cols' => $columns,
198
                'rows' => $query->all(),
199
                'static' => $static,
200
                'field' => $field
201
            ]
202
        );
203
    }
204
}
205