Location: PHPKode > projects > Network Configuration Store > NCS-3.0.2/wwwroot/inc/CSAlert.class.php
<?php

// Flag Controls
// This is not specific to Alert but here will do for now
//
define('CSF_ON', 1);
define('CSF_OFF', 0);

//
// Alert User Flags (alert_users.flags);
//

define('CSA_CHANGED',	0x0001);
define('CSA_ERROR',	0x0002);

class CSAlertTarget
{
	private $rec;

	// Constructor
	//	Load the alert target record identified by 'id'
	//
	public function __construct($id)
	{
		global $CS;

		if (empty($id))
			throw new Exception('CSAlertTarget: target_id not specified');

		$this->rec = $CS->getDb()->GetOneRecord('SELECT * FROM `targets` WHERE `target_id` = ?', $id);
		if (!$this->rec)
			throw new Exception('Cannot find alert target');
	}

	public function __destruct() { }

	// Magic Get
	//	Return requested field from the database record, or null
	//
	public function __get($field)
	{
		if (array_key_exists($field, $this->rec))
			return $this->rec[$field];
		return null;
	}

	// Magic Set
	//	Change the specified field to the specified value, keeping the database
	//	record in sync if possible.
	//
	public function __set($field, $value)
	{
		// If the key does not exist, it's not in the database!
		if (array_key_exists($field, $this->rec))
		{
			global $CS;

			// Try and keep the database record in sync
			//
			if ($CS->UpdateRecord('UPDATE `targets` SET `'.$field.'` = ? WHERE `target_id` = ?',
				array($value, $this->rec['target_id'])) == 1)
			{
				$this->rec[$field] = $value;
				return true;
			}

			// If this actually failed, there is something wrong, so inform
			// the caller that they cannot pass go!
			//
			// 3.0(1) - This can happen if the original and new values are identical
			//throw new Exception('CSAlertTarget::__set() failed to update "'.$field.'" field');
		}
		return false;
	}

	// Magic ToString
	//	Return a table row containing this element
	//
	public function __toString()
	{
		return '<tr><td><a href="AlertTargetAdmin.php?target_id='.$this->target_id.'">'.$this->email_address.
			'</a> (<a href="AlertTargetAdmin.php?alert_id='.$this->alert_id.'&amp;target_id='.$this->target_id.'&amp;delete">delete</a>)</td>'.
			'<td>'.$this->getEnabledFlags().'</td></tr>';
	}

	// setFlag()
	//	Helper method to manipulate bit flags field
	//	This triggers an update to the 'alert_users' table !
	//
	public function setFlag($flag, $action=CSF_ON)
	{
		if ($action == CSF_ON)
			$this->flags = ($this->flags | $flag);
		else
			$this->flags = ($this->flags & ~$flag);
	}

	// getEnabledFlags()
	//	Returns a pretty string showing what flags this target has enabled
	//
	public function getEnabledFlags()
	{
		$s = '';

		if ($this->flags & CSA_CHANGED)
		{
			$s = 'config changes';
		}

		if ($this->flags & CSA_ERROR)
		{
			if (strlen($s) > 0)
				$s .= ' and error msgs';
			else
				$s = 'error msgs';
		}

		// If no flags are enabled, tell the user
		if (strlen($s) == 0)
			$s = 'none';

		return $s;
	}
}

class CSAlert
{
	private $rec;

	private $targets;

	public function __construct($id)
	{
		global $CS;

		if (empty($id))
			throw new Exception('CSAlert: alert_id not specified');

		//
		// Load our alert record, or bail out
		//
		$this->rec = $CS->getDb()->GetOneRecord('SELECT * FROM `alerts` WHERE `alert_id` = ?', $id);
		if (!$this->rec)
			throw new Exception('Cannot find alert group');

		// Initialise our target data as NULL for now, so we don't have to bother going
		// to the database unless we really need to.
		$this->targets = null;

		// Initialise target count by calling the database, though.
		$this->rec['targetCount'] = $CS->getDb()->GetOneField('SELECT COUNT(*) AS "tc" FROM `targets` '.
							'WHERE `alert_id` = ?', 'tc', $id);

		// Record how many times this group is used by a device
		$this->rec['refCount'] = $CS->getDb()->GetOneField('SELECT COUNT(*) AS "rc" FROM `devices` '.
							'WHERE `alert_id` = ?', 'rc', $id);
	}

	public function __destruct() { }

	public function __get($field)
	{
		if (array_key_exists($field, $this->rec))
			return $this->rec[$field];
		return null;
	}

	public function __set($field, $value)
	{
		if (array_key_exists($field, $this->rec))
		{
			$this->rec[$field] = $value;
			return true;
		}
		return false;
	}

	private function LoadTargets()
	{
		global $CS;

		$this->targets = array();

		$rec = $CS->getDb()->GetRecords('SELECT `target_id` FROM `targets` WHERE `alert_id` = ?', $this->alert_id);
		if (is_array($rec))
		{
			foreach($rec as $record)
				$this->targets[] = new CSAlertTarget($record['target_id']);
		}

		// Sanity check:
		// We load our target count when we are constructed, so if this value does not
		// match how many targets we actually loaded, something must be wrong!
		//
		if ($this->targetCount != count($this->targets))
			throw new Exception('CSAlert::LoadTargets() failed to load all targets');
	}

	public function &GetTargetArray()
	{
		if (!is_array($this->targets))
			$this->LoadTargets();

		return $this->targets;
	}

	public static function GetAlertGroupList()
	{
		global $CS;

		$rec = $CS->getDb()->GetRecords('SELECT `alert_id`,`name`,`enabled` FROM `alerts` ORDER BY `name` ASC');
		if (!is_array($rec))
			throw new Exception('CSAlert::GetAlertGroupList() encountered non-array result');

		return array_merge(array( null ), $rec); // Insert our first entry as 'None' for the drop-down menus
	}

	public static function GetAlertGroupArray()
	{
		global $CS;

		$alertArray = CSAlert::GetAlertGroupList();
		$alerts = array();

		//
		// Build an array of CSAlert objects
		//

		foreach($alertArray as $alert)
		{
			if (is_null($alert)) // Skip 'None' entry
				continue;

			// query the database for how many targets are associated with this record,
			// and how many devices are associated with this record.
			//

			$alerts[] = array_merge( $alert, array(
					'targ_cnt' => $CS->getDb()->GetOneField('SELECT COUNT(*) AS "tc" FROM `targets` WHERE `alert_id` = ?',
						'tc', $alert['alert_id']),
					'ref_cnt' => $CS->getDb()->GetOneField('SELECT COUNT(*) AS "rc" FROM `devices` WHERE `alert_id` = ?',
						'rc', $alert['alert_id'])
				));
		}

		return $alerts;
	}
}

?>
Return current item: Network Configuration Store