You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.7 KiB
89 lines
2.7 KiB
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Model;
|
|
|
|
class Workflow extends Model
|
|
{
|
|
protected $fillable = ['business_id', 'name', 'desc', 'statuses'];
|
|
|
|
protected $reportable = [
|
|
'name'
|
|
];
|
|
|
|
protected $fillable_relations = ['statuses'];
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
'name' => 'required|string|min:3|max:225',
|
|
'desc' => 'nullable|string|min:3|max:225',
|
|
'statuses' => 'required|array|min:2',
|
|
'statuses.*' => 'required|array|min:2',
|
|
'statuses.*.id' => 'nullable|numeric',
|
|
'statuses.*.name' => 'required|string|min:3',
|
|
'statuses.*.state' => 'required|numeric|between:0,3',
|
|
'statuses.*.order' => 'nullable|numeric',
|
|
];
|
|
}
|
|
|
|
public function getValueOf(?string $key)
|
|
{
|
|
$values = [
|
|
'business_id' => $this->business_id,
|
|
'project_id' => null,
|
|
'sprint_id' => null,
|
|
'workflow_id' => $this->id,
|
|
'status_id' => null,
|
|
'system_id' => null,
|
|
'user_id' => null,
|
|
'task_id' => null,
|
|
'subject_id' => $this->id,
|
|
];
|
|
|
|
if ($key && isset($values, $key)) {
|
|
return $values[$key];
|
|
}
|
|
|
|
return $values;
|
|
}
|
|
|
|
public function updateRelations()
|
|
{
|
|
$old_statuses_name = isset(\request('_business_info')['workflows'][$this->id]['statuses']) ?
|
|
array_keys(collect(\request('_business_info')['workflows'][$this->id]['statuses'])->toArray()) :
|
|
[];
|
|
$new_statuses_name = array_keys(collect($this->filled_relations['statuses'])->keyBy('id')->toArray());
|
|
$removed_statuses_name = array_diff(array_merge($old_statuses_name, $new_statuses_name), $new_statuses_name);
|
|
|
|
foreach ($removed_statuses_name as $status_name) {
|
|
//delete all statuses that removed name's from request->statuses
|
|
$this->statuses()->where('id', $status_name)->first()->delete();
|
|
}
|
|
|
|
foreach (request('statuses') as $status) {
|
|
//sync another statuses
|
|
$this->statuses()
|
|
->updateOrCreate(
|
|
['id' => $status['id'] ?? null, 'business_id' => $this->business_id, 'workflow_id' => $this->id],
|
|
['name' => $status['name'], 'state' => $status['state'], 'order' => $status['order']]
|
|
);
|
|
}
|
|
}
|
|
|
|
public function business()
|
|
{
|
|
return $this->belongsTo(Business::class, 'business_id');
|
|
}
|
|
|
|
public function statuses()
|
|
{
|
|
return $this->hasMany(Status::class, 'workflow_id', 'id');
|
|
}
|
|
|
|
public function tasks()
|
|
{
|
|
return $this->hasMany(Task::class, 'workflow_id', 'id');
|
|
}
|
|
}
|