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;
|
2015-03-12 07:26:15 +00:00
|
|
|
use Carbon\Carbon;
|
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
|
|
|
|
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
|
|
|
|
2015-08-22 15:57:53 +01:00
|
|
|
/**
|
|
|
|
* The attributes that should be casted to native types.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
2015-10-01 21:05:14 +01:00
|
|
|
'visible' => 'int',
|
|
|
|
'scheduled_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',
|
2015-05-19 17:40:04 +01:00
|
|
|
'message',
|
|
|
|
'scheduled_at',
|
|
|
|
'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 = [
|
2015-10-20 20:33:23 +01:00
|
|
|
'component_id' => 'int',
|
2015-08-22 15:57:53 +01:00
|
|
|
'name' => 'required',
|
2015-10-20 20:33:23 +01:00
|
|
|
'status' => 'required|int',
|
2015-10-20 20:34:59 +01:00
|
|
|
'visible' => 'required|bool',
|
2015-08-22 15:57:53 +01:00
|
|
|
'message' => 'required',
|
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-02-23 13:34:22 -06:00
|
|
|
/**
|
|
|
|
* The sortable fields.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $sortable = [
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'status',
|
|
|
|
'visible',
|
|
|
|
'message',
|
|
|
|
];
|
|
|
|
|
2015-05-20 08:41:02 +01:00
|
|
|
/**
|
|
|
|
* Finds all visible incidents.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeVisible($query)
|
|
|
|
{
|
|
|
|
return $query->where('visible', 1);
|
|
|
|
}
|
|
|
|
|
2015-02-28 20:18:30 +00:00
|
|
|
/**
|
|
|
|
* Finds all scheduled incidents (maintenance).
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeScheduled($query)
|
|
|
|
{
|
2015-03-11 15:58:48 -06:00
|
|
|
return $query->where('status', 0)->where('scheduled_at', '>=', Carbon::now());
|
2015-02-28 20:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds all non-scheduled incidents.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeNotScheduled($query)
|
|
|
|
{
|
|
|
|
return $query->where(function ($query) {
|
2015-03-11 15:58:48 -06:00
|
|
|
return $query->whereNull('scheduled_at')->orWhere('scheduled_at', '<=', Carbon::now());
|
2015-02-28 20:18:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-11-27 16:05:00 +00:00
|
|
|
/**
|
|
|
|
* An incident belongs to a component.
|
2014-12-30 12:49:39 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2014-11-27 16:05:00 +00:00
|
|
|
*/
|
2014-12-20 21:20:17 +00:00
|
|
|
public function component()
|
|
|
|
{
|
2015-06-16 09:46:29 +01:00
|
|
|
return $this->belongsTo(Component::class, 'component_id', 'id');
|
2014-11-27 16:05:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-28 20:18:30 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the "incident" is scheduled or not.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getIsScheduledAttribute()
|
|
|
|
{
|
2015-09-04 08:18:10 +01:00
|
|
|
return $this->getOriginal('scheduled_at') !== null;
|
2015-02-28 20:18:30 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|