|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Tools; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
|
6
|
|
|
use Encore\Admin\Form\Field; |
|
7
|
|
|
use Encore\Admin\Form\Field\MultipleSelect; |
|
8
|
|
|
use Encore\Admin\Form\Field\Select; |
|
9
|
|
|
use Encore\Admin\Form\Field\Text; |
|
10
|
|
|
use Encore\Admin\Grid; |
|
11
|
|
|
use Illuminate\Contracts\Support\Renderable; |
|
12
|
|
|
use Illuminate\Support\Collection; |
|
13
|
|
|
|
|
14
|
|
|
class QuickCreate implements Renderable |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Grid |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $parent; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Collection |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $fields; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* QuickCreate constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param Grid $grid |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Grid $grid) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->parent = $grid; |
|
34
|
|
|
$this->fields = Collection::make(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function formatPlaceholder($placeholder) |
|
38
|
|
|
{ |
|
39
|
|
|
return array_filter((array)$placeholder); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $column |
|
44
|
|
|
* @param string $placeholder |
|
45
|
|
|
* |
|
46
|
|
|
* @return Text |
|
47
|
|
|
*/ |
|
48
|
|
|
public function text($column, $placeholder = '') |
|
49
|
|
|
{ |
|
50
|
|
|
$field = new Text($column, $this->formatPlaceholder($placeholder)); |
|
51
|
|
|
|
|
52
|
|
|
$this->addField($field->width('200px')); |
|
53
|
|
|
|
|
54
|
|
|
return $field; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $column |
|
59
|
|
|
* @param string $placeholder |
|
60
|
|
|
* |
|
61
|
|
|
* @return Text |
|
62
|
|
|
*/ |
|
63
|
|
|
public function email($column, $placeholder = '') |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->text($column, $placeholder) |
|
66
|
|
|
->inputmask(['alias' => 'email']); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $column |
|
71
|
|
|
* @param string $placeholder |
|
72
|
|
|
* |
|
73
|
|
|
* @return Text |
|
74
|
|
|
*/ |
|
75
|
|
|
public function ip($column, $placeholder = '') |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->text($column, $placeholder) |
|
78
|
|
|
->inputmask(['alias' => 'ip']) |
|
79
|
|
|
->width('120px'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $column |
|
84
|
|
|
* @param string $placeholder |
|
85
|
|
|
* |
|
86
|
|
|
* @return Text |
|
87
|
|
|
*/ |
|
88
|
|
|
public function url($column, $placeholder = '') |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->text($column, $placeholder) |
|
91
|
|
|
->inputmask(['alias' => 'url']); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $column |
|
96
|
|
|
* @param string $placeholder |
|
97
|
|
|
* |
|
98
|
|
|
* @return Text |
|
99
|
|
|
*/ |
|
100
|
|
|
public function password($column, $placeholder = '') |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->text($column, $placeholder) |
|
103
|
|
|
->attribute('type', 'password') |
|
104
|
|
|
->width('100px'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $column |
|
109
|
|
|
* @param string $placeholder |
|
110
|
|
|
* |
|
111
|
|
|
* @return Text |
|
112
|
|
|
*/ |
|
113
|
|
|
public function mobile($column, $placeholder = '') |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->text($column, $placeholder) |
|
116
|
|
|
->inputmask(['mask' => '99999999999']) |
|
117
|
|
|
->width('100px'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $column |
|
122
|
|
|
* @param string $placeholder |
|
123
|
|
|
* |
|
124
|
|
|
* @return Text |
|
125
|
|
|
*/ |
|
126
|
|
|
public function integer($column, $placeholder = '') |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->text($column, $placeholder) |
|
129
|
|
|
->inputmask(['alias' => 'integer']) |
|
130
|
|
|
->width('120px'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $column |
|
135
|
|
|
* @param string $placeholder |
|
136
|
|
|
* |
|
137
|
|
|
* @return Select |
|
138
|
|
|
*/ |
|
139
|
|
|
public function select($column, $placeholder = '') |
|
140
|
|
|
{ |
|
141
|
|
|
$field = new Select($column, $this->formatPlaceholder($placeholder)); |
|
142
|
|
|
|
|
143
|
|
|
$this->addField($field); |
|
144
|
|
|
|
|
145
|
|
|
return $field; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $column |
|
150
|
|
|
* @param string $placeholder |
|
151
|
|
|
* |
|
152
|
|
|
* @return MultipleSelect |
|
153
|
|
|
*/ |
|
154
|
|
|
public function multipleSelect($column, $placeholder = '') |
|
155
|
|
|
{ |
|
156
|
|
|
$field = new MultipleSelect($column, $this->formatPlaceholder($placeholder)); |
|
157
|
|
|
|
|
158
|
|
|
$this->addField($field); |
|
159
|
|
|
|
|
160
|
|
|
return $field; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param string $column |
|
165
|
|
|
* @param string $placeholder |
|
166
|
|
|
* |
|
167
|
|
|
* @return Field\Date |
|
168
|
|
|
*/ |
|
169
|
|
|
public function datetime($column, $placeholder = '') |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->date($column, $placeholder)->format('YYYY-MM-DD HH:mm:ss'); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param string $column |
|
176
|
|
|
* @param string $placeholder |
|
177
|
|
|
* |
|
178
|
|
|
* @return Field\Date |
|
179
|
|
|
*/ |
|
180
|
|
|
public function time($column, $placeholder = '') |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->date($column, $placeholder)->format('HH:mm:ss'); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param string $column |
|
187
|
|
|
* @param string $placeholder |
|
188
|
|
|
* |
|
189
|
|
|
* @return Field\Date |
|
190
|
|
|
*/ |
|
191
|
|
|
public function date($column, $placeholder = '') |
|
192
|
|
|
{ |
|
193
|
|
|
$field = new Field\Date($column, $this->formatPlaceholder($placeholder)); |
|
194
|
|
|
|
|
195
|
|
|
$this->addField($field); |
|
196
|
|
|
|
|
197
|
|
|
return $field; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param Field $field |
|
202
|
|
|
* |
|
203
|
|
|
* @return Field |
|
204
|
|
|
*/ |
|
205
|
|
|
protected function addField(Field $field) |
|
206
|
|
|
{ |
|
207
|
|
|
$field->setView($this->resolveView(get_class($field))); |
|
208
|
|
|
|
|
209
|
|
|
$this->fields->push($field); |
|
210
|
|
|
|
|
211
|
|
|
return $field; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @param string $class |
|
216
|
|
|
* |
|
217
|
|
|
* @return string |
|
218
|
|
|
*/ |
|
219
|
|
View Code Duplication |
protected function resolveView($class) |
|
|
|
|
|
|
220
|
|
|
{ |
|
221
|
|
|
$path = explode('\\', $class); |
|
222
|
|
|
|
|
223
|
|
|
$name = strtolower(array_pop($path)); |
|
224
|
|
|
|
|
225
|
|
|
return "admin::grid.quick-create.{$name}"; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
protected function script() |
|
229
|
|
|
{ |
|
230
|
|
|
$url = request()->url(); |
|
231
|
|
|
|
|
232
|
|
|
$script = <<<SCRIPT |
|
233
|
|
|
|
|
234
|
|
|
(function () { |
|
235
|
|
|
|
|
236
|
|
|
$('.quick-create .create').click(function () { |
|
237
|
|
|
$('.quick-create .create-form').show(); |
|
238
|
|
|
$(this).hide(); |
|
239
|
|
|
}); |
|
240
|
|
|
|
|
241
|
|
|
$('.quick-create .cancel').click(function () { |
|
242
|
|
|
$('.quick-create .create-form').hide(); |
|
243
|
|
|
$('.quick-create .create').show(); |
|
244
|
|
|
}); |
|
245
|
|
|
|
|
246
|
|
|
$('.quick-create .create-form').submit(function (e) { |
|
247
|
|
|
|
|
248
|
|
|
e.preventDefault(); |
|
249
|
|
|
|
|
250
|
|
|
$.ajax({ |
|
251
|
|
|
url: '{$url}', |
|
252
|
|
|
type: 'POST', |
|
253
|
|
|
data: $(this).serialize(), |
|
254
|
|
|
success: function(data, textStatus, jqXHR) { |
|
255
|
|
|
console.info(data); |
|
256
|
|
|
|
|
257
|
|
|
if (data.status == true) { |
|
258
|
|
|
$.admin.toastr.success(data.message, '', {positionClass:"toast-top-center"}); |
|
259
|
|
|
$.admin.reload(); |
|
260
|
|
|
return; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
if (typeof data.validation !== 'undefined') { |
|
264
|
|
|
$.admin.toastr.warning(data.message, '', {positionClass:"toast-top-center"}) |
|
265
|
|
|
} |
|
266
|
|
|
}, |
|
267
|
|
|
error:function(XMLHttpRequest, textStatus){ |
|
268
|
|
|
if (typeof XMLHttpRequest.responseJSON === 'object') { |
|
269
|
|
|
$.admin.toastr.error(XMLHttpRequest.responseJSON.message, '', {positionClass:"toast-top-center", timeOut: 10000}); |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
}); |
|
273
|
|
|
|
|
274
|
|
|
return false; |
|
275
|
|
|
}); |
|
276
|
|
|
|
|
277
|
|
|
})(); |
|
278
|
|
|
|
|
279
|
|
|
SCRIPT; |
|
280
|
|
|
|
|
281
|
|
|
Admin::script($script); |
|
282
|
|
|
|
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @param int $columnCount |
|
287
|
|
|
* |
|
288
|
|
|
* @return array|string |
|
289
|
|
|
*/ |
|
290
|
|
|
public function render($columnCount = 0) |
|
291
|
|
|
{ |
|
292
|
|
|
if ($this->fields->isEmpty()) { |
|
293
|
|
|
return ''; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
$this->script(); |
|
297
|
|
|
|
|
298
|
|
|
$vars = [ |
|
299
|
|
|
'columnCount' => $columnCount, |
|
300
|
|
|
'fields' => $this->fields, |
|
301
|
|
|
]; |
|
302
|
|
|
|
|
303
|
|
|
return view('admin::grid.quick-create.form', $vars)->render(); |
|
|
|
|
|
|
304
|
|
|
} |
|
305
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.