205 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
208 global $user, $langs, $conf, $mysoc, $hookmanager;
210 if (empty($srctemplatepath)) {
211 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
216 if (!is_object($hookmanager)) {
217 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
220 $hookmanager->initHooks(array(
'odtgeneration'));
223 if (!is_object($outputlangs)) {
224 $outputlangs = $langs;
226 $sav_charset_output = $outputlangs->charset_output;
227 $outputlangs->charset_output =
'UTF-8';
230 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
232 if ($conf->contrat->multidir_output[$object->entity]) {
234 if (!is_object($object)) {
236 $object =
new Contrat($this->db);
237 $result = $object->fetch($id);
244 $object->fetch_thirdparty();
246 $dir = $conf->contrat->multidir_output[$object->entity];
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);
267 $newfiletmp = $objectref .
'_' . $newfiletmp;
270 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
273 if ($format ==
'1') {
274 $format =
'%Y%m%d%H%M%S';
278 $filename = $newfiletmp .
'.' . $newfileformat;
280 $file = $dir .
'/' . $filename;
287 if (!is_writable($conf->contrat->dir_temp)) {
288 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->contrat->dir_temp);
289 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
295 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
296 if (count($arrayidcontact) > 0) {
298 $result = $object->fetch_contact($arrayidcontact[0]);
302 $contactobject =
null;
303 if (!empty($usecontact)) {
305 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
306 $object->contact->fetch_thirdparty();
307 $socobject = $object->contact->thirdparty;
308 $contactobject = $object->contact;
310 $socobject = $object->thirdparty;
312 $contactobject = $object->contact;
315 $socobject = $object->thirdparty;
318 $object->fetch_optionals();
330 $array_thirdparty_contact = array();
331 if ($usecontact && is_object($contactobject)) {
335 $substitutionarray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
338 $tmparray = $substitutionarray;
341 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
342 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
346 $paramfreetext =
'CONTRACT_FREE_TEXT';
347 if (!empty($conf->global->$paramfreetext)) {
353 require_once ODTPHP_PATH.
'odf.php';
355 $odfHandler =
new Odf(
358 'PATH_TO_TMP' => $conf->contrat->dir_temp,
360 'DELIMITER_LEFT' =>
'{',
361 'DELIMITER_RIGHT' =>
'}'
365 $this->error = $e->getMessage();
378 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
379 }
catch (OdfException $e) {
384 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
385 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
387 foreach ($tmparray as $key => $value) {
389 if (preg_match(
'/logo$/', $key)) {
391 if (file_exists($value)) {
392 $odfHandler->setImage($key, $value);
394 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
398 $odfHandler->setVars($key, $value,
true,
'UTF-8');
400 }
catch (OdfException $e) {
407 $foundtagforlines = 1;
409 $listlines = $odfHandler->setSegment(
'lines');
410 }
catch (OdfExceptionSegmentNotFound $e) {
412 $foundtagforlines = 0;
414 }
catch (OdfException $e) {
415 $foundtagforlines = 0;
418 if ($foundtagforlines) {
420 foreach ($object->lines as $line) {
425 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
426 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
427 foreach ($tmparray as $key => $val) {
429 $listlines->setVars($key, $val,
true,
'UTF-8');
430 }
catch (OdfException $e) {
432 }
catch (SegmentException $e) {
438 $odfHandler->mergeSegment($listlines);
440 }
catch (OdfException $e) {
441 $this->error = $e->getMessage();
447 $tmparray = $outputlangs->get_translations_for_substitutions();
448 foreach ($tmparray as $key => $value) {
450 $odfHandler->setVars($key, $value,
true,
'UTF-8');
451 }
catch (OdfException $e) {
457 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
458 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
463 $odfHandler->exportAsAttachedPDF($file);
465 $this->error = $e->getMessage();
471 $odfHandler->saveToDisk($file);
473 $this->error = $e->getMessage();
479 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
480 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
486 $this->result = array(
'fullpath'=>$file);
490 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);