201 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
204 global $user, $langs, $conf, $mysoc, $hookmanager;
206 if (empty($srctemplatepath)) {
207 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
212 if (!is_object($hookmanager)) {
213 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
216 $hookmanager->initHooks(array(
'odtgeneration'));
219 if (!is_object($outputlangs)) {
220 $outputlangs = $langs;
222 $sav_charset_output = $outputlangs->charset_output;
223 $outputlangs->charset_output =
'UTF-8';
225 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
227 if ($conf->reception->dir_output) {
229 if (!is_object($object)) {
232 $result = $object->fetch($id);
239 $object->fetch_thirdparty();
241 $dir = !empty($conf->reception->multidir_output[$object->entity]) ? $conf->reception->multidir_output[$object->entity] : $conf->reception->dir_output;
243 if (!preg_match(
'/specimen/i', $objectref)) {
244 $dir .=
"/".$objectref;
246 $file = $dir.
"/".$objectref.
".odt";
248 if (!file_exists($dir)) {
250 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
255 if (file_exists($dir)) {
257 $newfile = basename($srctemplatepath);
258 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
259 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
260 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
261 $newfiletmp = $objectref .
'_' . $newfiletmp;
264 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
267 if ($format ==
'1') {
268 $format =
'%Y%m%d%H%M%S';
272 $filename = $newfiletmp .
'.' . $newfileformat;
274 $file = $dir .
'/' . $filename;
281 if (!is_writable($conf->reception->dir_temp)) {
282 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->reception->dir_temp);
283 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
289 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
290 if (count($arrayidcontact) > 0) {
292 $result = $object->fetch_contact($arrayidcontact[0]);
296 $contactobject =
null;
297 if (!empty($usecontact)) {
299 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
300 $object->contact->fetch_thirdparty();
301 $socobject = $object->contact->thirdparty;
302 $contactobject = $object->contact;
304 $socobject = $object->thirdparty;
306 $contactobject = $object->contact;
309 $socobject = $object->thirdparty;
313 $substitutionarray = array(
314 '__FROM_NAME__' => $this->emetteur->name,
315 '__FROM_EMAIL__' => $this->emetteur->email,
316 '__TOTAL_TTC__' => $object->total_ttc,
317 '__TOTAL_HT__' => $object->total_ht,
318 '__TOTAL_VAT__' => $object->total_tva
323 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
324 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
328 $paramfreetext =
'RECEPTION_FREE_TEXT';
329 if (!empty($conf->global->$paramfreetext)) {
334 require_once ODTPHP_PATH.
'odf.php';
336 $odfHandler =
new Odf(
339 'PATH_TO_TMP' => $conf->reception->dir_temp,
341 'DELIMITER_LEFT' =>
'{',
342 'DELIMITER_RIGHT' =>
'}'
346 $this->error = $e->getMessage();
359 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
360 }
catch (OdfException $e) {
373 $array_thirdparty_contact = array();
374 if ($usecontact && is_object($contactobject)) {
378 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
382 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
383 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
384 foreach ($tmparray as $key => $value) {
386 if (preg_match(
'/logo$/', $key)) {
387 if (file_exists($value)) {
388 $odfHandler->setImage($key, $value);
390 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
393 $odfHandler->setVars($key, $value,
true,
'UTF-8');
395 }
catch (OdfException $e) {
401 $foundtagforlines = 1;
403 $listlines = $odfHandler->setSegment(
'lines');
404 }
catch (OdfExceptionSegmentNotFound $e) {
406 $foundtagforlines = 0;
408 }
catch (OdfException $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 (OdfException $e) {
426 }
catch (SegmentException $e) {
432 $odfHandler->mergeSegment($listlines);
434 }
catch (OdfException $e) {
435 $this->error = $e->getMessage();
441 $tmparray = $outputlangs->get_translations_for_substitutions();
442 foreach ($tmparray as $key => $value) {
444 $odfHandler->setVars($key, $value,
true,
'UTF-8');
445 }
catch (OdfException $e) {
451 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
452 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
457 $odfHandler->exportAsAttachedPDF($file);
459 $this->error = $e->getMessage();
465 $odfHandler->saveToDisk($file);
467 $this->error = $e->getMessage();
472 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
473 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
479 $this->result = array(
'fullpath'=>$file);
483 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);