Location: PHPKode > projects > astwebpanel > web/dao/ApplicationDao.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');
require_once('./dao/Dao.php');

/* Data Access Object for the dialplan.
*/
class ApplicationDao extends DBDao
{
	var $table = 'applications';
	var $data = array('id' => '', 'name' => '', 'description' => '', 'locked' => 'f');

	function get($name, &$values)
	{
	   	global $db;

        $query = "SELECT * FROM {$this->table}  WHERE name = ?";
 		$stmt = $db->prepare($query);	
		$querydata = array($name);

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

		if ( $result->numRows() == 1 )
		{
        	$row = $result->fetchRow();
	        foreach(array_keys($this->data) as $key) 
			{
				if ($key == 'locked')
					$values[$key] = ($row[$key] == 't') ? TRUE : FALSE;
				else
					$values[$key] = $row[$key];
    	    }
			$result->free();
			return TRUE;
		}
		$result->free();
		return FALSE;
	}

	function getAllNames(&$names)
	{
    	global $db;

		$names = $db->getCol("SELECT name FROM {$this->table}" ); 
	}

	function get_properties($name, &$values)
	{
	   	global $db;

        $query = "SELECT pname, pvalue FROM app_properties WHERE name = ?";
 		$stmt = $db->prepare($query);	
		$querydata = array($name);

   		$result = $db->execute($stmt, $querydata); 
		
		while( $row = $result->fetchRow())
		{
			$values[$row['pname']] = $row['pvalue'];
			$result->free();
			return TRUE;
		}
		$result->free();
		return FALSE;
	}
	
	function update_properties($name, &$values)
	{
	   	global $db;

		$props = array();
		$this->get_properties($name, $props);
			
		$query = "UPDATE app_properties  
		                SET  pvalue = ?
		                WHERE name = ? AND pname = ?"; 	
		$stmt = $db->prepare($query);	
		
		foreach($props as $pname => $pvalue)
		{
			if ($values[$pname]  != $pvalue)
			{
				$querydata = array( $values[$pname], $name, $pname );
				$db->execute($stmt, $querydata); 
			}
		}
	}

	function insert_properties($name, &$values)
	{
	   	global $db;

		$props = array();
		$this->get_properties($name, $props); // get prop's from app being cloned
			
		$query = "INSERT INTO app_properties  
		                (name, pname, pvalue)
						VALUES (?, ?, ?)";

		$stmt = $db->prepare($query);	
		
		foreach($props as $pname => $pvalue)
		{
			$querydata = array( $values['name'], $pname, $values[$pname] );
			$db->execute($stmt, $querydata); 			
		}
	}

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

   		foreach(array_keys($this->data) as $key) 
		{
			if (isset($values[$key]))
			{
				$this->data[$key] = $values[$key];
			}
	   	}

		$query = "INSERT INTO {$this->table}  
				  ( id, name, description, locked ) 
				  VALUES (?, ?, ?, ?)";

 		$stmt = $db->prepare($query);	
		$querydata = array($this->data['id'], $this->data['name'], $this->data['description'], 'f');

   		$result = $db->execute($stmt, $querydata); 
		return DB::isError ($result);
	}

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

   		foreach(array_keys($this->data) as $key) 
		{
			if (isset($values[$key]))
			{
				$this->data[$key] = $values[$key];
			}
	   	}
		$query = "UPDATE {$this->table}  
				  SET  description = ?
  				  WHERE name = ?"; 

 		$stmt = $db->prepare($query);	
		$querydata = array($this->data['description'], $this->data['name'] );

   		$result = $db->execute($stmt, $querydata); 
		return DB::isError ($result);
	}

	function delete($name)
	{
    	global $db;

		$query = "DELETE FROM {$this->table}  WHERE name = ? AND locked = 'f'"; 
 		$stmt = $db->prepare($query);	
		$querydata = array($name);

   		$result = $db->execute($stmt, $querydata); 
		return DB::isError ($result);
	}

	function getCount(&$pattern)
	{
		global $db;

		$sql = "SELECT count(*)";
		$from = "FROM {$this->table} ";
		$where = $this->buildWhere($pattern);

		$result = $db->query("$sql $from $where");
		$row = $result->fetchRow();
		return $row['count']; 
	}	

	function getMultiple( &$pattern, $offset, $limit, &$values)
	{
		global $db;

		$sql = "SELECT *";
		$from = "FROM {$this->table} ";
		$where = $this->buildWhere($pattern);

		$query = "$sql $from $where OFFSET $offset LIMIT $limit";
		$result = $db->query($query);
		while ($row = $result->fetchRow()) 
		{ 
			$values[] = $row;
		}
		$result->free();
	}
}


?>
Return current item: astwebpanel