<?php
/**
 * @copyright ONE/DEPO Project
 * @license   PHP License 3.01 <http://www.php.net/license/3_01.txt>
 */


/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * データインポート
 *
 * @package     Admin.Itemmanage.Import.Item.Result
 * @author      netnee watanabe
 * @access      public
 */
class Admin_Itemmanage_Import_Item_Result
{
    var $title;
    var $parentAction;
    var $pagename;

    var $dataimport;
    var $actionChain;

    var $tempimportdata;
    var $importfilename;
    var $importdata;
    var $exectime;
    var $datediv;

    /**
     * エラーチェックとデータ整形のみ
     *
     * @access  public
     */
    function execute()
    {
        $errorList =& $this->actionChain->getCurErrorList();

        $log =& LogFactory::getLog();
        $log->debug("Admin_Itemmanage_Import_Item_Resultが実行されました", "Admin_Itemmanage_Import_Item_Result#execute");

        if (!is_dir(IMPORT_TEMP)) {
            if (!is_file(IMPORTDIR_TEMP)) {
                mkdir(IMPORTDIR_TEMP);
            } else {
                $errorList -> add("FILE_NOTFOUND", "作業ディレクトリが作成できません");
                return 'input';
            }
        }
        $tempfile = IMPORTDIR_TEMP ."/temp_". $this->importfilename;
        if (!is_file($tempfile)) {
	  //$log->debug("file notfound ".$tempfile, "Admin_Itemmanage_Import_Result#execute");
            $errorList -> add("FILE_NOTFOUND", "ファイルが見つかりません");
            return 'input';
        }

        if (!is_dir(IMPORTDIR_WORK)) {
            if (!is_file(IMPORTDIR_WORK)) {
                mkdir(IMPORTDIR_WORK);
            } else {
                $errorList -> add("FILE_NOTFOUND", "作業ディレクトリが作成できません");
                return 'input';
            }
        }
        $workfile = IMPORTDIR_WORK ."/work_". $this->importfilename;
        @copy($tempfile, $workfile);
        if (!is_file($workfile)) {
	  //$log->debug("file notfound ".$workfile, "Admin_Itemmanage_Import_Result#execute");
            $errorList -> add("FILE_NOTFOUND", "ファイルが見つかりません");
            return 'input';
        }

        // CSVファイルを2次元配列へ。
        $this->tempimportdata = file($workfile);
        $this->importdata = array();
        $row = 1;
        foreach ($this->tempimportdata as $key => $val) {
            $val = trim($val);
            $val = preg_replace("/[\n]/", "", $val);
            $expdata = preg_split("/,/", $val);
            $bak_key = NULL;
            
            foreach ($expdata as $key2 => $val2) {
                if(preg_match("/^\"/", $val2) AND preg_match("/\"$/", $val2)) {
                    $expdata[$key2] = trim($val2,"\"");
                } else if($bak_key==NULL) {
                    if(preg_match("/^\"/", $val2) ) {
                        $expdata[$key2] = ltrim($val2,"\"");
                        $expdata[$key2] .= ",";
                        $bak_key = $key2;
                    }
                } else if($bak_key!=NULL) {
                    $expdata[$bak_key] .= $val2;
                    if(preg_match("/\"$/",$val2) ) {
                        $expdata[$bak_key] = rtrim($expdata[$bak_key],"\"");
                        $bak_key = NULL;
                    }
                    unset($expdata[$key2]);
                    
                }
            }
            
            $expdata2 = NULL;
            foreach ($expdata as $val2) {
                $expdata2[] = $val2;
            }
/*
            if (count($expdata2) < 63) {
                $this->importdata = array();
                //$log->debug("missing itemcount line". $row. "Admin_Itemmanage_Import_Item_Result#execute");
                $errorList -> add("MISSING_ITEMCOUNT", $row ."行目の項目数が足りません。");

                return 'input';
            }
*/
            $this->importdata[] = $this->makeImportdataArr($expdata2);
            $row++;
        }
        
        $cnt = count($this->importdata);
        // --

        if ($cnt <= 0) {
            $this->importdata = array();
            //$log->debug("import no data ", "Admin_Itemmanage_Import_Result#execute");
            $errorList -> add("IMPORT_NODATA ", "処理出来るデータがありません。");
            return 'input';
        }
        if ($cnt > IMPORT_MAX) {
            $this->importdata = array();
            //$log->debug("import max count over ". $cnt, "Admin_Itemmanage_Import_Result#execute");
            $errorList -> add("IMPORT_MAX ", "処理件数が多すぎます。". IMPORT_MAX ."件以内にしてください。");
            return 'input';
        }


        return 'success';
    }

    function doImport()
    {
        if (empty($this->importdata)) return "";
        $this->filenm = date('Ymd-His') .".csv";

        $time_start = $this->execTime();

        $result = $this->dataimport->doImport_item($this->importdata, $this->filenm, $this->tempimportdata, $this->datediv);
        $time_end = $this->execTime();
        $this->exectime = $time_end - $time_start;


        //$workfile = IMPORTDIR_WORK ."/work_". $this->importfilename;
        //$savefile = IMPORTDIR_SAVED ."/". $this->filenm;
        //@rename($workfile, $savefile);

        return $result;
    }

    function execTime()
    {
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }

    function getExecTime()
    {
        return round($this->exectime, 2);
    }

    function makeImportdataArr($data)
    {
        $ret = array();
        
        // ジャンルは配列に。
        @list($ret["code"],     // コード
            $ret["itemnm"],
            $ret["itemnmkana"],
            $ret["itemexp"],
            
            $ret["web_comment"],
            $ret["releasedate"],
            
            $ret["makeritemcd"], // 品番
            $ret["variationnm"],
            $ret["jancd"],
            $ret["salestartdate"],
            $ret["orderclosedate"],
            $ret["strict_comment"],
            
            $ret["genrecd"][0],  // ジャンル
            $ret["genrenm"][0],
            $ret["genrecd"][1],
            $ret["genrenm"][1],
            $ret["genrecd"][2],
            $ret["genrenm"][2],
            $ret["genrecd"][3],
            $ret["genrenm"][3],
            $ret["genrecd"][4],
            $ret["genrenm"][4],

            $ret["image_s"],    // 画像
            $ret["image_m"],
            $ret["image_l"]
        ) = $data;

        $log =& LogFactory::getLog();
        //$log->debug(print_r($ret,true), "Admin_Itemmanage_Import_Result#execute");
        return $ret;
    }

}
?>
