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


/**
 * 仮商品テーブル関連
 *
 * @package     Onedepo.Db.Preitem
 * @author      netnee H.watanabe<hiroshi.watanabe@netnee.jp>
 * @access      public
 */
class Onedepo_Db_Preitem
{

    var $dbaccess;
    
    var $resultData;  // 検索結果
    var $resultCount; // 検索件数
    
    
    var $_itemcd;     // 本商品コード
    var $_preitemcd;  // 仮商品コード
    
    
    // 商品コードを設定
    function setitemcd($itemcd) {
        $this->_itemcd = $itemcd;
        return true;
    }
    
    // 商品コードを返却
    function getitemcd() {
        return $this->_itemcd;
    }
    
    // 仮商品コードを設定
    function setPreitemcd($preitemcd) {
        $this->_preitemcd = $preitemcd;
        return true;
    }
    
    // 仮商品コードを返却
    function getPreitemcd() {
        return $this->_preitemcd;
    }
    
    /**
     * 仮商品テーブル 検索
     */
    function getSearchResults($itemnm, $sellercd, $genre_select, $download, $sort, $from, $to) 
    {
        $where = array();
        $params = array();

        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Db_Preitem#getSearchResults");
        
        // AND検索の為に半角空白で分割
        $itemnm = mb_convert_kana($itemnm, "s", "UTF-8");
        $itemnms = explode(" ", $itemnm);
        
        // WHERE句で「=」を使うもの
        $eq = array(
                  "sellercd" => $sellercd,
                  "download" => $download
              );
        $this->makeEqualParams($eq, $where, $params);
        //--
        
        // LIKEを使うもの
        $like = array("itemnm" => $itemnms);
        $this->makeLikeParams($like, $where, $params);
        //--

        $w = $p = array();
        
        // ジャンル
        if (!empty($genre_select)) {
            $w[] = "(SELECT itemcd FROM t_genre_item WHERE genrecd in (!) GROUP BY itemcd)";
            $p[] = implode(",", $genre_select);
        }
        //--
        
        if (!empty($w)) {
            $where[] = " basic.itemcd IN (". implode(" INTERSECT ALL ", $w) .")";
            foreach ($p as $v) $params[] = $v;
        }
        
        if ($sort == "title")         $sortkey = "itemnm, variationnm";
        else if ($sort == "seller")  $sortkey = "sellercd, preitemcd";
        else if ($sort == "readd")  $sortkey = "adddate desc";
        else                          $sortkey = "preitemcd";

        
        if (!is_numeric($from)) {
            $this->from = 1;
        }
        $from = ($from-1)*$to;

        //$c = $this->item->getSearchResultsCount($where, $params);
        //if ($resultmax>0 && $c > $resultmax) {
        //    $this->overcount=$c;
        //    return false;
        //}
        
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $sql = "SELECT
                  preitem.preitemcd,
                  preitem.itemnm,
                  preitem.itemnmkana,
                  preitem.itemexp,

                  preitem.basic_option01,
                  preitem.basic_option02,
                  preitem.basic_option03,
                  preitem.basic_option04,
                  preitem.basic_option05,
                  preitem.basic_option06,
                  preitem.basic_option07,
                  preitem.basic_option08,
                  preitem.basic_option09,
                  preitem.basic_option10,

                  preitem.variationnm,
                  preitem.makeritemcd,
                  preitem.jancd,
                  preitem.salestartdate,
                  preitem.orderclosedate,
                  preitem.comment,

                  preitem.struct_option01,
                  preitem.struct_option02,
                  preitem.struct_option03,
                  preitem.struct_option04,
                  preitem.struct_option05,
                  preitem.struct_option06,
                  preitem.struct_option07,
                  preitem.struct_option08,
                  preitem.struct_option09,
                  preitem.struct_option10,
                  preitem.ReleaseDate,
                  preitem.web_Comment,
                  preitem.sellercd,
                  preitem.download,
                  preitem.adddate,
                  member.lastnm,
                  member.firstnm
               ";
        $t1 = " FROM
                  t_pre_item_attr preitem
                LEFT JOIN t_member_basic_attr member
                        ON preitem.sellercd=member.membercd
                        AND member.delflg=FALSE
                WHERE
                  preitem.delflg = FALSE ";
        foreach ($where as $key => $val) {
            $t1 .= " AND $val";
        }
        $sql .= $t1;

        $sql .= " ORDER BY ". $sortkey;
        if(!is_null($to)) {
            $sql .= " LIMIT ". $to  ." OFFSET ". $from;
        }
        
    //print $sql ."<br>";
	//print_r($params); print "<Br>";
        $data = $db->getAll($sql, $params);
        if (DB::isError($data)) {
            $log->debug($data->userinfo, "search error");
            die($data->getMessage());
        }

        $nolimit = "SELECT count(preitem.preitemcd) as count ". $t1;
        $cnt = $db->getOne($nolimit, $params);
        $data += array("recordcount" => $cnt);

        $this->dbaccess->doClose($db);
        return $data;
    }
    
