198 public function write_file(
$object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
201 global $user, $langs, $conf, $mysoc, $hookmanager;
203 if (empty($srctemplatepath)) {
204 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
209 if (!is_object($hookmanager)) {
210 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
213 $hookmanager->initHooks(array(
'odtgeneration'));
216 if (!is_object($outputlangs)) {
217 $outputlangs = $langs;
219 $sav_charset_output = $outputlangs->charset_output;
220 $outputlangs->charset_output =
'UTF-8';
222 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
224 if ($conf->asset->dir_output) {
238 $dir = $conf->asset->multidir_output[isset(
$object->entity) ?
$object->entity : 1];
240 if (!preg_match(
'/specimen/i', $objectref)) {
241 $dir .=
"/".$objectref;
243 $file = $dir.
"/".$objectref.
".odt";
245 if (!file_exists($dir)) {
247 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
252 if (file_exists($dir)) {
254 $newfile = basename($srctemplatepath);
255 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
256 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
257 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
258 $newfiletmp = $objectref.
'_'.$newfiletmp;
261 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
264 if ($format ==
'1') {
265 $format =
'%Y%m%d%H%M%S';
269 $filename = $newfiletmp.
'.'.$newfileformat;
271 $file = $dir.
'/'.$filename;
278 if (!is_writable($conf->asset->dir_temp)) {
279 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->asset->dir_temp);
280 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
286 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
287 if (count($arrayidcontact) > 0) {
289 $result =
$object->fetch_contact($arrayidcontact[0]);
293 $contactobject =
null;
294 if (!empty($usecontact)) {
296 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
297 $object->contact->fetch_thirdparty();
298 $socobject =
$object->contact->thirdparty;
299 $contactobject =
$object->contact;
301 $socobject =
$object->thirdparty;
303 $contactobject =
$object->contact;
306 $socobject =
$object->thirdparty;
310 $substitutionarray = array(
311 '__FROM_NAME__' => $this->emetteur->name,
312 '__FROM_EMAIL__' => $this->emetteur->email,
313 '__TOTAL_TTC__' =>
$object->total_ttc,
314 '__TOTAL_HT__' =>
$object->total_ht,
315 '__TOTAL_VAT__' =>
$object->total_tva
319 $parameters = array(
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
320 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
324 $paramfreetext =
'ORDER_FREE_TEXT';
325 if (!empty($conf->global->$paramfreetext)) {
330 require_once ODTPHP_PATH.
'odf.php';
332 $odfHandler =
new Odf(
335 'PATH_TO_TMP' => $conf->asset->dir_temp,
336 'ZIP_PROXY' =>
'PclZipProxy',
337 'DELIMITER_LEFT' =>
'{',
338 'DELIMITER_RIGHT' =>
'}'
342 $this->error = $e->getMessage();
352 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
353 }
catch (OdfException $e) {
360 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
366 $array_thirdparty_contact = array();
367 if ($usecontact && is_object($contactobject)) {
371 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
375 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
376 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
378 foreach ($tmparray as $key => $value) {
380 if (preg_match(
'/logo$/', $key)) {
382 if (file_exists($value)) {
383 $odfHandler->setImage($key, $value);
385 $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);