<?php

/**
 * OpenKM, Open Knowledge Management System S.L.  (http://www.openkm.com)
 * Copyright (c) 2006-2023
 *
 * No bytes were intentionally harmed during the development of this application.
 *
 * This program is commercial software; you can use it under the terms of the
 * EULA - OpenKM SDK End User License Agreement as published by OpenKM Knowledge Management System S.L.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * EULA - OpenKM SDK End User License Agreement for more details:
 * http://docs.openkm.com/kcenter/view/licenses/eula-openkm-sdk-end-user-license-agreement.html
 */

namespace openkm\impl;

use openkm\impl\AuthImpl;
use openkm\impl\DocumentImpl;
use openkm\impl\FolderImpl;
use openkm\impl\MailImpl;
use openkm\impl\NoteImpl;
use openkm\impl\PropertyGroupImpl;
use openkm\impl\PropertyImpl;
use openkm\impl\RepositoryImpl;
use openkm\impl\SearchImpl;
use openkm\impl\RecordImpl;
use openkm\impl\RelationImpl;
use openkm\impl\WorkflowImpl;
use openkm\impl\ConversionImpl;
use openkm\impl\TaskImpl;
use openkm\impl\ReportImpl;

/**
 * OKMWebservices
 *
 * @author sochoa
 */
class OKMWebservices {

    private $auth;
    public $document;
    public $folder;
    private $mail;
    public $node;
    public $note;
    public $propertyGroup;
    public $repository;
    public $search;
    private $property;
    private $record;
    private $relation;
    private $workflow;
    private $conversion;
    private $report;
    public $task;
    public $plugin;

    public function __construct($host) {
        $this->auth = new AuthImpl($host);
        $this->document = new DocumentImpl($host);
        $this->folder = new FolderImpl($host);
        $this->mail = new MailImpl($host);
        $this->node = new NodeImpl($host);
        $this->note = new NoteImpl($host);
        $this->propertyGroup = new PropertyGroupImpl($host);
        $this->property = new PropertyImpl($host);
        $this->repository = new RepositoryImpl($host);
        $this->search = new SearchImpl($host);
        $this->record = new RecordImpl($host);
        $this->relation = new RelationImpl($host);
        $this->workflow = new WorkflowImpl($host);
        $this->conversion = new ConversionImpl($host);
        $this->report = new ReportImpl($host);
        $this->task = new TaskImpl($host);
        $this->plugin = new PluginImpl($host);
    }

    public function setAuthorizationToken($authorizationToken) {
        $this->auth->setAuthorizationToken($authorizationToken);
        $this->document->setAuthorizationToken($authorizationToken);
        $this->folder->setAuthorizationToken($authorizationToken);
        $this->mail->setAuthorizationToken($authorizationToken);
        $this->node->setAuthorizationToken($authorizationToken);
        $this->note->setAuthorizationToken($authorizationToken);
        $this->propertyGroup->setAuthorizationToken($authorizationToken);
        $this->property->setAuthorizationToken($authorizationToken);
        $this->repository->setAuthorizationToken($authorizationToken);
        $this->search->setAuthorizationToken($authorizationToken);
        $this->record->setAuthorizationToken($authorizationToken);
        $this->relation->setAuthorizationToken($authorizationToken);
        $this->workflow->setAuthorizationToken($authorizationToken);
        $this->conversion->setAuthorizationToken($authorizationToken);
        $this->report->setAuthorizationToken($authorizationToken);
        $this->task->setAuthorizationToken($authorizationToken);
        $this->plugin->setAuthorizationToken($authorizationToken);
    }

    /**
     * Simulate first login process
     *
     * @throws ConnectionErrorException
     * @throws AccessDeniedException
     * @throws UnknowException
     */
    public function login($username, $password, $expiration = 1, $restrictIp = false) {
        $authorizationToken = $this->auth->login($username, $password, $expiration, $restrictIp);
        $this->setAuthorizationToken($authorizationToken); // Initialize authorization token
        return $authorizationToken;
    }

    public function logout() {
        $this->auth->logout();
        $this->setAuthorizationToken(null); // Reset authorization token
    }
}

?>
