<?php

$json = getJSONobj();
$pll = $sll = array();
$i = $j = 0;

while (isset($_POST['p_' . $i])) {
    $pll[] = $json->decode(htmlspecialchars_decode($_POST['p_' . $i]));
    $_POST['p_' . $i] = '';
    $i++;
}

while (isset($_POST['s_' . $j])) {
    $sll[] = $json->decode(htmlspecialchars_decode($_POST['s_' . $j]));
    $_POST['s_' . $j] = '';
    $j++;
}

$_POST = $json->decode(htmlspecialchars_decode($_POST['otherFormData']));

$_POST['position_list'] = $pll;
$_POST['services_list'] = $sll;

if (isset($_REQUEST['record']) && $_REQUEST['record'] != '' && $_REQUEST['cache'] != "fromJava") {
    if (isset($_REQUEST['newWindow']) && $_REQUEST['newWindow'] == '1') {
        $_REQUEST['newWindow'] = 0;
        echo '<style type="text/css">html,body,div,table,p,ul,li,a,form,input,label{margin:0px; padding:0px;}</style>';
        echo "<iframe style='border:none;width:100%;height:100%;' frameborder='no' src='index.php?module=EcmServices&action=previewPDF&to_pdf=1&method=I&record=" . $_GET['record'] . "#toolbar=0&zoom=80%'>Yours browser not accept iframes!</iframe>";
    } else {
        require_once("modules/EcmServices/EcmService.php");
        $focus = new EcmService();

        $method = (isset($_REQUEST['method']) && $_REQUEST['method'] != '') ? $_REQUEST['method'] : 'D';
        $focus->getPDF($_REQUEST['record'], $method);
    }
} elseif ($_REQUEST['cache'] == "fromJava" && $_GET['from'] != "EcmServices") {
    $_SESSION['PDF_ECMSERVICES'] = $_POST;
} elseif ($_GET['from'] == "EcmServices") {
    require_once("modules/EcmServices/EcmService.php");
    $focus = new EcmService();

    if (isset($_SESSION['PDF_ECMSERVICES']['record']) && $_SESSION['PDF_ECMSERVICES']['record'] != '') {
        $focus->retrieve($_SESSION['PDF_ECMSERVICES']['record']);
    }

    if (!$focus->ACLAccess('Save')) {
        ACLController::displayNoAccess(true);

        sugar_cleanup(true);
    }

    foreach ($focus->column_fields as $field) {
        if (isset($_SESSION['PDF_ECMSERVICES'][$field])) {
            $value = $_SESSION['PDF_ECMSERVICES'][$field];
            $focus->$field = $value;
        }
    }

    foreach ($focus->additional_column_fields as $field) {
        if (isset($_SESSION['PDF_ECMSERVICES'][$field])) {
            $value = $_SESSION['PDF_ECMSERVICES'][$field];
            $focus->$field = $value;
        }
    }

    if (isset($_SESSION['PDF_ECMSERVICES']['to_is_vat_free']) && $_SESSION['PDF_ECMSERVICES']['to_is_vat_free'])
        $focus->to_is_vat_free = 1;
    else
        $focus->to_is_vat_free = 0;

    $json = getJSONobj();
    $pl = $_SESSION['PDF_ECMSERVICES']['position_list'];

    $focus->position_list = $pl;
    $focus->document_no = $_SESSION['PDF_ECMSERVICES']['document_no'];
    $focus->ecmpaymentcondition_text = EcmService::getTranslation('EcmPaymentConditions', $focus->ecmpaymentcondition_id, $focus->ecmlanguage);
    $focus->ecmdeliverycondition_text = EcmService::getTranslation('EcmDeliveryConditions', $focus->ecmdeliverycondition_id, $focus->ecmlanguage);

    $focus->getPDF();
}