summaryrefslogtreecommitdiff
path: root/app/Controller
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-10-12 14:44:28 -0400
committerFrederic Guillot <fred@kanboard.net>2015-10-12 14:44:28 -0400
commite515f37435db6cf883215f13f02391d8b2107d47 (patch)
tree3df01bc0c420a2c497302c5a38aa9d724b2e764c /app/Controller
parente57386a18393e85f073cccff70699ad9afc19119 (diff)
Add user CSV import
Diffstat (limited to 'app/Controller')
-rw-r--r--app/Controller/UserImport.php67
1 files changed, 67 insertions, 0 deletions
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 @@
+<?php
+
+namespace Controller;
+
+use Core\Csv;
+
+/**
+ * User Import controller
+ *
+ * @package controller
+ * @author Frederic Guillot
+ */
+class UserImport extends Base
+{
+ /**
+ * Upload the file and ask settings
+ *
+ */
+ public function step1(array $values = array(), array $errors = array())
+ {
+ $this->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()));
+ }
+}