|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Riclep\StoryblokForms; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Riclep\Storyblok\StoryblokFacade as StoryBlok; |
|
7
|
|
|
|
|
8
|
|
|
class FormResponse |
|
9
|
|
|
{ |
|
10
|
|
|
// TODO - pass extra data - imagine a staff contact form wrapped in a component where they select the address this instance should go to |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @param Request $request |
|
15
|
|
|
*/ |
|
16
|
|
|
public function __construct(Request $request) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->request = $request; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
$this->requestPage(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function requestPage() { |
|
28
|
|
|
$this->page = Storyblok::read($this->request->input('_slug')); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return \Illuminate\Support\Collection |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function form() { |
|
35
|
|
|
return $this->page->form; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function validate() { |
|
42
|
|
|
return $this->request->validate($this->form()->validationRules(), $this->form()->errorMessages()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function validationRules() { |
|
46
|
|
|
return $this->form()->validationRules(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return mixed |
|
51
|
|
|
*/ |
|
52
|
|
|
public function response() { |
|
53
|
|
|
return $this->responses(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return false|string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function json() { |
|
60
|
|
|
return json_encode($this->response()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function responses() { |
|
67
|
|
|
$input = $this->request->except(['_token', '_slug']); |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
return $this->form()->fields->map(function ($field) use ($input) { |
|
71
|
|
|
|
|
72
|
|
|
//dd($field, $input, $field->name); |
|
73
|
|
|
|
|
74
|
|
|
return $field->response($input[$field->name]); |
|
75
|
|
|
/*dd($field); |
|
76
|
|
|
|
|
77
|
|
|
return [ |
|
78
|
|
|
'label' => $field->label, |
|
79
|
|
|
'response' => $field->response($input[$field->name] ?? ''), |
|
80
|
|
|
];*/ |
|
81
|
|
|
})->toArray(); |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/* |
|
85
|
|
|
return $this->form()->flattenFieldsets()->map(function ($field) use ($input) { |
|
86
|
|
|
return [ |
|
87
|
|
|
'label' => $field->label, |
|
88
|
|
|
'response' => $field->response($input[$field->name] ?? ''), |
|
89
|
|
|
]; |
|
90
|
|
|
})->toArray();*/ |
|
91
|
|
|
} |
|
92
|
|
|
} |