Bristol SU Support Package
Logic.php
Go to the documentation of this file.
1 <?php
2 
4 
12 
16 class Logic extends Model
17 {
18  use HasRevisions;
19 
25  protected $fillable = [
26  'name',
27  'description',
28  'user_id'
29  ];
30 
41  protected $appends = [
42  'lowest_resource'
43  ];
44 
45 
53  public function __construct(array $attributes = [])
54  {
55  parent::__construct($attributes);
56  self::creating(function($model) {
57  if($model->user_id === null && ($user = app(UserAuthentication::class)->getUser()) !== null) {
58  $model->user_id = $user->controlId();
59  }
60  });
61  }
62 
70  public function filters()
71  {
72  return $this->hasMany(FilterInstance::class);
73  }
74 
75 
81  public function allTrueFilters()
82  {
83  return $this->hasMany(FilterInstance::class)->where('filter_instances.logic_type', '=', 'all_true');
84  }
85 
91  public function allFalseFilters()
92  {
93  return $this->hasMany(FilterInstance::class)->where('filter_instances.logic_type', '=', 'all_false');
94  }
95 
101  public function anyTrueFilters()
102  {
103  return $this->hasMany(FilterInstance::class)->where('filter_instances.logic_type', '=', 'any_true');
104  }
105 
111  public function anyFalseFilters()
112  {
113  return $this->hasMany(FilterInstance::class)->where('filter_instances.logic_type', '=', 'any_false');
114  }
115 
128  public function getLowestResourceAttribute()
129  {
131  $filters = $this->filters;
132  if ($filters->contains('for', 'role')) {
133  return 'role';
134  } else if ($filters->contains('for', 'group')) {
135  return 'group';
136  } else if ($filters->contains('for', 'user')) {
137  return 'user';
138  }
139  return 'none';
140  }
141 
148  public function user(): \BristolSU\ControlDB\Contracts\Models\User
149  {
150  if($this->user_id === null) {
151  throw new \Exception(sprintf('Logic #%u is not owned by a user.', $this->id));
152  }
153  return app(User::class)->getById($this->user_id);
154  }
155 }
__construct(array $attributes=[])
Definition: Logic.php:52