2014-12-22 19:15:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Model;
|
|
|
|
|
2015-01-16 14:23:05 +01:00
|
|
|
use Event\TaskEvent;
|
|
|
|
|
2014-12-22 19:15:38 +01:00
|
|
|
/**
|
|
|
|
* Task Position
|
|
|
|
*
|
|
|
|
* @package model
|
|
|
|
* @author Frederic Guillot
|
|
|
|
*/
|
|
|
|
class TaskPosition extends Base
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Move a task to another column or to another position
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param integer $project_id Project id
|
|
|
|
* @param integer $task_id Task id
|
|
|
|
* @param integer $column_id Column id
|
|
|
|
* @param integer $position Position (must be >= 1)
|
2015-01-16 14:23:05 +01:00
|
|
|
* @param integer $swimlane_id Swimlane id
|
2014-12-22 19:15:38 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-01-16 14:23:05 +01:00
|
|
|
public function movePosition($project_id, $task_id, $column_id, $position, $swimlane_id = 0)
|
2014-12-22 19:15:38 +01:00
|
|
|
{
|
|
|
|
$original_task = $this->taskFinder->getById($task_id);
|
|
|
|
|
2015-01-16 14:23:05 +01:00
|
|
|
$result = $this->calculateAndSave($project_id, $task_id, $column_id, $position, $swimlane_id);
|
2014-12-22 19:15:38 +01:00
|
|
|
|
2015-01-16 14:23:05 +01:00
|
|
|
if ($result) {
|
2014-12-22 19:15:38 +01:00
|
|
|
|
2015-01-16 14:23:05 +01:00
|
|
|
if ($original_task['swimlane_id'] != $swimlane_id) {
|
|
|
|
$this->calculateAndSave($project_id, 0, $column_id, 1, $original_task['swimlane_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fireEvents($original_task, $column_id, $position, $swimlane_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2014-12-22 19:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the new position of all tasks
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param integer $project_id Project id
|
|
|
|
* @param integer $task_id Task id
|
|
|
|
* @param integer $column_id Column id
|
|
|
|
* @param integer $position Position (must be >= 1)
|
2015-01-16 14:23:05 +01:00
|
|
|
* @param integer $swimlane_id Swimlane id
|
2014-12-22 19:15:38 +01:00
|
|
|
* @return array|boolean
|
|
|
|
*/
|
2015-01-16 14:23:05 +01:00
|
|
|
public function calculatePositions($project_id, $task_id, $column_id, $position, $swimlane_id = 0)
|
2014-12-22 19:15:38 +01:00
|
|
|
{
|
|
|
|
// The position can't be lower than 1
|
|
|
|
if ($position < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$board = $this->db->table(Board::TABLE)->eq('project_id', $project_id)->asc('position')->findAllByColumn('id');
|
|
|
|
$columns = array();
|
|
|
|
|
|
|
|
// For each column fetch all tasks ordered by position
|
|
|
|
foreach ($board as $board_column_id) {
|
|
|
|
|
|
|
|
$columns[$board_column_id] = $this->db->table(Task::TABLE)
|
|
|
|
->eq('is_active', 1)
|
2015-01-16 14:23:05 +01:00
|
|
|
->eq('swimlane_id', $swimlane_id)
|
2014-12-22 19:15:38 +01:00
|
|
|
->eq('project_id', $project_id)
|
|
|
|
->eq('column_id', $board_column_id)
|
|
|
|
->neq('id', $task_id)
|
|
|
|
->asc('position')
|
2015-01-16 14:23:05 +01:00
|
|
|
->asc('id') // Fix Postgresql unit test
|
2014-12-22 19:15:38 +01:00
|
|
|
->findAllByColumn('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
// The column must exists
|
|
|
|
if (! isset($columns[$column_id])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We put our task to the new position
|
2015-01-16 14:23:05 +01:00
|
|
|
if ($task_id) {
|
|
|
|
array_splice($columns[$column_id], $position - 1, 0, $task_id);
|
|
|
|
}
|
2014-12-22 19:15:38 +01:00
|
|
|
|
|
|
|
return $columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save task positions
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param array $columns Sorted tasks
|
2015-01-16 14:23:05 +01:00
|
|
|
* @param integer $swimlane_id Swimlane id
|
2014-12-22 19:15:38 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-01-16 14:23:05 +01:00
|
|
|
private function savePositions(array $columns, $swimlane_id)
|
2014-12-22 19:15:38 +01:00
|
|
|
{
|
2015-01-16 14:23:05 +01:00
|
|
|
return $this->db->transaction(function ($db) use ($columns, $swimlane_id) {
|
2014-12-22 19:15:38 +01:00
|
|
|
|
|
|
|
foreach ($columns as $column_id => $column) {
|
|
|
|
|
|
|
|
$position = 1;
|
|
|
|
|
|
|
|
foreach ($column as $task_id) {
|
|
|
|
|
|
|
|
$result = $db->table(Task::TABLE)->eq('id', $task_id)->update(array(
|
|
|
|
'position' => $position,
|
2015-01-16 14:23:05 +01:00
|
|
|
'column_id' => $column_id,
|
|
|
|
'swimlane_id' => $swimlane_id,
|
2014-12-22 19:15:38 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
if (! $result) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$position++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fire events
|
|
|
|
*
|
2015-01-16 14:23:05 +01:00
|
|
|
* @access private
|
2014-12-22 19:15:38 +01:00
|
|
|
* @param array $task
|
|
|
|
* @param integer $new_column_id
|
|
|
|
* @param integer $new_position
|
2015-01-16 14:23:05 +01:00
|
|
|
* @param integer $new_swimlane_id
|
2014-12-22 19:15:38 +01:00
|
|
|
*/
|
2015-01-16 14:23:05 +01:00
|
|
|
private function fireEvents(array $task, $new_column_id, $new_position, $new_swimlane_id)
|
2014-12-22 19:15:38 +01:00
|
|
|
{
|
|
|
|
$event_data = array(
|
|
|
|
'task_id' => $task['id'],
|
|
|
|
'project_id' => $task['project_id'],
|
|
|
|
'position' => $new_position,
|
|
|
|
'column_id' => $new_column_id,
|
2015-01-16 14:23:05 +01:00
|
|
|
'swimlane_id' => $new_swimlane_id,
|
2014-12-22 19:15:38 +01:00
|
|
|
);
|
|
|
|
|
2015-01-16 14:23:05 +01:00
|
|
|
if ($task['swimlane_id'] != $new_swimlane_id) {
|
|
|
|
$this->container['dispatcher']->dispatch(Task::EVENT_MOVE_SWIMLANE, new TaskEvent($event_data));
|
|
|
|
}
|
|
|
|
else if ($task['column_id'] != $new_column_id) {
|
|
|
|
$this->container['dispatcher']->dispatch(Task::EVENT_MOVE_COLUMN, new TaskEvent($event_data));
|
2014-12-22 19:15:38 +01:00
|
|
|
}
|
|
|
|
else if ($task['position'] != $new_position) {
|
2015-01-16 14:23:05 +01:00
|
|
|
$this->container['dispatcher']->dispatch(Task::EVENT_MOVE_POSITION, new TaskEvent($event_data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the new position of all tasks
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @param integer $project_id Project id
|
|
|
|
* @param integer $task_id Task id
|
|
|
|
* @param integer $column_id Column id
|
|
|
|
* @param integer $position Position (must be >= 1)
|
|
|
|
* @param integer $swimlane_id Swimlane id
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function calculateAndSave($project_id, $task_id, $column_id, $position, $swimlane_id)
|
|
|
|
{
|
|
|
|
$positions = $this->calculatePositions($project_id, $task_id, $column_id, $position, $swimlane_id);
|
|
|
|
|
|
|
|
if ($positions === false || ! $this->savePositions($positions, $swimlane_id)) {
|
|
|
|
return false;
|
2014-12-22 19:15:38 +01:00
|
|
|
}
|
2015-01-16 14:23:05 +01:00
|
|
|
|
|
|
|
return true;
|
2014-12-22 19:15:38 +01:00
|
|
|
}
|
|
|
|
}
|