    /**
     * 仮商品cdで1件取得
     */
    function getOnePreItem($preitemcd) 
    {
        $where = array();
        $params = array();

        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Db_Preitem#getOnePreItem");
                
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $sql = "SELECT
                  preitem.preitemcd,
                  preitem.itemnm,
                  preitem.itemnmkana,
                  preitem.itemexp,

                  preitem.basic_option01,
                  preitem.basic_option02,
                  preitem.basic_option03,
                  preitem.basic_option04,
                  preitem.basic_option05,
                  preitem.basic_option06,
                  preitem.basic_option07,
                  preitem.basic_option08,
                  preitem.basic_option09,
                  preitem.basic_option10,

                  preitem.variationnm,
                  preitem.makeritemcd,
                  preitem.jancd,
                  preitem.salestartdate,
                  preitem.orderclosedate,
                  preitem.comment,

                  preitem.struct_option01,
                  preitem.struct_option02,
                  preitem.struct_option03,
                  preitem.struct_option04,
                  preitem.struct_option05,
                  preitem.struct_option06,
                  preitem.struct_option07,
                  preitem.struct_option08,
                  preitem.struct_option09,
                  preitem.struct_option10,
                  preitem.ReleaseDate,
                  preitem.web_Comment,
                  preitem.sellercd,
                  preitem.download,
                  preitem.adddate,
                  preitem.updcnt,
                  member.lastnm,
                  member.firstnm
               ";
        $sql .= " FROM
                  t_pre_item_attr preitem
                LEFT JOIN t_member_basic_attr member
                        ON preitem.sellercd=member.membercd
                        AND member.delflg=FALSE
                WHERE
                  preitem.delflg = FALSE ";
        
        $sql .= " AND preitemcd = ?";
        $params = array($preitemcd);
        
        $data = $db->getRow($sql, $params);
        
        if (DB::isError($data)) {
            $log->debug($data->userinfo, "search error");
            die($data->getMessage());
        }

        $this->dbaccess->doClose($db);
        return $data;
    }
    
    /**
     * 仮商品ジャンル取得
     */
    function getOnePreGenre($preitemcd) 
    {
        $where = array();
        $params = array();

        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Db_Preitem#getOnePreGenre");
                
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $sql = "SELECT
                  pre_g_i.genrecd,
                  genre.genrenm
               ";
        $sql .= " FROM
                  t_pre_genre_item pre_g_i
                LEFT JOIN t_genre genre
                        ON pre_g_i.genrecd=genre.genrecd
                        AND genre.delflg=FALSE
                WHERE
                  pre_g_i.delflg = FALSE ";
        
        $sql .= " AND preitemcd = ?";
        $params = array($preitemcd);
        
        $data = $db->getAll($sql, $params);
        
        if (DB::isError($data)) {
            $log->debug($data->userinfo, "search error");
            die($data->getMessage());
        }

        $this->dbaccess->doClose($db);
        return $data;
    }
    
    
    
