|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Tinyissue package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Tinyissue\Extensions\Html; |
|
13
|
|
|
|
|
14
|
|
|
use Former; |
|
15
|
|
|
use Former\Traits\Field; |
|
16
|
|
|
use Request; |
|
17
|
|
|
use Tinyissue\Contracts\Form\FormInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* FormBuilder is a class to extend Laravel FormBuilder to add extra view macro. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class FormBuilder extends \Collective\Html\FormBuilder |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Render Form object into Html form with Former. |
|
28
|
|
|
* |
|
29
|
|
|
* @param FormInterface $form |
|
30
|
|
|
* @param array $attributes |
|
31
|
|
|
* |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function form(FormInterface $form, array $attributes = []) |
|
35
|
|
|
{ |
|
36
|
|
|
/** @var \Former\Form\Form $former Start former instance */ |
|
37
|
|
|
$former = call_user_func(['\Former', $form->openType()]); |
|
38
|
|
|
|
|
39
|
|
|
// Setup form |
|
40
|
|
|
$this->setupForm($former, $attributes, $form->rules()); |
|
41
|
|
|
|
|
42
|
|
|
// Render fields |
|
43
|
|
|
$former .= $this->renderFields($form->fields(), is_null($form->getModel())); |
|
44
|
|
|
|
|
45
|
|
|
// Render actions |
|
46
|
|
|
$former .= $this->actions($form); |
|
47
|
|
|
|
|
48
|
|
|
// Close the opened form |
|
49
|
|
|
$former .= Former::close(); |
|
50
|
|
|
|
|
51
|
|
|
return $former; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Generate Former field. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $name |
|
58
|
|
|
* @param array $field |
|
59
|
|
|
* |
|
60
|
|
|
* @return Field |
|
61
|
|
|
*/ |
|
62
|
|
|
public function element($name, array $field) |
|
63
|
|
|
{ |
|
64
|
|
|
$filterKeys = ['type']; |
|
65
|
|
|
$attributes = array_diff_key($field, array_flip($filterKeys)); |
|
66
|
|
|
|
|
67
|
|
|
// Create field with name |
|
68
|
|
|
$element = call_user_func(['\Former', $field['type']], $name); |
|
69
|
|
|
|
|
70
|
|
|
// Create field attributes |
|
71
|
|
|
$this->setAttributes($attributes, $element); |
|
72
|
|
|
|
|
73
|
|
|
return $element; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Render form actions. |
|
78
|
|
|
* |
|
79
|
|
|
* @param FormInterface $form |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function actions(FormInterface $form) |
|
84
|
|
|
{ |
|
85
|
|
|
$output = ''; |
|
86
|
|
|
$buttons = $form->actions(); |
|
87
|
|
|
if (!empty($buttons)) { |
|
88
|
|
|
$actions = Former::actions()->addClass('form-actions'); |
|
89
|
|
|
foreach ($buttons as $options) { |
|
90
|
|
|
if (is_array($options)) { |
|
91
|
|
|
$actions->{$options['type']}($options['label'], $options); |
|
92
|
|
|
} else { |
|
93
|
|
|
$actions->primary_submit(trans('tinyissue.' . $options)); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
$output .= $actions; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $output; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param $former |
|
104
|
|
|
* @param array $attributes |
|
105
|
|
|
* @param array $rules |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function setupForm($former, array $attributes, array $rules) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->setAttributes($attributes, $former); |
|
112
|
|
|
|
|
113
|
|
|
$former->rules($rules); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param array $attributes |
|
118
|
|
|
* @param object $object |
|
119
|
|
|
* |
|
120
|
|
|
* @return void |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function setAttributes(array $attributes, $object) |
|
123
|
|
|
{ |
|
124
|
|
|
array_walk($attributes, function ($value, $attr) use ($object) { |
|
125
|
|
|
if ($value === null) { |
|
126
|
|
|
$object->$attr(); |
|
127
|
|
|
} else { |
|
128
|
|
|
$object->$attr($value); |
|
129
|
|
|
} |
|
130
|
|
|
}); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param array $fields |
|
135
|
|
|
* @param bool $populate |
|
136
|
|
|
* |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function renderFields(array $fields, $populate = false) |
|
140
|
|
|
{ |
|
141
|
|
|
$output = ''; |
|
142
|
|
|
foreach ($fields as $name => $field) { |
|
143
|
|
|
$element = $this->element($name, $field); |
|
144
|
|
|
|
|
145
|
|
|
if ($element instanceof Field && $populate) { |
|
146
|
|
|
$element->value = Request::input($name); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$output .= $element; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $output; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|