212 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
215 global $product, $langs, $conf, $mysoc, $hookmanager, $user;
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';
237 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
239 if ($conf->product->dir_output) {
241 if (!is_object($object)) {
243 $object =
new Product($this->db);
244 $result = $object->fetch($id);
251 $supplierprices = $productFournisseur->list_product_fournisseur_price($object->id);
252 $object->supplierprices = $supplierprices;
254 $dir = $conf->product->dir_output;
256 if (!preg_match(
'/specimen/i', $objectref)) {
257 $dir .=
"/".$objectref;
259 $file = $dir.
"/".$objectref.
".odt";
261 if (!file_exists($dir)) {
263 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
268 if (file_exists($dir)) {
270 $newfile = basename($srctemplatepath);
271 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
272 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
273 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
275 $newfiletmp = $objectref .
'_' . $newfiletmp;
278 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
281 if ($format ==
'1') {
282 $format =
'%Y%m%d%H%M%S';
286 $filename = $newfiletmp .
'.' . $newfileformat;
288 $file = $dir .
'/' . $filename;
295 if (!is_writable($conf->product->dir_temp)) {
296 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->product->dir_temp);
297 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
303 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
304 if (count($arrayidcontact) > 0) {
306 $result = $object->fetch_contact($arrayidcontact[0]);
310 $contactobject =
null;
311 if (!empty($usecontact)) {
313 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))) {
314 $object->contact->fetch_thirdparty();
315 $socobject = $object->contact->thirdparty;
316 $contactobject = $object->contact;
318 $socobject = $object->thirdparty;
320 $contactobject = $object->contact;
323 $socobject = $object->thirdparty;
326 $substitutionarray = array(
327 '__FROM_NAME__' => $this->emetteur->name,
328 '__FROM_EMAIL__' => $this->emetteur->email,
329 '__TOTAL_TTC__' => $object->total_ttc,
330 '__TOTAL_HT__' => $object->total_ht,
331 '__TOTAL_VAT__' => $object->total_tva
335 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
336 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
340 $paramfreetext =
'PRODUCT_FREE_TEXT';
341 if (!empty($conf->global->$paramfreetext)) {
346 require_once ODTPHP_PATH.
'odf.php';
348 $odfHandler =
new Odf(
351 'PATH_TO_TMP' => $conf->product->dir_temp,
352 'ZIP_PROXY' =>
'PclZipProxy',
353 'DELIMITER_LEFT' =>
'{',
354 'DELIMITER_RIGHT' =>
'}'
358 $this->error = $e->getMessage();
371 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
372 }
catch (OdfException $e) {
385 $array_thirdparty_contact = array();
386 if ($usecontact && is_object($contactobject)) {
390 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
394 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
395 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
397 foreach ($tmparray as $key => $value) {
399 if (preg_match(
'/logo$/', $key)) {
400 if (file_exists($value)) {
401 $odfHandler->setImage($key, $value);
403 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
407 $odfHandler->setVars($key, $value,
true,
'UTF-8');
409 }
catch (OdfException $e) {
415 $listlines = $odfHandler->setSegment(
'supplierprices');
416 if (!empty($object->supplierprices)) {
417 foreach ($object->supplierprices as $supplierprice) {
421 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$array_lines,
'line'=>$supplierprice);
422 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
423 foreach ($array_lines as $key => $val) {
425 $listlines->setVars($key, $val,
true,
'UTF-8');
426 }
catch (OdfException $e) {
428 }
catch (SegmentException $e) {
435 $odfHandler->mergeSegment($listlines);
436 }
catch (OdfException $e) {
437 $this->error = $e->getMessage();
443 $tmparray = $outputlangs->get_translations_for_substitutions();
444 foreach ($tmparray as $key => $value) {
446 $odfHandler->setVars($key, $value,
true,
'UTF-8');
447 }
catch (OdfException $e) {
453 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
454 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
457 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
459 $odfHandler->exportAsAttachedPDF($file);
461 $this->error = $e->getMessage();
467 $odfHandler->saveToDisk($file);
469 $this->error = $e->getMessage();
475 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
476 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
482 $this->result = array(
'fullpath'=>$file);
486 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);