summaryrefslogtreecommitdiff
path: root/app/Import/TaskImport.php
blob: f5ca9b0e7e6fd42a2c9a8d39fb0ccad944649ac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php

namespace Kanboard\Import;

use Kanboard\Core\Base;
use Kanboard\Core\Csv;
use SimpleValidator\Validator;
use SimpleValidator\Validators;

/**
 * Task Import
 *
 * @package  import
 * @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->taskCreationModel->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   $row
     * @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->userModel->getIdByUsername($row['assignee']);
        }

        if (! empty($row['creator'])) {
            $values['creator_id'] = $this->userModel->getIdByUsername($row['creator']);
        }

        if (! empty($row['color'])) {
            $values['color_id'] = $this->colorModel->find($row['color']);
        }

        if (! empty($row['column'])) {
            $values['column_id'] = $this->columnModel->getColumnIdByTitle($this->projectId, $row['column']);
        }

        if (! empty($row['category'])) {
            $values['category_id'] = $this->categoryModel->getIdByName($this->projectId, $row['category']);
        }

        if (! empty($row['swimlane'])) {
            $values['swimlane_id'] = $this->swimlaneModel->getIdByName($this->projectId, $row['swimlane']);
        }

        if (! empty($row['date_due'])) {
            $values['date_due'] = $this->dateParser->getTimestampFromIsoFormat($row['date_due']);
        }

        $this->helper->model->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();
    }
}