1
|
|
|
<?php |
2
|
|
|
namespace CsvCombine\Controller\Component; |
3
|
|
|
use Cake\Controller\Component; |
4
|
|
|
use Cake\Core\Configure; |
5
|
|
|
use Cake\Event\Event; |
6
|
|
|
use Cake\Network\Exception\MethodNotAllowedException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* CsvExportComponent code license: |
10
|
|
|
* |
11
|
|
|
* @copyright Copyright (C) 2011 hagiwara. |
12
|
|
|
* @since CakePHP(tm) v 1.3 |
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
14
|
|
|
*/ |
15
|
|
|
class CsvExportComponent extends Component { |
16
|
|
|
|
17
|
|
|
private $_controller; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* コンポーネント初期化 |
21
|
|
|
* |
22
|
|
|
* @access public |
23
|
|
|
*/ |
24
|
|
|
public function startup(Event $event) |
25
|
|
|
{ |
26
|
|
|
$this->_controller = $event->subject(); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/* |
30
|
|
|
* export CSVの出力アクション |
31
|
|
|
* |
32
|
|
|
* @param array $list 出力のための配列(二次元配列が基本) |
33
|
|
|
* @param string $file_name 出力ファイル名(デフォルトはexport.csv) |
34
|
|
|
* @param string $delimiter 区切り文字の設定(デフォルトは",") |
35
|
|
|
* @param string $directory 一時保存ディレクトリ(デフォルトはTMP,最終的に削除をする) |
36
|
|
|
* @param string $export_encoding 入力するファイルのエンコード(デフォルトはSJIS-win |
37
|
|
|
* @param string $array_encoding 出力する配列のエンコード(デフォルトはUTF-8 |
38
|
|
|
*/ |
39
|
|
|
public function export($list, $file_name = 'export.csv', $delimiter = ",", $directory = TMP,$export_encoding = 'SJIS-win',$array_encoding = 'UTF-8') |
40
|
|
|
{ |
41
|
|
|
//layoutを切って autoRenderも外しておく |
42
|
|
|
$this->_controller->viewBuilder()->layout('ajax'); |
43
|
|
|
$this->_controller->autoRender = false; |
44
|
|
|
|
45
|
|
|
//headerのセット |
46
|
|
|
$save_directory = $this->make($list, $file_name , $delimiter , $directory ,$export_encoding ,$array_encoding); |
47
|
|
|
// 日本語ファイル名出力のため |
48
|
|
|
setlocale(LC_ALL, 'ja_JP.UTF-8'); |
49
|
|
|
$basename = basename($save_directory); |
50
|
|
|
|
51
|
|
|
// ファイル名の変換(IE対応) |
52
|
|
View Code Duplication |
if (strstr(env('HTTP_USER_AGENT'), 'MSIE') || strstr(env('HTTP_USER_AGENT'), 'Trident') || strstr(env('HTTP_USER_AGENT'), 'Edge')) { |
|
|
|
|
53
|
|
|
$basename = mb_convert_encoding($basename, "SJIS", "UTF-8"); |
54
|
|
|
} |
55
|
|
|
$this->_controller->response->file($save_directory, ['download' => true, 'name' => $basename]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/* |
59
|
|
|
* make CSVの生成アクション |
60
|
|
|
* |
61
|
|
|
* @param array $list 出力のための配列(二次元配列が基本) |
62
|
|
|
* @param string $file_name 出力ファイル名(デフォルトはexport.csv) |
63
|
|
|
* @param string $delimiter 区切り文字の設定(デフォルトは",") |
64
|
|
|
* @param string $directory 一時保存ディレクトリ(デフォルトはTMP,最終的に削除をする) |
65
|
|
|
* @param string $export_encoding 入力するファイルのエンコード(デフォルトはSJIS-win |
66
|
|
|
* @param string $array_encoding 出力する配列のエンコード(デフォルトはUTF-8 |
67
|
|
|
*/ |
68
|
|
|
public function make($list, $file_name = 'export.csv', $delimiter = ",", $directory = TMP,$export_encoding = 'SJIS-win',$array_encoding = 'UTF-8') |
69
|
|
|
{ |
70
|
|
|
Configure::write('debug', 0); |
71
|
|
|
ini_set("memory_limit", -1); |
72
|
|
|
set_time_limit(0); |
73
|
|
|
$csv_list = array(); |
74
|
|
|
mb_convert_variables($export_encoding, $array_encoding, $list); |
75
|
|
|
//$listにカンマか"がいた時の対応 |
76
|
|
|
if (isset($list)) { |
77
|
|
|
if (is_array($list)) { |
78
|
|
|
foreach ($list as $k => $list1) { |
79
|
|
|
if (is_array($list1)) { |
80
|
|
|
foreach ($list1 as $m => $v) { |
81
|
|
|
if (is_array($v)){ |
82
|
|
|
//3次元以上の配列の時はエラー |
83
|
|
|
throw new MethodNotAllowedException('array layer error'); |
84
|
|
|
} |
85
|
|
|
$csv_list[$k][$m] = $this->_parseCsv($v, $delimiter); |
86
|
|
|
} |
87
|
|
|
} else { |
88
|
|
|
//1次元の時は1列目に値が入る。 |
89
|
|
|
$csv_list[0][$k] = $this->_parseCsv($list1, $delimiter); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
//文字列の時は1カラムに値が入るだけ。 |
94
|
|
|
$csv_list[0][0] = $this->_parseCsv($list, $delimiter); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$save_directory = $directory . $file_name; |
99
|
|
|
$fp = fopen($save_directory, 'w'); |
100
|
|
|
foreach ($csv_list as $fields) { |
101
|
|
|
fputs($fp, implode($delimiter, $fields) . "\r\n"); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
fclose($fp); |
105
|
|
|
|
106
|
|
|
return $save_directory; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/* |
110
|
|
|
* _parseCsv |
111
|
|
|
* csv(など)の形式に変更 |
112
|
|
|
* |
113
|
|
|
* @param string $v 変換する値 |
114
|
|
|
* @param string $delimiter 区切り文字 |
115
|
|
|
*/ |
116
|
|
|
private function _parseCsv($v, $delimiter) |
117
|
|
|
{ |
118
|
|
|
//区切り文字・改行・ダブルクオートの時 |
119
|
|
View Code Duplication |
if (preg_match('/[' . $delimiter . '\\n"]/', $v)) { |
|
|
|
|
120
|
|
|
$v = str_replace('"', '""', $v); |
121
|
|
|
$v = '"' . $v . '"'; |
122
|
|
|
} |
123
|
|
|
return $v; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.