Skip to content

Added the option to make Asset serials required and error messages #14955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
3 changes: 2 additions & 1 deletion app/Http/Controllers/Assets/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ public function edit($assetId = null) : View | RedirectResponse

return view('hardware/edit', compact('item'))
->with('statuslabel_list', Helper::statusLabelList())
->with('statuslabel_types', Helper::statusTypeList());
->with('statuslabel_types', Helper::statusTypeList())
->with('settings', Setting::getSettings());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}


Expand Down
16 changes: 14 additions & 2 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Models\User;
use App\Notifications\FirstAdminNotification;
use App\Notifications\MailTest;
use Illuminate\Http\Client\HttpClientException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Storage;
Expand Down Expand Up @@ -323,8 +324,19 @@ public function postSettings(Request $request) : RedirectResponse
}

$setting->full_multiple_companies_support = $request->input('full_multiple_companies_support', '0');
$setting->unique_serial = $request->input('unique_serial', '0');
$setting->shortcuts_enabled = $request->input('shortcuts_enabled', '0');
if(!(Asset::select('serial')->groupBy('serial')->havingRaw('COUNT(*) > 1')->get()->count() > 0)){
$setting->unique_serial = $request->input('unique_serial');
}
else if($request->filled('unique_serial')){

return redirect()->route('settings.general.index')->with('error', trans('admin/settings/general.unique_serial_error', ['count' => Asset::select('serial')->groupBy('serial')->havingRaw('COUNT(*) > 1')->get()->count()]));
}
if(!(Asset::where('serial', '=', null)->count() > 0)) {
$setting->required_serial = $request->input('required_serial');
}
else if($request->filled('required_serial')) {
return redirect()->route('settings.general.index')->with('error', trans('admin/settings/general.required_serial_error', ['count' => Asset::where('serial', '=', null)->count()]));
}
$setting->show_images_in_email = $request->input('show_images_in_email', '0');
$setting->show_archived_in_list = $request->input('show_archived_in_list', '0');
$setting->dashboard_message = $request->input('dashboard_message');
Expand Down
19 changes: 19 additions & 0 deletions app/Providers/ValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ public function boot()
return $count < 1;
}
});
/**
* Required or nullable field
*
* This checks for a boolean if something should be required. passing the boolean column name from the settings as the parameter
* will check the settings table and apply a requirement if true.
*/
Validator::extend('potentially_required', function ($attribute, $value, $parameters, $validator) {
$required_bool = $parameters[0] ?? null;

if ($required_bool && Setting::getSettings()->{$required_bool}) {
return !empty($value);
}

return true;
});
Validator::replacer('potentially_required', function ($message, $attribute, $rule, $parameters) {
return str_replace(':attribute', $attribute, ':attribute is required.');
});


/**
* Unique if undeleted for two columns
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('settings', function (Blueprint $table) {
$table->boolean('required_serial')->after('unique_serial')->nullable()->default(0);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('settings', function (Blueprint $table) {
$table->dropColumn('required_serial');
});
}
};
5 changes: 5 additions & 0 deletions resources/lang/en-US/admin/settings/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@
'vertical' => 'vertical',
'horizontal' => 'horizontal',
'unique_serial' => 'Unique serial numbers',
'required_serial' => 'Require Serial numbers',
'require_serial_help_text' => 'Checking this box will enforce all assets to require a serial.',
'unique_serial_help_text' => 'Checking this box will enforce a uniqueness constraint on asset serials',
'zerofill_count' => 'Length of asset tags, including zerofill',
'username_format_help' => 'This setting will only be used by the import process if a username is not provided and we have to generate a username for you.',
Expand Down Expand Up @@ -383,5 +385,8 @@
'restore_default_avatar_help' => '',
'due_checkin_days' => 'Due For Checkin Warning',
'due_checkin_days_help' => 'How many days before the expected checkin of an asset should it be listed in the "Due for checkin" page?',
'required_serial_error' => 'All current assets must have a serial before you can select this option. There are currently :count Assets without serials.',
'unique_serial_error' => 'All current assets must have a unique serial before you can select this option. There are currently :count Assets with duplicate or empty serials.',
'default_avatar' => 'Upload default avatar',

];
3 changes: 1 addition & 2 deletions resources/views/hardware/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
@endif
</div>

@include ('partials.forms.edit.serial', ['fieldname'=> 'serials[1]', 'old_val_name' => 'serials.1', 'translated_serial' => trans('admin/hardware/form.serial')])

@include ('partials.forms.edit.serial', ['fieldname'=> 'serials[1]', 'old_val_name' => 'serials.1', 'translated_serial' => trans('admin/hardware/form.serial'), 'settings' => $settings])
<div class="input_fields_wrap">
</div>

Expand Down
2 changes: 1 addition & 1 deletion resources/views/partials/forms/edit/serial.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="form-group {{ $errors->has('serial') ? ' has-error' : '' }}">
<label for="{{ $fieldname }}" class="col-md-3 control-label">{{ trans('admin/hardware/form.serial') }} </label>
<div class="col-md-7 col-sm-12{{ (Helper::checkIfRequired($item, 'serial')) ? ' required' : '' }}">
<input class="form-control" type="text" name="{{ $fieldname }}" id="{{ $fieldname }}" value="{{ old((isset($old_val_name) ? $old_val_name : $fieldname), $item->serial) }}" />
<input class="form-control" type="text" name="{{ $fieldname }}" id="{{ $fieldname }}" value="{{ old((isset($old_val_name) ? $old_val_name : $fieldname), $item->serial) }}" {{$settings->required_serial ? 'required' : ''}} />
{!! $errors->first('serial', '<span class="alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> :message</span>') !!}
</div>
</div>
19 changes: 19 additions & 0 deletions resources/views/settings/general.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
<p class="help-block">
{{ trans('admin/settings/general.unique_serial_help_text') }}
</p>

</div>
</div>

Expand All @@ -178,6 +179,24 @@
</div>
</div>

<!-- required serial -->
<div class="form-group">
<div class="col-md-3">
<strong>{{ trans('admin/settings/general.required_serial') }}</strong>
</div>
<div class="col-md-9">
<label class="form-control">
{{ Form::checkbox('required_serial', '1', Request::old('required_serial', $setting->required_serial),array('class' => 'minimal')) }}
{{ trans('general.yes') }}
{!! $errors->first('required_serial', '<span class="alert-msg" aria-hidden="true">:message</span>') !!}
</label>

<p class="help-block">
{{ trans('admin/settings/general.require_serial_help_text') }}
</p>
</div>
</div>


<!-- Per Page -->
<div class="form-group {{ $errors->has('per_page') ? 'error' : '' }}">
Expand Down
Loading