201 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
204 global $user, $langs, $conf, $mysoc, $hookmanager;
206 if (empty($srctemplatepath)) {
207 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
212 if (!is_object($hookmanager)) {
213 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
216 $hookmanager->initHooks(array(
'odtgeneration'));
219 if (!is_object($outputlangs)) {
220 $outputlangs = $langs;
222 $sav_charset_output = $outputlangs->charset_output;
223 $outputlangs->charset_output =
'UTF-8';
226 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
228 if ($conf->bom->dir_output) {
230 if (!is_object($object)) {
232 $object =
new BOM($this->db);
233 $result = $object->fetch($id);
240 $object->fetch_thirdparty();
241 $object->fetch_product();
243 $dir = $conf->bom->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->bom->dir_temp)) {
284 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->bom->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 '__QTY_TO_PRODUCE__' => $object->qty,
320 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
321 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
325 $paramfreetext =
'BOM_FREE_TEXT';
326 if (!empty($conf->global->$paramfreetext)) {
331 require_once ODTPHP_PATH.
'odf.php';
333 $odfHandler =
new Odf(
336 'PATH_TO_TMP' => $conf->bom->dir_temp,
338 'DELIMITER_LEFT' =>
'{',
339 'DELIMITER_RIGHT' =>
'}'
343 $this->error = $e->getMessage();
356 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
357 }
catch (OdfException $e) {
370 $array_thirdparty_contact = array();
371 if ($usecontact && is_object($contactobject)) {
375 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
379 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
380 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
382 foreach ($tmparray as $key => $value) {
384 if (preg_match(
'/logo$/', $key)) {
385 if (file_exists($value)) {
386 $odfHandler->setImage($key, $value);
388 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
391 $odfHandler->setVars($key, $value,
true,
'UTF-8');
393 }
catch (OdfException $e) {
399 $foundtagforlines = 1;
401 $listlines = $odfHandler->setSegment(
'lines');
402 }
catch (OdfExceptionSegmentNotFound $e) {
404 $foundtagforlines = 0;
406 }
catch (OdfException $e) {
407 $foundtagforlines = 0;
410 if ($foundtagforlines) {
412 foreach ($object->lines as $line) {
415 if ($line->fk_product > 0) {
416 $line->fetch_product();
418 $line->product_ref = $line->product->ref;
419 $line->product_desc = $line->product->description;
420 $line->product_label = $line->product->label;
421 $line->product_type = $line->product->type;
422 $line->product_barcode = $line->product->barcode;
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) {
461 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
462 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
467 $odfHandler->exportAsAttachedPDF($file);
469 $this->error = $e->getMessage();
475 $odfHandler->saveToDisk($file);
477 $this->error = $e->getMessage();
483 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
484 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
490 $this->result = array(
'fullpath'=>$file);
494 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);