<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 * @property PaginatorComponent $Paginator
 */
class UsersController extends AppController {

	public $layout = 'procedure';
	public $name = 'users';

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('register','activate','confirm','sent');
    }

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->User->exists($id)) {
			throw new NotFoundException(__('Invalid user'));
		}
		$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
		$this->set('user', $this->User->find('first', $options));
	}


/**
 * register method
 *
 * @return void
 */
	public function register() {
		if($this->request->is('post') || $this->request->is('put')){
			$this->User->set($this->request->data);
			if($this->User->validates()){
				$this->Session->write('register',$this->request->data);
				$this->redirect(array('action'=>'confirm'));
			}else{
			}
		}
	}
	
/**
 * register confirm
 */
	public function confirm() {
		if($this->Session->read('register')){
			$this->set('register',$this->Session->read('register'));
		}else{
			$this->redirect(array('action'=>'register'));
		}
	}


/**
 * register sent
 */
	public function sent() {
		// if (!empty( $this->data)){
	 //        //  保存
	   if( $this->User->save($this->Session->read('register'))){
	            //  メール送信
		$this->set('register',$this->Session->read('register'));
		$name = $this->Session->read('register.User.login_id');
		$mail = $this->Session->read('register.User.email');
        // ユーザアクティベート(本登録)用URLの作成
        $url = 
            DS . 'users' .          // コントローラ
            DS . 'activate' .                       // アクション
            DS . $this->User->id .                  // ユーザID
            DS . $this->User->getActivationHash();  // ハッシュ値
        $url = Router::url( $url, true);  // ドメイン(+サブディレクトリ)を付与
		$comment = $url;

		$Email = new CakeEmail();
		$Email->charset('ISO-2022-JP');
		$Email->emailFormat('text');
		$Email->template('user_register');
		$Email->viewVars(array('name'=>$name,'comment'=>$comment));
		$Email->from($mail);
		$Email->to('hasegawa@i-hearts.jp');
		$Email->subject('[PICT CODE]問い合わせ');
		$Email->send();
	    }

	}	

/**
 * register activate
 */
	public function activate( $user_id = null, $in_hash = null) {
	    // UserモデルにIDをセット
	    $this->User->id = $user_id;
	    if ($this->User->exists() && $in_hash == $this->User->getActivationHash()) {
	    // 本登録に有効なURL
	        // statusフィールドを0に更新
	        $this->User->saveField( 'status', 0);
	        $this->Session->setFlash( 'Your account has been activated.');
	    }else{
	    // 本登録に無効なURL
	        $this->Session->setFlash( 'Invalid activation URL');
	    }
	}
	
/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$this->Flash->success(__('The user has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Flash->error(__('The user could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->User->exists($id)) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->User->save($this->request->data)) {
				$this->Flash->success(__('The user has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Flash->error(__('The user could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
			$this->request->data = $this->User->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		$this->request->allowMethod('post', 'delete');
		if ($this->User->delete()) {
			$this->Flash->success(__('The user has been deleted.'));
		} else {
			$this->Flash->error(__('The user could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}

/**
 * login method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function login() {
		//var_dump(Security::hash( "123", 'blowfish'));
		if($this->Auth->user()){
			$this->redirect($this->Auth->redirectUrl());                          
		}
		if ($this->request->is('post')) {
			if ($this->Auth->login()) {
				$this->redirect('/Users/login_top');                          
			} else {
				$this->Flash->error(__('error'));
				// $this->Flash->error(__('<section class="caution"><p>ニックネームか　パスワードに<br>まちがいが あるよ！</p></section>'));
			}
		}
	}	
/**
 * logout methods
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function logout() {
	    $this->redirect($this->Auth->logout());
	}


/**
 * login_top method
 *
 */
	public function login_top() {
	}



}


