207 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
210 global $user, $langs, $conf, $mysoc, $hookmanager;
212 if (empty($srctemplatepath)) {
213 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
218 if (!is_object($hookmanager)) {
219 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
222 $hookmanager->initHooks(array(
'odtgeneration'));
225 if (!is_object($outputlangs)) {
226 $outputlangs = $langs;
228 $sav_charset_output = $outputlangs->charset_output;
229 $outputlangs->charset_output =
'UTF-8';
231 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
233 if ($conf->reception->dir_output) {
247 $dir = !empty($conf->reception->multidir_output[
$object->entity]) ? $conf->reception->multidir_output[
$object->entity] : $conf->reception->dir_output;
249 if (!preg_match(
'/specimen/i', $objectref)) {
250 $dir .=
"/".$objectref;
252 $file = $dir.
"/".$objectref.
".odt";
254 if (!file_exists($dir)) {
256 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
261 if (file_exists($dir)) {
263 $newfile = basename($srctemplatepath);
264 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
265 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
266 $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->reception->dir_temp)) {
288 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->reception->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;
319 $substitutionarray = array(
320 '__FROM_NAME__' => $this->emetteur->name,
321 '__FROM_EMAIL__' => $this->emetteur->email,
322 '__TOTAL_TTC__' =>
$object->total_ttc,
323 '__TOTAL_HT__' =>
$object->total_ht,
324 '__TOTAL_VAT__' =>
$object->total_tva
329 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
330 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
334 $paramfreetext =
'RECEPTION_FREE_TEXT';
340 require_once ODTPHP_PATH.
'odf.php';
342 $odfHandler =
new Odf(
345 'PATH_TO_TMP' => $conf->reception->dir_temp,
347 'DELIMITER_LEFT' =>
'{',
348 'DELIMITER_RIGHT' =>
'}'
352 $this->error = $e->getMessage();
363 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
364 }
catch (OdfException $e) {
371 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
377 $array_thirdparty_contact = array();
378 if ($usecontact && is_object($contactobject)) {
382 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
386 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
387 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
388 foreach ($tmparray as $key => $value) {
390 if (preg_match(
'/logo$/', $key)) {
391 if (file_exists($value)) {
392 $odfHandler->setImage($key, $value);
394 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
397 $odfHandler->setVars($key, $value,
true,
'UTF-8');
399 }
catch (OdfException $e) {
404 $foundtagforlines = 1;
406 $listlines = $odfHandler->setSegment(
'lines');
407 }
catch (OdfExceptionSegmentNotFound $e) {
409 $foundtagforlines = 0;
412 if ($foundtagforlines) {
414 foreach (
$object->lines as $line) {
419 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
420 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
421 foreach ($tmparray as $key => $val) {
423 $listlines->setVars($key, $val,
true,
'UTF-8');
424 }
catch (SegmentException $e) {
431 $odfHandler->mergeSegment($listlines);
432 }
catch (OdfException $e) {
433 $this->error = $e->getMessage();
440 $tmparray = $outputlangs->get_translations_for_substitutions();
441 foreach ($tmparray as $key => $value) {
443 $odfHandler->setVars($key, $value,
true,
'UTF-8');
444 }
catch (OdfException $e) {
450 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
451 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
456 $odfHandler->exportAsAttachedPDF($file);
458 $this->error = $e->getMessage();
464 $odfHandler->saveToDisk($file);
466 $this->error = $e->getMessage();
471 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
472 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
478 $this->result = array(
'fullpath' => $file);
482 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);