<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class TopController extends AppController {

/**
 * This controller does not use a model
 *
 * @var array
 */
	var $name = 'top';
	public $uses = array('ContactValidate','Zipcode');
	public $layout = 'procedure';

	function beforeFilter(){
		$this->Auth->allow();
	}
/**
 * index
 */
	public function index() {
		if($this->Auth->user()){
					$this->redirect('/users/login_top');                          
		}
	}
/**
 * privacy_policy
 */
	public function privacy() {
	}
/**
 * terms
 */
	public function terms() {
	}
/**
 * about us
 */
	public function aboutus() {
	}
/**
 * company
 */
	public function company() {
	}
/**
 *	mypage 
 */
	public function mypage() {
	}
/**
 *	howto 
 */
	public function howto() {
	}
/**
 *	Withdrawal (Check)
 */
	public function withdraw_check() {
	}
/**
 *	Withdrawal (Complete) 
 */
	public function withdraw_comp() {
	}
/**
 *	Access Error 
 */
	public function error() {
	}
/**
 *	Address Error
 */
	public function error404() {
	}
/**
 * contact
 */
	public function contact() {
		if($this->request->is('post') || $this->request->is('put')){
			$this->ContactValidate->set($this->request->data);
			if($this->ContactValidate->validates()){
				$this->Session->write('contact',$this->request->data);
				$this->redirect(array('action'=>'contact_confirm'));
			}else{
			}
		}
	}
/**
 * contact_confirm
 */
	public function contact_confirm() {
		if($this->Session->read('contact')){
			$this->set('contact',$this->Session->read('contact'));
		}else{
			$this->redirect(array('action'=>'contact'));
		}
	}
/**
 * contact_complete
 */
	public function contact_complete() {
		$this->set('contact',$this->Session->read('contact'));
		$name = $this->Session->read('contact.ContactValidate.name');
		$mail = $this->Session->read('contact.ContactValidate.email');
		$comment = $this->Session->read('contact.ContactValidate.comment');
		$Email = new CakeEmail();
		$Email->charset('ISO-2022-JP');
		$Email->emailFormat('text');
		$Email->template('user_contact');
		$Email->viewVars(array('name'=>$name,'comment'=>$comment));
		$Email->from( MAIL_FROM );
		$Email->to($mail);
		$Email->subject('【PictCode】お問い合わせいただき、ありがとうございます。');
		$Email->send();
	}


/**
 *郵便番号から住所を自動入力する* 
 *
 * View/add.ctp
 */
	public function zipsearch() {
		App::import('Model','ConnectionManager');
        $db =ConnectionManager::getDataSource('default');

		//Users/zipsearch.ctpを自動で見に行くのを防止
		$this->autoRender = false;

        	//リクエストがajaxメソッドで送られてきた場合
       		if($this->request->is('ajax')) {
            
            	//formのパラメータ取得
           	$post = $this->request->data['ContactValidate']['postcode'];
           	// var_dump($post);
           	// exit;
			// $options = array(
			// 'conditions'=>array(
			// 	'postcode like' => $post.'%' ,
			// )
		// );
		$result = $db->query('select * from zipcodes where postcode like "'.$post.'%" limit 1');
       
		// データをjsonに変換して出力
		echo json_encode($result);

		}
	}

}
