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) {
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) {
357 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
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) {
394 $foundtagforlines = 1;
396 $listlines = $odfHandler->setSegment(
'lines');
397 }
catch (OdfExceptionSegmentNotFound $e) {
399 $foundtagforlines = 0;
402 if ($foundtagforlines) {
404 foreach (
$object->lines as $line) {
409 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
410 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
411 foreach ($tmparray as $key => $val) {
413 $listlines->setVars($key, $val,
true,
'UTF-8');
414 }
catch (SegmentException $e) {
421 $odfHandler->mergeSegment($listlines);
422 }
catch (OdfException $e) {
423 $this->error = $e->getMessage();
430 $tmparray = $outputlangs->get_translations_for_substitutions();
431 foreach ($tmparray as $key => $value) {
433 $odfHandler->setVars($key, $value,
true,
'UTF-8');
434 }
catch (OdfException $e) {
441 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
442 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
447 $odfHandler->exportAsAttachedPDF($file);
449 $this->error = $e->getMessage();
450 dol_syslog(
'Error in exportAsAttachedPDF: '.$e->getMessage(), LOG_INFO);
455 $odfHandler->saveToDisk($file);
457 $this->error = $e->getMessage();
458 dol_syslog(
'Error in saveToDisk: '.$e->getMessage(), LOG_INFO);
463 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
464 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
470 $this->result = array(
'fullpath'=>$file);
474 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);