2014-11-16 23:01:12 +00:00
|
|
|
<?php
|
|
|
|
|
2015-04-19 08:52:39 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
2015-07-06 17:37:01 +01:00
|
|
|
* (c) Alt Three Services Limited
|
2015-04-19 08:52:39 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2015-01-02 00:18:19 +00:00
|
|
|
namespace CachetHQ\Cachet\Models;
|
|
|
|
|
2015-08-03 22:32:36 +01:00
|
|
|
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;
|
2015-05-20 12:57:22 -05:00
|
|
|
use McCool\LaravelAutoPresenter\HasPresenter;
|
2014-11-27 16:05:00 +00:00
|
|
|
|
2015-05-20 12:57:22 -05:00
|
|
|
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;
|
2014-11-27 16:05:00 +00:00
|
|
|
|
2015-01-01 18:57:33 +00:00
|
|
|
/**
|
2015-08-22 15:57:53 +01: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[]
|
|
|
|
*/
|
2015-08-22 15:57:53 +01:00
|
|
|
protected $casts = [
|
|
|
|
'id' => 'int',
|
|
|
|
'order' => 'int',
|
|
|
|
'group_id' => 'int',
|
|
|
|
'description' => 'string',
|
|
|
|
'link' => 'string',
|
2015-10-01 21:05:14 +01:00
|
|
|
'deleted_at' => 'date',
|
2014-11-27 16:05:00 +00:00
|
|
|
];
|
|
|
|
|
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',
|
|
|
|
];
|
|
|
|
|
2015-01-05 23:00:41 +00:00
|
|
|
/**
|
2015-08-22 15:57:53 +01:00
|
|
|
* The validation rules.
|
2015-01-05 23:00:41 +00:00
|
|
|
*
|
2015-08-22 15:57:53 +01:00
|
|
|
* @var string[]
|
2015-01-05 23:00:41 +00:00
|
|
|
*/
|
2015-08-22 15:57:53 +01:00
|
|
|
public $rules = [
|
|
|
|
'name' => 'required|string',
|
2015-10-20 20:33:23 +01:00
|
|
|
'status' => 'int|required',
|
2015-08-22 15:57:53 +01:00
|
|
|
'link' => 'url',
|
2015-01-05 23:00:41 +00:00
|
|
|
];
|
|
|
|
|
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
|
|
|
}
|
2014-11-27 16:05:00 +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-11-27 16:05:00 +00:00
|
|
|
*/
|
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');
|
2014-11-27 16:05:00 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 09:51:22 +00:00
|
|
|
/**
|
|
|
|
* Components can have many tags.
|
|
|
|
*
|
2015-02-07 10:09:22 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
2015-01-16 09:51:22 +00:00
|
|
|
*/
|
|
|
|
public function tags()
|
|
|
|
{
|
2015-06-16 09:46:29 +01:00
|
|
|
return $this->belongsToMany(Tag::class);
|
2015-01-16 09:51:22 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-11-27 16:05:00 +00:00
|
|
|
/**
|
|
|
|
* Looks up the human readable version of the status.
|
2014-12-30 18:19:22 +00:00
|
|
|
*
|
2014-11-27 16:05:00 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-12-20 21:20:17 +00:00
|
|
|
public function getHumanStatusAttribute()
|
|
|
|
{
|
2015-01-05 11:12:34 +00:00
|
|
|
return trans('cachet.components.status.'.$this->status);
|
2014-11-27 16:05:00 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 09:51:22 +00:00
|
|
|
/**
|
|
|
|
* 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());
|
|
|
|
}
|
2015-05-20 12:57:22 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the presenter class.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPresenterClass()
|
|
|
|
{
|
2015-06-16 09:46:29 +01:00
|
|
|
return ComponentPresenter::class;
|
2015-05-20 12:57:22 -05:00
|
|
|
}
|
2014-11-27 22:08:28 +00:00
|
|
|
}
|