<?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/sectionIdxDB.php');
require_once (LIB_PATH.'/common/categoryDB.php');
require_once (LIB_PATH.'/common/pageIdxDB.php');
require_once (LIB_PATH.'/common/pageView.php');
class Admin_admin_contents_page_Edit extends pageIdxDB
{
	function prepare($view)
	{
		$this->_form = new HTML_QuickForm('form','post',$_SERVER['PHP_SELF'],'',null,true);
		$this->_sidx_code = $this->getRequest('sidx_code') ? $this->getRequest('sidx_code') : "";
		$this->_cat_code = $this->getRequest('cat_code') ? $this->getRequest('cat_code') : "";
		$this->_pidx_ptype = $this->getRequest('pidx_ptype') ? $this->getRequest('pidx_ptype') : 0;
		//v1.2からpidx_ptype:2は廃止
		//$this->_listflg = $this->getRequest('listflg') ? $this->getRequest('listflg') : $this->getPtypeList($this->_sidx_code,$this->_cat_code,$prev=true);
		
		$this->_mprev = $this->getRequest('mprev') ? true : false;
		
		
		if(is_numeric($this->_sidx_code)) {
			$this->_sidx_data = sectionIdxDB::getData($this->_sidx_code,$this->_mprev);
		}
		if(is_numeric($this->_cat_code)) {
			$this->_cat_data = categoryDB::getData($this->_cat_code,$this->_mprev);
		} else if(is_numeric($this->getRequest('pidx_cat_code'))) {
			$this->_cat_code = $this->getRequest('pidx_cat_code');
			$this->_cat_data = categoryDB::getData($this->_cat_code,$this->_mprev);
		}
		return null;
	}

	function perform($view)
	{
		$req = $this->getRequest('buttons');
		if($req== null) {
			unset($_SESSION["tmpImage"]);
			//何もクリックされていない場合は情報取得
			if(is_numeric($this->getRequest("pidx_code"))) {
				$this->_pidx_code = $this->getRequest("pidx_code");
				$record = $this->getTmpData($this->_pidx_code);
			} else {
				//新規登録時のデフォルト設定
				$pidx_ptype = $this->getrequest("pidx_ptype");
				if(!is_numeric($pidx_ptype)) {
					$pidx_ptype = 0;
				}
				$record = array("pidx_sidx_code"=>$this->_sidx_code,
								"pidx_cat_code"=>$this->_cat_code,
								"pidx_active"=>1,
								"pidx_view"=>0,
								"pidx_date"=>date('Y-m-d'),
								"pidx_ptype"=>$pidx_ptype,
								"pidx_comment_flg"=>0,
								"pidx_order"=>0);
			}
			$this->_form->setDefaults($record);

		}
		$this->setElements();
		$this->_form->addElement('hidden','mprev', $this->_mprev);
		$this->setRules();
		$this->setButtons();
		
		$index_path = "index.php";
		if( $this->_mprev) {
			$index_path = "index2.php";
		}
		
		if (isset($req['BTNList'])&& $req['BTNList']!=null) {	// 一覧表示がクリックされたとき
			header('location:'.$index_path.'?sidx_code='.$this->_sidx_code.'&cat_code='.$this->_cat_code);
			exit();	
		} elseif (isset($req['BTNnext'])&&$req['BTNnext']!=null) {
			$record["pidx_comment_pic"] = $this->getRequest('pidx_comment_pic');
			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=PIDX_IMG_W,$height=PIDX_IMG_H,$force=false);//画像プレ保存
				}
				if(!$result) {
					$this->_form->freeze();
					$this->setButtons($this->_form->isFrozen()); //ボタンの作り直し
					if(isset($_SESSION["tmpImage"])) { //新規画像登録がある場合はprev.tplで見せないように
						$record["pidx_comment_pic"] = null;
					}
				} else {
					$this->_form->setElementError("picture",$result);
				}
			}
		} else if((isset($req['BTNsave'])&&$req['BTNsave']!=null) || (isset($req['BTNsaveandback'])&&$req['BTNsaveandback']!=null)) {
			if($this->_form->validate()){
				$post = $this->_form->getSubmitValues();
				$rtn = $this->doSave($post);
				if($rtn==null) {
					$this->clrToken();
					if(isset($req['BTNsave'])&&$req['BTNsave']!=null) {
						header('location:layout.php?pidx_code='.$this->_pidx_code.'&sidx_code='.$post["sidx_code"].'&cat_code='.$post["pidx_cat_code"].'&mprev='.$this->_mprev);
						exit();
					} else {
						header('location:'.$index_path.'?sidx_code='.$post["sidx_code"].'&cat_code='.$post["pidx_cat_code"]);
						exit();
					}
				} else {
					$this->_form->setElementError("",$rtn);
				}
			}
		//削除ボタンクリック時
		}
		else if(isset($req['BTNdelete'])&&$req['BTNdelete']!=null) {
			$post = $this->_form->getSubmitValues();
			if(!check_token($post["token"])) {
				$this->putTokenError();
			}
			$rtn = $this->delete($post);
			if($rtn==null) {
				$this->clrToken();
				header('location:'.$index_path.'?sidx_code='.$this->_sidx_code.'&cat_code='.$this->_cat_code);
				exit();	
			} else {
				$this->_form->setElementError("",$rtn);
			}
		}
		if(empty($this->_form->_submitValues)) {
			$record["token"] = $this->getToken();
			$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('sidx_data',$this->_sidx_data);
		$view->assign('cat_data',$this->_cat_data);

		$view->assign('pidx_comment_pic',$record["pidx_comment_pic"]);
		$view->assign('PIDX_IMG_PATH', PIDX_IMG_PATH);
		$view->assign('PIDX_IMG_W', PIDX_IMG_W);
		$view->assign('PIDX_IMG_H', PIDX_IMG_H);
		
		//ページURL取得
		$p_path = pageView::getSiteURL($d=$this->_sidx_data["sidx_dir"],$c=$this->_cat_data["cat_dir"],$p=null,$ssl=$this->_sidx_data["sidx_ssl"],$prev=false);
		if(!URLTYPE) {
			if(strstr($p_path,".php?")){
				$p_path .= "&p=";
			}else{
				$p_path .= "?p=";
			}
		}
		$view->assign("PATH",$p_path);
		
		
		
		if($this->_form->isFrozen()) {
			return 'contents/page/prev.tpl';
		} else {
			return 'contents/page/edit.tpl';
		}
	}
}
?>