<?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/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 ROOT in order to edit frames
$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();
}


if(isset($_POST['path'])){
  $_GET['path'] = $_POST['path'];
}



if(isset($_GET['path'])){

	$File = &new File($_GET['path'], REPOSITORY_FOLDER.'/'.TEMPLATES_FOLDER);
  $file_name = $File->getNodeFullName();
  
  if(isset($_POST['content'])){
    $code = urldecode($_POST['content']);
    $fp = fopen($file_name, 'w');
    fwrite($fp, $code);
    fclose($fp);
  }
  
  $code = '';
  $fileType = 'generic';

  if(file_exists($file_name)) {
    $extension = $File->getExtension();
    $fileTypes = array('htm' => 'html', 'html' => 'html', 'xhtml' => 'xhtml', 'xsl' => 'xsl');

    foreach($fileTypes as $key=>$value) {
      if(strcasecmp($key, $extension) == 0) {
        $fileType = $value;
        break;
      }
    }

    $code = file_get_contents($file_name);
    $code = preg_replace("/>/","&gt;",$code);
    $code = preg_replace("/</","&lt;",$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="path" type="hidden" name="path" value="<?php echo $_GET['path'];?>">
</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;

	$dir = &new Dir(TEMPLATES_FOLDER, REPOSITORY_FOLDER);
	
	$listing = $dir->getListing();

	for($i = 0; $i < count($listing); ++$i){
    $node = &$listing[$i];
    $action = "setSelectedFile(\'".$node->getName()."\');";
    $link = &new egLink('template_file_'.$i, $node->getName(), 10, $vPos, 200, 18, $action, "interface/icons/icon_template.png");
    $listContainer->addObject($link);
  	$vPos += 20;
	}

	$fileNameStatic = &new egStatic("file_name_static", LANG_FILE_MENU_SELECTED." : ", 10, 140, 120, 0, true);
	$fileNameValue  = &new egStatic("file_name_value", "", 140, 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");

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

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



	$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 selectedFile = '';
function clickOKButton() {
  if(selectedFile != '') {
    if(confirm('Are you sure you want to save the modifications and overwrite the template :\\n'+ selectedFile + '?')) {
  	 document.getElementById('sbw_1').contentWindow.saveContent();
    }
  }
}
function launchFileRenamingWindow() {
	var vars = 'path='+ '".REPOSITORY_FOLDER."/' + '".TEMPLATES_FOLDER."/' + selectedFile;
  openWindow('rename_frame.php?'+'mode=ask_rename_file&'+vars,330, 150, 0, 0);
}
function launchFileDeletionWindow() {
	var vars = 'path='+ '".REPOSITORY_FOLDER."/' + '".TEMPLATES_FOLDER."/' + selectedFile;
  openWindow('delete_frame.php?'+'mode=ask_delete_file&'+vars,330, 150, 0, 0);
}
function launchCreateNodeWindow() {
  var vars = 'empty=1&default=.xhtml&type=file&name='+ '".TEMPLATES_FOLDER."';
  openWindow('create_frame.php?'+'mode=ask_create_node&'+vars,330, 150, 0, 0);
}
function setSelectedFile(file) {
	selectedFile = file;
	document.getElementById('file_name_value').innerHTML = '".REPOSITORY_FOLDER."/' + '".TEMPLATES_FOLDER."/' + file;

	if(document.getElementById('sbw_1') != null) {
    document.getElementById('sbw_1').src = '".$_SERVER['PHP_SELF']."?path=' + file;
	}
}
";

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

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