196 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
199 global $user, $langs, $conf, $mysoc, $hookmanager;
201 if (empty($srctemplatepath)) {
202 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
207 if (!is_object($hookmanager)) {
208 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
211 $hookmanager->initHooks(array(
'odtgeneration'));
214 if (!is_object($outputlangs)) {
215 $outputlangs = $langs;
217 $sav_charset_output = $outputlangs->charset_output;
218 $outputlangs->charset_output =
'UTF-8';
220 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
222 if ($conf->mrp->dir_output) {
224 if (!is_object($object)) {
226 $object =
new Mo($this->db);
227 $result = $object->fetch($id);
234 $object->fetch_thirdparty();
236 $dir = $conf->mrp->multidir_output[isset($object->entity) ? $object->entity : 1];
238 if (!preg_match(
'/specimen/i', $objectref)) {
239 $dir .=
"/".$objectref;
241 $file = $dir.
"/".$objectref.
".odt";
243 if (!file_exists($dir)) {
245 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
250 if (file_exists($dir)) {
252 $newfile = basename($srctemplatepath);
253 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
254 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
255 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
256 $newfiletmp = $objectref .
'_' . $newfiletmp;
259 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
262 if ($format ==
'1') {
263 $format =
'%Y%m%d%H%M%S';
267 $filename = $newfiletmp .
'.' . $newfileformat;
269 $file = $dir .
'/' . $filename;
276 if (!is_writable($conf->mrp->dir_temp)) {
277 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->mrp->dir_temp);
278 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
284 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
285 if (count($arrayidcontact) > 0) {
287 $result = $object->fetch_contact($arrayidcontact[0]);
291 $contactobject =
null;
292 if (!empty($usecontact)) {
294 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
295 $object->contact->fetch_thirdparty();
296 $socobject = $object->contact->thirdparty;
297 $contactobject = $object->contact;
299 $socobject = $object->thirdparty;
301 $contactobject = $object->contact;
304 $socobject = $object->thirdparty;
308 $substitutionarray = array(
309 '__QTY_TO_PRODUCE__' => $object->qty,
313 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
314 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
318 $paramfreetext =
'MRP_MO_FREE_TEXT';
319 if (!empty($conf->global->$paramfreetext)) {
324 require_once ODTPHP_PATH.
'odf.php';
326 $odfHandler =
new Odf(
329 'PATH_TO_TMP' => $conf->mrp->dir_temp,
331 'DELIMITER_LEFT' =>
'{',
332 'DELIMITER_RIGHT' =>
'}'
336 $this->error = $e->getMessage();
349 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
350 }
catch (OdfException $e) {
363 $array_thirdparty_contact = array();
364 if ($usecontact && is_object($contactobject)) {
368 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
372 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
373 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
375 foreach ($tmparray as $key => $value) {
377 if (preg_match(
'/logo$/', $key)) {
379 if (file_exists($value)) {
380 $odfHandler->setImage($key, $value);
382 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
386 $odfHandler->setVars($key, $value,
true,
'UTF-8');
388 }
catch (OdfException $e) {
395 $foundtagforlines = 1;
397 $listlines = $odfHandler->setSegment(
'lines');
398 }
catch (OdfExceptionSegmentNotFound $e) {
400 $foundtagforlines = 0;
402 }
catch (OdfException $e) {
403 $foundtagforlines = 0;
406 if ($foundtagforlines) {
408 foreach ($object->lines as $line) {
413 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
414 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
415 foreach ($tmparray as $key => $val) {
417 $listlines->setVars($key, $val,
true,
'UTF-8');
418 }
catch (OdfException $e) {
420 }
catch (SegmentException $e) {
426 $odfHandler->mergeSegment($listlines);
428 }
catch (OdfException $e) {
429 $this->error = $e->getMessage();
435 $tmparray = $outputlangs->get_translations_for_substitutions();
436 foreach ($tmparray as $key => $value) {
438 $odfHandler->setVars($key, $value,
true,
'UTF-8');
439 }
catch (OdfException $e) {
446 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
447 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
452 $odfHandler->exportAsAttachedPDF($file);
454 $this->error = $e->getMessage();
455 dol_syslog(
'Error in exportAsAttachedPDF: '.$e->getMessage(), LOG_INFO);
460 $odfHandler->saveToDisk($file);
462 $this->error = $e->getMessage();
463 dol_syslog(
'Error in saveToDisk: '.$e->getMessage(), LOG_INFO);
468 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
469 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
475 $this->result = array(
'fullpath'=>$file);
479 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);