private function getProductCatalogVariables($productCatalog)
 {
     $rootCategoriesSmarty = $this->getSmartyCategories($productCatalog->getAllCategories());
     $this->insertProducts($rootCategoriesSmarty);
     $description = htmlToLatex(from_html($productCatalog->description));
     //1.7.6 TODO create error handling here
     if ($description == null) {
         $GLOBALS['log']->error('OQC: Product catalog description is null!');
         //return null;
     }
     global $timedate;
     $validfrom = $timedate->to_display_date($productCatalog->validfrom);
     $validto = $timedate->to_display_date($productCatalog->validto);
     $frontpage = null;
     $attachment = null;
     if ($productCatalog->frontpage_id != null || $productCatalog->attachment_id != null) {
         $doc = new Document();
         if ($doc->retrieve($productCatalog->frontpage_id)) {
             $frontpage = str_replace("\\", '/', TMP_DIR . DIRECTORY_SEPARATOR . $doc->document_revision_id . '.pdf');
             copy(getcwd() . DIRECTORY_SEPARATOR . getDocumentFilename($doc->document_revision_id), $frontpage);
         }
         if ($doc->retrieve($productCatalog->attachment_id)) {
             $attachment = str_replace("\\", '/', TMP_DIR . DIRECTORY_SEPARATOR . $doc->document_revision_id . '.pdf');
             copy(getcwd() . DIRECTORY_SEPARATOR . getDocumentFilename($doc->document_revision_id), $attachment);
         }
     }
     //ProductCatalog currency setup
     $currencyArray = array();
     $currency = new Currency();
     $currency_id = $currency->retrieve_id_by_name($productCatalog->currency_id);
     //$GLOBALS['log']->error('Contract variables: currency: '. var_export($currency_id,true));
     if ($currency_id) {
         $currency->retrieve($currency_id);
         $currencyArray['currency_id'] = $currency->iso4217;
         $currencyArray['currency_symbol'] = $currency->symbol;
         $currencyArray['currency_ratio'] = $currency->conversion_rate;
     } else {
         $currencyArray['currency_id'] = $currency->getDefaultISO4217();
         $currencyArray['currency_symbol'] = $currency->getDefaultCurrencySymbol();
         $currencyArray['currency_ratio'] = 1.0;
     }
     $currencyArray['currency_symbol'] = str_replace("€", '\\euro{}', $currencyArray['currency_symbol']);
     $currencyArray['currency_symbol'] = str_replace('$', '\\$', $currencyArray['currency_symbol']);
     $currencyArray['currency_symbol'] = str_replace("£", '{\\pounds}', $currencyArray['currency_symbol']);
     $currencyArray['currency_symbol'] = str_replace("¥", '{Y\\hspace*{-1.4ex}--}', $currencyArray['currency_symbol']);
     $productCatalogVariables = array('name' => $productCatalog->name, 'validfrom' => $validfrom, 'validto' => $validto, 'graphicsDir' => LATEX_GRAPHICS_DIR, 'categoriesAndProducts' => $rootCategoriesSmarty, 'description' => $description, 'frontpage' => $frontpage, 'attachment' => $attachment, 'year' => date('Y'), 'currency' => $currencyArray, 'discount' => 1.0 - $productCatalog->oqc_catalog_discount / 100);
     //$GLOBALS['log']->error('Product Catalog variables: '. var_export($productCatalogVariables,true));
     return $productCatalogVariables;
 }
