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>