202 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
205 global $user, $langs, $conf, $mysoc, $hookmanager;
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';
226 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
228 if ($conf->reception->dir_output) {
242 $dir = !empty($conf->reception->multidir_output[
$object->entity]) ? $conf->reception->multidir_output[
$object->entity] : $conf->reception->dir_output;
244 if (!preg_match(
'/specimen/i', $objectref)) {
245 $dir .=
"/".$objectref;
247 $file = $dir.
"/".$objectref.
".odt";
249 if (!file_exists($dir)) {
251 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
256 if (file_exists($dir)) {
258 $newfile = basename($srctemplatepath);
259 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
260 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
261 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
262 $newfiletmp = $objectref .
'_' . $newfiletmp;
265 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
268 if ($format ==
'1') {
269 $format =
'%Y%m%d%H%M%S';
273 $filename = $newfiletmp .
'.' . $newfileformat;
275 $file = $dir .
'/' . $filename;
282 if (!is_writable($conf->reception->dir_temp)) {
283 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->reception->dir_temp);
284 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
290 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
291 if (count($arrayidcontact) > 0) {
293 $result =
$object->fetch_contact($arrayidcontact[0]);
297 $contactobject =
null;
298 if (!empty($usecontact)) {
300 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
301 $object->contact->fetch_thirdparty();
302 $socobject =
$object->contact->thirdparty;
303 $contactobject =
$object->contact;
305 $socobject =
$object->thirdparty;
307 $contactobject =
$object->contact;
310 $socobject =
$object->thirdparty;
314 $substitutionarray = array(
315 '__FROM_NAME__' => $this->emetteur->name,
316 '__FROM_EMAIL__' => $this->emetteur->email,
317 '__TOTAL_TTC__' =>
$object->total_ttc,
318 '__TOTAL_HT__' =>
$object->total_ht,
319 '__TOTAL_VAT__' =>
$object->total_tva
324 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
325 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
329 $paramfreetext =
'RECEPTION_FREE_TEXT';
335 require_once ODTPHP_PATH.
'odf.php';
337 $odfHandler =
new Odf(
340 'PATH_TO_TMP' => $conf->reception->dir_temp,
341 'ZIP_PROXY' =>
'PclZipProxy',
342 'DELIMITER_LEFT' =>
'{',
343 'DELIMITER_RIGHT' =>
'}'
347 $this->error = $e->getMessage();
358 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
359 }
catch (OdfException $e) {
366 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
372 $array_thirdparty_contact = array();
373 if ($usecontact && is_object($contactobject)) {
377 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
381 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
382 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
383 foreach ($tmparray as $key => $value) {
385 if (preg_match(
'/logo$/', $key)) {
386 if (file_exists($value)) {
387 $odfHandler->setImage($key, $value);
389 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
392 $odfHandler->setVars($key, $value,
true,
'UTF-8');
394 }
catch (OdfException $e) {
399 $foundtagforlines = 1;
401 $listlines = $odfHandler->setSegment(
'lines');
402 }
catch (OdfExceptionSegmentNotFound $e) {
404 $foundtagforlines = 0;
407 if ($foundtagforlines) {
409 foreach (
$object->lines as $line) {
414 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
415 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
416 foreach ($tmparray as $key => $val) {
418 $listlines->setVars($key, $val,
true,
'UTF-8');
419 }
catch (SegmentException $e) {
426 $odfHandler->mergeSegment($listlines);
427 }
catch (OdfException $e) {
428 $this->error = $e->getMessage();
435 $tmparray = $outputlangs->get_translations_for_substitutions();
436 foreach ($tmparray as $key => $value) {
438 $odfHandler->setVars($key, $value,
true,
'UTF-8');
439 }
catch (OdfException $e) {
445 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
446 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
451 $odfHandler->exportAsAttachedPDF($file);
453 $this->error = $e->getMessage();
459 $odfHandler->saveToDisk($file);
461 $this->error = $e->getMessage();
466 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
467 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
473 $this->result = array(
'fullpath' => $file);
477 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);