<?php
/* SVN FILE: $Id: registry.php 538 2008-06-05 17:22:44Z bb_yujiro $ */
/**
 * Model_Manip Controller
 *
 * PHP versions 5
 *
 *      Hitsuji : Social Network Service <http://rakuto.net/rktSNS/>
 *      Copyright (c) 2007 Yujiro Takahashi
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @package         Hitsuji
 * @copyright       Copyright (c) 2007 Yujiro Takahashi
 * @link            http://rakuto.net/
 * @author          Yujiro Takahashi <yujiro@rakuto.net>
 * @version         $Revision: 538 $
 * @modifiedby      $LastChangedBy: bb_yujiro $
 * @lastmodified    $Date: 2008-06-06 02:22:44 +0900 (金, 06 6 2008) $
 * @license         http://opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Controllerクラス
 *
 * @category        Hitsuji : controller
 * @package         Hitsuji
 * @copyright       Copyright (c) 2007 Yujiro Takahashi
 * @link            http://rakuto.net/rktSNS/
 * @author          Yujiro Takahashi <yujiro@rakuto.net>
 * @version         $Revision: 538 $
 * @modifiedby      $LastChangedBy: bb_yujiro $
 * @lastmodified    $Date: 2008-06-06 02:22:44 +0900 (金, 06 6 2008) $
 * @license         http://opensource.org/licenses/mit-license.php The MIT License
 */
class Controller extends Admin_Controller 
{
    /**
     * ユーザータイプ
     * @var integer
     */
    protected $type = null;

    /**
     * 名
     * @var string 
     */
    protected $first_name = null;

    /**
     * 姓
     * @var string 
     */
    protected $last_name = null;

    /**
     * ニックネーム
     * @var string 
     */
    protected $nickname = null;

    /**
     * メールアドレス
     * @var string 
     */
    protected $email = null;

    /**
     * パスワード
     * @var string 
     */
    protected $password = null;

    /**
     * メール送信フラグ
     * @var integer 
     */
    protected $mailing = null;

    /**
     * 実行処理
     *
     * @access public
     * @return void
     **/
    protected function action()
    {
        $this->_setOptions();
        $this->_checkEmail();

        $validated = $this->objrqt->getValidate();
        $action = array(
            'complete'=> false,
            'confirm'=> false,
            'goback'=> 'again'
        );

        /* 入力やり直し */
        if (!empty($_POST['again'])){
            $this->objvew->assign('request', $_SESSION['account']);
        }

        if ($this->checked_nonce){
            /* 新規登録 */
            if (!empty($_POST['manip'])){
                include LIB_DIR.'rktModel'.DS.'Manip.php';

                $this->_manipAccount();
                $this->_manipProfile();
                $this->_manipBlog();
                $this->_manipGadget();

                $action['complete'] = true;
                unset($_SESSION['account']);
            }

            /* 入力内容確認 */
            if (!empty($_POST['confirm'])){
                if ($validated){
                    $action['confirm'] = true;
                }
                $this->objvew->assign('alert', $this->objrqt->getAlerts());
                $_SESSION['account'] = $this->objrqt->getRequests();
            }
        } // if ($this->checked_nonce)

        $this->objvew->assign('action', $action);
    }

    /**
     * 入力条件の設定
     *
     * @access protected
     * @return void
     **/
    protected function setCondition()
    {
        $this->conditions = array (
            'type'=> array(
                'type'=>     'number',
                'option'=>   array(),
                'required'=> true,
                'input'=>    RKT_Model::INPUT_CHATCH,
            ),
            'first_name'=> array(
                'type'=>     'string',
                'option'=>   array(),
                'required'=> true,
                'input'=>    RKT_Model::INPUT_CHATCH,
            ),
            'last_name'=> array(
                'type'=>     'string',
                'option'=>   array(),
                'required'=> true,
                'input'=>    RKT_Model::INPUT_CHATCH,
            ),
            'nickname'=> array(
                'type'=>     'string',
                'option'=>   array(),
                'required'=> true,
                'input'=>    RKT_Model::INPUT_CHATCH,
            ),
            'email'=> array(
                'type'=>     'email',
                'option'=>   array(),
                'required'=> true,
                'input'=>    RKT_Model::INPUT_CHATCH,
            ),
            'password'=> array(
                'type'=>     'string',
                'option'=>   array(
                    'format'=> '\w+',
                    'min_length'=>4,
                    'max_length'=>16
                ),
                'required'=> true,
                'input'=>    RKT_Model::INPUT_CHATCH,
            ),
            'mailing'=> array(
                'type'=>     'number',
                'option'=>   array(),
                'required'=> true,
                'input'=>    RKT_Model::INPUT_CHATCH,
            ),
        );
    }

