<?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
 */

/**
 * @package Scripts
 */

// 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/core/types/TypesManager.class.php');
include_once('classes/core/types/Type.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 ROOT in order to edit templates
$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();
}

$dbManipulator = &$site->getDBManipulator();
$templatesManager = &$site->getTemplatesManager();

if(isset($_GET['template_name']) && isset($_GET['type_name'])){

  $template_name  = $_GET['template_name'];
  $type_name      = $_GET['type_name'];


  $template_id = $templatesManager->getTemplateId($template_name, $type_name);
  $template = &$templatesManager->getTemplate($template_id);

  if(isset($_POST['content'])) {
    $template->setTemplateXSL(urldecode($_POST['content']));
    $templatesManager->storeTemplate($template_id);
  }

  $code = '';
  $fileType = 'xsl';


  $code = $template->getTemplateXSL();

  $code = ereg_replace("&(.*);", "&amp;\\1;", $code);

  $code = preg_replace("/>/","&gt;",$code);
  $code = preg_replace("/</","&lt;",$code);
  $code = str_replace("\'","'",$code);

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="codepress/codepress.js"></script>
<style>
html, body {
  margin: 0;
  padding: 0;
}
</style>
</head>
<body>
<textarea id="cp" class="codepress <?php echo $fileType; ?> autocomplete-off" style="width:650px;height:335px;" wrap="off"><?php echo $code; ?></textarea>
<form id="form1" action="" method="post">
<input id="content" type="hidden" name="content" value="">
<input id="template_name" type="hidden" name="template_name" value="<?php echo $template_name; ?>">
<input id="type_name" type="hidden" name="type_name" value="<?php echo $type_name; ?>">
</form>
<script>
function saveContent() {
  var code = encodeURIComponent(cp.getCode());
  document.getElementById('content').value = code;
  document.getElementById('form1').submit();
}
</script>
</body>
</html>
<?php
}
else {

  $treeListWidth = 450;
  $treeListTop = 10;
  
  $manager = &egManager::getInstance();
  $main = &new egContainer("main_ctn", 0, 0, 0, 0);

  $listContainer = &new egContainer("list_ctn", 12, $treeListTop+2, $treeListWidth-3, 117, true);
  $frame = &new egFrame("fra_1", "", 10, $treeListTop, $treeListWidth, 120);

  $vPos = 2;
  $i = 1;
	$result_array = $dbManipulator->getRecord(TEMPLATES_TABLE, -1, '', 'id', 'name');

	while($row = $dbManipulator->fetchArray($result_array)){
    $template_name  = $row['name'];
    $type_name      = $row['type'];
    $action = "setSelectedTemplate(\'$template_name\', \'$type_name\');";
    $link = &new egLink('template_name_'.$i, $template_name, 10, $vPos, 200, 18, $action, "interface/icons/icon_template_xml.png");
    $type = &new egStatic('template_type_'.$i, $type_name, 220, $vPos, 200, 18, 0, false);
    $listContainer->addObject($link);
    $listContainer->addObject($type);
  	$vPos += 20;
    ++$i;
  }

	$fileNameStatic = &new egStatic("file_name_static", LANG_TEMPLATE_MENU_SELECTED." : ", 10, 140, 130, 0, true);
	$fileNameValue  = &new egStatic("file_name_value", "", 145, 140, 350, 0, false);

	$createLink = &new egLink("lnk_create", LANG_TEMPLATE_MENU_NEW, 470, 15, 200, 18, "launchCreateNodeWindow();", "interface/icons/icon_add_template.png");
	$renameLink = &new egLink("lnk_rename", LANG_TEMPLATE_MENU_RENAME, 470, 35, 200, 18, "launchFileRenamingWindow();", "interface/icons/edit.png");
	$deleteLink = &new egLink("lnk_delete", LANG_TEMPLATE_MENU_REMOVE, 470, 55, 200, 18, "launchFileDeletionWindow();", "interface/icons/remove.png");


	$subWindow = &new egSubWindow("sbw_1", "edit_frame.php?path=../../admin/codepress/empty.xhtml", 11, 161, 665, 350);

	$saveButton = &new egPressbutton("btn_save", LANG_FILE_MENU_SAVE, 595, 520, 80, null, "clickOKButton();");

	$main->addObject($fileNameStatic);
	$main->addObject($fileNameValue);

	$main->addObject($createLink);
	$main->addObject($renameLink);
	$main->addObject($deleteLink);

	$main->addObject($saveButton);

  $main->addObject($frame);
  $main->addContainer($listContainer);

	$main->addObject($subWindow);


  $scripts = new egScript();
  $line = "
var selectedType = '';
var selectedTemplate = ''
function clickOKButton() {
  if(selectedTemplate != '' && selectedType != '') {
    if(confirm('Are you sure you want to save the modifications and overwrite the template :\\n'+ selectedTemplate + '?')) {
  	 document.getElementById('sbw_1').contentWindow.saveContent();
    }
  }
}
function launchObjecteRenamingWindow() {
  openWindow('rename_object.php?'+'mode=ask_rename_object',330, 150, 0, 0);
}
function launchObjectDeletionWindow() {
  openWindow('delete_object.php?'+'mode=ask_delete_object',330, 150, 0, 0);
}
function launchCreateObjectWindow() {
  openWindow('create_object.php?'+'mode=ask_create_object',330, 150, 0, 0);
}
function setSelectedTemplate(template, type) {
	selectedTemplate = template;
	selectedType = type;
	document.getElementById('file_name_value').innerHTML = template + ', ' + type;

	if(document.getElementById('sbw_1') != null) {
    document.getElementById('sbw_1').src = '".$_SERVER['PHP_SELF']."?template_name='+selectedTemplate+'&type_name='+selectedType;
	}
}
";

  $scripts->addLine($line);
  $manager->addScripts($scripts);

  $manager->addContainer($main);

  print($manager->getHTML());
}
?>