<?php

/**
 * i-doit Report Manager.
 *
 * @package     i-doit
 * @subpackage  Reports
 * @author      Dennis Bluemer <dbluemer@synetics.de>
 * @copyright   synetics GmbH
 * @license     http://www.i-doit.com/license
 */
class isys_report_xml extends isys_report
{
    /**
     * Content-type getter.
     *
     * @return  string
     */
    public function getContentType()
    {
        return 'text/xml';
    }

    /**
     * This method will export the report to the desired format.
     *
     * @throws  Exception
     */
    public function export()
    {
        try {
            if (self::$m_as_download) {
                $this->output($this->toString());
            } else {
                $this->set_export_output($this->toString());
            }
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    /**
     * Transfers the export string to the client with the given content-type information.
     *
     * @param string $p_data
     */
    protected function output($p_data)
    {
        $l_title = isys_helper_upload::prepare_filename(html_entity_decode($this->getTitle()));

        header("Content-Type: application/xml");
        header("Expires: " . date("D, d M Y H:i:s") . " GMT");
        header("Content-Disposition: attachment; filename=" . date("ymd") . "-idoit-report-" . $l_title . ".xml");
        header("Pragma: no-cache");

        echo $p_data;
        die;
    }

    /**
     * Returns the report as an xml-String.
     *
     * @return String The report query result in xml format
     * @throws Exception
     */
    protected function toString()
    {
        $l_report = $this->query(true, false);

        $headers = $l_report['headers'];
        $content = isys_report::reformatResult($l_report, $this->shouldShowHtml(), $this->isCompressedMultivalueResults());
        $language = isys_application::instance()->container->get('language');

        $l_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
        $l_xml .= "<report>\n" .
            "   <title>\n" .
            "      " . html_entity_decode($this->getTitle()) . "\n" .
            "   </title>\n" .
            "   <description>\n" .
            "      " . $this->getDescription() . "\n" .
            "   </description>\n";

        if (is_array($headers)) {
            $l_xml .= "   <headers>\n";

            foreach ($headers as $l_value) {
                $l_xml .= "      <header>\n";
                $l_xml .= "         " . $l_value . "\n";
                $l_xml .= "      </header>\n";
            }

            $l_xml .= "   </headers>\n";
        } else {
            throw new Exception("Error processing report headers");
        }

        if (is_array($content)) {
            $l_xml .= "   <content>\n\n";

            foreach ($content as $l_data) {
                $l_xml .= "      <row>\n";

                if (is_array($l_data)) {
                    foreach ($l_data as $l_table => $l_value) {
                        // Replace <script>-Tags with content generated by QuickInfo-Tooltips.
                        $l_value = preg_replace('/<script[^>]*>[^<]*<[^>]script>/  ', '', $l_value);

                        // Remove other tags
                        // ID-10016 Remove whitespaces.
                        $l_value = trim(strip_tags(str_replace(['"', '&nbsp;'], ['\'', ' '], $l_value)));

                        if (!preg_match("/^__[\w]+__$/i", $l_table)) {
                            $l_xml .= "         <element>\n";
                            $l_xml .= "            <![CDATA[" . $language->get($l_value) . "]]>\n";
                            $l_xml .= "         </element>\n\n";
                        }
                    }
                }

                $l_xml .= "      </row>\n\n";
            }

            $l_xml .= "   </content>\n";
        }

        $l_xml .= "</report>";

        return $l_xml;
    }
}
