226 public function write_file(
$object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
229 global $user, $langs, $conf, $mysoc, $hookmanager;
232 if (empty($srctemplatepath)) {
233 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
238 $hookmanager->initHooks(array(
'odtgeneration'));
240 if (!is_object($outputlangs)) {
241 $outputlangs = $langs;
243 $sav_charset_output = $outputlangs->charset_output;
244 $outputlangs->charset_output =
'UTF-8';
246 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
248 if ($conf->mymodule->dir_output) {
262 $dir = $conf->mymodule->multidir_output[isset(
$object->entity) ?
$object->entity : 1];
264 if (!preg_match(
'/specimen/i', $objectref)) {
265 $dir .=
"/".$objectref;
267 $file = $dir.
"/".$objectref.
".odt";
269 if (!file_exists($dir)) {
271 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
276 if (file_exists($dir)) {
278 $newfile = basename($srctemplatepath);
279 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
280 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
281 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
283 $newfiletmp = $objectref .
'_' . $newfiletmp;
287 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
290 if ($format ==
'1') {
291 $format =
'%Y%m%d%H%M%S';
295 $filename = $newfiletmp .
'.' . $newfileformat;
297 $file = $dir .
'/' . $filename;
304 if (!is_writable($conf->mymodule->dir_temp)) {
305 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->mymodule->dir_temp);
306 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
312 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
313 if (count($arrayidcontact) > 0) {
315 $result =
$object->fetch_contact($arrayidcontact[0]);
319 $contactobject =
null;
320 if (!empty($usecontact)) {
322 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalInt(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
323 $object->contact->fetch_thirdparty();
324 $socobject =
$object->contact->thirdparty;
325 $contactobject =
$object->contact;
327 $socobject =
$object->thirdparty;
329 $contactobject =
$object->contact;
332 $socobject =
$object->thirdparty;
336 $substitutionarray = array(
337 '__FROM_NAME__' => $this->emetteur->name,
338 '__FROM_EMAIL__' => $this->emetteur->email,
339 '__TOTAL_TTC__' =>
$object->total_ttc,
340 '__TOTAL_HT__' =>
$object->total_ht,
341 '__TOTAL_VAT__' =>
$object->total_tva
345 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
346 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
350 $paramfreetext =
'MYMODULE_MYOBJECT_FREE_TEXT';
356 require_once ODTPHP_PATH.
'odf.php';
358 $odfHandler =
new Odf(
361 'PATH_TO_TMP' => $conf->mymodule->dir_temp,
362 'ZIP_PROXY' =>
'PclZipProxy',
363 'DELIMITER_LEFT' =>
'{',
364 'DELIMITER_RIGHT' =>
'}'
368 $this->error = $e->getMessage();
381 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
382 }
catch (OdfException $e) {
389 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
395 $array_thirdparty_contact = array();
396 if ($usecontact && is_object($contactobject)) {
400 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
404 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
405 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
407 foreach ($tmparray as $key => $value) {
409 if (preg_match(
'/logo$/', $key)) {
411 if (file_exists($value)) {
412 $odfHandler->setImage($key, $value);
414 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
418 $odfHandler->setVars($key, $value,
true,
'UTF-8');
420 }
catch (OdfException $e) {
425 $foundtagforlines = 1;
427 $listlines = $odfHandler->setSegment(
'lines');
428 }
catch (OdfExceptionSegmentNotFound $e) {
430 $foundtagforlines = 0;
434 if ($foundtagforlines) {
436 foreach (
$object->lines as $line) {
441 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
442 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
443 foreach ($tmparray as $key => $val) {
445 $listlines->setVars($key, $val,
true,
'UTF-8');
446 }
catch (SegmentException $e) {
453 $odfHandler->mergeSegment($listlines);
454 }
catch (OdfException $e) {
455 $this->error = $e->getMessage();
462 $tmparray = $outputlangs->get_translations_for_substitutions();
463 foreach ($tmparray as $key => $value) {
465 $odfHandler->setVars($key, $value,
true,
'UTF-8');
466 }
catch (OdfException $e) {
473 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
474 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
479 $odfHandler->exportAsAttachedPDF($file);
481 $this->error = $e->getMessage();
487 $odfHandler->saveToDisk($file);
489 $this->error = $e->getMessage();
495 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
496 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
502 $this->result = array(
'fullpath' => $file);
506 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);