202 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
205 global $user, $langs, $conf, $mysoc, $hookmanager;
207 if (empty($srctemplatepath)) {
208 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
213 if (!is_object($hookmanager)) {
214 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
217 $hookmanager->initHooks(array(
'odtgeneration'));
220 if (!is_object($outputlangs)) {
221 $outputlangs = $langs;
223 $sav_charset_output = $outputlangs->charset_output;
224 $outputlangs->charset_output =
'UTF-8';
227 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
229 if ($conf->bom->dir_output) {
244 $dir = $conf->bom->multidir_output[isset(
$object->entity) ?
$object->entity : 1];
246 if (!preg_match(
'/specimen/i', $objectref)) {
247 $dir .=
"/".$objectref;
249 $file = $dir.
"/".$objectref.
".odt";
251 if (!file_exists($dir)) {
253 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
258 if (file_exists($dir)) {
260 $newfile = basename($srctemplatepath);
261 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
262 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
263 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
264 $newfiletmp = $objectref .
'_' . $newfiletmp;
267 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
270 if ($format ==
'1') {
271 $format =
'%Y%m%d%H%M%S';
275 $filename = $newfiletmp .
'.' . $newfileformat;
277 $file = $dir .
'/' . $filename;
284 if (!is_writable($conf->bom->dir_temp)) {
285 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->bom->dir_temp);
286 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
292 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
293 if (count($arrayidcontact) > 0) {
295 $result =
$object->fetch_contact($arrayidcontact[0]);
299 $contactobject =
null;
300 if (!empty($usecontact)) {
302 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
303 $object->contact->fetch_thirdparty();
304 $socobject =
$object->contact->thirdparty;
305 $contactobject =
$object->contact;
307 $socobject =
$object->thirdparty;
309 $contactobject =
$object->contact;
312 $socobject =
$object->thirdparty;
316 $substitutionarray = array(
317 '__QTY_TO_PRODUCE__' =>
$object->qty,
321 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
322 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
326 $paramfreetext =
'BOM_FREE_TEXT';
332 require_once ODTPHP_PATH.
'odf.php';
334 $odfHandler =
new Odf(
337 'PATH_TO_TMP' => $conf->bom->dir_temp,
338 'ZIP_PROXY' =>
'PclZipProxy',
339 'DELIMITER_LEFT' =>
'{',
340 'DELIMITER_RIGHT' =>
'}'
344 $this->error = $e->getMessage();
354 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
355 }
catch (OdfException $e) {
362 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
368 $array_thirdparty_contact = array();
369 if ($usecontact && is_object($contactobject)) {
373 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
377 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
378 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
380 foreach ($tmparray as $key => $value) {
382 if (preg_match(
'/logo$/', $key)) {
383 if (file_exists($value)) {
384 $odfHandler->setImage($key, $value);
386 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
389 $odfHandler->setVars($key, $value,
true,
'UTF-8');
391 }
catch (OdfException $e) {
396 $foundtagforlines = 1;
398 $listlines = $odfHandler->setSegment(
'lines');
399 }
catch (OdfExceptionSegmentNotFound $e) {
401 $foundtagforlines = 0;
404 if ($foundtagforlines) {
406 foreach (
$object->lines as $line) {
409 if ($line->fk_product > 0) {
410 $line->fetch_product();
412 $line->product_ref = $line->product->ref;
413 $line->product_desc = $line->product->description;
414 $line->product_label = $line->product->label;
415 $line->product_type = $line->product->type;
416 $line->product_barcode = $line->product->barcode;
422 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
423 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
424 foreach ($tmparray as $key => $val) {
426 $listlines->setVars($key, $val,
true,
'UTF-8');
427 }
catch (SegmentException $e) {
434 $odfHandler->mergeSegment($listlines);
435 }
catch (OdfException $e) {
436 $this->error = $e->getMessage();
443 $tmparray = $outputlangs->get_translations_for_substitutions();
444 foreach ($tmparray as $key => $value) {
446 $odfHandler->setVars($key, $value,
true,
'UTF-8');
447 }
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);