<?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/unitDB.php');
require_once (LIB_PATH.'/common/pageunitDB.php');

class Admin_admin_contents_page_Unitclipboard extends pageUnitDB
{
	var $_pageu_code = null;
	
	function Admin_admin_contents_page_Unitclipboard()
	{
		$this->__construct();
	}
	function __construct()
	{
		pageUnitDB::__construct();
	}
	
	
	function prepare($view)
	{	
		return null;
	}

	function perform($view)
	{
		//情報取得
		$list = $this->getUnitClipList();
		$view->assign("list",$list);
		
		//デフォルトセット、および変更差分調査の為ユニットコピー名の配列を作成
		$unitclip_name_org =array();
		if(is_array($list)&&count($list)>0) {
			foreach($list as $key => $val) {
				$unitclip_name_org[$val["pageu_code"]] = $val["unitclip_name"];
			}
		}
		
		$this->_form = new HTML_QuickForm('form','post',$_SERVER['PHP_SELF'],'',null,true);
		$this->_form->addElement('hidden','token', null);
		if(empty($this->_form->_submitValues)) {
			$record["token"] = $this->getToken();
			$this->_form->setDefaults($record);
		}
		$this->_form->setDefaults(array("unitclip_name"=>$unitclip_name_org));
		
		$buttons = array();
		$buttons[] = $this->_form->createElement('submit','BTNback',' キャンセル ',array('id'=>'BTNback','class'=>'bBtnCancel', 'onClick'=>'parent.closeBox();'));
		$buttons[] = $this->_form->createElement('submit','BTNsave',' 保存 ',array('id'=>'BTNsave','class'=>'bBtnSave'));
		$this->_form->addGroup($buttons,"buttons", null, '&nbsp;&nbsp;');
		
		//名前変更フォーム
		if(is_array($list)&&count($list)>0) {
			foreach($list as $key=>$val) {
				$item[]=&HTML_QuickForm::createElement('text', $val["pageu_code"], $val["unitclip_name"], array("size"=>20, "maxlength"=>40));
				$delitem[]=&HTML_QuickForm::createElement('checkbox', $val["pageu_code"], $val["unitclip_name"]);
			}
			$this->_form->addGroup($item,'unitclip_name', null, '');
			$this->_form->addGroup($delitem,'unitclip_del', null, '');
		}
		$view->assign("skinlist",$skinlist);
		//削除フォーム
		
		//入力チェック
		$this->_form->applyFilter('__ALL__','trim');
		$this->_form->setJsWarnings('下記の項目が不正です。','');
		$this->_form->addGroupRule("unitclip_name", "ユニットコピー名は必須入力です。", 'required', null, null);
		
		$req = $this->getRequest('buttons');
		if(isset($req['BTNsave'])&&$req['BTNsave']!=null) { //保存処理
			if(!check_token($_REQUEST["token"])) {
				$this->putTokenError();
			}
			if($this->_form->validate()){
				$post = $this->_form->getSubmitValues();
				//保存処理
				$records = array();
				if(isset($post["unitclip_name"])) {
					foreach($post["unitclip_name"] as $key=>$val) {
						if($val!=$unitclip_name_org[$key] || (isset($post["unitclip_del"][$key]) && $post["unitclip_del"][$key]==1) ) {
							//名称変更、または削除が削除されたレコードのみ変更
							$records[] = array("pageu_code"=>$key,
													"unitclip_name"=>$val,
													"unitclip_active"=> (isset($post["unitclip_del"][$key]) && $post["unitclip_del"][$key]==1)? 0 : 1
													);
						}
					}
					//変更レコード数が1件以上なら保存処理
					if(count($records)>0) {
						$this->db->query('BEGIN');
						foreach($records as $key=>$val) {
							if(!$this->db->update("tbl_unitclip", $val, $where = "pageu_code=".$val["pageu_code"])) {
								$this->db->query('ROLLBACK');
								$this->_form->setElementError("","ユニットコピー名：".$val["unitclip_name"]."の保存に失敗しました");
								break;
							}
						}
						$this->db->query('COMMIT');
					}
				}
			}
			if(empty($this->_form->_errors)) {//保存OKなら閉じる処理を出力
				$view->assign("windowclose",'<script language="javascript">parent.closeBox();</script>');
			}
		} 
		
		//エラー表示
		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());
		
		return 'contents/page/unitclipboard.tpl';
	}
	
	
	function validate() {
		return true;
	}
}
?>
