<?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/groups/Group.class.php');
include_once('classes/core/users/User.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 identified to upload some file
$user = $site->getCurrentUser();
if(!$user || $user->getUserId() == USER_ID_ANONYMOUS) {
	die("access denied");
	exit();
}

$mode 		= $site->getSubmittedVar('mode');
$userId 	= $site->getSubmittedVar('user_id', 0);
$groupId 	= $site->getSubmittedVar('group_id', 0);

switch($mode) {
	case ASK_ADD_GROUP_USER :
		$manager = &egManager::getInstance();
		$main = &new egContainer("main_ctn", 0, 0, 0, 0);

		$txtValue = &new egStatic("txt_value_1", LANG_GROUP_USER_ADD_MESSAGE, 10, 15, 250, 0);
		$nameValue = &new egStatic("user_name_static", LANG_USER_LOGIN_STATIC.LANG_COLON, 10, 50, 80, 0, true);
		$nameItem = &new egEdit("user_name_item", '', 70, 50, 130, 18, 'name');
		$chooseButton = &new egPressbutton("btn_choose", LANG_BROWSE_BUTTON, 210, 48, 80, null, "chooseUser();");

		$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", ADD_GROUP_USER, 0, 0, 0, 0, "mode");
		$objUserId = &new egEdit("hidden_control_user_id", '', 0, 0, 0, 0, "user_id");
		$objGroupId = &new egEdit("hidden_control_group_id", $groupId, 0, 0, 0, 0, "group_id");

		$hiddenValuesContainer->addObject($objMode);
		$hiddenValuesContainer->addObject($objUserId);
		$hiddenValuesContainer->addObject($objGroupId);

		$manager->addContainer($hiddenValuesContainer);

		$main->addObject($chooseButton);
		$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();
}
function chooseUser() {
  openWindow('select_user.php?'+'parent_control_name=hidden_control_user_id_value&parent_control_name2=user_name_item_value',380, 350, 0, 0);
}
";
		$scripts->addLine($line);
		$manager->addScripts($scripts);

		$manager->addContainer($main);
		print($manager->getHTML());
		break;
	case ASK_DELETE_GROUP_USER :
		$manager = &egManager::getInstance();
		$main = &new egContainer("main_ctn", 0, 0, 0, 0);

		$usersManager = &$site->getUsersManager();
		$user = &$usersManager->getUser($userId);
		$groupsManager = &$site->getGroupsManager();
		$group = &$groupsManager->getGroup($groupId, true);

		$txtValue = &new egStatic("txt_value_1", LANG_GROUP_USER_DELETION_MESSAGE, 10, 15, 250, 0);
		$nameValue = &new egStatic("user_name_static", $user->getUserLogin(), 10, 50, 80, 0, true);
		$confirmValue = &new egStatic("confirm_static", LANG_CONFIRM, 10, 80, 200, 0);

		$okButton = &new egPressbutton("btn_ok", LANG_OK_BUTTON, 130, 110, 80, null, "clickOKButton();");
		$cancelButton = &new egPressbutton("btn_cancel", LANG_CANCEL_BUTTON, 215, 110, 80, null, "clickCancelButton();");

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

		$objMode = &new egEdit("hidden_control_mode", DELETE_GROUP_USER, 0, 0, 0, 0, "mode");
		$objUserId = &new egEdit("hidden_control_user_id", $userId, 0, 0, 0, 0, "user_id");
		$objGroupId = &new egEdit("hidden_control_group_id", $groupId, 0, 0, 0, 0, "group_id");

		$hiddenValuesContainer->addObject($objMode);
		$hiddenValuesContainer->addObject($objUserId);
		$hiddenValuesContainer->addObject($objGroupId);

		$manager->addContainer($hiddenValuesContainer);

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

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

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

		$manager->addContainer($main);
		print($manager->getHTML());
		break;
	case ADD_GROUP_USER :
		$manager = &egManager::getInstance();
		$main = &new egContainer("main_ctn", 0, 0, 0, 0);
		$manager->addContainer($main);

		if($userId > 0 && $groupId > 0) {
			$groupsManager = &$site->getGroupsManager();
			$group = &$groupsManager->getGroup($groupId, true);
			$group->addUserMembership($userId);
			$groupsManager->storeGroup($groupId);
		}

		$scripts = new egScript();
		$line = "window.opener.location.reload();window.close();";

		$scripts->addLine($line);

		$manager->addScripts($scripts);
		print($manager->getHTML());
		break;
	case DELETE_GROUP_USER :
		$manager = &egManager::getInstance();
		$main = &new egContainer("main_ctn", 0, 0, 0, 0);
		$manager->addContainer($main);

		if($userId > 0 && $groupId > 0) {
			$groupsManager = &$site->getGroupsManager();
			$group = &$groupsManager->getGroup($groupId, true);
			$group->delUserMembership($userId);
			$groupsManager->storeGroup($groupId);
		}

		$scripts = new egScript();
		$line = "window.opener.location.reload();window.close();";

		$scripts->addLine($line);

		$manager->addScripts($scripts);
		print($manager->getHTML());
		break;
	default:
		break;
}
?>