<?php
/**
* Class Directory_model
*
* @author dzungnv02
*
*/
class Directory_model extends CI_Model 
{

	public function __construct()
	{
		// Call the Model constructor
		parent::__construct();
	}
	
	public function getAllDirectory () {
		$dbFile = 'directory.db';
		$h = fopen($dbFile, 'r');
		$line = '';
		$aryDir = array();
		$aryField = array ('id', 'name', 'parentID');
		
		while (($line = fgets($h, 4096)) !== false) {
			$aryTmp = explode(',', trim($line));
			$aryDir[] = array_combine($aryField, $aryTmp);
		}
		if (!feof($h)) {
			echo "Error: unexpected fgets() fail\n";
		}
		fclose($h);
		
		return $aryDir;
	}
	
	public function getAllFile () {
		$dbFile = 'file.db';
		$h = fopen($dbFile, 'r');
		$line = '';
		$aryFile = array();
		$aryField = array ('id', 'name', 'minetype', 'size', 'parentID');
	
		while (($line = fgets($h, 4096)) !== false) {
			$aryTmp = explode(',', trim($line));
			$aryFile[] = array_combine($aryField, $aryTmp);
		}
		if (!feof($h)) {
			echo "Error: unexpected fgets() fail\n";
		}
		fclose($h);
	
		return $aryFile;
	}
	
	public function createDir($aryDir) {
		$dbFile = 'directory.db';
		$aryField = array ('id', 'name', 'parentID');
		$aryTmp = array();
		$h = fopen($dbFile, 'a');
		$line = '';
		foreach ($aryField as $key => $field) {
			$aryTmp[$field] = $aryDir[$field];
		}
		$line = implode(',',$aryTmp);
		fwrite($h, $line."\n");
		fclose($h);
		return true;
	}
	
	public function createFile($aryFile) {
		$dbFile = 'file.db';
		$aryField = array ('id', 'name', 'minetype', 'size', 'parentID');
		$aryTmp = array();
		$h = fopen($dbFile, 'a+');
		$line = '';
		foreach ($aryField as $key => $field) {
			$aryTmp[$field] = $aryFile[$field];
		}
		$line = implode(',',$aryTmp);
		fwrite($line."\n", $h);
		fclose($h);
		return true;
	}
	
	public function deleteDir ($dirID, $delAllChild = FALSE) {
		$dbFile = 'directory.db';
		$dirList = $this->getAllDirectory();
		
		$lineNumber = 0;
		
		foreach ($dirList as $key => $dir) {
			if ($dir[0] == $dirID) {
				$lineNumber = $key;
			}
		}	
		
		//$aryChildDir =  
		
		if ($delAllChild) {
			
		}
		
		unset($dirList[$lineNumber]);
		return $this->writeDB ($dbFile, $dirList);
	}
	
	public function deleteFile ($fileID) {
		$dbFile = 'file.db';
		$fileList = $this->getAllFile();
		
		$lineNumber = 0;
		
		foreach ($fileList as $key => $file) {
			if ($dir[0] == $dirID) {
				$lineNumber = $key;
			}
		}
		
		unset($fileList[$lineNumber]);
		return $this->writeDB ($dbFile, $fileList);
	}
	
	public function getAllFileChild ($dirID) {
		$fileList = $this->getAllFile();
		$aryChild = array();
		
		$aryDirChild = $this->getAllDirChild($dirID);
		
		foreach ($aryDirChild as $key => $child) {
			
		} 
		
	}
	
	public function getAllDirChild ($dirID) {
		$dirList = $this->getAllDirectory();
		$aryChild = array();
		
		foreach ($dirList as $key => $dir) {
			if ($dir['parentID'] == $dirID) {
				$aryChild[$dir['id']] = $dir;
				
				$aryChild[$dir['id']]['child'] = $this->getAllDirChild($dir['id']);
				if (count($aryChild[$dir['id']]['child']) == 0) unset($aryChild[$dir['id']]['child']);
			}
		}
		
		return $aryChild;
	}
	
	private function writeDB ($file, $aryDataSet, $flag = '') {
		$flag = ($flag == 'append') ? 'a' : 'w';
		
		if (file_exists($file) && count($aryDataSet) > 0) {
			$h = fopen ($file, $flag);
			foreach ($aryDataSet as $key => $aryTmp) {
				$line = implode(',',$aryTmp);
				fwrite($h, $line."\n");
			}
			fclose ($h);
			
			return true;
		}else {
			return false;
		}
	}
	
	public function getMaxID($isDir) {
		$list = $isDir = true ? $this->getAllDirectory() : $this->getAllFile();
		$maxID = 0;
		foreach ($list as $key => $e) {
			if ($e['id'] > $maxID) $maxID = $e['id'];
		}
		
		return $maxID;
	}
	
	public function rename ($id, $newName, $type = 'directory') {
		
		if (!$newName || !$id) return FALSE;
		$aryObj = $type == 'directory' ? $this->getAllDirectory() : $this->getAllFile();
		$dbFile = $type == 'directory' ? $dbFile = 'directory.db' : $dbFile = 'file.db';
		
		if (count($aryObj) > 0) {
			foreach ($aryObj as $key => $obj) {
				if ($obj['id'] == $id) break;
			}
			
			$aryObj[$key]['name'] = $newName;
			
			return $this->writeDB($dbFile, $aryObj);
		}
		
		return FALSE;
	}
	
}