CommunicationManager

<?php require_once "autoload.php";

class CommunicationManager extends Bean
{
	private static $instance;
	public static function getInstance()
	{
		if (!isset(self::$instance))
		{
			$c = __CLASS__;
			self::$instance = new $c;
		}
		return self::$instance;
	}

	protected $host;
	protected $deviceId;
	protected $pubDeviceId;

	function getHost()
	{
	 	return $this->host;
	}

	function getDeviceId()
	{
		return $this->deviceId;
	}
	
	function getPubDeviceId()
	{
		return $this->pubDeviceId();
	}

	function __construct()
	{
		parent::__construct();
	
		$this->host = "https://mobapp.exclaim.com/pvx/pvxgate.aspx";
		//$this->host = "http://test.exclaimmobile.com/pvx/pvxgate.aspx";

		$pm = PersistenceManager::getInstance();
		$this->deviceId = $pm->load("DEVICE_ID");
		$this->pubDeviceId = $pm->load("PUB_DEVICE_ID");

		$err = get_class($this)." - CommunicationManager:__construct - ";
		if ($this->deviceId)
			return;

		if (!$this->pubDeviceId)
		{
			$this->pubDeviceId = $this->syncDevice();
		}
		
		if (!$this->pubDeviceId)
		{
			ERROUT($err."Unable to syncDevice");
			return;
		}
	}

	function syncDevice()
	{
	INFOUT("syncdevice");
		$params["command"] = "syncDevice";
		$params["app"] = "REST";

		$data = $this->request($params);
		$this->pubDeviceId = urlencode($data["device_id"]);
		$pm = PersistenceManager::getInstance();
		$pm->save("PUB_DEVICE_ID", $this->pubDeviceId);
		return $this->pubDeviceId;
	}

	function authenticate($uname, $pwd)
	{
	INFOUT("authenticate");
		$params["device_id"] = $this->pubDeviceId;
		$params["command"] = "authenticate";
		$params["user_name"] = $uname;
		$params["password"] = $pwd;

		$data = $this->request($params);
		$this->deviceId = urlencode($data["device_id"]);
		$pm = PersistenceManager::getInstance();
		$pm->save("DEVICE_ID", $this->deviceId);
		return $this->deviceId;
	}

	function request($params)
	{		
		$delimiter = "?";
		$paramString = "";
		foreach (array_keys($params) as $key)
		{
			$paramString .= $delimiter;
			$paramString .= $key;
			$paramString .= "=";
			$paramString .= $params[$key];
			$delimiter = "&";
		}
		$jsonurl = $this->host.$paramString;
		$pm = PersistenceManager::getInstance();
		INFOUT("JSON Request: ".$jsonurl);
		$json = file_get_contents($jsonurl);
		$data = json_decode($json, true);
		INFOUT("JSON Response: ".print_r($data, true));
		
		return $data;
	}

	function getResources($ids)
	{
		$ret = array();
		$idsStr = null;
		do
		{
			$pm = PersistenceManager::getInstance();

			if (!is_array($ids))
				return null;

			$delimiter = "";
			
			
			$rt = ResourceTable::getInstance();
			foreach ($ids as $id)
			{
				if ($id == null)
				{
					continue;
				}
/*
				if ($id == "./PRINTS")
				{
					$ret[] = new Resource("./PRINTS", "Print Queue", "Print.png");
					continue;
				}

				if ($id == "./SELECTED_PRINTS")
				{
					$ret[] = new Resource("./SELECTED_PRINT", "Selected Prints", "Checked.png");
					continue;
				}

				if ($id == "./SOURCES")
				{
					$ret[] = new Resource("./SOURCES", "Home", "home.png");
					continue;
				}

				if ($id == "./TARGETS")
				{
					$ret[] = new Resource("./TARGETS", "Custom Sources", "Targets.png");
					continue;
				}

				if ($id == "./SELECTED_SOURCES")
				{
					$ret[] = new Resource("./SELECTED_SOURCES", "Selected Sources", "Checked.png");
					continue;
				}

				if ($id == "./SELECTED_TARGETS")
				{
					$ret[] = new Resource("./SELECTED_TARGETS", "Selected Targets", "Checked.png");
					continue;
				}
*/
				if ($id == "/")
				{
					$ret[] = $rt->getResource("/");
					continue;
				}				

				$idsStr .= $delimiter.$id;
				$delimiter = ",";
			}

			if ($idsStr === null)
			{
				break;
			}
			
			$params["command"] = "getResources";
			$params["resource_id"] = $idsStr;
			$params["device_id"] = $this->deviceId;
			$params["page"] = 1;
			$params["page_size"] = 400;

			$data = $this->request($params);
			foreach ($data["resource"] as $dataItem)
			{
				$ret[$i++] = new Resource($dataItem["resource_id"], $dataItem["name"], $dataItem["ThumbURL"],$dataItem["resource_type"]);
			}
		} while (false);

		return $ret;
	}

