From 20052c7dd295464c7782350628701675b1f07db7 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 15 May 2016 21:20:46 -0400 Subject: User import is now a popover --- app/Controller/UserImportController.php | 77 +++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 app/Controller/UserImportController.php (limited to 'app/Controller/UserImportController.php') diff --git a/app/Controller/UserImportController.php b/app/Controller/UserImportController.php new file mode 100644 index 00000000..b3cb52d1 --- /dev/null +++ b/app/Controller/UserImportController.php @@ -0,0 +1,77 @@ +response->html($this->template->render('user_import/show', array( + 'values' => $values, + 'errors' => $errors, + 'max_size' => ini_get('upload_max_filesize'), + 'delimiters' => Csv::getDelimiters(), + 'enclosures' => Csv::getEnclosures(), + ))); + } + + /** + * Submit form + */ + public function save() + { + $values = $this->request->getValues(); + $filename = $this->request->getFilePath('file'); + + if (! file_exists($filename)) { + $this->flash->failure(t('Unable to read your file')); + } else { + $this->importFile($values, $filename); + } + + $this->response->redirect($this->helper->url->to('user', 'index')); + } + + /** + * Generate template + * + */ + public function template() + { + $this->response->withFileDownload('users.csv'); + $this->response->csv(array($this->userImport->getColumnMapping())); + } + + /** + * Process file + * + * @param array $values + * @param $filename + */ + private function importFile(array $values, $filename) + { + $csv = new Csv($values['delimiter'], $values['enclosure']); + $csv->setColumnMapping($this->userImport->getColumnMapping()); + $csv->read($filename, array($this->userImport, 'import')); + + if ($this->userImport->counter > 0) { + $this->flash->success(t('%d user(s) have been imported successfully.', $this->userImport->counter)); + } else { + $this->flash->failure(t('Nothing have been imported!')); + } + } +} -- cgit v1.2.3