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


/**
 * カート関連
 *
 * @package     Onedepo.Db.Cart
 * @author      FUCO H.Tominaga
 * @access      public
 */
class Onedepo_Db_Usermanage_Request
{
    /**
     * DBアクセスオブジェクト
     *
     * @var     object
     * @access  private
     */
    var $dbaccess;

    /**
     * 全件取得
     * 
     * @param string $sort ソートキー
     * @return object 取得レコード
     */

    function selectAll($conditions, $sort)
    {
//注意　検索条件として引き数に格納されている$conditions[4]は、「探す」の場合は２、「買う」の場合は１、「全て」の場合は３としている
        switch($sort){
            case 'requestno':
            case 'closedate':
            case 'requestprice':
            case 'requestcondition':
                $sort = "t_r.".$sort;
            break;

            case 'itemnm':
                $sort = "t_i.".$sort;
            break;
            
            case 'lastnmkana':
                $sort = "t_m.".$sort;
            break;
            
            default:
                $sort = "t_r.requestno";                        
        }


        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $sql  = "SELECT t_r.requestno, t_r.itemcd, t_i.itemnm, t_r.requestmembercd, t_r.requestprice, t_r.requestcondition, t_r.closedate, t_r.requestdiv, t_m.lastnm, t_m.firstnm ";
        $sql .= "FROM t_request t_r, t_item_basic_attr t_i, t_member_basic_attr t_m ";
        $sql .= "WHERE t_r.delflg = FALSE ";

        if(isset($conditions)){
            //sql文の条件となるパラメータのセット
            if($conditions[0] != "") {
                $params[] = $conditions[0];
                $sql .= " AND t_r.requestno = ?";
            }
            if($conditions[1] != "") {
                $params[] = $conditions[1];
                $sql .= " AND t_i.itemnm LIKE ?";
            }
            if($conditions[2] != "") {
                $params[] = $conditions[2];
                $sql .= " AND t_r.closedate >= ?";
            }
            if($conditions[3] != "") {
                $params[] = $conditions[3];
                $sql .= " AND t_r.requestcondition >= ?";
            }
            if($conditions[4] != "") {
            	$params[] = $conditions[4];
            	$sql .= " AND t_r.requestdiv <> ? ";
            }
            if($conditions[5] != "") {
                $params[] = $conditions[5];
                $sql .= " AND t_r.requestmembercd = ?";
            }
            if($conditions[6] != "") {
                $params[] = $conditions[6];
                $sql .= " AND t_m.lastnm || t_m.firstnm LIKE ?";
            }
        }
        $sql .=	" AND t_r.itemcd = t_i.itemcd ";
        $sql .=	" AND t_r.requestmembercd = t_m.membercd ";
        $sql .= " ORDER BY %s;";
        $sql = sprintf($sql, $sort);
        $log->debug($sql, "Onedepo_Db_Usermanage_Request#selectAll");
        if(isset($params)){
            $data = $db->getAll($sql, $params);
        } else {
            $data = $db->getAll($sql);
        }
        $this->dbaccess->doClose($db);
        return $data;
    }

//会員テーブル削除
    function delete($requestno) {
        $ret = true;
        $log =& LogFactory::getLog();

        $params = array($requestno);
        $sql = "update t_request SET delflg=true where requestno=?;";

        $db = $this->dbaccess->doConnect();
        $res = $db->query($sql, $params);
        if (DB::isError($res) || !$res) {
            $log->trace("update rollback", "Onedepo_Db_Usermanage_Request#delete");

            $db->rollback();
            $ret = false;
        }
        $log->trace("commit", "Onedepo_Db_Usermanage_Request#delete");
        $db->commit();
        $this->dbaccess->doClose($db);
        return $ret;
    }
}
?>
