public function render ( $data ) {
        ob_start();

        if ( isset($_REQUEST['cache']) ) {
            header('Cache-Control: no-transform,public,max-age=300,s-maxage=900');
            header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
        } else {
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: private",false);
        }
        header('Content-Description: File Transfer');
        header("Content-Type: application/octet-stream");
        header("Content-Transfer-Encoding: binary");
        header('Content-Disposition: attachment; filename="' . $this->filename . '.xls"');

        $objPHPExcel = new PHPExcel();

        $rowNumber = 1;
        if ( is_array($data) ) {
            foreach ( $data as $row ) {
                $col = 'A';
                foreach ( $row as $cell ) {
                    $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
                    $col++;
                }
                $rowNumber++;
            }
        } else {
            LogHelper::log_error('Expecting array of data for export');
            LogHelper::log_error($data);
        }

        // Save as an Excel BIFF (xls) file
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');

        gd_get_session_messages(); // log and clear any messages

        $output = ob_get_clean();

        if ( !isset($_SERVER['HTTP_ACCEPT_ENCODING']) || empty($_SERVER['HTTP_ACCEPT_ENCODING']) ) {
            // the content length may vary if the server is using compression
            header('Content-Length: '.strlen($output));
        }

        return $output;
    }
    public function render ( $data ) {
        ob_start();

        if ( isset($_REQUEST['cache']) ) {
            header('Cache-Control: no-transform,public,max-age=300,s-maxage=900');
            header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
        } else {
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: private",false);
        }
        header('Content-Description: File Transfer');
        header("Content-Type: text/csv");
        header('Content-Disposition: attachment; filename="' . $this->filename . '.csv"');

        $outStream = fopen("php://output", 'w');
        if ( is_array($data) ) {
            foreach ( $data as $item ) {
                if ( is_array($item) ) {
                    fputcsv($outStream, $item, ',', '"');
                } else {
                    LogHelper::log_error('Expecting array of data for export');
                    LogHelper::log_error($item);
                }
            }
        } else {
            LogHelper::log_error('Expecting array of data for export');
            LogHelper::log_error($data);
        }
        fclose($outStream);

        gd_get_session_messages(); // log and clear any messages

        $output = ob_get_clean();

        if ( !isset($_SERVER['HTTP_ACCEPT_ENCODING']) || empty($_SERVER['HTTP_ACCEPT_ENCODING']) ) {
            // the content length may vary if the server is using compression
            header('Content-Length: '.strlen($output));
        }

        return $output;
    }
/**
 * @param $dashboardNode
 * @param $reportNid
 * @return int|null
 */
