﻿<?PHP

    //====== custom parameters ==========================
    
    $login = "login";
    $password = "password";
    
    //=========================================
    
	require_once('Common.php');
	require_once('util\http.php');
	
	use XmlSerialization\XmlSerializer;
	use \com\veeam\www\ent\v1_0 as restApi;
	
	$http = new HttpContext('127.0.0.1', 9399);
	$restApiInteraction = new RestApiInteraction($http);
    
	// log in using basic authentification
	$session = $restApiInteraction->LogInByBasicAuthentication($login, $password);
	if ($session->SessionId == null) { echo 'Login failed'; exit; }

	echo 'Logged in successfully, sessionId : ' . $session->SessionId . "\r\n";
    
    $editJobSpec = new restApi\JobEntityType();
    $editJobSpec -> ScheduleConfigured = true;
    $editJobSpec -> ScheduleEnabled = true;
    $editJobSpec -> JobScheduleOptions = new restApi\JobScheduleOptionsInfoType();
    $editJobSpec -> JobScheduleOptions -> OptionsDaily = new restApi\JobScheduleDailyOptionsType();
    $editJobSpec -> JobScheduleOptions -> OptionsDaily -> Kind = "Everyday";
    
    $editJobSpec -> JobScheduleOptions -> OptionsDaily -> Time = new DateTime("2000-01-01 00:00:00.00");
    
    //use querySvc to get job entity
    echo "Querying job entity\r\n";
    $jobQuery = $restApiInteraction -> GetObject(
                    "/api/query?type=Job&format=Entities&pageSize=1&page=1",
                    ClassHelper::CombineWithNamespace("QueryResultType")
                    );
    $editJob = $jobQuery -> Entities -> Jobs -> Job[0];
    
    $editJobLink = LinkHelper::FindLink($editJob->Links->Link, LinkRelations::Edit);
    
    echo "Editing job " . $editJob->UID . "\r\n";
    $editTask = $restApiInteraction->PutObject(
                        $editJobLink->Href,
                        $editJobSpec,
                        ClassHelper::CombineWithNamespace("TaskType")
                        );
                        
    if (!$editTask) { echo 'Failed to edit job'; exit; }
	
	// wait while it's running
	if (strcmp($editTask->State, 'Running') == 0) {
		$editTask = $restApiInteraction->WaitForTaskResult($editTask->Href, 1);
	}
	
	// check if clone task is succeeded
	if (!$editTask->Result->Success) { echo "Editing job failed\r\n\r\n"; var_dump($editTask); exit; }
	
	echo "Job edited successfully. Removing tasks\n";
    $restApiInteraction->DeleteTask($editTask);
    echo "Logout";
    $restApiInteraction->Logout($session);

?>