SharedObjectsMgr.php 4.14 KB
<?php
/**
 * @class SharedObjectsMgr
 * @version 
 * 
 */

class SharedObjectsMgr  {
	private $sharedObjectsMgr = array();
	
	private $treeFile = null;
	
	function __construct() {
		$this->init();
	}
	
	public function updateTree() {
		$sucess = TRUE;
		$message = "";
		foreach ($this->sharedObjectsMgr as $key => $mgr) {
			if (!isset($mgr))
				continue;
			$result = $this->treeFile->update($key, $mgr);
			$sucess &= $result['success'];
			if (!$result['success']) {
				if (!empty($message))
					$message .= "; ";
				$message .= $result['message'];
			}
		}
		
		return array('success' => $sucess, 'message' => $message);
	}
	
	public function add($object_type, $object_name, $folder_id, $src_object_path, $description, $sharedBy) {
		if (!array_key_exists($object_type, $this->sharedObjectsMgr) || !isset($this->sharedObjectsMgr[$object_type]))
			return array('success' => FALSE, 'message' => 'Unknown type');
		
		$folder_name = $this->treeFile->getFolderNameById($object_type, $folder_id);
		
		if (empty($folder_name))
			return array('success' => FALSE, 'message' => 'Cannot retrieve folder');
		
		$newId = $this->treeFile->reserveNewId($object_type);
		
		$result = $this->sharedObjectsMgr[$object_type]->add($object_name, $folder_name, $src_object_path, $description, $sharedBy, $newId);
		
		if ($result['success'])
			$result['folder_id'] = $folder_id;
		
		return $result;
	}

	public function update($object_type, $object_name, $folder_id, $src_object_path, $description, $sharedBy) {
		if (!array_key_exists($object_type, $this->sharedObjectsMgr) || !isset($this->sharedObjectsMgr[$object_type]))
			return array('success' => FALSE, 'message' => 'Unknown type');

		$folder_name = $this->treeFile->getFolderNameById($object_type, $folder_id);

		if (empty($folder_name))
			return array('success' => FALSE, 'message' => 'Cannot retrieve folder');

		$objects = $this->treeFile->getObjectsByName($object_type, $folder_name, $object_name);

		if (!empty($objects)) {
			//delete old objects
			foreach ($objects as $object_id) {
				$result = $this->treeFile->deleteObjectByObjectId($object_type, $folder_name, $object_id);
				$result = $this->sharedObjectsMgr[$object_type]->delete($folder_name, $object_id);
			}
		}

		//add new object
		return $this->add($object_type, $object_name, $folder_id, $src_object_path, $description, $sharedBy);
	}
	
	public function getFolders($object_type) {
		if (!array_key_exists($object_type, $this->sharedObjectsMgr) || !isset($this->sharedObjectsMgr[$object_type]))
			return array();
		
		return $this->treeFile->getFolders($object_type);
	}

	public function createFolder($object_type, $folder_name) {
		if (!array_key_exists($object_type, $this->sharedObjectsMgr) || !isset($this->sharedObjectsMgr[$object_type])) {
			return array('success' => FALSE, 'message' => 'Unknown type');
		}

		if (!$this->sharedObjectsMgr[$object_type]->createFolder($folder_name)) {
			return array('success' => FALSE, 'message' => 'Cannot create folder '.$folder_name);
		}

		return $this->updateTree();
	}
	
	public function isNameAlreadyUsed($object_type, $name) {
		if (!array_key_exists($object_type, $this->sharedObjectsMgr) || !isset($this->sharedObjectsMgr[$object_type]))
			return FALSE;
	
		return $this->treeFile->isNameAlreadyUsed($object_type, $name);
	}
	
	public function getDataFilePath($object_type, $object_id) {
		if (!array_key_exists($object_type, $this->sharedObjectsMgr) || !isset($this->sharedObjectsMgr[$object_type]))
			return "";
		
		$folder_id = $this->treeFile->getFolderIdByObjectId($object_type, $object_id);
		
		$folder_name = $this->treeFile->getFolderNameById($object_type, $folder_id);
		
		if (empty($folder_name))
			return "";
		
		return $this->sharedObjectsMgr[$object_type]->getDataFilePath($folder_name, $object_id);
	}
	
	public function getTreeFilePath() {
		return $this->treeFile->getFilePath();
	}
	
	private function init() {
		if (!is_dir(SHAREDPATH)) {
			mkdir(SHAREDPATH);
			chgrp(SHAREDPATH, APACHE_USER);
			chmod(SHAREDPATH, 0775);
		}
		
		$this->treeFile = new SharedObjectTreeFile();
		$this->sharedObjectsMgr["timeTable"] = new SharedTimeTableMgrImpl();
		$this->sharedObjectsMgr["catalog"] = new SharedCatalogMgrImpl();
	}
}

?>