1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Plugins\ServiceDesk\Controllers\FormBuilder; |
4
|
|
|
|
5
|
|
|
use App\Plugins\ServiceDesk\Controllers\BaseServiceDeskController; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use App\Plugins\ServiceDesk\Model\FormBuilder\Form; |
8
|
|
|
use App\Plugins\ServiceDesk\Model\FormBuilder\FormValue; |
9
|
|
|
use App\Plugins\ServiceDesk\Model\FormBuilder\FormField; |
10
|
|
|
use Exception; |
11
|
|
|
|
12
|
|
|
class FormBuilderController extends BaseServiceDeskController { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Create a new controller instance. |
16
|
|
|
* |
17
|
|
|
* @return void |
|
|
|
|
18
|
|
|
*/ |
19
|
|
|
public function __construct() { |
20
|
|
|
$this->middleware('auth'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function create() { |
24
|
|
|
return view('service::form-builder.create'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function store(Request $request) { |
28
|
|
|
$this->validate($request, [ |
29
|
|
|
'title' => 'required|unique:sd_forms', |
30
|
|
|
'form' => 'required', |
31
|
|
|
]); |
32
|
|
|
try { |
33
|
|
|
$forms = new Form(); |
34
|
|
|
$form = $request->input('form'); |
35
|
|
|
$title = $request->input('title'); |
36
|
|
|
$xmlNode = simplexml_load_string($form); |
37
|
|
|
$forms->title = $title; |
|
|
|
|
38
|
|
|
$forms->save(); |
39
|
|
|
$arrayData = \App\Plugins\ServiceDesk\Controllers\Library\UtilityController::xmlToArray($xmlNode); |
40
|
|
|
$save = $this->saveField($forms->id, $arrayData); |
|
|
|
|
41
|
|
|
$result = ["fails" => "We can't process your request"]; |
42
|
|
|
if ($save) { |
|
|
|
|
43
|
|
|
$result = ["success" => "Form $forms->title created successfully"]; |
|
|
|
|
44
|
|
|
} |
45
|
|
|
return response()->json(compact('result')); |
46
|
|
|
} catch (Exception $ex) { |
47
|
|
|
dd($ex); |
48
|
|
|
$result = ["fails" => $ex->getMessage()]; |
49
|
|
|
return response()->json(compact('result')); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function saveField($formid, $data) { |
54
|
|
|
//dd($data); |
55
|
|
|
$fields = new FormField(); |
56
|
|
|
$items = $fields->where('form_id', $formid)->get(); |
|
|
|
|
57
|
|
|
if ($items->count() > 0) { |
58
|
|
|
foreach ($items as $item) { |
59
|
|
|
$item->delete(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
foreach ($data['form-template']['fields']['field'] as $index => $item) { |
63
|
|
|
$field = $fields->create([ |
64
|
|
|
'name' => $this->checkField('name', $item), |
65
|
|
|
'label' => $this->checkField('label', $item), |
66
|
|
|
'form_id' => $formid, |
67
|
|
|
'type' => $this->checkField('type', $item), |
68
|
|
|
'sub_type' => $this->checkField('subtype', $item), |
69
|
|
|
'class' => $this->checkField('class', $item), |
70
|
|
|
'is_required' => $this->checkField('required', $item), |
71
|
|
|
'placeholder' => $this->checkField('placeholder', $item), |
72
|
|
|
'description' => $this->checkField('description', $item), |
73
|
|
|
'multiple' => $this->checkField('multiple', $item), |
74
|
|
|
'role' => $this->checkField('role', $item), |
75
|
|
|
]); |
76
|
|
|
if (is_string($item)) { |
77
|
|
|
if ($index == "option") { |
78
|
|
|
$this->saveFieldValue($field->id, $item['option']); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
} elseif (key_exists('option', $item)) { |
81
|
|
|
$this->saveFieldValue($field->id, $item['option']); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
if ($field) { |
|
|
|
|
85
|
|
|
return "success"; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function saveFieldValue($fieldid, $options = []) { |
90
|
|
|
$values = new FormValue(); |
91
|
|
|
foreach ($options as $option) { |
92
|
|
|
$values->create([ |
93
|
|
|
'field_id' => $fieldid, |
94
|
|
|
'option' => $option['option-name'], |
95
|
|
|
'value' => $option['value'], |
96
|
|
|
]); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function checkField($name, $array) { |
101
|
|
|
$res = ""; |
102
|
|
|
if (is_string($array)) { |
103
|
|
|
$res = $array; |
104
|
|
|
} elseif (key_exists($name, $array)) { |
105
|
|
|
$res = $array[$name]; |
106
|
|
|
} |
107
|
|
|
return $res; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function renderHtmlByFormId($id, $view = true, $assetid = '') { |
111
|
|
|
$html = ""; |
112
|
|
|
$forms = new \App\Plugins\ServiceDesk\Model\Assets\AssetFormRelation(); |
113
|
|
|
$form = $forms->where('asset_type_id', $id)->first(); |
|
|
|
|
114
|
|
|
if ($form) { |
115
|
|
|
$title = $form->title; |
116
|
|
|
$html = "<h1>$title</h1>" . $this->getFields($form->id, $assetid); |
117
|
|
|
} |
118
|
|
|
if ($view == true) { |
|
|
|
|
119
|
|
|
return view("service::form-builder.show", compact('html')); |
120
|
|
|
} else { |
121
|
|
|
return $html; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getFields($formid, $assetid = '', $json = false) { |
126
|
|
|
$item = ""; |
127
|
|
|
$fields = new FormField(); |
128
|
|
|
$field = $fields->where('form_id', $formid)->get(); |
|
|
|
|
129
|
|
|
|
130
|
|
|
if ($field->count() > 0) { |
131
|
|
|
foreach ($field as $key => $html) { |
132
|
|
|
|
133
|
|
|
$name = $this->checkField('name', $html->toArray()); |
134
|
|
|
|
135
|
|
|
$label = $this->checkField('label', $html->toArray()); |
136
|
|
|
$type = $this->checkField('type', $html->toArray()); |
137
|
|
|
$sub_type = $this->checkField('sub_type', $html->toArray()); |
138
|
|
|
$class = $this->checkField('class', $html->toArray()); |
139
|
|
|
$is_required = $this->checkField('is_required', $html->toArray()); |
140
|
|
|
$placeholder = $this->checkField('placeholder', $html->toArray()); |
141
|
|
|
$description = $this->checkField('description', $html->toArray()); |
142
|
|
|
$multiple = $this->checkField('multiple', $html->toArray()); |
143
|
|
|
$role = $this->checkField('role', $html->toArray()); |
144
|
|
|
|
145
|
|
|
if ($json == true) { |
|
|
|
|
146
|
|
|
|
147
|
|
|
$item['form-template']['fields'][$key] = $this->getJson($key, $html->id, $name, $label, $type, $sub_type, $class, $is_required, $placeholder, $description, $multiple, $role); |
148
|
|
|
} else { |
149
|
|
|
//dd([$html->id, $name, $label, $type, $sub_type, $class, $is_required, $placeholder, $description, $multiple]); |
|
|
|
|
150
|
|
|
//$item .="<form id='form'>"; |
|
|
|
|
151
|
|
|
$item .= "<div class='form-group col-md-6'>"; |
152
|
|
|
$item .= $this->fields($html->id, $name, $label, $type, $sub_type, $class, $is_required, $placeholder, $description, $multiple, $assetid); |
153
|
|
|
$item .= "</div>"; |
154
|
|
|
} |
155
|
|
|
//$item .= "</form>"; |
|
|
|
|
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
//dd($item); |
160
|
|
|
return $item; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function fields($fieldid, $name, $label, $type, $sub_type, $class, $is_required, $placeholder, $description, $multiple, $assetid = '') { |
164
|
|
|
$required = ""; |
165
|
|
|
$html = ""; |
166
|
|
|
if ($is_required == "true") { |
167
|
|
|
$required = "required"; |
168
|
|
|
} |
169
|
|
|
switch ($type) { |
170
|
|
|
case "button": |
171
|
|
|
return "<$sub_type class='$class' name='$name'>$label</$sub_type>"; |
172
|
|
|
case "checkbox": |
173
|
|
|
return "<label>$label</label>" |
174
|
|
|
. "<input type='$type' class='$class' placeholder='$placeholder' name='$name' $required>"; |
175
|
|
|
case "paragraph": |
176
|
|
|
return "<$sub_type class='$class'></$sub_type>"; |
177
|
|
|
case "header": |
178
|
|
|
return "<$sub_type class='$class'></$sub_type>"; |
179
|
|
|
case "textarea": |
180
|
|
|
return "<label>$label</label><$type class='$class' placeholder='$placeholder' $required></$type>"; |
181
|
|
|
case "text": |
182
|
|
|
return "<label>$label</label>" |
183
|
|
|
//.\Form::text($name,null,['class'=>$class,'placeholder'=>$placeholder,'required'=>$is_required]); |
|
|
|
|
184
|
|
|
. "<input type='$type' class='$class' placeholder='$placeholder' name='$name' value=" . $this->value($fieldid, $assetid) . " $required>"; |
185
|
|
|
case "date": |
186
|
|
|
return "<label>$label</label>" |
187
|
|
|
. "<input type='$type' class='$class' placeholder='$placeholder' name='$name' $required>"; |
188
|
|
|
case "file": |
189
|
|
|
return "<label>$label</label>" |
190
|
|
|
. "<input type='$type' class='$class' placeholder='$placeholder' name='$name' $required>"; |
191
|
|
|
case "checkbox-group": |
192
|
|
|
return $this->groupValue($fieldid, $name, $label, $type, $sub_type, $class, $required, $placeholder, $description, $multiple); |
193
|
|
|
case "radio-group": |
194
|
|
|
return $this->groupValue($fieldid, $name, $label, $type, $sub_type, $class, $required, $placeholder, $description, $multiple); |
195
|
|
|
case "select": |
196
|
|
|
return $this->groupValue($fieldid, $name, $label, $type, $sub_type, $class, $required, $placeholder, $description, $multiple); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $html; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function groupValue($fieldid, $name, $label, $type, $sub_type, $class, $required, $placeholder, $description, $multiple, $json = false) { |
203
|
|
|
$values = new FormValue(); |
204
|
|
|
$html = ""; |
|
|
|
|
205
|
|
|
$value = $values->where('field_id', $fieldid)->get(); |
|
|
|
|
206
|
|
|
$html = $this->getForeach($value, $name, $label, $type, $sub_type, $class, $required, $placeholder, $description, $multiple, $json); |
207
|
|
|
return $html; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function getForeach($values, $name, $label, $type, $sub_type, $class, $required, $placeholder, $description, $multiple, $json) { |
|
|
|
|
211
|
|
|
$html = ""; |
212
|
|
|
$array = []; |
|
|
|
|
213
|
|
|
if (count($values) > 0) { |
214
|
|
View Code Duplication |
if ($type == "checkbox-group") { |
|
|
|
|
215
|
|
|
$html .= "<label>$label</label></br>"; |
216
|
|
|
foreach ($values as $index => $value) { |
217
|
|
|
//if ($json == false) { |
|
|
|
|
218
|
|
|
$html .= "<input type='checkbox' name='$name' class='$class' value='$value->value'>" . $value->option . "</br>"; |
219
|
|
|
// } else { |
|
|
|
|
220
|
|
|
// $array[$index] = $values->lists('option','value')->toArray(); |
221
|
|
|
// } |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
View Code Duplication |
if ($type == "radio-group") { |
|
|
|
|
225
|
|
|
$html .= "<label>$label</label></br>"; |
226
|
|
|
foreach ($values as $index => $value) { |
227
|
|
|
//if ($json == false) { |
|
|
|
|
228
|
|
|
$html .= "<input type='radio' name='$name' class='$class' value='$value->value'>" . $value->option . "</br>"; |
229
|
|
|
// } else { |
|
|
|
|
230
|
|
|
// $array[$index] = $value->lists('option','value')->toArray(); |
231
|
|
|
// } |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
if ($type == "select") { |
235
|
|
|
$html .= "<label>$label</label><select class='$class' name='$name' placeholder='$placeholder' $required>"; |
236
|
|
|
foreach ($values as $index => $value) { |
237
|
|
|
//if ($json == false) { |
|
|
|
|
238
|
|
|
$html .= "<option value='$value->value'>" . $value->option . "</option>"; |
239
|
|
|
//} else { |
240
|
|
|
//$array[$index] = $value->lists('option','value')->toArray(); |
|
|
|
|
241
|
|
|
//} |
242
|
|
|
} |
243
|
|
|
$html .="</select>"; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
if ($json == false) { |
247
|
|
|
return $html; |
248
|
|
|
} |
249
|
|
|
return $values->lists('option', 'value')->toArray(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function value($formid, $assetid = "") { |
253
|
|
|
$result = ""; |
254
|
|
|
if ($assetid !== '') { |
255
|
|
|
$fields = new FormField(); |
256
|
|
|
$field = $fields->find($formid); |
|
|
|
|
257
|
|
|
if ($field) { |
258
|
|
|
$name = $field->name; |
259
|
|
|
$asset_forms = new \App\Plugins\ServiceDesk\Model\Assets\AssetForm(); |
260
|
|
|
$form = $asset_forms->where('asset_id', $assetid)->where('key', $name)->first(); |
|
|
|
|
261
|
|
|
if ($form) { |
262
|
|
|
$result = $form->value; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
return $result; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function index() { |
270
|
|
|
try { |
271
|
|
|
return view('service::form-builder.index'); |
272
|
|
|
} catch (Exception $ex) { |
273
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function getForm() { |
278
|
|
|
$forms = new Form(); |
279
|
|
|
$form = $forms->select('id', 'title')->get(); |
|
|
|
|
280
|
|
|
return \Datatable::Collection($form) |
281
|
|
|
->showColumns('title') |
282
|
|
|
->addColumn('action', function($model) { |
283
|
|
|
$preview = $this->show($model->id, 'popup'); |
284
|
|
|
$url = url('service-desk/form-builder/'.$model->id.'/delete'); |
285
|
|
|
$title = "Delete $model->title"; |
286
|
|
|
$delete = \App\Plugins\ServiceDesk\Controllers\Library\UtilityController::deletePopUp($model->id, $url, $title); |
287
|
|
|
return "<a href=" . url('service-desk/form-builder/' . $model->id . '/edit') . " class='btn btn-sm btn-primary'>Edit</a> " |
288
|
|
|
. $preview.$delete; |
289
|
|
|
}) |
290
|
|
|
->searchColumns('title') |
291
|
|
|
->orderColumns('title') |
292
|
|
|
->make(); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function edit($id) { |
296
|
|
|
try { |
297
|
|
|
$title = ""; |
298
|
|
|
$forms = new Form(); |
299
|
|
|
$form = $forms->find($id); |
|
|
|
|
300
|
|
|
if ($form) { |
301
|
|
|
$title = $form->title; |
302
|
|
|
} |
303
|
|
|
$fields = $this->convertForm($id); |
304
|
|
|
$array = $fields->toArray(); |
305
|
|
|
$values = \App\Plugins\ServiceDesk\Controllers\Library\UtilityController::arrayToXml($array); |
306
|
|
|
$xml = "<form-template><fields>$values</fields></form-template>"; |
307
|
|
|
$xml = str_replace("<field ></field>", '', $xml); |
308
|
|
|
return view("service::form-builder.edit", compact('xml', 'title', 'form')); |
309
|
|
|
} catch (Exception $ex) { |
310
|
|
|
dd($ex); |
311
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function getArray($key, $name, $label, $type, $sub_type, $class, $is_required, $placeholder, $description, $multiple, $role, $options = []) { |
|
|
|
|
316
|
|
|
$item['name'] = $name; |
|
|
|
|
317
|
|
|
$item['label'] = $label; |
318
|
|
|
$item['type'] = $type; |
319
|
|
|
$item['subtype'] = $sub_type; |
320
|
|
|
$item['class'] = $class; |
321
|
|
|
$item['placeholder'] = $placeholder; |
322
|
|
|
$item['description'] = $description; |
323
|
|
|
$item['multiple'] = $multiple; |
324
|
|
|
$item['role'] = $role; |
325
|
|
|
$item['options'] = $options; |
326
|
|
|
return $item; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function getJson($key, $fieldid, $name, $label, $type, $sub_type, $class, $is_required, $placeholder, $description, $multiple, $role, $options = []) { |
|
|
|
|
330
|
|
|
$options = $this->groupValue($fieldid, $name, $label, $type, $sub_type, $class, $is_required, $placeholder, $description, $multiple, true); |
331
|
|
|
$array = $this->getArray($key, $name, $label, $type, $sub_type, $class, $is_required, $placeholder, $description, $multiple, $role, $options); |
332
|
|
|
return $array; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function convertForm($formid) { |
336
|
|
|
$array = $this->getFields($formid, '', true); |
337
|
|
|
$collection = new \Illuminate\Support\Collection($array); |
338
|
|
|
return $collection; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
public function update($id, Request $request) { |
342
|
|
|
$this->validate($request, [ |
343
|
|
|
'title' => 'required|unique:sd_forms', |
344
|
|
|
'form' => 'required', |
345
|
|
|
]); |
346
|
|
|
try { |
347
|
|
|
$forms = new Form(); |
348
|
|
|
$model = $forms->find($id); |
|
|
|
|
349
|
|
|
if (!$model) { |
350
|
|
|
$result = ["fails" => "We can't process your request"]; |
351
|
|
|
return response()->json(compact('result')); |
352
|
|
|
} |
353
|
|
|
$form = $request->input('form'); |
354
|
|
|
$title = $request->input('title'); |
355
|
|
|
$xmlNode = simplexml_load_string($form); |
356
|
|
|
$model->title = $title; |
357
|
|
|
$model->save(); |
358
|
|
|
$arrayData = \App\Plugins\ServiceDesk\Controllers\Library\UtilityController::xmlToArray($xmlNode); |
359
|
|
|
$save = $this->saveField($model->id, $arrayData); |
360
|
|
|
$result = ["fails" => "We can't process your request"]; |
361
|
|
|
if ($save) { |
|
|
|
|
362
|
|
|
$result = ["success" => "Form $forms->title Updated successfully"]; |
|
|
|
|
363
|
|
|
} |
364
|
|
|
return response()->json(compact('result')); |
365
|
|
|
} catch (Exception $ex) { |
366
|
|
|
$result = ["fails" => $ex->getMessage()]; |
367
|
|
|
return response()->json(compact('result')); |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function show($id, $render = 'view') { |
372
|
|
|
try { |
373
|
|
|
$html = ""; |
374
|
|
|
$title = ""; |
375
|
|
|
$forms = new Form(); |
376
|
|
|
$form = $forms->find($id); |
|
|
|
|
377
|
|
|
if ($form) { |
378
|
|
|
$title = $form->title; |
379
|
|
|
$html = $this->getFields($form->id); |
380
|
|
|
} |
381
|
|
|
switch ($render) { |
382
|
|
|
case "view": |
383
|
|
|
$html = "<h1>$title</h1>" . $html; |
384
|
|
|
return view("service::form-builder.show", compact('html')); |
385
|
|
|
case "popup": |
386
|
|
|
return $this->popUp($id, $title, $html); |
387
|
|
|
default : |
388
|
|
|
return $html; |
389
|
|
|
} |
390
|
|
|
} catch (Exception $ex) { |
391
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
public function popUp($id, $title, $html) { |
396
|
|
|
return '<a href="#form" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#form' . $id . '">Preview</a> |
397
|
|
|
<div class="modal fade" id="form' . $id . '"> |
398
|
|
|
<div class="modal-dialog"> |
399
|
|
|
<div class="modal-content"> |
400
|
|
|
<div class="modal-header"> |
401
|
|
|
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> |
402
|
|
|
<h4 class="modal-title">' . $title . '</h4> |
403
|
|
|
</div> |
404
|
|
|
<div class="modal-body"> |
405
|
|
|
<div class="row"> |
406
|
|
|
<div class="col-md-12"> |
407
|
|
|
' . $html . ' |
408
|
|
|
</div> |
409
|
|
|
</div> |
410
|
|
|
</div> |
411
|
|
|
<div class="modal-footer"> |
412
|
|
|
<button type="button" id="close" class="btn btn-default pull-left" data-dismiss="modal">Close</button> |
413
|
|
|
</div> |
414
|
|
|
</div> |
415
|
|
|
</div> |
416
|
|
|
</div>'; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
public function delete($id){ |
420
|
|
|
try{ |
421
|
|
|
$forms = new Form(); |
422
|
|
|
$form = $forms->find($id); |
|
|
|
|
423
|
|
|
if ($form) { |
424
|
|
|
$form->delete(); |
425
|
|
|
} |
426
|
|
|
return redirect()->back()->with('success', "$form->title deleted successfully"); |
427
|
|
|
} catch (Exception $ex) { |
428
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
} |
433
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.