1 | <?php |
---|
2 | /* |
---|
3 | * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. |
---|
4 | * |
---|
5 | * http://www.lockon.co.jp/ |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * CSV 関連 のヘルパークラス. |
---|
10 | * |
---|
11 | * @package Page |
---|
12 | * @author LOCKON CO.,LTD. |
---|
13 | * @version $Id$ |
---|
14 | */ |
---|
15 | class SC_Helper_CSV { |
---|
16 | |
---|
17 | // {{{ properties |
---|
18 | |
---|
19 | /** 項目英名 */ |
---|
20 | var $arrSubnavi; |
---|
21 | |
---|
22 | /** 項目名 */ |
---|
23 | var $arrSubnaviName; |
---|
24 | |
---|
25 | /** レビュー管理項目 */ |
---|
26 | var $arrREVIEW_CVSCOL; |
---|
27 | |
---|
28 | /** レビュータイトル */ |
---|
29 | var $arrREVIEW_CVSTITLE; |
---|
30 | |
---|
31 | /** トラックバック項目 */ |
---|
32 | var $arrTRACKBACK_CVSCOL; |
---|
33 | |
---|
34 | /** トラックバックタイトル */ |
---|
35 | var $arrTRACKBACK_CVSTITLE; |
---|
36 | |
---|
37 | |
---|
38 | // }}} |
---|
39 | // {{{ constructor |
---|
40 | |
---|
41 | /** |
---|
42 | * デフォルトコンストラクタ. |
---|
43 | */ |
---|
44 | function SC_Helper_CSV() { |
---|
45 | $this->init(); |
---|
46 | |
---|
47 | $masterData = new SC_DB_MasterData_Ex(); |
---|
48 | $this->arrPref = $masterData->getMasterData("mtb_pref", |
---|
49 | array("pref_id", "pref_name", "rank")); |
---|
50 | $this->arrSex = $masterData->getMasterData("mtb_sex"); |
---|
51 | $this->arrDISP = $masterData->getMasterData("mtb_disp"); |
---|
52 | $this->arrRECOMMEND = $masterData->getMasterData("mtb_recommend"); |
---|
53 | } |
---|
54 | |
---|
55 | // }}} |
---|
56 | // {{{ functions |
---|
57 | |
---|
58 | /** |
---|
59 | * CSV 項目を出力する. |
---|
60 | * |
---|
61 | * @param integer $csv_id CSV ID |
---|
62 | * @param string $where SQL の WHERE 句 |
---|
63 | * @param array $arrVal WHERE 句の要素 |
---|
64 | * @return array CSV 項目の配列 |
---|
65 | */ |
---|
66 | function sfgetCsvOutput($csv_id = "", $where = "", $arrVal = array()){ |
---|
67 | $objQuery = new SC_Query(); |
---|
68 | $arrData = array(); |
---|
69 | $ret = array(); |
---|
70 | |
---|
71 | $sql = ""; |
---|
72 | $sql .= " SELECT "; |
---|
73 | $sql .= " no, "; |
---|
74 | $sql .= " csv_id, "; |
---|
75 | $sql .= " col, "; |
---|
76 | $sql .= " disp_name, "; |
---|
77 | $sql .= " rank, "; |
---|
78 | $sql .= " status, "; |
---|
79 | $sql .= " create_date, "; |
---|
80 | $sql .= " update_date "; |
---|
81 | $sql .= " FROM "; |
---|
82 | $sql .= " dtb_csv "; |
---|
83 | |
---|
84 | if ($where != "") { |
---|
85 | $sql .= $where; |
---|
86 | $arrData = $arrVal; |
---|
87 | }elseif($csv_id != ""){ |
---|
88 | $sql .= " WHERE csv_id = ? "; |
---|
89 | $arrData = array($csv_id); |
---|
90 | } |
---|
91 | |
---|
92 | $sql .= " ORDER BY "; |
---|
93 | $sql .= " rank , no"; |
---|
94 | $sql .= " "; |
---|
95 | |
---|
96 | $ret = $objQuery->getall($sql, $arrData); |
---|
97 | |
---|
98 | return $ret; |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | // CSVを送信する。(商品) |
---|
103 | function sfDownloadProductsCsv($where, $arrval, $order) { |
---|
104 | |
---|
105 | // CSV出力タイトル行の作成 |
---|
106 | $arrOutput = SC_Utils_Ex::sfSwapArray($this->sfgetCsvOutput(1, " WHERE csv_id = 1 AND status = 1")); |
---|
107 | if (count($arrOutput) <= 0) return false; // 失敗終了 |
---|
108 | $arrOutputCols = $arrOutput['col']; |
---|
109 | |
---|
110 | $objQuery = new SC_Query(); |
---|
111 | $objQuery->setorder($order); |
---|
112 | |
---|
113 | $dataRows = $objQuery->select( |
---|
114 | SC_Utils_Ex::sfGetCommaList($arrOutputCols, true, array('category_id')) |
---|
115 | ,'vw_product_class AS prdcls' |
---|
116 | ,$where |
---|
117 | ,$arrval |
---|
118 | ); |
---|
119 | |
---|
120 | // 規格分類名一覧 |
---|
121 | if (in_array('classcategory_id1', $arrOutputCols) || in_array('classcategory_id2', $arrOutputCols)) { |
---|
122 | $objDb = new SC_Helper_DB_Ex(); |
---|
123 | $arrClassCatName = $objDb->sfGetIDValueList("dtb_classcategory", "classcategory_id", "name"); |
---|
124 | } |
---|
125 | |
---|
126 | $outputArray = array(); |
---|
127 | |
---|
128 | // ヘッダ行 |
---|
129 | $outputArray[] = $arrOutput['disp_name']; |
---|
130 | |
---|
131 | // データ行 |
---|
132 | foreach ($dataRows as $row) { |
---|
133 | // 規格名1 |
---|
134 | if (in_array('classcategory_id1', $arrOutputCols)) { |
---|
135 | $row['classcategory_id1'] = $arrClassCatName[$row['classcategory_id1']]; |
---|
136 | } |
---|
137 | |
---|
138 | // 規格名2 |
---|
139 | if (in_array('classcategory_id2', $arrOutputCols)) { |
---|
140 | $row['classcategory_id2'] = $arrClassCatName[$row['classcategory_id2']]; |
---|
141 | } |
---|
142 | |
---|
143 | // カテゴリID |
---|
144 | if (in_array('category_id', $arrOutputCols)) { |
---|
145 | $row['category_id'] = $objQuery->getCol("dtb_product_categories", |
---|
146 | "category_id", |
---|
147 | "product_id = ?", |
---|
148 | array($row['product_id'])); |
---|
149 | } |
---|
150 | |
---|
151 | $outputArray[] = $row; |
---|
152 | } |
---|
153 | |
---|
154 | // CSVを送信する。 |
---|
155 | $this->lfDownloadCsv($outputArray); |
---|
156 | |
---|
157 | // 成功終了 |
---|
158 | return ture; |
---|
159 | } |
---|
160 | |
---|
161 | // CSV出力データを作成する。(レビュー) |
---|
162 | function lfGetReviewCSV($where, $option, $arrval) { |
---|
163 | |
---|
164 | $from = "dtb_review AS A INNER JOIN dtb_products AS B on A.product_id = B.product_id "; |
---|
165 | $cols = SC_Utils_Ex::sfGetCommaList($this->arrREVIEW_CVSCOL); |
---|
166 | |
---|
167 | $objQuery = new SC_Query(); |
---|
168 | $objQuery->setoption($option); |
---|
169 | |
---|
170 | $list_data = $objQuery->select($cols, $from, $where, $arrval); |
---|
171 | |
---|
172 | $max = count($list_data); |
---|
173 | if (!isset($data)) $data = ""; |
---|
174 | for($i = 0; $i < $max; $i++) { |
---|
175 | // 各項目をCSV出力用に変換する。 |
---|
176 | $data .= $this->lfMakeReviewCSV($list_data[$i]); |
---|
177 | } |
---|
178 | return $data; |
---|
179 | } |
---|
180 | |
---|
181 | // CSV出力データを作成する。(トラックバック) |
---|
182 | function lfGetTrackbackCSV($where, $option, $arrval) { |
---|
183 | $from = "dtb_trackback AS A INNER JOIN dtb_products AS B on A.product_id = B.product_id "; |
---|
184 | $cols = SC_Utils_Ex::sfGetCommaList($this->arrTRACKBACK_CVSCOL); |
---|
185 | |
---|
186 | $objQuery = new SC_Query(); |
---|
187 | $objQuery->setoption($option); |
---|
188 | |
---|
189 | $list_data = $objQuery->select($cols, $from, $where, $arrval); |
---|
190 | |
---|
191 | $max = count($list_data); |
---|
192 | if (!isset($data)) $data = ""; |
---|
193 | for($i = 0; $i < $max; $i++) { |
---|
194 | // 各項目をCSV出力用に変換する。 |
---|
195 | $data .= $this->lfMakeTrackbackCSV($list_data[$i]); |
---|
196 | } |
---|
197 | return $data; |
---|
198 | } |
---|
199 | |
---|
200 | // CSVを送信する。(カテゴリ) |
---|
201 | function sfDownloadCategoryCsv() { |
---|
202 | |
---|
203 | // CSV出力タイトル行の作成 |
---|
204 | $arrOutput = SC_Utils_Ex::sfSwapArray($this->sfgetCsvOutput(5, " WHERE csv_id = 5 AND status = 1")); |
---|
205 | if (count($arrOutput) <= 0) return false; // 失敗終了 |
---|
206 | $arrOutputCols = $arrOutput['col']; |
---|
207 | |
---|
208 | $objQuery = new SC_Query(); |
---|
209 | $objQuery->setorder('rank DESC'); |
---|
210 | |
---|
211 | $dataRows = $objQuery->select( |
---|
212 | SC_Utils_Ex::sfGetCommaList($arrOutputCols) |
---|
213 | ,'dtb_category' |
---|
214 | ,'del_flg = 0' |
---|
215 | ); |
---|
216 | |
---|
217 | $outputArray = array(); |
---|
218 | |
---|
219 | // ヘッダ行 |
---|
220 | $outputArray[] = $arrOutput['disp_name']; |
---|
221 | |
---|
222 | // データ行 |
---|
223 | foreach ($dataRows as $row) { |
---|
224 | $outputArray[] = $row; |
---|
225 | } |
---|
226 | |
---|
227 | // CSVを送信する。 |
---|
228 | $this->lfDownloadCsv($outputArray, 'category'); |
---|
229 | |
---|
230 | // 成功終了 |
---|
231 | return ture; |
---|
232 | } |
---|
233 | |
---|
234 | // CSV出力データを作成する。 |
---|
235 | function lfGetCSV($from, $where, $option, $arrval, $arrCsvOutputCols = "") { |
---|
236 | |
---|
237 | $cols = SC_Utils_Ex::sfGetCommaList($arrCsvOutputCols); |
---|
238 | |
---|
239 | $objQuery = new SC_Query(); |
---|
240 | $objQuery->setoption($option); |
---|
241 | |
---|
242 | $list_data = $objQuery->select($cols, $from, $where, $arrval); |
---|
243 | |
---|
244 | $max = count($list_data); |
---|
245 | if (!isset($data)) $data = ""; |
---|
246 | for($i = 0; $i < $max; $i++) { |
---|
247 | // 各項目をCSV出力用に変換する。 |
---|
248 | $data .= $this->lfMakeCSV($list_data[$i]); |
---|
249 | } |
---|
250 | return $data; |
---|
251 | } |
---|
252 | |
---|
253 | // 各項目をCSV出力用に変換する。 |
---|
254 | function lfMakeCSV($list) { |
---|
255 | $line = ""; |
---|
256 | |
---|
257 | foreach($list as $key => $val) { |
---|
258 | $tmp = ""; |
---|
259 | switch($key) { |
---|
260 | case 'order_pref': |
---|
261 | case 'deliv_pref': |
---|
262 | $tmp = $this->arrPref[$val]; |
---|
263 | break; |
---|
264 | default: |
---|
265 | $tmp = $val; |
---|
266 | break; |
---|
267 | } |
---|
268 | |
---|
269 | $tmp = ereg_replace("[\",]", " ", $tmp); |
---|
270 | $line .= "\"".$tmp."\","; |
---|
271 | } |
---|
272 | // 文末の","を変換 |
---|
273 | $line = $this->replaceLineSuffix($line); |
---|
274 | return $line; |
---|
275 | } |
---|
276 | |
---|
277 | // 各項目をCSV出力用に変換する。(レビュー) |
---|
278 | function lfMakeReviewCSV($list) { |
---|
279 | $line = ""; |
---|
280 | |
---|
281 | foreach($list as $key => $val) { |
---|
282 | $tmp = ""; |
---|
283 | switch($key) { |
---|
284 | case 'sex': |
---|
285 | $tmp = isset($this->arrSex[$val]) ? $this->arrSex[$val] : ""; |
---|
286 | break; |
---|
287 | case 'recommend_level': |
---|
288 | $tmp = isset($this->arrRECOMMEND[$val]) ? $this->arrRECOMMEND[$val] |
---|
289 | : ""; |
---|
290 | break; |
---|
291 | case 'status': |
---|
292 | $tmp = isset($this->arrDISP[$val]) ? $this->arrDISP[$val] : ""; |
---|
293 | break; |
---|
294 | default: |
---|
295 | $tmp = $val; |
---|
296 | break; |
---|
297 | } |
---|
298 | |
---|
299 | $tmp = ereg_replace("[\",]", " ", $tmp); |
---|
300 | $line .= "\"".$tmp."\","; |
---|
301 | } |
---|
302 | // 文末の","を変換 |
---|
303 | $line = $this->replaceLineSuffix($line); |
---|
304 | return $line; |
---|
305 | } |
---|
306 | |
---|
307 | // 各項目をCSV出力用に変換する。(トラックバック) |
---|
308 | function lfMakeTrackbackCSV($list) { |
---|
309 | |
---|
310 | $line = ""; |
---|
311 | |
---|
312 | foreach($list as $key => $val) { |
---|
313 | $tmp = ""; |
---|
314 | switch($key) { |
---|
315 | case 'status': |
---|
316 | $tmp = $this->arrTrackBackStatus[$val]; |
---|
317 | break; |
---|
318 | default: |
---|
319 | $tmp = $val; |
---|
320 | break; |
---|
321 | } |
---|
322 | |
---|
323 | $tmp = ereg_replace("[\",]", " ", $tmp); |
---|
324 | $line .= "\"".$tmp."\","; |
---|
325 | } |
---|
326 | // 文末の","を変換 |
---|
327 | $line = $this->replaceLineSuffix($line); |
---|
328 | return $line; |
---|
329 | } |
---|
330 | |
---|
331 | /** |
---|
332 | * 行末の ',' を CRLF へ変換する. |
---|
333 | * |
---|
334 | * @access private |
---|
335 | * @param string $line CSV出力用の1行分の文字列 |
---|
336 | * @return string 行末の ',' を CRLF に変換した文字列 |
---|
337 | */ |
---|
338 | function replaceLineSuffix($line) { |
---|
339 | return mb_ereg_replace(",$", "\r\n", $line); |
---|
340 | } |
---|
341 | |
---|
342 | /** |
---|
343 | * 項目情報を初期化する. |
---|
344 | * |
---|
345 | * @access private |
---|
346 | * @return void |
---|
347 | */ |
---|
348 | function init() { |
---|
349 | $this->arrSubnavi = array( |
---|
350 | 1 => 'product', |
---|
351 | 2 => 'customer', |
---|
352 | 3 => 'order', |
---|
353 | 4 => 'campaign', |
---|
354 | 5 => 'category' |
---|
355 | ); |
---|
356 | |
---|
357 | $this->arrSubnaviName = array( |
---|
358 | 1 => '商品管理', |
---|
359 | 2 => '顧客管理', |
---|
360 | 3 => '受注管理', |
---|
361 | 4 => 'キャンペーン', |
---|
362 | 5 => 'カテゴリ' |
---|
363 | ); |
---|
364 | |
---|
365 | |
---|
366 | $this->arrREVIEW_CVSCOL = array( |
---|
367 | 'B.name', |
---|
368 | 'A.status', |
---|
369 | 'A.create_date', |
---|
370 | 'A.reviewer_name', |
---|
371 | 'A.sex', |
---|
372 | 'A.recommend_level', |
---|
373 | 'A.title', |
---|
374 | 'A.comment' |
---|
375 | ); |
---|
376 | |
---|
377 | $this->arrREVIEW_CVSTITLE = array( |
---|
378 | '商品名', |
---|
379 | 'レビュー表示', |
---|
380 | '投稿日', |
---|
381 | '投稿者名', |
---|
382 | '性別', |
---|
383 | 'おすすめレベル', |
---|
384 | 'タイトル', |
---|
385 | 'コメント' |
---|
386 | ); |
---|
387 | |
---|
388 | $this->arrTRACKBACK_CVSTITLE = array( |
---|
389 | '商品名', |
---|
390 | 'ブログ名', |
---|
391 | 'ブログ記事タイトル', |
---|
392 | 'ブログ記事内容', |
---|
393 | '状態', |
---|
394 | '投稿日' |
---|
395 | ); |
---|
396 | |
---|
397 | $this->arrTRACKBACK_CVSCOL = array( |
---|
398 | 'B.name', |
---|
399 | 'A.blog_name', |
---|
400 | 'A.title', |
---|
401 | 'A.excerpt', |
---|
402 | 'A.status', |
---|
403 | 'A.create_date' |
---|
404 | ); |
---|
405 | } |
---|
406 | |
---|
407 | /** |
---|
408 | * 1次元配列を1行のCSVとして返す |
---|
409 | * 参考: http://jp.php.net/fputcsv |
---|
410 | */ |
---|
411 | function sfArrayToCsv($fields, $delimiter = ',', $enclosure = '"', $arrayDelimiter = '|') { |
---|
412 | |
---|
413 | if( strlen($delimiter) != 1 ) { |
---|
414 | trigger_error('delimiter must be a single character', E_USER_WARNING); |
---|
415 | return ""; |
---|
416 | } |
---|
417 | |
---|
418 | if( strlen($enclosure) < 1 ) { |
---|
419 | trigger_error('enclosure must be a single character', E_USER_WARNING); |
---|
420 | return ""; |
---|
421 | } |
---|
422 | |
---|
423 | foreach (array_keys($fields) as $key) { |
---|
424 | $field =& $fields[$key]; |
---|
425 | |
---|
426 | // 配列を「|」区切りの文字列に変換する |
---|
427 | if (is_array($field)) { |
---|
428 | $field = implode($arrayDelimiter, $field); |
---|
429 | } |
---|
430 | |
---|
431 | /* enclose a field that contains a delimiter, an enclosure character, or a newline */ |
---|
432 | if ( |
---|
433 | is_string($field) |
---|
434 | && preg_match('/[' . preg_quote($delimiter) . preg_quote($enclosure) . '\\s]/', $field) |
---|
435 | ) { |
---|
436 | $field = $enclosure . preg_replace('/' . preg_quote($enclosure) . '/', $enclosure . $enclosure, $field) . $enclosure; |
---|
437 | } |
---|
438 | } |
---|
439 | |
---|
440 | return implode($delimiter, $fields); |
---|
441 | } |
---|
442 | |
---|
443 | /** |
---|
444 | * CSVを送信する。 |
---|
445 | */ |
---|
446 | function lfDownloadCsv($arrayData, $prefix = ""){ |
---|
447 | |
---|
448 | if($prefix == "") { |
---|
449 | $dir_name = SC_Utils::sfUpDirName(); |
---|
450 | $file_name = $dir_name . date("ymdHis") .".csv"; |
---|
451 | } else { |
---|
452 | $file_name = $prefix . date("ymdHis") .".csv"; |
---|
453 | } |
---|
454 | |
---|
455 | /* HTTPヘッダの出力 */ |
---|
456 | Header("Content-disposition: attachment; filename=${file_name}"); |
---|
457 | Header("Content-type: application/octet-stream; name=${file_name}"); |
---|
458 | Header("Cache-Control: "); |
---|
459 | Header("Pragma: "); |
---|
460 | |
---|
461 | /* データを出力 */ |
---|
462 | foreach ($arrayData as $lineArray) { |
---|
463 | $lineString = $this->sfArrayToCsv($lineArray); |
---|
464 | $lineString = mb_convert_encoding($lineString, 'SJIS-Win'); |
---|
465 | echo $lineString . "\n"; |
---|
466 | } |
---|
467 | } |
---|
468 | } |
---|
469 | ?> |
---|