<?

class comments {

var $error;

var $tmpdb;

function get($path) {
	global $commentFileName, $fm;
	$dir = $fm->dirname($path);
	$file = $fm->basename($path);
	$descrFile = gluePath($dir,$commentFileName);
	if (file_exists($descrFile)) {
	$this->tmpdb = new CSV($commentFileName, $dir);
	$this->tmpdb->assoc = true;
	$this->tmpdb->readDB();
	$result = $this->tmpdb->find(array("object" => $file));
		if (is_array($result)) {
			return $result;
		} else {
			return false;
		}
	} else {
		return false;
	}
}


function set($path, $description = "") {
	global $commentFileName, $fm, $auth;
	if (strlen($description) > 0) {
		$dir = $fm->dirname($path);
		$file = $fm->basename($path);
		$db = new CSV($commentFileName, $dir);
		$db->assoc = true;
		$db->readDB();
		$db->newrows[] = array ("object"=>$file, "user" => $auth->currentUserInfo['username'], "date" => time(), "msg" => addslashes($description));
		$db->append();
		$db->writeDB(false, true);
	}
}

function delete($path, $date="") {
	global $commentFileName, $fm;
		$dir = $fm->dirname($path);
		$file = $fm->basename($path);

		$db = new CSV($commentFileName, $dir);
		$db->assoc = true;
		$db->readDB();
		if ($date) {
			$b = $db->find(array("object" => $file, "date" => $date));
		} else {
			$b = $db->find(array("object" => $file));
		}
		if (is_array($b)) {
		for ($i = 0 ; $i < sizeof($b) ; $i++) {
			$db->delete($b[$i]);
		}
		//print_r($db->db);
		$db->writeDB(false, true);
		//echo $db->error;
		}
		$this->deleteIfEmpty($path);
}



//deletes a description file if empty
function deleteIfEmpty($path) {
	global $commentFileName,$fm;
	$dir = $fm->dirname($path);
	
	$descrFile = gluePath($dir,"/",$commentFileName);
	$db = new CSV($commentFileName, $dir);
	$db->assoc = true;
	$db->readDB();
	if (sizeof($db->db) < 1) {
		$fm->delete($descrFile);
	}
}


function copy($oldPath, $newPath) {
		global $commentFileName, $fm;
	/* get current descriptions*/
		$dir = $fm->dirname($oldPath);
		$file = $fm->basename($oldPath);
		$descrFile = gluePath($dir,"/",$commentFileName);
		$db = new CSV($commentFileName, $dir);
		$db->assoc = true;
		$db->readDB();
		$b = $db->find(array("object" => $file));
		
		if (is_array($b)) {
		for ($i = 0 ; $i < sizeof($b) ; $i++) {
			$newrows[] = array("object"=>$db->db[$b[$i]]['object'], "user" => $db->db[$b[$i]]['user'], "date" => $db->db[$b[$i]]['date'], "msg" => $db->db[$b[$i]]['msg']);
		}
		
		
	/* move them */
		$dir = $fm->dirname($newPath);

		$db = new CSV($commentFileName, $dir);
		$db->assoc = true;
		$db->readDB();
		$db->newrows = $newrows;
		$db->append();
		//print_r($db->db);
		$db->writeDB(false, true);
		}
}

function move($oldPath, $newPath) {
		global $commentFileName,$fm;
	/* get current descriptions*/
		$dir = $fm->dirname($oldPath);
		$file = $fm->basename($oldPath);
		$descrFile = gluePath($dir,"/",$commentFileName);
		$db = new CSV($commentFileName, $dir);
		$db->assoc = true;
		$db->readDB();
		$b = $db->find(array("object" => $file));
		
		if (is_array($b)) {
		for ($i = 0 ; $i < sizeof($b) ; $i++) {
			$newrows[] = array("object"=>basename($newPath), "user" => $db->db[$b[$i]]['user'], "date" => $db->db[$b[$i]]['date'], "msg" => $db->db[$b[$i]]['msg']);
		}
		
		for ($i = 0 ; $i < sizeof($b) ; $i++) {
			$db->delete($b[$i]);
		}
		
		if (sizeof($db->db) == 0) {
			$fm->delete($descrFile);
		} else {
			$db->writeDB(false, true);
		}
		
		
	/* move them */
		$dir = $fm->dirname($newPath);
		$file = $fm->basename($newPath);
		$descrFile = gluePath($dir,"/",$commentFileName);
		$db = new CSV($commentFileName, $dir);
		$db->assoc = true;
		$db->readDB();
		$db->newrows = $newrows;
		$db->append();
		//print_r($db->db);
		$db->writeDB(false, true);
		}
}


function htmlDescription($des) {
	$des = trim($des);
	$des = htmlspecialchars(stripslashes($des), ENT_QUOTES);
	$des = wordwrap($des, 40, "\n", 1);
	$des = nl2br($des);
	$des = ereg_replace("(\r\n|\n|\r)", "",$des);
	if (strlen($des) == 0) {$des = "&nbsp;";}
	return $des;
}

}
?>