SharedObjectTreeFile.php 7.25 KB
<?php
/**
 * @class SharedObjectTreeFile
 * @version 
 * 
 */

class SharedObjectTreeFile  {
	public static $FILE_NAME = "SharedObjectTree.xml";
	
	private $doc = null;
	
	public static $ROOT_NODE_NAME = "shared";
	public static $LIST_NODE_NAME_SUFFIX = "List";
	
	function __construct() {
		$this->init();
	}
	
	public function update($object_type, $object_mgr) {
		//Get available objects
		$objects = $object_mgr->scanObjects();
		
		//Update related folders and objects
		$listNode = $this->getObjectListNode($object_type);
		
		if (!isset($listNode))
			$listNode = $this->createObjectListNode($object_type);
		
		$lastObjectId = $listNode->getAttribute("lastObjectId");
		
		foreach ($objects as $folder_name => $folder_objects) {
			$folderNode = $this->getObjectFolderNode($object_type, $folder_name);
			
			if (!isset($folderNode))
				$folderNode = $this->createObjectFolderNode($object_type, $folder_name);
			
			foreach ($folder_objects as $object_id) {
				$objectNode = $this->getObjectNode($object_type, $folder_name, $object_id);
				
				if (!isset($objectNode))
					$objectNode = $this->createObjectNode($object_type, $folder_name, $object_id);
				
				if (!isset($objectNode))
					continue;
				
				$description = $object_mgr->getDescription($folder_name, $object_id);
				
				foreach ($description as $key => $value)
					$objectNode->setAttribute($key, $value);
			}
		}
		
		
		//Remove obsolete folders and objects
		
		$this->doc->save($this->getFilePath());
		
		return array('success' => TRUE);
	}
	
	public function reserveNewId($object_type) {
		$listNode = $this->getObjectListNode($object_type);
	
		if (!isset($listNode))
			return NULL;
	
		$lastObjectId = $listNode->getAttribute("lastObjectId");
	
		if ($lastObjectId == "")
			$lastObjectId = 'shared' . $object_type . '_0';
		else {
			$res = sscanf($lastObjectId , 'shared' . $object_type . '_%d');
			$lastObjectId = 'shared' . $object_type . '_' . ($res[0] + 1);
		}
	
		$listNode->setAttribute("lastObjectId", $lastObjectId);
	
		$this->doc->save($this->getFilePath());
	
		return $lastObjectId;
	}
	
	public function getFolders($object_type) {
		$listNode = $this->getObjectListNode($object_type);
		
		if (!isset($listNode))
			return array();
		
		$folderNodes = $listNode->getElementsByTagName("folder");
		
		$folders = array();
		foreach ($folderNodes as $folderNode) {
			$nameAtt = $folderNode->getAttribute('name');
			$idAtt = $folderNode->getAttribute('xml:id');
			if (empty($nameAtt) || empty($idAtt))
				continue;
			$folders[] = array('id' => $idAtt, 'name' => $nameAtt);
		}
		
		return $folders;
	}
	
	public function isNameAlreadyUsed($object_type, $name) {
		$listNode = $this->getObjectListNode($object_type);
		
		if (!isset($listNode))
			return FALSE;
		
		$folderNodes = $listNode->getElementsByTagName("folder");
		
		foreach ($folderNodes as $folderNode) {
			$objectNodes = $folderNode->getElementsByTagName($object_type);
			foreach ($objectNodes as $objectNode) {
				if ($objectNode->getAttribute('name') == $name)
					return TRUE;
			}
		}
		
		return FALSE;
	}
	
	public function getFolderNameById($object_type, $folder_id) {
		$listNode = $this->getObjectListNode($object_type);
	
		if (!isset($listNode))
			return "";
	
		$folderNodes = $listNode->getElementsByTagName("folder");
	
		foreach ($folderNodes as $folderNode) {
			$nameAtt = $folderNode->getAttribute('name');
			$idAtt = $folderNode->getAttribute('xml:id');
			if (empty($nameAtt) || empty($idAtt))
				continue;
			if ($folder_id != $idAtt)
				continue;
			return $nameAtt;
		}
	
		return FALSE;
	}
	
