<?php

include '../src/openkm/OpenKM.php';

ini_set('display_errors', true);
error_reporting(E_ALL);

use openkm\OKMWebServicesFactory;
use openkm\OpenKM;

class ExampleWorkflow {

    const HOST = "http://localhost:8080/OpenKM/";
    const USER = "okmAdmin";
    const PASSWORD = "admin";

    private $ws;

    public function __construct() {
        $this->ws = OKMWebServicesFactory::build(self::HOST, self::USER, self::PASSWORD);
    }

    public function testRegisterProcessDefinition() {
        try {
            $fileName = dirname(__FILE__) . '/workflow/Purchase.par';
            $this->ws->registerProcessDefinition(file_get_contents($fileName));
            var_dump('Register Definition correct');
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testDeleteProcessDefinition() {
        try {
            $pdId = 91; // Valid workflow process definition
            $this->ws->deleteProcessDefinition($pdId);
            var_dump('Delete Process Definition correct');
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetProcessDefinition() {
        try {
            $pdId = 3; // Valid workflow process definition
            $processDefiniton = $this->ws->getProcessDefinition($pdId);
            var_dump($processDefiniton);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testRunProcessDefinition() {
        try {
            $pdId = 91; // Valid workflow process definition
            // Same modification with only affected FormElement
            $formElements = array();
            $price = new \openkm\bean\form\Input();
            $price->setName("price");
            $price->setValue(1000);
            $formElements[] = $price;

            $textArea = new \openkm\bean\form\TextArea();
            $textArea->setName("description");
            $textArea->setValue("some description here");
            $formElements[] = $textArea;

            $processInstance = $this->ws->runProcessDefinition($pdId, '768748c5-04ab-4921-b0d4-7545d239ce5a', $formElements);
            var_dump($processInstance);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testFindAllProcessDefinitions() {
        try {
            $processDefinitions = $this->ws->findAllProcessDefinitions();
            foreach ($processDefinitions as $processDefinition) {
                var_dump($processDefinition);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testFindProcessInstances() {
        try {
            // Get all workflow definitions
            $processDefinitions = $this->ws->findAllProcessDefinitions();
            foreach ($processDefinitions as $processDefinition) {
                echo '<h2>WF definition</h2>';
                var_dump($processDefinition);
                // Get all process of some workflow definition
                $processInstances = $this->ws->findProcessInstances($processDefinition->getId());
                foreach ($processInstances as $processInstance) {
                    echo '<h2>Process Instance</h2>';
                    var_dump($processInstance);
                }
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testFindLatestProcessDefinitions() {
        try {
            // Get all latest workflow definitions
            $processDefinitions = $this->ws->findLatestProcessDefinitions();
            foreach ($processDefinitions as $processDefinition) {
                var_dump($processDefinition);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testFindLastProcessDefinition() {
        try {
            $processInstance = $this->ws->findLastProcessDefinition("purchase");
            var_dump($processInstance);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetProcessInstance() {
        try {
            $piId = 77; // Some valid process instance id
            $processInstance = $this->ws->getProcessInstance($piId);
            var_dump($processInstance);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testFindUserTaskInstances() {
        try {
            // Get all user task intances
            $taskInstances = $this->ws->findUserTaskInstances();
            foreach ($taskInstances as $taskInstance) {
                var_dump($taskInstance);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testFindTaskInstances() {
        try {
            // Get all task instances of some process instance
            $piId = 77; // Some valid process instance id
            $taskInstances = $this->ws->findTaskInstances($piId);
            foreach ($taskInstances as $taskInstance) {
                var_dump($taskInstance);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testSetTaskInstanceValues() {
        try {
            $tiId = 5; // Some valid task instance id
            $formElements = array();
            $this->ws->setTaskInstanceValues($tiId, 'approve', $formElements);
            echo '<p>Set task Instance value</p>';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetTaskInstance() {
        try {
            $tiId = 4; // Some valid task instance id
            $taskInstance = $this->ws->getTaskInstance($tiId);
            var_dump($taskInstance);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testSetTaskInstanceActorId() {
        try {
            $tiId = 4; // Some valid task instance id            
            $this->ws->setTaskInstanceActorId($tiId, 'okmAdmin');
            echo '<p>Set task Instance actorid</p>';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testStartTaskInstance() {
        try {
            $tiId = 4; // Some valid task instance id            
            $this->ws->startTaskInstance($tiId);
            echo '<p>Start task Instance</p>';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testEndTaskInstance() {
        try {
            $tiId = 6; // Some valid task instance id            
            $this->ws->endTaskInstance($tiId, 'end');
            echo '<p>End task Instance</p>';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetProcessDefinitionForms() {
        try {
            $pdId = 3; // Valid workflow process definition           
            $processDefinitionForms = $this->ws->getProcessDefinitionForms($pdId);
            foreach ($processDefinitionForms as $key => $value) {
                var_dump('key' . $key);
                var_dump($value);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

}

$openkm = new OpenKM(); //autoload
$exampleWorkflow = new ExampleWorkflow();
$exampleWorkflow->testGetProcessDefinitionForms();
?>