<?php

class page_tree {

function make($application_id,$folders,$pages) {
	include_once 'HTML/TreeMenu.php';
	$base_url = quicty_base_url();
	$tree  = new HTML_TreeMenu();
	$top_node = new HTML_TreeNode(array('text'=>"top page：(<a href=\"$base_url/page/?application_id=".$application_id."&folder_id=-1\">トップページに機能を追加</a>)"));
	if(is_array($folders) and count($folders)>0)
	  foreach($folders as $folder) {
		$folder_id = $folder['id'];
		$folder_name = $folder['name'];
		$folder_node[$folder_id] = new HTML_TreeNode(array('text'=>$folder_name));
		$folder_node[$folder_id]->setOption('link',"$base_url/folder/edit?id=$folder_id");
		$folder_node[$folder_id]->setOption('icon','folder.gif');
		$folder_node[$folder_id]->setOption('expandedIcon','folder-expanded.gif');
	}

	if(is_array($pages) and count($pages)>0)
	  foreach($pages as $page) {
		$folder_id = $page['folder_id'];
		$page_id = $page['id'];
		$page_name = $page['name'];
		if($folder_id=='-1') {
			$top_node->setOption('text',"top page($page_name)");
			$top_node->setOption('link',"$base_url/page/edit?id=$page_id");
		} elseif($folder_id=='0') {
			$page_node[$page_id] = new HTML_TreeNode(array('text'=>$page_name));
			$page_node[$page_id]->setOption('link',"$base_url/page/edit?id=$page_id");
			$top_node->addItem($page_node[$page_id]);
		} else {
			$page_node[$page_id] = new HTML_TreeNode(array('text'=>$page_name));
			$page_node[$page_id]->setOption('link',"$base_url/page/edit?id=$page_id");
			$folder_node[$folder_id]->addItem($page_node[$page_id]);
		}
	}
	$add_new_page[0] = new HTML_TreeNode(array('text'=>'ページを追加'));
	$add_new_page[0]->setOption('link',"$base_url/page/?application_id=".$application_id.'&folder_id=0');
	$top_node->addItem($add_new_page[0]);
	$add_template[0] = new HTML_TreeNode(array('text'=>'パッケージを追加'));
	$add_template[0]->setOption('link',"$base_url/add_package?id=".$application_id);
	$top_node->addItem($add_template[0]);

	if(is_array($folders) and count($folders)>0)
	  foreach($folders as $folder) {
		$folder_id = $folder['id'];
		$add_new_page[$folder_id] = new HTML_TreeNode(array('text'=>'ページを追加'));
		$add_new_page[$folder_id]->setOption('link',"$base_url/page/?application_id=".$application_id.'&folder_id='.$folder_id);
		$folder_node[$folder_id]->addItem($add_new_page[$folder_id]);
		$add_template[$folder_id] = new HTML_TreeNode(array('text'=>'パッケージを追加'));
		$add_template[$folder_id]->setOption('link',"$base_url/add_package?id=".$application_id.'&folder_id='.$folder_id);
		$folder_node[$folder_id]->addItem($add_template[$folder_id]);
		$top_node->addItem($folder_node[$folder_id]);
	}
	$add_new_folder = new HTML_TreeNode(array('text'=>'フォルダを追加'));
	$add_new_folder->setOption('link',"$base_url/folder/?application_id=".$application_id);
	$add_new_folder->setOption('icon','folder.gif');
	$top_node->addItem($add_new_folder);

	$tree->addItem($top_node);
	$image_dir = "$base_url/img/treemenu";
	$page_tree = new HTML_TreeMenu_DHTML($tree,array('images'=>$image_dir));
	return $page_tree;
}

} // end of class page_tree

?>

