<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS 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 2 of the License, or
 * (at your option) any later version.
 *
 * easy-CMS 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 easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

// define the path context
chdir('..');
define('EASY_GUI_APP_DIR', '../');


include_once('config.inc.php');
include_once('classes/core/Site.class.php');

include_once('interface/egScript.class.php');
include_once('interface/egManager.class.php');
include_once('interface/egContainer.class.php');
include_once('interface/egMenuItem.class.php');
include_once('interface/egFrame.class.php');
include_once('interface/egToolTip.class.php');
include_once('interface/egRichText.class.php');
include_once('interface/egLink.class.php');
include_once('interface/egStatic.class.php');
include_once('interface/egEdit.class.php');
include_once('interface/egCheckBox.class.php');
include_once('interface/egRadioButton.class.php');
include_once('interface/egPressButton.class.php');
include_once('interface/egDropDownListBox.class.php');
include_once('interface/egPropertyList.class.php');
include_once('interface/egPropertySheet.class.php');
include_once('interface/egPropertyPage.class.php');
include_once('interface/egTreeNode.class.php');
include_once('interface/egTreeList.class.php');
include_once('interface/egSubWindow.class.php');

$site = &Site::getInstance();

//security check : user must be identified to browse repositories
$user = $site->getCurrentUser();
if(!$user || $user->getUserId() == USER_ID_ANONYMOUS) {
	die("access denied");
	exit();
}

function createTree(&$pagesManager, &$parentNode, &$rootPage, &$treeNodesIdList, $level, $langId) {
	$uniqueId = 0;

	if(!is_object($rootPage)) return;

	$list = &$pagesManager->getSubPagesList($rootPage->getPageId());
	foreach($list as $pageId){
		if (is_null($page = &$pagesManager->getPage($pageId))) {
			break;
		}
		$pageLabel = $page->getPageName($langId)." ";
		if($page->getPageAlias($langId) != "") {
			$pageLabel .= "(".$page->getPageAlias($langId).")";
		}
    $treeNodeId = "trn_".$level."_".(++$uniqueId);
		$treeNodeName = $treeNodesIdList."_".$treeNodeId;
		$page_alias = $page->getPageAlias($langId);
		if( strlen($page_alias) > 0 && $page_alias[0] != '/') {
			$page_alias = '/'.$page_alias;
		}
		$treeNode = &new egTreeNode($treeNodeId, $pageLabel, "interface/icons/page.png", "interface/icons/page.png", "0", "", "setPid(".$page->getPageId().");setLink(\'$page_alias\');");
		createTree($pagesManager, $treeNode, $page, $treeNodesIdList, $level+1, $langId);
		$parentNode->addChild($treeNode);
	}
}


$pagesManager 			= &$site->getPagesManager();
$langId 						= $site->getPersistentVar("lid", $site->getDefaultLangId());
$parentControlName 	= $site->getSubmittedVar('parent_control_name');
$parentControlName2 = $site->getSubmittedVar('parent_control_name2');
$mode 							= $site->getSubmittedVar('mode');

$rootPageId = $site->getRootPageId();

if (is_null($rootPage = &$pagesManager->getPage($rootPageId))) {
	die("root page (id=$rootPageId) not found");
	exit();
}
$pageLabel = $rootPage->getPageName($langId);
if($rootPage->getPageAlias($langId) != "") {
	$pageLabel .= " (".$rootPage->getPageAlias($langId).")";
}
// display the website tree
$manager = &egManager::getInstance();
$main = &new egContainer("main_ctn", 0, 0, 0, 0);

$treeListWidth = 400;

if($mode == 'admin') {
  $treeListWidth = 660;
}

$treeList = &new egTreeList("trl_1", "select page", 10, 10, $treeListWidth, 380, "choice_tree");
$treeNodeRoot = &new egTreeNode("trn_r", $pageLabel, "interface/icons/page.png", "interface/icons/page.png", "0", "", "setPid(".$rootPage->getPageId().");");
createTree($pagesManager, $treeNodeRoot, $rootPage, $treeNodeRoot->getId(), 0, $langId);
$treeList->insertRootNode($treeNodeRoot);

if($mode == 'admin'){
		$linkAdd = &new egLink("lnk_add", LANG_PAGE_MENU_ADD, 10, 420, 200, 18, "launchPageCreationWindow()", "interface/icons/add.png");
		$linkMove = &new egLink("lnk_move", LANG_PAGE_MENU_MOVE, 10, 440, 200, 18, "launchPageMovingWindow()", "interface/icons/move.png");
		$linkRename = &new egLink("lnk_rename", LANG_PAGE_MENU_EDIT, 10, 460, 200, 18, "launchPageEditionWindow()", "interface/icons/edit.png");
		$linkRemove = &new egLink("lnk_remove", LANG_PAGE_MENU_REMOVE, 10, 480, 200, 18, "launchPageDeletionWindow();", "interface/icons/remove.png");
		$main->addObject($linkAdd);
		$main->addObject($linkMove);
		$main->addObject($linkRename);
		$main->addObject($linkRemove);
}
else {
	$okButton = &new egPressbutton("btn_1", "Ok", 250, 410, 80, null, "clickOKButton();");
	$cancelButton = &new egPressbutton("btn_2", "Cancel", 335, 410, 80, null, "clickCancelButton();");
	$main->addObject($okButton);
	$main->addObject($cancelButton);
}

$main->addObject($treeList);

$manager->addContainer($main);

$scripts = new egScript();

$line = "function clickOKButton() {
	sendResponse();
}
function clickCancelButton() {
	window.close();
}
var pid = ".$rootPageId.";
var link = '';
function setPid(id) {
	pid = id;
}
function setLink(slink) {
	link = slink;
}
function launchPageMovingWindow() {
	var vars = 'page_id='+ pid;
  openWindow('move_page.php?'+'mode=ask_move_page&'+vars,360, 150, 0, 0);
}
function launchPageEditionWindow() {
  openWindow('../index.php?'+'display=edit_page&mode=ask_modif_page&'+'page_id='+pid,540, 600, 0, 0);
}
function launchPageCreationWindow() {
  openWindow('../index.php?'+'display=edit_page&mode=ask_modif_page&'+'parent_id='+pid,540, 600, 0, 0);
}
function launchPageDeletionWindow() {
	openWindow('../index.php?'+'display=edit_page&mode=ask_delete_page&'+'page_id='+pid,380, 280, 0, 0);
}
function sendResponse() {
	var parentControlName2 = '".$parentControlName2."';
	if(link != '') {
		window.opener.document.getElementById('".$parentControlName."').value=link;
	}
	else {
		window.opener.document.getElementById('".$parentControlName."').value='index.php?pid='+pid;
	}
	if(parentControlName2.length > 0) {
		window.opener.document.getElementById(parentControlName2).value='';
	}
	window.close();
}";
$scripts->addLine($line);

$manager->addScripts($scripts);

echo $manager->getHTML();

?>