<?php
defined('WikyBlog') or die("Not an entry point...");

wbLang::getFile('toolOptions');

class watchFile{
	function watchFile(){
		global $page,$dbObject;
		
		if( empty($_SESSION['username']) ){
			message('LOGIN_REQUIRED');
			return;
		}
		if( !$dbObject->exists ){
			message('NON_EXISTANT');
			return;
		}
		
		
		switch($page->userCmd){
			case 'watch':
				$this->watch();
			break;
			case 'unwatch':
				$this->unWatch();
			break;
		}
	}
	function watch(){
		global $wbTables,$dbObject,$langA;
		
		$query = 'REPLACE INTO '.$wbTables['all_watch'];
		$query .= ' SET `user_id` = "'.wbDB::escape($_SESSION['user_id']).'" ';
		$query .= ' , `file_id` = "'.wbDB::escape($dbObject->file_id).'" ';
		if( wbDB::runQuery($query) ){
			$link = wbLinks::special('WatchList',false,'',$_SESSION['username']);
			message('WATCH_UPDATED',$link);
			$dbObject->watching = '1';
		}
		//otherwise, there will be an error
	}
	function unwatch(){
		global $wbTables,$dbObject,$langA;
		
		$query = 'DELETE FROM '.$wbTables['all_watch'];
		$query .= ' WHERE `user_id` = "'.wbDB::escape($_SESSION['user_id']).'" ';
		$query .= ' AND `file_id` = "'.wbDB::escape($dbObject->file_id).'" ';
		if( wbDB::runQuery($query) ){
			$link = wbLinks::special('WatchList',false,'',$_SESSION['username']);
			message('WATCH_UPDATED',$link);
			$dbObject->watching = '0';
		}
		//otherwise, there will be an error
	}
}
new watchFile();
