<?php

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

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

use openkm\OKMWebServicesFactory;
use openkm\OpenKM;

class ExampleRelation {

    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 testGetRelationTypes() {
        try {
            $relationTypes = $this->ws->getRelationTypes(\openkm\bean\RelationType::PARENT_CHILD);
            foreach ($relationTypes as $relationType) {
                var_dump($relationType);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testAddRelation() {
        try {
            $relationTypes = $this->ws->getRelationTypes(\openkm\bean\RelationType::PARENT_CHILD);
            foreach ($relationTypes as $relationType) {
                // looking for a relation named Invoice of
                if ($relationType->getTitleAToB() == 'Invoice of') {
                    $this->ws->addRelation('/okm:root/SDK4PHP/invoice.pdf', '/okm:root/SDK4PHP/budget.pdf', $relationType->getId());
                    echo 'add Relation';
                }
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testDeleteRelation() {
        try {
            $relations = $this->ws->getRelations('/okm:root/SDK4PHP/invoice.pdf');
            foreach ($relations as $relation) {
                //looking for a relation named Invoice of
                if ($relation->getRelationTitle() == 'Invoice of') {
                    $this->ws->deleteRelation($relation->getId());
                    echo 'delete Relation';
                }
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetRelations() {
        try {
            $relations = $this->ws->getRelations('/okm:root/SDK4PHP/invoice.pdf');
            foreach ($relations as $relation) {
                var_dump($relation);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetRelationGroups() {
        try {
            $relationGroups = $this->ws->getRelationGroups('/okm:root/SDK4PHP/invoice.pdf');
            foreach ($relationGroups as $relationGroup) {
                var_dump($relationGroup);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testAddRelationGroup() {
        try {
            $relationTypes = $this->ws->getRelationTypes(\openkm\bean\RelationType::MANY_TO_MANY);
            foreach ($relationTypes as $relationType) {
                if ($relationType->getTitle() == 'staple') {
                    $this->ws->addRelationGroup('/okm:root/SDK4PHP/invoice.pdf', 'staple group', $relationType->getId());
                    echo 'add Relation Group';
                }
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testAddNodeToGroup() {
        try {
            $relationGroups = $this->ws->getRelationGroups('/okm:root/SDK4PHP/invoice.pdf');
            foreach ($relationGroups as $relationGroup) {
                if ($relationGroup->getName() == 'staple group') {
                    $this->ws->addNodeToGroup('/okm:root/SDK4PHP/complaint.pdf', $relationGroup->getId());
                    echo 'add Node To Group';
                }
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testDeleteRelationGroup() {
        try {
            $relationGroups = $this->ws->getRelationGroups('/okm:root/SDK4PHP/invoice.pdf');
            foreach ($relationGroups as $relationGroup) {
                if ($relationGroup->getName() == 'staple group') {
                    $this->ws->deleteRelationGroup('/okm:root/SDK4PHP/complaint.pdf', $relationGroup->getId());
                    echo 'delete relation group';
                }
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testFindRelationGroup() {
        try {
            $groupId = 2;
            var_dump($this->ws->findRelationGroup($groupId));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testSetRelationGroupName() {
        try {
            $groupId = 2;
            $this->ws->setRelationGroupName($groupId, 'new name');
            echo 'set group name';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

}

$openkm = new OpenKM(); //autoload
$exampleRelation = new ExampleRelation();
$exampleRelation->testGetRelationGroups();
?>