diff options
-rw-r--r-- | ChangeLog | 2 | ||||
-rw-r--r-- | app/Controller/TaskImport.php | 73 | ||||
-rw-r--r-- | app/Controller/UserImport.php | 2 | ||||
-rw-r--r-- | app/Model/Acl.php | 1 | ||||
-rw-r--r-- | app/Model/TaskImport.php | 158 | ||||
-rw-r--r-- | app/Model/TaskValidator.php | 1 | ||||
-rw-r--r-- | app/Model/User.php | 12 | ||||
-rw-r--r-- | app/Model/UserImport.php | 2 | ||||
-rw-r--r-- | app/ServiceProvider/ClassProvider.php | 1 | ||||
-rw-r--r-- | app/Template/project/sidebar.php | 3 | ||||
-rw-r--r-- | app/Template/task_import/step1.php | 34 |
11 files changed, 286 insertions, 3 deletions
@@ -3,7 +3,7 @@ Version 1.0.20 (unreleased) New features: -* Add users CSV import +* Add CSV import for users and tasks Improvements: 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')); diff --git a/app/Model/Acl.php b/app/Model/Acl.php index d05e4f77..35488c63 100644 --- a/app/Model/Acl.php +++ b/app/Model/Acl.php @@ -63,6 +63,7 @@ class Acl extends Base 'category' => '*', 'column' => '*', 'export' => '*', + 'taskimport' => '*', 'project' => array('edit', 'update', 'share', 'integration', 'users', 'alloweverybody', 'allow', 'setowner', 'revoke', 'duplicate', 'disable', 'enable'), 'swimlane' => '*', 'gantt' => array('project', 'savetaskdate', 'task', 'savetask'), diff --git a/app/Model/TaskImport.php b/app/Model/TaskImport.php new file mode 100644 index 00000000..2eb4eb8f --- /dev/null +++ b/app/Model/TaskImport.php @@ -0,0 +1,158 @@ +<?php + +namespace Model; + +use Core\Csv; +use SimpleValidator\Validator; +use SimpleValidator\Validators; + +/** + * Task Import + * + * @package model + * @author Frederic Guillot + */ +class TaskImport extends Base +{ + /** + * Number of successful import + * + * @access public + * @var integer + */ + public $counter = 0; + + /** + * Project id to import tasks + * + * @access public + * @var integer + */ + public $projectId; + + /** + * Get mapping between CSV header and SQL columns + * + * @access public + * @return array + */ + public function getColumnMapping() + { + return array( + 'reference' => 'Reference', + 'title' => 'Title', + 'description' => 'Description', + 'assignee' => 'Assignee Username', + 'creator' => 'Creator Username', + 'color' => 'Color Name', + 'column' => 'Column Name', + 'category' => 'Category Name', + 'swimlane' => 'Swimlane Name', + 'score' => 'Complexity', + 'time_estimated' => 'Time Estimated', + 'time_spent' => 'Time Spent', + 'date_due' => 'Due Date', + 'is_active' => 'Closed', + ); + } + + /** + * Import a single row + * + * @access public + * @param array $row + * @param integer $line_number + */ + public function import(array $row, $line_number) + { + $row = $this->prepare($row); + + if ($this->validateCreation($row)) { + if ($this->taskCreation->create($row) > 0) { + $this->logger->debug('TaskImport: imported successfully line '.$line_number); + $this->counter++; + } + else { + $this->logger->error('TaskImport: creation error at line '.$line_number); + } + } + else { + $this->logger->error('TaskImport: validation error at line '.$line_number); + } + } + + /** + * Format row before validation + * + * @access public + * @param array $data + * @return array + */ + public function prepare(array $row) + { + $values = array(); + $values['project_id'] = $this->projectId; + $values['reference'] = $row['reference']; + $values['title'] = $row['title']; + $values['description'] = $row['description']; + $values['is_active'] = Csv::getBooleanValue($row['is_active']) == 1 ? 0 : 1; + $values['score'] = (int) $row['score']; + $values['time_estimated'] = (float) $row['time_estimated']; + $values['time_spent'] = (float) $row['time_spent']; + + if (! empty($row['assignee'])) { + $values['owner_id'] = $this->user->getIdByUsername($row['assignee']); + } + + if (! empty($row['creator'])) { + $values['creator_id'] = $this->user->getIdByUsername($row['creator']); + } + + if (! empty($row['color'])) { + $values['color_id'] = $this->color->find($row['color']); + } + + if (! empty($row['column'])) { + $values['column_id'] = $this->board->getColumnIdByTitle($this->projectId, $row['column']); + } + + if (! empty($row['category'])) { + $values['category_id'] = $this->category->getIdByName($this->projectId, $row['category']); + } + + if (! empty($row['swimlane'])) { + $values['swimlane_id'] = $this->swimlane->getIdByName($this->projectId, $row['swimlane']); + } + + if (! empty($row['date_due'])) { + $values['date_due'] = $this->dateParser->getTimestampFromIsoFormat($row['date_due']); + } + + $this->removeEmptyFields( + $values, + array('owner_id', 'creator_id', 'color_id', 'column_id', 'category_id', 'swimlane_id', 'date_due') + ); + + return $values; + } + + /** + * Validate user creation + * + * @access public + * @param array $values + * @return boolean + */ + public function validateCreation(array $values) + { + $v = new Validator($values, array( + new Validators\Integer('project_id', t('This value must be an integer')), + new Validators\Required('project_id', t('The project is required')), + new Validators\Required('title', t('The title is required')), + new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200), + new Validators\MaxLength('reference', t('The maximum length is %d characters', 50), 50), + )); + + return $v->execute(); + } +} diff --git a/app/Model/TaskValidator.php b/app/Model/TaskValidator.php index 95b8a26c..89c66f2f 100644 --- a/app/Model/TaskValidator.php +++ b/app/Model/TaskValidator.php @@ -38,6 +38,7 @@ class TaskValidator extends Base new Validators\Integer('recurrence_trigger', t('This value must be an integer')), new Validators\Integer('recurrence_status', t('This value must be an integer')), new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200), + new Validators\MaxLength('reference', t('The maximum length is %d characters', 50), 50), new Validators\Date('date_due', t('Invalid date'), $this->dateParser->getDateFormats()), new Validators\Date('date_started', t('Invalid date'), $this->dateParser->getAllFormats()), new Validators\Numeric('time_spent', t('This value must be numeric')), diff --git a/app/Model/User.php b/app/Model/User.php index fd2ec954..2b6436f8 100644 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -167,6 +167,18 @@ class User extends Base } /** + * Get user_id by username + * + * @access public + * @param string $username Username + * @return array + */ + public function getIdByUsername($username) + { + return $this->db->table(self::TABLE)->eq('username', $username)->findOneColumn('id'); + } + + /** * Get a specific user by the email address * * @access public diff --git a/app/Model/UserImport.php b/app/Model/UserImport.php index afae0a48..3d7c0feb 100644 --- a/app/Model/UserImport.php +++ b/app/Model/UserImport.php @@ -78,7 +78,7 @@ class UserImport extends Base $row['username'] = strtolower($row['username']); foreach (array('is_admin', 'is_project_admin', 'is_ldap_user') as $field) { - $row[$field] = csv::getBooleanValue($row[$field]); + $row[$field] = Csv::getBooleanValue($row[$field]); } $this->removeEmptyFields($row, array('password', 'email', 'name')); diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index ac8fa750..2db0ec54 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -63,6 +63,7 @@ class ClassProvider implements ServiceProviderInterface 'TaskPosition', 'TaskStatus', 'TaskValidator', + 'TaskImport', 'Transition', 'User', 'UserImport', diff --git a/app/Template/project/sidebar.php b/app/Template/project/sidebar.php index d8b35e3b..971ed950 100644 --- a/app/Template/project/sidebar.php +++ b/app/Template/project/sidebar.php @@ -45,6 +45,9 @@ <?= $this->url->link(t('Enable'), 'project', 'enable', array('project_id' => $project['id']), true) ?> <?php endif ?> </li> + <li <?= $this->app->getRouterController() === 'taskImport' && $this->app->getRouterAction() === 'step1' ? 'class="active"' : '' ?>> + <?= $this->url->link(t('Import'), 'taskImport', 'step1', array('project_id' => $project['id'])) ?> + </li> <?php if ($this->user->isProjectAdministrationAllowed($project['id'])): ?> <li <?= $this->app->getRouterController() === 'project' && $this->app->getRouterAction() === 'remove' ? 'class="active"' : '' ?>> <?= $this->url->link(t('Remove'), 'project', 'remove', array('project_id' => $project['id'])) ?> diff --git a/app/Template/task_import/step1.php b/app/Template/task_import/step1.php new file mode 100644 index 00000000..7619216a --- /dev/null +++ b/app/Template/task_import/step1.php @@ -0,0 +1,34 @@ +<div class="page-header"> + <h2><?= t('Tasks Importation') ?></h2> +</div> +<form action="<?= $this->url->href('taskImport', 'step2', array('project_id' => $project['id'])) ?>" method="post" enctype="multipart/form-data"> + <?= $this->form->csrf() ?> + + <?= $this->form->label(t('Delimiter'), 'delimiter') ?> + <?= $this->form->select('delimiter', $delimiters, $values) ?> + + <?= $this->form->label(t('Enclosure'), 'enclosure') ?> + <?= $this->form->select('enclosure', $enclosures, $values) ?> + + <?= $this->form->label(t('CSV File'), 'file') ?> + <?= $this->form->file('file', $errors) ?> + + <p class="form-help"><?= t('Maximum size: ') ?><?= is_integer($max_size) ? $this->text->bytes($max_size) : $max_size ?></p> + + <div class="form-actions"> + <input type="submit" value="<?= t('Import') ?>" class="btn btn-blue"> + </div> +</form> +<div class="page-header"> + <h2><?= t('Instructions') ?></h2> +</div> +<div class="alert"> + <ul> + <li><?= t('Your file must use the predefined CSV format') ?></li> + <li><?= t('Your file must be encoded in UTF-8') ?></li> + <li><?= t('The first row must be the header') ?></li> + <li><?= t('Duplicates are not verified for you') ?></li> + <li><?= t('The due date must use the ISO format: YYYY-MM-DD') ?></li> + </ul> +</div> +<p><i class="fa fa-download fa-fw"></i><?= $this->url->link(t('Download CSV template'), 'taskImport', 'template', array('project_id' => $project['id'])) ?></p>
\ No newline at end of file |