| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Models; |
| 4 |
|
| 5 |
use FluentBoards\App\Services\Constant; |
| 6 |
use FluentBoards\Framework\Database\Orm\Builder; |
| 7 |
use FluentBoards\Framework\Support\Arr; |
| 8 |
|
| 9 |
class Stage extends BoardTerm |
| 10 |
{ |
| 11 |
// static $type = 'stage'; // it was working but I need a documentation to understand it better |
| 12 |
|
| 13 |
public static function boot() |
| 14 |
{ |
| 15 |
parent::boot(); |
| 16 |
static::creating(function ($model) { |
| 17 |
$model->type = $model->type ?: 'stage'; // default type is stage |
| 18 |
$model->settings = $model->settings ?: [ |
| 19 |
'default_task_status' => 'open', |
| 20 |
'is_template' => false, |
| 21 |
]; |
| 22 |
}); |
| 23 |
|
| 24 |
static::addGlobalScope('type', function (Builder $builder) { |
| 25 |
$builder->where('type', '=', 'stage'); |
| 26 |
}); |
| 27 |
} |
| 28 |
|
| 29 |
public function defaultTaskStatus() |
| 30 |
{ |
| 31 |
if(!$this->settings) { |
| 32 |
return 'open'; |
| 33 |
} |
| 34 |
|
| 35 |
return Arr::get($this->settings, 'default_task_status') == 'open' ? 'open' : 'closed'; |
| 36 |
} |
| 37 |
|
| 38 |
public function moveToNewPosition($newIndex) |
| 39 |
{ |
| 40 |
$newIndex = (int)$newIndex; |
| 41 |
if ($newIndex < 1) { |
| 42 |
$newIndex = 1; |
| 43 |
} |
| 44 |
|
| 45 |
$stageQuery = self::where('board_id', $this->board_id)->whereNull('archived_at'); |
| 46 |
|
| 47 |
|
| 48 |
if ($newIndex == 1) { |
| 49 |
$firstItem = $stageQuery->where('id', '!=', $this->id) |
| 50 |
->orderBy('position', 'asc') |
| 51 |
->first(); |
| 52 |
|
| 53 |
if ($firstItem) { |
| 54 |
if ($firstItem->position < 0.02) { |
| 55 |
self::reIndexStagesPositions($this->toArray()); |
| 56 |
return $this->moveToNewPosition($newIndex); |
| 57 |
} |
| 58 |
$index = round($firstItem->position / 2, 2); |
| 59 |
} else { |
| 60 |
$index = 1; |
| 61 |
} |
| 62 |
|
| 63 |
$this->position = $index; |
| 64 |
$this->save(); |
| 65 |
return $this; |
| 66 |
} |
| 67 |
|
| 68 |
$prevTask = $stageQuery |
| 69 |
->offset($newIndex - 2) |
| 70 |
->where('id', '!=', $this->id) |
| 71 |
->orderBy('position', 'asc') |
| 72 |
->first(); |
| 73 |
|
| 74 |
if (!$prevTask) { |
| 75 |
return $this->moveToNewPosition(1); |
| 76 |
} |
| 77 |
|
| 78 |
$nextItem = $stageQuery |
| 79 |
->offset($newIndex - 1) |
| 80 |
->where('id', '!=', $this->id) |
| 81 |
->orderBy('position', 'asc') |
| 82 |
->first(); |
| 83 |
|
| 84 |
if (!$nextItem) { |
| 85 |
$this->position = $prevTask->position + 1; |
| 86 |
$this->save(); |
| 87 |
return $this; |
| 88 |
} |
| 89 |
|
| 90 |
$newPosition = ($prevTask->position + $nextItem->position) / 2; |
| 91 |
|
| 92 |
// check if new position is already taken |
| 93 |
$exist = $stageQuery |
| 94 |
->where('position', $newPosition) |
| 95 |
->where('id', '!=', $this->id) |
| 96 |
->first(); |
| 97 |
|
| 98 |
if ($exist) { |
| 99 |
self::reIndexStagesPositions($this->toArray()); |
| 100 |
return $this->moveToNewPosition($newIndex); |
| 101 |
} |
| 102 |
|
| 103 |
$this->position = $newPosition; |
| 104 |
$this->save(); |
| 105 |
return $this; |
| 106 |
} |
| 107 |
|
| 108 |
public static function reIndexStagesPositions($stage) |
| 109 |
{ |
| 110 |
$allStages = self::where('board_id', $stage['board_id'])->where('type', 'stage')->orderBy('position', 'asc')->whereNull('archived_at')->get(); |
| 111 |
|
| 112 |
foreach ($allStages as $index => $stage) { |
| 113 |
$stage->position = $index + 1; |
| 114 |
$stage->save(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|