<?php

/* 
 * Longitudinal Reports Plugin
 * Luke Stevens, Murdoch Childrens Research Institute https://www.mcri.edu.au
 * Version date 16-Nov-2015 
 */
require_once dirname(__FILE__) . '/config.php';
// Get html report table
list($report_table, $num_results_returned) = LongitudinalReports::doReport($_POST['report_id'], 'report', 'html', false, false, false, false, false, false, false, false, false, false, false, isset($_GET['instruments']) ? explode(',', $_GET['instruments']) : array(), isset($_GET['events']) ? explode(',', $_GET['events']) : array());
// Display report and title and other text
print "<div id='report_div' style='margin:10px 0 20px;'>" . RCView::div(array('style' => ''), RCView::div(array('class' => 'hide_in_print', 'style' => 'float:left;width:350px;'), RCView::div(array('style' => 'font-weight:bold;'), $lang['custom_reports_02'] . RCView::span(array('style' => 'margin-left:5px;color:#800000;font-size:15px;'), User::number_format_user($num_results_returned))) . RCView::div(array('style' => ''), $lang['custom_reports_03'] . RCView::span(array('style' => 'margin-left:5px;'), User::number_format_user(LongitudinalRecords::getRecordCount())) . (!$longitudinal ? "" : RCView::div(array('style' => 'margin-top:3px;color:#888;font-size:11px;font-family:tahoma,arial;'), $lang['custom_reports_09'])))) . RCView::div(array('class' => 'hide_in_print', 'style' => 'float:left;'), ($user_rights['data_export_tool'] == '0' ? '' : RCView::button(array('class' => 'report_btn jqbuttonmed', 'onclick' => "showExportFormatDialog('{$_POST['report_id']}');", 'style' => 'font-size:11px;padding:1px 4px 0px !important;'), RCView::img(array('src' => 'go-down.png', 'style' => 'vertical-align:middle;')) . RCView::span(array('style' => 'vertical-align:middle;'), $lang['custom_reports_12']))) . RCView::SP . RCView::button(array('class' => 'report_btn jqbuttonmed', 'onclick' => "window.print();", 'style' => 'font-size:11px;padding:1px 4px 0px !important;'), RCView::img(array('src' => 'printer.png', 'style' => 'vertical-align:middle;')) . RCView::span(array('style' => 'vertical-align:middle;'), $lang['custom_reports_13'])) . RCView::SP . ($_POST['report_id'] == 'ALL' || $_POST['report_id'] == 'SELECTED' || !$user_rights['reports'] ? '' : RCView::button(array('class' => 'report_btn jqbuttonmed', 'onclick' => "window.location.href = '" . APP_PATH_WEBROOT . LR_PATH_FROM_WEBROOT . "index.php?pid=" . PROJECT_ID . "&report_id={$_POST['report_id']}&addedit=1';", 'style' => 'font-size:11px;padding:1px 4px 0px !important;'), RCView::img(array('src' => 'pencil_small.png', 'style' => 'vertical-align:middle;')) . RCView::span(array('style' => 'vertical-align:middle;'), $lang['custom_reports_14'])))) . RCView::div(array('class' => 'clear'), '')) . RCView::div(array('id' => 'this_report_title', 'style' => 'margin:40px 0 8px;padding:5px 3px;color:#800000;font-size:18px;font-weight:bold;'), LongitudinalReports::getReportNames($_POST['report_id'])) . $report_table . "</div>";
// Export survey fields
$outputSurveyFields = isset($_POST['export_survey_fields']) && $_POST['export_survey_fields'] == 'on';
// De-Identification settings
$hashRecordID = $Proj->table_pk_phi && $deidRights || isset($_POST['deid-hashid']) && $_POST['deid-hashid'] == 'on';
$removeIdentifierFields = $user_rights['data_export_tool'] == '3' || $deidRights || isset($_POST['deid-remove-identifiers']) && $_POST['deid-remove-identifiers'] == 'on';
$removeUnvalidatedTextFields = $deidRights || isset($_POST['deid-remove-text']) && $_POST['deid-remove-text'] == 'on';
$removeNotesFields = $deidRights || isset($_POST['deid-remove-notes']) && $_POST['deid-remove-notes'] == 'on';
$removeDateFields = isset($_POST['deid-dates-remove']) && $_POST['deid-dates-remove'] == 'on';
$dateShiftDates = !$removeDateFields && isset($_POST['deid-dates-shift']) && $_POST['deid-dates-shift'] == 'on';
$dateShiftSurveyTimestamps = !$removeDateFields && isset($_POST['deid-surveytimestamps-shift']) && $_POST['deid-surveytimestamps-shift'] == 'on';
// For de-id rights, make sure dates are either removed or date shifted
if ($deidRights && !$dateShiftDates && !$removeDateFields) {
    $removeDateFields = true;
}
// Export the data for this report
$saveSuccess = LongitudinalReports::doReport($_POST['report_id'], 'export', $outputFormat, false, $archiveFiles, $outputDags, $outputSurveyFields, $removeIdentifierFields, $hashRecordID, $removeUnvalidatedTextFields, $removeNotesFields, $removeDateFields, $dateShiftDates, $dateShiftSurveyTimestamps, isset($_GET['instruments']) ? explode(',', $_GET['instruments']) : array(), isset($_GET['events']) ? explode(',', $_GET['events']) : array());
if ($saveSuccess === false) {
    exit('0');
}
// Parse response to get the doc_id's of the files
$data_edoc_id = $saveSuccess[0];
$syntax_edoc_id = $saveSuccess[1];
// Set language based on export file type
switch ($outputFormat) {
    case "spss":
        $docs_header = $lang['data_export_tool_07'];
        $docs_logo = "spsslogo_small.png";
        $instr = $lang['data_export_tool_08'] . '<br>
				<a href="javascript:;" style="text-decoration:underline;font-size:11px;" onclick=\'$("#spss_detail").toggle("fade");\'>' . $lang['data_export_tool_08b'] . '</a>
				<div style="display:none;border-top:1px solid #aaa;margin-top:5px;padding-top:3px;" id="spss_detail">' . $lang['data_export_tool_08c'] . ' C:\\folder\\otherfolder<br><br>' . $lang['data_export_tool_08d'] . '
					<br><font color=green>FILE HANDLE data1 NAME=\'DATA.CSV\' LRECL=10000.</font><br><br>' . $lang['data_export_tool_08e'] . '<br>