    /**
     * 出品商品登録依頼に追加
     *
     */
    function insert($itemnm, $itemnmkana, $itemexp, 
                    $basic_option01, $basic_option02, $basic_option03, $basic_option04, $basic_option05,
                    $basic_option06, $basic_option07, $basic_option08, $basic_option09, $basic_option10,
                    
                    $upduser, $releasedate,  $web_comment, $genre,
                    $variationnm, $makeritemcd, $jancd, $salestartdate, $orderclosedate, $struct_comment, 
                    $struct_option01, $struct_option02, $struct_option03, $struct_option04, $struct_option05,
                    $struct_option06, $struct_option07, $struct_option08, $struct_option09, $struct_option10,
                    $download, $sellercd) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Preitem#insert";

        $db = $this->dbaccess->doConnect();
        $db->autoCommit(false);
        $db->query("BEGIN;");


        // 商品基本属性 ==>>
        if (empty($itemexp)) $itemexp = NULL;
        
        if (empty($basic_option01)) $basic_option01 = NULL; 
        if (empty($basic_option02)) $basic_option02 = NULL;
        if (empty($basic_option03)) $basic_option03 = NULL;
        if (empty($basic_option04)) $basic_option04 = NULL;
        if (empty($basic_option05)) $basic_option05 = NULL;
        if (empty($basic_option06)) $basic_option06 = NULL;
        if (empty($basic_option07)) $basic_option07 = NULL;
        if (empty($basic_option08)) $basic_option08 = NULL;
        if (empty($basic_option09)) $basic_option09 = NULL;
        if (empty($basic_option10)) $basic_option10 = NULL;
        
        // 商品ウェブ属性 ==>>
        if (empty($rankingrate)){ $rankingrate = NULL; }
        if (empty($releasedate)){ $releasedate = NULL; }

        // 構成情報 ==>>
        if (empty($struct_option01)) $struct_option01 = NULL;
        if (empty($struct_option02)) $struct_option02 = NULL;
        if (empty($struct_option03)) $struct_option03 = NULL;
        if (empty($struct_option04)) $struct_option04 = NULL;
        if (empty($struct_option05)) $struct_option05 = NULL;
        if (empty($struct_option06)) $struct_option06 = NULL;
        if (empty($struct_option07)) $struct_option07 = NULL;
        if (empty($struct_option08)) $struct_option08 = NULL;
        if (empty($struct_option09)) $struct_option09 = NULL;
        if (empty($struct_option10)) $struct_option10 = NULL;
        if (empty($makerprice))    $makerprice = NULL;
        if (empty($salestartdate)) $salestartdate = NULL;
        if (empty($orderclosedate)) $orderclosedate = NULL;
        
        $params = array(
            // basic
            $itemnm, $itemnmkana, $itemexp, 
            $basic_option01, $basic_option02, $basic_option03, 
            $basic_option04, $basic_option05, $basic_option06, 
            $basic_option07, $basic_option08, $basic_option09, 
            $basic_option10, 
            // struct
            $variationnm, $makeritemcd, $jancd, 
            $salestartdate, $orderclosedate, $struct_comment, 
            $struct_option01, $struct_option02, $struct_option03, 
            $struct_option04, $struct_option05, $struct_option06, 
            $struct_option07, $struct_option08, $struct_option09, 
            $struct_option10,
            // web
            $releasedate, $web_comment,
            // seller
            $sellercd,
            // download
            $download,
            $upduser, $_SERVER['REMOTE_ADDR']
        );
        
        $sql  = "select * from f_pre_item_attr_update(1, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL);";
        
        
        $res = $db->getRow($sql, $params);
        if (DB::isError($res) || $res[0] < 0) {
            $db->rollback();
            $db->query("ROLLBACK;");
            $log->error("f_pre_item_attr_update#rollback : ". $res->userinfo, $func_name);
            return false;
        }
        $fixcode = $res[2];
        
