Changeset 17799


Ignore:
Timestamp:
2009/02/17 23:19:20 (15 years ago)
Author:
Seasoft
Message:

CSVダウンロードの改善(商品、カテゴリ)
・RFC4180 に準拠。

 http://xoops.ec-cube.net/modules/newbb/viewtopic.php?topic_id=3387&forum=11

・処理を極力「CSV 関連 のヘルパークラス(SC_Helper_CSV)」で行うように改良。

Location:
branches/comu-ver2/data/class
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/comu-ver2/data/class/helper/SC_Helper_CSV.php

    r17756 r17799  
    100100 
    101101 
    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); 
    114  
     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         
    115120        // 規格分類名一覧 
    116121        if (in_array('classcategory_id1', $arrOutputCols) || in_array('classcategory_id2', $arrOutputCols)) { 
     122            $objDb = new SC_Helper_DB_Ex(); 
    117123            $arrClassCatName = $objDb->sfGetIDValueList("dtb_classcategory", "classcategory_id", "name"); 
    118124        } 
    119125 
    120         if (!isset($data)) $data = ""; 
    121         for($i = 0; $i < $max; $i++) { 
     126        $outputArray = array(); 
     127         
     128        // ヘッダ行 
     129        $outputArray[] = $arrOutput['disp_name']; 
     130         
     131        // データ行 
     132        foreach ($dataRows as $row) { 
    122133            // 規格名1 
    123134            if (in_array('classcategory_id1', $arrOutputCols)) { 
    124                 $list_data[$i]['classcategory_id1'] = $arrClassCatName[$list_data[$i]['classcategory_id1']]; 
    125             } 
     135                $row['classcategory_id1'] = $arrClassCatName[$row['classcategory_id1']]; 
     136            } 
     137 
    126138            // 規格名2 
    127139            if (in_array('classcategory_id2', $arrOutputCols)) { 
    128                 $list_data[$i]['classcategory_id2'] = $arrClassCatName[$list_data[$i]['classcategory_id2']]; 
     140                $row['classcategory_id2'] = $arrClassCatName[$row['classcategory_id2']]; 
    129141            } 
    130142 
    131143            // カテゴリID 
    132144            if (in_array('category_id', $arrOutputCols)) { 
    133                 $arrCategory_id = $objQuery->getCol("dtb_product_categories", 
     145                $row['category_id'] = $objQuery->getCol("dtb_product_categories", 
    134146                                  "category_id", 
    135147                                  "product_id = ?", 
    136                                   array($list_data[$i]['product_id'])); 
    137  
    138                 for ($j = 0; $j < count($arrCategory_id); $j++) { 
    139                     $list_data[$i]['category_id'] .= $arrCategory_id[$j]; 
    140                     if ($j < count($arrCategory_id) - 1) { 
    141                         $list_data[$i]['category_id'] .= "|"; 
    142                     } 
    143                 } 
    144             } 
    145  
    146             // 各項目をCSV出力用に変換する。 
    147             $data .= $this->lfMakeProductsCSV($list_data[$i], $arrOutputCols); 
    148         } 
    149         return $data; 
     148                                  array($row['product_id'])); 
     149            } 
     150 
     151            $outputArray[] = $row; 
     152        } 
     153         
     154        // CSVを送信する。 
     155        $this->lfDownloadCsv($outputArray); 
     156         
     157        // 成功終了 
     158        return ture; 
    150159    } 
    151160 
     
    189198    } 
    190199 
    191     // CSV出力データを作成する。(カテゴリ) 
    192     function lfGetCategoryCSV($where, $option, $arrval, $arrOutputCols) { 
    193         $objDb = new SC_Helper_DB_Ex(); 
    194  
    195         $from = "dtb_category"; 
    196         $cols = SC_Utils_Ex::sfGetCommaList($arrOutputCols); 
    197  
    198         $objQuery = new SC_Query(); 
    199         $objQuery->setoption($option); 
    200  
    201         $list_data = $objQuery->select($cols, $from, $where, $arrval); 
    202         $max = count($list_data); 
    203  
    204         if (!isset($data)) $data = ""; 
    205         for($i = 0; $i < $max; $i++) { 
    206             // 各項目をCSV出力用に変換する。 
    207             $data .= $this->lfMakeCSV($list_data[$i]); 
    208         } 
    209         return $data; 
     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; 
    210232    } 
    211233 
     
    236258            $tmp = ""; 
    237259            switch($key) { 
    238             case 'order_pref': 
    239             case 'deliv_pref': 
    240                 $tmp = $this->arrPref[$val]; 
    241                 break; 
    242             default: 
    243                 $tmp = $val; 
    244                 break; 
     260                case 'order_pref': 
     261                case 'deliv_pref': 
     262                    $tmp = $this->arrPref[$val]; 
     263                    break; 
     264                default: 
     265                    $tmp = $val; 
     266                    break; 
    245267            } 
    246268 
     
    250272        // 文末の","を変換 
    251273        $line = $this->replaceLineSuffix($line); 
    252         return $line; 
    253     } 
    254  
    255     // 各項目をCSV出力用に変換する。(商品) 
    256     function lfMakeProductsCSV($list, $arrOutputCols) { 
    257         $line = ""; 
    258         if(is_array($list)) { 
    259             foreach($arrOutputCols as $key) { 
    260                 $tmp = ""; 
    261                 switch($key) { 
    262                 case 'point_rate': 
    263                     if($val == "") { 
    264                         $tmp = '0'; 
    265                     } else { 
    266                         $tmp = $list[$key]; 
    267                     } 
    268                     break; 
    269                 default: 
    270                     $tmp = $list[$key]; 
    271                     break; 
    272                 } 
    273  
    274                 $tmp = str_replace("\"", "\\\"", $tmp); 
    275                 $line .= "\"".$tmp."\","; 
    276             } 
    277             // 文末の","を変換 
    278             $line = $this->replaceLineSuffix($line); 
    279         } 
    280274        return $line; 
    281275    } 
     
    410404                                           ); 
    411405    } 
     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    } 
    412468} 
    413469?> 
  • branches/comu-ver2/data/class/pages/admin/products/LC_Page_Admin_Products.php

    r17667 r17799  
    168168 
    169169                    switch ($key) { 
    170                         case 'search_product_id':   // 商品ID 
     170                        case 'search_product_id': // 商品ID 
    171171                            $where .= " AND product_id = ?"; 
    172172                            $view_where .= " AND product_id = ?"; 
     
    183183                            $view_where = $where; 
    184184                            break; 
    185                         case 'search_name':         // 商品名 
     185                        case 'search_name': // 商品名 
    186186                            $where .= " AND name ILIKE ?"; 
    187187                            $view_where .= " AND name ILIKE ?"; 
    188188                            $arrval[] = "%$val%"; 
    189189                            break; 
    190                         case 'search_category_id':  // カテゴリー 
     190                        case 'search_category_id': // カテゴリー 
    191191                            list($tmp_where, $tmp_arrval) = $objDb->sfGetCatWhere($val); 
    192192                            if($tmp_where != "") { 
     
    196196                            } 
    197197                            break; 
    198                         case 'search_product_code': // 商品コード 
     198                        case 'search_product_code': // 商品コード 
    199199                            $where .= " AND product_id IN (SELECT product_id FROM dtb_products_class WHERE product_code ILIKE ? GROUP BY product_id)"; 
    200200                            $view_where .= " AND EXISTS (SELECT product_id FROM dtb_products_class as cls WHERE cls.product_code ILIKE ? AND dtb_products.product_id = cls.product_id GROUP BY cls.product_id )"; 
    201201                            $arrval[] = "%$val%"; 
    202202                            break; 
    203                         case 'search_startyear':    // 登録更新日(FROM) 
     203                        case 'search_startyear': // 登録更新日(FROM) 
    204204                            $date = SC_Utils_Ex::sfGetTimestamp($_POST['search_startyear'], $_POST['search_startmonth'], $_POST['search_startday']); 
    205205                            $where.= " AND update_date >= '" . $_POST['search_startyear'] . "/" . $_POST['search_startmonth']. "/" .$_POST['search_startday'] . "'"; 
    206206                            $view_where.= " AND update_date >= '" . $_POST['search_startyear'] . "/" . $_POST['search_startmonth']. "/" .$_POST['search_startday'] . "'"; 
    207207                            break; 
    208                         case 'search_endyear':      // 登録更新日(TO) 
     208                        case 'search_endyear': // 登録更新日(TO) 
    209209                            $date = SC_Utils_Ex::sfGetTimestamp($_POST['search_endyear'], $_POST['search_endmonth'], $_POST['search_endday']); 
    210210                            $date = date('Y/m/d', strtotime($date) + 86400); 
     
    212212                            $view_where.= " AND update_date < date('" . $date . "')"; 
    213213                            break; 
    214                         case 'search_product_flag': //種別 
     214                        case 'search_product_flag': //種別 
    215215                            global $arrSTATUS; 
    216216                            $search_product_flag = SC_Utils_Ex::sfSearchCheckBoxes($val); 
     
    221221                            } 
    222222                            break; 
    223                         case 'search_status':       // ステータス 
     223                        case 'search_status': // ステータス 
    224224                            $tmp_where = ""; 
    225225                            foreach ($val as $element){ 
     
    248248 
    249249                switch($_POST['mode']) { 
    250                 case 'csv': 
    251  
    252                     require_once(CLASS_EX_PATH . "helper_extends/SC_Helper_CSV_Ex.php"); 
    253  
    254                     $objCSV = new SC_Helper_CSV_Ex(); 
    255                     // オプションの指定 
    256                     $option = "ORDER BY $order"; 
    257                     // CSV出力タイトル行の作成 
    258                     $arrOutput = SC_Utils_Ex::sfSwapArray($objCSV->sfgetCsvOutput(1, " WHERE csv_id = 1 AND status = 1")); 
    259  
    260                     if (count($arrOutput) <= 0) break; 
    261  
    262                     $arrOutputCols = $arrOutput['col']; 
    263                     $arrOutputTitle = $arrOutput['disp_name']; 
    264  
    265                     $head = SC_Utils_Ex::sfGetCSVList($arrOutputTitle); 
    266  
    267                     $data = $objCSV->lfGetProductsCSV($where, $option, $arrval, $arrOutputCols); 
    268  
    269                     // CSVを送信する。 
    270                     SC_Utils_Ex::sfCSVDownload($head.$data); 
    271                     exit; 
    272                     break; 
    273                 case 'delete_all': 
    274                     // 検索結果をすべて削除 
    275                     $where = "product_id IN (SELECT product_id FROM vw_products_allclass_detail AS alldtl WHERE $where)"; 
    276                     $sqlval['del_flg'] = 1; 
    277                     $objQuery->update("dtb_products", $sqlval, $where, $arrval); 
    278                     $objQuery->delete("dtb_customer_favorite_products", $where, $arrval); 
    279                     break; 
    280                 default: 
    281                     // 読み込む列とテーブルの指定 
    282                     $col = "product_id, name, main_list_image, status, product_code_min, product_code_max, price02_min, price02_max, stock_min, stock_max, stock_unlimited_min, stock_unlimited_max, update_date"; 
    283                     $from = "vw_products_allclass_detail AS alldtl "; 
    284  
    285                     // 行数の取得 
    286                     $linemax = $objQuery->count("dtb_products", $view_where, $arrval); 
    287                     $this->tpl_linemax = $linemax;              // 何件が該当しました。表示用 
    288  
    289                     // ページ送りの処理 
    290                     if(is_numeric($_POST['search_page_max'])) { 
    291                         $page_max = $_POST['search_page_max']; 
    292                     } else { 
    293                         $page_max = SEARCH_PMAX; 
    294                     } 
    295  
    296                     // ページ送りの取得 
    297                     $objNavi = new SC_PageNavi($this->arrHidden['search_pageno'], $linemax, $page_max, "fnNaviSearchPage", NAVI_PMAX); 
    298                     $startno = $objNavi->start_row; 
    299                     $this->arrPagenavi = $objNavi->arrPagenavi; 
    300  
    301                     //キャンペーン商品検索時は、全結果の商品IDを変数に格納する 
    302                     if(isset($_POST['search_mode']) && $_POST['search_mode'] == 'campaign') { 
    303                         $arrRet = $objQuery->select($col, $from, $where, $arrval); 
    304                         if(count($arrRet) > 0) { 
    305                             $arrRet = sfSwapArray($arrRet); 
    306                             $pid = implode("-", $arrRet['product_id']); 
    307                             $this->arrHidden['campaign_product_id'] = $pid; 
     250                    case 'csv': 
     251                        require_once(CLASS_EX_PATH . "helper_extends/SC_Helper_CSV_Ex.php"); 
     252 
     253                        $objCSV = new SC_Helper_CSV_Ex(); 
     254 
     255                        // CSVを送信する。正常終了の場合、終了。 
     256                        $objCSV->sfDownloadProductsCsv($where, $arrval, $order) && exit; 
     257                         
     258                        break; 
     259                    case 'delete_all': 
     260                        // 検索結果をすべて削除 
     261                        $where = "product_id IN (SELECT product_id FROM vw_products_allclass_detail AS alldtl WHERE $where)"; 
     262                        $sqlval['del_flg'] = 1; 
     263                        $objQuery->update("dtb_products", $sqlval, $where, $arrval); 
     264                        $objQuery->delete("dtb_customer_favorite_products", $where, $arrval); 
     265                        break; 
     266                    default: 
     267                        // 読み込む列とテーブルの指定 
     268                        $col = "product_id, name, main_list_image, status, product_code_min, product_code_max, price02_min, price02_max, stock_min, stock_max, stock_unlimited_min, stock_unlimited_max, update_date"; 
     269                        $from = "vw_products_allclass_detail AS alldtl "; 
     270 
     271                        // 行数の取得 
     272                        $linemax = $objQuery->count("dtb_products", $view_where, $arrval); 
     273                        $this->tpl_linemax = $linemax; // 何件が該当しました。表示用 
     274 
     275                        // ページ送りの処理 
     276                        if(is_numeric($_POST['search_page_max'])) { 
     277                            $page_max = $_POST['search_page_max']; 
     278                        } else { 
     279                            $page_max = SEARCH_PMAX; 
    308280                        } 
    309                     } 
    310  
    311                     // 取得範囲の指定(開始行番号、行数のセット) 
    312                     //                    if(DB_TYPE != "mysql") $objQuery->setlimitoffset($page_max, $startno); 
    313                     $objQuery->setlimitoffset($page_max, $startno); 
    314                     // 表示順序 
    315                     $objQuery->setorder($order); 
    316  
    317                     // 検索結果の取得 
    318                     $this->arrProducts = $objQuery->select($col, $from, $where, $arrval); 
    319                      
    320                     // 各商品ごとのカテゴリIDを取得 
    321                     if (count($this->arrProducts) > 0) { 
    322                         foreach ($this->arrProducts as $key => $val) { 
    323                             $this->arrProducts[$key]["categories"] = $objDb->sfGetCategoryId($val["product_id"]); 
    324                             $objDb->g_category_on = false; 
     281 
     282                        // ページ送りの取得 
     283                        $objNavi = new SC_PageNavi($this->arrHidden['search_pageno'], $linemax, $page_max, "fnNaviSearchPage", NAVI_PMAX); 
     284                        $startno = $objNavi->start_row; 
     285                        $this->arrPagenavi = $objNavi->arrPagenavi; 
     286 
     287                        //キャンペーン商品検索時は、全結果の商品IDを変数に格納する 
     288                        if(isset($_POST['search_mode']) && $_POST['search_mode'] == 'campaign') { 
     289                            $arrRet = $objQuery->select($col, $from, $where, $arrval); 
     290                            if(count($arrRet) > 0) { 
     291                                $arrRet = sfSwapArray($arrRet); 
     292                                $pid = implode("-", $arrRet['product_id']); 
     293                                $this->arrHidden['campaign_product_id'] = $pid; 
     294                            } 
    325295                        } 
    326                     } 
     296 
     297                        // 取得範囲の指定(開始行番号、行数のセット) 
     298                        //                    if(DB_TYPE != "mysql") $objQuery->setlimitoffset($page_max, $startno); 
     299                        $objQuery->setlimitoffset($page_max, $startno); 
     300                        // 表示順序 
     301                        $objQuery->setorder($order); 
     302 
     303                        // 検索結果の取得 
     304                        $this->arrProducts = $objQuery->select($col, $from, $where, $arrval); 
     305                         
     306                        // 各商品ごとのカテゴリIDを取得 
     307                        if (count($this->arrProducts) > 0) { 
     308                            foreach ($this->arrProducts as $key => $val) { 
     309                                $this->arrProducts[$key]["categories"] = $objDb->sfGetCategoryId($val["product_id"]); 
     310                                $objDb->g_category_on = false; 
     311                            } 
     312                        } 
    327313                } 
    328314            } 
     
    351337        global $objPage; 
    352338        /* 
    353          *  文字列の変換 
    354          *  K :  「半角(ハンカク)片仮名」を「全角片仮名」に変換 
    355          *  C :  「全角ひら仮名」を「全角かた仮名」に変換 
    356          *  V :  濁点付きの文字を一文字に変換。"K","H"と共に使用します 
    357          *  n :  「全角」数字を「半角(ハンカク)」に変換 
     339         *  文字列の変換 
     340         *  K :  「半角(ハンカク)片仮名」を「全角片仮名」に変換 
     341         *  C :  「全角ひら仮名」を「全角かた仮名」に変換 
     342         *  V :  濁点付きの文字を一文字に変換。"K","H"と共に使用します 
     343         *  n :  「全角」数字を「半角(ハンカク)」に変換 
    358344         */ 
    359345        $arrConvList['search_name'] = "KVa"; 
  • branches/comu-ver2/data/class/pages/admin/products/LC_Page_Admin_Products_Category.php

    r17190 r17799  
    180180 
    181181            $objCSV = new SC_Helper_CSV_Ex(); 
    182             // オプションの指定 
    183             $option = "ORDER BY rank DESC"; 
    184             // CSV出力タイトル行の作成 
    185             $arrOutput = SC_Utils_Ex::sfSwapArray($objCSV->sfgetCsvOutput(5, " WHERE csv_id = 5 AND status = 1")); 
    186  
    187             if (count($arrOutput) <= 0) break; 
    188  
    189             $arrOutputCols = $arrOutput['col']; 
    190             $arrOutputTitle = $arrOutput['disp_name']; 
    191  
    192             $head = SC_Utils_Ex::sfGetCSVList($arrOutputTitle); 
    193  
    194             $where = "del_flg = 0"; 
    195             $data = $objCSV->lfGetCategoryCSV($where, $option, $arrval, $arrOutputCols); 
    196  
    197             // CSVを送信する。 
    198             SC_Utils_Ex::sfCSVDownload($head.$data, 'category'); 
    199             exit; 
     182 
     183            // CSVを送信する。正常終了の場合、終了。 
     184            $objCSV->sfDownloadCategoryCsv() && exit; 
     185 
    200186            break; 
    201187        default: 
Note: See TracChangeset for help on using the changeset viewer.