227 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
230 global $user, $langs, $conf, $mysoc, $hookmanager;
232 if (empty($srctemplatepath)) {
233 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
238 if (!is_object($hookmanager)) {
239 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
242 $hookmanager->initHooks(array(
'odtgeneration'));
245 if (!is_object($outputlangs)) {
246 $outputlangs = $langs;
248 $sav_charset_output = $outputlangs->charset_output;
249 $outputlangs->charset_output =
'UTF-8';
252 $outputlangs->loadLangs(array(
"main",
"companies",
"bills",
"dict"));
254 if ($conf->supplier_proposal->dir_output) {
256 if (!is_object($object)) {
259 $result = $object->fetch($id);
266 $object->fetch_thirdparty();
268 $dir = $conf->supplier_proposal->dir_output;
270 if (!preg_match(
'/specimen/i', $objectref)) {
271 $dir .=
"/".$objectref;
273 $file = $dir.
"/".$objectref.
".odt";
275 if (!file_exists($dir)) {
277 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
282 if (file_exists($dir)) {
284 $newfile = basename($srctemplatepath);
285 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
286 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
287 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
289 $newfiletmp = $objectref .
'_' . $newfiletmp;
292 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
295 if ($format ==
'1') {
296 $format =
'%Y%m%d%H%M%S';
300 $filename = $newfiletmp .
'.' . $newfileformat;
302 $file = $dir .
'/' . $filename;
308 dol_mkdir($conf->supplier_proposal->dir_temp);
309 if (!is_writable($conf->supplier_proposal->dir_temp)) {
310 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->supplier_proposal->dir_temp);
311 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
317 $arrayidcontact = $object->getIdContact(
'external',
'BILLING');
318 if (count($arrayidcontact) > 0) {
320 $result = $object->fetch_contact($arrayidcontact[0]);
324 $contactobject =
null;
325 if (!empty($usecontact)) {
327 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
328 $object->contact->fetch_thirdparty();
329 $socobject = $object->contact->thirdparty;
330 $contactobject = $object->contact;
332 $socobject = $object->thirdparty;
334 $contactobject = $object->contact;
337 $socobject = $object->thirdparty;
341 $substitutionarray = array(
342 '__FROM_NAME__' => $this->emetteur->name,
343 '__FROM_EMAIL__' => $this->emetteur->email,
344 '__TOTAL_TTC__' => $object->total_ttc,
345 '__TOTAL_HT__' => $object->total_ht,
346 '__TOTAL_VAT__' => $object->total_tva
350 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
351 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
355 $paramfreetext =
'SUPPLIER_PROPOSAL_FREE_TEXT';
356 if (!empty($conf->global->$paramfreetext)) {
361 require_once ODTPHP_PATH.
'odf.php';
363 $odfHandler =
new Odf(
366 'PATH_TO_TMP' => $conf->supplier_proposal->dir_temp,
368 'DELIMITER_LEFT' =>
'{',
369 'DELIMITER_RIGHT' =>
'}'
373 $this->error = $e->getMessage();
386 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
387 }
catch (OdfException $e) {
399 $array_thirdparty_contact = array();
400 if ($usecontact && is_object($contactobject)) {
404 $tmparray = array_merge($substitutionarray, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
408 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
409 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
411 foreach ($tmparray as $key => $value) {
413 if (preg_match(
'/logo$/', $key)) {
414 if (file_exists($value)) {
415 $odfHandler->setImage($key, $value);
417 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
420 $odfHandler->setVars($key, $value,
true,
'UTF-8');
422 }
catch (OdfException $e) {
428 $foundtagforlines = 1;
430 $listlines = $odfHandler->setSegment(
'lines');
431 }
catch (OdfExceptionSegmentNotFound $e) {
433 $foundtagforlines = 0;
435 }
catch (OdfException $e) {
436 $foundtagforlines = 0;
439 if ($foundtagforlines) {
441 foreach ($object->lines as $line) {
446 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
447 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
448 foreach ($tmparray as $key => $val) {
450 $listlines->setVars($key, $val,
true,
'UTF-8');
451 }
catch (OdfException $e) {
453 }
catch (SegmentException $e) {
459 $odfHandler->mergeSegment($listlines);
461 }
catch (OdfException $e) {
462 $this->error = $e->getMessage();
468 $tmparray = $outputlangs->get_translations_for_substitutions();
469 foreach ($tmparray as $key => $value) {
471 $odfHandler->setVars($key, $value,
true,
'UTF-8');
472 }
catch (OdfException $e) {
478 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
479 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
484 $odfHandler->exportAsAttachedPDF($file);
486 $this->error = $e->getMessage();
492 $odfHandler->saveToDisk($file);
494 $this->error = $e->getMessage();
499 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
500 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
506 $this->result = array(
'fullpath'=>$file);
510 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);