<?php
/**
 * CodeIgniter report view
 *
 * @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'); }

require_once APPPATH.'us_libraries/US_report_view_result.php';

/**
 * A view for ods
 */
class View_ods extends US_report_view_result
{
    /**
     * File prefix for ODS
     *
     * Keep lowercase!
     */
    const FILE_PREFIX_ODS = 'ods';
    
    /**
     * Process the report based on the view mode
     *
     * @param   integer $mode   One of the US_report_view_result::M_... modes
     * @return  mixed   Either void or the filename
     *
     * @see US_report_view_result::M_DIRECT
     * @see US_report_view_result::M_FILE
     */
    protected function process($mode)
    {
        $file_name_content_xml = $this->_create_content_xml();

        $file_name_ods = Report_config::get_report_tmp_name(self::FILE_PREFIX_ODS);
        $this->CI->load->library('US_Ods', '', 'ods');

        $this->CI->ods->open_zip($file_name_ods);
        $this->CI->ods->add_main();
        $this->CI->ods->add_content($file_name_content_xml);
        $this->CI->ods->close();

        if ($mode===US_report_view_result::M_FILE) {
            return $file_name_ods;
        } else {
            Report::set_header($this->viewinfo);
            readfile($file_name_ods);
        }
    }

    /**
     * Create the content.xml file
     *
     * @return  string  The filename
     */
    private function _create_content_xml()
    {
        require_once Report_config::view_file_name('xslt', true);
        $xslt_view_class = Report_config::view_class_name('xslt');
        $xslt = new $xslt_view_class($this->viewinfo, $this->file_name, $this->rephead, $this->designinfo, $this->report_id);
        return $xslt->file();
    }

} // END class View_ods
