Fix a bug and improve project cloning code
This commit is contained in:
@@ -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
|
* Validate action creation
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -51,19 +51,20 @@ class Board extends Base
|
|||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param integer $project_id Project id
|
* @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
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function create($project_id, array $columns)
|
public function create($project_id, array $columns)
|
||||||
{
|
{
|
||||||
$position = 0;
|
$position = 0;
|
||||||
|
|
||||||
foreach ($columns as $title) {
|
foreach ($columns as $column) {
|
||||||
|
|
||||||
$values = array(
|
$values = array(
|
||||||
'title' => $title,
|
'title' => $column['title'],
|
||||||
'position' => ++$position,
|
'position' => ++$position,
|
||||||
'project_id' => $project_id,
|
'project_id' => $project_id,
|
||||||
|
'task_limit' => $column['task_limit'],
|
||||||
);
|
);
|
||||||
|
|
||||||
if (! $this->db->table(self::TABLE)->save($values)) {
|
if (! $this->db->table(self::TABLE)->save($values)) {
|
||||||
@@ -74,6 +75,25 @@ class Board extends Base
|
|||||||
return true;
|
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
|
* Add a new column to the board
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -117,6 +117,34 @@ class Category extends Base
|
|||||||
return $r1 && $r2;
|
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
|
* Validate category creation
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -391,58 +391,36 @@ class Project extends Base
|
|||||||
*/
|
*/
|
||||||
public function createProjectFromAnotherProject($project_id)
|
public function createProjectFromAnotherProject($project_id)
|
||||||
{
|
{
|
||||||
// Recover the template project data
|
$project_name = $this->db->table(self::TABLE)->eq('id', $project_id)->findOneColumn('name');
|
||||||
$project = $this->getById($project_id);
|
|
||||||
|
|
||||||
// Create a Clone project
|
$project = array(
|
||||||
$clone_project = array(
|
'name' => $project_name.' ('.t('Clone').')',
|
||||||
'name' => $project['name'].' ('.t('Clone').')',
|
|
||||||
'is_active' => true,
|
'is_active' => true,
|
||||||
'last_modified' => 0,
|
'last_modified' => 0,
|
||||||
'token' => Security::generateToken(),
|
'token' => Security::generateToken(),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Register the cloned project
|
if (! $this->db->table(self::TABLE)->save($project)) {
|
||||||
if (! $this->db->table(self::TABLE)->save($clone_project)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the cloned project Id
|
|
||||||
return $this->db->getConnection()->getLastId();
|
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
|
* @author Antonio Rabelo
|
||||||
* @param integer $project_from Project Template
|
* @param integer $project_from Project Template
|
||||||
* @return integer $project_to Project that receives the copy
|
* @return integer $project_to Project that receives the copy
|
||||||
* @return boolean
|
* @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');
|
$users = $this->getAllowedUsers($project_from);
|
||||||
return $this->board->create($project_to, $columns);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
foreach ($users as $user_id => $name) {
|
||||||
* Copy Categories from a project to another one.
|
if (! $this->allowUser($project_to, $user_id)) {
|
||||||
*
|
|
||||||
* @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)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -450,92 +428,6 @@ class Project extends Base
|
|||||||
return true;
|
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
|
* Clone a project
|
||||||
*
|
*
|
||||||
@@ -555,25 +447,25 @@ class Project extends Base
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Clone Board
|
// Clone Board
|
||||||
if (! $this->copyBoardFromAnotherProject($project_id, $clone_project_id)) {
|
if (! $this->board->duplicate($project_id, $clone_project_id)) {
|
||||||
$this->db->cancelTransaction();
|
$this->db->cancelTransaction();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clone Categories
|
// Clone Categories
|
||||||
if (! $this->copyCategoriesFromAnotherProject($project_id, $clone_project_id)) {
|
if (! $this->category->duplicate($project_id, $clone_project_id)) {
|
||||||
$this->db->cancelTransaction();
|
$this->db->cancelTransaction();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clone Allowed Users
|
// Clone Allowed Users
|
||||||
if (! $this->copyUserAccessFromAnotherProject($project_id, $clone_project_id)) {
|
if (! $this->duplicateUsers($project_id, $clone_project_id)) {
|
||||||
$this->db->cancelTransaction();
|
$this->db->cancelTransaction();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clone Actions
|
// Clone Actions
|
||||||
if (! $this->copyActionsFromAnotherProject($project_id, $clone_project_id)) {
|
if (! $this->action->duplicate($project_id, $clone_project_id)) {
|
||||||
$this->db->cancelTransaction();
|
$this->db->cancelTransaction();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -604,10 +496,10 @@ class Project extends Base
|
|||||||
$project_id = $this->db->getConnection()->getLastId();
|
$project_id = $this->db->getConnection()->getLastId();
|
||||||
|
|
||||||
$this->board->create($project_id, array(
|
$this->board->create($project_id, array(
|
||||||
t('Backlog'),
|
array('title' => t('Backlog'), 'task_limit' => 0),
|
||||||
t('Ready'),
|
array('title' => t('Ready'), 'task_limit' => 0),
|
||||||
t('Work in progress'),
|
array('title' => t('Work in progress'), 'task_limit' => 0),
|
||||||
t('Done'),
|
array('title' => t('Done'), 'task_limit' => 0),
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->db->closeTransaction();
|
$this->db->closeTransaction();
|
||||||
|
|||||||
Reference in New Issue
Block a user