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


/**
 * 画像マスタテーブル関連
 *
 * @package     Onedepo.Db.Image
 * @author      netnee T.Otogawa<tomokazu.otogawa@netnee.jp>
 * @access      public
 */
class Onedepo_Db_Image
{
    var $dbaccess;


    function selectKey($typediv, $imgsize, $general, $generalsub, $sell, $stock){
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Image#selectKey";

        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        if(empty($generalsub)){
            $generalsub = 0;
        }
        
        if(empty($sell)){
            $sell = 0;
        }
        
        if(empty($stock)){
            $stock = 0;
        }

        $params = array($typediv, $general, $generalsub, $imgsize, $sell, $stock);
        
        
        $sql  = "SELECT typediv, generalcd, generalsubcd, imgsizecd, sellercd, updcnt, stockcd ";
        $sql .= "  FROM t_image ";
        $sql .= " WHERE typediv = ? ";
        $sql .= "   AND generalcd = ?";
        $sql .= "   AND generalsubcd = ?";
        $sql .= "   AND imgsizecd = ?";
        $sql .= "   AND sellercd = ?";
        $sql .= "   AND stockcd = ?;";
        $data = $db->getRow($sql, $params);
        $this->dbaccess->doClose($db);

        return $data;
    }


    
    function getItemImageForItemcd($itemcd) {
        $ret = true;
        $log =& LogFactory::getLog();

        $db = $this->dbaccess->doConnect();

        $sql = "SELECT generalcd, imgsizecd 
                FROM t_image
                WHERE typediv=1 AND generalcd in (!)";
        $params = array($itemcd);

        $data = $db->getAssoc($sql, false, $params, DB_FETCHMODE_ORDERED, true);
        if (DB::isError($data)) {
            die($data->getMessage());
        }

        $this->dbaccess->doClose($db);
        return $data;
    }

    function getImageForGeneralcd($div, $generalcd) {
        $ret = true;
        $log =& LogFactory::getLog();

        $db = $this->dbaccess->doConnect();

        $sql = "SELECT generalcd, imgsizecd 
                FROM t_image
                WHERE typediv=? AND generalcd in (!)";
        $params = array($div,$generalcd);

        $data = $db->getAssoc($sql, false, $params, DB_FETCHMODE_ORDERED, true);
        if (DB::isError($data)) {
            die($data->getMessage());
        }

        $this->dbaccess->doClose($db);
        return $data;
    }
    
    function insert($typediv, $general, $imgsize, $seller, $upduser, $gensub) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Image#insert";
        
        // 更新ユーザ(仮商品以外)
        if ($typediv != 9) {
            $adminauth =& AdminAuthext::getInstance();
            $upduser = $adminauth->getAuthData("membernm");
            $log->debug("membernm", $upduser);
        }
        //--

        if(empty($gensub)){
            $gensub = 0;
        }

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($typediv, $general, $gensub, $imgsize, $seller, $upduser);
        $sql  = "select * from f_image_update(1, ?, ?, ?, ?, ?, ?, NULL);";

        $res = $db->getOne($sql, $params);
        
        if (DB::isError($res) || $res < 0) {
            $log->error("rollback". $res->userinfo, $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);

        $db->commit();
        $this->dbaccess->doClose($db);
        return true;

    }
    
    //出品者用画像登録
    //stockcd(在庫CD)含む
    function insert_stockcd($typediv, $general, $imgsize, $seller, $upduser, $gensub, $stock) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Image#insert";

        if(empty($gensub)){
            $gensub = 0;
        }

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($typediv, $general, $gensub, $imgsize, $seller, $stock, $upduser);
        $sql  = "select * from f_image_update_stockcd(1, ?, ?, ?, ?, ?, ?, ?, NULL);";

        $res = $db->getOne($sql, $params);
        
        if (DB::isError($res) || $res < 0) {
            $log->error("rollback". $res->userinfo, $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);

        $db->commit();
        $this->dbaccess->doClose($db);
        return true;

    }
    
    //出品者用画像削除
    function delete_stockcd($typediv, $general, $imgsize, $seller, $upduser, $gensub, $stock) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Image#insert";

        if(empty($gensub)){
            $gensub = 0;
        }

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($typediv, $general, $gensub, $imgsize, $seller, $stock, $upduser);
        $sql  = "select * from f_image_update_stockcd(3, ?, ?, ?, ?, ?, ?, ?, NULL);";
        $res = $db->getOne($sql, $params);
        
        if (DB::isError($res) || $res < 0) {
            $log->error("rollback". $res->userinfo, $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);

        $db->commit();
        $this->dbaccess->doClose($db);
        return true;

    }
    
/*    function update($typediv, $general, $imgsize, $seller, $updcnt, $upduser, $gensub="NULL") {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Genre#update";

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($code, $name, $dispsort, $upduser, $_SERVER['REMOTE_ADDR'], $updcnt);

        $sql  = "SELECT * FROM f_genre_update(2, ?, ?, ?, ?, ?, ?);";
        $log->debug($sql, $func_name);
        $log->debug(print_r($params, true), $func_name);

        $res = $db->getOne($sql, $params);
        
        $log->debug(print_r($res, true), $func_name);
        if (DB::isError($res) || $res < 0) {
            $log->debug("rollback", $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);

        $db->commit();
        $this->dbaccess->doClose($db);
        return true;
    }
    */
}
?>