function createAttachedDocumentsPdf($product)
{
    $attachedDocuments = $product->get_all_linked_attachments();
    //	trigger_error("Started to convert products to PDF!", E_USER_NOTICE);
    if (empty($attachedDocuments)) {
        return null;
    }
    $docHandler = null;
    $catArgs = array();
    $filenameArgs = array();
    $filenames = array();
    $attachArgs = array();
    $ids = range('A', chr(ord('A') + count($attachedDocuments) - 1));
    foreach ($attachedDocuments as $attachment_id) {
        $revision_id = $attachment_id;
        $revision_check = new DocumentRevision();
        if (!$revision_check->retrieve($attachment_id)) {
            $document = new Document();
            if ($document->retrieve($attachment_id)) {
                $revision_id = $document->document_revision_id;
                $purpose = $document->document_purpose_c;
            }
        } else {
            $document = new Document();
            if ($document->retrieve($revision_check->document_id)) {
                $purpose = $document->document_purpose_c;
            }
        }
        if ($purpose == 'Catalog') {
            //1.7.6 include only documents marked For Product Catalog
            $revision = new DocumentRevision();
            if ($revision->retrieve($revision_id)) {
                if ($revision->file_ext != 'pdf') {
                    //SugarCE determines file type unreliably, so only extension is used
                    if (!$docHandler) {
                        $docHandler = new DocumentHandler();
                    }
                    $filename = getDocumentFilename($revision_id);
                    // skuria failo varda su nuoroda
                    // we need a descriptive filename for word macros and pdf attachments
                    $realFilenameWithExtension = getRealFilename($revision);
                    copy($filename, $realFilenameWithExtension);
                    $filenames[] = $realFilenameWithExtension;
                    $attachmentFilename = $docHandler->convertToPdf($realFilenameWithExtension);
                    if ($attachmentFilename) {
                        $catArgs[] = current($ids);
                        $filenameArgs[] = current($ids) . '=' . $attachmentFilename;
                        $filenames[] = $attachmentFilename;
                        next($ids);
                    } else {
                        // file was not possible to convert
                        $attachArgs[] = $realFilenameWithExtension;
                        $GLOBALS['log']->fatal('OQC DocumentHandler: file ' . $realFilenameWithExtension . ' is not possible to convert to pdf!');
                        continue;
                    }
                } else {
                    $attachmentFilename = getDocumentFilename($revision_id);
                    $catArgs[] = current($ids);
                    $filenameArgs[] = current($ids) . '=' . $attachmentFilename;
                    next($ids);
                }
            }
        }
    }
    $testPdfFiles = implode('', $catArgs);
    //test if there are any pdf file available for pdftk
    //$GLOBALS['log']->error('OQC pdf testfiles'.$testPdfFiles);
    $testAttachFiles = implode('', $attachArgs);
    //test if there are attachement files
    if (empty($testPdfFiles)) {
        foreach ($filenames as $attachfile) {
            unlink($attachfile);
        }
        return null;
    }
    $outputFilename = tempnam(TMP_DIR, '');
    $finalFilename = tempnam(TMP_DIR, '');
    $contractFilename = tempnam(TMP_DIR, '');
    if (!empty($testPdfFiles)) {
        // merge files
        $pdfTkArgs = implode(' ', $filenameArgs) . ' cat ' . implode(' ', $catArgs) . " output {$outputFilename} keep_first_id";
        if (execute(PDFTK, $pdfTkArgs) == 0) {
            copy($outputFilename, $contractFilename);
            //owerwrite source file
        } else {
            $GLOBALS['log']->error('Failed to merge PDF files!');
            //cleanup of temporary files
            unlink($finalFilename);
            unlink($outputFilename);
            unlink($contractFilename);
            foreach ($filenames as $attachfile) {
                unlink($attachfile);
            }
            return null;
        }
    }
    if (!empty($testAttachFiles) && !empty($testPdfFiles)) {
        $pdfTkArgs = "{$contractFilename} attach_files " . implode(' ', $attachArgs) . " output {$finalFilename}";
        if (execute(PDFTK, $pdfTkArgs) == 0) {
            copy($finalFilename, $contractFilename);
        } else {
            $GLOBALS['log']->error('Failed to attach documents to final PDF!');
        }
    }
    //cleanup of temporary files
    unlink($finalFilename);
    unlink($outputFilename);
    foreach ($filenames as $attachfile) {
        unlink($attachfile);
    }
    $contractFilenamePdf = str_replace("\\", "/", dirname($contractFilename) . DIRECTORY_SEPARATOR . basename($contractFilename, '.tmp') . '.pdf');
    rename($contractFilename, $contractFilenamePdf);
    //$GLOBALS['log']->error('OQC: Attachment filename is '.$contractFilenamePdf);
    //return str_replace("\\","/",$contractFilename.'.pdf');
    return $contractFilenamePdf;
}