<?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.Complete
 * @author      Your Name <mail@example.com>
 * @access      public
 */
class Customer_Mypage_Member_Insert_Complete
{
    var $ba;
    
    var $c_member;
    var $l_mail;
    var $title;
    var $membercd;
    var $session;
    
    var $id;
    var $username;
    var $password;
    var $kubun;
    
    var $actionChain;
    
    
    /**
     * execute
     *
     * @access  public
     */
    function execute()
    {
        $log =& LogFactory::getLog();
        $log->debug("Customer_Mypage_Member_Insert_Completeが実行されました", "Customer_Mypage_Member_Insert_Complete#execute");
        
        $mobile = &Net_UserAgent_Mobile::isMobile();
        $re_str = ($mobile) ? "mobile_" : "";
        
        $res = $this -> checkParams();
        if($res) {
            $res = $this -> insertDb();
            if($res) {
                $container =& DIContainerFactory::getContainer();
                $auth =& new Authext();
                $auth->login();
                return $re_str .'success';
            } else {
                return $re_str .'failed';
            }
        } else {
            return $re_str .'input';
        }
    }

    function insertDb()
    {
        if(!isset($this->id)) {
            return false;
        } else {
            //$_GET['id']の値をもとに、仮会員ＤＢから値をもらってくる
            $data = $this -> c_member -> check_trial($this->id);
            if($data === false){
                return false;
            } else {
                //もらってきたデータを、本会員ＤＢに入れる
                //ここで会員番号を生成する
                $res = $this -> c_member -> insert($data);
                if($res === false) {
                    return false;
                } else {
                    $this -> membercd = $res;
                    $res = $this -> c_member -> trial_delete($this->id);
                    if($res == false) {
                        return false;
                    } else {
                        $res = $this->sendMail($res,$data);
                        return $res;
                    }
                }
            }
        }
    }

    function checkParams()
    {
        $errorList =& $this->actionChain->getCurErrorList();

        $res = true;
        $mailmatch = "/^[a-zA-Z0-9\"\._\?\+\/-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/";

        if ($res && !empty($this->username) && !preg_match($mailmatch, $this->username)) {
            $errorList->add(1, "メールアドレスかパスワードが間違っています。");
            $res = false;
        }

        if ($res && !empty($this->password) && !preg_match("/[a-zA-z0-9]+/", $this->password)) {
            $errorList->add(1, "メールアドレスかパスワードが間違っています。");
            $res = false;
        }

        $data = $this -> c_member -> check_trial($this->id);
        if($data == false || $res == false) {
            $res = false;
        } else {
            if($this->username != $data['mailaddr'] || sha1($this->password) != $data['password']){
                $errorList->add(1, "メールアドレスかパスワードが間違っています。");
                $res = false;
            }
        }
        return $res;
    }

    function sendMail($res, $data)
    {
        if($res){
            $mailaddress = $data["mailaddr"];
            $from        = MAIL_FROM;

            $params = array("lastname"  => $data["lastnm"],
                            "frontname" => $data["firstnm"]);

            return $this -> l_mail -> sendFromTemplate("regist_complete", $mailaddress, $from, $params);
        } else {
            return false;
        }
    }
    

    function getId()
    {
        return $this -> id;
    }

    function getUsername()
    {
        return $this -> username;
    }

    function setActionChain(&$value)
    {
        $this -> actionChain =& $value;
    }
    
    function getBa()
    {
        if($this -> ba){
            return $this -> ba;
        } else {
            return "general_portal";
        }        
    }
}
?>
