/**
 * @param $reportId
 * @return array
 * @throws Exception
 * @throws IllegalArgumentException
 */
function gd_report_admin_page_edit ( $reportId ) {
    $reportNode = gd_report_load($reportId);
    if ( !$reportNode ) {
        return MENU_NOT_FOUND;
    }

    gd_datasource_set_active(get_node_field_value($reportNode,'field_report_datasource'));

    if ( !gd_account_user_is_admin() && !gd_account_user_is_datasource_admin(null,gd_datasource_get_active()) ) {
        return MENU_ACCESS_DENIED;
    }

    drupal_add_library('gd_report_admin', 'GD_Admin_ReportSection_Builder');

    $options = array('fields' => array('datasource', 'config', 'filters', 'data', 'customview', 'tags'));
    $report = gd_report_create_api_object_from_node($reportNode,$options);

    $reportDataset = gd_data_controller_ui_metadata_get_dataset_ui_metadata($report->config->model->datasets[0],array_slice($report->config->model->datasets,1));

    drupal_add_http_header('Cache-Control','no-cache, max-age=0, must-revalidate, no-store');

    return gd_report_admin_page($report,$reportDataset);
}
/**
 * @param $dashboardNode
 * @return string
 */
function gd_dashboard_page_public_report_export ( $dashboardNode ) {
    try {
        if ( !gd_dashboard_access_view($dashboardNode) ) {
            return MENU_ACCESS_DENIED;
        }

        $config = variable_get('gd_report_config', array('export' => 0, 'print' => 0));
        $type = $_REQUEST['type'] . (isset($_REQUEST['raw']) ? '_raw' : '');
        $reportNid = $_REQUEST['report'];
        $viewFormat = null;

        if ($config['export'] == 0 || !isset($config[$type]) || $config[$type] == 0) {
            return MENU_ACCESS_DENIED;
        }

        $reportNode = gd_report_load($reportNid);
        if (!$reportNode) {
            return MENU_NOT_FOUND;
        }

        $ReportConfig = GD_ReportConfigFactory::getInstance()->getConfig($reportNode);

        // apply dashboard config, which is mainly filters
        $DashboardConfig = new GD_DashboardConfig($dashboardNode, $_REQUEST);
        $DashboardConfig->updateReportConfig($ReportConfig);

        switch ( $type ) {
            case 'xls':
            case 'xls_raw':
                $data = $ReportConfig->getExport(isset($_REQUEST['raw']));
                $viewFormat = new GD_ServicesViewFormat_Excel('public', $reportNid);
                break;

            case 'csv':
            case 'csv_raw':
                $data = $ReportConfig->getExport(isset($_REQUEST['raw']));
                $viewFormat = new GD_ServicesViewFormat_CSV('public', $reportNid);
                break;

            case 'pdf':
                $data = gd_report_get_export_table($ReportConfig, isset($_REQUEST['raw']));
                $viewFormat = new GD_ServicesViewFormat_PDF('public', $reportNid);
                break;

            default:
                $data = $ReportConfig->getExport(isset($_REQUEST['raw']));
                $viewFormat = new GD_ServicesViewFormat_CSV('public', $reportNid);
                break;
        }

        print $viewFormat->render($data);
        drupal_exit();
    } catch ( Exception $e ) {
        gd_exception_handler($e);
        drupal_set_message('An unexpected Error has occurred. Please contact your Site Administrator.', 'error');
        return ' ';
    }
}