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/Model/Project.php | |
parent | 532b16cbdd58d3d7083eb14886a0ae826629d645 (diff) |
Fix a bug and improve project cloning code
Diffstat (limited to 'app/Model/Project.php')
-rw-r--r-- | app/Model/Project.php | 142 |
1 files changed, 17 insertions, 125 deletions
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(); |