<?php

class OItem
{
    
    
    static public function importFromCsv($provider,$category,$textCsv){
        global $INI;
        $csv = explode("\r\n",$textCsv);
        
        DB::Delete('item', array('provider_id'=>$provider,'category_id'=>$category));
        $tb = new Table($INI['db']['prefix'] . "item");
        $succ=0;
        $err =0;
        foreach($csv as $fields){
            if($fields){
                $fields = explode(',',$fields);
                if(count($fields)!=11){
                    $err++;
                    continue;
                }
                $tb->provider_id = $provider;
                $tb->category_id = $category;
                $tb->item_number = $fields[0];
                $tb->season = $fields[1];
                $tb->origin = $fields[2];
                $tb->bclass = $fields[3];
                $tb->gender = $fields[4];
                $tb->color = $fields[5];
                $tb->series = $fields[6];
                $tb->sclass = $fields[7];
                $tb->size = $fields[8];
                $tb->quantity = $fields[9];
                $tb->weight = $fields[10];
                $tb->state = 'Y';
                $tb->image = '';
                $re = $tb->Insert("provider_id","category_id","item_number","season","origin","bclass","gender","color","series","sclass","size","quantity","weight","state","image");
            }
            ($re==false)? $err++ : $succ++;
        }
        if($succ!=0) Session::Set('success',$succ.' 个产品数据上传成功！');
        if($err!=0) Session::Set('error',$err.' 个产品数据上传失败！');
    }
    public static function getItem($item_id){
        global $tb_pf;
        return Table::Fetch($tb_pf.'item',$item_id);
    }
    public static function delItem(){
        global $tb_pf;
        return Table::Delete($tb_pf.'item',$item_id);
    }

}

function ut1(){
    include_once(dirname(dirname(dirname(__FILE__)))."/app.php");
    $csvfile = IMG_ROOT . "/test.csv";
    
    $csv = file($csvfile);
    $csv = file_get_contents($csvfile);
    $csv = explode("\r\n",$csv);
    
    
    
    echo "<pre>";
    
    print_r( $csv);
    echo "</pre>";
}

function ut2(){
    global $INI;
    include_once(dirname(dirname(dirname(__FILE__)))."/app.php");
    $csvfile = IMG_ROOT . "/test.csv";
    $csv = file_get_contents($csvfile);
    $sys = OSystem::getINI();
    $com = $sys['sys']['sys'];
    eval($com);
    OItem::importFromCsv($csv);
}
function ut3(){
    $a = array("a" => "aaa","b" => "bbb");
    
    eval("\$b = \$a;");
    echo $b;

}
function ut4(){
    global $INI;
    global $tb_pf;
    include_once(dirname(dirname(dirname(__FILE__)))."/app.php");

    $item = OItem::getItem(30);
    print_r($item);

    echo "<br />" . $tb_pf;
}

?>
