mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-24 21:24:13 -08:00
Putting uniqueUndeleted into a trait for use across models
This commit is contained in:
parent
60560e4994
commit
2e665e128f
23
app/Http/Traits/UniqueUndeletedTrait.php
Normal file
23
app/Http/Traits/UniqueUndeletedTrait.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
namespace App\Http\Traits;
|
||||
|
||||
trait UniqueUndeletedTrait
|
||||
{
|
||||
|
||||
/**
|
||||
* Prepare a unique_ids rule, adding a model identifier if required.
|
||||
*
|
||||
* @param array $parameters
|
||||
* @param string $field
|
||||
* @return string
|
||||
*/
|
||||
protected function prepareUniqueUndeletedRule($parameters, $field)
|
||||
{
|
||||
// Only perform a replacement if the model has been persisted.
|
||||
if ($this->exists) {
|
||||
return 'unique_undeleted:'.$this->table.','. $this->getKey();
|
||||
}
|
||||
|
||||
return 'unique_undeleted:'.$this->table.',0';
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
|||
use Log;
|
||||
use Parsedown;
|
||||
use Watson\Validating\ValidatingTrait;
|
||||
use App\Http\Traits\UniqueUndeletedTrait;
|
||||
use DateTime;
|
||||
use App\Models\Setting;
|
||||
use App\Helpers\Helper;
|
||||
|
@ -40,6 +41,7 @@ class Asset extends Depreciable
|
|||
*/
|
||||
protected $injectUniqueIdentifier = true;
|
||||
use ValidatingTrait;
|
||||
use UniqueUndeletedTrait;
|
||||
|
||||
protected $rules = [
|
||||
'name' => 'min:2|max:255',
|
||||
|
@ -51,7 +53,7 @@ class Asset extends Depreciable
|
|||
'checkout_date' => 'date|max:10|min:10',
|
||||
'checkin_date' => 'date|max:10|min:10',
|
||||
'supplier_id' => 'integer',
|
||||
'asset_tag' => 'required|min:1|max:255|unique_undeleted:assets',
|
||||
'asset_tag' => 'required|min:1|max:255|unique_undeleted',
|
||||
'status' => 'integer',
|
||||
];
|
||||
|
||||
|
@ -70,6 +72,8 @@ class Asset extends Depreciable
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Checkout asset
|
||||
*/
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace App\Models;
|
|||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Watson\Validating\ValidatingTrait;
|
||||
use App\Http\Traits\UniqueUndeletedTrait;
|
||||
|
||||
/**
|
||||
* Model for Categories. Categories are a higher-level group
|
||||
|
@ -38,6 +39,7 @@ class Category extends Model
|
|||
*/
|
||||
protected $injectUniqueIdentifier = true;
|
||||
use ValidatingTrait;
|
||||
use UniqueUndeletedTrait;
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -3,6 +3,7 @@ namespace App\Models;
|
|||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Watson\Validating\ValidatingTrait;
|
||||
use App\Http\Traits\UniqueUndeletedTrait;
|
||||
use Auth;
|
||||
use DB;
|
||||
|
||||
|
@ -30,6 +31,7 @@ final class Company extends Model
|
|||
*/
|
||||
protected $injectUniqueIdentifier = true;
|
||||
use ValidatingTrait;
|
||||
use UniqueUndeletedTrait;
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
|||
use App\Models\User;
|
||||
use App\Models\Asset;
|
||||
use Watson\Validating\ValidatingTrait;
|
||||
use App\Http\Traits\UniqueUndeletedTrait;
|
||||
|
||||
class Location extends Model
|
||||
{
|
||||
|
@ -31,6 +32,7 @@ class Location extends Model
|
|||
*/
|
||||
protected $injectUniqueIdentifier = true;
|
||||
use ValidatingTrait;
|
||||
use UniqueUndeletedTrait;
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -4,11 +4,13 @@ namespace App\Models;
|
|||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Watson\Validating\ValidatingTrait;
|
||||
use App\Http\Traits\UniqueUndeletedTrait;
|
||||
|
||||
class Statuslabel extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
use ValidatingTrait;
|
||||
use UniqueUndeletedTrait;
|
||||
|
||||
protected $injectUniqueIdentifier = true;
|
||||
protected $dates = ['deleted_at'];
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace App\Models;
|
|||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Watson\Validating\ValidatingTrait;
|
||||
use App\Http\Traits\UniqueUndeletedTrait;
|
||||
|
||||
class Supplier extends Model
|
||||
{
|
||||
|
@ -35,6 +36,7 @@ class Supplier extends Model
|
|||
*/
|
||||
protected $injectUniqueIdentifier = true;
|
||||
use ValidatingTrait;
|
||||
use UniqueUndeletedTrait;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
|
|
|
@ -7,8 +7,8 @@ use Illuminate\Auth\Passwords\CanResetPassword;
|
|||
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
||||
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
|
||||
use Watson\Validating\ValidatingTrait;
|
||||
use App\Models\Company;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use App\Http\Traits\UniqueUndeletedTrait;
|
||||
|
||||
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
|
||||
{
|
||||
|
@ -17,6 +17,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
|
|||
use ValidatingTrait;
|
||||
use Authenticatable;
|
||||
use CanResetPassword;
|
||||
use UniqueUndeletedTrait;
|
||||
|
||||
protected $dates = ['deleted_at'];
|
||||
protected $table = 'users';
|
||||
|
|
|
@ -5,6 +5,7 @@ use Validator;
|
|||
use Illuminate\Support\ServiceProvider;
|
||||
use DB;
|
||||
|
||||
|
||||
/**
|
||||
* This service provider handles a few custom validation rules.
|
||||
*
|
||||
|
@ -54,13 +55,13 @@ class AppServiceProvider extends ServiceProvider
|
|||
// (I think this is a bug in Laravel's validator?)
|
||||
Validator::extend('unique_undeleted', function($attribute, $value, $parameters, $validator) {
|
||||
|
||||
$count = DB::table($parameters[0])->where($attribute,'=',$value)->whereNull('deleted_at')->count();
|
||||
$count = DB::table($parameters[0])->select('id')->where($attribute,'=',$value)->whereNull('deleted_at')->where('id','!=',$parameters[1])->count();
|
||||
|
||||
if ($count < 1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue