Skip to content

Commit b934d2e

Browse files
committed
Merge branch 'develop'
# Conflicts: # config/version.php
2 parents fcc87b3 + 8cf70e7 commit b934d2e

File tree

11 files changed

+131
-50
lines changed

11 files changed

+131
-50
lines changed

app/Http/Controllers/Api/AssetsController.php

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use App\Helpers\Helper;
55
use App\Http\Controllers\Controller;
66
use App\Http\Requests\AssetRequest;
7+
use App\Http\Requests\AssetCheckoutRequest;
78
use App\Http\Transformers\AssetsTransformer;
89
use App\Models\Asset;
910
use App\Models\AssetModel;
@@ -506,7 +507,7 @@ public function destroy($id)
506507
* @since [v4.0]
507508
* @return JsonResponse
508509
*/
509-
public function checkout(Request $request, $asset_id)
510+
public function checkout(AssetCheckoutRequest $request, $asset_id)
510511
{
511512
$this->authorize('checkout', Asset::class);
512513
$asset = Asset::findOrFail($asset_id);
@@ -522,27 +523,37 @@ public function checkout(Request $request, $asset_id)
522523
'id' => $asset->id,
523524
'asset_tag' => $asset->asset_tag,
524525
];
525-
if ($request->has('user_id')) {
526-
$target = User::find($request->input('user_id'));
527-
$location = $target->location_id;
528-
$error_payload['target_id'] = $request->input('user_id');
529-
$error_payload['target_type'] = User::class;
530-
// Don't let the user check an asset out to itself
531-
} elseif ($request->has('asset_id')) {
532-
$target = Asset::where('id','!=',$asset_id)->find($request->input('asset_id'));
533-
$location = $target->location_id;
534-
$error_payload['target_id'] = $request->input('asset_id');
535-
$error_payload['target_type'] = Asset::class;
536-
} elseif ($request->has('location_id')) {
537-
$target = Location::find($request->input('location_id'));
538-
$location = $target->id;
539-
$target = Location::find($request->input('location_id'));
540-
$error_payload['target_id'] = $request->input('location_id');
541-
$error_payload['target_type'] = Location::class;
526+
527+
528+
// This item is checked out to a location
529+
if (request('checkout_to_type')=='location') {
530+
$target = Location::find(request('assigned_location'));
531+
$asset->location_id = ($target) ? $target->id : '';
532+
$error_payload['target_id'] = $request->input('assigned_location');
533+
$error_payload['target_type'] = 'location';
534+
535+
} elseif (request('checkout_to_type')=='asset') {
536+
$target = Asset::where('id','!=',$assetId)->find(request('assigned_asset'));
537+
$asset->location_id = $target->rtd_location_id;
538+
// Override with the asset's location_id if it has one
539+
if ($target->location_id!='') {
540+
$asset->location_id = ($target) ? $target->location_id : '';
541+
}
542+
$error_payload['target_id'] = $request->input('assigned_asset');
543+
$error_payload['target_type'] = 'asset';
544+
545+
} elseif (request('checkout_to_type')=='user') {
546+
// Fetch the target and set the asset's new location_id
547+
$target = User::find(request('assigned_user'));
548+
$asset->location_id = ($target) ? $target->location_id : '';
549+
$error_payload['target_id'] = $request->input('assigned_user');
550+
$error_payload['target_type'] = 'user';
542551
}
543552

553+
554+
544555
if (!isset($target)) {
545-
return response()->json(Helper::formatStandardApiResponse('error', $error_payload, 'No valid checkout target specified for asset '.e($asset->asset_tag).'.'));
556+
return response()->json(Helper::formatStandardApiResponse('error', $error_payload, 'Checkout target for asset '.e($asset->asset_tag).' is invalid - '.$error_payload['target_type'].' does not exist.'));
546557
}
547558

548559

@@ -557,11 +568,11 @@ public function checkout(Request $request, $asset_id)
557568
$asset->location_id = $target->rtd_location_id;
558569
}
559570

