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
|
<?php
namespace Kanboard\Controller;
use Kanboard\Core\Csv;
use Kanboard\Import\TaskImport;
/**
* Task Import controller
*
* @package Kanboard\Controller
* @author Frederic Guillot
*/
class TaskImportController extends BaseController
{
/**
* Upload the file and ask settings
*
* @param array $values
* @param array $errors
* @throws \Kanboard\Core\Controller\PageNotFoundException
*/
public function show(array $values = array(), array $errors = array())
{
$project = $this->getProject();
$this->response->html($this->template->render('task_import/show', array(
'project' => $project,
'values' => $values,
'errors' => $errors,
'max_size' => get_upload_max_size(),
'delimiters' => Csv::getDelimiters(),
'enclosures' => Csv::getEnclosures(),
)));
}
/**
* Process CSV file
*/
public function save()
{
$project = $this->getProject();
$values = $this->request->getValues();
$filename = $this->request->getFilePath('file');
if (! file_exists($filename)) {
$this->show($values, array('file' => array(t('Unable to read your file'))));
} else {
$taskImport = new TaskImport($this->container);
$taskImport->setProjectId($project['id']);
$csv = new Csv($values['delimiter'], $values['enclosure']);
$csv->setColumnMapping($taskImport->getColumnMapping());
$csv->read($filename, array($taskImport, 'importTask'));
if ($taskImport->getNumberOfImportedTasks() > 0) {
$this->flash->success(t('%d task(s) have been imported successfully.', $taskImport->getNumberOfImportedTasks()));
} else {
$this->flash->failure(t('Nothing have been imported!'));
}
$this->response->redirect($this->helper->url->to('TaskImportController', 'show', array('project_id' => $project['id'])), true);
}
}
/**
* Generate template
*
*/
public function template()
{
$taskImport = new TaskImport($this->container);
$this->response->withFileDownload('tasks.csv');
$this->response->csv(array($taskImport->getColumnMapping()));
}
}
|