snipe-it/app/Models/Statuslabel.php
Till Deeke baa3be728d Refactoring: A nicer and easier syntax for searching models (#5841)
* Adds the ability to search by dates

Adding extra „where“-conditions to the „TextSearch“ queries, allowing the users to search by dates

* Adds missing dates to $dates in models

* Removes duplicated „where“ conditions

* Adds the Searchable trait to models, defining the searchable attributes and relations

* Removes the old text search methods

* Adds back additional conditions to the search

These conditions could not be modeled in the „attributes“ or „relations“, so we include them here

* Removes unnecessary check for the deleted_at attribute

* Fixes typo in comments

* suppresses errors from Codacy

We can safely ignore the error codacy is throwing here, since this method is a standin/noop for models who need to implement more advanced searches
2018-07-16 14:13:07 -07:00

129 lines
3.2 KiB
PHP
Executable file

<?php
namespace App\Models;
use App\Http\Traits\UniqueUndeletedTrait;
use App\Models\SnipeModel;
use App\Models\Traits\Searchable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Watson\Validating\ValidatingTrait;
class Statuslabel extends SnipeModel
{
use SoftDeletes;
use ValidatingTrait;
use UniqueUndeletedTrait;
protected $injectUniqueIdentifier = true;
protected $dates = ['deleted_at'];
protected $table = 'status_labels';
protected $hidden = ['user_id','deleted_at'];
protected $rules = array(
'name' => 'required|string|unique_undeleted',
'notes' => 'string|nullable',
'deployable' => 'required',
'pending' => 'required',
'archived' => 'required',
);
protected $fillable = [
'archived',
'deployable',
'name',
'notes',
'pending',
];
use Searchable;
/**
* The attributes that should be included when searching the model.
*
* @var array
*/
protected $searchableAttributes = ['name'];
/**
* The relations and their attributes that should be included when searching the model.
*
* @var array
*/
protected $searchableRelations = [];
/**
* Get assets with associated status label
*
* @return \Illuminate\Support\Collection
*/
public function assets()
{
return $this->hasMany('\App\Models\Asset', 'status_id');
}
public function getStatuslabelType()
{
if (($this->pending == '1') && ($this->archived == '0') && ($this->deployable == '0')) {
return 'pending';
} elseif (($this->pending == '0') && ($this->archived == '1') && ($this->deployable == '0')) {
return 'archived';
} elseif (($this->pending == '0') && ($this->archived == '0') && ($this->deployable == '0')) {
return 'undeployable';
}
return 'deployable';
}
public function scopePending()
{
return $this->where('pending', '=', 1)
->where('archived', '=', 0)
->where('deployable', '=', 0);
}
public function scopeArchived()
{
return $this->where('pending', '=', 0)
->where('archived', '=', 1)
->where('deployable', '=', 0);
}
public function scopeDeployable()
{
return $this->where('pending', '=', 0)
->where('archived', '=', 0)
->where('deployable', '=', 1);
}
public static function getStatuslabelTypesForDB($type)
{
$statustype['pending'] = 0;
$statustype['deployable'] = 0;
$statustype['archived'] = 0;
if ($type == 'pending') {
$statustype['pending'] = 1;
$statustype['deployable'] = 0;
$statustype['archived'] = 0;
} elseif ($type == 'deployable') {
$statustype['pending'] = 0;
$statustype['deployable'] = 1;
$statustype['archived'] = 0;
} elseif ($type == 'archived') {
$statustype['pending'] = 0;
$statustype['deployable'] = 0;
$statustype['archived'] = 1;
}
return $statustype;
}
}