<?php
/*
	Copyright(c) 2010 Shift Tech Inc. All Rights Reserved.
 	http://www.shift-tech.jp/

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once (LIB_PATH.'/common/userDB.php');

class Admin_admin_user_Edit extends userDB
{
	function prepare($view)
	{
		$this->_form = new HTML_QuickForm('form','post',$_SERVER['PHP_SELF'],'',null,true);
		$this->setElements();
		$this->setButtons();
		$this->setRules();
		return null;
	}

	function perform($view)
	{
		$req = $this->getRequest('buttons');
		if (isset($req['BTNList'])&& $req['BTNList']!=null) {	// 一覧表示がクリックされたとき
			header('location:index.php');
			exit();	
		} elseif (isset($req['BTNnext'])&&$req['BTNnext']!=null) {
			$record["usr_mimg_path"] = $this->getRequest('usr_mimg_path');
			$record["usr_code"] = $this->getRequest('usr_code');
			if($this->_form->validate())
			{
				$result = null;
				if (is_uploaded_file($_FILES['picture']["tmp_name"])) {
					$image = new commonFile();//画像処理クラス
					$result = $image->preSave("picture",MAX_FILE_SIZE,$width=USR_PROF_IMG_W,$height=USR_PROF_IMG_W,$force=true);//画像プレ保存
				}

				if(!$result) {
					$this->_form->freeze();
					$this->setButtons($this->_form->isFrozen()); //ボタンの作り直し
					if(isset($_SESSION["tmpImage"])) { //新規画像登録がある場合はprev.tplで見せないように
						$record["usr_mimg_path"] = null;
					}
				} else {
					$this->_form->setElementError("picture",$result);
				}
			}
		//保存ボタンクリック時
		} else if(isset($req['BTNsave'])&&$req['BTNsave']!=null) {
			if($this->_form->validate()){
				$post = $this->_form->getSubmitValues();
				$rtn = $this->doSave($post);
				if($rtn==null) {
					header('location:index.php');
				} else {
					$this->_form->setElementError("",$rtn);
				}
			}
		//削除ボタンクリック時
		} else if(isset($req['BTNdelete'])&&$req['BTNdelete']!=null) {
			$post = $this->_form->getSubmitValues();
			//管理者が居なくなってしまわないかチェック
			if(check_usr_kanri_n($value=null,$post)) {
				$rtn = $this->delete($post);
				if($rtn==null) {
					header('location:index.php');
				} else {
					$this->_form->setElementError("",$rtn);
				}
			} else {
				$this->_form->setElementError("","・「管理者」が居なくなってしまうため削除できません");
			}
		} else {
			//何もクリックされていない場合は情報取得
			unset($_SESSION["tmpImage"]);
			//DBデータ取得
			$record = $this->getData($this->getrequest("usr_code"));	//	一旦登録情報を読み出しておく
			$this->_form->setDefaults($record);
		}
		//登録エラー時
		if(!empty($this->_form->_errors)) {
			$view->assign('ERROR',$this->_form->_errors);
			foreach($this->_form->_errors as $key => $val) {
				$this->_form->updateElementAttr($key, array("class"=>"err"));
			}
		}
		
		$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($view);
		$this->_form->accept($renderer);
		$view->assign('form',$renderer->toArray());
		$view->assign('usr_code',$record["usr_code"]);
		//既存画像ファイル名
		$view->assign('usr_mimg_path',$record["usr_mimg_path"]);
		$view->assign('USR_PROF_IMG_W', USR_PROF_IMG_W);

		if($this->_form->isFrozen()) {
			return 'user/prev.tpl';
		} else {
			return 'user/edit.tpl';
		}
	}
}
?>