SharedObjectMgrAbstract.php 4.43 KB
<?php
/**
 * @class SharedObjectMgrAbstract
 * @version 
 * 
 */

abstract class SharedObjectMgrAbstract {
	protected $rootdirname = "";
	
	private static $headerdirname = "header";
	
	private static $datadirname   = "data";
	
	function __construct() {
		if (!is_dir($this->getRootDirPath())) {
			mkdir($this->getRootDirPath());
			chgrp($this->getRootDirPath(), APACHE_USER);
			chmod($this->getRootDirPath(), 0775);
		}
	}
	
	abstract protected function getDataInfo($object_id);
	
	abstract protected function addData($src_object_path, $dst_data_path, $newId, $newName);
	
	public function getDescription($folder_name, $object_id) {
		$result = array();
		
		$headerFilePath   = $this->getHeaderFilePath($folder_name, $object_id);
		$headerFile       = new SharedObjectHeaderFile($headerFilePath);
		
		//Get info contained in data file
		$result = $this->getDataInfo($object_id);
		
		//Get info contained in header file
		$header_info = $headerFile->getInfo();
		
		foreach ($header_info as $key => $value) {
			//if (!array_key_exists($key, $result) && empty($value))
			//	continue;
			$result[$key] = $value;
		}
		
		return $result;
	}
	
	public function add($object_name, $folder_name, $src_object_path, $description, $sharedBy, $newId) {
		$dst_folder_path = $this->getFolderDirPath($folder_name);
		if (!is_dir($dst_folder_path))
			return array('success' => false, 'message' => 'Cannot get folder path');
		
		$dst_header_path = $this->getHeaderDirPath($folder_name);
		if (!is_dir($dst_header_path)) {
			mkdir($dst_header_path);
			chgrp($dst_header_path, APACHE_USER);
			chmod($dst_header_path, 0775);
		}
		
		$dst_data_path = $this->getDataDirPath($folder_name);
		if (!is_dir($dst_data_path)) {
			mkdir($dst_data_path);
			chgrp($dst_data_path, APACHE_USER);
			chmod($dst_data_path, 0775);
		}
		
		$headerFilePath   = $this->getHeaderFilePath($folder_name, $newId);
		$headerFile = new SharedObjectHeaderFile($headerFilePath);
		
		$headerFile->setName($object_name);
		$headerFile->setDescription($description);
		$headerFile->setSharedBy($sharedBy);
		date_default_timezone_set("UTC");
		$headerFile->setSharedDate(date("c"));
		
		$result = $this->addData($src_object_path, $dst_data_path, $newId, $object_name);
		
		if (!$result['success'])
			return $result;
		
		return array('success' => TRUE, 'object_id' => $newId);
	}
	
	public function getRootDirPath() {
		return SHAREDPATH . "/" . $this->rootdirname;
	}
	
	public function getFolderDirPath($folder_name) {
		return $this->getRootDirPath() . "/" . $folder_name;
	}
	
	public function getHeaderDirPath($folder_name) {
		return $this->getFolderDirPath($folder_name) . "/" . self::$headerdirname;
	}
	
	public function getDataDirPath($folder_name) {
		return $this->getFolderDirPath($folder_name) . "/" . self::$datadirname;
	}
	
	public function getHeaderFilePath($folder_name, $object_id) {
		return $this->getHeaderDirPath($folder_name) . "/" . $object_id . ".xml";
	}
	
	public function getDataFilePath($folder_name, $object_id) {
		return $this->getDataDirPath($folder_name) . "/" . $object_id . ".xml";
	}
	
	public function scanObjects() {
		$root_dir = $this->getRootDirPath();
		$folders = $this->scanDir($root_dir, TRUE, FALSE);
		$result = array();
		foreach ($folders as $folder_name) {
			$header_path = $this->getHeaderDirPath($folder_name);
			if (!is_dir($header_path)) {
				mkdir($header_path);
				chgrp($header_path, APACHE_USER);
				chmod($header_path, 0775);
			}
			$header_files = $this->scanDir($header_path, FALSE, TRUE);
			
			$data_path = $this->getDataDirPath($folder_name);
			if (!is_dir($data_path)) {
				mkdir($data_path);
				chgrp($data_path, APACHE_USER);
				chmod($data_path, 0775);
			}
			$data_files = $this->scanDir($data_path, FALSE, TRUE);
			
			foreach ($header_files as $header_file) {
				//Check if we have header and data files
				if (!in_array($header_file, $data_files))
					continue;
				if (!array_key_exists($folder_name, $result))
					$result[$folder_name] = array();
				
				$result[$folder_name][] = basename($header_file, ".xml");;
			}
			//Add empty folders
			if (!array_key_exists($folder_name, $result))
				$result[$folder_name] = array();
		}
		
		return $result;
	}
	
	private function scanDir($dir, $getDirs, $getFiles) {
		$dirs = array_diff(scandir($dir), Array( ".", ".." ));
		
		$result = array();
		foreach($dirs as $d){
			if($getDirs && is_dir($dir."/".$d))
				$result[] = $d;
			if($getFiles && !is_dir($dir."/".$d))
				$result[] = $d;
		}
		
		return $result;
	}
}

?>