<?php

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

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

use openkm\OKMWebServicesFactory;
use openkm\OpenKM;

class ExamplePropertyGroup {

    const HOST = "http://192.168.1.11:8888/openkm/";
    const USER = "okmAdmin";
    const PASSWORD = "admin";

    private $ws;

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

    public function testAddPropertyGroup() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $properties = [];
            $properties["okp:consulting.name"] = "new value";
            $properties["okp:consulting.important"] = "true";
            $this->ws->propertyGroup->addPropertyGroup('7e12a9ff-95b2-4945-bf5f-1bb709d89c14', 'okg:consulting', $properties);
            echo 'Add Group';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testRemovePropertyGroup() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $this->ws->propertyGroup->removePropertyGroup('7e12a9ff-95b2-4945-bf5f-1bb709d89c14', 'okg:consulting');
            echo 'Remove Group';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetPropertyGroups() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $propertyGroups = $this->ws->propertyGroup->getPropertyGroups('7e12a9ff-95b2-4945-bf5f-1bb709d89c14');
            foreach ($propertyGroups as $propertyGroup) {
                var_dump($propertyGroup);
                echo '<br>';
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetAllPropertyGroups() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $propertyGroups = $this->ws->propertyGroup->getAllPropertyGroups();
            foreach ($propertyGroups as $propertyGroup) {
                var_dump($propertyGroup);
                echo '<br>';
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetPropertyGroupForm() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $formElements = $this->ws->propertyGroup->getPropertyGroupForm('okg:consulting');
            foreach ($formElements as $formElement) {
                var_dump($formElement);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetPropertyGroupFormByNode() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $formElements = $this->ws->propertyGroup->getPropertyGroupFormByNode('7e12a9ff-95b2-4945-bf5f-1bb709d89c14', 'okg:consulting');
            foreach ($formElements as $formElement) {
                var_dump($formElement);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testSetPropertyGroupProperties() {
        echo '<pre>';
        try {
            $properties = [];
            $properties["okp:consulting.name"] = "update value";
            $properties["okp:consulting.important"] = "false";

            $this->ws->login(self::USER, self::PASSWORD);
            $this->ws->propertyGroup->setPropertyGroupProperties('7e12a9ff-95b2-4945-bf5f-1bb709d89c14', 'okg:consulting', $properties);
            echo 'updated';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testHasPropertyGroup() {
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            echo 'Have metadata group: ' . $this->ws->propertyGroup->hasPropertyGroup('7e12a9ff-95b2-4945-bf5f-1bb709d89c14', 'okg:consulting');
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetPropertyGroupsByVersion() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $propertyGroups = $this->ws->propertyGroup->getPropertyGroupsByVersion('7e12a9ff-95b2-4945-bf5f-1bb709d89c14', '1.3');
            foreach ($propertyGroups as $propertyGroup) {
                var_dump($propertyGroup);
                echo '<br>';
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetRegisteredPropertyGroupDefinition() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            var_dump($this->ws->propertyGroup->getRegisteredPropertyGroupDefinition());
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetPropertyGroupSuggestions() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $suggestions = $this->ws->propertyGroup->getPropertyGroupSuggestions('055b5206-35d0-4351-ba92-c304bbba7ddf', 'okg:technology', 'okp:technology.priority');
            foreach ($suggestions as $suggestion) {
                var_dump($suggestion);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testRegisterPropertyGroupDefinition() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $fileName = dirname(__FILE__) . '/propertygroup/propertyGroups.xml';
            $this->ws->propertyGroup->registerPropertyGroupDefinition(file_get_contents($fileName), 'sdk4php');
            var_dump('Register Definition correct');
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetPropertyGroupProperties() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $properties = $this->ws->propertyGroup->getPropertyGroupProperties('7e12a9ff-95b2-4945-bf5f-1bb709d89c14', 'okg:consulting');
            foreach ($properties as $key => $value) {
                echo 'key: ' . $key . ' value: ' . $value;
                echo '<br>';
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetPropertyGroupPropertiesByVersion() {
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $properties = $this->ws->propertyGroup->getPropertyGroupPropertiesByVersion('7e12a9ff-95b2-4945-bf5f-1bb709d89c14', 'okg:consulting', '1.3');
            foreach ($properties as $key => $value) {
                echo 'key: ' . $key . ' value: ' . $value;
                echo '<br>';
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetPropertyGroupByVersionForm() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $formElements = $this->ws->propertyGroup->getPropertyGroupByVersionForm('7e12a9ff-95b2-4945-bf5f-1bb709d89c14', 'okg:consulting', '1.3');
            foreach ($formElements as $formElement) {
                var_dump($formElement);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetPropertyGroup() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            var_dump($this->ws->propertyGroup->getPropertyGroup('okg:consulting'));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

}

$openkm = new OpenKM(); //autoload
$examplePropertyGroup = new ExamplePropertyGroup();
//$examplePropertyGroup->testAddPropertyGroup();
//$examplePropertyGroup->testRemovePropertyGroup();
//$examplePropertyGroup->testGetPropertyGroups();
//$examplePropertyGroup->testGetAllPropertyGroups();
//$examplePropertyGroup->testGetPropertyGroupForm();
//$examplePropertyGroup->testGetPropertyGroupFormByNode();
//$examplePropertyGroup->testSetPropertyGroupProperties();
//$examplePropertyGroup->testHasPropertyGroup();
//$examplePropertyGroup->testGetPropertyGroupsByVersion();
//$examplePropertyGroup->testGetRegisteredPropertyGroupDefinition();
//$examplePropertyGroup->testGetPropertyGroupSuggestions();
//$examplePropertyGroup->testRegisterPropertyGroupDefinition();
//$examplePropertyGroup->testGetPropertyGroupProperties();
//$examplePropertyGroup->testGetPropertyGroupPropertiesByVersion();
//$examplePropertyGroup->testGetPropertyGroupByVersionForm();
$examplePropertyGroup->testGetPropertyGroup();
?>