Add subtasks drag and drop
This commit is contained in:
@@ -23,7 +23,6 @@ class Subtask extends Base
|
||||
'project' => $this->getProject(),
|
||||
'subtasks' => $this->subtask->getAll($task['id']),
|
||||
'editable' => true,
|
||||
'redirect' => 'subtask',
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -169,15 +168,15 @@ class Subtask extends Base
|
||||
*/
|
||||
public function movePosition()
|
||||
{
|
||||
$this->checkCSRFParam();
|
||||
$project_id = $this->request->getIntegerParam('project_id');
|
||||
$task_id = $this->request->getIntegerParam('task_id');
|
||||
$subtask_id = $this->request->getIntegerParam('subtask_id');
|
||||
$direction = $this->request->getStringParam('direction');
|
||||
$method = $direction === 'up' ? 'moveUp' : 'moveDown';
|
||||
$redirect = $this->request->getStringParam('redirect', 'task');
|
||||
$values = $this->request->getJson();
|
||||
|
||||
$this->subtask->$method($task_id, $subtask_id);
|
||||
$this->response->redirect($this->helper->url->to($redirect, 'show', array('project_id' => $project_id, 'task_id' => $task_id), 'subtasks'));
|
||||
if (! empty($values) && $this->helper->user->hasProjectAccess('Subtask', 'movePosition', $project_id)) {
|
||||
$result = $this->subtask->changePosition($task_id, $values['subtask_id'], $values['position']);
|
||||
$this->response->json(array('result' => $result));
|
||||
}
|
||||
|
||||
$this->forbidden();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -284,68 +284,36 @@ class Subtask extends Base
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the new positions for a set of subtasks
|
||||
* Save subtask position
|
||||
*
|
||||
* @access public
|
||||
* @param array $subtasks Hashmap of column_id/column_position
|
||||
* @param integer $task_id
|
||||
* @param integer $subtask_id
|
||||
* @param integer $position
|
||||
* @return boolean
|
||||
*/
|
||||
public function savePositions(array $subtasks)
|
||||
public function changePosition($task_id, $subtask_id, $position)
|
||||
{
|
||||
return $this->db->transaction(function (Database $db) use ($subtasks) {
|
||||
if ($position < 1 || $position > $this->db->table(self::TABLE)->eq('task_id', $task_id)->count()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($subtasks as $subtask_id => $position) {
|
||||
if (! $db->table(Subtask::TABLE)->eq('id', $subtask_id)->update(array('position' => $position))) {
|
||||
return false;
|
||||
}
|
||||
$subtask_ids = $this->db->table(self::TABLE)->eq('task_id', $task_id)->neq('id', $subtask_id)->asc('position')->findAllByColumn('id');
|
||||
$offset = 1;
|
||||
$results = array();
|
||||
|
||||
foreach ($subtask_ids as $current_subtask_id) {
|
||||
if ($offset == $position) {
|
||||
$offset++;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Move a subtask down, increment the position value
|
||||
*
|
||||
* @access public
|
||||
* @param integer $task_id
|
||||
* @param integer $subtask_id
|
||||
* @return boolean
|
||||
*/
|
||||
public function moveDown($task_id, $subtask_id)
|
||||
{
|
||||
$subtasks = $this->getNormalizedPositions($task_id);
|
||||
$positions = array_flip($subtasks);
|
||||
|
||||
if (isset($subtasks[$subtask_id]) && $subtasks[$subtask_id] < count($subtasks)) {
|
||||
$position = ++$subtasks[$subtask_id];
|
||||
$subtasks[$positions[$position]]--;
|
||||
|
||||
return $this->savePositions($subtasks);
|
||||
$results[] = $this->db->table(self::TABLE)->eq('id', $current_subtask_id)->update(array('position' => $offset));
|
||||
$offset++;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
$results[] = $this->db->table(self::TABLE)->eq('id', $subtask_id)->update(array('position' => $position));
|
||||
|
||||
/**
|
||||
* Move a subtask up, decrement the position value
|
||||
*
|
||||
* @access public
|
||||
* @param integer $task_id
|
||||
* @param integer $subtask_id
|
||||
* @return boolean
|
||||
*/
|
||||
public function moveUp($task_id, $subtask_id)
|
||||
{
|
||||
$subtasks = $this->getNormalizedPositions($task_id);
|
||||
$positions = array_flip($subtasks);
|
||||
|
||||
if (isset($subtasks[$subtask_id]) && $subtasks[$subtask_id] > 1) {
|
||||
$position = --$subtasks[$subtask_id];
|
||||
$subtasks[$positions[$position]]++;
|
||||
|
||||
return $this->savePositions($subtasks);
|
||||
}
|
||||
|
||||
return false;
|
||||
return !in_array(false, $results, true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,16 +1,6 @@
|
||||
<div class="dropdown">
|
||||
<a href="#" class="dropdown-menu dropdown-menu-link-icon"><i class="fa fa-cog fa-fw"></i><i class="fa fa-caret-down"></i></a>
|
||||
<ul>
|
||||
<?php if ($subtask['position'] != $first_position): ?>
|
||||
<li>
|
||||
<?= $this->url->link(t('Move Up'), 'subtask', 'movePosition', array('project_id' => $task['project_id'], 'task_id' => $subtask['task_id'], 'subtask_id' => $subtask['id'], 'direction' => 'up', 'redirect' => $redirect), true) ?>
|
||||
</li>
|
||||
<?php endif ?>
|
||||
<?php if ($subtask['position'] != $last_position): ?>
|
||||
<li>
|
||||
<?= $this->url->link(t('Move Down'), 'subtask', 'movePosition', array('project_id' => $task['project_id'], 'task_id' => $subtask['task_id'], 'subtask_id' => $subtask['id'], 'direction' => 'down', 'redirect' => $redirect), true) ?>
|
||||
</li>
|
||||
<?php endif ?>
|
||||
<li>
|
||||
<?= $this->url->link(t('Edit'), 'subtask', 'edit', array('task_id' => $task['id'], 'project_id' => $task['project_id'], 'subtask_id' => $subtask['id']), false, 'popover') ?>
|
||||
</li>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
<div id="subtasks">
|
||||
|
||||
<?= $this->render('subtask/table', array('subtasks' => $subtasks, 'task' => $task, 'editable' => $editable, 'redirect' => $redirect)) ?>
|
||||
<?= $this->render('subtask/table', array('subtasks' => $subtasks, 'task' => $task, 'editable' => $editable)) ?>
|
||||
|
||||
<?php if ($editable && $this->user->hasProjectAccess('subtask', 'save', $task['project_id'])): ?>
|
||||
<form method="post" action="<?= $this->url->href('subtask', 'save', array('task_id' => $task['id'], 'project_id' => $task['project_id'])) ?>" autocomplete="off">
|
||||
|
||||
@@ -3,7 +3,11 @@
|
||||
<?php $first_position = $subtasks[0]['position']; ?>
|
||||
<?php $last_position = $subtasks[count($subtasks) - 1]['position']; ?>
|
||||
|
||||
<table class="subtasks-table">
|
||||
<table
|
||||
class="subtasks-table table-stripped"
|
||||
data-save-position-url="<?= $this->url->href('Subtask', 'movePosition', array('project_id' => $task['project_id'], 'task_id' => $task['id'])) ?>"
|
||||
>
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="column-40"><?= t('Title') ?></th>
|
||||
<th><?= t('Assignee') ?></th>
|
||||
@@ -12,10 +16,13 @@
|
||||
<th class="column-5"></th>
|
||||
<?php endif ?>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($subtasks as $subtask): ?>
|
||||
<tr>
|
||||
<tr data-subtask-id="<?= $subtask['id'] ?>">
|
||||
<td>
|
||||
<?php if ($editable): ?>
|
||||
<i class="fa fa-arrows-alt draggable-row-handle" title="<?= t('Move subtask position') ?>"></i>
|
||||
<?= $this->subtask->toggleStatus($subtask, $task['project_id'], true) ?>
|
||||
<?php else: ?>
|
||||
<?= $this->subtask->getTitle($subtask) ?>
|
||||
@@ -58,12 +65,12 @@
|
||||
'subtask' => $subtask,
|
||||
'first_position' => $first_position,
|
||||
'last_position' => $last_position,
|
||||
'redirect' => $redirect,
|
||||
)) ?>
|
||||
</td>
|
||||
<?php endif ?>
|
||||
</tr>
|
||||
<?php endforeach ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php else: ?>
|
||||
<p class="alert"><?= t('There is no subtask at the moment.') ?></p>
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
'project' => $project,
|
||||
'users_list' => isset($users_list) ? $users_list : array(),
|
||||
'editable' => true,
|
||||
'redirect' => 'task',
|
||||
)) ?>
|
||||
|
||||
<?= $this->render('tasklink/show', array(
|
||||
|
||||
Reference in New Issue
Block a user