1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hechoenlaravel\JarvisFoundation\Http\Controllers\Core; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Hechoenlaravel\JarvisFoundation\Traits\EntityManager; |
7
|
|
|
use Hechoenlaravel\JarvisFoundation\FieldGenerator\FieldModel; |
8
|
|
|
use Hechoenlaravel\JarvisFoundation\Http\Controllers\Controller; |
9
|
|
|
use Hechoenlaravel\JarvisFoundation\Http\Requests\EditFieldRequest; |
10
|
|
|
use Hechoenlaravel\JarvisFoundation\Http\Requests\CreateFieldRequest; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException; |
12
|
|
|
use Hechoenlaravel\JarvisFoundation\FieldGenerator\Transformers\FieldTransformer; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class FieldsController |
16
|
|
|
* @package Hechoenlaravel\JarvisFoundation\Http\Controllers\Core |
17
|
|
|
*/ |
18
|
|
|
class FieldsController extends Controller |
19
|
|
|
{ |
20
|
|
|
use EntityManager; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var FieldModel |
25
|
|
|
*/ |
26
|
|
|
protected $model; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var |
30
|
|
|
*/ |
31
|
|
|
protected $fieldType; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* FieldsController constructor. |
35
|
|
|
* @param FieldModel $model |
36
|
|
|
*/ |
37
|
|
|
public function __construct(FieldModel $model) |
38
|
|
|
{ |
39
|
|
|
$this->model = $model; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $id |
44
|
|
|
* @return \Illuminate\Http\JsonResponse |
45
|
|
|
*/ |
46
|
|
|
public function index($id) |
47
|
|
|
{ |
48
|
|
|
$model = $this->model->byEntity($id)->orderBy('order', 'ASC'); |
49
|
|
|
return response()->json(fractal()->collection($model->get(), new FieldTransformer())->toArray()); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param CreateFieldRequest $request |
54
|
|
|
* @param $id |
55
|
|
|
* @return \Illuminate\Http\JsonResponse |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function store(CreateFieldRequest $request, $id) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$data = $request->all(); |
60
|
|
|
$data['entity_id'] = $id; |
61
|
|
|
$field = $this->generateField($data); |
62
|
|
|
$response = fractal()->item($field, new FieldTransformer()) |
63
|
|
|
->addMeta(['return_url' => $data['returnUrl']]) |
64
|
|
|
->toArray(); |
65
|
|
|
return response()->json($response); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $id |
70
|
|
|
* @return \Illuminate\Http\JsonResponse |
71
|
|
|
*/ |
72
|
|
|
public function show($id) |
73
|
|
|
{ |
74
|
|
|
$response = fractal()->item($this->model->findOrFail($id), new FieldTransformer())->toArray(); |
75
|
|
|
return response()->json($response); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param EditFieldRequest $request |
81
|
|
|
* @param $id |
82
|
|
|
* @param $fields |
83
|
|
|
* @return \Illuminate\Http\JsonResponse |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function update(EditFieldRequest $request, $id, $fields) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$data = $request->all(); |
88
|
|
|
$data['id'] = $fields; |
89
|
|
|
$field = $this->editField($data); |
90
|
|
|
$response = fractal()->item($field, new FieldTransformer()) |
91
|
|
|
->addMeta(['return_url' => $data['returnUrl']]) |
92
|
|
|
->toArray(); |
93
|
|
|
return response()->json($response); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $id |
99
|
|
|
* @param $fields |
100
|
|
|
* @return \Illuminate\Http\JsonResponse |
101
|
|
|
*/ |
102
|
|
|
public function destroy($id, $fields) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$this->deleteField($fields); |
105
|
|
|
return response()->json(null, 204); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $type |
111
|
|
|
* @return \Illuminate\Http\JsonResponse |
112
|
|
|
*/ |
113
|
|
|
public function fieldTypeForm($type) |
114
|
|
|
{ |
115
|
|
|
$this->setFieldType($type); |
116
|
|
|
return response()->json(['form' => $this->fieldType->getOptionsForm()]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $type |
122
|
|
|
*/ |
123
|
|
|
private function setFieldType($type) |
124
|
|
|
{ |
125
|
|
|
$fieldTypes = app('field.types'); |
126
|
|
|
if (!isset($fieldTypes->types[$type])) { |
127
|
|
|
throw new NotAcceptableHttpException('El field type ' . $type . ' no esta registrado'); |
128
|
|
|
} |
129
|
|
|
$this->fieldType = app($fieldTypes->types[$type]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param Request $request |
135
|
|
|
* @param $id |
136
|
|
|
* @return \Illuminate\Http\JsonResponse |
137
|
|
|
*/ |
138
|
|
|
public function reOrderFieldId(Request $request, $id) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$this->reOrderField($request->get('items')); |
141
|
|
|
return response()->json(['ok' => true]); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.