<?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 browse repositories
$user = $site->getCurrentUser();
if(!$user || $user->getUserId() == USER_ID_ANONYMOUS) {
	die("access denied");
	exit();
}

function &encodeDirList($node){
	$path = '';
	if(is_object($node)) {
		do{
	    if(strlen($path)) {
				$path = $node->getName().','.$path;
			}
			else {
			  $path = $node->getName();
			}
			$node = $node->getParentNode();
		}while(is_object($node));
	}
	return urlencode($path);
}

function createTree(&$treeList, &$parentNode, &$dir, &$treeNodesIdList, $level) {
	$uniqueId = 0;

	if(!is_object($dir)) return;

	$listing = &$dir->getListing();

	for ($i = 0; $i < count($listing); ++$i){
    $node = &$listing[$i];
	  if($node->getNodeType() == DIR_NODE) {
	    $treeNodeId = "trn_".$level."_".(++$uniqueId);
			$treeNodeName = $treeNodesIdList."_".$treeNodeId;
			$treeNode = &new egTreeNode($treeNodeId, $node->getName(), "interface/icons/folder_closed.png", "interface/icons/folder_open.png", "0", "", "setCurrentDir(\'".encodeDirList($node)."\');");
			//$treeNode = &new egTreeNode($treeNodeId, $node->getName(), "interface/icons/folder_closed.png", "interface/icons/folder_open.png", "0", "", "");
			createTree($treeList, $treeNode, $node, $treeNodeName, $level+1);
			$parentNode->addChild($treeNode);
	  }
	}
}


$parentControlName = $site->getSubmittedVar('parent_control_name');
$repositoryType = $site->getSubmittedVar('repository_type');

switch($repositoryType) {
  case IMAGES_FOLDER :
    break;
  case DOCUMENTS_FOLDER :
    break;
  default :
		$repositoryType = DOCUMENTS_FOLDER;
    break;
}

// display the website tree
$manager = &egManager::getInstance();
$main = &new egContainer("main_ctn", 0, 0, 0, 0);

$treeListWidth = 400;

if($mode == 'admin') {
  $treeListWidth = 660;
}

$dir = new Dir($repositoryType, REPOSITORY_FOLDER);

$treeList = &new egTreeList("trl_1", "repository", 10, 10, $treeListWidth, 380, "choice_tree");
$treeNodeRoot = &new egTreeNode("trn_r", $repositoryType, "interface/icons/folder_closed.png", "interface/icons/folder_open.png", "0", "", "document.getElementById(\'sbw_1\').src=\'".$_SERVER['PHP_SELF']."?path=".encodeDirList($dir)."&id=trn_r&list=".$treeList->getId()."\';");
createTree($treeList, $treeNodeRoot, $dir, $treeNodeRoot->getId(), 0);
$treeList->insertRootNode($treeNodeRoot);


if($mode == 'admin'){
		$linkAdd = &new egLink("lnk_add", LANG_FILE_MANAGER_CREATE_SUB_DIR, 10, 420, 200, 18, "createSubDir()", "interface/icons/add.png");
		$linkRemove = &new egLink("lnk_remove", LANG_FILE_MANAGER_REMOVE_DIR, 10, 480, 200, 18, "removeDir()", "interface/icons/remove.png");
	  $main->addObject($linkAdd);
		$main->addObject($linkRemove);
}
else {
	$okButton = &new egPressbutton("btn_1", "Ok", 250, 410, 80, null, "clickOKButton();");
	$cancelButton = &new egPressbutton("btn_2", "Cancel", 335, 410, 80, null, "clickCancelButton();");
	$main->addObject($okButton);
	$main->addObject($cancelButton);
}


$main->addObject($treeList);

$manager->addContainer($main);

$scripts = new egScript();

$line = "function clickOKButton() {
	sendResponse();
}
function clickCancelButton() {
	window.close();
}
var currentDir = '".$repositoryType."/';
function setCurrentDir(dir) {
	currentDir = dir;
}
function launchDirDeletionWindow() {
	var vars = 'path='+ '".REPOSITORY_FOLDER."/' + currentDir;
  openWindow('delete_node.php?'+'mode=ask_delete_dir&'+vars,330, 150, 0, 0);
}
function launchCreateNodeWindow(vars) {
  openWindow('create_node.php?'+'mode=ask_create_node&'+vars,330, 150, 0, 0);
}
function removeDir() {
	launchDirDeletionWindow();
}
function createSubDir() {
	launchCreateNodeWindow('type=dir&name='+currentDir);
}
function decodeURLPath(urlPath){
  s = new String(urlPath);
  s = s.replace(/%2C/g, '/');
	return s;
}

function sendResponse() {
	window.opener.document.getElementById('".$parentControlName."').value= '".REPOSITORY_FOLDER."' + '/' + decodeURLPath(currentDir);
	window.close();
}";
$scripts->addLine($line);

$manager->addScripts($scripts);

echo $manager->getHTML();

?>