<?php

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

use openkm\OKMWebServicesFactory;
use openkm\OpenKM;
use openkm\bean\Plugin;

class ExamplePlugin {

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

    private $ws;

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

    public function testExecutePluginPost() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $params = [];
            $params["param1"] = "value1";
            $params["param2"] = "value2";
            $result = $this->ws->plugin->executePluginPost('com.openkm.plugin.rest.TestRestPlugin', $params, '', Plugin::TYPE_STRING);
            var_dump($result);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testExecutePluginPostReturnFile() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $params = [];
            $params["docId"] = "/okm:root/invoices/00000001_001_of_001.pdf";
            $content = $this->ws->plugin->executePluginPostReturnFile('com.openkm.plugin.rest.TestGetDocumentRestPlugin', $params, '');

            ob_clean();
            header("Cache-Control:  maxage=1");
            header("Pragma: public");
            header("Content-Type: application/pdf");
            header('Content-Disposition: attachment; filename="00000001_001_of_001.pdf"');
            header("Content-Length: " . strlen($content));
            echo $content;

        } catch (Exception $e) {
            var_dump($e);
        }
    }


    public function testExecutePluginGet() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $params = [];
            $params["doc1Id"] = "/okm:root/invoices/00000001_001_of_001.pdf";
            $params["doc2Id"] = "/okm:root/invoices/00000001_001_of_002.pdf";
            $result = $this->ws->plugin->executePluginGet('com.openkm.plugin.rest.TestRestPlugin', $params, Plugin::TYPE_STRING);
            var_dump($result);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testExecutePluginGetReturnFile() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $params = [];
            $params["docId"] = "/okm:root/invoices/00000001_001_of_001.pdf";
            $content = $this->ws->plugin->executePluginGetReturnFile('com.openkm.plugin.rest.TestGetDocumentRestPlugin', $params);

            ob_clean();
            header("Cache-Control:  maxage=1");
            header("Pragma: public");
            header("Content-Type: application/pdf");
            header('Content-Disposition: attachment; filename="00000001_001_of_001.pdf"');
            header("Content-Length: " . strlen($content));
            echo $content;

        } catch (Exception $e) {
            var_dump($e);
        }
    }



}

$openkm = new OpenKM(); //autoload
$examplePlugin = new ExamplePlugin();
//$examplePlugin->testExecutePluginPost();
// $examplePlugin->testExecutePluginGet();
// $examplePlugin->testExecutePluginGetReturnFile();
// $examplePlugin->testExecutePluginPostReturnFile();
?>
