| 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 | // 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 | ); |
|---|
| 118 | |
|---|
| 119 | // 規格分類名一覧 |
|---|
| 120 | if (in_array('classcategory_id1', $arrOutputCols) || in_array('classcategory_id2', $arrOutputCols)) { |
|---|
| 121 | $objDb = new SC_Helper_DB_Ex(); |
|---|
| 122 | $arrClassCatName = $objDb->sfGetIDValueList("dtb_classcategory", "classcategory_id", "name"); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | $outputArray = array(); |
|---|
| 126 | |
|---|
| 127 | // ヘッダ行 |
|---|
| 128 | $outputArray[] = $arrOutput['disp_name']; |
|---|
| 129 | |
|---|
| 130 | // データ行 |
|---|
| 131 | foreach ($dataRows as $row) { |
|---|
| 132 | // 規格名1 |
|---|
| 133 | if (in_array('classcategory_id1', $arrOutputCols)) { |
|---|
| 134 | $row['classcategory_id1'] = $arrClassCatName[$row['classcategory_id1']]; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | // 規格名2 |
|---|
| 138 | if (in_array('classcategory_id2', $arrOutputCols)) { |
|---|
| 139 | $row['classcategory_id2'] = $arrClassCatName[$row['classcategory_id2']]; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | // カテゴリID |
|---|
| 143 | if (in_array('category_id', $arrOutputCols)) { |
|---|
| 144 | $row['category_id'] = $objQuery->getCol("dtb_product_categories", |
|---|
| 145 | "category_id", |
|---|
| 146 | "product_id = ?", |
|---|
| 147 | array($row['product_id'])); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | $outputArray[] = $row; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | // CSVを送信する。 |
|---|
| 154 | $this->lfDownloadCsv($outputArray); |
|---|
| 155 | |
|---|
| 156 | // 成功終了 |
|---|
| 157 | return true; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | // CSV出力データを作成する。(レビュー) |
|---|
| 161 | function lfGetReviewCSV($where, $option, $arrval) { |
|---|
| 162 | |
|---|
| 163 | $from = "dtb_review AS A INNER JOIN dtb_products AS B on A.product_id = B.product_id "; |
|---|
| 164 | $cols = SC_Utils_Ex::sfGetCommaList($this->arrREVIEW_CVSCOL); |
|---|
| 165 | |
|---|
| 166 | $objQuery = new SC_Query(); |
|---|
| 167 | $objQuery->setOption($option); |
|---|
| 168 | |
|---|
| 169 | $list_data = $objQuery->select($cols, $from, $where, $arrval); |
|---|
| 170 | |
|---|
| 171 | $max = count($list_data); |
|---|
| 172 | if (!isset($data)) $data = ""; |
|---|
| 173 | for($i = 0; $i < $max; $i++) { |
|---|
| 174 | // 各項目をCSV出力用に変換する。 |
|---|
| 175 | $data .= $this->lfMakeReviewCSV($list_data[$i]); |
|---|
| 176 | } |
|---|
| 177 | return $data; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | // CSV出力データを作成する。(トラックバック) |
|---|
| 181 | function lfGetTrackbackCSV($where, $option, $arrval) { |
|---|
| 182 | $from = "dtb_trackback AS A INNER JOIN dtb_products AS B on A.product_id = B.product_id "; |
|---|
| 183 | $cols = SC_Utils_Ex::sfGetCommaList($this->arrTRACKBACK_CVSCOL); |
|---|
| 184 | |
|---|
| 185 | $objQuery = new SC_Query(); |
|---|
| 186 | $objQuery->setOption($option); |
|---|
| 187 | |
|---|
| 188 | $list_data = $objQuery->select($cols, $from, $where, $arrval); |
|---|
| 189 | |
|---|
| 190 | $max = count($list_data); |
|---|
| 191 | if (!isset($data)) $data = ""; |
|---|
| 192 | for($i = 0; $i < $max; $i++) { |
|---|
| 193 | // 各項目をCSV出力用に変換する。 |
|---|
| 194 | $data .= $this->lfMakeTrackbackCSV($list_data[$i]); |
|---|
| 195 | } |
|---|
| 196 | return $data; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 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; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | // CSV出力データを作成する。 |
|---|
| 234 | function lfGetCSV($from, $where, $option, $arrval, $arrCsvOutputCols = "") { |
|---|
| 235 | |
|---|
| 236 | $cols = SC_Utils_Ex::sfGetCommaList($arrCsvOutputCols); |
|---|
| 237 | |
|---|
| 238 | $objQuery = new SC_Query(); |
|---|
| 239 | $objQuery->setOption($option); |
|---|
| 240 | |
|---|
| 241 | $list_data = $objQuery->select($cols, $from, $where, $arrval); |
|---|
| 242 | |
|---|
| 243 | $max = count($list_data); |
|---|
| 244 | if (!isset($data)) $data = ""; |
|---|
| 245 | for($i = 0; $i < $max; $i++) { |
|---|
| 246 | // 各項目をCSV出力用に変換する。 |
|---|
| 247 | $data .= $this->lfMakeCSV($list_data[$i]); |
|---|
| 248 | } |
|---|
| 249 | return $data; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | // 各項目をCSV出力用に変換する。 |
|---|
| 253 | function lfMakeCSV($list) { |
|---|
| 254 | $line = ""; |
|---|
| 255 | |
|---|
| 256 | reset($list); |
|---|
| 257 | while(list($key, $val) = each($list)){ |
|---|
| 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 | reset($list); |
|---|
| 281 | while(list($key, $val) = each($list)){ |
|---|
| 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 | $line = ""; |
|---|
| 310 | reset($list); |
|---|
| 311 | while(list($key, $val) = each($list)){ |
|---|
| 312 | $tmp = ""; |
|---|
| 313 | switch($key) { |
|---|
| 314 | case 'status': |
|---|
| 315 | $tmp = $this->arrTrackBackStatus[$val]; |
|---|
| 316 | break; |
|---|
| 317 | default: |
|---|
| 318 | $tmp = $val; |
|---|
| 319 | break; |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | $tmp = ereg_replace("[\",]", " ", $tmp); |
|---|
| 323 | $line .= "\"".$tmp."\","; |
|---|
| 324 | } |
|---|
| 325 | // 文末の","を変換 |
|---|
| 326 | $line = $this->replaceLineSuffix($line); |
|---|
| 327 | return $line; |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | /** |
|---|
| 331 | * 行末の ',' を CRLF へ変換する. |
|---|
| 332 | * |
|---|
| 333 | * @access private |
|---|
| 334 | * @param string $line CSV出力用の1行分の文字列 |
|---|
| 335 | * @return string 行末の ',' を CRLF に変換した文字列 |
|---|
| 336 | */ |
|---|
| 337 | function replaceLineSuffix($line) { |
|---|
| 338 | return mb_ereg_replace(",$", "\r\n", $line); |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | /** |
|---|
| 342 | * 項目情報を初期化する. |
|---|
| 343 | * |
|---|
| 344 | * @access private |
|---|
| 345 | * @return void |
|---|
| 346 | */ |
|---|
| 347 | function init() { |
|---|
| 348 | $this->arrSubnavi = array( |
|---|
| 349 | 1 => 'product', |
|---|
| 350 | 2 => 'customer', |
|---|
| 351 | 3 => 'order', |
|---|
| 352 | 4 => 'campaign', |
|---|
| 353 | 5 => 'category' |
|---|
| 354 | ); |
|---|
| 355 | |
|---|
| 356 | $this->arrSubnaviName = array( |
|---|
| 357 | 1 => '商品管理', |
|---|
| 358 | 2 => '顧客管理', |
|---|
| 359 | 3 => '受注管理', |
|---|
| 360 | 4 => 'キャンペーン', |
|---|
| 361 | 5 => 'カテゴリ' |
|---|
| 362 | ); |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | $this->arrREVIEW_CVSCOL = array( |
|---|
| 366 | 'B.name', |
|---|
| 367 | 'A.status', |
|---|
| 368 | 'A.create_date', |
|---|
| 369 | 'A.reviewer_name', |
|---|
| 370 | 'A.sex', |
|---|
| 371 | 'A.recommend_level', |
|---|
| 372 | 'A.title', |
|---|
| 373 | 'A.comment' |
|---|
| 374 | ); |
|---|
| 375 | |
|---|
| 376 | $this->arrREVIEW_CVSTITLE = array( |
|---|
| 377 | '商品名', |
|---|
| 378 | 'レビュー表示', |
|---|
| 379 | '投稿日', |
|---|
| 380 | '投稿者名', |
|---|
| 381 | '性別', |
|---|
| 382 | 'おすすめレベル', |
|---|
| 383 | 'タイトル', |
|---|
| 384 | 'コメント' |
|---|
| 385 | ); |
|---|
| 386 | |
|---|
| 387 | $this->arrTRACKBACK_CVSTITLE = array( |
|---|
| 388 | '商品名', |
|---|
| 389 | 'ブログ名', |
|---|
| 390 | 'ブログ記事タイトル', |
|---|
| 391 | 'ブログ記事内容', |
|---|
| 392 | '状態', |
|---|
| 393 | '投稿日' |
|---|
| 394 | ); |
|---|
| 395 | |
|---|
| 396 | $this->arrTRACKBACK_CVSCOL = array( |
|---|
| 397 | 'B.name', |
|---|
| 398 | 'A.blog_name', |
|---|
| 399 | 'A.title', |
|---|
| 400 | 'A.excerpt', |
|---|
| 401 | 'A.status', |
|---|
| 402 | 'A.create_date' |
|---|
| 403 | ); |
|---|
| 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 | ?> |
|---|