2015-01-04 20:56:10 +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-04 20:56:10 +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;
|
2016-01-03 11:48:53 +00:00
|
|
|
use CachetHQ\Cachet\Presenters\ComponentGroupPresenter;
|
2016-10-02 15:57:32 +03:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2015-01-04 20:56:10 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2016-10-02 15:57:32 +03:00
|
|
|
use Illuminate\Support\Collection;
|
2016-01-03 11:48:53 +00:00
|
|
|
use McCool\LaravelAutoPresenter\HasPresenter;
|
2015-01-04 20:56:10 +00:00
|
|
|
|
2017-02-19 10:55:42 +00:00
|
|
|
/**
|
|
|
|
* This is the component group model class.
|
|
|
|
*
|
|
|
|
* @author James Brooks <james@alt-three.com>
|
|
|
|
*/
|
2016-01-03 11:48:53 +00:00
|
|
|
class ComponentGroup extends Model implements HasPresenter
|
2015-01-04 20:56:10 +00:00
|
|
|
{
|
2016-03-01 15:05:41 +00:00
|
|
|
use SearchableTrait, SortableTrait, ValidatingTrait;
|
2015-01-04 20:56:10 +00:00
|
|
|
|
2016-10-02 15:57:32 +03:00
|
|
|
/**
|
|
|
|
* Viewable only authenticated users.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const VISIBLE_AUTHENTICATED = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Viewable by public.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const VISIBLE_GUEST = 1;
|
|
|
|
|
2016-04-16 12:19:16 +01:00
|
|
|
/**
|
|
|
|
* The model's attributes.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $attributes = [
|
|
|
|
'order' => 0,
|
|
|
|
'collapsed' => 0,
|
2016-10-02 15:57:32 +03:00
|
|
|
'visible' => 0,
|
2016-04-16 12:19:16 +01:00
|
|
|
];
|
|
|
|
|
2015-01-04 20:56:10 +00:00
|
|
|
/**
|
2015-08-08 15:36:48 +01:00
|
|
|
* The attributes that should be casted to native types.
|
2015-01-04 20:56:10 +00:00
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2015-08-08 15:36:48 +01:00
|
|
|
protected $casts = [
|
2016-01-29 16:02:14 +00:00
|
|
|
'name' => 'string',
|
|
|
|
'order' => 'int',
|
2016-03-10 14:59:13 +00:00
|
|
|
'collapsed' => 'int',
|
2016-10-02 15:57:32 +03:00
|
|
|
'visible' => 'int',
|
2015-01-04 20:56:10 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The fillable properties.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2016-10-02 15:57:32 +03:00
|
|
|
protected $fillable = ['name', 'order', 'collapsed', 'visible'];
|
2015-01-04 20:56:10 +00:00
|
|
|
|
2015-08-08 15:36:48 +01:00
|
|
|
/**
|
|
|
|
* The validation rules.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
public $rules = [
|
2016-01-29 16:02:14 +00:00
|
|
|
'name' => 'required|string',
|
2016-10-19 12:29:16 +01:00
|
|
|
'order' => 'required|int',
|
2017-02-27 19:47:44 +00:00
|
|
|
'collapsed' => 'required|int|between:0,4',
|
|
|
|
'visible' => 'required|bool',
|
2015-08-08 15:36:48 +01:00
|
|
|
];
|
|
|
|
|
2016-03-01 15:05:41 +00:00
|
|
|
/**
|
|
|
|
* The searchable fields.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $searchable = [
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'order',
|
|
|
|
'collapsed',
|
2016-10-02 15:57:32 +03:00
|
|
|
'visible',
|
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',
|
|
|
|
'order',
|
|
|
|
'collapsed',
|
2016-10-02 15:57:32 +03:00
|
|
|
'visible',
|
2016-02-23 13:34:22 -06:00
|
|
|
];
|
|
|
|
|
2016-01-03 11:48:53 +00:00
|
|
|
/**
|
|
|
|
* The relations to eager load on every query.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2016-01-03 12:26:27 +00:00
|
|
|
protected $with = ['enabled_components', 'enabled_components_lowest'];
|
2016-01-03 11:48:53 +00:00
|
|
|
|
2015-01-04 20:56:10 +00:00
|
|
|
/**
|
2016-08-10 10:46:45 +01:00
|
|
|
* Get the components relation.
|
2015-01-04 20:56:10 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function components()
|
|
|
|
{
|
2017-02-19 11:00:40 +00:00
|
|
|
return $this->hasMany(Component::class, 'group_id', 'id');
|
2015-01-04 20:56:10 +00:00
|
|
|
}
|
2016-01-03 11:48:53 +00:00
|
|
|
|
2016-03-10 14:59:13 +00:00
|
|
|
/**
|
|
|
|
* Get the incidents relation.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
|
|
|
*/
|
|
|
|
public function incidents()
|
|
|
|
{
|
|
|
|
return $this->hasManyThrough(Incident::class, Component::class, 'id', 'component_id');
|
|
|
|
}
|
|
|
|
|
2016-01-03 11:48:53 +00:00
|
|
|
/**
|
|
|
|
* Return all of the enabled components.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function enabled_components()
|
|
|
|
{
|
2017-02-19 11:00:40 +00:00
|
|
|
return $this->components()->enabled()->orderBy('order');
|
2016-01-03 11:48:53 +00:00
|
|
|
}
|
|
|
|
|
2016-01-03 12:26:27 +00:00
|
|
|
/**
|
|
|
|
* Return all of the enabled components ordered by status.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function enabled_components_lowest()
|
|
|
|
{
|
|
|
|
return $this->components()->enabled()->orderBy('status', 'desc');
|
|
|
|
}
|
|
|
|
|
2016-01-03 11:48:53 +00:00
|
|
|
/**
|
|
|
|
* Get the presenter class.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPresenterClass()
|
|
|
|
{
|
|
|
|
return ComponentGroupPresenter::class;
|
|
|
|
}
|
2016-10-02 15:57:32 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds all component groups which are visible to public.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeVisible(Builder $query)
|
|
|
|
{
|
2016-10-19 12:29:16 +01:00
|
|
|
return $query->where('visible', '=', self::VISIBLE_GUEST);
|
2016-10-02 15:57:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds all used component groups.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
* @param \Illuminate\Support\Collection $usedComponentGroups
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeUsed(Builder $query, Collection $usedComponentGroups)
|
|
|
|
{
|
|
|
|
return $query->whereIn('id', $usedComponentGroups)
|
|
|
|
->orderBy('order');
|
|
|
|
}
|
2015-01-04 20:56:10 +00:00
|
|
|
}
|