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


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


    /**
     * 区分に一致したものを全件取得
     *
     * @param int $div 区分
     * @param string $sort ソートキー
     * @return object 取得レコード
     */
    function selectDivAll($div, $sort="disppriority")
    {
        $log =& LogFactory::getLog();

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

        $params = array($div);

        // プレースホルダを使うとエラーが出るんで、とりあえずソートは固定で。
        $sql = "select namecd, name, disppriority from t_name where namediv= ? and delflg=false order by disppriority";
        $data = $db->getAll($sql, $params);
        if (DB::isError($data)) {
            die($data->getMessage());
        }

        $this->dbaccess->doClose($db);

        return $data;
    }

    /**
     * 区分に一致したものを指定された条件で取得
     *
     * @param int $div 区分
     * @param string $where 条件
     * @param string $sort ソートキー
     * @return object 取得レコード
     */
    function selectDivTerm($div, $where="",  $sort="disppriority")
    {
        $log =& LogFactory::getLog();

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

        $params = array($div);

        // プレースホルダを使うとエラーが出るんで、とりあえずソートは固定で。
        $sql  = "select namecd, name, disppriority
                   from t_name
                  where namediv= ? and delflg=false ";
        $sql .= $where;
        $sql .= " order by disppriority";
        $data = $db->getAll($sql, $params);
        if (DB::isError($data)) {
            die($data->getMessage());
        }

        $this->dbaccess->doClose($db);

        return $data;
    }

    function selectKey($namediv, $namecode)
    {
        $log =& LogFactory::getLog();

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

        $params = array($namediv, $namecode);

        $sql = "select namecd, name, disppriority, updcnt from t_name where namediv=? AND namecd=? and delflg=false";
        $data = $db->getRow($sql, $params);
        if (DB::isError($data)) {
            die($data->getMessage());
        }

        $this->dbaccess->doClose($db);

        return $data;
    }

    function selectKeyOfName($namediv, $name)
    {
        $log =& LogFactory::getLog();

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

        $params = array($namediv, $name);

        $sql = "select namecd, name, disppriority, updcnt from t_name where namediv=? AND name LIKE ? and delflg=false";
        $data = $db->getRow($sql, $params);
        if (DB::isError($data)) {
            die($data->getMessage());
            return false;
        }

        $this->dbaccess->doClose($db);

        return $data;
    }

    /**
     * オプション情報取得
     *
     * @return object 取得レコード
     */
    function selectOptionTerm($term1,$term2)
    {
        $log =& LogFactory::getLog();

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

        $sql  = "SELECT namediv, namecd, name, disppriority FROM t_name ";
        $sql .= " WHERE namediv > ! AND namediv < ! AND delflg = false ";
        $sql .= "  ORDER BY namediv, disppriority";
        $params = array($term1,$term2);
        $data = $db->getAll($sql, $params);
        if (DB::isError($data)) {
            die($data->getMessage());
        }
        $this->dbaccess->doClose($db);

        return $data;
    }

    /**
     * オプション情報の一覧を取得
     *
     * @return object 取得レコード
     */
    function selectOptionList()
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Name#selectOptionList";

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

        $sql = "SELECT namediv, namecd, name, disppriority, updcnt FROM t_name ";
        $sql = $sql." WHERE namecd = 1 AND namediv >= 101 AND namediv < 300 AND delflg = false ";
        $sql = $sql." ORDER BY namediv, disppriority";
        $data = $db->getAll($sql);
        if (DB::isError($data)) {
            $log->error($data->getMessage(), $func_name);
            die($data->getMessage());
        }
        $this->dbaccess->doClose($db);

//        $log->debug(print_r($data, true), $func_name);
        return $data;
    }

    /**
     * オプション情報データの一覧を取得
     *
     * @param string  $namediv 名称区分
     *
     * @return object 取得レコード
     */
    function selectOptionListData($namediv)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Name#selectOptionListData";

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

        $params = array($namediv);

        $sql = "SELECT namediv, namecd, name, disppriority, updcnt FROM t_name ";
        $sql = $sql." WHERE namediv = ? AND namecd <> 1 AND delflg = false ";
        $sql = $sql."  ORDER BY disppriority";
        $data = $db->getAll($sql, $params);
        if (DB::isError($data)) {
            $log->error($data->getMessage(), $func_name);
            die($data->getMessage());
        }
        $this->dbaccess->doClose($db);

        $log->debug(print_r($data, true), $func_name);
        return $data;
    }

    /**
     * オプション情報の追加
     *
     * @param string  $namediv 名称区分
     * @param string  $name 名称
     * @param integer $disppriority 表示順
     *
     * @return boolean 結果ステータス
     */
    function insertOption($namediv, $name, $disppriority)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Name#insertOption";

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

        // オプション名　区分が 100 と 200
        // オプションデータ　それ以外
        $log->debug(print_r("namediv: ".$namediv, true), $func_name);
        $_namediv = $namediv;
        if ($namediv == 100 OR $namediv == 200) {
            $namediv_work = $namediv;

            // 名称区分の空きを取得
            $sql = "SELECT COUNT(namediv) FROM t_name ";
            $sql = $sql." WHERE namediv = ? ";
            $sql = $sql."   AND delflg = false ";
            $is_exit = false;
            while ($is_exit == false) {
                $namediv_work = $namediv_work + 1;
                $params = array($namediv_work);

                $data = $db->getOne($sql, $params);
                if (DB::isError($data)) {
                    $log->error($data->getMessage(), $func_name);
                    die($data->getMessage());
                    return false;
                }
                if ($data == 0) {
                    $_namediv = $namediv_work;
                    $is_exit = true;
                }
            }
            $log->debug(print_r("namediv: ". $_namediv, true), $func_name);
        }

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

        $db->autoCommit();
        if (!$disppriority) $disppriority = NULL;
        $params = array($name, $_namediv, $disppriority, $upduser, $_SERVER['REMOTE_ADDR']);
        $sql  = "SELECT f_name_update(1, NULL, ?, ?, ?, ?, ?, NULL);";
        $log->debug("f_name_update -> ". print_r($params, true), $func_name);

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

    /**
     * オプション情報の削除
     *
     * @param string  $namediv 名称区分
     * @param string  $namecd 名称C
     * @param integer $updcnt 更新カウンタ
     *
     * @return boolean 結果ステータス
     */
    function deleteOption($namediv, $namecd, $updcnt)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Name#deleteOption";

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

        $db->autoCommit();
        $params = array($namecd, $namediv, $updcnt);
        $sql  = "SELECT f_name_update(3, ?, NULL, ?, NULL, NULL, NULL, ?);";
        $log->debug("DELETE FROM t_name WHERE namecd = " .$namecd . " AND namediv = " . $namediv, $func_name);

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

    /**
     * オプション情報の更新
     *
     * @param string  $namediv 名称区分
     * @param string  $namecd 名称C
     * @param string  $name 名称
     * @param integer $disppriority 表示順
     * @param integer $updcnt 更新カウンタ
     *
     * @return boolean 結果ステータス
     */
    function updateOption($namediv, $namecd, $name, $disppriority, $updcnt)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Name#updateOption";

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

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

        $db->autoCommit();
        if (!$disppriority) $disppriority = NULL;
        $params = array($namecd, $name, $namediv, $disppriority, $upduser, $_SERVER['REMOTE_ADDR'], $updcnt);
        $sql  = "SELECT f_name_update(2, ?, ?, ?, ?, ?, ?, ?);";

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