|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
ÁTICA - Aplicación web para la gestión documental de centros educativos |
|
5
|
|
|
|
|
6
|
|
|
Copyright (C) 2015-2017: Luis Ramón López López |
|
7
|
|
|
|
|
8
|
|
|
This program is free software: you can redistribute it and/or modify |
|
9
|
|
|
it under the terms of the GNU Affero General Public License as published by |
|
10
|
|
|
the Free Software Foundation, either version 3 of the License, or |
|
11
|
|
|
(at your option) any later version. |
|
12
|
|
|
|
|
13
|
|
|
This program is distributed in the hope that it will be useful, |
|
14
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16
|
|
|
GNU Affero General Public License for more details. |
|
17
|
|
|
|
|
18
|
|
|
You should have received a copy of the GNU Affero General Public License |
|
19
|
|
|
along with this program. If not, see [http://www.gnu.org/licenses/]. |
|
20
|
|
|
|
|
21
|
|
|
Adapted from: http://php.net/manual/es/function.fgetcsv.php#68213 |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace AppBundle\Utils; |
|
25
|
|
|
|
|
26
|
|
|
class CsvImporter |
|
27
|
|
|
{ |
|
28
|
|
|
private $fp; |
|
29
|
|
|
private $parse_header; |
|
30
|
|
|
private $header; |
|
31
|
|
|
private $delimiter; |
|
32
|
|
|
private $length; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct($fileName, $parseHeader = true, $delimiter = ",", $length = 0) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->fp = fopen($fileName, 'rb'); |
|
37
|
|
|
$this->parse_header = $parseHeader; |
|
38
|
|
|
$this->delimiter = $delimiter; |
|
39
|
|
|
$this->length = $length; |
|
40
|
|
|
|
|
41
|
|
|
if ($this->parse_header) { |
|
42
|
|
|
$this->header = fgetcsv($this->fp, $this->length, $this->delimiter); |
|
43
|
|
|
foreach ($this->header as $key => $data) { |
|
44
|
|
|
$this->header[$key] = iconv('ISO-8859-1', 'UTF-8', $data); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function __destruct() |
|
51
|
|
|
{ |
|
52
|
|
|
if ($this->fp) { |
|
53
|
|
|
fclose($this->fp); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function get($max_lines = 0) |
|
58
|
|
|
{ |
|
59
|
|
|
//if $max_lines is set to 0, then get all the data |
|
60
|
|
|
|
|
61
|
|
|
$data = array(); |
|
62
|
|
|
|
|
63
|
|
|
if ($max_lines > 0) { |
|
64
|
|
|
$line_count = 0; |
|
65
|
|
|
} else { |
|
66
|
|
|
$line_count = -1; // so loop limit is ignored |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
while ($line_count < $max_lines && ($row = fgetcsv($this->fp, $this->length, $this->delimiter)) !== FALSE) |
|
70
|
|
|
{ |
|
71
|
|
|
foreach ($row as $key => $key_data) { |
|
72
|
|
|
$row[$key] = iconv('ISO-8859-1', 'UTF-8', $key_data); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ($this->parse_header) { |
|
76
|
|
|
$row_new = array(); |
|
77
|
|
|
foreach ($this->header as $i => $heading_i) { |
|
78
|
|
|
$row_new[$heading_i] = $row[$i]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$data[] = $row_new; |
|
82
|
|
|
} else { |
|
83
|
|
|
$data[] = $row; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ($max_lines > 0) { |
|
87
|
|
|
$line_count++; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
return $data; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|