<?php
if ( ! defined('BASEPATH') )
    exit( 'No direct script access allowed' );

class PHPToDoc{

    var $array_var = array();
    var $template = '';
    var $symbol = '#';

    public function __construct(){
    }

    public function addVariable($var){
        array_push($this->array_var,$var);
    }

    public function loadDoc($url){
        $this->template = file_get_contents($url);
    }

    public function addText($array_var,$template){
        if(empty($array_var))
            return;

        foreach ($array_var as $key) {
            //$this->template = str_replace("#nomorperkara#",''.$nomorPerkara.'',$template);
        }
    }

    public function setSymbolSeperator($symbol){
        if(!empty($symbol))
            $this->symbol = $symbol;
    }

    public function replaceVariableByText($variable){
        if(empty($this->template))
            return;
        if(empty($variable))
            return;

        foreach ($variable as $key => $value) {
            $seperator = ''.$this->symbol.$key.$this->symbol.'';
            $this->template = str_replace($seperator,''.$value.'',$this->template);
        }

        //print_r($this->template);
    }

    public function addNumbering(){
        $tag = '\pndec';
        $string = '
        <ol>
         <li>123
             <ol>
              <li>abcd</li>
              <li>defg</li>
              <li>hij</li>
             </ol>
         </li>
         <li>456</li>
         <li>789</li>
         <li>000
             <ol>
              <li>xxxx</li>
              <li>YYYY</li>
              <li>ZZZZ</li>
             </ol>
         </li>
        </ol>';
        $this->template = str_replace('#list#',$string,$this->template);
    }
    public function saveAndDownload($filename){
        header("Content-Type: application/vnd.ms-word");
        header("Expires: 0");
        header("Cache-Control:  must-revalidate, post-check=0, pre-check=0");
        header("Content-disposition: attachment; filename=\"".$filename.".rtf\"");
        echo $this->template;
    }


    public function download($name = null){
            
            $tmp_filename = $this->tmpDir."/".uniqid(true).".rtf";
            
            if(is_null($name)){
                $name = basename($tmp_filename);
            }
            
            //$this->save($tmp_filename);
            
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.$name);
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($tmp_filename));
            ob_clean();
            flush();
            readfile($tmp_filename);
            exit;
        }
}

?>