Start of search, reorged Service and Account models

This commit is contained in:
Deon George
2019-06-21 16:21:48 +10:00
parent 3ff82f5f10
commit a426c7b1a4
4 changed files with 277 additions and 41 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Models;
use App\User;
use Illuminate\Database\Eloquent\Model;
use App\Traits\NextKey;
@@ -69,19 +70,76 @@ class Account extends Model
return $this->belongsTo(\App\User::class);
}
/** SCOPES */
/**
* Search for a record
*
* @param User $uo
* @return
*/
public function scopeSearch($query,string $term)
{
// Build our where clause
// First Name, Last name
if (preg_match('/\ /',$term)) {
list($fn,$ln) = explode(' ',$term,2);
$query->where(function($query1) use ($fn,$ln,$term) {
$query1->where(function($query2) use ($fn,$ln) {
return $query2
->where('first_name','like','%'.$fn.'%')
->where('last_name','like','%'.$ln.'%');
})
->orWhere('company','like','%'.$term.'%');
});
} elseif (is_numeric($term)) {
$query->where('id','like','%'.$term.'%');
} elseif (preg_match('/\@/',$term)) {
$query->where('email','like','%'.$term.'%');
} else {
$query
->where('company','like','%'.$term.'%')
->orWhere('first_name','like','%'.$term.'%')
->orWhere('last_name','like','%'.$term.'%');
}
return $query;
}
/** ATTRIBUTES **/
public function getActiveDisplayAttribute($value)
{
return sprintf('<span class="btn-sm btn-block btn-%s text-center">%s</span>',$this->active ? 'success' : 'danger',$this->active ? 'Active' : 'Inactive');
}
/**
* @deprecated use getAIDAttribute()
*/
public function getAccountIdAttribute()
{
return sprintf('%02s-%04s',$this->site_id,$this->id);
return $this->getAIDAttribute();
}
/**
* @deprecated use getUrlAdminAttribute()
*/
public function getAccountIdUrlAttribute()
{
return sprintf('<a href="/r/account/view/%s">%s</a>',$this->id,$this->account_id);
return $this->getUrlAdminAttribute();
}
/**
* Return the Account Unique Identifier
* @return string
*/
public function getAIDAttribute()
{
return sprintf('%02s-%04s',$this->site_id,$this->id);
}
public function getNameAttribute()
@@ -104,6 +162,28 @@ class Account extends Model
return $this->company ? 'Business' : 'Private';
}
/**
* Return the Admin URL to manage the account
*
* @return string
*/
public function getUrlAdminAttribute(): string
{
return sprintf('<a href="/r/account/view/%s">%s</a>',$this->id,$this->account_id);
}
/**
* Return the User URL to manage the account
*
* @return string
*/
public function getUrlUserAttribute(): string
{
return sprintf('<a href="/u/account/view/%s">%s</a>',$this->id,$this->account_id);
}
/** FUNCTIONS **/
private function _address()
{
$return = [];