Cachet/app/Models/Component.php

167 lines
3.7 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of Cachet.
*
2015-07-06 17:37:01 +01:00
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Models;
use AltThree\Validator\ValidatingTrait;
2015-06-16 09:46:29 +01:00
use CachetHQ\Cachet\Presenters\ComponentPresenter;
2015-01-01 18:57:33 +00:00
use Illuminate\Database\Eloquent\Builder;
2015-01-01 12:23:17 +00:00
use Illuminate\Database\Eloquent\Model;
2015-03-20 18:30:45 -06:00
use Illuminate\Database\Eloquent\SoftDeletes;
use McCool\LaravelAutoPresenter\HasPresenter;
class Component extends Model implements HasPresenter
2014-12-20 21:20:17 +00:00
{
2015-03-20 18:30:45 -06:00
use SoftDeletes, ValidatingTrait;
2015-01-01 18:57:33 +00:00
/**
* List of attributes that have default values.
*
* @var mixed[]
*/
protected $attributes = [
'order' => 0,
'group_id' => 0,
'description' => '',
'link' => '',
];
/**
* The attributes that should be casted to native types.
2015-01-01 18:57:33 +00:00
*
* @var string[]
*/
protected $casts = [
'id' => 'int',
'order' => 'int',
'group_id' => 'int',
'description' => 'string',
'link' => 'string',
2015-10-01 21:05:14 +01:00
'deleted_at' => 'date',
];
2015-01-01 18:57:33 +00:00
/**
* The fillable properties.
*
* @var string[]
*/
2015-01-04 20:56:10 +00:00
protected $fillable = [
'name',
'description',
'status',
'tags',
'link',
'order',
'group_id',
];
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'name' => 'required|string',
2015-10-20 20:33:23 +01:00
'status' => 'int|required',
'link' => 'url',
];
2015-01-04 20:56:10 +00:00
/**
* Components can belong to a group.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function group()
{
2015-06-16 09:46:29 +01:00
return $this->belongsTo(ComponentGroup::class, 'group_id', 'id');
2015-01-04 20:56:10 +00:00
}
/**
* Lookup all of the incidents reported on the component.
2014-12-30 18:19:22 +00:00
*
2015-01-01 18:57:33 +00:00
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2014-12-20 21:20:17 +00:00
public function incidents()
{
2015-06-16 09:46:29 +01:00
return $this->hasMany(Incident::class, 'component_id', 'id');
}
/**
* Components can have many tags.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags()
{
2015-06-16 09:46:29 +01:00
return $this->belongsToMany(Tag::class);
}
2014-12-31 15:56:08 +00:00
/**
* Finds all components by status.
*
2015-01-01 12:23:17 +00:00
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $status
2014-12-31 15:56:08 +00:00
*
2015-01-01 12:23:17 +00:00
* @return \Illuminate\Database\Eloquent\Builder
2014-12-31 15:56:08 +00:00
*/
2015-01-01 18:57:33 +00:00
public function scopeStatus(Builder $query, $status)
2014-12-31 15:56:08 +00:00
{
return $query->where('status', $status);
}
/**
* Finds all components which don't have the given status.
*
2015-01-01 12:23:17 +00:00
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $status
2014-12-31 15:56:08 +00:00
*
2015-01-01 12:23:17 +00:00
* @return \Illuminate\Database\Eloquent\Builder
2014-12-31 15:56:08 +00:00
*/
2015-01-01 18:57:33 +00:00
public function scopeNotStatus(Builder $query, $status)
2014-12-31 15:56:08 +00:00
{
return $query->where('status', '<>', $status);
}
/**
* Looks up the human readable version of the status.
2014-12-30 18:19:22 +00:00
*
* @return string
*/
2014-12-20 21:20:17 +00:00
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()
{
2015-06-16 09:46:29 +01:00
return ComponentPresenter::class;
}
}