<?php
/**
 * DBクラス
 *
 * PHP versions 5
 *
 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
 *
 * @package    Magic3 Framework
 * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
 * @copyright  Copyright 2006-2007 Magic3 Project.
 * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
 * @version    SVN: $Id$
 * @link       http://www.magic3.org
 */
require_once($gEnvManager->getDbPath() . '/baseDb.php');

class ec_login_boxDb extends BaseDb
{
	/**
	 * 仮会員を正会員に変更
	 *
	 * @param int $userId			ユーザID兼データ更新ユーザ
	 * @return						true=成功、false=失敗
	 */
	function makeTmpMemberToProperMember($userId)
	{
		$now = date("Y/m/d H:i:s");	// 現在日時
				
		// トランザクション開始
		$this->startTransaction();
		
		$queryStr = 'SELECT * FROM shop_tmp_member WHERE sb_login_user_id = ? AND sb_deleted = false';
		$ret = $this->selectRecord($queryStr, array($userId), $row);
		if (!$ret){		// EC会員でないとき
			// トランザクション確定
			$this->endTransaction();
			return false;
		}
		
		// 新規IDを作成
		$newId = 1;
		$queryStr = 'select max(sm_id) as ms from shop_member ';
		$ret = $this->selectRecord($queryStr, array(), $maxRow);
		if ($ret) $newId = $maxRow['ms'] + 1;
		
		$type = 1;		// 会員種別(個人)

		// 新規レコードを追加
		$queryStr  = 'INSERT INTO shop_member (';
		$queryStr .=   'sm_id, ';
		$queryStr .=   'sm_history_index, ';
		$queryStr .=   'sm_language_id, ';
		$queryStr .=   'sm_type, ';
		$queryStr .=   'sm_person_info_id, ';
		$queryStr .=   'sm_login_user_id, ';
		$queryStr .=   'sm_create_user_id, ';
		$queryStr .=   'sm_create_dt ';
		$queryStr .= ') VALUES (';
		$queryStr .=   '?, ';
		$queryStr .=   '?, ';
		$queryStr .=   '?, ';
		$queryStr .=   '?, ';
		$queryStr .=   '?, ';
		$queryStr .=   '?, ';
		$queryStr .=   '?, ';
		$queryStr .=   '?) ';
		$ret = $this->execStatement($queryStr, array($newId, 0, $row['sb_language_id'], $type, $row['sb_person_info_id'], $row['sb_login_user_id'], $userId, $now));
		
		// 仮会員の情報を削除
		$queryStr  = 'UPDATE shop_tmp_member ';
		$queryStr .=   'SET sb_deleted = true, ';	// 削除
		$queryStr .=     'sb_update_user_id = ?, ';
		$queryStr .=     'sb_update_dt = ? ';
		$queryStr .=   'WHERE sb_serial = ?';
		$ret = $this->execStatement($queryStr, array($userId, $now, $row['sb_serial']));
		
		// トランザクション確定
		$ret = $this->endTransaction();
		return $ret;		
	}
}
?>