        // 登録された仮商品CDを設定
        $this->setPreitemcd($res[2]);
                        
        // ジャンル未選択時は登録動作を行わない
        if (!empty($genre)){
            $genre = implode(",", $genre);
            $genre = "{" . $genre . "}";

            $params = array($fixcode, $genre, $upduser, $_SERVER['REMOTE_ADDR']);
            $sql  = "select * from f_pre_genre_item_update(?, ?, ?, ?);";
            $res = $db->getRow($sql, $params);

            if (DB::isError($res) || $res[0] < 0) {
                $db->rollback();
                $db->query("ROLLBACK;");
                $log->error("f_pre_genre_item_update#rollback : ". $res->userinfo, $func_name);
                return false;
            }
        }

        $db->query("COMMIT;");
        $db->commit();
        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);

        return true;
    }
    
    
    /**
     * 承認実行
     * $preitemcd の仮商品を承認する
     */
    function accept($preitemcd) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Preitem#accept";

        // 更新ユーザ
        $adminauth =& AdminAuthext::getInstance();
        $upduser = $adminauth->getAuthData("membernm");
        $log->debug("membernm", $upduser);
        //--

        $db = $this->dbaccess->doConnect();
        $db->autoCommit(false);
        $db->query("BEGIN;");
        
        // 仮商品 カラムが空ならNULL
        $preitem = $this->getOnePreItem($preitemcd);
        foreach($preitem as &$val) {
            if($val == "") $val = NULL;
        }
//var_dump($preitem);
        $pregenre = $this->getOnePreGenre($preitemcd);
