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\Import; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
15
|
|
|
use Jitamin\Foundation\Csv; |
16
|
|
|
use SimpleValidator\Validator; |
17
|
|
|
use SimpleValidator\Validators; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Task Import. |
21
|
|
|
*/ |
22
|
|
|
class TaskImport extends Base |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Number of successful import. |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
public $counter = 0; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Project id to import tasks. |
33
|
|
|
* |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
public $projectId; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get mapping between CSV header and SQL columns. |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function getColumnMapping() |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
|
|
'reference' => 'Reference', |
47
|
|
|
'title' => 'Title', |
48
|
|
|
'description' => 'Description', |
49
|
|
|
'assignee' => 'Assignee Username', |
50
|
|
|
'creator' => 'Creator Username', |
51
|
|
|
'color' => 'Color Name', |
52
|
|
|
'column' => 'Column Name', |
53
|
|
|
'category' => 'Category Name', |
54
|
|
|
'swimlane' => 'Swimlane Name', |
55
|
|
|
'score' => 'Complexity', |
56
|
|
|
'time_estimated' => 'Time Estimated', |
57
|
|
|
'time_spent' => 'Time Spent', |
58
|
|
|
'date_due' => 'Due Date', |
59
|
|
|
'is_active' => 'Closed', |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Import a single row. |
65
|
|
|
* |
66
|
|
|
* @param array $row |
67
|
|
|
* @param int $line_number |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function import(array $row, $line_number) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$row = $this->prepare($row); |
72
|
|
|
|
73
|
|
|
if ($this->validateCreation($row)) { |
74
|
|
|
if ($this->taskModel->create($row) > 0) { |
|
|
|
|
75
|
|
|
$this->logger->debug('TaskImport: imported successfully line '.$line_number); |
|
|
|
|
76
|
|
|
$this->counter++; |
77
|
|
|
} else { |
78
|
|
|
$this->logger->error('TaskImport: creation error at line '.$line_number); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
} else { |
81
|
|
|
$this->logger->error('TaskImport: validation error at line '.$line_number); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Format row before validation. |
87
|
|
|
* |
88
|
|
|
* @param array $row |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function prepare(array $row) |
93
|
|
|
{ |
94
|
|
|
$values = []; |
95
|
|
|
$values['project_id'] = $this->projectId; |
96
|
|
|
$values['reference'] = $row['reference']; |
97
|
|
|
$values['title'] = $row['title']; |
98
|
|
|
$values['description'] = $row['description']; |
99
|
|
|
$values['is_active'] = Csv::getBooleanValue($row['is_active']) == 1 ? 0 : 1; |
100
|
|
|
$values['score'] = (int) $row['score']; |
101
|
|
|
$values['time_estimated'] = (float) $row['time_estimated']; |
102
|
|
|
$values['time_spent'] = (float) $row['time_spent']; |
103
|
|
|
|
104
|
|
|
if (!empty($row['assignee'])) { |
105
|
|
|
$values['owner_id'] = $this->userModel->getIdByUsername($row['assignee']); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (!empty($row['creator'])) { |
109
|
|
|
$values['creator_id'] = $this->userModel->getIdByUsername($row['creator']); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (!empty($row['color'])) { |
113
|
|
|
$values['color_id'] = $this->colorModel->find($row['color']); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (!empty($row['column'])) { |
117
|
|
|
$values['column_id'] = $this->columnModel->getColumnIdByTitle($this->projectId, $row['column']); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (!empty($row['category'])) { |
121
|
|
|
$values['category_id'] = $this->categoryModel->getIdByName($this->projectId, $row['category']); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (!empty($row['swimlane'])) { |
125
|
|
|
$values['swimlane_id'] = $this->swimlaneModel->getIdByName($this->projectId, $row['swimlane']); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (!empty($row['date_due'])) { |
129
|
|
|
$values['date_due'] = $this->dateParser->getTimestampFromIsoFormat($row['date_due']); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$this->helper->model->removeEmptyFields( |
|
|
|
|
133
|
|
|
$values, |
134
|
|
|
['owner_id', 'creator_id', 'color_id', 'column_id', 'category_id', 'swimlane_id', 'date_due'] |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
return $values; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Validate user creation. |
142
|
|
|
* |
143
|
|
|
* @param array $values |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
public function validateCreation(array $values) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$v = new Validator($values, [ |
150
|
|
|
new Validators\Integer('project_id', t('This value must be an integer')), |
151
|
|
|
new Validators\Required('project_id', t('The project is required')), |
152
|
|
|
new Validators\Required('title', t('The title is required')), |
153
|
|
|
new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200), |
154
|
|
|
new Validators\MaxLength('reference', t('The maximum length is %d characters', 50), 50), |
155
|
|
|
]); |
156
|
|
|
|
157
|
|
|
return $v->execute(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
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.