203 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
206 global $user, $langs, $conf, $mysoc, $hookmanager;
208 if (empty($srctemplatepath)) {
209 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
214 if (!is_object($hookmanager)) {
215 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
218 $hookmanager->initHooks(array(
'odtgeneration'));
221 if (!is_object($outputlangs)) {
222 $outputlangs = $langs;
224 $sav_charset_output = $outputlangs->charset_output;
225 $outputlangs->charset_output =
'UTF-8';
228 $outputlangs->loadLangs(array(
"main",
"companies",
"bills",
"dict"));
230 if ($conf->ticket->dir_output) {
232 if (!is_object($object)) {
234 $object =
new User($this->db);
235 $result = $object->fetch($id);
242 $object->fetch_thirdparty();
244 $dir = $conf->ticket->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->ticket->dir_temp)) {
286 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->ticket->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 if (!empty($usecontact)) {
302 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))) {
303 $object->contact->fetch_thirdparty();
304 $socobject = $object->contact->thirdparty;
305 $contactobject = $object->contact;
307 $socobject = $object->thirdparty;
309 $contactobject = $object->contact;
312 $socobject = $object->thirdparty;
316 require_once ODTPHP_PATH.
'odf.php';
318 $odfHandler =
new Odf(
321 'PATH_TO_TMP' => $conf->ticket->dir_temp,
322 'ZIP_PROXY' =>
'PclZipProxy',
323 'DELIMITER_LEFT' =>
'{',
324 'DELIMITER_RIGHT' =>
'}'
328 $this->error = $e->getMessage();
339 $array_thirdparty_contact = array();
340 if ($usecontact && is_object($contactobject)) {
344 $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
346 $object->fetch_optionals();
348 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
349 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
350 foreach ($tmparray as $key => $value) {
352 if (preg_match(
'/logo$/', $key)) {
353 if (file_exists($value)) {
354 $odfHandler->setImage($key, $value);
356 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
360 $odfHandler->setVars($key, $value,
true,
'UTF-8');
362 }
catch (OdfException $e) {
368 $tmparray = $outputlangs->get_translations_for_substitutions();
369 foreach ($tmparray as $key => $value) {
371 $odfHandler->setVars($key, $value,
true,
'UTF-8');
372 }
catch (OdfException $e) {
378 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
379 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
382 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
384 $odfHandler->exportAsAttachedPDF($file);
386 $this->error = $e->getMessage();
392 $odfHandler->saveToDisk($file);
394 $this->error = $e->getMessage();
400 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
406 $this->result = array(
'fullpath'=>$file);
410 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);