Add CSV import for tasks

This commit is contained in:
Frederic Guillot
2015-10-12 17:49:30 -04:00
parent e515f37435
commit 7d458cd500
11 changed files with 286 additions and 3 deletions

View File

@@ -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'),

158
app/Model/TaskImport.php Normal file
View File

@@ -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();
}
}

View File

@@ -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')),

View File

@@ -166,6 +166,18 @@ class User extends Base
return $this->db->table(self::TABLE)->eq('username', $username)->findOne();
}
/**
* 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
*

View File

@@ -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'));