|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Riclep\StoryblokForms; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
|
|
8
|
|
|
class MultiInput extends Input |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Adds extra data to the JSON interpretation of the field |
|
13
|
|
|
* |
|
14
|
|
|
* @return array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected function addToJson(): array |
|
17
|
|
|
{ |
|
18
|
|
|
$json['options'] = $this->options(); |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
return $json; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Processes the options entered in Storyblok |
|
25
|
|
|
* |
|
26
|
|
|
* @return \Illuminate\Support\Collection |
|
27
|
|
|
*/ |
|
28
|
|
|
public function options(): \Illuminate\Support\Collection |
|
29
|
|
|
{ |
|
30
|
|
|
return collect(preg_split('/\r\n|\r|\n/', $this->{$this->optionsName}))->filter()->map(function ($option) { |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Parses the possible option formats into a named array |
|
34
|
|
|
* name |
|
35
|
|
|
* [*]name |
|
36
|
|
|
* [key]name |
|
37
|
|
|
* [key][*]name |
|
38
|
|
|
* */ |
|
39
|
|
|
preg_match('/(?:\[(?<value>[\w]+)\])?(?:\[(?<selected>\*)\])?(?<label>.+)/', $option, $settings); |
|
40
|
|
|
|
|
41
|
|
|
if ($settings['value'] === '') { |
|
42
|
|
|
$settings['value'] = Str::slug($settings['label']); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (request()->session()->has('_old_input')) { |
|
46
|
|
|
$settings['selected'] = $this->optionIsSelected($settings['selected']); |
|
47
|
|
|
} else { |
|
48
|
|
|
$settings['selected'] = $settings['selected'] === '*' ? true : false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return array_filter($settings, function ($key) { |
|
52
|
|
|
return is_string($key); |
|
53
|
|
|
}, ARRAY_FILTER_USE_KEY); |
|
54
|
|
|
})->values(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Checks if an option was preselected or in old input |
|
60
|
|
|
* |
|
61
|
|
|
* @param $formInput |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function optionIsSelected($formInput): bool |
|
65
|
|
|
{ |
|
66
|
|
|
return request()->old($this->input_name) && (in_array(Str::slug($formInput), Arr::wrap(request()->old($this->input_name)))); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns the Input’s response after the form has been submitted and validated |
|
71
|
|
|
* All options are returned as an array with their name and a selected boolean |
|
72
|
|
|
* based on the user’s input |
|
73
|
|
|
* |
|
74
|
|
|
* @param $input |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
public function response($input): array |
|
78
|
|
|
{ |
|
79
|
|
|
$formatted = [ |
|
80
|
|
|
'label' => $this->label, |
|
|
|
|
|
|
81
|
|
|
'name' => $this->name, |
|
|
|
|
|
|
82
|
|
|
'response' => ['selected' => [], 'unselected' => []], |
|
83
|
|
|
'type' => $this->type, |
|
|
|
|
|
|
84
|
|
|
]; |
|
85
|
|
|
|
|
86
|
|
|
$this->options()->map(function ($formInput) use ($input, &$formatted) { |
|
87
|
|
|
if (in_array($formInput['value'], Arr::wrap($input), true)) { |
|
88
|
|
|
return $formatted['response']['selected'][$formInput['value']] = $formInput['label']; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $formatted['response']['unselected'][$formInput['value']] = $formInput['label']; |
|
92
|
|
|
})->toArray(); |
|
93
|
|
|
|
|
94
|
|
|
return $formatted; |
|
95
|
|
|
} |
|
96
|
|
|
} |