Location: PHPKode > projects > astwebpanel > web/dao/AlertsDao.php
<?php
/**
 * Copyright (C) 2004 - 2006, John Tarlton.
 *
 * This file is part of AstWebPanel - A web management user interface for Asterisk.
 *
 * AstWebPanel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation. See the LICENSE file distributed
 * with AstWebPanel.
 *
 * Database access object.
 */

require_once('./php/dbase.php');

/* Database access object for alert messages.
*/
class AlertsDao
{
	var $data = array('userid' => '', 'email' => '', 'enable' => '');

	function get($username, &$values)
	{
    	global $db;
        $query = "SELECT * FROM alerts WHERE userid = (SELECT userid FROM users WHERE username = ?)";
   		$stmt = $db->prepare($query);	
		$querydata = array($username);

   		$result = $db->execute($stmt, $querydata); 
		if ( $result->numRows() == 1 )
		{
        	$row = $result->fetchRow();
	        foreach(array_keys($this->data) as $key) 
			{
				$this->data[$key] = $row[$key];

 				if ($key == 'enable')
					$values[$key] = ($this->data[$key] == 't') ? TRUE : FALSE;
				else
					$values[$key] = $this->data[$key];
    	    }
			$result->free();
			return TRUE;
		}
		$result->free();
		return FALSE;
	}

	function insert($username, &$values)
	{
    	global $db;

	    foreach(array_keys($this->data) as $key) 
		{
			if (isset($values[$key]))
			{
				if ($key == 'enable')
					$this->data[$key] = ($values[$key] == TRUE) ? 't' : 'f';
				else
					$this->data[$key] = $values[$key];
			}
	   	}

		$query = "INSERT INTO alerts (userid, email, enable) 
				  VALUES ((SELECT userid FROM users WHERE username = ?), ?, ?)";

   		$stmt = $db->prepare($query);	
		$querydata = array($username, $this->data['email'], $this->data['enable']);

   		$result = $db->execute($stmt, $querydata); 

		return DB::isError ($result);
	}

	function update($username, &$values)
	{
    	global $db;

	    foreach(array_keys($this->data) as $key) 
		{
			if (isset($values[$key]))
			{
				if ($key == 'enable')
					$this->data[$key] = ($values[$key] == TRUE) ? 't' : 'f';
				else
					$this->data[$key] = $values[$key];
			}
	   	}

		$query = "UPDATE alerts 
				  SET email = ?, enable = ? 
				  WHERE userid = (SELECT userid FROM users WHERE username = ?)";

   		$stmt = $db->prepare($query);	
		$querydata = array($this->data['email'], $this->data['enable'], $username);

   		$result = $db->execute($stmt, $querydata); 

		return DB::isError ($result);
	}
}


?>
Return current item: astwebpanel