Cachet/app/Models/Component.php
2015-06-10 13:36:26 +01:00

171 lines
3.9 KiB
PHP

<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use McCool\LaravelAutoPresenter\HasPresenter;
use Watson\Validating\ValidatingTrait;
/**
* @property int $id
* @property string $name
* @property string $description
* @property int $status
* @property string $link
* @property int $order
* @property int $group_id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property \Carbon\Carbon $deleted_at
*/
class Component extends Model implements HasPresenter
{
use SoftDeletes, ValidatingTrait;
/**
* The validation rules.
*
* @var string[]
*/
protected $rules = [
'name' => 'required|string',
'status' => 'integer|required',
'link' => 'url',
];
/**
* The fillable properties.
*
* @var string[]
*/
protected $fillable = [
'name',
'description',
'status',
'tags',
'link',
'order',
'group_id',
];
/**
* List of attributes that have default values.
*
* @var mixed[]
*/
protected $attributes = [
'order' => 0,
'group_id' => 0,
'description' => '',
'link' => '',
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
/**
* Components can belong to a group.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function group()
{
return $this->belongsTo('CachetHQ\Cachet\Models\ComponentGroup', 'group_id', 'id');
}
/**
* Lookup all of the incidents reported on the component.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function incidents()
{
return $this->hasMany('CachetHQ\Cachet\Models\Incident', 'component_id', 'id');
}
/**
* Components can have many tags.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags()
{
return $this->belongsToMany('CachetHQ\Cachet\Models\Tag');
}
/**
* Finds all components by status.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $status
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeStatus(Builder $query, $status)
{
return $query->where('status', $status);
}
/**
* Finds all components which don't have the given status.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $status
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotStatus(Builder $query, $status)
{
return $query->where('status', '<>', $status);
}
/**
* Looks up the human readable version of the status.
*
* @return string
*/
public function getHumanStatusAttribute()
{
return trans('cachet.components.status.'.$this->status);
}
/**
* Returns all of the tags on this component.
*
* @return string
*/
public function getTagsListAttribute()
{
$tags = $this->tags->map(function ($tag) {
return $tag->name;
});
return implode(', ', $tags->toArray());
}
/**
* Get the presenter class.
*
* @return string
*/
public function getPresenterClass()
{
return 'CachetHQ\Cachet\Presenters\ComponentPresenter';
}
}