




/**
 * create_process_status
 * Description: Creates a record for FID in media_process_status table
 * @param  $fid, $type
 */
function create_process_status($fid, $type) {
	//Query setup and execute
	
	$result = db_query("INSERT INTO {media_process_status} (fid, type) VALUES (%d, %d)", $fid, $type);
	
	//If fail - watchdog notify
	if (!$result) {
		 watchdog('media_process', t("media_process: couldn't insert a record into media_process_table for fileid $fid"), WATCHDOG_WARNING);
	}
	
	//Return Result
	return $result;
}


//AND IT GETS CALLED BY detect_handler() function


/**
 * Detects que files, sort types, and handles
 * @param
 * @return
 * 		Returns Null if there was no job to be processed. False if process fails.
 */
function detect_handler() {
	
	//Narrow List of files to be processed - By Files & media_process_status tables comparisson, Order list by filemime
	//Query setup
	$node_type_allow = "'submission', 'submission_ar'"; //@todo get it from the front-end admin
	
	$query_filter  = "SELECT node.nid, node.vid, files.filename, files.filemime, files.fid, files.filepath FROM node, files WHERE  blah blah";	
	
	$result_filter = db_query($query_filter); //Grab result
	
	//Loop through records of the result
	while ($files = db_fetch_array($result_filter)) {
		
		//Setup Vars
		$filemime = $files["filemime"];
		$filename = $files["filename"];
		$filepath = $files["filepath"];
		$fid	  = (int) $files["fid"];
		$vid 	  = (int) $files["vid"];
		$nid  	  = (int) $files["nid"];
	
		$basepath = exec('pwd');  												//Grab the current location by executing pwd on system level
		$location = $basepath . "/" . file_directory_path()  .'/';				//Setup the Location
	
		//Check if file exists in Files folder
	    $filecheck_status = media_process_filecheck($filename, $location);	
	    
	    if ($filecheck_status == TRUE) {
	    	var_dump("found");
			//Call function CheckfileType() passing filemime type and filename to find out if image or video 
			$type = CheckfileType($filemime, $filename);
		
			//Create a record for it on update_status
			$result_create = create_process_status($fid, $type);
	
			//During the loop, if the file type is Image -- pass @param fid, nid, vid, filename to process_image()
			if ($type == 1) {
				$pimage_result = process_image($fid, $nid, $vid, $filename);
				
			//If the file type is Video -- pass $param fid, nid, filename to process_video()
			} else if ($type == 0) {
				$pimage_result = process_video($fid, $nid, $vid, $filename);
			
			//Else return null
			} else {
				$status = 3;
			}
		
		
		} else {
			watchdog('media_process', t("media_process: Filename $filename could not be found -- skipped the process"), WATCHDOG_WARNING);
		}
		
	}
	
	return;	
}