<?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('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 createTree(&$rootNode, &$groupsManager, &$usersManager ) {
	if($groupsIdList = &$groupsManager->getGroupsIdList()) {
		foreach($groupsIdList as $groupId){
			$group = &$groupsManager->getGroup($groupId, true);
			$groupName = $group->getGroupName();
			$treeNode = &new egTreeNode("trn_group_$groupId", $groupName, "interface/icons/icon_group.png", "interface/icons/icon_group.png", "0", "", "setGroup($groupId,\'$groupName\');");
			if($usersIdList = &$group->getUsersIdList()) {
				foreach($usersIdList as $userId){
					if($user = &$usersManager->getUser($userId)) {
						$userName = $user->getUserLogin();
						$treeSubNode = &new egTreeNode("trn_user_$userId", $userName, "interface/icons/icon_user.png", "interface/icons/icon_user.png", "0", "", "setUser($userId,\'$userName\');setGroup($groupId,\'$groupName\');");
						$treeNode->addChild($treeSubNode);
						unset($treeSubNode);
						unset($user);
					}
				}
			}
			$rootNode->addChild($treeNode);
		}
	}
}


$langId 						= $site->getPersistentVar("lid", $site->getDefaultLangId());
$parentControlName 	= $site->getSubmittedVar('parent_control_name');

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

$usersManager		= &$site->getUsersManager();
$groupsManager	= &$site->getGroupsManager();


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

$treeListWidth = 400;

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

$treeList = &new egTreeList("trl_1", "select page", 10, 10, $treeListWidth, 380, "choice_tree");
$treeNodeRoot = &new egTreeNode("trn_r", "groups", "interface/icons/page.png", "interface/icons/page.png", "0", "", "");
createTree($treeNodeRoot, $groupsManager, $usersManager);
$treeList->insertRootNode($treeNodeRoot);


if($mode == 'admin'){


	$groupNameStatic = &new egStatic("group_name_static", LANG_GROUP_MENU_SELECTED." : ", 10, 420, 150, 0, true);
	$groupNameValue  = &new egStatic("group_name_value", "", 160, 420, 350, 0, false);

	$userNameStatic = &new egStatic("user_name_static", LANG_USER_MENU_SELECTED." : ", 10, 440, 150, 0, true);
	$userNameValue  = &new egStatic("user_name_value", "", 160, 440, 350, 0, false);

	$main->addObject($groupNameStatic);
	$main->addObject($groupNameValue);
	$main->addObject($userNameStatic);
	$main->addObject($userNameValue);

	$linkAdd 				= &new egLink("lnk_add", LANG_GROUP_MENU_ADD, 10, 395, 250, 18, "launchGroupCreationWindow()", "interface/icons/icon_add_group.png");

	$linkAddUser 		= &new egLink("lnk_add_user", LANG_GROUP_MENU_ADD_USER, 160, 460, 250, 18, "launchAddGroupUserWindow()", "interface/icons/icon_add_user.png");
	$linkRemoveUser = &new egLink("lnk_remove_user", LANG_GROUP_MENU_REMOVE_USER, 160, 480, 250, 18, "launchRemoveGroupUserWindow()", "interface/icons/icon_remove_user.png");
	$linkRemove			= &new egLink("lnk_remove", LANG_GROUP_MENU_REMOVE, 160, 500, 250, 18, "launchGroupDeletionWindow()", "interface/icons/remove.png");
	$linkEdit 			= &new egLink("lnk_edit", LANG_GROUP_MENU_EDIT, 160, 520, 200, 18, "launchGroupEditionWindow()", "interface/icons/edit.png");

	$main->addObject($linkAdd);
	$main->addObject($linkRemove);
	$main->addObject($linkEdit);
	$main->addObject($linkAddUser);
	$main->addObject($linkRemoveUser);
}
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 groupid = 0;
var userid = 0;
function setGroup(id, name) {
	groupid = id;
	document.getElementById('group_name_value').innerHTML = name;
}
function setUser(id, name) {
	userid = id;
	document.getElementById('user_name_value').innerHTML = name;
}
function launchGroupEditionWindow() {
  openWindow('../index.php?display=edit_group&mode=ask_modif_group&group_id='+groupid,540, 600, 0, 0);
}
function launchGroupCreationWindow() {
  openWindow('../index.php?display=edit_group&mode=ask_modif_group&group_id=0',540, 600, 0, 0);
}
function launchAddGroupUserWindow() {
  openWindow('change_group_user.php?mode=ask_add_group_user&group_id='+groupid,300, 150, 0, 0);
}
function launchRemoveGroupUserWindow() {
  openWindow('change_group_user.php?mode=ask_delete_group_user&group_id='+groupid+'&user_id='+userid,300, 150, 0, 0);
}
function launchGroupDeletionWindow() {
	openWindow('../index.php?'+'display=edit_group&mode=ask_delete_group&group_id='+groupid,380, 280, 0, 0);
}
function sendResponse() {
	window.opener.document.getElementById('".$parentControlName."').value='../index.php?pid='+pid;
	window.close();
}
";
$scripts->addLine($line);

$manager->addScripts($scripts);

echo $manager->getHTML();

?>