| <?php |
|
|
| namespace Kanboard\Model; |
|
|
| use Kanboard\Core\Base; |
| use Kanboard\Core\Security\Role; |
|
|
| |
| |
| |
| |
| |
| |
| |
| class ProjectDuplicationModel extends Base |
| { |
| |
| |
| |
| |
| |
| |
| public function getOptionalSelection() |
| { |
| return array( |
| 'categoryModel', |
| 'projectRoleModel', |
| 'projectPermissionModel', |
| 'actionModel', |
| 'tagDuplicationModel', |
| 'customFilterModel', |
| 'projectMetadataModel', |
| 'projectTaskDuplicationModel', |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function getPossibleSelection() |
| { |
| return array( |
| 'swimlaneModel', |
| 'boardModel', |
| 'categoryModel', |
| 'projectRoleModel', |
| 'projectPermissionModel', |
| 'actionModel', |
| 'swimlaneModel', |
| 'tagDuplicationModel', |
| 'customFilterModel', |
| 'projectMetadataModel', |
| 'projectTaskDuplicationModel', |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function getClonedProjectName($name, $max_length = 50) |
| { |
| $suffix = ' ('.t('Clone').')'; |
|
|
| if (strlen($name.$suffix) > $max_length) { |
| $name = substr($name, 0, $max_length - strlen($suffix)); |
| } |
|
|
| return $name.$suffix; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function duplicate($src_project_id, $selection = array('projectPermissionModel', 'categoryModel', 'actionModel'), $owner_id = 0, $name = null, $private = null, $identifier = null) |
| { |
| $this->db->startTransaction(); |
|
|
| |
| $dst_project_id = $this->copy($src_project_id, $owner_id, $name, $private, $identifier); |
|
|
| if ($dst_project_id === false) { |
| $this->db->cancelTransaction(); |
| return false; |
| } |
|
|
| |
| foreach ($this->getPossibleSelection() as $model) { |
|
|
| |
| if (in_array($model, $this->getOptionalSelection()) && ! in_array($model, $selection)) { |
| continue; |
| } |
|
|
| |
| if ($private && $model === 'projectPermissionModel') { |
| continue; |
| } |
|
|
| if (! $this->$model->duplicate($src_project_id, $dst_project_id)) { |
| $this->db->cancelTransaction(); |
| return false; |
| } |
| } |
|
|
| if (! $this->makeOwnerManager($dst_project_id, $owner_id)) { |
| $this->db->cancelTransaction(); |
| return false; |
| } |
|
|
| $this->db->closeTransaction(); |
|
|
| return (int) $dst_project_id; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| private function copy($src_project_id, $owner_id = 0, $name = null, $private = null, $identifier = null) |
| { |
| $project = $this->projectModel->getById($src_project_id); |
| $is_private = empty($project['is_private']) ? 0 : 1; |
|
|
| if (! empty($identifier)) { |
| $identifier = strtoupper($identifier); |
| } |
|
|
| $values = array( |
| 'name' => $name ?: $this->getClonedProjectName($project['name']), |
| 'is_active' => 1, |
| 'last_modified' => time(), |
| 'token' => '', |
| 'is_public' => 0, |
| 'is_private' => $private ? 1 : $is_private, |
| 'owner_id' => $owner_id, |
| 'priority_default' => $project['priority_default'], |
| 'priority_start' => $project['priority_start'], |
| 'priority_end' => $project['priority_end'], |
| 'per_swimlane_task_limits' => empty($project['per_swimlane_task_limits']) ? 0 : 1, |
| 'task_limit' => $project['task_limit'], |
| 'identifier' => $identifier, |
| ); |
|
|
| return $this->db->table(ProjectModel::TABLE)->persist($values); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| private function makeOwnerManager($dst_project_id, $owner_id) |
| { |
| if ($owner_id > 0) { |
| $this->projectUserRoleModel->removeUser($dst_project_id, $owner_id); |
|
|
| if (! $this->projectUserRoleModel->addUser($dst_project_id, $owner_id, Role::PROJECT_MANAGER)) { |
| return false; |
| } |
| } |
|
|
| return true; |
| } |
| } |
|
|