1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Model; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Database\Model; |
15
|
|
|
use Jitamin\Foundation\Security\Role; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Project Duplication. |
19
|
|
|
*/ |
20
|
|
|
class ProjectDuplicationModel extends Model |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Get list of optional models to duplicate. |
24
|
|
|
* |
25
|
|
|
* @return string[] |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
public function getOptionalSelection() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
'categoryModel', |
31
|
|
|
'projectPermissionModel', |
32
|
|
|
'actionModel', |
33
|
|
|
'swimlaneModel', |
34
|
|
|
'tagDuplicationModel', |
35
|
|
|
'projectMetadataModel', |
36
|
|
|
'projectTaskDuplicationModel', |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get list of all possible models to duplicate. |
42
|
|
|
* |
43
|
|
|
* @return string[] |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function getPossibleSelection() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
'boardModel', |
49
|
|
|
'categoryModel', |
50
|
|
|
'projectPermissionModel', |
51
|
|
|
'actionModel', |
52
|
|
|
'swimlaneModel', |
53
|
|
|
'tagDuplicationModel', |
54
|
|
|
'projectMetadataModel', |
55
|
|
|
'projectTaskDuplicationModel', |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get a valid project name for the duplication. |
61
|
|
|
* |
62
|
|
|
* @param string $name Project name |
63
|
|
|
* @param int $max_length Max length allowed |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getClonedProjectName($name, $max_length = 50) |
68
|
|
|
{ |
69
|
|
|
$suffix = ' ('.t('Clone').')'; |
70
|
|
|
|
71
|
|
|
if (strlen($name.$suffix) > $max_length) { |
72
|
|
|
$name = substr($name, 0, $max_length - strlen($suffix)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $name.$suffix; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Clone a project with all settings. |
80
|
|
|
* |
81
|
|
|
* @param int $src_project_id Project Id |
82
|
|
|
* @param array $selection Selection of optional project parts to duplicate |
83
|
|
|
* @param int $owner_id Owner of the project |
84
|
|
|
* @param string $name Name of the project |
85
|
|
|
* @param bool $private Force the project to be private |
86
|
|
|
* |
87
|
|
|
* @return int Cloned Project Id |
88
|
|
|
*/ |
89
|
|
|
public function duplicate($src_project_id, $selection = ['projectPermissionModel', 'categoryModel', 'actionModel'], $owner_id = 0, $name = null, $private = null) |
90
|
|
|
{ |
91
|
|
|
$this->db->startTransaction(); |
|
|
|
|
92
|
|
|
|
93
|
|
|
// Get the cloned project Id |
94
|
|
|
$dst_project_id = $this->copy($src_project_id, $owner_id, $name, $private); |
95
|
|
|
|
96
|
|
|
if ($dst_project_id === false) { |
97
|
|
|
$this->db->cancelTransaction(); |
|
|
|
|
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Clone Columns, Categories, Permissions and Actions |
103
|
|
|
foreach ($this->getPossibleSelection() as $model) { |
104
|
|
|
|
105
|
|
|
// Skip if optional part has not been selected |
106
|
|
|
if (in_array($model, $this->getOptionalSelection()) && !in_array($model, $selection)) { |
107
|
|
|
continue; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Skip permissions for private projects |
111
|
|
|
if ($private && $model === 'projectPermissionModel') { |
112
|
|
|
continue; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (!$this->$model->duplicate($src_project_id, $dst_project_id)) { |
116
|
|
|
$this->db->cancelTransaction(); |
|
|
|
|
117
|
|
|
|
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (!$this->makeOwnerManager($dst_project_id, $owner_id)) { |
123
|
|
|
$this->db->cancelTransaction(); |
|
|
|
|
124
|
|
|
|
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->db->closeTransaction(); |
|
|
|
|
129
|
|
|
|
130
|
|
|
return (int) $dst_project_id; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Create a project from another one. |
135
|
|
|
* |
136
|
|
|
* @param int $src_project_id |
137
|
|
|
* @param int $owner_id |
138
|
|
|
* @param string $name |
139
|
|
|
* @param bool $private |
140
|
|
|
* |
141
|
|
|
* @return int |
142
|
|
|
*/ |
143
|
|
|
private function copy($src_project_id, $owner_id = 0, $name = null, $private = null) |
144
|
|
|
{ |
145
|
|
|
$project = $this->projectModel->getById($src_project_id); |
|
|
|
|
146
|
|
|
$is_private = empty($project['is_private']) ? 0 : 1; |
147
|
|
|
|
148
|
|
|
$values = [ |
149
|
|
|
'name' => $name ?: $this->getClonedProjectName($project['name']), |
150
|
|
|
'is_active' => 1, |
151
|
|
|
'last_modified' => time(), |
152
|
|
|
'token' => '', |
153
|
|
|
'is_public' => 0, |
154
|
|
|
'is_private' => $private ? 1 : $is_private, |
155
|
|
|
'owner_id' => $owner_id, |
156
|
|
|
'priority_default' => $project['priority_default'], |
157
|
|
|
'priority_start' => $project['priority_start'], |
158
|
|
|
'priority_end' => $project['priority_end'], |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
if (!$this->db->table(ProjectModel::TABLE)->save($values)) { |
|
|
|
|
162
|
|
|
return false; |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $this->db->getLastId(); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Make sure that the creator of the duplicated project is also owner. |
170
|
|
|
* |
171
|
|
|
* @param int $dst_project_id |
172
|
|
|
* @param int $owner_id |
173
|
|
|
* |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
private function makeOwnerManager($dst_project_id, $owner_id) |
177
|
|
|
{ |
178
|
|
|
if ($owner_id > 0) { |
179
|
|
|
$this->projectUserRoleModel->removeUser($dst_project_id, $owner_id); |
|
|
|
|
180
|
|
|
|
181
|
|
|
if (!$this->projectUserRoleModel->addUser($dst_project_id, $owner_id, Role::PROJECT_MANAGER)) { |
|
|
|
|
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return true; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.