212 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
215 global $user, $langs,
$conf, $mysoc, $hookmanager;
217 if (empty($srctemplatepath)) {
218 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
223 if (!is_object($hookmanager)) {
224 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
227 $hookmanager->initHooks(array(
'odtgeneration'));
230 if (!is_object($outputlangs)) {
231 $outputlangs = $langs;
233 $sav_charset_output = $outputlangs->charset_output;
234 $outputlangs->charset_output =
'UTF-8';
236 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
238 if (
$conf->fournisseur->commande->dir_output) {
242 $dir =
$conf->fournisseur->commande->dir_output;
243 $file = $dir.
"/SPECIMEN.pdf";
247 $dir =
$conf->fournisseur->commande->dir_output.
'/'.$objectref;
248 $file = $dir.
"/".$objectref.
".pdf";
250 $file = $dir.
"/".$objectref.($objectrefsupplier ?
"_".$objectrefsupplier :
"").
".pdf";
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->fournisseur->commande->dir_temp)) {
288 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->fournisseur->commande->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)) {
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->issuer->name,
321 '__FROM_EMAIL__' => $this->issuer->email,
322 '__TOTAL_TTC__' =>
$object->total_ttc,
323 '__TOTAL_HT__' =>
$object->total_ht,
324 '__TOTAL_VAT__' =>
$object->total_tva
328 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
329 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
332 $paramfreetext =
'ORDER_FREE_TEXT';
338 require_once ODTPHP_PATH.
'odf.php';
340 $odfHandler =
new Odf(
343 'PATH_TO_TMP' =>
$conf->fournisseur->dir_temp,
345 'DELIMITER_LEFT' =>
'{',
346 'DELIMITER_RIGHT' =>
'}'
350 $this->error = $e->getMessage();
360 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
361 }
catch (OdfException $e) {
368 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
374 $array_thirdparty_contact = array();
375 if ($usecontact && is_object($contactobject)) {
379 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
383 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
384 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
386 foreach ($tmparray as $key => $value) {
388 if (preg_match(
'/logo$/', $key)) {
389 if (file_exists($value)) {
390 $odfHandler->setImage($key, $value);
392 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
395 $odfHandler->setVars($key, $value,
true,
'UTF-8');
397 }
catch (OdfException $e) {
402 $foundtagforlines = 1;
404 $listlines = $odfHandler->setSegment(
'lines');
405 }
catch (OdfExceptionSegmentNotFound $e) {
407 $foundtagforlines = 0;
410 if ($foundtagforlines) {
412 foreach (
$object->lines as $line) {
417 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
418 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
419 foreach ($tmparray as $key => $val) {
421 $listlines->setVars($key, $val,
true,
'UTF-8');
422 }
catch (SegmentException $e) {
429 $odfHandler->mergeSegment($listlines);
430 }
catch (OdfException $e) {
431 $this->error = $e->getMessage();
438 $tmparray = $outputlangs->get_translations_for_substitutions();
439 foreach ($tmparray as $key => $value) {
441 $odfHandler->setVars($key, $value,
true,
'UTF-8');
442 }
catch (OdfException $e) {
449 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
450 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
455 $odfHandler->exportAsAttachedPDF($file);
457 $this->error = $e->getMessage();
463 $odfHandler->saveToDisk($file);
465 $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);