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


/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * 会員登録・変更完了
 *
 * @package     Customer.Mypage.Member.Insert.Done
 * @author      netnee <mail@example.com>
 * @access      public
 */
class Customer_Mypage_Member_Insert_Done
{
    var $ba;
    
    var $l_crypt;          //Mcryptによる暗号化・復号化のクラス
    var $l_mail;           //メール送信ロジック
    
    var $c_member;         //メンバーテーブル用のクラス
    var $c_moremember;     //その他の登録情報用ＤＢクラス
    
    var $title;
    var $id;
    
    var $newmember;        //新規会員登録情報
    var $newmember_serial; //新規会員登録情報のシリアライズ化情報
    
    var $kubun;
    var $auth;
    
    
    /**
     * execute
     *
     * @access  public
     */
    function execute()
    {
        $log =& LogFactory::getLog();
        $log->debug("Customer_Mypage_Member_Insert_Doneが実行されました", "Customer_Mypage_Member_Insert_Done#execute");
        
        $mobile = &Net_UserAgent_Mobile::isMobile();
        $re_str = ($mobile) ? "mobile_" : "";
        
        if($this->newmember->memberCd){
            //秘密の質問の答えをMcrypt関数によって暗号化
            $this->newmember->answer = $this->l_crypt->encrypt($this->newmember->answer);
            
            if($this->newmember->password == "" && $this->newmember->pass_confirm == ""){
                // DB より該当会員のパスワードを取得
                $updmemdata = $this->c_member->select_one($this->newmember->memberCd);
                if (!empty($updmemdata["password"])){
                    $this->newmember->password = $updmemdata["password"];
                } else {
                    return $re_str .'input';
                }
            } else {
                // 入力パスワードを sha1 暗号化
                $this->newmember->password = sha1($this->newmember->password);
            }
            
            // データ更新
            $res_m = $this->c_member->update($this->newmember);
            // ジャンル更新
            $this->c_moremember->updateGenreMember($this->newmember->category, $this->newmember->memberCd);
            
            if ( $res_m ){
                $this->kubun = 2;
                return $re_str .'complete';
            } else {
                return $re_str .'input';
            }
        } else {
            $this -> createId();
            $res = $this -> insertDb();
            $res = $this -> sendMail($res);
        }
        
        //仮会員のデータベース登録とメール送信の合否を元に、場合わけを行う
        if($res) {
            return $re_str .'success';
        } else {
            return $re_str .'input';
        }
    }
    
    function setNewmember_serial($value)
    {
        $value = urldecode($value);
        $this -> newmember_serial = $value;
        $this -> newmember = unserialize($value);
    }

    function createId()
    {
        do {
            $this -> id = mt_rand(10000000,99999999);
            $res = $this -> c_member -> check_trial($this -> id);
        } while($res != false);
    }

    function insertDb()
    {
        if($this -> newmember == "") {
            return false;
        } else {
            //秘密の質問の答えをMcrypt関数によって暗号化
            $this->newmember->answer = $this->l_crypt->encrypt($this->newmember->answer);

            $flg = $this->c_member->checkTrialMailAddr($this->newmember);
            if($flg){
                $res = $this->c_member->trial_insert($this->id, $this->newmember);
            } else {
                $res = $this->c_member->updateTrial($this->id, $this->newmember);
            }
            if($res == false) {
                return false;
            } else {
                return true;
            }
        }
    }

    function sendMail($res)
    {
        if($res){
            $mailaddress = $this->newmember->mailaddress_user."@".$this->newmember->mailaddress_domain;
            $from        = MAIL_FROM;

            $params = array("mailaddr"     => $mailaddress,
                            "lastnm"       => $this->newmember->lastname,
                            "firstnm"      => $this->newmember->frontname,
                            "lastnmkana"   => $this->newmember->lastname_kana,
                            "firstnmkana"  => $this->newmember->frontname_kana,
                            "nicknm"       => $this->newmember->nickname,
                            "regist_url"   => SSL_URL_ROOT.$_SERVER["SCRIPT_NAME"]. "?action=customer_mypage_member_insert_confirm&id=".$this -> id);

            return $this -> l_mail -> sendFromTemplate("interim_regist", $mailaddress, $from, $params);
        } else {
            return false;
        }
    }
    
    function getBa()
    {
        if($this -> ba){
            return $this -> ba;
        } else {
            return "general_portal";
        }        
    }
}
?>
