Fix a bug and improve project cloning code
This commit is contained in:
parent
532b16cbdd
commit
5e10d2d29f
|
|
@ -248,6 +248,71 @@ class Action extends Base
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy Actions and related Actions Parameters from a project to another one
|
||||
*
|
||||
* @author Antonio Rabelo
|
||||
* @param integer $project_from Project Template
|
||||
* @return integer $project_to Project that receives the copy
|
||||
* @return boolean
|
||||
*/
|
||||
public function duplicate($project_from, $project_to)
|
||||
{
|
||||
$actionTemplate = $this->action->getAllByProject($project_from);
|
||||
|
||||
foreach ($actionTemplate as $action) {
|
||||
|
||||
unset($action['id']);
|
||||
$action['project_id'] = $project_to;
|
||||
$actionParams = $action['params'];
|
||||
unset($action['params']);
|
||||
|
||||
if (! $this->db->table(self::TABLE)->save($action)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$action_clone_id = $this->db->getConnection()->getLastId();
|
||||
|
||||
foreach ($actionParams as $param) {
|
||||
unset($param['id']);
|
||||
$param['value'] = $this->resolveDuplicatedParameters($param, $project_to);
|
||||
$param['action_id'] = $action_clone_id;
|
||||
|
||||
if (! $this->db->table(self::TABLE_PARAMS)->save($param)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve type of action value from a project to the respective value in another project
|
||||
*
|
||||
* @author Antonio Rabelo
|
||||
* @param integer $param An action parameter
|
||||
* @return integer $project_to Project to find the corresponding values
|
||||
* @return mixed The corresponding values from $project_to
|
||||
*/
|
||||
private function resolveDuplicatedParameters($param, $project_to)
|
||||
{
|
||||
switch($param['name']) {
|
||||
case 'project_id':
|
||||
return $project_to;
|
||||
case 'category_id':
|
||||
$categoryTemplate = $this->category->getById($param['value']);
|
||||
$categoryFromNewProject = $this->db->table(Category::TABLE)->eq('project_id', $project_to)->eq('name', $categoryTemplate['name'])->findOne();
|
||||
return $categoryFromNewProject['id'];
|
||||
case 'column_id':
|
||||
$boardTemplate = $this->board->getColumn($param['value']);
|
||||
$boardFromNewProject = $this->db->table(Board::TABLE)->eq('project_id', $project_to)->eq('title', $boardTemplate['title'])->findOne();
|
||||
return $boardFromNewProject['id'];
|
||||
default:
|
||||
return $param['value'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate action creation
|
||||
*
|
||||
|
|
|
|||
|
|
@ -51,19 +51,20 @@ class Board extends Base
|
|||
*
|
||||
* @access public
|
||||
* @param integer $project_id Project id
|
||||
* @param array $columns List of columns title ['column1', 'column2', ...]
|
||||
* @param array $columns Column parameters [ 'title' => 'boo', 'task_limit' => 2 ... ]
|
||||
* @return boolean
|
||||
*/
|
||||
public function create($project_id, array $columns)
|
||||
{
|
||||
$position = 0;
|
||||
|
||||
foreach ($columns as $title) {
|
||||
foreach ($columns as $column) {
|
||||
|
||||
$values = array(
|
||||
'title' => $title,
|
||||
'title' => $column['title'],
|
||||
'position' => ++$position,
|
||||
'project_id' => $project_id,
|
||||
'task_limit' => $column['task_limit'],
|
||||
);
|
||||
|
||||
if (! $this->db->table(self::TABLE)->save($values)) {
|
||||
|
|
@ -74,6 +75,25 @@ class Board extends Base
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy board columns from a project to another one
|
||||
*
|
||||
* @author Antonio Rabelo
|
||||
* @param integer $project_from Project Template
|
||||
* @return integer $project_to Project that receives the copy
|
||||
* @return boolean
|
||||
*/
|
||||
public function duplicate($project_from, $project_to)
|
||||
{
|
||||
$columns = $this->db->table(Board::TABLE)
|
||||
->columns('title', 'task_limit')
|
||||
->eq('project_id', $project_from)
|
||||
->asc('position')
|
||||
->findAll();
|
||||
|
||||
return $this->board->create($project_to, $columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new column to the board
|
||||
*
|
||||
|
|
|
|||
|
|
@ -117,6 +117,34 @@ class Category extends Base
|
|||
return $r1 && $r2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Duplicate categories from a project to another one
|
||||
*
|
||||
* @author Antonio Rabelo
|
||||
* @param integer $project_from Project Template
|
||||
* @return integer $project_to Project that receives the copy
|
||||
* @return boolean
|
||||
*/
|
||||
public function duplicate($project_from, $project_to)
|
||||
{
|
||||
$categories = $this->db->table(self::TABLE)
|
||||
->columns('name')
|
||||
->eq('project_id', $project_from)
|
||||
->asc('name')
|
||||
->findAll();
|
||||
|
||||
foreach ($categories as $category) {
|
||||
|
||||
$category['project_id'] = $project_to;
|
||||
|
||||
if (! $this->category->create($category)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate category creation
|
||||
*
|
||||
|
|
|
|||
|
|
@ -391,58 +391,36 @@ class Project extends Base
|
|||
*/
|
||||
public function createProjectFromAnotherProject($project_id)
|
||||
{
|
||||
// Recover the template project data
|
||||
$project = $this->getById($project_id);
|
||||
$project_name = $this->db->table(self::TABLE)->eq('id', $project_id)->findOneColumn('name');
|
||||
|
||||
// Create a Clone project
|
||||
$clone_project = array(
|
||||
'name' => $project['name'].' ('.t('Clone').')',
|
||||
$project = array(
|
||||
'name' => $project_name.' ('.t('Clone').')',
|
||||
'is_active' => true,
|
||||
'last_modified' => 0,
|
||||
'token' => Security::generateToken(),
|
||||
);
|
||||
|
||||
// Register the cloned project
|
||||
if (! $this->db->table(self::TABLE)->save($clone_project)) {
|
||||
if (! $this->db->table(self::TABLE)->save($project)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the cloned project Id
|
||||
return $this->db->getConnection()->getLastId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy Board Columns from a project to another one.
|
||||
* Copy user access from a project to another one
|
||||
*
|
||||
* @author Antonio Rabelo
|
||||
* @param integer $project_from Project Template
|
||||
* @return integer $project_to Project that receives the copy
|
||||
* @return boolean
|
||||
*/
|
||||
public function copyBoardFromAnotherProject($project_from, $project_to)
|
||||
public function duplicateUsers($project_from, $project_to)
|
||||
{
|
||||
$columns = $this->db->table(Board::TABLE)->eq('project_id', $project_from)->asc('position')->findAllByColumn('title');
|
||||
return $this->board->create($project_to, $columns);
|
||||
}
|
||||
$users = $this->getAllowedUsers($project_from);
|
||||
|
||||
/**
|
||||
* Copy Categories from a project to another one.
|
||||
*
|
||||
* @author Antonio Rabelo
|
||||
* @param integer $project_from Project Template
|
||||
* @return integer $project_to Project that receives the copy
|
||||
* @return boolean
|
||||
*/
|
||||
public function copyCategoriesFromAnotherProject($project_from, $project_to)
|
||||
{
|
||||
$categoriesTemplate = $this->category->getAll($project_from);
|
||||
|
||||
foreach ($categoriesTemplate as $category) {
|
||||
|
||||
unset($category['id']);
|
||||
$category['project_id'] = $project_to;
|
||||
|
||||
if (! $this->category->create($category)) {
|
||||
foreach ($users as $user_id => $name) {
|
||||
if (! $this->allowUser($project_to, $user_id)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -450,92 +428,6 @@ class Project extends Base
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy User Access from a project to another one.
|
||||
*
|
||||
* @author Antonio Rabelo
|
||||
* @param integer $project_from Project Template
|
||||
* @return integer $project_to Project that receives the copy
|
||||
* @return boolean
|
||||
*/
|
||||
public function copyUserAccessFromAnotherProject($project_from, $project_to)
|
||||
{
|
||||
$usersList = $this->getAllowedUsers($project_from);
|
||||
|
||||
foreach ($usersList as $id => $userName) {
|
||||
if (! $this->allowUser($project_to, $id)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy Actions and related Actions Parameters from a project to another one.
|
||||
*
|
||||
* @author Antonio Rabelo
|
||||
* @param integer $project_from Project Template
|
||||
* @return integer $project_to Project that receives the copy
|
||||
* @return boolean
|
||||
*/
|
||||
public function copyActionsFromAnotherProject($project_from, $project_to)
|
||||
{
|
||||
$actionTemplate = $this->action->getAllByProject($project_from);
|
||||
|
||||
foreach ($actionTemplate as $action) {
|
||||
|
||||
unset($action['id']);
|
||||
$action['project_id'] = $project_to;
|
||||
$actionParams = $action['params'];
|
||||
unset($action['params']);
|
||||
|
||||
if (! $this->db->table(Action::TABLE)->save($action)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$action_clone_id = $this->db->getConnection()->getLastId();
|
||||
|
||||
foreach ($actionParams as $param) {
|
||||
unset($param['id']);
|
||||
$param['value'] = $this->resolveValueParamToClonedAction($param, $project_to);
|
||||
$param['action_id'] = $action_clone_id;
|
||||
|
||||
if (! $this->db->table(Action::TABLE_PARAMS)->save($param)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve type of action value from a project to the respective value in another project.
|
||||
*
|
||||
* @author Antonio Rabelo
|
||||
* @param integer $param A action parameter
|
||||
* @return integer $project_to Project to find the corresponding values
|
||||
* @return mixed The corresponding values from $project_to
|
||||
*/
|
||||
private function resolveValueParamToClonedAction($param, $project_to)
|
||||
{
|
||||
switch($param['name']) {
|
||||
case 'project_id':
|
||||
return $project_to;
|
||||
case 'category_id':
|
||||
$categoryTemplate = $this->category->getById($param['value']);
|
||||
$categoryFromNewProject = $this->db->table(Category::TABLE)->eq('project_id', $project_to)->eq('name', $categoryTemplate['name'])->findOne();
|
||||
return $categoryFromNewProject['id'];
|
||||
case 'column_id':
|
||||
$boardTemplate = $this->board->getColumn($param['value']);
|
||||
$boardFromNewProject = $this->db->table(Board::TABLE)->eq('project_id', $project_to)->eq('title', $boardTemplate['title'])->findOne();
|
||||
return $boardFromNewProject['id'];
|
||||
default:
|
||||
return $param['value'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a project
|
||||
*
|
||||
|
|
@ -555,25 +447,25 @@ class Project extends Base
|
|||
}
|
||||
|
||||
// Clone Board
|
||||
if (! $this->copyBoardFromAnotherProject($project_id, $clone_project_id)) {
|
||||
if (! $this->board->duplicate($project_id, $clone_project_id)) {
|
||||
$this->db->cancelTransaction();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Clone Categories
|
||||
if (! $this->copyCategoriesFromAnotherProject($project_id, $clone_project_id)) {
|
||||
if (! $this->category->duplicate($project_id, $clone_project_id)) {
|
||||
$this->db->cancelTransaction();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Clone Allowed Users
|
||||
if (! $this->copyUserAccessFromAnotherProject($project_id, $clone_project_id)) {
|
||||
if (! $this->duplicateUsers($project_id, $clone_project_id)) {
|
||||
$this->db->cancelTransaction();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Clone Actions
|
||||
if (! $this->copyActionsFromAnotherProject($project_id, $clone_project_id)) {
|
||||
if (! $this->action->duplicate($project_id, $clone_project_id)) {
|
||||
$this->db->cancelTransaction();
|
||||
return false;
|
||||
}
|
||||
|
|
@ -604,10 +496,10 @@ class Project extends Base
|
|||
$project_id = $this->db->getConnection()->getLastId();
|
||||
|
||||
$this->board->create($project_id, array(
|
||||
t('Backlog'),
|
||||
t('Ready'),
|
||||
t('Work in progress'),
|
||||
t('Done'),
|
||||
array('title' => t('Backlog'), 'task_limit' => 0),
|
||||
array('title' => t('Ready'), 'task_limit' => 0),
|
||||
array('title' => t('Work in progress'), 'task_limit' => 0),
|
||||
array('title' => t('Done'), 'task_limit' => 0),
|
||||
));
|
||||
|
||||
$this->db->closeTransaction();
|
||||
|
|
|
|||
Loading…
Reference in New Issue