<?php

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

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

use openkm\OKMWebServicesFactory;
use openkm\OpenKM;

class ExampleFolder {

    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 testCreateFolder() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $folder = $this->ws->folder->createFolder('f93ac8df-e83b-487f-9a14-0af9e910a818', 'test');
            var_dump($folder);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetFolderProperties() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $folder = $this->ws->folder->getFolderProperties("4a3b1c1b-c880-45a3-a6ff-2c8b7c5adfa5");
            var_dump($folder);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testDeleteFolder() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $this->ws->folder->deleteFolder("56c4d33b-daad-4389-bfdc-ee8dd5273899");
            echo 'delete folder';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testRenameFolder() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            // Exists folder /okm:root/sdk/test
            $folder = $this->ws->folder->renameFolder("0b89e4ca-0019-4837-82a4-05bc05d83353", "renamedFolder");
            // Folder has renamed to /okm:root/sdk/renamedFolder
            echo 'rename Folder';
            var_dump($folder);
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testMoveFolder() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            // Exists folder /okm:root/sdk/test
            $this->ws->folder->moveFolder("0b89e4ca-0019-4837-82a4-05bc05d83353", "55102c84-e4f5-4cbc-a3d1-cf1896367752");
            // Folder has moved to /okm:root/move/test
            echo 'move Folder';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetFolderChildren() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $folders = $this->ws->folder->getFolderChildren('f93ac8df-e83b-487f-9a14-0af9e910a818');
            foreach ($folders as $folder) {
                var_dump($folder);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testIsValidFolder() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            // Return false
            echo '/okm:root/sdk/logo.png';
            var_dump($this->ws->folder->isValidFolder('b02877cc-bc71-4be9-9bfb-2e50b1732fd3'));
            // Return true
            echo '/okm:root/sdk/test';
            var_dump($this->ws->folder->isValidFolder('4a3b1c1b-c880-45a3-a6ff-2c8b7c5adfa5'));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetFolderPath() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            // /okm:root/sdk/test
            var_dump($this->ws->folder->getFolderPath('4a3b1c1b-c880-45a3-a6ff-2c8b7c5adfa5'));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testCopyFolder() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $this->ws->folder->copyFolder('4a3b1c1b-c880-45a3-a6ff-2c8b7c5adfa5', '55102c84-e4f5-4cbc-a3d1-cf1896367752', "new_name");
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testExtentedFolderCopy() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $folder = $this->ws->folder->extendedFolderCopy('4a3b1c1b-c880-45a3-a6ff-2c8b7c5adfa5', '55102c84-e4f5-4cbc-a3d1-cf1896367752', 'new name extend copy', true, true, true, true, true);
            var_dump($folder);
        } catch (Exception $e) {
            var_dump($e);
        }
    }
    
    public function testCreateFolderFromTemplate() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            
            $properties = [];
            $properties["okp:consulting.name"] = "update value";
            $properties["okp:consulting.important"] = "false";
            
            $folder = $this->ws->folder->createFolderFromTemplate('47c70047-2924-483c-bc42-23d0cbef6b17', '/okm:root/sdk/foldertemplate', 'es', true, true, true, true, $properties);
            var_dump($folder);
        } catch (Exception $e) {
            var_dump($e);
        }
    }    

    public function testGetContentInfo() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            var_dump($this->ws->folder->getContentInfo('4a3b1c1b-c880-45a3-a6ff-2c8b7c5adfa5'));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testPurgeFolder() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $this->ws->folder->purgeFolder('d1faccb3-9e64-4d2d-865f-1c057e3a279b');
            echo 'purge folder';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testSetStyle() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $this->ws->folder->setStyle('0b89e4ca-0019-4837-82a4-05bc05d83353', 1);
            echo 'set style';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testCreateMissingFolders() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $this->ws->folder->createMissingFolders("/okm:root/sdk/missingfld2/missingfld3");
            var_dump('create missing folders');
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testSetFolderDescription() {
        echo '<pre>';
        try {
            $this->ws->login(self::USER, self::PASSWORD);
            $this->ws->folder->setFolderDescription('4a3b1c1b-c880-45a3-a6ff-2c8b7c5adfa5', 'update description');
            var_dump('set description folder');
        } catch (Exception $e) {
            var_dump($e);
        }
    }

}

$openkm = new OpenKM(); //autoload
$exampleFolder = new ExampleFolder();
// $exampleFolder->testCreateFolder();
// $exampleFolder->testGetFolderProperties();
// $exampleFolder->testDeleteFolder();
// $exampleFolder->testRenameFolder();
// $exampleFolder->testMoveFolder();
// $exampleFolder->testGetFolderChildren();
// $exampleFolder->testIsValidFolder();
// $exampleFolder->testGetFolderPath();
// $exampleFolder->testCopyFolder();
// $exampleFolder->testExtentedFolderCopy();
$exampleFolder->testCreateFolderFromTemplate();
// $exampleFolder->testGetContentInfo();
// $exampleFolder->testPurgeFolder();
// $exampleFolder->testSetStyle();
// $exampleFolder->testCreateMissingFolders();
// $exampleFolder->testSetFolderDescription();
?>