[15535] | 1 | <?php |
---|
| 2 | /* |
---|
[22206] | 3 | * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved. |
---|
[15535] | 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 | // {{{ constructor |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * デフォルトコンストラクタ. |
---|
| 30 | */ |
---|
[21767] | 31 | function __construct() { |
---|
[15535] | 32 | $this->init(); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | // }}} |
---|
| 36 | // {{{ functions |
---|
| 37 | |
---|
[15566] | 38 | /** |
---|
[20280] | 39 | * 項目情報を初期化する. |
---|
| 40 | * |
---|
| 41 | * @access private |
---|
| 42 | * @return void |
---|
| 43 | */ |
---|
| 44 | function init() { |
---|
| 45 | $this->arrSubnavi = array( |
---|
[21527] | 46 | 1 => 'product', |
---|
| 47 | 2 => 'customer', |
---|
| 48 | 3 => 'order', |
---|
| 49 | 4 => 'review', |
---|
| 50 | 5 => 'category', |
---|
| 51 | ); |
---|
[20280] | 52 | |
---|
| 53 | $this->arrSubnaviName = array( |
---|
[21527] | 54 | 1 => '商品管理', |
---|
| 55 | 2 => '会員管理', |
---|
| 56 | 3 => '受注管理', |
---|
| 57 | 4 => 'レビュー', |
---|
| 58 | 5 => 'カテゴリ', |
---|
| 59 | ); |
---|
[20280] | 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
[20737] | 63 | * CSVファイルを送信する |
---|
| 64 | * |
---|
| 65 | * @param integer $csv_id CSVフォーマットID |
---|
| 66 | * @param string $where WHERE条件文 |
---|
[21118] | 67 | * @param array $arrVal プリペアドステートメントの実行時に使用される配列。配列の要素数は、クエリ内のプレースホルダの数と同じでなければなりません。 |
---|
[20737] | 68 | * @param string $order ORDER文 |
---|
| 69 | * @param boolean $is_download true:ダウンロード用出力までさせる false:CSVの内容を返す(旧方式、メモリを食います。) |
---|
| 70 | * @return mixed $is_download = true時 成功失敗フラグ(boolean) 、$is_downalod = false時 string |
---|
| 71 | */ |
---|
[21514] | 72 | function sfDownloadCsv($csv_id, $where = '', $arrVal = array(), $order = '', $is_download = false) { |
---|
[20737] | 73 | // CSV出力タイトル行の作成 |
---|
| 74 | $arrOutput = SC_Utils_Ex::sfSwapArray($this->sfGetCsvOutput($csv_id, 'status = ' . CSV_COLUMN_STATUS_FLG_ENABLE)); |
---|
| 75 | if (count($arrOutput) <= 0) return false; // 失敗終了 |
---|
| 76 | $arrOutputCols = $arrOutput['col']; |
---|
| 77 | |
---|
| 78 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[21118] | 79 | $objQuery->setOrder($order); |
---|
[20737] | 80 | $cols = SC_Utils_Ex::sfGetCommaList($arrOutputCols, true); |
---|
| 81 | |
---|
| 82 | // TODO: 固有処理 なんかエレガントな処理にしたい |
---|
[21441] | 83 | if ($csv_id == '1') { |
---|
[20737] | 84 | //商品の場合 |
---|
| 85 | $objProduct = new SC_Product_Ex(); |
---|
| 86 | // このWhereを足さないと無効な規格も出力される。現行仕様と合わせる為追加。 |
---|
| 87 | $inner_where = 'dtb_products_class.del_flg = 0'; |
---|
| 88 | $sql = $objQuery->getSql($cols, $objProduct->prdclsSQL($inner_where),$where); |
---|
[21686] | 89 | } else if ($csv_id == '2') { |
---|
[20953] | 90 | // 会員の場合 |
---|
[21514] | 91 | $sql = 'SELECT ' . $cols . ' FROM dtb_customer ' . $where; |
---|
[21686] | 92 | } else if ($csv_id == '3') { |
---|
[20737] | 93 | // 注文の場合 |
---|
[21514] | 94 | $sql = 'SELECT ' . $cols . ' FROM dtb_order ' . $where; |
---|
[21686] | 95 | } else if ($csv_id == '4') { |
---|
[20737] | 96 | // レビューの場合 |
---|
[21514] | 97 | $sql = 'SELECT ' . $cols . ' FROM dtb_review AS A INNER JOIN dtb_products AS B on A.product_id = B.product_id ' . $where; |
---|
[21686] | 98 | } else if ($csv_id == '5') { |
---|
[20737] | 99 | // カテゴリの場合 |
---|
[21514] | 100 | $sql = 'SELECT ' . $cols . ' FROM dtb_category ' . $where; |
---|
[20737] | 101 | } |
---|
[22267] | 102 | // 「getSqlを使っているcsv_id=1」以外で「order指定」がある場合は末尾に付与 |
---|
| 103 | // 全csv_idでgetSqlを使用して生成するよう統一する場合は当メソッドの呼び元も要修正 |
---|
| 104 | $sql = ($csv_id != '1' && strlen($order) >= 1) ? $sql.' order by '.$order : $sql; |
---|
[20737] | 105 | // 固有処理ここまで |
---|
| 106 | return $this->sfDownloadCsvFromSql($sql, $arrVal, $this->arrSubnavi[$csv_id], $arrOutput['disp_name'], $is_download); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /** |
---|
[15566] | 110 | * CSV 項目を出力する. |
---|
| 111 | * |
---|
| 112 | * @param integer $csv_id CSV ID |
---|
| 113 | * @param string $where SQL の WHERE 句 |
---|
| 114 | * @param array $arrVal WHERE 句の要素 |
---|
[19752] | 115 | * @param array $order SQL の ORDER BY 句 |
---|
[15566] | 116 | * @return array CSV 項目の配列 |
---|
| 117 | */ |
---|
[21514] | 118 | function sfGetCsvOutput($csv_id = '', $where = '', $arrVal = array(), $order = 'rank, no') { |
---|
[20507] | 119 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[20540] | 120 | |
---|
[19752] | 121 | $cols = 'no, csv_id, col, disp_name, rank, status, rw_flg, mb_convert_kana_option, size_const_type, error_check_types'; |
---|
| 122 | $table = 'dtb_csv'; |
---|
[20540] | 123 | |
---|
[21441] | 124 | if (SC_Utils_Ex::sfIsInt($csv_id)) { |
---|
[21514] | 125 | if ($where == '') { |
---|
| 126 | $where = 'csv_id = ?'; |
---|
[21441] | 127 | } else { |
---|
[19752] | 128 | $where = "$where AND csv_id = ?"; |
---|
| 129 | } |
---|
[18616] | 130 | $arrVal[] = $csv_id; |
---|
[15566] | 131 | } |
---|
[19752] | 132 | $objQuery->setOrder($order); |
---|
[20540] | 133 | |
---|
[19752] | 134 | $arrRet = $objQuery->select($cols, $table, $where, $arrVal); |
---|
| 135 | return $arrRet; |
---|
[15566] | 136 | } |
---|
| 137 | |
---|
[19752] | 138 | /** |
---|
| 139 | * CSVが出力設定でインポート可能かのチェック |
---|
| 140 | * |
---|
[19999] | 141 | * @param array sfGetCsvOutputで取得した内容(またはそれと同等の配列) |
---|
[19752] | 142 | * @return boolean true:インポート可能、false:インポート不可 |
---|
| 143 | */ |
---|
| 144 | function sfIsImportCSVFrame(&$arrCSVFrame) { |
---|
| 145 | $result = true; |
---|
[21927] | 146 | foreach ($arrCSVFrame as $val) { |
---|
[21684] | 147 | if ($val['status'] != CSV_COLUMN_STATUS_FLG_ENABLE |
---|
| 148 | && $val['rw_flg'] == CSV_COLUMN_RW_FLG_READ_WRITE |
---|
| 149 | && $val['error_check_types'] != '' |
---|
| 150 | && strpos(strtoupper($val['error_check_types']), 'EXIST_CHECK') !== FALSE |
---|
| 151 | ) { |
---|
[19752] | 152 | //必須フィールド |
---|
| 153 | $result = false; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | return $result; |
---|
| 157 | } |
---|
[20540] | 158 | |
---|
[19752] | 159 | /** |
---|
| 160 | * CSVが出力設定で更新可能かのチェック |
---|
| 161 | * |
---|
[19999] | 162 | * @param array sfGetCsvOutputで取得した内容(またはそれと同等の配列) |
---|
[19752] | 163 | * @return boolean true:更新可能、false:新規追加のみ不可 |
---|
| 164 | */ |
---|
| 165 | function sfIsUpdateCSVFrame(&$arrCSVFrame) { |
---|
| 166 | $result = true; |
---|
[21927] | 167 | foreach ($arrCSVFrame as $val) { |
---|
[21684] | 168 | if ($val['status'] != CSV_COLUMN_STATUS_FLG_ENABLE |
---|
| 169 | && $val['rw_flg'] == CSV_COLUMN_RW_FLG_KEY_FIELD |
---|
| 170 | ) { |
---|
[19752] | 171 | //キーフィールド |
---|
| 172 | $result = false; |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | return $result; |
---|
| 176 | } |
---|
[20540] | 177 | |
---|
[19752] | 178 | /** |
---|
| 179 | * CSVファイルのカウント数を得る. |
---|
| 180 | * |
---|
| 181 | * @param resource $fp fopenを使用して作成したファイルポインタ |
---|
| 182 | * @return integer CSV のカウント数 |
---|
| 183 | */ |
---|
| 184 | function sfGetCSVRecordCount($fp) { |
---|
| 185 | $count = 0; |
---|
[21441] | 186 | while (!feof($fp)) { |
---|
[19752] | 187 | $arrCSV = fgetcsv($fp, CSV_LINE_MAX); |
---|
| 188 | $count++; |
---|
| 189 | } |
---|
| 190 | // ファイルポインタを戻す |
---|
| 191 | if (rewind($fp)) { |
---|
| 192 | return $count-1; |
---|
| 193 | } else { |
---|
| 194 | return FALSE; |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
[20280] | 198 | /** |
---|
| 199 | * CSV作成 テンポラリファイル出力 コールバック関数 |
---|
| 200 | * |
---|
| 201 | * @param mixed $data 出力データ |
---|
| 202 | * @return boolean true (true:固定 false:中断) |
---|
| 203 | */ |
---|
| 204 | function cbOutputCSV($data) { |
---|
[19742] | 205 | $line = $this->sfArrayToCSV($data); |
---|
| 206 | $line = mb_convert_encoding($line, 'SJIS-Win'); |
---|
| 207 | $line .= "\r\n"; |
---|
| 208 | fwrite($this->fpOutput, $line); |
---|
[22021] | 209 | SC_Utils_Ex::extendTimeOut(); |
---|
[19742] | 210 | return true; |
---|
| 211 | } |
---|
[18616] | 212 | |
---|
[20280] | 213 | /** |
---|
[20318] | 214 | * SQL文からクエリ実行し CSVファイルを送信する |
---|
| 215 | * |
---|
| 216 | * @param integer $sql SQL文 |
---|
[21118] | 217 | * @param array $arrVal プリペアドステートメントの実行時に使用される配列。配列の要素数は、クエリ内のプレースホルダの数と同じでなければなりません。 |
---|
[20318] | 218 | * @param string $file_head ファイル名の頭に付ける文字列 |
---|
| 219 | * @param array $arrHeader ヘッダ出力列配列 |
---|
| 220 | * @param boolean $is_download true:ダウンロード用出力までさせる false:CSVの内容を返す(旧方式、メモリを食います。) |
---|
| 221 | * @return mixed $is_download = true時 成功失敗フラグ(boolean) 、$is_downalod = false時 string |
---|
| 222 | */ |
---|
| 223 | function sfDownloadCsvFromSql($sql, $arrVal = array(), $file_head = 'csv', $arrHeader = array(), $is_download = false) { |
---|
[20507] | 224 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[20318] | 225 | |
---|
[20280] | 226 | // ヘッダ構築 |
---|
[21441] | 227 | if (is_array($arrHeader)) { |
---|
[20318] | 228 | $header = $this->sfArrayToCSV($arrHeader); |
---|
| 229 | $header = mb_convert_encoding($header, 'SJIS-Win'); |
---|
| 230 | $header .= "\r\n"; |
---|
| 231 | } |
---|
[20285] | 232 | |
---|
[20276] | 233 | //テンポラリファイル作成 |
---|
| 234 | // TODO: パフォーマンス向上には、ストリームを使うようにすると良い |
---|
[20318] | 235 | // 環境要件がPHPバージョン5.1以上になったら使うように変えても良いかと |
---|
[20276] | 236 | // fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+'); |
---|
[20318] | 237 | $tmp_filename = tempnam(CSV_TEMP_REALDIR, $file_head . '_csv'); |
---|
[21514] | 238 | $this->fpOutput = fopen($tmp_filename, 'w+'); |
---|
[20276] | 239 | fwrite($this->fpOutput, $header); |
---|
[20280] | 240 | $objQuery->doCallbackAll(array(&$this, 'cbOutputCSV'), $sql, $arrVal); |
---|
[20276] | 241 | |
---|
| 242 | fclose($this->fpOutput); |
---|
| 243 | |
---|
[21441] | 244 | if ($is_download) { |
---|
[20276] | 245 | // CSVを送信する。 |
---|
[21480] | 246 | $this->lfDownloadCSVFile($tmp_filename, $file_head . '_'); |
---|
[20276] | 247 | $res = true; |
---|
[21441] | 248 | } else { |
---|
[21757] | 249 | $res = SC_Helper_FileManager_Ex::sfReadFile($tmp_filename); |
---|
[20276] | 250 | } |
---|
[20318] | 251 | |
---|
[20276] | 252 | //テンポラリファイル削除 |
---|
| 253 | unlink($tmp_filename); |
---|
| 254 | return $res; |
---|
| 255 | } |
---|
| 256 | |
---|
[15535] | 257 | /** |
---|
[17799] | 258 | * 1次元配列を1行のCSVとして返す |
---|
| 259 | * 参考: http://jp.php.net/fputcsv |
---|
[20737] | 260 | * |
---|
| 261 | * @param array $fields データ1次元配列 |
---|
| 262 | * @param string $delimiter |
---|
| 263 | * @param string $enclosure |
---|
| 264 | * @param string $arrayDelimiter |
---|
| 265 | * @return string 結果行 |
---|
[17799] | 266 | */ |
---|
| 267 | function sfArrayToCsv($fields, $delimiter = ',', $enclosure = '"', $arrayDelimiter = '|') { |
---|
[21442] | 268 | if (strlen($delimiter) != 1) { |
---|
[17799] | 269 | trigger_error('delimiter must be a single character', E_USER_WARNING); |
---|
[21514] | 270 | return ''; |
---|
[17799] | 271 | } |
---|
[20540] | 272 | |
---|
[21442] | 273 | if (strlen($enclosure) < 1) { |
---|
[17799] | 274 | trigger_error('enclosure must be a single character', E_USER_WARNING); |
---|
[21514] | 275 | return ''; |
---|
[17799] | 276 | } |
---|
[20540] | 277 | |
---|
[21935] | 278 | foreach ($fields as $key => $value) { |
---|
[17799] | 279 | $field =& $fields[$key]; |
---|
[20540] | 280 | |
---|
[17799] | 281 | // 配列を「|」区切りの文字列に変換する |
---|
| 282 | if (is_array($field)) { |
---|
| 283 | $field = implode($arrayDelimiter, $field); |
---|
| 284 | } |
---|
[20540] | 285 | |
---|
[17799] | 286 | /* enclose a field that contains a delimiter, an enclosure character, or a newline */ |
---|
[21527] | 287 | if (is_string($field) |
---|
[17799] | 288 | && preg_match('/[' . preg_quote($delimiter) . preg_quote($enclosure) . '\\s]/', $field) |
---|
| 289 | ) { |
---|
| 290 | $field = $enclosure . preg_replace('/' . preg_quote($enclosure) . '/', $enclosure . $enclosure, $field) . $enclosure; |
---|
| 291 | } |
---|
| 292 | } |
---|
[20540] | 293 | |
---|
[17799] | 294 | return implode($delimiter, $fields); |
---|
| 295 | } |
---|
[20540] | 296 | |
---|
[17799] | 297 | /** |
---|
[20737] | 298 | * 配列データのCSVを送信する。 |
---|
| 299 | * |
---|
| 300 | * @param array $fields データ配列 |
---|
| 301 | * @param string $prefix |
---|
| 302 | * @return void |
---|
[17799] | 303 | */ |
---|
[21514] | 304 | function lfDownloadCsv($arrData, $prefix = '') { |
---|
[17799] | 305 | |
---|
[21514] | 306 | if ($prefix == '') { |
---|
[20484] | 307 | $dir_name = SC_Utils_Ex::sfUpDirName(); |
---|
[21514] | 308 | $file_name = $dir_name . date('ymdHis') .'.csv'; |
---|
[17799] | 309 | } else { |
---|
[21514] | 310 | $file_name = $prefix . date('ymdHis') .'.csv'; |
---|
[17799] | 311 | } |
---|
| 312 | |
---|
| 313 | /* HTTPヘッダの出力 */ |
---|
| 314 | Header("Content-disposition: attachment; filename=${file_name}"); |
---|
| 315 | Header("Content-type: application/octet-stream; name=${file_name}"); |
---|
[21514] | 316 | Header('Cache-Control: '); |
---|
| 317 | Header('Pragma: '); |
---|
[17799] | 318 | |
---|
| 319 | /* データを出力 */ |
---|
[20737] | 320 | foreach ($arrData as $lineArray) { |
---|
[17799] | 321 | $lineString = $this->sfArrayToCsv($lineArray); |
---|
| 322 | $lineString = mb_convert_encoding($lineString, 'SJIS-Win'); |
---|
[18616] | 323 | echo $lineString . "\r\n"; |
---|
[17799] | 324 | } |
---|
| 325 | } |
---|
[20540] | 326 | |
---|
[19742] | 327 | /** |
---|
| 328 | * CSVファイルを送信する。 |
---|
[20737] | 329 | * |
---|
| 330 | * @param string $filepath 送信するファイルのフルパス |
---|
| 331 | * @param string $prefix |
---|
| 332 | * @return void |
---|
[19742] | 333 | */ |
---|
[21514] | 334 | function lfDownloadCSVFile($filepath, $prefix = '') { |
---|
| 335 | $file_name = $prefix . date('YmdHis') . '.csv'; |
---|
[20540] | 336 | |
---|
[19742] | 337 | /* HTTPヘッダの出力 */ |
---|
[21927] | 338 | Header("Content-disposition: attachment; filename={$file_name}"); |
---|
| 339 | Header("Content-type: application/octet-stream; name={$file_name}"); |
---|
[21514] | 340 | Header('Cache-Control: '); |
---|
| 341 | Header('Pragma: '); |
---|
[20540] | 342 | |
---|
[19742] | 343 | /* データを出力 */ |
---|
| 344 | // file_get_contentsはメモリマッピングも自動的に使ってくれるので高速&省メモリ |
---|
| 345 | echo file_get_contents($filepath); |
---|
| 346 | } |
---|
[15542] | 347 | } |
---|