210 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
213 global $user, $langs, $conf, $mysoc, $hookmanager;
215 if (empty($srctemplatepath)) {
216 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
221 if (!is_object($hookmanager)) {
222 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
225 $hookmanager->initHooks(array(
'odtgeneration'));
228 if (!is_object($outputlangs)) {
229 $outputlangs = $langs;
231 $sav_charset_output = $outputlangs->charset_output;
232 $outputlangs->charset_output =
'UTF-8';
234 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
236 if ($conf->reception->dir_output) {
238 if (!is_object($object)) {
241 $result = $object->fetch($id);
248 $object->fetch_thirdparty();
250 $dir = !empty($conf->reception->multidir_output[$object->entity]) ? $conf->reception->multidir_output[$object->entity] : $conf->reception->dir_output;
252 if (!preg_match(
'/specimen/i', $objectref)) {
253 $dir .=
"/".$objectref;
255 $file = $dir.
"/".$objectref.
".odt";
257 if (!file_exists($dir)) {
259 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
264 if (file_exists($dir)) {
266 $newfile = basename($srctemplatepath);
267 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
268 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
269 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
270 $newfiletmp = $objectref .
'_' . $newfiletmp;
273 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
276 if ($format ==
'1') {
277 $format =
'%Y%m%d%H%M%S';
281 $filename = $newfiletmp .
'.' . $newfileformat;
283 $file = $dir .
'/' . $filename;
290 if (!is_writable($conf->reception->dir_temp)) {
291 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->reception->dir_temp);
292 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
298 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
299 if (count($arrayidcontact) > 0) {
301 $result = $object->fetch_contact($arrayidcontact[0]);
305 $contactobject =
null;
306 if (!empty($usecontact)) {
308 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT))) {
309 $object->contact->fetch_thirdparty();
310 $socobject = $object->contact->thirdparty;
311 $contactobject = $object->contact;
313 $socobject = $object->thirdparty;
315 $contactobject = $object->contact;
318 $socobject = $object->thirdparty;
322 $substitutionarray = array(
323 '__FROM_NAME__' => $this->emetteur->name,
324 '__FROM_EMAIL__' => $this->emetteur->email,
325 '__TOTAL_TTC__' => $object->total_ttc,
326 '__TOTAL_HT__' => $object->total_ht,
327 '__TOTAL_VAT__' => $object->total_tva
332 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
333 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
337 $paramfreetext =
'RECEPTION_FREE_TEXT';
338 if (!empty($conf->global->$paramfreetext)) {
343 require_once ODTPHP_PATH.
'odf.php';
345 $odfHandler =
new Odf(
348 'PATH_TO_TMP' => $conf->reception->dir_temp,
349 'ZIP_PROXY' =>
'PclZipProxy',
350 'DELIMITER_LEFT' =>
'{',
351 'DELIMITER_RIGHT' =>
'}'
355 $this->error = $e->getMessage();
368 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
369 }
catch (OdfException $e) {
382 $array_thirdparty_contact = array();
383 if ($usecontact && is_object($contactobject)) {
387 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
391 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
392 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
393 foreach ($tmparray as $key => $value) {
395 if (preg_match(
'/logo$/', $key)) {
396 if (file_exists($value)) {
397 $odfHandler->setImage($key, $value);
399 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
403 $odfHandler->setVars($key, $value,
true,
'UTF-8');
405 }
catch (OdfException $e) {
411 $foundtagforlines = 1;
413 $listlines = $odfHandler->setSegment(
'lines');
414 }
catch (OdfException $e) {
416 $foundtagforlines = 0;
419 if ($foundtagforlines) {
421 foreach ($object->lines as $line) {
426 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
427 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
428 foreach ($tmparray as $key => $val) {
430 $listlines->setVars($key, $val,
true,
'UTF-8');
431 }
catch (OdfException $e) {
433 }
catch (SegmentException $e) {
439 $odfHandler->mergeSegment($listlines);
441 }
catch (OdfException $e) {
442 $this->error = $e->getMessage();
448 $tmparray = $outputlangs->get_translations_for_substitutions();
449 foreach ($tmparray as $key => $value) {
451 $odfHandler->setVars($key, $value,
true,
'UTF-8');
452 }
catch (OdfException $e) {
458 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
459 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
462 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
464 $odfHandler->exportAsAttachedPDF($file);
466 $this->error = $e->getMessage();
472 $odfHandler->saveToDisk($file);
474 $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);