function gd_dashboard_report_view ( $dashboardNode, $reportNid ) {
    ob_start();
    try {
        if ( !gd_dashboard_access_view($dashboardNode) ) {
            return MENU_ACCESS_DENIED;
        }

        $DashboardConfig = new GD_DashboardConfig($dashboardNode);
        gd_datasource_set_active($DashboardConfig->getDatasource());

        foreach ( $DashboardConfig->items as $item ) {
            if ( $reportNid == $item->content ) {
                $options = array();

                if (isset($_GET['dashboardBuilder']) && $_GET['dashboardBuilder'] == TRUE) {
                    $options['admin'] = TRUE;
                }

                if ( $DashboardConfig->isPublic() && arg(0) == 'public' ) {
                    $options['public'] = TRUE;
                }

                $html = $DashboardConfig->getItemReportView($item, $options);
                $output = array('header'=>$html->header, 'body'=>$html->body, 'footer'=>$html->footer);

                $response = new stdClass();
                $response->status = new stdClass();
                $response->status->code = 200;
                $response->status->message = 'OK';

                $response->data = $output;

                module_invoke_all('gd_ext_response_alter',$response);

                $messages = gd_get_session_messages();
                if ( !empty($messages['error']) ) {
                    $response->status->code = 500;
                    $response->status->message = $messages['error'];
                } else {
                    if ( !isset($_GET['callback']) && $DashboardConfig->isPublic() && arg(0) == 'public' ) {
                        drupal_add_http_header('Cache-Control','no-transform,public,max-age=3600,s-maxage=3600');
                        drupal_add_http_header('Expires',gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
                    }
                }

                echo \GD\Utility\Json::getPayload($response,$_GET['callback']);
            }
        }

        echo ob_get_clean();
        drupal_exit();
    } catch ( Exception $e ) {
        gd_exception_handler($e);
        $response = new stdClass();
        $response->status->code = 500;
        $response->status->message = $e->getMessage();
        echo \GD\Utility\Json::getPayload($response,$_GET['callback']);
        echo ob_get_clean();
        drupal_exit();
    }
}
    public function render ( $data ) {
        if ( isset($_REQUEST['cache']) ) {
            header('Cache-Control: no-transform,public,max-age=300,s-maxage=900');
            header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
        } else {
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: private",false);
        }
        header('Content-Description: File Transfer');
        if (strpos(php_sapi_name(), 'cgi') === false) {
            header('Content-Type: application/force-download');
            header('Content-Type: application/octet-stream', false);
            header('Content-Type: application/download', false);
            header('Content-Type: application/pdf', false);
        } else {
            header('Content-Type: application/pdf');
        }
        header('Content-Disposition: attachment; filename="' . $this->filename . '.pdf"');
        header('Content-Transfer-Encoding: binary');

        $pdf = $this->buildPDF($data);
        $output = $pdf->Output($this->filename.'.pdf','S');

        if ( !isset($_SERVER['HTTP_ACCEPT_ENCODING']) || empty($_SERVER['HTTP_ACCEPT_ENCODING']) ) {
            // the content length may vary if the server is using compression
            header('Content-Length: '.strlen($output));
        }

        gd_get_session_messages(); // log and clear any messages
        return $output;
    }
/**
 * @param $datamartId
 * @param $export
 * @return services_error|stdClass
 * @throws Exception
 */
function gd_sync_resource_sync ( $datasourceName, $export ) {
    try {
        if (is_string($export)) {
            $content = json_decode($export);
        } else {
            $content = $export;
        }

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new Exception('Invalid JSON');
        }

        $importContext = new GD\Sync\Import\ImportContext(array('datasourceName'=>$datasourceName,'operation'=>'update'));
        $importStream = new GD\Sync\Import\ImportStream();
        $importStream->set(null,$content);

        $importController = new \GD\Sync\Import\ImportController();
        $importController->import($importStream,$importContext);

        $apiObject = new stdClass();
        $apiObject->name = $datasourceName;
        $apiObject->messages = gd_get_session_messages();
        return $apiObject;
    } catch ( Exception $e ) {
        return gd_admin_ui_service_exception_handler($e);
    }
}
function gd_js_ext () {
    ob_start();
    header('Content-Type: text/javascript; charset=UTF-8');

    echo '(function(global){ '."\n\n";

    foreach ( \GD\Js\Registry::getInstance()->getVendorFiles() as $file ) {
        echo file_get_contents($file)."\n\n";
    }

    foreach ( \GD\Js\Registry::getInstance()->getFiles() as $file ) {
        echo file_get_contents($file)."\n\n";
    }

    echo 'GD.options.host = "'.GOVDASH_HOST.'";'."\n\n";
    echo 'GD.options.themeList = ["table.css", "filter.css", "highcharts.css", "reportMenu.css", "report.css"];'."\n\n";
    echo 'GD.options.themePath = "' . path_to_theme() . '/css/viewer/";'."\n\n";
    echo 'GD.options.csrf = "' . drupal_get_token('services') . '";'."\n";

    echo "\n\n";
    echo file_get_contents(dirname(__FILE__) . '/js/apps/Ext.js')."\n\n";

    echo 'global.GD_Highcharts = Highcharts;'."\n";
    echo 'global.GD_jQuery = jQuery;'."\n";

    echo "\n";
    echo '})(typeof window === "undefined" ? this : window);'."\n";


    gd_get_session_messages();

    echo ob_get_clean();
    drupal_exit();
}
function gd_health_medic_admin_api_treatment () {
    try {
        $response = 'OK';

        switch ($_SERVER['REQUEST_METHOD']) {
            case 'GET' :
                break;

            case 'POST' :
                gd_health_medic_treatment_apply(json_decode($_POST['affected']), $_POST['treatment']);
                break;

            default:
                throw new Exception('Unsupported request method.');
        }
    } catch ( Exception $e ) {
        drupal_add_http_header('Status', '500 System Error');
        gd_get_session_messages();
        LogHelper::log_error($e);
        $response = $e->getMessage();
    }

    echo json_encode($response);
    drupal_exit();
}