	function getChildResources($ids)
	{
		$ret = null;
		$idsStr = null;

		do
		{
			$err = get_class($this)." - CommunicationManager:getChildResources - ";
			if (!$ids)
			{
				ERROUT($err."supplied path is null");
				break;
			}
			
			if (!is_array($ids))
			{
				ERROUT($err."supplied path is of the incorrect type - expecting array: ".print_r($ids, true));
				break;
			}


			$pm = PersistenceManager::getInstance();

			$delimiter = "";
			foreach ($ids as $id)
			{
				if ($id == null)
				{
					continue;
				}
/*				
				if ($id == "./")
				{
					$ret[] = new Resource("./SOURCES", "Custom Sources", "Pinned.png");
					$ret[] = new Resource("./TARGETS", "Custom Targets", "Pinned.png");
					$ret[] = new Resource("./SELECTED_SOURCES", "Selected Sources", "Checked.png");
					$ret[] = new Resource("./SELECTED_TARGETS", "Selected Targets", "Checked.png");
					$ret[] = new Resource("./PRINTS", "Print Queue", "Print.png");
					$ret[] = new Resource("./SELECTED_PRINTS", "Selected Prints", "Checked.png");
					continue;
				}
				
				if ($id == "./SOURCES")
				{
					$resources = $this->getResources($pm->load("SOURCE_IDS"));
					if (is_array($resources))
						$ret = array_merge($ret, $resources);
					continue;
				}
				if ($id == "./SELECTED_SOURCES")
				{
					$resources = $this->getResources($pm->load("SELECTED_SOURCE_IDS"));
					if (is_array($resources))
						$ret = array_merge($ret, $resources);
					continue;
				}
				if ($id == "./TARGETS")
				{
					$resources = $this->getResources($pm->load("TARGET_IDS"));
					if (is_array($resources))
						$ret = array_merge($ret, $resources);
					continue;
				}
				if ($id == "./SELECTED_TARGETS")
				{
					$resources = $this->getResources($pm->load("SELECTED_TARGET_IDS"));
					if (is_array($resources))
						$ret = array_merge($ret, $resources);
					continue;
				}
				if ($id == "./PRINTS")
				{
					$resources = $this->getResources($pm->load("PRINT_IDS"));
					if (is_array($resources))
						$ret = array_merge($ret, $resources);
					continue;
				}
				if ($id == "./SELECTED_PRINTS")
				{
					$resources = $this->getResources($pm->load("SELECTED_PRINT_IDS"));
					if (is_array($resources))
						$ret = array_merge($ret, $resources);
					continue;
				}
*/

				if ($id == "/")
				{
					$id = "";
				}

				$idsStr .= $delimiter.$id;
				$delimiter = ",";
			}

			if ($idsStr === null)
				break;

			$params["command"] = "getChildResources";
			$params["resource_id"] = $idsStr;
			$params["device_id"] = $this->deviceId;
			$params["page"] = 1;
			$params["page_size"] = 100;

			$data = $this->request($params);
			
			
			
			if ($data != null)
			{
				foreach ($data["resource"] as $dataItem)
				{
					$ret[] = new Resource($dataItem["resource_id"], $dataItem["name"], $dataItem["ThumbURL"],$dataItem["resource_type"]);
				}
			}

		} while (false);		

		return $ret;
	}
/*	
	public function pushState($title, $model)
	{
		$stack = $pm->load("STACK");
		$serialized = urlencode(serialize($model));
		$stack[$title] = $serialized;
		$pm->save("STACK", $stack);
	}
*/	
	public function popUntil($key)
	{
		$stack = $pm->load("STACK");
		//print_r($stack);
		while (count($stack) && (end($stack) != $stack[$key]))
			$item = array_pop($stack);
		
		//$item = array_pop($stack);
		
		//print_r(  unserialize(urldecode($item)));
		$pm->save("STACK", $stack);
		return unserialize(urldecode($item));
	}

	public function popState()
	{
		$stack = $pm->load("STACK");
		$item = array_pop($stack);
		$pm->save("STACK", $stack);
		return unserialize(urldecode($item));
	}


	public function updateSelection($collection, $domain, $selected)
	{
		$ret = null;

		do
		{
			if (!$domain)
				break; // nothing can be changed, so break

			if (!$collection)
			{
				if (is_array($selected))
				{
					foreach ($selected as $item)
					{
						if (in_array($item, $domain))
							$collection[] = $item; // the collection is empty so it resolves to the selection
					}
				}
			}

			foreach ($domain as $item)
			{
				$selected_index = false;

				if (is_array($selected))
				{
					$selected_index = array_search($item, $selected);
				}

				$collection_index = false;
				if (is_array($collection))
					$collection_index = array_search($item, $collection);
	
				if ($selected_index !== false) // should be in collection
				{
					if ($collection_index === false)
						$collection[] = $item;

				}
				else // should not be in collection
				{
					if ($collection_index !== false)
						unset($collection[$collection_index]);
				}
			}
		} while (false);

		$ret = $collection;

		return $ret;
	}

}
?>

Bean

<?php include_once "autoload.php";

abstract class Bean
{
	public function set($field, &$value) { $this->$field = $value; }
	public function &get($field) { return $this->$field; }

	public function __construct()
	{
	}

	public function map_to_vars(&$map)
	{
		foreach (array_keys(get_object_vars($this)) as $key)
		{
			$this->$key = $map[$key];
		}
	}
}
?>