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

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

$createForm = null; // If this becomes a CSForm later on, we have a create form to show!

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

		if ($_POST['action'] == 'create')
		{
			if (!CS::Check($_POST['name'], CSA_EMPTY))
				throw new CSArgumentMissingException('Alert Group Name');

			$CS->getDb()->InsertRecord('INSERT INTO `alerts` (`name`,`enabled`) VALUES (?, 1);', $_POST['name']);

			header('Location: Alerts.php');
			exit();
		}
		if ($_POST['action'] == 'modify')
		{
			CS::Check($_POST['alert_id'], CSA_EMPTY|CSA_THROW, 'AlertAdmin: modify action with no alert_id');
			CS::Check($_POST['enabled'], CSA_ISSET|CSA_THROW, 'AlertAdmin: modify action with no enabled flag');

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

			$CS->getDb()->UpdateRecord('UPDATE `alerts` SET `name` = ?, `enabled` = ? '.
				'WHERE `alert_id` = ?', array($_POST['name'], $_POST['enabled'], $_POST['alert_id']));

			header('Location: Alerts.php');
			exit();
		}
		if ($_POST['action'] == 'delete')
		{
			CS::Check($_POST['alert_id'], CSA_EMPTY|CSA_THROW, 'AlertAdmin: delete action with no alert_id');
			CS::Check($_POST['alert_id'], CSA_ISSET|CSA_THROW, 'AlertAdmin: delete action with no reassign_alert_id');

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

			$CS->getDb()->UpdateRecord('UPDATE `devices` SET `alert_id` = ? WHERE `alert_id` = ?',
				array($_POST['reassign_alert_id'], $_POST['alert_id']));
			$CS->getDb()->UpdateRecord('DELETE FROM `targets` WHERE `alert_id` = ?', $_POST['alert_id']);
			$CS->getDb()->UpdateRecord('DELETE FROM `alerts` WHERE `alert_id` = ?', $_POST['alert_id']);

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

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

	if (!empty($_GET['alert_id']))
	{
		$thisAlert = new CSAlert($_GET['alert_id']);

		//
		// Build a modify form
		//
		$modifyForm = new CSForm('AlertAdmin.php?submit');
		$modifyForm->AddHidden('action', 'modify');
		$modifyForm->AddHidden('alert_id', $thisAlert->alert_id);

		$modifyForm->AddControl('<strong>Alert Name</strong><br />Enter the alert group name',
			new CSFormControl(CSF_TEXTBOX, array('name' => 'name', 'value' => $thisAlert->name, 'size' => '40')));
		$s = $modifyForm->AddControl('<strong>Status</strong><br />Select the status of this alert group',
			new CSFormControl(CSF_RADIO, array('name' => 'enabled', 'value' => 1, 'id' => 'enabled_on',
								'checked' => $thisAlert->enabled == 1)));
		$s->AddChild(new CSFormControl(CSF_LABEL, array('for' => 'enabled_on', 'text' => 'Enabled')));
		$s->AddChild(new CSFormControl(CSF_RADIO, array('name' => 'enabled', 'value' => 0, 'id' => 'enabled_off',
								'checked' => $thisAlert->enabled == 0)));
		$s->AddChild(new CSFormControl(CSF_LABEL, array('for' => 'enabled_off', 'text' => 'Disabled')));

		$modifyForm->AddButton(new CSFormControl(CSF_SUBMIT, array('value' => 'Submit')));
		$modifyForm->AddButton(new CSFormControl(CSF_RESET, array('value' => 'Reset')));
		$modifyForm->AddButton(new CSFormControl(CSF_BUTTON, array('value' => 'Cancel', 'onclick' => 'history.back(1);')));


		//
		// Build a delete form
		//
		$deleteForm = new CSForm('AlertAdmin.php?submit');
		$deleteForm->AddHidden('action', 'delete');
		$deleteForm->AddHidden('alert_id', $thisAlert->alert_id);

		$s = $deleteForm->AddControl('<strong>Reassign References</strong><br />Select another alert group to re-assign devices to.',
			new CSFormControl(CSF_SELECT, array('name' => 'reassign_alert_id')));
		$s->AddChild(new CSFormControl(CSF_OPTION, array('value' => '0', 'text' => '[Do not reassign to another alert group]')));
		foreach(CSAlert::GetAlertGroupList() as $alert)
		{
			// Allow targets to be moved to another alert group
			if (is_array($alert) && $thisAlert->alert_id != $alert['alert_id'])			
				$s->AddChild(new CSFormControl(CSF_OPTION, array('value' => $alert['alert_id'], 'text' => $alert['name'])));
		}

		$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')));
		$deleteForm->AddButton(new CSFormControl(CSF_BUTTON, array('value' => 'Cancel', 'onclick' => 'history.back(1);')));
	}
	else
	{
		$createForm = new CSForm('AlertAdmin.php?submit');
		$createForm->AddHidden('action', 'create');

		$createForm->AddControl('<strong>Alert Name</strong><br />Specify the name of this group.',
			new CSFormControl(CSF_TEXTBOX, array('name' => 'name', 'size' => '40')));

		$createForm->AddButton(new CSFormControl(CSF_SUBMIT, array('value' => 'Submit')));
		$createForm->AddButton(new CSFormControl(CSF_BUTTON, array('value' => 'Cancel', 'onclick' => 'history.back(1);')));
	}
}
catch (Exception $e) {
	CS::Abort($e);
}

CS::PrintHtmlHeader('Alert Admin - Settings');

?>

<body id="Main">

	<h1>Alert Admin</h1>

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

	<h2>Modify</h2>
	<p>Remember to click Submit to commit any changes 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