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

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

function BuildAdminForm(&$form, &$alert, $target=null)
{
	$form->AddControl('<strong>Alert Group Name</strong><br />The group this target belongs to.',
		new CSFormControl(CSF_TEXT, array('text' => $alert->name)));

	$form->AddControl('<strong>E-Mail Address</strong><br />Enter the target e-mail address to send alerts to.',
		new CSFormControl(CSF_TEXTBOX, array('name' => 'email_address', 'size' => '50',
			'value' => ($target) ? $target->email_address : NULL)));

	$s = $form->AddControl('<strong>Alert Flags</strong><br />Specify what type of alerts to send.',
		new CSFormControl(CSF_CHECK, array('name' => 'f_changed', 'value' => 'yes', 'id' => 'f_changed',
			'checked' => ($target && ($target->flags & CSA_CHANGED)))));
	$s->AddChild(new CSFormControl(CSF_LABEL, array('for' => 'f_changed', 'text' => 'Config Changes')));

	$s->AddChild(new CSFormControl(CSF_CHECK, array('name' => 'f_error', 'value' => 'yes', 'id' => 'f_error',
			'checked' => ($target && ($target->flags & CSA_ERROR)))));
	$s->AddChild(new CSFormControl(CSF_LABEL, array('for' => 'f_error', 'text' => 'Error Messages')));
}

$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, 'AlertTargetAdmin: submit action not specified');

		if ($_POST['action'] == 'create')
		{
			CS::Check($_POST['alert_id'], CSA_EMPTY|CSA_THROW, 'AlertTargetAdmin: create action with no alert_id');

			if (!CS::Check($_POST['email_address'], CSA_EMAIL))
				throw new CSEmailAddressException();

			// Calculate our flag bits to set
			$f = 0;
			if (isset($_POST['f_changed']))
				$f = ($f | CSA_CHANGED);
			if (isset($_POST['f_error']))
				$f = ($f | CSA_ERROR);

			$CS->getDb()->InsertRecord('INSERT INTO `targets` (`alert_id`, `email_address`, `flags`) VALUES (?, ?, ?)',
				array($_POST['alert_id'], $_POST['email_address'], $f));

			header('Location: AlertTargets.php?alert_id='.$_POST['alert_id'].'&updated');
			exit();
		}

		if ($_POST['action'] == 'modify')
		{
			CS::Check($_POST['alert_id'], CSA_EMPTY|CSA_THROW, 'AlertTargetAdmin: modify action with no alert_id');
			CS::Check($_POST['target_id'], CSA_EMPTY|CSA_THROW, 'AlertTargetAdmin: modify action with no target_id');

			if (!CS::Check($_POST['email_address'], CSA_EMAIL))
				throw new CSEmailAddressException();

			// Calculate our flag bits to set
			$f = 0;
			if (isset($_POST['f_changed']))
				$f = ($f | CSA_CHANGED);
			if (isset($_POST['f_error']))
				$f = ($f | CSA_ERROR);

			$CS->getDb()->UpdateRecord('UPDATE `targets` SET `email_address` = ?, `flags` = ? WHERE `target_id` = ?',
				array($_POST['email_address'], $f, $_POST['target_id']));

			header('Location: AlertTargets.php?alert_id='.$_POST['alert_id'].'&updated');
			exit();
		}

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

	if (!empty($_GET['target_id']))
	{
		// target_id set, this is a change or a delete.
		if (isset($_GET['delete']))
		{
			CS::Check($_GET['alert_id'], CSA_EMPTY|CSA_THROW, 'AlertTargetAdmin: delete action with no alert_id'); // We require this to link back to it

			$CS->getDb()->UpdateRecord('DELETE FROM `targets` WHERE `target_id` = ?', $_GET['target_id']);
			header('Location: AlertTargets.php?alert_id='.$_GET['alert_id'].'&updated');
			exit();
		}

		// This is a modify...

		$thisAlertTarget = new CSAlertTarget($_GET['target_id']);
		$thisAlert = new CSAlert($thisAlertTarget->alert_id);

		$modifyForm = new CSForm('AlertTargetAdmin.php?submit');

		$modifyForm->AddHidden('action', 'modify');
		$modifyForm->AddHidden('alert_id', $thisAlert->alert_id);
		$modifyForm->AddHidden('target_id', $thisAlertTarget->target_id);

		BuildAdminForm($modifyForm, $thisAlert, $thisAlertTarget);

		$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);')));
	}
	else if (!empty($_GET['alert_id']))
	{
		// this is a create, load our alert
		$thisAlert = new CSAlert($_GET['alert_id']);

		// Build the create form...
		//
		$createForm = new CSForm('AlertTargetAdmin.php?alert_id='.$thisAlert->alert_id.'&submit');

		$createForm->AddHidden('action', 'create');
		$createForm->AddHidden('alert_id', $thisAlert->alert_id);

		BuildAdminForm($createForm, $thisAlert);

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

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

?>

<body id="Main">

	<h1>Alert Target Admin</h1>

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

	<h2>Modify Target</h2>
	<p>Click submit to commit any changes to the database.</p>
	<?php $modifyForm->InsertHtml(); ?>

<?php else: ?>

	<h2>Create Target</h2>
	<p>Please enter the target e-mail address and what alert types to send.</p>
	<?php $createForm->InsertHtml(); ?>

<?php endif; ?>

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