193 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
196 global $user, $langs, $conf, $mysoc, $hookmanager;
198 if (empty($srctemplatepath)) {
199 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
204 if (!is_object($hookmanager)) {
205 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
208 $hookmanager->initHooks(array(
'odtgeneration'));
211 if (!is_object($outputlangs)) {
212 $outputlangs = $langs;
214 $sav_charset_output = $outputlangs->charset_output;
215 $outputlangs->charset_output =
'UTF-8';
217 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
219 if ($conf->asset->dir_output) {
221 if (!is_object($object)) {
223 $object =
new Asset($this->db);
224 $result = $object->fetch($id);
231 $object->fetch_thirdparty();
233 $dir = $conf->asset->multidir_output[isset($object->entity) ? $object->entity : 1];
235 if (!preg_match(
'/specimen/i', $objectref)) {
236 $dir .=
"/".$objectref;
238 $file = $dir.
"/".$objectref.
".odt";
240 if (!file_exists($dir)) {
242 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
247 if (file_exists($dir)) {
249 $newfile = basename($srctemplatepath);
250 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
251 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
252 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
253 $newfiletmp = $objectref.
'_'.$newfiletmp;
256 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
259 if ($format ==
'1') {
260 $format =
'%Y%m%d%H%M%S';
264 $filename = $newfiletmp.
'.'.$newfileformat;
266 $file = $dir.
'/'.$filename;
273 if (!is_writable($conf->asset->dir_temp)) {
274 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->asset->dir_temp);
275 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
281 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
282 if (count($arrayidcontact) > 0) {
284 $result = $object->fetch_contact($arrayidcontact[0]);
288 $contactobject =
null;
289 if (!empty($usecontact)) {
291 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
292 $object->contact->fetch_thirdparty();
293 $socobject = $object->contact->thirdparty;
294 $contactobject = $object->contact;
296 $socobject = $object->thirdparty;
298 $contactobject = $object->contact;
301 $socobject = $object->thirdparty;
305 $substitutionarray = array(
306 '__FROM_NAME__' => $this->emetteur->name,
307 '__FROM_EMAIL__' => $this->emetteur->email,
308 '__TOTAL_TTC__' => $object->total_ttc,
309 '__TOTAL_HT__' => $object->total_ht,
310 '__TOTAL_VAT__' => $object->total_tva
314 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
315 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
319 $paramfreetext =
'ORDER_FREE_TEXT';
320 if (!empty($conf->global->$paramfreetext)) {
325 require_once ODTPHP_PATH.
'odf.php';
327 $odfHandler =
new Odf(
330 'PATH_TO_TMP' => $conf->asset->dir_temp,
332 'DELIMITER_LEFT' =>
'{',
333 'DELIMITER_RIGHT' =>
'}'
337 $this->error = $e->getMessage();
350 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
351 }
catch (OdfException $e) {
364 $array_thirdparty_contact = array();
365 if ($usecontact && is_object($contactobject)) {
369 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
373 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
374 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
376 foreach ($tmparray as $key => $value) {
378 if (preg_match(
'/logo$/', $key)) {
380 if (file_exists($value)) {
381 $odfHandler->setImage($key, $value);
383 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
387 $odfHandler->setVars($key, $value,
true,
'UTF-8');
389 }
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();
460 $odfHandler->saveToDisk($file);
462 $this->error = $e->getMessage();
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);