<?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"AdminIndex.php";
class Admin_admin_contents_page_Index2 extends Admin_admin_contents_page_Index
{
	function perform($view)
	{
		if(is_numeric($this->_sidx_code)&&is_numeric($this->_cat_code)) {
			$list = $this->getlistAll($this->_sidx_code, $this->_cat_code);
		}
		//index.phpページ有無チェック
		if($this->getPtype($this->_sidx_code,$this->_cat_code,$prev=true)) {
			$view->assign('Ptype1', true);
		}
		//リスト型index.php有無チェック
		/*pidx_ptype=1は廃止 v1.2
		$this->_listflg = $this->getPtypeList($this->_sidx_code,$this->_cat_code,$prev=true);
		$view->assign("Ptype2",$this->_listflg);
		*/
		$rtn = array();
		$rtn = Pager(AD_PAGER_LIMIT, $this->_p, $list, $method='GET', $urlvar='p');
		//url変換
		if(is_array($rtn["pagedata"])) {
			foreach($rtn["pagedata"] as $key => $val) {
				$rtn["pagedata"][$key]["url"] = pageView::getSiteURL($this->_sidx_data["sidx_dir"],$c=$this->_cat_data["cat_dir"],$p=$val["pidx_url_tmp"],$this->_sidx_data["sidx_ssl"]);
				$rtn["pagedata"][$key]["preurl"] = pageView::getSiteURL($this->_sidx_data["sidx_dir"],$c=$this->_cat_data["cat_dir"],$p=$val["pidx_url_tmp"],$this->_sidx_data["sidx_ssl"],$prev=true,$this->_mprev);
				$rtn["pagedata"][$key]["mpreurl"] = pageView::getSiteURL($this->_sidx_data["sidx_dir"],$c=$this->_cat_data["cat_dir"],$p=$val["pidx_url_tmp"],$this->_sidx_data["sidx_ssl"],$prev=false,$this->_mprev);
			}
		}
		$view->assign('list', $rtn["pagedata"]);
		$view->assign('Pages', $rtn["pageNavi"]["all"]);
		$view->assign('sidx_data',$this->_sidx_data);
		$view->assign('cat_data',$this->_cat_data);
		
		
		//ツリービュー
		$tree = $this->getTreeView($view);
		if($tree!=null) {
			$view->assign("treeView", $tree);
		} else {
			return 'contents/page/err.tpl';
		}
        return 'contents/page/index2.tpl';
	}
	
	function getTreeView(&$view) {
		$sectionIdxDB = New sectionIdxDB();
		$list = array(); //セクションリスト
		if($_SESSION["usr_info"]["usr_kanri"]!=1){
			$list = $sectionIdxDB->getlistAll($_SESSION["usr_info"]["usr_code"]);
		} else {
			$list = $sectionIdxDB->getlistAll();
		}
		$clist = array(); //セクションごとのカテゴリ格納ツリー
		if(is_array($list)) {
			foreach($list as $key=>$val) {
				$clist[$val["sidx_code_tmp"]]=array();
			}
		} else {
			//リストが無い場合は許可されているセクションがなし
			return null;
		}
		$categoryDB= new categoryDB();
		$call = $categoryDB->getlistAll();
		foreach($clist as $key=>$val){
			foreach($call as $ckey=>$cval) {
				if($cval["cat_sidx_code_tmp"] == null || $cval["cat_sidx_code_tmp"]==$key) {
					if($list[$cval["cat_sidx_code_tmp"]]["sidx_view_tmp"]!=2) {
						$clist[$key][] = $cval;
					}
				}
			}
		}
		$js = null;
		//sectionツリー
		if(is_array($list)) {
			$rootflg=false;
			foreach($list as $key=>$val) {
				if($val["sidx_view_tmp"]!=2) {
					$js .= 'var secp = {label:"'.htmlspecialchars($val["sidx_title_tmp"]).'", href:"index2.php?cat_code=1&sidx_code='.$val["sidx_code_tmp"].'"};'."\n";
					$open_flg = 'false';
					if($this->getRequest("sidx_code") == $val["sidx_code_tmp"]) {
						$open_flg = 'true';
					}
					$open_flg = 'true';

					//全部開いておく
					$open_flg = 'true';
					if($val["sidx_code_tmp"]==1) {
						$js .= 'var root = new YAHOO.widget.TextNode(secp, tree.getRoot(), true);'."\n";
						$rootflg = true;
					} else {
						$js .= 'var sec'.$val["sidx_code_tmp"].' = new YAHOO.widget.TextNode(secp, root, '.$open_flg.');'."\n";
					}
				}
			}
			if(!$rootflg) {
				$js = 'var rootp = { label:"ホーム(公開中,選択不可)", href:""};var root = new YAHOO.widget.TextNode(rootp, tree.getRoot(), true);'.$js;
			}
		}
		//カテゴリツリー
		$cnt = 0;
		foreach($clist as $ckey => $crecord) {
			foreach($crecord as $cckey => $ccrecord) {
				$jscat = null;
				if($ccrecord["cat_code_tmp"]!=null && $ccrecord["cat_code"]!=1) {
					$jscat = 'var catp = {label:"'.htmlspecialchars($ccrecord["cat_name_tmp"]).'", href:"index2.php?sidx_code='.$ckey.'&cat_code='.$ccrecord["cat_code_tmp"].'"};'."\n";
				}
				$open_flg = 'false';
				if($this->getRequest("cat_code") == $ccrecord["cat_code_tmp"]) {
					$open_flg = 'true';
				}
				if($jscat!=null) {
					$js .= $jscat;
					$js .= 'var cat'.$cnt.' = new YAHOO.widget.TextNode(catp, sec'.$ckey.', '.$open_flg.');'."\n";
				}
				$cnt++;
			}
		}
		return $js;
	}
	
	function setMprev() {
		$this->_mprev = true;
	}
}
?>