560-
$asset->location_id = $location;
571+
561572

562573

563574

564-
if ($asset->checkOut($target, Auth::user(), $checkout_at, $expected_checkin, $note, $asset_name, $location)) {
575+
if ($asset->checkOut($target, Auth::user(), $checkout_at, $expected_checkin, $note, $asset_name, $asset->location_id)) {
565576
return response()->json(Helper::formatStandardApiResponse('success', ['asset'=> e($asset->asset_tag)], trans('admin/hardware/message.checkout.success')));
566577
}
567578

app/Http/Controllers/AssetsController.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,13 @@ public function getCheckout($assetId)
431431

432432
$this->authorize('checkout', $asset);
433433

434+
if ($asset->availableForCheckout()) {
435+
return view('hardware/checkout', compact('asset'));
436+
}
437+
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkout.not_available'));
438+
434439
// Get the dropdown of users and then pass it to the checkout view
435-
return view('hardware/checkout', compact('asset'));
440+
436441
}
437442

438443
/**
@@ -454,28 +459,30 @@ public function postCheckout(AssetCheckoutRequest $request, $assetId)
454459
}
455460
$this->authorize('checkout', $asset);
456461
$admin = Auth::user();
457-
462+
463+
458464
// This item is checked out to a location
459465
if (request('checkout_to_type')=='location') {
460466
$target = Location::find(request('assigned_location'));
461467
$asset->location_id = ($target) ? $target->id : '';
468+
462469
} elseif (request('checkout_to_type')=='asset') {
463470
$target = Asset::where('id','!=',$assetId)->find(request('assigned_asset'));
464471
$asset->location_id = $target->rtd_location_id;
465472
// Override with the asset's location_id if it has one
466473
if ($target->location_id!='') {
467474
$asset->location_id = ($target) ? $target->location_id : '';
468475
}
469-
} else {
476+
477+
} elseif (request('checkout_to_type')=='user') {
470478
// Fetch the target and set the asset's new location_id
471479
$target = User::find(request('assigned_user'));
472480
$asset->location_id = ($target) ? $target->location_id : '';
473481
}
474482

475-
476483
// No valid target was found - error out
477484
if (!$target) {
478-
return redirect()->to("hardware/$assetId/checkout")->with('error', trans('admin/hardware/message.checkout.error'))->withErrors($asset->getErrors());
485+
return redirect()->back()->with('error', trans('admin/hardware/message.checkout.error'))->withErrors($asset->getErrors());
479486
}
480487

481488

app/Http/Requests/AssetCheckoutRequest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ public function authorize()
2424
public function rules()
2525
{
2626
$rules = [
27-
"assigned_user" => 'required_without_all:assigned_asset,assigned_location',
28-
"assigned_asset" => 'required_without_all:assigned_user,assigned_location|different:'.$this->id,
29-
"assigned_location" => 'required_without_all:assigned_user,assigned_asset',
27+
"assigned_user" => 'required_without_all:assigned_asset,assigned_location',
28+
"assigned_asset" => 'required_without_all:assigned_user,assigned_location|different:'.$this->id,
29+
"assigned_location" => 'required_without_all:assigned_user,assigned_asset',
30+
"checkout_to_type" => 'required|in:asset,location,user'
3031
];
3132

3233

app/Models/CustomField.php

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ class CustomField extends Model
1616
public static $PredefinedFormats=[
1717
"ANY" => "",
1818
"ALPHA" => "alpha",
19+
"ALPHA-DASH" => "alpha_dash",
20+
"NUMERIC" => "numeric",
21+
"ALPHA-NUMERIC" => "alpha_num",
1922
"EMAIL" => "email",
2023
"DATE" => "date",
2124
"URL" => "url",
22-
"NUMERIC" => "numeric",
23-
"MAC" => "regex:/^[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}$/",
2425
"IP" => "ip",
26+
"IPV4" => "ipv4",
27+
"IPV6" => "ipv6",
28+
"MAC" => "regex:/^[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}$/",
29+
"BOOLEAN" => "boolean",
2530
];
2631

2732
public $rules = [
@@ -30,36 +35,64 @@ class CustomField extends Model
3035

3136
// This is confusing, since it's actually the custom fields table that
3237
// we're usually modifying, but since we alter the assets table, we have to
33-
// say that here
38+
// say that here, otherwise the new fields get added onto the custom fields
39+
// table instead of the assets table.
3440
public static $table_name = "assets";
3541

42+
43+
/**
44+
* Convert the custom field's name property to a db-safe string.
45+
*
46+
* We could probably have used str_slug() here but not sure what it would
47+
* do with previously existing values. - @snipe
48+
*
49+
* @author [A. Gianotto] [<[email protected]>]
50+
* @since [v3.4]
51+
* @return String
52+
*/
3653
public static function name_to_db_name($name)
3754
{
3855
return "_snipeit_" . preg_replace("/[^a-zA-Z0-9]/", "_", strtolower($name));
3956
}
4057

