|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Riclep\StoryblokForms; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Http\UploadedFile; |
|
7
|
|
|
use Illuminate\Support\Arr; |
|
8
|
|
|
use Riclep\Storyblok\StoryblokFacade as StoryBlok; |
|
9
|
|
|
use Riclep\StoryblokForms\Blocks\LsfFieldset; |
|
10
|
|
|
|
|
11
|
|
|
class FormResponse |
|
12
|
|
|
{ |
|
13
|
|
|
// TODO - pass extra data - imagine a staff contact form wrapped in a component where they select the email address this instance should go to |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param Request $request |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct(Request $request) |
|
20
|
|
|
{ |
|
21
|
|
|
// convert JSON response to same format as standard HTML forms |
|
22
|
|
|
if ($request->isJson()) { |
|
23
|
|
|
$request->replace(Arr::undot($request->all())); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$this->request = $request; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
$this->requestPage(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function requestPage() { |
|
36
|
|
|
$this->page = Storyblok::read($this->request->input('_slug')); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return \Illuminate\Support\Collection |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function form() { |
|
43
|
|
|
return $this->page->form; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
|
|
public function validate() { |
|
50
|
|
|
return $this->request->validate($this->form()->validationRules(), $this->form()->errorMessages()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function validationRules() { |
|
54
|
|
|
return $this->form()->validationRules(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
public function fields() { |
|
61
|
|
|
return $this->responses(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return false|string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function json() { |
|
68
|
|
|
return json_encode($this->fields()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function responses() { |
|
75
|
|
|
$input = $this->request->except(['_token', '_slug']); |
|
76
|
|
|
|
|
77
|
|
|
return $this->form()->fields->map(function ($field) use ($input) { |
|
78
|
|
|
|
|
79
|
|
|
// Handle empty radio buttons sending nothing in POST request |
|
80
|
|
|
//if ($field instanceof \Riclep\StoryblokForms\Blocks\LsfRadioButton) { |
|
81
|
|
|
if (!array_key_exists($field->name, $input)) { |
|
82
|
|
|
$input[$field->name] = null; |
|
83
|
|
|
} |
|
84
|
|
|
//} |
|
85
|
|
|
|
|
86
|
|
|
return $field->response($input[$field->name]); |
|
87
|
|
|
})->toArray(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Flattens the response returning an array of field responses |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
public function flatten() { |
|
97
|
|
|
return Arr::flatten($this->fields()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function files() { |
|
101
|
|
|
return array_values(array_filter($this->flatten(), function ($response) { |
|
102
|
|
|
return $response instanceof UploadedFile; |
|
103
|
|
|
})); |
|
104
|
|
|
} |
|
105
|
|
|
} |