	public function getFolderIdByObjectId($object_type, $object_id) {
		$listNode = $this->getObjectListNode($object_type);
		if (!isset($listNode))
			return "";

		$folderNodes = $listNode->getElementsByTagName("folder");
		foreach ($folderNodes as $folderNode) {
			$nameAtt = $folderNode->getAttribute('name');
			$idAtt = $folderNode->getAttribute('xml:id');
			if (empty($nameAtt) || empty($idAtt))
				continue;
			
			$objectNodes = $folderNode->getElementsByTagName($object_type);
			
			foreach ($objectNodes as $objectNode) {
				if ($objectNode->getAttribute('xml:id') == $object_id)
					return $idAtt;
			}
		}
		return "";
	}
	
	private function init() {
		if (!file_exists($this->getFilePath()))
			$this->createFile();
		else
			$this->loadFile();
	}
	
	public function getFilePath() {
		
		return SHAREDPATH . "/" . self::$FILE_NAME;
	}
	
	private function createFile() {
		$this->doc = new DOMDocument("1.0", "UTF-8");
		$this->doc->preserveWhiteSpace = false;
		$this->doc->formatOutput = true;
		
		$xmlNode = $this->doc->createElement("shared");
		$this->doc->appendChild($xmlNode);
		
		$this->doc->save($this->getFilePath());
		chgrp($this->getFilePath(), APACHE_USER);
		chmod($this->getFilePath(), 0775);
	}
	
	private function loadFile() {
		$this->doc = new DOMDocument("1.0", "UTF-8");
		$this->doc->preserveWhiteSpace = false;
		$this->doc->formatOutput = true;
		
		$this->doc->load($this->getFilePath());
	}
	
	private function getObjectListNode($object_type) {
		$nodeName = $object_type . "List";
		$listNodes = $this->doc->documentElement->getElementsByTagName($nodeName);
		
		if (count($listNodes) == 0) 
			return NULL;
		
		return $listNodes->item(0);
	}
	
	private function createObjectListNode($object_type) {
		$nodeName = $object_type . "List";
		
		$listNode = $this->doc->createElement($nodeName);
		$listNode->setAttribute("xml:id", "shared" . $object_type . "-treeRootNode");
		$this->doc->documentElement->appendChild($listNode);
		$this->doc->save($this->getFilePath());
		return $listNode;
	}
	
	private function getObjectFolderNode($object_type, $folder_name) {
		$listNode = $this->getObjectListNode($object_type);
		if (!isset($listNode))
			return NULL;
		
		$folderNodes = $listNode->getElementsByTagName("folder");
		
		foreach ($folderNodes as $folderNode) {
			if ($folderNode->getAttribute('name') == $folder_name)
				return $folderNode;
		}
		
		return NULL;
	}
	
	private function createObjectFolderNode($object_type, $folder_name) {
		$listNode = $this->getObjectListNode($object_type);
		if (!isset($listNode))
			return NULL;
		
		$folderNode = $this->doc->createElement("folder");
		$folderNode->setAttribute("xml:id", $folder_name . '_' . $object_type);
		$folderNode->setAttribute("name", $folder_name);
		
		$listNode->appendChild($folderNode);
		$this->doc->save($this->getFilePath());
		return $folderNode;
	}
	
	private function getObjectNode($object_type, $folder_name, $object_id) {
		$folderNode = $this->getObjectFolderNode($object_type, $folder_name);
		
		if (!isset($folderNode))
			return NULL;
		
		$objectNodes = $folderNode->getElementsByTagName($object_type);
		
		foreach ($objectNodes as $objectNode) {
			if ($objectNode->getAttribute('xml:id') == $object_id)
				return $objectNode;
		}
		
		return NULL;
	}
	
	private function createObjectNode($object_type, $folder_name, $object_id) {
		$listNode = $this->getObjectListNode($object_type);
		
		if (!isset($listNode))
			return NULL;
		
		$folderNode = $this->getObjectFolderNode($object_type, $folder_name);
		
		if (!isset($folderNode))
			return NULL;
		
		$objectNode = $this->doc->createElement($object_type);
		$objectNode->setAttribute("xml:id", $object_id);
		
		$folderNode->appendChild($objectNode);
		
		$this->doc->save($this->getFilePath());
		
		return $objectNode;
	}
}

?>