diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-10-12 17:49:30 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-10-12 17:49:30 -0400 |
commit | 7d458cd5004d026dec0c41bbb1daacb259a52cfb (patch) | |
tree | 5afa8d858145a3507a72d0e323a552aa97ba671e /app/Controller | |
parent | e515f37435db6cf883215f13f02391d8b2107d47 (diff) |
Add CSV import for tasks
Diffstat (limited to 'app/Controller')
-rw-r--r-- | app/Controller/TaskImport.php | 73 | ||||
-rw-r--r-- | app/Controller/UserImport.php | 2 |
2 files changed, 74 insertions, 1 deletions
diff --git a/app/Controller/TaskImport.php b/app/Controller/TaskImport.php new file mode 100644 index 00000000..0343b1dc --- /dev/null +++ b/app/Controller/TaskImport.php @@ -0,0 +1,73 @@ +<?php + +namespace Controller; + +use Core\Csv; + +/** + * Task Import controller + * + * @package controller + * @author Frederic Guillot + */ +class TaskImport extends Base +{ + /** + * Upload the file and ask settings + * + */ + public function step1(array $values = array(), array $errors = array()) + { + $project = $this->getProject(); + + $this->response->html($this->projectLayout('task_import/step1', array( + 'project' => $project, + 'values' => $values, + 'errors' => $errors, + 'max_size' => ini_get('upload_max_filesize'), + 'delimiters' => Csv::getDelimiters(), + 'enclosures' => Csv::getEnclosures(), + 'title' => t('Import tasks from CSV file'), + ))); + } + + /** + * Process CSV file + * + */ + public function step2() + { + $project = $this->getProject(); + $values = $this->request->getValues(); + $filename = $this->request->getFilePath('file'); + + if (! file_exists($filename)) { + $this->step1($values, array('file' => array(t('Unable to read your file')))); + } + + $this->taskImport->projectId = $project['id']; + + $csv = new Csv($values['delimiter'], $values['enclosure']); + $csv->setColumnMapping($this->taskImport->getColumnMapping()); + $csv->read($filename, array($this->taskImport, 'import')); + + if ($this->taskImport->counter > 0) { + $this->session->flash(t('%d task(s) have been imported successfully.', $this->taskImport->counter)); + } + else { + $this->session->flashError(t('Nothing have been imported!')); + } + + $this->response->redirect($this->helper->url->to('taskImport', 'step1', array('project_id' => $project['id']))); + } + + /** + * Generate template + * + */ + public function template() + { + $this->response->forceDownload('tasks.csv'); + $this->response->csv(array($this->taskImport->getColumnMapping())); + } +} diff --git a/app/Controller/UserImport.php b/app/Controller/UserImport.php index e31ddbbb..9c27aa06 100644 --- a/app/Controller/UserImport.php +++ b/app/Controller/UserImport.php @@ -49,7 +49,7 @@ class UserImport extends Base $this->session->flash(t('%d user(s) have been imported successfully.', $this->userImport->counter)); } else { - $this->session->flash(t('Nothing have been imported!')); + $this->session->flashError(t('Nothing have been imported!')); } $this->response->redirect($this->helper->url->to('userImport', 'step1')); |