<?
class flags {
	
	function flags() {
		$this->monthlyCleanUp();
	}
	
	function monthlyCleanUp() {
		global $db,$settings;
		/* cleaning time */
		if ($db->GetOne("SELECT val FROM ".$settings->config['permvars_tablename']." WHERE var='flagsMonthlyCleanUpDate'") <= time()) {
		/* get no-flag paths */
			$db->query("DELETE FROM ".$settings->config['flags_tablename']." WHERE shared='0' AND public='0' AND discussion='0'");
			setDBPermVar('flagsMonthlyCleanUpDate', time()+604800);
			/* 
			604800 seconds = weekly cleanup
			 */
			return $db->Affected_Rows();
		} else {
			return false;
		}
	}
	
	function getFidByPath($path) {
		global $db, $auth, $settings, $str;
		$fid = $db->GetOne("SELECT id FROM ".$settings->config['flags_tablename']." WHERE uid='".$auth->currentUserInfo['id']."' and path=".$db->Quote($path)."",false);
		return $fid;
	}
	
	function getRecordByPath($path) {
		global $db, $auth, $settings, $str;
		$fid = $db->GetRow("SELECT * FROM ".$settings->config['flags_tablename']." WHERE uid='".$auth->currentUserInfo['id']."' and path=".$db->Quote($path)."",false);
		return $fid;
	}
	
	
	function getFidByPathAndFlag($path, $flag) {
		global $db, $auth, $settings, $str;
		$fid = $db->GetOne("SELECT id FROM ".$settings->config['flags_tablename']." WHERE uid='".$auth->currentUserInfo['id']."' and path=".$db->Quote($path)." and ".$flag."='1'",false);
		return $fid;
	}
	
	
	function getRecordByFid($fid) {
		global $db, $settings;
		return  $db->GetRow("SELECT * FROM ".$settings->config['flags_tablename']." WHERE id='".$fid."'",false);
	}
	
	
	function hasFlag($path, $flag) {
		global $db, $auth, $settings, $str;
		$fl = $db->GetOne("SELECT ".$flag." FROM ".$settings->config['flags_tablename']." WHERE uid='".$auth->currentUserInfo['id']."' and path=".$db->Quote($path)." and ".$flag."='1'",false);
		if ($fl) {
			return true;
		} else {
			return false;
		}
	}

	function hasRecord($path) {
		global $db, $auth, $settings, $str;
		$fl = $db->GetOne("SELECT id FROM ".$settings->config['flags_tablename']." WHERE uid='".$auth->currentUserInfo['id']."' and path=".$db->Quote($path)."",false);
		if ($fl) {
			return true;
		} else {
			return false;
		}
	}

	function setFlag($path, $flag, $value) {
		global $db, $auth, $settings, $str;
		if ($this->hasRecord($path)) {
			$db->query("UPDATE `".$settings->config['flags_tablename']."` SET ".$flag."='".$value."' WHERE uid='".$auth->currentUserInfo['id']."' and path=".$db->Quote($path)."");
		} else {
			$db->query("INSERT INTO `".$settings->config['flags_tablename']."` ( `uid` , `id` , `path` , `shared` , `public` , `discussion` ) 
VALUES (
'".$auth->currentUserInfo['id']."', '', '".$str->safeSQL($path)."', '".(($flag=="shared")?$value:"0")."', '".(($flag=="public")?$value:"0")."', '".(($flag=="discussion")?$value:"0")."'
)");
		}

		if ($db->Affected_Rows() > 0) {
			return true;
		} else {
			return false;
		}
	}

	function changeFlaggedPath($oldPath, $newPath) {
		global $db, $auth, $settings, $str;
		$db->query("UPDATE `".$settings->config['flags_tablename']."` SET path='".$str->safeSQL($newPath)."' WHERE uid='".$auth->currentUserInfo['id']."' and path=".$db->Quote($path)."");
		return true;
	}
	
	function updateSubPaths($oldStartPath, $newStartPath) {
		global $db, $auth, $settings, $str;
		$db->query("UPDATE `".$settings->config['flags_tablename']."` 
SET 
path=REPLACE(path,'".$str->safeSQL(gluePath($oldStartPath, "/"))."','".$str->safeSQL(gluePath($newStartPath, "/"))."')  WHERE uid='".$auth->currentUserInfo['id']."' AND path like '".$str->safeSQL(gluePath($oldStartPath, "/"))."%'");
		return true;
	}

}

?>