Import::validateFileImport()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 3
eloc 13
nc 3
nop 0
1
<?php
2
3
/**
4
 * @package Importer
5
 * @author Iurii Makukh
6
 * @copyright Copyright (c) 2017, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\import\controllers;
11
12
use gplcart\core\controllers\backend\Controller;
13
use gplcart\core\models\FileTransfer;
14
use gplcart\modules\import\helpers\Csv;
15
16
/**
17
 * Handles incoming requests and outputs data related to Import module
18
 */
19
class Import extends Controller
20
{
21
22
    /**
23
     * File transfer model class instance
24
     * @var \gplcart\core\models\FileTransfer $file_transfer
25
     */
26
    protected $file_transfer;
27
28
    /**
29
     * CSV helper class
30
     * @var \gplcart\modules\import\helpers\Csv $csv
31
     */
32
    protected $csv;
33
34
    /**
35
     * @param FileTransfer $file_transfer
36
     * @param Csv $csv
37
     */
38
    public function __construct(FileTransfer $file_transfer, Csv $csv)
39
    {
40
        parent::__construct();
41
42
        $this->csv = $csv;
43
        $this->file_transfer = $file_transfer;
44
    }
45
46
    /**
47
     * Displays the import page
48
     */
49
    public function doImport()
50
    {
51
        $this->downloadErrorsImport();
52
53
        $settings = $this->module->getSettings('import');
54
        $this->setData('settings', $settings);
55
56
        unset($settings['header']['product_id']);
57
        $this->setData('columns', $settings['header']);
58
59
        $this->submitImport();
60
        $this->setTitleDoImport();
61
        $this->setBreadcrumbDoImport();
62
63
        $this->outputDoImport();
64
    }
65
66
    /**
67
     * Sets titles on the import page
68
     */
69
    protected function setTitleDoImport()
70
    {
71
        $this->setTitle($this->text('Import'));
72
    }
73
74
    /**
75
     * Sets breadcrumbs on the import page
76
     */
77
    protected function setBreadcrumbDoImport()
78
    {
79
        $breadcrumb = array(
80
            'text' => $this->text('Dashboard'),
81
            'url' => $this->url('admin')
82
        );
83
84
        $this->setBreadcrumb($breadcrumb);
85
    }
86
87
    /**
88
     * Renders the import page
89
     */
90
    protected function outputDoImport()
91
    {
92
        $this->output('import|import');
93
    }
94
95
    /**
96
     * Downloads an error log file
97
     */
98
    protected function downloadErrorsImport()
99
    {
100
        $file = gplcart_file_private_temp('import_module_errors.csv');
101
102
        if ($this->isQuery('download_errors') && is_file($file)) {
103
            $this->download($file);
104
        }
105
    }
106
107
    /**
108
     * Start import
109
     */
110
    protected function submitImport()
111
    {
112
        if ($this->isPosted('import') && $this->validateImport()) {
113
            $this->setJobImport();
114
        }
115
    }
116
117
    /**
118
     * Validates submitted import data
119
     * @return boolean
120
     */
121
    protected function validateImport()
122
    {
123
        $this->setSubmitted('settings');
124
        $this->validateFileImport();
125
        $this->validateHeaderImport();
126
127
        return !$this->hasErrors();
128
    }
129
130
    /**
131
     * Validate uploaded CSV file
132
     * @return boolean
133
     */
134
    protected function validateFileImport()
135
    {
136
        $file = $this->request->file('file');
137
138
        if (empty($file)) {
139
            $this->setError('file', $this->text('File is required'));
140
            return false;
141
        }
142
143
        $result = $this->file_transfer->upload($file, 'csv', gplcart_file_private_module('import'));
144
145
        if ($result !== true) {
146
            $this->setError('file', $result);
147
            return false;
148
        }
149
150
        $uploaded = $this->file_transfer->getTransferred();
151
152
        $this->setSubmitted('filepath', $uploaded);
153
        $this->setSubmitted('filesize', filesize($uploaded));
154
        return true;
155
    }
156
157
    /**
158
     * Validates CSV header
159
     */
160
    public function validateHeaderImport()
161
    {
162
        if (!$this->isError()) {
163
164
            $header = $this->module->getSettings('import', 'header');
165
            $delimiter = $this->module->getSettings('import', 'delimiter');
166
167
            $real_header = $this->csv->open($this->getSubmitted('filepath'))
168
                ->setHeader($header)
169
                ->setDelimiter($delimiter)
170
                ->getHeader();
171
172
            if ($header != $real_header) {
173
                $error = $this->text('Wrong header. Required columns: @format', array('@format' => implode(' | ', $header)));
174
                $this->setError('file', $error);
175
            }
176
        }
177
    }
178
179
    /**
180
     * Sets up import
181
     */
182
    protected function setJobImport()
183
    {
184
        $submitted = $this->getSubmitted();
185
186
        $settings = $this->module->getSettings('import');
187
        $settings['mode'] = $submitted['mode'];
188
        $settings['update'] = $submitted['update'];
189
190
        $this->module->setSettings('import', $settings);
191
192
        $job = array(
193
            'id' => 'import_product',
194
            'total' => $submitted['filesize'],
195
            'data' => array_merge($settings, $submitted),
196
            'redirect_message' => array(
197
                'finish' => 'Success. Inserted: %inserted, updated: %updated',
198
                'errors' => $this->text('Inserted: %inserted, updated: %updated, errors: %errors. <a href="@url">See error log</a>', array(
199
                    '@url' => $this->url('', array('download_errors' => true))))
200
            ),
201
            'log' => array(
202
                'errors' => gplcart_file_private_temp('import_module_errors.csv')
203
            )
204
        );
205
206
        $this->job->submit($job);
207
    }
208
209
}
210