IHMUserWSManagerClass.php 2.91 KB
<?php

/**
 * @class IHMUserWSManagerClass
 * @brief Manager for IHM user workspace
 * @details
 */
class IHMUserWSManagerClass
{
	private static $WS_VERSION = 1;

	protected $wsInfo      = null;

	public function init()
	{
		//check user workspace
		if (IHMConfigClass::getUserName() == "" || !is_dir(IHMConfigClass::getUserPath()))
			throw new Exception('Cannot find user workspace.');

		//Load info about WS
		if (!$this->loadWSInfoFile())
			throw new Exception('Error to load workspace info file.');

		//Update WS if need
		if (!$this->update())
			throw new Exception('Error during user workspace conversion.');

		return TRUE;
	}

	public function update()
	{
		while (IHMUserWSManagerClass::$WS_VERSION > $this->wsInfo['version']) {
			$updateMethod = "updateFromVersion".$this->wsInfo['version'];
			if (method_exists($this,$updateMethod)) {
				if (!$this->{$updateMethod}()) {
					return FALSE;
				}
			}
			$this->wsInfo['version'] += 1;
			if (!$this->saveWSInfoFile()) {
				return FALSE;
			}
		}
		return TRUE;
	}

	private function loadWSInfoFile()
	{
		if (file_exists(IHMConfigClass::getUserWSInfoFilePath())) {
			$infoContent = file_get_contents(IHMConfigClass::getUserWSInfoFilePath());
			if (empty($infoContent)) {
				return FALSE;
			}
			$this->wsInfo = json_decode($infoContent, TRUE);
			if (empty($this->wsInfo)) {
				return FALSE;
			}
			return TRUE;
		}

		//Init info file
		$this->wsInfo = array(
			"version" => 0,
		);

		return $this->saveWSInfoFile();
	}

	private function saveWSInfoFile()
	{
		if (empty($this->wsInfo)) {
			return FALSE;
		}

		$json_data = json_encode($this->wsInfo);

		if ($json_data === FALSE) {
			return FALSE;
		}


		return file_put_contents(IHMConfigClass::getUserWSInfoFilePath(),$json_data);
	}

	private function updateFromVersion0() {
		// This update split plot requests

		// Load user requests file
		$req_mgr_file_path = IHMConfigClass::getUserRequestManagerFilePath();
		if (!file_exists($req_mgr_file_path)) {
			return TRUE;
		}

		$dom = new DOMDocument();
		if (!$dom->load($req_mgr_file_path)) {
			return FALSE;
		}

		// Retrieve all plot requests and last request id
		$plot_requests = array();
		$last_id = 0;
		foreach ($dom->getElementsByTagName('request') as $requestNode) {
			$request_id = $requestNode->getAttribute("xml:id");
			$plot_requests[$request_id] = $requestNode;
			sscanf($request_id, "req_%d", $crt_id);
			if ($crt_id > $last_id) {
				$last_id = $crt_id;
			}
		}

		if (empty($plot_requests)) {
			//No plot requests
			return TRUE;
		}

		// Split requests
		foreach ($plot_requests as $id => $node) {
			$req_data_path = IHMConfigClass::getStoredRequestPath() . $id;
			if (!file_exists($req_data_path)) {
				continue;
			}
			$req_data_content = file_get_contents($req_data_path);
			if (!$req_data_content) {
				continue;
			}
			$req_data_json = json_decode($req_data_content);
			if (!$req_data_json) {
				continue;
			}
			// ToDo
		}
		

		return TRUE;
	}
}