<?php
/**
 * CodeIgniter library
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Koller
 * @copyright 2009 untermStrich
 * @package views
 * @version $Id$
*/

if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

/**
 * PHP built-in xslt processer
 */
class Xslt_builtin
{
    /**
     * The main CI object
     */
    protected $CI;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->CI = CI_Controller::get_instance();
    }
    
    /**
     * Is this process useable
     *
     * @param   integer $mode       One of the My_controller::m_... modes
     * @param   integer $file_size  File size
     * @return  boolean
     */
    public function is_useable($mode, $file_size)
    {
        return true; //I'm the fallback - so always use me :-)
    }

    /**
     * Process the xslt
     *
     * @param   integer             $mode           One of the US_report_view_result::M_... modes
     * @param   SimpleXMLElement    $viewinfo       The view info
     * @param   string              $file_name_xml  File path to report XML
     * @param   string              $file_name_xsl  File path to the XSLT file
     * @param   SimpleXMLElement    $rephead        The head of the report XML
     * @param   array               $designinfo     The design settings
     * @return  mixed   Either void or the filename
     *
     * @see US_report_view_result::M_DIRECT
     * @see US_report_view_result::M_FILE
     */
    public function process(
        $mode,
        SimpleXMLElement $viewinfo,
        $file_name_xml,
        $file_name_xsl,
        SimpleXMLElement $rephead,
        array $designinfo
    ) {
        
        $xml = new DOMDocument('1.0', 'UTF-8');
        $xml->load($file_name_xml);

        $xsl = new DOMDocument('1.0', 'UTF-8');
        $xsl->load($file_name_xsl);

        libxml_use_internal_errors(true);
        $proc = new XSLTProcessor;
        foreach ($designinfo as $key => $value) {
            $proc->setParameter('', $key, us_xml_clean($value));
        }
        $proc->importStyleSheet($xsl);

        $errors = libxml_get_errors();
        if (!empty($errors)) {
            xml_error_basic_msg('Error loading xsl', $errors, __FUNCTION__);
        }
        
        if ($mode===US_report_view_result::M_FILE) {
            $temp_file = Report_config::get_report_tmp_name(US_Reports::FILE_PREFIX_REPORT_TEMP);
            if (substr($temp_file, 0, 1)!=='/') {
                $temp_uri = 'file:///'.$temp_file; //Bugfix for windows
            } else {
                $temp_uri = 'file://'.$temp_file;
            }
            $proc->transformToURI($xml, $temp_uri);
        } else {
            Report::set_header($viewinfo);
            echo $proc->transformToXML($xml);
        }

        $errors = libxml_get_errors();
        if (!empty($errors)) {
            xml_error_basic_msg('Error transforming xml using PHP', $errors, __FUNCTION__);
        }
        
        unset($proc);
        libxml_clear_errors();

        if ($mode===US_report_view_result::M_FILE) {
            return $temp_file;
        }
        
    }

} // END class Xslt_builtin
