Changeset 18561 for branches/version-2/data/class/helper/SC_Helper_CSV.php
- Timestamp:
- 2010/02/12 18:13:28 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2/data/class/helper/SC_Helper_CSV.php
r18177 r18561 99 99 } 100 100 101 102 // CSV出力データを作成する。(商品) 103 function lfGetProductsCSV($where, $option, $arrval, $arrOutputCols) { 104 $objDb = new SC_Helper_DB_Ex(); 105 106 $from = "vw_product_class AS prdcls"; 107 $cols = SC_Utils_Ex::sfGetCommaList($arrOutputCols, true, array('category_id')); 108 109 $objQuery = new SC_Query(); 110 $objQuery->setoption($option); 111 112 $list_data = $objQuery->select($cols, $from, $where, $arrval); 113 $max = count($list_data); 101 // CSVを送信する。(商品) 102 function sfDownloadProductsCsv($where, $arrval, $order) { 103 104 // CSV出力タイトル行の作成 105 $arrOutput = SC_Utils_Ex::sfSwapArray($this->sfgetCsvOutput(1, " WHERE csv_id = 1 AND status = 1")); 106 if (count($arrOutput) <= 0) return false; // 失敗終了 107 $arrOutputCols = $arrOutput['col']; 108 109 $objQuery = new SC_Query(); 110 $objQuery->setorder($order); 111 112 $dataRows = $objQuery->select( 113 SC_Utils_Ex::sfGetCommaList($arrOutputCols, true, array('category_id')) 114 ,'vw_product_class AS prdcls' 115 ,$where 116 ,$arrval 117 ); 114 118 115 119 // 規格分類名一覧 116 120 if (in_array('classcategory_id1', $arrOutputCols) || in_array('classcategory_id2', $arrOutputCols)) { 121 $objDb = new SC_Helper_DB_Ex(); 117 122 $arrClassCatName = $objDb->sfGetIDValueList("dtb_classcategory", "classcategory_id", "name"); 118 123 } 119 124 120 if (!isset($data)) $data = ""; 121 for($i = 0; $i < $max; $i++) { 122 // 関連商品情報の付与 125 $outputArray = array(); 126 127 // ヘッダ行 128 $outputArray[] = $arrOutput['disp_name']; 129 130 // データ行 131 foreach ($dataRows as $row) { 132 // 規格名1 123 133 if (in_array('classcategory_id1', $arrOutputCols)) { 124 $list_data[$i]['classcategory_id1'] = $arrClassCatName[$list_data[$i]['classcategory_id1']]; 125 } 134 $row['classcategory_id1'] = $arrClassCatName[$row['classcategory_id1']]; 135 } 136 137 // 規格名2 126 138 if (in_array('classcategory_id2', $arrOutputCols)) { 127 $list_data[$i]['classcategory_id2'] = $arrClassCatName[$list_data[$i]['classcategory_id2']]; 128 } 129 139 $row['classcategory_id2'] = $arrClassCatName[$row['classcategory_id2']]; 140 } 141 142 // カテゴリID 130 143 if (in_array('category_id', $arrOutputCols)) { 131 $ arrCategory_id= $objQuery->getCol("dtb_product_categories",144 $row['category_id'] = $objQuery->getCol("dtb_product_categories", 132 145 "category_id", 133 146 "product_id = ?", 134 array($list_data[$i]['product_id'])); 135 136 // カテゴリID 付与 137 for ($j = 0; $j < count($arrCategory_id); $j++) { 138 $list_data[$i]['category_id'] .= $arrCategory_id[$j]; 139 if ($j < count($arrCategory_id) - 1) { 140 $list_data[$i]['category_id'] .= "|"; 141 } 142 } 143 } 144 145 // 各項目をCSV出力用に変換する。 146 $data .= $this->lfMakeProductsCSV($list_data[$i]); 147 } 148 return $data; 147 array($row['product_id'])); 148 } 149 150 $outputArray[] = $row; 151 } 152 153 // CSVを送信する。 154 $this->lfDownloadCsv($outputArray); 155 156 // 成功終了 157 return true; 149 158 } 150 159 … … 188 197 } 189 198 190 // CSV出力データを作成する。(カテゴリ) 191 function lfGetCategoryCSV($where, $option, $arrval, $arrOutputCols) { 192 $objDb = new SC_Helper_DB_Ex(); 193 194 $from = "dtb_category"; 195 $cols = SC_Utils_Ex::sfGetCommaList($arrOutputCols); 196 197 $objQuery = new SC_Query(); 198 $objQuery->setoption($option); 199 200 $list_data = $objQuery->select($cols, $from, $where, $arrval); 201 $max = count($list_data); 202 203 if (!isset($data)) $data = ""; 204 for($i = 0; $i < $max; $i++) { 205 // 各項目をCSV出力用に変換する。 206 $data .= $this->lfMakeCSV($list_data[$i]); 207 } 208 return $data; 199 // CSVを送信する。(カテゴリ) 200 function sfDownloadCategoryCsv() { 201 202 // CSV出力タイトル行の作成 203 $arrOutput = SC_Utils_Ex::sfSwapArray($this->sfgetCsvOutput(5, " WHERE csv_id = 5 AND status = 1")); 204 if (count($arrOutput) <= 0) return false; // 失敗終了 205 $arrOutputCols = $arrOutput['col']; 206 207 $objQuery = new SC_Query(); 208 $objQuery->setorder('rank DESC'); 209 210 $dataRows = $objQuery->select( 211 SC_Utils_Ex::sfGetCommaList($arrOutputCols) 212 ,'dtb_category' 213 ,'del_flg = 0' 214 ); 215 216 $outputArray = array(); 217 218 // ヘッダ行 219 $outputArray[] = $arrOutput['disp_name']; 220 221 // データ行 222 foreach ($dataRows as $row) { 223 $outputArray[] = $row; 224 } 225 226 // CSVを送信する。 227 $this->lfDownloadCsv($outputArray, 'category'); 228 229 // 成功終了 230 return true; 209 231 } 210 232 … … 231 253 function lfMakeCSV($list) { 232 254 $line = ""; 233 234 255 256 reset($list); 235 257 while(list($key, $val) = each($list)){ 236 258 $tmp = ""; 237 259 switch($key) { 238 260 case 'order_pref': 239 $tmp = $this->arrPref[$val];240 break;241 261 case 'deliv_pref': 242 262 $tmp = $this->arrPref[$val]; … … 255 275 } 256 276 257 // 各項目をCSV出力用に変換する。(商品)258 function lfMakeProductsCSV($list) {259 $line = "";260 if(is_array($list)) {261 reset($list);262 while(list($key, $val) = each($list)){263 $tmp = "";264 switch($key) {265 case 'point_rate':266 if($val == "") {267 $tmp = '0';268 } else {269 $tmp = $val;270 }271 break;272 default:273 $tmp = $val;274 break;275 }276 $tmp = str_replace("\"", "\\\"", $tmp);277 $line .= "\"".$tmp."\",";278 }279 // 文末の","を変換280 $line = $this->replaceLineSuffix($line);281 }282 return $line;283 }284 285 277 // 各項目をCSV出力用に変換する。(レビュー) 286 278 function lfMakeReviewCSV($list) { 287 279 $line = ""; 288 289 280 reset($list); 281 while(list($key, $val) = each($list)){ 290 282 $tmp = ""; 291 283 switch($key) { … … 316 308 function lfMakeTrackbackCSV($list) { 317 309 $line = ""; 318 319 310 reset($list); 311 while(list($key, $val) = each($list)){ 320 312 $tmp = ""; 321 313 switch($key) { … … 411 403 ); 412 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 } 413 468 } 414 469 ?>
Note: See TracChangeset
for help on using the changeset viewer.