diff --git a/core/modules/saturne/modules_saturne.php b/core/modules/saturne/modules_saturne.php index 5869983f..3a7e343d 100644 --- a/core/modules/saturne/modules_saturne.php +++ b/core/modules/saturne/modules_saturne.php @@ -818,6 +818,97 @@ public function buildDocumentFilename($objectDocument, $outputLangs, $object, $m return $dir . '/' . $fileName; } + + /** + * Init documents values for pdf and ODT + * + * @param Object $object Object of the page you are on. + * @param Translate $outputLangs Lang object to use for output. + * @param array $moreParam More param (Object/user/etc) + * @param string $file Path of the source template + * + * @return void + * @throws Exception + */ + public function initOdtDocument($object, $outputLangs,$moreParam, $file) + { + global $action, $conf, $hookmanager, $mysoc; + $substitutionArray = []; + complete_substitutions_array($substitutionArray, $outputLangs, $object); + // Call the ODTSubstitution hook + $parameters = ['file' => $file, 'object' => $object, 'outputlangs' => $outputLangs, 'substitutionarray' => &$substitutionArray]; + $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks + + // Open and load template + require_once ODTPHP_PATH . 'odf.php'; + + // Define substitution array + $substitutionArray = getCommonSubstitutionArray($outputLangs, 0, null, $object); + $arrayObjectFromProperties = $this->get_substitutionarray_each_var_object($object, $outputLangs); + $arraySoc = $this->get_substitutionarray_mysoc($mysoc, $outputLangs); + $arraySoc['mycompany_logo'] = preg_replace('/_small/', '_mini', $arraySoc['mycompany_logo']); + + $tmpArray = array_merge($substitutionArray, $arrayObjectFromProperties, $arraySoc, $moreParam['tmparray']); + if (isModEnabled('multicompany')) { + $tmpArray['entity'] = $conf->entity; + } else { + $tmpArray['entity'] = ''; + } + } + + /** + * Init documents values for pdf + * + * @param Translate $outputLangs Lang object to use for output. + * + * @return void + * @throws Exception + */ + public function initPdfDocument($outputLangs) + { + global $user; + + $pdf = pdf_getInstance($this->format); + $defaultFontSize = pdf_getPDFFontSize($outputLangs) + 2; + + if (class_exists('TCPDF')) { + $pdf->setPrintHeader(false); + $pdf->setPrintFooter(false); + } + + $pdf->SetFont(pdf_getPDFFont($outputLangs)); + $pdf->Open(); + $pdf->SetDrawColor(128, 128, 128); + $pdf->SetTitle($outputLangs->convToOutputCharset($this->document_type)); + $pdf->SetSubject($outputLangs->transnoentities($this->document_type)); + $pdf->SetCreator('Dolibarr ' . DOL_VERSION); + $pdf->SetAuthor($outputLangs->convToOutputCharset($user->getFullName($outputLangs))); + $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); + $pdf->setPageOrientation($this->orientation, 1, $this->marge_basse); + $pdf->SetAutoPageBreak(1, $this->marge_basse); + } + + /** + * Init documents values for pdf and ODT + * + * @param Object $object Object of the page you are on. + * @param Translate $outputLangs Lang object to use for output. + * @param string $srcTemplatePath Path of the source template + * @param array $moreParam More param (Object/user/etc) + * @param string $file Path of the source template + * + * @return void + * @throws Exception + */ + public function initDocumentGenerator($object, $outputLangs,$moreParam, $file) + { + if ($this->type == 'odt') { + $this->initOdtDocument($object, $outputLangs,$moreParam, $file); + } else { + $this->initPdfDocument($outputLangs); + } + } + /** * Function to build a document on disk * @@ -866,49 +957,30 @@ public function write_file(SaturneDocuments $objectDocument, Translate $outputLa return -1; } - // Make substitution - $substitutionArray = []; - complete_substitutions_array($substitutionArray, $outputLangs, $object); - // Call the ODTSubstitution hook - $parameters = ['file' => $file, 'object' => $object, 'outputlangs' => $outputLangs, 'substitutionarray' => &$substitutionArray]; - $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks - - // Open and load template - require_once ODTPHP_PATH . 'odf.php'; - try { - $odfHandler = new odf( - $srcTemplatePath, - [ - 'PATH_TO_TMP' => $conf->$moduleNameLowerCase->dir_temp, - 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy - 'DELIMITER_LEFT' => '{', - 'DELIMITER_RIGHT' => '}' - ] - ); - } catch (Exception $e) { - $this->error = $e->getMessage(); - dol_syslog($e->getMessage()); - return -1; - } + $this->initDocumentGenerator($object, $outputLangs, $moreParam, $file); - // Define substitution array - $substitutionArray = getCommonSubstitutionArray($outputLangs, 0, null, $object); - $arrayObjectFromProperties = $this->get_substitutionarray_each_var_object($object, $outputLangs); - $arraySoc = $this->get_substitutionarray_mysoc($mysoc, $outputLangs); - $arraySoc['mycompany_logo'] = preg_replace('/_small/', '_mini', $arraySoc['mycompany_logo']); + // Replace labels translated + $tmpArray = $outputLangs->get_translations_for_substitutions(); - $tmpArray = array_merge($substitutionArray, $arrayObjectFromProperties, $arraySoc, $moreParam['tmparray']); - if (isModEnabled('multicompany')) { - $tmpArray['entity'] = $conf->entity; - } else { - $tmpArray['entity'] = ''; + if ($this->type == 'odt') { + try { + $odfHandler = new odf( + $srcTemplatePath, + [ + 'PATH_TO_TMP' => $conf->$moduleNameLowerCase->dir_temp, + 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy + 'DELIMITER_LEFT' => '{', + 'DELIMITER_RIGHT' => '}' + ] + ); + } catch (Exception $e) { + $this->error = $e->getMessage(); + dol_syslog($e->getMessage()); + return -1; + } } $this->fillTags($odfHandler, $outputLangs, $tmpArray, $moreParam); - - // Replace labels translated - $tmpArray = $outputLangs->get_translations_for_substitutions(); - // Call the beforeODTSave hook $parameters = ['odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputLangs, 'substitutionarray' => &$tmpArray]; $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks diff --git a/lib/medias.lib.php b/lib/medias.lib.php index a904f2dd..83353fb7 100644 --- a/lib/medias.lib.php +++ b/lib/medias.lib.php @@ -382,6 +382,7 @@ function saturne_get_thumb_name(string $filename, string $thumbType = 'small', s $fileExtension = pathinfo($filename, PATHINFO_EXTENSION); if (!empty($filePath)) { + require_once __DIR__ . './dolibarr.lib.php'; $filePathThumb = $filePath . '/thumbs'; if (!dol_is_dir($filePathThumb)) { dol_mkdir($filePathThumb);