<?php

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

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

use openkm\OKMWebServicesFactory;
use openkm\OpenKM;
use openkm\bean\Record;

class ExampleRecord {

    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 testCreateRecord() {
        try {
            $record = new Record();
            $record->setPath("/okm:root/SDK4PHP/PKI-100200");
            $record->setTitle("some title");
            $this->ws->createRecord($record);
            var_dump($record);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetRecordProperties() {
        try {
            $record = $this->ws->getRecordProperties("/okm:root/SDK4PHP/PKI-100200");
            var_dump($record);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testDeleteRecord() {
        try {
            $this->ws->deleteRecord("/okm:root/SDK4PHP/PKI-100200");
            echo 'Deleted';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testPurgeRecord() {
        try {
            $this->ws->purgeRecord("/okm:trash/okmAdmin/PKI-100200");
            echo 'Purge';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testRenameRecord() {
        try {
            $record = $this->ws->renameRecord("/okm:root/SDK4PHP/PKI-100200", 'new_name');
            var_dump($record);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testMoveRecord() {
        try {
            $this->ws->moveRecord("/okm:root/SDK4PHP/PKI-100200", "/okm:root/SDK4PHP/tmp");
            echo 'Move';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testcopyRecord() {
        try {
            $this->ws->copyRecord("/okm:root/SDK4PHP/PKI-100200", "/okm:root/SDK4PHP/tmp", 'new_name');
            echo 'Copy';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testIsValidRecord() {
        try {
            echo "Is a record:" . $this->ws->isValidRecord("/okm:root/SDK4PHP/PKI-100200");
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetRecordChildren() {
        try {
            $records = $this->ws->getRecordChildren('/okm:root/SDK4PHP');
            foreach ($records as $record) {
                var_dump($record);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testLockRecord() {
        try {
            $lockinfo = $this->ws->lockRecord('/okm:root/SDK4PHP/PKI-100200');
            var_dump($lockinfo);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testUnlockRecord() {
        try {
            $this->ws->unlockRecord("/okm:root/SDK4PHP/PKI-100200");
            echo 'unlock';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testForceUnlockRecord() {
        try {
            $this->ws->forceUnlockRecord("/okm:root/SDK4PHP/PKI-100200");
            echo 'forceUnlock';
        } catch (Exception $e) {
            var_dump($e);
        }
    }
    
    public function testSetRecordTitle() {
        try {
            $this->ws->setRecordTitle("/okm:root/SDK4PHP/PKI-100200",'some title');
            echo 'setTitle';
        } catch (Exception $e) {
            var_dump($e);
        }
    }
    
    public function testGetRecordPath() {
        try {
            var_dump($this->ws->getRecordPath("8e03c11a-fd67-4ba0-8924-180ce538e586"));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

}

$openkm = new OpenKM(); //autoload
$exampleRecord = new ExampleRecord();
$exampleRecord->testIsValidRecord();
?>