|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html\FieldElements; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Arguments; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Template; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Style; |
|
10
|
|
|
|
|
11
|
|
|
class Checkbox extends AbstractFieldElement |
|
12
|
|
|
{ |
|
13
|
|
|
public function defaults(): array |
|
14
|
|
|
{ |
|
15
|
|
|
return [ |
|
16
|
|
|
'checked' => false, |
|
17
|
|
|
]; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function inputType(): string |
|
21
|
|
|
{ |
|
22
|
|
|
return in_array($this->field->original_type, ['checkbox', 'radio']) |
|
23
|
|
|
? $this->field->type |
|
24
|
|
|
: $this->field->original_type; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function required(): array |
|
28
|
|
|
{ |
|
29
|
|
|
return [ |
|
30
|
|
|
'type' => 'checkbox', |
|
31
|
|
|
]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function tag(): string |
|
35
|
|
|
{ |
|
36
|
|
|
return 'input'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function buildInput(string $value, int $index, Arguments $args): string |
|
40
|
|
|
{ |
|
41
|
|
|
$type = $this->inputType(); |
|
42
|
|
|
$input = [ |
|
43
|
|
|
'checked' => in_array($value, Cast::toArray($args->value)), |
|
44
|
|
|
'class' => $args->class, |
|
45
|
|
|
'disabled' => $args->disabled, |
|
46
|
|
|
'id' => Helper::ifTrue(!empty($args->id), "{$args->id}-{$index}"), |
|
47
|
|
|
'name' => $args->name, |
|
48
|
|
|
'required' => $args->required, |
|
49
|
|
|
'tabindex' => $args->tabindex, |
|
50
|
|
|
'type' => $args->type, |
|
51
|
|
|
'value' => $value, |
|
52
|
|
|
]; |
|
53
|
|
|
if (!empty($args['data-conditions'])) { |
|
54
|
|
|
$input['data-conditions'] = $args['data-conditions']; |
|
55
|
|
|
} |
|
56
|
|
|
$html = glsr(Template::class)->build("templates/form/type-{$type}", [ |
|
57
|
|
|
'context' => [ |
|
58
|
|
|
'class' => glsr(Style::class)->defaultClasses('field')."-{$type}", // only use the default class here! |
|
59
|
|
|
'id' => $input['id'], |
|
60
|
|
|
'input' => $this->field->builder()->input($input), |
|
61
|
|
|
'text' => $args->options[$value], |
|
62
|
|
|
], |
|
63
|
|
|
'field' => $this->field, |
|
64
|
|
|
'input' => $input, |
|
65
|
|
|
]); |
|
66
|
|
|
return glsr()->filterString('rendered/field', $html, $type, $input); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
protected function buildReviewField(Arguments $args): string |
|
70
|
|
|
{ |
|
71
|
|
|
$index = 0; |
|
72
|
|
|
$optionKeys = array_keys($args->options); |
|
73
|
|
|
return array_reduce($optionKeys, function ($carry, $value) use (&$index, $args) { |
|
74
|
|
|
return $carry.$this->buildInput((string) $value, ++$index, $args); |
|
75
|
|
|
}, ''); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function normalizeOptions(): void |
|
79
|
|
|
{ |
|
80
|
|
|
if (empty($this->field->options)) { |
|
81
|
|
|
$label = $this->field->label ?: $this->field->text; |
|
82
|
|
|
$value = $this->field->value ?: 1; |
|
83
|
|
|
$this->field->label = ''; // clear the label |
|
84
|
|
|
$this->field->text = ''; // clear the text |
|
85
|
|
|
$this->field->options = [$value => $label]; |
|
86
|
|
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
$options = []; |
|
89
|
|
|
foreach ($this->field->options as $key => $values) { |
|
90
|
|
|
if (!is_array($values)) { |
|
91
|
|
|
$options[$key] = $values; |
|
92
|
|
|
continue; |
|
93
|
|
|
} |
|
94
|
|
|
$values = array_slice(array_filter($values, 'is_string'), 0, 2); |
|
95
|
|
|
if (1 === count($values)) { |
|
96
|
|
|
$options[$key] = Cast::toString($values); |
|
97
|
|
|
} elseif (2 === count($values)) { |
|
98
|
|
|
$values = array_reduce($values, fn ($carry, $val) => $carry.$this->field->builder()->span($val), ''); |
|
99
|
|
|
$options[$key] = $values; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
$this->field->options = $options; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function normalizeValue(): void |
|
106
|
|
|
{ |
|
107
|
|
|
if (!$this->field->isMultiField()) { |
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
if ('' === $this->field->value && $this->field->checked) { |
|
111
|
|
|
$this->field->value = array_keys($this->field->options); // all options are checked |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
$this->field->value = Cast::toArray($this->field->value); // cast value to array as the field accepts multiple values |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|