Пример #1
0
function Soap_generatePDF($userid)
{
    $adb = PearDatabase::getInstance();
    $current_user = vglobal('current_user');
    $_SESSION['type'] = "single";
    $user = new Users();
    $current_user = $user->retrieveCurrentUserInfoFromFile($userid);
    require_once "modules/OSSPdf/Print.php";
    require_once 'modules/OSSPdf/ModulesQueries.php';
    $module = $_REQUEST['usingmodule'];
    $id = $_REQUEST['recordid'];
    if (isset($_REQUEST['fromdetailview']) && $_REQUEST['fromdetailview'] == 'yes') {
        $document_list = array();
        if ($_REQUEST['return_name'] == "yes" || isset($_REQUEST['pdfajax'])) {
            $_REQUEST['template'] = explode(';', trim($_REQUEST['template'], ';'));
        }
        /* ----------------------------- */
        ##############
        ### PRZETWANIA ZMIENNYCH POCZATKOWYCH
        foreach ($_REQUEST['template'] as $templateid) {
            $_SESSION['template_to_perfom'] = $_REQUEST['template_to_perfom'] = $templateid;
            $pobierzdane = $adb->query("select osspdf_pdf_format,osspdf_pdf_orientation, filename, left_margin, right_margin, top_margin, bottom_margin from vtiger_osspdf where osspdfid = '{$templateid}'", true);
            $_REQUEST['pdf_format'] = $adb->query_result($pobierzdane, 0, "osspdf_pdf_format");
            $pdf_orientation_result = $adb->query_result($pobierzdane, 0, "osspdf_pdf_orientation");
            $_REQUEST['file_name'] = $adb->query_result($pobierzdane, 0, "filename");
            $_REQUEST['left'] = $adb->query_result($pobierzdane, 0, "left_margin");
            $_REQUEST['right'] = $adb->query_result($pobierzdane, 0, "right_margin");
            $_REQUEST['top'] = $adb->query_result($pobierzdane, 0, "top_margin");
            $_REQUEST['bottom'] = $adb->query_result($pobierzdane, 0, "bottom_margin");
            $_SESSION['top'] = $_REQUEST['top'];
            if ($pdf_orientation_result == 'Portrait') {
                $pdf_orientation = "P";
            } elseif ($pdf_orientation_result == 'Landscape') {
                $pdf_orientation = "L";
            }
            /* ----------------------------- */
            ##############
            ### INICJOWANIE PDFA, POBIERANIE DANYCH ETC
            $pdf = new Printer();
            $pdf->setPageFormat($_REQUEST['pdf_format'], $pdf_orientation);
            //$pdf->setPrintHeader(false);
            //$pdf->setPrintFooter(false);
            //	$pdf->SetHeaderData( '','','asd','' );
            $pdf->SetCompression(true);
            //$pdf->SetMargins( $left,$top, $right = -1,$keepmargins = false );
            if (isset($_REQUEST['left']) && $_REQUEST['left'] != '' && $_REQUEST['left'] != 0) {
                $pdf->SetLeftMargin($_REQUEST['left']);
            }
            if (isset($_REQUEST['right']) && $_REQUEST['right'] != '' && $_REQUEST['right'] != 0) {
                $pdf->SetRightMargin($_REQUEST['right']);
            }
            /*
                        if (isset($_REQUEST['top']) && $_REQUEST['top'] != '' && $_REQUEST['top'] != 0) {
                            $pdf->SetTopMargin($_REQUEST['top']);
                        }
            
                        if (isset($_REQUEST['bottom']) && $_REQUEST['bottom'] != '' && $_REQUEST['bottom'] != 0) {
                            $pdf->SetAutoPageBreak(true, $_REQUEST['bottom']);
                        }*/
            /* ----------------------------- */
            ################
            $date_var = $adb->formatDate(date('Y-m-d H:i:s'), true);
            $query = "insert into vtiger_audit_trial values(?,?,?,?,?,?)";
            $qparams = array($adb->getUniqueID('vtiger_audit_trial'), $current_user->id, $module, 'Generate PDF', $id, $date_var);
            $adb->pquery($query, $qparams, true);
            TakeContent($pdf, $module, $id, $site_URL);
            $filepath = $_REQUEST['file_name'] . '_' . $id . $templateid . '_' . date("YmdHis") . '.pdf';
            $pdf->Output($filepath, 'F');
            ###
            $pobierz = $adb->query("select * from vtiger_osspdf_config where conf_id = 'GENERALCONFIGURATION'", true);
            ###
            $data = array();
            for ($i = 0; $i < $adb->num_rows($pobierz); $i++) {
                $data[$adb->query_result($pobierz, $i, "name")] = $adb->query_result($pobierz, $i, "value");
            }
            $docid = 0;
            if ($data['ifsave'] == 'yes') {
                $document_id = CreateDocument($filepath, $data['ifattach'], $id, $module, $docid);
                $nr = $document_id + 1;
                $document_list[] = $nr . '_' . $filepath;
                $storage_path = decideFilePath();
                $pelnasciezka = $storage_path . $nr . '_' . $filepath;
            } else {
                $document_list[] = $filepath;
                $storage_path = decideFilePath();
                $pelnasciezka = $storage_path . $filepath;
            }
            chmod('storage', 0777);
            if ($_REQUEST['return_name'] != "yes" || $_REQUEST['return_name'] == "") {
                rename($filepath, $pelnasciezka);
            } else {
                $sciezka = "storage/" . $filepath;
                rename($filepath, $sciezka);
            }
            if ($data['ifattach'] == 'yes') {
                $sql = "INSERT INTO vtiger_senotesrel (`crmid`,`notesid`) VALUES ('{$id}','{$docid}')";
                $wykonaj = $adb->query($sql, true);
            }
        }
        if ($_REQUEST['return_name'] != "yes" || $_REQUEST['return_name'] == "") {
            $storage_path = decideFilePath();
        } else {
            $storage_path = "storage/";
        }
        $zip = getTranslatedString($_REQUEST['usingmodule']) . '_' . date("YmdHis") . '.zip';
        $zipname = 'storage/' . $zip;
        $zipname = file_exist_fn($zipname, 0);
        if (count($document_list) > 1) {
            zipFilesAndDownload($document_list, $zipname, $storage_path, $zip);
        } else {
            if ($_REQUEST['return_name'] != "yes") {
                header("Content-type: application/pdf");
                header("Content-Disposition: attachment; filename=" . $document_list[0]);
                header("Pragma: no-cache");
                header("Expires: 0");
                readfile($storage_path . "/" . $document_list[0]);
                exit;
            } elseif ($_REQUEST['return_name'] == "yes") {
                return $document_list[0];
            }
        }
    }
}