Improve task duplication

This commit is contained in:
Frédéric Guillot 2014-09-01 20:40:02 -08:00
parent e6d0658a0e
commit 457e181ffb
17 changed files with 137 additions and 79 deletions

View File

@ -332,7 +332,7 @@ class Task extends Base
}
/**
* Duplicate a task (fill the form for a new task)
* Duplicate a task
*
* @access public
*/
@ -340,25 +340,24 @@ class Task extends Base
{
$task = $this->getTask();
if (! empty($task['date_due'])) {
$task['date_due'] = date(t('m/d/Y'), $task['date_due']);
}
else {
$task['date_due'] = '';
if ($this->request->getStringParam('confirmation') === 'yes') {
$this->checkCSRFParam();
$task_id = $this->task->duplicateSameProject($task);
if ($task_id) {
$this->session->flash(t('Task created successfully.'));
$this->response->redirect('?controller=task&action=show&task_id='.$task_id);
} else {
$this->session->flashError(t('Unable to create this task.'));
$this->response->redirect('?controller=task&action=duplicate&task_id='.$task['id']);
}
}
$task['score'] = $task['score'] ?: '';
$this->response->html($this->template->layout('task_new', array(
'errors' => array(),
'values' => $task,
'columns_list' => $this->board->getColumnsList($task['project_id']),
'users_list' => $this->project->getUsersList($task['project_id']),
'colors_list' => $this->task->getColors(),
'categories_list' => $this->category->getList($task['project_id']),
'duplicate' => true,
$this->response->html($this->taskLayout('task_duplicate', array(
'task' => $task,
'menu' => 'tasks',
'title' => t('New task')
'title' => t('Duplicate a task')
)));
}

View File

@ -436,4 +436,6 @@ return array(
// 'Project activation' => '',
// 'Move the task to another project' => '',
// 'Move to another project' => '',
// 'Do you really want to duplicate this task?' => '',
// 'Duplicate a task' => '',
);

View File

@ -436,4 +436,6 @@ return array(
// 'Project activation' => '',
// 'Move the task to another project' => '',
// 'Move to another project' => '',
// 'Do you really want to duplicate this task?' => '',
// 'Duplicate a task' => '',
);

View File

@ -436,4 +436,6 @@ return array(
// 'Project activation' => '',
// 'Move the task to another project' => '',
// 'Move to another project' => '',
// 'Do you really want to duplicate this task?' => '',
// 'Duplicate a task' => '',
);

View File

@ -436,4 +436,6 @@ return array(
'Project activation' => 'Activation du projet',
'Move the task to another project' => 'Déplacer la tâche vers un autre projet',
'Move to another project' => 'Déplacer vers un autre projet',
'Do you really want to duplicate this task?' => 'Voulez-vous vraiment dupliquer cette tâche ?',
'Duplicate a task' => 'Dupliquer une tâche'
);

View File

@ -436,4 +436,6 @@ return array(
// 'Project activation' => '',
// 'Move the task to another project' => '',
// 'Move to another project' => '',
// 'Do you really want to duplicate this task?' => '',
// 'Duplicate a task' => '',
);

View File

@ -436,4 +436,6 @@ return array(
// 'Project activation' => '',
// 'Move the task to another project' => '',
// 'Move to another project' => '',
// 'Do you really want to duplicate this task?' => '',
// 'Duplicate a task' => '',
);

View File

@ -436,4 +436,6 @@ return array(
// 'Project activation' => '',
// 'Move the task to another project' => '',
// 'Move to another project' => '',
// 'Do you really want to duplicate this task?' => '',
// 'Duplicate a task' => '',
);

View File

@ -436,4 +436,6 @@ return array(
// 'Project activation' => '',
// 'Move the task to another project' => '',
// 'Move to another project' => '',
// 'Do you really want to duplicate this task?' => '',
// 'Duplicate a task' => '',
);

View File

@ -436,4 +436,6 @@ return array(
// 'Project activation' => '',
// 'Move the task to another project' => '',
// 'Move to another project' => '',
// 'Do you really want to duplicate this task?' => '',
// 'Duplicate a task' => '',
);

View File

@ -20,6 +20,19 @@ class Category extends Base
*/
const TABLE = 'project_has_categories';
/**
* Return true if a category exists for a given project
*
* @access public
* @param integer $category_id Category id
* @param integer $project_id Project id
* @return boolean
*/
public function exists($category_id, $project_id)
{
return $this->db->table(self::TABLE)->eq('id', $category_id)->eq('project_id', $project_id)->count() > 0;
}
/**
* Get a category by the id
*

View File

@ -263,55 +263,20 @@ class Task extends Base
}
/**
* Duplicate a task
* Generic method to duplicate a task
*
* @access public
* @param integer $task_id Task id
* @return boolean
* @param array $task Task data
* @param array $override Task properties to override
* @return integer|boolean
*/
public function duplicate($task_id)
public function copy(array $task, array $override = array())
{
$this->db->startTransaction();
// Get the original task
$task = $this->getById($task_id);
// Cleanup data
unset($task['id']);
unset($task['date_completed']);
// Assign new values
$task['date_creation'] = time();
$task['is_active'] = 1;
$task['position'] = $this->countByColumnId($task['project_id'], $task['column_id']);
// Save task
if (! $this->db->table(self::TABLE)->save($task)) {
$this->db->cancelTransaction();
return false;
// Values to override
if (! empty($override)) {
$task = $override + $task;
}
$task_id = $this->db->getConnection()->getLastId();
$this->db->closeTransaction();
// Trigger events
$this->event->trigger(self::EVENT_CREATE_UPDATE, array('task_id' => $task_id) + $task);
$this->event->trigger(self::EVENT_CREATE, array('task_id' => $task_id) + $task);
return $task_id;
}
/**
* Duplicate a task to another project (always copy to the first column)
*
* @access public
* @param integer $project_id Destination project id
* @param array $task Task data
* @return boolean
*/
public function duplicateToAnotherProject($project_id, array $task)
{
$this->db->startTransaction();
// Assign new values
@ -322,19 +287,24 @@ class Task extends Base
$values['date_modification'] = $values['date_creation'];
$values['date_due'] = $task['date_due'];
$values['color_id'] = $task['color_id'];
$values['project_id'] = $project_id;
$values['column_id'] = $this->board->getFirstColumn($project_id);
$values['project_id'] = $task['project_id'];
$values['column_id'] = $task['column_id'];
$values['owner_id'] = 0;
$values['creator_id'] = $task['creator_id'];
$values['position'] = $this->countByColumnId($project_id, $values['column_id']);
$values['position'] = $this->countByColumnId($values['project_id'], $values['column_id']) + 1;
$values['score'] = $task['score'];
$values['category_id'] = 0;
// Check if the assigned user is allowed for the new project
if ($task['owner_id'] && $this->project->isUserAllowed($project_id, $task['owner_id'])) {
if ($task['owner_id'] && $this->project->isUserAllowed($values['project_id'], $task['owner_id'])) {
$values['owner_id'] = $task['owner_id'];
}
// Check if the category exists
if ($task['category_id'] && $this->category->exists($task['category_id'], $task['project_id'])) {
$values['category_id'] = $task['category_id'];
}
// Save task
if (! $this->db->table(self::TABLE)->save($values)) {
$this->db->cancelTransaction();
@ -358,6 +328,34 @@ class Task extends Base
return $task_id;
}
/**
* Duplicate a task to the same project
*
* @access public
* @param array $task Task data
* @return integer|boolean
*/
public function duplicateSameProject($task)
{
return $this->copy($task);
}
/**
* Duplicate a task to another project (always copy to the first column)
*
* @access public
* @param integer $project_id Destination project id
* @param array $task Task data
* @return integer|boolean
*/
public function duplicateToAnotherProject($project_id, array $task)
{
return $this->copy($task, array(
'project_id' => $project_id,
'column_id' => $this->board->getFirstColumn($project_id),
));
}
/**
* Prepare data before task creation or modification
*
@ -399,7 +397,7 @@ class Task extends Base
$this->prepare($values);
$values['date_creation'] = time();
$values['date_modification'] = $values['date_creation'];
$values['position'] = $this->countByColumnId($values['project_id'], $values['column_id']);
$values['position'] = $this->countByColumnId($values['project_id'], $values['column_id']) + 1;
// Save task
if (! $this->db->table(self::TABLE)->save($values)) {
@ -592,7 +590,7 @@ class Task extends Base
// We use the first column of the new project
$values['column_id'] = $this->board->getFirstColumn($project_id);
$values['position'] = $this->countByColumnId($project_id, $values['column_id']);
$values['position'] = $this->countByColumnId($project_id, $values['column_id']) + 1;
$values['project_id'] = $project_id;
if ($this->db->table(self::TABLE)->eq('id', $task['id'])->update($values)) {

View File

@ -0,0 +1,14 @@
<div class="page-header">
<h2><?= t('Duplicate a task') ?></h2>
</div>
<div class="confirm">
<p class="alert alert-info">
<?= t('Do you really want to duplicate this task?') ?>
</p>
<div class="form-actions">
<a href="?controller=task&amp;action=duplicate&amp;confirmation=yes&amp;task_id=<?= $task['id'].Helper\param_csrf() ?>" class="btn btn-red"><?= t('Yes') ?></a>
<?= t('or') ?> <a href="?controller=task&amp;action=show&amp;task_id=<?= $task['id'] ?>"><?= t('cancel') ?></a>
</div>
</div>

View File

@ -41,6 +41,7 @@
<strong><?= Helper\escape($task['column_title']) ?></strong>
(<?= Helper\escape($task['project_name']) ?>)
</li>
<li><?= t('Task position:').' '.Helper\escape($task['position']) ?></li>
<?php if ($task['category_name']): ?>
<li>
<?= t('Category:') ?> <strong><?= Helper\escape($task['category_name']) ?></strong>

View File

@ -9,8 +9,8 @@
<li><a href="?controller=comment&amp;action=create&amp;task_id=<?= $task['id'] ?>"><?= t('Add a comment') ?></a></li>
<li><a href="?controller=file&amp;action=create&amp;task_id=<?= $task['id'] ?>"><?= t('Attach a document') ?></a></li>
<li><a href="?controller=task&amp;action=duplicate&amp;project_id=<?= $task['project_id'] ?>&amp;task_id=<?= $task['id'] ?>"><?= t('Duplicate') ?></a></li>
<li><a href="?controller=task&amp;action=move&amp;project_id=<?= $task['project_id'] ?>&amp;task_id=<?= $task['id'] ?>"><?= t('Move to another project') ?></a></li>
<li><a href="?controller=task&amp;action=copy&amp;project_id=<?= $task['project_id'] ?>&amp;task_id=<?= $task['id'] ?>"><?= t('Duplicate to another project') ?></a></li>
<li><a href="?controller=task&amp;action=move&amp;project_id=<?= $task['project_id'] ?>&amp;task_id=<?= $task['id'] ?>"><?= t('Move to another project') ?></a></li>
<li>
<?php if ($task['is_active'] == 1): ?>
<a href="?controller=task&amp;action=confirmClose&amp;task_id=<?= $task['id'] ?>"><?= t('Close this task') ?></a>

View File

@ -142,29 +142,29 @@ class ActionTest extends Base
// Our task should have the color red and position=0
$t1 = $task->getById(1);
$this->assertEquals(0, $t1['position']);
$this->assertEquals(1, $t1['position']);
$this->assertEquals(1, $t1['is_active']);
$this->assertEquals('red', $t1['color_id']);
$t1 = $task->getById(2);
$this->assertEquals(1, $t1['position']);
$this->assertEquals(2, $t1['position']);
$this->assertEquals(1, $t1['is_active']);
$this->assertEquals('yellow', $t1['color_id']);
// We move our tasks
$task->movePosition(1, 1, 1); // task #1 to position 1
$task->movePosition(2, 1, 0); // task #2 to position 0
$task->movePosition(1, 1, 2); // task #1 to position 2
$task->movePosition(2, 1, 1); // task #2 to position 1
$this->assertTrue($this->registry->event->isEventTriggered(Task::EVENT_MOVE_POSITION));
// Both tasks should be green
$t1 = $task->getById(1);
$this->assertEquals(1, $t1['position']);
$this->assertEquals(2, $t1['position']);
$this->assertEquals(1, $t1['is_active']);
$this->assertEquals('green', $t1['color_id']);
$t1 = $task->getById(2);
$this->assertEquals(0, $t1['position']);
$this->assertEquals(1, $t1['position']);
$this->assertEquals(1, $t1['is_active']);
$this->assertEquals('green', $t1['color_id']);
}

View File

@ -132,21 +132,29 @@ class TaskTest extends Base
$this->assertEquals('2014-03-05', date('Y-m-d', $t->parseDate('03/05/2014')));
}
public function testDuplicateTask()
public function testDuplicateToTheSameProject()
{
$t = new Task($this->registry);
$p = new Project($this->registry);
$c = new Category($this->registry);
// We create a task and a project
$this->assertEquals(1, $p->create(array('name' => 'test1')));
// Some categories
$this->assertNotFalse($c->create(array('name' => 'Category #1', 'project_id' => 1)));
$this->assertNotFalse($c->create(array('name' => 'Category #2', 'project_id' => 1)));
$this->assertTrue($c->exists(1, 1));
$this->assertTrue($c->exists(2, 1));
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1, 'category_id' => 2)));
$task = $t->getById(1);
$this->assertNotEmpty($task);
$this->assertEquals(0, $task['position']);
$this->assertEquals(1, $task['position']);
// We duplicate our task
$this->assertEquals(2, $t->duplicate(1));
$this->assertEquals(2, $t->duplicateSameProject($task));
$this->assertTrue($this->registry->event->isEventTriggered(Task::EVENT_CREATE));
// Check the values of the duplicated task
@ -155,21 +163,26 @@ class TaskTest extends Base
$this->assertEquals(Task::STATUS_OPEN, $task['is_active']);
$this->assertEquals(1, $task['project_id']);
$this->assertEquals(1, $task['owner_id']);
$this->assertEquals(1, $task['position']);
$this->assertEquals(2, $task['category_id']);
$this->assertEquals(3, $task['column_id']);
$this->assertEquals(2, $task['position']);
}
public function testDuplicateToAnotherProject()
{
$t = new Task($this->registry);
$p = new Project($this->registry);
$c = new Category($this->registry);
// We create 2 projects
$this->assertEquals(1, $p->create(array('name' => 'test1')));
$this->assertEquals(2, $p->create(array('name' => 'test2')));
$this->assertNotFalse($c->create(array('name' => 'Category #1', 'project_id' => 1)));
$this->assertTrue($c->exists(1, 1));
// We create a task
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1, 'category_id' => 1)));
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 1, 'category_id' => 1)));
$task = $t->getById(1);
// We duplicate our task to the 2nd project
@ -181,6 +194,8 @@ class TaskTest extends Base
$this->assertNotEmpty($task);
$this->assertEquals(1, $task['owner_id']);
$this->assertEquals(0, $task['category_id']);
$this->assertEquals(5, $task['column_id']);
$this->assertEquals(1, $task['position']);
$this->assertEquals(2, $task['project_id']);
$this->assertEquals('test', $task['title']);
}
@ -215,7 +230,7 @@ class TaskTest extends Base
$this->assertEquals(0, $task['category_id']);
$this->assertEquals(2, $task['project_id']);
$this->assertEquals(5, $task['column_id']);
$this->assertEquals(0, $task['position']);
$this->assertEquals(1, $task['position']);
$this->assertEquals('test', $task['title']);
// We allow only one user on the second project