202 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
205 global $product, $langs, $conf, $mysoc, $hookmanager, $user;
207 if (empty($srctemplatepath)) {
208 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
213 if (!is_object($hookmanager)) {
214 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
217 $hookmanager->initHooks(array(
'odtgeneration'));
220 if (!is_object($outputlangs)) {
221 $outputlangs = $langs;
223 $sav_charset_output = $outputlangs->charset_output;
224 $outputlangs->charset_output =
'UTF-8';
227 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
229 if ($conf->product->dir_output) {
231 if (!is_object($object)) {
233 $object =
new Product($this->db);
234 $result = $object->fetch($id);
241 $supplierprices = $productFournisseur->list_product_fournisseur_price($object->id);
242 $object->supplierprices = $supplierprices;
244 $dir = $conf->product->dir_output;
246 if (!preg_match(
'/specimen/i', $objectref)) {
247 $dir .=
"/".$objectref;
249 $file = $dir.
"/".$objectref.
".odt";
251 if (!file_exists($dir)) {
253 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
258 if (file_exists($dir)) {
260 $newfile = basename($srctemplatepath);
261 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
262 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
263 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
265 $newfiletmp = $objectref .
'_' . $newfiletmp;
268 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
271 if ($format ==
'1') {
272 $format =
'%Y%m%d%H%M%S';
276 $filename = $newfiletmp .
'.' . $newfileformat;
278 $file = $dir .
'/' . $filename;
285 if (!is_writable($conf->product->dir_temp)) {
286 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->product->dir_temp);
287 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
293 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
294 if (count($arrayidcontact) > 0) {
296 $result = $object->fetch_contact($arrayidcontact[0]);
300 $contactobject =
null;
301 if (!empty($usecontact)) {
303 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
304 $object->contact->fetch_thirdparty();
305 $socobject = $object->contact->thirdparty;
306 $contactobject = $object->contact;
308 $socobject = $object->thirdparty;
310 $contactobject = $object->contact;
313 $socobject = $object->thirdparty;
316 $substitutionarray = array(
317 '__FROM_NAME__' => $this->emetteur->name,
318 '__FROM_EMAIL__' => $this->emetteur->email,
319 '__TOTAL_TTC__' => $object->total_ttc,
320 '__TOTAL_HT__' => $object->total_ht,
321 '__TOTAL_VAT__' => $object->total_tva
325 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
326 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
330 $paramfreetext =
'PRODUCT_FREE_TEXT';
331 if (!empty($conf->global->$paramfreetext)) {
336 require_once ODTPHP_PATH.
'odf.php';
338 $odfHandler =
new Odf(
341 'PATH_TO_TMP' => $conf->product->dir_temp,
343 'DELIMITER_LEFT' =>
'{',
344 'DELIMITER_RIGHT' =>
'}'
348 $this->error = $e->getMessage();
361 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
362 }
catch (OdfException $e) {
375 $array_thirdparty_contact = array();
376 if ($usecontact && is_object($contactobject)) {
380 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
384 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
385 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
387 foreach ($tmparray as $key => $value) {
389 if (preg_match(
'/logo$/', $key)) {
390 if (file_exists($value)) {
391 $odfHandler->setImage($key, $value);
393 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
396 $odfHandler->setVars($key, $value,
true,
'UTF-8');
398 }
catch (OdfException $e) {
404 $listlines = $odfHandler->setSegment(
'supplierprices');
405 if (!empty($object->supplierprices)) {
406 foreach ($object->supplierprices as $supplierprice) {
410 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$array_lines,
'line'=>$supplierprice);
411 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
412 foreach ($array_lines as $key => $val) {
414 $listlines->setVars($key, $val,
true,
'UTF-8');
415 }
catch (OdfException $e) {
417 }
catch (SegmentException $e) {
424 $odfHandler->mergeSegment($listlines);
425 }
catch (OdfException $e) {
426 $this->error = $e->getMessage();
432 $tmparray = $outputlangs->get_translations_for_substitutions();
433 foreach ($tmparray as $key => $value) {
435 $odfHandler->setVars($key, $value,
true,
'UTF-8');
436 }
catch (OdfException $e) {
442 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
443 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
448 $odfHandler->exportAsAttachedPDF($file);
450 $this->error = $e->getMessage();
456 $odfHandler->saveToDisk($file);
458 $this->error = $e->getMessage();
464 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
465 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
471 $this->result = array(
'fullpath'=>$file);
475 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);