<?
 
/*
* CPMObjectEventHandler: SimpleIncidentCPM
* Package: OracleServiceCloud
* Objects: Incident
* Actions: Create, Update
* Version: 1.4
*/

use \RightNow\Connect\v1_4 as RNCPHP;
 
use \RightNow\CPM\v1 as RNCPM;

// this can be controlled using a custom configuration value 
define('DEBUG', '1');
 
class SimpleIncidentCPM implements RNCPM\ObjectEventHandler {
	
	public static function apply($run_mode, $action, $obj, $n_cycles){ 

		try{
			debug("ID ".$obj->ID.". Starting CPM run.");
			echo "run mode:".$run_mode;
			echo "\n";
			echo "action:".$action;
			echo "\n";
			echo $n_cycles;
		}
		catch(\Exception $e){
			debug("Exception: " . $e->getMessage());
		}
	} 
} 

class SimpleIncidentCPM_TestHarness implements RNCPM\ObjectEventHandler_TestHarness {

	static $inc_invented = NULL;
	
	public static function setup(){
		
		return;
	}
	 
	public static function fetchObject($action, $object_type){
		$incident=$object_type::first('ID > 0');
		echo "Fetched ".$object_type." ".$incident->ID;
		return($incident);
	}
	 
	public static function validate($action, $object){
	
		return true;
	}
	 
	public static function cleanup(){

		return;
	}
}

/*
//Use this to avoid log lines being overwritten with CPM customizations
// (use of global variable will also ensure that each spm_scheduler utility process writes to its own file)
//
$unique_string = strval(rand(9, 9999999));

function debug($string){
	global $unique_string;
	// check if we want to produce logging
	if (DEBUG == '1') {
		$fp = fopen("/tmp/SimpleIncidentCPM_" . $unique_string . "_" . date("Ymd") .".log", "a+");
		fwrite($fp, date("Y-m-d H:i:s") . " " . $string . "\n");
		fclose($fp);
	}
}
*/

function debug($string){
	// check if we want to produce logging
	if (DEBUG == '1') {
		$fp = fopen("/tmp/SimpleIncidentCPM_" . date("Ymd") .".log", "a+");
		fwrite($fp, date("Y-m-d H:i:s") . " " . $string . "\n");
		fclose($fp);
	}
}