|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CsvCombine\Form; |
|
4
|
|
|
|
|
5
|
|
|
use Cake\Form\Form; |
|
6
|
|
|
use Cake\Validation\Validator; |
|
7
|
|
|
use Cake\Utility\Hash; |
|
8
|
|
|
|
|
9
|
|
|
class CsvImportForm extends Form |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
protected function _buildValidator(Validator $validator) |
|
14
|
|
|
{ |
|
15
|
|
|
return $validator; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/* |
|
19
|
|
|
* loadDataCsv CSV読み込みアクション |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $fileName 固定長テキストファイ |
|
22
|
|
|
* @param array $column_list 各カラム情報(name:カラム名,length:バイト数) |
|
23
|
|
|
* @param string $delimiter 区切り文字(デフォルトは「,」) |
|
24
|
|
|
* @param string $array_encoding 出力するする配列のエンコード(デフォルトはUTF-8 |
|
25
|
|
|
* @param string $import_encoding 入力するテキストのエンコード(デフォルトはSJIS-win |
|
26
|
|
|
*/ |
|
27
|
|
|
public function loadDataCsv($fileName, $column_list = array(), $delimiter = ",", $array_encoding = 'utf8',$import_encoding = 'sjis-win') |
|
28
|
|
|
{ |
|
29
|
|
|
//保存をするのでモデルを読み込み |
|
30
|
|
|
try { |
|
31
|
|
|
$data = array(); |
|
32
|
|
|
$csvData = array(); |
|
33
|
|
|
$file = fopen($fileName,"r"); |
|
34
|
|
|
while($data = $this->fgetcsv_reg($file,65536,$delimiter)){//CSVファイルを","区切りで配列に |
|
35
|
|
|
mb_convert_variables($array_encoding,$import_encoding,$data); |
|
36
|
|
|
$csvData[] = $data; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$i = 0; |
|
40
|
|
|
foreach ($csvData as $line) { |
|
41
|
|
|
$this_data = array(); |
|
42
|
|
|
if (empty($column_list)) { |
|
43
|
|
|
$this_column_list = array(); |
|
44
|
|
|
$line_count = 0; |
|
45
|
|
|
foreach ($line as $line_v) { |
|
46
|
|
|
$this_column_list[] = $line_count; |
|
47
|
|
|
$line_count++; |
|
48
|
|
|
} |
|
49
|
|
|
} else { |
|
50
|
|
|
$this_column_list = $column_list; |
|
51
|
|
|
} |
|
52
|
|
|
foreach ($this_column_list as $k => $v) { |
|
53
|
|
|
if (isset($line[$k])) { |
|
54
|
|
|
//先頭と末尾の"を削除 |
|
55
|
|
|
$b = $line[$k]; |
|
56
|
|
|
//カラムの数だけセット |
|
57
|
|
|
$this_data = Hash::merge( |
|
58
|
|
|
$this_data, |
|
59
|
|
|
array($v => $b) |
|
60
|
|
|
); |
|
61
|
|
|
} else { |
|
62
|
|
|
$this_data = Hash::merge( |
|
63
|
|
|
$this_data, |
|
64
|
|
|
array($v => '') |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$data[$i] = $this_data; |
|
70
|
|
|
$i++; |
|
71
|
|
|
} |
|
72
|
|
|
} catch (\Exception $e) { |
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $data; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* fgetcsv_reg |
|
81
|
|
|
* |
|
82
|
|
|
* this is a port of the original code written by yossy. |
|
83
|
|
|
* |
|
84
|
|
|
* @author yossy |
|
85
|
|
|
* @author hagiwara |
|
86
|
|
|
* |
|
87
|
|
|
* @param resource $handle |
|
88
|
|
|
* @param integer $length |
|
89
|
|
|
* @param string $d |
|
90
|
|
|
* @param string $e |
|
91
|
|
|
* @see http://yossy.iimp.jp/wp/?p=56 |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
private function fgetcsv_reg (&$handle, $length = null, $d = ',', $e = '"') |
|
95
|
|
|
{ |
|
96
|
|
|
$d = preg_quote($d); |
|
97
|
|
|
$e = preg_quote($e); |
|
98
|
|
|
$_line = ""; |
|
99
|
|
|
$eof = false; // Added for PHP Warning. |
|
100
|
|
|
while ( $eof != true ) { |
|
|
|
|
|
|
101
|
|
|
$_line .= (empty($length) ? fgets($handle) : fgets($handle, $length)); |
|
102
|
|
|
$itemcnt = preg_match_all('/'.$e.'/', $_line, $dummy); |
|
103
|
|
|
if ($itemcnt % 2 == 0) $eof = true; |
|
104
|
|
|
} |
|
105
|
|
|
$_csv_line = preg_replace('/(?:\\r\\n|[\\r\\n])?$/', $d, trim($_line)); |
|
106
|
|
|
$_csv_pattern = '/('.$e.'[^'.$e.']*(?:'.$e.$e.'[^'.$e.']*)*'.$e.'|[^'.$d.']*)'.$d.'/'; |
|
107
|
|
|
|
|
108
|
|
|
preg_match_all($_csv_pattern, $_csv_line, $_csv_matches); |
|
109
|
|
|
|
|
110
|
|
|
$_csv_data = $_csv_matches[1]; |
|
111
|
|
|
|
|
112
|
|
|
for ( $_csv_i=0; $_csv_i<count($_csv_data); $_csv_i++ ) { |
|
|
|
|
|
|
113
|
|
|
$_csv_data[$_csv_i] = preg_replace('/^'.$e.'(.*)'.$e.'$/s', '$1', $_csv_data[$_csv_i]); |
|
114
|
|
|
$_csv_data[$_csv_i] = str_replace($e.$e, $e, $_csv_data[$_csv_i]); |
|
115
|
|
|
} |
|
116
|
|
|
return empty($_line) ? false : $_csv_data; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.