AbstractType::settingsHtml()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://raw.githubusercontent.com/flipboxfactory/craft-link/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-link
7
 */
8
9
namespace flipbox\craft\link\types;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
use flipbox\craft\link\fields\Link;
14
use yii\base\Model;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
abstract class AbstractType extends Model implements TypeInterface
21
{
22
    use BaseTrait;
23
24
    /**
25
     * The scenario used to validate input data
26
     */
27
    const SCENARIO_INPUT = 'input';
28
29
    /**
30
     * The base template path to the field type templates
31
     */
32
    const BASE_TEMPLATE_PATH = 'link/_components/fieldtypes/Link/types';
33
34
    /**
35
     * The settings template path
36
     */
37
    const SETTINGS_TEMPLATE_PATH = self::BASE_TEMPLATE_PATH . '/settings';
38
39
    /**
40
     * The input template path
41
     */
42
    const INPUT_TEMPLATE_PATH = self::BASE_TEMPLATE_PATH . '/input';
43
44
    /**
45
     * @inheritdoc
46
     * @throws \ReflectionException
47
     */
48
    public static function displayName(): string
49
    {
50
        $ref = new \ReflectionClass(static::class);
51
        return Craft::t('link', $ref->getShortName());
52
    }
53
54
    /**
55
     * @inheritdoc
56
     * @throws \Twig_Error_Loader
57
     * @throws \yii\base\Exception
58
     */
59
    public function settingsHtml(): string
60
    {
61
        return Craft::$app->getView()->renderTemplate(
62
            static::SETTINGS_TEMPLATE_PATH,
63
            [
64
                'type' => $this
65
            ]
66
        );
67
    }
68
69
    /**
70
     * Populate valid properties.  This occurs when we have a content value
71
     * and we need to populate it's contents on an existing TypeInterface
72
     *
73
     * @param array $properties
74
     */
75
    public function populate(array $properties)
76
    {
77
        // If the override text is empty, don't set it
78
        if ($overrideText = ArrayHelper::remove(
79
            $properties,
80
            'overrideText'
81
        )
82
        ) {
83
            $properties['overrideText'] = $overrideText;
84
        }
85
86
        foreach ($this->getProperties() as $key => $value) {
87
            if (array_key_exists($key, $properties)) {
88
                $this->{$key} = $properties[$key];
89
            }
90
        }
91
    }
92
93
    /**
94
     * @inheritdoc
95
     * @throws \Twig_Error_Loader
96
     * @throws \yii\base\Exception
97
     */
98
    public function inputHtml(Link $field): string
99
    {
100
        return Craft::$app->getView()->renderTemplate(
101
            static::INPUT_TEMPLATE_PATH,
102
            [
103
                'field' => $field,
104
                'type' => $this
105
            ]
106
        );
107
    }
108
109
    /**
110
     * @return bool
111
     */
112
    public function validateInput(): bool
113
    {
114
        $currentScenario = $this->getScenario();
115
        $this->setScenario(self::SCENARIO_INPUT);
116
117
        $validates = $this->validate();
118
119
        $this->setScenario($currentScenario);
120
        return $validates;
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126
    public function rules()
127
    {
128
        return array_merge(
129
            parent::rules(),
130
            [
131
                [
132
                    [
133
                        'overrideText'
134
                    ],
135
                    'required',
136
                    'when' => function (self $model) {
137
                        return $model->getSettings()['requireText'] == true;
138
                    },
139
                    'message' => \flipbox\craft\link\Link::t('Link text is required'),
140
                    'on' => [
141
                        self::SCENARIO_INPUT
142
                    ]
143
                ],
144
                [
145
                    [
146
                        'identifier',
147
                        'overrideText',
148
                        'target'
149
                    ],
150
                    'safe',
151
                    'on' => [
152
                        Model::SCENARIO_DEFAULT
153
                    ]
154
                ]
155
            ]
156
        );
157
    }
158
}
159