<?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(); 

//security check : user must be identified to rename a type
$user = $site->getCurrentUser();
if(ENABLE_ADMIN_AREA == 0 || is_null($user) || $user->getUserId() != USER_ID_ROOT) {
	die("You do not have permissions to create types: access denied.");
	exit();
}


$mode 		= $site->getSubmittedVar('mode', ASK_RENAME_TYPE);
$type 	 	= $site->getSubmittedVar('type');
$newName 	= $site->getSubmittedVar('new_name', $type);

$message = '';

$typesManager = &$site->getTypesManager();

if(!is_null($newName) && !$typesManager->isValidTypeName($newName)) {
  $mode = ASK_RENAME_TYPE;
  $message .= LANG_TYPE_RENAME_INVALID;
}

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


switch ($mode) {
	case ASK_RENAME_TYPE :
    if(strlen($message) == 0) {
      $message = LANG_TYPE_RENAME_NEW_NAME;
    }

		$txtValue = &new egStatic("txt_value_1", $message.LANG_COLON, 10, 15, 350, 0, false);
		$nameValue = &new egStatic("name_value_1", LANG_TYPE_RENAME_NEW_NAME_STATIC.LANG_COLON, 10, 60, 100, 0, true);

		$newNameItem = &new egEdit("new_name_item", $newName, 110, 60, 200, 18, 'new_name');

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

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

		$objMode = &new egEdit("hidden_control_mode", RENAME_TYPE, 0, 0, 0, 0, 'mode');
		$objType = &new egEdit("hidden_control_path", $type, 0, 0, 0, 0, 'type');

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

		$manager->addContainer($hiddenValuesContainer);

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

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

		$scripts = &new egScript();
		$line = "function clickOKButton() {
    document.forms[0].submit();
    }
    function clickCancelButton() {
    window.close();
    }
    ";
		$scripts->addLine($line);
		$manager->addScripts($scripts);

		break;
	case RENAME_TYPE :

    if($typesManager->isValidTypeName($newName)) {
      $typesManager->renameType($type, $newName);
    }

		$scripts = new egScript();
		$line = "window.opener.location.reload();window.close();";
		$scripts->addLine($line);
		$manager->addScripts($scripts);
		break;
}
print($manager->getHTML());
?>