Location: PHPKode > projects > PHP VoiceViewer > library/server.class.php
<?php
/**
 * PHP VoiceViewer - Server Class
 *
 * Server Root Entity
 *
 * Project   : PHP VoiceViewer
 * Copyright : Christopher Cooper © 2010
 * License   : FreeBSD
 * Author    : Christopher Cooper (PaNtHaLooN), hide@address.com
 * Date      : March 2010
 * Version   : 1.0.0
 *
 * Copyright 2010 Christopher Cooper. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY CHRISTOPHER COOPER ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CHRISTOPHER COOPER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Christopher Cooper.
 */
 
// Define acronyms
defined('DS') || define('DS', DIRECTORY_SEPARATOR);

// Main include
@require_once(dirname(__FILE__) . DS . '..' . DS . 'main.inc.php');

final class vvServer extends avvEntity implements ivvServer
{

	const ELEMENT_CHANNELS	= 'channels';

	private static $_proplist = array(	'id'					=> self::PROP_INIT_NOCDATA,
										'host'					=> self::PROP_INIT_NOCDATA,
										'hostname'				=> self::PROP_NOCDATA,
										'port'					=> self::PROP_INIT_NOCDATA,
										'name'					=> self::PROP_VALUE,
										'description'			=> self::PROP_VALUE,
										'patternuser'			=> self::PROP_VALUE,
										'patternchannel'		=> self::PROP_VALUE,
										self::ELEMENT_CHANNELS	=> self::PROP_IGNORE
										);
	
	private $_root = null;
	
	public function __construct( &$data )
	{
		// Call parent constructor with properties
		parent::__construct(self::$_proplist, $data);

		if ( $root =& vv::transformToDOMElement($data) !== null )
		{
			$xpath = new DOMXPath($root->ownerDocument);
			$query = './' . self::ELEMENT_CHANNELS . '/' . ivvChannel::ELEMENT_NAME;
			foreach( $xpath->query($query, $root) as $channel )
			{
				$this->_root =& new vvChannel($channel);
				break;
			}
		}
	}
	
	public function getElementName()
	{
		return self::ELEMENT_NAME;
	}
	
	public function &createDOMElement( DOMDocument &$doc )
	{
		$server =& parent::createDOMElement($doc);

		if ( $this->_root !== null )
		{
			$channels = $server->appendChild($doc->createElement(self::ELEMENT_CHANNELS));
			$channels->appendChild($this->_root->createDOMElement($doc));
		}
		return $server;
	}

	public function &getRoot()
	{
		return $this->_root;
	}
	
	public function setRoot( ivvChannel &$channel )
	{
		$this->_root =& $channel;
	}
	
	public function channelExists( $id )
	{
		if ( $this->_root !== null )
		{
			return $this->_root->channelExists($id, false);
		}
		return false;
	}

	public function userExists( $id )
	{
		if ( $this->_root !== null )
		{
			return $this->_root->userExists($id, false);
		}
		return false;
	}

	public function &getChannel( $id )
	{
		$result = null;
		if ( $this->_root !== null )
		{
			$result =& $this->_root->getChannel($id);
		}
		return $result;
	}

	public function &getUser( $id )
	{
		$result = null;
		if ( $this->_root !== null )
		{
			$result =& $this->_root->getUser($id);
		}
		return $result;
	}

	public function hasChannels()
	{
		if ( $this->_root !== null )
		{
			$result = $this->_root->hasChannels();
		}
		return false;
	}

	public function hasUsers()
	{
		if ( $this->_root !== null )
		{
			$result = $this->_root->hasUsers();
		}
		return false;
	}

	public function &removeChannel( $id )
	{
		$channel = null;
		if ( $this->_root !== null )
		{
			if ( $id !== $this->_root->getID() )
			{
				$channel =& $this->getChannel($id);
				if ( $channel === null )
				{
					throw new vvException('CHANNEL_REMOVE_UNKNOWN_CHANNEL', 0, $id);
				}
				if ( $parent =& $channel->getParent() === null )
				{
					throw new vvException('CHANNEL_REMOVE_UNLINKED_CHANNEL', 0, $id);
				}
				return $parent->removeChannel($id);
			}
			$this->_root = null;
		}
		return $channel;
	}

	public function &removeUser( $id )
	{
		$user = null;
		if ( $this->_root !== null )
		{
			$user =& $this->getUser($id);
			if ( $user === null )
			{
				throw new vvException('CHANNEL_REMOVE_UNKNOWN_USER', 0, $id);
			}
			if ( ($parent = $user->getParent()) === null )
			{
				throw new vvException('CHANNEL_REMOVE_UNLINKED_USER', 0, $id);
			}
			return $parent->removeUser($id);
		}
		return $user;
	}

}
 
?>
Return current item: PHP VoiceViewer