<?php
	class DirtyObjectModel {

		protected function generateWhereClause( $user_id = 0 , $object = "" , $subobject = "" ) {
			$filters = array();
			if ($user_id) $filters[]=sprintf( "user_id=%d" , $user_id );
			if (!empty( $object )) $filters[]=sprintf( "dirty_object='%s'" , FNTH_db_addslashes( $object ) );
			if (!empty( $subobject )) $filters[]=sprintf( "dirty_subobject='%s'" , FNTH_db_addslashes( $subobject ));
			if (empty( $filters )) return "";
			return " where " . implode( " and " , $filters );
		}

		public function setDirtyObject( $user_id = 0 , $object = "" , $subobject = "" ) {
			if ($this->isDirtyObject( $user_id , $object , $subobject )) return;
			$ins = array();
			if ($user_id) $ins["user_id"]=sprintf( "%d" , $user_id );
			if (!empty( $object )) $ins["dirty_object"]=sprintf( "'%s'" , FNTH_db_addslashes( $object ));
			if (!empty( $subobject )) $ins["dirty_subobject"]=sprintf( "'%s'" , FNTH_db_addslashes( $subobject ));
			FNTH_db_free_result(
				FNTH_db_query(
					FNTH_db_generate_insert_query( "dirty_flags" , $ins )
				)
			);
		}

		public function unsetDirtyObject( $user_id = 0 , $object = "" , $subobject = "" ) {
			$where_clause = $this->generateWhereClause( $user_id , $object , $subobject );
			$query = sprintf( "delete from dirty_flags %s" , $where_clause );
			FNTH_db_free_result( FNTH_db_query( $query ));
		}

		public function isDirtyObject( $user_id = 0 , $object = "" , $subobject = "" ) {
			$where_clause = $this->generateWhereClause( $user_id , $object , $subobject );
			$query = sprintf( "select count(*) as num from dirty_flags %s" , $where_clause );
			return (FNTH_db_get_field( $query , "num" )>0);
		}

	}	
?>
