<?php
/**
 *	Project:	Quicty: Quick application build environment depends on PEAR and Smarty
 *	File:		Quicty/modules/template/userauth/lib/Pages/index.class.php
 *
 *	@copyright	Tomoyuki Negishi and ZubaPitaTech, Inc.
 *	@author		Tomoyuki Negishi <tomoyu-n@zubapita.jp>
 *	@license	http://www.opensource.org/licenses/bsd-license.php The BSD License
 *	@package	Quicty
 *	@version	$Id:$
 */
class user extends myQuicty {

public function pre_dispatch_index() {
	parent::pre_dispatch_index();
	$this->page['base_url'] = quicty_base_url();
	
	return true;
}

function list_record() {
	$search_form = $this->new_search_obj('user_search');
	$this->assign_form('search_form',$search_form);
	$form = $this->new_data_set('userauth');
	$perPage = 10;
	$condition = $form->make_condition();
	$pager_option = $this->set_pager_option($perPage,$condition);
	$form->bind_pager($pager_option);
	$table = $form->select_table_with_pager(' order by id');
	$this->assign('table_view',$form->display_table_value($table));
	$this->assign('display_cmd','info');
	$this->page['pager'] = $form->get_pager_links();
	$this->assign('page',$this->page);
	//-- End of list_record
	$result = $this->display($this->current_template);
}

function display_record($condition='') {
	$form = $this->new_data_set('userauth');
	$form->set_base_link_addr(quicty_base_url().'<!--{$folder_name}-->/user/');
	$form->set_restart_link_addr(quicty_base_url().'<!--{$folder_name}-->/user/');
	$form->set_button_label('delete_confirm','削除（退会）');
	$form->set_button_label('delete','削除（退会）実行');
	$form->validate_and_freeze();
	$form->bind_button_control();
	$result = $form->automatic_form_handler($condition);
	switch($this->QuictyStatus) {
		case 'INSERT':
			$_SESSION['next'] = NULL;
			if(!$this->user['level']) {
				$_SESSION['regiserd_massege'] = "ユーザー登録できました。\nログインしてください。\n";
				if($_REQUEST['next']) {
					redirect('<!--{$folder_name}-->/user/login?next='.$_REQUEST['next']);
				} else {
					redirect('<!--{$folder_name}-->/user/login');
				}
				exit;
			}
	}

	$this->user = $result['values'];
	$this->assign_form('input_form',$form);
	$this->assign('page',$this->page);
	//-- End of display_record
	if($this->user['level'] >= admin_user) {
		$upper_path = '一覧ページ';
		$this->add_pathlist('<!--{$folder_name}-->/user/',$upper_path,false);
		$this->add_pathlist('',$this->page['name']);
	}
	$this->display($this->current_template);
}

//------------------------------------------------------------------

function dispatch_index() {
	if($this->user['level'] < admin_user) redirect('top');
	$this->page['name'] = 'ユーザー登録の一覧';
	$this->list_record('');
}

function dispatch_add() {
	if($_REQUEST['next']) $this->assign('next',$_REQUEST['next']);
	if($this->user['level'] >= registerd_user and $this->user['level'] < admin_user)
		redirect('<!--{$folder_name}-->/usr/info');
	$this->page['name'] = 'ユーザー登録の追加';
	$this->display_record('');
}

function dispatch_info() {
	if($this->user['level'] < registerd_user) redirect('top');
	if(isset($_REQUEST['id']) and $_REQUEST['id']!=$this->user['id'] and $this->user['level'] < admin_user)
		redirect('top');
	$this->page['name'] = 'ユーザー登録の表示';
	if(!$_REQUEST['id']) {
		$this->display_record('where id='.$this->user['id']);
	} else {
		$this->display_record();
	}
}

function dispatch_edit() {
	if($this->user['level'] < registerd_user) redirect('top');
	if(isset($_REQUEST['id']) and $_REQUEST['id']!=$this->user['id'] and $this->user['level'] < admin_user)
		redirect('top');
	$this->page['name'] = 'ユーザー登録の編集';
	if(!$_REQUEST['id']) {
		$this->display_record('where id='.$this->user['id']);
	} else {
		$this->display_record();
	}
}

function dispatch_delete() {
	if($this->user['level'] < registerd_user) redirect('top');
	if(isset($_REQUEST['id']) and $_REQUEST['id']!=$this->user['id'] and $this->user['level'] < admin_user)
		redirect('top');

	$form = $this->new_data_set('userauth');
	$user_id = $form->esfd($_REQUEST['id']);
	if($this->QuictyStatus=='DELETE' and $_REQUEST['id'] and 
		($_REQUEST['id']==$this->user['id'] or $this->user['level'] >= admin_user) ) {
		$condition = 'where id='.$user_id;
		$form->delete($condition);
		if($user_id==$this->user['id']) 
			redirect('<!--{$folder_name}-->/user/quit_member.html');
	}

	$this->page['name'] = 'ユーザー登録の削除（退会）';
	if($_REQUEST['id']) {
		$this->display_record('where id='.$user_id);
	} else {
		$this->display_record('where id='.$this->user['id']);
	}
}

//------------------------------------------------------------------

function dispatch_login() {
	$this->page['name'] = 'ログイン';
	if($_REQUEST['next']) $this->assign('next',$_REQUEST['next']);
	if($_SESSION['regiserd_massege']) {
		$this->assign('login_message',$_SESSION['regiserd_massege']);
		$_SESSION['regiserd_massege'] = '';
	}
	$auth = $this->get_auth_obj();
	if($auth->getAuth()) {
		$_SESSION['next'] = NULL;
		if($_POST['next']) {
			redirect('http://'.$_POST['next']);
		} else {
			redirect('top');
		}
	} else {
		$_FORM = $this->new_form_obj('login');
		$_FORM->bind_data_view('login.view.conf');
		$this->set_qs_status('SEND');
		$_FORM->bind_button_control();
		$auth_status = $auth->getStatus();
		if($auth_status==AUTH_WRONG_LOGIN) {
			$this->set_form_error("ログインIDもしくはパスワードが間違っています。");
		}
		$this->assign_form('form',$_FORM);
		$this->assign('page',$this->page);
		$this->display($this->current_template);
	}
}

function dispatch_logout() {
	$this->page['name'] = 'ログアウト';
	$auth = $this->get_auth_obj();
	$auth->logout();
	$user['authorized'] = false;
	$user['handle'] = 'ゲスト';
	$this->assign('user',$user);
	$this->assign('page',$this->page);
	$this->display($this->current_template);
}

function dispatch_quit_member() {
	$this->page['name'] = '退会';
	$auth = $this->get_auth_obj();
	$auth->logout();
	$user['authorized'] = false;
	$user['handle'] = 'ゲスト';
	$this->assign('user',$user);
	$this->assign('page',$this->page);
	$this->display($this->current_template);
}


} // end of user class
?>