2018-05-09 08:05:17 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
|
|
class Dashboard extends Model
|
|
|
|
|
{
|
|
|
|
|
public $timestamps = false;
|
|
|
|
|
protected $primaryKey = 'dashboard_id';
|
|
|
|
|
protected $fillable = ['user_id', 'dashboard_name', 'access'];
|
|
|
|
|
|
2018-12-16 15:18:17 -06:00
|
|
|
// ---- Helper Functions ---
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param User $user
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function canRead($user)
|
|
|
|
|
{
|
|
|
|
|
return $this->user_id == $user->user_id || $this->access > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param User $user
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function canWrite($user)
|
|
|
|
|
{
|
|
|
|
|
return $this->user_id == $user->user_id || $this->access > 1;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 08:05:17 -05:00
|
|
|
// ---- Query scopes ----
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Builder $query
|
2018-12-16 15:18:17 -06:00
|
|
|
* @param User $user
|
2018-05-09 08:05:17 -05:00
|
|
|
* @return Builder|static
|
|
|
|
|
*/
|
|
|
|
|
public function scopeAllAvailable(Builder $query, $user)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('user_id', $user->user_id)
|
|
|
|
|
->orWhere('access', '>', 0);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-16 15:18:17 -06:00
|
|
|
// ---- Define Relationships ----
|
2018-05-09 08:05:17 -05:00
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo('App\Models\User', 'user_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function widgets()
|
|
|
|
|
{
|
2018-09-11 07:51:35 -05:00
|
|
|
return $this->hasMany('App\Models\UserWidget', 'dashboard_id');
|
2018-05-09 08:05:17 -05:00
|
|
|
}
|
|
|
|
|
}
|