213 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
216 global $stock, $langs, $conf, $mysoc, $hookmanager, $user;
218 if (empty($srctemplatepath)) {
219 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
224 if (!is_object($hookmanager)) {
225 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
228 $hookmanager->initHooks(array(
'odtgeneration'));
231 if (!is_object($outputlangs)) {
232 $outputlangs = $langs;
234 $sav_charset_output = $outputlangs->charset_output;
235 $outputlangs->charset_output =
'UTF-8';
238 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
240 if ($conf->product->dir_output) {
242 if (!is_object($object)) {
245 $result = $object->fetch($id);
253 $supplierprices = $stockFournisseur->list_stock_fournisseur_price($object->id);
254 $object->supplierprices = $supplierprices;
256 $dir = $conf->product->dir_output;
258 if (!preg_match(
'/specimen/i', $objectref)) {
259 $dir .=
"/".$objectref;
261 $file = $dir.
"/".$objectref.
".odt";
263 if (!file_exists($dir)) {
265 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
270 if (file_exists($dir)) {
272 $newfile = basename($srctemplatepath);
273 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
274 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
275 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
277 $newfiletmp = $objectref .
'_' . $newfiletmp;
280 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
283 if ($format ==
'1') {
284 $format =
'%Y%m%d%H%M%S';
288 $filename = $newfiletmp .
'.' . $newfileformat;
290 $file = $dir .
'/' . $filename;
297 if (!is_writable($conf->product->dir_temp)) {
298 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->product->dir_temp);
299 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
305 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
306 if (count($arrayidcontact) > 0) {
308 $result = $object->fetch_contact($arrayidcontact[0]);
312 $contactobject =
null;
313 if (!empty($usecontact)) {
315 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))) {
316 $object->contact->fetch_thirdparty();
317 $socobject = $object->contact->thirdparty;
318 $contactobject = $object->contact;
320 $socobject = $object->thirdparty;
322 $contactobject = $object->contact;
325 $socobject = $object->thirdparty;
328 $substitutionarray = array(
329 '__FROM_NAME__' => $this->emetteur->name,
330 '__FROM_EMAIL__' => $this->emetteur->email,
331 '__TOTAL_TTC__' => $object->total_ttc,
332 '__TOTAL_HT__' => $object->total_ht,
333 '__TOTAL_VAT__' => $object->total_tva
337 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
338 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
342 $paramfreetext =
'stock_FREE_TEXT';
343 if (!empty($conf->global->$paramfreetext)) {
348 require_once ODTPHP_PATH.
'odf.php';
350 $odfHandler =
new Odf(
353 'PATH_TO_TMP' => $conf->product->dir_temp,
354 'ZIP_PROXY' =>
'PclZipProxy',
355 'DELIMITER_LEFT' =>
'{',
356 'DELIMITER_RIGHT' =>
'}'
360 $this->error = $e->getMessage();
370 $object->fetch_optionals();
374 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
375 }
catch (OdfException $e) {
388 $array_thirdparty_contact = array();
389 if ($usecontact && is_object($contactobject)) {
393 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
397 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
398 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
400 foreach ($tmparray as $key => $value) {
402 if (preg_match(
'/logo$/', $key)) {
403 if (file_exists($value)) {
404 $odfHandler->setImage($key, $value);
406 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
410 $odfHandler->setVars($key, $value,
true,
'UTF-8');
412 }
catch (OdfException $e) {
418 $listlines = $odfHandler->setSegment(
'supplierprices');
419 if (!empty($object->supplierprices)) {
420 foreach ($object->supplierprices as $supplierprice) {
424 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$array_lines,
'line'=>$supplierprice);
425 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
426 foreach ($array_lines as $key => $val) {
428 $listlines->setVars($key, $val,
true,
'UTF-8');
429 }
catch (OdfException $e) {
431 }
catch (SegmentException $e) {
438 $odfHandler->mergeSegment($listlines);
439 }
catch (OdfException $e) {
440 $this->error = $e->getMessage();
446 $tmparray = $outputlangs->get_translations_for_substitutions();
447 foreach ($tmparray as $key => $value) {
449 $odfHandler->setVars($key, $value,
true,
'UTF-8');
450 }
catch (OdfException $e) {
456 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
457 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
460 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
462 $odfHandler->exportAsAttachedPDF($file);
464 $this->error = $e->getMessage();
470 $odfHandler->saveToDisk($file);
472 $this->error = $e->getMessage();
478 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
484 $this->result = array(
'fullpath'=>$file);
488 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);