<?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.Result
 * @author      netnee T.Otogawa <tomokazu.otogawa@netnee.jp>
 * @access      public
 */
class Admin_Itemmanage_Import_Result
{
    var $title;
    var $pagename;

    var $dataimport;
    var $actionChain;

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

    var $release;
    var $images;

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

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


        $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';
        }

        $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);

            if (count($expdata) < REQUIRE_ITEMCOUNT) {
                $this->importdata = array();
                //$log->debug("missing itemcount line". $row. "Admin_Itemmanage_Import_Result#execute");
                $errorList -> add("MISSING_ITEMCOUNT", $row ."行目の項目数が足りません。");

                return 'input';
            }
            $this->importdata[] = $this->makeImportdataArr($expdata);
            $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($this->importdata, $this->filenm, $this->tempimportdata, $this->release, $this->images);

        $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["target"], 
             $ret["itemcd"], 
             $ret["itemnm"],
             $ret["itemnmkana"],
             $ret["makercd"],
             $ret["makernm"],
             $ret["labelcd"],  //
             $ret["labelnm"],  //
             $ret["seriescd"],
             $ret["seriesnm"],
             $ret["kubuncd"],
             $ret["kubunnm"],
             $ret["jushincd"],
             $ret["jushinnm"],
             $ret["jushinno"],
             $ret["workguide"],
             $ret["eirincd"],
             $ret["eirinnm"],
             $ret["subtitle"],  // 
             $ret["rank"],
             $ret["webcomment"],  // webコメント
             $ret["mediacd"],
             $ret["medianm"],
             $ret["makerno"],
             $ret["jancd"],
             $ret["isbncd"],
             $ret["saledate"],
             $ret["reserveenddate"],
             $ret["sellcd"],
             $ret["sellnm"],
             $ret["rectime"],
             $ret["makerprice"],
             $ret["sellprice"],
             $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["castcd"][0],
             $ret["castnm"][0],
             $ret["castcd"][1],
             $ret["castnm"][1],
             $ret["castcd"][2],
             $ret["castnm"][2],
             $ret["castcd"][3],
             $ret["castnm"][3],
             $ret["castcd"][4],
             $ret["castnm"][4],
             $ret["directorcd"][0],
             $ret["directornm"][0],
             $ret["imglarge"],
             $ret["imgmedium"],
             $ret["imgsmall"]) = $data;


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

}
?>
