<?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('classes/system/Dir.class.php');
include_once('classes/system/File.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(); // constructor checks the $_GET, $_POST and $_SESSION arrays

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

$mode 					= $site->getSubmittedVar('mode', ASK_MOVE_PAGE);
$pageId	 				= $site->getSubmittedVar('page_id');
$newParentId		= $site->getSubmittedVar('parent_id');

$pagesManager = &$site->getPagesManager();
$invalid = false;

if(!isset($pageId)) {
	die('missing parameter');
	exit(0);
}

if(isset($newParentId)) {
	list($script, $params) = explode("?", $newParentId);
	$paramsTable = explode("&", $params);
	foreach($paramsTable as $param) {
		list($paramName, $paramValue) = explode("=", $param);
		if($paramName=="pid") {
			$newParentId = $paramValue;
			break;
		}
	}
}

if(isset($newParentId)) {
	if(!$pagesManager->pageExists($newParentId)) {
		// invalid new parent page
		$mode  = ASK_MOVE_PAGE;
		$invalid = true;
		if($pagesManager->pageExists($pageId) && $page=$pagesManager->getPage($pageId)) {
			$newParentId = $page->getPageParentId();
		}
	}
	else if($newParentId == $pageId) {
		$mode  = ASK_MOVE_PAGE;
		$invalid = true;
		if($pagesManager->pageExists($pageId) && $page=$pagesManager->getPage($pageId)) {
			$newParentId = $page->getPageParentId();
		}
	}
}
else {
	if($pagesManager->pageExists($pageId) && $page=$pagesManager->getPage($pageId)) {
		$newParentId = $page->getPageParentId();
	}
}


if(!$pagesManager->pageExists($pageId)) {
	$mode = MOVE_PAGE;
}

switch ($mode) {
	case ASK_MOVE_PAGE :
		$manager = &egManager::getInstance(LANG_PAGE_MOVE_TITLE);
		$main = &new egContainer("main_ctn", 0, 0, 0, 0);
		$manager->addContainer($main);

		$stringTxtValue = "";
		$stringTxtBold = false;

		if($invalid) {
			$stringTxtValue = LANG_PAGE_MOVE_INVALID;
			$stringTxtBold = true;
		}
		else {
			$stringTxtValue = LANG_PAGE_MOVE_CHOOSE_PARENT;
		}

		$txtValue = &new egStatic("txt_value_1", $stringTxtValue, 10, 15, 350, 0, $stringTxtBold);
		$nameValue = &new egStatic("name_value_1", LANG_PAGE_MOVE_NEW_PARENT_STATIC.LANG_COLON, 10, 60, 130, 0, true);

		$newParentItem = &new egEdit("new_parent_item", "index.php?pid=".$newParentId, 135, 60, 130, 18, "parent_id");

		$chooseButton = &new egPressbutton("btn_choose", LANG_BROWSE_BUTTON, 275, 58, 80, null, "launchPageSelectionWindow();");

		$okButton = &new egPressbutton("btn_ok", LANG_OK_BUTTON, 190, 100, 80, null, "clickOKButton();");
		$cancelButton = &new egPressbutton("btn_cancel", LANG_CANCEL_BUTTON, 275, 100, 80, null, "clickCancelButton();");

		$hiddenValuesContainer = &new egContainer("ctn_hidden_values", 0, 0, 1, 1);

		$objMode = &new egEdit("hidden_control_mode", MOVE_PAGE, 0, 0, 0, 0, "mode");
		$objPage = &new egEdit("hidden_control_page", $pageId, 0, 0, 0, 0, "page_id");

		$hiddenValuesContainer->addObject($objMode);
		$hiddenValuesContainer->addObject($objPage);

		$manager->addContainer($hiddenValuesContainer);

		$main->addObject($chooseButton);
		$main->addObject($okButton);
		$main->addObject($cancelButton);

		$main->addObject($txtValue);
		$main->addObject($nameValue);
		$main->addObject($newParentItem);

		$scripts = &new egScript();
		$line = "function clickOKButton() {
document.forms[0].submit();
}
function clickCancelButton() {
window.close();
}
function launchPageSelectionWindow(vars) {
  openWindow('select_page_id.php?'+'parent_control_name=new_parent_item_value&'+vars,425, 450, 0, 0);
}
";
		$scripts->addLine($line);
		$manager->addScripts($scripts);

		echo $manager->getHTML();

		break;
	case MOVE_PAGE :
		if($pagesManager->pageExists($pageId) && $pagesManager->pageExists($newParentId) && !($pagesManager->isSubPage($pageId, $newParentId)) && ($page=&$pagesManager->getPage($pageId))) {
			$page->setPageParentId($newParentId);
			$pagesManager->storePage($pageId);
		}

		$manager = &egManager::getInstance();
		$main = &new egContainer("main_ctn", 0, 0, 0, 0);
		$manager->addContainer($main);

		$scripts = new egScript();
		$line = "window.opener.location.reload();window.close();";
		$scripts->addLine($line);

		$manager->addScripts($scripts);
		echo $manager->getHTML();

		break;
}
?>