208 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
211 global $user, $langs, $conf, $mysoc, $hookmanager;
213 if (empty($srctemplatepath)) {
214 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
219 if (!is_object($hookmanager)) {
220 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
223 $hookmanager->initHooks(array(
'odtgeneration'));
226 if (!is_object($outputlangs)) {
227 $outputlangs = $langs;
229 $sav_charset_output = $outputlangs->charset_output;
230 $outputlangs->charset_output =
'UTF-8';
233 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
235 if ($conf->commande->dir_output) {
237 if (!is_object($object)) {
240 $result = $object->fetch($id);
247 $object->fetch_thirdparty();
249 $dir = $conf->commande->multidir_output[$object->entity];
251 if (!preg_match(
'/specimen/i', $objectref)) {
252 $dir .=
"/".$objectref;
254 $file = $dir.
"/".$objectref.
".odt";
256 if (!file_exists($dir)) {
258 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
263 if (file_exists($dir)) {
265 $newfile = basename($srctemplatepath);
266 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
267 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
268 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
269 $newfiletmp = $objectref .
'_' . $newfiletmp;
272 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
275 if ($format ==
'1') {
276 $format =
'%Y%m%d%H%M%S';
280 $filename = $newfiletmp .
'.' . $newfileformat;
282 $file = $dir .
'/' . $filename;
289 if (!is_writable($conf->commande->dir_temp)) {
290 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->commande->dir_temp);
291 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
297 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
298 if (count($arrayidcontact) > 0) {
300 $result = $object->fetch_contact($arrayidcontact[0]);
304 $contactobject =
null;
305 if (!empty($usecontact)) {
307 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
308 $object->contact->fetch_thirdparty();
309 $socobject = $object->contact->thirdparty;
310 $contactobject = $object->contact;
312 $socobject = $object->thirdparty;
314 $contactobject = $object->contact;
317 $socobject = $object->thirdparty;
321 $substitutionarray = array(
322 '__FROM_NAME__' => $this->emetteur->name,
323 '__FROM_EMAIL__' => $this->emetteur->email,
324 '__TOTAL_TTC__' => $object->total_ttc,
325 '__TOTAL_HT__' => $object->total_ht,
326 '__TOTAL_VAT__' => $object->total_tva
330 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
331 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
335 $paramfreetext =
'ORDER_FREE_TEXT';
336 if (!empty($conf->global->$paramfreetext)) {
341 require_once ODTPHP_PATH.
'odf.php';
343 $odfHandler =
new Odf(
346 'PATH_TO_TMP' => $conf->commande->dir_temp,
348 'DELIMITER_LEFT' =>
'{',
349 'DELIMITER_RIGHT' =>
'}'
353 $this->error = $e->getMessage();
366 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
367 }
catch (OdfException $e) {
380 $array_thirdparty_contact = array();
382 if ($usecontact && is_object($contactobject)) {
386 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
390 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
391 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
393 foreach ($tmparray as $key => $value) {
395 if (preg_match(
'/logo$/', $key)) {
396 if (file_exists($value)) {
397 $odfHandler->setImage($key, $value);
399 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
402 $odfHandler->setVars($key, $value,
true,
'UTF-8');
404 }
catch (OdfException $e) {
410 $foundtagforlines = 1;
412 $listlines = $odfHandler->setSegment(
'lines');
413 }
catch (OdfExceptionSegmentNotFound $e) {
415 $foundtagforlines = 0;
417 }
catch (OdfException $e) {
418 $foundtagforlines = 0;
421 if ($foundtagforlines) {
423 foreach ($object->lines as $line) {
428 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
429 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
430 foreach ($tmparray as $key => $val) {
432 $listlines->setVars($key, $val,
true,
'UTF-8');
433 }
catch (OdfException $e) {
435 }
catch (SegmentException $e) {
441 $odfHandler->mergeSegment($listlines);
443 }
catch (OdfException $e) {
444 $this->error = $e->getMessage();
450 $tmparray = $outputlangs->get_translations_for_substitutions();
451 foreach ($tmparray as $key => $value) {
453 $odfHandler->setVars($key, $value,
true,
'UTF-8');
454 }
catch (OdfException $e) {
460 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
461 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
466 $odfHandler->exportAsAttachedPDF($file);
468 $this->error = $e->getMessage();
474 $odfHandler->saveToDisk($file);
476 $this->error = $e->getMessage();
482 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
483 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
489 $this->result = array(
'fullpath'=>$file);
493 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);