203 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
206 global $user, $langs, $conf, $mysoc, $hookmanager;
208 if (empty($srctemplatepath)) {
209 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
214 if (!is_object($hookmanager)) {
215 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
218 $hookmanager->initHooks(array(
'odtgeneration'));
221 if (!is_object($outputlangs)) {
222 $outputlangs = $langs;
224 $sav_charset_output = $outputlangs->charset_output;
225 $outputlangs->charset_output =
'UTF-8';
227 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
229 if ($conf->asset->dir_output) {
243 $dir = $conf->asset->multidir_output[isset(
$object->entity) ?
$object->entity : 1];
245 if (!preg_match(
'/specimen/i', $objectref)) {
246 $dir .=
"/".$objectref;
248 $file = $dir.
"/".$objectref.
".odt";
250 if (!file_exists($dir)) {
252 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
257 if (file_exists($dir)) {
259 $newfile = basename($srctemplatepath);
260 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
261 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
262 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
263 $newfiletmp = $objectref.
'_'.$newfiletmp;
266 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
269 if ($format ==
'1') {
270 $format =
'%Y%m%d%H%M%S';
274 $filename = $newfiletmp.
'.'.$newfileformat;
276 $file = $dir.
'/'.$filename;
283 if (!is_writable($conf->asset->dir_temp)) {
284 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->asset->dir_temp);
285 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
291 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
292 if (count($arrayidcontact) > 0) {
294 $result =
$object->fetch_contact($arrayidcontact[0]);
298 $contactobject =
null;
299 if (!empty($usecontact)) {
301 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
302 $object->contact->fetch_thirdparty();
303 $socobject =
$object->contact->thirdparty;
304 $contactobject =
$object->contact;
306 $socobject =
$object->thirdparty;
308 $contactobject =
$object->contact;
311 $socobject =
$object->thirdparty;
315 $substitutionarray = array(
316 '__FROM_NAME__' => $this->emetteur->name,
317 '__FROM_EMAIL__' => $this->emetteur->email,
318 '__TOTAL_TTC__' =>
$object->total_ttc,
319 '__TOTAL_HT__' =>
$object->total_ht,
320 '__TOTAL_VAT__' =>
$object->total_tva
324 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
325 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
329 $paramfreetext =
'ORDER_FREE_TEXT';
335 require_once ODTPHP_PATH.
'odf.php';
337 $odfHandler =
new Odf(
340 'PATH_TO_TMP' => $conf->asset->dir_temp,
342 'DELIMITER_LEFT' =>
'{',
343 'DELIMITER_RIGHT' =>
'}'
347 $this->error = $e->getMessage();
357 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
358 }
catch (OdfException $e) {
365 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
371 $array_thirdparty_contact = array();
372 if ($usecontact && is_object($contactobject)) {
376 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
380 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
381 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
383 foreach ($tmparray as $key => $value) {
385 if (preg_match(
'/logo$/', $key)) {
387 if (file_exists($value)) {
388 $odfHandler->setImage($key, $value);
390 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
394 $odfHandler->setVars($key, $value,
true,
'UTF-8');
396 }
catch (OdfException $e) {
401 $foundtagforlines = 1;
403 $listlines = $odfHandler->setSegment(
'lines');
404 }
catch (OdfExceptionSegmentNotFound $e) {
406 $foundtagforlines = 0;
409 if ($foundtagforlines) {
411 foreach (
$object->lines as $line) {
416 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
417 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
418 foreach ($tmparray as $key => $val) {
420 $listlines->setVars($key, $val,
true,
'UTF-8');
421 }
catch (SegmentException $e) {
428 $odfHandler->mergeSegment($listlines);
429 }
catch (OdfException $e) {
430 $this->error = $e->getMessage();
437 $tmparray = $outputlangs->get_translations_for_substitutions();
438 foreach ($tmparray as $key => $value) {
440 $odfHandler->setVars($key, $value,
true,
'UTF-8');
441 }
catch (OdfException $e) {
448 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
449 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
454 $odfHandler->exportAsAttachedPDF($file);
456 $this->error = $e->getMessage();
462 $odfHandler->saveToDisk($file);
464 $this->error = $e->getMessage();
470 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
471 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
477 $this->result = array(
'fullpath' => $file);
481 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);