<?php

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

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

use openkm\OKMWebServicesFactory;
use openkm\OpenKM;

class ExampleReport {

    const HOST = "http://localhost:8080O/penKM/";
    const USER = "okmAdmin";
    const PASSWORD = "admin";

    private $ws;

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

    public function testGetReports() {
        try {
            $reports = $this->ws->getReports(true);
            foreach ($reports as $report) {
                var_dump($report);
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testGetReport() {
        try {
            var_dump($this->ws->getReport(2));
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testExecuteReport($method) {
        $params = [];
        $params['from_date'] = '2017-01-01';
        $params['to_date'] = '2017-06-19';
        $format = \openkm\bean\Report::FORMAT_PDF;
        $content = $this->ws->executeReport(2, $params, $format);
        switch ($method) {
            case 1:
                $file = fopen(dirname(__FILE__) . '/files/report.pdf', 'w+');
                fwrite($file, $content);
                fclose($file);
                echo 'download correct';
                break;
            case 2:                
                header('Expires', 'Sat, 6 May 1971 12:00:00 GMT');
                header('Cache-Control', 'max-age=0, must-revalidate');
                header('Cache-Control', 'post-check=0, pre-check=0');
                header('Pragma', 'no-cache');
                header('Content-Type: ' . $format);
                header('Content-Disposition: attachment; filename="out.pdf"');
                echo $content;
                break;
        }
    }

}

$openkm = new OpenKM(); //autoload
$exampleReport = new ExampleReport();
$exampleReport->testExecuteReport(1);
?>