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';
231 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
233 if ($conf->bom->dir_output) {
249 $dir = $conf->bom->multidir_output[isset(
$object->entity) ?
$object->entity : 1];
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;
285 if (!is_writable($conf->bom->dir_temp)) {
286 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->bom->dir_temp);
287 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
293 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
294 if (count($arrayidcontact) > 0) {
296 $result =
$object->fetch_contact($arrayidcontact[0]);
300 $contactobject =
null;
301 if (!empty($usecontact)) {
303 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
304 $object->contact->fetch_thirdparty();
305 $socobject =
$object->contact->thirdparty;
306 $contactobject =
$object->contact;
308 $socobject =
$object->thirdparty;
310 $contactobject =
$object->contact;
313 $socobject =
$object->thirdparty;
317 $substitutionarray = array(
318 '__QTY_TO_PRODUCE__' =>
$object->qty,
322 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
323 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
327 $paramfreetext =
'BOM_FREE_TEXT';
333 require_once ODTPHP_PATH.
'odf.php';
335 $odfHandler =
new Odf(
338 'PATH_TO_TMP' => $conf->bom->dir_temp,
340 'DELIMITER_LEFT' =>
'{',
341 'DELIMITER_RIGHT' =>
'}'
345 $this->error = $e->getMessage();
355 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
356 }
catch (OdfException $e) {
363 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
369 $array_thirdparty_contact = array();
370 if ($usecontact && is_object($contactobject)) {
374 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
378 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
379 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
381 foreach ($tmparray as $key => $value) {
383 if (preg_match(
'/logo$/', $key)) {
384 if (file_exists($value)) {
385 $odfHandler->setImage($key, $value);
387 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
390 $odfHandler->setVars($key, $value,
true,
'UTF-8');
392 }
catch (OdfException $e) {
397 $foundtagforlines = 1;
399 $listlines = $odfHandler->setSegment(
'lines');
400 }
catch (OdfExceptionSegmentNotFound $e) {
402 $foundtagforlines = 0;
405 if ($foundtagforlines) {
407 foreach (
$object->lines as $line) {
410 if ($line->fk_product > 0) {
411 $line->fetch_product();
413 $line->product_ref = $line->product->ref;
414 $line->product_desc = $line->product->description;
415 $line->product_label = $line->product->label;
416 $line->product_type = $line->product->type;
417 $line->product_barcode = $line->product->barcode;
423 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
424 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
425 foreach ($tmparray as $key => $val) {
427 $listlines->setVars($key, $val,
true,
'UTF-8');
428 }
catch (SegmentException $e) {
435 $odfHandler->mergeSegment($listlines);
436 }
catch (OdfException $e) {
437 $this->error = $e->getMessage();
444 $tmparray = $outputlangs->get_translations_for_substitutions();
445 foreach ($tmparray as $key => $value) {
447 $odfHandler->setVars($key, $value,
true,
'UTF-8');
448 }
catch (OdfException $e) {
454 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
455 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
460 $odfHandler->exportAsAttachedPDF($file);
462 $this->error = $e->getMessage();
468 $odfHandler->saveToDisk($file);
470 $this->error = $e->getMessage();
476 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
477 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
483 $this->result = array(
'fullpath' => $file);
487 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);