<?php

require_once 'globals.php';


class RegisterEventServerCommand extends CConsoleCommand
{
	public function run($args)
	{
		//var_dump($args);
		
		echo "\n######################################################################";
		echo "\n##                                                                  ##";
		echo "\n##                    Register EventServer                          ##";
		echo "\n##                                                                  ##";
		echo "\n######################################################################";
		echo "\n";
		echo "\n";
		
		
		
		$name = "event_server_new";
		$description = "event_server_new";
		
		if(!isEmptyOrWhitespace($args[0]))
		{
			$name = $args[0];
			$description = $args[1];
		}
		
		if(!isEmptyOrWhitespace($args[1]))
			$description = $args[1];
		
			
			
		echo "name   = $name\n";
		echo "value =  $description\n";
		
		$es_id = md5(uniqid(rand(), true));
		
		echo "\n unique id = $es_id";
		
		kria_save_secret('event_server_id',  $es_id);
		
		$es_id_loaded = kria_load_secret('event_server_id');
		
		echo "\n unique id loaded = $es_id_loaded";		
				
		$EventServerReg = new EventServerRegistration;
		
		echo "\n set data";
		
		$EventServerReg->name = $name;
		$EventServerReg->description = $description;
		$EventServerReg->code = $es_id_loaded;
		
		$curTime = new DateTime('now'); 
		$curTime->add(new DateInterval('P20Y'));
		
		$EventServerReg->export_expiry_date = $curTime;
		$EventServerReg->epoch = DateTimeEx::now();
		
		if(!$EventServerReg->save())
			echo "\n error registering event server";
		else
			echo "\n event server registered";
		
			echo "\n done";

		
		return 0;
		
	}
}
?>