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) {
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,
337 'ZIP_PROXY' =>
'PclZipProxy',
338 'DELIMITER_LEFT' =>
'{',
339 'DELIMITER_RIGHT' =>
'}'
343 $this->error = $e->getMessage();
353 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
354 }
catch (OdfException $e) {
361 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
367 $array_thirdparty_contact = array();
368 if ($usecontact && is_object($contactobject)) {
372 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
376 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
377 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
379 foreach ($tmparray as $key => $value) {
381 if (preg_match(
'/logo$/', $key)) {
382 if (file_exists($value)) {
383 $odfHandler->setImage($key, $value);
385 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
388 $odfHandler->setVars($key, $value,
true,
'UTF-8');
390 }
catch (OdfException $e) {
395 $foundtagforlines = 1;
397 $listlines = $odfHandler->setSegment(
'lines');
398 }
catch (OdfExceptionSegmentNotFound $e) {
400 $foundtagforlines = 0;
403 if ($foundtagforlines) {
405 foreach (
$object->lines as $line) {
408 if ($line->fk_product > 0) {
409 $line->fetch_product();
411 $line->product_ref = $line->product->ref;
412 $line->product_desc = $line->product->description;
413 $line->product_label = $line->product->label;
414 $line->product_type = $line->product->type;
415 $line->product_barcode = $line->product->barcode;
421 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
422 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
423 foreach ($tmparray as $key => $val) {
425 $listlines->setVars($key, $val,
true,
'UTF-8');
426 }
catch (SegmentException $e) {
433 $odfHandler->mergeSegment($listlines);
434 }
catch (OdfException $e) {
435 $this->error = $e->getMessage();
442 $tmparray = $outputlangs->get_translations_for_substitutions();
443 foreach ($tmparray as $key => $value) {
445 $odfHandler->setVars($key, $value,
true,
'UTF-8');
446 }
catch (OdfException $e) {
453 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
454 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
459 $odfHandler->exportAsAttachedPDF($file);
461 $this->error = $e->getMessage();
467 $odfHandler->saveToDisk($file);
469 $this->error = $e->getMessage();
475 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
476 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
482 $this->result = array(
'fullpath'=>$file);
486 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);