<?php

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

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

use openkm\OKMWebServicesFactory;
use openkm\OpenKM;
use openkm\bean\AppVersion;
use openkm\bean\SqlQueryResults;
use openkm\bean\SqlQueryResultColumns;

class ExampleRepository {

    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 testGetRoolFolder() {
        try {
            $folders = $this->ws->getRootFolder();
            var_dump($folders);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetTrashFolder() {
        try {
            $folders = $this->ws->getTrashFolder();
            var_dump($folders);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetTrashFolderBase() {
        try {
            $folders = $this->ws->getTrashFolderBase();
            var_dump($folders);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetTemplatesFolder() {
        try {
            $folders = $this->ws->getTemplatesFolder();
            var_dump($folders);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetPersonalFolder() {
        try {
            $folders = $this->ws->getPersonalFolder();
            var_dump($folders);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetPersonalFolderBase() {
        try {
            $folders = $this->ws->getPersonalFolderBase();
            var_dump($folders);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetMailFolder() {
        try {
            $folders = $this->ws->getMailFolder();
            var_dump($folders);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetMailFolderBase() {
        try {
            $folders = $this->ws->getMailFolderBase();
            var_dump($folders);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetThesaurusFolder() {
        try {
            $folders = $this->ws->getThesaurusFolder();
            var_dump($folders);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetCategoriesFolder() {
        try {
            $folders = $this->ws->getCategoriesFolder();
            var_dump($folders);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testPurgeTrash() {
        try {
            $this->ws->purgeTrash();
            echo 'correct';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetUpdateMessage() {
        try {
            var_dump($this->ws->getUpdateMessage());
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetRepositoryUuid() {
        try {
            var_dump($this->ws->getRepositoryUuid());
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testHasNode() {
        try {
            echo 'Exists node: ' . $this->ws->hasNode('9ab27c7e-5837-42ff-adbb-7163f3644680');
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetNodePath() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            var_dump($this->ws->repository->getNodePath('a707cd9c-f5a1-48c3-832e-aabe34ff7675'));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetNodeUuid() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            var_dump($this->ws->repository->getNodeUuid('/okm:root/sdk/test.pdf'));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetAppVersion() {
        try {
            $appVersion = $this->ws->getAppVersion();
            var_dump($appVersion);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testCopyAttributes() {
        try {
            $this->ws->copyAttributes('/okm:root/SDK4PHP/invoice.pdf', '/okm:root/SDK4PHP/cloned_invoice.pdf', true, true, true, true, true);
            echo 'Correct';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testExecuteScript() {
        echo '<pre>';
        try {
            $fileName = dirname(__FILE__) . '/files/test.bsh';
            $scriptExecutionResult = new \openkm\bean\ScriptExecutionResult();
            $this->ws->login(self::USER, self::PASSWORD);
            $scriptExecutionResult = $this->ws->repository->executeScript(file_get_contents($fileName));
            var_dump($scriptExecutionResult->getResult());
            var_dump($scriptExecutionResult->getStdout());
            if ($scriptExecutionResult->getStderr() != '') {
                echo "Error happened";
                var_dump($scriptExecutionResult->getStderr());
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testExecuteSqlQuery() {
        echo '<pre>';
        try {
            $fileName = dirname(__FILE__) . '/files/testsql.sql';
            $this->ws->login(self::USER, self::PASSWORD);
            $sqlQueryResults = $this->ws->repository->executeSqlQuery(file_get_contents($fileName));
            foreach ($sqlQueryResults->getResults() as $sqlQueryResultColumns) {
                $columns = $sqlQueryResultColumns->getColumns();
                var_dump('uuid: ' . $columns[0] . ' name: ' . $columns[1]);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testExecuteHqlQuery() {
        echo '<pre>';
        try {
            $fileName = dirname(__FILE__) . '/files/testhql.sql';
            $hqlQueryResults = new \openkm\bean\HqlQueryResults();
            $this->ws->login(self::USER, self::PASSWORD);
            $hqlQueryResults = $this->ws->repository->executeHqlQuery(file_get_contents($fileName));
            foreach ($hqlQueryResults->getResults() as $hqlQueryResultColumns) {
                $columns = $hqlQueryResultColumns->getColumns();
                var_dump('uuid: ' . $columns[0] . ' name: ' . $columns[1]);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetTranslations() {
        try {
            $translations = $this->ws->getTranslations('en-GB', 'frontend');
            foreach ($translations as $translation) {
                var_dump('key: ' . $translation->getKey() . ', with translation: ' . $translation->getValue());
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetConfiguration() {
        try {
            $configuration = $this->ws->getConfiguration('system.ocr');
            var_dump($configuration);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetChangeLog() {
        try {
            $changeLoggedes = $this->ws->getChangeLog("/okm:root/SDK4PHP", date('Y-m-d H:i:s'));
            foreach ($changeLoggedes as $changeLogged) {
                var_dump($changeLogged);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

}

$openkm = new OpenKM(); //autoload
$example = new ExampleRepository();
// $example->testHasNode();
// $example->testGetNodePath();
// $example->testGetNodeUuid();
// $example->testExecuteHqlQuery();
// $example->testExecuteScript();
$example->testExecuteSqlQuery();
?>