//var_dump($pregenre);

        $params = array($preitem['itemnm'], $preitem['itemnmkana'], NULL, $preitem['itemexp'], $preitem['basic_option01'], $preitem['basic_option02'],
                        $preitem['basic_option03'], $preitem['basic_option04'], $preitem['basic_option05'], $preitem['basic_option06'], $preitem['basic_option07'],
                        $preitem['basic_option08'], $preitem['basic_option09'], $preitem['basic_option10'], $upduser, $_SERVER['REMOTE_ADDR']);
        $sql  = "select * from f_item_basic_attr_update(1, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL);";
        $res = $db->getRow($sql, $params);

        if (DB::isError($res) || $res[0] < 0) {
            $db->rollback();
            $db->query("ROLLBACK;");
            $log->error("f_item_basic_attr_update#rollback : ". $res->userinfo, $func_name);
            return false;
        }
        
        // 本商品コード設定
        $fixcode = $res[2];
        $this->setitemcd($fixcode);

        $params = array($fixcode, $preitem['releasedate'], NULL, $preitem['web_comment'], $upduser, $_SERVER['REMOTE_ADDR']);
        $sql  = "select * from f_item_web_attf_update(1, ?, ?, ?, ?, ?, ?, NULL);";
        $res = $db->getRow($sql, $params);

        if (DB::isError($res) || $res[0] < 0) {
            $db->rollback();
            $db->query("ROLLBACK;");
            $log->error("f_item_web_attf_update#rollback : ". $res->userinfo, $func_name);
            return false;
        }
        
        // ジャンル未登録時は登録動作を行わない
        if (count($pregenre) >= 1){
            $genre = array();
            foreach($pregenre as $val) {
                $genre[] = $val["genrecd"];
            }
            $genre = implode(",", $genre);
            $genre = "{" . $genre . "}";

            $params = array($fixcode, $genre, $upduser, $_SERVER['REMOTE_ADDR']);
            $sql  = "select * from f_genre_item_update(?, ?, ?, ?);";
            $res = $db->getRow($sql, $params);

            if (DB::isError($res) || $res[0] < 0) {
                $db->rollback();
                $db->query("ROLLBACK;");
                $log->error("f_genre_item_update#rollback : ". $res->userinfo, $func_name);
                return false;
            }
        }


        // 構成情報 ==>>
        // 発売日に 公開日を追加している事に注意
        $params = array($fixcode, $preitem['variationnm'], $preitem['makeritemcd'], $preitem['jancd'], $preitem['releasedate'], $preitem['orderclosedate'],
                        NULL, $preitem['comment'], $preitem['struct_option01'], $preitem['struct_option02'],
                        $preitem['struct_option03'], $preitem['struct_option04'], $preitem['struct_option05'], $preitem['struct_option06'], $preitem['struct_option07'],
                        $preitem['struct_option08'], $preitem['struct_option09'], $preitem['struct_option10'], $upduser, $_SERVER['REMOTE_ADDR']);
        $sql  = "SELECT * FROM f_item_struct_update(1, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL);";
        $res = $db->getRow($sql, $params);

        if (DB::isError($res) || $res[0] < 0) {
            $db->rollback();
            $db->query("ROLLBACK;");
            $log->error("f_item_struct_update#rollback : ". $res->userinfo, $func_name);
            return false;
        }

        // 構成コード取得
        $fixcode2 = $res[2];
        
        // 出品者品目追加
        $params = array($fixcode, $preitem['sellercd'], $upduser, $_SERVER['REMOTE_ADDR']);
        $sql  = "SELECT * FROM f_seller_item_update(1, ?, ?, ?, ?);";
        $res = $db->getRow($sql, $params);

        if (DB::isError($res) || $res[0] < 0) {
            $db->rollback();
            $db->query("ROLLBACK;");
            $log->error("f_seller_item_update#rollback : ". $res->userinfo, $func_name);
            return false;
        }

        
        // 仮商品削除
        $params = array($preitemcd, $upduser, $_SERVER['REMOTE_ADDR'], $preitem['updcnt']);
        $sql  = "SELECT * FROM f_pre_item_attr_update( 3, ?, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, ?, ?, ?);";
        $res = $db->getRow($sql, $params);

        if (DB::isError($res) || $res[0] < 0) {
            $db->rollback();
            $db->query("ROLLBACK;");
            $log->error("f_pre_item_attr_update#rollback : ". $res->userinfo, $func_name);
            return false;
        }

        // ダウンロード販売
        if($preitem['download']=='1') {

            // ダウンロード商品テーブル (ファイル存在フラグtrueで追加)
            $sql  = "SELECT * FROM f_dl_item_update(1, ?, ?, ?, ?, ?, ?, ?, ?, NULL);";
            $params = array($fixcode, $fixcode2, true, DL_COUNT, DL_REPIOD_DAYS, '', $upduser, $_SERVER['REMOTE_ADDR']);
            
            $res = $db->getRow($sql, $params);
            if (DB::isError($res) || $res[0] < 0) {
                $db->rollback();
                $db->query("ROLLBACK;");
                $log->error("f_dl_item_update#rollback : ". $res->userinfo, $func_name);
                return false;
            }
/*            
            // 仕入見出しテーブル
            $sql  = "SELECT * FROM f_stock_title_update(1, NULL, NULL, NULL, 1, ?, ?, NULL);";
            $params = array($upduser, $_SERVER['REMOTE_ADDR']);
            
            $res = $db->getRow($sql, $params);
            if (DB::isError($res) || $res[0] < 0) {
                $db->rollback();
                $db->query("ROLLBACK;");
                $log->error("f_stock_title_update#rollback : ". $res->userinfo, $func_name);
                return false;
            }
            
            // 追加した仕入れ見出しコード(stocktitlecd)取得
            $fixcode3 = $res[2];
            
            // 出品在庫明細
            $sql  = "SELECT * FROM f_ex_stock_detail_update(1, ?, ?, NULL, 9999,?, 10000, 1, 2000, 0, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,?,?, NULL);";
            $params = array($fixcode, $fixcode2, $fixcode3, $upduser, $_SERVER['REMOTE_ADDR']);
            
            $res = $db->getRow($sql, $params);
            if (DB::isError($res) || $res[0] < 0) {
                $db->rollback();
                $db->query("ROLLBACK;");
                $log->error("f_ex_stock_detail_update#rollback : ". $res->userinfo, $func_name);
                return false;
            }
*/            
            // ファイル名変更
            $basedir = IMAGE_DIR .'/7/';
            $filename = glob($basedir .'preitem_'. $preitemcd  .".*", GLOB_NOSORT);

            $pathinfo = pathInfo($filename[0]);

            // コピー先ファイル名 preitem_[登録された仮商品コード].*
            $from_name = 'preitem_' . $preitemcd .'.'. $pathinfo['extension'];
            // コピー先ファイル名 preitem_[登録された仮商品コード].*
            $to_name = $fixcode .'_'. $fixcode2 .'.'. $pathinfo['extension'];
            
            $from = $basedir . $from_name;
            $to = $basedir . $to_name;

            // コピー成功後、元ファイル削除
            if( is_file($from) ) {
                if( copy($from, $to) ) {
                    unlink($from);
                } else {
                    $db->rollback();
                    $db->query("ROLLBACK;");
                    $log->error("filecopy#rollback : ". "copy($from, $to)", $func_name);
                    return false;
                }
            } else {
                $db->rollback();
                $db->query("ROLLBACK;");
                $log->error("filecopy#rollback : ". "is_file($from)", $func_name);
                return false;
            }
            
        }
        if($this->setImage($preitemcd, $fixcode, 'S')) {
            // DB insert
            $params = array(1, $fixcode, 0, 'S', 0, $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;
            }
        }
        if($this->setImage($preitemcd, $fixcode, 'M')) {
            // DB insert
            $params = array(1, $fixcode, 0, 'M', 0, $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;
            }
        }
        if($this->setImage($preitemcd, $fixcode, 'L')) {
            // DB insert
            $params = array(1, $fixcode, 0, 'L', 0, $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;
            }
        }

        $db->query("COMMIT;");
        $db->commit();
        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);

        return true;
    }
    
    
    /**
     * 画像を移動する
     *
     */
    function setImage($preitemcd, $itemcd, $size) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Preitem#setImage";

        // ファイル名変更
        //$basedir = IMAGE_DIR .'/7/';
        $filename = glob(IMAGE_DIR .'/9/'. $preitemcd . $size .".jpg", GLOB_NOSORT);

        $pathinfo = pathInfo($filename[0]);

        // コピー先ファイル名 preitem_[登録された仮商品コード].*
        $from_name = $preitemcd . $size .'.jpg';
        // コピー先ファイル名 preitem_[登録された仮商品コード].*
        $to_name = $itemcd . $size .'.jpg';
        
        $from = IMAGE_DIR .'/9/' . $from_name;
        $to =   IMAGE_DIR .'/1/' . $to_name;

        // コピー成功後、元ファイル削除
        if( is_file($from) ) {
            if( copy($from, $to) ) {
                unlink($from);
            } else {
                return false;
            }
        } else {
            return false;
        }
        return true;
    }
    
    /**
     * = を使用するクエリ
     *
     */
    function makeEqualParams($basearr, &$where, &$params)
    {
        foreach ($basearr as $key => $val) {
            if(!empty($val)) {
                if (is_array($val)) {

                    foreach ($val as $v) {
                        $where[] = $key ." = ?";
                        $params[] = $v;
                    }

                } else {
                    $where[] = $key ." = ?";
                    $params[] = $val;
                }
            }
        }
    }

    /**
     * LIKE 部分一致
     *
     */
    function makeLikeParams($basearr, &$where, &$params)
    {
        foreach ($basearr as $key => $val) {
            if(!empty($val)) {
                if (is_array($val)) {

                    foreach ($val as $v) {
                        if (!empty($v)) {
                            $where[] = $key ." LIKE ?";
                            $params[] = '%'. $v .'%';
                        }
                    }

                } else {
                    $where[] = $key ." LIKE ?";
                    $params[] = '%'. $val .'%';
                }
            }
        }
    }
}
?>