58+
/**
59+
* Set some boot methods for creating and updating.
60+
*
61+
* There is never ever a time when we wouldn't want to be updating those asset
62+
* column names and the values of the db column name in the custom fields table
63+
* if they have changed, so we handle that here so that we don't have to remember
64+
* to do it in the controllers.
65+
*
66+
* @author [A. Gianotto] [<[email protected]>]
67+
* @since [v3.4]
68+
* @return Boolean
69+
*/
4170
public static function boot()
4271
{
4372
self::created(function ($custom_field) {
4473

45-
// column exists - nothing to do here
74+
// Column already exists on the assets table - nothing to do here.
75+
// This *shouldn't* happen in the wild.
4676
if (Schema::hasColumn(CustomField::$table_name, $custom_field->convertUnicodeDbSlug())) {
4777
return false;
4878
}
4979

80+
// Update the column name in the assets table
5081
Schema::table(CustomField::$table_name, function ($table) use ($custom_field) {
5182
$table->text($custom_field->convertUnicodeDbSlug())->nullable();
5283
});
5384

85+
// Update the db_column property in the custom fields table
5486
$custom_field->db_column = $custom_field->convertUnicodeDbSlug();
5587
$custom_field->save();
5688
});
5789

5890

5991
self::updating(function ($custom_field) {
6092

61-
// Column already exists. Nothing to update.
93+
// Column already exists on the assets table - nothing to do here.
6294
if ($custom_field->isDirty("name")) {
95+
6396
if (Schema::hasColumn(CustomField::$table_name, $custom_field->convertUnicodeDbSlug())) {
6497
return true;
6598
}
@@ -113,11 +146,22 @@ public function db_column_name()
113146
return $this->db_column;
114147
}
115148

116-
// mutators for 'format' attribute
149+
/**
150+
* Mutator for the 'format' attribute.
151+
*
152+
* This is used by the dropdown to store the laravel-specific
153+
* validator strings in the database but still return the
154+
* user-friendly text in the dropdowns, and in the custom fields display.
155+
*
156+
* @author [A. Gianotto] [<[email protected]>]
157+
* @since [v3.4]
158+
* @return Array
159+
*/
117160
public function getFormatAttribute($value)
118161
{
119162
foreach (self::$PredefinedFormats as $name => $pattern) {
120-
if ($pattern===$value) {
163+
\Log::debug($name.'=>'.$pattern);
164+
if ($pattern === $value) {
121165
return $name;
122166
}
123167
}
@@ -168,6 +212,13 @@ public function formatFieldValuesAsArray()
168212
return $result;
169213
}
170214

215+
/**
216+
* Check whether the field is encrypted
217+
*
218+
* @author [A. Gianotto] [<[email protected]>]
219+
* @since [v3.4]
220+
* @return Boolean
221+
*/
171222
public function isFieldDecryptable($string)
172223
{
173224
if (($this->field_encrypted=='1') && ($string!='')) {
@@ -177,6 +228,14 @@ public function isFieldDecryptable($string)
177228
}
178229

179230

231+
/**
232+
* Convert non-UTF-8 or weirdly encoded text into something that
233+
* won't break the database.
234+
*
235+
* @author [A. Gianotto] [<[email protected]>]
236+
* @since [v3.4]
237+
* @return Boolean
238+
*/
180239
public function convertUnicodeDbSlug($original = null)
181240
{
182241
$name = $original ? $original : $this->name;

app/Presenters/AssetPresenter.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,8 @@ public function imageSrc()
289289
$imagePath = '';
290290
if ($this->image && !empty($this->image)) {
291291
$imagePath = $this->image;
292-
return 'poop';
293292
} elseif ($this->model && !empty($this->model->image)) {
294293
$imagePath = $this->model->image;
295-
return 'fart';
296294
}
297295
if (!empty($imagePath)) {
298296
return config('app.url').'/uploads/assets/'.$imagePath;

config/version.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22
return array (
3-
'app_version' => 'v4.1.6',
4-
'full_app_version' => 'v4.1.6 - build 2963-g83c8449',
3+
'app_version' => 'v4.1.6-pre',
4+
'full_app_version' => 'v4.1.6-pre - build 2963-g83c8449',
55
'build_version' => '2963',
66
'prerelease_version' => '',
77
'hash_version' => 'g83c8449',
88
'full_hash' => 'v4.1.5-57-g83c8449',
99
'branch' => 'master',
10-
);
10+
);

public/mix-manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"/css/app.css.map": "/css/app.css.map?id=bdbe05e6ecd70ccfac72",
99
"/css/overrides.css.map": "/css/overrides.css.map?id=898c91d4a425b01b589b",
1010
"/css/dist/all.css": "/css/dist/all.css?id=7c3842d2639193ac7e88",
11-
"/js/dist/all.js": "/js/dist/all.js?id=7e993fb3b457ccc72b3f",
11+
"/js/dist/all.js": "/js/dist/all.js?id=7b52ead3a55086ea1f8d",
1212
"/css/build/all.css": "/css/build/all.css?id=7c3842d2639193ac7e88",
13-
"/js/build/all.js": "/js/build/all.js?id=7e993fb3b457ccc72b3f"
13+
"/js/build/all.js": "/js/build/all.js?id=7b52ead3a55086ea1f8d"
1414
}

resources/assets/js/snipeit.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,22 +268,23 @@ $(document).ready(function () {
268268
$('#assigned_user').hide();
269269
$('#assigned_location').hide();
270270
$('.notification-callout').fadeOut();
271-
271+
272272
} else if (assignto_type == 'location') {
273273
$('#current_assets_box').fadeOut();
274274
$('#assigned_asset').hide();
275275
$('#assigned_user').hide();
276276
$('#assigned_location').show();
277277
$('.notification-callout').fadeOut();
278278
} else {
279+
279280
$('#assigned_asset').hide();
280281
$('#assigned_user').show();
281282
$('#assigned_location').hide();
282283
if (userid) {
283284
$('#current_assets_box').fadeIn();
284285
}
285286
$('.notification-callout').fadeIn();
286-
287+
287288
}
288289
});
289290
});

resources/views/components/view.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class="table table-striped snipe-table"
9696
@endif
9797

9898
@if ($component->order_number)
99-
<div class="col-md-12" style="padding-bottom: 5px;"><strong>{{ trans('admin/components/general.order') }}:</strong>
99+
<div class="col-md-12" style="padding-bottom: 5px;"><strong>{{ trans('general.order_number') }}:</strong>
100100
{{ $component->order_number }} </div>
101101
@endif
102102
</div>

0 commit comments

Comments
 (0)