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';
230 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
232 if ($conf->mrp->dir_output) {
234 if (!is_object($object)) {
236 $object =
new Mo($this->db);
237 $result = $object->fetch($id);
244 $object->fetch_thirdparty();
246 $dir = $conf->mrp->multidir_output[isset($object->entity) ? $object->entity : 1];
248 if (!preg_match(
'/specimen/i', $objectref)) {
249 $dir .=
"/".$objectref;
251 $file = $dir.
"/".$objectref.
".odt";
253 if (!file_exists($dir)) {
255 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
260 if (file_exists($dir)) {
262 $newfile = basename($srctemplatepath);
263 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
264 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
265 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
266 $newfiletmp = $objectref .
'_' . $newfiletmp;
269 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
272 if ($format ==
'1') {
273 $format =
'%Y%m%d%H%M%S';
277 $filename = $newfiletmp .
'.' . $newfileformat;
279 $file = $dir .
'/' . $filename;
286 if (!is_writable($conf->mrp->dir_temp)) {
287 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->mrp->dir_temp);
288 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
294 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
295 if (count($arrayidcontact) > 0) {
297 $result = $object->fetch_contact($arrayidcontact[0]);
301 $contactobject =
null;
302 if (!empty($usecontact)) {
304 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))) {
305 $object->contact->fetch_thirdparty();
306 $socobject = $object->contact->thirdparty;
307 $contactobject = $object->contact;
309 $socobject = $object->thirdparty;
311 $contactobject = $object->contact;
314 $socobject = $object->thirdparty;
318 $substitutionarray = array(
319 '__QTY_TO_PRODUCE__' => $object->qty,
323 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
324 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
328 $paramfreetext =
'MRP_MO_FREE_TEXT';
329 if (!empty($conf->global->$paramfreetext)) {
334 require_once ODTPHP_PATH.
'odf.php';
336 $odfHandler =
new Odf(
339 'PATH_TO_TMP' => $conf->mrp->dir_temp,
340 'ZIP_PROXY' =>
'PclZipProxy',
341 'DELIMITER_LEFT' =>
'{',
342 'DELIMITER_RIGHT' =>
'}'
346 $this->error = $e->getMessage();
359 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
360 }
catch (OdfException $e) {
373 $array_thirdparty_contact = array();
374 if ($usecontact && is_object($contactobject)) {
378 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
382 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
383 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
385 foreach ($tmparray as $key => $value) {
387 if (preg_match(
'/logo$/', $key)) {
389 if (file_exists($value)) {
390 $odfHandler->setImage($key, $value);
392 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
396 $odfHandler->setVars($key, $value,
true,
'UTF-8');
398 }
catch (OdfException $e) {
405 $foundtagforlines = 1;
407 $listlines = $odfHandler->setSegment(
'lines');
408 }
catch (OdfException $e) {
410 $foundtagforlines = 0;
414 if ($foundtagforlines) {
416 foreach ($object->lines as $line) {
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 (OdfException $e) {
428 }
catch (SegmentException $e) {
434 $odfHandler->mergeSegment($listlines);
436 }
catch (OdfException $e) {
437 $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);
458 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
460 $odfHandler->exportAsAttachedPDF($file);
462 $this->error = $e->getMessage();
463 dol_syslog(
'Error in exportAsAttachedPDF: '.$e->getMessage(), LOG_INFO);
468 $odfHandler->saveToDisk($file);
470 $this->error = $e->getMessage();
471 dol_syslog(
'Error in saveToDisk: '.$e->getMessage(), LOG_INFO);
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);