1
|
|
|
<?php namespace Anomaly\Streams\Platform\Ui\Form\Multiple; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface; |
4
|
|
|
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface; |
5
|
|
|
use Anomaly\Streams\Platform\Field\Contract\FieldInterface; |
6
|
|
|
use Anomaly\Streams\Platform\Model\EloquentModel; |
7
|
|
|
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface; |
8
|
|
|
use Anomaly\Streams\Platform\Ui\Form\Form; |
9
|
|
|
use Anomaly\Streams\Platform\Ui\Form\FormBuilder; |
10
|
|
|
use Anomaly\Streams\Platform\Ui\Form\FormCollection; |
11
|
|
|
use Anomaly\Streams\Platform\Ui\Form\Multiple\Command\BuildForms; |
12
|
|
|
use Anomaly\Streams\Platform\Ui\Form\Multiple\Command\HandleErrors; |
13
|
|
|
use Anomaly\Streams\Platform\Ui\Form\Multiple\Command\MergeFields; |
14
|
|
|
use Anomaly\Streams\Platform\Ui\Form\Multiple\Command\PostForms; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class MultipleFormBuilder |
18
|
|
|
* |
19
|
|
|
* @link http://anomaly.is/streams-platform |
20
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
21
|
|
|
* @author Ryan Thompson <[email protected]> |
22
|
|
|
* @package Anomaly\Streams\Platform\Ui\Form\Support |
23
|
|
|
*/ |
24
|
|
|
class MultipleFormBuilder extends FormBuilder |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The form collection. |
29
|
|
|
* |
30
|
|
|
* @var FormCollection |
31
|
|
|
*/ |
32
|
|
|
protected $forms; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create a new MultipleFormBuilder instance. |
36
|
|
|
* |
37
|
|
|
* @param Form $form |
38
|
|
|
* @param FormCollection $forms |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Form $form, FormCollection $forms) |
41
|
|
|
{ |
42
|
|
|
$this->forms = $forms; |
43
|
|
|
|
44
|
|
|
parent::__construct($form); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Build the form. |
49
|
|
|
* |
50
|
|
|
* @param null $entry |
51
|
|
|
*/ |
52
|
|
|
public function build($entry = null) |
53
|
|
|
{ |
54
|
|
|
$this->dispatch(new BuildForms($this)); |
55
|
|
|
$this->dispatch(new PostForms($this)); |
56
|
|
|
$this->dispatch(new MergeFields($this)); |
57
|
|
|
$this->dispatch(new HandleErrors($this)); |
58
|
|
|
|
59
|
|
|
parent::build($entry); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Save the forms. |
64
|
|
|
*/ |
65
|
|
|
public function saveForm() |
66
|
|
|
{ |
67
|
|
|
$this->fire('saving', ['builder' => $this]); |
68
|
|
|
|
69
|
|
|
/* @var FormBuilder $builder */ |
70
|
|
|
foreach ($forms = $this->getForms() as $slug => $builder) { |
71
|
|
|
|
72
|
|
|
$this->fire('saving_' . $slug, compact('builder', 'forms')); |
73
|
|
|
|
74
|
|
|
$builder->saveForm(); |
75
|
|
|
|
76
|
|
|
$this->fire('saved_' . $slug, compact('builder', 'forms')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->fire('saved', ['builder' => $this]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the forms. |
84
|
|
|
* |
85
|
|
|
* @return FormCollection |
86
|
|
|
*/ |
87
|
|
|
public function getForms() |
88
|
|
|
{ |
89
|
|
|
return $this->forms; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set the forms. |
94
|
|
|
* |
95
|
|
|
* @param $forms |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
public function setForms(FormCollection $forms) |
99
|
|
|
{ |
100
|
|
|
$this->forms = $forms; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Add a form. |
107
|
|
|
* |
108
|
|
|
* @param $key |
109
|
|
|
* @param FormBuilder $builder |
110
|
|
|
* @return MultipleFormBuilder |
111
|
|
|
*/ |
112
|
|
|
public function addForm($key, FormBuilder $builder) |
113
|
|
|
{ |
114
|
|
|
$this->forms->put( |
115
|
|
|
$key, |
116
|
|
|
$builder |
117
|
|
|
->setSave(false) |
118
|
|
|
->setOption('prefix', $key . '_') |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get a child form. |
126
|
|
|
* |
127
|
|
|
* @param $key |
128
|
|
|
* @return FormBuilder |
129
|
|
|
*/ |
130
|
|
|
public function getChildForm($key) |
131
|
|
|
{ |
132
|
|
|
return $this->forms->get($key); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the stream of a child form. |
137
|
|
|
* |
138
|
|
|
* @param $key |
139
|
|
|
* @return StreamInterface|null |
140
|
|
|
*/ |
141
|
|
|
public function getChildFormStream($key) |
142
|
|
|
{ |
143
|
|
|
$builder = $this->getChildForm($key); |
144
|
|
|
|
145
|
|
|
return $builder->getFormStream(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the entry of a child form. |
150
|
|
|
* |
151
|
|
|
* @param $key |
152
|
|
|
* @return EloquentModel|EntryInterface|FieldInterface|AssignmentInterface|null |
153
|
|
|
*/ |
154
|
|
|
public function getChildFormEntry($key) |
155
|
|
|
{ |
156
|
|
|
$builder = $this->getChildForm($key); |
157
|
|
|
|
158
|
|
|
return $builder->getFormEntry(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get the entry ID of a child form. |
163
|
|
|
* |
164
|
|
|
* @param $key |
165
|
|
|
* @return int|null |
166
|
|
|
*/ |
167
|
|
|
public function getChildFormEntryId($key) |
168
|
|
|
{ |
169
|
|
|
$builder = $this->getChildForm($key); |
170
|
|
|
|
171
|
|
|
return $builder->getFormEntryId(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get a child's entry. |
176
|
|
|
* |
177
|
|
|
* @param $key |
178
|
|
|
* @return mixed |
179
|
|
|
*/ |
180
|
|
|
public function getChildEntry($key) |
181
|
|
|
{ |
182
|
|
|
$builder = $this->getChildForm($key); |
183
|
|
|
|
184
|
|
|
return $builder->getEntry(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the form mode. |
189
|
|
|
* |
190
|
|
|
* @return null|string |
191
|
|
|
*/ |
192
|
|
|
public function getFormMode() |
193
|
|
|
{ |
194
|
|
|
$form = $this->forms->first(); |
195
|
|
|
|
196
|
|
|
return $form->getFormMode(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get the contextual entry ID. |
201
|
|
|
* |
202
|
|
|
* @return int|mixed|null |
203
|
|
|
*/ |
204
|
|
|
public function getContextualId() |
205
|
|
|
{ |
206
|
|
|
/* @var FormBuilder $form */ |
207
|
|
|
$form = $this->forms->first(); |
208
|
|
|
|
209
|
|
|
return $form->getContextualId(); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|