From e515f37435db6cf883215f13f02391d8b2107d47 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Mon, 12 Oct 2015 14:44:28 -0400 Subject: Add user CSV import --- app/Controller/UserImport.php | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 app/Controller/UserImport.php (limited to 'app/Controller') diff --git a/app/Controller/UserImport.php b/app/Controller/UserImport.php new file mode 100644 index 00000000..e31ddbbb --- /dev/null +++ b/app/Controller/UserImport.php @@ -0,0 +1,67 @@ +response->html($this->template->layout('user_import/step1', array( + 'values' => $values, + 'errors' => $errors, + 'max_size' => ini_get('upload_max_filesize'), + 'delimiters' => Csv::getDelimiters(), + 'enclosures' => Csv::getEnclosures(), + 'title' => t('Import users from CSV file'), + ))); + } + + /** + * Process CSV file + * + */ + public function step2() + { + $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')))); + } + + $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->session->flash(t('%d user(s) have been imported successfully.', $this->userImport->counter)); + } + else { + $this->session->flash(t('Nothing have been imported!')); + } + + $this->response->redirect($this->helper->url->to('userImport', 'step1')); + } + + /** + * Generate template + * + */ + public function template() + { + $this->response->forceDownload('users.csv'); + $this->response->csv(array($this->userImport->getColumnMapping())); + } +} -- cgit v1.2.3