Example #1
0
function GeneratePDF($module, &$pdf, $pdf_orientation)
{
    global $adb, $list_max_entries_per_page;
    $product_module_list = array('SalesOrder', 'Quotes', 'PurchaseOrder', 'Invoice');
    $idlist = trim($_REQUEST['idstring'], ';');
    $idlist = explode(';', $idlist);
    chmod('storage', 0777);
    $_SESSION['no_of_records'] = count($idlist);
    if ($_REQUEST['ParticularSave'] == 'yes') {
        $document_list = array();
        foreach ($idlist as $id) {
            /* ----------------------------- */
            $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);
            /* ----------------------------- */
            $singlepdf = new Printer();
            $singlepdf->setPrintHeader(false);
            $singlepdf->setPrintFooter(false);
            $singlepdf->SetCompression(true);
            $pdf->setPageFormat($_REQUEST['pdf_format'], $pdf_orientation);
            $default_margin = 10;
            if ($_REQUEST['left'] != '') {
                $pdf->SetLeftMargin($_REQUEST['left']);
            } else {
                $pdf->SetLeftMargin($default_margin);
            }
            if ($_REQUEST['right'] != '') {
                $pdf->SetRightMargin($_REQUEST['right']);
            } else {
                $pdf->SetRightMargin($default_margin);
            }
            $_SESSION['top'] = $_REQUEST['top'];
            /*
            			if ( $_REQUEST['top'] != '' ) {
            				$pdf->SetTopMargin($_REQUEST['top']);
            			}else{
            				$pdf->SetTopMargin( $default_margin );
            			}
            
            			if ( $_REQUEST['bottom'] != '' ) {
            				$pdf->SetAutoPageBreak(true, $_REQUEST['bottom']);
            			}else{
            				$pdf->SetAutoPageBreak( $default_margin );
            			}*/
            TakeContent($singlepdf, $module, $id);
            ## przetworzenie danych z nazwy pliku
            /*if ($module == 'Calendar') {
            		require_once "modules/Calendar/Activity.php";
            		$module = "Activity";
            		} else {
            			require_once "modules/$module/$module.php";
            		}
            		require_once "modules/OSSPdf/OSSPdf.php";
            		require_once "modules/OSSPdf/Print.php";
            		### Zmienne
            		*/
            $focus = new $module();
            ### pobranie danych danego modułu
            if ($module == "Activity") {
                $focus->retrieve_entity_info($id, "Calendar");
            } else {
                $focus->retrieve_entity_info($id, $module);
            }
            $fields = $focus->column_fields;
            $recordModel = Vtiger_Record_Model::getCleanInstance('OSSPdf');
            $_REQUEST['file_name'] = $recordModel->replaceModuleFields($_REQUEST['file_name'], $fields, $module, $id);
            $_REQUEST['file_name'] = $recordModel->replaceRelatedModuleFields($_REQUEST['file_name'], $module, $id, $fields, $site_URL = 0);
            $_REQUEST['file_name'] = str_replace("#dd-mm-yyyy#", date("d-m-Y"), $_REQUEST['file_name']);
            $_REQUEST['file_name'] = str_replace("#mm-dd-yyyy#", date("m-d-Y"), $_REQUEST['file_name']);
            $_REQUEST['file_name'] = str_replace("#yyyy-mm-dd#", date("Y-m-d"), $_REQUEST['file_name']);
            // ## Koniec przetwarzania danych z nazwy pliku
            if (empty($_REQUEST['file_name'])) {
                $filepath = $_REQUEST['file_name'] . '_' . $id . '_' . date("YmdHis") . '.pdf';
            } else {
                $filepath = $_REQUEST['file_name'] . '.pdf';
            }
            $singlepdf->Output($filepath, 'F');
            $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);
        zipFilesAndDownload($document_list, $zipname, $storage_path, $zip);
    } else {
        foreach ($idlist as $id) {
            #############################
            $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);
        }
        ## przetworzenie danych z nazwy pliku
        /*if ($module == 'Calendar') {
        		require_once "modules/Calendar/Activity.php";
        		$module = "Activity";
        		} else {
        			require_once "modules/$module/$module.php";
        		}
        		require_once "modules/OSSPdf/OSSPdf.php";
        		require_once "modules/OSSPdf/Print.php";
        		### Zmienne
        		*/
        $focus = new $module();
        ### pobranie danych danego modułu
        if ($module == "Activity") {
            $focus->retrieve_entity_info($id, "Calendar");
        } else {
            $focus->retrieve_entity_info($id, $module);
        }
        $fields = $focus->column_fields;
        $recordModel = Vtiger_Record_Model::getCleanInstance('OSSPdf');
        $_REQUEST['file_name'] = $recordModel->replaceModuleFields($_REQUEST['file_name'], $fields, $module, $id);
        $_REQUEST['file_name'] = $recordModel->replaceRelatedModuleFields($_REQUEST['file_name'], $module, $id, $fields, $site_URL = 0);
        $_REQUEST['file_name'] = str_replace("#dd-mm-yyyy#", date("d-m-Y"), $_REQUEST['file_name']);
        $_REQUEST['file_name'] = str_replace("#mm-dd-yyyy#", date("m-d-Y"), $_REQUEST['file_name']);
        $_REQUEST['file_name'] = str_replace("#yyyy-mm-dd#", date("Y-m-d"), $_REQUEST['file_name']);
        // ## Koniec przetwarzania danych z nazwy pliku
        if (empty($_REQUEST['file_name'])) {
            $filepath = $_REQUEST['file_name'] . '_' . date("YmdHis") . '_ALL.pdf';
        } else {
            $filepath = $_REQUEST['file_name'] . '_ALL.pdf';
        }
        $onlyGenerate = (int) $_REQUEST['only_generate'];
        $pdf->Output($filepath, 'F');
        chmod('storage', 0777);
        //exit;
        $storage_path = decideFilePath();
        $pelnasciezka = $storage_path . $filepath;
        if ($data['ifsave'] == 'yes') {
            $nr_documentu = CreateDocument($filepath, $data['ifattach'], $id, $module, $docid);
            $nr = $nr_documentu + 1;
            $pelnasciezka = $storage_path . $nr . '_' . $filepath;
        }
        //var_dump($storage_path);
        if ($_REQUEST['return_name'] != "yes") {
            rename($filepath, $pelnasciezka);
            if (0 == $onlyGenerate) {
                $pdf->Output($filepath, 'D');
            } else {
                $path = decideFilePath() . $filepath;
                header("Location: index.php?module=OSSMail&view=compose&pdf_path=" . urldecode($pelnasciezka));
            }
            //echo $pelnasciezka;
        } elseif ($_REQUEST['return_name'] == "yes") {
            //echo $filepath;
            $sciezka = "storage/" . $filepath;
            echo $filepath;
            rename($filepath, $sciezka);
        }
        //} else {
        //  $path = decideFilePath() . $filepath;
        //            header("Location: index.php?module=OSSMail&view=compose&pdf_path=" . urldecode($path));
        //        }
        if ($data['ifattach'] == 'yes') {
            $sql = "INSERT INTO vtiger_senotesrel (`crmid`,`notesid`) VALUES ('{$id}','{$docid}')";
            $wykonaj = $adb->query($sql, true);
        }
    }
}