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

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

function &decodeURLPath($urlPath){
	return str_replace(',', '/', urldecode($urlPath));
}

$mode = $site->getSubmittedVar('mode');

switch($mode) {
	case ASK_CREATE_NODE :
		$name = $site->getSubmittedVar('name');
		$type = $site->getSubmittedVar('type');
    $empty = $site->getSubmittedVar('empty');
    $default_name = $site->getSubmittedVar('default', '');

		$manager = &egManager::getInstance();
		$main = &new egContainer("main_ctn", 0, 0, 0, 0);
		switch($type) {
			case DIR_NODE :
				$txtValue = &new egStatic("txt_value_1", LANG_NODE_NEW_DIR, 10, 15, 200, 0);
				$nameValue = &new egStatic("name_value_1", LANG_DIR_NAME_STATIC.LANG_COLON, 10, 50, 80, 0, true);
				$nameItem = &new egEdit("name_item_1", "", 70, 50, 150, 18, 'name');
				$okButton = &new egPressbutton("btn_ok", LANG_OK_BUTTON, 130, 100, 80, null, "clickOKButton();");
				$cancelButton = &new egPressbutton("btn_cancel", LANG_CANCEL_BUTTON, 215, 100, 80, null, "clickCancelButton();");

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

				$objMode = &new egEdit("hidden_control_mode", CREATE_NODE, 0, 0, 0, 0, "mode");
				$objType = &new egEdit("hidden_control_type", $type, 0, 0, 0, 0, "type");
				$objPath = &new egEdit("hidden_control_path", $name, 0, 0, 0, 0, "path");

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

				$manager->addContainer($hiddenValuesContainer);

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

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

				$scripts = &new egScript();
				$line = "
        function clickOKButton() {
        	document.forms[0].submit();
        }
        function clickCancelButton() {
        	window.close();
        }
        ";
				$scripts->addLine($line);
				$manager->addScripts($scripts);
				break;
			case FILE_NODE :
				$txtValue = &new egStatic("txt_value_1", LANG_NODE_CHOOSE_FILE, 10, 15, 250, 0);
				$nameValue = &new egStatic("name_value_1", LANG_FILE_NAME_STATIC.LANG_COLON, 10, 50, 80, 0, true);
				$okButton = &new egPressbutton("btn_ok", LANG_OK_BUTTON, 130, 100, 80, null, "clickOKButton();");
				$cancelButton = &new egPressbutton("btn_cancel", LANG_CANCEL_BUTTON, 215, 100, 80, null, "clickCancelButton();");

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

				$objMode = &new egEdit("hidden_control_mode", CREATE_NODE, 0, 0, 0, 0, "mode");
				$objType = &new egEdit("hidden_control_type", $type, 0, 0, 0, 0, "type");
				$objPath = &new egEdit("hidden_control_path", $name, 0, 0, 0, 0, "path");
				$objEmpty = &new egEdit("hidden_control_empty", $empty, 0, 0, 0, 0, "empty");

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

        if($empty != null) {
				  $hiddenValuesContainer->addObject($objEmpty);
        }

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

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

				$manager->addContainer($hiddenValuesContainer);


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

        if($empty == null) {
   				$scripts->addLine("document.forms[0].innerHTML += '<input type=\'file\' name=\'file\' style=\'position: absolute; display: block;left: 55px; top: 50px;\' />';");
        }
        else {
          $nameItem = &new egEdit("name_item_1", $default_name, 70, 50, 130, 18, 'name');
				  $main->addObject($nameItem);
        }

 				$manager->addScripts($scripts);

				break;
		}

		$manager->addContainer($main);
		print($manager->getHTML());
		break;

	case CREATE_NODE :
		$name = decodeURLPath($site->getSubmittedVar('name'));
		$path = decodeURLPath($site->getSubmittedVar('path'));
		$type = $site->getSubmittedVar('type');
    $empty = $site->getSubmittedVar('empty');

		if ($type == DIR_NODE) {
			$node = &new Dir($name, REPOSITORY_FOLDER."/".$path, true);
		}
		else{
      if($empty != null) {
        $node = &new File($name, REPOSITORY_FOLDER."/".$path, true, '');
      }
      else {
  			foreach($_FILES as $file) {
  				if (is_uploaded_file($file["tmp_name"])) {
  					$name = $file["name"];
  					$node = &new File($name, REPOSITORY_FOLDER."/".$path, true, file_get_contents($file["tmp_name"]));
  				}
  			}
      }
		}

		$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);
		print($manager->getHTML());
		break;
}
?>