211 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
214 global $user, $langs, $conf, $mysoc, $hookmanager;
216 if (empty($srctemplatepath)) {
217 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
222 if (!is_object($hookmanager)) {
223 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
226 $hookmanager->initHooks(array(
'odtgeneration'));
229 if (!is_object($outputlangs)) {
230 $outputlangs = $langs;
232 $sav_charset_output = $outputlangs->charset_output;
233 $outputlangs->charset_output =
'UTF-8';
236 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
238 if ($conf->bom->dir_output) {
240 if (!is_object($object)) {
242 $object =
new BOM($this->db);
243 $result = $object->fetch($id);
250 $object->fetch_thirdparty();
251 $object->fetch_product();
253 $dir = $conf->bom->multidir_output[isset($object->entity) ? $object->entity : 1];
255 if (!preg_match(
'/specimen/i', $objectref)) {
256 $dir .=
"/".$objectref;
258 $file = $dir.
"/".$objectref.
".odt";
260 if (!file_exists($dir)) {
262 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
267 if (file_exists($dir)) {
269 $newfile = basename($srctemplatepath);
270 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
271 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
272 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
273 $newfiletmp = $objectref .
'_' . $newfiletmp;
276 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
279 if ($format ==
'1') {
280 $format =
'%Y%m%d%H%M%S';
284 $filename = $newfiletmp .
'.' . $newfileformat;
286 $file = $dir .
'/' . $filename;
293 if (!is_writable($conf->bom->dir_temp)) {
294 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->bom->dir_temp);
295 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
301 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
302 if (count($arrayidcontact) > 0) {
304 $result = $object->fetch_contact($arrayidcontact[0]);
308 $contactobject =
null;
309 if (!empty($usecontact)) {
311 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT))) {
312 $object->contact->fetch_thirdparty();
313 $socobject = $object->contact->thirdparty;
314 $contactobject = $object->contact;
316 $socobject = $object->thirdparty;
318 $contactobject = $object->contact;
321 $socobject = $object->thirdparty;
325 $substitutionarray = array(
326 '__QTY_TO_PRODUCE__' => $object->qty,
330 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
331 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
335 $paramfreetext =
'BOM_FREE_TEXT';
336 if (!empty($conf->global->$paramfreetext)) {
341 require_once ODTPHP_PATH.
'odf.php';
343 $odfHandler =
new Odf(
346 'PATH_TO_TMP' => $conf->bom->dir_temp,
347 'ZIP_PROXY' =>
'PclZipProxy',
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();
381 if ($usecontact && is_object($contactobject)) {
385 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
389 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
390 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
392 foreach ($tmparray as $key => $value) {
394 if (preg_match(
'/logo$/', $key)) {
395 if (file_exists($value)) {
396 $odfHandler->setImage($key, $value);
398 $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 (OdfException $e) {
415 $foundtagforlines = 0;
419 if ($foundtagforlines) {
421 foreach ($object->lines as $line) {
424 if ($line->fk_product > 0) {
425 $line->fetch_product();
427 $line->product_ref = $line->product->ref;
428 $line->product_desc = $line->product->description;
429 $line->product_label = $line->product->label;
430 $line->product_type = $line->product->type;
431 $line->product_barcode = $line->product->barcode;
437 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
438 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
439 foreach ($tmparray as $key => $val) {
441 $listlines->setVars($key, $val,
true,
'UTF-8');
442 }
catch (OdfException $e) {
444 }
catch (SegmentException $e) {
450 $odfHandler->mergeSegment($listlines);
452 }
catch (OdfException $e) {
453 $this->error = $e->getMessage();
459 $tmparray = $outputlangs->get_translations_for_substitutions();
460 foreach ($tmparray as $key => $value) {
462 $odfHandler->setVars($key, $value,
true,
'UTF-8');
463 }
catch (OdfException $e) {
470 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
471 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
474 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
476 $odfHandler->exportAsAttachedPDF($file);
478 $this->error = $e->getMessage();
484 $odfHandler->saveToDisk($file);
486 $this->error = $e->getMessage();
492 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
493 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
499 $this->result = array(
'fullpath'=>$file);
503 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);