    /**
     * 選択肢の設定
     *
     * @access private
     * @return void
     **/
    private function _setOptions()
    {
        $type = array (
            STATUS_ENABLE => _('To receive mail'),
            STATUS_DISABLE=> _('Do not receive mail'),
        );
        $this->objvew->assign('mailings', $type);

        $type = array (
            STATUS_ADMIN => _('Administrator'),
            STATUS_MEMBER=> _('Member'),
        );
        $this->objvew->assign('types', $type);
    }

    /**
     * メールアドレスのチェック
     *
     * @access private
     * @return void
     **/
    private function _checkEmail()
    {
        if (empty($this->email)){
            return ;
        }
    
        $hashed_email = RKT_Crypt::getHashed($this->email);
        $condition = array(
            'hashed_email'=> $hashed_email
        );
        $account = RKT_DB::getRow('act_account', $condition);
        
        if (!empty($account['id'])){
            $this->objrqt->setAlert('ismember');
        }
    }

    /**
     * アカウント
     *
     * @access private
     * @return void
     **/
    private function _manipAccount()
    {
        include MODEL_DIR.'manips'.DS.'act_account.php';

        $now = date('Y-m-d H:i:s');

        $objact = new act_account();

        $objact->setValue('last_login',      $now);
        $objact->setValue('regist_date',     $now);
        $objact->setValue('avail',           STATUS_ENABLE);
        $objact->setValue('status',          $_SESSION['account']['type']);
        $objact->setValue('email',           $_SESSION['account']['email']);
        $objact->setValue('hashed_password', $_SESSION['account']['password']);
        $objact->setValue('mailing',         $_SESSION['account']['mailing']);

        $objact->manip();
        
        $this->ref_account = $objact->getCurId();
    }

    /**
     * プロフィール
     *
     * @access private
     * @return void
     **/
    private function _manipProfile()
    {
        include MODEL_DIR.'manips'.DS.'act_profile.php';

        $objprf = new act_profile();

        $objprf->setValue('ref_account', $this->ref_account);
        $objprf->setValue('first_name',  $_SESSION['account']['first_name']);
        $objprf->setValue('last_name',   $_SESSION['account']['last_name']);
        $objprf->setValue('nickname',    $_SESSION['account']['nickname']);

        $objprf->manip();
    }

    /**
     * ブログ
     *
     * @access private
     * @return void
     **/
    private function _manipBlog()
    {
        include MODEL_DIR.'manips'.DS.'blg_blog.php';

        $objblg = new blg_blog();

        $objblg->setValue('ref_account', $this->ref_account);

        $objblg->manip();
    }

    /**
     * ガジェット
     *
     * @access private
     * @return void
     **/
    private function _manipGadget()
    {
        include MODEL_DIR.'manips'.DS.'act_gadget.php';

        $objgad = new act_gadget();

        $objgad->setValue('ref_account', $this->ref_account);
        $objgad->setValue('title',       _('Blog'));
        $objgad->setValue('position',    POSITION_MIDDLE);
        $objgad->setValue('source',      HTTP_ACTION.'private/recent/blog/json/');
        $objgad->manip();

        $objgad->refreshColumn();
        $objgad->setValue('ref_account', $this->ref_account);
        $objgad->setValue('title',       _('Comment'));
        $objgad->setValue('position',    POSITION_MIDDLE);
        $objgad->setValue('source',      HTTP_ACTION.'private/recent/comment/json/');
        $objgad->manip();

        $objgad->refreshColumn();
        $objgad->setValue('ref_account', $this->ref_account);
        $objgad->setValue('title',       _('Topic'));
        $objgad->setValue('position',    POSITION_MIDDLE);
        $objgad->setValue('source',      HTTP_ACTION.'private/recent/topic/json/');
        $objgad->manip();

        $objgad->refreshColumn();
        $objgad->setValue('ref_account', $this->ref_account);
        $objgad->setValue('title',       _('Event'));
        $objgad->setValue('position',    POSITION_MIDDLE);
        $objgad->setValue('source',      HTTP_ACTION.'private/recent/event/json/');
        $objgad->manip();
    }
} // class Controller
?>
