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;
|
2016-03-01 15:05:41 +00:00
|
|
|
use CachetHQ\Cachet\Models\Traits\SearchableTrait;
|
2016-02-23 13:34:22 -06:00
|
|
|
use CachetHQ\Cachet\Models\Traits\SortableTrait;
|
2015-06-16 09:46:29 +01:00
|
|
|
use CachetHQ\Cachet\Presenters\IncidentPresenter;
|
2016-07-13 14:19:24 +01: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;
|
2014-11-27 16:05:00 +00:00
|
|
|
|
2016-10-29 17:25:52 +01:00
|
|
|
/**
|
|
|
|
* This is the incident model.
|
|
|
|
*
|
|
|
|
* @author James Brooks <james@alt-three.com>
|
|
|
|
* @author Joseph Cohen <joseph@alt-three.com>
|
|
|
|
* @author Graham Campbell <graham@alt-three.com>
|
|
|
|
*/
|
2015-03-20 18:30:45 -06:00
|
|
|
class Incident extends Model implements HasPresenter
|
2014-12-20 21:20:17 +00:00
|
|
|
{
|
2016-03-01 15:05:41 +00:00
|
|
|
use SearchableTrait, SoftDeletes, SortableTrait, ValidatingTrait;
|
2014-11-27 16:05:00 +00:00
|
|
|
|
2016-10-06 17:21:18 +01:00
|
|
|
/**
|
|
|
|
* Status for incident being investigated.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const INVESTIGATING = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Status for incident having been identified.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const IDENTIFIED = 2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Status for incident being watched.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const WATCHED = 3;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Status for incident now being fixed.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const FIXED = 4;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The accessors to append to the model's array form.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $appends = [
|
|
|
|
'is_resolved',
|
|
|
|
];
|
|
|
|
|
2015-08-22 15:57:53 +01:00
|
|
|
/**
|
|
|
|
* The attributes that should be casted to native types.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
2016-10-30 20:54:12 +00:00
|
|
|
'visible' => 'int',
|
|
|
|
'stickied' => 'bool',
|
|
|
|
'occurred_at' => 'date',
|
|
|
|
'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-05-19 17:40:04 +01:00
|
|
|
protected $fillable = [
|
|
|
|
'component_id',
|
|
|
|
'name',
|
|
|
|
'status',
|
2015-05-20 08:41:02 +01:00
|
|
|
'visible',
|
2016-08-17 01:12:21 +02:00
|
|
|
'stickied',
|
2015-05-19 17:40:04 +01:00
|
|
|
'message',
|
2016-10-29 17:25:52 +01:00
|
|
|
'occurred_at',
|
2015-05-19 17:40:04 +01:00
|
|
|
'created_at',
|
|
|
|
'updated_at',
|
|
|
|
];
|
2014-11-27 16:05:00 +00:00
|
|
|
|
2015-01-01 18:57:33 +00:00
|
|
|
/**
|
2015-08-22 15:57:53 +01:00
|
|
|
* The validation rules.
|
2015-06-18 16:13:02 +01:00
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2015-08-22 15:57:53 +01:00
|
|
|
public $rules = [
|
2016-10-19 12:29:16 +01:00
|
|
|
'component_id' => 'nullable|int',
|
|
|
|
'name' => 'required|string',
|
2015-10-20 20:33:23 +01:00
|
|
|
'status' => 'required|int',
|
2015-10-20 20:34:59 +01:00
|
|
|
'visible' => 'required|bool',
|
2016-10-19 12:29:16 +01:00
|
|
|
'stickied' => 'required|bool',
|
|
|
|
'message' => 'required|string',
|
2015-06-18 16:13:02 +01:00
|
|
|
];
|
|
|
|
|
2016-03-01 15:05:41 +00:00
|
|
|
/**
|
|
|
|
* The searchable fields.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $searchable = [
|
|
|
|
'id',
|
2016-06-02 08:46:59 +01:00
|
|
|
'component_id',
|
2016-03-01 15:05:41 +00:00
|
|
|
'name',
|
|
|
|
'status',
|
|
|
|
'visible',
|
2016-08-17 01:12:21 +02:00
|
|
|
'stickied',
|
2016-03-01 15:05:41 +00:00
|
|
|
];
|
|
|
|
|
2016-02-23 13:34:22 -06:00
|
|
|
/**
|
|
|
|
* The sortable fields.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $sortable = [
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'status',
|
|
|
|
'visible',
|
2016-08-17 01:12:21 +02:00
|
|
|
'stickied',
|
2016-02-23 13:34:22 -06:00
|
|
|
'message',
|
2016-10-29 17:25:52 +01:00
|
|
|
'occurred_at',
|
2016-02-23 13:34:22 -06:00
|
|
|
];
|
|
|
|
|
2016-10-06 17:21:18 +01:00
|
|
|
/**
|
|
|
|
* The relations to eager load on every query.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $with = ['updates'];
|
|
|
|
|
2016-08-10 10:46:45 +01:00
|
|
|
/**
|
|
|
|
* Get the component relation.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function component()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Component::class, 'component_id', 'id');
|
|
|
|
}
|
|
|
|
|
2016-10-06 17:21:18 +01:00
|
|
|
/**
|
|
|
|
* Get the updates relation.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function updates()
|
|
|
|
{
|
|
|
|
return $this->hasMany(IncidentUpdate::class)->orderBy('created_at', 'desc');
|
|
|
|
}
|
|
|
|
|
2015-05-20 08:41:02 +01:00
|
|
|
/**
|
|
|
|
* Finds all visible incidents.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
2016-07-13 14:19:24 +01:00
|
|
|
public function scopeVisible(Builder $query)
|
2015-05-20 08:41:02 +01:00
|
|
|
{
|
2016-10-19 12:29:16 +01:00
|
|
|
return $query->where('visible', '=', 1);
|
2015-05-20 08:41:02 +01:00
|
|
|
}
|
|
|
|
|
2016-08-17 01:12:21 +02:00
|
|
|
/**
|
|
|
|
* Finds all stickied incidents.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeStickied(Builder $query)
|
|
|
|
{
|
2016-10-19 12:29:16 +01:00
|
|
|
return $query->where('stickied', '=', true);
|
2016-08-17 01:12:21 +02:00
|
|
|
}
|
|
|
|
|
2016-10-06 17:21:18 +01:00
|
|
|
/**
|
|
|
|
* Is the incident resolved?
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getIsResolvedAttribute()
|
|
|
|
{
|
|
|
|
if ($updates = $this->updates->first()) {
|
|
|
|
return $updates->status === self::FIXED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->status === self::FIXED;
|
|
|
|
}
|
|
|
|
|
2014-11-27 16:05:00 +00:00
|
|
|
/**
|
2015-03-20 18:30:45 -06:00
|
|
|
* Get the presenter class.
|
2014-12-30 12:49:39 +00:00
|
|
|
*
|
2015-03-20 18:30:45 -06:00
|
|
|
* @return string
|
2014-11-27 16:05:00 +00:00
|
|
|
*/
|
2015-03-20 18:30:45 -06:00
|
|
|
public function getPresenterClass()
|
2014-12-20 21:20:17 +00:00
|
|
|
{
|
2015-06-16 09:46:29 +01:00
|
|
|
return IncidentPresenter::class;
|
2014-11-27 16:05:00 +00:00
|
|
|
}
|
2014-11-27 22:08:28 +00:00
|
|
|
}
|