diff options
author | Frédéric Guillot <fred@kanboard.net> | 2014-08-19 17:43:01 -0700 |
---|---|---|
committer | Frédéric Guillot <fred@kanboard.net> | 2014-08-19 17:43:01 -0700 |
commit | 5e10d2d29faaf0cce4274a5b7ffa0735a0aa04a1 (patch) | |
tree | 979d41628f961e281d1c37221bdd76c7196e4313 /app | |
parent | 532b16cbdd58d3d7083eb14886a0ae826629d645 (diff) |
Fix a bug and improve project cloning code
Diffstat (limited to 'app')
-rw-r--r-- | app/Model/Action.php | 65 | ||||
-rw-r--r-- | app/Model/Board.php | 26 | ||||
-rw-r--r-- | app/Model/Category.php | 28 | ||||
-rw-r--r-- | app/Model/Project.php | 142 |
4 files changed, 133 insertions, 128 deletions
diff --git a/app/Model/Action.php b/app/Model/Action.php index effe8707..f296a58f 100644 --- a/app/Model/Action.php +++ b/app/Model/Action.php @@ -249,6 +249,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 * * @access public diff --git a/app/Model/Board.php b/app/Model/Board.php index 168b185f..74e29040 100644 --- a/app/Model/Board.php +++ b/app/Model/Board.php @@ -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)) { @@ -75,6 +76,25 @@ class Board extends Base } /** + * 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 * * @access public diff --git a/app/Model/Category.php b/app/Model/Category.php index f86abe58..58841f9a 100644 --- a/app/Model/Category.php +++ b/app/Model/Category.php @@ -118,6 +118,34 @@ class Category extends Base } /** + * 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 * * @access public diff --git a/app/Model/Project.php b/app/Model/Project.php index 9ee54cbd..dc72addc 100644 --- a/app/Model/Project.php +++ b/app/Model/Project.php @@ -391,152 +391,44 @@ 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. - * - * @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) - { - $columns = $this->db->table(Board::TABLE)->eq('project_id', $project_from)->asc('position')->findAllByColumn('title'); - return $this->board->create($project_to, $columns); - } - - /** - * 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)) { - return false; - } - } - - 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. + * 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 copyActionsFromAnotherProject($project_from, $project_to) + public function duplicateUsers($project_from, $project_to) { - $actionTemplate = $this->action->getAllByProject($project_from); + $users = $this->getAllowedUsers($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)) { + foreach ($users as $user_id => $name) { + if (! $this->allowUser($project_to, $user_id)) { 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 * * @author Antonio Rabelo @@ -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(); |