<?php

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

use openkm\OKMWebServicesFactory;
use openkm\OpenKM;

class ExampleConversion {

    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 testDoc2pdf($method) {
        try {
            $fileName = dirname(__FILE__) . '/files/test.docx';
            $content = $this->ws->doc2pdf(file_get_contents($fileName), "test.docx");
            switch ($method) {
                case 1:
                    $file = fopen(dirname(__FILE__) . '/files/out.pdf', 'w+');
                    fwrite($file, $content);
                    fclose($file);
                    echo 'conversion 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: application/pdf');
                    header('Content-Disposition: attachment; filename="out.pdf"');
                    echo $content;
                    break;
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testPdf2swf($method) {
        try {
            $fileName = dirname(__FILE__) . '/files/test.pdf';
            $content = $this->ws->pdf2swf(file_get_contents($fileName), "test.pdf");
            switch ($method) {
                case 1:
                    $file = fopen(dirname(__FILE__) . '/files/out.swf', 'w+');
                    fwrite($file, $content);
                    fclose($file);
                    echo 'conversion 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: application/x-shockwave-flash');
                    header('Content-Disposition: attachment; filename="out.swf"');
                    echo $content;
                    break;
            }
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testImageConvert() {
        try {
            $fileName = dirname(__FILE__) . '/files/test.png';
            $content = $this->ws->imageConvert(file_get_contents($fileName), "test.png", "-resize 50% \${fileIn} \${fileOut}", "image/jpeg");
            $file = fopen(dirname(__FILE__) . '/files/out.jpg', 'w+');
            fwrite($file, $content);
            fclose($file);
            echo 'imvage convert';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testHtml2pdf() {
        try {            
            $content = $this->ws->html2pdf("http://www.openkm.com");
            $file = fopen(dirname(__FILE__) . '/files/outhtml.pdf', 'w+');
            fwrite($file, $content);
            fclose($file);
            echo 'html to pdf correct';
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testDoc2txt() {
        try {
            $fileName = dirname(__FILE__) . '/files/test.docx';
            $content = $this->ws->doc2txt(file_get_contents($fileName), "test.docx");
            echo $content;
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testImg2txt() {
        try {
            $fileName = dirname(__FILE__) . '/files/test.png';
            $content = $this->ws->img2txt(file_get_contents($fileName), "test.png");
            echo $content;
        } catch (Exception $e) {
            var_dump($e);
        }
    }

    public function testBarcode2txt() {
        try {
            $fileName = dirname(__FILE__) . '/files/test.png';
            $content = $this->ws->barcode2txt(file_get_contents($fileName), "test.png");
            echo $content;
        } catch (Exception $e) {
            var_dump($e);
        }
    }

}

$openkm = new OpenKM(); //autoload
$exampleConversion = new ExampleConversion();
$exampleConversion->testHtml2pdf();
?>