|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use CodexShaper\DBM\Facades\Manager as DBM; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
|
|
8
|
|
|
class TemplateController extends Controller |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Update Templates. |
|
12
|
|
|
* |
|
13
|
|
|
* @return void |
|
14
|
|
|
*/ |
|
15
|
|
|
public function updateTemplates(Request $request) |
|
16
|
|
|
{ |
|
17
|
|
|
if (is_array($request->templates) && count($request->templates) > 0) { |
|
18
|
|
|
foreach ($request->templates as $field) { |
|
19
|
|
|
if ($template = DBM::Template()->where('old_name', $field['oldName'])->first()) { |
|
20
|
|
|
$template->name = $field['name']; |
|
21
|
|
|
$template->old_name = $field['name']; |
|
22
|
|
|
$template->type = $field['type']['name']; |
|
23
|
|
|
$template->length = $field['length']; |
|
24
|
|
|
$template->index = $field['index']; |
|
25
|
|
|
$template->default = $field['default']; |
|
26
|
|
|
$template->notnull = $field['notnull']; |
|
27
|
|
|
$template->unsigned = $field['unsigned']; |
|
28
|
|
|
$template->auto_increment = $field['autoincrement']; |
|
29
|
|
|
|
|
30
|
|
|
$template->update(); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Create a new template. |
|
38
|
|
|
* |
|
39
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
40
|
|
|
*/ |
|
41
|
|
|
public function save(Request $request) |
|
42
|
|
|
{ |
|
43
|
|
|
$field = $request->template; |
|
44
|
|
|
try { |
|
45
|
|
|
if (DBM::Template()->where('name', $field['name'])->first()) { |
|
46
|
|
|
return response()->json([ |
|
47
|
|
|
'success' => false, |
|
48
|
|
|
'errors' => [' The template name must be unique. '.$field['name'].' already exist.'], |
|
49
|
|
|
], 400); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$template = DBM::Template(); |
|
53
|
|
|
$template->name = $field['name']; |
|
54
|
|
|
$template->old_name = $field['name']; |
|
55
|
|
|
$template->type = $field['type']['name']; |
|
56
|
|
|
$template->length = $field['length']; |
|
57
|
|
|
$template->index = $field['index']; |
|
58
|
|
|
$template->default = $field['default']; |
|
59
|
|
|
$template->notnull = $field['notnull']; |
|
60
|
|
|
$template->unsigned = $field['unsigned']; |
|
61
|
|
|
$template->auto_increment = $field['autoincrement']; |
|
62
|
|
|
|
|
63
|
|
|
if ($template->save()) { |
|
64
|
|
|
return response()->json(['success' => true, 'templates' => DBM::templates()]); |
|
65
|
|
|
} |
|
66
|
|
|
} catch (\Exception $e) { |
|
67
|
|
|
return response()->json([ |
|
68
|
|
|
'success' => false, |
|
69
|
|
|
'errors' => [$e->getMessage()], |
|
70
|
|
|
], 400); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return response()->json(['success' => true, 'template' => $request->all()]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Remove a template. |
|
78
|
|
|
* |
|
79
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
80
|
|
|
*/ |
|
81
|
|
|
public function remove(Request $request) |
|
82
|
|
|
{ |
|
83
|
|
|
if ($template = DBM::Template()->where('name', $request->name)->first()) { |
|
84
|
|
|
if ($template->delete()) { |
|
85
|
|
|
return response()->json(['success' => true, 'templates' => DBM::templates()]); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return response()->json([ |
|
90
|
|
|
'success' => false, |
|
91
|
|
|
'errors' => ['The template ' + $request->name.' not found'], |
|
92
|
|
|
], 400); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|