1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
<?php
namespace Kanboard\Import;
use Kanboard\Core\Base;
use Kanboard\Core\Csv;
use Kanboard\Core\ExternalLink\ExternalLinkManager;
use Kanboard\Core\ExternalLink\ExternalLinkProviderNotFound;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
/**
* Task CSV Import
*
* @package Kanboard\Import
* @author Frederic Guillot
*/
class TaskImport extends Base
{
protected $nbImportedTasks = 0;
protected $projectId = 0;
public function setProjectId($projectId)
{
$this->projectId = $projectId;
return $this;
}
public function getNumberOfImportedTasks()
{
return $this->nbImportedTasks;
}
public function getColumnMapping()
{
return array(
'reference' => e('Reference'),
'title' => e('Title'),
'description' => e('Description'),
'assignee' => e('Assignee Username'),
'creator' => e('Creator Username'),
'color' => e('Color Name'),
'column' => e('Column Name'),
'category' => e('Category Name'),
'swimlane' => e('Swimlane Name'),
'score' => e('Complexity'),
'time_estimated' => e('Time Estimated'),
'time_spent' => e('Time Spent'),
'date_started' => e('Start Date'),
'date_due' => e('Due Date'),
'priority' => e('Priority'),
'is_active' => e('Status'),
'tags' => e('Tags'),
'external_link' => e('External Link'),
);
}
public function importTask(array $row, $lineNumber)
{
$task = $this->prepareTask($row);
if ($this->validateCreation($task)) {
$taskId = $this->taskCreationModel->create($task);
if ($taskId > 0) {
$this->logger->debug(__METHOD__.': imported successfully line '.$lineNumber);
$this->nbImportedTasks++;
if (! empty($row['tags'])) {
$this->taskTagModel->save($this->projectId, $taskId, explode_csv_field($row['tags']));
}
if (! empty($row['external_link'])) {
$this->createExternalLink($taskId, $row['external_link']);
}
} else {
$this->logger->error(__METHOD__.': creation error at line '.$lineNumber);
}
} else {
$this->logger->error(__METHOD__.': validation error at line '.$lineNumber);
}
}
public function prepareTask(array $row)
{
$values = array();
$values['project_id'] = $this->projectId;
$values['reference'] = $row['reference'];
$values['title'] = $row['title'];
$values['description'] = $row['description'];
$values['is_active'] = Csv::getBooleanValue($row['is_active']) == 1 ? 0 : 1;
$values['score'] = (int) $row['score'];
$values['priority'] = (int) $row['priority'];
$values['time_estimated'] = (float) $row['time_estimated'];
$values['time_spent'] = (float) $row['time_spent'];
if (! empty($row['assignee'])) {
$values['owner_id'] = $this->userModel->getIdByUsername($row['assignee']);
}
if (! empty($row['creator'])) {
$values['creator_id'] = $this->userModel->getIdByUsername($row['creator']);
}
if (! empty($row['color'])) {
$values['color_id'] = $this->colorModel->find($row['color']);
}
if (! empty($row['column'])) {
$values['column_id'] = $this->columnModel->getColumnIdByTitle($this->projectId, $row['column']);
}
if (! empty($row['category'])) {
$values['category_id'] = $this->categoryModel->getIdByName($this->projectId, $row['category']);
}
if (! empty($row['swimlane'])) {
$values['swimlane_id'] = $this->swimlaneModel->getIdByName($this->projectId, $row['swimlane']);
}
if (! empty($row['date_due'])) {
$values['date_due'] = $this->dateParser->getTimestamp($row['date_due']);
}
if (! empty($row['date_started'])) {
$values['date_started'] = $this->dateParser->getTimestamp($row['date_started']);
}
$this->helper->model->removeEmptyFields(
$values,
array('owner_id', 'creator_id', 'color_id', 'column_id', 'category_id', 'swimlane_id', 'date_due', 'date_started', 'priority')
);
return $values;
}
protected function validateCreation(array $values)
{
$v = new Validator($values, array(
new Validators\Integer('project_id', t('This value must be an integer')),
new Validators\Required('project_id', t('The project is required')),
new Validators\Required('title', t('The title is required')),
new Validators\MaxLength('title', t('The maximum length is %d characters', 65535), 65535),
new Validators\MaxLength('reference', t('The maximum length is %d characters', 255), 255),
));
return $v->execute();
}
protected function createExternalLink($taskId, $externalLink)
{
try {
$provider = $this->externalLinkManager
->setUserInputText($externalLink)
->setUserInputType(ExternalLinkManager::TYPE_AUTO)
->find();
$link = $provider->getLink();
$dependencies = $provider->getDependencies();
$values = array(
'task_id' => $taskId,
'title' => $link->getTitle() ?: $link->getUrl(),
'url' => $link->getUrl(),
'link_type' => $provider->getType(),
'dependency' => key($dependencies),
);
list($valid, $errors) = $this->externalLinkValidator->validateCreation($values);
if ($valid) {
$this->taskExternalLinkModel->create($values);
} else {
$this->logger->error(__METHOD__.': '.var_export($errors, true));
}
} catch (ExternalLinkProviderNotFound $e) {
$this->logger->error(__METHOD__.': '.$e->getMessage());
}
}
}
|