Bristol SU Support Package
ActionInstance.php
Go to the documentation of this file.
1 <?php
2 
4 
14 
20 class ActionInstance extends Model
21 {
22  use HasRevisions;
23 
29  protected $fillable = [
30  'name', 'description', 'event', 'action', 'module_instance_id', 'user_id', 'should_queue'
31  ];
32 
38  protected $appends = [
39  'event_fields', 'action_schema'
40  ];
41 
47  protected $casts = [
48  'should_queue' => 'boolean'
49  ];
50 
58  public function __construct(array $attributes = [])
59  {
60  parent::__construct($attributes);
61  self::creating(function($model) {
62  if($model->user_id === null && ($user = app(UserAuthentication::class)->getUser()) !== null) {
63  $model->user_id = $user->controlId();
64  }
65  });
66  }
67 
72  public function actionInstanceFields() {
73  return $this->hasMany(ActionInstanceField::class);
74  }
75 
81  public function getEventFieldsAttribute()
82  {
83  return $this->event::getFieldMetaData();
84  }
85 
91  public function getActionSchemaAttribute()
92  {
93  return (new VFGTransformer())->transformToArray($this->action::options());
94  }
95 
101  public function moduleInstance()
102  {
103  return $this->belongsTo(ModuleInstance::class);
104  }
105 
112  public function user(): \BristolSU\ControlDB\Contracts\Models\User
113  {
114  if($this->user_id === null) {
115  throw new \Exception(sprintf('Action Instance #%u is not owned by a user.', $this->id));
116  }
117  return app(User::class)->getById($this->user_id);
118  }
120  public function history()
121  {
122  return $this->hasMany(ActionHistory::class);
123  }
124 }