206 public function write_file(
$object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
209 global $user, $langs, $conf, $mysoc, $hookmanager;
211 if (empty($srctemplatepath)) {
212 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
217 if (!is_object($hookmanager)) {
218 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
221 $hookmanager->initHooks(array(
'odtgeneration'));
224 if (!is_object($outputlangs)) {
225 $outputlangs = $langs;
227 $sav_charset_output = $outputlangs->charset_output;
228 $outputlangs->charset_output =
'UTF-8';
230 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
232 if ($conf->fournisseur->commande->dir_output) {
236 $dir = $conf->fournisseur->commande->dir_output;
237 $file = $dir.
"/SPECIMEN.pdf";
241 $dir = $conf->fournisseur->commande->dir_output.
'/'.$objectref;
242 $file = $dir.
"/".$objectref.
".pdf";
244 $file = $dir.
"/".$objectref.($objectrefsupplier ?
"_".$objectrefsupplier :
"").
".pdf";
248 if (!file_exists($dir)) {
250 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
255 if (file_exists($dir)) {
257 $newfile = basename($srctemplatepath);
258 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
259 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
260 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
261 $newfiletmp = $objectref .
'_' . $newfiletmp;
264 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
267 if ($format ==
'1') {
268 $format =
'%Y%m%d%H%M%S';
272 $filename = $newfiletmp .
'.' . $newfileformat;
274 $file = $dir .
'/' . $filename;
280 dol_mkdir($conf->fournisseur->commande->dir_temp);
281 if (!is_writable($conf->fournisseur->commande->dir_temp)) {
282 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->fournisseur->commande->dir_temp);
283 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
289 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
290 if (count($arrayidcontact) > 0) {
292 $result =
$object->fetch_contact($arrayidcontact[0]);
296 $contactobject =
null;
297 if (!empty($usecontact)) {
299 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
300 $object->contact->fetch_thirdparty();
301 $socobject =
$object->contact->thirdparty;
302 $contactobject =
$object->contact;
304 $socobject =
$object->thirdparty;
306 $contactobject =
$object->contact;
309 $socobject =
$object->thirdparty;
313 $substitutionarray = array(
314 '__FROM_NAME__' => $this->issuer->name,
315 '__FROM_EMAIL__' => $this->issuer->email,
316 '__TOTAL_TTC__' =>
$object->total_ttc,
317 '__TOTAL_HT__' =>
$object->total_ht,
318 '__TOTAL_VAT__' =>
$object->total_tva
322 $parameters = array(
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
323 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
326 $paramfreetext =
'ORDER_FREE_TEXT';
327 if (!empty($conf->global->$paramfreetext)) {
332 require_once ODTPHP_PATH.
'odf.php';
334 $odfHandler =
new Odf(
337 'PATH_TO_TMP' => $conf->fournisseur->dir_temp,
338 'ZIP_PROXY' =>
'PclZipProxy',
339 'DELIMITER_LEFT' =>
'{',
340 'DELIMITER_RIGHT' =>
'}'
344 $this->error = $e->getMessage();
354 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
355 }
catch (OdfException $e) {
362 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
368 $array_thirdparty_contact = array();
369 if ($usecontact && is_object($contactobject)) {
373 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
377 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
378 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
380 foreach ($tmparray as $key => $value) {
382 if (preg_match(
'/logo$/', $key)) {
383 if (file_exists($value)) {
384 $odfHandler->setImage($key, $value);
386 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
389 $odfHandler->setVars($key, $value,
true,
'UTF-8');
391 }
catch (OdfException $e) {
396 $foundtagforlines = 1;
398 $listlines = $odfHandler->setSegment(
'lines');
399 }
catch (OdfExceptionSegmentNotFound $e) {
401 $foundtagforlines = 0;
404 if ($foundtagforlines) {
406 foreach (
$object->lines as $line) {
411 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
412 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
413 foreach ($tmparray as $key => $val) {
415 $listlines->setVars($key, $val,
true,
'UTF-8');
416 }
catch (SegmentException $e) {
423 $odfHandler->mergeSegment($listlines);
424 }
catch (OdfException $e) {
425 $this->error = $e->getMessage();
432 $tmparray = $outputlangs->get_translations_for_substitutions();
433 foreach ($tmparray as $key => $value) {
435 $odfHandler->setVars($key, $value,
true,
'UTF-8');
436 }
catch (OdfException $e) {
443 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
444 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
449 $odfHandler->exportAsAttachedPDF($file);
451 $this->error = $e->getMessage();
457 $odfHandler->saveToDisk($file);
459 $this->error = $e->getMessage();
465 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
466 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
472 $this->result = array(
'fullpath'=>$file);
476 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);