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


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


    /**
     * 全件取得
     *
     */
    function selectAll($sort="membercd")
    {

        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Memberadmin#selectAll";

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

        $sql  = "SELECT membercd, membernm, password ";
        $sql .= "  FROM t_member_admin_attr ";
        $sql .= " WHERE delflg = FALSE ";
        $sql .= " ORDER BY %s";
        $sql = sprintf($sql, $sort);

        $data = $db->getAll($sql);
        if (DB::isError($data)) {
            $log->debug($data->userinfo, $func_name);
            die($data->getMessage());
        }
        $this->dbaccess->doClose($db);

        return $data;
    }

    /**
     * 全件取得
     *
     */
    function viewselectAll($sort="membercd")
    {

        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Memberadmin#viewselectAll";

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

        $params = array();
        $sql  = "SELECT * ";
        $sql .= "  FROM t_member_admin_attr ";
        $sql .= " WHERE delflg = FALSE ";
        $sql .= " ORDER BY membercd";
//        $sql = sprintf($sql, $sort);

        $data = $db->getAll($sql, $params);

        if (DB::isError($data)) {
            $log->debug($data->userinfo, $func_name);
            die($data->getMessage());
        }
        $this->dbaccess->doClose($db);

        return $data;
    }

    /**
     * 追加
     *
     */
    function insert($name, $password) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Memberadminr#insert";

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

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($name, sha1($password));

        $sql  = "select f_memberadmin_update(1, ?, ?);";

        $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($name, $password) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Memberadmin#update";

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

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($name, sha1($password));

        $sql  = "select f_memberadmin_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;
    }
    /**
     * 削除
     *
     */

    function delete($name) {
/***
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Memberadmin#delete";

        $sql = "DELETE FROM t_member_admin_attr WHERE membernm = ?";
        $params = array($name);
        $res = $db->query($sql,$params);

        if (DB::isError($res) || $res < 0) {
            $log->debug("rollback". $res->userinfo, $func_name);
            $db->rollback();
            return false;
        }

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

        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Memberadmin#delete";

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

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($name, "dummystring");

        $sql  = "SELECT f_memberadmin_update(3, ?, ?);";

        $res = $db->getOne($sql, $params);
        if (DB::isError($res) || $res < 0) {
            $log->debug("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;

    }

}
?>
