Location: PHPKode > projects > Network Configuration Store > NCS-3.0.2/wwwroot/Settings/GroupAdmin.php
<?php

require_once('./CS_Includes.inc.php');

$createForm = null; // Checked later

try {
	if (isset($_GET['submit']))
	{
		CS::Check($_POST['action'], CSA_EMPTY|CSA_THROW, 'GroupAdmin: submit action not specified');

		if ($_POST['action'] == 'create')
		{
			CS::Check($_POST['parent_id'], CSA_ISSET|CSA_THROW, 'GroupAdmin: create action with no parent_id');

			if (!CS::Check($_POST['name'], CSA_EMPTY))
				throw new CSArgumentMissingException('Device Group Name');

			$CS->getDb()->InsertRecord('INSERT INTO `groups` (`name`, `parent_id`) VALUES (?, ?)',
				array($_POST['name'], $_POST['parent_id']), TRUE);

			header('Location: Groups.php');
			exit();
		}
		if ($_POST['action'] == 'modify')
		{
			CS::Check($_POST['group_id'], CSA_EMPTY|CSA_THROW, 'GroupAdmin: modify action with no group_id');
			CS::Check($_POST['parent_id'], CSA_ISSET|CSA_THROW, 'GroupAdmin: modify action with no parent_id');

			if (!CS::Check($_POST['name'], CSA_EMPTY))
				throw new CSArgumentMissingException('Device Group Name');

			$CS->getDb()->UpdateRecord('UPDATE `groups` SET `name` = ?, `parent_id` = ? WHERE `group_id` = ?',
				array($_POST['name'], $_POST['parent_id'], $_POST['group_id']));
	
			header('Location: Groups.php');
			exit();
		}
		if ($_POST['action'] == 'delete')
		{
			CS::Check($_POST['group_id'], CSA_EMPTY|CSA_THROW, 'GroupAdmin: modify action with no group_id');
			CS::Check($_POST['child_parent_id'], CSA_ISSET|CSA_THROW, 'GroupAdmin: modify action with no child_parent_id');

			if (!CS::Check($_POST['confirm'], CSA_EMPTY))
				throw new CSActionUnconfirmedException();

			$CS->getDb()->UpdateRecord('UPDATE `devices` SET `group_id` = ? WHERE `group_id` = ?',
				array($_POST['child_parent_id'], $_POST['group_id']));
			$CS->getDb()->UpdateRecord('UPDATE `groups` SET `parent_id` = ? WHERE `parent_id` = ?',
					array($_POST['child_parent_id'], $_POST['group_id']));
			$CS->getDb()->UpdateRecord('DELETE FROM `groups` WHERE `group_id` = ?', $_POST['group_id']);

			header('Location: Groups.php');
			exit();
		}

		throw new Exception('GroupAdmin: submit action not supported');
	}

	//
	// Anything we do from here needs a group menu, so load it now.
	$groupRootNode = CSDevice::GetGroupById(0, FALSE);
	$groupMenuList = CSDevice::GetGroupMenuList($groupRootNode);

	if (!empty($_GET['group_id']))
	{
		$thisGroup = $CS->getDb()->GetOneRecord('SELECT * FROM `groups` WHERE `group_id` = ?', $_GET['group_id']);
		if (!count($thisGroup))
			throw new Exception('Group does not exist');

		//
		// Build the MODIFY form.......
		//
		$modifyForm = new CSForm('GroupAdmin.php?submit');
		$modifyForm->AddHidden('action', 'modify');
		$modifyForm->AddHidden('group_id', $thisGroup['group_id']);

		$modifyForm->AddControl('<strong>Name</strong><br />Enter the name of this Group',
					new CSFormControl(CSF_TEXTBOX,	array('name' => 'name', 'value' => $thisGroup['name'], 'size' => '40')));

		$s = $modifyForm->AddControl('<strong>Parent</strong><br />Select where you would like this group to appear',
					new CSFormControl(CSF_SELECT,	array('name' => 'parent_id')));
		foreach($groupMenuList as $groupId => $groupName)
		{
			// Don't allow the user to select the group to be parent to itself!
			if ($groupId != $thisGroup['group_id'])
			{
				$s->AddChild(new CSFormControl(CSF_OPTION, array(
						'value' => $groupId,
						'text' => $groupName,
						'selected' => ($groupId == $thisGroup['parent_id'])
					)));
			}
		}

		$modifyForm->AddButton(new CSFormControl(CSF_SUBMIT, array('value' => 'Submit')));
		$modifyForm->AddButton(new CSFormControl(CSF_RESET, array('value' => 'Reset')));

		//
		// Build the DELETE form....
		//
		$deleteForm = new CSForm('GroupAdmin.php?submit');
		$deleteForm->AddHidden('action', 'delete');
		$deleteForm->AddHidden('group_id', $thisGroup['group_id']);

		$s = $deleteForm->AddControl('<strong>Relocate Children</strong><br />Select where any child nodes should be moved to',
				new CSFormControl(CSF_SELECT,	array('name' => 'child_parent_id' )));
		foreach($groupMenuList as $groupId => $groupName)
		{
			// Don't let the user move nodes to this group as it's going to be deleted
			if ($groupId != $thisGroup['group_id'])
			{
				$s->AddChild(new CSFormControl(CSF_OPTION, array('value' => $groupId, 'text' => $groupName)));
			}
		}

		$s = $deleteForm->AddControl('<strong>Confirm Action</strong><br />Are you sure?',
			new CSFormControl(CSF_CHECK, array('name' => 'confirm', 'value' => 'yes', 'id' => 'confirm_yes')));
		$s->AddChild(new CSFormControl(CSF_LABEL, array('for' => 'confirm_yes', 'text' => 'Yes')));

		$deleteForm->AddButton(new CSFormControl(CSF_SUBMIT, array('value' => 'Delete')));
	}
	else
	{
		$createForm = new CSForm('GroupAdmin.php?submit');
		$createForm->AddHidden('action', 'create');

		$createForm->AddControl('<strong>Group Name</strong><br />Name of the group you wish to create',
				new CSFormControl(CSF_TEXTBOX, array('name' => 'name', 'size' => '40')));
		$s = $createForm->AddControl('<strong>Parent</strong><br />Select where you want the group to be',
				new CSFormControl(CSF_SELECT, array('name' => 'parent_id')));
		foreach($groupMenuList as $groupId => $groupName)
		{
			$s->AddChild(new CSFormControl(CSF_OPTION, array('value' => $groupId, 'text' => $groupName, 'selected' => ($groupId == 0))));
		}

		$createForm->AddButton(new CSFormControl(CSF_SUBMIT, array('value' => 'Create')));
	}
}
catch (Exception $e) {
	CS::Abort($e);
}

CS::PrintHtmlHeader('Group Admin');

?>

<body id="Main">
<h1>Group Admin</h1>

<?php if (is_null($createForm)): ?>

	<h2>Modify</h2>
	<p>Remember to click Submit to commit any change to the database.</p>
	<?php $modifyForm->InsertHtml(); ?>

	<h2>Delete</h2>
	<?php $deleteForm->InsertHtml(); ?>

<?php else: ?>

	<h2>Create</h2>
	<?php $createForm->InsertHtml(); ?>

<?php endif; ?>

</body>
</html>
Return current item: Network Configuration Store