Skip to content

Commit 1a541ce

Browse files
committed
Merge remote-tracking branch 'origin/develop'
Signed-off-by: snipe <[email protected]> # Conflicts: # .all-contributorsrc # CONTRIBUTORS.md
2 parents c1937b6 + aebe969 commit 1a541ce

23 files changed

+527
-145
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,6 +3137,15 @@
31373137
"doc"
31383138
]
31393139
},
3140+
{
3141+
"login": "FlorentDotMe",
3142+
"name": "Florent Bervas",
3143+
"avatar_url": "https://avatars.githubusercontent.com/u/292081?v=4",
3144+
"profile": "http://spoontux.net",
3145+
"contributions": [
3146+
"code"
3147+
]
3148+
},
31403149
{
31413150
"login": "Galaxy102",
31423151
"name": "Konstantin Köhring",
@@ -3146,5 +3155,6 @@
31463155
"code"
31473156
]
31483157
}
3158+
31493159
]
31503160
}

CONTRIBUTORS.md

Lines changed: 50 additions & 50 deletions
Large diffs are not rendered by default.

app/Http/Controllers/Api/ConsumablesController.php

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Events\CheckoutableCheckedOut;
66
use App\Helpers\Helper;
77
use App\Http\Controllers\Controller;
8+
use App\Http\Requests\StoreConsumableRequest;
89
use App\Http\Transformers\ConsumablesTransformer;
910
use App\Http\Transformers\SelectlistTransformer;
1011
use App\Models\Company;
@@ -27,27 +28,8 @@ public function index(Request $request) : array
2728
{
2829
$this->authorize('index', Consumable::class);
2930

30-
// This array is what determines which fields should be allowed to be sorted on ON the table itself, no relations
31-
// Relations will be handled in query scopes a little further down.
32-
$allowed_columns =
33-
[
34-
'id',
35-
'name',
36-
'order_number',
37-
'min_amt',
38-
'purchase_date',
39-
'purchase_cost',
40-
'company',
41-
'category',
42-
'model_number',
43-
'item_no',
44-
'qty',
45-
'image',
46-
'notes',
47-
];
48-
49-
$consumables = Consumable::select('consumables.*')
50-
->with('company', 'location', 'category', 'users', 'manufacturer');
31+
$consumables = Consumable::with('company', 'location', 'category', 'supplier', 'manufacturer')
32+
->withCount('users as consumables_users_count');
5133

5234
if ($request->filled('search')) {
5335
$consumables = $consumables->TextSearch(e($request->input('search')));
@@ -89,15 +71,9 @@ public function index(Request $request) : array
8971
// Make sure the offset and limit are actually integers and do not exceed system limits
9072
$offset = ($request->input('offset') > $consumables->count()) ? $consumables->count() : app('api_offset_value');
9173
$limit = app('api_limit_value');
92-
93-
$allowed_columns = ['id', 'name', 'order_number', 'min_amt', 'purchase_date', 'purchase_cost', 'company', 'category', 'model_number', 'item_no', 'manufacturer', 'location', 'qty', 'image'];
9474
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
9575

96-
$sort_override = $request->input('sort');
97-
$column_sort = in_array($sort_override, $allowed_columns) ? $sort_override : 'created_at';
98-
99-
100-
switch ($sort_override) {
76+
switch ($request->input('sort')) {
10177
case 'category':
10278
$consumables = $consumables->OrderCategory($order);
10379
break;
@@ -111,10 +87,30 @@ public function index(Request $request) : array
11187
$consumables = $consumables->OrderCompany($order);
11288
break;
11389
case 'supplier':
114-
$components = $consumables->OrderSupplier($order);
90+
$consumables = $consumables->OrderSupplier($order);
11591
break;
11692
default:
117-
$consumables = $consumables->orderBy($column_sort, $order);
93+
// This array is what determines which fields should be allowed to be sorted on ON the table itself.
94+
// These must match a column on the consumables table directly.
95+
$allowed_columns = [
96+
'id',
97+
'name',
98+
'order_number',
99+
'min_amt',
100+
'purchase_date',
101+
'purchase_cost',
102+
'company',
103+
'category',
104+
'model_number',
105+
'item_no',
106+
'manufacturer',
107+
'location',
108+
'qty',
109+
'image'
110+
];
111+
112+
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
113+
$consumables = $consumables->orderBy($sort, $order);
118114
break;
119115
}
120116

@@ -131,7 +127,7 @@ public function index(Request $request) : array
131127
* @since [v4.0]
132128
* @param \App\Http\Requests\ImageUploadRequest $request
133129
*/
134-
public function store(ImageUploadRequest $request) : JsonResponse
130+
public function store(StoreConsumableRequest $request) : JsonResponse
135131
{
136132
$this->authorize('create', Consumable::class);
137133
$consumable = new Consumable;
@@ -167,7 +163,7 @@ public function show($id) : array
167163
* @param \App\Http\Requests\ImageUploadRequest $request
168164
* @param int $id
169165
*/
170-
public function update(ImageUploadRequest $request, $id) : JsonResponse
166+
public function update(StoreConsumableRequest $request, $id) : JsonResponse
171167
{
172168
$this->authorize('update', Consumable::class);
173169
$consumable = Consumable::findOrFail($id);

app/Http/Controllers/Consumables/ConsumableCheckoutController.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
use App\Events\CheckoutableCheckedOut;
66
use App\Http\Controllers\Controller;
7-
use App\Models\Accessory;
87
use App\Models\Consumable;
98
use App\Models\User;
109
use Illuminate\Http\Request;
11-
use Illuminate\Support\Facades\Auth;
12-
use Illuminate\Support\Facades\Input;
10+
use \Illuminate\Contracts\View\View;
11+
use \Illuminate\Http\RedirectResponse;
1312

1413
class ConsumableCheckoutController extends Controller
1514
{
@@ -20,13 +19,11 @@ class ConsumableCheckoutController extends Controller
2019
* @see ConsumableCheckoutController::store() method that stores the data.
2120
* @since [v1.0]
2221
* @param int $id
23-
* @return \Illuminate\Contracts\View\View
24-
* @throws \Illuminate\Auth\Access\AuthorizationException
2522
*/
26-
public function create($id)
23+
public function create($id) : View | RedirectResponse
2724
{
2825

29-
if ($consumable = Consumable::with('users')->find($id)) {
26+
if ($consumable = Consumable::find($id)) {
3027

3128
$this->authorize('checkout', $consumable);
3229

app/Http/Controllers/Consumables/ConsumablesController.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
use App\Models\Company;
99
use App\Models\Consumable;
1010
use Illuminate\Support\Facades\Auth;
11-
use Illuminate\Support\Facades\Input;
1211
use Illuminate\Support\Facades\Validator;
12+
use Illuminate\Http\RedirectResponse;
13+
use \Illuminate\Contracts\View\View;
14+
use App\Http\Requests\StoreConsumableRequest;
1315

1416
/**
1517
* This controller handles all actions related to Consumables for
@@ -62,7 +64,7 @@ public function create()
6264
* @return \Illuminate\Http\RedirectResponse
6365
* @throws \Illuminate\Auth\Access\AuthorizationException
6466
*/
65-
public function store(ImageUploadRequest $request)
67+
public function store(StoreConsumableRequest $request)
6668
{
6769
$this->authorize('create', Consumable::class);
6870
$consumable = new Consumable();
@@ -99,10 +101,8 @@ public function store(ImageUploadRequest $request)
99101
* @param int $consumableId
100102
* @see ConsumablesController::postEdit() method that stores the form data.
101103
* @since [v1.0]
102-
* @return \Illuminate\Contracts\View\View
103-
* @throws \Illuminate\Auth\Access\AuthorizationException
104104
*/
105-
public function edit($consumableId = null)
105+
public function edit($consumableId = null) : View | RedirectResponse
106106
{
107107
if ($item = Consumable::find($consumableId)) {
108108
$this->authorize($item);
@@ -124,7 +124,7 @@ public function edit($consumableId = null)
124124
* @see ConsumablesController::getEdit() method that stores the form data.
125125
* @since [v1.0]
126126
*/
127-
public function update(ImageUploadRequest $request, $consumableId = null)
127+
public function update(StoreConsumableRequest $request, $consumableId = null)
128128
{
129129
if (is_null($consumable = Consumable::find($consumableId))) {
130130
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.does_not_exist'));
@@ -182,6 +182,7 @@ public function destroy($consumableId)
182182
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.not_found'));
183183
}
184184
$this->authorize($consumable);
185+
185186
$consumable->delete();
186187
// Redirect to the locations management page
187188
return redirect()->route('consumables.index')->with('success', trans('admin/consumables/message.delete.success'));
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use App\Models\Consumable;
6+
use App\Models\Category;
7+
use Illuminate\Support\Facades\Gate;
8+
9+
class StoreConsumableRequest extends ImageUploadRequest
10+
{
11+
12+
/**
13+
* Determine if the user is authorized to make this request.
14+
*/
15+
public function authorize(): bool
16+
{
17+
return Gate::allows('create', new Consumable);
18+
}
19+
20+
public function prepareForValidation(): void
21+
{
22+
23+
if ($this->category_id) {
24+
if ($category = Category::find($this->category_id)) {
25+
$this->merge([
26+
'category_type' => $category->category_type ?? null,
27+
]);
28+
}
29+
}
30+
31+
}
32+
33+
/**
34+
* Get the validation rules that apply to the request.
35+
*
36+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
37+
*/
38+
public function rules(): array
39+
{
40+
return array_merge(
41+
['category_type' => 'in:consumable'],
42+
parent::rules(),
43+
);
44+
}
45+
46+
public function messages(): array
47+
{
48+
$messages = ['category_type.in' => trans('admin/consumables/message.invalid_category_type')];
49+
return $messages;
50+
}
51+
52+
public function response(array $errors)
53+
{
54+
return $this->redirector->back()->withInput()->withErrors($errors, $this->errorBag);
55+
}
56+
}

0 commit comments

Comments
 (0)